1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
+ |
import java.io.ByteArrayInputStream; |
11 |
+ |
import java.io.ByteArrayOutputStream; |
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; |
23 |
|
import java.security.CodeSource; |
24 |
|
import java.security.Permission; |
25 |
|
import java.security.PermissionCollection; |
268 |
|
SHORT_DELAY_MS = getShortDelay(); |
269 |
|
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
270 |
|
MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10; |
271 |
< |
LONG_DELAY_MS = SHORT_DELAY_MS * 50; |
271 |
> |
LONG_DELAY_MS = SHORT_DELAY_MS * 200; |
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 |
|
/** |
313 |
|
* earlier by threadRecordFailure. |
314 |
|
*/ |
315 |
|
public void tearDown() throws Exception { |
316 |
< |
Throwable t = threadFailure.get(); |
316 |
> |
Throwable t = threadFailure.getAndSet(null); |
317 |
|
if (t != null) { |
318 |
|
if (t instanceof Error) |
319 |
|
throw (Error) t; |
465 |
|
} |
466 |
|
|
467 |
|
/** |
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 |
+ |
static void delay(long millis) throws InterruptedException { |
473 |
+ |
long startTime = System.nanoTime(); |
474 |
+ |
long ns = millis * 1000 * 1000; |
475 |
+ |
for (;;) { |
476 |
+ |
if (millis > 0L) |
477 |
+ |
Thread.sleep(millis); |
478 |
+ |
else // too short to sleep |
479 |
+ |
Thread.yield(); |
480 |
+ |
long d = ns - (System.nanoTime() - startTime); |
481 |
+ |
if (d > 0L) |
482 |
+ |
millis = d / (1000 * 1000); |
483 |
+ |
else |
484 |
+ |
break; |
485 |
+ |
} |
486 |
+ |
} |
487 |
+ |
|
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", |
495 |
< |
exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
495 |
> |
exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)); |
496 |
|
} catch (SecurityException ok) { |
497 |
|
// Allowed in case test doesn't have privs |
498 |
|
} catch (InterruptedException ie) { |
500 |
|
} |
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 |
+ |
void assertThreadStaysAlive(Thread thread, long millis) { |
515 |
+ |
try { |
516 |
+ |
// No need to optimize the failing case via Thread.join. |
517 |
+ |
delay(millis); |
518 |
+ |
assertTrue(thread.isAlive()); |
519 |
+ |
} catch (InterruptedException ie) { |
520 |
+ |
fail("Unexpected InterruptedException"); |
521 |
+ |
} |
522 |
+ |
} |
523 |
|
|
524 |
|
/** |
525 |
|
* Fails with message "should throw exception". |
651 |
|
*/ |
652 |
|
void sleep(long millis) { |
653 |
|
try { |
654 |
< |
Thread.sleep(millis); |
654 |
> |
delay(millis); |
655 |
|
} catch (InterruptedException ie) { |
656 |
|
AssertionFailedError afe = |
657 |
|
new AssertionFailedError("Unexpected InterruptedException"); |
661 |
|
} |
662 |
|
|
663 |
|
/** |
664 |
< |
* Sleeps until the timeout has elapsed, or interrupted. |
665 |
< |
* Does <em>NOT</em> throw InterruptedException. |
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 |
|
*/ |
667 |
< |
void sleepTillInterrupted(long timeoutMillis) { |
668 |
< |
try { |
669 |
< |
Thread.sleep(timeoutMillis); |
670 |
< |
} catch (InterruptedException wakeup) {} |
667 |
> |
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
668 |
> |
long timeoutNanos = timeoutMillis * 1000L * 1000L; |
669 |
> |
long t0 = System.nanoTime(); |
670 |
> |
for (;;) { |
671 |
> |
Thread.State s = thread.getState(); |
672 |
> |
if (s == Thread.State.BLOCKED || |
673 |
> |
s == Thread.State.WAITING || |
674 |
> |
s == Thread.State.TIMED_WAITING) |
675 |
> |
return; |
676 |
> |
else if (s == Thread.State.TERMINATED) |
677 |
> |
fail("Unexpected thread termination"); |
678 |
> |
else if (System.nanoTime() - t0 > timeoutNanos) { |
679 |
> |
threadAssertTrue(thread.isAlive()); |
680 |
> |
return; |
681 |
> |
} |
682 |
> |
Thread.yield(); |
683 |
> |
} |
684 |
> |
} |
685 |
> |
|
686 |
> |
/** |
687 |
> |
* Waits up to LONG_DELAY_MS for the given thread to enter a wait |
688 |
> |
* state: BLOCKED, WAITING, or TIMED_WAITING. |
689 |
> |
*/ |
690 |
> |
void waitForThreadToEnterWaitState(Thread thread) { |
691 |
> |
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS); |
692 |
> |
} |
693 |
> |
|
694 |
> |
/** |
695 |
> |
* Returns the number of milliseconds since time given by |
696 |
> |
* startNanoTime, which must have been previously returned from a |
697 |
> |
* call to {@link System.nanoTime()}. |
698 |
> |
*/ |
699 |
> |
long millisElapsedSince(long startNanoTime) { |
700 |
> |
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
701 |
|
} |
702 |
|
|
703 |
|
/** |
728 |
|
} |
729 |
|
} |
730 |
|
|
731 |
+ |
/** |
732 |
+ |
* Waits for LONG_DELAY_MS milliseconds for the thread to |
733 |
+ |
* terminate (using {@link Thread#join(long)}), else interrupts |
734 |
+ |
* the thread (in the hope that it may terminate later) and fails. |
735 |
+ |
*/ |
736 |
+ |
void awaitTermination(Thread t) { |
737 |
+ |
awaitTermination(t, LONG_DELAY_MS); |
738 |
+ |
} |
739 |
+ |
|
740 |
|
// Some convenient Runnable classes |
741 |
|
|
742 |
|
public abstract class CheckedRunnable implements Runnable { |
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 |
|
} |
853 |
|
|
854 |
|
public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) { |
855 |
|
return new CheckedCallable<String>() { |
856 |
< |
public String realCall() { |
856 |
> |
protected String realCall() { |
857 |
|
try { |
858 |
|
latch.await(); |
859 |
|
} catch (InterruptedException quittingTime) {} |
861 |
|
}}; |
862 |
|
} |
863 |
|
|
864 |
+ |
public Runnable awaiter(final CountDownLatch latch) { |
865 |
+ |
return new CheckedRunnable() { |
866 |
+ |
public void realRun() throws InterruptedException { |
867 |
+ |
await(latch); |
868 |
+ |
}}; |
869 |
+ |
} |
870 |
+ |
|
871 |
+ |
public void await(CountDownLatch latch) { |
872 |
+ |
try { |
873 |
+ |
assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); |
874 |
+ |
} catch (Throwable t) { |
875 |
+ |
threadUnexpectedException(t); |
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 |
|
} |
905 |
|
|
906 |
|
public class ShortRunnable extends CheckedRunnable { |
907 |
|
protected void realRun() throws Throwable { |
908 |
< |
Thread.sleep(SHORT_DELAY_MS); |
908 |
> |
delay(SHORT_DELAY_MS); |
909 |
|
} |
910 |
|
} |
911 |
|
|
912 |
|
public class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
913 |
|
protected void realRun() throws InterruptedException { |
914 |
< |
Thread.sleep(SHORT_DELAY_MS); |
914 |
> |
delay(SHORT_DELAY_MS); |
915 |
|
} |
916 |
|
} |
917 |
|
|
918 |
|
public class SmallRunnable extends CheckedRunnable { |
919 |
|
protected void realRun() throws Throwable { |
920 |
< |
Thread.sleep(SMALL_DELAY_MS); |
920 |
> |
delay(SMALL_DELAY_MS); |
921 |
|
} |
922 |
|
} |
923 |
|
|
924 |
|
public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
925 |
|
protected void realRun() { |
926 |
|
try { |
927 |
< |
Thread.sleep(SMALL_DELAY_MS); |
927 |
> |
delay(SMALL_DELAY_MS); |
928 |
|
} catch (InterruptedException ok) {} |
929 |
|
} |
930 |
|
} |
931 |
|
|
932 |
|
public class SmallCallable extends CheckedCallable { |
933 |
|
protected Object realCall() throws InterruptedException { |
934 |
< |
Thread.sleep(SMALL_DELAY_MS); |
934 |
> |
delay(SMALL_DELAY_MS); |
935 |
|
return Boolean.TRUE; |
936 |
|
} |
937 |
|
} |
938 |
|
|
939 |
|
public class MediumRunnable extends CheckedRunnable { |
940 |
|
protected void realRun() throws Throwable { |
941 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
941 |
> |
delay(MEDIUM_DELAY_MS); |
942 |
|
} |
943 |
|
} |
944 |
|
|
945 |
|
public class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
946 |
|
protected void realRun() throws InterruptedException { |
947 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
947 |
> |
delay(MEDIUM_DELAY_MS); |
948 |
|
} |
949 |
|
} |
950 |
|
|
952 |
|
return new CheckedRunnable() { |
953 |
|
protected void realRun() { |
954 |
|
try { |
955 |
< |
Thread.sleep(timeoutMillis); |
955 |
> |
delay(timeoutMillis); |
956 |
|
} catch (InterruptedException ok) {} |
957 |
|
}}; |
958 |
|
} |
960 |
|
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
961 |
|
protected void realRun() { |
962 |
|
try { |
963 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
963 |
> |
delay(MEDIUM_DELAY_MS); |
964 |
|
} catch (InterruptedException ok) {} |
965 |
|
} |
966 |
|
} |
968 |
|
public class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
969 |
|
protected void realRun() { |
970 |
|
try { |
971 |
< |
Thread.sleep(LONG_DELAY_MS); |
971 |
> |
delay(LONG_DELAY_MS); |
972 |
|
} catch (InterruptedException ok) {} |
973 |
|
} |
974 |
|
} |
992 |
|
public boolean isDone() { return done; } |
993 |
|
public void run() { |
994 |
|
try { |
995 |
< |
Thread.sleep(timeoutMillis); |
995 |
> |
delay(timeoutMillis); |
996 |
|
done = true; |
997 |
|
} catch (InterruptedException ok) {} |
998 |
|
} |
1003 |
|
public volatile boolean done = false; |
1004 |
|
public void run() { |
1005 |
|
try { |
1006 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1006 |
> |
delay(SHORT_DELAY_MS); |
1007 |
|
done = true; |
1008 |
|
} catch (InterruptedException ok) {} |
1009 |
|
} |
1013 |
|
public volatile boolean done = false; |
1014 |
|
public void run() { |
1015 |
|
try { |
1016 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1016 |
> |
delay(SMALL_DELAY_MS); |
1017 |
|
done = true; |
1018 |
|
} catch (InterruptedException ok) {} |
1019 |
|
} |
1023 |
|
public volatile boolean done = false; |
1024 |
|
public void run() { |
1025 |
|
try { |
1026 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1026 |
> |
delay(MEDIUM_DELAY_MS); |
1027 |
|
done = true; |
1028 |
|
} catch (InterruptedException ok) {} |
1029 |
|
} |
1033 |
|
public volatile boolean done = false; |
1034 |
|
public void run() { |
1035 |
|
try { |
1036 |
< |
Thread.sleep(LONG_DELAY_MS); |
1036 |
> |
delay(LONG_DELAY_MS); |
1037 |
|
done = true; |
1038 |
|
} catch (InterruptedException ok) {} |
1039 |
|
} |
1050 |
|
public volatile boolean done = false; |
1051 |
|
public Object call() { |
1052 |
|
try { |
1053 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1053 |
> |
delay(SMALL_DELAY_MS); |
1054 |
|
done = true; |
1055 |
|
} catch (InterruptedException ok) {} |
1056 |
|
return Boolean.TRUE; |
1115 |
|
} |
1116 |
|
} |
1117 |
|
|
1118 |
+ |
void checkEmpty(BlockingQueue q) { |
1119 |
+ |
try { |
1120 |
+ |
assertTrue(q.isEmpty()); |
1121 |
+ |
assertEquals(0, q.size()); |
1122 |
+ |
assertNull(q.peek()); |
1123 |
+ |
assertNull(q.poll()); |
1124 |
+ |
assertNull(q.poll(0, MILLISECONDS)); |
1125 |
+ |
assertEquals(q.toString(), "[]"); |
1126 |
+ |
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
1127 |
+ |
assertFalse(q.iterator().hasNext()); |
1128 |
+ |
try { |
1129 |
+ |
q.element(); |
1130 |
+ |
shouldThrow(); |
1131 |
+ |
} catch (NoSuchElementException success) {} |
1132 |
+ |
try { |
1133 |
+ |
q.iterator().next(); |
1134 |
+ |
shouldThrow(); |
1135 |
+ |
} catch (NoSuchElementException success) {} |
1136 |
+ |
try { |
1137 |
+ |
q.remove(); |
1138 |
+ |
shouldThrow(); |
1139 |
+ |
} catch (NoSuchElementException success) {} |
1140 |
+ |
} catch (InterruptedException ie) { |
1141 |
+ |
threadUnexpectedException(ie); |
1142 |
+ |
} |
1143 |
+ |
} |
1144 |
+ |
|
1145 |
+ |
@SuppressWarnings("unchecked") |
1146 |
+ |
<T> T serialClone(T o) { |
1147 |
+ |
try { |
1148 |
+ |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1149 |
+ |
ObjectOutputStream oos = new ObjectOutputStream(bos); |
1150 |
+ |
oos.writeObject(o); |
1151 |
+ |
oos.flush(); |
1152 |
+ |
oos.close(); |
1153 |
+ |
ByteArrayInputStream bin = |
1154 |
+ |
new ByteArrayInputStream(bos.toByteArray()); |
1155 |
+ |
ObjectInputStream ois = new ObjectInputStream(bin); |
1156 |
+ |
return (T) ois.readObject(); |
1157 |
+ |
} catch (Throwable t) { |
1158 |
+ |
threadUnexpectedException(t); |
1159 |
+ |
return null; |
1160 |
+ |
} |
1161 |
+ |
} |
1162 |
|
} |