1 |
|
/* |
2 |
< |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
< |
* Expert Group and released to the public domain, as explained at |
2 |
> |
* Written by Doug Lea and Martin Buchholz with assistance from |
3 |
> |
* members of JCP JSR-166 Expert Group and released to the public |
4 |
> |
* domain, as explained at |
5 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
6 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
7 |
|
* Pat Fisher, Mike Judd. |
9 |
|
|
10 |
|
/* |
11 |
|
* @test |
12 |
< |
* @summary JSR-166 tck tests |
13 |
< |
* @modules java.management |
12 |
> |
* @summary JSR-166 tck tests, in a number of variations. |
13 |
> |
* The first is the conformance testing variant, |
14 |
> |
* while others also test implementation details. |
15 |
|
* @build * |
16 |
< |
* @run junit/othervm/timeout=1000 -Djsr166.testImplementationDetails=true JSR166TestCase |
17 |
< |
* @run junit/othervm/timeout=1000 -Djava.util.concurrent.ForkJoinPool.common.parallelism=0 -Djsr166.testImplementationDetails=true JSR166TestCase |
18 |
< |
* @run junit/othervm/timeout=1000 -Djava.util.concurrent.ForkJoinPool.common.parallelism=1 -Djava.util.secureRandomSeed=true JSR166TestCase |
16 |
> |
* @modules java.management |
17 |
> |
* @run junit/othervm/timeout=1000 JSR166TestCase |
18 |
> |
* @run junit/othervm/timeout=1000 |
19 |
> |
* --add-opens java.base/java.util.concurrent=ALL-UNNAMED |
20 |
> |
* --add-opens java.base/java.lang=ALL-UNNAMED |
21 |
> |
* -Djsr166.testImplementationDetails=true |
22 |
> |
* JSR166TestCase |
23 |
> |
* @run junit/othervm/timeout=1000 |
24 |
> |
* --add-opens java.base/java.util.concurrent=ALL-UNNAMED |
25 |
> |
* --add-opens java.base/java.lang=ALL-UNNAMED |
26 |
> |
* -Djsr166.testImplementationDetails=true |
27 |
> |
* -Djava.util.concurrent.ForkJoinPool.common.parallelism=0 |
28 |
> |
* JSR166TestCase |
29 |
> |
* @run junit/othervm/timeout=1000 |
30 |
> |
* --add-opens java.base/java.util.concurrent=ALL-UNNAMED |
31 |
> |
* --add-opens java.base/java.lang=ALL-UNNAMED |
32 |
> |
* -Djsr166.testImplementationDetails=true |
33 |
> |
* -Djava.util.concurrent.ForkJoinPool.common.parallelism=1 |
34 |
> |
* -Djava.util.secureRandomSeed=true |
35 |
> |
* JSR166TestCase |
36 |
> |
* @run junit/othervm/timeout=1000/policy=tck.policy |
37 |
> |
* --add-opens java.base/java.util.concurrent=ALL-UNNAMED |
38 |
> |
* --add-opens java.base/java.lang=ALL-UNNAMED |
39 |
> |
* -Djsr166.testImplementationDetails=true |
40 |
> |
* JSR166TestCase |
41 |
|
*/ |
42 |
|
|
43 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
54 |
|
import java.lang.reflect.Constructor; |
55 |
|
import java.lang.reflect.Method; |
56 |
|
import java.lang.reflect.Modifier; |
33 |
– |
import java.nio.file.Files; |
34 |
– |
import java.nio.file.Paths; |
57 |
|
import java.security.CodeSource; |
58 |
|
import java.security.Permission; |
59 |
|
import java.security.PermissionCollection; |
63 |
|
import java.security.SecurityPermission; |
64 |
|
import java.util.ArrayList; |
65 |
|
import java.util.Arrays; |
66 |
+ |
import java.util.Collection; |
67 |
|
import java.util.Collections; |
68 |
|
import java.util.Date; |
69 |
|
import java.util.Enumeration; |
76 |
|
import java.util.concurrent.CountDownLatch; |
77 |
|
import java.util.concurrent.CyclicBarrier; |
78 |
|
import java.util.concurrent.ExecutionException; |
79 |
+ |
import java.util.concurrent.Executor; |
80 |
|
import java.util.concurrent.Executors; |
81 |
|
import java.util.concurrent.ExecutorService; |
82 |
|
import java.util.concurrent.ForkJoinPool; |
83 |
|
import java.util.concurrent.Future; |
84 |
+ |
import java.util.concurrent.FutureTask; |
85 |
|
import java.util.concurrent.RecursiveAction; |
86 |
|
import java.util.concurrent.RecursiveTask; |
87 |
+ |
import java.util.concurrent.RejectedExecutionException; |
88 |
|
import java.util.concurrent.RejectedExecutionHandler; |
89 |
|
import java.util.concurrent.Semaphore; |
90 |
+ |
import java.util.concurrent.ScheduledExecutorService; |
91 |
+ |
import java.util.concurrent.ScheduledFuture; |
92 |
|
import java.util.concurrent.SynchronousQueue; |
93 |
|
import java.util.concurrent.ThreadFactory; |
94 |
|
import java.util.concurrent.ThreadLocalRandom; |
95 |
|
import java.util.concurrent.ThreadPoolExecutor; |
96 |
+ |
import java.util.concurrent.TimeUnit; |
97 |
|
import java.util.concurrent.TimeoutException; |
98 |
|
import java.util.concurrent.atomic.AtomicBoolean; |
99 |
|
import java.util.concurrent.atomic.AtomicReference; |
71 |
– |
import java.util.regex.Matcher; |
100 |
|
import java.util.regex.Pattern; |
101 |
|
|
102 |
|
import junit.framework.AssertionFailedError; |
306 |
|
|
307 |
|
// public static String cpuModel() { |
308 |
|
// try { |
309 |
< |
// Matcher matcher = Pattern.compile("model name\\s*: (.*)") |
309 |
> |
// java.util.regex.Matcher matcher |
310 |
> |
// = Pattern.compile("model name\\s*: (.*)") |
311 |
|
// .matcher(new String( |
312 |
< |
// Files.readAllBytes(Paths.get("/proc/cpuinfo")), "UTF-8")); |
312 |
> |
// java.nio.file.Files.readAllBytes( |
313 |
> |
// java.nio.file.Paths.get("/proc/cpuinfo")), "UTF-8")); |
314 |
|
// matcher.find(); |
315 |
|
// return matcher.group(1); |
316 |
|
// } catch (Exception ex) { return null; } |
529 |
|
TreeMapTest.suite(), |
530 |
|
TreeSetTest.suite(), |
531 |
|
TreeSubMapTest.suite(), |
532 |
< |
TreeSubSetTest.suite()); |
532 |
> |
TreeSubSetTest.suite(), |
533 |
> |
VectorTest.suite()); |
534 |
|
|
535 |
|
// Java8+ test classes |
536 |
|
if (atLeastJava8()) { |
544 |
|
"DoubleAdderTest", |
545 |
|
"ForkJoinPool8Test", |
546 |
|
"ForkJoinTask8Test", |
547 |
+ |
"LinkedBlockingDeque8Test", |
548 |
+ |
"LinkedBlockingQueue8Test", |
549 |
|
"LongAccumulatorTest", |
550 |
|
"LongAdderTest", |
551 |
|
"SplittableRandomTest", |
568 |
|
"AtomicReference9Test", |
569 |
|
"AtomicReferenceArray9Test", |
570 |
|
"ExecutorCompletionService9Test", |
571 |
+ |
"ForkJoinPool9Test", |
572 |
|
}; |
573 |
|
addNamedTestClasses(suite, java9TestClassNames); |
574 |
|
} |
579 |
|
/** Returns list of junit-style test method names in given class. */ |
580 |
|
public static ArrayList<String> testMethodNames(Class<?> testClass) { |
581 |
|
Method[] methods = testClass.getDeclaredMethods(); |
582 |
< |
ArrayList<String> names = new ArrayList<String>(methods.length); |
582 |
> |
ArrayList<String> names = new ArrayList<>(methods.length); |
583 |
|
for (Method method : methods) { |
584 |
|
if (method.getName().startsWith("test") |
585 |
|
&& Modifier.isPublic(method.getModifiers()) |
644 |
|
public static long MEDIUM_DELAY_MS; |
645 |
|
public static long LONG_DELAY_MS; |
646 |
|
|
647 |
+ |
private static final long RANDOM_TIMEOUT; |
648 |
+ |
private static final long RANDOM_EXPIRED_TIMEOUT; |
649 |
+ |
private static final TimeUnit RANDOM_TIMEUNIT; |
650 |
+ |
static { |
651 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
652 |
+ |
long[] timeouts = { Long.MIN_VALUE, -1, 0, 1, Long.MAX_VALUE }; |
653 |
+ |
RANDOM_TIMEOUT = timeouts[rnd.nextInt(timeouts.length)]; |
654 |
+ |
RANDOM_EXPIRED_TIMEOUT = timeouts[rnd.nextInt(3)]; |
655 |
+ |
TimeUnit[] timeUnits = TimeUnit.values(); |
656 |
+ |
RANDOM_TIMEUNIT = timeUnits[rnd.nextInt(timeUnits.length)]; |
657 |
+ |
} |
658 |
+ |
|
659 |
+ |
/** |
660 |
+ |
* Returns a timeout for use when any value at all will do. |
661 |
+ |
*/ |
662 |
+ |
static long randomTimeout() { return RANDOM_TIMEOUT; } |
663 |
+ |
|
664 |
+ |
/** |
665 |
+ |
* Returns a timeout that means "no waiting", i.e. not positive. |
666 |
+ |
*/ |
667 |
+ |
static long randomExpiredTimeout() { return RANDOM_EXPIRED_TIMEOUT; } |
668 |
+ |
|
669 |
+ |
/** |
670 |
+ |
* Returns a random non-null TimeUnit. |
671 |
+ |
*/ |
672 |
+ |
static TimeUnit randomTimeUnit() { return RANDOM_TIMEUNIT; } |
673 |
+ |
|
674 |
|
/** |
675 |
|
* Returns the shortest timed delay. This can be scaled up for |
676 |
|
* slow machines using the jsr166.delay.factor system property, |
691 |
|
LONG_DELAY_MS = SHORT_DELAY_MS * 200; |
692 |
|
} |
693 |
|
|
694 |
+ |
private static final long TIMEOUT_DELAY_MS |
695 |
+ |
= (long) (12.0 * Math.cbrt(delayFactor)); |
696 |
+ |
|
697 |
|
/** |
698 |
< |
* Returns a timeout in milliseconds to be used in tests that |
699 |
< |
* verify that operations block or time out. |
698 |
> |
* Returns a timeout in milliseconds to be used in tests that verify |
699 |
> |
* that operations block or time out. We want this to be longer |
700 |
> |
* than the OS scheduling quantum, but not too long, so don't scale |
701 |
> |
* linearly with delayFactor; we use "crazy" cube root instead. |
702 |
|
*/ |
703 |
< |
long timeoutMillis() { |
704 |
< |
return SHORT_DELAY_MS / 4; |
703 |
> |
static long timeoutMillis() { |
704 |
> |
return TIMEOUT_DELAY_MS; |
705 |
|
} |
706 |
|
|
707 |
|
/** |
717 |
|
* The first exception encountered if any threadAssertXXX method fails. |
718 |
|
*/ |
719 |
|
private final AtomicReference<Throwable> threadFailure |
720 |
< |
= new AtomicReference<Throwable>(null); |
720 |
> |
= new AtomicReference<>(null); |
721 |
|
|
722 |
|
/** |
723 |
|
* Records an exception so that it can be rethrown later in the test |
1096 |
|
} |
1097 |
|
|
1098 |
|
/** |
1099 |
< |
* Checks that thread does not terminate within the default |
1034 |
< |
* millisecond delay of {@code timeoutMillis()}. |
1035 |
< |
*/ |
1036 |
< |
void assertThreadStaysAlive(Thread thread) { |
1037 |
< |
assertThreadStaysAlive(thread, timeoutMillis()); |
1038 |
< |
} |
1039 |
< |
|
1040 |
< |
/** |
1041 |
< |
* Checks that thread does not terminate within the given millisecond delay. |
1099 |
> |
* Checks that thread eventually enters the expected blocked thread state. |
1100 |
|
*/ |
1101 |
< |
void assertThreadStaysAlive(Thread thread, long millis) { |
1102 |
< |
try { |
1103 |
< |
// No need to optimize the failing case via Thread.join. |
1104 |
< |
delay(millis); |
1105 |
< |
assertTrue(thread.isAlive()); |
1106 |
< |
} catch (InterruptedException fail) { |
1107 |
< |
threadFail("Unexpected InterruptedException"); |
1108 |
< |
} |
1109 |
< |
} |
1110 |
< |
|
1111 |
< |
/** |
1112 |
< |
* Checks that the threads do not terminate within the default |
1113 |
< |
* millisecond delay of {@code timeoutMillis()}. |
1056 |
< |
*/ |
1057 |
< |
void assertThreadsStayAlive(Thread... threads) { |
1058 |
< |
assertThreadsStayAlive(timeoutMillis(), threads); |
1059 |
< |
} |
1060 |
< |
|
1061 |
< |
/** |
1062 |
< |
* Checks that the threads do not terminate within the given millisecond delay. |
1063 |
< |
*/ |
1064 |
< |
void assertThreadsStayAlive(long millis, Thread... threads) { |
1065 |
< |
try { |
1066 |
< |
// No need to optimize the failing case via Thread.join. |
1067 |
< |
delay(millis); |
1068 |
< |
for (Thread thread : threads) |
1069 |
< |
assertTrue(thread.isAlive()); |
1070 |
< |
} catch (InterruptedException fail) { |
1071 |
< |
threadFail("Unexpected InterruptedException"); |
1101 |
> |
void assertThreadBlocks(Thread thread, Thread.State expected) { |
1102 |
> |
// always sleep at least 1 ms, with high probability avoiding |
1103 |
> |
// transitory states |
1104 |
> |
for (long retries = LONG_DELAY_MS * 3 / 4; retries-->0; ) { |
1105 |
> |
try { delay(1); } |
1106 |
> |
catch (InterruptedException fail) { |
1107 |
> |
fail("Unexpected InterruptedException"); |
1108 |
> |
} |
1109 |
> |
Thread.State s = thread.getState(); |
1110 |
> |
if (s == expected) |
1111 |
> |
return; |
1112 |
> |
else if (s == Thread.State.TERMINATED) |
1113 |
> |
fail("Unexpected thread termination"); |
1114 |
|
} |
1115 |
+ |
fail("timed out waiting for thread to enter thread state " + expected); |
1116 |
|
} |
1117 |
|
|
1118 |
|
/** |
1153 |
|
} |
1154 |
|
|
1155 |
|
/** |
1156 |
+ |
* The maximum number of consecutive spurious wakeups we should |
1157 |
+ |
* tolerate (from APIs like LockSupport.park) before failing a test. |
1158 |
+ |
*/ |
1159 |
+ |
static final int MAX_SPURIOUS_WAKEUPS = 10; |
1160 |
+ |
|
1161 |
+ |
/** |
1162 |
|
* The number of elements to place in collections, arrays, etc. |
1163 |
|
*/ |
1164 |
|
public static final int SIZE = 20; |
1262 |
|
} |
1263 |
|
public void refresh() {} |
1264 |
|
public String toString() { |
1265 |
< |
List<Permission> ps = new ArrayList<Permission>(); |
1265 |
> |
List<Permission> ps = new ArrayList<>(); |
1266 |
|
for (Enumeration<Permission> e = perms.elements(); e.hasMoreElements();) |
1267 |
|
ps.add(e.nextElement()); |
1268 |
|
return "AdjustablePolicy with permissions " + ps; |
1321 |
|
startTime = System.nanoTime(); |
1322 |
|
else if (millisElapsedSince(startTime) > timeoutMillis) { |
1323 |
|
threadAssertTrue(thread.isAlive()); |
1324 |
< |
return; |
1324 |
> |
fail("timed out waiting for thread to enter wait state"); |
1325 |
|
} |
1326 |
|
Thread.yield(); |
1327 |
|
} |
1328 |
|
} |
1329 |
|
|
1330 |
|
/** |
1331 |
< |
* Waits up to LONG_DELAY_MS for the given thread to enter a wait |
1332 |
< |
* state: BLOCKED, WAITING, or TIMED_WAITING. |
1331 |
> |
* Spin-waits up to the specified number of milliseconds for the given |
1332 |
> |
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING, |
1333 |
> |
* and additionally satisfy the given condition. |
1334 |
> |
*/ |
1335 |
> |
void waitForThreadToEnterWaitState( |
1336 |
> |
Thread thread, long timeoutMillis, Callable<Boolean> waitingForGodot) { |
1337 |
> |
long startTime = 0L; |
1338 |
> |
for (;;) { |
1339 |
> |
Thread.State s = thread.getState(); |
1340 |
> |
if (s == Thread.State.BLOCKED || |
1341 |
> |
s == Thread.State.WAITING || |
1342 |
> |
s == Thread.State.TIMED_WAITING) { |
1343 |
> |
try { |
1344 |
> |
if (waitingForGodot.call()) |
1345 |
> |
return; |
1346 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
1347 |
> |
} |
1348 |
> |
else if (s == Thread.State.TERMINATED) |
1349 |
> |
fail("Unexpected thread termination"); |
1350 |
> |
else if (startTime == 0L) |
1351 |
> |
startTime = System.nanoTime(); |
1352 |
> |
else if (millisElapsedSince(startTime) > timeoutMillis) { |
1353 |
> |
threadAssertTrue(thread.isAlive()); |
1354 |
> |
fail("timed out waiting for thread to enter wait state"); |
1355 |
> |
} |
1356 |
> |
Thread.yield(); |
1357 |
> |
} |
1358 |
> |
} |
1359 |
> |
|
1360 |
> |
/** |
1361 |
> |
* Spin-waits up to LONG_DELAY_MS milliseconds for the given thread to |
1362 |
> |
* enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
1363 |
|
*/ |
1364 |
|
void waitForThreadToEnterWaitState(Thread thread) { |
1365 |
|
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS); |
1366 |
|
} |
1367 |
|
|
1368 |
|
/** |
1369 |
+ |
* Spin-waits up to LONG_DELAY_MS milliseconds for the given thread to |
1370 |
+ |
* enter a wait state: BLOCKED, WAITING, or TIMED_WAITING, |
1371 |
+ |
* and additionally satisfy the given condition. |
1372 |
+ |
*/ |
1373 |
+ |
void waitForThreadToEnterWaitState( |
1374 |
+ |
Thread thread, Callable<Boolean> waitingForGodot) { |
1375 |
+ |
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS, waitingForGodot); |
1376 |
+ |
} |
1377 |
+ |
|
1378 |
+ |
/** |
1379 |
|
* Returns the number of milliseconds since time given by |
1380 |
|
* startNanoTime, which must have been previously returned from a |
1381 |
|
* call to {@link System#nanoTime()}. |
1627 |
|
} |
1628 |
|
} |
1629 |
|
|
1630 |
+ |
public void await(CyclicBarrier barrier) { |
1631 |
+ |
try { |
1632 |
+ |
barrier.await(LONG_DELAY_MS, MILLISECONDS); |
1633 |
+ |
} catch (Throwable fail) { |
1634 |
+ |
threadUnexpectedException(fail); |
1635 |
+ |
} |
1636 |
+ |
} |
1637 |
+ |
|
1638 |
|
// /** |
1639 |
|
// * Spin-waits up to LONG_DELAY_MS until flag becomes true. |
1640 |
|
// */ |
1658 |
|
public String call() { throw new NullPointerException(); } |
1659 |
|
} |
1660 |
|
|
1564 |
– |
public static class CallableOne implements Callable<Integer> { |
1565 |
– |
public Integer call() { return one; } |
1566 |
– |
} |
1567 |
– |
|
1568 |
– |
public class ShortRunnable extends CheckedRunnable { |
1569 |
– |
protected void realRun() throws Throwable { |
1570 |
– |
delay(SHORT_DELAY_MS); |
1571 |
– |
} |
1572 |
– |
} |
1573 |
– |
|
1574 |
– |
public class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
1575 |
– |
protected void realRun() throws InterruptedException { |
1576 |
– |
delay(SHORT_DELAY_MS); |
1577 |
– |
} |
1578 |
– |
} |
1579 |
– |
|
1580 |
– |
public class SmallRunnable extends CheckedRunnable { |
1581 |
– |
protected void realRun() throws Throwable { |
1582 |
– |
delay(SMALL_DELAY_MS); |
1583 |
– |
} |
1584 |
– |
} |
1585 |
– |
|
1661 |
|
public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
1662 |
|
protected void realRun() { |
1663 |
|
try { |
1666 |
|
} |
1667 |
|
} |
1668 |
|
|
1594 |
– |
public class SmallCallable extends CheckedCallable { |
1595 |
– |
protected Object realCall() throws InterruptedException { |
1596 |
– |
delay(SMALL_DELAY_MS); |
1597 |
– |
return Boolean.TRUE; |
1598 |
– |
} |
1599 |
– |
} |
1600 |
– |
|
1601 |
– |
public class MediumRunnable extends CheckedRunnable { |
1602 |
– |
protected void realRun() throws Throwable { |
1603 |
– |
delay(MEDIUM_DELAY_MS); |
1604 |
– |
} |
1605 |
– |
} |
1606 |
– |
|
1607 |
– |
public class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
1608 |
– |
protected void realRun() throws InterruptedException { |
1609 |
– |
delay(MEDIUM_DELAY_MS); |
1610 |
– |
} |
1611 |
– |
} |
1612 |
– |
|
1669 |
|
public Runnable possiblyInterruptedRunnable(final long timeoutMillis) { |
1670 |
|
return new CheckedRunnable() { |
1671 |
|
protected void realRun() { |
1675 |
|
}}; |
1676 |
|
} |
1677 |
|
|
1622 |
– |
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
1623 |
– |
protected void realRun() { |
1624 |
– |
try { |
1625 |
– |
delay(MEDIUM_DELAY_MS); |
1626 |
– |
} catch (InterruptedException ok) {} |
1627 |
– |
} |
1628 |
– |
} |
1629 |
– |
|
1630 |
– |
public class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
1631 |
– |
protected void realRun() { |
1632 |
– |
try { |
1633 |
– |
delay(LONG_DELAY_MS); |
1634 |
– |
} catch (InterruptedException ok) {} |
1635 |
– |
} |
1636 |
– |
} |
1637 |
– |
|
1678 |
|
/** |
1679 |
|
* For use as ThreadFactory in constructors |
1680 |
|
*/ |
1688 |
|
boolean isDone(); |
1689 |
|
} |
1690 |
|
|
1651 |
– |
public static TrackedRunnable trackedRunnable(final long timeoutMillis) { |
1652 |
– |
return new TrackedRunnable() { |
1653 |
– |
private volatile boolean done = false; |
1654 |
– |
public boolean isDone() { return done; } |
1655 |
– |
public void run() { |
1656 |
– |
try { |
1657 |
– |
delay(timeoutMillis); |
1658 |
– |
done = true; |
1659 |
– |
} catch (InterruptedException ok) {} |
1660 |
– |
} |
1661 |
– |
}; |
1662 |
– |
} |
1663 |
– |
|
1664 |
– |
public static class TrackedShortRunnable implements Runnable { |
1665 |
– |
public volatile boolean done = false; |
1666 |
– |
public void run() { |
1667 |
– |
try { |
1668 |
– |
delay(SHORT_DELAY_MS); |
1669 |
– |
done = true; |
1670 |
– |
} catch (InterruptedException ok) {} |
1671 |
– |
} |
1672 |
– |
} |
1673 |
– |
|
1674 |
– |
public static class TrackedSmallRunnable implements Runnable { |
1675 |
– |
public volatile boolean done = false; |
1676 |
– |
public void run() { |
1677 |
– |
try { |
1678 |
– |
delay(SMALL_DELAY_MS); |
1679 |
– |
done = true; |
1680 |
– |
} catch (InterruptedException ok) {} |
1681 |
– |
} |
1682 |
– |
} |
1683 |
– |
|
1684 |
– |
public static class TrackedMediumRunnable implements Runnable { |
1685 |
– |
public volatile boolean done = false; |
1686 |
– |
public void run() { |
1687 |
– |
try { |
1688 |
– |
delay(MEDIUM_DELAY_MS); |
1689 |
– |
done = true; |
1690 |
– |
} catch (InterruptedException ok) {} |
1691 |
– |
} |
1692 |
– |
} |
1693 |
– |
|
1694 |
– |
public static class TrackedLongRunnable implements Runnable { |
1695 |
– |
public volatile boolean done = false; |
1696 |
– |
public void run() { |
1697 |
– |
try { |
1698 |
– |
delay(LONG_DELAY_MS); |
1699 |
– |
done = true; |
1700 |
– |
} catch (InterruptedException ok) {} |
1701 |
– |
} |
1702 |
– |
} |
1703 |
– |
|
1691 |
|
public static class TrackedNoOpRunnable implements Runnable { |
1692 |
|
public volatile boolean done = false; |
1693 |
|
public void run() { |
1695 |
|
} |
1696 |
|
} |
1697 |
|
|
1711 |
– |
public static class TrackedCallable implements Callable { |
1712 |
– |
public volatile boolean done = false; |
1713 |
– |
public Object call() { |
1714 |
– |
try { |
1715 |
– |
delay(SMALL_DELAY_MS); |
1716 |
– |
done = true; |
1717 |
– |
} catch (InterruptedException ok) {} |
1718 |
– |
return Boolean.TRUE; |
1719 |
– |
} |
1720 |
– |
} |
1721 |
– |
|
1698 |
|
/** |
1699 |
|
* Analog of CheckedRunnable for RecursiveAction |
1700 |
|
*/ |
1761 |
|
assertEquals(0, q.size()); |
1762 |
|
assertNull(q.peek()); |
1763 |
|
assertNull(q.poll()); |
1764 |
< |
assertNull(q.poll(0, MILLISECONDS)); |
1764 |
> |
assertNull(q.poll(randomExpiredTimeout(), randomTimeUnit())); |
1765 |
|
assertEquals(q.toString(), "[]"); |
1766 |
|
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
1767 |
|
assertFalse(q.iterator().hasNext()); |
1802 |
|
} |
1803 |
|
} |
1804 |
|
|
1805 |
+ |
void assertImmutable(final Object o) { |
1806 |
+ |
if (o instanceof Collection) { |
1807 |
+ |
assertThrows( |
1808 |
+ |
UnsupportedOperationException.class, |
1809 |
+ |
new Runnable() { public void run() { |
1810 |
+ |
((Collection) o).add(null);}}); |
1811 |
+ |
} |
1812 |
+ |
} |
1813 |
+ |
|
1814 |
|
@SuppressWarnings("unchecked") |
1815 |
|
<T> T serialClone(T o) { |
1816 |
|
try { |
1817 |
|
ObjectInputStream ois = new ObjectInputStream |
1818 |
|
(new ByteArrayInputStream(serialBytes(o))); |
1819 |
|
T clone = (T) ois.readObject(); |
1820 |
+ |
if (o == clone) assertImmutable(o); |
1821 |
|
assertSame(o.getClass(), clone.getClass()); |
1822 |
|
return clone; |
1823 |
|
} catch (Throwable fail) { |
1826 |
|
} |
1827 |
|
} |
1828 |
|
|
1829 |
+ |
/** |
1830 |
+ |
* A version of serialClone that leaves error handling (for |
1831 |
+ |
* e.g. NotSerializableException) up to the caller. |
1832 |
+ |
*/ |
1833 |
+ |
@SuppressWarnings("unchecked") |
1834 |
+ |
<T> T serialClonePossiblyFailing(T o) |
1835 |
+ |
throws ReflectiveOperationException, java.io.IOException { |
1836 |
+ |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1837 |
+ |
ObjectOutputStream oos = new ObjectOutputStream(bos); |
1838 |
+ |
oos.writeObject(o); |
1839 |
+ |
oos.flush(); |
1840 |
+ |
oos.close(); |
1841 |
+ |
ObjectInputStream ois = new ObjectInputStream |
1842 |
+ |
(new ByteArrayInputStream(bos.toByteArray())); |
1843 |
+ |
T clone = (T) ois.readObject(); |
1844 |
+ |
if (o == clone) assertImmutable(o); |
1845 |
+ |
assertSame(o.getClass(), clone.getClass()); |
1846 |
+ |
return clone; |
1847 |
+ |
} |
1848 |
+ |
|
1849 |
+ |
/** |
1850 |
+ |
* If o implements Cloneable and has a public clone method, |
1851 |
+ |
* returns a clone of o, else null. |
1852 |
+ |
*/ |
1853 |
+ |
@SuppressWarnings("unchecked") |
1854 |
+ |
<T> T cloneableClone(T o) { |
1855 |
+ |
if (!(o instanceof Cloneable)) return null; |
1856 |
+ |
final T clone; |
1857 |
+ |
try { |
1858 |
+ |
clone = (T) o.getClass().getMethod("clone").invoke(o); |
1859 |
+ |
} catch (NoSuchMethodException ok) { |
1860 |
+ |
return null; |
1861 |
+ |
} catch (ReflectiveOperationException unexpected) { |
1862 |
+ |
throw new Error(unexpected); |
1863 |
+ |
} |
1864 |
+ |
assertNotSame(o, clone); // not 100% guaranteed by spec |
1865 |
+ |
assertSame(o.getClass(), clone.getClass()); |
1866 |
+ |
return clone; |
1867 |
+ |
} |
1868 |
+ |
|
1869 |
|
public void assertThrows(Class<? extends Throwable> expectedExceptionClass, |
1870 |
|
Runnable... throwingActions) { |
1871 |
|
for (Runnable throwingAction : throwingActions) { |
1912 |
|
static <T> void shuffle(T[] array) { |
1913 |
|
Collections.shuffle(Arrays.asList(array), ThreadLocalRandom.current()); |
1914 |
|
} |
1915 |
+ |
|
1916 |
+ |
// --- Shared assertions for Executor tests --- |
1917 |
+ |
|
1918 |
+ |
/** |
1919 |
+ |
* Returns maximum number of tasks that can be submitted to given |
1920 |
+ |
* pool (with bounded queue) before saturation (when submission |
1921 |
+ |
* throws RejectedExecutionException). |
1922 |
+ |
*/ |
1923 |
+ |
static final int saturatedSize(ThreadPoolExecutor pool) { |
1924 |
+ |
BlockingQueue<Runnable> q = pool.getQueue(); |
1925 |
+ |
return pool.getMaximumPoolSize() + q.size() + q.remainingCapacity(); |
1926 |
+ |
} |
1927 |
+ |
|
1928 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
1929 |
+ |
void assertNullTaskSubmissionThrowsNullPointerException(Executor e) { |
1930 |
+ |
try { |
1931 |
+ |
e.execute((Runnable) null); |
1932 |
+ |
shouldThrow(); |
1933 |
+ |
} catch (NullPointerException success) {} |
1934 |
+ |
|
1935 |
+ |
if (! (e instanceof ExecutorService)) return; |
1936 |
+ |
ExecutorService es = (ExecutorService) e; |
1937 |
+ |
try { |
1938 |
+ |
es.submit((Runnable) null); |
1939 |
+ |
shouldThrow(); |
1940 |
+ |
} catch (NullPointerException success) {} |
1941 |
+ |
try { |
1942 |
+ |
es.submit((Runnable) null, Boolean.TRUE); |
1943 |
+ |
shouldThrow(); |
1944 |
+ |
} catch (NullPointerException success) {} |
1945 |
+ |
try { |
1946 |
+ |
es.submit((Callable) null); |
1947 |
+ |
shouldThrow(); |
1948 |
+ |
} catch (NullPointerException success) {} |
1949 |
+ |
|
1950 |
+ |
if (! (e instanceof ScheduledExecutorService)) return; |
1951 |
+ |
ScheduledExecutorService ses = (ScheduledExecutorService) e; |
1952 |
+ |
try { |
1953 |
+ |
ses.schedule((Runnable) null, |
1954 |
+ |
randomTimeout(), randomTimeUnit()); |
1955 |
+ |
shouldThrow(); |
1956 |
+ |
} catch (NullPointerException success) {} |
1957 |
+ |
try { |
1958 |
+ |
ses.schedule((Callable) null, |
1959 |
+ |
randomTimeout(), randomTimeUnit()); |
1960 |
+ |
shouldThrow(); |
1961 |
+ |
} catch (NullPointerException success) {} |
1962 |
+ |
try { |
1963 |
+ |
ses.scheduleAtFixedRate((Runnable) null, |
1964 |
+ |
randomTimeout(), LONG_DELAY_MS, MILLISECONDS); |
1965 |
+ |
shouldThrow(); |
1966 |
+ |
} catch (NullPointerException success) {} |
1967 |
+ |
try { |
1968 |
+ |
ses.scheduleWithFixedDelay((Runnable) null, |
1969 |
+ |
randomTimeout(), LONG_DELAY_MS, MILLISECONDS); |
1970 |
+ |
shouldThrow(); |
1971 |
+ |
} catch (NullPointerException success) {} |
1972 |
+ |
} |
1973 |
+ |
|
1974 |
+ |
void setRejectedExecutionHandler( |
1975 |
+ |
ThreadPoolExecutor p, RejectedExecutionHandler handler) { |
1976 |
+ |
p.setRejectedExecutionHandler(handler); |
1977 |
+ |
assertSame(handler, p.getRejectedExecutionHandler()); |
1978 |
+ |
} |
1979 |
+ |
|
1980 |
+ |
void assertTaskSubmissionsAreRejected(ThreadPoolExecutor p) { |
1981 |
+ |
final RejectedExecutionHandler savedHandler = p.getRejectedExecutionHandler(); |
1982 |
+ |
final long savedTaskCount = p.getTaskCount(); |
1983 |
+ |
final long savedCompletedTaskCount = p.getCompletedTaskCount(); |
1984 |
+ |
final int savedQueueSize = p.getQueue().size(); |
1985 |
+ |
final boolean stock = (p.getClass().getClassLoader() == null); |
1986 |
+ |
|
1987 |
+ |
Runnable r = () -> {}; |
1988 |
+ |
Callable<Boolean> c = () -> Boolean.TRUE; |
1989 |
+ |
|
1990 |
+ |
class Recorder implements RejectedExecutionHandler { |
1991 |
+ |
public volatile Runnable r = null; |
1992 |
+ |
public volatile ThreadPoolExecutor p = null; |
1993 |
+ |
public void reset() { r = null; p = null; } |
1994 |
+ |
public void rejectedExecution(Runnable r, ThreadPoolExecutor p) { |
1995 |
+ |
assertNull(this.r); |
1996 |
+ |
assertNull(this.p); |
1997 |
+ |
this.r = r; |
1998 |
+ |
this.p = p; |
1999 |
+ |
} |
2000 |
+ |
} |
2001 |
+ |
|
2002 |
+ |
// check custom handler is invoked exactly once per task |
2003 |
+ |
Recorder recorder = new Recorder(); |
2004 |
+ |
setRejectedExecutionHandler(p, recorder); |
2005 |
+ |
for (int i = 2; i--> 0; ) { |
2006 |
+ |
recorder.reset(); |
2007 |
+ |
p.execute(r); |
2008 |
+ |
if (stock && p.getClass() == ThreadPoolExecutor.class) |
2009 |
+ |
assertSame(r, recorder.r); |
2010 |
+ |
assertSame(p, recorder.p); |
2011 |
+ |
|
2012 |
+ |
recorder.reset(); |
2013 |
+ |
assertFalse(p.submit(r).isDone()); |
2014 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2015 |
+ |
assertSame(p, recorder.p); |
2016 |
+ |
|
2017 |
+ |
recorder.reset(); |
2018 |
+ |
assertFalse(p.submit(r, Boolean.TRUE).isDone()); |
2019 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2020 |
+ |
assertSame(p, recorder.p); |
2021 |
+ |
|
2022 |
+ |
recorder.reset(); |
2023 |
+ |
assertFalse(p.submit(c).isDone()); |
2024 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2025 |
+ |
assertSame(p, recorder.p); |
2026 |
+ |
|
2027 |
+ |
if (p instanceof ScheduledExecutorService) { |
2028 |
+ |
ScheduledExecutorService s = (ScheduledExecutorService) p; |
2029 |
+ |
ScheduledFuture<?> future; |
2030 |
+ |
|
2031 |
+ |
recorder.reset(); |
2032 |
+ |
future = s.schedule(r, randomTimeout(), randomTimeUnit()); |
2033 |
+ |
assertFalse(future.isDone()); |
2034 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2035 |
+ |
assertSame(p, recorder.p); |
2036 |
+ |
|
2037 |
+ |
recorder.reset(); |
2038 |
+ |
future = s.schedule(c, randomTimeout(), randomTimeUnit()); |
2039 |
+ |
assertFalse(future.isDone()); |
2040 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2041 |
+ |
assertSame(p, recorder.p); |
2042 |
+ |
|
2043 |
+ |
recorder.reset(); |
2044 |
+ |
future = s.scheduleAtFixedRate(r, randomTimeout(), LONG_DELAY_MS, MILLISECONDS); |
2045 |
+ |
assertFalse(future.isDone()); |
2046 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2047 |
+ |
assertSame(p, recorder.p); |
2048 |
+ |
|
2049 |
+ |
recorder.reset(); |
2050 |
+ |
future = s.scheduleWithFixedDelay(r, randomTimeout(), LONG_DELAY_MS, MILLISECONDS); |
2051 |
+ |
assertFalse(future.isDone()); |
2052 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2053 |
+ |
assertSame(p, recorder.p); |
2054 |
+ |
} |
2055 |
+ |
} |
2056 |
+ |
|
2057 |
+ |
// Checking our custom handler above should be sufficient, but |
2058 |
+ |
// we add some integration tests of standard handlers. |
2059 |
+ |
final AtomicReference<Thread> thread = new AtomicReference<>(); |
2060 |
+ |
final Runnable setThread = () -> thread.set(Thread.currentThread()); |
2061 |
+ |
|
2062 |
+ |
setRejectedExecutionHandler(p, new ThreadPoolExecutor.AbortPolicy()); |
2063 |
+ |
try { |
2064 |
+ |
p.execute(setThread); |
2065 |
+ |
shouldThrow(); |
2066 |
+ |
} catch (RejectedExecutionException success) {} |
2067 |
+ |
assertNull(thread.get()); |
2068 |
+ |
|
2069 |
+ |
setRejectedExecutionHandler(p, new ThreadPoolExecutor.DiscardPolicy()); |
2070 |
+ |
p.execute(setThread); |
2071 |
+ |
assertNull(thread.get()); |
2072 |
+ |
|
2073 |
+ |
setRejectedExecutionHandler(p, new ThreadPoolExecutor.CallerRunsPolicy()); |
2074 |
+ |
p.execute(setThread); |
2075 |
+ |
if (p.isShutdown()) |
2076 |
+ |
assertNull(thread.get()); |
2077 |
+ |
else |
2078 |
+ |
assertSame(Thread.currentThread(), thread.get()); |
2079 |
+ |
|
2080 |
+ |
setRejectedExecutionHandler(p, savedHandler); |
2081 |
+ |
|
2082 |
+ |
// check that pool was not perturbed by handlers |
2083 |
+ |
assertEquals(savedTaskCount, p.getTaskCount()); |
2084 |
+ |
assertEquals(savedCompletedTaskCount, p.getCompletedTaskCount()); |
2085 |
+ |
assertEquals(savedQueueSize, p.getQueue().size()); |
2086 |
+ |
} |
2087 |
|
} |