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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines