ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractExecutorServiceTest.java
Revision: 1.32
Committed: Sun May 29 07:01:17 2011 UTC (12 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.31: +17 -16 lines
Log Message:
various test case improvements

File Contents

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