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 |
|
*/ |
155 |
|
System.exit(0); |
156 |
|
} |
157 |
|
|
158 |
+ |
public static TestSuite newTestSuite(Object... suiteOrClasses) { |
159 |
+ |
TestSuite suite = new TestSuite(); |
160 |
+ |
for (Object suiteOrClass : suiteOrClasses) { |
161 |
+ |
if (suiteOrClass instanceof TestSuite) |
162 |
+ |
suite.addTest((TestSuite) suiteOrClass); |
163 |
+ |
else if (suiteOrClass instanceof Class) |
164 |
+ |
suite.addTest(new TestSuite((Class<?>) suiteOrClass)); |
165 |
+ |
else |
166 |
+ |
throw new ClassCastException("not a test suite or class"); |
167 |
+ |
} |
168 |
+ |
return suite; |
169 |
+ |
} |
170 |
+ |
|
171 |
|
/** |
172 |
< |
* Collects all JSR166 unit tests as one suite |
172 |
> |
* Collects all JSR166 unit tests as one suite. |
173 |
|
*/ |
174 |
|
public static Test suite() { |
175 |
< |
TestSuite suite = new TestSuite("JSR166 Unit Tests"); |
176 |
< |
|
177 |
< |
suite.addTest(ForkJoinPoolTest.suite()); |
178 |
< |
suite.addTest(ForkJoinTaskTest.suite()); |
179 |
< |
suite.addTest(RecursiveActionTest.suite()); |
180 |
< |
suite.addTest(RecursiveTaskTest.suite()); |
181 |
< |
suite.addTest(LinkedTransferQueueTest.suite()); |
182 |
< |
suite.addTest(PhaserTest.suite()); |
183 |
< |
suite.addTest(ThreadLocalRandomTest.suite()); |
184 |
< |
suite.addTest(AbstractExecutorServiceTest.suite()); |
185 |
< |
suite.addTest(AbstractQueueTest.suite()); |
186 |
< |
suite.addTest(AbstractQueuedSynchronizerTest.suite()); |
187 |
< |
suite.addTest(AbstractQueuedLongSynchronizerTest.suite()); |
188 |
< |
suite.addTest(ArrayBlockingQueueTest.suite()); |
189 |
< |
suite.addTest(ArrayDequeTest.suite()); |
190 |
< |
suite.addTest(AtomicBooleanTest.suite()); |
191 |
< |
suite.addTest(AtomicIntegerArrayTest.suite()); |
192 |
< |
suite.addTest(AtomicIntegerFieldUpdaterTest.suite()); |
193 |
< |
suite.addTest(AtomicIntegerTest.suite()); |
194 |
< |
suite.addTest(AtomicLongArrayTest.suite()); |
195 |
< |
suite.addTest(AtomicLongFieldUpdaterTest.suite()); |
196 |
< |
suite.addTest(AtomicLongTest.suite()); |
197 |
< |
suite.addTest(AtomicMarkableReferenceTest.suite()); |
198 |
< |
suite.addTest(AtomicReferenceArrayTest.suite()); |
199 |
< |
suite.addTest(AtomicReferenceFieldUpdaterTest.suite()); |
200 |
< |
suite.addTest(AtomicReferenceTest.suite()); |
201 |
< |
suite.addTest(AtomicStampedReferenceTest.suite()); |
202 |
< |
suite.addTest(ConcurrentHashMapTest.suite()); |
203 |
< |
suite.addTest(ConcurrentLinkedDequeTest.suite()); |
204 |
< |
suite.addTest(ConcurrentLinkedQueueTest.suite()); |
205 |
< |
suite.addTest(ConcurrentSkipListMapTest.suite()); |
206 |
< |
suite.addTest(ConcurrentSkipListSubMapTest.suite()); |
207 |
< |
suite.addTest(ConcurrentSkipListSetTest.suite()); |
208 |
< |
suite.addTest(ConcurrentSkipListSubSetTest.suite()); |
209 |
< |
suite.addTest(CopyOnWriteArrayListTest.suite()); |
210 |
< |
suite.addTest(CopyOnWriteArraySetTest.suite()); |
211 |
< |
suite.addTest(CountDownLatchTest.suite()); |
212 |
< |
suite.addTest(CyclicBarrierTest.suite()); |
213 |
< |
suite.addTest(DelayQueueTest.suite()); |
214 |
< |
suite.addTest(EntryTest.suite()); |
215 |
< |
suite.addTest(ExchangerTest.suite()); |
216 |
< |
suite.addTest(ExecutorsTest.suite()); |
217 |
< |
suite.addTest(ExecutorCompletionServiceTest.suite()); |
218 |
< |
suite.addTest(FutureTaskTest.suite()); |
219 |
< |
suite.addTest(LinkedBlockingDequeTest.suite()); |
220 |
< |
suite.addTest(LinkedBlockingQueueTest.suite()); |
221 |
< |
suite.addTest(LinkedListTest.suite()); |
222 |
< |
suite.addTest(LockSupportTest.suite()); |
223 |
< |
suite.addTest(PriorityBlockingQueueTest.suite()); |
224 |
< |
suite.addTest(PriorityQueueTest.suite()); |
225 |
< |
suite.addTest(ReentrantLockTest.suite()); |
226 |
< |
suite.addTest(ReentrantReadWriteLockTest.suite()); |
227 |
< |
suite.addTest(ScheduledExecutorTest.suite()); |
228 |
< |
suite.addTest(ScheduledExecutorSubclassTest.suite()); |
229 |
< |
suite.addTest(SemaphoreTest.suite()); |
230 |
< |
suite.addTest(SynchronousQueueTest.suite()); |
231 |
< |
suite.addTest(SystemTest.suite()); |
232 |
< |
suite.addTest(ThreadLocalTest.suite()); |
233 |
< |
suite.addTest(ThreadPoolExecutorTest.suite()); |
234 |
< |
suite.addTest(ThreadPoolExecutorSubclassTest.suite()); |
235 |
< |
suite.addTest(ThreadTest.suite()); |
236 |
< |
suite.addTest(TimeUnitTest.suite()); |
237 |
< |
suite.addTest(TreeMapTest.suite()); |
238 |
< |
suite.addTest(TreeSetTest.suite()); |
239 |
< |
suite.addTest(TreeSubMapTest.suite()); |
188 |
< |
suite.addTest(TreeSubSetTest.suite()); |
189 |
< |
|
190 |
< |
return suite; |
175 |
> |
return newTestSuite( |
176 |
> |
ForkJoinPoolTest.suite(), |
177 |
> |
ForkJoinTaskTest.suite(), |
178 |
> |
RecursiveActionTest.suite(), |
179 |
> |
RecursiveTaskTest.suite(), |
180 |
> |
LinkedTransferQueueTest.suite(), |
181 |
> |
PhaserTest.suite(), |
182 |
> |
ThreadLocalRandomTest.suite(), |
183 |
> |
AbstractExecutorServiceTest.suite(), |
184 |
> |
AbstractQueueTest.suite(), |
185 |
> |
AbstractQueuedSynchronizerTest.suite(), |
186 |
> |
AbstractQueuedLongSynchronizerTest.suite(), |
187 |
> |
ArrayBlockingQueueTest.suite(), |
188 |
> |
ArrayDequeTest.suite(), |
189 |
> |
AtomicBooleanTest.suite(), |
190 |
> |
AtomicIntegerArrayTest.suite(), |
191 |
> |
AtomicIntegerFieldUpdaterTest.suite(), |
192 |
> |
AtomicIntegerTest.suite(), |
193 |
> |
AtomicLongArrayTest.suite(), |
194 |
> |
AtomicLongFieldUpdaterTest.suite(), |
195 |
> |
AtomicLongTest.suite(), |
196 |
> |
AtomicMarkableReferenceTest.suite(), |
197 |
> |
AtomicReferenceArrayTest.suite(), |
198 |
> |
AtomicReferenceFieldUpdaterTest.suite(), |
199 |
> |
AtomicReferenceTest.suite(), |
200 |
> |
AtomicStampedReferenceTest.suite(), |
201 |
> |
ConcurrentHashMapTest.suite(), |
202 |
> |
ConcurrentLinkedDequeTest.suite(), |
203 |
> |
ConcurrentLinkedQueueTest.suite(), |
204 |
> |
ConcurrentSkipListMapTest.suite(), |
205 |
> |
ConcurrentSkipListSubMapTest.suite(), |
206 |
> |
ConcurrentSkipListSetTest.suite(), |
207 |
> |
ConcurrentSkipListSubSetTest.suite(), |
208 |
> |
CopyOnWriteArrayListTest.suite(), |
209 |
> |
CopyOnWriteArraySetTest.suite(), |
210 |
> |
CountDownLatchTest.suite(), |
211 |
> |
CyclicBarrierTest.suite(), |
212 |
> |
DelayQueueTest.suite(), |
213 |
> |
EntryTest.suite(), |
214 |
> |
ExchangerTest.suite(), |
215 |
> |
ExecutorsTest.suite(), |
216 |
> |
ExecutorCompletionServiceTest.suite(), |
217 |
> |
FutureTaskTest.suite(), |
218 |
> |
LinkedBlockingDequeTest.suite(), |
219 |
> |
LinkedBlockingQueueTest.suite(), |
220 |
> |
LinkedListTest.suite(), |
221 |
> |
LockSupportTest.suite(), |
222 |
> |
PriorityBlockingQueueTest.suite(), |
223 |
> |
PriorityQueueTest.suite(), |
224 |
> |
ReentrantLockTest.suite(), |
225 |
> |
ReentrantReadWriteLockTest.suite(), |
226 |
> |
ScheduledExecutorTest.suite(), |
227 |
> |
ScheduledExecutorSubclassTest.suite(), |
228 |
> |
SemaphoreTest.suite(), |
229 |
> |
SynchronousQueueTest.suite(), |
230 |
> |
SystemTest.suite(), |
231 |
> |
ThreadLocalTest.suite(), |
232 |
> |
ThreadPoolExecutorTest.suite(), |
233 |
> |
ThreadPoolExecutorSubclassTest.suite(), |
234 |
> |
ThreadTest.suite(), |
235 |
> |
TimeUnitTest.suite(), |
236 |
> |
TreeMapTest.suite(), |
237 |
> |
TreeSetTest.suite(), |
238 |
> |
TreeSubMapTest.suite(), |
239 |
> |
TreeSubSetTest.suite()); |
240 |
|
} |
241 |
|
|
242 |
|
|
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; |
449 |
|
try { |
450 |
|
exec.shutdown(); |
451 |
|
assertTrue("ExecutorService did not terminate in a timely manner", |
452 |
< |
exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
452 |
> |
exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)); |
453 |
|
} catch (SecurityException ok) { |
454 |
|
// Allowed in case test doesn't have privs |
455 |
|
} catch (InterruptedException ie) { |
583 |
|
} |
584 |
|
|
585 |
|
/** |
586 |
+ |
* Sleeps until the given time has elapsed. |
587 |
+ |
* Throws AssertionFailedError if interrupted. |
588 |
+ |
*/ |
589 |
+ |
void sleep(long millis) { |
590 |
+ |
try { |
591 |
+ |
Thread.sleep(millis); |
592 |
+ |
} catch (InterruptedException ie) { |
593 |
+ |
AssertionFailedError afe = |
594 |
+ |
new AssertionFailedError("Unexpected InterruptedException"); |
595 |
+ |
afe.initCause(ie); |
596 |
+ |
throw afe; |
597 |
+ |
} |
598 |
+ |
} |
599 |
+ |
|
600 |
+ |
/** |
601 |
|
* Sleeps until the timeout has elapsed, or interrupted. |
602 |
|
* Does <em>NOT</em> throw InterruptedException. |
603 |
|
*/ |
608 |
|
} |
609 |
|
|
610 |
|
/** |
611 |
+ |
* Waits up to the specified number of milliseconds for the given |
612 |
+ |
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
613 |
+ |
*/ |
614 |
+ |
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
615 |
+ |
long timeoutNanos = timeoutMillis * 1000L * 1000L; |
616 |
+ |
long t0 = System.nanoTime(); |
617 |
+ |
for (;;) { |
618 |
+ |
Thread.State s = thread.getState(); |
619 |
+ |
if (s == Thread.State.BLOCKED || |
620 |
+ |
s == Thread.State.WAITING || |
621 |
+ |
s == Thread.State.TIMED_WAITING) |
622 |
+ |
return; |
623 |
+ |
else if (s == Thread.State.TERMINATED) |
624 |
+ |
fail("Unexpected thread termination"); |
625 |
+ |
else if (System.nanoTime() - t0 > timeoutNanos) { |
626 |
+ |
threadAssertTrue(thread.isAlive()); |
627 |
+ |
return; |
628 |
+ |
} |
629 |
+ |
Thread.yield(); |
630 |
+ |
} |
631 |
+ |
} |
632 |
+ |
|
633 |
+ |
/** |
634 |
+ |
* Waits up to LONG_DELAY_MS for the given thread to enter a wait |
635 |
+ |
* state: BLOCKED, WAITING, or TIMED_WAITING. |
636 |
+ |
*/ |
637 |
+ |
void waitForThreadToEnterWaitState(Thread thread) { |
638 |
+ |
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS); |
639 |
+ |
} |
640 |
+ |
|
641 |
+ |
/** |
642 |
+ |
* Returns the number of milliseconds since time given by |
643 |
+ |
* startNanoTime, which must have been previously returned from a |
644 |
+ |
* call to {@link System.nanoTime()}. |
645 |
+ |
*/ |
646 |
+ |
long millisElapsedSince(long startNanoTime) { |
647 |
+ |
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
648 |
+ |
} |
649 |
+ |
|
650 |
+ |
/** |
651 |
|
* Returns a new started daemon Thread running the given runnable. |
652 |
|
*/ |
653 |
|
Thread newStartedThread(Runnable runnable) { |
675 |
|
} |
676 |
|
} |
677 |
|
|
678 |
+ |
/** |
679 |
+ |
* Waits for LONG_DELAY_MS milliseconds for the thread to |
680 |
+ |
* terminate (using {@link Thread#join(long)}), else interrupts |
681 |
+ |
* the thread (in the hope that it may terminate later) and fails. |
682 |
+ |
*/ |
683 |
+ |
void awaitTermination(Thread t) { |
684 |
+ |
awaitTermination(t, LONG_DELAY_MS); |
685 |
+ |
} |
686 |
+ |
|
687 |
|
// Some convenient Runnable classes |
688 |
|
|
689 |
|
public abstract class CheckedRunnable implements Runnable { |
798 |
|
|
799 |
|
public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) { |
800 |
|
return new CheckedCallable<String>() { |
801 |
< |
public String realCall() { |
801 |
> |
protected String realCall() { |
802 |
|
try { |
803 |
|
latch.await(); |
804 |
|
} catch (InterruptedException quittingTime) {} |
806 |
|
}}; |
807 |
|
} |
808 |
|
|
809 |
+ |
public Runnable awaiter(final CountDownLatch latch) { |
810 |
+ |
return new CheckedRunnable() { |
811 |
+ |
public void realRun() throws InterruptedException { |
812 |
+ |
latch.await(); |
813 |
+ |
}}; |
814 |
+ |
} |
815 |
+ |
|
816 |
|
public static class NPETask implements Callable<String> { |
817 |
|
public String call() { throw new NullPointerException(); } |
818 |
|
} |
866 |
|
} |
867 |
|
} |
868 |
|
|
869 |
+ |
public Runnable possiblyInterruptedRunnable(final long timeoutMillis) { |
870 |
+ |
return new CheckedRunnable() { |
871 |
+ |
protected void realRun() { |
872 |
+ |
try { |
873 |
+ |
Thread.sleep(timeoutMillis); |
874 |
+ |
} catch (InterruptedException ok) {} |
875 |
+ |
}}; |
876 |
+ |
} |
877 |
+ |
|
878 |
|
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
879 |
|
protected void realRun() { |
880 |
|
try { |
900 |
|
} |
901 |
|
} |
902 |
|
|
903 |
+ |
public interface TrackedRunnable extends Runnable { |
904 |
+ |
boolean isDone(); |
905 |
+ |
} |
906 |
+ |
|
907 |
+ |
public static TrackedRunnable trackedRunnable(final long timeoutMillis) { |
908 |
+ |
return new TrackedRunnable() { |
909 |
+ |
private volatile boolean done = false; |
910 |
+ |
public boolean isDone() { return done; } |
911 |
+ |
public void run() { |
912 |
+ |
try { |
913 |
+ |
Thread.sleep(timeoutMillis); |
914 |
+ |
done = true; |
915 |
+ |
} catch (InterruptedException ok) {} |
916 |
+ |
} |
917 |
+ |
}; |
918 |
+ |
} |
919 |
+ |
|
920 |
|
public static class TrackedShortRunnable implements Runnable { |
921 |
|
public volatile boolean done = false; |
922 |
|
public void run() { |
923 |
|
try { |
924 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
925 |
+ |
done = true; |
926 |
+ |
} catch (InterruptedException ok) {} |
927 |
+ |
} |
928 |
+ |
} |
929 |
+ |
|
930 |
+ |
public static class TrackedSmallRunnable implements Runnable { |
931 |
+ |
public volatile boolean done = false; |
932 |
+ |
public void run() { |
933 |
+ |
try { |
934 |
|
Thread.sleep(SMALL_DELAY_MS); |
935 |
|
done = true; |
936 |
|
} catch (InterruptedException ok) {} |
1014 |
|
ThreadPoolExecutor executor) {} |
1015 |
|
} |
1016 |
|
|
1017 |
+ |
/** |
1018 |
+ |
* A CyclicBarrier that fails with AssertionFailedErrors instead |
1019 |
+ |
* of throwing checked exceptions. |
1020 |
+ |
*/ |
1021 |
+ |
public class CheckedBarrier extends CyclicBarrier { |
1022 |
+ |
public CheckedBarrier(int parties) { super(parties); } |
1023 |
+ |
|
1024 |
+ |
public int await() { |
1025 |
+ |
try { |
1026 |
+ |
return super.await(); |
1027 |
+ |
} catch (Exception e) { |
1028 |
+ |
AssertionFailedError afe = |
1029 |
+ |
new AssertionFailedError("Unexpected exception: " + e); |
1030 |
+ |
afe.initCause(e); |
1031 |
+ |
throw afe; |
1032 |
+ |
} |
1033 |
+ |
} |
1034 |
+ |
} |
1035 |
+ |
|
1036 |
+ |
public void checkEmpty(BlockingQueue q) { |
1037 |
+ |
try { |
1038 |
+ |
assertTrue(q.isEmpty()); |
1039 |
+ |
assertEquals(0, q.size()); |
1040 |
+ |
assertNull(q.peek()); |
1041 |
+ |
assertNull(q.poll()); |
1042 |
+ |
assertNull(q.poll(0, MILLISECONDS)); |
1043 |
+ |
assertEquals(q.toString(), "[]"); |
1044 |
+ |
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
1045 |
+ |
assertFalse(q.iterator().hasNext()); |
1046 |
+ |
try { |
1047 |
+ |
q.element(); |
1048 |
+ |
shouldThrow(); |
1049 |
+ |
} catch (NoSuchElementException success) {} |
1050 |
+ |
try { |
1051 |
+ |
q.iterator().next(); |
1052 |
+ |
shouldThrow(); |
1053 |
+ |
} catch (NoSuchElementException success) {} |
1054 |
+ |
try { |
1055 |
+ |
q.remove(); |
1056 |
+ |
shouldThrow(); |
1057 |
+ |
} catch (NoSuchElementException success) {} |
1058 |
+ |
} catch (InterruptedException ie) { |
1059 |
+ |
threadUnexpectedException(ie); |
1060 |
+ |
} |
1061 |
+ |
} |
1062 |
+ |
|
1063 |
|
} |