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.lang.management.ManagementFactory; |
17 |
+ |
import java.lang.management.ThreadInfo; |
18 |
+ |
import java.lang.reflect.Constructor; |
19 |
+ |
import java.lang.reflect.Method; |
20 |
+ |
import java.lang.reflect.Modifier; |
21 |
+ |
import java.security.CodeSource; |
22 |
+ |
import java.security.Permission; |
23 |
+ |
import java.security.PermissionCollection; |
24 |
+ |
import java.security.Permissions; |
25 |
+ |
import java.security.Policy; |
26 |
+ |
import java.security.ProtectionDomain; |
27 |
+ |
import java.security.SecurityPermission; |
28 |
|
import java.util.ArrayList; |
29 |
|
import java.util.Arrays; |
30 |
|
import java.util.Date; |
31 |
|
import java.util.Enumeration; |
32 |
+ |
import java.util.Iterator; |
33 |
|
import java.util.List; |
34 |
|
import java.util.NoSuchElementException; |
35 |
|
import java.util.PropertyPermission; |
36 |
< |
import java.util.concurrent.*; |
37 |
< |
import java.util.concurrent.atomic.AtomicBoolean; |
36 |
> |
import java.util.concurrent.BlockingQueue; |
37 |
> |
import java.util.concurrent.Callable; |
38 |
> |
import java.util.concurrent.CountDownLatch; |
39 |
> |
import java.util.concurrent.CyclicBarrier; |
40 |
> |
import java.util.concurrent.ExecutionException; |
41 |
> |
import java.util.concurrent.Executors; |
42 |
> |
import java.util.concurrent.ExecutorService; |
43 |
> |
import java.util.concurrent.Future; |
44 |
> |
import java.util.concurrent.RecursiveAction; |
45 |
> |
import java.util.concurrent.RecursiveTask; |
46 |
> |
import java.util.concurrent.RejectedExecutionHandler; |
47 |
> |
import java.util.concurrent.Semaphore; |
48 |
> |
import java.util.concurrent.ThreadFactory; |
49 |
> |
import java.util.concurrent.ThreadPoolExecutor; |
50 |
> |
import java.util.concurrent.TimeoutException; |
51 |
|
import java.util.concurrent.atomic.AtomicReference; |
52 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
53 |
< |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
54 |
< |
import java.security.CodeSource; |
55 |
< |
import java.security.Permission; |
56 |
< |
import java.security.PermissionCollection; |
57 |
< |
import java.security.Permissions; |
58 |
< |
import java.security.Policy; |
31 |
< |
import java.security.ProtectionDomain; |
32 |
< |
import java.security.SecurityPermission; |
52 |
> |
import java.util.regex.Pattern; |
53 |
> |
|
54 |
> |
import junit.framework.AssertionFailedError; |
55 |
> |
import junit.framework.Test; |
56 |
> |
import junit.framework.TestCase; |
57 |
> |
import junit.framework.TestResult; |
58 |
> |
import junit.framework.TestSuite; |
59 |
|
|
60 |
|
/** |
61 |
|
* Base class for JSR166 Junit TCK tests. Defines some constants, |
138 |
|
Boolean.getBoolean("jsr166.expensiveTests"); |
139 |
|
|
140 |
|
/** |
141 |
+ |
* If true, also run tests that are not part of the official tck |
142 |
+ |
* because they test unspecified implementation details. |
143 |
+ |
*/ |
144 |
+ |
protected static final boolean testImplementationDetails = |
145 |
+ |
Boolean.getBoolean("jsr166.testImplementationDetails"); |
146 |
+ |
|
147 |
+ |
/** |
148 |
|
* If true, report on stdout all "slow" tests, that is, ones that |
149 |
|
* take more than profileThreshold milliseconds to execute. |
150 |
|
*/ |
158 |
|
private static final long profileThreshold = |
159 |
|
Long.getLong("jsr166.profileThreshold", 100); |
160 |
|
|
161 |
+ |
/** |
162 |
+ |
* The number of repetitions per test (for tickling rare bugs). |
163 |
+ |
*/ |
164 |
+ |
private static final int runsPerTest = |
165 |
+ |
Integer.getInteger("jsr166.runsPerTest", 1); |
166 |
+ |
|
167 |
+ |
/** |
168 |
+ |
* The number of repetitions of the test suite (for finding leaks?). |
169 |
+ |
*/ |
170 |
+ |
private static final int suiteRuns = |
171 |
+ |
Integer.getInteger("jsr166.suiteRuns", 1); |
172 |
+ |
|
173 |
+ |
public JSR166TestCase() { super(); } |
174 |
+ |
public JSR166TestCase(String name) { super(name); } |
175 |
+ |
|
176 |
+ |
/** |
177 |
+ |
* A filter for tests to run, matching strings of the form |
178 |
+ |
* methodName(className), e.g. "testInvokeAll5(ForkJoinPoolTest)" |
179 |
+ |
* Usefully combined with jsr166.runsPerTest. |
180 |
+ |
*/ |
181 |
+ |
private static final Pattern methodFilter = methodFilter(); |
182 |
+ |
|
183 |
+ |
private static Pattern methodFilter() { |
184 |
+ |
String regex = System.getProperty("jsr166.methodFilter"); |
185 |
+ |
return (regex == null) ? null : Pattern.compile(regex); |
186 |
+ |
} |
187 |
+ |
|
188 |
|
protected void runTest() throws Throwable { |
189 |
< |
if (profileTests) |
190 |
< |
runTestProfiled(); |
191 |
< |
else |
192 |
< |
super.runTest(); |
189 |
> |
if (methodFilter == null |
190 |
> |
|| methodFilter.matcher(toString()).find()) { |
191 |
> |
for (int i = 0; i < runsPerTest; i++) { |
192 |
> |
if (profileTests) |
193 |
> |
runTestProfiled(); |
194 |
> |
else |
195 |
> |
super.runTest(); |
196 |
> |
} |
197 |
> |
} |
198 |
|
} |
199 |
|
|
200 |
|
protected void runTestProfiled() throws Throwable { |
201 |
+ |
// Warmup run, notably to trigger all needed classloading. |
202 |
+ |
super.runTest(); |
203 |
|
long t0 = System.nanoTime(); |
204 |
|
try { |
205 |
|
super.runTest(); |
206 |
|
} finally { |
207 |
< |
long elapsedMillis = |
141 |
< |
(System.nanoTime() - t0) / (1000L * 1000L); |
207 |
> |
long elapsedMillis = millisElapsedSince(t0); |
208 |
|
if (elapsedMillis >= profileThreshold) |
209 |
|
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); |
210 |
|
} |
211 |
|
} |
212 |
|
|
213 |
|
/** |
214 |
< |
* Runs all JSR166 unit tests using junit.textui.TestRunner |
214 |
> |
* Runs all JSR166 unit tests using junit.textui.TestRunner. |
215 |
|
*/ |
216 |
|
public static void main(String[] args) { |
217 |
+ |
main(suite(), args); |
218 |
+ |
} |
219 |
+ |
|
220 |
+ |
/** |
221 |
+ |
* Runs all unit tests in the given test suite. |
222 |
+ |
* Actual behavior influenced by jsr166.* system properties. |
223 |
+ |
*/ |
224 |
+ |
static void main(Test suite, String[] args) { |
225 |
|
if (useSecurityManager) { |
226 |
|
System.err.println("Setting a permissive security manager"); |
227 |
|
Policy.setPolicy(permissivePolicy()); |
228 |
|
System.setSecurityManager(new SecurityManager()); |
229 |
|
} |
230 |
< |
int iters = (args.length == 0) ? 1 : Integer.parseInt(args[0]); |
231 |
< |
|
232 |
< |
Test s = suite(); |
233 |
< |
for (int i = 0; i < iters; ++i) { |
160 |
< |
junit.textui.TestRunner.run(s); |
230 |
> |
for (int i = 0; i < suiteRuns; i++) { |
231 |
> |
TestResult result = junit.textui.TestRunner.run(suite); |
232 |
> |
if (!result.wasSuccessful()) |
233 |
> |
System.exit(1); |
234 |
|
System.gc(); |
235 |
|
System.runFinalization(); |
236 |
|
} |
164 |
– |
System.exit(0); |
237 |
|
} |
238 |
|
|
239 |
|
public static TestSuite newTestSuite(Object... suiteOrClasses) { |
249 |
|
return suite; |
250 |
|
} |
251 |
|
|
252 |
+ |
public static void addNamedTestClasses(TestSuite suite, |
253 |
+ |
String... testClassNames) { |
254 |
+ |
for (String testClassName : testClassNames) { |
255 |
+ |
try { |
256 |
+ |
Class<?> testClass = Class.forName(testClassName); |
257 |
+ |
Method m = testClass.getDeclaredMethod("suite", |
258 |
+ |
new Class<?>[0]); |
259 |
+ |
suite.addTest(newTestSuite((Test)m.invoke(null))); |
260 |
+ |
} catch (Exception e) { |
261 |
+ |
throw new Error("Missing test class", e); |
262 |
+ |
} |
263 |
+ |
} |
264 |
+ |
} |
265 |
+ |
|
266 |
+ |
public static final double JAVA_CLASS_VERSION; |
267 |
+ |
public static final String JAVA_SPECIFICATION_VERSION; |
268 |
+ |
static { |
269 |
+ |
try { |
270 |
+ |
JAVA_CLASS_VERSION = java.security.AccessController.doPrivileged( |
271 |
+ |
new java.security.PrivilegedAction<Double>() { |
272 |
+ |
public Double run() { |
273 |
+ |
return Double.valueOf(System.getProperty("java.class.version"));}}); |
274 |
+ |
JAVA_SPECIFICATION_VERSION = java.security.AccessController.doPrivileged( |
275 |
+ |
new java.security.PrivilegedAction<String>() { |
276 |
+ |
public String run() { |
277 |
+ |
return System.getProperty("java.specification.version");}}); |
278 |
+ |
} catch (Throwable t) { |
279 |
+ |
throw new Error(t); |
280 |
+ |
} |
281 |
+ |
} |
282 |
+ |
|
283 |
+ |
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
284 |
+ |
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
285 |
+ |
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
286 |
+ |
public static boolean atLeastJava9() { |
287 |
+ |
return JAVA_CLASS_VERSION >= 53.0 |
288 |
+ |
// As of 2015-09, java9 still uses 52.0 class file version |
289 |
+ |
|| JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?(9|[0-9][0-9])$"); |
290 |
+ |
} |
291 |
+ |
public static boolean atLeastJava10() { |
292 |
+ |
return JAVA_CLASS_VERSION >= 54.0 |
293 |
+ |
|| JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?[0-9][0-9]$"); |
294 |
+ |
} |
295 |
+ |
|
296 |
|
/** |
297 |
|
* Collects all JSR166 unit tests as one suite. |
298 |
|
*/ |
299 |
|
public static Test suite() { |
300 |
< |
return newTestSuite( |
300 |
> |
// Java7+ test classes |
301 |
> |
TestSuite suite = newTestSuite( |
302 |
|
ForkJoinPoolTest.suite(), |
303 |
|
ForkJoinTaskTest.suite(), |
304 |
|
RecursiveActionTest.suite(), |
363 |
|
TreeSetTest.suite(), |
364 |
|
TreeSubMapTest.suite(), |
365 |
|
TreeSubSetTest.suite()); |
366 |
+ |
|
367 |
+ |
// Java8+ test classes |
368 |
+ |
if (atLeastJava8()) { |
369 |
+ |
String[] java8TestClassNames = { |
370 |
+ |
"Atomic8Test", |
371 |
+ |
"CompletableFutureTest", |
372 |
+ |
"ConcurrentHashMap8Test", |
373 |
+ |
"CountedCompleterTest", |
374 |
+ |
"DoubleAccumulatorTest", |
375 |
+ |
"DoubleAdderTest", |
376 |
+ |
"ForkJoinPool8Test", |
377 |
+ |
"ForkJoinTask8Test", |
378 |
+ |
"LongAccumulatorTest", |
379 |
+ |
"LongAdderTest", |
380 |
+ |
"SplittableRandomTest", |
381 |
+ |
"StampedLockTest", |
382 |
+ |
"ThreadLocalRandom8Test", |
383 |
+ |
}; |
384 |
+ |
addNamedTestClasses(suite, java8TestClassNames); |
385 |
+ |
} |
386 |
+ |
|
387 |
+ |
// Java9+ test classes |
388 |
+ |
if (atLeastJava9()) { |
389 |
+ |
String[] java9TestClassNames = { |
390 |
+ |
// Currently empty |
391 |
+ |
}; |
392 |
+ |
addNamedTestClasses(suite, java9TestClassNames); |
393 |
+ |
} |
394 |
+ |
|
395 |
+ |
return suite; |
396 |
+ |
} |
397 |
+ |
|
398 |
+ |
/** Returns list of junit-style test method names in given class. */ |
399 |
+ |
public static ArrayList<String> testMethodNames(Class<?> testClass) { |
400 |
+ |
Method[] methods = testClass.getDeclaredMethods(); |
401 |
+ |
ArrayList<String> names = new ArrayList<String>(methods.length); |
402 |
+ |
for (Method method : methods) { |
403 |
+ |
if (method.getName().startsWith("test") |
404 |
+ |
&& Modifier.isPublic(method.getModifiers()) |
405 |
+ |
// method.getParameterCount() requires jdk8+ |
406 |
+ |
&& method.getParameterTypes().length == 0) { |
407 |
+ |
names.add(method.getName()); |
408 |
+ |
} |
409 |
+ |
} |
410 |
+ |
return names; |
411 |
+ |
} |
412 |
+ |
|
413 |
+ |
/** |
414 |
+ |
* Returns junit-style testSuite for the given test class, but |
415 |
+ |
* parameterized by passing extra data to each test. |
416 |
+ |
*/ |
417 |
+ |
public static <ExtraData> Test parameterizedTestSuite |
418 |
+ |
(Class<? extends JSR166TestCase> testClass, |
419 |
+ |
Class<ExtraData> dataClass, |
420 |
+ |
ExtraData data) { |
421 |
+ |
try { |
422 |
+ |
TestSuite suite = new TestSuite(); |
423 |
+ |
Constructor c = |
424 |
+ |
testClass.getDeclaredConstructor(dataClass, String.class); |
425 |
+ |
for (String methodName : testMethodNames(testClass)) |
426 |
+ |
suite.addTest((Test) c.newInstance(data, methodName)); |
427 |
+ |
return suite; |
428 |
+ |
} catch (Exception e) { |
429 |
+ |
throw new Error(e); |
430 |
+ |
} |
431 |
|
} |
432 |
|
|
433 |
+ |
/** |
434 |
+ |
* Returns junit-style testSuite for the jdk8 extension of the |
435 |
+ |
* given test class, but parameterized by passing extra data to |
436 |
+ |
* each test. Uses reflection to allow compilation in jdk7. |
437 |
+ |
*/ |
438 |
+ |
public static <ExtraData> Test jdk8ParameterizedTestSuite |
439 |
+ |
(Class<? extends JSR166TestCase> testClass, |
440 |
+ |
Class<ExtraData> dataClass, |
441 |
+ |
ExtraData data) { |
442 |
+ |
if (atLeastJava8()) { |
443 |
+ |
String name = testClass.getName(); |
444 |
+ |
String name8 = name.replaceAll("Test$", "8Test"); |
445 |
+ |
if (name.equals(name8)) throw new Error(name); |
446 |
+ |
try { |
447 |
+ |
return (Test) |
448 |
+ |
Class.forName(name8) |
449 |
+ |
.getMethod("testSuite", new Class[] { dataClass }) |
450 |
+ |
.invoke(null, data); |
451 |
+ |
} catch (Exception e) { |
452 |
+ |
throw new Error(e); |
453 |
+ |
} |
454 |
+ |
} else { |
455 |
+ |
return new TestSuite(); |
456 |
+ |
} |
457 |
+ |
|
458 |
+ |
} |
459 |
+ |
|
460 |
+ |
// Delays for timing-dependent tests, in milliseconds. |
461 |
|
|
462 |
|
public static long SHORT_DELAY_MS; |
463 |
|
public static long SMALL_DELAY_MS; |
464 |
|
public static long MEDIUM_DELAY_MS; |
465 |
|
public static long LONG_DELAY_MS; |
466 |
|
|
257 |
– |
|
467 |
|
/** |
468 |
|
* Returns the shortest timed delay. This could |
469 |
|
* be reimplemented to use for example a Property. |
491 |
|
} |
492 |
|
|
493 |
|
/** |
494 |
< |
* Returns a new Date instance representing a time delayMillis |
495 |
< |
* milliseconds in the future. |
494 |
> |
* Returns a new Date instance representing a time at least |
495 |
> |
* delayMillis milliseconds in the future. |
496 |
|
*/ |
497 |
|
Date delayedDate(long delayMillis) { |
498 |
< |
return new Date(System.currentTimeMillis() + delayMillis); |
498 |
> |
// Add 1 because currentTimeMillis is known to round into the past. |
499 |
> |
return new Date(System.currentTimeMillis() + delayMillis + 1); |
500 |
|
} |
501 |
|
|
502 |
|
/** |
547 |
|
|
548 |
|
if (Thread.interrupted()) |
549 |
|
throw new AssertionFailedError("interrupt status set in main thread"); |
550 |
+ |
|
551 |
+ |
checkForkJoinPoolThreadLeaks(); |
552 |
+ |
} |
553 |
+ |
|
554 |
+ |
/** |
555 |
+ |
* Finds missing try { ... } finally { joinPool(e); } |
556 |
+ |
*/ |
557 |
+ |
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
558 |
+ |
Thread[] survivors = new Thread[5]; |
559 |
+ |
int count = Thread.enumerate(survivors); |
560 |
+ |
for (int i = 0; i < count; i++) { |
561 |
+ |
Thread thread = survivors[i]; |
562 |
+ |
String name = thread.getName(); |
563 |
+ |
if (name.startsWith("ForkJoinPool-")) { |
564 |
+ |
// give thread some time to terminate |
565 |
+ |
thread.join(LONG_DELAY_MS); |
566 |
+ |
if (!thread.isAlive()) continue; |
567 |
+ |
throw new AssertionFailedError |
568 |
+ |
(String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n", |
569 |
+ |
toString(), name)); |
570 |
+ |
} |
571 |
+ |
} |
572 |
|
} |
573 |
|
|
574 |
|
/** |
649 |
|
public void threadAssertEquals(Object x, Object y) { |
650 |
|
try { |
651 |
|
assertEquals(x, y); |
652 |
< |
} catch (AssertionFailedError t) { |
653 |
< |
threadRecordFailure(t); |
654 |
< |
throw t; |
655 |
< |
} catch (Throwable t) { |
656 |
< |
threadUnexpectedException(t); |
652 |
> |
} catch (AssertionFailedError fail) { |
653 |
> |
threadRecordFailure(fail); |
654 |
> |
throw fail; |
655 |
> |
} catch (Throwable fail) { |
656 |
> |
threadUnexpectedException(fail); |
657 |
|
} |
658 |
|
} |
659 |
|
|
665 |
|
public void threadAssertSame(Object x, Object y) { |
666 |
|
try { |
667 |
|
assertSame(x, y); |
668 |
< |
} catch (AssertionFailedError t) { |
669 |
< |
threadRecordFailure(t); |
670 |
< |
throw t; |
668 |
> |
} catch (AssertionFailedError fail) { |
669 |
> |
threadRecordFailure(fail); |
670 |
> |
throw fail; |
671 |
|
} |
672 |
|
} |
673 |
|
|
729 |
|
/** |
730 |
|
* Waits out termination of a thread pool or fails doing so. |
731 |
|
*/ |
732 |
< |
void joinPool(ExecutorService exec) { |
732 |
> |
void joinPool(ExecutorService pool) { |
733 |
|
try { |
734 |
< |
exec.shutdown(); |
735 |
< |
assertTrue("ExecutorService did not terminate in a timely manner", |
736 |
< |
exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)); |
734 |
> |
pool.shutdown(); |
735 |
> |
if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) |
736 |
> |
fail("ExecutorService " + pool + |
737 |
> |
" did not terminate in a timely manner"); |
738 |
|
} catch (SecurityException ok) { |
739 |
|
// Allowed in case test doesn't have privs |
740 |
< |
} catch (InterruptedException ie) { |
740 |
> |
} catch (InterruptedException fail) { |
741 |
|
fail("Unexpected InterruptedException"); |
742 |
|
} |
743 |
|
} |
744 |
|
|
745 |
+ |
/** Like Runnable, but with the freedom to throw anything */ |
746 |
+ |
interface Thunk { public void run() throws Throwable; } |
747 |
+ |
|
748 |
+ |
/** |
749 |
+ |
* Runs all the given tasks in parallel, failing if any fail. |
750 |
+ |
* Useful for running multiple variants of tests that are |
751 |
+ |
* necessarily individually slow because they must block. |
752 |
+ |
*/ |
753 |
+ |
void testInParallel(Thunk ... thunks) { |
754 |
+ |
ExecutorService pool = Executors.newCachedThreadPool(); |
755 |
+ |
try { |
756 |
+ |
ArrayList<Future<?>> futures = new ArrayList<>(thunks.length); |
757 |
+ |
for (final Thunk thunk : thunks) |
758 |
+ |
futures.add(pool.submit(new CheckedRunnable() { |
759 |
+ |
public void realRun() throws Throwable { thunk.run();}})); |
760 |
+ |
for (Future<?> future : futures) |
761 |
+ |
try { |
762 |
+ |
assertNull(future.get(LONG_DELAY_MS, MILLISECONDS)); |
763 |
+ |
} catch (ExecutionException ex) { |
764 |
+ |
threadUnexpectedException(ex.getCause()); |
765 |
+ |
} catch (Exception ex) { |
766 |
+ |
threadUnexpectedException(ex); |
767 |
+ |
} |
768 |
+ |
} finally { |
769 |
+ |
joinPool(pool); |
770 |
+ |
} |
771 |
+ |
} |
772 |
+ |
|
773 |
+ |
/** |
774 |
+ |
* A debugging tool to print all stack traces, as jstack does. |
775 |
+ |
*/ |
776 |
+ |
static void printAllStackTraces() { |
777 |
+ |
for (ThreadInfo info : |
778 |
+ |
ManagementFactory.getThreadMXBean() |
779 |
+ |
.dumpAllThreads(true, true)) |
780 |
+ |
System.err.print(info); |
781 |
+ |
} |
782 |
+ |
|
783 |
|
/** |
784 |
|
* Checks that thread does not terminate within the default |
785 |
|
* millisecond delay of {@code timeoutMillis()}. |
796 |
|
// No need to optimize the failing case via Thread.join. |
797 |
|
delay(millis); |
798 |
|
assertTrue(thread.isAlive()); |
799 |
< |
} catch (InterruptedException ie) { |
799 |
> |
} catch (InterruptedException fail) { |
800 |
|
fail("Unexpected InterruptedException"); |
801 |
|
} |
802 |
|
} |
818 |
|
delay(millis); |
819 |
|
for (Thread thread : threads) |
820 |
|
assertTrue(thread.isAlive()); |
821 |
< |
} catch (InterruptedException ie) { |
821 |
> |
} catch (InterruptedException fail) { |
822 |
|
fail("Unexpected InterruptedException"); |
823 |
|
} |
824 |
|
} |
840 |
|
future.get(timeoutMillis, MILLISECONDS); |
841 |
|
shouldThrow(); |
842 |
|
} catch (TimeoutException success) { |
843 |
< |
} catch (Exception e) { |
844 |
< |
threadUnexpectedException(e); |
843 |
> |
} catch (Exception fail) { |
844 |
> |
threadUnexpectedException(fail); |
845 |
|
} finally { future.cancel(true); } |
846 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
847 |
|
} |
885 |
|
public static final Integer m6 = new Integer(-6); |
886 |
|
public static final Integer m10 = new Integer(-10); |
887 |
|
|
617 |
– |
|
888 |
|
/** |
889 |
|
* Runs Runnable r with a security policy that permits precisely |
890 |
|
* the specified permissions. If there is no current security |
997 |
|
void sleep(long millis) { |
998 |
|
try { |
999 |
|
delay(millis); |
1000 |
< |
} catch (InterruptedException ie) { |
1000 |
> |
} catch (InterruptedException fail) { |
1001 |
|
AssertionFailedError afe = |
1002 |
|
new AssertionFailedError("Unexpected InterruptedException"); |
1003 |
< |
afe.initCause(ie); |
1003 |
> |
afe.initCause(fail); |
1004 |
|
throw afe; |
1005 |
|
} |
1006 |
|
} |
1038 |
|
/** |
1039 |
|
* Returns the number of milliseconds since time given by |
1040 |
|
* startNanoTime, which must have been previously returned from a |
1041 |
< |
* call to {@link System.nanoTime()}. |
1041 |
> |
* call to {@link System#nanoTime()}. |
1042 |
|
*/ |
1043 |
< |
long millisElapsedSince(long startNanoTime) { |
1043 |
> |
static long millisElapsedSince(long startNanoTime) { |
1044 |
|
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
1045 |
|
} |
1046 |
|
|
1047 |
+ |
// void assertTerminatesPromptly(long timeoutMillis, Runnable r) { |
1048 |
+ |
// long startTime = System.nanoTime(); |
1049 |
+ |
// try { |
1050 |
+ |
// r.run(); |
1051 |
+ |
// } catch (Throwable fail) { threadUnexpectedException(fail); } |
1052 |
+ |
// if (millisElapsedSince(startTime) > timeoutMillis/2) |
1053 |
+ |
// throw new AssertionFailedError("did not return promptly"); |
1054 |
+ |
// } |
1055 |
+ |
|
1056 |
+ |
// void assertTerminatesPromptly(Runnable r) { |
1057 |
+ |
// assertTerminatesPromptly(LONG_DELAY_MS/2, r); |
1058 |
+ |
// } |
1059 |
+ |
|
1060 |
+ |
/** |
1061 |
+ |
* Checks that timed f.get() returns the expected value, and does not |
1062 |
+ |
* wait for the timeout to elapse before returning. |
1063 |
+ |
*/ |
1064 |
+ |
<T> void checkTimedGet(Future<T> f, T expectedValue, long timeoutMillis) { |
1065 |
+ |
long startTime = System.nanoTime(); |
1066 |
+ |
try { |
1067 |
+ |
assertEquals(expectedValue, f.get(timeoutMillis, MILLISECONDS)); |
1068 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
1069 |
+ |
if (millisElapsedSince(startTime) > timeoutMillis/2) |
1070 |
+ |
throw new AssertionFailedError("timed get did not return promptly"); |
1071 |
+ |
} |
1072 |
+ |
|
1073 |
+ |
<T> void checkTimedGet(Future<T> f, T expectedValue) { |
1074 |
+ |
checkTimedGet(f, expectedValue, LONG_DELAY_MS); |
1075 |
+ |
} |
1076 |
+ |
|
1077 |
|
/** |
1078 |
|
* Returns a new started daemon Thread running the given runnable. |
1079 |
|
*/ |
1092 |
|
void awaitTermination(Thread t, long timeoutMillis) { |
1093 |
|
try { |
1094 |
|
t.join(timeoutMillis); |
1095 |
< |
} catch (InterruptedException ie) { |
1096 |
< |
threadUnexpectedException(ie); |
1095 |
> |
} catch (InterruptedException fail) { |
1096 |
> |
threadUnexpectedException(fail); |
1097 |
|
} finally { |
1098 |
|
if (t.getState() != Thread.State.TERMINATED) { |
1099 |
|
t.interrupt(); |
1119 |
|
public final void run() { |
1120 |
|
try { |
1121 |
|
realRun(); |
1122 |
< |
} catch (Throwable t) { |
1123 |
< |
threadUnexpectedException(t); |
1122 |
> |
} catch (Throwable fail) { |
1123 |
> |
threadUnexpectedException(fail); |
1124 |
|
} |
1125 |
|
} |
1126 |
|
} |
1174 |
|
threadShouldThrow("InterruptedException"); |
1175 |
|
} catch (InterruptedException success) { |
1176 |
|
threadAssertFalse(Thread.interrupted()); |
1177 |
< |
} catch (Throwable t) { |
1178 |
< |
threadUnexpectedException(t); |
1177 |
> |
} catch (Throwable fail) { |
1178 |
> |
threadUnexpectedException(fail); |
1179 |
|
} |
1180 |
|
} |
1181 |
|
} |
1186 |
|
public final T call() { |
1187 |
|
try { |
1188 |
|
return realCall(); |
1189 |
< |
} catch (Throwable t) { |
1190 |
< |
threadUnexpectedException(t); |
1189 |
> |
} catch (Throwable fail) { |
1190 |
> |
threadUnexpectedException(fail); |
1191 |
|
return null; |
1192 |
|
} |
1193 |
|
} |
1204 |
|
return result; |
1205 |
|
} catch (InterruptedException success) { |
1206 |
|
threadAssertFalse(Thread.interrupted()); |
1207 |
< |
} catch (Throwable t) { |
1208 |
< |
threadUnexpectedException(t); |
1207 |
> |
} catch (Throwable fail) { |
1208 |
> |
threadUnexpectedException(fail); |
1209 |
|
} |
1210 |
|
return null; |
1211 |
|
} |
1245 |
|
public void await(CountDownLatch latch) { |
1246 |
|
try { |
1247 |
|
assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); |
1248 |
< |
} catch (Throwable t) { |
1249 |
< |
threadUnexpectedException(t); |
1248 |
> |
} catch (Throwable fail) { |
1249 |
> |
threadUnexpectedException(fail); |
1250 |
|
} |
1251 |
|
} |
1252 |
|
|
1253 |
|
public void await(Semaphore semaphore) { |
1254 |
|
try { |
1255 |
|
assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
1256 |
< |
} catch (Throwable t) { |
1257 |
< |
threadUnexpectedException(t); |
1256 |
> |
} catch (Throwable fail) { |
1257 |
> |
threadUnexpectedException(fail); |
1258 |
|
} |
1259 |
|
} |
1260 |
|
|
1445 |
|
public abstract class CheckedRecursiveAction extends RecursiveAction { |
1446 |
|
protected abstract void realCompute() throws Throwable; |
1447 |
|
|
1448 |
< |
public final void compute() { |
1448 |
> |
@Override protected final void compute() { |
1449 |
|
try { |
1450 |
|
realCompute(); |
1451 |
< |
} catch (Throwable t) { |
1452 |
< |
threadUnexpectedException(t); |
1451 |
> |
} catch (Throwable fail) { |
1452 |
> |
threadUnexpectedException(fail); |
1453 |
|
} |
1454 |
|
} |
1455 |
|
} |
1460 |
|
public abstract class CheckedRecursiveTask<T> extends RecursiveTask<T> { |
1461 |
|
protected abstract T realCompute() throws Throwable; |
1462 |
|
|
1463 |
< |
public final T compute() { |
1463 |
> |
@Override protected final T compute() { |
1464 |
|
try { |
1465 |
|
return realCompute(); |
1466 |
< |
} catch (Throwable t) { |
1467 |
< |
threadUnexpectedException(t); |
1466 |
> |
} catch (Throwable fail) { |
1467 |
> |
threadUnexpectedException(fail); |
1468 |
|
return null; |
1469 |
|
} |
1470 |
|
} |
1488 |
|
public int await() { |
1489 |
|
try { |
1490 |
|
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1491 |
< |
} catch (TimeoutException e) { |
1491 |
> |
} catch (TimeoutException timedOut) { |
1492 |
|
throw new AssertionFailedError("timed out"); |
1493 |
< |
} catch (Exception e) { |
1493 |
> |
} catch (Exception fail) { |
1494 |
|
AssertionFailedError afe = |
1495 |
< |
new AssertionFailedError("Unexpected exception: " + e); |
1496 |
< |
afe.initCause(e); |
1495 |
> |
new AssertionFailedError("Unexpected exception: " + fail); |
1496 |
> |
afe.initCause(fail); |
1497 |
|
throw afe; |
1498 |
|
} |
1499 |
|
} |
1521 |
|
q.remove(); |
1522 |
|
shouldThrow(); |
1523 |
|
} catch (NoSuchElementException success) {} |
1524 |
< |
} catch (InterruptedException ie) { |
1225 |
< |
threadUnexpectedException(ie); |
1226 |
< |
} |
1524 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
1525 |
|
} |
1526 |
|
|
1527 |
|
void assertSerialEquals(Object x, Object y) { |
1540 |
|
oos.flush(); |
1541 |
|
oos.close(); |
1542 |
|
return bos.toByteArray(); |
1543 |
< |
} catch (Throwable t) { |
1544 |
< |
threadUnexpectedException(t); |
1543 |
> |
} catch (Throwable fail) { |
1544 |
> |
threadUnexpectedException(fail); |
1545 |
|
return new byte[0]; |
1546 |
|
} |
1547 |
|
} |
1554 |
|
T clone = (T) ois.readObject(); |
1555 |
|
assertSame(o.getClass(), clone.getClass()); |
1556 |
|
return clone; |
1557 |
< |
} catch (Throwable t) { |
1558 |
< |
threadUnexpectedException(t); |
1557 |
> |
} catch (Throwable fail) { |
1558 |
> |
threadUnexpectedException(fail); |
1559 |
|
return null; |
1560 |
|
} |
1561 |
|
} |
1562 |
+ |
|
1563 |
+ |
public void assertThrows(Class<? extends Throwable> expectedExceptionClass, |
1564 |
+ |
Runnable... throwingActions) { |
1565 |
+ |
for (Runnable throwingAction : throwingActions) { |
1566 |
+ |
boolean threw = false; |
1567 |
+ |
try { throwingAction.run(); } |
1568 |
+ |
catch (Throwable t) { |
1569 |
+ |
threw = true; |
1570 |
+ |
if (!expectedExceptionClass.isInstance(t)) { |
1571 |
+ |
AssertionFailedError afe = |
1572 |
+ |
new AssertionFailedError |
1573 |
+ |
("Expected " + expectedExceptionClass.getName() + |
1574 |
+ |
", got " + t.getClass().getName()); |
1575 |
+ |
afe.initCause(t); |
1576 |
+ |
threadUnexpectedException(afe); |
1577 |
+ |
} |
1578 |
+ |
} |
1579 |
+ |
if (!threw) |
1580 |
+ |
shouldThrow(expectedExceptionClass.getName()); |
1581 |
+ |
} |
1582 |
+ |
} |
1583 |
+ |
|
1584 |
+ |
public void assertIteratorExhausted(Iterator<?> it) { |
1585 |
+ |
try { |
1586 |
+ |
it.next(); |
1587 |
+ |
shouldThrow(); |
1588 |
+ |
} catch (NoSuchElementException success) {} |
1589 |
+ |
assertFalse(it.hasNext()); |
1590 |
+ |
} |
1591 |
|
} |