--- jsr166/src/test/tck/ScheduledExecutorSubclassTest.java 2011/05/06 11:22:07 1.21 +++ jsr166/src/test/tck/ScheduledExecutorSubclassTest.java 2011/05/27 19:43:27 1.25 @@ -48,7 +48,6 @@ public class ScheduledExecutorSubclassTe } } - public class CustomExecutor extends ScheduledThreadPoolExecutor { protected RunnableScheduledFuture decorateTask(Runnable r, RunnableScheduledFuture task) { @@ -73,7 +72,6 @@ public class ScheduledExecutorSubclassTe } - /** * execute successfully executes a runnable */ @@ -92,7 +90,6 @@ public class ScheduledExecutorSubclassTe } } - /** * delayed schedule of callable successfully executes after delay */ @@ -229,7 +226,6 @@ public class ScheduledExecutorSubclassTe joinPool(p); } - /** * execute(null) throws NPE */ @@ -510,7 +506,7 @@ public class ScheduledExecutorSubclassTe } /** - * isShutDown is false before shutdown, true after + * isShutdown is false before shutdown, true after */ public void testIsShutdown() { CustomExecutor p = new CustomExecutor(1); @@ -523,7 +519,6 @@ public class ScheduledExecutorSubclassTe assertTrue(p.isShutdown()); } - /** * isTerminated is false before termination, true after */ @@ -642,24 +637,23 @@ public class ScheduledExecutorSubclassTe public void testPurge() throws InterruptedException { CustomExecutor p = new CustomExecutor(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; - delay(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); @@ -668,28 +662,29 @@ public class ScheduledExecutorSubclassTe } /** - * shutDownNow returns a list containing tasks that were not run + * shutdownNow returns a list containing tasks that were not run */ - public void testShutDownNow() { + public void testShutdownNow() { CustomExecutor p = new CustomExecutor(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 { CustomExecutor p = new CustomExecutor(1); assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); @@ -714,12 +709,11 @@ public class ScheduledExecutorSubclassTe } } - /** * If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, * delayed tasks are cancelled at shutdown */ - public void testShutDown2() throws InterruptedException { + public void testShutdown2() throws InterruptedException { CustomExecutor p = new CustomExecutor(1); p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); @@ -741,35 +735,34 @@ public class ScheduledExecutorSubclassTe } } - /** * If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, * periodic tasks are cancelled at shutdown */ - public void testShutDown3() throws InterruptedException { + public void testShutdown3() throws InterruptedException { CustomExecutor p = new CustomExecutor(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 { CustomExecutor p = new CustomExecutor(1); final CountDownLatch counter = new CountDownLatch(2); try {