summaryrefslogtreecommitdiffstats
path: root/junit4/src/test/java/org/junit/tests/experimental/parallel/ParallelClassTest.java
blob: 3f9b2f1e68ec203e189981ea8b2fbba9dacf1f20 (plain)
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
51
52
53
54
package org.junit.tests.experimental.parallel;

import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.junit.Test;
import org.junit.experimental.ParallelComputer;
import org.junit.internal.matchers.TypeSafeMatcher;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;


public class ParallelClassTest {

	public static class Example1 {
		@Test public void one() throws InterruptedException {
			Thread.sleep(1000);
		}
	}
	public static class Example2 {
		@Test public void one() throws InterruptedException {
			Thread.sleep(1000);
		}
	}
	
	@RunWith(Suite.class)
	@SuiteClasses({Example1.class, Example2.class})
	public static class ExampleSuite {}
	
	@Test(timeout=1500) public void testsRunInParallel() {
		long start= System.currentTimeMillis();
		Result result= JUnitCore.runClasses(ParallelComputer.classes(), Example1.class, Example2.class);
		assertTrue(result.wasSuccessful());
		long end= System.currentTimeMillis();
		assertThat(end - start, greaterThan(999)); // Overhead could be less than half a millisecond
	}

	private Matcher<Long> greaterThan(final long l) {
		return new TypeSafeMatcher<Long>() {
			@Override
			public boolean matchesSafely(Long item) {
				return item > l;
			}

			public void describeTo(Description description) {
				description.appendText("greater than " + l);
			}
		};
	}
}