Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run concurrency unit test?

How to use junit to run concurrency test?

Let's say I have a class

public class MessageBoard {     public synchronized void postMessage(String message)     {         ....     }      public void updateMessage(Long id, String message)     {         ....     } } 

I wan to test multiple access to this postMessage concurrently. Any advice on this? I wish to run this kind of concurrency test against all my setter functions (or any method that involves create/update/delete operation).

like image 542
janetsmith Avatar asked Aug 04 '09 10:08

janetsmith


People also ask

How do you test for concurrency?

Concurrent testing methods Some common methods for performing concurrency testing include the following: Fuzz Test: The tester inserts incorrect random data and monitors the system's response. Random Test: Multiple strands are tested simultaneously by randomizing the test inputs.

How do you unit test synchronized methods?

It is possible to test that a function is sychronised only by its side effects. There is a testable pattern that orgs the code such that you dependency inject the sync rather than using the Jave intrinsic, but if all there is is your original question then I would rely on visual inspection and obvious correctness.

Do jest tests run concurrently?

By the way @trusktr Jest DOES run tests in parallel, just not ones in the same file. So you can run into issues with interference between tests if they are running on the same database.


2 Answers

Unfortunately I don't believe you can definitively prove that your code is thread-safe by using run-time testing. You can throw as many threads as you like against it, and it may/may not pass depending on the scheduling.

Perhaps you should look at some static analysis tools, such as PMD, that can determine how you're using synchronisation and identify usage problems.

like image 184
Brian Agnew Avatar answered Sep 21 '22 22:09

Brian Agnew


I would recommend using MultithreadedTC - Written by the concurrency master himself Bill Pugh (and Nat Ayewah). Quote from their overview:

MultithreadedTC is a framework for testing concurrent applications. It features a metronome that is used to provide fine control over the sequence of activities in multiple threads.

This framework allows you to deterministically test every thread interleaving in separate tests

like image 32
Peter Avatar answered Sep 22 '22 22:09

Peter