128 |
|
private static final long profileThreshold = |
129 |
|
Long.getLong("jsr166.profileThreshold", 100); |
130 |
|
|
131 |
+ |
/** |
132 |
+ |
* The number of repetitions per test (for tickling rare bugs). |
133 |
+ |
*/ |
134 |
+ |
private static final int runsPerTest = |
135 |
+ |
Integer.getInteger("jsr166.runsPerTest", 1); |
136 |
+ |
|
137 |
|
protected void runTest() throws Throwable { |
138 |
< |
if (profileTests) |
139 |
< |
runTestProfiled(); |
140 |
< |
else |
141 |
< |
super.runTest(); |
138 |
> |
for (int i = 0; i < runsPerTest; i++) { |
139 |
> |
if (profileTests) |
140 |
> |
runTestProfiled(); |
141 |
> |
else |
142 |
> |
super.runTest(); |
143 |
> |
} |
144 |
|
} |
145 |
|
|
146 |
|
protected void runTestProfiled() throws Throwable { |
190 |
|
return suite; |
191 |
|
} |
192 |
|
|
193 |
< |
static void addTestReflectively(TestSuite suite, String testClassName) { |
194 |
< |
try { |
195 |
< |
Class klazz = Class.forName(testClassName); |
196 |
< |
Method m = klazz.getDeclaredMethod("suite", new Class<?>[0]); |
197 |
< |
suite.addTest(newTestSuite((Test)m.invoke(null))); |
198 |
< |
} catch (Exception e) { |
199 |
< |
throw new Error(e); |
193 |
> |
public static void addNamedTestClasses(TestSuite suite, |
194 |
> |
String... testClassNames) { |
195 |
> |
for (String testClassName : testClassNames) { |
196 |
> |
try { |
197 |
> |
Class<?> testClass = Class.forName(testClassName); |
198 |
> |
Method m = testClass.getDeclaredMethod("suite", |
199 |
> |
new Class<?>[0]); |
200 |
> |
suite.addTest(newTestSuite((Test)m.invoke(null))); |
201 |
> |
} catch (Exception e) { |
202 |
> |
throw new Error("Missing test class", e); |
203 |
> |
} |
204 |
|
} |
205 |
|
} |
206 |
|
|
216 |
|
} |
217 |
|
} |
218 |
|
|
219 |
< |
public static boolean isAtLeastJdk6() { return JAVA_CLASS_VERSION >= 50.0; } |
220 |
< |
public static boolean isAtLeastJdk7() { return JAVA_CLASS_VERSION >= 51.0; } |
221 |
< |
public static boolean isAtLeastJdk8() { return JAVA_CLASS_VERSION >= 52.0; } |
219 |
> |
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
220 |
> |
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
221 |
> |
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
222 |
|
|
223 |
|
/** |
224 |
|
* Collects all JSR166 unit tests as one suite. |
225 |
|
*/ |
226 |
|
public static Test suite() { |
227 |
+ |
// Java7+ test classes |
228 |
|
TestSuite suite = newTestSuite( |
229 |
|
ForkJoinPoolTest.suite(), |
230 |
|
ForkJoinTaskTest.suite(), |
290 |
|
TreeSetTest.suite(), |
291 |
|
TreeSubMapTest.suite(), |
292 |
|
TreeSubSetTest.suite()); |
293 |
< |
if (isAtLeastJdk8()) { |
294 |
< |
addTestReflectively(suite, "StampedLockTest"); |
293 |
> |
|
294 |
> |
// Java8+ test classes |
295 |
> |
if (atLeastJava8()) { |
296 |
> |
String[] java8TestClassNames = { |
297 |
> |
"CompletableFutureTest", |
298 |
> |
"ConcurrentHashMap8Test", |
299 |
> |
"CountedCompleterTest", |
300 |
> |
"DoubleAccumulatorTest", |
301 |
> |
"DoubleAdderTest", |
302 |
> |
"ForkJoinPool8Test", |
303 |
> |
"ForkJoinTask8Test", |
304 |
> |
"LongAccumulatorTest", |
305 |
> |
"LongAdderTest", |
306 |
> |
"SplittableRandomTest", |
307 |
> |
"StampedLockTest", |
308 |
> |
}; |
309 |
> |
addNamedTestClasses(suite, java8TestClassNames); |
310 |
|
} |
311 |
+ |
|
312 |
|
return suite; |
313 |
|
} |
314 |
|
|
315 |
+ |
// Delays for timing-dependent tests, in milliseconds. |
316 |
|
|
317 |
|
public static long SHORT_DELAY_MS; |
318 |
|
public static long SMALL_DELAY_MS; |
319 |
|
public static long MEDIUM_DELAY_MS; |
320 |
|
public static long LONG_DELAY_MS; |
321 |
|
|
292 |
– |
|
322 |
|
/** |
323 |
|
* Returns the shortest timed delay. This could |
324 |
|
* be reimplemented to use for example a Property. |
401 |
|
|
402 |
|
if (Thread.interrupted()) |
403 |
|
throw new AssertionFailedError("interrupt status set in main thread"); |
404 |
+ |
|
405 |
+ |
checkForkJoinPoolThreadLeaks(); |
406 |
+ |
} |
407 |
+ |
|
408 |
+ |
/** |
409 |
+ |
* Find missing try { ... } finally { joinPool(e); } |
410 |
+ |
*/ |
411 |
+ |
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
412 |
+ |
Thread[] survivors = new Thread[5]; |
413 |
+ |
int count = Thread.enumerate(survivors); |
414 |
+ |
for (int i = 0; i < count; i++) { |
415 |
+ |
Thread thread = survivors[i]; |
416 |
+ |
String name = thread.getName(); |
417 |
+ |
if (name.startsWith("ForkJoinPool-")) { |
418 |
+ |
// give thread some time to terminate |
419 |
+ |
thread.join(LONG_DELAY_MS); |
420 |
+ |
if (!thread.isAlive()) continue; |
421 |
+ |
thread.stop(); |
422 |
+ |
throw new AssertionFailedError |
423 |
+ |
(String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n", |
424 |
+ |
toString(), name)); |
425 |
+ |
} |
426 |
+ |
} |
427 |
|
} |
428 |
|
|
429 |
|
/** |
711 |
|
public static final Integer m6 = new Integer(-6); |
712 |
|
public static final Integer m10 = new Integer(-10); |
713 |
|
|
662 |
– |
|
714 |
|
/** |
715 |
|
* Runs Runnable r with a security policy that permits precisely |
716 |
|
* the specified permissions. If there is no current security |
1241 |
|
public abstract class CheckedRecursiveAction extends RecursiveAction { |
1242 |
|
protected abstract void realCompute() throws Throwable; |
1243 |
|
|
1244 |
< |
public final void compute() { |
1244 |
> |
@Override protected final void compute() { |
1245 |
|
try { |
1246 |
|
realCompute(); |
1247 |
|
} catch (Throwable t) { |
1256 |
|
public abstract class CheckedRecursiveTask<T> extends RecursiveTask<T> { |
1257 |
|
protected abstract T realCompute() throws Throwable; |
1258 |
|
|
1259 |
< |
public final T compute() { |
1259 |
> |
@Override protected final T compute() { |
1260 |
|
try { |
1261 |
|
return realCompute(); |
1262 |
|
} catch (Throwable t) { |
1357 |
|
return null; |
1358 |
|
} |
1359 |
|
} |
1360 |
+ |
|
1361 |
+ |
public void assertThrows(Class<? extends Throwable> expectedExceptionClass, |
1362 |
+ |
Runnable... throwingActions) { |
1363 |
+ |
for (Runnable throwingAction : throwingActions) { |
1364 |
+ |
boolean threw = false; |
1365 |
+ |
try { throwingAction.run(); } |
1366 |
+ |
catch (Throwable t) { |
1367 |
+ |
threw = true; |
1368 |
+ |
if (!expectedExceptionClass.isInstance(t)) { |
1369 |
+ |
AssertionFailedError afe = |
1370 |
+ |
new AssertionFailedError |
1371 |
+ |
("Expected " + expectedExceptionClass.getName() + |
1372 |
+ |
", got " + t.getClass().getName()); |
1373 |
+ |
afe.initCause(t); |
1374 |
+ |
threadUnexpectedException(afe); |
1375 |
+ |
} |
1376 |
+ |
} |
1377 |
+ |
if (!threw) |
1378 |
+ |
shouldThrow(expectedExceptionClass.getName()); |
1379 |
+ |
} |
1380 |
+ |
} |
1381 |
|
} |