40 |
|
import java.util.concurrent.ExecutionException; |
41 |
|
import java.util.concurrent.Executors; |
42 |
|
import java.util.concurrent.ExecutorService; |
43 |
+ |
import java.util.concurrent.ForkJoinPool; |
44 |
|
import java.util.concurrent.Future; |
45 |
|
import java.util.concurrent.RecursiveAction; |
46 |
|
import java.util.concurrent.RecursiveTask; |
68 |
|
* |
69 |
|
* <ol> |
70 |
|
* |
71 |
< |
* <li> All assertions in code running in generated threads must use |
71 |
> |
* <li>All assertions in code running in generated threads must use |
72 |
|
* the forms {@link #threadFail}, {@link #threadAssertTrue}, {@link |
73 |
|
* #threadAssertEquals}, or {@link #threadAssertNull}, (not |
74 |
|
* {@code fail}, {@code assertTrue}, etc.) It is OK (but not |
75 |
|
* particularly recommended) for other code to use these forms too. |
76 |
|
* Only the most typically used JUnit assertion methods are defined |
77 |
< |
* this way, but enough to live with.</li> |
77 |
> |
* this way, but enough to live with. |
78 |
|
* |
79 |
< |
* <li> If you override {@link #setUp} or {@link #tearDown}, make sure |
79 |
> |
* <li>If you override {@link #setUp} or {@link #tearDown}, make sure |
80 |
|
* to invoke {@code super.setUp} and {@code super.tearDown} within |
81 |
|
* them. These methods are used to clear and check for thread |
82 |
< |
* assertion failures.</li> |
82 |
> |
* assertion failures. |
83 |
|
* |
84 |
|
* <li>All delays and timeouts must use one of the constants {@code |
85 |
|
* SHORT_DELAY_MS}, {@code SMALL_DELAY_MS}, {@code MEDIUM_DELAY_MS}, |
90 |
|
* is always discriminable as larger than SHORT and smaller than |
91 |
|
* MEDIUM. And so on. These constants are set to conservative values, |
92 |
|
* but even so, if there is ever any doubt, they can all be increased |
93 |
< |
* in one spot to rerun tests on slower platforms.</li> |
93 |
> |
* in one spot to rerun tests on slower platforms. |
94 |
|
* |
95 |
< |
* <li> All threads generated must be joined inside each test case |
95 |
> |
* <li>All threads generated must be joined inside each test case |
96 |
|
* method (or {@code fail} to do so) before returning from the |
97 |
|
* method. The {@code joinPool} method can be used to do this when |
98 |
< |
* using Executors.</li> |
98 |
> |
* using Executors. |
99 |
|
* |
100 |
|
* </ol> |
101 |
|
* |
102 |
|
* <p><b>Other notes</b> |
103 |
|
* <ul> |
104 |
|
* |
105 |
< |
* <li> Usually, there is one testcase method per JSR166 method |
105 |
> |
* <li>Usually, there is one testcase method per JSR166 method |
106 |
|
* covering "normal" operation, and then as many exception-testing |
107 |
|
* methods as there are exceptions the method can throw. Sometimes |
108 |
|
* there are multiple tests per JSR166 method when the different |
109 |
|
* "normal" behaviors differ significantly. And sometimes testcases |
110 |
|
* cover multiple methods when they cannot be tested in |
111 |
< |
* isolation.</li> |
111 |
> |
* isolation. |
112 |
|
* |
113 |
< |
* <li> The documentation style for testcases is to provide as javadoc |
113 |
> |
* <li>The documentation style for testcases is to provide as javadoc |
114 |
|
* a simple sentence or two describing the property that the testcase |
115 |
|
* method purports to test. The javadocs do not say anything about how |
116 |
< |
* the property is tested. To find out, read the code.</li> |
116 |
> |
* the property is tested. To find out, read the code. |
117 |
|
* |
118 |
< |
* <li> These tests are "conformance tests", and do not attempt to |
118 |
> |
* <li>These tests are "conformance tests", and do not attempt to |
119 |
|
* test throughput, latency, scalability or other performance factors |
120 |
|
* (see the separate "jtreg" tests for a set intended to check these |
121 |
|
* for the most central aspects of functionality.) So, most tests use |
122 |
|
* the smallest sensible numbers of threads, collection sizes, etc |
123 |
< |
* needed to check basic conformance.</li> |
123 |
> |
* needed to check basic conformance. |
124 |
|
* |
125 |
|
* <li>The test classes currently do not declare inclusion in |
126 |
|
* any particular package to simplify things for people integrating |
127 |
< |
* them in TCK test suites.</li> |
127 |
> |
* them in TCK test suites. |
128 |
|
* |
129 |
< |
* <li> As a convenience, the {@code main} of this class (JSR166TestCase) |
130 |
< |
* runs all JSR166 unit tests.</li> |
129 |
> |
* <li>As a convenience, the {@code main} of this class (JSR166TestCase) |
130 |
> |
* runs all JSR166 unit tests. |
131 |
|
* |
132 |
|
* </ul> |
133 |
|
*/ |
186 |
|
return (regex == null) ? null : Pattern.compile(regex); |
187 |
|
} |
188 |
|
|
189 |
< |
protected void runTest() throws Throwable { |
189 |
> |
public void runBare() throws Throwable { |
190 |
|
if (methodFilter == null |
191 |
< |
|| methodFilter.matcher(toString()).find()) { |
192 |
< |
for (int i = 0; i < runsPerTest; i++) { |
193 |
< |
if (profileTests) |
194 |
< |
runTestProfiled(); |
195 |
< |
else |
196 |
< |
super.runTest(); |
197 |
< |
} |
191 |
> |
|| methodFilter.matcher(toString()).find()) |
192 |
> |
super.runBare(); |
193 |
> |
} |
194 |
> |
|
195 |
> |
protected void runTest() throws Throwable { |
196 |
> |
for (int i = 0; i < runsPerTest; i++) { |
197 |
> |
if (profileTests) |
198 |
> |
runTestProfiled(); |
199 |
> |
else |
200 |
> |
super.runTest(); |
201 |
|
} |
202 |
|
} |
203 |
|
|
204 |
|
protected void runTestProfiled() throws Throwable { |
205 |
< |
// Warmup run, notably to trigger all needed classloading. |
206 |
< |
super.runTest(); |
203 |
< |
long t0 = System.nanoTime(); |
204 |
< |
try { |
205 |
> |
for (int i = 0; i < 2; i++) { |
206 |
> |
long startTime = System.nanoTime(); |
207 |
|
super.runTest(); |
208 |
< |
} finally { |
209 |
< |
long elapsedMillis = millisElapsedSince(t0); |
210 |
< |
if (elapsedMillis >= profileThreshold) |
208 |
> |
long elapsedMillis = millisElapsedSince(startTime); |
209 |
> |
if (elapsedMillis < profileThreshold) |
210 |
> |
break; |
211 |
> |
// Never report first run of any test; treat it as a |
212 |
> |
// warmup run, notably to trigger all needed classloading, |
213 |
> |
if (i > 0) |
214 |
|
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); |
215 |
|
} |
216 |
|
} |
384 |
|
"LongAdderTest", |
385 |
|
"SplittableRandomTest", |
386 |
|
"StampedLockTest", |
387 |
+ |
"SubmissionPublisherTest", |
388 |
|
"ThreadLocalRandom8Test", |
389 |
|
}; |
390 |
|
addNamedTestClasses(suite, java8TestClassNames); |
393 |
|
// Java9+ test classes |
394 |
|
if (atLeastJava9()) { |
395 |
|
String[] java9TestClassNames = { |
396 |
< |
// Currently empty |
396 |
> |
// Currently empty, but expecting varhandle tests |
397 |
|
}; |
398 |
|
addNamedTestClasses(suite, java9TestClassNames); |
399 |
|
} |
525 |
|
setDelays(); |
526 |
|
} |
527 |
|
|
528 |
+ |
void tearDownFail(String format, Object... args) { |
529 |
+ |
String msg = toString() + ": " + String.format(format, args); |
530 |
+ |
System.err.println(msg); |
531 |
+ |
printAllStackTraces(); |
532 |
+ |
throw new AssertionFailedError(msg); |
533 |
+ |
} |
534 |
+ |
|
535 |
|
/** |
536 |
|
* Extra checks that get done for all test cases. |
537 |
|
* |
559 |
|
} |
560 |
|
|
561 |
|
if (Thread.interrupted()) |
562 |
< |
throw new AssertionFailedError("interrupt status set in main thread"); |
562 |
> |
tearDownFail("interrupt status set in main thread"); |
563 |
|
|
564 |
|
checkForkJoinPoolThreadLeaks(); |
565 |
|
} |
568 |
|
* Finds missing try { ... } finally { joinPool(e); } |
569 |
|
*/ |
570 |
|
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
571 |
< |
Thread[] survivors = new Thread[5]; |
571 |
> |
Thread[] survivors = new Thread[7]; |
572 |
|
int count = Thread.enumerate(survivors); |
573 |
|
for (int i = 0; i < count; i++) { |
574 |
|
Thread thread = survivors[i]; |
576 |
|
if (name.startsWith("ForkJoinPool-")) { |
577 |
|
// give thread some time to terminate |
578 |
|
thread.join(LONG_DELAY_MS); |
579 |
< |
if (!thread.isAlive()) continue; |
580 |
< |
throw new AssertionFailedError |
581 |
< |
(String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n", |
569 |
< |
toString(), name)); |
579 |
> |
if (thread.isAlive()) |
580 |
> |
tearDownFail("Found leaked ForkJoinPool thread thread=%s", |
581 |
> |
thread); |
582 |
|
} |
583 |
|
} |
584 |
+ |
|
585 |
+ |
if (!ForkJoinPool.commonPool() |
586 |
+ |
.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS)) |
587 |
+ |
tearDownFail("ForkJoin common pool thread stuck"); |
588 |
|
} |
589 |
|
|
590 |
|
/** |
759 |
|
} |
760 |
|
|
761 |
|
/** Like Runnable, but with the freedom to throw anything */ |
762 |
< |
interface Thunk { public void run() throws Throwable; } |
762 |
> |
interface Action { public void run() throws Throwable; } |
763 |
|
|
764 |
|
/** |
765 |
< |
* Runs all the given tasks in parallel, failing if any fail. |
765 |
> |
* Runs all the given actions in parallel, failing if any fail. |
766 |
|
* Useful for running multiple variants of tests that are |
767 |
|
* necessarily individually slow because they must block. |
768 |
|
*/ |
769 |
< |
void testInParallel(Thunk ... thunks) { |
769 |
> |
void testInParallel(Action ... actions) { |
770 |
|
ExecutorService pool = Executors.newCachedThreadPool(); |
771 |
|
try { |
772 |
< |
ArrayList<Future<?>> futures = new ArrayList<>(thunks.length); |
773 |
< |
for (final Thunk thunk : thunks) |
772 |
> |
ArrayList<Future<?>> futures = new ArrayList<>(actions.length); |
773 |
> |
for (final Action action : actions) |
774 |
|
futures.add(pool.submit(new CheckedRunnable() { |
775 |
< |
public void realRun() throws Throwable { thunk.run();}})); |
775 |
> |
public void realRun() throws Throwable { action.run();}})); |
776 |
|
for (Future<?> future : futures) |
777 |
|
try { |
778 |
|
assertNull(future.get(LONG_DELAY_MS, MILLISECONDS)); |
1238 |
|
public static final String TEST_STRING = "a test string"; |
1239 |
|
|
1240 |
|
public static class StringTask implements Callable<String> { |
1241 |
< |
public String call() { return TEST_STRING; } |
1241 |
> |
final String value; |
1242 |
> |
public StringTask() { this(TEST_STRING); } |
1243 |
> |
public StringTask(String value) { this.value = value; } |
1244 |
> |
public String call() { return value; } |
1245 |
|
} |
1246 |
|
|
1247 |
|
public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) { |