Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to model concurrency in unit tests?

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

  1. "channel.Send will not block" or
  2. "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?

like image 604
Dario Avatar asked Jan 08 '10 17:01

Dario


People also ask

Is junit multithreaded?

Concurrent-junit library helps the users to test the methods for multi threading. It will create threads for testing methods. By default, number of threads created by this library is 4, but we can set the desired number of threads.

Does unit test run in parallel?

By default, unittest-parallel runs unit tests on all CPU cores available. To run your unit tests with coverage, add either the "--coverage" option (for line coverage) or the "--coverage-branch" for line and branch coverage.


2 Answers

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.

like image 163
LBushkin Avatar answered Oct 02 '22 07:10

LBushkin


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.

like image 43
Drew Wills Avatar answered Oct 02 '22 06:10

Drew Wills