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.9 by jsr166, Sat Feb 7 17:43:38 2015 UTC vs.
Revision 1.24 by jsr166, Sun Oct 11 15:34:06 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 <            f.complete();
366 >            catch (Throwable ex) {
367 >                compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE);
368 >            }
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);
730 <                AsyncFib g = new AsyncFib(9);
731 <                AsyncFib h = null;
879 >                AsyncFib nul = null;
880                  Runnable[] throwingActions = {
881 <                    () -> invokeAll(h),
882 <                    () -> invokeAll(f, g, h),
883 <                    () -> invokeAll(f, h, g),
884 <                    () -> invokeAll(h, f, g),
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() {
749                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);
808 <                set.add(g);
809 <                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 831 | 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 852 | 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 871 | 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 892 | Line 1067 | public class ForkJoinTask8Test extends J
1067                  assertSame(f, pollNextLocalTask());
1068                  helpQuiesce();
1069                  checkNotDone(f);
1070 <                assertEquals(34, g.number);
896 <                checkCompletedNormally(g);
1070 >                g.checkCompletedNormally();
1071              }};
1072          testInvokeOnPool(singletonPool(), a);
1073      }
# Line 911 | 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 929 | 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);
934 <                checkCompletedNormally(g);
1106 >                f.checkCompletedNormally();
1107 >                g.checkCompletedNormally();
1108              }};
1109          testInvokeOnPool(asyncSingletonPool(), a);
1110      }
# Line 949 | Line 1122 | public class ForkJoinTask8Test extends J
1122                  assertSame(f, f.fork());
1123                  assertSame(g, pollNextLocalTask());
1124                  helpQuiesce();
1125 <                assertEquals(21, f.number);
953 <                checkCompletedNormally(f);
1125 >                f.checkCompletedNormally();
1126                  checkNotDone(g);
1127              }};
1128          testInvokeOnPool(asyncSingletonPool(), a);
# Line 969 | Line 1141 | public class ForkJoinTask8Test extends J
1141                  assertSame(f, f.fork());
1142                  assertSame(g, pollTask());
1143                  helpQuiesce();
1144 <                assertEquals(21, f.number);
973 <                checkCompletedNormally(f);
1144 >                f.checkCompletedNormally();
1145                  checkNotDone(g);
1146              }};
1147          testInvokeOnPool(asyncSingletonPool(), a);
1148      }
1149  
979    // versions for singleton pools
980
981    /**
982     * invoke returns when task completes normally.
983     * isCompletedAbnormally and isCancelled return false for normally
984     * completed tasks; getRawResult returns null.
985     */
986    public void testInvokeSingleton() {
987        RecursiveAction a = new CheckedRecursiveAction() {
988            protected void realCompute() {
989                AsyncFib f = new AsyncFib(8);
990                assertNull(f.invoke());
991                assertEquals(21, f.number);
992                checkCompletedNormally(f);
993            }};
994        testInvokeOnPool(singletonPool(), a);
995    }
996
997    /**
998     * quietlyInvoke task returns when task completes normally.
999     * isCompletedAbnormally and isCancelled return false for normally
1000     * completed tasks
1001     */
1002    public void testQuietlyInvokeSingleton() {
1003        RecursiveAction a = new CheckedRecursiveAction() {
1004            protected void realCompute() {
1005                AsyncFib f = new AsyncFib(8);
1006                f.quietlyInvoke();
1007                assertEquals(21, f.number);
1008                checkCompletedNormally(f);
1009            }};
1010        testInvokeOnPool(singletonPool(), a);
1011    }
1012
1013    /**
1014     * join of a forked task returns when task completes
1015     */
1016    public void testForkJoinSingleton() {
1017        RecursiveAction a = new CheckedRecursiveAction() {
1018            protected void realCompute() {
1019                AsyncFib f = new AsyncFib(8);
1020                assertSame(f, f.fork());
1021                assertNull(f.join());
1022                assertEquals(21, f.number);
1023                checkCompletedNormally(f);
1024            }};
1025        testInvokeOnPool(singletonPool(), a);
1026    }
1027
1028    /**
1029     * get of a forked task returns when task completes
1030     */
1031    public void testForkGetSingleton() {
1032        RecursiveAction a = new CheckedRecursiveAction() {
1033            protected void realCompute() throws Exception {
1034                AsyncFib f = new AsyncFib(8);
1035                assertSame(f, f.fork());
1036                assertNull(f.get());
1037                assertEquals(21, f.number);
1038                checkCompletedNormally(f);
1039            }};
1040        testInvokeOnPool(singletonPool(), a);
1041    }
1042
1043    /**
1044     * timed get of a forked task returns when task completes
1045     */
1046    public void testForkTimedGetSingleton() {
1047        RecursiveAction a = new CheckedRecursiveAction() {
1048            protected void realCompute() throws Exception {
1049                AsyncFib f = new AsyncFib(8);
1050                assertSame(f, f.fork());
1051                assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
1052                assertEquals(21, f.number);
1053                checkCompletedNormally(f);
1054            }};
1055        testInvokeOnPool(singletonPool(), a);
1056    }
1057
1058    /**
1059     * timed get with null time unit throws NPE
1060     */
1061    public void testForkTimedGetNPESingleton() {
1062        RecursiveAction a = new CheckedRecursiveAction() {
1063            protected void realCompute() throws Exception {
1064                AsyncFib f = new AsyncFib(8);
1065                assertSame(f, f.fork());
1066                try {
1067                    f.get(5L, null);
1068                    shouldThrow();
1069                } catch (NullPointerException success) {}
1070            }};
1071        testInvokeOnPool(singletonPool(), a);
1072    }
1073
1074    /**
1075     * quietlyJoin of a forked task returns when task completes
1076     */
1077    public void testForkQuietlyJoinSingleton() {
1078        RecursiveAction a = new CheckedRecursiveAction() {
1079            protected void realCompute() {
1080                AsyncFib f = new AsyncFib(8);
1081                assertSame(f, f.fork());
1082                f.quietlyJoin();
1083                assertEquals(21, f.number);
1084                checkCompletedNormally(f);
1085            }};
1086        testInvokeOnPool(singletonPool(), a);
1087    }
1088
1089    /**
1090     * helpQuiesce returns when tasks are complete.
1091     * getQueuedTaskCount returns 0 when quiescent
1092     */
1093    public void testForkHelpQuiesceSingleton() {
1094        RecursiveAction a = new CheckedRecursiveAction() {
1095            protected void realCompute() {
1096                AsyncFib f = new AsyncFib(8);
1097                assertSame(f, f.fork());
1098                helpQuiesce();
1099                assertEquals(0, getQueuedTaskCount());
1100                assertEquals(21, f.number);
1101                checkCompletedNormally(f);
1102            }};
1103        testInvokeOnPool(singletonPool(), a);
1104    }
1105
1106    /**
1107     * invoke task throws exception when task completes abnormally
1108     */
1109    public void testAbnormalInvokeSingleton() {
1110        RecursiveAction a = new CheckedRecursiveAction() {
1111            protected void realCompute() {
1112                FailingAsyncFib f = new FailingAsyncFib(8);
1113                try {
1114                    f.invoke();
1115                    shouldThrow();
1116                } catch (FJException success) {
1117                    checkCompletedAbnormally(f, success);
1118                }
1119            }};
1120        testInvokeOnPool(singletonPool(), a);
1121    }
1122
1123    /**
1124     * quietlyInvoke task returns when task completes abnormally
1125     */
1126    public void testAbnormalQuietlyInvokeSingleton() {
1127        RecursiveAction a = new CheckedRecursiveAction() {
1128            protected void realCompute() {
1129                FailingAsyncFib f = new FailingAsyncFib(8);
1130                f.quietlyInvoke();
1131                assertTrue(f.getException() instanceof FJException);
1132                checkCompletedAbnormally(f, f.getException());
1133            }};
1134        testInvokeOnPool(singletonPool(), a);
1135    }
1136
1137    /**
1138     * join of a forked task throws exception when task completes abnormally
1139     */
1140    public void testAbnormalForkJoinSingleton() {
1141        RecursiveAction a = new CheckedRecursiveAction() {
1142            protected void realCompute() {
1143                FailingAsyncFib f = new FailingAsyncFib(8);
1144                assertSame(f, f.fork());
1145                try {
1146                    f.join();
1147                    shouldThrow();
1148                } catch (FJException success) {
1149                    checkCompletedAbnormally(f, success);
1150                }
1151            }};
1152        testInvokeOnPool(singletonPool(), a);
1153    }
1154
1155    /**
1156     * get of a forked task throws exception when task completes abnormally
1157     */
1158    public void testAbnormalForkGetSingleton() {
1159        RecursiveAction a = new CheckedRecursiveAction() {
1160            protected void realCompute() throws Exception {
1161                FailingAsyncFib f = new FailingAsyncFib(8);
1162                assertSame(f, f.fork());
1163                try {
1164                    f.get();
1165                    shouldThrow();
1166                } catch (ExecutionException success) {
1167                    Throwable cause = success.getCause();
1168                    assertTrue(cause instanceof FJException);
1169                    checkCompletedAbnormally(f, cause);
1170                }
1171            }};
1172        testInvokeOnPool(singletonPool(), a);
1173    }
1174
1175    /**
1176     * timed get of a forked task throws exception when task completes abnormally
1177     */
1178    public void testAbnormalForkTimedGetSingleton() {
1179        RecursiveAction a = new CheckedRecursiveAction() {
1180            protected void realCompute() throws Exception {
1181                FailingAsyncFib f = new FailingAsyncFib(8);
1182                assertSame(f, f.fork());
1183                try {
1184                    f.get(LONG_DELAY_MS, MILLISECONDS);
1185                    shouldThrow();
1186                } catch (ExecutionException success) {
1187                    Throwable cause = success.getCause();
1188                    assertTrue(cause instanceof FJException);
1189                    checkCompletedAbnormally(f, cause);
1190                }
1191            }};
1192        testInvokeOnPool(singletonPool(), a);
1193    }
1194
1195    /**
1196     * quietlyJoin of a forked task returns when task completes abnormally
1197     */
1198    public void testAbnormalForkQuietlyJoinSingleton() {
1199        RecursiveAction a = new CheckedRecursiveAction() {
1200            protected void realCompute() {
1201                FailingAsyncFib f = new FailingAsyncFib(8);
1202                assertSame(f, f.fork());
1203                f.quietlyJoin();
1204                assertTrue(f.getException() instanceof FJException);
1205                checkCompletedAbnormally(f, f.getException());
1206            }};
1207        testInvokeOnPool(singletonPool(), a);
1208    }
1209
1210    /**
1211     * invoke task throws exception after invoking completeExceptionally
1212     */
1213    public void testCompleteExceptionallySingleton() {
1214        RecursiveAction a = new CheckedRecursiveAction() {
1215            protected void realCompute() {
1216                AsyncFib f = new AsyncFib(8);
1217                f.completeExceptionally(new FJException());
1218                try {
1219                    f.invoke();
1220                    shouldThrow();
1221                } catch (FJException success) {
1222                    checkCompletedAbnormally(f, success);
1223                }
1224            }};
1225        testInvokeOnPool(singletonPool(), a);
1226    }
1227
1228    /**
1229     * invokeAll(t1, t2) invokes all task arguments
1230     */
1231    public void testInvokeAll2Singleton() {
1232        RecursiveAction a = new CheckedRecursiveAction() {
1233            protected void realCompute() {
1234                AsyncFib f = new AsyncFib(8);
1235                AsyncFib g = new AsyncFib(9);
1236                invokeAll(f, g);
1237                assertEquals(21, f.number);
1238                assertEquals(34, g.number);
1239                checkCompletedNormally(f);
1240                checkCompletedNormally(g);
1241            }};
1242        testInvokeOnPool(singletonPool(), a);
1243    }
1244
1245    /**
1246     * invokeAll(tasks) with 1 argument invokes task
1247     */
1248    public void testInvokeAll1Singleton() {
1249        RecursiveAction a = new CheckedRecursiveAction() {
1250            protected void realCompute() {
1251                AsyncFib f = new AsyncFib(8);
1252                invokeAll(f);
1253                checkCompletedNormally(f);
1254                assertEquals(21, f.number);
1255            }};
1256        testInvokeOnPool(singletonPool(), a);
1257    }
1258
1259    /**
1260     * invokeAll(tasks) with > 2 argument invokes tasks
1261     */
1262    public void testInvokeAll3Singleton() {
1263        RecursiveAction a = new CheckedRecursiveAction() {
1264            protected void realCompute() {
1265                AsyncFib f = new AsyncFib(8);
1266                AsyncFib g = new AsyncFib(9);
1267                AsyncFib h = new AsyncFib(7);
1268                invokeAll(f, g, h);
1269                assertEquals(21, f.number);
1270                assertEquals(34, g.number);
1271                assertEquals(13, h.number);
1272                checkCompletedNormally(f);
1273                checkCompletedNormally(g);
1274                checkCompletedNormally(h);
1275            }};
1276        testInvokeOnPool(singletonPool(), a);
1277    }
1278
1279    /**
1280     * invokeAll(collection) invokes all tasks in the collection
1281     */
1282    public void testInvokeAllCollectionSingleton() {
1283        RecursiveAction a = new CheckedRecursiveAction() {
1284            protected void realCompute() {
1285                AsyncFib f = new AsyncFib(8);
1286                AsyncFib g = new AsyncFib(9);
1287                AsyncFib h = new AsyncFib(7);
1288                HashSet set = new HashSet();
1289                set.add(f);
1290                set.add(g);
1291                set.add(h);
1292                invokeAll(set);
1293                assertEquals(21, f.number);
1294                assertEquals(34, g.number);
1295                assertEquals(13, h.number);
1296                checkCompletedNormally(f);
1297                checkCompletedNormally(g);
1298                checkCompletedNormally(h);
1299            }};
1300        testInvokeOnPool(singletonPool(), a);
1301    }
1302
1303    /**
1304     * invokeAll(tasks) with any null task throws NPE
1305     */
1306    public void testInvokeAllNPESingleton() {
1307        RecursiveAction a = new CheckedRecursiveAction() {
1308            protected void realCompute() {
1309                AsyncFib f = new AsyncFib(8);
1310                AsyncFib g = new AsyncFib(9);
1311                AsyncFib h = null;
1312                try {
1313                    invokeAll(f, g, h);
1314                    shouldThrow();
1315                } catch (NullPointerException success) {}
1316            }};
1317        testInvokeOnPool(singletonPool(), a);
1318    }
1319
1320    /**
1321     * invokeAll(t1, t2) throw exception if any task does
1322     */
1323    public void testAbnormalInvokeAll2Singleton() {
1324        RecursiveAction a = new CheckedRecursiveAction() {
1325            protected void realCompute() {
1326                AsyncFib f = new AsyncFib(8);
1327                FailingAsyncFib g = new FailingAsyncFib(9);
1328                try {
1329                    invokeAll(f, g);
1330                    shouldThrow();
1331                } catch (FJException success) {
1332                    checkCompletedAbnormally(g, success);
1333                }
1334            }};
1335        testInvokeOnPool(singletonPool(), a);
1336    }
1337
1338    /**
1339     * invokeAll(tasks) with 1 argument throws exception if task does
1340     */
1341    public void testAbnormalInvokeAll1Singleton() {
1342        RecursiveAction a = new CheckedRecursiveAction() {
1343            protected void realCompute() {
1344                FailingAsyncFib g = new FailingAsyncFib(9);
1345                try {
1346                    invokeAll(g);
1347                    shouldThrow();
1348                } catch (FJException success) {
1349                    checkCompletedAbnormally(g, success);
1350                }
1351            }};
1352        testInvokeOnPool(singletonPool(), a);
1353    }
1354
1355    /**
1356     * invokeAll(tasks) with > 2 argument throws exception if any task does
1357     */
1358    public void testAbnormalInvokeAll3Singleton() {
1359        RecursiveAction a = new CheckedRecursiveAction() {
1360            protected void realCompute() {
1361                AsyncFib f = new AsyncFib(8);
1362                FailingAsyncFib g = new FailingAsyncFib(9);
1363                AsyncFib h = new AsyncFib(7);
1364                try {
1365                    invokeAll(f, g, h);
1366                    shouldThrow();
1367                } catch (FJException success) {
1368                    checkCompletedAbnormally(g, success);
1369                }
1370            }};
1371        testInvokeOnPool(singletonPool(), a);
1372    }
1373
1374    /**
1375     * invokeAll(collection) throws exception if any task does
1376     */
1377    public void testAbnormalInvokeAllCollectionSingleton() {
1378        RecursiveAction a = new CheckedRecursiveAction() {
1379            protected void realCompute() {
1380                FailingAsyncFib f = new FailingAsyncFib(8);
1381                AsyncFib g = new AsyncFib(9);
1382                AsyncFib h = new AsyncFib(7);
1383                HashSet set = new HashSet();
1384                set.add(f);
1385                set.add(g);
1386                set.add(h);
1387                try {
1388                    invokeAll(set);
1389                    shouldThrow();
1390                } catch (FJException success) {
1391                    checkCompletedAbnormally(f, success);
1392                }
1393            }};
1394        testInvokeOnPool(singletonPool(), a);
1395    }
1396
1150      /**
1151       * ForkJoinTask.quietlyComplete returns when task completes
1152       * normally without setting a value. The most recent value
# Line 1415 | 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