ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ScheduledExecutorTest.java
Revision: 1.34
Committed: Mon Oct 11 15:46:40 2010 UTC (13 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.33: +9 -1 lines
Log Message:
add a few assertions

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