ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractExecutorServiceTest.java
Revision: 1.19
Committed: Fri Nov 20 05:25:10 2009 UTC (14 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.18: +173 -272 lines
Log Message:
better exception handling

File Contents

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