VS Unit Tests, LINQ and multithreading RRS feed

  • Question

  • Afternoon everyone,

    I've come across a strange scenario during unit testing involving VS unit tests, LINQ to SQL and multi-threading. I have a library that simply parses xml and spawns multiple threads (num determined by the number of nodes) that in turn do some futher parsing of child nodes, and communicate with a DB using LINQ. Each thread spawned creates a new instance of the same class that encapsulates the DB communication logic. There is no need for locking or waiting as the processing in each thead is completely independent from each other.

    When using unit tests to test this process of parsing xml > spawning new threads with delegate to method that creates a new instance of the same class for each child node > this class reading from the DB > each of the threads fall out of the execution of the code and complete wihout any errors whenever a LINQ select query is exectued (e.g. Entity e = db.Entities.First(e => e.Id = id);

    I've tried this using the ThreadPool and using Thread t = new Thead(new ParamaterizedThreadStart(MethodName)) with the same results. I also changed the code to spawn just one thread with the same results.

    I replaced the LINQ query with a SqlCommand and the problem was resolved (but obviously I do not want to use SqlCommand). But the most confusing part of the whole scenario is when I executed the unit test code from a console application and there were no problems at all, everything executed smoothly.

    Anyone any idea what is going on here? Do unit tests have issues exectuing multithreaded LINQ data access code?

    Monday, July 27, 2009 9:48 AM