--- jsr166/src/test/tck/ScheduledExecutorTest.java 2015/10/08 03:08:37 1.76 +++ jsr166/src/test/tck/ScheduledExecutorTest.java 2017/03/25 23:35:15 1.87 @@ -7,6 +7,7 @@ */ import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.NANOSECONDS; import static java.util.concurrent.TimeUnit.SECONDS; import java.util.ArrayList; @@ -17,7 +18,6 @@ import java.util.concurrent.Callable; import java.util.concurrent.CancellationException; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; -import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; import java.util.concurrent.RejectedExecutionException; @@ -25,7 +25,9 @@ import java.util.concurrent.ScheduledFut import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; import junit.framework.Test; import junit.framework.TestSuite; @@ -48,7 +50,7 @@ public class ScheduledExecutorTest exten final Runnable task = new CheckedRunnable() { public void realRun() { done.countDown(); }}; p.execute(task); - assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); + await(done); } } @@ -143,54 +145,77 @@ public class ScheduledExecutorTest exten } /** - * scheduleAtFixedRate executes series of tasks at given rate + * scheduleAtFixedRate executes series of tasks at given rate. + * Eventually, it must hold that: + * cycles - 1 <= elapsedMillis/delay < cycles */ public void testFixedRateSequence() throws InterruptedException { final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); try (PoolCleaner cleaner = cleaner(p)) { for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { - long startTime = System.nanoTime(); - int cycles = 10; + final long startTime = System.nanoTime(); + final int cycles = 8; final CountDownLatch done = new CountDownLatch(cycles); - Runnable task = new CheckedRunnable() { + final Runnable task = new CheckedRunnable() { public void realRun() { done.countDown(); }}; - ScheduledFuture h = + final ScheduledFuture periodicTask = p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); - await(done); - h.cancel(true); - double normalizedTime = - (double) millisElapsedSince(startTime) / delay; - if (normalizedTime >= cycles - 1 && - normalizedTime <= cycles) + final int totalDelayMillis = (cycles - 1) * delay; + await(done, totalDelayMillis + LONG_DELAY_MS); + periodicTask.cancel(true); + final long elapsedMillis = millisElapsedSince(startTime); + assertTrue(elapsedMillis >= totalDelayMillis); + if (elapsedMillis <= cycles * delay) return; + // else retry with longer delay } - throw new AssertionError("unexpected execution rate"); + fail("unexpected execution rate"); } } /** - * scheduleWithFixedDelay executes series of tasks with given period + * scheduleWithFixedDelay executes series of tasks with given period. + * Eventually, it must hold that each task starts at least delay and at + * most 2 * delay after the termination of the previous task. */ public void testFixedDelaySequence() throws InterruptedException { final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); try (PoolCleaner cleaner = cleaner(p)) { for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { - long startTime = System.nanoTime(); - int cycles = 10; + final long startTime = System.nanoTime(); + final AtomicLong previous = new AtomicLong(startTime); + final AtomicBoolean tryLongerDelay = new AtomicBoolean(false); + final int cycles = 8; final CountDownLatch done = new CountDownLatch(cycles); - Runnable task = new CheckedRunnable() { - public void realRun() { done.countDown(); }}; - ScheduledFuture h = + final int d = delay; + final Runnable task = new CheckedRunnable() { + public void realRun() { + long now = System.nanoTime(); + long elapsedMillis + = NANOSECONDS.toMillis(now - previous.get()); + if (done.getCount() == cycles) { // first execution + if (elapsedMillis >= d) + tryLongerDelay.set(true); + } else { + assertTrue(elapsedMillis >= d); + if (elapsedMillis >= 2 * d) + tryLongerDelay.set(true); + } + previous.set(now); + done.countDown(); + }}; + final ScheduledFuture periodicTask = p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); - await(done); - h.cancel(true); - double normalizedTime = - (double) millisElapsedSince(startTime) / delay; - if (normalizedTime >= cycles - 1 && - normalizedTime <= cycles) + final int totalDelayMillis = (cycles - 1) * delay; + await(done, totalDelayMillis + cycles * LONG_DELAY_MS); + periodicTask.cancel(true); + final long elapsedMillis = millisElapsedSince(startTime); + assertTrue(elapsedMillis >= totalDelayMillis); + if (!tryLongerDelay.get()) return; + // else retry with longer delay } - throw new AssertionError("unexpected execution rate"); + fail("unexpected execution rate"); } } @@ -337,13 +362,13 @@ public class ScheduledExecutorTest exten public void realRun() throws InterruptedException { threadStarted.countDown(); assertEquals(0, p.getCompletedTaskCount()); - threadProceed.await(); + await(threadProceed); threadDone.countDown(); }}); await(threadStarted); assertEquals(0, p.getCompletedTaskCount()); threadProceed.countDown(); - threadDone.await(); + await(threadDone); long startTime = System.nanoTime(); while (p.getCompletedTaskCount() != 1) { if (millisElapsedSince(startTime) > LONG_DELAY_MS) @@ -482,18 +507,28 @@ public class ScheduledExecutorTest exten } /** + * The default rejected execution handler is AbortPolicy. + */ + public void testDefaultRejectedExecutionHandler() { + final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); + try (PoolCleaner cleaner = cleaner(p)) { + assertTrue(p.getRejectedExecutionHandler() + instanceof ThreadPoolExecutor.AbortPolicy); + } + } + + /** * isShutdown is false before shutdown, true after */ public void testIsShutdown() { - final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - try { - assertFalse(p.isShutdown()); - } - finally { - try { p.shutdown(); } catch (SecurityException ok) { return; } + assertFalse(p.isShutdown()); + try (PoolCleaner cleaner = cleaner(p)) { + try { + p.shutdown(); + assertTrue(p.isShutdown()); + } catch (SecurityException ok) {} } - assertTrue(p.isShutdown()); } /** @@ -709,19 +744,19 @@ public class ScheduledExecutorTest exten * - setContinueExistingPeriodicTasksAfterShutdownPolicy */ public void testShutdown_cancellation() throws Exception { - Boolean[] allBooleans = { null, Boolean.FALSE, Boolean.TRUE }; - for (Boolean policy : allBooleans) + final int UNSPECIFIED = 0, YES = 1, NO = 2; + for (int maybe : new int[] { UNSPECIFIED, YES, NO }) { final int poolSize = 2; final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(poolSize); - final boolean effectiveDelayedPolicy = (policy != Boolean.FALSE); - final boolean effectivePeriodicPolicy = (policy == Boolean.TRUE); - final boolean effectiveRemovePolicy = (policy == Boolean.TRUE); - if (policy != null) { - p.setExecuteExistingDelayedTasksAfterShutdownPolicy(policy); - p.setContinueExistingPeriodicTasksAfterShutdownPolicy(policy); - p.setRemoveOnCancelPolicy(policy); + final boolean effectiveDelayedPolicy = (maybe != NO); + final boolean effectivePeriodicPolicy = (maybe == YES); + final boolean effectiveRemovePolicy = (maybe == YES); + if (maybe != UNSPECIFIED) { + p.setExecuteExistingDelayedTasksAfterShutdownPolicy(maybe == YES); + p.setContinueExistingPeriodicTasksAfterShutdownPolicy(maybe == YES); + p.setRemoveOnCancelPolicy(maybe == YES); } assertEquals(effectiveDelayedPolicy, p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); @@ -738,7 +773,7 @@ public class ScheduledExecutorTest exten Runnable task = new CheckedRunnable() { public void realRun() throws InterruptedException { poolBlocked.countDown(); - assertTrue(unblock.await(LONG_DELAY_MS, MILLISECONDS)); + await(unblock); ran.getAndIncrement(); }}; List> blockers = new ArrayList<>(); @@ -746,12 +781,12 @@ public class ScheduledExecutorTest exten List> delayeds = new ArrayList<>(); for (int i = 0; i < poolSize; i++) blockers.add(p.submit(task)); - assertTrue(poolBlocked.await(LONG_DELAY_MS, MILLISECONDS)); + await(poolBlocked); - periodics.add(p.scheduleAtFixedRate(countDowner(periodicLatch1), - 1, 1, MILLISECONDS)); - periodics.add(p.scheduleWithFixedDelay(countDowner(periodicLatch2), - 1, 1, MILLISECONDS)); + periodics.add(p.scheduleAtFixedRate( + countDowner(periodicLatch1), 1, 1, MILLISECONDS)); + periodics.add(p.scheduleWithFixedDelay( + countDowner(periodicLatch2), 1, 1, MILLISECONDS)); delayeds.add(p.schedule(task, 1, MILLISECONDS)); assertTrue(p.getQueue().containsAll(periodics)); @@ -773,25 +808,31 @@ public class ScheduledExecutorTest exten assertEquals(effectiveDelayedPolicy, p.getQueue().containsAll(delayeds)); } - // Release all pool threads - unblock.countDown(); + unblock.countDown(); // Release all pool threads - for (Future delayed : delayeds) { - if (effectiveDelayedPolicy) { - assertNull(delayed.get()); - } - } + if (effectiveDelayedPolicy) + for (Future delayed : delayeds) assertNull(delayed.get()); if (effectivePeriodicPolicy) { - assertTrue(periodicLatch1.await(LONG_DELAY_MS, MILLISECONDS)); - assertTrue(periodicLatch2.await(LONG_DELAY_MS, MILLISECONDS)); + await(periodicLatch1); + await(periodicLatch2); for (Future periodic : periodics) { assertTrue(periodic.cancel(false)); assertTrue(periodic.isCancelled()); assertTrue(periodic.isDone()); } } + for (Future blocker : blockers) assertNull(blocker.get()); assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); assertTrue(p.isTerminated()); + + for (Future future : delayeds) { + assertTrue(effectiveDelayedPolicy ^ future.isCancelled()); + assertTrue(future.isDone()); + } + for (Future future : periodics) + assertTrue(future.isCancelled()); + for (Future future : blockers) + assertNull(future.get()); assertEquals(2 + (effectiveDelayedPolicy ? 1 : 0), ran.get()); }} @@ -864,7 +905,7 @@ public class ScheduledExecutorTest exten CountDownLatch latch = new CountDownLatch(1); final ExecutorService e = new ScheduledThreadPoolExecutor(2); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(latchAwaitingStringTask(latch)); l.add(null); try { @@ -881,7 +922,7 @@ public class ScheduledExecutorTest exten public void testInvokeAny4() throws Exception { final ExecutorService e = new ScheduledThreadPoolExecutor(2); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new NPETask()); try { e.invokeAny(l); @@ -898,7 +939,7 @@ public class ScheduledExecutorTest exten public void testInvokeAny5() throws Exception { final ExecutorService e = new ScheduledThreadPoolExecutor(2); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new StringTask()); l.add(new StringTask()); String result = e.invokeAny(l); @@ -936,7 +977,7 @@ public class ScheduledExecutorTest exten public void testInvokeAll3() throws Exception { final ExecutorService e = new ScheduledThreadPoolExecutor(2); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new StringTask()); l.add(null); try { @@ -952,7 +993,7 @@ public class ScheduledExecutorTest exten public void testInvokeAll4() throws Exception { final ExecutorService e = new ScheduledThreadPoolExecutor(2); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new NPETask()); List> futures = e.invokeAll(l); assertEquals(1, futures.size()); @@ -971,7 +1012,7 @@ public class ScheduledExecutorTest exten public void testInvokeAll5() throws Exception { final ExecutorService e = new ScheduledThreadPoolExecutor(2); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new StringTask()); l.add(new StringTask()); List> futures = e.invokeAll(l); @@ -1000,7 +1041,7 @@ public class ScheduledExecutorTest exten public void testTimedInvokeAnyNullTimeUnit() throws Exception { final ExecutorService e = new ScheduledThreadPoolExecutor(2); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new StringTask()); try { e.invokeAny(l, MEDIUM_DELAY_MS, null); @@ -1029,7 +1070,7 @@ public class ScheduledExecutorTest exten CountDownLatch latch = new CountDownLatch(1); final ExecutorService e = new ScheduledThreadPoolExecutor(2); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(latchAwaitingStringTask(latch)); l.add(null); try { @@ -1047,7 +1088,7 @@ public class ScheduledExecutorTest exten final ExecutorService e = new ScheduledThreadPoolExecutor(2); try (PoolCleaner cleaner = cleaner(e)) { long startTime = System.nanoTime(); - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new NPETask()); try { e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); @@ -1066,7 +1107,7 @@ public class ScheduledExecutorTest exten final ExecutorService e = new ScheduledThreadPoolExecutor(2); try (PoolCleaner cleaner = cleaner(e)) { long startTime = System.nanoTime(); - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new StringTask()); l.add(new StringTask()); String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); @@ -1094,7 +1135,7 @@ public class ScheduledExecutorTest exten public void testTimedInvokeAllNullTimeUnit() throws Exception { final ExecutorService e = new ScheduledThreadPoolExecutor(2); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new StringTask()); try { e.invokeAll(l, MEDIUM_DELAY_MS, null); @@ -1121,7 +1162,7 @@ public class ScheduledExecutorTest exten public void testTimedInvokeAll3() throws Exception { final ExecutorService e = new ScheduledThreadPoolExecutor(2); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new StringTask()); l.add(null); try { @@ -1137,7 +1178,7 @@ public class ScheduledExecutorTest exten public void testTimedInvokeAll4() throws Exception { final ExecutorService e = new ScheduledThreadPoolExecutor(2); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new NPETask()); List> futures = e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); @@ -1157,7 +1198,7 @@ public class ScheduledExecutorTest exten public void testTimedInvokeAll5() throws Exception { final ExecutorService e = new ScheduledThreadPoolExecutor(2); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new StringTask()); l.add(new StringTask()); List> futures = @@ -1206,4 +1247,27 @@ public class ScheduledExecutorTest exten } } + /** + * A fixed delay task with overflowing period should not prevent a + * one-shot task from executing. + * https://bugs.openjdk.java.net/browse/JDK-8051859 + */ + public void testScheduleWithFixedDelay_overflow() throws Exception { + final CountDownLatch delayedDone = new CountDownLatch(1); + final CountDownLatch immediateDone = new CountDownLatch(1); + final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); + try (PoolCleaner cleaner = cleaner(p)) { + final Runnable immediate = new Runnable() { public void run() { + immediateDone.countDown(); + }}; + final Runnable delayed = new Runnable() { public void run() { + delayedDone.countDown(); + p.submit(immediate); + }}; + p.scheduleWithFixedDelay(delayed, 0L, Long.MAX_VALUE, SECONDS); + await(delayedDone); + await(immediateDone); + } + } + }