views:

163

answers:

4

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?

+3  A: 

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

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.

LBushkin
+2  A: 

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.

Drew Wills
A: 

Have you looked at CHESS at all?

http://research.microsoft.com/en-us/projects/chess/

Conrad Frix
Yes, and that's not exactly what I was looking for. I don't want to prove multithreaded code to be correct but write a test that replicates the behaviour itself.
Dario
A: 

This should be an interesring read -> http://c2.com/cgi-bin/wiki?ExtremeProgrammingChallengeFourteen

baskin