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

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

3 comments:

Anonymous said...

I tried to use it, but it seems it is not compatible with JUnit 4.7+ ?

Unknown said...

Yes, it's not compatible with the newest version of JUnit. A major upgrade is required. I might be able to find a time to upgrade it this month.

Unknown said...

Created a new version at: http://sourceforge.net/projects/amino-cbbs/files/cbbs%20junit%20extention/0.1/cbbs-junit-ext-0.4.1.jar/download

Please try and let me know if any problem