ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinPoolTest.java
Revision: 1.10
Committed: Sat Nov 21 10:25:05 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.9: +6 -10 lines
Log Message:
improve 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 */
6
7
8 import junit.framework.*;
9 import java.util.*;
10 import java.util.concurrent.*;
11 import static java.util.concurrent.TimeUnit.MILLISECONDS;
12 import java.util.concurrent.locks.*;
13 import java.security.*;
14
15 public class ForkJoinPoolTest extends JSR166TestCase {
16 public static void main(String[] args) {
17 junit.textui.TestRunner.run (suite());
18 }
19 public static Test suite() {
20 return new TestSuite(ForkJoinPoolTest.class);
21 }
22
23 /**
24 * Testing coverage notes:
25 *
26 * 1. shutdown and related methods are tested via super.joinPool.
27 *
28 * 2. newTaskFor and adapters are tested in submit/invoke tests
29 *
30 * 3. We cannot portably test monitoring methods such as
31 * getStealCount() since they rely ultimately on random task
32 * stealing that may cause tasks not to be stolen/propagated
33 * across threads, especially on uniprocessors.
34 *
35 * 4. There are no independently testable ForkJoinWorkerThread
36 * methods, but they are covered here and in task tests.
37 */
38
39 // Some classes to test extension and factory methods
40
41 static class MyHandler implements Thread.UncaughtExceptionHandler {
42 int catches = 0;
43 public void uncaughtException(Thread t, Throwable e) {
44 ++catches;
45 }
46 }
47
48 // to test handlers
49 static class FailingFJWSubclass extends ForkJoinWorkerThread {
50 public FailingFJWSubclass(ForkJoinPool p) { super(p) ; }
51 protected void onStart() { throw new Error(); }
52 }
53
54 static class FailingThreadFactory
55 implements ForkJoinPool.ForkJoinWorkerThreadFactory {
56 int calls = 0;
57 public ForkJoinWorkerThread newThread(ForkJoinPool p) {
58 if (++calls > 1) return null;
59 return new FailingFJWSubclass(p);
60 }
61 }
62
63 static class SubFJP extends ForkJoinPool { // to expose protected
64 SubFJP() { super(1); }
65 public int drainTasksTo(Collection<? super ForkJoinTask<?>> c) {
66 return super.drainTasksTo(c);
67 }
68 public ForkJoinTask<?> pollSubmission() {
69 return super.pollSubmission();
70 }
71 }
72
73 static class ManagedLocker implements ForkJoinPool.ManagedBlocker {
74 final ReentrantLock lock;
75 boolean hasLock = false;
76 ManagedLocker(ReentrantLock lock) { this.lock = lock; }
77 public boolean block() {
78 if (!hasLock)
79 lock.lock();
80 return true;
81 }
82 public boolean isReleasable() {
83 return hasLock || (hasLock = lock.tryLock());
84 }
85 }
86
87 // A simple recursive task for testing
88 static final class FibTask extends RecursiveTask<Integer> {
89 final int number;
90 FibTask(int n) { number = n; }
91 public Integer compute() {
92 int n = number;
93 if (n <= 1)
94 return n;
95 FibTask f1 = new FibTask(n - 1);
96 f1.fork();
97 return (new FibTask(n - 2)).compute() + f1.join();
98 }
99 }
100
101 // A failing task for testing
102 static final class FailingTask extends ForkJoinTask<Void> {
103 public final Void getRawResult() { return null; }
104 protected final void setRawResult(Void mustBeNull) { }
105 protected final boolean exec() { throw new Error(); }
106 FailingTask() {}
107 }
108
109 // Fib needlessly using locking to test ManagedBlockers
110 static final class LockingFibTask extends RecursiveTask<Integer> {
111 final int number;
112 final ManagedLocker locker;
113 final ReentrantLock lock;
114 LockingFibTask(int n, ManagedLocker locker, ReentrantLock lock) {
115 number = n;
116 this.locker = locker;
117 this.lock = lock;
118 }
119 public Integer compute() {
120 int n;
121 LockingFibTask f1 = null;
122 LockingFibTask f2 = null;
123 locker.block();
124 n = number;
125 if (n > 1) {
126 f1 = new LockingFibTask(n - 1, locker, lock);
127 f2 = new LockingFibTask(n - 2, locker, lock);
128 }
129 lock.unlock();
130 if (n <= 1)
131 return n;
132 else {
133 f1.fork();
134 return f2.compute() + f1.join();
135 }
136 }
137 }
138
139 /**
140 * Successfully constructed pool reports default factory,
141 * parallelism and async mode policies, no active threads or
142 * tasks, and quiescent running state.
143 */
144 public void testDefaultInitialState() {
145 ForkJoinPool p = null;
146 try {
147 p = new ForkJoinPool(1);
148 assertTrue(p.getFactory() ==
149 ForkJoinPool.defaultForkJoinWorkerThreadFactory);
150 assertTrue(p.isQuiescent());
151 assertTrue(p.getMaintainsParallelism());
152 assertFalse(p.getAsyncMode());
153 assertTrue(p.getActiveThreadCount() == 0);
154 assertTrue(p.getStealCount() == 0);
155 assertTrue(p.getQueuedTaskCount() == 0);
156 assertTrue(p.getQueuedSubmissionCount() == 0);
157 assertFalse(p.hasQueuedSubmissions());
158 assertFalse(p.isShutdown());
159 assertFalse(p.isTerminating());
160 assertFalse(p.isTerminated());
161 } finally {
162 joinPool(p);
163 }
164 }
165
166 /**
167 * Constructor throws if size argument is less than zero
168 */
169 public void testConstructor1() {
170 try {
171 new ForkJoinPool(-1);
172 shouldThrow();
173 } catch (IllegalArgumentException success) {}
174 }
175
176 /**
177 * Constructor throws if factory argument is null
178 */
179 public void testConstructor2() {
180 try {
181 new ForkJoinPool(1, null);
182 shouldThrow();
183 } catch (NullPointerException success) {}
184 }
185
186
187 /**
188 * getParallelism returns size set in constructor
189 */
190 public void testGetParallelism() {
191 ForkJoinPool p = null;
192 try {
193 p = new ForkJoinPool(1);
194 assertTrue(p.getParallelism() == 1);
195 } finally {
196 joinPool(p);
197 }
198 }
199
200 /**
201 * setParallelism changes reported parallelism level.
202 */
203 public void testSetParallelism() {
204 ForkJoinPool p = null;
205 try {
206 p = new ForkJoinPool(1);
207 assertTrue(p.getParallelism() == 1);
208 p.setParallelism(2);
209 assertTrue(p.getParallelism() == 2);
210 } finally {
211 joinPool(p);
212 }
213 }
214
215 /**
216 * setParallelism with argument <= 0 throws exception
217 */
218 public void testSetParallelism2() {
219 ForkJoinPool p = null;
220 try {
221 p = new ForkJoinPool(1);
222 assertTrue(p.getParallelism() == 1);
223 p.setParallelism(-2);
224 shouldThrow();
225 } catch (IllegalArgumentException success) {
226 } finally {
227 joinPool(p);
228 }
229 }
230
231 /**
232 * getPoolSize returns number of started workers.
233 */
234 public void testGetPoolSize() {
235 ForkJoinPool p = null;
236 try {
237 p = new ForkJoinPool(1);
238 assertTrue(p.getPoolSize() == 0);
239 Future<String> future = p.submit(new StringTask());
240 assertTrue(p.getPoolSize() == 1);
241
242 } finally {
243 joinPool(p);
244 }
245 }
246
247 /**
248 * setMaximumPoolSize changes size reported by getMaximumPoolSize.
249 */
250 public void testSetMaximumPoolSize() {
251 ForkJoinPool p = null;
252 try {
253 p = new ForkJoinPool(1);
254 p.setMaximumPoolSize(2);
255 assertTrue(p.getMaximumPoolSize() == 2);
256 } finally {
257 joinPool(p);
258 }
259 }
260
261 /**
262 * setMaximumPoolSize with argument <= 0 throws exception
263 */
264 public void testSetMaximumPoolSize2() {
265 ForkJoinPool p = null;
266 try {
267 p = new ForkJoinPool(1);
268 p.setMaximumPoolSize(-2);
269 shouldThrow();
270 } catch (IllegalArgumentException success) {
271 } finally {
272 joinPool(p);
273 }
274 }
275
276 /**
277 * setMaintainsParallelism changes policy reported by
278 * getMaintainsParallelism.
279 */
280 public void testSetMaintainsParallelism() {
281 ForkJoinPool p = null;
282 try {
283 p = new ForkJoinPool(1);
284 p.setMaintainsParallelism(false);
285 assertFalse(p.getMaintainsParallelism());
286 } finally {
287 joinPool(p);
288 }
289 }
290
291 /**
292 * setAsyncMode changes policy reported by
293 * getAsyncMode.
294 */
295 public void testSetAsyncMode() {
296 ForkJoinPool p = null;
297 try {
298 p = new ForkJoinPool(1);
299 p.setAsyncMode(true);
300 assertTrue(p.getAsyncMode());
301 } finally {
302 joinPool(p);
303 }
304 }
305
306 /**
307 * setUncaughtExceptionHandler changes handler for uncaught exceptions.
308 *
309 * Additionally tests: Overriding ForkJoinWorkerThread.onStart
310 * performs its defined action
311 */
312 public void testSetUncaughtExceptionHandler() throws InterruptedException {
313 ForkJoinPool p = null;
314 try {
315 p = new ForkJoinPool(1, new FailingThreadFactory());
316 MyHandler eh = new MyHandler();
317 p.setUncaughtExceptionHandler(eh);
318 assertEquals(eh, p.getUncaughtExceptionHandler());
319 p.execute(new FailingTask());
320 Thread.sleep(MEDIUM_DELAY_MS);
321 assertTrue(eh.catches > 0);
322 } finally {
323 joinPool(p);
324 }
325 }
326
327 /**
328 * setUncaughtExceptionHandler of null removes handler
329 */
330 public void testSetUncaughtExceptionHandler2() {
331 ForkJoinPool p = null;
332 try {
333 p = new ForkJoinPool(1);
334 p.setUncaughtExceptionHandler(null);
335 assertNull(p.getUncaughtExceptionHandler());
336 } finally {
337 joinPool(p);
338 }
339 }
340
341
342 /**
343 * After invoking a single task, isQuiescent is true,
344 * queues are empty, threads are not active, and
345 * construction parameters continue to hold
346 */
347 public void testisQuiescent() throws InterruptedException {
348 ForkJoinPool p = null;
349 try {
350 p = new ForkJoinPool(2);
351 p.invoke(new FibTask(20));
352 assertTrue(p.getFactory() ==
353 ForkJoinPool.defaultForkJoinWorkerThreadFactory);
354 Thread.sleep(MEDIUM_DELAY_MS);
355 assertTrue(p.isQuiescent());
356 assertTrue(p.getMaintainsParallelism());
357 assertFalse(p.getAsyncMode());
358 assertTrue(p.getActiveThreadCount() == 0);
359 assertTrue(p.getQueuedTaskCount() == 0);
360 assertTrue(p.getQueuedSubmissionCount() == 0);
361 assertFalse(p.hasQueuedSubmissions());
362 assertFalse(p.isShutdown());
363 assertFalse(p.isTerminating());
364 assertFalse(p.isTerminated());
365 } finally {
366 joinPool(p);
367 }
368 }
369
370 /**
371 * Completed submit(ForkJoinTask) returns result
372 */
373 public void testSubmitForkJoinTask() throws Throwable {
374 ForkJoinPool p = null;
375 try {
376 p = new ForkJoinPool(1);
377 ForkJoinTask<Integer> f = p.submit(new FibTask(8));
378 int r = f.get();
379 assertTrue(r == 21);
380 } finally {
381 joinPool(p);
382 }
383 }
384
385 /**
386 * A task submitted after shutdown is rejected
387 */
388 public void testSubmitAfterShutdown() {
389 ForkJoinPool p = null;
390 try {
391 p = new ForkJoinPool(1);
392 p.shutdown();
393 assertTrue(p.isShutdown());
394 ForkJoinTask<Integer> f = p.submit(new FibTask(8));
395 shouldThrow();
396 } catch (RejectedExecutionException success) {
397 } finally {
398 joinPool(p);
399 }
400 }
401
402 /**
403 * Pool maintains parallelism when using ManagedBlocker
404 */
405 public void testBlockingForkJoinTask() throws Throwable {
406 ForkJoinPool p = null;
407 try {
408 p = new ForkJoinPool(4);
409 ReentrantLock lock = new ReentrantLock();
410 ManagedLocker locker = new ManagedLocker(lock);
411 ForkJoinTask<Integer> f = new LockingFibTask(30, locker, lock);
412 p.execute(f);
413 assertTrue(p.getPoolSize() >= 4);
414 int r = f.get();
415 assertTrue(r == 832040);
416 } finally {
417 p.shutdownNow(); // don't wait out shutdown
418 }
419 }
420
421 /**
422 * pollSubmission returns unexecuted submitted task, if present
423 */
424 public void testPollSubmission() {
425 SubFJP p = null;
426 try {
427 p = new SubFJP();
428 ForkJoinTask a = p.submit(new MediumRunnable());
429 ForkJoinTask b = p.submit(new MediumRunnable());
430 ForkJoinTask c = p.submit(new MediumRunnable());
431 ForkJoinTask r = p.pollSubmission();
432 assertTrue(r == a || r == b || r == c);
433 assertFalse(r.isDone());
434 } finally {
435 joinPool(p);
436 }
437 }
438
439 /**
440 * drainTasksTo transfers unexecuted submitted tasks, if present
441 */
442 public void testDrainTasksTo() {
443 SubFJP p = null;
444 try {
445 p = new SubFJP();
446 ForkJoinTask a = p.submit(new MediumRunnable());
447 ForkJoinTask b = p.submit(new MediumRunnable());
448 ForkJoinTask c = p.submit(new MediumRunnable());
449 ArrayList<ForkJoinTask> al = new ArrayList();
450 p.drainTasksTo(al);
451 assertTrue(al.size() > 0);
452 for (ForkJoinTask r : al) {
453 assertTrue(r == a || r == b || r == c);
454 assertFalse(r.isDone());
455 }
456 } finally {
457 joinPool(p);
458 }
459 }
460
461
462 // FJ Versions of AbstractExecutorService tests
463
464 /**
465 * execute(runnable) runs it to completion
466 */
467 public void testExecuteRunnable() throws Throwable {
468 ExecutorService e = new ForkJoinPool(1);
469 TrackedShortRunnable task = new TrackedShortRunnable();
470 assertFalse(task.done);
471 Future<?> future = e.submit(task);
472 future.get();
473 assertTrue(task.done);
474 }
475
476
477 /**
478 * Completed submit(callable) returns result
479 */
480 public void testSubmitCallable() throws Throwable {
481 ExecutorService e = new ForkJoinPool(1);
482 Future<String> future = e.submit(new StringTask());
483 String result = future.get();
484 assertSame(TEST_STRING, result);
485 }
486
487 /**
488 * Completed submit(runnable) returns successfully
489 */
490 public void testSubmitRunnable() throws Throwable {
491 ExecutorService e = new ForkJoinPool(1);
492 Future<?> future = e.submit(new NoOpRunnable());
493 future.get();
494 assertTrue(future.isDone());
495 }
496
497 /**
498 * Completed submit(runnable, result) returns result
499 */
500 public void testSubmitRunnable2() throws Throwable {
501 ExecutorService e = new ForkJoinPool(1);
502 Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
503 String result = future.get();
504 assertSame(TEST_STRING, result);
505 }
506
507
508 /**
509 * A submitted privileged action to completion
510 */
511 public void testSubmitPrivilegedAction() throws Throwable {
512 Policy savedPolicy = null;
513 try {
514 savedPolicy = Policy.getPolicy();
515 AdjustablePolicy policy = new AdjustablePolicy();
516 policy.addPermission(new RuntimePermission("getContextClassLoader"));
517 policy.addPermission(new RuntimePermission("setContextClassLoader"));
518 Policy.setPolicy(policy);
519 } catch (AccessControlException ok) {
520 return;
521 }
522 try {
523 ExecutorService e = new ForkJoinPool(1);
524 Future future = e.submit(Executors.callable(new PrivilegedAction() {
525 public Object run() {
526 return TEST_STRING;
527 }}));
528
529 Object result = future.get();
530 assertSame(TEST_STRING, result);
531 }
532 finally {
533 Policy.setPolicy(savedPolicy);
534 }
535 }
536
537 /**
538 * A submitted a privileged exception action runs to completion
539 */
540 public void testSubmitPrivilegedExceptionAction() throws Throwable {
541 Policy savedPolicy = null;
542 try {
543 savedPolicy = Policy.getPolicy();
544 AdjustablePolicy policy = new AdjustablePolicy();
545 policy.addPermission(new RuntimePermission("getContextClassLoader"));
546 policy.addPermission(new RuntimePermission("setContextClassLoader"));
547 Policy.setPolicy(policy);
548 } catch (AccessControlException ok) {
549 return;
550 }
551
552 try {
553 ExecutorService e = new ForkJoinPool(1);
554 Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
555 public Object run() {
556 return TEST_STRING;
557 }}));
558
559 Object result = future.get();
560 assertSame(TEST_STRING, result);
561 }
562 finally {
563 Policy.setPolicy(savedPolicy);
564 }
565 }
566
567 /**
568 * A submitted failed privileged exception action reports exception
569 */
570 public void testSubmitFailedPrivilegedExceptionAction() throws Throwable {
571 Policy savedPolicy = null;
572 try {
573 savedPolicy = Policy.getPolicy();
574 AdjustablePolicy policy = new AdjustablePolicy();
575 policy.addPermission(new RuntimePermission("getContextClassLoader"));
576 policy.addPermission(new RuntimePermission("setContextClassLoader"));
577 Policy.setPolicy(policy);
578 } catch (AccessControlException ok) {
579 return;
580 }
581
582
583 try {
584 ExecutorService e = new ForkJoinPool(1);
585 Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
586 public Object run() throws Exception {
587 throw new IndexOutOfBoundsException();
588 }}));
589
590 Object result = future.get();
591 shouldThrow();
592 } catch (ExecutionException success) {
593 } finally {
594 Policy.setPolicy(savedPolicy);
595 }
596 }
597
598 /**
599 * execute(null runnable) throws NullPointerException
600 */
601 public void testExecuteNullRunnable() {
602 try {
603 ExecutorService e = new ForkJoinPool(1);
604 TrackedShortRunnable task = null;
605 Future<?> future = e.submit(task);
606 shouldThrow();
607 } catch (NullPointerException success) {}
608 }
609
610
611 /**
612 * submit(null callable) throws NullPointerException
613 */
614 public void testSubmitNullCallable() {
615 try {
616 ExecutorService e = new ForkJoinPool(1);
617 StringTask t = null;
618 Future<String> future = e.submit(t);
619 shouldThrow();
620 } catch (NullPointerException success) {}
621 }
622
623
624 /**
625 * Blocking on submit(callable) throws InterruptedException if
626 * caller interrupted.
627 */
628 public void testInterruptedSubmit() throws InterruptedException {
629 final ForkJoinPool p = new ForkJoinPool(1);
630
631 Thread t = new Thread(new CheckedInterruptedRunnable() {
632 void realRun() throws Throwable {
633 p.submit(new CheckedCallable<Object>() {
634 public Object realCall() throws Throwable {
635 try {
636 Thread.sleep(MEDIUM_DELAY_MS);
637 } catch (InterruptedException ok) {
638 }
639 return null;
640 }}).get();
641 }});
642
643 t.start();
644 Thread.sleep(SHORT_DELAY_MS);
645 t.interrupt();
646 t.join();
647 p.shutdownNow();
648 joinPool(p);
649 }
650
651 /**
652 * get of submit(callable) throws ExecutionException if callable
653 * throws exception
654 */
655 public void testSubmitEE() throws Throwable {
656 ForkJoinPool p = new ForkJoinPool(1);
657 try {
658 p.submit(new Callable() {
659 public Object call() {
660 int i = 5/0;
661 return Boolean.TRUE;
662 }}).get();
663 shouldThrow();
664 } catch (ExecutionException success) {}
665
666 joinPool(p);
667 }
668
669 /**
670 * invokeAny(null) throws NullPointerException
671 */
672 public void testInvokeAny1() throws Throwable {
673 ExecutorService e = new ForkJoinPool(1);
674 try {
675 e.invokeAny(null);
676 shouldThrow();
677 } catch (NullPointerException success) {
678 } finally {
679 joinPool(e);
680 }
681 }
682
683 /**
684 * invokeAny(empty collection) throws IllegalArgumentException
685 */
686 public void testInvokeAny2() throws Throwable {
687 ExecutorService e = new ForkJoinPool(1);
688 try {
689 e.invokeAny(new ArrayList<Callable<String>>());
690 shouldThrow();
691 } catch (IllegalArgumentException success) {
692 } finally {
693 joinPool(e);
694 }
695 }
696
697 /**
698 * invokeAny(c) throws NullPointerException if c has a single null element
699 */
700 public void testInvokeAny3() throws Throwable {
701 ExecutorService e = new ForkJoinPool(1);
702 try {
703 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
704 l.add(null);
705 e.invokeAny(l);
706 shouldThrow();
707 } catch (NullPointerException success) {
708 } finally {
709 joinPool(e);
710 }
711 }
712
713 /**
714 * invokeAny(c) throws NullPointerException if c has null elements
715 */
716 public void testInvokeAny4() throws Throwable {
717 ExecutorService e = new ForkJoinPool(1);
718 try {
719 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
720 l.add(new Callable<String>() {
721 public String call() {
722 // The delay gives the pool a chance to notice
723 // the null element.
724 sleepTillInterrupted(SMALL_DELAY_MS);
725 return "foo";
726 }});
727 l.add(null);
728 e.invokeAny(l);
729 shouldThrow();
730 } catch (NullPointerException success) {
731 } finally {
732 joinPool(e);
733 }
734 }
735
736 /**
737 * invokeAny(c) throws ExecutionException if no task in c completes
738 */
739 public void testInvokeAny5() throws Throwable {
740 ExecutorService e = new ForkJoinPool(1);
741 try {
742 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
743 l.add(new NPETask());
744 e.invokeAny(l);
745 shouldThrow();
746 } catch (ExecutionException success) {
747 } finally {
748 joinPool(e);
749 }
750 }
751
752 /**
753 * invokeAny(c) returns result of some task in c if at least one completes
754 */
755 public void testInvokeAny6() throws Throwable {
756 ExecutorService e = new ForkJoinPool(1);
757 try {
758 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
759 l.add(new StringTask());
760 l.add(new StringTask());
761 String result = e.invokeAny(l);
762 assertSame(TEST_STRING, result);
763 } finally {
764 joinPool(e);
765 }
766 }
767
768 /**
769 * invokeAll(null) throws NullPointerException
770 */
771 public void testInvokeAll1() throws Throwable {
772 ExecutorService e = new ForkJoinPool(1);
773 try {
774 e.invokeAll(null);
775 shouldThrow();
776 } catch (NullPointerException success) {
777 } finally {
778 joinPool(e);
779 }
780 }
781
782 /**
783 * invokeAll(empty collection) returns empty collection
784 */
785 public void testInvokeAll2() throws InterruptedException {
786 ExecutorService e = new ForkJoinPool(1);
787 try {
788 List<Future<String>> r
789 = e.invokeAll(new ArrayList<Callable<String>>());
790 assertTrue(r.isEmpty());
791 } finally {
792 joinPool(e);
793 }
794 }
795
796 /**
797 * invokeAll(c) throws NullPointerException if c has null elements
798 */
799 public void testInvokeAll3() throws InterruptedException {
800 ExecutorService e = new ForkJoinPool(1);
801 try {
802 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
803 l.add(new StringTask());
804 l.add(null);
805 e.invokeAll(l);
806 shouldThrow();
807 } catch (NullPointerException success) {
808 } finally {
809 joinPool(e);
810 }
811 }
812
813 /**
814 * get of returned element of invokeAll(c) throws
815 * ExecutionException on failed task
816 */
817 public void testInvokeAll4() throws Throwable {
818 ExecutorService e = new ForkJoinPool(1);
819 try {
820 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
821 l.add(new NPETask());
822 List<Future<String>> result = e.invokeAll(l);
823 assertEquals(1, result.size());
824 for (Future<String> future : result)
825 future.get();
826 shouldThrow();
827 } catch (ExecutionException success) {
828 } finally {
829 joinPool(e);
830 }
831 }
832
833 /**
834 * invokeAll(c) returns results of all completed tasks in c
835 */
836 public void testInvokeAll5() throws Throwable {
837 ExecutorService e = new ForkJoinPool(1);
838 try {
839 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
840 l.add(new StringTask());
841 l.add(new StringTask());
842 List<Future<String>> result = e.invokeAll(l);
843 assertEquals(2, result.size());
844 for (Future<String> future : result)
845 assertSame(TEST_STRING, future.get());
846 } finally {
847 joinPool(e);
848 }
849 }
850
851
852 /**
853 * timed invokeAny(null) throws NullPointerException
854 */
855 public void testTimedInvokeAny1() throws Throwable {
856 ExecutorService e = new ForkJoinPool(1);
857 try {
858 e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
859 shouldThrow();
860 } catch (NullPointerException success) {
861 } finally {
862 joinPool(e);
863 }
864 }
865
866 /**
867 * timed invokeAny(null time unit) throws NullPointerException
868 */
869 public void testTimedInvokeAnyNullTimeUnit() throws Throwable {
870 ExecutorService e = new ForkJoinPool(1);
871 try {
872 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
873 l.add(new StringTask());
874 e.invokeAny(l, MEDIUM_DELAY_MS, null);
875 shouldThrow();
876 } catch (NullPointerException success) {
877 } finally {
878 joinPool(e);
879 }
880 }
881
882 /**
883 * timed invokeAny(empty collection) throws IllegalArgumentException
884 */
885 public void testTimedInvokeAny2() throws Throwable {
886 ExecutorService e = new ForkJoinPool(1);
887 try {
888 e.invokeAny(new ArrayList<Callable<String>>(),
889 MEDIUM_DELAY_MS, MILLISECONDS);
890 shouldThrow();
891 } catch (IllegalArgumentException success) {
892 } finally {
893 joinPool(e);
894 }
895 }
896
897 /**
898 * timed invokeAny(c) throws NullPointerException if c has null elements
899 */
900 public void testTimedInvokeAny3() throws Throwable {
901 ExecutorService e = new ForkJoinPool(1);
902 try {
903 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
904 l.add(new StringTask());
905 l.add(null);
906 e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
907 shouldThrow();
908 } catch (NullPointerException success) {
909 } finally {
910 joinPool(e);
911 }
912 }
913
914 /**
915 * timed invokeAny(c) throws ExecutionException if no task completes
916 */
917 public void testTimedInvokeAny4() throws Throwable {
918 ExecutorService e = new ForkJoinPool(1);
919 try {
920 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
921 l.add(new NPETask());
922 e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
923 shouldThrow();
924 } catch (ExecutionException success) {
925 } finally {
926 joinPool(e);
927 }
928 }
929
930 /**
931 * timed invokeAny(c) returns result of some task in c
932 */
933 public void testTimedInvokeAny5() throws Throwable {
934 ExecutorService e = new ForkJoinPool(1);
935 try {
936 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
937 l.add(new StringTask());
938 l.add(new StringTask());
939 String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
940 assertSame(TEST_STRING, result);
941 } finally {
942 joinPool(e);
943 }
944 }
945
946 /**
947 * timed invokeAll(null) throws NullPointerException
948 */
949 public void testTimedInvokeAll1() throws Throwable {
950 ExecutorService e = new ForkJoinPool(1);
951 try {
952 e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
953 shouldThrow();
954 } catch (NullPointerException success) {
955 } finally {
956 joinPool(e);
957 }
958 }
959
960 /**
961 * timed invokeAll(null time unit) throws NullPointerException
962 */
963 public void testTimedInvokeAllNullTimeUnit() throws Throwable {
964 ExecutorService e = new ForkJoinPool(1);
965 try {
966 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
967 l.add(new StringTask());
968 e.invokeAll(l, MEDIUM_DELAY_MS, null);
969 shouldThrow();
970 } catch (NullPointerException success) {
971 } finally {
972 joinPool(e);
973 }
974 }
975
976 /**
977 * timed invokeAll(empty collection) returns empty collection
978 */
979 public void testTimedInvokeAll2() throws InterruptedException {
980 ExecutorService e = new ForkJoinPool(1);
981 try {
982 List<Future<String>> r
983 = e.invokeAll(new ArrayList<Callable<String>>(),
984 MEDIUM_DELAY_MS, MILLISECONDS);
985 assertTrue(r.isEmpty());
986 } finally {
987 joinPool(e);
988 }
989 }
990
991 /**
992 * timed invokeAll(c) throws NullPointerException if c has null elements
993 */
994 public void testTimedInvokeAll3() throws InterruptedException {
995 ExecutorService e = new ForkJoinPool(1);
996 try {
997 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
998 l.add(new StringTask());
999 l.add(null);
1000 e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1001 shouldThrow();
1002 } catch (NullPointerException success) {
1003 } finally {
1004 joinPool(e);
1005 }
1006 }
1007
1008 /**
1009 * get of returned element of invokeAll(c) throws exception on failed task
1010 */
1011 public void testTimedInvokeAll4() throws Throwable {
1012 ExecutorService e = new ForkJoinPool(1);
1013 try {
1014 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1015 l.add(new NPETask());
1016 List<Future<String>> result
1017 = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1018 assertEquals(1, result.size());
1019 for (Future<String> future : result)
1020 future.get();
1021 shouldThrow();
1022 } catch (ExecutionException success) {
1023 } finally {
1024 joinPool(e);
1025 }
1026 }
1027
1028 /**
1029 * timed invokeAll(c) returns results of all completed tasks in c
1030 */
1031 public void testTimedInvokeAll5() throws Throwable {
1032 ExecutorService e = new ForkJoinPool(1);
1033 try {
1034 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1035 l.add(new StringTask());
1036 l.add(new StringTask());
1037 List<Future<String>> result
1038 = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1039 assertEquals(2, result.size());
1040 for (Future<String> future : result)
1041 assertSame(TEST_STRING, future.get());
1042 } finally {
1043 joinPool(e);
1044 }
1045 }
1046
1047 }