528 |
|
} |
529 |
|
|
530 |
|
/** |
531 |
+ |
* Checks that the threads do not terminate within the default |
532 |
+ |
* millisecond delay of {@code timeoutMillis()}. |
533 |
+ |
*/ |
534 |
+ |
void assertThreadsStayAlive(Thread... threads) { |
535 |
+ |
assertThreadsStayAlive(timeoutMillis(), threads); |
536 |
+ |
} |
537 |
+ |
|
538 |
+ |
/** |
539 |
+ |
* Checks that the threads do not terminate within the given millisecond delay. |
540 |
+ |
*/ |
541 |
+ |
void assertThreadsStayAlive(long millis, Thread... threads) { |
542 |
+ |
try { |
543 |
+ |
// No need to optimize the failing case via Thread.join. |
544 |
+ |
delay(millis); |
545 |
+ |
for (Thread thread : threads) |
546 |
+ |
assertTrue(thread.isAlive()); |
547 |
+ |
} catch (InterruptedException ie) { |
548 |
+ |
fail("Unexpected InterruptedException"); |
549 |
+ |
} |
550 |
+ |
} |
551 |
+ |
|
552 |
+ |
/** |
553 |
|
* Checks that future.get times out, with the default timeout of |
554 |
|
* {@code timeoutMillis()}. |
555 |
|
*/ |
712 |
|
} |
713 |
|
|
714 |
|
/** |
715 |
< |
* Waits up to the specified number of milliseconds for the given |
715 |
> |
* Spin-waits up to the specified number of milliseconds for the given |
716 |
|
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
717 |
|
*/ |
718 |
|
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
719 |
< |
long timeoutNanos = timeoutMillis * 1000L * 1000L; |
698 |
< |
long t0 = System.nanoTime(); |
719 |
> |
long startTime = System.nanoTime(); |
720 |
|
for (;;) { |
721 |
|
Thread.State s = thread.getState(); |
722 |
|
if (s == Thread.State.BLOCKED || |
725 |
|
return; |
726 |
|
else if (s == Thread.State.TERMINATED) |
727 |
|
fail("Unexpected thread termination"); |
728 |
< |
else if (System.nanoTime() - t0 > timeoutNanos) { |
728 |
> |
else if (millisElapsedSince(startTime) > timeoutMillis) { |
729 |
|
threadAssertTrue(thread.isAlive()); |
730 |
|
return; |
731 |
|
} |
926 |
|
} |
927 |
|
} |
928 |
|
|
929 |
+ |
public void await(Semaphore semaphore) { |
930 |
+ |
try { |
931 |
+ |
assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
932 |
+ |
} catch (Throwable t) { |
933 |
+ |
threadUnexpectedException(t); |
934 |
+ |
} |
935 |
+ |
} |
936 |
+ |
|
937 |
|
// /** |
938 |
|
// * Spin-waits up to LONG_DELAY_MS until flag becomes true. |
939 |
|
// */ |
1210 |
|
oos.writeObject(o); |
1211 |
|
oos.flush(); |
1212 |
|
oos.close(); |
1213 |
< |
ByteArrayInputStream bin = |
1214 |
< |
new ByteArrayInputStream(bos.toByteArray()); |
1215 |
< |
ObjectInputStream ois = new ObjectInputStream(bin); |
1216 |
< |
return (T) ois.readObject(); |
1213 |
> |
ObjectInputStream ois = new ObjectInputStream |
1214 |
> |
(new ByteArrayInputStream(bos.toByteArray())); |
1215 |
> |
T clone = (T) ois.readObject(); |
1216 |
> |
assertSame(o.getClass(), clone.getClass()); |
1217 |
> |
return clone; |
1218 |
|
} catch (Throwable t) { |
1219 |
|
threadUnexpectedException(t); |
1220 |
|
return null; |