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.8 by jsr166, Thu Jan 15 18:34:19 2015 UTC vs.
Revision 1.23 by dl, Sun Oct 11 13:30:30 2015 UTC

# Line 7 | Line 7
7   import static java.util.concurrent.TimeUnit.MILLISECONDS;
8   import static java.util.concurrent.TimeUnit.SECONDS;
9  
10 < import java.util.HashSet;
10 > import java.util.Arrays;
11 > import java.util.Collections;
12 > import java.util.concurrent.CountDownLatch;
13   import java.util.concurrent.ExecutionException;
14   import java.util.concurrent.ForkJoinPool;
15   import java.util.concurrent.ForkJoinTask;
16 + import java.util.concurrent.ForkJoinWorkerThread;
17   import java.util.concurrent.RecursiveAction;
18   import java.util.concurrent.TimeoutException;
19  
# Line 34 | Line 37 | public class ForkJoinTask8Test extends J
37      static final short EXCEPTION_STATE = 1;
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 59 | 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 76 | Line 90 | public class ForkJoinTask8Test extends J
90              assertFalse(a.isCancelled());
91              assertNull(a.getException());
92              assertNull(a.getRawResult());
79        } finally {
80            joinPool(pool);
93          }
94      }
95  
# Line 114 | 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 160 | 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 186 | Line 198 | public class ForkJoinTask8Test extends J
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 231 | Line 243 | public class ForkJoinTask8Test extends J
243              super.completeExceptionally(ex);
244          }
245  
246 +        public boolean cancel(boolean mayInterruptIfRunning) {
247 +            if (super.cancel(mayInterruptIfRunning)) {
248 +                completeExceptionally(new FJException());
249 +                return true;
250 +            }
251 +            return false;
252 +        }
253 +        
254          public final void complete() {
255              BinaryAsyncAction a = this;
256              for (;;) {
# Line 253 | Line 273 | public class ForkJoinTask8Test extends J
273          }
274  
275          public final void completeExceptionally(Throwable ex) {
276 <            BinaryAsyncAction a = this;
257 <            while (!a.isCompletedAbnormally()) {
276 >            for (BinaryAsyncAction a = this;;) {
277                  a.completeThisExceptionally(ex);
278                  BinaryAsyncAction s = a.sibling;
279 <                if (s != null)
280 <                    s.cancel(false);
281 <                if (!a.onException() || (a = a.parent) == null)
279 >                if (s != null && !s.isDone())
280 >                    s.completeExceptionally(ex);
281 >                if ((a = a.parent) == null)
282                      break;
283              }
284          }
# Line 279 | Line 298 | public class ForkJoinTask8Test extends J
298  
299      }
300  
301 <    static final class AsyncFib extends BinaryAsyncAction {
301 >    final class AsyncFib extends BinaryAsyncAction {
302          int number;
303 <        public AsyncFib(int n) {
304 <            this.number = n;
303 >        int expectedResult;
304 >        public AsyncFib(int number) {
305 >            this.number = number;
306 >            this.expectedResult = fib[number];
307          }
308  
309          public final boolean exec() {
# Line 304 | Line 325 | public class ForkJoinTask8Test extends J
325              catch (Throwable ex) {
326                  compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE);
327              }
328 +            if (getForkJoinTaskTag() == EXCEPTION_STATE)
329 +                throw new FJException();
330              return false;
331          }
332  
# Line 311 | Line 334 | public class ForkJoinTask8Test extends J
334              number = ((AsyncFib)x).number + ((AsyncFib)y).number;
335              super.onComplete(x, y);
336          }
337 +
338 +        public void checkCompletedNormally() {
339 +            assertEquals(expectedResult, number);
340 +            ForkJoinTask8Test.this.checkCompletedNormally(this);
341 +        }
342      }
343  
344      static final class FailingAsyncFib extends BinaryAsyncAction {
# Line 320 | Line 348 | public class ForkJoinTask8Test extends J
348          }
349  
350          public final boolean exec() {
351 <            FailingAsyncFib f = this;
352 <            int n = f.number;
353 <            if (n > 1) {
354 <                while (n > 1) {
355 <                    FailingAsyncFib p = f;
356 <                    FailingAsyncFib r = new FailingAsyncFib(n - 2);
357 <                    f = new FailingAsyncFib(--n);
358 <                    p.linkSubtasks(r, f);
359 <                    r.fork();
351 >            try {
352 >                FailingAsyncFib f = this;
353 >                int n = f.number;
354 >                if (n > 1) {
355 >                    while (n > 1) {
356 >                        FailingAsyncFib p = f;
357 >                        FailingAsyncFib r = new FailingAsyncFib(n - 2);
358 >                        f = new FailingAsyncFib(--n);
359 >                        p.linkSubtasks(r, f);
360 >                        r.fork();
361 >                    }
362 >                    f.number = n;
363                  }
364 <                f.number = n;
364 >                f.complete();
365 >            }
366 >            catch (Throwable ex) {
367 >                compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE);
368              }
369 <            f.complete();
369 >            if (getForkJoinTaskTag() == EXCEPTION_STATE)
370 >                throw new FJException();
371              return false;
372          }
373  
# Line 347 | Line 382 | public class ForkJoinTask8Test extends J
382       * completed tasks; getRawResult returns null.
383       */
384      public void testInvoke() {
385 +        testInvoke(mainPool());
386 +    }
387 +    public void testInvoke_Singleton() {
388 +        testInvoke(singletonPool());
389 +    }
390 +    public void testInvoke(ForkJoinPool pool) {
391          RecursiveAction a = new CheckedRecursiveAction() {
392              protected void realCompute() {
393                  AsyncFib f = new AsyncFib(8);
394                  assertNull(f.invoke());
395 <                assertEquals(21, f.number);
355 <                checkCompletedNormally(f);
395 >                f.checkCompletedNormally();
396              }};
397 <        testInvokeOnPool(mainPool(), a);
397 >        testInvokeOnPool(pool, a);
398      }
399  
400      /**
# Line 363 | Line 403 | public class ForkJoinTask8Test extends J
403       * completed tasks
404       */
405      public void testQuietlyInvoke() {
406 +        testQuietlyInvoke(mainPool());
407 +    }
408 +    public void testQuietlyInvoke_Singleton() {
409 +        testQuietlyInvoke(singletonPool());
410 +    }
411 +    public void testQuietlyInvoke(ForkJoinPool pool) {
412          RecursiveAction a = new CheckedRecursiveAction() {
413              protected void realCompute() {
414                  AsyncFib f = new AsyncFib(8);
415                  f.quietlyInvoke();
416 <                assertEquals(21, f.number);
371 <                checkCompletedNormally(f);
416 >                f.checkCompletedNormally();
417              }};
418 <        testInvokeOnPool(mainPool(), a);
418 >        testInvokeOnPool(pool, a);
419      }
420  
421      /**
422       * join of a forked task returns when task completes
423       */
424      public void testForkJoin() {
425 +        testForkJoin(mainPool());
426 +    }
427 +    public void testForkJoin_Singleton() {
428 +        testForkJoin(singletonPool());
429 +    }
430 +    public void testForkJoin(ForkJoinPool pool) {
431          RecursiveAction a = new CheckedRecursiveAction() {
432              protected void realCompute() {
433                  AsyncFib f = new AsyncFib(8);
434                  assertSame(f, f.fork());
435                  assertNull(f.join());
436 <                assertEquals(21, f.number);
386 <                checkCompletedNormally(f);
436 >                f.checkCompletedNormally();
437              }};
438 <        testInvokeOnPool(mainPool(), a);
438 >        testInvokeOnPool(pool, a);
439      }
440  
441      /**
442       * get of a forked task returns when task completes
443       */
444      public void testForkGet() {
445 +        testForkGet(mainPool());
446 +    }
447 +    public void testForkGet_Singleton() {
448 +        testForkGet(singletonPool());
449 +    }
450 +    public void testForkGet(ForkJoinPool pool) {
451          RecursiveAction a = new CheckedRecursiveAction() {
452              protected void realCompute() throws Exception {
453                  AsyncFib f = new AsyncFib(8);
454                  assertSame(f, f.fork());
455                  assertNull(f.get());
456 <                assertEquals(21, f.number);
401 <                checkCompletedNormally(f);
456 >                f.checkCompletedNormally();
457              }};
458 <        testInvokeOnPool(mainPool(), a);
458 >        testInvokeOnPool(pool, a);
459      }
460  
461      /**
462       * timed get of a forked task returns when task completes
463       */
464      public void testForkTimedGet() {
465 +        testForkTimedGet(mainPool());
466 +    }
467 +    public void testForkTimedGet_Singleton() {
468 +        testForkTimedGet(singletonPool());
469 +    }
470 +    public void testForkTimedGet(ForkJoinPool pool) {
471          RecursiveAction a = new CheckedRecursiveAction() {
472              protected void realCompute() throws Exception {
473                  AsyncFib f = new AsyncFib(8);
474                  assertSame(f, f.fork());
475                  assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
476 <                assertEquals(21, f.number);
416 <                checkCompletedNormally(f);
476 >                f.checkCompletedNormally();
477              }};
478 <        testInvokeOnPool(mainPool(), a);
478 >        testInvokeOnPool(pool, a);
479      }
480  
481      /**
482 <     * timed get with null time unit throws NPE
482 >     * timed get with null time unit throws NullPointerException
483       */
484 <    public void testForkTimedGetNPE() {
484 >    public void testForkTimedGetNullTimeUnit() {
485 >        testForkTimedGetNullTimeUnit(mainPool());
486 >    }
487 >    public void testForkTimedGetNullTimeUnit_Singleton() {
488 >        testForkTimedGet(singletonPool());
489 >    }
490 >    public void testForkTimedGetNullTimeUnit(ForkJoinPool pool) {
491          RecursiveAction a = new CheckedRecursiveAction() {
492              protected void realCompute() throws Exception {
493                  AsyncFib f = new AsyncFib(8);
# Line 431 | Line 497 | public class ForkJoinTask8Test extends J
497                      shouldThrow();
498                  } catch (NullPointerException success) {}
499              }};
500 <        testInvokeOnPool(mainPool(), a);
500 >        testInvokeOnPool(pool, a);
501      }
502  
503      /**
504       * quietlyJoin of a forked task returns when task completes
505       */
506      public void testForkQuietlyJoin() {
507 +        testForkQuietlyJoin(mainPool());
508 +    }
509 +    public void testForkQuietlyJoin_Singleton() {
510 +        testForkQuietlyJoin(singletonPool());
511 +    }
512 +    public void testForkQuietlyJoin(ForkJoinPool pool) {
513          RecursiveAction a = new CheckedRecursiveAction() {
514              protected void realCompute() {
515                  AsyncFib f = new AsyncFib(8);
516                  assertSame(f, f.fork());
517                  f.quietlyJoin();
518 <                assertEquals(21, f.number);
447 <                checkCompletedNormally(f);
518 >                f.checkCompletedNormally();
519              }};
520 <        testInvokeOnPool(mainPool(), a);
520 >        testInvokeOnPool(pool, a);
521      }
522  
523      /**
# Line 454 | Line 525 | public class ForkJoinTask8Test extends J
525       * getQueuedTaskCount returns 0 when quiescent
526       */
527      public void testForkHelpQuiesce() {
528 +        testForkHelpQuiesce(mainPool());
529 +    }
530 +    public void testForkHelpQuiesce_Singleton() {
531 +        testForkHelpQuiesce(singletonPool());
532 +    }
533 +    public void testForkHelpQuiesce(ForkJoinPool pool) {
534          RecursiveAction a = new CheckedRecursiveAction() {
535              protected void realCompute() {
536                  AsyncFib f = new AsyncFib(8);
537                  assertSame(f, f.fork());
538                  helpQuiesce();
462                assertEquals(21, f.number);
539                  assertEquals(0, getQueuedTaskCount());
540 <                checkCompletedNormally(f);
540 >                f.checkCompletedNormally();
541              }};
542 <        testInvokeOnPool(mainPool(), a);
542 >        testInvokeOnPool(pool, a);
543      }
544  
545      /**
546       * invoke task throws exception when task completes abnormally
547       */
548      public void testAbnormalInvoke() {
549 +        testAbnormalInvoke(mainPool());
550 +    }
551 +    public void testAbnormalInvoke_Singleton() {
552 +        testAbnormalInvoke(singletonPool());
553 +    }
554 +    public void testAbnormalInvoke(ForkJoinPool pool) {
555          RecursiveAction a = new CheckedRecursiveAction() {
556              protected void realCompute() {
557                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 480 | Line 562 | public class ForkJoinTask8Test extends J
562                      checkCompletedAbnormally(f, success);
563                  }
564              }};
565 <        testInvokeOnPool(mainPool(), a);
565 >        testInvokeOnPool(pool, a);
566      }
567  
568      /**
569       * quietlyInvoke task returns when task completes abnormally
570       */
571      public void testAbnormalQuietlyInvoke() {
572 +        testAbnormalQuietlyInvoke(mainPool());
573 +    }
574 +    public void testAbnormalQuietlyInvoke_Singleton() {
575 +        testAbnormalQuietlyInvoke(singletonPool());
576 +    }
577 +    public void testAbnormalQuietlyInvoke(ForkJoinPool pool) {
578          RecursiveAction a = new CheckedRecursiveAction() {
579              protected void realCompute() {
580                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 494 | Line 582 | public class ForkJoinTask8Test extends J
582                  assertTrue(f.getException() instanceof FJException);
583                  checkCompletedAbnormally(f, f.getException());
584              }};
585 <        testInvokeOnPool(mainPool(), a);
585 >        testInvokeOnPool(pool, a);
586      }
587  
588      /**
589       * join of a forked task throws exception when task completes abnormally
590       */
591      public void testAbnormalForkJoin() {
592 +        testAbnormalForkJoin(mainPool());
593 +    }
594 +    public void testAbnormalForkJoin_Singleton() {
595 +        testAbnormalForkJoin(singletonPool());
596 +    }
597 +    public void testAbnormalForkJoin(ForkJoinPool pool) {
598          RecursiveAction a = new CheckedRecursiveAction() {
599              protected void realCompute() {
600                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 512 | Line 606 | public class ForkJoinTask8Test extends J
606                      checkCompletedAbnormally(f, success);
607                  }
608              }};
609 <        testInvokeOnPool(mainPool(), a);
609 >        testInvokeOnPool(pool, a);
610      }
611  
612      /**
613       * get of a forked task throws exception when task completes abnormally
614       */
615      public void testAbnormalForkGet() {
616 +        testAbnormalForkGet(mainPool());
617 +    }
618 +    public void testAbnormalForkGet_Singleton() {
619 +        testAbnormalForkJoin(singletonPool());
620 +    }
621 +    public void testAbnormalForkGet(ForkJoinPool pool) {
622          RecursiveAction a = new CheckedRecursiveAction() {
623              protected void realCompute() throws Exception {
624                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 532 | Line 632 | public class ForkJoinTask8Test extends J
632                      checkCompletedAbnormally(f, cause);
633                  }
634              }};
635 <        testInvokeOnPool(mainPool(), a);
635 >        testInvokeOnPool(pool, a);
636      }
637  
638      /**
639       * timed get of a forked task throws exception when task completes abnormally
640       */
641      public void testAbnormalForkTimedGet() {
642 +        testAbnormalForkTimedGet(mainPool());
643 +    }
644 +    public void testAbnormalForkTimedGet_Singleton() {
645 +        testAbnormalForkTimedGet(singletonPool());
646 +    }
647 +    public void testAbnormalForkTimedGet(ForkJoinPool pool) {
648          RecursiveAction a = new CheckedRecursiveAction() {
649              protected void realCompute() throws Exception {
650                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 552 | Line 658 | public class ForkJoinTask8Test extends J
658                      checkCompletedAbnormally(f, cause);
659                  }
660              }};
661 <        testInvokeOnPool(mainPool(), a);
661 >        testInvokeOnPool(pool, a);
662      }
663  
664      /**
665       * quietlyJoin of a forked task returns when task completes abnormally
666       */
667      public void testAbnormalForkQuietlyJoin() {
668 +        testAbnormalForkQuietlyJoin(mainPool());
669 +    }
670 +    public void testAbnormalForkQuietlyJoin_Singleton() {
671 +        testAbnormalForkQuietlyJoin(singletonPool());
672 +    }
673 +    public void testAbnormalForkQuietlyJoin(ForkJoinPool pool) {
674          RecursiveAction a = new CheckedRecursiveAction() {
675              protected void realCompute() {
676                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 567 | Line 679 | public class ForkJoinTask8Test extends J
679                  assertTrue(f.getException() instanceof FJException);
680                  checkCompletedAbnormally(f, f.getException());
681              }};
682 <        testInvokeOnPool(mainPool(), a);
682 >        testInvokeOnPool(pool, a);
683      }
684  
685      /**
686       * getPool of executing task returns its pool
687       */
688      public void testGetPool() {
689 <        final ForkJoinPool mainPool = mainPool();
689 >        testGetPool(mainPool());
690 >    }
691 >    public void testGetPool_Singleton() {
692 >        testGetPool(singletonPool());
693 >    }
694 >    public void testGetPool(ForkJoinPool pool) {
695          RecursiveAction a = new CheckedRecursiveAction() {
696              protected void realCompute() {
697 <                assertSame(mainPool, getPool());
697 >                assertSame(pool, getPool());
698              }};
699 <        testInvokeOnPool(mainPool, a);
699 >        testInvokeOnPool(pool, a);
700      }
701  
702      /**
# Line 597 | Line 714 | public class ForkJoinTask8Test extends J
714       * inForkJoinPool of executing task returns true
715       */
716      public void testInForkJoinPool() {
717 +        testInForkJoinPool(mainPool());
718 +    }
719 +    public void testInForkJoinPool_Singleton() {
720 +        testInForkJoinPool(singletonPool());
721 +    }
722 +    public void testInForkJoinPool(ForkJoinPool pool) {
723          RecursiveAction a = new CheckedRecursiveAction() {
724              protected void realCompute() {
725                  assertTrue(inForkJoinPool());
726              }};
727 <        testInvokeOnPool(mainPool(), a);
727 >        testInvokeOnPool(pool, a);
728      }
729  
730      /**
# Line 631 | Line 754 | public class ForkJoinTask8Test extends J
754       * invoke task throws exception after invoking completeExceptionally
755       */
756      public void testCompleteExceptionally() {
757 +        testCompleteExceptionally(mainPool());
758 +    }
759 +    public void testCompleteExceptionally_Singleton() {
760 +        testCompleteExceptionally(singletonPool());
761 +    }
762 +    public void testCompleteExceptionally(ForkJoinPool pool) {
763          RecursiveAction a = new CheckedRecursiveAction() {
764              protected void realCompute() {
765                  AsyncFib f = new AsyncFib(8);
# Line 642 | Line 771 | public class ForkJoinTask8Test extends J
771                      checkCompletedAbnormally(f, success);
772                  }
773              }};
774 <        testInvokeOnPool(mainPool(), a);
774 >        testInvokeOnPool(pool, a);
775      }
776  
777      /**
778 <     * invokeAll(t1, t2) invokes all task arguments
778 >     * invokeAll(tasks) with 1 argument invokes task
779       */
780 <    public void testInvokeAll2() {
780 >    public void testInvokeAll1() {
781 >        testInvokeAll1(mainPool());
782 >    }
783 >    public void testInvokeAll1_Singleton() {
784 >        testInvokeAll1(singletonPool());
785 >    }
786 >    public void testInvokeAll1(ForkJoinPool pool) {
787          RecursiveAction a = new CheckedRecursiveAction() {
788              protected void realCompute() {
789                  AsyncFib f = new AsyncFib(8);
790 <                AsyncFib g = new AsyncFib(9);
791 <                invokeAll(f, g);
657 <                assertEquals(21, f.number);
658 <                assertEquals(34, g.number);
659 <                checkCompletedNormally(f);
660 <                checkCompletedNormally(g);
790 >                invokeAll(f);
791 >                f.checkCompletedNormally();
792              }};
793 <        testInvokeOnPool(mainPool(), a);
793 >        testInvokeOnPool(pool, a);
794      }
795  
796      /**
797 <     * invokeAll(tasks) with 1 argument invokes task
797 >     * invokeAll(t1, t2) invokes all task arguments
798       */
799 <    public void testInvokeAll1() {
799 >    public void testInvokeAll2() {
800 >        testInvokeAll2(mainPool());
801 >    }
802 >    public void testInvokeAll2_Singleton() {
803 >        testInvokeAll2(singletonPool());
804 >    }
805 >    public void testInvokeAll2(ForkJoinPool pool) {
806          RecursiveAction a = new CheckedRecursiveAction() {
807              protected void realCompute() {
808 <                AsyncFib f = new AsyncFib(8);
809 <                invokeAll(f);
810 <                checkCompletedNormally(f);
811 <                assertEquals(21, f.number);
808 >                AsyncFib[] tasks = {
809 >                    new AsyncFib(8),
810 >                    new AsyncFib(9),
811 >                };
812 >                invokeAll(tasks[0], tasks[1]);
813 >                for (AsyncFib task : tasks) assertTrue(task.isDone());
814 >                for (AsyncFib task : tasks) task.checkCompletedNormally();
815              }};
816 <        testInvokeOnPool(mainPool(), a);
816 >        testInvokeOnPool(pool, a);
817      }
818  
819      /**
820       * invokeAll(tasks) with > 2 argument invokes tasks
821       */
822      public void testInvokeAll3() {
823 +        testInvokeAll3(mainPool());
824 +    }
825 +    public void testInvokeAll3_Singleton() {
826 +        testInvokeAll3(singletonPool());
827 +    }
828 +    public void testInvokeAll3(ForkJoinPool pool) {
829          RecursiveAction a = new CheckedRecursiveAction() {
830              protected void realCompute() {
831 <                AsyncFib f = new AsyncFib(8);
832 <                AsyncFib g = new AsyncFib(9);
833 <                AsyncFib h = new AsyncFib(7);
834 <                invokeAll(f, g, h);
835 <                assertEquals(21, f.number);
836 <                assertEquals(34, g.number);
837 <                assertEquals(13, h.number);
838 <                checkCompletedNormally(f);
693 <                checkCompletedNormally(g);
694 <                checkCompletedNormally(h);
831 >                AsyncFib[] tasks = {
832 >                    new AsyncFib(8),
833 >                    new AsyncFib(9),
834 >                    new AsyncFib(7),
835 >                };
836 >                invokeAll(tasks[0], tasks[1], tasks[2]);
837 >                for (AsyncFib task : tasks) assertTrue(task.isDone());
838 >                for (AsyncFib task : tasks) task.checkCompletedNormally();
839              }};
840 <        testInvokeOnPool(mainPool(), a);
840 >        testInvokeOnPool(pool, a);
841      }
842  
843      /**
844       * invokeAll(collection) invokes all tasks in the collection
845       */
846      public void testInvokeAllCollection() {
847 +        testInvokeAllCollection(mainPool());
848 +    }
849 +    public void testInvokeAllCollection_Singleton() {
850 +        testInvokeAllCollection(singletonPool());
851 +    }
852 +    public void testInvokeAllCollection(ForkJoinPool pool) {
853          RecursiveAction a = new CheckedRecursiveAction() {
854              protected void realCompute() {
855 <                AsyncFib f = new AsyncFib(8);
856 <                AsyncFib g = new AsyncFib(9);
857 <                AsyncFib h = new AsyncFib(7);
858 <                HashSet set = new HashSet();
859 <                set.add(f);
860 <                set.add(g);
861 <                set.add(h);
862 <                invokeAll(set);
713 <                assertEquals(21, f.number);
714 <                assertEquals(34, g.number);
715 <                assertEquals(13, h.number);
716 <                checkCompletedNormally(f);
717 <                checkCompletedNormally(g);
718 <                checkCompletedNormally(h);
855 >                AsyncFib[] tasks = {
856 >                    new AsyncFib(8),
857 >                    new AsyncFib(9),
858 >                    new AsyncFib(7),
859 >                };
860 >                invokeAll(Arrays.asList(tasks));
861 >                for (AsyncFib task : tasks) assertTrue(task.isDone());
862 >                for (AsyncFib task : tasks) task.checkCompletedNormally();
863              }};
864 <        testInvokeOnPool(mainPool(), a);
864 >        testInvokeOnPool(pool, a);
865      }
866  
867      /**
868 <     * invokeAll(tasks) with any null task throws NPE
868 >     * invokeAll(tasks) with any null task throws NullPointerException
869       */
870 <    public void testInvokeAllNPE() {
870 >    public void testInvokeAllNullTask() {
871 >        testInvokeAllNullTask(mainPool());
872 >    }
873 >    public void testInvokeAllNullTask_Singleton() {
874 >        testInvokeAllNullTask(singletonPool());
875 >    }
876 >    public void testInvokeAllNullTask(ForkJoinPool pool) {
877          RecursiveAction a = new CheckedRecursiveAction() {
878              protected void realCompute() {
879 <                AsyncFib f = new AsyncFib(8);
880 <                AsyncFib g = new AsyncFib(9);
881 <                AsyncFib h = null;
882 <                try {
883 <                    invokeAll(f, g, h);
884 <                    shouldThrow();
885 <                } catch (NullPointerException success) {}
879 >                AsyncFib nul = null;
880 >                Runnable[] throwingActions = {
881 >                    () -> invokeAll(nul),
882 >                    () -> invokeAll(nul, nul),
883 >                    () -> invokeAll(new AsyncFib(8), new AsyncFib(9), nul),
884 >                    () -> invokeAll(new AsyncFib(8), nul, new AsyncFib(9)),
885 >                    () -> invokeAll(nul, new AsyncFib(8), new AsyncFib(9)),
886 >                };
887 >                assertThrows(NullPointerException.class, throwingActions);
888              }};
889 <        testInvokeOnPool(mainPool(), a);
889 >        testInvokeOnPool(pool, a);
890      }
891  
892      /**
893 <     * invokeAll(t1, t2) throw exception if any task does
893 >     * invokeAll(tasks) with 1 argument throws exception if task does
894       */
895 <    public void testAbnormalInvokeAll2() {
895 >    public void testAbnormalInvokeAll1() {
896 >        testAbnormalInvokeAll1(mainPool());
897 >    }
898 >    public void testAbnormalInvokeAll1_Singleton() {
899 >        testAbnormalInvokeAll1(singletonPool());
900 >    }
901 >    public void testAbnormalInvokeAll1(ForkJoinPool pool) {
902          RecursiveAction a = new CheckedRecursiveAction() {
903              protected void realCompute() {
746                AsyncFib f = new AsyncFib(8);
904                  FailingAsyncFib g = new FailingAsyncFib(9);
905                  try {
906 <                    invokeAll(f, g);
906 >                    invokeAll(g);
907                      shouldThrow();
908                  } catch (FJException success) {
909                      checkCompletedAbnormally(g, success);
910                  }
911              }};
912 <        testInvokeOnPool(mainPool(), a);
912 >        testInvokeOnPool(pool, a);
913      }
914  
915      /**
916 <     * invokeAll(tasks) with 1 argument throws exception if task does
916 >     * invokeAll(t1, t2) throw exception if any task does
917       */
918 <    public void testAbnormalInvokeAll1() {
918 >    public void testAbnormalInvokeAll2() {
919 >        testAbnormalInvokeAll2(mainPool());
920 >    }
921 >    public void testAbnormalInvokeAll2_Singleton() {
922 >        testAbnormalInvokeAll2(singletonPool());
923 >    }
924 >    public void testAbnormalInvokeAll2(ForkJoinPool pool) {
925          RecursiveAction a = new CheckedRecursiveAction() {
926              protected void realCompute() {
927 +                AsyncFib f = new AsyncFib(8);
928                  FailingAsyncFib g = new FailingAsyncFib(9);
929 +                ForkJoinTask[] tasks = { f, g };
930 +                Collections.shuffle(Arrays.asList(tasks));
931                  try {
932 <                    invokeAll(g);
932 >                    invokeAll(tasks[0], tasks[1]);
933                      shouldThrow();
934                  } catch (FJException success) {
935                      checkCompletedAbnormally(g, success);
936                  }
937              }};
938 <        testInvokeOnPool(mainPool(), a);
938 >        testInvokeOnPool(pool, a);
939      }
940  
941      /**
942       * invokeAll(tasks) with > 2 argument throws exception if any task does
943       */
944      public void testAbnormalInvokeAll3() {
945 +        testAbnormalInvokeAll3(mainPool());
946 +    }
947 +    public void testAbnormalInvokeAll3_Singleton() {
948 +        testAbnormalInvokeAll3(singletonPool());
949 +    }
950 +    public void testAbnormalInvokeAll3(ForkJoinPool pool) {
951          RecursiveAction a = new CheckedRecursiveAction() {
952              protected void realCompute() {
953                  AsyncFib f = new AsyncFib(8);
954                  FailingAsyncFib g = new FailingAsyncFib(9);
955                  AsyncFib h = new AsyncFib(7);
956 +                ForkJoinTask[] tasks = { f, g, h };
957 +                Collections.shuffle(Arrays.asList(tasks));
958                  try {
959 <                    invokeAll(f, g, h);
959 >                    invokeAll(tasks[0], tasks[1], tasks[2]);
960                      shouldThrow();
961                  } catch (FJException success) {
962                      checkCompletedAbnormally(g, success);
963                  }
964              }};
965 <        testInvokeOnPool(mainPool(), a);
965 >        testInvokeOnPool(pool, a);
966      }
967  
968      /**
969       * invokeAll(collection) throws exception if any task does
970       */
971      public void testAbnormalInvokeAllCollection() {
972 +        testAbnormalInvokeAllCollection(mainPool());
973 +    }
974 +    public void testAbnormalInvokeAllCollection_Singleton() {
975 +        testAbnormalInvokeAllCollection(singletonPool());
976 +    }
977 +    public void testAbnormalInvokeAllCollection(ForkJoinPool pool) {
978          RecursiveAction a = new CheckedRecursiveAction() {
979              protected void realCompute() {
980                  FailingAsyncFib f = new FailingAsyncFib(8);
981                  AsyncFib g = new AsyncFib(9);
982                  AsyncFib h = new AsyncFib(7);
983 <                HashSet set = new HashSet();
984 <                set.add(f);
805 <                set.add(g);
806 <                set.add(h);
983 >                ForkJoinTask[] tasks = { f, g, h };
984 >                Collections.shuffle(Arrays.asList(tasks));
985                  try {
986 <                    invokeAll(set);
986 >                    invokeAll(Arrays.asList(tasks));
987                      shouldThrow();
988                  } catch (FJException success) {
989                      checkCompletedAbnormally(f, success);
990                  }
991              }};
992 <        testInvokeOnPool(mainPool(), a);
992 >        testInvokeOnPool(pool, a);
993      }
994  
995      /**
# Line 828 | Line 1006 | public class ForkJoinTask8Test extends J
1006                  assertTrue(f.tryUnfork());
1007                  helpQuiesce();
1008                  checkNotDone(f);
1009 <                checkCompletedNormally(g);
1009 >                g.checkCompletedNormally();
1010              }};
1011          testInvokeOnPool(singletonPool(), a);
1012      }
# Line 849 | Line 1027 | public class ForkJoinTask8Test extends J
1027                  assertTrue(getSurplusQueuedTaskCount() > 0);
1028                  helpQuiesce();
1029                  assertEquals(0, getSurplusQueuedTaskCount());
1030 <                checkCompletedNormally(f);
1031 <                checkCompletedNormally(g);
1032 <                checkCompletedNormally(h);
1030 >                f.checkCompletedNormally();
1031 >                g.checkCompletedNormally();
1032 >                h.checkCompletedNormally();
1033              }};
1034          testInvokeOnPool(singletonPool(), a);
1035      }
# Line 868 | Line 1046 | public class ForkJoinTask8Test extends J
1046                  assertSame(f, f.fork());
1047                  assertSame(f, peekNextLocalTask());
1048                  assertNull(f.join());
1049 <                checkCompletedNormally(f);
1049 >                f.checkCompletedNormally();
1050                  helpQuiesce();
1051 <                checkCompletedNormally(g);
1051 >                g.checkCompletedNormally();
1052              }};
1053          testInvokeOnPool(singletonPool(), a);
1054      }
# Line 889 | Line 1067 | public class ForkJoinTask8Test extends J
1067                  assertSame(f, pollNextLocalTask());
1068                  helpQuiesce();
1069                  checkNotDone(f);
1070 <                assertEquals(34, g.number);
893 <                checkCompletedNormally(g);
1070 >                g.checkCompletedNormally();
1071              }};
1072          testInvokeOnPool(singletonPool(), a);
1073      }
# Line 908 | Line 1085 | public class ForkJoinTask8Test extends J
1085                  assertSame(f, pollTask());
1086                  helpQuiesce();
1087                  checkNotDone(f);
1088 <                checkCompletedNormally(g);
1088 >                g.checkCompletedNormally();
1089              }};
1090          testInvokeOnPool(singletonPool(), a);
1091      }
# Line 926 | Line 1103 | public class ForkJoinTask8Test extends J
1103                  assertSame(g, peekNextLocalTask());
1104                  assertNull(f.join());
1105                  helpQuiesce();
1106 <                checkCompletedNormally(f);
1107 <                assertEquals(34, g.number);
931 <                checkCompletedNormally(g);
1106 >                f.checkCompletedNormally();
1107 >                g.checkCompletedNormally();
1108              }};
1109          testInvokeOnPool(asyncSingletonPool(), a);
1110      }
# Line 946 | Line 1122 | public class ForkJoinTask8Test extends J
1122                  assertSame(f, f.fork());
1123                  assertSame(g, pollNextLocalTask());
1124                  helpQuiesce();
1125 <                assertEquals(21, f.number);
950 <                checkCompletedNormally(f);
1125 >                f.checkCompletedNormally();
1126                  checkNotDone(g);
1127              }};
1128          testInvokeOnPool(asyncSingletonPool(), a);
# Line 966 | Line 1141 | public class ForkJoinTask8Test extends J
1141                  assertSame(f, f.fork());
1142                  assertSame(g, pollTask());
1143                  helpQuiesce();
1144 <                assertEquals(21, f.number);
970 <                checkCompletedNormally(f);
1144 >                f.checkCompletedNormally();
1145                  checkNotDone(g);
1146              }};
1147          testInvokeOnPool(asyncSingletonPool(), a);
1148      }
1149  
976    // versions for singleton pools
977
978    /**
979     * invoke returns when task completes normally.
980     * isCompletedAbnormally and isCancelled return false for normally
981     * completed tasks; getRawResult returns null.
982     */
983    public void testInvokeSingleton() {
984        RecursiveAction a = new CheckedRecursiveAction() {
985            protected void realCompute() {
986                AsyncFib f = new AsyncFib(8);
987                assertNull(f.invoke());
988                assertEquals(21, f.number);
989                checkCompletedNormally(f);
990            }};
991        testInvokeOnPool(singletonPool(), a);
992    }
993
994    /**
995     * quietlyInvoke task returns when task completes normally.
996     * isCompletedAbnormally and isCancelled return false for normally
997     * completed tasks
998     */
999    public void testQuietlyInvokeSingleton() {
1000        RecursiveAction a = new CheckedRecursiveAction() {
1001            protected void realCompute() {
1002                AsyncFib f = new AsyncFib(8);
1003                f.quietlyInvoke();
1004                assertEquals(21, f.number);
1005                checkCompletedNormally(f);
1006            }};
1007        testInvokeOnPool(singletonPool(), a);
1008    }
1009
1010    /**
1011     * join of a forked task returns when task completes
1012     */
1013    public void testForkJoinSingleton() {
1014        RecursiveAction a = new CheckedRecursiveAction() {
1015            protected void realCompute() {
1016                AsyncFib f = new AsyncFib(8);
1017                assertSame(f, f.fork());
1018                assertNull(f.join());
1019                assertEquals(21, f.number);
1020                checkCompletedNormally(f);
1021            }};
1022        testInvokeOnPool(singletonPool(), a);
1023    }
1024
1025    /**
1026     * get of a forked task returns when task completes
1027     */
1028    public void testForkGetSingleton() {
1029        RecursiveAction a = new CheckedRecursiveAction() {
1030            protected void realCompute() throws Exception {
1031                AsyncFib f = new AsyncFib(8);
1032                assertSame(f, f.fork());
1033                assertNull(f.get());
1034                assertEquals(21, f.number);
1035                checkCompletedNormally(f);
1036            }};
1037        testInvokeOnPool(singletonPool(), a);
1038    }
1039
1040    /**
1041     * timed get of a forked task returns when task completes
1042     */
1043    public void testForkTimedGetSingleton() {
1044        RecursiveAction a = new CheckedRecursiveAction() {
1045            protected void realCompute() throws Exception {
1046                AsyncFib f = new AsyncFib(8);
1047                assertSame(f, f.fork());
1048                assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
1049                assertEquals(21, f.number);
1050                checkCompletedNormally(f);
1051            }};
1052        testInvokeOnPool(singletonPool(), a);
1053    }
1054
1055    /**
1056     * timed get with null time unit throws NPE
1057     */
1058    public void testForkTimedGetNPESingleton() {
1059        RecursiveAction a = new CheckedRecursiveAction() {
1060            protected void realCompute() throws Exception {
1061                AsyncFib f = new AsyncFib(8);
1062                assertSame(f, f.fork());
1063                try {
1064                    f.get(5L, null);
1065                    shouldThrow();
1066                } catch (NullPointerException success) {}
1067            }};
1068        testInvokeOnPool(singletonPool(), a);
1069    }
1070
1071    /**
1072     * quietlyJoin of a forked task returns when task completes
1073     */
1074    public void testForkQuietlyJoinSingleton() {
1075        RecursiveAction a = new CheckedRecursiveAction() {
1076            protected void realCompute() {
1077                AsyncFib f = new AsyncFib(8);
1078                assertSame(f, f.fork());
1079                f.quietlyJoin();
1080                assertEquals(21, f.number);
1081                checkCompletedNormally(f);
1082            }};
1083        testInvokeOnPool(singletonPool(), a);
1084    }
1085
1086    /**
1087     * helpQuiesce returns when tasks are complete.
1088     * getQueuedTaskCount returns 0 when quiescent
1089     */
1090    public void testForkHelpQuiesceSingleton() {
1091        RecursiveAction a = new CheckedRecursiveAction() {
1092            protected void realCompute() {
1093                AsyncFib f = new AsyncFib(8);
1094                assertSame(f, f.fork());
1095                helpQuiesce();
1096                assertEquals(0, getQueuedTaskCount());
1097                assertEquals(21, f.number);
1098                checkCompletedNormally(f);
1099            }};
1100        testInvokeOnPool(singletonPool(), a);
1101    }
1102
1103    /**
1104     * invoke task throws exception when task completes abnormally
1105     */
1106    public void testAbnormalInvokeSingleton() {
1107        RecursiveAction a = new CheckedRecursiveAction() {
1108            protected void realCompute() {
1109                FailingAsyncFib f = new FailingAsyncFib(8);
1110                try {
1111                    f.invoke();
1112                    shouldThrow();
1113                } catch (FJException success) {
1114                    checkCompletedAbnormally(f, success);
1115                }
1116            }};
1117        testInvokeOnPool(singletonPool(), a);
1118    }
1119
1120    /**
1121     * quietlyInvoke task returns when task completes abnormally
1122     */
1123    public void testAbnormalQuietlyInvokeSingleton() {
1124        RecursiveAction a = new CheckedRecursiveAction() {
1125            protected void realCompute() {
1126                FailingAsyncFib f = new FailingAsyncFib(8);
1127                f.quietlyInvoke();
1128                assertTrue(f.getException() instanceof FJException);
1129                checkCompletedAbnormally(f, f.getException());
1130            }};
1131        testInvokeOnPool(singletonPool(), a);
1132    }
1133
1134    /**
1135     * join of a forked task throws exception when task completes abnormally
1136     */
1137    public void testAbnormalForkJoinSingleton() {
1138        RecursiveAction a = new CheckedRecursiveAction() {
1139            protected void realCompute() {
1140                FailingAsyncFib f = new FailingAsyncFib(8);
1141                assertSame(f, f.fork());
1142                try {
1143                    f.join();
1144                    shouldThrow();
1145                } catch (FJException success) {
1146                    checkCompletedAbnormally(f, success);
1147                }
1148            }};
1149        testInvokeOnPool(singletonPool(), a);
1150    }
1151
1152    /**
1153     * get of a forked task throws exception when task completes abnormally
1154     */
1155    public void testAbnormalForkGetSingleton() {
1156        RecursiveAction a = new CheckedRecursiveAction() {
1157            protected void realCompute() throws Exception {
1158                FailingAsyncFib f = new FailingAsyncFib(8);
1159                assertSame(f, f.fork());
1160                try {
1161                    f.get();
1162                    shouldThrow();
1163                } catch (ExecutionException success) {
1164                    Throwable cause = success.getCause();
1165                    assertTrue(cause instanceof FJException);
1166                    checkCompletedAbnormally(f, cause);
1167                }
1168            }};
1169        testInvokeOnPool(singletonPool(), a);
1170    }
1171
1172    /**
1173     * timed get of a forked task throws exception when task completes abnormally
1174     */
1175    public void testAbnormalForkTimedGetSingleton() {
1176        RecursiveAction a = new CheckedRecursiveAction() {
1177            protected void realCompute() throws Exception {
1178                FailingAsyncFib f = new FailingAsyncFib(8);
1179                assertSame(f, f.fork());
1180                try {
1181                    f.get(LONG_DELAY_MS, MILLISECONDS);
1182                    shouldThrow();
1183                } catch (ExecutionException success) {
1184                    Throwable cause = success.getCause();
1185                    assertTrue(cause instanceof FJException);
1186                    checkCompletedAbnormally(f, cause);
1187                }
1188            }};
1189        testInvokeOnPool(singletonPool(), a);
1190    }
1191
1192    /**
1193     * quietlyJoin of a forked task returns when task completes abnormally
1194     */
1195    public void testAbnormalForkQuietlyJoinSingleton() {
1196        RecursiveAction a = new CheckedRecursiveAction() {
1197            protected void realCompute() {
1198                FailingAsyncFib f = new FailingAsyncFib(8);
1199                assertSame(f, f.fork());
1200                f.quietlyJoin();
1201                assertTrue(f.getException() instanceof FJException);
1202                checkCompletedAbnormally(f, f.getException());
1203            }};
1204        testInvokeOnPool(singletonPool(), a);
1205    }
1206
1207    /**
1208     * invoke task throws exception after invoking completeExceptionally
1209     */
1210    public void testCompleteExceptionallySingleton() {
1211        RecursiveAction a = new CheckedRecursiveAction() {
1212            protected void realCompute() {
1213                AsyncFib f = new AsyncFib(8);
1214                f.completeExceptionally(new FJException());
1215                try {
1216                    f.invoke();
1217                    shouldThrow();
1218                } catch (FJException success) {
1219                    checkCompletedAbnormally(f, success);
1220                }
1221            }};
1222        testInvokeOnPool(singletonPool(), a);
1223    }
1224
1225    /**
1226     * invokeAll(t1, t2) invokes all task arguments
1227     */
1228    public void testInvokeAll2Singleton() {
1229        RecursiveAction a = new CheckedRecursiveAction() {
1230            protected void realCompute() {
1231                AsyncFib f = new AsyncFib(8);
1232                AsyncFib g = new AsyncFib(9);
1233                invokeAll(f, g);
1234                assertEquals(21, f.number);
1235                assertEquals(34, g.number);
1236                checkCompletedNormally(f);
1237                checkCompletedNormally(g);
1238            }};
1239        testInvokeOnPool(singletonPool(), a);
1240    }
1241
1242    /**
1243     * invokeAll(tasks) with 1 argument invokes task
1244     */
1245    public void testInvokeAll1Singleton() {
1246        RecursiveAction a = new CheckedRecursiveAction() {
1247            protected void realCompute() {
1248                AsyncFib f = new AsyncFib(8);
1249                invokeAll(f);
1250                checkCompletedNormally(f);
1251                assertEquals(21, f.number);
1252            }};
1253        testInvokeOnPool(singletonPool(), a);
1254    }
1255
1256    /**
1257     * invokeAll(tasks) with > 2 argument invokes tasks
1258     */
1259    public void testInvokeAll3Singleton() {
1260        RecursiveAction a = new CheckedRecursiveAction() {
1261            protected void realCompute() {
1262                AsyncFib f = new AsyncFib(8);
1263                AsyncFib g = new AsyncFib(9);
1264                AsyncFib h = new AsyncFib(7);
1265                invokeAll(f, g, h);
1266                assertEquals(21, f.number);
1267                assertEquals(34, g.number);
1268                assertEquals(13, h.number);
1269                checkCompletedNormally(f);
1270                checkCompletedNormally(g);
1271                checkCompletedNormally(h);
1272            }};
1273        testInvokeOnPool(singletonPool(), a);
1274    }
1275
1276    /**
1277     * invokeAll(collection) invokes all tasks in the collection
1278     */
1279    public void testInvokeAllCollectionSingleton() {
1280        RecursiveAction a = new CheckedRecursiveAction() {
1281            protected void realCompute() {
1282                AsyncFib f = new AsyncFib(8);
1283                AsyncFib g = new AsyncFib(9);
1284                AsyncFib h = new AsyncFib(7);
1285                HashSet set = new HashSet();
1286                set.add(f);
1287                set.add(g);
1288                set.add(h);
1289                invokeAll(set);
1290                assertEquals(21, f.number);
1291                assertEquals(34, g.number);
1292                assertEquals(13, h.number);
1293                checkCompletedNormally(f);
1294                checkCompletedNormally(g);
1295                checkCompletedNormally(h);
1296            }};
1297        testInvokeOnPool(singletonPool(), a);
1298    }
1299
1300    /**
1301     * invokeAll(tasks) with any null task throws NPE
1302     */
1303    public void testInvokeAllNPESingleton() {
1304        RecursiveAction a = new CheckedRecursiveAction() {
1305            protected void realCompute() {
1306                AsyncFib f = new AsyncFib(8);
1307                AsyncFib g = new AsyncFib(9);
1308                AsyncFib h = null;
1309                try {
1310                    invokeAll(f, g, h);
1311                    shouldThrow();
1312                } catch (NullPointerException success) {}
1313            }};
1314        testInvokeOnPool(singletonPool(), a);
1315    }
1316
1317    /**
1318     * invokeAll(t1, t2) throw exception if any task does
1319     */
1320    public void testAbnormalInvokeAll2Singleton() {
1321        RecursiveAction a = new CheckedRecursiveAction() {
1322            protected void realCompute() {
1323                AsyncFib f = new AsyncFib(8);
1324                FailingAsyncFib g = new FailingAsyncFib(9);
1325                try {
1326                    invokeAll(f, g);
1327                    shouldThrow();
1328                } catch (FJException success) {
1329                    checkCompletedAbnormally(g, success);
1330                }
1331            }};
1332        testInvokeOnPool(singletonPool(), a);
1333    }
1334
1335    /**
1336     * invokeAll(tasks) with 1 argument throws exception if task does
1337     */
1338    public void testAbnormalInvokeAll1Singleton() {
1339        RecursiveAction a = new CheckedRecursiveAction() {
1340            protected void realCompute() {
1341                FailingAsyncFib g = new FailingAsyncFib(9);
1342                try {
1343                    invokeAll(g);
1344                    shouldThrow();
1345                } catch (FJException success) {
1346                    checkCompletedAbnormally(g, success);
1347                }
1348            }};
1349        testInvokeOnPool(singletonPool(), a);
1350    }
1351
1352    /**
1353     * invokeAll(tasks) with > 2 argument throws exception if any task does
1354     */
1355    public void testAbnormalInvokeAll3Singleton() {
1356        RecursiveAction a = new CheckedRecursiveAction() {
1357            protected void realCompute() {
1358                AsyncFib f = new AsyncFib(8);
1359                FailingAsyncFib g = new FailingAsyncFib(9);
1360                AsyncFib h = new AsyncFib(7);
1361                try {
1362                    invokeAll(f, g, h);
1363                    shouldThrow();
1364                } catch (FJException success) {
1365                    checkCompletedAbnormally(g, success);
1366                }
1367            }};
1368        testInvokeOnPool(singletonPool(), a);
1369    }
1370
1371    /**
1372     * invokeAll(collection) throws exception if any task does
1373     */
1374    public void testAbnormalInvokeAllCollectionSingleton() {
1375        RecursiveAction a = new CheckedRecursiveAction() {
1376            protected void realCompute() {
1377                FailingAsyncFib f = new FailingAsyncFib(8);
1378                AsyncFib g = new AsyncFib(9);
1379                AsyncFib h = new AsyncFib(7);
1380                HashSet set = new HashSet();
1381                set.add(f);
1382                set.add(g);
1383                set.add(h);
1384                try {
1385                    invokeAll(set);
1386                    shouldThrow();
1387                } catch (FJException success) {
1388                    checkCompletedAbnormally(f, success);
1389                }
1390            }};
1391        testInvokeOnPool(singletonPool(), a);
1392    }
1393
1150      /**
1151       * ForkJoinTask.quietlyComplete returns when task completes
1152       * normally without setting a value. The most recent value
# Line 1412 | Line 1168 | public class ForkJoinTask8Test extends J
1168          testInvokeOnPool(mainPool(), a);
1169      }
1170  
1171 +    // jdk9
1172 +
1173 +    /**
1174 +     * pollSubmission returns unexecuted submitted task, if present
1175 +     */
1176 +    public void testPollSubmission() {
1177 +        final CountDownLatch done = new CountDownLatch(1);
1178 +        final ForkJoinTask a = ForkJoinTask.adapt(awaiter(done));
1179 +        final ForkJoinTask b = ForkJoinTask.adapt(awaiter(done));
1180 +        final ForkJoinTask c = ForkJoinTask.adapt(awaiter(done));
1181 +        final ForkJoinPool p = singletonPool();
1182 +        try (PoolCleaner cleaner = cleaner(p, done)) {
1183 +            Thread external = new Thread(new CheckedRunnable() {
1184 +                public void realRun() {
1185 +                    p.execute(a);
1186 +                    p.execute(b);
1187 +                    p.execute(c);
1188 +                }});
1189 +            RecursiveAction s = new CheckedRecursiveAction() {
1190 +                protected void realCompute() {
1191 +                    external.start();
1192 +                    try {
1193 +                        external.join();
1194 +                    } catch (Exception ex) {
1195 +                        threadUnexpectedException(ex);
1196 +                    }
1197 +                    assertTrue(p.hasQueuedSubmissions());
1198 +                    assertTrue(Thread.currentThread() instanceof ForkJoinWorkerThread);
1199 +                    ForkJoinTask r = ForkJoinTask.pollSubmission();
1200 +                    assertTrue(r == a || r == b || r == c);
1201 +                    assertFalse(r.isDone());
1202 +                }};
1203 +            p.invoke(s);
1204 +        }
1205 +    }
1206 +
1207   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines