ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinPool8Test.java
(Generate patch)

Comparing jsr166/src/test/tck/ForkJoinPool8Test.java (file contents):
Revision 1.1 by jsr166, Tue Feb 5 03:39:34 2013 UTC vs.
Revision 1.28 by jsr166, Wed Dec 31 16:44:01 2014 UTC

# Line 5 | Line 5
5   */
6  
7   import junit.framework.*;
8 + import java.util.concurrent.CancellationException;
9 + import java.util.concurrent.ExecutionException;
10   import java.util.concurrent.ForkJoinPool;
11   import java.util.concurrent.ForkJoinTask;
12 < import java.util.concurrent.ForkJoinWorkerThread;
12 > import java.util.concurrent.RecursiveAction;
13 > import java.util.concurrent.CountedCompleter;
14 > import java.util.concurrent.TimeoutException;
15 > import static java.util.concurrent.TimeUnit.SECONDS;
16 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
17 > import java.util.HashSet;
18  
19   public class ForkJoinPool8Test extends JSR166TestCase {
20      public static void main(String[] args) {
# Line 25 | Line 32 | public class ForkJoinPool8Test extends J
32          assertEquals(ForkJoinPool.getCommonPoolParallelism(),
33                       ForkJoinPool.commonPool().getParallelism());
34      }
35 +
36 +    /**
37 +     * Common pool cannot be shut down
38 +     */
39 +    public void testCommonPoolShutDown() {
40 +        assertFalse(ForkJoinPool.commonPool().isShutdown());
41 +        assertFalse(ForkJoinPool.commonPool().isTerminating());
42 +        assertFalse(ForkJoinPool.commonPool().isTerminated());
43 +        ForkJoinPool.commonPool().shutdown();
44 +        assertFalse(ForkJoinPool.commonPool().isShutdown());
45 +        assertFalse(ForkJoinPool.commonPool().isTerminating());
46 +        assertFalse(ForkJoinPool.commonPool().isTerminated());
47 +        ForkJoinPool.commonPool().shutdownNow();
48 +        assertFalse(ForkJoinPool.commonPool().isShutdown());
49 +        assertFalse(ForkJoinPool.commonPool().isTerminating());
50 +        assertFalse(ForkJoinPool.commonPool().isTerminated());
51 +    }
52 +
53 +    /*
54 +     * All of the following test methods are adaptations of those for
55 +     * RecursiveAction and CountedCompleter, but with all actions
56 +     * executed in the common pool, generally implicitly via
57 +     * checkInvoke.
58 +     */
59 +
60 +    private void checkInvoke(ForkJoinTask a) {
61 +        checkNotDone(a);
62 +        assertNull(a.invoke());
63 +        checkCompletedNormally(a);
64 +    }
65 +
66 +    void checkNotDone(ForkJoinTask a) {
67 +        assertFalse(a.isDone());
68 +        assertFalse(a.isCompletedNormally());
69 +        assertFalse(a.isCompletedAbnormally());
70 +        assertFalse(a.isCancelled());
71 +        assertNull(a.getException());
72 +        assertNull(a.getRawResult());
73 +
74 +        if (! ForkJoinTask.inForkJoinPool()) {
75 +            Thread.currentThread().interrupt();
76 +            try {
77 +                a.get();
78 +                shouldThrow();
79 +            } catch (InterruptedException success) {
80 +            } catch (Throwable fail) { threadUnexpectedException(fail); }
81 +
82 +            Thread.currentThread().interrupt();
83 +            try {
84 +                a.get(5L, SECONDS);
85 +                shouldThrow();
86 +            } catch (InterruptedException success) {
87 +            } catch (Throwable fail) { threadUnexpectedException(fail); }
88 +        }
89 +
90 +        try {
91 +            a.get(0L, SECONDS);
92 +            shouldThrow();
93 +        } catch (TimeoutException success) {
94 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
95 +    }
96 +
97 +    void checkCompletedNormally(ForkJoinTask a) {
98 +        assertTrue(a.isDone());
99 +        assertFalse(a.isCancelled());
100 +        assertTrue(a.isCompletedNormally());
101 +        assertFalse(a.isCompletedAbnormally());
102 +        assertNull(a.getException());
103 +        assertNull(a.getRawResult());
104 +        assertNull(a.join());
105 +        assertFalse(a.cancel(false));
106 +        assertFalse(a.cancel(true));
107 +        try {
108 +            assertNull(a.get());
109 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
110 +        try {
111 +            assertNull(a.get(5L, SECONDS));
112 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
113 +    }
114 +
115 +    void checkCancelled(ForkJoinTask a) {
116 +        assertTrue(a.isDone());
117 +        assertTrue(a.isCancelled());
118 +        assertFalse(a.isCompletedNormally());
119 +        assertTrue(a.isCompletedAbnormally());
120 +        assertTrue(a.getException() instanceof CancellationException);
121 +        assertNull(a.getRawResult());
122 +
123 +        try {
124 +            a.join();
125 +            shouldThrow();
126 +        } catch (CancellationException success) {
127 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
128 +
129 +        try {
130 +            a.get();
131 +            shouldThrow();
132 +        } catch (CancellationException success) {
133 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
134 +
135 +        try {
136 +            a.get(5L, SECONDS);
137 +            shouldThrow();
138 +        } catch (CancellationException success) {
139 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
140 +    }
141 +
142 +    void checkCompletedAbnormally(ForkJoinTask a, Throwable t) {
143 +        assertTrue(a.isDone());
144 +        assertFalse(a.isCancelled());
145 +        assertFalse(a.isCompletedNormally());
146 +        assertTrue(a.isCompletedAbnormally());
147 +        assertSame(t.getClass(), a.getException().getClass());
148 +        assertNull(a.getRawResult());
149 +        assertFalse(a.cancel(false));
150 +        assertFalse(a.cancel(true));
151 +
152 +        try {
153 +            a.join();
154 +            shouldThrow();
155 +        } catch (Throwable expected) {
156 +            assertSame(expected.getClass(), t.getClass());
157 +        }
158 +
159 +        try {
160 +            a.get();
161 +            shouldThrow();
162 +        } catch (ExecutionException success) {
163 +            assertSame(t.getClass(), success.getCause().getClass());
164 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
165 +
166 +        try {
167 +            a.get(5L, SECONDS);
168 +            shouldThrow();
169 +        } catch (ExecutionException success) {
170 +            assertSame(t.getClass(), success.getCause().getClass());
171 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
172 +    }
173 +
174 +    public static final class FJException extends RuntimeException {
175 +        public FJException() { super(); }
176 +        public FJException(Throwable cause) { super(cause); }
177 +    }
178 +
179 +    // A simple recursive action for testing
180 +    final class FibAction extends CheckedRecursiveAction {
181 +        final int number;
182 +        int result;
183 +        FibAction(int n) { number = n; }
184 +        protected void realCompute() {
185 +            int n = number;
186 +            if (n <= 1)
187 +                result = n;
188 +            else {
189 +                FibAction f1 = new FibAction(n - 1);
190 +                FibAction f2 = new FibAction(n - 2);
191 +                invokeAll(f1, f2);
192 +                result = f1.result + f2.result;
193 +            }
194 +        }
195 +    }
196 +
197 +    // A recursive action failing in base case
198 +    static final class FailingFibAction extends RecursiveAction {
199 +        final int number;
200 +        int result;
201 +        FailingFibAction(int n) { number = n; }
202 +        public void compute() {
203 +            int n = number;
204 +            if (n <= 1)
205 +                throw new FJException();
206 +            else {
207 +                FailingFibAction f1 = new FailingFibAction(n - 1);
208 +                FailingFibAction f2 = new FailingFibAction(n - 2);
209 +                invokeAll(f1, f2);
210 +                result = f1.result + f2.result;
211 +            }
212 +        }
213 +    }
214 +
215 +    /**
216 +     * invoke returns when task completes normally.
217 +     * isCompletedAbnormally and isCancelled return false for normally
218 +     * completed tasks. getRawResult of a RecursiveAction returns null;
219 +     */
220 +    public void testInvoke() {
221 +        RecursiveAction a = new CheckedRecursiveAction() {
222 +            protected void realCompute() {
223 +                FibAction f = new FibAction(8);
224 +                assertNull(f.invoke());
225 +                assertEquals(21, f.result);
226 +                checkCompletedNormally(f);
227 +            }};
228 +        checkInvoke(a);
229 +    }
230 +
231 +    /**
232 +     * quietlyInvoke task returns when task completes normally.
233 +     * isCompletedAbnormally and isCancelled return false for normally
234 +     * completed tasks
235 +     */
236 +    public void testQuietlyInvoke() {
237 +        RecursiveAction a = new CheckedRecursiveAction() {
238 +            protected void realCompute() {
239 +                FibAction f = new FibAction(8);
240 +                f.quietlyInvoke();
241 +                assertEquals(21, f.result);
242 +                checkCompletedNormally(f);
243 +            }};
244 +        checkInvoke(a);
245 +    }
246 +
247 +    /**
248 +     * join of a forked task returns when task completes
249 +     */
250 +    public void testForkJoin() {
251 +        RecursiveAction a = new CheckedRecursiveAction() {
252 +            protected void realCompute() {
253 +                FibAction f = new FibAction(8);
254 +                assertSame(f, f.fork());
255 +                assertNull(f.join());
256 +                assertEquals(21, f.result);
257 +                checkCompletedNormally(f);
258 +            }};
259 +        checkInvoke(a);
260 +    }
261 +
262 +    /**
263 +     * join/quietlyJoin of a forked task succeeds in the presence of interrupts
264 +     */
265 +    public void testJoinIgnoresInterrupts() {
266 +        RecursiveAction a = new CheckedRecursiveAction() {
267 +            protected void realCompute() {
268 +                FibAction f = new FibAction(8);
269 +                final Thread myself = Thread.currentThread();
270 +
271 +                // test join()
272 +                assertSame(f, f.fork());
273 +                myself.interrupt();
274 +                assertTrue(myself.isInterrupted());
275 +                assertNull(f.join());
276 +                Thread.interrupted();
277 +                assertEquals(21, f.result);
278 +                checkCompletedNormally(f);
279 +
280 +                f = new FibAction(8);
281 +                f.cancel(true);
282 +                assertSame(f, f.fork());
283 +                myself.interrupt();
284 +                assertTrue(myself.isInterrupted());
285 +                try {
286 +                    f.join();
287 +                    shouldThrow();
288 +                } catch (CancellationException success) {
289 +                    Thread.interrupted();
290 +                    checkCancelled(f);
291 +                }
292 +
293 +                f = new FibAction(8);
294 +                f.completeExceptionally(new FJException());
295 +                assertSame(f, f.fork());
296 +                myself.interrupt();
297 +                assertTrue(myself.isInterrupted());
298 +                try {
299 +                    f.join();
300 +                    shouldThrow();
301 +                } catch (FJException success) {
302 +                    Thread.interrupted();
303 +                    checkCompletedAbnormally(f, success);
304 +                }
305 +
306 +                // test quietlyJoin()
307 +                f = new FibAction(8);
308 +                assertSame(f, f.fork());
309 +                myself.interrupt();
310 +                assertTrue(myself.isInterrupted());
311 +                f.quietlyJoin();
312 +                Thread.interrupted();
313 +                assertEquals(21, f.result);
314 +                checkCompletedNormally(f);
315 +
316 +                f = new FibAction(8);
317 +                f.cancel(true);
318 +                assertSame(f, f.fork());
319 +                myself.interrupt();
320 +                assertTrue(myself.isInterrupted());
321 +                f.quietlyJoin();
322 +                Thread.interrupted();
323 +                checkCancelled(f);
324 +
325 +                f = new FibAction(8);
326 +                f.completeExceptionally(new FJException());
327 +                assertSame(f, f.fork());
328 +                myself.interrupt();
329 +                assertTrue(myself.isInterrupted());
330 +                f.quietlyJoin();
331 +                Thread.interrupted();
332 +                checkCompletedAbnormally(f, f.getException());
333 +            }};
334 +        checkInvoke(a);
335 +        a.reinitialize();
336 +        checkInvoke(a);
337 +    }
338 +
339 +    /**
340 +     * get of a forked task returns when task completes
341 +     */
342 +    public void testForkGet() {
343 +        RecursiveAction a = new CheckedRecursiveAction() {
344 +            protected void realCompute() throws Exception {
345 +                FibAction f = new FibAction(8);
346 +                assertSame(f, f.fork());
347 +                assertNull(f.get());
348 +                assertEquals(21, f.result);
349 +                checkCompletedNormally(f);
350 +            }};
351 +        checkInvoke(a);
352 +    }
353 +
354 +    /**
355 +     * timed get of a forked task returns when task completes
356 +     */
357 +    public void testForkTimedGet() {
358 +        RecursiveAction a = new CheckedRecursiveAction() {
359 +            protected void realCompute() throws Exception {
360 +                FibAction f = new FibAction(8);
361 +                assertSame(f, f.fork());
362 +                assertNull(f.get(5L, SECONDS));
363 +                assertEquals(21, f.result);
364 +                checkCompletedNormally(f);
365 +            }};
366 +        checkInvoke(a);
367 +    }
368 +
369 +    /**
370 +     * timed get with null time unit throws NPE
371 +     */
372 +    public void testForkTimedGetNPE() {
373 +        RecursiveAction a = new CheckedRecursiveAction() {
374 +            protected void realCompute() throws Exception {
375 +                FibAction f = new FibAction(8);
376 +                assertSame(f, f.fork());
377 +                try {
378 +                    f.get(5L, null);
379 +                    shouldThrow();
380 +                } catch (NullPointerException success) {}
381 +            }};
382 +        checkInvoke(a);
383 +    }
384 +
385 +    /**
386 +     * quietlyJoin of a forked task returns when task completes
387 +     */
388 +    public void testForkQuietlyJoin() {
389 +        RecursiveAction a = new CheckedRecursiveAction() {
390 +            protected void realCompute() {
391 +                FibAction f = new FibAction(8);
392 +                assertSame(f, f.fork());
393 +                f.quietlyJoin();
394 +                assertEquals(21, f.result);
395 +                checkCompletedNormally(f);
396 +            }};
397 +        checkInvoke(a);
398 +    }
399 +
400 +    /**
401 +     * invoke task throws exception when task completes abnormally
402 +     */
403 +    public void testAbnormalInvoke() {
404 +        RecursiveAction a = new CheckedRecursiveAction() {
405 +            protected void realCompute() {
406 +                FailingFibAction f = new FailingFibAction(8);
407 +                try {
408 +                    f.invoke();
409 +                    shouldThrow();
410 +                } catch (FJException success) {
411 +                    checkCompletedAbnormally(f, success);
412 +                }
413 +            }};
414 +        checkInvoke(a);
415 +    }
416 +
417 +    /**
418 +     * quietlyInvoke task returns when task completes abnormally
419 +     */
420 +    public void testAbnormalQuietlyInvoke() {
421 +        RecursiveAction a = new CheckedRecursiveAction() {
422 +            protected void realCompute() {
423 +                FailingFibAction f = new FailingFibAction(8);
424 +                f.quietlyInvoke();
425 +                assertTrue(f.getException() instanceof FJException);
426 +                checkCompletedAbnormally(f, f.getException());
427 +            }};
428 +        checkInvoke(a);
429 +    }
430 +
431 +    /**
432 +     * join of a forked task throws exception when task completes abnormally
433 +     */
434 +    public void testAbnormalForkJoin() {
435 +        RecursiveAction a = new CheckedRecursiveAction() {
436 +            protected void realCompute() {
437 +                FailingFibAction f = new FailingFibAction(8);
438 +                assertSame(f, f.fork());
439 +                try {
440 +                    f.join();
441 +                    shouldThrow();
442 +                } catch (FJException success) {
443 +                    checkCompletedAbnormally(f, success);
444 +                }
445 +            }};
446 +        checkInvoke(a);
447 +    }
448 +
449 +    /**
450 +     * get of a forked task throws exception when task completes abnormally
451 +     */
452 +    public void testAbnormalForkGet() {
453 +        RecursiveAction a = new CheckedRecursiveAction() {
454 +            protected void realCompute() throws Exception {
455 +                FailingFibAction f = new FailingFibAction(8);
456 +                assertSame(f, f.fork());
457 +                try {
458 +                    f.get();
459 +                    shouldThrow();
460 +                } catch (ExecutionException success) {
461 +                    Throwable cause = success.getCause();
462 +                    assertTrue(cause instanceof FJException);
463 +                    checkCompletedAbnormally(f, cause);
464 +                }
465 +            }};
466 +        checkInvoke(a);
467 +    }
468 +
469 +    /**
470 +     * timed get of a forked task throws exception when task completes abnormally
471 +     */
472 +    public void testAbnormalForkTimedGet() {
473 +        RecursiveAction a = new CheckedRecursiveAction() {
474 +            protected void realCompute() throws Exception {
475 +                FailingFibAction f = new FailingFibAction(8);
476 +                assertSame(f, f.fork());
477 +                try {
478 +                    f.get(5L, SECONDS);
479 +                    shouldThrow();
480 +                } catch (ExecutionException success) {
481 +                    Throwable cause = success.getCause();
482 +                    assertTrue(cause instanceof FJException);
483 +                    checkCompletedAbnormally(f, cause);
484 +                }
485 +            }};
486 +        checkInvoke(a);
487 +    }
488 +
489 +    /**
490 +     * quietlyJoin of a forked task returns when task completes abnormally
491 +     */
492 +    public void testAbnormalForkQuietlyJoin() {
493 +        RecursiveAction a = new CheckedRecursiveAction() {
494 +            protected void realCompute() {
495 +                FailingFibAction f = new FailingFibAction(8);
496 +                assertSame(f, f.fork());
497 +                f.quietlyJoin();
498 +                assertTrue(f.getException() instanceof FJException);
499 +                checkCompletedAbnormally(f, f.getException());
500 +            }};
501 +        checkInvoke(a);
502 +    }
503 +
504 +    /**
505 +     * invoke task throws exception when task cancelled
506 +     */
507 +    public void testCancelledInvoke() {
508 +        RecursiveAction a = new CheckedRecursiveAction() {
509 +            protected void realCompute() {
510 +                FibAction f = new FibAction(8);
511 +                assertTrue(f.cancel(true));
512 +                try {
513 +                    f.invoke();
514 +                    shouldThrow();
515 +                } catch (CancellationException success) {
516 +                    checkCancelled(f);
517 +                }
518 +            }};
519 +        checkInvoke(a);
520 +    }
521 +
522 +    /**
523 +     * join of a forked task throws exception when task cancelled
524 +     */
525 +    public void testCancelledForkJoin() {
526 +        RecursiveAction a = new CheckedRecursiveAction() {
527 +            protected void realCompute() {
528 +                FibAction f = new FibAction(8);
529 +                assertTrue(f.cancel(true));
530 +                assertSame(f, f.fork());
531 +                try {
532 +                    f.join();
533 +                    shouldThrow();
534 +                } catch (CancellationException success) {
535 +                    checkCancelled(f);
536 +                }
537 +            }};
538 +        checkInvoke(a);
539 +    }
540 +
541 +    /**
542 +     * get of a forked task throws exception when task cancelled
543 +     */
544 +    public void testCancelledForkGet() {
545 +        RecursiveAction a = new CheckedRecursiveAction() {
546 +            protected void realCompute() throws Exception {
547 +                FibAction f = new FibAction(8);
548 +                assertTrue(f.cancel(true));
549 +                assertSame(f, f.fork());
550 +                try {
551 +                    f.get();
552 +                    shouldThrow();
553 +                } catch (CancellationException success) {
554 +                    checkCancelled(f);
555 +                }
556 +            }};
557 +        checkInvoke(a);
558 +    }
559 +
560 +    /**
561 +     * timed get of a forked task throws exception when task cancelled
562 +     */
563 +    public void testCancelledForkTimedGet() {
564 +        RecursiveAction a = new CheckedRecursiveAction() {
565 +            protected void realCompute() throws Exception {
566 +                FibAction f = new FibAction(8);
567 +                assertTrue(f.cancel(true));
568 +                assertSame(f, f.fork());
569 +                try {
570 +                    f.get(5L, SECONDS);
571 +                    shouldThrow();
572 +                } catch (CancellationException success) {
573 +                    checkCancelled(f);
574 +                }
575 +            }};
576 +        checkInvoke(a);
577 +    }
578 +
579 +    /**
580 +     * quietlyJoin of a forked task returns when task cancelled
581 +     */
582 +    public void testCancelledForkQuietlyJoin() {
583 +        RecursiveAction a = new CheckedRecursiveAction() {
584 +            protected void realCompute() {
585 +                FibAction f = new FibAction(8);
586 +                assertTrue(f.cancel(true));
587 +                assertSame(f, f.fork());
588 +                f.quietlyJoin();
589 +                checkCancelled(f);
590 +            }};
591 +        checkInvoke(a);
592 +    }
593 +
594 +    /**
595 +     * inForkJoinPool of non-FJ task returns false
596 +     */
597 +    public void testInForkJoinPool2() {
598 +        RecursiveAction a = new CheckedRecursiveAction() {
599 +            protected void realCompute() {
600 +                assertFalse(inForkJoinPool());
601 +            }};
602 +        assertNull(a.invoke());
603 +    }
604 +
605 +    /**
606 +     * A reinitialized normally completed task may be re-invoked
607 +     */
608 +    public void testReinitialize() {
609 +        RecursiveAction a = new CheckedRecursiveAction() {
610 +            protected void realCompute() {
611 +                FibAction f = new FibAction(8);
612 +                checkNotDone(f);
613 +
614 +                for (int i = 0; i < 3; i++) {
615 +                    assertNull(f.invoke());
616 +                    assertEquals(21, f.result);
617 +                    checkCompletedNormally(f);
618 +                    f.reinitialize();
619 +                    checkNotDone(f);
620 +                }
621 +            }};
622 +        checkInvoke(a);
623 +    }
624 +
625 +    /**
626 +     * A reinitialized abnormally completed task may be re-invoked
627 +     */
628 +    public void testReinitializeAbnormal() {
629 +        RecursiveAction a = new CheckedRecursiveAction() {
630 +            protected void realCompute() {
631 +                FailingFibAction f = new FailingFibAction(8);
632 +                checkNotDone(f);
633 +
634 +                for (int i = 0; i < 3; i++) {
635 +                    try {
636 +                        f.invoke();
637 +                        shouldThrow();
638 +                    } catch (FJException success) {
639 +                        checkCompletedAbnormally(f, success);
640 +                    }
641 +                    f.reinitialize();
642 +                    checkNotDone(f);
643 +                }
644 +            }};
645 +        checkInvoke(a);
646 +    }
647 +
648 +    /**
649 +     * invoke task throws exception after invoking completeExceptionally
650 +     */
651 +    public void testCompleteExceptionally() {
652 +        RecursiveAction a = new CheckedRecursiveAction() {
653 +            protected void realCompute() {
654 +                FibAction f = new FibAction(8);
655 +                f.completeExceptionally(new FJException());
656 +                try {
657 +                    f.invoke();
658 +                    shouldThrow();
659 +                } catch (FJException success) {
660 +                    checkCompletedAbnormally(f, success);
661 +                }
662 +            }};
663 +        checkInvoke(a);
664 +    }
665 +
666 +    /**
667 +     * invoke task suppresses execution invoking complete
668 +     */
669 +    public void testComplete() {
670 +        RecursiveAction a = new CheckedRecursiveAction() {
671 +            protected void realCompute() {
672 +                FibAction f = new FibAction(8);
673 +                f.complete(null);
674 +                assertNull(f.invoke());
675 +                assertEquals(0, f.result);
676 +                checkCompletedNormally(f);
677 +            }};
678 +        checkInvoke(a);
679 +    }
680 +
681 +    /**
682 +     * invokeAll(t1, t2) invokes all task arguments
683 +     */
684 +    public void testInvokeAll2() {
685 +        RecursiveAction a = new CheckedRecursiveAction() {
686 +            protected void realCompute() {
687 +                FibAction f = new FibAction(8);
688 +                FibAction g = new FibAction(9);
689 +                invokeAll(f, g);
690 +                checkCompletedNormally(f);
691 +                assertEquals(21, f.result);
692 +                checkCompletedNormally(g);
693 +                assertEquals(34, g.result);
694 +            }};
695 +        checkInvoke(a);
696 +    }
697 +
698 +    /**
699 +     * invokeAll(tasks) with 1 argument invokes task
700 +     */
701 +    public void testInvokeAll1() {
702 +        RecursiveAction a = new CheckedRecursiveAction() {
703 +            protected void realCompute() {
704 +                FibAction f = new FibAction(8);
705 +                invokeAll(f);
706 +                checkCompletedNormally(f);
707 +                assertEquals(21, f.result);
708 +            }};
709 +        checkInvoke(a);
710 +    }
711 +
712 +    /**
713 +     * invokeAll(tasks) with > 2 argument invokes tasks
714 +     */
715 +    public void testInvokeAll3() {
716 +        RecursiveAction a = new CheckedRecursiveAction() {
717 +            protected void realCompute() {
718 +                FibAction f = new FibAction(8);
719 +                FibAction g = new FibAction(9);
720 +                FibAction h = new FibAction(7);
721 +                invokeAll(f, g, h);
722 +                assertTrue(f.isDone());
723 +                assertTrue(g.isDone());
724 +                assertTrue(h.isDone());
725 +                checkCompletedNormally(f);
726 +                assertEquals(21, f.result);
727 +                checkCompletedNormally(g);
728 +                assertEquals(34, g.result);
729 +                checkCompletedNormally(g);
730 +                assertEquals(13, h.result);
731 +            }};
732 +        checkInvoke(a);
733 +    }
734 +
735 +    /**
736 +     * invokeAll(collection) invokes all tasks in the collection
737 +     */
738 +    public void testInvokeAllCollection() {
739 +        RecursiveAction a = new CheckedRecursiveAction() {
740 +            protected void realCompute() {
741 +                FibAction f = new FibAction(8);
742 +                FibAction g = new FibAction(9);
743 +                FibAction h = new FibAction(7);
744 +                HashSet set = new HashSet();
745 +                set.add(f);
746 +                set.add(g);
747 +                set.add(h);
748 +                invokeAll(set);
749 +                assertTrue(f.isDone());
750 +                assertTrue(g.isDone());
751 +                assertTrue(h.isDone());
752 +                checkCompletedNormally(f);
753 +                assertEquals(21, f.result);
754 +                checkCompletedNormally(g);
755 +                assertEquals(34, g.result);
756 +                checkCompletedNormally(g);
757 +                assertEquals(13, h.result);
758 +            }};
759 +        checkInvoke(a);
760 +    }
761 +
762 +    /**
763 +     * invokeAll(tasks) with any null task throws NPE
764 +     */
765 +    public void testInvokeAllNPE() {
766 +        RecursiveAction a = new CheckedRecursiveAction() {
767 +            protected void realCompute() {
768 +                FibAction f = new FibAction(8);
769 +                FibAction g = new FibAction(9);
770 +                FibAction h = null;
771 +                try {
772 +                    invokeAll(f, g, h);
773 +                    shouldThrow();
774 +                } catch (NullPointerException success) {}
775 +            }};
776 +        checkInvoke(a);
777 +    }
778 +
779 +    /**
780 +     * invokeAll(t1, t2) throw exception if any task does
781 +     */
782 +    public void testAbnormalInvokeAll2() {
783 +        RecursiveAction a = new CheckedRecursiveAction() {
784 +            protected void realCompute() {
785 +                FibAction f = new FibAction(8);
786 +                FailingFibAction g = new FailingFibAction(9);
787 +                try {
788 +                    invokeAll(f, g);
789 +                    shouldThrow();
790 +                } catch (FJException success) {
791 +                    checkCompletedAbnormally(g, success);
792 +                }
793 +            }};
794 +        checkInvoke(a);
795 +    }
796 +
797 +    /**
798 +     * invokeAll(tasks) with 1 argument throws exception if task does
799 +     */
800 +    public void testAbnormalInvokeAll1() {
801 +        RecursiveAction a = new CheckedRecursiveAction() {
802 +            protected void realCompute() {
803 +                FailingFibAction g = new FailingFibAction(9);
804 +                try {
805 +                    invokeAll(g);
806 +                    shouldThrow();
807 +                } catch (FJException success) {
808 +                    checkCompletedAbnormally(g, success);
809 +                }
810 +            }};
811 +        checkInvoke(a);
812 +    }
813 +
814 +    /**
815 +     * invokeAll(tasks) with > 2 argument throws exception if any task does
816 +     */
817 +    public void testAbnormalInvokeAll3() {
818 +        RecursiveAction a = new CheckedRecursiveAction() {
819 +            protected void realCompute() {
820 +                FibAction f = new FibAction(8);
821 +                FailingFibAction g = new FailingFibAction(9);
822 +                FibAction h = new FibAction(7);
823 +                try {
824 +                    invokeAll(f, g, h);
825 +                    shouldThrow();
826 +                } catch (FJException success) {
827 +                    checkCompletedAbnormally(g, success);
828 +                }
829 +            }};
830 +        checkInvoke(a);
831 +    }
832 +
833 +    /**
834 +     * invokeAll(collection) throws exception if any task does
835 +     */
836 +    public void testAbnormalInvokeAllCollection() {
837 +        RecursiveAction a = new CheckedRecursiveAction() {
838 +            protected void realCompute() {
839 +                FailingFibAction f = new FailingFibAction(8);
840 +                FibAction g = new FibAction(9);
841 +                FibAction h = new FibAction(7);
842 +                HashSet set = new HashSet();
843 +                set.add(f);
844 +                set.add(g);
845 +                set.add(h);
846 +                try {
847 +                    invokeAll(set);
848 +                    shouldThrow();
849 +                } catch (FJException success) {
850 +                    checkCompletedAbnormally(f, success);
851 +                }
852 +            }};
853 +        checkInvoke(a);
854 +    }
855 +
856 +    // CountedCompleter versions
857 +
858 +    abstract static class CCF extends CountedCompleter {
859 +        int number;
860 +        int rnumber;
861 +
862 +        public CCF(CountedCompleter parent, int n) {
863 +            super(parent, 1);
864 +            this.number = n;
865 +        }
866 +
867 +        public final void compute() {
868 +            CountedCompleter p;
869 +            CCF f = this;
870 +            int n = number;
871 +            while (n >= 2) {
872 +                new RCCF(f, n - 2).fork();
873 +                f = new LCCF(f, --n);
874 +            }
875 +            f.number = n;
876 +            f.onCompletion(f);
877 +            if ((p = f.getCompleter()) != null)
878 +                p.tryComplete();
879 +            else
880 +                f.quietlyComplete();
881 +        }
882 +    }
883 +
884 +    static final class LCCF extends CCF {
885 +        public LCCF(CountedCompleter parent, int n) {
886 +            super(parent, n);
887 +        }
888 +        public final void onCompletion(CountedCompleter caller) {
889 +            CCF p = (CCF)getCompleter();
890 +            int n = number + rnumber;
891 +            if (p != null)
892 +                p.number = n;
893 +            else
894 +                number = n;
895 +        }
896 +    }
897 +    static final class RCCF extends CCF {
898 +        public RCCF(CountedCompleter parent, int n) {
899 +            super(parent, n);
900 +        }
901 +        public final void onCompletion(CountedCompleter caller) {
902 +            CCF p = (CCF)getCompleter();
903 +            int n = number + rnumber;
904 +            if (p != null)
905 +                p.rnumber = n;
906 +            else
907 +                number = n;
908 +        }
909 +    }
910 +
911 +    // Version of CCF with forced failure in left completions
912 +    abstract static class FailingCCF extends CountedCompleter {
913 +        int number;
914 +        int rnumber;
915 +
916 +        public FailingCCF(CountedCompleter parent, int n) {
917 +            super(parent, 1);
918 +            this.number = n;
919 +        }
920 +
921 +        public final void compute() {
922 +            CountedCompleter p;
923 +            FailingCCF f = this;
924 +            int n = number;
925 +            while (n >= 2) {
926 +                new RFCCF(f, n - 2).fork();
927 +                f = new LFCCF(f, --n);
928 +            }
929 +            f.number = n;
930 +            f.onCompletion(f);
931 +            if ((p = f.getCompleter()) != null)
932 +                p.tryComplete();
933 +            else
934 +                f.quietlyComplete();
935 +        }
936 +    }
937 +
938 +    static final class LFCCF extends FailingCCF {
939 +        public LFCCF(CountedCompleter parent, int n) {
940 +            super(parent, n);
941 +        }
942 +        public final void onCompletion(CountedCompleter caller) {
943 +            FailingCCF p = (FailingCCF)getCompleter();
944 +            int n = number + rnumber;
945 +            if (p != null)
946 +                p.number = n;
947 +            else
948 +                number = n;
949 +        }
950 +    }
951 +    static final class RFCCF extends FailingCCF {
952 +        public RFCCF(CountedCompleter parent, int n) {
953 +            super(parent, n);
954 +        }
955 +        public final void onCompletion(CountedCompleter caller) {
956 +            completeExceptionally(new FJException());
957 +        }
958 +    }
959 +
960 +    /**
961 +     * invoke returns when task completes normally.
962 +     * isCompletedAbnormally and isCancelled return false for normally
963 +     * completed tasks; getRawResult returns null.
964 +     */
965 +    public void testInvokeCC() {
966 +        ForkJoinTask a = new CheckedRecursiveAction() {
967 +            protected void realCompute() {
968 +                CCF f = new LCCF(null, 8);
969 +                assertNull(f.invoke());
970 +                assertEquals(21, f.number);
971 +                checkCompletedNormally(f);
972 +            }};
973 +        checkInvoke(a);
974 +    }
975 +
976 +    /**
977 +     * quietlyInvoke task returns when task completes normally.
978 +     * isCompletedAbnormally and isCancelled return false for normally
979 +     * completed tasks
980 +     */
981 +    public void testQuietlyInvokeCC() {
982 +        ForkJoinTask a = new CheckedRecursiveAction() {
983 +            protected void realCompute() {
984 +                CCF f = new LCCF(null, 8);
985 +                f.quietlyInvoke();
986 +                assertEquals(21, f.number);
987 +                checkCompletedNormally(f);
988 +            }};
989 +        checkInvoke(a);
990 +    }
991 +
992 +    /**
993 +     * join of a forked task returns when task completes
994 +     */
995 +    public void testForkJoinCC() {
996 +        ForkJoinTask a = new CheckedRecursiveAction() {
997 +            protected void realCompute() {
998 +                CCF f = new LCCF(null, 8);
999 +                assertSame(f, f.fork());
1000 +                assertNull(f.join());
1001 +                assertEquals(21, f.number);
1002 +                checkCompletedNormally(f);
1003 +            }};
1004 +        checkInvoke(a);
1005 +    }
1006 +
1007 +    /**
1008 +     * get of a forked task returns when task completes
1009 +     */
1010 +    public void testForkGetCC() {
1011 +        ForkJoinTask a = new CheckedRecursiveAction() {
1012 +            protected void realCompute() throws Exception {
1013 +                CCF f = new LCCF(null, 8);
1014 +                assertSame(f, f.fork());
1015 +                assertNull(f.get());
1016 +                assertEquals(21, f.number);
1017 +                checkCompletedNormally(f);
1018 +            }};
1019 +        checkInvoke(a);
1020 +    }
1021 +
1022 +    /**
1023 +     * timed get of a forked task returns when task completes
1024 +     */
1025 +    public void testForkTimedGetCC() {
1026 +        ForkJoinTask a = new CheckedRecursiveAction() {
1027 +            protected void realCompute() throws Exception {
1028 +                CCF f = new LCCF(null, 8);
1029 +                assertSame(f, f.fork());
1030 +                assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
1031 +                assertEquals(21, f.number);
1032 +                checkCompletedNormally(f);
1033 +            }};
1034 +        checkInvoke(a);
1035 +    }
1036 +
1037 +    /**
1038 +     * timed get with null time unit throws NPE
1039 +     */
1040 +    public void testForkTimedGetNPECC() {
1041 +        ForkJoinTask a = new CheckedRecursiveAction() {
1042 +            protected void realCompute() throws Exception {
1043 +                CCF f = new LCCF(null, 8);
1044 +                assertSame(f, f.fork());
1045 +                try {
1046 +                    f.get(5L, null);
1047 +                    shouldThrow();
1048 +                } catch (NullPointerException success) {}
1049 +            }};
1050 +        checkInvoke(a);
1051 +    }
1052 +
1053 +    /**
1054 +     * quietlyJoin of a forked task returns when task completes
1055 +     */
1056 +    public void testForkQuietlyJoinCC() {
1057 +        ForkJoinTask a = new CheckedRecursiveAction() {
1058 +            protected void realCompute() {
1059 +                CCF f = new LCCF(null, 8);
1060 +                assertSame(f, f.fork());
1061 +                f.quietlyJoin();
1062 +                assertEquals(21, f.number);
1063 +                checkCompletedNormally(f);
1064 +            }};
1065 +        checkInvoke(a);
1066 +    }
1067 +
1068 +    /**
1069 +     * invoke task throws exception when task completes abnormally
1070 +     */
1071 +    public void testAbnormalInvokeCC() {
1072 +        ForkJoinTask a = new CheckedRecursiveAction() {
1073 +            protected void realCompute() {
1074 +                FailingCCF f = new LFCCF(null, 8);
1075 +                try {
1076 +                    f.invoke();
1077 +                    shouldThrow();
1078 +                } catch (FJException success) {
1079 +                    checkCompletedAbnormally(f, success);
1080 +                }
1081 +            }};
1082 +        checkInvoke(a);
1083 +    }
1084 +
1085 +    /**
1086 +     * quietlyInvoke task returns when task completes abnormally
1087 +     */
1088 +    public void testAbnormalQuietlyInvokeCC() {
1089 +        ForkJoinTask a = new CheckedRecursiveAction() {
1090 +            protected void realCompute() {
1091 +                FailingCCF f = new LFCCF(null, 8);
1092 +                f.quietlyInvoke();
1093 +                assertTrue(f.getException() instanceof FJException);
1094 +                checkCompletedAbnormally(f, f.getException());
1095 +            }};
1096 +        checkInvoke(a);
1097 +    }
1098 +
1099 +    /**
1100 +     * join of a forked task throws exception when task completes abnormally
1101 +     */
1102 +    public void testAbnormalForkJoinCC() {
1103 +        ForkJoinTask a = new CheckedRecursiveAction() {
1104 +            protected void realCompute() {
1105 +                FailingCCF f = new LFCCF(null, 8);
1106 +                assertSame(f, f.fork());
1107 +                try {
1108 +                    f.join();
1109 +                    shouldThrow();
1110 +                } catch (FJException success) {
1111 +                    checkCompletedAbnormally(f, success);
1112 +                }
1113 +            }};
1114 +        checkInvoke(a);
1115 +    }
1116 +
1117 +    /**
1118 +     * get of a forked task throws exception when task completes abnormally
1119 +     */
1120 +    public void testAbnormalForkGetCC() {
1121 +        ForkJoinTask a = new CheckedRecursiveAction() {
1122 +            protected void realCompute() throws Exception {
1123 +                FailingCCF f = new LFCCF(null, 8);
1124 +                assertSame(f, f.fork());
1125 +                try {
1126 +                    f.get();
1127 +                    shouldThrow();
1128 +                } catch (ExecutionException success) {
1129 +                    Throwable cause = success.getCause();
1130 +                    assertTrue(cause instanceof FJException);
1131 +                    checkCompletedAbnormally(f, cause);
1132 +                }
1133 +            }};
1134 +        checkInvoke(a);
1135 +    }
1136 +
1137 +    /**
1138 +     * timed get of a forked task throws exception when task completes abnormally
1139 +     */
1140 +    public void testAbnormalForkTimedGetCC() {
1141 +        ForkJoinTask a = new CheckedRecursiveAction() {
1142 +            protected void realCompute() throws Exception {
1143 +                FailingCCF f = new LFCCF(null, 8);
1144 +                assertSame(f, f.fork());
1145 +                try {
1146 +                    f.get(LONG_DELAY_MS, MILLISECONDS);
1147 +                    shouldThrow();
1148 +                } catch (ExecutionException success) {
1149 +                    Throwable cause = success.getCause();
1150 +                    assertTrue(cause instanceof FJException);
1151 +                    checkCompletedAbnormally(f, cause);
1152 +                }
1153 +            }};
1154 +        checkInvoke(a);
1155 +    }
1156 +
1157 +    /**
1158 +     * quietlyJoin of a forked task returns when task completes abnormally
1159 +     */
1160 +    public void testAbnormalForkQuietlyJoinCC() {
1161 +        ForkJoinTask a = new CheckedRecursiveAction() {
1162 +            protected void realCompute() {
1163 +                FailingCCF f = new LFCCF(null, 8);
1164 +                assertSame(f, f.fork());
1165 +                f.quietlyJoin();
1166 +                assertTrue(f.getException() instanceof FJException);
1167 +                checkCompletedAbnormally(f, f.getException());
1168 +            }};
1169 +        checkInvoke(a);
1170 +    }
1171 +
1172 +    /**
1173 +     * invoke task throws exception when task cancelled
1174 +     */
1175 +    public void testCancelledInvokeCC() {
1176 +        ForkJoinTask a = new CheckedRecursiveAction() {
1177 +            protected void realCompute() {
1178 +                CCF f = new LCCF(null, 8);
1179 +                assertTrue(f.cancel(true));
1180 +                try {
1181 +                    f.invoke();
1182 +                    shouldThrow();
1183 +                } catch (CancellationException success) {
1184 +                    checkCancelled(f);
1185 +                }
1186 +            }};
1187 +        checkInvoke(a);
1188 +    }
1189 +
1190 +    /**
1191 +     * join of a forked task throws exception when task cancelled
1192 +     */
1193 +    public void testCancelledForkJoinCC() {
1194 +        ForkJoinTask a = new CheckedRecursiveAction() {
1195 +            protected void realCompute() {
1196 +                CCF f = new LCCF(null, 8);
1197 +                assertTrue(f.cancel(true));
1198 +                assertSame(f, f.fork());
1199 +                try {
1200 +                    f.join();
1201 +                    shouldThrow();
1202 +                } catch (CancellationException success) {
1203 +                    checkCancelled(f);
1204 +                }
1205 +            }};
1206 +        checkInvoke(a);
1207 +    }
1208 +
1209 +    /**
1210 +     * get of a forked task throws exception when task cancelled
1211 +     */
1212 +    public void testCancelledForkGetCC() {
1213 +        ForkJoinTask a = new CheckedRecursiveAction() {
1214 +            protected void realCompute() throws Exception {
1215 +                CCF f = new LCCF(null, 8);
1216 +                assertTrue(f.cancel(true));
1217 +                assertSame(f, f.fork());
1218 +                try {
1219 +                    f.get();
1220 +                    shouldThrow();
1221 +                } catch (CancellationException success) {
1222 +                    checkCancelled(f);
1223 +                }
1224 +            }};
1225 +        checkInvoke(a);
1226 +    }
1227 +
1228 +    /**
1229 +     * timed get of a forked task throws exception when task cancelled
1230 +     */
1231 +    public void testCancelledForkTimedGetCC() throws Exception {
1232 +        ForkJoinTask a = new CheckedRecursiveAction() {
1233 +            protected void realCompute() throws Exception {
1234 +                CCF f = new LCCF(null, 8);
1235 +                assertTrue(f.cancel(true));
1236 +                assertSame(f, f.fork());
1237 +                try {
1238 +                    f.get(LONG_DELAY_MS, MILLISECONDS);
1239 +                    shouldThrow();
1240 +                } catch (CancellationException success) {
1241 +                    checkCancelled(f);
1242 +                }
1243 +            }};
1244 +        checkInvoke(a);
1245 +    }
1246 +
1247 +    /**
1248 +     * quietlyJoin of a forked task returns when task cancelled
1249 +     */
1250 +    public void testCancelledForkQuietlyJoinCC() {
1251 +        ForkJoinTask a = new CheckedRecursiveAction() {
1252 +            protected void realCompute() {
1253 +                CCF f = new LCCF(null, 8);
1254 +                assertTrue(f.cancel(true));
1255 +                assertSame(f, f.fork());
1256 +                f.quietlyJoin();
1257 +                checkCancelled(f);
1258 +            }};
1259 +        checkInvoke(a);
1260 +    }
1261 +
1262 +    /**
1263 +     * getPool of non-FJ task returns null
1264 +     */
1265 +    public void testGetPool2CC() {
1266 +        ForkJoinTask a = new CheckedRecursiveAction() {
1267 +            protected void realCompute() {
1268 +                assertNull(getPool());
1269 +            }};
1270 +        assertNull(a.invoke());
1271 +    }
1272 +
1273 +    /**
1274 +     * inForkJoinPool of non-FJ task returns false
1275 +     */
1276 +    public void testInForkJoinPool2CC() {
1277 +        ForkJoinTask a = new CheckedRecursiveAction() {
1278 +            protected void realCompute() {
1279 +                assertFalse(inForkJoinPool());
1280 +            }};
1281 +        assertNull(a.invoke());
1282 +    }
1283 +
1284 +    /**
1285 +     * setRawResult(null) succeeds
1286 +     */
1287 +    public void testSetRawResultCC() {
1288 +        ForkJoinTask a = new CheckedRecursiveAction() {
1289 +            protected void realCompute() {
1290 +                setRawResult(null);
1291 +                assertNull(getRawResult());
1292 +            }};
1293 +        assertNull(a.invoke());
1294 +    }
1295 +
1296 +    /**
1297 +     * invoke task throws exception after invoking completeExceptionally
1298 +     */
1299 +    public void testCompleteExceptionally2CC() {
1300 +        ForkJoinTask a = new CheckedRecursiveAction() {
1301 +            protected void realCompute() {
1302 +                CCF f = new LCCF(null, 8);
1303 +                f.completeExceptionally(new FJException());
1304 +                try {
1305 +                    f.invoke();
1306 +                    shouldThrow();
1307 +                } catch (FJException success) {
1308 +                    checkCompletedAbnormally(f, success);
1309 +                }
1310 +            }};
1311 +        checkInvoke(a);
1312 +    }
1313 +
1314 +    /**
1315 +     * invokeAll(t1, t2) invokes all task arguments
1316 +     */
1317 +    public void testInvokeAll2CC() {
1318 +        ForkJoinTask a = new CheckedRecursiveAction() {
1319 +            protected void realCompute() {
1320 +                CCF f = new LCCF(null, 8);
1321 +                CCF g = new LCCF(null, 9);
1322 +                invokeAll(f, g);
1323 +                assertEquals(21, f.number);
1324 +                assertEquals(34, g.number);
1325 +                checkCompletedNormally(f);
1326 +                checkCompletedNormally(g);
1327 +            }};
1328 +        checkInvoke(a);
1329 +    }
1330 +
1331 +    /**
1332 +     * invokeAll(tasks) with 1 argument invokes task
1333 +     */
1334 +    public void testInvokeAll1CC() {
1335 +        ForkJoinTask a = new CheckedRecursiveAction() {
1336 +            protected void realCompute() {
1337 +                CCF f = new LCCF(null, 8);
1338 +                invokeAll(f);
1339 +                checkCompletedNormally(f);
1340 +                assertEquals(21, f.number);
1341 +            }};
1342 +        checkInvoke(a);
1343 +    }
1344 +
1345 +    /**
1346 +     * invokeAll(tasks) with > 2 argument invokes tasks
1347 +     */
1348 +    public void testInvokeAll3CC() {
1349 +        ForkJoinTask a = new CheckedRecursiveAction() {
1350 +            protected void realCompute() {
1351 +                CCF f = new LCCF(null, 8);
1352 +                CCF g = new LCCF(null, 9);
1353 +                CCF h = new LCCF(null, 7);
1354 +                invokeAll(f, g, h);
1355 +                assertEquals(21, f.number);
1356 +                assertEquals(34, g.number);
1357 +                assertEquals(13, h.number);
1358 +                checkCompletedNormally(f);
1359 +                checkCompletedNormally(g);
1360 +                checkCompletedNormally(h);
1361 +            }};
1362 +        checkInvoke(a);
1363 +    }
1364 +
1365 +    /**
1366 +     * invokeAll(collection) invokes all tasks in the collection
1367 +     */
1368 +    public void testInvokeAllCollectionCC() {
1369 +        ForkJoinTask a = new CheckedRecursiveAction() {
1370 +            protected void realCompute() {
1371 +                CCF f = new LCCF(null, 8);
1372 +                CCF g = new LCCF(null, 9);
1373 +                CCF h = new LCCF(null, 7);
1374 +                HashSet set = new HashSet();
1375 +                set.add(f);
1376 +                set.add(g);
1377 +                set.add(h);
1378 +                invokeAll(set);
1379 +                assertEquals(21, f.number);
1380 +                assertEquals(34, g.number);
1381 +                assertEquals(13, h.number);
1382 +                checkCompletedNormally(f);
1383 +                checkCompletedNormally(g);
1384 +                checkCompletedNormally(h);
1385 +            }};
1386 +        checkInvoke(a);
1387 +    }
1388 +
1389 +    /**
1390 +     * invokeAll(tasks) with any null task throws NPE
1391 +     */
1392 +    public void testInvokeAllNPECC() {
1393 +        ForkJoinTask a = new CheckedRecursiveAction() {
1394 +            protected void realCompute() {
1395 +                CCF f = new LCCF(null, 8);
1396 +                CCF g = new LCCF(null, 9);
1397 +                CCF h = null;
1398 +                try {
1399 +                    invokeAll(f, g, h);
1400 +                    shouldThrow();
1401 +                } catch (NullPointerException success) {}
1402 +            }};
1403 +        checkInvoke(a);
1404 +    }
1405 +
1406 +    /**
1407 +     * invokeAll(t1, t2) throw exception if any task does
1408 +     */
1409 +    public void testAbnormalInvokeAll2CC() {
1410 +        ForkJoinTask a = new CheckedRecursiveAction() {
1411 +            protected void realCompute() {
1412 +                CCF f = new LCCF(null, 8);
1413 +                FailingCCF g = new LFCCF(null, 9);
1414 +                try {
1415 +                    invokeAll(f, g);
1416 +                    shouldThrow();
1417 +                } catch (FJException success) {
1418 +                    checkCompletedAbnormally(g, success);
1419 +                }
1420 +            }};
1421 +        checkInvoke(a);
1422 +    }
1423 +
1424 +    /**
1425 +     * invokeAll(tasks) with 1 argument throws exception if task does
1426 +     */
1427 +    public void testAbnormalInvokeAll1CC() {
1428 +        ForkJoinTask a = new CheckedRecursiveAction() {
1429 +            protected void realCompute() {
1430 +                FailingCCF g = new LFCCF(null, 9);
1431 +                try {
1432 +                    invokeAll(g);
1433 +                    shouldThrow();
1434 +                } catch (FJException success) {
1435 +                    checkCompletedAbnormally(g, success);
1436 +                }
1437 +            }};
1438 +        checkInvoke(a);
1439 +    }
1440 +
1441 +    /**
1442 +     * invokeAll(tasks) with > 2 argument throws exception if any task does
1443 +     */
1444 +    public void testAbnormalInvokeAll3CC() {
1445 +        ForkJoinTask a = new CheckedRecursiveAction() {
1446 +            protected void realCompute() {
1447 +                CCF f = new LCCF(null, 8);
1448 +                FailingCCF g = new LFCCF(null, 9);
1449 +                CCF h = new LCCF(null, 7);
1450 +                try {
1451 +                    invokeAll(f, g, h);
1452 +                    shouldThrow();
1453 +                } catch (FJException success) {
1454 +                    checkCompletedAbnormally(g, success);
1455 +                }
1456 +            }};
1457 +        checkInvoke(a);
1458 +    }
1459 +
1460 +    /**
1461 +     * invokeAll(collection) throws exception if any task does
1462 +     */
1463 +    public void testAbnormalInvokeAllCollectionCC() {
1464 +        ForkJoinTask a = new CheckedRecursiveAction() {
1465 +            protected void realCompute() {
1466 +                FailingCCF f = new LFCCF(null, 8);
1467 +                CCF g = new LCCF(null, 9);
1468 +                CCF h = new LCCF(null, 7);
1469 +                HashSet set = new HashSet();
1470 +                set.add(f);
1471 +                set.add(g);
1472 +                set.add(h);
1473 +                try {
1474 +                    invokeAll(set);
1475 +                    shouldThrow();
1476 +                } catch (FJException success) {
1477 +                    checkCompletedAbnormally(f, success);
1478 +                }
1479 +            }};
1480 +        checkInvoke(a);
1481 +    }
1482 +
1483 +    /**
1484 +     * awaitQuiescence by a worker is equivalent in effect to
1485 +     * ForkJoinTask.helpQuiesce()
1486 +     */
1487 +    public void testAwaitQuiescence1() throws Exception {
1488 +        final ForkJoinPool p = new ForkJoinPool();
1489 +        try {
1490 +            final long startTime = System.nanoTime();
1491 +            assertTrue(p.isQuiescent());
1492 +            ForkJoinTask a = new CheckedRecursiveAction() {
1493 +                protected void realCompute() {
1494 +                    FibAction f = new FibAction(8);
1495 +                    assertSame(f, f.fork());
1496 +                    assertSame(p, ForkJoinTask.getPool());
1497 +                    boolean quiescent = p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS);
1498 +                    assertTrue(quiescent);
1499 +                    assertFalse(p.isQuiescent());
1500 +                    while (!f.isDone()) {
1501 +                        assertFalse(p.getAsyncMode());
1502 +                        assertFalse(p.isShutdown());
1503 +                        assertFalse(p.isTerminating());
1504 +                        assertFalse(p.isTerminated());
1505 +                        Thread.yield();
1506 +                    }
1507 +                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1508 +                    assertFalse(p.isQuiescent());
1509 +                    assertEquals(0, ForkJoinTask.getQueuedTaskCount());
1510 +                    assertEquals(21, f.result);
1511 +                }};
1512 +            p.execute(a);
1513 +            while (!a.isDone() || !p.isQuiescent()) {
1514 +                assertFalse(p.getAsyncMode());
1515 +                assertFalse(p.isShutdown());
1516 +                assertFalse(p.isTerminating());
1517 +                assertFalse(p.isTerminated());
1518 +                Thread.yield();
1519 +            }
1520 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1521 +            assertEquals(0, p.getQueuedTaskCount());
1522 +            assertFalse(p.getAsyncMode());
1523 +            assertEquals(0, p.getActiveThreadCount());
1524 +            assertEquals(0, p.getQueuedTaskCount());
1525 +            assertEquals(0, p.getQueuedSubmissionCount());
1526 +            assertFalse(p.hasQueuedSubmissions());
1527 +            assertFalse(p.isShutdown());
1528 +            assertFalse(p.isTerminating());
1529 +            assertFalse(p.isTerminated());
1530 +        } finally {
1531 +            joinPool(p);
1532 +        }
1533 +    }
1534 +
1535 +    /**
1536 +     * awaitQuiescence returns when pool isQuiescent() or the indicated
1537 +     * timeout elapsed
1538 +     */
1539 +    public void testAwaitQuiescence2() throws Exception {
1540 +        /**
1541 +         * """It is possible to disable or limit the use of threads in the
1542 +         * common pool by setting the parallelism property to zero. However
1543 +         * doing so may cause unjoined tasks to never be executed."""
1544 +         */
1545 +        if ("0".equals(System.getProperty(
1546 +             "java.util.concurrent.ForkJoinPool.common.parallelism")))
1547 +            return;
1548 +        final ForkJoinPool p = new ForkJoinPool();
1549 +        try {
1550 +            assertTrue(p.isQuiescent());
1551 +            final long startTime = System.nanoTime();
1552 +            ForkJoinTask a = new CheckedRecursiveAction() {
1553 +                protected void realCompute() {
1554 +                    FibAction f = new FibAction(8);
1555 +                    assertSame(f, f.fork());
1556 +                    while (!f.isDone()
1557 +                           && millisElapsedSince(startTime) < LONG_DELAY_MS) {
1558 +                        assertFalse(p.getAsyncMode());
1559 +                        assertFalse(p.isShutdown());
1560 +                        assertFalse(p.isTerminating());
1561 +                        assertFalse(p.isTerminated());
1562 +                        Thread.yield();
1563 +                    }
1564 +                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1565 +                    assertEquals(0, ForkJoinTask.getQueuedTaskCount());
1566 +                    assertEquals(21, f.result);
1567 +                }};
1568 +            p.execute(a);
1569 +            assertTrue(p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS));
1570 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1571 +            assertTrue(p.isQuiescent());
1572 +            assertTrue(a.isDone());
1573 +            assertEquals(0, p.getQueuedTaskCount());
1574 +            assertFalse(p.getAsyncMode());
1575 +            assertEquals(0, p.getActiveThreadCount());
1576 +            assertEquals(0, p.getQueuedTaskCount());
1577 +            assertEquals(0, p.getQueuedSubmissionCount());
1578 +            assertFalse(p.hasQueuedSubmissions());
1579 +            assertFalse(p.isShutdown());
1580 +            assertFalse(p.isTerminating());
1581 +            assertFalse(p.isTerminated());
1582 +        } finally {
1583 +            joinPool(p);
1584 +        }
1585 +    }
1586 +
1587   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines