12 |
|
import java.io.ObjectInputStream; |
13 |
|
import java.io.ObjectOutputStream; |
14 |
|
import java.util.Arrays; |
15 |
+ |
import java.util.Date; |
16 |
|
import java.util.NoSuchElementException; |
17 |
|
import java.util.PropertyPermission; |
18 |
|
import java.util.concurrent.*; |
19 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
20 |
|
import java.util.concurrent.atomic.AtomicReference; |
21 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
22 |
|
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
260 |
|
return 50; |
261 |
|
} |
262 |
|
|
261 |
– |
|
263 |
|
/** |
264 |
|
* Sets delays as multiples of SHORT_DELAY. |
265 |
|
*/ |
271 |
|
} |
272 |
|
|
273 |
|
/** |
274 |
+ |
* Returns a timeout in milliseconds to be used in tests that |
275 |
+ |
* verify that operations block or time out. |
276 |
+ |
*/ |
277 |
+ |
long timeoutMillis() { |
278 |
+ |
return SHORT_DELAY_MS / 4; |
279 |
+ |
} |
280 |
+ |
|
281 |
+ |
/** |
282 |
+ |
* Returns a new Date instance representing a time delayMillis |
283 |
+ |
* milliseconds in the future. |
284 |
+ |
*/ |
285 |
+ |
Date delayedDate(long delayMillis) { |
286 |
+ |
return new Date(System.currentTimeMillis() + delayMillis); |
287 |
+ |
} |
288 |
+ |
|
289 |
+ |
/** |
290 |
|
* The first exception encountered if any threadAssertXXX method fails. |
291 |
|
*/ |
292 |
|
private final AtomicReference<Throwable> threadFailure |
458 |
|
else { |
459 |
|
AssertionFailedError afe = |
460 |
|
new AssertionFailedError("unexpected exception: " + t); |
461 |
< |
t.initCause(t); |
461 |
> |
afe.initCause(t); |
462 |
|
throw afe; |
463 |
|
} |
464 |
|
} |
465 |
|
|
466 |
|
/** |
467 |
< |
* Delays, via Thread.sleep for the given millisecond delay, but |
467 |
> |
* Delays, via Thread.sleep, for the given millisecond delay, but |
468 |
|
* if the sleep is shorter than specified, may re-sleep or yield |
469 |
|
* until time elapses. |
470 |
|
*/ |
471 |
< |
public static void delay(long ms) throws InterruptedException { |
471 |
> |
static void delay(long millis) throws InterruptedException { |
472 |
|
long startTime = System.nanoTime(); |
473 |
< |
long ns = ms * 1000 * 1000; |
473 |
> |
long ns = millis * 1000 * 1000; |
474 |
|
for (;;) { |
475 |
< |
if (ms > 0L) |
476 |
< |
Thread.sleep(ms); |
475 |
> |
if (millis > 0L) |
476 |
> |
Thread.sleep(millis); |
477 |
|
else // too short to sleep |
478 |
|
Thread.yield(); |
479 |
|
long d = ns - (System.nanoTime() - startTime); |
480 |
|
if (d > 0L) |
481 |
< |
ms = d / (1000 * 1000); |
481 |
> |
millis = d / (1000 * 1000); |
482 |
|
else |
483 |
|
break; |
484 |
|
} |
487 |
|
/** |
488 |
|
* Waits out termination of a thread pool or fails doing so. |
489 |
|
*/ |
490 |
< |
public void joinPool(ExecutorService exec) { |
490 |
> |
void joinPool(ExecutorService exec) { |
491 |
|
try { |
492 |
|
exec.shutdown(); |
493 |
|
assertTrue("ExecutorService did not terminate in a timely manner", |
500 |
|
} |
501 |
|
|
502 |
|
/** |
503 |
< |
* Checks that thread does not terminate within timeoutMillis |
504 |
< |
* milliseconds (that is, Thread.join times out). |
503 |
> |
* Checks that thread does not terminate within the default |
504 |
> |
* millisecond delay of {@code timeoutMillis()}. |
505 |
|
*/ |
506 |
< |
public void assertThreadJoinTimesOut(Thread thread, long timeoutMillis) { |
506 |
> |
void assertThreadStaysAlive(Thread thread) { |
507 |
> |
assertThreadStaysAlive(thread, timeoutMillis()); |
508 |
> |
} |
509 |
> |
|
510 |
> |
/** |
511 |
> |
* Checks that thread does not terminate within the given millisecond delay. |
512 |
> |
*/ |
513 |
> |
void assertThreadStaysAlive(Thread thread, long millis) { |
514 |
|
try { |
515 |
< |
long startTime = System.nanoTime(); |
516 |
< |
thread.join(timeoutMillis); |
515 |
> |
// No need to optimize the failing case via Thread.join. |
516 |
> |
delay(millis); |
517 |
|
assertTrue(thread.isAlive()); |
494 |
– |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
518 |
|
} catch (InterruptedException ie) { |
519 |
|
fail("Unexpected InterruptedException"); |
520 |
|
} |
521 |
|
} |
522 |
|
|
523 |
|
/** |
524 |
+ |
* Checks that future.get times out, with the default timeout of |
525 |
+ |
* {@code timeoutMillis()}. |
526 |
+ |
*/ |
527 |
+ |
void assertFutureTimesOut(Future future) { |
528 |
+ |
assertFutureTimesOut(future, timeoutMillis()); |
529 |
+ |
} |
530 |
+ |
|
531 |
+ |
/** |
532 |
+ |
* Checks that future.get times out, with the given millisecond timeout. |
533 |
+ |
*/ |
534 |
+ |
void assertFutureTimesOut(Future future, long timeoutMillis) { |
535 |
+ |
long startTime = System.nanoTime(); |
536 |
+ |
try { |
537 |
+ |
future.get(timeoutMillis, MILLISECONDS); |
538 |
+ |
shouldThrow(); |
539 |
+ |
} catch (TimeoutException success) { |
540 |
+ |
} catch (Exception e) { |
541 |
+ |
threadUnexpectedException(e); |
542 |
+ |
} finally { future.cancel(true); } |
543 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
544 |
+ |
} |
545 |
+ |
|
546 |
+ |
/** |
547 |
|
* Fails with message "should throw exception". |
548 |
|
*/ |
549 |
|
public void shouldThrow() { |
683 |
|
} |
684 |
|
|
685 |
|
/** |
640 |
– |
* Sleeps until the timeout has elapsed, or interrupted. |
641 |
– |
* Does <em>NOT</em> throw InterruptedException. |
642 |
– |
*/ |
643 |
– |
void sleepTillInterrupted(long timeoutMillis) { |
644 |
– |
try { |
645 |
– |
Thread.sleep(timeoutMillis); |
646 |
– |
} catch (InterruptedException wakeup) {} |
647 |
– |
} |
648 |
– |
|
649 |
– |
/** |
686 |
|
* Waits up to the specified number of milliseconds for the given |
687 |
|
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
688 |
|
*/ |
743 |
|
} catch (InterruptedException ie) { |
744 |
|
threadUnexpectedException(ie); |
745 |
|
} finally { |
746 |
< |
if (t.isAlive()) { |
746 |
> |
if (t.getState() != Thread.State.TERMINATED) { |
747 |
|
t.interrupt(); |
748 |
|
fail("Test timed out"); |
749 |
|
} |
821 |
|
realRun(); |
822 |
|
threadShouldThrow("InterruptedException"); |
823 |
|
} catch (InterruptedException success) { |
824 |
+ |
threadAssertFalse(Thread.interrupted()); |
825 |
|
} catch (Throwable t) { |
826 |
|
threadUnexpectedException(t); |
827 |
|
} |
851 |
|
threadShouldThrow("InterruptedException"); |
852 |
|
return result; |
853 |
|
} catch (InterruptedException success) { |
854 |
+ |
threadAssertFalse(Thread.interrupted()); |
855 |
|
} catch (Throwable t) { |
856 |
|
threadUnexpectedException(t); |
857 |
|
} |
898 |
|
} |
899 |
|
} |
900 |
|
|
901 |
+ |
// /** |
902 |
+ |
// * Spin-waits up to LONG_DELAY_MS until flag becomes true. |
903 |
+ |
// */ |
904 |
+ |
// public void await(AtomicBoolean flag) { |
905 |
+ |
// await(flag, LONG_DELAY_MS); |
906 |
+ |
// } |
907 |
+ |
|
908 |
+ |
// /** |
909 |
+ |
// * Spin-waits up to the specified timeout until flag becomes true. |
910 |
+ |
// */ |
911 |
+ |
// public void await(AtomicBoolean flag, long timeoutMillis) { |
912 |
+ |
// long startTime = System.nanoTime(); |
913 |
+ |
// while (!flag.get()) { |
914 |
+ |
// if (millisElapsedSince(startTime) > timeoutMillis) |
915 |
+ |
// throw new AssertionFailedError("timed out"); |
916 |
+ |
// Thread.yield(); |
917 |
+ |
// } |
918 |
+ |
// } |
919 |
+ |
|
920 |
|
public static class NPETask implements Callable<String> { |
921 |
|
public String call() { throw new NullPointerException(); } |
922 |
|
} |
1137 |
|
} |
1138 |
|
} |
1139 |
|
|
1140 |
< |
public void checkEmpty(BlockingQueue q) { |
1140 |
> |
void checkEmpty(BlockingQueue q) { |
1141 |
|
try { |
1142 |
|
assertTrue(q.isEmpty()); |
1143 |
|
assertEquals(0, q.size()); |
1165 |
|
} |
1166 |
|
|
1167 |
|
@SuppressWarnings("unchecked") |
1168 |
< |
public <T> T serialClone(T o) { |
1168 |
> |
<T> T serialClone(T o) { |
1169 |
|
try { |
1170 |
|
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1171 |
|
ObjectOutputStream oos = new ObjectOutputStream(bos); |