Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spawning threads in a unit test

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

like image 557
Serve Laurijssen Avatar asked Jul 10 '26 20:07

Serve Laurijssen


1 Answers

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.

like image 57
flindeberg Avatar answered Jul 13 '26 11:07

flindeberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!