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; } |
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() { |
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 |
|
|
1377 |
– |
// void assertTerminatesPromptly(long timeoutMillis, Runnable r) { |
1378 |
– |
// long startTime = System.nanoTime(); |
1379 |
– |
// try { |
1380 |
– |
// r.run(); |
1381 |
– |
// } catch (Throwable fail) { threadUnexpectedException(fail); } |
1382 |
– |
// if (millisElapsedSince(startTime) > timeoutMillis/2) |
1383 |
– |
// throw new AssertionError("did not return promptly"); |
1384 |
– |
// } |
1385 |
– |
|
1386 |
– |
// void assertTerminatesPromptly(Runnable r) { |
1387 |
– |
// assertTerminatesPromptly(LONG_DELAY_MS/2, r); |
1388 |
– |
// } |
1389 |
– |
|
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 |
|
} |