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.ArrayList; |
15 |
+ |
import java.util.Arrays; |
16 |
+ |
import java.util.Date; |
17 |
+ |
import java.util.Enumeration; |
18 |
+ |
import java.util.List; |
19 |
+ |
import java.util.NoSuchElementException; |
20 |
|
import java.util.PropertyPermission; |
21 |
|
import java.util.concurrent.*; |
22 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
23 |
|
import java.util.concurrent.atomic.AtomicReference; |
24 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
25 |
+ |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
26 |
|
import java.security.CodeSource; |
27 |
|
import java.security.Permission; |
28 |
|
import java.security.PermissionCollection; |
72 |
|
* |
73 |
|
* </ol> |
74 |
|
* |
75 |
< |
* <p> <b>Other notes</b> |
75 |
> |
* <p><b>Other notes</b> |
76 |
|
* <ul> |
77 |
|
* |
78 |
|
* <li> Usually, there is one testcase method per JSR166 method |
108 |
|
private static final boolean useSecurityManager = |
109 |
|
Boolean.getBoolean("jsr166.useSecurityManager"); |
110 |
|
|
111 |
+ |
protected static final boolean expensiveTests = |
112 |
+ |
Boolean.getBoolean("jsr166.expensiveTests"); |
113 |
+ |
|
114 |
+ |
/** |
115 |
+ |
* If true, report on stdout all "slow" tests, that is, ones that |
116 |
+ |
* take more than profileThreshold milliseconds to execute. |
117 |
+ |
*/ |
118 |
+ |
private static final boolean profileTests = |
119 |
+ |
Boolean.getBoolean("jsr166.profileTests"); |
120 |
+ |
|
121 |
+ |
/** |
122 |
+ |
* The number of milliseconds that tests are permitted for |
123 |
+ |
* execution without being reported, when profileTests is set. |
124 |
+ |
*/ |
125 |
+ |
private static final long profileThreshold = |
126 |
+ |
Long.getLong("jsr166.profileThreshold", 100); |
127 |
+ |
|
128 |
+ |
protected void runTest() throws Throwable { |
129 |
+ |
if (profileTests) |
130 |
+ |
runTestProfiled(); |
131 |
+ |
else |
132 |
+ |
super.runTest(); |
133 |
+ |
} |
134 |
+ |
|
135 |
+ |
protected void runTestProfiled() throws Throwable { |
136 |
+ |
long t0 = System.nanoTime(); |
137 |
+ |
try { |
138 |
+ |
super.runTest(); |
139 |
+ |
} finally { |
140 |
+ |
long elapsedMillis = |
141 |
+ |
(System.nanoTime() - t0) / (1000L * 1000L); |
142 |
+ |
if (elapsedMillis >= profileThreshold) |
143 |
+ |
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); |
144 |
+ |
} |
145 |
+ |
} |
146 |
+ |
|
147 |
|
/** |
148 |
|
* Runs all JSR166 unit tests using junit.textui.TestRunner |
149 |
|
*/ |
164 |
|
System.exit(0); |
165 |
|
} |
166 |
|
|
167 |
+ |
public static TestSuite newTestSuite(Object... suiteOrClasses) { |
168 |
+ |
TestSuite suite = new TestSuite(); |
169 |
+ |
for (Object suiteOrClass : suiteOrClasses) { |
170 |
+ |
if (suiteOrClass instanceof TestSuite) |
171 |
+ |
suite.addTest((TestSuite) suiteOrClass); |
172 |
+ |
else if (suiteOrClass instanceof Class) |
173 |
+ |
suite.addTest(new TestSuite((Class<?>) suiteOrClass)); |
174 |
+ |
else |
175 |
+ |
throw new ClassCastException("not a test suite or class"); |
176 |
+ |
} |
177 |
+ |
return suite; |
178 |
+ |
} |
179 |
+ |
|
180 |
|
/** |
181 |
< |
* Collects all JSR166 unit tests as one suite |
181 |
> |
* Collects all JSR166 unit tests as one suite. |
182 |
|
*/ |
183 |
|
public static Test suite() { |
184 |
< |
TestSuite suite = new TestSuite("JSR166 Unit Tests"); |
185 |
< |
|
186 |
< |
suite.addTest(ForkJoinPoolTest.suite()); |
187 |
< |
suite.addTest(ForkJoinTaskTest.suite()); |
188 |
< |
suite.addTest(RecursiveActionTest.suite()); |
189 |
< |
suite.addTest(RecursiveTaskTest.suite()); |
190 |
< |
suite.addTest(LinkedTransferQueueTest.suite()); |
191 |
< |
suite.addTest(PhaserTest.suite()); |
192 |
< |
suite.addTest(ThreadLocalRandomTest.suite()); |
193 |
< |
suite.addTest(AbstractExecutorServiceTest.suite()); |
194 |
< |
suite.addTest(AbstractQueueTest.suite()); |
195 |
< |
suite.addTest(AbstractQueuedSynchronizerTest.suite()); |
196 |
< |
suite.addTest(AbstractQueuedLongSynchronizerTest.suite()); |
197 |
< |
suite.addTest(ArrayBlockingQueueTest.suite()); |
198 |
< |
suite.addTest(ArrayDequeTest.suite()); |
199 |
< |
suite.addTest(AtomicBooleanTest.suite()); |
200 |
< |
suite.addTest(AtomicIntegerArrayTest.suite()); |
201 |
< |
suite.addTest(AtomicIntegerFieldUpdaterTest.suite()); |
202 |
< |
suite.addTest(AtomicIntegerTest.suite()); |
203 |
< |
suite.addTest(AtomicLongArrayTest.suite()); |
204 |
< |
suite.addTest(AtomicLongFieldUpdaterTest.suite()); |
205 |
< |
suite.addTest(AtomicLongTest.suite()); |
206 |
< |
suite.addTest(AtomicMarkableReferenceTest.suite()); |
207 |
< |
suite.addTest(AtomicReferenceArrayTest.suite()); |
208 |
< |
suite.addTest(AtomicReferenceFieldUpdaterTest.suite()); |
209 |
< |
suite.addTest(AtomicReferenceTest.suite()); |
210 |
< |
suite.addTest(AtomicStampedReferenceTest.suite()); |
211 |
< |
suite.addTest(ConcurrentHashMapTest.suite()); |
212 |
< |
suite.addTest(ConcurrentLinkedDequeTest.suite()); |
213 |
< |
suite.addTest(ConcurrentLinkedQueueTest.suite()); |
214 |
< |
suite.addTest(ConcurrentSkipListMapTest.suite()); |
215 |
< |
suite.addTest(ConcurrentSkipListSubMapTest.suite()); |
216 |
< |
suite.addTest(ConcurrentSkipListSetTest.suite()); |
217 |
< |
suite.addTest(ConcurrentSkipListSubSetTest.suite()); |
218 |
< |
suite.addTest(CopyOnWriteArrayListTest.suite()); |
219 |
< |
suite.addTest(CopyOnWriteArraySetTest.suite()); |
220 |
< |
suite.addTest(CountDownLatchTest.suite()); |
221 |
< |
suite.addTest(CyclicBarrierTest.suite()); |
222 |
< |
suite.addTest(DelayQueueTest.suite()); |
223 |
< |
suite.addTest(EntryTest.suite()); |
224 |
< |
suite.addTest(ExchangerTest.suite()); |
225 |
< |
suite.addTest(ExecutorsTest.suite()); |
226 |
< |
suite.addTest(ExecutorCompletionServiceTest.suite()); |
227 |
< |
suite.addTest(FutureTaskTest.suite()); |
228 |
< |
suite.addTest(LinkedBlockingDequeTest.suite()); |
229 |
< |
suite.addTest(LinkedBlockingQueueTest.suite()); |
230 |
< |
suite.addTest(LinkedListTest.suite()); |
231 |
< |
suite.addTest(LockSupportTest.suite()); |
232 |
< |
suite.addTest(PriorityBlockingQueueTest.suite()); |
233 |
< |
suite.addTest(PriorityQueueTest.suite()); |
234 |
< |
suite.addTest(ReentrantLockTest.suite()); |
235 |
< |
suite.addTest(ReentrantReadWriteLockTest.suite()); |
236 |
< |
suite.addTest(ScheduledExecutorTest.suite()); |
237 |
< |
suite.addTest(ScheduledExecutorSubclassTest.suite()); |
238 |
< |
suite.addTest(SemaphoreTest.suite()); |
239 |
< |
suite.addTest(SynchronousQueueTest.suite()); |
240 |
< |
suite.addTest(SystemTest.suite()); |
241 |
< |
suite.addTest(ThreadLocalTest.suite()); |
242 |
< |
suite.addTest(ThreadPoolExecutorTest.suite()); |
243 |
< |
suite.addTest(ThreadPoolExecutorSubclassTest.suite()); |
244 |
< |
suite.addTest(ThreadTest.suite()); |
245 |
< |
suite.addTest(TimeUnitTest.suite()); |
246 |
< |
suite.addTest(TreeMapTest.suite()); |
247 |
< |
suite.addTest(TreeSetTest.suite()); |
248 |
< |
suite.addTest(TreeSubMapTest.suite()); |
188 |
< |
suite.addTest(TreeSubSetTest.suite()); |
189 |
< |
|
190 |
< |
return suite; |
184 |
> |
return newTestSuite( |
185 |
> |
ForkJoinPoolTest.suite(), |
186 |
> |
ForkJoinTaskTest.suite(), |
187 |
> |
RecursiveActionTest.suite(), |
188 |
> |
RecursiveTaskTest.suite(), |
189 |
> |
LinkedTransferQueueTest.suite(), |
190 |
> |
PhaserTest.suite(), |
191 |
> |
ThreadLocalRandomTest.suite(), |
192 |
> |
AbstractExecutorServiceTest.suite(), |
193 |
> |
AbstractQueueTest.suite(), |
194 |
> |
AbstractQueuedSynchronizerTest.suite(), |
195 |
> |
AbstractQueuedLongSynchronizerTest.suite(), |
196 |
> |
ArrayBlockingQueueTest.suite(), |
197 |
> |
ArrayDequeTest.suite(), |
198 |
> |
AtomicBooleanTest.suite(), |
199 |
> |
AtomicIntegerArrayTest.suite(), |
200 |
> |
AtomicIntegerFieldUpdaterTest.suite(), |
201 |
> |
AtomicIntegerTest.suite(), |
202 |
> |
AtomicLongArrayTest.suite(), |
203 |
> |
AtomicLongFieldUpdaterTest.suite(), |
204 |
> |
AtomicLongTest.suite(), |
205 |
> |
AtomicMarkableReferenceTest.suite(), |
206 |
> |
AtomicReferenceArrayTest.suite(), |
207 |
> |
AtomicReferenceFieldUpdaterTest.suite(), |
208 |
> |
AtomicReferenceTest.suite(), |
209 |
> |
AtomicStampedReferenceTest.suite(), |
210 |
> |
ConcurrentHashMapTest.suite(), |
211 |
> |
ConcurrentLinkedDequeTest.suite(), |
212 |
> |
ConcurrentLinkedQueueTest.suite(), |
213 |
> |
ConcurrentSkipListMapTest.suite(), |
214 |
> |
ConcurrentSkipListSubMapTest.suite(), |
215 |
> |
ConcurrentSkipListSetTest.suite(), |
216 |
> |
ConcurrentSkipListSubSetTest.suite(), |
217 |
> |
CopyOnWriteArrayListTest.suite(), |
218 |
> |
CopyOnWriteArraySetTest.suite(), |
219 |
> |
CountDownLatchTest.suite(), |
220 |
> |
CyclicBarrierTest.suite(), |
221 |
> |
DelayQueueTest.suite(), |
222 |
> |
EntryTest.suite(), |
223 |
> |
ExchangerTest.suite(), |
224 |
> |
ExecutorsTest.suite(), |
225 |
> |
ExecutorCompletionServiceTest.suite(), |
226 |
> |
FutureTaskTest.suite(), |
227 |
> |
LinkedBlockingDequeTest.suite(), |
228 |
> |
LinkedBlockingQueueTest.suite(), |
229 |
> |
LinkedListTest.suite(), |
230 |
> |
LockSupportTest.suite(), |
231 |
> |
PriorityBlockingQueueTest.suite(), |
232 |
> |
PriorityQueueTest.suite(), |
233 |
> |
ReentrantLockTest.suite(), |
234 |
> |
ReentrantReadWriteLockTest.suite(), |
235 |
> |
ScheduledExecutorTest.suite(), |
236 |
> |
ScheduledExecutorSubclassTest.suite(), |
237 |
> |
SemaphoreTest.suite(), |
238 |
> |
SynchronousQueueTest.suite(), |
239 |
> |
SystemTest.suite(), |
240 |
> |
ThreadLocalTest.suite(), |
241 |
> |
ThreadPoolExecutorTest.suite(), |
242 |
> |
ThreadPoolExecutorSubclassTest.suite(), |
243 |
> |
ThreadTest.suite(), |
244 |
> |
TimeUnitTest.suite(), |
245 |
> |
TreeMapTest.suite(), |
246 |
> |
TreeSetTest.suite(), |
247 |
> |
TreeSubMapTest.suite(), |
248 |
> |
TreeSubSetTest.suite()); |
249 |
|
} |
250 |
|
|
251 |
|
|
263 |
|
return 50; |
264 |
|
} |
265 |
|
|
208 |
– |
|
266 |
|
/** |
267 |
|
* Sets delays as multiples of SHORT_DELAY. |
268 |
|
*/ |
270 |
|
SHORT_DELAY_MS = getShortDelay(); |
271 |
|
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
272 |
|
MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10; |
273 |
< |
LONG_DELAY_MS = SHORT_DELAY_MS * 50; |
273 |
> |
LONG_DELAY_MS = SHORT_DELAY_MS * 200; |
274 |
> |
} |
275 |
> |
|
276 |
> |
/** |
277 |
> |
* Returns a timeout in milliseconds to be used in tests that |
278 |
> |
* verify that operations block or time out. |
279 |
> |
*/ |
280 |
> |
long timeoutMillis() { |
281 |
> |
return SHORT_DELAY_MS / 4; |
282 |
> |
} |
283 |
> |
|
284 |
> |
/** |
285 |
> |
* Returns a new Date instance representing a time delayMillis |
286 |
> |
* milliseconds in the future. |
287 |
> |
*/ |
288 |
> |
Date delayedDate(long delayMillis) { |
289 |
> |
return new Date(System.currentTimeMillis() + delayMillis); |
290 |
|
} |
291 |
|
|
292 |
|
/** |
310 |
|
} |
311 |
|
|
312 |
|
/** |
313 |
+ |
* Extra checks that get done for all test cases. |
314 |
+ |
* |
315 |
|
* Triggers test case failure if any thread assertions have failed, |
316 |
|
* by rethrowing, in the test harness thread, any exception recorded |
317 |
|
* earlier by threadRecordFailure. |
318 |
+ |
* |
319 |
+ |
* Triggers test case failure if interrupt status is set in the main thread. |
320 |
|
*/ |
321 |
|
public void tearDown() throws Exception { |
322 |
< |
Throwable t = threadFailure.get(); |
322 |
> |
Throwable t = threadFailure.getAndSet(null); |
323 |
|
if (t != null) { |
324 |
|
if (t instanceof Error) |
325 |
|
throw (Error) t; |
334 |
|
throw afe; |
335 |
|
} |
336 |
|
} |
337 |
+ |
|
338 |
+ |
if (Thread.interrupted()) |
339 |
+ |
throw new AssertionFailedError("interrupt status set in main thread"); |
340 |
|
} |
341 |
|
|
342 |
|
/** |
468 |
|
else { |
469 |
|
AssertionFailedError afe = |
470 |
|
new AssertionFailedError("unexpected exception: " + t); |
471 |
< |
t.initCause(t); |
471 |
> |
afe.initCause(t); |
472 |
|
throw afe; |
473 |
|
} |
474 |
|
} |
475 |
|
|
476 |
|
/** |
477 |
+ |
* Delays, via Thread.sleep, for the given millisecond delay, but |
478 |
+ |
* if the sleep is shorter than specified, may re-sleep or yield |
479 |
+ |
* until time elapses. |
480 |
+ |
*/ |
481 |
+ |
static void delay(long millis) throws InterruptedException { |
482 |
+ |
long startTime = System.nanoTime(); |
483 |
+ |
long ns = millis * 1000 * 1000; |
484 |
+ |
for (;;) { |
485 |
+ |
if (millis > 0L) |
486 |
+ |
Thread.sleep(millis); |
487 |
+ |
else // too short to sleep |
488 |
+ |
Thread.yield(); |
489 |
+ |
long d = ns - (System.nanoTime() - startTime); |
490 |
+ |
if (d > 0L) |
491 |
+ |
millis = d / (1000 * 1000); |
492 |
+ |
else |
493 |
+ |
break; |
494 |
+ |
} |
495 |
+ |
} |
496 |
+ |
|
497 |
+ |
/** |
498 |
|
* Waits out termination of a thread pool or fails doing so. |
499 |
|
*/ |
500 |
< |
public void joinPool(ExecutorService exec) { |
500 |
> |
void joinPool(ExecutorService exec) { |
501 |
|
try { |
502 |
|
exec.shutdown(); |
503 |
|
assertTrue("ExecutorService did not terminate in a timely manner", |
504 |
< |
exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
504 |
> |
exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)); |
505 |
|
} catch (SecurityException ok) { |
506 |
|
// Allowed in case test doesn't have privs |
507 |
|
} catch (InterruptedException ie) { |
509 |
|
} |
510 |
|
} |
511 |
|
|
512 |
+ |
/** |
513 |
+ |
* Checks that thread does not terminate within the default |
514 |
+ |
* millisecond delay of {@code timeoutMillis()}. |
515 |
+ |
*/ |
516 |
+ |
void assertThreadStaysAlive(Thread thread) { |
517 |
+ |
assertThreadStaysAlive(thread, timeoutMillis()); |
518 |
+ |
} |
519 |
+ |
|
520 |
+ |
/** |
521 |
+ |
* Checks that thread does not terminate within the given millisecond delay. |
522 |
+ |
*/ |
523 |
+ |
void assertThreadStaysAlive(Thread thread, long millis) { |
524 |
+ |
try { |
525 |
+ |
// No need to optimize the failing case via Thread.join. |
526 |
+ |
delay(millis); |
527 |
+ |
assertTrue(thread.isAlive()); |
528 |
+ |
} catch (InterruptedException ie) { |
529 |
+ |
fail("Unexpected InterruptedException"); |
530 |
+ |
} |
531 |
+ |
} |
532 |
+ |
|
533 |
+ |
/** |
534 |
+ |
* Checks that the threads do not terminate within the default |
535 |
+ |
* millisecond delay of {@code timeoutMillis()}. |
536 |
+ |
*/ |
537 |
+ |
void assertThreadsStayAlive(Thread... threads) { |
538 |
+ |
assertThreadsStayAlive(timeoutMillis(), threads); |
539 |
+ |
} |
540 |
+ |
|
541 |
+ |
/** |
542 |
+ |
* Checks that the threads do not terminate within the given millisecond delay. |
543 |
+ |
*/ |
544 |
+ |
void assertThreadsStayAlive(long millis, Thread... threads) { |
545 |
+ |
try { |
546 |
+ |
// No need to optimize the failing case via Thread.join. |
547 |
+ |
delay(millis); |
548 |
+ |
for (Thread thread : threads) |
549 |
+ |
assertTrue(thread.isAlive()); |
550 |
+ |
} catch (InterruptedException ie) { |
551 |
+ |
fail("Unexpected InterruptedException"); |
552 |
+ |
} |
553 |
+ |
} |
554 |
+ |
|
555 |
+ |
/** |
556 |
+ |
* Checks that future.get times out, with the default timeout of |
557 |
+ |
* {@code timeoutMillis()}. |
558 |
+ |
*/ |
559 |
+ |
void assertFutureTimesOut(Future future) { |
560 |
+ |
assertFutureTimesOut(future, timeoutMillis()); |
561 |
+ |
} |
562 |
+ |
|
563 |
+ |
/** |
564 |
+ |
* Checks that future.get times out, with the given millisecond timeout. |
565 |
+ |
*/ |
566 |
+ |
void assertFutureTimesOut(Future future, long timeoutMillis) { |
567 |
+ |
long startTime = System.nanoTime(); |
568 |
+ |
try { |
569 |
+ |
future.get(timeoutMillis, MILLISECONDS); |
570 |
+ |
shouldThrow(); |
571 |
+ |
} catch (TimeoutException success) { |
572 |
+ |
} catch (Exception e) { |
573 |
+ |
threadUnexpectedException(e); |
574 |
+ |
} finally { future.cancel(true); } |
575 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
576 |
+ |
} |
577 |
|
|
578 |
|
/** |
579 |
|
* Fails with message "should throw exception". |
626 |
|
SecurityManager sm = System.getSecurityManager(); |
627 |
|
if (sm == null) { |
628 |
|
r.run(); |
629 |
+ |
} |
630 |
+ |
runWithSecurityManagerWithPermissions(r, permissions); |
631 |
+ |
} |
632 |
+ |
|
633 |
+ |
/** |
634 |
+ |
* Runs Runnable r with a security policy that permits precisely |
635 |
+ |
* the specified permissions. If there is no current security |
636 |
+ |
* manager, a temporary one is set for the duration of the |
637 |
+ |
* Runnable. We require that any security manager permit |
638 |
+ |
* getPolicy/setPolicy. |
639 |
+ |
*/ |
640 |
+ |
public void runWithSecurityManagerWithPermissions(Runnable r, |
641 |
+ |
Permission... permissions) { |
642 |
+ |
SecurityManager sm = System.getSecurityManager(); |
643 |
+ |
if (sm == null) { |
644 |
|
Policy savedPolicy = Policy.getPolicy(); |
645 |
|
try { |
646 |
|
Policy.setPolicy(permissivePolicy()); |
647 |
|
System.setSecurityManager(new SecurityManager()); |
648 |
< |
runWithPermissions(r, permissions); |
648 |
> |
runWithSecurityManagerWithPermissions(r, permissions); |
649 |
|
} finally { |
650 |
|
System.setSecurityManager(null); |
651 |
|
Policy.setPolicy(savedPolicy); |
693 |
|
return perms.implies(p); |
694 |
|
} |
695 |
|
public void refresh() {} |
696 |
+ |
public String toString() { |
697 |
+ |
List<Permission> ps = new ArrayList<Permission>(); |
698 |
+ |
for (Enumeration<Permission> e = perms.elements(); e.hasMoreElements();) |
699 |
+ |
ps.add(e.nextElement()); |
700 |
+ |
return "AdjustablePolicy with permissions " + ps; |
701 |
+ |
} |
702 |
|
} |
703 |
|
|
704 |
|
/** |
721 |
|
} |
722 |
|
|
723 |
|
/** |
724 |
< |
* Sleep until the timeout has elapsed, or interrupted. |
725 |
< |
* Does <em>NOT</em> throw InterruptedException. |
724 |
> |
* Sleeps until the given time has elapsed. |
725 |
> |
* Throws AssertionFailedError if interrupted. |
726 |
|
*/ |
727 |
< |
void sleepTillInterrupted(long timeoutMillis) { |
727 |
> |
void sleep(long millis) { |
728 |
|
try { |
729 |
< |
Thread.sleep(timeoutMillis); |
730 |
< |
} catch (InterruptedException wakeup) {} |
729 |
> |
delay(millis); |
730 |
> |
} catch (InterruptedException ie) { |
731 |
> |
AssertionFailedError afe = |
732 |
> |
new AssertionFailedError("Unexpected InterruptedException"); |
733 |
> |
afe.initCause(ie); |
734 |
> |
throw afe; |
735 |
> |
} |
736 |
> |
} |
737 |
> |
|
738 |
> |
/** |
739 |
> |
* Spin-waits up to the specified number of milliseconds for the given |
740 |
> |
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
741 |
> |
*/ |
742 |
> |
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
743 |
> |
long startTime = System.nanoTime(); |
744 |
> |
for (;;) { |
745 |
> |
Thread.State s = thread.getState(); |
746 |
> |
if (s == Thread.State.BLOCKED || |
747 |
> |
s == Thread.State.WAITING || |
748 |
> |
s == Thread.State.TIMED_WAITING) |
749 |
> |
return; |
750 |
> |
else if (s == Thread.State.TERMINATED) |
751 |
> |
fail("Unexpected thread termination"); |
752 |
> |
else if (millisElapsedSince(startTime) > timeoutMillis) { |
753 |
> |
threadAssertTrue(thread.isAlive()); |
754 |
> |
return; |
755 |
> |
} |
756 |
> |
Thread.yield(); |
757 |
> |
} |
758 |
> |
} |
759 |
> |
|
760 |
> |
/** |
761 |
> |
* Waits up to LONG_DELAY_MS for the given thread to enter a wait |
762 |
> |
* state: BLOCKED, WAITING, or TIMED_WAITING. |
763 |
> |
*/ |
764 |
> |
void waitForThreadToEnterWaitState(Thread thread) { |
765 |
> |
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS); |
766 |
> |
} |
767 |
> |
|
768 |
> |
/** |
769 |
> |
* Returns the number of milliseconds since time given by |
770 |
> |
* startNanoTime, which must have been previously returned from a |
771 |
> |
* call to {@link System.nanoTime()}. |
772 |
> |
*/ |
773 |
> |
long millisElapsedSince(long startNanoTime) { |
774 |
> |
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
775 |
|
} |
776 |
|
|
777 |
|
/** |
784 |
|
return t; |
785 |
|
} |
786 |
|
|
787 |
+ |
/** |
788 |
+ |
* Waits for the specified time (in milliseconds) for the thread |
789 |
+ |
* to terminate (using {@link Thread#join(long)}), else interrupts |
790 |
+ |
* the thread (in the hope that it may terminate later) and fails. |
791 |
+ |
*/ |
792 |
+ |
void awaitTermination(Thread t, long timeoutMillis) { |
793 |
+ |
try { |
794 |
+ |
t.join(timeoutMillis); |
795 |
+ |
} catch (InterruptedException ie) { |
796 |
+ |
threadUnexpectedException(ie); |
797 |
+ |
} finally { |
798 |
+ |
if (t.getState() != Thread.State.TERMINATED) { |
799 |
+ |
t.interrupt(); |
800 |
+ |
fail("Test timed out"); |
801 |
+ |
} |
802 |
+ |
} |
803 |
+ |
} |
804 |
+ |
|
805 |
+ |
/** |
806 |
+ |
* Waits for LONG_DELAY_MS milliseconds for the thread to |
807 |
+ |
* terminate (using {@link Thread#join(long)}), else interrupts |
808 |
+ |
* the thread (in the hope that it may terminate later) and fails. |
809 |
+ |
*/ |
810 |
+ |
void awaitTermination(Thread t) { |
811 |
+ |
awaitTermination(t, LONG_DELAY_MS); |
812 |
+ |
} |
813 |
+ |
|
814 |
|
// Some convenient Runnable classes |
815 |
|
|
816 |
|
public abstract class CheckedRunnable implements Runnable { |
873 |
|
realRun(); |
874 |
|
threadShouldThrow("InterruptedException"); |
875 |
|
} catch (InterruptedException success) { |
876 |
+ |
threadAssertFalse(Thread.interrupted()); |
877 |
|
} catch (Throwable t) { |
878 |
|
threadUnexpectedException(t); |
879 |
|
} |
903 |
|
threadShouldThrow("InterruptedException"); |
904 |
|
return result; |
905 |
|
} catch (InterruptedException success) { |
906 |
+ |
threadAssertFalse(Thread.interrupted()); |
907 |
|
} catch (Throwable t) { |
908 |
|
threadUnexpectedException(t); |
909 |
|
} |
927 |
|
|
928 |
|
public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) { |
929 |
|
return new CheckedCallable<String>() { |
930 |
< |
public String realCall() { |
930 |
> |
protected String realCall() { |
931 |
|
try { |
932 |
|
latch.await(); |
933 |
|
} catch (InterruptedException quittingTime) {} |
935 |
|
}}; |
936 |
|
} |
937 |
|
|
938 |
+ |
public Runnable awaiter(final CountDownLatch latch) { |
939 |
+ |
return new CheckedRunnable() { |
940 |
+ |
public void realRun() throws InterruptedException { |
941 |
+ |
await(latch); |
942 |
+ |
}}; |
943 |
+ |
} |
944 |
+ |
|
945 |
+ |
public void await(CountDownLatch latch) { |
946 |
+ |
try { |
947 |
+ |
assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); |
948 |
+ |
} catch (Throwable t) { |
949 |
+ |
threadUnexpectedException(t); |
950 |
+ |
} |
951 |
+ |
} |
952 |
+ |
|
953 |
+ |
public void await(Semaphore semaphore) { |
954 |
+ |
try { |
955 |
+ |
assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
956 |
+ |
} catch (Throwable t) { |
957 |
+ |
threadUnexpectedException(t); |
958 |
+ |
} |
959 |
+ |
} |
960 |
+ |
|
961 |
+ |
// /** |
962 |
+ |
// * Spin-waits up to LONG_DELAY_MS until flag becomes true. |
963 |
+ |
// */ |
964 |
+ |
// public void await(AtomicBoolean flag) { |
965 |
+ |
// await(flag, LONG_DELAY_MS); |
966 |
+ |
// } |
967 |
+ |
|
968 |
+ |
// /** |
969 |
+ |
// * Spin-waits up to the specified timeout until flag becomes true. |
970 |
+ |
// */ |
971 |
+ |
// public void await(AtomicBoolean flag, long timeoutMillis) { |
972 |
+ |
// long startTime = System.nanoTime(); |
973 |
+ |
// while (!flag.get()) { |
974 |
+ |
// if (millisElapsedSince(startTime) > timeoutMillis) |
975 |
+ |
// throw new AssertionFailedError("timed out"); |
976 |
+ |
// Thread.yield(); |
977 |
+ |
// } |
978 |
+ |
// } |
979 |
+ |
|
980 |
|
public static class NPETask implements Callable<String> { |
981 |
|
public String call() { throw new NullPointerException(); } |
982 |
|
} |
987 |
|
|
988 |
|
public class ShortRunnable extends CheckedRunnable { |
989 |
|
protected void realRun() throws Throwable { |
990 |
< |
Thread.sleep(SHORT_DELAY_MS); |
990 |
> |
delay(SHORT_DELAY_MS); |
991 |
|
} |
992 |
|
} |
993 |
|
|
994 |
|
public class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
995 |
|
protected void realRun() throws InterruptedException { |
996 |
< |
Thread.sleep(SHORT_DELAY_MS); |
996 |
> |
delay(SHORT_DELAY_MS); |
997 |
|
} |
998 |
|
} |
999 |
|
|
1000 |
|
public class SmallRunnable extends CheckedRunnable { |
1001 |
|
protected void realRun() throws Throwable { |
1002 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1002 |
> |
delay(SMALL_DELAY_MS); |
1003 |
|
} |
1004 |
|
} |
1005 |
|
|
1006 |
|
public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
1007 |
|
protected void realRun() { |
1008 |
|
try { |
1009 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1009 |
> |
delay(SMALL_DELAY_MS); |
1010 |
|
} catch (InterruptedException ok) {} |
1011 |
|
} |
1012 |
|
} |
1013 |
|
|
1014 |
|
public class SmallCallable extends CheckedCallable { |
1015 |
|
protected Object realCall() throws InterruptedException { |
1016 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1016 |
> |
delay(SMALL_DELAY_MS); |
1017 |
|
return Boolean.TRUE; |
1018 |
|
} |
1019 |
|
} |
1020 |
|
|
1021 |
|
public class MediumRunnable extends CheckedRunnable { |
1022 |
|
protected void realRun() throws Throwable { |
1023 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1023 |
> |
delay(MEDIUM_DELAY_MS); |
1024 |
|
} |
1025 |
|
} |
1026 |
|
|
1027 |
|
public class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
1028 |
|
protected void realRun() throws InterruptedException { |
1029 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1029 |
> |
delay(MEDIUM_DELAY_MS); |
1030 |
|
} |
1031 |
|
} |
1032 |
|
|
1033 |
+ |
public Runnable possiblyInterruptedRunnable(final long timeoutMillis) { |
1034 |
+ |
return new CheckedRunnable() { |
1035 |
+ |
protected void realRun() { |
1036 |
+ |
try { |
1037 |
+ |
delay(timeoutMillis); |
1038 |
+ |
} catch (InterruptedException ok) {} |
1039 |
+ |
}}; |
1040 |
+ |
} |
1041 |
+ |
|
1042 |
|
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
1043 |
|
protected void realRun() { |
1044 |
|
try { |
1045 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1045 |
> |
delay(MEDIUM_DELAY_MS); |
1046 |
|
} catch (InterruptedException ok) {} |
1047 |
|
} |
1048 |
|
} |
1050 |
|
public class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
1051 |
|
protected void realRun() { |
1052 |
|
try { |
1053 |
< |
Thread.sleep(LONG_DELAY_MS); |
1053 |
> |
delay(LONG_DELAY_MS); |
1054 |
|
} catch (InterruptedException ok) {} |
1055 |
|
} |
1056 |
|
} |
1064 |
|
} |
1065 |
|
} |
1066 |
|
|
1067 |
+ |
public interface TrackedRunnable extends Runnable { |
1068 |
+ |
boolean isDone(); |
1069 |
+ |
} |
1070 |
+ |
|
1071 |
+ |
public static TrackedRunnable trackedRunnable(final long timeoutMillis) { |
1072 |
+ |
return new TrackedRunnable() { |
1073 |
+ |
private volatile boolean done = false; |
1074 |
+ |
public boolean isDone() { return done; } |
1075 |
+ |
public void run() { |
1076 |
+ |
try { |
1077 |
+ |
delay(timeoutMillis); |
1078 |
+ |
done = true; |
1079 |
+ |
} catch (InterruptedException ok) {} |
1080 |
+ |
} |
1081 |
+ |
}; |
1082 |
+ |
} |
1083 |
+ |
|
1084 |
|
public static class TrackedShortRunnable implements Runnable { |
1085 |
|
public volatile boolean done = false; |
1086 |
|
public void run() { |
1087 |
|
try { |
1088 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1088 |
> |
delay(SHORT_DELAY_MS); |
1089 |
> |
done = true; |
1090 |
> |
} catch (InterruptedException ok) {} |
1091 |
> |
} |
1092 |
> |
} |
1093 |
> |
|
1094 |
> |
public static class TrackedSmallRunnable implements Runnable { |
1095 |
> |
public volatile boolean done = false; |
1096 |
> |
public void run() { |
1097 |
> |
try { |
1098 |
> |
delay(SMALL_DELAY_MS); |
1099 |
|
done = true; |
1100 |
|
} catch (InterruptedException ok) {} |
1101 |
|
} |
1105 |
|
public volatile boolean done = false; |
1106 |
|
public void run() { |
1107 |
|
try { |
1108 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1108 |
> |
delay(MEDIUM_DELAY_MS); |
1109 |
|
done = true; |
1110 |
|
} catch (InterruptedException ok) {} |
1111 |
|
} |
1115 |
|
public volatile boolean done = false; |
1116 |
|
public void run() { |
1117 |
|
try { |
1118 |
< |
Thread.sleep(LONG_DELAY_MS); |
1118 |
> |
delay(LONG_DELAY_MS); |
1119 |
|
done = true; |
1120 |
|
} catch (InterruptedException ok) {} |
1121 |
|
} |
1132 |
|
public volatile boolean done = false; |
1133 |
|
public Object call() { |
1134 |
|
try { |
1135 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1135 |
> |
delay(SMALL_DELAY_MS); |
1136 |
|
done = true; |
1137 |
|
} catch (InterruptedException ok) {} |
1138 |
|
return Boolean.TRUE; |
1178 |
|
ThreadPoolExecutor executor) {} |
1179 |
|
} |
1180 |
|
|
1181 |
+ |
/** |
1182 |
+ |
* A CyclicBarrier that uses timed await and fails with |
1183 |
+ |
* AssertionFailedErrors instead of throwing checked exceptions. |
1184 |
+ |
*/ |
1185 |
+ |
public class CheckedBarrier extends CyclicBarrier { |
1186 |
+ |
public CheckedBarrier(int parties) { super(parties); } |
1187 |
+ |
|
1188 |
+ |
public int await() { |
1189 |
+ |
try { |
1190 |
+ |
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1191 |
+ |
} catch (TimeoutException e) { |
1192 |
+ |
throw new AssertionFailedError("timed out"); |
1193 |
+ |
} catch (Exception e) { |
1194 |
+ |
AssertionFailedError afe = |
1195 |
+ |
new AssertionFailedError("Unexpected exception: " + e); |
1196 |
+ |
afe.initCause(e); |
1197 |
+ |
throw afe; |
1198 |
+ |
} |
1199 |
+ |
} |
1200 |
+ |
} |
1201 |
+ |
|
1202 |
+ |
void checkEmpty(BlockingQueue q) { |
1203 |
+ |
try { |
1204 |
+ |
assertTrue(q.isEmpty()); |
1205 |
+ |
assertEquals(0, q.size()); |
1206 |
+ |
assertNull(q.peek()); |
1207 |
+ |
assertNull(q.poll()); |
1208 |
+ |
assertNull(q.poll(0, MILLISECONDS)); |
1209 |
+ |
assertEquals(q.toString(), "[]"); |
1210 |
+ |
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
1211 |
+ |
assertFalse(q.iterator().hasNext()); |
1212 |
+ |
try { |
1213 |
+ |
q.element(); |
1214 |
+ |
shouldThrow(); |
1215 |
+ |
} catch (NoSuchElementException success) {} |
1216 |
+ |
try { |
1217 |
+ |
q.iterator().next(); |
1218 |
+ |
shouldThrow(); |
1219 |
+ |
} catch (NoSuchElementException success) {} |
1220 |
+ |
try { |
1221 |
+ |
q.remove(); |
1222 |
+ |
shouldThrow(); |
1223 |
+ |
} catch (NoSuchElementException success) {} |
1224 |
+ |
} catch (InterruptedException ie) { |
1225 |
+ |
threadUnexpectedException(ie); |
1226 |
+ |
} |
1227 |
+ |
} |
1228 |
+ |
|
1229 |
+ |
void assertSerialEquals(Object x, Object y) { |
1230 |
+ |
assertTrue(Arrays.equals(serialBytes(x), serialBytes(y))); |
1231 |
+ |
} |
1232 |
+ |
|
1233 |
+ |
void assertNotSerialEquals(Object x, Object y) { |
1234 |
+ |
assertFalse(Arrays.equals(serialBytes(x), serialBytes(y))); |
1235 |
+ |
} |
1236 |
+ |
|
1237 |
+ |
byte[] serialBytes(Object o) { |
1238 |
+ |
try { |
1239 |
+ |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1240 |
+ |
ObjectOutputStream oos = new ObjectOutputStream(bos); |
1241 |
+ |
oos.writeObject(o); |
1242 |
+ |
oos.flush(); |
1243 |
+ |
oos.close(); |
1244 |
+ |
return bos.toByteArray(); |
1245 |
+ |
} catch (Throwable t) { |
1246 |
+ |
threadUnexpectedException(t); |
1247 |
+ |
return new byte[0]; |
1248 |
+ |
} |
1249 |
+ |
} |
1250 |
+ |
|
1251 |
+ |
@SuppressWarnings("unchecked") |
1252 |
+ |
<T> T serialClone(T o) { |
1253 |
+ |
try { |
1254 |
+ |
ObjectInputStream ois = new ObjectInputStream |
1255 |
+ |
(new ByteArrayInputStream(serialBytes(o))); |
1256 |
+ |
T clone = (T) ois.readObject(); |
1257 |
+ |
assertSame(o.getClass(), clone.getClass()); |
1258 |
+ |
return clone; |
1259 |
+ |
} catch (Throwable t) { |
1260 |
+ |
threadUnexpectedException(t); |
1261 |
+ |
return null; |
1262 |
+ |
} |
1263 |
+ |
} |
1264 |
|
} |