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 ... |
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 |
|
} |
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; |
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() { |
1106 |
|
continue; |
1107 |
|
if ("Reference Handler".equals(name) |
1108 |
|
&& (lockName = info.getLockName()) != null |
1109 |
< |
&& lockName.startsWith("java.lang.ref.Reference$Lock")) |
1109 |
> |
&& lockName.startsWith("java.lang.ref")) |
1110 |
|
continue; |
1111 |
< |
if ("Finalizer".equals(name) |
1111 |
> |
if (("Finalizer".equals(name) || "Common-Cleaner".equals(name)) |
1112 |
|
&& (lockName = info.getLockName()) != null |
1113 |
< |
&& lockName.startsWith("java.lang.ref.ReferenceQueue$Lock")) |
1113 |
> |
&& lockName.startsWith("java.lang.ref")) |
1114 |
|
continue; |
1115 |
< |
if ("checkForWedgedTest".equals(name)) |
1115 |
> |
if ("WedgedTestDetector".equals(name)) |
1116 |
|
continue; |
1117 |
|
System.err.print(info); |
1118 |
|
} |
1388 |
|
} |
1389 |
|
|
1390 |
|
/** |
1391 |
+ |
* Spin-waits up to LONG_DELAY_MS milliseconds for the current thread to |
1392 |
+ |
* be interrupted. Clears the interrupt status before returning. |
1393 |
+ |
*/ |
1394 |
+ |
void awaitInterrupted() { |
1395 |
+ |
for (long startTime = 0L; !Thread.interrupted(); ) { |
1396 |
+ |
if (startTime == 0L) |
1397 |
+ |
startTime = System.nanoTime(); |
1398 |
+ |
else if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
1399 |
+ |
fail("timed out waiting for thread interrupt"); |
1400 |
+ |
Thread.yield(); |
1401 |
+ |
} |
1402 |
+ |
} |
1403 |
+ |
|
1404 |
+ |
/** |
1405 |
|
* Returns the number of milliseconds since time given by |
1406 |
|
* startNanoTime, which must have been previously returned from a |
1407 |
|
* call to {@link System#nanoTime()}. |
1410 |
|
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
1411 |
|
} |
1412 |
|
|
1385 |
– |
// void assertTerminatesPromptly(long timeoutMillis, Runnable r) { |
1386 |
– |
// long startTime = System.nanoTime(); |
1387 |
– |
// try { |
1388 |
– |
// r.run(); |
1389 |
– |
// } catch (Throwable fail) { threadUnexpectedException(fail); } |
1390 |
– |
// if (millisElapsedSince(startTime) > timeoutMillis/2) |
1391 |
– |
// throw new AssertionError("did not return promptly"); |
1392 |
– |
// } |
1393 |
– |
|
1394 |
– |
// void assertTerminatesPromptly(Runnable r) { |
1395 |
– |
// assertTerminatesPromptly(LONG_DELAY_MS/2, r); |
1396 |
– |
// } |
1397 |
– |
|
1413 |
|
/** |
1414 |
|
* Checks that timed f.get() returns the expected value, and does not |
1415 |
|
* wait for the timeout to elapse before returning. |
1444 |
|
* to terminate (using {@link Thread#join(long)}), else interrupts |
1445 |
|
* the thread (in the hope that it may terminate later) and fails. |
1446 |
|
*/ |
1447 |
< |
void awaitTermination(Thread t, long timeoutMillis) { |
1447 |
> |
void awaitTermination(Thread thread, long timeoutMillis) { |
1448 |
|
try { |
1449 |
< |
t.join(timeoutMillis); |
1449 |
> |
thread.join(timeoutMillis); |
1450 |
|
} catch (InterruptedException fail) { |
1451 |
|
threadUnexpectedException(fail); |
1452 |
< |
} finally { |
1453 |
< |
if (t.getState() != Thread.State.TERMINATED) { |
1454 |
< |
t.interrupt(); |
1455 |
< |
threadFail("timed out waiting for thread to terminate"); |
1452 |
> |
} |
1453 |
> |
if (thread.getState() != Thread.State.TERMINATED) { |
1454 |
> |
String detail = String.format( |
1455 |
> |
"timed out waiting for thread to terminate, thread=%s, state=%s" , |
1456 |
> |
thread, thread.getState()); |
1457 |
> |
try { |
1458 |
> |
threadFail(detail); |
1459 |
> |
} finally { |
1460 |
> |
// Interrupt thread __after__ having reported its stack trace |
1461 |
> |
thread.interrupt(); |
1462 |
|
} |
1463 |
|
} |
1464 |
|
} |