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

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8