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.8 by dl, Sun Oct 5 23:00:40 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
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    }
20  
21      /**
22 <     *  Test to verify execute successfully runs the given Runnable
23 <     */
24 <    public void testExecute(){
25 <        try{
26 <            MyRunnable runnable =new MyRunnable();
27 <            ScheduledExecutor one = new ScheduledExecutor(1);
28 <            one.execute(runnable);
29 <            Thread.sleep(SHORT_DELAY_MS/2);
30 <            assertTrue(runnable.waiting);
31 <            one.shutdown();
32 <            try{
22 >     * execute successfully executes a runnable
23 >     */
24 >    public void testExecute() {
25 >        try {
26 >            TrackedShortRunnable runnable =new TrackedShortRunnable();
27 >            ScheduledExecutor p1 = new ScheduledExecutor(1);
28 >            p1.execute(runnable);
29 >            assertFalse(runnable.done);
30 >            Thread.sleep(SHORT_DELAY_MS);
31 >            p1.shutdown();
32 >            try {
33                  Thread.sleep(MEDIUM_DELAY_MS);
34              } catch(InterruptedException e){
35 <                fail("unexpected exception");
35 >                unexpectedException();
36              }
88            assertFalse(runnable.waiting);
37              assertTrue(runnable.done);
38 <            one.shutdown();
38 >            p1.shutdown();
39 >            joinPool(p1);
40          }
41          catch(Exception e){
42 <            fail("unexpected exception");
42 >            unexpectedException();
43          }
44 +        
45      }
46  
47 +
48      /**
49 <     *  Test to verify schedule successfully runs the given Callable.
99 <     *  The waiting flag shows that the Callable is not started until
100 <     *  immediately.
49 >     * delayed schedule of callable successfully executes after delay
50       */
51 <    public void testSchedule1(){
52 <        try{
53 <            MyCallable callable = new MyCallable();
54 <            ScheduledExecutor one = new ScheduledExecutor(1);
55 <            Future f = one.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
56 <            assertTrue(callable.waiting);
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 <        }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, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
76 <            Thread.sleep(SHORT_DELAY_MS/2);
77 <            assertTrue(runnable.waiting);
71 >    public void testSchedule3() {
72 >        try {
73 >            TrackedShortRunnable runnable = new TrackedShortRunnable();
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();
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);
95 <            //      Thread.sleep(505);
145 <            assertTrue(runnable.waiting);
90 >    public void testSchedule4() {
91 >        try {
92 >            TrackedShortRunnable runnable = new TrackedShortRunnable();
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();
98 >            h.cancel(true);
99 >            p1.shutdown();
100 >            joinPool(p1);
101          } catch(Exception e){
102 <            fail("unexpected exception");
102 >            unexpectedException();
103          }
104      }
153    
154  
155    // exception tests
105  
106      /**
107 <     *  Test to verify schedule(Runnable, long) throws RejectedExecutionException
159 <     *  This occurs on an attempt to schedule a task on a shutdown executor
107 >     * scheduleWithFixedDelay executes runnable after given initial delay
108       */
109 <    public void testSchedule1_RejectedExecutionException(){
110 <        try{
111 <            ScheduledExecutor se = new ScheduledExecutor(1);
112 <            se.shutdown();
113 <            se.schedule(new Runnable(){
114 <                    public void run(){}
115 <                }, 10000, TimeUnit.MILLISECONDS);
116 <            fail("shoud throw");
117 <        }catch(RejectedExecutionException e){}    
109 >    public void testSchedule5() {
110 >        try {
111 >            TrackedShortRunnable runnable = new TrackedShortRunnable();
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 <
124 >    
125      /**
126 <     *  Test to verify schedule(Callable, long, TimeUnit) throws RejectedExecutionException
174 <     *  This occurs on an attempt to schedule a task on a shutdown executor
126 >     *  execute (null) throws NPE
127       */
128 <    public void testSchedule2_RejectedExecutionException(){
129 <        try{
130 <            ScheduledExecutor se = new ScheduledExecutor(1);
131 <            se.shutdown();
132 <            se.schedule(new Callable(){
133 <                    public Object call(){
134 <                        return Boolean.TRUE;
135 <                    }
136 <                }, (long)100, TimeUnit.SECONDS);
137 <            fail("should throw");
138 <        }catch(RejectedExecutionException e){}    
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 <     *  Test to verify schedule(Callable, long) throws RejectedExecutionException
144 <     *  This occurs on an attempt to schedule a task on a shutdown executor
145 <     */
146 <     public void testSchedule3_RejectedExecutionException(){
147 <        try{
148 <            ScheduledExecutor se = new ScheduledExecutor(1);
149 <            se.shutdown();
150 <            se.schedule(new Callable(){
151 <                    public Object call(){
152 <                        return Boolean.TRUE;
153 <                    }
154 <                },  10000, TimeUnit.MILLISECONDS);
155 <            fail("should throw");
203 <        }catch(RejectedExecutionException e){}    
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 <
157 >  
158      /**
159 <     *  Test to verify scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws
160 <     *  RejectedExecutionException.
161 <     *  This occurs on an attempt to schedule a task on a shutdown executor
162 <     */
163 <    public void testScheduleAtFixedRate1_RejectedExecutionException(){
212 <        try{
213 <            ScheduledExecutor se = new ScheduledExecutor(1);
159 >     * execute throws RejectedExecutionException if shutdown
160 >     */
161 >    public void testSchedule1_RejectedExecutionException() {
162 >        ScheduledExecutor se = new ScheduledExecutor(1);
163 >        try {
164              se.shutdown();
165 <            se.scheduleAtFixedRate(new Runnable(){
166 <                    public void run(){}
167 <                }, 100, 100, TimeUnit.SECONDS);
168 <            fail("should throw");
169 <        }catch(RejectedExecutionException e){}    
165 >            se.schedule(new NoOpRunnable(),
166 >                        MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
167 >            shouldThrow();
168 >        } catch(RejectedExecutionException success){
169 >        }
170 >        joinPool(se);
171 >
172      }
173 <    
173 >
174      /**
175 <     *  Test to verify scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws
176 <     *  RejectedExecutionException.
177 <     *  This occurs on an attempt to schedule a task on a shutdown executor
178 <     */
179 <    public void testScheduleAtFixedRate2_RejectedExecutionException(){
228 <        try{
229 <            ScheduledExecutor se = new ScheduledExecutor(1);
175 >     * schedule throws RejectedExecutionException if shutdown
176 >     */
177 >    public void testSchedule2_RejectedExecutionException() {
178 >        ScheduledExecutor se = new ScheduledExecutor(1);
179 >        try {
180              se.shutdown();
181 <            se.scheduleAtFixedRate(new Runnable(){
182 <                    public void run(){}
183 <                },  1, 100, TimeUnit.SECONDS);
184 <            fail("should throw");
185 <        }catch(RejectedExecutionException e){}    
181 >            se.schedule(new NoOpCallable(),
182 >                        MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
183 >            shouldThrow();
184 >        } catch(RejectedExecutionException success){
185 >        }
186 >        joinPool(se);
187      }
188  
189      /**
190 <     *  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
190 >     * schedule callable throws RejectedExecutionException if shutdown
191       */
192 <    public void testScheduleWithFixedDelay1_RejectedExecutionException(){
193 <        try{
194 <            ScheduledExecutor se = new ScheduledExecutor(1);
192 >     public void testSchedule3_RejectedExecutionException() {
193 >         ScheduledExecutor se = new ScheduledExecutor(1);
194 >         try {
195              se.shutdown();
196 <            se.scheduleWithFixedDelay(new Runnable(){
197 <                    public void run(){}
198 <                }, 100, 100, TimeUnit.SECONDS);
199 <            fail("should throw");
200 <        }catch(RejectedExecutionException e){}    
196 >            se.schedule(new NoOpCallable(),
197 >                        MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
198 >            shouldThrow();
199 >        } catch(RejectedExecutionException success){
200 >        }
201 >         joinPool(se);
202      }
203  
204      /**
205 <     *  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
205 >     *  scheduleAtFixedRate throws RejectedExecutionException if shutdown
206       */
207 <     public void testScheduleWithFixedDelay2_RejectedExecutionException(){
208 <        try{
209 <            ScheduledExecutor se = new ScheduledExecutor(1);
207 >    public void testScheduleAtFixedRate1_RejectedExecutionException() {
208 >        ScheduledExecutor se = new ScheduledExecutor(1);
209 >        try {
210              se.shutdown();
211 <            se.scheduleWithFixedDelay(new Runnable(){
212 <                    public void run(){}
213 <                },  1, 100, TimeUnit.SECONDS);
214 <            fail("should throw");
215 <        }catch(RejectedExecutionException e){}    
211 >            se.scheduleAtFixedRate(new NoOpRunnable(),
212 >                                   MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
213 >            shouldThrow();
214 >        } catch(RejectedExecutionException success){
215 >        }
216 >        joinPool(se);
217      }
218 <
218 >    
219      /**
220 <     *  Test to verify execute throws RejectedExecutionException
272 <     *  This occurs on an attempt to schedule a task on a shutdown executor
220 >     * scheduleWithFixedDelay throws RejectedExecutionException if shutdown
221       */
222 <    public void testExecute_RejectedExecutionException(){
223 <        try{
224 <            ScheduledExecutor se = new ScheduledExecutor(1);
222 >    public void testScheduleWithFixedDelay1_RejectedExecutionException() {
223 >        ScheduledExecutor se = new ScheduledExecutor(1);
224 >        try {
225              se.shutdown();
226 <            se.execute(new Runnable(){
227 <                    public void run(){}
228 <                });
229 <            fail("should throw");
230 <        }catch(RejectedExecutionException e){}    
226 >            se.scheduleWithFixedDelay(new NoOpRunnable(),
227 >                                      MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
228 >            shouldThrow();
229 >        } catch(RejectedExecutionException success){
230 >        }
231 >        joinPool(se);
232      }
233  
285
286
234      /**
235 <     *  Test to verify getActiveCount gives correct values
236 <     */
237 <    public void testGetActiveCount(){
238 <        ScheduledExecutor two = new ScheduledExecutor(2);
235 >     *  getActiveCount increases but doesn't overestimate, when a
236 >     *  thread becomes active
237 >     */
238 >    public void testGetActiveCount() {
239 >        ScheduledExecutor p2 = new ScheduledExecutor(2);
240 >        assertEquals(0, p2.getActiveCount());
241 >        p2.execute(new SmallRunnable());
242          try {
243 <            assertEquals(0, two.getActiveCount());
244 <            two.execute(newRunnable());
245 <            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();
243 >            Thread.sleep(SHORT_DELAY_MS);
244 >        } catch(Exception e){
245 >            unexpectedException();
246          }
247 +        assertEquals(1, p2.getActiveCount());
248 +        joinPool(p2);
249      }
250      
251      /**
252 <     *  Test to verify 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);
255 >    public void testGetCompletedTaskCount() {
256 >        ScheduledExecutor p2 = new ScheduledExecutor(2);
257 >        assertEquals(0, p2.getCompletedTaskCount());
258 >        p2.execute(new SmallRunnable());
259          try {
260 <            assertEquals(0, two.getCompletedTaskCount());
261 <            two.execute(newRunnable());
262 <            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();
260 >            Thread.sleep(MEDIUM_DELAY_MS);
261 >        } catch(Exception e){
262 >            unexpectedException();
263          }
264 +        assertEquals(1, p2.getCompletedTaskCount());
265 +        joinPool(p2);
266      }
267      
268      /**
269 <     *  Test to verify 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 <        try {
274 <            assertEquals(1, one.getCorePoolSize());
332 <        } finally {
333 <            one.shutdown();
334 <        }
271 >    public void testGetCorePoolSize() {
272 >        ScheduledExecutor p1 = new ScheduledExecutor(1);
273 >        assertEquals(1, p1.getCorePoolSize());
274 >        joinPool(p1);
275      }
276      
277      /**
278 <     *  Test to verify 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);
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 <            assertEquals(0, two.getLargestPoolSize());
288 <            two.execute(newRunnable());
289 <            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();
287 >            Thread.sleep(SHORT_DELAY_MS);
288 >        } catch(Exception e){
289 >            unexpectedException();
290          }
291 +        assertEquals(2, p2.getLargestPoolSize());
292 +        joinPool(p2);
293      }
294      
295      /**
296 <     *  Test to verify 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 <        try {
302 <            assertEquals(0, one.getPoolSize());
303 <            one.execute(newRunnable());
304 <            assertEquals(1, one.getPoolSize());
366 <        } finally {
367 <            one.shutdown();
368 <        }
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 <     *  Test to verify 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);
311 >    public void testGetTaskCount() {
312 >        ScheduledExecutor p1 = new ScheduledExecutor(1);
313 >        assertEquals(0, p1.getTaskCount());
314 >        for(int i = 0; i < 5; i++)
315 >            p1.execute(new SmallRunnable());
316          try {
317 <            assertEquals(0, one.getTaskCount());
318 <            for(int i = 0; i < 5; i++)
319 <                one.execute(newRunnable());
320 <            try{
321 <                Thread.sleep(SHORT_DELAY_MS);
322 <            } catch(Exception e){
323 <                fail("unexpected exception");
324 <            }
325 <            assertEquals(5, one.getTaskCount());
317 >            Thread.sleep(SHORT_DELAY_MS);
318 >        } catch(Exception e){
319 >            unexpectedException();
320 >        }
321 >        assertEquals(5, p1.getTaskCount());
322 >        joinPool(p1);
323 >    }
324 >
325 >    /**
326 >     * getThreadFactory returns factory in constructor if not set
327 >     */
328 >    public void testGetThreadFactory() {
329 >        ThreadFactory tf = new SimpleThreadFactory();
330 >        ScheduledExecutor p = new ScheduledExecutor(1, tf);
331 >        assertSame(tf, p.getThreadFactory());
332 >        p.shutdown();
333 >        joinPool(p);
334 >    }
335 >
336 >    /**
337 >     * setThreadFactory sets the thread factory returned by getThreadFactory
338 >     */
339 >    public void testSetThreadFactory() {
340 >        ThreadFactory tf = new SimpleThreadFactory();
341 >        ScheduledExecutor p = new ScheduledExecutor(1);
342 >        p.setThreadFactory(tf);
343 >        assertSame(tf, p.getThreadFactory());
344 >        p.shutdown();
345 >        joinPool(p);
346 >    }
347 >
348 >    /**
349 >     * setThreadFactory(null) throws NPE
350 >     */
351 >    public void testSetThreadFactoryNull() {
352 >        ScheduledExecutor p = new ScheduledExecutor(1);
353 >        try {
354 >            p.setThreadFactory(null);
355 >            shouldThrow();
356 >        } catch (NullPointerException success) {
357          } finally {
358 <            one.shutdown();
358 >            joinPool(p);
359          }
360      }
361      
362      /**
363 <     *  Test to verify isShutDown gives correct values
363 >     *   is isShutDown is false before shutdown, true after
364       */
365 <    public void testIsShutdown(){
365 >    public void testIsShutdown() {
366          
367 <        ScheduledExecutor one = new ScheduledExecutor(1);
367 >        ScheduledExecutor p1 = new ScheduledExecutor(1);
368          try {
369 <            assertFalse(one.isShutdown());
369 >            assertFalse(p1.isShutdown());
370          }
371          finally {
372 <            one.shutdown();
372 >            p1.shutdown();
373          }
374 <        assertTrue(one.isShutdown());
374 >        assertTrue(p1.isShutdown());
375      }
376  
377          
378      /**
379 <     *  Test to verify isTerminated gives correct values
409 <     *  Makes sure termination does not take an innapropriate
410 <     *  amount of time
379 >     *   isTerminated is false before termination, true after
380       */
381 <    public void testIsTerminated(){
382 <        ScheduledExecutor one = new ScheduledExecutor(1);
381 >    public void testIsTerminated() {
382 >        ScheduledExecutor p1 = new ScheduledExecutor(1);
383          try {
384 <            one.execute(newRunnable());
384 >            p1.execute(new SmallRunnable());
385          } finally {
386 <            one.shutdown();
386 >            p1.shutdown();
387          }
388 <        boolean flag = false;
389 <        try{
390 <            flag = one.awaitTermination(10, TimeUnit.SECONDS);
388 >        try {
389 >            assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
390 >            assertTrue(p1.isTerminated());
391          } catch(Exception e){
392 <            fail("unexpected exception");
392 >            unexpectedException();
393          }      
425        assertTrue(one.isTerminated());
426        if(!flag)
427            fail("ThreadPoolExecutor - thread pool did not terminate within suitable timeframe");
394      }
395  
396      /**
397 <     *  Test to verify that purge correctly removes cancelled tasks
432 <     *  from the queue
397 >     *  isTerminating is not true when running or when terminated
398       */
399 <    public void testPurge(){
400 <        ScheduledExecutor one = new ScheduledExecutor(1);
399 >    public void testIsTerminating() {
400 >        ScheduledExecutor p1 = new ScheduledExecutor(1);
401 >        assertFalse(p1.isTerminating());
402          try {
403 <            ScheduledCancellable[] tasks = new ScheduledCancellable[5];
404 <            for(int i = 0; i < 5; i++){
439 <                tasks[i] = one.schedule(newRunnable(), 1, TimeUnit.MILLISECONDS);
440 <            }
441 <            int max = 5;
442 <            if (tasks[4].cancel(true)) --max;
443 <            if (tasks[3].cancel(true)) --max;
444 <            one.purge();
445 <            long count = one.getTaskCount();
446 <            assertTrue(count > 0 && count <= max);
403 >            p1.execute(new SmallRunnable());
404 >            assertFalse(p1.isTerminating());
405          } finally {
406 <            one.shutdown();
406 >            p1.shutdown();
407          }
408 +        try {
409 +            assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
410 +            assertTrue(p1.isTerminated());
411 +            assertFalse(p1.isTerminating());
412 +        } catch(Exception e){
413 +            unexpectedException();
414 +        }      
415      }
416  
417      /**
418 <     *  Test to verify shutDownNow returns a list
454 <     *  containing the correct number of elements
418 >     * getQueue returns the work queue, which contains queued tasks
419       */
420 <    public void testShutDownNow(){
421 <        ScheduledExecutor one = new ScheduledExecutor(1);
420 >    public void testGetQueue() {
421 >        ScheduledExecutor p1 = new ScheduledExecutor(1);
422 >        ScheduledCancellable[] tasks = new ScheduledCancellable[5];
423 >        for(int i = 0; i < 5; i++){
424 >            tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS);
425 >        }
426 >        try {
427 >            Thread.sleep(SHORT_DELAY_MS);
428 >            BlockingQueue<Runnable> q = p1.getQueue();
429 >            assertTrue(q.contains(tasks[4]));
430 >            assertFalse(q.contains(tasks[0]));
431 >            p1.shutdownNow();
432 >        } catch(Exception e) {
433 >            unexpectedException();
434 >        } finally {
435 >            joinPool(p1);
436 >        }
437 >    }
438 >
439 >    /**
440 >     * remove(task) removes queued task, and fails to remove active task
441 >     */
442 >    public void testRemove() {
443 >        ScheduledExecutor p1 = new ScheduledExecutor(1);
444 >        ScheduledCancellable[] tasks = new ScheduledCancellable[5];
445 >        for(int i = 0; i < 5; i++){
446 >            tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS);
447 >        }
448 >        try {
449 >            Thread.sleep(SHORT_DELAY_MS);
450 >            BlockingQueue<Runnable> q = p1.getQueue();
451 >            assertFalse(p1.remove((Runnable)tasks[0]));
452 >            assertTrue(q.contains((Runnable)tasks[4]));
453 >            assertTrue(q.contains((Runnable)tasks[3]));
454 >            assertTrue(p1.remove((Runnable)tasks[4]));
455 >            assertFalse(p1.remove((Runnable)tasks[4]));
456 >            assertFalse(q.contains((Runnable)tasks[4]));
457 >            assertTrue(q.contains((Runnable)tasks[3]));
458 >            assertTrue(p1.remove((Runnable)tasks[3]));
459 >            assertFalse(q.contains((Runnable)tasks[3]));
460 >            p1.shutdownNow();
461 >        } catch(Exception e) {
462 >            unexpectedException();
463 >        } finally {
464 >            joinPool(p1);
465 >        }
466 >    }
467 >
468 >    /**
469 >     *  purge removes cancelled tasks from the queue
470 >     */
471 >    public void testPurge() {
472 >        ScheduledExecutor p1 = new ScheduledExecutor(1);
473 >        ScheduledCancellable[] tasks = new ScheduledCancellable[5];
474 >        for(int i = 0; i < 5; i++){
475 >            tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS);
476 >        }
477 >        int max = 5;
478 >        if (tasks[4].cancel(true)) --max;
479 >        if (tasks[3].cancel(true)) --max;
480 >        p1.purge();
481 >        long count = p1.getTaskCount();
482 >        assertTrue(count > 0 && count <= max);
483 >        joinPool(p1);
484 >    }
485 >
486 >    /**
487 >     *  shutDownNow returns a list containing tasks that were not run
488 >     */
489 >    public void testShutDownNow() {
490 >        ScheduledExecutor p1 = new ScheduledExecutor(1);
491          for(int i = 0; i < 5; i++)
492 <            one.schedule(newRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
493 <        List l = one.shutdownNow();
494 <        assertTrue(one.isShutdown());
492 >            p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
493 >        List l = p1.shutdownNow();
494 >        assertTrue(p1.isShutdown());
495          assertTrue(l.size() > 0 && l.size() <= 5);
496 +        joinPool(p1);
497      }
498  
499 <    public void testShutDown1(){
499 >    /**
500 >     * In default setting, shutdown cancels periodic but not delayed
501 >     * tasks at shutdown
502 >     */
503 >    public void testShutDown1() {
504          try {
505 <            ScheduledExecutor one = new ScheduledExecutor(1);
506 <            assertTrue(one.getExecuteExistingDelayedTasksAfterShutdownPolicy());
507 <            assertFalse(one.getContinueExistingPeriodicTasksAfterShutdownPolicy());
505 >            ScheduledExecutor p1 = new ScheduledExecutor(1);
506 >            assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy());
507 >            assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy());
508  
509              ScheduledCancellable[] tasks = new ScheduledCancellable[5];
510              for(int i = 0; i < 5; i++)
511 <                tasks[i] = one.schedule(newNoopRunnable(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS);
512 <            one.shutdown();
513 <            BlockingQueue q = one.getQueue();
511 >                tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
512 >            p1.shutdown();
513 >            BlockingQueue q = p1.getQueue();
514              for (Iterator it = q.iterator(); it.hasNext();) {
515                  ScheduledCancellable t = (ScheduledCancellable)it.next();
516                  assertFalse(t.isCancelled());
517              }
518 <            assertTrue(one.isShutdown());
519 <            Thread.sleep(SHORT_DELAY_MS);
518 >            assertTrue(p1.isShutdown());
519 >            Thread.sleep(SMALL_DELAY_MS);
520              for (int i = 0; i < 5; ++i) {
521                  assertTrue(tasks[i].isDone());
522                  assertFalse(tasks[i].isCancelled());
# Line 486 | Line 524 | public class ScheduledExecutorTest exten
524              
525          }
526          catch(Exception ex) {
527 <            fail("unexpected exception");
527 >            unexpectedException();
528          }
529      }
530  
531  
532 <    public void testShutDown2(){
532 >    /**
533 >     * If setExecuteExistingDelayedTasksAfterShutdownPolicy is false,
534 >     * delayed tasks are cancelled at shutdown
535 >     */
536 >    public void testShutDown2() {
537          try {
538 <            ScheduledExecutor one = new ScheduledExecutor(1);
539 <            one.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
538 >            ScheduledExecutor p1 = new ScheduledExecutor(1);
539 >            p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
540              ScheduledCancellable[] tasks = new ScheduledCancellable[5];
541              for(int i = 0; i < 5; i++)
542 <                tasks[i] = one.schedule(newNoopRunnable(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS);
543 <            one.shutdown();
544 <            assertTrue(one.isShutdown());
545 <            BlockingQueue q = one.getQueue();
542 >                tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
543 >            p1.shutdown();
544 >            assertTrue(p1.isShutdown());
545 >            BlockingQueue q = p1.getQueue();
546              assertTrue(q.isEmpty());
547 <            Thread.sleep(SHORT_DELAY_MS);
548 <            assertTrue(one.isTerminated());
547 >            Thread.sleep(SMALL_DELAY_MS);
548 >            assertTrue(p1.isTerminated());
549          }
550          catch(Exception ex) {
551 <            fail("unexpected exception");
551 >            unexpectedException();
552          }
553      }
554  
555  
556 <    public void testShutDown3(){
556 >    /**
557 >     * If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false,
558 >     * periodic tasks are not cancelled at shutdown
559 >     */
560 >    public void testShutDown3() {
561          try {
562 <            ScheduledExecutor one = new ScheduledExecutor(1);
563 <            one.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
562 >            ScheduledExecutor p1 = new ScheduledExecutor(1);
563 >            p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
564              ScheduledCancellable task =
565 <                one.scheduleAtFixedRate(newNoopRunnable(), 5, 5, TimeUnit.MILLISECONDS);
566 <            one.shutdown();
567 <            assertTrue(one.isShutdown());
568 <            BlockingQueue q = one.getQueue();
565 >                p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS);
566 >            p1.shutdown();
567 >            assertTrue(p1.isShutdown());
568 >            BlockingQueue q = p1.getQueue();
569              assertTrue(q.isEmpty());
570              Thread.sleep(SHORT_DELAY_MS);
571 <            assertTrue(one.isTerminated());
571 >            assertTrue(p1.isTerminated());
572          }
573          catch(Exception ex) {
574 <            fail("unexpected exception");
574 >            unexpectedException();
575          }
576      }
577  
578 <    public void testShutDown4(){
579 <        ScheduledExecutor one = new ScheduledExecutor(1);
578 >    /**
579 >     * if setContinueExistingPeriodicTasksAfterShutdownPolicy is true,
580 >     * periodic tasks are cancelled at shutdown
581 >     */
582 >    public void testShutDown4() {
583 >        ScheduledExecutor p1 = new ScheduledExecutor(1);
584          try {
585 <            one.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
585 >            p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
586              ScheduledCancellable task =
587 <                one.scheduleAtFixedRate(newNoopRunnable(), 5, 5, TimeUnit.MILLISECONDS);
587 >                p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS);
588              assertFalse(task.isCancelled());
589 <            one.shutdown();
589 >            p1.shutdown();
590              assertFalse(task.isCancelled());
591 <            assertFalse(one.isTerminated());
592 <            assertTrue(one.isShutdown());
591 >            assertFalse(p1.isTerminated());
592 >            assertTrue(p1.isShutdown());
593              Thread.sleep(SHORT_DELAY_MS);
594              assertFalse(task.isCancelled());
595              task.cancel(true);
596              assertTrue(task.isCancelled());
597              Thread.sleep(SHORT_DELAY_MS);
598 <            assertTrue(one.isTerminated());
598 >            assertTrue(p1.isTerminated());
599          }
600          catch(Exception ex) {
601 <            fail("unexpected exception");
601 >            unexpectedException();
602          }
603          finally {
604 <            one.shutdownNow();
604 >            p1.shutdownNow();
605          }
606      }
607  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines