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.Test;
14  import org.junit.experimental.ParallelComputer;
15  import org.junit.runner.JUnitCore;
16  import org.junit.runner.Result;
17  
18  public class ParallelClassTest {
19      private static final long TIMEOUT = 15;
20      private static volatile Thread fExample1One = null;
21      private static volatile Thread fExample1Two = null;
22      private static volatile Thread fExample2One = null;
23      private static volatile Thread fExample2Two = null;
24      private static volatile CountDownLatch fSynchronizer;
25  
26      public static class Example1 {
27          @Test
28          public void one() throws InterruptedException {
29              fSynchronizer.countDown();
30              assertTrue(fSynchronizer.await(TIMEOUT, TimeUnit.SECONDS));
31              fExample1One = Thread.currentThread();
32          }
33  
34          @Test
35          public void two() throws InterruptedException {
36              fSynchronizer.countDown();
37              assertTrue(fSynchronizer.await(TIMEOUT, TimeUnit.SECONDS));
38              fExample1Two = Thread.currentThread();
39          }
40      }
41  
42      public static class Example2 {
43          @Test
44          public void one() throws InterruptedException {
45              fSynchronizer.countDown();
46              assertTrue(fSynchronizer.await(TIMEOUT, TimeUnit.SECONDS));
47              fExample2One = Thread.currentThread();
48          }
49  
50          @Test
51          public void two() throws InterruptedException {
52              fSynchronizer.countDown();
53              assertTrue(fSynchronizer.await(TIMEOUT, TimeUnit.SECONDS));
54              fExample2Two = Thread.currentThread();
55          }
56      }
57  
58      @Before
59      public void init() {
60          fExample1One = null;
61          fExample1Two = null;
62          fExample2One = null;
63          fExample2Two = null;
64          fSynchronizer = new CountDownLatch(2);
65      }
66  
67      @Test
68      public void testsRunInParallel() {
69          Result result = JUnitCore.runClasses(ParallelComputer.classes(), Example1.class, Example2.class);
70          assertTrue(result.wasSuccessful());
71          assertNotNull(fExample1One);
72          assertNotNull(fExample1Two);
73          assertNotNull(fExample2One);
74          assertNotNull(fExample2Two);
75          assertThat(fExample1One, is(fExample1Two));
76          assertThat(fExample2One, is(fExample2Two));
77          assertThat(fExample1One, is(not(fExample2One)));
78      }
79  }