26 |
|
import java.util.concurrent.atomic.AtomicReference; |
27 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
28 |
|
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
29 |
+ |
import java.util.regex.Pattern; |
30 |
|
import java.security.CodeSource; |
31 |
|
import java.security.Permission; |
32 |
|
import java.security.PermissionCollection; |
115 |
|
protected static final boolean expensiveTests = |
116 |
|
Boolean.getBoolean("jsr166.expensiveTests"); |
117 |
|
|
118 |
+ |
protected static final boolean testImplementationDetails = |
119 |
+ |
Boolean.getBoolean("jsr166.testImplementationDetails"); |
120 |
+ |
|
121 |
|
/** |
122 |
|
* If true, report on stdout all "slow" tests, that is, ones that |
123 |
|
* take more than profileThreshold milliseconds to execute. |
132 |
|
private static final long profileThreshold = |
133 |
|
Long.getLong("jsr166.profileThreshold", 100); |
134 |
|
|
135 |
+ |
/** |
136 |
+ |
* The number of repetitions per test (for tickling rare bugs). |
137 |
+ |
*/ |
138 |
+ |
private static final int runsPerTest = |
139 |
+ |
Integer.getInteger("jsr166.runsPerTest", 1); |
140 |
+ |
|
141 |
+ |
/** |
142 |
+ |
* A filter for tests to run, matching strings of the form |
143 |
+ |
* methodName(className), e.g. "testInvokeAll5(ForkJoinPoolTest)" |
144 |
+ |
* Usefully combined with jsr166.runsPerTest. |
145 |
+ |
*/ |
146 |
+ |
private static final Pattern methodFilter = methodFilter(); |
147 |
+ |
|
148 |
+ |
private static Pattern methodFilter() { |
149 |
+ |
String regex = System.getProperty("jsr166.methodFilter"); |
150 |
+ |
return (regex == null) ? null : Pattern.compile(regex); |
151 |
+ |
} |
152 |
+ |
|
153 |
|
protected void runTest() throws Throwable { |
154 |
< |
if (profileTests) |
155 |
< |
runTestProfiled(); |
156 |
< |
else |
157 |
< |
super.runTest(); |
154 |
> |
if (methodFilter == null |
155 |
> |
|| methodFilter.matcher(toString()).find()) { |
156 |
> |
for (int i = 0; i < runsPerTest; i++) { |
157 |
> |
if (profileTests) |
158 |
> |
runTestProfiled(); |
159 |
> |
else |
160 |
> |
super.runTest(); |
161 |
> |
} |
162 |
> |
} |
163 |
|
} |
164 |
|
|
165 |
|
protected void runTestProfiled() throws Throwable { |
166 |
+ |
// Warmup run, notably to trigger all needed classloading. |
167 |
+ |
super.runTest(); |
168 |
|
long t0 = System.nanoTime(); |
169 |
|
try { |
170 |
|
super.runTest(); |
171 |
|
} finally { |
172 |
< |
long elapsedMillis = |
144 |
< |
(System.nanoTime() - t0) / (1000L * 1000L); |
172 |
> |
long elapsedMillis = millisElapsedSince(t0); |
173 |
|
if (elapsedMillis >= profileThreshold) |
174 |
|
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); |
175 |
|
} |
225 |
|
} |
226 |
|
|
227 |
|
public static final double JAVA_CLASS_VERSION; |
228 |
+ |
public static final String JAVA_SPECIFICATION_VERSION; |
229 |
|
static { |
230 |
|
try { |
231 |
|
JAVA_CLASS_VERSION = java.security.AccessController.doPrivileged( |
232 |
|
new java.security.PrivilegedAction<Double>() { |
233 |
|
public Double run() { |
234 |
|
return Double.valueOf(System.getProperty("java.class.version"));}}); |
235 |
+ |
JAVA_SPECIFICATION_VERSION = java.security.AccessController.doPrivileged( |
236 |
+ |
new java.security.PrivilegedAction<String>() { |
237 |
+ |
public String run() { |
238 |
+ |
return System.getProperty("java.specification.version");}}); |
239 |
|
} catch (Throwable t) { |
240 |
|
throw new Error(t); |
241 |
|
} |
244 |
|
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
245 |
|
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
246 |
|
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
247 |
+ |
public static boolean atLeastJava9() { |
248 |
+ |
// As of 2014-05, java9 still uses 52.0 class file version |
249 |
+ |
return JAVA_SPECIFICATION_VERSION.startsWith("1.9"); |
250 |
+ |
} |
251 |
|
|
252 |
|
/** |
253 |
|
* Collects all JSR166 unit tests as one suite. |
323 |
|
// Java8+ test classes |
324 |
|
if (atLeastJava8()) { |
325 |
|
String[] java8TestClassNames = { |
326 |
+ |
"Atomic8Test", |
327 |
|
"CompletableFutureTest", |
328 |
|
"ConcurrentHashMap8Test", |
329 |
|
"CountedCompleterTest", |
330 |
|
"DoubleAccumulatorTest", |
331 |
|
"DoubleAdderTest", |
332 |
|
"ForkJoinPool8Test", |
333 |
+ |
"ForkJoinTask8Test", |
334 |
|
"LongAccumulatorTest", |
335 |
|
"LongAdderTest", |
336 |
+ |
"SplittableRandomTest", |
337 |
|
"StampedLockTest", |
338 |
+ |
"ThreadLocalRandom8Test", |
339 |
|
}; |
340 |
|
addNamedTestClasses(suite, java8TestClassNames); |
341 |
|
} |
342 |
|
|
343 |
+ |
// Java9+ test classes |
344 |
+ |
if (atLeastJava9()) { |
345 |
+ |
String[] java9TestClassNames = { |
346 |
+ |
"ThreadPoolExecutor9Test", |
347 |
+ |
}; |
348 |
+ |
addNamedTestClasses(suite, java9TestClassNames); |
349 |
+ |
} |
350 |
+ |
|
351 |
|
return suite; |
352 |
|
} |
353 |
|
|
354 |
+ |
// Delays for timing-dependent tests, in milliseconds. |
355 |
|
|
356 |
|
public static long SHORT_DELAY_MS; |
357 |
|
public static long SMALL_DELAY_MS; |
358 |
|
public static long MEDIUM_DELAY_MS; |
359 |
|
public static long LONG_DELAY_MS; |
360 |
|
|
311 |
– |
|
361 |
|
/** |
362 |
|
* Returns the shortest timed delay. This could |
363 |
|
* be reimplemented to use for example a Property. |
750 |
|
public static final Integer m6 = new Integer(-6); |
751 |
|
public static final Integer m10 = new Integer(-10); |
752 |
|
|
704 |
– |
|
753 |
|
/** |
754 |
|
* Runs Runnable r with a security policy that permits precisely |
755 |
|
* the specified permissions. If there is no current security |
905 |
|
* startNanoTime, which must have been previously returned from a |
906 |
|
* call to {@link System.nanoTime()}. |
907 |
|
*/ |
908 |
< |
long millisElapsedSince(long startNanoTime) { |
908 |
> |
static long millisElapsedSince(long startNanoTime) { |
909 |
|
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
910 |
|
} |
911 |
|
|
1280 |
|
public abstract class CheckedRecursiveAction extends RecursiveAction { |
1281 |
|
protected abstract void realCompute() throws Throwable; |
1282 |
|
|
1283 |
< |
public final void compute() { |
1283 |
> |
@Override protected final void compute() { |
1284 |
|
try { |
1285 |
|
realCompute(); |
1286 |
|
} catch (Throwable t) { |
1295 |
|
public abstract class CheckedRecursiveTask<T> extends RecursiveTask<T> { |
1296 |
|
protected abstract T realCompute() throws Throwable; |
1297 |
|
|
1298 |
< |
public final T compute() { |
1298 |
> |
@Override protected final T compute() { |
1299 |
|
try { |
1300 |
|
return realCompute(); |
1301 |
|
} catch (Throwable t) { |
1396 |
|
return null; |
1397 |
|
} |
1398 |
|
} |
1399 |
+ |
|
1400 |
+ |
public void assertThrows(Class<? extends Throwable> expectedExceptionClass, |
1401 |
+ |
Runnable... throwingActions) { |
1402 |
+ |
for (Runnable throwingAction : throwingActions) { |
1403 |
+ |
boolean threw = false; |
1404 |
+ |
try { throwingAction.run(); } |
1405 |
+ |
catch (Throwable t) { |
1406 |
+ |
threw = true; |
1407 |
+ |
if (!expectedExceptionClass.isInstance(t)) { |
1408 |
+ |
AssertionFailedError afe = |
1409 |
+ |
new AssertionFailedError |
1410 |
+ |
("Expected " + expectedExceptionClass.getName() + |
1411 |
+ |
", got " + t.getClass().getName()); |
1412 |
+ |
afe.initCause(t); |
1413 |
+ |
threadUnexpectedException(afe); |
1414 |
+ |
} |
1415 |
+ |
} |
1416 |
+ |
if (!threw) |
1417 |
+ |
shouldThrow(expectedExceptionClass.getName()); |
1418 |
+ |
} |
1419 |
+ |
} |
1420 |
|
} |