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; |
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)) |
799 |
|
|
800 |
|
/** |
801 |
|
* A debugging tool to print all stack traces, as jstack does. |
802 |
+ |
* Uninteresting threads are filtered out. |
803 |
|
*/ |
804 |
|
static void printAllStackTraces() { |
805 |
< |
for (ThreadInfo info : |
806 |
< |
ManagementFactory.getThreadMXBean() |
807 |
< |
.dumpAllThreads(true, true)) |
805 |
> |
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); |
806 |
> |
System.err.println("------ stacktrace dump start ------"); |
807 |
> |
for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) { |
808 |
> |
String name = info.getThreadName(); |
809 |
> |
if ("Signal Dispatcher".equals(name)) |
810 |
> |
continue; |
811 |
> |
if ("Reference Handler".equals(name) |
812 |
> |
&& info.getLockName().startsWith("java.lang.ref.Reference$Lock")) |
813 |
> |
continue; |
814 |
> |
if ("Finalizer".equals(name) |
815 |
> |
&& info.getLockName().startsWith("java.lang.ref.ReferenceQueue$Lock")) |
816 |
> |
continue; |
817 |
|
System.err.print(info); |
818 |
+ |
} |
819 |
+ |
System.err.println("------ stacktrace dump end ------"); |
820 |
|
} |
821 |
|
|
822 |
|
/** |
1277 |
|
}}; |
1278 |
|
} |
1279 |
|
|
1280 |
+ |
public Runnable countDowner(final CountDownLatch latch) { |
1281 |
+ |
return new CheckedRunnable() { |
1282 |
+ |
public void realRun() throws InterruptedException { |
1283 |
+ |
latch.countDown(); |
1284 |
+ |
}}; |
1285 |
+ |
} |
1286 |
+ |
|
1287 |
|
public Runnable awaiter(final CountDownLatch latch) { |
1288 |
|
return new CheckedRunnable() { |
1289 |
|
public void realRun() throws InterruptedException { |