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.util.Arrays; |
11 |
+ |
import java.util.NoSuchElementException; |
12 |
|
import java.util.PropertyPermission; |
13 |
|
import java.util.concurrent.*; |
14 |
|
import java.util.concurrent.atomic.AtomicReference; |
15 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
16 |
+ |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
17 |
|
import java.security.CodeSource; |
18 |
|
import java.security.Permission; |
19 |
|
import java.security.PermissionCollection; |
99 |
|
private static final boolean useSecurityManager = |
100 |
|
Boolean.getBoolean("jsr166.useSecurityManager"); |
101 |
|
|
102 |
+ |
protected static final boolean expensiveTests = |
103 |
+ |
Boolean.getBoolean("jsr166.expensiveTests"); |
104 |
+ |
|
105 |
+ |
/** |
106 |
+ |
* If true, report on stdout all "slow" tests, that is, ones that |
107 |
+ |
* take more than profileThreshold milliseconds to execute. |
108 |
+ |
*/ |
109 |
+ |
private static final boolean profileTests = |
110 |
+ |
Boolean.getBoolean("jsr166.profileTests"); |
111 |
+ |
|
112 |
+ |
/** |
113 |
+ |
* The number of milliseconds that tests are permitted for |
114 |
+ |
* execution without being reported, when profileTests is set. |
115 |
+ |
*/ |
116 |
+ |
private static final long profileThreshold = |
117 |
+ |
Long.getLong("jsr166.profileThreshold", 100); |
118 |
+ |
|
119 |
+ |
protected void runTest() throws Throwable { |
120 |
+ |
if (profileTests) |
121 |
+ |
runTestProfiled(); |
122 |
+ |
else |
123 |
+ |
super.runTest(); |
124 |
+ |
} |
125 |
+ |
|
126 |
+ |
protected void runTestProfiled() throws Throwable { |
127 |
+ |
long t0 = System.nanoTime(); |
128 |
+ |
try { |
129 |
+ |
super.runTest(); |
130 |
+ |
} finally { |
131 |
+ |
long elapsedMillis = |
132 |
+ |
(System.nanoTime() - t0) / (1000L * 1000L); |
133 |
+ |
if (elapsedMillis >= profileThreshold) |
134 |
+ |
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); |
135 |
+ |
} |
136 |
+ |
} |
137 |
+ |
|
138 |
|
/** |
139 |
|
* Runs all JSR166 unit tests using junit.textui.TestRunner |
140 |
|
*/ |
262 |
|
SHORT_DELAY_MS = getShortDelay(); |
263 |
|
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
264 |
|
MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10; |
265 |
< |
LONG_DELAY_MS = SHORT_DELAY_MS * 50; |
265 |
> |
LONG_DELAY_MS = SHORT_DELAY_MS * 200; |
266 |
|
} |
267 |
|
|
268 |
|
/** |
291 |
|
* earlier by threadRecordFailure. |
292 |
|
*/ |
293 |
|
public void tearDown() throws Exception { |
294 |
< |
Throwable t = threadFailure.get(); |
294 |
> |
Throwable t = threadFailure.getAndSet(null); |
295 |
|
if (t != null) { |
296 |
|
if (t instanceof Error) |
297 |
|
throw (Error) t; |
443 |
|
} |
444 |
|
|
445 |
|
/** |
446 |
+ |
* Delays, via Thread.sleep for the given millisecond delay, but |
447 |
+ |
* if the sleep is shorter than specified, may re-sleep or yield |
448 |
+ |
* until time elapses. |
449 |
+ |
*/ |
450 |
+ |
public static void delay(long ms) throws InterruptedException { |
451 |
+ |
long startTime = System.nanoTime(); |
452 |
+ |
long ns = ms * 1000 * 1000; |
453 |
+ |
for (;;) { |
454 |
+ |
if (ms > 0L) |
455 |
+ |
Thread.sleep(ms); |
456 |
+ |
else // too short to sleep |
457 |
+ |
Thread.yield(); |
458 |
+ |
long d = ns - (System.nanoTime() - startTime); |
459 |
+ |
if (d > 0L) |
460 |
+ |
ms = d / (1000 * 1000); |
461 |
+ |
else |
462 |
+ |
break; |
463 |
+ |
} |
464 |
+ |
} |
465 |
+ |
|
466 |
+ |
/** |
467 |
|
* Waits out termination of a thread pool or fails doing so. |
468 |
|
*/ |
469 |
|
public void joinPool(ExecutorService exec) { |
470 |
|
try { |
471 |
|
exec.shutdown(); |
472 |
|
assertTrue("ExecutorService did not terminate in a timely manner", |
473 |
< |
exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
473 |
> |
exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)); |
474 |
|
} catch (SecurityException ok) { |
475 |
|
// Allowed in case test doesn't have privs |
476 |
|
} catch (InterruptedException ie) { |
478 |
|
} |
479 |
|
} |
480 |
|
|
481 |
+ |
/** |
482 |
+ |
* Checks that thread does not terminate within timeoutMillis |
483 |
+ |
* milliseconds (that is, Thread.join times out). |
484 |
+ |
*/ |
485 |
+ |
public void assertThreadJoinTimesOut(Thread thread, long timeoutMillis) { |
486 |
+ |
try { |
487 |
+ |
long startTime = System.nanoTime(); |
488 |
+ |
thread.join(timeoutMillis); |
489 |
+ |
assertTrue(thread.isAlive()); |
490 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
491 |
+ |
} catch (InterruptedException ie) { |
492 |
+ |
fail("Unexpected InterruptedException"); |
493 |
+ |
} |
494 |
+ |
} |
495 |
|
|
496 |
|
/** |
497 |
|
* Fails with message "should throw exception". |
623 |
|
*/ |
624 |
|
void sleep(long millis) { |
625 |
|
try { |
626 |
< |
Thread.sleep(millis); |
626 |
> |
delay(millis); |
627 |
|
} catch (InterruptedException ie) { |
628 |
|
AssertionFailedError afe = |
629 |
|
new AssertionFailedError("Unexpected InterruptedException"); |
643 |
|
} |
644 |
|
|
645 |
|
/** |
646 |
+ |
* Waits up to the specified number of milliseconds for the given |
647 |
+ |
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
648 |
+ |
*/ |
649 |
+ |
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
650 |
+ |
long timeoutNanos = timeoutMillis * 1000L * 1000L; |
651 |
+ |
long t0 = System.nanoTime(); |
652 |
+ |
for (;;) { |
653 |
+ |
Thread.State s = thread.getState(); |
654 |
+ |
if (s == Thread.State.BLOCKED || |
655 |
+ |
s == Thread.State.WAITING || |
656 |
+ |
s == Thread.State.TIMED_WAITING) |
657 |
+ |
return; |
658 |
+ |
else if (s == Thread.State.TERMINATED) |
659 |
+ |
fail("Unexpected thread termination"); |
660 |
+ |
else if (System.nanoTime() - t0 > timeoutNanos) { |
661 |
+ |
threadAssertTrue(thread.isAlive()); |
662 |
+ |
return; |
663 |
+ |
} |
664 |
+ |
Thread.yield(); |
665 |
+ |
} |
666 |
+ |
} |
667 |
+ |
|
668 |
+ |
/** |
669 |
+ |
* Waits up to LONG_DELAY_MS for the given thread to enter a wait |
670 |
+ |
* state: BLOCKED, WAITING, or TIMED_WAITING. |
671 |
+ |
*/ |
672 |
+ |
void waitForThreadToEnterWaitState(Thread thread) { |
673 |
+ |
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS); |
674 |
+ |
} |
675 |
+ |
|
676 |
+ |
/** |
677 |
+ |
* Returns the number of milliseconds since time given by |
678 |
+ |
* startNanoTime, which must have been previously returned from a |
679 |
+ |
* call to {@link System.nanoTime()}. |
680 |
+ |
*/ |
681 |
+ |
long millisElapsedSince(long startNanoTime) { |
682 |
+ |
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
683 |
+ |
} |
684 |
+ |
|
685 |
+ |
/** |
686 |
|
* Returns a new started daemon Thread running the given runnable. |
687 |
|
*/ |
688 |
|
Thread newStartedThread(Runnable runnable) { |
710 |
|
} |
711 |
|
} |
712 |
|
|
713 |
+ |
/** |
714 |
+ |
* Waits for LONG_DELAY_MS milliseconds for the thread to |
715 |
+ |
* terminate (using {@link Thread#join(long)}), else interrupts |
716 |
+ |
* the thread (in the hope that it may terminate later) and fails. |
717 |
+ |
*/ |
718 |
+ |
void awaitTermination(Thread t) { |
719 |
+ |
awaitTermination(t, LONG_DELAY_MS); |
720 |
+ |
} |
721 |
+ |
|
722 |
|
// Some convenient Runnable classes |
723 |
|
|
724 |
|
public abstract class CheckedRunnable implements Runnable { |
833 |
|
|
834 |
|
public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) { |
835 |
|
return new CheckedCallable<String>() { |
836 |
< |
public String realCall() { |
836 |
> |
protected String realCall() { |
837 |
|
try { |
838 |
|
latch.await(); |
839 |
|
} catch (InterruptedException quittingTime) {} |
841 |
|
}}; |
842 |
|
} |
843 |
|
|
844 |
+ |
public Runnable awaiter(final CountDownLatch latch) { |
845 |
+ |
return new CheckedRunnable() { |
846 |
+ |
public void realRun() throws InterruptedException { |
847 |
+ |
latch.await(); |
848 |
+ |
}}; |
849 |
+ |
} |
850 |
+ |
|
851 |
|
public static class NPETask implements Callable<String> { |
852 |
|
public String call() { throw new NullPointerException(); } |
853 |
|
} |
858 |
|
|
859 |
|
public class ShortRunnable extends CheckedRunnable { |
860 |
|
protected void realRun() throws Throwable { |
861 |
< |
Thread.sleep(SHORT_DELAY_MS); |
861 |
> |
delay(SHORT_DELAY_MS); |
862 |
|
} |
863 |
|
} |
864 |
|
|
865 |
|
public class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
866 |
|
protected void realRun() throws InterruptedException { |
867 |
< |
Thread.sleep(SHORT_DELAY_MS); |
867 |
> |
delay(SHORT_DELAY_MS); |
868 |
|
} |
869 |
|
} |
870 |
|
|
871 |
|
public class SmallRunnable extends CheckedRunnable { |
872 |
|
protected void realRun() throws Throwable { |
873 |
< |
Thread.sleep(SMALL_DELAY_MS); |
873 |
> |
delay(SMALL_DELAY_MS); |
874 |
|
} |
875 |
|
} |
876 |
|
|
877 |
|
public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
878 |
|
protected void realRun() { |
879 |
|
try { |
880 |
< |
Thread.sleep(SMALL_DELAY_MS); |
880 |
> |
delay(SMALL_DELAY_MS); |
881 |
|
} catch (InterruptedException ok) {} |
882 |
|
} |
883 |
|
} |
884 |
|
|
885 |
|
public class SmallCallable extends CheckedCallable { |
886 |
|
protected Object realCall() throws InterruptedException { |
887 |
< |
Thread.sleep(SMALL_DELAY_MS); |
887 |
> |
delay(SMALL_DELAY_MS); |
888 |
|
return Boolean.TRUE; |
889 |
|
} |
890 |
|
} |
891 |
|
|
892 |
|
public class MediumRunnable extends CheckedRunnable { |
893 |
|
protected void realRun() throws Throwable { |
894 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
894 |
> |
delay(MEDIUM_DELAY_MS); |
895 |
|
} |
896 |
|
} |
897 |
|
|
898 |
|
public class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
899 |
|
protected void realRun() throws InterruptedException { |
900 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
900 |
> |
delay(MEDIUM_DELAY_MS); |
901 |
|
} |
902 |
|
} |
903 |
|
|
904 |
+ |
public Runnable possiblyInterruptedRunnable(final long timeoutMillis) { |
905 |
+ |
return new CheckedRunnable() { |
906 |
+ |
protected void realRun() { |
907 |
+ |
try { |
908 |
+ |
delay(timeoutMillis); |
909 |
+ |
} catch (InterruptedException ok) {} |
910 |
+ |
}}; |
911 |
+ |
} |
912 |
+ |
|
913 |
|
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
914 |
|
protected void realRun() { |
915 |
|
try { |
916 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
916 |
> |
delay(MEDIUM_DELAY_MS); |
917 |
|
} catch (InterruptedException ok) {} |
918 |
|
} |
919 |
|
} |
921 |
|
public class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
922 |
|
protected void realRun() { |
923 |
|
try { |
924 |
< |
Thread.sleep(LONG_DELAY_MS); |
924 |
> |
delay(LONG_DELAY_MS); |
925 |
|
} catch (InterruptedException ok) {} |
926 |
|
} |
927 |
|
} |
935 |
|
} |
936 |
|
} |
937 |
|
|
938 |
+ |
public interface TrackedRunnable extends Runnable { |
939 |
+ |
boolean isDone(); |
940 |
+ |
} |
941 |
+ |
|
942 |
+ |
public static TrackedRunnable trackedRunnable(final long timeoutMillis) { |
943 |
+ |
return new TrackedRunnable() { |
944 |
+ |
private volatile boolean done = false; |
945 |
+ |
public boolean isDone() { return done; } |
946 |
+ |
public void run() { |
947 |
+ |
try { |
948 |
+ |
delay(timeoutMillis); |
949 |
+ |
done = true; |
950 |
+ |
} catch (InterruptedException ok) {} |
951 |
+ |
} |
952 |
+ |
}; |
953 |
+ |
} |
954 |
+ |
|
955 |
|
public static class TrackedShortRunnable implements Runnable { |
956 |
|
public volatile boolean done = false; |
957 |
|
public void run() { |
958 |
|
try { |
959 |
< |
Thread.sleep(SMALL_DELAY_MS); |
959 |
> |
delay(SHORT_DELAY_MS); |
960 |
> |
done = true; |
961 |
> |
} catch (InterruptedException ok) {} |
962 |
> |
} |
963 |
> |
} |
964 |
> |
|
965 |
> |
public static class TrackedSmallRunnable implements Runnable { |
966 |
> |
public volatile boolean done = false; |
967 |
> |
public void run() { |
968 |
> |
try { |
969 |
> |
delay(SMALL_DELAY_MS); |
970 |
|
done = true; |
971 |
|
} catch (InterruptedException ok) {} |
972 |
|
} |
976 |
|
public volatile boolean done = false; |
977 |
|
public void run() { |
978 |
|
try { |
979 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
979 |
> |
delay(MEDIUM_DELAY_MS); |
980 |
|
done = true; |
981 |
|
} catch (InterruptedException ok) {} |
982 |
|
} |
986 |
|
public volatile boolean done = false; |
987 |
|
public void run() { |
988 |
|
try { |
989 |
< |
Thread.sleep(LONG_DELAY_MS); |
989 |
> |
delay(LONG_DELAY_MS); |
990 |
|
done = true; |
991 |
|
} catch (InterruptedException ok) {} |
992 |
|
} |
1003 |
|
public volatile boolean done = false; |
1004 |
|
public Object call() { |
1005 |
|
try { |
1006 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1006 |
> |
delay(SMALL_DELAY_MS); |
1007 |
|
done = true; |
1008 |
|
} catch (InterruptedException ok) {} |
1009 |
|
return Boolean.TRUE; |
1068 |
|
} |
1069 |
|
} |
1070 |
|
|
1071 |
+ |
public void checkEmpty(BlockingQueue q) { |
1072 |
+ |
try { |
1073 |
+ |
assertTrue(q.isEmpty()); |
1074 |
+ |
assertEquals(0, q.size()); |
1075 |
+ |
assertNull(q.peek()); |
1076 |
+ |
assertNull(q.poll()); |
1077 |
+ |
assertNull(q.poll(0, MILLISECONDS)); |
1078 |
+ |
assertEquals(q.toString(), "[]"); |
1079 |
+ |
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
1080 |
+ |
assertFalse(q.iterator().hasNext()); |
1081 |
+ |
try { |
1082 |
+ |
q.element(); |
1083 |
+ |
shouldThrow(); |
1084 |
+ |
} catch (NoSuchElementException success) {} |
1085 |
+ |
try { |
1086 |
+ |
q.iterator().next(); |
1087 |
+ |
shouldThrow(); |
1088 |
+ |
} catch (NoSuchElementException success) {} |
1089 |
+ |
try { |
1090 |
+ |
q.remove(); |
1091 |
+ |
shouldThrow(); |
1092 |
+ |
} catch (NoSuchElementException success) {} |
1093 |
+ |
} catch (InterruptedException ie) { |
1094 |
+ |
threadUnexpectedException(ie); |
1095 |
+ |
} |
1096 |
+ |
} |
1097 |
+ |
|
1098 |
|
} |