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

Comparing jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java (file contents):
Revision 1.92 by jsr166, Tue Oct 6 05:56:01 2015 UTC vs.
Revision 1.106 by jsr166, Wed Jan 27 01:57:24 2021 UTC

# Line 10 | Line 10 | import static java.util.concurrent.TimeU
10   import static java.util.concurrent.TimeUnit.SECONDS;
11  
12   import java.util.ArrayList;
13 + import java.util.Collection;
14 + import java.util.Collections;
15   import java.util.List;
16   import java.util.concurrent.ArrayBlockingQueue;
17   import java.util.concurrent.BlockingQueue;
# Line 17 | Line 19 | import java.util.concurrent.Callable;
19   import java.util.concurrent.CancellationException;
20   import java.util.concurrent.CountDownLatch;
21   import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.Executors;
22   import java.util.concurrent.ExecutorService;
23   import java.util.concurrent.Future;
24   import java.util.concurrent.FutureTask;
25   import java.util.concurrent.LinkedBlockingQueue;
25 import java.util.concurrent.RejectedExecutionException;
26   import java.util.concurrent.RejectedExecutionHandler;
27   import java.util.concurrent.RunnableFuture;
28   import java.util.concurrent.SynchronousQueue;
29   import java.util.concurrent.ThreadFactory;
30 + import java.util.concurrent.ThreadLocalRandom;
31   import java.util.concurrent.ThreadPoolExecutor;
32   import java.util.concurrent.TimeoutException;
33   import java.util.concurrent.TimeUnit;
# Line 239 | Line 240 | public class ThreadPoolExecutorSubclassT
240              final Runnable task = new CheckedRunnable() {
241                  public void realRun() { done.countDown(); }};
242              p.execute(task);
243 <            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
243 >            await(done);
244          }
245      }
246  
# Line 333 | Line 334 | public class ThreadPoolExecutorSubclassT
334                  public void realRun() throws InterruptedException {
335                      threadStarted.countDown();
336                      assertEquals(0, p.getCompletedTaskCount());
337 <                    threadProceed.await();
337 >                    await(threadProceed);
338                      threadDone.countDown();
339                  }});
340              await(threadStarted);
341              assertEquals(0, p.getCompletedTaskCount());
342              threadProceed.countDown();
343 <            threadDone.await();
343 >            await(threadDone);
344              long startTime = System.nanoTime();
345              while (p.getCompletedTaskCount() != 1) {
346                  if (millisElapsedSince(startTime) > LONG_DELAY_MS)
# Line 652 | Line 653 | public class ThreadPoolExecutorSubclassT
653       */
654      public void testGetQueue() throws InterruptedException {
655          final CountDownLatch done = new CountDownLatch(1);
656 <        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
656 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
657          final ThreadPoolExecutor p =
658              new CustomTPE(1, 1,
659                            LONG_DELAY_MS, MILLISECONDS,
660                            q);
661          try (PoolCleaner cleaner = cleaner(p, done)) {
662              final CountDownLatch threadStarted = new CountDownLatch(1);
663 <            FutureTask[] tasks = new FutureTask[5];
663 >            FutureTask[] rtasks = new FutureTask[5];
664 >            @SuppressWarnings("unchecked")
665 >            FutureTask<Boolean>[] tasks = (FutureTask<Boolean>[])rtasks;
666              for (int i = 0; i < tasks.length; i++) {
667 <                Callable task = new CheckedCallable<Boolean>() {
667 >                Callable<Boolean> task = new CheckedCallable<>() {
668                      public Boolean realCall() throws InterruptedException {
669                          threadStarted.countDown();
670                          assertSame(q, p.getQueue());
671                          await(done);
672                          return Boolean.TRUE;
673                      }};
674 <                tasks[i] = new FutureTask(task);
674 >                tasks[i] = new FutureTask<>(task);
675                  p.execute(tasks[i]);
676              }
677              await(threadStarted);
# Line 684 | Line 687 | public class ThreadPoolExecutorSubclassT
687       */
688      public void testRemove() throws InterruptedException {
689          final CountDownLatch done = new CountDownLatch(1);
690 <        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
690 >        BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
691          final ThreadPoolExecutor p =
692              new CustomTPE(1, 1,
693                            LONG_DELAY_MS, MILLISECONDS,
# Line 719 | Line 722 | public class ThreadPoolExecutorSubclassT
722      public void testPurge() throws InterruptedException {
723          final CountDownLatch threadStarted = new CountDownLatch(1);
724          final CountDownLatch done = new CountDownLatch(1);
725 <        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
725 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
726          final ThreadPoolExecutor p =
727              new CustomTPE(1, 1,
728                            LONG_DELAY_MS, MILLISECONDS,
729                            q);
730          try (PoolCleaner cleaner = cleaner(p, done)) {
731 <            FutureTask[] tasks = new FutureTask[5];
731 >            FutureTask[] rtasks = new FutureTask[5];
732 >            @SuppressWarnings("unchecked")
733 >            FutureTask<Boolean>[] tasks = (FutureTask<Boolean>[])rtasks;
734              for (int i = 0; i < tasks.length; i++) {
735 <                Callable task = new CheckedCallable<Boolean>() {
735 >                Callable<Boolean> task = new CheckedCallable<>() {
736                      public Boolean realCall() throws InterruptedException {
737                          threadStarted.countDown();
738                          await(done);
739                          return Boolean.TRUE;
740                      }};
741 <                tasks[i] = new FutureTask(task);
741 >                tasks[i] = new FutureTask<>(task);
742                  p.execute(tasks[i]);
743              }
744              await(threadStarted);
# Line 768 | Line 773 | public class ThreadPoolExecutorSubclassT
773          Runnable waiter = new CheckedRunnable() { public void realRun() {
774              threadsStarted.countDown();
775              try {
776 <                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
776 >                MILLISECONDS.sleep(LONGER_DELAY_MS);
777              } catch (InterruptedException success) {}
778              ran.getAndIncrement();
779          }};
# Line 1130 | Line 1135 | public class ThreadPoolExecutorSubclassT
1135      }
1136  
1137      /**
1138 <     * execute throws RejectedExecutionException if saturated.
1138 >     * Submitted tasks are rejected when saturated or shutdown
1139       */
1140 <    public void testSaturatedExecute() {
1136 <        final CountDownLatch done = new CountDownLatch(1);
1140 >    public void testSubmittedTasksRejectedWhenSaturatedOrShutdown() throws InterruptedException {
1141          final ThreadPoolExecutor p =
1142              new CustomTPE(1, 1,
1143                            LONG_DELAY_MS, MILLISECONDS,
1144                            new ArrayBlockingQueue<Runnable>(1));
1145 <        try (PoolCleaner cleaner = cleaner(p, done)) {
1146 <            Runnable task = new CheckedRunnable() {
1147 <                public void realRun() throws InterruptedException {
1148 <                    await(done);
1149 <                }};
1150 <            for (int i = 0; i < 2; ++i)
1151 <                p.execute(task);
1148 <            for (int i = 0; i < 2; ++i) {
1145 >        final int saturatedSize = saturatedSize(p);
1146 >        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
1147 >        final CountDownLatch threadsStarted = new CountDownLatch(p.getMaximumPoolSize());
1148 >        final CountDownLatch done = new CountDownLatch(1);
1149 >        final Runnable r = () -> {
1150 >            threadsStarted.countDown();
1151 >            for (;;) {
1152                  try {
1153 <                    p.execute(task);
1154 <                    shouldThrow();
1155 <                } catch (RejectedExecutionException success) {}
1156 <                assertTrue(p.getTaskCount() <= 2);
1157 <            }
1158 <        }
1159 <    }
1153 >                    done.await();
1154 >                    return;
1155 >                } catch (InterruptedException shutdownNowDeliberatelyIgnored) {}
1156 >            }};
1157 >        final Callable<Boolean> c = () -> {
1158 >            threadsStarted.countDown();
1159 >            for (;;) {
1160 >                try {
1161 >                    done.await();
1162 >                    return Boolean.TRUE;
1163 >                } catch (InterruptedException shutdownNowDeliberatelyIgnored) {}
1164 >            }};
1165 >        final boolean shutdownNow = rnd.nextBoolean();
1166  
1158    /**
1159     * executor using CallerRunsPolicy runs task if saturated.
1160     */
1161    public void testSaturatedExecute2() {
1162        final CountDownLatch done = new CountDownLatch(1);
1163        final ThreadPoolExecutor p =
1164            new CustomTPE(1, 1,
1165                          LONG_DELAY_MS, MILLISECONDS,
1166                          new ArrayBlockingQueue<Runnable>(1),
1167                          new CustomTPE.CallerRunsPolicy());
1167          try (PoolCleaner cleaner = cleaner(p, done)) {
1168 <            Runnable blocker = new CheckedRunnable() {
1169 <                public void realRun() throws InterruptedException {
1170 <                    await(done);
1171 <                }};
1172 <            p.execute(blocker);
1173 <            TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1174 <            for (int i = 0; i < tasks.length; i++)
1175 <                tasks[i] = new TrackedNoOpRunnable();
1176 <            for (int i = 0; i < tasks.length; i++)
1178 <                p.execute(tasks[i]);
1179 <            for (int i = 1; i < tasks.length; i++)
1180 <                assertTrue(tasks[i].done);
1181 <            assertFalse(tasks[0].done); // waiting in queue
1182 <        }
1183 <    }
1168 >            // saturate
1169 >            for (int i = saturatedSize; i--> 0; ) {
1170 >                switch (rnd.nextInt(4)) {
1171 >                case 0: p.execute(r); break;
1172 >                case 1: assertFalse(p.submit(r).isDone()); break;
1173 >                case 2: assertFalse(p.submit(r, Boolean.TRUE).isDone()); break;
1174 >                case 3: assertFalse(p.submit(c).isDone()); break;
1175 >                }
1176 >            }
1177  
1178 <    /**
1179 <     * executor using DiscardPolicy drops task if saturated.
1187 <     */
1188 <    public void testSaturatedExecute3() {
1189 <        final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1190 <        for (int i = 0; i < tasks.length; ++i)
1191 <            tasks[i] = new TrackedNoOpRunnable();
1192 <        final CountDownLatch done = new CountDownLatch(1);
1193 <        final ThreadPoolExecutor p =
1194 <            new CustomTPE(1, 1,
1195 <                          LONG_DELAY_MS, MILLISECONDS,
1196 <                          new ArrayBlockingQueue<Runnable>(1),
1197 <                          new CustomTPE.DiscardPolicy());
1198 <        try (PoolCleaner cleaner = cleaner(p, done)) {
1199 <            p.execute(awaiter(done));
1178 >            await(threadsStarted);
1179 >            assertTaskSubmissionsAreRejected(p);
1180  
1181 <            for (TrackedNoOpRunnable task : tasks)
1182 <                p.execute(task);
1183 <            for (int i = 1; i < tasks.length; i++)
1184 <                assertFalse(tasks[i].done);
1185 <        }
1186 <        for (int i = 1; i < tasks.length; i++)
1187 <            assertFalse(tasks[i].done);
1188 <        assertTrue(tasks[0].done); // was waiting in queue
1181 >            if (shutdownNow)
1182 >                p.shutdownNow();
1183 >            else
1184 >                p.shutdown();
1185 >            // Pool is shutdown, but not yet terminated
1186 >            assertTaskSubmissionsAreRejected(p);
1187 >            assertFalse(p.isTerminated());
1188 >
1189 >            done.countDown();   // release blocking tasks
1190 >            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
1191 >
1192 >            assertTaskSubmissionsAreRejected(p);
1193 >        }
1194 >        assertEquals(saturatedSize(p)
1195 >                     - (shutdownNow ? p.getQueue().remainingCapacity() : 0),
1196 >                     p.getCompletedTaskCount());
1197      }
1198  
1199      /**
1200       * executor using DiscardOldestPolicy drops oldest task if saturated.
1201       */
1202 <    public void testSaturatedExecute4() {
1202 >    public void testSaturatedExecute_DiscardOldestPolicy() {
1203          final CountDownLatch done = new CountDownLatch(1);
1204          LatchAwaiter r1 = awaiter(done);
1205          LatchAwaiter r2 = awaiter(done);
# Line 1220 | Line 1208 | public class ThreadPoolExecutorSubclassT
1208              new CustomTPE(1, 1,
1209                            LONG_DELAY_MS, MILLISECONDS,
1210                            new ArrayBlockingQueue<Runnable>(1),
1211 <                          new CustomTPE.DiscardOldestPolicy());
1211 >                          new ThreadPoolExecutor.DiscardOldestPolicy());
1212          try (PoolCleaner cleaner = cleaner(p, done)) {
1213              assertEquals(LatchAwaiter.NEW, r1.state);
1214              assertEquals(LatchAwaiter.NEW, r2.state);
# Line 1238 | Line 1226 | public class ThreadPoolExecutorSubclassT
1226      }
1227  
1228      /**
1241     * execute throws RejectedExecutionException if shutdown
1242     */
1243    public void testRejectedExecutionExceptionOnShutdown() {
1244        final ThreadPoolExecutor p =
1245            new CustomTPE(1, 1,
1246                          LONG_DELAY_MS, MILLISECONDS,
1247                          new ArrayBlockingQueue<Runnable>(1));
1248        try { p.shutdown(); } catch (SecurityException ok) { return; }
1249        try (PoolCleaner cleaner = cleaner(p)) {
1250            try {
1251                p.execute(new NoOpRunnable());
1252                shouldThrow();
1253            } catch (RejectedExecutionException success) {}
1254        }
1255    }
1256
1257    /**
1258     * execute using CallerRunsPolicy drops task on shutdown
1259     */
1260    public void testCallerRunsOnShutdown() {
1261        final ThreadPoolExecutor p =
1262            new CustomTPE(1, 1,
1263                          LONG_DELAY_MS, MILLISECONDS,
1264                          new ArrayBlockingQueue<Runnable>(1),
1265                          new CustomTPE.CallerRunsPolicy());
1266        try { p.shutdown(); } catch (SecurityException ok) { return; }
1267        try (PoolCleaner cleaner = cleaner(p)) {
1268            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1269            p.execute(r);
1270            assertFalse(r.done);
1271        }
1272    }
1273
1274    /**
1275     * execute using DiscardPolicy drops task on shutdown
1276     */
1277    public void testDiscardOnShutdown() {
1278        final ThreadPoolExecutor p =
1279            new CustomTPE(1, 1,
1280                          LONG_DELAY_MS, MILLISECONDS,
1281                          new ArrayBlockingQueue<Runnable>(1),
1282                          new CustomTPE.DiscardPolicy());
1283        try { p.shutdown(); } catch (SecurityException ok) { return; }
1284        try (PoolCleaner cleaner = cleaner(p)) {
1285            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1286            p.execute(r);
1287            assertFalse(r.done);
1288        }
1289    }
1290
1291    /**
1229       * execute using DiscardOldestPolicy drops task on shutdown
1230       */
1231      public void testDiscardOldestOnShutdown() {
# Line 1296 | Line 1233 | public class ThreadPoolExecutorSubclassT
1233              new CustomTPE(1, 1,
1234                            LONG_DELAY_MS, MILLISECONDS,
1235                            new ArrayBlockingQueue<Runnable>(1),
1236 <                          new CustomTPE.DiscardOldestPolicy());
1236 >                          new ThreadPoolExecutor.DiscardOldestPolicy());
1237  
1238          try { p.shutdown(); } catch (SecurityException ok) { return; }
1239          try (PoolCleaner cleaner = cleaner(p)) {
# Line 1307 | Line 1244 | public class ThreadPoolExecutorSubclassT
1244      }
1245  
1246      /**
1247 <     * execute(null) throws NPE
1247 >     * Submitting null tasks throws NullPointerException
1248       */
1249 <    public void testExecuteNull() {
1249 >    public void testNullTaskSubmission() {
1250          final ThreadPoolExecutor p =
1251              new CustomTPE(1, 2,
1252                            1L, SECONDS,
1253                            new ArrayBlockingQueue<Runnable>(10));
1254          try (PoolCleaner cleaner = cleaner(p)) {
1255 <            try {
1319 <                p.execute(null);
1320 <                shouldThrow();
1321 <            } catch (NullPointerException success) {}
1255 >            assertNullTaskSubmissionThrowsNullPointerException(p);
1256          }
1257      }
1258  
# Line 1362 | Line 1296 | public class ThreadPoolExecutorSubclassT
1296      public void testMaximumPoolSizeIllegalArgumentException2() {
1297          final ThreadPoolExecutor p =
1298              new CustomTPE(2, 3,
1299 <                          LONG_DELAY_MS,
1300 <                          MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1299 >                          LONG_DELAY_MS, MILLISECONDS,
1300 >                          new ArrayBlockingQueue<Runnable>(10));
1301          try (PoolCleaner cleaner = cleaner(p)) {
1302              try {
1303                  p.setMaximumPoolSize(-1);
# Line 1465 | Line 1399 | public class ThreadPoolExecutorSubclassT
1399      }
1400  
1401      /**
1402 <     * invokeAny(null) throws NPE
1402 >     * invokeAny(null) throws NullPointerException
1403       */
1404      public void testInvokeAny1() throws Exception {
1405          final ExecutorService e =
# Line 1481 | Line 1415 | public class ThreadPoolExecutorSubclassT
1415      }
1416  
1417      /**
1418 <     * invokeAny(empty collection) throws IAE
1418 >     * invokeAny(empty collection) throws IllegalArgumentException
1419       */
1420      public void testInvokeAny2() throws Exception {
1421          final ExecutorService e =
# Line 1506 | Line 1440 | public class ThreadPoolExecutorSubclassT
1440                            LONG_DELAY_MS, MILLISECONDS,
1441                            new ArrayBlockingQueue<Runnable>(10));
1442          try (PoolCleaner cleaner = cleaner(e)) {
1443 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1443 >            List<Callable<String>> l = new ArrayList<>();
1444              l.add(latchAwaitingStringTask(latch));
1445              l.add(null);
1446              try {
# Line 1526 | Line 1460 | public class ThreadPoolExecutorSubclassT
1460                            LONG_DELAY_MS, MILLISECONDS,
1461                            new ArrayBlockingQueue<Runnable>(10));
1462          try (PoolCleaner cleaner = cleaner(e)) {
1463 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1463 >            List<Callable<String>> l = new ArrayList<>();
1464              l.add(new NPETask());
1465              try {
1466                  e.invokeAny(l);
# Line 1546 | Line 1480 | public class ThreadPoolExecutorSubclassT
1480                            LONG_DELAY_MS, MILLISECONDS,
1481                            new ArrayBlockingQueue<Runnable>(10));
1482          try (PoolCleaner cleaner = cleaner(e)) {
1483 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1483 >            List<Callable<String>> l = new ArrayList<>();
1484              l.add(new StringTask());
1485              l.add(new StringTask());
1486              String result = e.invokeAny(l);
# Line 1571 | Line 1505 | public class ThreadPoolExecutorSubclassT
1505      }
1506  
1507      /**
1508 <     * invokeAll(empty collection) returns empty collection
1508 >     * invokeAll(empty collection) returns empty list
1509       */
1510      public void testInvokeAll2() throws Exception {
1511          final ExecutorService e =
1512              new CustomTPE(2, 2,
1513                            LONG_DELAY_MS, MILLISECONDS,
1514                            new ArrayBlockingQueue<Runnable>(10));
1515 +        final Collection<Callable<String>> emptyCollection
1516 +            = Collections.emptyList();
1517          try (PoolCleaner cleaner = cleaner(e)) {
1518 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1518 >            List<Future<String>> r = e.invokeAll(emptyCollection);
1519              assertTrue(r.isEmpty());
1520          }
1521      }
# Line 1593 | Line 1529 | public class ThreadPoolExecutorSubclassT
1529                            LONG_DELAY_MS, MILLISECONDS,
1530                            new ArrayBlockingQueue<Runnable>(10));
1531          try (PoolCleaner cleaner = cleaner(e)) {
1532 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1532 >            List<Callable<String>> l = new ArrayList<>();
1533              l.add(new StringTask());
1534              l.add(null);
1535              try {
# Line 1612 | Line 1548 | public class ThreadPoolExecutorSubclassT
1548                            LONG_DELAY_MS, MILLISECONDS,
1549                            new ArrayBlockingQueue<Runnable>(10));
1550          try (PoolCleaner cleaner = cleaner(e)) {
1551 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1551 >            List<Callable<String>> l = new ArrayList<>();
1552              l.add(new NPETask());
1553              List<Future<String>> futures = e.invokeAll(l);
1554              assertEquals(1, futures.size());
# Line 1634 | Line 1570 | public class ThreadPoolExecutorSubclassT
1570                            LONG_DELAY_MS, MILLISECONDS,
1571                            new ArrayBlockingQueue<Runnable>(10));
1572          try (PoolCleaner cleaner = cleaner(e)) {
1573 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1573 >            List<Callable<String>> l = new ArrayList<>();
1574              l.add(new StringTask());
1575              l.add(new StringTask());
1576              List<Future<String>> futures = e.invokeAll(l);
# Line 1654 | Line 1590 | public class ThreadPoolExecutorSubclassT
1590                            new ArrayBlockingQueue<Runnable>(10));
1591          try (PoolCleaner cleaner = cleaner(e)) {
1592              try {
1593 <                e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1593 >                e.invokeAny(null, randomTimeout(), randomTimeUnit());
1594                  shouldThrow();
1595              } catch (NullPointerException success) {}
1596          }
# Line 1669 | Line 1605 | public class ThreadPoolExecutorSubclassT
1605                            LONG_DELAY_MS, MILLISECONDS,
1606                            new ArrayBlockingQueue<Runnable>(10));
1607          try (PoolCleaner cleaner = cleaner(e)) {
1608 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1608 >            List<Callable<String>> l = new ArrayList<>();
1609              l.add(new StringTask());
1610              try {
1611 <                e.invokeAny(l, MEDIUM_DELAY_MS, null);
1611 >                e.invokeAny(l, randomTimeout(), null);
1612                  shouldThrow();
1613              } catch (NullPointerException success) {}
1614          }
1615      }
1616  
1617      /**
1618 <     * timed invokeAny(empty collection) throws IAE
1618 >     * timed invokeAny(empty collection) throws IllegalArgumentException
1619       */
1620      public void testTimedInvokeAny2() throws Exception {
1621          final ExecutorService e =
1622              new CustomTPE(2, 2,
1623                            LONG_DELAY_MS, MILLISECONDS,
1624                            new ArrayBlockingQueue<Runnable>(10));
1625 +        final Collection<Callable<String>> emptyCollection
1626 +            = Collections.emptyList();
1627          try (PoolCleaner cleaner = cleaner(e)) {
1628              try {
1629 <                e.invokeAny(new ArrayList<Callable<String>>(),
1692 <                            MEDIUM_DELAY_MS, MILLISECONDS);
1629 >                e.invokeAny(emptyCollection, randomTimeout(), randomTimeUnit());
1630                  shouldThrow();
1631              } catch (IllegalArgumentException success) {}
1632          }
# Line 1705 | Line 1642 | public class ThreadPoolExecutorSubclassT
1642                            LONG_DELAY_MS, MILLISECONDS,
1643                            new ArrayBlockingQueue<Runnable>(10));
1644          try (PoolCleaner cleaner = cleaner(e)) {
1645 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1645 >            List<Callable<String>> l = new ArrayList<>();
1646              l.add(latchAwaitingStringTask(latch));
1647              l.add(null);
1648              try {
1649 <                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1649 >                e.invokeAny(l, randomTimeout(), randomTimeUnit());
1650                  shouldThrow();
1651              } catch (NullPointerException success) {}
1652              latch.countDown();
# Line 1725 | Line 1662 | public class ThreadPoolExecutorSubclassT
1662                            LONG_DELAY_MS, MILLISECONDS,
1663                            new ArrayBlockingQueue<Runnable>(10));
1664          try (PoolCleaner cleaner = cleaner(e)) {
1665 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1665 >            long startTime = System.nanoTime();
1666 >            List<Callable<String>> l = new ArrayList<>();
1667              l.add(new NPETask());
1668              try {
1669 <                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1669 >                e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1670                  shouldThrow();
1671              } catch (ExecutionException success) {
1672                  assertTrue(success.getCause() instanceof NullPointerException);
1673              }
1674 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1675          }
1676      }
1677  
# Line 1745 | Line 1684 | public class ThreadPoolExecutorSubclassT
1684                            LONG_DELAY_MS, MILLISECONDS,
1685                            new ArrayBlockingQueue<Runnable>(10));
1686          try (PoolCleaner cleaner = cleaner(e)) {
1687 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1687 >            long startTime = System.nanoTime();
1688 >            List<Callable<String>> l = new ArrayList<>();
1689              l.add(new StringTask());
1690              l.add(new StringTask());
1691 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1691 >            String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1692              assertSame(TEST_STRING, result);
1693 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1694          }
1695      }
1696  
# Line 1763 | Line 1704 | public class ThreadPoolExecutorSubclassT
1704                            new ArrayBlockingQueue<Runnable>(10));
1705          try (PoolCleaner cleaner = cleaner(e)) {
1706              try {
1707 <                e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1707 >                e.invokeAll(null, randomTimeout(), randomTimeUnit());
1708                  shouldThrow();
1709              } catch (NullPointerException success) {}
1710          }
# Line 1778 | Line 1719 | public class ThreadPoolExecutorSubclassT
1719                            LONG_DELAY_MS, MILLISECONDS,
1720                            new ArrayBlockingQueue<Runnable>(10));
1721          try (PoolCleaner cleaner = cleaner(e)) {
1722 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1722 >            List<Callable<String>> l = new ArrayList<>();
1723              l.add(new StringTask());
1724              try {
1725 <                e.invokeAll(l, MEDIUM_DELAY_MS, null);
1725 >                e.invokeAll(l, randomTimeout(), null);
1726                  shouldThrow();
1727              } catch (NullPointerException success) {}
1728          }
1729      }
1730  
1731      /**
1732 <     * timed invokeAll(empty collection) returns empty collection
1732 >     * timed invokeAll(empty collection) returns empty list
1733       */
1734      public void testTimedInvokeAll2() throws Exception {
1735          final ExecutorService e =
1736              new CustomTPE(2, 2,
1737                            LONG_DELAY_MS, MILLISECONDS,
1738                            new ArrayBlockingQueue<Runnable>(10));
1739 +        final Collection<Callable<String>> emptyCollection
1740 +            = Collections.emptyList();
1741          try (PoolCleaner cleaner = cleaner(e)) {
1742 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(),
1743 <                                                 MEDIUM_DELAY_MS, MILLISECONDS);
1742 >            List<Future<String>> r =
1743 >                e.invokeAll(emptyCollection, randomTimeout(), randomTimeUnit());
1744              assertTrue(r.isEmpty());
1745          }
1746      }
# Line 1811 | Line 1754 | public class ThreadPoolExecutorSubclassT
1754                            LONG_DELAY_MS, MILLISECONDS,
1755                            new ArrayBlockingQueue<Runnable>(10));
1756          try (PoolCleaner cleaner = cleaner(e)) {
1757 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1757 >            List<Callable<String>> l = new ArrayList<>();
1758              l.add(new StringTask());
1759              l.add(null);
1760              try {
1761 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1761 >                e.invokeAll(l, randomTimeout(), randomTimeUnit());
1762                  shouldThrow();
1763              } catch (NullPointerException success) {}
1764          }
# Line 1830 | Line 1773 | public class ThreadPoolExecutorSubclassT
1773                            LONG_DELAY_MS, MILLISECONDS,
1774                            new ArrayBlockingQueue<Runnable>(10));
1775          try (PoolCleaner cleaner = cleaner(e)) {
1776 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1776 >            List<Callable<String>> l = new ArrayList<>();
1777              l.add(new NPETask());
1778              List<Future<String>> futures =
1779                  e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
# Line 1853 | Line 1796 | public class ThreadPoolExecutorSubclassT
1796                            LONG_DELAY_MS, MILLISECONDS,
1797                            new ArrayBlockingQueue<Runnable>(10));
1798          try (PoolCleaner cleaner = cleaner(e)) {
1799 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1799 >            List<Callable<String>> l = new ArrayList<>();
1800              l.add(new StringTask());
1801              l.add(new StringTask());
1802              List<Future<String>> futures =
# Line 1868 | Line 1811 | public class ThreadPoolExecutorSubclassT
1811       * timed invokeAll(c) cancels tasks not completed by timeout
1812       */
1813      public void testTimedInvokeAll6() throws Exception {
1814 <        final ExecutorService e =
1815 <            new CustomTPE(2, 2,
1816 <                          LONG_DELAY_MS, MILLISECONDS,
1817 <                          new ArrayBlockingQueue<Runnable>(10));
1818 <        try (PoolCleaner cleaner = cleaner(e)) {
1819 <            for (long timeout = timeoutMillis();;) {
1814 >        for (long timeout = timeoutMillis();;) {
1815 >            final CountDownLatch done = new CountDownLatch(1);
1816 >            final Callable<String> waiter = new CheckedCallable<>() {
1817 >                public String realCall() {
1818 >                    try { done.await(LONG_DELAY_MS, MILLISECONDS); }
1819 >                    catch (InterruptedException ok) {}
1820 >                    return "1"; }};
1821 >            final ExecutorService p =
1822 >                new CustomTPE(2, 2,
1823 >                              LONG_DELAY_MS, MILLISECONDS,
1824 >                              new ArrayBlockingQueue<Runnable>(10));
1825 >            try (PoolCleaner cleaner = cleaner(p, done)) {
1826                  List<Callable<String>> tasks = new ArrayList<>();
1827                  tasks.add(new StringTask("0"));
1828 <                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1828 >                tasks.add(waiter);
1829                  tasks.add(new StringTask("2"));
1830                  long startTime = System.nanoTime();
1831                  List<Future<String>> futures =
1832 <                    e.invokeAll(tasks, timeout, MILLISECONDS);
1832 >                    p.invokeAll(tasks, timeout, MILLISECONDS);
1833                  assertEquals(tasks.size(), futures.size());
1834                  assertTrue(millisElapsedSince(startTime) >= timeout);
1835 <                for (Future future : futures)
1835 >                for (Future<?> future : futures)
1836                      assertTrue(future.isDone());
1837                  assertTrue(futures.get(1).isCancelled());
1838                  try {
# Line 1917 | Line 1866 | public class ThreadPoolExecutorSubclassT
1866                      public void realRun() {
1867                          done.countDown();
1868                      }});
1869 <            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1869 >            await(done);
1870          }
1871      }
1872  
# Line 2021 | Line 1970 | public class ThreadPoolExecutorSubclassT
1970              }
1971          }
1972      }
1973 +    @SuppressWarnings("deprecation")
1974 +    public void testFinalizeMethodCallsSuperFinalize() {
1975 +        new CustomTPE(1, 1,
1976 +                      LONG_DELAY_MS, MILLISECONDS,
1977 +                      new LinkedBlockingQueue<Runnable>()) {
1978 +
1979 +            /**
1980 +             * A finalize method without "throws Throwable", that
1981 +             * calls super.finalize().
1982 +             */
1983 +            protected void finalize() {
1984 +                super.finalize();
1985 +            }
1986 +        }.shutdown();
1987 +    }
1988  
1989   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines