forked from junit-team/junit4
/
ParallelMethodTest.java
50 lines (44 loc) · 1.38 KB
/
ParallelMethodTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package org.junit.tests.experimental.parallel;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.junit.experimental.ParallelComputer;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.Before;
import org.junit.BeforeClass;
import java.util.concurrent.CountDownLatch;
public class ParallelMethodTest {
private static volatile Thread fOne= null;
private static volatile Thread fTwo= null;
public static class Example {
private static volatile CountDownLatch fSynchronizer;
@BeforeClass public static void init() {
fSynchronizer= new CountDownLatch(2);
}
@Test public void one() throws InterruptedException {
fSynchronizer.countDown();
fSynchronizer.await();
fOne= Thread.currentThread();
}
@Test public void two() throws InterruptedException {
fSynchronizer.countDown();
fSynchronizer.await();
fTwo= Thread.currentThread();
}
}
@Before public void init() {
fOne= null;
fTwo= null;
}
@Test public void testsRunInParallel() {
Result result= JUnitCore.runClasses(ParallelComputer.methods(), Example.class);
assertTrue(result.wasSuccessful());
assertNotNull(fOne);
assertNotNull(fTwo);
assertThat(fOne, is(not(fTwo)));
}
}