49 |
|
import java.io.ObjectInputStream; |
50 |
|
import java.io.ObjectOutputStream; |
51 |
|
import java.lang.management.ManagementFactory; |
52 |
+ |
import java.lang.management.LockInfo; |
53 |
|
import java.lang.management.ThreadInfo; |
54 |
|
import java.lang.management.ThreadMXBean; |
55 |
|
import java.lang.reflect.Constructor; |
244 |
|
} |
245 |
|
} |
246 |
|
|
247 |
+ |
private static final ThreadMXBean THREAD_MXBEAN |
248 |
+ |
= ManagementFactory.getThreadMXBean(); |
249 |
+ |
|
250 |
|
/** |
251 |
|
* The scaling factor to apply to standard delays used in tests. |
252 |
|
* May be initialized from any of: |
288 |
|
static volatile TestCase currentTestCase; |
289 |
|
// static volatile int currentRun = 0; |
290 |
|
static { |
291 |
< |
Runnable checkForWedgedTest = new Runnable() { public void run() { |
291 |
> |
Runnable wedgedTestDetector = new Runnable() { public void run() { |
292 |
|
// Avoid spurious reports with enormous runsPerTest. |
293 |
|
// A single test case run should never take more than 1 second. |
294 |
|
// But let's cap it at the high end too ... |
295 |
< |
final int timeoutMinutes = |
296 |
< |
Math.min(15, Math.max(runsPerTest / 60, 1)); |
295 |
> |
final int timeoutMinutesMin = Math.max(runsPerTest / 60, 1) |
296 |
> |
* Math.max((int) delayFactor, 1); |
297 |
> |
final int timeoutMinutes = Math.min(15, timeoutMinutesMin); |
298 |
|
for (TestCase lastTestCase = currentTestCase;;) { |
299 |
|
try { MINUTES.sleep(timeoutMinutes); } |
300 |
|
catch (InterruptedException unexpected) { break; } |
314 |
|
} |
315 |
|
lastTestCase = currentTestCase; |
316 |
|
}}}; |
317 |
< |
Thread thread = new Thread(checkForWedgedTest, "checkForWedgedTest"); |
317 |
> |
Thread thread = new Thread(wedgedTestDetector, "WedgedTestDetector"); |
318 |
|
thread.setDaemon(true); |
319 |
|
thread.start(); |
320 |
|
} |
358 |
|
// Never report first run of any test; treat it as a |
359 |
|
// warmup run, notably to trigger all needed classloading, |
360 |
|
if (i > 0) |
361 |
< |
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); |
361 |
> |
System.out.printf("%s: %d%n", toString(), elapsedMillis); |
362 |
|
} |
363 |
|
} |
364 |
|
|
465 |
|
public static boolean atLeastJava9() { return JAVA_CLASS_VERSION >= 53.0; } |
466 |
|
public static boolean atLeastJava10() { return JAVA_CLASS_VERSION >= 54.0; } |
467 |
|
public static boolean atLeastJava11() { return JAVA_CLASS_VERSION >= 55.0; } |
468 |
+ |
public static boolean atLeastJava12() { return JAVA_CLASS_VERSION >= 56.0; } |
469 |
+ |
public static boolean atLeastJava13() { return JAVA_CLASS_VERSION >= 57.0; } |
470 |
+ |
public static boolean atLeastJava14() { return JAVA_CLASS_VERSION >= 58.0; } |
471 |
+ |
public static boolean atLeastJava15() { return JAVA_CLASS_VERSION >= 59.0; } |
472 |
+ |
public static boolean atLeastJava16() { return JAVA_CLASS_VERSION >= 60.0; } |
473 |
+ |
public static boolean atLeastJava17() { return JAVA_CLASS_VERSION >= 61.0; } |
474 |
|
|
475 |
|
/** |
476 |
|
* Collects all JSR166 unit tests as one suite. |
522 |
|
ExecutorsTest.suite(), |
523 |
|
ExecutorCompletionServiceTest.suite(), |
524 |
|
FutureTaskTest.suite(), |
525 |
+ |
HashtableTest.suite(), |
526 |
|
LinkedBlockingDequeTest.suite(), |
527 |
|
LinkedBlockingQueueTest.suite(), |
528 |
|
LinkedListTest.suite(), |
562 |
|
"HashMapTest", |
563 |
|
"LinkedBlockingDeque8Test", |
564 |
|
"LinkedBlockingQueue8Test", |
565 |
+ |
"LinkedHashMapTest", |
566 |
|
"LongAccumulatorTest", |
567 |
|
"LongAdderTest", |
568 |
|
"SplittableRandomTest", |
661 |
|
public static long MEDIUM_DELAY_MS; |
662 |
|
public static long LONG_DELAY_MS; |
663 |
|
|
664 |
+ |
/** |
665 |
+ |
* A delay significantly longer than LONG_DELAY_MS. |
666 |
+ |
* Use this in a thread that is waited for via awaitTermination(Thread). |
667 |
+ |
*/ |
668 |
+ |
public static long LONGER_DELAY_MS; |
669 |
+ |
|
670 |
|
private static final long RANDOM_TIMEOUT; |
671 |
|
private static final long RANDOM_EXPIRED_TIMEOUT; |
672 |
|
private static final TimeUnit RANDOM_TIMEUNIT; |
695 |
|
static TimeUnit randomTimeUnit() { return RANDOM_TIMEUNIT; } |
696 |
|
|
697 |
|
/** |
698 |
+ |
* Returns a random boolean; a "coin flip". |
699 |
+ |
*/ |
700 |
+ |
static boolean randomBoolean() { |
701 |
+ |
return ThreadLocalRandom.current().nextBoolean(); |
702 |
+ |
} |
703 |
+ |
|
704 |
+ |
/** |
705 |
+ |
* Returns a random element from given choices. |
706 |
+ |
*/ |
707 |
+ |
<T> T chooseRandomly(List<T> choices) { |
708 |
+ |
return choices.get(ThreadLocalRandom.current().nextInt(choices.size())); |
709 |
+ |
} |
710 |
+ |
|
711 |
+ |
/** |
712 |
+ |
* Returns a random element from given choices. |
713 |
+ |
*/ |
714 |
+ |
<T> T chooseRandomly(T... choices) { |
715 |
+ |
return choices[ThreadLocalRandom.current().nextInt(choices.length)]; |
716 |
+ |
} |
717 |
+ |
|
718 |
+ |
/** |
719 |
|
* Returns the shortest timed delay. This can be scaled up for |
720 |
|
* slow machines using the jsr166.delay.factor system property, |
721 |
|
* or via jtreg's -timeoutFactor: flag. |
733 |
|
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
734 |
|
MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10; |
735 |
|
LONG_DELAY_MS = SHORT_DELAY_MS * 200; |
736 |
+ |
LONGER_DELAY_MS = 2 * LONG_DELAY_MS; |
737 |
|
} |
738 |
|
|
739 |
|
private static final long TIMEOUT_DELAY_MS |
772 |
|
*/ |
773 |
|
public void threadRecordFailure(Throwable t) { |
774 |
|
System.err.println(t); |
775 |
< |
dumpTestThreads(); |
776 |
< |
threadFailure.compareAndSet(null, t); |
775 |
> |
if (threadFailure.compareAndSet(null, t)) |
776 |
> |
dumpTestThreads(); |
777 |
|
} |
778 |
|
|
779 |
|
public void setUp() { |
1094 |
|
} |
1095 |
|
} |
1096 |
|
|
1097 |
+ |
/** Returns true if thread info might be useful in a thread dump. */ |
1098 |
+ |
static boolean threadOfInterest(ThreadInfo info) { |
1099 |
+ |
final String name = info.getThreadName(); |
1100 |
+ |
String lockName; |
1101 |
+ |
if (name == null) |
1102 |
+ |
return true; |
1103 |
+ |
if (name.equals("Signal Dispatcher") |
1104 |
+ |
|| name.equals("WedgedTestDetector")) |
1105 |
+ |
return false; |
1106 |
+ |
if (name.equals("Reference Handler")) { |
1107 |
+ |
// Reference Handler stacktrace changed in JDK-8156500 |
1108 |
+ |
StackTraceElement[] stackTrace; String methodName; |
1109 |
+ |
if ((stackTrace = info.getStackTrace()) != null |
1110 |
+ |
&& stackTrace.length > 0 |
1111 |
+ |
&& (methodName = stackTrace[0].getMethodName()) != null |
1112 |
+ |
&& methodName.equals("waitForReferencePendingList")) |
1113 |
+ |
return false; |
1114 |
+ |
// jdk8 Reference Handler stacktrace |
1115 |
+ |
if ((lockName = info.getLockName()) != null |
1116 |
+ |
&& lockName.startsWith("java.lang.ref")) |
1117 |
+ |
return false; |
1118 |
+ |
} |
1119 |
+ |
if ((name.equals("Finalizer") || name.equals("Common-Cleaner")) |
1120 |
+ |
&& (lockName = info.getLockName()) != null |
1121 |
+ |
&& lockName.startsWith("java.lang.ref")) |
1122 |
+ |
return false; |
1123 |
+ |
if (name.startsWith("ForkJoinPool.commonPool-worker") |
1124 |
+ |
&& (lockName = info.getLockName()) != null |
1125 |
+ |
&& lockName.startsWith("java.util.concurrent.ForkJoinPool")) |
1126 |
+ |
return false; |
1127 |
+ |
return true; |
1128 |
+ |
} |
1129 |
+ |
|
1130 |
|
/** |
1131 |
|
* A debugging tool to print stack traces of most threads, as jstack does. |
1132 |
|
* Uninteresting threads are filtered out. |
1141 |
|
} |
1142 |
|
} |
1143 |
|
|
1070 |
– |
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); |
1144 |
|
System.err.println("------ stacktrace dump start ------"); |
1145 |
< |
for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) { |
1146 |
< |
final String name = info.getThreadName(); |
1147 |
< |
String lockName; |
1075 |
< |
if ("Signal Dispatcher".equals(name)) |
1076 |
< |
continue; |
1077 |
< |
if ("Reference Handler".equals(name) |
1078 |
< |
&& (lockName = info.getLockName()) != null |
1079 |
< |
&& lockName.startsWith("java.lang.ref.Reference$Lock")) |
1080 |
< |
continue; |
1081 |
< |
if ("Finalizer".equals(name) |
1082 |
< |
&& (lockName = info.getLockName()) != null |
1083 |
< |
&& lockName.startsWith("java.lang.ref.ReferenceQueue$Lock")) |
1084 |
< |
continue; |
1085 |
< |
if ("checkForWedgedTest".equals(name)) |
1086 |
< |
continue; |
1087 |
< |
System.err.print(info); |
1088 |
< |
} |
1145 |
> |
for (ThreadInfo info : THREAD_MXBEAN.dumpAllThreads(true, true)) |
1146 |
> |
if (threadOfInterest(info)) |
1147 |
> |
System.err.print(info); |
1148 |
|
System.err.println("------ stacktrace dump end ------"); |
1149 |
|
|
1150 |
|
if (sm != null) System.setSecurityManager(sm); |
1171 |
|
} |
1172 |
|
|
1173 |
|
/** |
1174 |
+ |
* Returns the thread's blocker's class name, if any, else null. |
1175 |
+ |
*/ |
1176 |
+ |
String blockerClassName(Thread thread) { |
1177 |
+ |
ThreadInfo threadInfo; LockInfo lockInfo; |
1178 |
+ |
if ((threadInfo = THREAD_MXBEAN.getThreadInfo(thread.getId(), 0)) != null |
1179 |
+ |
&& (lockInfo = threadInfo.getLockInfo()) != null) |
1180 |
+ |
return lockInfo.getClassName(); |
1181 |
+ |
return null; |
1182 |
+ |
} |
1183 |
+ |
|
1184 |
+ |
/** |
1185 |
|
* Checks that future.get times out, with the default timeout of |
1186 |
|
* {@code timeoutMillis()}. |
1187 |
|
*/ |
1375 |
|
Callable<Boolean> waitingForGodot) { |
1376 |
|
for (long startTime = 0L;;) { |
1377 |
|
switch (thread.getState()) { |
1378 |
+ |
default: break; |
1379 |
|
case BLOCKED: case WAITING: case TIMED_WAITING: |
1380 |
|
try { |
1381 |
|
if (waitingForGodot == null || waitingForGodot.call()) |
1428 |
|
} |
1429 |
|
|
1430 |
|
/** |
1431 |
+ |
* Spin-waits up to LONG_DELAY_MS milliseconds for the current thread to |
1432 |
+ |
* be interrupted. Clears the interrupt status before returning. |
1433 |
+ |
*/ |
1434 |
+ |
void awaitInterrupted() { |
1435 |
+ |
for (long startTime = 0L; !Thread.interrupted(); ) { |
1436 |
+ |
if (startTime == 0L) |
1437 |
+ |
startTime = System.nanoTime(); |
1438 |
+ |
else if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
1439 |
+ |
fail("timed out waiting for thread interrupt"); |
1440 |
+ |
Thread.yield(); |
1441 |
+ |
} |
1442 |
+ |
} |
1443 |
+ |
|
1444 |
+ |
/** |
1445 |
|
* Returns the number of milliseconds since time given by |
1446 |
|
* startNanoTime, which must have been previously returned from a |
1447 |
|
* call to {@link System#nanoTime()}. |
1450 |
|
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
1451 |
|
} |
1452 |
|
|
1368 |
– |
// void assertTerminatesPromptly(long timeoutMillis, Runnable r) { |
1369 |
– |
// long startTime = System.nanoTime(); |
1370 |
– |
// try { |
1371 |
– |
// r.run(); |
1372 |
– |
// } catch (Throwable fail) { threadUnexpectedException(fail); } |
1373 |
– |
// if (millisElapsedSince(startTime) > timeoutMillis/2) |
1374 |
– |
// throw new AssertionError("did not return promptly"); |
1375 |
– |
// } |
1376 |
– |
|
1377 |
– |
// void assertTerminatesPromptly(Runnable r) { |
1378 |
– |
// assertTerminatesPromptly(LONG_DELAY_MS/2, r); |
1379 |
– |
// } |
1380 |
– |
|
1453 |
|
/** |
1454 |
|
* Checks that timed f.get() returns the expected value, and does not |
1455 |
|
* wait for the timeout to elapse before returning. |
1480 |
|
} |
1481 |
|
|
1482 |
|
/** |
1483 |
+ |
* Returns a new started daemon Thread running the given action, |
1484 |
+ |
* wrapped in a CheckedRunnable. |
1485 |
+ |
*/ |
1486 |
+ |
Thread newStartedThread(Action action) { |
1487 |
+ |
return newStartedThread(checkedRunnable(action)); |
1488 |
+ |
} |
1489 |
+ |
|
1490 |
+ |
/** |
1491 |
|
* Waits for the specified time (in milliseconds) for the thread |
1492 |
|
* to terminate (using {@link Thread#join(long)}), else interrupts |
1493 |
|
* the thread (in the hope that it may terminate later) and fails. |
1494 |
|
*/ |
1495 |
< |
void awaitTermination(Thread t, long timeoutMillis) { |
1495 |
> |
void awaitTermination(Thread thread, long timeoutMillis) { |
1496 |
|
try { |
1497 |
< |
t.join(timeoutMillis); |
1497 |
> |
thread.join(timeoutMillis); |
1498 |
|
} catch (InterruptedException fail) { |
1499 |
|
threadUnexpectedException(fail); |
1500 |
< |
} finally { |
1501 |
< |
if (t.getState() != Thread.State.TERMINATED) { |
1502 |
< |
t.interrupt(); |
1503 |
< |
threadFail("timed out waiting for thread to terminate"); |
1500 |
> |
} |
1501 |
> |
if (thread.getState() != Thread.State.TERMINATED) { |
1502 |
> |
String detail = String.format( |
1503 |
> |
"timed out waiting for thread to terminate, thread=%s, state=%s" , |
1504 |
> |
thread, thread.getState()); |
1505 |
> |
try { |
1506 |
> |
threadFail(detail); |
1507 |
> |
} finally { |
1508 |
> |
// Interrupt thread __after__ having reported its stack trace |
1509 |
> |
thread.interrupt(); |
1510 |
|
} |
1511 |
|
} |
1512 |
|
} |
1534 |
|
} |
1535 |
|
} |
1536 |
|
|
1537 |
+ |
Runnable checkedRunnable(Action action) { |
1538 |
+ |
return new CheckedRunnable() { |
1539 |
+ |
public void realRun() throws Throwable { |
1540 |
+ |
action.run(); |
1541 |
+ |
}}; |
1542 |
+ |
} |
1543 |
+ |
|
1544 |
|
public abstract class ThreadShouldThrow extends Thread { |
1545 |
|
protected abstract void realRun() throws Throwable; |
1546 |
|
|
1702 |
|
public String call() { throw new NullPointerException(); } |
1703 |
|
} |
1704 |
|
|
1612 |
– |
public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
1613 |
– |
protected void realRun() { |
1614 |
– |
try { |
1615 |
– |
delay(SMALL_DELAY_MS); |
1616 |
– |
} catch (InterruptedException ok) {} |
1617 |
– |
} |
1618 |
– |
} |
1619 |
– |
|
1705 |
|
public Runnable possiblyInterruptedRunnable(final long timeoutMillis) { |
1706 |
|
return new CheckedRunnable() { |
1707 |
|
protected void realRun() { |
1779 |
|
|
1780 |
|
public int await() { |
1781 |
|
try { |
1782 |
< |
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1782 |
> |
return super.await(LONGER_DELAY_MS, MILLISECONDS); |
1783 |
|
} catch (TimeoutException timedOut) { |
1784 |
|
throw new AssertionError("timed out"); |
1785 |
|
} catch (Exception fail) { |
1835 |
|
} |
1836 |
|
} |
1837 |
|
|
1838 |
< |
void assertImmutable(final Object o) { |
1838 |
> |
void assertImmutable(Object o) { |
1839 |
|
if (o instanceof Collection) { |
1840 |
|
assertThrows( |
1841 |
|
UnsupportedOperationException.class, |
1842 |
< |
new Runnable() { public void run() { |
1758 |
< |
((Collection) o).add(null);}}); |
1842 |
> |
() -> ((Collection) o).add(null)); |
1843 |
|
} |
1844 |
|
} |
1845 |
|
|
1899 |
|
} |
1900 |
|
|
1901 |
|
public void assertThrows(Class<? extends Throwable> expectedExceptionClass, |
1902 |
< |
Runnable... throwingActions) { |
1903 |
< |
for (Runnable throwingAction : throwingActions) { |
1902 |
> |
Action... throwingActions) { |
1903 |
> |
for (Action throwingAction : throwingActions) { |
1904 |
|
boolean threw = false; |
1905 |
|
try { throwingAction.run(); } |
1906 |
|
catch (Throwable t) { |