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 (conformance testing mode) |
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 |
|
* @modules java.management |
17 |
|
* @run junit/othervm/timeout=1000 JSR166TestCase |
15 |
– |
*/ |
16 |
– |
|
17 |
– |
/* |
18 |
– |
* @test |
19 |
– |
* @summary JSR-166 tck tests (whitebox tests allowed) |
20 |
– |
* @build * |
21 |
– |
* @modules java.base/java.util.concurrent:open |
22 |
– |
* java.base/java.lang:open |
23 |
– |
* java.management |
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 |
|
*/ |
54 |
|
import java.lang.reflect.Constructor; |
55 |
|
import java.lang.reflect.Method; |
56 |
|
import java.lang.reflect.Modifier; |
55 |
– |
import java.nio.file.Files; |
56 |
– |
import java.nio.file.Paths; |
57 |
|
import java.security.CodeSource; |
58 |
|
import java.security.Permission; |
59 |
|
import java.security.PermissionCollection; |
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; |
94 |
– |
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; } |
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 |
|
/** |
1096 |
|
} |
1097 |
|
|
1098 |
|
/** |
1099 |
< |
* Checks that thread does not terminate within the default |
1061 |
< |
* millisecond delay of {@code timeoutMillis()}. |
1099 |
> |
* Checks that thread eventually enters the expected blocked thread state. |
1100 |
|
*/ |
1101 |
< |
void assertThreadStaysAlive(Thread thread) { |
1102 |
< |
assertThreadStaysAlive(thread, timeoutMillis()); |
1103 |
< |
} |
1104 |
< |
|
1105 |
< |
/** |
1106 |
< |
* Checks that thread does not terminate within the given millisecond delay. |
1107 |
< |
*/ |
1108 |
< |
void assertThreadStaysAlive(Thread thread, long millis) { |
1109 |
< |
try { |
1110 |
< |
// No need to optimize the failing case via Thread.join. |
1111 |
< |
delay(millis); |
1112 |
< |
assertTrue(thread.isAlive()); |
1113 |
< |
} catch (InterruptedException fail) { |
1076 |
< |
threadFail("Unexpected InterruptedException"); |
1077 |
< |
} |
1078 |
< |
} |
1079 |
< |
|
1080 |
< |
/** |
1081 |
< |
* Checks that the threads do not terminate within the default |
1082 |
< |
* millisecond delay of {@code timeoutMillis()}. |
1083 |
< |
*/ |
1084 |
< |
void assertThreadsStayAlive(Thread... threads) { |
1085 |
< |
assertThreadsStayAlive(timeoutMillis(), threads); |
1086 |
< |
} |
1087 |
< |
|
1088 |
< |
/** |
1089 |
< |
* Checks that the threads do not terminate within the given millisecond delay. |
1090 |
< |
*/ |
1091 |
< |
void assertThreadsStayAlive(long millis, Thread... threads) { |
1092 |
< |
try { |
1093 |
< |
// No need to optimize the failing case via Thread.join. |
1094 |
< |
delay(millis); |
1095 |
< |
for (Thread thread : threads) |
1096 |
< |
assertTrue(thread.isAlive()); |
1097 |
< |
} catch (InterruptedException fail) { |
1098 |
< |
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; |
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 |
|
|
1631 |
– |
public static class CallableOne implements Callable<Integer> { |
1632 |
– |
public Integer call() { return one; } |
1633 |
– |
} |
1634 |
– |
|
1635 |
– |
public class ShortRunnable extends CheckedRunnable { |
1636 |
– |
protected void realRun() throws Throwable { |
1637 |
– |
delay(SHORT_DELAY_MS); |
1638 |
– |
} |
1639 |
– |
} |
1640 |
– |
|
1641 |
– |
public class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
1642 |
– |
protected void realRun() throws InterruptedException { |
1643 |
– |
delay(SHORT_DELAY_MS); |
1644 |
– |
} |
1645 |
– |
} |
1646 |
– |
|
1647 |
– |
public class SmallRunnable extends CheckedRunnable { |
1648 |
– |
protected void realRun() throws Throwable { |
1649 |
– |
delay(SMALL_DELAY_MS); |
1650 |
– |
} |
1651 |
– |
} |
1652 |
– |
|
1661 |
|
public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
1662 |
|
protected void realRun() { |
1663 |
|
try { |
1666 |
|
} |
1667 |
|
} |
1668 |
|
|
1661 |
– |
public class SmallCallable extends CheckedCallable { |
1662 |
– |
protected Object realCall() throws InterruptedException { |
1663 |
– |
delay(SMALL_DELAY_MS); |
1664 |
– |
return Boolean.TRUE; |
1665 |
– |
} |
1666 |
– |
} |
1667 |
– |
|
1668 |
– |
public class MediumRunnable extends CheckedRunnable { |
1669 |
– |
protected void realRun() throws Throwable { |
1670 |
– |
delay(MEDIUM_DELAY_MS); |
1671 |
– |
} |
1672 |
– |
} |
1673 |
– |
|
1674 |
– |
public class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
1675 |
– |
protected void realRun() throws InterruptedException { |
1676 |
– |
delay(MEDIUM_DELAY_MS); |
1677 |
– |
} |
1678 |
– |
} |
1679 |
– |
|
1669 |
|
public Runnable possiblyInterruptedRunnable(final long timeoutMillis) { |
1670 |
|
return new CheckedRunnable() { |
1671 |
|
protected void realRun() { |
1675 |
|
}}; |
1676 |
|
} |
1677 |
|
|
1689 |
– |
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
1690 |
– |
protected void realRun() { |
1691 |
– |
try { |
1692 |
– |
delay(MEDIUM_DELAY_MS); |
1693 |
– |
} catch (InterruptedException ok) {} |
1694 |
– |
} |
1695 |
– |
} |
1696 |
– |
|
1697 |
– |
public class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
1698 |
– |
protected void realRun() { |
1699 |
– |
try { |
1700 |
– |
delay(LONG_DELAY_MS); |
1701 |
– |
} catch (InterruptedException ok) {} |
1702 |
– |
} |
1703 |
– |
} |
1704 |
– |
|
1678 |
|
/** |
1679 |
|
* For use as ThreadFactory in constructors |
1680 |
|
*/ |
1688 |
|
boolean isDone(); |
1689 |
|
} |
1690 |
|
|
1718 |
– |
public static TrackedRunnable trackedRunnable(final long timeoutMillis) { |
1719 |
– |
return new TrackedRunnable() { |
1720 |
– |
private volatile boolean done = false; |
1721 |
– |
public boolean isDone() { return done; } |
1722 |
– |
public void run() { |
1723 |
– |
try { |
1724 |
– |
delay(timeoutMillis); |
1725 |
– |
done = true; |
1726 |
– |
} catch (InterruptedException ok) {} |
1727 |
– |
} |
1728 |
– |
}; |
1729 |
– |
} |
1730 |
– |
|
1731 |
– |
public static class TrackedShortRunnable implements Runnable { |
1732 |
– |
public volatile boolean done = false; |
1733 |
– |
public void run() { |
1734 |
– |
try { |
1735 |
– |
delay(SHORT_DELAY_MS); |
1736 |
– |
done = true; |
1737 |
– |
} catch (InterruptedException ok) {} |
1738 |
– |
} |
1739 |
– |
} |
1740 |
– |
|
1741 |
– |
public static class TrackedSmallRunnable implements Runnable { |
1742 |
– |
public volatile boolean done = false; |
1743 |
– |
public void run() { |
1744 |
– |
try { |
1745 |
– |
delay(SMALL_DELAY_MS); |
1746 |
– |
done = true; |
1747 |
– |
} catch (InterruptedException ok) {} |
1748 |
– |
} |
1749 |
– |
} |
1750 |
– |
|
1751 |
– |
public static class TrackedMediumRunnable implements Runnable { |
1752 |
– |
public volatile boolean done = false; |
1753 |
– |
public void run() { |
1754 |
– |
try { |
1755 |
– |
delay(MEDIUM_DELAY_MS); |
1756 |
– |
done = true; |
1757 |
– |
} catch (InterruptedException ok) {} |
1758 |
– |
} |
1759 |
– |
} |
1760 |
– |
|
1761 |
– |
public static class TrackedLongRunnable implements Runnable { |
1762 |
– |
public volatile boolean done = false; |
1763 |
– |
public void run() { |
1764 |
– |
try { |
1765 |
– |
delay(LONG_DELAY_MS); |
1766 |
– |
done = true; |
1767 |
– |
} catch (InterruptedException ok) {} |
1768 |
– |
} |
1769 |
– |
} |
1770 |
– |
|
1691 |
|
public static class TrackedNoOpRunnable implements Runnable { |
1692 |
|
public volatile boolean done = false; |
1693 |
|
public void run() { |
1695 |
|
} |
1696 |
|
} |
1697 |
|
|
1778 |
– |
public static class TrackedCallable implements Callable { |
1779 |
– |
public volatile boolean done = false; |
1780 |
– |
public Object call() { |
1781 |
– |
try { |
1782 |
– |
delay(SMALL_DELAY_MS); |
1783 |
– |
done = true; |
1784 |
– |
} catch (InterruptedException ok) {} |
1785 |
– |
return Boolean.TRUE; |
1786 |
– |
} |
1787 |
– |
} |
1788 |
– |
|
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()); |
1912 |
|
static <T> void shuffle(T[] array) { |
1913 |
|
Collections.shuffle(Arrays.asList(array), ThreadLocalRandom.current()); |
1914 |
|
} |
1915 |
+ |
|
1916 |
+ |
/** |
1917 |
+ |
* Returns the same String as would be returned by {@link |
1918 |
+ |
* Object#toString}, whether or not the given object's class |
1919 |
+ |
* overrides toString(). |
1920 |
+ |
* |
1921 |
+ |
* @see System#identityHashCode |
1922 |
+ |
*/ |
1923 |
+ |
static String identityString(Object x) { |
1924 |
+ |
return x.getClass().getName() |
1925 |
+ |
+ "@" + Integer.toHexString(System.identityHashCode(x)); |
1926 |
+ |
} |
1927 |
+ |
|
1928 |
+ |
// --- Shared assertions for Executor tests --- |
1929 |
+ |
|
1930 |
+ |
/** |
1931 |
+ |
* Returns maximum number of tasks that can be submitted to given |
1932 |
+ |
* pool (with bounded queue) before saturation (when submission |
1933 |
+ |
* throws RejectedExecutionException). |
1934 |
+ |
*/ |
1935 |
+ |
static final int saturatedSize(ThreadPoolExecutor pool) { |
1936 |
+ |
BlockingQueue<Runnable> q = pool.getQueue(); |
1937 |
+ |
return pool.getMaximumPoolSize() + q.size() + q.remainingCapacity(); |
1938 |
+ |
} |
1939 |
+ |
|
1940 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
1941 |
+ |
void assertNullTaskSubmissionThrowsNullPointerException(Executor e) { |
1942 |
+ |
try { |
1943 |
+ |
e.execute((Runnable) null); |
1944 |
+ |
shouldThrow(); |
1945 |
+ |
} catch (NullPointerException success) {} |
1946 |
+ |
|
1947 |
+ |
if (! (e instanceof ExecutorService)) return; |
1948 |
+ |
ExecutorService es = (ExecutorService) e; |
1949 |
+ |
try { |
1950 |
+ |
es.submit((Runnable) null); |
1951 |
+ |
shouldThrow(); |
1952 |
+ |
} catch (NullPointerException success) {} |
1953 |
+ |
try { |
1954 |
+ |
es.submit((Runnable) null, Boolean.TRUE); |
1955 |
+ |
shouldThrow(); |
1956 |
+ |
} catch (NullPointerException success) {} |
1957 |
+ |
try { |
1958 |
+ |
es.submit((Callable) null); |
1959 |
+ |
shouldThrow(); |
1960 |
+ |
} catch (NullPointerException success) {} |
1961 |
+ |
|
1962 |
+ |
if (! (e instanceof ScheduledExecutorService)) return; |
1963 |
+ |
ScheduledExecutorService ses = (ScheduledExecutorService) e; |
1964 |
+ |
try { |
1965 |
+ |
ses.schedule((Runnable) null, |
1966 |
+ |
randomTimeout(), randomTimeUnit()); |
1967 |
+ |
shouldThrow(); |
1968 |
+ |
} catch (NullPointerException success) {} |
1969 |
+ |
try { |
1970 |
+ |
ses.schedule((Callable) null, |
1971 |
+ |
randomTimeout(), randomTimeUnit()); |
1972 |
+ |
shouldThrow(); |
1973 |
+ |
} catch (NullPointerException success) {} |
1974 |
+ |
try { |
1975 |
+ |
ses.scheduleAtFixedRate((Runnable) null, |
1976 |
+ |
randomTimeout(), LONG_DELAY_MS, MILLISECONDS); |
1977 |
+ |
shouldThrow(); |
1978 |
+ |
} catch (NullPointerException success) {} |
1979 |
+ |
try { |
1980 |
+ |
ses.scheduleWithFixedDelay((Runnable) null, |
1981 |
+ |
randomTimeout(), LONG_DELAY_MS, MILLISECONDS); |
1982 |
+ |
shouldThrow(); |
1983 |
+ |
} catch (NullPointerException success) {} |
1984 |
+ |
} |
1985 |
+ |
|
1986 |
+ |
void setRejectedExecutionHandler( |
1987 |
+ |
ThreadPoolExecutor p, RejectedExecutionHandler handler) { |
1988 |
+ |
p.setRejectedExecutionHandler(handler); |
1989 |
+ |
assertSame(handler, p.getRejectedExecutionHandler()); |
1990 |
+ |
} |
1991 |
+ |
|
1992 |
+ |
void assertTaskSubmissionsAreRejected(ThreadPoolExecutor p) { |
1993 |
+ |
final RejectedExecutionHandler savedHandler = p.getRejectedExecutionHandler(); |
1994 |
+ |
final long savedTaskCount = p.getTaskCount(); |
1995 |
+ |
final long savedCompletedTaskCount = p.getCompletedTaskCount(); |
1996 |
+ |
final int savedQueueSize = p.getQueue().size(); |
1997 |
+ |
final boolean stock = (p.getClass().getClassLoader() == null); |
1998 |
+ |
|
1999 |
+ |
Runnable r = () -> {}; |
2000 |
+ |
Callable<Boolean> c = () -> Boolean.TRUE; |
2001 |
+ |
|
2002 |
+ |
class Recorder implements RejectedExecutionHandler { |
2003 |
+ |
public volatile Runnable r = null; |
2004 |
+ |
public volatile ThreadPoolExecutor p = null; |
2005 |
+ |
public void reset() { r = null; p = null; } |
2006 |
+ |
public void rejectedExecution(Runnable r, ThreadPoolExecutor p) { |
2007 |
+ |
assertNull(this.r); |
2008 |
+ |
assertNull(this.p); |
2009 |
+ |
this.r = r; |
2010 |
+ |
this.p = p; |
2011 |
+ |
} |
2012 |
+ |
} |
2013 |
+ |
|
2014 |
+ |
// check custom handler is invoked exactly once per task |
2015 |
+ |
Recorder recorder = new Recorder(); |
2016 |
+ |
setRejectedExecutionHandler(p, recorder); |
2017 |
+ |
for (int i = 2; i--> 0; ) { |
2018 |
+ |
recorder.reset(); |
2019 |
+ |
p.execute(r); |
2020 |
+ |
if (stock && p.getClass() == ThreadPoolExecutor.class) |
2021 |
+ |
assertSame(r, recorder.r); |
2022 |
+ |
assertSame(p, recorder.p); |
2023 |
+ |
|
2024 |
+ |
recorder.reset(); |
2025 |
+ |
assertFalse(p.submit(r).isDone()); |
2026 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2027 |
+ |
assertSame(p, recorder.p); |
2028 |
+ |
|
2029 |
+ |
recorder.reset(); |
2030 |
+ |
assertFalse(p.submit(r, Boolean.TRUE).isDone()); |
2031 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2032 |
+ |
assertSame(p, recorder.p); |
2033 |
+ |
|
2034 |
+ |
recorder.reset(); |
2035 |
+ |
assertFalse(p.submit(c).isDone()); |
2036 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2037 |
+ |
assertSame(p, recorder.p); |
2038 |
+ |
|
2039 |
+ |
if (p instanceof ScheduledExecutorService) { |
2040 |
+ |
ScheduledExecutorService s = (ScheduledExecutorService) p; |
2041 |
+ |
ScheduledFuture<?> future; |
2042 |
+ |
|
2043 |
+ |
recorder.reset(); |
2044 |
+ |
future = s.schedule(r, randomTimeout(), randomTimeUnit()); |
2045 |
+ |
assertFalse(future.isDone()); |
2046 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2047 |
+ |
assertSame(p, recorder.p); |
2048 |
+ |
|
2049 |
+ |
recorder.reset(); |
2050 |
+ |
future = s.schedule(c, randomTimeout(), randomTimeUnit()); |
2051 |
+ |
assertFalse(future.isDone()); |
2052 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2053 |
+ |
assertSame(p, recorder.p); |
2054 |
+ |
|
2055 |
+ |
recorder.reset(); |
2056 |
+ |
future = s.scheduleAtFixedRate(r, randomTimeout(), LONG_DELAY_MS, MILLISECONDS); |
2057 |
+ |
assertFalse(future.isDone()); |
2058 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2059 |
+ |
assertSame(p, recorder.p); |
2060 |
+ |
|
2061 |
+ |
recorder.reset(); |
2062 |
+ |
future = s.scheduleWithFixedDelay(r, randomTimeout(), LONG_DELAY_MS, MILLISECONDS); |
2063 |
+ |
assertFalse(future.isDone()); |
2064 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2065 |
+ |
assertSame(p, recorder.p); |
2066 |
+ |
} |
2067 |
+ |
} |
2068 |
+ |
|
2069 |
+ |
// Checking our custom handler above should be sufficient, but |
2070 |
+ |
// we add some integration tests of standard handlers. |
2071 |
+ |
final AtomicReference<Thread> thread = new AtomicReference<>(); |
2072 |
+ |
final Runnable setThread = () -> thread.set(Thread.currentThread()); |
2073 |
+ |
|
2074 |
+ |
setRejectedExecutionHandler(p, new ThreadPoolExecutor.AbortPolicy()); |
2075 |
+ |
try { |
2076 |
+ |
p.execute(setThread); |
2077 |
+ |
shouldThrow(); |
2078 |
+ |
} catch (RejectedExecutionException success) {} |
2079 |
+ |
assertNull(thread.get()); |
2080 |
+ |
|
2081 |
+ |
setRejectedExecutionHandler(p, new ThreadPoolExecutor.DiscardPolicy()); |
2082 |
+ |
p.execute(setThread); |
2083 |
+ |
assertNull(thread.get()); |
2084 |
+ |
|
2085 |
+ |
setRejectedExecutionHandler(p, new ThreadPoolExecutor.CallerRunsPolicy()); |
2086 |
+ |
p.execute(setThread); |
2087 |
+ |
if (p.isShutdown()) |
2088 |
+ |
assertNull(thread.get()); |
2089 |
+ |
else |
2090 |
+ |
assertSame(Thread.currentThread(), thread.get()); |
2091 |
+ |
|
2092 |
+ |
setRejectedExecutionHandler(p, savedHandler); |
2093 |
+ |
|
2094 |
+ |
// check that pool was not perturbed by handlers |
2095 |
+ |
assertEquals(savedTaskCount, p.getTaskCount()); |
2096 |
+ |
assertEquals(savedCompletedTaskCount, p.getCompletedTaskCount()); |
2097 |
+ |
assertEquals(savedQueueSize, p.getQueue().size()); |
2098 |
+ |
} |
2099 |
|
} |