260 |
|
return 50; |
261 |
|
} |
262 |
|
|
263 |
– |
|
263 |
|
/** |
264 |
|
* Sets delays as multiples of SHORT_DELAY. |
265 |
|
*/ |
283 |
|
* milliseconds in the future. |
284 |
|
*/ |
285 |
|
Date delayedDate(long delayMillis) { |
286 |
< |
return new Date(new Date().getTime() + delayMillis); |
286 |
> |
return new Date(System.currentTimeMillis() + delayMillis); |
287 |
|
} |
288 |
|
|
289 |
|
/** |
307 |
|
} |
308 |
|
|
309 |
|
/** |
310 |
+ |
* Extra checks that get done for all test cases. |
311 |
+ |
* |
312 |
|
* Triggers test case failure if any thread assertions have failed, |
313 |
|
* by rethrowing, in the test harness thread, any exception recorded |
314 |
|
* earlier by threadRecordFailure. |
315 |
+ |
* |
316 |
+ |
* Triggers test case failure if interrupt status is set in the main thread. |
317 |
|
*/ |
318 |
|
public void tearDown() throws Exception { |
319 |
|
Throwable t = threadFailure.getAndSet(null); |
331 |
|
throw afe; |
332 |
|
} |
333 |
|
} |
334 |
+ |
|
335 |
+ |
if (Thread.interrupted()) |
336 |
+ |
throw new AssertionFailedError("interrupt status set in main thread"); |
337 |
|
} |
338 |
|
|
339 |
|
/** |
528 |
|
} |
529 |
|
|
530 |
|
/** |
531 |
+ |
* Checks that future.get times out, with the default timeout of |
532 |
+ |
* {@code timeoutMillis()}. |
533 |
+ |
*/ |
534 |
+ |
void assertFutureTimesOut(Future future) { |
535 |
+ |
assertFutureTimesOut(future, timeoutMillis()); |
536 |
+ |
} |
537 |
+ |
|
538 |
+ |
/** |
539 |
+ |
* Checks that future.get times out, with the given millisecond timeout. |
540 |
+ |
*/ |
541 |
+ |
void assertFutureTimesOut(Future future, long timeoutMillis) { |
542 |
+ |
long startTime = System.nanoTime(); |
543 |
+ |
try { |
544 |
+ |
future.get(timeoutMillis, MILLISECONDS); |
545 |
+ |
shouldThrow(); |
546 |
+ |
} catch (TimeoutException success) { |
547 |
+ |
} catch (Exception e) { |
548 |
+ |
threadUnexpectedException(e); |
549 |
+ |
} finally { future.cancel(true); } |
550 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
551 |
+ |
} |
552 |
+ |
|
553 |
+ |
/** |
554 |
|
* Fails with message "should throw exception". |
555 |
|
*/ |
556 |
|
public void shouldThrow() { |
750 |
|
} catch (InterruptedException ie) { |
751 |
|
threadUnexpectedException(ie); |
752 |
|
} finally { |
753 |
< |
if (t.isAlive()) { |
753 |
> |
if (t.getState() != Thread.State.TERMINATED) { |
754 |
|
t.interrupt(); |
755 |
|
fail("Test timed out"); |
756 |
|
} |
1126 |
|
} |
1127 |
|
|
1128 |
|
/** |
1129 |
< |
* A CyclicBarrier that fails with AssertionFailedErrors instead |
1130 |
< |
* of throwing checked exceptions. |
1129 |
> |
* A CyclicBarrier that uses timed await and fails with |
1130 |
> |
* AssertionFailedErrors instead of throwing checked exceptions. |
1131 |
|
*/ |
1132 |
|
public class CheckedBarrier extends CyclicBarrier { |
1133 |
|
public CheckedBarrier(int parties) { super(parties); } |
1134 |
|
|
1135 |
|
public int await() { |
1136 |
|
try { |
1137 |
< |
return super.await(); |
1137 |
> |
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1138 |
> |
} catch (TimeoutException e) { |
1139 |
> |
throw new AssertionFailedError("timed out"); |
1140 |
|
} catch (Exception e) { |
1141 |
|
AssertionFailedError afe = |
1142 |
|
new AssertionFailedError("Unexpected exception: " + e); |
1181 |
|
oos.writeObject(o); |
1182 |
|
oos.flush(); |
1183 |
|
oos.close(); |
1184 |
< |
ByteArrayInputStream bin = |
1185 |
< |
new ByteArrayInputStream(bos.toByteArray()); |
1186 |
< |
ObjectInputStream ois = new ObjectInputStream(bin); |
1187 |
< |
return (T) ois.readObject(); |
1184 |
> |
ObjectInputStream ois = new ObjectInputStream |
1185 |
> |
(new ByteArrayInputStream(bos.toByteArray())); |
1186 |
> |
T clone = (T) ois.readObject(); |
1187 |
> |
assertSame(o.getClass(), clone.getClass()); |
1188 |
> |
return clone; |
1189 |
|
} catch (Throwable t) { |
1190 |
|
threadUnexpectedException(t); |
1191 |
|
return null; |