--- jsr166/src/test/tck/ScheduledExecutorTest.java 2017/01/04 06:09:58 1.83 +++ jsr166/src/test/tck/ScheduledExecutorTest.java 2017/03/25 23:35:15 1.87 @@ -50,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); } } @@ -362,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) @@ -507,6 +507,17 @@ 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() { @@ -733,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()); @@ -762,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<>(); @@ -770,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)); @@ -797,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()); }}