Junit for concurrency testing

I am trying to check java.util.concurrent.ConcurrentLinkedQueue when accessing through multiple threads. Below is my Junit test using RepeatedTest to run in two parallel threads. My questions are: is it correct to use RepeatedTest to test concurrency, for example, on ConcurrentLinkedQueue? The source code is listed 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); } 
+6
java concurrency junit
source share
2 answers

JUnitPerf uses RepeatedTest to test parallel code, so it seems reasonable to use it to run the same test with the above test:

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

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

See also: Parallel Device Testing Code

+7
source share

You can never run tests to check concurrency problems. The fact that no problem appears on a specific test machine (on a given OS, with a certain number of cores or processors, or even with other processes running at the same time) does not mean that there are no problems.

-one
source share

All Articles