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.28 by jsr166, Sat Mar 18 20:42:20 2017 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 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 180 | Line 193 | public class ForkJoinTask8Test extends J
193          } catch (Throwable fail) { threadUnexpectedException(fail); }
194      }
195  
183
196      public static final class FJException extends RuntimeException {
197          FJException() { super(); }
198      }
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 232 | 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 254 | Line 274 | public class ForkJoinTask8Test extends J
274          }
275  
276          public final void completeExceptionally(Throwable ex) {
277 <            BinaryAsyncAction a = this;
258 <            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 280 | 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);
299 <                        r.fork();
300 <                    }
301 <                    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 312 | 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 321 | 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 331 | 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 <            f.complete();
361 >            catch (Throwable ex) {
362 >                compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE);
363 >            }
364 >            if (getForkJoinTaskTag() == EXCEPTION_STATE)
365 >                throw new FJException();
366              return false;
367          }
368  
# Line 348 | 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);
356 <                checkCompletedNormally(f);
390 >                f.checkCompletedNormally();
391              }};
392 <        testInvokeOnPool(mainPool(), a);
392 >        testInvokeOnPool(pool, a);
393      }
394  
395      /**
# Line 364 | 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);
372 <                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);
387 <                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);
402 <                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);
417 <                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 432 | 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);
448 <                checkCompletedNormally(f);
513 >                f.checkCompletedNormally();
514              }};
515 <        testInvokeOnPool(mainPool(), a);
515 >        testInvokeOnPool(pool, a);
516      }
517  
518      /**
# Line 455 | 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();
463                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 481 | 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 495 | 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 513 | 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 533 | 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 553 | 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 568 | 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 598 | 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 632 | 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 643 | 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);
658 <                assertEquals(21, f.number);
659 <                assertEquals(34, g.number);
660 <                checkCompletedNormally(f);
661 <                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);
694 <                checkCompletedNormally(g);
695 <                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);
714 <                assertEquals(21, f.number);
715 <                assertEquals(34, g.number);
716 <                assertEquals(13, h.number);
717 <                checkCompletedNormally(f);
718 <                checkCompletedNormally(g);
719 <                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() {
747                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
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);
806 <                set.add(g);
807 <                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 829 | 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 850 | 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 869 | 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 890 | Line 1062 | public class ForkJoinTask8Test extends J
1062                  assertSame(f, pollNextLocalTask());
1063                  helpQuiesce();
1064                  checkNotDone(f);
1065 <                assertEquals(34, g.number);
894 <                checkCompletedNormally(g);
1065 >                g.checkCompletedNormally();
1066              }};
1067          testInvokeOnPool(singletonPool(), a);
1068      }
# Line 909 | 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 927 | 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);
932 <                checkCompletedNormally(g);
1101 >                f.checkCompletedNormally();
1102 >                g.checkCompletedNormally();
1103              }};
1104          testInvokeOnPool(asyncSingletonPool(), a);
1105      }
# Line 947 | Line 1117 | public class ForkJoinTask8Test extends J
1117                  assertSame(f, f.fork());
1118                  assertSame(g, pollNextLocalTask());
1119                  helpQuiesce();
1120 <                assertEquals(21, f.number);
951 <                checkCompletedNormally(f);
1120 >                f.checkCompletedNormally();
1121                  checkNotDone(g);
1122              }};
1123          testInvokeOnPool(asyncSingletonPool(), a);
# Line 967 | Line 1136 | public class ForkJoinTask8Test extends J
1136                  assertSame(f, f.fork());
1137                  assertSame(g, pollTask());
1138                  helpQuiesce();
1139 <                assertEquals(21, f.number);
971 <                checkCompletedNormally(f);
1139 >                f.checkCompletedNormally();
1140                  checkNotDone(g);
1141              }};
1142          testInvokeOnPool(asyncSingletonPool(), a);
1143      }
1144  
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
1145      /**
1146       * ForkJoinTask.quietlyComplete returns when task completes
1147       * normally without setting a value. The most recent value
# Line 1413 | 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