--- jsr166/src/test/tck/ScheduledExecutorSubclassTest.java 2011/05/29 07:01:17 1.27 +++ jsr166/src/test/tck/ScheduledExecutorSubclassTest.java 2013/09/24 18:35:21 1.31 @@ -8,7 +8,7 @@ import junit.framework.*; import java.util.*; import java.util.concurrent.*; import static java.util.concurrent.TimeUnit.MILLISECONDS; -import java.util.concurrent.atomic.*; +import java.util.concurrent.atomic.AtomicInteger; public class ScheduledExecutorSubclassTest extends JSR166TestCase { public static void main(String[] args) { @@ -36,12 +36,12 @@ public class ScheduledExecutorSubclassTe } public boolean isCancelled() { return task.isCancelled(); } public boolean isDone() { return task.isDone(); } - public V get() throws InterruptedException, ExecutionException { + public V get() throws InterruptedException, ExecutionException { V v = task.get(); assertTrue(ran); return v; } - public V get(long time, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { + public V get(long time, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { V v = task.get(time, unit); assertTrue(ran); return v; @@ -57,7 +57,7 @@ public class ScheduledExecutorSubclassTe protected RunnableScheduledFuture decorateTask(Callable c, RunnableScheduledFuture task) { return new CustomTask(task); } - CustomExecutor(int corePoolSize) { super(corePoolSize);} + CustomExecutor(int corePoolSize) { super(corePoolSize); } CustomExecutor(int corePoolSize, RejectedExecutionHandler handler) { super(corePoolSize, handler); } @@ -193,17 +193,28 @@ public class ScheduledExecutorSubclassTe */ public void testFixedRateSequence() throws InterruptedException { CustomExecutor p = new CustomExecutor(1); - RunnableCounter counter = new RunnableCounter(); - ScheduledFuture h = - p.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS); - delay(SMALL_DELAY_MS); - h.cancel(true); - int c = counter.count.get(); - // By time scaling conventions, we must have at least - // an execution per SHORT delay, but no more than one SHORT more - assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); - assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); - joinPool(p); + try { + for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { + long startTime = System.nanoTime(); + int cycles = 10; + final CountDownLatch done = new CountDownLatch(cycles); + CheckedRunnable task = new CheckedRunnable() { + public void realRun() { done.countDown(); }}; + + ScheduledFuture h = + p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); + done.await(); + h.cancel(true); + double normalizedTime = + (double) millisElapsedSince(startTime) / delay; + if (normalizedTime >= cycles - 1 && + normalizedTime <= cycles) + return; + } + throw new AssertionError("unexpected execution rate"); + } finally { + joinPool(p); + } } /** @@ -211,15 +222,28 @@ public class ScheduledExecutorSubclassTe */ public void testFixedDelaySequence() throws InterruptedException { CustomExecutor p = new CustomExecutor(1); - RunnableCounter counter = new RunnableCounter(); - ScheduledFuture h = - p.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS); - delay(SMALL_DELAY_MS); - h.cancel(true); - int c = counter.count.get(); - assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); - assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); - joinPool(p); + try { + for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { + long startTime = System.nanoTime(); + int cycles = 10; + final CountDownLatch done = new CountDownLatch(cycles); + CheckedRunnable task = new CheckedRunnable() { + public void realRun() { done.countDown(); }}; + + ScheduledFuture h = + p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); + done.await(); + h.cancel(true); + double normalizedTime = + (double) millisElapsedSince(startTime) / delay; + if (normalizedTime >= cycles - 1 && + normalizedTime <= cycles) + return; + } + throw new AssertionError("unexpected execution rate"); + } finally { + joinPool(p); + } } /**