View Javadoc
1   package org.junit.tests.experimental.parallel;
2   
3   import static org.hamcrest.core.Is.is;
4   import static org.hamcrest.core.IsNot.not;
5   import static org.junit.Assert.assertNotNull;
6   import static org.junit.Assert.assertThat;
7   import static org.junit.Assert.assertTrue;
8   
9   import java.util.concurrent.CountDownLatch;
10  import java.util.concurrent.TimeUnit;
11  
12  import org.junit.Before;
13  import org.junit.BeforeClass;
14  import org.junit.Test;
15  import org.junit.experimental.ParallelComputer;
16  import org.junit.runner.JUnitCore;
17  import org.junit.runner.Result;
18  
19  public class ParallelMethodTest {
20      private static final long TIMEOUT = 15;
21      private static volatile Thread fOne = null;
22      private static volatile Thread fTwo = null;
23  
24      public static class Example {
25          private static volatile CountDownLatch fSynchronizer;
26  
27          @BeforeClass
28          public static void init() {
29              fSynchronizer = new CountDownLatch(2);
30          }
31  
32          @Test
33          public void one() throws InterruptedException {
34              fSynchronizer.countDown();
35              assertTrue(fSynchronizer.await(TIMEOUT, TimeUnit.SECONDS));
36              fOne = Thread.currentThread();
37          }
38  
39          @Test
40          public void two() throws InterruptedException {
41              fSynchronizer.countDown();
42              assertTrue(fSynchronizer.await(TIMEOUT, TimeUnit.SECONDS));
43              fTwo = Thread.currentThread();
44          }
45      }
46  
47      @Before
48      public void init() {
49          fOne = null;
50          fTwo = null;
51      }
52  
53      @Test
54      public void testsRunInParallel() {
55          Result result = JUnitCore.runClasses(ParallelComputer.methods(), Example.class);
56          assertTrue(result.wasSuccessful());
57          assertNotNull(fOne);
58          assertNotNull(fTwo);
59          assertThat(fOne, is(not(fTwo)));
60      }
61  }