--- jsr166/src/test/tck/ScheduledExecutorTest.java 2003/09/08 12:12:42 1.3 +++ jsr166/src/test/tck/ScheduledExecutorTest.java 2003/09/14 20:42:40 1.4 @@ -9,30 +9,19 @@ 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(SHORT_DELAY_MS); - waiting = false; + Thread.sleep(SMALL_DELAY_MS); done = true; } catch(Exception e){ } @@ -40,75 +29,50 @@ public class ScheduledExecutorTest exten } static class MyCallable implements Callable { - volatile boolean waiting = true; volatile boolean done = false; public Object call(){ try{ - Thread.sleep(SHORT_DELAY_MS); - waiting = false; + Thread.sleep(SMALL_DELAY_MS); done = true; }catch(Exception e){} return Boolean.TRUE; } } - public Runnable newRunnable(){ - return new Runnable(){ - public void run(){ - try{Thread.sleep(SHORT_DELAY_MS); - } catch(Exception e){ - } - } - }; - } - - public Runnable newNoopRunnable() { - return new Runnable(){ - public void run(){ - } - }; - } - - /** - * 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(SHORT_DELAY_MS/2); - assertTrue(runnable.waiting); + assertFalse(runnable.done); + Thread.sleep(SHORT_DELAY_MS); one.shutdown(); try{ Thread.sleep(MEDIUM_DELAY_MS); } catch(InterruptedException e){ fail("unexpected exception"); } - assertFalse(runnable.waiting); 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, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); - assertTrue(callable.waiting); + 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){ fail("unexpected exception"); @@ -122,12 +86,13 @@ public class ScheduledExecutorTest exten try{ MyRunnable runnable = new MyRunnable(); ScheduledExecutor one = new ScheduledExecutor(1); - one.schedule(runnable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); - Thread.sleep(SHORT_DELAY_MS/2); - assertTrue(runnable.waiting); + 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"); } @@ -141,11 +106,11 @@ public class ScheduledExecutorTest exten MyRunnable runnable = new MyRunnable(); ScheduledExecutor one = new ScheduledExecutor(1); one.schedule(runnable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); - // Thread.sleep(505); - assertTrue(runnable.waiting); + assertFalse(runnable.done); Thread.sleep(MEDIUM_DELAY_MS); assertTrue(runnable.done); one.shutdown(); + joinPool(one); } catch(Exception e){ fail("unexpected exception"); } @@ -155,241 +120,225 @@ 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); } - - /** - * Test to verify getActiveCount gives correct values + * getActiveCount gives correct values */ public void testGetActiveCount(){ ScheduledExecutor two = new ScheduledExecutor(2); - try { - assertEquals(0, two.getActiveCount()); - two.execute(newRunnable()); - try{ - Thread.sleep(SHORT_DELAY_MS/2); - } catch(Exception e){ - fail("unexpected exception"); - } - assertEquals(1, two.getActiveCount()); - } finally { - two.shutdown(); + 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); } /** - * Test to verify getCompleteTaskCount gives correct values + * getCompleteTaskCount gives correct values */ public void testGetCompletedTaskCount(){ ScheduledExecutor two = new ScheduledExecutor(2); - try { - assertEquals(0, two.getCompletedTaskCount()); - two.execute(newRunnable()); - try{ - Thread.sleep(MEDIUM_DELAY_MS/2); - } catch(Exception e){ - fail("unexpected exception"); - } - assertEquals(1, two.getCompletedTaskCount()); - } finally { - two.shutdown(); + 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); } /** - * Test to verify getCorePoolSize gives correct values + * getCorePoolSize gives correct values */ public void testGetCorePoolSize(){ ScheduledExecutor one = new ScheduledExecutor(1); - try { - assertEquals(1, one.getCorePoolSize()); - } finally { - one.shutdown(); - } + assertEquals(1, one.getCorePoolSize()); + joinPool(one); } /** - * Test to verify getLargestPoolSize gives correct values + * getLargestPoolSize gives correct values */ public void testGetLargestPoolSize(){ ScheduledExecutor two = new ScheduledExecutor(2); - try { - assertEquals(0, two.getLargestPoolSize()); - two.execute(newRunnable()); - two.execute(newRunnable()); - try{ - Thread.sleep(SHORT_DELAY_MS/2); - } catch(Exception e){ - fail("unexpected exception"); - } - assertEquals(2, two.getLargestPoolSize()); - } finally { - two.shutdown(); + 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); } /** - * Test to verify getPoolSize gives correct values + * getPoolSize gives correct values */ public void testGetPoolSize(){ ScheduledExecutor one = new ScheduledExecutor(1); - try { - assertEquals(0, one.getPoolSize()); - one.execute(newRunnable()); - assertEquals(1, one.getPoolSize()); - } finally { - one.shutdown(); - } + assertEquals(0, one.getPoolSize()); + one.execute(new SmallRunnable()); + assertEquals(1, one.getPoolSize()); + joinPool(one); } /** - * Test to verify getTaskCount gives correct values + * getTaskCount gives correct values */ public void testGetTaskCount(){ ScheduledExecutor one = new ScheduledExecutor(1); - try { - assertEquals(0, one.getTaskCount()); - for(int i = 0; i < 5; i++) - one.execute(newRunnable()); - try{ - Thread.sleep(SHORT_DELAY_MS/2); - } catch(Exception e){ - fail("unexpected exception"); - } - assertEquals(5, one.getTaskCount()); - } finally { - one.shutdown(); + 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); } /** - * Test to verify isShutDown gives correct values + * isShutDown gives correct values */ public void testIsShutdown(){ @@ -405,61 +354,56 @@ public class ScheduledExecutorTest exten /** - * Test to verify isTerminated gives correct values + * 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(newRunnable()); + one.execute(new SmallRunnable()); } finally { one.shutdown(); } - boolean flag = false; - try{ - flag = one.awaitTermination(10, TimeUnit.SECONDS); + try { + assertTrue(one.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); + assertTrue(one.isTerminated()); } catch(Exception e){ fail("unexpected exception"); } - assertTrue(one.isTerminated()); - if(!flag) - fail("ThreadPoolExecutor - thread pool did not terminate within suitable timeframe"); } /** - * Test to verify that purge correctly removes cancelled tasks + * that purge correctly removes cancelled tasks * from the queue */ public void testPurge(){ ScheduledExecutor one = new ScheduledExecutor(1); - try { - ScheduledCancellable[] tasks = new ScheduledCancellable[5]; - for(int i = 0; i < 5; i++){ - tasks[i] = one.schedule(newRunnable(), 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); - } finally { - one.shutdown(); + 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); } /** - * Test to verify shutDownNow returns a list + * 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(newRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + 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(){ @@ -470,7 +414,7 @@ public class ScheduledExecutorTest exten ScheduledCancellable[] tasks = new ScheduledCancellable[5]; for(int i = 0; i < 5; i++) - tasks[i] = one.schedule(newNoopRunnable(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS); + tasks[i] = one.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); one.shutdown(); BlockingQueue q = one.getQueue(); for (Iterator it = q.iterator(); it.hasNext();) { @@ -478,7 +422,7 @@ public class ScheduledExecutorTest exten assertFalse(t.isCancelled()); } assertTrue(one.isShutdown()); - Thread.sleep(SHORT_DELAY_MS); + Thread.sleep(SMALL_DELAY_MS); for (int i = 0; i < 5; ++i) { assertTrue(tasks[i].isDone()); assertFalse(tasks[i].isCancelled()); @@ -497,12 +441,12 @@ public class ScheduledExecutorTest exten one.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); ScheduledCancellable[] tasks = new ScheduledCancellable[5]; for(int i = 0; i < 5; i++) - tasks[i] = one.schedule(newNoopRunnable(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS); + 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(SHORT_DELAY_MS); + Thread.sleep(SMALL_DELAY_MS); assertTrue(one.isTerminated()); } catch(Exception ex) { @@ -516,7 +460,7 @@ public class ScheduledExecutorTest exten ScheduledExecutor one = new ScheduledExecutor(1); one.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); ScheduledCancellable task = - one.scheduleAtFixedRate(newNoopRunnable(), 5, 5, TimeUnit.MILLISECONDS); + one.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); one.shutdown(); assertTrue(one.isShutdown()); BlockingQueue q = one.getQueue(); @@ -534,7 +478,7 @@ public class ScheduledExecutorTest exten try { one.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); ScheduledCancellable task = - one.scheduleAtFixedRate(newNoopRunnable(), 5, 5, TimeUnit.MILLISECONDS); + one.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); assertFalse(task.isCancelled()); one.shutdown(); assertFalse(task.isCancelled());