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.122 by jsr166, Sat Jul 15 18:42:01 2017 UTC

# Line 11 | Line 11 | import static java.util.concurrent.TimeU
11   import static java.util.concurrent.TimeUnit.SECONDS;
12  
13   import java.util.ArrayList;
14 + import java.util.Collection;
15 + import java.util.Collections;
16   import java.util.List;
17   import java.util.concurrent.ArrayBlockingQueue;
18   import java.util.concurrent.BlockingQueue;
# Line 18 | Line 20 | import java.util.concurrent.Callable;
20   import java.util.concurrent.CancellationException;
21   import java.util.concurrent.CountDownLatch;
22   import java.util.concurrent.ExecutionException;
21 import java.util.concurrent.Executors;
23   import java.util.concurrent.ExecutorService;
24   import java.util.concurrent.Future;
25   import java.util.concurrent.FutureTask;
# Line 28 | Line 29 | import java.util.concurrent.RejectedExec
29   import java.util.concurrent.SynchronousQueue;
30   import java.util.concurrent.ThreadFactory;
31   import java.util.concurrent.ThreadPoolExecutor;
32 < import java.util.concurrent.TimeUnit;
32 > import java.util.concurrent.ThreadPoolExecutor.AbortPolicy;
33 > import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy;
34 > import java.util.concurrent.ThreadPoolExecutor.DiscardPolicy;
35 > import java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy;
36   import java.util.concurrent.atomic.AtomicInteger;
37 + import java.util.concurrent.atomic.AtomicReference;
38  
39   import junit.framework.Test;
40   import junit.framework.TestSuite;
# Line 92 | Line 97 | public class ThreadPoolExecutorTest exte
97              final Runnable task = new CheckedRunnable() {
98                  public void realRun() { done.countDown(); }};
99              p.execute(task);
100 <            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
100 >            await(done);
101          }
102      }
103  
# Line 101 | Line 106 | public class ThreadPoolExecutorTest exte
106       * thread becomes active
107       */
108      public void testGetActiveCount() throws InterruptedException {
109 +        final CountDownLatch done = new CountDownLatch(1);
110          final ThreadPoolExecutor p =
111              new ThreadPoolExecutor(2, 2,
112                                     LONG_DELAY_MS, MILLISECONDS,
113                                     new ArrayBlockingQueue<Runnable>(10));
114 <        try (PoolCleaner cleaner = cleaner(p)) {
114 >        try (PoolCleaner cleaner = cleaner(p, done)) {
115              final CountDownLatch threadStarted = new CountDownLatch(1);
110            final CountDownLatch done = new CountDownLatch(1);
116              assertEquals(0, p.getActiveCount());
117              p.execute(new CheckedRunnable() {
118                  public void realRun() throws InterruptedException {
119                      threadStarted.countDown();
120                      assertEquals(1, p.getActiveCount());
121 <                    done.await();
121 >                    await(done);
122                  }});
123 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
123 >            await(threadStarted);
124              assertEquals(1, p.getActiveCount());
120            done.countDown();
125          }
126      }
127  
# Line 187 | Line 191 | public class ThreadPoolExecutorTest exte
191                  public void realRun() throws InterruptedException {
192                      threadStarted.countDown();
193                      assertEquals(0, p.getCompletedTaskCount());
194 <                    threadProceed.await();
194 >                    await(threadProceed);
195                      threadDone.countDown();
196                  }});
197              await(threadStarted);
198              assertEquals(0, p.getCompletedTaskCount());
199              threadProceed.countDown();
200 <            threadDone.await();
200 >            await(threadDone);
201              long startTime = System.nanoTime();
202              while (p.getCompletedTaskCount() != 1) {
203                  if (millisElapsedSince(startTime) > LONG_DELAY_MS)
# Line 277 | Line 281 | public class ThreadPoolExecutorTest exte
281      }
282  
283      /**
284 +     * The default rejected execution handler is AbortPolicy.
285 +     */
286 +    public void testDefaultRejectedExecutionHandler() {
287 +        final ThreadPoolExecutor p =
288 +            new ThreadPoolExecutor(1, 2,
289 +                                   LONG_DELAY_MS, MILLISECONDS,
290 +                                   new ArrayBlockingQueue<Runnable>(10));
291 +        try (PoolCleaner cleaner = cleaner(p)) {
292 +            assertTrue(p.getRejectedExecutionHandler() instanceof AbortPolicy);
293 +        }
294 +    }
295 +
296 +    /**
297       * getRejectedExecutionHandler returns handler in constructor if not set
298       */
299      public void testGetRejectedExecutionHandler() {
# Line 329 | Line 346 | public class ThreadPoolExecutorTest exte
346       */
347      public void testGetLargestPoolSize() throws InterruptedException {
348          final int THREADS = 3;
349 +        final CountDownLatch done = new CountDownLatch(1);
350          final ThreadPoolExecutor p =
351              new ThreadPoolExecutor(THREADS, THREADS,
352                                     LONG_DELAY_MS, MILLISECONDS,
353                                     new ArrayBlockingQueue<Runnable>(10));
354 <        try (PoolCleaner cleaner = cleaner(p)) {
337 <            final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
338 <            final CountDownLatch done = new CountDownLatch(1);
354 >        try (PoolCleaner cleaner = cleaner(p, done)) {
355              assertEquals(0, p.getLargestPoolSize());
356 +            final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
357              for (int i = 0; i < THREADS; i++)
358                  p.execute(new CheckedRunnable() {
359                      public void realRun() throws InterruptedException {
360                          threadsStarted.countDown();
361 <                        done.await();
361 >                        await(done);
362                          assertEquals(THREADS, p.getLargestPoolSize());
363                      }});
364 <            assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
364 >            await(threadsStarted);
365              assertEquals(THREADS, p.getLargestPoolSize());
349            done.countDown();   // release pool
366          }
367          assertEquals(THREADS, p.getLargestPoolSize());
368      }
# Line 374 | Line 390 | public class ThreadPoolExecutorTest exte
390       * become active
391       */
392      public void testGetPoolSize() throws InterruptedException {
393 +        final CountDownLatch done = new CountDownLatch(1);
394          final ThreadPoolExecutor p =
395              new ThreadPoolExecutor(1, 1,
396                                     LONG_DELAY_MS, MILLISECONDS,
397                                     new ArrayBlockingQueue<Runnable>(10));
398 <        try (PoolCleaner cleaner = cleaner(p)) {
382 <            final CountDownLatch threadStarted = new CountDownLatch(1);
383 <            final CountDownLatch done = new CountDownLatch(1);
398 >        try (PoolCleaner cleaner = cleaner(p, done)) {
399              assertEquals(0, p.getPoolSize());
400 +            final CountDownLatch threadStarted = new CountDownLatch(1);
401              p.execute(new CheckedRunnable() {
402                  public void realRun() throws InterruptedException {
403                      threadStarted.countDown();
404                      assertEquals(1, p.getPoolSize());
405 <                    done.await();
405 >                    await(done);
406                  }});
407 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
407 >            await(threadStarted);
408              assertEquals(1, p.getPoolSize());
393            done.countDown();   // release pool
409          }
410      }
411  
# Line 398 | Line 413 | public class ThreadPoolExecutorTest exte
413       * getTaskCount increases, but doesn't overestimate, when tasks submitted
414       */
415      public void testGetTaskCount() throws InterruptedException {
416 +        final int TASKS = 3;
417 +        final CountDownLatch done = new CountDownLatch(1);
418          final ThreadPoolExecutor p =
419              new ThreadPoolExecutor(1, 1,
420                                     LONG_DELAY_MS, MILLISECONDS,
421                                     new ArrayBlockingQueue<Runnable>(10));
422 <        try (PoolCleaner cleaner = cleaner(p)) {
422 >        try (PoolCleaner cleaner = cleaner(p, done)) {
423              final CountDownLatch threadStarted = new CountDownLatch(1);
407            final CountDownLatch done = new CountDownLatch(1);
424              assertEquals(0, p.getTaskCount());
425 +            assertEquals(0, p.getCompletedTaskCount());
426              p.execute(new CheckedRunnable() {
427                  public void realRun() throws InterruptedException {
428                      threadStarted.countDown();
429 <                    assertEquals(1, p.getTaskCount());
413 <                    done.await();
429 >                    await(done);
430                  }});
431 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
431 >            await(threadStarted);
432              assertEquals(1, p.getTaskCount());
433 <            done.countDown();
433 >            assertEquals(0, p.getCompletedTaskCount());
434 >            for (int i = 0; i < TASKS; i++) {
435 >                assertEquals(1 + i, p.getTaskCount());
436 >                p.execute(new CheckedRunnable() {
437 >                    public void realRun() throws InterruptedException {
438 >                        threadStarted.countDown();
439 >                        assertEquals(1 + TASKS, p.getTaskCount());
440 >                        await(done);
441 >                    }});
442 >            }
443 >            assertEquals(1 + TASKS, p.getTaskCount());
444 >            assertEquals(0, p.getCompletedTaskCount());
445          }
446 +        assertEquals(1 + TASKS, p.getTaskCount());
447 +        assertEquals(1 + TASKS, p.getCompletedTaskCount());
448      }
449  
450      /**
# Line 447 | Line 476 | public class ThreadPoolExecutorTest exte
476              assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
477              assertFalse(p.awaitTermination(-1L, NANOSECONDS));
478              assertFalse(p.awaitTermination(-1L, MILLISECONDS));
479 <            assertFalse(p.awaitTermination(0L, NANOSECONDS));
480 <            assertFalse(p.awaitTermination(0L, MILLISECONDS));
479 >            assertFalse(p.awaitTermination(randomExpiredTimeout(),
480 >                                           randomTimeUnit()));
481              long timeoutNanos = 999999L;
482              long startTime = System.nanoTime();
483              assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
# Line 481 | Line 510 | public class ThreadPoolExecutorTest exte
510                  public void realRun() throws InterruptedException {
511                      assertFalse(p.isTerminating());
512                      threadStarted.countDown();
513 <                    done.await();
513 >                    await(done);
514                  }});
515 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
515 >            await(threadStarted);
516              assertFalse(p.isTerminating());
517              done.countDown();
518              try { p.shutdown(); } catch (SecurityException ok) { return; }
# Line 509 | Line 538 | public class ThreadPoolExecutorTest exte
538                  public void realRun() throws InterruptedException {
539                      assertFalse(p.isTerminating());
540                      threadStarted.countDown();
541 <                    done.await();
541 >                    await(done);
542                  }});
543 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
543 >            await(threadStarted);
544              assertFalse(p.isTerminating());
545              done.countDown();
546              try { p.shutdown(); } catch (SecurityException ok) { return; }
# Line 525 | Line 554 | public class ThreadPoolExecutorTest exte
554       * getQueue returns the work queue, which contains queued tasks
555       */
556      public void testGetQueue() throws InterruptedException {
557 <        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
557 >        final CountDownLatch done = new CountDownLatch(1);
558 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
559          final ThreadPoolExecutor p =
560              new ThreadPoolExecutor(1, 1,
561                                     LONG_DELAY_MS, MILLISECONDS,
562                                     q);
563 <        try (PoolCleaner cleaner = cleaner(p)) {
563 >        try (PoolCleaner cleaner = cleaner(p, done)) {
564              final CountDownLatch threadStarted = new CountDownLatch(1);
535            final CountDownLatch done = new CountDownLatch(1);
565              FutureTask[] tasks = new FutureTask[5];
566              for (int i = 0; i < tasks.length; i++) {
567                  Callable task = new CheckedCallable<Boolean>() {
568                      public Boolean realCall() throws InterruptedException {
569                          threadStarted.countDown();
570                          assertSame(q, p.getQueue());
571 <                        done.await();
571 >                        await(done);
572                          return Boolean.TRUE;
573                      }};
574                  tasks[i] = new FutureTask(task);
575                  p.execute(tasks[i]);
576              }
577 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
577 >            await(threadStarted);
578              assertSame(q, p.getQueue());
579              assertFalse(q.contains(tasks[0]));
580              assertTrue(q.contains(tasks[tasks.length - 1]));
581              assertEquals(tasks.length - 1, q.size());
553            done.countDown();
582          }
583      }
584  
# Line 558 | Line 586 | public class ThreadPoolExecutorTest exte
586       * remove(task) removes queued task, and fails to remove active task
587       */
588      public void testRemove() throws InterruptedException {
589 <        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
589 >        final CountDownLatch done = new CountDownLatch(1);
590 >        BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
591          final ThreadPoolExecutor p =
592              new ThreadPoolExecutor(1, 1,
593                                     LONG_DELAY_MS, MILLISECONDS,
594                                     q);
595 <        try (PoolCleaner cleaner = cleaner(p)) {
595 >        try (PoolCleaner cleaner = cleaner(p, done)) {
596              Runnable[] tasks = new Runnable[6];
597              final CountDownLatch threadStarted = new CountDownLatch(1);
569            final CountDownLatch done = new CountDownLatch(1);
598              for (int i = 0; i < tasks.length; i++) {
599                  tasks[i] = new CheckedRunnable() {
600                      public void realRun() throws InterruptedException {
601                          threadStarted.countDown();
602 <                        done.await();
602 >                        await(done);
603                      }};
604                  p.execute(tasks[i]);
605              }
606 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
606 >            await(threadStarted);
607              assertFalse(p.remove(tasks[0]));
608              assertTrue(q.contains(tasks[4]));
609              assertTrue(q.contains(tasks[3]));
# Line 585 | Line 613 | public class ThreadPoolExecutorTest exte
613              assertTrue(q.contains(tasks[3]));
614              assertTrue(p.remove(tasks[3]));
615              assertFalse(q.contains(tasks[3]));
588            done.countDown();
616          }
617      }
618  
# Line 595 | Line 622 | public class ThreadPoolExecutorTest exte
622      public void testPurge() throws InterruptedException {
623          final CountDownLatch threadStarted = new CountDownLatch(1);
624          final CountDownLatch done = new CountDownLatch(1);
625 <        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
625 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
626          final ThreadPoolExecutor p =
627              new ThreadPoolExecutor(1, 1,
628                                     LONG_DELAY_MS, MILLISECONDS,
629                                     q);
630 <        try (PoolCleaner cleaner = cleaner(p)) {
630 >        try (PoolCleaner cleaner = cleaner(p, done)) {
631              FutureTask[] tasks = new FutureTask[5];
632              for (int i = 0; i < tasks.length; i++) {
633                  Callable task = new CheckedCallable<Boolean>() {
634                      public Boolean realCall() throws InterruptedException {
635                          threadStarted.countDown();
636 <                        done.await();
636 >                        await(done);
637                          return Boolean.TRUE;
638                      }};
639                  tasks[i] = new FutureTask(task);
640                  p.execute(tasks[i]);
641              }
642 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
642 >            await(threadStarted);
643              assertEquals(tasks.length, p.getTaskCount());
644              assertEquals(tasks.length - 1, q.size());
645              assertEquals(1L, p.getActiveCount());
# Line 625 | Line 652 | public class ThreadPoolExecutorTest exte
652              p.purge();         // Nothing to do
653              assertEquals(tasks.length - 3, q.size());
654              assertEquals(tasks.length - 2, p.getTaskCount());
628            done.countDown();
655          }
656      }
657  
# Line 641 | Line 667 | public class ThreadPoolExecutorTest exte
667              new ThreadPoolExecutor(poolSize, poolSize,
668                                     LONG_DELAY_MS, MILLISECONDS,
669                                     new ArrayBlockingQueue<Runnable>(10));
670 <        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
670 >        final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
671          Runnable waiter = new CheckedRunnable() { public void realRun() {
672              threadsStarted.countDown();
673              try {
# Line 651 | Line 677 | public class ThreadPoolExecutorTest exte
677          }};
678          for (int i = 0; i < count; i++)
679              p.execute(waiter);
680 <        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
680 >        await(threadsStarted);
681          assertEquals(poolSize, p.getActiveCount());
682          assertEquals(0, p.getCompletedTaskCount());
683          final List<Runnable> queuedTasks;
# Line 1013 | Line 1039 | public class ThreadPoolExecutorTest exte
1039       * get of submitted callable throws InterruptedException if interrupted
1040       */
1041      public void testInterruptedSubmit() throws InterruptedException {
1042 +        final CountDownLatch done = new CountDownLatch(1);
1043          final ThreadPoolExecutor p =
1044              new ThreadPoolExecutor(1, 1,
1045                                     60, SECONDS,
1046                                     new ArrayBlockingQueue<Runnable>(10));
1047  
1048 <        try (PoolCleaner cleaner = cleaner(p)) {
1048 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1049              final CountDownLatch threadStarted = new CountDownLatch(1);
1023            final CountDownLatch done = new CountDownLatch(1);
1050              Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1051                  public void realRun() throws Exception {
1052                      Callable task = new CheckedCallable<Boolean>() {
1053                          public Boolean realCall() throws InterruptedException {
1054                              threadStarted.countDown();
1055 <                            done.await();
1055 >                            await(done);
1056                              return Boolean.TRUE;
1057                          }};
1058                      p.submit(task).get();
1059                  }});
1060  
1061 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
1061 >            await(threadStarted);
1062              t.interrupt();
1063 <            awaitTermination(t, MEDIUM_DELAY_MS);
1038 <            done.countDown();
1063 >            awaitTermination(t);
1064          }
1065      }
1066  
# Line 1043 | Line 1068 | public class ThreadPoolExecutorTest exte
1068       * execute throws RejectedExecutionException if saturated.
1069       */
1070      public void testSaturatedExecute() {
1071 +        final CountDownLatch done = new CountDownLatch(1);
1072          final ThreadPoolExecutor p =
1073              new ThreadPoolExecutor(1, 1,
1074                                     LONG_DELAY_MS, MILLISECONDS,
1075                                     new ArrayBlockingQueue<Runnable>(1));
1076 <        try (PoolCleaner cleaner = cleaner(p)) {
1051 <            final CountDownLatch done = new CountDownLatch(1);
1076 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1077              Runnable task = new CheckedRunnable() {
1078                  public void realRun() throws InterruptedException {
1079 <                    done.await();
1079 >                    await(done);
1080                  }};
1081              for (int i = 0; i < 2; ++i)
1082                  p.execute(task);
# Line 1062 | Line 1087 | public class ThreadPoolExecutorTest exte
1087                  } catch (RejectedExecutionException success) {}
1088                  assertTrue(p.getTaskCount() <= 2);
1089              }
1065            done.countDown();
1090          }
1091      }
1092  
# Line 1070 | Line 1094 | public class ThreadPoolExecutorTest exte
1094       * submit(runnable) throws RejectedExecutionException if saturated.
1095       */
1096      public void testSaturatedSubmitRunnable() {
1097 +        final CountDownLatch done = new CountDownLatch(1);
1098          final ThreadPoolExecutor p =
1099              new ThreadPoolExecutor(1, 1,
1100                                     LONG_DELAY_MS, MILLISECONDS,
1101                                     new ArrayBlockingQueue<Runnable>(1));
1102 <        try (PoolCleaner cleaner = cleaner(p)) {
1078 <            final CountDownLatch done = new CountDownLatch(1);
1102 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1103              Runnable task = new CheckedRunnable() {
1104                  public void realRun() throws InterruptedException {
1105 <                    done.await();
1105 >                    await(done);
1106                  }};
1107              for (int i = 0; i < 2; ++i)
1108                  p.submit(task);
# Line 1089 | Line 1113 | public class ThreadPoolExecutorTest exte
1113                  } catch (RejectedExecutionException success) {}
1114                  assertTrue(p.getTaskCount() <= 2);
1115              }
1092            done.countDown();
1116          }
1117      }
1118  
# Line 1097 | Line 1120 | public class ThreadPoolExecutorTest exte
1120       * submit(callable) throws RejectedExecutionException if saturated.
1121       */
1122      public void testSaturatedSubmitCallable() {
1123 +        final CountDownLatch done = new CountDownLatch(1);
1124          final ThreadPoolExecutor p =
1125              new ThreadPoolExecutor(1, 1,
1126                                     LONG_DELAY_MS, MILLISECONDS,
1127                                     new ArrayBlockingQueue<Runnable>(1));
1128 <        try (PoolCleaner cleaner = cleaner(p)) {
1105 <            final CountDownLatch done = new CountDownLatch(1);
1128 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1129              Runnable task = new CheckedRunnable() {
1130                  public void realRun() throws InterruptedException {
1131 <                    done.await();
1131 >                    await(done);
1132                  }};
1133              for (int i = 0; i < 2; ++i)
1134 <                p.submit(Executors.callable(task));
1134 >                p.execute(task);
1135              for (int i = 0; i < 2; ++i) {
1136                  try {
1137                      p.execute(task);
# Line 1116 | Line 1139 | public class ThreadPoolExecutorTest exte
1139                  } catch (RejectedExecutionException success) {}
1140                  assertTrue(p.getTaskCount() <= 2);
1141              }
1119            done.countDown();
1142          }
1143      }
1144  
# Line 1129 | Line 1151 | public class ThreadPoolExecutorTest exte
1151                                     LONG_DELAY_MS,
1152                                     MILLISECONDS,
1153                                     new ArrayBlockingQueue<Runnable>(1),
1154 <                                   new ThreadPoolExecutor.CallerRunsPolicy());
1154 >                                   new CallerRunsPolicy());
1155          try (PoolCleaner cleaner = cleaner(p)) {
1156              final CountDownLatch done = new CountDownLatch(1);
1157              Runnable blocker = new CheckedRunnable() {
1158                  public void realRun() throws InterruptedException {
1159 <                    done.await();
1159 >                    await(done);
1160                  }};
1161              p.execute(blocker);
1162              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
# Line 1153 | Line 1175 | public class ThreadPoolExecutorTest exte
1175       * executor using DiscardPolicy drops task if saturated.
1176       */
1177      public void testSaturatedExecute3() {
1178 +        final CountDownLatch done = new CountDownLatch(1);
1179          final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1180          for (int i = 0; i < tasks.length; ++i)
1181              tasks[i] = new TrackedNoOpRunnable();
# Line 1160 | Line 1183 | public class ThreadPoolExecutorTest exte
1183              new ThreadPoolExecutor(1, 1,
1184                            LONG_DELAY_MS, MILLISECONDS,
1185                            new ArrayBlockingQueue<Runnable>(1),
1186 <                          new ThreadPoolExecutor.DiscardPolicy());
1187 <        try (PoolCleaner cleaner = cleaner(p)) {
1165 <            final CountDownLatch done = new CountDownLatch(1);
1186 >                          new DiscardPolicy());
1187 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1188              p.execute(awaiter(done));
1189  
1190              for (TrackedNoOpRunnable task : tasks)
1191                  p.execute(task);
1192              for (int i = 1; i < tasks.length; i++)
1193                  assertFalse(tasks[i].done);
1172            done.countDown();
1194          }
1195          for (int i = 1; i < tasks.length; i++)
1196              assertFalse(tasks[i].done);
# Line 1188 | Line 1209 | public class ThreadPoolExecutorTest exte
1209              new ThreadPoolExecutor(1, 1,
1210                                     LONG_DELAY_MS, MILLISECONDS,
1211                                     new ArrayBlockingQueue<Runnable>(1),
1212 <                                   new ThreadPoolExecutor.DiscardOldestPolicy());
1213 <        try (PoolCleaner cleaner = cleaner(p)) {
1212 >                                   new DiscardOldestPolicy());
1213 >        try (PoolCleaner cleaner = cleaner(p, done)) {
1214              assertEquals(LatchAwaiter.NEW, r1.state);
1215              assertEquals(LatchAwaiter.NEW, r2.state);
1216              assertEquals(LatchAwaiter.NEW, r3.state);
# Line 1199 | Line 1220 | public class ThreadPoolExecutorTest exte
1220              p.execute(r3);
1221              assertFalse(p.getQueue().contains(r2));
1222              assertTrue(p.getQueue().contains(r3));
1202            done.countDown();
1223          }
1224          assertEquals(LatchAwaiter.DONE, r1.state);
1225          assertEquals(LatchAwaiter.NEW, r2.state);
# Line 1227 | Line 1247 | public class ThreadPoolExecutorTest exte
1247       * execute using CallerRunsPolicy drops task on shutdown
1248       */
1249      public void testCallerRunsOnShutdown() {
1250 <        RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
1250 >        RejectedExecutionHandler h = new CallerRunsPolicy();
1251          final ThreadPoolExecutor p =
1252              new ThreadPoolExecutor(1, 1,
1253                                     LONG_DELAY_MS, MILLISECONDS,
# Line 1249 | Line 1269 | public class ThreadPoolExecutorTest exte
1269              new ThreadPoolExecutor(1, 1,
1270                                     LONG_DELAY_MS, MILLISECONDS,
1271                                     new ArrayBlockingQueue<Runnable>(1),
1272 <                                   new ThreadPoolExecutor.DiscardPolicy());
1272 >                                   new DiscardPolicy());
1273  
1274          try { p.shutdown(); } catch (SecurityException ok) { return; }
1275          try (PoolCleaner cleaner = cleaner(p)) {
# Line 1267 | Line 1287 | public class ThreadPoolExecutorTest exte
1287              new ThreadPoolExecutor(1, 1,
1288                                     LONG_DELAY_MS, MILLISECONDS,
1289                                     new ArrayBlockingQueue<Runnable>(1),
1290 <                                   new ThreadPoolExecutor.DiscardOldestPolicy());
1290 >                                   new DiscardOldestPolicy());
1291  
1292          try { p.shutdown(); } catch (SecurityException ok) { return; }
1293          try (PoolCleaner cleaner = cleaner(p)) {
# Line 1481 | Line 1501 | public class ThreadPoolExecutorTest exte
1501      }
1502  
1503      /**
1504 <     * invokeAny(empty collection) throws IAE
1504 >     * invokeAny(empty collection) throws IllegalArgumentException
1505       */
1506      public void testInvokeAny2() throws Exception {
1507          final ExecutorService e =
# Line 1506 | Line 1526 | public class ThreadPoolExecutorTest exte
1526                                     LONG_DELAY_MS, MILLISECONDS,
1527                                     new ArrayBlockingQueue<Runnable>(10));
1528          try (PoolCleaner cleaner = cleaner(e)) {
1529 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1529 >            List<Callable<String>> l = new ArrayList<>();
1530              l.add(latchAwaitingStringTask(latch));
1531              l.add(null);
1532              try {
# Line 1526 | Line 1546 | public class ThreadPoolExecutorTest exte
1546                                     LONG_DELAY_MS, MILLISECONDS,
1547                                     new ArrayBlockingQueue<Runnable>(10));
1548          try (PoolCleaner cleaner = cleaner(e)) {
1549 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1549 >            List<Callable<String>> l = new ArrayList<>();
1550              l.add(new NPETask());
1551              try {
1552                  e.invokeAny(l);
# Line 1546 | Line 1566 | public class ThreadPoolExecutorTest exte
1566                                     LONG_DELAY_MS, MILLISECONDS,
1567                                     new ArrayBlockingQueue<Runnable>(10));
1568          try (PoolCleaner cleaner = cleaner(e)) {
1569 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1569 >            List<Callable<String>> l = new ArrayList<>();
1570              l.add(new StringTask());
1571              l.add(new StringTask());
1572              String result = e.invokeAny(l);
# Line 1571 | Line 1591 | public class ThreadPoolExecutorTest exte
1591      }
1592  
1593      /**
1594 <     * invokeAll(empty collection) returns empty collection
1594 >     * invokeAll(empty collection) returns empty list
1595       */
1596      public void testInvokeAll2() throws InterruptedException {
1597          final ExecutorService e =
1598              new ThreadPoolExecutor(2, 2,
1599                                     LONG_DELAY_MS, MILLISECONDS,
1600                                     new ArrayBlockingQueue<Runnable>(10));
1601 +        final Collection<Callable<String>> emptyCollection
1602 +            = Collections.emptyList();
1603          try (PoolCleaner cleaner = cleaner(e)) {
1604 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1604 >            List<Future<String>> r = e.invokeAll(emptyCollection);
1605              assertTrue(r.isEmpty());
1606          }
1607      }
# Line 1593 | Line 1615 | public class ThreadPoolExecutorTest exte
1615                                     LONG_DELAY_MS, MILLISECONDS,
1616                                     new ArrayBlockingQueue<Runnable>(10));
1617          try (PoolCleaner cleaner = cleaner(e)) {
1618 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1618 >            List<Callable<String>> l = new ArrayList<>();
1619              l.add(new StringTask());
1620              l.add(null);
1621              try {
# Line 1612 | Line 1634 | public class ThreadPoolExecutorTest exte
1634                                     LONG_DELAY_MS, MILLISECONDS,
1635                                     new ArrayBlockingQueue<Runnable>(10));
1636          try (PoolCleaner cleaner = cleaner(e)) {
1637 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1637 >            List<Callable<String>> l = new ArrayList<>();
1638              l.add(new NPETask());
1639              List<Future<String>> futures = e.invokeAll(l);
1640              assertEquals(1, futures.size());
# Line 1634 | Line 1656 | public class ThreadPoolExecutorTest exte
1656                                     LONG_DELAY_MS, MILLISECONDS,
1657                                     new ArrayBlockingQueue<Runnable>(10));
1658          try (PoolCleaner cleaner = cleaner(e)) {
1659 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1659 >            List<Callable<String>> l = new ArrayList<>();
1660              l.add(new StringTask());
1661              l.add(new StringTask());
1662              List<Future<String>> futures = e.invokeAll(l);
# Line 1654 | Line 1676 | public class ThreadPoolExecutorTest exte
1676                                     new ArrayBlockingQueue<Runnable>(10));
1677          try (PoolCleaner cleaner = cleaner(e)) {
1678              try {
1679 <                e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1679 >                e.invokeAny(null, randomTimeout(), randomTimeUnit());
1680                  shouldThrow();
1681              } catch (NullPointerException success) {}
1682          }
# Line 1669 | Line 1691 | public class ThreadPoolExecutorTest exte
1691                                     LONG_DELAY_MS, MILLISECONDS,
1692                                     new ArrayBlockingQueue<Runnable>(10));
1693          try (PoolCleaner cleaner = cleaner(e)) {
1694 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1694 >            List<Callable<String>> l = new ArrayList<>();
1695              l.add(new StringTask());
1696              try {
1697 <                e.invokeAny(l, MEDIUM_DELAY_MS, null);
1697 >                e.invokeAny(l, randomTimeout(), null);
1698                  shouldThrow();
1699              } catch (NullPointerException success) {}
1700          }
1701      }
1702  
1703      /**
1704 <     * timed invokeAny(empty collection) throws IAE
1704 >     * timed invokeAny(empty collection) throws IllegalArgumentException
1705       */
1706      public void testTimedInvokeAny2() throws Exception {
1707          final ExecutorService e =
# Line 1689 | Line 1711 | public class ThreadPoolExecutorTest exte
1711          try (PoolCleaner cleaner = cleaner(e)) {
1712              try {
1713                  e.invokeAny(new ArrayList<Callable<String>>(),
1714 <                            MEDIUM_DELAY_MS, MILLISECONDS);
1714 >                            randomTimeout(), randomTimeUnit());
1715                  shouldThrow();
1716              } catch (IllegalArgumentException success) {}
1717          }
1718      }
1719  
1720      /**
1721 <     * timed invokeAny(c) throws NPE if c has null elements
1721 >     * timed invokeAny(c) throws NullPointerException if c has null elements
1722       */
1723      public void testTimedInvokeAny3() throws Exception {
1724          final CountDownLatch latch = new CountDownLatch(1);
# Line 1705 | Line 1727 | public class ThreadPoolExecutorTest exte
1727                                     LONG_DELAY_MS, MILLISECONDS,
1728                                     new ArrayBlockingQueue<Runnable>(10));
1729          try (PoolCleaner cleaner = cleaner(e)) {
1730 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1730 >            List<Callable<String>> l = new ArrayList<>();
1731              l.add(latchAwaitingStringTask(latch));
1732              l.add(null);
1733              try {
1734 <                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1734 >                e.invokeAny(l, randomTimeout(), randomTimeUnit());
1735                  shouldThrow();
1736              } catch (NullPointerException success) {}
1737              latch.countDown();
# Line 1725 | Line 1747 | public class ThreadPoolExecutorTest exte
1747                                     LONG_DELAY_MS, MILLISECONDS,
1748                                     new ArrayBlockingQueue<Runnable>(10));
1749          try (PoolCleaner cleaner = cleaner(e)) {
1750 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1750 >            long startTime = System.nanoTime();
1751 >            List<Callable<String>> l = new ArrayList<>();
1752              l.add(new NPETask());
1753              try {
1754 <                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1754 >                e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1755                  shouldThrow();
1756              } catch (ExecutionException success) {
1757                  assertTrue(success.getCause() instanceof NullPointerException);
1758              }
1759 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1760          }
1761      }
1762  
# Line 1745 | Line 1769 | public class ThreadPoolExecutorTest exte
1769                                     LONG_DELAY_MS, MILLISECONDS,
1770                                     new ArrayBlockingQueue<Runnable>(10));
1771          try (PoolCleaner cleaner = cleaner(e)) {
1772 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1772 >            long startTime = System.nanoTime();
1773 >            List<Callable<String>> l = new ArrayList<>();
1774              l.add(new StringTask());
1775              l.add(new StringTask());
1776 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1776 >            String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1777              assertSame(TEST_STRING, result);
1778 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1779          }
1780      }
1781  
# Line 1763 | Line 1789 | public class ThreadPoolExecutorTest exte
1789                                     new ArrayBlockingQueue<Runnable>(10));
1790          try (PoolCleaner cleaner = cleaner(e)) {
1791              try {
1792 <                e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1792 >                e.invokeAll(null, randomTimeout(), randomTimeUnit());
1793                  shouldThrow();
1794              } catch (NullPointerException success) {}
1795          }
# Line 1778 | Line 1804 | public class ThreadPoolExecutorTest exte
1804                                     LONG_DELAY_MS, MILLISECONDS,
1805                                     new ArrayBlockingQueue<Runnable>(10));
1806          try (PoolCleaner cleaner = cleaner(e)) {
1807 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1807 >            List<Callable<String>> l = new ArrayList<>();
1808              l.add(new StringTask());
1809              try {
1810 <                e.invokeAll(l, MEDIUM_DELAY_MS, null);
1810 >                e.invokeAll(l, randomTimeout(), null);
1811                  shouldThrow();
1812              } catch (NullPointerException success) {}
1813          }
1814      }
1815  
1816      /**
1817 <     * timed invokeAll(empty collection) returns empty collection
1817 >     * timed invokeAll(empty collection) returns empty list
1818       */
1819      public void testTimedInvokeAll2() throws InterruptedException {
1820          final ExecutorService e =
1821              new ThreadPoolExecutor(2, 2,
1822                                     LONG_DELAY_MS, MILLISECONDS,
1823                                     new ArrayBlockingQueue<Runnable>(10));
1824 +        final Collection<Callable<String>> emptyCollection
1825 +            = Collections.emptyList();
1826          try (PoolCleaner cleaner = cleaner(e)) {
1827 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(),
1828 <                                                 MEDIUM_DELAY_MS, MILLISECONDS);
1827 >            List<Future<String>> r =
1828 >                e.invokeAll(emptyCollection, randomTimeout(), randomTimeUnit());
1829              assertTrue(r.isEmpty());
1830          }
1831      }
# Line 1811 | Line 1839 | public class ThreadPoolExecutorTest exte
1839                                     LONG_DELAY_MS, MILLISECONDS,
1840                                     new ArrayBlockingQueue<Runnable>(10));
1841          try (PoolCleaner cleaner = cleaner(e)) {
1842 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1842 >            List<Callable<String>> l = new ArrayList<>();
1843              l.add(new StringTask());
1844              l.add(null);
1845              try {
1846 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1846 >                e.invokeAll(l, randomTimeout(), randomTimeUnit());
1847                  shouldThrow();
1848              } catch (NullPointerException success) {}
1849          }
# Line 1830 | Line 1858 | public class ThreadPoolExecutorTest exte
1858                                     LONG_DELAY_MS, MILLISECONDS,
1859                                     new ArrayBlockingQueue<Runnable>(10));
1860          try (PoolCleaner cleaner = cleaner(e)) {
1861 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1861 >            List<Callable<String>> l = new ArrayList<>();
1862              l.add(new NPETask());
1863              List<Future<String>> futures =
1864 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1864 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1865              assertEquals(1, futures.size());
1866              try {
1867                  futures.get(0).get();
# Line 1853 | Line 1881 | public class ThreadPoolExecutorTest exte
1881                                     LONG_DELAY_MS, MILLISECONDS,
1882                                     new ArrayBlockingQueue<Runnable>(10));
1883          try (PoolCleaner cleaner = cleaner(e)) {
1884 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1884 >            List<Callable<String>> l = new ArrayList<>();
1885              l.add(new StringTask());
1886              l.add(new StringTask());
1887              List<Future<String>> futures =
# Line 1868 | Line 1896 | public class ThreadPoolExecutorTest exte
1896       * timed invokeAll(c) cancels tasks not completed by timeout
1897       */
1898      public void testTimedInvokeAll6() throws Exception {
1899 <        final ExecutorService e =
1900 <            new ThreadPoolExecutor(2, 2,
1901 <                                   LONG_DELAY_MS, MILLISECONDS,
1902 <                                   new ArrayBlockingQueue<Runnable>(10));
1903 <        try (PoolCleaner cleaner = cleaner(e)) {
1904 <            for (long timeout = timeoutMillis();;) {
1899 >        for (long timeout = timeoutMillis();;) {
1900 >            final CountDownLatch done = new CountDownLatch(1);
1901 >            final Callable<String> waiter = new CheckedCallable<String>() {
1902 >                public String realCall() {
1903 >                    try { done.await(LONG_DELAY_MS, MILLISECONDS); }
1904 >                    catch (InterruptedException ok) {}
1905 >                    return "1"; }};
1906 >            final ExecutorService p =
1907 >                new ThreadPoolExecutor(2, 2,
1908 >                                       LONG_DELAY_MS, MILLISECONDS,
1909 >                                       new ArrayBlockingQueue<Runnable>(10));
1910 >            try (PoolCleaner cleaner = cleaner(p, done)) {
1911                  List<Callable<String>> tasks = new ArrayList<>();
1912                  tasks.add(new StringTask("0"));
1913 <                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1913 >                tasks.add(waiter);
1914                  tasks.add(new StringTask("2"));
1915                  long startTime = System.nanoTime();
1916                  List<Future<String>> futures =
1917 <                    e.invokeAll(tasks, timeout, MILLISECONDS);
1917 >                    p.invokeAll(tasks, timeout, MILLISECONDS);
1918                  assertEquals(tasks.size(), futures.size());
1919                  assertTrue(millisElapsedSince(startTime) >= timeout);
1920                  for (Future future : futures)
# Line 1917 | Line 1951 | public class ThreadPoolExecutorTest exte
1951                      public void realRun() {
1952                          done.countDown();
1953                      }});
1954 <            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1954 >            await(done);
1955          }
1956      }
1957  
# Line 2010 | Line 2044 | public class ThreadPoolExecutorTest exte
2044                  }
2045              }
2046              // enough time to run all tasks
2047 <            assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS));
2047 >            await(done, nTasks * SHORT_DELAY_MS);
2048          }
2049      }
2050  
# Line 2018 | Line 2052 | public class ThreadPoolExecutorTest exte
2052       * get(cancelled task) throws CancellationException
2053       */
2054      public void testGet_cancelled() throws Exception {
2055 +        final CountDownLatch done = new CountDownLatch(1);
2056          final ExecutorService e =
2057              new ThreadPoolExecutor(1, 1,
2058                                     LONG_DELAY_MS, MILLISECONDS,
2059                                     new LinkedBlockingQueue<Runnable>());
2060 <        try (PoolCleaner cleaner = cleaner(e)) {
2060 >        try (PoolCleaner cleaner = cleaner(e, done)) {
2061              final CountDownLatch blockerStarted = new CountDownLatch(1);
2027            final CountDownLatch done = new CountDownLatch(1);
2062              final List<Future<?>> futures = new ArrayList<>();
2063              for (int i = 0; i < 2; i++) {
2064                  Runnable r = new CheckedRunnable() { public void realRun()
# Line 2034 | Line 2068 | public class ThreadPoolExecutorTest exte
2068                  }};
2069                  futures.add(e.submit(r));
2070              }
2071 <            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
2071 >            await(blockerStarted);
2072              for (Future<?> future : futures) future.cancel(false);
2073              for (Future<?> future : futures) {
2074                  try {
# Line 2048 | Line 2082 | public class ThreadPoolExecutorTest exte
2082                  assertTrue(future.isCancelled());
2083                  assertTrue(future.isDone());
2084              }
2051            done.countDown();
2085          }
2086      }
2087  
2088 +    /** Directly test simple ThreadPoolExecutor RejectedExecutionHandlers. */
2089 +    public void testStandardRejectedExecutionHandlers() {
2090 +        final ThreadPoolExecutor p =
2091 +            new ThreadPoolExecutor(1, 1, 1, SECONDS,
2092 +                                   new ArrayBlockingQueue<Runnable>(1));
2093 +        final AtomicReference<Thread> thread = new AtomicReference<>();
2094 +        final Runnable r = new Runnable() { public void run() {
2095 +            thread.set(Thread.currentThread()); }};
2096 +
2097 +        try {
2098 +            new AbortPolicy().rejectedExecution(r, p);
2099 +            shouldThrow();
2100 +        } catch (RejectedExecutionException success) {}
2101 +        assertNull(thread.get());
2102 +
2103 +        new DiscardPolicy().rejectedExecution(r, p);
2104 +        assertNull(thread.get());
2105 +
2106 +        new CallerRunsPolicy().rejectedExecution(r, p);
2107 +        assertSame(Thread.currentThread(), thread.get());
2108 +
2109 +        // check that pool was not perturbed by handlers
2110 +        assertTrue(p.getRejectedExecutionHandler() instanceof AbortPolicy);
2111 +        assertEquals(0, p.getTaskCount());
2112 +        assertTrue(p.getQueue().isEmpty());
2113 +    }
2114 +
2115   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines