ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ScheduledExecutorTest.java
Revision: 1.72
Committed: Tue Oct 6 05:30:44 2015 UTC (8 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.71: +6 -6 lines
Log Message:
improve test diagnosability

File Contents

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