--- jsr166/src/test/tck/ScheduledExecutorTest.java 2015/10/04 18:28:51 1.63 +++ jsr166/src/test/tck/ScheduledExecutorTest.java 2015/10/05 22:38:38 1.68 @@ -48,7 +48,7 @@ public class ScheduledExecutorTest exten final Runnable task = new CheckedRunnable() { public void realRun() { done.countDown(); }}; p.execute(task); - assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); + assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); } } @@ -317,7 +317,7 @@ public class ScheduledExecutorTest exten assertEquals(1, p.getActiveCount()); done.await(); }}); - assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); + assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); assertEquals(1, p.getActiveCount()); done.countDown(); } @@ -382,7 +382,7 @@ public class ScheduledExecutorTest exten done.await(); assertEquals(THREADS, p.getLargestPoolSize()); }}); - assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); + assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); assertEquals(THREADS, p.getLargestPoolSize()); done.countDown(); } @@ -405,7 +405,7 @@ public class ScheduledExecutorTest exten assertEquals(1, p.getPoolSize()); done.await(); }}); - assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); + assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); assertEquals(1, p.getPoolSize()); done.countDown(); } @@ -416,22 +416,35 @@ public class ScheduledExecutorTest exten * submitted */ public void testGetTaskCount() throws InterruptedException { + final int TASKS = 3; + final CountDownLatch done = new CountDownLatch(1); final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - try (PoolCleaner cleaner = cleaner(p)) { + try (PoolCleaner cleaner = cleaner(p, done)) { final CountDownLatch threadStarted = new CountDownLatch(1); - final CountDownLatch done = new CountDownLatch(1); - final int TASKS = 5; assertEquals(0, p.getTaskCount()); - for (int i = 0; i < TASKS; i++) + assertEquals(0, p.getCompletedTaskCount()); + p.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadStarted.countDown(); + done.await(); + }}); + assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); + assertEquals(1, p.getTaskCount()); + assertEquals(0, p.getCompletedTaskCount()); + for (int i = 0; i < TASKS; i++) { + assertEquals(1 + i, p.getTaskCount()); p.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { threadStarted.countDown(); + assertEquals(1 + TASKS, p.getTaskCount()); done.await(); }}); - assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); - assertEquals(TASKS, p.getTaskCount()); - done.countDown(); + } + assertEquals(1 + TASKS, p.getTaskCount()); + assertEquals(0, p.getCompletedTaskCount()); } + assertEquals(1 + TASKS, p.getTaskCount()); + assertEquals(1 + TASKS, p.getCompletedTaskCount()); } /** @@ -501,7 +514,7 @@ public class ScheduledExecutorTest exten threadStarted.countDown(); done.await(); }}); - assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); + assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); assertFalse(p.isTerminating()); done.countDown(); try { p.shutdown(); } catch (SecurityException ok) { return; } @@ -525,7 +538,7 @@ public class ScheduledExecutorTest exten threadStarted.countDown(); done.await(); }}); - assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); + assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); assertFalse(p.isTerminating()); done.countDown(); try { p.shutdown(); } catch (SecurityException ok) { return; } @@ -552,7 +565,7 @@ public class ScheduledExecutorTest exten }}; tasks[i] = p.schedule(r, 1, MILLISECONDS); } - assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); + assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); BlockingQueue q = p.getQueue(); assertTrue(q.contains(tasks[tasks.length - 1])); assertFalse(q.contains(tasks[0])); @@ -577,7 +590,7 @@ public class ScheduledExecutorTest exten }}; tasks[i] = p.schedule(r, 1, MILLISECONDS); } - assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); + assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); BlockingQueue q = p.getQueue(); assertFalse(p.remove((Runnable)tasks[0])); assertTrue(q.contains((Runnable)tasks[4])); @@ -596,12 +609,15 @@ public class ScheduledExecutorTest exten * purge eventually removes cancelled tasks from the queue */ public void testPurge() throws InterruptedException { - ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - ScheduledFuture[] tasks = new ScheduledFuture[5]; - for (int i = 0; i < tasks.length; i++) - tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), - LONG_DELAY_MS, MILLISECONDS); - try { + final ScheduledFuture[] tasks = new ScheduledFuture[5]; + final Runnable releaser = new Runnable() { public void run() { + for (ScheduledFuture task : tasks) + if (task != null) task.cancel(true); }}; + final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); + try (PoolCleaner cleaner = cleaner(p, releaser)) { + for (int i = 0; i < tasks.length; i++) + tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), + LONG_DELAY_MS, MILLISECONDS); int max = tasks.length; if (tasks[4].cancel(true)) --max; if (tasks[3].cancel(true)) --max; @@ -613,12 +629,8 @@ public class ScheduledExecutorTest exten long count = p.getTaskCount(); if (count == max) return; - } while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS); + } while (millisElapsedSince(startTime) < LONG_DELAY_MS); fail("Purge failed to remove cancelled tasks"); - } finally { - for (ScheduledFuture task : tasks) - task.cancel(true); - joinPool(p); } } @@ -632,7 +644,7 @@ public class ScheduledExecutorTest exten final AtomicInteger ran = new AtomicInteger(0); final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(poolSize); - CountDownLatch threadsStarted = new CountDownLatch(poolSize); + final CountDownLatch threadsStarted = new CountDownLatch(poolSize); Runnable waiter = new CheckedRunnable() { public void realRun() { threadsStarted.countDown(); try {