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.6 by jsr166, Wed Dec 31 17:00:58 2014 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 < import java.util.HashSet;
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;
13   import java.util.concurrent.ForkJoinPool;
14   import java.util.concurrent.ForkJoinTask;
15 + import java.util.concurrent.ForkJoinWorkerThread;
16   import java.util.concurrent.RecursiveAction;
17   import java.util.concurrent.TimeoutException;
18 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
19 < import static java.util.concurrent.TimeUnit.SECONDS;
20 < import junit.framework.*;
18 >
19 > import junit.framework.Test;
20 > import junit.framework.TestSuite;
21  
22   public class ForkJoinTask8Test extends JSR166TestCase {
23  
# Line 30 | 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 55 | 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 72 | Line 89 | public class ForkJoinTask8Test extends J
89              assertFalse(a.isCancelled());
90              assertNull(a.getException());
91              assertNull(a.getRawResult());
75        } finally {
76            joinPool(pool);
92          }
93      }
94  
# Line 85 | 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 106 | 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 156 | 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 176 | Line 193 | public class ForkJoinTask8Test extends J
193          } catch (Throwable fail) { threadUnexpectedException(fail); }
194      }
195  
179
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 228 | 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 250 | Line 274 | public class ForkJoinTask8Test extends J
274          }
275  
276          public final void completeExceptionally(Throwable ex) {
277 <            BinaryAsyncAction a = this;
254 <            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 276 | 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);
295 <                        r.fork();
296 <                    }
297 <                    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 308 | 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 317 | 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 327 | 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 344 | 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);
352 <                checkCompletedNormally(f);
390 >                f.checkCompletedNormally();
391              }};
392 <        testInvokeOnPool(mainPool(), a);
392 >        testInvokeOnPool(pool, a);
393      }
394  
395      /**
# Line 360 | 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);
368 <                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);
383 <                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);
398 <                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);
413 <                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 428 | 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);
444 <                checkCompletedNormally(f);
513 >                f.checkCompletedNormally();
514              }};
515 <        testInvokeOnPool(mainPool(), a);
515 >        testInvokeOnPool(pool, a);
516      }
517  
518      /**
# Line 451 | 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();
459                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 477 | 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 491 | 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 509 | 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 529 | 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 549 | 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 564 | 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 594 | 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 628 | 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 639 | 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);
654 <                assertEquals(21, f.number);
655 <                assertEquals(34, g.number);
656 <                checkCompletedNormally(f);
657 <                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);
690 <                checkCompletedNormally(g);
691 <                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);
710 <                assertEquals(21, f.number);
711 <                assertEquals(34, g.number);
712 <                assertEquals(13, h.number);
713 <                checkCompletedNormally(f);
714 <                checkCompletedNormally(g);
715 <                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() {
743                AsyncFib f = new AsyncFib(8);
899                  FailingAsyncFib g = new FailingAsyncFib(9);
900                  try {
901 <                    invokeAll(f, g);
901 >                    invokeAll(g);
902                      shouldThrow();
903                  } catch (FJException success) {
904                      checkCompletedAbnormally(g, success);
905                  }
906              }};
907 <        testInvokeOnPool(mainPool(), a);
907 >        testInvokeOnPool(pool, a);
908      }
909  
910      /**
911 <     * invokeAll(tasks) with 1 argument throws exception if task does
911 >     * invokeAll(t1, t2) throw exception if any task does
912       */
913 <    public void testAbnormalInvokeAll1() {
913 >    public void testAbnormalInvokeAll2() {
914 >        testAbnormalInvokeAll2(mainPool());
915 >    }
916 >    public void testAbnormalInvokeAll2_Singleton() {
917 >        testAbnormalInvokeAll2(singletonPool());
918 >    }
919 >    public void testAbnormalInvokeAll2(ForkJoinPool pool) {
920          RecursiveAction a = new CheckedRecursiveAction() {
921              protected void realCompute() {
922 +                AsyncFib f = new AsyncFib(8);
923                  FailingAsyncFib g = new FailingAsyncFib(9);
924 +                ForkJoinTask[] tasks = { f, g };
925 +                shuffle(tasks);
926                  try {
927 <                    invokeAll(g);
927 >                    invokeAll(tasks[0], tasks[1]);
928                      shouldThrow();
929                  } catch (FJException success) {
930                      checkCompletedAbnormally(g, success);
931                  }
932              }};
933 <        testInvokeOnPool(mainPool(), a);
933 >        testInvokeOnPool(pool, a);
934      }
935  
936      /**
937       * invokeAll(tasks) with > 2 argument throws exception if any task does
938       */
939      public void testAbnormalInvokeAll3() {
940 +        testAbnormalInvokeAll3(mainPool());
941 +    }
942 +    public void testAbnormalInvokeAll3_Singleton() {
943 +        testAbnormalInvokeAll3(singletonPool());
944 +    }
945 +    public void testAbnormalInvokeAll3(ForkJoinPool pool) {
946          RecursiveAction a = new CheckedRecursiveAction() {
947              protected void realCompute() {
948                  AsyncFib f = new AsyncFib(8);
949                  FailingAsyncFib g = new FailingAsyncFib(9);
950                  AsyncFib h = new AsyncFib(7);
951 +                ForkJoinTask[] tasks = { f, g, h };
952 +                shuffle(tasks);
953                  try {
954 <                    invokeAll(f, g, h);
954 >                    invokeAll(tasks[0], tasks[1], tasks[2]);
955                      shouldThrow();
956                  } catch (FJException success) {
957                      checkCompletedAbnormally(g, success);
958                  }
959              }};
960 <        testInvokeOnPool(mainPool(), a);
960 >        testInvokeOnPool(pool, a);
961      }
962  
963      /**
964       * invokeAll(collection) throws exception if any task does
965       */
966      public void testAbnormalInvokeAllCollection() {
967 +        testAbnormalInvokeAllCollection(mainPool());
968 +    }
969 +    public void testAbnormalInvokeAllCollection_Singleton() {
970 +        testAbnormalInvokeAllCollection(singletonPool());
971 +    }
972 +    public void testAbnormalInvokeAllCollection(ForkJoinPool pool) {
973          RecursiveAction a = new CheckedRecursiveAction() {
974              protected void realCompute() {
975                  FailingAsyncFib f = new FailingAsyncFib(8);
976                  AsyncFib g = new AsyncFib(9);
977                  AsyncFib h = new AsyncFib(7);
978 <                HashSet set = new HashSet();
979 <                set.add(f);
802 <                set.add(g);
803 <                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 825 | 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 846 | 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 865 | 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 886 | Line 1062 | public class ForkJoinTask8Test extends J
1062                  assertSame(f, pollNextLocalTask());
1063                  helpQuiesce();
1064                  checkNotDone(f);
1065 <                assertEquals(34, g.number);
890 <                checkCompletedNormally(g);
1065 >                g.checkCompletedNormally();
1066              }};
1067          testInvokeOnPool(singletonPool(), a);
1068      }
# Line 905 | 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 923 | 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);
928 <                checkCompletedNormally(g);
1101 >                f.checkCompletedNormally();
1102 >                g.checkCompletedNormally();
1103              }};
1104          testInvokeOnPool(asyncSingletonPool(), a);
1105      }
# Line 943 | Line 1117 | public class ForkJoinTask8Test extends J
1117                  assertSame(f, f.fork());
1118                  assertSame(g, pollNextLocalTask());
1119                  helpQuiesce();
1120 <                assertEquals(21, f.number);
947 <                checkCompletedNormally(f);
1120 >                f.checkCompletedNormally();
1121                  checkNotDone(g);
1122              }};
1123          testInvokeOnPool(asyncSingletonPool(), a);
# Line 963 | Line 1136 | public class ForkJoinTask8Test extends J
1136                  assertSame(f, f.fork());
1137                  assertSame(g, pollTask());
1138                  helpQuiesce();
1139 <                assertEquals(21, f.number);
967 <                checkCompletedNormally(f);
1139 >                f.checkCompletedNormally();
1140                  checkNotDone(g);
1141              }};
1142          testInvokeOnPool(asyncSingletonPool(), a);
1143      }
1144  
973    // versions for singleton pools
974
975    /**
976     * invoke returns when task completes normally.
977     * isCompletedAbnormally and isCancelled return false for normally
978     * completed tasks; getRawResult returns null.
979     */
980    public void testInvokeSingleton() {
981        RecursiveAction a = new CheckedRecursiveAction() {
982            protected void realCompute() {
983                AsyncFib f = new AsyncFib(8);
984                assertNull(f.invoke());
985                assertEquals(21, f.number);
986                checkCompletedNormally(f);
987            }};
988        testInvokeOnPool(singletonPool(), a);
989    }
990
991    /**
992     * quietlyInvoke task returns when task completes normally.
993     * isCompletedAbnormally and isCancelled return false for normally
994     * completed tasks
995     */
996    public void testQuietlyInvokeSingleton() {
997        RecursiveAction a = new CheckedRecursiveAction() {
998            protected void realCompute() {
999                AsyncFib f = new AsyncFib(8);
1000                f.quietlyInvoke();
1001                assertEquals(21, f.number);
1002                checkCompletedNormally(f);
1003            }};
1004        testInvokeOnPool(singletonPool(), a);
1005    }
1006
1007    /**
1008     * join of a forked task returns when task completes
1009     */
1010    public void testForkJoinSingleton() {
1011        RecursiveAction a = new CheckedRecursiveAction() {
1012            protected void realCompute() {
1013                AsyncFib f = new AsyncFib(8);
1014                assertSame(f, f.fork());
1015                assertNull(f.join());
1016                assertEquals(21, f.number);
1017                checkCompletedNormally(f);
1018            }};
1019        testInvokeOnPool(singletonPool(), a);
1020    }
1021
1022    /**
1023     * get of a forked task returns when task completes
1024     */
1025    public void testForkGetSingleton() {
1026        RecursiveAction a = new CheckedRecursiveAction() {
1027            protected void realCompute() throws Exception {
1028                AsyncFib f = new AsyncFib(8);
1029                assertSame(f, f.fork());
1030                assertNull(f.get());
1031                assertEquals(21, f.number);
1032                checkCompletedNormally(f);
1033            }};
1034        testInvokeOnPool(singletonPool(), a);
1035    }
1036
1037    /**
1038     * timed get of a forked task returns when task completes
1039     */
1040    public void testForkTimedGetSingleton() {
1041        RecursiveAction a = new CheckedRecursiveAction() {
1042            protected void realCompute() throws Exception {
1043                AsyncFib f = new AsyncFib(8);
1044                assertSame(f, f.fork());
1045                assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
1046                assertEquals(21, f.number);
1047                checkCompletedNormally(f);
1048            }};
1049        testInvokeOnPool(singletonPool(), a);
1050    }
1051
1052    /**
1053     * timed get with null time unit throws NPE
1054     */
1055    public void testForkTimedGetNPESingleton() {
1056        RecursiveAction a = new CheckedRecursiveAction() {
1057            protected void realCompute() throws Exception {
1058                AsyncFib f = new AsyncFib(8);
1059                assertSame(f, f.fork());
1060                try {
1061                    f.get(5L, null);
1062                    shouldThrow();
1063                } catch (NullPointerException success) {}
1064            }};
1065        testInvokeOnPool(singletonPool(), a);
1066    }
1067
1068    /**
1069     * quietlyJoin of a forked task returns when task completes
1070     */
1071    public void testForkQuietlyJoinSingleton() {
1072        RecursiveAction a = new CheckedRecursiveAction() {
1073            protected void realCompute() {
1074                AsyncFib f = new AsyncFib(8);
1075                assertSame(f, f.fork());
1076                f.quietlyJoin();
1077                assertEquals(21, f.number);
1078                checkCompletedNormally(f);
1079            }};
1080        testInvokeOnPool(singletonPool(), a);
1081    }
1082
1083    /**
1084     * helpQuiesce returns when tasks are complete.
1085     * getQueuedTaskCount returns 0 when quiescent
1086     */
1087    public void testForkHelpQuiesceSingleton() {
1088        RecursiveAction a = new CheckedRecursiveAction() {
1089            protected void realCompute() {
1090                AsyncFib f = new AsyncFib(8);
1091                assertSame(f, f.fork());
1092                helpQuiesce();
1093                assertEquals(0, getQueuedTaskCount());
1094                assertEquals(21, f.number);
1095                checkCompletedNormally(f);
1096            }};
1097        testInvokeOnPool(singletonPool(), a);
1098    }
1099
1100    /**
1101     * invoke task throws exception when task completes abnormally
1102     */
1103    public void testAbnormalInvokeSingleton() {
1104        RecursiveAction a = new CheckedRecursiveAction() {
1105            protected void realCompute() {
1106                FailingAsyncFib f = new FailingAsyncFib(8);
1107                try {
1108                    f.invoke();
1109                    shouldThrow();
1110                } catch (FJException success) {
1111                    checkCompletedAbnormally(f, success);
1112                }
1113            }};
1114        testInvokeOnPool(singletonPool(), a);
1115    }
1116
1117    /**
1118     * quietlyInvoke task returns when task completes abnormally
1119     */
1120    public void testAbnormalQuietlyInvokeSingleton() {
1121        RecursiveAction a = new CheckedRecursiveAction() {
1122            protected void realCompute() {
1123                FailingAsyncFib f = new FailingAsyncFib(8);
1124                f.quietlyInvoke();
1125                assertTrue(f.getException() instanceof FJException);
1126                checkCompletedAbnormally(f, f.getException());
1127            }};
1128        testInvokeOnPool(singletonPool(), a);
1129    }
1130
1131    /**
1132     * join of a forked task throws exception when task completes abnormally
1133     */
1134    public void testAbnormalForkJoinSingleton() {
1135        RecursiveAction a = new CheckedRecursiveAction() {
1136            protected void realCompute() {
1137                FailingAsyncFib f = new FailingAsyncFib(8);
1138                assertSame(f, f.fork());
1139                try {
1140                    f.join();
1141                    shouldThrow();
1142                } catch (FJException success) {
1143                    checkCompletedAbnormally(f, success);
1144                }
1145            }};
1146        testInvokeOnPool(singletonPool(), a);
1147    }
1148
1149    /**
1150     * get of a forked task throws exception when task completes abnormally
1151     */
1152    public void testAbnormalForkGetSingleton() {
1153        RecursiveAction a = new CheckedRecursiveAction() {
1154            protected void realCompute() throws Exception {
1155                FailingAsyncFib f = new FailingAsyncFib(8);
1156                assertSame(f, f.fork());
1157                try {
1158                    f.get();
1159                    shouldThrow();
1160                } catch (ExecutionException success) {
1161                    Throwable cause = success.getCause();
1162                    assertTrue(cause instanceof FJException);
1163                    checkCompletedAbnormally(f, cause);
1164                }
1165            }};
1166        testInvokeOnPool(singletonPool(), a);
1167    }
1168
1169    /**
1170     * timed get of a forked task throws exception when task completes abnormally
1171     */
1172    public void testAbnormalForkTimedGetSingleton() {
1173        RecursiveAction a = new CheckedRecursiveAction() {
1174            protected void realCompute() throws Exception {
1175                FailingAsyncFib f = new FailingAsyncFib(8);
1176                assertSame(f, f.fork());
1177                try {
1178                    f.get(LONG_DELAY_MS, MILLISECONDS);
1179                    shouldThrow();
1180                } catch (ExecutionException success) {
1181                    Throwable cause = success.getCause();
1182                    assertTrue(cause instanceof FJException);
1183                    checkCompletedAbnormally(f, cause);
1184                }
1185            }};
1186        testInvokeOnPool(singletonPool(), a);
1187    }
1188
1189    /**
1190     * quietlyJoin of a forked task returns when task completes abnormally
1191     */
1192    public void testAbnormalForkQuietlyJoinSingleton() {
1193        RecursiveAction a = new CheckedRecursiveAction() {
1194            protected void realCompute() {
1195                FailingAsyncFib f = new FailingAsyncFib(8);
1196                assertSame(f, f.fork());
1197                f.quietlyJoin();
1198                assertTrue(f.getException() instanceof FJException);
1199                checkCompletedAbnormally(f, f.getException());
1200            }};
1201        testInvokeOnPool(singletonPool(), a);
1202    }
1203
1204    /**
1205     * invoke task throws exception after invoking completeExceptionally
1206     */
1207    public void testCompleteExceptionallySingleton() {
1208        RecursiveAction a = new CheckedRecursiveAction() {
1209            protected void realCompute() {
1210                AsyncFib f = new AsyncFib(8);
1211                f.completeExceptionally(new FJException());
1212                try {
1213                    f.invoke();
1214                    shouldThrow();
1215                } catch (FJException success) {
1216                    checkCompletedAbnormally(f, success);
1217                }
1218            }};
1219        testInvokeOnPool(singletonPool(), a);
1220    }
1221
1222    /**
1223     * invokeAll(t1, t2) invokes all task arguments
1224     */
1225    public void testInvokeAll2Singleton() {
1226        RecursiveAction a = new CheckedRecursiveAction() {
1227            protected void realCompute() {
1228                AsyncFib f = new AsyncFib(8);
1229                AsyncFib g = new AsyncFib(9);
1230                invokeAll(f, g);
1231                assertEquals(21, f.number);
1232                assertEquals(34, g.number);
1233                checkCompletedNormally(f);
1234                checkCompletedNormally(g);
1235            }};
1236        testInvokeOnPool(singletonPool(), a);
1237    }
1238
1239    /**
1240     * invokeAll(tasks) with 1 argument invokes task
1241     */
1242    public void testInvokeAll1Singleton() {
1243        RecursiveAction a = new CheckedRecursiveAction() {
1244            protected void realCompute() {
1245                AsyncFib f = new AsyncFib(8);
1246                invokeAll(f);
1247                checkCompletedNormally(f);
1248                assertEquals(21, f.number);
1249            }};
1250        testInvokeOnPool(singletonPool(), a);
1251    }
1252
1253    /**
1254     * invokeAll(tasks) with > 2 argument invokes tasks
1255     */
1256    public void testInvokeAll3Singleton() {
1257        RecursiveAction a = new CheckedRecursiveAction() {
1258            protected void realCompute() {
1259                AsyncFib f = new AsyncFib(8);
1260                AsyncFib g = new AsyncFib(9);
1261                AsyncFib h = new AsyncFib(7);
1262                invokeAll(f, g, h);
1263                assertEquals(21, f.number);
1264                assertEquals(34, g.number);
1265                assertEquals(13, h.number);
1266                checkCompletedNormally(f);
1267                checkCompletedNormally(g);
1268                checkCompletedNormally(h);
1269            }};
1270        testInvokeOnPool(singletonPool(), a);
1271    }
1272
1273    /**
1274     * invokeAll(collection) invokes all tasks in the collection
1275     */
1276    public void testInvokeAllCollectionSingleton() {
1277        RecursiveAction a = new CheckedRecursiveAction() {
1278            protected void realCompute() {
1279                AsyncFib f = new AsyncFib(8);
1280                AsyncFib g = new AsyncFib(9);
1281                AsyncFib h = new AsyncFib(7);
1282                HashSet set = new HashSet();
1283                set.add(f);
1284                set.add(g);
1285                set.add(h);
1286                invokeAll(set);
1287                assertEquals(21, f.number);
1288                assertEquals(34, g.number);
1289                assertEquals(13, h.number);
1290                checkCompletedNormally(f);
1291                checkCompletedNormally(g);
1292                checkCompletedNormally(h);
1293            }};
1294        testInvokeOnPool(singletonPool(), a);
1295    }
1296
1297    /**
1298     * invokeAll(tasks) with any null task throws NPE
1299     */
1300    public void testInvokeAllNPESingleton() {
1301        RecursiveAction a = new CheckedRecursiveAction() {
1302            protected void realCompute() {
1303                AsyncFib f = new AsyncFib(8);
1304                AsyncFib g = new AsyncFib(9);
1305                AsyncFib h = null;
1306                try {
1307                    invokeAll(f, g, h);
1308                    shouldThrow();
1309                } catch (NullPointerException success) {}
1310            }};
1311        testInvokeOnPool(singletonPool(), a);
1312    }
1313
1314    /**
1315     * invokeAll(t1, t2) throw exception if any task does
1316     */
1317    public void testAbnormalInvokeAll2Singleton() {
1318        RecursiveAction a = new CheckedRecursiveAction() {
1319            protected void realCompute() {
1320                AsyncFib f = new AsyncFib(8);
1321                FailingAsyncFib g = new FailingAsyncFib(9);
1322                try {
1323                    invokeAll(f, g);
1324                    shouldThrow();
1325                } catch (FJException success) {
1326                    checkCompletedAbnormally(g, success);
1327                }
1328            }};
1329        testInvokeOnPool(singletonPool(), a);
1330    }
1331
1332    /**
1333     * invokeAll(tasks) with 1 argument throws exception if task does
1334     */
1335    public void testAbnormalInvokeAll1Singleton() {
1336        RecursiveAction a = new CheckedRecursiveAction() {
1337            protected void realCompute() {
1338                FailingAsyncFib g = new FailingAsyncFib(9);
1339                try {
1340                    invokeAll(g);
1341                    shouldThrow();
1342                } catch (FJException success) {
1343                    checkCompletedAbnormally(g, success);
1344                }
1345            }};
1346        testInvokeOnPool(singletonPool(), a);
1347    }
1348
1349    /**
1350     * invokeAll(tasks) with > 2 argument throws exception if any task does
1351     */
1352    public void testAbnormalInvokeAll3Singleton() {
1353        RecursiveAction a = new CheckedRecursiveAction() {
1354            protected void realCompute() {
1355                AsyncFib f = new AsyncFib(8);
1356                FailingAsyncFib g = new FailingAsyncFib(9);
1357                AsyncFib h = new AsyncFib(7);
1358                try {
1359                    invokeAll(f, g, h);
1360                    shouldThrow();
1361                } catch (FJException success) {
1362                    checkCompletedAbnormally(g, success);
1363                }
1364            }};
1365        testInvokeOnPool(singletonPool(), a);
1366    }
1367
1368    /**
1369     * invokeAll(collection) throws exception if any task does
1370     */
1371    public void testAbnormalInvokeAllCollectionSingleton() {
1372        RecursiveAction a = new CheckedRecursiveAction() {
1373            protected void realCompute() {
1374                FailingAsyncFib f = new FailingAsyncFib(8);
1375                AsyncFib g = new AsyncFib(9);
1376                AsyncFib h = new AsyncFib(7);
1377                HashSet set = new HashSet();
1378                set.add(f);
1379                set.add(g);
1380                set.add(h);
1381                try {
1382                    invokeAll(set);
1383                    shouldThrow();
1384                } catch (FJException success) {
1385                    checkCompletedAbnormally(f, success);
1386                }
1387            }};
1388        testInvokeOnPool(singletonPool(), a);
1389    }
1390
1145      /**
1146       * ForkJoinTask.quietlyComplete returns when task completes
1147       * normally without setting a value. The most recent value
# Line 1409 | 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