--- jsr166/src/test/tck/ScheduledExecutorTest.java 2010/11/30 02:12:52 1.36 +++ jsr166/src/test/tck/ScheduledExecutorTest.java 2011/05/27 19:48:58 1.43 @@ -1,7 +1,7 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ * Other contributors include Andrew Wright, Jeffrey Hayes, * Pat Fisher, Mike Judd. */ @@ -20,7 +20,6 @@ public class ScheduledExecutorTest exten return new TestSuite(ScheduledExecutorTest.class); } - /** * execute successfully executes a runnable */ @@ -39,7 +38,6 @@ public class ScheduledExecutorTest exten } } - /** * delayed schedule of callable successfully executes after delay */ @@ -150,7 +148,7 @@ public class ScheduledExecutorTest exten RunnableCounter counter = new RunnableCounter(); ScheduledFuture h = p.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS); - Thread.sleep(SMALL_DELAY_MS); + delay(SMALL_DELAY_MS); h.cancel(true); int c = counter.count.get(); // By time scaling conventions, we must have at least @@ -168,7 +166,7 @@ public class ScheduledExecutorTest exten RunnableCounter counter = new RunnableCounter(); ScheduledFuture h = p.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS); - Thread.sleep(SMALL_DELAY_MS); + delay(SMALL_DELAY_MS); h.cancel(true); int c = counter.count.get(); assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); @@ -176,7 +174,6 @@ public class ScheduledExecutorTest exten joinPool(p); } - /** * execute(null) throws NPE */ @@ -240,9 +237,9 @@ public class ScheduledExecutorTest exten /** * schedule callable throws RejectedExecutionException if shutdown */ - public void testSchedule3_RejectedExecutionException() throws InterruptedException { - ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); - try { + public void testSchedule3_RejectedExecutionException() throws InterruptedException { + ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); + try { se.shutdown(); se.schedule(new NoOpCallable(), MEDIUM_DELAY_MS, MILLISECONDS); @@ -250,7 +247,7 @@ public class ScheduledExecutorTest exten } catch (RejectedExecutionException success) { } catch (SecurityException ok) { } - joinPool(se); + joinPool(se); } /** @@ -331,7 +328,7 @@ public class ScheduledExecutorTest exten assertEquals(0, p.getCompletedTaskCount()); threadProceed.countDown(); threadDone.await(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertEquals(1, p.getCompletedTaskCount()); } finally { joinPool(p); @@ -459,7 +456,7 @@ public class ScheduledExecutorTest exten } /** - * isShutDown is false before shutdown, true after + * isShutdown is false before shutdown, true after */ public void testIsShutdown() { @@ -473,7 +470,6 @@ public class ScheduledExecutorTest exten assertTrue(p.isShutdown()); } - /** * isTerminated is false before termination, true after */ @@ -587,29 +583,28 @@ public class ScheduledExecutorTest exten } /** - * purge removes cancelled tasks from the queue + * purge eventually removes cancelled tasks from the queue */ public void testPurge() throws InterruptedException { ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); ScheduledFuture[] tasks = new ScheduledFuture[5]; - for (int i = 0; i < tasks.length; i++) { - tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); - } + for (int i = 0; i < tasks.length; i++) + tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), + LONG_DELAY_MS, MILLISECONDS); try { int max = tasks.length; if (tasks[4].cancel(true)) --max; if (tasks[3].cancel(true)) --max; // There must eventually be an interference-free point at // which purge will not fail. (At worst, when queue is empty.) - int k; - for (k = 0; k < SMALL_DELAY_MS; ++k) { + long startTime = System.nanoTime(); + do { p.purge(); long count = p.getTaskCount(); - if (count >= 0 && count <= max) - break; - Thread.sleep(1); - } - assertTrue(k < SMALL_DELAY_MS); + if (count == max) + return; + } while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS); + fail("Purge failed to remove cancelled tasks"); } finally { for (ScheduledFuture task : tasks) task.cancel(true); @@ -618,28 +613,29 @@ public class ScheduledExecutorTest exten } /** - * shutDownNow returns a list containing tasks that were not run + * shutdownNow returns a list containing tasks that were not run */ - public void testShutDownNow() throws InterruptedException { + public void testShutdownNow() { ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); for (int i = 0; i < 5; i++) - p.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); - List l; + p.schedule(new SmallPossiblyInterruptedRunnable(), + LONG_DELAY_MS, MILLISECONDS); try { - l = p.shutdownNow(); + List l = p.shutdownNow(); + assertTrue(p.isShutdown()); + assertEquals(5, l.size()); } catch (SecurityException ok) { - return; + // Allowed in case test doesn't have privs + } finally { + joinPool(p); } - assertTrue(p.isShutdown()); - assertTrue(l.size() > 0 && l.size() <= 5); - joinPool(p); } /** * In default setting, shutdown cancels periodic but not delayed * tasks at shutdown */ - public void testShutDown1() throws InterruptedException { + public void testShutdown1() throws InterruptedException { ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); @@ -664,12 +660,11 @@ public class ScheduledExecutorTest exten } } - /** * If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, * delayed tasks are cancelled at shutdown */ - public void testShutDown2() throws InterruptedException { + public void testShutdown2() throws InterruptedException { ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); @@ -695,30 +690,30 @@ public class ScheduledExecutorTest exten * If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, * periodic tasks are cancelled at shutdown */ - public void testShutDown3() throws InterruptedException { + public void testShutdown3() throws InterruptedException { ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); + long initialDelay = LONG_DELAY_MS; ScheduledFuture task = - p.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, MILLISECONDS); + p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay, + 5, MILLISECONDS); try { p.shutdown(); } catch (SecurityException ok) { return; } assertTrue(p.isShutdown()); - BlockingQueue q = p.getQueue(); assertTrue(p.getQueue().isEmpty()); assertTrue(task.isDone()); assertTrue(task.isCancelled()); - assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); - assertTrue(p.isTerminated()); + joinPool(p); } /** * if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, * periodic tasks are not cancelled at shutdown */ - public void testShutDown4() throws InterruptedException { + public void testShutdown4() throws InterruptedException { ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); final CountDownLatch counter = new CountDownLatch(2); try { @@ -1175,5 +1170,4 @@ public class ScheduledExecutorTest exten } } - }