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.7 by jsr166, Sat Nov 21 02:07:27 2009 UTC vs.
Revision 1.31 by jsr166, Tue Sep 24 18:35:21 2013 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.*;
8   import java.util.*;
9   import java.util.concurrent.*;
10   import static java.util.concurrent.TimeUnit.MILLISECONDS;
11 < import java.util.concurrent.atomic.*;
11 > import java.util.concurrent.atomic.AtomicInteger;
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 36 | Line 36 | public class ScheduledExecutorSubclassTe
36          }
37          public boolean isCancelled() { return task.isCancelled(); }
38          public boolean isDone() { return task.isDone(); }
39 <        public V get() throws InterruptedException,  ExecutionException {
39 >        public V get() throws InterruptedException, ExecutionException {
40              V v = task.get();
41              assertTrue(ran);
42              return v;
43          }
44 <        public V get(long time, TimeUnit unit) throws InterruptedException,  ExecutionException, TimeoutException {
44 >        public V get(long time, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
45              V v = task.get(time, unit);
46              assertTrue(ran);
47              return v;
48          }
49      }
50  
51
51      public class CustomExecutor extends ScheduledThreadPoolExecutor {
52  
53          protected <V> RunnableScheduledFuture<V> decorateTask(Runnable r, RunnableScheduledFuture<V> task) {
# Line 58 | Line 57 | public class ScheduledExecutorSubclassTe
57          protected <V> RunnableScheduledFuture<V> decorateTask(Callable<V> c, RunnableScheduledFuture<V> task) {
58              return new CustomTask<V>(task);
59          }
60 <        CustomExecutor(int corePoolSize) { super(corePoolSize);}
60 >        CustomExecutor(int corePoolSize) { super(corePoolSize); }
61          CustomExecutor(int corePoolSize, RejectedExecutionHandler handler) {
62              super(corePoolSize, handler);
63          }
# 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 startTime = System.nanoTime();
99 >        final CountDownLatch done = new CountDownLatch(1);
100 >        try {
101 >            Callable task = new CheckedCallable<Boolean>() {
102 >                public Boolean realCall() {
103 >                    done.countDown();
104 >                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
105 >                    return Boolean.TRUE;
106 >                }};
107 >            Future f = p.schedule(task, timeoutMillis(), MILLISECONDS);
108 >            assertSame(Boolean.TRUE, f.get());
109 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
110 >            assertTrue(done.await(0L, MILLISECONDS));
111 >        } finally {
112 >            joinPool(p);
113 >        }
114      }
115  
116      /**
117 <     *  delayed schedule of runnable successfully executes after delay
118 <     */
119 <    public void testSchedule3() throws InterruptedException {
120 <        TrackedShortRunnable runnable = new TrackedShortRunnable();
121 <        CustomExecutor p1 = new CustomExecutor(1);
122 <        p1.schedule(runnable, SMALL_DELAY_MS, MILLISECONDS);
123 <        Thread.sleep(SHORT_DELAY_MS);
124 <        assertFalse(runnable.done);
125 <        Thread.sleep(MEDIUM_DELAY_MS);
126 <        assertTrue(runnable.done);
127 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
128 <        joinPool(p1);
117 >     * delayed schedule of runnable successfully executes after delay
118 >     */
119 >    public void testSchedule3() throws Exception {
120 >        CustomExecutor p = new CustomExecutor(1);
121 >        final long startTime = System.nanoTime();
122 >        final CountDownLatch done = new CountDownLatch(1);
123 >        try {
124 >            Runnable task = new CheckedRunnable() {
125 >                public void realRun() {
126 >                    done.countDown();
127 >                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
128 >                }};
129 >            Future f = p.schedule(task, timeoutMillis(), MILLISECONDS);
130 >            await(done);
131 >            assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
132 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
133 >        } finally {
134 >            joinPool(p);
135 >        }
136      }
137  
138      /**
139       * scheduleAtFixedRate executes runnable after given initial delay
140       */
141      public void testSchedule4() throws InterruptedException {
142 <        TrackedShortRunnable runnable = new TrackedShortRunnable();
143 <        CustomExecutor p1 = new CustomExecutor(1);
144 <        ScheduledFuture h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS);
145 <        assertFalse(runnable.done);
146 <        Thread.sleep(MEDIUM_DELAY_MS);
147 <        assertTrue(runnable.done);
148 <        h.cancel(true);
149 <        joinPool(p1);
150 <    }
151 <
152 <    static class RunnableCounter implements Runnable {
153 <        AtomicInteger count = new AtomicInteger(0);
154 <        public void run() { count.getAndIncrement(); }
142 >        CustomExecutor p = new CustomExecutor(1);
143 >        final long startTime = System.nanoTime();
144 >        final CountDownLatch done = new CountDownLatch(1);
145 >        try {
146 >            Runnable task = new CheckedRunnable() {
147 >                public void realRun() {
148 >                    done.countDown();
149 >                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
150 >                }};
151 >            ScheduledFuture f =
152 >                p.scheduleAtFixedRate(task, timeoutMillis(),
153 >                                      LONG_DELAY_MS, MILLISECONDS);
154 >            await(done);
155 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
156 >            f.cancel(true);
157 >        } finally {
158 >            joinPool(p);
159 >        }
160      }
161  
162      /**
163       * scheduleWithFixedDelay executes runnable after given initial delay
164       */
165      public void testSchedule5() throws InterruptedException {
166 <        TrackedShortRunnable runnable = new TrackedShortRunnable();
167 <        CustomExecutor p1 = new CustomExecutor(1);
168 <        ScheduledFuture h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS);
169 <        assertFalse(runnable.done);
170 <        Thread.sleep(MEDIUM_DELAY_MS);
171 <        assertTrue(runnable.done);
172 <        h.cancel(true);
173 <        joinPool(p1);
166 >        CustomExecutor p = new CustomExecutor(1);
167 >        final long startTime = System.nanoTime();
168 >        final CountDownLatch done = new CountDownLatch(1);
169 >        try {
170 >            Runnable task = new CheckedRunnable() {
171 >                public void realRun() {
172 >                    done.countDown();
173 >                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
174 >                }};
175 >            ScheduledFuture f =
176 >                p.scheduleWithFixedDelay(task, timeoutMillis(),
177 >                                         LONG_DELAY_MS, MILLISECONDS);
178 >            await(done);
179 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
180 >            f.cancel(true);
181 >        } finally {
182 >            joinPool(p);
183 >        }
184 >    }
185 >
186 >    static class RunnableCounter implements Runnable {
187 >        AtomicInteger count = new AtomicInteger(0);
188 >        public void run() { count.getAndIncrement(); }
189      }
190  
191      /**
192       * scheduleAtFixedRate executes series of tasks at given rate
193       */
194      public void testFixedRateSequence() throws InterruptedException {
195 <        CustomExecutor p1 = new CustomExecutor(1);
196 <        RunnableCounter counter = new RunnableCounter();
197 <        ScheduledFuture h =
198 <            p1.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS);
199 <        Thread.sleep(SMALL_DELAY_MS);
200 <        h.cancel(true);
201 <        int c = counter.count.get();
202 <        // By time scaling conventions, we must have at least
203 <        // an execution per SHORT delay, but no more than one SHORT more
204 <        assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
205 <        assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
206 <        joinPool(p1);
195 >        CustomExecutor p = new CustomExecutor(1);
196 >        try {
197 >            for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
198 >                long startTime = System.nanoTime();
199 >                int cycles = 10;
200 >                final CountDownLatch done = new CountDownLatch(cycles);
201 >                CheckedRunnable task = new CheckedRunnable() {
202 >                    public void realRun() { done.countDown(); }};
203 >                
204 >                ScheduledFuture h =
205 >                    p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS);
206 >                done.await();
207 >                h.cancel(true);
208 >                double normalizedTime =
209 >                    (double) millisElapsedSince(startTime) / delay;
210 >                if (normalizedTime >= cycles - 1 &&
211 >                    normalizedTime <= cycles)
212 >                    return;
213 >            }
214 >            throw new AssertionError("unexpected execution rate");
215 >        } finally {
216 >            joinPool(p);
217 >        }
218      }
219  
220      /**
221       * scheduleWithFixedDelay executes series of tasks with given period
222       */
223      public void testFixedDelaySequence() throws InterruptedException {
224 <        CustomExecutor p1 = new CustomExecutor(1);
225 <        RunnableCounter counter = new RunnableCounter();
226 <        ScheduledFuture h =
227 <            p1.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS);
228 <        Thread.sleep(SMALL_DELAY_MS);
229 <        h.cancel(true);
230 <        int c = counter.count.get();
231 <        assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
232 <        assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
233 <        joinPool(p1);
224 >        CustomExecutor p = new CustomExecutor(1);
225 >        try {
226 >            for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
227 >                long startTime = System.nanoTime();
228 >                int cycles = 10;
229 >                final CountDownLatch done = new CountDownLatch(cycles);
230 >                CheckedRunnable task = new CheckedRunnable() {
231 >                    public void realRun() { done.countDown(); }};
232 >                
233 >                ScheduledFuture h =
234 >                    p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS);
235 >                done.await();
236 >                h.cancel(true);
237 >                double normalizedTime =
238 >                    (double) millisElapsedSince(startTime) / delay;
239 >                if (normalizedTime >= cycles - 1 &&
240 >                    normalizedTime <= cycles)
241 >                    return;
242 >            }
243 >            throw new AssertionError("unexpected execution rate");
244 >        } finally {
245 >            joinPool(p);
246 >        }
247      }
248  
192
249      /**
250 <     *  execute (null) throws NPE
250 >     * execute(null) throws NPE
251       */
252      public void testExecuteNull() throws InterruptedException {
253          CustomExecutor se = new CustomExecutor(1);
# Line 203 | Line 259 | public class ScheduledExecutorSubclassTe
259      }
260  
261      /**
262 <     * schedule (null) throws NPE
262 >     * schedule(null) throws NPE
263       */
264      public void testScheduleNull() throws InterruptedException {
265          CustomExecutor se = new CustomExecutor(1);
# Line 251 | Line 307 | public class ScheduledExecutorSubclassTe
307      /**
308       * schedule callable throws RejectedExecutionException if shutdown
309       */
310 <     public void testSchedule3_RejectedExecutionException() {
311 <         CustomExecutor se = new CustomExecutor(1);
312 <         try {
313 <             se.shutdown();
314 <             se.schedule(new NoOpCallable(),
315 <                         MEDIUM_DELAY_MS, MILLISECONDS);
316 <             shouldThrow();
317 <         } catch (RejectedExecutionException success) {
318 <         } catch (SecurityException ok) {
319 <         }
320 <         joinPool(se);
310 >    public void testSchedule3_RejectedExecutionException() {
311 >        CustomExecutor se = new CustomExecutor(1);
312 >        try {
313 >            se.shutdown();
314 >            se.schedule(new NoOpCallable(),
315 >                        MEDIUM_DELAY_MS, MILLISECONDS);
316 >            shouldThrow();
317 >        } catch (RejectedExecutionException success) {
318 >        } catch (SecurityException ok) {
319 >        }
320 >        joinPool(se);
321      }
322  
323      /**
324 <     *  scheduleAtFixedRate throws RejectedExecutionException if shutdown
324 >     * scheduleAtFixedRate throws RejectedExecutionException if shutdown
325       */
326      public void testScheduleAtFixedRate1_RejectedExecutionException() {
327          CustomExecutor se = new CustomExecutor(1);
# Line 297 | Line 353 | public class ScheduledExecutorSubclassTe
353      }
354  
355      /**
356 <     *  getActiveCount increases but doesn't overestimate, when a
357 <     *  thread becomes active
356 >     * getActiveCount increases but doesn't overestimate, when a
357 >     * thread becomes active
358       */
359      public void testGetActiveCount() throws InterruptedException {
360 <        CustomExecutor p2 = new CustomExecutor(2);
361 <        assertEquals(0, p2.getActiveCount());
362 <        p2.execute(new SmallRunnable());
363 <        Thread.sleep(SHORT_DELAY_MS);
364 <        assertEquals(1, p2.getActiveCount());
365 <        joinPool(p2);
360 >        final ThreadPoolExecutor p = new CustomExecutor(2);
361 >        final CountDownLatch threadStarted = new CountDownLatch(1);
362 >        final CountDownLatch done = new CountDownLatch(1);
363 >        try {
364 >            assertEquals(0, p.getActiveCount());
365 >            p.execute(new CheckedRunnable() {
366 >                public void realRun() throws InterruptedException {
367 >                    threadStarted.countDown();
368 >                    assertEquals(1, p.getActiveCount());
369 >                    done.await();
370 >                }});
371 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
372 >            assertEquals(1, p.getActiveCount());
373 >        } finally {
374 >            done.countDown();
375 >            joinPool(p);
376 >        }
377      }
378  
379      /**
380 <     *    getCompletedTaskCount increases, but doesn't overestimate,
381 <     *   when tasks complete
380 >     * getCompletedTaskCount increases, but doesn't overestimate,
381 >     * when tasks complete
382       */
383 <    public void testGetCompletedTaskCount()throws InterruptedException  {
384 <        CustomExecutor p2 = new CustomExecutor(2);
385 <        assertEquals(0, p2.getCompletedTaskCount());
386 <        p2.execute(new SmallRunnable());
387 <        Thread.sleep(MEDIUM_DELAY_MS);
388 <        assertEquals(1, p2.getCompletedTaskCount());
389 <        joinPool(p2);
383 >    public void testGetCompletedTaskCount() throws InterruptedException {
384 >        final ThreadPoolExecutor p = new CustomExecutor(2);
385 >        final CountDownLatch threadStarted = new CountDownLatch(1);
386 >        final CountDownLatch threadProceed = new CountDownLatch(1);
387 >        final CountDownLatch threadDone = new CountDownLatch(1);
388 >        try {
389 >            assertEquals(0, p.getCompletedTaskCount());
390 >            p.execute(new CheckedRunnable() {
391 >                public void realRun() throws InterruptedException {
392 >                    threadStarted.countDown();
393 >                    assertEquals(0, p.getCompletedTaskCount());
394 >                    threadProceed.await();
395 >                    threadDone.countDown();
396 >                }});
397 >            await(threadStarted);
398 >            assertEquals(0, p.getCompletedTaskCount());
399 >            threadProceed.countDown();
400 >            threadDone.await();
401 >            long startTime = System.nanoTime();
402 >            while (p.getCompletedTaskCount() != 1) {
403 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
404 >                    fail("timed out");
405 >                Thread.yield();
406 >            }
407 >        } finally {
408 >            joinPool(p);
409 >        }
410      }
411  
412      /**
413 <     *  getCorePoolSize returns size given in constructor if not otherwise set
413 >     * getCorePoolSize returns size given in constructor if not otherwise set
414       */
415      public void testGetCorePoolSize() {
416 <        CustomExecutor p1 = new CustomExecutor(1);
417 <        assertEquals(1, p1.getCorePoolSize());
418 <        joinPool(p1);
416 >        CustomExecutor p = new CustomExecutor(1);
417 >        assertEquals(1, p.getCorePoolSize());
418 >        joinPool(p);
419      }
420  
421      /**
422 <     *    getLargestPoolSize increases, but doesn't overestimate, when
423 <     *   multiple threads active
422 >     * getLargestPoolSize increases, but doesn't overestimate, when
423 >     * multiple threads active
424       */
425      public void testGetLargestPoolSize() throws InterruptedException {
426 <        CustomExecutor p2 = new CustomExecutor(2);
427 <        assertEquals(0, p2.getLargestPoolSize());
428 <        p2.execute(new SmallRunnable());
429 <        p2.execute(new SmallRunnable());
430 <        Thread.sleep(SHORT_DELAY_MS);
431 <        assertEquals(2, p2.getLargestPoolSize());
432 <        joinPool(p2);
426 >        final int THREADS = 3;
427 >        final ThreadPoolExecutor p = new CustomExecutor(THREADS);
428 >        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
429 >        final CountDownLatch done = new CountDownLatch(1);
430 >        try {
431 >            assertEquals(0, p.getLargestPoolSize());
432 >            for (int i = 0; i < THREADS; i++)
433 >                p.execute(new CheckedRunnable() {
434 >                    public void realRun() throws InterruptedException {
435 >                        threadsStarted.countDown();
436 >                        done.await();
437 >                        assertEquals(THREADS, p.getLargestPoolSize());
438 >                    }});
439 >            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
440 >            assertEquals(THREADS, p.getLargestPoolSize());
441 >        } finally {
442 >            done.countDown();
443 >            joinPool(p);
444 >            assertEquals(THREADS, p.getLargestPoolSize());
445 >        }
446      }
447  
448      /**
449 <     *   getPoolSize increases, but doesn't overestimate, when threads
450 <     *   become active
449 >     * getPoolSize increases, but doesn't overestimate, when threads
450 >     * become active
451       */
452 <    public void testGetPoolSize() {
453 <        CustomExecutor p1 = new CustomExecutor(1);
454 <        assertEquals(0, p1.getPoolSize());
455 <        p1.execute(new SmallRunnable());
456 <        assertEquals(1, p1.getPoolSize());
457 <        joinPool(p1);
452 >    public void testGetPoolSize() throws InterruptedException {
453 >        final ThreadPoolExecutor p = new CustomExecutor(1);
454 >        final CountDownLatch threadStarted = new CountDownLatch(1);
455 >        final CountDownLatch done = new CountDownLatch(1);
456 >        try {
457 >            assertEquals(0, p.getPoolSize());
458 >            p.execute(new CheckedRunnable() {
459 >                public void realRun() throws InterruptedException {
460 >                    threadStarted.countDown();
461 >                    assertEquals(1, p.getPoolSize());
462 >                    done.await();
463 >                }});
464 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
465 >            assertEquals(1, p.getPoolSize());
466 >        } finally {
467 >            done.countDown();
468 >            joinPool(p);
469 >        }
470      }
471  
472      /**
473 <     *    getTaskCount increases, but doesn't overestimate, when tasks
474 <     *    submitted
473 >     * getTaskCount increases, but doesn't overestimate, when tasks
474 >     * submitted
475       */
476      public void testGetTaskCount() throws InterruptedException {
477 <        CustomExecutor p1 = new CustomExecutor(1);
478 <        assertEquals(0, p1.getTaskCount());
479 <        for (int i = 0; i < 5; i++)
480 <            p1.execute(new SmallRunnable());
481 <        Thread.sleep(SHORT_DELAY_MS);
482 <        assertEquals(5, p1.getTaskCount());
483 <        joinPool(p1);
477 >        final ThreadPoolExecutor p = new CustomExecutor(1);
478 >        final CountDownLatch threadStarted = new CountDownLatch(1);
479 >        final CountDownLatch done = new CountDownLatch(1);
480 >        final int TASKS = 5;
481 >        try {
482 >            assertEquals(0, p.getTaskCount());
483 >            for (int i = 0; i < TASKS; i++)
484 >                p.execute(new CheckedRunnable() {
485 >                    public void realRun() throws InterruptedException {
486 >                        threadStarted.countDown();
487 >                        done.await();
488 >                    }});
489 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
490 >            assertEquals(TASKS, p.getTaskCount());
491 >        } finally {
492 >            done.countDown();
493 >            joinPool(p);
494 >        }
495      }
496  
497      /**
# Line 407 | Line 530 | public class ScheduledExecutorSubclassTe
530      }
531  
532      /**
533 <     *   is isShutDown is false before shutdown, true after
533 >     * isShutdown is false before shutdown, true after
534       */
535      public void testIsShutdown() {
536 <        CustomExecutor p1 = new CustomExecutor(1);
536 >        CustomExecutor p = new CustomExecutor(1);
537          try {
538 <            assertFalse(p1.isShutdown());
538 >            assertFalse(p.isShutdown());
539          }
540          finally {
541 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
541 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
542          }
543 <        assertTrue(p1.isShutdown());
543 >        assertTrue(p.isShutdown());
544      }
545  
423
546      /**
547 <     *  isTerminated is false before termination, true after
547 >     * isTerminated is false before termination, true after
548       */
549      public void testIsTerminated() throws InterruptedException {
550 <        CustomExecutor p1 = new CustomExecutor(1);
550 >        final ThreadPoolExecutor p = new CustomExecutor(1);
551 >        final CountDownLatch threadStarted = new CountDownLatch(1);
552 >        final CountDownLatch done = new CountDownLatch(1);
553 >        assertFalse(p.isTerminated());
554          try {
555 <            p1.execute(new SmallRunnable());
555 >            p.execute(new CheckedRunnable() {
556 >                public void realRun() throws InterruptedException {
557 >                    assertFalse(p.isTerminated());
558 >                    threadStarted.countDown();
559 >                    done.await();
560 >                }});
561 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
562 >            assertFalse(p.isTerminating());
563 >            done.countDown();
564          } finally {
565 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
565 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
566          }
567 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
568 <        assertTrue(p1.isTerminated());
567 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
568 >        assertTrue(p.isTerminated());
569      }
570  
571      /**
572 <     *  isTerminating is not true when running or when terminated
572 >     * isTerminating is not true when running or when terminated
573       */
574      public void testIsTerminating() throws InterruptedException {
575 <        CustomExecutor p1 = new CustomExecutor(1);
576 <        assertFalse(p1.isTerminating());
577 <        try {
578 <            p1.execute(new SmallRunnable());
579 <            assertFalse(p1.isTerminating());
580 <        } finally {
581 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
582 <        }
583 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
584 <        assertTrue(p1.isTerminated());
585 <        assertFalse(p1.isTerminating());
575 >        final ThreadPoolExecutor p = new CustomExecutor(1);
576 >        final CountDownLatch threadStarted = new CountDownLatch(1);
577 >        final CountDownLatch done = new CountDownLatch(1);
578 >        try {
579 >            assertFalse(p.isTerminating());
580 >            p.execute(new CheckedRunnable() {
581 >                public void realRun() throws InterruptedException {
582 >                    assertFalse(p.isTerminating());
583 >                    threadStarted.countDown();
584 >                    done.await();
585 >                }});
586 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
587 >            assertFalse(p.isTerminating());
588 >            done.countDown();
589 >        } finally {
590 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
591 >        }
592 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
593 >        assertTrue(p.isTerminated());
594 >        assertFalse(p.isTerminating());
595      }
596  
597      /**
598       * getQueue returns the work queue, which contains queued tasks
599       */
600      public void testGetQueue() throws InterruptedException {
601 <        CustomExecutor p1 = new CustomExecutor(1);
602 <        ScheduledFuture[] tasks = new ScheduledFuture[5];
603 <        for (int i = 0; i < 5; i++) {
604 <            tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS);
605 <        }
606 <        try {
607 <            Thread.sleep(SHORT_DELAY_MS);
608 <            BlockingQueue<Runnable> q = p1.getQueue();
609 <            assertTrue(q.contains(tasks[4]));
601 >        ScheduledThreadPoolExecutor p = new CustomExecutor(1);
602 >        final CountDownLatch threadStarted = new CountDownLatch(1);
603 >        final CountDownLatch done = new CountDownLatch(1);
604 >        try {
605 >            ScheduledFuture[] tasks = new ScheduledFuture[5];
606 >            for (int i = 0; i < tasks.length; i++) {
607 >                Runnable r = new CheckedRunnable() {
608 >                    public void realRun() throws InterruptedException {
609 >                        threadStarted.countDown();
610 >                        done.await();
611 >                    }};
612 >                tasks[i] = p.schedule(r, 1, MILLISECONDS);
613 >            }
614 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
615 >            BlockingQueue<Runnable> q = p.getQueue();
616 >            assertTrue(q.contains(tasks[tasks.length - 1]));
617              assertFalse(q.contains(tasks[0]));
618          } finally {
619 <            joinPool(p1);
619 >            done.countDown();
620 >            joinPool(p);
621          }
622      }
623  
# Line 475 | Line 625 | public class ScheduledExecutorSubclassTe
625       * remove(task) removes queued task, and fails to remove active task
626       */
627      public void testRemove() throws InterruptedException {
628 <        CustomExecutor p1 = new CustomExecutor(1);
628 >        final ScheduledThreadPoolExecutor p = new CustomExecutor(1);
629          ScheduledFuture[] tasks = new ScheduledFuture[5];
630 <        for (int i = 0; i < 5; i++) {
631 <            tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS);
482 <        }
630 >        final CountDownLatch threadStarted = new CountDownLatch(1);
631 >        final CountDownLatch done = new CountDownLatch(1);
632          try {
633 <            Thread.sleep(SHORT_DELAY_MS);
634 <            BlockingQueue<Runnable> q = p1.getQueue();
635 <            assertFalse(p1.remove((Runnable)tasks[0]));
633 >            for (int i = 0; i < tasks.length; i++) {
634 >                Runnable r = new CheckedRunnable() {
635 >                    public void realRun() throws InterruptedException {
636 >                        threadStarted.countDown();
637 >                        done.await();
638 >                    }};
639 >                tasks[i] = p.schedule(r, 1, MILLISECONDS);
640 >            }
641 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
642 >            BlockingQueue<Runnable> q = p.getQueue();
643 >            assertFalse(p.remove((Runnable)tasks[0]));
644              assertTrue(q.contains((Runnable)tasks[4]));
645              assertTrue(q.contains((Runnable)tasks[3]));
646 <            assertTrue(p1.remove((Runnable)tasks[4]));
647 <            assertFalse(p1.remove((Runnable)tasks[4]));
646 >            assertTrue(p.remove((Runnable)tasks[4]));
647 >            assertFalse(p.remove((Runnable)tasks[4]));
648              assertFalse(q.contains((Runnable)tasks[4]));
649              assertTrue(q.contains((Runnable)tasks[3]));
650 <            assertTrue(p1.remove((Runnable)tasks[3]));
650 >            assertTrue(p.remove((Runnable)tasks[3]));
651              assertFalse(q.contains((Runnable)tasks[3]));
652          } finally {
653 <            joinPool(p1);
653 >            done.countDown();
654 >            joinPool(p);
655          }
656      }
657  
658      /**
659 <     *  purge removes cancelled tasks from the queue
659 >     * purge removes cancelled tasks from the queue
660       */
661      public void testPurge() throws InterruptedException {
662 <        CustomExecutor p1 = new CustomExecutor(1);
662 >        CustomExecutor p = new CustomExecutor(1);
663          ScheduledFuture[] tasks = new ScheduledFuture[5];
664 <        for (int i = 0; i < 5; i++) {
665 <            tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS);
666 <        }
664 >        for (int i = 0; i < tasks.length; i++)
665 >            tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(),
666 >                                  LONG_DELAY_MS, MILLISECONDS);
667          try {
668 <            int max = 5;
668 >            int max = tasks.length;
669              if (tasks[4].cancel(true)) --max;
670              if (tasks[3].cancel(true)) --max;
671              // There must eventually be an interference-free point at
672              // which purge will not fail. (At worst, when queue is empty.)
673 <            int k;
674 <            for (k = 0; k < SMALL_DELAY_MS; ++k) {
675 <                p1.purge();
676 <                long count = p1.getTaskCount();
677 <                if (count >= 0 && count <= max)
678 <                    break;
679 <                Thread.sleep(1);
680 <            }
523 <            assertTrue(k < SMALL_DELAY_MS);
673 >            long startTime = System.nanoTime();
674 >            do {
675 >                p.purge();
676 >                long count = p.getTaskCount();
677 >                if (count == max)
678 >                    return;
679 >            } while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
680 >            fail("Purge failed to remove cancelled tasks");
681          } finally {
682 <            joinPool(p1);
682 >            for (ScheduledFuture task : tasks)
683 >                task.cancel(true);
684 >            joinPool(p);
685          }
686      }
687  
688      /**
689 <     *  shutDownNow returns a list containing tasks that were not run
689 >     * shutdownNow returns a list containing tasks that were not run
690       */
691 <    public void testShutDownNow() {
692 <        CustomExecutor p1 = new CustomExecutor(1);
691 >    public void testShutdownNow() {
692 >        CustomExecutor p = new CustomExecutor(1);
693          for (int i = 0; i < 5; i++)
694 <            p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS);
695 <        List l;
694 >            p.schedule(new SmallPossiblyInterruptedRunnable(),
695 >                       LONG_DELAY_MS, MILLISECONDS);
696          try {
697 <            l = p1.shutdownNow();
697 >            List<Runnable> l = p.shutdownNow();
698 >            assertTrue(p.isShutdown());
699 >            assertEquals(5, l.size());
700          } catch (SecurityException ok) {
701 <            return;
701 >            // Allowed in case test doesn't have privs
702 >        } finally {
703 >            joinPool(p);
704          }
542        assertTrue(p1.isShutdown());
543        assertTrue(l.size() > 0 && l.size() <= 5);
544        joinPool(p1);
705      }
706  
707      /**
708       * In default setting, shutdown cancels periodic but not delayed
709       * tasks at shutdown
710       */
711 <    public void testShutDown1() throws InterruptedException {
712 <        CustomExecutor p1 = new CustomExecutor(1);
713 <        assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy());
714 <        assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy());
711 >    public void testShutdown1() throws InterruptedException {
712 >        CustomExecutor p = new CustomExecutor(1);
713 >        assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
714 >        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
715  
716          ScheduledFuture[] tasks = new ScheduledFuture[5];
717 <        for (int i = 0; i < 5; i++)
718 <            tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS);
719 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
720 <        BlockingQueue q = p1.getQueue();
721 <        for (Iterator it = q.iterator(); it.hasNext();) {
722 <            ScheduledFuture t = (ScheduledFuture)it.next();
723 <            assertFalse(t.isCancelled());
724 <        }
725 <        assertTrue(p1.isShutdown());
726 <        Thread.sleep(SMALL_DELAY_MS);
727 <        for (int i = 0; i < 5; ++i) {
728 <            assertTrue(tasks[i].isDone());
729 <            assertFalse(tasks[i].isCancelled());
717 >        for (int i = 0; i < tasks.length; i++)
718 >            tasks[i] = p.schedule(new NoOpRunnable(),
719 >                                  SHORT_DELAY_MS, MILLISECONDS);
720 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
721 >        BlockingQueue<Runnable> q = p.getQueue();
722 >        for (ScheduledFuture task : tasks) {
723 >            assertFalse(task.isDone());
724 >            assertFalse(task.isCancelled());
725 >            assertTrue(q.contains(task));
726 >        }
727 >        assertTrue(p.isShutdown());
728 >        assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
729 >        assertTrue(p.isTerminated());
730 >        for (ScheduledFuture task : tasks) {
731 >            assertTrue(task.isDone());
732 >            assertFalse(task.isCancelled());
733          }
734      }
735  
573
736      /**
737       * If setExecuteExistingDelayedTasksAfterShutdownPolicy is false,
738       * delayed tasks are cancelled at shutdown
739       */
740 <    public void testShutDown2() throws InterruptedException {
741 <        CustomExecutor p1 = new CustomExecutor(1);
742 <        p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
740 >    public void testShutdown2() throws InterruptedException {
741 >        CustomExecutor p = new CustomExecutor(1);
742 >        p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
743 >        assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
744 >        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
745          ScheduledFuture[] tasks = new ScheduledFuture[5];
746 <        for (int i = 0; i < 5; i++)
747 <            tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS);
748 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
749 <        assertTrue(p1.isShutdown());
750 <        BlockingQueue q = p1.getQueue();
746 >        for (int i = 0; i < tasks.length; i++)
747 >            tasks[i] = p.schedule(new NoOpRunnable(),
748 >                                  SHORT_DELAY_MS, MILLISECONDS);
749 >        BlockingQueue q = p.getQueue();
750 >        assertEquals(tasks.length, q.size());
751 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
752 >        assertTrue(p.isShutdown());
753          assertTrue(q.isEmpty());
754 <        Thread.sleep(SMALL_DELAY_MS);
755 <        assertTrue(p1.isTerminated());
754 >        assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
755 >        assertTrue(p.isTerminated());
756 >        for (ScheduledFuture task : tasks) {
757 >            assertTrue(task.isDone());
758 >            assertTrue(task.isCancelled());
759 >        }
760      }
761  
592
762      /**
763       * If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false,
764 <     * periodic tasks are not cancelled at shutdown
764 >     * periodic tasks are cancelled at shutdown
765       */
766 <    public void testShutDown3() throws InterruptedException {
767 <        CustomExecutor p1 = new CustomExecutor(1);
768 <        p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
766 >    public void testShutdown3() throws InterruptedException {
767 >        CustomExecutor p = new CustomExecutor(1);
768 >        assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
769 >        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
770 >        p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
771 >        assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
772 >        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
773 >        long initialDelay = LONG_DELAY_MS;
774          ScheduledFuture task =
775 <            p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, MILLISECONDS);
776 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
777 <        assertTrue(p1.isShutdown());
778 <        BlockingQueue q = p1.getQueue();
779 <        assertTrue(q.isEmpty());
780 <        Thread.sleep(SHORT_DELAY_MS);
781 <        assertTrue(p1.isTerminated());
775 >            p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay,
776 >                                  5, MILLISECONDS);
777 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
778 >        assertTrue(p.isShutdown());
779 >        assertTrue(p.getQueue().isEmpty());
780 >        assertTrue(task.isDone());
781 >        assertTrue(task.isCancelled());
782 >        joinPool(p);
783      }
784  
785      /**
786       * if setContinueExistingPeriodicTasksAfterShutdownPolicy is true,
787 <     * periodic tasks are cancelled at shutdown
787 >     * periodic tasks are not cancelled at shutdown
788       */
789 <    public void testShutDown4() throws InterruptedException {
790 <        CustomExecutor p1 = new CustomExecutor(1);
789 >    public void testShutdown4() throws InterruptedException {
790 >        CustomExecutor p = new CustomExecutor(1);
791 >        final CountDownLatch counter = new CountDownLatch(2);
792          try {
793 <            p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
793 >            p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
794 >            assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
795 >            assertTrue(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
796 >            final Runnable r = new CheckedRunnable() {
797 >                public void realRun() {
798 >                    counter.countDown();
799 >                }};
800              ScheduledFuture task =
801 <                p1.scheduleAtFixedRate(new NoOpRunnable(), 1, 1, MILLISECONDS);
801 >                p.scheduleAtFixedRate(r, 1, 1, MILLISECONDS);
802 >            assertFalse(task.isDone());
803              assertFalse(task.isCancelled());
804 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
804 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
805              assertFalse(task.isCancelled());
806 <            assertFalse(p1.isTerminated());
807 <            assertTrue(p1.isShutdown());
808 <            Thread.sleep(SHORT_DELAY_MS);
806 >            assertFalse(p.isTerminated());
807 >            assertTrue(p.isShutdown());
808 >            assertTrue(counter.await(SMALL_DELAY_MS, MILLISECONDS));
809              assertFalse(task.isCancelled());
810 <            assertTrue(task.cancel(true));
810 >            assertTrue(task.cancel(false));
811              assertTrue(task.isDone());
812 <            Thread.sleep(SHORT_DELAY_MS);
813 <            assertTrue(p1.isTerminated());
812 >            assertTrue(task.isCancelled());
813 >            assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
814 >            assertTrue(p.isTerminated());
815          }
816          finally {
817 <            joinPool(p1);
817 >            joinPool(p);
818          }
819      }
820  
# Line 708 | Line 892 | public class ScheduledExecutorSubclassTe
892       * invokeAny(c) throws NPE if c has null elements
893       */
894      public void testInvokeAny3() throws Exception {
895 <        final CountDownLatch latch = new CountDownLatch(1);
895 >        CountDownLatch latch = new CountDownLatch(1);
896          ExecutorService e = new CustomExecutor(2);
897 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
898 +        l.add(latchAwaitingStringTask(latch));
899 +        l.add(null);
900          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 ok) {}
720                          return TEST_STRING;
721                      }});
722            l.add(null);
901              e.invokeAny(l);
902              shouldThrow();
903          } catch (NullPointerException success) {
# Line 734 | Line 912 | public class ScheduledExecutorSubclassTe
912       */
913      public void testInvokeAny4() throws Exception {
914          ExecutorService e = new CustomExecutor(2);
915 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
916 +        l.add(new NPETask());
917          try {
738            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
739            l.add(new NPETask());
918              e.invokeAny(l);
919              shouldThrow();
920          } catch (ExecutionException success) {
# Line 752 | Line 930 | public class ScheduledExecutorSubclassTe
930      public void testInvokeAny5() throws Exception {
931          ExecutorService e = new CustomExecutor(2);
932          try {
933 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
933 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
934              l.add(new StringTask());
935              l.add(new StringTask());
936              String result = e.invokeAny(l);
# Line 794 | Line 972 | public class ScheduledExecutorSubclassTe
972       */
973      public void testInvokeAll3() throws Exception {
974          ExecutorService e = new CustomExecutor(2);
975 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
976 +        l.add(new StringTask());
977 +        l.add(null);
978          try {
798            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
799            l.add(new StringTask());
800            l.add(null);
979              e.invokeAll(l);
980              shouldThrow();
981          } catch (NullPointerException success) {
# Line 811 | Line 989 | public class ScheduledExecutorSubclassTe
989       */
990      public void testInvokeAll4() throws Exception {
991          ExecutorService e = new CustomExecutor(2);
992 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
993 +        l.add(new NPETask());
994 +        List<Future<String>> futures = e.invokeAll(l);
995 +        assertEquals(1, futures.size());
996          try {
997 <            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();
997 >            futures.get(0).get();
998              shouldThrow();
999          } catch (ExecutionException success) {
1000              assertTrue(success.getCause() instanceof NullPointerException);
# Line 832 | Line 1009 | public class ScheduledExecutorSubclassTe
1009      public void testInvokeAll5() throws Exception {
1010          ExecutorService e = new CustomExecutor(2);
1011          try {
1012 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1012 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1013              l.add(new StringTask());
1014              l.add(new StringTask());
1015 <            List<Future<String>> result = e.invokeAll(l);
1016 <            assertEquals(2, result.size());
1017 <            for (Future<String> future : result)
1015 >            List<Future<String>> futures = e.invokeAll(l);
1016 >            assertEquals(2, futures.size());
1017 >            for (Future<String> future : futures)
1018                  assertSame(TEST_STRING, future.get());
1019          } finally {
1020              joinPool(e);
# Line 863 | Line 1040 | public class ScheduledExecutorSubclassTe
1040       */
1041      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1042          ExecutorService e = new CustomExecutor(2);
1043 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1044 +        l.add(new StringTask());
1045          try {
867            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
868            l.add(new StringTask());
1046              e.invokeAny(l, MEDIUM_DELAY_MS, null);
1047              shouldThrow();
1048          } catch (NullPointerException success) {
# Line 892 | Line 1069 | public class ScheduledExecutorSubclassTe
1069       * timed invokeAny(c) throws NPE if c has null elements
1070       */
1071      public void testTimedInvokeAny3() throws Exception {
1072 <        final CountDownLatch latch = new CountDownLatch(1);
1072 >        CountDownLatch latch = new CountDownLatch(1);
1073          ExecutorService e = new CustomExecutor(2);
1074 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1075 +        l.add(latchAwaitingStringTask(latch));
1076 +        l.add(null);
1077          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 ok) {}
904                          return TEST_STRING;
905                      }});
906            l.add(null);
1078              e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1079              shouldThrow();
1080          } catch (NullPointerException success) {
# Line 918 | Line 1089 | public class ScheduledExecutorSubclassTe
1089       */
1090      public void testTimedInvokeAny4() throws Exception {
1091          ExecutorService e = new CustomExecutor(2);
1092 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1093 +        l.add(new NPETask());
1094          try {
922            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
923            l.add(new NPETask());
1095              e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1096              shouldThrow();
1097          } catch (ExecutionException success) {
# Line 936 | Line 1107 | public class ScheduledExecutorSubclassTe
1107      public void testTimedInvokeAny5() throws Exception {
1108          ExecutorService e = new CustomExecutor(2);
1109          try {
1110 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1110 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1111              l.add(new StringTask());
1112              l.add(new StringTask());
1113              String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
# Line 965 | Line 1136 | public class ScheduledExecutorSubclassTe
1136       */
1137      public void testTimedInvokeAllNullTimeUnit() throws Exception {
1138          ExecutorService e = new CustomExecutor(2);
1139 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1140 +        l.add(new StringTask());
1141          try {
969            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
970            l.add(new StringTask());
1142              e.invokeAll(l, MEDIUM_DELAY_MS, null);
1143              shouldThrow();
1144          } catch (NullPointerException success) {
# Line 994 | Line 1165 | public class ScheduledExecutorSubclassTe
1165       */
1166      public void testTimedInvokeAll3() throws Exception {
1167          ExecutorService e = new CustomExecutor(2);
1168 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1169 +        l.add(new StringTask());
1170 +        l.add(null);
1171          try {
998            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
999            l.add(new StringTask());
1000            l.add(null);
1172              e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1173              shouldThrow();
1174          } catch (NullPointerException success) {
# Line 1011 | Line 1182 | public class ScheduledExecutorSubclassTe
1182       */
1183      public void testTimedInvokeAll4() throws Exception {
1184          ExecutorService e = new CustomExecutor(2);
1185 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1186 +        l.add(new NPETask());
1187 +        List<Future<String>> futures =
1188 +            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1189 +        assertEquals(1, futures.size());
1190          try {
1191 <            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();
1191 >            futures.get(0).get();
1192              shouldThrow();
1193          } catch (ExecutionException success) {
1194              assertTrue(success.getCause() instanceof NullPointerException);
# Line 1032 | Line 1203 | public class ScheduledExecutorSubclassTe
1203      public void testTimedInvokeAll5() throws Exception {
1204          ExecutorService e = new CustomExecutor(2);
1205          try {
1206 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1206 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1207              l.add(new StringTask());
1208              l.add(new StringTask());
1209 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1210 <            assertEquals(2, result.size());
1211 <            for (Future<String> future : result)
1209 >            List<Future<String>> futures =
1210 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1211 >            assertEquals(2, futures.size());
1212 >            for (Future<String> future : futures)
1213                  assertSame(TEST_STRING, future.get());
1214          } finally {
1215              joinPool(e);
# Line 1050 | Line 1222 | public class ScheduledExecutorSubclassTe
1222      public void testTimedInvokeAll6() throws Exception {
1223          ExecutorService e = new CustomExecutor(2);
1224          try {
1225 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1225 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1226              l.add(new StringTask());
1227              l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1228              l.add(new StringTask());
1229 <            List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1230 <            assertEquals(3, result.size());
1231 <            Iterator<Future<String>> it = result.iterator();
1232 <            Future<String> f1 = it.next();
1233 <            Future<String> f2 = it.next();
1234 <            Future<String> f3 = it.next();
1235 <            assertTrue(f1.isDone());
1064 <            assertTrue(f2.isDone());
1065 <            assertTrue(f3.isDone());
1066 <            assertFalse(f1.isCancelled());
1067 <            assertTrue(f2.isCancelled());
1229 >            List<Future<String>> futures =
1230 >                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1231 >            assertEquals(l.size(), futures.size());
1232 >            for (Future future : futures)
1233 >                assertTrue(future.isDone());
1234 >            assertFalse(futures.get(0).isCancelled());
1235 >            assertTrue(futures.get(1).isCancelled());
1236          } finally {
1237              joinPool(e);
1238          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines