--- jsr166/src/test/tck/ScheduledExecutorTest.java 2017/03/28 18:13:10 1.89 +++ jsr166/src/test/tck/ScheduledExecutorTest.java 2017/05/29 19:15:03 1.93 @@ -73,7 +73,7 @@ public class ScheduledExecutorTest exten Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); assertSame(Boolean.TRUE, f.get()); assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); - assertTrue(done.await(0L, MILLISECONDS)); + assertEquals(0L, done.getCount()); } } @@ -241,8 +241,8 @@ public class ScheduledExecutorTest exten final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); try (PoolCleaner cleaner = cleaner(p)) { try { - TrackedCallable callable = null; - Future f = p.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); + Future f = p.schedule((Callable)null, + randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (NullPointerException success) {} } @@ -746,11 +746,13 @@ public class ScheduledExecutorTest exten * - setContinueExistingPeriodicTasksAfterShutdownPolicy */ public void testShutdown_cancellation() throws Exception { - final int poolSize = 6; + final int poolSize = 4; final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(poolSize); final BlockingQueue q = p.getQueue(); final ThreadLocalRandom rnd = ThreadLocalRandom.current(); + final long delay = rnd.nextInt(2); + final int rounds = rnd.nextInt(1, 3); final boolean effectiveDelayedPolicy; final boolean effectivePeriodicPolicy; final boolean effectiveRemovePolicy; @@ -779,29 +781,33 @@ public class ScheduledExecutorTest exten assertEquals(effectiveRemovePolicy, p.getRemoveOnCancelPolicy()); - // System.err.println("effectiveDelayedPolicy="+effectiveDelayedPolicy); - // System.err.println("effectivePeriodicPolicy="+effectivePeriodicPolicy); - // System.err.println("effectiveRemovePolicy="+effectiveRemovePolicy); + final boolean periodicTasksContinue = effectivePeriodicPolicy && rnd.nextBoolean(); // Strategy: Wedge the pool with one wave of "blocker" tasks, - // then add a second wave that waits in the queue. + // then add a second wave that waits in the queue until unblocked. final AtomicInteger ran = new AtomicInteger(0); final CountDownLatch poolBlocked = new CountDownLatch(poolSize); final CountDownLatch unblock = new CountDownLatch(1); + final RuntimeException exception = new RuntimeException(); - class Task extends CheckedRunnable { - public void realRun() throws InterruptedException { - ran.getAndIncrement(); - poolBlocked.countDown(); - await(unblock); + class Task implements Runnable { + public void run() { + try { + ran.getAndIncrement(); + poolBlocked.countDown(); + await(unblock); + } catch (Throwable fail) { threadUnexpectedException(fail); } } } class PeriodicTask extends Task { PeriodicTask(int rounds) { this.rounds = rounds; } int rounds; - public void realRun() throws InterruptedException { - if (--rounds == 0) super.realRun(); + public void run() { + if (--rounds == 0) super.run(); + // throw exception to surely terminate this periodic task, + // but in a separate execution and in a detectable way. + if (rounds == -1) throw exception; } } @@ -812,29 +818,25 @@ public class ScheduledExecutorTest exten List> periodics = new ArrayList<>(); immediates.add(p.submit(task)); - delayeds.add(p.schedule(task, 1, MILLISECONDS)); - for (int rounds : new int[] { 1, 2 }) { - periodics.add(p.scheduleAtFixedRate( - new PeriodicTask(rounds), 1, 1, MILLISECONDS)); - periodics.add(p.scheduleWithFixedDelay( - new PeriodicTask(rounds), 1, 1, MILLISECONDS)); - } + delayeds.add(p.schedule(task, delay, MILLISECONDS)); + periodics.add(p.scheduleAtFixedRate( + new PeriodicTask(rounds), delay, 1, MILLISECONDS)); + periodics.add(p.scheduleWithFixedDelay( + new PeriodicTask(rounds), delay, 1, MILLISECONDS)); await(poolBlocked); assertEquals(poolSize, ran.get()); + assertEquals(poolSize, p.getActiveCount()); assertTrue(q.isEmpty()); // Add second wave of tasks. immediates.add(p.submit(task)); - long delay_ms = effectiveDelayedPolicy ? 1 : LONG_DELAY_MS; - delayeds.add(p.schedule(task, delay_ms, MILLISECONDS)); - for (int rounds : new int[] { 1, 2 }) { - periodics.add(p.scheduleAtFixedRate( - new PeriodicTask(rounds), 1, 1, MILLISECONDS)); - periodics.add(p.scheduleWithFixedDelay( - new PeriodicTask(rounds), 1, 1, MILLISECONDS)); - } + delayeds.add(p.schedule(task, effectiveDelayedPolicy ? delay : LONG_DELAY_MS, MILLISECONDS)); + periodics.add(p.scheduleAtFixedRate( + new PeriodicTask(rounds), delay, 1, MILLISECONDS)); + periodics.add(p.scheduleWithFixedDelay( + new PeriodicTask(rounds), delay, 1, MILLISECONDS)); assertEquals(poolSize, q.size()); assertEquals(poolSize, ran.get()); @@ -864,7 +866,7 @@ public class ScheduledExecutorTest exten assertTrue(!effectiveDelayedPolicy ^ q.contains(delayeds.get(1))); assertTrue(!effectivePeriodicPolicy - ^ q.containsAll(periodics.subList(4, 8))); + ^ q.containsAll(periodics.subList(2, 4))); immediates.forEach(f -> assertFalse(f.isDone())); @@ -874,18 +876,18 @@ public class ScheduledExecutorTest exten else assertTrue(delayeds.get(1).isCancelled()); - if (testImplementationDetails) { - if (effectivePeriodicPolicy) - // TODO: ensure periodic tasks continue executing - periodics.forEach( - f -> { - assertFalse(f.isDone()); + if (effectivePeriodicPolicy) + periodics.forEach( + f -> { + assertFalse(f.isDone()); + if (!periodicTasksContinue) { assertTrue(f.cancel(false)); - }); - else { - periodics.subList(0, 4).forEach(f -> assertFalse(f.isDone())); - periodics.subList(4, 8).forEach(f -> assertTrue(f.isCancelled())); - } + assertTrue(f.isCancelled()); + } + }); + else { + periodics.subList(0, 2).forEach(f -> assertFalse(f.isDone())); + periodics.subList(2, 4).forEach(f -> assertTrue(f.isCancelled())); } unblock.countDown(); // Release all pool threads @@ -896,6 +898,9 @@ public class ScheduledExecutorTest exten assertTrue(q.isEmpty()); + Stream.of(immediates, delayeds, periodics).flatMap(c -> c.stream()) + .forEach(f -> assertTrue(f.isDone())); + for (Future f : immediates) assertNull(f.get()); assertNull(delayeds.get(0).get()); @@ -904,10 +909,22 @@ public class ScheduledExecutorTest exten else assertTrue(delayeds.get(1).isCancelled()); - periodics.forEach(f -> assertTrue(f.isDone())); - periodics.forEach(f -> assertTrue(f.isCancelled())); + if (periodicTasksContinue) + periodics.forEach( + f -> { + try { f.get(); } + catch (ExecutionException success) { + assertSame(exception, success.getCause()); + } + catch (Throwable fail) { threadUnexpectedException(fail); } + }); + else + periodics.forEach(f -> assertTrue(f.isCancelled())); - assertEquals(poolSize + 1 + (effectiveDelayedPolicy ? 1 : 0), ran.get()); + assertEquals(poolSize + 1 + + (effectiveDelayedPolicy ? 1 : 0) + + (periodicTasksContinue ? 2 : 0), + ran.get()); } /**