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.12 by dl, Thu Dec 25 19:48:57 2003 UTC vs.
Revision 1.52 by jsr166, Mon Sep 14 03:27:11 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines