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.ForkJoinPool; |
44 |
> |
import java.util.concurrent.Future; |
45 |
> |
import java.util.concurrent.RecursiveAction; |
46 |
> |
import java.util.concurrent.RecursiveTask; |
47 |
> |
import java.util.concurrent.RejectedExecutionHandler; |
48 |
> |
import java.util.concurrent.Semaphore; |
49 |
> |
import java.util.concurrent.ThreadFactory; |
50 |
> |
import java.util.concurrent.ThreadPoolExecutor; |
51 |
> |
import java.util.concurrent.TimeoutException; |
52 |
|
import java.util.concurrent.atomic.AtomicReference; |
53 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
54 |
< |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
55 |
< |
import java.security.CodeSource; |
56 |
< |
import java.security.Permission; |
57 |
< |
import java.security.PermissionCollection; |
58 |
< |
import java.security.Permissions; |
59 |
< |
import java.security.Policy; |
34 |
< |
import java.security.ProtectionDomain; |
35 |
< |
import java.security.SecurityPermission; |
53 |
> |
import java.util.regex.Pattern; |
54 |
> |
|
55 |
> |
import junit.framework.AssertionFailedError; |
56 |
> |
import junit.framework.Test; |
57 |
> |
import junit.framework.TestCase; |
58 |
> |
import junit.framework.TestResult; |
59 |
> |
import junit.framework.TestSuite; |
60 |
|
|
61 |
|
/** |
62 |
|
* Base class for JSR166 Junit TCK tests. Defines some constants, |
68 |
|
* |
69 |
|
* <ol> |
70 |
|
* |
71 |
< |
* <li> All assertions in code running in generated threads must use |
71 |
> |
* <li>All assertions in code running in generated threads must use |
72 |
|
* the forms {@link #threadFail}, {@link #threadAssertTrue}, {@link |
73 |
|
* #threadAssertEquals}, or {@link #threadAssertNull}, (not |
74 |
|
* {@code fail}, {@code assertTrue}, etc.) It is OK (but not |
75 |
|
* particularly recommended) for other code to use these forms too. |
76 |
|
* Only the most typically used JUnit assertion methods are defined |
77 |
< |
* this way, but enough to live with.</li> |
77 |
> |
* this way, but enough to live with. |
78 |
|
* |
79 |
< |
* <li> If you override {@link #setUp} or {@link #tearDown}, make sure |
79 |
> |
* <li>If you override {@link #setUp} or {@link #tearDown}, make sure |
80 |
|
* to invoke {@code super.setUp} and {@code super.tearDown} within |
81 |
|
* them. These methods are used to clear and check for thread |
82 |
< |
* assertion failures.</li> |
82 |
> |
* assertion failures. |
83 |
|
* |
84 |
|
* <li>All delays and timeouts must use one of the constants {@code |
85 |
|
* SHORT_DELAY_MS}, {@code SMALL_DELAY_MS}, {@code MEDIUM_DELAY_MS}, |
90 |
|
* is always discriminable as larger than SHORT and smaller than |
91 |
|
* MEDIUM. And so on. These constants are set to conservative values, |
92 |
|
* but even so, if there is ever any doubt, they can all be increased |
93 |
< |
* in one spot to rerun tests on slower platforms.</li> |
93 |
> |
* in one spot to rerun tests on slower platforms. |
94 |
|
* |
95 |
< |
* <li> All threads generated must be joined inside each test case |
95 |
> |
* <li>All threads generated must be joined inside each test case |
96 |
|
* method (or {@code fail} to do so) before returning from the |
97 |
|
* method. The {@code joinPool} method can be used to do this when |
98 |
< |
* using Executors.</li> |
98 |
> |
* using Executors. |
99 |
|
* |
100 |
|
* </ol> |
101 |
|
* |
102 |
|
* <p><b>Other notes</b> |
103 |
|
* <ul> |
104 |
|
* |
105 |
< |
* <li> Usually, there is one testcase method per JSR166 method |
105 |
> |
* <li>Usually, there is one testcase method per JSR166 method |
106 |
|
* covering "normal" operation, and then as many exception-testing |
107 |
|
* methods as there are exceptions the method can throw. Sometimes |
108 |
|
* there are multiple tests per JSR166 method when the different |
109 |
|
* "normal" behaviors differ significantly. And sometimes testcases |
110 |
|
* cover multiple methods when they cannot be tested in |
111 |
< |
* isolation.</li> |
111 |
> |
* isolation. |
112 |
|
* |
113 |
< |
* <li> The documentation style for testcases is to provide as javadoc |
113 |
> |
* <li>The documentation style for testcases is to provide as javadoc |
114 |
|
* a simple sentence or two describing the property that the testcase |
115 |
|
* method purports to test. The javadocs do not say anything about how |
116 |
< |
* the property is tested. To find out, read the code.</li> |
116 |
> |
* the property is tested. To find out, read the code. |
117 |
|
* |
118 |
< |
* <li> These tests are "conformance tests", and do not attempt to |
118 |
> |
* <li>These tests are "conformance tests", and do not attempt to |
119 |
|
* test throughput, latency, scalability or other performance factors |
120 |
|
* (see the separate "jtreg" tests for a set intended to check these |
121 |
|
* for the most central aspects of functionality.) So, most tests use |
122 |
|
* the smallest sensible numbers of threads, collection sizes, etc |
123 |
< |
* needed to check basic conformance.</li> |
123 |
> |
* needed to check basic conformance. |
124 |
|
* |
125 |
|
* <li>The test classes currently do not declare inclusion in |
126 |
|
* any particular package to simplify things for people integrating |
127 |
< |
* them in TCK test suites.</li> |
127 |
> |
* them in TCK test suites. |
128 |
|
* |
129 |
< |
* <li> As a convenience, the {@code main} of this class (JSR166TestCase) |
130 |
< |
* runs all JSR166 unit tests.</li> |
129 |
> |
* <li>As a convenience, the {@code main} of this class (JSR166TestCase) |
130 |
> |
* runs all JSR166 unit tests. |
131 |
|
* |
132 |
|
* </ul> |
133 |
|
*/ |
139 |
|
Boolean.getBoolean("jsr166.expensiveTests"); |
140 |
|
|
141 |
|
/** |
142 |
+ |
* If true, also run tests that are not part of the official tck |
143 |
+ |
* because they test unspecified implementation details. |
144 |
+ |
*/ |
145 |
+ |
protected static final boolean testImplementationDetails = |
146 |
+ |
Boolean.getBoolean("jsr166.testImplementationDetails"); |
147 |
+ |
|
148 |
+ |
/** |
149 |
|
* If true, report on stdout all "slow" tests, that is, ones that |
150 |
|
* take more than profileThreshold milliseconds to execute. |
151 |
|
*/ |
159 |
|
private static final long profileThreshold = |
160 |
|
Long.getLong("jsr166.profileThreshold", 100); |
161 |
|
|
162 |
+ |
/** |
163 |
+ |
* The number of repetitions per test (for tickling rare bugs). |
164 |
+ |
*/ |
165 |
+ |
private static final int runsPerTest = |
166 |
+ |
Integer.getInteger("jsr166.runsPerTest", 1); |
167 |
+ |
|
168 |
+ |
/** |
169 |
+ |
* The number of repetitions of the test suite (for finding leaks?). |
170 |
+ |
*/ |
171 |
+ |
private static final int suiteRuns = |
172 |
+ |
Integer.getInteger("jsr166.suiteRuns", 1); |
173 |
+ |
|
174 |
+ |
public JSR166TestCase() { super(); } |
175 |
+ |
public JSR166TestCase(String name) { super(name); } |
176 |
+ |
|
177 |
+ |
/** |
178 |
+ |
* A filter for tests to run, matching strings of the form |
179 |
+ |
* methodName(className), e.g. "testInvokeAll5(ForkJoinPoolTest)" |
180 |
+ |
* Usefully combined with jsr166.runsPerTest. |
181 |
+ |
*/ |
182 |
+ |
private static final Pattern methodFilter = methodFilter(); |
183 |
+ |
|
184 |
+ |
private static Pattern methodFilter() { |
185 |
+ |
String regex = System.getProperty("jsr166.methodFilter"); |
186 |
+ |
return (regex == null) ? null : Pattern.compile(regex); |
187 |
+ |
} |
188 |
+ |
|
189 |
+ |
public void runBare() throws Throwable { |
190 |
+ |
if (methodFilter == null |
191 |
+ |
|| methodFilter.matcher(toString()).find()) |
192 |
+ |
super.runBare(); |
193 |
+ |
} |
194 |
+ |
|
195 |
|
protected void runTest() throws Throwable { |
196 |
< |
if (profileTests) |
197 |
< |
runTestProfiled(); |
198 |
< |
else |
199 |
< |
super.runTest(); |
196 |
> |
for (int i = 0; i < runsPerTest; i++) { |
197 |
> |
if (profileTests) |
198 |
> |
runTestProfiled(); |
199 |
> |
else |
200 |
> |
super.runTest(); |
201 |
> |
} |
202 |
|
} |
203 |
|
|
204 |
|
protected void runTestProfiled() throws Throwable { |
205 |
< |
long t0 = System.nanoTime(); |
206 |
< |
try { |
205 |
> |
for (int i = 0; i < 2; i++) { |
206 |
> |
long startTime = System.nanoTime(); |
207 |
|
super.runTest(); |
208 |
< |
} finally { |
209 |
< |
long elapsedMillis = |
210 |
< |
(System.nanoTime() - t0) / (1000L * 1000L); |
211 |
< |
if (elapsedMillis >= profileThreshold) |
208 |
> |
long elapsedMillis = millisElapsedSince(startTime); |
209 |
> |
if (elapsedMillis < profileThreshold) |
210 |
> |
break; |
211 |
> |
// Never report first run of any test; treat it as a |
212 |
> |
// warmup run, notably to trigger all needed classloading, |
213 |
> |
if (i > 0) |
214 |
|
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); |
215 |
|
} |
216 |
|
} |
217 |
|
|
218 |
|
/** |
219 |
|
* Runs all JSR166 unit tests using junit.textui.TestRunner. |
152 |
– |
* Optional command line arg provides the number of iterations to |
153 |
– |
* repeat running the tests. |
220 |
|
*/ |
221 |
|
public static void main(String[] args) { |
222 |
+ |
main(suite(), args); |
223 |
+ |
} |
224 |
+ |
|
225 |
+ |
/** |
226 |
+ |
* Runs all unit tests in the given test suite. |
227 |
+ |
* Actual behavior influenced by jsr166.* system properties. |
228 |
+ |
*/ |
229 |
+ |
static void main(Test suite, String[] args) { |
230 |
|
if (useSecurityManager) { |
231 |
|
System.err.println("Setting a permissive security manager"); |
232 |
|
Policy.setPolicy(permissivePolicy()); |
233 |
|
System.setSecurityManager(new SecurityManager()); |
234 |
|
} |
235 |
< |
int iters = (args.length == 0) ? 1 : Integer.parseInt(args[0]); |
236 |
< |
|
237 |
< |
Test s = suite(); |
238 |
< |
for (int i = 0; i < iters; ++i) { |
165 |
< |
junit.textui.TestRunner.run(s); |
235 |
> |
for (int i = 0; i < suiteRuns; i++) { |
236 |
> |
TestResult result = junit.textui.TestRunner.run(suite); |
237 |
> |
if (!result.wasSuccessful()) |
238 |
> |
System.exit(1); |
239 |
|
System.gc(); |
240 |
|
System.runFinalization(); |
241 |
|
} |
169 |
– |
System.exit(0); |
242 |
|
} |
243 |
|
|
244 |
|
public static TestSuite newTestSuite(Object... suiteOrClasses) { |
269 |
|
} |
270 |
|
|
271 |
|
public static final double JAVA_CLASS_VERSION; |
272 |
+ |
public static final String JAVA_SPECIFICATION_VERSION; |
273 |
|
static { |
274 |
|
try { |
275 |
|
JAVA_CLASS_VERSION = java.security.AccessController.doPrivileged( |
276 |
|
new java.security.PrivilegedAction<Double>() { |
277 |
|
public Double run() { |
278 |
|
return Double.valueOf(System.getProperty("java.class.version"));}}); |
279 |
+ |
JAVA_SPECIFICATION_VERSION = java.security.AccessController.doPrivileged( |
280 |
+ |
new java.security.PrivilegedAction<String>() { |
281 |
+ |
public String run() { |
282 |
+ |
return System.getProperty("java.specification.version");}}); |
283 |
|
} catch (Throwable t) { |
284 |
|
throw new Error(t); |
285 |
|
} |
288 |
|
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
289 |
|
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
290 |
|
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
291 |
+ |
public static boolean atLeastJava9() { |
292 |
+ |
return JAVA_CLASS_VERSION >= 53.0 |
293 |
+ |
// As of 2015-09, java9 still uses 52.0 class file version |
294 |
+ |
|| JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?(9|[0-9][0-9])$"); |
295 |
+ |
} |
296 |
+ |
public static boolean atLeastJava10() { |
297 |
+ |
return JAVA_CLASS_VERSION >= 54.0 |
298 |
+ |
|| JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?[0-9][0-9]$"); |
299 |
+ |
} |
300 |
|
|
301 |
|
/** |
302 |
|
* Collects all JSR166 unit tests as one suite. |
372 |
|
// Java8+ test classes |
373 |
|
if (atLeastJava8()) { |
374 |
|
String[] java8TestClassNames = { |
375 |
< |
"StampedLockTest", |
375 |
> |
"Atomic8Test", |
376 |
> |
"CompletableFutureTest", |
377 |
> |
"ConcurrentHashMap8Test", |
378 |
> |
"CountedCompleterTest", |
379 |
> |
"DoubleAccumulatorTest", |
380 |
> |
"DoubleAdderTest", |
381 |
|
"ForkJoinPool8Test", |
382 |
+ |
"ForkJoinTask8Test", |
383 |
+ |
"LongAccumulatorTest", |
384 |
+ |
"LongAdderTest", |
385 |
+ |
"SplittableRandomTest", |
386 |
+ |
"StampedLockTest", |
387 |
+ |
"SubmissionPublisherTest", |
388 |
+ |
"ThreadLocalRandom8Test", |
389 |
|
}; |
390 |
|
addNamedTestClasses(suite, java8TestClassNames); |
391 |
|
} |
392 |
|
|
393 |
+ |
// Java9+ test classes |
394 |
+ |
if (atLeastJava9()) { |
395 |
+ |
String[] java9TestClassNames = { |
396 |
+ |
// Currently empty, but expecting varhandle tests |
397 |
+ |
}; |
398 |
+ |
addNamedTestClasses(suite, java9TestClassNames); |
399 |
+ |
} |
400 |
+ |
|
401 |
|
return suite; |
402 |
|
} |
403 |
|
|
404 |
+ |
/** Returns list of junit-style test method names in given class. */ |
405 |
+ |
public static ArrayList<String> testMethodNames(Class<?> testClass) { |
406 |
+ |
Method[] methods = testClass.getDeclaredMethods(); |
407 |
+ |
ArrayList<String> names = new ArrayList<String>(methods.length); |
408 |
+ |
for (Method method : methods) { |
409 |
+ |
if (method.getName().startsWith("test") |
410 |
+ |
&& Modifier.isPublic(method.getModifiers()) |
411 |
+ |
// method.getParameterCount() requires jdk8+ |
412 |
+ |
&& method.getParameterTypes().length == 0) { |
413 |
+ |
names.add(method.getName()); |
414 |
+ |
} |
415 |
+ |
} |
416 |
+ |
return names; |
417 |
+ |
} |
418 |
+ |
|
419 |
+ |
/** |
420 |
+ |
* Returns junit-style testSuite for the given test class, but |
421 |
+ |
* parameterized by passing extra data to each test. |
422 |
+ |
*/ |
423 |
+ |
public static <ExtraData> Test parameterizedTestSuite |
424 |
+ |
(Class<? extends JSR166TestCase> testClass, |
425 |
+ |
Class<ExtraData> dataClass, |
426 |
+ |
ExtraData data) { |
427 |
+ |
try { |
428 |
+ |
TestSuite suite = new TestSuite(); |
429 |
+ |
Constructor c = |
430 |
+ |
testClass.getDeclaredConstructor(dataClass, String.class); |
431 |
+ |
for (String methodName : testMethodNames(testClass)) |
432 |
+ |
suite.addTest((Test) c.newInstance(data, methodName)); |
433 |
+ |
return suite; |
434 |
+ |
} catch (Exception e) { |
435 |
+ |
throw new Error(e); |
436 |
+ |
} |
437 |
+ |
} |
438 |
+ |
|
439 |
+ |
/** |
440 |
+ |
* Returns junit-style testSuite for the jdk8 extension of the |
441 |
+ |
* given test class, but parameterized by passing extra data to |
442 |
+ |
* each test. Uses reflection to allow compilation in jdk7. |
443 |
+ |
*/ |
444 |
+ |
public static <ExtraData> Test jdk8ParameterizedTestSuite |
445 |
+ |
(Class<? extends JSR166TestCase> testClass, |
446 |
+ |
Class<ExtraData> dataClass, |
447 |
+ |
ExtraData data) { |
448 |
+ |
if (atLeastJava8()) { |
449 |
+ |
String name = testClass.getName(); |
450 |
+ |
String name8 = name.replaceAll("Test$", "8Test"); |
451 |
+ |
if (name.equals(name8)) throw new Error(name); |
452 |
+ |
try { |
453 |
+ |
return (Test) |
454 |
+ |
Class.forName(name8) |
455 |
+ |
.getMethod("testSuite", new Class[] { dataClass }) |
456 |
+ |
.invoke(null, data); |
457 |
+ |
} catch (Exception e) { |
458 |
+ |
throw new Error(e); |
459 |
+ |
} |
460 |
+ |
} else { |
461 |
+ |
return new TestSuite(); |
462 |
+ |
} |
463 |
+ |
|
464 |
+ |
} |
465 |
+ |
|
466 |
+ |
// Delays for timing-dependent tests, in milliseconds. |
467 |
|
|
468 |
|
public static long SHORT_DELAY_MS; |
469 |
|
public static long SMALL_DELAY_MS; |
470 |
|
public static long MEDIUM_DELAY_MS; |
471 |
|
public static long LONG_DELAY_MS; |
472 |
|
|
304 |
– |
|
473 |
|
/** |
474 |
|
* Returns the shortest timed delay. This could |
475 |
|
* be reimplemented to use for example a Property. |
497 |
|
} |
498 |
|
|
499 |
|
/** |
500 |
< |
* Returns a new Date instance representing a time delayMillis |
501 |
< |
* milliseconds in the future. |
500 |
> |
* Returns a new Date instance representing a time at least |
501 |
> |
* delayMillis milliseconds in the future. |
502 |
|
*/ |
503 |
|
Date delayedDate(long delayMillis) { |
504 |
< |
return new Date(System.currentTimeMillis() + delayMillis); |
504 |
> |
// Add 1 because currentTimeMillis is known to round into the past. |
505 |
> |
return new Date(System.currentTimeMillis() + delayMillis + 1); |
506 |
|
} |
507 |
|
|
508 |
|
/** |
525 |
|
setDelays(); |
526 |
|
} |
527 |
|
|
528 |
+ |
void tearDownFail(String format, Object... args) { |
529 |
+ |
String msg = toString() + ": " + String.format(format, args); |
530 |
+ |
System.err.println(msg); |
531 |
+ |
printAllStackTraces(); |
532 |
+ |
throw new AssertionFailedError(msg); |
533 |
+ |
} |
534 |
+ |
|
535 |
|
/** |
536 |
|
* Extra checks that get done for all test cases. |
537 |
|
* |
559 |
|
} |
560 |
|
|
561 |
|
if (Thread.interrupted()) |
562 |
< |
throw new AssertionFailedError("interrupt status set in main thread"); |
562 |
> |
tearDownFail("interrupt status set in main thread"); |
563 |
> |
|
564 |
> |
checkForkJoinPoolThreadLeaks(); |
565 |
> |
} |
566 |
> |
|
567 |
> |
/** |
568 |
> |
* Finds missing try { ... } finally { joinPool(e); } |
569 |
> |
*/ |
570 |
> |
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
571 |
> |
Thread[] survivors = new Thread[7]; |
572 |
> |
int count = Thread.enumerate(survivors); |
573 |
> |
for (int i = 0; i < count; i++) { |
574 |
> |
Thread thread = survivors[i]; |
575 |
> |
String name = thread.getName(); |
576 |
> |
if (name.startsWith("ForkJoinPool-")) { |
577 |
> |
// give thread some time to terminate |
578 |
> |
thread.join(LONG_DELAY_MS); |
579 |
> |
if (thread.isAlive()) |
580 |
> |
tearDownFail("Found leaked ForkJoinPool thread thread=%s", |
581 |
> |
thread); |
582 |
> |
} |
583 |
> |
} |
584 |
> |
|
585 |
> |
if (!ForkJoinPool.commonPool() |
586 |
> |
.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS)) |
587 |
> |
tearDownFail("ForkJoin common pool thread stuck"); |
588 |
|
} |
589 |
|
|
590 |
|
/** |
665 |
|
public void threadAssertEquals(Object x, Object y) { |
666 |
|
try { |
667 |
|
assertEquals(x, y); |
668 |
< |
} catch (AssertionFailedError t) { |
669 |
< |
threadRecordFailure(t); |
670 |
< |
throw t; |
671 |
< |
} catch (Throwable t) { |
672 |
< |
threadUnexpectedException(t); |
668 |
> |
} catch (AssertionFailedError fail) { |
669 |
> |
threadRecordFailure(fail); |
670 |
> |
throw fail; |
671 |
> |
} catch (Throwable fail) { |
672 |
> |
threadUnexpectedException(fail); |
673 |
|
} |
674 |
|
} |
675 |
|
|
681 |
|
public void threadAssertSame(Object x, Object y) { |
682 |
|
try { |
683 |
|
assertSame(x, y); |
684 |
< |
} catch (AssertionFailedError t) { |
685 |
< |
threadRecordFailure(t); |
686 |
< |
throw t; |
684 |
> |
} catch (AssertionFailedError fail) { |
685 |
> |
threadRecordFailure(fail); |
686 |
> |
throw fail; |
687 |
|
} |
688 |
|
} |
689 |
|
|
745 |
|
/** |
746 |
|
* Waits out termination of a thread pool or fails doing so. |
747 |
|
*/ |
748 |
< |
void joinPool(ExecutorService exec) { |
748 |
> |
void joinPool(ExecutorService pool) { |
749 |
|
try { |
750 |
< |
exec.shutdown(); |
751 |
< |
assertTrue("ExecutorService did not terminate in a timely manner", |
752 |
< |
exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)); |
750 |
> |
pool.shutdown(); |
751 |
> |
if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) |
752 |
> |
fail("ExecutorService " + pool + |
753 |
> |
" did not terminate in a timely manner"); |
754 |
|
} catch (SecurityException ok) { |
755 |
|
// Allowed in case test doesn't have privs |
756 |
< |
} catch (InterruptedException ie) { |
756 |
> |
} catch (InterruptedException fail) { |
757 |
|
fail("Unexpected InterruptedException"); |
758 |
|
} |
759 |
|
} |
760 |
|
|
761 |
+ |
/** Like Runnable, but with the freedom to throw anything */ |
762 |
+ |
interface Action { public void run() throws Throwable; } |
763 |
+ |
|
764 |
+ |
/** |
765 |
+ |
* Runs all the given actions in parallel, failing if any fail. |
766 |
+ |
* Useful for running multiple variants of tests that are |
767 |
+ |
* necessarily individually slow because they must block. |
768 |
+ |
*/ |
769 |
+ |
void testInParallel(Action ... actions) { |
770 |
+ |
ExecutorService pool = Executors.newCachedThreadPool(); |
771 |
+ |
try { |
772 |
+ |
ArrayList<Future<?>> futures = new ArrayList<>(actions.length); |
773 |
+ |
for (final Action action : actions) |
774 |
+ |
futures.add(pool.submit(new CheckedRunnable() { |
775 |
+ |
public void realRun() throws Throwable { action.run();}})); |
776 |
+ |
for (Future<?> future : futures) |
777 |
+ |
try { |
778 |
+ |
assertNull(future.get(LONG_DELAY_MS, MILLISECONDS)); |
779 |
+ |
} catch (ExecutionException ex) { |
780 |
+ |
threadUnexpectedException(ex.getCause()); |
781 |
+ |
} catch (Exception ex) { |
782 |
+ |
threadUnexpectedException(ex); |
783 |
+ |
} |
784 |
+ |
} finally { |
785 |
+ |
joinPool(pool); |
786 |
+ |
} |
787 |
+ |
} |
788 |
+ |
|
789 |
|
/** |
790 |
|
* A debugging tool to print all stack traces, as jstack does. |
791 |
|
*/ |
812 |
|
// No need to optimize the failing case via Thread.join. |
813 |
|
delay(millis); |
814 |
|
assertTrue(thread.isAlive()); |
815 |
< |
} catch (InterruptedException ie) { |
815 |
> |
} catch (InterruptedException fail) { |
816 |
|
fail("Unexpected InterruptedException"); |
817 |
|
} |
818 |
|
} |
834 |
|
delay(millis); |
835 |
|
for (Thread thread : threads) |
836 |
|
assertTrue(thread.isAlive()); |
837 |
< |
} catch (InterruptedException ie) { |
837 |
> |
} catch (InterruptedException fail) { |
838 |
|
fail("Unexpected InterruptedException"); |
839 |
|
} |
840 |
|
} |
856 |
|
future.get(timeoutMillis, MILLISECONDS); |
857 |
|
shouldThrow(); |
858 |
|
} catch (TimeoutException success) { |
859 |
< |
} catch (Exception e) { |
860 |
< |
threadUnexpectedException(e); |
859 |
> |
} catch (Exception fail) { |
860 |
> |
threadUnexpectedException(fail); |
861 |
|
} finally { future.cancel(true); } |
862 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
863 |
|
} |
901 |
|
public static final Integer m6 = new Integer(-6); |
902 |
|
public static final Integer m10 = new Integer(-10); |
903 |
|
|
674 |
– |
|
904 |
|
/** |
905 |
|
* Runs Runnable r with a security policy that permits precisely |
906 |
|
* the specified permissions. If there is no current security |
1013 |
|
void sleep(long millis) { |
1014 |
|
try { |
1015 |
|
delay(millis); |
1016 |
< |
} catch (InterruptedException ie) { |
1016 |
> |
} catch (InterruptedException fail) { |
1017 |
|
AssertionFailedError afe = |
1018 |
|
new AssertionFailedError("Unexpected InterruptedException"); |
1019 |
< |
afe.initCause(ie); |
1019 |
> |
afe.initCause(fail); |
1020 |
|
throw afe; |
1021 |
|
} |
1022 |
|
} |
1054 |
|
/** |
1055 |
|
* Returns the number of milliseconds since time given by |
1056 |
|
* startNanoTime, which must have been previously returned from a |
1057 |
< |
* call to {@link System.nanoTime()}. |
1057 |
> |
* call to {@link System#nanoTime()}. |
1058 |
|
*/ |
1059 |
< |
long millisElapsedSince(long startNanoTime) { |
1059 |
> |
static long millisElapsedSince(long startNanoTime) { |
1060 |
|
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
1061 |
|
} |
1062 |
|
|
1063 |
+ |
// void assertTerminatesPromptly(long timeoutMillis, Runnable r) { |
1064 |
+ |
// long startTime = System.nanoTime(); |
1065 |
+ |
// try { |
1066 |
+ |
// r.run(); |
1067 |
+ |
// } catch (Throwable fail) { threadUnexpectedException(fail); } |
1068 |
+ |
// if (millisElapsedSince(startTime) > timeoutMillis/2) |
1069 |
+ |
// throw new AssertionFailedError("did not return promptly"); |
1070 |
+ |
// } |
1071 |
+ |
|
1072 |
+ |
// void assertTerminatesPromptly(Runnable r) { |
1073 |
+ |
// assertTerminatesPromptly(LONG_DELAY_MS/2, r); |
1074 |
+ |
// } |
1075 |
+ |
|
1076 |
+ |
/** |
1077 |
+ |
* Checks that timed f.get() returns the expected value, and does not |
1078 |
+ |
* wait for the timeout to elapse before returning. |
1079 |
+ |
*/ |
1080 |
+ |
<T> void checkTimedGet(Future<T> f, T expectedValue, long timeoutMillis) { |
1081 |
+ |
long startTime = System.nanoTime(); |
1082 |
+ |
try { |
1083 |
+ |
assertEquals(expectedValue, f.get(timeoutMillis, MILLISECONDS)); |
1084 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
1085 |
+ |
if (millisElapsedSince(startTime) > timeoutMillis/2) |
1086 |
+ |
throw new AssertionFailedError("timed get did not return promptly"); |
1087 |
+ |
} |
1088 |
+ |
|
1089 |
+ |
<T> void checkTimedGet(Future<T> f, T expectedValue) { |
1090 |
+ |
checkTimedGet(f, expectedValue, LONG_DELAY_MS); |
1091 |
+ |
} |
1092 |
+ |
|
1093 |
|
/** |
1094 |
|
* Returns a new started daemon Thread running the given runnable. |
1095 |
|
*/ |
1108 |
|
void awaitTermination(Thread t, long timeoutMillis) { |
1109 |
|
try { |
1110 |
|
t.join(timeoutMillis); |
1111 |
< |
} catch (InterruptedException ie) { |
1112 |
< |
threadUnexpectedException(ie); |
1111 |
> |
} catch (InterruptedException fail) { |
1112 |
> |
threadUnexpectedException(fail); |
1113 |
|
} finally { |
1114 |
|
if (t.getState() != Thread.State.TERMINATED) { |
1115 |
|
t.interrupt(); |
1135 |
|
public final void run() { |
1136 |
|
try { |
1137 |
|
realRun(); |
1138 |
< |
} catch (Throwable t) { |
1139 |
< |
threadUnexpectedException(t); |
1138 |
> |
} catch (Throwable fail) { |
1139 |
> |
threadUnexpectedException(fail); |
1140 |
|
} |
1141 |
|
} |
1142 |
|
} |
1190 |
|
threadShouldThrow("InterruptedException"); |
1191 |
|
} catch (InterruptedException success) { |
1192 |
|
threadAssertFalse(Thread.interrupted()); |
1193 |
< |
} catch (Throwable t) { |
1194 |
< |
threadUnexpectedException(t); |
1193 |
> |
} catch (Throwable fail) { |
1194 |
> |
threadUnexpectedException(fail); |
1195 |
|
} |
1196 |
|
} |
1197 |
|
} |
1202 |
|
public final T call() { |
1203 |
|
try { |
1204 |
|
return realCall(); |
1205 |
< |
} catch (Throwable t) { |
1206 |
< |
threadUnexpectedException(t); |
1205 |
> |
} catch (Throwable fail) { |
1206 |
> |
threadUnexpectedException(fail); |
1207 |
|
return null; |
1208 |
|
} |
1209 |
|
} |
1220 |
|
return result; |
1221 |
|
} catch (InterruptedException success) { |
1222 |
|
threadAssertFalse(Thread.interrupted()); |
1223 |
< |
} catch (Throwable t) { |
1224 |
< |
threadUnexpectedException(t); |
1223 |
> |
} catch (Throwable fail) { |
1224 |
> |
threadUnexpectedException(fail); |
1225 |
|
} |
1226 |
|
return null; |
1227 |
|
} |
1238 |
|
public static final String TEST_STRING = "a test string"; |
1239 |
|
|
1240 |
|
public static class StringTask implements Callable<String> { |
1241 |
< |
public String call() { return TEST_STRING; } |
1241 |
> |
final String value; |
1242 |
> |
public StringTask() { this(TEST_STRING); } |
1243 |
> |
public StringTask(String value) { this.value = value; } |
1244 |
> |
public String call() { return value; } |
1245 |
|
} |
1246 |
|
|
1247 |
|
public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) { |
1254 |
|
}}; |
1255 |
|
} |
1256 |
|
|
1257 |
+ |
public Runnable countDowner(final CountDownLatch latch) { |
1258 |
+ |
return new CheckedRunnable() { |
1259 |
+ |
public void realRun() throws InterruptedException { |
1260 |
+ |
latch.countDown(); |
1261 |
+ |
}}; |
1262 |
+ |
} |
1263 |
+ |
|
1264 |
|
public Runnable awaiter(final CountDownLatch latch) { |
1265 |
|
return new CheckedRunnable() { |
1266 |
|
public void realRun() throws InterruptedException { |
1271 |
|
public void await(CountDownLatch latch) { |
1272 |
|
try { |
1273 |
|
assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); |
1274 |
< |
} catch (Throwable t) { |
1275 |
< |
threadUnexpectedException(t); |
1274 |
> |
} catch (Throwable fail) { |
1275 |
> |
threadUnexpectedException(fail); |
1276 |
|
} |
1277 |
|
} |
1278 |
|
|
1279 |
|
public void await(Semaphore semaphore) { |
1280 |
|
try { |
1281 |
|
assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
1282 |
< |
} catch (Throwable t) { |
1283 |
< |
threadUnexpectedException(t); |
1282 |
> |
} catch (Throwable fail) { |
1283 |
> |
threadUnexpectedException(fail); |
1284 |
|
} |
1285 |
|
} |
1286 |
|
|
1471 |
|
public abstract class CheckedRecursiveAction extends RecursiveAction { |
1472 |
|
protected abstract void realCompute() throws Throwable; |
1473 |
|
|
1474 |
< |
public final void compute() { |
1474 |
> |
@Override protected final void compute() { |
1475 |
|
try { |
1476 |
|
realCompute(); |
1477 |
< |
} catch (Throwable t) { |
1478 |
< |
threadUnexpectedException(t); |
1477 |
> |
} catch (Throwable fail) { |
1478 |
> |
threadUnexpectedException(fail); |
1479 |
|
} |
1480 |
|
} |
1481 |
|
} |
1486 |
|
public abstract class CheckedRecursiveTask<T> extends RecursiveTask<T> { |
1487 |
|
protected abstract T realCompute() throws Throwable; |
1488 |
|
|
1489 |
< |
public final T compute() { |
1489 |
> |
@Override protected final T compute() { |
1490 |
|
try { |
1491 |
|
return realCompute(); |
1492 |
< |
} catch (Throwable t) { |
1493 |
< |
threadUnexpectedException(t); |
1492 |
> |
} catch (Throwable fail) { |
1493 |
> |
threadUnexpectedException(fail); |
1494 |
|
return null; |
1495 |
|
} |
1496 |
|
} |
1514 |
|
public int await() { |
1515 |
|
try { |
1516 |
|
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1517 |
< |
} catch (TimeoutException e) { |
1517 |
> |
} catch (TimeoutException timedOut) { |
1518 |
|
throw new AssertionFailedError("timed out"); |
1519 |
< |
} catch (Exception e) { |
1519 |
> |
} catch (Exception fail) { |
1520 |
|
AssertionFailedError afe = |
1521 |
< |
new AssertionFailedError("Unexpected exception: " + e); |
1522 |
< |
afe.initCause(e); |
1521 |
> |
new AssertionFailedError("Unexpected exception: " + fail); |
1522 |
> |
afe.initCause(fail); |
1523 |
|
throw afe; |
1524 |
|
} |
1525 |
|
} |
1547 |
|
q.remove(); |
1548 |
|
shouldThrow(); |
1549 |
|
} catch (NoSuchElementException success) {} |
1550 |
< |
} catch (InterruptedException ie) { |
1282 |
< |
threadUnexpectedException(ie); |
1283 |
< |
} |
1550 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
1551 |
|
} |
1552 |
|
|
1553 |
|
void assertSerialEquals(Object x, Object y) { |
1566 |
|
oos.flush(); |
1567 |
|
oos.close(); |
1568 |
|
return bos.toByteArray(); |
1569 |
< |
} catch (Throwable t) { |
1570 |
< |
threadUnexpectedException(t); |
1569 |
> |
} catch (Throwable fail) { |
1570 |
> |
threadUnexpectedException(fail); |
1571 |
|
return new byte[0]; |
1572 |
|
} |
1573 |
|
} |
1580 |
|
T clone = (T) ois.readObject(); |
1581 |
|
assertSame(o.getClass(), clone.getClass()); |
1582 |
|
return clone; |
1583 |
< |
} catch (Throwable t) { |
1584 |
< |
threadUnexpectedException(t); |
1583 |
> |
} catch (Throwable fail) { |
1584 |
> |
threadUnexpectedException(fail); |
1585 |
|
return null; |
1586 |
|
} |
1587 |
|
} |
1588 |
+ |
|
1589 |
+ |
public void assertThrows(Class<? extends Throwable> expectedExceptionClass, |
1590 |
+ |
Runnable... throwingActions) { |
1591 |
+ |
for (Runnable throwingAction : throwingActions) { |
1592 |
+ |
boolean threw = false; |
1593 |
+ |
try { throwingAction.run(); } |
1594 |
+ |
catch (Throwable t) { |
1595 |
+ |
threw = true; |
1596 |
+ |
if (!expectedExceptionClass.isInstance(t)) { |
1597 |
+ |
AssertionFailedError afe = |
1598 |
+ |
new AssertionFailedError |
1599 |
+ |
("Expected " + expectedExceptionClass.getName() + |
1600 |
+ |
", got " + t.getClass().getName()); |
1601 |
+ |
afe.initCause(t); |
1602 |
+ |
threadUnexpectedException(afe); |
1603 |
+ |
} |
1604 |
+ |
} |
1605 |
+ |
if (!threw) |
1606 |
+ |
shouldThrow(expectedExceptionClass.getName()); |
1607 |
+ |
} |
1608 |
+ |
} |
1609 |
+ |
|
1610 |
+ |
public void assertIteratorExhausted(Iterator<?> it) { |
1611 |
+ |
try { |
1612 |
+ |
it.next(); |
1613 |
+ |
shouldThrow(); |
1614 |
+ |
} catch (NoSuchElementException success) {} |
1615 |
+ |
assertFalse(it.hasNext()); |
1616 |
+ |
} |
1617 |
|
} |