Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Junit to test concurrency

I'm trying to test java.util.concurrent.ConcurrentLinkedQueue when accessed via multiple threads. Mentioned below is my Junit test using RepeatedTest to run in two concurrent threads. My questions is: is it correct to use RepeatedTest to test concurrency for example on ConcurrentLinkedQueue? The source code is mentioned below.

Thanks

import java.util.concurrent.ConcurrentLinkedQueue;
import junit.extensions.ActiveTestSuite;
import junit.extensions.RepeatedTest;
import junit.extensions.TestSetup;
import junit.framework.TestCase;

public class TestNonBlockingConcurrentQueue extends TestCase{

private static ConcurrentLinkedQueue clq;

public void testPut() throws Exception {
    int messageCounter = 0;
    for(;messageCounter <10000; messageCounter++){
        clq.offer(messageCounter);
    }
    assertEquals(clq.size(), messageCounter);
}

public void testGet() throws Exception {
    while(!clq.isEmpty()){
        clq.poll();
    }
    assertEquals("Size should be zero", clq.size(), 0);
}

public static junit.framework.Test suite( ) {
    ActiveTestSuite ats = new ActiveTestSuite();

    TestSetup setup = new TestSetup(ats) {
       protected void setUp() throws Exception {
            System.out.println("Creating ConcurrentLinkedQueue..");
            clq = new ConcurrentLinkedQueue();
        }
        protected void tearDown(  ) throws Exception {
            clq = null;
        }
    };
    ats.addTest(new RepeatedTest(new TestNonBlockingConcurrentQueue("testPut"), 2));
    ats.addTest(new RepeatedTest(new TestNonBlockingConcurrentQueue("testGet"), 2));
    return setup;

}

public TestNonBlockingConcurrentQueue(String testName){
    super(testName);
}
like image 553
java_pill Avatar asked Oct 26 '10 17:10

java_pill


People also ask

Do JUnit tests run concurrently?

Once parallel test execution property is enabled, the JUnit Jupiter engine will execute tests in parallel according to the provided configuration with declared synchronization mechanisms.

Is parallel testing possible in JUnit?

By enabling parallel execution, the JUnit engine starts using the ForkJoin thread pool. Next, we need to add a configuration to utilize this thread pool. We need to choose a parallelization strategy. JUnit provides two implementations (dynamic and fixed) and a custom option to create our implementation.

How can I test my multithreaded application?

Test your multithreaded program by having multiple instances of the program active at the same time. If your application allows the number of threads to vary, configure each instance of the program with a different number of threads.


1 Answers

JUnitPerf uses RepeatedTest to test concurrent code so it seems reasonable to use it to do the same thing with your test above, see:

http://www.clarkware.com/software/JUnitPerf.html

There are other methods for unit testing concurrent code, although none of them can really verify that your code is thread safe:

  • Concurrent Runner
  • ConcJunit

Also see: Unit Testing Concurrent Code

like image 116
Jon Avatar answered Nov 14 '22 22:11

Jon