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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines