6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
9 |
> |
/* |
10 |
> |
* @test |
11 |
> |
* @summary JSR-166 tck tests |
12 |
> |
* @modules java.management |
13 |
> |
* @build * |
14 |
> |
* @run junit/othervm/timeout=1000 -Djsr166.testImplementationDetails=true JSR166TestCase |
15 |
> |
*/ |
16 |
> |
|
17 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
18 |
> |
import static java.util.concurrent.TimeUnit.MINUTES; |
19 |
> |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
20 |
> |
|
21 |
|
import java.io.ByteArrayInputStream; |
22 |
|
import java.io.ByteArrayOutputStream; |
23 |
|
import java.io.ObjectInputStream; |
24 |
|
import java.io.ObjectOutputStream; |
25 |
|
import java.lang.management.ManagementFactory; |
26 |
|
import java.lang.management.ThreadInfo; |
27 |
+ |
import java.lang.management.ThreadMXBean; |
28 |
+ |
import java.lang.reflect.Constructor; |
29 |
|
import java.lang.reflect.Method; |
30 |
+ |
import java.lang.reflect.Modifier; |
31 |
+ |
import java.nio.file.Files; |
32 |
+ |
import java.nio.file.Paths; |
33 |
+ |
import java.security.CodeSource; |
34 |
+ |
import java.security.Permission; |
35 |
+ |
import java.security.PermissionCollection; |
36 |
+ |
import java.security.Permissions; |
37 |
+ |
import java.security.Policy; |
38 |
+ |
import java.security.ProtectionDomain; |
39 |
+ |
import java.security.SecurityPermission; |
40 |
|
import java.util.ArrayList; |
41 |
|
import java.util.Arrays; |
42 |
|
import java.util.Date; |
43 |
|
import java.util.Enumeration; |
44 |
+ |
import java.util.Iterator; |
45 |
|
import java.util.List; |
46 |
|
import java.util.NoSuchElementException; |
47 |
|
import java.util.PropertyPermission; |
48 |
< |
import java.util.concurrent.*; |
49 |
< |
import java.util.concurrent.atomic.AtomicBoolean; |
48 |
> |
import java.util.concurrent.BlockingQueue; |
49 |
> |
import java.util.concurrent.Callable; |
50 |
> |
import java.util.concurrent.CountDownLatch; |
51 |
> |
import java.util.concurrent.CyclicBarrier; |
52 |
> |
import java.util.concurrent.ExecutionException; |
53 |
> |
import java.util.concurrent.Executors; |
54 |
> |
import java.util.concurrent.ExecutorService; |
55 |
> |
import java.util.concurrent.ForkJoinPool; |
56 |
> |
import java.util.concurrent.Future; |
57 |
> |
import java.util.concurrent.RecursiveAction; |
58 |
> |
import java.util.concurrent.RecursiveTask; |
59 |
> |
import java.util.concurrent.RejectedExecutionHandler; |
60 |
> |
import java.util.concurrent.Semaphore; |
61 |
> |
import java.util.concurrent.ThreadFactory; |
62 |
> |
import java.util.concurrent.ThreadPoolExecutor; |
63 |
> |
import java.util.concurrent.TimeoutException; |
64 |
|
import java.util.concurrent.atomic.AtomicReference; |
65 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
66 |
< |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
67 |
< |
import java.security.CodeSource; |
68 |
< |
import java.security.Permission; |
69 |
< |
import java.security.PermissionCollection; |
70 |
< |
import java.security.Permissions; |
71 |
< |
import java.security.Policy; |
72 |
< |
import java.security.ProtectionDomain; |
35 |
< |
import java.security.SecurityPermission; |
65 |
> |
import java.util.regex.Matcher; |
66 |
> |
import java.util.regex.Pattern; |
67 |
> |
|
68 |
> |
import junit.framework.AssertionFailedError; |
69 |
> |
import junit.framework.Test; |
70 |
> |
import junit.framework.TestCase; |
71 |
> |
import junit.framework.TestResult; |
72 |
> |
import junit.framework.TestSuite; |
73 |
|
|
74 |
|
/** |
75 |
|
* Base class for JSR166 Junit TCK tests. Defines some constants, |
81 |
|
* |
82 |
|
* <ol> |
83 |
|
* |
84 |
< |
* <li> All assertions in code running in generated threads must use |
84 |
> |
* <li>All assertions in code running in generated threads must use |
85 |
|
* the forms {@link #threadFail}, {@link #threadAssertTrue}, {@link |
86 |
|
* #threadAssertEquals}, or {@link #threadAssertNull}, (not |
87 |
|
* {@code fail}, {@code assertTrue}, etc.) It is OK (but not |
88 |
|
* particularly recommended) for other code to use these forms too. |
89 |
|
* Only the most typically used JUnit assertion methods are defined |
90 |
< |
* this way, but enough to live with.</li> |
90 |
> |
* this way, but enough to live with. |
91 |
|
* |
92 |
< |
* <li> If you override {@link #setUp} or {@link #tearDown}, make sure |
92 |
> |
* <li>If you override {@link #setUp} or {@link #tearDown}, make sure |
93 |
|
* to invoke {@code super.setUp} and {@code super.tearDown} within |
94 |
|
* them. These methods are used to clear and check for thread |
95 |
< |
* assertion failures.</li> |
95 |
> |
* assertion failures. |
96 |
|
* |
97 |
|
* <li>All delays and timeouts must use one of the constants {@code |
98 |
|
* SHORT_DELAY_MS}, {@code SMALL_DELAY_MS}, {@code MEDIUM_DELAY_MS}, |
103 |
|
* is always discriminable as larger than SHORT and smaller than |
104 |
|
* MEDIUM. And so on. These constants are set to conservative values, |
105 |
|
* but even so, if there is ever any doubt, they can all be increased |
106 |
< |
* in one spot to rerun tests on slower platforms.</li> |
106 |
> |
* in one spot to rerun tests on slower platforms. |
107 |
|
* |
108 |
< |
* <li> All threads generated must be joined inside each test case |
108 |
> |
* <li>All threads generated must be joined inside each test case |
109 |
|
* method (or {@code fail} to do so) before returning from the |
110 |
|
* method. The {@code joinPool} method can be used to do this when |
111 |
< |
* using Executors.</li> |
111 |
> |
* using Executors. |
112 |
|
* |
113 |
|
* </ol> |
114 |
|
* |
115 |
|
* <p><b>Other notes</b> |
116 |
|
* <ul> |
117 |
|
* |
118 |
< |
* <li> Usually, there is one testcase method per JSR166 method |
118 |
> |
* <li>Usually, there is one testcase method per JSR166 method |
119 |
|
* covering "normal" operation, and then as many exception-testing |
120 |
|
* methods as there are exceptions the method can throw. Sometimes |
121 |
|
* there are multiple tests per JSR166 method when the different |
122 |
|
* "normal" behaviors differ significantly. And sometimes testcases |
123 |
< |
* cover multiple methods when they cannot be tested in |
87 |
< |
* isolation.</li> |
123 |
> |
* cover multiple methods when they cannot be tested in isolation. |
124 |
|
* |
125 |
< |
* <li> The documentation style for testcases is to provide as javadoc |
125 |
> |
* <li>The documentation style for testcases is to provide as javadoc |
126 |
|
* a simple sentence or two describing the property that the testcase |
127 |
|
* method purports to test. The javadocs do not say anything about how |
128 |
< |
* the property is tested. To find out, read the code.</li> |
128 |
> |
* the property is tested. To find out, read the code. |
129 |
|
* |
130 |
< |
* <li> These tests are "conformance tests", and do not attempt to |
130 |
> |
* <li>These tests are "conformance tests", and do not attempt to |
131 |
|
* test throughput, latency, scalability or other performance factors |
132 |
|
* (see the separate "jtreg" tests for a set intended to check these |
133 |
|
* for the most central aspects of functionality.) So, most tests use |
134 |
|
* the smallest sensible numbers of threads, collection sizes, etc |
135 |
< |
* needed to check basic conformance.</li> |
135 |
> |
* needed to check basic conformance. |
136 |
|
* |
137 |
|
* <li>The test classes currently do not declare inclusion in |
138 |
|
* any particular package to simplify things for people integrating |
139 |
< |
* them in TCK test suites.</li> |
139 |
> |
* them in TCK test suites. |
140 |
|
* |
141 |
< |
* <li> As a convenience, the {@code main} of this class (JSR166TestCase) |
142 |
< |
* runs all JSR166 unit tests.</li> |
141 |
> |
* <li>As a convenience, the {@code main} of this class (JSR166TestCase) |
142 |
> |
* runs all JSR166 unit tests. |
143 |
|
* |
144 |
|
* </ul> |
145 |
|
*/ |
151 |
|
Boolean.getBoolean("jsr166.expensiveTests"); |
152 |
|
|
153 |
|
/** |
154 |
+ |
* If true, also run tests that are not part of the official tck |
155 |
+ |
* because they test unspecified implementation details. |
156 |
+ |
*/ |
157 |
+ |
protected static final boolean testImplementationDetails = |
158 |
+ |
Boolean.getBoolean("jsr166.testImplementationDetails"); |
159 |
+ |
|
160 |
+ |
/** |
161 |
|
* If true, report on stdout all "slow" tests, that is, ones that |
162 |
|
* take more than profileThreshold milliseconds to execute. |
163 |
|
*/ |
177 |
|
private static final int runsPerTest = |
178 |
|
Integer.getInteger("jsr166.runsPerTest", 1); |
179 |
|
|
180 |
+ |
/** |
181 |
+ |
* The number of repetitions of the test suite (for finding leaks?). |
182 |
+ |
*/ |
183 |
+ |
private static final int suiteRuns = |
184 |
+ |
Integer.getInteger("jsr166.suiteRuns", 1); |
185 |
+ |
|
186 |
+ |
/** |
187 |
+ |
* The scaling factor to apply to standard delays used in tests. |
188 |
+ |
*/ |
189 |
+ |
private static final int delayFactor = |
190 |
+ |
Integer.getInteger("jsr166.delay.factor", 1); |
191 |
+ |
|
192 |
+ |
public JSR166TestCase() { super(); } |
193 |
+ |
public JSR166TestCase(String name) { super(name); } |
194 |
+ |
|
195 |
+ |
/** |
196 |
+ |
* A filter for tests to run, matching strings of the form |
197 |
+ |
* methodName(className), e.g. "testInvokeAll5(ForkJoinPoolTest)" |
198 |
+ |
* Usefully combined with jsr166.runsPerTest. |
199 |
+ |
*/ |
200 |
+ |
private static final Pattern methodFilter = methodFilter(); |
201 |
+ |
|
202 |
+ |
private static Pattern methodFilter() { |
203 |
+ |
String regex = System.getProperty("jsr166.methodFilter"); |
204 |
+ |
return (regex == null) ? null : Pattern.compile(regex); |
205 |
+ |
} |
206 |
+ |
|
207 |
+ |
// Instrumentation to debug very rare, but very annoying hung test runs. |
208 |
+ |
static volatile TestCase currentTestCase; |
209 |
+ |
// static volatile int currentRun = 0; |
210 |
+ |
static { |
211 |
+ |
Runnable checkForWedgedTest = new Runnable() { public void run() { |
212 |
+ |
// Avoid spurious reports with enormous runsPerTest. |
213 |
+ |
// A single test case run should never take more than 1 second. |
214 |
+ |
// But let's cap it at the high end too ... |
215 |
+ |
final int timeoutMinutes = |
216 |
+ |
Math.min(15, Math.max(runsPerTest / 60, 1)); |
217 |
+ |
for (TestCase lastTestCase = currentTestCase;;) { |
218 |
+ |
try { MINUTES.sleep(timeoutMinutes); } |
219 |
+ |
catch (InterruptedException unexpected) { break; } |
220 |
+ |
if (lastTestCase == currentTestCase) { |
221 |
+ |
System.err.printf( |
222 |
+ |
"Looks like we're stuck running test: %s%n", |
223 |
+ |
lastTestCase); |
224 |
+ |
// System.err.printf( |
225 |
+ |
// "Looks like we're stuck running test: %s (%d/%d)%n", |
226 |
+ |
// lastTestCase, currentRun, runsPerTest); |
227 |
+ |
// System.err.println("availableProcessors=" + |
228 |
+ |
// Runtime.getRuntime().availableProcessors()); |
229 |
+ |
// System.err.printf("cpu model = %s%n", cpuModel()); |
230 |
+ |
dumpTestThreads(); |
231 |
+ |
// one stack dump is probably enough; more would be spam |
232 |
+ |
break; |
233 |
+ |
} |
234 |
+ |
lastTestCase = currentTestCase; |
235 |
+ |
}}}; |
236 |
+ |
Thread thread = new Thread(checkForWedgedTest, "checkForWedgedTest"); |
237 |
+ |
thread.setDaemon(true); |
238 |
+ |
thread.start(); |
239 |
+ |
} |
240 |
+ |
|
241 |
+ |
// public static String cpuModel() { |
242 |
+ |
// try { |
243 |
+ |
// Matcher matcher = Pattern.compile("model name\\s*: (.*)") |
244 |
+ |
// .matcher(new String( |
245 |
+ |
// Files.readAllBytes(Paths.get("/proc/cpuinfo")), "UTF-8")); |
246 |
+ |
// matcher.find(); |
247 |
+ |
// return matcher.group(1); |
248 |
+ |
// } catch (Exception ex) { return null; } |
249 |
+ |
// } |
250 |
+ |
|
251 |
+ |
public void runBare() throws Throwable { |
252 |
+ |
currentTestCase = this; |
253 |
+ |
if (methodFilter == null |
254 |
+ |
|| methodFilter.matcher(toString()).find()) |
255 |
+ |
super.runBare(); |
256 |
+ |
} |
257 |
+ |
|
258 |
|
protected void runTest() throws Throwable { |
259 |
|
for (int i = 0; i < runsPerTest; i++) { |
260 |
+ |
// currentRun = i; |
261 |
|
if (profileTests) |
262 |
|
runTestProfiled(); |
263 |
|
else |
266 |
|
} |
267 |
|
|
268 |
|
protected void runTestProfiled() throws Throwable { |
269 |
< |
long t0 = System.nanoTime(); |
270 |
< |
try { |
269 |
> |
for (int i = 0; i < 2; i++) { |
270 |
> |
long startTime = System.nanoTime(); |
271 |
|
super.runTest(); |
272 |
< |
} finally { |
273 |
< |
long elapsedMillis = |
274 |
< |
(System.nanoTime() - t0) / (1000L * 1000L); |
275 |
< |
if (elapsedMillis >= profileThreshold) |
272 |
> |
long elapsedMillis = millisElapsedSince(startTime); |
273 |
> |
if (elapsedMillis < profileThreshold) |
274 |
> |
break; |
275 |
> |
// Never report first run of any test; treat it as a |
276 |
> |
// warmup run, notably to trigger all needed classloading, |
277 |
> |
if (i > 0) |
278 |
|
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); |
279 |
|
} |
280 |
|
} |
281 |
|
|
282 |
|
/** |
283 |
|
* Runs all JSR166 unit tests using junit.textui.TestRunner. |
160 |
– |
* Optional command line arg provides the number of iterations to |
161 |
– |
* repeat running the tests. |
284 |
|
*/ |
285 |
|
public static void main(String[] args) { |
286 |
+ |
main(suite(), args); |
287 |
+ |
} |
288 |
+ |
|
289 |
+ |
static class PithyResultPrinter extends junit.textui.ResultPrinter { |
290 |
+ |
PithyResultPrinter(java.io.PrintStream writer) { super(writer); } |
291 |
+ |
long runTime; |
292 |
+ |
public void startTest(Test test) {} |
293 |
+ |
protected void printHeader(long runTime) { |
294 |
+ |
this.runTime = runTime; // defer printing for later |
295 |
+ |
} |
296 |
+ |
protected void printFooter(TestResult result) { |
297 |
+ |
if (result.wasSuccessful()) { |
298 |
+ |
getWriter().println("OK (" + result.runCount() + " tests)" |
299 |
+ |
+ " Time: " + elapsedTimeAsString(runTime)); |
300 |
+ |
} else { |
301 |
+ |
getWriter().println("Time: " + elapsedTimeAsString(runTime)); |
302 |
+ |
super.printFooter(result); |
303 |
+ |
} |
304 |
+ |
} |
305 |
+ |
} |
306 |
+ |
|
307 |
+ |
/** |
308 |
+ |
* Returns a TestRunner that doesn't bother with unnecessary |
309 |
+ |
* fluff, like printing a "." for each test case. |
310 |
+ |
*/ |
311 |
+ |
static junit.textui.TestRunner newPithyTestRunner() { |
312 |
+ |
junit.textui.TestRunner runner = new junit.textui.TestRunner(); |
313 |
+ |
runner.setPrinter(new PithyResultPrinter(System.out)); |
314 |
+ |
return runner; |
315 |
+ |
} |
316 |
+ |
|
317 |
+ |
/** |
318 |
+ |
* Runs all unit tests in the given test suite. |
319 |
+ |
* Actual behavior influenced by jsr166.* system properties. |
320 |
+ |
*/ |
321 |
+ |
static void main(Test suite, String[] args) { |
322 |
|
if (useSecurityManager) { |
323 |
|
System.err.println("Setting a permissive security manager"); |
324 |
|
Policy.setPolicy(permissivePolicy()); |
325 |
|
System.setSecurityManager(new SecurityManager()); |
326 |
|
} |
327 |
< |
int iters = (args.length == 0) ? 1 : Integer.parseInt(args[0]); |
328 |
< |
|
329 |
< |
Test s = suite(); |
330 |
< |
for (int i = 0; i < iters; ++i) { |
173 |
< |
junit.textui.TestRunner.run(s); |
327 |
> |
for (int i = 0; i < suiteRuns; i++) { |
328 |
> |
TestResult result = newPithyTestRunner().doRun(suite); |
329 |
> |
if (!result.wasSuccessful()) |
330 |
> |
System.exit(1); |
331 |
|
System.gc(); |
332 |
|
System.runFinalization(); |
333 |
|
} |
177 |
– |
System.exit(0); |
334 |
|
} |
335 |
|
|
336 |
|
public static TestSuite newTestSuite(Object... suiteOrClasses) { |
361 |
|
} |
362 |
|
|
363 |
|
public static final double JAVA_CLASS_VERSION; |
364 |
+ |
public static final String JAVA_SPECIFICATION_VERSION; |
365 |
|
static { |
366 |
|
try { |
367 |
|
JAVA_CLASS_VERSION = java.security.AccessController.doPrivileged( |
368 |
|
new java.security.PrivilegedAction<Double>() { |
369 |
|
public Double run() { |
370 |
|
return Double.valueOf(System.getProperty("java.class.version"));}}); |
371 |
+ |
JAVA_SPECIFICATION_VERSION = java.security.AccessController.doPrivileged( |
372 |
+ |
new java.security.PrivilegedAction<String>() { |
373 |
+ |
public String run() { |
374 |
+ |
return System.getProperty("java.specification.version");}}); |
375 |
|
} catch (Throwable t) { |
376 |
|
throw new Error(t); |
377 |
|
} |
380 |
|
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
381 |
|
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
382 |
|
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
383 |
+ |
public static boolean atLeastJava9() { |
384 |
+ |
return JAVA_CLASS_VERSION >= 53.0 |
385 |
+ |
// As of 2015-09, java9 still uses 52.0 class file version |
386 |
+ |
|| JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?(9|[0-9][0-9])$"); |
387 |
+ |
} |
388 |
+ |
public static boolean atLeastJava10() { |
389 |
+ |
return JAVA_CLASS_VERSION >= 54.0 |
390 |
+ |
|| JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?[0-9][0-9]$"); |
391 |
+ |
} |
392 |
|
|
393 |
|
/** |
394 |
|
* Collects all JSR166 unit tests as one suite. |
464 |
|
// Java8+ test classes |
465 |
|
if (atLeastJava8()) { |
466 |
|
String[] java8TestClassNames = { |
467 |
+ |
"Atomic8Test", |
468 |
|
"CompletableFutureTest", |
469 |
|
"ConcurrentHashMap8Test", |
470 |
|
"CountedCompleterTest", |
471 |
|
"DoubleAccumulatorTest", |
472 |
|
"DoubleAdderTest", |
473 |
|
"ForkJoinPool8Test", |
474 |
+ |
"ForkJoinTask8Test", |
475 |
|
"LongAccumulatorTest", |
476 |
|
"LongAdderTest", |
477 |
+ |
"SplittableRandomTest", |
478 |
|
"StampedLockTest", |
479 |
+ |
"SubmissionPublisherTest", |
480 |
+ |
"ThreadLocalRandom8Test", |
481 |
|
}; |
482 |
|
addNamedTestClasses(suite, java8TestClassNames); |
483 |
|
} |
484 |
|
|
485 |
+ |
// Java9+ test classes |
486 |
+ |
if (atLeastJava9()) { |
487 |
+ |
String[] java9TestClassNames = { |
488 |
+ |
// Currently empty, but expecting varhandle tests |
489 |
+ |
}; |
490 |
+ |
addNamedTestClasses(suite, java9TestClassNames); |
491 |
+ |
} |
492 |
+ |
|
493 |
|
return suite; |
494 |
|
} |
495 |
|
|
496 |
+ |
/** Returns list of junit-style test method names in given class. */ |
497 |
+ |
public static ArrayList<String> testMethodNames(Class<?> testClass) { |
498 |
+ |
Method[] methods = testClass.getDeclaredMethods(); |
499 |
+ |
ArrayList<String> names = new ArrayList<String>(methods.length); |
500 |
+ |
for (Method method : methods) { |
501 |
+ |
if (method.getName().startsWith("test") |
502 |
+ |
&& Modifier.isPublic(method.getModifiers()) |
503 |
+ |
// method.getParameterCount() requires jdk8+ |
504 |
+ |
&& method.getParameterTypes().length == 0) { |
505 |
+ |
names.add(method.getName()); |
506 |
+ |
} |
507 |
+ |
} |
508 |
+ |
return names; |
509 |
+ |
} |
510 |
+ |
|
511 |
+ |
/** |
512 |
+ |
* Returns junit-style testSuite for the given test class, but |
513 |
+ |
* parameterized by passing extra data to each test. |
514 |
+ |
*/ |
515 |
+ |
public static <ExtraData> Test parameterizedTestSuite |
516 |
+ |
(Class<? extends JSR166TestCase> testClass, |
517 |
+ |
Class<ExtraData> dataClass, |
518 |
+ |
ExtraData data) { |
519 |
+ |
try { |
520 |
+ |
TestSuite suite = new TestSuite(); |
521 |
+ |
Constructor c = |
522 |
+ |
testClass.getDeclaredConstructor(dataClass, String.class); |
523 |
+ |
for (String methodName : testMethodNames(testClass)) |
524 |
+ |
suite.addTest((Test) c.newInstance(data, methodName)); |
525 |
+ |
return suite; |
526 |
+ |
} catch (Exception e) { |
527 |
+ |
throw new Error(e); |
528 |
+ |
} |
529 |
+ |
} |
530 |
+ |
|
531 |
+ |
/** |
532 |
+ |
* Returns junit-style testSuite for the jdk8 extension of the |
533 |
+ |
* given test class, but parameterized by passing extra data to |
534 |
+ |
* each test. Uses reflection to allow compilation in jdk7. |
535 |
+ |
*/ |
536 |
+ |
public static <ExtraData> Test jdk8ParameterizedTestSuite |
537 |
+ |
(Class<? extends JSR166TestCase> testClass, |
538 |
+ |
Class<ExtraData> dataClass, |
539 |
+ |
ExtraData data) { |
540 |
+ |
if (atLeastJava8()) { |
541 |
+ |
String name = testClass.getName(); |
542 |
+ |
String name8 = name.replaceAll("Test$", "8Test"); |
543 |
+ |
if (name.equals(name8)) throw new Error(name); |
544 |
+ |
try { |
545 |
+ |
return (Test) |
546 |
+ |
Class.forName(name8) |
547 |
+ |
.getMethod("testSuite", new Class[] { dataClass }) |
548 |
+ |
.invoke(null, data); |
549 |
+ |
} catch (Exception e) { |
550 |
+ |
throw new Error(e); |
551 |
+ |
} |
552 |
+ |
} else { |
553 |
+ |
return new TestSuite(); |
554 |
+ |
} |
555 |
+ |
} |
556 |
+ |
|
557 |
+ |
// Delays for timing-dependent tests, in milliseconds. |
558 |
|
|
559 |
|
public static long SHORT_DELAY_MS; |
560 |
|
public static long SMALL_DELAY_MS; |
561 |
|
public static long MEDIUM_DELAY_MS; |
562 |
|
public static long LONG_DELAY_MS; |
563 |
|
|
319 |
– |
|
564 |
|
/** |
565 |
< |
* Returns the shortest timed delay. This could |
566 |
< |
* be reimplemented to use for example a Property. |
565 |
> |
* Returns the shortest timed delay. This can be scaled up for |
566 |
> |
* slow machines using the jsr166.delay.factor system property. |
567 |
|
*/ |
568 |
|
protected long getShortDelay() { |
569 |
< |
return 50; |
569 |
> |
return 50 * delayFactor; |
570 |
|
} |
571 |
|
|
572 |
|
/** |
588 |
|
} |
589 |
|
|
590 |
|
/** |
591 |
< |
* Returns a new Date instance representing a time delayMillis |
592 |
< |
* milliseconds in the future. |
591 |
> |
* Returns a new Date instance representing a time at least |
592 |
> |
* delayMillis milliseconds in the future. |
593 |
|
*/ |
594 |
|
Date delayedDate(long delayMillis) { |
595 |
< |
return new Date(System.currentTimeMillis() + delayMillis); |
595 |
> |
// Add 1 because currentTimeMillis is known to round into the past. |
596 |
> |
return new Date(System.currentTimeMillis() + delayMillis + 1); |
597 |
|
} |
598 |
|
|
599 |
|
/** |
609 |
|
* the same test have no effect. |
610 |
|
*/ |
611 |
|
public void threadRecordFailure(Throwable t) { |
612 |
+ |
System.err.println(t); |
613 |
+ |
dumpTestThreads(); |
614 |
|
threadFailure.compareAndSet(null, t); |
615 |
|
} |
616 |
|
|
618 |
|
setDelays(); |
619 |
|
} |
620 |
|
|
621 |
+ |
void tearDownFail(String format, Object... args) { |
622 |
+ |
String msg = toString() + ": " + String.format(format, args); |
623 |
+ |
System.err.println(msg); |
624 |
+ |
dumpTestThreads(); |
625 |
+ |
throw new AssertionFailedError(msg); |
626 |
+ |
} |
627 |
+ |
|
628 |
|
/** |
629 |
|
* Extra checks that get done for all test cases. |
630 |
|
* |
652 |
|
} |
653 |
|
|
654 |
|
if (Thread.interrupted()) |
655 |
< |
throw new AssertionFailedError("interrupt status set in main thread"); |
655 |
> |
tearDownFail("interrupt status set in main thread"); |
656 |
|
|
657 |
|
checkForkJoinPoolThreadLeaks(); |
658 |
|
} |
659 |
|
|
660 |
|
/** |
661 |
< |
* Find missing try { ... } finally { joinPool(e); } |
661 |
> |
* Finds missing PoolCleaners |
662 |
|
*/ |
663 |
|
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
664 |
< |
Thread[] survivors = new Thread[5]; |
664 |
> |
Thread[] survivors = new Thread[7]; |
665 |
|
int count = Thread.enumerate(survivors); |
666 |
|
for (int i = 0; i < count; i++) { |
667 |
|
Thread thread = survivors[i]; |
669 |
|
if (name.startsWith("ForkJoinPool-")) { |
670 |
|
// give thread some time to terminate |
671 |
|
thread.join(LONG_DELAY_MS); |
672 |
< |
if (!thread.isAlive()) continue; |
673 |
< |
thread.stop(); |
674 |
< |
throw new AssertionFailedError |
421 |
< |
(String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n", |
422 |
< |
toString(), name)); |
672 |
> |
if (thread.isAlive()) |
673 |
> |
tearDownFail("Found leaked ForkJoinPool thread thread=%s", |
674 |
> |
thread); |
675 |
|
} |
676 |
|
} |
677 |
+ |
|
678 |
+ |
if (!ForkJoinPool.commonPool() |
679 |
+ |
.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS)) |
680 |
+ |
tearDownFail("ForkJoin common pool thread stuck"); |
681 |
|
} |
682 |
|
|
683 |
|
/** |
690 |
|
fail(reason); |
691 |
|
} catch (AssertionFailedError t) { |
692 |
|
threadRecordFailure(t); |
693 |
< |
fail(reason); |
693 |
> |
throw t; |
694 |
|
} |
695 |
|
} |
696 |
|
|
758 |
|
public void threadAssertEquals(Object x, Object y) { |
759 |
|
try { |
760 |
|
assertEquals(x, y); |
761 |
< |
} catch (AssertionFailedError t) { |
762 |
< |
threadRecordFailure(t); |
763 |
< |
throw t; |
764 |
< |
} catch (Throwable t) { |
765 |
< |
threadUnexpectedException(t); |
761 |
> |
} catch (AssertionFailedError fail) { |
762 |
> |
threadRecordFailure(fail); |
763 |
> |
throw fail; |
764 |
> |
} catch (Throwable fail) { |
765 |
> |
threadUnexpectedException(fail); |
766 |
|
} |
767 |
|
} |
768 |
|
|
774 |
|
public void threadAssertSame(Object x, Object y) { |
775 |
|
try { |
776 |
|
assertSame(x, y); |
777 |
< |
} catch (AssertionFailedError t) { |
778 |
< |
threadRecordFailure(t); |
779 |
< |
throw t; |
777 |
> |
} catch (AssertionFailedError fail) { |
778 |
> |
threadRecordFailure(fail); |
779 |
> |
throw fail; |
780 |
|
} |
781 |
|
} |
782 |
|
|
817 |
|
/** |
818 |
|
* Delays, via Thread.sleep, for the given millisecond delay, but |
819 |
|
* if the sleep is shorter than specified, may re-sleep or yield |
820 |
< |
* until time elapses. |
820 |
> |
* until time elapses. Ensures that the given time, as measured |
821 |
> |
* by System.nanoTime(), has elapsed. |
822 |
|
*/ |
823 |
|
static void delay(long millis) throws InterruptedException { |
824 |
< |
long startTime = System.nanoTime(); |
825 |
< |
long ns = millis * 1000 * 1000; |
826 |
< |
for (;;) { |
824 |
> |
long nanos = millis * (1000 * 1000); |
825 |
> |
final long wakeupTime = System.nanoTime() + nanos; |
826 |
> |
do { |
827 |
|
if (millis > 0L) |
828 |
|
Thread.sleep(millis); |
829 |
|
else // too short to sleep |
830 |
|
Thread.yield(); |
831 |
< |
long d = ns - (System.nanoTime() - startTime); |
832 |
< |
if (d > 0L) |
833 |
< |
millis = d / (1000 * 1000); |
834 |
< |
else |
835 |
< |
break; |
831 |
> |
nanos = wakeupTime - System.nanoTime(); |
832 |
> |
millis = nanos / (1000 * 1000); |
833 |
> |
} while (nanos >= 0L); |
834 |
> |
} |
835 |
> |
|
836 |
> |
/** |
837 |
> |
* Allows use of try-with-resources with per-test thread pools. |
838 |
> |
*/ |
839 |
> |
class PoolCleaner implements AutoCloseable { |
840 |
> |
private final ExecutorService pool; |
841 |
> |
public PoolCleaner(ExecutorService pool) { this.pool = pool; } |
842 |
> |
public void close() { joinPool(pool); } |
843 |
> |
} |
844 |
> |
|
845 |
> |
/** |
846 |
> |
* An extension of PoolCleaner that has an action to release the pool. |
847 |
> |
*/ |
848 |
> |
class PoolCleanerWithReleaser extends PoolCleaner { |
849 |
> |
private final Runnable releaser; |
850 |
> |
public PoolCleanerWithReleaser(ExecutorService pool, Runnable releaser) { |
851 |
> |
super(pool); |
852 |
> |
this.releaser = releaser; |
853 |
|
} |
854 |
+ |
public void close() { |
855 |
+ |
try { |
856 |
+ |
releaser.run(); |
857 |
+ |
} finally { |
858 |
+ |
super.close(); |
859 |
+ |
} |
860 |
+ |
} |
861 |
+ |
} |
862 |
+ |
|
863 |
+ |
PoolCleaner cleaner(ExecutorService pool) { |
864 |
+ |
return new PoolCleaner(pool); |
865 |
+ |
} |
866 |
+ |
|
867 |
+ |
PoolCleaner cleaner(ExecutorService pool, Runnable releaser) { |
868 |
+ |
return new PoolCleanerWithReleaser(pool, releaser); |
869 |
+ |
} |
870 |
+ |
|
871 |
+ |
PoolCleaner cleaner(ExecutorService pool, CountDownLatch latch) { |
872 |
+ |
return new PoolCleanerWithReleaser(pool, releaser(latch)); |
873 |
+ |
} |
874 |
+ |
|
875 |
+ |
Runnable releaser(final CountDownLatch latch) { |
876 |
+ |
return new Runnable() { public void run() { |
877 |
+ |
do { latch.countDown(); } |
878 |
+ |
while (latch.getCount() > 0); |
879 |
+ |
}}; |
880 |
|
} |
881 |
|
|
882 |
|
/** |
883 |
|
* Waits out termination of a thread pool or fails doing so. |
884 |
|
*/ |
885 |
< |
void joinPool(ExecutorService exec) { |
885 |
> |
void joinPool(ExecutorService pool) { |
886 |
|
try { |
887 |
< |
exec.shutdown(); |
888 |
< |
assertTrue("ExecutorService did not terminate in a timely manner", |
889 |
< |
exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)); |
887 |
> |
pool.shutdown(); |
888 |
> |
if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) { |
889 |
> |
try { |
890 |
> |
threadFail("ExecutorService " + pool + |
891 |
> |
" did not terminate in a timely manner"); |
892 |
> |
} finally { |
893 |
> |
// last resort, for the benefit of subsequent tests |
894 |
> |
pool.shutdownNow(); |
895 |
> |
pool.awaitTermination(MEDIUM_DELAY_MS, MILLISECONDS); |
896 |
> |
} |
897 |
> |
} |
898 |
|
} catch (SecurityException ok) { |
899 |
|
// Allowed in case test doesn't have privs |
900 |
< |
} catch (InterruptedException ie) { |
901 |
< |
fail("Unexpected InterruptedException"); |
900 |
> |
} catch (InterruptedException fail) { |
901 |
> |
threadFail("Unexpected InterruptedException"); |
902 |
|
} |
903 |
|
} |
904 |
|
|
905 |
+ |
/** Like Runnable, but with the freedom to throw anything */ |
906 |
+ |
interface Action { public void run() throws Throwable; } |
907 |
+ |
|
908 |
|
/** |
909 |
< |
* A debugging tool to print all stack traces, as jstack does. |
909 |
> |
* Runs all the given actions in parallel, failing if any fail. |
910 |
> |
* Useful for running multiple variants of tests that are |
911 |
> |
* necessarily individually slow because they must block. |
912 |
|
*/ |
913 |
< |
static void printAllStackTraces() { |
914 |
< |
for (ThreadInfo info : |
915 |
< |
ManagementFactory.getThreadMXBean() |
916 |
< |
.dumpAllThreads(true, true)) |
913 |
> |
void testInParallel(Action ... actions) { |
914 |
> |
ExecutorService pool = Executors.newCachedThreadPool(); |
915 |
> |
try (PoolCleaner cleaner = cleaner(pool)) { |
916 |
> |
ArrayList<Future<?>> futures = new ArrayList<>(actions.length); |
917 |
> |
for (final Action action : actions) |
918 |
> |
futures.add(pool.submit(new CheckedRunnable() { |
919 |
> |
public void realRun() throws Throwable { action.run();}})); |
920 |
> |
for (Future<?> future : futures) |
921 |
> |
try { |
922 |
> |
assertNull(future.get(LONG_DELAY_MS, MILLISECONDS)); |
923 |
> |
} catch (ExecutionException ex) { |
924 |
> |
threadUnexpectedException(ex.getCause()); |
925 |
> |
} catch (Exception ex) { |
926 |
> |
threadUnexpectedException(ex); |
927 |
> |
} |
928 |
> |
} |
929 |
> |
} |
930 |
> |
|
931 |
> |
/** |
932 |
> |
* A debugging tool to print stack traces of most threads, as jstack does. |
933 |
> |
* Uninteresting threads are filtered out. |
934 |
> |
*/ |
935 |
> |
static void dumpTestThreads() { |
936 |
> |
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); |
937 |
> |
System.err.println("------ stacktrace dump start ------"); |
938 |
> |
for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) { |
939 |
> |
String name = info.getThreadName(); |
940 |
> |
if ("Signal Dispatcher".equals(name)) |
941 |
> |
continue; |
942 |
> |
if ("Reference Handler".equals(name) |
943 |
> |
&& info.getLockName().startsWith("java.lang.ref.Reference$Lock")) |
944 |
> |
continue; |
945 |
> |
if ("Finalizer".equals(name) |
946 |
> |
&& info.getLockName().startsWith("java.lang.ref.ReferenceQueue$Lock")) |
947 |
> |
continue; |
948 |
> |
if ("checkForWedgedTest".equals(name)) |
949 |
> |
continue; |
950 |
|
System.err.print(info); |
951 |
+ |
} |
952 |
+ |
System.err.println("------ stacktrace dump end ------"); |
953 |
|
} |
954 |
|
|
955 |
|
/** |
968 |
|
// No need to optimize the failing case via Thread.join. |
969 |
|
delay(millis); |
970 |
|
assertTrue(thread.isAlive()); |
971 |
< |
} catch (InterruptedException ie) { |
972 |
< |
fail("Unexpected InterruptedException"); |
971 |
> |
} catch (InterruptedException fail) { |
972 |
> |
threadFail("Unexpected InterruptedException"); |
973 |
|
} |
974 |
|
} |
975 |
|
|
990 |
|
delay(millis); |
991 |
|
for (Thread thread : threads) |
992 |
|
assertTrue(thread.isAlive()); |
993 |
< |
} catch (InterruptedException ie) { |
994 |
< |
fail("Unexpected InterruptedException"); |
993 |
> |
} catch (InterruptedException fail) { |
994 |
> |
threadFail("Unexpected InterruptedException"); |
995 |
|
} |
996 |
|
} |
997 |
|
|
1012 |
|
future.get(timeoutMillis, MILLISECONDS); |
1013 |
|
shouldThrow(); |
1014 |
|
} catch (TimeoutException success) { |
1015 |
< |
} catch (Exception e) { |
1016 |
< |
threadUnexpectedException(e); |
1015 |
> |
} catch (Exception fail) { |
1016 |
> |
threadUnexpectedException(fail); |
1017 |
|
} finally { future.cancel(true); } |
1018 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
1019 |
|
} |
1057 |
|
public static final Integer m6 = new Integer(-6); |
1058 |
|
public static final Integer m10 = new Integer(-10); |
1059 |
|
|
712 |
– |
|
1060 |
|
/** |
1061 |
|
* Runs Runnable r with a security policy that permits precisely |
1062 |
|
* the specified permissions. If there is no current security |
1169 |
|
void sleep(long millis) { |
1170 |
|
try { |
1171 |
|
delay(millis); |
1172 |
< |
} catch (InterruptedException ie) { |
1172 |
> |
} catch (InterruptedException fail) { |
1173 |
|
AssertionFailedError afe = |
1174 |
|
new AssertionFailedError("Unexpected InterruptedException"); |
1175 |
< |
afe.initCause(ie); |
1175 |
> |
afe.initCause(fail); |
1176 |
|
throw afe; |
1177 |
|
} |
1178 |
|
} |
1210 |
|
/** |
1211 |
|
* Returns the number of milliseconds since time given by |
1212 |
|
* startNanoTime, which must have been previously returned from a |
1213 |
< |
* call to {@link System.nanoTime()}. |
1213 |
> |
* call to {@link System#nanoTime()}. |
1214 |
|
*/ |
1215 |
< |
long millisElapsedSince(long startNanoTime) { |
1215 |
> |
static long millisElapsedSince(long startNanoTime) { |
1216 |
|
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
1217 |
|
} |
1218 |
|
|
1219 |
+ |
// void assertTerminatesPromptly(long timeoutMillis, Runnable r) { |
1220 |
+ |
// long startTime = System.nanoTime(); |
1221 |
+ |
// try { |
1222 |
+ |
// r.run(); |
1223 |
+ |
// } catch (Throwable fail) { threadUnexpectedException(fail); } |
1224 |
+ |
// if (millisElapsedSince(startTime) > timeoutMillis/2) |
1225 |
+ |
// throw new AssertionFailedError("did not return promptly"); |
1226 |
+ |
// } |
1227 |
+ |
|
1228 |
+ |
// void assertTerminatesPromptly(Runnable r) { |
1229 |
+ |
// assertTerminatesPromptly(LONG_DELAY_MS/2, r); |
1230 |
+ |
// } |
1231 |
+ |
|
1232 |
+ |
/** |
1233 |
+ |
* Checks that timed f.get() returns the expected value, and does not |
1234 |
+ |
* wait for the timeout to elapse before returning. |
1235 |
+ |
*/ |
1236 |
+ |
<T> void checkTimedGet(Future<T> f, T expectedValue, long timeoutMillis) { |
1237 |
+ |
long startTime = System.nanoTime(); |
1238 |
+ |
try { |
1239 |
+ |
assertEquals(expectedValue, f.get(timeoutMillis, MILLISECONDS)); |
1240 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
1241 |
+ |
if (millisElapsedSince(startTime) > timeoutMillis/2) |
1242 |
+ |
throw new AssertionFailedError("timed get did not return promptly"); |
1243 |
+ |
} |
1244 |
+ |
|
1245 |
+ |
<T> void checkTimedGet(Future<T> f, T expectedValue) { |
1246 |
+ |
checkTimedGet(f, expectedValue, LONG_DELAY_MS); |
1247 |
+ |
} |
1248 |
+ |
|
1249 |
|
/** |
1250 |
|
* Returns a new started daemon Thread running the given runnable. |
1251 |
|
*/ |
1264 |
|
void awaitTermination(Thread t, long timeoutMillis) { |
1265 |
|
try { |
1266 |
|
t.join(timeoutMillis); |
1267 |
< |
} catch (InterruptedException ie) { |
1268 |
< |
threadUnexpectedException(ie); |
1267 |
> |
} catch (InterruptedException fail) { |
1268 |
> |
threadUnexpectedException(fail); |
1269 |
|
} finally { |
1270 |
|
if (t.getState() != Thread.State.TERMINATED) { |
1271 |
|
t.interrupt(); |
1272 |
< |
fail("Test timed out"); |
1272 |
> |
threadFail("timed out waiting for thread to terminate"); |
1273 |
|
} |
1274 |
|
} |
1275 |
|
} |
1291 |
|
public final void run() { |
1292 |
|
try { |
1293 |
|
realRun(); |
1294 |
< |
} catch (Throwable t) { |
1295 |
< |
threadUnexpectedException(t); |
1294 |
> |
} catch (Throwable fail) { |
1295 |
> |
threadUnexpectedException(fail); |
1296 |
|
} |
1297 |
|
} |
1298 |
|
} |
1346 |
|
threadShouldThrow("InterruptedException"); |
1347 |
|
} catch (InterruptedException success) { |
1348 |
|
threadAssertFalse(Thread.interrupted()); |
1349 |
< |
} catch (Throwable t) { |
1350 |
< |
threadUnexpectedException(t); |
1349 |
> |
} catch (Throwable fail) { |
1350 |
> |
threadUnexpectedException(fail); |
1351 |
|
} |
1352 |
|
} |
1353 |
|
} |
1358 |
|
public final T call() { |
1359 |
|
try { |
1360 |
|
return realCall(); |
1361 |
< |
} catch (Throwable t) { |
1362 |
< |
threadUnexpectedException(t); |
1361 |
> |
} catch (Throwable fail) { |
1362 |
> |
threadUnexpectedException(fail); |
1363 |
|
return null; |
1364 |
|
} |
1365 |
|
} |
1376 |
|
return result; |
1377 |
|
} catch (InterruptedException success) { |
1378 |
|
threadAssertFalse(Thread.interrupted()); |
1379 |
< |
} catch (Throwable t) { |
1380 |
< |
threadUnexpectedException(t); |
1379 |
> |
} catch (Throwable fail) { |
1380 |
> |
threadUnexpectedException(fail); |
1381 |
|
} |
1382 |
|
return null; |
1383 |
|
} |
1394 |
|
public static final String TEST_STRING = "a test string"; |
1395 |
|
|
1396 |
|
public static class StringTask implements Callable<String> { |
1397 |
< |
public String call() { return TEST_STRING; } |
1397 |
> |
final String value; |
1398 |
> |
public StringTask() { this(TEST_STRING); } |
1399 |
> |
public StringTask(String value) { this.value = value; } |
1400 |
> |
public String call() { return value; } |
1401 |
|
} |
1402 |
|
|
1403 |
|
public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) { |
1410 |
|
}}; |
1411 |
|
} |
1412 |
|
|
1413 |
< |
public Runnable awaiter(final CountDownLatch latch) { |
1413 |
> |
public Runnable countDowner(final CountDownLatch latch) { |
1414 |
|
return new CheckedRunnable() { |
1415 |
|
public void realRun() throws InterruptedException { |
1416 |
< |
await(latch); |
1416 |
> |
latch.countDown(); |
1417 |
|
}}; |
1418 |
|
} |
1419 |
|
|
1420 |
+ |
class LatchAwaiter extends CheckedRunnable { |
1421 |
+ |
static final int NEW = 0; |
1422 |
+ |
static final int RUNNING = 1; |
1423 |
+ |
static final int DONE = 2; |
1424 |
+ |
final CountDownLatch latch; |
1425 |
+ |
int state = NEW; |
1426 |
+ |
LatchAwaiter(CountDownLatch latch) { this.latch = latch; } |
1427 |
+ |
public void realRun() throws InterruptedException { |
1428 |
+ |
state = 1; |
1429 |
+ |
await(latch); |
1430 |
+ |
state = 2; |
1431 |
+ |
} |
1432 |
+ |
} |
1433 |
+ |
|
1434 |
+ |
public LatchAwaiter awaiter(CountDownLatch latch) { |
1435 |
+ |
return new LatchAwaiter(latch); |
1436 |
+ |
} |
1437 |
+ |
|
1438 |
|
public void await(CountDownLatch latch) { |
1439 |
|
try { |
1440 |
< |
assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); |
1441 |
< |
} catch (Throwable t) { |
1442 |
< |
threadUnexpectedException(t); |
1440 |
> |
if (!latch.await(LONG_DELAY_MS, MILLISECONDS)) |
1441 |
> |
fail("timed out waiting for CountDownLatch for " |
1442 |
> |
+ (LONG_DELAY_MS/1000) + " sec"); |
1443 |
> |
} catch (Throwable fail) { |
1444 |
> |
threadUnexpectedException(fail); |
1445 |
|
} |
1446 |
|
} |
1447 |
|
|
1448 |
|
public void await(Semaphore semaphore) { |
1449 |
|
try { |
1450 |
< |
assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
1451 |
< |
} catch (Throwable t) { |
1452 |
< |
threadUnexpectedException(t); |
1450 |
> |
if (!semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)) |
1451 |
> |
fail("timed out waiting for Semaphore for " |
1452 |
> |
+ (LONG_DELAY_MS/1000) + " sec"); |
1453 |
> |
} catch (Throwable fail) { |
1454 |
> |
threadUnexpectedException(fail); |
1455 |
|
} |
1456 |
|
} |
1457 |
|
|
1645 |
|
@Override protected final void compute() { |
1646 |
|
try { |
1647 |
|
realCompute(); |
1648 |
< |
} catch (Throwable t) { |
1649 |
< |
threadUnexpectedException(t); |
1648 |
> |
} catch (Throwable fail) { |
1649 |
> |
threadUnexpectedException(fail); |
1650 |
|
} |
1651 |
|
} |
1652 |
|
} |
1660 |
|
@Override protected final T compute() { |
1661 |
|
try { |
1662 |
|
return realCompute(); |
1663 |
< |
} catch (Throwable t) { |
1664 |
< |
threadUnexpectedException(t); |
1663 |
> |
} catch (Throwable fail) { |
1664 |
> |
threadUnexpectedException(fail); |
1665 |
|
return null; |
1666 |
|
} |
1667 |
|
} |
1685 |
|
public int await() { |
1686 |
|
try { |
1687 |
|
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1688 |
< |
} catch (TimeoutException e) { |
1688 |
> |
} catch (TimeoutException timedOut) { |
1689 |
|
throw new AssertionFailedError("timed out"); |
1690 |
< |
} catch (Exception e) { |
1690 |
> |
} catch (Exception fail) { |
1691 |
|
AssertionFailedError afe = |
1692 |
< |
new AssertionFailedError("Unexpected exception: " + e); |
1693 |
< |
afe.initCause(e); |
1692 |
> |
new AssertionFailedError("Unexpected exception: " + fail); |
1693 |
> |
afe.initCause(fail); |
1694 |
|
throw afe; |
1695 |
|
} |
1696 |
|
} |
1718 |
|
q.remove(); |
1719 |
|
shouldThrow(); |
1720 |
|
} catch (NoSuchElementException success) {} |
1721 |
< |
} catch (InterruptedException ie) { |
1320 |
< |
threadUnexpectedException(ie); |
1321 |
< |
} |
1721 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
1722 |
|
} |
1723 |
|
|
1724 |
|
void assertSerialEquals(Object x, Object y) { |
1737 |
|
oos.flush(); |
1738 |
|
oos.close(); |
1739 |
|
return bos.toByteArray(); |
1740 |
< |
} catch (Throwable t) { |
1741 |
< |
threadUnexpectedException(t); |
1740 |
> |
} catch (Throwable fail) { |
1741 |
> |
threadUnexpectedException(fail); |
1742 |
|
return new byte[0]; |
1743 |
|
} |
1744 |
|
} |
1751 |
|
T clone = (T) ois.readObject(); |
1752 |
|
assertSame(o.getClass(), clone.getClass()); |
1753 |
|
return clone; |
1754 |
< |
} catch (Throwable t) { |
1755 |
< |
threadUnexpectedException(t); |
1754 |
> |
} catch (Throwable fail) { |
1755 |
> |
threadUnexpectedException(fail); |
1756 |
|
return null; |
1757 |
|
} |
1758 |
|
} |
1777 |
|
shouldThrow(expectedExceptionClass.getName()); |
1778 |
|
} |
1779 |
|
} |
1780 |
+ |
|
1781 |
+ |
public void assertIteratorExhausted(Iterator<?> it) { |
1782 |
+ |
try { |
1783 |
+ |
it.next(); |
1784 |
+ |
shouldThrow(); |
1785 |
+ |
} catch (NoSuchElementException success) {} |
1786 |
+ |
assertFalse(it.hasNext()); |
1787 |
+ |
} |
1788 |
|
} |