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.34 by jsr166, Wed Dec 31 19:05:43 2014 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.*;
7   import static java.util.concurrent.TimeUnit.MILLISECONDS;
8 < import java.util.concurrent.atomic.*;
8 >
9 > import java.util.ArrayList;
10 > import java.util.List;
11 > import java.util.concurrent.BlockingQueue;
12 > import java.util.concurrent.Callable;
13 > import java.util.concurrent.CountDownLatch;
14 > import java.util.concurrent.Delayed;
15 > import java.util.concurrent.ExecutionException;
16 > import java.util.concurrent.Executors;
17 > import java.util.concurrent.ExecutorService;
18 > import java.util.concurrent.Future;
19 > import java.util.concurrent.RejectedExecutionException;
20 > import java.util.concurrent.RejectedExecutionHandler;
21 > import java.util.concurrent.RunnableScheduledFuture;
22 > import java.util.concurrent.ScheduledFuture;
23 > import java.util.concurrent.ScheduledThreadPoolExecutor;
24 > import java.util.concurrent.ThreadFactory;
25 > import java.util.concurrent.ThreadPoolExecutor;
26 > import java.util.concurrent.TimeoutException;
27 > import java.util.concurrent.TimeUnit;
28 > import java.util.concurrent.atomic.AtomicInteger;
29 >
30 > import junit.framework.Test;
31 > import junit.framework.TestSuite;
32  
33   public class ScheduledExecutorSubclassTest extends JSR166TestCase {
34      public static void main(String[] args) {
35 <        junit.textui.TestRunner.run (suite());
35 >        junit.textui.TestRunner.run(suite());
36      }
37      public static Test suite() {
38          return new TestSuite(ScheduledExecutorSubclassTest.class);
# Line 36 | Line 56 | public class ScheduledExecutorSubclassTe
56          }
57          public boolean isCancelled() { return task.isCancelled(); }
58          public boolean isDone() { return task.isDone(); }
59 <        public V get() throws InterruptedException,  ExecutionException {
59 >        public V get() throws InterruptedException, ExecutionException {
60              V v = task.get();
61              assertTrue(ran);
62              return v;
63          }
64 <        public V get(long time, TimeUnit unit) throws InterruptedException,  ExecutionException, TimeoutException {
64 >        public V get(long time, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
65              V v = task.get(time, unit);
66              assertTrue(ran);
67              return v;
68          }
69      }
70  
51
71      public class CustomExecutor extends ScheduledThreadPoolExecutor {
72  
73          protected <V> RunnableScheduledFuture<V> decorateTask(Runnable r, RunnableScheduledFuture<V> task) {
# Line 58 | Line 77 | public class ScheduledExecutorSubclassTe
77          protected <V> RunnableScheduledFuture<V> decorateTask(Callable<V> c, RunnableScheduledFuture<V> task) {
78              return new CustomTask<V>(task);
79          }
80 <        CustomExecutor(int corePoolSize) { super(corePoolSize);}
80 >        CustomExecutor(int corePoolSize) { super(corePoolSize); }
81          CustomExecutor(int corePoolSize, RejectedExecutionHandler handler) {
82              super(corePoolSize, handler);
83          }
# Line 73 | Line 92 | public class ScheduledExecutorSubclassTe
92  
93      }
94  
76
77
95      /**
96       * execute successfully executes a runnable
97       */
98      public void testExecute() throws InterruptedException {
99 <        TrackedShortRunnable runnable =new TrackedShortRunnable();
100 <        CustomExecutor p1 = new CustomExecutor(1);
101 <        p1.execute(runnable);
102 <        assertFalse(runnable.done);
103 <        Thread.sleep(SHORT_DELAY_MS);
104 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
105 <        Thread.sleep(MEDIUM_DELAY_MS);
106 <        assertTrue(runnable.done);
107 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
108 <        joinPool(p1);
99 >        CustomExecutor p = new CustomExecutor(1);
100 >        final CountDownLatch done = new CountDownLatch(1);
101 >        final Runnable task = new CheckedRunnable() {
102 >            public void realRun() {
103 >                done.countDown();
104 >            }};
105 >        try {
106 >            p.execute(task);
107 >            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
108 >        } finally {
109 >            joinPool(p);
110 >        }
111      }
112  
94
113      /**
114       * delayed schedule of callable successfully executes after delay
115       */
116      public void testSchedule1() throws Exception {
117 <        TrackedCallable callable = new TrackedCallable();
118 <        CustomExecutor p1 = new CustomExecutor(1);
119 <        Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
120 <        assertFalse(callable.done);
121 <        Thread.sleep(MEDIUM_DELAY_MS);
122 <        assertTrue(callable.done);
123 <        assertEquals(Boolean.TRUE, f.get());
124 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
125 <        joinPool(p1);
117 >        CustomExecutor p = new CustomExecutor(1);
118 >        final long startTime = System.nanoTime();
119 >        final CountDownLatch done = new CountDownLatch(1);
120 >        try {
121 >            Callable task = new CheckedCallable<Boolean>() {
122 >                public Boolean realCall() {
123 >                    done.countDown();
124 >                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
125 >                    return Boolean.TRUE;
126 >                }};
127 >            Future f = p.schedule(task, timeoutMillis(), MILLISECONDS);
128 >            assertSame(Boolean.TRUE, f.get());
129 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
130 >            assertTrue(done.await(0L, MILLISECONDS));
131 >        } finally {
132 >            joinPool(p);
133 >        }
134      }
135  
136      /**
137 <     *  delayed schedule of runnable successfully executes after delay
138 <     */
139 <    public void testSchedule3() throws InterruptedException {
140 <        TrackedShortRunnable runnable = new TrackedShortRunnable();
141 <        CustomExecutor p1 = new CustomExecutor(1);
142 <        p1.schedule(runnable, SMALL_DELAY_MS, MILLISECONDS);
143 <        Thread.sleep(SHORT_DELAY_MS);
144 <        assertFalse(runnable.done);
145 <        Thread.sleep(MEDIUM_DELAY_MS);
146 <        assertTrue(runnable.done);
147 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
148 <        joinPool(p1);
137 >     * delayed schedule of runnable successfully executes after delay
138 >     */
139 >    public void testSchedule3() throws Exception {
140 >        CustomExecutor p = new CustomExecutor(1);
141 >        final long startTime = System.nanoTime();
142 >        final CountDownLatch done = new CountDownLatch(1);
143 >        try {
144 >            Runnable task = new CheckedRunnable() {
145 >                public void realRun() {
146 >                    done.countDown();
147 >                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
148 >                }};
149 >            Future f = p.schedule(task, timeoutMillis(), MILLISECONDS);
150 >            await(done);
151 >            assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
152 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
153 >        } finally {
154 >            joinPool(p);
155 >        }
156      }
157  
158      /**
159       * scheduleAtFixedRate executes runnable after given initial delay
160       */
161      public void testSchedule4() throws InterruptedException {
162 <        TrackedShortRunnable runnable = new TrackedShortRunnable();
163 <        CustomExecutor p1 = new CustomExecutor(1);
164 <        ScheduledFuture h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS);
165 <        assertFalse(runnable.done);
166 <        Thread.sleep(MEDIUM_DELAY_MS);
167 <        assertTrue(runnable.done);
168 <        h.cancel(true);
169 <        joinPool(p1);
170 <    }
171 <
172 <    static class RunnableCounter implements Runnable {
173 <        AtomicInteger count = new AtomicInteger(0);
174 <        public void run() { count.getAndIncrement(); }
162 >        CustomExecutor p = new CustomExecutor(1);
163 >        final long startTime = System.nanoTime();
164 >        final CountDownLatch done = new CountDownLatch(1);
165 >        try {
166 >            Runnable task = new CheckedRunnable() {
167 >                public void realRun() {
168 >                    done.countDown();
169 >                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
170 >                }};
171 >            ScheduledFuture f =
172 >                p.scheduleAtFixedRate(task, timeoutMillis(),
173 >                                      LONG_DELAY_MS, MILLISECONDS);
174 >            await(done);
175 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
176 >            f.cancel(true);
177 >        } finally {
178 >            joinPool(p);
179 >        }
180      }
181  
182      /**
183       * scheduleWithFixedDelay executes runnable after given initial delay
184       */
185      public void testSchedule5() throws InterruptedException {
186 <        TrackedShortRunnable runnable = new TrackedShortRunnable();
187 <        CustomExecutor p1 = new CustomExecutor(1);
188 <        ScheduledFuture h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS);
189 <        assertFalse(runnable.done);
190 <        Thread.sleep(MEDIUM_DELAY_MS);
191 <        assertTrue(runnable.done);
192 <        h.cancel(true);
193 <        joinPool(p1);
186 >        CustomExecutor p = new CustomExecutor(1);
187 >        final long startTime = System.nanoTime();
188 >        final CountDownLatch done = new CountDownLatch(1);
189 >        try {
190 >            Runnable task = new CheckedRunnable() {
191 >                public void realRun() {
192 >                    done.countDown();
193 >                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
194 >                }};
195 >            ScheduledFuture f =
196 >                p.scheduleWithFixedDelay(task, timeoutMillis(),
197 >                                         LONG_DELAY_MS, MILLISECONDS);
198 >            await(done);
199 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
200 >            f.cancel(true);
201 >        } finally {
202 >            joinPool(p);
203 >        }
204 >    }
205 >
206 >    static class RunnableCounter implements Runnable {
207 >        AtomicInteger count = new AtomicInteger(0);
208 >        public void run() { count.getAndIncrement(); }
209      }
210  
211      /**
212       * scheduleAtFixedRate executes series of tasks at given rate
213       */
214      public void testFixedRateSequence() throws InterruptedException {
215 <        CustomExecutor p1 = new CustomExecutor(1);
216 <        RunnableCounter counter = new RunnableCounter();
217 <        ScheduledFuture h =
218 <            p1.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS);
219 <        Thread.sleep(SMALL_DELAY_MS);
220 <        h.cancel(true);
221 <        int c = counter.count.get();
222 <        // By time scaling conventions, we must have at least
223 <        // an execution per SHORT delay, but no more than one SHORT more
224 <        assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
225 <        assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
226 <        joinPool(p1);
215 >        CustomExecutor p = new CustomExecutor(1);
216 >        try {
217 >            for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
218 >                long startTime = System.nanoTime();
219 >                int cycles = 10;
220 >                final CountDownLatch done = new CountDownLatch(cycles);
221 >                Runnable task = new CheckedRunnable() {
222 >                    public void realRun() { done.countDown(); }};
223 >                ScheduledFuture h =
224 >                    p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS);
225 >                done.await();
226 >                h.cancel(true);
227 >                double normalizedTime =
228 >                    (double) millisElapsedSince(startTime) / delay;
229 >                if (normalizedTime >= cycles - 1 &&
230 >                    normalizedTime <= cycles)
231 >                    return;
232 >            }
233 >            throw new AssertionError("unexpected execution rate");
234 >        } finally {
235 >            joinPool(p);
236 >        }
237      }
238  
239      /**
240       * scheduleWithFixedDelay executes series of tasks with given period
241       */
242      public void testFixedDelaySequence() throws InterruptedException {
243 <        CustomExecutor p1 = new CustomExecutor(1);
244 <        RunnableCounter counter = new RunnableCounter();
245 <        ScheduledFuture h =
246 <            p1.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS);
247 <        Thread.sleep(SMALL_DELAY_MS);
248 <        h.cancel(true);
249 <        int c = counter.count.get();
250 <        assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
251 <        assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
252 <        joinPool(p1);
243 >        CustomExecutor p = new CustomExecutor(1);
244 >        try {
245 >            for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
246 >                long startTime = System.nanoTime();
247 >                int cycles = 10;
248 >                final CountDownLatch done = new CountDownLatch(cycles);
249 >                Runnable task = new CheckedRunnable() {
250 >                    public void realRun() { done.countDown(); }};
251 >                ScheduledFuture h =
252 >                    p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS);
253 >                done.await();
254 >                h.cancel(true);
255 >                double normalizedTime =
256 >                    (double) millisElapsedSince(startTime) / delay;
257 >                if (normalizedTime >= cycles - 1 &&
258 >                    normalizedTime <= cycles)
259 >                    return;
260 >            }
261 >            throw new AssertionError("unexpected execution rate");
262 >        } finally {
263 >            joinPool(p);
264 >        }
265      }
266  
192
267      /**
268 <     *  execute (null) throws NPE
268 >     * execute(null) throws NPE
269       */
270      public void testExecuteNull() throws InterruptedException {
271          CustomExecutor se = new CustomExecutor(1);
# Line 203 | Line 277 | public class ScheduledExecutorSubclassTe
277      }
278  
279      /**
280 <     * schedule (null) throws NPE
280 >     * schedule(null) throws NPE
281       */
282      public void testScheduleNull() throws InterruptedException {
283          CustomExecutor se = new CustomExecutor(1);
# Line 251 | Line 325 | public class ScheduledExecutorSubclassTe
325      /**
326       * schedule callable throws RejectedExecutionException if shutdown
327       */
328 <     public void testSchedule3_RejectedExecutionException() {
329 <         CustomExecutor se = new CustomExecutor(1);
330 <         try {
331 <             se.shutdown();
332 <             se.schedule(new NoOpCallable(),
333 <                         MEDIUM_DELAY_MS, MILLISECONDS);
334 <             shouldThrow();
335 <         } catch (RejectedExecutionException success) {
336 <         } catch (SecurityException ok) {
337 <         }
338 <         joinPool(se);
328 >    public void testSchedule3_RejectedExecutionException() {
329 >        CustomExecutor se = new CustomExecutor(1);
330 >        try {
331 >            se.shutdown();
332 >            se.schedule(new NoOpCallable(),
333 >                        MEDIUM_DELAY_MS, MILLISECONDS);
334 >            shouldThrow();
335 >        } catch (RejectedExecutionException success) {
336 >        } catch (SecurityException ok) {
337 >        }
338 >        joinPool(se);
339      }
340  
341      /**
342 <     *  scheduleAtFixedRate throws RejectedExecutionException if shutdown
342 >     * scheduleAtFixedRate throws RejectedExecutionException if shutdown
343       */
344      public void testScheduleAtFixedRate1_RejectedExecutionException() {
345          CustomExecutor se = new CustomExecutor(1);
# Line 297 | Line 371 | public class ScheduledExecutorSubclassTe
371      }
372  
373      /**
374 <     *  getActiveCount increases but doesn't overestimate, when a
375 <     *  thread becomes active
374 >     * getActiveCount increases but doesn't overestimate, when a
375 >     * thread becomes active
376       */
377      public void testGetActiveCount() throws InterruptedException {
378 <        CustomExecutor p2 = new CustomExecutor(2);
379 <        assertEquals(0, p2.getActiveCount());
380 <        p2.execute(new SmallRunnable());
381 <        Thread.sleep(SHORT_DELAY_MS);
382 <        assertEquals(1, p2.getActiveCount());
383 <        joinPool(p2);
378 >        final ThreadPoolExecutor p = new CustomExecutor(2);
379 >        final CountDownLatch threadStarted = new CountDownLatch(1);
380 >        final CountDownLatch done = new CountDownLatch(1);
381 >        try {
382 >            assertEquals(0, p.getActiveCount());
383 >            p.execute(new CheckedRunnable() {
384 >                public void realRun() throws InterruptedException {
385 >                    threadStarted.countDown();
386 >                    assertEquals(1, p.getActiveCount());
387 >                    done.await();
388 >                }});
389 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
390 >            assertEquals(1, p.getActiveCount());
391 >        } finally {
392 >            done.countDown();
393 >            joinPool(p);
394 >        }
395      }
396  
397      /**
398 <     *    getCompletedTaskCount increases, but doesn't overestimate,
399 <     *   when tasks complete
398 >     * getCompletedTaskCount increases, but doesn't overestimate,
399 >     * when tasks complete
400       */
401 <    public void testGetCompletedTaskCount()throws InterruptedException  {
402 <        CustomExecutor p2 = new CustomExecutor(2);
403 <        assertEquals(0, p2.getCompletedTaskCount());
404 <        p2.execute(new SmallRunnable());
405 <        Thread.sleep(MEDIUM_DELAY_MS);
406 <        assertEquals(1, p2.getCompletedTaskCount());
407 <        joinPool(p2);
401 >    public void testGetCompletedTaskCount() throws InterruptedException {
402 >        final ThreadPoolExecutor p = new CustomExecutor(2);
403 >        final CountDownLatch threadStarted = new CountDownLatch(1);
404 >        final CountDownLatch threadProceed = new CountDownLatch(1);
405 >        final CountDownLatch threadDone = new CountDownLatch(1);
406 >        try {
407 >            assertEquals(0, p.getCompletedTaskCount());
408 >            p.execute(new CheckedRunnable() {
409 >                public void realRun() throws InterruptedException {
410 >                    threadStarted.countDown();
411 >                    assertEquals(0, p.getCompletedTaskCount());
412 >                    threadProceed.await();
413 >                    threadDone.countDown();
414 >                }});
415 >            await(threadStarted);
416 >            assertEquals(0, p.getCompletedTaskCount());
417 >            threadProceed.countDown();
418 >            threadDone.await();
419 >            long startTime = System.nanoTime();
420 >            while (p.getCompletedTaskCount() != 1) {
421 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
422 >                    fail("timed out");
423 >                Thread.yield();
424 >            }
425 >        } finally {
426 >            joinPool(p);
427 >        }
428      }
429  
430      /**
431 <     *  getCorePoolSize returns size given in constructor if not otherwise set
431 >     * getCorePoolSize returns size given in constructor if not otherwise set
432       */
433      public void testGetCorePoolSize() {
434 <        CustomExecutor p1 = new CustomExecutor(1);
435 <        assertEquals(1, p1.getCorePoolSize());
436 <        joinPool(p1);
434 >        CustomExecutor p = new CustomExecutor(1);
435 >        assertEquals(1, p.getCorePoolSize());
436 >        joinPool(p);
437      }
438  
439      /**
440 <     *    getLargestPoolSize increases, but doesn't overestimate, when
441 <     *   multiple threads active
440 >     * getLargestPoolSize increases, but doesn't overestimate, when
441 >     * multiple threads active
442       */
443      public void testGetLargestPoolSize() throws InterruptedException {
444 <        CustomExecutor p2 = new CustomExecutor(2);
445 <        assertEquals(0, p2.getLargestPoolSize());
446 <        p2.execute(new SmallRunnable());
447 <        p2.execute(new SmallRunnable());
448 <        Thread.sleep(SHORT_DELAY_MS);
449 <        assertEquals(2, p2.getLargestPoolSize());
450 <        joinPool(p2);
444 >        final int THREADS = 3;
445 >        final ThreadPoolExecutor p = new CustomExecutor(THREADS);
446 >        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
447 >        final CountDownLatch done = new CountDownLatch(1);
448 >        try {
449 >            assertEquals(0, p.getLargestPoolSize());
450 >            for (int i = 0; i < THREADS; i++)
451 >                p.execute(new CheckedRunnable() {
452 >                    public void realRun() throws InterruptedException {
453 >                        threadsStarted.countDown();
454 >                        done.await();
455 >                        assertEquals(THREADS, p.getLargestPoolSize());
456 >                    }});
457 >            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
458 >            assertEquals(THREADS, p.getLargestPoolSize());
459 >        } finally {
460 >            done.countDown();
461 >            joinPool(p);
462 >            assertEquals(THREADS, p.getLargestPoolSize());
463 >        }
464      }
465  
466      /**
467 <     *   getPoolSize increases, but doesn't overestimate, when threads
468 <     *   become active
467 >     * getPoolSize increases, but doesn't overestimate, when threads
468 >     * become active
469       */
470 <    public void testGetPoolSize() {
471 <        CustomExecutor p1 = new CustomExecutor(1);
472 <        assertEquals(0, p1.getPoolSize());
473 <        p1.execute(new SmallRunnable());
474 <        assertEquals(1, p1.getPoolSize());
475 <        joinPool(p1);
470 >    public void testGetPoolSize() throws InterruptedException {
471 >        final ThreadPoolExecutor p = new CustomExecutor(1);
472 >        final CountDownLatch threadStarted = new CountDownLatch(1);
473 >        final CountDownLatch done = new CountDownLatch(1);
474 >        try {
475 >            assertEquals(0, p.getPoolSize());
476 >            p.execute(new CheckedRunnable() {
477 >                public void realRun() throws InterruptedException {
478 >                    threadStarted.countDown();
479 >                    assertEquals(1, p.getPoolSize());
480 >                    done.await();
481 >                }});
482 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
483 >            assertEquals(1, p.getPoolSize());
484 >        } finally {
485 >            done.countDown();
486 >            joinPool(p);
487 >        }
488      }
489  
490      /**
491 <     *    getTaskCount increases, but doesn't overestimate, when tasks
492 <     *    submitted
491 >     * getTaskCount increases, but doesn't overestimate, when tasks
492 >     * submitted
493       */
494      public void testGetTaskCount() throws InterruptedException {
495 <        CustomExecutor p1 = new CustomExecutor(1);
496 <        assertEquals(0, p1.getTaskCount());
497 <        for (int i = 0; i < 5; i++)
498 <            p1.execute(new SmallRunnable());
499 <        Thread.sleep(SHORT_DELAY_MS);
500 <        assertEquals(5, p1.getTaskCount());
501 <        joinPool(p1);
495 >        final ThreadPoolExecutor p = new CustomExecutor(1);
496 >        final CountDownLatch threadStarted = new CountDownLatch(1);
497 >        final CountDownLatch done = new CountDownLatch(1);
498 >        final int TASKS = 5;
499 >        try {
500 >            assertEquals(0, p.getTaskCount());
501 >            for (int i = 0; i < TASKS; i++)
502 >                p.execute(new CheckedRunnable() {
503 >                    public void realRun() throws InterruptedException {
504 >                        threadStarted.countDown();
505 >                        done.await();
506 >                    }});
507 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
508 >            assertEquals(TASKS, p.getTaskCount());
509 >        } finally {
510 >            done.countDown();
511 >            joinPool(p);
512 >        }
513      }
514  
515      /**
# Line 407 | Line 548 | public class ScheduledExecutorSubclassTe
548      }
549  
550      /**
551 <     *   is isShutDown is false before shutdown, true after
551 >     * isShutdown is false before shutdown, true after
552       */
553      public void testIsShutdown() {
554 <        CustomExecutor p1 = new CustomExecutor(1);
554 >        CustomExecutor p = new CustomExecutor(1);
555          try {
556 <            assertFalse(p1.isShutdown());
556 >            assertFalse(p.isShutdown());
557          }
558          finally {
559 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
559 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
560          }
561 <        assertTrue(p1.isShutdown());
561 >        assertTrue(p.isShutdown());
562      }
563  
423
564      /**
565 <     *  isTerminated is false before termination, true after
565 >     * isTerminated is false before termination, true after
566       */
567      public void testIsTerminated() throws InterruptedException {
568 <        CustomExecutor p1 = new CustomExecutor(1);
568 >        final ThreadPoolExecutor p = new CustomExecutor(1);
569 >        final CountDownLatch threadStarted = new CountDownLatch(1);
570 >        final CountDownLatch done = new CountDownLatch(1);
571 >        assertFalse(p.isTerminated());
572          try {
573 <            p1.execute(new SmallRunnable());
573 >            p.execute(new CheckedRunnable() {
574 >                public void realRun() throws InterruptedException {
575 >                    assertFalse(p.isTerminated());
576 >                    threadStarted.countDown();
577 >                    done.await();
578 >                }});
579 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
580 >            assertFalse(p.isTerminating());
581 >            done.countDown();
582          } finally {
583 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
583 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
584          }
585 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
586 <        assertTrue(p1.isTerminated());
585 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
586 >        assertTrue(p.isTerminated());
587      }
588  
589      /**
590 <     *  isTerminating is not true when running or when terminated
590 >     * isTerminating is not true when running or when terminated
591       */
592      public void testIsTerminating() throws InterruptedException {
593 <        CustomExecutor p1 = new CustomExecutor(1);
594 <        assertFalse(p1.isTerminating());
595 <        try {
596 <            p1.execute(new SmallRunnable());
597 <            assertFalse(p1.isTerminating());
598 <        } finally {
599 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
600 <        }
601 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
602 <        assertTrue(p1.isTerminated());
603 <        assertFalse(p1.isTerminating());
593 >        final ThreadPoolExecutor p = new CustomExecutor(1);
594 >        final CountDownLatch threadStarted = new CountDownLatch(1);
595 >        final CountDownLatch done = new CountDownLatch(1);
596 >        try {
597 >            assertFalse(p.isTerminating());
598 >            p.execute(new CheckedRunnable() {
599 >                public void realRun() throws InterruptedException {
600 >                    assertFalse(p.isTerminating());
601 >                    threadStarted.countDown();
602 >                    done.await();
603 >                }});
604 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
605 >            assertFalse(p.isTerminating());
606 >            done.countDown();
607 >        } finally {
608 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
609 >        }
610 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
611 >        assertTrue(p.isTerminated());
612 >        assertFalse(p.isTerminating());
613      }
614  
615      /**
616       * getQueue returns the work queue, which contains queued tasks
617       */
618      public void testGetQueue() throws InterruptedException {
619 <        CustomExecutor p1 = new CustomExecutor(1);
620 <        ScheduledFuture[] tasks = new ScheduledFuture[5];
621 <        for (int i = 0; i < 5; i++) {
622 <            tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS);
623 <        }
624 <        try {
625 <            Thread.sleep(SHORT_DELAY_MS);
626 <            BlockingQueue<Runnable> q = p1.getQueue();
627 <            assertTrue(q.contains(tasks[4]));
619 >        ScheduledThreadPoolExecutor p = new CustomExecutor(1);
620 >        final CountDownLatch threadStarted = new CountDownLatch(1);
621 >        final CountDownLatch done = new CountDownLatch(1);
622 >        try {
623 >            ScheduledFuture[] tasks = new ScheduledFuture[5];
624 >            for (int i = 0; i < tasks.length; i++) {
625 >                Runnable r = new CheckedRunnable() {
626 >                    public void realRun() throws InterruptedException {
627 >                        threadStarted.countDown();
628 >                        done.await();
629 >                    }};
630 >                tasks[i] = p.schedule(r, 1, MILLISECONDS);
631 >            }
632 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
633 >            BlockingQueue<Runnable> q = p.getQueue();
634 >            assertTrue(q.contains(tasks[tasks.length - 1]));
635              assertFalse(q.contains(tasks[0]));
636          } finally {
637 <            joinPool(p1);
637 >            done.countDown();
638 >            joinPool(p);
639          }
640      }
641  
# Line 475 | Line 643 | public class ScheduledExecutorSubclassTe
643       * remove(task) removes queued task, and fails to remove active task
644       */
645      public void testRemove() throws InterruptedException {
646 <        CustomExecutor p1 = new CustomExecutor(1);
646 >        final ScheduledThreadPoolExecutor p = new CustomExecutor(1);
647          ScheduledFuture[] tasks = new ScheduledFuture[5];
648 <        for (int i = 0; i < 5; i++) {
649 <            tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS);
482 <        }
648 >        final CountDownLatch threadStarted = new CountDownLatch(1);
649 >        final CountDownLatch done = new CountDownLatch(1);
650          try {
651 <            Thread.sleep(SHORT_DELAY_MS);
652 <            BlockingQueue<Runnable> q = p1.getQueue();
653 <            assertFalse(p1.remove((Runnable)tasks[0]));
651 >            for (int i = 0; i < tasks.length; i++) {
652 >                Runnable r = new CheckedRunnable() {
653 >                    public void realRun() throws InterruptedException {
654 >                        threadStarted.countDown();
655 >                        done.await();
656 >                    }};
657 >                tasks[i] = p.schedule(r, 1, MILLISECONDS);
658 >            }
659 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
660 >            BlockingQueue<Runnable> q = p.getQueue();
661 >            assertFalse(p.remove((Runnable)tasks[0]));
662              assertTrue(q.contains((Runnable)tasks[4]));
663              assertTrue(q.contains((Runnable)tasks[3]));
664 <            assertTrue(p1.remove((Runnable)tasks[4]));
665 <            assertFalse(p1.remove((Runnable)tasks[4]));
664 >            assertTrue(p.remove((Runnable)tasks[4]));
665 >            assertFalse(p.remove((Runnable)tasks[4]));
666              assertFalse(q.contains((Runnable)tasks[4]));
667              assertTrue(q.contains((Runnable)tasks[3]));
668 <            assertTrue(p1.remove((Runnable)tasks[3]));
668 >            assertTrue(p.remove((Runnable)tasks[3]));
669              assertFalse(q.contains((Runnable)tasks[3]));
670          } finally {
671 <            joinPool(p1);
671 >            done.countDown();
672 >            joinPool(p);
673          }
674      }
675  
676      /**
677 <     *  purge removes cancelled tasks from the queue
677 >     * purge removes cancelled tasks from the queue
678       */
679      public void testPurge() throws InterruptedException {
680 <        CustomExecutor p1 = new CustomExecutor(1);
680 >        CustomExecutor p = new CustomExecutor(1);
681          ScheduledFuture[] tasks = new ScheduledFuture[5];
682 <        for (int i = 0; i < 5; i++) {
683 <            tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS);
684 <        }
682 >        for (int i = 0; i < tasks.length; i++)
683 >            tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(),
684 >                                  LONG_DELAY_MS, MILLISECONDS);
685          try {
686 <            int max = 5;
686 >            int max = tasks.length;
687              if (tasks[4].cancel(true)) --max;
688              if (tasks[3].cancel(true)) --max;
689              // There must eventually be an interference-free point at
690              // which purge will not fail. (At worst, when queue is empty.)
691 <            int k;
692 <            for (k = 0; k < SMALL_DELAY_MS; ++k) {
693 <                p1.purge();
694 <                long count = p1.getTaskCount();
695 <                if (count >= 0 && count <= max)
696 <                    break;
697 <                Thread.sleep(1);
698 <            }
523 <            assertTrue(k < SMALL_DELAY_MS);
691 >            long startTime = System.nanoTime();
692 >            do {
693 >                p.purge();
694 >                long count = p.getTaskCount();
695 >                if (count == max)
696 >                    return;
697 >            } while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
698 >            fail("Purge failed to remove cancelled tasks");
699          } finally {
700 <            joinPool(p1);
700 >            for (ScheduledFuture task : tasks)
701 >                task.cancel(true);
702 >            joinPool(p);
703          }
704      }
705  
706      /**
707 <     *  shutDownNow returns a list containing tasks that were not run
707 >     * shutdownNow returns a list containing tasks that were not run
708       */
709 <    public void testShutDownNow() {
710 <        CustomExecutor p1 = new CustomExecutor(1);
709 >    public void testShutdownNow() {
710 >        CustomExecutor p = new CustomExecutor(1);
711          for (int i = 0; i < 5; i++)
712 <            p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS);
713 <        List l;
712 >            p.schedule(new SmallPossiblyInterruptedRunnable(),
713 >                       LONG_DELAY_MS, MILLISECONDS);
714          try {
715 <            l = p1.shutdownNow();
715 >            List<Runnable> l = p.shutdownNow();
716 >            assertTrue(p.isShutdown());
717 >            assertEquals(5, l.size());
718          } catch (SecurityException ok) {
719 <            return;
719 >            // Allowed in case test doesn't have privs
720 >        } finally {
721 >            joinPool(p);
722          }
542        assertTrue(p1.isShutdown());
543        assertTrue(l.size() > 0 && l.size() <= 5);
544        joinPool(p1);
723      }
724  
725      /**
726       * In default setting, shutdown cancels periodic but not delayed
727       * tasks at shutdown
728       */
729 <    public void testShutDown1() throws InterruptedException {
730 <        CustomExecutor p1 = new CustomExecutor(1);
731 <        assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy());
732 <        assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy());
729 >    public void testShutdown1() throws InterruptedException {
730 >        CustomExecutor p = new CustomExecutor(1);
731 >        assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
732 >        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
733  
734          ScheduledFuture[] tasks = new ScheduledFuture[5];
735 <        for (int i = 0; i < 5; i++)
736 <            tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS);
737 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
738 <        BlockingQueue q = p1.getQueue();
739 <        for (Iterator it = q.iterator(); it.hasNext();) {
740 <            ScheduledFuture t = (ScheduledFuture)it.next();
741 <            assertFalse(t.isCancelled());
742 <        }
743 <        assertTrue(p1.isShutdown());
744 <        Thread.sleep(SMALL_DELAY_MS);
745 <        for (int i = 0; i < 5; ++i) {
746 <            assertTrue(tasks[i].isDone());
747 <            assertFalse(tasks[i].isCancelled());
735 >        for (int i = 0; i < tasks.length; i++)
736 >            tasks[i] = p.schedule(new NoOpRunnable(),
737 >                                  SHORT_DELAY_MS, MILLISECONDS);
738 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
739 >        BlockingQueue<Runnable> q = p.getQueue();
740 >        for (ScheduledFuture task : tasks) {
741 >            assertFalse(task.isDone());
742 >            assertFalse(task.isCancelled());
743 >            assertTrue(q.contains(task));
744 >        }
745 >        assertTrue(p.isShutdown());
746 >        assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
747 >        assertTrue(p.isTerminated());
748 >        for (ScheduledFuture task : tasks) {
749 >            assertTrue(task.isDone());
750 >            assertFalse(task.isCancelled());
751          }
752      }
753  
573
754      /**
755       * If setExecuteExistingDelayedTasksAfterShutdownPolicy is false,
756       * delayed tasks are cancelled at shutdown
757       */
758 <    public void testShutDown2() throws InterruptedException {
759 <        CustomExecutor p1 = new CustomExecutor(1);
760 <        p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
758 >    public void testShutdown2() throws InterruptedException {
759 >        CustomExecutor p = new CustomExecutor(1);
760 >        p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
761 >        assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
762 >        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
763          ScheduledFuture[] tasks = new ScheduledFuture[5];
764 <        for (int i = 0; i < 5; i++)
765 <            tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS);
766 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
767 <        assertTrue(p1.isShutdown());
768 <        BlockingQueue q = p1.getQueue();
764 >        for (int i = 0; i < tasks.length; i++)
765 >            tasks[i] = p.schedule(new NoOpRunnable(),
766 >                                  SHORT_DELAY_MS, MILLISECONDS);
767 >        BlockingQueue q = p.getQueue();
768 >        assertEquals(tasks.length, q.size());
769 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
770 >        assertTrue(p.isShutdown());
771          assertTrue(q.isEmpty());
772 <        Thread.sleep(SMALL_DELAY_MS);
773 <        assertTrue(p1.isTerminated());
772 >        assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
773 >        assertTrue(p.isTerminated());
774 >        for (ScheduledFuture task : tasks) {
775 >            assertTrue(task.isDone());
776 >            assertTrue(task.isCancelled());
777 >        }
778      }
779  
592
780      /**
781       * If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false,
782 <     * periodic tasks are not cancelled at shutdown
782 >     * periodic tasks are cancelled at shutdown
783       */
784 <    public void testShutDown3() throws InterruptedException {
785 <        CustomExecutor p1 = new CustomExecutor(1);
786 <        p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
784 >    public void testShutdown3() throws InterruptedException {
785 >        CustomExecutor p = new CustomExecutor(1);
786 >        assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
787 >        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
788 >        p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
789 >        assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
790 >        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
791 >        long initialDelay = LONG_DELAY_MS;
792          ScheduledFuture task =
793 <            p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, MILLISECONDS);
794 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
795 <        assertTrue(p1.isShutdown());
796 <        BlockingQueue q = p1.getQueue();
797 <        assertTrue(q.isEmpty());
798 <        Thread.sleep(SHORT_DELAY_MS);
799 <        assertTrue(p1.isTerminated());
793 >            p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay,
794 >                                  5, MILLISECONDS);
795 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
796 >        assertTrue(p.isShutdown());
797 >        assertTrue(p.getQueue().isEmpty());
798 >        assertTrue(task.isDone());
799 >        assertTrue(task.isCancelled());
800 >        joinPool(p);
801      }
802  
803      /**
804       * if setContinueExistingPeriodicTasksAfterShutdownPolicy is true,
805 <     * periodic tasks are cancelled at shutdown
805 >     * periodic tasks are not cancelled at shutdown
806       */
807 <    public void testShutDown4() throws InterruptedException {
808 <        CustomExecutor p1 = new CustomExecutor(1);
807 >    public void testShutdown4() throws InterruptedException {
808 >        CustomExecutor p = new CustomExecutor(1);
809 >        final CountDownLatch counter = new CountDownLatch(2);
810          try {
811 <            p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
811 >            p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
812 >            assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
813 >            assertTrue(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
814 >            final Runnable r = new CheckedRunnable() {
815 >                public void realRun() {
816 >                    counter.countDown();
817 >                }};
818              ScheduledFuture task =
819 <                p1.scheduleAtFixedRate(new NoOpRunnable(), 1, 1, MILLISECONDS);
819 >                p.scheduleAtFixedRate(r, 1, 1, MILLISECONDS);
820 >            assertFalse(task.isDone());
821              assertFalse(task.isCancelled());
822 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
822 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
823              assertFalse(task.isCancelled());
824 <            assertFalse(p1.isTerminated());
825 <            assertTrue(p1.isShutdown());
826 <            Thread.sleep(SHORT_DELAY_MS);
824 >            assertFalse(p.isTerminated());
825 >            assertTrue(p.isShutdown());
826 >            assertTrue(counter.await(SMALL_DELAY_MS, MILLISECONDS));
827              assertFalse(task.isCancelled());
828 <            assertTrue(task.cancel(true));
828 >            assertTrue(task.cancel(false));
829              assertTrue(task.isDone());
830 <            Thread.sleep(SHORT_DELAY_MS);
831 <            assertTrue(p1.isTerminated());
830 >            assertTrue(task.isCancelled());
831 >            assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
832 >            assertTrue(p.isTerminated());
833          }
834          finally {
835 <            joinPool(p1);
835 >            joinPool(p);
836          }
837      }
838  
# Line 708 | Line 910 | public class ScheduledExecutorSubclassTe
910       * invokeAny(c) throws NPE if c has null elements
911       */
912      public void testInvokeAny3() throws Exception {
913 <        final CountDownLatch latch = new CountDownLatch(1);
913 >        CountDownLatch latch = new CountDownLatch(1);
914          ExecutorService e = new CustomExecutor(2);
915 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
916 +        l.add(latchAwaitingStringTask(latch));
917 +        l.add(null);
918          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);
919              e.invokeAny(l);
920              shouldThrow();
921          } catch (NullPointerException success) {
# Line 734 | Line 930 | public class ScheduledExecutorSubclassTe
930       */
931      public void testInvokeAny4() throws Exception {
932          ExecutorService e = new CustomExecutor(2);
933 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
934 +        l.add(new NPETask());
935          try {
738            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
739            l.add(new NPETask());
936              e.invokeAny(l);
937              shouldThrow();
938          } catch (ExecutionException success) {
# Line 752 | Line 948 | public class ScheduledExecutorSubclassTe
948      public void testInvokeAny5() throws Exception {
949          ExecutorService e = new CustomExecutor(2);
950          try {
951 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
951 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
952              l.add(new StringTask());
953              l.add(new StringTask());
954              String result = e.invokeAny(l);
# Line 794 | Line 990 | public class ScheduledExecutorSubclassTe
990       */
991      public void testInvokeAll3() throws Exception {
992          ExecutorService e = new CustomExecutor(2);
993 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
994 +        l.add(new StringTask());
995 +        l.add(null);
996          try {
798            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
799            l.add(new StringTask());
800            l.add(null);
997              e.invokeAll(l);
998              shouldThrow();
999          } catch (NullPointerException success) {
# Line 811 | Line 1007 | public class ScheduledExecutorSubclassTe
1007       */
1008      public void testInvokeAll4() throws Exception {
1009          ExecutorService e = new CustomExecutor(2);
1010 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1011 +        l.add(new NPETask());
1012 +        List<Future<String>> futures = e.invokeAll(l);
1013 +        assertEquals(1, futures.size());
1014          try {
1015 <            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();
1015 >            futures.get(0).get();
1016              shouldThrow();
1017          } catch (ExecutionException success) {
1018              assertTrue(success.getCause() instanceof NullPointerException);
# Line 832 | Line 1027 | public class ScheduledExecutorSubclassTe
1027      public void testInvokeAll5() throws Exception {
1028          ExecutorService e = new CustomExecutor(2);
1029          try {
1030 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1030 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1031              l.add(new StringTask());
1032              l.add(new StringTask());
1033 <            List<Future<String>> result = e.invokeAll(l);
1034 <            assertEquals(2, result.size());
1035 <            for (Future<String> future : result)
1033 >            List<Future<String>> futures = e.invokeAll(l);
1034 >            assertEquals(2, futures.size());
1035 >            for (Future<String> future : futures)
1036                  assertSame(TEST_STRING, future.get());
1037          } finally {
1038              joinPool(e);
# Line 863 | Line 1058 | public class ScheduledExecutorSubclassTe
1058       */
1059      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1060          ExecutorService e = new CustomExecutor(2);
1061 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1062 +        l.add(new StringTask());
1063          try {
867            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
868            l.add(new StringTask());
1064              e.invokeAny(l, MEDIUM_DELAY_MS, null);
1065              shouldThrow();
1066          } catch (NullPointerException success) {
# Line 892 | Line 1087 | public class ScheduledExecutorSubclassTe
1087       * timed invokeAny(c) throws NPE if c has null elements
1088       */
1089      public void testTimedInvokeAny3() throws Exception {
1090 <        final CountDownLatch latch = new CountDownLatch(1);
1090 >        CountDownLatch latch = new CountDownLatch(1);
1091          ExecutorService e = new CustomExecutor(2);
1092 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1093 +        l.add(latchAwaitingStringTask(latch));
1094 +        l.add(null);
1095          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);
1096              e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1097              shouldThrow();
1098          } catch (NullPointerException success) {
# Line 918 | Line 1107 | public class ScheduledExecutorSubclassTe
1107       */
1108      public void testTimedInvokeAny4() throws Exception {
1109          ExecutorService e = new CustomExecutor(2);
1110 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1111 +        l.add(new NPETask());
1112          try {
922            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
923            l.add(new NPETask());
1113              e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1114              shouldThrow();
1115          } catch (ExecutionException success) {
# Line 936 | Line 1125 | public class ScheduledExecutorSubclassTe
1125      public void testTimedInvokeAny5() throws Exception {
1126          ExecutorService e = new CustomExecutor(2);
1127          try {
1128 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1128 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1129              l.add(new StringTask());
1130              l.add(new StringTask());
1131              String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
# Line 965 | Line 1154 | public class ScheduledExecutorSubclassTe
1154       */
1155      public void testTimedInvokeAllNullTimeUnit() throws Exception {
1156          ExecutorService e = new CustomExecutor(2);
1157 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1158 +        l.add(new StringTask());
1159          try {
969            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
970            l.add(new StringTask());
1160              e.invokeAll(l, MEDIUM_DELAY_MS, null);
1161              shouldThrow();
1162          } catch (NullPointerException success) {
# Line 994 | Line 1183 | public class ScheduledExecutorSubclassTe
1183       */
1184      public void testTimedInvokeAll3() throws Exception {
1185          ExecutorService e = new CustomExecutor(2);
1186 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1187 +        l.add(new StringTask());
1188 +        l.add(null);
1189          try {
998            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
999            l.add(new StringTask());
1000            l.add(null);
1190              e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1191              shouldThrow();
1192          } catch (NullPointerException success) {
# Line 1011 | Line 1200 | public class ScheduledExecutorSubclassTe
1200       */
1201      public void testTimedInvokeAll4() throws Exception {
1202          ExecutorService e = new CustomExecutor(2);
1203 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1204 +        l.add(new NPETask());
1205 +        List<Future<String>> futures =
1206 +            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1207 +        assertEquals(1, futures.size());
1208          try {
1209 <            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();
1209 >            futures.get(0).get();
1210              shouldThrow();
1211          } catch (ExecutionException success) {
1212              assertTrue(success.getCause() instanceof NullPointerException);
# Line 1032 | Line 1221 | public class ScheduledExecutorSubclassTe
1221      public void testTimedInvokeAll5() throws Exception {
1222          ExecutorService e = new CustomExecutor(2);
1223          try {
1224 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1224 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1225              l.add(new StringTask());
1226              l.add(new StringTask());
1227 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1228 <            assertEquals(2, result.size());
1229 <            for (Future<String> future : result)
1227 >            List<Future<String>> futures =
1228 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1229 >            assertEquals(2, futures.size());
1230 >            for (Future<String> future : futures)
1231                  assertSame(TEST_STRING, future.get());
1232          } finally {
1233              joinPool(e);
# Line 1050 | Line 1240 | public class ScheduledExecutorSubclassTe
1240      public void testTimedInvokeAll6() throws Exception {
1241          ExecutorService e = new CustomExecutor(2);
1242          try {
1243 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1243 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1244              l.add(new StringTask());
1245              l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1246              l.add(new StringTask());
1247 <            List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1248 <            assertEquals(3, result.size());
1249 <            Iterator<Future<String>> it = result.iterator();
1250 <            Future<String> f1 = it.next();
1251 <            Future<String> f2 = it.next();
1252 <            Future<String> f3 = it.next();
1253 <            assertTrue(f1.isDone());
1064 <            assertTrue(f2.isDone());
1065 <            assertTrue(f3.isDone());
1066 <            assertFalse(f1.isCancelled());
1067 <            assertTrue(f2.isCancelled());
1247 >            List<Future<String>> futures =
1248 >                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1249 >            assertEquals(l.size(), futures.size());
1250 >            for (Future future : futures)
1251 >                assertTrue(future.isDone());
1252 >            assertFalse(futures.get(0).isCancelled());
1253 >            assertTrue(futures.get(1).isCancelled());
1254          } finally {
1255              joinPool(e);
1256          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines