Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #508 from Tibor17/junit.issues

Improved Test: ParallelMethodTest fails few times
  • Loading branch information...
commit 129a5f762b4fb97503801a229061479355c0347a 2 parents 532428d + f71a2d4
@dsaff dsaff authored
View
54 src/test/java/org/junit/tests/experimental/parallel/ParallelMethodTest.java
@@ -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)));
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.