Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write multi-threaded unit tests?

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?

like image 819
MRalwasser Avatar asked Jul 15 '10 13:07

MRalwasser


People also ask

What is multithreaded testing?

Multithread testing: Multithreaded testing is where concurrent transactions of an application are running at the same time. It can run on a single machine or distribute across multiple machines.

Is Pytest multithreaded?

This plugin makes it possible to run tests quickly using multiprocessing (parallelism) and multithreading (concurrency).

How do you perform a thread test?

Threads are checked separately and then tested incrementally as subsystem and then performed as whole system. There are generally two types of thread testing i.e. Single thread testing: One application transaction done at a time is called single thread testing.

Why is testing multithreaded concurrent code so difficult?

Testing a multithreaded application is more difficult than testing a single-threaded application because defects are often timing-related and more difficult to reproduce. Existing code often requires significant re-architecting to take advantage of multithreading and multicontexting.


1 Answers

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.

like image 55
mdma Avatar answered Oct 01 '22 17:10

mdma