11 |
|
import java.io.ByteArrayOutputStream; |
12 |
|
import java.io.ObjectInputStream; |
13 |
|
import java.io.ObjectOutputStream; |
14 |
+ |
import java.lang.management.ManagementFactory; |
15 |
+ |
import java.lang.management.ThreadInfo; |
16 |
+ |
import java.lang.reflect.Method; |
17 |
+ |
import java.util.ArrayList; |
18 |
|
import java.util.Arrays; |
19 |
|
import java.util.Date; |
20 |
+ |
import java.util.Enumeration; |
21 |
+ |
import java.util.List; |
22 |
|
import java.util.NoSuchElementException; |
23 |
|
import java.util.PropertyPermission; |
24 |
|
import java.util.concurrent.*; |
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 { |
156 |
|
} |
157 |
|
|
158 |
|
/** |
159 |
< |
* Runs all JSR166 unit tests using junit.textui.TestRunner |
159 |
> |
* Runs all JSR166 unit tests using junit.textui.TestRunner. |
160 |
> |
* Optional command line arg provides the number of iterations to |
161 |
> |
* repeat running the tests. |
162 |
|
*/ |
163 |
|
public static void main(String[] args) { |
164 |
|
if (useSecurityManager) { |
190 |
|
return suite; |
191 |
|
} |
192 |
|
|
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 |
+ |
|
207 |
+ |
public static final double JAVA_CLASS_VERSION; |
208 |
+ |
static { |
209 |
+ |
try { |
210 |
+ |
JAVA_CLASS_VERSION = java.security.AccessController.doPrivileged( |
211 |
+ |
new java.security.PrivilegedAction<Double>() { |
212 |
+ |
public Double run() { |
213 |
+ |
return Double.valueOf(System.getProperty("java.class.version"));}}); |
214 |
+ |
} catch (Throwable t) { |
215 |
+ |
throw new Error(t); |
216 |
+ |
} |
217 |
+ |
} |
218 |
+ |
|
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 |
< |
return newTestSuite( |
227 |
> |
// Java7+ test classes |
228 |
> |
TestSuite suite = newTestSuite( |
229 |
|
ForkJoinPoolTest.suite(), |
230 |
|
ForkJoinTaskTest.suite(), |
231 |
|
RecursiveActionTest.suite(), |
232 |
|
RecursiveTaskTest.suite(), |
233 |
|
LinkedTransferQueueTest.suite(), |
234 |
|
PhaserTest.suite(), |
235 |
+ |
SplittableRandomTest.suite(), |
236 |
|
ThreadLocalRandomTest.suite(), |
237 |
|
AbstractExecutorServiceTest.suite(), |
238 |
|
AbstractQueueTest.suite(), |
291 |
|
TreeSetTest.suite(), |
292 |
|
TreeSubMapTest.suite(), |
293 |
|
TreeSubSetTest.suite()); |
294 |
+ |
|
295 |
+ |
// Java8+ test classes |
296 |
+ |
if (atLeastJava8()) { |
297 |
+ |
String[] java8TestClassNames = { |
298 |
+ |
"CompletableFutureTest", |
299 |
+ |
"ConcurrentHashMap8Test", |
300 |
+ |
"CountedCompleterTest", |
301 |
+ |
"DoubleAccumulatorTest", |
302 |
+ |
"DoubleAdderTest", |
303 |
+ |
"ForkJoinPool8Test", |
304 |
+ |
"LongAccumulatorTest", |
305 |
+ |
"LongAdderTest", |
306 |
+ |
"StampedLockTest", |
307 |
+ |
}; |
308 |
+ |
addNamedTestClasses(suite, java8TestClassNames); |
309 |
+ |
} |
310 |
+ |
|
311 |
+ |
return suite; |
312 |
|
} |
313 |
|
|
314 |
+ |
// Delays for timing-dependent tests, in milliseconds. |
315 |
|
|
316 |
|
public static long SHORT_DELAY_MS; |
317 |
|
public static long SMALL_DELAY_MS; |
318 |
|
public static long MEDIUM_DELAY_MS; |
319 |
|
public static long LONG_DELAY_MS; |
320 |
|
|
254 |
– |
|
321 |
|
/** |
322 |
|
* Returns the shortest timed delay. This could |
323 |
|
* be reimplemented to use for example a Property. |
400 |
|
|
401 |
|
if (Thread.interrupted()) |
402 |
|
throw new AssertionFailedError("interrupt status set in main thread"); |
403 |
+ |
|
404 |
+ |
checkForkJoinPoolThreadLeaks(); |
405 |
+ |
} |
406 |
+ |
|
407 |
+ |
/** |
408 |
+ |
* Find missing try { ... } finally { joinPool(e); } |
409 |
+ |
*/ |
410 |
+ |
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
411 |
+ |
Thread[] survivors = new Thread[5]; |
412 |
+ |
int count = Thread.enumerate(survivors); |
413 |
+ |
for (int i = 0; i < count; i++) { |
414 |
+ |
Thread thread = survivors[i]; |
415 |
+ |
String name = thread.getName(); |
416 |
+ |
if (name.startsWith("ForkJoinPool-")) { |
417 |
+ |
// give thread some time to terminate |
418 |
+ |
thread.join(LONG_DELAY_MS); |
419 |
+ |
if (!thread.isAlive()) continue; |
420 |
+ |
thread.stop(); |
421 |
+ |
throw new AssertionFailedError |
422 |
+ |
(String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n", |
423 |
+ |
toString(), name)); |
424 |
+ |
} |
425 |
+ |
} |
426 |
|
} |
427 |
|
|
428 |
|
/** |
596 |
|
} |
597 |
|
|
598 |
|
/** |
599 |
+ |
* A debugging tool to print all stack traces, as jstack does. |
600 |
+ |
*/ |
601 |
+ |
static void printAllStackTraces() { |
602 |
+ |
for (ThreadInfo info : |
603 |
+ |
ManagementFactory.getThreadMXBean() |
604 |
+ |
.dumpAllThreads(true, true)) |
605 |
+ |
System.err.print(info); |
606 |
+ |
} |
607 |
+ |
|
608 |
+ |
/** |
609 |
|
* Checks that thread does not terminate within the default |
610 |
|
* millisecond delay of {@code timeoutMillis()}. |
611 |
|
*/ |
710 |
|
public static final Integer m6 = new Integer(-6); |
711 |
|
public static final Integer m10 = new Integer(-10); |
712 |
|
|
614 |
– |
|
713 |
|
/** |
714 |
|
* Runs Runnable r with a security policy that permits precisely |
715 |
|
* the specified permissions. If there is no current security |
721 |
|
SecurityManager sm = System.getSecurityManager(); |
722 |
|
if (sm == null) { |
723 |
|
r.run(); |
724 |
+ |
} |
725 |
+ |
runWithSecurityManagerWithPermissions(r, permissions); |
726 |
+ |
} |
727 |
+ |
|
728 |
+ |
/** |
729 |
+ |
* Runs Runnable r with a security policy that permits precisely |
730 |
+ |
* the specified permissions. If there is no current security |
731 |
+ |
* manager, a temporary one is set for the duration of the |
732 |
+ |
* Runnable. We require that any security manager permit |
733 |
+ |
* getPolicy/setPolicy. |
734 |
+ |
*/ |
735 |
+ |
public void runWithSecurityManagerWithPermissions(Runnable r, |
736 |
+ |
Permission... permissions) { |
737 |
+ |
SecurityManager sm = System.getSecurityManager(); |
738 |
+ |
if (sm == null) { |
739 |
|
Policy savedPolicy = Policy.getPolicy(); |
740 |
|
try { |
741 |
|
Policy.setPolicy(permissivePolicy()); |
742 |
|
System.setSecurityManager(new SecurityManager()); |
743 |
< |
runWithPermissions(r, permissions); |
743 |
> |
runWithSecurityManagerWithPermissions(r, permissions); |
744 |
|
} finally { |
745 |
|
System.setSecurityManager(null); |
746 |
|
Policy.setPolicy(savedPolicy); |
788 |
|
return perms.implies(p); |
789 |
|
} |
790 |
|
public void refresh() {} |
791 |
+ |
public String toString() { |
792 |
+ |
List<Permission> ps = new ArrayList<Permission>(); |
793 |
+ |
for (Enumeration<Permission> e = perms.elements(); e.hasMoreElements();) |
794 |
+ |
ps.add(e.nextElement()); |
795 |
+ |
return "AdjustablePolicy with permissions " + ps; |
796 |
+ |
} |
797 |
|
} |
798 |
|
|
799 |
|
/** |
1240 |
|
public abstract class CheckedRecursiveAction extends RecursiveAction { |
1241 |
|
protected abstract void realCompute() throws Throwable; |
1242 |
|
|
1243 |
< |
public final void compute() { |
1243 |
> |
@Override protected final void compute() { |
1244 |
|
try { |
1245 |
|
realCompute(); |
1246 |
|
} catch (Throwable t) { |
1255 |
|
public abstract class CheckedRecursiveTask<T> extends RecursiveTask<T> { |
1256 |
|
protected abstract T realCompute() throws Throwable; |
1257 |
|
|
1258 |
< |
public final T compute() { |
1258 |
> |
@Override protected final T compute() { |
1259 |
|
try { |
1260 |
|
return realCompute(); |
1261 |
|
} catch (Throwable t) { |
1356 |
|
return null; |
1357 |
|
} |
1358 |
|
} |
1359 |
+ |
|
1360 |
+ |
public void assertThrows(Class<? extends Throwable> expectedExceptionClass, |
1361 |
+ |
Runnable... throwingActions) { |
1362 |
+ |
for (Runnable throwingAction : throwingActions) { |
1363 |
+ |
boolean threw = false; |
1364 |
+ |
try { throwingAction.run(); } |
1365 |
+ |
catch (Throwable t) { |
1366 |
+ |
threw = true; |
1367 |
+ |
if (!expectedExceptionClass.isInstance(t)) { |
1368 |
+ |
AssertionFailedError afe = |
1369 |
+ |
new AssertionFailedError |
1370 |
+ |
("Expected " + expectedExceptionClass.getName() + |
1371 |
+ |
", got " + t.getClass().getName()); |
1372 |
+ |
afe.initCause(t); |
1373 |
+ |
threadUnexpectedException(afe); |
1374 |
+ |
} |
1375 |
+ |
} |
1376 |
+ |
if (!threw) |
1377 |
+ |
shouldThrow(expectedExceptionClass.getName()); |
1378 |
+ |
} |
1379 |
+ |
} |
1380 |
|
} |