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

Comparing jsr166/src/test/tck/ScheduledExecutorTest.java (file contents):
Revision 1.58 by jsr166, Mon Sep 28 08:23:49 2015 UTC vs.
Revision 1.73 by jsr166, Tue Oct 6 06:02:53 2015 UTC

# Line 42 | Line 42 | public class ScheduledExecutorTest exten
42       * execute successfully executes a runnable
43       */
44      public void testExecute() throws InterruptedException {
45 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
46 <        final CountDownLatch done = new CountDownLatch(1);
47 <        final Runnable task = new CheckedRunnable() {
48 <            public void realRun() {
49 <                done.countDown();
50 <            }};
51 <        try {
45 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
46 >        try (PoolCleaner cleaner = cleaner(p)) {
47 >            final CountDownLatch done = new CountDownLatch(1);
48 >            final Runnable task = new CheckedRunnable() {
49 >                public void realRun() { done.countDown(); }};
50              p.execute(task);
51 <            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
54 <        } finally {
55 <            joinPool(p);
51 >            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
52          }
53      }
54  
# Line 60 | Line 56 | public class ScheduledExecutorTest exten
56       * delayed schedule of callable successfully executes after delay
57       */
58      public void testSchedule1() throws Exception {
59 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
60 <        final long startTime = System.nanoTime();
61 <        final CountDownLatch done = new CountDownLatch(1);
62 <        try {
59 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
60 >        try (PoolCleaner cleaner = cleaner(p)) {
61 >            final long startTime = System.nanoTime();
62 >            final CountDownLatch done = new CountDownLatch(1);
63              Callable task = new CheckedCallable<Boolean>() {
64                  public Boolean realCall() {
65                      done.countDown();
# Line 74 | Line 70 | public class ScheduledExecutorTest exten
70              assertSame(Boolean.TRUE, f.get());
71              assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
72              assertTrue(done.await(0L, MILLISECONDS));
77        } finally {
78            joinPool(p);
73          }
74      }
75  
# Line 83 | Line 77 | public class ScheduledExecutorTest exten
77       * delayed schedule of runnable successfully executes after delay
78       */
79      public void testSchedule3() throws Exception {
80 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
81 <        final long startTime = System.nanoTime();
82 <        final CountDownLatch done = new CountDownLatch(1);
83 <        try {
80 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
81 >        try (PoolCleaner cleaner = cleaner(p)) {
82 >            final long startTime = System.nanoTime();
83 >            final CountDownLatch done = new CountDownLatch(1);
84              Runnable task = new CheckedRunnable() {
85                  public void realRun() {
86                      done.countDown();
# Line 96 | Line 90 | public class ScheduledExecutorTest exten
90              await(done);
91              assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
92              assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
99        } finally {
100            joinPool(p);
93          }
94      }
95  
# Line 105 | Line 97 | public class ScheduledExecutorTest exten
97       * scheduleAtFixedRate executes runnable after given initial delay
98       */
99      public void testSchedule4() throws Exception {
100 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
101 <        final long startTime = System.nanoTime();
102 <        final CountDownLatch done = new CountDownLatch(1);
103 <        try {
100 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
101 >        try (PoolCleaner cleaner = cleaner(p)) {
102 >            final long startTime = System.nanoTime();
103 >            final CountDownLatch done = new CountDownLatch(1);
104              Runnable task = new CheckedRunnable() {
105                  public void realRun() {
106                      done.countDown();
# Line 120 | Line 112 | public class ScheduledExecutorTest exten
112              await(done);
113              assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
114              f.cancel(true);
123        } finally {
124            joinPool(p);
115          }
116      }
117  
# Line 129 | Line 119 | public class ScheduledExecutorTest exten
119       * scheduleWithFixedDelay executes runnable after given initial delay
120       */
121      public void testSchedule5() throws Exception {
122 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
123 <        final long startTime = System.nanoTime();
124 <        final CountDownLatch done = new CountDownLatch(1);
125 <        try {
122 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
123 >        try (PoolCleaner cleaner = cleaner(p)) {
124 >            final long startTime = System.nanoTime();
125 >            final CountDownLatch done = new CountDownLatch(1);
126              Runnable task = new CheckedRunnable() {
127                  public void realRun() {
128                      done.countDown();
# Line 144 | Line 134 | public class ScheduledExecutorTest exten
134              await(done);
135              assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
136              f.cancel(true);
147        } finally {
148            joinPool(p);
137          }
138      }
139  
# Line 158 | Line 146 | public class ScheduledExecutorTest exten
146       * scheduleAtFixedRate executes series of tasks at given rate
147       */
148      public void testFixedRateSequence() throws InterruptedException {
149 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
150 <        try {
149 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
150 >        try (PoolCleaner cleaner = cleaner(p)) {
151              for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
152                  long startTime = System.nanoTime();
153                  int cycles = 10;
# Line 168 | Line 156 | public class ScheduledExecutorTest exten
156                      public void realRun() { done.countDown(); }};
157                  ScheduledFuture h =
158                      p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS);
159 <                done.await();
159 >                await(done);
160                  h.cancel(true);
161                  double normalizedTime =
162                      (double) millisElapsedSince(startTime) / delay;
# Line 177 | Line 165 | public class ScheduledExecutorTest exten
165                      return;
166              }
167              throw new AssertionError("unexpected execution rate");
180        } finally {
181            joinPool(p);
168          }
169      }
170  
# Line 186 | Line 172 | public class ScheduledExecutorTest exten
172       * scheduleWithFixedDelay executes series of tasks with given period
173       */
174      public void testFixedDelaySequence() throws InterruptedException {
175 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
176 <        try {
175 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
176 >        try (PoolCleaner cleaner = cleaner(p)) {
177              for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
178                  long startTime = System.nanoTime();
179                  int cycles = 10;
# Line 196 | Line 182 | public class ScheduledExecutorTest exten
182                      public void realRun() { done.countDown(); }};
183                  ScheduledFuture h =
184                      p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS);
185 <                done.await();
185 >                await(done);
186                  h.cancel(true);
187                  double normalizedTime =
188                      (double) millisElapsedSince(startTime) / delay;
# Line 205 | Line 191 | public class ScheduledExecutorTest exten
191                      return;
192              }
193              throw new AssertionError("unexpected execution rate");
208        } finally {
209            joinPool(p);
194          }
195      }
196  
# Line 214 | Line 198 | public class ScheduledExecutorTest exten
198       * execute(null) throws NPE
199       */
200      public void testExecuteNull() throws InterruptedException {
201 <        ScheduledThreadPoolExecutor se = null;
202 <        try {
203 <            se = new ScheduledThreadPoolExecutor(1);
204 <            se.execute(null);
205 <            shouldThrow();
206 <        } catch (NullPointerException success) {}
207 <
224 <        joinPool(se);
201 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
202 >        try (PoolCleaner cleaner = cleaner(p)) {
203 >            try {
204 >                p.execute(null);
205 >                shouldThrow();
206 >            } catch (NullPointerException success) {}
207 >        }
208      }
209  
210      /**
211       * schedule(null) throws NPE
212       */
213      public void testScheduleNull() throws InterruptedException {
214 <        ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
215 <        try {
216 <            TrackedCallable callable = null;
217 <            Future f = se.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
218 <            shouldThrow();
219 <        } catch (NullPointerException success) {}
220 <        joinPool(se);
214 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
215 >        try (PoolCleaner cleaner = cleaner(p)) {
216 >            try {
217 >                TrackedCallable callable = null;
218 >                Future f = p.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
219 >                shouldThrow();
220 >            } catch (NullPointerException success) {}
221 >        }
222      }
223  
224      /**
225       * execute throws RejectedExecutionException if shutdown
226       */
227      public void testSchedule1_RejectedExecutionException() throws InterruptedException {
228 <        ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
229 <        try {
230 <            se.shutdown();
231 <            se.schedule(new NoOpRunnable(),
232 <                        MEDIUM_DELAY_MS, MILLISECONDS);
233 <            shouldThrow();
234 <        } catch (RejectedExecutionException success) {
235 <        } catch (SecurityException ok) {
228 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
229 >        try (PoolCleaner cleaner = cleaner(p)) {
230 >            try {
231 >                p.shutdown();
232 >                p.schedule(new NoOpRunnable(),
233 >                           MEDIUM_DELAY_MS, MILLISECONDS);
234 >                shouldThrow();
235 >            } catch (RejectedExecutionException success) {
236 >            } catch (SecurityException ok) {}
237          }
253
254        joinPool(se);
238      }
239  
240      /**
241       * schedule throws RejectedExecutionException if shutdown
242       */
243      public void testSchedule2_RejectedExecutionException() throws InterruptedException {
244 <        ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
245 <        try {
246 <            se.shutdown();
247 <            se.schedule(new NoOpCallable(),
248 <                        MEDIUM_DELAY_MS, MILLISECONDS);
249 <            shouldThrow();
250 <        } catch (RejectedExecutionException success) {
251 <        } catch (SecurityException ok) {
244 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
245 >        try (PoolCleaner cleaner = cleaner(p)) {
246 >            try {
247 >                p.shutdown();
248 >                p.schedule(new NoOpCallable(),
249 >                           MEDIUM_DELAY_MS, MILLISECONDS);
250 >                shouldThrow();
251 >            } catch (RejectedExecutionException success) {
252 >            } catch (SecurityException ok) {}
253          }
270        joinPool(se);
254      }
255  
256      /**
257       * schedule callable throws RejectedExecutionException if shutdown
258       */
259      public void testSchedule3_RejectedExecutionException() throws InterruptedException {
260 <        ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
261 <        try {
262 <            se.shutdown();
263 <            se.schedule(new NoOpCallable(),
264 <                        MEDIUM_DELAY_MS, MILLISECONDS);
265 <            shouldThrow();
266 <        } catch (RejectedExecutionException success) {
267 <        } catch (SecurityException ok) {
260 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
261 >        try (PoolCleaner cleaner = cleaner(p)) {
262 >            try {
263 >                p.shutdown();
264 >                p.schedule(new NoOpCallable(),
265 >                           MEDIUM_DELAY_MS, MILLISECONDS);
266 >                shouldThrow();
267 >            } catch (RejectedExecutionException success) {
268 >            } catch (SecurityException ok) {}
269          }
286        joinPool(se);
270      }
271  
272      /**
273       * scheduleAtFixedRate throws RejectedExecutionException if shutdown
274       */
275      public void testScheduleAtFixedRate1_RejectedExecutionException() throws InterruptedException {
276 <        ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
277 <        try {
278 <            se.shutdown();
279 <            se.scheduleAtFixedRate(new NoOpRunnable(),
280 <                                   MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
281 <            shouldThrow();
282 <        } catch (RejectedExecutionException success) {
283 <        } catch (SecurityException ok) {
276 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
277 >        try (PoolCleaner cleaner = cleaner(p)) {
278 >            try {
279 >                p.shutdown();
280 >                p.scheduleAtFixedRate(new NoOpRunnable(),
281 >                                      MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
282 >                shouldThrow();
283 >            } catch (RejectedExecutionException success) {
284 >            } catch (SecurityException ok) {}
285          }
302        joinPool(se);
286      }
287  
288      /**
289       * scheduleWithFixedDelay throws RejectedExecutionException if shutdown
290       */
291      public void testScheduleWithFixedDelay1_RejectedExecutionException() throws InterruptedException {
292 <        ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
293 <        try {
294 <            se.shutdown();
295 <            se.scheduleWithFixedDelay(new NoOpRunnable(),
296 <                                      MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
297 <            shouldThrow();
298 <        } catch (RejectedExecutionException success) {
299 <        } catch (SecurityException ok) {
292 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
293 >        try (PoolCleaner cleaner = cleaner(p)) {
294 >            try {
295 >                p.shutdown();
296 >                p.scheduleWithFixedDelay(new NoOpRunnable(),
297 >                                         MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
298 >                shouldThrow();
299 >            } catch (RejectedExecutionException success) {
300 >            } catch (SecurityException ok) {}
301          }
318        joinPool(se);
302      }
303  
304      /**
# Line 323 | Line 306 | public class ScheduledExecutorTest exten
306       * thread becomes active
307       */
308      public void testGetActiveCount() throws InterruptedException {
326        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2);
327        final CountDownLatch threadStarted = new CountDownLatch(1);
309          final CountDownLatch done = new CountDownLatch(1);
310 <        try {
310 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2);
311 >        try (PoolCleaner cleaner = cleaner(p, done)) {
312 >            final CountDownLatch threadStarted = new CountDownLatch(1);
313              assertEquals(0, p.getActiveCount());
314              p.execute(new CheckedRunnable() {
315                  public void realRun() throws InterruptedException {
316                      threadStarted.countDown();
317                      assertEquals(1, p.getActiveCount());
318 <                    done.await();
318 >                    await(done);
319                  }});
320 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
320 >            await(threadStarted);
321              assertEquals(1, p.getActiveCount());
339        } finally {
340            done.countDown();
341            joinPool(p);
322          }
323      }
324  
# Line 348 | Line 328 | public class ScheduledExecutorTest exten
328       */
329      public void testGetCompletedTaskCount() throws InterruptedException {
330          final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2);
331 <        final CountDownLatch threadStarted = new CountDownLatch(1);
332 <        final CountDownLatch threadProceed = new CountDownLatch(1);
333 <        final CountDownLatch threadDone = new CountDownLatch(1);
334 <        try {
331 >        try (PoolCleaner cleaner = cleaner(p)) {
332 >            final CountDownLatch threadStarted = new CountDownLatch(1);
333 >            final CountDownLatch threadProceed = new CountDownLatch(1);
334 >            final CountDownLatch threadDone = new CountDownLatch(1);
335              assertEquals(0, p.getCompletedTaskCount());
336              p.execute(new CheckedRunnable() {
337                  public void realRun() throws InterruptedException {
# Line 370 | Line 350 | public class ScheduledExecutorTest exten
350                      fail("timed out");
351                  Thread.yield();
352              }
373        } finally {
374            joinPool(p);
353          }
354      }
355  
# Line 380 | Line 358 | public class ScheduledExecutorTest exten
358       */
359      public void testGetCorePoolSize() throws InterruptedException {
360          ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
361 <        assertEquals(1, p.getCorePoolSize());
362 <        joinPool(p);
361 >        try (PoolCleaner cleaner = cleaner(p)) {
362 >            assertEquals(1, p.getCorePoolSize());
363 >        }
364      }
365  
366      /**
# Line 393 | Line 372 | public class ScheduledExecutorTest exten
372          final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(THREADS);
373          final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
374          final CountDownLatch done = new CountDownLatch(1);
375 <        try {
375 >        try (PoolCleaner cleaner = cleaner(p, done)) {
376              assertEquals(0, p.getLargestPoolSize());
377              for (int i = 0; i < THREADS; i++)
378                  p.execute(new CheckedRunnable() {
379                      public void realRun() throws InterruptedException {
380                          threadsStarted.countDown();
381 <                        done.await();
381 >                        await(done);
382                          assertEquals(THREADS, p.getLargestPoolSize());
383                      }});
384 <            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
406 <            assertEquals(THREADS, p.getLargestPoolSize());
407 <        } finally {
408 <            done.countDown();
409 <            joinPool(p);
384 >            await(threadsStarted);
385              assertEquals(THREADS, p.getLargestPoolSize());
386          }
387 +        assertEquals(THREADS, p.getLargestPoolSize());
388      }
389  
390      /**
# Line 419 | Line 395 | public class ScheduledExecutorTest exten
395          final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
396          final CountDownLatch threadStarted = new CountDownLatch(1);
397          final CountDownLatch done = new CountDownLatch(1);
398 <        try {
398 >        try (PoolCleaner cleaner = cleaner(p, done)) {
399              assertEquals(0, p.getPoolSize());
400              p.execute(new CheckedRunnable() {
401                  public void realRun() throws InterruptedException {
402                      threadStarted.countDown();
403                      assertEquals(1, p.getPoolSize());
404 <                    done.await();
404 >                    await(done);
405                  }});
406 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
406 >            await(threadStarted);
407              assertEquals(1, p.getPoolSize());
432        } finally {
433            done.countDown();
434            joinPool(p);
408          }
409      }
410  
# Line 440 | Line 413 | public class ScheduledExecutorTest exten
413       * submitted
414       */
415      public void testGetTaskCount() throws InterruptedException {
416 <        final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
444 <        final CountDownLatch threadStarted = new CountDownLatch(1);
416 >        final int TASKS = 3;
417          final CountDownLatch done = new CountDownLatch(1);
418 <        final int TASKS = 5;
419 <        try {
418 >        final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
419 >        try (PoolCleaner cleaner = cleaner(p, done)) {
420 >            final CountDownLatch threadStarted = new CountDownLatch(1);
421              assertEquals(0, p.getTaskCount());
422 <            for (int i = 0; i < TASKS; i++)
422 >            assertEquals(0, p.getCompletedTaskCount());
423 >            p.execute(new CheckedRunnable() {
424 >                public void realRun() throws InterruptedException {
425 >                    threadStarted.countDown();
426 >                    await(done);
427 >                }});
428 >            await(threadStarted);
429 >            assertEquals(1, p.getTaskCount());
430 >            assertEquals(0, p.getCompletedTaskCount());
431 >            for (int i = 0; i < TASKS; i++) {
432 >                assertEquals(1 + i, p.getTaskCount());
433                  p.execute(new CheckedRunnable() {
434                      public void realRun() throws InterruptedException {
435                          threadStarted.countDown();
436 <                        done.await();
436 >                        assertEquals(1 + TASKS, p.getTaskCount());
437 >                        await(done);
438                      }});
439 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
440 <            assertEquals(TASKS, p.getTaskCount());
441 <        } finally {
458 <            done.countDown();
459 <            joinPool(p);
439 >            }
440 >            assertEquals(1 + TASKS, p.getTaskCount());
441 >            assertEquals(0, p.getCompletedTaskCount());
442          }
443 +        assertEquals(1 + TASKS, p.getTaskCount());
444 +        assertEquals(1 + TASKS, p.getCompletedTaskCount());
445      }
446  
447      /**
448       * getThreadFactory returns factory in constructor if not set
449       */
450      public void testGetThreadFactory() throws InterruptedException {
451 <        ThreadFactory tf = new SimpleThreadFactory();
452 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, tf);
453 <        assertSame(tf, p.getThreadFactory());
454 <        joinPool(p);
451 >        final ThreadFactory threadFactory = new SimpleThreadFactory();
452 >        final ScheduledThreadPoolExecutor p =
453 >            new ScheduledThreadPoolExecutor(1, threadFactory);
454 >        try (PoolCleaner cleaner = cleaner(p)) {
455 >            assertSame(threadFactory, p.getThreadFactory());
456 >        }
457      }
458  
459      /**
460       * setThreadFactory sets the thread factory returned by getThreadFactory
461       */
462      public void testSetThreadFactory() throws InterruptedException {
463 <        ThreadFactory tf = new SimpleThreadFactory();
464 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
465 <        p.setThreadFactory(tf);
466 <        assertSame(tf, p.getThreadFactory());
467 <        joinPool(p);
463 >        ThreadFactory threadFactory = new SimpleThreadFactory();
464 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
465 >        try (PoolCleaner cleaner = cleaner(p)) {
466 >            p.setThreadFactory(threadFactory);
467 >            assertSame(threadFactory, p.getThreadFactory());
468 >        }
469      }
470  
471      /**
472       * setThreadFactory(null) throws NPE
473       */
474      public void testSetThreadFactoryNull() throws InterruptedException {
475 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
476 <        try {
477 <            p.setThreadFactory(null);
478 <            shouldThrow();
479 <        } catch (NullPointerException success) {
480 <        } finally {
494 <            joinPool(p);
475 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
476 >        try (PoolCleaner cleaner = cleaner(p)) {
477 >            try {
478 >                p.setThreadFactory(null);
479 >                shouldThrow();
480 >            } catch (NullPointerException success) {}
481          }
482      }
483  
# Line 500 | Line 486 | public class ScheduledExecutorTest exten
486       */
487      public void testIsShutdown() {
488  
489 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
489 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
490          try {
491              assertFalse(p.isShutdown());
492          }
# Line 515 | Line 501 | public class ScheduledExecutorTest exten
501       */
502      public void testIsTerminated() throws InterruptedException {
503          final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
504 <        final CountDownLatch threadStarted = new CountDownLatch(1);
505 <        final CountDownLatch done = new CountDownLatch(1);
506 <        assertFalse(p.isTerminated());
507 <        try {
504 >        try (PoolCleaner cleaner = cleaner(p)) {
505 >            final CountDownLatch threadStarted = new CountDownLatch(1);
506 >            final CountDownLatch done = new CountDownLatch(1);
507 >            assertFalse(p.isTerminated());
508              p.execute(new CheckedRunnable() {
509                  public void realRun() throws InterruptedException {
510                      assertFalse(p.isTerminated());
511                      threadStarted.countDown();
512 <                    done.await();
512 >                    await(done);
513                  }});
514 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
514 >            await(threadStarted);
515              assertFalse(p.isTerminating());
516              done.countDown();
531        } finally {
517              try { p.shutdown(); } catch (SecurityException ok) { return; }
518 +            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
519 +            assertTrue(p.isTerminated());
520          }
534        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
535        assertTrue(p.isTerminated());
521      }
522  
523      /**
# Line 542 | Line 527 | public class ScheduledExecutorTest exten
527          final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
528          final CountDownLatch threadStarted = new CountDownLatch(1);
529          final CountDownLatch done = new CountDownLatch(1);
530 <        try {
530 >        try (PoolCleaner cleaner = cleaner(p)) {
531              assertFalse(p.isTerminating());
532              p.execute(new CheckedRunnable() {
533                  public void realRun() throws InterruptedException {
534                      assertFalse(p.isTerminating());
535                      threadStarted.countDown();
536 <                    done.await();
536 >                    await(done);
537                  }});
538 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
538 >            await(threadStarted);
539              assertFalse(p.isTerminating());
540              done.countDown();
556        } finally {
541              try { p.shutdown(); } catch (SecurityException ok) { return; }
542 +            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
543 +            assertTrue(p.isTerminated());
544 +            assertFalse(p.isTerminating());
545          }
559        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
560        assertTrue(p.isTerminated());
561        assertFalse(p.isTerminating());
546      }
547  
548      /**
549       * getQueue returns the work queue, which contains queued tasks
550       */
551      public void testGetQueue() throws InterruptedException {
568        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
569        final CountDownLatch threadStarted = new CountDownLatch(1);
552          final CountDownLatch done = new CountDownLatch(1);
553 <        try {
553 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
554 >        try (PoolCleaner cleaner = cleaner(p, done)) {
555 >            final CountDownLatch threadStarted = new CountDownLatch(1);
556              ScheduledFuture[] tasks = new ScheduledFuture[5];
557              for (int i = 0; i < tasks.length; i++) {
558                  Runnable r = new CheckedRunnable() {
559                      public void realRun() throws InterruptedException {
560                          threadStarted.countDown();
561 <                        done.await();
561 >                        await(done);
562                      }};
563                  tasks[i] = p.schedule(r, 1, MILLISECONDS);
564              }
565 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
565 >            await(threadStarted);
566              BlockingQueue<Runnable> q = p.getQueue();
567              assertTrue(q.contains(tasks[tasks.length - 1]));
568              assertFalse(q.contains(tasks[0]));
585        } finally {
586            done.countDown();
587            joinPool(p);
569          }
570      }
571  
# Line 592 | Line 573 | public class ScheduledExecutorTest exten
573       * remove(task) removes queued task, and fails to remove active task
574       */
575      public void testRemove() throws InterruptedException {
595        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
596        ScheduledFuture[] tasks = new ScheduledFuture[5];
597        final CountDownLatch threadStarted = new CountDownLatch(1);
576          final CountDownLatch done = new CountDownLatch(1);
577 <        try {
577 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
578 >        try (PoolCleaner cleaner = cleaner(p, done)) {
579 >            ScheduledFuture[] tasks = new ScheduledFuture[5];
580 >            final CountDownLatch threadStarted = new CountDownLatch(1);
581              for (int i = 0; i < tasks.length; i++) {
582                  Runnable r = new CheckedRunnable() {
583                      public void realRun() throws InterruptedException {
584                          threadStarted.countDown();
585 <                        done.await();
585 >                        await(done);
586                      }};
587                  tasks[i] = p.schedule(r, 1, MILLISECONDS);
588              }
589 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
589 >            await(threadStarted);
590              BlockingQueue<Runnable> q = p.getQueue();
591              assertFalse(p.remove((Runnable)tasks[0]));
592              assertTrue(q.contains((Runnable)tasks[4]));
# Line 616 | Line 597 | public class ScheduledExecutorTest exten
597              assertTrue(q.contains((Runnable)tasks[3]));
598              assertTrue(p.remove((Runnable)tasks[3]));
599              assertFalse(q.contains((Runnable)tasks[3]));
619        } finally {
620            done.countDown();
621            joinPool(p);
600          }
601      }
602  
# Line 626 | Line 604 | public class ScheduledExecutorTest exten
604       * purge eventually removes cancelled tasks from the queue
605       */
606      public void testPurge() throws InterruptedException {
607 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
608 <        ScheduledFuture[] tasks = new ScheduledFuture[5];
609 <        for (int i = 0; i < tasks.length; i++)
610 <            tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(),
611 <                                  LONG_DELAY_MS, MILLISECONDS);
612 <        try {
607 >        final ScheduledFuture[] tasks = new ScheduledFuture[5];
608 >        final Runnable releaser = new Runnable() { public void run() {
609 >            for (ScheduledFuture task : tasks)
610 >                if (task != null) task.cancel(true); }};
611 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
612 >        try (PoolCleaner cleaner = cleaner(p, releaser)) {
613 >            for (int i = 0; i < tasks.length; i++)
614 >                tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(),
615 >                                      LONG_DELAY_MS, MILLISECONDS);
616              int max = tasks.length;
617              if (tasks[4].cancel(true)) --max;
618              if (tasks[3].cancel(true)) --max;
# Line 643 | Line 624 | public class ScheduledExecutorTest exten
624                  long count = p.getTaskCount();
625                  if (count == max)
626                      return;
627 <            } while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
627 >            } while (millisElapsedSince(startTime) < LONG_DELAY_MS);
628              fail("Purge failed to remove cancelled tasks");
648        } finally {
649            for (ScheduledFuture task : tasks)
650                task.cancel(true);
651            joinPool(p);
629          }
630      }
631  
# Line 662 | Line 639 | public class ScheduledExecutorTest exten
639          final AtomicInteger ran = new AtomicInteger(0);
640          final ScheduledThreadPoolExecutor p =
641              new ScheduledThreadPoolExecutor(poolSize);
642 <        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
642 >        final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
643          Runnable waiter = new CheckedRunnable() { public void realRun() {
644              threadsStarted.countDown();
645              try {
# Line 672 | Line 649 | public class ScheduledExecutorTest exten
649          }};
650          for (int i = 0; i < count; i++)
651              p.execute(waiter);
652 <        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
652 >        await(threadsStarted);
653          assertEquals(poolSize, p.getActiveCount());
654          assertEquals(0, p.getCompletedTaskCount());
655          final List<Runnable> queuedTasks;
# Line 695 | Line 672 | public class ScheduledExecutorTest exten
672       * and those tasks are drained from the queue
673       */
674      public void testShutdownNow_delayedTasks() throws InterruptedException {
675 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
675 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
676          List<ScheduledFuture> tasks = new ArrayList<>();
677          for (int i = 0; i < 3; i++) {
678              Runnable r = new NoOpRunnable();
# Line 822 | Line 799 | public class ScheduledExecutorTest exten
799       * completed submit of callable returns result
800       */
801      public void testSubmitCallable() throws Exception {
802 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
803 <        try {
802 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
803 >        try (PoolCleaner cleaner = cleaner(e)) {
804              Future<String> future = e.submit(new StringTask());
805              String result = future.get();
806              assertSame(TEST_STRING, result);
830        } finally {
831            joinPool(e);
807          }
808      }
809  
# Line 836 | Line 811 | public class ScheduledExecutorTest exten
811       * completed submit of runnable returns successfully
812       */
813      public void testSubmitRunnable() throws Exception {
814 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
815 <        try {
814 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
815 >        try (PoolCleaner cleaner = cleaner(e)) {
816              Future<?> future = e.submit(new NoOpRunnable());
817              future.get();
818              assertTrue(future.isDone());
844        } finally {
845            joinPool(e);
819          }
820      }
821  
# Line 850 | Line 823 | public class ScheduledExecutorTest exten
823       * completed submit of (runnable, result) returns result
824       */
825      public void testSubmitRunnable2() throws Exception {
826 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
827 <        try {
826 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
827 >        try (PoolCleaner cleaner = cleaner(e)) {
828              Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
829              String result = future.get();
830              assertSame(TEST_STRING, result);
858        } finally {
859            joinPool(e);
831          }
832      }
833  
# Line 864 | Line 835 | public class ScheduledExecutorTest exten
835       * invokeAny(null) throws NPE
836       */
837      public void testInvokeAny1() throws Exception {
838 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
839 <        try {
840 <            e.invokeAny(null);
841 <            shouldThrow();
842 <        } catch (NullPointerException success) {
843 <        } finally {
873 <            joinPool(e);
838 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
839 >        try (PoolCleaner cleaner = cleaner(e)) {
840 >            try {
841 >                e.invokeAny(null);
842 >                shouldThrow();
843 >            } catch (NullPointerException success) {}
844          }
845      }
846  
# Line 878 | Line 848 | public class ScheduledExecutorTest exten
848       * invokeAny(empty collection) throws IAE
849       */
850      public void testInvokeAny2() throws Exception {
851 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
852 <        try {
853 <            e.invokeAny(new ArrayList<Callable<String>>());
854 <            shouldThrow();
855 <        } catch (IllegalArgumentException success) {
856 <        } finally {
887 <            joinPool(e);
851 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
852 >        try (PoolCleaner cleaner = cleaner(e)) {
853 >            try {
854 >                e.invokeAny(new ArrayList<Callable<String>>());
855 >                shouldThrow();
856 >            } catch (IllegalArgumentException success) {}
857          }
858      }
859  
# Line 893 | Line 862 | public class ScheduledExecutorTest exten
862       */
863      public void testInvokeAny3() throws Exception {
864          CountDownLatch latch = new CountDownLatch(1);
865 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
866 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
867 <        l.add(latchAwaitingStringTask(latch));
868 <        l.add(null);
869 <        try {
870 <            e.invokeAny(l);
871 <            shouldThrow();
872 <        } catch (NullPointerException success) {
873 <        } finally {
865 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
866 >        try (PoolCleaner cleaner = cleaner(e)) {
867 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
868 >            l.add(latchAwaitingStringTask(latch));
869 >            l.add(null);
870 >            try {
871 >                e.invokeAny(l);
872 >                shouldThrow();
873 >            } catch (NullPointerException success) {}
874              latch.countDown();
906            joinPool(e);
875          }
876      }
877  
# Line 911 | Line 879 | public class ScheduledExecutorTest exten
879       * invokeAny(c) throws ExecutionException if no task completes
880       */
881      public void testInvokeAny4() throws Exception {
882 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
883 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
884 <        l.add(new NPETask());
885 <        try {
886 <            e.invokeAny(l);
887 <            shouldThrow();
888 <        } catch (ExecutionException success) {
889 <            assertTrue(success.getCause() instanceof NullPointerException);
890 <        } finally {
891 <            joinPool(e);
882 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
883 >        try (PoolCleaner cleaner = cleaner(e)) {
884 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
885 >            l.add(new NPETask());
886 >            try {
887 >                e.invokeAny(l);
888 >                shouldThrow();
889 >            } catch (ExecutionException success) {
890 >                assertTrue(success.getCause() instanceof NullPointerException);
891 >            }
892          }
893      }
894  
# Line 928 | Line 896 | public class ScheduledExecutorTest exten
896       * invokeAny(c) returns result of some task
897       */
898      public void testInvokeAny5() throws Exception {
899 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
900 <        try {
899 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
900 >        try (PoolCleaner cleaner = cleaner(e)) {
901              List<Callable<String>> l = new ArrayList<Callable<String>>();
902              l.add(new StringTask());
903              l.add(new StringTask());
904              String result = e.invokeAny(l);
905              assertSame(TEST_STRING, result);
938        } finally {
939            joinPool(e);
906          }
907      }
908  
# Line 944 | Line 910 | public class ScheduledExecutorTest exten
910       * invokeAll(null) throws NPE
911       */
912      public void testInvokeAll1() throws Exception {
913 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
914 <        try {
915 <            e.invokeAll(null);
916 <            shouldThrow();
917 <        } catch (NullPointerException success) {
918 <        } finally {
953 <            joinPool(e);
913 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
914 >        try (PoolCleaner cleaner = cleaner(e)) {
915 >            try {
916 >                e.invokeAll(null);
917 >                shouldThrow();
918 >            } catch (NullPointerException success) {}
919          }
920      }
921  
# Line 958 | Line 923 | public class ScheduledExecutorTest exten
923       * invokeAll(empty collection) returns empty collection
924       */
925      public void testInvokeAll2() throws Exception {
926 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
927 <        try {
926 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
927 >        try (PoolCleaner cleaner = cleaner(e)) {
928              List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
929              assertTrue(r.isEmpty());
965        } finally {
966            joinPool(e);
930          }
931      }
932  
# Line 971 | Line 934 | public class ScheduledExecutorTest exten
934       * invokeAll(c) throws NPE if c has null elements
935       */
936      public void testInvokeAll3() throws Exception {
937 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
938 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
939 <        l.add(new StringTask());
940 <        l.add(null);
941 <        try {
942 <            e.invokeAll(l);
943 <            shouldThrow();
944 <        } catch (NullPointerException success) {
945 <        } finally {
983 <            joinPool(e);
937 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
938 >        try (PoolCleaner cleaner = cleaner(e)) {
939 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
940 >            l.add(new StringTask());
941 >            l.add(null);
942 >            try {
943 >                e.invokeAll(l);
944 >                shouldThrow();
945 >            } catch (NullPointerException success) {}
946          }
947      }
948  
# Line 988 | Line 950 | public class ScheduledExecutorTest exten
950       * get of invokeAll(c) throws exception on failed task
951       */
952      public void testInvokeAll4() throws Exception {
953 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
954 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
955 <        l.add(new NPETask());
956 <        List<Future<String>> futures = e.invokeAll(l);
957 <        assertEquals(1, futures.size());
958 <        try {
959 <            futures.get(0).get();
960 <            shouldThrow();
961 <        } catch (ExecutionException success) {
962 <            assertTrue(success.getCause() instanceof NullPointerException);
963 <        } finally {
964 <            joinPool(e);
953 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
954 >        try (PoolCleaner cleaner = cleaner(e)) {
955 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
956 >            l.add(new NPETask());
957 >            List<Future<String>> futures = e.invokeAll(l);
958 >            assertEquals(1, futures.size());
959 >            try {
960 >                futures.get(0).get();
961 >                shouldThrow();
962 >            } catch (ExecutionException success) {
963 >                assertTrue(success.getCause() instanceof NullPointerException);
964 >            }
965          }
966      }
967  
# Line 1007 | Line 969 | public class ScheduledExecutorTest exten
969       * invokeAll(c) returns results of all completed tasks
970       */
971      public void testInvokeAll5() throws Exception {
972 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
973 <        try {
972 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
973 >        try (PoolCleaner cleaner = cleaner(e)) {
974              List<Callable<String>> l = new ArrayList<Callable<String>>();
975              l.add(new StringTask());
976              l.add(new StringTask());
# Line 1016 | Line 978 | public class ScheduledExecutorTest exten
978              assertEquals(2, futures.size());
979              for (Future<String> future : futures)
980                  assertSame(TEST_STRING, future.get());
1019        } finally {
1020            joinPool(e);
981          }
982      }
983  
# Line 1025 | Line 985 | public class ScheduledExecutorTest exten
985       * timed invokeAny(null) throws NPE
986       */
987      public void testTimedInvokeAny1() throws Exception {
988 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
989 <        try {
990 <            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
991 <            shouldThrow();
992 <        } catch (NullPointerException success) {
993 <        } finally {
1034 <            joinPool(e);
988 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
989 >        try (PoolCleaner cleaner = cleaner(e)) {
990 >            try {
991 >                e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
992 >                shouldThrow();
993 >            } catch (NullPointerException success) {}
994          }
995      }
996  
# Line 1039 | Line 998 | public class ScheduledExecutorTest exten
998       * timed invokeAny(,,null) throws NPE
999       */
1000      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1001 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1002 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1003 <        l.add(new StringTask());
1004 <        try {
1005 <            e.invokeAny(l, MEDIUM_DELAY_MS, null);
1006 <            shouldThrow();
1007 <        } catch (NullPointerException success) {
1008 <        } finally {
1050 <            joinPool(e);
1001 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1002 >        try (PoolCleaner cleaner = cleaner(e)) {
1003 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1004 >            l.add(new StringTask());
1005 >            try {
1006 >                e.invokeAny(l, MEDIUM_DELAY_MS, null);
1007 >                shouldThrow();
1008 >            } catch (NullPointerException success) {}
1009          }
1010      }
1011  
# Line 1055 | Line 1013 | public class ScheduledExecutorTest exten
1013       * timed invokeAny(empty collection) throws IAE
1014       */
1015      public void testTimedInvokeAny2() throws Exception {
1016 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1017 <        try {
1018 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1019 <            shouldThrow();
1020 <        } catch (IllegalArgumentException success) {
1021 <        } finally {
1064 <            joinPool(e);
1016 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1017 >        try (PoolCleaner cleaner = cleaner(e)) {
1018 >            try {
1019 >                e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1020 >                shouldThrow();
1021 >            } catch (IllegalArgumentException success) {}
1022          }
1023      }
1024  
# Line 1070 | Line 1027 | public class ScheduledExecutorTest exten
1027       */
1028      public void testTimedInvokeAny3() throws Exception {
1029          CountDownLatch latch = new CountDownLatch(1);
1030 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1031 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1032 <        l.add(latchAwaitingStringTask(latch));
1033 <        l.add(null);
1034 <        try {
1035 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1036 <            shouldThrow();
1037 <        } catch (NullPointerException success) {
1038 <        } finally {
1030 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1031 >        try (PoolCleaner cleaner = cleaner(e)) {
1032 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1033 >            l.add(latchAwaitingStringTask(latch));
1034 >            l.add(null);
1035 >            try {
1036 >                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1037 >                shouldThrow();
1038 >            } catch (NullPointerException success) {}
1039              latch.countDown();
1083            joinPool(e);
1040          }
1041      }
1042  
# Line 1088 | Line 1044 | public class ScheduledExecutorTest exten
1044       * timed invokeAny(c) throws ExecutionException if no task completes
1045       */
1046      public void testTimedInvokeAny4() throws Exception {
1047 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1048 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1049 <        l.add(new NPETask());
1050 <        try {
1051 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1052 <            shouldThrow();
1053 <        } catch (ExecutionException success) {
1054 <            assertTrue(success.getCause() instanceof NullPointerException);
1055 <        } finally {
1056 <            joinPool(e);
1047 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1048 >        try (PoolCleaner cleaner = cleaner(e)) {
1049 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1050 >            l.add(new NPETask());
1051 >            try {
1052 >                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1053 >                shouldThrow();
1054 >            } catch (ExecutionException success) {
1055 >                assertTrue(success.getCause() instanceof NullPointerException);
1056 >            }
1057          }
1058      }
1059  
# Line 1105 | Line 1061 | public class ScheduledExecutorTest exten
1061       * timed invokeAny(c) returns result of some task
1062       */
1063      public void testTimedInvokeAny5() throws Exception {
1064 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1065 <        try {
1064 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1065 >        try (PoolCleaner cleaner = cleaner(e)) {
1066              List<Callable<String>> l = new ArrayList<Callable<String>>();
1067              l.add(new StringTask());
1068              l.add(new StringTask());
1069              String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1070              assertSame(TEST_STRING, result);
1115        } finally {
1116            joinPool(e);
1071          }
1072      }
1073  
# Line 1121 | Line 1075 | public class ScheduledExecutorTest exten
1075       * timed invokeAll(null) throws NPE
1076       */
1077      public void testTimedInvokeAll1() throws Exception {
1078 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1079 <        try {
1080 <            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1081 <            shouldThrow();
1082 <        } catch (NullPointerException success) {
1083 <        } finally {
1130 <            joinPool(e);
1078 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1079 >        try (PoolCleaner cleaner = cleaner(e)) {
1080 >            try {
1081 >                e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1082 >                shouldThrow();
1083 >            } catch (NullPointerException success) {}
1084          }
1085      }
1086  
# Line 1135 | Line 1088 | public class ScheduledExecutorTest exten
1088       * timed invokeAll(,,null) throws NPE
1089       */
1090      public void testTimedInvokeAllNullTimeUnit() throws Exception {
1091 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1092 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1093 <        l.add(new StringTask());
1094 <        try {
1095 <            e.invokeAll(l, MEDIUM_DELAY_MS, null);
1096 <            shouldThrow();
1097 <        } catch (NullPointerException success) {
1098 <        } finally {
1146 <            joinPool(e);
1091 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1092 >        try (PoolCleaner cleaner = cleaner(e)) {
1093 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1094 >            l.add(new StringTask());
1095 >            try {
1096 >                e.invokeAll(l, MEDIUM_DELAY_MS, null);
1097 >                shouldThrow();
1098 >            } catch (NullPointerException success) {}
1099          }
1100      }
1101  
# Line 1151 | Line 1103 | public class ScheduledExecutorTest exten
1103       * timed invokeAll(empty collection) returns empty collection
1104       */
1105      public void testTimedInvokeAll2() throws Exception {
1106 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1107 <        try {
1108 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1106 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1107 >        try (PoolCleaner cleaner = cleaner(e)) {
1108 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(),
1109 >                                                 MEDIUM_DELAY_MS, MILLISECONDS);
1110              assertTrue(r.isEmpty());
1158        } finally {
1159            joinPool(e);
1111          }
1112      }
1113  
# Line 1164 | Line 1115 | public class ScheduledExecutorTest exten
1115       * timed invokeAll(c) throws NPE if c has null elements
1116       */
1117      public void testTimedInvokeAll3() throws Exception {
1118 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1119 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1120 <        l.add(new StringTask());
1121 <        l.add(null);
1122 <        try {
1123 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1124 <            shouldThrow();
1125 <        } catch (NullPointerException success) {
1126 <        } finally {
1176 <            joinPool(e);
1118 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1119 >        try (PoolCleaner cleaner = cleaner(e)) {
1120 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1121 >            l.add(new StringTask());
1122 >            l.add(null);
1123 >            try {
1124 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1125 >                shouldThrow();
1126 >            } catch (NullPointerException success) {}
1127          }
1128      }
1129  
# Line 1181 | Line 1131 | public class ScheduledExecutorTest exten
1131       * get of element of invokeAll(c) throws exception on failed task
1132       */
1133      public void testTimedInvokeAll4() throws Exception {
1134 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1135 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
1136 <        l.add(new NPETask());
1137 <        List<Future<String>> futures =
1138 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1139 <        assertEquals(1, futures.size());
1140 <        try {
1141 <            futures.get(0).get();
1142 <            shouldThrow();
1143 <        } catch (ExecutionException success) {
1144 <            assertTrue(success.getCause() instanceof NullPointerException);
1145 <        } finally {
1146 <            joinPool(e);
1134 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1135 >        try (PoolCleaner cleaner = cleaner(e)) {
1136 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1137 >            l.add(new NPETask());
1138 >            List<Future<String>> futures =
1139 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1140 >            assertEquals(1, futures.size());
1141 >            try {
1142 >                futures.get(0).get();
1143 >                shouldThrow();
1144 >            } catch (ExecutionException success) {
1145 >                assertTrue(success.getCause() instanceof NullPointerException);
1146 >            }
1147          }
1148      }
1149  
# Line 1201 | Line 1151 | public class ScheduledExecutorTest exten
1151       * timed invokeAll(c) returns results of all completed tasks
1152       */
1153      public void testTimedInvokeAll5() throws Exception {
1154 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1155 <        try {
1154 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1155 >        try (PoolCleaner cleaner = cleaner(e)) {
1156              List<Callable<String>> l = new ArrayList<Callable<String>>();
1157              l.add(new StringTask());
1158              l.add(new StringTask());
1159              List<Future<String>> futures =
1160 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1160 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1161              assertEquals(2, futures.size());
1162              for (Future<String> future : futures)
1163                  assertSame(TEST_STRING, future.get());
1214        } finally {
1215            joinPool(e);
1164          }
1165      }
1166  
# Line 1220 | Line 1168 | public class ScheduledExecutorTest exten
1168       * timed invokeAll(c) cancels tasks not completed by timeout
1169       */
1170      public void testTimedInvokeAll6() throws Exception {
1171 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1172 <        try {
1171 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1172 >        try (PoolCleaner cleaner = cleaner(e)) {
1173              for (long timeout = timeoutMillis();;) {
1174                  List<Callable<String>> tasks = new ArrayList<>();
1175                  tasks.add(new StringTask("0"));
# Line 1245 | Line 1193 | public class ScheduledExecutorTest exten
1193                          fail("expected exactly one task to be cancelled");
1194                  }
1195              }
1248        } finally {
1249            joinPool(e);
1196          }
1197      }
1198  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines