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.*; |
25 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
26 |
|
import java.util.concurrent.atomic.AtomicReference; |
27 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
28 |
|
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
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 |
148 |
|
} |
149 |
|
|
150 |
|
/** |
151 |
< |
* Runs all JSR166 unit tests using junit.textui.TestRunner |
151 |
> |
* Runs all JSR166 unit tests using junit.textui.TestRunner. |
152 |
> |
* Optional command line arg provides the number of iterations to |
153 |
> |
* repeat running the tests. |
154 |
|
*/ |
155 |
|
public static void main(String[] args) { |
156 |
|
if (useSecurityManager) { |
182 |
|
return suite; |
183 |
|
} |
184 |
|
|
185 |
+ |
public static void addNamedTestClasses(TestSuite suite, |
186 |
+ |
String... testClassNames) { |
187 |
+ |
for (String testClassName : testClassNames) { |
188 |
+ |
try { |
189 |
+ |
Class<?> testClass = Class.forName(testClassName); |
190 |
+ |
Method m = testClass.getDeclaredMethod("suite", |
191 |
+ |
new Class<?>[0]); |
192 |
+ |
suite.addTest(newTestSuite((Test)m.invoke(null))); |
193 |
+ |
} catch (Exception e) { |
194 |
+ |
throw new Error("Missing test class", e); |
195 |
+ |
} |
196 |
+ |
} |
197 |
+ |
} |
198 |
+ |
|
199 |
+ |
public static final double JAVA_CLASS_VERSION; |
200 |
+ |
static { |
201 |
+ |
try { |
202 |
+ |
JAVA_CLASS_VERSION = java.security.AccessController.doPrivileged( |
203 |
+ |
new java.security.PrivilegedAction<Double>() { |
204 |
+ |
public Double run() { |
205 |
+ |
return Double.valueOf(System.getProperty("java.class.version"));}}); |
206 |
+ |
} catch (Throwable t) { |
207 |
+ |
throw new Error(t); |
208 |
+ |
} |
209 |
+ |
} |
210 |
+ |
|
211 |
+ |
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
212 |
+ |
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
213 |
+ |
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
214 |
+ |
|
215 |
|
/** |
216 |
|
* Collects all JSR166 unit tests as one suite. |
217 |
|
*/ |
218 |
|
public static Test suite() { |
219 |
< |
return newTestSuite( |
219 |
> |
// Java7+ test classes |
220 |
> |
TestSuite suite = newTestSuite( |
221 |
|
ForkJoinPoolTest.suite(), |
222 |
|
ForkJoinTaskTest.suite(), |
223 |
|
RecursiveActionTest.suite(), |
282 |
|
TreeSetTest.suite(), |
283 |
|
TreeSubMapTest.suite(), |
284 |
|
TreeSubSetTest.suite()); |
285 |
+ |
|
286 |
+ |
// Java8+ test classes |
287 |
+ |
if (atLeastJava8()) { |
288 |
+ |
String[] java8TestClassNames = { |
289 |
+ |
"CompletableFutureTest", |
290 |
+ |
"ConcurrentHashMap8Test", |
291 |
+ |
"CountedCompleterTest", |
292 |
+ |
"DoubleAccumulatorTest", |
293 |
+ |
"DoubleAdderTest", |
294 |
+ |
"ForkJoinPool8Test", |
295 |
+ |
"LongAccumulatorTest", |
296 |
+ |
"LongAdderTest", |
297 |
+ |
"StampedLockTest", |
298 |
+ |
}; |
299 |
+ |
addNamedTestClasses(suite, java8TestClassNames); |
300 |
+ |
} |
301 |
+ |
|
302 |
+ |
return suite; |
303 |
|
} |
304 |
|
|
305 |
|
|
317 |
|
return 50; |
318 |
|
} |
319 |
|
|
261 |
– |
|
320 |
|
/** |
321 |
|
* Sets delays as multiples of SHORT_DELAY. |
322 |
|
*/ |
328 |
|
} |
329 |
|
|
330 |
|
/** |
331 |
+ |
* Returns a timeout in milliseconds to be used in tests that |
332 |
+ |
* verify that operations block or time out. |
333 |
+ |
*/ |
334 |
+ |
long timeoutMillis() { |
335 |
+ |
return SHORT_DELAY_MS / 4; |
336 |
+ |
} |
337 |
+ |
|
338 |
+ |
/** |
339 |
+ |
* Returns a new Date instance representing a time delayMillis |
340 |
+ |
* milliseconds in the future. |
341 |
+ |
*/ |
342 |
+ |
Date delayedDate(long delayMillis) { |
343 |
+ |
return new Date(System.currentTimeMillis() + delayMillis); |
344 |
+ |
} |
345 |
+ |
|
346 |
+ |
/** |
347 |
|
* The first exception encountered if any threadAssertXXX method fails. |
348 |
|
*/ |
349 |
|
private final AtomicReference<Throwable> threadFailure |
364 |
|
} |
365 |
|
|
366 |
|
/** |
367 |
+ |
* Extra checks that get done for all test cases. |
368 |
+ |
* |
369 |
|
* Triggers test case failure if any thread assertions have failed, |
370 |
|
* by rethrowing, in the test harness thread, any exception recorded |
371 |
|
* earlier by threadRecordFailure. |
372 |
+ |
* |
373 |
+ |
* Triggers test case failure if interrupt status is set in the main thread. |
374 |
|
*/ |
375 |
|
public void tearDown() throws Exception { |
376 |
|
Throwable t = threadFailure.getAndSet(null); |
388 |
|
throw afe; |
389 |
|
} |
390 |
|
} |
391 |
+ |
|
392 |
+ |
if (Thread.interrupted()) |
393 |
+ |
throw new AssertionFailedError("interrupt status set in main thread"); |
394 |
+ |
|
395 |
+ |
checkForkJoinPoolThreadLeaks(); |
396 |
+ |
} |
397 |
+ |
|
398 |
+ |
/** |
399 |
+ |
* Find missing try { ... } finally { joinPool(e); } |
400 |
+ |
*/ |
401 |
+ |
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
402 |
+ |
Thread[] survivors = new Thread[5]; |
403 |
+ |
int count = Thread.enumerate(survivors); |
404 |
+ |
for (int i = 0; i < count; i++) { |
405 |
+ |
Thread thread = survivors[i]; |
406 |
+ |
String name = thread.getName(); |
407 |
+ |
if (name.startsWith("ForkJoinPool-")) { |
408 |
+ |
// give thread some time to terminate |
409 |
+ |
thread.join(LONG_DELAY_MS); |
410 |
+ |
if (!thread.isAlive()) continue; |
411 |
+ |
thread.stop(); |
412 |
+ |
throw new AssertionFailedError |
413 |
+ |
(String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n", |
414 |
+ |
toString(), name)); |
415 |
+ |
} |
416 |
+ |
} |
417 |
|
} |
418 |
|
|
419 |
|
/** |
545 |
|
else { |
546 |
|
AssertionFailedError afe = |
547 |
|
new AssertionFailedError("unexpected exception: " + t); |
548 |
< |
t.initCause(t); |
548 |
> |
afe.initCause(t); |
549 |
|
throw afe; |
550 |
|
} |
551 |
|
} |
552 |
|
|
553 |
|
/** |
554 |
< |
* Delays, via Thread.sleep for the given millisecond delay, but |
554 |
> |
* Delays, via Thread.sleep, for the given millisecond delay, but |
555 |
|
* if the sleep is shorter than specified, may re-sleep or yield |
556 |
|
* until time elapses. |
557 |
|
*/ |
558 |
< |
public static void delay(long millis) throws InterruptedException { |
558 |
> |
static void delay(long millis) throws InterruptedException { |
559 |
|
long startTime = System.nanoTime(); |
560 |
|
long ns = millis * 1000 * 1000; |
561 |
|
for (;;) { |
574 |
|
/** |
575 |
|
* Waits out termination of a thread pool or fails doing so. |
576 |
|
*/ |
577 |
< |
public void joinPool(ExecutorService exec) { |
577 |
> |
void joinPool(ExecutorService exec) { |
578 |
|
try { |
579 |
|
exec.shutdown(); |
580 |
|
assertTrue("ExecutorService did not terminate in a timely manner", |
587 |
|
} |
588 |
|
|
589 |
|
/** |
590 |
+ |
* A debugging tool to print all stack traces, as jstack does. |
591 |
+ |
*/ |
592 |
+ |
static void printAllStackTraces() { |
593 |
+ |
for (ThreadInfo info : |
594 |
+ |
ManagementFactory.getThreadMXBean() |
595 |
+ |
.dumpAllThreads(true, true)) |
596 |
+ |
System.err.print(info); |
597 |
+ |
} |
598 |
+ |
|
599 |
+ |
/** |
600 |
+ |
* Checks that thread does not terminate within the default |
601 |
+ |
* millisecond delay of {@code timeoutMillis()}. |
602 |
+ |
*/ |
603 |
+ |
void assertThreadStaysAlive(Thread thread) { |
604 |
+ |
assertThreadStaysAlive(thread, timeoutMillis()); |
605 |
+ |
} |
606 |
+ |
|
607 |
+ |
/** |
608 |
|
* Checks that thread does not terminate within the given millisecond delay. |
609 |
|
*/ |
610 |
< |
public void assertThreadStaysAlive(Thread thread, long millis) { |
610 |
> |
void assertThreadStaysAlive(Thread thread, long millis) { |
611 |
|
try { |
612 |
|
// No need to optimize the failing case via Thread.join. |
613 |
|
delay(millis); |
618 |
|
} |
619 |
|
|
620 |
|
/** |
621 |
+ |
* Checks that the threads do not terminate within the default |
622 |
+ |
* millisecond delay of {@code timeoutMillis()}. |
623 |
+ |
*/ |
624 |
+ |
void assertThreadsStayAlive(Thread... threads) { |
625 |
+ |
assertThreadsStayAlive(timeoutMillis(), threads); |
626 |
+ |
} |
627 |
+ |
|
628 |
+ |
/** |
629 |
+ |
* Checks that the threads do not terminate within the given millisecond delay. |
630 |
+ |
*/ |
631 |
+ |
void assertThreadsStayAlive(long millis, Thread... threads) { |
632 |
+ |
try { |
633 |
+ |
// No need to optimize the failing case via Thread.join. |
634 |
+ |
delay(millis); |
635 |
+ |
for (Thread thread : threads) |
636 |
+ |
assertTrue(thread.isAlive()); |
637 |
+ |
} catch (InterruptedException ie) { |
638 |
+ |
fail("Unexpected InterruptedException"); |
639 |
+ |
} |
640 |
+ |
} |
641 |
+ |
|
642 |
+ |
/** |
643 |
+ |
* Checks that future.get times out, with the default timeout of |
644 |
+ |
* {@code timeoutMillis()}. |
645 |
+ |
*/ |
646 |
+ |
void assertFutureTimesOut(Future future) { |
647 |
+ |
assertFutureTimesOut(future, timeoutMillis()); |
648 |
+ |
} |
649 |
+ |
|
650 |
+ |
/** |
651 |
+ |
* Checks that future.get times out, with the given millisecond timeout. |
652 |
+ |
*/ |
653 |
+ |
void assertFutureTimesOut(Future future, long timeoutMillis) { |
654 |
+ |
long startTime = System.nanoTime(); |
655 |
+ |
try { |
656 |
+ |
future.get(timeoutMillis, MILLISECONDS); |
657 |
+ |
shouldThrow(); |
658 |
+ |
} catch (TimeoutException success) { |
659 |
+ |
} catch (Exception e) { |
660 |
+ |
threadUnexpectedException(e); |
661 |
+ |
} finally { future.cancel(true); } |
662 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
663 |
+ |
} |
664 |
+ |
|
665 |
+ |
/** |
666 |
|
* Fails with message "should throw exception". |
667 |
|
*/ |
668 |
|
public void shouldThrow() { |
713 |
|
SecurityManager sm = System.getSecurityManager(); |
714 |
|
if (sm == null) { |
715 |
|
r.run(); |
716 |
+ |
} |
717 |
+ |
runWithSecurityManagerWithPermissions(r, permissions); |
718 |
+ |
} |
719 |
+ |
|
720 |
+ |
/** |
721 |
+ |
* Runs Runnable r with a security policy that permits precisely |
722 |
+ |
* the specified permissions. If there is no current security |
723 |
+ |
* manager, a temporary one is set for the duration of the |
724 |
+ |
* Runnable. We require that any security manager permit |
725 |
+ |
* getPolicy/setPolicy. |
726 |
+ |
*/ |
727 |
+ |
public void runWithSecurityManagerWithPermissions(Runnable r, |
728 |
+ |
Permission... permissions) { |
729 |
+ |
SecurityManager sm = System.getSecurityManager(); |
730 |
+ |
if (sm == null) { |
731 |
|
Policy savedPolicy = Policy.getPolicy(); |
732 |
|
try { |
733 |
|
Policy.setPolicy(permissivePolicy()); |
734 |
|
System.setSecurityManager(new SecurityManager()); |
735 |
< |
runWithPermissions(r, permissions); |
735 |
> |
runWithSecurityManagerWithPermissions(r, permissions); |
736 |
|
} finally { |
737 |
|
System.setSecurityManager(null); |
738 |
|
Policy.setPolicy(savedPolicy); |
780 |
|
return perms.implies(p); |
781 |
|
} |
782 |
|
public void refresh() {} |
783 |
+ |
public String toString() { |
784 |
+ |
List<Permission> ps = new ArrayList<Permission>(); |
785 |
+ |
for (Enumeration<Permission> e = perms.elements(); e.hasMoreElements();) |
786 |
+ |
ps.add(e.nextElement()); |
787 |
+ |
return "AdjustablePolicy with permissions " + ps; |
788 |
+ |
} |
789 |
|
} |
790 |
|
|
791 |
|
/** |
823 |
|
} |
824 |
|
|
825 |
|
/** |
826 |
< |
* Sleeps until the timeout has elapsed, or interrupted. |
639 |
< |
* Does <em>NOT</em> throw InterruptedException. |
640 |
< |
*/ |
641 |
< |
void sleepTillInterrupted(long timeoutMillis) { |
642 |
< |
try { |
643 |
< |
Thread.sleep(timeoutMillis); |
644 |
< |
} catch (InterruptedException wakeup) {} |
645 |
< |
} |
646 |
< |
|
647 |
< |
/** |
648 |
< |
* Waits up to the specified number of milliseconds for the given |
826 |
> |
* Spin-waits up to the specified number of milliseconds for the given |
827 |
|
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
828 |
|
*/ |
829 |
|
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
830 |
< |
long timeoutNanos = timeoutMillis * 1000L * 1000L; |
653 |
< |
long t0 = System.nanoTime(); |
830 |
> |
long startTime = System.nanoTime(); |
831 |
|
for (;;) { |
832 |
|
Thread.State s = thread.getState(); |
833 |
|
if (s == Thread.State.BLOCKED || |
836 |
|
return; |
837 |
|
else if (s == Thread.State.TERMINATED) |
838 |
|
fail("Unexpected thread termination"); |
839 |
< |
else if (System.nanoTime() - t0 > timeoutNanos) { |
839 |
> |
else if (millisElapsedSince(startTime) > timeoutMillis) { |
840 |
|
threadAssertTrue(thread.isAlive()); |
841 |
|
return; |
842 |
|
} |
882 |
|
} catch (InterruptedException ie) { |
883 |
|
threadUnexpectedException(ie); |
884 |
|
} finally { |
885 |
< |
if (t.isAlive()) { |
885 |
> |
if (t.getState() != Thread.State.TERMINATED) { |
886 |
|
t.interrupt(); |
887 |
|
fail("Test timed out"); |
888 |
|
} |
960 |
|
realRun(); |
961 |
|
threadShouldThrow("InterruptedException"); |
962 |
|
} catch (InterruptedException success) { |
963 |
+ |
threadAssertFalse(Thread.interrupted()); |
964 |
|
} catch (Throwable t) { |
965 |
|
threadUnexpectedException(t); |
966 |
|
} |
990 |
|
threadShouldThrow("InterruptedException"); |
991 |
|
return result; |
992 |
|
} catch (InterruptedException success) { |
993 |
+ |
threadAssertFalse(Thread.interrupted()); |
994 |
|
} catch (Throwable t) { |
995 |
|
threadUnexpectedException(t); |
996 |
|
} |
1037 |
|
} |
1038 |
|
} |
1039 |
|
|
1040 |
+ |
public void await(Semaphore semaphore) { |
1041 |
+ |
try { |
1042 |
+ |
assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
1043 |
+ |
} catch (Throwable t) { |
1044 |
+ |
threadUnexpectedException(t); |
1045 |
+ |
} |
1046 |
+ |
} |
1047 |
+ |
|
1048 |
+ |
// /** |
1049 |
+ |
// * Spin-waits up to LONG_DELAY_MS until flag becomes true. |
1050 |
+ |
// */ |
1051 |
+ |
// public void await(AtomicBoolean flag) { |
1052 |
+ |
// await(flag, LONG_DELAY_MS); |
1053 |
+ |
// } |
1054 |
+ |
|
1055 |
+ |
// /** |
1056 |
+ |
// * Spin-waits up to the specified timeout until flag becomes true. |
1057 |
+ |
// */ |
1058 |
+ |
// public void await(AtomicBoolean flag, long timeoutMillis) { |
1059 |
+ |
// long startTime = System.nanoTime(); |
1060 |
+ |
// while (!flag.get()) { |
1061 |
+ |
// if (millisElapsedSince(startTime) > timeoutMillis) |
1062 |
+ |
// throw new AssertionFailedError("timed out"); |
1063 |
+ |
// Thread.yield(); |
1064 |
+ |
// } |
1065 |
+ |
// } |
1066 |
+ |
|
1067 |
|
public static class NPETask implements Callable<String> { |
1068 |
|
public String call() { throw new NullPointerException(); } |
1069 |
|
} |
1266 |
|
} |
1267 |
|
|
1268 |
|
/** |
1269 |
< |
* A CyclicBarrier that fails with AssertionFailedErrors instead |
1270 |
< |
* of throwing checked exceptions. |
1269 |
> |
* A CyclicBarrier that uses timed await and fails with |
1270 |
> |
* AssertionFailedErrors instead of throwing checked exceptions. |
1271 |
|
*/ |
1272 |
|
public class CheckedBarrier extends CyclicBarrier { |
1273 |
|
public CheckedBarrier(int parties) { super(parties); } |
1274 |
|
|
1275 |
|
public int await() { |
1276 |
|
try { |
1277 |
< |
return super.await(); |
1277 |
> |
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1278 |
> |
} catch (TimeoutException e) { |
1279 |
> |
throw new AssertionFailedError("timed out"); |
1280 |
|
} catch (Exception e) { |
1281 |
|
AssertionFailedError afe = |
1282 |
|
new AssertionFailedError("Unexpected exception: " + e); |
1286 |
|
} |
1287 |
|
} |
1288 |
|
|
1289 |
< |
public void checkEmpty(BlockingQueue q) { |
1289 |
> |
void checkEmpty(BlockingQueue q) { |
1290 |
|
try { |
1291 |
|
assertTrue(q.isEmpty()); |
1292 |
|
assertEquals(0, q.size()); |
1313 |
|
} |
1314 |
|
} |
1315 |
|
|
1316 |
< |
@SuppressWarnings("unchecked") |
1317 |
< |
public <T> T serialClone(T o) { |
1316 |
> |
void assertSerialEquals(Object x, Object y) { |
1317 |
> |
assertTrue(Arrays.equals(serialBytes(x), serialBytes(y))); |
1318 |
> |
} |
1319 |
> |
|
1320 |
> |
void assertNotSerialEquals(Object x, Object y) { |
1321 |
> |
assertFalse(Arrays.equals(serialBytes(x), serialBytes(y))); |
1322 |
> |
} |
1323 |
> |
|
1324 |
> |
byte[] serialBytes(Object o) { |
1325 |
|
try { |
1326 |
|
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1327 |
|
ObjectOutputStream oos = new ObjectOutputStream(bos); |
1328 |
|
oos.writeObject(o); |
1329 |
|
oos.flush(); |
1330 |
|
oos.close(); |
1331 |
< |
ByteArrayInputStream bin = |
1332 |
< |
new ByteArrayInputStream(bos.toByteArray()); |
1333 |
< |
ObjectInputStream ois = new ObjectInputStream(bin); |
1334 |
< |
return (T) ois.readObject(); |
1331 |
> |
return bos.toByteArray(); |
1332 |
> |
} catch (Throwable t) { |
1333 |
> |
threadUnexpectedException(t); |
1334 |
> |
return new byte[0]; |
1335 |
> |
} |
1336 |
> |
} |
1337 |
> |
|
1338 |
> |
@SuppressWarnings("unchecked") |
1339 |
> |
<T> T serialClone(T o) { |
1340 |
> |
try { |
1341 |
> |
ObjectInputStream ois = new ObjectInputStream |
1342 |
> |
(new ByteArrayInputStream(serialBytes(o))); |
1343 |
> |
T clone = (T) ois.readObject(); |
1344 |
> |
assertSame(o.getClass(), clone.getClass()); |
1345 |
> |
return clone; |
1346 |
|
} catch (Throwable t) { |
1347 |
|
threadUnexpectedException(t); |
1348 |
|
return null; |