ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/JSR166TestCase.java
(Generate patch)

Comparing jsr166/src/test/tck/JSR166TestCase.java (file contents):
Revision 1.233 by jsr166, Sat Jul 15 23:15:21 2017 UTC vs.
Revision 1.241 by jsr166, Sun Jan 28 16:20:42 2018 UTC

# Line 76 | Line 76 | import java.util.concurrent.Callable;
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;
# Line 94 | Line 99 | import java.util.concurrent.atomic.Atomi
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;
# Line 414 | Line 418 | public class JSR166TestCase extends Test
418          for (String testClassName : testClassNames) {
419              try {
420                  Class<?> testClass = Class.forName(testClassName);
421 <                Method m = testClass.getDeclaredMethod("suite",
418 <                                                       new Class<?>[0]);
421 >                Method m = testClass.getDeclaredMethod("suite");
422                  suite.addTest(newTestSuite((Test)m.invoke(null)));
423 <            } catch (Exception e) {
424 <                throw new Error("Missing test class", e);
423 >            } catch (ReflectiveOperationException e) {
424 >                throw new AssertionError("Missing test class", e);
425              }
426          }
427      }
# Line 440 | Line 443 | public class JSR166TestCase extends Test
443          }
444      }
445  
446 <    public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; }
447 <    public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; }
448 <    public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; }
449 <    public static boolean atLeastJava9() {
450 <        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 <    }
446 >    public static boolean atLeastJava6()  { return JAVA_CLASS_VERSION >= 50.0; }
447 >    public static boolean atLeastJava7()  { return JAVA_CLASS_VERSION >= 51.0; }
448 >    public static boolean atLeastJava8()  { return JAVA_CLASS_VERSION >= 52.0; }
449 >    public static boolean atLeastJava9()  { return JAVA_CLASS_VERSION >= 53.0; }
450 >    public static boolean atLeastJava10() { return JAVA_CLASS_VERSION >= 54.0; }
451  
452      /**
453       * Collects all JSR166 unit tests as one suite.
# Line 539 | Line 535 | public class JSR166TestCase extends Test
535                  "DoubleAdderTest",
536                  "ForkJoinPool8Test",
537                  "ForkJoinTask8Test",
538 +                "HashMapTest",
539                  "LinkedBlockingDeque8Test",
540                  "LinkedBlockingQueue8Test",
541                  "LongAccumulatorTest",
# Line 601 | Line 598 | public class JSR166TestCase extends Test
598              for (String methodName : testMethodNames(testClass))
599                  suite.addTest((Test) c.newInstance(data, methodName));
600              return suite;
601 <        } catch (Exception e) {
602 <            throw new Error(e);
601 >        } catch (ReflectiveOperationException e) {
602 >            throw new AssertionError(e);
603          }
604      }
605  
# Line 618 | Line 615 | public class JSR166TestCase extends Test
615          if (atLeastJava8()) {
616              String name = testClass.getName();
617              String name8 = name.replaceAll("Test$", "8Test");
618 <            if (name.equals(name8)) throw new Error(name);
618 >            if (name.equals(name8)) throw new AssertionError(name);
619              try {
620                  return (Test)
621                      Class.forName(name8)
622 <                    .getMethod("testSuite", new Class[] { dataClass })
622 >                    .getMethod("testSuite", dataClass)
623                      .invoke(null, data);
624 <            } catch (Exception e) {
625 <                throw new Error(e);
624 >            } catch (ReflectiveOperationException e) {
625 >                throw new AssertionError(e);
626              }
627          } else {
628              return new TestSuite();
# Line 734 | Line 731 | public class JSR166TestCase extends Test
731          String msg = toString() + ": " + String.format(format, args);
732          System.err.println(msg);
733          dumpTestThreads();
734 <        throw new AssertionFailedError(msg);
734 >        throw new AssertionError(msg);
735      }
736  
737      /**
# Line 755 | Line 752 | public class JSR166TestCase extends Test
752                  throw (RuntimeException) t;
753              else if (t instanceof Exception)
754                  throw (Exception) t;
755 <            else {
756 <                AssertionFailedError afe =
760 <                    new AssertionFailedError(t.toString());
761 <                afe.initCause(t);
762 <                throw afe;
763 <            }
755 >            else
756 >                throw new AssertionError(t.toString(), t);
757          }
758  
759          if (Thread.interrupted())
# Line 794 | Line 787 | public class JSR166TestCase extends Test
787  
788      /**
789       * Just like fail(reason), but additionally recording (using
790 <     * threadRecordFailure) any AssertionFailedError thrown, so that
791 <     * the current testcase will fail.
790 >     * threadRecordFailure) any AssertionError thrown, so that the
791 >     * current testcase will fail.
792       */
793      public void threadFail(String reason) {
794          try {
795              fail(reason);
796 <        } catch (AssertionFailedError t) {
797 <            threadRecordFailure(t);
798 <            throw t;
796 >        } catch (AssertionError fail) {
797 >            threadRecordFailure(fail);
798 >            throw fail;
799          }
800      }
801  
802      /**
803       * Just like assertTrue(b), but additionally recording (using
804 <     * threadRecordFailure) any AssertionFailedError thrown, so that
805 <     * the current testcase will fail.
804 >     * threadRecordFailure) any AssertionError thrown, so that the
805 >     * current testcase will fail.
806       */
807      public void threadAssertTrue(boolean b) {
808          try {
809              assertTrue(b);
810 <        } catch (AssertionFailedError t) {
811 <            threadRecordFailure(t);
812 <            throw t;
810 >        } catch (AssertionError fail) {
811 >            threadRecordFailure(fail);
812 >            throw fail;
813          }
814      }
815  
816      /**
817       * Just like assertFalse(b), but additionally recording (using
818 <     * threadRecordFailure) any AssertionFailedError thrown, so that
819 <     * the current testcase will fail.
818 >     * threadRecordFailure) any AssertionError thrown, so that the
819 >     * current testcase will fail.
820       */
821      public void threadAssertFalse(boolean b) {
822          try {
823              assertFalse(b);
824 <        } catch (AssertionFailedError t) {
825 <            threadRecordFailure(t);
826 <            throw t;
824 >        } catch (AssertionError fail) {
825 >            threadRecordFailure(fail);
826 >            throw fail;
827          }
828      }
829  
830      /**
831       * Just like assertNull(x), but additionally recording (using
832 <     * threadRecordFailure) any AssertionFailedError thrown, so that
833 <     * the current testcase will fail.
832 >     * threadRecordFailure) any AssertionError thrown, so that the
833 >     * current testcase will fail.
834       */
835      public void threadAssertNull(Object x) {
836          try {
837              assertNull(x);
838 <        } catch (AssertionFailedError t) {
839 <            threadRecordFailure(t);
840 <            throw t;
838 >        } catch (AssertionError fail) {
839 >            threadRecordFailure(fail);
840 >            throw fail;
841          }
842      }
843  
844      /**
845       * Just like assertEquals(x, y), but additionally recording (using
846 <     * threadRecordFailure) any AssertionFailedError thrown, so that
847 <     * the current testcase will fail.
846 >     * threadRecordFailure) any AssertionError thrown, so that the
847 >     * current testcase will fail.
848       */
849      public void threadAssertEquals(long x, long y) {
850          try {
851              assertEquals(x, y);
852 <        } catch (AssertionFailedError t) {
853 <            threadRecordFailure(t);
854 <            throw t;
852 >        } catch (AssertionError fail) {
853 >            threadRecordFailure(fail);
854 >            throw fail;
855          }
856      }
857  
858      /**
859       * Just like assertEquals(x, y), but additionally recording (using
860 <     * threadRecordFailure) any AssertionFailedError thrown, so that
861 <     * the current testcase will fail.
860 >     * threadRecordFailure) any AssertionError thrown, so that the
861 >     * current testcase will fail.
862       */
863      public void threadAssertEquals(Object x, Object y) {
864          try {
865              assertEquals(x, y);
866 <        } catch (AssertionFailedError fail) {
866 >        } catch (AssertionError fail) {
867              threadRecordFailure(fail);
868              throw fail;
869          } catch (Throwable fail) {
# Line 880 | Line 873 | public class JSR166TestCase extends Test
873  
874      /**
875       * Just like assertSame(x, y), but additionally recording (using
876 <     * threadRecordFailure) any AssertionFailedError thrown, so that
877 <     * the current testcase will fail.
876 >     * threadRecordFailure) any AssertionError thrown, so that the
877 >     * current testcase will fail.
878       */
879      public void threadAssertSame(Object x, Object y) {
880          try {
881              assertSame(x, y);
882 <        } catch (AssertionFailedError fail) {
882 >        } catch (AssertionError fail) {
883              threadRecordFailure(fail);
884              throw fail;
885          }
# Line 908 | Line 901 | public class JSR166TestCase extends Test
901  
902      /**
903       * Records the given exception using {@link #threadRecordFailure},
904 <     * then rethrows the exception, wrapping it in an
905 <     * AssertionFailedError if necessary.
904 >     * then rethrows the exception, wrapping it in an AssertionError
905 >     * if necessary.
906       */
907      public void threadUnexpectedException(Throwable t) {
908          threadRecordFailure(t);
# Line 918 | Line 911 | public class JSR166TestCase extends Test
911              throw (RuntimeException) t;
912          else if (t instanceof Error)
913              throw (Error) t;
914 <        else {
915 <            AssertionFailedError afe =
923 <                new AssertionFailedError("unexpected exception: " + t);
924 <            afe.initCause(t);
925 <            throw afe;
926 <        }
914 >        else
915 >            throw new AssertionError("unexpected exception: " + t, t);
916      }
917  
918      /**
# Line 1099 | Line 1088 | public class JSR166TestCase extends Test
1088          for (long retries = LONG_DELAY_MS * 3 / 4; retries-->0; ) {
1089              try { delay(1); }
1090              catch (InterruptedException fail) {
1091 <                fail("Unexpected InterruptedException");
1091 >                throw new AssertionError("Unexpected InterruptedException", fail);
1092              }
1093              Thread.State s = thread.getState();
1094              if (s == expected)
# Line 1111 | Line 1100 | public class JSR166TestCase extends Test
1100      }
1101  
1102      /**
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    /**
1103       * Checks that future.get times out, with the default timeout of
1104       * {@code timeoutMillis()}.
1105       */
# Line 1332 | Line 1274 | public class JSR166TestCase extends Test
1274  
1275      /**
1276       * Sleeps until the given time has elapsed.
1277 <     * Throws AssertionFailedError if interrupted.
1277 >     * Throws AssertionError if interrupted.
1278       */
1279      static void sleep(long millis) {
1280          try {
1281              delay(millis);
1282          } catch (InterruptedException fail) {
1283 <            AssertionFailedError afe =
1342 <                new AssertionFailedError("Unexpected InterruptedException");
1343 <            afe.initCause(fail);
1344 <            throw afe;
1283 >            throw new AssertionError("Unexpected InterruptedException", fail);
1284          }
1285      }
1286  
# Line 1432 | Line 1371 | public class JSR166TestCase extends Test
1371   //             r.run();
1372   //         } catch (Throwable fail) { threadUnexpectedException(fail); }
1373   //         if (millisElapsedSince(startTime) > timeoutMillis/2)
1374 < //             throw new AssertionFailedError("did not return promptly");
1374 > //             throw new AssertionError("did not return promptly");
1375   //     }
1376  
1377   //     void assertTerminatesPromptly(Runnable r) {
# Line 1449 | Line 1388 | public class JSR166TestCase extends Test
1388              assertEquals(expectedValue, f.get(timeoutMillis, MILLISECONDS));
1389          } catch (Throwable fail) { threadUnexpectedException(fail); }
1390          if (millisElapsedSince(startTime) > timeoutMillis/2)
1391 <            throw new AssertionFailedError("timed get did not return promptly");
1391 >            throw new AssertionError("timed get did not return promptly");
1392      }
1393  
1394      <T> void checkTimedGet(Future<T> f, T expectedValue) {
# Line 1691 | Line 1630 | public class JSR166TestCase extends Test
1630   //         long startTime = System.nanoTime();
1631   //         while (!flag.get()) {
1632   //             if (millisElapsedSince(startTime) > timeoutMillis)
1633 < //                 throw new AssertionFailedError("timed out");
1633 > //                 throw new AssertionError("timed out");
1634   //             Thread.yield();
1635   //         }
1636   //     }
# Line 1778 | Line 1717 | public class JSR166TestCase extends Test
1717  
1718      /**
1719       * A CyclicBarrier that uses timed await and fails with
1720 <     * AssertionFailedErrors instead of throwing checked exceptions.
1720 >     * AssertionErrors instead of throwing checked exceptions.
1721       */
1722      public static class CheckedBarrier extends CyclicBarrier {
1723          public CheckedBarrier(int parties) { super(parties); }
# Line 1787 | Line 1726 | public class JSR166TestCase extends Test
1726              try {
1727                  return super.await(2 * LONG_DELAY_MS, MILLISECONDS);
1728              } catch (TimeoutException timedOut) {
1729 <                throw new AssertionFailedError("timed out");
1729 >                throw new AssertionError("timed out");
1730              } catch (Exception fail) {
1731 <                AssertionFailedError afe =
1793 <                    new AssertionFailedError("Unexpected exception: " + fail);
1794 <                afe.initCause(fail);
1795 <                throw afe;
1731 >                throw new AssertionError("Unexpected exception: " + fail, fail);
1732              }
1733          }
1734      }
# Line 1915 | Line 1851 | public class JSR166TestCase extends Test
1851              try { throwingAction.run(); }
1852              catch (Throwable t) {
1853                  threw = true;
1854 <                if (!expectedExceptionClass.isInstance(t)) {
1855 <                    AssertionFailedError afe =
1856 <                        new AssertionFailedError
1857 <                        ("Expected " + expectedExceptionClass.getName() +
1858 <                         ", got " + t.getClass().getName());
1923 <                    afe.initCause(t);
1924 <                    threadUnexpectedException(afe);
1925 <                }
1854 >                if (!expectedExceptionClass.isInstance(t))
1855 >                    throw new AssertionError(
1856 >                            "Expected " + expectedExceptionClass.getName() +
1857 >                            ", got " + t.getClass().getName(),
1858 >                            t);
1859              }
1860              if (!threw)
1861                  shouldThrow(expectedExceptionClass.getName());
# Line 1951 | Line 1884 | public class JSR166TestCase extends Test
1884                                 1000L, MILLISECONDS,
1885                                 new SynchronousQueue<Runnable>());
1886  
1887 +    static <T> void shuffle(T[] array) {
1888 +        Collections.shuffle(Arrays.asList(array), ThreadLocalRandom.current());
1889 +    }
1890 +
1891 +    /**
1892 +     * Returns the same String as would be returned by {@link
1893 +     * Object#toString}, whether or not the given object's class
1894 +     * overrides toString().
1895 +     *
1896 +     * @see System#identityHashCode
1897 +     */
1898 +    static String identityString(Object x) {
1899 +        return x.getClass().getName()
1900 +            + "@" + Integer.toHexString(System.identityHashCode(x));
1901 +    }
1902 +
1903 +    // --- Shared assertions for Executor tests ---
1904 +
1905      /**
1906       * Returns maximum number of tasks that can be submitted to given
1907       * pool (with bounded queue) before saturation (when submission
# Line 1961 | Line 1912 | public class JSR166TestCase extends Test
1912          return pool.getMaximumPoolSize() + q.size() + q.remainingCapacity();
1913      }
1914  
1915 <    static <T> void shuffle(T[] array) {
1916 <        Collections.shuffle(Arrays.asList(array), ThreadLocalRandom.current());
1915 >    @SuppressWarnings("FutureReturnValueIgnored")
1916 >    void assertNullTaskSubmissionThrowsNullPointerException(Executor e) {
1917 >        try {
1918 >            e.execute((Runnable) null);
1919 >            shouldThrow();
1920 >        } catch (NullPointerException success) {}
1921 >
1922 >        if (! (e instanceof ExecutorService)) return;
1923 >        ExecutorService es = (ExecutorService) e;
1924 >        try {
1925 >            es.submit((Runnable) null);
1926 >            shouldThrow();
1927 >        } catch (NullPointerException success) {}
1928 >        try {
1929 >            es.submit((Runnable) null, Boolean.TRUE);
1930 >            shouldThrow();
1931 >        } catch (NullPointerException success) {}
1932 >        try {
1933 >            es.submit((Callable) null);
1934 >            shouldThrow();
1935 >        } catch (NullPointerException success) {}
1936 >
1937 >        if (! (e instanceof ScheduledExecutorService)) return;
1938 >        ScheduledExecutorService ses = (ScheduledExecutorService) e;
1939 >        try {
1940 >            ses.schedule((Runnable) null,
1941 >                         randomTimeout(), randomTimeUnit());
1942 >            shouldThrow();
1943 >        } catch (NullPointerException success) {}
1944 >        try {
1945 >            ses.schedule((Callable) null,
1946 >                         randomTimeout(), randomTimeUnit());
1947 >            shouldThrow();
1948 >        } catch (NullPointerException success) {}
1949 >        try {
1950 >            ses.scheduleAtFixedRate((Runnable) null,
1951 >                                    randomTimeout(), LONG_DELAY_MS, MILLISECONDS);
1952 >            shouldThrow();
1953 >        } catch (NullPointerException success) {}
1954 >        try {
1955 >            ses.scheduleWithFixedDelay((Runnable) null,
1956 >                                       randomTimeout(), LONG_DELAY_MS, MILLISECONDS);
1957 >            shouldThrow();
1958 >        } catch (NullPointerException success) {}
1959 >    }
1960 >
1961 >    void setRejectedExecutionHandler(
1962 >        ThreadPoolExecutor p, RejectedExecutionHandler handler) {
1963 >        p.setRejectedExecutionHandler(handler);
1964 >        assertSame(handler, p.getRejectedExecutionHandler());
1965 >    }
1966 >
1967 >    void assertTaskSubmissionsAreRejected(ThreadPoolExecutor p) {
1968 >        final RejectedExecutionHandler savedHandler = p.getRejectedExecutionHandler();
1969 >        final long savedTaskCount = p.getTaskCount();
1970 >        final long savedCompletedTaskCount = p.getCompletedTaskCount();
1971 >        final int savedQueueSize = p.getQueue().size();
1972 >        final boolean stock = (p.getClass().getClassLoader() == null);
1973 >
1974 >        Runnable r = () -> {};
1975 >        Callable<Boolean> c = () -> Boolean.TRUE;
1976 >
1977 >        class Recorder implements RejectedExecutionHandler {
1978 >            public volatile Runnable r = null;
1979 >            public volatile ThreadPoolExecutor p = null;
1980 >            public void reset() { r = null; p = null; }
1981 >            public void rejectedExecution(Runnable r, ThreadPoolExecutor p) {
1982 >                assertNull(this.r);
1983 >                assertNull(this.p);
1984 >                this.r = r;
1985 >                this.p = p;
1986 >            }
1987 >        }
1988 >
1989 >        // check custom handler is invoked exactly once per task
1990 >        Recorder recorder = new Recorder();
1991 >        setRejectedExecutionHandler(p, recorder);
1992 >        for (int i = 2; i--> 0; ) {
1993 >            recorder.reset();
1994 >            p.execute(r);
1995 >            if (stock && p.getClass() == ThreadPoolExecutor.class)
1996 >                assertSame(r, recorder.r);
1997 >            assertSame(p, recorder.p);
1998 >
1999 >            recorder.reset();
2000 >            assertFalse(p.submit(r).isDone());
2001 >            if (stock) assertTrue(!((FutureTask) recorder.r).isDone());
2002 >            assertSame(p, recorder.p);
2003 >
2004 >            recorder.reset();
2005 >            assertFalse(p.submit(r, Boolean.TRUE).isDone());
2006 >            if (stock) assertTrue(!((FutureTask) recorder.r).isDone());
2007 >            assertSame(p, recorder.p);
2008 >
2009 >            recorder.reset();
2010 >            assertFalse(p.submit(c).isDone());
2011 >            if (stock) assertTrue(!((FutureTask) recorder.r).isDone());
2012 >            assertSame(p, recorder.p);
2013 >
2014 >            if (p instanceof ScheduledExecutorService) {
2015 >                ScheduledExecutorService s = (ScheduledExecutorService) p;
2016 >                ScheduledFuture<?> future;
2017 >
2018 >                recorder.reset();
2019 >                future = s.schedule(r, randomTimeout(), randomTimeUnit());
2020 >                assertFalse(future.isDone());
2021 >                if (stock) assertTrue(!((FutureTask) recorder.r).isDone());
2022 >                assertSame(p, recorder.p);
2023 >
2024 >                recorder.reset();
2025 >                future = s.schedule(c, randomTimeout(), randomTimeUnit());
2026 >                assertFalse(future.isDone());
2027 >                if (stock) assertTrue(!((FutureTask) recorder.r).isDone());
2028 >                assertSame(p, recorder.p);
2029 >
2030 >                recorder.reset();
2031 >                future = s.scheduleAtFixedRate(r, randomTimeout(), LONG_DELAY_MS, MILLISECONDS);
2032 >                assertFalse(future.isDone());
2033 >                if (stock) assertTrue(!((FutureTask) recorder.r).isDone());
2034 >                assertSame(p, recorder.p);
2035 >
2036 >                recorder.reset();
2037 >                future = s.scheduleWithFixedDelay(r, randomTimeout(), LONG_DELAY_MS, MILLISECONDS);
2038 >                assertFalse(future.isDone());
2039 >                if (stock) assertTrue(!((FutureTask) recorder.r).isDone());
2040 >                assertSame(p, recorder.p);
2041 >            }
2042 >        }
2043 >
2044 >        // Checking our custom handler above should be sufficient, but
2045 >        // we add some integration tests of standard handlers.
2046 >        final AtomicReference<Thread> thread = new AtomicReference<>();
2047 >        final Runnable setThread = () -> thread.set(Thread.currentThread());
2048 >
2049 >        setRejectedExecutionHandler(p, new ThreadPoolExecutor.AbortPolicy());
2050 >        try {
2051 >            p.execute(setThread);
2052 >            shouldThrow();
2053 >        } catch (RejectedExecutionException success) {}
2054 >        assertNull(thread.get());
2055 >
2056 >        setRejectedExecutionHandler(p, new ThreadPoolExecutor.DiscardPolicy());
2057 >        p.execute(setThread);
2058 >        assertNull(thread.get());
2059 >
2060 >        setRejectedExecutionHandler(p, new ThreadPoolExecutor.CallerRunsPolicy());
2061 >        p.execute(setThread);
2062 >        if (p.isShutdown())
2063 >            assertNull(thread.get());
2064 >        else
2065 >            assertSame(Thread.currentThread(), thread.get());
2066 >
2067 >        setRejectedExecutionHandler(p, savedHandler);
2068 >
2069 >        // check that pool was not perturbed by handlers
2070 >        assertEquals(savedTaskCount, p.getTaskCount());
2071 >        assertEquals(savedCompletedTaskCount, p.getCompletedTaskCount());
2072 >        assertEquals(savedQueueSize, p.getQueue().size());
2073      }
2074   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines