How to model concurrency in unit tests?

c#, concurrency, unit-testing, visual-studio

Solution

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.

Problem

I'm pretty new to unit testing and currently experimenting a bit with Visual Studio's testing tools. My question is how to define assertions about concurrent behaviour in these tests. E.g. given a class `BoundedChan<T>` implementing a bounded channel, how can I specify tests like - "`channel.Send` will not block" or - "If the channel's capacity is exceeded, `channel.Send` will block until a value is read" Is there an elegant solution to write these assertions?

Original source

Related problems