Permalink
Browse files

Improved Test: ParallelMethodTest fails few times

  • Loading branch information...
1 parent b9e14f6 commit c236a9e019a814b3542dcfe3a3a61b036c14d252 @Tibor17 committed Sep 20, 2012
Showing with 15 additions and 23 deletions.
  1. +15 −23 src/test/java/org/junit/tests/experimental/parallel/ParallelMethodTest.java
@@ -1,44 +1,36 @@
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;
public class ParallelMethodTest {
+ private static volatile Thread fOne= null;
+ private static volatile Thread fTwo= null;
+
public static class Example {
- @Test public void one() throws InterruptedException {
- Thread.sleep(1000);
+ @Test public void one() {
+ fOne= Thread.currentThread();
}
- @Test public void two() throws InterruptedException {
- Thread.sleep(1000);
+ @Test public void two() {
+ fTwo= Thread.currentThread();
}
}
@Test public void testsRunInParallel() {
- long start= System.currentTimeMillis();
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)));
+ fOne= null;
+ fTwo= null;
}
}

0 comments on commit c236a9e

Please sign in to comment.