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; |
260 |
|
return 50; |
261 |
|
} |
262 |
|
|
255 |
– |
|
263 |
|
/** |
264 |
|
* Sets delays as multiples of SHORT_DELAY. |
265 |
|
*/ |
267 |
|
SHORT_DELAY_MS = getShortDelay(); |
268 |
|
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
269 |
|
MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10; |
270 |
< |
LONG_DELAY_MS = SHORT_DELAY_MS * 50; |
270 |
> |
LONG_DELAY_MS = SHORT_DELAY_MS * 200; |
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 |
|
/** |
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.get(); |
319 |
> |
Throwable t = threadFailure.getAndSet(null); |
320 |
|
if (t != null) { |
321 |
|
if (t instanceof Error) |
322 |
|
throw (Error) t; |
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 |
|
} |
472 |
|
|
473 |
|
/** |
474 |
+ |
* Delays, via Thread.sleep, for the given millisecond delay, but |
475 |
+ |
* if the sleep is shorter than specified, may re-sleep or yield |
476 |
+ |
* until time elapses. |
477 |
+ |
*/ |
478 |
+ |
static void delay(long millis) throws InterruptedException { |
479 |
+ |
long startTime = System.nanoTime(); |
480 |
+ |
long ns = millis * 1000 * 1000; |
481 |
+ |
for (;;) { |
482 |
+ |
if (millis > 0L) |
483 |
+ |
Thread.sleep(millis); |
484 |
+ |
else // too short to sleep |
485 |
+ |
Thread.yield(); |
486 |
+ |
long d = ns - (System.nanoTime() - startTime); |
487 |
+ |
if (d > 0L) |
488 |
+ |
millis = d / (1000 * 1000); |
489 |
+ |
else |
490 |
+ |
break; |
491 |
+ |
} |
492 |
+ |
} |
493 |
+ |
|
494 |
+ |
/** |
495 |
|
* Waits out termination of a thread pool or fails doing so. |
496 |
|
*/ |
497 |
< |
public void joinPool(ExecutorService exec) { |
497 |
> |
void joinPool(ExecutorService exec) { |
498 |
|
try { |
499 |
|
exec.shutdown(); |
500 |
|
assertTrue("ExecutorService did not terminate in a timely manner", |
501 |
< |
exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
501 |
> |
exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)); |
502 |
|
} catch (SecurityException ok) { |
503 |
|
// Allowed in case test doesn't have privs |
504 |
|
} catch (InterruptedException ie) { |
506 |
|
} |
507 |
|
} |
508 |
|
|
509 |
+ |
/** |
510 |
+ |
* Checks that thread does not terminate within the default |
511 |
+ |
* millisecond delay of {@code timeoutMillis()}. |
512 |
+ |
*/ |
513 |
+ |
void assertThreadStaysAlive(Thread thread) { |
514 |
+ |
assertThreadStaysAlive(thread, timeoutMillis()); |
515 |
+ |
} |
516 |
+ |
|
517 |
+ |
/** |
518 |
+ |
* Checks that thread does not terminate within the given millisecond delay. |
519 |
+ |
*/ |
520 |
+ |
void assertThreadStaysAlive(Thread thread, long millis) { |
521 |
+ |
try { |
522 |
+ |
// No need to optimize the failing case via Thread.join. |
523 |
+ |
delay(millis); |
524 |
+ |
assertTrue(thread.isAlive()); |
525 |
+ |
} catch (InterruptedException ie) { |
526 |
+ |
fail("Unexpected InterruptedException"); |
527 |
+ |
} |
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". |
680 |
|
*/ |
681 |
|
void sleep(long millis) { |
682 |
|
try { |
683 |
< |
Thread.sleep(millis); |
683 |
> |
delay(millis); |
684 |
|
} catch (InterruptedException ie) { |
685 |
|
AssertionFailedError afe = |
686 |
|
new AssertionFailedError("Unexpected InterruptedException"); |
690 |
|
} |
691 |
|
|
692 |
|
/** |
599 |
– |
* Sleeps until the timeout has elapsed, or interrupted. |
600 |
– |
* Does <em>NOT</em> throw InterruptedException. |
601 |
– |
*/ |
602 |
– |
void sleepTillInterrupted(long timeoutMillis) { |
603 |
– |
try { |
604 |
– |
Thread.sleep(timeoutMillis); |
605 |
– |
} catch (InterruptedException wakeup) {} |
606 |
– |
} |
607 |
– |
|
608 |
– |
/** |
693 |
|
* Waits up to the specified number of milliseconds for the given |
694 |
|
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
695 |
|
*/ |
700 |
|
Thread.State s = thread.getState(); |
701 |
|
if (s == Thread.State.BLOCKED || |
702 |
|
s == Thread.State.WAITING || |
703 |
< |
s == Thread.State.TIMED_WAITING || |
704 |
< |
System.nanoTime() - t0 > timeoutNanos) |
703 |
> |
s == Thread.State.TIMED_WAITING) |
704 |
> |
return; |
705 |
> |
else if (s == Thread.State.TERMINATED) |
706 |
> |
fail("Unexpected thread termination"); |
707 |
> |
else if (System.nanoTime() - t0 > timeoutNanos) { |
708 |
> |
threadAssertTrue(thread.isAlive()); |
709 |
|
return; |
710 |
+ |
} |
711 |
|
Thread.yield(); |
712 |
|
} |
713 |
|
} |
714 |
|
|
715 |
|
/** |
716 |
+ |
* Waits up to LONG_DELAY_MS for the given thread to enter a wait |
717 |
+ |
* state: BLOCKED, WAITING, or TIMED_WAITING. |
718 |
+ |
*/ |
719 |
+ |
void waitForThreadToEnterWaitState(Thread thread) { |
720 |
+ |
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS); |
721 |
+ |
} |
722 |
+ |
|
723 |
+ |
/** |
724 |
|
* Returns the number of milliseconds since time given by |
725 |
|
* startNanoTime, which must have been previously returned from a |
726 |
|
* call to {@link System.nanoTime()}. |
728 |
|
long millisElapsedSince(long startNanoTime) { |
729 |
|
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
730 |
|
} |
731 |
< |
|
731 |
> |
|
732 |
|
/** |
733 |
|
* Returns a new started daemon Thread running the given runnable. |
734 |
|
*/ |
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 |
|
} |
757 |
|
} |
758 |
|
} |
759 |
|
|
760 |
+ |
/** |
761 |
+ |
* Waits for LONG_DELAY_MS milliseconds for the thread to |
762 |
+ |
* terminate (using {@link Thread#join(long)}), else interrupts |
763 |
+ |
* the thread (in the hope that it may terminate later) and fails. |
764 |
+ |
*/ |
765 |
+ |
void awaitTermination(Thread t) { |
766 |
+ |
awaitTermination(t, LONG_DELAY_MS); |
767 |
+ |
} |
768 |
+ |
|
769 |
|
// Some convenient Runnable classes |
770 |
|
|
771 |
|
public abstract class CheckedRunnable implements Runnable { |
828 |
|
realRun(); |
829 |
|
threadShouldThrow("InterruptedException"); |
830 |
|
} catch (InterruptedException success) { |
831 |
+ |
threadAssertFalse(Thread.interrupted()); |
832 |
|
} catch (Throwable t) { |
833 |
|
threadUnexpectedException(t); |
834 |
|
} |
858 |
|
threadShouldThrow("InterruptedException"); |
859 |
|
return result; |
860 |
|
} catch (InterruptedException success) { |
861 |
+ |
threadAssertFalse(Thread.interrupted()); |
862 |
|
} catch (Throwable t) { |
863 |
|
threadUnexpectedException(t); |
864 |
|
} |
890 |
|
}}; |
891 |
|
} |
892 |
|
|
893 |
+ |
public Runnable awaiter(final CountDownLatch latch) { |
894 |
+ |
return new CheckedRunnable() { |
895 |
+ |
public void realRun() throws InterruptedException { |
896 |
+ |
await(latch); |
897 |
+ |
}}; |
898 |
+ |
} |
899 |
+ |
|
900 |
+ |
public void await(CountDownLatch latch) { |
901 |
+ |
try { |
902 |
+ |
assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); |
903 |
+ |
} catch (Throwable t) { |
904 |
+ |
threadUnexpectedException(t); |
905 |
+ |
} |
906 |
+ |
} |
907 |
+ |
|
908 |
+ |
// /** |
909 |
+ |
// * Spin-waits up to LONG_DELAY_MS until flag becomes true. |
910 |
+ |
// */ |
911 |
+ |
// public void await(AtomicBoolean flag) { |
912 |
+ |
// await(flag, LONG_DELAY_MS); |
913 |
+ |
// } |
914 |
+ |
|
915 |
+ |
// /** |
916 |
+ |
// * Spin-waits up to the specified timeout until flag becomes true. |
917 |
+ |
// */ |
918 |
+ |
// public void await(AtomicBoolean flag, long timeoutMillis) { |
919 |
+ |
// long startTime = System.nanoTime(); |
920 |
+ |
// while (!flag.get()) { |
921 |
+ |
// if (millisElapsedSince(startTime) > timeoutMillis) |
922 |
+ |
// throw new AssertionFailedError("timed out"); |
923 |
+ |
// Thread.yield(); |
924 |
+ |
// } |
925 |
+ |
// } |
926 |
+ |
|
927 |
|
public static class NPETask implements Callable<String> { |
928 |
|
public String call() { throw new NullPointerException(); } |
929 |
|
} |
934 |
|
|
935 |
|
public class ShortRunnable extends CheckedRunnable { |
936 |
|
protected void realRun() throws Throwable { |
937 |
< |
Thread.sleep(SHORT_DELAY_MS); |
937 |
> |
delay(SHORT_DELAY_MS); |
938 |
|
} |
939 |
|
} |
940 |
|
|
941 |
|
public class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
942 |
|
protected void realRun() throws InterruptedException { |
943 |
< |
Thread.sleep(SHORT_DELAY_MS); |
943 |
> |
delay(SHORT_DELAY_MS); |
944 |
|
} |
945 |
|
} |
946 |
|
|
947 |
|
public class SmallRunnable extends CheckedRunnable { |
948 |
|
protected void realRun() throws Throwable { |
949 |
< |
Thread.sleep(SMALL_DELAY_MS); |
949 |
> |
delay(SMALL_DELAY_MS); |
950 |
|
} |
951 |
|
} |
952 |
|
|
953 |
|
public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
954 |
|
protected void realRun() { |
955 |
|
try { |
956 |
< |
Thread.sleep(SMALL_DELAY_MS); |
956 |
> |
delay(SMALL_DELAY_MS); |
957 |
|
} catch (InterruptedException ok) {} |
958 |
|
} |
959 |
|
} |
960 |
|
|
961 |
|
public class SmallCallable extends CheckedCallable { |
962 |
|
protected Object realCall() throws InterruptedException { |
963 |
< |
Thread.sleep(SMALL_DELAY_MS); |
963 |
> |
delay(SMALL_DELAY_MS); |
964 |
|
return Boolean.TRUE; |
965 |
|
} |
966 |
|
} |
967 |
|
|
968 |
|
public class MediumRunnable extends CheckedRunnable { |
969 |
|
protected void realRun() throws Throwable { |
970 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
970 |
> |
delay(MEDIUM_DELAY_MS); |
971 |
|
} |
972 |
|
} |
973 |
|
|
974 |
|
public class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
975 |
|
protected void realRun() throws InterruptedException { |
976 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
976 |
> |
delay(MEDIUM_DELAY_MS); |
977 |
|
} |
978 |
|
} |
979 |
|
|
981 |
|
return new CheckedRunnable() { |
982 |
|
protected void realRun() { |
983 |
|
try { |
984 |
< |
Thread.sleep(timeoutMillis); |
984 |
> |
delay(timeoutMillis); |
985 |
|
} catch (InterruptedException ok) {} |
986 |
|
}}; |
987 |
|
} |
989 |
|
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
990 |
|
protected void realRun() { |
991 |
|
try { |
992 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
992 |
> |
delay(MEDIUM_DELAY_MS); |
993 |
|
} catch (InterruptedException ok) {} |
994 |
|
} |
995 |
|
} |
997 |
|
public class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
998 |
|
protected void realRun() { |
999 |
|
try { |
1000 |
< |
Thread.sleep(LONG_DELAY_MS); |
1000 |
> |
delay(LONG_DELAY_MS); |
1001 |
|
} catch (InterruptedException ok) {} |
1002 |
|
} |
1003 |
|
} |
1021 |
|
public boolean isDone() { return done; } |
1022 |
|
public void run() { |
1023 |
|
try { |
1024 |
< |
Thread.sleep(timeoutMillis); |
1024 |
> |
delay(timeoutMillis); |
1025 |
|
done = true; |
1026 |
|
} catch (InterruptedException ok) {} |
1027 |
|
} |
1032 |
|
public volatile boolean done = false; |
1033 |
|
public void run() { |
1034 |
|
try { |
1035 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1035 |
> |
delay(SHORT_DELAY_MS); |
1036 |
|
done = true; |
1037 |
|
} catch (InterruptedException ok) {} |
1038 |
|
} |
1042 |
|
public volatile boolean done = false; |
1043 |
|
public void run() { |
1044 |
|
try { |
1045 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1045 |
> |
delay(SMALL_DELAY_MS); |
1046 |
|
done = true; |
1047 |
|
} catch (InterruptedException ok) {} |
1048 |
|
} |
1052 |
|
public volatile boolean done = false; |
1053 |
|
public void run() { |
1054 |
|
try { |
1055 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1055 |
> |
delay(MEDIUM_DELAY_MS); |
1056 |
|
done = true; |
1057 |
|
} catch (InterruptedException ok) {} |
1058 |
|
} |
1062 |
|
public volatile boolean done = false; |
1063 |
|
public void run() { |
1064 |
|
try { |
1065 |
< |
Thread.sleep(LONG_DELAY_MS); |
1065 |
> |
delay(LONG_DELAY_MS); |
1066 |
|
done = true; |
1067 |
|
} catch (InterruptedException ok) {} |
1068 |
|
} |
1079 |
|
public volatile boolean done = false; |
1080 |
|
public Object call() { |
1081 |
|
try { |
1082 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1082 |
> |
delay(SMALL_DELAY_MS); |
1083 |
|
done = true; |
1084 |
|
} catch (InterruptedException ok) {} |
1085 |
|
return Boolean.TRUE; |
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); |
1146 |
|
} |
1147 |
|
} |
1148 |
|
|
1149 |
+ |
void checkEmpty(BlockingQueue q) { |
1150 |
+ |
try { |
1151 |
+ |
assertTrue(q.isEmpty()); |
1152 |
+ |
assertEquals(0, q.size()); |
1153 |
+ |
assertNull(q.peek()); |
1154 |
+ |
assertNull(q.poll()); |
1155 |
+ |
assertNull(q.poll(0, MILLISECONDS)); |
1156 |
+ |
assertEquals(q.toString(), "[]"); |
1157 |
+ |
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
1158 |
+ |
assertFalse(q.iterator().hasNext()); |
1159 |
+ |
try { |
1160 |
+ |
q.element(); |
1161 |
+ |
shouldThrow(); |
1162 |
+ |
} catch (NoSuchElementException success) {} |
1163 |
+ |
try { |
1164 |
+ |
q.iterator().next(); |
1165 |
+ |
shouldThrow(); |
1166 |
+ |
} catch (NoSuchElementException success) {} |
1167 |
+ |
try { |
1168 |
+ |
q.remove(); |
1169 |
+ |
shouldThrow(); |
1170 |
+ |
} catch (NoSuchElementException success) {} |
1171 |
+ |
} catch (InterruptedException ie) { |
1172 |
+ |
threadUnexpectedException(ie); |
1173 |
+ |
} |
1174 |
+ |
} |
1175 |
+ |
|
1176 |
+ |
@SuppressWarnings("unchecked") |
1177 |
+ |
<T> T serialClone(T o) { |
1178 |
+ |
try { |
1179 |
+ |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1180 |
+ |
ObjectOutputStream oos = new ObjectOutputStream(bos); |
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(); |
1188 |
+ |
} catch (Throwable t) { |
1189 |
+ |
threadUnexpectedException(t); |
1190 |
+ |
return null; |
1191 |
+ |
} |
1192 |
+ |
} |
1193 |
|
} |