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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines