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.2 by dl, Sun Sep 7 23:50:09 2003 UTC

# Line 31 | Line 31 | public class ScheduledExecutorTest exten
31          volatile boolean done = false;
32          public void run(){
33              try{
34 <                Thread.sleep(MEDIUM_DELAY_MS);
34 >                Thread.sleep(SHORT_DELAY_MS);
35                  waiting = false;
36                  done = true;
37 <            } catch(Exception e){}
37 >            } catch(Exception e){
38 >            }
39          }
40      }
41  
# Line 43 | Line 44 | public class ScheduledExecutorTest exten
44          volatile boolean done = false;
45          public Object call(){
46              try{
47 <                Thread.sleep(MEDIUM_DELAY_MS);
47 >                Thread.sleep(SHORT_DELAY_MS);
48                  waiting = false;
49                  done = true;
50              }catch(Exception e){}
# Line 51 | Line 52 | public class ScheduledExecutorTest exten
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 +    }
71 +
72      /**
73       *  Test to verify execute successfully runs the given Runnable
74       */
# Line 59 | Line 77 | public class ScheduledExecutorTest exten
77              MyRunnable runnable =new MyRunnable();
78              ScheduledExecutor one = new ScheduledExecutor(1);
79              one.execute(runnable);
80 <            Thread.sleep(100);
80 >            Thread.sleep(SHORT_DELAY_MS/2);
81              assertTrue(runnable.waiting);
82              one.shutdown();
83 <            // make sure the Runnable has time to complete
84 <            try{Thread.sleep(1010);}catch(InterruptedException e){}
83 >            try{
84 >                Thread.sleep(MEDIUM_DELAY_MS);
85 >            } catch(InterruptedException e){
86 >                fail("unexpected exception");
87 >            }
88              assertFalse(runnable.waiting);
89              assertTrue(runnable.done);
90              one.shutdown();
# Line 82 | Line 103 | public class ScheduledExecutorTest exten
103          try{
104              MyCallable callable = new MyCallable();
105              ScheduledExecutor one = new ScheduledExecutor(1);
106 <            Future f = one.schedule(callable, 500, TimeUnit.MILLISECONDS);
86 <            //      Thread.sleep(505);
106 >            Future f = one.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
107              assertTrue(callable.waiting);
108 <            Thread.sleep(2000);
108 >            Thread.sleep(MEDIUM_DELAY_MS);
109              assertTrue(callable.done);
110              assertEquals(Boolean.TRUE, f.get());
111              one.shutdown();
112          }catch(RejectedExecutionException e){}
113 <        catch(Exception e){}
113 >        catch(Exception e){
114 >            fail("unexpected exception");
115 >        }
116      }
117  
118      /**
# Line 100 | Line 122 | public class ScheduledExecutorTest exten
122          try{
123              MyRunnable runnable = new MyRunnable();
124              ScheduledExecutor one = new ScheduledExecutor(1);
125 <            one.schedule(runnable, 500, TimeUnit.MILLISECONDS);
126 <            Thread.sleep(50);
125 >            one.schedule(runnable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
126 >            Thread.sleep(SHORT_DELAY_MS/2);
127              assertTrue(runnable.waiting);
128 <            Thread.sleep(2000);
128 >            Thread.sleep(MEDIUM_DELAY_MS);
129              assertTrue(runnable.done);
130              one.shutdown();
131          } catch(Exception e){
# Line 118 | Line 140 | public class ScheduledExecutorTest exten
140          try{
141              MyRunnable runnable = new MyRunnable();
142              ScheduledExecutor one = new ScheduledExecutor(1);
143 <            one.schedule(runnable, 500, TimeUnit.MILLISECONDS);
143 >            one.schedule(runnable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
144              //      Thread.sleep(505);
145              assertTrue(runnable.waiting);
146 <            Thread.sleep(2000);
146 >            Thread.sleep(MEDIUM_DELAY_MS);
147              assertTrue(runnable.done);
148              one.shutdown();
149          } catch(Exception e){
# Line 260 | Line 282 | public class ScheduledExecutorTest exten
282          }catch(RejectedExecutionException e){}    
283      }
284  
285 +
286 +
287 +    /**
288 +     *  Test to verify getActiveCount gives correct values
289 +     */
290 +    public void testGetActiveCount(){
291 +        ScheduledExecutor two = new ScheduledExecutor(2);
292 +        try {
293 +            assertEquals(0, two.getActiveCount());
294 +            two.execute(newRunnable());
295 +            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();
303 +        }
304 +    }
305 +    
306 +    /**
307 +     *  Test to verify getCompleteTaskCount gives correct values
308 +     */
309 +    public void testGetCompletedTaskCount(){
310 +        ScheduledExecutor two = new ScheduledExecutor(2);
311 +        try {
312 +            assertEquals(0, two.getCompletedTaskCount());
313 +            two.execute(newRunnable());
314 +            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();
322 +        }
323 +    }
324 +    
325 +    /**
326 +     *  Test to verify getCorePoolSize gives correct values
327 +     */
328 +    public void testGetCorePoolSize(){
329 +        ScheduledExecutor one = new ScheduledExecutor(1);
330 +        try {
331 +            assertEquals(1, one.getCorePoolSize());
332 +        } finally {
333 +            one.shutdown();
334 +        }
335 +    }
336 +    
337 +    /**
338 +     *  Test to verify getLargestPoolSize gives correct values
339 +     */
340 +    public void testGetLargestPoolSize(){
341 +        ScheduledExecutor two = new ScheduledExecutor(2);
342 +        try {
343 +            assertEquals(0, two.getLargestPoolSize());
344 +            two.execute(newRunnable());
345 +            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();
354 +        }
355 +    }
356 +    
357 +    /**
358 +     *  Test to verify getPoolSize gives correct values
359 +     */
360 +    public void testGetPoolSize(){
361 +        ScheduledExecutor one = new ScheduledExecutor(1);
362 +        try {
363 +            assertEquals(0, one.getPoolSize());
364 +            one.execute(newRunnable());
365 +            assertEquals(1, one.getPoolSize());
366 +        } finally {
367 +            one.shutdown();
368 +        }
369 +    }
370 +    
371 +    /**
372 +     *  Test to verify getTaskCount gives correct values
373 +     */
374 +    public void testGetTaskCount(){
375 +        ScheduledExecutor one = new ScheduledExecutor(1);
376 +        try {
377 +            assertEquals(0, one.getTaskCount());
378 +            for(int i = 0; i < 5; i++)
379 +                one.execute(newRunnable());
380 +            try{
381 +                Thread.sleep(SHORT_DELAY_MS);
382 +            } catch(Exception e){
383 +                fail("unexpected exception");
384 +            }
385 +            assertEquals(5, one.getTaskCount());
386 +        } finally {
387 +            one.shutdown();
388 +        }
389 +    }
390 +    
391 +    /**
392 +     *  Test to verify isShutDown gives correct values
393 +     */
394 +    public void testIsShutdown(){
395 +        
396 +        ScheduledExecutor one = new ScheduledExecutor(1);
397 +        try {
398 +            assertFalse(one.isShutdown());
399 +        }
400 +        finally {
401 +            one.shutdown();
402 +        }
403 +        assertTrue(one.isShutdown());
404 +    }
405 +
406 +        
407 +    /**
408 +     *  Test to verify isTerminated gives correct values
409 +     *  Makes sure termination does not take an innapropriate
410 +     *  amount of time
411 +     */
412 +    public void testIsTerminated(){
413 +        ScheduledExecutor one = new ScheduledExecutor(1);
414 +        try {
415 +            one.execute(newRunnable());
416 +        } finally {
417 +            one.shutdown();
418 +        }
419 +        boolean flag = false;
420 +        try{
421 +            flag = one.awaitTermination(10, TimeUnit.SECONDS);
422 +        } catch(Exception e){
423 +            fail("unexpected exception");
424 +        }      
425 +        assertTrue(one.isTerminated());
426 +        if(!flag)
427 +            fail("ThreadPoolExecutor - thread pool did not terminate within suitable timeframe");
428 +    }
429 +
430 +    /**
431 +     *  Test to verify that purge correctly removes cancelled tasks
432 +     *  from the queue
433 +     */
434 +    public void testPurge(){
435 +        ScheduledExecutor one = new ScheduledExecutor(1);
436 +        try {
437 +            ScheduledCancellable[] tasks = new ScheduledCancellable[5];
438 +            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);
447 +        } finally {
448 +            one.shutdown();
449 +        }
450 +    }
451 +
452 +    /**
453 +     *  Test to verify shutDownNow returns a list
454 +     *  containing the correct number of elements
455 +     */
456 +    public void testShutDownNow(){
457 +        ScheduledExecutor one = new ScheduledExecutor(1);
458 +        for(int i = 0; i < 5; i++)
459 +            one.schedule(newRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
460 +        List l = one.shutdownNow();
461 +        assertTrue(one.isShutdown());
462 +        assertTrue(l.size() > 0 && l.size() <= 5);
463 +    }
464 +
465 +    public void testShutDown1(){
466 +        try {
467 +            ScheduledExecutor one = new ScheduledExecutor(1);
468 +            assertTrue(one.getExecuteExistingDelayedTasksAfterShutdownPolicy());
469 +            assertFalse(one.getContinueExistingPeriodicTasksAfterShutdownPolicy());
470 +
471 +            ScheduledCancellable[] tasks = new ScheduledCancellable[5];
472 +            for(int i = 0; i < 5; i++)
473 +                tasks[i] = one.schedule(newNoopRunnable(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS);
474 +            one.shutdown();
475 +            BlockingQueue q = one.getQueue();
476 +            for (Iterator it = q.iterator(); it.hasNext();) {
477 +                ScheduledCancellable t = (ScheduledCancellable)it.next();
478 +                assertFalse(t.isCancelled());
479 +            }
480 +            assertTrue(one.isShutdown());
481 +            Thread.sleep(SHORT_DELAY_MS);
482 +            for (int i = 0; i < 5; ++i) {
483 +                assertTrue(tasks[i].isDone());
484 +                assertFalse(tasks[i].isCancelled());
485 +            }
486 +            
487 +        }
488 +        catch(Exception ex) {
489 +            fail("unexpected exception");
490 +        }
491 +    }
492 +
493 +
494 +    public void testShutDown2(){
495 +        try {
496 +            ScheduledExecutor one = new ScheduledExecutor(1);
497 +            one.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
498 +            ScheduledCancellable[] tasks = new ScheduledCancellable[5];
499 +            for(int i = 0; i < 5; i++)
500 +                tasks[i] = one.schedule(newNoopRunnable(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS);
501 +            one.shutdown();
502 +            assertTrue(one.isShutdown());
503 +            BlockingQueue q = one.getQueue();
504 +            assertTrue(q.isEmpty());
505 +            Thread.sleep(SHORT_DELAY_MS);
506 +            assertTrue(one.isTerminated());
507 +        }
508 +        catch(Exception ex) {
509 +            fail("unexpected exception");
510 +        }
511 +    }
512 +
513 +
514 +    public void testShutDown3(){
515 +        try {
516 +            ScheduledExecutor one = new ScheduledExecutor(1);
517 +            one.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
518 +            ScheduledCancellable task =
519 +                one.scheduleAtFixedRate(newNoopRunnable(), 5, 5, TimeUnit.MILLISECONDS);
520 +            one.shutdown();
521 +            assertTrue(one.isShutdown());
522 +            BlockingQueue q = one.getQueue();
523 +            assertTrue(q.isEmpty());
524 +            Thread.sleep(SHORT_DELAY_MS);
525 +            assertTrue(one.isTerminated());
526 +        }
527 +        catch(Exception ex) {
528 +            fail("unexpected exception");
529 +        }
530 +    }
531 +
532 +    public void testShutDown4(){
533 +        ScheduledExecutor one = new ScheduledExecutor(1);
534 +        try {
535 +            one.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
536 +            ScheduledCancellable task =
537 +                one.scheduleAtFixedRate(newNoopRunnable(), 5, 5, TimeUnit.MILLISECONDS);
538 +            assertFalse(task.isCancelled());
539 +            one.shutdown();
540 +            assertFalse(task.isCancelled());
541 +            assertFalse(one.isTerminated());
542 +            assertTrue(one.isShutdown());
543 +            Thread.sleep(SHORT_DELAY_MS);
544 +            assertFalse(task.isCancelled());
545 +            task.cancel(true);
546 +            assertTrue(task.isCancelled());
547 +            Thread.sleep(SHORT_DELAY_MS);
548 +            assertTrue(one.isTerminated());
549 +        }
550 +        catch(Exception ex) {
551 +            fail("unexpected exception");
552 +        }
553 +        finally {
554 +            one.shutdownNow();
555 +        }
556 +    }
557 +
558   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines