Extending JUnit Test Parallel programs

Source: Internet
Author: User

What is the difference between testing a parallel program and the past?

With the popularization of multi-core, the development of parallel programs has been put on the agenda. Parallel programs are more likely to go wrong than serial programs. On the one hand, the execution sequence of parallel program has very strong randomness, the sequence of thread staggered execution may be different every time, and as long as a sequence has a problem, the whole program is incorrect. On the other hand, parallel programs are a new area for most programmers, with relatively little experience, which is another factor that can be easily faulted.

That being the case, we need to test our parallel programs and components more carefully. There are already some JUnit extensions that can create multiple threads and run multiple test cases at the same time, speeding up the execution of test case sets, such as P-unit. But for testing parallel programs and components, these features do not meet all of the requirements. Because developers often want to have precise control over the synchronization between multiple threads.

As with the test sequence program, we want to be able to prepare some test data first before testing the parallel operation. Then, start a multiple thread test to perform a different operation. Finally, wait for all threads to finish, verify the correctness of the results. In the second phase, multiple threads can be interleaved in any order. The correctness of the results should not be checked with the execution sequence of the thread.

Standard JUnit captures only the Exception from the main thread. The Exception generated in other threads is silently ignored, allowing us to get "Green Bar" when the child thread is running in error. This is obviously not a test behavior that programmers like, and we want the test results to correctly reflect the results of all the threads running.

This test pattern for parallel programs will continue to be repeated when testing parallel programs. If developers need to recreate these frameworks every time, it is cumbersome and easy to introduce errors. By using the simple extensions described below, you can make testing of parallel programs as simple as sequential programs. This extension does not affect the other features of JUnit and the use of the various IDE's JUnit plug-ins.

Download and use the extended framework

First, we give an example of parallel testing using a new extension.

Example 1. Using JUnit extensions for parallel testing

/**
 * @author Zhi Gan
 *
 */
 @RunWith (Parallelized.class)
 @ParallelSetting(threadNumber = { 1, 2, 4, 8  })
 public class TestThreaded {
 Set<String> strSet;

 @Before
 public void setUp() {
  strSet = new  LockFreeSet();
 }

 @Test
 public void doNothing() {

 }

  @InitFor("testThread")
 public void  putSomeData(int size){
  strSet.add("putSomeData");
 }

  @Threadedpublic void testThread(int rank, int size) {
  //  every thread adds element to set
  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);
 }
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.