ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ScheduledExecutorTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ScheduledExecutorTest.java (file contents):
Revision 1.22 by jsr166, Mon Nov 2 20:28:32 2009 UTC vs.
Revision 1.96 by jsr166, Mon Jan 8 02:04:15 2018 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
8  
9 < import junit.framework.*;
10 < import java.util.*;
11 < import java.util.concurrent.*;
12 < import java.util.concurrent.atomic.*;
9 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 > import static java.util.concurrent.TimeUnit.NANOSECONDS;
11 > import static java.util.concurrent.TimeUnit.SECONDS;
12 >
13 > import java.util.ArrayList;
14 > import java.util.Collection;
15 > import java.util.Collections;
16 > import java.util.HashSet;
17 > import java.util.List;
18 > import java.util.concurrent.BlockingQueue;
19 > import java.util.concurrent.Callable;
20 > import java.util.concurrent.CancellationException;
21 > import java.util.concurrent.CountDownLatch;
22 > import java.util.concurrent.ExecutionException;
23 > import java.util.concurrent.ExecutorService;
24 > import java.util.concurrent.Future;
25 > import java.util.concurrent.RejectedExecutionException;
26 > import java.util.concurrent.ScheduledFuture;
27 > import java.util.concurrent.ScheduledThreadPoolExecutor;
28 > import java.util.concurrent.ThreadFactory;
29 > import java.util.concurrent.ThreadLocalRandom;
30 > import java.util.concurrent.ThreadPoolExecutor;
31 > import java.util.concurrent.atomic.AtomicBoolean;
32 > import java.util.concurrent.atomic.AtomicInteger;
33 > import java.util.concurrent.atomic.AtomicLong;
34 > import java.util.stream.Stream;
35 >
36 > import junit.framework.Test;
37 > import junit.framework.TestSuite;
38  
39   public class ScheduledExecutorTest extends JSR166TestCase {
40      public static void main(String[] args) {
41 <        junit.textui.TestRunner.run (suite());
41 >        main(suite(), args);
42      }
43      public static Test suite() {
44 <        return new TestSuite(ScheduledExecutorTest.class);
44 >        return new TestSuite(ScheduledExecutorTest.class);
45      }
46  
22
47      /**
48       * execute successfully executes a runnable
49       */
50 <    public void testExecute() {
51 <        try {
52 <            TrackedShortRunnable runnable =new TrackedShortRunnable();
53 <            ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
54 <            p1.execute(runnable);
55 <            assertFalse(runnable.done);
56 <            Thread.sleep(SHORT_DELAY_MS);
57 <            try { p1.shutdown(); } catch(SecurityException ok) { return; }
34 <            try {
35 <                Thread.sleep(MEDIUM_DELAY_MS);
36 <            } catch(InterruptedException e){
37 <                unexpectedException();
38 <            }
39 <            assertTrue(runnable.done);
40 <            try { p1.shutdown(); } catch(SecurityException ok) { return; }
41 <            joinPool(p1);
50 >    public void testExecute() throws InterruptedException {
51 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
52 >        try (PoolCleaner cleaner = cleaner(p)) {
53 >            final CountDownLatch done = new CountDownLatch(1);
54 >            final Runnable task = new CheckedRunnable() {
55 >                public void realRun() { done.countDown(); }};
56 >            p.execute(task);
57 >            await(done);
58          }
43        catch(Exception e){
44            unexpectedException();
45        }
46
59      }
60  
49
61      /**
62       * delayed schedule of callable successfully executes after delay
63       */
64 <    public void testSchedule1() {
65 <        try {
66 <            TrackedCallable callable = new TrackedCallable();
67 <            ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
68 <            Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
69 <            assertFalse(callable.done);
70 <            Thread.sleep(MEDIUM_DELAY_MS);
71 <            assertTrue(callable.done);
72 <            assertEquals(Boolean.TRUE, f.get());
73 <            try { p1.shutdown(); } catch(SecurityException ok) { return; }
74 <            joinPool(p1);
75 <        } catch(RejectedExecutionException e){}
76 <        catch(Exception e){
77 <            e.printStackTrace();
78 <            unexpectedException();
64 >    public void testSchedule1() throws Exception {
65 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
66 >        try (PoolCleaner cleaner = cleaner(p)) {
67 >            final long startTime = System.nanoTime();
68 >            final CountDownLatch done = new CountDownLatch(1);
69 >            Callable task = new CheckedCallable<Boolean>() {
70 >                public Boolean realCall() {
71 >                    done.countDown();
72 >                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
73 >                    return Boolean.TRUE;
74 >                }};
75 >            Future f = p.schedule(task, timeoutMillis(), MILLISECONDS);
76 >            assertSame(Boolean.TRUE, f.get());
77 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
78 >            assertEquals(0L, done.getCount());
79          }
80      }
81  
82      /**
83 <     *  delayed schedule of runnable successfully executes after delay
84 <     */
85 <    public void testSchedule3() {
86 <        try {
87 <            TrackedShortRunnable runnable = new TrackedShortRunnable();
88 <            ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
89 <            p1.schedule(runnable, SMALL_DELAY_MS, TimeUnit.MILLISECONDS);
90 <            Thread.sleep(SHORT_DELAY_MS);
91 <            assertFalse(runnable.done);
92 <            Thread.sleep(MEDIUM_DELAY_MS);
93 <            assertTrue(runnable.done);
94 <            try { p1.shutdown(); } catch(SecurityException ok) { return; }
95 <            joinPool(p1);
96 <        } catch(Exception e){
97 <            unexpectedException();
83 >     * delayed schedule of runnable successfully executes after delay
84 >     */
85 >    public void testSchedule3() throws Exception {
86 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
87 >        try (PoolCleaner cleaner = cleaner(p)) {
88 >            final long startTime = System.nanoTime();
89 >            final CountDownLatch done = new CountDownLatch(1);
90 >            Runnable task = new CheckedRunnable() {
91 >                public void realRun() {
92 >                    done.countDown();
93 >                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
94 >                }};
95 >            Future f = p.schedule(task, timeoutMillis(), MILLISECONDS);
96 >            await(done);
97 >            assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
98 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
99          }
100      }
101  
102      /**
103       * scheduleAtFixedRate executes runnable after given initial delay
104       */
105 <    public void testSchedule4() {
106 <        try {
107 <            TrackedShortRunnable runnable = new TrackedShortRunnable();
108 <            ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
109 <            ScheduledFuture h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
110 <            assertFalse(runnable.done);
111 <            Thread.sleep(MEDIUM_DELAY_MS);
112 <            assertTrue(runnable.done);
113 <            h.cancel(true);
114 <            joinPool(p1);
115 <        } catch(Exception e){
116 <            unexpectedException();
105 >    public void testSchedule4() throws Exception {
106 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
107 >        try (PoolCleaner cleaner = cleaner(p)) {
108 >            final long startTime = System.nanoTime();
109 >            final CountDownLatch done = new CountDownLatch(1);
110 >            Runnable task = new CheckedRunnable() {
111 >                public void realRun() {
112 >                    done.countDown();
113 >                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
114 >                }};
115 >            ScheduledFuture f =
116 >                p.scheduleAtFixedRate(task, timeoutMillis(),
117 >                                      LONG_DELAY_MS, MILLISECONDS);
118 >            await(done);
119 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
120 >            f.cancel(true);
121          }
122      }
123  
108    static class RunnableCounter implements Runnable {
109        AtomicInteger count = new AtomicInteger(0);
110        public void run() { count.getAndIncrement(); }
111    }
112
124      /**
125       * scheduleWithFixedDelay executes runnable after given initial delay
126       */
127 <    public void testSchedule5() {
128 <        try {
129 <            TrackedShortRunnable runnable = new TrackedShortRunnable();
130 <            ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
131 <            ScheduledFuture h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
132 <            assertFalse(runnable.done);
133 <            Thread.sleep(MEDIUM_DELAY_MS);
134 <            assertTrue(runnable.done);
135 <            h.cancel(true);
136 <            joinPool(p1);
137 <        } catch(Exception e){
138 <            unexpectedException();
139 <        }
140 <    }
141 <
142 <    /**
132 <     * scheduleAtFixedRate executes series of tasks at given rate
133 <     */
134 <    public void testFixedRateSequence() {
135 <        try {
136 <            ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
137 <            RunnableCounter counter = new RunnableCounter();
138 <            ScheduledFuture h =
139 <                p1.scheduleAtFixedRate(counter, 0, 1, TimeUnit.MILLISECONDS);
140 <            Thread.sleep(SMALL_DELAY_MS);
141 <            h.cancel(true);
142 <            int c = counter.count.get();
143 <            // By time scaling conventions, we must have at least
144 <            // an execution per SHORT delay, but no more than one SHORT more
145 <            assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
146 <            assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
147 <            joinPool(p1);
148 <        } catch(Exception e){
149 <            unexpectedException();
127 >    public void testSchedule5() throws Exception {
128 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
129 >        try (PoolCleaner cleaner = cleaner(p)) {
130 >            final long startTime = System.nanoTime();
131 >            final CountDownLatch done = new CountDownLatch(1);
132 >            Runnable task = new CheckedRunnable() {
133 >                public void realRun() {
134 >                    done.countDown();
135 >                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
136 >                }};
137 >            ScheduledFuture f =
138 >                p.scheduleWithFixedDelay(task, timeoutMillis(),
139 >                                         LONG_DELAY_MS, MILLISECONDS);
140 >            await(done);
141 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
142 >            f.cancel(true);
143          }
144      }
145  
146 <    /**
147 <     * scheduleWithFixedDelay executes series of tasks with given period
148 <     */
156 <    public void testFixedDelaySequence() {
157 <        try {
158 <            ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
159 <            RunnableCounter counter = new RunnableCounter();
160 <            ScheduledFuture h =
161 <                p1.scheduleWithFixedDelay(counter, 0, 1, TimeUnit.MILLISECONDS);
162 <            Thread.sleep(SMALL_DELAY_MS);
163 <            h.cancel(true);
164 <            int c = counter.count.get();
165 <            assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
166 <            assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
167 <            joinPool(p1);
168 <        } catch(Exception e){
169 <            unexpectedException();
170 <        }
146 >    static class RunnableCounter implements Runnable {
147 >        AtomicInteger count = new AtomicInteger(0);
148 >        public void run() { count.getAndIncrement(); }
149      }
150  
173
151      /**
152 <     *  execute (null) throws NPE
153 <     */
154 <    public void testExecuteNull() {
155 <        ScheduledThreadPoolExecutor se = null;
156 <        try {
157 <            se = new ScheduledThreadPoolExecutor(1);
158 <            se.execute(null);
159 <            shouldThrow();
160 <        } catch(NullPointerException success){}
161 <        catch(Exception e){
162 <            unexpectedException();
152 >     * scheduleAtFixedRate executes series of tasks at given rate.
153 >     * Eventually, it must hold that:
154 >     *   cycles - 1 <= elapsedMillis/delay < cycles
155 >     */
156 >    public void testFixedRateSequence() throws InterruptedException {
157 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
158 >        try (PoolCleaner cleaner = cleaner(p)) {
159 >            for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
160 >                final long startTime = System.nanoTime();
161 >                final int cycles = 8;
162 >                final CountDownLatch done = new CountDownLatch(cycles);
163 >                final Runnable task = new CheckedRunnable() {
164 >                    public void realRun() { done.countDown(); }};
165 >                final ScheduledFuture periodicTask =
166 >                    p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS);
167 >                final int totalDelayMillis = (cycles - 1) * delay;
168 >                await(done, totalDelayMillis + LONG_DELAY_MS);
169 >                periodicTask.cancel(true);
170 >                final long elapsedMillis = millisElapsedSince(startTime);
171 >                assertTrue(elapsedMillis >= totalDelayMillis);
172 >                if (elapsedMillis <= cycles * delay)
173 >                    return;
174 >                // else retry with longer delay
175 >            }
176 >            fail("unexpected execution rate");
177          }
187
188        joinPool(se);
178      }
179  
180      /**
181 <     * schedule (null) throws NPE
182 <     */
183 <    public void testScheduleNull() {
184 <        ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
185 <        try {
186 <            TrackedCallable callable = null;
187 <            Future f = se.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
188 <            shouldThrow();
189 <        } catch(NullPointerException success){}
190 <        catch(Exception e){
191 <            unexpectedException();
181 >     * scheduleWithFixedDelay executes series of tasks with given period.
182 >     * Eventually, it must hold that each task starts at least delay and at
183 >     * most 2 * delay after the termination of the previous task.
184 >     */
185 >    public void testFixedDelaySequence() throws InterruptedException {
186 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
187 >        try (PoolCleaner cleaner = cleaner(p)) {
188 >            for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
189 >                final long startTime = System.nanoTime();
190 >                final AtomicLong previous = new AtomicLong(startTime);
191 >                final AtomicBoolean tryLongerDelay = new AtomicBoolean(false);
192 >                final int cycles = 8;
193 >                final CountDownLatch done = new CountDownLatch(cycles);
194 >                final int d = delay;
195 >                final Runnable task = new CheckedRunnable() {
196 >                    public void realRun() {
197 >                        long now = System.nanoTime();
198 >                        long elapsedMillis
199 >                            = NANOSECONDS.toMillis(now - previous.get());
200 >                        if (done.getCount() == cycles) { // first execution
201 >                            if (elapsedMillis >= d)
202 >                                tryLongerDelay.set(true);
203 >                        } else {
204 >                            assertTrue(elapsedMillis >= d);
205 >                            if (elapsedMillis >= 2 * d)
206 >                                tryLongerDelay.set(true);
207 >                        }
208 >                        previous.set(now);
209 >                        done.countDown();
210 >                    }};
211 >                final ScheduledFuture periodicTask =
212 >                    p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS);
213 >                final int totalDelayMillis = (cycles - 1) * delay;
214 >                await(done, totalDelayMillis + cycles * LONG_DELAY_MS);
215 >                periodicTask.cancel(true);
216 >                final long elapsedMillis = millisElapsedSince(startTime);
217 >                assertTrue(elapsedMillis >= totalDelayMillis);
218 >                if (!tryLongerDelay.get())
219 >                    return;
220 >                // else retry with longer delay
221 >            }
222 >            fail("unexpected execution rate");
223          }
204        joinPool(se);
224      }
225  
226      /**
227 <     * execute throws RejectedExecutionException if shutdown
227 >     * Submitting null tasks throws NullPointerException
228       */
229 <    public void testSchedule1_RejectedExecutionException() {
230 <        ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
231 <        try {
232 <            se.shutdown();
214 <            se.schedule(new NoOpRunnable(),
215 <                        MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
216 <            shouldThrow();
217 <        } catch(RejectedExecutionException success){
218 <        } catch (SecurityException ok) {
229 >    public void testNullTaskSubmission() {
230 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
231 >        try (PoolCleaner cleaner = cleaner(p)) {
232 >            assertNullTaskSubmissionThrowsNullPointerException(p);
233          }
220
221        joinPool(se);
222
234      }
235  
236      /**
237 <     * schedule throws RejectedExecutionException if shutdown
238 <     */
239 <    public void testSchedule2_RejectedExecutionException() {
240 <        ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
241 <        try {
242 <            se.shutdown();
243 <            se.schedule(new NoOpCallable(),
244 <                        MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
245 <            shouldThrow();
246 <        } catch(RejectedExecutionException success){
247 <        } catch (SecurityException ok) {
248 <        }
249 <        joinPool(se);
250 <    }
237 >     * Submitted tasks are rejected when shutdown
238 >     */
239 >    public void testSubmittedTasksRejectedWhenShutdown() throws InterruptedException {
240 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
241 >        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
242 >        final CountDownLatch threadsStarted = new CountDownLatch(p.getCorePoolSize());
243 >        final CountDownLatch done = new CountDownLatch(1);
244 >        final Runnable r = () -> {
245 >            threadsStarted.countDown();
246 >            for (;;) {
247 >                try {
248 >                    done.await();
249 >                    return;
250 >                } catch (InterruptedException shutdownNowDeliberatelyIgnored) {}
251 >            }};
252 >        final Callable<Boolean> c = () -> {
253 >            threadsStarted.countDown();
254 >            for (;;) {
255 >                try {
256 >                    done.await();
257 >                    return Boolean.TRUE;
258 >                } catch (InterruptedException shutdownNowDeliberatelyIgnored) {}
259 >            }};
260 >
261 >        try (PoolCleaner cleaner = cleaner(p, done)) {
262 >            for (int i = p.getCorePoolSize(); i--> 0; ) {
263 >                switch (rnd.nextInt(4)) {
264 >                case 0: p.execute(r); break;
265 >                case 1: assertFalse(p.submit(r).isDone()); break;
266 >                case 2: assertFalse(p.submit(r, Boolean.TRUE).isDone()); break;
267 >                case 3: assertFalse(p.submit(c).isDone()); break;
268 >                }
269 >            }
270  
271 <    /**
272 <     * schedule callable throws RejectedExecutionException if shutdown
243 <     */
244 <     public void testSchedule3_RejectedExecutionException() {
245 <         ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
246 <         try {
247 <            se.shutdown();
248 <            se.schedule(new NoOpCallable(),
249 <                        MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
250 <            shouldThrow();
251 <        } catch(RejectedExecutionException success){
252 <        } catch (SecurityException ok) {
253 <        }
254 <         joinPool(se);
255 <    }
271 >            // ScheduledThreadPoolExecutor has an unbounded queue, so never saturated.
272 >            await(threadsStarted);
273  
274 <    /**
275 <     *  scheduleAtFixedRate throws RejectedExecutionException if shutdown
276 <     */
277 <    public void testScheduleAtFixedRate1_RejectedExecutionException() {
278 <        ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
279 <        try {
280 <            se.shutdown();
281 <            se.scheduleAtFixedRate(new NoOpRunnable(),
282 <                                   MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
283 <            shouldThrow();
284 <        } catch(RejectedExecutionException success){
285 <        } catch (SecurityException ok) {
274 >            if (rnd.nextBoolean())
275 >                p.shutdownNow();
276 >            else
277 >                p.shutdown();
278 >            // Pool is shutdown, but not yet terminated
279 >            assertTaskSubmissionsAreRejected(p);
280 >            assertFalse(p.isTerminated());
281 >
282 >            done.countDown();   // release blocking tasks
283 >            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
284 >
285 >            assertTaskSubmissionsAreRejected(p);
286 >        }
287 >        assertEquals(p.getCorePoolSize(), p.getCompletedTaskCount());
288 >    }
289 >
290 >    /**
291 >     * getActiveCount increases but doesn't overestimate, when a
292 >     * thread becomes active
293 >     */
294 >    public void testGetActiveCount() throws InterruptedException {
295 >        final CountDownLatch done = new CountDownLatch(1);
296 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2);
297 >        try (PoolCleaner cleaner = cleaner(p, done)) {
298 >            final CountDownLatch threadStarted = new CountDownLatch(1);
299 >            assertEquals(0, p.getActiveCount());
300 >            p.execute(new CheckedRunnable() {
301 >                public void realRun() throws InterruptedException {
302 >                    threadStarted.countDown();
303 >                    assertEquals(1, p.getActiveCount());
304 >                    await(done);
305 >                }});
306 >            await(threadStarted);
307 >            assertEquals(1, p.getActiveCount());
308 >        }
309 >    }
310 >
311 >    /**
312 >     * getCompletedTaskCount increases, but doesn't overestimate,
313 >     * when tasks complete
314 >     */
315 >    public void testGetCompletedTaskCount() throws InterruptedException {
316 >        final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2);
317 >        try (PoolCleaner cleaner = cleaner(p)) {
318 >            final CountDownLatch threadStarted = new CountDownLatch(1);
319 >            final CountDownLatch threadProceed = new CountDownLatch(1);
320 >            final CountDownLatch threadDone = new CountDownLatch(1);
321 >            assertEquals(0, p.getCompletedTaskCount());
322 >            p.execute(new CheckedRunnable() {
323 >                public void realRun() throws InterruptedException {
324 >                    threadStarted.countDown();
325 >                    assertEquals(0, p.getCompletedTaskCount());
326 >                    await(threadProceed);
327 >                    threadDone.countDown();
328 >                }});
329 >            await(threadStarted);
330 >            assertEquals(0, p.getCompletedTaskCount());
331 >            threadProceed.countDown();
332 >            await(threadDone);
333 >            long startTime = System.nanoTime();
334 >            while (p.getCompletedTaskCount() != 1) {
335 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
336 >                    fail("timed out");
337 >                Thread.yield();
338 >            }
339          }
270        joinPool(se);
340      }
341  
342      /**
343 <     * scheduleWithFixedDelay throws RejectedExecutionException if shutdown
343 >     * getCorePoolSize returns size given in constructor if not otherwise set
344       */
345 <    public void testScheduleWithFixedDelay1_RejectedExecutionException() {
346 <        ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
347 <        try {
348 <            se.shutdown();
349 <            se.scheduleWithFixedDelay(new NoOpRunnable(),
350 <                                      MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
351 <            shouldThrow();
352 <        } catch(RejectedExecutionException success){
353 <        } catch (SecurityException ok) {
345 >    public void testGetCorePoolSize() throws InterruptedException {
346 >        ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
347 >        try (PoolCleaner cleaner = cleaner(p)) {
348 >            assertEquals(1, p.getCorePoolSize());
349 >        }
350 >    }
351 >
352 >    /**
353 >     * getLargestPoolSize increases, but doesn't overestimate, when
354 >     * multiple threads active
355 >     */
356 >    public void testGetLargestPoolSize() throws InterruptedException {
357 >        final int THREADS = 3;
358 >        final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(THREADS);
359 >        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
360 >        final CountDownLatch done = new CountDownLatch(1);
361 >        try (PoolCleaner cleaner = cleaner(p, done)) {
362 >            assertEquals(0, p.getLargestPoolSize());
363 >            for (int i = 0; i < THREADS; i++)
364 >                p.execute(new CheckedRunnable() {
365 >                    public void realRun() throws InterruptedException {
366 >                        threadsStarted.countDown();
367 >                        await(done);
368 >                        assertEquals(THREADS, p.getLargestPoolSize());
369 >                    }});
370 >            await(threadsStarted);
371 >            assertEquals(THREADS, p.getLargestPoolSize());
372 >        }
373 >        assertEquals(THREADS, p.getLargestPoolSize());
374 >    }
375 >
376 >    /**
377 >     * getPoolSize increases, but doesn't overestimate, when threads
378 >     * become active
379 >     */
380 >    public void testGetPoolSize() throws InterruptedException {
381 >        final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
382 >        final CountDownLatch threadStarted = new CountDownLatch(1);
383 >        final CountDownLatch done = new CountDownLatch(1);
384 >        try (PoolCleaner cleaner = cleaner(p, done)) {
385 >            assertEquals(0, p.getPoolSize());
386 >            p.execute(new CheckedRunnable() {
387 >                public void realRun() throws InterruptedException {
388 >                    threadStarted.countDown();
389 >                    assertEquals(1, p.getPoolSize());
390 >                    await(done);
391 >                }});
392 >            await(threadStarted);
393 >            assertEquals(1, p.getPoolSize());
394 >        }
395 >    }
396 >
397 >    /**
398 >     * getTaskCount increases, but doesn't overestimate, when tasks
399 >     * submitted
400 >     */
401 >    public void testGetTaskCount() throws InterruptedException {
402 >        final int TASKS = 3;
403 >        final CountDownLatch done = new CountDownLatch(1);
404 >        final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
405 >        try (PoolCleaner cleaner = cleaner(p, done)) {
406 >            final CountDownLatch threadStarted = new CountDownLatch(1);
407 >            assertEquals(0, p.getTaskCount());
408 >            assertEquals(0, p.getCompletedTaskCount());
409 >            p.execute(new CheckedRunnable() {
410 >                public void realRun() throws InterruptedException {
411 >                    threadStarted.countDown();
412 >                    await(done);
413 >                }});
414 >            await(threadStarted);
415 >            assertEquals(1, p.getTaskCount());
416 >            assertEquals(0, p.getCompletedTaskCount());
417 >            for (int i = 0; i < TASKS; i++) {
418 >                assertEquals(1 + i, p.getTaskCount());
419 >                p.execute(new CheckedRunnable() {
420 >                    public void realRun() throws InterruptedException {
421 >                        threadStarted.countDown();
422 >                        assertEquals(1 + TASKS, p.getTaskCount());
423 >                        await(done);
424 >                    }});
425 >            }
426 >            assertEquals(1 + TASKS, p.getTaskCount());
427 >            assertEquals(0, p.getCompletedTaskCount());
428          }
429 <        joinPool(se);
429 >        assertEquals(1 + TASKS, p.getTaskCount());
430 >        assertEquals(1 + TASKS, p.getCompletedTaskCount());
431      }
432  
433      /**
434 <     *  getActiveCount increases but doesn't overestimate, when a
291 <     *  thread becomes active
434 >     * getThreadFactory returns factory in constructor if not set
435       */
436 <    public void testGetActiveCount() {
437 <        ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2);
438 <        assertEquals(0, p2.getActiveCount());
439 <        p2.execute(new SmallRunnable());
440 <        try {
441 <            Thread.sleep(SHORT_DELAY_MS);
299 <        } catch(Exception e){
300 <            unexpectedException();
436 >    public void testGetThreadFactory() throws InterruptedException {
437 >        final ThreadFactory threadFactory = new SimpleThreadFactory();
438 >        final ScheduledThreadPoolExecutor p =
439 >            new ScheduledThreadPoolExecutor(1, threadFactory);
440 >        try (PoolCleaner cleaner = cleaner(p)) {
441 >            assertSame(threadFactory, p.getThreadFactory());
442          }
302        assertEquals(1, p2.getActiveCount());
303        joinPool(p2);
443      }
444  
445      /**
446 <     *    getCompletedTaskCount increases, but doesn't overestimate,
308 <     *   when tasks complete
309 <     */
310 <    public void testGetCompletedTaskCount() {
311 <        ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2);
312 <        assertEquals(0, p2.getCompletedTaskCount());
313 <        p2.execute(new SmallRunnable());
314 <        try {
315 <            Thread.sleep(MEDIUM_DELAY_MS);
316 <        } catch(Exception e){
317 <            unexpectedException();
318 <        }
319 <        assertEquals(1, p2.getCompletedTaskCount());
320 <        joinPool(p2);
321 <    }
322 <
323 <    /**
324 <     *  getCorePoolSize returns size given in constructor if not otherwise set
325 <     */
326 <    public void testGetCorePoolSize() {
327 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
328 <        assertEquals(1, p1.getCorePoolSize());
329 <        joinPool(p1);
330 <    }
331 <
332 <    /**
333 <     *    getLargestPoolSize increases, but doesn't overestimate, when
334 <     *   multiple threads active
446 >     * setThreadFactory sets the thread factory returned by getThreadFactory
447       */
448 <    public void testGetLargestPoolSize() {
449 <        ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2);
450 <        assertEquals(0, p2.getLargestPoolSize());
451 <        p2.execute(new SmallRunnable());
452 <        p2.execute(new SmallRunnable());
453 <        try {
342 <            Thread.sleep(SHORT_DELAY_MS);
343 <        } catch(Exception e){
344 <            unexpectedException();
448 >    public void testSetThreadFactory() throws InterruptedException {
449 >        ThreadFactory threadFactory = new SimpleThreadFactory();
450 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
451 >        try (PoolCleaner cleaner = cleaner(p)) {
452 >            p.setThreadFactory(threadFactory);
453 >            assertSame(threadFactory, p.getThreadFactory());
454          }
346        assertEquals(2, p2.getLargestPoolSize());
347        joinPool(p2);
348    }
349
350    /**
351     *   getPoolSize increases, but doesn't overestimate, when threads
352     *   become active
353     */
354    public void testGetPoolSize() {
355        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
356        assertEquals(0, p1.getPoolSize());
357        p1.execute(new SmallRunnable());
358        assertEquals(1, p1.getPoolSize());
359        joinPool(p1);
455      }
456  
457      /**
458 <     *    getTaskCount increases, but doesn't overestimate, when tasks
364 <     *    submitted
458 >     * setThreadFactory(null) throws NPE
459       */
460 <    public void testGetTaskCount() {
461 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
462 <        assertEquals(0, p1.getTaskCount());
463 <        for(int i = 0; i < 5; i++)
464 <            p1.execute(new SmallRunnable());
465 <        try {
466 <            Thread.sleep(SHORT_DELAY_MS);
373 <        } catch(Exception e){
374 <            unexpectedException();
460 >    public void testSetThreadFactoryNull() throws InterruptedException {
461 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
462 >        try (PoolCleaner cleaner = cleaner(p)) {
463 >            try {
464 >                p.setThreadFactory(null);
465 >                shouldThrow();
466 >            } catch (NullPointerException success) {}
467          }
376        assertEquals(5, p1.getTaskCount());
377        joinPool(p1);
378    }
379
380    /**
381     * getThreadFactory returns factory in constructor if not set
382     */
383    public void testGetThreadFactory() {
384        ThreadFactory tf = new SimpleThreadFactory();
385        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, tf);
386        assertSame(tf, p.getThreadFactory());
387        joinPool(p);
468      }
469  
470      /**
471 <     * setThreadFactory sets the thread factory returned by getThreadFactory
471 >     * The default rejected execution handler is AbortPolicy.
472       */
473 <    public void testSetThreadFactory() {
474 <        ThreadFactory tf = new SimpleThreadFactory();
475 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
476 <        p.setThreadFactory(tf);
477 <        assertSame(tf, p.getThreadFactory());
398 <        joinPool(p);
399 <    }
400 <
401 <    /**
402 <     * setThreadFactory(null) throws NPE
403 <     */
404 <    public void testSetThreadFactoryNull() {
405 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
406 <        try {
407 <            p.setThreadFactory(null);
408 <            shouldThrow();
409 <        } catch (NullPointerException success) {
410 <        } finally {
411 <            joinPool(p);
473 >    public void testDefaultRejectedExecutionHandler() {
474 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
475 >        try (PoolCleaner cleaner = cleaner(p)) {
476 >            assertTrue(p.getRejectedExecutionHandler()
477 >                       instanceof ThreadPoolExecutor.AbortPolicy);
478          }
479      }
480  
481      /**
482 <     *   is isShutDown is false before shutdown, true after
482 >     * isShutdown is false before shutdown, true after
483       */
484      public void testIsShutdown() {
485 <
486 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
487 <        try {
488 <            assertFalse(p1.isShutdown());
489 <        }
490 <        finally {
491 <            try { p1.shutdown(); } catch(SecurityException ok) { return; }
485 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
486 >        assertFalse(p.isShutdown());
487 >        try (PoolCleaner cleaner = cleaner(p)) {
488 >            try {
489 >                p.shutdown();
490 >                assertTrue(p.isShutdown());
491 >            } catch (SecurityException ok) {}
492          }
427        assertTrue(p1.isShutdown());
493      }
494  
430
495      /**
496 <     *   isTerminated is false before termination, true after
497 <     */
498 <    public void testIsTerminated() {
499 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
500 <        try {
501 <            p1.execute(new SmallRunnable());
502 <        } finally {
503 <            try { p1.shutdown(); } catch(SecurityException ok) { return; }
504 <        }
505 <        try {
506 <            assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
507 <            assertTrue(p1.isTerminated());
508 <        } catch(Exception e){
509 <            unexpectedException();
496 >     * isTerminated is false before termination, true after
497 >     */
498 >    public void testIsTerminated() throws InterruptedException {
499 >        final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
500 >        try (PoolCleaner cleaner = cleaner(p)) {
501 >            final CountDownLatch threadStarted = new CountDownLatch(1);
502 >            final CountDownLatch done = new CountDownLatch(1);
503 >            assertFalse(p.isTerminated());
504 >            p.execute(new CheckedRunnable() {
505 >                public void realRun() throws InterruptedException {
506 >                    assertFalse(p.isTerminated());
507 >                    threadStarted.countDown();
508 >                    await(done);
509 >                }});
510 >            await(threadStarted);
511 >            assertFalse(p.isTerminating());
512 >            done.countDown();
513 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
514 >            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
515 >            assertTrue(p.isTerminated());
516          }
517      }
518  
519      /**
520 <     *  isTerminating is not true when running or when terminated
521 <     */
522 <    public void testIsTerminating() {
523 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
524 <        assertFalse(p1.isTerminating());
525 <        try {
526 <            p1.execute(new SmallRunnable());
527 <            assertFalse(p1.isTerminating());
528 <        } finally {
529 <            try { p1.shutdown(); } catch(SecurityException ok) { return; }
530 <        }
531 <        try {
532 <            assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
533 <            assertTrue(p1.isTerminated());
534 <            assertFalse(p1.isTerminating());
535 <        } catch(Exception e){
536 <            unexpectedException();
520 >     * isTerminating is not true when running or when terminated
521 >     */
522 >    public void testIsTerminating() throws InterruptedException {
523 >        final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
524 >        final CountDownLatch threadStarted = new CountDownLatch(1);
525 >        final CountDownLatch done = new CountDownLatch(1);
526 >        try (PoolCleaner cleaner = cleaner(p)) {
527 >            assertFalse(p.isTerminating());
528 >            p.execute(new CheckedRunnable() {
529 >                public void realRun() throws InterruptedException {
530 >                    assertFalse(p.isTerminating());
531 >                    threadStarted.countDown();
532 >                    await(done);
533 >                }});
534 >            await(threadStarted);
535 >            assertFalse(p.isTerminating());
536 >            done.countDown();
537 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
538 >            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
539 >            assertTrue(p.isTerminated());
540 >            assertFalse(p.isTerminating());
541          }
542      }
543  
544      /**
545       * getQueue returns the work queue, which contains queued tasks
546       */
547 <    public void testGetQueue() {
548 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
549 <        ScheduledFuture[] tasks = new ScheduledFuture[5];
550 <        for(int i = 0; i < 5; i++){
551 <            tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS);
552 <        }
553 <        try {
554 <            Thread.sleep(SHORT_DELAY_MS);
555 <            BlockingQueue<Runnable> q = p1.getQueue();
556 <            assertTrue(q.contains(tasks[4]));
547 >    public void testGetQueue() throws InterruptedException {
548 >        final CountDownLatch done = new CountDownLatch(1);
549 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
550 >        try (PoolCleaner cleaner = cleaner(p, done)) {
551 >            final CountDownLatch threadStarted = new CountDownLatch(1);
552 >            ScheduledFuture[] tasks = new ScheduledFuture[5];
553 >            for (int i = 0; i < tasks.length; i++) {
554 >                Runnable r = new CheckedRunnable() {
555 >                    public void realRun() throws InterruptedException {
556 >                        threadStarted.countDown();
557 >                        await(done);
558 >                    }};
559 >                tasks[i] = p.schedule(r, 1, MILLISECONDS);
560 >            }
561 >            await(threadStarted);
562 >            BlockingQueue<Runnable> q = p.getQueue();
563 >            assertTrue(q.contains(tasks[tasks.length - 1]));
564              assertFalse(q.contains(tasks[0]));
484        } catch(Exception e) {
485            unexpectedException();
486        } finally {
487            joinPool(p1);
565          }
566      }
567  
568      /**
569       * remove(task) removes queued task, and fails to remove active task
570       */
571 <    public void testRemove() {
572 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
573 <        ScheduledFuture[] tasks = new ScheduledFuture[5];
574 <        for(int i = 0; i < 5; i++){
575 <            tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS);
576 <        }
577 <        try {
578 <            Thread.sleep(SHORT_DELAY_MS);
579 <            BlockingQueue<Runnable> q = p1.getQueue();
580 <            assertFalse(p1.remove((Runnable)tasks[0]));
571 >    public void testRemove() throws InterruptedException {
572 >        final CountDownLatch done = new CountDownLatch(1);
573 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
574 >        try (PoolCleaner cleaner = cleaner(p, done)) {
575 >            ScheduledFuture[] tasks = new ScheduledFuture[5];
576 >            final CountDownLatch threadStarted = new CountDownLatch(1);
577 >            for (int i = 0; i < tasks.length; i++) {
578 >                Runnable r = new CheckedRunnable() {
579 >                    public void realRun() throws InterruptedException {
580 >                        threadStarted.countDown();
581 >                        await(done);
582 >                    }};
583 >                tasks[i] = p.schedule(r, 1, MILLISECONDS);
584 >            }
585 >            await(threadStarted);
586 >            BlockingQueue<Runnable> q = p.getQueue();
587 >            assertFalse(p.remove((Runnable)tasks[0]));
588              assertTrue(q.contains((Runnable)tasks[4]));
589              assertTrue(q.contains((Runnable)tasks[3]));
590 <            assertTrue(p1.remove((Runnable)tasks[4]));
591 <            assertFalse(p1.remove((Runnable)tasks[4]));
590 >            assertTrue(p.remove((Runnable)tasks[4]));
591 >            assertFalse(p.remove((Runnable)tasks[4]));
592              assertFalse(q.contains((Runnable)tasks[4]));
593              assertTrue(q.contains((Runnable)tasks[3]));
594 <            assertTrue(p1.remove((Runnable)tasks[3]));
594 >            assertTrue(p.remove((Runnable)tasks[3]));
595              assertFalse(q.contains((Runnable)tasks[3]));
512        } catch(Exception e) {
513            unexpectedException();
514        } finally {
515            joinPool(p1);
596          }
597      }
598  
599      /**
600 <     *  purge removes cancelled tasks from the queue
600 >     * purge eventually removes cancelled tasks from the queue
601       */
602 <    public void testPurge() {
603 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
604 <        ScheduledFuture[] tasks = new ScheduledFuture[5];
605 <        for(int i = 0; i < 5; i++){
606 <            tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
607 <        }
608 <        try {
609 <            int max = 5;
602 >    public void testPurge() throws InterruptedException {
603 >        final ScheduledFuture[] tasks = new ScheduledFuture[5];
604 >        final Runnable releaser = new Runnable() { public void run() {
605 >            for (ScheduledFuture task : tasks)
606 >                if (task != null) task.cancel(true); }};
607 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
608 >        try (PoolCleaner cleaner = cleaner(p, releaser)) {
609 >            for (int i = 0; i < tasks.length; i++)
610 >                tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(),
611 >                                      LONG_DELAY_MS, MILLISECONDS);
612 >            int max = tasks.length;
613              if (tasks[4].cancel(true)) --max;
614              if (tasks[3].cancel(true)) --max;
615              // There must eventually be an interference-free point at
616              // which purge will not fail. (At worst, when queue is empty.)
617 <            int k;
618 <            for (k = 0; k < SMALL_DELAY_MS; ++k) {
619 <                p1.purge();
620 <                long count = p1.getTaskCount();
621 <                if (count >= 0 && count <= max)
622 <                    break;
623 <                Thread.sleep(1);
624 <            }
542 <            assertTrue(k < SMALL_DELAY_MS);
543 <        } catch(Exception e) {
544 <            unexpectedException();
545 <        } finally {
546 <            joinPool(p1);
617 >            long startTime = System.nanoTime();
618 >            do {
619 >                p.purge();
620 >                long count = p.getTaskCount();
621 >                if (count == max)
622 >                    return;
623 >            } while (millisElapsedSince(startTime) < LONG_DELAY_MS);
624 >            fail("Purge failed to remove cancelled tasks");
625          }
626      }
627  
628      /**
629 <     *  shutDownNow returns a list containing tasks that were not run
630 <     */
631 <    public void testShutDownNow() {
632 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
633 <        for(int i = 0; i < 5; i++)
634 <            p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
635 <        List l;
629 >     * shutdownNow returns a list containing tasks that were not run,
630 >     * and those tasks are drained from the queue
631 >     */
632 >    public void testShutdownNow() throws InterruptedException {
633 >        final int poolSize = 2;
634 >        final int count = 5;
635 >        final AtomicInteger ran = new AtomicInteger(0);
636 >        final ScheduledThreadPoolExecutor p =
637 >            new ScheduledThreadPoolExecutor(poolSize);
638 >        final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
639 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
640 >            threadsStarted.countDown();
641 >            try {
642 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
643 >            } catch (InterruptedException success) {}
644 >            ran.getAndIncrement();
645 >        }};
646 >        for (int i = 0; i < count; i++)
647 >            p.execute(waiter);
648 >        await(threadsStarted);
649 >        assertEquals(poolSize, p.getActiveCount());
650 >        assertEquals(0, p.getCompletedTaskCount());
651 >        final List<Runnable> queuedTasks;
652          try {
653 <            l = p1.shutdownNow();
653 >            queuedTasks = p.shutdownNow();
654          } catch (SecurityException ok) {
655 <            return;
655 >            return; // Allowed in case test doesn't have privs
656          }
657 <        assertTrue(p1.isShutdown());
658 <        assertTrue(l.size() > 0 && l.size() <= 5);
659 <        joinPool(p1);
657 >        assertTrue(p.isShutdown());
658 >        assertTrue(p.getQueue().isEmpty());
659 >        assertEquals(count - poolSize, queuedTasks.size());
660 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
661 >        assertTrue(p.isTerminated());
662 >        assertEquals(poolSize, ran.get());
663 >        assertEquals(poolSize, p.getCompletedTaskCount());
664      }
665  
666      /**
667 <     * In default setting, shutdown cancels periodic but not delayed
668 <     * tasks at shutdown
669 <     */
670 <    public void testShutDown1() {
667 >     * shutdownNow returns a list containing tasks that were not run,
668 >     * and those tasks are drained from the queue
669 >     */
670 >    public void testShutdownNow_delayedTasks() throws InterruptedException {
671 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
672 >        List<ScheduledFuture> tasks = new ArrayList<>();
673 >        for (int i = 0; i < 3; i++) {
674 >            Runnable r = new NoOpRunnable();
675 >            tasks.add(p.schedule(r, 9, SECONDS));
676 >            tasks.add(p.scheduleAtFixedRate(r, 9, 9, SECONDS));
677 >            tasks.add(p.scheduleWithFixedDelay(r, 9, 9, SECONDS));
678 >        }
679 >        if (testImplementationDetails)
680 >            assertEquals(new HashSet(tasks), new HashSet(p.getQueue()));
681 >        final List<Runnable> queuedTasks;
682          try {
683 <            ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
684 <            assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy());
685 <            assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy());
577 <
578 <            ScheduledFuture[] tasks = new ScheduledFuture[5];
579 <            for(int i = 0; i < 5; i++)
580 <                tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
581 <            try { p1.shutdown(); } catch(SecurityException ok) { return; }
582 <            BlockingQueue q = p1.getQueue();
583 <            for (Iterator it = q.iterator(); it.hasNext();) {
584 <                ScheduledFuture t = (ScheduledFuture)it.next();
585 <                assertFalse(t.isCancelled());
586 <            }
587 <            assertTrue(p1.isShutdown());
588 <            Thread.sleep(SMALL_DELAY_MS);
589 <            for (int i = 0; i < 5; ++i) {
590 <                assertTrue(tasks[i].isDone());
591 <                assertFalse(tasks[i].isCancelled());
592 <            }
593 <
683 >            queuedTasks = p.shutdownNow();
684 >        } catch (SecurityException ok) {
685 >            return; // Allowed in case test doesn't have privs
686          }
687 <        catch(Exception ex) {
688 <            unexpectedException();
687 >        assertTrue(p.isShutdown());
688 >        assertTrue(p.getQueue().isEmpty());
689 >        if (testImplementationDetails)
690 >            assertEquals(new HashSet(tasks), new HashSet(queuedTasks));
691 >        assertEquals(tasks.size(), queuedTasks.size());
692 >        for (ScheduledFuture task : tasks) {
693 >            assertFalse(task.isDone());
694 >            assertFalse(task.isCancelled());
695          }
696 +        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
697 +        assertTrue(p.isTerminated());
698      }
699  
600
700      /**
701 <     * If setExecuteExistingDelayedTasksAfterShutdownPolicy is false,
702 <     * delayed tasks are cancelled at shutdown
703 <     */
704 <    public void testShutDown2() {
705 <        try {
706 <            ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
707 <            p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
708 <            ScheduledFuture[] tasks = new ScheduledFuture[5];
709 <            for(int i = 0; i < 5; i++)
710 <                tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
711 <            try { p1.shutdown(); } catch(SecurityException ok) { return; }
712 <            assertTrue(p1.isShutdown());
713 <            BlockingQueue q = p1.getQueue();
714 <            assertTrue(q.isEmpty());
715 <            Thread.sleep(SMALL_DELAY_MS);
716 <            assertTrue(p1.isTerminated());
717 <        }
718 <        catch(Exception ex) {
719 <            unexpectedException();
701 >     * By default, periodic tasks are cancelled at shutdown.
702 >     * By default, delayed tasks keep running after shutdown.
703 >     * Check that changing the default values work:
704 >     * - setExecuteExistingDelayedTasksAfterShutdownPolicy
705 >     * - setContinueExistingPeriodicTasksAfterShutdownPolicy
706 >     */
707 >    @SuppressWarnings("FutureReturnValueIgnored")
708 >    public void testShutdown_cancellation() throws Exception {
709 >        final int poolSize = 4;
710 >        final ScheduledThreadPoolExecutor p
711 >            = new ScheduledThreadPoolExecutor(poolSize);
712 >        final BlockingQueue<Runnable> q = p.getQueue();
713 >        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
714 >        final long delay = rnd.nextInt(2);
715 >        final int rounds = rnd.nextInt(1, 3);
716 >        final boolean effectiveDelayedPolicy;
717 >        final boolean effectivePeriodicPolicy;
718 >        final boolean effectiveRemovePolicy;
719 >
720 >        if (rnd.nextBoolean())
721 >            p.setExecuteExistingDelayedTasksAfterShutdownPolicy(
722 >                effectiveDelayedPolicy = rnd.nextBoolean());
723 >        else
724 >            effectiveDelayedPolicy = true;
725 >        assertEquals(effectiveDelayedPolicy,
726 >                     p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
727 >
728 >        if (rnd.nextBoolean())
729 >            p.setContinueExistingPeriodicTasksAfterShutdownPolicy(
730 >                effectivePeriodicPolicy = rnd.nextBoolean());
731 >        else
732 >            effectivePeriodicPolicy = false;
733 >        assertEquals(effectivePeriodicPolicy,
734 >                     p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
735 >
736 >        if (rnd.nextBoolean())
737 >            p.setRemoveOnCancelPolicy(
738 >                effectiveRemovePolicy = rnd.nextBoolean());
739 >        else
740 >            effectiveRemovePolicy = false;
741 >        assertEquals(effectiveRemovePolicy,
742 >                     p.getRemoveOnCancelPolicy());
743 >
744 >        final boolean periodicTasksContinue = effectivePeriodicPolicy && rnd.nextBoolean();
745 >
746 >        // Strategy: Wedge the pool with one wave of "blocker" tasks,
747 >        // then add a second wave that waits in the queue until unblocked.
748 >        final AtomicInteger ran = new AtomicInteger(0);
749 >        final CountDownLatch poolBlocked = new CountDownLatch(poolSize);
750 >        final CountDownLatch unblock = new CountDownLatch(1);
751 >        final RuntimeException exception = new RuntimeException();
752 >
753 >        class Task implements Runnable {
754 >            public void run() {
755 >                try {
756 >                    ran.getAndIncrement();
757 >                    poolBlocked.countDown();
758 >                    await(unblock);
759 >                } catch (Throwable fail) { threadUnexpectedException(fail); }
760 >            }
761          }
622    }
623
762  
763 <    /**
764 <     * If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false,
765 <     * periodic tasks are not cancelled at shutdown
766 <     */
767 <    public void testShutDown3() {
768 <        try {
769 <            ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
770 <            p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
771 <            ScheduledFuture task =
634 <                p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS);
635 <            try { p1.shutdown(); } catch(SecurityException ok) { return; }
636 <            assertTrue(p1.isShutdown());
637 <            BlockingQueue q = p1.getQueue();
638 <            assertTrue(q.isEmpty());
639 <            Thread.sleep(SHORT_DELAY_MS);
640 <            assertTrue(p1.isTerminated());
641 <        }
642 <        catch(Exception ex) {
643 <            unexpectedException();
763 >        class PeriodicTask extends Task {
764 >            PeriodicTask(int rounds) { this.rounds = rounds; }
765 >            int rounds;
766 >            public void run() {
767 >                if (--rounds == 0) super.run();
768 >                // throw exception to surely terminate this periodic task,
769 >                // but in a separate execution and in a detectable way.
770 >                if (rounds == -1) throw exception;
771 >            }
772          }
645    }
773  
774 <    /**
775 <     * if setContinueExistingPeriodicTasksAfterShutdownPolicy is true,
776 <     * periodic tasks are cancelled at shutdown
777 <     */
778 <    public void testShutDown4() {
779 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
780 <        try {
781 <            p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
782 <            ScheduledFuture task =
783 <                p1.scheduleAtFixedRate(new NoOpRunnable(), 1, 1, TimeUnit.MILLISECONDS);
784 <            assertFalse(task.isCancelled());
785 <            try { p1.shutdown(); } catch(SecurityException ok) { return; }
786 <            assertFalse(task.isCancelled());
787 <            assertFalse(p1.isTerminated());
788 <            assertTrue(p1.isShutdown());
789 <            Thread.sleep(SHORT_DELAY_MS);
790 <            assertFalse(task.isCancelled());
791 <            assertTrue(task.cancel(true));
792 <            assertTrue(task.isDone());
793 <            Thread.sleep(SHORT_DELAY_MS);
794 <            assertTrue(p1.isTerminated());
795 <        }
796 <        catch(Exception ex) {
797 <            unexpectedException();
798 <        }
799 <        finally {
800 <            joinPool(p1);
801 <        }
774 >        Runnable task = new Task();
775 >
776 >        List<Future<?>> immediates = new ArrayList<>();
777 >        List<Future<?>> delayeds   = new ArrayList<>();
778 >        List<Future<?>> periodics  = new ArrayList<>();
779 >
780 >        immediates.add(p.submit(task));
781 >        delayeds.add(p.schedule(task, delay, MILLISECONDS));
782 >        periodics.add(p.scheduleAtFixedRate(
783 >                          new PeriodicTask(rounds), delay, 1, MILLISECONDS));
784 >        periodics.add(p.scheduleWithFixedDelay(
785 >                          new PeriodicTask(rounds), delay, 1, MILLISECONDS));
786 >
787 >        await(poolBlocked);
788 >
789 >        assertEquals(poolSize, ran.get());
790 >        assertEquals(poolSize, p.getActiveCount());
791 >        assertTrue(q.isEmpty());
792 >
793 >        // Add second wave of tasks.
794 >        immediates.add(p.submit(task));
795 >        delayeds.add(p.schedule(task, effectiveDelayedPolicy ? delay : LONG_DELAY_MS, MILLISECONDS));
796 >        periodics.add(p.scheduleAtFixedRate(
797 >                          new PeriodicTask(rounds), delay, 1, MILLISECONDS));
798 >        periodics.add(p.scheduleWithFixedDelay(
799 >                          new PeriodicTask(rounds), delay, 1, MILLISECONDS));
800 >
801 >        assertEquals(poolSize, q.size());
802 >        assertEquals(poolSize, ran.get());
803 >
804 >        immediates.forEach(
805 >            f -> assertTrue(((ScheduledFuture)f).getDelay(NANOSECONDS) <= 0L));
806 >
807 >        Stream.of(immediates, delayeds, periodics).flatMap(Collection::stream)
808 >            .forEach(f -> assertFalse(f.isDone()));
809 >
810 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
811 >        assertTrue(p.isShutdown());
812 >        assertTrue(p.isTerminating());
813 >        assertFalse(p.isTerminated());
814 >
815 >        if (rnd.nextBoolean())
816 >            assertThrows(
817 >                RejectedExecutionException.class,
818 >                () -> p.submit(task),
819 >                () -> p.schedule(task, 1, SECONDS),
820 >                () -> p.scheduleAtFixedRate(
821 >                    new PeriodicTask(1), 1, 1, SECONDS),
822 >                () -> p.scheduleWithFixedDelay(
823 >                    new PeriodicTask(2), 1, 1, SECONDS));
824 >
825 >        assertTrue(q.contains(immediates.get(1)));
826 >        assertTrue(!effectiveDelayedPolicy
827 >                   ^ q.contains(delayeds.get(1)));
828 >        assertTrue(!effectivePeriodicPolicy
829 >                   ^ q.containsAll(periodics.subList(2, 4)));
830 >
831 >        immediates.forEach(f -> assertFalse(f.isDone()));
832 >
833 >        assertFalse(delayeds.get(0).isDone());
834 >        if (effectiveDelayedPolicy)
835 >            assertFalse(delayeds.get(1).isDone());
836 >        else
837 >            assertTrue(delayeds.get(1).isCancelled());
838 >
839 >        if (effectivePeriodicPolicy)
840 >            periodics.forEach(
841 >                f -> {
842 >                    assertFalse(f.isDone());
843 >                    if (!periodicTasksContinue) {
844 >                        assertTrue(f.cancel(false));
845 >                        assertTrue(f.isCancelled());
846 >                    }
847 >                });
848 >        else {
849 >            periodics.subList(0, 2).forEach(f -> assertFalse(f.isDone()));
850 >            periodics.subList(2, 4).forEach(f -> assertTrue(f.isCancelled()));
851 >        }
852 >
853 >        unblock.countDown();    // Release all pool threads
854 >
855 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
856 >        assertFalse(p.isTerminating());
857 >        assertTrue(p.isTerminated());
858 >
859 >        assertTrue(q.isEmpty());
860 >
861 >        Stream.of(immediates, delayeds, periodics).flatMap(Collection::stream)
862 >            .forEach(f -> assertTrue(f.isDone()));
863 >
864 >        for (Future<?> f : immediates) assertNull(f.get());
865 >
866 >        assertNull(delayeds.get(0).get());
867 >        if (effectiveDelayedPolicy)
868 >            assertNull(delayeds.get(1).get());
869 >        else
870 >            assertTrue(delayeds.get(1).isCancelled());
871 >
872 >        if (periodicTasksContinue)
873 >            periodics.forEach(
874 >                f -> {
875 >                    try { f.get(); }
876 >                    catch (ExecutionException success) {
877 >                        assertSame(exception, success.getCause());
878 >                    }
879 >                    catch (Throwable fail) { threadUnexpectedException(fail); }
880 >                });
881 >        else
882 >            periodics.forEach(f -> assertTrue(f.isCancelled()));
883 >
884 >        assertEquals(poolSize + 1
885 >                     + (effectiveDelayedPolicy ? 1 : 0)
886 >                     + (periodicTasksContinue ? 2 : 0),
887 >                     ran.get());
888      }
889  
890      /**
891       * completed submit of callable returns result
892       */
893 <    public void testSubmitCallable() {
894 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
895 <        try {
893 >    public void testSubmitCallable() throws Exception {
894 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
895 >        try (PoolCleaner cleaner = cleaner(e)) {
896              Future<String> future = e.submit(new StringTask());
897              String result = future.get();
898              assertSame(TEST_STRING, result);
899          }
687        catch (ExecutionException ex) {
688            unexpectedException();
689        }
690        catch (InterruptedException ex) {
691            unexpectedException();
692        } finally {
693            joinPool(e);
694        }
900      }
901  
902      /**
903       * completed submit of runnable returns successfully
904       */
905 <    public void testSubmitRunnable() {
906 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
907 <        try {
905 >    public void testSubmitRunnable() throws Exception {
906 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
907 >        try (PoolCleaner cleaner = cleaner(e)) {
908              Future<?> future = e.submit(new NoOpRunnable());
909              future.get();
910              assertTrue(future.isDone());
911          }
707        catch (ExecutionException ex) {
708            unexpectedException();
709        }
710        catch (InterruptedException ex) {
711            unexpectedException();
712        } finally {
713            joinPool(e);
714        }
912      }
913  
914      /**
915       * completed submit of (runnable, result) returns result
916       */
917 <    public void testSubmitRunnable2() {
918 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
919 <        try {
917 >    public void testSubmitRunnable2() throws Exception {
918 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
919 >        try (PoolCleaner cleaner = cleaner(e)) {
920              Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
921              String result = future.get();
922              assertSame(TEST_STRING, result);
923          }
727        catch (ExecutionException ex) {
728            unexpectedException();
729        }
730        catch (InterruptedException ex) {
731            unexpectedException();
732        } finally {
733            joinPool(e);
734        }
924      }
925  
926      /**
927 <     * invokeAny(null) throws NPE
927 >     * invokeAny(null) throws NullPointerException
928       */
929 <    public void testInvokeAny1() {
930 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
931 <        try {
932 <            e.invokeAny(null);
933 <        } catch (NullPointerException success) {
934 <        } catch(Exception ex) {
935 <            unexpectedException();
747 <        } finally {
748 <            joinPool(e);
929 >    public void testInvokeAny1() throws Exception {
930 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
931 >        try (PoolCleaner cleaner = cleaner(e)) {
932 >            try {
933 >                e.invokeAny(null);
934 >                shouldThrow();
935 >            } catch (NullPointerException success) {}
936          }
937      }
938  
939      /**
940 <     * invokeAny(empty collection) throws IAE
940 >     * invokeAny(empty collection) throws IllegalArgumentException
941       */
942 <    public void testInvokeAny2() {
943 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
944 <        try {
945 <            e.invokeAny(new ArrayList<Callable<String>>());
946 <        } catch (IllegalArgumentException success) {
947 <        } catch(Exception ex) {
948 <            unexpectedException();
762 <        } finally {
763 <            joinPool(e);
942 >    public void testInvokeAny2() throws Exception {
943 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
944 >        try (PoolCleaner cleaner = cleaner(e)) {
945 >            try {
946 >                e.invokeAny(new ArrayList<Callable<String>>());
947 >                shouldThrow();
948 >            } catch (IllegalArgumentException success) {}
949          }
950      }
951  
952      /**
953 <     * invokeAny(c) throws NPE if c has null elements
953 >     * invokeAny(c) throws NullPointerException if c has null elements
954       */
955 <    public void testInvokeAny3() {
956 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
957 <        try {
958 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
959 <            l.add(new StringTask());
955 >    public void testInvokeAny3() throws Exception {
956 >        CountDownLatch latch = new CountDownLatch(1);
957 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
958 >        try (PoolCleaner cleaner = cleaner(e)) {
959 >            List<Callable<String>> l = new ArrayList<>();
960 >            l.add(latchAwaitingStringTask(latch));
961              l.add(null);
962 <            e.invokeAny(l);
963 <        } catch (NullPointerException success) {
964 <        } catch(Exception ex) {
965 <            unexpectedException();
966 <        } finally {
781 <            joinPool(e);
962 >            try {
963 >                e.invokeAny(l);
964 >                shouldThrow();
965 >            } catch (NullPointerException success) {}
966 >            latch.countDown();
967          }
968      }
969  
970      /**
971       * invokeAny(c) throws ExecutionException if no task completes
972       */
973 <    public void testInvokeAny4() {
974 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
975 <        try {
976 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
973 >    public void testInvokeAny4() throws Exception {
974 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
975 >        try (PoolCleaner cleaner = cleaner(e)) {
976 >            List<Callable<String>> l = new ArrayList<>();
977              l.add(new NPETask());
978 <            e.invokeAny(l);
979 <        } catch (ExecutionException success) {
980 <        } catch(Exception ex) {
981 <            unexpectedException();
982 <        } finally {
983 <            joinPool(e);
978 >            try {
979 >                e.invokeAny(l);
980 >                shouldThrow();
981 >            } catch (ExecutionException success) {
982 >                assertTrue(success.getCause() instanceof NullPointerException);
983 >            }
984          }
985      }
986  
987      /**
988       * invokeAny(c) returns result of some task
989       */
990 <    public void testInvokeAny5() {
991 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
992 <        try {
993 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
990 >    public void testInvokeAny5() throws Exception {
991 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
992 >        try (PoolCleaner cleaner = cleaner(e)) {
993 >            List<Callable<String>> l = new ArrayList<>();
994              l.add(new StringTask());
995              l.add(new StringTask());
996              String result = e.invokeAny(l);
997              assertSame(TEST_STRING, result);
813        } catch (ExecutionException success) {
814        } catch(Exception ex) {
815            unexpectedException();
816        } finally {
817            joinPool(e);
998          }
999      }
1000  
1001      /**
1002       * invokeAll(null) throws NPE
1003       */
1004 <    public void testInvokeAll1() {
1005 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1006 <        try {
1007 <            e.invokeAll(null);
1008 <        } catch (NullPointerException success) {
1009 <        } catch(Exception ex) {
1010 <            unexpectedException();
831 <        } finally {
832 <            joinPool(e);
1004 >    public void testInvokeAll1() throws Exception {
1005 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1006 >        try (PoolCleaner cleaner = cleaner(e)) {
1007 >            try {
1008 >                e.invokeAll(null);
1009 >                shouldThrow();
1010 >            } catch (NullPointerException success) {}
1011          }
1012      }
1013  
1014      /**
1015 <     * invokeAll(empty collection) returns empty collection
1015 >     * invokeAll(empty collection) returns empty list
1016       */
1017 <    public void testInvokeAll2() {
1018 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1019 <        try {
1020 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1017 >    public void testInvokeAll2() throws Exception {
1018 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1019 >        final Collection<Callable<String>> emptyCollection
1020 >            = Collections.emptyList();
1021 >        try (PoolCleaner cleaner = cleaner(e)) {
1022 >            List<Future<String>> r = e.invokeAll(emptyCollection);
1023              assertTrue(r.isEmpty());
844        } catch(Exception ex) {
845            unexpectedException();
846        } finally {
847            joinPool(e);
1024          }
1025      }
1026  
1027      /**
1028       * invokeAll(c) throws NPE if c has null elements
1029       */
1030 <    public void testInvokeAll3() {
1031 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1032 <        try {
1033 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1030 >    public void testInvokeAll3() throws Exception {
1031 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1032 >        try (PoolCleaner cleaner = cleaner(e)) {
1033 >            List<Callable<String>> l = new ArrayList<>();
1034              l.add(new StringTask());
1035              l.add(null);
1036 <            e.invokeAll(l);
1037 <        } catch (NullPointerException success) {
1038 <        } catch(Exception ex) {
1039 <            unexpectedException();
864 <        } finally {
865 <            joinPool(e);
1036 >            try {
1037 >                e.invokeAll(l);
1038 >                shouldThrow();
1039 >            } catch (NullPointerException success) {}
1040          }
1041      }
1042  
1043      /**
1044       * get of invokeAll(c) throws exception on failed task
1045       */
1046 <    public void testInvokeAll4() {
1047 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1048 <        try {
1049 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1046 >    public void testInvokeAll4() throws Exception {
1047 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1048 >        try (PoolCleaner cleaner = cleaner(e)) {
1049 >            List<Callable<String>> l = new ArrayList<>();
1050              l.add(new NPETask());
1051 <            List<Future<String>> result = e.invokeAll(l);
1052 <            assertEquals(1, result.size());
1053 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1054 <                it.next().get();
1055 <        } catch(ExecutionException success) {
1056 <        } catch(Exception ex) {
1057 <            unexpectedException();
1058 <        } finally {
885 <            joinPool(e);
1051 >            List<Future<String>> futures = e.invokeAll(l);
1052 >            assertEquals(1, futures.size());
1053 >            try {
1054 >                futures.get(0).get();
1055 >                shouldThrow();
1056 >            } catch (ExecutionException success) {
1057 >                assertTrue(success.getCause() instanceof NullPointerException);
1058 >            }
1059          }
1060      }
1061  
1062      /**
1063       * invokeAll(c) returns results of all completed tasks
1064       */
1065 <    public void testInvokeAll5() {
1066 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1067 <        try {
1068 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1065 >    public void testInvokeAll5() throws Exception {
1066 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1067 >        try (PoolCleaner cleaner = cleaner(e)) {
1068 >            List<Callable<String>> l = new ArrayList<>();
1069              l.add(new StringTask());
1070              l.add(new StringTask());
1071 <            List<Future<String>> result = e.invokeAll(l);
1072 <            assertEquals(2, result.size());
1073 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1074 <                assertSame(TEST_STRING, it.next().get());
902 <        } catch (ExecutionException success) {
903 <        } catch(Exception ex) {
904 <            unexpectedException();
905 <        } finally {
906 <            joinPool(e);
1071 >            List<Future<String>> futures = e.invokeAll(l);
1072 >            assertEquals(2, futures.size());
1073 >            for (Future<String> future : futures)
1074 >                assertSame(TEST_STRING, future.get());
1075          }
1076      }
1077  
1078      /**
1079       * timed invokeAny(null) throws NPE
1080       */
1081 <    public void testTimedInvokeAny1() {
1082 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1083 <        try {
1084 <            e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1085 <        } catch (NullPointerException success) {
1086 <        } catch(Exception ex) {
1087 <            unexpectedException();
920 <        } finally {
921 <            joinPool(e);
1081 >    public void testTimedInvokeAny1() throws Exception {
1082 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1083 >        try (PoolCleaner cleaner = cleaner(e)) {
1084 >            try {
1085 >                e.invokeAny(null, randomTimeout(), randomTimeUnit());
1086 >                shouldThrow();
1087 >            } catch (NullPointerException success) {}
1088          }
1089      }
1090  
1091      /**
1092 <     * timed invokeAny(,,null) throws NPE
1092 >     * timed invokeAny(,,null) throws NullPointerException
1093       */
1094 <    public void testTimedInvokeAnyNullTimeUnit() {
1095 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1096 <        try {
1097 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1094 >    public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1095 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1096 >        try (PoolCleaner cleaner = cleaner(e)) {
1097 >            List<Callable<String>> l = new ArrayList<>();
1098              l.add(new StringTask());
1099 <            e.invokeAny(l, MEDIUM_DELAY_MS, null);
1100 <        } catch (NullPointerException success) {
1101 <        } catch(Exception ex) {
1102 <            unexpectedException();
937 <        } finally {
938 <            joinPool(e);
1099 >            try {
1100 >                e.invokeAny(l, randomTimeout(), null);
1101 >                shouldThrow();
1102 >            } catch (NullPointerException success) {}
1103          }
1104      }
1105  
1106      /**
1107 <     * timed invokeAny(empty collection) throws IAE
1107 >     * timed invokeAny(empty collection) throws IllegalArgumentException
1108       */
1109 <    public void testTimedInvokeAny2() {
1110 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1111 <        try {
1112 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1113 <        } catch (IllegalArgumentException success) {
1114 <        } catch(Exception ex) {
1115 <            unexpectedException();
1116 <        } finally {
1117 <            joinPool(e);
1109 >    public void testTimedInvokeAny2() throws Exception {
1110 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1111 >        final Collection<Callable<String>> emptyCollection
1112 >            = Collections.emptyList();
1113 >        try (PoolCleaner cleaner = cleaner(e)) {
1114 >            try {
1115 >                e.invokeAny(emptyCollection, randomTimeout(), randomTimeUnit());
1116 >                shouldThrow();
1117 >            } catch (IllegalArgumentException success) {}
1118          }
1119      }
1120  
1121      /**
1122       * timed invokeAny(c) throws NPE if c has null elements
1123       */
1124 <    public void testTimedInvokeAny3() {
1125 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1126 <        try {
1127 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1128 <            l.add(new StringTask());
1124 >    public void testTimedInvokeAny3() throws Exception {
1125 >        CountDownLatch latch = new CountDownLatch(1);
1126 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1127 >        try (PoolCleaner cleaner = cleaner(e)) {
1128 >            List<Callable<String>> l = new ArrayList<>();
1129 >            l.add(latchAwaitingStringTask(latch));
1130              l.add(null);
1131 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1132 <        } catch (NullPointerException success) {
1133 <        } catch(Exception ex) {
1134 <            ex.printStackTrace();
1135 <            unexpectedException();
971 <        } finally {
972 <            joinPool(e);
1131 >            try {
1132 >                e.invokeAny(l, randomTimeout(), randomTimeUnit());
1133 >                shouldThrow();
1134 >            } catch (NullPointerException success) {}
1135 >            latch.countDown();
1136          }
1137      }
1138  
1139      /**
1140       * timed invokeAny(c) throws ExecutionException if no task completes
1141       */
1142 <    public void testTimedInvokeAny4() {
1143 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1144 <        try {
1145 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1142 >    public void testTimedInvokeAny4() throws Exception {
1143 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1144 >        try (PoolCleaner cleaner = cleaner(e)) {
1145 >            long startTime = System.nanoTime();
1146 >            List<Callable<String>> l = new ArrayList<>();
1147              l.add(new NPETask());
1148 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1149 <        } catch(ExecutionException success) {
1150 <        } catch(Exception ex) {
1151 <            unexpectedException();
1152 <        } finally {
1153 <            joinPool(e);
1148 >            try {
1149 >                e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1150 >                shouldThrow();
1151 >            } catch (ExecutionException success) {
1152 >                assertTrue(success.getCause() instanceof NullPointerException);
1153 >            }
1154 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1155          }
1156      }
1157  
1158      /**
1159       * timed invokeAny(c) returns result of some task
1160       */
1161 <    public void testTimedInvokeAny5() {
1162 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1163 <        try {
1164 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1161 >    public void testTimedInvokeAny5() throws Exception {
1162 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1163 >        try (PoolCleaner cleaner = cleaner(e)) {
1164 >            long startTime = System.nanoTime();
1165 >            List<Callable<String>> l = new ArrayList<>();
1166              l.add(new StringTask());
1167              l.add(new StringTask());
1168 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1168 >            String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1169              assertSame(TEST_STRING, result);
1170 <        } catch (ExecutionException success) {
1005 <        } catch(Exception ex) {
1006 <            unexpectedException();
1007 <        } finally {
1008 <            joinPool(e);
1170 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1171          }
1172      }
1173  
1174      /**
1175       * timed invokeAll(null) throws NPE
1176       */
1177 <    public void testTimedInvokeAll1() {
1178 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1179 <        try {
1180 <            e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1181 <        } catch (NullPointerException success) {
1182 <        } catch(Exception ex) {
1183 <            unexpectedException();
1022 <        } finally {
1023 <            joinPool(e);
1177 >    public void testTimedInvokeAll1() throws Exception {
1178 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1179 >        try (PoolCleaner cleaner = cleaner(e)) {
1180 >            try {
1181 >                e.invokeAll(null, randomTimeout(), randomTimeUnit());
1182 >                shouldThrow();
1183 >            } catch (NullPointerException success) {}
1184          }
1185      }
1186  
1187      /**
1188       * timed invokeAll(,,null) throws NPE
1189       */
1190 <    public void testTimedInvokeAllNullTimeUnit() {
1191 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1192 <        try {
1193 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1190 >    public void testTimedInvokeAllNullTimeUnit() throws Exception {
1191 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1192 >        try (PoolCleaner cleaner = cleaner(e)) {
1193 >            List<Callable<String>> l = new ArrayList<>();
1194              l.add(new StringTask());
1195 <            e.invokeAll(l, MEDIUM_DELAY_MS, null);
1196 <        } catch (NullPointerException success) {
1197 <        } catch(Exception ex) {
1198 <            unexpectedException();
1039 <        } finally {
1040 <            joinPool(e);
1195 >            try {
1196 >                e.invokeAll(l, randomTimeout(), null);
1197 >                shouldThrow();
1198 >            } catch (NullPointerException success) {}
1199          }
1200      }
1201  
1202      /**
1203 <     * timed invokeAll(empty collection) returns empty collection
1203 >     * timed invokeAll(empty collection) returns empty list
1204       */
1205 <    public void testTimedInvokeAll2() {
1206 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1207 <        try {
1208 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1205 >    public void testTimedInvokeAll2() throws Exception {
1206 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1207 >        final Collection<Callable<String>> emptyCollection
1208 >            = Collections.emptyList();
1209 >        try (PoolCleaner cleaner = cleaner(e)) {
1210 >            List<Future<String>> r =
1211 >                e.invokeAll(emptyCollection, randomTimeout(), randomTimeUnit());
1212              assertTrue(r.isEmpty());
1052        } catch(Exception ex) {
1053            unexpectedException();
1054        } finally {
1055            joinPool(e);
1213          }
1214      }
1215  
1216      /**
1217       * timed invokeAll(c) throws NPE if c has null elements
1218       */
1219 <    public void testTimedInvokeAll3() {
1220 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1221 <        try {
1222 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1219 >    public void testTimedInvokeAll3() throws Exception {
1220 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1221 >        try (PoolCleaner cleaner = cleaner(e)) {
1222 >            List<Callable<String>> l = new ArrayList<>();
1223              l.add(new StringTask());
1224              l.add(null);
1225 <            e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1226 <        } catch (NullPointerException success) {
1227 <        } catch(Exception ex) {
1228 <            unexpectedException();
1072 <        } finally {
1073 <            joinPool(e);
1225 >            try {
1226 >                e.invokeAll(l, randomTimeout(), randomTimeUnit());
1227 >                shouldThrow();
1228 >            } catch (NullPointerException success) {}
1229          }
1230      }
1231  
1232      /**
1233       * get of element of invokeAll(c) throws exception on failed task
1234       */
1235 <    public void testTimedInvokeAll4() {
1236 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1237 <        try {
1238 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1235 >    public void testTimedInvokeAll4() throws Exception {
1236 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1237 >        try (PoolCleaner cleaner = cleaner(e)) {
1238 >            List<Callable<String>> l = new ArrayList<>();
1239              l.add(new NPETask());
1240 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1241 <            assertEquals(1, result.size());
1242 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1243 <                it.next().get();
1244 <        } catch(ExecutionException success) {
1245 <        } catch(Exception ex) {
1246 <            unexpectedException();
1247 <        } finally {
1248 <            joinPool(e);
1240 >            List<Future<String>> futures =
1241 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1242 >            assertEquals(1, futures.size());
1243 >            try {
1244 >                futures.get(0).get();
1245 >                shouldThrow();
1246 >            } catch (ExecutionException success) {
1247 >                assertTrue(success.getCause() instanceof NullPointerException);
1248 >            }
1249          }
1250      }
1251  
1252      /**
1253       * timed invokeAll(c) returns results of all completed tasks
1254       */
1255 <    public void testTimedInvokeAll5() {
1256 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1257 <        try {
1258 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1255 >    public void testTimedInvokeAll5() throws Exception {
1256 >        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1257 >        try (PoolCleaner cleaner = cleaner(e)) {
1258 >            List<Callable<String>> l = new ArrayList<>();
1259              l.add(new StringTask());
1260              l.add(new StringTask());
1261 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1262 <            assertEquals(2, result.size());
1263 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1264 <                assertSame(TEST_STRING, it.next().get());
1265 <        } catch (ExecutionException success) {
1111 <        } catch(Exception ex) {
1112 <            unexpectedException();
1113 <        } finally {
1114 <            joinPool(e);
1261 >            List<Future<String>> futures =
1262 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1263 >            assertEquals(2, futures.size());
1264 >            for (Future<String> future : futures)
1265 >                assertSame(TEST_STRING, future.get());
1266          }
1267      }
1268  
1269      /**
1270       * timed invokeAll(c) cancels tasks not completed by timeout
1271       */
1272 <    public void testTimedInvokeAll6() {
1273 <        ExecutorService e = new ScheduledThreadPoolExecutor(2);
1274 <        try {
1275 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1276 <            l.add(new StringTask());
1277 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1278 <            l.add(new StringTask());
1279 <            List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
1280 <            assertEquals(3, result.size());
1281 <            Iterator<Future<String>> it = result.iterator();
1282 <            Future<String> f1 = it.next();
1283 <            Future<String> f2 = it.next();
1284 <            Future<String> f3 = it.next();
1285 <            assertTrue(f1.isDone());
1286 <            assertTrue(f2.isDone());
1287 <            assertTrue(f3.isDone());
1288 <            assertFalse(f1.isCancelled());
1289 <            assertTrue(f2.isCancelled());
1290 <        } catch(Exception ex) {
1291 <            unexpectedException();
1292 <        } finally {
1293 <            joinPool(e);
1272 >    public void testTimedInvokeAll6() throws Exception {
1273 >        for (long timeout = timeoutMillis();;) {
1274 >            final CountDownLatch done = new CountDownLatch(1);
1275 >            final Callable<String> waiter = new CheckedCallable<String>() {
1276 >                public String realCall() {
1277 >                    try { done.await(LONG_DELAY_MS, MILLISECONDS); }
1278 >                    catch (InterruptedException ok) {}
1279 >                    return "1"; }};
1280 >            final ExecutorService p = new ScheduledThreadPoolExecutor(2);
1281 >            try (PoolCleaner cleaner = cleaner(p, done)) {
1282 >                List<Callable<String>> tasks = new ArrayList<>();
1283 >                tasks.add(new StringTask("0"));
1284 >                tasks.add(waiter);
1285 >                tasks.add(new StringTask("2"));
1286 >                long startTime = System.nanoTime();
1287 >                List<Future<String>> futures =
1288 >                    p.invokeAll(tasks, timeout, MILLISECONDS);
1289 >                assertEquals(tasks.size(), futures.size());
1290 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1291 >                for (Future future : futures)
1292 >                    assertTrue(future.isDone());
1293 >                assertTrue(futures.get(1).isCancelled());
1294 >                try {
1295 >                    assertEquals("0", futures.get(0).get());
1296 >                    assertEquals("2", futures.get(2).get());
1297 >                    break;
1298 >                } catch (CancellationException retryWithLongerTimeout) {
1299 >                    timeout *= 2;
1300 >                    if (timeout >= LONG_DELAY_MS / 2)
1301 >                        fail("expected exactly one task to be cancelled");
1302 >                }
1303 >            }
1304          }
1305      }
1306  
1307 +    /**
1308 +     * A fixed delay task with overflowing period should not prevent a
1309 +     * one-shot task from executing.
1310 +     * https://bugs.openjdk.java.net/browse/JDK-8051859
1311 +     */
1312 +    @SuppressWarnings("FutureReturnValueIgnored")
1313 +    public void testScheduleWithFixedDelay_overflow() throws Exception {
1314 +        final CountDownLatch delayedDone = new CountDownLatch(1);
1315 +        final CountDownLatch immediateDone = new CountDownLatch(1);
1316 +        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
1317 +        try (PoolCleaner cleaner = cleaner(p)) {
1318 +            final Runnable delayed = () -> {
1319 +                delayedDone.countDown();
1320 +                p.submit(() -> immediateDone.countDown());
1321 +            };
1322 +            p.scheduleWithFixedDelay(delayed, 0L, Long.MAX_VALUE, SECONDS);
1323 +            await(delayedDone);
1324 +            await(immediateDone);
1325 +        }
1326 +    }
1327  
1328   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines