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; |
29 |
+ |
import java.util.regex.Pattern; |
30 |
|
import java.security.CodeSource; |
31 |
|
import java.security.Permission; |
32 |
|
import java.security.PermissionCollection; |
76 |
|
* |
77 |
|
* </ol> |
78 |
|
* |
79 |
< |
* <p> <b>Other notes</b> |
79 |
> |
* <p><b>Other notes</b> |
80 |
|
* <ul> |
81 |
|
* |
82 |
|
* <li> Usually, there is one testcase method per JSR166 method |
129 |
|
private static final long profileThreshold = |
130 |
|
Long.getLong("jsr166.profileThreshold", 100); |
131 |
|
|
132 |
+ |
/** |
133 |
+ |
* The number of repetitions per test (for tickling rare bugs). |
134 |
+ |
*/ |
135 |
+ |
private static final int runsPerTest = |
136 |
+ |
Integer.getInteger("jsr166.runsPerTest", 1); |
137 |
+ |
|
138 |
+ |
/** |
139 |
+ |
* A filter for tests to run, matching strings of the form |
140 |
+ |
* methodName(className), e.g. "testInvokeAll5(ForkJoinPoolTest)" |
141 |
+ |
* Usefully combined with jsr166.runsPerTest. |
142 |
+ |
*/ |
143 |
+ |
private static final Pattern methodFilter = methodFilter(); |
144 |
+ |
|
145 |
+ |
private static Pattern methodFilter() { |
146 |
+ |
String regex = System.getProperty("jsr166.methodFilter"); |
147 |
+ |
return (regex == null) ? null : Pattern.compile(regex); |
148 |
+ |
} |
149 |
+ |
|
150 |
|
protected void runTest() throws Throwable { |
151 |
< |
if (profileTests) |
152 |
< |
runTestProfiled(); |
153 |
< |
else |
154 |
< |
super.runTest(); |
151 |
> |
if (methodFilter == null |
152 |
> |
|| methodFilter.matcher(toString()).find()) { |
153 |
> |
for (int i = 0; i < runsPerTest; i++) { |
154 |
> |
if (profileTests) |
155 |
> |
runTestProfiled(); |
156 |
> |
else |
157 |
> |
super.runTest(); |
158 |
> |
} |
159 |
> |
} |
160 |
|
} |
161 |
|
|
162 |
|
protected void runTestProfiled() throws Throwable { |
172 |
|
} |
173 |
|
|
174 |
|
/** |
175 |
< |
* Runs all JSR166 unit tests using junit.textui.TestRunner |
175 |
> |
* Runs all JSR166 unit tests using junit.textui.TestRunner. |
176 |
> |
* Optional command line arg provides the number of iterations to |
177 |
> |
* repeat running the tests. |
178 |
|
*/ |
179 |
|
public static void main(String[] args) { |
180 |
|
if (useSecurityManager) { |
206 |
|
return suite; |
207 |
|
} |
208 |
|
|
209 |
+ |
public static void addNamedTestClasses(TestSuite suite, |
210 |
+ |
String... testClassNames) { |
211 |
+ |
for (String testClassName : testClassNames) { |
212 |
+ |
try { |
213 |
+ |
Class<?> testClass = Class.forName(testClassName); |
214 |
+ |
Method m = testClass.getDeclaredMethod("suite", |
215 |
+ |
new Class<?>[0]); |
216 |
+ |
suite.addTest(newTestSuite((Test)m.invoke(null))); |
217 |
+ |
} catch (Exception e) { |
218 |
+ |
throw new Error("Missing test class", e); |
219 |
+ |
} |
220 |
+ |
} |
221 |
+ |
} |
222 |
+ |
|
223 |
+ |
public static final double JAVA_CLASS_VERSION; |
224 |
+ |
public static final String JAVA_SPECIFICATION_VERSION; |
225 |
+ |
static { |
226 |
+ |
try { |
227 |
+ |
JAVA_CLASS_VERSION = java.security.AccessController.doPrivileged( |
228 |
+ |
new java.security.PrivilegedAction<Double>() { |
229 |
+ |
public Double run() { |
230 |
+ |
return Double.valueOf(System.getProperty("java.class.version"));}}); |
231 |
+ |
JAVA_SPECIFICATION_VERSION = java.security.AccessController.doPrivileged( |
232 |
+ |
new java.security.PrivilegedAction<String>() { |
233 |
+ |
public String run() { |
234 |
+ |
return System.getProperty("java.specification.version");}}); |
235 |
+ |
} catch (Throwable t) { |
236 |
+ |
throw new Error(t); |
237 |
+ |
} |
238 |
+ |
} |
239 |
+ |
|
240 |
+ |
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
241 |
+ |
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
242 |
+ |
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
243 |
+ |
public static boolean atLeastJava9() { |
244 |
+ |
// As of 2014-05, java9 still uses 52.0 class file version |
245 |
+ |
return JAVA_SPECIFICATION_VERSION.startsWith("1.9"); |
246 |
+ |
} |
247 |
+ |
|
248 |
|
/** |
249 |
|
* Collects all JSR166 unit tests as one suite. |
250 |
|
*/ |
251 |
|
public static Test suite() { |
252 |
< |
return newTestSuite( |
252 |
> |
// Java7+ test classes |
253 |
> |
TestSuite suite = newTestSuite( |
254 |
|
ForkJoinPoolTest.suite(), |
255 |
|
ForkJoinTaskTest.suite(), |
256 |
|
RecursiveActionTest.suite(), |
315 |
|
TreeSetTest.suite(), |
316 |
|
TreeSubMapTest.suite(), |
317 |
|
TreeSubSetTest.suite()); |
318 |
+ |
|
319 |
+ |
// Java8+ test classes |
320 |
+ |
if (atLeastJava8()) { |
321 |
+ |
String[] java8TestClassNames = { |
322 |
+ |
"Atomic8Test", |
323 |
+ |
"CompletableFutureTest", |
324 |
+ |
"ConcurrentHashMap8Test", |
325 |
+ |
"CountedCompleterTest", |
326 |
+ |
"DoubleAccumulatorTest", |
327 |
+ |
"DoubleAdderTest", |
328 |
+ |
"ForkJoinPool8Test", |
329 |
+ |
"ForkJoinTask8Test", |
330 |
+ |
"LongAccumulatorTest", |
331 |
+ |
"LongAdderTest", |
332 |
+ |
"SplittableRandomTest", |
333 |
+ |
"StampedLockTest", |
334 |
+ |
"ThreadLocalRandom8Test", |
335 |
+ |
}; |
336 |
+ |
addNamedTestClasses(suite, java8TestClassNames); |
337 |
+ |
} |
338 |
+ |
|
339 |
+ |
// Java9+ test classes |
340 |
+ |
if (atLeastJava9()) { |
341 |
+ |
String[] java9TestClassNames = { |
342 |
+ |
"ThreadPoolExecutor9Test", |
343 |
+ |
}; |
344 |
+ |
addNamedTestClasses(suite, java9TestClassNames); |
345 |
+ |
} |
346 |
+ |
|
347 |
+ |
return suite; |
348 |
|
} |
349 |
|
|
350 |
+ |
// Delays for timing-dependent tests, in milliseconds. |
351 |
|
|
352 |
|
public static long SHORT_DELAY_MS; |
353 |
|
public static long SMALL_DELAY_MS; |
354 |
|
public static long MEDIUM_DELAY_MS; |
355 |
|
public static long LONG_DELAY_MS; |
356 |
|
|
248 |
– |
|
357 |
|
/** |
358 |
|
* Returns the shortest timed delay. This could |
359 |
|
* be reimplemented to use for example a Property. |
362 |
|
return 50; |
363 |
|
} |
364 |
|
|
257 |
– |
|
365 |
|
/** |
366 |
|
* Sets delays as multiples of SHORT_DELAY. |
367 |
|
*/ |
373 |
|
} |
374 |
|
|
375 |
|
/** |
376 |
+ |
* Returns a timeout in milliseconds to be used in tests that |
377 |
+ |
* verify that operations block or time out. |
378 |
+ |
*/ |
379 |
+ |
long timeoutMillis() { |
380 |
+ |
return SHORT_DELAY_MS / 4; |
381 |
+ |
} |
382 |
+ |
|
383 |
+ |
/** |
384 |
+ |
* Returns a new Date instance representing a time delayMillis |
385 |
+ |
* milliseconds in the future. |
386 |
+ |
*/ |
387 |
+ |
Date delayedDate(long delayMillis) { |
388 |
+ |
return new Date(System.currentTimeMillis() + delayMillis); |
389 |
+ |
} |
390 |
+ |
|
391 |
+ |
/** |
392 |
|
* The first exception encountered if any threadAssertXXX method fails. |
393 |
|
*/ |
394 |
|
private final AtomicReference<Throwable> threadFailure |
409 |
|
} |
410 |
|
|
411 |
|
/** |
412 |
+ |
* Extra checks that get done for all test cases. |
413 |
+ |
* |
414 |
|
* Triggers test case failure if any thread assertions have failed, |
415 |
|
* by rethrowing, in the test harness thread, any exception recorded |
416 |
|
* earlier by threadRecordFailure. |
417 |
+ |
* |
418 |
+ |
* Triggers test case failure if interrupt status is set in the main thread. |
419 |
|
*/ |
420 |
|
public void tearDown() throws Exception { |
421 |
|
Throwable t = threadFailure.getAndSet(null); |
433 |
|
throw afe; |
434 |
|
} |
435 |
|
} |
436 |
+ |
|
437 |
+ |
if (Thread.interrupted()) |
438 |
+ |
throw new AssertionFailedError("interrupt status set in main thread"); |
439 |
+ |
|
440 |
+ |
checkForkJoinPoolThreadLeaks(); |
441 |
+ |
} |
442 |
+ |
|
443 |
+ |
/** |
444 |
+ |
* Find missing try { ... } finally { joinPool(e); } |
445 |
+ |
*/ |
446 |
+ |
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
447 |
+ |
Thread[] survivors = new Thread[5]; |
448 |
+ |
int count = Thread.enumerate(survivors); |
449 |
+ |
for (int i = 0; i < count; i++) { |
450 |
+ |
Thread thread = survivors[i]; |
451 |
+ |
String name = thread.getName(); |
452 |
+ |
if (name.startsWith("ForkJoinPool-")) { |
453 |
+ |
// give thread some time to terminate |
454 |
+ |
thread.join(LONG_DELAY_MS); |
455 |
+ |
if (!thread.isAlive()) continue; |
456 |
+ |
thread.stop(); |
457 |
+ |
throw new AssertionFailedError |
458 |
+ |
(String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n", |
459 |
+ |
toString(), name)); |
460 |
+ |
} |
461 |
+ |
} |
462 |
|
} |
463 |
|
|
464 |
|
/** |
590 |
|
else { |
591 |
|
AssertionFailedError afe = |
592 |
|
new AssertionFailedError("unexpected exception: " + t); |
593 |
< |
t.initCause(t); |
593 |
> |
afe.initCause(t); |
594 |
|
throw afe; |
595 |
|
} |
596 |
|
} |
597 |
|
|
598 |
|
/** |
599 |
+ |
* Delays, via Thread.sleep, for the given millisecond delay, but |
600 |
+ |
* if the sleep is shorter than specified, may re-sleep or yield |
601 |
+ |
* until time elapses. |
602 |
+ |
*/ |
603 |
+ |
static void delay(long millis) throws InterruptedException { |
604 |
+ |
long startTime = System.nanoTime(); |
605 |
+ |
long ns = millis * 1000 * 1000; |
606 |
+ |
for (;;) { |
607 |
+ |
if (millis > 0L) |
608 |
+ |
Thread.sleep(millis); |
609 |
+ |
else // too short to sleep |
610 |
+ |
Thread.yield(); |
611 |
+ |
long d = ns - (System.nanoTime() - startTime); |
612 |
+ |
if (d > 0L) |
613 |
+ |
millis = d / (1000 * 1000); |
614 |
+ |
else |
615 |
+ |
break; |
616 |
+ |
} |
617 |
+ |
} |
618 |
+ |
|
619 |
+ |
/** |
620 |
|
* Waits out termination of a thread pool or fails doing so. |
621 |
|
*/ |
622 |
< |
public void joinPool(ExecutorService exec) { |
622 |
> |
void joinPool(ExecutorService exec) { |
623 |
|
try { |
624 |
|
exec.shutdown(); |
625 |
|
assertTrue("ExecutorService did not terminate in a timely manner", |
631 |
|
} |
632 |
|
} |
633 |
|
|
634 |
+ |
/** |
635 |
+ |
* A debugging tool to print all stack traces, as jstack does. |
636 |
+ |
*/ |
637 |
+ |
static void printAllStackTraces() { |
638 |
+ |
for (ThreadInfo info : |
639 |
+ |
ManagementFactory.getThreadMXBean() |
640 |
+ |
.dumpAllThreads(true, true)) |
641 |
+ |
System.err.print(info); |
642 |
+ |
} |
643 |
+ |
|
644 |
+ |
/** |
645 |
+ |
* Checks that thread does not terminate within the default |
646 |
+ |
* millisecond delay of {@code timeoutMillis()}. |
647 |
+ |
*/ |
648 |
+ |
void assertThreadStaysAlive(Thread thread) { |
649 |
+ |
assertThreadStaysAlive(thread, timeoutMillis()); |
650 |
+ |
} |
651 |
+ |
|
652 |
+ |
/** |
653 |
+ |
* Checks that thread does not terminate within the given millisecond delay. |
654 |
+ |
*/ |
655 |
+ |
void assertThreadStaysAlive(Thread thread, long millis) { |
656 |
+ |
try { |
657 |
+ |
// No need to optimize the failing case via Thread.join. |
658 |
+ |
delay(millis); |
659 |
+ |
assertTrue(thread.isAlive()); |
660 |
+ |
} catch (InterruptedException ie) { |
661 |
+ |
fail("Unexpected InterruptedException"); |
662 |
+ |
} |
663 |
+ |
} |
664 |
+ |
|
665 |
+ |
/** |
666 |
+ |
* Checks that the threads do not terminate within the default |
667 |
+ |
* millisecond delay of {@code timeoutMillis()}. |
668 |
+ |
*/ |
669 |
+ |
void assertThreadsStayAlive(Thread... threads) { |
670 |
+ |
assertThreadsStayAlive(timeoutMillis(), threads); |
671 |
+ |
} |
672 |
+ |
|
673 |
+ |
/** |
674 |
+ |
* Checks that the threads do not terminate within the given millisecond delay. |
675 |
+ |
*/ |
676 |
+ |
void assertThreadsStayAlive(long millis, Thread... threads) { |
677 |
+ |
try { |
678 |
+ |
// No need to optimize the failing case via Thread.join. |
679 |
+ |
delay(millis); |
680 |
+ |
for (Thread thread : threads) |
681 |
+ |
assertTrue(thread.isAlive()); |
682 |
+ |
} catch (InterruptedException ie) { |
683 |
+ |
fail("Unexpected InterruptedException"); |
684 |
+ |
} |
685 |
+ |
} |
686 |
+ |
|
687 |
+ |
/** |
688 |
+ |
* Checks that future.get times out, with the default timeout of |
689 |
+ |
* {@code timeoutMillis()}. |
690 |
+ |
*/ |
691 |
+ |
void assertFutureTimesOut(Future future) { |
692 |
+ |
assertFutureTimesOut(future, timeoutMillis()); |
693 |
+ |
} |
694 |
+ |
|
695 |
+ |
/** |
696 |
+ |
* Checks that future.get times out, with the given millisecond timeout. |
697 |
+ |
*/ |
698 |
+ |
void assertFutureTimesOut(Future future, long timeoutMillis) { |
699 |
+ |
long startTime = System.nanoTime(); |
700 |
+ |
try { |
701 |
+ |
future.get(timeoutMillis, MILLISECONDS); |
702 |
+ |
shouldThrow(); |
703 |
+ |
} catch (TimeoutException success) { |
704 |
+ |
} catch (Exception e) { |
705 |
+ |
threadUnexpectedException(e); |
706 |
+ |
} finally { future.cancel(true); } |
707 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
708 |
+ |
} |
709 |
|
|
710 |
|
/** |
711 |
|
* Fails with message "should throw exception". |
746 |
|
public static final Integer m6 = new Integer(-6); |
747 |
|
public static final Integer m10 = new Integer(-10); |
748 |
|
|
500 |
– |
|
749 |
|
/** |
750 |
|
* Runs Runnable r with a security policy that permits precisely |
751 |
|
* the specified permissions. If there is no current security |
757 |
|
SecurityManager sm = System.getSecurityManager(); |
758 |
|
if (sm == null) { |
759 |
|
r.run(); |
760 |
+ |
} |
761 |
+ |
runWithSecurityManagerWithPermissions(r, permissions); |
762 |
+ |
} |
763 |
+ |
|
764 |
+ |
/** |
765 |
+ |
* Runs Runnable r with a security policy that permits precisely |
766 |
+ |
* the specified permissions. If there is no current security |
767 |
+ |
* manager, a temporary one is set for the duration of the |
768 |
+ |
* Runnable. We require that any security manager permit |
769 |
+ |
* getPolicy/setPolicy. |
770 |
+ |
*/ |
771 |
+ |
public void runWithSecurityManagerWithPermissions(Runnable r, |
772 |
+ |
Permission... permissions) { |
773 |
+ |
SecurityManager sm = System.getSecurityManager(); |
774 |
+ |
if (sm == null) { |
775 |
|
Policy savedPolicy = Policy.getPolicy(); |
776 |
|
try { |
777 |
|
Policy.setPolicy(permissivePolicy()); |
778 |
|
System.setSecurityManager(new SecurityManager()); |
779 |
< |
runWithPermissions(r, permissions); |
779 |
> |
runWithSecurityManagerWithPermissions(r, permissions); |
780 |
|
} finally { |
781 |
|
System.setSecurityManager(null); |
782 |
|
Policy.setPolicy(savedPolicy); |
824 |
|
return perms.implies(p); |
825 |
|
} |
826 |
|
public void refresh() {} |
827 |
+ |
public String toString() { |
828 |
+ |
List<Permission> ps = new ArrayList<Permission>(); |
829 |
+ |
for (Enumeration<Permission> e = perms.elements(); e.hasMoreElements();) |
830 |
+ |
ps.add(e.nextElement()); |
831 |
+ |
return "AdjustablePolicy with permissions " + ps; |
832 |
+ |
} |
833 |
|
} |
834 |
|
|
835 |
|
/** |
857 |
|
*/ |
858 |
|
void sleep(long millis) { |
859 |
|
try { |
860 |
< |
Thread.sleep(millis); |
860 |
> |
delay(millis); |
861 |
|
} catch (InterruptedException ie) { |
862 |
|
AssertionFailedError afe = |
863 |
|
new AssertionFailedError("Unexpected InterruptedException"); |
867 |
|
} |
868 |
|
|
869 |
|
/** |
870 |
< |
* Sleeps until the timeout has elapsed, or interrupted. |
602 |
< |
* Does <em>NOT</em> throw InterruptedException. |
603 |
< |
*/ |
604 |
< |
void sleepTillInterrupted(long timeoutMillis) { |
605 |
< |
try { |
606 |
< |
Thread.sleep(timeoutMillis); |
607 |
< |
} catch (InterruptedException wakeup) {} |
608 |
< |
} |
609 |
< |
|
610 |
< |
/** |
611 |
< |
* Waits up to the specified number of milliseconds for the given |
870 |
> |
* Spin-waits up to the specified number of milliseconds for the given |
871 |
|
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
872 |
|
*/ |
873 |
|
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
874 |
< |
long timeoutNanos = timeoutMillis * 1000L * 1000L; |
616 |
< |
long t0 = System.nanoTime(); |
874 |
> |
long startTime = System.nanoTime(); |
875 |
|
for (;;) { |
876 |
|
Thread.State s = thread.getState(); |
877 |
|
if (s == Thread.State.BLOCKED || |
880 |
|
return; |
881 |
|
else if (s == Thread.State.TERMINATED) |
882 |
|
fail("Unexpected thread termination"); |
883 |
< |
else if (System.nanoTime() - t0 > timeoutNanos) { |
883 |
> |
else if (millisElapsedSince(startTime) > timeoutMillis) { |
884 |
|
threadAssertTrue(thread.isAlive()); |
885 |
|
return; |
886 |
|
} |
889 |
|
} |
890 |
|
|
891 |
|
/** |
892 |
+ |
* Waits up to LONG_DELAY_MS for the given thread to enter a wait |
893 |
+ |
* state: BLOCKED, WAITING, or TIMED_WAITING. |
894 |
+ |
*/ |
895 |
+ |
void waitForThreadToEnterWaitState(Thread thread) { |
896 |
+ |
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS); |
897 |
+ |
} |
898 |
+ |
|
899 |
+ |
/** |
900 |
|
* Returns the number of milliseconds since time given by |
901 |
|
* startNanoTime, which must have been previously returned from a |
902 |
|
* call to {@link System.nanoTime()}. |
926 |
|
} catch (InterruptedException ie) { |
927 |
|
threadUnexpectedException(ie); |
928 |
|
} finally { |
929 |
< |
if (t.isAlive()) { |
929 |
> |
if (t.getState() != Thread.State.TERMINATED) { |
930 |
|
t.interrupt(); |
931 |
|
fail("Test timed out"); |
932 |
|
} |
933 |
|
} |
934 |
|
} |
935 |
|
|
936 |
+ |
/** |
937 |
+ |
* Waits for LONG_DELAY_MS milliseconds for the thread to |
938 |
+ |
* terminate (using {@link Thread#join(long)}), else interrupts |
939 |
+ |
* the thread (in the hope that it may terminate later) and fails. |
940 |
+ |
*/ |
941 |
+ |
void awaitTermination(Thread t) { |
942 |
+ |
awaitTermination(t, LONG_DELAY_MS); |
943 |
+ |
} |
944 |
+ |
|
945 |
|
// Some convenient Runnable classes |
946 |
|
|
947 |
|
public abstract class CheckedRunnable implements Runnable { |
1004 |
|
realRun(); |
1005 |
|
threadShouldThrow("InterruptedException"); |
1006 |
|
} catch (InterruptedException success) { |
1007 |
+ |
threadAssertFalse(Thread.interrupted()); |
1008 |
|
} catch (Throwable t) { |
1009 |
|
threadUnexpectedException(t); |
1010 |
|
} |
1034 |
|
threadShouldThrow("InterruptedException"); |
1035 |
|
return result; |
1036 |
|
} catch (InterruptedException success) { |
1037 |
+ |
threadAssertFalse(Thread.interrupted()); |
1038 |
|
} catch (Throwable t) { |
1039 |
|
threadUnexpectedException(t); |
1040 |
|
} |
1066 |
|
}}; |
1067 |
|
} |
1068 |
|
|
1069 |
+ |
public Runnable awaiter(final CountDownLatch latch) { |
1070 |
+ |
return new CheckedRunnable() { |
1071 |
+ |
public void realRun() throws InterruptedException { |
1072 |
+ |
await(latch); |
1073 |
+ |
}}; |
1074 |
+ |
} |
1075 |
+ |
|
1076 |
+ |
public void await(CountDownLatch latch) { |
1077 |
+ |
try { |
1078 |
+ |
assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); |
1079 |
+ |
} catch (Throwable t) { |
1080 |
+ |
threadUnexpectedException(t); |
1081 |
+ |
} |
1082 |
+ |
} |
1083 |
+ |
|
1084 |
+ |
public void await(Semaphore semaphore) { |
1085 |
+ |
try { |
1086 |
+ |
assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
1087 |
+ |
} catch (Throwable t) { |
1088 |
+ |
threadUnexpectedException(t); |
1089 |
+ |
} |
1090 |
+ |
} |
1091 |
+ |
|
1092 |
+ |
// /** |
1093 |
+ |
// * Spin-waits up to LONG_DELAY_MS until flag becomes true. |
1094 |
+ |
// */ |
1095 |
+ |
// public void await(AtomicBoolean flag) { |
1096 |
+ |
// await(flag, LONG_DELAY_MS); |
1097 |
+ |
// } |
1098 |
+ |
|
1099 |
+ |
// /** |
1100 |
+ |
// * Spin-waits up to the specified timeout until flag becomes true. |
1101 |
+ |
// */ |
1102 |
+ |
// public void await(AtomicBoolean flag, long timeoutMillis) { |
1103 |
+ |
// long startTime = System.nanoTime(); |
1104 |
+ |
// while (!flag.get()) { |
1105 |
+ |
// if (millisElapsedSince(startTime) > timeoutMillis) |
1106 |
+ |
// throw new AssertionFailedError("timed out"); |
1107 |
+ |
// Thread.yield(); |
1108 |
+ |
// } |
1109 |
+ |
// } |
1110 |
+ |
|
1111 |
|
public static class NPETask implements Callable<String> { |
1112 |
|
public String call() { throw new NullPointerException(); } |
1113 |
|
} |
1118 |
|
|
1119 |
|
public class ShortRunnable extends CheckedRunnable { |
1120 |
|
protected void realRun() throws Throwable { |
1121 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1121 |
> |
delay(SHORT_DELAY_MS); |
1122 |
|
} |
1123 |
|
} |
1124 |
|
|
1125 |
|
public class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
1126 |
|
protected void realRun() throws InterruptedException { |
1127 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1127 |
> |
delay(SHORT_DELAY_MS); |
1128 |
|
} |
1129 |
|
} |
1130 |
|
|
1131 |
|
public class SmallRunnable extends CheckedRunnable { |
1132 |
|
protected void realRun() throws Throwable { |
1133 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1133 |
> |
delay(SMALL_DELAY_MS); |
1134 |
|
} |
1135 |
|
} |
1136 |
|
|
1137 |
|
public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
1138 |
|
protected void realRun() { |
1139 |
|
try { |
1140 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1140 |
> |
delay(SMALL_DELAY_MS); |
1141 |
|
} catch (InterruptedException ok) {} |
1142 |
|
} |
1143 |
|
} |
1144 |
|
|
1145 |
|
public class SmallCallable extends CheckedCallable { |
1146 |
|
protected Object realCall() throws InterruptedException { |
1147 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1147 |
> |
delay(SMALL_DELAY_MS); |
1148 |
|
return Boolean.TRUE; |
1149 |
|
} |
1150 |
|
} |
1151 |
|
|
1152 |
|
public class MediumRunnable extends CheckedRunnable { |
1153 |
|
protected void realRun() throws Throwable { |
1154 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1154 |
> |
delay(MEDIUM_DELAY_MS); |
1155 |
|
} |
1156 |
|
} |
1157 |
|
|
1158 |
|
public class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
1159 |
|
protected void realRun() throws InterruptedException { |
1160 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1160 |
> |
delay(MEDIUM_DELAY_MS); |
1161 |
|
} |
1162 |
|
} |
1163 |
|
|
1165 |
|
return new CheckedRunnable() { |
1166 |
|
protected void realRun() { |
1167 |
|
try { |
1168 |
< |
Thread.sleep(timeoutMillis); |
1168 |
> |
delay(timeoutMillis); |
1169 |
|
} catch (InterruptedException ok) {} |
1170 |
|
}}; |
1171 |
|
} |
1173 |
|
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
1174 |
|
protected void realRun() { |
1175 |
|
try { |
1176 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1176 |
> |
delay(MEDIUM_DELAY_MS); |
1177 |
|
} catch (InterruptedException ok) {} |
1178 |
|
} |
1179 |
|
} |
1181 |
|
public class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
1182 |
|
protected void realRun() { |
1183 |
|
try { |
1184 |
< |
Thread.sleep(LONG_DELAY_MS); |
1184 |
> |
delay(LONG_DELAY_MS); |
1185 |
|
} catch (InterruptedException ok) {} |
1186 |
|
} |
1187 |
|
} |
1205 |
|
public boolean isDone() { return done; } |
1206 |
|
public void run() { |
1207 |
|
try { |
1208 |
< |
Thread.sleep(timeoutMillis); |
1208 |
> |
delay(timeoutMillis); |
1209 |
|
done = true; |
1210 |
|
} catch (InterruptedException ok) {} |
1211 |
|
} |
1216 |
|
public volatile boolean done = false; |
1217 |
|
public void run() { |
1218 |
|
try { |
1219 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1219 |
> |
delay(SHORT_DELAY_MS); |
1220 |
|
done = true; |
1221 |
|
} catch (InterruptedException ok) {} |
1222 |
|
} |
1226 |
|
public volatile boolean done = false; |
1227 |
|
public void run() { |
1228 |
|
try { |
1229 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1229 |
> |
delay(SMALL_DELAY_MS); |
1230 |
|
done = true; |
1231 |
|
} catch (InterruptedException ok) {} |
1232 |
|
} |
1236 |
|
public volatile boolean done = false; |
1237 |
|
public void run() { |
1238 |
|
try { |
1239 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1239 |
> |
delay(MEDIUM_DELAY_MS); |
1240 |
|
done = true; |
1241 |
|
} catch (InterruptedException ok) {} |
1242 |
|
} |
1246 |
|
public volatile boolean done = false; |
1247 |
|
public void run() { |
1248 |
|
try { |
1249 |
< |
Thread.sleep(LONG_DELAY_MS); |
1249 |
> |
delay(LONG_DELAY_MS); |
1250 |
|
done = true; |
1251 |
|
} catch (InterruptedException ok) {} |
1252 |
|
} |
1263 |
|
public volatile boolean done = false; |
1264 |
|
public Object call() { |
1265 |
|
try { |
1266 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1266 |
> |
delay(SMALL_DELAY_MS); |
1267 |
|
done = true; |
1268 |
|
} catch (InterruptedException ok) {} |
1269 |
|
return Boolean.TRUE; |
1276 |
|
public abstract class CheckedRecursiveAction extends RecursiveAction { |
1277 |
|
protected abstract void realCompute() throws Throwable; |
1278 |
|
|
1279 |
< |
public final void compute() { |
1279 |
> |
@Override protected final void compute() { |
1280 |
|
try { |
1281 |
|
realCompute(); |
1282 |
|
} catch (Throwable t) { |
1291 |
|
public abstract class CheckedRecursiveTask<T> extends RecursiveTask<T> { |
1292 |
|
protected abstract T realCompute() throws Throwable; |
1293 |
|
|
1294 |
< |
public final T compute() { |
1294 |
> |
@Override protected final T compute() { |
1295 |
|
try { |
1296 |
|
return realCompute(); |
1297 |
|
} catch (Throwable t) { |
1310 |
|
} |
1311 |
|
|
1312 |
|
/** |
1313 |
< |
* A CyclicBarrier that fails with AssertionFailedErrors instead |
1314 |
< |
* of throwing checked exceptions. |
1313 |
> |
* A CyclicBarrier that uses timed await and fails with |
1314 |
> |
* AssertionFailedErrors instead of throwing checked exceptions. |
1315 |
|
*/ |
1316 |
|
public class CheckedBarrier extends CyclicBarrier { |
1317 |
|
public CheckedBarrier(int parties) { super(parties); } |
1318 |
|
|
1319 |
|
public int await() { |
1320 |
|
try { |
1321 |
< |
return super.await(); |
1321 |
> |
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1322 |
> |
} catch (TimeoutException e) { |
1323 |
> |
throw new AssertionFailedError("timed out"); |
1324 |
|
} catch (Exception e) { |
1325 |
|
AssertionFailedError afe = |
1326 |
|
new AssertionFailedError("Unexpected exception: " + e); |
1330 |
|
} |
1331 |
|
} |
1332 |
|
|
1333 |
< |
public void checkEmpty(BlockingQueue q) { |
1333 |
> |
void checkEmpty(BlockingQueue q) { |
1334 |
|
try { |
1335 |
|
assertTrue(q.isEmpty()); |
1336 |
|
assertEquals(0, q.size()); |
1357 |
|
} |
1358 |
|
} |
1359 |
|
|
1360 |
+ |
void assertSerialEquals(Object x, Object y) { |
1361 |
+ |
assertTrue(Arrays.equals(serialBytes(x), serialBytes(y))); |
1362 |
+ |
} |
1363 |
+ |
|
1364 |
+ |
void assertNotSerialEquals(Object x, Object y) { |
1365 |
+ |
assertFalse(Arrays.equals(serialBytes(x), serialBytes(y))); |
1366 |
+ |
} |
1367 |
+ |
|
1368 |
+ |
byte[] serialBytes(Object o) { |
1369 |
+ |
try { |
1370 |
+ |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1371 |
+ |
ObjectOutputStream oos = new ObjectOutputStream(bos); |
1372 |
+ |
oos.writeObject(o); |
1373 |
+ |
oos.flush(); |
1374 |
+ |
oos.close(); |
1375 |
+ |
return bos.toByteArray(); |
1376 |
+ |
} catch (Throwable t) { |
1377 |
+ |
threadUnexpectedException(t); |
1378 |
+ |
return new byte[0]; |
1379 |
+ |
} |
1380 |
+ |
} |
1381 |
+ |
|
1382 |
+ |
@SuppressWarnings("unchecked") |
1383 |
+ |
<T> T serialClone(T o) { |
1384 |
+ |
try { |
1385 |
+ |
ObjectInputStream ois = new ObjectInputStream |
1386 |
+ |
(new ByteArrayInputStream(serialBytes(o))); |
1387 |
+ |
T clone = (T) ois.readObject(); |
1388 |
+ |
assertSame(o.getClass(), clone.getClass()); |
1389 |
+ |
return clone; |
1390 |
+ |
} catch (Throwable t) { |
1391 |
+ |
threadUnexpectedException(t); |
1392 |
+ |
return null; |
1393 |
+ |
} |
1394 |
+ |
} |
1395 |
+ |
|
1396 |
+ |
public void assertThrows(Class<? extends Throwable> expectedExceptionClass, |
1397 |
+ |
Runnable... throwingActions) { |
1398 |
+ |
for (Runnable throwingAction : throwingActions) { |
1399 |
+ |
boolean threw = false; |
1400 |
+ |
try { throwingAction.run(); } |
1401 |
+ |
catch (Throwable t) { |
1402 |
+ |
threw = true; |
1403 |
+ |
if (!expectedExceptionClass.isInstance(t)) { |
1404 |
+ |
AssertionFailedError afe = |
1405 |
+ |
new AssertionFailedError |
1406 |
+ |
("Expected " + expectedExceptionClass.getName() + |
1407 |
+ |
", got " + t.getClass().getName()); |
1408 |
+ |
afe.initCause(t); |
1409 |
+ |
threadUnexpectedException(afe); |
1410 |
+ |
} |
1411 |
+ |
} |
1412 |
+ |
if (!threw) |
1413 |
+ |
shouldThrow(expectedExceptionClass.getName()); |
1414 |
+ |
} |
1415 |
+ |
} |
1416 |
|
} |