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; |
37 |
|
import java.util.concurrent.Callable; |
38 |
|
import java.util.concurrent.CountDownLatch; |
39 |
|
import java.util.concurrent.CyclicBarrier; |
40 |
+ |
import java.util.concurrent.ExecutionException; |
41 |
+ |
import java.util.concurrent.Executors; |
42 |
|
import java.util.concurrent.ExecutorService; |
43 |
|
import java.util.concurrent.Future; |
44 |
|
import java.util.concurrent.RecursiveAction; |
54 |
|
import junit.framework.AssertionFailedError; |
55 |
|
import junit.framework.Test; |
56 |
|
import junit.framework.TestCase; |
57 |
+ |
import junit.framework.TestResult; |
58 |
|
import junit.framework.TestSuite; |
59 |
|
|
60 |
|
/** |
67 |
|
* |
68 |
|
* <ol> |
69 |
|
* |
70 |
< |
* <li> All assertions in code running in generated threads must use |
70 |
> |
* <li>All assertions in code running in generated threads must use |
71 |
|
* the forms {@link #threadFail}, {@link #threadAssertTrue}, {@link |
72 |
|
* #threadAssertEquals}, or {@link #threadAssertNull}, (not |
73 |
|
* {@code fail}, {@code assertTrue}, etc.) It is OK (but not |
74 |
|
* particularly recommended) for other code to use these forms too. |
75 |
|
* Only the most typically used JUnit assertion methods are defined |
76 |
< |
* this way, but enough to live with.</li> |
76 |
> |
* this way, but enough to live with. |
77 |
|
* |
78 |
< |
* <li> If you override {@link #setUp} or {@link #tearDown}, make sure |
78 |
> |
* <li>If you override {@link #setUp} or {@link #tearDown}, make sure |
79 |
|
* to invoke {@code super.setUp} and {@code super.tearDown} within |
80 |
|
* them. These methods are used to clear and check for thread |
81 |
< |
* assertion failures.</li> |
81 |
> |
* assertion failures. |
82 |
|
* |
83 |
|
* <li>All delays and timeouts must use one of the constants {@code |
84 |
|
* SHORT_DELAY_MS}, {@code SMALL_DELAY_MS}, {@code MEDIUM_DELAY_MS}, |
89 |
|
* is always discriminable as larger than SHORT and smaller than |
90 |
|
* MEDIUM. And so on. These constants are set to conservative values, |
91 |
|
* but even so, if there is ever any doubt, they can all be increased |
92 |
< |
* in one spot to rerun tests on slower platforms.</li> |
92 |
> |
* in one spot to rerun tests on slower platforms. |
93 |
|
* |
94 |
< |
* <li> All threads generated must be joined inside each test case |
94 |
> |
* <li>All threads generated must be joined inside each test case |
95 |
|
* method (or {@code fail} to do so) before returning from the |
96 |
|
* method. The {@code joinPool} method can be used to do this when |
97 |
< |
* using Executors.</li> |
97 |
> |
* using Executors. |
98 |
|
* |
99 |
|
* </ol> |
100 |
|
* |
101 |
|
* <p><b>Other notes</b> |
102 |
|
* <ul> |
103 |
|
* |
104 |
< |
* <li> Usually, there is one testcase method per JSR166 method |
104 |
> |
* <li>Usually, there is one testcase method per JSR166 method |
105 |
|
* covering "normal" operation, and then as many exception-testing |
106 |
|
* methods as there are exceptions the method can throw. Sometimes |
107 |
|
* there are multiple tests per JSR166 method when the different |
108 |
|
* "normal" behaviors differ significantly. And sometimes testcases |
109 |
|
* cover multiple methods when they cannot be tested in |
110 |
< |
* isolation.</li> |
110 |
> |
* isolation. |
111 |
|
* |
112 |
< |
* <li> The documentation style for testcases is to provide as javadoc |
112 |
> |
* <li>The documentation style for testcases is to provide as javadoc |
113 |
|
* a simple sentence or two describing the property that the testcase |
114 |
|
* method purports to test. The javadocs do not say anything about how |
115 |
< |
* the property is tested. To find out, read the code.</li> |
115 |
> |
* the property is tested. To find out, read the code. |
116 |
|
* |
117 |
< |
* <li> These tests are "conformance tests", and do not attempt to |
117 |
> |
* <li>These tests are "conformance tests", and do not attempt to |
118 |
|
* test throughput, latency, scalability or other performance factors |
119 |
|
* (see the separate "jtreg" tests for a set intended to check these |
120 |
|
* for the most central aspects of functionality.) So, most tests use |
121 |
|
* the smallest sensible numbers of threads, collection sizes, etc |
122 |
< |
* needed to check basic conformance.</li> |
122 |
> |
* needed to check basic conformance. |
123 |
|
* |
124 |
|
* <li>The test classes currently do not declare inclusion in |
125 |
|
* any particular package to simplify things for people integrating |
126 |
< |
* them in TCK test suites.</li> |
126 |
> |
* them in TCK test suites. |
127 |
|
* |
128 |
< |
* <li> As a convenience, the {@code main} of this class (JSR166TestCase) |
129 |
< |
* runs all JSR166 unit tests.</li> |
128 |
> |
* <li>As a convenience, the {@code main} of this class (JSR166TestCase) |
129 |
> |
* runs all JSR166 unit tests. |
130 |
|
* |
131 |
|
* </ul> |
132 |
|
*/ |
165 |
|
Integer.getInteger("jsr166.runsPerTest", 1); |
166 |
|
|
167 |
|
/** |
168 |
+ |
* The number of repetitions of the test suite (for finding leaks?). |
169 |
+ |
*/ |
170 |
+ |
private static final int suiteRuns = |
171 |
+ |
Integer.getInteger("jsr166.suiteRuns", 1); |
172 |
+ |
|
173 |
+ |
public JSR166TestCase() { super(); } |
174 |
+ |
public JSR166TestCase(String name) { super(name); } |
175 |
+ |
|
176 |
+ |
/** |
177 |
|
* A filter for tests to run, matching strings of the form |
178 |
|
* methodName(className), e.g. "testInvokeAll5(ForkJoinPoolTest)" |
179 |
|
* Usefully combined with jsr166.runsPerTest. |
212 |
|
|
213 |
|
/** |
214 |
|
* Runs all JSR166 unit tests using junit.textui.TestRunner. |
201 |
– |
* Optional command line arg provides the number of iterations to |
202 |
– |
* repeat running the tests. |
215 |
|
*/ |
216 |
|
public static void main(String[] args) { |
217 |
+ |
main(suite(), args); |
218 |
+ |
} |
219 |
+ |
|
220 |
+ |
/** |
221 |
+ |
* Runs all unit tests in the given test suite. |
222 |
+ |
* Actual behavior influenced by jsr166.* system properties. |
223 |
+ |
*/ |
224 |
+ |
static void main(Test suite, String[] args) { |
225 |
|
if (useSecurityManager) { |
226 |
|
System.err.println("Setting a permissive security manager"); |
227 |
|
Policy.setPolicy(permissivePolicy()); |
228 |
|
System.setSecurityManager(new SecurityManager()); |
229 |
|
} |
230 |
< |
int iters = (args.length == 0) ? 1 : Integer.parseInt(args[0]); |
231 |
< |
|
232 |
< |
Test s = suite(); |
233 |
< |
for (int i = 0; i < iters; ++i) { |
214 |
< |
junit.textui.TestRunner.run(s); |
230 |
> |
for (int i = 0; i < suiteRuns; i++) { |
231 |
> |
TestResult result = junit.textui.TestRunner.run(suite); |
232 |
> |
if (!result.wasSuccessful()) |
233 |
> |
System.exit(1); |
234 |
|
System.gc(); |
235 |
|
System.runFinalization(); |
236 |
|
} |
218 |
– |
System.exit(0); |
237 |
|
} |
238 |
|
|
239 |
|
public static TestSuite newTestSuite(Object... suiteOrClasses) { |
284 |
|
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
285 |
|
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
286 |
|
public static boolean atLeastJava9() { |
287 |
< |
// As of 2014-05, java9 still uses 52.0 class file version |
288 |
< |
return JAVA_SPECIFICATION_VERSION.startsWith("1.9"); |
287 |
> |
return JAVA_CLASS_VERSION >= 53.0 |
288 |
> |
// As of 2015-09, java9 still uses 52.0 class file version |
289 |
> |
|| JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?(9|[0-9][0-9])$"); |
290 |
> |
} |
291 |
> |
public static boolean atLeastJava10() { |
292 |
> |
return JAVA_CLASS_VERSION >= 54.0 |
293 |
> |
|| JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?[0-9][0-9]$"); |
294 |
|
} |
295 |
|
|
296 |
|
/** |
379 |
|
"LongAdderTest", |
380 |
|
"SplittableRandomTest", |
381 |
|
"StampedLockTest", |
382 |
+ |
"SubmissionPublisherTest", |
383 |
|
"ThreadLocalRandom8Test", |
384 |
|
}; |
385 |
|
addNamedTestClasses(suite, java8TestClassNames); |
388 |
|
// Java9+ test classes |
389 |
|
if (atLeastJava9()) { |
390 |
|
String[] java9TestClassNames = { |
391 |
< |
"ThreadPoolExecutor9Test", |
391 |
> |
// Currently empty, but expecting varhandle tests |
392 |
|
}; |
393 |
|
addNamedTestClasses(suite, java9TestClassNames); |
394 |
|
} |
396 |
|
return suite; |
397 |
|
} |
398 |
|
|
399 |
+ |
/** Returns list of junit-style test method names in given class. */ |
400 |
+ |
public static ArrayList<String> testMethodNames(Class<?> testClass) { |
401 |
+ |
Method[] methods = testClass.getDeclaredMethods(); |
402 |
+ |
ArrayList<String> names = new ArrayList<String>(methods.length); |
403 |
+ |
for (Method method : methods) { |
404 |
+ |
if (method.getName().startsWith("test") |
405 |
+ |
&& Modifier.isPublic(method.getModifiers()) |
406 |
+ |
// method.getParameterCount() requires jdk8+ |
407 |
+ |
&& method.getParameterTypes().length == 0) { |
408 |
+ |
names.add(method.getName()); |
409 |
+ |
} |
410 |
+ |
} |
411 |
+ |
return names; |
412 |
+ |
} |
413 |
+ |
|
414 |
+ |
/** |
415 |
+ |
* Returns junit-style testSuite for the given test class, but |
416 |
+ |
* parameterized by passing extra data to each test. |
417 |
+ |
*/ |
418 |
+ |
public static <ExtraData> Test parameterizedTestSuite |
419 |
+ |
(Class<? extends JSR166TestCase> testClass, |
420 |
+ |
Class<ExtraData> dataClass, |
421 |
+ |
ExtraData data) { |
422 |
+ |
try { |
423 |
+ |
TestSuite suite = new TestSuite(); |
424 |
+ |
Constructor c = |
425 |
+ |
testClass.getDeclaredConstructor(dataClass, String.class); |
426 |
+ |
for (String methodName : testMethodNames(testClass)) |
427 |
+ |
suite.addTest((Test) c.newInstance(data, methodName)); |
428 |
+ |
return suite; |
429 |
+ |
} catch (Exception e) { |
430 |
+ |
throw new Error(e); |
431 |
+ |
} |
432 |
+ |
} |
433 |
+ |
|
434 |
+ |
/** |
435 |
+ |
* Returns junit-style testSuite for the jdk8 extension of the |
436 |
+ |
* given test class, but parameterized by passing extra data to |
437 |
+ |
* each test. Uses reflection to allow compilation in jdk7. |
438 |
+ |
*/ |
439 |
+ |
public static <ExtraData> Test jdk8ParameterizedTestSuite |
440 |
+ |
(Class<? extends JSR166TestCase> testClass, |
441 |
+ |
Class<ExtraData> dataClass, |
442 |
+ |
ExtraData data) { |
443 |
+ |
if (atLeastJava8()) { |
444 |
+ |
String name = testClass.getName(); |
445 |
+ |
String name8 = name.replaceAll("Test$", "8Test"); |
446 |
+ |
if (name.equals(name8)) throw new Error(name); |
447 |
+ |
try { |
448 |
+ |
return (Test) |
449 |
+ |
Class.forName(name8) |
450 |
+ |
.getMethod("testSuite", new Class[] { dataClass }) |
451 |
+ |
.invoke(null, data); |
452 |
+ |
} catch (Exception e) { |
453 |
+ |
throw new Error(e); |
454 |
+ |
} |
455 |
+ |
} else { |
456 |
+ |
return new TestSuite(); |
457 |
+ |
} |
458 |
+ |
|
459 |
+ |
} |
460 |
+ |
|
461 |
|
// Delays for timing-dependent tests, in milliseconds. |
462 |
|
|
463 |
|
public static long SHORT_DELAY_MS; |
492 |
|
} |
493 |
|
|
494 |
|
/** |
495 |
< |
* Returns a new Date instance representing a time delayMillis |
496 |
< |
* milliseconds in the future. |
495 |
> |
* Returns a new Date instance representing a time at least |
496 |
> |
* delayMillis milliseconds in the future. |
497 |
|
*/ |
498 |
|
Date delayedDate(long delayMillis) { |
499 |
< |
return new Date(System.currentTimeMillis() + delayMillis); |
499 |
> |
// Add 1 because currentTimeMillis is known to round into the past. |
500 |
> |
return new Date(System.currentTimeMillis() + delayMillis + 1); |
501 |
|
} |
502 |
|
|
503 |
|
/** |
565 |
|
// give thread some time to terminate |
566 |
|
thread.join(LONG_DELAY_MS); |
567 |
|
if (!thread.isAlive()) continue; |
481 |
– |
thread.stop(); |
568 |
|
throw new AssertionFailedError |
569 |
|
(String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n", |
570 |
|
toString(), name)); |
650 |
|
public void threadAssertEquals(Object x, Object y) { |
651 |
|
try { |
652 |
|
assertEquals(x, y); |
653 |
< |
} catch (AssertionFailedError t) { |
654 |
< |
threadRecordFailure(t); |
655 |
< |
throw t; |
656 |
< |
} catch (Throwable t) { |
657 |
< |
threadUnexpectedException(t); |
653 |
> |
} catch (AssertionFailedError fail) { |
654 |
> |
threadRecordFailure(fail); |
655 |
> |
throw fail; |
656 |
> |
} catch (Throwable fail) { |
657 |
> |
threadUnexpectedException(fail); |
658 |
|
} |
659 |
|
} |
660 |
|
|
666 |
|
public void threadAssertSame(Object x, Object y) { |
667 |
|
try { |
668 |
|
assertSame(x, y); |
669 |
< |
} catch (AssertionFailedError t) { |
670 |
< |
threadRecordFailure(t); |
671 |
< |
throw t; |
669 |
> |
} catch (AssertionFailedError fail) { |
670 |
> |
threadRecordFailure(fail); |
671 |
> |
throw fail; |
672 |
|
} |
673 |
|
} |
674 |
|
|
730 |
|
/** |
731 |
|
* Waits out termination of a thread pool or fails doing so. |
732 |
|
*/ |
733 |
< |
void joinPool(ExecutorService exec) { |
733 |
> |
void joinPool(ExecutorService pool) { |
734 |
|
try { |
735 |
< |
exec.shutdown(); |
736 |
< |
if (!exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) |
737 |
< |
fail("ExecutorService " + exec + |
735 |
> |
pool.shutdown(); |
736 |
> |
if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) |
737 |
> |
fail("ExecutorService " + pool + |
738 |
|
" did not terminate in a timely manner"); |
739 |
|
} catch (SecurityException ok) { |
740 |
|
// Allowed in case test doesn't have privs |
743 |
|
} |
744 |
|
} |
745 |
|
|
746 |
+ |
/** Like Runnable, but with the freedom to throw anything */ |
747 |
+ |
interface Action { public void run() throws Throwable; } |
748 |
+ |
|
749 |
+ |
/** |
750 |
+ |
* Runs all the given actions in parallel, failing if any fail. |
751 |
+ |
* Useful for running multiple variants of tests that are |
752 |
+ |
* necessarily individually slow because they must block. |
753 |
+ |
*/ |
754 |
+ |
void testInParallel(Action ... actions) { |
755 |
+ |
ExecutorService pool = Executors.newCachedThreadPool(); |
756 |
+ |
try { |
757 |
+ |
ArrayList<Future<?>> futures = new ArrayList<>(actions.length); |
758 |
+ |
for (final Action action : actions) |
759 |
+ |
futures.add(pool.submit(new CheckedRunnable() { |
760 |
+ |
public void realRun() throws Throwable { action.run();}})); |
761 |
+ |
for (Future<?> future : futures) |
762 |
+ |
try { |
763 |
+ |
assertNull(future.get(LONG_DELAY_MS, MILLISECONDS)); |
764 |
+ |
} catch (ExecutionException ex) { |
765 |
+ |
threadUnexpectedException(ex.getCause()); |
766 |
+ |
} catch (Exception ex) { |
767 |
+ |
threadUnexpectedException(ex); |
768 |
+ |
} |
769 |
+ |
} finally { |
770 |
+ |
joinPool(pool); |
771 |
+ |
} |
772 |
+ |
} |
773 |
+ |
|
774 |
|
/** |
775 |
|
* A debugging tool to print all stack traces, as jstack does. |
776 |
|
*/ |