[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.40 - (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 Callable<Void> awaiter = new CheckedCallable<Void>() {
196 :     public Void realCall() throws InterruptedException {
197 :     quittingTime.await();
198 :     return null;
199 :     }};
200 : jsr166 1.40 final ExecutorService p
201 :     = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS,
202 :     new ArrayBlockingQueue<Runnable>(10));
203 :     try (PoolCleaner cleaner = cleaner(p)) {
204 : jsr166 1.25 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 :     quittingTime.countDown();
215 :     }
216 : tim 1.1 }
217 :    
218 :     /**
219 : jsr166 1.27 * get of submit(callable) throws ExecutionException if callable
220 :     * throws exception
221 : tim 1.1 */
222 : jsr166 1.19 public void testSubmitEE() throws InterruptedException {
223 : jsr166 1.40 final ThreadPoolExecutor p =
224 : jsr166 1.19 new ThreadPoolExecutor(1, 1,
225 :     60, TimeUnit.SECONDS,
226 :     new ArrayBlockingQueue<Runnable>(10));
227 : jsr166 1.40 try (PoolCleaner cleaner = cleaner(p)) {
228 :     Callable c = new Callable() {
229 :     public Object call() { throw new ArithmeticException(); }};
230 :     try {
231 :     p.submit(c).get();
232 :     shouldThrow();
233 :     } catch (ExecutionException success) {
234 :     assertTrue(success.getCause() instanceof ArithmeticException);
235 :     }
236 : tim 1.1 }
237 : dl 1.6 }
238 :    
239 :     /**
240 :     * invokeAny(null) throws NPE
241 :     */
242 : jsr166 1.28 public void testInvokeAny1() throws Exception {
243 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
244 :     try (PoolCleaner cleaner = cleaner(e)) {
245 :     try {
246 :     e.invokeAny(null);
247 :     shouldThrow();
248 :     } catch (NullPointerException success) {}
249 : dl 1.6 }
250 :     }
251 :    
252 :     /**
253 :     * invokeAny(empty collection) throws IAE
254 :     */
255 : jsr166 1.28 public void testInvokeAny2() throws Exception {
256 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
257 :     try (PoolCleaner cleaner = cleaner(e)) {
258 :     try {
259 :     e.invokeAny(new ArrayList<Callable<String>>());
260 :     shouldThrow();
261 :     } catch (IllegalArgumentException success) {}
262 : dl 1.6 }
263 :     }
264 :    
265 :     /**
266 :     * invokeAny(c) throws NPE if c has null elements
267 :     */
268 : jsr166 1.19 public void testInvokeAny3() throws Exception {
269 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
270 :     try (PoolCleaner cleaner = cleaner(e)) {
271 :     List<Callable<Long>> l = new ArrayList<Callable<Long>>();
272 :     l.add(new Callable<Long>() {
273 :     public Long call() { throw new ArithmeticException(); }});
274 :     l.add(null);
275 :     try {
276 :     e.invokeAny(l);
277 :     shouldThrow();
278 :     } catch (NullPointerException success) {}
279 : dl 1.6 }
280 :     }
281 :    
282 :     /**
283 : dl 1.12 * invokeAny(c) throws ExecutionException if no task in c completes
284 : dl 1.6 */
285 : jsr166 1.19 public void testInvokeAny4() throws InterruptedException {
286 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
287 :     try (PoolCleaner cleaner = cleaner(e)) {
288 :     List<Callable<String>> l = new ArrayList<Callable<String>>();
289 :     l.add(new NPETask());
290 :     try {
291 :     e.invokeAny(l);
292 :     shouldThrow();
293 :     } catch (ExecutionException success) {
294 :     assertTrue(success.getCause() instanceof NullPointerException);
295 :     }
296 : dl 1.6 }
297 :     }
298 :    
299 :     /**
300 : dl 1.12 * invokeAny(c) returns result of some task in c if at least one completes
301 : dl 1.6 */
302 : jsr166 1.19 public void testInvokeAny5() throws Exception {
303 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
304 :     try (PoolCleaner cleaner = cleaner(e)) {
305 : jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
306 : dl 1.6 l.add(new StringTask());
307 :     l.add(new StringTask());
308 :     String result = e.invokeAny(l);
309 :     assertSame(TEST_STRING, result);
310 :     }
311 :     }
312 :    
313 :     /**
314 :     * invokeAll(null) throws NPE
315 :     */
316 : jsr166 1.19 public void testInvokeAll1() throws InterruptedException {
317 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
318 :     try (PoolCleaner cleaner = cleaner(e)) {
319 :     try {
320 :     e.invokeAll(null);
321 :     shouldThrow();
322 :     } catch (NullPointerException success) {}
323 : dl 1.6 }
324 :     }
325 :    
326 :     /**
327 :     * invokeAll(empty collection) returns empty collection
328 :     */
329 : jsr166 1.19 public void testInvokeAll2() throws InterruptedException {
330 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
331 :     try (PoolCleaner cleaner = cleaner(e)) {
332 : dl 1.6 List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
333 :     assertTrue(r.isEmpty());
334 :     }
335 :     }
336 :    
337 :     /**
338 :     * invokeAll(c) throws NPE if c has null elements
339 :     */
340 : jsr166 1.19 public void testInvokeAll3() throws InterruptedException {
341 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
342 :     try (PoolCleaner cleaner = cleaner(e)) {
343 :     List<Callable<String>> l = new ArrayList<Callable<String>>();
344 :     l.add(new StringTask());
345 :     l.add(null);
346 :     try {
347 :     e.invokeAll(l);
348 :     shouldThrow();
349 :     } catch (NullPointerException success) {}
350 : dl 1.6 }
351 :     }
352 :    
353 :     /**
354 : dl 1.12 * get of returned element of invokeAll(c) throws exception on failed task
355 : dl 1.6 */
356 : jsr166 1.19 public void testInvokeAll4() throws Exception {
357 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
358 :     try (PoolCleaner cleaner = cleaner(e)) {
359 : jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
360 : dl 1.6 l.add(new NPETask());
361 : jsr166 1.22 List<Future<String>> futures = e.invokeAll(l);
362 :     assertEquals(1, futures.size());
363 :     try {
364 :     futures.get(0).get();
365 :     shouldThrow();
366 :     } catch (ExecutionException success) {
367 :     assertTrue(success.getCause() instanceof NullPointerException);
368 : jsr166 1.19 }
369 : dl 1.6 }
370 :     }
371 :    
372 :     /**
373 : dl 1.12 * invokeAll(c) returns results of all completed tasks in c
374 : dl 1.6 */
375 : jsr166 1.19 public void testInvokeAll5() throws Exception {
376 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
377 :     try (PoolCleaner cleaner = cleaner(e)) {
378 : jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
379 : dl 1.6 l.add(new StringTask());
380 :     l.add(new StringTask());
381 : jsr166 1.22 List<Future<String>> futures = e.invokeAll(l);
382 :     assertEquals(2, futures.size());
383 :     for (Future<String> future : futures)
384 : jsr166 1.19 assertSame(TEST_STRING, future.get());
385 : dl 1.8 }
386 :     }
387 :    
388 :     /**
389 :     * timed invokeAny(null) throws NPE
390 :     */
391 : jsr166 1.19 public void testTimedInvokeAny1() throws Exception {
392 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
393 :     try (PoolCleaner cleaner = cleaner(e)) {
394 :     try {
395 :     e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
396 :     shouldThrow();
397 :     } catch (NullPointerException success) {}
398 : dl 1.8 }
399 :     }
400 :    
401 :     /**
402 : dl 1.12 * timed invokeAny(null time unit) throws NPE
403 : dl 1.8 */
404 : jsr166 1.19 public void testTimedInvokeAnyNullTimeUnit() throws Exception {
405 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
406 :     try (PoolCleaner cleaner = cleaner(e)) {
407 :     List<Callable<String>> l = new ArrayList<Callable<String>>();
408 :     l.add(new StringTask());
409 :     try {
410 :     e.invokeAny(l, MEDIUM_DELAY_MS, null);
411 :     shouldThrow();
412 :     } catch (NullPointerException success) {}
413 : dl 1.8 }
414 :     }
415 :    
416 :     /**
417 :     * timed invokeAny(empty collection) throws IAE
418 :     */
419 : jsr166 1.19 public void testTimedInvokeAny2() throws Exception {
420 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
421 :     try (PoolCleaner cleaner = cleaner(e)) {
422 :     try {
423 :     e.invokeAny(new ArrayList<Callable<String>>(),
424 :     MEDIUM_DELAY_MS, MILLISECONDS);
425 :     shouldThrow();
426 :     } catch (IllegalArgumentException success) {}
427 : dl 1.8 }
428 :     }
429 :    
430 :     /**
431 :     * timed invokeAny(c) throws NPE if c has null elements
432 :     */
433 : jsr166 1.19 public void testTimedInvokeAny3() throws Exception {
434 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
435 :     try (PoolCleaner cleaner = cleaner(e)) {
436 :     List<Callable<Long>> l = new ArrayList<Callable<Long>>();
437 :     l.add(new Callable<Long>() {
438 :     public Long call() { throw new ArithmeticException(); }});
439 :     l.add(null);
440 :     try {
441 :     e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
442 :     shouldThrow();
443 :     } catch (NullPointerException success) {}
444 : dl 1.8 }
445 :     }
446 :    
447 :     /**
448 :     * timed invokeAny(c) throws ExecutionException if no task completes
449 :     */
450 : jsr166 1.19 public void testTimedInvokeAny4() throws Exception {
451 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
452 :     try (PoolCleaner cleaner = cleaner(e)) {
453 :     List<Callable<String>> l = new ArrayList<Callable<String>>();
454 :     l.add(new NPETask());
455 :     try {
456 :     e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
457 :     shouldThrow();
458 :     } catch (ExecutionException success) {
459 :     assertTrue(success.getCause() instanceof NullPointerException);
460 :     }
461 : dl 1.8 }
462 :     }
463 :    
464 :     /**
465 : dl 1.12 * timed invokeAny(c) returns result of some task in c
466 : dl 1.8 */
467 : jsr166 1.19 public void testTimedInvokeAny5() throws Exception {
468 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
469 :     try (PoolCleaner cleaner = cleaner(e)) {
470 : jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
471 : dl 1.8 l.add(new StringTask());
472 :     l.add(new StringTask());
473 : jsr166 1.20 String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
474 : dl 1.8 assertSame(TEST_STRING, result);
475 :     }
476 :     }
477 :    
478 :     /**
479 :     * timed invokeAll(null) throws NPE
480 :     */
481 : jsr166 1.19 public void testTimedInvokeAll1() throws InterruptedException {
482 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
483 :     try (PoolCleaner cleaner = cleaner(e)) {
484 :     try {
485 :     e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
486 :     shouldThrow();
487 :     } catch (NullPointerException success) {}
488 : dl 1.8 }
489 :     }
490 :    
491 :     /**
492 : dl 1.12 * timed invokeAll(null time unit) throws NPE
493 : dl 1.8 */
494 : jsr166 1.19 public void testTimedInvokeAllNullTimeUnit() throws InterruptedException {
495 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
496 :     try (PoolCleaner cleaner = cleaner(e)) {
497 :     List<Callable<String>> l = new ArrayList<Callable<String>>();
498 :     l.add(new StringTask());
499 :     try {
500 :     e.invokeAll(l, MEDIUM_DELAY_MS, null);
501 :     shouldThrow();
502 :     } catch (NullPointerException success) {}
503 : dl 1.8 }
504 :     }
505 :    
506 :     /**
507 :     * timed invokeAll(empty collection) returns empty collection
508 :     */
509 : jsr166 1.19 public void testTimedInvokeAll2() throws InterruptedException {
510 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
511 :     try (PoolCleaner cleaner = cleaner(e)) {
512 : jsr166 1.20 List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
513 : dl 1.8 assertTrue(r.isEmpty());
514 :     }
515 :     }
516 :    
517 :     /**
518 :     * timed invokeAll(c) throws NPE if c has null elements
519 :     */
520 : jsr166 1.19 public void testTimedInvokeAll3() throws InterruptedException {
521 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
522 :     try (PoolCleaner cleaner = cleaner(e)) {
523 :     List<Callable<String>> l = new ArrayList<Callable<String>>();
524 :     l.add(new StringTask());
525 :     l.add(null);
526 :     try {
527 :     e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
528 :     shouldThrow();
529 :     } catch (NullPointerException success) {}
530 : dl 1.8 }
531 :     }
532 :    
533 :     /**
534 : dl 1.12 * get of returned element of invokeAll(c) throws exception on failed task
535 : dl 1.8 */
536 : jsr166 1.19 public void testTimedInvokeAll4() throws Exception {
537 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
538 :     try (PoolCleaner cleaner = cleaner(e)) {
539 : jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
540 : dl 1.8 l.add(new NPETask());
541 : jsr166 1.22 List<Future<String>> futures =
542 :     e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
543 :     assertEquals(1, futures.size());
544 :     try {
545 :     futures.get(0).get();
546 :     shouldThrow();
547 :     } catch (ExecutionException success) {
548 :     assertTrue(success.getCause() instanceof NullPointerException);
549 : jsr166 1.19 }
550 : dl 1.8 }
551 :     }
552 :    
553 :     /**
554 : dl 1.12 * timed invokeAll(c) returns results of all completed tasks in c
555 : dl 1.8 */
556 : jsr166 1.19 public void testTimedInvokeAll5() throws Exception {
557 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
558 :     try (PoolCleaner cleaner = cleaner(e)) {
559 : jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
560 : dl 1.8 l.add(new StringTask());
561 :     l.add(new StringTask());
562 : jsr166 1.22 List<Future<String>> futures =
563 : jsr166 1.38 e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
564 : jsr166 1.22 assertEquals(2, futures.size());
565 :     for (Future<String> future : futures)
566 : jsr166 1.19 assertSame(TEST_STRING, future.get());
567 : dl 1.8 }
568 :     }
569 :    
570 :     /**
571 : dl 1.12 * timed invokeAll cancels tasks not completed by timeout
572 : dl 1.8 */
573 : jsr166 1.38 public void testTimedInvokeAll6() throws Exception {
574 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
575 :     try (PoolCleaner cleaner = cleaner(e)) {
576 : jsr166 1.39 for (long timeout = timeoutMillis();;) {
577 :     List<Callable<String>> tasks = new ArrayList<>();
578 :     tasks.add(new StringTask("0"));
579 :     tasks.add(Executors.callable(possiblyInterruptedRunnable(timeout),
580 :     TEST_STRING));
581 :     tasks.add(new StringTask("2"));
582 :     long startTime = System.nanoTime();
583 :     List<Future<String>> futures =
584 :     e.invokeAll(tasks, timeout, MILLISECONDS);
585 :     assertEquals(tasks.size(), futures.size());
586 :     assertTrue(millisElapsedSince(startTime) >= timeout);
587 :     for (Future future : futures)
588 :     assertTrue(future.isDone());
589 :     try {
590 :     assertEquals("0", futures.get(0).get());
591 :     assertEquals(TEST_STRING, futures.get(1).get());
592 :     } catch (CancellationException retryWithLongerTimeout) {
593 :     // unusual delay before starting second task
594 :     timeout *= 2;
595 :     if (timeout >= LONG_DELAY_MS / 2)
596 :     fail("expected exactly one task to be cancelled");
597 :     continue;
598 :     }
599 :     assertTrue(futures.get(2).isCancelled());
600 :     break;
601 :     }
602 : dl 1.6 }
603 : tim 1.1 }
604 :    
605 : dl 1.3 }

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8