ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractExecutorServiceTest.java
Revision: 1.19
Committed: Fri Nov 20 05:25:10 2009 UTC (14 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.18: +173 -272 lines
Log Message:
better exception handling

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 jsr166 1.18 public class AbstractExecutorServiceTest extends JSR166TestCase {
17 tim 1.1 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 jsr166 1.19 public void testExecuteRunnable() throws Exception {
42     ExecutorService e = new DirectExecutorService();
43     TrackedShortRunnable task = new TrackedShortRunnable();
44     assertFalse(task.done);
45     Future<?> future = e.submit(task);
46     future.get();
47     assertTrue(task.done);
48 tim 1.1 }
49    
50    
51     /**
52 dl 1.12 * Completed submit(callable) returns result
53 tim 1.1 */
54 jsr166 1.19 public void testSubmitCallable() throws Exception {
55     ExecutorService e = new DirectExecutorService();
56     Future<String> future = e.submit(new StringTask());
57     String result = future.get();
58     assertSame(TEST_STRING, result);
59 tim 1.1 }
60    
61 dl 1.6 /**
62 dl 1.12 * Completed submit(runnable) returns successfully
63 dl 1.6 */
64 jsr166 1.19 public void testSubmitRunnable() throws Exception {
65     ExecutorService e = new DirectExecutorService();
66     Future<?> future = e.submit(new NoOpRunnable());
67     future.get();
68     assertTrue(future.isDone());
69 dl 1.6 }
70    
71     /**
72 dl 1.12 * Completed submit(runnable, result) returns result
73 dl 1.6 */
74 jsr166 1.19 public void testSubmitRunnable2() throws Exception {
75     ExecutorService e = new DirectExecutorService();
76     Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
77     String result = future.get();
78     assertSame(TEST_STRING, result);
79 dl 1.6 }
80    
81 tim 1.1
82     /**
83 dl 1.12 * A submitted privileged action to completion
84 tim 1.1 */
85 jsr166 1.19 public void testSubmitPrivilegedAction() throws Exception {
86 dl 1.15 Policy savedPolicy = null;
87     try {
88     savedPolicy = Policy.getPolicy();
89     AdjustablePolicy policy = new AdjustablePolicy();
90     policy.addPermission(new RuntimePermission("getContextClassLoader"));
91     policy.addPermission(new RuntimePermission("setContextClassLoader"));
92     Policy.setPolicy(policy);
93 jsr166 1.17 } catch (AccessControlException ok) {
94 dl 1.15 return;
95     }
96 tim 1.1 try {
97     ExecutorService e = new DirectExecutorService();
98 dl 1.5 Future future = e.submit(Executors.callable(new PrivilegedAction() {
99 tim 1.1 public Object run() {
100     return TEST_STRING;
101 dl 1.5 }}));
102 tim 1.1
103     Object result = future.get();
104     assertSame(TEST_STRING, result);
105     }
106     finally {
107 dl 1.15 try {
108     Policy.setPolicy(savedPolicy);
109 jsr166 1.17 } catch (AccessControlException ok) {
110 dl 1.15 return;
111     }
112 tim 1.1 }
113     }
114    
115     /**
116 dl 1.12 * A submitted a privileged exception action runs to completion
117 tim 1.1 */
118 jsr166 1.19 public void testSubmitPrivilegedExceptionAction() throws Exception {
119 dl 1.15 Policy savedPolicy = null;
120     try {
121     savedPolicy = Policy.getPolicy();
122     AdjustablePolicy policy = new AdjustablePolicy();
123     policy.addPermission(new RuntimePermission("getContextClassLoader"));
124     policy.addPermission(new RuntimePermission("setContextClassLoader"));
125     Policy.setPolicy(policy);
126 jsr166 1.17 } catch (AccessControlException ok) {
127 dl 1.15 return;
128     }
129    
130 tim 1.1 try {
131     ExecutorService e = new DirectExecutorService();
132 dl 1.5 Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
133 tim 1.1 public Object run() {
134     return TEST_STRING;
135 dl 1.5 }}));
136 tim 1.1
137     Object result = future.get();
138     assertSame(TEST_STRING, result);
139     }
140     finally {
141     Policy.setPolicy(savedPolicy);
142     }
143     }
144    
145     /**
146 dl 1.12 * A submitted failed privileged exception action reports exception
147 tim 1.1 */
148 jsr166 1.19 public void testSubmitFailedPrivilegedExceptionAction() throws Exception {
149 dl 1.15 Policy savedPolicy = null;
150     try {
151     savedPolicy = Policy.getPolicy();
152     AdjustablePolicy policy = new AdjustablePolicy();
153     policy.addPermission(new RuntimePermission("getContextClassLoader"));
154     policy.addPermission(new RuntimePermission("setContextClassLoader"));
155     Policy.setPolicy(policy);
156 jsr166 1.17 } catch (AccessControlException ok) {
157 dl 1.15 return;
158     }
159    
160 tim 1.1 try {
161     ExecutorService e = new DirectExecutorService();
162 dl 1.5 Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
163 tim 1.1 public Object run() throws Exception {
164     throw new IndexOutOfBoundsException();
165 dl 1.5 }}));
166 tim 1.1
167 jsr166 1.19 future.get();
168 tim 1.1 shouldThrow();
169 jsr166 1.19 } catch (ExecutionException success) {
170     assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
171 tim 1.1 }
172     finally {
173     Policy.setPolicy(savedPolicy);
174     }
175     }
176    
177     /**
178 dl 1.12 * execute(null runnable) throws NPE
179 tim 1.1 */
180     public void testExecuteNullRunnable() {
181     try {
182     ExecutorService e = new DirectExecutorService();
183 jsr166 1.19 e.submit((Runnable) null);
184 tim 1.1 shouldThrow();
185 jsr166 1.19 } catch (NullPointerException success) {}
186 tim 1.1 }
187    
188    
189     /**
190 dl 1.12 * submit(null callable) throws NPE
191 tim 1.1 */
192 dl 1.6 public void testSubmitNullCallable() {
193 tim 1.1 try {
194     ExecutorService e = new DirectExecutorService();
195 jsr166 1.19 e.submit((Callable) null);
196 tim 1.1 shouldThrow();
197 jsr166 1.19 } catch (NullPointerException success) {}
198 tim 1.1 }
199    
200     /**
201 dl 1.12 * submit(runnable) throws RejectedExecutionException if
202     * executor is saturated.
203 tim 1.1 */
204     public void testExecute1() {
205 jsr166 1.19 ThreadPoolExecutor p =
206     new ThreadPoolExecutor(1, 1,
207     60, TimeUnit.SECONDS,
208     new ArrayBlockingQueue<Runnable>(1));
209 tim 1.1 try {
210 jsr166 1.19 for (int i = 0; i < 2; ++i)
211 dl 1.4 p.submit(new MediumRunnable());
212 jsr166 1.19 for (int i = 0; i < 2; ++i) {
213     try {
214     p.submit(new MediumRunnable());
215     shouldThrow();
216     } catch (RejectedExecutionException success) {}
217 tim 1.1 }
218 jsr166 1.19 } finally {
219     joinPool(p);
220     }
221 tim 1.1 }
222    
223     /**
224 dl 1.12 * submit(callable) throws RejectedExecutionException
225     * if executor is saturated.
226 tim 1.1 */
227     public void testExecute2() {
228 jsr166 1.19 ThreadPoolExecutor p =
229     new ThreadPoolExecutor(1, 1,
230     60, TimeUnit.SECONDS,
231     new ArrayBlockingQueue<Runnable>(1));
232 tim 1.1 try {
233 jsr166 1.19 for (int i = 0; i < 2; ++i)
234     p.submit(new MediumRunnable());
235     for (int i = 0; i < 2; ++i) {
236     try {
237     p.submit(new SmallCallable());
238     shouldThrow();
239     } catch (RejectedExecutionException success) {}
240 tim 1.1 }
241 jsr166 1.19 } finally {
242     joinPool(p);
243     }
244 tim 1.1 }
245    
246    
247     /**
248 dl 1.12 * Blocking on submit(callable) throws InterruptedException if
249 tim 1.1 * caller interrupted.
250     */
251 jsr166 1.19 public void testInterruptedSubmit() throws InterruptedException {
252 dl 1.13 final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
253 jsr166 1.19 Thread t = new Thread(new CheckedInterruptedRunnable() {
254     public void realRun() throws Exception {
255     p.submit(new CheckedCallable<Object>() {
256     public Object realCall()
257     throws InterruptedException {
258     Thread.sleep(SMALL_DELAY_MS);
259     return null;
260     }}).get();
261     }});
262    
263     t.start();
264     Thread.sleep(SHORT_DELAY_MS);
265     t.interrupt();
266 tim 1.1 joinPool(p);
267     }
268    
269     /**
270 jsr166 1.19 * get of submitted callable throws InterruptedException if callable
271 dl 1.6 * interrupted
272 tim 1.1 */
273 jsr166 1.19 public void testSubmitIE() throws InterruptedException {
274     final ThreadPoolExecutor p =
275     new ThreadPoolExecutor(1, 1,
276     60, TimeUnit.SECONDS,
277     new ArrayBlockingQueue<Runnable>(10));
278    
279     Thread t = new Thread(new CheckedInterruptedRunnable() {
280     public void realRun() throws Exception {
281     p.submit(new SmallCallable()).get();
282     }});
283    
284     t.start();
285     Thread.sleep(SHORT_DELAY_MS);
286     t.interrupt();
287     t.join();
288 tim 1.1 joinPool(p);
289     }
290    
291     /**
292 dl 1.12 * get of submit(callable) throws ExecutionException if callable
293     * throws exception
294 tim 1.1 */
295 jsr166 1.19 public void testSubmitEE() throws InterruptedException {
296     ThreadPoolExecutor p =
297     new ThreadPoolExecutor(1, 1,
298     60, TimeUnit.SECONDS,
299     new ArrayBlockingQueue<Runnable>(10));
300    
301     Callable c = new Callable() {
302     public Object call() { return 5/0; }};
303 tim 1.1
304     try {
305 jsr166 1.19 p.submit(c).get();
306 tim 1.1 shouldThrow();
307 jsr166 1.19 } catch (ExecutionException success) {
308     assertTrue(success.getCause() instanceof ArithmeticException);
309 tim 1.1 }
310     joinPool(p);
311 dl 1.6 }
312    
313     /**
314     * invokeAny(null) throws NPE
315     */
316 jsr166 1.19 public void testInvokeAny1()
317     throws InterruptedException, ExecutionException {
318 dl 1.6 ExecutorService e = new DirectExecutorService();
319     try {
320     e.invokeAny(null);
321 jsr166 1.19 shouldThrow();
322 dl 1.6 } catch (NullPointerException success) {
323     } finally {
324     joinPool(e);
325     }
326     }
327    
328     /**
329     * invokeAny(empty collection) throws IAE
330     */
331 jsr166 1.19 public void testInvokeAny2()
332     throws InterruptedException, ExecutionException {
333 dl 1.6 ExecutorService e = new DirectExecutorService();
334     try {
335     e.invokeAny(new ArrayList<Callable<String>>());
336 jsr166 1.19 shouldThrow();
337 dl 1.6 } catch (IllegalArgumentException success) {
338     } finally {
339     joinPool(e);
340     }
341     }
342    
343     /**
344     * invokeAny(c) throws NPE if c has null elements
345     */
346 jsr166 1.19 public void testInvokeAny3() throws Exception {
347     final CountDownLatch latch = new CountDownLatch(1);
348 dl 1.6 ExecutorService e = new DirectExecutorService();
349     try {
350 jsr166 1.19 ArrayList<Callable<Integer>> l
351     = new ArrayList<Callable<Integer>>();
352     l.add(new Callable<Integer>() {
353     public Integer call() { return 5/0; }});
354 dl 1.6 l.add(null);
355     e.invokeAny(l);
356 jsr166 1.19 shouldThrow();
357 dl 1.6 } catch (NullPointerException success) {
358     } finally {
359 jsr166 1.19 latch.countDown();
360 dl 1.6 joinPool(e);
361     }
362     }
363    
364     /**
365 dl 1.12 * invokeAny(c) throws ExecutionException if no task in c completes
366 dl 1.6 */
367 jsr166 1.19 public void testInvokeAny4() throws InterruptedException {
368 dl 1.6 ExecutorService e = new DirectExecutorService();
369     try {
370     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
371     l.add(new NPETask());
372 dl 1.8 e.invokeAny(l);
373 jsr166 1.19 shouldThrow();
374 jsr166 1.17 } catch (ExecutionException success) {
375 jsr166 1.19 assertTrue(success.getCause() instanceof NullPointerException);
376 dl 1.6 } finally {
377     joinPool(e);
378     }
379     }
380    
381     /**
382 dl 1.12 * invokeAny(c) returns result of some task in c if at least one completes
383 dl 1.6 */
384 jsr166 1.19 public void testInvokeAny5() throws Exception {
385 dl 1.6 ExecutorService e = new DirectExecutorService();
386     try {
387     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
388     l.add(new StringTask());
389     l.add(new StringTask());
390     String result = e.invokeAny(l);
391     assertSame(TEST_STRING, result);
392     } finally {
393     joinPool(e);
394     }
395     }
396    
397     /**
398     * invokeAll(null) throws NPE
399     */
400 jsr166 1.19 public void testInvokeAll1() throws InterruptedException {
401 dl 1.6 ExecutorService e = new DirectExecutorService();
402     try {
403     e.invokeAll(null);
404 jsr166 1.19 shouldThrow();
405 dl 1.6 } catch (NullPointerException success) {
406     } finally {
407     joinPool(e);
408     }
409     }
410    
411     /**
412     * invokeAll(empty collection) returns empty collection
413     */
414 jsr166 1.19 public void testInvokeAll2() throws InterruptedException {
415 dl 1.6 ExecutorService e = new DirectExecutorService();
416     try {
417     List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
418     assertTrue(r.isEmpty());
419     } finally {
420     joinPool(e);
421     }
422     }
423    
424     /**
425     * invokeAll(c) throws NPE if c has null elements
426     */
427 jsr166 1.19 public void testInvokeAll3() throws InterruptedException {
428 dl 1.6 ExecutorService e = new DirectExecutorService();
429     try {
430     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
431     l.add(new StringTask());
432     l.add(null);
433     e.invokeAll(l);
434 jsr166 1.19 shouldThrow();
435 dl 1.6 } catch (NullPointerException success) {
436     } finally {
437     joinPool(e);
438     }
439     }
440    
441     /**
442 dl 1.12 * get of returned element of invokeAll(c) throws exception on failed task
443 dl 1.6 */
444 jsr166 1.19 public void testInvokeAll4() throws Exception {
445 dl 1.6 ExecutorService e = new DirectExecutorService();
446     try {
447     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
448     l.add(new NPETask());
449     List<Future<String>> result = e.invokeAll(l);
450     assertEquals(1, result.size());
451 jsr166 1.19 for (Future<String> future : result) {
452     try {
453     future.get();
454     shouldThrow();
455     } catch (ExecutionException success) {
456     Throwable cause = success.getCause();
457     assertTrue(cause instanceof NullPointerException);
458     }
459     }
460 dl 1.6 } finally {
461     joinPool(e);
462     }
463     }
464    
465     /**
466 dl 1.12 * invokeAll(c) returns results of all completed tasks in c
467 dl 1.6 */
468 jsr166 1.19 public void testInvokeAll5() throws Exception {
469 dl 1.6 ExecutorService e = new DirectExecutorService();
470     try {
471     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
472     l.add(new StringTask());
473     l.add(new StringTask());
474     List<Future<String>> result = e.invokeAll(l);
475     assertEquals(2, result.size());
476 jsr166 1.19 for (Future<String> future : result)
477     assertSame(TEST_STRING, future.get());
478 dl 1.8 } finally {
479     joinPool(e);
480     }
481     }
482    
483    
484     /**
485     * timed invokeAny(null) throws NPE
486     */
487 jsr166 1.19 public void testTimedInvokeAny1() throws Exception {
488 dl 1.8 ExecutorService e = new DirectExecutorService();
489     try {
490     e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
491 jsr166 1.19 shouldThrow();
492 dl 1.8 } catch (NullPointerException success) {
493     } finally {
494     joinPool(e);
495     }
496     }
497    
498     /**
499 dl 1.12 * timed invokeAny(null time unit) throws NPE
500 dl 1.8 */
501 jsr166 1.19 public void testTimedInvokeAnyNullTimeUnit() throws Exception {
502 dl 1.8 ExecutorService e = new DirectExecutorService();
503     try {
504     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
505     l.add(new StringTask());
506     e.invokeAny(l, MEDIUM_DELAY_MS, null);
507 jsr166 1.19 shouldThrow();
508 dl 1.8 } catch (NullPointerException success) {
509     } finally {
510     joinPool(e);
511     }
512     }
513    
514     /**
515     * timed invokeAny(empty collection) throws IAE
516     */
517 jsr166 1.19 public void testTimedInvokeAny2() throws Exception {
518 dl 1.8 ExecutorService e = new DirectExecutorService();
519     try {
520     e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
521 jsr166 1.19 shouldThrow();
522 dl 1.8 } catch (IllegalArgumentException success) {
523     } finally {
524     joinPool(e);
525     }
526     }
527    
528     /**
529     * timed invokeAny(c) throws NPE if c has null elements
530     */
531 jsr166 1.19 public void testTimedInvokeAny3() throws Exception {
532     final CountDownLatch latch = new CountDownLatch(1);
533 dl 1.8 ExecutorService e = new DirectExecutorService();
534     try {
535 jsr166 1.19 ArrayList<Callable<Integer>> l
536     = new ArrayList<Callable<Integer>>();
537     l.add(new Callable<Integer>() {
538     public Integer call() { return 5/0; }});
539 dl 1.8 l.add(null);
540     e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
541 jsr166 1.19 shouldThrow();
542 dl 1.8 } catch (NullPointerException success) {
543     } finally {
544 jsr166 1.19 latch.countDown();
545 dl 1.8 joinPool(e);
546     }
547     }
548    
549     /**
550     * timed invokeAny(c) throws ExecutionException if no task completes
551     */
552 jsr166 1.19 public void testTimedInvokeAny4() throws Exception {
553 dl 1.8 ExecutorService e = new DirectExecutorService();
554     try {
555     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
556     l.add(new NPETask());
557     e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
558 jsr166 1.19 shouldThrow();
559 jsr166 1.17 } catch (ExecutionException success) {
560 jsr166 1.19 assertTrue(success.getCause() instanceof NullPointerException);
561 dl 1.8 } finally {
562     joinPool(e);
563     }
564     }
565    
566     /**
567 dl 1.12 * timed invokeAny(c) returns result of some task in c
568 dl 1.8 */
569 jsr166 1.19 public void testTimedInvokeAny5() throws Exception {
570 dl 1.8 ExecutorService e = new DirectExecutorService();
571     try {
572     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
573     l.add(new StringTask());
574     l.add(new StringTask());
575     String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
576     assertSame(TEST_STRING, result);
577     } finally {
578     joinPool(e);
579     }
580     }
581    
582     /**
583     * timed invokeAll(null) throws NPE
584     */
585 jsr166 1.19 public void testTimedInvokeAll1() throws InterruptedException {
586 dl 1.8 ExecutorService e = new DirectExecutorService();
587     try {
588     e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
589 jsr166 1.19 shouldThrow();
590 dl 1.8 } catch (NullPointerException success) {
591     } finally {
592     joinPool(e);
593     }
594     }
595    
596     /**
597 dl 1.12 * timed invokeAll(null time unit) throws NPE
598 dl 1.8 */
599 jsr166 1.19 public void testTimedInvokeAllNullTimeUnit() throws InterruptedException {
600 dl 1.8 ExecutorService e = new DirectExecutorService();
601     try {
602     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
603     l.add(new StringTask());
604     e.invokeAll(l, MEDIUM_DELAY_MS, null);
605 jsr166 1.19 shouldThrow();
606 dl 1.8 } catch (NullPointerException success) {
607     } finally {
608     joinPool(e);
609     }
610     }
611    
612     /**
613     * timed invokeAll(empty collection) returns empty collection
614     */
615 jsr166 1.19 public void testTimedInvokeAll2() throws InterruptedException {
616 dl 1.8 ExecutorService e = new DirectExecutorService();
617     try {
618     List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
619     assertTrue(r.isEmpty());
620     } finally {
621     joinPool(e);
622     }
623     }
624    
625     /**
626     * timed invokeAll(c) throws NPE if c has null elements
627     */
628 jsr166 1.19 public void testTimedInvokeAll3() throws InterruptedException {
629 dl 1.8 ExecutorService e = new DirectExecutorService();
630     try {
631     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
632     l.add(new StringTask());
633     l.add(null);
634     e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
635 jsr166 1.19 shouldThrow();
636 dl 1.8 } catch (NullPointerException success) {
637     } finally {
638     joinPool(e);
639     }
640     }
641    
642     /**
643 dl 1.12 * get of returned element of invokeAll(c) throws exception on failed task
644 dl 1.8 */
645 jsr166 1.19 public void testTimedInvokeAll4() throws Exception {
646 dl 1.8 ExecutorService e = new DirectExecutorService();
647     try {
648     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
649     l.add(new NPETask());
650     List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
651     assertEquals(1, result.size());
652 jsr166 1.19 for (Future<String> future : result) {
653     try {
654     future.get();
655     } catch (ExecutionException success) {
656     assertTrue(success.getCause() instanceof NullPointerException);
657     }
658     }
659 dl 1.8 } finally {
660     joinPool(e);
661     }
662     }
663    
664     /**
665 dl 1.12 * timed invokeAll(c) returns results of all completed tasks in c
666 dl 1.8 */
667 jsr166 1.19 public void testTimedInvokeAll5() throws Exception {
668 dl 1.8 ExecutorService e = new DirectExecutorService();
669     try {
670     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
671     l.add(new StringTask());
672     l.add(new StringTask());
673     List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
674     assertEquals(2, result.size());
675 jsr166 1.19 for (Future<String> future : result)
676     assertSame(TEST_STRING, future.get());
677 dl 1.8 } finally {
678     joinPool(e);
679     }
680     }
681    
682     /**
683 dl 1.12 * timed invokeAll cancels tasks not completed by timeout
684 dl 1.8 */
685 jsr166 1.19 public void testTimedInvokeAll6() throws InterruptedException {
686 dl 1.9 ExecutorService e = new DirectExecutorService();
687 dl 1.8 try {
688     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
689     l.add(new StringTask());
690 dl 1.11 l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
691     l.add(new StringTask());
692 dl 1.14 List<Future<String>> result = e.invokeAll(l, SMALL_DELAY_MS, TimeUnit.MILLISECONDS);
693 dl 1.11 assertEquals(3, result.size());
694 jsr166 1.16 Iterator<Future<String>> it = result.iterator();
695 dl 1.8 Future<String> f1 = it.next();
696     Future<String> f2 = it.next();
697 dl 1.11 Future<String> f3 = it.next();
698 dl 1.8 assertTrue(f1.isDone());
699     assertFalse(f1.isCancelled());
700     assertTrue(f2.isDone());
701 dl 1.11 assertTrue(f3.isDone());
702     assertTrue(f3.isCancelled());
703 dl 1.6 } finally {
704     joinPool(e);
705     }
706 tim 1.1 }
707    
708 dl 1.3 }