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 |
115 |
|
protected static final boolean expensiveTests = |
116 |
|
Boolean.getBoolean("jsr166.expensiveTests"); |
117 |
|
|
118 |
+ |
protected static final boolean testImplementationDetails = |
119 |
+ |
Boolean.getBoolean("jsr166.testImplementationDetails"); |
120 |
+ |
|
121 |
|
/** |
122 |
|
* If true, report on stdout all "slow" tests, that is, ones that |
123 |
|
* take more than profileThreshold milliseconds to execute. |
132 |
|
private static final long profileThreshold = |
133 |
|
Long.getLong("jsr166.profileThreshold", 100); |
134 |
|
|
135 |
+ |
/** |
136 |
+ |
* The number of repetitions per test (for tickling rare bugs). |
137 |
+ |
*/ |
138 |
+ |
private static final int runsPerTest = |
139 |
+ |
Integer.getInteger("jsr166.runsPerTest", 1); |
140 |
+ |
|
141 |
+ |
/** |
142 |
+ |
* A filter for tests to run, matching strings of the form |
143 |
+ |
* methodName(className), e.g. "testInvokeAll5(ForkJoinPoolTest)" |
144 |
+ |
* Usefully combined with jsr166.runsPerTest. |
145 |
+ |
*/ |
146 |
+ |
private static final Pattern methodFilter = methodFilter(); |
147 |
+ |
|
148 |
+ |
private static Pattern methodFilter() { |
149 |
+ |
String regex = System.getProperty("jsr166.methodFilter"); |
150 |
+ |
return (regex == null) ? null : Pattern.compile(regex); |
151 |
+ |
} |
152 |
+ |
|
153 |
|
protected void runTest() throws Throwable { |
154 |
< |
if (profileTests) |
155 |
< |
runTestProfiled(); |
156 |
< |
else |
157 |
< |
super.runTest(); |
154 |
> |
if (methodFilter == null |
155 |
> |
|| methodFilter.matcher(toString()).find()) { |
156 |
> |
for (int i = 0; i < runsPerTest; i++) { |
157 |
> |
if (profileTests) |
158 |
> |
runTestProfiled(); |
159 |
> |
else |
160 |
> |
super.runTest(); |
161 |
> |
} |
162 |
> |
} |
163 |
|
} |
164 |
|
|
165 |
|
protected void runTestProfiled() throws Throwable { |
166 |
+ |
// Warmup run, notably to trigger all needed classloading. |
167 |
+ |
super.runTest(); |
168 |
|
long t0 = System.nanoTime(); |
169 |
|
try { |
170 |
|
super.runTest(); |
171 |
|
} finally { |
172 |
< |
long elapsedMillis = |
130 |
< |
(System.nanoTime() - t0) / (1000L * 1000L); |
172 |
> |
long elapsedMillis = millisElapsedSince(t0); |
173 |
|
if (elapsedMillis >= profileThreshold) |
174 |
|
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); |
175 |
|
} |
176 |
|
} |
177 |
|
|
178 |
|
/** |
179 |
< |
* Runs all JSR166 unit tests using junit.textui.TestRunner |
179 |
> |
* Runs all JSR166 unit tests using junit.textui.TestRunner. |
180 |
> |
* Optional command line arg provides the number of iterations to |
181 |
> |
* repeat running the tests. |
182 |
|
*/ |
183 |
|
public static void main(String[] args) { |
184 |
|
if (useSecurityManager) { |
210 |
|
return suite; |
211 |
|
} |
212 |
|
|
213 |
+ |
public static void addNamedTestClasses(TestSuite suite, |
214 |
+ |
String... testClassNames) { |
215 |
+ |
for (String testClassName : testClassNames) { |
216 |
+ |
try { |
217 |
+ |
Class<?> testClass = Class.forName(testClassName); |
218 |
+ |
Method m = testClass.getDeclaredMethod("suite", |
219 |
+ |
new Class<?>[0]); |
220 |
+ |
suite.addTest(newTestSuite((Test)m.invoke(null))); |
221 |
+ |
} catch (Exception e) { |
222 |
+ |
throw new Error("Missing test class", e); |
223 |
+ |
} |
224 |
+ |
} |
225 |
+ |
} |
226 |
+ |
|
227 |
+ |
public static final double JAVA_CLASS_VERSION; |
228 |
+ |
public static final String JAVA_SPECIFICATION_VERSION; |
229 |
+ |
static { |
230 |
+ |
try { |
231 |
+ |
JAVA_CLASS_VERSION = java.security.AccessController.doPrivileged( |
232 |
+ |
new java.security.PrivilegedAction<Double>() { |
233 |
+ |
public Double run() { |
234 |
+ |
return Double.valueOf(System.getProperty("java.class.version"));}}); |
235 |
+ |
JAVA_SPECIFICATION_VERSION = java.security.AccessController.doPrivileged( |
236 |
+ |
new java.security.PrivilegedAction<String>() { |
237 |
+ |
public String run() { |
238 |
+ |
return System.getProperty("java.specification.version");}}); |
239 |
+ |
} catch (Throwable t) { |
240 |
+ |
throw new Error(t); |
241 |
+ |
} |
242 |
+ |
} |
243 |
+ |
|
244 |
+ |
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
245 |
+ |
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
246 |
+ |
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
247 |
+ |
public static boolean atLeastJava9() { |
248 |
+ |
// As of 2014-05, java9 still uses 52.0 class file version |
249 |
+ |
return JAVA_SPECIFICATION_VERSION.startsWith("1.9"); |
250 |
+ |
} |
251 |
+ |
|
252 |
|
/** |
253 |
|
* Collects all JSR166 unit tests as one suite. |
254 |
|
*/ |
255 |
|
public static Test suite() { |
256 |
< |
return newTestSuite( |
256 |
> |
// Java7+ test classes |
257 |
> |
TestSuite suite = newTestSuite( |
258 |
|
ForkJoinPoolTest.suite(), |
259 |
|
ForkJoinTaskTest.suite(), |
260 |
|
RecursiveActionTest.suite(), |
319 |
|
TreeSetTest.suite(), |
320 |
|
TreeSubMapTest.suite(), |
321 |
|
TreeSubSetTest.suite()); |
322 |
+ |
|
323 |
+ |
// Java8+ test classes |
324 |
+ |
if (atLeastJava8()) { |
325 |
+ |
String[] java8TestClassNames = { |
326 |
+ |
"Atomic8Test", |
327 |
+ |
"CompletableFutureTest", |
328 |
+ |
"ConcurrentHashMap8Test", |
329 |
+ |
"CountedCompleterTest", |
330 |
+ |
"DoubleAccumulatorTest", |
331 |
+ |
"DoubleAdderTest", |
332 |
+ |
"ForkJoinPool8Test", |
333 |
+ |
"ForkJoinTask8Test", |
334 |
+ |
"LongAccumulatorTest", |
335 |
+ |
"LongAdderTest", |
336 |
+ |
"SplittableRandomTest", |
337 |
+ |
"StampedLockTest", |
338 |
+ |
"ThreadLocalRandom8Test", |
339 |
+ |
}; |
340 |
+ |
addNamedTestClasses(suite, java8TestClassNames); |
341 |
+ |
} |
342 |
+ |
|
343 |
+ |
// Java9+ test classes |
344 |
+ |
if (atLeastJava9()) { |
345 |
+ |
String[] java9TestClassNames = { |
346 |
+ |
"ThreadPoolExecutor9Test", |
347 |
+ |
}; |
348 |
+ |
addNamedTestClasses(suite, java9TestClassNames); |
349 |
+ |
} |
350 |
+ |
|
351 |
+ |
return suite; |
352 |
|
} |
353 |
|
|
354 |
+ |
// Delays for timing-dependent tests, in milliseconds. |
355 |
|
|
356 |
|
public static long SHORT_DELAY_MS; |
357 |
|
public static long SMALL_DELAY_MS; |
358 |
|
public static long MEDIUM_DELAY_MS; |
359 |
|
public static long LONG_DELAY_MS; |
360 |
|
|
246 |
– |
|
361 |
|
/** |
362 |
|
* Returns the shortest timed delay. This could |
363 |
|
* be reimplemented to use for example a Property. |
366 |
|
return 50; |
367 |
|
} |
368 |
|
|
255 |
– |
|
369 |
|
/** |
370 |
|
* Sets delays as multiples of SHORT_DELAY. |
371 |
|
*/ |
373 |
|
SHORT_DELAY_MS = getShortDelay(); |
374 |
|
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
375 |
|
MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10; |
376 |
< |
LONG_DELAY_MS = SHORT_DELAY_MS * 50; |
376 |
> |
LONG_DELAY_MS = SHORT_DELAY_MS * 200; |
377 |
> |
} |
378 |
> |
|
379 |
> |
/** |
380 |
> |
* Returns a timeout in milliseconds to be used in tests that |
381 |
> |
* verify that operations block or time out. |
382 |
> |
*/ |
383 |
> |
long timeoutMillis() { |
384 |
> |
return SHORT_DELAY_MS / 4; |
385 |
> |
} |
386 |
> |
|
387 |
> |
/** |
388 |
> |
* Returns a new Date instance representing a time delayMillis |
389 |
> |
* milliseconds in the future. |
390 |
> |
*/ |
391 |
> |
Date delayedDate(long delayMillis) { |
392 |
> |
return new Date(System.currentTimeMillis() + delayMillis); |
393 |
|
} |
394 |
|
|
395 |
|
/** |
413 |
|
} |
414 |
|
|
415 |
|
/** |
416 |
+ |
* Extra checks that get done for all test cases. |
417 |
+ |
* |
418 |
|
* Triggers test case failure if any thread assertions have failed, |
419 |
|
* by rethrowing, in the test harness thread, any exception recorded |
420 |
|
* earlier by threadRecordFailure. |
421 |
+ |
* |
422 |
+ |
* Triggers test case failure if interrupt status is set in the main thread. |
423 |
|
*/ |
424 |
|
public void tearDown() throws Exception { |
425 |
|
Throwable t = threadFailure.getAndSet(null); |
437 |
|
throw afe; |
438 |
|
} |
439 |
|
} |
440 |
+ |
|
441 |
+ |
if (Thread.interrupted()) |
442 |
+ |
throw new AssertionFailedError("interrupt status set in main thread"); |
443 |
+ |
|
444 |
+ |
checkForkJoinPoolThreadLeaks(); |
445 |
+ |
} |
446 |
+ |
|
447 |
+ |
/** |
448 |
+ |
* Find missing try { ... } finally { joinPool(e); } |
449 |
+ |
*/ |
450 |
+ |
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
451 |
+ |
Thread[] survivors = new Thread[5]; |
452 |
+ |
int count = Thread.enumerate(survivors); |
453 |
+ |
for (int i = 0; i < count; i++) { |
454 |
+ |
Thread thread = survivors[i]; |
455 |
+ |
String name = thread.getName(); |
456 |
+ |
if (name.startsWith("ForkJoinPool-")) { |
457 |
+ |
// give thread some time to terminate |
458 |
+ |
thread.join(LONG_DELAY_MS); |
459 |
+ |
if (!thread.isAlive()) continue; |
460 |
+ |
thread.stop(); |
461 |
+ |
throw new AssertionFailedError |
462 |
+ |
(String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n", |
463 |
+ |
toString(), name)); |
464 |
+ |
} |
465 |
+ |
} |
466 |
|
} |
467 |
|
|
468 |
|
/** |
594 |
|
else { |
595 |
|
AssertionFailedError afe = |
596 |
|
new AssertionFailedError("unexpected exception: " + t); |
597 |
< |
t.initCause(t); |
597 |
> |
afe.initCause(t); |
598 |
|
throw afe; |
599 |
|
} |
600 |
|
} |
601 |
|
|
602 |
|
/** |
603 |
+ |
* Delays, via Thread.sleep, for the given millisecond delay, but |
604 |
+ |
* if the sleep is shorter than specified, may re-sleep or yield |
605 |
+ |
* until time elapses. |
606 |
+ |
*/ |
607 |
+ |
static void delay(long millis) throws InterruptedException { |
608 |
+ |
long startTime = System.nanoTime(); |
609 |
+ |
long ns = millis * 1000 * 1000; |
610 |
+ |
for (;;) { |
611 |
+ |
if (millis > 0L) |
612 |
+ |
Thread.sleep(millis); |
613 |
+ |
else // too short to sleep |
614 |
+ |
Thread.yield(); |
615 |
+ |
long d = ns - (System.nanoTime() - startTime); |
616 |
+ |
if (d > 0L) |
617 |
+ |
millis = d / (1000 * 1000); |
618 |
+ |
else |
619 |
+ |
break; |
620 |
+ |
} |
621 |
+ |
} |
622 |
+ |
|
623 |
+ |
/** |
624 |
|
* Waits out termination of a thread pool or fails doing so. |
625 |
|
*/ |
626 |
< |
public void joinPool(ExecutorService exec) { |
626 |
> |
void joinPool(ExecutorService exec) { |
627 |
|
try { |
628 |
|
exec.shutdown(); |
629 |
|
assertTrue("ExecutorService did not terminate in a timely manner", |
630 |
< |
exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
630 |
> |
exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)); |
631 |
|
} catch (SecurityException ok) { |
632 |
|
// Allowed in case test doesn't have privs |
633 |
|
} catch (InterruptedException ie) { |
635 |
|
} |
636 |
|
} |
637 |
|
|
638 |
+ |
/** |
639 |
+ |
* A debugging tool to print all stack traces, as jstack does. |
640 |
+ |
*/ |
641 |
+ |
static void printAllStackTraces() { |
642 |
+ |
for (ThreadInfo info : |
643 |
+ |
ManagementFactory.getThreadMXBean() |
644 |
+ |
.dumpAllThreads(true, true)) |
645 |
+ |
System.err.print(info); |
646 |
+ |
} |
647 |
+ |
|
648 |
+ |
/** |
649 |
+ |
* Checks that thread does not terminate within the default |
650 |
+ |
* millisecond delay of {@code timeoutMillis()}. |
651 |
+ |
*/ |
652 |
+ |
void assertThreadStaysAlive(Thread thread) { |
653 |
+ |
assertThreadStaysAlive(thread, timeoutMillis()); |
654 |
+ |
} |
655 |
+ |
|
656 |
+ |
/** |
657 |
+ |
* Checks that thread does not terminate within the given millisecond delay. |
658 |
+ |
*/ |
659 |
+ |
void assertThreadStaysAlive(Thread thread, long millis) { |
660 |
+ |
try { |
661 |
+ |
// No need to optimize the failing case via Thread.join. |
662 |
+ |
delay(millis); |
663 |
+ |
assertTrue(thread.isAlive()); |
664 |
+ |
} catch (InterruptedException ie) { |
665 |
+ |
fail("Unexpected InterruptedException"); |
666 |
+ |
} |
667 |
+ |
} |
668 |
+ |
|
669 |
+ |
/** |
670 |
+ |
* Checks that the threads do not terminate within the default |
671 |
+ |
* millisecond delay of {@code timeoutMillis()}. |
672 |
+ |
*/ |
673 |
+ |
void assertThreadsStayAlive(Thread... threads) { |
674 |
+ |
assertThreadsStayAlive(timeoutMillis(), threads); |
675 |
+ |
} |
676 |
+ |
|
677 |
+ |
/** |
678 |
+ |
* Checks that the threads do not terminate within the given millisecond delay. |
679 |
+ |
*/ |
680 |
+ |
void assertThreadsStayAlive(long millis, Thread... threads) { |
681 |
+ |
try { |
682 |
+ |
// No need to optimize the failing case via Thread.join. |
683 |
+ |
delay(millis); |
684 |
+ |
for (Thread thread : threads) |
685 |
+ |
assertTrue(thread.isAlive()); |
686 |
+ |
} catch (InterruptedException ie) { |
687 |
+ |
fail("Unexpected InterruptedException"); |
688 |
+ |
} |
689 |
+ |
} |
690 |
+ |
|
691 |
+ |
/** |
692 |
+ |
* Checks that future.get times out, with the default timeout of |
693 |
+ |
* {@code timeoutMillis()}. |
694 |
+ |
*/ |
695 |
+ |
void assertFutureTimesOut(Future future) { |
696 |
+ |
assertFutureTimesOut(future, timeoutMillis()); |
697 |
+ |
} |
698 |
+ |
|
699 |
+ |
/** |
700 |
+ |
* Checks that future.get times out, with the given millisecond timeout. |
701 |
+ |
*/ |
702 |
+ |
void assertFutureTimesOut(Future future, long timeoutMillis) { |
703 |
+ |
long startTime = System.nanoTime(); |
704 |
+ |
try { |
705 |
+ |
future.get(timeoutMillis, MILLISECONDS); |
706 |
+ |
shouldThrow(); |
707 |
+ |
} catch (TimeoutException success) { |
708 |
+ |
} catch (Exception e) { |
709 |
+ |
threadUnexpectedException(e); |
710 |
+ |
} finally { future.cancel(true); } |
711 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
712 |
+ |
} |
713 |
|
|
714 |
|
/** |
715 |
|
* Fails with message "should throw exception". |
750 |
|
public static final Integer m6 = new Integer(-6); |
751 |
|
public static final Integer m10 = new Integer(-10); |
752 |
|
|
498 |
– |
|
753 |
|
/** |
754 |
|
* Runs Runnable r with a security policy that permits precisely |
755 |
|
* the specified permissions. If there is no current security |
761 |
|
SecurityManager sm = System.getSecurityManager(); |
762 |
|
if (sm == null) { |
763 |
|
r.run(); |
764 |
+ |
} |
765 |
+ |
runWithSecurityManagerWithPermissions(r, permissions); |
766 |
+ |
} |
767 |
+ |
|
768 |
+ |
/** |
769 |
+ |
* Runs Runnable r with a security policy that permits precisely |
770 |
+ |
* the specified permissions. If there is no current security |
771 |
+ |
* manager, a temporary one is set for the duration of the |
772 |
+ |
* Runnable. We require that any security manager permit |
773 |
+ |
* getPolicy/setPolicy. |
774 |
+ |
*/ |
775 |
+ |
public void runWithSecurityManagerWithPermissions(Runnable r, |
776 |
+ |
Permission... permissions) { |
777 |
+ |
SecurityManager sm = System.getSecurityManager(); |
778 |
+ |
if (sm == null) { |
779 |
|
Policy savedPolicy = Policy.getPolicy(); |
780 |
|
try { |
781 |
|
Policy.setPolicy(permissivePolicy()); |
782 |
|
System.setSecurityManager(new SecurityManager()); |
783 |
< |
runWithPermissions(r, permissions); |
783 |
> |
runWithSecurityManagerWithPermissions(r, permissions); |
784 |
|
} finally { |
785 |
|
System.setSecurityManager(null); |
786 |
|
Policy.setPolicy(savedPolicy); |
828 |
|
return perms.implies(p); |
829 |
|
} |
830 |
|
public void refresh() {} |
831 |
+ |
public String toString() { |
832 |
+ |
List<Permission> ps = new ArrayList<Permission>(); |
833 |
+ |
for (Enumeration<Permission> e = perms.elements(); e.hasMoreElements();) |
834 |
+ |
ps.add(e.nextElement()); |
835 |
+ |
return "AdjustablePolicy with permissions " + ps; |
836 |
+ |
} |
837 |
|
} |
838 |
|
|
839 |
|
/** |
861 |
|
*/ |
862 |
|
void sleep(long millis) { |
863 |
|
try { |
864 |
< |
Thread.sleep(millis); |
864 |
> |
delay(millis); |
865 |
|
} catch (InterruptedException ie) { |
866 |
|
AssertionFailedError afe = |
867 |
|
new AssertionFailedError("Unexpected InterruptedException"); |
871 |
|
} |
872 |
|
|
873 |
|
/** |
874 |
< |
* 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 |
874 |
> |
* Spin-waits up to the specified number of milliseconds for the given |
875 |
|
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
876 |
|
*/ |
877 |
|
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
878 |
< |
long timeoutNanos = timeoutMillis * 1000L * 1000L; |
614 |
< |
long t0 = System.nanoTime(); |
878 |
> |
long startTime = System.nanoTime(); |
879 |
|
for (;;) { |
880 |
|
Thread.State s = thread.getState(); |
881 |
|
if (s == Thread.State.BLOCKED || |
884 |
|
return; |
885 |
|
else if (s == Thread.State.TERMINATED) |
886 |
|
fail("Unexpected thread termination"); |
887 |
< |
else if (System.nanoTime() - t0 > timeoutNanos) { |
887 |
> |
else if (millisElapsedSince(startTime) > timeoutMillis) { |
888 |
|
threadAssertTrue(thread.isAlive()); |
889 |
|
return; |
890 |
|
} |
893 |
|
} |
894 |
|
|
895 |
|
/** |
896 |
+ |
* Waits up to LONG_DELAY_MS for the given thread to enter a wait |
897 |
+ |
* state: BLOCKED, WAITING, or TIMED_WAITING. |
898 |
+ |
*/ |
899 |
+ |
void waitForThreadToEnterWaitState(Thread thread) { |
900 |
+ |
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS); |
901 |
+ |
} |
902 |
+ |
|
903 |
+ |
/** |
904 |
|
* Returns the number of milliseconds since time given by |
905 |
|
* startNanoTime, which must have been previously returned from a |
906 |
|
* call to {@link System.nanoTime()}. |
907 |
|
*/ |
908 |
< |
long millisElapsedSince(long startNanoTime) { |
908 |
> |
static long millisElapsedSince(long startNanoTime) { |
909 |
|
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
910 |
|
} |
911 |
|
|
930 |
|
} catch (InterruptedException ie) { |
931 |
|
threadUnexpectedException(ie); |
932 |
|
} finally { |
933 |
< |
if (t.isAlive()) { |
933 |
> |
if (t.getState() != Thread.State.TERMINATED) { |
934 |
|
t.interrupt(); |
935 |
|
fail("Test timed out"); |
936 |
|
} |
937 |
|
} |
938 |
|
} |
939 |
|
|
940 |
+ |
/** |
941 |
+ |
* Waits for LONG_DELAY_MS milliseconds for the thread to |
942 |
+ |
* terminate (using {@link Thread#join(long)}), else interrupts |
943 |
+ |
* the thread (in the hope that it may terminate later) and fails. |
944 |
+ |
*/ |
945 |
+ |
void awaitTermination(Thread t) { |
946 |
+ |
awaitTermination(t, LONG_DELAY_MS); |
947 |
+ |
} |
948 |
+ |
|
949 |
|
// Some convenient Runnable classes |
950 |
|
|
951 |
|
public abstract class CheckedRunnable implements Runnable { |
1008 |
|
realRun(); |
1009 |
|
threadShouldThrow("InterruptedException"); |
1010 |
|
} catch (InterruptedException success) { |
1011 |
+ |
threadAssertFalse(Thread.interrupted()); |
1012 |
|
} catch (Throwable t) { |
1013 |
|
threadUnexpectedException(t); |
1014 |
|
} |
1038 |
|
threadShouldThrow("InterruptedException"); |
1039 |
|
return result; |
1040 |
|
} catch (InterruptedException success) { |
1041 |
+ |
threadAssertFalse(Thread.interrupted()); |
1042 |
|
} catch (Throwable t) { |
1043 |
|
threadUnexpectedException(t); |
1044 |
|
} |
1070 |
|
}}; |
1071 |
|
} |
1072 |
|
|
1073 |
+ |
public Runnable awaiter(final CountDownLatch latch) { |
1074 |
+ |
return new CheckedRunnable() { |
1075 |
+ |
public void realRun() throws InterruptedException { |
1076 |
+ |
await(latch); |
1077 |
+ |
}}; |
1078 |
+ |
} |
1079 |
+ |
|
1080 |
+ |
public void await(CountDownLatch latch) { |
1081 |
+ |
try { |
1082 |
+ |
assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); |
1083 |
+ |
} catch (Throwable t) { |
1084 |
+ |
threadUnexpectedException(t); |
1085 |
+ |
} |
1086 |
+ |
} |
1087 |
+ |
|
1088 |
+ |
public void await(Semaphore semaphore) { |
1089 |
+ |
try { |
1090 |
+ |
assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
1091 |
+ |
} catch (Throwable t) { |
1092 |
+ |
threadUnexpectedException(t); |
1093 |
+ |
} |
1094 |
+ |
} |
1095 |
+ |
|
1096 |
+ |
// /** |
1097 |
+ |
// * Spin-waits up to LONG_DELAY_MS until flag becomes true. |
1098 |
+ |
// */ |
1099 |
+ |
// public void await(AtomicBoolean flag) { |
1100 |
+ |
// await(flag, LONG_DELAY_MS); |
1101 |
+ |
// } |
1102 |
+ |
|
1103 |
+ |
// /** |
1104 |
+ |
// * Spin-waits up to the specified timeout until flag becomes true. |
1105 |
+ |
// */ |
1106 |
+ |
// public void await(AtomicBoolean flag, long timeoutMillis) { |
1107 |
+ |
// long startTime = System.nanoTime(); |
1108 |
+ |
// while (!flag.get()) { |
1109 |
+ |
// if (millisElapsedSince(startTime) > timeoutMillis) |
1110 |
+ |
// throw new AssertionFailedError("timed out"); |
1111 |
+ |
// Thread.yield(); |
1112 |
+ |
// } |
1113 |
+ |
// } |
1114 |
+ |
|
1115 |
|
public static class NPETask implements Callable<String> { |
1116 |
|
public String call() { throw new NullPointerException(); } |
1117 |
|
} |
1122 |
|
|
1123 |
|
public class ShortRunnable extends CheckedRunnable { |
1124 |
|
protected void realRun() throws Throwable { |
1125 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1125 |
> |
delay(SHORT_DELAY_MS); |
1126 |
|
} |
1127 |
|
} |
1128 |
|
|
1129 |
|
public class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
1130 |
|
protected void realRun() throws InterruptedException { |
1131 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1131 |
> |
delay(SHORT_DELAY_MS); |
1132 |
|
} |
1133 |
|
} |
1134 |
|
|
1135 |
|
public class SmallRunnable extends CheckedRunnable { |
1136 |
|
protected void realRun() throws Throwable { |
1137 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1137 |
> |
delay(SMALL_DELAY_MS); |
1138 |
|
} |
1139 |
|
} |
1140 |
|
|
1141 |
|
public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
1142 |
|
protected void realRun() { |
1143 |
|
try { |
1144 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1144 |
> |
delay(SMALL_DELAY_MS); |
1145 |
|
} catch (InterruptedException ok) {} |
1146 |
|
} |
1147 |
|
} |
1148 |
|
|
1149 |
|
public class SmallCallable extends CheckedCallable { |
1150 |
|
protected Object realCall() throws InterruptedException { |
1151 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1151 |
> |
delay(SMALL_DELAY_MS); |
1152 |
|
return Boolean.TRUE; |
1153 |
|
} |
1154 |
|
} |
1155 |
|
|
1156 |
|
public class MediumRunnable extends CheckedRunnable { |
1157 |
|
protected void realRun() throws Throwable { |
1158 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1158 |
> |
delay(MEDIUM_DELAY_MS); |
1159 |
|
} |
1160 |
|
} |
1161 |
|
|
1162 |
|
public class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
1163 |
|
protected void realRun() throws InterruptedException { |
1164 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1164 |
> |
delay(MEDIUM_DELAY_MS); |
1165 |
|
} |
1166 |
|
} |
1167 |
|
|
1169 |
|
return new CheckedRunnable() { |
1170 |
|
protected void realRun() { |
1171 |
|
try { |
1172 |
< |
Thread.sleep(timeoutMillis); |
1172 |
> |
delay(timeoutMillis); |
1173 |
|
} catch (InterruptedException ok) {} |
1174 |
|
}}; |
1175 |
|
} |
1177 |
|
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
1178 |
|
protected void realRun() { |
1179 |
|
try { |
1180 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1180 |
> |
delay(MEDIUM_DELAY_MS); |
1181 |
|
} catch (InterruptedException ok) {} |
1182 |
|
} |
1183 |
|
} |
1185 |
|
public class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
1186 |
|
protected void realRun() { |
1187 |
|
try { |
1188 |
< |
Thread.sleep(LONG_DELAY_MS); |
1188 |
> |
delay(LONG_DELAY_MS); |
1189 |
|
} catch (InterruptedException ok) {} |
1190 |
|
} |
1191 |
|
} |
1209 |
|
public boolean isDone() { return done; } |
1210 |
|
public void run() { |
1211 |
|
try { |
1212 |
< |
Thread.sleep(timeoutMillis); |
1212 |
> |
delay(timeoutMillis); |
1213 |
|
done = true; |
1214 |
|
} catch (InterruptedException ok) {} |
1215 |
|
} |
1220 |
|
public volatile boolean done = false; |
1221 |
|
public void run() { |
1222 |
|
try { |
1223 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1223 |
> |
delay(SHORT_DELAY_MS); |
1224 |
|
done = true; |
1225 |
|
} catch (InterruptedException ok) {} |
1226 |
|
} |
1230 |
|
public volatile boolean done = false; |
1231 |
|
public void run() { |
1232 |
|
try { |
1233 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1233 |
> |
delay(SMALL_DELAY_MS); |
1234 |
|
done = true; |
1235 |
|
} catch (InterruptedException ok) {} |
1236 |
|
} |
1240 |
|
public volatile boolean done = false; |
1241 |
|
public void run() { |
1242 |
|
try { |
1243 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1243 |
> |
delay(MEDIUM_DELAY_MS); |
1244 |
|
done = true; |
1245 |
|
} catch (InterruptedException ok) {} |
1246 |
|
} |
1250 |
|
public volatile boolean done = false; |
1251 |
|
public void run() { |
1252 |
|
try { |
1253 |
< |
Thread.sleep(LONG_DELAY_MS); |
1253 |
> |
delay(LONG_DELAY_MS); |
1254 |
|
done = true; |
1255 |
|
} catch (InterruptedException ok) {} |
1256 |
|
} |
1267 |
|
public volatile boolean done = false; |
1268 |
|
public Object call() { |
1269 |
|
try { |
1270 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1270 |
> |
delay(SMALL_DELAY_MS); |
1271 |
|
done = true; |
1272 |
|
} catch (InterruptedException ok) {} |
1273 |
|
return Boolean.TRUE; |
1280 |
|
public abstract class CheckedRecursiveAction extends RecursiveAction { |
1281 |
|
protected abstract void realCompute() throws Throwable; |
1282 |
|
|
1283 |
< |
public final void compute() { |
1283 |
> |
@Override protected final void compute() { |
1284 |
|
try { |
1285 |
|
realCompute(); |
1286 |
|
} catch (Throwable t) { |
1295 |
|
public abstract class CheckedRecursiveTask<T> extends RecursiveTask<T> { |
1296 |
|
protected abstract T realCompute() throws Throwable; |
1297 |
|
|
1298 |
< |
public final T compute() { |
1298 |
> |
@Override protected final T compute() { |
1299 |
|
try { |
1300 |
|
return realCompute(); |
1301 |
|
} catch (Throwable t) { |
1314 |
|
} |
1315 |
|
|
1316 |
|
/** |
1317 |
< |
* A CyclicBarrier that fails with AssertionFailedErrors instead |
1318 |
< |
* of throwing checked exceptions. |
1317 |
> |
* A CyclicBarrier that uses timed await and fails with |
1318 |
> |
* AssertionFailedErrors instead of throwing checked exceptions. |
1319 |
|
*/ |
1320 |
|
public class CheckedBarrier extends CyclicBarrier { |
1321 |
|
public CheckedBarrier(int parties) { super(parties); } |
1322 |
|
|
1323 |
|
public int await() { |
1324 |
|
try { |
1325 |
< |
return super.await(); |
1325 |
> |
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1326 |
> |
} catch (TimeoutException e) { |
1327 |
> |
throw new AssertionFailedError("timed out"); |
1328 |
|
} catch (Exception e) { |
1329 |
|
AssertionFailedError afe = |
1330 |
|
new AssertionFailedError("Unexpected exception: " + e); |
1334 |
|
} |
1335 |
|
} |
1336 |
|
|
1337 |
+ |
void checkEmpty(BlockingQueue q) { |
1338 |
+ |
try { |
1339 |
+ |
assertTrue(q.isEmpty()); |
1340 |
+ |
assertEquals(0, q.size()); |
1341 |
+ |
assertNull(q.peek()); |
1342 |
+ |
assertNull(q.poll()); |
1343 |
+ |
assertNull(q.poll(0, MILLISECONDS)); |
1344 |
+ |
assertEquals(q.toString(), "[]"); |
1345 |
+ |
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
1346 |
+ |
assertFalse(q.iterator().hasNext()); |
1347 |
+ |
try { |
1348 |
+ |
q.element(); |
1349 |
+ |
shouldThrow(); |
1350 |
+ |
} catch (NoSuchElementException success) {} |
1351 |
+ |
try { |
1352 |
+ |
q.iterator().next(); |
1353 |
+ |
shouldThrow(); |
1354 |
+ |
} catch (NoSuchElementException success) {} |
1355 |
+ |
try { |
1356 |
+ |
q.remove(); |
1357 |
+ |
shouldThrow(); |
1358 |
+ |
} catch (NoSuchElementException success) {} |
1359 |
+ |
} catch (InterruptedException ie) { |
1360 |
+ |
threadUnexpectedException(ie); |
1361 |
+ |
} |
1362 |
+ |
} |
1363 |
+ |
|
1364 |
+ |
void assertSerialEquals(Object x, Object y) { |
1365 |
+ |
assertTrue(Arrays.equals(serialBytes(x), serialBytes(y))); |
1366 |
+ |
} |
1367 |
+ |
|
1368 |
+ |
void assertNotSerialEquals(Object x, Object y) { |
1369 |
+ |
assertFalse(Arrays.equals(serialBytes(x), serialBytes(y))); |
1370 |
+ |
} |
1371 |
+ |
|
1372 |
+ |
byte[] serialBytes(Object o) { |
1373 |
+ |
try { |
1374 |
+ |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1375 |
+ |
ObjectOutputStream oos = new ObjectOutputStream(bos); |
1376 |
+ |
oos.writeObject(o); |
1377 |
+ |
oos.flush(); |
1378 |
+ |
oos.close(); |
1379 |
+ |
return bos.toByteArray(); |
1380 |
+ |
} catch (Throwable t) { |
1381 |
+ |
threadUnexpectedException(t); |
1382 |
+ |
return new byte[0]; |
1383 |
+ |
} |
1384 |
+ |
} |
1385 |
+ |
|
1386 |
+ |
@SuppressWarnings("unchecked") |
1387 |
+ |
<T> T serialClone(T o) { |
1388 |
+ |
try { |
1389 |
+ |
ObjectInputStream ois = new ObjectInputStream |
1390 |
+ |
(new ByteArrayInputStream(serialBytes(o))); |
1391 |
+ |
T clone = (T) ois.readObject(); |
1392 |
+ |
assertSame(o.getClass(), clone.getClass()); |
1393 |
+ |
return clone; |
1394 |
+ |
} catch (Throwable t) { |
1395 |
+ |
threadUnexpectedException(t); |
1396 |
+ |
return null; |
1397 |
+ |
} |
1398 |
+ |
} |
1399 |
+ |
|
1400 |
+ |
public void assertThrows(Class<? extends Throwable> expectedExceptionClass, |
1401 |
+ |
Runnable... throwingActions) { |
1402 |
+ |
for (Runnable throwingAction : throwingActions) { |
1403 |
+ |
boolean threw = false; |
1404 |
+ |
try { throwingAction.run(); } |
1405 |
+ |
catch (Throwable t) { |
1406 |
+ |
threw = true; |
1407 |
+ |
if (!expectedExceptionClass.isInstance(t)) { |
1408 |
+ |
AssertionFailedError afe = |
1409 |
+ |
new AssertionFailedError |
1410 |
+ |
("Expected " + expectedExceptionClass.getName() + |
1411 |
+ |
", got " + t.getClass().getName()); |
1412 |
+ |
afe.initCause(t); |
1413 |
+ |
threadUnexpectedException(afe); |
1414 |
+ |
} |
1415 |
+ |
} |
1416 |
+ |
if (!threw) |
1417 |
+ |
shouldThrow(expectedExceptionClass.getName()); |
1418 |
+ |
} |
1419 |
+ |
} |
1420 |
|
} |