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.43 by jsr166, Sat Oct 3 00:37:31 2015 UTC vs.
Revision 1.58 by jsr166, Thu Oct 8 03:08:37 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 >            await(done);
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 {
120        CustomExecutor p = new CustomExecutor(1);
121        final long startTime = System.nanoTime();
116          final CountDownLatch done = new CountDownLatch(1);
117 <        try {
117 >        final CustomExecutor p = new CustomExecutor(1);
118 >        try (PoolCleaner cleaner = cleaner(p, done)) {
119 >            final long startTime = System.nanoTime();
120              Callable task = new CheckedCallable<Boolean>() {
121                  public Boolean realCall() {
122                      done.countDown();
# Line 130 | Line 126 | public class ScheduledExecutorSubclassTe
126              Future f = p.schedule(task, timeoutMillis(), MILLISECONDS);
127              assertSame(Boolean.TRUE, f.get());
128              assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
133            assertTrue(done.await(0L, MILLISECONDS));
134        } finally {
135            joinPool(p);
129          }
130      }
131  
# Line 140 | Line 133 | public class ScheduledExecutorSubclassTe
133       * delayed schedule of runnable successfully executes after delay
134       */
135      public void testSchedule3() throws Exception {
136 <        CustomExecutor p = new CustomExecutor(1);
137 <        final long startTime = System.nanoTime();
138 <        final CountDownLatch done = new CountDownLatch(1);
139 <        try {
136 >        final CustomExecutor p = new CustomExecutor(1);
137 >        try (PoolCleaner cleaner = cleaner(p)) {
138 >            final long startTime = System.nanoTime();
139 >            final CountDownLatch done = new CountDownLatch(1);
140              Runnable task = new CheckedRunnable() {
141                  public void realRun() {
142                      done.countDown();
# Line 153 | Line 146 | public class ScheduledExecutorSubclassTe
146              await(done);
147              assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
148              assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
156        } finally {
157            joinPool(p);
149          }
150      }
151  
# Line 162 | Line 153 | public class ScheduledExecutorSubclassTe
153       * scheduleAtFixedRate executes runnable after given initial delay
154       */
155      public void testSchedule4() throws InterruptedException {
156 <        CustomExecutor p = new CustomExecutor(1);
157 <        final long startTime = System.nanoTime();
158 <        final CountDownLatch done = new CountDownLatch(1);
159 <        try {
156 >        final CustomExecutor p = new CustomExecutor(1);
157 >        try (PoolCleaner cleaner = cleaner(p)) {
158 >            final long startTime = System.nanoTime();
159 >            final CountDownLatch done = new CountDownLatch(1);
160              Runnable task = new CheckedRunnable() {
161                  public void realRun() {
162                      done.countDown();
# Line 177 | Line 168 | public class ScheduledExecutorSubclassTe
168              await(done);
169              assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
170              f.cancel(true);
180        } finally {
181            joinPool(p);
171          }
172      }
173  
# Line 186 | Line 175 | public class ScheduledExecutorSubclassTe
175       * scheduleWithFixedDelay executes runnable after given initial delay
176       */
177      public void testSchedule5() throws InterruptedException {
178 <        CustomExecutor p = new CustomExecutor(1);
179 <        final long startTime = System.nanoTime();
180 <        final CountDownLatch done = new CountDownLatch(1);
181 <        try {
178 >        final CustomExecutor p = new CustomExecutor(1);
179 >        try (PoolCleaner cleaner = cleaner(p)) {
180 >            final long startTime = System.nanoTime();
181 >            final CountDownLatch done = new CountDownLatch(1);
182              Runnable task = new CheckedRunnable() {
183                  public void realRun() {
184                      done.countDown();
# Line 201 | Line 190 | public class ScheduledExecutorSubclassTe
190              await(done);
191              assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
192              f.cancel(true);
204        } finally {
205            joinPool(p);
193          }
194      }
195  
# Line 215 | Line 202 | public class ScheduledExecutorSubclassTe
202       * scheduleAtFixedRate executes series of tasks at given rate
203       */
204      public void testFixedRateSequence() throws InterruptedException {
205 <        CustomExecutor p = new CustomExecutor(1);
206 <        try {
205 >        final CustomExecutor p = new CustomExecutor(1);
206 >        try (PoolCleaner cleaner = cleaner(p)) {
207              for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
208                  long startTime = System.nanoTime();
209                  int cycles = 10;
# Line 225 | Line 212 | public class ScheduledExecutorSubclassTe
212                      public void realRun() { done.countDown(); }};
213                  ScheduledFuture h =
214                      p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS);
215 <                done.await();
215 >                await(done);
216                  h.cancel(true);
217                  double normalizedTime =
218                      (double) millisElapsedSince(startTime) / delay;
# Line 234 | Line 221 | public class ScheduledExecutorSubclassTe
221                      return;
222              }
223              throw new AssertionError("unexpected execution rate");
237        } finally {
238            joinPool(p);
224          }
225      }
226  
# Line 243 | Line 228 | public class ScheduledExecutorSubclassTe
228       * scheduleWithFixedDelay executes series of tasks with given period
229       */
230      public void testFixedDelaySequence() throws InterruptedException {
231 <        CustomExecutor p = new CustomExecutor(1);
232 <        try {
231 >        final CustomExecutor p = new CustomExecutor(1);
232 >        try (PoolCleaner cleaner = cleaner(p)) {
233              for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
234                  long startTime = System.nanoTime();
235                  int cycles = 10;
# Line 253 | Line 238 | public class ScheduledExecutorSubclassTe
238                      public void realRun() { done.countDown(); }};
239                  ScheduledFuture h =
240                      p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS);
241 <                done.await();
241 >                await(done);
242                  h.cancel(true);
243                  double normalizedTime =
244                      (double) millisElapsedSince(startTime) / delay;
# Line 262 | Line 247 | public class ScheduledExecutorSubclassTe
247                      return;
248              }
249              throw new AssertionError("unexpected execution rate");
265        } finally {
266            joinPool(p);
250          }
251      }
252  
# Line 271 | Line 254 | public class ScheduledExecutorSubclassTe
254       * execute(null) throws NPE
255       */
256      public void testExecuteNull() throws InterruptedException {
257 <        CustomExecutor se = new CustomExecutor(1);
258 <        try {
259 <            se.execute(null);
260 <            shouldThrow();
261 <        } catch (NullPointerException success) {}
262 <        joinPool(se);
257 >        final CustomExecutor p = new CustomExecutor(1);
258 >        try (PoolCleaner cleaner = cleaner(p)) {
259 >            try {
260 >                p.execute(null);
261 >                shouldThrow();
262 >            } catch (NullPointerException success) {}
263 >        }
264      }
265  
266      /**
267       * schedule(null) throws NPE
268       */
269      public void testScheduleNull() throws InterruptedException {
270 <        CustomExecutor se = new CustomExecutor(1);
271 <        try {
272 <            TrackedCallable callable = null;
273 <            Future f = se.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
274 <            shouldThrow();
275 <        } catch (NullPointerException success) {}
276 <        joinPool(se);
270 >        final CustomExecutor p = new CustomExecutor(1);
271 >        try (PoolCleaner cleaner = cleaner(p)) {
272 >            try {
273 >                TrackedCallable callable = null;
274 >                Future f = p.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
275 >                shouldThrow();
276 >            } catch (NullPointerException success) {}
277 >        }
278      }
279  
280      /**
281       * execute throws RejectedExecutionException if shutdown
282       */
283      public void testSchedule1_RejectedExecutionException() {
284 <        CustomExecutor se = new CustomExecutor(1);
285 <        try {
286 <            se.shutdown();
287 <            se.schedule(new NoOpRunnable(),
288 <                        MEDIUM_DELAY_MS, MILLISECONDS);
289 <            shouldThrow();
290 <        } catch (RejectedExecutionException success) {
291 <        } catch (SecurityException ok) {
284 >        final CustomExecutor p = new CustomExecutor(1);
285 >        try (PoolCleaner cleaner = cleaner(p)) {
286 >            try {
287 >                p.shutdown();
288 >                p.schedule(new NoOpRunnable(),
289 >                           MEDIUM_DELAY_MS, MILLISECONDS);
290 >                shouldThrow();
291 >            } catch (RejectedExecutionException success) {
292 >            } catch (SecurityException ok) {}
293          }
308
309        joinPool(se);
294      }
295  
296      /**
297       * schedule throws RejectedExecutionException if shutdown
298       */
299      public void testSchedule2_RejectedExecutionException() {
300 <        CustomExecutor se = new CustomExecutor(1);
301 <        try {
302 <            se.shutdown();
303 <            se.schedule(new NoOpCallable(),
304 <                        MEDIUM_DELAY_MS, MILLISECONDS);
305 <            shouldThrow();
306 <        } catch (RejectedExecutionException success) {
307 <        } catch (SecurityException ok) {
300 >        final CustomExecutor p = new CustomExecutor(1);
301 >        try (PoolCleaner cleaner = cleaner(p)) {
302 >            try {
303 >                p.shutdown();
304 >                p.schedule(new NoOpCallable(),
305 >                           MEDIUM_DELAY_MS, MILLISECONDS);
306 >                shouldThrow();
307 >            } catch (RejectedExecutionException success) {
308 >            } catch (SecurityException ok) {}
309          }
325        joinPool(se);
310      }
311  
312      /**
313       * schedule callable throws RejectedExecutionException if shutdown
314       */
315      public void testSchedule3_RejectedExecutionException() {
316 <        CustomExecutor se = new CustomExecutor(1);
317 <        try {
318 <            se.shutdown();
319 <            se.schedule(new NoOpCallable(),
320 <                        MEDIUM_DELAY_MS, MILLISECONDS);
321 <            shouldThrow();
322 <        } catch (RejectedExecutionException success) {
323 <        } catch (SecurityException ok) {
316 >        final CustomExecutor p = new CustomExecutor(1);
317 >        try (PoolCleaner cleaner = cleaner(p)) {
318 >            try {
319 >                p.shutdown();
320 >                p.schedule(new NoOpCallable(),
321 >                           MEDIUM_DELAY_MS, MILLISECONDS);
322 >                shouldThrow();
323 >            } catch (RejectedExecutionException success) {
324 >            } catch (SecurityException ok) {}
325          }
341        joinPool(se);
326      }
327  
328      /**
329       * scheduleAtFixedRate throws RejectedExecutionException if shutdown
330       */
331      public void testScheduleAtFixedRate1_RejectedExecutionException() {
332 <        CustomExecutor se = new CustomExecutor(1);
333 <        try {
334 <            se.shutdown();
335 <            se.scheduleAtFixedRate(new NoOpRunnable(),
336 <                                   MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
337 <            shouldThrow();
338 <        } catch (RejectedExecutionException success) {
339 <        } catch (SecurityException ok) {
332 >        final CustomExecutor p = new CustomExecutor(1);
333 >        try (PoolCleaner cleaner = cleaner(p)) {
334 >            try {
335 >                p.shutdown();
336 >                p.scheduleAtFixedRate(new NoOpRunnable(),
337 >                                      MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
338 >                shouldThrow();
339 >            } catch (RejectedExecutionException success) {
340 >            } catch (SecurityException ok) {}
341          }
357        joinPool(se);
342      }
343  
344      /**
345       * scheduleWithFixedDelay throws RejectedExecutionException if shutdown
346       */
347      public void testScheduleWithFixedDelay1_RejectedExecutionException() {
348 <        CustomExecutor se = new CustomExecutor(1);
349 <        try {
350 <            se.shutdown();
351 <            se.scheduleWithFixedDelay(new NoOpRunnable(),
352 <                                      MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
353 <            shouldThrow();
354 <        } catch (RejectedExecutionException success) {
355 <        } catch (SecurityException ok) {
348 >        final CustomExecutor p = new CustomExecutor(1);
349 >        try (PoolCleaner cleaner = cleaner(p)) {
350 >            try {
351 >                p.shutdown();
352 >                p.scheduleWithFixedDelay(new NoOpRunnable(),
353 >                                         MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
354 >                shouldThrow();
355 >            } catch (RejectedExecutionException success) {
356 >            } catch (SecurityException ok) {}
357          }
373        joinPool(se);
358      }
359  
360      /**
# Line 378 | Line 362 | public class ScheduledExecutorSubclassTe
362       * thread becomes active
363       */
364      public void testGetActiveCount() throws InterruptedException {
381        final ThreadPoolExecutor p = new CustomExecutor(2);
382        final CountDownLatch threadStarted = new CountDownLatch(1);
365          final CountDownLatch done = new CountDownLatch(1);
366 <        try {
366 >        final ThreadPoolExecutor p = new CustomExecutor(2);
367 >        try (PoolCleaner cleaner = cleaner(p, done)) {
368 >            final CountDownLatch threadStarted = new CountDownLatch(1);
369              assertEquals(0, p.getActiveCount());
370              p.execute(new CheckedRunnable() {
371                  public void realRun() throws InterruptedException {
372                      threadStarted.countDown();
373                      assertEquals(1, p.getActiveCount());
374 <                    done.await();
374 >                    await(done);
375                  }});
376 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
376 >            await(threadStarted);
377              assertEquals(1, p.getActiveCount());
394        } finally {
395            done.countDown();
396            joinPool(p);
378          }
379      }
380  
# Line 403 | Line 384 | public class ScheduledExecutorSubclassTe
384       */
385      public void testGetCompletedTaskCount() throws InterruptedException {
386          final ThreadPoolExecutor p = new CustomExecutor(2);
387 <        final CountDownLatch threadStarted = new CountDownLatch(1);
388 <        final CountDownLatch threadProceed = new CountDownLatch(1);
389 <        final CountDownLatch threadDone = new CountDownLatch(1);
390 <        try {
387 >        try (PoolCleaner cleaner = cleaner(p)) {
388 >            final CountDownLatch threadStarted = new CountDownLatch(1);
389 >            final CountDownLatch threadProceed = new CountDownLatch(1);
390 >            final CountDownLatch threadDone = new CountDownLatch(1);
391              assertEquals(0, p.getCompletedTaskCount());
392              p.execute(new CheckedRunnable() {
393                  public void realRun() throws InterruptedException {
# Line 425 | Line 406 | public class ScheduledExecutorSubclassTe
406                      fail("timed out");
407                  Thread.yield();
408              }
428        } finally {
429            joinPool(p);
409          }
410      }
411  
# Line 434 | Line 413 | public class ScheduledExecutorSubclassTe
413       * getCorePoolSize returns size given in constructor if not otherwise set
414       */
415      public void testGetCorePoolSize() {
416 <        CustomExecutor p = new CustomExecutor(1);
417 <        assertEquals(1, p.getCorePoolSize());
418 <        joinPool(p);
416 >        final CustomExecutor p = new CustomExecutor(1);
417 >        try (PoolCleaner cleaner = cleaner(p)) {
418 >            assertEquals(1, p.getCorePoolSize());
419 >        }
420      }
421  
422      /**
# Line 445 | Line 425 | public class ScheduledExecutorSubclassTe
425       */
426      public void testGetLargestPoolSize() throws InterruptedException {
427          final int THREADS = 3;
448        final ThreadPoolExecutor p = new CustomExecutor(THREADS);
449        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
428          final CountDownLatch done = new CountDownLatch(1);
429 <        try {
429 >        final ThreadPoolExecutor p = new CustomExecutor(THREADS);
430 >        try (PoolCleaner cleaner = cleaner(p, done)) {
431 >            final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
432              assertEquals(0, p.getLargestPoolSize());
433              for (int i = 0; i < THREADS; i++)
434                  p.execute(new CheckedRunnable() {
435                      public void realRun() throws InterruptedException {
436                          threadsStarted.countDown();
437 <                        done.await();
437 >                        await(done);
438                          assertEquals(THREADS, p.getLargestPoolSize());
439                      }});
440 <            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
461 <            assertEquals(THREADS, p.getLargestPoolSize());
462 <        } finally {
463 <            done.countDown();
464 <            joinPool(p);
440 >            await(threadsStarted);
441              assertEquals(THREADS, p.getLargestPoolSize());
442          }
443 +        assertEquals(THREADS, p.getLargestPoolSize());
444      }
445  
446      /**
# Line 471 | Line 448 | public class ScheduledExecutorSubclassTe
448       * become active
449       */
450      public void testGetPoolSize() throws InterruptedException {
474        final ThreadPoolExecutor p = new CustomExecutor(1);
475        final CountDownLatch threadStarted = new CountDownLatch(1);
451          final CountDownLatch done = new CountDownLatch(1);
452 <        try {
452 >        final ThreadPoolExecutor p = new CustomExecutor(1);
453 >        try (PoolCleaner cleaner = cleaner(p, done)) {
454 >            final CountDownLatch threadStarted = new CountDownLatch(1);
455              assertEquals(0, p.getPoolSize());
456              p.execute(new CheckedRunnable() {
457                  public void realRun() throws InterruptedException {
458                      threadStarted.countDown();
459                      assertEquals(1, p.getPoolSize());
460 <                    done.await();
460 >                    await(done);
461                  }});
462 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
462 >            await(threadStarted);
463              assertEquals(1, p.getPoolSize());
487        } finally {
488            done.countDown();
489            joinPool(p);
464          }
465      }
466  
# Line 495 | Line 469 | public class ScheduledExecutorSubclassTe
469       * submitted
470       */
471      public void testGetTaskCount() throws InterruptedException {
472 <        final ThreadPoolExecutor p = new CustomExecutor(1);
499 <        final CountDownLatch threadStarted = new CountDownLatch(1);
472 >        final int TASKS = 3;
473          final CountDownLatch done = new CountDownLatch(1);
474 <        final int TASKS = 5;
475 <        try {
474 >        final ThreadPoolExecutor p = new CustomExecutor(1);
475 >        try (PoolCleaner cleaner = cleaner(p, done)) {
476 >            final CountDownLatch threadStarted = new CountDownLatch(1);
477              assertEquals(0, p.getTaskCount());
478 <            for (int i = 0; i < TASKS; i++)
478 >            assertEquals(0, p.getCompletedTaskCount());
479 >            p.execute(new CheckedRunnable() {
480 >                public void realRun() throws InterruptedException {
481 >                    threadStarted.countDown();
482 >                    await(done);
483 >                }});
484 >            await(threadStarted);
485 >            assertEquals(1, p.getTaskCount());
486 >            assertEquals(0, p.getCompletedTaskCount());
487 >            for (int i = 0; i < TASKS; i++) {
488 >                assertEquals(1 + i, p.getTaskCount());
489                  p.execute(new CheckedRunnable() {
490                      public void realRun() throws InterruptedException {
491                          threadStarted.countDown();
492 <                        done.await();
492 >                        assertEquals(1 + TASKS, p.getTaskCount());
493 >                        await(done);
494                      }});
495 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
496 <            assertEquals(TASKS, p.getTaskCount());
497 <        } finally {
513 <            done.countDown();
514 <            joinPool(p);
495 >            }
496 >            assertEquals(1 + TASKS, p.getTaskCount());
497 >            assertEquals(0, p.getCompletedTaskCount());
498          }
499 +        assertEquals(1 + TASKS, p.getTaskCount());
500 +        assertEquals(1 + TASKS, p.getCompletedTaskCount());
501      }
502  
503      /**
504       * getThreadFactory returns factory in constructor if not set
505       */
506      public void testGetThreadFactory() {
507 <        ThreadFactory tf = new SimpleThreadFactory();
508 <        CustomExecutor p = new CustomExecutor(1, tf);
509 <        assertSame(tf, p.getThreadFactory());
510 <        joinPool(p);
507 >        final ThreadFactory threadFactory = new SimpleThreadFactory();
508 >        final CustomExecutor p = new CustomExecutor(1, threadFactory);
509 >        try (PoolCleaner cleaner = cleaner(p)) {
510 >            assertSame(threadFactory, p.getThreadFactory());
511 >        }
512      }
513  
514      /**
515       * setThreadFactory sets the thread factory returned by getThreadFactory
516       */
517      public void testSetThreadFactory() {
518 <        ThreadFactory tf = new SimpleThreadFactory();
519 <        CustomExecutor p = new CustomExecutor(1);
520 <        p.setThreadFactory(tf);
521 <        assertSame(tf, p.getThreadFactory());
522 <        joinPool(p);
518 >        final ThreadFactory threadFactory = new SimpleThreadFactory();
519 >        final CustomExecutor p = new CustomExecutor(1);
520 >        try (PoolCleaner cleaner = cleaner(p)) {
521 >            p.setThreadFactory(threadFactory);
522 >            assertSame(threadFactory, p.getThreadFactory());
523 >        }
524      }
525  
526      /**
527       * setThreadFactory(null) throws NPE
528       */
529      public void testSetThreadFactoryNull() {
530 <        CustomExecutor p = new CustomExecutor(1);
531 <        try {
532 <            p.setThreadFactory(null);
533 <            shouldThrow();
534 <        } catch (NullPointerException success) {
535 <        } finally {
549 <            joinPool(p);
530 >        final CustomExecutor p = new CustomExecutor(1);
531 >        try (PoolCleaner cleaner = cleaner(p)) {
532 >            try {
533 >                p.setThreadFactory(null);
534 >                shouldThrow();
535 >            } catch (NullPointerException success) {}
536          }
537      }
538  
# Line 554 | Line 540 | public class ScheduledExecutorSubclassTe
540       * isShutdown is false before shutdown, true after
541       */
542      public void testIsShutdown() {
543 <        CustomExecutor p = new CustomExecutor(1);
544 <        try {
543 >        final CustomExecutor p = new CustomExecutor(1);
544 >        try (PoolCleaner cleaner = cleaner(p)) {
545              assertFalse(p.isShutdown());
560        }
561        finally {
546              try { p.shutdown(); } catch (SecurityException ok) { return; }
547 +            assertTrue(p.isShutdown());
548          }
564        assertTrue(p.isShutdown());
549      }
550  
551      /**
552       * isTerminated is false before termination, true after
553       */
554      public void testIsTerminated() throws InterruptedException {
571        final ThreadPoolExecutor p = new CustomExecutor(1);
572        final CountDownLatch threadStarted = new CountDownLatch(1);
555          final CountDownLatch done = new CountDownLatch(1);
556 <        assertFalse(p.isTerminated());
557 <        try {
556 >        final ThreadPoolExecutor p = new CustomExecutor(1);
557 >        try (PoolCleaner cleaner = cleaner(p)) {
558 >            final CountDownLatch threadStarted = new CountDownLatch(1);
559              p.execute(new CheckedRunnable() {
560                  public void realRun() throws InterruptedException {
561                      assertFalse(p.isTerminated());
562                      threadStarted.countDown();
563 <                    done.await();
563 >                    await(done);
564                  }});
565 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
565 >            await(threadStarted);
566 >            assertFalse(p.isTerminated());
567              assertFalse(p.isTerminating());
568              done.countDown();
585        } finally {
569              try { p.shutdown(); } catch (SecurityException ok) { return; }
570 +            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
571 +            assertTrue(p.isTerminated());
572          }
588        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
589        assertTrue(p.isTerminated());
573      }
574  
575      /**
576       * isTerminating is not true when running or when terminated
577       */
578      public void testIsTerminating() throws InterruptedException {
596        final ThreadPoolExecutor p = new CustomExecutor(1);
597        final CountDownLatch threadStarted = new CountDownLatch(1);
579          final CountDownLatch done = new CountDownLatch(1);
580 <        try {
580 >        final ThreadPoolExecutor p = new CustomExecutor(1);
581 >        try (PoolCleaner cleaner = cleaner(p)) {
582 >            final CountDownLatch threadStarted = new CountDownLatch(1);
583              assertFalse(p.isTerminating());
584              p.execute(new CheckedRunnable() {
585                  public void realRun() throws InterruptedException {
586                      assertFalse(p.isTerminating());
587                      threadStarted.countDown();
588 <                    done.await();
588 >                    await(done);
589                  }});
590 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
590 >            await(threadStarted);
591              assertFalse(p.isTerminating());
592              done.countDown();
610        } finally {
593              try { p.shutdown(); } catch (SecurityException ok) { return; }
594 +            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
595 +            assertTrue(p.isTerminated());
596 +            assertFalse(p.isTerminating());
597          }
613        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
614        assertTrue(p.isTerminated());
615        assertFalse(p.isTerminating());
598      }
599  
600      /**
601       * getQueue returns the work queue, which contains queued tasks
602       */
603      public void testGetQueue() throws InterruptedException {
622        ScheduledThreadPoolExecutor p = new CustomExecutor(1);
623        final CountDownLatch threadStarted = new CountDownLatch(1);
604          final CountDownLatch done = new CountDownLatch(1);
605 <        try {
605 >        final ScheduledThreadPoolExecutor p = new CustomExecutor(1);
606 >        try (PoolCleaner cleaner = cleaner(p, done)) {
607 >            final CountDownLatch threadStarted = new CountDownLatch(1);
608              ScheduledFuture[] tasks = new ScheduledFuture[5];
609              for (int i = 0; i < tasks.length; i++) {
610                  Runnable r = new CheckedRunnable() {
611                      public void realRun() throws InterruptedException {
612                          threadStarted.countDown();
613 <                        done.await();
613 >                        await(done);
614                      }};
615                  tasks[i] = p.schedule(r, 1, MILLISECONDS);
616              }
617 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
617 >            await(threadStarted);
618              BlockingQueue<Runnable> q = p.getQueue();
619              assertTrue(q.contains(tasks[tasks.length - 1]));
620              assertFalse(q.contains(tasks[0]));
639        } finally {
640            done.countDown();
641            joinPool(p);
621          }
622      }
623  
# Line 646 | Line 625 | public class ScheduledExecutorSubclassTe
625       * remove(task) removes queued task, and fails to remove active task
626       */
627      public void testRemove() throws InterruptedException {
649        final ScheduledThreadPoolExecutor p = new CustomExecutor(1);
650        ScheduledFuture[] tasks = new ScheduledFuture[5];
651        final CountDownLatch threadStarted = new CountDownLatch(1);
628          final CountDownLatch done = new CountDownLatch(1);
629 <        try {
629 >        final ScheduledThreadPoolExecutor p = new CustomExecutor(1);
630 >        try (PoolCleaner cleaner = cleaner(p, done)) {
631 >            ScheduledFuture[] tasks = new ScheduledFuture[5];
632 >            final CountDownLatch threadStarted = new CountDownLatch(1);
633              for (int i = 0; i < tasks.length; i++) {
634                  Runnable r = new CheckedRunnable() {
635                      public void realRun() throws InterruptedException {
636                          threadStarted.countDown();
637 <                        done.await();
637 >                        await(done);
638                      }};
639                  tasks[i] = p.schedule(r, 1, MILLISECONDS);
640              }
641 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
641 >            await(threadStarted);
642              BlockingQueue<Runnable> q = p.getQueue();
643              assertFalse(p.remove((Runnable)tasks[0]));
644              assertTrue(q.contains((Runnable)tasks[4]));
# Line 670 | Line 649 | public class ScheduledExecutorSubclassTe
649              assertTrue(q.contains((Runnable)tasks[3]));
650              assertTrue(p.remove((Runnable)tasks[3]));
651              assertFalse(q.contains((Runnable)tasks[3]));
673        } finally {
674            done.countDown();
675            joinPool(p);
652          }
653      }
654  
# Line 680 | Line 656 | public class ScheduledExecutorSubclassTe
656       * purge removes cancelled tasks from the queue
657       */
658      public void testPurge() throws InterruptedException {
659 <        CustomExecutor p = new CustomExecutor(1);
660 <        ScheduledFuture[] tasks = new ScheduledFuture[5];
661 <        for (int i = 0; i < tasks.length; i++)
662 <            tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(),
663 <                                  LONG_DELAY_MS, MILLISECONDS);
664 <        try {
659 >        final ScheduledFuture[] tasks = new ScheduledFuture[5];
660 >        final Runnable releaser = new Runnable() { public void run() {
661 >            for (ScheduledFuture task : tasks)
662 >                if (task != null) task.cancel(true); }};
663 >        final CustomExecutor p = new CustomExecutor(1);
664 >        try (PoolCleaner cleaner = cleaner(p, releaser)) {
665 >            for (int i = 0; i < tasks.length; i++)
666 >                tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(),
667 >                                      LONG_DELAY_MS, MILLISECONDS);
668              int max = tasks.length;
669              if (tasks[4].cancel(true)) --max;
670              if (tasks[3].cancel(true)) --max;
# Line 697 | Line 676 | public class ScheduledExecutorSubclassTe
676                  long count = p.getTaskCount();
677                  if (count == max)
678                      return;
679 <            } while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
679 >            } while (millisElapsedSince(startTime) < LONG_DELAY_MS);
680              fail("Purge failed to remove cancelled tasks");
702        } finally {
703            for (ScheduledFuture task : tasks)
704                task.cancel(true);
705            joinPool(p);
681          }
682      }
683  
# Line 715 | Line 690 | public class ScheduledExecutorSubclassTe
690          final int count = 5;
691          final AtomicInteger ran = new AtomicInteger(0);
692          final CustomExecutor p = new CustomExecutor(poolSize);
693 <        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
693 >        final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
694          Runnable waiter = new CheckedRunnable() { public void realRun() {
695              threadsStarted.countDown();
696              try {
# Line 725 | Line 700 | public class ScheduledExecutorSubclassTe
700          }};
701          for (int i = 0; i < count; i++)
702              p.execute(waiter);
703 <        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
703 >        await(threadsStarted);
704          assertEquals(poolSize, p.getActiveCount());
705          assertEquals(0, p.getCompletedTaskCount());
706          final List<Runnable> queuedTasks;
# Line 748 | Line 723 | public class ScheduledExecutorSubclassTe
723       * and those tasks are drained from the queue
724       */
725      public void testShutdownNow_delayedTasks() throws InterruptedException {
726 <        CustomExecutor p = new CustomExecutor(1);
726 >        final CustomExecutor p = new CustomExecutor(1);
727          List<ScheduledFuture> tasks = new ArrayList<>();
728          for (int i = 0; i < 3; i++) {
729              Runnable r = new NoOpRunnable();
# Line 875 | Line 850 | public class ScheduledExecutorSubclassTe
850       * completed submit of callable returns result
851       */
852      public void testSubmitCallable() throws Exception {
853 <        ExecutorService e = new CustomExecutor(2);
854 <        try {
853 >        final ExecutorService e = new CustomExecutor(2);
854 >        try (PoolCleaner cleaner = cleaner(e)) {
855              Future<String> future = e.submit(new StringTask());
856              String result = future.get();
857              assertSame(TEST_STRING, result);
883        } finally {
884            joinPool(e);
858          }
859      }
860  
# Line 889 | Line 862 | public class ScheduledExecutorSubclassTe
862       * completed submit of runnable returns successfully
863       */
864      public void testSubmitRunnable() throws Exception {
865 <        ExecutorService e = new CustomExecutor(2);
866 <        try {
865 >        final ExecutorService e = new CustomExecutor(2);
866 >        try (PoolCleaner cleaner = cleaner(e)) {
867              Future<?> future = e.submit(new NoOpRunnable());
868              future.get();
869              assertTrue(future.isDone());
897        } finally {
898            joinPool(e);
870          }
871      }
872  
# Line 903 | Line 874 | public class ScheduledExecutorSubclassTe
874       * completed submit of (runnable, result) returns result
875       */
876      public void testSubmitRunnable2() throws Exception {
877 <        ExecutorService e = new CustomExecutor(2);
878 <        try {
877 >        final ExecutorService e = new CustomExecutor(2);
878 >        try (PoolCleaner cleaner = cleaner(e)) {
879              Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
880              String result = future.get();
881              assertSame(TEST_STRING, result);
911        } finally {
912            joinPool(e);
882          }
883      }
884  
# Line 917 | Line 886 | public class ScheduledExecutorSubclassTe
886       * invokeAny(null) throws NPE
887       */
888      public void testInvokeAny1() throws Exception {
889 <        ExecutorService e = new CustomExecutor(2);
890 <        try {
891 <            e.invokeAny(null);
892 <            shouldThrow();
893 <        } catch (NullPointerException success) {
894 <        } finally {
926 <            joinPool(e);
889 >        final ExecutorService e = new CustomExecutor(2);
890 >        try (PoolCleaner cleaner = cleaner(e)) {
891 >            try {
892 >                e.invokeAny(null);
893 >                shouldThrow();
894 >            } catch (NullPointerException success) {}
895          }
896      }
897  
# Line 931 | Line 899 | public class ScheduledExecutorSubclassTe
899       * invokeAny(empty collection) throws IAE
900       */
901      public void testInvokeAny2() throws Exception {
902 <        ExecutorService e = new CustomExecutor(2);
903 <        try {
904 <            e.invokeAny(new ArrayList<Callable<String>>());
905 <            shouldThrow();
906 <        } catch (IllegalArgumentException success) {
907 <        } finally {
940 <            joinPool(e);
902 >        final ExecutorService e = new CustomExecutor(2);
903 >        try (PoolCleaner cleaner = cleaner(e)) {
904 >            try {
905 >                e.invokeAny(new ArrayList<Callable<String>>());
906 >                shouldThrow();
907 >            } catch (IllegalArgumentException success) {}
908          }
909      }
910  
# Line 945 | Line 912 | public class ScheduledExecutorSubclassTe
912       * invokeAny(c) throws NPE if c has null elements
913       */
914      public void testInvokeAny3() throws Exception {
915 <        CountDownLatch latch = new CountDownLatch(1);
916 <        ExecutorService e = new CustomExecutor(2);
917 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
918 <        l.add(latchAwaitingStringTask(latch));
919 <        l.add(null);
920 <        try {
921 <            e.invokeAny(l);
922 <            shouldThrow();
923 <        } catch (NullPointerException success) {
924 <        } finally {
915 >        final CountDownLatch latch = new CountDownLatch(1);
916 >        final ExecutorService e = new CustomExecutor(2);
917 >        try (PoolCleaner cleaner = cleaner(e)) {
918 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
919 >            l.add(latchAwaitingStringTask(latch));
920 >            l.add(null);
921 >            try {
922 >                e.invokeAny(l);
923 >                shouldThrow();
924 >            } catch (NullPointerException success) {}
925              latch.countDown();
959            joinPool(e);
926          }
927      }
928  
# Line 964 | Line 930 | public class ScheduledExecutorSubclassTe
930       * invokeAny(c) throws ExecutionException if no task completes
931       */
932      public void testInvokeAny4() throws Exception {
933 <        ExecutorService e = new CustomExecutor(2);
934 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
935 <        l.add(new NPETask());
936 <        try {
937 <            e.invokeAny(l);
938 <            shouldThrow();
939 <        } catch (ExecutionException success) {
940 <            assertTrue(success.getCause() instanceof NullPointerException);
941 <        } finally {
942 <            joinPool(e);
933 >        final ExecutorService e = new CustomExecutor(2);
934 >        try (PoolCleaner cleaner = cleaner(e)) {
935 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
936 >            l.add(new NPETask());
937 >            try {
938 >                e.invokeAny(l);
939 >                shouldThrow();
940 >            } catch (ExecutionException success) {
941 >                assertTrue(success.getCause() instanceof NullPointerException);
942 >            }
943          }
944      }
945  
# Line 981 | Line 947 | public class ScheduledExecutorSubclassTe
947       * invokeAny(c) returns result of some task
948       */
949      public void testInvokeAny5() throws Exception {
950 <        ExecutorService e = new CustomExecutor(2);
951 <        try {
950 >        final ExecutorService e = new CustomExecutor(2);
951 >        try (PoolCleaner cleaner = cleaner(e)) {
952              List<Callable<String>> l = new ArrayList<Callable<String>>();
953              l.add(new StringTask());
954              l.add(new StringTask());
955              String result = e.invokeAny(l);
956              assertSame(TEST_STRING, result);
991        } finally {
992            joinPool(e);
957          }
958      }
959  
# Line 997 | Line 961 | public class ScheduledExecutorSubclassTe
961       * invokeAll(null) throws NPE
962       */
963      public void testInvokeAll1() throws Exception {
964 <        ExecutorService e = new CustomExecutor(2);
965 <        try {
966 <            e.invokeAll(null);
967 <            shouldThrow();
968 <        } catch (NullPointerException success) {
969 <        } finally {
1006 <            joinPool(e);
964 >        final ExecutorService e = new CustomExecutor(2);
965 >        try (PoolCleaner cleaner = cleaner(e)) {
966 >            try {
967 >                e.invokeAll(null);
968 >                shouldThrow();
969 >            } catch (NullPointerException success) {}
970          }
971      }
972  
# Line 1011 | Line 974 | public class ScheduledExecutorSubclassTe
974       * invokeAll(empty collection) returns empty collection
975       */
976      public void testInvokeAll2() throws Exception {
977 <        ExecutorService e = new CustomExecutor(2);
978 <        try {
977 >        final ExecutorService e = new CustomExecutor(2);
978 >        try (PoolCleaner cleaner = cleaner(e)) {
979              List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
980              assertTrue(r.isEmpty());
1018        } finally {
1019            joinPool(e);
981          }
982      }
983  
# Line 1024 | Line 985 | public class ScheduledExecutorSubclassTe
985       * invokeAll(c) throws NPE if c has null elements
986       */
987      public void testInvokeAll3() throws Exception {
988 <        ExecutorService e = new CustomExecutor(2);
989 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
990 <        l.add(new StringTask());
991 <        l.add(null);
992 <        try {
993 <            e.invokeAll(l);
994 <            shouldThrow();
995 <        } catch (NullPointerException success) {
996 <        } finally {
1036 <            joinPool(e);
988 >        final ExecutorService e = new CustomExecutor(2);
989 >        try (PoolCleaner cleaner = cleaner(e)) {
990 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
991 >            l.add(new StringTask());
992 >            l.add(null);
993 >            try {
994 >                e.invokeAll(l);
995 >                shouldThrow();
996 >            } catch (NullPointerException success) {}
997          }
998      }
999  
# Line 1041 | Line 1001 | public class ScheduledExecutorSubclassTe
1001       * get of invokeAll(c) throws exception on failed task
1002       */
1003      public void testInvokeAll4() throws Exception {
1004 <        ExecutorService e = new CustomExecutor(2);
1005 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1006 <        l.add(new NPETask());
1007 <        List<Future<String>> futures = e.invokeAll(l);
1008 <        assertEquals(1, futures.size());
1009 <        try {
1010 <            futures.get(0).get();
1011 <            shouldThrow();
1012 <        } catch (ExecutionException success) {
1013 <            assertTrue(success.getCause() instanceof NullPointerException);
1014 <        } finally {
1015 <            joinPool(e);
1004 >        final ExecutorService e = new CustomExecutor(2);
1005 >        try (PoolCleaner cleaner = cleaner(e)) {
1006 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1007 >            l.add(new NPETask());
1008 >            List<Future<String>> futures = e.invokeAll(l);
1009 >            assertEquals(1, futures.size());
1010 >            try {
1011 >                futures.get(0).get();
1012 >                shouldThrow();
1013 >            } catch (ExecutionException success) {
1014 >                assertTrue(success.getCause() instanceof NullPointerException);
1015 >            }
1016          }
1017      }
1018  
# Line 1060 | Line 1020 | public class ScheduledExecutorSubclassTe
1020       * invokeAll(c) returns results of all completed tasks
1021       */
1022      public void testInvokeAll5() throws Exception {
1023 <        ExecutorService e = new CustomExecutor(2);
1024 <        try {
1023 >        final ExecutorService e = new CustomExecutor(2);
1024 >        try (PoolCleaner cleaner = cleaner(e)) {
1025              List<Callable<String>> l = new ArrayList<Callable<String>>();
1026              l.add(new StringTask());
1027              l.add(new StringTask());
# Line 1069 | Line 1029 | public class ScheduledExecutorSubclassTe
1029              assertEquals(2, futures.size());
1030              for (Future<String> future : futures)
1031                  assertSame(TEST_STRING, future.get());
1072        } finally {
1073            joinPool(e);
1032          }
1033      }
1034  
# Line 1078 | Line 1036 | public class ScheduledExecutorSubclassTe
1036       * timed invokeAny(null) throws NPE
1037       */
1038      public void testTimedInvokeAny1() throws Exception {
1039 <        ExecutorService e = new CustomExecutor(2);
1040 <        try {
1041 <            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1042 <            shouldThrow();
1043 <        } catch (NullPointerException success) {
1044 <        } finally {
1087 <            joinPool(e);
1039 >        final ExecutorService e = new CustomExecutor(2);
1040 >        try (PoolCleaner cleaner = cleaner(e)) {
1041 >            try {
1042 >                e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1043 >                shouldThrow();
1044 >            } catch (NullPointerException success) {}
1045          }
1046      }
1047  
# Line 1092 | Line 1049 | public class ScheduledExecutorSubclassTe
1049       * timed invokeAny(,,null) throws NPE
1050       */
1051      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1052 <        ExecutorService e = new CustomExecutor(2);
1053 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1054 <        l.add(new StringTask());
1055 <        try {
1056 <            e.invokeAny(l, MEDIUM_DELAY_MS, null);
1057 <            shouldThrow();
1058 <        } catch (NullPointerException success) {
1059 <        } finally {
1103 <            joinPool(e);
1052 >        final ExecutorService e = new CustomExecutor(2);
1053 >        try (PoolCleaner cleaner = cleaner(e)) {
1054 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1055 >            l.add(new StringTask());
1056 >            try {
1057 >                e.invokeAny(l, MEDIUM_DELAY_MS, null);
1058 >                shouldThrow();
1059 >            } catch (NullPointerException success) {}
1060          }
1061      }
1062  
# Line 1108 | Line 1064 | public class ScheduledExecutorSubclassTe
1064       * timed invokeAny(empty collection) throws IAE
1065       */
1066      public void testTimedInvokeAny2() throws Exception {
1067 <        ExecutorService e = new CustomExecutor(2);
1068 <        try {
1069 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1070 <            shouldThrow();
1071 <        } catch (IllegalArgumentException success) {
1072 <        } finally {
1117 <            joinPool(e);
1067 >        final ExecutorService e = new CustomExecutor(2);
1068 >        try (PoolCleaner cleaner = cleaner(e)) {
1069 >            try {
1070 >                e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1071 >                shouldThrow();
1072 >            } catch (IllegalArgumentException success) {}
1073          }
1074      }
1075  
# Line 1123 | Line 1078 | public class ScheduledExecutorSubclassTe
1078       */
1079      public void testTimedInvokeAny3() throws Exception {
1080          CountDownLatch latch = new CountDownLatch(1);
1081 <        ExecutorService e = new CustomExecutor(2);
1082 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1083 <        l.add(latchAwaitingStringTask(latch));
1084 <        l.add(null);
1085 <        try {
1086 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1087 <            shouldThrow();
1088 <        } catch (NullPointerException success) {
1089 <        } finally {
1081 >        final ExecutorService e = new CustomExecutor(2);
1082 >        try (PoolCleaner cleaner = cleaner(e)) {
1083 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1084 >            l.add(latchAwaitingStringTask(latch));
1085 >            l.add(null);
1086 >            try {
1087 >                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1088 >                shouldThrow();
1089 >            } catch (NullPointerException success) {}
1090              latch.countDown();
1136            joinPool(e);
1091          }
1092      }
1093  
# Line 1141 | Line 1095 | public class ScheduledExecutorSubclassTe
1095       * timed invokeAny(c) throws ExecutionException if no task completes
1096       */
1097      public void testTimedInvokeAny4() throws Exception {
1098 <        ExecutorService e = new CustomExecutor(2);
1099 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1100 <        l.add(new NPETask());
1101 <        try {
1102 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1103 <            shouldThrow();
1104 <        } catch (ExecutionException success) {
1105 <            assertTrue(success.getCause() instanceof NullPointerException);
1106 <        } finally {
1107 <            joinPool(e);
1098 >        final ExecutorService e = new CustomExecutor(2);
1099 >        try (PoolCleaner cleaner = cleaner(e)) {
1100 >            long startTime = System.nanoTime();
1101 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1102 >            l.add(new NPETask());
1103 >            try {
1104 >                e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1105 >                shouldThrow();
1106 >            } catch (ExecutionException success) {
1107 >                assertTrue(success.getCause() instanceof NullPointerException);
1108 >            }
1109 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1110          }
1111      }
1112  
# Line 1158 | Line 1114 | public class ScheduledExecutorSubclassTe
1114       * timed invokeAny(c) returns result of some task
1115       */
1116      public void testTimedInvokeAny5() throws Exception {
1117 <        ExecutorService e = new CustomExecutor(2);
1118 <        try {
1117 >        final ExecutorService e = new CustomExecutor(2);
1118 >        try (PoolCleaner cleaner = cleaner(e)) {
1119 >            long startTime = System.nanoTime();
1120              List<Callable<String>> l = new ArrayList<Callable<String>>();
1121              l.add(new StringTask());
1122              l.add(new StringTask());
1123 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1123 >            String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1124              assertSame(TEST_STRING, result);
1125 <        } finally {
1169 <            joinPool(e);
1125 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1126          }
1127      }
1128  
# Line 1174 | Line 1130 | public class ScheduledExecutorSubclassTe
1130       * timed invokeAll(null) throws NPE
1131       */
1132      public void testTimedInvokeAll1() throws Exception {
1133 <        ExecutorService e = new CustomExecutor(2);
1134 <        try {
1135 <            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1136 <            shouldThrow();
1137 <        } catch (NullPointerException success) {
1138 <        } finally {
1183 <            joinPool(e);
1133 >        final ExecutorService e = new CustomExecutor(2);
1134 >        try (PoolCleaner cleaner = cleaner(e)) {
1135 >            try {
1136 >                e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1137 >                shouldThrow();
1138 >            } catch (NullPointerException success) {}
1139          }
1140      }
1141  
# Line 1188 | Line 1143 | public class ScheduledExecutorSubclassTe
1143       * timed invokeAll(,,null) throws NPE
1144       */
1145      public void testTimedInvokeAllNullTimeUnit() throws Exception {
1146 <        ExecutorService e = new CustomExecutor(2);
1147 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1148 <        l.add(new StringTask());
1149 <        try {
1150 <            e.invokeAll(l, MEDIUM_DELAY_MS, null);
1151 <            shouldThrow();
1152 <        } catch (NullPointerException success) {
1153 <        } finally {
1199 <            joinPool(e);
1146 >        final ExecutorService e = new CustomExecutor(2);
1147 >        try (PoolCleaner cleaner = cleaner(e)) {
1148 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1149 >            l.add(new StringTask());
1150 >            try {
1151 >                e.invokeAll(l, MEDIUM_DELAY_MS, null);
1152 >                shouldThrow();
1153 >            } catch (NullPointerException success) {}
1154          }
1155      }
1156  
# Line 1204 | Line 1158 | public class ScheduledExecutorSubclassTe
1158       * timed invokeAll(empty collection) returns empty collection
1159       */
1160      public void testTimedInvokeAll2() throws Exception {
1161 <        ExecutorService e = new CustomExecutor(2);
1162 <        try {
1161 >        final ExecutorService e = new CustomExecutor(2);
1162 >        try (PoolCleaner cleaner = cleaner(e)) {
1163              List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1164              assertTrue(r.isEmpty());
1211        } finally {
1212            joinPool(e);
1165          }
1166      }
1167  
# Line 1217 | Line 1169 | public class ScheduledExecutorSubclassTe
1169       * timed invokeAll(c) throws NPE if c has null elements
1170       */
1171      public void testTimedInvokeAll3() throws Exception {
1172 <        ExecutorService e = new CustomExecutor(2);
1173 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1174 <        l.add(new StringTask());
1175 <        l.add(null);
1176 <        try {
1177 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1178 <            shouldThrow();
1179 <        } catch (NullPointerException success) {
1180 <        } finally {
1229 <            joinPool(e);
1172 >        final ExecutorService e = new CustomExecutor(2);
1173 >        try (PoolCleaner cleaner = cleaner(e)) {
1174 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1175 >            l.add(new StringTask());
1176 >            l.add(null);
1177 >            try {
1178 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1179 >                shouldThrow();
1180 >            } catch (NullPointerException success) {}
1181          }
1182      }
1183  
# Line 1234 | Line 1185 | public class ScheduledExecutorSubclassTe
1185       * get of element of invokeAll(c) throws exception on failed task
1186       */
1187      public void testTimedInvokeAll4() throws Exception {
1188 <        ExecutorService e = new CustomExecutor(2);
1189 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1190 <        l.add(new NPETask());
1191 <        List<Future<String>> futures =
1192 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1193 <        assertEquals(1, futures.size());
1194 <        try {
1195 <            futures.get(0).get();
1196 <            shouldThrow();
1197 <        } catch (ExecutionException success) {
1198 <            assertTrue(success.getCause() instanceof NullPointerException);
1199 <        } finally {
1200 <            joinPool(e);
1188 >        final ExecutorService e = new CustomExecutor(2);
1189 >        try (PoolCleaner cleaner = cleaner(e)) {
1190 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1191 >            l.add(new NPETask());
1192 >            List<Future<String>> futures =
1193 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1194 >            assertEquals(1, futures.size());
1195 >            try {
1196 >                futures.get(0).get();
1197 >                shouldThrow();
1198 >            } catch (ExecutionException success) {
1199 >                assertTrue(success.getCause() instanceof NullPointerException);
1200 >            }
1201          }
1202      }
1203  
# Line 1254 | Line 1205 | public class ScheduledExecutorSubclassTe
1205       * timed invokeAll(c) returns results of all completed tasks
1206       */
1207      public void testTimedInvokeAll5() throws Exception {
1208 <        ExecutorService e = new CustomExecutor(2);
1209 <        try {
1208 >        final ExecutorService e = new CustomExecutor(2);
1209 >        try (PoolCleaner cleaner = cleaner(e)) {
1210              List<Callable<String>> l = new ArrayList<Callable<String>>();
1211              l.add(new StringTask());
1212              l.add(new StringTask());
# Line 1264 | Line 1215 | public class ScheduledExecutorSubclassTe
1215              assertEquals(2, futures.size());
1216              for (Future<String> future : futures)
1217                  assertSame(TEST_STRING, future.get());
1267        } finally {
1268            joinPool(e);
1218          }
1219      }
1220  
# Line 1273 | Line 1222 | public class ScheduledExecutorSubclassTe
1222       * timed invokeAll(c) cancels tasks not completed by timeout
1223       */
1224      public void testTimedInvokeAll6() throws Exception {
1225 <        ExecutorService e = new CustomExecutor(2);
1226 <        try {
1227 <            for (long timeout = timeoutMillis();;) {
1225 >        for (long timeout = timeoutMillis();;) {
1226 >            final CountDownLatch done = new CountDownLatch(1);
1227 >            final Callable<String> waiter = new CheckedCallable<String>() {
1228 >                public String realCall() {
1229 >                    try { done.await(LONG_DELAY_MS, MILLISECONDS); }
1230 >                    catch (InterruptedException ok) {}
1231 >                    return "1"; }};
1232 >            final ExecutorService p = new CustomExecutor(2);
1233 >            try (PoolCleaner cleaner = cleaner(p, done)) {
1234                  List<Callable<String>> tasks = new ArrayList<>();
1235                  tasks.add(new StringTask("0"));
1236 <                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1236 >                tasks.add(waiter);
1237                  tasks.add(new StringTask("2"));
1238                  long startTime = System.nanoTime();
1239                  List<Future<String>> futures =
1240 <                    e.invokeAll(tasks, timeout, MILLISECONDS);
1240 >                    p.invokeAll(tasks, timeout, MILLISECONDS);
1241                  assertEquals(tasks.size(), futures.size());
1242                  assertTrue(millisElapsedSince(startTime) >= timeout);
1243                  for (Future future : futures)
# Line 1298 | Line 1253 | public class ScheduledExecutorSubclassTe
1253                          fail("expected exactly one task to be cancelled");
1254                  }
1255              }
1301        } finally {
1302            joinPool(e);
1256          }
1257      }
1258  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines