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.*; |
75 |
|
* |
76 |
|
* </ol> |
77 |
|
* |
78 |
< |
* <p> <b>Other notes</b> |
78 |
> |
* <p><b>Other notes</b> |
79 |
|
* <ul> |
80 |
|
* |
81 |
|
* <li> Usually, there is one testcase method per JSR166 method |
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(), |
290 |
|
TreeSetTest.suite(), |
291 |
|
TreeSubMapTest.suite(), |
292 |
|
TreeSubSetTest.suite()); |
293 |
+ |
|
294 |
+ |
// Java8+ test classes |
295 |
+ |
if (atLeastJava8()) { |
296 |
+ |
String[] java8TestClassNames = { |
297 |
+ |
"CompletableFutureTest", |
298 |
+ |
"ConcurrentHashMap8Test", |
299 |
+ |
"CountedCompleterTest", |
300 |
+ |
"DoubleAccumulatorTest", |
301 |
+ |
"DoubleAdderTest", |
302 |
+ |
"ForkJoinPool8Test", |
303 |
+ |
"LongAccumulatorTest", |
304 |
+ |
"LongAdderTest", |
305 |
+ |
"StampedLockTest", |
306 |
+ |
}; |
307 |
+ |
addNamedTestClasses(suite, java8TestClassNames); |
308 |
+ |
} |
309 |
+ |
|
310 |
+ |
return suite; |
311 |
|
} |
312 |
|
|
313 |
|
|
399 |
|
|
400 |
|
if (Thread.interrupted()) |
401 |
|
throw new AssertionFailedError("interrupt status set in main thread"); |
402 |
+ |
|
403 |
+ |
checkForkJoinPoolThreadLeaks(); |
404 |
+ |
} |
405 |
+ |
|
406 |
+ |
/** |
407 |
+ |
* Find missing try { ... } finally { joinPool(e); } |
408 |
+ |
*/ |
409 |
+ |
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
410 |
+ |
Thread[] survivors = new Thread[5]; |
411 |
+ |
int count = Thread.enumerate(survivors); |
412 |
+ |
for (int i = 0; i < count; i++) { |
413 |
+ |
Thread thread = survivors[i]; |
414 |
+ |
String name = thread.getName(); |
415 |
+ |
if (name.startsWith("ForkJoinPool-")) { |
416 |
+ |
// give thread some time to terminate |
417 |
+ |
thread.join(LONG_DELAY_MS); |
418 |
+ |
if (!thread.isAlive()) continue; |
419 |
+ |
thread.stop(); |
420 |
+ |
throw new AssertionFailedError |
421 |
+ |
(String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n", |
422 |
+ |
toString(), name)); |
423 |
+ |
} |
424 |
+ |
} |
425 |
|
} |
426 |
|
|
427 |
|
/** |
595 |
|
} |
596 |
|
|
597 |
|
/** |
598 |
+ |
* A debugging tool to print all stack traces, as jstack does. |
599 |
+ |
*/ |
600 |
+ |
static void printAllStackTraces() { |
601 |
+ |
for (ThreadInfo info : |
602 |
+ |
ManagementFactory.getThreadMXBean() |
603 |
+ |
.dumpAllThreads(true, true)) |
604 |
+ |
System.err.print(info); |
605 |
+ |
} |
606 |
+ |
|
607 |
+ |
/** |
608 |
|
* Checks that thread does not terminate within the default |
609 |
|
* millisecond delay of {@code timeoutMillis()}. |
610 |
|
*/ |
626 |
|
} |
627 |
|
|
628 |
|
/** |
629 |
+ |
* Checks that the threads do not terminate within the default |
630 |
+ |
* millisecond delay of {@code timeoutMillis()}. |
631 |
+ |
*/ |
632 |
+ |
void assertThreadsStayAlive(Thread... threads) { |
633 |
+ |
assertThreadsStayAlive(timeoutMillis(), threads); |
634 |
+ |
} |
635 |
+ |
|
636 |
+ |
/** |
637 |
+ |
* Checks that the threads do not terminate within the given millisecond delay. |
638 |
+ |
*/ |
639 |
+ |
void assertThreadsStayAlive(long millis, Thread... threads) { |
640 |
+ |
try { |
641 |
+ |
// No need to optimize the failing case via Thread.join. |
642 |
+ |
delay(millis); |
643 |
+ |
for (Thread thread : threads) |
644 |
+ |
assertTrue(thread.isAlive()); |
645 |
+ |
} catch (InterruptedException ie) { |
646 |
+ |
fail("Unexpected InterruptedException"); |
647 |
+ |
} |
648 |
+ |
} |
649 |
+ |
|
650 |
+ |
/** |
651 |
|
* Checks that future.get times out, with the default timeout of |
652 |
|
* {@code timeoutMillis()}. |
653 |
|
*/ |
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 |
|
/** |
1045 |
|
} |
1046 |
|
} |
1047 |
|
|
1048 |
+ |
public void await(Semaphore semaphore) { |
1049 |
+ |
try { |
1050 |
+ |
assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
1051 |
+ |
} catch (Throwable t) { |
1052 |
+ |
threadUnexpectedException(t); |
1053 |
+ |
} |
1054 |
+ |
} |
1055 |
+ |
|
1056 |
|
// /** |
1057 |
|
// * Spin-waits up to LONG_DELAY_MS until flag becomes true. |
1058 |
|
// */ |
1321 |
|
} |
1322 |
|
} |
1323 |
|
|
1324 |
< |
@SuppressWarnings("unchecked") |
1325 |
< |
<T> T serialClone(T o) { |
1324 |
> |
void assertSerialEquals(Object x, Object y) { |
1325 |
> |
assertTrue(Arrays.equals(serialBytes(x), serialBytes(y))); |
1326 |
> |
} |
1327 |
> |
|
1328 |
> |
void assertNotSerialEquals(Object x, Object y) { |
1329 |
> |
assertFalse(Arrays.equals(serialBytes(x), serialBytes(y))); |
1330 |
> |
} |
1331 |
> |
|
1332 |
> |
byte[] serialBytes(Object o) { |
1333 |
|
try { |
1334 |
|
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1335 |
|
ObjectOutputStream oos = new ObjectOutputStream(bos); |
1336 |
|
oos.writeObject(o); |
1337 |
|
oos.flush(); |
1338 |
|
oos.close(); |
1339 |
+ |
return bos.toByteArray(); |
1340 |
+ |
} catch (Throwable t) { |
1341 |
+ |
threadUnexpectedException(t); |
1342 |
+ |
return new byte[0]; |
1343 |
+ |
} |
1344 |
+ |
} |
1345 |
+ |
|
1346 |
+ |
@SuppressWarnings("unchecked") |
1347 |
+ |
<T> T serialClone(T o) { |
1348 |
+ |
try { |
1349 |
|
ObjectInputStream ois = new ObjectInputStream |
1350 |
< |
(new ByteArrayInputStream(bos.toByteArray())); |
1350 |
> |
(new ByteArrayInputStream(serialBytes(o))); |
1351 |
|
T clone = (T) ois.readObject(); |
1352 |
|
assertSame(o.getClass(), clone.getClass()); |
1353 |
|
return clone; |
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 |
|
} |