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.4 by dl, Sun Sep 14 20:42:40 2003 UTC vs.
Revision 1.7 by dl, Fri Sep 26 15:33:13 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines