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.98 by jsr166, Sun Oct 4 07:23:20 2015 UTC vs.
Revision 1.116 by jsr166, Wed Mar 22 20:19:55 2017 UTC

# Line 18 | Line 18 | import java.util.concurrent.Callable;
18   import java.util.concurrent.CancellationException;
19   import java.util.concurrent.CountDownLatch;
20   import java.util.concurrent.ExecutionException;
21 import java.util.concurrent.Executors;
21   import java.util.concurrent.ExecutorService;
22   import java.util.concurrent.Future;
23   import java.util.concurrent.FutureTask;
# Line 28 | Line 27 | import java.util.concurrent.RejectedExec
27   import java.util.concurrent.SynchronousQueue;
28   import java.util.concurrent.ThreadFactory;
29   import java.util.concurrent.ThreadPoolExecutor;
31 import java.util.concurrent.TimeUnit;
30   import java.util.concurrent.atomic.AtomicInteger;
31  
32   import junit.framework.Test;
# Line 101 | Line 99 | public class ThreadPoolExecutorTest exte
99       * thread becomes active
100       */
101      public void testGetActiveCount() throws InterruptedException {
102 +        final CountDownLatch done = new CountDownLatch(1);
103          final ThreadPoolExecutor p =
104              new ThreadPoolExecutor(2, 2,
105                                     LONG_DELAY_MS, MILLISECONDS,
106                                     new ArrayBlockingQueue<Runnable>(10));
107 <        try (PoolCleaner cleaner = cleaner(p)) {
107 >        try (PoolCleaner cleaner = cleaner(p, done)) {
108              final CountDownLatch threadStarted = new CountDownLatch(1);
110            final CountDownLatch done = new CountDownLatch(1);
109              assertEquals(0, p.getActiveCount());
110              p.execute(new CheckedRunnable() {
111                  public void realRun() throws InterruptedException {
112                      threadStarted.countDown();
113                      assertEquals(1, p.getActiveCount());
114 <                    done.await();
114 >                    await(done);
115                  }});
116 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
116 >            await(threadStarted);
117              assertEquals(1, p.getActiveCount());
120            done.countDown();
118          }
119      }
120  
# Line 277 | Line 274 | public class ThreadPoolExecutorTest exte
274      }
275  
276      /**
277 +     * The default rejected execution handler is AbortPolicy.
278 +     */
279 +    public void testDefaultRejectedExecutionHandler() {
280 +        final ThreadPoolExecutor p =
281 +            new ThreadPoolExecutor(1, 2,
282 +                                   LONG_DELAY_MS, MILLISECONDS,
283 +                                   new ArrayBlockingQueue<Runnable>(10));
284 +        try (PoolCleaner cleaner = cleaner(p)) {
285 +            assertTrue(p.getRejectedExecutionHandler()
286 +                       instanceof ThreadPoolExecutor.AbortPolicy);
287 +        }
288 +    }
289 +
290 +    /**
291       * getRejectedExecutionHandler returns handler in constructor if not set
292       */
293      public void testGetRejectedExecutionHandler() {
# Line 329 | Line 340 | public class ThreadPoolExecutorTest exte
340       */
341      public void testGetLargestPoolSize() throws InterruptedException {
342          final int THREADS = 3;
343 +        final CountDownLatch done = new CountDownLatch(1);
344          final ThreadPoolExecutor p =
345              new ThreadPoolExecutor(THREADS, THREADS,
346                                     LONG_DELAY_MS, MILLISECONDS,
347                                     new ArrayBlockingQueue<Runnable>(10));
348 <        try (PoolCleaner cleaner = cleaner(p)) {
337 <            final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
338 <            final CountDownLatch done = new CountDownLatch(1);
348 >        try (PoolCleaner cleaner = cleaner(p, done)) {
349              assertEquals(0, p.getLargestPoolSize());
350 +            final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
351              for (int i = 0; i < THREADS; i++)
352                  p.execute(new CheckedRunnable() {
353                      public void realRun() throws InterruptedException {
354                          threadsStarted.countDown();
355 <                        done.await();
355 >                        await(done);
356                          assertEquals(THREADS, p.getLargestPoolSize());
357                      }});
358 <            assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
358 >            await(threadsStarted);
359              assertEquals(THREADS, p.getLargestPoolSize());
349            done.countDown();   // release pool
360          }
361          assertEquals(THREADS, p.getLargestPoolSize());
362      }
# Line 374 | Line 384 | public class ThreadPoolExecutorTest exte
384       * become active
385       */
386      public void testGetPoolSize() throws InterruptedException {
387 +        final CountDownLatch done = new CountDownLatch(1);
388          final ThreadPoolExecutor p =
389              new ThreadPoolExecutor(1, 1,
390                                     LONG_DELAY_MS, MILLISECONDS,
391                                     new ArrayBlockingQueue<Runnable>(10));
392 <        try (PoolCleaner cleaner = cleaner(p)) {
382 <            final CountDownLatch threadStarted = new CountDownLatch(1);
383 <            final CountDownLatch done = new CountDownLatch(1);
392 >        try (PoolCleaner cleaner = cleaner(p, done)) {
393              assertEquals(0, p.getPoolSize());
394 +            final CountDownLatch threadStarted = new CountDownLatch(1);
395              p.execute(new CheckedRunnable() {
396                  public void realRun() throws InterruptedException {
397                      threadStarted.countDown();
398                      assertEquals(1, p.getPoolSize());
399 <                    done.await();
399 >                    await(done);
400                  }});
401 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
401 >            await(threadStarted);
402              assertEquals(1, p.getPoolSize());
393            done.countDown();   // release pool
403          }
404      }
405  
# Line 398 | Line 407 | public class ThreadPoolExecutorTest exte
407       * getTaskCount increases, but doesn't overestimate, when tasks submitted
408       */
409      public void testGetTaskCount() throws InterruptedException {
410 +        final int TASKS = 3;
411 +        final CountDownLatch done = new CountDownLatch(1);
412          final ThreadPoolExecutor p =
413              new ThreadPoolExecutor(1, 1,
414                                     LONG_DELAY_MS, MILLISECONDS,
415                                     new ArrayBlockingQueue<Runnable>(10));
416 <        try (PoolCleaner cleaner = cleaner(p)) {
416 >        try (PoolCleaner cleaner = cleaner(p, done)) {
417              final CountDownLatch threadStarted = new CountDownLatch(1);
407            final CountDownLatch done = new CountDownLatch(1);
418              assertEquals(0, p.getTaskCount());
419 +            assertEquals(0, p.getCompletedTaskCount());
420              p.execute(new CheckedRunnable() {
421                  public void realRun() throws InterruptedException {
422                      threadStarted.countDown();
423 <                    assertEquals(1, p.getTaskCount());
413 <                    done.await();
423 >                    await(done);
424                  }});
425 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
425 >            await(threadStarted);
426              assertEquals(1, p.getTaskCount());
427 <            done.countDown();
427 >            assertEquals(0, p.getCompletedTaskCount());
428 >            for (int i = 0; i < TASKS; i++) {
429 >                assertEquals(1 + i, p.getTaskCount());
430 >                p.execute(new CheckedRunnable() {
431 >                    public void realRun() throws InterruptedException {
432 >                        threadStarted.countDown();
433 >                        assertEquals(1 + TASKS, p.getTaskCount());
434 >                        await(done);
435 >                    }});
436 >            }
437 >            assertEquals(1 + TASKS, p.getTaskCount());
438 >            assertEquals(0, p.getCompletedTaskCount());
439          }
440 +        assertEquals(1 + TASKS, p.getTaskCount());
441 +        assertEquals(1 + TASKS, p.getCompletedTaskCount());
442      }
443  
444      /**
# Line 481 | Line 504 | public class ThreadPoolExecutorTest exte
504                  public void realRun() throws InterruptedException {
505                      assertFalse(p.isTerminating());
506                      threadStarted.countDown();
507 <                    done.await();
507 >                    await(done);
508                  }});
509 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
509 >            await(threadStarted);
510              assertFalse(p.isTerminating());
511              done.countDown();
512              try { p.shutdown(); } catch (SecurityException ok) { return; }
# Line 509 | Line 532 | public class ThreadPoolExecutorTest exte
532                  public void realRun() throws InterruptedException {
533                      assertFalse(p.isTerminating());
534                      threadStarted.countDown();
535 <                    done.await();
535 >                    await(done);
536                  }});
537 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
537 >            await(threadStarted);
538              assertFalse(p.isTerminating());
539              done.countDown();
540              try { p.shutdown(); } catch (SecurityException ok) { return; }
# Line 525 | Line 548 | public class ThreadPoolExecutorTest exte
548       * getQueue returns the work queue, which contains queued tasks
549       */
550      public void testGetQueue() throws InterruptedException {
551 <        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
551 >        final CountDownLatch done = new CountDownLatch(1);
552 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
553          final ThreadPoolExecutor p =
554              new ThreadPoolExecutor(1, 1,
555                                     LONG_DELAY_MS, MILLISECONDS,
556                                     q);
557 <        try (PoolCleaner cleaner = cleaner(p)) {
557 >        try (PoolCleaner cleaner = cleaner(p, done)) {
558              final CountDownLatch threadStarted = new CountDownLatch(1);
535            final CountDownLatch done = new CountDownLatch(1);
559              FutureTask[] tasks = new FutureTask[5];
560              for (int i = 0; i < tasks.length; i++) {
561                  Callable task = new CheckedCallable<Boolean>() {
562                      public Boolean realCall() throws InterruptedException {
563                          threadStarted.countDown();
564                          assertSame(q, p.getQueue());
565 <                        done.await();
565 >                        await(done);
566                          return Boolean.TRUE;
567                      }};
568                  tasks[i] = new FutureTask(task);
569                  p.execute(tasks[i]);
570              }
571 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
571 >            await(threadStarted);
572              assertSame(q, p.getQueue());
573              assertFalse(q.contains(tasks[0]));
574              assertTrue(q.contains(tasks[tasks.length - 1]));
575              assertEquals(tasks.length - 1, q.size());
553            done.countDown();
576          }
577      }
578  
# Line 558 | Line 580 | public class ThreadPoolExecutorTest exte
580       * remove(task) removes queued task, and fails to remove active task
581       */
582      public void testRemove() throws InterruptedException {
583 <        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
583 >        final CountDownLatch done = new CountDownLatch(1);
584 >        BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
585          final ThreadPoolExecutor p =
586              new ThreadPoolExecutor(1, 1,
587                                     LONG_DELAY_MS, MILLISECONDS,
588                                     q);
589 <        try (PoolCleaner cleaner = cleaner(p)) {
589 >        try (PoolCleaner cleaner = cleaner(p, done)) {
590              Runnable[] tasks = new Runnable[6];
591              final CountDownLatch threadStarted = new CountDownLatch(1);
569            final CountDownLatch done = new CountDownLatch(1);
592              for (int i = 0; i < tasks.length; i++) {
593                  tasks[i] = new CheckedRunnable() {
594                      public void realRun() throws InterruptedException {
595                          threadStarted.countDown();
596 <                        done.await();
596 >                        await(done);
597                      }};
598                  p.execute(tasks[i]);
599              }
600 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
600 >            await(threadStarted);
601              assertFalse(p.remove(tasks[0]));
602              assertTrue(q.contains(tasks[4]));
603              assertTrue(q.contains(tasks[3]));
# Line 585 | Line 607 | public class ThreadPoolExecutorTest exte
607              assertTrue(q.contains(tasks[3]));
608              assertTrue(p.remove(tasks[3]));
609              assertFalse(q.contains(tasks[3]));
588            done.countDown();
610          }
611      }
612  
# Line 595 | Line 616 | public class ThreadPoolExecutorTest exte
616      public void testPurge() throws InterruptedException {
617          final CountDownLatch threadStarted = new CountDownLatch(1);
618          final CountDownLatch done = new CountDownLatch(1);
619 <        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
619 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
620          final ThreadPoolExecutor p =
621              new ThreadPoolExecutor(1, 1,
622                                     LONG_DELAY_MS, MILLISECONDS,
623                                     q);
624 <        try (PoolCleaner cleaner = cleaner(p)) {
624 >        try (PoolCleaner cleaner = cleaner(p, done)) {
625              FutureTask[] tasks = new FutureTask[5];
626              for (int i = 0; i < tasks.length; i++) {
627                  Callable task = new CheckedCallable<Boolean>() {
628                      public Boolean realCall() throws InterruptedException {
629                          threadStarted.countDown();
630 <                        done.await();
630 >                        await(done);
631                          return Boolean.TRUE;
632                      }};
633                  tasks[i] = new FutureTask(task);
634                  p.execute(tasks[i]);
635              }
636 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
636 >            await(threadStarted);
637              assertEquals(tasks.length, p.getTaskCount());
638              assertEquals(tasks.length - 1, q.size());
639              assertEquals(1L, p.getActiveCount());
# Line 625 | Line 646 | public class ThreadPoolExecutorTest exte
646              p.purge();         // Nothing to do
647              assertEquals(tasks.length - 3, q.size());
648              assertEquals(tasks.length - 2, p.getTaskCount());
628            done.countDown();
649          }
650      }
651  
# Line 641 | Line 661 | public class ThreadPoolExecutorTest exte
661              new ThreadPoolExecutor(poolSize, poolSize,
662                                     LONG_DELAY_MS, MILLISECONDS,
663                                     new ArrayBlockingQueue<Runnable>(10));
664 <        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
664 >        final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
665          Runnable waiter = new CheckedRunnable() { public void realRun() {
666              threadsStarted.countDown();
667              try {
# Line 651 | Line 671 | public class ThreadPoolExecutorTest exte
671          }};
672          for (int i = 0; i < count; i++)
673              p.execute(waiter);
674 <        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
674 >        await(threadsStarted);
675          assertEquals(poolSize, p.getActiveCount());
676          assertEquals(0, p.getCompletedTaskCount());
677          final List<Runnable> queuedTasks;
# Line 1013 | Line 1033 | public class ThreadPoolExecutorTest exte
1033       * get of submitted callable throws InterruptedException if interrupted
1034       */
1035      public void testInterruptedSubmit() throws InterruptedException {
1036 +        final CountDownLatch done = new CountDownLatch(1);
1037          final ThreadPoolExecutor p =
1038              new ThreadPoolExecutor(1, 1,
1039                                     60, SECONDS,
1040                                     new ArrayBlockingQueue<Runnable>(10));
1041  
1042 <        try (PoolCleaner cleaner = cleaner(p)) {
1042 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1043              final CountDownLatch threadStarted = new CountDownLatch(1);
1023            final CountDownLatch done = new CountDownLatch(1);
1044              Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1045                  public void realRun() throws Exception {
1046                      Callable task = new CheckedCallable<Boolean>() {
1047                          public Boolean realCall() throws InterruptedException {
1048                              threadStarted.countDown();
1049 <                            done.await();
1049 >                            await(done);
1050                              return Boolean.TRUE;
1051                          }};
1052                      p.submit(task).get();
1053                  }});
1054  
1055 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
1055 >            await(threadStarted);
1056              t.interrupt();
1057 <            awaitTermination(t, MEDIUM_DELAY_MS);
1038 <            done.countDown();
1057 >            awaitTermination(t);
1058          }
1059      }
1060  
# Line 1043 | Line 1062 | public class ThreadPoolExecutorTest exte
1062       * execute throws RejectedExecutionException if saturated.
1063       */
1064      public void testSaturatedExecute() {
1065 +        final CountDownLatch done = new CountDownLatch(1);
1066          final ThreadPoolExecutor p =
1067              new ThreadPoolExecutor(1, 1,
1068                                     LONG_DELAY_MS, MILLISECONDS,
1069                                     new ArrayBlockingQueue<Runnable>(1));
1070 <        try (PoolCleaner cleaner = cleaner(p)) {
1051 <            final CountDownLatch done = new CountDownLatch(1);
1070 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1071              Runnable task = new CheckedRunnable() {
1072                  public void realRun() throws InterruptedException {
1073 <                    done.await();
1073 >                    await(done);
1074                  }};
1075              for (int i = 0; i < 2; ++i)
1076                  p.execute(task);
# Line 1062 | Line 1081 | public class ThreadPoolExecutorTest exte
1081                  } catch (RejectedExecutionException success) {}
1082                  assertTrue(p.getTaskCount() <= 2);
1083              }
1065            done.countDown();
1084          }
1085      }
1086  
# Line 1070 | Line 1088 | public class ThreadPoolExecutorTest exte
1088       * submit(runnable) throws RejectedExecutionException if saturated.
1089       */
1090      public void testSaturatedSubmitRunnable() {
1091 +        final CountDownLatch done = new CountDownLatch(1);
1092          final ThreadPoolExecutor p =
1093              new ThreadPoolExecutor(1, 1,
1094                                     LONG_DELAY_MS, MILLISECONDS,
1095                                     new ArrayBlockingQueue<Runnable>(1));
1096 <        try (PoolCleaner cleaner = cleaner(p)) {
1078 <            final CountDownLatch done = new CountDownLatch(1);
1096 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1097              Runnable task = new CheckedRunnable() {
1098                  public void realRun() throws InterruptedException {
1099 <                    done.await();
1099 >                    await(done);
1100                  }};
1101              for (int i = 0; i < 2; ++i)
1102                  p.submit(task);
# Line 1089 | Line 1107 | public class ThreadPoolExecutorTest exte
1107                  } catch (RejectedExecutionException success) {}
1108                  assertTrue(p.getTaskCount() <= 2);
1109              }
1092            done.countDown();
1110          }
1111      }
1112  
# Line 1097 | Line 1114 | public class ThreadPoolExecutorTest exte
1114       * submit(callable) throws RejectedExecutionException if saturated.
1115       */
1116      public void testSaturatedSubmitCallable() {
1117 +        final CountDownLatch done = new CountDownLatch(1);
1118          final ThreadPoolExecutor p =
1119              new ThreadPoolExecutor(1, 1,
1120                                     LONG_DELAY_MS, MILLISECONDS,
1121                                     new ArrayBlockingQueue<Runnable>(1));
1122 <        try (PoolCleaner cleaner = cleaner(p)) {
1105 <            final CountDownLatch done = new CountDownLatch(1);
1122 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1123              Runnable task = new CheckedRunnable() {
1124                  public void realRun() throws InterruptedException {
1125 <                    done.await();
1125 >                    await(done);
1126                  }};
1127              for (int i = 0; i < 2; ++i)
1128 <                p.submit(Executors.callable(task));
1128 >                p.execute(task);
1129              for (int i = 0; i < 2; ++i) {
1130                  try {
1131                      p.execute(task);
# Line 1116 | Line 1133 | public class ThreadPoolExecutorTest exte
1133                  } catch (RejectedExecutionException success) {}
1134                  assertTrue(p.getTaskCount() <= 2);
1135              }
1119            done.countDown();
1136          }
1137      }
1138  
# Line 1134 | Line 1150 | public class ThreadPoolExecutorTest exte
1150              final CountDownLatch done = new CountDownLatch(1);
1151              Runnable blocker = new CheckedRunnable() {
1152                  public void realRun() throws InterruptedException {
1153 <                    done.await();
1153 >                    await(done);
1154                  }};
1155              p.execute(blocker);
1156              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
# Line 1153 | Line 1169 | public class ThreadPoolExecutorTest exte
1169       * executor using DiscardPolicy drops task if saturated.
1170       */
1171      public void testSaturatedExecute3() {
1172 +        final CountDownLatch done = new CountDownLatch(1);
1173          final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1174          for (int i = 0; i < tasks.length; ++i)
1175              tasks[i] = new TrackedNoOpRunnable();
# Line 1161 | Line 1178 | public class ThreadPoolExecutorTest exte
1178                            LONG_DELAY_MS, MILLISECONDS,
1179                            new ArrayBlockingQueue<Runnable>(1),
1180                            new ThreadPoolExecutor.DiscardPolicy());
1181 <        try (PoolCleaner cleaner = cleaner(p)) {
1165 <            final CountDownLatch done = new CountDownLatch(1);
1181 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1182              p.execute(awaiter(done));
1183  
1184              for (TrackedNoOpRunnable task : tasks)
1185                  p.execute(task);
1186              for (int i = 1; i < tasks.length; i++)
1187                  assertFalse(tasks[i].done);
1172            done.countDown();
1188          }
1189          for (int i = 1; i < tasks.length; i++)
1190              assertFalse(tasks[i].done);
# Line 1189 | Line 1204 | public class ThreadPoolExecutorTest exte
1204                                     LONG_DELAY_MS, MILLISECONDS,
1205                                     new ArrayBlockingQueue<Runnable>(1),
1206                                     new ThreadPoolExecutor.DiscardOldestPolicy());
1207 <        try (PoolCleaner cleaner = cleaner(p)) {
1207 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1208              assertEquals(LatchAwaiter.NEW, r1.state);
1209              assertEquals(LatchAwaiter.NEW, r2.state);
1210              assertEquals(LatchAwaiter.NEW, r3.state);
# Line 1199 | Line 1214 | public class ThreadPoolExecutorTest exte
1214              p.execute(r3);
1215              assertFalse(p.getQueue().contains(r2));
1216              assertTrue(p.getQueue().contains(r3));
1202            done.countDown();
1217          }
1218          assertEquals(LatchAwaiter.DONE, r1.state);
1219          assertEquals(LatchAwaiter.NEW, r2.state);
# Line 1506 | Line 1520 | public class ThreadPoolExecutorTest exte
1520                                     LONG_DELAY_MS, MILLISECONDS,
1521                                     new ArrayBlockingQueue<Runnable>(10));
1522          try (PoolCleaner cleaner = cleaner(e)) {
1523 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1523 >            List<Callable<String>> l = new ArrayList<>();
1524              l.add(latchAwaitingStringTask(latch));
1525              l.add(null);
1526              try {
# Line 1526 | Line 1540 | public class ThreadPoolExecutorTest exte
1540                                     LONG_DELAY_MS, MILLISECONDS,
1541                                     new ArrayBlockingQueue<Runnable>(10));
1542          try (PoolCleaner cleaner = cleaner(e)) {
1543 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1543 >            List<Callable<String>> l = new ArrayList<>();
1544              l.add(new NPETask());
1545              try {
1546                  e.invokeAny(l);
# Line 1546 | Line 1560 | public class ThreadPoolExecutorTest exte
1560                                     LONG_DELAY_MS, MILLISECONDS,
1561                                     new ArrayBlockingQueue<Runnable>(10));
1562          try (PoolCleaner cleaner = cleaner(e)) {
1563 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1563 >            List<Callable<String>> l = new ArrayList<>();
1564              l.add(new StringTask());
1565              l.add(new StringTask());
1566              String result = e.invokeAny(l);
# Line 1593 | Line 1607 | public class ThreadPoolExecutorTest exte
1607                                     LONG_DELAY_MS, MILLISECONDS,
1608                                     new ArrayBlockingQueue<Runnable>(10));
1609          try (PoolCleaner cleaner = cleaner(e)) {
1610 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1610 >            List<Callable<String>> l = new ArrayList<>();
1611              l.add(new StringTask());
1612              l.add(null);
1613              try {
# Line 1612 | Line 1626 | public class ThreadPoolExecutorTest exte
1626                                     LONG_DELAY_MS, MILLISECONDS,
1627                                     new ArrayBlockingQueue<Runnable>(10));
1628          try (PoolCleaner cleaner = cleaner(e)) {
1629 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1629 >            List<Callable<String>> l = new ArrayList<>();
1630              l.add(new NPETask());
1631              List<Future<String>> futures = e.invokeAll(l);
1632              assertEquals(1, futures.size());
# Line 1634 | Line 1648 | public class ThreadPoolExecutorTest exte
1648                                     LONG_DELAY_MS, MILLISECONDS,
1649                                     new ArrayBlockingQueue<Runnable>(10));
1650          try (PoolCleaner cleaner = cleaner(e)) {
1651 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1651 >            List<Callable<String>> l = new ArrayList<>();
1652              l.add(new StringTask());
1653              l.add(new StringTask());
1654              List<Future<String>> futures = e.invokeAll(l);
# Line 1669 | Line 1683 | public class ThreadPoolExecutorTest exte
1683                                     LONG_DELAY_MS, MILLISECONDS,
1684                                     new ArrayBlockingQueue<Runnable>(10));
1685          try (PoolCleaner cleaner = cleaner(e)) {
1686 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1686 >            List<Callable<String>> l = new ArrayList<>();
1687              l.add(new StringTask());
1688              try {
1689                  e.invokeAny(l, MEDIUM_DELAY_MS, null);
# Line 1705 | Line 1719 | public class ThreadPoolExecutorTest exte
1719                                     LONG_DELAY_MS, MILLISECONDS,
1720                                     new ArrayBlockingQueue<Runnable>(10));
1721          try (PoolCleaner cleaner = cleaner(e)) {
1722 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1722 >            List<Callable<String>> l = new ArrayList<>();
1723              l.add(latchAwaitingStringTask(latch));
1724              l.add(null);
1725              try {
# Line 1725 | Line 1739 | public class ThreadPoolExecutorTest exte
1739                                     LONG_DELAY_MS, MILLISECONDS,
1740                                     new ArrayBlockingQueue<Runnable>(10));
1741          try (PoolCleaner cleaner = cleaner(e)) {
1742 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1742 >            long startTime = System.nanoTime();
1743 >            List<Callable<String>> l = new ArrayList<>();
1744              l.add(new NPETask());
1745              try {
1746 <                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1746 >                e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1747                  shouldThrow();
1748              } catch (ExecutionException success) {
1749                  assertTrue(success.getCause() instanceof NullPointerException);
1750              }
1751 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1752          }
1753      }
1754  
# Line 1745 | Line 1761 | public class ThreadPoolExecutorTest exte
1761                                     LONG_DELAY_MS, MILLISECONDS,
1762                                     new ArrayBlockingQueue<Runnable>(10));
1763          try (PoolCleaner cleaner = cleaner(e)) {
1764 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1764 >            long startTime = System.nanoTime();
1765 >            List<Callable<String>> l = new ArrayList<>();
1766              l.add(new StringTask());
1767              l.add(new StringTask());
1768 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1768 >            String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1769              assertSame(TEST_STRING, result);
1770 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1771          }
1772      }
1773  
# Line 1778 | Line 1796 | public class ThreadPoolExecutorTest exte
1796                                     LONG_DELAY_MS, MILLISECONDS,
1797                                     new ArrayBlockingQueue<Runnable>(10));
1798          try (PoolCleaner cleaner = cleaner(e)) {
1799 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1799 >            List<Callable<String>> l = new ArrayList<>();
1800              l.add(new StringTask());
1801              try {
1802                  e.invokeAll(l, MEDIUM_DELAY_MS, null);
# Line 1811 | Line 1829 | public class ThreadPoolExecutorTest exte
1829                                     LONG_DELAY_MS, MILLISECONDS,
1830                                     new ArrayBlockingQueue<Runnable>(10));
1831          try (PoolCleaner cleaner = cleaner(e)) {
1832 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1832 >            List<Callable<String>> l = new ArrayList<>();
1833              l.add(new StringTask());
1834              l.add(null);
1835              try {
# Line 1830 | Line 1848 | public class ThreadPoolExecutorTest exte
1848                                     LONG_DELAY_MS, MILLISECONDS,
1849                                     new ArrayBlockingQueue<Runnable>(10));
1850          try (PoolCleaner cleaner = cleaner(e)) {
1851 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1851 >            List<Callable<String>> l = new ArrayList<>();
1852              l.add(new NPETask());
1853              List<Future<String>> futures =
1854 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1854 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1855              assertEquals(1, futures.size());
1856              try {
1857                  futures.get(0).get();
# Line 1853 | Line 1871 | public class ThreadPoolExecutorTest exte
1871                                     LONG_DELAY_MS, MILLISECONDS,
1872                                     new ArrayBlockingQueue<Runnable>(10));
1873          try (PoolCleaner cleaner = cleaner(e)) {
1874 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1874 >            List<Callable<String>> l = new ArrayList<>();
1875              l.add(new StringTask());
1876              l.add(new StringTask());
1877              List<Future<String>> futures =
# Line 1868 | Line 1886 | public class ThreadPoolExecutorTest exte
1886       * timed invokeAll(c) cancels tasks not completed by timeout
1887       */
1888      public void testTimedInvokeAll6() throws Exception {
1889 <        final ExecutorService e =
1890 <            new ThreadPoolExecutor(2, 2,
1891 <                                   LONG_DELAY_MS, MILLISECONDS,
1892 <                                   new ArrayBlockingQueue<Runnable>(10));
1893 <        try (PoolCleaner cleaner = cleaner(e)) {
1894 <            for (long timeout = timeoutMillis();;) {
1889 >        for (long timeout = timeoutMillis();;) {
1890 >            final CountDownLatch done = new CountDownLatch(1);
1891 >            final Callable<String> waiter = new CheckedCallable<String>() {
1892 >                public String realCall() {
1893 >                    try { done.await(LONG_DELAY_MS, MILLISECONDS); }
1894 >                    catch (InterruptedException ok) {}
1895 >                    return "1"; }};
1896 >            final ExecutorService p =
1897 >                new ThreadPoolExecutor(2, 2,
1898 >                                       LONG_DELAY_MS, MILLISECONDS,
1899 >                                       new ArrayBlockingQueue<Runnable>(10));
1900 >            try (PoolCleaner cleaner = cleaner(p, done)) {
1901                  List<Callable<String>> tasks = new ArrayList<>();
1902                  tasks.add(new StringTask("0"));
1903 <                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1903 >                tasks.add(waiter);
1904                  tasks.add(new StringTask("2"));
1905                  long startTime = System.nanoTime();
1906                  List<Future<String>> futures =
1907 <                    e.invokeAll(tasks, timeout, MILLISECONDS);
1907 >                    p.invokeAll(tasks, timeout, MILLISECONDS);
1908                  assertEquals(tasks.size(), futures.size());
1909                  assertTrue(millisElapsedSince(startTime) >= timeout);
1910                  for (Future future : futures)
# Line 2018 | Line 2042 | public class ThreadPoolExecutorTest exte
2042       * get(cancelled task) throws CancellationException
2043       */
2044      public void testGet_cancelled() throws Exception {
2045 +        final CountDownLatch done = new CountDownLatch(1);
2046          final ExecutorService e =
2047              new ThreadPoolExecutor(1, 1,
2048                                     LONG_DELAY_MS, MILLISECONDS,
2049                                     new LinkedBlockingQueue<Runnable>());
2050 <        try (PoolCleaner cleaner = cleaner(e)) {
2050 >        try (PoolCleaner cleaner = cleaner(e, done)) {
2051              final CountDownLatch blockerStarted = new CountDownLatch(1);
2027            final CountDownLatch done = new CountDownLatch(1);
2052              final List<Future<?>> futures = new ArrayList<>();
2053              for (int i = 0; i < 2; i++) {
2054                  Runnable r = new CheckedRunnable() { public void realRun()
# Line 2034 | Line 2058 | public class ThreadPoolExecutorTest exte
2058                  }};
2059                  futures.add(e.submit(r));
2060              }
2061 <            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
2061 >            await(blockerStarted);
2062              for (Future<?> future : futures) future.cancel(false);
2063              for (Future<?> future : futures) {
2064                  try {
# Line 2048 | Line 2072 | public class ThreadPoolExecutorTest exte
2072                  assertTrue(future.isCancelled());
2073                  assertTrue(future.isDone());
2074              }
2051            done.countDown();
2075          }
2076      }
2077  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines