--- jsr166/src/test/tck/ScheduledExecutorTest.java 2003/09/20 18:20:08 1.5 +++ jsr166/src/test/tck/ScheduledExecutorTest.java 2003/10/05 23:00:40 1.8 @@ -17,35 +17,13 @@ public class ScheduledExecutorTest exten return new TestSuite(ScheduledExecutorTest.class); } - static class MyRunnable implements Runnable { - volatile boolean done = false; - public void run() { - try { - Thread.sleep(SMALL_DELAY_MS); - done = true; - } catch(Exception e){ - } - } - } - - static class MyCallable implements Callable { - volatile boolean done = false; - public Object call() { - try { - Thread.sleep(SMALL_DELAY_MS); - done = true; - } catch(Exception e){ - } - return Boolean.TRUE; - } - } /** - * + * execute successfully executes a runnable */ public void testExecute() { try { - MyRunnable runnable =new MyRunnable(); + TrackedShortRunnable runnable =new TrackedShortRunnable(); ScheduledExecutor p1 = new ScheduledExecutor(1); p1.execute(runnable); assertFalse(runnable.done); @@ -66,12 +44,13 @@ public class ScheduledExecutorTest exten } + /** - * + * delayed schedule of callable successfully executes after delay */ public void testSchedule1() { try { - MyCallable callable = new MyCallable(); + TrackedCallable callable = new TrackedCallable(); ScheduledExecutor p1 = new ScheduledExecutor(1); Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); assertFalse(callable.done); @@ -87,11 +66,11 @@ public class ScheduledExecutorTest exten } /** - * + * delayed schedule of runnable successfully executes after delay */ public void testSchedule3() { try { - MyRunnable runnable = new MyRunnable(); + TrackedShortRunnable runnable = new TrackedShortRunnable(); ScheduledExecutor p1 = new ScheduledExecutor(1); p1.schedule(runnable, SMALL_DELAY_MS, TimeUnit.MILLISECONDS); Thread.sleep(SHORT_DELAY_MS); @@ -106,16 +85,36 @@ public class ScheduledExecutorTest exten } /** - * + * scheduleAtFixedRate executes runnable after given initial delay */ public void testSchedule4() { try { - MyRunnable runnable = new MyRunnable(); + TrackedShortRunnable runnable = new TrackedShortRunnable(); + 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); + h.cancel(true); + p1.shutdown(); + joinPool(p1); + } catch(Exception e){ + unexpectedException(); + } + } + + /** + * scheduleWithFixedDelay executes runnable after given initial delay + */ + public void testSchedule5() { + try { + TrackedShortRunnable runnable = new TrackedShortRunnable(); ScheduledExecutor p1 = new ScheduledExecutor(1); - p1.schedule(runnable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + 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){ @@ -123,12 +122,41 @@ public class ScheduledExecutorTest exten } } - - // exception tests + /** + * execute (null) throws NPE + */ + public void testExecuteNull() { + ScheduledExecutor se = null; + try { + se = new ScheduledExecutor(1); + se.execute(null); + shouldThrow(); + } catch(NullPointerException success){} + catch(Exception e){ + unexpectedException(); + } + + joinPool(se); + } /** - * schedule(Runnable, long) throws RejectedExecutionException - * This occurs on an attempt to schedule a task on a shutdown executor + * schedule (null) throws NPE + */ + public void testScheduleNull() { + ScheduledExecutor se = new ScheduledExecutor(1); + try { + TrackedCallable callable = null; + Future f = se.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + shouldThrow(); + } catch(NullPointerException success){} + catch(Exception e){ + unexpectedException(); + } + joinPool(se); + } + + /** + * execute throws RejectedExecutionException if shutdown */ public void testSchedule1_RejectedExecutionException() { ScheduledExecutor se = new ScheduledExecutor(1); @@ -144,8 +172,7 @@ public class ScheduledExecutorTest exten } /** - * schedule(Callable, long, TimeUnit) throws RejectedExecutionException - * This occurs on an attempt to schedule a task on a shutdown executor + * schedule throws RejectedExecutionException if shutdown */ public void testSchedule2_RejectedExecutionException() { ScheduledExecutor se = new ScheduledExecutor(1); @@ -160,8 +187,7 @@ public class ScheduledExecutorTest exten } /** - * schedule(Callable, long) throws RejectedExecutionException - * This occurs on an attempt to schedule a task on a shutdown executor + * schedule callable throws RejectedExecutionException if shutdown */ public void testSchedule3_RejectedExecutionException() { ScheduledExecutor se = new ScheduledExecutor(1); @@ -176,9 +202,7 @@ public class ScheduledExecutorTest exten } /** - * scheduleAtFixedRate(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 testScheduleAtFixedRate1_RejectedExecutionException() { ScheduledExecutor se = new ScheduledExecutor(1); @@ -193,26 +217,7 @@ public class ScheduledExecutorTest exten } /** - * 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 { - se.shutdown(); - se.scheduleAtFixedRate(new NoOpRunnable(), - 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); - shouldThrow(); - } catch(RejectedExecutionException success){ - } - joinPool(se); - } - - /** - * scheduleWithFixedDelay(Runnable, long, long, TimeUnit) throws - * RejectedExecutionException. - * This occurs on an attempt to schedule a task on a shutdown executor + * scheduleWithFixedDelay throws RejectedExecutionException if shutdown */ public void testScheduleWithFixedDelay1_RejectedExecutionException() { ScheduledExecutor se = new ScheduledExecutor(1); @@ -227,39 +232,8 @@ public class ScheduledExecutorTest exten } /** - * 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 { - se.shutdown(); - se.scheduleWithFixedDelay(new NoOpRunnable(), - 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); - shouldThrow(); - } catch(RejectedExecutionException success){ - } - joinPool(se); - } - - /** - * 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 { - se.shutdown(); - se.execute(new NoOpRunnable()); - shouldThrow(); - } catch(RejectedExecutionException success){ - } - joinPool(se); - } - - /** - * getActiveCount gives correct values + * getActiveCount increases but doesn't overestimate, when a + * thread becomes active */ public void testGetActiveCount() { ScheduledExecutor p2 = new ScheduledExecutor(2); @@ -275,7 +249,8 @@ public class ScheduledExecutorTest exten } /** - * getCompleteTaskCount gives correct values + * getCompletedTaskCount increases, but doesn't overestimate, + * when tasks complete */ public void testGetCompletedTaskCount() { ScheduledExecutor p2 = new ScheduledExecutor(2); @@ -291,7 +266,7 @@ public class ScheduledExecutorTest exten } /** - * getCorePoolSize gives correct values + * getCorePoolSize returns size given in constructor if not otherwise set */ public void testGetCorePoolSize() { ScheduledExecutor p1 = new ScheduledExecutor(1); @@ -300,7 +275,8 @@ public class ScheduledExecutorTest exten } /** - * getLargestPoolSize gives correct values + * getLargestPoolSize increases, but doesn't overestimate, when + * multiple threads active */ public void testGetLargestPoolSize() { ScheduledExecutor p2 = new ScheduledExecutor(2); @@ -317,7 +293,8 @@ public class ScheduledExecutorTest exten } /** - * getPoolSize gives correct values + * getPoolSize increases, but doesn't overestimate, when threads + * become active */ public void testGetPoolSize() { ScheduledExecutor p1 = new ScheduledExecutor(1); @@ -328,7 +305,8 @@ public class ScheduledExecutorTest exten } /** - * getTaskCount gives correct values + * getTaskCount increases, but doesn't overestimate, when tasks + * submitted */ public void testGetTaskCount() { ScheduledExecutor p1 = new ScheduledExecutor(1); @@ -343,9 +321,46 @@ public class ScheduledExecutorTest exten assertEquals(5, p1.getTaskCount()); joinPool(p1); } + + /** + * getThreadFactory returns factory in constructor if not set + */ + public void testGetThreadFactory() { + ThreadFactory tf = new SimpleThreadFactory(); + ScheduledExecutor p = new ScheduledExecutor(1, tf); + assertSame(tf, p.getThreadFactory()); + p.shutdown(); + joinPool(p); + } + + /** + * setThreadFactory sets the thread factory returned by getThreadFactory + */ + public void testSetThreadFactory() { + ThreadFactory tf = new SimpleThreadFactory(); + ScheduledExecutor p = new ScheduledExecutor(1); + p.setThreadFactory(tf); + assertSame(tf, p.getThreadFactory()); + p.shutdown(); + joinPool(p); + } + + /** + * setThreadFactory(null) throws NPE + */ + public void testSetThreadFactoryNull() { + ScheduledExecutor p = new ScheduledExecutor(1); + try { + p.setThreadFactory(null); + shouldThrow(); + } catch (NullPointerException success) { + } finally { + joinPool(p); + } + } /** - * isShutDown gives correct values + * is isShutDown is false before shutdown, true after */ public void testIsShutdown() { @@ -361,7 +376,7 @@ public class ScheduledExecutorTest exten /** - * isTerminated gives correct values + * isTerminated is false before termination, true after */ public void testIsTerminated() { ScheduledExecutor p1 = new ScheduledExecutor(1); @@ -379,7 +394,7 @@ public class ScheduledExecutorTest exten } /** - * isTerminating gives correct values + * isTerminating is not true when running or when terminated */ public void testIsTerminating() { ScheduledExecutor p1 = new ScheduledExecutor(1); @@ -400,14 +415,64 @@ public class ScheduledExecutorTest exten } /** - * that purge correctly removes cancelled tasks - * from the queue + * getQueue returns the work queue, which contains queued tasks + */ + public void testGetQueue() { + ScheduledExecutor p1 = new ScheduledExecutor(1); + ScheduledCancellable[] tasks = new ScheduledCancellable[5]; + for(int i = 0; i < 5; i++){ + tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS); + } + try { + Thread.sleep(SHORT_DELAY_MS); + BlockingQueue q = p1.getQueue(); + assertTrue(q.contains(tasks[4])); + assertFalse(q.contains(tasks[0])); + p1.shutdownNow(); + } catch(Exception e) { + unexpectedException(); + } finally { + joinPool(p1); + } + } + + /** + * remove(task) removes queued task, and fails to remove active task + */ + public void testRemove() { + ScheduledExecutor p1 = new ScheduledExecutor(1); + ScheduledCancellable[] tasks = new ScheduledCancellable[5]; + for(int i = 0; i < 5; i++){ + tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS); + } + try { + Thread.sleep(SHORT_DELAY_MS); + BlockingQueue q = p1.getQueue(); + assertFalse(p1.remove((Runnable)tasks[0])); + assertTrue(q.contains((Runnable)tasks[4])); + assertTrue(q.contains((Runnable)tasks[3])); + assertTrue(p1.remove((Runnable)tasks[4])); + assertFalse(p1.remove((Runnable)tasks[4])); + assertFalse(q.contains((Runnable)tasks[4])); + assertTrue(q.contains((Runnable)tasks[3])); + assertTrue(p1.remove((Runnable)tasks[3])); + assertFalse(q.contains((Runnable)tasks[3])); + p1.shutdownNow(); + } catch(Exception e) { + unexpectedException(); + } finally { + joinPool(p1); + } + } + + /** + * 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); + tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS); } int max = 5; if (tasks[4].cancel(true)) --max; @@ -419,13 +484,12 @@ public class ScheduledExecutorTest exten } /** - * shutDownNow returns a list - * containing the correct number of elements + * shutDownNow returns a list containing tasks that were not run */ public void testShutDownNow() { ScheduledExecutor p1 = new ScheduledExecutor(1); for(int i = 0; i < 5; i++) - p1.schedule(new SmallRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); List l = p1.shutdownNow(); assertTrue(p1.isShutdown()); assertTrue(l.size() > 0 && l.size() <= 5); @@ -433,7 +497,8 @@ public class ScheduledExecutorTest exten } /** - * + * In default setting, shutdown cancels periodic but not delayed + * tasks at shutdown */ public void testShutDown1() { try { @@ -465,7 +530,8 @@ public class ScheduledExecutorTest exten /** - * + * If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, + * delayed tasks are cancelled at shutdown */ public void testShutDown2() { try { @@ -488,7 +554,8 @@ public class ScheduledExecutorTest exten /** - * + * If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, + * periodic tasks are not cancelled at shutdown */ public void testShutDown3() { try { @@ -509,7 +576,8 @@ public class ScheduledExecutorTest exten } /** - * + * if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, + * periodic tasks are cancelled at shutdown */ public void testShutDown4() { ScheduledExecutor p1 = new ScheduledExecutor(1);