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

Comparing jsr166/src/test/tck/ScheduledExecutorSubclassTest.java (file contents):
Revision 1.38 by jsr166, Sun Sep 27 20:17:39 2015 UTC vs.
Revision 1.48 by jsr166, Mon Oct 5 21:42:48 2015 UTC

# Line 99 | Line 99 | public class ScheduledExecutorSubclassTe
99       * execute successfully executes a runnable
100       */
101      public void testExecute() throws InterruptedException {
102 <        CustomExecutor p = new CustomExecutor(1);
103 <        final CountDownLatch done = new CountDownLatch(1);
104 <        final Runnable task = new CheckedRunnable() {
105 <            public void realRun() {
106 <                done.countDown();
107 <            }};
108 <        try {
102 >        final CustomExecutor p = new CustomExecutor(1);
103 >        try (PoolCleaner cleaner = cleaner(p)) {
104 >            final CountDownLatch done = new CountDownLatch(1);
105 >            final Runnable task = new CheckedRunnable() {
106 >                public void realRun() { done.countDown(); }};
107              p.execute(task);
108 <            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
111 <        } finally {
112 <            joinPool(p);
108 >            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
109          }
110      }
111  
# Line 117 | Line 113 | public class ScheduledExecutorSubclassTe
113       * delayed schedule of callable successfully executes after delay
114       */
115      public void testSchedule1() throws Exception {
116 <        CustomExecutor p = new CustomExecutor(1);
117 <        final long startTime = System.nanoTime();
118 <        final CountDownLatch done = new CountDownLatch(1);
119 <        try {
116 >        final CustomExecutor p = new CustomExecutor(1);
117 >        try (PoolCleaner cleaner = cleaner(p)) {
118 >            final long startTime = System.nanoTime();
119 >            final CountDownLatch done = new CountDownLatch(1);
120              Callable task = new CheckedCallable<Boolean>() {
121                  public Boolean realCall() {
122                      done.countDown();
# Line 131 | Line 127 | public class ScheduledExecutorSubclassTe
127              assertSame(Boolean.TRUE, f.get());
128              assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
129              assertTrue(done.await(0L, MILLISECONDS));
134        } finally {
135            joinPool(p);
130          }
131      }
132  
# Line 140 | Line 134 | public class ScheduledExecutorSubclassTe
134       * delayed schedule of runnable successfully executes after delay
135       */
136      public void testSchedule3() throws Exception {
137 <        CustomExecutor p = new CustomExecutor(1);
138 <        final long startTime = System.nanoTime();
139 <        final CountDownLatch done = new CountDownLatch(1);
140 <        try {
137 >        final CustomExecutor p = new CustomExecutor(1);
138 >        try (PoolCleaner cleaner = cleaner(p)) {
139 >            final long startTime = System.nanoTime();
140 >            final CountDownLatch done = new CountDownLatch(1);
141              Runnable task = new CheckedRunnable() {
142                  public void realRun() {
143                      done.countDown();
# Line 153 | Line 147 | public class ScheduledExecutorSubclassTe
147              await(done);
148              assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
149              assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
156        } finally {
157            joinPool(p);
150          }
151      }
152  
# Line 162 | Line 154 | public class ScheduledExecutorSubclassTe
154       * scheduleAtFixedRate executes runnable after given initial delay
155       */
156      public void testSchedule4() throws InterruptedException {
157 <        CustomExecutor p = new CustomExecutor(1);
158 <        final long startTime = System.nanoTime();
159 <        final CountDownLatch done = new CountDownLatch(1);
160 <        try {
157 >        final CustomExecutor p = new CustomExecutor(1);
158 >        try (PoolCleaner cleaner = cleaner(p)) {
159 >            final long startTime = System.nanoTime();
160 >            final CountDownLatch done = new CountDownLatch(1);
161              Runnable task = new CheckedRunnable() {
162                  public void realRun() {
163                      done.countDown();
# Line 177 | Line 169 | public class ScheduledExecutorSubclassTe
169              await(done);
170              assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
171              f.cancel(true);
180        } finally {
181            joinPool(p);
172          }
173      }
174  
# Line 186 | Line 176 | public class ScheduledExecutorSubclassTe
176       * scheduleWithFixedDelay executes runnable after given initial delay
177       */
178      public void testSchedule5() throws InterruptedException {
179 <        CustomExecutor p = new CustomExecutor(1);
180 <        final long startTime = System.nanoTime();
181 <        final CountDownLatch done = new CountDownLatch(1);
182 <        try {
179 >        final CustomExecutor p = new CustomExecutor(1);
180 >        try (PoolCleaner cleaner = cleaner(p)) {
181 >            final long startTime = System.nanoTime();
182 >            final CountDownLatch done = new CountDownLatch(1);
183              Runnable task = new CheckedRunnable() {
184                  public void realRun() {
185                      done.countDown();
# Line 201 | Line 191 | public class ScheduledExecutorSubclassTe
191              await(done);
192              assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
193              f.cancel(true);
204        } finally {
205            joinPool(p);
194          }
195      }
196  
# Line 215 | Line 203 | public class ScheduledExecutorSubclassTe
203       * scheduleAtFixedRate executes series of tasks at given rate
204       */
205      public void testFixedRateSequence() throws InterruptedException {
206 <        CustomExecutor p = new CustomExecutor(1);
207 <        try {
206 >        final CustomExecutor p = new CustomExecutor(1);
207 >        try (PoolCleaner cleaner = cleaner(p)) {
208              for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
209                  long startTime = System.nanoTime();
210                  int cycles = 10;
# Line 234 | Line 222 | public class ScheduledExecutorSubclassTe
222                      return;
223              }
224              throw new AssertionError("unexpected execution rate");
237        } finally {
238            joinPool(p);
225          }
226      }
227  
# Line 243 | Line 229 | public class ScheduledExecutorSubclassTe
229       * scheduleWithFixedDelay executes series of tasks with given period
230       */
231      public void testFixedDelaySequence() throws InterruptedException {
232 <        CustomExecutor p = new CustomExecutor(1);
233 <        try {
232 >        final CustomExecutor p = new CustomExecutor(1);
233 >        try (PoolCleaner cleaner = cleaner(p)) {
234              for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
235                  long startTime = System.nanoTime();
236                  int cycles = 10;
# Line 262 | Line 248 | public class ScheduledExecutorSubclassTe
248                      return;
249              }
250              throw new AssertionError("unexpected execution rate");
265        } finally {
266            joinPool(p);
251          }
252      }
253  
# Line 271 | Line 255 | public class ScheduledExecutorSubclassTe
255       * execute(null) throws NPE
256       */
257      public void testExecuteNull() throws InterruptedException {
258 <        CustomExecutor se = new CustomExecutor(1);
259 <        try {
260 <            se.execute(null);
261 <            shouldThrow();
262 <        } catch (NullPointerException success) {}
263 <        joinPool(se);
258 >        final CustomExecutor p = new CustomExecutor(1);
259 >        try (PoolCleaner cleaner = cleaner(p)) {
260 >            try {
261 >                p.execute(null);
262 >                shouldThrow();
263 >            } catch (NullPointerException success) {}
264 >        }
265      }
266  
267      /**
268       * schedule(null) throws NPE
269       */
270      public void testScheduleNull() throws InterruptedException {
271 <        CustomExecutor se = new CustomExecutor(1);
272 <        try {
273 <            TrackedCallable callable = null;
274 <            Future f = se.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
275 <            shouldThrow();
276 <        } catch (NullPointerException success) {}
277 <        joinPool(se);
271 >        final CustomExecutor p = new CustomExecutor(1);
272 >        try (PoolCleaner cleaner = cleaner(p)) {
273 >            try {
274 >                TrackedCallable callable = null;
275 >                Future f = p.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
276 >                shouldThrow();
277 >            } catch (NullPointerException success) {}
278 >        }
279      }
280  
281      /**
282       * execute throws RejectedExecutionException if shutdown
283       */
284      public void testSchedule1_RejectedExecutionException() {
285 <        CustomExecutor se = new CustomExecutor(1);
286 <        try {
287 <            se.shutdown();
288 <            se.schedule(new NoOpRunnable(),
289 <                        MEDIUM_DELAY_MS, MILLISECONDS);
290 <            shouldThrow();
291 <        } catch (RejectedExecutionException success) {
292 <        } catch (SecurityException ok) {
285 >        final CustomExecutor p = new CustomExecutor(1);
286 >        try (PoolCleaner cleaner = cleaner(p)) {
287 >            try {
288 >                p.shutdown();
289 >                p.schedule(new NoOpRunnable(),
290 >                           MEDIUM_DELAY_MS, MILLISECONDS);
291 >                shouldThrow();
292 >            } catch (RejectedExecutionException success) {
293 >            } catch (SecurityException ok) {}
294          }
308
309        joinPool(se);
295      }
296  
297      /**
298       * schedule throws RejectedExecutionException if shutdown
299       */
300      public void testSchedule2_RejectedExecutionException() {
301 <        CustomExecutor se = new CustomExecutor(1);
302 <        try {
303 <            se.shutdown();
304 <            se.schedule(new NoOpCallable(),
305 <                        MEDIUM_DELAY_MS, MILLISECONDS);
306 <            shouldThrow();
307 <        } catch (RejectedExecutionException success) {
308 <        } catch (SecurityException ok) {
301 >        final CustomExecutor p = new CustomExecutor(1);
302 >        try (PoolCleaner cleaner = cleaner(p)) {
303 >            try {
304 >                p.shutdown();
305 >                p.schedule(new NoOpCallable(),
306 >                           MEDIUM_DELAY_MS, MILLISECONDS);
307 >                shouldThrow();
308 >            } catch (RejectedExecutionException success) {
309 >            } catch (SecurityException ok) {}
310          }
325        joinPool(se);
311      }
312  
313      /**
314       * schedule callable throws RejectedExecutionException if shutdown
315       */
316      public void testSchedule3_RejectedExecutionException() {
317 <        CustomExecutor se = new CustomExecutor(1);
318 <        try {
319 <            se.shutdown();
320 <            se.schedule(new NoOpCallable(),
321 <                        MEDIUM_DELAY_MS, MILLISECONDS);
322 <            shouldThrow();
323 <        } catch (RejectedExecutionException success) {
324 <        } catch (SecurityException ok) {
317 >        final CustomExecutor p = new CustomExecutor(1);
318 >        try (PoolCleaner cleaner = cleaner(p)) {
319 >            try {
320 >                p.shutdown();
321 >                p.schedule(new NoOpCallable(),
322 >                           MEDIUM_DELAY_MS, MILLISECONDS);
323 >                shouldThrow();
324 >            } catch (RejectedExecutionException success) {
325 >            } catch (SecurityException ok) {}
326          }
341        joinPool(se);
327      }
328  
329      /**
330       * scheduleAtFixedRate throws RejectedExecutionException if shutdown
331       */
332      public void testScheduleAtFixedRate1_RejectedExecutionException() {
333 <        CustomExecutor se = new CustomExecutor(1);
334 <        try {
335 <            se.shutdown();
336 <            se.scheduleAtFixedRate(new NoOpRunnable(),
337 <                                   MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
338 <            shouldThrow();
339 <        } catch (RejectedExecutionException success) {
340 <        } catch (SecurityException ok) {
333 >        final CustomExecutor p = new CustomExecutor(1);
334 >        try (PoolCleaner cleaner = cleaner(p)) {
335 >            try {
336 >                p.shutdown();
337 >                p.scheduleAtFixedRate(new NoOpRunnable(),
338 >                                      MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
339 >                shouldThrow();
340 >            } catch (RejectedExecutionException success) {
341 >            } catch (SecurityException ok) {}
342          }
357        joinPool(se);
343      }
344  
345      /**
346       * scheduleWithFixedDelay throws RejectedExecutionException if shutdown
347       */
348      public void testScheduleWithFixedDelay1_RejectedExecutionException() {
349 <        CustomExecutor se = new CustomExecutor(1);
350 <        try {
351 <            se.shutdown();
352 <            se.scheduleWithFixedDelay(new NoOpRunnable(),
353 <                                      MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
354 <            shouldThrow();
355 <        } catch (RejectedExecutionException success) {
356 <        } catch (SecurityException ok) {
349 >        final CustomExecutor p = new CustomExecutor(1);
350 >        try (PoolCleaner cleaner = cleaner(p)) {
351 >            try {
352 >                p.shutdown();
353 >                p.scheduleWithFixedDelay(new NoOpRunnable(),
354 >                                         MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
355 >                shouldThrow();
356 >            } catch (RejectedExecutionException success) {
357 >            } catch (SecurityException ok) {}
358          }
373        joinPool(se);
359      }
360  
361      /**
# Line 379 | Line 364 | public class ScheduledExecutorSubclassTe
364       */
365      public void testGetActiveCount() throws InterruptedException {
366          final ThreadPoolExecutor p = new CustomExecutor(2);
367 <        final CountDownLatch threadStarted = new CountDownLatch(1);
368 <        final CountDownLatch done = new CountDownLatch(1);
369 <        try {
367 >        try (PoolCleaner cleaner = cleaner(p)) {
368 >            final CountDownLatch threadStarted = new CountDownLatch(1);
369 >            final CountDownLatch done = new CountDownLatch(1);
370              assertEquals(0, p.getActiveCount());
371              p.execute(new CheckedRunnable() {
372                  public void realRun() throws InterruptedException {
# Line 389 | Line 374 | public class ScheduledExecutorSubclassTe
374                      assertEquals(1, p.getActiveCount());
375                      done.await();
376                  }});
377 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
377 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
378              assertEquals(1, p.getActiveCount());
394        } finally {
379              done.countDown();
396            joinPool(p);
380          }
381      }
382  
# Line 403 | Line 386 | public class ScheduledExecutorSubclassTe
386       */
387      public void testGetCompletedTaskCount() throws InterruptedException {
388          final ThreadPoolExecutor p = new CustomExecutor(2);
389 <        final CountDownLatch threadStarted = new CountDownLatch(1);
390 <        final CountDownLatch threadProceed = new CountDownLatch(1);
391 <        final CountDownLatch threadDone = new CountDownLatch(1);
392 <        try {
389 >        try (PoolCleaner cleaner = cleaner(p)) {
390 >            final CountDownLatch threadStarted = new CountDownLatch(1);
391 >            final CountDownLatch threadProceed = new CountDownLatch(1);
392 >            final CountDownLatch threadDone = new CountDownLatch(1);
393              assertEquals(0, p.getCompletedTaskCount());
394              p.execute(new CheckedRunnable() {
395                  public void realRun() throws InterruptedException {
# Line 425 | Line 408 | public class ScheduledExecutorSubclassTe
408                      fail("timed out");
409                  Thread.yield();
410              }
428        } finally {
429            joinPool(p);
411          }
412      }
413  
# Line 434 | Line 415 | public class ScheduledExecutorSubclassTe
415       * getCorePoolSize returns size given in constructor if not otherwise set
416       */
417      public void testGetCorePoolSize() {
418 <        CustomExecutor p = new CustomExecutor(1);
419 <        assertEquals(1, p.getCorePoolSize());
420 <        joinPool(p);
418 >        final CustomExecutor p = new CustomExecutor(1);
419 >        try (PoolCleaner cleaner = cleaner(p)) {
420 >            assertEquals(1, p.getCorePoolSize());
421 >        }
422      }
423  
424      /**
# Line 446 | Line 428 | public class ScheduledExecutorSubclassTe
428      public void testGetLargestPoolSize() throws InterruptedException {
429          final int THREADS = 3;
430          final ThreadPoolExecutor p = new CustomExecutor(THREADS);
431 <        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
432 <        final CountDownLatch done = new CountDownLatch(1);
433 <        try {
431 >        try (PoolCleaner cleaner = cleaner(p)) {
432 >            final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
433 >            final CountDownLatch done = new CountDownLatch(1);
434              assertEquals(0, p.getLargestPoolSize());
435              for (int i = 0; i < THREADS; i++)
436                  p.execute(new CheckedRunnable() {
# Line 457 | Line 439 | public class ScheduledExecutorSubclassTe
439                          done.await();
440                          assertEquals(THREADS, p.getLargestPoolSize());
441                      }});
442 <            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
442 >            assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
443              assertEquals(THREADS, p.getLargestPoolSize());
462        } finally {
444              done.countDown();
464            joinPool(p);
465            assertEquals(THREADS, p.getLargestPoolSize());
445          }
446 +        assertEquals(THREADS, p.getLargestPoolSize());
447      }
448  
449      /**
# Line 472 | Line 452 | public class ScheduledExecutorSubclassTe
452       */
453      public void testGetPoolSize() throws InterruptedException {
454          final ThreadPoolExecutor p = new CustomExecutor(1);
455 <        final CountDownLatch threadStarted = new CountDownLatch(1);
456 <        final CountDownLatch done = new CountDownLatch(1);
457 <        try {
455 >        try (PoolCleaner cleaner = cleaner(p)) {
456 >            final CountDownLatch threadStarted = new CountDownLatch(1);
457 >            final CountDownLatch done = new CountDownLatch(1);
458              assertEquals(0, p.getPoolSize());
459              p.execute(new CheckedRunnable() {
460                  public void realRun() throws InterruptedException {
# Line 482 | Line 462 | public class ScheduledExecutorSubclassTe
462                      assertEquals(1, p.getPoolSize());
463                      done.await();
464                  }});
465 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
465 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
466              assertEquals(1, p.getPoolSize());
487        } finally {
467              done.countDown();
489            joinPool(p);
468          }
469      }
470  
# Line 495 | Line 473 | public class ScheduledExecutorSubclassTe
473       * submitted
474       */
475      public void testGetTaskCount() throws InterruptedException {
476 <        final ThreadPoolExecutor p = new CustomExecutor(1);
499 <        final CountDownLatch threadStarted = new CountDownLatch(1);
476 >        final int TASKS = 3;
477          final CountDownLatch done = new CountDownLatch(1);
478 <        final int TASKS = 5;
479 <        try {
478 >        final ThreadPoolExecutor p = new CustomExecutor(1);
479 >        try (PoolCleaner cleaner = cleaner(p, done)) {
480 >            final CountDownLatch threadStarted = new CountDownLatch(1);
481              assertEquals(0, p.getTaskCount());
482 <            for (int i = 0; i < TASKS; i++)
482 >            assertEquals(0, p.getCompletedTaskCount());
483 >            p.execute(new CheckedRunnable() {
484 >                public void realRun() throws InterruptedException {
485 >                    threadStarted.countDown();
486 >                    done.await();
487 >                }});
488 >            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
489 >            assertEquals(1, p.getTaskCount());
490 >            assertEquals(0, p.getCompletedTaskCount());
491 >            for (int i = 0; i < TASKS; i++) {
492 >                assertEquals(1 + i, p.getTaskCount());
493                  p.execute(new CheckedRunnable() {
494                      public void realRun() throws InterruptedException {
495                          threadStarted.countDown();
496 +                        assertEquals(1 + TASKS, p.getTaskCount());
497                          done.await();
498                      }});
499 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
500 <            assertEquals(TASKS, p.getTaskCount());
501 <        } finally {
513 <            done.countDown();
514 <            joinPool(p);
499 >            }
500 >            assertEquals(1 + TASKS, p.getTaskCount());
501 >            assertEquals(0, p.getCompletedTaskCount());
502          }
503 +        assertEquals(1 + TASKS, p.getTaskCount());
504 +        assertEquals(1 + TASKS, p.getCompletedTaskCount());
505      }
506  
507      /**
508       * getThreadFactory returns factory in constructor if not set
509       */
510      public void testGetThreadFactory() {
511 <        ThreadFactory tf = new SimpleThreadFactory();
512 <        CustomExecutor p = new CustomExecutor(1, tf);
513 <        assertSame(tf, p.getThreadFactory());
514 <        joinPool(p);
511 >        final ThreadFactory threadFactory = new SimpleThreadFactory();
512 >        final CustomExecutor p = new CustomExecutor(1, threadFactory);
513 >        try (PoolCleaner cleaner = cleaner(p)) {
514 >            assertSame(threadFactory, p.getThreadFactory());
515 >        }
516      }
517  
518      /**
519       * setThreadFactory sets the thread factory returned by getThreadFactory
520       */
521      public void testSetThreadFactory() {
522 <        ThreadFactory tf = new SimpleThreadFactory();
523 <        CustomExecutor p = new CustomExecutor(1);
524 <        p.setThreadFactory(tf);
525 <        assertSame(tf, p.getThreadFactory());
526 <        joinPool(p);
522 >        final ThreadFactory threadFactory = new SimpleThreadFactory();
523 >        final CustomExecutor p = new CustomExecutor(1);
524 >        try (PoolCleaner cleaner = cleaner(p)) {
525 >            p.setThreadFactory(threadFactory);
526 >            assertSame(threadFactory, p.getThreadFactory());
527 >        }
528      }
529  
530      /**
531       * setThreadFactory(null) throws NPE
532       */
533      public void testSetThreadFactoryNull() {
534 <        CustomExecutor p = new CustomExecutor(1);
535 <        try {
536 <            p.setThreadFactory(null);
537 <            shouldThrow();
538 <        } catch (NullPointerException success) {
539 <        } finally {
549 <            joinPool(p);
534 >        final CustomExecutor p = new CustomExecutor(1);
535 >        try (PoolCleaner cleaner = cleaner(p)) {
536 >            try {
537 >                p.setThreadFactory(null);
538 >                shouldThrow();
539 >            } catch (NullPointerException success) {}
540          }
541      }
542  
# Line 554 | Line 544 | public class ScheduledExecutorSubclassTe
544       * isShutdown is false before shutdown, true after
545       */
546      public void testIsShutdown() {
547 <        CustomExecutor p = new CustomExecutor(1);
548 <        try {
547 >        final CustomExecutor p = new CustomExecutor(1);
548 >        try (PoolCleaner cleaner = cleaner(p)) {
549              assertFalse(p.isShutdown());
560        }
561        finally {
550              try { p.shutdown(); } catch (SecurityException ok) { return; }
551 +            assertTrue(p.isShutdown());
552          }
564        assertTrue(p.isShutdown());
553      }
554  
555      /**
# Line 569 | Line 557 | public class ScheduledExecutorSubclassTe
557       */
558      public void testIsTerminated() throws InterruptedException {
559          final ThreadPoolExecutor p = new CustomExecutor(1);
560 <        final CountDownLatch threadStarted = new CountDownLatch(1);
561 <        final CountDownLatch done = new CountDownLatch(1);
562 <        assertFalse(p.isTerminated());
563 <        try {
560 >        try (PoolCleaner cleaner = cleaner(p)) {
561 >            final CountDownLatch threadStarted = new CountDownLatch(1);
562 >            final CountDownLatch done = new CountDownLatch(1);
563 >            assertFalse(p.isTerminated());
564              p.execute(new CheckedRunnable() {
565                  public void realRun() throws InterruptedException {
566                      assertFalse(p.isTerminated());
567                      threadStarted.countDown();
568                      done.await();
569                  }});
570 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
570 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
571              assertFalse(p.isTerminating());
572              done.countDown();
585        } finally {
573              try { p.shutdown(); } catch (SecurityException ok) { return; }
574 +            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
575 +            assertTrue(p.isTerminated());
576          }
588        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
589        assertTrue(p.isTerminated());
577      }
578  
579      /**
# Line 594 | Line 581 | public class ScheduledExecutorSubclassTe
581       */
582      public void testIsTerminating() throws InterruptedException {
583          final ThreadPoolExecutor p = new CustomExecutor(1);
584 <        final CountDownLatch threadStarted = new CountDownLatch(1);
585 <        final CountDownLatch done = new CountDownLatch(1);
586 <        try {
584 >        try (PoolCleaner cleaner = cleaner(p)) {
585 >            final CountDownLatch threadStarted = new CountDownLatch(1);
586 >            final CountDownLatch done = new CountDownLatch(1);
587              assertFalse(p.isTerminating());
588              p.execute(new CheckedRunnable() {
589                  public void realRun() throws InterruptedException {
# Line 604 | Line 591 | public class ScheduledExecutorSubclassTe
591                      threadStarted.countDown();
592                      done.await();
593                  }});
594 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
594 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
595              assertFalse(p.isTerminating());
596              done.countDown();
610        } finally {
597              try { p.shutdown(); } catch (SecurityException ok) { return; }
598 +            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
599 +            assertTrue(p.isTerminated());
600 +            assertFalse(p.isTerminating());
601          }
613        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
614        assertTrue(p.isTerminated());
615        assertFalse(p.isTerminating());
602      }
603  
604      /**
605       * getQueue returns the work queue, which contains queued tasks
606       */
607      public void testGetQueue() throws InterruptedException {
608 <        ScheduledThreadPoolExecutor p = new CustomExecutor(1);
609 <        final CountDownLatch threadStarted = new CountDownLatch(1);
610 <        final CountDownLatch done = new CountDownLatch(1);
611 <        try {
608 >        final ScheduledThreadPoolExecutor p = new CustomExecutor(1);
609 >        try (PoolCleaner cleaner = cleaner(p)) {
610 >            final CountDownLatch threadStarted = new CountDownLatch(1);
611 >            final CountDownLatch done = new CountDownLatch(1);
612              ScheduledFuture[] tasks = new ScheduledFuture[5];
613              for (int i = 0; i < tasks.length; i++) {
614                  Runnable r = new CheckedRunnable() {
# Line 632 | Line 618 | public class ScheduledExecutorSubclassTe
618                      }};
619                  tasks[i] = p.schedule(r, 1, MILLISECONDS);
620              }
621 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
621 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
622              BlockingQueue<Runnable> q = p.getQueue();
623              assertTrue(q.contains(tasks[tasks.length - 1]));
624              assertFalse(q.contains(tasks[0]));
639        } finally {
625              done.countDown();
641            joinPool(p);
626          }
627      }
628  
# Line 647 | Line 631 | public class ScheduledExecutorSubclassTe
631       */
632      public void testRemove() throws InterruptedException {
633          final ScheduledThreadPoolExecutor p = new CustomExecutor(1);
634 <        ScheduledFuture[] tasks = new ScheduledFuture[5];
635 <        final CountDownLatch threadStarted = new CountDownLatch(1);
636 <        final CountDownLatch done = new CountDownLatch(1);
637 <        try {
634 >        try (PoolCleaner cleaner = cleaner(p)) {
635 >            ScheduledFuture[] tasks = new ScheduledFuture[5];
636 >            final CountDownLatch threadStarted = new CountDownLatch(1);
637 >            final CountDownLatch done = new CountDownLatch(1);
638              for (int i = 0; i < tasks.length; i++) {
639                  Runnable r = new CheckedRunnable() {
640                      public void realRun() throws InterruptedException {
# Line 659 | Line 643 | public class ScheduledExecutorSubclassTe
643                      }};
644                  tasks[i] = p.schedule(r, 1, MILLISECONDS);
645              }
646 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
646 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
647              BlockingQueue<Runnable> q = p.getQueue();
648              assertFalse(p.remove((Runnable)tasks[0]));
649              assertTrue(q.contains((Runnable)tasks[4]));
# Line 670 | Line 654 | public class ScheduledExecutorSubclassTe
654              assertTrue(q.contains((Runnable)tasks[3]));
655              assertTrue(p.remove((Runnable)tasks[3]));
656              assertFalse(q.contains((Runnable)tasks[3]));
673        } finally {
657              done.countDown();
675            joinPool(p);
658          }
659      }
660  
# Line 680 | Line 662 | public class ScheduledExecutorSubclassTe
662       * purge removes cancelled tasks from the queue
663       */
664      public void testPurge() throws InterruptedException {
665 <        CustomExecutor p = new CustomExecutor(1);
665 >        final CustomExecutor p = new CustomExecutor(1);
666          ScheduledFuture[] tasks = new ScheduledFuture[5];
667          for (int i = 0; i < tasks.length; i++)
668              tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(),
# Line 710 | Line 692 | public class ScheduledExecutorSubclassTe
692       * shutdownNow returns a list containing tasks that were not run,
693       * and those tasks are drained from the queue
694       */
695 +    public void testShutdownNow() throws InterruptedException {
696 +        final int poolSize = 2;
697 +        final int count = 5;
698 +        final AtomicInteger ran = new AtomicInteger(0);
699 +        final CustomExecutor p = new CustomExecutor(poolSize);
700 +        final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
701 +        Runnable waiter = new CheckedRunnable() { public void realRun() {
702 +            threadsStarted.countDown();
703 +            try {
704 +                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
705 +            } catch (InterruptedException success) {}
706 +            ran.getAndIncrement();
707 +        }};
708 +        for (int i = 0; i < count; i++)
709 +            p.execute(waiter);
710 +        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
711 +        assertEquals(poolSize, p.getActiveCount());
712 +        assertEquals(0, p.getCompletedTaskCount());
713 +        final List<Runnable> queuedTasks;
714 +        try {
715 +            queuedTasks = p.shutdownNow();
716 +        } catch (SecurityException ok) {
717 +            return; // Allowed in case test doesn't have privs
718 +        }
719 +        assertTrue(p.isShutdown());
720 +        assertTrue(p.getQueue().isEmpty());
721 +        assertEquals(count - poolSize, queuedTasks.size());
722 +        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
723 +        assertTrue(p.isTerminated());
724 +        assertEquals(poolSize, ran.get());
725 +        assertEquals(poolSize, p.getCompletedTaskCount());
726 +    }
727 +
728 +    /**
729 +     * shutdownNow returns a list containing tasks that were not run,
730 +     * and those tasks are drained from the queue
731 +     */
732      public void testShutdownNow_delayedTasks() throws InterruptedException {
733 <        CustomExecutor p = new CustomExecutor(1);
733 >        final CustomExecutor p = new CustomExecutor(1);
734          List<ScheduledFuture> tasks = new ArrayList<>();
735          for (int i = 0; i < 3; i++) {
736              Runnable r = new NoOpRunnable();
# Line 719 | Line 738 | public class ScheduledExecutorSubclassTe
738              tasks.add(p.scheduleAtFixedRate(r, 9, 9, SECONDS));
739              tasks.add(p.scheduleWithFixedDelay(r, 9, 9, SECONDS));
740          }
741 <        assertEquals(new HashSet(tasks), new HashSet(p.getQueue()));
741 >        if (testImplementationDetails)
742 >            assertEquals(new HashSet(tasks), new HashSet(p.getQueue()));
743          final List<Runnable> queuedTasks;
744          try {
745              queuedTasks = p.shutdownNow();
# Line 728 | Line 748 | public class ScheduledExecutorSubclassTe
748          }
749          assertTrue(p.isShutdown());
750          assertTrue(p.getQueue().isEmpty());
751 <        assertEquals(new HashSet(tasks), new HashSet(queuedTasks));
751 >        if (testImplementationDetails)
752 >            assertEquals(new HashSet(tasks), new HashSet(queuedTasks));
753          assertEquals(tasks.size(), queuedTasks.size());
754          for (ScheduledFuture task : tasks) {
755              assertFalse(((CustomTask)task).ran);
# Line 740 | Line 761 | public class ScheduledExecutorSubclassTe
761      }
762  
763      /**
764 <     * In default setting, shutdown cancels periodic but not delayed
765 <     * tasks at shutdown
766 <     */
767 <    public void testShutdown1() throws InterruptedException {
768 <        CustomExecutor p = new CustomExecutor(1);
769 <        assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
770 <        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
764 >     * By default, periodic tasks are cancelled at shutdown.
765 >     * By default, delayed tasks keep running after shutdown.
766 >     * Check that changing the default values work:
767 >     * - setExecuteExistingDelayedTasksAfterShutdownPolicy
768 >     * - setContinueExistingPeriodicTasksAfterShutdownPolicy
769 >     */
770 >    public void testShutdown_cancellation() throws Exception {
771 >        Boolean[] allBooleans = { null, Boolean.FALSE, Boolean.TRUE };
772 >        for (Boolean policy : allBooleans)
773 >    {
774 >        final int poolSize = 2;
775 >        final CustomExecutor p = new CustomExecutor(poolSize);
776 >        final boolean effectiveDelayedPolicy = (policy != Boolean.FALSE);
777 >        final boolean effectivePeriodicPolicy = (policy == Boolean.TRUE);
778 >        final boolean effectiveRemovePolicy = (policy == Boolean.TRUE);
779 >        if (policy != null) {
780 >            p.setExecuteExistingDelayedTasksAfterShutdownPolicy(policy);
781 >            p.setContinueExistingPeriodicTasksAfterShutdownPolicy(policy);
782 >            p.setRemoveOnCancelPolicy(policy);
783 >        }
784 >        assertEquals(effectiveDelayedPolicy,
785 >                     p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
786 >        assertEquals(effectivePeriodicPolicy,
787 >                     p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
788 >        assertEquals(effectiveRemovePolicy,
789 >                     p.getRemoveOnCancelPolicy());
790 >        // Strategy: Wedge the pool with poolSize "blocker" threads
791 >        final AtomicInteger ran = new AtomicInteger(0);
792 >        final CountDownLatch poolBlocked = new CountDownLatch(poolSize);
793 >        final CountDownLatch unblock = new CountDownLatch(1);
794 >        final CountDownLatch periodicLatch1 = new CountDownLatch(2);
795 >        final CountDownLatch periodicLatch2 = new CountDownLatch(2);
796 >        Runnable task = new CheckedRunnable() { public void realRun()
797 >                                                    throws InterruptedException {
798 >            poolBlocked.countDown();
799 >            assertTrue(unblock.await(LONG_DELAY_MS, MILLISECONDS));
800 >            ran.getAndIncrement();
801 >        }};
802 >        List<Future<?>> blockers = new ArrayList<>();
803 >        List<Future<?>> periodics = new ArrayList<>();
804 >        List<Future<?>> delayeds = new ArrayList<>();
805 >        for (int i = 0; i < poolSize; i++)
806 >            blockers.add(p.submit(task));
807 >        assertTrue(poolBlocked.await(LONG_DELAY_MS, MILLISECONDS));
808 >
809 >        periodics.add(p.scheduleAtFixedRate(countDowner(periodicLatch1),
810 >                                            1, 1, MILLISECONDS));
811 >        periodics.add(p.scheduleWithFixedDelay(countDowner(periodicLatch2),
812 >                                               1, 1, MILLISECONDS));
813 >        delayeds.add(p.schedule(task, 1, MILLISECONDS));
814  
815 <        ScheduledFuture[] tasks = new ScheduledFuture[5];
816 <        for (int i = 0; i < tasks.length; i++)
753 <            tasks[i] = p.schedule(new NoOpRunnable(),
754 <                                  SHORT_DELAY_MS, MILLISECONDS);
755 <        try { p.shutdown(); } catch (SecurityException ok) { return; }
756 <        BlockingQueue<Runnable> q = p.getQueue();
757 <        for (ScheduledFuture task : tasks) {
758 <            assertFalse(task.isDone());
759 <            assertFalse(task.isCancelled());
760 <            assertTrue(q.contains(task));
761 <        }
762 <        assertTrue(p.isShutdown());
763 <        assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
764 <        assertTrue(p.isTerminated());
765 <        for (ScheduledFuture task : tasks) {
766 <            assertTrue(task.isDone());
767 <            assertFalse(task.isCancelled());
768 <        }
769 <    }
770 <
771 <    /**
772 <     * If setExecuteExistingDelayedTasksAfterShutdownPolicy is false,
773 <     * delayed tasks are cancelled at shutdown
774 <     */
775 <    public void testShutdown2() throws InterruptedException {
776 <        CustomExecutor p = new CustomExecutor(1);
777 <        p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
778 <        assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
779 <        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
780 <        ScheduledFuture[] tasks = new ScheduledFuture[5];
781 <        for (int i = 0; i < tasks.length; i++)
782 <            tasks[i] = p.schedule(new NoOpRunnable(),
783 <                                  SHORT_DELAY_MS, MILLISECONDS);
784 <        BlockingQueue q = p.getQueue();
785 <        assertEquals(tasks.length, q.size());
786 <        try { p.shutdown(); } catch (SecurityException ok) { return; }
787 <        assertTrue(p.isShutdown());
788 <        assertTrue(q.isEmpty());
789 <        assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
790 <        assertTrue(p.isTerminated());
791 <        for (ScheduledFuture task : tasks) {
792 <            assertTrue(task.isDone());
793 <            assertTrue(task.isCancelled());
794 <        }
795 <    }
796 <
797 <    /**
798 <     * If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false,
799 <     * periodic tasks are cancelled at shutdown
800 <     */
801 <    public void testShutdown3() throws InterruptedException {
802 <        CustomExecutor p = new CustomExecutor(1);
803 <        assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
804 <        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
805 <        p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
806 <        assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
807 <        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
808 <        long initialDelay = LONG_DELAY_MS;
809 <        ScheduledFuture task =
810 <            p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay,
811 <                                  5, MILLISECONDS);
815 >        assertTrue(p.getQueue().containsAll(periodics));
816 >        assertTrue(p.getQueue().containsAll(delayeds));
817          try { p.shutdown(); } catch (SecurityException ok) { return; }
818          assertTrue(p.isShutdown());
819 <        assertTrue(p.getQueue().isEmpty());
820 <        assertTrue(task.isDone());
821 <        assertTrue(task.isCancelled());
822 <        joinPool(p);
823 <    }
824 <
825 <    /**
826 <     * if setContinueExistingPeriodicTasksAfterShutdownPolicy is true,
827 <     * periodic tasks are not cancelled at shutdown
828 <     */
829 <    public void testShutdown4() throws InterruptedException {
830 <        CustomExecutor p = new CustomExecutor(1);
831 <        final CountDownLatch counter = new CountDownLatch(2);
832 <        try {
833 <            p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
834 <            assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
835 <            assertTrue(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
836 <            final Runnable r = new CheckedRunnable() {
837 <                public void realRun() {
838 <                    counter.countDown();
839 <                }};
840 <            ScheduledFuture task =
836 <                p.scheduleAtFixedRate(r, 1, 1, MILLISECONDS);
837 <            assertFalse(task.isDone());
838 <            assertFalse(task.isCancelled());
839 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
840 <            assertFalse(task.isCancelled());
841 <            assertFalse(p.isTerminated());
842 <            assertTrue(p.isShutdown());
843 <            assertTrue(counter.await(SMALL_DELAY_MS, MILLISECONDS));
844 <            assertFalse(task.isCancelled());
845 <            assertTrue(task.cancel(false));
846 <            assertTrue(task.isDone());
847 <            assertTrue(task.isCancelled());
848 <            assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
849 <            assertTrue(p.isTerminated());
819 >        assertFalse(p.isTerminated());
820 >        for (Future<?> periodic : periodics) {
821 >            assertTrue(effectivePeriodicPolicy ^ periodic.isCancelled());
822 >            assertTrue(effectivePeriodicPolicy ^ periodic.isDone());
823 >        }
824 >        for (Future<?> delayed : delayeds) {
825 >            assertTrue(effectiveDelayedPolicy ^ delayed.isCancelled());
826 >            assertTrue(effectiveDelayedPolicy ^ delayed.isDone());
827 >        }
828 >        if (testImplementationDetails) {
829 >            assertEquals(effectivePeriodicPolicy,
830 >                         p.getQueue().containsAll(periodics));
831 >            assertEquals(effectiveDelayedPolicy,
832 >                         p.getQueue().containsAll(delayeds));
833 >        }
834 >        // Release all pool threads
835 >        unblock.countDown();
836 >
837 >        for (Future<?> delayed : delayeds) {
838 >            if (effectiveDelayedPolicy) {
839 >                assertNull(delayed.get());
840 >            }
841          }
842 <        finally {
843 <            joinPool(p);
842 >        if (effectivePeriodicPolicy) {
843 >            assertTrue(periodicLatch1.await(LONG_DELAY_MS, MILLISECONDS));
844 >            assertTrue(periodicLatch2.await(LONG_DELAY_MS, MILLISECONDS));
845 >            for (Future<?> periodic : periodics) {
846 >                assertTrue(periodic.cancel(false));
847 >                assertTrue(periodic.isCancelled());
848 >                assertTrue(periodic.isDone());
849 >            }
850          }
851 <    }
851 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
852 >        assertTrue(p.isTerminated());
853 >        assertEquals(2 + (effectiveDelayedPolicy ? 1 : 0), ran.get());
854 >    }}
855  
856      /**
857       * completed submit of callable returns result
858       */
859      public void testSubmitCallable() throws Exception {
860 <        ExecutorService e = new CustomExecutor(2);
861 <        try {
860 >        final ExecutorService e = new CustomExecutor(2);
861 >        try (PoolCleaner cleaner = cleaner(e)) {
862              Future<String> future = e.submit(new StringTask());
863              String result = future.get();
864              assertSame(TEST_STRING, result);
865        } finally {
866            joinPool(e);
865          }
866      }
867  
# Line 871 | Line 869 | public class ScheduledExecutorSubclassTe
869       * completed submit of runnable returns successfully
870       */
871      public void testSubmitRunnable() throws Exception {
872 <        ExecutorService e = new CustomExecutor(2);
873 <        try {
872 >        final ExecutorService e = new CustomExecutor(2);
873 >        try (PoolCleaner cleaner = cleaner(e)) {
874              Future<?> future = e.submit(new NoOpRunnable());
875              future.get();
876              assertTrue(future.isDone());
879        } finally {
880            joinPool(e);
877          }
878      }
879  
# Line 885 | Line 881 | public class ScheduledExecutorSubclassTe
881       * completed submit of (runnable, result) returns result
882       */
883      public void testSubmitRunnable2() throws Exception {
884 <        ExecutorService e = new CustomExecutor(2);
885 <        try {
884 >        final ExecutorService e = new CustomExecutor(2);
885 >        try (PoolCleaner cleaner = cleaner(e)) {
886              Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
887              String result = future.get();
888              assertSame(TEST_STRING, result);
893        } finally {
894            joinPool(e);
889          }
890      }
891  
# Line 899 | Line 893 | public class ScheduledExecutorSubclassTe
893       * invokeAny(null) throws NPE
894       */
895      public void testInvokeAny1() throws Exception {
896 <        ExecutorService e = new CustomExecutor(2);
897 <        try {
898 <            e.invokeAny(null);
899 <            shouldThrow();
900 <        } catch (NullPointerException success) {
901 <        } finally {
908 <            joinPool(e);
896 >        final ExecutorService e = new CustomExecutor(2);
897 >        try (PoolCleaner cleaner = cleaner(e)) {
898 >            try {
899 >                e.invokeAny(null);
900 >                shouldThrow();
901 >            } catch (NullPointerException success) {}
902          }
903      }
904  
# Line 913 | Line 906 | public class ScheduledExecutorSubclassTe
906       * invokeAny(empty collection) throws IAE
907       */
908      public void testInvokeAny2() throws Exception {
909 <        ExecutorService e = new CustomExecutor(2);
910 <        try {
911 <            e.invokeAny(new ArrayList<Callable<String>>());
912 <            shouldThrow();
913 <        } catch (IllegalArgumentException success) {
914 <        } finally {
922 <            joinPool(e);
909 >        final ExecutorService e = new CustomExecutor(2);
910 >        try (PoolCleaner cleaner = cleaner(e)) {
911 >            try {
912 >                e.invokeAny(new ArrayList<Callable<String>>());
913 >                shouldThrow();
914 >            } catch (IllegalArgumentException success) {}
915          }
916      }
917  
# Line 927 | Line 919 | public class ScheduledExecutorSubclassTe
919       * invokeAny(c) throws NPE if c has null elements
920       */
921      public void testInvokeAny3() throws Exception {
922 <        CountDownLatch latch = new CountDownLatch(1);
923 <        ExecutorService e = new CustomExecutor(2);
924 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
925 <        l.add(latchAwaitingStringTask(latch));
926 <        l.add(null);
927 <        try {
928 <            e.invokeAny(l);
929 <            shouldThrow();
930 <        } catch (NullPointerException success) {
931 <        } finally {
922 >        final CountDownLatch latch = new CountDownLatch(1);
923 >        final ExecutorService e = new CustomExecutor(2);
924 >        try (PoolCleaner cleaner = cleaner(e)) {
925 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
926 >            l.add(latchAwaitingStringTask(latch));
927 >            l.add(null);
928 >            try {
929 >                e.invokeAny(l);
930 >                shouldThrow();
931 >            } catch (NullPointerException success) {}
932              latch.countDown();
941            joinPool(e);
933          }
934      }
935  
# Line 946 | Line 937 | public class ScheduledExecutorSubclassTe
937       * invokeAny(c) throws ExecutionException if no task completes
938       */
939      public void testInvokeAny4() throws Exception {
940 <        ExecutorService e = new CustomExecutor(2);
941 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
942 <        l.add(new NPETask());
943 <        try {
944 <            e.invokeAny(l);
945 <            shouldThrow();
946 <        } catch (ExecutionException success) {
947 <            assertTrue(success.getCause() instanceof NullPointerException);
948 <        } finally {
949 <            joinPool(e);
940 >        final ExecutorService e = new CustomExecutor(2);
941 >        try (PoolCleaner cleaner = cleaner(e)) {
942 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
943 >            l.add(new NPETask());
944 >            try {
945 >                e.invokeAny(l);
946 >                shouldThrow();
947 >            } catch (ExecutionException success) {
948 >                assertTrue(success.getCause() instanceof NullPointerException);
949 >            }
950          }
951      }
952  
# Line 963 | Line 954 | public class ScheduledExecutorSubclassTe
954       * invokeAny(c) returns result of some task
955       */
956      public void testInvokeAny5() throws Exception {
957 <        ExecutorService e = new CustomExecutor(2);
958 <        try {
957 >        final ExecutorService e = new CustomExecutor(2);
958 >        try (PoolCleaner cleaner = cleaner(e)) {
959              List<Callable<String>> l = new ArrayList<Callable<String>>();
960              l.add(new StringTask());
961              l.add(new StringTask());
962              String result = e.invokeAny(l);
963              assertSame(TEST_STRING, result);
973        } finally {
974            joinPool(e);
964          }
965      }
966  
# Line 979 | Line 968 | public class ScheduledExecutorSubclassTe
968       * invokeAll(null) throws NPE
969       */
970      public void testInvokeAll1() throws Exception {
971 <        ExecutorService e = new CustomExecutor(2);
972 <        try {
973 <            e.invokeAll(null);
974 <            shouldThrow();
975 <        } catch (NullPointerException success) {
976 <        } finally {
988 <            joinPool(e);
971 >        final ExecutorService e = new CustomExecutor(2);
972 >        try (PoolCleaner cleaner = cleaner(e)) {
973 >            try {
974 >                e.invokeAll(null);
975 >                shouldThrow();
976 >            } catch (NullPointerException success) {}
977          }
978      }
979  
# Line 993 | Line 981 | public class ScheduledExecutorSubclassTe
981       * invokeAll(empty collection) returns empty collection
982       */
983      public void testInvokeAll2() throws Exception {
984 <        ExecutorService e = new CustomExecutor(2);
985 <        try {
984 >        final ExecutorService e = new CustomExecutor(2);
985 >        try (PoolCleaner cleaner = cleaner(e)) {
986              List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
987              assertTrue(r.isEmpty());
1000        } finally {
1001            joinPool(e);
988          }
989      }
990  
# Line 1006 | Line 992 | public class ScheduledExecutorSubclassTe
992       * invokeAll(c) throws NPE if c has null elements
993       */
994      public void testInvokeAll3() throws Exception {
995 <        ExecutorService e = new CustomExecutor(2);
996 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
997 <        l.add(new StringTask());
998 <        l.add(null);
999 <        try {
1000 <            e.invokeAll(l);
1001 <            shouldThrow();
1002 <        } catch (NullPointerException success) {
1003 <        } finally {
1018 <            joinPool(e);
995 >        final ExecutorService e = new CustomExecutor(2);
996 >        try (PoolCleaner cleaner = cleaner(e)) {
997 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
998 >            l.add(new StringTask());
999 >            l.add(null);
1000 >            try {
1001 >                e.invokeAll(l);
1002 >                shouldThrow();
1003 >            } catch (NullPointerException success) {}
1004          }
1005      }
1006  
# Line 1023 | Line 1008 | public class ScheduledExecutorSubclassTe
1008       * get of invokeAll(c) throws exception on failed task
1009       */
1010      public void testInvokeAll4() throws Exception {
1011 <        ExecutorService e = new CustomExecutor(2);
1012 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1013 <        l.add(new NPETask());
1014 <        List<Future<String>> futures = e.invokeAll(l);
1015 <        assertEquals(1, futures.size());
1016 <        try {
1017 <            futures.get(0).get();
1018 <            shouldThrow();
1019 <        } catch (ExecutionException success) {
1020 <            assertTrue(success.getCause() instanceof NullPointerException);
1021 <        } finally {
1022 <            joinPool(e);
1011 >        final ExecutorService e = new CustomExecutor(2);
1012 >        try (PoolCleaner cleaner = cleaner(e)) {
1013 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1014 >            l.add(new NPETask());
1015 >            List<Future<String>> futures = e.invokeAll(l);
1016 >            assertEquals(1, futures.size());
1017 >            try {
1018 >                futures.get(0).get();
1019 >                shouldThrow();
1020 >            } catch (ExecutionException success) {
1021 >                assertTrue(success.getCause() instanceof NullPointerException);
1022 >            }
1023          }
1024      }
1025  
# Line 1042 | Line 1027 | public class ScheduledExecutorSubclassTe
1027       * invokeAll(c) returns results of all completed tasks
1028       */
1029      public void testInvokeAll5() throws Exception {
1030 <        ExecutorService e = new CustomExecutor(2);
1031 <        try {
1030 >        final ExecutorService e = new CustomExecutor(2);
1031 >        try (PoolCleaner cleaner = cleaner(e)) {
1032              List<Callable<String>> l = new ArrayList<Callable<String>>();
1033              l.add(new StringTask());
1034              l.add(new StringTask());
# Line 1051 | Line 1036 | public class ScheduledExecutorSubclassTe
1036              assertEquals(2, futures.size());
1037              for (Future<String> future : futures)
1038                  assertSame(TEST_STRING, future.get());
1054        } finally {
1055            joinPool(e);
1039          }
1040      }
1041  
# Line 1060 | Line 1043 | public class ScheduledExecutorSubclassTe
1043       * timed invokeAny(null) throws NPE
1044       */
1045      public void testTimedInvokeAny1() throws Exception {
1046 <        ExecutorService e = new CustomExecutor(2);
1047 <        try {
1048 <            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1049 <            shouldThrow();
1050 <        } catch (NullPointerException success) {
1051 <        } finally {
1069 <            joinPool(e);
1046 >        final ExecutorService e = new CustomExecutor(2);
1047 >        try (PoolCleaner cleaner = cleaner(e)) {
1048 >            try {
1049 >                e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1050 >                shouldThrow();
1051 >            } catch (NullPointerException success) {}
1052          }
1053      }
1054  
# Line 1074 | Line 1056 | public class ScheduledExecutorSubclassTe
1056       * timed invokeAny(,,null) throws NPE
1057       */
1058      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1059 <        ExecutorService e = new CustomExecutor(2);
1060 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1061 <        l.add(new StringTask());
1062 <        try {
1063 <            e.invokeAny(l, MEDIUM_DELAY_MS, null);
1064 <            shouldThrow();
1065 <        } catch (NullPointerException success) {
1066 <        } finally {
1085 <            joinPool(e);
1059 >        final ExecutorService e = new CustomExecutor(2);
1060 >        try (PoolCleaner cleaner = cleaner(e)) {
1061 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1062 >            l.add(new StringTask());
1063 >            try {
1064 >                e.invokeAny(l, MEDIUM_DELAY_MS, null);
1065 >                shouldThrow();
1066 >            } catch (NullPointerException success) {}
1067          }
1068      }
1069  
# Line 1090 | Line 1071 | public class ScheduledExecutorSubclassTe
1071       * timed invokeAny(empty collection) throws IAE
1072       */
1073      public void testTimedInvokeAny2() throws Exception {
1074 <        ExecutorService e = new CustomExecutor(2);
1075 <        try {
1076 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1077 <            shouldThrow();
1078 <        } catch (IllegalArgumentException success) {
1079 <        } finally {
1099 <            joinPool(e);
1074 >        final ExecutorService e = new CustomExecutor(2);
1075 >        try (PoolCleaner cleaner = cleaner(e)) {
1076 >            try {
1077 >                e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1078 >                shouldThrow();
1079 >            } catch (IllegalArgumentException success) {}
1080          }
1081      }
1082  
# Line 1105 | Line 1085 | public class ScheduledExecutorSubclassTe
1085       */
1086      public void testTimedInvokeAny3() throws Exception {
1087          CountDownLatch latch = new CountDownLatch(1);
1088 <        ExecutorService e = new CustomExecutor(2);
1089 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1090 <        l.add(latchAwaitingStringTask(latch));
1091 <        l.add(null);
1092 <        try {
1093 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1094 <            shouldThrow();
1095 <        } catch (NullPointerException success) {
1096 <        } finally {
1088 >        final ExecutorService e = new CustomExecutor(2);
1089 >        try (PoolCleaner cleaner = cleaner(e)) {
1090 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1091 >            l.add(latchAwaitingStringTask(latch));
1092 >            l.add(null);
1093 >            try {
1094 >                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1095 >                shouldThrow();
1096 >            } catch (NullPointerException success) {}
1097              latch.countDown();
1118            joinPool(e);
1098          }
1099      }
1100  
# Line 1123 | Line 1102 | public class ScheduledExecutorSubclassTe
1102       * timed invokeAny(c) throws ExecutionException if no task completes
1103       */
1104      public void testTimedInvokeAny4() throws Exception {
1105 <        ExecutorService e = new CustomExecutor(2);
1106 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1107 <        l.add(new NPETask());
1108 <        try {
1109 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1110 <            shouldThrow();
1111 <        } catch (ExecutionException success) {
1112 <            assertTrue(success.getCause() instanceof NullPointerException);
1113 <        } finally {
1114 <            joinPool(e);
1105 >        final ExecutorService e = new CustomExecutor(2);
1106 >        try (PoolCleaner cleaner = cleaner(e)) {
1107 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1108 >            l.add(new NPETask());
1109 >            try {
1110 >                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1111 >                shouldThrow();
1112 >            } catch (ExecutionException success) {
1113 >                assertTrue(success.getCause() instanceof NullPointerException);
1114 >            }
1115          }
1116      }
1117  
# Line 1140 | Line 1119 | public class ScheduledExecutorSubclassTe
1119       * timed invokeAny(c) returns result of some task
1120       */
1121      public void testTimedInvokeAny5() throws Exception {
1122 <        ExecutorService e = new CustomExecutor(2);
1123 <        try {
1122 >        final ExecutorService e = new CustomExecutor(2);
1123 >        try (PoolCleaner cleaner = cleaner(e)) {
1124              List<Callable<String>> l = new ArrayList<Callable<String>>();
1125              l.add(new StringTask());
1126              l.add(new StringTask());
1127              String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1128              assertSame(TEST_STRING, result);
1150        } finally {
1151            joinPool(e);
1129          }
1130      }
1131  
# Line 1156 | Line 1133 | public class ScheduledExecutorSubclassTe
1133       * timed invokeAll(null) throws NPE
1134       */
1135      public void testTimedInvokeAll1() throws Exception {
1136 <        ExecutorService e = new CustomExecutor(2);
1137 <        try {
1138 <            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1139 <            shouldThrow();
1140 <        } catch (NullPointerException success) {
1141 <        } finally {
1165 <            joinPool(e);
1136 >        final ExecutorService e = new CustomExecutor(2);
1137 >        try (PoolCleaner cleaner = cleaner(e)) {
1138 >            try {
1139 >                e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1140 >                shouldThrow();
1141 >            } catch (NullPointerException success) {}
1142          }
1143      }
1144  
# Line 1170 | Line 1146 | public class ScheduledExecutorSubclassTe
1146       * timed invokeAll(,,null) throws NPE
1147       */
1148      public void testTimedInvokeAllNullTimeUnit() throws Exception {
1149 <        ExecutorService e = new CustomExecutor(2);
1150 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1151 <        l.add(new StringTask());
1152 <        try {
1153 <            e.invokeAll(l, MEDIUM_DELAY_MS, null);
1154 <            shouldThrow();
1155 <        } catch (NullPointerException success) {
1156 <        } finally {
1181 <            joinPool(e);
1149 >        final ExecutorService e = new CustomExecutor(2);
1150 >        try (PoolCleaner cleaner = cleaner(e)) {
1151 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1152 >            l.add(new StringTask());
1153 >            try {
1154 >                e.invokeAll(l, MEDIUM_DELAY_MS, null);
1155 >                shouldThrow();
1156 >            } catch (NullPointerException success) {}
1157          }
1158      }
1159  
# Line 1186 | Line 1161 | public class ScheduledExecutorSubclassTe
1161       * timed invokeAll(empty collection) returns empty collection
1162       */
1163      public void testTimedInvokeAll2() throws Exception {
1164 <        ExecutorService e = new CustomExecutor(2);
1165 <        try {
1164 >        final ExecutorService e = new CustomExecutor(2);
1165 >        try (PoolCleaner cleaner = cleaner(e)) {
1166              List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1167              assertTrue(r.isEmpty());
1193        } finally {
1194            joinPool(e);
1168          }
1169      }
1170  
# Line 1199 | Line 1172 | public class ScheduledExecutorSubclassTe
1172       * timed invokeAll(c) throws NPE if c has null elements
1173       */
1174      public void testTimedInvokeAll3() throws Exception {
1175 <        ExecutorService e = new CustomExecutor(2);
1176 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1177 <        l.add(new StringTask());
1178 <        l.add(null);
1179 <        try {
1180 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1181 <            shouldThrow();
1182 <        } catch (NullPointerException success) {
1183 <        } finally {
1211 <            joinPool(e);
1175 >        final ExecutorService e = new CustomExecutor(2);
1176 >        try (PoolCleaner cleaner = cleaner(e)) {
1177 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1178 >            l.add(new StringTask());
1179 >            l.add(null);
1180 >            try {
1181 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1182 >                shouldThrow();
1183 >            } catch (NullPointerException success) {}
1184          }
1185      }
1186  
# Line 1216 | Line 1188 | public class ScheduledExecutorSubclassTe
1188       * get of element of invokeAll(c) throws exception on failed task
1189       */
1190      public void testTimedInvokeAll4() throws Exception {
1191 <        ExecutorService e = new CustomExecutor(2);
1192 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1193 <        l.add(new NPETask());
1194 <        List<Future<String>> futures =
1195 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1196 <        assertEquals(1, futures.size());
1197 <        try {
1198 <            futures.get(0).get();
1199 <            shouldThrow();
1200 <        } catch (ExecutionException success) {
1201 <            assertTrue(success.getCause() instanceof NullPointerException);
1202 <        } finally {
1203 <            joinPool(e);
1191 >        final ExecutorService e = new CustomExecutor(2);
1192 >        try (PoolCleaner cleaner = cleaner(e)) {
1193 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1194 >            l.add(new NPETask());
1195 >            List<Future<String>> futures =
1196 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1197 >            assertEquals(1, futures.size());
1198 >            try {
1199 >                futures.get(0).get();
1200 >                shouldThrow();
1201 >            } catch (ExecutionException success) {
1202 >                assertTrue(success.getCause() instanceof NullPointerException);
1203 >            }
1204          }
1205      }
1206  
# Line 1236 | Line 1208 | public class ScheduledExecutorSubclassTe
1208       * timed invokeAll(c) returns results of all completed tasks
1209       */
1210      public void testTimedInvokeAll5() throws Exception {
1211 <        ExecutorService e = new CustomExecutor(2);
1212 <        try {
1211 >        final ExecutorService e = new CustomExecutor(2);
1212 >        try (PoolCleaner cleaner = cleaner(e)) {
1213              List<Callable<String>> l = new ArrayList<Callable<String>>();
1214              l.add(new StringTask());
1215              l.add(new StringTask());
1216              List<Future<String>> futures =
1217 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1217 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1218              assertEquals(2, futures.size());
1219              for (Future<String> future : futures)
1220                  assertSame(TEST_STRING, future.get());
1249        } finally {
1250            joinPool(e);
1221          }
1222      }
1223  
# Line 1255 | Line 1225 | public class ScheduledExecutorSubclassTe
1225       * timed invokeAll(c) cancels tasks not completed by timeout
1226       */
1227      public void testTimedInvokeAll6() throws Exception {
1228 <        ExecutorService e = new CustomExecutor(2);
1229 <        try {
1228 >        final ExecutorService e = new CustomExecutor(2);
1229 >        try (PoolCleaner cleaner = cleaner(e)) {
1230              for (long timeout = timeoutMillis();;) {
1231                  List<Callable<String>> tasks = new ArrayList<>();
1232                  tasks.add(new StringTask("0"));
# Line 1280 | Line 1250 | public class ScheduledExecutorSubclassTe
1250                          fail("expected exactly one task to be cancelled");
1251                  }
1252              }
1283        } finally {
1284            joinPool(e);
1253          }
1254      }
1255  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines