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.28 by jsr166, Sat Mar 18 20:42:20 2017 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.concurrent.CountDownLatch;
12   import java.util.concurrent.ExecutionException;
13   import java.util.concurrent.ForkJoinPool;
14   import java.util.concurrent.ForkJoinTask;
15 + import java.util.concurrent.ForkJoinWorkerThread;
16   import java.util.concurrent.RecursiveAction;
17   import java.util.concurrent.TimeoutException;
18  
# Line 34 | Line 36 | public class ForkJoinTask8Test extends J
36      static final short EXCEPTION_STATE = 1;
37  
38      public static void main(String[] args) {
39 <        junit.textui.TestRunner.run(suite());
39 >        main(suite(), args);
40      }
41  
42      public static Test suite() {
# Line 59 | Line 61 | public class ForkJoinTask8Test extends J
61                                  null, true);
62      }
63  
64 +    // Compute fib naively and efficiently
65 +    final int[] fib;
66 +    {
67 +        int[] fib = new int[10];
68 +        fib[0] = 0;
69 +        fib[1] = 1;
70 +        for (int i = 2; i < fib.length; i++)
71 +            fib[i] = fib[i - 1] + fib[i - 2];
72 +        this.fib = fib;
73 +    }
74 +
75      private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
76 <        try {
76 >        try (PoolCleaner cleaner = cleaner(pool)) {
77              assertFalse(a.isDone());
78              assertFalse(a.isCompletedNormally());
79              assertFalse(a.isCompletedAbnormally());
# Line 76 | Line 89 | public class ForkJoinTask8Test extends J
89              assertFalse(a.isCancelled());
90              assertNull(a.getException());
91              assertNull(a.getRawResult());
79        } finally {
80            joinPool(pool);
92          }
93      }
94  
# Line 89 | Line 100 | public class ForkJoinTask8Test extends J
100          assertNull(a.getException());
101          assertNull(a.getRawResult());
102          if (a instanceof BinaryAsyncAction)
103 <            assertTrue(((BinaryAsyncAction)a).getForkJoinTaskTag() == INITIAL_STATE);
103 >            assertEquals(INITIAL_STATE,
104 >                         ((BinaryAsyncAction)a).getForkJoinTaskTag());
105  
106          try {
107              a.get(0L, SECONDS);
# Line 110 | Line 122 | public class ForkJoinTask8Test extends J
122          assertNull(a.getException());
123          assertSame(expected, a.getRawResult());
124          if (a instanceof BinaryAsyncAction)
125 <            assertTrue(((BinaryAsyncAction)a).getForkJoinTaskTag() == COMPLETE_STATE);
125 >            assertEquals(COMPLETE_STATE,
126 >                         ((BinaryAsyncAction)a).getForkJoinTaskTag());
127  
128          {
129              Thread.currentThread().interrupt();
130 <            long t0 = System.nanoTime();
130 >            long startTime = System.nanoTime();
131              assertSame(expected, a.join());
132 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
132 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
133              Thread.interrupted();
134          }
135  
136          {
137              Thread.currentThread().interrupt();
138 <            long t0 = System.nanoTime();
138 >            long startTime = System.nanoTime();
139              a.quietlyJoin();        // should be no-op
140 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
140 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
141              Thread.interrupted();
142          }
143  
# Line 160 | Line 173 | public class ForkJoinTask8Test extends J
173          Thread.interrupted();
174  
175          {
176 <            long t0 = System.nanoTime();
176 >            long startTime = System.nanoTime();
177              a.quietlyJoin();        // should be no-op
178 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
178 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
179          }
180  
181          try {
# Line 186 | Line 199 | public class ForkJoinTask8Test extends J
199  
200      abstract static class BinaryAsyncAction extends ForkJoinTask<Void> {
201  
202 <        private BinaryAsyncAction parent;
202 >        private volatile BinaryAsyncAction parent;
203  
204 <        private BinaryAsyncAction sibling;
204 >        private volatile BinaryAsyncAction sibling;
205  
206          protected BinaryAsyncAction() {
207              setForkJoinTaskTag(INITIAL_STATE);
# Line 231 | Line 244 | public class ForkJoinTask8Test extends J
244              super.completeExceptionally(ex);
245          }
246  
247 +        public boolean cancel(boolean mayInterruptIfRunning) {
248 +            if (super.cancel(mayInterruptIfRunning)) {
249 +                completeExceptionally(new FJException());
250 +                return true;
251 +            }
252 +            return false;
253 +        }
254 +
255          public final void complete() {
256              BinaryAsyncAction a = this;
257              for (;;) {
# Line 253 | Line 274 | public class ForkJoinTask8Test extends J
274          }
275  
276          public final void completeExceptionally(Throwable ex) {
277 <            BinaryAsyncAction a = this;
257 <            while (!a.isCompletedAbnormally()) {
277 >            for (BinaryAsyncAction a = this;;) {
278                  a.completeThisExceptionally(ex);
279                  BinaryAsyncAction s = a.sibling;
280 <                if (s != null)
281 <                    s.cancel(false);
282 <                if (!a.onException() || (a = a.parent) == null)
280 >                if (s != null && !s.isDone())
281 >                    s.completeExceptionally(ex);
282 >                if ((a = a.parent) == null)
283                      break;
284              }
285          }
# Line 279 | Line 299 | public class ForkJoinTask8Test extends J
299  
300      }
301  
302 <    static final class AsyncFib extends BinaryAsyncAction {
302 >    final class AsyncFib extends BinaryAsyncAction {
303          int number;
304 <        public AsyncFib(int n) {
305 <            this.number = n;
304 >        int expectedResult;
305 >        public AsyncFib(int number) {
306 >            this.number = number;
307 >            this.expectedResult = fib[number];
308          }
309  
310          public final boolean exec() {
311              try {
312                  AsyncFib f = this;
313                  int n = f.number;
314 <                if (n > 1) {
315 <                    while (n > 1) {
316 <                        AsyncFib p = f;
317 <                        AsyncFib r = new AsyncFib(n - 2);
318 <                        f = new AsyncFib(--n);
319 <                        p.linkSubtasks(r, f);
298 <                        r.fork();
299 <                    }
300 <                    f.number = n;
314 >                while (n > 1) {
315 >                    AsyncFib p = f;
316 >                    AsyncFib r = new AsyncFib(n - 2);
317 >                    f = new AsyncFib(--n);
318 >                    p.linkSubtasks(r, f);
319 >                    r.fork();
320                  }
321                  f.complete();
322              }
323              catch (Throwable ex) {
324                  compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE);
325              }
326 +            if (getForkJoinTaskTag() == EXCEPTION_STATE)
327 +                throw new FJException();
328              return false;
329          }
330  
# Line 311 | Line 332 | public class ForkJoinTask8Test extends J
332              number = ((AsyncFib)x).number + ((AsyncFib)y).number;
333              super.onComplete(x, y);
334          }
335 +
336 +        public void checkCompletedNormally() {
337 +            assertEquals(expectedResult, number);
338 +            ForkJoinTask8Test.this.checkCompletedNormally(this);
339 +        }
340      }
341  
342      static final class FailingAsyncFib extends BinaryAsyncAction {
# Line 320 | Line 346 | public class ForkJoinTask8Test extends J
346          }
347  
348          public final boolean exec() {
349 <            FailingAsyncFib f = this;
350 <            int n = f.number;
351 <            if (n > 1) {
349 >            try {
350 >                FailingAsyncFib f = this;
351 >                int n = f.number;
352                  while (n > 1) {
353                      FailingAsyncFib p = f;
354                      FailingAsyncFib r = new FailingAsyncFib(n - 2);
# Line 330 | Line 356 | public class ForkJoinTask8Test extends J
356                      p.linkSubtasks(r, f);
357                      r.fork();
358                  }
359 <                f.number = n;
359 >                f.complete();
360 >            }
361 >            catch (Throwable ex) {
362 >                compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE);
363              }
364 <            f.complete();
364 >            if (getForkJoinTaskTag() == EXCEPTION_STATE)
365 >                throw new FJException();
366              return false;
367          }
368  
# Line 347 | Line 377 | public class ForkJoinTask8Test extends J
377       * completed tasks; getRawResult returns null.
378       */
379      public void testInvoke() {
380 +        testInvoke(mainPool());
381 +    }
382 +    public void testInvoke_Singleton() {
383 +        testInvoke(singletonPool());
384 +    }
385 +    public void testInvoke(ForkJoinPool pool) {
386          RecursiveAction a = new CheckedRecursiveAction() {
387              protected void realCompute() {
388                  AsyncFib f = new AsyncFib(8);
389                  assertNull(f.invoke());
390 <                assertEquals(21, f.number);
355 <                checkCompletedNormally(f);
390 >                f.checkCompletedNormally();
391              }};
392 <        testInvokeOnPool(mainPool(), a);
392 >        testInvokeOnPool(pool, a);
393      }
394  
395      /**
# Line 363 | Line 398 | public class ForkJoinTask8Test extends J
398       * completed tasks
399       */
400      public void testQuietlyInvoke() {
401 +        testQuietlyInvoke(mainPool());
402 +    }
403 +    public void testQuietlyInvoke_Singleton() {
404 +        testQuietlyInvoke(singletonPool());
405 +    }
406 +    public void testQuietlyInvoke(ForkJoinPool pool) {
407          RecursiveAction a = new CheckedRecursiveAction() {
408              protected void realCompute() {
409                  AsyncFib f = new AsyncFib(8);
410                  f.quietlyInvoke();
411 <                assertEquals(21, f.number);
371 <                checkCompletedNormally(f);
411 >                f.checkCompletedNormally();
412              }};
413 <        testInvokeOnPool(mainPool(), a);
413 >        testInvokeOnPool(pool, a);
414      }
415  
416      /**
417       * join of a forked task returns when task completes
418       */
419      public void testForkJoin() {
420 +        testForkJoin(mainPool());
421 +    }
422 +    public void testForkJoin_Singleton() {
423 +        testForkJoin(singletonPool());
424 +    }
425 +    public void testForkJoin(ForkJoinPool pool) {
426          RecursiveAction a = new CheckedRecursiveAction() {
427              protected void realCompute() {
428                  AsyncFib f = new AsyncFib(8);
429                  assertSame(f, f.fork());
430                  assertNull(f.join());
431 <                assertEquals(21, f.number);
386 <                checkCompletedNormally(f);
431 >                f.checkCompletedNormally();
432              }};
433 <        testInvokeOnPool(mainPool(), a);
433 >        testInvokeOnPool(pool, a);
434      }
435  
436      /**
437       * get of a forked task returns when task completes
438       */
439      public void testForkGet() {
440 +        testForkGet(mainPool());
441 +    }
442 +    public void testForkGet_Singleton() {
443 +        testForkGet(singletonPool());
444 +    }
445 +    public void testForkGet(ForkJoinPool pool) {
446          RecursiveAction a = new CheckedRecursiveAction() {
447              protected void realCompute() throws Exception {
448                  AsyncFib f = new AsyncFib(8);
449                  assertSame(f, f.fork());
450                  assertNull(f.get());
451 <                assertEquals(21, f.number);
401 <                checkCompletedNormally(f);
451 >                f.checkCompletedNormally();
452              }};
453 <        testInvokeOnPool(mainPool(), a);
453 >        testInvokeOnPool(pool, a);
454      }
455  
456      /**
457       * timed get of a forked task returns when task completes
458       */
459      public void testForkTimedGet() {
460 +        testForkTimedGet(mainPool());
461 +    }
462 +    public void testForkTimedGet_Singleton() {
463 +        testForkTimedGet(singletonPool());
464 +    }
465 +    public void testForkTimedGet(ForkJoinPool pool) {
466          RecursiveAction a = new CheckedRecursiveAction() {
467              protected void realCompute() throws Exception {
468                  AsyncFib f = new AsyncFib(8);
469                  assertSame(f, f.fork());
470                  assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
471 <                assertEquals(21, f.number);
416 <                checkCompletedNormally(f);
471 >                f.checkCompletedNormally();
472              }};
473 <        testInvokeOnPool(mainPool(), a);
473 >        testInvokeOnPool(pool, a);
474      }
475  
476      /**
477 <     * timed get with null time unit throws NPE
477 >     * timed get with null time unit throws NullPointerException
478       */
479 <    public void testForkTimedGetNPE() {
479 >    public void testForkTimedGetNullTimeUnit() {
480 >        testForkTimedGetNullTimeUnit(mainPool());
481 >    }
482 >    public void testForkTimedGetNullTimeUnit_Singleton() {
483 >        testForkTimedGet(singletonPool());
484 >    }
485 >    public void testForkTimedGetNullTimeUnit(ForkJoinPool pool) {
486          RecursiveAction a = new CheckedRecursiveAction() {
487              protected void realCompute() throws Exception {
488                  AsyncFib f = new AsyncFib(8);
# Line 431 | Line 492 | public class ForkJoinTask8Test extends J
492                      shouldThrow();
493                  } catch (NullPointerException success) {}
494              }};
495 <        testInvokeOnPool(mainPool(), a);
495 >        testInvokeOnPool(pool, a);
496      }
497  
498      /**
499       * quietlyJoin of a forked task returns when task completes
500       */
501      public void testForkQuietlyJoin() {
502 +        testForkQuietlyJoin(mainPool());
503 +    }
504 +    public void testForkQuietlyJoin_Singleton() {
505 +        testForkQuietlyJoin(singletonPool());
506 +    }
507 +    public void testForkQuietlyJoin(ForkJoinPool pool) {
508          RecursiveAction a = new CheckedRecursiveAction() {
509              protected void realCompute() {
510                  AsyncFib f = new AsyncFib(8);
511                  assertSame(f, f.fork());
512                  f.quietlyJoin();
513 <                assertEquals(21, f.number);
447 <                checkCompletedNormally(f);
513 >                f.checkCompletedNormally();
514              }};
515 <        testInvokeOnPool(mainPool(), a);
515 >        testInvokeOnPool(pool, a);
516      }
517  
518      /**
# Line 454 | Line 520 | public class ForkJoinTask8Test extends J
520       * getQueuedTaskCount returns 0 when quiescent
521       */
522      public void testForkHelpQuiesce() {
523 +        testForkHelpQuiesce(mainPool());
524 +    }
525 +    public void testForkHelpQuiesce_Singleton() {
526 +        testForkHelpQuiesce(singletonPool());
527 +    }
528 +    public void testForkHelpQuiesce(ForkJoinPool pool) {
529          RecursiveAction a = new CheckedRecursiveAction() {
530              protected void realCompute() {
531                  AsyncFib f = new AsyncFib(8);
532                  assertSame(f, f.fork());
533                  helpQuiesce();
462                assertEquals(21, f.number);
534                  assertEquals(0, getQueuedTaskCount());
535 <                checkCompletedNormally(f);
535 >                f.checkCompletedNormally();
536              }};
537 <        testInvokeOnPool(mainPool(), a);
537 >        testInvokeOnPool(pool, a);
538      }
539  
540      /**
541       * invoke task throws exception when task completes abnormally
542       */
543      public void testAbnormalInvoke() {
544 +        testAbnormalInvoke(mainPool());
545 +    }
546 +    public void testAbnormalInvoke_Singleton() {
547 +        testAbnormalInvoke(singletonPool());
548 +    }
549 +    public void testAbnormalInvoke(ForkJoinPool pool) {
550          RecursiveAction a = new CheckedRecursiveAction() {
551              protected void realCompute() {
552                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 480 | Line 557 | public class ForkJoinTask8Test extends J
557                      checkCompletedAbnormally(f, success);
558                  }
559              }};
560 <        testInvokeOnPool(mainPool(), a);
560 >        testInvokeOnPool(pool, a);
561      }
562  
563      /**
564       * quietlyInvoke task returns when task completes abnormally
565       */
566      public void testAbnormalQuietlyInvoke() {
567 +        testAbnormalQuietlyInvoke(mainPool());
568 +    }
569 +    public void testAbnormalQuietlyInvoke_Singleton() {
570 +        testAbnormalQuietlyInvoke(singletonPool());
571 +    }
572 +    public void testAbnormalQuietlyInvoke(ForkJoinPool pool) {
573          RecursiveAction a = new CheckedRecursiveAction() {
574              protected void realCompute() {
575                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 494 | Line 577 | public class ForkJoinTask8Test extends J
577                  assertTrue(f.getException() instanceof FJException);
578                  checkCompletedAbnormally(f, f.getException());
579              }};
580 <        testInvokeOnPool(mainPool(), a);
580 >        testInvokeOnPool(pool, a);
581      }
582  
583      /**
584       * join of a forked task throws exception when task completes abnormally
585       */
586      public void testAbnormalForkJoin() {
587 +        testAbnormalForkJoin(mainPool());
588 +    }
589 +    public void testAbnormalForkJoin_Singleton() {
590 +        testAbnormalForkJoin(singletonPool());
591 +    }
592 +    public void testAbnormalForkJoin(ForkJoinPool pool) {
593          RecursiveAction a = new CheckedRecursiveAction() {
594              protected void realCompute() {
595                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 512 | Line 601 | public class ForkJoinTask8Test extends J
601                      checkCompletedAbnormally(f, success);
602                  }
603              }};
604 <        testInvokeOnPool(mainPool(), a);
604 >        testInvokeOnPool(pool, a);
605      }
606  
607      /**
608       * get of a forked task throws exception when task completes abnormally
609       */
610      public void testAbnormalForkGet() {
611 +        testAbnormalForkGet(mainPool());
612 +    }
613 +    public void testAbnormalForkGet_Singleton() {
614 +        testAbnormalForkJoin(singletonPool());
615 +    }
616 +    public void testAbnormalForkGet(ForkJoinPool pool) {
617          RecursiveAction a = new CheckedRecursiveAction() {
618              protected void realCompute() throws Exception {
619                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 532 | Line 627 | public class ForkJoinTask8Test extends J
627                      checkCompletedAbnormally(f, cause);
628                  }
629              }};
630 <        testInvokeOnPool(mainPool(), a);
630 >        testInvokeOnPool(pool, a);
631      }
632  
633      /**
634       * timed get of a forked task throws exception when task completes abnormally
635       */
636      public void testAbnormalForkTimedGet() {
637 +        testAbnormalForkTimedGet(mainPool());
638 +    }
639 +    public void testAbnormalForkTimedGet_Singleton() {
640 +        testAbnormalForkTimedGet(singletonPool());
641 +    }
642 +    public void testAbnormalForkTimedGet(ForkJoinPool pool) {
643          RecursiveAction a = new CheckedRecursiveAction() {
644              protected void realCompute() throws Exception {
645                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 552 | Line 653 | public class ForkJoinTask8Test extends J
653                      checkCompletedAbnormally(f, cause);
654                  }
655              }};
656 <        testInvokeOnPool(mainPool(), a);
656 >        testInvokeOnPool(pool, a);
657      }
658  
659      /**
660       * quietlyJoin of a forked task returns when task completes abnormally
661       */
662      public void testAbnormalForkQuietlyJoin() {
663 +        testAbnormalForkQuietlyJoin(mainPool());
664 +    }
665 +    public void testAbnormalForkQuietlyJoin_Singleton() {
666 +        testAbnormalForkQuietlyJoin(singletonPool());
667 +    }
668 +    public void testAbnormalForkQuietlyJoin(ForkJoinPool pool) {
669          RecursiveAction a = new CheckedRecursiveAction() {
670              protected void realCompute() {
671                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 567 | Line 674 | public class ForkJoinTask8Test extends J
674                  assertTrue(f.getException() instanceof FJException);
675                  checkCompletedAbnormally(f, f.getException());
676              }};
677 <        testInvokeOnPool(mainPool(), a);
677 >        testInvokeOnPool(pool, a);
678      }
679  
680      /**
681       * getPool of executing task returns its pool
682       */
683      public void testGetPool() {
684 <        final ForkJoinPool mainPool = mainPool();
684 >        testGetPool(mainPool());
685 >    }
686 >    public void testGetPool_Singleton() {
687 >        testGetPool(singletonPool());
688 >    }
689 >    public void testGetPool(ForkJoinPool pool) {
690          RecursiveAction a = new CheckedRecursiveAction() {
691              protected void realCompute() {
692 <                assertSame(mainPool, getPool());
692 >                assertSame(pool, getPool());
693              }};
694 <        testInvokeOnPool(mainPool, a);
694 >        testInvokeOnPool(pool, a);
695      }
696  
697      /**
# Line 597 | Line 709 | public class ForkJoinTask8Test extends J
709       * inForkJoinPool of executing task returns true
710       */
711      public void testInForkJoinPool() {
712 +        testInForkJoinPool(mainPool());
713 +    }
714 +    public void testInForkJoinPool_Singleton() {
715 +        testInForkJoinPool(singletonPool());
716 +    }
717 +    public void testInForkJoinPool(ForkJoinPool pool) {
718          RecursiveAction a = new CheckedRecursiveAction() {
719              protected void realCompute() {
720                  assertTrue(inForkJoinPool());
721              }};
722 <        testInvokeOnPool(mainPool(), a);
722 >        testInvokeOnPool(pool, a);
723      }
724  
725      /**
# Line 631 | Line 749 | public class ForkJoinTask8Test extends J
749       * invoke task throws exception after invoking completeExceptionally
750       */
751      public void testCompleteExceptionally() {
752 +        testCompleteExceptionally(mainPool());
753 +    }
754 +    public void testCompleteExceptionally_Singleton() {
755 +        testCompleteExceptionally(singletonPool());
756 +    }
757 +    public void testCompleteExceptionally(ForkJoinPool pool) {
758          RecursiveAction a = new CheckedRecursiveAction() {
759              protected void realCompute() {
760                  AsyncFib f = new AsyncFib(8);
# Line 642 | Line 766 | public class ForkJoinTask8Test extends J
766                      checkCompletedAbnormally(f, success);
767                  }
768              }};
769 <        testInvokeOnPool(mainPool(), a);
769 >        testInvokeOnPool(pool, a);
770      }
771  
772      /**
773 <     * invokeAll(t1, t2) invokes all task arguments
773 >     * invokeAll(tasks) with 1 argument invokes task
774       */
775 <    public void testInvokeAll2() {
775 >    public void testInvokeAll1() {
776 >        testInvokeAll1(mainPool());
777 >    }
778 >    public void testInvokeAll1_Singleton() {
779 >        testInvokeAll1(singletonPool());
780 >    }
781 >    public void testInvokeAll1(ForkJoinPool pool) {
782          RecursiveAction a = new CheckedRecursiveAction() {
783              protected void realCompute() {
784                  AsyncFib f = new AsyncFib(8);
785 <                AsyncFib g = new AsyncFib(9);
786 <                invokeAll(f, g);
657 <                assertEquals(21, f.number);
658 <                assertEquals(34, g.number);
659 <                checkCompletedNormally(f);
660 <                checkCompletedNormally(g);
785 >                invokeAll(f);
786 >                f.checkCompletedNormally();
787              }};
788 <        testInvokeOnPool(mainPool(), a);
788 >        testInvokeOnPool(pool, a);
789      }
790  
791      /**
792 <     * invokeAll(tasks) with 1 argument invokes task
792 >     * invokeAll(t1, t2) invokes all task arguments
793       */
794 <    public void testInvokeAll1() {
794 >    public void testInvokeAll2() {
795 >        testInvokeAll2(mainPool());
796 >    }
797 >    public void testInvokeAll2_Singleton() {
798 >        testInvokeAll2(singletonPool());
799 >    }
800 >    public void testInvokeAll2(ForkJoinPool pool) {
801          RecursiveAction a = new CheckedRecursiveAction() {
802              protected void realCompute() {
803 <                AsyncFib f = new AsyncFib(8);
804 <                invokeAll(f);
805 <                checkCompletedNormally(f);
806 <                assertEquals(21, f.number);
803 >                AsyncFib[] tasks = {
804 >                    new AsyncFib(8),
805 >                    new AsyncFib(9),
806 >                };
807 >                invokeAll(tasks[0], tasks[1]);
808 >                for (AsyncFib task : tasks) assertTrue(task.isDone());
809 >                for (AsyncFib task : tasks) task.checkCompletedNormally();
810              }};
811 <        testInvokeOnPool(mainPool(), a);
811 >        testInvokeOnPool(pool, a);
812      }
813  
814      /**
815       * invokeAll(tasks) with > 2 argument invokes tasks
816       */
817      public void testInvokeAll3() {
818 +        testInvokeAll3(mainPool());
819 +    }
820 +    public void testInvokeAll3_Singleton() {
821 +        testInvokeAll3(singletonPool());
822 +    }
823 +    public void testInvokeAll3(ForkJoinPool pool) {
824          RecursiveAction a = new CheckedRecursiveAction() {
825              protected void realCompute() {
826 <                AsyncFib f = new AsyncFib(8);
827 <                AsyncFib g = new AsyncFib(9);
828 <                AsyncFib h = new AsyncFib(7);
829 <                invokeAll(f, g, h);
830 <                assertEquals(21, f.number);
831 <                assertEquals(34, g.number);
832 <                assertEquals(13, h.number);
833 <                checkCompletedNormally(f);
693 <                checkCompletedNormally(g);
694 <                checkCompletedNormally(h);
826 >                AsyncFib[] tasks = {
827 >                    new AsyncFib(8),
828 >                    new AsyncFib(9),
829 >                    new AsyncFib(7),
830 >                };
831 >                invokeAll(tasks[0], tasks[1], tasks[2]);
832 >                for (AsyncFib task : tasks) assertTrue(task.isDone());
833 >                for (AsyncFib task : tasks) task.checkCompletedNormally();
834              }};
835 <        testInvokeOnPool(mainPool(), a);
835 >        testInvokeOnPool(pool, a);
836      }
837  
838      /**
839       * invokeAll(collection) invokes all tasks in the collection
840       */
841      public void testInvokeAllCollection() {
842 +        testInvokeAllCollection(mainPool());
843 +    }
844 +    public void testInvokeAllCollection_Singleton() {
845 +        testInvokeAllCollection(singletonPool());
846 +    }
847 +    public void testInvokeAllCollection(ForkJoinPool pool) {
848          RecursiveAction a = new CheckedRecursiveAction() {
849              protected void realCompute() {
850 <                AsyncFib f = new AsyncFib(8);
851 <                AsyncFib g = new AsyncFib(9);
852 <                AsyncFib h = new AsyncFib(7);
853 <                HashSet set = new HashSet();
854 <                set.add(f);
855 <                set.add(g);
856 <                set.add(h);
857 <                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);
850 >                AsyncFib[] tasks = {
851 >                    new AsyncFib(8),
852 >                    new AsyncFib(9),
853 >                    new AsyncFib(7),
854 >                };
855 >                invokeAll(Arrays.asList(tasks));
856 >                for (AsyncFib task : tasks) assertTrue(task.isDone());
857 >                for (AsyncFib task : tasks) task.checkCompletedNormally();
858              }};
859 <        testInvokeOnPool(mainPool(), a);
859 >        testInvokeOnPool(pool, a);
860      }
861  
862      /**
863 <     * invokeAll(tasks) with any null task throws NPE
863 >     * invokeAll(tasks) with any null task throws NullPointerException
864       */
865 <    public void testInvokeAllNPE() {
865 >    public void testInvokeAllNullTask() {
866 >        testInvokeAllNullTask(mainPool());
867 >    }
868 >    public void testInvokeAllNullTask_Singleton() {
869 >        testInvokeAllNullTask(singletonPool());
870 >    }
871 >    public void testInvokeAllNullTask(ForkJoinPool pool) {
872          RecursiveAction a = new CheckedRecursiveAction() {
873              protected void realCompute() {
874 <                AsyncFib f = new AsyncFib(8);
875 <                AsyncFib g = new AsyncFib(9);
876 <                AsyncFib h = null;
877 <                try {
878 <                    invokeAll(f, g, h);
879 <                    shouldThrow();
880 <                } catch (NullPointerException success) {}
874 >                AsyncFib nul = null;
875 >                Runnable[] throwingActions = {
876 >                    () -> invokeAll(nul),
877 >                    () -> invokeAll(nul, nul),
878 >                    () -> invokeAll(new AsyncFib(8), new AsyncFib(9), nul),
879 >                    () -> invokeAll(new AsyncFib(8), nul, new AsyncFib(9)),
880 >                    () -> invokeAll(nul, new AsyncFib(8), new AsyncFib(9)),
881 >                };
882 >                assertThrows(NullPointerException.class, throwingActions);
883              }};
884 <        testInvokeOnPool(mainPool(), a);
884 >        testInvokeOnPool(pool, a);
885      }
886  
887      /**
888 <     * invokeAll(t1, t2) throw exception if any task does
888 >     * invokeAll(tasks) with 1 argument throws exception if task does
889       */
890 <    public void testAbnormalInvokeAll2() {
890 >    public void testAbnormalInvokeAll1() {
891 >        testAbnormalInvokeAll1(mainPool());
892 >    }
893 >    public void testAbnormalInvokeAll1_Singleton() {
894 >        testAbnormalInvokeAll1(singletonPool());
895 >    }
896 >    public void testAbnormalInvokeAll1(ForkJoinPool pool) {
897          RecursiveAction a = new CheckedRecursiveAction() {
898              protected void realCompute() {
746                AsyncFib f = new AsyncFib(8);
899                  FailingAsyncFib g = new FailingAsyncFib(9);
900                  try {
901 <                    invokeAll(f, g);
901 >                    invokeAll(g);
902                      shouldThrow();
903                  } catch (FJException success) {
904                      checkCompletedAbnormally(g, success);
905                  }
906              }};
907 <        testInvokeOnPool(mainPool(), a);
907 >        testInvokeOnPool(pool, a);
908      }
909  
910      /**
911 <     * invokeAll(tasks) with 1 argument throws exception if task does
911 >     * invokeAll(t1, t2) throw exception if any task does
912       */
913 <    public void testAbnormalInvokeAll1() {
913 >    public void testAbnormalInvokeAll2() {
914 >        testAbnormalInvokeAll2(mainPool());
915 >    }
916 >    public void testAbnormalInvokeAll2_Singleton() {
917 >        testAbnormalInvokeAll2(singletonPool());
918 >    }
919 >    public void testAbnormalInvokeAll2(ForkJoinPool pool) {
920          RecursiveAction a = new CheckedRecursiveAction() {
921              protected void realCompute() {
922 +                AsyncFib f = new AsyncFib(8);
923                  FailingAsyncFib g = new FailingAsyncFib(9);
924 +                ForkJoinTask[] tasks = { f, g };
925 +                shuffle(tasks);
926                  try {
927 <                    invokeAll(g);
927 >                    invokeAll(tasks[0], tasks[1]);
928                      shouldThrow();
929                  } catch (FJException success) {
930                      checkCompletedAbnormally(g, success);
931                  }
932              }};
933 <        testInvokeOnPool(mainPool(), a);
933 >        testInvokeOnPool(pool, a);
934      }
935  
936      /**
937       * invokeAll(tasks) with > 2 argument throws exception if any task does
938       */
939      public void testAbnormalInvokeAll3() {
940 +        testAbnormalInvokeAll3(mainPool());
941 +    }
942 +    public void testAbnormalInvokeAll3_Singleton() {
943 +        testAbnormalInvokeAll3(singletonPool());
944 +    }
945 +    public void testAbnormalInvokeAll3(ForkJoinPool pool) {
946          RecursiveAction a = new CheckedRecursiveAction() {
947              protected void realCompute() {
948                  AsyncFib f = new AsyncFib(8);
949                  FailingAsyncFib g = new FailingAsyncFib(9);
950                  AsyncFib h = new AsyncFib(7);
951 +                ForkJoinTask[] tasks = { f, g, h };
952 +                shuffle(tasks);
953                  try {
954 <                    invokeAll(f, g, h);
954 >                    invokeAll(tasks[0], tasks[1], tasks[2]);
955                      shouldThrow();
956                  } catch (FJException success) {
957                      checkCompletedAbnormally(g, success);
958                  }
959              }};
960 <        testInvokeOnPool(mainPool(), a);
960 >        testInvokeOnPool(pool, a);
961      }
962  
963      /**
964       * invokeAll(collection) throws exception if any task does
965       */
966      public void testAbnormalInvokeAllCollection() {
967 +        testAbnormalInvokeAllCollection(mainPool());
968 +    }
969 +    public void testAbnormalInvokeAllCollection_Singleton() {
970 +        testAbnormalInvokeAllCollection(singletonPool());
971 +    }
972 +    public void testAbnormalInvokeAllCollection(ForkJoinPool pool) {
973          RecursiveAction a = new CheckedRecursiveAction() {
974              protected void realCompute() {
975                  FailingAsyncFib f = new FailingAsyncFib(8);
976                  AsyncFib g = new AsyncFib(9);
977                  AsyncFib h = new AsyncFib(7);
978 <                HashSet set = new HashSet();
979 <                set.add(f);
805 <                set.add(g);
806 <                set.add(h);
978 >                ForkJoinTask[] tasks = { f, g, h };
979 >                shuffle(tasks);
980                  try {
981 <                    invokeAll(set);
981 >                    invokeAll(Arrays.asList(tasks));
982                      shouldThrow();
983                  } catch (FJException success) {
984                      checkCompletedAbnormally(f, success);
985                  }
986              }};
987 <        testInvokeOnPool(mainPool(), a);
987 >        testInvokeOnPool(pool, a);
988      }
989  
990      /**
# Line 828 | Line 1001 | public class ForkJoinTask8Test extends J
1001                  assertTrue(f.tryUnfork());
1002                  helpQuiesce();
1003                  checkNotDone(f);
1004 <                checkCompletedNormally(g);
1004 >                g.checkCompletedNormally();
1005              }};
1006          testInvokeOnPool(singletonPool(), a);
1007      }
# Line 849 | Line 1022 | public class ForkJoinTask8Test extends J
1022                  assertTrue(getSurplusQueuedTaskCount() > 0);
1023                  helpQuiesce();
1024                  assertEquals(0, getSurplusQueuedTaskCount());
1025 <                checkCompletedNormally(f);
1026 <                checkCompletedNormally(g);
1027 <                checkCompletedNormally(h);
1025 >                f.checkCompletedNormally();
1026 >                g.checkCompletedNormally();
1027 >                h.checkCompletedNormally();
1028              }};
1029          testInvokeOnPool(singletonPool(), a);
1030      }
# Line 868 | Line 1041 | public class ForkJoinTask8Test extends J
1041                  assertSame(f, f.fork());
1042                  assertSame(f, peekNextLocalTask());
1043                  assertNull(f.join());
1044 <                checkCompletedNormally(f);
1044 >                f.checkCompletedNormally();
1045                  helpQuiesce();
1046 <                checkCompletedNormally(g);
1046 >                g.checkCompletedNormally();
1047              }};
1048          testInvokeOnPool(singletonPool(), a);
1049      }
# Line 889 | Line 1062 | public class ForkJoinTask8Test extends J
1062                  assertSame(f, pollNextLocalTask());
1063                  helpQuiesce();
1064                  checkNotDone(f);
1065 <                assertEquals(34, g.number);
893 <                checkCompletedNormally(g);
1065 >                g.checkCompletedNormally();
1066              }};
1067          testInvokeOnPool(singletonPool(), a);
1068      }
# Line 908 | Line 1080 | public class ForkJoinTask8Test extends J
1080                  assertSame(f, pollTask());
1081                  helpQuiesce();
1082                  checkNotDone(f);
1083 <                checkCompletedNormally(g);
1083 >                g.checkCompletedNormally();
1084              }};
1085          testInvokeOnPool(singletonPool(), a);
1086      }
# Line 926 | Line 1098 | public class ForkJoinTask8Test extends J
1098                  assertSame(g, peekNextLocalTask());
1099                  assertNull(f.join());
1100                  helpQuiesce();
1101 <                checkCompletedNormally(f);
1102 <                assertEquals(34, g.number);
931 <                checkCompletedNormally(g);
1101 >                f.checkCompletedNormally();
1102 >                g.checkCompletedNormally();
1103              }};
1104          testInvokeOnPool(asyncSingletonPool(), a);
1105      }
# Line 946 | Line 1117 | public class ForkJoinTask8Test extends J
1117                  assertSame(f, f.fork());
1118                  assertSame(g, pollNextLocalTask());
1119                  helpQuiesce();
1120 <                assertEquals(21, f.number);
950 <                checkCompletedNormally(f);
1120 >                f.checkCompletedNormally();
1121                  checkNotDone(g);
1122              }};
1123          testInvokeOnPool(asyncSingletonPool(), a);
# Line 966 | Line 1136 | public class ForkJoinTask8Test extends J
1136                  assertSame(f, f.fork());
1137                  assertSame(g, pollTask());
1138                  helpQuiesce();
1139 <                assertEquals(21, f.number);
970 <                checkCompletedNormally(f);
1139 >                f.checkCompletedNormally();
1140                  checkNotDone(g);
1141              }};
1142          testInvokeOnPool(asyncSingletonPool(), a);
1143      }
1144  
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
1145      /**
1146       * ForkJoinTask.quietlyComplete returns when task completes
1147       * normally without setting a value. The most recent value
# Line 1412 | Line 1163 | public class ForkJoinTask8Test extends J
1163          testInvokeOnPool(mainPool(), a);
1164      }
1165  
1166 +    // jdk9
1167 +
1168 +    /**
1169 +     * pollSubmission returns unexecuted submitted task, if present
1170 +     */
1171 +    public void testPollSubmission() {
1172 +        final CountDownLatch done = new CountDownLatch(1);
1173 +        final ForkJoinTask a = ForkJoinTask.adapt(awaiter(done));
1174 +        final ForkJoinTask b = ForkJoinTask.adapt(awaiter(done));
1175 +        final ForkJoinTask c = ForkJoinTask.adapt(awaiter(done));
1176 +        final ForkJoinPool p = singletonPool();
1177 +        try (PoolCleaner cleaner = cleaner(p, done)) {
1178 +            Thread external = new Thread(new CheckedRunnable() {
1179 +                public void realRun() {
1180 +                    p.execute(a);
1181 +                    p.execute(b);
1182 +                    p.execute(c);
1183 +                }});
1184 +            RecursiveAction s = new CheckedRecursiveAction() {
1185 +                protected void realCompute() {
1186 +                    external.start();
1187 +                    try {
1188 +                        external.join();
1189 +                    } catch (Exception ex) {
1190 +                        threadUnexpectedException(ex);
1191 +                    }
1192 +                    assertTrue(p.hasQueuedSubmissions());
1193 +                    assertTrue(Thread.currentThread() instanceof ForkJoinWorkerThread);
1194 +                    ForkJoinTask r = ForkJoinTask.pollSubmission();
1195 +                    assertTrue(r == a || r == b || r == c);
1196 +                    assertFalse(r.isDone());
1197 +                }};
1198 +            p.invoke(s);
1199 +        }
1200 +    }
1201 +
1202   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines