3

There is a lot of information on how to unit test threaded code but not on spawning threads inside a unit test method to test synchronization mechanisms.

[TestMethod]
public void TestDiscountThreading() {
  Thread[] threads = new Thread[50];
  for (int i = 0; i < threads.Length; i++) {
    threads[i] = new Thread(PriceThread);
    threads[i].Start();
  }

  for (int i = 0; i < threads.Length; i++)
    threads[i].Join();
}

I want to stresstest if synchronization in the code inside PriceThread is implemented correctly but every single time the method runs I get the error "The agent process was stopped while the test was running". Is it even possible to spawn threads inside unit tests or what can be wrong here?

Im using Visual Studio 2010 with the shipped unit testing framework

Serve Laurijssen
  • 9,266
  • 5
  • 45
  • 98
  • 1
    This doesn't even sound like a unit test tot me. More like load test or functional test. – T.S. Aug 26 '13 at 14:50
  • You can't test that synchronization is working correctly, only discover that it's *not* working correctly. – Daniel Mann Aug 26 '13 at 15:44
  • I agree, its not 100% useful to test synchronization as its unpredictable in a live app how the code runs but a test spawning 100 threads calling the same function should tell something – Serve Laurijssen Aug 27 '13 at 06:02

1 Answers1

1

This is not really a full answer, but I would recommend you to have a look at VS 2010 Test Runner error "The agent process was stopped while the test was running." for ideas.

Do you know where in the code the exception is thrown?

EDIT with answer:

This is the result when a thread other than the main thread throws an unhandled exception. Which was the case here, ie no issues with the loops or the threads themselves.

Community
  • 1
  • 1
flindeberg
  • 4,887
  • 1
  • 24
  • 37
  • there is no exception thrown neither in test code itself nor in the code which is tested – Serve Laurijssen Aug 26 '13 at 13:31
  • after turning on break on exception thrown it showed a null ref exception in the test code itself.....I was so busy with the whole thread thing that I had a blind spot for the normal code. feeling stupid, three months therapy should do the trick – Serve Laurijssen Aug 26 '13 at 13:59
  • Hms, downvote to an answer that helped the OP even if it is not a full answer, wtf? And no comment? – flindeberg Aug 26 '13 at 14:29