Thursday, November 27, 2008

Extending JUnit for Testing Parallel Application

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})

publicclassTestThreaded{

SetstrSet;

@Before

publicvoidsetUp(){

strSet=newLockFreeSet();

}

@Test

publicvoiddoNothing(){

}

@InitFor("testThread")

publicvoidputSomeData(intsize){

strSet.add("putSomeData");

}

@Threaded

publicvoidtestThread(intrank,intsize){

strSet.add("abcde"+rank);

}

@CheckFor("testThread")

publicvoidcheckResult(intsize){

assertEquals(size+1,strSet.size());

}

publicstaticvoidmain(String[]args){

for(inti=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.