--- jsr166/src/test/tck/ScheduledExecutorTest.java 2011/05/06 11:22:07 1.39 +++ jsr166/src/test/tck/ScheduledExecutorTest.java 2011/05/29 07:01:17 1.45 @@ -20,7 +20,6 @@ public class ScheduledExecutorTest exten return new TestSuite(ScheduledExecutorTest.class); } - /** * execute successfully executes a runnable */ @@ -39,25 +38,23 @@ public class ScheduledExecutorTest exten } } - /** * delayed schedule of callable successfully executes after delay */ public void testSchedule1() throws Exception { ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - final long t0 = System.nanoTime(); - final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; + final long startTime = System.nanoTime(); final CountDownLatch done = new CountDownLatch(1); try { Callable task = new CheckedCallable() { public Boolean realCall() { done.countDown(); - assertTrue(System.nanoTime() - t0 >= timeoutNanos); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); return Boolean.TRUE; }}; - Future f = p.schedule(task, SHORT_DELAY_MS, MILLISECONDS); - assertEquals(Boolean.TRUE, f.get()); - assertTrue(System.nanoTime() - t0 >= timeoutNanos); + Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); + assertSame(Boolean.TRUE, f.get()); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); assertTrue(done.await(0L, MILLISECONDS)); } finally { joinPool(p); @@ -69,19 +66,18 @@ public class ScheduledExecutorTest exten */ public void testSchedule3() throws Exception { ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - final long t0 = System.nanoTime(); - final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; + final long startTime = System.nanoTime(); final CountDownLatch done = new CountDownLatch(1); try { Runnable task = new CheckedRunnable() { public void realRun() { done.countDown(); - assertTrue(System.nanoTime() - t0 >= timeoutNanos); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); }}; - Future f = p.schedule(task, SHORT_DELAY_MS, MILLISECONDS); - assertNull(f.get()); - assertTrue(System.nanoTime() - t0 >= timeoutNanos); - assertTrue(done.await(0L, MILLISECONDS)); + Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); + await(done); + assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); } finally { joinPool(p); } @@ -92,20 +88,19 @@ public class ScheduledExecutorTest exten */ public void testSchedule4() throws Exception { ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - final long t0 = System.nanoTime(); - final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; + final long startTime = System.nanoTime(); final CountDownLatch done = new CountDownLatch(1); try { Runnable task = new CheckedRunnable() { public void realRun() { done.countDown(); - assertTrue(System.nanoTime() - t0 >= timeoutNanos); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); }}; ScheduledFuture f = - p.scheduleAtFixedRate(task, SHORT_DELAY_MS, - SHORT_DELAY_MS, MILLISECONDS); - assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); - assertTrue(System.nanoTime() - t0 >= timeoutNanos); + p.scheduleAtFixedRate(task, timeoutMillis(), + LONG_DELAY_MS, MILLISECONDS); + await(done); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); f.cancel(true); } finally { joinPool(p); @@ -115,22 +110,21 @@ public class ScheduledExecutorTest exten /** * scheduleWithFixedDelay executes runnable after given initial delay */ - public void testSchedule5() throws InterruptedException { + public void testSchedule5() throws Exception { ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - final long t0 = System.nanoTime(); - final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; + final long startTime = System.nanoTime(); final CountDownLatch done = new CountDownLatch(1); try { Runnable task = new CheckedRunnable() { public void realRun() { done.countDown(); - assertTrue(System.nanoTime() - t0 >= timeoutNanos); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); }}; ScheduledFuture f = - p.scheduleWithFixedDelay(task, SHORT_DELAY_MS, - SHORT_DELAY_MS, MILLISECONDS); - assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); - assertTrue(System.nanoTime() - t0 >= timeoutNanos); + p.scheduleWithFixedDelay(task, timeoutMillis(), + LONG_DELAY_MS, MILLISECONDS); + await(done); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); f.cancel(true); } finally { joinPool(p); @@ -176,7 +170,6 @@ public class ScheduledExecutorTest exten joinPool(p); } - /** * execute(null) throws NPE */ @@ -327,12 +320,16 @@ public class ScheduledExecutorTest exten threadProceed.await(); threadDone.countDown(); }}); - assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); + await(threadStarted); assertEquals(0, p.getCompletedTaskCount()); threadProceed.countDown(); threadDone.await(); - delay(SHORT_DELAY_MS); - assertEquals(1, p.getCompletedTaskCount()); + long startTime = System.nanoTime(); + while (p.getCompletedTaskCount() != 1) { + if (millisElapsedSince(startTime) > LONG_DELAY_MS) + fail("timed out"); + Thread.yield(); + } } 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; - 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); @@ -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 { @@ -1160,20 +1155,14 @@ public class ScheduledExecutorTest exten l.add(new StringTask()); List> futures = e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); - assertEquals(3, futures.size()); - Iterator> it = futures.iterator(); - Future f1 = it.next(); - Future f2 = it.next(); - Future f3 = it.next(); - assertTrue(f1.isDone()); - assertTrue(f2.isDone()); - assertTrue(f3.isDone()); - assertFalse(f1.isCancelled()); - assertTrue(f2.isCancelled()); + assertEquals(l.size(), futures.size()); + for (Future future : futures) + assertTrue(future.isDone()); + assertFalse(futures.get(0).isCancelled()); + assertTrue(futures.get(1).isCancelled()); } finally { joinPool(e); } } - }