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; |
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; |
29 |
|
import java.util.Enumeration; |
30 |
+ |
import java.util.Iterator; |
31 |
|
import java.util.List; |
32 |
|
import java.util.NoSuchElementException; |
33 |
|
import java.util.PropertyPermission; |
34 |
< |
import java.util.concurrent.*; |
35 |
< |
import java.util.concurrent.atomic.AtomicBoolean; |
34 |
> |
import java.util.concurrent.BlockingQueue; |
35 |
> |
import java.util.concurrent.Callable; |
36 |
> |
import java.util.concurrent.CountDownLatch; |
37 |
> |
import java.util.concurrent.CyclicBarrier; |
38 |
> |
import java.util.concurrent.ExecutorService; |
39 |
> |
import java.util.concurrent.Future; |
40 |
> |
import java.util.concurrent.RecursiveAction; |
41 |
> |
import java.util.concurrent.RecursiveTask; |
42 |
> |
import java.util.concurrent.RejectedExecutionHandler; |
43 |
> |
import java.util.concurrent.Semaphore; |
44 |
> |
import java.util.concurrent.ThreadFactory; |
45 |
> |
import java.util.concurrent.ThreadPoolExecutor; |
46 |
> |
import java.util.concurrent.TimeoutException; |
47 |
|
import java.util.concurrent.atomic.AtomicReference; |
48 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
49 |
< |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
50 |
< |
import java.security.CodeSource; |
51 |
< |
import java.security.Permission; |
52 |
< |
import java.security.PermissionCollection; |
53 |
< |
import java.security.Permissions; |
33 |
< |
import java.security.Policy; |
34 |
< |
import java.security.ProtectionDomain; |
35 |
< |
import java.security.SecurityPermission; |
48 |
> |
import java.util.regex.Pattern; |
49 |
> |
|
50 |
> |
import junit.framework.AssertionFailedError; |
51 |
> |
import junit.framework.Test; |
52 |
> |
import junit.framework.TestCase; |
53 |
> |
import junit.framework.TestSuite; |
54 |
|
|
55 |
|
/** |
56 |
|
* Base class for JSR166 Junit TCK tests. Defines some constants, |
133 |
|
Boolean.getBoolean("jsr166.expensiveTests"); |
134 |
|
|
135 |
|
/** |
136 |
+ |
* If true, also run tests that are not part of the official tck |
137 |
+ |
* because they test unspecified implementation details. |
138 |
+ |
*/ |
139 |
+ |
protected static final boolean testImplementationDetails = |
140 |
+ |
Boolean.getBoolean("jsr166.testImplementationDetails"); |
141 |
+ |
|
142 |
+ |
/** |
143 |
|
* If true, report on stdout all "slow" tests, that is, ones that |
144 |
|
* take more than profileThreshold milliseconds to execute. |
145 |
|
*/ |
159 |
|
private static final int runsPerTest = |
160 |
|
Integer.getInteger("jsr166.runsPerTest", 1); |
161 |
|
|
162 |
+ |
/** |
163 |
+ |
* A filter for tests to run, matching strings of the form |
164 |
+ |
* methodName(className), e.g. "testInvokeAll5(ForkJoinPoolTest)" |
165 |
+ |
* Usefully combined with jsr166.runsPerTest. |
166 |
+ |
*/ |
167 |
+ |
private static final Pattern methodFilter = methodFilter(); |
168 |
+ |
|
169 |
+ |
private static Pattern methodFilter() { |
170 |
+ |
String regex = System.getProperty("jsr166.methodFilter"); |
171 |
+ |
return (regex == null) ? null : Pattern.compile(regex); |
172 |
+ |
} |
173 |
+ |
|
174 |
|
protected void runTest() throws Throwable { |
175 |
< |
for (int i = 0; i < runsPerTest; i++) { |
176 |
< |
if (profileTests) |
177 |
< |
runTestProfiled(); |
178 |
< |
else |
179 |
< |
super.runTest(); |
175 |
> |
if (methodFilter == null |
176 |
> |
|| methodFilter.matcher(toString()).find()) { |
177 |
> |
for (int i = 0; i < runsPerTest; i++) { |
178 |
> |
if (profileTests) |
179 |
> |
runTestProfiled(); |
180 |
> |
else |
181 |
> |
super.runTest(); |
182 |
> |
} |
183 |
|
} |
184 |
|
} |
185 |
|
|
186 |
|
protected void runTestProfiled() throws Throwable { |
187 |
+ |
// Warmup run, notably to trigger all needed classloading. |
188 |
+ |
super.runTest(); |
189 |
|
long t0 = System.nanoTime(); |
190 |
|
try { |
191 |
|
super.runTest(); |
192 |
|
} finally { |
193 |
< |
long elapsedMillis = |
152 |
< |
(System.nanoTime() - t0) / (1000L * 1000L); |
193 |
> |
long elapsedMillis = millisElapsedSince(t0); |
194 |
|
if (elapsedMillis >= profileThreshold) |
195 |
|
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); |
196 |
|
} |
246 |
|
} |
247 |
|
|
248 |
|
public static final double JAVA_CLASS_VERSION; |
249 |
+ |
public static final String JAVA_SPECIFICATION_VERSION; |
250 |
|
static { |
251 |
|
try { |
252 |
|
JAVA_CLASS_VERSION = java.security.AccessController.doPrivileged( |
253 |
|
new java.security.PrivilegedAction<Double>() { |
254 |
|
public Double run() { |
255 |
|
return Double.valueOf(System.getProperty("java.class.version"));}}); |
256 |
+ |
JAVA_SPECIFICATION_VERSION = java.security.AccessController.doPrivileged( |
257 |
+ |
new java.security.PrivilegedAction<String>() { |
258 |
+ |
public String run() { |
259 |
+ |
return System.getProperty("java.specification.version");}}); |
260 |
|
} catch (Throwable t) { |
261 |
|
throw new Error(t); |
262 |
|
} |
265 |
|
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
266 |
|
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
267 |
|
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
268 |
+ |
public static boolean atLeastJava9() { |
269 |
+ |
// As of 2014-05, java9 still uses 52.0 class file version |
270 |
+ |
return JAVA_SPECIFICATION_VERSION.startsWith("1.9"); |
271 |
+ |
} |
272 |
|
|
273 |
|
/** |
274 |
|
* Collects all JSR166 unit tests as one suite. |
344 |
|
// Java8+ test classes |
345 |
|
if (atLeastJava8()) { |
346 |
|
String[] java8TestClassNames = { |
347 |
+ |
"Atomic8Test", |
348 |
|
"CompletableFutureTest", |
349 |
|
"ConcurrentHashMap8Test", |
350 |
|
"CountedCompleterTest", |
361 |
|
addNamedTestClasses(suite, java8TestClassNames); |
362 |
|
} |
363 |
|
|
364 |
+ |
// Java9+ test classes |
365 |
+ |
if (atLeastJava9()) { |
366 |
+ |
String[] java9TestClassNames = { |
367 |
+ |
"ThreadPoolExecutor9Test", |
368 |
+ |
}; |
369 |
+ |
addNamedTestClasses(suite, java9TestClassNames); |
370 |
+ |
} |
371 |
+ |
|
372 |
|
return suite; |
373 |
|
} |
374 |
|
|
466 |
|
} |
467 |
|
|
468 |
|
/** |
469 |
< |
* Find missing try { ... } finally { joinPool(e); } |
469 |
> |
* Finds missing try { ... } finally { joinPool(e); } |
470 |
|
*/ |
471 |
|
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
472 |
|
Thread[] survivors = new Thread[5]; |
647 |
|
void joinPool(ExecutorService exec) { |
648 |
|
try { |
649 |
|
exec.shutdown(); |
650 |
< |
assertTrue("ExecutorService did not terminate in a timely manner", |
651 |
< |
exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)); |
650 |
> |
if (!exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) |
651 |
> |
fail("ExecutorService " + exec + |
652 |
> |
" did not terminate in a timely manner"); |
653 |
|
} catch (SecurityException ok) { |
654 |
|
// Allowed in case test doesn't have privs |
655 |
|
} catch (InterruptedException ie) { |
925 |
|
/** |
926 |
|
* Returns the number of milliseconds since time given by |
927 |
|
* startNanoTime, which must have been previously returned from a |
928 |
< |
* call to {@link System.nanoTime()}. |
928 |
> |
* call to {@link System#nanoTime()}. |
929 |
|
*/ |
930 |
< |
long millisElapsedSince(long startNanoTime) { |
930 |
> |
static long millisElapsedSince(long startNanoTime) { |
931 |
|
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
932 |
|
} |
933 |
|
|
934 |
+ |
// void assertTerminatesPromptly(long timeoutMillis, Runnable r) { |
935 |
+ |
// long startTime = System.nanoTime(); |
936 |
+ |
// try { |
937 |
+ |
// r.run(); |
938 |
+ |
// } catch (Throwable fail) { threadUnexpectedException(fail); } |
939 |
+ |
// if (millisElapsedSince(startTime) > timeoutMillis/2) |
940 |
+ |
// throw new AssertionFailedError("did not return promptly"); |
941 |
+ |
// } |
942 |
+ |
|
943 |
+ |
// void assertTerminatesPromptly(Runnable r) { |
944 |
+ |
// assertTerminatesPromptly(LONG_DELAY_MS/2, r); |
945 |
+ |
// } |
946 |
+ |
|
947 |
+ |
/** |
948 |
+ |
* Checks that timed f.get() returns the expected value, and does not |
949 |
+ |
* wait for the timeout to elapse before returning. |
950 |
+ |
*/ |
951 |
+ |
<T> void checkTimedGet(Future<T> f, T expectedValue, long timeoutMillis) { |
952 |
+ |
long startTime = System.nanoTime(); |
953 |
+ |
try { |
954 |
+ |
assertEquals(expectedValue, f.get(timeoutMillis, MILLISECONDS)); |
955 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
956 |
+ |
if (millisElapsedSince(startTime) > timeoutMillis/2) |
957 |
+ |
throw new AssertionFailedError("timed get did not return promptly"); |
958 |
+ |
} |
959 |
+ |
|
960 |
+ |
<T> void checkTimedGet(Future<T> f, T expectedValue) { |
961 |
+ |
checkTimedGet(f, expectedValue, LONG_DELAY_MS); |
962 |
+ |
} |
963 |
+ |
|
964 |
|
/** |
965 |
|
* Returns a new started daemon Thread running the given runnable. |
966 |
|
*/ |
1469 |
|
shouldThrow(expectedExceptionClass.getName()); |
1470 |
|
} |
1471 |
|
} |
1472 |
+ |
|
1473 |
+ |
public void assertIteratorExhausted(Iterator<?> it) { |
1474 |
+ |
try { |
1475 |
+ |
it.next(); |
1476 |
+ |
shouldThrow(); |
1477 |
+ |
} catch (NoSuchElementException success) {} |
1478 |
+ |
assertFalse(it.hasNext()); |
1479 |
+ |
} |
1480 |
|
} |