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.3 by dl, Thu Mar 21 16:26:43 2013 UTC vs.
Revision 1.41 by dl, Tue Jan 26 13:33:06 2021 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines