15 |
|
import java.io.ObjectOutputStream; |
16 |
|
import java.lang.management.ManagementFactory; |
17 |
|
import java.lang.management.ThreadInfo; |
18 |
+ |
import java.lang.management.ThreadMXBean; |
19 |
|
import java.lang.reflect.Constructor; |
20 |
|
import java.lang.reflect.Method; |
21 |
|
import java.lang.reflect.Modifier; |
41 |
|
import java.util.concurrent.ExecutionException; |
42 |
|
import java.util.concurrent.Executors; |
43 |
|
import java.util.concurrent.ExecutorService; |
44 |
+ |
import java.util.concurrent.ForkJoinPool; |
45 |
|
import java.util.concurrent.Future; |
46 |
|
import java.util.concurrent.RecursiveAction; |
47 |
|
import java.util.concurrent.RecursiveTask; |
69 |
|
* |
70 |
|
* <ol> |
71 |
|
* |
72 |
< |
* <li> All assertions in code running in generated threads must use |
72 |
> |
* <li>All assertions in code running in generated threads must use |
73 |
|
* the forms {@link #threadFail}, {@link #threadAssertTrue}, {@link |
74 |
|
* #threadAssertEquals}, or {@link #threadAssertNull}, (not |
75 |
|
* {@code fail}, {@code assertTrue}, etc.) It is OK (but not |
76 |
|
* particularly recommended) for other code to use these forms too. |
77 |
|
* Only the most typically used JUnit assertion methods are defined |
78 |
< |
* this way, but enough to live with.</li> |
78 |
> |
* this way, but enough to live with. |
79 |
|
* |
80 |
< |
* <li> If you override {@link #setUp} or {@link #tearDown}, make sure |
80 |
> |
* <li>If you override {@link #setUp} or {@link #tearDown}, make sure |
81 |
|
* to invoke {@code super.setUp} and {@code super.tearDown} within |
82 |
|
* them. These methods are used to clear and check for thread |
83 |
< |
* assertion failures.</li> |
83 |
> |
* assertion failures. |
84 |
|
* |
85 |
|
* <li>All delays and timeouts must use one of the constants {@code |
86 |
|
* SHORT_DELAY_MS}, {@code SMALL_DELAY_MS}, {@code MEDIUM_DELAY_MS}, |
91 |
|
* is always discriminable as larger than SHORT and smaller than |
92 |
|
* MEDIUM. And so on. These constants are set to conservative values, |
93 |
|
* but even so, if there is ever any doubt, they can all be increased |
94 |
< |
* in one spot to rerun tests on slower platforms.</li> |
94 |
> |
* in one spot to rerun tests on slower platforms. |
95 |
|
* |
96 |
< |
* <li> All threads generated must be joined inside each test case |
96 |
> |
* <li>All threads generated must be joined inside each test case |
97 |
|
* method (or {@code fail} to do so) before returning from the |
98 |
|
* method. The {@code joinPool} method can be used to do this when |
99 |
< |
* using Executors.</li> |
99 |
> |
* using Executors. |
100 |
|
* |
101 |
|
* </ol> |
102 |
|
* |
103 |
|
* <p><b>Other notes</b> |
104 |
|
* <ul> |
105 |
|
* |
106 |
< |
* <li> Usually, there is one testcase method per JSR166 method |
106 |
> |
* <li>Usually, there is one testcase method per JSR166 method |
107 |
|
* covering "normal" operation, and then as many exception-testing |
108 |
|
* methods as there are exceptions the method can throw. Sometimes |
109 |
|
* there are multiple tests per JSR166 method when the different |
110 |
|
* "normal" behaviors differ significantly. And sometimes testcases |
111 |
|
* cover multiple methods when they cannot be tested in |
112 |
< |
* isolation.</li> |
112 |
> |
* isolation. |
113 |
|
* |
114 |
< |
* <li> The documentation style for testcases is to provide as javadoc |
114 |
> |
* <li>The documentation style for testcases is to provide as javadoc |
115 |
|
* a simple sentence or two describing the property that the testcase |
116 |
|
* method purports to test. The javadocs do not say anything about how |
117 |
< |
* the property is tested. To find out, read the code.</li> |
117 |
> |
* the property is tested. To find out, read the code. |
118 |
|
* |
119 |
< |
* <li> These tests are "conformance tests", and do not attempt to |
119 |
> |
* <li>These tests are "conformance tests", and do not attempt to |
120 |
|
* test throughput, latency, scalability or other performance factors |
121 |
|
* (see the separate "jtreg" tests for a set intended to check these |
122 |
|
* for the most central aspects of functionality.) So, most tests use |
123 |
|
* the smallest sensible numbers of threads, collection sizes, etc |
124 |
< |
* needed to check basic conformance.</li> |
124 |
> |
* needed to check basic conformance. |
125 |
|
* |
126 |
|
* <li>The test classes currently do not declare inclusion in |
127 |
|
* any particular package to simplify things for people integrating |
128 |
< |
* them in TCK test suites.</li> |
128 |
> |
* them in TCK test suites. |
129 |
|
* |
130 |
< |
* <li> As a convenience, the {@code main} of this class (JSR166TestCase) |
131 |
< |
* runs all JSR166 unit tests.</li> |
130 |
> |
* <li>As a convenience, the {@code main} of this class (JSR166TestCase) |
131 |
> |
* runs all JSR166 unit tests. |
132 |
|
* |
133 |
|
* </ul> |
134 |
|
*/ |
187 |
|
return (regex == null) ? null : Pattern.compile(regex); |
188 |
|
} |
189 |
|
|
190 |
< |
protected void runTest() throws Throwable { |
190 |
> |
public void runBare() throws Throwable { |
191 |
|
if (methodFilter == null |
192 |
< |
|| methodFilter.matcher(toString()).find()) { |
193 |
< |
for (int i = 0; i < runsPerTest; i++) { |
194 |
< |
if (profileTests) |
195 |
< |
runTestProfiled(); |
196 |
< |
else |
197 |
< |
super.runTest(); |
198 |
< |
} |
192 |
> |
|| methodFilter.matcher(toString()).find()) |
193 |
> |
super.runBare(); |
194 |
> |
} |
195 |
> |
|
196 |
> |
protected void runTest() throws Throwable { |
197 |
> |
for (int i = 0; i < runsPerTest; i++) { |
198 |
> |
if (profileTests) |
199 |
> |
runTestProfiled(); |
200 |
> |
else |
201 |
> |
super.runTest(); |
202 |
|
} |
203 |
|
} |
204 |
|
|
205 |
|
protected void runTestProfiled() throws Throwable { |
206 |
< |
// Warmup run, notably to trigger all needed classloading. |
207 |
< |
super.runTest(); |
203 |
< |
long t0 = System.nanoTime(); |
204 |
< |
try { |
206 |
> |
for (int i = 0; i < 2; i++) { |
207 |
> |
long startTime = System.nanoTime(); |
208 |
|
super.runTest(); |
209 |
< |
} finally { |
210 |
< |
long elapsedMillis = millisElapsedSince(t0); |
211 |
< |
if (elapsedMillis >= profileThreshold) |
209 |
> |
long elapsedMillis = millisElapsedSince(startTime); |
210 |
> |
if (elapsedMillis < profileThreshold) |
211 |
> |
break; |
212 |
> |
// Never report first run of any test; treat it as a |
213 |
> |
// warmup run, notably to trigger all needed classloading, |
214 |
> |
if (i > 0) |
215 |
|
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); |
216 |
|
} |
217 |
|
} |
526 |
|
setDelays(); |
527 |
|
} |
528 |
|
|
529 |
+ |
void tearDownFail(String format, Object... args) { |
530 |
+ |
String msg = toString() + ": " + String.format(format, args); |
531 |
+ |
System.err.println(msg); |
532 |
+ |
printAllStackTraces(); |
533 |
+ |
throw new AssertionFailedError(msg); |
534 |
+ |
} |
535 |
+ |
|
536 |
|
/** |
537 |
|
* Extra checks that get done for all test cases. |
538 |
|
* |
560 |
|
} |
561 |
|
|
562 |
|
if (Thread.interrupted()) |
563 |
< |
throw new AssertionFailedError("interrupt status set in main thread"); |
563 |
> |
tearDownFail("interrupt status set in main thread"); |
564 |
|
|
565 |
|
checkForkJoinPoolThreadLeaks(); |
566 |
|
} |
569 |
|
* Finds missing try { ... } finally { joinPool(e); } |
570 |
|
*/ |
571 |
|
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
572 |
< |
Thread[] survivors = new Thread[5]; |
572 |
> |
Thread[] survivors = new Thread[7]; |
573 |
|
int count = Thread.enumerate(survivors); |
574 |
|
for (int i = 0; i < count; i++) { |
575 |
|
Thread thread = survivors[i]; |
577 |
|
if (name.startsWith("ForkJoinPool-")) { |
578 |
|
// give thread some time to terminate |
579 |
|
thread.join(LONG_DELAY_MS); |
580 |
< |
if (!thread.isAlive()) continue; |
581 |
< |
throw new AssertionFailedError |
582 |
< |
(String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n", |
570 |
< |
toString(), name)); |
580 |
> |
if (thread.isAlive()) |
581 |
> |
tearDownFail("Found leaked ForkJoinPool thread thread=%s", |
582 |
> |
thread); |
583 |
|
} |
584 |
|
} |
585 |
+ |
|
586 |
+ |
if (!ForkJoinPool.commonPool() |
587 |
+ |
.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS)) |
588 |
+ |
tearDownFail("ForkJoin common pool thread stuck"); |
589 |
|
} |
590 |
|
|
591 |
|
/** |
744 |
|
} |
745 |
|
|
746 |
|
/** |
747 |
+ |
* Allows use of try-with-resources with per-test thread pools. |
748 |
+ |
*/ |
749 |
+ |
static class PoolCloser<T extends ExecutorService> |
750 |
+ |
implements AutoCloseable { |
751 |
+ |
public final T pool; |
752 |
+ |
public PoolCloser(T pool) { this.pool = pool; } |
753 |
+ |
public void close() { joinPool(pool); } |
754 |
+ |
} |
755 |
+ |
|
756 |
+ |
/** |
757 |
|
* Waits out termination of a thread pool or fails doing so. |
758 |
|
*/ |
759 |
< |
void joinPool(ExecutorService pool) { |
759 |
> |
static void joinPool(ExecutorService pool) { |
760 |
|
try { |
761 |
|
pool.shutdown(); |
762 |
|
if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) |
778 |
|
* necessarily individually slow because they must block. |
779 |
|
*/ |
780 |
|
void testInParallel(Action ... actions) { |
781 |
< |
ExecutorService pool = Executors.newCachedThreadPool(); |
782 |
< |
try { |
781 |
> |
try (PoolCloser<ExecutorService> poolCloser |
782 |
> |
= new PoolCloser<>(Executors.newCachedThreadPool())) { |
783 |
> |
ExecutorService pool = poolCloser.pool; |
784 |
|
ArrayList<Future<?>> futures = new ArrayList<>(actions.length); |
785 |
|
for (final Action action : actions) |
786 |
|
futures.add(pool.submit(new CheckedRunnable() { |
793 |
|
} catch (Exception ex) { |
794 |
|
threadUnexpectedException(ex); |
795 |
|
} |
769 |
– |
} finally { |
770 |
– |
joinPool(pool); |
796 |
|
} |
797 |
|
} |
798 |
|
|
799 |
|
/** |
800 |
|
* A debugging tool to print all stack traces, as jstack does. |
801 |
+ |
* Uninteresting threads are filtered out. |
802 |
|
*/ |
803 |
|
static void printAllStackTraces() { |
804 |
< |
for (ThreadInfo info : |
805 |
< |
ManagementFactory.getThreadMXBean() |
806 |
< |
.dumpAllThreads(true, true)) |
804 |
> |
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); |
805 |
> |
System.err.println("------ stacktrace dump start ------"); |
806 |
> |
for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) { |
807 |
> |
String name = info.getThreadName(); |
808 |
> |
if ("Signal Dispatcher".equals(name)) |
809 |
> |
continue; |
810 |
> |
if ("Reference Handler".equals(name) |
811 |
> |
&& info.getLockName().startsWith("java.lang.ref.Reference$Lock")) |
812 |
> |
continue; |
813 |
> |
if ("Finalizer".equals(name) |
814 |
> |
&& info.getLockName().startsWith("java.lang.ref.ReferenceQueue$Lock")) |
815 |
> |
continue; |
816 |
|
System.err.print(info); |
817 |
+ |
} |
818 |
+ |
System.err.println("------ stacktrace dump end ------"); |
819 |
|
} |
820 |
|
|
821 |
|
/** |
1260 |
|
public static final String TEST_STRING = "a test string"; |
1261 |
|
|
1262 |
|
public static class StringTask implements Callable<String> { |
1263 |
< |
public String call() { return TEST_STRING; } |
1263 |
> |
final String value; |
1264 |
> |
public StringTask() { this(TEST_STRING); } |
1265 |
> |
public StringTask(String value) { this.value = value; } |
1266 |
> |
public String call() { return value; } |
1267 |
|
} |
1268 |
|
|
1269 |
|
public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) { |
1276 |
|
}}; |
1277 |
|
} |
1278 |
|
|
1279 |
+ |
public Runnable countDowner(final CountDownLatch latch) { |
1280 |
+ |
return new CheckedRunnable() { |
1281 |
+ |
public void realRun() throws InterruptedException { |
1282 |
+ |
latch.countDown(); |
1283 |
+ |
}}; |
1284 |
+ |
} |
1285 |
+ |
|
1286 |
|
public Runnable awaiter(final CountDownLatch latch) { |
1287 |
|
return new CheckedRunnable() { |
1288 |
|
public void realRun() throws InterruptedException { |