How to model concurrency in unit tests?

This question could lead to enough content to fill a book.

In general, I would not recommend adding unit tests to your classes for concurrent scenarios. With practice I think you'll learn that automated unit tests have one or several "sweet spots" -- and that focusing your efforts in these areas (and using other practices in other areas) yields better ROI.

But your class seems to be about concurrency (can't tell for certain based on info provided), and therefore this could be a case where you really want a concurrency-simulating test.

You can (as far as I know) start up multiple threads in a unit test if you wish. But there may be a simpler way. Consider leveraging the Compose Method pattern. While we're on the subject -- this pattern is pretty critical to effective unit tests over all, not just with concurrency.


Unfortunately, concurrency is still an area of unit testing that is challenging to structure easily. It's not a simple problem, and still requires that you implement some of your own synchronization and concurrency logic in the test.

For the example you provide, it may be impossible to write a test that conclusively demonstrates that a method will or won't block under certain conditions. You may be able to achieve some level of confidence by first creating worst-case circumstances where you would expect the blocking behavior - and then write tests to determine if that occurs or not.

Here's a blog article that discusses the topic; it focuses on NUnit.