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.103 by jsr166, Sun Aug 11 22:29:27 2019 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,
# Line 661 | Line 662 | public class ThreadPoolExecutorSubclassT
662              final CountDownLatch threadStarted = new CountDownLatch(1);
663              FutureTask[] tasks = new FutureTask[5];
664              for (int i = 0; i < tasks.length; i++) {
665 <                Callable task = new CheckedCallable<Boolean>() {
665 >                Callable<Boolean> task = new CheckedCallable<Boolean>() {
666                      public Boolean realCall() throws InterruptedException {
667                          threadStarted.countDown();
668                          assertSame(q, p.getQueue());
# Line 684 | Line 685 | public class ThreadPoolExecutorSubclassT
685       */
686      public void testRemove() throws InterruptedException {
687          final CountDownLatch done = new CountDownLatch(1);
688 <        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
688 >        BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
689          final ThreadPoolExecutor p =
690              new CustomTPE(1, 1,
691                            LONG_DELAY_MS, MILLISECONDS,
# Line 719 | Line 720 | public class ThreadPoolExecutorSubclassT
720      public void testPurge() throws InterruptedException {
721          final CountDownLatch threadStarted = new CountDownLatch(1);
722          final CountDownLatch done = new CountDownLatch(1);
723 <        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
723 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
724          final ThreadPoolExecutor p =
725              new CustomTPE(1, 1,
726                            LONG_DELAY_MS, MILLISECONDS,
# Line 727 | Line 728 | public class ThreadPoolExecutorSubclassT
728          try (PoolCleaner cleaner = cleaner(p, done)) {
729              FutureTask[] tasks = new FutureTask[5];
730              for (int i = 0; i < tasks.length; i++) {
731 <                Callable task = new CheckedCallable<Boolean>() {
731 >                Callable<Boolean> task = new CheckedCallable<Boolean>() {
732                      public Boolean realCall() throws InterruptedException {
733                          threadStarted.countDown();
734                          await(done);
# Line 1130 | Line 1131 | public class ThreadPoolExecutorSubclassT
1131      }
1132  
1133      /**
1134 <     * execute throws RejectedExecutionException if saturated.
1134 >     * Submitted tasks are rejected when saturated or shutdown
1135       */
1136 <    public void testSaturatedExecute() {
1136 <        final CountDownLatch done = new CountDownLatch(1);
1136 >    public void testSubmittedTasksRejectedWhenSaturatedOrShutdown() throws InterruptedException {
1137          final ThreadPoolExecutor p =
1138              new CustomTPE(1, 1,
1139                            LONG_DELAY_MS, MILLISECONDS,
1140                            new ArrayBlockingQueue<Runnable>(1));
1141 <        try (PoolCleaner cleaner = cleaner(p, done)) {
1142 <            Runnable task = new CheckedRunnable() {
1143 <                public void realRun() throws InterruptedException {
1144 <                    await(done);
1145 <                }};
1146 <            for (int i = 0; i < 2; ++i)
1147 <                p.execute(task);
1148 <            for (int i = 0; i < 2; ++i) {
1141 >        final int saturatedSize = saturatedSize(p);
1142 >        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
1143 >        final CountDownLatch threadsStarted = new CountDownLatch(p.getMaximumPoolSize());
1144 >        final CountDownLatch done = new CountDownLatch(1);
1145 >        final Runnable r = () -> {
1146 >            threadsStarted.countDown();
1147 >            for (;;) {
1148                  try {
1149 <                    p.execute(task);
1150 <                    shouldThrow();
1151 <                } catch (RejectedExecutionException success) {}
1152 <                assertTrue(p.getTaskCount() <= 2);
1153 <            }
1154 <        }
1155 <    }
1149 >                    done.await();
1150 >                    return;
1151 >                } catch (InterruptedException shutdownNowDeliberatelyIgnored) {}
1152 >            }};
1153 >        final Callable<Boolean> c = () -> {
1154 >            threadsStarted.countDown();
1155 >            for (;;) {
1156 >                try {
1157 >                    done.await();
1158 >                    return Boolean.TRUE;
1159 >                } catch (InterruptedException shutdownNowDeliberatelyIgnored) {}
1160 >            }};
1161 >        final boolean shutdownNow = rnd.nextBoolean();
1162  
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());
1163          try (PoolCleaner cleaner = cleaner(p, done)) {
1164 <            Runnable blocker = new CheckedRunnable() {
1165 <                public void realRun() throws InterruptedException {
1166 <                    await(done);
1167 <                }};
1168 <            p.execute(blocker);
1169 <            TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1170 <            for (int i = 0; i < tasks.length; i++)
1171 <                tasks[i] = new TrackedNoOpRunnable();
1172 <            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 <    }
1164 >            // saturate
1165 >            for (int i = saturatedSize; i--> 0; ) {
1166 >                switch (rnd.nextInt(4)) {
1167 >                case 0: p.execute(r); break;
1168 >                case 1: assertFalse(p.submit(r).isDone()); break;
1169 >                case 2: assertFalse(p.submit(r, Boolean.TRUE).isDone()); break;
1170 >                case 3: assertFalse(p.submit(c).isDone()); break;
1171 >                }
1172 >            }
1173  
1174 <    /**
1175 <     * executor using DiscardPolicy drops task if saturated.
1176 <     */
1177 <    public void testSaturatedExecute3() {
1178 <        final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1179 <        for (int i = 0; i < tasks.length; ++i)
1180 <            tasks[i] = new TrackedNoOpRunnable();
1181 <        final CountDownLatch done = new CountDownLatch(1);
1182 <        final ThreadPoolExecutor p =
1183 <            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));
1174 >            await(threadsStarted);
1175 >            assertTaskSubmissionsAreRejected(p);
1176 >
1177 >            if (shutdownNow)
1178 >                p.shutdownNow();
1179 >            else
1180 >                p.shutdown();
1181 >            // Pool is shutdown, but not yet terminated
1182 >            assertTaskSubmissionsAreRejected(p);
1183 >            assertFalse(p.isTerminated());
1184  
1185 <            for (TrackedNoOpRunnable task : tasks)
1186 <                p.execute(task);
1187 <            for (int i = 1; i < tasks.length; i++)
1188 <                assertFalse(tasks[i].done);
1189 <        }
1190 <        for (int i = 1; i < tasks.length; i++)
1191 <            assertFalse(tasks[i].done);
1192 <        assertTrue(tasks[0].done); // was waiting in queue
1185 >            done.countDown();   // release blocking tasks
1186 >            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
1187 >
1188 >            assertTaskSubmissionsAreRejected(p);
1189 >        }
1190 >        assertEquals(saturatedSize(p)
1191 >                     - (shutdownNow ? p.getQueue().remainingCapacity() : 0),
1192 >                     p.getCompletedTaskCount());
1193      }
1194  
1195      /**
1196       * executor using DiscardOldestPolicy drops oldest task if saturated.
1197       */
1198 <    public void testSaturatedExecute4() {
1198 >    public void testSaturatedExecute_DiscardOldestPolicy() {
1199          final CountDownLatch done = new CountDownLatch(1);
1200          LatchAwaiter r1 = awaiter(done);
1201          LatchAwaiter r2 = awaiter(done);
# Line 1220 | Line 1204 | public class ThreadPoolExecutorSubclassT
1204              new CustomTPE(1, 1,
1205                            LONG_DELAY_MS, MILLISECONDS,
1206                            new ArrayBlockingQueue<Runnable>(1),
1207 <                          new CustomTPE.DiscardOldestPolicy());
1207 >                          new ThreadPoolExecutor.DiscardOldestPolicy());
1208          try (PoolCleaner cleaner = cleaner(p, done)) {
1209              assertEquals(LatchAwaiter.NEW, r1.state);
1210              assertEquals(LatchAwaiter.NEW, r2.state);
# Line 1238 | Line 1222 | public class ThreadPoolExecutorSubclassT
1222      }
1223  
1224      /**
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    /**
1225       * execute using DiscardOldestPolicy drops task on shutdown
1226       */
1227      public void testDiscardOldestOnShutdown() {
# Line 1296 | Line 1229 | public class ThreadPoolExecutorSubclassT
1229              new CustomTPE(1, 1,
1230                            LONG_DELAY_MS, MILLISECONDS,
1231                            new ArrayBlockingQueue<Runnable>(1),
1232 <                          new CustomTPE.DiscardOldestPolicy());
1232 >                          new ThreadPoolExecutor.DiscardOldestPolicy());
1233  
1234          try { p.shutdown(); } catch (SecurityException ok) { return; }
1235          try (PoolCleaner cleaner = cleaner(p)) {
# Line 1307 | Line 1240 | public class ThreadPoolExecutorSubclassT
1240      }
1241  
1242      /**
1243 <     * execute(null) throws NPE
1243 >     * Submitting null tasks throws NullPointerException
1244       */
1245 <    public void testExecuteNull() {
1245 >    public void testNullTaskSubmission() {
1246          final ThreadPoolExecutor p =
1247              new CustomTPE(1, 2,
1248                            1L, SECONDS,
1249                            new ArrayBlockingQueue<Runnable>(10));
1250          try (PoolCleaner cleaner = cleaner(p)) {
1251 <            try {
1319 <                p.execute(null);
1320 <                shouldThrow();
1321 <            } catch (NullPointerException success) {}
1251 >            assertNullTaskSubmissionThrowsNullPointerException(p);
1252          }
1253      }
1254  
# Line 1362 | Line 1292 | public class ThreadPoolExecutorSubclassT
1292      public void testMaximumPoolSizeIllegalArgumentException2() {
1293          final ThreadPoolExecutor p =
1294              new CustomTPE(2, 3,
1295 <                          LONG_DELAY_MS,
1296 <                          MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1295 >                          LONG_DELAY_MS, MILLISECONDS,
1296 >                          new ArrayBlockingQueue<Runnable>(10));
1297          try (PoolCleaner cleaner = cleaner(p)) {
1298              try {
1299                  p.setMaximumPoolSize(-1);
# Line 1465 | Line 1395 | public class ThreadPoolExecutorSubclassT
1395      }
1396  
1397      /**
1398 <     * invokeAny(null) throws NPE
1398 >     * invokeAny(null) throws NullPointerException
1399       */
1400      public void testInvokeAny1() throws Exception {
1401          final ExecutorService e =
# Line 1481 | Line 1411 | public class ThreadPoolExecutorSubclassT
1411      }
1412  
1413      /**
1414 <     * invokeAny(empty collection) throws IAE
1414 >     * invokeAny(empty collection) throws IllegalArgumentException
1415       */
1416      public void testInvokeAny2() throws Exception {
1417          final ExecutorService e =
# Line 1506 | Line 1436 | public class ThreadPoolExecutorSubclassT
1436                            LONG_DELAY_MS, MILLISECONDS,
1437                            new ArrayBlockingQueue<Runnable>(10));
1438          try (PoolCleaner cleaner = cleaner(e)) {
1439 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1439 >            List<Callable<String>> l = new ArrayList<>();
1440              l.add(latchAwaitingStringTask(latch));
1441              l.add(null);
1442              try {
# Line 1526 | Line 1456 | public class ThreadPoolExecutorSubclassT
1456                            LONG_DELAY_MS, MILLISECONDS,
1457                            new ArrayBlockingQueue<Runnable>(10));
1458          try (PoolCleaner cleaner = cleaner(e)) {
1459 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1459 >            List<Callable<String>> l = new ArrayList<>();
1460              l.add(new NPETask());
1461              try {
1462                  e.invokeAny(l);
# Line 1546 | Line 1476 | public class ThreadPoolExecutorSubclassT
1476                            LONG_DELAY_MS, MILLISECONDS,
1477                            new ArrayBlockingQueue<Runnable>(10));
1478          try (PoolCleaner cleaner = cleaner(e)) {
1479 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1479 >            List<Callable<String>> l = new ArrayList<>();
1480              l.add(new StringTask());
1481              l.add(new StringTask());
1482              String result = e.invokeAny(l);
# Line 1571 | Line 1501 | public class ThreadPoolExecutorSubclassT
1501      }
1502  
1503      /**
1504 <     * invokeAll(empty collection) returns empty collection
1504 >     * invokeAll(empty collection) returns empty list
1505       */
1506      public void testInvokeAll2() throws Exception {
1507          final ExecutorService e =
1508              new CustomTPE(2, 2,
1509                            LONG_DELAY_MS, MILLISECONDS,
1510                            new ArrayBlockingQueue<Runnable>(10));
1511 +        final Collection<Callable<String>> emptyCollection
1512 +            = Collections.emptyList();
1513          try (PoolCleaner cleaner = cleaner(e)) {
1514 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1514 >            List<Future<String>> r = e.invokeAll(emptyCollection);
1515              assertTrue(r.isEmpty());
1516          }
1517      }
# Line 1593 | Line 1525 | public class ThreadPoolExecutorSubclassT
1525                            LONG_DELAY_MS, MILLISECONDS,
1526                            new ArrayBlockingQueue<Runnable>(10));
1527          try (PoolCleaner cleaner = cleaner(e)) {
1528 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1528 >            List<Callable<String>> l = new ArrayList<>();
1529              l.add(new StringTask());
1530              l.add(null);
1531              try {
# Line 1612 | Line 1544 | public class ThreadPoolExecutorSubclassT
1544                            LONG_DELAY_MS, MILLISECONDS,
1545                            new ArrayBlockingQueue<Runnable>(10));
1546          try (PoolCleaner cleaner = cleaner(e)) {
1547 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1547 >            List<Callable<String>> l = new ArrayList<>();
1548              l.add(new NPETask());
1549              List<Future<String>> futures = e.invokeAll(l);
1550              assertEquals(1, futures.size());
# Line 1634 | Line 1566 | public class ThreadPoolExecutorSubclassT
1566                            LONG_DELAY_MS, MILLISECONDS,
1567                            new ArrayBlockingQueue<Runnable>(10));
1568          try (PoolCleaner cleaner = cleaner(e)) {
1569 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1569 >            List<Callable<String>> l = new ArrayList<>();
1570              l.add(new StringTask());
1571              l.add(new StringTask());
1572              List<Future<String>> futures = e.invokeAll(l);
# Line 1654 | Line 1586 | public class ThreadPoolExecutorSubclassT
1586                            new ArrayBlockingQueue<Runnable>(10));
1587          try (PoolCleaner cleaner = cleaner(e)) {
1588              try {
1589 <                e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1589 >                e.invokeAny(null, randomTimeout(), randomTimeUnit());
1590                  shouldThrow();
1591              } catch (NullPointerException success) {}
1592          }
# Line 1669 | Line 1601 | public class ThreadPoolExecutorSubclassT
1601                            LONG_DELAY_MS, MILLISECONDS,
1602                            new ArrayBlockingQueue<Runnable>(10));
1603          try (PoolCleaner cleaner = cleaner(e)) {
1604 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1604 >            List<Callable<String>> l = new ArrayList<>();
1605              l.add(new StringTask());
1606              try {
1607 <                e.invokeAny(l, MEDIUM_DELAY_MS, null);
1607 >                e.invokeAny(l, randomTimeout(), null);
1608                  shouldThrow();
1609              } catch (NullPointerException success) {}
1610          }
1611      }
1612  
1613      /**
1614 <     * timed invokeAny(empty collection) throws IAE
1614 >     * timed invokeAny(empty collection) throws IllegalArgumentException
1615       */
1616      public void testTimedInvokeAny2() throws Exception {
1617          final ExecutorService e =
1618              new CustomTPE(2, 2,
1619                            LONG_DELAY_MS, MILLISECONDS,
1620                            new ArrayBlockingQueue<Runnable>(10));
1621 +        final Collection<Callable<String>> emptyCollection
1622 +            = Collections.emptyList();
1623          try (PoolCleaner cleaner = cleaner(e)) {
1624              try {
1625 <                e.invokeAny(new ArrayList<Callable<String>>(),
1692 <                            MEDIUM_DELAY_MS, MILLISECONDS);
1625 >                e.invokeAny(emptyCollection, randomTimeout(), randomTimeUnit());
1626                  shouldThrow();
1627              } catch (IllegalArgumentException success) {}
1628          }
# Line 1705 | Line 1638 | public class ThreadPoolExecutorSubclassT
1638                            LONG_DELAY_MS, MILLISECONDS,
1639                            new ArrayBlockingQueue<Runnable>(10));
1640          try (PoolCleaner cleaner = cleaner(e)) {
1641 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1641 >            List<Callable<String>> l = new ArrayList<>();
1642              l.add(latchAwaitingStringTask(latch));
1643              l.add(null);
1644              try {
1645 <                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1645 >                e.invokeAny(l, randomTimeout(), randomTimeUnit());
1646                  shouldThrow();
1647              } catch (NullPointerException success) {}
1648              latch.countDown();
# Line 1725 | Line 1658 | public class ThreadPoolExecutorSubclassT
1658                            LONG_DELAY_MS, MILLISECONDS,
1659                            new ArrayBlockingQueue<Runnable>(10));
1660          try (PoolCleaner cleaner = cleaner(e)) {
1661 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1661 >            long startTime = System.nanoTime();
1662 >            List<Callable<String>> l = new ArrayList<>();
1663              l.add(new NPETask());
1664              try {
1665 <                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1665 >                e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1666                  shouldThrow();
1667              } catch (ExecutionException success) {
1668                  assertTrue(success.getCause() instanceof NullPointerException);
1669              }
1670 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1671          }
1672      }
1673  
# Line 1745 | Line 1680 | public class ThreadPoolExecutorSubclassT
1680                            LONG_DELAY_MS, MILLISECONDS,
1681                            new ArrayBlockingQueue<Runnable>(10));
1682          try (PoolCleaner cleaner = cleaner(e)) {
1683 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1683 >            long startTime = System.nanoTime();
1684 >            List<Callable<String>> l = new ArrayList<>();
1685              l.add(new StringTask());
1686              l.add(new StringTask());
1687 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1687 >            String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1688              assertSame(TEST_STRING, result);
1689 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1690          }
1691      }
1692  
# Line 1763 | Line 1700 | public class ThreadPoolExecutorSubclassT
1700                            new ArrayBlockingQueue<Runnable>(10));
1701          try (PoolCleaner cleaner = cleaner(e)) {
1702              try {
1703 <                e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1703 >                e.invokeAll(null, randomTimeout(), randomTimeUnit());
1704                  shouldThrow();
1705              } catch (NullPointerException success) {}
1706          }
# Line 1778 | Line 1715 | public class ThreadPoolExecutorSubclassT
1715                            LONG_DELAY_MS, MILLISECONDS,
1716                            new ArrayBlockingQueue<Runnable>(10));
1717          try (PoolCleaner cleaner = cleaner(e)) {
1718 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1718 >            List<Callable<String>> l = new ArrayList<>();
1719              l.add(new StringTask());
1720              try {
1721 <                e.invokeAll(l, MEDIUM_DELAY_MS, null);
1721 >                e.invokeAll(l, randomTimeout(), null);
1722                  shouldThrow();
1723              } catch (NullPointerException success) {}
1724          }
1725      }
1726  
1727      /**
1728 <     * timed invokeAll(empty collection) returns empty collection
1728 >     * timed invokeAll(empty collection) returns empty list
1729       */
1730      public void testTimedInvokeAll2() throws Exception {
1731          final ExecutorService e =
1732              new CustomTPE(2, 2,
1733                            LONG_DELAY_MS, MILLISECONDS,
1734                            new ArrayBlockingQueue<Runnable>(10));
1735 +        final Collection<Callable<String>> emptyCollection
1736 +            = Collections.emptyList();
1737          try (PoolCleaner cleaner = cleaner(e)) {
1738 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(),
1739 <                                                 MEDIUM_DELAY_MS, MILLISECONDS);
1738 >            List<Future<String>> r =
1739 >                e.invokeAll(emptyCollection, randomTimeout(), randomTimeUnit());
1740              assertTrue(r.isEmpty());
1741          }
1742      }
# Line 1811 | Line 1750 | public class ThreadPoolExecutorSubclassT
1750                            LONG_DELAY_MS, MILLISECONDS,
1751                            new ArrayBlockingQueue<Runnable>(10));
1752          try (PoolCleaner cleaner = cleaner(e)) {
1753 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1753 >            List<Callable<String>> l = new ArrayList<>();
1754              l.add(new StringTask());
1755              l.add(null);
1756              try {
1757 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1757 >                e.invokeAll(l, randomTimeout(), randomTimeUnit());
1758                  shouldThrow();
1759              } catch (NullPointerException success) {}
1760          }
# Line 1830 | Line 1769 | public class ThreadPoolExecutorSubclassT
1769                            LONG_DELAY_MS, MILLISECONDS,
1770                            new ArrayBlockingQueue<Runnable>(10));
1771          try (PoolCleaner cleaner = cleaner(e)) {
1772 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1772 >            List<Callable<String>> l = new ArrayList<>();
1773              l.add(new NPETask());
1774              List<Future<String>> futures =
1775                  e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
# Line 1853 | Line 1792 | public class ThreadPoolExecutorSubclassT
1792                            LONG_DELAY_MS, MILLISECONDS,
1793                            new ArrayBlockingQueue<Runnable>(10));
1794          try (PoolCleaner cleaner = cleaner(e)) {
1795 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1795 >            List<Callable<String>> l = new ArrayList<>();
1796              l.add(new StringTask());
1797              l.add(new StringTask());
1798              List<Future<String>> futures =
# Line 1868 | Line 1807 | public class ThreadPoolExecutorSubclassT
1807       * timed invokeAll(c) cancels tasks not completed by timeout
1808       */
1809      public void testTimedInvokeAll6() throws Exception {
1810 <        final ExecutorService e =
1811 <            new CustomTPE(2, 2,
1812 <                          LONG_DELAY_MS, MILLISECONDS,
1813 <                          new ArrayBlockingQueue<Runnable>(10));
1814 <        try (PoolCleaner cleaner = cleaner(e)) {
1815 <            for (long timeout = timeoutMillis();;) {
1810 >        for (long timeout = timeoutMillis();;) {
1811 >            final CountDownLatch done = new CountDownLatch(1);
1812 >            final Callable<String> waiter = new CheckedCallable<String>() {
1813 >                public String realCall() {
1814 >                    try { done.await(LONG_DELAY_MS, MILLISECONDS); }
1815 >                    catch (InterruptedException ok) {}
1816 >                    return "1"; }};
1817 >            final ExecutorService p =
1818 >                new CustomTPE(2, 2,
1819 >                              LONG_DELAY_MS, MILLISECONDS,
1820 >                              new ArrayBlockingQueue<Runnable>(10));
1821 >            try (PoolCleaner cleaner = cleaner(p, done)) {
1822                  List<Callable<String>> tasks = new ArrayList<>();
1823                  tasks.add(new StringTask("0"));
1824 <                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1824 >                tasks.add(waiter);
1825                  tasks.add(new StringTask("2"));
1826                  long startTime = System.nanoTime();
1827                  List<Future<String>> futures =
1828 <                    e.invokeAll(tasks, timeout, MILLISECONDS);
1828 >                    p.invokeAll(tasks, timeout, MILLISECONDS);
1829                  assertEquals(tasks.size(), futures.size());
1830                  assertTrue(millisElapsedSince(startTime) >= timeout);
1831                  for (Future future : futures)
# Line 1917 | Line 1862 | public class ThreadPoolExecutorSubclassT
1862                      public void realRun() {
1863                          done.countDown();
1864                      }});
1865 <            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1865 >            await(done);
1866          }
1867      }
1868  
# Line 2022 | Line 1967 | public class ThreadPoolExecutorSubclassT
1967          }
1968      }
1969  
1970 +    public void testFinalizeMethodCallsSuperFinalize() {
1971 +        new CustomTPE(1, 1,
1972 +                      LONG_DELAY_MS, MILLISECONDS,
1973 +                      new LinkedBlockingQueue<Runnable>()) {
1974 +
1975 +            /**
1976 +             * A finalize method without "throws Throwable", that
1977 +             * calls super.finalize().
1978 +             */
1979 +            protected void finalize() {
1980 +                super.finalize();
1981 +            }
1982 +        }.shutdown();
1983 +    }
1984 +
1985   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines