6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
9 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
> |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
11 |
> |
|
12 |
|
import java.io.ByteArrayInputStream; |
13 |
|
import java.io.ByteArrayOutputStream; |
14 |
|
import java.io.ObjectInputStream; |
15 |
|
import java.io.ObjectOutputStream; |
16 |
< |
import java.util.Arrays; |
17 |
< |
import java.util.Date; |
18 |
< |
import java.util.NoSuchElementException; |
17 |
< |
import java.util.PropertyPermission; |
18 |
< |
import java.util.concurrent.*; |
19 |
< |
import java.util.concurrent.atomic.AtomicBoolean; |
20 |
< |
import java.util.concurrent.atomic.AtomicReference; |
21 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
22 |
< |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
16 |
> |
import java.lang.management.ManagementFactory; |
17 |
> |
import java.lang.management.ThreadInfo; |
18 |
> |
import java.lang.reflect.Method; |
19 |
|
import java.security.CodeSource; |
20 |
|
import java.security.Permission; |
21 |
|
import java.security.PermissionCollection; |
23 |
|
import java.security.Policy; |
24 |
|
import java.security.ProtectionDomain; |
25 |
|
import java.security.SecurityPermission; |
26 |
+ |
import java.util.ArrayList; |
27 |
+ |
import java.util.Arrays; |
28 |
+ |
import java.util.Date; |
29 |
+ |
import java.util.Enumeration; |
30 |
+ |
import java.util.Iterator; |
31 |
+ |
import java.util.List; |
32 |
+ |
import java.util.NoSuchElementException; |
33 |
+ |
import java.util.PropertyPermission; |
34 |
+ |
import java.util.concurrent.BlockingQueue; |
35 |
+ |
import java.util.concurrent.Callable; |
36 |
+ |
import java.util.concurrent.CountDownLatch; |
37 |
+ |
import java.util.concurrent.CyclicBarrier; |
38 |
+ |
import java.util.concurrent.ExecutorService; |
39 |
+ |
import java.util.concurrent.Future; |
40 |
+ |
import java.util.concurrent.RecursiveAction; |
41 |
+ |
import java.util.concurrent.RecursiveTask; |
42 |
+ |
import java.util.concurrent.RejectedExecutionHandler; |
43 |
+ |
import java.util.concurrent.Semaphore; |
44 |
+ |
import java.util.concurrent.ThreadFactory; |
45 |
+ |
import java.util.concurrent.ThreadPoolExecutor; |
46 |
+ |
import java.util.concurrent.TimeoutException; |
47 |
+ |
import java.util.concurrent.atomic.AtomicReference; |
48 |
+ |
import java.util.regex.Pattern; |
49 |
+ |
|
50 |
+ |
import junit.framework.AssertionFailedError; |
51 |
+ |
import junit.framework.Test; |
52 |
+ |
import junit.framework.TestCase; |
53 |
+ |
import junit.framework.TestResult; |
54 |
+ |
import junit.framework.TestSuite; |
55 |
|
|
56 |
|
/** |
57 |
|
* Base class for JSR166 Junit TCK tests. Defines some constants, |
94 |
|
* |
95 |
|
* </ol> |
96 |
|
* |
97 |
< |
* <p> <b>Other notes</b> |
97 |
> |
* <p><b>Other notes</b> |
98 |
|
* <ul> |
99 |
|
* |
100 |
|
* <li> Usually, there is one testcase method per JSR166 method |
134 |
|
Boolean.getBoolean("jsr166.expensiveTests"); |
135 |
|
|
136 |
|
/** |
137 |
+ |
* If true, also run tests that are not part of the official tck |
138 |
+ |
* because they test unspecified implementation details. |
139 |
+ |
*/ |
140 |
+ |
protected static final boolean testImplementationDetails = |
141 |
+ |
Boolean.getBoolean("jsr166.testImplementationDetails"); |
142 |
+ |
|
143 |
+ |
/** |
144 |
|
* If true, report on stdout all "slow" tests, that is, ones that |
145 |
|
* take more than profileThreshold milliseconds to execute. |
146 |
|
*/ |
154 |
|
private static final long profileThreshold = |
155 |
|
Long.getLong("jsr166.profileThreshold", 100); |
156 |
|
|
157 |
+ |
/** |
158 |
+ |
* The number of repetitions per test (for tickling rare bugs). |
159 |
+ |
*/ |
160 |
+ |
private static final int runsPerTest = |
161 |
+ |
Integer.getInteger("jsr166.runsPerTest", 1); |
162 |
+ |
|
163 |
+ |
/** |
164 |
+ |
* The number of repetitions of the test suite (for finding leaks?). |
165 |
+ |
*/ |
166 |
+ |
private static final int suiteRuns = |
167 |
+ |
Integer.getInteger("jsr166.suiteRuns", 1); |
168 |
+ |
|
169 |
+ |
/** |
170 |
+ |
* A filter for tests to run, matching strings of the form |
171 |
+ |
* methodName(className), e.g. "testInvokeAll5(ForkJoinPoolTest)" |
172 |
+ |
* Usefully combined with jsr166.runsPerTest. |
173 |
+ |
*/ |
174 |
+ |
private static final Pattern methodFilter = methodFilter(); |
175 |
+ |
|
176 |
+ |
private static Pattern methodFilter() { |
177 |
+ |
String regex = System.getProperty("jsr166.methodFilter"); |
178 |
+ |
return (regex == null) ? null : Pattern.compile(regex); |
179 |
+ |
} |
180 |
+ |
|
181 |
|
protected void runTest() throws Throwable { |
182 |
< |
if (profileTests) |
183 |
< |
runTestProfiled(); |
184 |
< |
else |
185 |
< |
super.runTest(); |
182 |
> |
if (methodFilter == null |
183 |
> |
|| methodFilter.matcher(toString()).find()) { |
184 |
> |
for (int i = 0; i < runsPerTest; i++) { |
185 |
> |
if (profileTests) |
186 |
> |
runTestProfiled(); |
187 |
> |
else |
188 |
> |
super.runTest(); |
189 |
> |
} |
190 |
> |
} |
191 |
|
} |
192 |
|
|
193 |
|
protected void runTestProfiled() throws Throwable { |
194 |
+ |
// Warmup run, notably to trigger all needed classloading. |
195 |
+ |
super.runTest(); |
196 |
|
long t0 = System.nanoTime(); |
197 |
|
try { |
198 |
|
super.runTest(); |
199 |
|
} finally { |
200 |
< |
long elapsedMillis = |
138 |
< |
(System.nanoTime() - t0) / (1000L * 1000L); |
200 |
> |
long elapsedMillis = millisElapsedSince(t0); |
201 |
|
if (elapsedMillis >= profileThreshold) |
202 |
|
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); |
203 |
|
} |
204 |
|
} |
205 |
|
|
206 |
|
/** |
207 |
< |
* Runs all JSR166 unit tests using junit.textui.TestRunner |
207 |
> |
* Runs all JSR166 unit tests using junit.textui.TestRunner. |
208 |
|
*/ |
209 |
|
public static void main(String[] args) { |
210 |
+ |
main(suite(), args); |
211 |
+ |
} |
212 |
+ |
|
213 |
+ |
/** |
214 |
+ |
* Runs all unit tests in the given test suite. |
215 |
+ |
* Actual behavior influenced by jsr166.* system properties. |
216 |
+ |
*/ |
217 |
+ |
static void main(Test suite, String[] args) { |
218 |
|
if (useSecurityManager) { |
219 |
|
System.err.println("Setting a permissive security manager"); |
220 |
|
Policy.setPolicy(permissivePolicy()); |
221 |
|
System.setSecurityManager(new SecurityManager()); |
222 |
|
} |
223 |
< |
int iters = (args.length == 0) ? 1 : Integer.parseInt(args[0]); |
224 |
< |
|
225 |
< |
Test s = suite(); |
226 |
< |
for (int i = 0; i < iters; ++i) { |
157 |
< |
junit.textui.TestRunner.run(s); |
223 |
> |
for (int i = 0; i < suiteRuns; i++) { |
224 |
> |
TestResult result = junit.textui.TestRunner.run(suite); |
225 |
> |
if (!result.wasSuccessful()) |
226 |
> |
System.exit(1); |
227 |
|
System.gc(); |
228 |
|
System.runFinalization(); |
229 |
|
} |
161 |
– |
System.exit(0); |
230 |
|
} |
231 |
|
|
232 |
|
public static TestSuite newTestSuite(Object... suiteOrClasses) { |
242 |
|
return suite; |
243 |
|
} |
244 |
|
|
245 |
+ |
public static void addNamedTestClasses(TestSuite suite, |
246 |
+ |
String... testClassNames) { |
247 |
+ |
for (String testClassName : testClassNames) { |
248 |
+ |
try { |
249 |
+ |
Class<?> testClass = Class.forName(testClassName); |
250 |
+ |
Method m = testClass.getDeclaredMethod("suite", |
251 |
+ |
new Class<?>[0]); |
252 |
+ |
suite.addTest(newTestSuite((Test)m.invoke(null))); |
253 |
+ |
} catch (Exception e) { |
254 |
+ |
throw new Error("Missing test class", e); |
255 |
+ |
} |
256 |
+ |
} |
257 |
+ |
} |
258 |
+ |
|
259 |
+ |
public static final double JAVA_CLASS_VERSION; |
260 |
+ |
public static final String JAVA_SPECIFICATION_VERSION; |
261 |
+ |
static { |
262 |
+ |
try { |
263 |
+ |
JAVA_CLASS_VERSION = java.security.AccessController.doPrivileged( |
264 |
+ |
new java.security.PrivilegedAction<Double>() { |
265 |
+ |
public Double run() { |
266 |
+ |
return Double.valueOf(System.getProperty("java.class.version"));}}); |
267 |
+ |
JAVA_SPECIFICATION_VERSION = java.security.AccessController.doPrivileged( |
268 |
+ |
new java.security.PrivilegedAction<String>() { |
269 |
+ |
public String run() { |
270 |
+ |
return System.getProperty("java.specification.version");}}); |
271 |
+ |
} catch (Throwable t) { |
272 |
+ |
throw new Error(t); |
273 |
+ |
} |
274 |
+ |
} |
275 |
+ |
|
276 |
+ |
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
277 |
+ |
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
278 |
+ |
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
279 |
+ |
public static boolean atLeastJava9() { return JAVA_CLASS_VERSION >= 53.0; } |
280 |
+ |
|
281 |
|
/** |
282 |
|
* Collects all JSR166 unit tests as one suite. |
283 |
|
*/ |
284 |
|
public static Test suite() { |
285 |
< |
return newTestSuite( |
285 |
> |
// Java7+ test classes |
286 |
> |
TestSuite suite = newTestSuite( |
287 |
|
ForkJoinPoolTest.suite(), |
288 |
|
ForkJoinTaskTest.suite(), |
289 |
|
RecursiveActionTest.suite(), |
348 |
|
TreeSetTest.suite(), |
349 |
|
TreeSubMapTest.suite(), |
350 |
|
TreeSubSetTest.suite()); |
351 |
+ |
|
352 |
+ |
// Java8+ test classes |
353 |
+ |
if (atLeastJava8()) { |
354 |
+ |
String[] java8TestClassNames = { |
355 |
+ |
"Atomic8Test", |
356 |
+ |
"CompletableFutureTest", |
357 |
+ |
"ConcurrentHashMap8Test", |
358 |
+ |
"CountedCompleterTest", |
359 |
+ |
"DoubleAccumulatorTest", |
360 |
+ |
"DoubleAdderTest", |
361 |
+ |
"ForkJoinPool8Test", |
362 |
+ |
"ForkJoinTask8Test", |
363 |
+ |
"LongAccumulatorTest", |
364 |
+ |
"LongAdderTest", |
365 |
+ |
"SplittableRandomTest", |
366 |
+ |
"StampedLockTest", |
367 |
+ |
"ThreadLocalRandom8Test", |
368 |
+ |
}; |
369 |
+ |
addNamedTestClasses(suite, java8TestClassNames); |
370 |
+ |
} |
371 |
+ |
|
372 |
+ |
// Java9+ test classes |
373 |
+ |
if (atLeastJava9()) { |
374 |
+ |
String[] java9TestClassNames = { |
375 |
+ |
"ThreadPoolExecutor9Test", |
376 |
+ |
}; |
377 |
+ |
addNamedTestClasses(suite, java9TestClassNames); |
378 |
+ |
} |
379 |
+ |
|
380 |
+ |
return suite; |
381 |
|
} |
382 |
|
|
383 |
+ |
// Delays for timing-dependent tests, in milliseconds. |
384 |
|
|
385 |
|
public static long SHORT_DELAY_MS; |
386 |
|
public static long SMALL_DELAY_MS; |
387 |
|
public static long MEDIUM_DELAY_MS; |
388 |
|
public static long LONG_DELAY_MS; |
389 |
|
|
254 |
– |
|
390 |
|
/** |
391 |
|
* Returns the shortest timed delay. This could |
392 |
|
* be reimplemented to use for example a Property. |
469 |
|
|
470 |
|
if (Thread.interrupted()) |
471 |
|
throw new AssertionFailedError("interrupt status set in main thread"); |
472 |
+ |
|
473 |
+ |
checkForkJoinPoolThreadLeaks(); |
474 |
+ |
} |
475 |
+ |
|
476 |
+ |
/** |
477 |
+ |
* Finds missing try { ... } finally { joinPool(e); } |
478 |
+ |
*/ |
479 |
+ |
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
480 |
+ |
Thread[] survivors = new Thread[5]; |
481 |
+ |
int count = Thread.enumerate(survivors); |
482 |
+ |
for (int i = 0; i < count; i++) { |
483 |
+ |
Thread thread = survivors[i]; |
484 |
+ |
String name = thread.getName(); |
485 |
+ |
if (name.startsWith("ForkJoinPool-")) { |
486 |
+ |
// give thread some time to terminate |
487 |
+ |
thread.join(LONG_DELAY_MS); |
488 |
+ |
if (!thread.isAlive()) continue; |
489 |
+ |
throw new AssertionFailedError |
490 |
+ |
(String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n", |
491 |
+ |
toString(), name)); |
492 |
+ |
} |
493 |
+ |
} |
494 |
|
} |
495 |
|
|
496 |
|
/** |
571 |
|
public void threadAssertEquals(Object x, Object y) { |
572 |
|
try { |
573 |
|
assertEquals(x, y); |
574 |
< |
} catch (AssertionFailedError t) { |
575 |
< |
threadRecordFailure(t); |
576 |
< |
throw t; |
577 |
< |
} catch (Throwable t) { |
578 |
< |
threadUnexpectedException(t); |
574 |
> |
} catch (AssertionFailedError fail) { |
575 |
> |
threadRecordFailure(fail); |
576 |
> |
throw fail; |
577 |
> |
} catch (Throwable fail) { |
578 |
> |
threadUnexpectedException(fail); |
579 |
|
} |
580 |
|
} |
581 |
|
|
587 |
|
public void threadAssertSame(Object x, Object y) { |
588 |
|
try { |
589 |
|
assertSame(x, y); |
590 |
< |
} catch (AssertionFailedError t) { |
591 |
< |
threadRecordFailure(t); |
592 |
< |
throw t; |
590 |
> |
} catch (AssertionFailedError fail) { |
591 |
> |
threadRecordFailure(fail); |
592 |
> |
throw fail; |
593 |
|
} |
594 |
|
} |
595 |
|
|
654 |
|
void joinPool(ExecutorService exec) { |
655 |
|
try { |
656 |
|
exec.shutdown(); |
657 |
< |
assertTrue("ExecutorService did not terminate in a timely manner", |
658 |
< |
exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)); |
657 |
> |
if (!exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) |
658 |
> |
fail("ExecutorService " + exec + |
659 |
> |
" did not terminate in a timely manner"); |
660 |
|
} catch (SecurityException ok) { |
661 |
|
// Allowed in case test doesn't have privs |
662 |
< |
} catch (InterruptedException ie) { |
662 |
> |
} catch (InterruptedException fail) { |
663 |
|
fail("Unexpected InterruptedException"); |
664 |
|
} |
665 |
|
} |
666 |
|
|
667 |
|
/** |
668 |
+ |
* A debugging tool to print all stack traces, as jstack does. |
669 |
+ |
*/ |
670 |
+ |
static void printAllStackTraces() { |
671 |
+ |
for (ThreadInfo info : |
672 |
+ |
ManagementFactory.getThreadMXBean() |
673 |
+ |
.dumpAllThreads(true, true)) |
674 |
+ |
System.err.print(info); |
675 |
+ |
} |
676 |
+ |
|
677 |
+ |
/** |
678 |
|
* Checks that thread does not terminate within the default |
679 |
|
* millisecond delay of {@code timeoutMillis()}. |
680 |
|
*/ |
690 |
|
// No need to optimize the failing case via Thread.join. |
691 |
|
delay(millis); |
692 |
|
assertTrue(thread.isAlive()); |
693 |
< |
} catch (InterruptedException ie) { |
693 |
> |
} catch (InterruptedException fail) { |
694 |
> |
fail("Unexpected InterruptedException"); |
695 |
> |
} |
696 |
> |
} |
697 |
> |
|
698 |
> |
/** |
699 |
> |
* Checks that the threads do not terminate within the default |
700 |
> |
* millisecond delay of {@code timeoutMillis()}. |
701 |
> |
*/ |
702 |
> |
void assertThreadsStayAlive(Thread... threads) { |
703 |
> |
assertThreadsStayAlive(timeoutMillis(), threads); |
704 |
> |
} |
705 |
> |
|
706 |
> |
/** |
707 |
> |
* Checks that the threads do not terminate within the given millisecond delay. |
708 |
> |
*/ |
709 |
> |
void assertThreadsStayAlive(long millis, Thread... threads) { |
710 |
> |
try { |
711 |
> |
// No need to optimize the failing case via Thread.join. |
712 |
> |
delay(millis); |
713 |
> |
for (Thread thread : threads) |
714 |
> |
assertTrue(thread.isAlive()); |
715 |
> |
} catch (InterruptedException fail) { |
716 |
|
fail("Unexpected InterruptedException"); |
717 |
|
} |
718 |
|
} |
734 |
|
future.get(timeoutMillis, MILLISECONDS); |
735 |
|
shouldThrow(); |
736 |
|
} catch (TimeoutException success) { |
737 |
< |
} catch (Exception e) { |
738 |
< |
threadUnexpectedException(e); |
737 |
> |
} catch (Exception fail) { |
738 |
> |
threadUnexpectedException(fail); |
739 |
|
} finally { future.cancel(true); } |
740 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
741 |
|
} |
779 |
|
public static final Integer m6 = new Integer(-6); |
780 |
|
public static final Integer m10 = new Integer(-10); |
781 |
|
|
592 |
– |
|
782 |
|
/** |
783 |
|
* Runs Runnable r with a security policy that permits precisely |
784 |
|
* the specified permissions. If there is no current security |
790 |
|
SecurityManager sm = System.getSecurityManager(); |
791 |
|
if (sm == null) { |
792 |
|
r.run(); |
793 |
+ |
} |
794 |
+ |
runWithSecurityManagerWithPermissions(r, permissions); |
795 |
+ |
} |
796 |
+ |
|
797 |
+ |
/** |
798 |
+ |
* Runs Runnable r with a security policy that permits precisely |
799 |
+ |
* the specified permissions. If there is no current security |
800 |
+ |
* manager, a temporary one is set for the duration of the |
801 |
+ |
* Runnable. We require that any security manager permit |
802 |
+ |
* getPolicy/setPolicy. |
803 |
+ |
*/ |
804 |
+ |
public void runWithSecurityManagerWithPermissions(Runnable r, |
805 |
+ |
Permission... permissions) { |
806 |
+ |
SecurityManager sm = System.getSecurityManager(); |
807 |
+ |
if (sm == null) { |
808 |
|
Policy savedPolicy = Policy.getPolicy(); |
809 |
|
try { |
810 |
|
Policy.setPolicy(permissivePolicy()); |
811 |
|
System.setSecurityManager(new SecurityManager()); |
812 |
< |
runWithPermissions(r, permissions); |
812 |
> |
runWithSecurityManagerWithPermissions(r, permissions); |
813 |
|
} finally { |
814 |
|
System.setSecurityManager(null); |
815 |
|
Policy.setPolicy(savedPolicy); |
857 |
|
return perms.implies(p); |
858 |
|
} |
859 |
|
public void refresh() {} |
860 |
+ |
public String toString() { |
861 |
+ |
List<Permission> ps = new ArrayList<Permission>(); |
862 |
+ |
for (Enumeration<Permission> e = perms.elements(); e.hasMoreElements();) |
863 |
+ |
ps.add(e.nextElement()); |
864 |
+ |
return "AdjustablePolicy with permissions " + ps; |
865 |
+ |
} |
866 |
|
} |
867 |
|
|
868 |
|
/** |
891 |
|
void sleep(long millis) { |
892 |
|
try { |
893 |
|
delay(millis); |
894 |
< |
} catch (InterruptedException ie) { |
894 |
> |
} catch (InterruptedException fail) { |
895 |
|
AssertionFailedError afe = |
896 |
|
new AssertionFailedError("Unexpected InterruptedException"); |
897 |
< |
afe.initCause(ie); |
897 |
> |
afe.initCause(fail); |
898 |
|
throw afe; |
899 |
|
} |
900 |
|
} |
932 |
|
/** |
933 |
|
* Returns the number of milliseconds since time given by |
934 |
|
* startNanoTime, which must have been previously returned from a |
935 |
< |
* call to {@link System.nanoTime()}. |
935 |
> |
* call to {@link System#nanoTime()}. |
936 |
|
*/ |
937 |
< |
long millisElapsedSince(long startNanoTime) { |
937 |
> |
static long millisElapsedSince(long startNanoTime) { |
938 |
|
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
939 |
|
} |
940 |
|
|
941 |
+ |
// void assertTerminatesPromptly(long timeoutMillis, Runnable r) { |
942 |
+ |
// long startTime = System.nanoTime(); |
943 |
+ |
// try { |
944 |
+ |
// r.run(); |
945 |
+ |
// } catch (Throwable fail) { threadUnexpectedException(fail); } |
946 |
+ |
// if (millisElapsedSince(startTime) > timeoutMillis/2) |
947 |
+ |
// throw new AssertionFailedError("did not return promptly"); |
948 |
+ |
// } |
949 |
+ |
|
950 |
+ |
// void assertTerminatesPromptly(Runnable r) { |
951 |
+ |
// assertTerminatesPromptly(LONG_DELAY_MS/2, r); |
952 |
+ |
// } |
953 |
+ |
|
954 |
+ |
/** |
955 |
+ |
* Checks that timed f.get() returns the expected value, and does not |
956 |
+ |
* wait for the timeout to elapse before returning. |
957 |
+ |
*/ |
958 |
+ |
<T> void checkTimedGet(Future<T> f, T expectedValue, long timeoutMillis) { |
959 |
+ |
long startTime = System.nanoTime(); |
960 |
+ |
try { |
961 |
+ |
assertEquals(expectedValue, f.get(timeoutMillis, MILLISECONDS)); |
962 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
963 |
+ |
if (millisElapsedSince(startTime) > timeoutMillis/2) |
964 |
+ |
throw new AssertionFailedError("timed get did not return promptly"); |
965 |
+ |
} |
966 |
+ |
|
967 |
+ |
<T> void checkTimedGet(Future<T> f, T expectedValue) { |
968 |
+ |
checkTimedGet(f, expectedValue, LONG_DELAY_MS); |
969 |
+ |
} |
970 |
+ |
|
971 |
|
/** |
972 |
|
* Returns a new started daemon Thread running the given runnable. |
973 |
|
*/ |
986 |
|
void awaitTermination(Thread t, long timeoutMillis) { |
987 |
|
try { |
988 |
|
t.join(timeoutMillis); |
989 |
< |
} catch (InterruptedException ie) { |
990 |
< |
threadUnexpectedException(ie); |
989 |
> |
} catch (InterruptedException fail) { |
990 |
> |
threadUnexpectedException(fail); |
991 |
|
} finally { |
992 |
|
if (t.getState() != Thread.State.TERMINATED) { |
993 |
|
t.interrupt(); |
1013 |
|
public final void run() { |
1014 |
|
try { |
1015 |
|
realRun(); |
1016 |
< |
} catch (Throwable t) { |
1017 |
< |
threadUnexpectedException(t); |
1016 |
> |
} catch (Throwable fail) { |
1017 |
> |
threadUnexpectedException(fail); |
1018 |
|
} |
1019 |
|
} |
1020 |
|
} |
1068 |
|
threadShouldThrow("InterruptedException"); |
1069 |
|
} catch (InterruptedException success) { |
1070 |
|
threadAssertFalse(Thread.interrupted()); |
1071 |
< |
} catch (Throwable t) { |
1072 |
< |
threadUnexpectedException(t); |
1071 |
> |
} catch (Throwable fail) { |
1072 |
> |
threadUnexpectedException(fail); |
1073 |
|
} |
1074 |
|
} |
1075 |
|
} |
1080 |
|
public final T call() { |
1081 |
|
try { |
1082 |
|
return realCall(); |
1083 |
< |
} catch (Throwable t) { |
1084 |
< |
threadUnexpectedException(t); |
1083 |
> |
} catch (Throwable fail) { |
1084 |
> |
threadUnexpectedException(fail); |
1085 |
|
return null; |
1086 |
|
} |
1087 |
|
} |
1098 |
|
return result; |
1099 |
|
} catch (InterruptedException success) { |
1100 |
|
threadAssertFalse(Thread.interrupted()); |
1101 |
< |
} catch (Throwable t) { |
1102 |
< |
threadUnexpectedException(t); |
1101 |
> |
} catch (Throwable fail) { |
1102 |
> |
threadUnexpectedException(fail); |
1103 |
|
} |
1104 |
|
return null; |
1105 |
|
} |
1139 |
|
public void await(CountDownLatch latch) { |
1140 |
|
try { |
1141 |
|
assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); |
1142 |
< |
} catch (Throwable t) { |
1143 |
< |
threadUnexpectedException(t); |
1142 |
> |
} catch (Throwable fail) { |
1143 |
> |
threadUnexpectedException(fail); |
1144 |
|
} |
1145 |
|
} |
1146 |
|
|
1147 |
|
public void await(Semaphore semaphore) { |
1148 |
|
try { |
1149 |
|
assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
1150 |
< |
} catch (Throwable t) { |
1151 |
< |
threadUnexpectedException(t); |
1150 |
> |
} catch (Throwable fail) { |
1151 |
> |
threadUnexpectedException(fail); |
1152 |
|
} |
1153 |
|
} |
1154 |
|
|
1339 |
|
public abstract class CheckedRecursiveAction extends RecursiveAction { |
1340 |
|
protected abstract void realCompute() throws Throwable; |
1341 |
|
|
1342 |
< |
public final void compute() { |
1342 |
> |
@Override protected final void compute() { |
1343 |
|
try { |
1344 |
|
realCompute(); |
1345 |
< |
} catch (Throwable t) { |
1346 |
< |
threadUnexpectedException(t); |
1345 |
> |
} catch (Throwable fail) { |
1346 |
> |
threadUnexpectedException(fail); |
1347 |
|
} |
1348 |
|
} |
1349 |
|
} |
1354 |
|
public abstract class CheckedRecursiveTask<T> extends RecursiveTask<T> { |
1355 |
|
protected abstract T realCompute() throws Throwable; |
1356 |
|
|
1357 |
< |
public final T compute() { |
1357 |
> |
@Override protected final T compute() { |
1358 |
|
try { |
1359 |
|
return realCompute(); |
1360 |
< |
} catch (Throwable t) { |
1361 |
< |
threadUnexpectedException(t); |
1360 |
> |
} catch (Throwable fail) { |
1361 |
> |
threadUnexpectedException(fail); |
1362 |
|
return null; |
1363 |
|
} |
1364 |
|
} |
1382 |
|
public int await() { |
1383 |
|
try { |
1384 |
|
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1385 |
< |
} catch (TimeoutException e) { |
1385 |
> |
} catch (TimeoutException timedOut) { |
1386 |
|
throw new AssertionFailedError("timed out"); |
1387 |
< |
} catch (Exception e) { |
1387 |
> |
} catch (Exception fail) { |
1388 |
|
AssertionFailedError afe = |
1389 |
< |
new AssertionFailedError("Unexpected exception: " + e); |
1390 |
< |
afe.initCause(e); |
1389 |
> |
new AssertionFailedError("Unexpected exception: " + fail); |
1390 |
> |
afe.initCause(fail); |
1391 |
|
throw afe; |
1392 |
|
} |
1393 |
|
} |
1415 |
|
q.remove(); |
1416 |
|
shouldThrow(); |
1417 |
|
} catch (NoSuchElementException success) {} |
1418 |
< |
} catch (InterruptedException ie) { |
1179 |
< |
threadUnexpectedException(ie); |
1180 |
< |
} |
1418 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
1419 |
|
} |
1420 |
|
|
1421 |
< |
@SuppressWarnings("unchecked") |
1422 |
< |
<T> T serialClone(T o) { |
1421 |
> |
void assertSerialEquals(Object x, Object y) { |
1422 |
> |
assertTrue(Arrays.equals(serialBytes(x), serialBytes(y))); |
1423 |
> |
} |
1424 |
> |
|
1425 |
> |
void assertNotSerialEquals(Object x, Object y) { |
1426 |
> |
assertFalse(Arrays.equals(serialBytes(x), serialBytes(y))); |
1427 |
> |
} |
1428 |
> |
|
1429 |
> |
byte[] serialBytes(Object o) { |
1430 |
|
try { |
1431 |
|
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1432 |
|
ObjectOutputStream oos = new ObjectOutputStream(bos); |
1433 |
|
oos.writeObject(o); |
1434 |
|
oos.flush(); |
1435 |
|
oos.close(); |
1436 |
+ |
return bos.toByteArray(); |
1437 |
+ |
} catch (Throwable fail) { |
1438 |
+ |
threadUnexpectedException(fail); |
1439 |
+ |
return new byte[0]; |
1440 |
+ |
} |
1441 |
+ |
} |
1442 |
+ |
|
1443 |
+ |
@SuppressWarnings("unchecked") |
1444 |
+ |
<T> T serialClone(T o) { |
1445 |
+ |
try { |
1446 |
|
ObjectInputStream ois = new ObjectInputStream |
1447 |
< |
(new ByteArrayInputStream(bos.toByteArray())); |
1447 |
> |
(new ByteArrayInputStream(serialBytes(o))); |
1448 |
|
T clone = (T) ois.readObject(); |
1449 |
|
assertSame(o.getClass(), clone.getClass()); |
1450 |
|
return clone; |
1451 |
< |
} catch (Throwable t) { |
1452 |
< |
threadUnexpectedException(t); |
1451 |
> |
} catch (Throwable fail) { |
1452 |
> |
threadUnexpectedException(fail); |
1453 |
|
return null; |
1454 |
|
} |
1455 |
|
} |
1456 |
+ |
|
1457 |
+ |
public void assertThrows(Class<? extends Throwable> expectedExceptionClass, |
1458 |
+ |
Runnable... throwingActions) { |
1459 |
+ |
for (Runnable throwingAction : throwingActions) { |
1460 |
+ |
boolean threw = false; |
1461 |
+ |
try { throwingAction.run(); } |
1462 |
+ |
catch (Throwable t) { |
1463 |
+ |
threw = true; |
1464 |
+ |
if (!expectedExceptionClass.isInstance(t)) { |
1465 |
+ |
AssertionFailedError afe = |
1466 |
+ |
new AssertionFailedError |
1467 |
+ |
("Expected " + expectedExceptionClass.getName() + |
1468 |
+ |
", got " + t.getClass().getName()); |
1469 |
+ |
afe.initCause(t); |
1470 |
+ |
threadUnexpectedException(afe); |
1471 |
+ |
} |
1472 |
+ |
} |
1473 |
+ |
if (!threw) |
1474 |
+ |
shouldThrow(expectedExceptionClass.getName()); |
1475 |
+ |
} |
1476 |
+ |
} |
1477 |
+ |
|
1478 |
+ |
public void assertIteratorExhausted(Iterator<?> it) { |
1479 |
+ |
try { |
1480 |
+ |
it.next(); |
1481 |
+ |
shouldThrow(); |
1482 |
+ |
} catch (NoSuchElementException success) {} |
1483 |
+ |
assertFalse(it.hasNext()); |
1484 |
+ |
} |
1485 |
|
} |