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

Comparing jsr166/src/test/tck/ThreadPoolExecutorTest.java (file contents):
Revision 1.114 by jsr166, Wed Jan 4 06:09:58 2017 UTC vs.
Revision 1.124 by jsr166, Mon Jul 17 22:27:31 2017 UTC

# Line 11 | Line 11 | import static java.util.concurrent.TimeU
11   import static java.util.concurrent.TimeUnit.SECONDS;
12  
13   import java.util.ArrayList;
14 + import java.util.Collection;
15 + import java.util.Collections;
16   import java.util.List;
17   import java.util.concurrent.ArrayBlockingQueue;
18   import java.util.concurrent.BlockingQueue;
# Line 18 | Line 20 | import java.util.concurrent.Callable;
20   import java.util.concurrent.CancellationException;
21   import java.util.concurrent.CountDownLatch;
22   import java.util.concurrent.ExecutionException;
21 import java.util.concurrent.Executors;
23   import java.util.concurrent.ExecutorService;
24   import java.util.concurrent.Future;
25   import java.util.concurrent.FutureTask;
# Line 27 | Line 28 | import java.util.concurrent.RejectedExec
28   import java.util.concurrent.RejectedExecutionHandler;
29   import java.util.concurrent.SynchronousQueue;
30   import java.util.concurrent.ThreadFactory;
31 + import java.util.concurrent.ThreadLocalRandom;
32   import java.util.concurrent.ThreadPoolExecutor;
33 + import java.util.concurrent.ThreadPoolExecutor.AbortPolicy;
34 + import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy;
35 + import java.util.concurrent.ThreadPoolExecutor.DiscardPolicy;
36 + import java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy;
37   import java.util.concurrent.atomic.AtomicInteger;
38 + import java.util.concurrent.atomic.AtomicReference;
39  
40   import junit.framework.Test;
41   import junit.framework.TestSuite;
# Line 91 | Line 98 | public class ThreadPoolExecutorTest exte
98              final Runnable task = new CheckedRunnable() {
99                  public void realRun() { done.countDown(); }};
100              p.execute(task);
101 <            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
101 >            await(done);
102          }
103      }
104  
# Line 185 | Line 192 | public class ThreadPoolExecutorTest exte
192                  public void realRun() throws InterruptedException {
193                      threadStarted.countDown();
194                      assertEquals(0, p.getCompletedTaskCount());
195 <                    threadProceed.await();
195 >                    await(threadProceed);
196                      threadDone.countDown();
197                  }});
198              await(threadStarted);
199              assertEquals(0, p.getCompletedTaskCount());
200              threadProceed.countDown();
201 <            threadDone.await();
201 >            await(threadDone);
202              long startTime = System.nanoTime();
203              while (p.getCompletedTaskCount() != 1) {
204                  if (millisElapsedSince(startTime) > LONG_DELAY_MS)
# Line 275 | Line 282 | public class ThreadPoolExecutorTest exte
282      }
283  
284      /**
285 +     * The default rejected execution handler is AbortPolicy.
286 +     */
287 +    public void testDefaultRejectedExecutionHandler() {
288 +        final ThreadPoolExecutor p =
289 +            new ThreadPoolExecutor(1, 2,
290 +                                   LONG_DELAY_MS, MILLISECONDS,
291 +                                   new ArrayBlockingQueue<Runnable>(10));
292 +        try (PoolCleaner cleaner = cleaner(p)) {
293 +            assertTrue(p.getRejectedExecutionHandler() instanceof AbortPolicy);
294 +        }
295 +    }
296 +
297 +    /**
298       * getRejectedExecutionHandler returns handler in constructor if not set
299       */
300      public void testGetRejectedExecutionHandler() {
# Line 457 | Line 477 | public class ThreadPoolExecutorTest exte
477              assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
478              assertFalse(p.awaitTermination(-1L, NANOSECONDS));
479              assertFalse(p.awaitTermination(-1L, MILLISECONDS));
480 <            assertFalse(p.awaitTermination(0L, NANOSECONDS));
481 <            assertFalse(p.awaitTermination(0L, MILLISECONDS));
480 >            assertFalse(p.awaitTermination(randomExpiredTimeout(),
481 >                                           randomTimeUnit()));
482              long timeoutNanos = 999999L;
483              long startTime = System.nanoTime();
484              assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
# Line 1039 | Line 1059 | public class ThreadPoolExecutorTest exte
1059                      p.submit(task).get();
1060                  }});
1061  
1062 <            await(threadStarted);
1062 >            await(threadStarted); // ensure quiescence
1063              t.interrupt();
1064              awaitTermination(t);
1065          }
1066      }
1067  
1068      /**
1069 <     * execute throws RejectedExecutionException if saturated.
1069 >     * Submitted tasks are rejected when saturated or shutdown
1070       */
1071 <    public void testSaturatedExecute() {
1071 >    public void testSubmittedTasksRejectedWhenSaturatedOrShutdown() throws InterruptedException {
1072 >        final ThreadPoolExecutor p = new ThreadPoolExecutor(
1073 >            1, 1, 1, SECONDS, new ArrayBlockingQueue<Runnable>(1));
1074 >        final int saturatedSize = saturatedSize(p);
1075 >        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
1076 >        final CountDownLatch threadsStarted = new CountDownLatch(p.getMaximumPoolSize());
1077          final CountDownLatch done = new CountDownLatch(1);
1078 <        final ThreadPoolExecutor p =
1079 <            new ThreadPoolExecutor(1, 1,
1080 <                                   LONG_DELAY_MS, MILLISECONDS,
1056 <                                   new ArrayBlockingQueue<Runnable>(1));
1057 <        try (PoolCleaner cleaner = cleaner(p, done)) {
1058 <            Runnable task = new CheckedRunnable() {
1059 <                public void realRun() throws InterruptedException {
1060 <                    await(done);
1061 <                }};
1062 <            for (int i = 0; i < 2; ++i)
1063 <                p.execute(task);
1064 <            for (int i = 0; i < 2; ++i) {
1078 >        final Runnable r = () -> {
1079 >            threadsStarted.countDown();
1080 >            for (;;) {
1081                  try {
1082 <                    p.execute(task);
1083 <                    shouldThrow();
1084 <                } catch (RejectedExecutionException success) {}
1085 <                assertTrue(p.getTaskCount() <= 2);
1086 <            }
1087 <        }
1088 <    }
1073 <
1074 <    /**
1075 <     * submit(runnable) throws RejectedExecutionException if saturated.
1076 <     */
1077 <    public void testSaturatedSubmitRunnable() {
1078 <        final CountDownLatch done = new CountDownLatch(1);
1079 <        final ThreadPoolExecutor p =
1080 <            new ThreadPoolExecutor(1, 1,
1081 <                                   LONG_DELAY_MS, MILLISECONDS,
1082 <                                   new ArrayBlockingQueue<Runnable>(1));
1083 <        try (PoolCleaner cleaner = cleaner(p, done)) {
1084 <            Runnable task = new CheckedRunnable() {
1085 <                public void realRun() throws InterruptedException {
1086 <                    await(done);
1087 <                }};
1088 <            for (int i = 0; i < 2; ++i)
1089 <                p.submit(task);
1090 <            for (int i = 0; i < 2; ++i) {
1082 >                    done.await();
1083 >                    return;
1084 >                } catch (InterruptedException shutdownNowDeliberatelyIgnored) {}
1085 >            }};
1086 >        final Callable<Boolean> c = () -> {
1087 >            threadsStarted.countDown();
1088 >            for (;;) {
1089                  try {
1090 <                    p.execute(task);
1091 <                    shouldThrow();
1092 <                } catch (RejectedExecutionException success) {}
1093 <                assertTrue(p.getTaskCount() <= 2);
1094 <            }
1097 <        }
1098 <    }
1090 >                    done.await();
1091 >                    return Boolean.TRUE;
1092 >                } catch (InterruptedException shutdownNowDeliberatelyIgnored) {}
1093 >            }};
1094 >        final boolean shutdownNow = rnd.nextBoolean();
1095  
1100    /**
1101     * submit(callable) throws RejectedExecutionException if saturated.
1102     */
1103    public void testSaturatedSubmitCallable() {
1104        final CountDownLatch done = new CountDownLatch(1);
1105        final ThreadPoolExecutor p =
1106            new ThreadPoolExecutor(1, 1,
1107                                   LONG_DELAY_MS, MILLISECONDS,
1108                                   new ArrayBlockingQueue<Runnable>(1));
1096          try (PoolCleaner cleaner = cleaner(p, done)) {
1097 <            Runnable task = new CheckedRunnable() {
1098 <                public void realRun() throws InterruptedException {
1099 <                    await(done);
1100 <                }};
1101 <            for (int i = 0; i < 2; ++i)
1102 <                p.submit(Executors.callable(task));
1103 <            for (int i = 0; i < 2; ++i) {
1104 <                try {
1118 <                    p.execute(task);
1119 <                    shouldThrow();
1120 <                } catch (RejectedExecutionException success) {}
1121 <                assertTrue(p.getTaskCount() <= 2);
1097 >            // saturate
1098 >            for (int i = saturatedSize; i--> 0; ) {
1099 >                switch (rnd.nextInt(4)) {
1100 >                case 0: p.execute(r); break;
1101 >                case 1: assertFalse(p.submit(r).isDone()); break;
1102 >                case 2: assertFalse(p.submit(r, Boolean.TRUE).isDone()); break;
1103 >                case 3: assertFalse(p.submit(c).isDone()); break;
1104 >                }
1105              }
1123        }
1124    }
1106  
1107 <    /**
1108 <     * executor using CallerRunsPolicy runs task if saturated.
1128 <     */
1129 <    public void testSaturatedExecute2() {
1130 <        final ThreadPoolExecutor p =
1131 <            new ThreadPoolExecutor(1, 1,
1132 <                                   LONG_DELAY_MS,
1133 <                                   MILLISECONDS,
1134 <                                   new ArrayBlockingQueue<Runnable>(1),
1135 <                                   new ThreadPoolExecutor.CallerRunsPolicy());
1136 <        try (PoolCleaner cleaner = cleaner(p)) {
1137 <            final CountDownLatch done = new CountDownLatch(1);
1138 <            Runnable blocker = new CheckedRunnable() {
1139 <                public void realRun() throws InterruptedException {
1140 <                    await(done);
1141 <                }};
1142 <            p.execute(blocker);
1143 <            TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1144 <            for (int i = 0; i < tasks.length; i++)
1145 <                tasks[i] = new TrackedNoOpRunnable();
1146 <            for (int i = 0; i < tasks.length; i++)
1147 <                p.execute(tasks[i]);
1148 <            for (int i = 1; i < tasks.length; i++)
1149 <                assertTrue(tasks[i].done);
1150 <            assertFalse(tasks[0].done); // waiting in queue
1151 <            done.countDown();
1152 <        }
1153 <    }
1107 >            await(threadsStarted);
1108 >            assertTaskSubmissionsAreRejected(p);
1109  
1110 <    /**
1111 <     * executor using DiscardPolicy drops task if saturated.
1112 <     */
1113 <    public void testSaturatedExecute3() {
1114 <        final CountDownLatch done = new CountDownLatch(1);
1115 <        final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1116 <        for (int i = 0; i < tasks.length; ++i)
1117 <            tasks[i] = new TrackedNoOpRunnable();
1118 <        final ThreadPoolExecutor p =
1119 <            new ThreadPoolExecutor(1, 1,
1165 <                          LONG_DELAY_MS, MILLISECONDS,
1166 <                          new ArrayBlockingQueue<Runnable>(1),
1167 <                          new ThreadPoolExecutor.DiscardPolicy());
1168 <        try (PoolCleaner cleaner = cleaner(p, done)) {
1169 <            p.execute(awaiter(done));
1110 >            if (shutdownNow)
1111 >                p.shutdownNow();
1112 >            else
1113 >                p.shutdown();
1114 >            // Pool is shutdown, but not yet terminated
1115 >            assertTaskSubmissionsAreRejected(p);
1116 >            assertFalse(p.isTerminated());
1117 >
1118 >            done.countDown();   // release blocking tasks
1119 >            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
1120  
1121 <            for (TrackedNoOpRunnable task : tasks)
1122 <                p.execute(task);
1123 <            for (int i = 1; i < tasks.length; i++)
1124 <                assertFalse(tasks[i].done);
1125 <        }
1176 <        for (int i = 1; i < tasks.length; i++)
1177 <            assertFalse(tasks[i].done);
1178 <        assertTrue(tasks[0].done); // was waiting in queue
1121 >            assertTaskSubmissionsAreRejected(p);
1122 >        }
1123 >        assertEquals(saturatedSize(p)
1124 >                     - (shutdownNow ? p.getQueue().remainingCapacity() : 0),
1125 >                     p.getCompletedTaskCount());
1126      }
1127  
1128      /**
1129       * executor using DiscardOldestPolicy drops oldest task if saturated.
1130       */
1131 <    public void testSaturatedExecute4() {
1131 >    public void testSaturatedExecute_DiscardOldestPolicy() {
1132          final CountDownLatch done = new CountDownLatch(1);
1133          LatchAwaiter r1 = awaiter(done);
1134          LatchAwaiter r2 = awaiter(done);
# Line 1190 | Line 1137 | public class ThreadPoolExecutorTest exte
1137              new ThreadPoolExecutor(1, 1,
1138                                     LONG_DELAY_MS, MILLISECONDS,
1139                                     new ArrayBlockingQueue<Runnable>(1),
1140 <                                   new ThreadPoolExecutor.DiscardOldestPolicy());
1140 >                                   new DiscardOldestPolicy());
1141          try (PoolCleaner cleaner = cleaner(p, done)) {
1142              assertEquals(LatchAwaiter.NEW, r1.state);
1143              assertEquals(LatchAwaiter.NEW, r2.state);
# Line 1208 | Line 1155 | public class ThreadPoolExecutorTest exte
1155      }
1156  
1157      /**
1211     * execute throws RejectedExecutionException if shutdown
1212     */
1213    public void testRejectedExecutionExceptionOnShutdown() {
1214        final ThreadPoolExecutor p =
1215            new ThreadPoolExecutor(1, 1,
1216                                   LONG_DELAY_MS, MILLISECONDS,
1217                                   new ArrayBlockingQueue<Runnable>(1));
1218        try { p.shutdown(); } catch (SecurityException ok) { return; }
1219        try (PoolCleaner cleaner = cleaner(p)) {
1220            try {
1221                p.execute(new NoOpRunnable());
1222                shouldThrow();
1223            } catch (RejectedExecutionException success) {}
1224        }
1225    }
1226
1227    /**
1228     * execute using CallerRunsPolicy drops task on shutdown
1229     */
1230    public void testCallerRunsOnShutdown() {
1231        RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
1232        final ThreadPoolExecutor p =
1233            new ThreadPoolExecutor(1, 1,
1234                                   LONG_DELAY_MS, MILLISECONDS,
1235                                   new ArrayBlockingQueue<Runnable>(1), h);
1236
1237        try { p.shutdown(); } catch (SecurityException ok) { return; }
1238        try (PoolCleaner cleaner = cleaner(p)) {
1239            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1240            p.execute(r);
1241            assertFalse(r.done);
1242        }
1243    }
1244
1245    /**
1246     * execute using DiscardPolicy drops task on shutdown
1247     */
1248    public void testDiscardOnShutdown() {
1249        final ThreadPoolExecutor p =
1250            new ThreadPoolExecutor(1, 1,
1251                                   LONG_DELAY_MS, MILLISECONDS,
1252                                   new ArrayBlockingQueue<Runnable>(1),
1253                                   new ThreadPoolExecutor.DiscardPolicy());
1254
1255        try { p.shutdown(); } catch (SecurityException ok) { return; }
1256        try (PoolCleaner cleaner = cleaner(p)) {
1257            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1258            p.execute(r);
1259            assertFalse(r.done);
1260        }
1261    }
1262
1263    /**
1158       * execute using DiscardOldestPolicy drops task on shutdown
1159       */
1160      public void testDiscardOldestOnShutdown() {
# Line 1268 | Line 1162 | public class ThreadPoolExecutorTest exte
1162              new ThreadPoolExecutor(1, 1,
1163                                     LONG_DELAY_MS, MILLISECONDS,
1164                                     new ArrayBlockingQueue<Runnable>(1),
1165 <                                   new ThreadPoolExecutor.DiscardOldestPolicy());
1165 >                                   new DiscardOldestPolicy());
1166  
1167          try { p.shutdown(); } catch (SecurityException ok) { return; }
1168          try (PoolCleaner cleaner = cleaner(p)) {
# Line 1279 | Line 1173 | public class ThreadPoolExecutorTest exte
1173      }
1174  
1175      /**
1176 <     * execute(null) throws NPE
1176 >     * Submitting null tasks throws NullPointerException
1177       */
1178 <    public void testExecuteNull() {
1178 >    public void testNullTaskSubmission() {
1179          final ThreadPoolExecutor p =
1180              new ThreadPoolExecutor(1, 2,
1181                                     1L, SECONDS,
1182                                     new ArrayBlockingQueue<Runnable>(10));
1183          try (PoolCleaner cleaner = cleaner(p)) {
1184 <            try {
1291 <                p.execute(null);
1292 <                shouldThrow();
1293 <            } catch (NullPointerException success) {}
1184 >            assertNullTaskSubmissionThrowsNullPointerException(p);
1185          }
1186      }
1187  
# Line 1482 | Line 1373 | public class ThreadPoolExecutorTest exte
1373      }
1374  
1375      /**
1376 <     * invokeAny(empty collection) throws IAE
1376 >     * invokeAny(empty collection) throws IllegalArgumentException
1377       */
1378      public void testInvokeAny2() throws Exception {
1379          final ExecutorService e =
# Line 1572 | Line 1463 | public class ThreadPoolExecutorTest exte
1463      }
1464  
1465      /**
1466 <     * invokeAll(empty collection) returns empty collection
1466 >     * invokeAll(empty collection) returns empty list
1467       */
1468      public void testInvokeAll2() throws InterruptedException {
1469          final ExecutorService e =
1470              new ThreadPoolExecutor(2, 2,
1471                                     LONG_DELAY_MS, MILLISECONDS,
1472                                     new ArrayBlockingQueue<Runnable>(10));
1473 +        final Collection<Callable<String>> emptyCollection
1474 +            = Collections.emptyList();
1475          try (PoolCleaner cleaner = cleaner(e)) {
1476 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1476 >            List<Future<String>> r = e.invokeAll(emptyCollection);
1477              assertTrue(r.isEmpty());
1478          }
1479      }
# Line 1655 | Line 1548 | public class ThreadPoolExecutorTest exte
1548                                     new ArrayBlockingQueue<Runnable>(10));
1549          try (PoolCleaner cleaner = cleaner(e)) {
1550              try {
1551 <                e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1551 >                e.invokeAny(null, randomTimeout(), randomTimeUnit());
1552                  shouldThrow();
1553              } catch (NullPointerException success) {}
1554          }
# Line 1673 | Line 1566 | public class ThreadPoolExecutorTest exte
1566              List<Callable<String>> l = new ArrayList<>();
1567              l.add(new StringTask());
1568              try {
1569 <                e.invokeAny(l, MEDIUM_DELAY_MS, null);
1569 >                e.invokeAny(l, randomTimeout(), null);
1570                  shouldThrow();
1571              } catch (NullPointerException success) {}
1572          }
1573      }
1574  
1575      /**
1576 <     * timed invokeAny(empty collection) throws IAE
1576 >     * timed invokeAny(empty collection) throws IllegalArgumentException
1577       */
1578      public void testTimedInvokeAny2() throws Exception {
1579          final ExecutorService e =
# Line 1690 | Line 1583 | public class ThreadPoolExecutorTest exte
1583          try (PoolCleaner cleaner = cleaner(e)) {
1584              try {
1585                  e.invokeAny(new ArrayList<Callable<String>>(),
1586 <                            MEDIUM_DELAY_MS, MILLISECONDS);
1586 >                            randomTimeout(), randomTimeUnit());
1587                  shouldThrow();
1588              } catch (IllegalArgumentException success) {}
1589          }
1590      }
1591  
1592      /**
1593 <     * timed invokeAny(c) throws NPE if c has null elements
1593 >     * timed invokeAny(c) throws NullPointerException if c has null elements
1594       */
1595      public void testTimedInvokeAny3() throws Exception {
1596          final CountDownLatch latch = new CountDownLatch(1);
# Line 1710 | Line 1603 | public class ThreadPoolExecutorTest exte
1603              l.add(latchAwaitingStringTask(latch));
1604              l.add(null);
1605              try {
1606 <                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1606 >                e.invokeAny(l, randomTimeout(), randomTimeUnit());
1607                  shouldThrow();
1608              } catch (NullPointerException success) {}
1609              latch.countDown();
# Line 1768 | Line 1661 | public class ThreadPoolExecutorTest exte
1661                                     new ArrayBlockingQueue<Runnable>(10));
1662          try (PoolCleaner cleaner = cleaner(e)) {
1663              try {
1664 <                e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1664 >                e.invokeAll(null, randomTimeout(), randomTimeUnit());
1665                  shouldThrow();
1666              } catch (NullPointerException success) {}
1667          }
# Line 1786 | Line 1679 | public class ThreadPoolExecutorTest exte
1679              List<Callable<String>> l = new ArrayList<>();
1680              l.add(new StringTask());
1681              try {
1682 <                e.invokeAll(l, MEDIUM_DELAY_MS, null);
1682 >                e.invokeAll(l, randomTimeout(), null);
1683                  shouldThrow();
1684              } catch (NullPointerException success) {}
1685          }
1686      }
1687  
1688      /**
1689 <     * timed invokeAll(empty collection) returns empty collection
1689 >     * timed invokeAll(empty collection) returns empty list
1690       */
1691      public void testTimedInvokeAll2() throws InterruptedException {
1692          final ExecutorService e =
1693              new ThreadPoolExecutor(2, 2,
1694                                     LONG_DELAY_MS, MILLISECONDS,
1695                                     new ArrayBlockingQueue<Runnable>(10));
1696 +        final Collection<Callable<String>> emptyCollection
1697 +            = Collections.emptyList();
1698          try (PoolCleaner cleaner = cleaner(e)) {
1699 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(),
1700 <                                                 MEDIUM_DELAY_MS, MILLISECONDS);
1699 >            List<Future<String>> r =
1700 >                e.invokeAll(emptyCollection, randomTimeout(), randomTimeUnit());
1701              assertTrue(r.isEmpty());
1702          }
1703      }
# Line 1820 | Line 1715 | public class ThreadPoolExecutorTest exte
1715              l.add(new StringTask());
1716              l.add(null);
1717              try {
1718 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1718 >                e.invokeAll(l, randomTimeout(), randomTimeUnit());
1719                  shouldThrow();
1720              } catch (NullPointerException success) {}
1721          }
# Line 1928 | Line 1823 | public class ThreadPoolExecutorTest exte
1823                      public void realRun() {
1824                          done.countDown();
1825                      }});
1826 <            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1826 >            await(done);
1827          }
1828      }
1829  
# Line 2021 | Line 1916 | public class ThreadPoolExecutorTest exte
1916                  }
1917              }
1918              // enough time to run all tasks
1919 <            assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS));
1919 >            await(done, nTasks * SHORT_DELAY_MS);
1920          }
1921      }
1922  
# Line 2062 | Line 1957 | public class ThreadPoolExecutorTest exte
1957          }
1958      }
1959  
1960 +    /** Directly test simple ThreadPoolExecutor RejectedExecutionHandlers. */
1961 +    public void testStandardRejectedExecutionHandlers() {
1962 +        final ThreadPoolExecutor p =
1963 +            new ThreadPoolExecutor(1, 1, 1, SECONDS,
1964 +                                   new ArrayBlockingQueue<Runnable>(1));
1965 +        final AtomicReference<Thread> thread = new AtomicReference<>();
1966 +        final Runnable r = new Runnable() { public void run() {
1967 +            thread.set(Thread.currentThread()); }};
1968 +
1969 +        try {
1970 +            new AbortPolicy().rejectedExecution(r, p);
1971 +            shouldThrow();
1972 +        } catch (RejectedExecutionException success) {}
1973 +        assertNull(thread.get());
1974 +
1975 +        new DiscardPolicy().rejectedExecution(r, p);
1976 +        assertNull(thread.get());
1977 +
1978 +        new CallerRunsPolicy().rejectedExecution(r, p);
1979 +        assertSame(Thread.currentThread(), thread.get());
1980 +
1981 +        // check that pool was not perturbed by handlers
1982 +        assertTrue(p.getRejectedExecutionHandler() instanceof AbortPolicy);
1983 +        assertEquals(0, p.getTaskCount());
1984 +        assertTrue(p.getQueue().isEmpty());
1985 +    }
1986 +
1987   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines