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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines