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.93 by jsr166, Sun Oct 4 03:51:35 2015 UTC vs.
Revision 1.111 by jsr166, Thu Oct 8 03:03:36 2015 UTC

# Line 101 | Line 101 | public class ThreadPoolExecutorTest exte
101       * thread becomes active
102       */
103      public void testGetActiveCount() throws InterruptedException {
104 +        final CountDownLatch done = new CountDownLatch(1);
105          final ThreadPoolExecutor p =
106              new ThreadPoolExecutor(2, 2,
107                                     LONG_DELAY_MS, MILLISECONDS,
108                                     new ArrayBlockingQueue<Runnable>(10));
109 <        try (PoolCleaner cleaner = cleaner(p)) {
109 >        try (PoolCleaner cleaner = cleaner(p, done)) {
110              final CountDownLatch threadStarted = new CountDownLatch(1);
110            final CountDownLatch done = new CountDownLatch(1);
111              assertEquals(0, p.getActiveCount());
112              p.execute(new CheckedRunnable() {
113                  public void realRun() throws InterruptedException {
114                      threadStarted.countDown();
115                      assertEquals(1, p.getActiveCount());
116 <                    done.await();
116 >                    await(done);
117                  }});
118 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
118 >            await(threadStarted);
119              assertEquals(1, p.getActiveCount());
120            done.countDown();
120          }
121      }
122  
# Line 329 | Line 328 | public class ThreadPoolExecutorTest exte
328       */
329      public void testGetLargestPoolSize() throws InterruptedException {
330          final int THREADS = 3;
331 +        final CountDownLatch done = new CountDownLatch(1);
332          final ThreadPoolExecutor p =
333              new ThreadPoolExecutor(THREADS, THREADS,
334                                     LONG_DELAY_MS, MILLISECONDS,
335                                     new ArrayBlockingQueue<Runnable>(10));
336 <        try (PoolCleaner cleaner = cleaner(p)) {
337 <            final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
338 <            final CountDownLatch done = new CountDownLatch(1);
336 >        try (PoolCleaner cleaner = cleaner(p, done)) {
337              assertEquals(0, p.getLargestPoolSize());
338 +            final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
339              for (int i = 0; i < THREADS; i++)
340                  p.execute(new CheckedRunnable() {
341                      public void realRun() throws InterruptedException {
342                          threadsStarted.countDown();
343 <                        done.await();
343 >                        await(done);
344                          assertEquals(THREADS, p.getLargestPoolSize());
345                      }});
346 <            assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
346 >            await(threadsStarted);
347              assertEquals(THREADS, p.getLargestPoolSize());
349            done.countDown();   // release pool
348          }
349          assertEquals(THREADS, p.getLargestPoolSize());
350      }
# Line 374 | Line 372 | public class ThreadPoolExecutorTest exte
372       * become active
373       */
374      public void testGetPoolSize() throws InterruptedException {
375 +        final CountDownLatch done = new CountDownLatch(1);
376          final ThreadPoolExecutor p =
377              new ThreadPoolExecutor(1, 1,
378                                     LONG_DELAY_MS, MILLISECONDS,
379                                     new ArrayBlockingQueue<Runnable>(10));
380 <        try (PoolCleaner cleaner = cleaner(p)) {
382 <            final CountDownLatch threadStarted = new CountDownLatch(1);
383 <            final CountDownLatch done = new CountDownLatch(1);
380 >        try (PoolCleaner cleaner = cleaner(p, done)) {
381              assertEquals(0, p.getPoolSize());
382 +            final CountDownLatch threadStarted = new CountDownLatch(1);
383              p.execute(new CheckedRunnable() {
384                  public void realRun() throws InterruptedException {
385                      threadStarted.countDown();
386                      assertEquals(1, p.getPoolSize());
387 <                    done.await();
387 >                    await(done);
388                  }});
389 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
389 >            await(threadStarted);
390              assertEquals(1, p.getPoolSize());
393            done.countDown();   // release pool
391          }
392      }
393  
# Line 398 | Line 395 | public class ThreadPoolExecutorTest exte
395       * getTaskCount increases, but doesn't overestimate, when tasks submitted
396       */
397      public void testGetTaskCount() throws InterruptedException {
398 +        final int TASKS = 3;
399 +        final CountDownLatch done = new CountDownLatch(1);
400          final ThreadPoolExecutor p =
401              new ThreadPoolExecutor(1, 1,
402                                     LONG_DELAY_MS, MILLISECONDS,
403                                     new ArrayBlockingQueue<Runnable>(10));
404 <        try (PoolCleaner cleaner = cleaner(p)) {
404 >        try (PoolCleaner cleaner = cleaner(p, done)) {
405              final CountDownLatch threadStarted = new CountDownLatch(1);
407            final CountDownLatch done = new CountDownLatch(1);
406              assertEquals(0, p.getTaskCount());
407 +            assertEquals(0, p.getCompletedTaskCount());
408              p.execute(new CheckedRunnable() {
409                  public void realRun() throws InterruptedException {
410                      threadStarted.countDown();
411 <                    assertEquals(1, p.getTaskCount());
413 <                    done.await();
411 >                    await(done);
412                  }});
413 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
413 >            await(threadStarted);
414              assertEquals(1, p.getTaskCount());
415 <            done.countDown();
415 >            assertEquals(0, p.getCompletedTaskCount());
416 >            for (int i = 0; i < TASKS; i++) {
417 >                assertEquals(1 + i, p.getTaskCount());
418 >                p.execute(new CheckedRunnable() {
419 >                    public void realRun() throws InterruptedException {
420 >                        threadStarted.countDown();
421 >                        assertEquals(1 + TASKS, p.getTaskCount());
422 >                        await(done);
423 >                    }});
424 >            }
425 >            assertEquals(1 + TASKS, p.getTaskCount());
426 >            assertEquals(0, p.getCompletedTaskCount());
427          }
428 +        assertEquals(1 + TASKS, p.getTaskCount());
429 +        assertEquals(1 + TASKS, p.getCompletedTaskCount());
430      }
431  
432      /**
# Line 481 | Line 492 | public class ThreadPoolExecutorTest exte
492                  public void realRun() throws InterruptedException {
493                      assertFalse(p.isTerminating());
494                      threadStarted.countDown();
495 <                    done.await();
495 >                    await(done);
496                  }});
497 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
497 >            await(threadStarted);
498              assertFalse(p.isTerminating());
499              done.countDown();
500              try { p.shutdown(); } catch (SecurityException ok) { return; }
# Line 509 | Line 520 | public class ThreadPoolExecutorTest exte
520                  public void realRun() throws InterruptedException {
521                      assertFalse(p.isTerminating());
522                      threadStarted.countDown();
523 <                    done.await();
523 >                    await(done);
524                  }});
525 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
525 >            await(threadStarted);
526              assertFalse(p.isTerminating());
527              done.countDown();
528              try { p.shutdown(); } catch (SecurityException ok) { return; }
# Line 525 | Line 536 | public class ThreadPoolExecutorTest exte
536       * getQueue returns the work queue, which contains queued tasks
537       */
538      public void testGetQueue() throws InterruptedException {
539 +        final CountDownLatch done = new CountDownLatch(1);
540          final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
541          final ThreadPoolExecutor p =
542              new ThreadPoolExecutor(1, 1,
543                                     LONG_DELAY_MS, MILLISECONDS,
544                                     q);
545 <        try (PoolCleaner cleaner = cleaner(p)) {
545 >        try (PoolCleaner cleaner = cleaner(p, done)) {
546              final CountDownLatch threadStarted = new CountDownLatch(1);
535            final CountDownLatch done = new CountDownLatch(1);
547              FutureTask[] tasks = new FutureTask[5];
548              for (int i = 0; i < tasks.length; i++) {
549                  Callable task = new CheckedCallable<Boolean>() {
550                      public Boolean realCall() throws InterruptedException {
551                          threadStarted.countDown();
552                          assertSame(q, p.getQueue());
553 <                        done.await();
553 >                        await(done);
554                          return Boolean.TRUE;
555                      }};
556                  tasks[i] = new FutureTask(task);
557                  p.execute(tasks[i]);
558              }
559 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
559 >            await(threadStarted);
560              assertSame(q, p.getQueue());
561              assertFalse(q.contains(tasks[0]));
562              assertTrue(q.contains(tasks[tasks.length - 1]));
563              assertEquals(tasks.length - 1, q.size());
553            done.countDown();
564          }
565      }
566  
# Line 558 | Line 568 | public class ThreadPoolExecutorTest exte
568       * remove(task) removes queued task, and fails to remove active task
569       */
570      public void testRemove() throws InterruptedException {
571 +        final CountDownLatch done = new CountDownLatch(1);
572          BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
573          final ThreadPoolExecutor p =
574              new ThreadPoolExecutor(1, 1,
575                                     LONG_DELAY_MS, MILLISECONDS,
576                                     q);
577 <        try (PoolCleaner cleaner = cleaner(p)) {
577 >        try (PoolCleaner cleaner = cleaner(p, done)) {
578              Runnable[] tasks = new Runnable[6];
579              final CountDownLatch threadStarted = new CountDownLatch(1);
569            final CountDownLatch done = new CountDownLatch(1);
580              for (int i = 0; i < tasks.length; i++) {
581                  tasks[i] = new CheckedRunnable() {
582                      public void realRun() throws InterruptedException {
583                          threadStarted.countDown();
584 <                        done.await();
584 >                        await(done);
585                      }};
586                  p.execute(tasks[i]);
587              }
588 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
588 >            await(threadStarted);
589              assertFalse(p.remove(tasks[0]));
590              assertTrue(q.contains(tasks[4]));
591              assertTrue(q.contains(tasks[3]));
# Line 585 | Line 595 | public class ThreadPoolExecutorTest exte
595              assertTrue(q.contains(tasks[3]));
596              assertTrue(p.remove(tasks[3]));
597              assertFalse(q.contains(tasks[3]));
588            done.countDown();
598          }
599      }
600  
# Line 600 | Line 609 | public class ThreadPoolExecutorTest exte
609              new ThreadPoolExecutor(1, 1,
610                                     LONG_DELAY_MS, MILLISECONDS,
611                                     q);
612 <        try (PoolCleaner cleaner = cleaner(p)) {
612 >        try (PoolCleaner cleaner = cleaner(p, done)) {
613              FutureTask[] tasks = new FutureTask[5];
614              for (int i = 0; i < tasks.length; i++) {
615                  Callable task = new CheckedCallable<Boolean>() {
616                      public Boolean realCall() throws InterruptedException {
617                          threadStarted.countDown();
618 <                        done.await();
618 >                        await(done);
619                          return Boolean.TRUE;
620                      }};
621                  tasks[i] = new FutureTask(task);
622                  p.execute(tasks[i]);
623              }
624 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
624 >            await(threadStarted);
625              assertEquals(tasks.length, p.getTaskCount());
626              assertEquals(tasks.length - 1, q.size());
627              assertEquals(1L, p.getActiveCount());
# Line 625 | Line 634 | public class ThreadPoolExecutorTest exte
634              p.purge();         // Nothing to do
635              assertEquals(tasks.length - 3, q.size());
636              assertEquals(tasks.length - 2, p.getTaskCount());
628            done.countDown();
637          }
638      }
639  
# Line 641 | Line 649 | public class ThreadPoolExecutorTest exte
649              new ThreadPoolExecutor(poolSize, poolSize,
650                                     LONG_DELAY_MS, MILLISECONDS,
651                                     new ArrayBlockingQueue<Runnable>(10));
652 <        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
652 >        final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
653          Runnable waiter = new CheckedRunnable() { public void realRun() {
654              threadsStarted.countDown();
655              try {
# Line 651 | Line 659 | public class ThreadPoolExecutorTest exte
659          }};
660          for (int i = 0; i < count; i++)
661              p.execute(waiter);
662 <        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
662 >        await(threadsStarted);
663          assertEquals(poolSize, p.getActiveCount());
664          assertEquals(0, p.getCompletedTaskCount());
665          final List<Runnable> queuedTasks;
# Line 1013 | Line 1021 | public class ThreadPoolExecutorTest exte
1021       * get of submitted callable throws InterruptedException if interrupted
1022       */
1023      public void testInterruptedSubmit() throws InterruptedException {
1024 +        final CountDownLatch done = new CountDownLatch(1);
1025          final ThreadPoolExecutor p =
1026              new ThreadPoolExecutor(1, 1,
1027                                     60, SECONDS,
1028                                     new ArrayBlockingQueue<Runnable>(10));
1029  
1030 <        try (PoolCleaner cleaner = cleaner(p)) {
1030 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1031              final CountDownLatch threadStarted = new CountDownLatch(1);
1023            final CountDownLatch done = new CountDownLatch(1);
1032              Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1033                  public void realRun() throws Exception {
1034                      Callable task = new CheckedCallable<Boolean>() {
1035                          public Boolean realCall() throws InterruptedException {
1036                              threadStarted.countDown();
1037 <                            done.await();
1037 >                            await(done);
1038                              return Boolean.TRUE;
1039                          }};
1040                      p.submit(task).get();
1041                  }});
1042  
1043 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
1043 >            await(threadStarted);
1044              t.interrupt();
1045 <            awaitTermination(t, MEDIUM_DELAY_MS);
1038 <            done.countDown();
1045 >            awaitTermination(t);
1046          }
1047      }
1048  
# Line 1043 | Line 1050 | public class ThreadPoolExecutorTest exte
1050       * execute throws RejectedExecutionException if saturated.
1051       */
1052      public void testSaturatedExecute() {
1053 +        final CountDownLatch done = new CountDownLatch(1);
1054          final ThreadPoolExecutor p =
1055              new ThreadPoolExecutor(1, 1,
1056                                     LONG_DELAY_MS, MILLISECONDS,
1057                                     new ArrayBlockingQueue<Runnable>(1));
1058 <        try (PoolCleaner cleaner = cleaner(p)) {
1051 <            final CountDownLatch done = new CountDownLatch(1);
1058 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1059              Runnable task = new CheckedRunnable() {
1060                  public void realRun() throws InterruptedException {
1061 <                    done.await();
1061 >                    await(done);
1062                  }};
1063              for (int i = 0; i < 2; ++i)
1064                  p.execute(task);
# Line 1062 | Line 1069 | public class ThreadPoolExecutorTest exte
1069                  } catch (RejectedExecutionException success) {}
1070                  assertTrue(p.getTaskCount() <= 2);
1071              }
1065            done.countDown();
1072          }
1073      }
1074  
# Line 1070 | Line 1076 | public class ThreadPoolExecutorTest exte
1076       * submit(runnable) throws RejectedExecutionException if saturated.
1077       */
1078      public void testSaturatedSubmitRunnable() {
1079 <        ThreadPoolExecutor p =
1079 >        final CountDownLatch done = new CountDownLatch(1);
1080 >        final ThreadPoolExecutor p =
1081              new ThreadPoolExecutor(1, 1,
1082                                     LONG_DELAY_MS, MILLISECONDS,
1083                                     new ArrayBlockingQueue<Runnable>(1));
1084 <        final CountDownLatch done = new CountDownLatch(1);
1078 <        try {
1084 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1085              Runnable task = new CheckedRunnable() {
1086                  public void realRun() throws InterruptedException {
1087 <                    done.await();
1087 >                    await(done);
1088                  }};
1089              for (int i = 0; i < 2; ++i)
1090                  p.submit(task);
# Line 1089 | Line 1095 | public class ThreadPoolExecutorTest exte
1095                  } catch (RejectedExecutionException success) {}
1096                  assertTrue(p.getTaskCount() <= 2);
1097              }
1092        } finally {
1093            done.countDown();
1094            joinPool(p);
1098          }
1099      }
1100  
# Line 1099 | Line 1102 | public class ThreadPoolExecutorTest exte
1102       * submit(callable) throws RejectedExecutionException if saturated.
1103       */
1104      public void testSaturatedSubmitCallable() {
1105 <        ThreadPoolExecutor p =
1105 >        final CountDownLatch done = new CountDownLatch(1);
1106 >        final ThreadPoolExecutor p =
1107              new ThreadPoolExecutor(1, 1,
1108                                     LONG_DELAY_MS, MILLISECONDS,
1109                                     new ArrayBlockingQueue<Runnable>(1));
1110 <        final CountDownLatch done = new CountDownLatch(1);
1107 <        try {
1110 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1111              Runnable task = new CheckedRunnable() {
1112                  public void realRun() throws InterruptedException {
1113 <                    done.await();
1113 >                    await(done);
1114                  }};
1115              for (int i = 0; i < 2; ++i)
1116                  p.submit(Executors.callable(task));
# Line 1118 | Line 1121 | public class ThreadPoolExecutorTest exte
1121                  } catch (RejectedExecutionException success) {}
1122                  assertTrue(p.getTaskCount() <= 2);
1123              }
1121        } finally {
1122            done.countDown();
1123            joinPool(p);
1124          }
1125      }
1126  
# Line 1138 | Line 1138 | public class ThreadPoolExecutorTest exte
1138              final CountDownLatch done = new CountDownLatch(1);
1139              Runnable blocker = new CheckedRunnable() {
1140                  public void realRun() throws InterruptedException {
1141 <                    done.await();
1141 >                    await(done);
1142                  }};
1143              p.execute(blocker);
1144              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
# Line 1157 | Line 1157 | public class ThreadPoolExecutorTest exte
1157       * executor using DiscardPolicy drops task if saturated.
1158       */
1159      public void testSaturatedExecute3() {
1160 +        final CountDownLatch done = new CountDownLatch(1);
1161          final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1162          for (int i = 0; i < tasks.length; ++i)
1163              tasks[i] = new TrackedNoOpRunnable();
# Line 1165 | Line 1166 | public class ThreadPoolExecutorTest exte
1166                            LONG_DELAY_MS, MILLISECONDS,
1167                            new ArrayBlockingQueue<Runnable>(1),
1168                            new ThreadPoolExecutor.DiscardPolicy());
1169 <        try (PoolCleaner cleaner = cleaner(p)) {
1169 <            final CountDownLatch done = new CountDownLatch(1);
1169 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1170              p.execute(awaiter(done));
1171  
1172              for (TrackedNoOpRunnable task : tasks)
1173                  p.execute(task);
1174              for (int i = 1; i < tasks.length; i++)
1175                  assertFalse(tasks[i].done);
1176            done.countDown();
1176          }
1177          for (int i = 1; i < tasks.length; i++)
1178              assertFalse(tasks[i].done);
# Line 1193 | Line 1192 | public class ThreadPoolExecutorTest exte
1192                                     LONG_DELAY_MS, MILLISECONDS,
1193                                     new ArrayBlockingQueue<Runnable>(1),
1194                                     new ThreadPoolExecutor.DiscardOldestPolicy());
1195 <        try (PoolCleaner cleaner = cleaner(p)) {
1195 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1196              assertEquals(LatchAwaiter.NEW, r1.state);
1197              assertEquals(LatchAwaiter.NEW, r2.state);
1198              assertEquals(LatchAwaiter.NEW, r3.state);
# Line 1203 | Line 1202 | public class ThreadPoolExecutorTest exte
1202              p.execute(r3);
1203              assertFalse(p.getQueue().contains(r2));
1204              assertTrue(p.getQueue().contains(r3));
1206            done.countDown();
1205          }
1206          assertEquals(LatchAwaiter.DONE, r1.state);
1207          assertEquals(LatchAwaiter.NEW, r2.state);
# Line 1214 | Line 1212 | public class ThreadPoolExecutorTest exte
1212       * execute throws RejectedExecutionException if shutdown
1213       */
1214      public void testRejectedExecutionExceptionOnShutdown() {
1215 <        ThreadPoolExecutor p =
1215 >        final ThreadPoolExecutor p =
1216              new ThreadPoolExecutor(1, 1,
1217                                     LONG_DELAY_MS, MILLISECONDS,
1218                                     new ArrayBlockingQueue<Runnable>(1));
# Line 1238 | Line 1236 | public class ThreadPoolExecutorTest exte
1236                                     new ArrayBlockingQueue<Runnable>(1), h);
1237  
1238          try { p.shutdown(); } catch (SecurityException ok) { return; }
1239 <        try {
1239 >        try (PoolCleaner cleaner = cleaner(p)) {
1240              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1241              p.execute(r);
1242              assertFalse(r.done);
1245        } finally {
1246            joinPool(p);
1243          }
1244      }
1245  
# Line 1251 | Line 1247 | public class ThreadPoolExecutorTest exte
1247       * execute using DiscardPolicy drops task on shutdown
1248       */
1249      public void testDiscardOnShutdown() {
1250 <        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
1255 <        ThreadPoolExecutor p =
1250 >        final ThreadPoolExecutor p =
1251              new ThreadPoolExecutor(1, 1,
1252                                     LONG_DELAY_MS, MILLISECONDS,
1253                                     new ArrayBlockingQueue<Runnable>(1),
1254 <                                   h);
1254 >                                   new ThreadPoolExecutor.DiscardPolicy());
1255  
1256          try { p.shutdown(); } catch (SecurityException ok) { return; }
1257 <        try {
1257 >        try (PoolCleaner cleaner = cleaner(p)) {
1258              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1259              p.execute(r);
1260              assertFalse(r.done);
1266        } finally {
1267            joinPool(p);
1261          }
1262      }
1263  
# Line 1272 | Line 1265 | public class ThreadPoolExecutorTest exte
1265       * execute using DiscardOldestPolicy drops task on shutdown
1266       */
1267      public void testDiscardOldestOnShutdown() {
1268 <        RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
1276 <        ThreadPoolExecutor p =
1268 >        final ThreadPoolExecutor p =
1269              new ThreadPoolExecutor(1, 1,
1270                                     LONG_DELAY_MS, MILLISECONDS,
1271                                     new ArrayBlockingQueue<Runnable>(1),
1272 <                                   h);
1272 >                                   new ThreadPoolExecutor.DiscardOldestPolicy());
1273  
1274          try { p.shutdown(); } catch (SecurityException ok) { return; }
1275 <        try {
1275 >        try (PoolCleaner cleaner = cleaner(p)) {
1276              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1277              p.execute(r);
1278              assertFalse(r.done);
1287        } finally {
1288            joinPool(p);
1279          }
1280      }
1281  
# Line 1293 | Line 1283 | public class ThreadPoolExecutorTest exte
1283       * execute(null) throws NPE
1284       */
1285      public void testExecuteNull() {
1286 <        ThreadPoolExecutor p =
1287 <            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1286 >        final ThreadPoolExecutor p =
1287 >            new ThreadPoolExecutor(1, 2,
1288 >                                   1L, SECONDS,
1289                                     new ArrayBlockingQueue<Runnable>(10));
1290 <        try {
1291 <            p.execute(null);
1292 <            shouldThrow();
1293 <        } catch (NullPointerException success) {}
1294 <
1295 <        joinPool(p);
1290 >        try (PoolCleaner cleaner = cleaner(p)) {
1291 >            try {
1292 >                p.execute(null);
1293 >                shouldThrow();
1294 >            } catch (NullPointerException success) {}
1295 >        }
1296      }
1297  
1298      /**
1299       * setCorePoolSize of negative value throws IllegalArgumentException
1300       */
1301      public void testCorePoolSizeIllegalArgumentException() {
1302 <        ThreadPoolExecutor p =
1302 >        final ThreadPoolExecutor p =
1303              new ThreadPoolExecutor(1, 2,
1304                                     LONG_DELAY_MS, MILLISECONDS,
1305                                     new ArrayBlockingQueue<Runnable>(10));
1306 <        try {
1307 <            p.setCorePoolSize(-1);
1308 <            shouldThrow();
1309 <        } catch (IllegalArgumentException success) {
1310 <        } finally {
1320 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1306 >        try (PoolCleaner cleaner = cleaner(p)) {
1307 >            try {
1308 >                p.setCorePoolSize(-1);
1309 >                shouldThrow();
1310 >            } catch (IllegalArgumentException success) {}
1311          }
1322        joinPool(p);
1312      }
1313  
1314      /**
# Line 1327 | Line 1316 | public class ThreadPoolExecutorTest exte
1316       * given a value less the core pool size
1317       */
1318      public void testMaximumPoolSizeIllegalArgumentException() {
1319 <        ThreadPoolExecutor p =
1319 >        final ThreadPoolExecutor p =
1320              new ThreadPoolExecutor(2, 3,
1321                                     LONG_DELAY_MS, MILLISECONDS,
1322                                     new ArrayBlockingQueue<Runnable>(10));
1323 <        try {
1324 <            p.setMaximumPoolSize(1);
1325 <            shouldThrow();
1326 <        } catch (IllegalArgumentException success) {
1327 <        } finally {
1339 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1323 >        try (PoolCleaner cleaner = cleaner(p)) {
1324 >            try {
1325 >                p.setMaximumPoolSize(1);
1326 >                shouldThrow();
1327 >            } catch (IllegalArgumentException success) {}
1328          }
1341        joinPool(p);
1329      }
1330  
1331      /**
# Line 1346 | Line 1333 | public class ThreadPoolExecutorTest exte
1333       * if given a negative value
1334       */
1335      public void testMaximumPoolSizeIllegalArgumentException2() {
1336 <        ThreadPoolExecutor p =
1336 >        final ThreadPoolExecutor p =
1337              new ThreadPoolExecutor(2, 3,
1338                                     LONG_DELAY_MS, MILLISECONDS,
1339                                     new ArrayBlockingQueue<Runnable>(10));
1340 <        try {
1341 <            p.setMaximumPoolSize(-1);
1342 <            shouldThrow();
1343 <        } catch (IllegalArgumentException success) {
1344 <        } finally {
1358 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1340 >        try (PoolCleaner cleaner = cleaner(p)) {
1341 >            try {
1342 >                p.setMaximumPoolSize(-1);
1343 >                shouldThrow();
1344 >            } catch (IllegalArgumentException success) {}
1345          }
1360        joinPool(p);
1346      }
1347  
1348      /**
# Line 1365 | Line 1350 | public class ThreadPoolExecutorTest exte
1350       * max pool size result in IllegalArgumentException.
1351       */
1352      public void testPoolSizeInvariants() {
1353 <        ThreadPoolExecutor p =
1353 >        final ThreadPoolExecutor p =
1354              new ThreadPoolExecutor(1, 1,
1355                                     LONG_DELAY_MS, MILLISECONDS,
1356                                     new ArrayBlockingQueue<Runnable>(10));
1357 <        for (int s = 1; s < 5; s++) {
1358 <            p.setMaximumPoolSize(s);
1359 <            p.setCorePoolSize(s);
1360 <            try {
1361 <                p.setMaximumPoolSize(s - 1);
1362 <                shouldThrow();
1363 <            } catch (IllegalArgumentException success) {}
1364 <            assertEquals(s, p.getCorePoolSize());
1365 <            assertEquals(s, p.getMaximumPoolSize());
1366 <            try {
1367 <                p.setCorePoolSize(s + 1);
1368 <                shouldThrow();
1369 <            } catch (IllegalArgumentException success) {}
1370 <            assertEquals(s, p.getCorePoolSize());
1371 <            assertEquals(s, p.getMaximumPoolSize());
1357 >        try (PoolCleaner cleaner = cleaner(p)) {
1358 >            for (int s = 1; s < 5; s++) {
1359 >                p.setMaximumPoolSize(s);
1360 >                p.setCorePoolSize(s);
1361 >                try {
1362 >                    p.setMaximumPoolSize(s - 1);
1363 >                    shouldThrow();
1364 >                } catch (IllegalArgumentException success) {}
1365 >                assertEquals(s, p.getCorePoolSize());
1366 >                assertEquals(s, p.getMaximumPoolSize());
1367 >                try {
1368 >                    p.setCorePoolSize(s + 1);
1369 >                    shouldThrow();
1370 >                } catch (IllegalArgumentException success) {}
1371 >                assertEquals(s, p.getCorePoolSize());
1372 >                assertEquals(s, p.getMaximumPoolSize());
1373 >            }
1374          }
1388        joinPool(p);
1375      }
1376  
1377      /**
# Line 1393 | Line 1379 | public class ThreadPoolExecutorTest exte
1379       * when given a negative value
1380       */
1381      public void testKeepAliveTimeIllegalArgumentException() {
1382 <        ThreadPoolExecutor p =
1382 >        final ThreadPoolExecutor p =
1383              new ThreadPoolExecutor(2, 3,
1384                                     LONG_DELAY_MS, MILLISECONDS,
1385                                     new ArrayBlockingQueue<Runnable>(10));
1386 <        try {
1387 <            p.setKeepAliveTime(-1,MILLISECONDS);
1388 <            shouldThrow();
1389 <        } catch (IllegalArgumentException success) {
1390 <        } finally {
1405 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
1386 >        try (PoolCleaner cleaner = cleaner(p)) {
1387 >            try {
1388 >                p.setKeepAliveTime(-1, MILLISECONDS);
1389 >                shouldThrow();
1390 >            } catch (IllegalArgumentException success) {}
1391          }
1407        joinPool(p);
1392      }
1393  
1394      /**
# Line 1412 | Line 1396 | public class ThreadPoolExecutorTest exte
1396       */
1397      public void testTerminated() {
1398          ExtendedTPE p = new ExtendedTPE();
1399 <        try { p.shutdown(); } catch (SecurityException ok) { return; }
1400 <        assertTrue(p.terminatedCalled());
1401 <        joinPool(p);
1399 >        try (PoolCleaner cleaner = cleaner(p)) {
1400 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1401 >            assertTrue(p.terminatedCalled());
1402 >            assertTrue(p.isShutdown());
1403 >        }
1404      }
1405  
1406      /**
# Line 1422 | Line 1408 | public class ThreadPoolExecutorTest exte
1408       */
1409      public void testBeforeAfter() throws InterruptedException {
1410          ExtendedTPE p = new ExtendedTPE();
1411 <        try {
1411 >        try (PoolCleaner cleaner = cleaner(p)) {
1412              final CountDownLatch done = new CountDownLatch(1);
1413              p.execute(new CheckedRunnable() {
1414                  public void realRun() {
# Line 1432 | Line 1418 | public class ThreadPoolExecutorTest exte
1418              assertEquals(0, done.getCount());
1419              assertTrue(p.afterCalled());
1420              assertTrue(p.beforeCalled());
1435            try { p.shutdown(); } catch (SecurityException ok) { return; }
1436        } finally {
1437            joinPool(p);
1421          }
1422      }
1423  
# Line 1442 | Line 1425 | public class ThreadPoolExecutorTest exte
1425       * completed submit of callable returns result
1426       */
1427      public void testSubmitCallable() throws Exception {
1428 <        ExecutorService e =
1428 >        final ExecutorService e =
1429              new ThreadPoolExecutor(2, 2,
1430                                     LONG_DELAY_MS, MILLISECONDS,
1431                                     new ArrayBlockingQueue<Runnable>(10));
1432 <        try {
1432 >        try (PoolCleaner cleaner = cleaner(e)) {
1433              Future<String> future = e.submit(new StringTask());
1434              String result = future.get();
1435              assertSame(TEST_STRING, result);
1453        } finally {
1454            joinPool(e);
1436          }
1437      }
1438  
# Line 1459 | Line 1440 | public class ThreadPoolExecutorTest exte
1440       * completed submit of runnable returns successfully
1441       */
1442      public void testSubmitRunnable() throws Exception {
1443 <        ExecutorService e =
1443 >        final ExecutorService e =
1444              new ThreadPoolExecutor(2, 2,
1445                                     LONG_DELAY_MS, MILLISECONDS,
1446                                     new ArrayBlockingQueue<Runnable>(10));
1447 <        try {
1447 >        try (PoolCleaner cleaner = cleaner(e)) {
1448              Future<?> future = e.submit(new NoOpRunnable());
1449              future.get();
1450              assertTrue(future.isDone());
1470        } finally {
1471            joinPool(e);
1451          }
1452      }
1453  
# Line 1476 | Line 1455 | public class ThreadPoolExecutorTest exte
1455       * completed submit of (runnable, result) returns result
1456       */
1457      public void testSubmitRunnable2() throws Exception {
1458 <        ExecutorService e =
1458 >        final ExecutorService e =
1459              new ThreadPoolExecutor(2, 2,
1460                                     LONG_DELAY_MS, MILLISECONDS,
1461                                     new ArrayBlockingQueue<Runnable>(10));
1462 <        try {
1462 >        try (PoolCleaner cleaner = cleaner(e)) {
1463              Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1464              String result = future.get();
1465              assertSame(TEST_STRING, result);
1487        } finally {
1488            joinPool(e);
1466          }
1467      }
1468  
# Line 1493 | Line 1470 | public class ThreadPoolExecutorTest exte
1470       * invokeAny(null) throws NPE
1471       */
1472      public void testInvokeAny1() throws Exception {
1473 <        ExecutorService e =
1473 >        final ExecutorService e =
1474              new ThreadPoolExecutor(2, 2,
1475                                     LONG_DELAY_MS, MILLISECONDS,
1476                                     new ArrayBlockingQueue<Runnable>(10));
1477 <        try {
1478 <            e.invokeAny(null);
1479 <            shouldThrow();
1480 <        } catch (NullPointerException success) {
1481 <        } finally {
1505 <            joinPool(e);
1477 >        try (PoolCleaner cleaner = cleaner(e)) {
1478 >            try {
1479 >                e.invokeAny(null);
1480 >                shouldThrow();
1481 >            } catch (NullPointerException success) {}
1482          }
1483      }
1484  
# Line 1510 | Line 1486 | public class ThreadPoolExecutorTest exte
1486       * invokeAny(empty collection) throws IAE
1487       */
1488      public void testInvokeAny2() throws Exception {
1489 <        ExecutorService e =
1489 >        final ExecutorService e =
1490              new ThreadPoolExecutor(2, 2,
1491                                     LONG_DELAY_MS, MILLISECONDS,
1492                                     new ArrayBlockingQueue<Runnable>(10));
1493 <        try {
1494 <            e.invokeAny(new ArrayList<Callable<String>>());
1495 <            shouldThrow();
1496 <        } catch (IllegalArgumentException success) {
1497 <        } finally {
1522 <            joinPool(e);
1493 >        try (PoolCleaner cleaner = cleaner(e)) {
1494 >            try {
1495 >                e.invokeAny(new ArrayList<Callable<String>>());
1496 >                shouldThrow();
1497 >            } catch (IllegalArgumentException success) {}
1498          }
1499      }
1500  
# Line 1532 | Line 1507 | public class ThreadPoolExecutorTest exte
1507              new ThreadPoolExecutor(2, 2,
1508                                     LONG_DELAY_MS, MILLISECONDS,
1509                                     new ArrayBlockingQueue<Runnable>(10));
1510 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1511 <        l.add(latchAwaitingStringTask(latch));
1512 <        l.add(null);
1513 <        try {
1514 <            e.invokeAny(l);
1515 <            shouldThrow();
1516 <        } catch (NullPointerException success) {
1517 <        } finally {
1510 >        try (PoolCleaner cleaner = cleaner(e)) {
1511 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1512 >            l.add(latchAwaitingStringTask(latch));
1513 >            l.add(null);
1514 >            try {
1515 >                e.invokeAny(l);
1516 >                shouldThrow();
1517 >            } catch (NullPointerException success) {}
1518              latch.countDown();
1544            joinPool(e);
1519          }
1520      }
1521  
# Line 1549 | Line 1523 | public class ThreadPoolExecutorTest exte
1523       * invokeAny(c) throws ExecutionException if no task completes
1524       */
1525      public void testInvokeAny4() throws Exception {
1526 <        ExecutorService e =
1526 >        final ExecutorService e =
1527              new ThreadPoolExecutor(2, 2,
1528                                     LONG_DELAY_MS, MILLISECONDS,
1529                                     new ArrayBlockingQueue<Runnable>(10));
1530 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1531 <        l.add(new NPETask());
1532 <        try {
1533 <            e.invokeAny(l);
1534 <            shouldThrow();
1535 <        } catch (ExecutionException success) {
1536 <            assertTrue(success.getCause() instanceof NullPointerException);
1537 <        } finally {
1538 <            joinPool(e);
1530 >        try (PoolCleaner cleaner = cleaner(e)) {
1531 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1532 >            l.add(new NPETask());
1533 >            try {
1534 >                e.invokeAny(l);
1535 >                shouldThrow();
1536 >            } catch (ExecutionException success) {
1537 >                assertTrue(success.getCause() instanceof NullPointerException);
1538 >            }
1539          }
1540      }
1541  
# Line 1569 | Line 1543 | public class ThreadPoolExecutorTest exte
1543       * invokeAny(c) returns result of some task
1544       */
1545      public void testInvokeAny5() throws Exception {
1546 <        ExecutorService e =
1546 >        final ExecutorService e =
1547              new ThreadPoolExecutor(2, 2,
1548                                     LONG_DELAY_MS, MILLISECONDS,
1549                                     new ArrayBlockingQueue<Runnable>(10));
1550 <        try {
1550 >        try (PoolCleaner cleaner = cleaner(e)) {
1551              List<Callable<String>> l = new ArrayList<Callable<String>>();
1552              l.add(new StringTask());
1553              l.add(new StringTask());
1554              String result = e.invokeAny(l);
1555              assertSame(TEST_STRING, result);
1582        } finally {
1583            joinPool(e);
1556          }
1557      }
1558  
# Line 1588 | Line 1560 | public class ThreadPoolExecutorTest exte
1560       * invokeAll(null) throws NPE
1561       */
1562      public void testInvokeAll1() throws Exception {
1563 <        ExecutorService e =
1563 >        final ExecutorService e =
1564              new ThreadPoolExecutor(2, 2,
1565                                     LONG_DELAY_MS, MILLISECONDS,
1566                                     new ArrayBlockingQueue<Runnable>(10));
1567 <        try {
1568 <            e.invokeAll(null);
1569 <            shouldThrow();
1570 <        } catch (NullPointerException success) {
1571 <        } finally {
1600 <            joinPool(e);
1567 >        try (PoolCleaner cleaner = cleaner(e)) {
1568 >            try {
1569 >                e.invokeAll(null);
1570 >                shouldThrow();
1571 >            } catch (NullPointerException success) {}
1572          }
1573      }
1574  
# Line 1605 | Line 1576 | public class ThreadPoolExecutorTest exte
1576       * invokeAll(empty collection) returns empty collection
1577       */
1578      public void testInvokeAll2() throws InterruptedException {
1579 <        ExecutorService e =
1579 >        final ExecutorService e =
1580              new ThreadPoolExecutor(2, 2,
1581                                     LONG_DELAY_MS, MILLISECONDS,
1582                                     new ArrayBlockingQueue<Runnable>(10));
1583 <        try {
1583 >        try (PoolCleaner cleaner = cleaner(e)) {
1584              List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1585              assertTrue(r.isEmpty());
1615        } finally {
1616            joinPool(e);
1586          }
1587      }
1588  
# Line 1621 | Line 1590 | public class ThreadPoolExecutorTest exte
1590       * invokeAll(c) throws NPE if c has null elements
1591       */
1592      public void testInvokeAll3() throws Exception {
1593 <        ExecutorService e =
1593 >        final ExecutorService e =
1594              new ThreadPoolExecutor(2, 2,
1595                                     LONG_DELAY_MS, MILLISECONDS,
1596                                     new ArrayBlockingQueue<Runnable>(10));
1597 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1598 <        l.add(new StringTask());
1599 <        l.add(null);
1600 <        try {
1601 <            e.invokeAll(l);
1602 <            shouldThrow();
1603 <        } catch (NullPointerException success) {
1604 <        } finally {
1636 <            joinPool(e);
1597 >        try (PoolCleaner cleaner = cleaner(e)) {
1598 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1599 >            l.add(new StringTask());
1600 >            l.add(null);
1601 >            try {
1602 >                e.invokeAll(l);
1603 >                shouldThrow();
1604 >            } catch (NullPointerException success) {}
1605          }
1606      }
1607  
# Line 1641 | Line 1609 | public class ThreadPoolExecutorTest exte
1609       * get of element of invokeAll(c) throws exception on failed task
1610       */
1611      public void testInvokeAll4() throws Exception {
1612 <        ExecutorService e =
1612 >        final ExecutorService e =
1613              new ThreadPoolExecutor(2, 2,
1614                                     LONG_DELAY_MS, MILLISECONDS,
1615                                     new ArrayBlockingQueue<Runnable>(10));
1616 <        try {
1616 >        try (PoolCleaner cleaner = cleaner(e)) {
1617              List<Callable<String>> l = new ArrayList<Callable<String>>();
1618              l.add(new NPETask());
1619              List<Future<String>> futures = e.invokeAll(l);
# Line 1656 | Line 1624 | public class ThreadPoolExecutorTest exte
1624              } catch (ExecutionException success) {
1625                  assertTrue(success.getCause() instanceof NullPointerException);
1626              }
1659        } finally {
1660            joinPool(e);
1627          }
1628      }
1629  
# Line 1665 | Line 1631 | public class ThreadPoolExecutorTest exte
1631       * invokeAll(c) returns results of all completed tasks
1632       */
1633      public void testInvokeAll5() throws Exception {
1634 <        ExecutorService e =
1634 >        final ExecutorService e =
1635              new ThreadPoolExecutor(2, 2,
1636                                     LONG_DELAY_MS, MILLISECONDS,
1637                                     new ArrayBlockingQueue<Runnable>(10));
1638 <        try {
1638 >        try (PoolCleaner cleaner = cleaner(e)) {
1639              List<Callable<String>> l = new ArrayList<Callable<String>>();
1640              l.add(new StringTask());
1641              l.add(new StringTask());
# Line 1677 | Line 1643 | public class ThreadPoolExecutorTest exte
1643              assertEquals(2, futures.size());
1644              for (Future<String> future : futures)
1645                  assertSame(TEST_STRING, future.get());
1680        } finally {
1681            joinPool(e);
1646          }
1647      }
1648  
# Line 1686 | Line 1650 | public class ThreadPoolExecutorTest exte
1650       * timed invokeAny(null) throws NPE
1651       */
1652      public void testTimedInvokeAny1() throws Exception {
1653 <        ExecutorService e =
1653 >        final ExecutorService e =
1654              new ThreadPoolExecutor(2, 2,
1655                                     LONG_DELAY_MS, MILLISECONDS,
1656                                     new ArrayBlockingQueue<Runnable>(10));
1657 <        try {
1658 <            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1659 <            shouldThrow();
1660 <        } catch (NullPointerException success) {
1661 <        } finally {
1698 <            joinPool(e);
1657 >        try (PoolCleaner cleaner = cleaner(e)) {
1658 >            try {
1659 >                e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1660 >                shouldThrow();
1661 >            } catch (NullPointerException success) {}
1662          }
1663      }
1664  
# Line 1703 | Line 1666 | public class ThreadPoolExecutorTest exte
1666       * timed invokeAny(,,null) throws NPE
1667       */
1668      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1669 <        ExecutorService e =
1669 >        final ExecutorService e =
1670              new ThreadPoolExecutor(2, 2,
1671                                     LONG_DELAY_MS, MILLISECONDS,
1672                                     new ArrayBlockingQueue<Runnable>(10));
1673 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1674 <        l.add(new StringTask());
1675 <        try {
1676 <            e.invokeAny(l, MEDIUM_DELAY_MS, null);
1677 <            shouldThrow();
1678 <        } catch (NullPointerException success) {
1679 <        } finally {
1717 <            joinPool(e);
1673 >        try (PoolCleaner cleaner = cleaner(e)) {
1674 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1675 >            l.add(new StringTask());
1676 >            try {
1677 >                e.invokeAny(l, MEDIUM_DELAY_MS, null);
1678 >                shouldThrow();
1679 >            } catch (NullPointerException success) {}
1680          }
1681      }
1682  
# Line 1722 | Line 1684 | public class ThreadPoolExecutorTest exte
1684       * timed invokeAny(empty collection) throws IAE
1685       */
1686      public void testTimedInvokeAny2() throws Exception {
1687 <        ExecutorService e =
1687 >        final ExecutorService e =
1688              new ThreadPoolExecutor(2, 2,
1689                                     LONG_DELAY_MS, MILLISECONDS,
1690                                     new ArrayBlockingQueue<Runnable>(10));
1691 <        try {
1692 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1693 <            shouldThrow();
1694 <        } catch (IllegalArgumentException success) {
1695 <        } finally {
1696 <            joinPool(e);
1691 >        try (PoolCleaner cleaner = cleaner(e)) {
1692 >            try {
1693 >                e.invokeAny(new ArrayList<Callable<String>>(),
1694 >                            MEDIUM_DELAY_MS, MILLISECONDS);
1695 >                shouldThrow();
1696 >            } catch (IllegalArgumentException success) {}
1697          }
1698      }
1699  
# Line 1744 | Line 1706 | public class ThreadPoolExecutorTest exte
1706              new ThreadPoolExecutor(2, 2,
1707                                     LONG_DELAY_MS, MILLISECONDS,
1708                                     new ArrayBlockingQueue<Runnable>(10));
1709 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1710 <        l.add(latchAwaitingStringTask(latch));
1711 <        l.add(null);
1712 <        try {
1713 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1714 <            shouldThrow();
1715 <        } catch (NullPointerException success) {
1716 <        } finally {
1709 >        try (PoolCleaner cleaner = cleaner(e)) {
1710 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1711 >            l.add(latchAwaitingStringTask(latch));
1712 >            l.add(null);
1713 >            try {
1714 >                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1715 >                shouldThrow();
1716 >            } catch (NullPointerException success) {}
1717              latch.countDown();
1756            joinPool(e);
1718          }
1719      }
1720  
# Line 1761 | Line 1722 | public class ThreadPoolExecutorTest exte
1722       * timed invokeAny(c) throws ExecutionException if no task completes
1723       */
1724      public void testTimedInvokeAny4() throws Exception {
1725 <        ExecutorService e =
1725 >        final ExecutorService e =
1726              new ThreadPoolExecutor(2, 2,
1727                                     LONG_DELAY_MS, MILLISECONDS,
1728                                     new ArrayBlockingQueue<Runnable>(10));
1729 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1730 <        l.add(new NPETask());
1731 <        try {
1732 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1733 <            shouldThrow();
1734 <        } catch (ExecutionException success) {
1735 <            assertTrue(success.getCause() instanceof NullPointerException);
1736 <        } finally {
1737 <            joinPool(e);
1729 >        try (PoolCleaner cleaner = cleaner(e)) {
1730 >            long startTime = System.nanoTime();
1731 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1732 >            l.add(new NPETask());
1733 >            try {
1734 >                e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1735 >                shouldThrow();
1736 >            } catch (ExecutionException success) {
1737 >                assertTrue(success.getCause() instanceof NullPointerException);
1738 >            }
1739 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1740          }
1741      }
1742  
# Line 1781 | Line 1744 | public class ThreadPoolExecutorTest exte
1744       * timed invokeAny(c) returns result of some task
1745       */
1746      public void testTimedInvokeAny5() throws Exception {
1747 <        ExecutorService e =
1747 >        final ExecutorService e =
1748              new ThreadPoolExecutor(2, 2,
1749                                     LONG_DELAY_MS, MILLISECONDS,
1750                                     new ArrayBlockingQueue<Runnable>(10));
1751 <        try {
1751 >        try (PoolCleaner cleaner = cleaner(e)) {
1752              List<Callable<String>> l = new ArrayList<Callable<String>>();
1753              l.add(new StringTask());
1754              l.add(new StringTask());
1755              String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1756              assertSame(TEST_STRING, result);
1794        } finally {
1795            joinPool(e);
1757          }
1758      }
1759  
# Line 1800 | Line 1761 | public class ThreadPoolExecutorTest exte
1761       * timed invokeAll(null) throws NPE
1762       */
1763      public void testTimedInvokeAll1() throws Exception {
1764 <        ExecutorService e =
1764 >        final ExecutorService e =
1765              new ThreadPoolExecutor(2, 2,
1766                                     LONG_DELAY_MS, MILLISECONDS,
1767                                     new ArrayBlockingQueue<Runnable>(10));
1768 <        try {
1769 <            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1770 <            shouldThrow();
1771 <        } catch (NullPointerException success) {
1772 <        } finally {
1812 <            joinPool(e);
1768 >        try (PoolCleaner cleaner = cleaner(e)) {
1769 >            try {
1770 >                e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1771 >                shouldThrow();
1772 >            } catch (NullPointerException success) {}
1773          }
1774      }
1775  
# Line 1817 | Line 1777 | public class ThreadPoolExecutorTest exte
1777       * timed invokeAll(,,null) throws NPE
1778       */
1779      public void testTimedInvokeAllNullTimeUnit() throws Exception {
1780 <        ExecutorService e =
1780 >        final ExecutorService e =
1781              new ThreadPoolExecutor(2, 2,
1782                                     LONG_DELAY_MS, MILLISECONDS,
1783                                     new ArrayBlockingQueue<Runnable>(10));
1784 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1785 <        l.add(new StringTask());
1786 <        try {
1787 <            e.invokeAll(l, MEDIUM_DELAY_MS, null);
1788 <            shouldThrow();
1789 <        } catch (NullPointerException success) {
1790 <        } finally {
1831 <            joinPool(e);
1784 >        try (PoolCleaner cleaner = cleaner(e)) {
1785 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1786 >            l.add(new StringTask());
1787 >            try {
1788 >                e.invokeAll(l, MEDIUM_DELAY_MS, null);
1789 >                shouldThrow();
1790 >            } catch (NullPointerException success) {}
1791          }
1792      }
1793  
# Line 1836 | Line 1795 | public class ThreadPoolExecutorTest exte
1795       * timed invokeAll(empty collection) returns empty collection
1796       */
1797      public void testTimedInvokeAll2() throws InterruptedException {
1798 <        ExecutorService e =
1798 >        final ExecutorService e =
1799              new ThreadPoolExecutor(2, 2,
1800                                     LONG_DELAY_MS, MILLISECONDS,
1801                                     new ArrayBlockingQueue<Runnable>(10));
1802 <        try {
1803 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1802 >        try (PoolCleaner cleaner = cleaner(e)) {
1803 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(),
1804 >                                                 MEDIUM_DELAY_MS, MILLISECONDS);
1805              assertTrue(r.isEmpty());
1846        } finally {
1847            joinPool(e);
1806          }
1807      }
1808  
# Line 1852 | Line 1810 | public class ThreadPoolExecutorTest exte
1810       * timed invokeAll(c) throws NPE if c has null elements
1811       */
1812      public void testTimedInvokeAll3() throws Exception {
1813 <        ExecutorService e =
1813 >        final ExecutorService e =
1814              new ThreadPoolExecutor(2, 2,
1815                                     LONG_DELAY_MS, MILLISECONDS,
1816                                     new ArrayBlockingQueue<Runnable>(10));
1817 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1818 <        l.add(new StringTask());
1819 <        l.add(null);
1820 <        try {
1821 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1822 <            shouldThrow();
1823 <        } catch (NullPointerException success) {
1824 <        } finally {
1867 <            joinPool(e);
1817 >        try (PoolCleaner cleaner = cleaner(e)) {
1818 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1819 >            l.add(new StringTask());
1820 >            l.add(null);
1821 >            try {
1822 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1823 >                shouldThrow();
1824 >            } catch (NullPointerException success) {}
1825          }
1826      }
1827  
# Line 1872 | Line 1829 | public class ThreadPoolExecutorTest exte
1829       * get of element of invokeAll(c) throws exception on failed task
1830       */
1831      public void testTimedInvokeAll4() throws Exception {
1832 <        ExecutorService e =
1832 >        final ExecutorService e =
1833              new ThreadPoolExecutor(2, 2,
1834                                     LONG_DELAY_MS, MILLISECONDS,
1835                                     new ArrayBlockingQueue<Runnable>(10));
1836 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1837 <        l.add(new NPETask());
1838 <        List<Future<String>> futures =
1839 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1840 <        assertEquals(1, futures.size());
1841 <        try {
1842 <            futures.get(0).get();
1843 <            shouldThrow();
1844 <        } catch (ExecutionException success) {
1845 <            assertTrue(success.getCause() instanceof NullPointerException);
1846 <        } finally {
1847 <            joinPool(e);
1836 >        try (PoolCleaner cleaner = cleaner(e)) {
1837 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1838 >            l.add(new NPETask());
1839 >            List<Future<String>> futures =
1840 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1841 >            assertEquals(1, futures.size());
1842 >            try {
1843 >                futures.get(0).get();
1844 >                shouldThrow();
1845 >            } catch (ExecutionException success) {
1846 >                assertTrue(success.getCause() instanceof NullPointerException);
1847 >            }
1848          }
1849      }
1850  
# Line 1895 | Line 1852 | public class ThreadPoolExecutorTest exte
1852       * timed invokeAll(c) returns results of all completed tasks
1853       */
1854      public void testTimedInvokeAll5() throws Exception {
1855 <        ExecutorService e =
1855 >        final ExecutorService e =
1856              new ThreadPoolExecutor(2, 2,
1857                                     LONG_DELAY_MS, MILLISECONDS,
1858                                     new ArrayBlockingQueue<Runnable>(10));
1859 <        try {
1859 >        try (PoolCleaner cleaner = cleaner(e)) {
1860              List<Callable<String>> l = new ArrayList<Callable<String>>();
1861              l.add(new StringTask());
1862              l.add(new StringTask());
# Line 1908 | Line 1865 | public class ThreadPoolExecutorTest exte
1865              assertEquals(2, futures.size());
1866              for (Future<String> future : futures)
1867                  assertSame(TEST_STRING, future.get());
1911        } finally {
1912            joinPool(e);
1868          }
1869      }
1870  
# Line 1917 | Line 1872 | public class ThreadPoolExecutorTest exte
1872       * timed invokeAll(c) cancels tasks not completed by timeout
1873       */
1874      public void testTimedInvokeAll6() throws Exception {
1875 <        ExecutorService e =
1876 <            new ThreadPoolExecutor(2, 2,
1877 <                                   LONG_DELAY_MS, MILLISECONDS,
1878 <                                   new ArrayBlockingQueue<Runnable>(10));
1879 <        try {
1880 <            for (long timeout = timeoutMillis();;) {
1875 >        for (long timeout = timeoutMillis();;) {
1876 >            final CountDownLatch done = new CountDownLatch(1);
1877 >            final Callable<String> waiter = new CheckedCallable<String>() {
1878 >                public String realCall() {
1879 >                    try { done.await(LONG_DELAY_MS, MILLISECONDS); }
1880 >                    catch (InterruptedException ok) {}
1881 >                    return "1"; }};
1882 >            final ExecutorService p =
1883 >                new ThreadPoolExecutor(2, 2,
1884 >                                       LONG_DELAY_MS, MILLISECONDS,
1885 >                                       new ArrayBlockingQueue<Runnable>(10));
1886 >            try (PoolCleaner cleaner = cleaner(p, done)) {
1887                  List<Callable<String>> tasks = new ArrayList<>();
1888                  tasks.add(new StringTask("0"));
1889 <                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1889 >                tasks.add(waiter);
1890                  tasks.add(new StringTask("2"));
1891                  long startTime = System.nanoTime();
1892                  List<Future<String>> futures =
1893 <                    e.invokeAll(tasks, timeout, MILLISECONDS);
1893 >                    p.invokeAll(tasks, timeout, MILLISECONDS);
1894                  assertEquals(tasks.size(), futures.size());
1895                  assertTrue(millisElapsedSince(startTime) >= timeout);
1896                  for (Future future : futures)
# Line 1945 | Line 1906 | public class ThreadPoolExecutorTest exte
1906                          fail("expected exactly one task to be cancelled");
1907                  }
1908              }
1948        } finally {
1949            joinPool(e);
1909          }
1910      }
1911  
# Line 1960 | Line 1919 | public class ThreadPoolExecutorTest exte
1919                                     LONG_DELAY_MS, MILLISECONDS,
1920                                     new LinkedBlockingQueue<Runnable>(),
1921                                     new FailingThreadFactory());
1922 <        try {
1922 >        try (PoolCleaner cleaner = cleaner(e)) {
1923              final int TASKS = 100;
1924              final CountDownLatch done = new CountDownLatch(TASKS);
1925              for (int k = 0; k < TASKS; ++k)
# Line 1969 | Line 1928 | public class ThreadPoolExecutorTest exte
1928                          done.countDown();
1929                      }});
1930              assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1972        } finally {
1973            joinPool(e);
1931          }
1932      }
1933  
# Line 1982 | Line 1939 | public class ThreadPoolExecutorTest exte
1939              new ThreadPoolExecutor(2, 2,
1940                                     1000, MILLISECONDS,
1941                                     new ArrayBlockingQueue<Runnable>(10));
1942 <        assertFalse(p.allowsCoreThreadTimeOut());
1943 <        joinPool(p);
1942 >        try (PoolCleaner cleaner = cleaner(p)) {
1943 >            assertFalse(p.allowsCoreThreadTimeOut());
1944 >        }
1945      }
1946  
1947      /**
# Line 1995 | Line 1953 | public class ThreadPoolExecutorTest exte
1953              new ThreadPoolExecutor(2, 10,
1954                                     keepAliveTime, MILLISECONDS,
1955                                     new ArrayBlockingQueue<Runnable>(10));
1956 <        final CountDownLatch threadStarted = new CountDownLatch(1);
1957 <        try {
1956 >        try (PoolCleaner cleaner = cleaner(p)) {
1957 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1958              p.allowCoreThreadTimeOut(true);
1959              p.execute(new CheckedRunnable() {
1960                  public void realRun() {
# Line 2011 | Line 1969 | public class ThreadPoolExecutorTest exte
1969                  Thread.yield();
1970              assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1971              assertEquals(0, p.getPoolSize());
2014        } finally {
2015            joinPool(p);
1972          }
1973      }
1974  
# Line 2025 | Line 1981 | public class ThreadPoolExecutorTest exte
1981              new ThreadPoolExecutor(2, 10,
1982                                     keepAliveTime, MILLISECONDS,
1983                                     new ArrayBlockingQueue<Runnable>(10));
1984 <        final CountDownLatch threadStarted = new CountDownLatch(1);
1985 <        try {
1984 >        try (PoolCleaner cleaner = cleaner(p)) {
1985 >            final CountDownLatch threadStarted = new CountDownLatch(1);
1986              p.allowCoreThreadTimeOut(false);
1987              p.execute(new CheckedRunnable() {
1988                  public void realRun() throws InterruptedException {
# Line 2035 | Line 1991 | public class ThreadPoolExecutorTest exte
1991                  }});
1992              delay(2 * keepAliveTime);
1993              assertTrue(p.getPoolSize() >= 1);
2038        } finally {
2039            joinPool(p);
1994          }
1995      }
1996  
# Line 2055 | Line 2009 | public class ThreadPoolExecutorTest exte
2009              new ThreadPoolExecutor(1, 30,
2010                                     60, SECONDS,
2011                                     new ArrayBlockingQueue(30));
2012 <        try {
2012 >        try (PoolCleaner cleaner = cleaner(p)) {
2013              for (int i = 0; i < nTasks; ++i) {
2014                  for (;;) {
2015                      try {
# Line 2067 | Line 2021 | public class ThreadPoolExecutorTest exte
2021              }
2022              // enough time to run all tasks
2023              assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS));
2070        } finally {
2071            joinPool(p);
2024          }
2025      }
2026  
# Line 2076 | Line 2028 | public class ThreadPoolExecutorTest exte
2028       * get(cancelled task) throws CancellationException
2029       */
2030      public void testGet_cancelled() throws Exception {
2031 +        final CountDownLatch done = new CountDownLatch(1);
2032          final ExecutorService e =
2033              new ThreadPoolExecutor(1, 1,
2034                                     LONG_DELAY_MS, MILLISECONDS,
2035                                     new LinkedBlockingQueue<Runnable>());
2036 <        try {
2036 >        try (PoolCleaner cleaner = cleaner(e, done)) {
2037              final CountDownLatch blockerStarted = new CountDownLatch(1);
2085            final CountDownLatch done = new CountDownLatch(1);
2038              final List<Future<?>> futures = new ArrayList<>();
2039              for (int i = 0; i < 2; i++) {
2040                  Runnable r = new CheckedRunnable() { public void realRun()
# Line 2092 | Line 2044 | public class ThreadPoolExecutorTest exte
2044                  }};
2045                  futures.add(e.submit(r));
2046              }
2047 <            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
2047 >            await(blockerStarted);
2048              for (Future<?> future : futures) future.cancel(false);
2049              for (Future<?> future : futures) {
2050                  try {
# Line 2106 | Line 2058 | public class ThreadPoolExecutorTest exte
2058                  assertTrue(future.isCancelled());
2059                  assertTrue(future.isDone());
2060              }
2109            done.countDown();
2110        } finally {
2111            joinPool(e);
2061          }
2062      }
2063  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines