ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ScheduledExecutorSubclassTest.java
Revision: 1.22
Committed: Sat May 7 19:34:51 2011 UTC (13 years ago) by jsr166
Branch: MAIN
Changes since 1.21: +17 -17 lines
Log Message:
Improve testPurge and testShutdownNow

File Contents

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