690 |
|
} |
691 |
|
|
692 |
|
/** |
693 |
< |
* Waits up to the specified number of milliseconds for the given |
693 |
> |
* Spin-waits up to the specified number of milliseconds for the given |
694 |
|
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
695 |
|
*/ |
696 |
|
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
697 |
< |
long timeoutNanos = timeoutMillis * 1000L * 1000L; |
698 |
< |
long t0 = System.nanoTime(); |
697 |
> |
long startTime = System.nanoTime(); |
698 |
|
for (;;) { |
699 |
|
Thread.State s = thread.getState(); |
700 |
|
if (s == Thread.State.BLOCKED || |
703 |
|
return; |
704 |
|
else if (s == Thread.State.TERMINATED) |
705 |
|
fail("Unexpected thread termination"); |
706 |
< |
else if (System.nanoTime() - t0 > timeoutNanos) { |
706 |
> |
else if (millisElapsedSince(startTime) > timeoutMillis) { |
707 |
|
threadAssertTrue(thread.isAlive()); |
708 |
|
return; |
709 |
|
} |
904 |
|
} |
905 |
|
} |
906 |
|
|
907 |
+ |
public void await(Semaphore semaphore) { |
908 |
+ |
try { |
909 |
+ |
assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
910 |
+ |
} catch (Throwable t) { |
911 |
+ |
threadUnexpectedException(t); |
912 |
+ |
} |
913 |
+ |
} |
914 |
+ |
|
915 |
|
// /** |
916 |
|
// * Spin-waits up to LONG_DELAY_MS until flag becomes true. |
917 |
|
// */ |
1133 |
|
} |
1134 |
|
|
1135 |
|
/** |
1136 |
< |
* A CyclicBarrier that fails with AssertionFailedErrors instead |
1137 |
< |
* of throwing checked exceptions. |
1136 |
> |
* A CyclicBarrier that uses timed await and fails with |
1137 |
> |
* AssertionFailedErrors instead of throwing checked exceptions. |
1138 |
|
*/ |
1139 |
|
public class CheckedBarrier extends CyclicBarrier { |
1140 |
|
public CheckedBarrier(int parties) { super(parties); } |
1141 |
|
|
1142 |
|
public int await() { |
1143 |
|
try { |
1144 |
< |
return super.await(); |
1144 |
> |
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1145 |
> |
} catch (TimeoutException e) { |
1146 |
> |
throw new AssertionFailedError("timed out"); |
1147 |
|
} catch (Exception e) { |
1148 |
|
AssertionFailedError afe = |
1149 |
|
new AssertionFailedError("Unexpected exception: " + e); |
1188 |
|
oos.writeObject(o); |
1189 |
|
oos.flush(); |
1190 |
|
oos.close(); |
1191 |
< |
ByteArrayInputStream bin = |
1192 |
< |
new ByteArrayInputStream(bos.toByteArray()); |
1193 |
< |
ObjectInputStream ois = new ObjectInputStream(bin); |
1194 |
< |
return (T) ois.readObject(); |
1191 |
> |
ObjectInputStream ois = new ObjectInputStream |
1192 |
> |
(new ByteArrayInputStream(bos.toByteArray())); |
1193 |
> |
T clone = (T) ois.readObject(); |
1194 |
> |
assertSame(o.getClass(), clone.getClass()); |
1195 |
> |
return clone; |
1196 |
|
} catch (Throwable t) { |
1197 |
|
threadUnexpectedException(t); |
1198 |
|
return null; |