ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck-jsr166e/ForkJoinPoolTest.java
Revision: 1.1
Committed: Sun Jul 14 19:55:05 2013 UTC (10 years, 10 months ago) by jsr166
Branch: MAIN
Log Message:
backport jsr166e to run on jdk6; backport all applicable tck tests from tck to tck-jsr166e

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