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.1 by dl, Sun Jul 21 22:24:18 2013 UTC vs.
Revision 1.27 by jsr166, Wed Aug 24 22:22:39 2016 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines