ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ScheduledExecutorTest.java
Revision: 1.50
Committed: Wed Dec 31 19:05:43 2014 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.49: +18 -3 lines
Log Message:
no wildcard imports

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