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.1 by dl, Sun Aug 31 19:24:55 2003 UTC vs.
Revision 1.6 by dl, Thu Sep 25 11:02:41 2003 UTC

# Line 9 | Line 9 | import junit.framework.*;
9   import java.util.*;
10   import java.util.concurrent.*;
11  
12 < public class ScheduledExecutorTest extends TestCase{
13 <    
14 <    boolean flag = false;
15 <
12 > public class ScheduledExecutorTest extends JSR166TestCase {
13      public static void main(String[] args) {
14          junit.textui.TestRunner.run (suite());  
15      }
19    
20
16      public static Test suite() {
17          return new TestSuite(ScheduledExecutorTest.class);
18      }
19  
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
20      static class MyRunnable implements Runnable {
30        volatile boolean waiting = true;
21          volatile boolean done = false;
22 <        public void run(){
23 <            try{
24 <                Thread.sleep(MEDIUM_DELAY_MS);
35 <                waiting = false;
22 >        public void run() {
23 >            try {
24 >                Thread.sleep(SMALL_DELAY_MS);
25                  done = true;
26 <            } catch(Exception e){}
26 >            } catch(Exception e){
27 >            }
28          }
29      }
30  
31      static class MyCallable implements Callable {
42        volatile boolean waiting = true;
32          volatile boolean done = false;
33 <        public Object call(){
34 <            try{
35 <                Thread.sleep(MEDIUM_DELAY_MS);
47 <                waiting = false;
33 >        public Object call() {
34 >            try {
35 >                Thread.sleep(SMALL_DELAY_MS);
36                  done = true;
37 <            }catch(Exception e){}
37 >            } catch(Exception e){
38 >            }
39              return Boolean.TRUE;
40          }
41      }
42  
43      /**
44 <     *  Test to verify execute successfully runs the given Runnable
44 >     * execute successfully executes a runnable
45       */
46 <    public void testExecute(){
47 <        try{
46 >    public void testExecute() {
47 >        try {
48              MyRunnable runnable =new MyRunnable();
49 <            ScheduledExecutor one = new ScheduledExecutor(1);
50 <            one.execute(runnable);
51 <            Thread.sleep(100);
52 <            assertTrue(runnable.waiting);
53 <            one.shutdown();
54 <            // make sure the Runnable has time to complete
55 <            try{Thread.sleep(1010);}catch(InterruptedException e){}
56 <            assertFalse(runnable.waiting);
49 >            ScheduledExecutor p1 = new ScheduledExecutor(1);
50 >            p1.execute(runnable);
51 >            assertFalse(runnable.done);
52 >            Thread.sleep(SHORT_DELAY_MS);
53 >            p1.shutdown();
54 >            try {
55 >                Thread.sleep(MEDIUM_DELAY_MS);
56 >            } catch(InterruptedException e){
57 >                unexpectedException();
58 >            }
59              assertTrue(runnable.done);
60 <            one.shutdown();
60 >            p1.shutdown();
61 >            joinPool(p1);
62          }
63          catch(Exception e){
64 <            fail("unexpected exception");
64 >            unexpectedException();
65          }
66 +        
67      }
68  
69 +
70      /**
71 <     *  Test to verify schedule successfully runs the given Callable.
78 <     *  The waiting flag shows that the Callable is not started until
79 <     *  immediately.
71 >     * delayed schedule of callable successfully executes after delay
72       */
73 <    public void testSchedule1(){
74 <        try{
73 >    public void testSchedule1() {
74 >        try {
75              MyCallable callable = new MyCallable();
76 <            ScheduledExecutor one = new ScheduledExecutor(1);
77 <            Future f = one.schedule(callable, 500, TimeUnit.MILLISECONDS);
78 <            //      Thread.sleep(505);
79 <            assertTrue(callable.waiting);
88 <            Thread.sleep(2000);
76 >            ScheduledExecutor p1 = new ScheduledExecutor(1);
77 >            Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
78 >            assertFalse(callable.done);
79 >            Thread.sleep(MEDIUM_DELAY_MS);
80              assertTrue(callable.done);
81              assertEquals(Boolean.TRUE, f.get());
82 <            one.shutdown();
83 <        }catch(RejectedExecutionException e){}
84 <        catch(Exception e){}
82 >            p1.shutdown();
83 >            joinPool(p1);
84 >        } catch(RejectedExecutionException e){}
85 >        catch(Exception e){
86 >            unexpectedException();
87 >        }
88      }
89  
90      /**
91 <     *  Another version of schedule, only using Runnable instead of Callable
91 >     *  delayed schedule of runnable successfully executes after delay
92       */
93 <    public void testSchedule3(){
94 <        try{
93 >    public void testSchedule3() {
94 >        try {
95              MyRunnable runnable = new MyRunnable();
96 <            ScheduledExecutor one = new ScheduledExecutor(1);
97 <            one.schedule(runnable, 500, TimeUnit.MILLISECONDS);
98 <            Thread.sleep(50);
99 <            assertTrue(runnable.waiting);
100 <            Thread.sleep(2000);
96 >            ScheduledExecutor p1 = new ScheduledExecutor(1);
97 >            p1.schedule(runnable, SMALL_DELAY_MS, TimeUnit.MILLISECONDS);
98 >            Thread.sleep(SHORT_DELAY_MS);
99 >            assertFalse(runnable.done);
100 >            Thread.sleep(MEDIUM_DELAY_MS);
101              assertTrue(runnable.done);
102 <            one.shutdown();
102 >            p1.shutdown();
103 >            joinPool(p1);
104          } catch(Exception e){
105 <            fail("unexpected exception");
105 >            unexpectedException();
106          }
107      }
108      
109      /**
110 <     *  The final version of schedule, using both long, TimeUnit and Runnable
110 >     * scheduleAtFixedRate executes runnable after given initial delay
111       */
112 <    public void testSchedule4(){
113 <        try{
112 >    public void testSchedule4() {
113 >        try {
114              MyRunnable runnable = new MyRunnable();
115 <            ScheduledExecutor one = new ScheduledExecutor(1);
116 <            one.schedule(runnable, 500, TimeUnit.MILLISECONDS);
117 <            //      Thread.sleep(505);
118 <            assertTrue(runnable.waiting);
124 <            Thread.sleep(2000);
115 >            ScheduledExecutor p1 = new ScheduledExecutor(1);
116 >            ScheduledCancellable h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
117 >            assertFalse(runnable.done);
118 >            Thread.sleep(MEDIUM_DELAY_MS);
119              assertTrue(runnable.done);
120 <            one.shutdown();
120 >            h.cancel(true);
121 >            p1.shutdown();
122 >            joinPool(p1);
123          } catch(Exception e){
124 <            fail("unexpected exception");
124 >            unexpectedException();
125 >        }
126 >    }
127 >
128 >    /**
129 >     * scheduleWithFixedDelay executes runnable after given initial delay
130 >     */
131 >    public void testSchedule5() {
132 >        try {
133 >            MyRunnable runnable = new MyRunnable();
134 >            ScheduledExecutor p1 = new ScheduledExecutor(1);
135 >            ScheduledCancellable h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
136 >            assertFalse(runnable.done);
137 >            Thread.sleep(MEDIUM_DELAY_MS);
138 >            assertTrue(runnable.done);
139 >            h.cancel(true);
140 >            p1.shutdown();
141 >            joinPool(p1);
142 >        } catch(Exception e){
143 >            unexpectedException();
144          }
145      }
146      
147 <  
148 <    // exception tests
147 >    /**
148 >     *  execute (null) throws NPE
149 >     */
150 >    public void testExecuteNull() {
151 >        ScheduledExecutor se = null;
152 >        try {
153 >            se = new ScheduledExecutor(1);
154 >            se.execute(null);
155 >            shouldThrow();
156 >        } catch(NullPointerException success){}
157 >        catch(Exception e){
158 >            unexpectedException();
159 >        }
160 >        
161 >        joinPool(se);
162 >    }
163  
164      /**
165 <     *  Test to verify schedule(Runnable, long) throws RejectedExecutionException
137 <     *  This occurs on an attempt to schedule a task on a shutdown executor
165 >     * schedule (null) throws NPE
166       */
167 <    public void testSchedule1_RejectedExecutionException(){
168 <        try{
169 <            ScheduledExecutor se = new ScheduledExecutor(1);
167 >    public void testScheduleNull() {
168 >        ScheduledExecutor se = new ScheduledExecutor(1);
169 >        try {
170 >            MyCallable callable = null;
171 >            Future f = se.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
172 >            shouldThrow();
173 >        } catch(NullPointerException success){}
174 >        catch(Exception e){
175 >            unexpectedException();
176 >        }
177 >        joinPool(se);
178 >    }
179 >  
180 >    /**
181 >     * execute throws RejectedExecutionException if shutdown
182 >     */
183 >    public void testSchedule1_RejectedExecutionException() {
184 >        ScheduledExecutor se = new ScheduledExecutor(1);
185 >        try {
186              se.shutdown();
187 <            se.schedule(new Runnable(){
188 <                    public void run(){}
189 <                }, 10000, TimeUnit.MILLISECONDS);
190 <            fail("shoud throw");
191 <        }catch(RejectedExecutionException e){}    
187 >            se.schedule(new NoOpRunnable(),
188 >                        MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
189 >            shouldThrow();
190 >        } catch(RejectedExecutionException success){
191 >        }
192 >        joinPool(se);
193 >
194      }
195  
196      /**
197 <     *  Test to verify schedule(Callable, long, TimeUnit) throws RejectedExecutionException
152 <     *  This occurs on an attempt to schedule a task on a shutdown executor
197 >     * schedule throws RejectedExecutionException if shutdown
198       */
199 <    public void testSchedule2_RejectedExecutionException(){
200 <        try{
201 <            ScheduledExecutor se = new ScheduledExecutor(1);
199 >    public void testSchedule2_RejectedExecutionException() {
200 >        ScheduledExecutor se = new ScheduledExecutor(1);
201 >        try {
202              se.shutdown();
203 <            se.schedule(new Callable(){
204 <                    public Object call(){
205 <                        return Boolean.TRUE;
206 <                    }
207 <                }, (long)100, TimeUnit.SECONDS);
208 <            fail("should throw");
164 <        }catch(RejectedExecutionException e){}    
203 >            se.schedule(new NoOpCallable(),
204 >                        MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
205 >            shouldThrow();
206 >        } catch(RejectedExecutionException success){
207 >        }
208 >        joinPool(se);
209      }
210  
211      /**
212 <     *  Test to verify schedule(Callable, long) throws RejectedExecutionException
213 <     *  This occurs on an attempt to schedule a task on a shutdown executor
214 <     */
215 <     public void testSchedule3_RejectedExecutionException(){
216 <        try{
173 <            ScheduledExecutor se = new ScheduledExecutor(1);
212 >     * schedule callable throws RejectedExecutionException if shutdown
213 >     */
214 >     public void testSchedule3_RejectedExecutionException() {
215 >         ScheduledExecutor se = new ScheduledExecutor(1);
216 >         try {
217              se.shutdown();
218 <            se.schedule(new Callable(){
219 <                    public Object call(){
220 <                        return Boolean.TRUE;
221 <                    }
222 <                },  10000, TimeUnit.MILLISECONDS);
223 <            fail("should throw");
181 <        }catch(RejectedExecutionException e){}    
218 >            se.schedule(new NoOpCallable(),
219 >                        MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
220 >            shouldThrow();
221 >        } catch(RejectedExecutionException success){
222 >        }
223 >         joinPool(se);
224      }
225  
226      /**
227 <     *  Test to verify scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws
228 <     *  RejectedExecutionException.
229 <     *  This occurs on an attempt to schedule a task on a shutdown executor
230 <     */
231 <    public void testScheduleAtFixedRate1_RejectedExecutionException(){
190 <        try{
191 <            ScheduledExecutor se = new ScheduledExecutor(1);
227 >     *  scheduleAtFixedRate throws RejectedExecutionException if shutdown
228 >     */
229 >    public void testScheduleAtFixedRate1_RejectedExecutionException() {
230 >        ScheduledExecutor se = new ScheduledExecutor(1);
231 >        try {
232              se.shutdown();
233 <            se.scheduleAtFixedRate(new Runnable(){
234 <                    public void run(){}
235 <                }, 100, 100, TimeUnit.SECONDS);
236 <            fail("should throw");
237 <        }catch(RejectedExecutionException e){}    
233 >            se.scheduleAtFixedRate(new NoOpRunnable(),
234 >                                   MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
235 >            shouldThrow();
236 >        } catch(RejectedExecutionException success){
237 >        }
238 >        joinPool(se);
239      }
240      
241      /**
242 <     *  Test to verify scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws
202 <     *  RejectedExecutionException.
203 <     *  This occurs on an attempt to schedule a task on a shutdown executor
242 >     * scheduleWithFixedDelay throws RejectedExecutionException if shutdown
243       */
244 <    public void testScheduleAtFixedRate2_RejectedExecutionException(){
245 <        try{
246 <            ScheduledExecutor se = new ScheduledExecutor(1);
244 >    public void testScheduleWithFixedDelay1_RejectedExecutionException() {
245 >        ScheduledExecutor se = new ScheduledExecutor(1);
246 >        try {
247              se.shutdown();
248 <            se.scheduleAtFixedRate(new Runnable(){
249 <                    public void run(){}
250 <                },  1, 100, TimeUnit.SECONDS);
251 <            fail("should throw");
252 <        }catch(RejectedExecutionException e){}    
248 >            se.scheduleWithFixedDelay(new NoOpRunnable(),
249 >                                      MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
250 >            shouldThrow();
251 >        } catch(RejectedExecutionException success){
252 >        }
253 >        joinPool(se);
254      }
255  
256      /**
257 <     *  Test to verify scheduleWithFixedDelay(Runnable, long, long, TimeUnit) throws
258 <     *  RejectedExecutionException.
259 <     *  This occurs on an attempt to schedule a task on a shutdown executor
257 >     *  getActiveCount increases but doesn't overestimate, when a
258 >     *  thread becomes active
259 >     */
260 >    public void testGetActiveCount() {
261 >        ScheduledExecutor p2 = new ScheduledExecutor(2);
262 >        assertEquals(0, p2.getActiveCount());
263 >        p2.execute(new SmallRunnable());
264 >        try {
265 >            Thread.sleep(SHORT_DELAY_MS);
266 >        } catch(Exception e){
267 >            unexpectedException();
268 >        }
269 >        assertEquals(1, p2.getActiveCount());
270 >        joinPool(p2);
271 >    }
272 >    
273 >    /**
274 >     *    getCompletedTaskCount increases, but doesn't overestimate,
275 >     *   when tasks complete
276       */
277 <    public void testScheduleWithFixedDelay1_RejectedExecutionException(){
278 <        try{
279 <            ScheduledExecutor se = new ScheduledExecutor(1);
280 <            se.shutdown();
281 <            se.scheduleWithFixedDelay(new Runnable(){
282 <                    public void run(){}
283 <                }, 100, 100, TimeUnit.SECONDS);
284 <            fail("should throw");
285 <        }catch(RejectedExecutionException e){}    
277 >    public void testGetCompletedTaskCount() {
278 >        ScheduledExecutor p2 = new ScheduledExecutor(2);
279 >        assertEquals(0, p2.getCompletedTaskCount());
280 >        p2.execute(new SmallRunnable());
281 >        try {
282 >            Thread.sleep(MEDIUM_DELAY_MS);
283 >        } catch(Exception e){
284 >            unexpectedException();
285 >        }
286 >        assertEquals(1, p2.getCompletedTaskCount());
287 >        joinPool(p2);
288 >    }
289 >    
290 >    /**
291 >     *  getCorePoolSize returns size given in constructor if not otherwise set
292 >     */
293 >    public void testGetCorePoolSize() {
294 >        ScheduledExecutor p1 = new ScheduledExecutor(1);
295 >        assertEquals(1, p1.getCorePoolSize());
296 >        joinPool(p1);
297 >    }
298 >    
299 >    /**
300 >     *    getLargestPoolSize increases, but doesn't overestimate, when
301 >     *   multiple threads active
302 >     */
303 >    public void testGetLargestPoolSize() {
304 >        ScheduledExecutor p2 = new ScheduledExecutor(2);
305 >        assertEquals(0, p2.getLargestPoolSize());
306 >        p2.execute(new SmallRunnable());
307 >        p2.execute(new SmallRunnable());
308 >        try {
309 >            Thread.sleep(SHORT_DELAY_MS);
310 >        } catch(Exception e){
311 >            unexpectedException();
312 >        }
313 >        assertEquals(2, p2.getLargestPoolSize());
314 >        joinPool(p2);
315 >    }
316 >    
317 >    /**
318 >     *   getPoolSize increases, but doesn't overestimate, when threads
319 >     *   become active
320 >     */
321 >    public void testGetPoolSize() {
322 >        ScheduledExecutor p1 = new ScheduledExecutor(1);
323 >        assertEquals(0, p1.getPoolSize());
324 >        p1.execute(new SmallRunnable());
325 >        assertEquals(1, p1.getPoolSize());
326 >        joinPool(p1);
327 >    }
328 >    
329 >    /**
330 >     *    getTaskCount increases, but doesn't overestimate, when tasks
331 >     *    submitted
332 >     */
333 >    public void testGetTaskCount() {
334 >        ScheduledExecutor p1 = new ScheduledExecutor(1);
335 >        assertEquals(0, p1.getTaskCount());
336 >        for(int i = 0; i < 5; i++)
337 >            p1.execute(new SmallRunnable());
338 >        try {
339 >            Thread.sleep(SHORT_DELAY_MS);
340 >        } catch(Exception e){
341 >            unexpectedException();
342 >        }
343 >        assertEquals(5, p1.getTaskCount());
344 >        joinPool(p1);
345 >    }
346 >    
347 >    /**
348 >     *   is isShutDown is false before shutdown, true after
349 >     */
350 >    public void testIsShutdown() {
351 >        
352 >        ScheduledExecutor p1 = new ScheduledExecutor(1);
353 >        try {
354 >            assertFalse(p1.isShutdown());
355 >        }
356 >        finally {
357 >            p1.shutdown();
358 >        }
359 >        assertTrue(p1.isShutdown());
360      }
361  
362 +        
363      /**
364 <     *  Test to verify scheduleWithFixedDelay(Runnable, long, long, TimeUnit) throws
234 <     *  RejectedExecutionException.
235 <     *  This occurs on an attempt to schedule a task on a shutdown executor
364 >     *   isTerminated is false before termination, true after
365       */
366 <     public void testScheduleWithFixedDelay2_RejectedExecutionException(){
367 <        try{
368 <            ScheduledExecutor se = new ScheduledExecutor(1);
369 <            se.shutdown();
370 <            se.scheduleWithFixedDelay(new Runnable(){
371 <                    public void run(){}
372 <                },  1, 100, TimeUnit.SECONDS);
373 <            fail("should throw");
374 <        }catch(RejectedExecutionException e){}    
366 >    public void testIsTerminated() {
367 >        ScheduledExecutor p1 = new ScheduledExecutor(1);
368 >        try {
369 >            p1.execute(new SmallRunnable());
370 >        } finally {
371 >            p1.shutdown();
372 >        }
373 >        try {
374 >            assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
375 >            assertTrue(p1.isTerminated());
376 >        } catch(Exception e){
377 >            unexpectedException();
378 >        }      
379 >    }
380 >
381 >    /**
382 >     *  isTerminating is not true when running or when terminated
383 >     */
384 >    public void testIsTerminating() {
385 >        ScheduledExecutor p1 = new ScheduledExecutor(1);
386 >        assertFalse(p1.isTerminating());
387 >        try {
388 >            p1.execute(new SmallRunnable());
389 >            assertFalse(p1.isTerminating());
390 >        } finally {
391 >            p1.shutdown();
392 >        }
393 >        try {
394 >            assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
395 >            assertTrue(p1.isTerminated());
396 >            assertFalse(p1.isTerminating());
397 >        } catch(Exception e){
398 >            unexpectedException();
399 >        }      
400 >    }
401 >
402 >    /**
403 >     *   purge removes cancelled tasks from the queue
404 >     */
405 >    public void testPurge() {
406 >        ScheduledExecutor p1 = new ScheduledExecutor(1);
407 >        ScheduledCancellable[] tasks = new ScheduledCancellable[5];
408 >        for(int i = 0; i < 5; i++){
409 >            tasks[i] = p1.schedule(new SmallRunnable(), 1, TimeUnit.MILLISECONDS);
410 >        }
411 >        int max = 5;
412 >        if (tasks[4].cancel(true)) --max;
413 >        if (tasks[3].cancel(true)) --max;
414 >        p1.purge();
415 >        long count = p1.getTaskCount();
416 >        assertTrue(count > 0 && count <= max);
417 >        joinPool(p1);
418      }
419  
420      /**
421 <     *  Test to verify execute throws RejectedExecutionException
422 <     *  This occurs on an attempt to schedule a task on a shutdown executor
421 >     *  shutDownNow returns a list containing tasks that were not run
422 >     */
423 >    public void testShutDownNow() {
424 >        ScheduledExecutor p1 = new ScheduledExecutor(1);
425 >        for(int i = 0; i < 5; i++)
426 >            p1.schedule(new SmallRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
427 >        List l = p1.shutdownNow();
428 >        assertTrue(p1.isShutdown());
429 >        assertTrue(l.size() > 0 && l.size() <= 5);
430 >        joinPool(p1);
431 >    }
432 >
433 >    /**
434 >     * In default setting, shutdown cancels periodic but not delayed
435 >     * tasks at shutdown
436 >     */
437 >    public void testShutDown1() {
438 >        try {
439 >            ScheduledExecutor p1 = new ScheduledExecutor(1);
440 >            assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy());
441 >            assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy());
442 >
443 >            ScheduledCancellable[] tasks = new ScheduledCancellable[5];
444 >            for(int i = 0; i < 5; i++)
445 >                tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
446 >            p1.shutdown();
447 >            BlockingQueue q = p1.getQueue();
448 >            for (Iterator it = q.iterator(); it.hasNext();) {
449 >                ScheduledCancellable t = (ScheduledCancellable)it.next();
450 >                assertFalse(t.isCancelled());
451 >            }
452 >            assertTrue(p1.isShutdown());
453 >            Thread.sleep(SMALL_DELAY_MS);
454 >            for (int i = 0; i < 5; ++i) {
455 >                assertTrue(tasks[i].isDone());
456 >                assertFalse(tasks[i].isCancelled());
457 >            }
458 >            
459 >        }
460 >        catch(Exception ex) {
461 >            unexpectedException();
462 >        }
463 >    }
464 >
465 >
466 >    /**
467 >     * If setExecuteExistingDelayedTasksAfterShutdownPolicy is false,
468 >     * delayed tasks are cancelled at shutdown
469       */
470 <    public void testExecute_RejectedExecutionException(){
471 <        try{
472 <            ScheduledExecutor se = new ScheduledExecutor(1);
473 <            se.shutdown();
474 <            se.execute(new Runnable(){
475 <                    public void run(){}
476 <                });
477 <            fail("should throw");
478 <        }catch(RejectedExecutionException e){}    
470 >    public void testShutDown2() {
471 >        try {
472 >            ScheduledExecutor p1 = new ScheduledExecutor(1);
473 >            p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
474 >            ScheduledCancellable[] tasks = new ScheduledCancellable[5];
475 >            for(int i = 0; i < 5; i++)
476 >                tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
477 >            p1.shutdown();
478 >            assertTrue(p1.isShutdown());
479 >            BlockingQueue q = p1.getQueue();
480 >            assertTrue(q.isEmpty());
481 >            Thread.sleep(SMALL_DELAY_MS);
482 >            assertTrue(p1.isTerminated());
483 >        }
484 >        catch(Exception ex) {
485 >            unexpectedException();
486 >        }
487 >    }
488 >
489 >
490 >    /**
491 >     * If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false,
492 >     * periodic tasks are not cancelled at shutdown
493 >     */
494 >    public void testShutDown3() {
495 >        try {
496 >            ScheduledExecutor p1 = new ScheduledExecutor(1);
497 >            p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
498 >            ScheduledCancellable task =
499 >                p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS);
500 >            p1.shutdown();
501 >            assertTrue(p1.isShutdown());
502 >            BlockingQueue q = p1.getQueue();
503 >            assertTrue(q.isEmpty());
504 >            Thread.sleep(SHORT_DELAY_MS);
505 >            assertTrue(p1.isTerminated());
506 >        }
507 >        catch(Exception ex) {
508 >            unexpectedException();
509 >        }
510 >    }
511 >
512 >    /**
513 >     * if setContinueExistingPeriodicTasksAfterShutdownPolicy is true,
514 >     * periodic tasks are cancelled at shutdown
515 >     */
516 >    public void testShutDown4() {
517 >        ScheduledExecutor p1 = new ScheduledExecutor(1);
518 >        try {
519 >            p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
520 >            ScheduledCancellable task =
521 >                p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS);
522 >            assertFalse(task.isCancelled());
523 >            p1.shutdown();
524 >            assertFalse(task.isCancelled());
525 >            assertFalse(p1.isTerminated());
526 >            assertTrue(p1.isShutdown());
527 >            Thread.sleep(SHORT_DELAY_MS);
528 >            assertFalse(task.isCancelled());
529 >            task.cancel(true);
530 >            assertTrue(task.isCancelled());
531 >            Thread.sleep(SHORT_DELAY_MS);
532 >            assertTrue(p1.isTerminated());
533 >        }
534 >        catch(Exception ex) {
535 >            unexpectedException();
536 >        }
537 >        finally {
538 >            p1.shutdownNow();
539 >        }
540      }
541  
542   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines