--- jsr166/src/test/tck/ScheduledExecutorTest.java 2003/08/31 19:24:55 1.1 +++ jsr166/src/test/tck/ScheduledExecutorTest.java 2003/09/14 20:42:40 1.4 @@ -9,88 +9,74 @@ import junit.framework.*; import java.util.*; import java.util.concurrent.*; -public class ScheduledExecutorTest extends TestCase{ - - boolean flag = false; - +public class ScheduledExecutorTest extends JSR166TestCase { public static void main(String[] args) { junit.textui.TestRunner.run (suite()); } - - public static Test suite() { return new TestSuite(ScheduledExecutorTest.class); } - private static long SHORT_DELAY_MS = 100; - private static long MEDIUM_DELAY_MS = 1000; - private static long LONG_DELAY_MS = 10000; - static class MyRunnable implements Runnable { - volatile boolean waiting = true; volatile boolean done = false; public void run(){ try{ - Thread.sleep(MEDIUM_DELAY_MS); - waiting = false; + Thread.sleep(SMALL_DELAY_MS); done = true; - } catch(Exception e){} + } catch(Exception e){ + } } } static class MyCallable implements Callable { - volatile boolean waiting = true; volatile boolean done = false; public Object call(){ try{ - Thread.sleep(MEDIUM_DELAY_MS); - waiting = false; + Thread.sleep(SMALL_DELAY_MS); done = true; }catch(Exception e){} return Boolean.TRUE; } } - /** - * Test to verify execute successfully runs the given Runnable - */ public void testExecute(){ try{ MyRunnable runnable =new MyRunnable(); ScheduledExecutor one = new ScheduledExecutor(1); one.execute(runnable); - Thread.sleep(100); - assertTrue(runnable.waiting); + assertFalse(runnable.done); + Thread.sleep(SHORT_DELAY_MS); one.shutdown(); - // make sure the Runnable has time to complete - try{Thread.sleep(1010);}catch(InterruptedException e){} - assertFalse(runnable.waiting); + try{ + Thread.sleep(MEDIUM_DELAY_MS); + } catch(InterruptedException e){ + fail("unexpected exception"); + } assertTrue(runnable.done); one.shutdown(); + joinPool(one); } catch(Exception e){ fail("unexpected exception"); } + } - /** - * Test to verify schedule successfully runs the given Callable. - * The waiting flag shows that the Callable is not started until - * immediately. - */ public void testSchedule1(){ try{ MyCallable callable = new MyCallable(); ScheduledExecutor one = new ScheduledExecutor(1); - Future f = one.schedule(callable, 500, TimeUnit.MILLISECONDS); - // Thread.sleep(505); - assertTrue(callable.waiting); - Thread.sleep(2000); + Future f = one.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + assertFalse(callable.done); + Thread.sleep(MEDIUM_DELAY_MS); assertTrue(callable.done); assertEquals(Boolean.TRUE, f.get()); one.shutdown(); + joinPool(one); }catch(RejectedExecutionException e){} - catch(Exception e){} + catch(Exception e){ + fail("unexpected exception"); + } } /** @@ -100,12 +86,13 @@ public class ScheduledExecutorTest exten try{ MyRunnable runnable = new MyRunnable(); ScheduledExecutor one = new ScheduledExecutor(1); - one.schedule(runnable, 500, TimeUnit.MILLISECONDS); - Thread.sleep(50); - assertTrue(runnable.waiting); - Thread.sleep(2000); + one.schedule(runnable, SMALL_DELAY_MS, TimeUnit.MILLISECONDS); + Thread.sleep(SHORT_DELAY_MS); + assertFalse(runnable.done); + Thread.sleep(MEDIUM_DELAY_MS); assertTrue(runnable.done); one.shutdown(); + joinPool(one); } catch(Exception e){ fail("unexpected exception"); } @@ -118,12 +105,12 @@ public class ScheduledExecutorTest exten try{ MyRunnable runnable = new MyRunnable(); ScheduledExecutor one = new ScheduledExecutor(1); - one.schedule(runnable, 500, TimeUnit.MILLISECONDS); - // Thread.sleep(505); - assertTrue(runnable.waiting); - Thread.sleep(2000); + one.schedule(runnable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + assertFalse(runnable.done); + Thread.sleep(MEDIUM_DELAY_MS); assertTrue(runnable.done); one.shutdown(); + joinPool(one); } catch(Exception e){ fail("unexpected exception"); } @@ -133,131 +120,383 @@ public class ScheduledExecutorTest exten // exception tests /** - * Test to verify schedule(Runnable, long) throws RejectedExecutionException + * schedule(Runnable, long) throws RejectedExecutionException * This occurs on an attempt to schedule a task on a shutdown executor */ public void testSchedule1_RejectedExecutionException(){ + ScheduledExecutor se = new ScheduledExecutor(1); try{ - ScheduledExecutor se = new ScheduledExecutor(1); se.shutdown(); - se.schedule(new Runnable(){ - public void run(){} - }, 10000, TimeUnit.MILLISECONDS); + se.schedule(new NoOpRunnable(), + MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); fail("shoud throw"); - }catch(RejectedExecutionException e){} + } catch(RejectedExecutionException success){ + } + joinPool(se); + } /** - * Test to verify schedule(Callable, long, TimeUnit) throws RejectedExecutionException + * schedule(Callable, long, TimeUnit) throws RejectedExecutionException * This occurs on an attempt to schedule a task on a shutdown executor */ public void testSchedule2_RejectedExecutionException(){ + ScheduledExecutor se = new ScheduledExecutor(1); try{ - ScheduledExecutor se = new ScheduledExecutor(1); se.shutdown(); - se.schedule(new Callable(){ - public Object call(){ - return Boolean.TRUE; - } - }, (long)100, TimeUnit.SECONDS); + se.schedule(new NoOpCallable(), + MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); fail("should throw"); - }catch(RejectedExecutionException e){} + } catch(RejectedExecutionException success){ + } + joinPool(se); } /** - * Test to verify schedule(Callable, long) throws RejectedExecutionException + * schedule(Callable, long) throws RejectedExecutionException * This occurs on an attempt to schedule a task on a shutdown executor */ public void testSchedule3_RejectedExecutionException(){ - try{ - ScheduledExecutor se = new ScheduledExecutor(1); + ScheduledExecutor se = new ScheduledExecutor(1); + try{ se.shutdown(); - se.schedule(new Callable(){ - public Object call(){ - return Boolean.TRUE; - } - }, 10000, TimeUnit.MILLISECONDS); + se.schedule(new NoOpCallable(), + MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); fail("should throw"); - }catch(RejectedExecutionException e){} + } catch(RejectedExecutionException success){ + } + joinPool(se); } /** - * Test to verify scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws + * scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws * RejectedExecutionException. * This occurs on an attempt to schedule a task on a shutdown executor */ public void testScheduleAtFixedRate1_RejectedExecutionException(){ + ScheduledExecutor se = new ScheduledExecutor(1); try{ - ScheduledExecutor se = new ScheduledExecutor(1); se.shutdown(); - se.scheduleAtFixedRate(new Runnable(){ - public void run(){} - }, 100, 100, TimeUnit.SECONDS); + se.scheduleAtFixedRate(new NoOpRunnable(), + MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); fail("should throw"); - }catch(RejectedExecutionException e){} + } catch(RejectedExecutionException success){ + } + joinPool(se); } /** - * Test to verify scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws + * scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws * RejectedExecutionException. * This occurs on an attempt to schedule a task on a shutdown executor */ public void testScheduleAtFixedRate2_RejectedExecutionException(){ + ScheduledExecutor se = new ScheduledExecutor(1); try{ - ScheduledExecutor se = new ScheduledExecutor(1); se.shutdown(); - se.scheduleAtFixedRate(new Runnable(){ - public void run(){} - }, 1, 100, TimeUnit.SECONDS); + se.scheduleAtFixedRate(new NoOpRunnable(), + 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); fail("should throw"); - }catch(RejectedExecutionException e){} + } catch(RejectedExecutionException success){ + } + joinPool(se); } /** - * Test to verify scheduleWithFixedDelay(Runnable, long, long, TimeUnit) throws + * scheduleWithFixedDelay(Runnable, long, long, TimeUnit) throws * RejectedExecutionException. * This occurs on an attempt to schedule a task on a shutdown executor */ public void testScheduleWithFixedDelay1_RejectedExecutionException(){ + ScheduledExecutor se = new ScheduledExecutor(1); try{ - ScheduledExecutor se = new ScheduledExecutor(1); se.shutdown(); - se.scheduleWithFixedDelay(new Runnable(){ - public void run(){} - }, 100, 100, TimeUnit.SECONDS); + se.scheduleWithFixedDelay(new NoOpRunnable(), + MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); fail("should throw"); - }catch(RejectedExecutionException e){} + } catch(RejectedExecutionException success){ + } + joinPool(se); } /** - * Test to verify scheduleWithFixedDelay(Runnable, long, long, TimeUnit) throws + * scheduleWithFixedDelay(Runnable, long, long, TimeUnit) throws * RejectedExecutionException. * This occurs on an attempt to schedule a task on a shutdown executor */ public void testScheduleWithFixedDelay2_RejectedExecutionException(){ + ScheduledExecutor se = new ScheduledExecutor(1); try{ - ScheduledExecutor se = new ScheduledExecutor(1); se.shutdown(); - se.scheduleWithFixedDelay(new Runnable(){ - public void run(){} - }, 1, 100, TimeUnit.SECONDS); + se.scheduleWithFixedDelay(new NoOpRunnable(), + 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); fail("should throw"); - }catch(RejectedExecutionException e){} + } catch(RejectedExecutionException success){ + } + joinPool(se); } /** - * Test to verify execute throws RejectedExecutionException + * execute throws RejectedExecutionException * This occurs on an attempt to schedule a task on a shutdown executor */ public void testExecute_RejectedExecutionException(){ + ScheduledExecutor se = new ScheduledExecutor(1); try{ - ScheduledExecutor se = new ScheduledExecutor(1); se.shutdown(); - se.execute(new Runnable(){ - public void run(){} - }); + se.execute(new NoOpRunnable()); fail("should throw"); - }catch(RejectedExecutionException e){} + } catch(RejectedExecutionException success){ + } + joinPool(se); + } + + /** + * getActiveCount gives correct values + */ + public void testGetActiveCount(){ + ScheduledExecutor two = new ScheduledExecutor(2); + assertEquals(0, two.getActiveCount()); + two.execute(new SmallRunnable()); + try{ + Thread.sleep(SHORT_DELAY_MS); + } catch(Exception e){ + fail("unexpected exception"); + } + assertEquals(1, two.getActiveCount()); + joinPool(two); + } + + /** + * getCompleteTaskCount gives correct values + */ + public void testGetCompletedTaskCount(){ + ScheduledExecutor two = new ScheduledExecutor(2); + assertEquals(0, two.getCompletedTaskCount()); + two.execute(new SmallRunnable()); + try{ + Thread.sleep(MEDIUM_DELAY_MS); + } catch(Exception e){ + fail("unexpected exception"); + } + assertEquals(1, two.getCompletedTaskCount()); + joinPool(two); + } + + /** + * getCorePoolSize gives correct values + */ + public void testGetCorePoolSize(){ + ScheduledExecutor one = new ScheduledExecutor(1); + assertEquals(1, one.getCorePoolSize()); + joinPool(one); + } + + /** + * getLargestPoolSize gives correct values + */ + public void testGetLargestPoolSize(){ + ScheduledExecutor two = new ScheduledExecutor(2); + assertEquals(0, two.getLargestPoolSize()); + two.execute(new SmallRunnable()); + two.execute(new SmallRunnable()); + try{ + Thread.sleep(SHORT_DELAY_MS); + } catch(Exception e){ + fail("unexpected exception"); + } + assertEquals(2, two.getLargestPoolSize()); + joinPool(two); + } + + /** + * getPoolSize gives correct values + */ + public void testGetPoolSize(){ + ScheduledExecutor one = new ScheduledExecutor(1); + assertEquals(0, one.getPoolSize()); + one.execute(new SmallRunnable()); + assertEquals(1, one.getPoolSize()); + joinPool(one); + } + + /** + * getTaskCount gives correct values + */ + public void testGetTaskCount(){ + ScheduledExecutor one = new ScheduledExecutor(1); + assertEquals(0, one.getTaskCount()); + for(int i = 0; i < 5; i++) + one.execute(new SmallRunnable()); + try{ + Thread.sleep(SHORT_DELAY_MS); + } catch(Exception e){ + fail("unexpected exception"); + } + assertEquals(5, one.getTaskCount()); + joinPool(one); + } + + /** + * isShutDown gives correct values + */ + public void testIsShutdown(){ + + ScheduledExecutor one = new ScheduledExecutor(1); + try { + assertFalse(one.isShutdown()); + } + finally { + one.shutdown(); + } + assertTrue(one.isShutdown()); + } + + + /** + * isTerminated gives correct values + * Makes sure termination does not take an innapropriate + * amount of time + */ + public void testIsTerminated(){ + ScheduledExecutor one = new ScheduledExecutor(1); + try { + one.execute(new SmallRunnable()); + } finally { + one.shutdown(); + } + try { + assertTrue(one.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); + assertTrue(one.isTerminated()); + } catch(Exception e){ + fail("unexpected exception"); + } + } + + /** + * that purge correctly removes cancelled tasks + * from the queue + */ + public void testPurge(){ + ScheduledExecutor one = new ScheduledExecutor(1); + ScheduledCancellable[] tasks = new ScheduledCancellable[5]; + for(int i = 0; i < 5; i++){ + tasks[i] = one.schedule(new SmallRunnable(), 1, TimeUnit.MILLISECONDS); + } + int max = 5; + if (tasks[4].cancel(true)) --max; + if (tasks[3].cancel(true)) --max; + one.purge(); + long count = one.getTaskCount(); + assertTrue(count > 0 && count <= max); + joinPool(one); + } + + /** + * shutDownNow returns a list + * containing the correct number of elements + */ + public void testShutDownNow(){ + ScheduledExecutor one = new ScheduledExecutor(1); + for(int i = 0; i < 5; i++) + one.schedule(new SmallRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + List l = one.shutdownNow(); + assertTrue(one.isShutdown()); + assertTrue(l.size() > 0 && l.size() <= 5); + joinPool(one); + } + + public void testShutDown1(){ + try { + ScheduledExecutor one = new ScheduledExecutor(1); + assertTrue(one.getExecuteExistingDelayedTasksAfterShutdownPolicy()); + assertFalse(one.getContinueExistingPeriodicTasksAfterShutdownPolicy()); + + ScheduledCancellable[] tasks = new ScheduledCancellable[5]; + for(int i = 0; i < 5; i++) + tasks[i] = one.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + one.shutdown(); + BlockingQueue q = one.getQueue(); + for (Iterator it = q.iterator(); it.hasNext();) { + ScheduledCancellable t = (ScheduledCancellable)it.next(); + assertFalse(t.isCancelled()); + } + assertTrue(one.isShutdown()); + Thread.sleep(SMALL_DELAY_MS); + for (int i = 0; i < 5; ++i) { + assertTrue(tasks[i].isDone()); + assertFalse(tasks[i].isCancelled()); + } + + } + catch(Exception ex) { + fail("unexpected exception"); + } + } + + + public void testShutDown2(){ + try { + ScheduledExecutor one = new ScheduledExecutor(1); + one.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); + ScheduledCancellable[] tasks = new ScheduledCancellable[5]; + for(int i = 0; i < 5; i++) + tasks[i] = one.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + one.shutdown(); + assertTrue(one.isShutdown()); + BlockingQueue q = one.getQueue(); + assertTrue(q.isEmpty()); + Thread.sleep(SMALL_DELAY_MS); + assertTrue(one.isTerminated()); + } + catch(Exception ex) { + fail("unexpected exception"); + } + } + + + public void testShutDown3(){ + try { + ScheduledExecutor one = new ScheduledExecutor(1); + one.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); + ScheduledCancellable task = + one.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); + one.shutdown(); + assertTrue(one.isShutdown()); + BlockingQueue q = one.getQueue(); + assertTrue(q.isEmpty()); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(one.isTerminated()); + } + catch(Exception ex) { + fail("unexpected exception"); + } + } + + public void testShutDown4(){ + ScheduledExecutor one = new ScheduledExecutor(1); + try { + one.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); + ScheduledCancellable task = + one.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); + assertFalse(task.isCancelled()); + one.shutdown(); + assertFalse(task.isCancelled()); + assertFalse(one.isTerminated()); + assertTrue(one.isShutdown()); + Thread.sleep(SHORT_DELAY_MS); + assertFalse(task.isCancelled()); + task.cancel(true); + assertTrue(task.isCancelled()); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(one.isTerminated()); + } + catch(Exception ex) { + fail("unexpected exception"); + } + finally { + one.shutdownNow(); + } } }