--- jsr166/src/test/tck/ScheduledExecutorTest.java 2003/09/14 20:42:40 1.4 +++ jsr166/src/test/tck/ScheduledExecutorTest.java 2003/12/22 00:48:56 1.10 @@ -17,119 +17,155 @@ 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; - } - } - - public void testExecute(){ - try{ - MyRunnable runnable =new MyRunnable(); - ScheduledExecutor one = new ScheduledExecutor(1); - one.execute(runnable); + /** + * execute successfully executes a runnable + */ + public void testExecute() { + try { + TrackedShortRunnable runnable =new TrackedShortRunnable(); + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); + p1.execute(runnable); assertFalse(runnable.done); Thread.sleep(SHORT_DELAY_MS); - one.shutdown(); - try{ + p1.shutdown(); + try { Thread.sleep(MEDIUM_DELAY_MS); } catch(InterruptedException e){ - fail("unexpected exception"); + unexpectedException(); } assertTrue(runnable.done); - one.shutdown(); - joinPool(one); + p1.shutdown(); + joinPool(p1); } catch(Exception e){ - fail("unexpected exception"); + unexpectedException(); } } - public void testSchedule1(){ - try{ - MyCallable callable = new MyCallable(); - ScheduledExecutor one = new ScheduledExecutor(1); - Future f = one.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + + /** + * delayed schedule of callable successfully executes after delay + */ + public void testSchedule1() { + try { + TrackedCallable callable = new TrackedCallable(); + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(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(); - joinPool(one); - }catch(RejectedExecutionException e){} + p1.shutdown(); + joinPool(p1); + } catch(RejectedExecutionException e){} catch(Exception e){ - fail("unexpected exception"); + e.printStackTrace(); + unexpectedException(); } } /** - * Another version of schedule, only using Runnable instead of Callable + * delayed schedule of runnable successfully executes after delay */ - public void testSchedule3(){ - try{ - MyRunnable runnable = new MyRunnable(); - ScheduledExecutor one = new ScheduledExecutor(1); - one.schedule(runnable, SMALL_DELAY_MS, TimeUnit.MILLISECONDS); + public void testSchedule3() { + try { + TrackedShortRunnable runnable = new TrackedShortRunnable(); + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(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(); - joinPool(one); + 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 { + TrackedShortRunnable runnable = new TrackedShortRunnable(); + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); + ScheduledFuture 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 testSchedule4(){ - try{ - MyRunnable runnable = new MyRunnable(); - ScheduledExecutor one = new ScheduledExecutor(1); - one.schedule(runnable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + public void testSchedule5() { + try { + TrackedShortRunnable runnable = new TrackedShortRunnable(); + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); + ScheduledFuture h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); assertFalse(runnable.done); Thread.sleep(MEDIUM_DELAY_MS); assertTrue(runnable.done); - one.shutdown(); - joinPool(one); + h.cancel(true); + p1.shutdown(); + joinPool(p1); } catch(Exception e){ - fail("unexpected exception"); + unexpectedException(); } } - - // exception tests + /** + * execute (null) throws NPE + */ + public void testExecuteNull() { + ScheduledThreadPoolExecutor se = null; + try { + se = new ScheduledThreadPoolExecutor(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 testSchedule1_RejectedExecutionException(){ - ScheduledExecutor se = new ScheduledExecutor(1); - try{ + public void testScheduleNull() { + ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(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() { + ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); + try { se.shutdown(); se.schedule(new NoOpRunnable(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); - fail("shoud throw"); + shouldThrow(); } catch(RejectedExecutionException success){ } joinPool(se); @@ -137,291 +173,350 @@ 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); - try{ + public void testSchedule2_RejectedExecutionException() { + ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); + try { se.shutdown(); se.schedule(new NoOpCallable(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); - fail("should throw"); + shouldThrow(); } catch(RejectedExecutionException success){ } joinPool(se); } /** - * 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); - try{ + public void testSchedule3_RejectedExecutionException() { + ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); + try { se.shutdown(); se.schedule(new NoOpCallable(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); - fail("should throw"); + shouldThrow(); } catch(RejectedExecutionException success){ } joinPool(se); } /** - * 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{ + * scheduleAtFixedRate throws RejectedExecutionException if shutdown + */ + public void testScheduleAtFixedRate1_RejectedExecutionException() { + ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); + try { se.shutdown(); se.scheduleAtFixedRate(new NoOpRunnable(), MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); - fail("should throw"); + shouldThrow(); } catch(RejectedExecutionException success){ } joinPool(se); } /** - * 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); - fail("should throw"); - } 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 - */ - public void testScheduleWithFixedDelay1_RejectedExecutionException(){ - ScheduledExecutor se = new ScheduledExecutor(1); - try{ + * scheduleWithFixedDelay throws RejectedExecutionException if shutdown + */ + public void testScheduleWithFixedDelay1_RejectedExecutionException() { + ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); + try { se.shutdown(); se.scheduleWithFixedDelay(new NoOpRunnable(), MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); - fail("should throw"); - } 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 - */ - public void testScheduleWithFixedDelay2_RejectedExecutionException(){ - ScheduledExecutor se = new ScheduledExecutor(1); - try{ - se.shutdown(); - se.scheduleWithFixedDelay(new NoOpRunnable(), - 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); - fail("should throw"); + shouldThrow(); } catch(RejectedExecutionException success){ } joinPool(se); } /** - * execute throws RejectedExecutionException - * This occurs on an attempt to schedule a task on a shutdown executor + * getActiveCount increases but doesn't overestimate, when a + * thread becomes active */ - public void testExecute_RejectedExecutionException(){ - ScheduledExecutor se = new ScheduledExecutor(1); - try{ - se.shutdown(); - se.execute(new NoOpRunnable()); - fail("should throw"); - } 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{ + public void testGetActiveCount() { + ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2); + assertEquals(0, p2.getActiveCount()); + p2.execute(new SmallRunnable()); + try { Thread.sleep(SHORT_DELAY_MS); } catch(Exception e){ - fail("unexpected exception"); + unexpectedException(); } - assertEquals(1, two.getActiveCount()); - joinPool(two); + assertEquals(1, p2.getActiveCount()); + joinPool(p2); } /** - * getCompleteTaskCount gives correct values + * getCompletedTaskCount increases, but doesn't overestimate, + * when tasks complete */ - public void testGetCompletedTaskCount(){ - ScheduledExecutor two = new ScheduledExecutor(2); - assertEquals(0, two.getCompletedTaskCount()); - two.execute(new SmallRunnable()); - try{ + public void testGetCompletedTaskCount() { + ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2); + assertEquals(0, p2.getCompletedTaskCount()); + p2.execute(new SmallRunnable()); + try { Thread.sleep(MEDIUM_DELAY_MS); } catch(Exception e){ - fail("unexpected exception"); + unexpectedException(); } - assertEquals(1, two.getCompletedTaskCount()); - joinPool(two); + assertEquals(1, p2.getCompletedTaskCount()); + joinPool(p2); } /** - * getCorePoolSize gives correct values + * getCorePoolSize returns size given in constructor if not otherwise set */ - public void testGetCorePoolSize(){ - ScheduledExecutor one = new ScheduledExecutor(1); - assertEquals(1, one.getCorePoolSize()); - joinPool(one); + public void testGetCorePoolSize() { + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); + assertEquals(1, p1.getCorePoolSize()); + joinPool(p1); } /** - * getLargestPoolSize gives correct values + * getLargestPoolSize increases, but doesn't overestimate, when + * multiple threads active */ - public void testGetLargestPoolSize(){ - ScheduledExecutor two = new ScheduledExecutor(2); - assertEquals(0, two.getLargestPoolSize()); - two.execute(new SmallRunnable()); - two.execute(new SmallRunnable()); - try{ + public void testGetLargestPoolSize() { + ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2); + assertEquals(0, p2.getLargestPoolSize()); + p2.execute(new SmallRunnable()); + p2.execute(new SmallRunnable()); + try { Thread.sleep(SHORT_DELAY_MS); } catch(Exception e){ - fail("unexpected exception"); + unexpectedException(); } - assertEquals(2, two.getLargestPoolSize()); - joinPool(two); + assertEquals(2, p2.getLargestPoolSize()); + joinPool(p2); } /** - * getPoolSize gives correct values + * getPoolSize increases, but doesn't overestimate, when threads + * become active */ - public void testGetPoolSize(){ - ScheduledExecutor one = new ScheduledExecutor(1); - assertEquals(0, one.getPoolSize()); - one.execute(new SmallRunnable()); - assertEquals(1, one.getPoolSize()); - joinPool(one); + public void testGetPoolSize() { + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); + assertEquals(0, p1.getPoolSize()); + p1.execute(new SmallRunnable()); + assertEquals(1, p1.getPoolSize()); + joinPool(p1); } /** - * getTaskCount gives correct values + * getTaskCount increases, but doesn't overestimate, when tasks + * submitted */ - public void testGetTaskCount(){ - ScheduledExecutor one = new ScheduledExecutor(1); - assertEquals(0, one.getTaskCount()); + public void testGetTaskCount() { + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); + assertEquals(0, p1.getTaskCount()); for(int i = 0; i < 5; i++) - one.execute(new SmallRunnable()); - try{ + p1.execute(new SmallRunnable()); + try { Thread.sleep(SHORT_DELAY_MS); } catch(Exception e){ - fail("unexpected exception"); + unexpectedException(); + } + assertEquals(5, p1.getTaskCount()); + joinPool(p1); + } + + /** + * getThreadFactory returns factory in constructor if not set + */ + public void testGetThreadFactory() { + ThreadFactory tf = new SimpleThreadFactory(); + ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(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(); + ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); + p.setThreadFactory(tf); + assertSame(tf, p.getThreadFactory()); + p.shutdown(); + joinPool(p); + } + + /** + * setThreadFactory(null) throws NPE + */ + public void testSetThreadFactoryNull() { + ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); + try { + p.setThreadFactory(null); + shouldThrow(); + } catch (NullPointerException success) { + } finally { + joinPool(p); } - assertEquals(5, one.getTaskCount()); - joinPool(one); } /** - * isShutDown gives correct values + * is isShutDown is false before shutdown, true after */ - public void testIsShutdown(){ + public void testIsShutdown() { - ScheduledExecutor one = new ScheduledExecutor(1); + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); try { - assertFalse(one.isShutdown()); + assertFalse(p1.isShutdown()); } finally { - one.shutdown(); + p1.shutdown(); } - assertTrue(one.isShutdown()); + assertTrue(p1.isShutdown()); } /** - * 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() { + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); try { - one.execute(new SmallRunnable()); + p1.execute(new SmallRunnable()); } finally { - one.shutdown(); + p1.shutdown(); } try { - assertTrue(one.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); - assertTrue(one.isTerminated()); + assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); + assertTrue(p1.isTerminated()); } catch(Exception e){ - fail("unexpected exception"); + unexpectedException(); } } /** - * 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); - ScheduledCancellable[] tasks = new ScheduledCancellable[5]; + public void testIsTerminating() { + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); + assertFalse(p1.isTerminating()); + try { + p1.execute(new SmallRunnable()); + assertFalse(p1.isTerminating()); + } finally { + p1.shutdown(); + } + try { + assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); + assertTrue(p1.isTerminated()); + assertFalse(p1.isTerminating()); + } catch(Exception e){ + unexpectedException(); + } + } + + /** + * getQueue returns the work queue, which contains queued tasks + */ + public void testGetQueue() { + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); + ScheduledFuture[] tasks = new ScheduledFuture[5]; for(int i = 0; i < 5; i++){ - tasks[i] = one.schedule(new SmallRunnable(), 1, TimeUnit.MILLISECONDS); + 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() { + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); + ScheduledFuture[] tasks = new ScheduledFuture[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() { + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); + ScheduledFuture[] tasks = new ScheduledFuture[5]; + for(int i = 0; i < 5; i++){ + tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS); } int max = 5; if (tasks[4].cancel(true)) --max; if (tasks[3].cancel(true)) --max; - one.purge(); - long count = one.getTaskCount(); + p1.purge(); + long count = p1.getTaskCount(); assertTrue(count > 0 && count <= max); - joinPool(one); + joinPool(p1); } /** - * 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() { + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); for(int i = 0; i < 5; i++) - one.schedule(new SmallRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); - List l = one.shutdownNow(); - assertTrue(one.isShutdown()); + p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + List l = p1.shutdownNow(); + assertTrue(p1.isShutdown()); assertTrue(l.size() > 0 && l.size() <= 5); - joinPool(one); + 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()); + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); + assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy()); + assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy()); - ScheduledCancellable[] tasks = new ScheduledCancellable[5]; + ScheduledFuture[] tasks = new ScheduledFuture[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(); + 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(); + ScheduledFuture t = (ScheduledFuture)it.next(); assertFalse(t.isCancelled()); } - assertTrue(one.isShutdown()); + assertTrue(p1.isShutdown()); Thread.sleep(SMALL_DELAY_MS); for (int i = 0; i < 5; ++i) { assertTrue(tasks[i].isDone()); @@ -430,73 +525,323 @@ 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); - ScheduledCancellable[] tasks = new ScheduledCancellable[5]; + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); + p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); + ScheduledFuture[] tasks = new ScheduledFuture[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(); + 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(SMALL_DELAY_MS); - assertTrue(one.isTerminated()); + 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); - ScheduledCancellable task = - one.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); - one.shutdown(); - assertTrue(one.isShutdown()); - BlockingQueue q = one.getQueue(); + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); + p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); + ScheduledFuture task = + 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() { + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); try { - one.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); - ScheduledCancellable task = - one.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); + p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); + ScheduledFuture task = + 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(); + } + } + + /** + * completed submit of callable returns result + */ + public void testSubmitCallable() { + ExecutorService e = new ScheduledThreadPoolExecutor(2); + try { + Future future = e.submit(new StringTask()); + String result = future.get(); + assertSame(TEST_STRING, result); + } + catch (ExecutionException ex) { + unexpectedException(); + } + catch (InterruptedException ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * completed submit of runnable returns successfully + */ + public void testSubmitRunnable() { + ExecutorService e = new ScheduledThreadPoolExecutor(2); + try { + Future future = e.submit(new NoOpRunnable()); + future.get(); + assertTrue(future.isDone()); + } + catch (ExecutionException ex) { + unexpectedException(); + } + catch (InterruptedException ex) { + unexpectedException(); + } finally { + joinPool(e); } } + /** + * completed submit of (runnable, result) returns result + */ + public void testSubmitRunnable2() { + ExecutorService e = new ScheduledThreadPoolExecutor(2); + try { + Future future = e.submit(new NoOpRunnable(), TEST_STRING); + String result = future.get(); + assertSame(TEST_STRING, result); + } + catch (ExecutionException ex) { + unexpectedException(); + } + catch (InterruptedException ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + + + + + /** + * invokeAny(null) throws NPE + */ + public void testInvokeAny1() { + ExecutorService e = new ScheduledThreadPoolExecutor(2); + try { + e.invokeAny(null); + } catch (NullPointerException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * invokeAny(empty collection) throws IAE + */ + public void testInvokeAny2() { + ExecutorService e = new ScheduledThreadPoolExecutor(2); + try { + e.invokeAny(new ArrayList>()); + } catch (IllegalArgumentException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * invokeAny(c) throws NPE if c has null elements + */ + public void testInvokeAny3() { + ExecutorService e = new ScheduledThreadPoolExecutor(2); + try { + ArrayList> l = new ArrayList>(); + l.add(new StringTask()); + l.add(null); + e.invokeAny(l); + } catch (NullPointerException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * invokeAny(c) throws ExecutionException if no task completes + */ + public void testInvokeAny4() { + ExecutorService e = new ScheduledThreadPoolExecutor(2); + try { + ArrayList> l = new ArrayList>(); + l.add(new NPETask()); + e.invokeAny(l); + } catch (ExecutionException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * invokeAny(c) returns result of some task + */ + public void testInvokeAny5() { + ExecutorService e = new ScheduledThreadPoolExecutor(2); + try { + ArrayList> l = new ArrayList>(); + l.add(new StringTask()); + l.add(new StringTask()); + String result = e.invokeAny(l); + assertSame(TEST_STRING, result); + } catch (ExecutionException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * invokeAll(null) throws NPE + */ + public void testInvokeAll1() { + ExecutorService e = new ScheduledThreadPoolExecutor(2); + try { + e.invokeAll(null); + } catch (NullPointerException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * invokeAll(empty collection) returns empty collection + */ + public void testInvokeAll2() { + ExecutorService e = new ScheduledThreadPoolExecutor(2); + try { + List> r = e.invokeAll(new ArrayList>()); + assertTrue(r.isEmpty()); + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * invokeAll(c) throws NPE if c has null elements + */ + public void testInvokeAll3() { + ExecutorService e = new ScheduledThreadPoolExecutor(2); + try { + ArrayList> l = new ArrayList>(); + l.add(new StringTask()); + l.add(null); + e.invokeAll(l); + } catch (NullPointerException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * get of invokeAll(c) throws exception on failed task + */ + public void testInvokeAll4() { + ExecutorService e = new ScheduledThreadPoolExecutor(2); + try { + ArrayList> l = new ArrayList>(); + l.add(new NPETask()); + List> result = e.invokeAll(l); + assertEquals(1, result.size()); + for (Iterator> it = result.iterator(); it.hasNext();) + it.next().get(); + } catch(ExecutionException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * invokeAll(c) returns results of all completed tasks + */ + public void testInvokeAll5() { + ExecutorService e = new ScheduledThreadPoolExecutor(2); + try { + ArrayList> l = new ArrayList>(); + l.add(new StringTask()); + l.add(new StringTask()); + List> result = e.invokeAll(l); + assertEquals(2, result.size()); + for (Iterator> it = result.iterator(); it.hasNext();) + assertSame(TEST_STRING, it.next().get()); + } catch (ExecutionException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + }