--- jsr166/src/test/tck/ScheduledExecutorTest.java 2003/09/07 23:50:09 1.2 +++ jsr166/src/test/tck/ScheduledExecutorTest.java 2003/09/25 11:02:41 1.6 @@ -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; + public void run() { + try { + Thread.sleep(SMALL_DELAY_MS); done = true; } catch(Exception e){ } @@ -40,445 +29,428 @@ 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; + public Object call() { + try { + Thread.sleep(SMALL_DELAY_MS); done = true; - }catch(Exception e){} + } 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 + * execute successfully executes a runnable */ - public void testExecute(){ - try{ + 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); - one.shutdown(); - try{ + ScheduledExecutor p1 = new ScheduledExecutor(1); + p1.execute(runnable); + assertFalse(runnable.done); + Thread.sleep(SHORT_DELAY_MS); + p1.shutdown(); + try { Thread.sleep(MEDIUM_DELAY_MS); } catch(InterruptedException e){ - fail("unexpected exception"); + unexpectedException(); } - assertFalse(runnable.waiting); assertTrue(runnable.done); - one.shutdown(); + p1.shutdown(); + joinPool(p1); } catch(Exception e){ - fail("unexpected exception"); + unexpectedException(); } + } + /** - * Test to verify schedule successfully runs the given Callable. - * The waiting flag shows that the Callable is not started until - * immediately. + * delayed schedule of callable successfully executes after delay */ - public void testSchedule1(){ - try{ + 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); + ScheduledExecutor p1 = new ScheduledExecutor(1); + Future f = p1.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(); - }catch(RejectedExecutionException e){} + p1.shutdown(); + joinPool(p1); + } catch(RejectedExecutionException e){} catch(Exception e){ - fail("unexpected exception"); + unexpectedException(); } } /** - * Another version of schedule, only using Runnable instead of Callable + * delayed schedule of runnable successfully executes after delay */ - public void testSchedule3(){ - try{ + public void testSchedule3() { + 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); + ScheduledExecutor p1 = new ScheduledExecutor(1); + p1.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(); + p1.shutdown(); + joinPool(p1); } catch(Exception e){ - fail("unexpected exception"); + unexpectedException(); } } /** - * The final version of schedule, using both long, TimeUnit and Runnable + * scheduleAtFixedRate executes runnable after given initial delay */ - public void testSchedule4(){ - try{ + public void testSchedule4() { + try { MyRunnable runnable = new MyRunnable(); - ScheduledExecutor one = new ScheduledExecutor(1); - one.schedule(runnable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); - // Thread.sleep(505); - assertTrue(runnable.waiting); + ScheduledExecutor p1 = new ScheduledExecutor(1); + ScheduledCancellable h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + assertFalse(runnable.done); Thread.sleep(MEDIUM_DELAY_MS); assertTrue(runnable.done); - one.shutdown(); + h.cancel(true); + p1.shutdown(); + joinPool(p1); } catch(Exception e){ - fail("unexpected exception"); + unexpectedException(); } } - - - // exception tests /** - * Test to verify schedule(Runnable, long) throws RejectedExecutionException - * This occurs on an attempt to schedule a task on a shutdown executor + * scheduleWithFixedDelay executes runnable after given initial delay */ - public void testSchedule1_RejectedExecutionException(){ - try{ - ScheduledExecutor se = new ScheduledExecutor(1); - se.shutdown(); - se.schedule(new Runnable(){ - public void run(){} - }, 10000, TimeUnit.MILLISECONDS); - fail("shoud throw"); - }catch(RejectedExecutionException e){} + public void testSchedule5() { + try { + MyRunnable runnable = new MyRunnable(); + ScheduledExecutor p1 = new ScheduledExecutor(1); + ScheduledCancellable h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + assertFalse(runnable.done); + Thread.sleep(MEDIUM_DELAY_MS); + assertTrue(runnable.done); + h.cancel(true); + p1.shutdown(); + joinPool(p1); + } catch(Exception e){ + unexpectedException(); + } } - + /** - * Test to verify schedule(Callable, long, TimeUnit) throws RejectedExecutionException - * This occurs on an attempt to schedule a task on a shutdown executor + * execute (null) throws NPE */ - public void testSchedule2_RejectedExecutionException(){ - try{ - ScheduledExecutor se = new ScheduledExecutor(1); - se.shutdown(); - se.schedule(new Callable(){ - public Object call(){ - return Boolean.TRUE; - } - }, (long)100, TimeUnit.SECONDS); - fail("should throw"); - }catch(RejectedExecutionException e){} + public void testExecuteNull() { + ScheduledExecutor se = null; + try { + se = new ScheduledExecutor(1); + se.execute(null); + shouldThrow(); + } catch(NullPointerException success){} + catch(Exception e){ + unexpectedException(); + } + + joinPool(se); } /** - * Test to verify 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); - se.shutdown(); - se.schedule(new Callable(){ - public Object call(){ - return Boolean.TRUE; - } - }, 10000, TimeUnit.MILLISECONDS); - fail("should throw"); - }catch(RejectedExecutionException e){} + * schedule (null) throws NPE + */ + public void testScheduleNull() { + ScheduledExecutor se = new ScheduledExecutor(1); + try { + MyCallable callable = null; + Future f = se.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + shouldThrow(); + } catch(NullPointerException success){} + catch(Exception e){ + unexpectedException(); + } + joinPool(se); } - + /** - * Test to verify scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws - * RejectedExecutionException. - * This occurs on an attempt to schedule a task on a shutdown executor - */ - public void testScheduleAtFixedRate1_RejectedExecutionException(){ - try{ - ScheduledExecutor se = new ScheduledExecutor(1); + * execute throws RejectedExecutionException if shutdown + */ + public void testSchedule1_RejectedExecutionException() { + ScheduledExecutor se = new ScheduledExecutor(1); + try { se.shutdown(); - se.scheduleAtFixedRate(new Runnable(){ - public void run(){} - }, 100, 100, TimeUnit.SECONDS); - fail("should throw"); - }catch(RejectedExecutionException e){} + se.schedule(new NoOpRunnable(), + MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + shouldThrow(); + } catch(RejectedExecutionException success){ + } + joinPool(se); + } - + /** - * Test to verify scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws - * RejectedExecutionException. - * This occurs on an attempt to schedule a task on a shutdown executor - */ - public void testScheduleAtFixedRate2_RejectedExecutionException(){ - try{ - ScheduledExecutor se = new ScheduledExecutor(1); + * schedule throws RejectedExecutionException if shutdown + */ + public void testSchedule2_RejectedExecutionException() { + ScheduledExecutor se = new ScheduledExecutor(1); + try { se.shutdown(); - se.scheduleAtFixedRate(new Runnable(){ - public void run(){} - }, 1, 100, TimeUnit.SECONDS); - fail("should throw"); - }catch(RejectedExecutionException e){} + se.schedule(new NoOpCallable(), + MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + shouldThrow(); + } catch(RejectedExecutionException success){ + } + joinPool(se); } /** - * Test to verify scheduleWithFixedDelay(Runnable, long, long, TimeUnit) throws - * RejectedExecutionException. - * This occurs on an attempt to schedule a task on a shutdown executor + * schedule callable throws RejectedExecutionException if shutdown */ - public void testScheduleWithFixedDelay1_RejectedExecutionException(){ - try{ - ScheduledExecutor se = new ScheduledExecutor(1); + public void testSchedule3_RejectedExecutionException() { + ScheduledExecutor se = new ScheduledExecutor(1); + try { se.shutdown(); - se.scheduleWithFixedDelay(new Runnable(){ - public void run(){} - }, 100, 100, TimeUnit.SECONDS); - fail("should throw"); - }catch(RejectedExecutionException e){} + se.schedule(new NoOpCallable(), + MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + shouldThrow(); + } catch(RejectedExecutionException success){ + } + joinPool(se); } /** - * Test to verify scheduleWithFixedDelay(Runnable, long, long, TimeUnit) throws - * RejectedExecutionException. - * This occurs on an attempt to schedule a task on a shutdown executor + * scheduleAtFixedRate throws RejectedExecutionException if shutdown */ - public void testScheduleWithFixedDelay2_RejectedExecutionException(){ - try{ - ScheduledExecutor se = new ScheduledExecutor(1); + public void testScheduleAtFixedRate1_RejectedExecutionException() { + ScheduledExecutor se = new ScheduledExecutor(1); + try { se.shutdown(); - se.scheduleWithFixedDelay(new Runnable(){ - public void run(){} - }, 1, 100, TimeUnit.SECONDS); - fail("should throw"); - }catch(RejectedExecutionException e){} + se.scheduleAtFixedRate(new NoOpRunnable(), + MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + shouldThrow(); + } catch(RejectedExecutionException success){ + } + joinPool(se); } - + /** - * Test to verify execute throws RejectedExecutionException - * This occurs on an attempt to schedule a task on a shutdown executor + * scheduleWithFixedDelay throws RejectedExecutionException if shutdown */ - public void testExecute_RejectedExecutionException(){ - try{ - ScheduledExecutor se = new ScheduledExecutor(1); + public void testScheduleWithFixedDelay1_RejectedExecutionException() { + ScheduledExecutor se = new ScheduledExecutor(1); + try { se.shutdown(); - se.execute(new Runnable(){ - public void run(){} - }); - fail("should throw"); - }catch(RejectedExecutionException e){} + se.scheduleWithFixedDelay(new NoOpRunnable(), + MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + shouldThrow(); + } catch(RejectedExecutionException success){ + } + joinPool(se); } - - /** - * Test to verify getActiveCount gives correct values - */ - public void testGetActiveCount(){ - ScheduledExecutor two = new ScheduledExecutor(2); + * getActiveCount increases but doesn't overestimate, when a + * thread becomes active + */ + public void testGetActiveCount() { + ScheduledExecutor p2 = new ScheduledExecutor(2); + assertEquals(0, p2.getActiveCount()); + p2.execute(new SmallRunnable()); 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(); + Thread.sleep(SHORT_DELAY_MS); + } catch(Exception e){ + unexpectedException(); } + assertEquals(1, p2.getActiveCount()); + joinPool(p2); } /** - * Test to verify getCompleteTaskCount gives correct values + * getCompletedTaskCount increases, but doesn't overestimate, + * when tasks complete */ - public void testGetCompletedTaskCount(){ - ScheduledExecutor two = new ScheduledExecutor(2); + public void testGetCompletedTaskCount() { + ScheduledExecutor p2 = new ScheduledExecutor(2); + assertEquals(0, p2.getCompletedTaskCount()); + p2.execute(new SmallRunnable()); try { - assertEquals(0, two.getCompletedTaskCount()); - two.execute(newRunnable()); - try{ - Thread.sleep(MEDIUM_DELAY_MS); - } catch(Exception e){ - fail("unexpected exception"); - } - assertEquals(1, two.getCompletedTaskCount()); - } finally { - two.shutdown(); + Thread.sleep(MEDIUM_DELAY_MS); + } catch(Exception e){ + unexpectedException(); } + assertEquals(1, p2.getCompletedTaskCount()); + joinPool(p2); } /** - * Test to verify getCorePoolSize gives correct values + * getCorePoolSize returns size given in constructor if not otherwise set */ - public void testGetCorePoolSize(){ - ScheduledExecutor one = new ScheduledExecutor(1); - try { - assertEquals(1, one.getCorePoolSize()); - } finally { - one.shutdown(); - } + public void testGetCorePoolSize() { + ScheduledExecutor p1 = new ScheduledExecutor(1); + assertEquals(1, p1.getCorePoolSize()); + joinPool(p1); } /** - * Test to verify getLargestPoolSize gives correct values + * getLargestPoolSize increases, but doesn't overestimate, when + * multiple threads active */ - public void testGetLargestPoolSize(){ - ScheduledExecutor two = new ScheduledExecutor(2); + public void testGetLargestPoolSize() { + ScheduledExecutor p2 = new ScheduledExecutor(2); + assertEquals(0, p2.getLargestPoolSize()); + p2.execute(new SmallRunnable()); + p2.execute(new SmallRunnable()); try { - assertEquals(0, two.getLargestPoolSize()); - two.execute(newRunnable()); - two.execute(newRunnable()); - try{ - Thread.sleep(SHORT_DELAY_MS); - } catch(Exception e){ - fail("unexpected exception"); - } - assertEquals(2, two.getLargestPoolSize()); - } finally { - two.shutdown(); + Thread.sleep(SHORT_DELAY_MS); + } catch(Exception e){ + unexpectedException(); } + assertEquals(2, p2.getLargestPoolSize()); + joinPool(p2); } /** - * Test to verify getPoolSize gives correct values + * getPoolSize increases, but doesn't overestimate, when threads + * become active */ - public void testGetPoolSize(){ - ScheduledExecutor one = new ScheduledExecutor(1); - try { - assertEquals(0, one.getPoolSize()); - one.execute(newRunnable()); - assertEquals(1, one.getPoolSize()); - } finally { - one.shutdown(); - } + public void testGetPoolSize() { + ScheduledExecutor p1 = new ScheduledExecutor(1); + assertEquals(0, p1.getPoolSize()); + p1.execute(new SmallRunnable()); + assertEquals(1, p1.getPoolSize()); + joinPool(p1); } /** - * Test to verify getTaskCount gives correct values + * getTaskCount increases, but doesn't overestimate, when tasks + * submitted */ - public void testGetTaskCount(){ - ScheduledExecutor one = new ScheduledExecutor(1); + public void testGetTaskCount() { + ScheduledExecutor p1 = new ScheduledExecutor(1); + assertEquals(0, p1.getTaskCount()); + for(int i = 0; i < 5; i++) + p1.execute(new SmallRunnable()); try { - assertEquals(0, one.getTaskCount()); - for(int i = 0; i < 5; i++) - one.execute(newRunnable()); - try{ - Thread.sleep(SHORT_DELAY_MS); - } catch(Exception e){ - fail("unexpected exception"); - } - assertEquals(5, one.getTaskCount()); - } finally { - one.shutdown(); + Thread.sleep(SHORT_DELAY_MS); + } catch(Exception e){ + unexpectedException(); } + assertEquals(5, p1.getTaskCount()); + joinPool(p1); } /** - * Test to verify isShutDown gives correct values + * is isShutDown is false before shutdown, true after */ - public void testIsShutdown(){ + public void testIsShutdown() { - ScheduledExecutor one = new ScheduledExecutor(1); + ScheduledExecutor p1 = new ScheduledExecutor(1); try { - assertFalse(one.isShutdown()); + assertFalse(p1.isShutdown()); } finally { - one.shutdown(); + p1.shutdown(); } - assertTrue(one.isShutdown()); + assertTrue(p1.isShutdown()); } /** - * Test to verify isTerminated gives correct values - * Makes sure termination does not take an innapropriate - * amount of time + * isTerminated is false before termination, true after */ - public void testIsTerminated(){ - ScheduledExecutor one = new ScheduledExecutor(1); + public void testIsTerminated() { + ScheduledExecutor p1 = new ScheduledExecutor(1); try { - one.execute(newRunnable()); + p1.execute(new SmallRunnable()); } finally { - one.shutdown(); + p1.shutdown(); } - boolean flag = false; - try{ - flag = one.awaitTermination(10, TimeUnit.SECONDS); + try { + assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); + assertTrue(p1.isTerminated()); } catch(Exception e){ - fail("unexpected exception"); + unexpectedException(); } - assertTrue(one.isTerminated()); - if(!flag) - fail("ThreadPoolExecutor - thread pool did not terminate within suitable timeframe"); } /** - * Test to verify that purge correctly removes cancelled tasks - * from the queue + * isTerminating is not true when running or when terminated */ - public void testPurge(){ - ScheduledExecutor one = new ScheduledExecutor(1); + public void testIsTerminating() { + ScheduledExecutor p1 = new ScheduledExecutor(1); + assertFalse(p1.isTerminating()); 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); + p1.execute(new SmallRunnable()); + assertFalse(p1.isTerminating()); } finally { - one.shutdown(); + p1.shutdown(); + } + try { + assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); + assertTrue(p1.isTerminated()); + assertFalse(p1.isTerminating()); + } catch(Exception e){ + unexpectedException(); + } + } + + /** + * purge removes cancelled tasks from the queue + */ + public void testPurge() { + ScheduledExecutor p1 = new ScheduledExecutor(1); + ScheduledCancellable[] tasks = new ScheduledCancellable[5]; + for(int i = 0; i < 5; i++){ + tasks[i] = p1.schedule(new SmallRunnable(), 1, TimeUnit.MILLISECONDS); } + int max = 5; + if (tasks[4].cancel(true)) --max; + if (tasks[3].cancel(true)) --max; + p1.purge(); + long count = p1.getTaskCount(); + assertTrue(count > 0 && count <= max); + joinPool(p1); } /** - * Test to verify shutDownNow returns a list - * containing the correct number of elements + * shutDownNow returns a list containing tasks that were not run */ - public void testShutDownNow(){ - ScheduledExecutor one = new ScheduledExecutor(1); + public void testShutDownNow() { + ScheduledExecutor p1 = new ScheduledExecutor(1); for(int i = 0; i < 5; i++) - one.schedule(newRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); - List l = one.shutdownNow(); - assertTrue(one.isShutdown()); + p1.schedule(new SmallRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + List l = p1.shutdownNow(); + assertTrue(p1.isShutdown()); assertTrue(l.size() > 0 && l.size() <= 5); + joinPool(p1); } - public void testShutDown1(){ + /** + * In default setting, shutdown cancels periodic but not delayed + * tasks at shutdown + */ + public void testShutDown1() { try { - ScheduledExecutor one = new ScheduledExecutor(1); - assertTrue(one.getExecuteExistingDelayedTasksAfterShutdownPolicy()); - assertFalse(one.getContinueExistingPeriodicTasksAfterShutdownPolicy()); + ScheduledExecutor p1 = new ScheduledExecutor(1); + assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy()); + assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy()); ScheduledCancellable[] tasks = new ScheduledCancellable[5]; for(int i = 0; i < 5; i++) - tasks[i] = one.schedule(newNoopRunnable(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS); - one.shutdown(); - BlockingQueue q = one.getQueue(); + tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + p1.shutdown(); + BlockingQueue q = p1.getQueue(); for (Iterator it = q.iterator(); it.hasNext();) { ScheduledCancellable t = (ScheduledCancellable)it.next(); assertFalse(t.isCancelled()); } - assertTrue(one.isShutdown()); - Thread.sleep(SHORT_DELAY_MS); + assertTrue(p1.isShutdown()); + Thread.sleep(SMALL_DELAY_MS); for (int i = 0; i < 5; ++i) { assertTrue(tasks[i].isDone()); assertFalse(tasks[i].isCancelled()); @@ -486,72 +458,84 @@ public class ScheduledExecutorTest exten } catch(Exception ex) { - fail("unexpected exception"); + unexpectedException(); } } - public void testShutDown2(){ + /** + * If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, + * delayed tasks are cancelled at shutdown + */ + public void testShutDown2() { try { - ScheduledExecutor one = new ScheduledExecutor(1); - one.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); + ScheduledExecutor p1 = new ScheduledExecutor(1); + p1.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); - one.shutdown(); - assertTrue(one.isShutdown()); - BlockingQueue q = one.getQueue(); + tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + p1.shutdown(); + assertTrue(p1.isShutdown()); + BlockingQueue q = p1.getQueue(); assertTrue(q.isEmpty()); - Thread.sleep(SHORT_DELAY_MS); - assertTrue(one.isTerminated()); + Thread.sleep(SMALL_DELAY_MS); + assertTrue(p1.isTerminated()); } catch(Exception ex) { - fail("unexpected exception"); + unexpectedException(); } } - public void testShutDown3(){ + /** + * If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, + * periodic tasks are not cancelled at shutdown + */ + public void testShutDown3() { try { - ScheduledExecutor one = new ScheduledExecutor(1); - one.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); + ScheduledExecutor p1 = new ScheduledExecutor(1); + p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); ScheduledCancellable task = - one.scheduleAtFixedRate(newNoopRunnable(), 5, 5, TimeUnit.MILLISECONDS); - one.shutdown(); - assertTrue(one.isShutdown()); - BlockingQueue q = one.getQueue(); + p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); + p1.shutdown(); + assertTrue(p1.isShutdown()); + BlockingQueue q = p1.getQueue(); assertTrue(q.isEmpty()); Thread.sleep(SHORT_DELAY_MS); - assertTrue(one.isTerminated()); + assertTrue(p1.isTerminated()); } catch(Exception ex) { - fail("unexpected exception"); + unexpectedException(); } } - public void testShutDown4(){ - ScheduledExecutor one = new ScheduledExecutor(1); + /** + * if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, + * periodic tasks are cancelled at shutdown + */ + public void testShutDown4() { + ScheduledExecutor p1 = new ScheduledExecutor(1); try { - one.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); + p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); ScheduledCancellable task = - one.scheduleAtFixedRate(newNoopRunnable(), 5, 5, TimeUnit.MILLISECONDS); + p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); assertFalse(task.isCancelled()); - one.shutdown(); + p1.shutdown(); assertFalse(task.isCancelled()); - assertFalse(one.isTerminated()); - assertTrue(one.isShutdown()); + assertFalse(p1.isTerminated()); + assertTrue(p1.isShutdown()); Thread.sleep(SHORT_DELAY_MS); assertFalse(task.isCancelled()); task.cancel(true); assertTrue(task.isCancelled()); Thread.sleep(SHORT_DELAY_MS); - assertTrue(one.isTerminated()); + assertTrue(p1.isTerminated()); } catch(Exception ex) { - fail("unexpected exception"); + unexpectedException(); } finally { - one.shutdownNow(); + p1.shutdownNow(); } }