--- jsr166/src/test/tck/ScheduledExecutorTest.java 2015/09/27 20:17:39 1.54 +++ jsr166/src/test/tck/ScheduledExecutorTest.java 2015/09/28 03:05:23 1.57 @@ -656,6 +656,44 @@ public class ScheduledExecutorTest exten * shutdownNow returns a list containing tasks that were not run, * and those tasks are drained from the queue */ + public void testShutdownNow() throws InterruptedException { + final int poolSize = 2; + final int count = 5; + final AtomicInteger ran = new AtomicInteger(0); + final ScheduledThreadPoolExecutor p = + new ScheduledThreadPoolExecutor(poolSize); + CountDownLatch threadsStarted = new CountDownLatch(poolSize); + CheckedRunnable waiter = new CheckedRunnable() { public void realRun() { + threadsStarted.countDown(); + try { + MILLISECONDS.sleep(2 * LONG_DELAY_MS); + } catch (InterruptedException success) {} + ran.getAndIncrement(); + }}; + for (int i = 0; i < count; i++) + p.execute(waiter); + assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); + assertEquals(poolSize, p.getActiveCount()); + assertEquals(0, p.getCompletedTaskCount()); + final List queuedTasks; + try { + queuedTasks = p.shutdownNow(); + } catch (SecurityException ok) { + return; // Allowed in case test doesn't have privs + } + assertTrue(p.isShutdown()); + assertTrue(p.getQueue().isEmpty()); + assertEquals(count - poolSize, queuedTasks.size()); + assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(p.isTerminated()); + assertEquals(poolSize, ran.get()); + assertEquals(poolSize, p.getCompletedTaskCount()); + } + + /** + * shutdownNow returns a list containing tasks that were not run, + * and those tasks are drained from the queue + */ public void testShutdownNow_delayedTasks() throws InterruptedException { ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); List tasks = new ArrayList<>(); @@ -665,7 +703,8 @@ public class ScheduledExecutorTest exten tasks.add(p.scheduleAtFixedRate(r, 9, 9, SECONDS)); tasks.add(p.scheduleWithFixedDelay(r, 9, 9, SECONDS)); } - assertEquals(new HashSet(tasks), new HashSet(p.getQueue())); + if (testImplementationDetails) + assertEquals(new HashSet(tasks), new HashSet(p.getQueue())); final List queuedTasks; try { queuedTasks = p.shutdownNow(); @@ -674,7 +713,8 @@ public class ScheduledExecutorTest exten } assertTrue(p.isShutdown()); assertTrue(p.getQueue().isEmpty()); - assertEquals(new HashSet(tasks), new HashSet(queuedTasks)); + if (testImplementationDetails) + assertEquals(new HashSet(tasks), new HashSet(queuedTasks)); assertEquals(tasks.size(), queuedTasks.size()); for (ScheduledFuture task : tasks) { assertFalse(task.isDone());