69 |
|
* |
70 |
|
* </ol> |
71 |
|
* |
72 |
< |
* <p> <b>Other notes</b> |
72 |
> |
* <p><b>Other notes</b> |
73 |
|
* <ul> |
74 |
|
* |
75 |
|
* <li> Usually, there is one testcase method per JSR166 method |
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 |
|
/** |
465 |
|
else { |
466 |
|
AssertionFailedError afe = |
467 |
|
new AssertionFailedError("unexpected exception: " + t); |
468 |
< |
t.initCause(t); |
468 |
> |
afe.initCause(t); |
469 |
|
throw afe; |
470 |
|
} |
471 |
|
} |
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 |
+ |
*/ |
556 |
+ |
void assertFutureTimesOut(Future future) { |
557 |
+ |
assertFutureTimesOut(future, timeoutMillis()); |
558 |
+ |
} |
559 |
+ |
|
560 |
+ |
/** |
561 |
+ |
* Checks that future.get times out, with the given millisecond timeout. |
562 |
+ |
*/ |
563 |
+ |
void assertFutureTimesOut(Future future, long timeoutMillis) { |
564 |
+ |
long startTime = System.nanoTime(); |
565 |
+ |
try { |
566 |
+ |
future.get(timeoutMillis, MILLISECONDS); |
567 |
+ |
shouldThrow(); |
568 |
+ |
} catch (TimeoutException success) { |
569 |
+ |
} catch (Exception e) { |
570 |
+ |
threadUnexpectedException(e); |
571 |
+ |
} finally { future.cancel(true); } |
572 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
573 |
+ |
} |
574 |
+ |
|
575 |
+ |
/** |
576 |
|
* Fails with message "should throw exception". |
577 |
|
*/ |
578 |
|
public void shouldThrow() { |
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; |
669 |
< |
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 |
|
} |
771 |
|
} catch (InterruptedException ie) { |
772 |
|
threadUnexpectedException(ie); |
773 |
|
} finally { |
774 |
< |
if (t.isAlive()) { |
774 |
> |
if (t.getState() != Thread.State.TERMINATED) { |
775 |
|
t.interrupt(); |
776 |
|
fail("Test timed out"); |
777 |
|
} |
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 |
|
// */ |
1155 |
|
} |
1156 |
|
|
1157 |
|
/** |
1158 |
< |
* A CyclicBarrier that fails with AssertionFailedErrors instead |
1159 |
< |
* of throwing checked exceptions. |
1158 |
> |
* A CyclicBarrier that uses timed await and fails with |
1159 |
> |
* AssertionFailedErrors instead of throwing checked exceptions. |
1160 |
|
*/ |
1161 |
|
public class CheckedBarrier extends CyclicBarrier { |
1162 |
|
public CheckedBarrier(int parties) { super(parties); } |
1163 |
|
|
1164 |
|
public int await() { |
1165 |
|
try { |
1166 |
< |
return super.await(); |
1166 |
> |
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1167 |
> |
} catch (TimeoutException e) { |
1168 |
> |
throw new AssertionFailedError("timed out"); |
1169 |
|
} catch (Exception e) { |
1170 |
|
AssertionFailedError afe = |
1171 |
|
new AssertionFailedError("Unexpected exception: " + e); |
1202 |
|
} |
1203 |
|
} |
1204 |
|
|
1205 |
< |
@SuppressWarnings("unchecked") |
1206 |
< |
<T> T serialClone(T o) { |
1205 |
> |
void assertSerialEquals(Object x, Object y) { |
1206 |
> |
assertTrue(Arrays.equals(serialBytes(x), serialBytes(y))); |
1207 |
> |
} |
1208 |
> |
|
1209 |
> |
void assertNotSerialEquals(Object x, Object y) { |
1210 |
> |
assertFalse(Arrays.equals(serialBytes(x), serialBytes(y))); |
1211 |
> |
} |
1212 |
> |
|
1213 |
> |
byte[] serialBytes(Object o) { |
1214 |
|
try { |
1215 |
|
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1216 |
|
ObjectOutputStream oos = new ObjectOutputStream(bos); |
1217 |
|
oos.writeObject(o); |
1218 |
|
oos.flush(); |
1219 |
|
oos.close(); |
1220 |
< |
ByteArrayInputStream bin = |
1221 |
< |
new ByteArrayInputStream(bos.toByteArray()); |
1222 |
< |
ObjectInputStream ois = new ObjectInputStream(bin); |
1223 |
< |
return (T) ois.readObject(); |
1220 |
> |
return bos.toByteArray(); |
1221 |
> |
} catch (Throwable t) { |
1222 |
> |
threadUnexpectedException(t); |
1223 |
> |
return new byte[0]; |
1224 |
> |
} |
1225 |
> |
} |
1226 |
> |
|
1227 |
> |
@SuppressWarnings("unchecked") |
1228 |
> |
<T> T serialClone(T o) { |
1229 |
> |
try { |
1230 |
> |
ObjectInputStream ois = new ObjectInputStream |
1231 |
> |
(new ByteArrayInputStream(serialBytes(o))); |
1232 |
> |
T clone = (T) ois.readObject(); |
1233 |
> |
assertSame(o.getClass(), clone.getClass()); |
1234 |
> |
return clone; |
1235 |
|
} catch (Throwable t) { |
1236 |
|
threadUnexpectedException(t); |
1237 |
|
return null; |