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

Comparing jsr166/src/test/tck/ScheduledExecutorTest.java (file contents):
Revision 1.23 by jsr166, Mon Nov 16 04:57:10 2009 UTC vs.
Revision 1.43 by jsr166, Fri May 27 19:48:58 2011 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
# Line 9 | Line 9
9   import junit.framework.*;
10   import java.util.*;
11   import java.util.concurrent.*;
12 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
13   import java.util.concurrent.atomic.*;
14  
15   public class ScheduledExecutorTest extends JSR166TestCase {
16      public static void main(String[] args) {
17 <        junit.textui.TestRunner.run (suite());
17 >        junit.textui.TestRunner.run(suite());
18      }
19      public static Test suite() {
20 <        return new TestSuite(ScheduledExecutorTest.class);
20 >        return new TestSuite(ScheduledExecutorTest.class);
21      }
22  
22
23      /**
24       * execute successfully executes a runnable
25       */
26 <    public void testExecute() {
27 <        try {
28 <            TrackedShortRunnable runnable =new TrackedShortRunnable();
29 <            ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
30 <            p1.execute(runnable);
31 <            assertFalse(runnable.done);
32 <            Thread.sleep(SHORT_DELAY_MS);
33 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
34 <            try {
35 <                Thread.sleep(MEDIUM_DELAY_MS);
36 <            } catch (InterruptedException e){
37 <                unexpectedException();
38 <            }
39 <            assertTrue(runnable.done);
40 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
41 <            joinPool(p1);
42 <        }
43 <        catch (Exception e){
44 <            unexpectedException();
26 >    public void testExecute() throws InterruptedException {
27 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
28 >        final CountDownLatch done = new CountDownLatch(1);
29 >        final Runnable task = new CheckedRunnable() {
30 >            public void realRun() {
31 >                done.countDown();
32 >            }};
33 >        try {
34 >            p.execute(task);
35 >            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
36 >        } finally {
37 >            joinPool(p);
38          }
46
39      }
40  
49
41      /**
42       * delayed schedule of callable successfully executes after delay
43       */
44 <    public void testSchedule1() {
45 <        try {
46 <            TrackedCallable callable = new TrackedCallable();
47 <            ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
48 <            Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
49 <            assertFalse(callable.done);
50 <            Thread.sleep(MEDIUM_DELAY_MS);
51 <            assertTrue(callable.done);
52 <            assertEquals(Boolean.TRUE, f.get());
53 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
54 <            joinPool(p1);
55 <        } catch (RejectedExecutionException e){}
56 <        catch (Exception e){
57 <            e.printStackTrace();
58 <            unexpectedException();
44 >    public void testSchedule1() throws Exception {
45 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
46 >        final long t0 = System.nanoTime();
47 >        final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
48 >        final CountDownLatch done = new CountDownLatch(1);
49 >        try {
50 >            Callable task = new CheckedCallable<Boolean>() {
51 >                public Boolean realCall() {
52 >                    done.countDown();
53 >                    assertTrue(System.nanoTime() - t0 >= timeoutNanos);
54 >                    return Boolean.TRUE;
55 >                }};
56 >            Future f = p.schedule(task, SHORT_DELAY_MS, MILLISECONDS);
57 >            assertEquals(Boolean.TRUE, f.get());
58 >            assertTrue(System.nanoTime() - t0 >= timeoutNanos);
59 >            assertTrue(done.await(0L, MILLISECONDS));
60 >        } finally {
61 >            joinPool(p);
62          }
63      }
64  
65      /**
66 <     *  delayed schedule of runnable successfully executes after delay
67 <     */
68 <    public void testSchedule3() {
69 <        try {
70 <            TrackedShortRunnable runnable = new TrackedShortRunnable();
71 <            ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
72 <            p1.schedule(runnable, SMALL_DELAY_MS, TimeUnit.MILLISECONDS);
73 <            Thread.sleep(SHORT_DELAY_MS);
74 <            assertFalse(runnable.done);
75 <            Thread.sleep(MEDIUM_DELAY_MS);
76 <            assertTrue(runnable.done);
77 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
78 <            joinPool(p1);
79 <        } catch (Exception e){
80 <            unexpectedException();
66 >     * delayed schedule of runnable successfully executes after delay
67 >     */
68 >    public void testSchedule3() throws Exception {
69 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
70 >        final long t0 = System.nanoTime();
71 >        final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
72 >        final CountDownLatch done = new CountDownLatch(1);
73 >        try {
74 >            Runnable task = new CheckedRunnable() {
75 >                public void realRun() {
76 >                    done.countDown();
77 >                    assertTrue(System.nanoTime() - t0 >= timeoutNanos);
78 >                }};
79 >            Future f = p.schedule(task, SHORT_DELAY_MS, MILLISECONDS);
80 >            assertNull(f.get());
81 >            assertTrue(System.nanoTime() - t0 >= timeoutNanos);
82 >            assertTrue(done.await(0L, MILLISECONDS));
83 >        } finally {
84 >            joinPool(p);
85          }
86      }
87  
88      /**
89       * scheduleAtFixedRate executes runnable after given initial delay
90       */
91 <    public void testSchedule4() {
92 <        try {
93 <            TrackedShortRunnable runnable = new TrackedShortRunnable();
94 <            ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
95 <            ScheduledFuture h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
96 <            assertFalse(runnable.done);
97 <            Thread.sleep(MEDIUM_DELAY_MS);
98 <            assertTrue(runnable.done);
99 <            h.cancel(true);
100 <            joinPool(p1);
101 <        } catch (Exception e){
102 <            unexpectedException();
91 >    public void testSchedule4() throws Exception {
92 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
93 >        final long t0 = System.nanoTime();
94 >        final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
95 >        final CountDownLatch done = new CountDownLatch(1);
96 >        try {
97 >            Runnable task = new CheckedRunnable() {
98 >                public void realRun() {
99 >                    done.countDown();
100 >                    assertTrue(System.nanoTime() - t0 >= timeoutNanos);
101 >                }};
102 >            ScheduledFuture f =
103 >                p.scheduleAtFixedRate(task, SHORT_DELAY_MS,
104 >                                      SHORT_DELAY_MS, MILLISECONDS);
105 >            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
106 >            assertTrue(System.nanoTime() - t0 >= timeoutNanos);
107 >            f.cancel(true);
108 >        } finally {
109 >            joinPool(p);
110          }
111      }
112  
108    static class RunnableCounter implements Runnable {
109        AtomicInteger count = new AtomicInteger(0);
110        public void run() { count.getAndIncrement(); }
111    }
112
113      /**
114       * scheduleWithFixedDelay executes runnable after given initial delay
115       */
116 <    public void testSchedule5() {
117 <        try {
118 <            TrackedShortRunnable runnable = new TrackedShortRunnable();
119 <            ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
120 <            ScheduledFuture h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
121 <            assertFalse(runnable.done);
122 <            Thread.sleep(MEDIUM_DELAY_MS);
123 <            assertTrue(runnable.done);
124 <            h.cancel(true);
125 <            joinPool(p1);
126 <        } catch (Exception e){
127 <            unexpectedException();
116 >    public void testSchedule5() throws InterruptedException {
117 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
118 >        final long t0 = System.nanoTime();
119 >        final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
120 >        final CountDownLatch done = new CountDownLatch(1);
121 >        try {
122 >            Runnable task = new CheckedRunnable() {
123 >                public void realRun() {
124 >                    done.countDown();
125 >                    assertTrue(System.nanoTime() - t0 >= timeoutNanos);
126 >                }};
127 >            ScheduledFuture f =
128 >                p.scheduleWithFixedDelay(task, SHORT_DELAY_MS,
129 >                                         SHORT_DELAY_MS, MILLISECONDS);
130 >            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
131 >            assertTrue(System.nanoTime() - t0 >= timeoutNanos);
132 >            f.cancel(true);
133 >        } finally {
134 >            joinPool(p);
135          }
136      }
137  
138 +    static class RunnableCounter implements Runnable {
139 +        AtomicInteger count = new AtomicInteger(0);
140 +        public void run() { count.getAndIncrement(); }
141 +    }
142 +
143      /**
144       * scheduleAtFixedRate executes series of tasks at given rate
145       */
146 <    public void testFixedRateSequence() {
147 <        try {
148 <            ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
149 <            RunnableCounter counter = new RunnableCounter();
150 <            ScheduledFuture h =
151 <                p1.scheduleAtFixedRate(counter, 0, 1, TimeUnit.MILLISECONDS);
152 <            Thread.sleep(SMALL_DELAY_MS);
153 <            h.cancel(true);
154 <            int c = counter.count.get();
155 <            // By time scaling conventions, we must have at least
156 <            // an execution per SHORT delay, but no more than one SHORT more
157 <            assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
158 <            assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
147 <            joinPool(p1);
148 <        } catch (Exception e){
149 <            unexpectedException();
150 <        }
146 >    public void testFixedRateSequence() throws InterruptedException {
147 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
148 >        RunnableCounter counter = new RunnableCounter();
149 >        ScheduledFuture h =
150 >            p.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS);
151 >        delay(SMALL_DELAY_MS);
152 >        h.cancel(true);
153 >        int c = counter.count.get();
154 >        // By time scaling conventions, we must have at least
155 >        // an execution per SHORT delay, but no more than one SHORT more
156 >        assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
157 >        assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
158 >        joinPool(p);
159      }
160  
161      /**
162       * scheduleWithFixedDelay executes series of tasks with given period
163       */
164 <    public void testFixedDelaySequence() {
165 <        try {
166 <            ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
167 <            RunnableCounter counter = new RunnableCounter();
168 <            ScheduledFuture h =
169 <                p1.scheduleWithFixedDelay(counter, 0, 1, TimeUnit.MILLISECONDS);
170 <            Thread.sleep(SMALL_DELAY_MS);
171 <            h.cancel(true);
172 <            int c = counter.count.get();
173 <            assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
174 <            assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
167 <            joinPool(p1);
168 <        } catch (Exception e){
169 <            unexpectedException();
170 <        }
164 >    public void testFixedDelaySequence() throws InterruptedException {
165 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
166 >        RunnableCounter counter = new RunnableCounter();
167 >        ScheduledFuture h =
168 >            p.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS);
169 >        delay(SMALL_DELAY_MS);
170 >        h.cancel(true);
171 >        int c = counter.count.get();
172 >        assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
173 >        assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
174 >        joinPool(p);
175      }
176  
173
177      /**
178 <     *  execute (null) throws NPE
178 >     * execute(null) throws NPE
179       */
180 <    public void testExecuteNull() {
180 >    public void testExecuteNull() throws InterruptedException {
181          ScheduledThreadPoolExecutor se = null;
182          try {
183 <            se = new ScheduledThreadPoolExecutor(1);
184 <            se.execute(null);
183 >            se = new ScheduledThreadPoolExecutor(1);
184 >            se.execute(null);
185              shouldThrow();
186 <        } catch (NullPointerException success){}
184 <        catch (Exception e){
185 <            unexpectedException();
186 <        }
186 >        } catch (NullPointerException success) {}
187  
188 <        joinPool(se);
188 >        joinPool(se);
189      }
190  
191      /**
192 <     * schedule (null) throws NPE
192 >     * schedule(null) throws NPE
193       */
194 <    public void testScheduleNull() {
194 >    public void testScheduleNull() throws InterruptedException {
195          ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
196 <        try {
196 >        try {
197              TrackedCallable callable = null;
198 <            Future f = se.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
198 >            Future f = se.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
199              shouldThrow();
200 <        } catch (NullPointerException success){}
201 <        catch (Exception e){
202 <            unexpectedException();
203 <        }
204 <        joinPool(se);
200 >        } catch (NullPointerException success) {}
201 >        joinPool(se);
202      }
203  
204      /**
205       * execute throws RejectedExecutionException if shutdown
206       */
207 <    public void testSchedule1_RejectedExecutionException() {
207 >    public void testSchedule1_RejectedExecutionException() throws InterruptedException {
208          ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
209          try {
210              se.shutdown();
211              se.schedule(new NoOpRunnable(),
212 <                        MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
212 >                        MEDIUM_DELAY_MS, MILLISECONDS);
213              shouldThrow();
214 <        } catch (RejectedExecutionException success){
214 >        } catch (RejectedExecutionException success) {
215          } catch (SecurityException ok) {
216          }
217  
218          joinPool(se);
222
219      }
220  
221      /**
222       * schedule throws RejectedExecutionException if shutdown
223       */
224 <    public void testSchedule2_RejectedExecutionException() {
224 >    public void testSchedule2_RejectedExecutionException() throws InterruptedException {
225          ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
226          try {
227              se.shutdown();
228              se.schedule(new NoOpCallable(),
229 <                        MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
229 >                        MEDIUM_DELAY_MS, MILLISECONDS);
230              shouldThrow();
231 <        } catch (RejectedExecutionException success){
231 >        } catch (RejectedExecutionException success) {
232          } catch (SecurityException ok) {
233          }
234          joinPool(se);
# Line 241 | Line 237 | public class ScheduledExecutorTest exten
237      /**
238       * schedule callable throws RejectedExecutionException if shutdown
239       */
240 <     public void testSchedule3_RejectedExecutionException() {
241 <         ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
242 <         try {
240 >    public void testSchedule3_RejectedExecutionException() throws InterruptedException {
241 >        ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
242 >        try {
243              se.shutdown();
244              se.schedule(new NoOpCallable(),
245 <                        MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
245 >                        MEDIUM_DELAY_MS, MILLISECONDS);
246              shouldThrow();
247 <        } catch (RejectedExecutionException success){
247 >        } catch (RejectedExecutionException success) {
248          } catch (SecurityException ok) {
249          }
250 <         joinPool(se);
250 >        joinPool(se);
251      }
252  
253      /**
254 <     *  scheduleAtFixedRate throws RejectedExecutionException if shutdown
254 >     * scheduleAtFixedRate throws RejectedExecutionException if shutdown
255       */
256 <    public void testScheduleAtFixedRate1_RejectedExecutionException() {
256 >    public void testScheduleAtFixedRate1_RejectedExecutionException() throws InterruptedException {
257          ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
258          try {
259              se.shutdown();
260              se.scheduleAtFixedRate(new NoOpRunnable(),
261 <                                   MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
261 >                                   MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
262              shouldThrow();
263 <        } catch (RejectedExecutionException success){
263 >        } catch (RejectedExecutionException success) {
264          } catch (SecurityException ok) {
265          }
266          joinPool(se);
# Line 273 | Line 269 | public class ScheduledExecutorTest exten
269      /**
270       * scheduleWithFixedDelay throws RejectedExecutionException if shutdown
271       */
272 <    public void testScheduleWithFixedDelay1_RejectedExecutionException() {
272 >    public void testScheduleWithFixedDelay1_RejectedExecutionException() throws InterruptedException {
273          ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
274          try {
275              se.shutdown();
276              se.scheduleWithFixedDelay(new NoOpRunnable(),
277 <                                      MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
277 >                                      MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
278              shouldThrow();
279 <        } catch (RejectedExecutionException success){
279 >        } catch (RejectedExecutionException success) {
280          } catch (SecurityException ok) {
281          }
282          joinPool(se);
283      }
284  
285      /**
286 <     *  getActiveCount increases but doesn't overestimate, when a
287 <     *  thread becomes active
286 >     * getActiveCount increases but doesn't overestimate, when a
287 >     * thread becomes active
288       */
289 <    public void testGetActiveCount() {
290 <        ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2);
291 <        assertEquals(0, p2.getActiveCount());
292 <        p2.execute(new SmallRunnable());
293 <        try {
294 <            Thread.sleep(SHORT_DELAY_MS);
295 <        } catch (Exception e){
296 <            unexpectedException();
289 >    public void testGetActiveCount() throws InterruptedException {
290 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2);
291 >        final CountDownLatch threadStarted = new CountDownLatch(1);
292 >        final CountDownLatch done = new CountDownLatch(1);
293 >        try {
294 >            assertEquals(0, p.getActiveCount());
295 >            p.execute(new CheckedRunnable() {
296 >                public void realRun() throws InterruptedException {
297 >                    threadStarted.countDown();
298 >                    assertEquals(1, p.getActiveCount());
299 >                    done.await();
300 >                }});
301 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
302 >            assertEquals(1, p.getActiveCount());
303 >        } finally {
304 >            done.countDown();
305 >            joinPool(p);
306          }
302        assertEquals(1, p2.getActiveCount());
303        joinPool(p2);
307      }
308  
309      /**
310 <     *    getCompletedTaskCount increases, but doesn't overestimate,
311 <     *   when tasks complete
310 >     * getCompletedTaskCount increases, but doesn't overestimate,
311 >     * when tasks complete
312       */
313 <    public void testGetCompletedTaskCount() {
314 <        ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2);
315 <        assertEquals(0, p2.getCompletedTaskCount());
316 <        p2.execute(new SmallRunnable());
317 <        try {
318 <            Thread.sleep(MEDIUM_DELAY_MS);
319 <        } catch (Exception e){
320 <            unexpectedException();
313 >    public void testGetCompletedTaskCount() throws InterruptedException {
314 >        final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2);
315 >        final CountDownLatch threadStarted = new CountDownLatch(1);
316 >        final CountDownLatch threadProceed = new CountDownLatch(1);
317 >        final CountDownLatch threadDone = new CountDownLatch(1);
318 >        try {
319 >            assertEquals(0, p.getCompletedTaskCount());
320 >            p.execute(new CheckedRunnable() {
321 >                public void realRun() throws InterruptedException {
322 >                    threadStarted.countDown();
323 >                    assertEquals(0, p.getCompletedTaskCount());
324 >                    threadProceed.await();
325 >                    threadDone.countDown();
326 >                }});
327 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
328 >            assertEquals(0, p.getCompletedTaskCount());
329 >            threadProceed.countDown();
330 >            threadDone.await();
331 >            delay(SHORT_DELAY_MS);
332 >            assertEquals(1, p.getCompletedTaskCount());
333 >        } finally {
334 >            joinPool(p);
335          }
319        assertEquals(1, p2.getCompletedTaskCount());
320        joinPool(p2);
336      }
337  
338      /**
339 <     *  getCorePoolSize returns size given in constructor if not otherwise set
339 >     * getCorePoolSize returns size given in constructor if not otherwise set
340       */
341 <    public void testGetCorePoolSize() {
342 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
343 <        assertEquals(1, p1.getCorePoolSize());
344 <        joinPool(p1);
341 >    public void testGetCorePoolSize() throws InterruptedException {
342 >        ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
343 >        assertEquals(1, p.getCorePoolSize());
344 >        joinPool(p);
345      }
346  
347      /**
348 <     *    getLargestPoolSize increases, but doesn't overestimate, when
349 <     *   multiple threads active
348 >     * getLargestPoolSize increases, but doesn't overestimate, when
349 >     * multiple threads active
350       */
351 <    public void testGetLargestPoolSize() {
352 <        ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2);
353 <        assertEquals(0, p2.getLargestPoolSize());
354 <        p2.execute(new SmallRunnable());
355 <        p2.execute(new SmallRunnable());
356 <        try {
357 <            Thread.sleep(SHORT_DELAY_MS);
358 <        } catch (Exception e){
359 <            unexpectedException();
351 >    public void testGetLargestPoolSize() throws InterruptedException {
352 >        final int THREADS = 3;
353 >        final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(THREADS);
354 >        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
355 >        final CountDownLatch done = new CountDownLatch(1);
356 >        try {
357 >            assertEquals(0, p.getLargestPoolSize());
358 >            for (int i = 0; i < THREADS; i++)
359 >                p.execute(new CheckedRunnable() {
360 >                    public void realRun() throws InterruptedException {
361 >                        threadsStarted.countDown();
362 >                        done.await();
363 >                        assertEquals(THREADS, p.getLargestPoolSize());
364 >                    }});
365 >            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
366 >            assertEquals(THREADS, p.getLargestPoolSize());
367 >        } finally {
368 >            done.countDown();
369 >            joinPool(p);
370 >            assertEquals(THREADS, p.getLargestPoolSize());
371          }
346        assertEquals(2, p2.getLargestPoolSize());
347        joinPool(p2);
372      }
373  
374      /**
375 <     *   getPoolSize increases, but doesn't overestimate, when threads
376 <     *   become active
375 >     * getPoolSize increases, but doesn't overestimate, when threads
376 >     * become active
377       */
378 <    public void testGetPoolSize() {
379 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
380 <        assertEquals(0, p1.getPoolSize());
381 <        p1.execute(new SmallRunnable());
382 <        assertEquals(1, p1.getPoolSize());
383 <        joinPool(p1);
378 >    public void testGetPoolSize() throws InterruptedException {
379 >        final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
380 >        final CountDownLatch threadStarted = new CountDownLatch(1);
381 >        final CountDownLatch done = new CountDownLatch(1);
382 >        try {
383 >            assertEquals(0, p.getPoolSize());
384 >            p.execute(new CheckedRunnable() {
385 >                public void realRun() throws InterruptedException {
386 >                    threadStarted.countDown();
387 >                    assertEquals(1, p.getPoolSize());
388 >                    done.await();
389 >                }});
390 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
391 >            assertEquals(1, p.getPoolSize());
392 >        } finally {
393 >            done.countDown();
394 >            joinPool(p);
395 >        }
396      }
397  
398      /**
399 <     *    getTaskCount increases, but doesn't overestimate, when tasks
400 <     *    submitted
399 >     * getTaskCount increases, but doesn't overestimate, when tasks
400 >     * submitted
401       */
402 <    public void testGetTaskCount() {
403 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
404 <        assertEquals(0, p1.getTaskCount());
405 <        for (int i = 0; i < 5; i++)
406 <            p1.execute(new SmallRunnable());
407 <        try {
408 <            Thread.sleep(SHORT_DELAY_MS);
409 <        } catch (Exception e){
410 <            unexpectedException();
402 >    public void testGetTaskCount() throws InterruptedException {
403 >        final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
404 >        final CountDownLatch threadStarted = new CountDownLatch(1);
405 >        final CountDownLatch done = new CountDownLatch(1);
406 >        final int TASKS = 5;
407 >        try {
408 >            assertEquals(0, p.getTaskCount());
409 >            for (int i = 0; i < TASKS; i++)
410 >                p.execute(new CheckedRunnable() {
411 >                    public void realRun() throws InterruptedException {
412 >                        threadStarted.countDown();
413 >                        done.await();
414 >                    }});
415 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
416 >            assertEquals(TASKS, p.getTaskCount());
417 >        } finally {
418 >            done.countDown();
419 >            joinPool(p);
420          }
376        assertEquals(5, p1.getTaskCount());
377        joinPool(p1);
421      }
422  
423      /**
424       * getThreadFactory returns factory in constructor if not set
425       */
426 <    public void testGetThreadFactory() {
426 >    public void testGetThreadFactory() throws InterruptedException {
427          ThreadFactory tf = new SimpleThreadFactory();
428 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, tf);
428 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, tf);
429          assertSame(tf, p.getThreadFactory());
430          joinPool(p);
431      }
# Line 390 | Line 433 | public class ScheduledExecutorTest exten
433      /**
434       * setThreadFactory sets the thread factory returned by getThreadFactory
435       */
436 <    public void testSetThreadFactory() {
436 >    public void testSetThreadFactory() throws InterruptedException {
437          ThreadFactory tf = new SimpleThreadFactory();
438 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
438 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
439          p.setThreadFactory(tf);
440          assertSame(tf, p.getThreadFactory());
441          joinPool(p);
# Line 401 | Line 444 | public class ScheduledExecutorTest exten
444      /**
445       * setThreadFactory(null) throws NPE
446       */
447 <    public void testSetThreadFactoryNull() {
448 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
447 >    public void testSetThreadFactoryNull() throws InterruptedException {
448 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
449          try {
450              p.setThreadFactory(null);
451              shouldThrow();
# Line 413 | Line 456 | public class ScheduledExecutorTest exten
456      }
457  
458      /**
459 <     *   is isShutDown is false before shutdown, true after
459 >     * isShutdown is false before shutdown, true after
460       */
461      public void testIsShutdown() {
462  
463 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
463 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
464          try {
465 <            assertFalse(p1.isShutdown());
465 >            assertFalse(p.isShutdown());
466          }
467          finally {
468 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
468 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
469          }
470 <        assertTrue(p1.isShutdown());
470 >        assertTrue(p.isShutdown());
471      }
472  
430
473      /**
474 <     *   isTerminated is false before termination, true after
474 >     * isTerminated is false before termination, true after
475       */
476 <    public void testIsTerminated() {
477 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
476 >    public void testIsTerminated() throws InterruptedException {
477 >        final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
478 >        final CountDownLatch threadStarted = new CountDownLatch(1);
479 >        final CountDownLatch done = new CountDownLatch(1);
480 >        assertFalse(p.isTerminated());
481          try {
482 <            p1.execute(new SmallRunnable());
482 >            p.execute(new CheckedRunnable() {
483 >                public void realRun() throws InterruptedException {
484 >                    assertFalse(p.isTerminated());
485 >                    threadStarted.countDown();
486 >                    done.await();
487 >                }});
488 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
489 >            assertFalse(p.isTerminating());
490 >            done.countDown();
491          } finally {
492 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
440 <        }
441 <        try {
442 <            assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
443 <            assertTrue(p1.isTerminated());
444 <        } catch (Exception e){
445 <            unexpectedException();
492 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
493          }
494 +        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
495 +        assertTrue(p.isTerminated());
496      }
497  
498      /**
499 <     *  isTerminating is not true when running or when terminated
499 >     * isTerminating is not true when running or when terminated
500       */
501 <    public void testIsTerminating() {
502 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
503 <        assertFalse(p1.isTerminating());
501 >    public void testIsTerminating() throws InterruptedException {
502 >        final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
503 >        final CountDownLatch threadStarted = new CountDownLatch(1);
504 >        final CountDownLatch done = new CountDownLatch(1);
505          try {
506 <            p1.execute(new SmallRunnable());
507 <            assertFalse(p1.isTerminating());
506 >            assertFalse(p.isTerminating());
507 >            p.execute(new CheckedRunnable() {
508 >                public void realRun() throws InterruptedException {
509 >                    assertFalse(p.isTerminating());
510 >                    threadStarted.countDown();
511 >                    done.await();
512 >                }});
513 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
514 >            assertFalse(p.isTerminating());
515 >            done.countDown();
516          } finally {
517 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
460 <        }
461 <        try {
462 <            assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
463 <            assertTrue(p1.isTerminated());
464 <            assertFalse(p1.isTerminating());
465 <        } catch (Exception e){
466 <            unexpectedException();
517 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
518          }
519 +        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
520 +        assertTrue(p.isTerminated());
521 +        assertFalse(p.isTerminating());
522      }
523  
524      /**
525       * getQueue returns the work queue, which contains queued tasks
526       */
527 <    public void testGetQueue() {
528 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
529 <        ScheduledFuture[] tasks = new ScheduledFuture[5];
530 <        for (int i = 0; i < 5; i++){
477 <            tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS);
478 <        }
527 >    public void testGetQueue() throws InterruptedException {
528 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
529 >        final CountDownLatch threadStarted = new CountDownLatch(1);
530 >        final CountDownLatch done = new CountDownLatch(1);
531          try {
532 <            Thread.sleep(SHORT_DELAY_MS);
533 <            BlockingQueue<Runnable> q = p1.getQueue();
534 <            assertTrue(q.contains(tasks[4]));
532 >            ScheduledFuture[] tasks = new ScheduledFuture[5];
533 >            for (int i = 0; i < tasks.length; i++) {
534 >                Runnable r = new CheckedRunnable() {
535 >                    public void realRun() throws InterruptedException {
536 >                        threadStarted.countDown();
537 >                        done.await();
538 >                    }};
539 >                tasks[i] = p.schedule(r, 1, MILLISECONDS);
540 >            }
541 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
542 >            BlockingQueue<Runnable> q = p.getQueue();
543 >            assertTrue(q.contains(tasks[tasks.length - 1]));
544              assertFalse(q.contains(tasks[0]));
484        } catch (Exception e) {
485            unexpectedException();
545          } finally {
546 <            joinPool(p1);
546 >            done.countDown();
547 >            joinPool(p);
548          }
549      }
550  
551      /**
552       * remove(task) removes queued task, and fails to remove active task
553       */
554 <    public void testRemove() {
555 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
554 >    public void testRemove() throws InterruptedException {
555 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
556          ScheduledFuture[] tasks = new ScheduledFuture[5];
557 <        for (int i = 0; i < 5; i++){
558 <            tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS);
499 <        }
557 >        final CountDownLatch threadStarted = new CountDownLatch(1);
558 >        final CountDownLatch done = new CountDownLatch(1);
559          try {
560 <            Thread.sleep(SHORT_DELAY_MS);
561 <            BlockingQueue<Runnable> q = p1.getQueue();
562 <            assertFalse(p1.remove((Runnable)tasks[0]));
560 >            for (int i = 0; i < tasks.length; i++) {
561 >                Runnable r = new CheckedRunnable() {
562 >                    public void realRun() throws InterruptedException {
563 >                        threadStarted.countDown();
564 >                        done.await();
565 >                    }};
566 >                tasks[i] = p.schedule(r, 1, MILLISECONDS);
567 >            }
568 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
569 >            BlockingQueue<Runnable> q = p.getQueue();
570 >            assertFalse(p.remove((Runnable)tasks[0]));
571              assertTrue(q.contains((Runnable)tasks[4]));
572              assertTrue(q.contains((Runnable)tasks[3]));
573 <            assertTrue(p1.remove((Runnable)tasks[4]));
574 <            assertFalse(p1.remove((Runnable)tasks[4]));
573 >            assertTrue(p.remove((Runnable)tasks[4]));
574 >            assertFalse(p.remove((Runnable)tasks[4]));
575              assertFalse(q.contains((Runnable)tasks[4]));
576              assertTrue(q.contains((Runnable)tasks[3]));
577 <            assertTrue(p1.remove((Runnable)tasks[3]));
577 >            assertTrue(p.remove((Runnable)tasks[3]));
578              assertFalse(q.contains((Runnable)tasks[3]));
512        } catch (Exception e) {
513            unexpectedException();
579          } finally {
580 <            joinPool(p1);
580 >            done.countDown();
581 >            joinPool(p);
582          }
583      }
584  
585      /**
586 <     *  purge removes cancelled tasks from the queue
586 >     * purge eventually removes cancelled tasks from the queue
587       */
588 <    public void testPurge() {
589 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
588 >    public void testPurge() throws InterruptedException {
589 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
590          ScheduledFuture[] tasks = new ScheduledFuture[5];
591 <        for (int i = 0; i < 5; i++){
592 <            tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
593 <        }
591 >        for (int i = 0; i < tasks.length; i++)
592 >            tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(),
593 >                                  LONG_DELAY_MS, MILLISECONDS);
594          try {
595 <            int max = 5;
595 >            int max = tasks.length;
596              if (tasks[4].cancel(true)) --max;
597              if (tasks[3].cancel(true)) --max;
598              // There must eventually be an interference-free point at
599              // which purge will not fail. (At worst, when queue is empty.)
600 <            int k;
601 <            for (k = 0; k < SMALL_DELAY_MS; ++k) {
602 <                p1.purge();
603 <                long count = p1.getTaskCount();
604 <                if (count >= 0 && count <= max)
605 <                    break;
606 <                Thread.sleep(1);
607 <            }
542 <            assertTrue(k < SMALL_DELAY_MS);
543 <        } catch (Exception e) {
544 <            unexpectedException();
600 >            long startTime = System.nanoTime();
601 >            do {
602 >                p.purge();
603 >                long count = p.getTaskCount();
604 >                if (count == max)
605 >                    return;
606 >            } while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
607 >            fail("Purge failed to remove cancelled tasks");
608          } finally {
609 <            joinPool(p1);
609 >            for (ScheduledFuture task : tasks)
610 >                task.cancel(true);
611 >            joinPool(p);
612          }
613      }
614  
615      /**
616 <     *  shutDownNow returns a list containing tasks that were not run
616 >     * shutdownNow returns a list containing tasks that were not run
617       */
618 <    public void testShutDownNow() {
619 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
618 >    public void testShutdownNow() {
619 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
620          for (int i = 0; i < 5; i++)
621 <            p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
622 <        List l;
621 >            p.schedule(new SmallPossiblyInterruptedRunnable(),
622 >                       LONG_DELAY_MS, MILLISECONDS);
623          try {
624 <            l = p1.shutdownNow();
624 >            List<Runnable> l = p.shutdownNow();
625 >            assertTrue(p.isShutdown());
626 >            assertEquals(5, l.size());
627          } catch (SecurityException ok) {
628 <            return;
628 >            // Allowed in case test doesn't have privs
629 >        } finally {
630 >            joinPool(p);
631          }
563        assertTrue(p1.isShutdown());
564        assertTrue(l.size() > 0 && l.size() <= 5);
565        joinPool(p1);
632      }
633  
634      /**
635       * In default setting, shutdown cancels periodic but not delayed
636       * tasks at shutdown
637       */
638 <    public void testShutDown1() {
639 <        try {
640 <            ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
641 <            assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy());
576 <            assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy());
577 <
578 <            ScheduledFuture[] tasks = new ScheduledFuture[5];
579 <            for (int i = 0; i < 5; i++)
580 <                tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
581 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
582 <            BlockingQueue q = p1.getQueue();
583 <            for (Iterator it = q.iterator(); it.hasNext();) {
584 <                ScheduledFuture t = (ScheduledFuture)it.next();
585 <                assertFalse(t.isCancelled());
586 <            }
587 <            assertTrue(p1.isShutdown());
588 <            Thread.sleep(SMALL_DELAY_MS);
589 <            for (int i = 0; i < 5; ++i) {
590 <                assertTrue(tasks[i].isDone());
591 <                assertFalse(tasks[i].isCancelled());
592 <            }
638 >    public void testShutdown1() throws InterruptedException {
639 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
640 >        assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
641 >        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
642  
643 +        ScheduledFuture[] tasks = new ScheduledFuture[5];
644 +        for (int i = 0; i < tasks.length; i++)
645 +            tasks[i] = p.schedule(new NoOpRunnable(),
646 +                                  SHORT_DELAY_MS, MILLISECONDS);
647 +        try { p.shutdown(); } catch (SecurityException ok) { return; }
648 +        BlockingQueue<Runnable> q = p.getQueue();
649 +        for (ScheduledFuture task : tasks) {
650 +            assertFalse(task.isDone());
651 +            assertFalse(task.isCancelled());
652 +            assertTrue(q.contains(task));
653          }
654 <        catch (Exception ex) {
655 <            unexpectedException();
654 >        assertTrue(p.isShutdown());
655 >        assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
656 >        assertTrue(p.isTerminated());
657 >        for (ScheduledFuture task : tasks) {
658 >            assertTrue(task.isDone());
659 >            assertFalse(task.isCancelled());
660          }
661      }
662  
600
663      /**
664       * If setExecuteExistingDelayedTasksAfterShutdownPolicy is false,
665       * delayed tasks are cancelled at shutdown
666       */
667 <    public void testShutDown2() {
668 <        try {
669 <            ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
670 <            p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
671 <            ScheduledFuture[] tasks = new ScheduledFuture[5];
672 <            for (int i = 0; i < 5; i++)
673 <                tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
674 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
675 <            assertTrue(p1.isShutdown());
676 <            BlockingQueue q = p1.getQueue();
677 <            assertTrue(q.isEmpty());
678 <            Thread.sleep(SMALL_DELAY_MS);
679 <            assertTrue(p1.isTerminated());
680 <        }
681 <        catch (Exception ex) {
682 <            unexpectedException();
667 >    public void testShutdown2() throws InterruptedException {
668 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
669 >        p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
670 >        assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
671 >        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
672 >        ScheduledFuture[] tasks = new ScheduledFuture[5];
673 >        for (int i = 0; i < tasks.length; i++)
674 >            tasks[i] = p.schedule(new NoOpRunnable(),
675 >                                  SHORT_DELAY_MS, MILLISECONDS);
676 >        BlockingQueue q = p.getQueue();
677 >        assertEquals(tasks.length, q.size());
678 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
679 >        assertTrue(p.isShutdown());
680 >        assertTrue(q.isEmpty());
681 >        assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
682 >        assertTrue(p.isTerminated());
683 >        for (ScheduledFuture task : tasks) {
684 >            assertTrue(task.isDone());
685 >            assertTrue(task.isCancelled());
686          }
687      }
688  
624
689      /**
690       * If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false,
691 <     * periodic tasks are not cancelled at shutdown
691 >     * periodic tasks are cancelled at shutdown
692       */
693 <    public void testShutDown3() {
694 <        try {
695 <            ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
696 <            p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
697 <            ScheduledFuture task =
698 <                p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS);
699 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
700 <            assertTrue(p1.isShutdown());
701 <            BlockingQueue q = p1.getQueue();
702 <            assertTrue(q.isEmpty());
703 <            Thread.sleep(SHORT_DELAY_MS);
704 <            assertTrue(p1.isTerminated());
705 <        }
706 <        catch (Exception ex) {
707 <            unexpectedException();
708 <        }
693 >    public void testShutdown3() throws InterruptedException {
694 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
695 >        assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
696 >        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
697 >        p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
698 >        assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
699 >        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
700 >        long initialDelay = LONG_DELAY_MS;
701 >        ScheduledFuture task =
702 >            p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay,
703 >                                  5, MILLISECONDS);
704 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
705 >        assertTrue(p.isShutdown());
706 >        assertTrue(p.getQueue().isEmpty());
707 >        assertTrue(task.isDone());
708 >        assertTrue(task.isCancelled());
709 >        joinPool(p);
710      }
711  
712      /**
713       * if setContinueExistingPeriodicTasksAfterShutdownPolicy is true,
714 <     * periodic tasks are cancelled at shutdown
714 >     * periodic tasks are not cancelled at shutdown
715       */
716 <    public void testShutDown4() {
717 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
718 <        try {
719 <            p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
716 >    public void testShutdown4() throws InterruptedException {
717 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
718 >        final CountDownLatch counter = new CountDownLatch(2);
719 >        try {
720 >            p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
721 >            assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
722 >            assertTrue(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
723 >            final Runnable r = new CheckedRunnable() {
724 >                public void realRun() {
725 >                    counter.countDown();
726 >                }};
727              ScheduledFuture task =
728 <                p1.scheduleAtFixedRate(new NoOpRunnable(), 1, 1, TimeUnit.MILLISECONDS);
728 >                p.scheduleAtFixedRate(r, 1, 1, MILLISECONDS);
729 >            assertFalse(task.isDone());
730              assertFalse(task.isCancelled());
731 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
731 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
732              assertFalse(task.isCancelled());
733 <            assertFalse(p1.isTerminated());
734 <            assertTrue(p1.isShutdown());
735 <            Thread.sleep(SHORT_DELAY_MS);
733 >            assertFalse(p.isTerminated());
734 >            assertTrue(p.isShutdown());
735 >            assertTrue(counter.await(SMALL_DELAY_MS, MILLISECONDS));
736              assertFalse(task.isCancelled());
737 <            assertTrue(task.cancel(true));
737 >            assertTrue(task.cancel(false));
738              assertTrue(task.isDone());
739 <            Thread.sleep(SHORT_DELAY_MS);
740 <            assertTrue(p1.isTerminated());
741 <        }
669 <        catch (Exception ex) {
670 <            unexpectedException();
739 >            assertTrue(task.isCancelled());
740 >            assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
741 >            assertTrue(p.isTerminated());
742          }
743          finally {
744 <            joinPool(p1);
744 >            joinPool(p);
745          }
746      }
747  
748      /**
749       * completed submit of callable returns result
750       */
751 <    public void testSubmitCallable() {
751 >    public void testSubmitCallable() throws Exception {
752          ExecutorService e = new ScheduledThreadPoolExecutor(2);
753          try {
754              Future<String> future = e.submit(new StringTask());
755              String result = future.get();
756              assertSame(TEST_STRING, result);
686        }
687        catch (ExecutionException ex) {
688            unexpectedException();
689        }
690        catch (InterruptedException ex) {
691            unexpectedException();
757          } finally {
758              joinPool(e);
759          }
# Line 697 | Line 762 | public class ScheduledExecutorTest exten
762      /**
763       * completed submit of runnable returns successfully
764       */
765 <    public void testSubmitRunnable() {
765 >    public void testSubmitRunnable() throws Exception {
766          ExecutorService e = new ScheduledThreadPoolExecutor(2);
767          try {
768              Future<?> future = e.submit(new NoOpRunnable());
769              future.get();
770              assertTrue(future.isDone());
706        }
707        catch (ExecutionException ex) {
708            unexpectedException();
709        }
710        catch (InterruptedException ex) {
711            unexpectedException();
771          } finally {
772              joinPool(e);
773          }
# Line 717 | Line 776 | public class ScheduledExecutorTest exten
776      /**
777       * completed submit of (runnable, result) returns result
778       */
779 <    public void testSubmitRunnable2() {
779 >    public void testSubmitRunnable2() throws Exception {
780          ExecutorService e = new ScheduledThreadPoolExecutor(2);
781          try {
782              Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
783              String result = future.get();
784              assertSame(TEST_STRING, result);
726        }
727        catch (ExecutionException ex) {
728            unexpectedException();
729        }
730        catch (InterruptedException ex) {
731            unexpectedException();
785          } finally {
786              joinPool(e);
787          }
# Line 737 | Line 790 | public class ScheduledExecutorTest exten
790      /**
791       * invokeAny(null) throws NPE
792       */
793 <    public void testInvokeAny1() {
793 >    public void testInvokeAny1() throws Exception {
794          ExecutorService e = new ScheduledThreadPoolExecutor(2);
795          try {
796              e.invokeAny(null);
797 +            shouldThrow();
798          } catch (NullPointerException success) {
745        } catch (Exception ex) {
746            unexpectedException();
799          } finally {
800              joinPool(e);
801          }
# Line 752 | Line 804 | public class ScheduledExecutorTest exten
804      /**
805       * invokeAny(empty collection) throws IAE
806       */
807 <    public void testInvokeAny2() {
807 >    public void testInvokeAny2() throws Exception {
808          ExecutorService e = new ScheduledThreadPoolExecutor(2);
809          try {
810              e.invokeAny(new ArrayList<Callable<String>>());
811 +            shouldThrow();
812          } catch (IllegalArgumentException success) {
760        } catch (Exception ex) {
761            unexpectedException();
813          } finally {
814              joinPool(e);
815          }
# Line 767 | Line 818 | public class ScheduledExecutorTest exten
818      /**
819       * invokeAny(c) throws NPE if c has null elements
820       */
821 <    public void testInvokeAny3() {
821 >    public void testInvokeAny3() throws Exception {
822 >        CountDownLatch latch = new CountDownLatch(1);
823          ExecutorService e = new ScheduledThreadPoolExecutor(2);
824 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
825 +        l.add(latchAwaitingStringTask(latch));
826 +        l.add(null);
827          try {
773            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
774            l.add(new StringTask());
775            l.add(null);
828              e.invokeAny(l);
829 +            shouldThrow();
830          } catch (NullPointerException success) {
778        } catch (Exception ex) {
779            unexpectedException();
831          } finally {
832 +            latch.countDown();
833              joinPool(e);
834          }
835      }
# Line 785 | Line 837 | public class ScheduledExecutorTest exten
837      /**
838       * invokeAny(c) throws ExecutionException if no task completes
839       */
840 <    public void testInvokeAny4() {
840 >    public void testInvokeAny4() throws Exception {
841          ExecutorService e = new ScheduledThreadPoolExecutor(2);
842 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
843 +        l.add(new NPETask());
844          try {
791            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
792            l.add(new NPETask());
845              e.invokeAny(l);
846 +            shouldThrow();
847          } catch (ExecutionException success) {
848 <        } catch (Exception ex) {
796 <            unexpectedException();
848 >            assertTrue(success.getCause() instanceof NullPointerException);
849          } finally {
850              joinPool(e);
851          }
# Line 802 | Line 854 | public class ScheduledExecutorTest exten
854      /**
855       * invokeAny(c) returns result of some task
856       */
857 <    public void testInvokeAny5() {
857 >    public void testInvokeAny5() throws Exception {
858          ExecutorService e = new ScheduledThreadPoolExecutor(2);
859          try {
860 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
860 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
861              l.add(new StringTask());
862              l.add(new StringTask());
863              String result = e.invokeAny(l);
864              assertSame(TEST_STRING, result);
813        } catch (ExecutionException success) {
814        } catch (Exception ex) {
815            unexpectedException();
865          } finally {
866              joinPool(e);
867          }
# Line 821 | Line 870 | public class ScheduledExecutorTest exten
870      /**
871       * invokeAll(null) throws NPE
872       */
873 <    public void testInvokeAll1() {
873 >    public void testInvokeAll1() throws Exception {
874          ExecutorService e = new ScheduledThreadPoolExecutor(2);
875          try {
876              e.invokeAll(null);
877 +            shouldThrow();
878          } catch (NullPointerException success) {
829        } catch (Exception ex) {
830            unexpectedException();
879          } finally {
880              joinPool(e);
881          }
# Line 836 | Line 884 | public class ScheduledExecutorTest exten
884      /**
885       * invokeAll(empty collection) returns empty collection
886       */
887 <    public void testInvokeAll2() {
887 >    public void testInvokeAll2() throws Exception {
888          ExecutorService e = new ScheduledThreadPoolExecutor(2);
889          try {
890              List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
891              assertTrue(r.isEmpty());
844        } catch (Exception ex) {
845            unexpectedException();
892          } finally {
893              joinPool(e);
894          }
# Line 851 | Line 897 | public class ScheduledExecutorTest exten
897      /**
898       * invokeAll(c) throws NPE if c has null elements
899       */
900 <    public void testInvokeAll3() {
900 >    public void testInvokeAll3() throws Exception {
901          ExecutorService e = new ScheduledThreadPoolExecutor(2);
902 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
903 +        l.add(new StringTask());
904 +        l.add(null);
905          try {
857            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
858            l.add(new StringTask());
859            l.add(null);
906              e.invokeAll(l);
907 +            shouldThrow();
908          } catch (NullPointerException success) {
862        } catch (Exception ex) {
863            unexpectedException();
909          } finally {
910              joinPool(e);
911          }
# Line 869 | Line 914 | public class ScheduledExecutorTest exten
914      /**
915       * get of invokeAll(c) throws exception on failed task
916       */
917 <    public void testInvokeAll4() {
917 >    public void testInvokeAll4() throws Exception {
918          ExecutorService e = new ScheduledThreadPoolExecutor(2);
919 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
920 +        l.add(new NPETask());
921 +        List<Future<String>> futures = e.invokeAll(l);
922 +        assertEquals(1, futures.size());
923          try {
924 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
925 <            l.add(new NPETask());
877 <            List<Future<String>> result = e.invokeAll(l);
878 <            assertEquals(1, result.size());
879 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
880 <                it.next().get();
924 >            futures.get(0).get();
925 >            shouldThrow();
926          } catch (ExecutionException success) {
927 <        } catch (Exception ex) {
883 <            unexpectedException();
927 >            assertTrue(success.getCause() instanceof NullPointerException);
928          } finally {
929              joinPool(e);
930          }
# Line 889 | Line 933 | public class ScheduledExecutorTest exten
933      /**
934       * invokeAll(c) returns results of all completed tasks
935       */
936 <    public void testInvokeAll5() {
936 >    public void testInvokeAll5() throws Exception {
937          ExecutorService e = new ScheduledThreadPoolExecutor(2);
938          try {
939 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
939 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
940              l.add(new StringTask());
941              l.add(new StringTask());
942 <            List<Future<String>> result = e.invokeAll(l);
943 <            assertEquals(2, result.size());
944 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
945 <                assertSame(TEST_STRING, it.next().get());
902 <        } catch (ExecutionException success) {
903 <        } catch (Exception ex) {
904 <            unexpectedException();
942 >            List<Future<String>> futures = e.invokeAll(l);
943 >            assertEquals(2, futures.size());
944 >            for (Future<String> future : futures)
945 >                assertSame(TEST_STRING, future.get());
946          } finally {
947              joinPool(e);
948          }
# Line 910 | Line 951 | public class ScheduledExecutorTest exten
951      /**
952       * timed invokeAny(null) throws NPE
953       */
954 <    public void testTimedInvokeAny1() {
954 >    public void testTimedInvokeAny1() throws Exception {
955          ExecutorService e = new ScheduledThreadPoolExecutor(2);
956          try {
957 <            e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
957 >            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
958 >            shouldThrow();
959          } catch (NullPointerException success) {
918        } catch (Exception ex) {
919            unexpectedException();
960          } finally {
961              joinPool(e);
962          }
# Line 925 | Line 965 | public class ScheduledExecutorTest exten
965      /**
966       * timed invokeAny(,,null) throws NPE
967       */
968 <    public void testTimedInvokeAnyNullTimeUnit() {
968 >    public void testTimedInvokeAnyNullTimeUnit() throws Exception {
969          ExecutorService e = new ScheduledThreadPoolExecutor(2);
970 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
971 +        l.add(new StringTask());
972          try {
931            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
932            l.add(new StringTask());
973              e.invokeAny(l, MEDIUM_DELAY_MS, null);
974 +            shouldThrow();
975          } catch (NullPointerException success) {
935        } catch (Exception ex) {
936            unexpectedException();
976          } finally {
977              joinPool(e);
978          }
# Line 942 | Line 981 | public class ScheduledExecutorTest exten
981      /**
982       * timed invokeAny(empty collection) throws IAE
983       */
984 <    public void testTimedInvokeAny2() {
984 >    public void testTimedInvokeAny2() throws Exception {
985          ExecutorService e = new ScheduledThreadPoolExecutor(2);
986          try {
987 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
987 >            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
988 >            shouldThrow();
989          } catch (IllegalArgumentException success) {
950        } catch (Exception ex) {
951            unexpectedException();
990          } finally {
991              joinPool(e);
992          }
# Line 957 | Line 995 | public class ScheduledExecutorTest exten
995      /**
996       * timed invokeAny(c) throws NPE if c has null elements
997       */
998 <    public void testTimedInvokeAny3() {
998 >    public void testTimedInvokeAny3() throws Exception {
999 >        CountDownLatch latch = new CountDownLatch(1);
1000          ExecutorService e = new ScheduledThreadPoolExecutor(2);
1001 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1002 +        l.add(latchAwaitingStringTask(latch));
1003 +        l.add(null);
1004          try {
1005 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1006 <            l.add(new StringTask());
965 <            l.add(null);
966 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1005 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1006 >            shouldThrow();
1007          } catch (NullPointerException success) {
968        } catch (Exception ex) {
969            ex.printStackTrace();
970            unexpectedException();
1008          } finally {
1009 +            latch.countDown();
1010              joinPool(e);
1011          }
1012      }
# Line 976 | Line 1014 | public class ScheduledExecutorTest exten
1014      /**
1015       * timed invokeAny(c) throws ExecutionException if no task completes
1016       */
1017 <    public void testTimedInvokeAny4() {
1017 >    public void testTimedInvokeAny4() throws Exception {
1018          ExecutorService e = new ScheduledThreadPoolExecutor(2);
1019 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1020 +        l.add(new NPETask());
1021          try {
1022 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1023 <            l.add(new NPETask());
984 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1022 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1023 >            shouldThrow();
1024          } catch (ExecutionException success) {
1025 <        } catch (Exception ex) {
987 <            unexpectedException();
1025 >            assertTrue(success.getCause() instanceof NullPointerException);
1026          } finally {
1027              joinPool(e);
1028          }
# Line 993 | Line 1031 | public class ScheduledExecutorTest exten
1031      /**
1032       * timed invokeAny(c) returns result of some task
1033       */
1034 <    public void testTimedInvokeAny5() {
1034 >    public void testTimedInvokeAny5() throws Exception {
1035          ExecutorService e = new ScheduledThreadPoolExecutor(2);
1036          try {
1037 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1037 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1038              l.add(new StringTask());
1039              l.add(new StringTask());
1040 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1040 >            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1041              assertSame(TEST_STRING, result);
1004        } catch (ExecutionException success) {
1005        } catch (Exception ex) {
1006            unexpectedException();
1042          } finally {
1043              joinPool(e);
1044          }
# Line 1012 | Line 1047 | public class ScheduledExecutorTest exten
1047      /**
1048       * timed invokeAll(null) throws NPE
1049       */
1050 <    public void testTimedInvokeAll1() {
1050 >    public void testTimedInvokeAll1() throws Exception {
1051          ExecutorService e = new ScheduledThreadPoolExecutor(2);
1052          try {
1053 <            e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1053 >            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1054 >            shouldThrow();
1055          } catch (NullPointerException success) {
1020        } catch (Exception ex) {
1021            unexpectedException();
1056          } finally {
1057              joinPool(e);
1058          }
# Line 1027 | Line 1061 | public class ScheduledExecutorTest exten
1061      /**
1062       * timed invokeAll(,,null) throws NPE
1063       */
1064 <    public void testTimedInvokeAllNullTimeUnit() {
1064 >    public void testTimedInvokeAllNullTimeUnit() throws Exception {
1065          ExecutorService e = new ScheduledThreadPoolExecutor(2);
1066 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1067 +        l.add(new StringTask());
1068          try {
1033            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1034            l.add(new StringTask());
1069              e.invokeAll(l, MEDIUM_DELAY_MS, null);
1070 +            shouldThrow();
1071          } catch (NullPointerException success) {
1037        } catch (Exception ex) {
1038            unexpectedException();
1072          } finally {
1073              joinPool(e);
1074          }
# Line 1044 | Line 1077 | public class ScheduledExecutorTest exten
1077      /**
1078       * timed invokeAll(empty collection) returns empty collection
1079       */
1080 <    public void testTimedInvokeAll2() {
1080 >    public void testTimedInvokeAll2() throws Exception {
1081          ExecutorService e = new ScheduledThreadPoolExecutor(2);
1082          try {
1083 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1083 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1084              assertTrue(r.isEmpty());
1052        } catch (Exception ex) {
1053            unexpectedException();
1085          } finally {
1086              joinPool(e);
1087          }
# Line 1059 | Line 1090 | public class ScheduledExecutorTest exten
1090      /**
1091       * timed invokeAll(c) throws NPE if c has null elements
1092       */
1093 <    public void testTimedInvokeAll3() {
1093 >    public void testTimedInvokeAll3() throws Exception {
1094          ExecutorService e = new ScheduledThreadPoolExecutor(2);
1095 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1096 +        l.add(new StringTask());
1097 +        l.add(null);
1098          try {
1099 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1100 <            l.add(new StringTask());
1067 <            l.add(null);
1068 <            e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1099 >            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1100 >            shouldThrow();
1101          } catch (NullPointerException success) {
1070        } catch (Exception ex) {
1071            unexpectedException();
1102          } finally {
1103              joinPool(e);
1104          }
# Line 1077 | Line 1107 | public class ScheduledExecutorTest exten
1107      /**
1108       * get of element of invokeAll(c) throws exception on failed task
1109       */
1110 <    public void testTimedInvokeAll4() {
1110 >    public void testTimedInvokeAll4() throws Exception {
1111          ExecutorService e = new ScheduledThreadPoolExecutor(2);
1112 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1113 +        l.add(new NPETask());
1114 +        List<Future<String>> futures =
1115 +            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1116 +        assertEquals(1, futures.size());
1117          try {
1118 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1119 <            l.add(new NPETask());
1085 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1086 <            assertEquals(1, result.size());
1087 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1088 <                it.next().get();
1118 >            futures.get(0).get();
1119 >            shouldThrow();
1120          } catch (ExecutionException success) {
1121 <        } catch (Exception ex) {
1091 <            unexpectedException();
1121 >            assertTrue(success.getCause() instanceof NullPointerException);
1122          } finally {
1123              joinPool(e);
1124          }
# Line 1097 | Line 1127 | public class ScheduledExecutorTest exten
1127      /**
1128       * timed invokeAll(c) returns results of all completed tasks
1129       */
1130 <    public void testTimedInvokeAll5() {
1130 >    public void testTimedInvokeAll5() throws Exception {
1131          ExecutorService e = new ScheduledThreadPoolExecutor(2);
1132          try {
1133 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1133 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1134              l.add(new StringTask());
1135              l.add(new StringTask());
1136 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1137 <            assertEquals(2, result.size());
1138 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1139 <                assertSame(TEST_STRING, it.next().get());
1140 <        } catch (ExecutionException success) {
1111 <        } catch (Exception ex) {
1112 <            unexpectedException();
1136 >            List<Future<String>> futures =
1137 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1138 >            assertEquals(2, futures.size());
1139 >            for (Future<String> future : futures)
1140 >                assertSame(TEST_STRING, future.get());
1141          } finally {
1142              joinPool(e);
1143          }
# Line 1118 | Line 1146 | public class ScheduledExecutorTest exten
1146      /**
1147       * timed invokeAll(c) cancels tasks not completed by timeout
1148       */
1149 <    public void testTimedInvokeAll6() {
1149 >    public void testTimedInvokeAll6() throws Exception {
1150          ExecutorService e = new ScheduledThreadPoolExecutor(2);
1151          try {
1152 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1152 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1153              l.add(new StringTask());
1154              l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1155              l.add(new StringTask());
1156 <            List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
1157 <            assertEquals(3, result.size());
1158 <            Iterator<Future<String>> it = result.iterator();
1156 >            List<Future<String>> futures =
1157 >                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1158 >            assertEquals(3, futures.size());
1159 >            Iterator<Future<String>> it = futures.iterator();
1160              Future<String> f1 = it.next();
1161              Future<String> f2 = it.next();
1162              Future<String> f3 = it.next();
# Line 1136 | Line 1165 | public class ScheduledExecutorTest exten
1165              assertTrue(f3.isDone());
1166              assertFalse(f1.isCancelled());
1167              assertTrue(f2.isCancelled());
1139        } catch (Exception ex) {
1140            unexpectedException();
1168          } finally {
1169              joinPool(e);
1170          }
1171      }
1172  
1146
1173   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines