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

Comparing jsr166/src/test/tck/ForkJoinTask8Test.java (file contents):
Revision 1.1 by dl, Sun Jul 21 22:24:18 2013 UTC vs.
Revision 1.22 by dl, Thu Oct 8 23:02:21 2015 UTC

# Line 3 | Line 3
3   * Expert Group and released to the public domain, as explained at
4   * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6 +
7 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
8 + import static java.util.concurrent.TimeUnit.SECONDS;
9 +
10 + import java.util.Arrays;
11 + import java.util.Collections;
12 + import java.util.concurrent.CountDownLatch;
13   import java.util.concurrent.ExecutionException;
7 import java.util.concurrent.CancellationException;
14   import java.util.concurrent.ForkJoinPool;
15   import java.util.concurrent.ForkJoinTask;
16   import java.util.concurrent.ForkJoinWorkerThread;
17   import java.util.concurrent.RecursiveAction;
12 import java.util.concurrent.TimeUnit;
18   import java.util.concurrent.TimeoutException;
19 < import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
20 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
21 < import static java.util.concurrent.TimeUnit.SECONDS;
17 < import java.util.HashSet;
18 < import junit.framework.*;
19 >
20 > import junit.framework.Test;
21 > import junit.framework.TestSuite;
22  
23   public class ForkJoinTask8Test extends JSR166TestCase {
24  
# Line 32 | Line 35 | public class ForkJoinTask8Test extends J
35      static final short INITIAL_STATE = -1;
36      static final short COMPLETE_STATE = 0;
37      static final short EXCEPTION_STATE = 1;
35        
38  
39      public static void main(String[] args) {
40 <        junit.textui.TestRunner.run(suite());
40 >        main(suite(), args);
41      }
42  
43      public static Test suite() {
# Line 60 | Line 62 | public class ForkJoinTask8Test extends J
62                                  null, true);
63      }
64  
65 +    // Compute fib naively and efficiently
66 +    final int[] fib;
67 +    {
68 +        int[] fib = new int[10];
69 +        fib[0] = 0;
70 +        fib[1] = 1;
71 +        for (int i = 2; i < fib.length; i++)
72 +            fib[i] = fib[i - 1] + fib[i - 2];
73 +        this.fib = fib;
74 +    }
75 +
76      private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
77 <        try {
77 >        try (PoolCleaner cleaner = cleaner(pool)) {
78              assertFalse(a.isDone());
79              assertFalse(a.isCompletedNormally());
80              assertFalse(a.isCompletedAbnormally());
# Line 77 | Line 90 | public class ForkJoinTask8Test extends J
90              assertFalse(a.isCancelled());
91              assertNull(a.getException());
92              assertNull(a.getRawResult());
80        } finally {
81            joinPool(pool);
93          }
94      }
95  
# Line 115 | Line 126 | public class ForkJoinTask8Test extends J
126  
127          {
128              Thread.currentThread().interrupt();
129 <            long t0 = System.nanoTime();
129 >            long startTime = System.nanoTime();
130              assertSame(expected, a.join());
131 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
131 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
132              Thread.interrupted();
133          }
134  
135          {
136              Thread.currentThread().interrupt();
137 <            long t0 = System.nanoTime();
137 >            long startTime = System.nanoTime();
138              a.quietlyJoin();        // should be no-op
139 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
139 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
140              Thread.interrupted();
141          }
142  
# Line 161 | Line 172 | public class ForkJoinTask8Test extends J
172          Thread.interrupted();
173  
174          {
175 <            long t0 = System.nanoTime();
175 >            long startTime = System.nanoTime();
176              a.quietlyJoin();        // should be no-op
177 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
177 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
178          }
179  
180          try {
# Line 181 | Line 192 | public class ForkJoinTask8Test extends J
192          } catch (Throwable fail) { threadUnexpectedException(fail); }
193      }
194  
184
195      public static final class FJException extends RuntimeException {
196          FJException() { super(); }
197      }
198  
199      abstract static class BinaryAsyncAction extends ForkJoinTask<Void> {
200  
201 <        private BinaryAsyncAction parent;
201 >        private volatile BinaryAsyncAction parent;
202  
203 <        private BinaryAsyncAction sibling;
203 >        private volatile BinaryAsyncAction sibling;
204  
205          protected BinaryAsyncAction() {
206              setForkJoinTaskTag(INITIAL_STATE);
# Line 206 | Line 216 | public class ForkJoinTask8Test extends J
216          }
217  
218          protected void onComplete(BinaryAsyncAction x, BinaryAsyncAction y) {
219 <            if (this.getForkJoinTaskTag() != COMPLETE_STATE ||
220 <                x.getForkJoinTaskTag() != COMPLETE_STATE ||
219 >            if (this.getForkJoinTaskTag() != COMPLETE_STATE ||
220 >                x.getForkJoinTaskTag() != COMPLETE_STATE ||
221                  y.getForkJoinTaskTag() != COMPLETE_STATE) {
222                  completeThisExceptionally(new FJException());
223              }
# Line 241 | Line 251 | public class ForkJoinTask8Test extends J
251                  a.sibling = null;
252                  a.parent = null;
253                  a.completeThis();
254 <                if (p == null ||
254 >                if (p == null ||
255                      p.compareAndSetForkJoinTaskTag(INITIAL_STATE, COMPLETE_STATE))
256                      break;
257                  try {
# Line 279 | Line 289 | public class ForkJoinTask8Test extends J
289              super.reinitialize();
290          }
291  
282
292      }
293  
294 <    static final class AsyncFib extends BinaryAsyncAction {
294 >    final class AsyncFib extends BinaryAsyncAction {
295          int number;
296 <        public AsyncFib(int n) {
297 <            this.number = n;
296 >        int expectedResult;
297 >        public AsyncFib(int number) {
298 >            this.number = number;
299 >            this.expectedResult = fib[number];
300          }
301  
302          public final boolean exec() {
# Line 304 | Line 315 | public class ForkJoinTask8Test extends J
315                  }
316                  f.complete();
317              }
318 <            catch(Throwable ex) {
318 >            catch (Throwable ex) {
319                  compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE);
320 +                throw new Error(ex);
321              }
322              return false;
323          }
# Line 314 | Line 326 | public class ForkJoinTask8Test extends J
326              number = ((AsyncFib)x).number + ((AsyncFib)y).number;
327              super.onComplete(x, y);
328          }
329 +
330 +        public void checkCompletedNormally() {
331 +            assertEquals(expectedResult, number);
332 +            ForkJoinTask8Test.this.checkCompletedNormally(this);
333 +        }
334      }
335  
336      static final class FailingAsyncFib extends BinaryAsyncAction {
# Line 350 | Line 367 | public class ForkJoinTask8Test extends J
367       * completed tasks; getRawResult returns null.
368       */
369      public void testInvoke() {
370 +        testInvoke(mainPool());
371 +    }
372 +    public void testInvoke_Singleton() {
373 +        testInvoke(singletonPool());
374 +    }
375 +    public void testInvoke(ForkJoinPool pool) {
376          RecursiveAction a = new CheckedRecursiveAction() {
377              protected void realCompute() {
378                  AsyncFib f = new AsyncFib(8);
379                  assertNull(f.invoke());
380 <                assertEquals(21, f.number);
358 <                checkCompletedNormally(f);
380 >                f.checkCompletedNormally();
381              }};
382 <        testInvokeOnPool(mainPool(), a);
382 >        testInvokeOnPool(pool, a);
383      }
384  
385      /**
# Line 366 | Line 388 | public class ForkJoinTask8Test extends J
388       * completed tasks
389       */
390      public void testQuietlyInvoke() {
391 +        testQuietlyInvoke(mainPool());
392 +    }
393 +    public void testQuietlyInvoke_Singleton() {
394 +        testQuietlyInvoke(singletonPool());
395 +    }
396 +    public void testQuietlyInvoke(ForkJoinPool pool) {
397          RecursiveAction a = new CheckedRecursiveAction() {
398              protected void realCompute() {
399                  AsyncFib f = new AsyncFib(8);
400                  f.quietlyInvoke();
401 <                assertEquals(21, f.number);
374 <                checkCompletedNormally(f);
401 >                f.checkCompletedNormally();
402              }};
403 <        testInvokeOnPool(mainPool(), a);
403 >        testInvokeOnPool(pool, a);
404      }
405  
406      /**
407       * join of a forked task returns when task completes
408       */
409      public void testForkJoin() {
410 +        testForkJoin(mainPool());
411 +    }
412 +    public void testForkJoin_Singleton() {
413 +        testForkJoin(singletonPool());
414 +    }
415 +    public void testForkJoin(ForkJoinPool pool) {
416          RecursiveAction a = new CheckedRecursiveAction() {
417              protected void realCompute() {
418                  AsyncFib f = new AsyncFib(8);
419                  assertSame(f, f.fork());
420                  assertNull(f.join());
421 <                assertEquals(21, f.number);
389 <                checkCompletedNormally(f);
421 >                f.checkCompletedNormally();
422              }};
423 <        testInvokeOnPool(mainPool(), a);
423 >        testInvokeOnPool(pool, a);
424      }
425  
426      /**
427       * get of a forked task returns when task completes
428       */
429      public void testForkGet() {
430 +        testForkGet(mainPool());
431 +    }
432 +    public void testForkGet_Singleton() {
433 +        testForkGet(singletonPool());
434 +    }
435 +    public void testForkGet(ForkJoinPool pool) {
436          RecursiveAction a = new CheckedRecursiveAction() {
437              protected void realCompute() throws Exception {
438                  AsyncFib f = new AsyncFib(8);
439                  assertSame(f, f.fork());
440                  assertNull(f.get());
441 <                assertEquals(21, f.number);
404 <                checkCompletedNormally(f);
441 >                f.checkCompletedNormally();
442              }};
443 <        testInvokeOnPool(mainPool(), a);
443 >        testInvokeOnPool(pool, a);
444      }
445  
446      /**
447       * timed get of a forked task returns when task completes
448       */
449      public void testForkTimedGet() {
450 +        testForkTimedGet(mainPool());
451 +    }
452 +    public void testForkTimedGet_Singleton() {
453 +        testForkTimedGet(singletonPool());
454 +    }
455 +    public void testForkTimedGet(ForkJoinPool pool) {
456          RecursiveAction a = new CheckedRecursiveAction() {
457              protected void realCompute() throws Exception {
458                  AsyncFib f = new AsyncFib(8);
459                  assertSame(f, f.fork());
460                  assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
461 <                assertEquals(21, f.number);
419 <                checkCompletedNormally(f);
461 >                f.checkCompletedNormally();
462              }};
463 <        testInvokeOnPool(mainPool(), a);
463 >        testInvokeOnPool(pool, a);
464      }
465  
466      /**
467 <     * timed get with null time unit throws NPE
467 >     * timed get with null time unit throws NullPointerException
468       */
469 <    public void testForkTimedGetNPE() {
469 >    public void testForkTimedGetNullTimeUnit() {
470 >        testForkTimedGetNullTimeUnit(mainPool());
471 >    }
472 >    public void testForkTimedGetNullTimeUnit_Singleton() {
473 >        testForkTimedGet(singletonPool());
474 >    }
475 >    public void testForkTimedGetNullTimeUnit(ForkJoinPool pool) {
476          RecursiveAction a = new CheckedRecursiveAction() {
477              protected void realCompute() throws Exception {
478                  AsyncFib f = new AsyncFib(8);
# Line 434 | Line 482 | public class ForkJoinTask8Test extends J
482                      shouldThrow();
483                  } catch (NullPointerException success) {}
484              }};
485 <        testInvokeOnPool(mainPool(), a);
485 >        testInvokeOnPool(pool, a);
486      }
487  
488      /**
489       * quietlyJoin of a forked task returns when task completes
490       */
491      public void testForkQuietlyJoin() {
492 +        testForkQuietlyJoin(mainPool());
493 +    }
494 +    public void testForkQuietlyJoin_Singleton() {
495 +        testForkQuietlyJoin(singletonPool());
496 +    }
497 +    public void testForkQuietlyJoin(ForkJoinPool pool) {
498          RecursiveAction a = new CheckedRecursiveAction() {
499              protected void realCompute() {
500                  AsyncFib f = new AsyncFib(8);
501                  assertSame(f, f.fork());
502                  f.quietlyJoin();
503 <                assertEquals(21, f.number);
450 <                checkCompletedNormally(f);
503 >                f.checkCompletedNormally();
504              }};
505 <        testInvokeOnPool(mainPool(), a);
505 >        testInvokeOnPool(pool, a);
506      }
507  
508      /**
# Line 457 | Line 510 | public class ForkJoinTask8Test extends J
510       * getQueuedTaskCount returns 0 when quiescent
511       */
512      public void testForkHelpQuiesce() {
513 +        testForkHelpQuiesce(mainPool());
514 +    }
515 +    public void testForkHelpQuiesce_Singleton() {
516 +        testForkHelpQuiesce(singletonPool());
517 +    }
518 +    public void testForkHelpQuiesce(ForkJoinPool pool) {
519          RecursiveAction a = new CheckedRecursiveAction() {
520              protected void realCompute() {
521                  AsyncFib f = new AsyncFib(8);
522                  assertSame(f, f.fork());
523                  helpQuiesce();
465                assertEquals(21, f.number);
524                  assertEquals(0, getQueuedTaskCount());
525 <                checkCompletedNormally(f);
525 >                f.checkCompletedNormally();
526              }};
527 <        testInvokeOnPool(mainPool(), a);
527 >        testInvokeOnPool(pool, a);
528      }
529  
530      /**
531       * invoke task throws exception when task completes abnormally
532       */
533      public void testAbnormalInvoke() {
534 +        testAbnormalInvoke(mainPool());
535 +    }
536 +    public void testAbnormalInvoke_Singleton() {
537 +        testAbnormalInvoke(singletonPool());
538 +    }
539 +    public void testAbnormalInvoke(ForkJoinPool pool) {
540          RecursiveAction a = new CheckedRecursiveAction() {
541              protected void realCompute() {
542                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 483 | Line 547 | public class ForkJoinTask8Test extends J
547                      checkCompletedAbnormally(f, success);
548                  }
549              }};
550 <        testInvokeOnPool(mainPool(), a);
550 >        testInvokeOnPool(pool, a);
551      }
552  
553      /**
554       * quietlyInvoke task returns when task completes abnormally
555       */
556      public void testAbnormalQuietlyInvoke() {
557 +        testAbnormalQuietlyInvoke(mainPool());
558 +    }
559 +    public void testAbnormalQuietlyInvoke_Singleton() {
560 +        testAbnormalQuietlyInvoke(singletonPool());
561 +    }
562 +    public void testAbnormalQuietlyInvoke(ForkJoinPool pool) {
563          RecursiveAction a = new CheckedRecursiveAction() {
564              protected void realCompute() {
565                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 497 | Line 567 | public class ForkJoinTask8Test extends J
567                  assertTrue(f.getException() instanceof FJException);
568                  checkCompletedAbnormally(f, f.getException());
569              }};
570 <        testInvokeOnPool(mainPool(), a);
570 >        testInvokeOnPool(pool, a);
571      }
572  
573      /**
574       * join of a forked task throws exception when task completes abnormally
575       */
576      public void testAbnormalForkJoin() {
577 +        testAbnormalForkJoin(mainPool());
578 +    }
579 +    public void testAbnormalForkJoin_Singleton() {
580 +        testAbnormalForkJoin(singletonPool());
581 +    }
582 +    public void testAbnormalForkJoin(ForkJoinPool pool) {
583          RecursiveAction a = new CheckedRecursiveAction() {
584              protected void realCompute() {
585                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 515 | Line 591 | public class ForkJoinTask8Test extends J
591                      checkCompletedAbnormally(f, success);
592                  }
593              }};
594 <        testInvokeOnPool(mainPool(), a);
594 >        testInvokeOnPool(pool, a);
595      }
596  
597      /**
598       * get of a forked task throws exception when task completes abnormally
599       */
600      public void testAbnormalForkGet() {
601 +        testAbnormalForkGet(mainPool());
602 +    }
603 +    public void testAbnormalForkGet_Singleton() {
604 +        testAbnormalForkJoin(singletonPool());
605 +    }
606 +    public void testAbnormalForkGet(ForkJoinPool pool) {
607          RecursiveAction a = new CheckedRecursiveAction() {
608              protected void realCompute() throws Exception {
609                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 535 | Line 617 | public class ForkJoinTask8Test extends J
617                      checkCompletedAbnormally(f, cause);
618                  }
619              }};
620 <        testInvokeOnPool(mainPool(), a);
620 >        testInvokeOnPool(pool, a);
621      }
622  
623      /**
624       * timed get of a forked task throws exception when task completes abnormally
625       */
626      public void testAbnormalForkTimedGet() {
627 +        testAbnormalForkTimedGet(mainPool());
628 +    }
629 +    public void testAbnormalForkTimedGet_Singleton() {
630 +        testAbnormalForkTimedGet(singletonPool());
631 +    }
632 +    public void testAbnormalForkTimedGet(ForkJoinPool pool) {
633          RecursiveAction a = new CheckedRecursiveAction() {
634              protected void realCompute() throws Exception {
635                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 555 | Line 643 | public class ForkJoinTask8Test extends J
643                      checkCompletedAbnormally(f, cause);
644                  }
645              }};
646 <        testInvokeOnPool(mainPool(), a);
646 >        testInvokeOnPool(pool, a);
647      }
648  
649      /**
650       * quietlyJoin of a forked task returns when task completes abnormally
651       */
652      public void testAbnormalForkQuietlyJoin() {
653 +        testAbnormalForkQuietlyJoin(mainPool());
654 +    }
655 +    public void testAbnormalForkQuietlyJoin_Singleton() {
656 +        testAbnormalForkQuietlyJoin(singletonPool());
657 +    }
658 +    public void testAbnormalForkQuietlyJoin(ForkJoinPool pool) {
659          RecursiveAction a = new CheckedRecursiveAction() {
660              protected void realCompute() {
661                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 570 | Line 664 | public class ForkJoinTask8Test extends J
664                  assertTrue(f.getException() instanceof FJException);
665                  checkCompletedAbnormally(f, f.getException());
666              }};
667 <        testInvokeOnPool(mainPool(), a);
667 >        testInvokeOnPool(pool, a);
668      }
669  
576
670      /**
671       * getPool of executing task returns its pool
672       */
673      public void testGetPool() {
674 <        final ForkJoinPool mainPool = mainPool();
674 >        testGetPool(mainPool());
675 >    }
676 >    public void testGetPool_Singleton() {
677 >        testGetPool(singletonPool());
678 >    }
679 >    public void testGetPool(ForkJoinPool pool) {
680          RecursiveAction a = new CheckedRecursiveAction() {
681              protected void realCompute() {
682 <                assertSame(mainPool, getPool());
682 >                assertSame(pool, getPool());
683              }};
684 <        testInvokeOnPool(mainPool, a);
684 >        testInvokeOnPool(pool, a);
685      }
686  
687      /**
# Line 601 | Line 699 | public class ForkJoinTask8Test extends J
699       * inForkJoinPool of executing task returns true
700       */
701      public void testInForkJoinPool() {
702 +        testInForkJoinPool(mainPool());
703 +    }
704 +    public void testInForkJoinPool_Singleton() {
705 +        testInForkJoinPool(singletonPool());
706 +    }
707 +    public void testInForkJoinPool(ForkJoinPool pool) {
708          RecursiveAction a = new CheckedRecursiveAction() {
709              protected void realCompute() {
710                  assertTrue(inForkJoinPool());
711              }};
712 <        testInvokeOnPool(mainPool(), a);
712 >        testInvokeOnPool(pool, a);
713      }
714  
715      /**
# Line 635 | Line 739 | public class ForkJoinTask8Test extends J
739       * invoke task throws exception after invoking completeExceptionally
740       */
741      public void testCompleteExceptionally() {
742 +        testCompleteExceptionally(mainPool());
743 +    }
744 +    public void testCompleteExceptionally_Singleton() {
745 +        testCompleteExceptionally(singletonPool());
746 +    }
747 +    public void testCompleteExceptionally(ForkJoinPool pool) {
748          RecursiveAction a = new CheckedRecursiveAction() {
749              protected void realCompute() {
750                  AsyncFib f = new AsyncFib(8);
# Line 646 | Line 756 | public class ForkJoinTask8Test extends J
756                      checkCompletedAbnormally(f, success);
757                  }
758              }};
759 <        testInvokeOnPool(mainPool(), a);
759 >        testInvokeOnPool(pool, a);
760      }
761  
762      /**
763 <     * invokeAll(t1, t2) invokes all task arguments
763 >     * invokeAll(tasks) with 1 argument invokes task
764       */
765 <    public void testInvokeAll2() {
765 >    public void testInvokeAll1() {
766 >        testInvokeAll1(mainPool());
767 >    }
768 >    public void testInvokeAll1_Singleton() {
769 >        testInvokeAll1(singletonPool());
770 >    }
771 >    public void testInvokeAll1(ForkJoinPool pool) {
772          RecursiveAction a = new CheckedRecursiveAction() {
773              protected void realCompute() {
774                  AsyncFib f = new AsyncFib(8);
775 <                AsyncFib g = new AsyncFib(9);
776 <                invokeAll(f, g);
661 <                assertEquals(21, f.number);
662 <                assertEquals(34, g.number);
663 <                checkCompletedNormally(f);
664 <                checkCompletedNormally(g);
775 >                invokeAll(f);
776 >                f.checkCompletedNormally();
777              }};
778 <        testInvokeOnPool(mainPool(), a);
778 >        testInvokeOnPool(pool, a);
779      }
780  
781      /**
782 <     * invokeAll(tasks) with 1 argument invokes task
782 >     * invokeAll(t1, t2) invokes all task arguments
783       */
784 <    public void testInvokeAll1() {
784 >    public void testInvokeAll2() {
785 >        testInvokeAll2(mainPool());
786 >    }
787 >    public void testInvokeAll2_Singleton() {
788 >        testInvokeAll2(singletonPool());
789 >    }
790 >    public void testInvokeAll2(ForkJoinPool pool) {
791          RecursiveAction a = new CheckedRecursiveAction() {
792              protected void realCompute() {
793 <                AsyncFib f = new AsyncFib(8);
794 <                invokeAll(f);
795 <                checkCompletedNormally(f);
796 <                assertEquals(21, f.number);
793 >                AsyncFib[] tasks = {
794 >                    new AsyncFib(8),
795 >                    new AsyncFib(9),
796 >                };
797 >                invokeAll(tasks[0], tasks[1]);
798 >                for (AsyncFib task : tasks) assertTrue(task.isDone());
799 >                for (AsyncFib task : tasks) task.checkCompletedNormally();
800              }};
801 <        testInvokeOnPool(mainPool(), a);
801 >        testInvokeOnPool(pool, a);
802      }
803  
804      /**
805       * invokeAll(tasks) with > 2 argument invokes tasks
806       */
807      public void testInvokeAll3() {
808 +        testInvokeAll3(mainPool());
809 +    }
810 +    public void testInvokeAll3_Singleton() {
811 +        testInvokeAll3(singletonPool());
812 +    }
813 +    public void testInvokeAll3(ForkJoinPool pool) {
814          RecursiveAction a = new CheckedRecursiveAction() {
815              protected void realCompute() {
816 <                AsyncFib f = new AsyncFib(8);
817 <                AsyncFib g = new AsyncFib(9);
818 <                AsyncFib h = new AsyncFib(7);
819 <                invokeAll(f, g, h);
820 <                assertEquals(21, f.number);
821 <                assertEquals(34, g.number);
822 <                assertEquals(13, h.number);
823 <                checkCompletedNormally(f);
697 <                checkCompletedNormally(g);
698 <                checkCompletedNormally(h);
816 >                AsyncFib[] tasks = {
817 >                    new AsyncFib(8),
818 >                    new AsyncFib(9),
819 >                    new AsyncFib(7),
820 >                };
821 >                invokeAll(tasks[0], tasks[1], tasks[2]);
822 >                for (AsyncFib task : tasks) assertTrue(task.isDone());
823 >                for (AsyncFib task : tasks) task.checkCompletedNormally();
824              }};
825 <        testInvokeOnPool(mainPool(), a);
825 >        testInvokeOnPool(pool, a);
826      }
827  
828      /**
829       * invokeAll(collection) invokes all tasks in the collection
830       */
831      public void testInvokeAllCollection() {
832 +        testInvokeAllCollection(mainPool());
833 +    }
834 +    public void testInvokeAllCollection_Singleton() {
835 +        testInvokeAllCollection(singletonPool());
836 +    }
837 +    public void testInvokeAllCollection(ForkJoinPool pool) {
838          RecursiveAction a = new CheckedRecursiveAction() {
839              protected void realCompute() {
840 <                AsyncFib f = new AsyncFib(8);
841 <                AsyncFib g = new AsyncFib(9);
842 <                AsyncFib h = new AsyncFib(7);
843 <                HashSet set = new HashSet();
844 <                set.add(f);
845 <                set.add(g);
846 <                set.add(h);
847 <                invokeAll(set);
717 <                assertEquals(21, f.number);
718 <                assertEquals(34, g.number);
719 <                assertEquals(13, h.number);
720 <                checkCompletedNormally(f);
721 <                checkCompletedNormally(g);
722 <                checkCompletedNormally(h);
840 >                AsyncFib[] tasks = {
841 >                    new AsyncFib(8),
842 >                    new AsyncFib(9),
843 >                    new AsyncFib(7),
844 >                };
845 >                invokeAll(Arrays.asList(tasks));
846 >                for (AsyncFib task : tasks) assertTrue(task.isDone());
847 >                for (AsyncFib task : tasks) task.checkCompletedNormally();
848              }};
849 <        testInvokeOnPool(mainPool(), a);
849 >        testInvokeOnPool(pool, a);
850      }
851  
852      /**
853 <     * invokeAll(tasks) with any null task throws NPE
853 >     * invokeAll(tasks) with any null task throws NullPointerException
854       */
855 <    public void testInvokeAllNPE() {
855 >    public void testInvokeAllNullTask() {
856 >        testInvokeAllNullTask(mainPool());
857 >    }
858 >    public void testInvokeAllNullTask_Singleton() {
859 >        testInvokeAllNullTask(singletonPool());
860 >    }
861 >    public void testInvokeAllNullTask(ForkJoinPool pool) {
862          RecursiveAction a = new CheckedRecursiveAction() {
863              protected void realCompute() {
864 <                AsyncFib f = new AsyncFib(8);
865 <                AsyncFib g = new AsyncFib(9);
866 <                AsyncFib h = null;
867 <                try {
868 <                    invokeAll(f, g, h);
869 <                    shouldThrow();
870 <                } catch (NullPointerException success) {}
864 >                AsyncFib nul = null;
865 >                Runnable[] throwingActions = {
866 >                    () -> invokeAll(nul),
867 >                    () -> invokeAll(nul, nul),
868 >                    () -> invokeAll(new AsyncFib(8), new AsyncFib(9), nul),
869 >                    () -> invokeAll(new AsyncFib(8), nul, new AsyncFib(9)),
870 >                    () -> invokeAll(nul, new AsyncFib(8), new AsyncFib(9)),
871 >                };
872 >                assertThrows(NullPointerException.class, throwingActions);
873              }};
874 <        testInvokeOnPool(mainPool(), a);
874 >        testInvokeOnPool(pool, a);
875      }
876  
877      /**
878 <     * invokeAll(t1, t2) throw exception if any task does
878 >     * invokeAll(tasks) with 1 argument throws exception if task does
879       */
880 <    public void testAbnormalInvokeAll2() {
880 >    public void testAbnormalInvokeAll1() {
881 >        testAbnormalInvokeAll1(mainPool());
882 >    }
883 >    public void testAbnormalInvokeAll1_Singleton() {
884 >        testAbnormalInvokeAll1(singletonPool());
885 >    }
886 >    public void testAbnormalInvokeAll1(ForkJoinPool pool) {
887          RecursiveAction a = new CheckedRecursiveAction() {
888              protected void realCompute() {
750                AsyncFib f = new AsyncFib(8);
889                  FailingAsyncFib g = new FailingAsyncFib(9);
890                  try {
891 <                    invokeAll(f, g);
891 >                    invokeAll(g);
892                      shouldThrow();
893                  } catch (FJException success) {
894                      checkCompletedAbnormally(g, success);
895                  }
896              }};
897 <        testInvokeOnPool(mainPool(), a);
897 >        testInvokeOnPool(pool, a);
898      }
899  
900      /**
901 <     * invokeAll(tasks) with 1 argument throws exception if task does
901 >     * invokeAll(t1, t2) throw exception if any task does
902       */
903 <    public void testAbnormalInvokeAll1() {
903 >    public void testAbnormalInvokeAll2() {
904 >        testAbnormalInvokeAll2(mainPool());
905 >    }
906 >    public void testAbnormalInvokeAll2_Singleton() {
907 >        testAbnormalInvokeAll2(singletonPool());
908 >    }
909 >    public void testAbnormalInvokeAll2(ForkJoinPool pool) {
910          RecursiveAction a = new CheckedRecursiveAction() {
911              protected void realCompute() {
912 +                AsyncFib f = new AsyncFib(8);
913                  FailingAsyncFib g = new FailingAsyncFib(9);
914 +                ForkJoinTask[] tasks = { f, g };
915 +                Collections.shuffle(Arrays.asList(tasks));
916                  try {
917 <                    invokeAll(g);
917 >                    invokeAll(tasks[0], tasks[1]);
918                      shouldThrow();
919                  } catch (FJException success) {
920                      checkCompletedAbnormally(g, success);
921                  }
922              }};
923 <        testInvokeOnPool(mainPool(), a);
923 >        testInvokeOnPool(pool, a);
924      }
925  
926      /**
927       * invokeAll(tasks) with > 2 argument throws exception if any task does
928       */
929      public void testAbnormalInvokeAll3() {
930 +        testAbnormalInvokeAll3(mainPool());
931 +    }
932 +    public void testAbnormalInvokeAll3_Singleton() {
933 +        testAbnormalInvokeAll3(singletonPool());
934 +    }
935 +    public void testAbnormalInvokeAll3(ForkJoinPool pool) {
936          RecursiveAction a = new CheckedRecursiveAction() {
937              protected void realCompute() {
938                  AsyncFib f = new AsyncFib(8);
939                  FailingAsyncFib g = new FailingAsyncFib(9);
940                  AsyncFib h = new AsyncFib(7);
941 +                ForkJoinTask[] tasks = { f, g, h };
942 +                Collections.shuffle(Arrays.asList(tasks));
943                  try {
944 <                    invokeAll(f, g, h);
944 >                    invokeAll(tasks[0], tasks[1], tasks[2]);
945                      shouldThrow();
946                  } catch (FJException success) {
947                      checkCompletedAbnormally(g, success);
948                  }
949              }};
950 <        testInvokeOnPool(mainPool(), a);
950 >        testInvokeOnPool(pool, a);
951      }
952  
953      /**
954 <     * invokeAll(collection)  throws exception if any task does
954 >     * invokeAll(collection) throws exception if any task does
955       */
956      public void testAbnormalInvokeAllCollection() {
957 +        testAbnormalInvokeAllCollection(mainPool());
958 +    }
959 +    public void testAbnormalInvokeAllCollection_Singleton() {
960 +        testAbnormalInvokeAllCollection(singletonPool());
961 +    }
962 +    public void testAbnormalInvokeAllCollection(ForkJoinPool pool) {
963          RecursiveAction a = new CheckedRecursiveAction() {
964              protected void realCompute() {
965                  FailingAsyncFib f = new FailingAsyncFib(8);
966                  AsyncFib g = new AsyncFib(9);
967                  AsyncFib h = new AsyncFib(7);
968 <                HashSet set = new HashSet();
969 <                set.add(f);
809 <                set.add(g);
810 <                set.add(h);
968 >                ForkJoinTask[] tasks = { f, g, h };
969 >                Collections.shuffle(Arrays.asList(tasks));
970                  try {
971 <                    invokeAll(set);
971 >                    invokeAll(Arrays.asList(tasks));
972                      shouldThrow();
973                  } catch (FJException success) {
974                      checkCompletedAbnormally(f, success);
975                  }
976              }};
977 <        testInvokeOnPool(mainPool(), a);
977 >        testInvokeOnPool(pool, a);
978      }
979  
980      /**
# Line 832 | Line 991 | public class ForkJoinTask8Test extends J
991                  assertTrue(f.tryUnfork());
992                  helpQuiesce();
993                  checkNotDone(f);
994 <                checkCompletedNormally(g);
994 >                g.checkCompletedNormally();
995              }};
996          testInvokeOnPool(singletonPool(), a);
997      }
# Line 853 | Line 1012 | public class ForkJoinTask8Test extends J
1012                  assertTrue(getSurplusQueuedTaskCount() > 0);
1013                  helpQuiesce();
1014                  assertEquals(0, getSurplusQueuedTaskCount());
1015 <                checkCompletedNormally(f);
1016 <                checkCompletedNormally(g);
1017 <                checkCompletedNormally(h);
1015 >                f.checkCompletedNormally();
1016 >                g.checkCompletedNormally();
1017 >                h.checkCompletedNormally();
1018              }};
1019          testInvokeOnPool(singletonPool(), a);
1020      }
# Line 872 | Line 1031 | public class ForkJoinTask8Test extends J
1031                  assertSame(f, f.fork());
1032                  assertSame(f, peekNextLocalTask());
1033                  assertNull(f.join());
1034 <                checkCompletedNormally(f);
1034 >                f.checkCompletedNormally();
1035                  helpQuiesce();
1036 <                checkCompletedNormally(g);
1036 >                g.checkCompletedNormally();
1037              }};
1038          testInvokeOnPool(singletonPool(), a);
1039      }
# Line 893 | Line 1052 | public class ForkJoinTask8Test extends J
1052                  assertSame(f, pollNextLocalTask());
1053                  helpQuiesce();
1054                  checkNotDone(f);
1055 <                assertEquals(34, g.number);
897 <                checkCompletedNormally(g);
1055 >                g.checkCompletedNormally();
1056              }};
1057          testInvokeOnPool(singletonPool(), a);
1058      }
# Line 912 | Line 1070 | public class ForkJoinTask8Test extends J
1070                  assertSame(f, pollTask());
1071                  helpQuiesce();
1072                  checkNotDone(f);
1073 <                checkCompletedNormally(g);
1073 >                g.checkCompletedNormally();
1074              }};
1075          testInvokeOnPool(singletonPool(), a);
1076      }
# Line 930 | Line 1088 | public class ForkJoinTask8Test extends J
1088                  assertSame(g, peekNextLocalTask());
1089                  assertNull(f.join());
1090                  helpQuiesce();
1091 <                checkCompletedNormally(f);
1092 <                assertEquals(34, g.number);
935 <                checkCompletedNormally(g);
1091 >                f.checkCompletedNormally();
1092 >                g.checkCompletedNormally();
1093              }};
1094          testInvokeOnPool(asyncSingletonPool(), a);
1095      }
# Line 950 | Line 1107 | public class ForkJoinTask8Test extends J
1107                  assertSame(f, f.fork());
1108                  assertSame(g, pollNextLocalTask());
1109                  helpQuiesce();
1110 <                assertEquals(21, f.number);
954 <                checkCompletedNormally(f);
1110 >                f.checkCompletedNormally();
1111                  checkNotDone(g);
1112              }};
1113          testInvokeOnPool(asyncSingletonPool(), a);
# Line 970 | Line 1126 | public class ForkJoinTask8Test extends J
1126                  assertSame(f, f.fork());
1127                  assertSame(g, pollTask());
1128                  helpQuiesce();
1129 <                assertEquals(21, f.number);
974 <                checkCompletedNormally(f);
1129 >                f.checkCompletedNormally();
1130                  checkNotDone(g);
1131              }};
1132          testInvokeOnPool(asyncSingletonPool(), a);
1133      }
1134  
980    // versions for singleton pools
981
982    /**
983     * invoke returns when task completes normally.
984     * isCompletedAbnormally and isCancelled return false for normally
985     * completed tasks; getRawResult returns null.
986     */
987    public void testInvokeSingleton() {
988        RecursiveAction a = new CheckedRecursiveAction() {
989            protected void realCompute() {
990                AsyncFib f = new AsyncFib(8);
991                assertNull(f.invoke());
992                assertEquals(21, f.number);
993                checkCompletedNormally(f);
994            }};
995        testInvokeOnPool(singletonPool(), a);
996    }
997
998    /**
999     * quietlyInvoke task returns when task completes normally.
1000     * isCompletedAbnormally and isCancelled return false for normally
1001     * completed tasks
1002     */
1003    public void testQuietlyInvokeSingleton() {
1004        RecursiveAction a = new CheckedRecursiveAction() {
1005            protected void realCompute() {
1006                AsyncFib f = new AsyncFib(8);
1007                f.quietlyInvoke();
1008                assertEquals(21, f.number);
1009                checkCompletedNormally(f);
1010            }};
1011        testInvokeOnPool(singletonPool(), a);
1012    }
1013
1014    /**
1015     * join of a forked task returns when task completes
1016     */
1017    public void testForkJoinSingleton() {
1018        RecursiveAction a = new CheckedRecursiveAction() {
1019            protected void realCompute() {
1020                AsyncFib f = new AsyncFib(8);
1021                assertSame(f, f.fork());
1022                assertNull(f.join());
1023                assertEquals(21, f.number);
1024                checkCompletedNormally(f);
1025            }};
1026        testInvokeOnPool(singletonPool(), a);
1027    }
1028
1029    /**
1030     * get of a forked task returns when task completes
1031     */
1032    public void testForkGetSingleton() {
1033        RecursiveAction a = new CheckedRecursiveAction() {
1034            protected void realCompute() throws Exception {
1035                AsyncFib f = new AsyncFib(8);
1036                assertSame(f, f.fork());
1037                assertNull(f.get());
1038                assertEquals(21, f.number);
1039                checkCompletedNormally(f);
1040            }};
1041        testInvokeOnPool(singletonPool(), a);
1042    }
1043
1044    /**
1045     * timed get of a forked task returns when task completes
1046     */
1047    public void testForkTimedGetSingleton() {
1048        RecursiveAction a = new CheckedRecursiveAction() {
1049            protected void realCompute() throws Exception {
1050                AsyncFib f = new AsyncFib(8);
1051                assertSame(f, f.fork());
1052                assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
1053                assertEquals(21, f.number);
1054                checkCompletedNormally(f);
1055            }};
1056        testInvokeOnPool(singletonPool(), a);
1057    }
1058
1059    /**
1060     * timed get with null time unit throws NPE
1061     */
1062    public void testForkTimedGetNPESingleton() {
1063        RecursiveAction a = new CheckedRecursiveAction() {
1064            protected void realCompute() throws Exception {
1065                AsyncFib f = new AsyncFib(8);
1066                assertSame(f, f.fork());
1067                try {
1068                    f.get(5L, null);
1069                    shouldThrow();
1070                } catch (NullPointerException success) {}
1071            }};
1072        testInvokeOnPool(singletonPool(), a);
1073    }
1074
1075    /**
1076     * quietlyJoin of a forked task returns when task completes
1077     */
1078    public void testForkQuietlyJoinSingleton() {
1079        RecursiveAction a = new CheckedRecursiveAction() {
1080            protected void realCompute() {
1081                AsyncFib f = new AsyncFib(8);
1082                assertSame(f, f.fork());
1083                f.quietlyJoin();
1084                assertEquals(21, f.number);
1085                checkCompletedNormally(f);
1086            }};
1087        testInvokeOnPool(singletonPool(), a);
1088    }
1089
1090    /**
1091     * helpQuiesce returns when tasks are complete.
1092     * getQueuedTaskCount returns 0 when quiescent
1093     */
1094    public void testForkHelpQuiesceSingleton() {
1095        RecursiveAction a = new CheckedRecursiveAction() {
1096            protected void realCompute() {
1097                AsyncFib f = new AsyncFib(8);
1098                assertSame(f, f.fork());
1099                helpQuiesce();
1100                assertEquals(0, getQueuedTaskCount());
1101                assertEquals(21, f.number);
1102                checkCompletedNormally(f);
1103            }};
1104        testInvokeOnPool(singletonPool(), a);
1105    }
1106
1107    /**
1108     * invoke task throws exception when task completes abnormally
1109     */
1110    public void testAbnormalInvokeSingleton() {
1111        RecursiveAction a = new CheckedRecursiveAction() {
1112            protected void realCompute() {
1113                FailingAsyncFib f = new FailingAsyncFib(8);
1114                try {
1115                    f.invoke();
1116                    shouldThrow();
1117                } catch (FJException success) {
1118                    checkCompletedAbnormally(f, success);
1119                }
1120            }};
1121        testInvokeOnPool(singletonPool(), a);
1122    }
1123
1124    /**
1125     * quietlyInvoke task returns when task completes abnormally
1126     */
1127    public void testAbnormalQuietlyInvokeSingleton() {
1128        RecursiveAction a = new CheckedRecursiveAction() {
1129            protected void realCompute() {
1130                FailingAsyncFib f = new FailingAsyncFib(8);
1131                f.quietlyInvoke();
1132                assertTrue(f.getException() instanceof FJException);
1133                checkCompletedAbnormally(f, f.getException());
1134            }};
1135        testInvokeOnPool(singletonPool(), a);
1136    }
1137
1138    /**
1139     * join of a forked task throws exception when task completes abnormally
1140     */
1141    public void testAbnormalForkJoinSingleton() {
1142        RecursiveAction a = new CheckedRecursiveAction() {
1143            protected void realCompute() {
1144                FailingAsyncFib f = new FailingAsyncFib(8);
1145                assertSame(f, f.fork());
1146                try {
1147                    f.join();
1148                    shouldThrow();
1149                } catch (FJException success) {
1150                    checkCompletedAbnormally(f, success);
1151                }
1152            }};
1153        testInvokeOnPool(singletonPool(), a);
1154    }
1155
1156    /**
1157     * get of a forked task throws exception when task completes abnormally
1158     */
1159    public void testAbnormalForkGetSingleton() {
1160        RecursiveAction a = new CheckedRecursiveAction() {
1161            protected void realCompute() throws Exception {
1162                FailingAsyncFib f = new FailingAsyncFib(8);
1163                assertSame(f, f.fork());
1164                try {
1165                    f.get();
1166                    shouldThrow();
1167                } catch (ExecutionException success) {
1168                    Throwable cause = success.getCause();
1169                    assertTrue(cause instanceof FJException);
1170                    checkCompletedAbnormally(f, cause);
1171                }
1172            }};
1173        testInvokeOnPool(singletonPool(), a);
1174    }
1175
1176    /**
1177     * timed get of a forked task throws exception when task completes abnormally
1178     */
1179    public void testAbnormalForkTimedGetSingleton() {
1180        RecursiveAction a = new CheckedRecursiveAction() {
1181            protected void realCompute() throws Exception {
1182                FailingAsyncFib f = new FailingAsyncFib(8);
1183                assertSame(f, f.fork());
1184                try {
1185                    f.get(LONG_DELAY_MS, MILLISECONDS);
1186                    shouldThrow();
1187                } catch (ExecutionException success) {
1188                    Throwable cause = success.getCause();
1189                    assertTrue(cause instanceof FJException);
1190                    checkCompletedAbnormally(f, cause);
1191                }
1192            }};
1193        testInvokeOnPool(singletonPool(), a);
1194    }
1195
1196    /**
1197     * quietlyJoin of a forked task returns when task completes abnormally
1198     */
1199    public void testAbnormalForkQuietlyJoinSingleton() {
1200        RecursiveAction a = new CheckedRecursiveAction() {
1201            protected void realCompute() {
1202                FailingAsyncFib f = new FailingAsyncFib(8);
1203                assertSame(f, f.fork());
1204                f.quietlyJoin();
1205                assertTrue(f.getException() instanceof FJException);
1206                checkCompletedAbnormally(f, f.getException());
1207            }};
1208        testInvokeOnPool(singletonPool(), a);
1209    }
1210
1211    /**
1212     * invoke task throws exception after invoking completeExceptionally
1213     */
1214    public void testCompleteExceptionallySingleton() {
1215        RecursiveAction a = new CheckedRecursiveAction() {
1216            protected void realCompute() {
1217                AsyncFib f = new AsyncFib(8);
1218                f.completeExceptionally(new FJException());
1219                try {
1220                    f.invoke();
1221                    shouldThrow();
1222                } catch (FJException success) {
1223                    checkCompletedAbnormally(f, success);
1224                }
1225            }};
1226        testInvokeOnPool(singletonPool(), a);
1227    }
1228
1229    /**
1230     * invokeAll(t1, t2) invokes all task arguments
1231     */
1232    public void testInvokeAll2Singleton() {
1233        RecursiveAction a = new CheckedRecursiveAction() {
1234            protected void realCompute() {
1235                AsyncFib f = new AsyncFib(8);
1236                AsyncFib g = new AsyncFib(9);
1237                invokeAll(f, g);
1238                assertEquals(21, f.number);
1239                assertEquals(34, g.number);
1240                checkCompletedNormally(f);
1241                checkCompletedNormally(g);
1242            }};
1243        testInvokeOnPool(singletonPool(), a);
1244    }
1245
1246    /**
1247     * invokeAll(tasks) with 1 argument invokes task
1248     */
1249    public void testInvokeAll1Singleton() {
1250        RecursiveAction a = new CheckedRecursiveAction() {
1251            protected void realCompute() {
1252                AsyncFib f = new AsyncFib(8);
1253                invokeAll(f);
1254                checkCompletedNormally(f);
1255                assertEquals(21, f.number);
1256            }};
1257        testInvokeOnPool(singletonPool(), a);
1258    }
1259
1260    /**
1261     * invokeAll(tasks) with > 2 argument invokes tasks
1262     */
1263    public void testInvokeAll3Singleton() {
1264        RecursiveAction a = new CheckedRecursiveAction() {
1265            protected void realCompute() {
1266                AsyncFib f = new AsyncFib(8);
1267                AsyncFib g = new AsyncFib(9);
1268                AsyncFib h = new AsyncFib(7);
1269                invokeAll(f, g, h);
1270                assertEquals(21, f.number);
1271                assertEquals(34, g.number);
1272                assertEquals(13, h.number);
1273                checkCompletedNormally(f);
1274                checkCompletedNormally(g);
1275                checkCompletedNormally(h);
1276            }};
1277        testInvokeOnPool(singletonPool(), a);
1278    }
1279
1280    /**
1281     * invokeAll(collection) invokes all tasks in the collection
1282     */
1283    public void testInvokeAllCollectionSingleton() {
1284        RecursiveAction a = new CheckedRecursiveAction() {
1285            protected void realCompute() {
1286                AsyncFib f = new AsyncFib(8);
1287                AsyncFib g = new AsyncFib(9);
1288                AsyncFib h = new AsyncFib(7);
1289                HashSet set = new HashSet();
1290                set.add(f);
1291                set.add(g);
1292                set.add(h);
1293                invokeAll(set);
1294                assertEquals(21, f.number);
1295                assertEquals(34, g.number);
1296                assertEquals(13, h.number);
1297                checkCompletedNormally(f);
1298                checkCompletedNormally(g);
1299                checkCompletedNormally(h);
1300            }};
1301        testInvokeOnPool(singletonPool(), a);
1302    }
1303
1304    /**
1305     * invokeAll(tasks) with any null task throws NPE
1306     */
1307    public void testInvokeAllNPESingleton() {
1308        RecursiveAction a = new CheckedRecursiveAction() {
1309            protected void realCompute() {
1310                AsyncFib f = new AsyncFib(8);
1311                AsyncFib g = new AsyncFib(9);
1312                AsyncFib h = null;
1313                try {
1314                    invokeAll(f, g, h);
1315                    shouldThrow();
1316                } catch (NullPointerException success) {}
1317            }};
1318        testInvokeOnPool(singletonPool(), a);
1319    }
1320
1321    /**
1322     * invokeAll(t1, t2) throw exception if any task does
1323     */
1324    public void testAbnormalInvokeAll2Singleton() {
1325        RecursiveAction a = new CheckedRecursiveAction() {
1326            protected void realCompute() {
1327                AsyncFib f = new AsyncFib(8);
1328                FailingAsyncFib g = new FailingAsyncFib(9);
1329                try {
1330                    invokeAll(f, g);
1331                    shouldThrow();
1332                } catch (FJException success) {
1333                    checkCompletedAbnormally(g, success);
1334                }
1335            }};
1336        testInvokeOnPool(singletonPool(), a);
1337    }
1338
1339    /**
1340     * invokeAll(tasks) with 1 argument throws exception if task does
1341     */
1342    public void testAbnormalInvokeAll1Singleton() {
1343        RecursiveAction a = new CheckedRecursiveAction() {
1344            protected void realCompute() {
1345                FailingAsyncFib g = new FailingAsyncFib(9);
1346                try {
1347                    invokeAll(g);
1348                    shouldThrow();
1349                } catch (FJException success) {
1350                    checkCompletedAbnormally(g, success);
1351                }
1352            }};
1353        testInvokeOnPool(singletonPool(), a);
1354    }
1355
1356    /**
1357     * invokeAll(tasks) with > 2 argument throws exception if any task does
1358     */
1359    public void testAbnormalInvokeAll3Singleton() {
1360        RecursiveAction a = new CheckedRecursiveAction() {
1361            protected void realCompute() {
1362                AsyncFib f = new AsyncFib(8);
1363                FailingAsyncFib g = new FailingAsyncFib(9);
1364                AsyncFib h = new AsyncFib(7);
1365                try {
1366                    invokeAll(f, g, h);
1367                    shouldThrow();
1368                } catch (FJException success) {
1369                    checkCompletedAbnormally(g, success);
1370                }
1371            }};
1372        testInvokeOnPool(singletonPool(), a);
1373    }
1374
1375    /**
1376     * invokeAll(collection)  throws exception if any task does
1377     */
1378    public void testAbnormalInvokeAllCollectionSingleton() {
1379        RecursiveAction a = new CheckedRecursiveAction() {
1380            protected void realCompute() {
1381                FailingAsyncFib f = new FailingAsyncFib(8);
1382                AsyncFib g = new AsyncFib(9);
1383                AsyncFib h = new AsyncFib(7);
1384                HashSet set = new HashSet();
1385                set.add(f);
1386                set.add(g);
1387                set.add(h);
1388                try {
1389                    invokeAll(set);
1390                    shouldThrow();
1391                } catch (FJException success) {
1392                    checkCompletedAbnormally(f, success);
1393                }
1394            }};
1395        testInvokeOnPool(singletonPool(), a);
1396    }
1397
1398
1135      /**
1136       * ForkJoinTask.quietlyComplete returns when task completes
1137       * normally without setting a value. The most recent value
# Line 1417 | Line 1153 | public class ForkJoinTask8Test extends J
1153          testInvokeOnPool(mainPool(), a);
1154      }
1155  
1156 +    // jdk9
1157 +
1158 +    /**
1159 +     * pollSubmission returns unexecuted submitted task, if present
1160 +     */
1161 +    public void testPollSubmission() {
1162 +        final CountDownLatch done = new CountDownLatch(1);
1163 +        final ForkJoinTask a = ForkJoinTask.adapt(awaiter(done));
1164 +        final ForkJoinTask b = ForkJoinTask.adapt(awaiter(done));
1165 +        final ForkJoinTask c = ForkJoinTask.adapt(awaiter(done));
1166 +        final ForkJoinPool p = singletonPool();
1167 +        try (PoolCleaner cleaner = cleaner(p, done)) {
1168 +            Thread external = new Thread(new CheckedRunnable() {
1169 +                public void realRun() {
1170 +                    p.execute(a);
1171 +                    p.execute(b);
1172 +                    p.execute(c);
1173 +                }});
1174 +            RecursiveAction s = new CheckedRecursiveAction() {
1175 +                protected void realCompute() {
1176 +                    external.start();
1177 +                    try {
1178 +                        external.join();
1179 +                    } catch (Exception ex) {
1180 +                        threadUnexpectedException(ex);
1181 +                    }
1182 +                    assertTrue(p.hasQueuedSubmissions());
1183 +                    assertTrue(Thread.currentThread() instanceof ForkJoinWorkerThread);
1184 +                    ForkJoinTask r = ForkJoinTask.pollSubmission();
1185 +                    assertTrue(r == a || r == b || r == c);
1186 +                    assertFalse(r.isDone());
1187 +                }};
1188 +            p.invoke(s);
1189 +        }
1190 +    }
1191 +
1192   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines