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.Sendwill not block” or - “If the channel’s capacity is exceeded,
channel.Sendwill block until a value is read”
Is there an elegant solution to write these assertions?
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.