ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinPoolTest.java
Revision: 1.20
Committed: Wed Aug 11 19:50:02 2010 UTC (13 years, 9 months ago) by dl
Branch: MAIN
Changes since 1.19: +97 -160 lines
Log Message:
Sync with API simplifications

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