[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.48 - (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.48 * Submitting null tasks throws NullPointerException
170 : tim 1.1 */
171 : jsr166 1.48 public void testNullTaskSubmission() {
172 : jsr166 1.47 final ExecutorService e = new DirectExecutorService();
173 :     try (PoolCleaner cleaner = cleaner(e)) {
174 : jsr166 1.48 assertNullTaskSubmissionThrowsNullPointerException(e);
175 : jsr166 1.47 }
176 : tim 1.1 }
177 :    
178 :     /**
179 : jsr166 1.25 * submit(callable).get() throws InterruptedException if interrupted
180 : tim 1.1 */
181 : jsr166 1.19 public void testInterruptedSubmit() throws InterruptedException {
182 : jsr166 1.25 final CountDownLatch submitted = new CountDownLatch(1);
183 :     final CountDownLatch quittingTime = new CountDownLatch(1);
184 :     final Callable<Void> awaiter = new CheckedCallable<Void>() {
185 :     public Void realCall() throws InterruptedException {
186 : jsr166 1.41 assertTrue(quittingTime.await(2*LONG_DELAY_MS, MILLISECONDS));
187 : jsr166 1.25 return null;
188 :     }};
189 : jsr166 1.40 final ExecutorService p
190 :     = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS,
191 :     new ArrayBlockingQueue<Runnable>(10));
192 : jsr166 1.41 try (PoolCleaner cleaner = cleaner(p, quittingTime)) {
193 :     Thread t = newStartedThread(new CheckedInterruptedRunnable() {
194 : jsr166 1.25 public void realRun() throws Exception {
195 :     Future<Void> future = p.submit(awaiter);
196 :     submitted.countDown();
197 :     future.get();
198 :     }});
199 : jsr166 1.41
200 :     await(submitted);
201 : jsr166 1.25 t.interrupt();
202 : jsr166 1.41 awaitTermination(t);
203 : jsr166 1.25 }
204 : tim 1.1 }
205 :    
206 :     /**
207 : jsr166 1.27 * get of submit(callable) throws ExecutionException if callable
208 :     * throws exception
209 : tim 1.1 */
210 : jsr166 1.19 public void testSubmitEE() throws InterruptedException {
211 : jsr166 1.40 final ThreadPoolExecutor p =
212 : jsr166 1.19 new ThreadPoolExecutor(1, 1,
213 :     60, TimeUnit.SECONDS,
214 :     new ArrayBlockingQueue<Runnable>(10));
215 : jsr166 1.40 try (PoolCleaner cleaner = cleaner(p)) {
216 :     Callable c = new Callable() {
217 :     public Object call() { throw new ArithmeticException(); }};
218 :     try {
219 :     p.submit(c).get();
220 :     shouldThrow();
221 :     } catch (ExecutionException success) {
222 :     assertTrue(success.getCause() instanceof ArithmeticException);
223 :     }
224 : tim 1.1 }
225 : dl 1.6 }
226 :    
227 :     /**
228 :     * invokeAny(null) throws NPE
229 :     */
230 : jsr166 1.28 public void testInvokeAny1() throws Exception {
231 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
232 :     try (PoolCleaner cleaner = cleaner(e)) {
233 :     try {
234 :     e.invokeAny(null);
235 :     shouldThrow();
236 :     } catch (NullPointerException success) {}
237 : dl 1.6 }
238 :     }
239 :    
240 :     /**
241 : jsr166 1.46 * invokeAny(empty collection) throws IllegalArgumentException
242 : dl 1.6 */
243 : jsr166 1.28 public void testInvokeAny2() throws Exception {
244 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
245 : jsr166 1.46 final Collection<Callable<String>> emptyCollection
246 :     = Collections.emptyList();
247 : jsr166 1.40 try (PoolCleaner cleaner = cleaner(e)) {
248 :     try {
249 : jsr166 1.46 e.invokeAny(emptyCollection);
250 : jsr166 1.40 shouldThrow();
251 :     } catch (IllegalArgumentException success) {}
252 : dl 1.6 }
253 :     }
254 :    
255 :     /**
256 :     * invokeAny(c) throws NPE if c has null elements
257 :     */
258 : jsr166 1.19 public void testInvokeAny3() throws Exception {
259 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
260 :     try (PoolCleaner cleaner = cleaner(e)) {
261 : jsr166 1.45 List<Callable<Long>> l = new ArrayList<>();
262 : jsr166 1.40 l.add(new Callable<Long>() {
263 :     public Long call() { throw new ArithmeticException(); }});
264 :     l.add(null);
265 :     try {
266 :     e.invokeAny(l);
267 :     shouldThrow();
268 :     } catch (NullPointerException success) {}
269 : dl 1.6 }
270 :     }
271 :    
272 :     /**
273 : dl 1.12 * invokeAny(c) throws ExecutionException if no task in c completes
274 : dl 1.6 */
275 : jsr166 1.19 public void testInvokeAny4() throws InterruptedException {
276 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
277 :     try (PoolCleaner cleaner = cleaner(e)) {
278 : jsr166 1.45 List<Callable<String>> l = new ArrayList<>();
279 : jsr166 1.40 l.add(new NPETask());
280 :     try {
281 :     e.invokeAny(l);
282 :     shouldThrow();
283 :     } catch (ExecutionException success) {
284 :     assertTrue(success.getCause() instanceof NullPointerException);
285 :     }
286 : dl 1.6 }
287 :     }
288 :    
289 :     /**
290 : dl 1.12 * invokeAny(c) returns result of some task in c if at least one completes
291 : dl 1.6 */
292 : jsr166 1.19 public void testInvokeAny5() throws Exception {
293 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
294 :     try (PoolCleaner cleaner = cleaner(e)) {
295 : jsr166 1.45 List<Callable<String>> l = new ArrayList<>();
296 : dl 1.6 l.add(new StringTask());
297 :     l.add(new StringTask());
298 :     String result = e.invokeAny(l);
299 :     assertSame(TEST_STRING, result);
300 :     }
301 :     }
302 :    
303 :     /**
304 :     * invokeAll(null) throws NPE
305 :     */
306 : jsr166 1.19 public void testInvokeAll1() throws InterruptedException {
307 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
308 :     try (PoolCleaner cleaner = cleaner(e)) {
309 :     try {
310 :     e.invokeAll(null);
311 :     shouldThrow();
312 :     } catch (NullPointerException success) {}
313 : dl 1.6 }
314 :     }
315 :    
316 :     /**
317 : jsr166 1.46 * invokeAll(empty collection) returns empty list
318 : dl 1.6 */
319 : jsr166 1.19 public void testInvokeAll2() throws InterruptedException {
320 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
321 : jsr166 1.46 final Collection<Callable<String>> emptyCollection
322 :     = Collections.emptyList();
323 : jsr166 1.40 try (PoolCleaner cleaner = cleaner(e)) {
324 : jsr166 1.46 List<Future<String>> r = e.invokeAll(emptyCollection);
325 : dl 1.6 assertTrue(r.isEmpty());
326 :     }
327 :     }
328 :    
329 :     /**
330 :     * invokeAll(c) throws NPE if c has null elements
331 :     */
332 : jsr166 1.19 public void testInvokeAll3() throws InterruptedException {
333 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
334 :     try (PoolCleaner cleaner = cleaner(e)) {
335 : jsr166 1.45 List<Callable<String>> l = new ArrayList<>();
336 : jsr166 1.40 l.add(new StringTask());
337 :     l.add(null);
338 :     try {
339 :     e.invokeAll(l);
340 :     shouldThrow();
341 :     } catch (NullPointerException success) {}
342 : dl 1.6 }
343 :     }
344 :    
345 :     /**
346 : dl 1.12 * get of returned element of invokeAll(c) throws exception on failed task
347 : dl 1.6 */
348 : jsr166 1.19 public void testInvokeAll4() throws Exception {
349 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
350 :     try (PoolCleaner cleaner = cleaner(e)) {
351 : jsr166 1.45 List<Callable<String>> l = new ArrayList<>();
352 : dl 1.6 l.add(new NPETask());
353 : jsr166 1.22 List<Future<String>> futures = e.invokeAll(l);
354 :     assertEquals(1, futures.size());
355 :     try {
356 :     futures.get(0).get();
357 :     shouldThrow();
358 :     } catch (ExecutionException success) {
359 :     assertTrue(success.getCause() instanceof NullPointerException);
360 : jsr166 1.19 }
361 : dl 1.6 }
362 :     }
363 :    
364 :     /**
365 : dl 1.12 * invokeAll(c) returns results of all completed tasks in c
366 : dl 1.6 */
367 : jsr166 1.19 public void testInvokeAll5() throws Exception {
368 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
369 :     try (PoolCleaner cleaner = cleaner(e)) {
370 : jsr166 1.45 List<Callable<String>> l = new ArrayList<>();
371 : dl 1.6 l.add(new StringTask());
372 :     l.add(new StringTask());
373 : jsr166 1.22 List<Future<String>> futures = e.invokeAll(l);
374 :     assertEquals(2, futures.size());
375 :     for (Future<String> future : futures)
376 : jsr166 1.19 assertSame(TEST_STRING, future.get());
377 : dl 1.8 }
378 :     }
379 :    
380 :     /**
381 :     * timed invokeAny(null) throws NPE
382 :     */
383 : jsr166 1.19 public void testTimedInvokeAny1() throws Exception {
384 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
385 :     try (PoolCleaner cleaner = cleaner(e)) {
386 :     try {
387 : jsr166 1.46 e.invokeAny(null, randomTimeout(), randomTimeUnit());
388 : jsr166 1.40 shouldThrow();
389 :     } catch (NullPointerException success) {}
390 : dl 1.8 }
391 :     }
392 :    
393 :     /**
394 : jsr166 1.46 * timed invokeAny(null time unit) throws NullPointerException
395 : dl 1.8 */
396 : jsr166 1.19 public void testTimedInvokeAnyNullTimeUnit() throws Exception {
397 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
398 :     try (PoolCleaner cleaner = cleaner(e)) {
399 : jsr166 1.45 List<Callable<String>> l = new ArrayList<>();
400 : jsr166 1.40 l.add(new StringTask());
401 :     try {
402 : jsr166 1.46 e.invokeAny(l, randomTimeout(), null);
403 : jsr166 1.40 shouldThrow();
404 :     } catch (NullPointerException success) {}
405 : dl 1.8 }
406 :     }
407 :    
408 :     /**
409 : jsr166 1.46 * timed invokeAny(empty collection) throws IllegalArgumentException
410 : dl 1.8 */
411 : jsr166 1.19 public void testTimedInvokeAny2() throws Exception {
412 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
413 : jsr166 1.46 final Collection<Callable<String>> emptyCollection
414 :     = Collections.emptyList();
415 : jsr166 1.40 try (PoolCleaner cleaner = cleaner(e)) {
416 :     try {
417 : jsr166 1.46 e.invokeAny(emptyCollection, randomTimeout(), randomTimeUnit());
418 : jsr166 1.40 shouldThrow();
419 :     } catch (IllegalArgumentException success) {}
420 : dl 1.8 }
421 :     }
422 :    
423 :     /**
424 :     * timed invokeAny(c) throws NPE if c has null elements
425 :     */
426 : jsr166 1.19 public void testTimedInvokeAny3() throws Exception {
427 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
428 :     try (PoolCleaner cleaner = cleaner(e)) {
429 : jsr166 1.45 List<Callable<Long>> l = new ArrayList<>();
430 : jsr166 1.40 l.add(new Callable<Long>() {
431 :     public Long call() { throw new ArithmeticException(); }});
432 :     l.add(null);
433 :     try {
434 : jsr166 1.46 e.invokeAny(l, randomTimeout(), randomTimeUnit());
435 : jsr166 1.40 shouldThrow();
436 :     } catch (NullPointerException success) {}
437 : dl 1.8 }
438 :     }
439 :    
440 :     /**
441 :     * timed invokeAny(c) throws ExecutionException if no task completes
442 :     */
443 : jsr166 1.19 public void testTimedInvokeAny4() throws Exception {
444 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
445 :     try (PoolCleaner cleaner = cleaner(e)) {
446 : jsr166 1.43 long startTime = System.nanoTime();
447 : jsr166 1.45 List<Callable<String>> l = new ArrayList<>();
448 : jsr166 1.40 l.add(new NPETask());
449 :     try {
450 : jsr166 1.43 e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
451 : jsr166 1.40 shouldThrow();
452 :     } catch (ExecutionException success) {
453 :     assertTrue(success.getCause() instanceof NullPointerException);
454 :     }
455 : jsr166 1.43 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
456 : dl 1.8 }
457 :     }
458 :    
459 :     /**
460 : dl 1.12 * timed invokeAny(c) returns result of some task in c
461 : dl 1.8 */
462 : jsr166 1.19 public void testTimedInvokeAny5() throws Exception {
463 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
464 :     try (PoolCleaner cleaner = cleaner(e)) {
465 : jsr166 1.44 long startTime = System.nanoTime();
466 : jsr166 1.45 List<Callable<String>> l = new ArrayList<>();
467 : dl 1.8 l.add(new StringTask());
468 :     l.add(new StringTask());
469 : jsr166 1.44 String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
470 : dl 1.8 assertSame(TEST_STRING, result);
471 : jsr166 1.44 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
472 : dl 1.8 }
473 :     }
474 :    
475 :     /**
476 : jsr166 1.46 * timed invokeAll(null) throws NullPointerException
477 : dl 1.8 */
478 : jsr166 1.19 public void testTimedInvokeAll1() throws InterruptedException {
479 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
480 :     try (PoolCleaner cleaner = cleaner(e)) {
481 :     try {
482 : jsr166 1.46 e.invokeAll(null, randomTimeout(), randomTimeUnit());
483 : jsr166 1.40 shouldThrow();
484 :     } catch (NullPointerException success) {}
485 : dl 1.8 }
486 :     }
487 :    
488 :     /**
489 : dl 1.12 * timed invokeAll(null time unit) throws NPE
490 : dl 1.8 */
491 : jsr166 1.19 public void testTimedInvokeAllNullTimeUnit() throws InterruptedException {
492 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
493 :     try (PoolCleaner cleaner = cleaner(e)) {
494 : jsr166 1.45 List<Callable<String>> l = new ArrayList<>();
495 : jsr166 1.40 l.add(new StringTask());
496 :     try {
497 : jsr166 1.46 e.invokeAll(l, randomTimeout(), null);
498 : jsr166 1.40 shouldThrow();
499 :     } catch (NullPointerException success) {}
500 : dl 1.8 }
501 :     }
502 :    
503 :     /**
504 : jsr166 1.46 * timed invokeAll(empty collection) returns empty list
505 : dl 1.8 */
506 : jsr166 1.19 public void testTimedInvokeAll2() throws InterruptedException {
507 : jsr166 1.40 final ExecutorService e = new DirectExecutorService();
508 : jsr166 1.46 final Collection<Callable<String>> emptyCollection
509 :     = Collections.emptyList();
510 : jsr166 1.40 try (PoolCleaner cleaner = cleaner(e)) {
511 : jsr166 1.46 List<Future<String>> r =
512 :     e.invokeAll(emptyCollection, randomTimeout(), randomTimeUnit());
513 : dl 1.8 assertTrue(r.isEmpty());
514 :     }
515 :     }
516 :    
517 :     /**
518 : jsr166 1.46 * timed invokeAll(c) throws NullPointerException if c has null elements
519 : dl 1.8 */
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 : jsr166 1.45 List<Callable<String>> l = new ArrayList<>();
524 : jsr166 1.40 l.add(new StringTask());
525 :     l.add(null);
526 :     try {
527 : jsr166 1.46 e.invokeAll(l, randomTimeout(), randomTimeUnit());
528 : jsr166 1.40 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.45 List<Callable<String>> l = new ArrayList<>();
540 : dl 1.8 l.add(new NPETask());
541 : jsr166 1.22 List<Future<String>> futures =
542 : jsr166 1.42 e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
543 : jsr166 1.22 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.45 List<Callable<String>> l = new ArrayList<>();
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