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

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8