37 |
|
import java.util.concurrent.Callable; |
38 |
|
import java.util.concurrent.CountDownLatch; |
39 |
|
import java.util.concurrent.CyclicBarrier; |
40 |
+ |
import java.util.concurrent.ExecutionException; |
41 |
+ |
import java.util.concurrent.Executors; |
42 |
|
import java.util.concurrent.ExecutorService; |
43 |
|
import java.util.concurrent.Future; |
44 |
|
import java.util.concurrent.RecursiveAction; |
67 |
|
* |
68 |
|
* <ol> |
69 |
|
* |
70 |
< |
* <li> All assertions in code running in generated threads must use |
70 |
> |
* <li>All assertions in code running in generated threads must use |
71 |
|
* the forms {@link #threadFail}, {@link #threadAssertTrue}, {@link |
72 |
|
* #threadAssertEquals}, or {@link #threadAssertNull}, (not |
73 |
|
* {@code fail}, {@code assertTrue}, etc.) It is OK (but not |
74 |
|
* particularly recommended) for other code to use these forms too. |
75 |
|
* Only the most typically used JUnit assertion methods are defined |
76 |
< |
* this way, but enough to live with.</li> |
76 |
> |
* this way, but enough to live with. |
77 |
|
* |
78 |
< |
* <li> If you override {@link #setUp} or {@link #tearDown}, make sure |
78 |
> |
* <li>If you override {@link #setUp} or {@link #tearDown}, make sure |
79 |
|
* to invoke {@code super.setUp} and {@code super.tearDown} within |
80 |
|
* them. These methods are used to clear and check for thread |
81 |
< |
* assertion failures.</li> |
81 |
> |
* assertion failures. |
82 |
|
* |
83 |
|
* <li>All delays and timeouts must use one of the constants {@code |
84 |
|
* SHORT_DELAY_MS}, {@code SMALL_DELAY_MS}, {@code MEDIUM_DELAY_MS}, |
89 |
|
* is always discriminable as larger than SHORT and smaller than |
90 |
|
* MEDIUM. And so on. These constants are set to conservative values, |
91 |
|
* but even so, if there is ever any doubt, they can all be increased |
92 |
< |
* in one spot to rerun tests on slower platforms.</li> |
92 |
> |
* in one spot to rerun tests on slower platforms. |
93 |
|
* |
94 |
< |
* <li> All threads generated must be joined inside each test case |
94 |
> |
* <li>All threads generated must be joined inside each test case |
95 |
|
* method (or {@code fail} to do so) before returning from the |
96 |
|
* method. The {@code joinPool} method can be used to do this when |
97 |
< |
* using Executors.</li> |
97 |
> |
* using Executors. |
98 |
|
* |
99 |
|
* </ol> |
100 |
|
* |
101 |
|
* <p><b>Other notes</b> |
102 |
|
* <ul> |
103 |
|
* |
104 |
< |
* <li> Usually, there is one testcase method per JSR166 method |
104 |
> |
* <li>Usually, there is one testcase method per JSR166 method |
105 |
|
* covering "normal" operation, and then as many exception-testing |
106 |
|
* methods as there are exceptions the method can throw. Sometimes |
107 |
|
* there are multiple tests per JSR166 method when the different |
108 |
|
* "normal" behaviors differ significantly. And sometimes testcases |
109 |
|
* cover multiple methods when they cannot be tested in |
110 |
< |
* isolation.</li> |
110 |
> |
* isolation. |
111 |
|
* |
112 |
< |
* <li> The documentation style for testcases is to provide as javadoc |
112 |
> |
* <li>The documentation style for testcases is to provide as javadoc |
113 |
|
* a simple sentence or two describing the property that the testcase |
114 |
|
* method purports to test. The javadocs do not say anything about how |
115 |
< |
* the property is tested. To find out, read the code.</li> |
115 |
> |
* the property is tested. To find out, read the code. |
116 |
|
* |
117 |
< |
* <li> These tests are "conformance tests", and do not attempt to |
117 |
> |
* <li>These tests are "conformance tests", and do not attempt to |
118 |
|
* test throughput, latency, scalability or other performance factors |
119 |
|
* (see the separate "jtreg" tests for a set intended to check these |
120 |
|
* for the most central aspects of functionality.) So, most tests use |
121 |
|
* the smallest sensible numbers of threads, collection sizes, etc |
122 |
< |
* needed to check basic conformance.</li> |
122 |
> |
* needed to check basic conformance. |
123 |
|
* |
124 |
|
* <li>The test classes currently do not declare inclusion in |
125 |
|
* any particular package to simplify things for people integrating |
126 |
< |
* them in TCK test suites.</li> |
126 |
> |
* them in TCK test suites. |
127 |
|
* |
128 |
< |
* <li> As a convenience, the {@code main} of this class (JSR166TestCase) |
129 |
< |
* runs all JSR166 unit tests.</li> |
128 |
> |
* <li>As a convenience, the {@code main} of this class (JSR166TestCase) |
129 |
> |
* runs all JSR166 unit tests. |
130 |
|
* |
131 |
|
* </ul> |
132 |
|
*/ |
198 |
|
} |
199 |
|
|
200 |
|
protected void runTestProfiled() throws Throwable { |
201 |
< |
// Warmup run, notably to trigger all needed classloading. |
202 |
< |
super.runTest(); |
201 |
< |
long t0 = System.nanoTime(); |
202 |
< |
try { |
201 |
> |
for (int i = 0; i < 2; i++) { |
202 |
> |
long startTime = System.nanoTime(); |
203 |
|
super.runTest(); |
204 |
< |
} finally { |
205 |
< |
long elapsedMillis = millisElapsedSince(t0); |
206 |
< |
if (elapsedMillis >= profileThreshold) |
204 |
> |
long elapsedMillis = millisElapsedSince(startTime); |
205 |
> |
if (elapsedMillis < profileThreshold) |
206 |
> |
break; |
207 |
> |
// Never report first run of any test; treat it as a |
208 |
> |
// warmup run, notably to trigger all needed classloading, |
209 |
> |
if (i > 0) |
210 |
|
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); |
211 |
|
} |
212 |
|
} |
284 |
|
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
285 |
|
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
286 |
|
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
287 |
< |
public static boolean atLeastJava9() { return JAVA_CLASS_VERSION >= 53.0; } |
287 |
> |
public static boolean atLeastJava9() { |
288 |
> |
return JAVA_CLASS_VERSION >= 53.0 |
289 |
> |
// As of 2015-09, java9 still uses 52.0 class file version |
290 |
> |
|| JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?(9|[0-9][0-9])$"); |
291 |
> |
} |
292 |
> |
public static boolean atLeastJava10() { |
293 |
> |
return JAVA_CLASS_VERSION >= 54.0 |
294 |
> |
|| JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?[0-9][0-9]$"); |
295 |
> |
} |
296 |
|
|
297 |
|
/** |
298 |
|
* Collects all JSR166 unit tests as one suite. |
380 |
|
"LongAdderTest", |
381 |
|
"SplittableRandomTest", |
382 |
|
"StampedLockTest", |
383 |
+ |
"SubmissionPublisherTest", |
384 |
|
"ThreadLocalRandom8Test", |
385 |
|
}; |
386 |
|
addNamedTestClasses(suite, java8TestClassNames); |
389 |
|
// Java9+ test classes |
390 |
|
if (atLeastJava9()) { |
391 |
|
String[] java9TestClassNames = { |
392 |
< |
"ThreadPoolExecutor9Test", |
392 |
> |
// Currently empty, but expecting varhandle tests |
393 |
|
}; |
394 |
|
addNamedTestClasses(suite, java9TestClassNames); |
395 |
|
} |
731 |
|
/** |
732 |
|
* Waits out termination of a thread pool or fails doing so. |
733 |
|
*/ |
734 |
< |
void joinPool(ExecutorService exec) { |
734 |
> |
void joinPool(ExecutorService pool) { |
735 |
|
try { |
736 |
< |
exec.shutdown(); |
737 |
< |
if (!exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) |
738 |
< |
fail("ExecutorService " + exec + |
736 |
> |
pool.shutdown(); |
737 |
> |
if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) |
738 |
> |
fail("ExecutorService " + pool + |
739 |
|
" did not terminate in a timely manner"); |
740 |
|
} catch (SecurityException ok) { |
741 |
|
// Allowed in case test doesn't have privs |
744 |
|
} |
745 |
|
} |
746 |
|
|
747 |
+ |
/** Like Runnable, but with the freedom to throw anything */ |
748 |
+ |
interface Action { public void run() throws Throwable; } |
749 |
+ |
|
750 |
+ |
/** |
751 |
+ |
* Runs all the given actions in parallel, failing if any fail. |
752 |
+ |
* Useful for running multiple variants of tests that are |
753 |
+ |
* necessarily individually slow because they must block. |
754 |
+ |
*/ |
755 |
+ |
void testInParallel(Action ... actions) { |
756 |
+ |
ExecutorService pool = Executors.newCachedThreadPool(); |
757 |
+ |
try { |
758 |
+ |
ArrayList<Future<?>> futures = new ArrayList<>(actions.length); |
759 |
+ |
for (final Action action : actions) |
760 |
+ |
futures.add(pool.submit(new CheckedRunnable() { |
761 |
+ |
public void realRun() throws Throwable { action.run();}})); |
762 |
+ |
for (Future<?> future : futures) |
763 |
+ |
try { |
764 |
+ |
assertNull(future.get(LONG_DELAY_MS, MILLISECONDS)); |
765 |
+ |
} catch (ExecutionException ex) { |
766 |
+ |
threadUnexpectedException(ex.getCause()); |
767 |
+ |
} catch (Exception ex) { |
768 |
+ |
threadUnexpectedException(ex); |
769 |
+ |
} |
770 |
+ |
} finally { |
771 |
+ |
joinPool(pool); |
772 |
+ |
} |
773 |
+ |
} |
774 |
+ |
|
775 |
|
/** |
776 |
|
* A debugging tool to print all stack traces, as jstack does. |
777 |
|
*/ |
1224 |
|
public static final String TEST_STRING = "a test string"; |
1225 |
|
|
1226 |
|
public static class StringTask implements Callable<String> { |
1227 |
< |
public String call() { return TEST_STRING; } |
1227 |
> |
final String value; |
1228 |
> |
public StringTask() { this(TEST_STRING); } |
1229 |
> |
public StringTask(String value) { this.value = value; } |
1230 |
> |
public String call() { return value; } |
1231 |
|
} |
1232 |
|
|
1233 |
|
public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) { |