Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Improved Test: ParallelMethodTest fails few times

  • Loading branch information...
commit f0209ca61f012925613645accbe56852a831b128 1 parent b9e14f6
Tibor Digana authored
36 src/test/java/org/junit/tests/experimental/parallel/ParallelMethodTest.java
... ... @@ -1,44 +1,34 @@
1 1 package org.junit.tests.experimental.parallel;
2 2
  3 +import static org.junit.Assert.assertNotNull;
3 4 import static org.junit.Assert.assertThat;
4 5 import static org.junit.Assert.assertTrue;
5   -import org.hamcrest.Description;
6   -import org.hamcrest.Matcher;
7   -import org.hamcrest.TypeSafeMatcher;
8 6 import org.junit.Test;
9 7 import org.junit.experimental.ParallelComputer;
10 8 import org.junit.runner.JUnitCore;
11 9 import org.junit.runner.Result;
12 10
13 11 public class ParallelMethodTest {
  12 + private static volatile Thread fOne= null;
  13 + private static volatile Thread fTwo= null;
  14 +
14 15 public static class Example {
15   - @Test public void one() throws InterruptedException {
16   - Thread.sleep(1000);
  16 + @Test public void one() {
  17 + fOne= Thread.currentThread();
17 18 }
18   - @Test public void two() throws InterruptedException {
19   - Thread.sleep(1000);
  19 + @Test public void two() {
  20 + fTwo= Thread.currentThread();
20 21 }
21 22 }
22 23
23 24 @Test public void testsRunInParallel() {
24   - long start= System.currentTimeMillis();
25 25 Result result= JUnitCore.runClasses(ParallelComputer.methods(),
26 26 Example.class);
27 27 assertTrue(result.wasSuccessful());
28   - long end= System.currentTimeMillis();
29   - assertThat(end - start, betweenInclusive(1000, 1900));
30   - }
31   -
32   - private Matcher<Long> betweenInclusive(final long min, final long max) {
33   - return new TypeSafeMatcher<Long>() {
34   - @Override
35   - public boolean matchesSafely(Long item) {
36   - return item >= min && item <= max;
37   - }
38   -
39   - public void describeTo(Description description) {
40   - description.appendText("between " + min + " and " + max);
41   - }
42   - };
  28 + assertNotNull(fOne);
  29 + assertNotNull(fTwo);
  30 + assertThat(fOne, is(not(fTwo)));
  31 + fOne= null;
  32 + fTwo= null;
43 33 }
44 34 }

0 comments on commit f0209ca

Please sign in to comment.
Something went wrong with that request. Please try again.