As multi-core becomes main stream, it seems inevitable we finally need to do unit tests in parallel. In order to create a parallel test case, developers need to control the mess of multiple threads themselves, which is not interesting and error-prone. Additionally, if exceptions are thrown from child threads, JUnit will silently ignore them.
Here I gave an example on how our JUnit extension works for a parallel data structure.
@RunWith(Parallelized.class)@ParallelSetting(threadNumber = { 1, 2, 4, 8 })
public class TestThreaded {
Set
strSet;
@Before
public void setUp() {
strSet = new LockFreeSet();
}
@Test
public void doNothing() {
}
@InitFor("testThread")
public void putSomeData(int size){
strSet.add("putSomeData");
}
@Threaded
public void testThread(int rank, int size) {
strSet.add("abcde" + rank);
}
@CheckFor("testThread")
public void checkResult(int size) {
assertEquals(size+1, strSet.size());
}
public static void main(String[] args) {
for (int i = 0; i < 10; i++)
JUnitCore.runClasses(TestThreaded.class);
}
}
The explanation of annotations are listed here:
Name | Annotation Argument | Arguments of annotated method | Comments |
@ParallelSetting | We can specify number of threads as a named argument “threadNumber” |
| This annotation can be used to specify parallel settings for whole test case. |
@InitFor | One string argument to specify which method to help | Annotated method should have one int type argument to accept number of threads used by this running. | It's used to mark setup method for multi-threaded test. Please note this is different as @Before since it only works for one test. |
@Threaded | No argument | Two arguments should be used. One for thread number, and one for rank of current thread. | Methods marked with @Threaded will be executed by multiple thread. |
@CheckFor | One string argument to specify which method to check correctness | Annotated method should have one int type argument to accept number of threads used by this running. | It's used to mark a method, which has duty to check the result of execution of @Threaded test case. |
Now we can test our components in parallel without pain:
Reference
- Download Amino JUnit Extension (Apache License v2) from our SourceForge site and try it. Your feedback is welcome!
- Junit version 4.3 and above.