66 |
|
import java.util.Collection; |
67 |
|
import java.util.Collections; |
68 |
|
import java.util.Date; |
69 |
+ |
import java.util.Deque; |
70 |
|
import java.util.Enumeration; |
71 |
+ |
import java.util.HashSet; |
72 |
|
import java.util.Iterator; |
73 |
|
import java.util.List; |
74 |
|
import java.util.NoSuchElementException; |
75 |
|
import java.util.PropertyPermission; |
76 |
+ |
import java.util.Set; |
77 |
|
import java.util.concurrent.BlockingQueue; |
78 |
|
import java.util.concurrent.Callable; |
79 |
|
import java.util.concurrent.CountDownLatch; |
80 |
|
import java.util.concurrent.CyclicBarrier; |
81 |
|
import java.util.concurrent.ExecutionException; |
82 |
+ |
import java.util.concurrent.Executor; |
83 |
|
import java.util.concurrent.Executors; |
84 |
|
import java.util.concurrent.ExecutorService; |
85 |
|
import java.util.concurrent.ForkJoinPool; |
86 |
|
import java.util.concurrent.Future; |
87 |
+ |
import java.util.concurrent.FutureTask; |
88 |
|
import java.util.concurrent.RecursiveAction; |
89 |
|
import java.util.concurrent.RecursiveTask; |
90 |
+ |
import java.util.concurrent.RejectedExecutionException; |
91 |
|
import java.util.concurrent.RejectedExecutionHandler; |
92 |
|
import java.util.concurrent.Semaphore; |
93 |
+ |
import java.util.concurrent.ScheduledExecutorService; |
94 |
+ |
import java.util.concurrent.ScheduledFuture; |
95 |
|
import java.util.concurrent.SynchronousQueue; |
96 |
|
import java.util.concurrent.ThreadFactory; |
97 |
|
import java.util.concurrent.ThreadLocalRandom; |
102 |
|
import java.util.concurrent.atomic.AtomicReference; |
103 |
|
import java.util.regex.Pattern; |
104 |
|
|
97 |
– |
import junit.framework.AssertionFailedError; |
105 |
|
import junit.framework.Test; |
106 |
|
import junit.framework.TestCase; |
107 |
|
import junit.framework.TestResult; |
421 |
|
for (String testClassName : testClassNames) { |
422 |
|
try { |
423 |
|
Class<?> testClass = Class.forName(testClassName); |
424 |
< |
Method m = testClass.getDeclaredMethod("suite", |
418 |
< |
new Class<?>[0]); |
424 |
> |
Method m = testClass.getDeclaredMethod("suite"); |
425 |
|
suite.addTest(newTestSuite((Test)m.invoke(null))); |
426 |
< |
} catch (Exception e) { |
427 |
< |
throw new Error("Missing test class", e); |
426 |
> |
} catch (ReflectiveOperationException e) { |
427 |
> |
throw new AssertionError("Missing test class", e); |
428 |
|
} |
429 |
|
} |
430 |
|
} |
446 |
|
} |
447 |
|
} |
448 |
|
|
449 |
< |
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
450 |
< |
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
451 |
< |
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
452 |
< |
public static boolean atLeastJava9() { |
453 |
< |
return JAVA_CLASS_VERSION >= 53.0 |
454 |
< |
// 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 |
< |
} |
449 |
> |
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
450 |
> |
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
451 |
> |
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
452 |
> |
public static boolean atLeastJava9() { return JAVA_CLASS_VERSION >= 53.0; } |
453 |
> |
public static boolean atLeastJava10() { return JAVA_CLASS_VERSION >= 54.0; } |
454 |
> |
public static boolean atLeastJava11() { return JAVA_CLASS_VERSION >= 55.0; } |
455 |
|
|
456 |
|
/** |
457 |
|
* Collects all JSR166 unit tests as one suite. |
539 |
|
"DoubleAdderTest", |
540 |
|
"ForkJoinPool8Test", |
541 |
|
"ForkJoinTask8Test", |
542 |
+ |
"HashMapTest", |
543 |
|
"LinkedBlockingDeque8Test", |
544 |
|
"LinkedBlockingQueue8Test", |
545 |
|
"LongAccumulatorTest", |
602 |
|
for (String methodName : testMethodNames(testClass)) |
603 |
|
suite.addTest((Test) c.newInstance(data, methodName)); |
604 |
|
return suite; |
605 |
< |
} catch (Exception e) { |
606 |
< |
throw new Error(e); |
605 |
> |
} catch (ReflectiveOperationException e) { |
606 |
> |
throw new AssertionError(e); |
607 |
|
} |
608 |
|
} |
609 |
|
|
619 |
|
if (atLeastJava8()) { |
620 |
|
String name = testClass.getName(); |
621 |
|
String name8 = name.replaceAll("Test$", "8Test"); |
622 |
< |
if (name.equals(name8)) throw new Error(name); |
622 |
> |
if (name.equals(name8)) throw new AssertionError(name); |
623 |
|
try { |
624 |
|
return (Test) |
625 |
|
Class.forName(name8) |
626 |
< |
.getMethod("testSuite", new Class[] { dataClass }) |
626 |
> |
.getMethod("testSuite", dataClass) |
627 |
|
.invoke(null, data); |
628 |
< |
} catch (Exception e) { |
629 |
< |
throw new Error(e); |
628 |
> |
} catch (ReflectiveOperationException e) { |
629 |
> |
throw new AssertionError(e); |
630 |
|
} |
631 |
|
} else { |
632 |
|
return new TestSuite(); |
735 |
|
String msg = toString() + ": " + String.format(format, args); |
736 |
|
System.err.println(msg); |
737 |
|
dumpTestThreads(); |
738 |
< |
throw new AssertionFailedError(msg); |
738 |
> |
throw new AssertionError(msg); |
739 |
|
} |
740 |
|
|
741 |
|
/** |
756 |
|
throw (RuntimeException) t; |
757 |
|
else if (t instanceof Exception) |
758 |
|
throw (Exception) t; |
759 |
< |
else { |
760 |
< |
AssertionFailedError afe = |
760 |
< |
new AssertionFailedError(t.toString()); |
761 |
< |
afe.initCause(t); |
762 |
< |
throw afe; |
763 |
< |
} |
759 |
> |
else |
760 |
> |
throw new AssertionError(t.toString(), t); |
761 |
|
} |
762 |
|
|
763 |
|
if (Thread.interrupted()) |
791 |
|
|
792 |
|
/** |
793 |
|
* Just like fail(reason), but additionally recording (using |
794 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
795 |
< |
* the current testcase will fail. |
794 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
795 |
> |
* current testcase will fail. |
796 |
|
*/ |
797 |
|
public void threadFail(String reason) { |
798 |
|
try { |
799 |
|
fail(reason); |
800 |
< |
} catch (AssertionFailedError t) { |
801 |
< |
threadRecordFailure(t); |
802 |
< |
throw t; |
800 |
> |
} catch (AssertionError fail) { |
801 |
> |
threadRecordFailure(fail); |
802 |
> |
throw fail; |
803 |
|
} |
804 |
|
} |
805 |
|
|
806 |
|
/** |
807 |
|
* Just like assertTrue(b), but additionally recording (using |
808 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
809 |
< |
* the current testcase will fail. |
808 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
809 |
> |
* current testcase will fail. |
810 |
|
*/ |
811 |
|
public void threadAssertTrue(boolean b) { |
812 |
|
try { |
813 |
|
assertTrue(b); |
814 |
< |
} catch (AssertionFailedError t) { |
815 |
< |
threadRecordFailure(t); |
816 |
< |
throw t; |
814 |
> |
} catch (AssertionError fail) { |
815 |
> |
threadRecordFailure(fail); |
816 |
> |
throw fail; |
817 |
|
} |
818 |
|
} |
819 |
|
|
820 |
|
/** |
821 |
|
* Just like assertFalse(b), but additionally recording (using |
822 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
823 |
< |
* the current testcase will fail. |
822 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
823 |
> |
* current testcase will fail. |
824 |
|
*/ |
825 |
|
public void threadAssertFalse(boolean b) { |
826 |
|
try { |
827 |
|
assertFalse(b); |
828 |
< |
} catch (AssertionFailedError t) { |
829 |
< |
threadRecordFailure(t); |
830 |
< |
throw t; |
828 |
> |
} catch (AssertionError fail) { |
829 |
> |
threadRecordFailure(fail); |
830 |
> |
throw fail; |
831 |
|
} |
832 |
|
} |
833 |
|
|
834 |
|
/** |
835 |
|
* Just like assertNull(x), but additionally recording (using |
836 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
837 |
< |
* the current testcase will fail. |
836 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
837 |
> |
* current testcase will fail. |
838 |
|
*/ |
839 |
|
public void threadAssertNull(Object x) { |
840 |
|
try { |
841 |
|
assertNull(x); |
842 |
< |
} catch (AssertionFailedError t) { |
843 |
< |
threadRecordFailure(t); |
844 |
< |
throw t; |
842 |
> |
} catch (AssertionError fail) { |
843 |
> |
threadRecordFailure(fail); |
844 |
> |
throw fail; |
845 |
|
} |
846 |
|
} |
847 |
|
|
848 |
|
/** |
849 |
|
* Just like assertEquals(x, y), but additionally recording (using |
850 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
851 |
< |
* the current testcase will fail. |
850 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
851 |
> |
* current testcase will fail. |
852 |
|
*/ |
853 |
|
public void threadAssertEquals(long x, long y) { |
854 |
|
try { |
855 |
|
assertEquals(x, y); |
856 |
< |
} catch (AssertionFailedError t) { |
857 |
< |
threadRecordFailure(t); |
858 |
< |
throw t; |
856 |
> |
} catch (AssertionError fail) { |
857 |
> |
threadRecordFailure(fail); |
858 |
> |
throw fail; |
859 |
|
} |
860 |
|
} |
861 |
|
|
862 |
|
/** |
863 |
|
* Just like assertEquals(x, y), but additionally recording (using |
864 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
865 |
< |
* the current testcase will fail. |
864 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
865 |
> |
* current testcase will fail. |
866 |
|
*/ |
867 |
|
public void threadAssertEquals(Object x, Object y) { |
868 |
|
try { |
869 |
|
assertEquals(x, y); |
870 |
< |
} catch (AssertionFailedError fail) { |
870 |
> |
} catch (AssertionError fail) { |
871 |
|
threadRecordFailure(fail); |
872 |
|
throw fail; |
873 |
|
} catch (Throwable fail) { |
877 |
|
|
878 |
|
/** |
879 |
|
* Just like assertSame(x, y), but additionally recording (using |
880 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
881 |
< |
* the current testcase will fail. |
880 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
881 |
> |
* current testcase will fail. |
882 |
|
*/ |
883 |
|
public void threadAssertSame(Object x, Object y) { |
884 |
|
try { |
885 |
|
assertSame(x, y); |
886 |
< |
} catch (AssertionFailedError fail) { |
886 |
> |
} catch (AssertionError fail) { |
887 |
|
threadRecordFailure(fail); |
888 |
|
throw fail; |
889 |
|
} |
905 |
|
|
906 |
|
/** |
907 |
|
* Records the given exception using {@link #threadRecordFailure}, |
908 |
< |
* then rethrows the exception, wrapping it in an |
909 |
< |
* AssertionFailedError if necessary. |
908 |
> |
* then rethrows the exception, wrapping it in an AssertionError |
909 |
> |
* if necessary. |
910 |
|
*/ |
911 |
|
public void threadUnexpectedException(Throwable t) { |
912 |
|
threadRecordFailure(t); |
915 |
|
throw (RuntimeException) t; |
916 |
|
else if (t instanceof Error) |
917 |
|
throw (Error) t; |
918 |
< |
else { |
919 |
< |
AssertionFailedError afe = |
923 |
< |
new AssertionFailedError("unexpected exception: " + t); |
924 |
< |
afe.initCause(t); |
925 |
< |
throw afe; |
926 |
< |
} |
918 |
> |
else |
919 |
> |
throw new AssertionError("unexpected exception: " + t, t); |
920 |
|
} |
921 |
|
|
922 |
|
/** |
1092 |
|
for (long retries = LONG_DELAY_MS * 3 / 4; retries-->0; ) { |
1093 |
|
try { delay(1); } |
1094 |
|
catch (InterruptedException fail) { |
1095 |
< |
fail("Unexpected InterruptedException"); |
1095 |
> |
throw new AssertionError("Unexpected InterruptedException", fail); |
1096 |
|
} |
1097 |
|
Thread.State s = thread.getState(); |
1098 |
|
if (s == expected) |
1104 |
|
} |
1105 |
|
|
1106 |
|
/** |
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 |
– |
/** |
1107 |
|
* Checks that future.get times out, with the default timeout of |
1108 |
|
* {@code timeoutMillis()}. |
1109 |
|
*/ |
1278 |
|
|
1279 |
|
/** |
1280 |
|
* Sleeps until the given time has elapsed. |
1281 |
< |
* Throws AssertionFailedError if interrupted. |
1281 |
> |
* Throws AssertionError if interrupted. |
1282 |
|
*/ |
1283 |
|
static void sleep(long millis) { |
1284 |
|
try { |
1285 |
|
delay(millis); |
1286 |
|
} catch (InterruptedException fail) { |
1287 |
< |
AssertionFailedError afe = |
1342 |
< |
new AssertionFailedError("Unexpected InterruptedException"); |
1343 |
< |
afe.initCause(fail); |
1344 |
< |
throw afe; |
1287 |
> |
throw new AssertionError("Unexpected InterruptedException", fail); |
1288 |
|
} |
1289 |
|
} |
1290 |
|
|
1375 |
|
// r.run(); |
1376 |
|
// } catch (Throwable fail) { threadUnexpectedException(fail); } |
1377 |
|
// if (millisElapsedSince(startTime) > timeoutMillis/2) |
1378 |
< |
// throw new AssertionFailedError("did not return promptly"); |
1378 |
> |
// throw new AssertionError("did not return promptly"); |
1379 |
|
// } |
1380 |
|
|
1381 |
|
// void assertTerminatesPromptly(Runnable r) { |
1388 |
|
*/ |
1389 |
|
<T> void checkTimedGet(Future<T> f, T expectedValue, long timeoutMillis) { |
1390 |
|
long startTime = System.nanoTime(); |
1391 |
+ |
T actual = null; |
1392 |
|
try { |
1393 |
< |
assertEquals(expectedValue, f.get(timeoutMillis, MILLISECONDS)); |
1393 |
> |
actual = f.get(timeoutMillis, MILLISECONDS); |
1394 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
1395 |
+ |
assertEquals(expectedValue, actual); |
1396 |
|
if (millisElapsedSince(startTime) > timeoutMillis/2) |
1397 |
< |
throw new AssertionFailedError("timed get did not return promptly"); |
1397 |
> |
throw new AssertionError("timed get did not return promptly"); |
1398 |
|
} |
1399 |
|
|
1400 |
|
<T> void checkTimedGet(Future<T> f, T expectedValue) { |
1452 |
|
} |
1453 |
|
} |
1454 |
|
|
1510 |
– |
public abstract class RunnableShouldThrow implements Runnable { |
1511 |
– |
protected abstract void realRun() throws Throwable; |
1512 |
– |
|
1513 |
– |
final Class<?> exceptionClass; |
1514 |
– |
|
1515 |
– |
<T extends Throwable> RunnableShouldThrow(Class<T> exceptionClass) { |
1516 |
– |
this.exceptionClass = exceptionClass; |
1517 |
– |
} |
1518 |
– |
|
1519 |
– |
public final void run() { |
1520 |
– |
try { |
1521 |
– |
realRun(); |
1522 |
– |
threadShouldThrow(exceptionClass.getSimpleName()); |
1523 |
– |
} catch (Throwable t) { |
1524 |
– |
if (! exceptionClass.isInstance(t)) |
1525 |
– |
threadUnexpectedException(t); |
1526 |
– |
} |
1527 |
– |
} |
1528 |
– |
} |
1529 |
– |
|
1455 |
|
public abstract class ThreadShouldThrow extends Thread { |
1456 |
|
protected abstract void realRun() throws Throwable; |
1457 |
|
|
1616 |
|
// long startTime = System.nanoTime(); |
1617 |
|
// while (!flag.get()) { |
1618 |
|
// if (millisElapsedSince(startTime) > timeoutMillis) |
1619 |
< |
// throw new AssertionFailedError("timed out"); |
1619 |
> |
// throw new AssertionError("timed out"); |
1620 |
|
// Thread.yield(); |
1621 |
|
// } |
1622 |
|
// } |
1703 |
|
|
1704 |
|
/** |
1705 |
|
* A CyclicBarrier that uses timed await and fails with |
1706 |
< |
* AssertionFailedErrors instead of throwing checked exceptions. |
1706 |
> |
* AssertionErrors instead of throwing checked exceptions. |
1707 |
|
*/ |
1708 |
|
public static class CheckedBarrier extends CyclicBarrier { |
1709 |
|
public CheckedBarrier(int parties) { super(parties); } |
1712 |
|
try { |
1713 |
|
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1714 |
|
} catch (TimeoutException timedOut) { |
1715 |
< |
throw new AssertionFailedError("timed out"); |
1715 |
> |
throw new AssertionError("timed out"); |
1716 |
|
} catch (Exception fail) { |
1717 |
< |
AssertionFailedError afe = |
1793 |
< |
new AssertionFailedError("Unexpected exception: " + fail); |
1794 |
< |
afe.initCause(fail); |
1795 |
< |
throw afe; |
1717 |
> |
throw new AssertionError("Unexpected exception: " + fail, fail); |
1718 |
|
} |
1719 |
|
} |
1720 |
|
} |
1837 |
|
try { throwingAction.run(); } |
1838 |
|
catch (Throwable t) { |
1839 |
|
threw = true; |
1840 |
< |
if (!expectedExceptionClass.isInstance(t)) { |
1841 |
< |
AssertionFailedError afe = |
1842 |
< |
new AssertionFailedError |
1843 |
< |
("Expected " + expectedExceptionClass.getName() + |
1844 |
< |
", got " + t.getClass().getName()); |
1923 |
< |
afe.initCause(t); |
1924 |
< |
threadUnexpectedException(afe); |
1925 |
< |
} |
1840 |
> |
if (!expectedExceptionClass.isInstance(t)) |
1841 |
> |
throw new AssertionError( |
1842 |
> |
"Expected " + expectedExceptionClass.getName() + |
1843 |
> |
", got " + t.getClass().getName(), |
1844 |
> |
t); |
1845 |
|
} |
1846 |
|
if (!threw) |
1847 |
|
shouldThrow(expectedExceptionClass.getName()); |
1870 |
|
1000L, MILLISECONDS, |
1871 |
|
new SynchronousQueue<Runnable>()); |
1872 |
|
|
1873 |
+ |
static <T> void shuffle(T[] array) { |
1874 |
+ |
Collections.shuffle(Arrays.asList(array), ThreadLocalRandom.current()); |
1875 |
+ |
} |
1876 |
+ |
|
1877 |
+ |
/** |
1878 |
+ |
* Returns the same String as would be returned by {@link |
1879 |
+ |
* Object#toString}, whether or not the given object's class |
1880 |
+ |
* overrides toString(). |
1881 |
+ |
* |
1882 |
+ |
* @see System#identityHashCode |
1883 |
+ |
*/ |
1884 |
+ |
static String identityString(Object x) { |
1885 |
+ |
return x.getClass().getName() |
1886 |
+ |
+ "@" + Integer.toHexString(System.identityHashCode(x)); |
1887 |
+ |
} |
1888 |
+ |
|
1889 |
+ |
// --- Shared assertions for Executor tests --- |
1890 |
+ |
|
1891 |
|
/** |
1892 |
|
* Returns maximum number of tasks that can be submitted to given |
1893 |
|
* pool (with bounded queue) before saturation (when submission |
1898 |
|
return pool.getMaximumPoolSize() + q.size() + q.remainingCapacity(); |
1899 |
|
} |
1900 |
|
|
1901 |
< |
static <T> void shuffle(T[] array) { |
1902 |
< |
Collections.shuffle(Arrays.asList(array), ThreadLocalRandom.current()); |
1901 |
> |
@SuppressWarnings("FutureReturnValueIgnored") |
1902 |
> |
void assertNullTaskSubmissionThrowsNullPointerException(Executor e) { |
1903 |
> |
try { |
1904 |
> |
e.execute((Runnable) null); |
1905 |
> |
shouldThrow(); |
1906 |
> |
} catch (NullPointerException success) {} |
1907 |
> |
|
1908 |
> |
if (! (e instanceof ExecutorService)) return; |
1909 |
> |
ExecutorService es = (ExecutorService) e; |
1910 |
> |
try { |
1911 |
> |
es.submit((Runnable) null); |
1912 |
> |
shouldThrow(); |
1913 |
> |
} catch (NullPointerException success) {} |
1914 |
> |
try { |
1915 |
> |
es.submit((Runnable) null, Boolean.TRUE); |
1916 |
> |
shouldThrow(); |
1917 |
> |
} catch (NullPointerException success) {} |
1918 |
> |
try { |
1919 |
> |
es.submit((Callable) null); |
1920 |
> |
shouldThrow(); |
1921 |
> |
} catch (NullPointerException success) {} |
1922 |
> |
|
1923 |
> |
if (! (e instanceof ScheduledExecutorService)) return; |
1924 |
> |
ScheduledExecutorService ses = (ScheduledExecutorService) e; |
1925 |
> |
try { |
1926 |
> |
ses.schedule((Runnable) null, |
1927 |
> |
randomTimeout(), randomTimeUnit()); |
1928 |
> |
shouldThrow(); |
1929 |
> |
} catch (NullPointerException success) {} |
1930 |
> |
try { |
1931 |
> |
ses.schedule((Callable) null, |
1932 |
> |
randomTimeout(), randomTimeUnit()); |
1933 |
> |
shouldThrow(); |
1934 |
> |
} catch (NullPointerException success) {} |
1935 |
> |
try { |
1936 |
> |
ses.scheduleAtFixedRate((Runnable) null, |
1937 |
> |
randomTimeout(), LONG_DELAY_MS, MILLISECONDS); |
1938 |
> |
shouldThrow(); |
1939 |
> |
} catch (NullPointerException success) {} |
1940 |
> |
try { |
1941 |
> |
ses.scheduleWithFixedDelay((Runnable) null, |
1942 |
> |
randomTimeout(), LONG_DELAY_MS, MILLISECONDS); |
1943 |
> |
shouldThrow(); |
1944 |
> |
} catch (NullPointerException success) {} |
1945 |
> |
} |
1946 |
> |
|
1947 |
> |
void setRejectedExecutionHandler( |
1948 |
> |
ThreadPoolExecutor p, RejectedExecutionHandler handler) { |
1949 |
> |
p.setRejectedExecutionHandler(handler); |
1950 |
> |
assertSame(handler, p.getRejectedExecutionHandler()); |
1951 |
> |
} |
1952 |
> |
|
1953 |
> |
void assertTaskSubmissionsAreRejected(ThreadPoolExecutor p) { |
1954 |
> |
final RejectedExecutionHandler savedHandler = p.getRejectedExecutionHandler(); |
1955 |
> |
final long savedTaskCount = p.getTaskCount(); |
1956 |
> |
final long savedCompletedTaskCount = p.getCompletedTaskCount(); |
1957 |
> |
final int savedQueueSize = p.getQueue().size(); |
1958 |
> |
final boolean stock = (p.getClass().getClassLoader() == null); |
1959 |
> |
|
1960 |
> |
Runnable r = () -> {}; |
1961 |
> |
Callable<Boolean> c = () -> Boolean.TRUE; |
1962 |
> |
|
1963 |
> |
class Recorder implements RejectedExecutionHandler { |
1964 |
> |
public volatile Runnable r = null; |
1965 |
> |
public volatile ThreadPoolExecutor p = null; |
1966 |
> |
public void reset() { r = null; p = null; } |
1967 |
> |
public void rejectedExecution(Runnable r, ThreadPoolExecutor p) { |
1968 |
> |
assertNull(this.r); |
1969 |
> |
assertNull(this.p); |
1970 |
> |
this.r = r; |
1971 |
> |
this.p = p; |
1972 |
> |
} |
1973 |
> |
} |
1974 |
> |
|
1975 |
> |
// check custom handler is invoked exactly once per task |
1976 |
> |
Recorder recorder = new Recorder(); |
1977 |
> |
setRejectedExecutionHandler(p, recorder); |
1978 |
> |
for (int i = 2; i--> 0; ) { |
1979 |
> |
recorder.reset(); |
1980 |
> |
p.execute(r); |
1981 |
> |
if (stock && p.getClass() == ThreadPoolExecutor.class) |
1982 |
> |
assertSame(r, recorder.r); |
1983 |
> |
assertSame(p, recorder.p); |
1984 |
> |
|
1985 |
> |
recorder.reset(); |
1986 |
> |
assertFalse(p.submit(r).isDone()); |
1987 |
> |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
1988 |
> |
assertSame(p, recorder.p); |
1989 |
> |
|
1990 |
> |
recorder.reset(); |
1991 |
> |
assertFalse(p.submit(r, Boolean.TRUE).isDone()); |
1992 |
> |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
1993 |
> |
assertSame(p, recorder.p); |
1994 |
> |
|
1995 |
> |
recorder.reset(); |
1996 |
> |
assertFalse(p.submit(c).isDone()); |
1997 |
> |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
1998 |
> |
assertSame(p, recorder.p); |
1999 |
> |
|
2000 |
> |
if (p instanceof ScheduledExecutorService) { |
2001 |
> |
ScheduledExecutorService s = (ScheduledExecutorService) p; |
2002 |
> |
ScheduledFuture<?> future; |
2003 |
> |
|
2004 |
> |
recorder.reset(); |
2005 |
> |
future = s.schedule(r, randomTimeout(), randomTimeUnit()); |
2006 |
> |
assertFalse(future.isDone()); |
2007 |
> |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2008 |
> |
assertSame(p, recorder.p); |
2009 |
> |
|
2010 |
> |
recorder.reset(); |
2011 |
> |
future = s.schedule(c, randomTimeout(), randomTimeUnit()); |
2012 |
> |
assertFalse(future.isDone()); |
2013 |
> |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2014 |
> |
assertSame(p, recorder.p); |
2015 |
> |
|
2016 |
> |
recorder.reset(); |
2017 |
> |
future = s.scheduleAtFixedRate(r, randomTimeout(), LONG_DELAY_MS, MILLISECONDS); |
2018 |
> |
assertFalse(future.isDone()); |
2019 |
> |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2020 |
> |
assertSame(p, recorder.p); |
2021 |
> |
|
2022 |
> |
recorder.reset(); |
2023 |
> |
future = s.scheduleWithFixedDelay(r, randomTimeout(), LONG_DELAY_MS, MILLISECONDS); |
2024 |
> |
assertFalse(future.isDone()); |
2025 |
> |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2026 |
> |
assertSame(p, recorder.p); |
2027 |
> |
} |
2028 |
> |
} |
2029 |
> |
|
2030 |
> |
// Checking our custom handler above should be sufficient, but |
2031 |
> |
// we add some integration tests of standard handlers. |
2032 |
> |
final AtomicReference<Thread> thread = new AtomicReference<>(); |
2033 |
> |
final Runnable setThread = () -> thread.set(Thread.currentThread()); |
2034 |
> |
|
2035 |
> |
setRejectedExecutionHandler(p, new ThreadPoolExecutor.AbortPolicy()); |
2036 |
> |
try { |
2037 |
> |
p.execute(setThread); |
2038 |
> |
shouldThrow(); |
2039 |
> |
} catch (RejectedExecutionException success) {} |
2040 |
> |
assertNull(thread.get()); |
2041 |
> |
|
2042 |
> |
setRejectedExecutionHandler(p, new ThreadPoolExecutor.DiscardPolicy()); |
2043 |
> |
p.execute(setThread); |
2044 |
> |
assertNull(thread.get()); |
2045 |
> |
|
2046 |
> |
setRejectedExecutionHandler(p, new ThreadPoolExecutor.CallerRunsPolicy()); |
2047 |
> |
p.execute(setThread); |
2048 |
> |
if (p.isShutdown()) |
2049 |
> |
assertNull(thread.get()); |
2050 |
> |
else |
2051 |
> |
assertSame(Thread.currentThread(), thread.get()); |
2052 |
> |
|
2053 |
> |
setRejectedExecutionHandler(p, savedHandler); |
2054 |
> |
|
2055 |
> |
// check that pool was not perturbed by handlers |
2056 |
> |
assertEquals(savedTaskCount, p.getTaskCount()); |
2057 |
> |
assertEquals(savedCompletedTaskCount, p.getCompletedTaskCount()); |
2058 |
> |
assertEquals(savedQueueSize, p.getQueue().size()); |
2059 |
> |
} |
2060 |
> |
|
2061 |
> |
void assertCollectionsEquals(Collection<?> x, Collection<?> y) { |
2062 |
> |
assertEquals(x, y); |
2063 |
> |
assertEquals(y, x); |
2064 |
> |
assertEquals(x.isEmpty(), y.isEmpty()); |
2065 |
> |
assertEquals(x.size(), y.size()); |
2066 |
> |
if (x instanceof List) { |
2067 |
> |
assertEquals(x.toString(), y.toString()); |
2068 |
> |
} |
2069 |
> |
if (x instanceof List || x instanceof Set) { |
2070 |
> |
assertEquals(x.hashCode(), y.hashCode()); |
2071 |
> |
} |
2072 |
> |
if (x instanceof List || x instanceof Deque) { |
2073 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
2074 |
> |
assertTrue(Arrays.equals(x.toArray(new Object[0]), |
2075 |
> |
y.toArray(new Object[0]))); |
2076 |
> |
} |
2077 |
> |
} |
2078 |
> |
|
2079 |
> |
/** |
2080 |
> |
* A weaker form of assertCollectionsEquals which does not insist |
2081 |
> |
* that the two collections satisfy Object#equals(Object), since |
2082 |
> |
* they may use identity semantics as Deques do. |
2083 |
> |
*/ |
2084 |
> |
void assertCollectionsEquivalent(Collection<?> x, Collection<?> y) { |
2085 |
> |
if (x instanceof List || x instanceof Set) |
2086 |
> |
assertCollectionsEquals(x, y); |
2087 |
> |
else { |
2088 |
> |
assertEquals(x.isEmpty(), y.isEmpty()); |
2089 |
> |
assertEquals(x.size(), y.size()); |
2090 |
> |
assertEquals(new HashSet(x), new HashSet(y)); |
2091 |
> |
if (x instanceof Deque) { |
2092 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
2093 |
> |
assertTrue(Arrays.equals(x.toArray(new Object[0]), |
2094 |
> |
y.toArray(new Object[0]))); |
2095 |
> |
} |
2096 |
> |
} |
2097 |
|
} |
2098 |
|
} |