[cvs] / jsr166 / src / test / tck / AbstractExecutorServiceTest.java Repository:
ViewVC logotype

Annotation of /jsr166/src/test/tck/AbstractExecutorServiceTest.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.38 - (view) (download)

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 : jsr166 1.30 * http://creativecommons.org/publicdomain/zero/1.0/
5 : jsr166 1.16 * Other contributors include Andrew Wright, Jeffrey Hayes,
6 :     * Pat Fisher, Mike Judd.
7 : tim 1.1 */
8 :    
9 : jsr166 1.35 import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 :    
11 :     import java.security.PrivilegedAction;
12 :     import java.security.PrivilegedExceptionAction;
13 :     import java.util.ArrayList;
14 :     import java.util.Collections;
15 :     import java.util.List;
16 :     import java.util.concurrent.AbstractExecutorService;
17 :     import java.util.concurrent.ArrayBlockingQueue;
18 :     import java.util.concurrent.Callable;
19 : jsr166 1.38 import java.util.concurrent.CancellationException;
20 : jsr166 1.35 import java.util.concurrent.CountDownLatch;
21 :     import java.util.concurrent.ExecutionException;
22 :     import java.util.concurrent.Executors;
23 :     import java.util.concurrent.ExecutorService;
24 :     import java.util.concurrent.Future;
25 :     import java.util.concurrent.ThreadPoolExecutor;
26 :     import java.util.concurrent.TimeUnit;
27 : jsr166 1.32 import java.util.concurrent.atomic.AtomicBoolean;
28 : jsr166 1.35
29 :     import junit.framework.Test;
30 :     import junit.framework.TestSuite;
31 : tim 1.1
32 : jsr166 1.18 public class AbstractExecutorServiceTest extends JSR166TestCase {
33 : tim 1.1 public static void main(String[] args) {
34 : jsr166 1.37 main(suite(), args);
35 : tim 1.1 }
36 :     public static Test suite() {
37 : dl 1.6 return new TestSuite(AbstractExecutorServiceTest.class);
38 : tim 1.1 }
39 :    
40 : jsr166 1.16 /**
41 : tim 1.1 * A no-frills implementation of AbstractExecutorService, designed
42 : dl 1.6 * to test the submit methods only.
43 : tim 1.1 */
44 :     static class DirectExecutorService extends AbstractExecutorService {
45 :     public void execute(Runnable r) { r.run(); }
46 :     public void shutdown() { shutdown = true; }
47 : jsr166 1.26 public List<Runnable> shutdownNow() {
48 :     shutdown = true;
49 :     return Collections.EMPTY_LIST;
50 :     }
51 : tim 1.1 public boolean isShutdown() { return shutdown; }
52 :     public boolean isTerminated() { return isShutdown(); }
53 : jsr166 1.26 public boolean awaitTermination(long timeout, TimeUnit unit) {
54 :     return isShutdown();
55 :     }
56 : tim 1.1 private volatile boolean shutdown = false;
57 :     }
58 :    
59 :     /**
60 : dl 1.12 * execute(runnable) runs it to completion
61 : tim 1.1 */
62 : jsr166 1.19 public void testExecuteRunnable() throws Exception {
63 :     ExecutorService e = new DirectExecutorService();
64 : jsr166 1.32 final AtomicBoolean done = new AtomicBoolean(false);
65 : jsr166 1.34 Future<?> future = e.submit(new CheckedRunnable() {
66 : jsr166 1.32 public void realRun() {
67 :     done.set(true);
68 : jsr166 1.34 }});
69 : jsr166 1.32 assertNull(future.get());
70 :     assertNull(future.get(0, MILLISECONDS));
71 :     assertTrue(done.get());
72 :     assertTrue(future.isDone());
73 :     assertFalse(future.isCancelled());
74 : tim 1.1 }
75 :    
76 :     /**
77 : dl 1.12 * Completed submit(callable) returns result
78 : tim 1.1 */
79 : jsr166 1.19 public void testSubmitCallable() throws Exception {
80 :     ExecutorService e = new DirectExecutorService();
81 :     Future<String> future = e.submit(new StringTask());
82 :     String result = future.get();
83 :     assertSame(TEST_STRING, result);
84 : tim 1.1 }
85 :    
86 : dl 1.6 /**
87 : dl 1.12 * Completed submit(runnable) returns successfully
88 : dl 1.6 */
89 : jsr166 1.19 public void testSubmitRunnable() throws Exception {
90 :     ExecutorService e = new DirectExecutorService();
91 :     Future<?> future = e.submit(new NoOpRunnable());
92 :     future.get();
93 :     assertTrue(future.isDone());
94 : dl 1.6 }
95 :    
96 :     /**
97 : dl 1.12 * Completed submit(runnable, result) returns result
98 : dl 1.6 */
99 : jsr166 1.19 public void testSubmitRunnable2() throws Exception {
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 : dl 1.6 }
105 :    
106 : tim 1.1 /**
107 : jsr166 1.23 * A submitted privileged action runs to completion
108 : tim 1.1 */
109 : jsr166 1.19 public void testSubmitPrivilegedAction() throws Exception {
110 : jsr166 1.23 Runnable r = new CheckedRunnable() {
111 :     public void realRun() throws Exception {
112 :     ExecutorService e = new DirectExecutorService();
113 :     Future future = e.submit(Executors.callable(new PrivilegedAction() {
114 : tim 1.1 public Object run() {
115 :     return TEST_STRING;
116 : dl 1.5 }}));
117 : tim 1.1
118 : jsr166 1.23 assertSame(TEST_STRING, future.get());
119 :     }};
120 :    
121 :     runWithPermissions(r,
122 :     new RuntimePermission("getClassLoader"),
123 :     new RuntimePermission("setContextClassLoader"),
124 :     new RuntimePermission("modifyThread"));
125 : tim 1.1 }
126 :    
127 :     /**
128 : jsr166 1.23 * A submitted privileged exception action runs to completion
129 : tim 1.1 */
130 : jsr166 1.19 public void testSubmitPrivilegedExceptionAction() throws Exception {
131 : jsr166 1.23 Runnable r = new CheckedRunnable() {
132 :     public void realRun() throws Exception {
133 :     ExecutorService e = new DirectExecutorService();
134 :     Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
135 : tim 1.1 public Object run() {
136 :     return TEST_STRING;
137 : dl 1.5 }}));
138 : tim 1.1
139 : jsr166 1.23 assertSame(TEST_STRING, future.get());
140 :     }};
141 :    
142 :     runWithPermissions(r);
143 : tim 1.1 }
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 : jsr166 1.23 Runnable r = new CheckedRunnable() {
150 :     public void realRun() throws Exception {
151 :     ExecutorService e = new DirectExecutorService();
152 :     Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
153 : tim 1.1 public Object run() throws Exception {
154 :     throw new IndexOutOfBoundsException();
155 : dl 1.5 }}));
156 : tim 1.1
157 : jsr166 1.23 try {
158 :     future.get();
159 :     shouldThrow();
160 :     } catch (ExecutionException success) {
161 :     assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
162 :     }}};
163 :    
164 :     runWithPermissions(r);
165 : tim 1.1 }
166 :    
167 :     /**
168 : dl 1.12 * execute(null runnable) throws NPE
169 : tim 1.1 */
170 :     public void testExecuteNullRunnable() {
171 : jsr166 1.36 ExecutorService e = new DirectExecutorService();
172 : tim 1.1 try {
173 : jsr166 1.19 e.submit((Runnable) null);
174 : tim 1.1 shouldThrow();
175 : jsr166 1.19 } catch (NullPointerException success) {}
176 : tim 1.1 }
177 :    
178 :     /**
179 : dl 1.12 * submit(null callable) throws NPE
180 : tim 1.1 */
181 : dl 1.6 public void testSubmitNullCallable() {
182 : jsr166 1.36 ExecutorService e = new DirectExecutorService();
183 : tim 1.1 try {
184 : jsr166 1.19 e.submit((Callable) null);
185 : tim 1.1 shouldThrow();
186 : jsr166 1.19 } catch (NullPointerException success) {}
187 : tim 1.1 }
188 :    
189 :     /**
190 : jsr166 1.25 * submit(callable).get() throws InterruptedException if interrupted
191 : tim 1.1 */
192 : jsr166 1.19 public void testInterruptedSubmit() throws InterruptedException {
193 : jsr166 1.25 final CountDownLatch submitted = new CountDownLatch(1);
194 :     final CountDownLatch quittingTime = new CountDownLatch(1);
195 :     final ExecutorService p
196 :     = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS,
197 :     new ArrayBlockingQueue<Runnable>(10));
198 :     final Callable<Void> awaiter = new CheckedCallable<Void>() {
199 :     public Void realCall() throws InterruptedException {
200 :     quittingTime.await();
201 :     return null;
202 :     }};
203 :     try {
204 :     Thread t = new Thread(new CheckedInterruptedRunnable() {
205 :     public void realRun() throws Exception {
206 :     Future<Void> future = p.submit(awaiter);
207 :     submitted.countDown();
208 :     future.get();
209 :     }});
210 :     t.start();
211 :     submitted.await();
212 :     t.interrupt();
213 :     t.join();
214 :     } finally {
215 :     quittingTime.countDown();
216 :     joinPool(p);
217 :     }
218 : tim 1.1 }
219 :    
220 :     /**
221 : jsr166 1.27 * get of submit(callable) throws ExecutionException if callable
222 :     * throws exception
223 : tim 1.1 */
224 : jsr166 1.19 public void testSubmitEE() throws InterruptedException {
225 :     ThreadPoolExecutor p =
226 :     new ThreadPoolExecutor(1, 1,
227 :     60, TimeUnit.SECONDS,
228 :     new ArrayBlockingQueue<Runnable>(10));
229 :    
230 :     Callable c = new Callable() {
231 : jsr166 1.33 public Object call() { throw new ArithmeticException(); }};
232 : tim 1.1
233 :     try {
234 : jsr166 1.19 p.submit(c).get();
235 : tim 1.1 shouldThrow();
236 : jsr166 1.19 } catch (ExecutionException success) {
237 :     assertTrue(success.getCause() instanceof ArithmeticException);
238 : tim 1.1 }
239 :     joinPool(p);
240 : dl 1.6 }
241 :    
242 :     /**
243 :     * invokeAny(null) throws NPE
244 :     */
245 : jsr166 1.28 public void testInvokeAny1() throws Exception {
246 : dl 1.6 ExecutorService e = new DirectExecutorService();
247 :     try {
248 :     e.invokeAny(null);
249 : jsr166 1.19 shouldThrow();
250 : dl 1.6 } catch (NullPointerException success) {
251 :     } finally {
252 :     joinPool(e);
253 :     }
254 :     }
255 :    
256 :     /**
257 :     * invokeAny(empty collection) throws IAE
258 :     */
259 : jsr166 1.28 public void testInvokeAny2() throws Exception {
260 : dl 1.6 ExecutorService e = new DirectExecutorService();
261 :     try {
262 :     e.invokeAny(new ArrayList<Callable<String>>());
263 : jsr166 1.19 shouldThrow();
264 : dl 1.6 } catch (IllegalArgumentException success) {
265 :     } finally {
266 :     joinPool(e);
267 :     }
268 :     }
269 :    
270 :     /**
271 :     * invokeAny(c) throws NPE if c has null elements
272 :     */
273 : jsr166 1.19 public void testInvokeAny3() throws Exception {
274 : dl 1.6 ExecutorService e = new DirectExecutorService();
275 : jsr166 1.33 List<Callable<Long>> l = new ArrayList<Callable<Long>>();
276 :     l.add(new Callable<Long>() {
277 :     public Long call() { throw new ArithmeticException(); }});
278 : jsr166 1.22 l.add(null);
279 : dl 1.6 try {
280 :     e.invokeAny(l);
281 : jsr166 1.19 shouldThrow();
282 : dl 1.6 } catch (NullPointerException success) {
283 :     } finally {
284 :     joinPool(e);
285 :     }
286 :     }
287 :    
288 :     /**
289 : dl 1.12 * invokeAny(c) throws ExecutionException if no task in c completes
290 : dl 1.6 */
291 : jsr166 1.19 public void testInvokeAny4() throws InterruptedException {
292 : dl 1.6 ExecutorService e = new DirectExecutorService();
293 : jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
294 :     l.add(new NPETask());
295 : dl 1.6 try {
296 : dl 1.8 e.invokeAny(l);
297 : jsr166 1.19 shouldThrow();
298 : jsr166 1.17 } catch (ExecutionException success) {
299 : jsr166 1.19 assertTrue(success.getCause() instanceof NullPointerException);
300 : dl 1.6 } finally {
301 :     joinPool(e);
302 :     }
303 :     }
304 :    
305 :     /**
306 : dl 1.12 * invokeAny(c) returns result of some task in c if at least one completes
307 : dl 1.6 */
308 : jsr166 1.19 public void testInvokeAny5() throws Exception {
309 : dl 1.6 ExecutorService e = new DirectExecutorService();
310 :     try {
311 : jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
312 : dl 1.6 l.add(new StringTask());
313 :     l.add(new StringTask());
314 :     String result = e.invokeAny(l);
315 :     assertSame(TEST_STRING, result);
316 :     } finally {
317 :     joinPool(e);
318 :     }
319 :     }
320 :    
321 :     /**
322 :     * invokeAll(null) throws NPE
323 :     */
324 : jsr166 1.19 public void testInvokeAll1() throws InterruptedException {
325 : dl 1.6 ExecutorService e = new DirectExecutorService();
326 :     try {
327 :     e.invokeAll(null);
328 : jsr166 1.19 shouldThrow();
329 : dl 1.6 } catch (NullPointerException success) {
330 :     } finally {
331 :     joinPool(e);
332 :     }
333 :     }
334 :    
335 :     /**
336 :     * invokeAll(empty collection) returns empty collection
337 :     */
338 : jsr166 1.19 public void testInvokeAll2() throws InterruptedException {
339 : dl 1.6 ExecutorService e = new DirectExecutorService();
340 :     try {
341 :     List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
342 :     assertTrue(r.isEmpty());
343 :     } finally {
344 :     joinPool(e);
345 :     }
346 :     }
347 :    
348 :     /**
349 :     * invokeAll(c) throws NPE if c has null elements
350 :     */
351 : jsr166 1.19 public void testInvokeAll3() throws InterruptedException {
352 : dl 1.6 ExecutorService e = new DirectExecutorService();
353 : jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
354 :     l.add(new StringTask());
355 :     l.add(null);
356 : dl 1.6 try {
357 :     e.invokeAll(l);
358 : jsr166 1.19 shouldThrow();
359 : dl 1.6 } catch (NullPointerException success) {
360 :     } finally {
361 :     joinPool(e);
362 :     }
363 :     }
364 :    
365 :     /**
366 : dl 1.12 * get of returned element of invokeAll(c) throws exception on failed task
367 : dl 1.6 */
368 : jsr166 1.19 public void testInvokeAll4() throws Exception {
369 : dl 1.6 ExecutorService e = new DirectExecutorService();
370 :     try {
371 : jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
372 : dl 1.6 l.add(new NPETask());
373 : jsr166 1.22 List<Future<String>> futures = e.invokeAll(l);
374 :     assertEquals(1, futures.size());
375 :     try {
376 :     futures.get(0).get();
377 :     shouldThrow();
378 :     } catch (ExecutionException success) {
379 :     assertTrue(success.getCause() instanceof NullPointerException);
380 : jsr166 1.19 }
381 : dl 1.6 } finally {
382 :     joinPool(e);
383 :     }
384 :     }
385 :    
386 :     /**
387 : dl 1.12 * invokeAll(c) returns results of all completed tasks in c
388 : dl 1.6 */
389 : jsr166 1.19 public void testInvokeAll5() throws Exception {
390 : dl 1.6 ExecutorService e = new DirectExecutorService();
391 :     try {
392 : jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
393 : dl 1.6 l.add(new StringTask());
394 :     l.add(new StringTask());
395 : jsr166 1.22 List<Future<String>> futures = e.invokeAll(l);
396 :     assertEquals(2, futures.size());
397 :     for (Future<String> future : futures)
398 : jsr166 1.19 assertSame(TEST_STRING, future.get());
399 : dl 1.8 } finally {
400 :     joinPool(e);
401 :     }
402 :     }
403 :    
404 :     /**
405 :     * timed invokeAny(null) throws NPE
406 :     */
407 : jsr166 1.19 public void testTimedInvokeAny1() throws Exception {
408 : dl 1.8 ExecutorService e = new DirectExecutorService();
409 :     try {
410 : jsr166 1.20 e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
411 : jsr166 1.19 shouldThrow();
412 : dl 1.8 } catch (NullPointerException success) {
413 :     } finally {
414 :     joinPool(e);
415 :     }
416 :     }
417 :    
418 :     /**
419 : dl 1.12 * timed invokeAny(null time unit) throws NPE
420 : dl 1.8 */
421 : jsr166 1.19 public void testTimedInvokeAnyNullTimeUnit() throws Exception {
422 : dl 1.8 ExecutorService e = new DirectExecutorService();
423 : jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
424 :     l.add(new StringTask());
425 : dl 1.8 try {
426 :     e.invokeAny(l, MEDIUM_DELAY_MS, null);
427 : jsr166 1.19 shouldThrow();
428 : dl 1.8 } catch (NullPointerException success) {
429 :     } finally {
430 :     joinPool(e);
431 :     }
432 :     }
433 :    
434 :     /**
435 :     * timed invokeAny(empty collection) throws IAE
436 :     */
437 : jsr166 1.19 public void testTimedInvokeAny2() throws Exception {
438 : dl 1.8 ExecutorService e = new DirectExecutorService();
439 :     try {
440 : jsr166 1.20 e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
441 : jsr166 1.19 shouldThrow();
442 : dl 1.8 } catch (IllegalArgumentException success) {
443 :     } finally {
444 :     joinPool(e);
445 :     }
446 :     }
447 :    
448 :     /**
449 :     * timed invokeAny(c) throws NPE if c has null elements
450 :     */
451 : jsr166 1.19 public void testTimedInvokeAny3() throws Exception {
452 : dl 1.8 ExecutorService e = new DirectExecutorService();
453 : jsr166 1.33 List<Callable<Long>> l = new ArrayList<Callable<Long>>();
454 :     l.add(new Callable<Long>() {
455 :     public Long call() { throw new ArithmeticException(); }});
456 : jsr166 1.22 l.add(null);
457 : dl 1.8 try {
458 : jsr166 1.20 e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
459 : jsr166 1.19 shouldThrow();
460 : dl 1.8 } catch (NullPointerException success) {
461 :     } finally {
462 :     joinPool(e);
463 :     }
464 :     }
465 :    
466 :     /**
467 :     * timed invokeAny(c) throws ExecutionException if no task completes
468 :     */
469 : jsr166 1.19 public void testTimedInvokeAny4() throws Exception {
470 : dl 1.8 ExecutorService e = new DirectExecutorService();
471 : jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
472 :     l.add(new NPETask());
473 : dl 1.8 try {
474 : jsr166 1.20 e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
475 : jsr166 1.19 shouldThrow();
476 : jsr166 1.17 } catch (ExecutionException success) {
477 : jsr166 1.19 assertTrue(success.getCause() instanceof NullPointerException);
478 : dl 1.8 } finally {
479 :     joinPool(e);
480 :     }
481 :     }
482 :    
483 :     /**
484 : dl 1.12 * timed invokeAny(c) returns result of some task in c
485 : dl 1.8 */
486 : jsr166 1.19 public void testTimedInvokeAny5() throws Exception {
487 : dl 1.8 ExecutorService e = new DirectExecutorService();
488 :     try {
489 : jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
490 : dl 1.8 l.add(new StringTask());
491 :     l.add(new StringTask());
492 : jsr166 1.20 String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
493 : dl 1.8 assertSame(TEST_STRING, result);
494 :     } finally {
495 :     joinPool(e);
496 :     }
497 :     }
498 :    
499 :     /**
500 :     * timed invokeAll(null) throws NPE
501 :     */
502 : jsr166 1.19 public void testTimedInvokeAll1() throws InterruptedException {
503 : dl 1.8 ExecutorService e = new DirectExecutorService();
504 :     try {
505 : jsr166 1.20 e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
506 : jsr166 1.19 shouldThrow();
507 : dl 1.8 } catch (NullPointerException success) {
508 :     } finally {
509 :     joinPool(e);
510 :     }
511 :     }
512 :    
513 :     /**
514 : dl 1.12 * timed invokeAll(null time unit) throws NPE
515 : dl 1.8 */
516 : jsr166 1.19 public void testTimedInvokeAllNullTimeUnit() throws InterruptedException {
517 : dl 1.8 ExecutorService e = new DirectExecutorService();
518 : jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
519 :     l.add(new StringTask());
520 : dl 1.8 try {
521 :     e.invokeAll(l, MEDIUM_DELAY_MS, null);
522 : jsr166 1.19 shouldThrow();
523 : dl 1.8 } catch (NullPointerException success) {
524 :     } finally {
525 :     joinPool(e);
526 :     }
527 :     }
528 :    
529 :     /**
530 :     * timed invokeAll(empty collection) returns empty collection
531 :     */
532 : jsr166 1.19 public void testTimedInvokeAll2() throws InterruptedException {
533 : dl 1.8 ExecutorService e = new DirectExecutorService();
534 :     try {
535 : jsr166 1.20 List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
536 : dl 1.8 assertTrue(r.isEmpty());
537 :     } finally {
538 :     joinPool(e);
539 :     }
540 :     }
541 :    
542 :     /**
543 :     * timed invokeAll(c) throws NPE if c has null elements
544 :     */
545 : jsr166 1.19 public void testTimedInvokeAll3() throws InterruptedException {
546 : dl 1.8 ExecutorService e = new DirectExecutorService();
547 : jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
548 :     l.add(new StringTask());
549 :     l.add(null);
550 : dl 1.8 try {
551 : jsr166 1.20 e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
552 : jsr166 1.19 shouldThrow();
553 : dl 1.8 } catch (NullPointerException success) {
554 :     } finally {
555 :     joinPool(e);
556 :     }
557 :     }
558 :    
559 :     /**
560 : dl 1.12 * get of returned element of invokeAll(c) throws exception on failed task
561 : dl 1.8 */
562 : jsr166 1.19 public void testTimedInvokeAll4() throws Exception {
563 : dl 1.8 ExecutorService e = new DirectExecutorService();
564 :     try {
565 : jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
566 : dl 1.8 l.add(new NPETask());
567 : jsr166 1.22 List<Future<String>> futures =
568 :     e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
569 :     assertEquals(1, futures.size());
570 :     try {
571 :     futures.get(0).get();
572 :     shouldThrow();
573 :     } catch (ExecutionException success) {
574 :     assertTrue(success.getCause() instanceof NullPointerException);
575 : jsr166 1.19 }
576 : dl 1.8 } finally {
577 :     joinPool(e);
578 :     }
579 :     }
580 :    
581 :     /**
582 : dl 1.12 * timed invokeAll(c) returns results of all completed tasks in c
583 : dl 1.8 */
584 : jsr166 1.19 public void testTimedInvokeAll5() throws Exception {
585 : dl 1.8 ExecutorService e = new DirectExecutorService();
586 :     try {
587 : jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
588 : dl 1.8 l.add(new StringTask());
589 :     l.add(new StringTask());
590 : jsr166 1.22 List<Future<String>> futures =
591 : jsr166 1.38 e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
592 : jsr166 1.22 assertEquals(2, futures.size());
593 :     for (Future<String> future : futures)
594 : jsr166 1.19 assertSame(TEST_STRING, future.get());
595 : dl 1.8 } finally {
596 :     joinPool(e);
597 :     }
598 :     }
599 :    
600 :     /**
601 : dl 1.12 * timed invokeAll cancels tasks not completed by timeout
602 : dl 1.8 */
603 : jsr166 1.38 public void testTimedInvokeAll6() throws Exception {
604 : dl 1.9 ExecutorService e = new DirectExecutorService();
605 : dl 1.8 try {
606 : jsr166 1.38 long timeout = timeoutMillis();
607 :     List<Callable<String>> tasks = new ArrayList<>();
608 :     tasks.add(new StringTask("0"));
609 :     tasks.add(Executors.callable(possiblyInterruptedRunnable(timeout),
610 :     TEST_STRING));
611 :     tasks.add(new StringTask("2"));
612 :     long startTime = System.nanoTime();
613 : jsr166 1.22 List<Future<String>> futures =
614 : jsr166 1.38 e.invokeAll(tasks, timeout, MILLISECONDS);
615 :     assertEquals(tasks.size(), futures.size());
616 :     assertTrue(millisElapsedSince(startTime) >= timeout);
617 : jsr166 1.32 for (Future future : futures)
618 :     assertTrue(future.isDone());
619 : jsr166 1.38 assertEquals("0", futures.get(0).get());
620 :     assertEquals(TEST_STRING, futures.get(1).get());
621 : jsr166 1.32 assertTrue(futures.get(2).isCancelled());
622 : dl 1.6 } finally {
623 :     joinPool(e);
624 :     }
625 : tim 1.1 }
626 :    
627 : dl 1.3 }

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8