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.28 by jsr166, Sat Mar 18 20:42:20 2017 UTC

# Line 3 | Line 3
3   * Expert Group and released to the public domain, as explained at
4   * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6 +
7 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
8 + import static java.util.concurrent.TimeUnit.SECONDS;
9 +
10 + import java.util.Arrays;
11 + import java.util.concurrent.CountDownLatch;
12   import java.util.concurrent.ExecutionException;
7 import java.util.concurrent.CancellationException;
13   import java.util.concurrent.ForkJoinPool;
14   import java.util.concurrent.ForkJoinTask;
15   import java.util.concurrent.ForkJoinWorkerThread;
16   import java.util.concurrent.RecursiveAction;
12 import java.util.concurrent.TimeUnit;
17   import java.util.concurrent.TimeoutException;
18 < import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
19 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
20 < import static java.util.concurrent.TimeUnit.SECONDS;
17 < import java.util.HashSet;
18 < import junit.framework.*;
18 >
19 > import junit.framework.Test;
20 > import junit.framework.TestSuite;
21  
22   public class ForkJoinTask8Test extends JSR166TestCase {
23  
# Line 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 90 | Line 100 | public class ForkJoinTask8Test extends J
100          assertNull(a.getException());
101          assertNull(a.getRawResult());
102          if (a instanceof BinaryAsyncAction)
103 <            assertTrue(((BinaryAsyncAction)a).getForkJoinTaskTag() == INITIAL_STATE);
103 >            assertEquals(INITIAL_STATE,
104 >                         ((BinaryAsyncAction)a).getForkJoinTaskTag());
105  
106          try {
107              a.get(0L, SECONDS);
# Line 111 | Line 122 | public class ForkJoinTask8Test extends J
122          assertNull(a.getException());
123          assertSame(expected, a.getRawResult());
124          if (a instanceof BinaryAsyncAction)
125 <            assertTrue(((BinaryAsyncAction)a).getForkJoinTaskTag() == COMPLETE_STATE);
125 >            assertEquals(COMPLETE_STATE,
126 >                         ((BinaryAsyncAction)a).getForkJoinTaskTag());
127  
128          {
129              Thread.currentThread().interrupt();
130 <            long t0 = System.nanoTime();
130 >            long startTime = System.nanoTime();
131              assertSame(expected, a.join());
132 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
132 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
133              Thread.interrupted();
134          }
135  
136          {
137              Thread.currentThread().interrupt();
138 <            long t0 = System.nanoTime();
138 >            long startTime = System.nanoTime();
139              a.quietlyJoin();        // should be no-op
140 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
140 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
141              Thread.interrupted();
142          }
143  
# Line 161 | Line 173 | public class ForkJoinTask8Test extends J
173          Thread.interrupted();
174  
175          {
176 <            long t0 = System.nanoTime();
176 >            long startTime = System.nanoTime();
177              a.quietlyJoin();        // should be no-op
178 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
178 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
179          }
180  
181          try {
# Line 181 | Line 193 | public class ForkJoinTask8Test extends J
193          } catch (Throwable fail) { threadUnexpectedException(fail); }
194      }
195  
184
196      public static final class FJException extends RuntimeException {
197          FJException() { super(); }
198      }
199  
200      abstract static class BinaryAsyncAction extends ForkJoinTask<Void> {
201  
202 <        private BinaryAsyncAction parent;
202 >        private volatile BinaryAsyncAction parent;
203  
204 <        private BinaryAsyncAction sibling;
204 >        private volatile BinaryAsyncAction sibling;
205  
206          protected BinaryAsyncAction() {
207              setForkJoinTaskTag(INITIAL_STATE);
# Line 206 | Line 217 | public class ForkJoinTask8Test extends J
217          }
218  
219          protected void onComplete(BinaryAsyncAction x, BinaryAsyncAction y) {
220 <            if (this.getForkJoinTaskTag() != COMPLETE_STATE ||
221 <                x.getForkJoinTaskTag() != COMPLETE_STATE ||
220 >            if (this.getForkJoinTaskTag() != COMPLETE_STATE ||
221 >                x.getForkJoinTaskTag() != COMPLETE_STATE ||
222                  y.getForkJoinTaskTag() != COMPLETE_STATE) {
223                  completeThisExceptionally(new FJException());
224              }
# Line 233 | Line 244 | public class ForkJoinTask8Test extends J
244              super.completeExceptionally(ex);
245          }
246  
247 +        public boolean cancel(boolean mayInterruptIfRunning) {
248 +            if (super.cancel(mayInterruptIfRunning)) {
249 +                completeExceptionally(new FJException());
250 +                return true;
251 +            }
252 +            return false;
253 +        }
254 +
255          public final void complete() {
256              BinaryAsyncAction a = this;
257              for (;;) {
# Line 241 | Line 260 | public class ForkJoinTask8Test extends J
260                  a.sibling = null;
261                  a.parent = null;
262                  a.completeThis();
263 <                if (p == null ||
263 >                if (p == null ||
264                      p.compareAndSetForkJoinTaskTag(INITIAL_STATE, COMPLETE_STATE))
265                      break;
266                  try {
# Line 255 | Line 274 | public class ForkJoinTask8Test extends J
274          }
275  
276          public final void completeExceptionally(Throwable ex) {
277 <            BinaryAsyncAction a = this;
259 <            while (!a.isCompletedAbnormally()) {
277 >            for (BinaryAsyncAction a = this;;) {
278                  a.completeThisExceptionally(ex);
279                  BinaryAsyncAction s = a.sibling;
280 <                if (s != null)
281 <                    s.cancel(false);
282 <                if (!a.onException() || (a = a.parent) == null)
280 >                if (s != null && !s.isDone())
281 >                    s.completeExceptionally(ex);
282 >                if ((a = a.parent) == null)
283                      break;
284              }
285          }
# Line 279 | Line 297 | public class ForkJoinTask8Test extends J
297              super.reinitialize();
298          }
299  
282
300      }
301  
302 <    static final class AsyncFib extends BinaryAsyncAction {
302 >    final class AsyncFib extends BinaryAsyncAction {
303          int number;
304 <        public AsyncFib(int n) {
305 <            this.number = n;
304 >        int expectedResult;
305 >        public AsyncFib(int number) {
306 >            this.number = number;
307 >            this.expectedResult = fib[number];
308          }
309  
310          public final boolean exec() {
311              try {
312                  AsyncFib f = this;
313                  int n = f.number;
314 <                if (n > 1) {
315 <                    while (n > 1) {
316 <                        AsyncFib p = f;
317 <                        AsyncFib r = new AsyncFib(n - 2);
318 <                        f = new AsyncFib(--n);
319 <                        p.linkSubtasks(r, f);
301 <                        r.fork();
302 <                    }
303 <                    f.number = n;
314 >                while (n > 1) {
315 >                    AsyncFib p = f;
316 >                    AsyncFib r = new AsyncFib(n - 2);
317 >                    f = new AsyncFib(--n);
318 >                    p.linkSubtasks(r, f);
319 >                    r.fork();
320                  }
321                  f.complete();
322              }
323 <            catch(Throwable ex) {
323 >            catch (Throwable ex) {
324                  compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE);
325              }
326 +            if (getForkJoinTaskTag() == EXCEPTION_STATE)
327 +                throw new FJException();
328              return false;
329          }
330  
# Line 314 | Line 332 | public class ForkJoinTask8Test extends J
332              number = ((AsyncFib)x).number + ((AsyncFib)y).number;
333              super.onComplete(x, y);
334          }
335 +
336 +        public void checkCompletedNormally() {
337 +            assertEquals(expectedResult, number);
338 +            ForkJoinTask8Test.this.checkCompletedNormally(this);
339 +        }
340      }
341  
342      static final class FailingAsyncFib extends BinaryAsyncAction {
# Line 323 | Line 346 | public class ForkJoinTask8Test extends J
346          }
347  
348          public final boolean exec() {
349 <            FailingAsyncFib f = this;
350 <            int n = f.number;
351 <            if (n > 1) {
349 >            try {
350 >                FailingAsyncFib f = this;
351 >                int n = f.number;
352                  while (n > 1) {
353                      FailingAsyncFib p = f;
354                      FailingAsyncFib r = new FailingAsyncFib(n - 2);
# Line 333 | Line 356 | public class ForkJoinTask8Test extends J
356                      p.linkSubtasks(r, f);
357                      r.fork();
358                  }
359 <                f.number = n;
359 >                f.complete();
360              }
361 <            f.complete();
361 >            catch (Throwable ex) {
362 >                compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE);
363 >            }
364 >            if (getForkJoinTaskTag() == EXCEPTION_STATE)
365 >                throw new FJException();
366              return false;
367          }
368  
# Line 350 | Line 377 | public class ForkJoinTask8Test extends J
377       * completed tasks; getRawResult returns null.
378       */
379      public void testInvoke() {
380 +        testInvoke(mainPool());
381 +    }
382 +    public void testInvoke_Singleton() {
383 +        testInvoke(singletonPool());
384 +    }
385 +    public void testInvoke(ForkJoinPool pool) {
386          RecursiveAction a = new CheckedRecursiveAction() {
387              protected void realCompute() {
388                  AsyncFib f = new AsyncFib(8);
389                  assertNull(f.invoke());
390 <                assertEquals(21, f.number);
358 <                checkCompletedNormally(f);
390 >                f.checkCompletedNormally();
391              }};
392 <        testInvokeOnPool(mainPool(), a);
392 >        testInvokeOnPool(pool, a);
393      }
394  
395      /**
# Line 366 | Line 398 | public class ForkJoinTask8Test extends J
398       * completed tasks
399       */
400      public void testQuietlyInvoke() {
401 +        testQuietlyInvoke(mainPool());
402 +    }
403 +    public void testQuietlyInvoke_Singleton() {
404 +        testQuietlyInvoke(singletonPool());
405 +    }
406 +    public void testQuietlyInvoke(ForkJoinPool pool) {
407          RecursiveAction a = new CheckedRecursiveAction() {
408              protected void realCompute() {
409                  AsyncFib f = new AsyncFib(8);
410                  f.quietlyInvoke();
411 <                assertEquals(21, f.number);
374 <                checkCompletedNormally(f);
411 >                f.checkCompletedNormally();
412              }};
413 <        testInvokeOnPool(mainPool(), a);
413 >        testInvokeOnPool(pool, a);
414      }
415  
416      /**
417       * join of a forked task returns when task completes
418       */
419      public void testForkJoin() {
420 +        testForkJoin(mainPool());
421 +    }
422 +    public void testForkJoin_Singleton() {
423 +        testForkJoin(singletonPool());
424 +    }
425 +    public void testForkJoin(ForkJoinPool pool) {
426          RecursiveAction a = new CheckedRecursiveAction() {
427              protected void realCompute() {
428                  AsyncFib f = new AsyncFib(8);
429                  assertSame(f, f.fork());
430                  assertNull(f.join());
431 <                assertEquals(21, f.number);
389 <                checkCompletedNormally(f);
431 >                f.checkCompletedNormally();
432              }};
433 <        testInvokeOnPool(mainPool(), a);
433 >        testInvokeOnPool(pool, a);
434      }
435  
436      /**
437       * get of a forked task returns when task completes
438       */
439      public void testForkGet() {
440 +        testForkGet(mainPool());
441 +    }
442 +    public void testForkGet_Singleton() {
443 +        testForkGet(singletonPool());
444 +    }
445 +    public void testForkGet(ForkJoinPool pool) {
446          RecursiveAction a = new CheckedRecursiveAction() {
447              protected void realCompute() throws Exception {
448                  AsyncFib f = new AsyncFib(8);
449                  assertSame(f, f.fork());
450                  assertNull(f.get());
451 <                assertEquals(21, f.number);
404 <                checkCompletedNormally(f);
451 >                f.checkCompletedNormally();
452              }};
453 <        testInvokeOnPool(mainPool(), a);
453 >        testInvokeOnPool(pool, a);
454      }
455  
456      /**
457       * timed get of a forked task returns when task completes
458       */
459      public void testForkTimedGet() {
460 +        testForkTimedGet(mainPool());
461 +    }
462 +    public void testForkTimedGet_Singleton() {
463 +        testForkTimedGet(singletonPool());
464 +    }
465 +    public void testForkTimedGet(ForkJoinPool pool) {
466          RecursiveAction a = new CheckedRecursiveAction() {
467              protected void realCompute() throws Exception {
468                  AsyncFib f = new AsyncFib(8);
469                  assertSame(f, f.fork());
470                  assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
471 <                assertEquals(21, f.number);
419 <                checkCompletedNormally(f);
471 >                f.checkCompletedNormally();
472              }};
473 <        testInvokeOnPool(mainPool(), a);
473 >        testInvokeOnPool(pool, a);
474      }
475  
476      /**
477 <     * timed get with null time unit throws NPE
477 >     * timed get with null time unit throws NullPointerException
478       */
479 <    public void testForkTimedGetNPE() {
479 >    public void testForkTimedGetNullTimeUnit() {
480 >        testForkTimedGetNullTimeUnit(mainPool());
481 >    }
482 >    public void testForkTimedGetNullTimeUnit_Singleton() {
483 >        testForkTimedGet(singletonPool());
484 >    }
485 >    public void testForkTimedGetNullTimeUnit(ForkJoinPool pool) {
486          RecursiveAction a = new CheckedRecursiveAction() {
487              protected void realCompute() throws Exception {
488                  AsyncFib f = new AsyncFib(8);
# Line 434 | Line 492 | public class ForkJoinTask8Test extends J
492                      shouldThrow();
493                  } catch (NullPointerException success) {}
494              }};
495 <        testInvokeOnPool(mainPool(), a);
495 >        testInvokeOnPool(pool, a);
496      }
497  
498      /**
499       * quietlyJoin of a forked task returns when task completes
500       */
501      public void testForkQuietlyJoin() {
502 +        testForkQuietlyJoin(mainPool());
503 +    }
504 +    public void testForkQuietlyJoin_Singleton() {
505 +        testForkQuietlyJoin(singletonPool());
506 +    }
507 +    public void testForkQuietlyJoin(ForkJoinPool pool) {
508          RecursiveAction a = new CheckedRecursiveAction() {
509              protected void realCompute() {
510                  AsyncFib f = new AsyncFib(8);
511                  assertSame(f, f.fork());
512                  f.quietlyJoin();
513 <                assertEquals(21, f.number);
450 <                checkCompletedNormally(f);
513 >                f.checkCompletedNormally();
514              }};
515 <        testInvokeOnPool(mainPool(), a);
515 >        testInvokeOnPool(pool, a);
516      }
517  
518      /**
# Line 457 | Line 520 | public class ForkJoinTask8Test extends J
520       * getQueuedTaskCount returns 0 when quiescent
521       */
522      public void testForkHelpQuiesce() {
523 +        testForkHelpQuiesce(mainPool());
524 +    }
525 +    public void testForkHelpQuiesce_Singleton() {
526 +        testForkHelpQuiesce(singletonPool());
527 +    }
528 +    public void testForkHelpQuiesce(ForkJoinPool pool) {
529          RecursiveAction a = new CheckedRecursiveAction() {
530              protected void realCompute() {
531                  AsyncFib f = new AsyncFib(8);
532                  assertSame(f, f.fork());
533                  helpQuiesce();
465                assertEquals(21, f.number);
534                  assertEquals(0, getQueuedTaskCount());
535 <                checkCompletedNormally(f);
535 >                f.checkCompletedNormally();
536              }};
537 <        testInvokeOnPool(mainPool(), a);
537 >        testInvokeOnPool(pool, a);
538      }
539  
540      /**
541       * invoke task throws exception when task completes abnormally
542       */
543      public void testAbnormalInvoke() {
544 +        testAbnormalInvoke(mainPool());
545 +    }
546 +    public void testAbnormalInvoke_Singleton() {
547 +        testAbnormalInvoke(singletonPool());
548 +    }
549 +    public void testAbnormalInvoke(ForkJoinPool pool) {
550          RecursiveAction a = new CheckedRecursiveAction() {
551              protected void realCompute() {
552                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 483 | Line 557 | public class ForkJoinTask8Test extends J
557                      checkCompletedAbnormally(f, success);
558                  }
559              }};
560 <        testInvokeOnPool(mainPool(), a);
560 >        testInvokeOnPool(pool, a);
561      }
562  
563      /**
564       * quietlyInvoke task returns when task completes abnormally
565       */
566      public void testAbnormalQuietlyInvoke() {
567 +        testAbnormalQuietlyInvoke(mainPool());
568 +    }
569 +    public void testAbnormalQuietlyInvoke_Singleton() {
570 +        testAbnormalQuietlyInvoke(singletonPool());
571 +    }
572 +    public void testAbnormalQuietlyInvoke(ForkJoinPool pool) {
573          RecursiveAction a = new CheckedRecursiveAction() {
574              protected void realCompute() {
575                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 497 | Line 577 | public class ForkJoinTask8Test extends J
577                  assertTrue(f.getException() instanceof FJException);
578                  checkCompletedAbnormally(f, f.getException());
579              }};
580 <        testInvokeOnPool(mainPool(), a);
580 >        testInvokeOnPool(pool, a);
581      }
582  
583      /**
584       * join of a forked task throws exception when task completes abnormally
585       */
586      public void testAbnormalForkJoin() {
587 +        testAbnormalForkJoin(mainPool());
588 +    }
589 +    public void testAbnormalForkJoin_Singleton() {
590 +        testAbnormalForkJoin(singletonPool());
591 +    }
592 +    public void testAbnormalForkJoin(ForkJoinPool pool) {
593          RecursiveAction a = new CheckedRecursiveAction() {
594              protected void realCompute() {
595                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 515 | Line 601 | public class ForkJoinTask8Test extends J
601                      checkCompletedAbnormally(f, success);
602                  }
603              }};
604 <        testInvokeOnPool(mainPool(), a);
604 >        testInvokeOnPool(pool, a);
605      }
606  
607      /**
608       * get of a forked task throws exception when task completes abnormally
609       */
610      public void testAbnormalForkGet() {
611 +        testAbnormalForkGet(mainPool());
612 +    }
613 +    public void testAbnormalForkGet_Singleton() {
614 +        testAbnormalForkJoin(singletonPool());
615 +    }
616 +    public void testAbnormalForkGet(ForkJoinPool pool) {
617          RecursiveAction a = new CheckedRecursiveAction() {
618              protected void realCompute() throws Exception {
619                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 535 | Line 627 | public class ForkJoinTask8Test extends J
627                      checkCompletedAbnormally(f, cause);
628                  }
629              }};
630 <        testInvokeOnPool(mainPool(), a);
630 >        testInvokeOnPool(pool, a);
631      }
632  
633      /**
634       * timed get of a forked task throws exception when task completes abnormally
635       */
636      public void testAbnormalForkTimedGet() {
637 +        testAbnormalForkTimedGet(mainPool());
638 +    }
639 +    public void testAbnormalForkTimedGet_Singleton() {
640 +        testAbnormalForkTimedGet(singletonPool());
641 +    }
642 +    public void testAbnormalForkTimedGet(ForkJoinPool pool) {
643          RecursiveAction a = new CheckedRecursiveAction() {
644              protected void realCompute() throws Exception {
645                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 555 | Line 653 | public class ForkJoinTask8Test extends J
653                      checkCompletedAbnormally(f, cause);
654                  }
655              }};
656 <        testInvokeOnPool(mainPool(), a);
656 >        testInvokeOnPool(pool, a);
657      }
658  
659      /**
660       * quietlyJoin of a forked task returns when task completes abnormally
661       */
662      public void testAbnormalForkQuietlyJoin() {
663 +        testAbnormalForkQuietlyJoin(mainPool());
664 +    }
665 +    public void testAbnormalForkQuietlyJoin_Singleton() {
666 +        testAbnormalForkQuietlyJoin(singletonPool());
667 +    }
668 +    public void testAbnormalForkQuietlyJoin(ForkJoinPool pool) {
669          RecursiveAction a = new CheckedRecursiveAction() {
670              protected void realCompute() {
671                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 570 | Line 674 | public class ForkJoinTask8Test extends J
674                  assertTrue(f.getException() instanceof FJException);
675                  checkCompletedAbnormally(f, f.getException());
676              }};
677 <        testInvokeOnPool(mainPool(), a);
677 >        testInvokeOnPool(pool, a);
678      }
679  
576
680      /**
681       * getPool of executing task returns its pool
682       */
683      public void testGetPool() {
684 <        final ForkJoinPool mainPool = mainPool();
684 >        testGetPool(mainPool());
685 >    }
686 >    public void testGetPool_Singleton() {
687 >        testGetPool(singletonPool());
688 >    }
689 >    public void testGetPool(ForkJoinPool pool) {
690          RecursiveAction a = new CheckedRecursiveAction() {
691              protected void realCompute() {
692 <                assertSame(mainPool, getPool());
692 >                assertSame(pool, getPool());
693              }};
694 <        testInvokeOnPool(mainPool, a);
694 >        testInvokeOnPool(pool, a);
695      }
696  
697      /**
# Line 601 | Line 709 | public class ForkJoinTask8Test extends J
709       * inForkJoinPool of executing task returns true
710       */
711      public void testInForkJoinPool() {
712 +        testInForkJoinPool(mainPool());
713 +    }
714 +    public void testInForkJoinPool_Singleton() {
715 +        testInForkJoinPool(singletonPool());
716 +    }
717 +    public void testInForkJoinPool(ForkJoinPool pool) {
718          RecursiveAction a = new CheckedRecursiveAction() {
719              protected void realCompute() {
720                  assertTrue(inForkJoinPool());
721              }};
722 <        testInvokeOnPool(mainPool(), a);
722 >        testInvokeOnPool(pool, a);
723      }
724  
725      /**
# Line 635 | Line 749 | public class ForkJoinTask8Test extends J
749       * invoke task throws exception after invoking completeExceptionally
750       */
751      public void testCompleteExceptionally() {
752 +        testCompleteExceptionally(mainPool());
753 +    }
754 +    public void testCompleteExceptionally_Singleton() {
755 +        testCompleteExceptionally(singletonPool());
756 +    }
757 +    public void testCompleteExceptionally(ForkJoinPool pool) {
758          RecursiveAction a = new CheckedRecursiveAction() {
759              protected void realCompute() {
760                  AsyncFib f = new AsyncFib(8);
# Line 646 | Line 766 | public class ForkJoinTask8Test extends J
766                      checkCompletedAbnormally(f, success);
767                  }
768              }};
769 <        testInvokeOnPool(mainPool(), a);
769 >        testInvokeOnPool(pool, a);
770      }
771  
772      /**
773 <     * invokeAll(t1, t2) invokes all task arguments
773 >     * invokeAll(tasks) with 1 argument invokes task
774       */
775 <    public void testInvokeAll2() {
775 >    public void testInvokeAll1() {
776 >        testInvokeAll1(mainPool());
777 >    }
778 >    public void testInvokeAll1_Singleton() {
779 >        testInvokeAll1(singletonPool());
780 >    }
781 >    public void testInvokeAll1(ForkJoinPool pool) {
782          RecursiveAction a = new CheckedRecursiveAction() {
783              protected void realCompute() {
784                  AsyncFib f = new AsyncFib(8);
785 <                AsyncFib g = new AsyncFib(9);
786 <                invokeAll(f, g);
661 <                assertEquals(21, f.number);
662 <                assertEquals(34, g.number);
663 <                checkCompletedNormally(f);
664 <                checkCompletedNormally(g);
785 >                invokeAll(f);
786 >                f.checkCompletedNormally();
787              }};
788 <        testInvokeOnPool(mainPool(), a);
788 >        testInvokeOnPool(pool, a);
789      }
790  
791      /**
792 <     * invokeAll(tasks) with 1 argument invokes task
792 >     * invokeAll(t1, t2) invokes all task arguments
793       */
794 <    public void testInvokeAll1() {
794 >    public void testInvokeAll2() {
795 >        testInvokeAll2(mainPool());
796 >    }
797 >    public void testInvokeAll2_Singleton() {
798 >        testInvokeAll2(singletonPool());
799 >    }
800 >    public void testInvokeAll2(ForkJoinPool pool) {
801          RecursiveAction a = new CheckedRecursiveAction() {
802              protected void realCompute() {
803 <                AsyncFib f = new AsyncFib(8);
804 <                invokeAll(f);
805 <                checkCompletedNormally(f);
806 <                assertEquals(21, f.number);
803 >                AsyncFib[] tasks = {
804 >                    new AsyncFib(8),
805 >                    new AsyncFib(9),
806 >                };
807 >                invokeAll(tasks[0], tasks[1]);
808 >                for (AsyncFib task : tasks) assertTrue(task.isDone());
809 >                for (AsyncFib task : tasks) task.checkCompletedNormally();
810              }};
811 <        testInvokeOnPool(mainPool(), a);
811 >        testInvokeOnPool(pool, a);
812      }
813  
814      /**
815       * invokeAll(tasks) with > 2 argument invokes tasks
816       */
817      public void testInvokeAll3() {
818 +        testInvokeAll3(mainPool());
819 +    }
820 +    public void testInvokeAll3_Singleton() {
821 +        testInvokeAll3(singletonPool());
822 +    }
823 +    public void testInvokeAll3(ForkJoinPool pool) {
824          RecursiveAction a = new CheckedRecursiveAction() {
825              protected void realCompute() {
826 <                AsyncFib f = new AsyncFib(8);
827 <                AsyncFib g = new AsyncFib(9);
828 <                AsyncFib h = new AsyncFib(7);
829 <                invokeAll(f, g, h);
830 <                assertEquals(21, f.number);
831 <                assertEquals(34, g.number);
832 <                assertEquals(13, h.number);
833 <                checkCompletedNormally(f);
697 <                checkCompletedNormally(g);
698 <                checkCompletedNormally(h);
826 >                AsyncFib[] tasks = {
827 >                    new AsyncFib(8),
828 >                    new AsyncFib(9),
829 >                    new AsyncFib(7),
830 >                };
831 >                invokeAll(tasks[0], tasks[1], tasks[2]);
832 >                for (AsyncFib task : tasks) assertTrue(task.isDone());
833 >                for (AsyncFib task : tasks) task.checkCompletedNormally();
834              }};
835 <        testInvokeOnPool(mainPool(), a);
835 >        testInvokeOnPool(pool, a);
836      }
837  
838      /**
839       * invokeAll(collection) invokes all tasks in the collection
840       */
841      public void testInvokeAllCollection() {
842 +        testInvokeAllCollection(mainPool());
843 +    }
844 +    public void testInvokeAllCollection_Singleton() {
845 +        testInvokeAllCollection(singletonPool());
846 +    }
847 +    public void testInvokeAllCollection(ForkJoinPool pool) {
848          RecursiveAction a = new CheckedRecursiveAction() {
849              protected void realCompute() {
850 <                AsyncFib f = new AsyncFib(8);
851 <                AsyncFib g = new AsyncFib(9);
852 <                AsyncFib h = new AsyncFib(7);
853 <                HashSet set = new HashSet();
854 <                set.add(f);
855 <                set.add(g);
856 <                set.add(h);
857 <                invokeAll(set);
717 <                assertEquals(21, f.number);
718 <                assertEquals(34, g.number);
719 <                assertEquals(13, h.number);
720 <                checkCompletedNormally(f);
721 <                checkCompletedNormally(g);
722 <                checkCompletedNormally(h);
850 >                AsyncFib[] tasks = {
851 >                    new AsyncFib(8),
852 >                    new AsyncFib(9),
853 >                    new AsyncFib(7),
854 >                };
855 >                invokeAll(Arrays.asList(tasks));
856 >                for (AsyncFib task : tasks) assertTrue(task.isDone());
857 >                for (AsyncFib task : tasks) task.checkCompletedNormally();
858              }};
859 <        testInvokeOnPool(mainPool(), a);
859 >        testInvokeOnPool(pool, a);
860      }
861  
862      /**
863 <     * invokeAll(tasks) with any null task throws NPE
863 >     * invokeAll(tasks) with any null task throws NullPointerException
864       */
865 <    public void testInvokeAllNPE() {
865 >    public void testInvokeAllNullTask() {
866 >        testInvokeAllNullTask(mainPool());
867 >    }
868 >    public void testInvokeAllNullTask_Singleton() {
869 >        testInvokeAllNullTask(singletonPool());
870 >    }
871 >    public void testInvokeAllNullTask(ForkJoinPool pool) {
872          RecursiveAction a = new CheckedRecursiveAction() {
873              protected void realCompute() {
874 <                AsyncFib f = new AsyncFib(8);
875 <                AsyncFib g = new AsyncFib(9);
876 <                AsyncFib h = null;
877 <                try {
878 <                    invokeAll(f, g, h);
879 <                    shouldThrow();
880 <                } catch (NullPointerException success) {}
874 >                AsyncFib nul = null;
875 >                Runnable[] throwingActions = {
876 >                    () -> invokeAll(nul),
877 >                    () -> invokeAll(nul, nul),
878 >                    () -> invokeAll(new AsyncFib(8), new AsyncFib(9), nul),
879 >                    () -> invokeAll(new AsyncFib(8), nul, new AsyncFib(9)),
880 >                    () -> invokeAll(nul, new AsyncFib(8), new AsyncFib(9)),
881 >                };
882 >                assertThrows(NullPointerException.class, throwingActions);
883              }};
884 <        testInvokeOnPool(mainPool(), a);
884 >        testInvokeOnPool(pool, a);
885      }
886  
887      /**
888 <     * invokeAll(t1, t2) throw exception if any task does
888 >     * invokeAll(tasks) with 1 argument throws exception if task does
889       */
890 <    public void testAbnormalInvokeAll2() {
890 >    public void testAbnormalInvokeAll1() {
891 >        testAbnormalInvokeAll1(mainPool());
892 >    }
893 >    public void testAbnormalInvokeAll1_Singleton() {
894 >        testAbnormalInvokeAll1(singletonPool());
895 >    }
896 >    public void testAbnormalInvokeAll1(ForkJoinPool pool) {
897          RecursiveAction a = new CheckedRecursiveAction() {
898              protected void realCompute() {
750                AsyncFib f = new AsyncFib(8);
899                  FailingAsyncFib g = new FailingAsyncFib(9);
900                  try {
901 <                    invokeAll(f, g);
901 >                    invokeAll(g);
902                      shouldThrow();
903                  } catch (FJException success) {
904                      checkCompletedAbnormally(g, success);
905                  }
906              }};
907 <        testInvokeOnPool(mainPool(), a);
907 >        testInvokeOnPool(pool, a);
908      }
909  
910      /**
911 <     * invokeAll(tasks) with 1 argument throws exception if task does
911 >     * invokeAll(t1, t2) throw exception if any task does
912       */
913 <    public void testAbnormalInvokeAll1() {
913 >    public void testAbnormalInvokeAll2() {
914 >        testAbnormalInvokeAll2(mainPool());
915 >    }
916 >    public void testAbnormalInvokeAll2_Singleton() {
917 >        testAbnormalInvokeAll2(singletonPool());
918 >    }
919 >    public void testAbnormalInvokeAll2(ForkJoinPool pool) {
920          RecursiveAction a = new CheckedRecursiveAction() {
921              protected void realCompute() {
922 +                AsyncFib f = new AsyncFib(8);
923                  FailingAsyncFib g = new FailingAsyncFib(9);
924 +                ForkJoinTask[] tasks = { f, g };
925 +                shuffle(tasks);
926                  try {
927 <                    invokeAll(g);
927 >                    invokeAll(tasks[0], tasks[1]);
928                      shouldThrow();
929                  } catch (FJException success) {
930                      checkCompletedAbnormally(g, success);
931                  }
932              }};
933 <        testInvokeOnPool(mainPool(), a);
933 >        testInvokeOnPool(pool, a);
934      }
935  
936      /**
937       * invokeAll(tasks) with > 2 argument throws exception if any task does
938       */
939      public void testAbnormalInvokeAll3() {
940 +        testAbnormalInvokeAll3(mainPool());
941 +    }
942 +    public void testAbnormalInvokeAll3_Singleton() {
943 +        testAbnormalInvokeAll3(singletonPool());
944 +    }
945 +    public void testAbnormalInvokeAll3(ForkJoinPool pool) {
946          RecursiveAction a = new CheckedRecursiveAction() {
947              protected void realCompute() {
948                  AsyncFib f = new AsyncFib(8);
949                  FailingAsyncFib g = new FailingAsyncFib(9);
950                  AsyncFib h = new AsyncFib(7);
951 +                ForkJoinTask[] tasks = { f, g, h };
952 +                shuffle(tasks);
953                  try {
954 <                    invokeAll(f, g, h);
954 >                    invokeAll(tasks[0], tasks[1], tasks[2]);
955                      shouldThrow();
956                  } catch (FJException success) {
957                      checkCompletedAbnormally(g, success);
958                  }
959              }};
960 <        testInvokeOnPool(mainPool(), a);
960 >        testInvokeOnPool(pool, a);
961      }
962  
963      /**
964 <     * invokeAll(collection)  throws exception if any task does
964 >     * invokeAll(collection) throws exception if any task does
965       */
966      public void testAbnormalInvokeAllCollection() {
967 +        testAbnormalInvokeAllCollection(mainPool());
968 +    }
969 +    public void testAbnormalInvokeAllCollection_Singleton() {
970 +        testAbnormalInvokeAllCollection(singletonPool());
971 +    }
972 +    public void testAbnormalInvokeAllCollection(ForkJoinPool pool) {
973          RecursiveAction a = new CheckedRecursiveAction() {
974              protected void realCompute() {
975                  FailingAsyncFib f = new FailingAsyncFib(8);
976                  AsyncFib g = new AsyncFib(9);
977                  AsyncFib h = new AsyncFib(7);
978 <                HashSet set = new HashSet();
979 <                set.add(f);
809 <                set.add(g);
810 <                set.add(h);
978 >                ForkJoinTask[] tasks = { f, g, h };
979 >                shuffle(tasks);
980                  try {
981 <                    invokeAll(set);
981 >                    invokeAll(Arrays.asList(tasks));
982                      shouldThrow();
983                  } catch (FJException success) {
984                      checkCompletedAbnormally(f, success);
985                  }
986              }};
987 <        testInvokeOnPool(mainPool(), a);
987 >        testInvokeOnPool(pool, a);
988      }
989  
990      /**
# Line 832 | Line 1001 | public class ForkJoinTask8Test extends J
1001                  assertTrue(f.tryUnfork());
1002                  helpQuiesce();
1003                  checkNotDone(f);
1004 <                checkCompletedNormally(g);
1004 >                g.checkCompletedNormally();
1005              }};
1006          testInvokeOnPool(singletonPool(), a);
1007      }
# Line 853 | Line 1022 | public class ForkJoinTask8Test extends J
1022                  assertTrue(getSurplusQueuedTaskCount() > 0);
1023                  helpQuiesce();
1024                  assertEquals(0, getSurplusQueuedTaskCount());
1025 <                checkCompletedNormally(f);
1026 <                checkCompletedNormally(g);
1027 <                checkCompletedNormally(h);
1025 >                f.checkCompletedNormally();
1026 >                g.checkCompletedNormally();
1027 >                h.checkCompletedNormally();
1028              }};
1029          testInvokeOnPool(singletonPool(), a);
1030      }
# Line 872 | Line 1041 | public class ForkJoinTask8Test extends J
1041                  assertSame(f, f.fork());
1042                  assertSame(f, peekNextLocalTask());
1043                  assertNull(f.join());
1044 <                checkCompletedNormally(f);
1044 >                f.checkCompletedNormally();
1045                  helpQuiesce();
1046 <                checkCompletedNormally(g);
1046 >                g.checkCompletedNormally();
1047              }};
1048          testInvokeOnPool(singletonPool(), a);
1049      }
# Line 893 | Line 1062 | public class ForkJoinTask8Test extends J
1062                  assertSame(f, pollNextLocalTask());
1063                  helpQuiesce();
1064                  checkNotDone(f);
1065 <                assertEquals(34, g.number);
897 <                checkCompletedNormally(g);
1065 >                g.checkCompletedNormally();
1066              }};
1067          testInvokeOnPool(singletonPool(), a);
1068      }
# Line 912 | Line 1080 | public class ForkJoinTask8Test extends J
1080                  assertSame(f, pollTask());
1081                  helpQuiesce();
1082                  checkNotDone(f);
1083 <                checkCompletedNormally(g);
1083 >                g.checkCompletedNormally();
1084              }};
1085          testInvokeOnPool(singletonPool(), a);
1086      }
# Line 930 | Line 1098 | public class ForkJoinTask8Test extends J
1098                  assertSame(g, peekNextLocalTask());
1099                  assertNull(f.join());
1100                  helpQuiesce();
1101 <                checkCompletedNormally(f);
1102 <                assertEquals(34, g.number);
935 <                checkCompletedNormally(g);
1101 >                f.checkCompletedNormally();
1102 >                g.checkCompletedNormally();
1103              }};
1104          testInvokeOnPool(asyncSingletonPool(), a);
1105      }
# Line 950 | Line 1117 | public class ForkJoinTask8Test extends J
1117                  assertSame(f, f.fork());
1118                  assertSame(g, pollNextLocalTask());
1119                  helpQuiesce();
1120 <                assertEquals(21, f.number);
954 <                checkCompletedNormally(f);
1120 >                f.checkCompletedNormally();
1121                  checkNotDone(g);
1122              }};
1123          testInvokeOnPool(asyncSingletonPool(), a);
# Line 970 | Line 1136 | public class ForkJoinTask8Test extends J
1136                  assertSame(f, f.fork());
1137                  assertSame(g, pollTask());
1138                  helpQuiesce();
1139 <                assertEquals(21, f.number);
974 <                checkCompletedNormally(f);
1139 >                f.checkCompletedNormally();
1140                  checkNotDone(g);
1141              }};
1142          testInvokeOnPool(asyncSingletonPool(), a);
1143      }
1144  
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
1145      /**
1146       * ForkJoinTask.quietlyComplete returns when task completes
1147       * normally without setting a value. The most recent value
# Line 1417 | Line 1163 | public class ForkJoinTask8Test extends J
1163          testInvokeOnPool(mainPool(), a);
1164      }
1165  
1166 +    // jdk9
1167 +
1168 +    /**
1169 +     * pollSubmission returns unexecuted submitted task, if present
1170 +     */
1171 +    public void testPollSubmission() {
1172 +        final CountDownLatch done = new CountDownLatch(1);
1173 +        final ForkJoinTask a = ForkJoinTask.adapt(awaiter(done));
1174 +        final ForkJoinTask b = ForkJoinTask.adapt(awaiter(done));
1175 +        final ForkJoinTask c = ForkJoinTask.adapt(awaiter(done));
1176 +        final ForkJoinPool p = singletonPool();
1177 +        try (PoolCleaner cleaner = cleaner(p, done)) {
1178 +            Thread external = new Thread(new CheckedRunnable() {
1179 +                public void realRun() {
1180 +                    p.execute(a);
1181 +                    p.execute(b);
1182 +                    p.execute(c);
1183 +                }});
1184 +            RecursiveAction s = new CheckedRecursiveAction() {
1185 +                protected void realCompute() {
1186 +                    external.start();
1187 +                    try {
1188 +                        external.join();
1189 +                    } catch (Exception ex) {
1190 +                        threadUnexpectedException(ex);
1191 +                    }
1192 +                    assertTrue(p.hasQueuedSubmissions());
1193 +                    assertTrue(Thread.currentThread() instanceof ForkJoinWorkerThread);
1194 +                    ForkJoinTask r = ForkJoinTask.pollSubmission();
1195 +                    assertTrue(r == a || r == b || r == c);
1196 +                    assertFalse(r.isDone());
1197 +                }};
1198 +            p.invoke(s);
1199 +        }
1200 +    }
1201 +
1202   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines