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.3 by jsr166, Mon Jul 22 18:01:03 2013 UTC vs.
Revision 1.27 by jsr166, Wed Aug 24 22:22:39 2016 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines