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.AtomicReference; |
26 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
27 |
|
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
28 |
+ |
import java.util.regex.Pattern; |
29 |
|
import java.security.CodeSource; |
30 |
|
import java.security.Permission; |
31 |
|
import java.security.PermissionCollection; |
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 |
115 |
|
Boolean.getBoolean("jsr166.expensiveTests"); |
116 |
|
|
117 |
|
/** |
118 |
+ |
* If true, also run tests that are not part of the official tck |
119 |
+ |
* because they test unspecified implementation details. |
120 |
+ |
*/ |
121 |
+ |
protected static final boolean testImplementationDetails = |
122 |
+ |
Boolean.getBoolean("jsr166.testImplementationDetails"); |
123 |
+ |
|
124 |
+ |
/** |
125 |
|
* If true, report on stdout all "slow" tests, that is, ones that |
126 |
|
* take more than profileThreshold milliseconds to execute. |
127 |
|
*/ |
135 |
|
private static final long profileThreshold = |
136 |
|
Long.getLong("jsr166.profileThreshold", 100); |
137 |
|
|
138 |
+ |
/** |
139 |
+ |
* The number of repetitions per test (for tickling rare bugs). |
140 |
+ |
*/ |
141 |
+ |
private static final int runsPerTest = |
142 |
+ |
Integer.getInteger("jsr166.runsPerTest", 1); |
143 |
+ |
|
144 |
+ |
/** |
145 |
+ |
* A filter for tests to run, matching strings of the form |
146 |
+ |
* methodName(className), e.g. "testInvokeAll5(ForkJoinPoolTest)" |
147 |
+ |
* Usefully combined with jsr166.runsPerTest. |
148 |
+ |
*/ |
149 |
+ |
private static final Pattern methodFilter = methodFilter(); |
150 |
+ |
|
151 |
+ |
private static Pattern methodFilter() { |
152 |
+ |
String regex = System.getProperty("jsr166.methodFilter"); |
153 |
+ |
return (regex == null) ? null : Pattern.compile(regex); |
154 |
+ |
} |
155 |
+ |
|
156 |
|
protected void runTest() throws Throwable { |
157 |
< |
if (profileTests) |
158 |
< |
runTestProfiled(); |
159 |
< |
else |
160 |
< |
super.runTest(); |
157 |
> |
if (methodFilter == null |
158 |
> |
|| methodFilter.matcher(toString()).find()) { |
159 |
> |
for (int i = 0; i < runsPerTest; i++) { |
160 |
> |
if (profileTests) |
161 |
> |
runTestProfiled(); |
162 |
> |
else |
163 |
> |
super.runTest(); |
164 |
> |
} |
165 |
> |
} |
166 |
|
} |
167 |
|
|
168 |
|
protected void runTestProfiled() throws Throwable { |
169 |
+ |
// Warmup run, notably to trigger all needed classloading. |
170 |
+ |
super.runTest(); |
171 |
|
long t0 = System.nanoTime(); |
172 |
|
try { |
173 |
|
super.runTest(); |
174 |
|
} finally { |
175 |
< |
long elapsedMillis = |
132 |
< |
(System.nanoTime() - t0) / (1000L * 1000L); |
175 |
> |
long elapsedMillis = millisElapsedSince(t0); |
176 |
|
if (elapsedMillis >= profileThreshold) |
177 |
|
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); |
178 |
|
} |
179 |
|
} |
180 |
|
|
181 |
|
/** |
182 |
< |
* Runs all JSR166 unit tests using junit.textui.TestRunner |
182 |
> |
* Runs all JSR166 unit tests using junit.textui.TestRunner. |
183 |
> |
* Optional command line arg provides the number of iterations to |
184 |
> |
* repeat running the tests. |
185 |
|
*/ |
186 |
|
public static void main(String[] args) { |
187 |
|
if (useSecurityManager) { |
213 |
|
return suite; |
214 |
|
} |
215 |
|
|
216 |
+ |
public static void addNamedTestClasses(TestSuite suite, |
217 |
+ |
String... testClassNames) { |
218 |
+ |
for (String testClassName : testClassNames) { |
219 |
+ |
try { |
220 |
+ |
Class<?> testClass = Class.forName(testClassName); |
221 |
+ |
Method m = testClass.getDeclaredMethod("suite", |
222 |
+ |
new Class<?>[0]); |
223 |
+ |
suite.addTest(newTestSuite((Test)m.invoke(null))); |
224 |
+ |
} catch (Exception e) { |
225 |
+ |
throw new Error("Missing test class", e); |
226 |
+ |
} |
227 |
+ |
} |
228 |
+ |
} |
229 |
+ |
|
230 |
+ |
public static final double JAVA_CLASS_VERSION; |
231 |
+ |
public static final String JAVA_SPECIFICATION_VERSION; |
232 |
+ |
static { |
233 |
+ |
try { |
234 |
+ |
JAVA_CLASS_VERSION = java.security.AccessController.doPrivileged( |
235 |
+ |
new java.security.PrivilegedAction<Double>() { |
236 |
+ |
public Double run() { |
237 |
+ |
return Double.valueOf(System.getProperty("java.class.version"));}}); |
238 |
+ |
JAVA_SPECIFICATION_VERSION = java.security.AccessController.doPrivileged( |
239 |
+ |
new java.security.PrivilegedAction<String>() { |
240 |
+ |
public String run() { |
241 |
+ |
return System.getProperty("java.specification.version");}}); |
242 |
+ |
} catch (Throwable t) { |
243 |
+ |
throw new Error(t); |
244 |
+ |
} |
245 |
+ |
} |
246 |
+ |
|
247 |
+ |
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
248 |
+ |
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
249 |
+ |
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
250 |
+ |
public static boolean atLeastJava9() { |
251 |
+ |
// As of 2014-05, java9 still uses 52.0 class file version |
252 |
+ |
return JAVA_SPECIFICATION_VERSION.startsWith("1.9"); |
253 |
+ |
} |
254 |
+ |
|
255 |
|
/** |
256 |
|
* Collects all JSR166 unit tests as one suite. |
257 |
|
*/ |
258 |
|
public static Test suite() { |
259 |
< |
return newTestSuite( |
259 |
> |
// Java7+ test classes |
260 |
> |
TestSuite suite = newTestSuite( |
261 |
|
ForkJoinPoolTest.suite(), |
262 |
|
ForkJoinTaskTest.suite(), |
263 |
|
RecursiveActionTest.suite(), |
322 |
|
TreeSetTest.suite(), |
323 |
|
TreeSubMapTest.suite(), |
324 |
|
TreeSubSetTest.suite()); |
325 |
+ |
|
326 |
+ |
// Java8+ test classes |
327 |
+ |
if (atLeastJava8()) { |
328 |
+ |
String[] java8TestClassNames = { |
329 |
+ |
"Atomic8Test", |
330 |
+ |
"CompletableFutureTest", |
331 |
+ |
"ConcurrentHashMap8Test", |
332 |
+ |
"CountedCompleterTest", |
333 |
+ |
"DoubleAccumulatorTest", |
334 |
+ |
"DoubleAdderTest", |
335 |
+ |
"ForkJoinPool8Test", |
336 |
+ |
"ForkJoinTask8Test", |
337 |
+ |
"LongAccumulatorTest", |
338 |
+ |
"LongAdderTest", |
339 |
+ |
"SplittableRandomTest", |
340 |
+ |
"StampedLockTest", |
341 |
+ |
"ThreadLocalRandom8Test", |
342 |
+ |
}; |
343 |
+ |
addNamedTestClasses(suite, java8TestClassNames); |
344 |
+ |
} |
345 |
+ |
|
346 |
+ |
// Java9+ test classes |
347 |
+ |
if (atLeastJava9()) { |
348 |
+ |
String[] java9TestClassNames = { |
349 |
+ |
"ThreadPoolExecutor9Test", |
350 |
+ |
}; |
351 |
+ |
addNamedTestClasses(suite, java9TestClassNames); |
352 |
+ |
} |
353 |
+ |
|
354 |
+ |
return suite; |
355 |
|
} |
356 |
|
|
357 |
+ |
// Delays for timing-dependent tests, in milliseconds. |
358 |
|
|
359 |
|
public static long SHORT_DELAY_MS; |
360 |
|
public static long SMALL_DELAY_MS; |
361 |
|
public static long MEDIUM_DELAY_MS; |
362 |
|
public static long LONG_DELAY_MS; |
363 |
|
|
248 |
– |
|
364 |
|
/** |
365 |
|
* Returns the shortest timed delay. This could |
366 |
|
* be reimplemented to use for example a Property. |
369 |
|
return 50; |
370 |
|
} |
371 |
|
|
257 |
– |
|
372 |
|
/** |
373 |
|
* Sets delays as multiples of SHORT_DELAY. |
374 |
|
*/ |
380 |
|
} |
381 |
|
|
382 |
|
/** |
383 |
+ |
* Returns a timeout in milliseconds to be used in tests that |
384 |
+ |
* verify that operations block or time out. |
385 |
+ |
*/ |
386 |
+ |
long timeoutMillis() { |
387 |
+ |
return SHORT_DELAY_MS / 4; |
388 |
+ |
} |
389 |
+ |
|
390 |
+ |
/** |
391 |
+ |
* Returns a new Date instance representing a time delayMillis |
392 |
+ |
* milliseconds in the future. |
393 |
+ |
*/ |
394 |
+ |
Date delayedDate(long delayMillis) { |
395 |
+ |
return new Date(System.currentTimeMillis() + delayMillis); |
396 |
+ |
} |
397 |
+ |
|
398 |
+ |
/** |
399 |
|
* The first exception encountered if any threadAssertXXX method fails. |
400 |
|
*/ |
401 |
|
private final AtomicReference<Throwable> threadFailure |
416 |
|
} |
417 |
|
|
418 |
|
/** |
419 |
+ |
* Extra checks that get done for all test cases. |
420 |
+ |
* |
421 |
|
* Triggers test case failure if any thread assertions have failed, |
422 |
|
* by rethrowing, in the test harness thread, any exception recorded |
423 |
|
* earlier by threadRecordFailure. |
424 |
+ |
* |
425 |
+ |
* Triggers test case failure if interrupt status is set in the main thread. |
426 |
|
*/ |
427 |
|
public void tearDown() throws Exception { |
428 |
|
Throwable t = threadFailure.getAndSet(null); |
440 |
|
throw afe; |
441 |
|
} |
442 |
|
} |
443 |
+ |
|
444 |
+ |
if (Thread.interrupted()) |
445 |
+ |
throw new AssertionFailedError("interrupt status set in main thread"); |
446 |
+ |
|
447 |
+ |
checkForkJoinPoolThreadLeaks(); |
448 |
+ |
} |
449 |
+ |
|
450 |
+ |
/** |
451 |
+ |
* Find missing try { ... } finally { joinPool(e); } |
452 |
+ |
*/ |
453 |
+ |
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
454 |
+ |
Thread[] survivors = new Thread[5]; |
455 |
+ |
int count = Thread.enumerate(survivors); |
456 |
+ |
for (int i = 0; i < count; i++) { |
457 |
+ |
Thread thread = survivors[i]; |
458 |
+ |
String name = thread.getName(); |
459 |
+ |
if (name.startsWith("ForkJoinPool-")) { |
460 |
+ |
// give thread some time to terminate |
461 |
+ |
thread.join(LONG_DELAY_MS); |
462 |
+ |
if (!thread.isAlive()) continue; |
463 |
+ |
thread.stop(); |
464 |
+ |
throw new AssertionFailedError |
465 |
+ |
(String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n", |
466 |
+ |
toString(), name)); |
467 |
+ |
} |
468 |
+ |
} |
469 |
|
} |
470 |
|
|
471 |
|
/** |
597 |
|
else { |
598 |
|
AssertionFailedError afe = |
599 |
|
new AssertionFailedError("unexpected exception: " + t); |
600 |
< |
t.initCause(t); |
600 |
> |
afe.initCause(t); |
601 |
|
throw afe; |
602 |
|
} |
603 |
|
} |
604 |
|
|
605 |
|
/** |
606 |
+ |
* Delays, via Thread.sleep, for the given millisecond delay, but |
607 |
+ |
* if the sleep is shorter than specified, may re-sleep or yield |
608 |
+ |
* until time elapses. |
609 |
+ |
*/ |
610 |
+ |
static void delay(long millis) throws InterruptedException { |
611 |
+ |
long startTime = System.nanoTime(); |
612 |
+ |
long ns = millis * 1000 * 1000; |
613 |
+ |
for (;;) { |
614 |
+ |
if (millis > 0L) |
615 |
+ |
Thread.sleep(millis); |
616 |
+ |
else // too short to sleep |
617 |
+ |
Thread.yield(); |
618 |
+ |
long d = ns - (System.nanoTime() - startTime); |
619 |
+ |
if (d > 0L) |
620 |
+ |
millis = d / (1000 * 1000); |
621 |
+ |
else |
622 |
+ |
break; |
623 |
+ |
} |
624 |
+ |
} |
625 |
+ |
|
626 |
+ |
/** |
627 |
|
* Waits out termination of a thread pool or fails doing so. |
628 |
|
*/ |
629 |
< |
public void joinPool(ExecutorService exec) { |
629 |
> |
void joinPool(ExecutorService exec) { |
630 |
|
try { |
631 |
|
exec.shutdown(); |
632 |
< |
assertTrue("ExecutorService did not terminate in a timely manner", |
633 |
< |
exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)); |
632 |
> |
if (!exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) |
633 |
> |
fail("ExecutorService " + exec + |
634 |
> |
" did not terminate in a timely manner"); |
635 |
|
} catch (SecurityException ok) { |
636 |
|
// Allowed in case test doesn't have privs |
637 |
|
} catch (InterruptedException ie) { |
639 |
|
} |
640 |
|
} |
641 |
|
|
642 |
+ |
/** |
643 |
+ |
* A debugging tool to print all stack traces, as jstack does. |
644 |
+ |
*/ |
645 |
+ |
static void printAllStackTraces() { |
646 |
+ |
for (ThreadInfo info : |
647 |
+ |
ManagementFactory.getThreadMXBean() |
648 |
+ |
.dumpAllThreads(true, true)) |
649 |
+ |
System.err.print(info); |
650 |
+ |
} |
651 |
+ |
|
652 |
+ |
/** |
653 |
+ |
* Checks that thread does not terminate within the default |
654 |
+ |
* millisecond delay of {@code timeoutMillis()}. |
655 |
+ |
*/ |
656 |
+ |
void assertThreadStaysAlive(Thread thread) { |
657 |
+ |
assertThreadStaysAlive(thread, timeoutMillis()); |
658 |
+ |
} |
659 |
+ |
|
660 |
+ |
/** |
661 |
+ |
* Checks that thread does not terminate within the given millisecond delay. |
662 |
+ |
*/ |
663 |
+ |
void assertThreadStaysAlive(Thread thread, long millis) { |
664 |
+ |
try { |
665 |
+ |
// No need to optimize the failing case via Thread.join. |
666 |
+ |
delay(millis); |
667 |
+ |
assertTrue(thread.isAlive()); |
668 |
+ |
} catch (InterruptedException ie) { |
669 |
+ |
fail("Unexpected InterruptedException"); |
670 |
+ |
} |
671 |
+ |
} |
672 |
+ |
|
673 |
+ |
/** |
674 |
+ |
* Checks that the threads do not terminate within the default |
675 |
+ |
* millisecond delay of {@code timeoutMillis()}. |
676 |
+ |
*/ |
677 |
+ |
void assertThreadsStayAlive(Thread... threads) { |
678 |
+ |
assertThreadsStayAlive(timeoutMillis(), threads); |
679 |
+ |
} |
680 |
+ |
|
681 |
+ |
/** |
682 |
+ |
* Checks that the threads do not terminate within the given millisecond delay. |
683 |
+ |
*/ |
684 |
+ |
void assertThreadsStayAlive(long millis, Thread... threads) { |
685 |
+ |
try { |
686 |
+ |
// No need to optimize the failing case via Thread.join. |
687 |
+ |
delay(millis); |
688 |
+ |
for (Thread thread : threads) |
689 |
+ |
assertTrue(thread.isAlive()); |
690 |
+ |
} catch (InterruptedException ie) { |
691 |
+ |
fail("Unexpected InterruptedException"); |
692 |
+ |
} |
693 |
+ |
} |
694 |
+ |
|
695 |
+ |
/** |
696 |
+ |
* Checks that future.get times out, with the default timeout of |
697 |
+ |
* {@code timeoutMillis()}. |
698 |
+ |
*/ |
699 |
+ |
void assertFutureTimesOut(Future future) { |
700 |
+ |
assertFutureTimesOut(future, timeoutMillis()); |
701 |
+ |
} |
702 |
+ |
|
703 |
+ |
/** |
704 |
+ |
* Checks that future.get times out, with the given millisecond timeout. |
705 |
+ |
*/ |
706 |
+ |
void assertFutureTimesOut(Future future, long timeoutMillis) { |
707 |
+ |
long startTime = System.nanoTime(); |
708 |
+ |
try { |
709 |
+ |
future.get(timeoutMillis, MILLISECONDS); |
710 |
+ |
shouldThrow(); |
711 |
+ |
} catch (TimeoutException success) { |
712 |
+ |
} catch (Exception e) { |
713 |
+ |
threadUnexpectedException(e); |
714 |
+ |
} finally { future.cancel(true); } |
715 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
716 |
+ |
} |
717 |
|
|
718 |
|
/** |
719 |
|
* Fails with message "should throw exception". |
754 |
|
public static final Integer m6 = new Integer(-6); |
755 |
|
public static final Integer m10 = new Integer(-10); |
756 |
|
|
500 |
– |
|
757 |
|
/** |
758 |
|
* Runs Runnable r with a security policy that permits precisely |
759 |
|
* the specified permissions. If there is no current security |
765 |
|
SecurityManager sm = System.getSecurityManager(); |
766 |
|
if (sm == null) { |
767 |
|
r.run(); |
768 |
+ |
} |
769 |
+ |
runWithSecurityManagerWithPermissions(r, permissions); |
770 |
+ |
} |
771 |
+ |
|
772 |
+ |
/** |
773 |
+ |
* Runs Runnable r with a security policy that permits precisely |
774 |
+ |
* the specified permissions. If there is no current security |
775 |
+ |
* manager, a temporary one is set for the duration of the |
776 |
+ |
* Runnable. We require that any security manager permit |
777 |
+ |
* getPolicy/setPolicy. |
778 |
+ |
*/ |
779 |
+ |
public void runWithSecurityManagerWithPermissions(Runnable r, |
780 |
+ |
Permission... permissions) { |
781 |
+ |
SecurityManager sm = System.getSecurityManager(); |
782 |
+ |
if (sm == null) { |
783 |
|
Policy savedPolicy = Policy.getPolicy(); |
784 |
|
try { |
785 |
|
Policy.setPolicy(permissivePolicy()); |
786 |
|
System.setSecurityManager(new SecurityManager()); |
787 |
< |
runWithPermissions(r, permissions); |
787 |
> |
runWithSecurityManagerWithPermissions(r, permissions); |
788 |
|
} finally { |
789 |
|
System.setSecurityManager(null); |
790 |
|
Policy.setPolicy(savedPolicy); |
832 |
|
return perms.implies(p); |
833 |
|
} |
834 |
|
public void refresh() {} |
835 |
+ |
public String toString() { |
836 |
+ |
List<Permission> ps = new ArrayList<Permission>(); |
837 |
+ |
for (Enumeration<Permission> e = perms.elements(); e.hasMoreElements();) |
838 |
+ |
ps.add(e.nextElement()); |
839 |
+ |
return "AdjustablePolicy with permissions " + ps; |
840 |
+ |
} |
841 |
|
} |
842 |
|
|
843 |
|
/** |
865 |
|
*/ |
866 |
|
void sleep(long millis) { |
867 |
|
try { |
868 |
< |
Thread.sleep(millis); |
868 |
> |
delay(millis); |
869 |
|
} catch (InterruptedException ie) { |
870 |
|
AssertionFailedError afe = |
871 |
|
new AssertionFailedError("Unexpected InterruptedException"); |
875 |
|
} |
876 |
|
|
877 |
|
/** |
878 |
< |
* 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 |
878 |
> |
* Spin-waits up to the specified number of milliseconds for the given |
879 |
|
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
880 |
|
*/ |
881 |
|
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
882 |
< |
long timeoutNanos = timeoutMillis * 1000L * 1000L; |
616 |
< |
long t0 = System.nanoTime(); |
882 |
> |
long startTime = System.nanoTime(); |
883 |
|
for (;;) { |
884 |
|
Thread.State s = thread.getState(); |
885 |
|
if (s == Thread.State.BLOCKED || |
888 |
|
return; |
889 |
|
else if (s == Thread.State.TERMINATED) |
890 |
|
fail("Unexpected thread termination"); |
891 |
< |
else if (System.nanoTime() - t0 > timeoutNanos) { |
891 |
> |
else if (millisElapsedSince(startTime) > timeoutMillis) { |
892 |
|
threadAssertTrue(thread.isAlive()); |
893 |
|
return; |
894 |
|
} |
897 |
|
} |
898 |
|
|
899 |
|
/** |
900 |
+ |
* Waits up to LONG_DELAY_MS for the given thread to enter a wait |
901 |
+ |
* state: BLOCKED, WAITING, or TIMED_WAITING. |
902 |
+ |
*/ |
903 |
+ |
void waitForThreadToEnterWaitState(Thread thread) { |
904 |
+ |
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS); |
905 |
+ |
} |
906 |
+ |
|
907 |
+ |
/** |
908 |
|
* Returns the number of milliseconds since time given by |
909 |
|
* startNanoTime, which must have been previously returned from a |
910 |
|
* call to {@link System.nanoTime()}. |
911 |
|
*/ |
912 |
< |
long millisElapsedSince(long startNanoTime) { |
912 |
> |
static long millisElapsedSince(long startNanoTime) { |
913 |
|
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
914 |
|
} |
915 |
|
|
916 |
+ |
// void assertTerminatesPromptly(long timeoutMillis, Runnable r) { |
917 |
+ |
// long startTime = System.nanoTime(); |
918 |
+ |
// try { |
919 |
+ |
// r.run(); |
920 |
+ |
// } catch (Throwable fail) { threadUnexpectedException(fail); } |
921 |
+ |
// if (millisElapsedSince(startTime) > timeoutMillis/2) |
922 |
+ |
// throw new AssertionFailedError("did not return promptly"); |
923 |
+ |
// } |
924 |
+ |
|
925 |
+ |
// void assertTerminatesPromptly(Runnable r) { |
926 |
+ |
// assertTerminatesPromptly(LONG_DELAY_MS/2, r); |
927 |
+ |
// } |
928 |
+ |
|
929 |
+ |
/** |
930 |
+ |
* Checks that timed f.get() returns the expected value, and does not |
931 |
+ |
* wait for the timeout to elapse before returning. |
932 |
+ |
*/ |
933 |
+ |
<T> void checkTimedGet(Future<T> f, T expectedValue, long timeoutMillis) { |
934 |
+ |
long startTime = System.nanoTime(); |
935 |
+ |
try { |
936 |
+ |
assertEquals(expectedValue, f.get(timeoutMillis, MILLISECONDS)); |
937 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
938 |
+ |
if (millisElapsedSince(startTime) > timeoutMillis/2) |
939 |
+ |
throw new AssertionFailedError("timed get did not return promptly"); |
940 |
+ |
} |
941 |
+ |
|
942 |
+ |
<T> void checkTimedGet(Future<T> f, T expectedValue) { |
943 |
+ |
checkTimedGet(f, expectedValue, LONG_DELAY_MS); |
944 |
+ |
} |
945 |
+ |
|
946 |
|
/** |
947 |
|
* Returns a new started daemon Thread running the given runnable. |
948 |
|
*/ |
964 |
|
} catch (InterruptedException ie) { |
965 |
|
threadUnexpectedException(ie); |
966 |
|
} finally { |
967 |
< |
if (t.isAlive()) { |
967 |
> |
if (t.getState() != Thread.State.TERMINATED) { |
968 |
|
t.interrupt(); |
969 |
|
fail("Test timed out"); |
970 |
|
} |
971 |
|
} |
972 |
|
} |
973 |
|
|
974 |
+ |
/** |
975 |
+ |
* Waits for LONG_DELAY_MS milliseconds for the thread to |
976 |
+ |
* terminate (using {@link Thread#join(long)}), else interrupts |
977 |
+ |
* the thread (in the hope that it may terminate later) and fails. |
978 |
+ |
*/ |
979 |
+ |
void awaitTermination(Thread t) { |
980 |
+ |
awaitTermination(t, LONG_DELAY_MS); |
981 |
+ |
} |
982 |
+ |
|
983 |
|
// Some convenient Runnable classes |
984 |
|
|
985 |
|
public abstract class CheckedRunnable implements Runnable { |
1042 |
|
realRun(); |
1043 |
|
threadShouldThrow("InterruptedException"); |
1044 |
|
} catch (InterruptedException success) { |
1045 |
+ |
threadAssertFalse(Thread.interrupted()); |
1046 |
|
} catch (Throwable t) { |
1047 |
|
threadUnexpectedException(t); |
1048 |
|
} |
1072 |
|
threadShouldThrow("InterruptedException"); |
1073 |
|
return result; |
1074 |
|
} catch (InterruptedException success) { |
1075 |
+ |
threadAssertFalse(Thread.interrupted()); |
1076 |
|
} catch (Throwable t) { |
1077 |
|
threadUnexpectedException(t); |
1078 |
|
} |
1104 |
|
}}; |
1105 |
|
} |
1106 |
|
|
1107 |
+ |
public Runnable awaiter(final CountDownLatch latch) { |
1108 |
+ |
return new CheckedRunnable() { |
1109 |
+ |
public void realRun() throws InterruptedException { |
1110 |
+ |
await(latch); |
1111 |
+ |
}}; |
1112 |
+ |
} |
1113 |
+ |
|
1114 |
+ |
public void await(CountDownLatch latch) { |
1115 |
+ |
try { |
1116 |
+ |
assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); |
1117 |
+ |
} catch (Throwable t) { |
1118 |
+ |
threadUnexpectedException(t); |
1119 |
+ |
} |
1120 |
+ |
} |
1121 |
+ |
|
1122 |
+ |
public void await(Semaphore semaphore) { |
1123 |
+ |
try { |
1124 |
+ |
assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
1125 |
+ |
} catch (Throwable t) { |
1126 |
+ |
threadUnexpectedException(t); |
1127 |
+ |
} |
1128 |
+ |
} |
1129 |
+ |
|
1130 |
+ |
// /** |
1131 |
+ |
// * Spin-waits up to LONG_DELAY_MS until flag becomes true. |
1132 |
+ |
// */ |
1133 |
+ |
// public void await(AtomicBoolean flag) { |
1134 |
+ |
// await(flag, LONG_DELAY_MS); |
1135 |
+ |
// } |
1136 |
+ |
|
1137 |
+ |
// /** |
1138 |
+ |
// * Spin-waits up to the specified timeout until flag becomes true. |
1139 |
+ |
// */ |
1140 |
+ |
// public void await(AtomicBoolean flag, long timeoutMillis) { |
1141 |
+ |
// long startTime = System.nanoTime(); |
1142 |
+ |
// while (!flag.get()) { |
1143 |
+ |
// if (millisElapsedSince(startTime) > timeoutMillis) |
1144 |
+ |
// throw new AssertionFailedError("timed out"); |
1145 |
+ |
// Thread.yield(); |
1146 |
+ |
// } |
1147 |
+ |
// } |
1148 |
+ |
|
1149 |
|
public static class NPETask implements Callable<String> { |
1150 |
|
public String call() { throw new NullPointerException(); } |
1151 |
|
} |
1156 |
|
|
1157 |
|
public class ShortRunnable extends CheckedRunnable { |
1158 |
|
protected void realRun() throws Throwable { |
1159 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1159 |
> |
delay(SHORT_DELAY_MS); |
1160 |
|
} |
1161 |
|
} |
1162 |
|
|
1163 |
|
public class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
1164 |
|
protected void realRun() throws InterruptedException { |
1165 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1165 |
> |
delay(SHORT_DELAY_MS); |
1166 |
|
} |
1167 |
|
} |
1168 |
|
|
1169 |
|
public class SmallRunnable extends CheckedRunnable { |
1170 |
|
protected void realRun() throws Throwable { |
1171 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1171 |
> |
delay(SMALL_DELAY_MS); |
1172 |
|
} |
1173 |
|
} |
1174 |
|
|
1175 |
|
public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
1176 |
|
protected void realRun() { |
1177 |
|
try { |
1178 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1178 |
> |
delay(SMALL_DELAY_MS); |
1179 |
|
} catch (InterruptedException ok) {} |
1180 |
|
} |
1181 |
|
} |
1182 |
|
|
1183 |
|
public class SmallCallable extends CheckedCallable { |
1184 |
|
protected Object realCall() throws InterruptedException { |
1185 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1185 |
> |
delay(SMALL_DELAY_MS); |
1186 |
|
return Boolean.TRUE; |
1187 |
|
} |
1188 |
|
} |
1189 |
|
|
1190 |
|
public class MediumRunnable extends CheckedRunnable { |
1191 |
|
protected void realRun() throws Throwable { |
1192 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1192 |
> |
delay(MEDIUM_DELAY_MS); |
1193 |
|
} |
1194 |
|
} |
1195 |
|
|
1196 |
|
public class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
1197 |
|
protected void realRun() throws InterruptedException { |
1198 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1198 |
> |
delay(MEDIUM_DELAY_MS); |
1199 |
|
} |
1200 |
|
} |
1201 |
|
|
1203 |
|
return new CheckedRunnable() { |
1204 |
|
protected void realRun() { |
1205 |
|
try { |
1206 |
< |
Thread.sleep(timeoutMillis); |
1206 |
> |
delay(timeoutMillis); |
1207 |
|
} catch (InterruptedException ok) {} |
1208 |
|
}}; |
1209 |
|
} |
1211 |
|
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
1212 |
|
protected void realRun() { |
1213 |
|
try { |
1214 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1214 |
> |
delay(MEDIUM_DELAY_MS); |
1215 |
|
} catch (InterruptedException ok) {} |
1216 |
|
} |
1217 |
|
} |
1219 |
|
public class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
1220 |
|
protected void realRun() { |
1221 |
|
try { |
1222 |
< |
Thread.sleep(LONG_DELAY_MS); |
1222 |
> |
delay(LONG_DELAY_MS); |
1223 |
|
} catch (InterruptedException ok) {} |
1224 |
|
} |
1225 |
|
} |
1243 |
|
public boolean isDone() { return done; } |
1244 |
|
public void run() { |
1245 |
|
try { |
1246 |
< |
Thread.sleep(timeoutMillis); |
1246 |
> |
delay(timeoutMillis); |
1247 |
|
done = true; |
1248 |
|
} catch (InterruptedException ok) {} |
1249 |
|
} |
1254 |
|
public volatile boolean done = false; |
1255 |
|
public void run() { |
1256 |
|
try { |
1257 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1257 |
> |
delay(SHORT_DELAY_MS); |
1258 |
|
done = true; |
1259 |
|
} catch (InterruptedException ok) {} |
1260 |
|
} |
1264 |
|
public volatile boolean done = false; |
1265 |
|
public void run() { |
1266 |
|
try { |
1267 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1267 |
> |
delay(SMALL_DELAY_MS); |
1268 |
|
done = true; |
1269 |
|
} catch (InterruptedException ok) {} |
1270 |
|
} |
1274 |
|
public volatile boolean done = false; |
1275 |
|
public void run() { |
1276 |
|
try { |
1277 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1277 |
> |
delay(MEDIUM_DELAY_MS); |
1278 |
|
done = true; |
1279 |
|
} catch (InterruptedException ok) {} |
1280 |
|
} |
1284 |
|
public volatile boolean done = false; |
1285 |
|
public void run() { |
1286 |
|
try { |
1287 |
< |
Thread.sleep(LONG_DELAY_MS); |
1287 |
> |
delay(LONG_DELAY_MS); |
1288 |
|
done = true; |
1289 |
|
} catch (InterruptedException ok) {} |
1290 |
|
} |
1301 |
|
public volatile boolean done = false; |
1302 |
|
public Object call() { |
1303 |
|
try { |
1304 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1304 |
> |
delay(SMALL_DELAY_MS); |
1305 |
|
done = true; |
1306 |
|
} catch (InterruptedException ok) {} |
1307 |
|
return Boolean.TRUE; |
1314 |
|
public abstract class CheckedRecursiveAction extends RecursiveAction { |
1315 |
|
protected abstract void realCompute() throws Throwable; |
1316 |
|
|
1317 |
< |
public final void compute() { |
1317 |
> |
@Override protected final void compute() { |
1318 |
|
try { |
1319 |
|
realCompute(); |
1320 |
|
} catch (Throwable t) { |
1329 |
|
public abstract class CheckedRecursiveTask<T> extends RecursiveTask<T> { |
1330 |
|
protected abstract T realCompute() throws Throwable; |
1331 |
|
|
1332 |
< |
public final T compute() { |
1332 |
> |
@Override protected final T compute() { |
1333 |
|
try { |
1334 |
|
return realCompute(); |
1335 |
|
} catch (Throwable t) { |
1348 |
|
} |
1349 |
|
|
1350 |
|
/** |
1351 |
< |
* A CyclicBarrier that fails with AssertionFailedErrors instead |
1352 |
< |
* of throwing checked exceptions. |
1351 |
> |
* A CyclicBarrier that uses timed await and fails with |
1352 |
> |
* AssertionFailedErrors instead of throwing checked exceptions. |
1353 |
|
*/ |
1354 |
|
public class CheckedBarrier extends CyclicBarrier { |
1355 |
|
public CheckedBarrier(int parties) { super(parties); } |
1356 |
|
|
1357 |
|
public int await() { |
1358 |
|
try { |
1359 |
< |
return super.await(); |
1359 |
> |
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1360 |
> |
} catch (TimeoutException e) { |
1361 |
> |
throw new AssertionFailedError("timed out"); |
1362 |
|
} catch (Exception e) { |
1363 |
|
AssertionFailedError afe = |
1364 |
|
new AssertionFailedError("Unexpected exception: " + e); |
1368 |
|
} |
1369 |
|
} |
1370 |
|
|
1371 |
< |
public void checkEmpty(BlockingQueue q) { |
1371 |
> |
void checkEmpty(BlockingQueue q) { |
1372 |
|
try { |
1373 |
|
assertTrue(q.isEmpty()); |
1374 |
|
assertEquals(0, q.size()); |
1395 |
|
} |
1396 |
|
} |
1397 |
|
|
1398 |
+ |
void assertSerialEquals(Object x, Object y) { |
1399 |
+ |
assertTrue(Arrays.equals(serialBytes(x), serialBytes(y))); |
1400 |
+ |
} |
1401 |
+ |
|
1402 |
+ |
void assertNotSerialEquals(Object x, Object y) { |
1403 |
+ |
assertFalse(Arrays.equals(serialBytes(x), serialBytes(y))); |
1404 |
+ |
} |
1405 |
+ |
|
1406 |
+ |
byte[] serialBytes(Object o) { |
1407 |
+ |
try { |
1408 |
+ |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1409 |
+ |
ObjectOutputStream oos = new ObjectOutputStream(bos); |
1410 |
+ |
oos.writeObject(o); |
1411 |
+ |
oos.flush(); |
1412 |
+ |
oos.close(); |
1413 |
+ |
return bos.toByteArray(); |
1414 |
+ |
} catch (Throwable t) { |
1415 |
+ |
threadUnexpectedException(t); |
1416 |
+ |
return new byte[0]; |
1417 |
+ |
} |
1418 |
+ |
} |
1419 |
+ |
|
1420 |
+ |
@SuppressWarnings("unchecked") |
1421 |
+ |
<T> T serialClone(T o) { |
1422 |
+ |
try { |
1423 |
+ |
ObjectInputStream ois = new ObjectInputStream |
1424 |
+ |
(new ByteArrayInputStream(serialBytes(o))); |
1425 |
+ |
T clone = (T) ois.readObject(); |
1426 |
+ |
assertSame(o.getClass(), clone.getClass()); |
1427 |
+ |
return clone; |
1428 |
+ |
} catch (Throwable t) { |
1429 |
+ |
threadUnexpectedException(t); |
1430 |
+ |
return null; |
1431 |
+ |
} |
1432 |
+ |
} |
1433 |
+ |
|
1434 |
+ |
public void assertThrows(Class<? extends Throwable> expectedExceptionClass, |
1435 |
+ |
Runnable... throwingActions) { |
1436 |
+ |
for (Runnable throwingAction : throwingActions) { |
1437 |
+ |
boolean threw = false; |
1438 |
+ |
try { throwingAction.run(); } |
1439 |
+ |
catch (Throwable t) { |
1440 |
+ |
threw = true; |
1441 |
+ |
if (!expectedExceptionClass.isInstance(t)) { |
1442 |
+ |
AssertionFailedError afe = |
1443 |
+ |
new AssertionFailedError |
1444 |
+ |
("Expected " + expectedExceptionClass.getName() + |
1445 |
+ |
", got " + t.getClass().getName()); |
1446 |
+ |
afe.initCause(t); |
1447 |
+ |
threadUnexpectedException(afe); |
1448 |
+ |
} |
1449 |
+ |
} |
1450 |
+ |
if (!threw) |
1451 |
+ |
shouldThrow(expectedExceptionClass.getName()); |
1452 |
+ |
} |
1453 |
+ |
} |
1454 |
|
} |