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.Method; |
19 |
+ |
import java.security.CodeSource; |
20 |
+ |
import java.security.Permission; |
21 |
+ |
import java.security.PermissionCollection; |
22 |
+ |
import java.security.Permissions; |
23 |
+ |
import java.security.Policy; |
24 |
+ |
import java.security.ProtectionDomain; |
25 |
+ |
import java.security.SecurityPermission; |
26 |
|
import java.util.ArrayList; |
27 |
|
import java.util.Arrays; |
28 |
|
import java.util.Date; |
30 |
|
import java.util.List; |
31 |
|
import java.util.NoSuchElementException; |
32 |
|
import java.util.PropertyPermission; |
33 |
< |
import java.util.concurrent.*; |
34 |
< |
import java.util.concurrent.atomic.AtomicBoolean; |
33 |
> |
import java.util.concurrent.BlockingQueue; |
34 |
> |
import java.util.concurrent.Callable; |
35 |
> |
import java.util.concurrent.CountDownLatch; |
36 |
> |
import java.util.concurrent.CyclicBarrier; |
37 |
> |
import java.util.concurrent.ExecutorService; |
38 |
> |
import java.util.concurrent.Future; |
39 |
> |
import java.util.concurrent.RecursiveAction; |
40 |
> |
import java.util.concurrent.RecursiveTask; |
41 |
> |
import java.util.concurrent.RejectedExecutionHandler; |
42 |
> |
import java.util.concurrent.Semaphore; |
43 |
> |
import java.util.concurrent.ThreadFactory; |
44 |
> |
import java.util.concurrent.ThreadPoolExecutor; |
45 |
> |
import java.util.concurrent.TimeoutException; |
46 |
|
import java.util.concurrent.atomic.AtomicReference; |
47 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
48 |
< |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
49 |
< |
import java.security.CodeSource; |
50 |
< |
import java.security.Permission; |
51 |
< |
import java.security.PermissionCollection; |
52 |
< |
import java.security.Permissions; |
32 |
< |
import java.security.Policy; |
33 |
< |
import java.security.ProtectionDomain; |
34 |
< |
import java.security.SecurityPermission; |
47 |
> |
import java.util.regex.Pattern; |
48 |
> |
|
49 |
> |
import junit.framework.AssertionFailedError; |
50 |
> |
import junit.framework.Test; |
51 |
> |
import junit.framework.TestCase; |
52 |
> |
import junit.framework.TestSuite; |
53 |
|
|
54 |
|
/** |
55 |
|
* Base class for JSR166 Junit TCK tests. Defines some constants, |
132 |
|
Boolean.getBoolean("jsr166.expensiveTests"); |
133 |
|
|
134 |
|
/** |
135 |
+ |
* If true, also run tests that are not part of the official tck |
136 |
+ |
* because they test unspecified implementation details. |
137 |
+ |
*/ |
138 |
+ |
protected static final boolean testImplementationDetails = |
139 |
+ |
Boolean.getBoolean("jsr166.testImplementationDetails"); |
140 |
+ |
|
141 |
+ |
/** |
142 |
|
* If true, report on stdout all "slow" tests, that is, ones that |
143 |
|
* take more than profileThreshold milliseconds to execute. |
144 |
|
*/ |
152 |
|
private static final long profileThreshold = |
153 |
|
Long.getLong("jsr166.profileThreshold", 100); |
154 |
|
|
155 |
+ |
/** |
156 |
+ |
* The number of repetitions per test (for tickling rare bugs). |
157 |
+ |
*/ |
158 |
+ |
private static final int runsPerTest = |
159 |
+ |
Integer.getInteger("jsr166.runsPerTest", 1); |
160 |
+ |
|
161 |
+ |
/** |
162 |
+ |
* A filter for tests to run, matching strings of the form |
163 |
+ |
* methodName(className), e.g. "testInvokeAll5(ForkJoinPoolTest)" |
164 |
+ |
* Usefully combined with jsr166.runsPerTest. |
165 |
+ |
*/ |
166 |
+ |
private static final Pattern methodFilter = methodFilter(); |
167 |
+ |
|
168 |
+ |
private static Pattern methodFilter() { |
169 |
+ |
String regex = System.getProperty("jsr166.methodFilter"); |
170 |
+ |
return (regex == null) ? null : Pattern.compile(regex); |
171 |
+ |
} |
172 |
+ |
|
173 |
|
protected void runTest() throws Throwable { |
174 |
< |
if (profileTests) |
175 |
< |
runTestProfiled(); |
176 |
< |
else |
177 |
< |
super.runTest(); |
174 |
> |
if (methodFilter == null |
175 |
> |
|| methodFilter.matcher(toString()).find()) { |
176 |
> |
for (int i = 0; i < runsPerTest; i++) { |
177 |
> |
if (profileTests) |
178 |
> |
runTestProfiled(); |
179 |
> |
else |
180 |
> |
super.runTest(); |
181 |
> |
} |
182 |
> |
} |
183 |
|
} |
184 |
|
|
185 |
|
protected void runTestProfiled() throws Throwable { |
186 |
+ |
// Warmup run, notably to trigger all needed classloading. |
187 |
+ |
super.runTest(); |
188 |
|
long t0 = System.nanoTime(); |
189 |
|
try { |
190 |
|
super.runTest(); |
191 |
|
} finally { |
192 |
< |
long elapsedMillis = |
143 |
< |
(System.nanoTime() - t0) / (1000L * 1000L); |
192 |
> |
long elapsedMillis = millisElapsedSince(t0); |
193 |
|
if (elapsedMillis >= profileThreshold) |
194 |
|
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); |
195 |
|
} |
230 |
|
return suite; |
231 |
|
} |
232 |
|
|
233 |
+ |
public static void addNamedTestClasses(TestSuite suite, |
234 |
+ |
String... testClassNames) { |
235 |
+ |
for (String testClassName : testClassNames) { |
236 |
+ |
try { |
237 |
+ |
Class<?> testClass = Class.forName(testClassName); |
238 |
+ |
Method m = testClass.getDeclaredMethod("suite", |
239 |
+ |
new Class<?>[0]); |
240 |
+ |
suite.addTest(newTestSuite((Test)m.invoke(null))); |
241 |
+ |
} catch (Exception e) { |
242 |
+ |
throw new Error("Missing test class", e); |
243 |
+ |
} |
244 |
+ |
} |
245 |
+ |
} |
246 |
+ |
|
247 |
+ |
public static final double JAVA_CLASS_VERSION; |
248 |
+ |
public static final String JAVA_SPECIFICATION_VERSION; |
249 |
+ |
static { |
250 |
+ |
try { |
251 |
+ |
JAVA_CLASS_VERSION = java.security.AccessController.doPrivileged( |
252 |
+ |
new java.security.PrivilegedAction<Double>() { |
253 |
+ |
public Double run() { |
254 |
+ |
return Double.valueOf(System.getProperty("java.class.version"));}}); |
255 |
+ |
JAVA_SPECIFICATION_VERSION = java.security.AccessController.doPrivileged( |
256 |
+ |
new java.security.PrivilegedAction<String>() { |
257 |
+ |
public String run() { |
258 |
+ |
return System.getProperty("java.specification.version");}}); |
259 |
+ |
} catch (Throwable t) { |
260 |
+ |
throw new Error(t); |
261 |
+ |
} |
262 |
+ |
} |
263 |
+ |
|
264 |
+ |
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
265 |
+ |
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
266 |
+ |
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
267 |
+ |
public static boolean atLeastJava9() { |
268 |
+ |
// As of 2014-05, java9 still uses 52.0 class file version |
269 |
+ |
return JAVA_SPECIFICATION_VERSION.startsWith("1.9"); |
270 |
+ |
} |
271 |
+ |
|
272 |
|
/** |
273 |
|
* Collects all JSR166 unit tests as one suite. |
274 |
|
*/ |
275 |
|
public static Test suite() { |
276 |
< |
return newTestSuite( |
276 |
> |
// Java7+ test classes |
277 |
> |
TestSuite suite = newTestSuite( |
278 |
|
ForkJoinPoolTest.suite(), |
279 |
|
ForkJoinTaskTest.suite(), |
280 |
|
RecursiveActionTest.suite(), |
339 |
|
TreeSetTest.suite(), |
340 |
|
TreeSubMapTest.suite(), |
341 |
|
TreeSubSetTest.suite()); |
342 |
+ |
|
343 |
+ |
// Java8+ test classes |
344 |
+ |
if (atLeastJava8()) { |
345 |
+ |
String[] java8TestClassNames = { |
346 |
+ |
"Atomic8Test", |
347 |
+ |
"CompletableFutureTest", |
348 |
+ |
"ConcurrentHashMap8Test", |
349 |
+ |
"CountedCompleterTest", |
350 |
+ |
"DoubleAccumulatorTest", |
351 |
+ |
"DoubleAdderTest", |
352 |
+ |
"ForkJoinPool8Test", |
353 |
+ |
"ForkJoinTask8Test", |
354 |
+ |
"LongAccumulatorTest", |
355 |
+ |
"LongAdderTest", |
356 |
+ |
"SplittableRandomTest", |
357 |
+ |
"StampedLockTest", |
358 |
+ |
"ThreadLocalRandom8Test", |
359 |
+ |
}; |
360 |
+ |
addNamedTestClasses(suite, java8TestClassNames); |
361 |
+ |
} |
362 |
+ |
|
363 |
+ |
// Java9+ test classes |
364 |
+ |
if (atLeastJava9()) { |
365 |
+ |
String[] java9TestClassNames = { |
366 |
+ |
"ThreadPoolExecutor9Test", |
367 |
+ |
}; |
368 |
+ |
addNamedTestClasses(suite, java9TestClassNames); |
369 |
+ |
} |
370 |
+ |
|
371 |
+ |
return suite; |
372 |
|
} |
373 |
|
|
374 |
+ |
// Delays for timing-dependent tests, in milliseconds. |
375 |
|
|
376 |
|
public static long SHORT_DELAY_MS; |
377 |
|
public static long SMALL_DELAY_MS; |
378 |
|
public static long MEDIUM_DELAY_MS; |
379 |
|
public static long LONG_DELAY_MS; |
380 |
|
|
261 |
– |
|
381 |
|
/** |
382 |
|
* Returns the shortest timed delay. This could |
383 |
|
* be reimplemented to use for example a Property. |
460 |
|
|
461 |
|
if (Thread.interrupted()) |
462 |
|
throw new AssertionFailedError("interrupt status set in main thread"); |
463 |
+ |
|
464 |
+ |
checkForkJoinPoolThreadLeaks(); |
465 |
+ |
} |
466 |
+ |
|
467 |
+ |
/** |
468 |
+ |
* Finds missing try { ... } finally { joinPool(e); } |
469 |
+ |
*/ |
470 |
+ |
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
471 |
+ |
Thread[] survivors = new Thread[5]; |
472 |
+ |
int count = Thread.enumerate(survivors); |
473 |
+ |
for (int i = 0; i < count; i++) { |
474 |
+ |
Thread thread = survivors[i]; |
475 |
+ |
String name = thread.getName(); |
476 |
+ |
if (name.startsWith("ForkJoinPool-")) { |
477 |
+ |
// give thread some time to terminate |
478 |
+ |
thread.join(LONG_DELAY_MS); |
479 |
+ |
if (!thread.isAlive()) continue; |
480 |
+ |
thread.stop(); |
481 |
+ |
throw new AssertionFailedError |
482 |
+ |
(String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n", |
483 |
+ |
toString(), name)); |
484 |
+ |
} |
485 |
+ |
} |
486 |
|
} |
487 |
|
|
488 |
|
/** |
646 |
|
void joinPool(ExecutorService exec) { |
647 |
|
try { |
648 |
|
exec.shutdown(); |
649 |
< |
assertTrue("ExecutorService did not terminate in a timely manner", |
650 |
< |
exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)); |
649 |
> |
if (!exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) |
650 |
> |
fail("ExecutorService " + exec + |
651 |
> |
" did not terminate in a timely manner"); |
652 |
|
} catch (SecurityException ok) { |
653 |
|
// Allowed in case test doesn't have privs |
654 |
|
} catch (InterruptedException ie) { |
771 |
|
public static final Integer m6 = new Integer(-6); |
772 |
|
public static final Integer m10 = new Integer(-10); |
773 |
|
|
631 |
– |
|
774 |
|
/** |
775 |
|
* Runs Runnable r with a security policy that permits precisely |
776 |
|
* the specified permissions. If there is no current security |
924 |
|
/** |
925 |
|
* Returns the number of milliseconds since time given by |
926 |
|
* startNanoTime, which must have been previously returned from a |
927 |
< |
* call to {@link System.nanoTime()}. |
927 |
> |
* call to {@link System#nanoTime()}. |
928 |
|
*/ |
929 |
< |
long millisElapsedSince(long startNanoTime) { |
929 |
> |
static long millisElapsedSince(long startNanoTime) { |
930 |
|
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
931 |
|
} |
932 |
|
|
933 |
+ |
// void assertTerminatesPromptly(long timeoutMillis, Runnable r) { |
934 |
+ |
// long startTime = System.nanoTime(); |
935 |
+ |
// try { |
936 |
+ |
// r.run(); |
937 |
+ |
// } catch (Throwable fail) { threadUnexpectedException(fail); } |
938 |
+ |
// if (millisElapsedSince(startTime) > timeoutMillis/2) |
939 |
+ |
// throw new AssertionFailedError("did not return promptly"); |
940 |
+ |
// } |
941 |
+ |
|
942 |
+ |
// void assertTerminatesPromptly(Runnable r) { |
943 |
+ |
// assertTerminatesPromptly(LONG_DELAY_MS/2, r); |
944 |
+ |
// } |
945 |
+ |
|
946 |
+ |
/** |
947 |
+ |
* Checks that timed f.get() returns the expected value, and does not |
948 |
+ |
* wait for the timeout to elapse before returning. |
949 |
+ |
*/ |
950 |
+ |
<T> void checkTimedGet(Future<T> f, T expectedValue, long timeoutMillis) { |
951 |
+ |
long startTime = System.nanoTime(); |
952 |
+ |
try { |
953 |
+ |
assertEquals(expectedValue, f.get(timeoutMillis, MILLISECONDS)); |
954 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
955 |
+ |
if (millisElapsedSince(startTime) > timeoutMillis/2) |
956 |
+ |
throw new AssertionFailedError("timed get did not return promptly"); |
957 |
+ |
} |
958 |
+ |
|
959 |
+ |
<T> void checkTimedGet(Future<T> f, T expectedValue) { |
960 |
+ |
checkTimedGet(f, expectedValue, LONG_DELAY_MS); |
961 |
+ |
} |
962 |
+ |
|
963 |
|
/** |
964 |
|
* Returns a new started daemon Thread running the given runnable. |
965 |
|
*/ |
1331 |
|
public abstract class CheckedRecursiveAction extends RecursiveAction { |
1332 |
|
protected abstract void realCompute() throws Throwable; |
1333 |
|
|
1334 |
< |
public final void compute() { |
1334 |
> |
@Override protected final void compute() { |
1335 |
|
try { |
1336 |
|
realCompute(); |
1337 |
|
} catch (Throwable t) { |
1346 |
|
public abstract class CheckedRecursiveTask<T> extends RecursiveTask<T> { |
1347 |
|
protected abstract T realCompute() throws Throwable; |
1348 |
|
|
1349 |
< |
public final T compute() { |
1349 |
> |
@Override protected final T compute() { |
1350 |
|
try { |
1351 |
|
return realCompute(); |
1352 |
|
} catch (Throwable t) { |
1447 |
|
return null; |
1448 |
|
} |
1449 |
|
} |
1450 |
+ |
|
1451 |
+ |
public void assertThrows(Class<? extends Throwable> expectedExceptionClass, |
1452 |
+ |
Runnable... throwingActions) { |
1453 |
+ |
for (Runnable throwingAction : throwingActions) { |
1454 |
+ |
boolean threw = false; |
1455 |
+ |
try { throwingAction.run(); } |
1456 |
+ |
catch (Throwable t) { |
1457 |
+ |
threw = true; |
1458 |
+ |
if (!expectedExceptionClass.isInstance(t)) { |
1459 |
+ |
AssertionFailedError afe = |
1460 |
+ |
new AssertionFailedError |
1461 |
+ |
("Expected " + expectedExceptionClass.getName() + |
1462 |
+ |
", got " + t.getClass().getName()); |
1463 |
+ |
afe.initCause(t); |
1464 |
+ |
threadUnexpectedException(afe); |
1465 |
+ |
} |
1466 |
+ |
} |
1467 |
+ |
if (!threw) |
1468 |
+ |
shouldThrow(expectedExceptionClass.getName()); |
1469 |
+ |
} |
1470 |
+ |
} |
1471 |
|
} |