ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck-jsr166e/ForkJoinPoolTest.java
Revision: 1.2
Committed: Thu Feb 26 06:53:34 2015 UTC (9 years, 2 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +0 -5 lines
Log Message:
delete unused imports

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