Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any satisfactory approaches to unit testing thread safety in Java?

I am looking at improving a package that I believe not to be threadsafe when its input is shared between multiple worker threads. According to TDD principles, I should write some tests that fail in the first instance, and these would certainly be useful in assessing the problem.

I realise that this is not a simple thing to acheive, and that naively, multi-threaded tests will be nondeterministic as the operating system will determine scheduling and the exact order that various operations are interleaved. I have looked at and used MultithreadedTC in the past, and this was useful. However, in that case I knew in advance exactly where the existing implementation fell down, and thus was able to cook up a nice set of tests that covered it.

However, if you're not at the point where you know exactly what the problem is, is there a good way of going about writing a test that stands a good chance of throwing up any potential problems? Are there any libraries that others have found helpful? Would I be right in thinking that from a purist point of view, a multi-threaded test case should just be the same calls and assertions as the usual single-threaded test, only run with multiple worker threads as appropriate?

Any offers on tools/best practices/philosophy in general would be welcome.

like image 598
Andrzej Doyle Avatar asked Jan 08 '09 14:01

Andrzej Doyle


People also ask

How can we achieve thread-safety in Java?

Using Atomic Variable Using an atomic variable is another way to achieve thread-safety in java. When variables are shared by multiple threads, the atomic variable ensures that threads don't crash into each other.

What strategies we can use to achieve thread-safety?

The best way to achieve thread safety is to avoid shared state. For the state, you need to share you can either use message parsing together with immutable classes or the concurrent data structures together with synchronized blocks and volatile fields.

What is thread-safety and tell 2 ways to achieve thread-safety in Java?

1) Immutable objects are by default thread-safe because their state can not be modified once created. Since String is immutable in Java, it's inherently thread-safe. 2) Read-only or final variables in Java are also thread-safe in Java. 3) Locking is one way of achieving thread-safety in Java.

How do you prove thread-safety?

To test if the combination of two methods, a and b, is thread-safe, call them from two different threads. Put the complete test in a while loop iterating over all thread interleavings with the help from the class AllInterleavings from vmlens. Test if the result is either an after b or b after a.


1 Answers

Java Concurrency in Practice has some great information about how to write tests for concurrency issues. However they are not true unit tests. It is nearly impossible to write a true unit test for a concurrency issue.

Basically it boils down to this. Create a bunch of test threads and start them. Each thread should

  • wait for a count down latch
  • repeatedly call some method that modifies the mutable state in question
  • count down on a second latch and exit

The junit thread creates all the threads and starts them, then counts down on the first latch once to let them all go, then waits for the second latch, then makes some assertions about the mutable state.

Even more so than other types of bugs, it's easier to write a failing unit test for a concurrency bug after you have found the bug.

like image 169
Craig P. Motlin Avatar answered Sep 29 '22 17:09

Craig P. Motlin