ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinPoolTest.java
Revision: 1.32
Committed: Mon Oct 11 04:39:12 2010 UTC (13 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.31: +11 -11 lines
Log Message:
optimize runtime

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