ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinPoolTest.java
Revision: 1.23
Committed: Sat Sep 11 19:04:12 2010 UTC (13 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.22: +80 -59 lines
Log Message:
shut down all pools to prevent "thread leaks"

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