ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinPoolTest.java
Revision: 1.2
Committed: Fri Jul 31 23:37:31 2009 UTC (14 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.1: +77 -78 lines
Log Message:
whitespace

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