views:

162

answers:

3

I'd like to know if there are some unit testing frameworks which are capable of writing multi-threaded tests easily?

I would imagine something like: invoke a special test method by n threads at the same time for m times. After all test threads finished, an assertion method where some constraints should be validated would be invoked.

My current approach is to create Thread objects inside a junit test method, loop manually the real test cases inside each run() method, wait for all threads and then validate the assertions. But using this, I have a large boilerplate code block for each test.

What are your experiences?

+8  A: 

There is ConTest, and also GroboUtils.

I've used GroboUtils many years ago, and it did the job. ConTest is newer, and would be my preferred starting point now, since rather than just relying on trial and error, the instrumentation forces specific interleavings of the threads, providing a deterministic test. In contrast, GroboUtils MultiThreadedTestRunner simply runs the tests and hopes the scheduler produces an interleaving that causes the thread bug to appear.

EDIT: See also ConcuTest which also forces interleavings and is free.

mdma
ConTest looks good, but seems not to be freely available (I can only find a trial).
MRalwasser
There's also ConcuTest, which I've added as an edit.
mdma
+2  A: 

There is also MultithreadedTC by Bill Pugh of FindBugs fame.

Holger Hoffstätte
+1: I've used it before, and found it very good at generating specific interleavings and asserting properties of your implementation.
Andrzej Doyle
A: 

Just using the concurrency libraries would simplify your code. You can turn your boiler plate code into one method.

Something like

public static void runAll(int times, Runnable... tests) {

}
Peter Lawrey
This does not answer the question. Yes, the code would be simpler, and some risks eliminated, but still it should be tested somehow.
Péter Török