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; |
99 |
|
import java.util.concurrent.atomic.AtomicReference; |
100 |
|
import java.util.regex.Pattern; |
101 |
|
|
97 |
– |
import junit.framework.AssertionFailedError; |
102 |
|
import junit.framework.Test; |
103 |
|
import junit.framework.TestCase; |
104 |
|
import junit.framework.TestResult; |
444 |
|
} |
445 |
|
} |
446 |
|
|
447 |
< |
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
448 |
< |
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
449 |
< |
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
450 |
< |
public static boolean atLeastJava9() { |
451 |
< |
return JAVA_CLASS_VERSION >= 53.0 |
448 |
< |
// As of 2015-09, java9 still uses 52.0 class file version |
449 |
< |
|| JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?(9|[0-9][0-9])$"); |
450 |
< |
} |
451 |
< |
public static boolean atLeastJava10() { |
452 |
< |
return JAVA_CLASS_VERSION >= 54.0 |
453 |
< |
|| JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?[0-9][0-9]$"); |
454 |
< |
} |
447 |
> |
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
448 |
> |
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
449 |
> |
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
450 |
> |
public static boolean atLeastJava9() { return JAVA_CLASS_VERSION >= 53.0; } |
451 |
> |
public static boolean atLeastJava10() { return JAVA_CLASS_VERSION >= 54.0; } |
452 |
|
|
453 |
|
/** |
454 |
|
* Collects all JSR166 unit tests as one suite. |
536 |
|
"DoubleAdderTest", |
537 |
|
"ForkJoinPool8Test", |
538 |
|
"ForkJoinTask8Test", |
539 |
+ |
"HashMapTest", |
540 |
|
"LinkedBlockingDeque8Test", |
541 |
|
"LinkedBlockingQueue8Test", |
542 |
|
"LongAccumulatorTest", |
732 |
|
String msg = toString() + ": " + String.format(format, args); |
733 |
|
System.err.println(msg); |
734 |
|
dumpTestThreads(); |
735 |
< |
throw new AssertionFailedError(msg); |
735 |
> |
throw new AssertionError(msg); |
736 |
|
} |
737 |
|
|
738 |
|
/** |
753 |
|
throw (RuntimeException) t; |
754 |
|
else if (t instanceof Exception) |
755 |
|
throw (Exception) t; |
756 |
< |
else { |
757 |
< |
AssertionFailedError afe = |
760 |
< |
new AssertionFailedError(t.toString()); |
761 |
< |
afe.initCause(t); |
762 |
< |
throw afe; |
763 |
< |
} |
756 |
> |
else |
757 |
> |
throw new AssertionError(t.toString(), t); |
758 |
|
} |
759 |
|
|
760 |
|
if (Thread.interrupted()) |
788 |
|
|
789 |
|
/** |
790 |
|
* Just like fail(reason), but additionally recording (using |
791 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
792 |
< |
* the current testcase will fail. |
791 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
792 |
> |
* current testcase will fail. |
793 |
|
*/ |
794 |
|
public void threadFail(String reason) { |
795 |
|
try { |
796 |
|
fail(reason); |
797 |
< |
} catch (AssertionFailedError t) { |
798 |
< |
threadRecordFailure(t); |
799 |
< |
throw t; |
797 |
> |
} catch (AssertionError fail) { |
798 |
> |
threadRecordFailure(fail); |
799 |
> |
throw fail; |
800 |
|
} |
801 |
|
} |
802 |
|
|
803 |
|
/** |
804 |
|
* Just like assertTrue(b), but additionally recording (using |
805 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
806 |
< |
* the current testcase will fail. |
805 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
806 |
> |
* current testcase will fail. |
807 |
|
*/ |
808 |
|
public void threadAssertTrue(boolean b) { |
809 |
|
try { |
810 |
|
assertTrue(b); |
811 |
< |
} catch (AssertionFailedError t) { |
812 |
< |
threadRecordFailure(t); |
813 |
< |
throw t; |
811 |
> |
} catch (AssertionError fail) { |
812 |
> |
threadRecordFailure(fail); |
813 |
> |
throw fail; |
814 |
|
} |
815 |
|
} |
816 |
|
|
817 |
|
/** |
818 |
|
* Just like assertFalse(b), but additionally recording (using |
819 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
820 |
< |
* the current testcase will fail. |
819 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
820 |
> |
* current testcase will fail. |
821 |
|
*/ |
822 |
|
public void threadAssertFalse(boolean b) { |
823 |
|
try { |
824 |
|
assertFalse(b); |
825 |
< |
} catch (AssertionFailedError t) { |
826 |
< |
threadRecordFailure(t); |
827 |
< |
throw t; |
825 |
> |
} catch (AssertionError fail) { |
826 |
> |
threadRecordFailure(fail); |
827 |
> |
throw fail; |
828 |
|
} |
829 |
|
} |
830 |
|
|
831 |
|
/** |
832 |
|
* Just like assertNull(x), but additionally recording (using |
833 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
834 |
< |
* the current testcase will fail. |
833 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
834 |
> |
* current testcase will fail. |
835 |
|
*/ |
836 |
|
public void threadAssertNull(Object x) { |
837 |
|
try { |
838 |
|
assertNull(x); |
839 |
< |
} catch (AssertionFailedError t) { |
840 |
< |
threadRecordFailure(t); |
841 |
< |
throw t; |
839 |
> |
} catch (AssertionError fail) { |
840 |
> |
threadRecordFailure(fail); |
841 |
> |
throw fail; |
842 |
|
} |
843 |
|
} |
844 |
|
|
845 |
|
/** |
846 |
|
* Just like assertEquals(x, y), but additionally recording (using |
847 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
848 |
< |
* the current testcase will fail. |
847 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
848 |
> |
* current testcase will fail. |
849 |
|
*/ |
850 |
|
public void threadAssertEquals(long x, long y) { |
851 |
|
try { |
852 |
|
assertEquals(x, y); |
853 |
< |
} catch (AssertionFailedError t) { |
854 |
< |
threadRecordFailure(t); |
855 |
< |
throw t; |
853 |
> |
} catch (AssertionError fail) { |
854 |
> |
threadRecordFailure(fail); |
855 |
> |
throw fail; |
856 |
|
} |
857 |
|
} |
858 |
|
|
859 |
|
/** |
860 |
|
* Just like assertEquals(x, y), but additionally recording (using |
861 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
862 |
< |
* the current testcase will fail. |
861 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
862 |
> |
* current testcase will fail. |
863 |
|
*/ |
864 |
|
public void threadAssertEquals(Object x, Object y) { |
865 |
|
try { |
866 |
|
assertEquals(x, y); |
867 |
< |
} catch (AssertionFailedError fail) { |
867 |
> |
} catch (AssertionError fail) { |
868 |
|
threadRecordFailure(fail); |
869 |
|
throw fail; |
870 |
|
} catch (Throwable fail) { |
874 |
|
|
875 |
|
/** |
876 |
|
* Just like assertSame(x, y), but additionally recording (using |
877 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
878 |
< |
* the current testcase will fail. |
877 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
878 |
> |
* current testcase will fail. |
879 |
|
*/ |
880 |
|
public void threadAssertSame(Object x, Object y) { |
881 |
|
try { |
882 |
|
assertSame(x, y); |
883 |
< |
} catch (AssertionFailedError fail) { |
883 |
> |
} catch (AssertionError fail) { |
884 |
|
threadRecordFailure(fail); |
885 |
|
throw fail; |
886 |
|
} |
902 |
|
|
903 |
|
/** |
904 |
|
* Records the given exception using {@link #threadRecordFailure}, |
905 |
< |
* then rethrows the exception, wrapping it in an |
906 |
< |
* AssertionFailedError if necessary. |
905 |
> |
* then rethrows the exception, wrapping it in an AssertionError |
906 |
> |
* if necessary. |
907 |
|
*/ |
908 |
|
public void threadUnexpectedException(Throwable t) { |
909 |
|
threadRecordFailure(t); |
912 |
|
throw (RuntimeException) t; |
913 |
|
else if (t instanceof Error) |
914 |
|
throw (Error) t; |
915 |
< |
else { |
916 |
< |
AssertionFailedError afe = |
923 |
< |
new AssertionFailedError("unexpected exception: " + t); |
924 |
< |
afe.initCause(t); |
925 |
< |
throw afe; |
926 |
< |
} |
915 |
> |
else |
916 |
> |
throw new AssertionError("unexpected exception: " + t, t); |
917 |
|
} |
918 |
|
|
919 |
|
/** |
1089 |
|
for (long retries = LONG_DELAY_MS * 3 / 4; retries-->0; ) { |
1090 |
|
try { delay(1); } |
1091 |
|
catch (InterruptedException fail) { |
1092 |
< |
fail("Unexpected InterruptedException"); |
1092 |
> |
throw new AssertionError("Unexpected InterruptedException", fail); |
1093 |
|
} |
1094 |
|
Thread.State s = thread.getState(); |
1095 |
|
if (s == expected) |
1101 |
|
} |
1102 |
|
|
1103 |
|
/** |
1114 |
– |
* Checks that thread does not terminate within the default |
1115 |
– |
* millisecond delay of {@code timeoutMillis()}. |
1116 |
– |
* TODO: REMOVEME |
1117 |
– |
*/ |
1118 |
– |
void assertThreadStaysAlive(Thread thread) { |
1119 |
– |
assertThreadStaysAlive(thread, timeoutMillis()); |
1120 |
– |
} |
1121 |
– |
|
1122 |
– |
/** |
1123 |
– |
* Checks that thread does not terminate within the given millisecond delay. |
1124 |
– |
* TODO: REMOVEME |
1125 |
– |
*/ |
1126 |
– |
void assertThreadStaysAlive(Thread thread, long millis) { |
1127 |
– |
try { |
1128 |
– |
// No need to optimize the failing case via Thread.join. |
1129 |
– |
delay(millis); |
1130 |
– |
assertTrue(thread.isAlive()); |
1131 |
– |
} catch (InterruptedException fail) { |
1132 |
– |
threadFail("Unexpected InterruptedException"); |
1133 |
– |
} |
1134 |
– |
} |
1135 |
– |
|
1136 |
– |
/** |
1137 |
– |
* Checks that the threads do not terminate within the default |
1138 |
– |
* millisecond delay of {@code timeoutMillis()}. |
1139 |
– |
* TODO: REMOVEME |
1140 |
– |
*/ |
1141 |
– |
void assertThreadsStayAlive(Thread... threads) { |
1142 |
– |
assertThreadsStayAlive(timeoutMillis(), threads); |
1143 |
– |
} |
1144 |
– |
|
1145 |
– |
/** |
1146 |
– |
* Checks that the threads do not terminate within the given millisecond delay. |
1147 |
– |
* TODO: REMOVEME |
1148 |
– |
*/ |
1149 |
– |
void assertThreadsStayAlive(long millis, Thread... threads) { |
1150 |
– |
try { |
1151 |
– |
// No need to optimize the failing case via Thread.join. |
1152 |
– |
delay(millis); |
1153 |
– |
for (Thread thread : threads) |
1154 |
– |
assertTrue(thread.isAlive()); |
1155 |
– |
} catch (InterruptedException fail) { |
1156 |
– |
threadFail("Unexpected InterruptedException"); |
1157 |
– |
} |
1158 |
– |
} |
1159 |
– |
|
1160 |
– |
/** |
1104 |
|
* Checks that future.get times out, with the default timeout of |
1105 |
|
* {@code timeoutMillis()}. |
1106 |
|
*/ |
1275 |
|
|
1276 |
|
/** |
1277 |
|
* Sleeps until the given time has elapsed. |
1278 |
< |
* Throws AssertionFailedError if interrupted. |
1278 |
> |
* Throws AssertionError if interrupted. |
1279 |
|
*/ |
1280 |
|
static void sleep(long millis) { |
1281 |
|
try { |
1282 |
|
delay(millis); |
1283 |
|
} catch (InterruptedException fail) { |
1284 |
< |
AssertionFailedError afe = |
1342 |
< |
new AssertionFailedError("Unexpected InterruptedException"); |
1343 |
< |
afe.initCause(fail); |
1344 |
< |
throw afe; |
1284 |
> |
throw new AssertionError("Unexpected InterruptedException", fail); |
1285 |
|
} |
1286 |
|
} |
1287 |
|
|
1372 |
|
// r.run(); |
1373 |
|
// } catch (Throwable fail) { threadUnexpectedException(fail); } |
1374 |
|
// if (millisElapsedSince(startTime) > timeoutMillis/2) |
1375 |
< |
// throw new AssertionFailedError("did not return promptly"); |
1375 |
> |
// throw new AssertionError("did not return promptly"); |
1376 |
|
// } |
1377 |
|
|
1378 |
|
// void assertTerminatesPromptly(Runnable r) { |
1389 |
|
assertEquals(expectedValue, f.get(timeoutMillis, MILLISECONDS)); |
1390 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
1391 |
|
if (millisElapsedSince(startTime) > timeoutMillis/2) |
1392 |
< |
throw new AssertionFailedError("timed get did not return promptly"); |
1392 |
> |
throw new AssertionError("timed get did not return promptly"); |
1393 |
|
} |
1394 |
|
|
1395 |
|
<T> void checkTimedGet(Future<T> f, T expectedValue) { |
1631 |
|
// long startTime = System.nanoTime(); |
1632 |
|
// while (!flag.get()) { |
1633 |
|
// if (millisElapsedSince(startTime) > timeoutMillis) |
1634 |
< |
// throw new AssertionFailedError("timed out"); |
1634 |
> |
// throw new AssertionError("timed out"); |
1635 |
|
// Thread.yield(); |
1636 |
|
// } |
1637 |
|
// } |
1718 |
|
|
1719 |
|
/** |
1720 |
|
* A CyclicBarrier that uses timed await and fails with |
1721 |
< |
* AssertionFailedErrors instead of throwing checked exceptions. |
1721 |
> |
* AssertionErrors instead of throwing checked exceptions. |
1722 |
|
*/ |
1723 |
|
public static class CheckedBarrier extends CyclicBarrier { |
1724 |
|
public CheckedBarrier(int parties) { super(parties); } |
1727 |
|
try { |
1728 |
|
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1729 |
|
} catch (TimeoutException timedOut) { |
1730 |
< |
throw new AssertionFailedError("timed out"); |
1730 |
> |
throw new AssertionError("timed out"); |
1731 |
|
} catch (Exception fail) { |
1732 |
< |
AssertionFailedError afe = |
1793 |
< |
new AssertionFailedError("Unexpected exception: " + fail); |
1794 |
< |
afe.initCause(fail); |
1795 |
< |
throw afe; |
1732 |
> |
throw new AssertionError("Unexpected exception: " + fail, fail); |
1733 |
|
} |
1734 |
|
} |
1735 |
|
} |
1852 |
|
try { throwingAction.run(); } |
1853 |
|
catch (Throwable t) { |
1854 |
|
threw = true; |
1855 |
< |
if (!expectedExceptionClass.isInstance(t)) { |
1856 |
< |
AssertionFailedError afe = |
1857 |
< |
new AssertionFailedError |
1858 |
< |
("Expected " + expectedExceptionClass.getName() + |
1859 |
< |
", got " + t.getClass().getName()); |
1923 |
< |
afe.initCause(t); |
1924 |
< |
threadUnexpectedException(afe); |
1925 |
< |
} |
1855 |
> |
if (!expectedExceptionClass.isInstance(t)) |
1856 |
> |
throw new AssertionError( |
1857 |
> |
"Expected " + expectedExceptionClass.getName() + |
1858 |
> |
", got " + t.getClass().getName(), |
1859 |
> |
t); |
1860 |
|
} |
1861 |
|
if (!threw) |
1862 |
|
shouldThrow(expectedExceptionClass.getName()); |
1888 |
|
static <T> void shuffle(T[] array) { |
1889 |
|
Collections.shuffle(Arrays.asList(array), ThreadLocalRandom.current()); |
1890 |
|
} |
1891 |
+ |
|
1892 |
+ |
/** |
1893 |
+ |
* Returns the same String as would be returned by {@link |
1894 |
+ |
* Object#toString}, whether or not the given object's class |
1895 |
+ |
* overrides toString(). |
1896 |
+ |
* |
1897 |
+ |
* @see System#identityHashCode |
1898 |
+ |
*/ |
1899 |
+ |
static String identityString(Object x) { |
1900 |
+ |
return x.getClass().getName() |
1901 |
+ |
+ "@" + Integer.toHexString(System.identityHashCode(x)); |
1902 |
+ |
} |
1903 |
+ |
|
1904 |
+ |
// --- Shared assertions for Executor tests --- |
1905 |
+ |
|
1906 |
+ |
/** |
1907 |
+ |
* Returns maximum number of tasks that can be submitted to given |
1908 |
+ |
* pool (with bounded queue) before saturation (when submission |
1909 |
+ |
* throws RejectedExecutionException). |
1910 |
+ |
*/ |
1911 |
+ |
static final int saturatedSize(ThreadPoolExecutor pool) { |
1912 |
+ |
BlockingQueue<Runnable> q = pool.getQueue(); |
1913 |
+ |
return pool.getMaximumPoolSize() + q.size() + q.remainingCapacity(); |
1914 |
+ |
} |
1915 |
+ |
|
1916 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
1917 |
+ |
void assertNullTaskSubmissionThrowsNullPointerException(Executor e) { |
1918 |
+ |
try { |
1919 |
+ |
e.execute((Runnable) null); |
1920 |
+ |
shouldThrow(); |
1921 |
+ |
} catch (NullPointerException success) {} |
1922 |
+ |
|
1923 |
+ |
if (! (e instanceof ExecutorService)) return; |
1924 |
+ |
ExecutorService es = (ExecutorService) e; |
1925 |
+ |
try { |
1926 |
+ |
es.submit((Runnable) null); |
1927 |
+ |
shouldThrow(); |
1928 |
+ |
} catch (NullPointerException success) {} |
1929 |
+ |
try { |
1930 |
+ |
es.submit((Runnable) null, Boolean.TRUE); |
1931 |
+ |
shouldThrow(); |
1932 |
+ |
} catch (NullPointerException success) {} |
1933 |
+ |
try { |
1934 |
+ |
es.submit((Callable) null); |
1935 |
+ |
shouldThrow(); |
1936 |
+ |
} catch (NullPointerException success) {} |
1937 |
+ |
|
1938 |
+ |
if (! (e instanceof ScheduledExecutorService)) return; |
1939 |
+ |
ScheduledExecutorService ses = (ScheduledExecutorService) e; |
1940 |
+ |
try { |
1941 |
+ |
ses.schedule((Runnable) null, |
1942 |
+ |
randomTimeout(), randomTimeUnit()); |
1943 |
+ |
shouldThrow(); |
1944 |
+ |
} catch (NullPointerException success) {} |
1945 |
+ |
try { |
1946 |
+ |
ses.schedule((Callable) null, |
1947 |
+ |
randomTimeout(), randomTimeUnit()); |
1948 |
+ |
shouldThrow(); |
1949 |
+ |
} catch (NullPointerException success) {} |
1950 |
+ |
try { |
1951 |
+ |
ses.scheduleAtFixedRate((Runnable) null, |
1952 |
+ |
randomTimeout(), LONG_DELAY_MS, MILLISECONDS); |
1953 |
+ |
shouldThrow(); |
1954 |
+ |
} catch (NullPointerException success) {} |
1955 |
+ |
try { |
1956 |
+ |
ses.scheduleWithFixedDelay((Runnable) null, |
1957 |
+ |
randomTimeout(), LONG_DELAY_MS, MILLISECONDS); |
1958 |
+ |
shouldThrow(); |
1959 |
+ |
} catch (NullPointerException success) {} |
1960 |
+ |
} |
1961 |
+ |
|
1962 |
+ |
void setRejectedExecutionHandler( |
1963 |
+ |
ThreadPoolExecutor p, RejectedExecutionHandler handler) { |
1964 |
+ |
p.setRejectedExecutionHandler(handler); |
1965 |
+ |
assertSame(handler, p.getRejectedExecutionHandler()); |
1966 |
+ |
} |
1967 |
+ |
|
1968 |
+ |
void assertTaskSubmissionsAreRejected(ThreadPoolExecutor p) { |
1969 |
+ |
final RejectedExecutionHandler savedHandler = p.getRejectedExecutionHandler(); |
1970 |
+ |
final long savedTaskCount = p.getTaskCount(); |
1971 |
+ |
final long savedCompletedTaskCount = p.getCompletedTaskCount(); |
1972 |
+ |
final int savedQueueSize = p.getQueue().size(); |
1973 |
+ |
final boolean stock = (p.getClass().getClassLoader() == null); |
1974 |
+ |
|
1975 |
+ |
Runnable r = () -> {}; |
1976 |
+ |
Callable<Boolean> c = () -> Boolean.TRUE; |
1977 |
+ |
|
1978 |
+ |
class Recorder implements RejectedExecutionHandler { |
1979 |
+ |
public volatile Runnable r = null; |
1980 |
+ |
public volatile ThreadPoolExecutor p = null; |
1981 |
+ |
public void reset() { r = null; p = null; } |
1982 |
+ |
public void rejectedExecution(Runnable r, ThreadPoolExecutor p) { |
1983 |
+ |
assertNull(this.r); |
1984 |
+ |
assertNull(this.p); |
1985 |
+ |
this.r = r; |
1986 |
+ |
this.p = p; |
1987 |
+ |
} |
1988 |
+ |
} |
1989 |
+ |
|
1990 |
+ |
// check custom handler is invoked exactly once per task |
1991 |
+ |
Recorder recorder = new Recorder(); |
1992 |
+ |
setRejectedExecutionHandler(p, recorder); |
1993 |
+ |
for (int i = 2; i--> 0; ) { |
1994 |
+ |
recorder.reset(); |
1995 |
+ |
p.execute(r); |
1996 |
+ |
if (stock && p.getClass() == ThreadPoolExecutor.class) |
1997 |
+ |
assertSame(r, recorder.r); |
1998 |
+ |
assertSame(p, recorder.p); |
1999 |
+ |
|
2000 |
+ |
recorder.reset(); |
2001 |
+ |
assertFalse(p.submit(r).isDone()); |
2002 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2003 |
+ |
assertSame(p, recorder.p); |
2004 |
+ |
|
2005 |
+ |
recorder.reset(); |
2006 |
+ |
assertFalse(p.submit(r, Boolean.TRUE).isDone()); |
2007 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2008 |
+ |
assertSame(p, recorder.p); |
2009 |
+ |
|
2010 |
+ |
recorder.reset(); |
2011 |
+ |
assertFalse(p.submit(c).isDone()); |
2012 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2013 |
+ |
assertSame(p, recorder.p); |
2014 |
+ |
|
2015 |
+ |
if (p instanceof ScheduledExecutorService) { |
2016 |
+ |
ScheduledExecutorService s = (ScheduledExecutorService) p; |
2017 |
+ |
ScheduledFuture<?> future; |
2018 |
+ |
|
2019 |
+ |
recorder.reset(); |
2020 |
+ |
future = s.schedule(r, randomTimeout(), randomTimeUnit()); |
2021 |
+ |
assertFalse(future.isDone()); |
2022 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2023 |
+ |
assertSame(p, recorder.p); |
2024 |
+ |
|
2025 |
+ |
recorder.reset(); |
2026 |
+ |
future = s.schedule(c, randomTimeout(), randomTimeUnit()); |
2027 |
+ |
assertFalse(future.isDone()); |
2028 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2029 |
+ |
assertSame(p, recorder.p); |
2030 |
+ |
|
2031 |
+ |
recorder.reset(); |
2032 |
+ |
future = s.scheduleAtFixedRate(r, randomTimeout(), LONG_DELAY_MS, MILLISECONDS); |
2033 |
+ |
assertFalse(future.isDone()); |
2034 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2035 |
+ |
assertSame(p, recorder.p); |
2036 |
+ |
|
2037 |
+ |
recorder.reset(); |
2038 |
+ |
future = s.scheduleWithFixedDelay(r, randomTimeout(), LONG_DELAY_MS, MILLISECONDS); |
2039 |
+ |
assertFalse(future.isDone()); |
2040 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2041 |
+ |
assertSame(p, recorder.p); |
2042 |
+ |
} |
2043 |
+ |
} |
2044 |
+ |
|
2045 |
+ |
// Checking our custom handler above should be sufficient, but |
2046 |
+ |
// we add some integration tests of standard handlers. |
2047 |
+ |
final AtomicReference<Thread> thread = new AtomicReference<>(); |
2048 |
+ |
final Runnable setThread = () -> thread.set(Thread.currentThread()); |
2049 |
+ |
|
2050 |
+ |
setRejectedExecutionHandler(p, new ThreadPoolExecutor.AbortPolicy()); |
2051 |
+ |
try { |
2052 |
+ |
p.execute(setThread); |
2053 |
+ |
shouldThrow(); |
2054 |
+ |
} catch (RejectedExecutionException success) {} |
2055 |
+ |
assertNull(thread.get()); |
2056 |
+ |
|
2057 |
+ |
setRejectedExecutionHandler(p, new ThreadPoolExecutor.DiscardPolicy()); |
2058 |
+ |
p.execute(setThread); |
2059 |
+ |
assertNull(thread.get()); |
2060 |
+ |
|
2061 |
+ |
setRejectedExecutionHandler(p, new ThreadPoolExecutor.CallerRunsPolicy()); |
2062 |
+ |
p.execute(setThread); |
2063 |
+ |
if (p.isShutdown()) |
2064 |
+ |
assertNull(thread.get()); |
2065 |
+ |
else |
2066 |
+ |
assertSame(Thread.currentThread(), thread.get()); |
2067 |
+ |
|
2068 |
+ |
setRejectedExecutionHandler(p, savedHandler); |
2069 |
+ |
|
2070 |
+ |
// check that pool was not perturbed by handlers |
2071 |
+ |
assertEquals(savedTaskCount, p.getTaskCount()); |
2072 |
+ |
assertEquals(savedCompletedTaskCount, p.getCompletedTaskCount()); |
2073 |
+ |
assertEquals(savedQueueSize, p.getQueue().size()); |
2074 |
+ |
} |
2075 |
|
} |