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.8 by jsr166, Sat Nov 21 17:38:05 2009 UTC vs.
Revision 1.25 by jsr166, Fri May 27 19:43:27 2011 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7   import junit.framework.*;
# Line 12 | Line 12 | import java.util.concurrent.atomic.*;
12  
13   public class ScheduledExecutorSubclassTest extends JSR166TestCase {
14      public static void main(String[] args) {
15 <        junit.textui.TestRunner.run (suite());
15 >        junit.textui.TestRunner.run(suite());
16      }
17      public static Test suite() {
18          return new TestSuite(ScheduledExecutorSubclassTest.class);
# Line 48 | Line 48 | public class ScheduledExecutorSubclassTe
48          }
49      }
50  
51
51      public class CustomExecutor extends ScheduledThreadPoolExecutor {
52  
53          protected <V> RunnableScheduledFuture<V> decorateTask(Runnable r, RunnableScheduledFuture<V> task) {
# Line 73 | Line 72 | public class ScheduledExecutorSubclassTe
72  
73      }
74  
76
77
75      /**
76       * execute successfully executes a runnable
77       */
78      public void testExecute() throws InterruptedException {
79 <        TrackedShortRunnable runnable =new TrackedShortRunnable();
80 <        CustomExecutor p1 = new CustomExecutor(1);
81 <        p1.execute(runnable);
82 <        assertFalse(runnable.done);
83 <        Thread.sleep(SHORT_DELAY_MS);
84 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
85 <        Thread.sleep(MEDIUM_DELAY_MS);
86 <        assertTrue(runnable.done);
87 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
88 <        joinPool(p1);
79 >        CustomExecutor p = new CustomExecutor(1);
80 >        final CountDownLatch done = new CountDownLatch(1);
81 >        final Runnable task = new CheckedRunnable() {
82 >            public void realRun() {
83 >                done.countDown();
84 >            }};
85 >        try {
86 >            p.execute(task);
87 >            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
88 >        } finally {
89 >            joinPool(p);
90 >        }
91      }
92  
94
93      /**
94       * delayed schedule of callable successfully executes after delay
95       */
96      public void testSchedule1() throws Exception {
97 <        TrackedCallable callable = new TrackedCallable();
98 <        CustomExecutor p1 = new CustomExecutor(1);
99 <        Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
100 <        assertFalse(callable.done);
101 <        Thread.sleep(MEDIUM_DELAY_MS);
102 <        assertTrue(callable.done);
103 <        assertEquals(Boolean.TRUE, f.get());
104 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
105 <        joinPool(p1);
97 >        CustomExecutor p = new CustomExecutor(1);
98 >        final long t0 = System.nanoTime();
99 >        final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
100 >        final CountDownLatch done = new CountDownLatch(1);
101 >        try {
102 >            Callable task = new CheckedCallable<Boolean>() {
103 >                public Boolean realCall() {
104 >                    done.countDown();
105 >                    assertTrue(System.nanoTime() - t0 >= timeoutNanos);
106 >                    return Boolean.TRUE;
107 >                }};
108 >            Future f = p.schedule(task, SHORT_DELAY_MS, MILLISECONDS);
109 >            assertEquals(Boolean.TRUE, f.get());
110 >            assertTrue(System.nanoTime() - t0 >= timeoutNanos);
111 >            assertTrue(done.await(0L, MILLISECONDS));
112 >        } finally {
113 >            joinPool(p);
114 >        }
115      }
116  
117      /**
118 <     *  delayed schedule of runnable successfully executes after delay
119 <     */
120 <    public void testSchedule3() throws InterruptedException {
121 <        TrackedShortRunnable runnable = new TrackedShortRunnable();
122 <        CustomExecutor p1 = new CustomExecutor(1);
123 <        p1.schedule(runnable, SMALL_DELAY_MS, MILLISECONDS);
124 <        Thread.sleep(SHORT_DELAY_MS);
125 <        assertFalse(runnable.done);
126 <        Thread.sleep(MEDIUM_DELAY_MS);
127 <        assertTrue(runnable.done);
128 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
129 <        joinPool(p1);
118 >     * delayed schedule of runnable successfully executes after delay
119 >     */
120 >    public void testSchedule3() throws Exception {
121 >        CustomExecutor p = new CustomExecutor(1);
122 >        final long t0 = System.nanoTime();
123 >        final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
124 >        final CountDownLatch done = new CountDownLatch(1);
125 >        try {
126 >            Runnable task = new CheckedRunnable() {
127 >                public void realRun() {
128 >                    done.countDown();
129 >                    assertTrue(System.nanoTime() - t0 >= timeoutNanos);
130 >                }};
131 >            Future f = p.schedule(task, SHORT_DELAY_MS, MILLISECONDS);
132 >            assertNull(f.get());
133 >            assertTrue(System.nanoTime() - t0 >= timeoutNanos);
134 >            assertTrue(done.await(0L, MILLISECONDS));
135 >        } finally {
136 >            joinPool(p);
137 >        }
138      }
139  
140      /**
141       * scheduleAtFixedRate executes runnable after given initial delay
142       */
143      public void testSchedule4() throws InterruptedException {
144 <        TrackedShortRunnable runnable = new TrackedShortRunnable();
145 <        CustomExecutor p1 = new CustomExecutor(1);
146 <        ScheduledFuture h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS);
147 <        assertFalse(runnable.done);
148 <        Thread.sleep(MEDIUM_DELAY_MS);
149 <        assertTrue(runnable.done);
150 <        h.cancel(true);
151 <        joinPool(p1);
152 <    }
153 <
154 <    static class RunnableCounter implements Runnable {
155 <        AtomicInteger count = new AtomicInteger(0);
156 <        public void run() { count.getAndIncrement(); }
144 >        CustomExecutor p = new CustomExecutor(1);
145 >        final long t0 = System.nanoTime();
146 >        final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
147 >        final CountDownLatch done = new CountDownLatch(1);
148 >        try {
149 >            Runnable task = new CheckedRunnable() {
150 >                public void realRun() {
151 >                    done.countDown();
152 >                    assertTrue(System.nanoTime() - t0 >= timeoutNanos);
153 >                }};
154 >            ScheduledFuture f =
155 >                p.scheduleAtFixedRate(task, SHORT_DELAY_MS,
156 >                                      SHORT_DELAY_MS, MILLISECONDS);
157 >            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
158 >            assertTrue(System.nanoTime() - t0 >= timeoutNanos);
159 >            f.cancel(true);
160 >        } finally {
161 >            joinPool(p);
162 >        }
163      }
164  
165      /**
166       * scheduleWithFixedDelay executes runnable after given initial delay
167       */
168      public void testSchedule5() throws InterruptedException {
169 <        TrackedShortRunnable runnable = new TrackedShortRunnable();
170 <        CustomExecutor p1 = new CustomExecutor(1);
171 <        ScheduledFuture h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS);
172 <        assertFalse(runnable.done);
173 <        Thread.sleep(MEDIUM_DELAY_MS);
174 <        assertTrue(runnable.done);
175 <        h.cancel(true);
176 <        joinPool(p1);
169 >        CustomExecutor p = new CustomExecutor(1);
170 >        final long t0 = System.nanoTime();
171 >        final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
172 >        final CountDownLatch done = new CountDownLatch(1);
173 >        try {
174 >            Runnable task = new CheckedRunnable() {
175 >                public void realRun() {
176 >                    done.countDown();
177 >                    assertTrue(System.nanoTime() - t0 >= timeoutNanos);
178 >                }};
179 >            ScheduledFuture f =
180 >                p.scheduleWithFixedDelay(task, SHORT_DELAY_MS,
181 >                                         SHORT_DELAY_MS, MILLISECONDS);
182 >            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
183 >            assertTrue(System.nanoTime() - t0 >= timeoutNanos);
184 >            f.cancel(true);
185 >        } finally {
186 >            joinPool(p);
187 >        }
188 >    }
189 >
190 >    static class RunnableCounter implements Runnable {
191 >        AtomicInteger count = new AtomicInteger(0);
192 >        public void run() { count.getAndIncrement(); }
193      }
194  
195      /**
196       * scheduleAtFixedRate executes series of tasks at given rate
197       */
198      public void testFixedRateSequence() throws InterruptedException {
199 <        CustomExecutor p1 = new CustomExecutor(1);
199 >        CustomExecutor p = new CustomExecutor(1);
200          RunnableCounter counter = new RunnableCounter();
201          ScheduledFuture h =
202 <            p1.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS);
203 <        Thread.sleep(SMALL_DELAY_MS);
202 >            p.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS);
203 >        delay(SMALL_DELAY_MS);
204          h.cancel(true);
205          int c = counter.count.get();
206          // By time scaling conventions, we must have at least
207          // an execution per SHORT delay, but no more than one SHORT more
208          assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
209          assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
210 <        joinPool(p1);
210 >        joinPool(p);
211      }
212  
213      /**
214       * scheduleWithFixedDelay executes series of tasks with given period
215       */
216      public void testFixedDelaySequence() throws InterruptedException {
217 <        CustomExecutor p1 = new CustomExecutor(1);
217 >        CustomExecutor p = new CustomExecutor(1);
218          RunnableCounter counter = new RunnableCounter();
219          ScheduledFuture h =
220 <            p1.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS);
221 <        Thread.sleep(SMALL_DELAY_MS);
220 >            p.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS);
221 >        delay(SMALL_DELAY_MS);
222          h.cancel(true);
223          int c = counter.count.get();
224          assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
225          assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
226 <        joinPool(p1);
226 >        joinPool(p);
227      }
228  
192
229      /**
230 <     *  execute (null) throws NPE
230 >     * execute(null) throws NPE
231       */
232      public void testExecuteNull() throws InterruptedException {
233          CustomExecutor se = new CustomExecutor(1);
# Line 203 | Line 239 | public class ScheduledExecutorSubclassTe
239      }
240  
241      /**
242 <     * schedule (null) throws NPE
242 >     * schedule(null) throws NPE
243       */
244      public void testScheduleNull() throws InterruptedException {
245          CustomExecutor se = new CustomExecutor(1);
# Line 251 | Line 287 | public class ScheduledExecutorSubclassTe
287      /**
288       * schedule callable throws RejectedExecutionException if shutdown
289       */
290 <     public void testSchedule3_RejectedExecutionException() {
291 <         CustomExecutor se = new CustomExecutor(1);
292 <         try {
293 <             se.shutdown();
294 <             se.schedule(new NoOpCallable(),
295 <                         MEDIUM_DELAY_MS, MILLISECONDS);
296 <             shouldThrow();
297 <         } catch (RejectedExecutionException success) {
298 <         } catch (SecurityException ok) {
299 <         }
300 <         joinPool(se);
290 >    public void testSchedule3_RejectedExecutionException() {
291 >        CustomExecutor se = new CustomExecutor(1);
292 >        try {
293 >            se.shutdown();
294 >            se.schedule(new NoOpCallable(),
295 >                        MEDIUM_DELAY_MS, MILLISECONDS);
296 >            shouldThrow();
297 >        } catch (RejectedExecutionException success) {
298 >        } catch (SecurityException ok) {
299 >        }
300 >        joinPool(se);
301      }
302  
303      /**
304 <     *  scheduleAtFixedRate throws RejectedExecutionException if shutdown
304 >     * scheduleAtFixedRate throws RejectedExecutionException if shutdown
305       */
306      public void testScheduleAtFixedRate1_RejectedExecutionException() {
307          CustomExecutor se = new CustomExecutor(1);
# Line 297 | Line 333 | public class ScheduledExecutorSubclassTe
333      }
334  
335      /**
336 <     *  getActiveCount increases but doesn't overestimate, when a
337 <     *  thread becomes active
336 >     * getActiveCount increases but doesn't overestimate, when a
337 >     * thread becomes active
338       */
339      public void testGetActiveCount() throws InterruptedException {
340 <        CustomExecutor p2 = new CustomExecutor(2);
341 <        assertEquals(0, p2.getActiveCount());
342 <        p2.execute(new SmallRunnable());
343 <        Thread.sleep(SHORT_DELAY_MS);
344 <        assertEquals(1, p2.getActiveCount());
345 <        joinPool(p2);
340 >        final ThreadPoolExecutor p = new CustomExecutor(2);
341 >        final CountDownLatch threadStarted = new CountDownLatch(1);
342 >        final CountDownLatch done = new CountDownLatch(1);
343 >        try {
344 >            assertEquals(0, p.getActiveCount());
345 >            p.execute(new CheckedRunnable() {
346 >                public void realRun() throws InterruptedException {
347 >                    threadStarted.countDown();
348 >                    assertEquals(1, p.getActiveCount());
349 >                    done.await();
350 >                }});
351 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
352 >            assertEquals(1, p.getActiveCount());
353 >        } finally {
354 >            done.countDown();
355 >            joinPool(p);
356 >        }
357      }
358  
359      /**
360 <     *    getCompletedTaskCount increases, but doesn't overestimate,
361 <     *   when tasks complete
360 >     * getCompletedTaskCount increases, but doesn't overestimate,
361 >     * when tasks complete
362       */
363 <    public void testGetCompletedTaskCount()throws InterruptedException  {
364 <        CustomExecutor p2 = new CustomExecutor(2);
365 <        assertEquals(0, p2.getCompletedTaskCount());
366 <        p2.execute(new SmallRunnable());
367 <        Thread.sleep(MEDIUM_DELAY_MS);
368 <        assertEquals(1, p2.getCompletedTaskCount());
369 <        joinPool(p2);
363 >    public void testGetCompletedTaskCount() throws InterruptedException {
364 >        final ThreadPoolExecutor p = new CustomExecutor(2);
365 >        final CountDownLatch threadStarted = new CountDownLatch(1);
366 >        final CountDownLatch threadProceed = new CountDownLatch(1);
367 >        final CountDownLatch threadDone = new CountDownLatch(1);
368 >        try {
369 >            assertEquals(0, p.getCompletedTaskCount());
370 >            p.execute(new CheckedRunnable() {
371 >                public void realRun() throws InterruptedException {
372 >                    threadStarted.countDown();
373 >                    assertEquals(0, p.getCompletedTaskCount());
374 >                    threadProceed.await();
375 >                    threadDone.countDown();
376 >                }});
377 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
378 >            assertEquals(0, p.getCompletedTaskCount());
379 >            threadProceed.countDown();
380 >            threadDone.await();
381 >            delay(SHORT_DELAY_MS);
382 >            assertEquals(1, p.getCompletedTaskCount());
383 >        } finally {
384 >            joinPool(p);
385 >        }
386      }
387  
388      /**
389 <     *  getCorePoolSize returns size given in constructor if not otherwise set
389 >     * getCorePoolSize returns size given in constructor if not otherwise set
390       */
391      public void testGetCorePoolSize() {
392 <        CustomExecutor p1 = new CustomExecutor(1);
393 <        assertEquals(1, p1.getCorePoolSize());
394 <        joinPool(p1);
392 >        CustomExecutor p = new CustomExecutor(1);
393 >        assertEquals(1, p.getCorePoolSize());
394 >        joinPool(p);
395      }
396  
397      /**
398 <     *    getLargestPoolSize increases, but doesn't overestimate, when
399 <     *   multiple threads active
398 >     * getLargestPoolSize increases, but doesn't overestimate, when
399 >     * multiple threads active
400       */
401      public void testGetLargestPoolSize() throws InterruptedException {
402 <        CustomExecutor p2 = new CustomExecutor(2);
403 <        assertEquals(0, p2.getLargestPoolSize());
404 <        p2.execute(new SmallRunnable());
405 <        p2.execute(new SmallRunnable());
406 <        Thread.sleep(SHORT_DELAY_MS);
407 <        assertEquals(2, p2.getLargestPoolSize());
408 <        joinPool(p2);
402 >        final int THREADS = 3;
403 >        final ThreadPoolExecutor p = new CustomExecutor(THREADS);
404 >        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
405 >        final CountDownLatch done = new CountDownLatch(1);
406 >        try {
407 >            assertEquals(0, p.getLargestPoolSize());
408 >            for (int i = 0; i < THREADS; i++)
409 >                p.execute(new CheckedRunnable() {
410 >                    public void realRun() throws InterruptedException {
411 >                        threadsStarted.countDown();
412 >                        done.await();
413 >                        assertEquals(THREADS, p.getLargestPoolSize());
414 >                    }});
415 >            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
416 >            assertEquals(THREADS, p.getLargestPoolSize());
417 >        } finally {
418 >            done.countDown();
419 >            joinPool(p);
420 >            assertEquals(THREADS, p.getLargestPoolSize());
421 >        }
422      }
423  
424      /**
425 <     *   getPoolSize increases, but doesn't overestimate, when threads
426 <     *   become active
425 >     * getPoolSize increases, but doesn't overestimate, when threads
426 >     * become active
427       */
428 <    public void testGetPoolSize() {
429 <        CustomExecutor p1 = new CustomExecutor(1);
430 <        assertEquals(0, p1.getPoolSize());
431 <        p1.execute(new SmallRunnable());
432 <        assertEquals(1, p1.getPoolSize());
433 <        joinPool(p1);
428 >    public void testGetPoolSize() throws InterruptedException {
429 >        final ThreadPoolExecutor p = new CustomExecutor(1);
430 >        final CountDownLatch threadStarted = new CountDownLatch(1);
431 >        final CountDownLatch done = new CountDownLatch(1);
432 >        try {
433 >            assertEquals(0, p.getPoolSize());
434 >            p.execute(new CheckedRunnable() {
435 >                public void realRun() throws InterruptedException {
436 >                    threadStarted.countDown();
437 >                    assertEquals(1, p.getPoolSize());
438 >                    done.await();
439 >                }});
440 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
441 >            assertEquals(1, p.getPoolSize());
442 >        } finally {
443 >            done.countDown();
444 >            joinPool(p);
445 >        }
446      }
447  
448      /**
449 <     *    getTaskCount increases, but doesn't overestimate, when tasks
450 <     *    submitted
449 >     * getTaskCount increases, but doesn't overestimate, when tasks
450 >     * submitted
451       */
452      public void testGetTaskCount() throws InterruptedException {
453 <        CustomExecutor p1 = new CustomExecutor(1);
454 <        assertEquals(0, p1.getTaskCount());
455 <        for (int i = 0; i < 5; i++)
456 <            p1.execute(new SmallRunnable());
457 <        Thread.sleep(SHORT_DELAY_MS);
458 <        assertEquals(5, p1.getTaskCount());
459 <        joinPool(p1);
453 >        final ThreadPoolExecutor p = new CustomExecutor(1);
454 >        final CountDownLatch threadStarted = new CountDownLatch(1);
455 >        final CountDownLatch done = new CountDownLatch(1);
456 >        final int TASKS = 5;
457 >        try {
458 >            assertEquals(0, p.getTaskCount());
459 >            for (int i = 0; i < TASKS; i++)
460 >                p.execute(new CheckedRunnable() {
461 >                    public void realRun() throws InterruptedException {
462 >                        threadStarted.countDown();
463 >                        done.await();
464 >                    }});
465 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
466 >            assertEquals(TASKS, p.getTaskCount());
467 >        } finally {
468 >            done.countDown();
469 >            joinPool(p);
470 >        }
471      }
472  
473      /**
# Line 407 | Line 506 | public class ScheduledExecutorSubclassTe
506      }
507  
508      /**
509 <     *   is isShutDown is false before shutdown, true after
509 >     * isShutdown is false before shutdown, true after
510       */
511      public void testIsShutdown() {
512 <        CustomExecutor p1 = new CustomExecutor(1);
512 >        CustomExecutor p = new CustomExecutor(1);
513          try {
514 <            assertFalse(p1.isShutdown());
514 >            assertFalse(p.isShutdown());
515          }
516          finally {
517 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
517 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
518          }
519 <        assertTrue(p1.isShutdown());
519 >        assertTrue(p.isShutdown());
520      }
521  
423
522      /**
523 <     *  isTerminated is false before termination, true after
523 >     * isTerminated is false before termination, true after
524       */
525      public void testIsTerminated() throws InterruptedException {
526 <        CustomExecutor p1 = new CustomExecutor(1);
526 >        final ThreadPoolExecutor p = new CustomExecutor(1);
527 >        final CountDownLatch threadStarted = new CountDownLatch(1);
528 >        final CountDownLatch done = new CountDownLatch(1);
529 >        assertFalse(p.isTerminated());
530          try {
531 <            p1.execute(new SmallRunnable());
531 >            p.execute(new CheckedRunnable() {
532 >                public void realRun() throws InterruptedException {
533 >                    assertFalse(p.isTerminated());
534 >                    threadStarted.countDown();
535 >                    done.await();
536 >                }});
537 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
538 >            assertFalse(p.isTerminating());
539 >            done.countDown();
540          } finally {
541 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
541 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
542          }
543 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
544 <        assertTrue(p1.isTerminated());
543 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
544 >        assertTrue(p.isTerminated());
545      }
546  
547      /**
548 <     *  isTerminating is not true when running or when terminated
548 >     * isTerminating is not true when running or when terminated
549       */
550      public void testIsTerminating() throws InterruptedException {
551 <        CustomExecutor p1 = new CustomExecutor(1);
552 <        assertFalse(p1.isTerminating());
553 <        try {
554 <            p1.execute(new SmallRunnable());
555 <            assertFalse(p1.isTerminating());
556 <        } finally {
557 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
558 <        }
559 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
560 <        assertTrue(p1.isTerminated());
561 <        assertFalse(p1.isTerminating());
551 >        final ThreadPoolExecutor p = new CustomExecutor(1);
552 >        final CountDownLatch threadStarted = new CountDownLatch(1);
553 >        final CountDownLatch done = new CountDownLatch(1);
554 >        try {
555 >            assertFalse(p.isTerminating());
556 >            p.execute(new CheckedRunnable() {
557 >                public void realRun() throws InterruptedException {
558 >                    assertFalse(p.isTerminating());
559 >                    threadStarted.countDown();
560 >                    done.await();
561 >                }});
562 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
563 >            assertFalse(p.isTerminating());
564 >            done.countDown();
565 >        } finally {
566 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
567 >        }
568 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
569 >        assertTrue(p.isTerminated());
570 >        assertFalse(p.isTerminating());
571      }
572  
573      /**
574       * getQueue returns the work queue, which contains queued tasks
575       */
576      public void testGetQueue() throws InterruptedException {
577 <        CustomExecutor p1 = new CustomExecutor(1);
578 <        ScheduledFuture[] tasks = new ScheduledFuture[5];
579 <        for (int i = 0; i < 5; i++) {
580 <            tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS);
581 <        }
582 <        try {
583 <            Thread.sleep(SHORT_DELAY_MS);
584 <            BlockingQueue<Runnable> q = p1.getQueue();
585 <            assertTrue(q.contains(tasks[4]));
577 >        ScheduledThreadPoolExecutor p = new CustomExecutor(1);
578 >        final CountDownLatch threadStarted = new CountDownLatch(1);
579 >        final CountDownLatch done = new CountDownLatch(1);
580 >        try {
581 >            ScheduledFuture[] tasks = new ScheduledFuture[5];
582 >            for (int i = 0; i < tasks.length; i++) {
583 >                Runnable r = new CheckedRunnable() {
584 >                    public void realRun() throws InterruptedException {
585 >                        threadStarted.countDown();
586 >                        done.await();
587 >                    }};
588 >                tasks[i] = p.schedule(r, 1, MILLISECONDS);
589 >            }
590 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
591 >            BlockingQueue<Runnable> q = p.getQueue();
592 >            assertTrue(q.contains(tasks[tasks.length - 1]));
593              assertFalse(q.contains(tasks[0]));
594          } finally {
595 <            joinPool(p1);
595 >            done.countDown();
596 >            joinPool(p);
597          }
598      }
599  
# Line 475 | Line 601 | public class ScheduledExecutorSubclassTe
601       * remove(task) removes queued task, and fails to remove active task
602       */
603      public void testRemove() throws InterruptedException {
604 <        CustomExecutor p1 = new CustomExecutor(1);
604 >        final ScheduledThreadPoolExecutor p = new CustomExecutor(1);
605          ScheduledFuture[] tasks = new ScheduledFuture[5];
606 <        for (int i = 0; i < 5; i++) {
607 <            tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS);
482 <        }
606 >        final CountDownLatch threadStarted = new CountDownLatch(1);
607 >        final CountDownLatch done = new CountDownLatch(1);
608          try {
609 <            Thread.sleep(SHORT_DELAY_MS);
610 <            BlockingQueue<Runnable> q = p1.getQueue();
611 <            assertFalse(p1.remove((Runnable)tasks[0]));
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();
614 >                    }};
615 >                tasks[i] = p.schedule(r, 1, MILLISECONDS);
616 >            }
617 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
618 >            BlockingQueue<Runnable> q = p.getQueue();
619 >            assertFalse(p.remove((Runnable)tasks[0]));
620              assertTrue(q.contains((Runnable)tasks[4]));
621              assertTrue(q.contains((Runnable)tasks[3]));
622 <            assertTrue(p1.remove((Runnable)tasks[4]));
623 <            assertFalse(p1.remove((Runnable)tasks[4]));
622 >            assertTrue(p.remove((Runnable)tasks[4]));
623 >            assertFalse(p.remove((Runnable)tasks[4]));
624              assertFalse(q.contains((Runnable)tasks[4]));
625              assertTrue(q.contains((Runnable)tasks[3]));
626 <            assertTrue(p1.remove((Runnable)tasks[3]));
626 >            assertTrue(p.remove((Runnable)tasks[3]));
627              assertFalse(q.contains((Runnable)tasks[3]));
628          } finally {
629 <            joinPool(p1);
629 >            done.countDown();
630 >            joinPool(p);
631          }
632      }
633  
634      /**
635 <     *  purge removes cancelled tasks from the queue
635 >     * purge removes cancelled tasks from the queue
636       */
637      public void testPurge() throws InterruptedException {
638 <        CustomExecutor p1 = new CustomExecutor(1);
638 >        CustomExecutor p = new CustomExecutor(1);
639          ScheduledFuture[] tasks = new ScheduledFuture[5];
640 <        for (int i = 0; i < 5; i++) {
641 <            tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS);
642 <        }
640 >        for (int i = 0; i < tasks.length; i++)
641 >            tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(),
642 >                                  LONG_DELAY_MS, MILLISECONDS);
643          try {
644 <            int max = 5;
644 >            int max = tasks.length;
645              if (tasks[4].cancel(true)) --max;
646              if (tasks[3].cancel(true)) --max;
647              // There must eventually be an interference-free point at
648              // which purge will not fail. (At worst, when queue is empty.)
649 <            int k;
650 <            for (k = 0; k < SMALL_DELAY_MS; ++k) {
651 <                p1.purge();
652 <                long count = p1.getTaskCount();
653 <                if (count >= 0 && count <= max)
654 <                    break;
655 <                Thread.sleep(1);
656 <            }
523 <            assertTrue(k < SMALL_DELAY_MS);
649 >            long startTime = System.nanoTime();
650 >            do {
651 >                p.purge();
652 >                long count = p.getTaskCount();
653 >                if (count == max)
654 >                    return;
655 >            } while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
656 >            fail("Purge failed to remove cancelled tasks");
657          } finally {
658 <            joinPool(p1);
658 >            for (ScheduledFuture task : tasks)
659 >                task.cancel(true);
660 >            joinPool(p);
661          }
662      }
663  
664      /**
665 <     *  shutDownNow returns a list containing tasks that were not run
665 >     * shutdownNow returns a list containing tasks that were not run
666       */
667 <    public void testShutDownNow() {
668 <        CustomExecutor p1 = new CustomExecutor(1);
667 >    public void testShutdownNow() {
668 >        CustomExecutor p = new CustomExecutor(1);
669          for (int i = 0; i < 5; i++)
670 <            p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS);
671 <        List l;
670 >            p.schedule(new SmallPossiblyInterruptedRunnable(),
671 >                       LONG_DELAY_MS, MILLISECONDS);
672          try {
673 <            l = p1.shutdownNow();
673 >            List<Runnable> l = p.shutdownNow();
674 >            assertTrue(p.isShutdown());
675 >            assertEquals(5, l.size());
676          } catch (SecurityException ok) {
677 <            return;
677 >            // Allowed in case test doesn't have privs
678 >        } finally {
679 >            joinPool(p);
680          }
542        assertTrue(p1.isShutdown());
543        assertTrue(l.size() > 0 && l.size() <= 5);
544        joinPool(p1);
681      }
682  
683      /**
684       * In default setting, shutdown cancels periodic but not delayed
685       * tasks at shutdown
686       */
687 <    public void testShutDown1() throws InterruptedException {
688 <        CustomExecutor p1 = new CustomExecutor(1);
689 <        assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy());
690 <        assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy());
687 >    public void testShutdown1() throws InterruptedException {
688 >        CustomExecutor p = new CustomExecutor(1);
689 >        assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
690 >        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
691  
692          ScheduledFuture[] tasks = new ScheduledFuture[5];
693 <        for (int i = 0; i < 5; i++)
694 <            tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS);
695 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
696 <        BlockingQueue q = p1.getQueue();
697 <        for (Iterator it = q.iterator(); it.hasNext();) {
698 <            ScheduledFuture t = (ScheduledFuture)it.next();
699 <            assertFalse(t.isCancelled());
700 <        }
701 <        assertTrue(p1.isShutdown());
702 <        Thread.sleep(SMALL_DELAY_MS);
703 <        for (int i = 0; i < 5; ++i) {
704 <            assertTrue(tasks[i].isDone());
705 <            assertFalse(tasks[i].isCancelled());
693 >        for (int i = 0; i < tasks.length; i++)
694 >            tasks[i] = p.schedule(new NoOpRunnable(),
695 >                                  SHORT_DELAY_MS, MILLISECONDS);
696 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
697 >        BlockingQueue<Runnable> q = p.getQueue();
698 >        for (ScheduledFuture task : tasks) {
699 >            assertFalse(task.isDone());
700 >            assertFalse(task.isCancelled());
701 >            assertTrue(q.contains(task));
702 >        }
703 >        assertTrue(p.isShutdown());
704 >        assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
705 >        assertTrue(p.isTerminated());
706 >        for (ScheduledFuture task : tasks) {
707 >            assertTrue(task.isDone());
708 >            assertFalse(task.isCancelled());
709          }
710      }
711  
573
712      /**
713       * If setExecuteExistingDelayedTasksAfterShutdownPolicy is false,
714       * delayed tasks are cancelled at shutdown
715       */
716 <    public void testShutDown2() throws InterruptedException {
717 <        CustomExecutor p1 = new CustomExecutor(1);
718 <        p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
716 >    public void testShutdown2() throws InterruptedException {
717 >        CustomExecutor p = new CustomExecutor(1);
718 >        p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
719 >        assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
720 >        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
721          ScheduledFuture[] tasks = new ScheduledFuture[5];
722 <        for (int i = 0; i < 5; i++)
723 <            tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS);
724 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
725 <        assertTrue(p1.isShutdown());
726 <        BlockingQueue q = p1.getQueue();
722 >        for (int i = 0; i < tasks.length; i++)
723 >            tasks[i] = p.schedule(new NoOpRunnable(),
724 >                                  SHORT_DELAY_MS, MILLISECONDS);
725 >        BlockingQueue q = p.getQueue();
726 >        assertEquals(tasks.length, q.size());
727 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
728 >        assertTrue(p.isShutdown());
729          assertTrue(q.isEmpty());
730 <        Thread.sleep(SMALL_DELAY_MS);
731 <        assertTrue(p1.isTerminated());
730 >        assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
731 >        assertTrue(p.isTerminated());
732 >        for (ScheduledFuture task : tasks) {
733 >            assertTrue(task.isDone());
734 >            assertTrue(task.isCancelled());
735 >        }
736      }
737  
592
738      /**
739       * If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false,
740 <     * periodic tasks are not cancelled at shutdown
740 >     * periodic tasks are cancelled at shutdown
741       */
742 <    public void testShutDown3() throws InterruptedException {
743 <        CustomExecutor p1 = new CustomExecutor(1);
744 <        p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
742 >    public void testShutdown3() throws InterruptedException {
743 >        CustomExecutor p = new CustomExecutor(1);
744 >        assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
745 >        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
746 >        p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
747 >        assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
748 >        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
749 >        long initialDelay = LONG_DELAY_MS;
750          ScheduledFuture task =
751 <            p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, MILLISECONDS);
752 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
753 <        assertTrue(p1.isShutdown());
754 <        BlockingQueue q = p1.getQueue();
755 <        assertTrue(q.isEmpty());
756 <        Thread.sleep(SHORT_DELAY_MS);
757 <        assertTrue(p1.isTerminated());
751 >            p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay,
752 >                                  5, MILLISECONDS);
753 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
754 >        assertTrue(p.isShutdown());
755 >        assertTrue(p.getQueue().isEmpty());
756 >        assertTrue(task.isDone());
757 >        assertTrue(task.isCancelled());
758 >        joinPool(p);
759      }
760  
761      /**
762       * if setContinueExistingPeriodicTasksAfterShutdownPolicy is true,
763 <     * periodic tasks are cancelled at shutdown
763 >     * periodic tasks are not cancelled at shutdown
764       */
765 <    public void testShutDown4() throws InterruptedException {
766 <        CustomExecutor p1 = new CustomExecutor(1);
765 >    public void testShutdown4() throws InterruptedException {
766 >        CustomExecutor p = new CustomExecutor(1);
767 >        final CountDownLatch counter = new CountDownLatch(2);
768          try {
769 <            p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
769 >            p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
770 >            assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
771 >            assertTrue(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
772 >            final Runnable r = new CheckedRunnable() {
773 >                public void realRun() {
774 >                    counter.countDown();
775 >                }};
776              ScheduledFuture task =
777 <                p1.scheduleAtFixedRate(new NoOpRunnable(), 1, 1, MILLISECONDS);
777 >                p.scheduleAtFixedRate(r, 1, 1, MILLISECONDS);
778 >            assertFalse(task.isDone());
779              assertFalse(task.isCancelled());
780 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
780 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
781              assertFalse(task.isCancelled());
782 <            assertFalse(p1.isTerminated());
783 <            assertTrue(p1.isShutdown());
784 <            Thread.sleep(SHORT_DELAY_MS);
782 >            assertFalse(p.isTerminated());
783 >            assertTrue(p.isShutdown());
784 >            assertTrue(counter.await(SMALL_DELAY_MS, MILLISECONDS));
785              assertFalse(task.isCancelled());
786 <            assertTrue(task.cancel(true));
786 >            assertTrue(task.cancel(false));
787              assertTrue(task.isDone());
788 <            Thread.sleep(SHORT_DELAY_MS);
789 <            assertTrue(p1.isTerminated());
788 >            assertTrue(task.isCancelled());
789 >            assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
790 >            assertTrue(p.isTerminated());
791          }
792          finally {
793 <            joinPool(p1);
793 >            joinPool(p);
794          }
795      }
796  
# Line 708 | Line 868 | public class ScheduledExecutorSubclassTe
868       * invokeAny(c) throws NPE if c has null elements
869       */
870      public void testInvokeAny3() throws Exception {
871 <        final CountDownLatch latch = new CountDownLatch(1);
871 >        CountDownLatch latch = new CountDownLatch(1);
872          ExecutorService e = new CustomExecutor(2);
873 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
874 +        l.add(latchAwaitingStringTask(latch));
875 +        l.add(null);
876          try {
714            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
715            l.add(new Callable<String>() {
716                      public String call() {
717                          try {
718                              latch.await();
719                          } catch (InterruptedException quittingTime) {}
720                          return TEST_STRING;
721                      }});
722            l.add(null);
877              e.invokeAny(l);
878              shouldThrow();
879          } catch (NullPointerException success) {
# Line 734 | Line 888 | public class ScheduledExecutorSubclassTe
888       */
889      public void testInvokeAny4() throws Exception {
890          ExecutorService e = new CustomExecutor(2);
891 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
892 +        l.add(new NPETask());
893          try {
738            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
739            l.add(new NPETask());
894              e.invokeAny(l);
895              shouldThrow();
896          } catch (ExecutionException success) {
# Line 752 | Line 906 | public class ScheduledExecutorSubclassTe
906      public void testInvokeAny5() throws Exception {
907          ExecutorService e = new CustomExecutor(2);
908          try {
909 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
909 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
910              l.add(new StringTask());
911              l.add(new StringTask());
912              String result = e.invokeAny(l);
# Line 794 | Line 948 | public class ScheduledExecutorSubclassTe
948       */
949      public void testInvokeAll3() throws Exception {
950          ExecutorService e = new CustomExecutor(2);
951 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
952 +        l.add(new StringTask());
953 +        l.add(null);
954          try {
798            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
799            l.add(new StringTask());
800            l.add(null);
955              e.invokeAll(l);
956              shouldThrow();
957          } catch (NullPointerException success) {
# Line 811 | Line 965 | public class ScheduledExecutorSubclassTe
965       */
966      public void testInvokeAll4() throws Exception {
967          ExecutorService e = new CustomExecutor(2);
968 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
969 +        l.add(new NPETask());
970 +        List<Future<String>> futures = e.invokeAll(l);
971 +        assertEquals(1, futures.size());
972          try {
973 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
816 <            l.add(new NPETask());
817 <            List<Future<String>> result = e.invokeAll(l);
818 <            assertEquals(1, result.size());
819 <            for (Future<String> future : result)
820 <                future.get();
973 >            futures.get(0).get();
974              shouldThrow();
975          } catch (ExecutionException success) {
976              assertTrue(success.getCause() instanceof NullPointerException);
# Line 832 | Line 985 | public class ScheduledExecutorSubclassTe
985      public void testInvokeAll5() throws Exception {
986          ExecutorService e = new CustomExecutor(2);
987          try {
988 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
988 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
989              l.add(new StringTask());
990              l.add(new StringTask());
991 <            List<Future<String>> result = e.invokeAll(l);
992 <            assertEquals(2, result.size());
993 <            for (Future<String> future : result)
991 >            List<Future<String>> futures = e.invokeAll(l);
992 >            assertEquals(2, futures.size());
993 >            for (Future<String> future : futures)
994                  assertSame(TEST_STRING, future.get());
995          } finally {
996              joinPool(e);
# Line 863 | Line 1016 | public class ScheduledExecutorSubclassTe
1016       */
1017      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1018          ExecutorService e = new CustomExecutor(2);
1019 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1020 +        l.add(new StringTask());
1021          try {
867            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
868            l.add(new StringTask());
1022              e.invokeAny(l, MEDIUM_DELAY_MS, null);
1023              shouldThrow();
1024          } catch (NullPointerException success) {
# Line 892 | Line 1045 | public class ScheduledExecutorSubclassTe
1045       * timed invokeAny(c) throws NPE if c has null elements
1046       */
1047      public void testTimedInvokeAny3() throws Exception {
1048 <        final CountDownLatch latch = new CountDownLatch(1);
1048 >        CountDownLatch latch = new CountDownLatch(1);
1049          ExecutorService e = new CustomExecutor(2);
1050 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1051 +        l.add(latchAwaitingStringTask(latch));
1052 +        l.add(null);
1053          try {
898            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
899            l.add(new Callable<String>() {
900                      public String call() {
901                          try {
902                              latch.await();
903                          } catch (InterruptedException quittingTime) {}
904                          return TEST_STRING;
905                      }});
906            l.add(null);
1054              e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1055              shouldThrow();
1056          } catch (NullPointerException success) {
# Line 918 | Line 1065 | public class ScheduledExecutorSubclassTe
1065       */
1066      public void testTimedInvokeAny4() throws Exception {
1067          ExecutorService e = new CustomExecutor(2);
1068 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1069 +        l.add(new NPETask());
1070          try {
922            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
923            l.add(new NPETask());
1071              e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1072              shouldThrow();
1073          } catch (ExecutionException success) {
# Line 936 | Line 1083 | public class ScheduledExecutorSubclassTe
1083      public void testTimedInvokeAny5() throws Exception {
1084          ExecutorService e = new CustomExecutor(2);
1085          try {
1086 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1086 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1087              l.add(new StringTask());
1088              l.add(new StringTask());
1089              String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
# Line 965 | Line 1112 | public class ScheduledExecutorSubclassTe
1112       */
1113      public void testTimedInvokeAllNullTimeUnit() throws Exception {
1114          ExecutorService e = new CustomExecutor(2);
1115 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1116 +        l.add(new StringTask());
1117          try {
969            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
970            l.add(new StringTask());
1118              e.invokeAll(l, MEDIUM_DELAY_MS, null);
1119              shouldThrow();
1120          } catch (NullPointerException success) {
# Line 994 | Line 1141 | public class ScheduledExecutorSubclassTe
1141       */
1142      public void testTimedInvokeAll3() throws Exception {
1143          ExecutorService e = new CustomExecutor(2);
1144 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1145 +        l.add(new StringTask());
1146 +        l.add(null);
1147          try {
998            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
999            l.add(new StringTask());
1000            l.add(null);
1148              e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1149              shouldThrow();
1150          } catch (NullPointerException success) {
# Line 1011 | Line 1158 | public class ScheduledExecutorSubclassTe
1158       */
1159      public void testTimedInvokeAll4() throws Exception {
1160          ExecutorService e = new CustomExecutor(2);
1161 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1162 +        l.add(new NPETask());
1163 +        List<Future<String>> futures =
1164 +            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1165 +        assertEquals(1, futures.size());
1166          try {
1167 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1016 <            l.add(new NPETask());
1017 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1018 <            assertEquals(1, result.size());
1019 <            for (Future<String> future : result)
1020 <                future.get();
1167 >            futures.get(0).get();
1168              shouldThrow();
1169          } catch (ExecutionException success) {
1170              assertTrue(success.getCause() instanceof NullPointerException);
# Line 1032 | Line 1179 | public class ScheduledExecutorSubclassTe
1179      public void testTimedInvokeAll5() throws Exception {
1180          ExecutorService e = new CustomExecutor(2);
1181          try {
1182 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1182 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1183              l.add(new StringTask());
1184              l.add(new StringTask());
1185 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1186 <            assertEquals(2, result.size());
1187 <            for (Future<String> future : result)
1185 >            List<Future<String>> futures =
1186 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1187 >            assertEquals(2, futures.size());
1188 >            for (Future<String> future : futures)
1189                  assertSame(TEST_STRING, future.get());
1190          } finally {
1191              joinPool(e);
# Line 1050 | Line 1198 | public class ScheduledExecutorSubclassTe
1198      public void testTimedInvokeAll6() throws Exception {
1199          ExecutorService e = new CustomExecutor(2);
1200          try {
1201 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1201 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1202              l.add(new StringTask());
1203              l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1204              l.add(new StringTask());
1205 <            List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1206 <            assertEquals(3, result.size());
1207 <            Iterator<Future<String>> it = result.iterator();
1205 >            List<Future<String>> futures =
1206 >                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1207 >            assertEquals(3, futures.size());
1208 >            Iterator<Future<String>> it = futures.iterator();
1209              Future<String> f1 = it.next();
1210              Future<String> f2 = it.next();
1211              Future<String> f3 = it.next();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines