ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractExecutorServiceTest.java
Revision: 1.17
Committed: Mon Nov 16 04:57:09 2009 UTC (14 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.16: +47 -47 lines
Log Message:
whitespace

File Contents

# User Rev Content
1 tim 1.1 /*
2 dl 1.10 * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain, as explained at
4     * http://creativecommons.org/licenses/publicdomain
5 jsr166 1.16 * Other contributors include Andrew Wright, Jeffrey Hayes,
6     * Pat Fisher, Mike Judd.
7 tim 1.1 */
8    
9    
10     import junit.framework.*;
11     import java.util.*;
12     import java.util.concurrent.*;
13     import java.math.BigInteger;
14     import java.security.*;
15    
16     public class AbstractExecutorServiceTest extends JSR166TestCase{
17     public static void main(String[] args) {
18     junit.textui.TestRunner.run (suite());
19     }
20     public static Test suite() {
21 dl 1.6 return new TestSuite(AbstractExecutorServiceTest.class);
22 tim 1.1 }
23    
24 jsr166 1.16 /**
25 tim 1.1 * A no-frills implementation of AbstractExecutorService, designed
26 dl 1.6 * to test the submit methods only.
27 tim 1.1 */
28     static class DirectExecutorService extends AbstractExecutorService {
29     public void execute(Runnable r) { r.run(); }
30     public void shutdown() { shutdown = true; }
31 tim 1.2 public List<Runnable> shutdownNow() { shutdown = true; return Collections.EMPTY_LIST; }
32 tim 1.1 public boolean isShutdown() { return shutdown; }
33     public boolean isTerminated() { return isShutdown(); }
34     public boolean awaitTermination(long timeout, TimeUnit unit) { return isShutdown(); }
35     private volatile boolean shutdown = false;
36     }
37    
38     /**
39 dl 1.12 * execute(runnable) runs it to completion
40 tim 1.1 */
41     public void testExecuteRunnable() {
42     try {
43     ExecutorService e = new DirectExecutorService();
44     TrackedShortRunnable task = new TrackedShortRunnable();
45     assertFalse(task.done);
46 dl 1.4 Future<?> future = e.submit(task);
47 tim 1.1 future.get();
48     assertTrue(task.done);
49     }
50     catch (ExecutionException ex) {
51     unexpectedException();
52     }
53     catch (InterruptedException ex) {
54     unexpectedException();
55     }
56     }
57    
58    
59     /**
60 dl 1.12 * Completed submit(callable) returns result
61 tim 1.1 */
62 dl 1.6 public void testSubmitCallable() {
63 tim 1.1 try {
64     ExecutorService e = new DirectExecutorService();
65     Future<String> future = e.submit(new StringTask());
66     String result = future.get();
67     assertSame(TEST_STRING, result);
68     }
69     catch (ExecutionException ex) {
70     unexpectedException();
71     }
72     catch (InterruptedException ex) {
73     unexpectedException();
74     }
75     }
76    
77 dl 1.6 /**
78 dl 1.12 * Completed submit(runnable) returns successfully
79 dl 1.6 */
80     public void testSubmitRunnable() {
81     try {
82     ExecutorService e = new DirectExecutorService();
83     Future<?> future = e.submit(new NoOpRunnable());
84     future.get();
85     assertTrue(future.isDone());
86     }
87     catch (ExecutionException ex) {
88     unexpectedException();
89     }
90     catch (InterruptedException ex) {
91     unexpectedException();
92     }
93     }
94    
95     /**
96 dl 1.12 * Completed submit(runnable, result) returns result
97 dl 1.6 */
98     public void testSubmitRunnable2() {
99     try {
100     ExecutorService e = new DirectExecutorService();
101     Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
102     String result = future.get();
103     assertSame(TEST_STRING, result);
104     }
105     catch (ExecutionException ex) {
106     unexpectedException();
107     }
108     catch (InterruptedException ex) {
109     unexpectedException();
110     }
111     }
112    
113 tim 1.1
114     /**
115 dl 1.12 * A submitted privileged action to completion
116 tim 1.1 */
117 dl 1.6 public void testSubmitPrivilegedAction() {
118 dl 1.15 Policy savedPolicy = null;
119     try {
120     savedPolicy = Policy.getPolicy();
121     AdjustablePolicy policy = new AdjustablePolicy();
122     policy.addPermission(new RuntimePermission("getContextClassLoader"));
123     policy.addPermission(new RuntimePermission("setContextClassLoader"));
124     Policy.setPolicy(policy);
125 jsr166 1.17 } catch (AccessControlException ok) {
126 dl 1.15 return;
127     }
128 tim 1.1 try {
129     ExecutorService e = new DirectExecutorService();
130 dl 1.5 Future future = e.submit(Executors.callable(new PrivilegedAction() {
131 tim 1.1 public Object run() {
132     return TEST_STRING;
133 dl 1.5 }}));
134 tim 1.1
135     Object result = future.get();
136     assertSame(TEST_STRING, result);
137     }
138     catch (ExecutionException ex) {
139     unexpectedException();
140     }
141     catch (InterruptedException ex) {
142     unexpectedException();
143     }
144     finally {
145 dl 1.15 try {
146     Policy.setPolicy(savedPolicy);
147 jsr166 1.17 } catch (AccessControlException ok) {
148 dl 1.15 return;
149     }
150 tim 1.1 }
151     }
152    
153     /**
154 dl 1.12 * A submitted a privileged exception action runs to completion
155 tim 1.1 */
156 dl 1.6 public void testSubmitPrivilegedExceptionAction() {
157 dl 1.15 Policy savedPolicy = null;
158     try {
159     savedPolicy = Policy.getPolicy();
160     AdjustablePolicy policy = new AdjustablePolicy();
161     policy.addPermission(new RuntimePermission("getContextClassLoader"));
162     policy.addPermission(new RuntimePermission("setContextClassLoader"));
163     Policy.setPolicy(policy);
164 jsr166 1.17 } catch (AccessControlException ok) {
165 dl 1.15 return;
166     }
167    
168 tim 1.1 try {
169     ExecutorService e = new DirectExecutorService();
170 dl 1.5 Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
171 tim 1.1 public Object run() {
172     return TEST_STRING;
173 dl 1.5 }}));
174 tim 1.1
175     Object result = future.get();
176     assertSame(TEST_STRING, result);
177     }
178     catch (ExecutionException ex) {
179     unexpectedException();
180     }
181     catch (InterruptedException ex) {
182     unexpectedException();
183     }
184     finally {
185     Policy.setPolicy(savedPolicy);
186     }
187     }
188    
189     /**
190 dl 1.12 * A submitted failed privileged exception action reports exception
191 tim 1.1 */
192 dl 1.6 public void testSubmitFailedPrivilegedExceptionAction() {
193 dl 1.15 Policy savedPolicy = null;
194     try {
195     savedPolicy = Policy.getPolicy();
196     AdjustablePolicy policy = new AdjustablePolicy();
197     policy.addPermission(new RuntimePermission("getContextClassLoader"));
198     policy.addPermission(new RuntimePermission("setContextClassLoader"));
199     Policy.setPolicy(policy);
200 jsr166 1.17 } catch (AccessControlException ok) {
201 dl 1.15 return;
202     }
203    
204    
205 tim 1.1 try {
206     ExecutorService e = new DirectExecutorService();
207 dl 1.5 Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
208 tim 1.1 public Object run() throws Exception {
209     throw new IndexOutOfBoundsException();
210 dl 1.5 }}));
211 tim 1.1
212     Object result = future.get();
213     shouldThrow();
214     }
215     catch (ExecutionException success) {
216     }
217     catch (InterruptedException ex) {
218     unexpectedException();
219     }
220     finally {
221     Policy.setPolicy(savedPolicy);
222     }
223     }
224    
225     /**
226 dl 1.12 * execute(null runnable) throws NPE
227 tim 1.1 */
228     public void testExecuteNullRunnable() {
229     try {
230     ExecutorService e = new DirectExecutorService();
231     TrackedShortRunnable task = null;
232 dl 1.4 Future<?> future = e.submit(task);
233 tim 1.1 shouldThrow();
234     }
235     catch (NullPointerException success) {
236     }
237     catch (Exception ex) {
238     unexpectedException();
239     }
240     }
241    
242    
243     /**
244 dl 1.12 * submit(null callable) throws NPE
245 tim 1.1 */
246 dl 1.6 public void testSubmitNullCallable() {
247 tim 1.1 try {
248     ExecutorService e = new DirectExecutorService();
249     StringTask t = null;
250     Future<String> future = e.submit(t);
251     shouldThrow();
252     }
253     catch (NullPointerException success) {
254     }
255     catch (Exception ex) {
256     unexpectedException();
257     }
258     }
259    
260     /**
261 dl 1.12 * submit(runnable) throws RejectedExecutionException if
262     * executor is saturated.
263 tim 1.1 */
264     public void testExecute1() {
265 dl 1.13 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1));
266 tim 1.1 try {
267    
268 jsr166 1.17 for (int i = 0; i < 5; ++i){
269 dl 1.4 p.submit(new MediumRunnable());
270 tim 1.1 }
271     shouldThrow();
272 jsr166 1.17 } catch (RejectedExecutionException success){}
273 tim 1.1 joinPool(p);
274     }
275    
276     /**
277 dl 1.12 * submit(callable) throws RejectedExecutionException
278     * if executor is saturated.
279 tim 1.1 */
280     public void testExecute2() {
281 dl 1.13 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1));
282 tim 1.1 try {
283 jsr166 1.17 for (int i = 0; i < 5; ++i) {
284 tim 1.1 p.submit(new SmallCallable());
285     }
286     shouldThrow();
287 jsr166 1.17 } catch (RejectedExecutionException e){}
288 tim 1.1 joinPool(p);
289     }
290    
291    
292     /**
293 dl 1.12 * Blocking on submit(callable) throws InterruptedException if
294 tim 1.1 * caller interrupted.
295     */
296 dl 1.6 public void testInterruptedSubmit() {
297 dl 1.13 final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
298 tim 1.1 Thread t = new Thread(new Runnable() {
299     public void run() {
300     try {
301 dl 1.6 p.submit(new Callable<Object>() {
302 dl 1.4 public Object call() {
303 tim 1.1 try {
304     Thread.sleep(MEDIUM_DELAY_MS);
305     shouldThrow();
306 jsr166 1.17 } catch (InterruptedException e){
307 tim 1.1 }
308 dl 1.4 return null;
309 tim 1.1 }
310 dl 1.6 }).get();
311 jsr166 1.17 } catch (InterruptedException success){
312     } catch (Exception e) {
313 tim 1.1 unexpectedException();
314     }
315    
316     }
317     });
318     try {
319     t.start();
320     Thread.sleep(SHORT_DELAY_MS);
321     t.interrupt();
322 jsr166 1.17 } catch (Exception e){
323 tim 1.1 unexpectedException();
324     }
325     joinPool(p);
326     }
327    
328     /**
329 dl 1.12 * get of submitted callable throws Exception if callable
330 dl 1.6 * interrupted
331 tim 1.1 */
332 dl 1.6 public void testSubmitIE() {
333 dl 1.13 final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
334 tim 1.1
335     final Callable c = new Callable() {
336     public Object call() {
337     try {
338 dl 1.6 p.submit(new SmallCallable()).get();
339 tim 1.1 shouldThrow();
340 jsr166 1.17 } catch (InterruptedException e){}
341     catch (RejectedExecutionException e2){}
342     catch (ExecutionException e3){}
343 tim 1.1 return Boolean.TRUE;
344     }
345     };
346    
347    
348    
349     Thread t = new Thread(new Runnable() {
350     public void run() {
351     try {
352     c.call();
353 jsr166 1.17 } catch (Exception e){}
354 tim 1.1 }
355     });
356     try {
357     t.start();
358     Thread.sleep(SHORT_DELAY_MS);
359     t.interrupt();
360     t.join();
361 jsr166 1.17 } catch (InterruptedException e){
362 tim 1.1 unexpectedException();
363     }
364    
365     joinPool(p);
366     }
367    
368     /**
369 dl 1.12 * get of submit(callable) throws ExecutionException if callable
370     * throws exception
371 tim 1.1 */
372 dl 1.6 public void testSubmitEE() {
373 dl 1.13 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
374 tim 1.1
375     try {
376     Callable c = new Callable() {
377     public Object call() {
378     int i = 5/0;
379     return Boolean.TRUE;
380     }
381     };
382    
383 jsr166 1.17 for (int i =0; i < 5; i++){
384 dl 1.6 p.submit(c).get();
385 tim 1.1 }
386    
387     shouldThrow();
388     }
389 jsr166 1.17 catch (ExecutionException success){
390     } catch (Exception e) {
391 tim 1.1 unexpectedException();
392     }
393     joinPool(p);
394 dl 1.6 }
395    
396     /**
397     * invokeAny(null) throws NPE
398     */
399     public void testInvokeAny1() {
400     ExecutorService e = new DirectExecutorService();
401     try {
402     e.invokeAny(null);
403     } catch (NullPointerException success) {
404 jsr166 1.17 } catch (Exception ex) {
405 dl 1.6 unexpectedException();
406     } finally {
407     joinPool(e);
408     }
409     }
410    
411     /**
412     * invokeAny(empty collection) throws IAE
413     */
414     public void testInvokeAny2() {
415     ExecutorService e = new DirectExecutorService();
416     try {
417     e.invokeAny(new ArrayList<Callable<String>>());
418     } catch (IllegalArgumentException success) {
419 jsr166 1.17 } catch (Exception ex) {
420 dl 1.6 unexpectedException();
421     } finally {
422     joinPool(e);
423     }
424     }
425    
426     /**
427     * invokeAny(c) throws NPE if c has null elements
428     */
429     public void testInvokeAny3() {
430     ExecutorService e = new DirectExecutorService();
431     try {
432     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
433     l.add(new StringTask());
434     l.add(null);
435     e.invokeAny(l);
436     } catch (NullPointerException success) {
437 jsr166 1.17 } catch (Exception ex) {
438 dl 1.7 ex.printStackTrace();
439 dl 1.6 unexpectedException();
440     } finally {
441     joinPool(e);
442     }
443     }
444    
445     /**
446 dl 1.12 * invokeAny(c) throws ExecutionException if no task in c completes
447 dl 1.6 */
448     public void testInvokeAny4() {
449     ExecutorService e = new DirectExecutorService();
450     try {
451     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
452     l.add(new NPETask());
453 dl 1.8 e.invokeAny(l);
454 jsr166 1.17 } catch (ExecutionException success) {
455     } catch (Exception ex) {
456 dl 1.6 unexpectedException();
457     } finally {
458     joinPool(e);
459     }
460     }
461    
462     /**
463 dl 1.12 * invokeAny(c) returns result of some task in c if at least one completes
464 dl 1.6 */
465     public void testInvokeAny5() {
466     ExecutorService e = new DirectExecutorService();
467     try {
468     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
469     l.add(new StringTask());
470     l.add(new StringTask());
471     String result = e.invokeAny(l);
472     assertSame(TEST_STRING, result);
473     } catch (ExecutionException success) {
474 jsr166 1.17 } catch (Exception ex) {
475 dl 1.6 unexpectedException();
476     } finally {
477     joinPool(e);
478     }
479     }
480    
481     /**
482     * invokeAll(null) throws NPE
483     */
484     public void testInvokeAll1() {
485     ExecutorService e = new DirectExecutorService();
486     try {
487     e.invokeAll(null);
488     } catch (NullPointerException success) {
489 jsr166 1.17 } catch (Exception ex) {
490 dl 1.6 unexpectedException();
491     } finally {
492     joinPool(e);
493     }
494     }
495    
496     /**
497     * invokeAll(empty collection) returns empty collection
498     */
499     public void testInvokeAll2() {
500     ExecutorService e = new DirectExecutorService();
501     try {
502     List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
503     assertTrue(r.isEmpty());
504 jsr166 1.17 } catch (Exception ex) {
505 dl 1.6 unexpectedException();
506     } finally {
507     joinPool(e);
508     }
509     }
510    
511     /**
512     * invokeAll(c) throws NPE if c has null elements
513     */
514     public void testInvokeAll3() {
515     ExecutorService e = new DirectExecutorService();
516     try {
517     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
518     l.add(new StringTask());
519     l.add(null);
520     e.invokeAll(l);
521     } catch (NullPointerException success) {
522 jsr166 1.17 } catch (Exception ex) {
523 dl 1.6 unexpectedException();
524     } finally {
525     joinPool(e);
526     }
527     }
528    
529     /**
530 dl 1.12 * get of returned element of invokeAll(c) throws exception on failed task
531 dl 1.6 */
532     public void testInvokeAll4() {
533     ExecutorService e = new DirectExecutorService();
534     try {
535     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
536     l.add(new NPETask());
537     List<Future<String>> result = e.invokeAll(l);
538     assertEquals(1, result.size());
539 jsr166 1.16 for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
540 dl 1.6 it.next().get();
541 jsr166 1.17 } catch (ExecutionException success) {
542     } catch (Exception ex) {
543 dl 1.6 unexpectedException();
544     } finally {
545     joinPool(e);
546     }
547     }
548    
549     /**
550 dl 1.12 * invokeAll(c) returns results of all completed tasks in c
551 dl 1.6 */
552     public void testInvokeAll5() {
553     ExecutorService e = new DirectExecutorService();
554     try {
555     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
556     l.add(new StringTask());
557     l.add(new StringTask());
558     List<Future<String>> result = e.invokeAll(l);
559     assertEquals(2, result.size());
560 jsr166 1.16 for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
561 dl 1.6 assertSame(TEST_STRING, it.next().get());
562     } catch (ExecutionException success) {
563 jsr166 1.17 } catch (Exception ex) {
564 dl 1.8 unexpectedException();
565     } finally {
566     joinPool(e);
567     }
568     }
569    
570    
571     /**
572     * timed invokeAny(null) throws NPE
573     */
574     public void testTimedInvokeAny1() {
575     ExecutorService e = new DirectExecutorService();
576     try {
577     e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
578     } catch (NullPointerException success) {
579 jsr166 1.17 } catch (Exception ex) {
580 dl 1.8 unexpectedException();
581     } finally {
582     joinPool(e);
583     }
584     }
585    
586     /**
587 dl 1.12 * timed invokeAny(null time unit) throws NPE
588 dl 1.8 */
589     public void testTimedInvokeAnyNullTimeUnit() {
590     ExecutorService e = new DirectExecutorService();
591     try {
592     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
593     l.add(new StringTask());
594     e.invokeAny(l, MEDIUM_DELAY_MS, null);
595     } catch (NullPointerException success) {
596 jsr166 1.17 } catch (Exception ex) {
597 dl 1.8 unexpectedException();
598     } finally {
599     joinPool(e);
600     }
601     }
602    
603     /**
604     * timed invokeAny(empty collection) throws IAE
605     */
606     public void testTimedInvokeAny2() {
607     ExecutorService e = new DirectExecutorService();
608     try {
609     e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
610     } catch (IllegalArgumentException success) {
611 jsr166 1.17 } catch (Exception ex) {
612 dl 1.8 unexpectedException();
613     } finally {
614     joinPool(e);
615     }
616     }
617    
618     /**
619     * timed invokeAny(c) throws NPE if c has null elements
620     */
621     public void testTimedInvokeAny3() {
622     ExecutorService e = new DirectExecutorService();
623     try {
624     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
625     l.add(new StringTask());
626     l.add(null);
627     e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
628     } catch (NullPointerException success) {
629 jsr166 1.17 } catch (Exception ex) {
630 dl 1.8 ex.printStackTrace();
631     unexpectedException();
632     } finally {
633     joinPool(e);
634     }
635     }
636    
637     /**
638     * timed invokeAny(c) throws ExecutionException if no task completes
639     */
640     public void testTimedInvokeAny4() {
641     ExecutorService e = new DirectExecutorService();
642     try {
643     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
644     l.add(new NPETask());
645     e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
646 jsr166 1.17 } catch (ExecutionException success) {
647     } catch (Exception ex) {
648 dl 1.8 unexpectedException();
649     } finally {
650     joinPool(e);
651     }
652     }
653    
654     /**
655 dl 1.12 * timed invokeAny(c) returns result of some task in c
656 dl 1.8 */
657     public void testTimedInvokeAny5() {
658     ExecutorService e = new DirectExecutorService();
659     try {
660     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
661     l.add(new StringTask());
662     l.add(new StringTask());
663     String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
664     assertSame(TEST_STRING, result);
665     } catch (ExecutionException success) {
666 jsr166 1.17 } catch (Exception ex) {
667 dl 1.8 unexpectedException();
668     } finally {
669     joinPool(e);
670     }
671     }
672    
673     /**
674     * timed invokeAll(null) throws NPE
675     */
676     public void testTimedInvokeAll1() {
677     ExecutorService e = new DirectExecutorService();
678     try {
679     e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
680     } catch (NullPointerException success) {
681 jsr166 1.17 } catch (Exception ex) {
682 dl 1.8 unexpectedException();
683     } finally {
684     joinPool(e);
685     }
686     }
687    
688     /**
689 dl 1.12 * timed invokeAll(null time unit) throws NPE
690 dl 1.8 */
691     public void testTimedInvokeAllNullTimeUnit() {
692     ExecutorService e = new DirectExecutorService();
693     try {
694     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
695     l.add(new StringTask());
696     e.invokeAll(l, MEDIUM_DELAY_MS, null);
697     } catch (NullPointerException success) {
698 jsr166 1.17 } catch (Exception ex) {
699 dl 1.8 unexpectedException();
700     } finally {
701     joinPool(e);
702     }
703     }
704    
705     /**
706     * timed invokeAll(empty collection) returns empty collection
707     */
708     public void testTimedInvokeAll2() {
709     ExecutorService e = new DirectExecutorService();
710     try {
711     List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
712     assertTrue(r.isEmpty());
713 jsr166 1.17 } catch (Exception ex) {
714 dl 1.8 unexpectedException();
715     } finally {
716     joinPool(e);
717     }
718     }
719    
720     /**
721     * timed invokeAll(c) throws NPE if c has null elements
722     */
723     public void testTimedInvokeAll3() {
724     ExecutorService e = new DirectExecutorService();
725     try {
726     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
727     l.add(new StringTask());
728     l.add(null);
729     e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
730     } catch (NullPointerException success) {
731 jsr166 1.17 } catch (Exception ex) {
732 dl 1.8 unexpectedException();
733     } finally {
734     joinPool(e);
735     }
736     }
737    
738     /**
739 dl 1.12 * get of returned element of invokeAll(c) throws exception on failed task
740 dl 1.8 */
741     public void testTimedInvokeAll4() {
742     ExecutorService e = new DirectExecutorService();
743     try {
744     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
745     l.add(new NPETask());
746     List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
747     assertEquals(1, result.size());
748 jsr166 1.16 for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
749 dl 1.8 it.next().get();
750 jsr166 1.17 } catch (ExecutionException success) {
751     } catch (Exception ex) {
752 dl 1.8 unexpectedException();
753     } finally {
754     joinPool(e);
755     }
756     }
757    
758     /**
759 dl 1.12 * timed invokeAll(c) returns results of all completed tasks in c
760 dl 1.8 */
761     public void testTimedInvokeAll5() {
762     ExecutorService e = new DirectExecutorService();
763     try {
764     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
765     l.add(new StringTask());
766     l.add(new StringTask());
767     List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
768     assertEquals(2, result.size());
769 jsr166 1.16 for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
770 dl 1.8 assertSame(TEST_STRING, it.next().get());
771     } catch (ExecutionException success) {
772 jsr166 1.17 } catch (Exception ex) {
773 dl 1.8 unexpectedException();
774     } finally {
775     joinPool(e);
776     }
777     }
778    
779     /**
780 dl 1.12 * timed invokeAll cancels tasks not completed by timeout
781 dl 1.8 */
782     public void testTimedInvokeAll6() {
783 dl 1.9 ExecutorService e = new DirectExecutorService();
784 dl 1.8 try {
785     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
786     l.add(new StringTask());
787 dl 1.11 l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
788     l.add(new StringTask());
789 dl 1.14 List<Future<String>> result = e.invokeAll(l, SMALL_DELAY_MS, TimeUnit.MILLISECONDS);
790 dl 1.11 assertEquals(3, result.size());
791 jsr166 1.16 Iterator<Future<String>> it = result.iterator();
792 dl 1.8 Future<String> f1 = it.next();
793     Future<String> f2 = it.next();
794 dl 1.11 Future<String> f3 = it.next();
795 dl 1.8 assertTrue(f1.isDone());
796     assertFalse(f1.isCancelled());
797     assertTrue(f2.isDone());
798 dl 1.11 assertTrue(f3.isDone());
799     assertTrue(f3.isCancelled());
800 jsr166 1.17 } catch (Exception ex) {
801 dl 1.6 unexpectedException();
802     } finally {
803     joinPool(e);
804     }
805 tim 1.1 }
806    
807 dl 1.3 }