117 |
|
* |
118 |
|
* <ol> |
119 |
|
* |
120 |
< |
* <li>All assertions in code running in generated threads must use |
121 |
< |
* the forms {@link #threadFail}, {@link #threadAssertTrue}, {@link |
122 |
< |
* #threadAssertEquals}, or {@link #threadAssertNull}, (not |
123 |
< |
* {@code fail}, {@code assertTrue}, etc.) It is OK (but not |
124 |
< |
* particularly recommended) for other code to use these forms too. |
125 |
< |
* Only the most typically used JUnit assertion methods are defined |
126 |
< |
* this way, but enough to live with. |
120 |
> |
* <li>All code not running in the main test thread (manually spawned threads |
121 |
> |
* or the common fork join pool) must be checked for failure (and completion!). |
122 |
> |
* Mechanisms that can be used to ensure this are: |
123 |
> |
* <ol> |
124 |
> |
* <li>Signalling via a synchronizer like AtomicInteger or CountDownLatch |
125 |
> |
* that the task completed normally, which is checked before returning from |
126 |
> |
* the test method in the main thread. |
127 |
> |
* <li>Using the forms {@link #threadFail}, {@link #threadAssertTrue}, |
128 |
> |
* or {@link #threadAssertNull}, (not {@code fail}, {@code assertTrue}, etc.) |
129 |
> |
* Only the most typically used JUnit assertion methods are defined |
130 |
> |
* this way, but enough to live with. |
131 |
> |
* <li>Recording failure explicitly using {@link #threadUnexpectedException} |
132 |
> |
* or {@link #threadRecordFailure}. |
133 |
> |
* <li>Using a wrapper like CheckedRunnable that uses one the mechanisms above. |
134 |
> |
* </ol> |
135 |
|
* |
136 |
|
* <li>If you override {@link #setUp} or {@link #tearDown}, make sure |
137 |
|
* to invoke {@code super.setUp} and {@code super.tearDown} within |
1299 |
|
/** |
1300 |
|
* Spin-waits up to the specified number of milliseconds for the given |
1301 |
|
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
1302 |
+ |
* @param waitingForGodot if non-null, an additional condition to satisfy |
1303 |
|
*/ |
1304 |
< |
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
1305 |
< |
long startTime = 0L; |
1306 |
< |
for (;;) { |
1307 |
< |
Thread.State s = thread.getState(); |
1308 |
< |
if (s == Thread.State.BLOCKED || |
1309 |
< |
s == Thread.State.WAITING || |
1310 |
< |
s == Thread.State.TIMED_WAITING) |
1311 |
< |
return; |
1312 |
< |
else if (s == Thread.State.TERMINATED) |
1304 |
> |
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis, |
1305 |
> |
Callable<Boolean> waitingForGodot) { |
1306 |
> |
for (long startTime = 0L;;) { |
1307 |
> |
switch (thread.getState()) { |
1308 |
> |
case BLOCKED: case WAITING: case TIMED_WAITING: |
1309 |
> |
try { |
1310 |
> |
if (waitingForGodot == null || waitingForGodot.call()) |
1311 |
> |
return; |
1312 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
1313 |
> |
break; |
1314 |
> |
case TERMINATED: |
1315 |
|
fail("Unexpected thread termination"); |
1316 |
< |
else if (startTime == 0L) |
1316 |
> |
} |
1317 |
> |
|
1318 |
> |
if (startTime == 0L) |
1319 |
|
startTime = System.nanoTime(); |
1320 |
|
else if (millisElapsedSince(startTime) > timeoutMillis) { |
1321 |
< |
threadAssertTrue(thread.isAlive()); |
1322 |
< |
fail("timed out waiting for thread to enter wait state"); |
1321 |
> |
assertTrue(thread.isAlive()); |
1322 |
> |
if (waitingForGodot == null |
1323 |
> |
|| thread.getState() == Thread.State.RUNNABLE) |
1324 |
> |
fail("timed out waiting for thread to enter wait state"); |
1325 |
> |
else |
1326 |
> |
fail("timed out waiting for condition, thread state=" |
1327 |
> |
+ thread.getState()); |
1328 |
|
} |
1329 |
|
Thread.yield(); |
1330 |
|
} |
1332 |
|
|
1333 |
|
/** |
1334 |
|
* Spin-waits up to the specified number of milliseconds for the given |
1335 |
< |
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING, |
1318 |
< |
* and additionally satisfy the given condition. |
1335 |
> |
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
1336 |
|
*/ |
1337 |
< |
void waitForThreadToEnterWaitState( |
1338 |
< |
Thread thread, long timeoutMillis, Callable<Boolean> waitingForGodot) { |
1322 |
< |
long startTime = 0L; |
1323 |
< |
for (;;) { |
1324 |
< |
Thread.State s = thread.getState(); |
1325 |
< |
if (s == Thread.State.BLOCKED || |
1326 |
< |
s == Thread.State.WAITING || |
1327 |
< |
s == Thread.State.TIMED_WAITING) { |
1328 |
< |
try { |
1329 |
< |
if (waitingForGodot.call()) |
1330 |
< |
return; |
1331 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
1332 |
< |
} |
1333 |
< |
else if (s == Thread.State.TERMINATED) |
1334 |
< |
fail("Unexpected thread termination"); |
1335 |
< |
else if (startTime == 0L) |
1336 |
< |
startTime = System.nanoTime(); |
1337 |
< |
else if (millisElapsedSince(startTime) > timeoutMillis) { |
1338 |
< |
threadAssertTrue(thread.isAlive()); |
1339 |
< |
fail("timed out waiting for thread to enter wait state"); |
1340 |
< |
} |
1341 |
< |
Thread.yield(); |
1342 |
< |
} |
1337 |
> |
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
1338 |
> |
waitForThreadToEnterWaitState(thread, timeoutMillis, null); |
1339 |
|
} |
1340 |
|
|
1341 |
|
/** |
1343 |
|
* enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
1344 |
|
*/ |
1345 |
|
void waitForThreadToEnterWaitState(Thread thread) { |
1346 |
< |
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS); |
1346 |
> |
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS, null); |
1347 |
|
} |
1348 |
|
|
1349 |
|
/** |
1351 |
|
* enter a wait state: BLOCKED, WAITING, or TIMED_WAITING, |
1352 |
|
* and additionally satisfy the given condition. |
1353 |
|
*/ |
1354 |
< |
void waitForThreadToEnterWaitState( |
1355 |
< |
Thread thread, Callable<Boolean> waitingForGodot) { |
1354 |
> |
void waitForThreadToEnterWaitState(Thread thread, |
1355 |
> |
Callable<Boolean> waitingForGodot) { |
1356 |
|
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS, waitingForGodot); |
1357 |
|
} |
1358 |
|
|
1460 |
|
public final void run() { |
1461 |
|
try { |
1462 |
|
realRun(); |
1467 |
– |
threadShouldThrow(exceptionClass.getSimpleName()); |
1463 |
|
} catch (Throwable t) { |
1464 |
|
if (! exceptionClass.isInstance(t)) |
1465 |
|
threadUnexpectedException(t); |
1466 |
+ |
return; |
1467 |
|
} |
1468 |
+ |
threadShouldThrow(exceptionClass.getSimpleName()); |
1469 |
|
} |
1470 |
|
} |
1471 |
|
|
1475 |
|
public final void run() { |
1476 |
|
try { |
1477 |
|
realRun(); |
1481 |
– |
threadShouldThrow("InterruptedException"); |
1478 |
|
} catch (InterruptedException success) { |
1479 |
|
threadAssertFalse(Thread.interrupted()); |
1480 |
+ |
return; |
1481 |
|
} catch (Throwable fail) { |
1482 |
|
threadUnexpectedException(fail); |
1483 |
|
} |
1484 |
+ |
threadShouldThrow("InterruptedException"); |
1485 |
|
} |
1486 |
|
} |
1487 |
|
|
1493 |
|
return realCall(); |
1494 |
|
} catch (Throwable fail) { |
1495 |
|
threadUnexpectedException(fail); |
1498 |
– |
return null; |
1499 |
– |
} |
1500 |
– |
} |
1501 |
– |
} |
1502 |
– |
|
1503 |
– |
public abstract class CheckedInterruptedCallable<T> |
1504 |
– |
implements Callable<T> { |
1505 |
– |
protected abstract T realCall() throws Throwable; |
1506 |
– |
|
1507 |
– |
public final T call() { |
1508 |
– |
try { |
1509 |
– |
T result = realCall(); |
1510 |
– |
threadShouldThrow("InterruptedException"); |
1511 |
– |
return result; |
1512 |
– |
} catch (InterruptedException success) { |
1513 |
– |
threadAssertFalse(Thread.interrupted()); |
1514 |
– |
} catch (Throwable fail) { |
1515 |
– |
threadUnexpectedException(fail); |
1496 |
|
} |
1497 |
< |
return null; |
1497 |
> |
throw new AssertionError("unreached"); |
1498 |
|
} |
1499 |
|
} |
1500 |
|
|
1672 |
|
return realCompute(); |
1673 |
|
} catch (Throwable fail) { |
1674 |
|
threadUnexpectedException(fail); |
1695 |
– |
return null; |
1675 |
|
} |
1676 |
+ |
throw new AssertionError("unreached"); |
1677 |
|
} |
1678 |
|
} |
1679 |
|
|