ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ScheduledExecutorTest.java
Revision: 1.47
Committed: Tue Sep 24 18:35:21 2013 UTC (10 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.46: +44 -20 lines
Log Message:
Improve expected execution times of testFixedDelaySequence,testFixedRateSequence

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