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.NoSuchElementException; |
18 |
< |
import java.util.PropertyPermission; |
19 |
< |
import java.util.concurrent.*; |
20 |
< |
import java.util.concurrent.atomic.AtomicReference; |
19 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
20 |
< |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
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; |
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.BlockingQueue; |
37 |
+ |
import java.util.concurrent.Callable; |
38 |
+ |
import java.util.concurrent.CountDownLatch; |
39 |
+ |
import java.util.concurrent.CyclicBarrier; |
40 |
+ |
import java.util.concurrent.ExecutorService; |
41 |
+ |
import java.util.concurrent.Future; |
42 |
+ |
import java.util.concurrent.RecursiveAction; |
43 |
+ |
import java.util.concurrent.RecursiveTask; |
44 |
+ |
import java.util.concurrent.RejectedExecutionHandler; |
45 |
+ |
import java.util.concurrent.Semaphore; |
46 |
+ |
import java.util.concurrent.ThreadFactory; |
47 |
+ |
import java.util.concurrent.ThreadPoolExecutor; |
48 |
+ |
import java.util.concurrent.TimeoutException; |
49 |
+ |
import java.util.concurrent.atomic.AtomicReference; |
50 |
+ |
import java.util.regex.Pattern; |
51 |
+ |
|
52 |
+ |
import junit.framework.AssertionFailedError; |
53 |
+ |
import junit.framework.Test; |
54 |
+ |
import junit.framework.TestCase; |
55 |
+ |
import junit.framework.TestResult; |
56 |
+ |
import junit.framework.TestSuite; |
57 |
|
|
58 |
|
/** |
59 |
|
* Base class for JSR166 Junit TCK tests. Defines some constants, |
96 |
|
* |
97 |
|
* </ol> |
98 |
|
* |
99 |
< |
* <p> <b>Other notes</b> |
99 |
> |
* <p><b>Other notes</b> |
100 |
|
* <ul> |
101 |
|
* |
102 |
|
* <li> Usually, there is one testcase method per JSR166 method |
136 |
|
Boolean.getBoolean("jsr166.expensiveTests"); |
137 |
|
|
138 |
|
/** |
139 |
+ |
* If true, also run tests that are not part of the official tck |
140 |
+ |
* because they test unspecified implementation details. |
141 |
+ |
*/ |
142 |
+ |
protected static final boolean testImplementationDetails = |
143 |
+ |
Boolean.getBoolean("jsr166.testImplementationDetails"); |
144 |
+ |
|
145 |
+ |
/** |
146 |
|
* If true, report on stdout all "slow" tests, that is, ones that |
147 |
|
* take more than profileThreshold milliseconds to execute. |
148 |
|
*/ |
156 |
|
private static final long profileThreshold = |
157 |
|
Long.getLong("jsr166.profileThreshold", 100); |
158 |
|
|
159 |
+ |
/** |
160 |
+ |
* The number of repetitions per test (for tickling rare bugs). |
161 |
+ |
*/ |
162 |
+ |
private static final int runsPerTest = |
163 |
+ |
Integer.getInteger("jsr166.runsPerTest", 1); |
164 |
+ |
|
165 |
+ |
/** |
166 |
+ |
* The number of repetitions of the test suite (for finding leaks?). |
167 |
+ |
*/ |
168 |
+ |
private static final int suiteRuns = |
169 |
+ |
Integer.getInteger("jsr166.suiteRuns", 1); |
170 |
+ |
|
171 |
+ |
public JSR166TestCase() { super(); } |
172 |
+ |
public JSR166TestCase(String name) { super(name); } |
173 |
+ |
|
174 |
+ |
/** |
175 |
+ |
* A filter for tests to run, matching strings of the form |
176 |
+ |
* methodName(className), e.g. "testInvokeAll5(ForkJoinPoolTest)" |
177 |
+ |
* Usefully combined with jsr166.runsPerTest. |
178 |
+ |
*/ |
179 |
+ |
private static final Pattern methodFilter = methodFilter(); |
180 |
+ |
|
181 |
+ |
private static Pattern methodFilter() { |
182 |
+ |
String regex = System.getProperty("jsr166.methodFilter"); |
183 |
+ |
return (regex == null) ? null : Pattern.compile(regex); |
184 |
+ |
} |
185 |
+ |
|
186 |
|
protected void runTest() throws Throwable { |
187 |
< |
if (profileTests) |
188 |
< |
runTestProfiled(); |
189 |
< |
else |
190 |
< |
super.runTest(); |
187 |
> |
if (methodFilter == null |
188 |
> |
|| methodFilter.matcher(toString()).find()) { |
189 |
> |
for (int i = 0; i < runsPerTest; i++) { |
190 |
> |
if (profileTests) |
191 |
> |
runTestProfiled(); |
192 |
> |
else |
193 |
> |
super.runTest(); |
194 |
> |
} |
195 |
> |
} |
196 |
|
} |
197 |
|
|
198 |
|
protected void runTestProfiled() throws Throwable { |
199 |
+ |
// Warmup run, notably to trigger all needed classloading. |
200 |
+ |
super.runTest(); |
201 |
|
long t0 = System.nanoTime(); |
202 |
|
try { |
203 |
|
super.runTest(); |
204 |
|
} finally { |
205 |
< |
long elapsedMillis = |
136 |
< |
(System.nanoTime() - t0) / (1000L * 1000L); |
205 |
> |
long elapsedMillis = millisElapsedSince(t0); |
206 |
|
if (elapsedMillis >= profileThreshold) |
207 |
|
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); |
208 |
|
} |
209 |
|
} |
210 |
|
|
211 |
|
/** |
212 |
< |
* Runs all JSR166 unit tests using junit.textui.TestRunner |
212 |
> |
* Runs all JSR166 unit tests using junit.textui.TestRunner. |
213 |
|
*/ |
214 |
|
public static void main(String[] args) { |
215 |
+ |
main(suite(), args); |
216 |
+ |
} |
217 |
+ |
|
218 |
+ |
/** |
219 |
+ |
* Runs all unit tests in the given test suite. |
220 |
+ |
* Actual behavior influenced by jsr166.* system properties. |
221 |
+ |
*/ |
222 |
+ |
static void main(Test suite, String[] args) { |
223 |
|
if (useSecurityManager) { |
224 |
|
System.err.println("Setting a permissive security manager"); |
225 |
|
Policy.setPolicy(permissivePolicy()); |
226 |
|
System.setSecurityManager(new SecurityManager()); |
227 |
|
} |
228 |
< |
int iters = (args.length == 0) ? 1 : Integer.parseInt(args[0]); |
229 |
< |
|
230 |
< |
Test s = suite(); |
231 |
< |
for (int i = 0; i < iters; ++i) { |
155 |
< |
junit.textui.TestRunner.run(s); |
228 |
> |
for (int i = 0; i < suiteRuns; i++) { |
229 |
> |
TestResult result = junit.textui.TestRunner.run(suite); |
230 |
> |
if (!result.wasSuccessful()) |
231 |
> |
System.exit(1); |
232 |
|
System.gc(); |
233 |
|
System.runFinalization(); |
234 |
|
} |
159 |
– |
System.exit(0); |
235 |
|
} |
236 |
|
|
237 |
|
public static TestSuite newTestSuite(Object... suiteOrClasses) { |
247 |
|
return suite; |
248 |
|
} |
249 |
|
|
250 |
+ |
public static void addNamedTestClasses(TestSuite suite, |
251 |
+ |
String... testClassNames) { |
252 |
+ |
for (String testClassName : testClassNames) { |
253 |
+ |
try { |
254 |
+ |
Class<?> testClass = Class.forName(testClassName); |
255 |
+ |
Method m = testClass.getDeclaredMethod("suite", |
256 |
+ |
new Class<?>[0]); |
257 |
+ |
suite.addTest(newTestSuite((Test)m.invoke(null))); |
258 |
+ |
} catch (Exception e) { |
259 |
+ |
throw new Error("Missing test class", e); |
260 |
+ |
} |
261 |
+ |
} |
262 |
+ |
} |
263 |
+ |
|
264 |
+ |
public static final double JAVA_CLASS_VERSION; |
265 |
+ |
public static final String JAVA_SPECIFICATION_VERSION; |
266 |
+ |
static { |
267 |
+ |
try { |
268 |
+ |
JAVA_CLASS_VERSION = java.security.AccessController.doPrivileged( |
269 |
+ |
new java.security.PrivilegedAction<Double>() { |
270 |
+ |
public Double run() { |
271 |
+ |
return Double.valueOf(System.getProperty("java.class.version"));}}); |
272 |
+ |
JAVA_SPECIFICATION_VERSION = java.security.AccessController.doPrivileged( |
273 |
+ |
new java.security.PrivilegedAction<String>() { |
274 |
+ |
public String run() { |
275 |
+ |
return System.getProperty("java.specification.version");}}); |
276 |
+ |
} catch (Throwable t) { |
277 |
+ |
throw new Error(t); |
278 |
+ |
} |
279 |
+ |
} |
280 |
+ |
|
281 |
+ |
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
282 |
+ |
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
283 |
+ |
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
284 |
+ |
public static boolean atLeastJava9() { |
285 |
+ |
return JAVA_CLASS_VERSION >= 53.0 |
286 |
+ |
// As of 2015-09, java9 still uses 52.0 class file version |
287 |
+ |
|| JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?(9|[0-9][0-9])$"); |
288 |
+ |
} |
289 |
+ |
public static boolean atLeastJava10() { |
290 |
+ |
return JAVA_CLASS_VERSION >= 54.0 |
291 |
+ |
|| JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?[0-9][0-9]$"); |
292 |
+ |
} |
293 |
+ |
|
294 |
|
/** |
295 |
|
* Collects all JSR166 unit tests as one suite. |
296 |
|
*/ |
297 |
|
public static Test suite() { |
298 |
< |
return newTestSuite( |
298 |
> |
// Java7+ test classes |
299 |
> |
TestSuite suite = newTestSuite( |
300 |
|
ForkJoinPoolTest.suite(), |
301 |
|
ForkJoinTaskTest.suite(), |
302 |
|
RecursiveActionTest.suite(), |
361 |
|
TreeSetTest.suite(), |
362 |
|
TreeSubMapTest.suite(), |
363 |
|
TreeSubSetTest.suite()); |
364 |
+ |
|
365 |
+ |
// Java8+ test classes |
366 |
+ |
if (atLeastJava8()) { |
367 |
+ |
String[] java8TestClassNames = { |
368 |
+ |
"Atomic8Test", |
369 |
+ |
"CompletableFutureTest", |
370 |
+ |
"ConcurrentHashMap8Test", |
371 |
+ |
"CountedCompleterTest", |
372 |
+ |
"DoubleAccumulatorTest", |
373 |
+ |
"DoubleAdderTest", |
374 |
+ |
"ForkJoinPool8Test", |
375 |
+ |
"ForkJoinTask8Test", |
376 |
+ |
"LongAccumulatorTest", |
377 |
+ |
"LongAdderTest", |
378 |
+ |
"SplittableRandomTest", |
379 |
+ |
"StampedLockTest", |
380 |
+ |
"ThreadLocalRandom8Test", |
381 |
+ |
}; |
382 |
+ |
addNamedTestClasses(suite, java8TestClassNames); |
383 |
+ |
} |
384 |
+ |
|
385 |
+ |
// Java9+ test classes |
386 |
+ |
if (atLeastJava9()) { |
387 |
+ |
String[] java9TestClassNames = { |
388 |
+ |
"ThreadPoolExecutor9Test", |
389 |
+ |
}; |
390 |
+ |
addNamedTestClasses(suite, java9TestClassNames); |
391 |
+ |
} |
392 |
+ |
|
393 |
+ |
return suite; |
394 |
+ |
} |
395 |
+ |
|
396 |
+ |
/** Returns list of junit-style test method names in given class. */ |
397 |
+ |
public static ArrayList<String> testMethodNames(Class<?> testClass) { |
398 |
+ |
Method[] methods = testClass.getDeclaredMethods(); |
399 |
+ |
ArrayList<String> names = new ArrayList<String>(methods.length); |
400 |
+ |
for (Method method : methods) { |
401 |
+ |
if (method.getName().startsWith("test") |
402 |
+ |
&& Modifier.isPublic(method.getModifiers()) |
403 |
+ |
// method.getParameterCount() requires jdk8+ |
404 |
+ |
&& method.getParameterTypes().length == 0) { |
405 |
+ |
names.add(method.getName()); |
406 |
+ |
} |
407 |
+ |
} |
408 |
+ |
return names; |
409 |
+ |
} |
410 |
+ |
|
411 |
+ |
/** |
412 |
+ |
* Returns junit-style testSuite for the given test class, but |
413 |
+ |
* parameterized by passing extra data to each test. |
414 |
+ |
*/ |
415 |
+ |
public static <ExtraData> Test parameterizedTestSuite |
416 |
+ |
(Class<? extends JSR166TestCase> testClass, |
417 |
+ |
Class<ExtraData> dataClass, |
418 |
+ |
ExtraData data) { |
419 |
+ |
try { |
420 |
+ |
TestSuite suite = new TestSuite(); |
421 |
+ |
Constructor c = |
422 |
+ |
testClass.getDeclaredConstructor(dataClass, String.class); |
423 |
+ |
for (String methodName : testMethodNames(testClass)) |
424 |
+ |
suite.addTest((Test) c.newInstance(data, methodName)); |
425 |
+ |
return suite; |
426 |
+ |
} catch (Exception e) { |
427 |
+ |
throw new Error(e); |
428 |
+ |
} |
429 |
+ |
} |
430 |
+ |
|
431 |
+ |
/** |
432 |
+ |
* Returns junit-style testSuite for the jdk8 extension of the |
433 |
+ |
* given test class, but parameterized by passing extra data to |
434 |
+ |
* each test. Uses reflection to allow compilation in jdk7. |
435 |
+ |
*/ |
436 |
+ |
public static <ExtraData> Test jdk8ParameterizedTestSuite |
437 |
+ |
(Class<? extends JSR166TestCase> testClass, |
438 |
+ |
Class<ExtraData> dataClass, |
439 |
+ |
ExtraData data) { |
440 |
+ |
if (atLeastJava8()) { |
441 |
+ |
String name = testClass.getName(); |
442 |
+ |
String name8 = name.replaceAll("Test$", "8Test"); |
443 |
+ |
if (name.equals(name8)) throw new Error(name); |
444 |
+ |
try { |
445 |
+ |
return (Test) |
446 |
+ |
Class.forName(name8) |
447 |
+ |
.getMethod("testSuite", new Class[] { dataClass }) |
448 |
+ |
.invoke(null, data); |
449 |
+ |
} catch (Exception e) { |
450 |
+ |
throw new Error(e); |
451 |
+ |
} |
452 |
+ |
} else { |
453 |
+ |
return new TestSuite(); |
454 |
+ |
} |
455 |
+ |
|
456 |
|
} |
457 |
|
|
458 |
+ |
// Delays for timing-dependent tests, in milliseconds. |
459 |
|
|
460 |
|
public static long SHORT_DELAY_MS; |
461 |
|
public static long SMALL_DELAY_MS; |
462 |
|
public static long MEDIUM_DELAY_MS; |
463 |
|
public static long LONG_DELAY_MS; |
464 |
|
|
252 |
– |
|
465 |
|
/** |
466 |
|
* Returns the shortest timed delay. This could |
467 |
|
* be reimplemented to use for example a Property. |
470 |
|
return 50; |
471 |
|
} |
472 |
|
|
261 |
– |
|
473 |
|
/** |
474 |
|
* Sets delays as multiples of SHORT_DELAY. |
475 |
|
*/ |
481 |
|
} |
482 |
|
|
483 |
|
/** |
484 |
+ |
* Returns a timeout in milliseconds to be used in tests that |
485 |
+ |
* verify that operations block or time out. |
486 |
+ |
*/ |
487 |
+ |
long timeoutMillis() { |
488 |
+ |
return SHORT_DELAY_MS / 4; |
489 |
+ |
} |
490 |
+ |
|
491 |
+ |
/** |
492 |
+ |
* Returns a new Date instance representing a time at least |
493 |
+ |
* delayMillis milliseconds in the future. |
494 |
+ |
*/ |
495 |
+ |
Date delayedDate(long delayMillis) { |
496 |
+ |
// Add 1 because currentTimeMillis is known to round into the past. |
497 |
+ |
return new Date(System.currentTimeMillis() + delayMillis + 1); |
498 |
+ |
} |
499 |
+ |
|
500 |
+ |
/** |
501 |
|
* The first exception encountered if any threadAssertXXX method fails. |
502 |
|
*/ |
503 |
|
private final AtomicReference<Throwable> threadFailure |
518 |
|
} |
519 |
|
|
520 |
|
/** |
521 |
+ |
* Extra checks that get done for all test cases. |
522 |
+ |
* |
523 |
|
* Triggers test case failure if any thread assertions have failed, |
524 |
|
* by rethrowing, in the test harness thread, any exception recorded |
525 |
|
* earlier by threadRecordFailure. |
526 |
+ |
* |
527 |
+ |
* Triggers test case failure if interrupt status is set in the main thread. |
528 |
|
*/ |
529 |
|
public void tearDown() throws Exception { |
530 |
|
Throwable t = threadFailure.getAndSet(null); |
542 |
|
throw afe; |
543 |
|
} |
544 |
|
} |
545 |
+ |
|
546 |
+ |
if (Thread.interrupted()) |
547 |
+ |
throw new AssertionFailedError("interrupt status set in main thread"); |
548 |
+ |
|
549 |
+ |
checkForkJoinPoolThreadLeaks(); |
550 |
+ |
} |
551 |
+ |
|
552 |
+ |
/** |
553 |
+ |
* Finds missing try { ... } finally { joinPool(e); } |
554 |
+ |
*/ |
555 |
+ |
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
556 |
+ |
Thread[] survivors = new Thread[5]; |
557 |
+ |
int count = Thread.enumerate(survivors); |
558 |
+ |
for (int i = 0; i < count; i++) { |
559 |
+ |
Thread thread = survivors[i]; |
560 |
+ |
String name = thread.getName(); |
561 |
+ |
if (name.startsWith("ForkJoinPool-")) { |
562 |
+ |
// give thread some time to terminate |
563 |
+ |
thread.join(LONG_DELAY_MS); |
564 |
+ |
if (!thread.isAlive()) continue; |
565 |
+ |
throw new AssertionFailedError |
566 |
+ |
(String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n", |
567 |
+ |
toString(), name)); |
568 |
+ |
} |
569 |
+ |
} |
570 |
|
} |
571 |
|
|
572 |
|
/** |
647 |
|
public void threadAssertEquals(Object x, Object y) { |
648 |
|
try { |
649 |
|
assertEquals(x, y); |
650 |
< |
} catch (AssertionFailedError t) { |
651 |
< |
threadRecordFailure(t); |
652 |
< |
throw t; |
653 |
< |
} catch (Throwable t) { |
654 |
< |
threadUnexpectedException(t); |
650 |
> |
} catch (AssertionFailedError fail) { |
651 |
> |
threadRecordFailure(fail); |
652 |
> |
throw fail; |
653 |
> |
} catch (Throwable fail) { |
654 |
> |
threadUnexpectedException(fail); |
655 |
|
} |
656 |
|
} |
657 |
|
|
663 |
|
public void threadAssertSame(Object x, Object y) { |
664 |
|
try { |
665 |
|
assertSame(x, y); |
666 |
< |
} catch (AssertionFailedError t) { |
667 |
< |
threadRecordFailure(t); |
668 |
< |
throw t; |
666 |
> |
} catch (AssertionFailedError fail) { |
667 |
> |
threadRecordFailure(fail); |
668 |
> |
throw fail; |
669 |
|
} |
670 |
|
} |
671 |
|
|
698 |
|
else { |
699 |
|
AssertionFailedError afe = |
700 |
|
new AssertionFailedError("unexpected exception: " + t); |
701 |
< |
t.initCause(t); |
701 |
> |
afe.initCause(t); |
702 |
|
throw afe; |
703 |
|
} |
704 |
|
} |
705 |
|
|
706 |
|
/** |
707 |
< |
* Delays, via Thread.sleep for the given millisecond delay, but |
707 |
> |
* Delays, via Thread.sleep, for the given millisecond delay, but |
708 |
|
* if the sleep is shorter than specified, may re-sleep or yield |
709 |
|
* until time elapses. |
710 |
|
*/ |
711 |
< |
public static void delay(long ms) throws InterruptedException { |
711 |
> |
static void delay(long millis) throws InterruptedException { |
712 |
|
long startTime = System.nanoTime(); |
713 |
< |
long ns = ms * 1000 * 1000; |
713 |
> |
long ns = millis * 1000 * 1000; |
714 |
|
for (;;) { |
715 |
< |
if (ms > 0L) |
716 |
< |
Thread.sleep(ms); |
715 |
> |
if (millis > 0L) |
716 |
> |
Thread.sleep(millis); |
717 |
|
else // too short to sleep |
718 |
|
Thread.yield(); |
719 |
|
long d = ns - (System.nanoTime() - startTime); |
720 |
|
if (d > 0L) |
721 |
< |
ms = d / (1000 * 1000); |
721 |
> |
millis = d / (1000 * 1000); |
722 |
|
else |
723 |
|
break; |
724 |
|
} |
727 |
|
/** |
728 |
|
* Waits out termination of a thread pool or fails doing so. |
729 |
|
*/ |
730 |
< |
public void joinPool(ExecutorService exec) { |
730 |
> |
void joinPool(ExecutorService exec) { |
731 |
|
try { |
732 |
|
exec.shutdown(); |
733 |
< |
assertTrue("ExecutorService did not terminate in a timely manner", |
734 |
< |
exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)); |
733 |
> |
if (!exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) |
734 |
> |
fail("ExecutorService " + exec + |
735 |
> |
" did not terminate in a timely manner"); |
736 |
|
} catch (SecurityException ok) { |
737 |
|
// Allowed in case test doesn't have privs |
738 |
< |
} catch (InterruptedException ie) { |
738 |
> |
} catch (InterruptedException fail) { |
739 |
|
fail("Unexpected InterruptedException"); |
740 |
|
} |
741 |
|
} |
742 |
|
|
743 |
|
/** |
744 |
< |
* Checks that thread does not terminate within timeoutMillis |
745 |
< |
* milliseconds (that is, Thread.join times out). |
744 |
> |
* A debugging tool to print all stack traces, as jstack does. |
745 |
> |
*/ |
746 |
> |
static void printAllStackTraces() { |
747 |
> |
for (ThreadInfo info : |
748 |
> |
ManagementFactory.getThreadMXBean() |
749 |
> |
.dumpAllThreads(true, true)) |
750 |
> |
System.err.print(info); |
751 |
> |
} |
752 |
> |
|
753 |
> |
/** |
754 |
> |
* Checks that thread does not terminate within the default |
755 |
> |
* millisecond delay of {@code timeoutMillis()}. |
756 |
> |
*/ |
757 |
> |
void assertThreadStaysAlive(Thread thread) { |
758 |
> |
assertThreadStaysAlive(thread, timeoutMillis()); |
759 |
> |
} |
760 |
> |
|
761 |
> |
/** |
762 |
> |
* Checks that thread does not terminate within the given millisecond delay. |
763 |
|
*/ |
764 |
< |
public void assertThreadJoinTimesOut(Thread thread, long timeoutMillis) { |
764 |
> |
void assertThreadStaysAlive(Thread thread, long millis) { |
765 |
|
try { |
766 |
< |
long startTime = System.nanoTime(); |
767 |
< |
thread.join(timeoutMillis); |
766 |
> |
// No need to optimize the failing case via Thread.join. |
767 |
> |
delay(millis); |
768 |
|
assertTrue(thread.isAlive()); |
769 |
< |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
770 |
< |
} catch (InterruptedException ie) { |
769 |
> |
} catch (InterruptedException fail) { |
770 |
> |
fail("Unexpected InterruptedException"); |
771 |
> |
} |
772 |
> |
} |
773 |
> |
|
774 |
> |
/** |
775 |
> |
* Checks that the threads do not terminate within the default |
776 |
> |
* millisecond delay of {@code timeoutMillis()}. |
777 |
> |
*/ |
778 |
> |
void assertThreadsStayAlive(Thread... threads) { |
779 |
> |
assertThreadsStayAlive(timeoutMillis(), threads); |
780 |
> |
} |
781 |
> |
|
782 |
> |
/** |
783 |
> |
* Checks that the threads do not terminate within the given millisecond delay. |
784 |
> |
*/ |
785 |
> |
void assertThreadsStayAlive(long millis, Thread... threads) { |
786 |
> |
try { |
787 |
> |
// No need to optimize the failing case via Thread.join. |
788 |
> |
delay(millis); |
789 |
> |
for (Thread thread : threads) |
790 |
> |
assertTrue(thread.isAlive()); |
791 |
> |
} catch (InterruptedException fail) { |
792 |
|
fail("Unexpected InterruptedException"); |
793 |
|
} |
794 |
|
} |
795 |
|
|
796 |
|
/** |
797 |
+ |
* Checks that future.get times out, with the default timeout of |
798 |
+ |
* {@code timeoutMillis()}. |
799 |
+ |
*/ |
800 |
+ |
void assertFutureTimesOut(Future future) { |
801 |
+ |
assertFutureTimesOut(future, timeoutMillis()); |
802 |
+ |
} |
803 |
+ |
|
804 |
+ |
/** |
805 |
+ |
* Checks that future.get times out, with the given millisecond timeout. |
806 |
+ |
*/ |
807 |
+ |
void assertFutureTimesOut(Future future, long timeoutMillis) { |
808 |
+ |
long startTime = System.nanoTime(); |
809 |
+ |
try { |
810 |
+ |
future.get(timeoutMillis, MILLISECONDS); |
811 |
+ |
shouldThrow(); |
812 |
+ |
} catch (TimeoutException success) { |
813 |
+ |
} catch (Exception fail) { |
814 |
+ |
threadUnexpectedException(fail); |
815 |
+ |
} finally { future.cancel(true); } |
816 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
817 |
+ |
} |
818 |
+ |
|
819 |
+ |
/** |
820 |
|
* Fails with message "should throw exception". |
821 |
|
*/ |
822 |
|
public void shouldThrow() { |
855 |
|
public static final Integer m6 = new Integer(-6); |
856 |
|
public static final Integer m10 = new Integer(-10); |
857 |
|
|
539 |
– |
|
858 |
|
/** |
859 |
|
* Runs Runnable r with a security policy that permits precisely |
860 |
|
* the specified permissions. If there is no current security |
866 |
|
SecurityManager sm = System.getSecurityManager(); |
867 |
|
if (sm == null) { |
868 |
|
r.run(); |
869 |
+ |
} |
870 |
+ |
runWithSecurityManagerWithPermissions(r, permissions); |
871 |
+ |
} |
872 |
+ |
|
873 |
+ |
/** |
874 |
+ |
* Runs Runnable r with a security policy that permits precisely |
875 |
+ |
* the specified permissions. If there is no current security |
876 |
+ |
* manager, a temporary one is set for the duration of the |
877 |
+ |
* Runnable. We require that any security manager permit |
878 |
+ |
* getPolicy/setPolicy. |
879 |
+ |
*/ |
880 |
+ |
public void runWithSecurityManagerWithPermissions(Runnable r, |
881 |
+ |
Permission... permissions) { |
882 |
+ |
SecurityManager sm = System.getSecurityManager(); |
883 |
+ |
if (sm == null) { |
884 |
|
Policy savedPolicy = Policy.getPolicy(); |
885 |
|
try { |
886 |
|
Policy.setPolicy(permissivePolicy()); |
887 |
|
System.setSecurityManager(new SecurityManager()); |
888 |
< |
runWithPermissions(r, permissions); |
888 |
> |
runWithSecurityManagerWithPermissions(r, permissions); |
889 |
|
} finally { |
890 |
|
System.setSecurityManager(null); |
891 |
|
Policy.setPolicy(savedPolicy); |
933 |
|
return perms.implies(p); |
934 |
|
} |
935 |
|
public void refresh() {} |
936 |
+ |
public String toString() { |
937 |
+ |
List<Permission> ps = new ArrayList<Permission>(); |
938 |
+ |
for (Enumeration<Permission> e = perms.elements(); e.hasMoreElements();) |
939 |
+ |
ps.add(e.nextElement()); |
940 |
+ |
return "AdjustablePolicy with permissions " + ps; |
941 |
+ |
} |
942 |
|
} |
943 |
|
|
944 |
|
/** |
967 |
|
void sleep(long millis) { |
968 |
|
try { |
969 |
|
delay(millis); |
970 |
< |
} catch (InterruptedException ie) { |
970 |
> |
} catch (InterruptedException fail) { |
971 |
|
AssertionFailedError afe = |
972 |
|
new AssertionFailedError("Unexpected InterruptedException"); |
973 |
< |
afe.initCause(ie); |
973 |
> |
afe.initCause(fail); |
974 |
|
throw afe; |
975 |
|
} |
976 |
|
} |
977 |
|
|
978 |
|
/** |
979 |
< |
* Sleeps until the timeout has elapsed, or interrupted. |
641 |
< |
* Does <em>NOT</em> throw InterruptedException. |
642 |
< |
*/ |
643 |
< |
void sleepTillInterrupted(long timeoutMillis) { |
644 |
< |
try { |
645 |
< |
Thread.sleep(timeoutMillis); |
646 |
< |
} catch (InterruptedException wakeup) {} |
647 |
< |
} |
648 |
< |
|
649 |
< |
/** |
650 |
< |
* Waits up to the specified number of milliseconds for the given |
979 |
> |
* Spin-waits up to the specified number of milliseconds for the given |
980 |
|
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
981 |
|
*/ |
982 |
|
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
983 |
< |
long timeoutNanos = timeoutMillis * 1000L * 1000L; |
655 |
< |
long t0 = System.nanoTime(); |
983 |
> |
long startTime = System.nanoTime(); |
984 |
|
for (;;) { |
985 |
|
Thread.State s = thread.getState(); |
986 |
|
if (s == Thread.State.BLOCKED || |
989 |
|
return; |
990 |
|
else if (s == Thread.State.TERMINATED) |
991 |
|
fail("Unexpected thread termination"); |
992 |
< |
else if (System.nanoTime() - t0 > timeoutNanos) { |
992 |
> |
else if (millisElapsedSince(startTime) > timeoutMillis) { |
993 |
|
threadAssertTrue(thread.isAlive()); |
994 |
|
return; |
995 |
|
} |
1008 |
|
/** |
1009 |
|
* Returns the number of milliseconds since time given by |
1010 |
|
* startNanoTime, which must have been previously returned from a |
1011 |
< |
* call to {@link System.nanoTime()}. |
1011 |
> |
* call to {@link System#nanoTime()}. |
1012 |
|
*/ |
1013 |
< |
long millisElapsedSince(long startNanoTime) { |
1013 |
> |
static long millisElapsedSince(long startNanoTime) { |
1014 |
|
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
1015 |
|
} |
1016 |
|
|
1017 |
+ |
// void assertTerminatesPromptly(long timeoutMillis, Runnable r) { |
1018 |
+ |
// long startTime = System.nanoTime(); |
1019 |
+ |
// try { |
1020 |
+ |
// r.run(); |
1021 |
+ |
// } catch (Throwable fail) { threadUnexpectedException(fail); } |
1022 |
+ |
// if (millisElapsedSince(startTime) > timeoutMillis/2) |
1023 |
+ |
// throw new AssertionFailedError("did not return promptly"); |
1024 |
+ |
// } |
1025 |
+ |
|
1026 |
+ |
// void assertTerminatesPromptly(Runnable r) { |
1027 |
+ |
// assertTerminatesPromptly(LONG_DELAY_MS/2, r); |
1028 |
+ |
// } |
1029 |
+ |
|
1030 |
+ |
/** |
1031 |
+ |
* Checks that timed f.get() returns the expected value, and does not |
1032 |
+ |
* wait for the timeout to elapse before returning. |
1033 |
+ |
*/ |
1034 |
+ |
<T> void checkTimedGet(Future<T> f, T expectedValue, long timeoutMillis) { |
1035 |
+ |
long startTime = System.nanoTime(); |
1036 |
+ |
try { |
1037 |
+ |
assertEquals(expectedValue, f.get(timeoutMillis, MILLISECONDS)); |
1038 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
1039 |
+ |
if (millisElapsedSince(startTime) > timeoutMillis/2) |
1040 |
+ |
throw new AssertionFailedError("timed get did not return promptly"); |
1041 |
+ |
} |
1042 |
+ |
|
1043 |
+ |
<T> void checkTimedGet(Future<T> f, T expectedValue) { |
1044 |
+ |
checkTimedGet(f, expectedValue, LONG_DELAY_MS); |
1045 |
+ |
} |
1046 |
+ |
|
1047 |
|
/** |
1048 |
|
* Returns a new started daemon Thread running the given runnable. |
1049 |
|
*/ |
1062 |
|
void awaitTermination(Thread t, long timeoutMillis) { |
1063 |
|
try { |
1064 |
|
t.join(timeoutMillis); |
1065 |
< |
} catch (InterruptedException ie) { |
1066 |
< |
threadUnexpectedException(ie); |
1065 |
> |
} catch (InterruptedException fail) { |
1066 |
> |
threadUnexpectedException(fail); |
1067 |
|
} finally { |
1068 |
< |
if (t.isAlive()) { |
1068 |
> |
if (t.getState() != Thread.State.TERMINATED) { |
1069 |
|
t.interrupt(); |
1070 |
|
fail("Test timed out"); |
1071 |
|
} |
1089 |
|
public final void run() { |
1090 |
|
try { |
1091 |
|
realRun(); |
1092 |
< |
} catch (Throwable t) { |
1093 |
< |
threadUnexpectedException(t); |
1092 |
> |
} catch (Throwable fail) { |
1093 |
> |
threadUnexpectedException(fail); |
1094 |
|
} |
1095 |
|
} |
1096 |
|
} |
1143 |
|
realRun(); |
1144 |
|
threadShouldThrow("InterruptedException"); |
1145 |
|
} catch (InterruptedException success) { |
1146 |
< |
} catch (Throwable t) { |
1147 |
< |
threadUnexpectedException(t); |
1146 |
> |
threadAssertFalse(Thread.interrupted()); |
1147 |
> |
} catch (Throwable fail) { |
1148 |
> |
threadUnexpectedException(fail); |
1149 |
|
} |
1150 |
|
} |
1151 |
|
} |
1156 |
|
public final T call() { |
1157 |
|
try { |
1158 |
|
return realCall(); |
1159 |
< |
} catch (Throwable t) { |
1160 |
< |
threadUnexpectedException(t); |
1159 |
> |
} catch (Throwable fail) { |
1160 |
> |
threadUnexpectedException(fail); |
1161 |
|
return null; |
1162 |
|
} |
1163 |
|
} |
1173 |
|
threadShouldThrow("InterruptedException"); |
1174 |
|
return result; |
1175 |
|
} catch (InterruptedException success) { |
1176 |
< |
} catch (Throwable t) { |
1177 |
< |
threadUnexpectedException(t); |
1176 |
> |
threadAssertFalse(Thread.interrupted()); |
1177 |
> |
} catch (Throwable fail) { |
1178 |
> |
threadUnexpectedException(fail); |
1179 |
|
} |
1180 |
|
return null; |
1181 |
|
} |
1215 |
|
public void await(CountDownLatch latch) { |
1216 |
|
try { |
1217 |
|
assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); |
1218 |
< |
} catch (Throwable t) { |
1219 |
< |
threadUnexpectedException(t); |
1218 |
> |
} catch (Throwable fail) { |
1219 |
> |
threadUnexpectedException(fail); |
1220 |
> |
} |
1221 |
> |
} |
1222 |
> |
|
1223 |
> |
public void await(Semaphore semaphore) { |
1224 |
> |
try { |
1225 |
> |
assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
1226 |
> |
} catch (Throwable fail) { |
1227 |
> |
threadUnexpectedException(fail); |
1228 |
|
} |
1229 |
|
} |
1230 |
|
|
1231 |
+ |
// /** |
1232 |
+ |
// * Spin-waits up to LONG_DELAY_MS until flag becomes true. |
1233 |
+ |
// */ |
1234 |
+ |
// public void await(AtomicBoolean flag) { |
1235 |
+ |
// await(flag, LONG_DELAY_MS); |
1236 |
+ |
// } |
1237 |
+ |
|
1238 |
+ |
// /** |
1239 |
+ |
// * Spin-waits up to the specified timeout until flag becomes true. |
1240 |
+ |
// */ |
1241 |
+ |
// public void await(AtomicBoolean flag, long timeoutMillis) { |
1242 |
+ |
// long startTime = System.nanoTime(); |
1243 |
+ |
// while (!flag.get()) { |
1244 |
+ |
// if (millisElapsedSince(startTime) > timeoutMillis) |
1245 |
+ |
// throw new AssertionFailedError("timed out"); |
1246 |
+ |
// Thread.yield(); |
1247 |
+ |
// } |
1248 |
+ |
// } |
1249 |
+ |
|
1250 |
|
public static class NPETask implements Callable<String> { |
1251 |
|
public String call() { throw new NullPointerException(); } |
1252 |
|
} |
1415 |
|
public abstract class CheckedRecursiveAction extends RecursiveAction { |
1416 |
|
protected abstract void realCompute() throws Throwable; |
1417 |
|
|
1418 |
< |
public final void compute() { |
1418 |
> |
@Override protected final void compute() { |
1419 |
|
try { |
1420 |
|
realCompute(); |
1421 |
< |
} catch (Throwable t) { |
1422 |
< |
threadUnexpectedException(t); |
1421 |
> |
} catch (Throwable fail) { |
1422 |
> |
threadUnexpectedException(fail); |
1423 |
|
} |
1424 |
|
} |
1425 |
|
} |
1430 |
|
public abstract class CheckedRecursiveTask<T> extends RecursiveTask<T> { |
1431 |
|
protected abstract T realCompute() throws Throwable; |
1432 |
|
|
1433 |
< |
public final T compute() { |
1433 |
> |
@Override protected final T compute() { |
1434 |
|
try { |
1435 |
|
return realCompute(); |
1436 |
< |
} catch (Throwable t) { |
1437 |
< |
threadUnexpectedException(t); |
1436 |
> |
} catch (Throwable fail) { |
1437 |
> |
threadUnexpectedException(fail); |
1438 |
|
return null; |
1439 |
|
} |
1440 |
|
} |
1449 |
|
} |
1450 |
|
|
1451 |
|
/** |
1452 |
< |
* A CyclicBarrier that fails with AssertionFailedErrors instead |
1453 |
< |
* of throwing checked exceptions. |
1452 |
> |
* A CyclicBarrier that uses timed await and fails with |
1453 |
> |
* AssertionFailedErrors instead of throwing checked exceptions. |
1454 |
|
*/ |
1455 |
|
public class CheckedBarrier extends CyclicBarrier { |
1456 |
|
public CheckedBarrier(int parties) { super(parties); } |
1457 |
|
|
1458 |
|
public int await() { |
1459 |
|
try { |
1460 |
< |
return super.await(); |
1461 |
< |
} catch (Exception e) { |
1460 |
> |
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1461 |
> |
} catch (TimeoutException timedOut) { |
1462 |
> |
throw new AssertionFailedError("timed out"); |
1463 |
> |
} catch (Exception fail) { |
1464 |
|
AssertionFailedError afe = |
1465 |
< |
new AssertionFailedError("Unexpected exception: " + e); |
1466 |
< |
afe.initCause(e); |
1465 |
> |
new AssertionFailedError("Unexpected exception: " + fail); |
1466 |
> |
afe.initCause(fail); |
1467 |
|
throw afe; |
1468 |
|
} |
1469 |
|
} |
1470 |
|
} |
1471 |
|
|
1472 |
< |
public void checkEmpty(BlockingQueue q) { |
1472 |
> |
void checkEmpty(BlockingQueue q) { |
1473 |
|
try { |
1474 |
|
assertTrue(q.isEmpty()); |
1475 |
|
assertEquals(0, q.size()); |
1491 |
|
q.remove(); |
1492 |
|
shouldThrow(); |
1493 |
|
} catch (NoSuchElementException success) {} |
1494 |
< |
} catch (InterruptedException ie) { |
1106 |
< |
threadUnexpectedException(ie); |
1107 |
< |
} |
1494 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
1495 |
|
} |
1496 |
|
|
1497 |
< |
@SuppressWarnings("unchecked") |
1498 |
< |
public <T> T serialClone(T o) { |
1497 |
> |
void assertSerialEquals(Object x, Object y) { |
1498 |
> |
assertTrue(Arrays.equals(serialBytes(x), serialBytes(y))); |
1499 |
> |
} |
1500 |
> |
|
1501 |
> |
void assertNotSerialEquals(Object x, Object y) { |
1502 |
> |
assertFalse(Arrays.equals(serialBytes(x), serialBytes(y))); |
1503 |
> |
} |
1504 |
> |
|
1505 |
> |
byte[] serialBytes(Object o) { |
1506 |
|
try { |
1507 |
|
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1508 |
|
ObjectOutputStream oos = new ObjectOutputStream(bos); |
1509 |
|
oos.writeObject(o); |
1510 |
|
oos.flush(); |
1511 |
|
oos.close(); |
1512 |
< |
ByteArrayInputStream bin = |
1513 |
< |
new ByteArrayInputStream(bos.toByteArray()); |
1514 |
< |
ObjectInputStream ois = new ObjectInputStream(bin); |
1515 |
< |
return (T) ois.readObject(); |
1516 |
< |
} catch (Throwable t) { |
1517 |
< |
threadUnexpectedException(t); |
1512 |
> |
return bos.toByteArray(); |
1513 |
> |
} catch (Throwable fail) { |
1514 |
> |
threadUnexpectedException(fail); |
1515 |
> |
return new byte[0]; |
1516 |
> |
} |
1517 |
> |
} |
1518 |
> |
|
1519 |
> |
@SuppressWarnings("unchecked") |
1520 |
> |
<T> T serialClone(T o) { |
1521 |
> |
try { |
1522 |
> |
ObjectInputStream ois = new ObjectInputStream |
1523 |
> |
(new ByteArrayInputStream(serialBytes(o))); |
1524 |
> |
T clone = (T) ois.readObject(); |
1525 |
> |
assertSame(o.getClass(), clone.getClass()); |
1526 |
> |
return clone; |
1527 |
> |
} catch (Throwable fail) { |
1528 |
> |
threadUnexpectedException(fail); |
1529 |
|
return null; |
1530 |
|
} |
1531 |
|
} |
1532 |
+ |
|
1533 |
+ |
public void assertThrows(Class<? extends Throwable> expectedExceptionClass, |
1534 |
+ |
Runnable... throwingActions) { |
1535 |
+ |
for (Runnable throwingAction : throwingActions) { |
1536 |
+ |
boolean threw = false; |
1537 |
+ |
try { throwingAction.run(); } |
1538 |
+ |
catch (Throwable t) { |
1539 |
+ |
threw = true; |
1540 |
+ |
if (!expectedExceptionClass.isInstance(t)) { |
1541 |
+ |
AssertionFailedError afe = |
1542 |
+ |
new AssertionFailedError |
1543 |
+ |
("Expected " + expectedExceptionClass.getName() + |
1544 |
+ |
", got " + t.getClass().getName()); |
1545 |
+ |
afe.initCause(t); |
1546 |
+ |
threadUnexpectedException(afe); |
1547 |
+ |
} |
1548 |
+ |
} |
1549 |
+ |
if (!threw) |
1550 |
+ |
shouldThrow(expectedExceptionClass.getName()); |
1551 |
+ |
} |
1552 |
+ |
} |
1553 |
+ |
|
1554 |
+ |
public void assertIteratorExhausted(Iterator<?> it) { |
1555 |
+ |
try { |
1556 |
+ |
it.next(); |
1557 |
+ |
shouldThrow(); |
1558 |
+ |
} catch (NoSuchElementException success) {} |
1559 |
+ |
assertFalse(it.hasNext()); |
1560 |
+ |
} |
1561 |
|
} |