284 |
|
static volatile TestCase currentTestCase; |
285 |
|
// static volatile int currentRun = 0; |
286 |
|
static { |
287 |
< |
Runnable checkForWedgedTest = new Runnable() { public void run() { |
287 |
> |
Runnable wedgedTestDetector = new Runnable() { public void run() { |
288 |
|
// Avoid spurious reports with enormous runsPerTest. |
289 |
|
// A single test case run should never take more than 1 second. |
290 |
|
// But let's cap it at the high end too ... |
291 |
< |
final int timeoutMinutes = |
292 |
< |
Math.min(15, Math.max(runsPerTest / 60, 1)); |
291 |
> |
final int timeoutMinutesMin = Math.max(runsPerTest / 60, 1) |
292 |
> |
* Math.max((int) delayFactor, 1); |
293 |
> |
final int timeoutMinutes = Math.min(15, timeoutMinutesMin); |
294 |
|
for (TestCase lastTestCase = currentTestCase;;) { |
295 |
|
try { MINUTES.sleep(timeoutMinutes); } |
296 |
|
catch (InterruptedException unexpected) { break; } |
310 |
|
} |
311 |
|
lastTestCase = currentTestCase; |
312 |
|
}}}; |
313 |
< |
Thread thread = new Thread(checkForWedgedTest, "checkForWedgedTest"); |
313 |
> |
Thread thread = new Thread(wedgedTestDetector, "WedgedTestDetector"); |
314 |
|
thread.setDaemon(true); |
315 |
|
thread.start(); |
316 |
|
} |
354 |
|
// Never report first run of any test; treat it as a |
355 |
|
// warmup run, notably to trigger all needed classloading, |
356 |
|
if (i > 0) |
357 |
< |
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); |
357 |
> |
System.out.printf("%s: %d%n", toString(), elapsedMillis); |
358 |
|
} |
359 |
|
} |
360 |
|
|
518 |
|
ExecutorsTest.suite(), |
519 |
|
ExecutorCompletionServiceTest.suite(), |
520 |
|
FutureTaskTest.suite(), |
521 |
+ |
HashtableTest.suite(), |
522 |
|
LinkedBlockingDequeTest.suite(), |
523 |
|
LinkedBlockingQueueTest.suite(), |
524 |
|
LinkedListTest.suite(), |
657 |
|
public static long MEDIUM_DELAY_MS; |
658 |
|
public static long LONG_DELAY_MS; |
659 |
|
|
660 |
+ |
/** |
661 |
+ |
* A delay significantly longer than LONG_DELAY_MS. |
662 |
+ |
* Use this in a thread that is waited for via awaitTermination(Thread). |
663 |
+ |
*/ |
664 |
+ |
public static long LONGER_DELAY_MS; |
665 |
+ |
|
666 |
|
private static final long RANDOM_TIMEOUT; |
667 |
|
private static final long RANDOM_EXPIRED_TIMEOUT; |
668 |
|
private static final TimeUnit RANDOM_TIMEUNIT; |
691 |
|
static TimeUnit randomTimeUnit() { return RANDOM_TIMEUNIT; } |
692 |
|
|
693 |
|
/** |
694 |
+ |
* Returns a random boolean; a "coin flip". |
695 |
+ |
*/ |
696 |
+ |
static boolean randomBoolean() { |
697 |
+ |
return ThreadLocalRandom.current().nextBoolean(); |
698 |
+ |
} |
699 |
+ |
|
700 |
+ |
/** |
701 |
+ |
* Returns a random element from given choices. |
702 |
+ |
*/ |
703 |
+ |
<T> T chooseRandomly(T... choices) { |
704 |
+ |
return choices[ThreadLocalRandom.current().nextInt(choices.length)]; |
705 |
+ |
} |
706 |
+ |
|
707 |
+ |
/** |
708 |
|
* Returns the shortest timed delay. This can be scaled up for |
709 |
|
* slow machines using the jsr166.delay.factor system property, |
710 |
|
* or via jtreg's -timeoutFactor: flag. |
722 |
|
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
723 |
|
MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10; |
724 |
|
LONG_DELAY_MS = SHORT_DELAY_MS * 200; |
725 |
+ |
LONGER_DELAY_MS = 2 * LONG_DELAY_MS; |
726 |
|
} |
727 |
|
|
728 |
|
private static final long TIMEOUT_DELAY_MS |
761 |
|
*/ |
762 |
|
public void threadRecordFailure(Throwable t) { |
763 |
|
System.err.println(t); |
764 |
< |
dumpTestThreads(); |
765 |
< |
threadFailure.compareAndSet(null, t); |
764 |
> |
if (threadFailure.compareAndSet(null, t)) |
765 |
> |
dumpTestThreads(); |
766 |
|
} |
767 |
|
|
768 |
|
public void setUp() { |
1083 |
|
} |
1084 |
|
} |
1085 |
|
|
1086 |
+ |
/** Returns true if thread info might be useful in a thread dump. */ |
1087 |
+ |
static boolean threadOfInterest(ThreadInfo info) { |
1088 |
+ |
final String name = info.getThreadName(); |
1089 |
+ |
String lockName; |
1090 |
+ |
if (name == null) |
1091 |
+ |
return true; |
1092 |
+ |
if (name.equals("Signal Dispatcher") |
1093 |
+ |
|| name.equals("WedgedTestDetector")) |
1094 |
+ |
return false; |
1095 |
+ |
if (name.equals("Reference Handler")) { |
1096 |
+ |
// Reference Handler stacktrace changed in JDK-8156500 |
1097 |
+ |
StackTraceElement[] stackTrace; String methodName; |
1098 |
+ |
if ((stackTrace = info.getStackTrace()) != null |
1099 |
+ |
&& stackTrace.length > 0 |
1100 |
+ |
&& (methodName = stackTrace[0].getMethodName()) != null |
1101 |
+ |
&& methodName.equals("waitForReferencePendingList")) |
1102 |
+ |
return false; |
1103 |
+ |
// jdk8 Reference Handler stacktrace |
1104 |
+ |
if ((lockName = info.getLockName()) != null |
1105 |
+ |
&& lockName.startsWith("java.lang.ref")) |
1106 |
+ |
return false; |
1107 |
+ |
} |
1108 |
+ |
if ((name.equals("Finalizer") || name.equals("Common-Cleaner")) |
1109 |
+ |
&& (lockName = info.getLockName()) != null |
1110 |
+ |
&& lockName.startsWith("java.lang.ref")) |
1111 |
+ |
return false; |
1112 |
+ |
if (name.startsWith("ForkJoinPool.commonPool-worker") |
1113 |
+ |
&& (lockName = info.getLockName()) != null |
1114 |
+ |
&& lockName.startsWith("java.util.concurrent.ForkJoinPool")) |
1115 |
+ |
return false; |
1116 |
+ |
return true; |
1117 |
+ |
} |
1118 |
+ |
|
1119 |
|
/** |
1120 |
|
* A debugging tool to print stack traces of most threads, as jstack does. |
1121 |
|
* Uninteresting threads are filtered out. |
1132 |
|
|
1133 |
|
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); |
1134 |
|
System.err.println("------ stacktrace dump start ------"); |
1135 |
< |
for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) { |
1136 |
< |
final String name = info.getThreadName(); |
1137 |
< |
String lockName; |
1082 |
< |
if ("Signal Dispatcher".equals(name)) |
1083 |
< |
continue; |
1084 |
< |
if ("Reference Handler".equals(name) |
1085 |
< |
&& (lockName = info.getLockName()) != null |
1086 |
< |
&& lockName.startsWith("java.lang.ref.Reference$Lock")) |
1087 |
< |
continue; |
1088 |
< |
if ("Finalizer".equals(name) |
1089 |
< |
&& (lockName = info.getLockName()) != null |
1090 |
< |
&& lockName.startsWith("java.lang.ref.ReferenceQueue$Lock")) |
1091 |
< |
continue; |
1092 |
< |
if ("checkForWedgedTest".equals(name)) |
1093 |
< |
continue; |
1094 |
< |
System.err.print(info); |
1095 |
< |
} |
1135 |
> |
for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) |
1136 |
> |
if (threadOfInterest(info)) |
1137 |
> |
System.err.print(info); |
1138 |
|
System.err.println("------ stacktrace dump end ------"); |
1139 |
|
|
1140 |
|
if (sm != null) System.setSecurityManager(sm); |
1407 |
|
} |
1408 |
|
|
1409 |
|
/** |
1410 |
+ |
* Spin-waits up to LONG_DELAY_MS milliseconds for the current thread to |
1411 |
+ |
* be interrupted. Clears the interrupt status before returning. |
1412 |
+ |
*/ |
1413 |
+ |
void awaitInterrupted() { |
1414 |
+ |
for (long startTime = 0L; !Thread.interrupted(); ) { |
1415 |
+ |
if (startTime == 0L) |
1416 |
+ |
startTime = System.nanoTime(); |
1417 |
+ |
else if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
1418 |
+ |
fail("timed out waiting for thread interrupt"); |
1419 |
+ |
Thread.yield(); |
1420 |
+ |
} |
1421 |
+ |
} |
1422 |
+ |
|
1423 |
+ |
/** |
1424 |
|
* Returns the number of milliseconds since time given by |
1425 |
|
* startNanoTime, which must have been previously returned from a |
1426 |
|
* call to {@link System#nanoTime()}. |
1429 |
|
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
1430 |
|
} |
1431 |
|
|
1376 |
– |
// void assertTerminatesPromptly(long timeoutMillis, Runnable r) { |
1377 |
– |
// long startTime = System.nanoTime(); |
1378 |
– |
// try { |
1379 |
– |
// r.run(); |
1380 |
– |
// } catch (Throwable fail) { threadUnexpectedException(fail); } |
1381 |
– |
// if (millisElapsedSince(startTime) > timeoutMillis/2) |
1382 |
– |
// throw new AssertionError("did not return promptly"); |
1383 |
– |
// } |
1384 |
– |
|
1385 |
– |
// void assertTerminatesPromptly(Runnable r) { |
1386 |
– |
// assertTerminatesPromptly(LONG_DELAY_MS/2, r); |
1387 |
– |
// } |
1388 |
– |
|
1432 |
|
/** |
1433 |
|
* Checks that timed f.get() returns the expected value, and does not |
1434 |
|
* wait for the timeout to elapse before returning. |
1463 |
|
* to terminate (using {@link Thread#join(long)}), else interrupts |
1464 |
|
* the thread (in the hope that it may terminate later) and fails. |
1465 |
|
*/ |
1466 |
< |
void awaitTermination(Thread t, long timeoutMillis) { |
1466 |
> |
void awaitTermination(Thread thread, long timeoutMillis) { |
1467 |
|
try { |
1468 |
< |
t.join(timeoutMillis); |
1468 |
> |
thread.join(timeoutMillis); |
1469 |
|
} catch (InterruptedException fail) { |
1470 |
|
threadUnexpectedException(fail); |
1471 |
< |
} finally { |
1472 |
< |
if (t.getState() != Thread.State.TERMINATED) { |
1473 |
< |
t.interrupt(); |
1474 |
< |
threadFail("timed out waiting for thread to terminate"); |
1471 |
> |
} |
1472 |
> |
if (thread.getState() != Thread.State.TERMINATED) { |
1473 |
> |
String detail = String.format( |
1474 |
> |
"timed out waiting for thread to terminate, thread=%s, state=%s" , |
1475 |
> |
thread, thread.getState()); |
1476 |
> |
try { |
1477 |
> |
threadFail(detail); |
1478 |
> |
} finally { |
1479 |
> |
// Interrupt thread __after__ having reported its stack trace |
1480 |
> |
thread.interrupt(); |
1481 |
|
} |
1482 |
|
} |
1483 |
|
} |