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; |
272 |
|
} |
273 |
|
|
274 |
|
/** |
275 |
+ |
* Returns a timeout in milliseconds to be used in tests that |
276 |
+ |
* verify that operations block or time out. |
277 |
+ |
*/ |
278 |
+ |
long timeoutMillis() { |
279 |
+ |
return SHORT_DELAY_MS / 4; |
280 |
+ |
} |
281 |
+ |
|
282 |
+ |
/** |
283 |
+ |
* Returns a new Date instance representing a time delayMillis |
284 |
+ |
* milliseconds in the future. |
285 |
+ |
*/ |
286 |
+ |
Date delayedDate(long delayMillis) { |
287 |
+ |
return new Date(new Date().getTime() + delayMillis); |
288 |
+ |
} |
289 |
+ |
|
290 |
+ |
/** |
291 |
|
* The first exception encountered if any threadAssertXXX method fails. |
292 |
|
*/ |
293 |
|
private final AtomicReference<Throwable> threadFailure |
465 |
|
} |
466 |
|
|
467 |
|
/** |
468 |
< |
* Delays, via Thread.sleep for the given millisecond delay, but |
468 |
> |
* Delays, via Thread.sleep, for the given millisecond delay, but |
469 |
|
* if the sleep is shorter than specified, may re-sleep or yield |
470 |
|
* until time elapses. |
471 |
|
*/ |
472 |
< |
public static void delay(long millis) throws InterruptedException { |
472 |
> |
static void delay(long millis) throws InterruptedException { |
473 |
|
long startTime = System.nanoTime(); |
474 |
|
long ns = millis * 1000 * 1000; |
475 |
|
for (;;) { |
488 |
|
/** |
489 |
|
* Waits out termination of a thread pool or fails doing so. |
490 |
|
*/ |
491 |
< |
public void joinPool(ExecutorService exec) { |
491 |
> |
void joinPool(ExecutorService exec) { |
492 |
|
try { |
493 |
|
exec.shutdown(); |
494 |
|
assertTrue("ExecutorService did not terminate in a timely manner", |
501 |
|
} |
502 |
|
|
503 |
|
/** |
504 |
+ |
* Checks that thread does not terminate within the default |
505 |
+ |
* millisecond delay of {@code timeoutMillis()}. |
506 |
+ |
*/ |
507 |
+ |
void assertThreadStaysAlive(Thread thread) { |
508 |
+ |
assertThreadStaysAlive(thread, timeoutMillis()); |
509 |
+ |
} |
510 |
+ |
|
511 |
+ |
/** |
512 |
|
* Checks that thread does not terminate within the given millisecond delay. |
513 |
|
*/ |
514 |
< |
public void assertThreadStaysAlive(Thread thread, long millis) { |
514 |
> |
void assertThreadStaysAlive(Thread thread, long millis) { |
515 |
|
try { |
516 |
|
// No need to optimize the failing case via Thread.join. |
517 |
|
delay(millis); |
661 |
|
} |
662 |
|
|
663 |
|
/** |
638 |
– |
* Sleeps until the timeout has elapsed, or interrupted. |
639 |
– |
* Does <em>NOT</em> throw InterruptedException. |
640 |
– |
*/ |
641 |
– |
void sleepTillInterrupted(long timeoutMillis) { |
642 |
– |
try { |
643 |
– |
Thread.sleep(timeoutMillis); |
644 |
– |
} catch (InterruptedException wakeup) {} |
645 |
– |
} |
646 |
– |
|
647 |
– |
/** |
664 |
|
* Waits up to the specified number of milliseconds for the given |
665 |
|
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
666 |
|
*/ |
799 |
|
realRun(); |
800 |
|
threadShouldThrow("InterruptedException"); |
801 |
|
} catch (InterruptedException success) { |
802 |
+ |
threadAssertFalse(Thread.interrupted()); |
803 |
|
} catch (Throwable t) { |
804 |
|
threadUnexpectedException(t); |
805 |
|
} |
829 |
|
threadShouldThrow("InterruptedException"); |
830 |
|
return result; |
831 |
|
} catch (InterruptedException success) { |
832 |
+ |
threadAssertFalse(Thread.interrupted()); |
833 |
|
} catch (Throwable t) { |
834 |
|
threadUnexpectedException(t); |
835 |
|
} |
876 |
|
} |
877 |
|
} |
878 |
|
|
879 |
+ |
// /** |
880 |
+ |
// * Spin-waits up to LONG_DELAY_MS until flag becomes true. |
881 |
+ |
// */ |
882 |
+ |
// public void await(AtomicBoolean flag) { |
883 |
+ |
// await(flag, LONG_DELAY_MS); |
884 |
+ |
// } |
885 |
+ |
|
886 |
+ |
// /** |
887 |
+ |
// * Spin-waits up to the specified timeout until flag becomes true. |
888 |
+ |
// */ |
889 |
+ |
// public void await(AtomicBoolean flag, long timeoutMillis) { |
890 |
+ |
// long startTime = System.nanoTime(); |
891 |
+ |
// while (!flag.get()) { |
892 |
+ |
// if (millisElapsedSince(startTime) > timeoutMillis) |
893 |
+ |
// throw new AssertionFailedError("timed out"); |
894 |
+ |
// Thread.yield(); |
895 |
+ |
// } |
896 |
+ |
// } |
897 |
+ |
|
898 |
|
public static class NPETask implements Callable<String> { |
899 |
|
public String call() { throw new NullPointerException(); } |
900 |
|
} |
1115 |
|
} |
1116 |
|
} |
1117 |
|
|
1118 |
< |
public void checkEmpty(BlockingQueue q) { |
1118 |
> |
void checkEmpty(BlockingQueue q) { |
1119 |
|
try { |
1120 |
|
assertTrue(q.isEmpty()); |
1121 |
|
assertEquals(0, q.size()); |
1143 |
|
} |
1144 |
|
|
1145 |
|
@SuppressWarnings("unchecked") |
1146 |
< |
public <T> T serialClone(T o) { |
1146 |
> |
<T> T serialClone(T o) { |
1147 |
|
try { |
1148 |
|
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1149 |
|
ObjectOutputStream oos = new ObjectOutputStream(bos); |