forked from junit-team/junit4
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request junit-team#508 from Tibor17/junit.issues
Improved Test: ParallelMethodTest fails few times
- Loading branch information
Showing
1 changed file
with
31 additions
and
23 deletions.
There are no files selected for viewing
54 changes: 31 additions & 23 deletions
54
src/test/java/org/junit/tests/experimental/parallel/ParallelMethodTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,52 @@ | ||
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.hamcrest.Description; | ||
import org.hamcrest.Matcher; | ||
import org.hamcrest.TypeSafeMatcher; | ||
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; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class ParallelMethodTest { | ||
private static final long TIMEOUT= 15; | ||
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 { | ||
Thread.sleep(1000); | ||
fSynchronizer.countDown(); | ||
assertTrue(fSynchronizer.await(TIMEOUT, TimeUnit.SECONDS)); | ||
fOne= Thread.currentThread(); | ||
} | ||
@Test public void two() throws InterruptedException { | ||
Thread.sleep(1000); | ||
fSynchronizer.countDown(); | ||
assertTrue(fSynchronizer.await(TIMEOUT, TimeUnit.SECONDS)); | ||
fTwo= Thread.currentThread(); | ||
} | ||
} | ||
|
||
@Before public void init() { | ||
fOne= null; | ||
fTwo= null; | ||
} | ||
|
||
@Test public void testsRunInParallel() { | ||
long start= System.currentTimeMillis(); | ||
Result result= JUnitCore.runClasses(ParallelComputer.methods(), | ||
Example.class); | ||
Result result= JUnitCore.runClasses(ParallelComputer.methods(), Example.class); | ||
assertTrue(result.wasSuccessful()); | ||
long end= System.currentTimeMillis(); | ||
assertThat(end - start, betweenInclusive(1000, 1900)); | ||
} | ||
|
||
private Matcher<Long> betweenInclusive(final long min, final long max) { | ||
return new TypeSafeMatcher<Long>() { | ||
@Override | ||
public boolean matchesSafely(Long item) { | ||
return item >= min && item <= max; | ||
} | ||
|
||
public void describeTo(Description description) { | ||
description.appendText("between " + min + " and " + max); | ||
} | ||
}; | ||
assertNotNull(fOne); | ||
assertNotNull(fTwo); | ||
assertThat(fOne, is(not(fTwo))); | ||
} | ||
} |