Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write an automated test for thread safety

I have a class which is not thread safe:

class Foo { 
    /* Abstract base class, code which is not thread safe */ 
};

Moreover, if you have foo1 and foo2 objects, you cannot call foo1->someFunc() until foo2->anotherFunc() has returned (this can happen with two threads). This is the situation and it can't be changed (a Foo subclass is actually a wrapper for a python script).

In order to prevent unwanted calls I've created the following -

class FooWrapper {
public:
    FooWrapper(Foo* foo, FooWrappersMutex* mutex);
    /* Wrapped functions from Foo */
};

Internally, FooWrapper wraps calls to the Foo functions with the shared mutex.

I want to test FooWrapper for thread safety. My biggest problem is the fact that threads are managed by the operating system, which means I've got less control on their execution. What I would like to test is the following scenario:

  • Thread 1 calls fooWrapper1->someFunc() and blocks while inside the function
  • Thread 2 calls fooWrapper2->anotherFunc() and returns immediately (since someFunc() is still executing)
  • Thread 1 finishes the execution

What is the simplest to test a scenario like this automatically?

I'm using QT on Win32, although I would prefer a solution which is at least cross-platform as QT is.

like image 433
kshahar Avatar asked Dec 27 '08 11:12

kshahar


People also ask

How do you make a test thread-safe?

Tests for thread safety differ from typical single-threaded tests. 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.

How do you test multithreading?

To test multi-thread functionality, let the multiple instances of the application or program to be tested be active at the same time. Run the multi-thread program on different hardware. Thread testing is a form of session testing for which sessions are formed of threads.

How do you test concurrency code?

One good way to test this is to make sure you have access to a multi-cpu machine, then run your test for as long a time/with as many iterations as possible. For example if you have a multithreaded producer consumer algorithm, fill the queue with a good sized list and use 2x as many threads as you might in production.

How do you write a thread for Junit?

Spawn some threads. Have the main thread wait or sleep. Perform assertions from within the worker threads (which via ConcurrentUnit, are reported back to the main thread) Resume the main thread from one of the worker threads once all assertions are complete.


1 Answers

You might want to check out CHESS: A Systematic Testing Tool for Concurrent Software by Microsoft Research. It is a testing framework for multithreaded programs (both .NET and native code).

If I understood that correctly, it replaces the operating system's threading libraries with its own, so that it can control thread switching. Then it analyzes the program to figure out every possible way that the execution streams of the threads can interleave and it re-runs the test suite for every possible interleaving.

like image 194
Jörg W Mittag Avatar answered Oct 09 '22 21:10

Jörg W Mittag