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.lang.management.ManagementFactory; |
15 |
+ |
import java.lang.management.ThreadInfo; |
16 |
+ |
import java.lang.reflect.Method; |
17 |
+ |
import java.util.ArrayList; |
18 |
+ |
import java.util.Arrays; |
19 |
+ |
import java.util.Date; |
20 |
+ |
import java.util.Enumeration; |
21 |
+ |
import java.util.List; |
22 |
+ |
import java.util.NoSuchElementException; |
23 |
|
import java.util.PropertyPermission; |
24 |
|
import java.util.concurrent.*; |
25 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
26 |
|
import java.util.concurrent.atomic.AtomicReference; |
27 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
28 |
|
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
75 |
|
* |
76 |
|
* </ol> |
77 |
|
* |
78 |
< |
* <p> <b>Other notes</b> |
78 |
> |
* <p><b>Other notes</b> |
79 |
|
* <ul> |
80 |
|
* |
81 |
|
* <li> Usually, there is one testcase method per JSR166 method |
148 |
|
} |
149 |
|
|
150 |
|
/** |
151 |
< |
* Runs all JSR166 unit tests using junit.textui.TestRunner |
151 |
> |
* Runs all JSR166 unit tests using junit.textui.TestRunner. |
152 |
> |
* Optional command line arg provides the number of iterations to |
153 |
> |
* repeat running the tests. |
154 |
|
*/ |
155 |
|
public static void main(String[] args) { |
156 |
|
if (useSecurityManager) { |
182 |
|
return suite; |
183 |
|
} |
184 |
|
|
185 |
+ |
public static void addNamedTestClasses(TestSuite suite, |
186 |
+ |
String... testClassNames) { |
187 |
+ |
for (String testClassName : testClassNames) { |
188 |
+ |
try { |
189 |
+ |
Class<?> testClass = Class.forName(testClassName); |
190 |
+ |
Method m = testClass.getDeclaredMethod("suite", |
191 |
+ |
new Class<?>[0]); |
192 |
+ |
suite.addTest(newTestSuite((Test)m.invoke(null))); |
193 |
+ |
} catch (Exception e) { |
194 |
+ |
throw new Error("Missing test class", e); |
195 |
+ |
} |
196 |
+ |
} |
197 |
+ |
} |
198 |
+ |
|
199 |
+ |
public static final double JAVA_CLASS_VERSION; |
200 |
+ |
static { |
201 |
+ |
try { |
202 |
+ |
JAVA_CLASS_VERSION = java.security.AccessController.doPrivileged( |
203 |
+ |
new java.security.PrivilegedAction<Double>() { |
204 |
+ |
public Double run() { |
205 |
+ |
return Double.valueOf(System.getProperty("java.class.version"));}}); |
206 |
+ |
} catch (Throwable t) { |
207 |
+ |
throw new Error(t); |
208 |
+ |
} |
209 |
+ |
} |
210 |
+ |
|
211 |
+ |
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
212 |
+ |
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
213 |
+ |
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
214 |
+ |
|
215 |
|
/** |
216 |
|
* Collects all JSR166 unit tests as one suite. |
217 |
|
*/ |
218 |
|
public static Test suite() { |
219 |
< |
return newTestSuite( |
219 |
> |
// Java7+ test classes |
220 |
> |
TestSuite suite = newTestSuite( |
221 |
|
ForkJoinPoolTest.suite(), |
222 |
|
ForkJoinTaskTest.suite(), |
223 |
|
RecursiveActionTest.suite(), |
282 |
|
TreeSetTest.suite(), |
283 |
|
TreeSubMapTest.suite(), |
284 |
|
TreeSubSetTest.suite()); |
285 |
+ |
|
286 |
+ |
// Java8+ test classes |
287 |
+ |
if (atLeastJava8()) { |
288 |
+ |
String[] java8TestClassNames = { |
289 |
+ |
"LongAdderTest", |
290 |
+ |
"LongAccumulatorTest", |
291 |
+ |
"DoubleAdderTest", |
292 |
+ |
"DoubleAccumulatorTest", |
293 |
+ |
"CompletableFutureTest", |
294 |
+ |
"ForkJoinPool8Test", |
295 |
+ |
"StampedLockTest", |
296 |
+ |
}; |
297 |
+ |
addNamedTestClasses(suite, java8TestClassNames); |
298 |
+ |
} |
299 |
+ |
|
300 |
+ |
return suite; |
301 |
|
} |
302 |
|
|
303 |
|
|
315 |
|
return 50; |
316 |
|
} |
317 |
|
|
255 |
– |
|
318 |
|
/** |
319 |
|
* Sets delays as multiples of SHORT_DELAY. |
320 |
|
*/ |
322 |
|
SHORT_DELAY_MS = getShortDelay(); |
323 |
|
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
324 |
|
MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10; |
325 |
< |
LONG_DELAY_MS = SHORT_DELAY_MS * 50; |
325 |
> |
LONG_DELAY_MS = SHORT_DELAY_MS * 200; |
326 |
> |
} |
327 |
> |
|
328 |
> |
/** |
329 |
> |
* Returns a timeout in milliseconds to be used in tests that |
330 |
> |
* verify that operations block or time out. |
331 |
> |
*/ |
332 |
> |
long timeoutMillis() { |
333 |
> |
return SHORT_DELAY_MS / 4; |
334 |
> |
} |
335 |
> |
|
336 |
> |
/** |
337 |
> |
* Returns a new Date instance representing a time delayMillis |
338 |
> |
* milliseconds in the future. |
339 |
> |
*/ |
340 |
> |
Date delayedDate(long delayMillis) { |
341 |
> |
return new Date(System.currentTimeMillis() + delayMillis); |
342 |
|
} |
343 |
|
|
344 |
|
/** |
362 |
|
} |
363 |
|
|
364 |
|
/** |
365 |
+ |
* Extra checks that get done for all test cases. |
366 |
+ |
* |
367 |
|
* Triggers test case failure if any thread assertions have failed, |
368 |
|
* by rethrowing, in the test harness thread, any exception recorded |
369 |
|
* earlier by threadRecordFailure. |
370 |
+ |
* |
371 |
+ |
* Triggers test case failure if interrupt status is set in the main thread. |
372 |
|
*/ |
373 |
|
public void tearDown() throws Exception { |
374 |
|
Throwable t = threadFailure.getAndSet(null); |
386 |
|
throw afe; |
387 |
|
} |
388 |
|
} |
389 |
+ |
|
390 |
+ |
if (Thread.interrupted()) |
391 |
+ |
throw new AssertionFailedError("interrupt status set in main thread"); |
392 |
+ |
|
393 |
+ |
checkForkJoinPoolThreadLeaks(); |
394 |
+ |
} |
395 |
+ |
|
396 |
+ |
/** |
397 |
+ |
* Find missing try { ... } finally { joinPool(e); } |
398 |
+ |
*/ |
399 |
+ |
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
400 |
+ |
Thread[] survivors = new Thread[5]; |
401 |
+ |
int count = Thread.enumerate(survivors); |
402 |
+ |
for (int i = 0; i < count; i++) { |
403 |
+ |
Thread thread = survivors[i]; |
404 |
+ |
String name = thread.getName(); |
405 |
+ |
if (name.startsWith("ForkJoinPool-")) { |
406 |
+ |
// give thread some time to terminate |
407 |
+ |
thread.join(LONG_DELAY_MS); |
408 |
+ |
if (!thread.isAlive()) continue; |
409 |
+ |
thread.stop(); |
410 |
+ |
throw new AssertionFailedError |
411 |
+ |
(String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n", |
412 |
+ |
toString(), name)); |
413 |
+ |
} |
414 |
+ |
} |
415 |
|
} |
416 |
|
|
417 |
|
/** |
543 |
|
else { |
544 |
|
AssertionFailedError afe = |
545 |
|
new AssertionFailedError("unexpected exception: " + t); |
546 |
< |
t.initCause(t); |
546 |
> |
afe.initCause(t); |
547 |
|
throw afe; |
548 |
|
} |
549 |
|
} |
550 |
|
|
551 |
|
/** |
552 |
+ |
* Delays, via Thread.sleep, for the given millisecond delay, but |
553 |
+ |
* if the sleep is shorter than specified, may re-sleep or yield |
554 |
+ |
* until time elapses. |
555 |
+ |
*/ |
556 |
+ |
static void delay(long millis) throws InterruptedException { |
557 |
+ |
long startTime = System.nanoTime(); |
558 |
+ |
long ns = millis * 1000 * 1000; |
559 |
+ |
for (;;) { |
560 |
+ |
if (millis > 0L) |
561 |
+ |
Thread.sleep(millis); |
562 |
+ |
else // too short to sleep |
563 |
+ |
Thread.yield(); |
564 |
+ |
long d = ns - (System.nanoTime() - startTime); |
565 |
+ |
if (d > 0L) |
566 |
+ |
millis = d / (1000 * 1000); |
567 |
+ |
else |
568 |
+ |
break; |
569 |
+ |
} |
570 |
+ |
} |
571 |
+ |
|
572 |
+ |
/** |
573 |
|
* Waits out termination of a thread pool or fails doing so. |
574 |
|
*/ |
575 |
< |
public void joinPool(ExecutorService exec) { |
575 |
> |
void joinPool(ExecutorService exec) { |
576 |
|
try { |
577 |
|
exec.shutdown(); |
578 |
|
assertTrue("ExecutorService did not terminate in a timely manner", |
579 |
< |
exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
579 |
> |
exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)); |
580 |
|
} catch (SecurityException ok) { |
581 |
|
// Allowed in case test doesn't have privs |
582 |
|
} catch (InterruptedException ie) { |
584 |
|
} |
585 |
|
} |
586 |
|
|
587 |
+ |
/** |
588 |
+ |
* A debugging tool to print all stack traces, as jstack does. |
589 |
+ |
*/ |
590 |
+ |
static void printAllStackTraces() { |
591 |
+ |
for (ThreadInfo info : |
592 |
+ |
ManagementFactory.getThreadMXBean() |
593 |
+ |
.dumpAllThreads(true, true)) |
594 |
+ |
System.err.print(info); |
595 |
+ |
} |
596 |
+ |
|
597 |
+ |
/** |
598 |
+ |
* Checks that thread does not terminate within the default |
599 |
+ |
* millisecond delay of {@code timeoutMillis()}. |
600 |
+ |
*/ |
601 |
+ |
void assertThreadStaysAlive(Thread thread) { |
602 |
+ |
assertThreadStaysAlive(thread, timeoutMillis()); |
603 |
+ |
} |
604 |
+ |
|
605 |
+ |
/** |
606 |
+ |
* Checks that thread does not terminate within the given millisecond delay. |
607 |
+ |
*/ |
608 |
+ |
void assertThreadStaysAlive(Thread thread, long millis) { |
609 |
+ |
try { |
610 |
+ |
// No need to optimize the failing case via Thread.join. |
611 |
+ |
delay(millis); |
612 |
+ |
assertTrue(thread.isAlive()); |
613 |
+ |
} catch (InterruptedException ie) { |
614 |
+ |
fail("Unexpected InterruptedException"); |
615 |
+ |
} |
616 |
+ |
} |
617 |
+ |
|
618 |
+ |
/** |
619 |
+ |
* Checks that the threads do not terminate within the default |
620 |
+ |
* millisecond delay of {@code timeoutMillis()}. |
621 |
+ |
*/ |
622 |
+ |
void assertThreadsStayAlive(Thread... threads) { |
623 |
+ |
assertThreadsStayAlive(timeoutMillis(), threads); |
624 |
+ |
} |
625 |
+ |
|
626 |
+ |
/** |
627 |
+ |
* Checks that the threads do not terminate within the given millisecond delay. |
628 |
+ |
*/ |
629 |
+ |
void assertThreadsStayAlive(long millis, Thread... threads) { |
630 |
+ |
try { |
631 |
+ |
// No need to optimize the failing case via Thread.join. |
632 |
+ |
delay(millis); |
633 |
+ |
for (Thread thread : threads) |
634 |
+ |
assertTrue(thread.isAlive()); |
635 |
+ |
} catch (InterruptedException ie) { |
636 |
+ |
fail("Unexpected InterruptedException"); |
637 |
+ |
} |
638 |
+ |
} |
639 |
+ |
|
640 |
+ |
/** |
641 |
+ |
* Checks that future.get times out, with the default timeout of |
642 |
+ |
* {@code timeoutMillis()}. |
643 |
+ |
*/ |
644 |
+ |
void assertFutureTimesOut(Future future) { |
645 |
+ |
assertFutureTimesOut(future, timeoutMillis()); |
646 |
+ |
} |
647 |
+ |
|
648 |
+ |
/** |
649 |
+ |
* Checks that future.get times out, with the given millisecond timeout. |
650 |
+ |
*/ |
651 |
+ |
void assertFutureTimesOut(Future future, long timeoutMillis) { |
652 |
+ |
long startTime = System.nanoTime(); |
653 |
+ |
try { |
654 |
+ |
future.get(timeoutMillis, MILLISECONDS); |
655 |
+ |
shouldThrow(); |
656 |
+ |
} catch (TimeoutException success) { |
657 |
+ |
} catch (Exception e) { |
658 |
+ |
threadUnexpectedException(e); |
659 |
+ |
} finally { future.cancel(true); } |
660 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
661 |
+ |
} |
662 |
|
|
663 |
|
/** |
664 |
|
* Fails with message "should throw exception". |
711 |
|
SecurityManager sm = System.getSecurityManager(); |
712 |
|
if (sm == null) { |
713 |
|
r.run(); |
714 |
+ |
} |
715 |
+ |
runWithSecurityManagerWithPermissions(r, permissions); |
716 |
+ |
} |
717 |
+ |
|
718 |
+ |
/** |
719 |
+ |
* Runs Runnable r with a security policy that permits precisely |
720 |
+ |
* the specified permissions. If there is no current security |
721 |
+ |
* manager, a temporary one is set for the duration of the |
722 |
+ |
* Runnable. We require that any security manager permit |
723 |
+ |
* getPolicy/setPolicy. |
724 |
+ |
*/ |
725 |
+ |
public void runWithSecurityManagerWithPermissions(Runnable r, |
726 |
+ |
Permission... permissions) { |
727 |
+ |
SecurityManager sm = System.getSecurityManager(); |
728 |
+ |
if (sm == null) { |
729 |
|
Policy savedPolicy = Policy.getPolicy(); |
730 |
|
try { |
731 |
|
Policy.setPolicy(permissivePolicy()); |
732 |
|
System.setSecurityManager(new SecurityManager()); |
733 |
< |
runWithPermissions(r, permissions); |
733 |
> |
runWithSecurityManagerWithPermissions(r, permissions); |
734 |
|
} finally { |
735 |
|
System.setSecurityManager(null); |
736 |
|
Policy.setPolicy(savedPolicy); |
778 |
|
return perms.implies(p); |
779 |
|
} |
780 |
|
public void refresh() {} |
781 |
+ |
public String toString() { |
782 |
+ |
List<Permission> ps = new ArrayList<Permission>(); |
783 |
+ |
for (Enumeration<Permission> e = perms.elements(); e.hasMoreElements();) |
784 |
+ |
ps.add(e.nextElement()); |
785 |
+ |
return "AdjustablePolicy with permissions " + ps; |
786 |
+ |
} |
787 |
|
} |
788 |
|
|
789 |
|
/** |
811 |
|
*/ |
812 |
|
void sleep(long millis) { |
813 |
|
try { |
814 |
< |
Thread.sleep(millis); |
814 |
> |
delay(millis); |
815 |
|
} catch (InterruptedException ie) { |
816 |
|
AssertionFailedError afe = |
817 |
|
new AssertionFailedError("Unexpected InterruptedException"); |
821 |
|
} |
822 |
|
|
823 |
|
/** |
824 |
< |
* 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 |
< |
/** |
609 |
< |
* Waits up to the specified number of milliseconds for the given |
824 |
> |
* Spin-waits up to the specified number of milliseconds for the given |
825 |
|
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
826 |
|
*/ |
827 |
|
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
828 |
< |
long timeoutNanos = timeoutMillis * 1000L * 1000L; |
614 |
< |
long t0 = System.nanoTime(); |
828 |
> |
long startTime = System.nanoTime(); |
829 |
|
for (;;) { |
830 |
|
Thread.State s = thread.getState(); |
831 |
|
if (s == Thread.State.BLOCKED || |
834 |
|
return; |
835 |
|
else if (s == Thread.State.TERMINATED) |
836 |
|
fail("Unexpected thread termination"); |
837 |
< |
else if (System.nanoTime() - t0 > timeoutNanos) { |
837 |
> |
else if (millisElapsedSince(startTime) > timeoutMillis) { |
838 |
|
threadAssertTrue(thread.isAlive()); |
839 |
|
return; |
840 |
|
} |
843 |
|
} |
844 |
|
|
845 |
|
/** |
846 |
+ |
* Waits up to LONG_DELAY_MS for the given thread to enter a wait |
847 |
+ |
* state: BLOCKED, WAITING, or TIMED_WAITING. |
848 |
+ |
*/ |
849 |
+ |
void waitForThreadToEnterWaitState(Thread thread) { |
850 |
+ |
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS); |
851 |
+ |
} |
852 |
+ |
|
853 |
+ |
/** |
854 |
|
* Returns the number of milliseconds since time given by |
855 |
|
* startNanoTime, which must have been previously returned from a |
856 |
|
* call to {@link System.nanoTime()}. |
880 |
|
} catch (InterruptedException ie) { |
881 |
|
threadUnexpectedException(ie); |
882 |
|
} finally { |
883 |
< |
if (t.isAlive()) { |
883 |
> |
if (t.getState() != Thread.State.TERMINATED) { |
884 |
|
t.interrupt(); |
885 |
|
fail("Test timed out"); |
886 |
|
} |
887 |
|
} |
888 |
|
} |
889 |
|
|
890 |
+ |
/** |
891 |
+ |
* Waits for LONG_DELAY_MS milliseconds for the thread to |
892 |
+ |
* terminate (using {@link Thread#join(long)}), else interrupts |
893 |
+ |
* the thread (in the hope that it may terminate later) and fails. |
894 |
+ |
*/ |
895 |
+ |
void awaitTermination(Thread t) { |
896 |
+ |
awaitTermination(t, LONG_DELAY_MS); |
897 |
+ |
} |
898 |
+ |
|
899 |
|
// Some convenient Runnable classes |
900 |
|
|
901 |
|
public abstract class CheckedRunnable implements Runnable { |
958 |
|
realRun(); |
959 |
|
threadShouldThrow("InterruptedException"); |
960 |
|
} catch (InterruptedException success) { |
961 |
+ |
threadAssertFalse(Thread.interrupted()); |
962 |
|
} catch (Throwable t) { |
963 |
|
threadUnexpectedException(t); |
964 |
|
} |
988 |
|
threadShouldThrow("InterruptedException"); |
989 |
|
return result; |
990 |
|
} catch (InterruptedException success) { |
991 |
+ |
threadAssertFalse(Thread.interrupted()); |
992 |
|
} catch (Throwable t) { |
993 |
|
threadUnexpectedException(t); |
994 |
|
} |
1020 |
|
}}; |
1021 |
|
} |
1022 |
|
|
1023 |
+ |
public Runnable awaiter(final CountDownLatch latch) { |
1024 |
+ |
return new CheckedRunnable() { |
1025 |
+ |
public void realRun() throws InterruptedException { |
1026 |
+ |
await(latch); |
1027 |
+ |
}}; |
1028 |
+ |
} |
1029 |
+ |
|
1030 |
+ |
public void await(CountDownLatch latch) { |
1031 |
+ |
try { |
1032 |
+ |
assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); |
1033 |
+ |
} catch (Throwable t) { |
1034 |
+ |
threadUnexpectedException(t); |
1035 |
+ |
} |
1036 |
+ |
} |
1037 |
+ |
|
1038 |
+ |
public void await(Semaphore semaphore) { |
1039 |
+ |
try { |
1040 |
+ |
assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
1041 |
+ |
} catch (Throwable t) { |
1042 |
+ |
threadUnexpectedException(t); |
1043 |
+ |
} |
1044 |
+ |
} |
1045 |
+ |
|
1046 |
+ |
// /** |
1047 |
+ |
// * Spin-waits up to LONG_DELAY_MS until flag becomes true. |
1048 |
+ |
// */ |
1049 |
+ |
// public void await(AtomicBoolean flag) { |
1050 |
+ |
// await(flag, LONG_DELAY_MS); |
1051 |
+ |
// } |
1052 |
+ |
|
1053 |
+ |
// /** |
1054 |
+ |
// * Spin-waits up to the specified timeout until flag becomes true. |
1055 |
+ |
// */ |
1056 |
+ |
// public void await(AtomicBoolean flag, long timeoutMillis) { |
1057 |
+ |
// long startTime = System.nanoTime(); |
1058 |
+ |
// while (!flag.get()) { |
1059 |
+ |
// if (millisElapsedSince(startTime) > timeoutMillis) |
1060 |
+ |
// throw new AssertionFailedError("timed out"); |
1061 |
+ |
// Thread.yield(); |
1062 |
+ |
// } |
1063 |
+ |
// } |
1064 |
+ |
|
1065 |
|
public static class NPETask implements Callable<String> { |
1066 |
|
public String call() { throw new NullPointerException(); } |
1067 |
|
} |
1072 |
|
|
1073 |
|
public class ShortRunnable extends CheckedRunnable { |
1074 |
|
protected void realRun() throws Throwable { |
1075 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1075 |
> |
delay(SHORT_DELAY_MS); |
1076 |
|
} |
1077 |
|
} |
1078 |
|
|
1079 |
|
public class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
1080 |
|
protected void realRun() throws InterruptedException { |
1081 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1081 |
> |
delay(SHORT_DELAY_MS); |
1082 |
|
} |
1083 |
|
} |
1084 |
|
|
1085 |
|
public class SmallRunnable extends CheckedRunnable { |
1086 |
|
protected void realRun() throws Throwable { |
1087 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1087 |
> |
delay(SMALL_DELAY_MS); |
1088 |
|
} |
1089 |
|
} |
1090 |
|
|
1091 |
|
public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
1092 |
|
protected void realRun() { |
1093 |
|
try { |
1094 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1094 |
> |
delay(SMALL_DELAY_MS); |
1095 |
|
} catch (InterruptedException ok) {} |
1096 |
|
} |
1097 |
|
} |
1098 |
|
|
1099 |
|
public class SmallCallable extends CheckedCallable { |
1100 |
|
protected Object realCall() throws InterruptedException { |
1101 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1101 |
> |
delay(SMALL_DELAY_MS); |
1102 |
|
return Boolean.TRUE; |
1103 |
|
} |
1104 |
|
} |
1105 |
|
|
1106 |
|
public class MediumRunnable extends CheckedRunnable { |
1107 |
|
protected void realRun() throws Throwable { |
1108 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1108 |
> |
delay(MEDIUM_DELAY_MS); |
1109 |
|
} |
1110 |
|
} |
1111 |
|
|
1112 |
|
public class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
1113 |
|
protected void realRun() throws InterruptedException { |
1114 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1114 |
> |
delay(MEDIUM_DELAY_MS); |
1115 |
|
} |
1116 |
|
} |
1117 |
|
|
1119 |
|
return new CheckedRunnable() { |
1120 |
|
protected void realRun() { |
1121 |
|
try { |
1122 |
< |
Thread.sleep(timeoutMillis); |
1122 |
> |
delay(timeoutMillis); |
1123 |
|
} catch (InterruptedException ok) {} |
1124 |
|
}}; |
1125 |
|
} |
1127 |
|
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
1128 |
|
protected void realRun() { |
1129 |
|
try { |
1130 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1130 |
> |
delay(MEDIUM_DELAY_MS); |
1131 |
|
} catch (InterruptedException ok) {} |
1132 |
|
} |
1133 |
|
} |
1135 |
|
public class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
1136 |
|
protected void realRun() { |
1137 |
|
try { |
1138 |
< |
Thread.sleep(LONG_DELAY_MS); |
1138 |
> |
delay(LONG_DELAY_MS); |
1139 |
|
} catch (InterruptedException ok) {} |
1140 |
|
} |
1141 |
|
} |
1159 |
|
public boolean isDone() { return done; } |
1160 |
|
public void run() { |
1161 |
|
try { |
1162 |
< |
Thread.sleep(timeoutMillis); |
1162 |
> |
delay(timeoutMillis); |
1163 |
|
done = true; |
1164 |
|
} catch (InterruptedException ok) {} |
1165 |
|
} |
1170 |
|
public volatile boolean done = false; |
1171 |
|
public void run() { |
1172 |
|
try { |
1173 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1173 |
> |
delay(SHORT_DELAY_MS); |
1174 |
|
done = true; |
1175 |
|
} catch (InterruptedException ok) {} |
1176 |
|
} |
1180 |
|
public volatile boolean done = false; |
1181 |
|
public void run() { |
1182 |
|
try { |
1183 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1183 |
> |
delay(SMALL_DELAY_MS); |
1184 |
|
done = true; |
1185 |
|
} catch (InterruptedException ok) {} |
1186 |
|
} |
1190 |
|
public volatile boolean done = false; |
1191 |
|
public void run() { |
1192 |
|
try { |
1193 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1193 |
> |
delay(MEDIUM_DELAY_MS); |
1194 |
|
done = true; |
1195 |
|
} catch (InterruptedException ok) {} |
1196 |
|
} |
1200 |
|
public volatile boolean done = false; |
1201 |
|
public void run() { |
1202 |
|
try { |
1203 |
< |
Thread.sleep(LONG_DELAY_MS); |
1203 |
> |
delay(LONG_DELAY_MS); |
1204 |
|
done = true; |
1205 |
|
} catch (InterruptedException ok) {} |
1206 |
|
} |
1217 |
|
public volatile boolean done = false; |
1218 |
|
public Object call() { |
1219 |
|
try { |
1220 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1220 |
> |
delay(SMALL_DELAY_MS); |
1221 |
|
done = true; |
1222 |
|
} catch (InterruptedException ok) {} |
1223 |
|
return Boolean.TRUE; |
1264 |
|
} |
1265 |
|
|
1266 |
|
/** |
1267 |
< |
* A CyclicBarrier that fails with AssertionFailedErrors instead |
1268 |
< |
* of throwing checked exceptions. |
1267 |
> |
* A CyclicBarrier that uses timed await and fails with |
1268 |
> |
* AssertionFailedErrors instead of throwing checked exceptions. |
1269 |
|
*/ |
1270 |
|
public class CheckedBarrier extends CyclicBarrier { |
1271 |
|
public CheckedBarrier(int parties) { super(parties); } |
1272 |
|
|
1273 |
|
public int await() { |
1274 |
|
try { |
1275 |
< |
return super.await(); |
1275 |
> |
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1276 |
> |
} catch (TimeoutException e) { |
1277 |
> |
throw new AssertionFailedError("timed out"); |
1278 |
|
} catch (Exception e) { |
1279 |
|
AssertionFailedError afe = |
1280 |
|
new AssertionFailedError("Unexpected exception: " + e); |
1284 |
|
} |
1285 |
|
} |
1286 |
|
|
1287 |
+ |
void checkEmpty(BlockingQueue q) { |
1288 |
+ |
try { |
1289 |
+ |
assertTrue(q.isEmpty()); |
1290 |
+ |
assertEquals(0, q.size()); |
1291 |
+ |
assertNull(q.peek()); |
1292 |
+ |
assertNull(q.poll()); |
1293 |
+ |
assertNull(q.poll(0, MILLISECONDS)); |
1294 |
+ |
assertEquals(q.toString(), "[]"); |
1295 |
+ |
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
1296 |
+ |
assertFalse(q.iterator().hasNext()); |
1297 |
+ |
try { |
1298 |
+ |
q.element(); |
1299 |
+ |
shouldThrow(); |
1300 |
+ |
} catch (NoSuchElementException success) {} |
1301 |
+ |
try { |
1302 |
+ |
q.iterator().next(); |
1303 |
+ |
shouldThrow(); |
1304 |
+ |
} catch (NoSuchElementException success) {} |
1305 |
+ |
try { |
1306 |
+ |
q.remove(); |
1307 |
+ |
shouldThrow(); |
1308 |
+ |
} catch (NoSuchElementException success) {} |
1309 |
+ |
} catch (InterruptedException ie) { |
1310 |
+ |
threadUnexpectedException(ie); |
1311 |
+ |
} |
1312 |
+ |
} |
1313 |
+ |
|
1314 |
+ |
void assertSerialEquals(Object x, Object y) { |
1315 |
+ |
assertTrue(Arrays.equals(serialBytes(x), serialBytes(y))); |
1316 |
+ |
} |
1317 |
+ |
|
1318 |
+ |
void assertNotSerialEquals(Object x, Object y) { |
1319 |
+ |
assertFalse(Arrays.equals(serialBytes(x), serialBytes(y))); |
1320 |
+ |
} |
1321 |
+ |
|
1322 |
+ |
byte[] serialBytes(Object o) { |
1323 |
+ |
try { |
1324 |
+ |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1325 |
+ |
ObjectOutputStream oos = new ObjectOutputStream(bos); |
1326 |
+ |
oos.writeObject(o); |
1327 |
+ |
oos.flush(); |
1328 |
+ |
oos.close(); |
1329 |
+ |
return bos.toByteArray(); |
1330 |
+ |
} catch (Throwable t) { |
1331 |
+ |
threadUnexpectedException(t); |
1332 |
+ |
return new byte[0]; |
1333 |
+ |
} |
1334 |
+ |
} |
1335 |
+ |
|
1336 |
+ |
@SuppressWarnings("unchecked") |
1337 |
+ |
<T> T serialClone(T o) { |
1338 |
+ |
try { |
1339 |
+ |
ObjectInputStream ois = new ObjectInputStream |
1340 |
+ |
(new ByteArrayInputStream(serialBytes(o))); |
1341 |
+ |
T clone = (T) ois.readObject(); |
1342 |
+ |
assertSame(o.getClass(), clone.getClass()); |
1343 |
+ |
return clone; |
1344 |
+ |
} catch (Throwable t) { |
1345 |
+ |
threadUnexpectedException(t); |
1346 |
+ |
return null; |
1347 |
+ |
} |
1348 |
+ |
} |
1349 |
|
} |