--- jsr166/src/test/tck/ScheduledExecutorSubclassTest.java 2015/10/05 21:54:33 1.49 +++ jsr166/src/test/tck/ScheduledExecutorSubclassTest.java 2017/03/25 21:41:10 1.65 @@ -5,6 +5,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; @@ -16,7 +17,6 @@ import java.util.concurrent.Cancellation import java.util.concurrent.CountDownLatch; import java.util.concurrent.Delayed; 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; @@ -28,7 +28,9 @@ import java.util.concurrent.ThreadFactor import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeUnit; +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; @@ -42,9 +44,9 @@ public class ScheduledExecutorSubclassTe } static class CustomTask implements RunnableScheduledFuture { - RunnableScheduledFuture task; + private final RunnableScheduledFuture task; volatile boolean ran; - CustomTask(RunnableScheduledFuture t) { task = t; } + CustomTask(RunnableScheduledFuture task) { this.task = task; } public boolean isPeriodic() { return task.isPeriodic(); } public void run() { ran = true; @@ -105,7 +107,7 @@ public class ScheduledExecutorSubclassTe final Runnable task = new CheckedRunnable() { public void realRun() { done.countDown(); }}; p.execute(task); - assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); + await(done); } } @@ -113,10 +115,10 @@ public class ScheduledExecutorSubclassTe * delayed schedule of callable successfully executes after delay */ public void testSchedule1() throws Exception { + final CountDownLatch done = new CountDownLatch(1); final CustomExecutor p = new CustomExecutor(1); - try (PoolCleaner cleaner = cleaner(p)) { + try (PoolCleaner cleaner = cleaner(p, done)) { final long startTime = System.nanoTime(); - final CountDownLatch done = new CountDownLatch(1); Callable task = new CheckedCallable() { public Boolean realCall() { done.countDown(); @@ -126,7 +128,6 @@ public class ScheduledExecutorSubclassTe Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); assertSame(Boolean.TRUE, f.get()); assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); - assertTrue(done.await(0L, MILLISECONDS)); } } @@ -200,54 +201,77 @@ public class ScheduledExecutorSubclassTe } /** - * 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 CustomExecutor p = new CustomExecutor(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); - done.await(); - 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 CustomExecutor p = new CustomExecutor(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); - done.await(); - 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"); } } @@ -363,20 +387,19 @@ public class ScheduledExecutorSubclassTe * thread becomes active */ public void testGetActiveCount() throws InterruptedException { + final CountDownLatch done = new CountDownLatch(1); final ThreadPoolExecutor p = new CustomExecutor(2); - try (PoolCleaner cleaner = cleaner(p)) { + try (PoolCleaner cleaner = cleaner(p, done)) { final CountDownLatch threadStarted = new CountDownLatch(1); - final CountDownLatch done = new CountDownLatch(1); assertEquals(0, p.getActiveCount()); p.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { threadStarted.countDown(); assertEquals(1, p.getActiveCount()); - done.await(); + await(done); }}); - assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); + await(threadStarted); assertEquals(1, p.getActiveCount()); - done.countDown(); } } @@ -395,13 +418,13 @@ public class ScheduledExecutorSubclassTe 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) @@ -427,21 +450,20 @@ public class ScheduledExecutorSubclassTe */ public void testGetLargestPoolSize() throws InterruptedException { final int THREADS = 3; + final CountDownLatch done = new CountDownLatch(1); final ThreadPoolExecutor p = new CustomExecutor(THREADS); - try (PoolCleaner cleaner = cleaner(p)) { + try (PoolCleaner cleaner = cleaner(p, done)) { final CountDownLatch threadsStarted = new CountDownLatch(THREADS); - final CountDownLatch done = new CountDownLatch(1); assertEquals(0, p.getLargestPoolSize()); for (int i = 0; i < THREADS; i++) p.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { threadsStarted.countDown(); - done.await(); + await(done); assertEquals(THREADS, p.getLargestPoolSize()); }}); - assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); + await(threadsStarted); assertEquals(THREADS, p.getLargestPoolSize()); - done.countDown(); } assertEquals(THREADS, p.getLargestPoolSize()); } @@ -451,20 +473,19 @@ public class ScheduledExecutorSubclassTe * become active */ public void testGetPoolSize() throws InterruptedException { + final CountDownLatch done = new CountDownLatch(1); final ThreadPoolExecutor p = new CustomExecutor(1); - try (PoolCleaner cleaner = cleaner(p)) { + try (PoolCleaner cleaner = cleaner(p, done)) { final CountDownLatch threadStarted = new CountDownLatch(1); - final CountDownLatch done = new CountDownLatch(1); assertEquals(0, p.getPoolSize()); p.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { threadStarted.countDown(); assertEquals(1, p.getPoolSize()); - done.await(); + await(done); }}); - assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); + await(threadStarted); assertEquals(1, p.getPoolSize()); - done.countDown(); } } @@ -483,9 +504,9 @@ public class ScheduledExecutorSubclassTe p.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { threadStarted.countDown(); - done.await(); + await(done); }}); - assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); + await(threadStarted); assertEquals(1, p.getTaskCount()); assertEquals(0, p.getCompletedTaskCount()); for (int i = 0; i < TASKS; i++) { @@ -494,7 +515,7 @@ public class ScheduledExecutorSubclassTe public void realRun() throws InterruptedException { threadStarted.countDown(); assertEquals(1 + TASKS, p.getTaskCount()); - done.await(); + await(done); }}); } assertEquals(1 + TASKS, p.getTaskCount()); @@ -556,18 +577,18 @@ public class ScheduledExecutorSubclassTe * isTerminated is false before termination, true after */ public void testIsTerminated() throws InterruptedException { + final CountDownLatch done = new CountDownLatch(1); final ThreadPoolExecutor p = new CustomExecutor(1); try (PoolCleaner cleaner = cleaner(p)) { final CountDownLatch threadStarted = new CountDownLatch(1); - final CountDownLatch done = new CountDownLatch(1); - assertFalse(p.isTerminated()); p.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { assertFalse(p.isTerminated()); threadStarted.countDown(); - done.await(); + await(done); }}); - assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); + await(threadStarted); + assertFalse(p.isTerminated()); assertFalse(p.isTerminating()); done.countDown(); try { p.shutdown(); } catch (SecurityException ok) { return; } @@ -580,18 +601,18 @@ public class ScheduledExecutorSubclassTe * isTerminating is not true when running or when terminated */ public void testIsTerminating() throws InterruptedException { + final CountDownLatch done = new CountDownLatch(1); final ThreadPoolExecutor p = new CustomExecutor(1); try (PoolCleaner cleaner = cleaner(p)) { final CountDownLatch threadStarted = new CountDownLatch(1); - final CountDownLatch done = new CountDownLatch(1); assertFalse(p.isTerminating()); p.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { assertFalse(p.isTerminating()); threadStarted.countDown(); - done.await(); + await(done); }}); - assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); + await(threadStarted); assertFalse(p.isTerminating()); done.countDown(); try { p.shutdown(); } catch (SecurityException ok) { return; } @@ -605,24 +626,23 @@ public class ScheduledExecutorSubclassTe * getQueue returns the work queue, which contains queued tasks */ public void testGetQueue() throws InterruptedException { + final CountDownLatch done = new CountDownLatch(1); final ScheduledThreadPoolExecutor p = new CustomExecutor(1); - try (PoolCleaner cleaner = cleaner(p)) { + try (PoolCleaner cleaner = cleaner(p, done)) { final CountDownLatch threadStarted = new CountDownLatch(1); - final CountDownLatch done = new CountDownLatch(1); ScheduledFuture[] tasks = new ScheduledFuture[5]; for (int i = 0; i < tasks.length; i++) { Runnable r = new CheckedRunnable() { public void realRun() throws InterruptedException { threadStarted.countDown(); - done.await(); + await(done); }}; tasks[i] = p.schedule(r, 1, MILLISECONDS); } - assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); + await(threadStarted); BlockingQueue q = p.getQueue(); assertTrue(q.contains(tasks[tasks.length - 1])); assertFalse(q.contains(tasks[0])); - done.countDown(); } } @@ -630,20 +650,20 @@ public class ScheduledExecutorSubclassTe * remove(task) removes queued task, and fails to remove active task */ public void testRemove() throws InterruptedException { + final CountDownLatch done = new CountDownLatch(1); final ScheduledThreadPoolExecutor p = new CustomExecutor(1); - try (PoolCleaner cleaner = cleaner(p)) { + try (PoolCleaner cleaner = cleaner(p, done)) { ScheduledFuture[] tasks = new ScheduledFuture[5]; final CountDownLatch threadStarted = new CountDownLatch(1); - final CountDownLatch done = new CountDownLatch(1); for (int i = 0; i < tasks.length; i++) { Runnable r = new CheckedRunnable() { public void realRun() throws InterruptedException { threadStarted.countDown(); - done.await(); + await(done); }}; tasks[i] = p.schedule(r, 1, MILLISECONDS); } - assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); + await(threadStarted); BlockingQueue q = p.getQueue(); assertFalse(p.remove((Runnable)tasks[0])); assertTrue(q.contains((Runnable)tasks[4])); @@ -654,7 +674,6 @@ public class ScheduledExecutorSubclassTe assertTrue(q.contains((Runnable)tasks[3])); assertTrue(p.remove((Runnable)tasks[3])); assertFalse(q.contains((Runnable)tasks[3])); - done.countDown(); } } @@ -706,7 +725,7 @@ public class ScheduledExecutorSubclassTe }}; for (int i = 0; i < count; i++) p.execute(waiter); - assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); + await(threadsStarted); assertEquals(poolSize, p.getActiveCount()); assertEquals(0, p.getCompletedTaskCount()); final List queuedTasks; @@ -795,7 +814,7 @@ public class ScheduledExecutorSubclassTe 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<>(); @@ -803,7 +822,7 @@ public class ScheduledExecutorSubclassTe 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)); @@ -839,8 +858,8 @@ public class ScheduledExecutorSubclassTe } } 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()); @@ -921,7 +940,7 @@ public class ScheduledExecutorSubclassTe final CountDownLatch latch = new CountDownLatch(1); final ExecutorService e = new CustomExecutor(2); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(latchAwaitingStringTask(latch)); l.add(null); try { @@ -938,7 +957,7 @@ public class ScheduledExecutorSubclassTe public void testInvokeAny4() throws Exception { final ExecutorService e = new CustomExecutor(2); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new NPETask()); try { e.invokeAny(l); @@ -955,7 +974,7 @@ public class ScheduledExecutorSubclassTe public void testInvokeAny5() throws Exception { final ExecutorService e = new CustomExecutor(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); @@ -993,7 +1012,7 @@ public class ScheduledExecutorSubclassTe public void testInvokeAll3() throws Exception { final ExecutorService e = new CustomExecutor(2); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new StringTask()); l.add(null); try { @@ -1009,7 +1028,7 @@ public class ScheduledExecutorSubclassTe public void testInvokeAll4() throws Exception { final ExecutorService e = new CustomExecutor(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()); @@ -1028,7 +1047,7 @@ public class ScheduledExecutorSubclassTe public void testInvokeAll5() throws Exception { final ExecutorService e = new CustomExecutor(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); @@ -1057,7 +1076,7 @@ public class ScheduledExecutorSubclassTe public void testTimedInvokeAnyNullTimeUnit() throws Exception { final ExecutorService e = new CustomExecutor(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); @@ -1086,7 +1105,7 @@ public class ScheduledExecutorSubclassTe CountDownLatch latch = new CountDownLatch(1); final ExecutorService e = new CustomExecutor(2); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(latchAwaitingStringTask(latch)); l.add(null); try { @@ -1103,14 +1122,16 @@ public class ScheduledExecutorSubclassTe public void testTimedInvokeAny4() throws Exception { final ExecutorService e = new CustomExecutor(2); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + long startTime = System.nanoTime(); + List> l = new ArrayList<>(); l.add(new NPETask()); try { - e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); + e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (ExecutionException success) { assertTrue(success.getCause() instanceof NullPointerException); } + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } } @@ -1120,11 +1141,13 @@ public class ScheduledExecutorSubclassTe public void testTimedInvokeAny5() throws Exception { final ExecutorService e = new CustomExecutor(2); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + long startTime = System.nanoTime(); + List> l = new ArrayList<>(); l.add(new StringTask()); l.add(new StringTask()); - String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); + String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); assertSame(TEST_STRING, result); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } } @@ -1147,7 +1170,7 @@ public class ScheduledExecutorSubclassTe public void testTimedInvokeAllNullTimeUnit() throws Exception { final ExecutorService e = new CustomExecutor(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); @@ -1173,7 +1196,7 @@ public class ScheduledExecutorSubclassTe public void testTimedInvokeAll3() throws Exception { final ExecutorService e = new CustomExecutor(2); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new StringTask()); l.add(null); try { @@ -1189,7 +1212,7 @@ public class ScheduledExecutorSubclassTe public void testTimedInvokeAll4() throws Exception { final ExecutorService e = new CustomExecutor(2); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new NPETask()); List> futures = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); @@ -1209,7 +1232,7 @@ public class ScheduledExecutorSubclassTe public void testTimedInvokeAll5() throws Exception { final ExecutorService e = new CustomExecutor(2); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new StringTask()); l.add(new StringTask()); List> futures = @@ -1224,16 +1247,22 @@ public class ScheduledExecutorSubclassTe * timed invokeAll(c) cancels tasks not completed by timeout */ public void testTimedInvokeAll6() throws Exception { - final ExecutorService e = new CustomExecutor(2); - try (PoolCleaner cleaner = cleaner(e)) { - for (long timeout = timeoutMillis();;) { + for (long timeout = timeoutMillis();;) { + final CountDownLatch done = new CountDownLatch(1); + final Callable waiter = new CheckedCallable() { + public String realCall() { + try { done.await(LONG_DELAY_MS, MILLISECONDS); } + catch (InterruptedException ok) {} + return "1"; }}; + final ExecutorService p = new CustomExecutor(2); + try (PoolCleaner cleaner = cleaner(p, done)) { List> tasks = new ArrayList<>(); tasks.add(new StringTask("0")); - tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); + tasks.add(waiter); tasks.add(new StringTask("2")); long startTime = System.nanoTime(); List> futures = - e.invokeAll(tasks, timeout, MILLISECONDS); + p.invokeAll(tasks, timeout, MILLISECONDS); assertEquals(tasks.size(), futures.size()); assertTrue(millisElapsedSince(startTime) >= timeout); for (Future future : futures)