ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ScheduledExecutorTest.java
Revision: 1.49
Committed: Wed Sep 25 07:39:17 2013 UTC (10 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.48: +2 -2 lines
Log Message:
cosmetic changes

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