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; |
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) { |
604 |
|
} |
605 |
|
|
606 |
|
/** |
607 |
+ |
* Sleeps until the given time has elapsed. |
608 |
+ |
* Throws AssertionFailedError if interrupted. |
609 |
+ |
*/ |
610 |
+ |
void sleep(long millis) { |
611 |
+ |
try { |
612 |
+ |
delay(millis); |
613 |
+ |
} catch (InterruptedException ie) { |
614 |
+ |
AssertionFailedError afe = |
615 |
+ |
new AssertionFailedError("Unexpected InterruptedException"); |
616 |
+ |
afe.initCause(ie); |
617 |
+ |
throw afe; |
618 |
+ |
} |
619 |
+ |
} |
620 |
+ |
|
621 |
+ |
/** |
622 |
|
* Sleeps until the timeout has elapsed, or interrupted. |
623 |
|
* Does <em>NOT</em> throw InterruptedException. |
624 |
|
*/ |
629 |
|
} |
630 |
|
|
631 |
|
/** |
632 |
+ |
* Waits up to the specified number of milliseconds for the given |
633 |
+ |
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
634 |
+ |
*/ |
635 |
+ |
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
636 |
+ |
long timeoutNanos = timeoutMillis * 1000L * 1000L; |
637 |
+ |
long t0 = System.nanoTime(); |
638 |
+ |
for (;;) { |
639 |
+ |
Thread.State s = thread.getState(); |
640 |
+ |
if (s == Thread.State.BLOCKED || |
641 |
+ |
s == Thread.State.WAITING || |
642 |
+ |
s == Thread.State.TIMED_WAITING) |
643 |
+ |
return; |
644 |
+ |
else if (s == Thread.State.TERMINATED) |
645 |
+ |
fail("Unexpected thread termination"); |
646 |
+ |
else if (System.nanoTime() - t0 > timeoutNanos) { |
647 |
+ |
threadAssertTrue(thread.isAlive()); |
648 |
+ |
return; |
649 |
+ |
} |
650 |
+ |
Thread.yield(); |
651 |
+ |
} |
652 |
+ |
} |
653 |
+ |
|
654 |
+ |
/** |
655 |
+ |
* Waits up to LONG_DELAY_MS for the given thread to enter a wait |
656 |
+ |
* state: BLOCKED, WAITING, or TIMED_WAITING. |
657 |
+ |
*/ |
658 |
+ |
void waitForThreadToEnterWaitState(Thread thread) { |
659 |
+ |
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS); |
660 |
+ |
} |
661 |
+ |
|
662 |
+ |
/** |
663 |
+ |
* Returns the number of milliseconds since time given by |
664 |
+ |
* startNanoTime, which must have been previously returned from a |
665 |
+ |
* call to {@link System.nanoTime()}. |
666 |
+ |
*/ |
667 |
+ |
long millisElapsedSince(long startNanoTime) { |
668 |
+ |
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
669 |
+ |
} |
670 |
+ |
|
671 |
+ |
/** |
672 |
|
* Returns a new started daemon Thread running the given runnable. |
673 |
|
*/ |
674 |
|
Thread newStartedThread(Runnable runnable) { |
696 |
|
} |
697 |
|
} |
698 |
|
|
699 |
+ |
/** |
700 |
+ |
* Waits for LONG_DELAY_MS milliseconds for the thread to |
701 |
+ |
* terminate (using {@link Thread#join(long)}), else interrupts |
702 |
+ |
* the thread (in the hope that it may terminate later) and fails. |
703 |
+ |
*/ |
704 |
+ |
void awaitTermination(Thread t) { |
705 |
+ |
awaitTermination(t, LONG_DELAY_MS); |
706 |
+ |
} |
707 |
+ |
|
708 |
|
// Some convenient Runnable classes |
709 |
|
|
710 |
|
public abstract class CheckedRunnable implements Runnable { |
819 |
|
|
820 |
|
public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) { |
821 |
|
return new CheckedCallable<String>() { |
822 |
< |
public String realCall() { |
822 |
> |
protected String realCall() { |
823 |
|
try { |
824 |
|
latch.await(); |
825 |
|
} catch (InterruptedException quittingTime) {} |
827 |
|
}}; |
828 |
|
} |
829 |
|
|
830 |
+ |
public Runnable awaiter(final CountDownLatch latch) { |
831 |
+ |
return new CheckedRunnable() { |
832 |
+ |
public void realRun() throws InterruptedException { |
833 |
+ |
latch.await(); |
834 |
+ |
}}; |
835 |
+ |
} |
836 |
+ |
|
837 |
|
public static class NPETask implements Callable<String> { |
838 |
|
public String call() { throw new NullPointerException(); } |
839 |
|
} |
844 |
|
|
845 |
|
public class ShortRunnable extends CheckedRunnable { |
846 |
|
protected void realRun() throws Throwable { |
847 |
< |
Thread.sleep(SHORT_DELAY_MS); |
847 |
> |
delay(SHORT_DELAY_MS); |
848 |
|
} |
849 |
|
} |
850 |
|
|
851 |
|
public class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
852 |
|
protected void realRun() throws InterruptedException { |
853 |
< |
Thread.sleep(SHORT_DELAY_MS); |
853 |
> |
delay(SHORT_DELAY_MS); |
854 |
|
} |
855 |
|
} |
856 |
|
|
857 |
|
public class SmallRunnable extends CheckedRunnable { |
858 |
|
protected void realRun() throws Throwable { |
859 |
< |
Thread.sleep(SMALL_DELAY_MS); |
859 |
> |
delay(SMALL_DELAY_MS); |
860 |
|
} |
861 |
|
} |
862 |
|
|
863 |
|
public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
864 |
|
protected void realRun() { |
865 |
|
try { |
866 |
< |
Thread.sleep(SMALL_DELAY_MS); |
866 |
> |
delay(SMALL_DELAY_MS); |
867 |
|
} catch (InterruptedException ok) {} |
868 |
|
} |
869 |
|
} |
870 |
|
|
871 |
|
public class SmallCallable extends CheckedCallable { |
872 |
|
protected Object realCall() throws InterruptedException { |
873 |
< |
Thread.sleep(SMALL_DELAY_MS); |
873 |
> |
delay(SMALL_DELAY_MS); |
874 |
|
return Boolean.TRUE; |
875 |
|
} |
876 |
|
} |
877 |
|
|
878 |
|
public class MediumRunnable extends CheckedRunnable { |
879 |
|
protected void realRun() throws Throwable { |
880 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
880 |
> |
delay(MEDIUM_DELAY_MS); |
881 |
|
} |
882 |
|
} |
883 |
|
|
884 |
|
public class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
885 |
|
protected void realRun() throws InterruptedException { |
886 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
886 |
> |
delay(MEDIUM_DELAY_MS); |
887 |
|
} |
888 |
|
} |
889 |
|
|
890 |
+ |
public Runnable possiblyInterruptedRunnable(final long timeoutMillis) { |
891 |
+ |
return new CheckedRunnable() { |
892 |
+ |
protected void realRun() { |
893 |
+ |
try { |
894 |
+ |
delay(timeoutMillis); |
895 |
+ |
} catch (InterruptedException ok) {} |
896 |
+ |
}}; |
897 |
+ |
} |
898 |
+ |
|
899 |
|
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
900 |
|
protected void realRun() { |
901 |
|
try { |
902 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
902 |
> |
delay(MEDIUM_DELAY_MS); |
903 |
|
} catch (InterruptedException ok) {} |
904 |
|
} |
905 |
|
} |
907 |
|
public class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
908 |
|
protected void realRun() { |
909 |
|
try { |
910 |
< |
Thread.sleep(LONG_DELAY_MS); |
910 |
> |
delay(LONG_DELAY_MS); |
911 |
|
} catch (InterruptedException ok) {} |
912 |
|
} |
913 |
|
} |
921 |
|
} |
922 |
|
} |
923 |
|
|
924 |
+ |
public interface TrackedRunnable extends Runnable { |
925 |
+ |
boolean isDone(); |
926 |
+ |
} |
927 |
+ |
|
928 |
+ |
public static TrackedRunnable trackedRunnable(final long timeoutMillis) { |
929 |
+ |
return new TrackedRunnable() { |
930 |
+ |
private volatile boolean done = false; |
931 |
+ |
public boolean isDone() { return done; } |
932 |
+ |
public void run() { |
933 |
+ |
try { |
934 |
+ |
delay(timeoutMillis); |
935 |
+ |
done = true; |
936 |
+ |
} catch (InterruptedException ok) {} |
937 |
+ |
} |
938 |
+ |
}; |
939 |
+ |
} |
940 |
+ |
|
941 |
|
public static class TrackedShortRunnable implements Runnable { |
942 |
|
public volatile boolean done = false; |
943 |
|
public void run() { |
944 |
|
try { |
945 |
< |
Thread.sleep(SMALL_DELAY_MS); |
945 |
> |
delay(SHORT_DELAY_MS); |
946 |
> |
done = true; |
947 |
> |
} catch (InterruptedException ok) {} |
948 |
> |
} |
949 |
> |
} |
950 |
> |
|
951 |
> |
public static class TrackedSmallRunnable implements Runnable { |
952 |
> |
public volatile boolean done = false; |
953 |
> |
public void run() { |
954 |
> |
try { |
955 |
> |
delay(SMALL_DELAY_MS); |
956 |
|
done = true; |
957 |
|
} catch (InterruptedException ok) {} |
958 |
|
} |
962 |
|
public volatile boolean done = false; |
963 |
|
public void run() { |
964 |
|
try { |
965 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
965 |
> |
delay(MEDIUM_DELAY_MS); |
966 |
|
done = true; |
967 |
|
} catch (InterruptedException ok) {} |
968 |
|
} |
972 |
|
public volatile boolean done = false; |
973 |
|
public void run() { |
974 |
|
try { |
975 |
< |
Thread.sleep(LONG_DELAY_MS); |
975 |
> |
delay(LONG_DELAY_MS); |
976 |
|
done = true; |
977 |
|
} catch (InterruptedException ok) {} |
978 |
|
} |
989 |
|
public volatile boolean done = false; |
990 |
|
public Object call() { |
991 |
|
try { |
992 |
< |
Thread.sleep(SMALL_DELAY_MS); |
992 |
> |
delay(SMALL_DELAY_MS); |
993 |
|
done = true; |
994 |
|
} catch (InterruptedException ok) {} |
995 |
|
return Boolean.TRUE; |
1035 |
|
ThreadPoolExecutor executor) {} |
1036 |
|
} |
1037 |
|
|
1038 |
+ |
/** |
1039 |
+ |
* A CyclicBarrier that fails with AssertionFailedErrors instead |
1040 |
+ |
* of throwing checked exceptions. |
1041 |
+ |
*/ |
1042 |
+ |
public class CheckedBarrier extends CyclicBarrier { |
1043 |
+ |
public CheckedBarrier(int parties) { super(parties); } |
1044 |
+ |
|
1045 |
+ |
public int await() { |
1046 |
+ |
try { |
1047 |
+ |
return super.await(); |
1048 |
+ |
} catch (Exception e) { |
1049 |
+ |
AssertionFailedError afe = |
1050 |
+ |
new AssertionFailedError("Unexpected exception: " + e); |
1051 |
+ |
afe.initCause(e); |
1052 |
+ |
throw afe; |
1053 |
+ |
} |
1054 |
+ |
} |
1055 |
+ |
} |
1056 |
+ |
|
1057 |
+ |
public void checkEmpty(BlockingQueue q) { |
1058 |
+ |
try { |
1059 |
+ |
assertTrue(q.isEmpty()); |
1060 |
+ |
assertEquals(0, q.size()); |
1061 |
+ |
assertNull(q.peek()); |
1062 |
+ |
assertNull(q.poll()); |
1063 |
+ |
assertNull(q.poll(0, MILLISECONDS)); |
1064 |
+ |
assertEquals(q.toString(), "[]"); |
1065 |
+ |
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
1066 |
+ |
assertFalse(q.iterator().hasNext()); |
1067 |
+ |
try { |
1068 |
+ |
q.element(); |
1069 |
+ |
shouldThrow(); |
1070 |
+ |
} catch (NoSuchElementException success) {} |
1071 |
+ |
try { |
1072 |
+ |
q.iterator().next(); |
1073 |
+ |
shouldThrow(); |
1074 |
+ |
} catch (NoSuchElementException success) {} |
1075 |
+ |
try { |
1076 |
+ |
q.remove(); |
1077 |
+ |
shouldThrow(); |
1078 |
+ |
} catch (NoSuchElementException success) {} |
1079 |
+ |
} catch (InterruptedException ie) { |
1080 |
+ |
threadUnexpectedException(ie); |
1081 |
+ |
} |
1082 |
+ |
} |
1083 |
+ |
|
1084 |
|
} |