451 |
|
* if the sleep is shorter than specified, may re-sleep or yield |
452 |
|
* until time elapses. |
453 |
|
*/ |
454 |
< |
public static void delay(long ms) throws InterruptedException { |
454 |
> |
public static void delay(long millis) throws InterruptedException { |
455 |
|
long startTime = System.nanoTime(); |
456 |
< |
long ns = ms * 1000 * 1000; |
456 |
> |
long ns = millis * 1000 * 1000; |
457 |
|
for (;;) { |
458 |
< |
if (ms > 0L) |
459 |
< |
Thread.sleep(ms); |
458 |
> |
if (millis > 0L) |
459 |
> |
Thread.sleep(millis); |
460 |
|
else // too short to sleep |
461 |
|
Thread.yield(); |
462 |
|
long d = ns - (System.nanoTime() - startTime); |
463 |
|
if (d > 0L) |
464 |
< |
ms = d / (1000 * 1000); |
464 |
> |
millis = d / (1000 * 1000); |
465 |
|
else |
466 |
|
break; |
467 |
|
} |
483 |
|
} |
484 |
|
|
485 |
|
/** |
486 |
< |
* Checks that thread does not terminate within timeoutMillis |
487 |
< |
* milliseconds (that is, Thread.join times out). |
486 |
> |
* Checks that thread does not terminate within the given millisecond delay. |
487 |
|
*/ |
488 |
< |
public void assertThreadJoinTimesOut(Thread thread, long timeoutMillis) { |
488 |
> |
public void assertThreadStaysAlive(Thread thread, long millis) { |
489 |
|
try { |
490 |
< |
long startTime = System.nanoTime(); |
491 |
< |
thread.join(timeoutMillis); |
490 |
> |
// No need to optimize the failing case via Thread.join. |
491 |
> |
delay(millis); |
492 |
|
assertTrue(thread.isAlive()); |
494 |
– |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
493 |
|
} catch (InterruptedException ie) { |
494 |
|
fail("Unexpected InterruptedException"); |
495 |
|
} |