views:

45

answers:

5

I am writing a unit test for some thread locking logic, so as to make the test more likely to fail quickly; I wish to have all the threads switch between each other very often and at random times.

I know this will not prove we don’t have any bugs, but at least it should make the bugs show up more often.


Thanks to everyone saying "don't do this", but I have very simple locking that should never fail and we only have unit tests in place at present. Anything to reduce the risk of someone removing the lockings without understanding what they are doing is worth while.

+1  A: 

Take a look at CHESS.

Mauricio Scheffer
Two people posting the same answer is not a reason to downvote.
Sapph
@Steven: take a look at the times. This answer came first.
Mauricio Scheffer
@Mauricio: I'm sorry. I'm unable to upvote the message again. I'll make it up to you on another thread.
Steven
+1  A: 

I beg to differ that randomly shifting threads will test your code more efficiently. The best-written multithreaded code can be made to deadlock if you thrash it hard enough.

I can't be more specific without knowing what you're doing, but generally, I think load testing is a more effective way to test thread locking logic.

Dave Swersky
+2  A: 

This question is not about a thread shaker, but about testing threads in general. Have a look at the recommended tools. One commentor linked this other question, which could be interesting, too.

tanascius
+1  A: 

It might be worth your time to investigate CHESS over at MS Research.

Sapph
Why the downvote...?
Sapph
A: 

I strongly advise you not to do this. This will make your unit tests untrustworthy and will render them useless. A unit test should always have the same result (when the code doesn't change). When such a test is unpredictable, developers will not look at it anymore and it than became a waste writing this unit test.

Testing this can be useful, but not as a unit test. It seems more like a special test to me.

[UPDATE] Look at this answer in this thread. The writer feels the same about multi threaded unit tests, but he explains it much better than I did.

Steven