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.8 by jsr166, Thu Jan 15 18:34:19 2015 UTC vs.
Revision 1.12 by jsr166, Sat Feb 7 22:32:48 2015 UTC

# Line 7 | Line 7
7   import static java.util.concurrent.TimeUnit.MILLISECONDS;
8   import static java.util.concurrent.TimeUnit.SECONDS;
9  
10 < import java.util.HashSet;
10 > import java.util.Arrays;
11   import java.util.concurrent.ExecutionException;
12   import java.util.concurrent.ForkJoinPool;
13   import java.util.concurrent.ForkJoinTask;
# Line 59 | Line 59 | public class ForkJoinTask8Test extends J
59                                  null, true);
60      }
61  
62 +    // Compute fib naively and efficiently
63 +    final int[] fib;
64 +    {
65 +        int[] fib = new int[10];
66 +        fib[0] = 0;
67 +        fib[1] = 1;
68 +        for (int i = 2; i < fib.length; i++)
69 +            fib[i] = fib[i - 1] + fib[i - 2];
70 +        this.fib = fib;
71 +    }
72 +
73      private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
74          try {
75              assertFalse(a.isDone());
# Line 279 | Line 290 | public class ForkJoinTask8Test extends J
290  
291      }
292  
293 <    static final class AsyncFib extends BinaryAsyncAction {
293 >    final class AsyncFib extends BinaryAsyncAction {
294          int number;
295 <        public AsyncFib(int n) {
296 <            this.number = n;
295 >        int expectedResult;
296 >        public AsyncFib(int number) {
297 >            this.number = number;
298 >            this.expectedResult = fib[number];
299          }
300  
301          public final boolean exec() {
# Line 311 | Line 324 | public class ForkJoinTask8Test extends J
324              number = ((AsyncFib)x).number + ((AsyncFib)y).number;
325              super.onComplete(x, y);
326          }
327 +
328 +        public void checkCompletedNormally() {
329 +            assertEquals(expectedResult, number);
330 +            ForkJoinTask8Test.this.checkCompletedNormally(this);
331 +        }
332      }
333  
334      static final class FailingAsyncFib extends BinaryAsyncAction {
# Line 347 | Line 365 | public class ForkJoinTask8Test extends J
365       * completed tasks; getRawResult returns null.
366       */
367      public void testInvoke() {
368 +        testInvoke(mainPool());
369 +    }
370 +    public void testInvoke_Singleton() {
371 +        testInvoke(singletonPool());
372 +    }
373 +    public void testInvoke(ForkJoinPool pool) {
374          RecursiveAction a = new CheckedRecursiveAction() {
375              protected void realCompute() {
376                  AsyncFib f = new AsyncFib(8);
377                  assertNull(f.invoke());
378 <                assertEquals(21, f.number);
355 <                checkCompletedNormally(f);
378 >                f.checkCompletedNormally();
379              }};
380 <        testInvokeOnPool(mainPool(), a);
380 >        testInvokeOnPool(pool, a);
381      }
382  
383      /**
# Line 363 | Line 386 | public class ForkJoinTask8Test extends J
386       * completed tasks
387       */
388      public void testQuietlyInvoke() {
389 +        testQuietlyInvoke(mainPool());
390 +    }
391 +    public void testQuietlyInvoke_Singleton() {
392 +        testQuietlyInvoke(singletonPool());
393 +    }
394 +    public void testQuietlyInvoke(ForkJoinPool pool) {
395          RecursiveAction a = new CheckedRecursiveAction() {
396              protected void realCompute() {
397                  AsyncFib f = new AsyncFib(8);
398                  f.quietlyInvoke();
399 <                assertEquals(21, f.number);
371 <                checkCompletedNormally(f);
399 >                f.checkCompletedNormally();
400              }};
401 <        testInvokeOnPool(mainPool(), a);
401 >        testInvokeOnPool(pool, a);
402      }
403  
404      /**
405       * join of a forked task returns when task completes
406       */
407      public void testForkJoin() {
408 +        testForkJoin(mainPool());
409 +    }
410 +    public void testForkJoin_Singleton() {
411 +        testForkJoin(singletonPool());
412 +    }
413 +    public void testForkJoin(ForkJoinPool pool) {
414          RecursiveAction a = new CheckedRecursiveAction() {
415              protected void realCompute() {
416                  AsyncFib f = new AsyncFib(8);
417                  assertSame(f, f.fork());
418                  assertNull(f.join());
419 <                assertEquals(21, f.number);
386 <                checkCompletedNormally(f);
419 >                f.checkCompletedNormally();
420              }};
421 <        testInvokeOnPool(mainPool(), a);
421 >        testInvokeOnPool(pool, a);
422      }
423  
424      /**
425       * get of a forked task returns when task completes
426       */
427      public void testForkGet() {
428 +        testForkGet(mainPool());
429 +    }
430 +    public void testForkGet_Singleton() {
431 +        testForkGet(singletonPool());
432 +    }
433 +    public void testForkGet(ForkJoinPool pool) {
434          RecursiveAction a = new CheckedRecursiveAction() {
435              protected void realCompute() throws Exception {
436                  AsyncFib f = new AsyncFib(8);
437                  assertSame(f, f.fork());
438                  assertNull(f.get());
439 <                assertEquals(21, f.number);
401 <                checkCompletedNormally(f);
439 >                f.checkCompletedNormally();
440              }};
441 <        testInvokeOnPool(mainPool(), a);
441 >        testInvokeOnPool(pool, a);
442      }
443  
444      /**
445       * timed get of a forked task returns when task completes
446       */
447      public void testForkTimedGet() {
448 +        testForkTimedGet(mainPool());
449 +    }
450 +    public void testForkTimedGet_Singleton() {
451 +        testForkTimedGet(singletonPool());
452 +    }
453 +    public void testForkTimedGet(ForkJoinPool pool) {
454          RecursiveAction a = new CheckedRecursiveAction() {
455              protected void realCompute() throws Exception {
456                  AsyncFib f = new AsyncFib(8);
457                  assertSame(f, f.fork());
458                  assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
459 <                assertEquals(21, f.number);
416 <                checkCompletedNormally(f);
459 >                f.checkCompletedNormally();
460              }};
461 <        testInvokeOnPool(mainPool(), a);
461 >        testInvokeOnPool(pool, a);
462      }
463  
464      /**
465 <     * timed get with null time unit throws NPE
465 >     * timed get with null time unit throws NullPointerException
466       */
467 <    public void testForkTimedGetNPE() {
467 >    public void testForkTimedGetNullTimeUnit() {
468 >        testForkTimedGetNullTimeUnit(mainPool());
469 >    }
470 >    public void testForkTimedGetNullTimeUnit_Singleton() {
471 >        testForkTimedGet(singletonPool());
472 >    }
473 >    public void testForkTimedGetNullTimeUnit(ForkJoinPool pool) {
474          RecursiveAction a = new CheckedRecursiveAction() {
475              protected void realCompute() throws Exception {
476                  AsyncFib f = new AsyncFib(8);
# Line 431 | Line 480 | public class ForkJoinTask8Test extends J
480                      shouldThrow();
481                  } catch (NullPointerException success) {}
482              }};
483 <        testInvokeOnPool(mainPool(), a);
483 >        testInvokeOnPool(pool, a);
484      }
485  
486      /**
487       * quietlyJoin of a forked task returns when task completes
488       */
489      public void testForkQuietlyJoin() {
490 +        testForkQuietlyJoin(mainPool());
491 +    }
492 +    public void testForkQuietlyJoin_Singleton() {
493 +        testForkQuietlyJoin(singletonPool());
494 +    }
495 +    public void testForkQuietlyJoin(ForkJoinPool pool) {
496          RecursiveAction a = new CheckedRecursiveAction() {
497              protected void realCompute() {
498                  AsyncFib f = new AsyncFib(8);
499                  assertSame(f, f.fork());
500                  f.quietlyJoin();
501 <                assertEquals(21, f.number);
447 <                checkCompletedNormally(f);
501 >                f.checkCompletedNormally();
502              }};
503 <        testInvokeOnPool(mainPool(), a);
503 >        testInvokeOnPool(pool, a);
504      }
505  
506      /**
# Line 454 | Line 508 | public class ForkJoinTask8Test extends J
508       * getQueuedTaskCount returns 0 when quiescent
509       */
510      public void testForkHelpQuiesce() {
511 +        testForkHelpQuiesce(mainPool());
512 +    }
513 +    public void testForkHelpQuiesce_Singleton() {
514 +        testForkHelpQuiesce(singletonPool());
515 +    }
516 +    public void testForkHelpQuiesce(ForkJoinPool pool) {
517          RecursiveAction a = new CheckedRecursiveAction() {
518              protected void realCompute() {
519                  AsyncFib f = new AsyncFib(8);
520                  assertSame(f, f.fork());
521                  helpQuiesce();
462                assertEquals(21, f.number);
522                  assertEquals(0, getQueuedTaskCount());
523 <                checkCompletedNormally(f);
523 >                f.checkCompletedNormally();
524              }};
525 <        testInvokeOnPool(mainPool(), a);
525 >        testInvokeOnPool(pool, a);
526      }
527  
528      /**
529       * invoke task throws exception when task completes abnormally
530       */
531      public void testAbnormalInvoke() {
532 +        testAbnormalInvoke(mainPool());
533 +    }
534 +    public void testAbnormalInvoke_Singleton() {
535 +        testAbnormalInvoke(singletonPool());
536 +    }
537 +    public void testAbnormalInvoke(ForkJoinPool pool) {
538          RecursiveAction a = new CheckedRecursiveAction() {
539              protected void realCompute() {
540                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 480 | Line 545 | public class ForkJoinTask8Test extends J
545                      checkCompletedAbnormally(f, success);
546                  }
547              }};
548 <        testInvokeOnPool(mainPool(), a);
548 >        testInvokeOnPool(pool, a);
549      }
550  
551      /**
552       * quietlyInvoke task returns when task completes abnormally
553       */
554      public void testAbnormalQuietlyInvoke() {
555 +        testAbnormalQuietlyInvoke(mainPool());
556 +    }
557 +    public void testAbnormalQuietlyInvoke_Singleton() {
558 +        testAbnormalQuietlyInvoke(singletonPool());
559 +    }
560 +    public void testAbnormalQuietlyInvoke(ForkJoinPool pool) {
561          RecursiveAction a = new CheckedRecursiveAction() {
562              protected void realCompute() {
563                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 494 | Line 565 | public class ForkJoinTask8Test extends J
565                  assertTrue(f.getException() instanceof FJException);
566                  checkCompletedAbnormally(f, f.getException());
567              }};
568 <        testInvokeOnPool(mainPool(), a);
568 >        testInvokeOnPool(pool, a);
569      }
570  
571      /**
572       * join of a forked task throws exception when task completes abnormally
573       */
574      public void testAbnormalForkJoin() {
575 +        testAbnormalForkJoin(mainPool());
576 +    }
577 +    public void testAbnormalForkJoin_Singleton() {
578 +        testAbnormalForkJoin(singletonPool());
579 +    }
580 +    public void testAbnormalForkJoin(ForkJoinPool pool) {
581          RecursiveAction a = new CheckedRecursiveAction() {
582              protected void realCompute() {
583                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 512 | Line 589 | public class ForkJoinTask8Test extends J
589                      checkCompletedAbnormally(f, success);
590                  }
591              }};
592 <        testInvokeOnPool(mainPool(), a);
592 >        testInvokeOnPool(pool, a);
593      }
594  
595      /**
596       * get of a forked task throws exception when task completes abnormally
597       */
598      public void testAbnormalForkGet() {
599 +        testAbnormalForkGet(mainPool());
600 +    }
601 +    public void testAbnormalForkGet_Singleton() {
602 +        testAbnormalForkJoin(singletonPool());
603 +    }
604 +    public void testAbnormalForkGet(ForkJoinPool pool) {
605          RecursiveAction a = new CheckedRecursiveAction() {
606              protected void realCompute() throws Exception {
607                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 532 | Line 615 | public class ForkJoinTask8Test extends J
615                      checkCompletedAbnormally(f, cause);
616                  }
617              }};
618 <        testInvokeOnPool(mainPool(), a);
618 >        testInvokeOnPool(pool, a);
619      }
620  
621      /**
622       * timed get of a forked task throws exception when task completes abnormally
623       */
624      public void testAbnormalForkTimedGet() {
625 +        testAbnormalForkTimedGet(mainPool());
626 +    }
627 +    public void testAbnormalForkTimedGet_Singleton() {
628 +        testAbnormalForkTimedGet(singletonPool());
629 +    }
630 +    public void testAbnormalForkTimedGet(ForkJoinPool pool) {
631          RecursiveAction a = new CheckedRecursiveAction() {
632              protected void realCompute() throws Exception {
633                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 552 | Line 641 | public class ForkJoinTask8Test extends J
641                      checkCompletedAbnormally(f, cause);
642                  }
643              }};
644 <        testInvokeOnPool(mainPool(), a);
644 >        testInvokeOnPool(pool, a);
645      }
646  
647      /**
648       * quietlyJoin of a forked task returns when task completes abnormally
649       */
650      public void testAbnormalForkQuietlyJoin() {
651 +        testAbnormalForkQuietlyJoin(mainPool());
652 +    }
653 +    public void testAbnormalForkQuietlyJoin_Singleton() {
654 +        testAbnormalForkQuietlyJoin(singletonPool());
655 +    }
656 +    public void testAbnormalForkQuietlyJoin(ForkJoinPool pool) {
657          RecursiveAction a = new CheckedRecursiveAction() {
658              protected void realCompute() {
659                  FailingAsyncFib f = new FailingAsyncFib(8);
# Line 567 | Line 662 | public class ForkJoinTask8Test extends J
662                  assertTrue(f.getException() instanceof FJException);
663                  checkCompletedAbnormally(f, f.getException());
664              }};
665 <        testInvokeOnPool(mainPool(), a);
665 >        testInvokeOnPool(pool, a);
666      }
667  
668      /**
669       * getPool of executing task returns its pool
670       */
671      public void testGetPool() {
672 <        final ForkJoinPool mainPool = mainPool();
672 >        testGetPool(mainPool());
673 >    }
674 >    public void testGetPool_Singleton() {
675 >        testGetPool(singletonPool());
676 >    }
677 >    public void testGetPool(ForkJoinPool pool) {
678          RecursiveAction a = new CheckedRecursiveAction() {
679              protected void realCompute() {
680 <                assertSame(mainPool, getPool());
680 >                assertSame(pool, getPool());
681              }};
682 <        testInvokeOnPool(mainPool, a);
682 >        testInvokeOnPool(pool, a);
683      }
684  
685      /**
# Line 597 | Line 697 | public class ForkJoinTask8Test extends J
697       * inForkJoinPool of executing task returns true
698       */
699      public void testInForkJoinPool() {
700 +        testInForkJoinPool(mainPool());
701 +    }
702 +    public void testInForkJoinPool_Singleton() {
703 +        testInForkJoinPool(singletonPool());
704 +    }
705 +    public void testInForkJoinPool(ForkJoinPool pool) {
706          RecursiveAction a = new CheckedRecursiveAction() {
707              protected void realCompute() {
708                  assertTrue(inForkJoinPool());
709              }};
710 <        testInvokeOnPool(mainPool(), a);
710 >        testInvokeOnPool(pool, a);
711      }
712  
713      /**
# Line 631 | Line 737 | public class ForkJoinTask8Test extends J
737       * invoke task throws exception after invoking completeExceptionally
738       */
739      public void testCompleteExceptionally() {
740 +        testCompleteExceptionally(mainPool());
741 +    }
742 +    public void testCompleteExceptionally_Singleton() {
743 +        testCompleteExceptionally(singletonPool());
744 +    }
745 +    public void testCompleteExceptionally(ForkJoinPool pool) {
746          RecursiveAction a = new CheckedRecursiveAction() {
747              protected void realCompute() {
748                  AsyncFib f = new AsyncFib(8);
# Line 642 | Line 754 | public class ForkJoinTask8Test extends J
754                      checkCompletedAbnormally(f, success);
755                  }
756              }};
757 <        testInvokeOnPool(mainPool(), a);
757 >        testInvokeOnPool(pool, a);
758      }
759  
760      /**
761 <     * invokeAll(t1, t2) invokes all task arguments
761 >     * invokeAll(tasks) with 1 argument invokes task
762       */
763 <    public void testInvokeAll2() {
763 >    public void testInvokeAll1() {
764 >        testInvokeAll1(mainPool());
765 >    }
766 >    public void testInvokeAll1_Singleton() {
767 >        testInvokeAll1(singletonPool());
768 >    }
769 >    public void testInvokeAll1(ForkJoinPool pool) {
770          RecursiveAction a = new CheckedRecursiveAction() {
771              protected void realCompute() {
772                  AsyncFib f = new AsyncFib(8);
773 <                AsyncFib g = new AsyncFib(9);
774 <                invokeAll(f, g);
657 <                assertEquals(21, f.number);
658 <                assertEquals(34, g.number);
659 <                checkCompletedNormally(f);
660 <                checkCompletedNormally(g);
773 >                invokeAll(f);
774 >                f.checkCompletedNormally();
775              }};
776 <        testInvokeOnPool(mainPool(), a);
776 >        testInvokeOnPool(pool, a);
777      }
778  
779      /**
780 <     * invokeAll(tasks) with 1 argument invokes task
780 >     * invokeAll(t1, t2) invokes all task arguments
781       */
782 <    public void testInvokeAll1() {
782 >    public void testInvokeAll2() {
783 >        testInvokeAll2(mainPool());
784 >    }
785 >    public void testInvokeAll2_Singleton() {
786 >        testInvokeAll2(singletonPool());
787 >    }
788 >    public void testInvokeAll2(ForkJoinPool pool) {
789          RecursiveAction a = new CheckedRecursiveAction() {
790              protected void realCompute() {
791 <                AsyncFib f = new AsyncFib(8);
792 <                invokeAll(f);
793 <                checkCompletedNormally(f);
794 <                assertEquals(21, f.number);
791 >                AsyncFib[] tasks = {
792 >                    new AsyncFib(8),
793 >                    new AsyncFib(9),
794 >                };
795 >                invokeAll(tasks[0], tasks[1]);
796 >                for (AsyncFib task : tasks) assertTrue(task.isDone());
797 >                for (AsyncFib task : tasks) task.checkCompletedNormally();
798              }};
799 <        testInvokeOnPool(mainPool(), a);
799 >        testInvokeOnPool(pool, a);
800      }
801  
802      /**
803       * invokeAll(tasks) with > 2 argument invokes tasks
804       */
805      public void testInvokeAll3() {
806 +        testInvokeAll3(mainPool());
807 +    }
808 +    public void testInvokeAll3_Singleton() {
809 +        testInvokeAll3(singletonPool());
810 +    }
811 +    public void testInvokeAll3(ForkJoinPool pool) {
812          RecursiveAction a = new CheckedRecursiveAction() {
813              protected void realCompute() {
814 <                AsyncFib f = new AsyncFib(8);
815 <                AsyncFib g = new AsyncFib(9);
816 <                AsyncFib h = new AsyncFib(7);
817 <                invokeAll(f, g, h);
818 <                assertEquals(21, f.number);
819 <                assertEquals(34, g.number);
820 <                assertEquals(13, h.number);
821 <                checkCompletedNormally(f);
693 <                checkCompletedNormally(g);
694 <                checkCompletedNormally(h);
814 >                AsyncFib[] tasks = {
815 >                    new AsyncFib(8),
816 >                    new AsyncFib(9),
817 >                    new AsyncFib(7),
818 >                };
819 >                invokeAll(tasks[0], tasks[1], tasks[2]);
820 >                for (AsyncFib task : tasks) assertTrue(task.isDone());
821 >                for (AsyncFib task : tasks) task.checkCompletedNormally();
822              }};
823 <        testInvokeOnPool(mainPool(), a);
823 >        testInvokeOnPool(pool, a);
824      }
825  
826      /**
827       * invokeAll(collection) invokes all tasks in the collection
828       */
829      public void testInvokeAllCollection() {
830 +        testInvokeAllCollection(mainPool());
831 +    }
832 +    public void testInvokeAllCollection_Singleton() {
833 +        testInvokeAllCollection(singletonPool());
834 +    }
835 +    public void testInvokeAllCollection(ForkJoinPool pool) {
836          RecursiveAction a = new CheckedRecursiveAction() {
837              protected void realCompute() {
838 <                AsyncFib f = new AsyncFib(8);
839 <                AsyncFib g = new AsyncFib(9);
840 <                AsyncFib h = new AsyncFib(7);
841 <                HashSet set = new HashSet();
842 <                set.add(f);
843 <                set.add(g);
844 <                set.add(h);
845 <                invokeAll(set);
713 <                assertEquals(21, f.number);
714 <                assertEquals(34, g.number);
715 <                assertEquals(13, h.number);
716 <                checkCompletedNormally(f);
717 <                checkCompletedNormally(g);
718 <                checkCompletedNormally(h);
838 >                AsyncFib[] tasks = {
839 >                    new AsyncFib(8),
840 >                    new AsyncFib(9),
841 >                    new AsyncFib(7),
842 >                };
843 >                invokeAll(Arrays.asList(tasks));
844 >                for (AsyncFib task : tasks) assertTrue(task.isDone());
845 >                for (AsyncFib task : tasks) task.checkCompletedNormally();
846              }};
847 <        testInvokeOnPool(mainPool(), a);
847 >        testInvokeOnPool(pool, a);
848      }
849  
850      /**
851 <     * invokeAll(tasks) with any null task throws NPE
851 >     * invokeAll(tasks) with any null task throws NullPointerException
852       */
853 <    public void testInvokeAllNPE() {
853 >    public void testInvokeAllNullTask() {
854 >        testInvokeAllNullTask(mainPool());
855 >    }
856 >    public void testInvokeAllNullTask_Singleton() {
857 >        testInvokeAllNullTask(singletonPool());
858 >    }
859 >    public void testInvokeAllNullTask(ForkJoinPool pool) {
860          RecursiveAction a = new CheckedRecursiveAction() {
861              protected void realCompute() {
862 <                AsyncFib f = new AsyncFib(8);
863 <                AsyncFib g = new AsyncFib(9);
864 <                AsyncFib h = null;
865 <                try {
866 <                    invokeAll(f, g, h);
867 <                    shouldThrow();
868 <                } catch (NullPointerException success) {}
862 >                AsyncFib nul = null;
863 >                Runnable[] throwingActions = {
864 >                    () -> invokeAll(nul),
865 >                    () -> invokeAll(nul, nul),
866 >                    () -> invokeAll(new AsyncFib(8), new AsyncFib(9), nul),
867 >                    () -> invokeAll(new AsyncFib(8), nul, new AsyncFib(9)),
868 >                    () -> invokeAll(nul, new AsyncFib(8), new AsyncFib(9)),
869 >                };
870 >                assertThrows(NullPointerException.class, throwingActions);
871              }};
872 <        testInvokeOnPool(mainPool(), a);
872 >        testInvokeOnPool(pool, a);
873      }
874  
875      /**
876       * invokeAll(t1, t2) throw exception if any task does
877       */
878      public void testAbnormalInvokeAll2() {
879 +        testAbnormalInvokeAll2(mainPool());
880 +    }
881 +    public void testAbnormalInvokeAll2_Singleton() {
882 +        testAbnormalInvokeAll2(singletonPool());
883 +    }
884 +    public void testAbnormalInvokeAll2(ForkJoinPool pool) {
885          RecursiveAction a = new CheckedRecursiveAction() {
886              protected void realCompute() {
887                  AsyncFib f = new AsyncFib(8);
# Line 752 | Line 893 | public class ForkJoinTask8Test extends J
893                      checkCompletedAbnormally(g, success);
894                  }
895              }};
896 <        testInvokeOnPool(mainPool(), a);
896 >        testInvokeOnPool(pool, a);
897      }
898  
899      /**
900       * invokeAll(tasks) with 1 argument throws exception if task does
901       */
902      public void testAbnormalInvokeAll1() {
903 +        testAbnormalInvokeAll1(mainPool());
904 +    }
905 +    public void testAbnormalInvokeAll1_Singleton() {
906 +        testAbnormalInvokeAll1(singletonPool());
907 +    }
908 +    public void testAbnormalInvokeAll1(ForkJoinPool pool) {
909          RecursiveAction a = new CheckedRecursiveAction() {
910              protected void realCompute() {
911                  FailingAsyncFib g = new FailingAsyncFib(9);
# Line 769 | Line 916 | public class ForkJoinTask8Test extends J
916                      checkCompletedAbnormally(g, success);
917                  }
918              }};
919 <        testInvokeOnPool(mainPool(), a);
919 >        testInvokeOnPool(pool, a);
920      }
921  
922      /**
923       * invokeAll(tasks) with > 2 argument throws exception if any task does
924       */
925      public void testAbnormalInvokeAll3() {
926 +        testAbnormalInvokeAll3(mainPool());
927 +    }
928 +    public void testAbnormalInvokeAll3_Singleton() {
929 +        testAbnormalInvokeAll3(singletonPool());
930 +    }
931 +    public void testAbnormalInvokeAll3(ForkJoinPool pool) {
932          RecursiveAction a = new CheckedRecursiveAction() {
933              protected void realCompute() {
934                  AsyncFib f = new AsyncFib(8);
# Line 788 | Line 941 | public class ForkJoinTask8Test extends J
941                      checkCompletedAbnormally(g, success);
942                  }
943              }};
944 <        testInvokeOnPool(mainPool(), a);
944 >        testInvokeOnPool(pool, a);
945      }
946  
947      /**
948       * invokeAll(collection) throws exception if any task does
949       */
950      public void testAbnormalInvokeAllCollection() {
951 +        testAbnormalInvokeAllCollection(mainPool());
952 +    }
953 +    public void testAbnormalInvokeAllCollection_Singleton() {
954 +        testAbnormalInvokeAllCollection(singletonPool());
955 +    }
956 +    public void testAbnormalInvokeAllCollection(ForkJoinPool pool) {
957          RecursiveAction a = new CheckedRecursiveAction() {
958              protected void realCompute() {
959                  FailingAsyncFib f = new FailingAsyncFib(8);
960                  AsyncFib g = new AsyncFib(9);
961                  AsyncFib h = new AsyncFib(7);
962 <                HashSet set = new HashSet();
804 <                set.add(f);
805 <                set.add(g);
806 <                set.add(h);
962 >                ForkJoinTask[] tasks = { f, g, h };
963                  try {
964 <                    invokeAll(set);
964 >                    invokeAll(Arrays.asList(tasks));
965                      shouldThrow();
966                  } catch (FJException success) {
967                      checkCompletedAbnormally(f, success);
968                  }
969              }};
970 <        testInvokeOnPool(mainPool(), a);
970 >        testInvokeOnPool(pool, a);
971      }
972  
973      /**
# Line 828 | Line 984 | public class ForkJoinTask8Test extends J
984                  assertTrue(f.tryUnfork());
985                  helpQuiesce();
986                  checkNotDone(f);
987 <                checkCompletedNormally(g);
987 >                g.checkCompletedNormally();
988              }};
989          testInvokeOnPool(singletonPool(), a);
990      }
# Line 849 | Line 1005 | public class ForkJoinTask8Test extends J
1005                  assertTrue(getSurplusQueuedTaskCount() > 0);
1006                  helpQuiesce();
1007                  assertEquals(0, getSurplusQueuedTaskCount());
1008 <                checkCompletedNormally(f);
1009 <                checkCompletedNormally(g);
1010 <                checkCompletedNormally(h);
1008 >                f.checkCompletedNormally();
1009 >                g.checkCompletedNormally();
1010 >                h.checkCompletedNormally();
1011              }};
1012          testInvokeOnPool(singletonPool(), a);
1013      }
# Line 868 | Line 1024 | public class ForkJoinTask8Test extends J
1024                  assertSame(f, f.fork());
1025                  assertSame(f, peekNextLocalTask());
1026                  assertNull(f.join());
1027 <                checkCompletedNormally(f);
1027 >                f.checkCompletedNormally();
1028                  helpQuiesce();
1029 <                checkCompletedNormally(g);
1029 >                g.checkCompletedNormally();
1030              }};
1031          testInvokeOnPool(singletonPool(), a);
1032      }
# Line 889 | Line 1045 | public class ForkJoinTask8Test extends J
1045                  assertSame(f, pollNextLocalTask());
1046                  helpQuiesce();
1047                  checkNotDone(f);
1048 <                assertEquals(34, g.number);
893 <                checkCompletedNormally(g);
1048 >                g.checkCompletedNormally();
1049              }};
1050          testInvokeOnPool(singletonPool(), a);
1051      }
# Line 908 | Line 1063 | public class ForkJoinTask8Test extends J
1063                  assertSame(f, pollTask());
1064                  helpQuiesce();
1065                  checkNotDone(f);
1066 <                checkCompletedNormally(g);
1066 >                g.checkCompletedNormally();
1067              }};
1068          testInvokeOnPool(singletonPool(), a);
1069      }
# Line 926 | Line 1081 | public class ForkJoinTask8Test extends J
1081                  assertSame(g, peekNextLocalTask());
1082                  assertNull(f.join());
1083                  helpQuiesce();
1084 <                checkCompletedNormally(f);
1085 <                assertEquals(34, g.number);
931 <                checkCompletedNormally(g);
1084 >                f.checkCompletedNormally();
1085 >                g.checkCompletedNormally();
1086              }};
1087          testInvokeOnPool(asyncSingletonPool(), a);
1088      }
# Line 946 | Line 1100 | public class ForkJoinTask8Test extends J
1100                  assertSame(f, f.fork());
1101                  assertSame(g, pollNextLocalTask());
1102                  helpQuiesce();
1103 <                assertEquals(21, f.number);
950 <                checkCompletedNormally(f);
1103 >                f.checkCompletedNormally();
1104                  checkNotDone(g);
1105              }};
1106          testInvokeOnPool(asyncSingletonPool(), a);
# Line 966 | Line 1119 | public class ForkJoinTask8Test extends J
1119                  assertSame(f, f.fork());
1120                  assertSame(g, pollTask());
1121                  helpQuiesce();
1122 <                assertEquals(21, f.number);
970 <                checkCompletedNormally(f);
1122 >                f.checkCompletedNormally();
1123                  checkNotDone(g);
1124              }};
1125          testInvokeOnPool(asyncSingletonPool(), a);
1126      }
1127  
976    // versions for singleton pools
977
978    /**
979     * invoke returns when task completes normally.
980     * isCompletedAbnormally and isCancelled return false for normally
981     * completed tasks; getRawResult returns null.
982     */
983    public void testInvokeSingleton() {
984        RecursiveAction a = new CheckedRecursiveAction() {
985            protected void realCompute() {
986                AsyncFib f = new AsyncFib(8);
987                assertNull(f.invoke());
988                assertEquals(21, f.number);
989                checkCompletedNormally(f);
990            }};
991        testInvokeOnPool(singletonPool(), a);
992    }
993
994    /**
995     * quietlyInvoke task returns when task completes normally.
996     * isCompletedAbnormally and isCancelled return false for normally
997     * completed tasks
998     */
999    public void testQuietlyInvokeSingleton() {
1000        RecursiveAction a = new CheckedRecursiveAction() {
1001            protected void realCompute() {
1002                AsyncFib f = new AsyncFib(8);
1003                f.quietlyInvoke();
1004                assertEquals(21, f.number);
1005                checkCompletedNormally(f);
1006            }};
1007        testInvokeOnPool(singletonPool(), a);
1008    }
1009
1010    /**
1011     * join of a forked task returns when task completes
1012     */
1013    public void testForkJoinSingleton() {
1014        RecursiveAction a = new CheckedRecursiveAction() {
1015            protected void realCompute() {
1016                AsyncFib f = new AsyncFib(8);
1017                assertSame(f, f.fork());
1018                assertNull(f.join());
1019                assertEquals(21, f.number);
1020                checkCompletedNormally(f);
1021            }};
1022        testInvokeOnPool(singletonPool(), a);
1023    }
1024
1025    /**
1026     * get of a forked task returns when task completes
1027     */
1028    public void testForkGetSingleton() {
1029        RecursiveAction a = new CheckedRecursiveAction() {
1030            protected void realCompute() throws Exception {
1031                AsyncFib f = new AsyncFib(8);
1032                assertSame(f, f.fork());
1033                assertNull(f.get());
1034                assertEquals(21, f.number);
1035                checkCompletedNormally(f);
1036            }};
1037        testInvokeOnPool(singletonPool(), a);
1038    }
1039
1040    /**
1041     * timed get of a forked task returns when task completes
1042     */
1043    public void testForkTimedGetSingleton() {
1044        RecursiveAction a = new CheckedRecursiveAction() {
1045            protected void realCompute() throws Exception {
1046                AsyncFib f = new AsyncFib(8);
1047                assertSame(f, f.fork());
1048                assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
1049                assertEquals(21, f.number);
1050                checkCompletedNormally(f);
1051            }};
1052        testInvokeOnPool(singletonPool(), a);
1053    }
1054
1055    /**
1056     * timed get with null time unit throws NPE
1057     */
1058    public void testForkTimedGetNPESingleton() {
1059        RecursiveAction a = new CheckedRecursiveAction() {
1060            protected void realCompute() throws Exception {
1061                AsyncFib f = new AsyncFib(8);
1062                assertSame(f, f.fork());
1063                try {
1064                    f.get(5L, null);
1065                    shouldThrow();
1066                } catch (NullPointerException success) {}
1067            }};
1068        testInvokeOnPool(singletonPool(), a);
1069    }
1070
1071    /**
1072     * quietlyJoin of a forked task returns when task completes
1073     */
1074    public void testForkQuietlyJoinSingleton() {
1075        RecursiveAction a = new CheckedRecursiveAction() {
1076            protected void realCompute() {
1077                AsyncFib f = new AsyncFib(8);
1078                assertSame(f, f.fork());
1079                f.quietlyJoin();
1080                assertEquals(21, f.number);
1081                checkCompletedNormally(f);
1082            }};
1083        testInvokeOnPool(singletonPool(), a);
1084    }
1085
1086    /**
1087     * helpQuiesce returns when tasks are complete.
1088     * getQueuedTaskCount returns 0 when quiescent
1089     */
1090    public void testForkHelpQuiesceSingleton() {
1091        RecursiveAction a = new CheckedRecursiveAction() {
1092            protected void realCompute() {
1093                AsyncFib f = new AsyncFib(8);
1094                assertSame(f, f.fork());
1095                helpQuiesce();
1096                assertEquals(0, getQueuedTaskCount());
1097                assertEquals(21, f.number);
1098                checkCompletedNormally(f);
1099            }};
1100        testInvokeOnPool(singletonPool(), a);
1101    }
1102
1103    /**
1104     * invoke task throws exception when task completes abnormally
1105     */
1106    public void testAbnormalInvokeSingleton() {
1107        RecursiveAction a = new CheckedRecursiveAction() {
1108            protected void realCompute() {
1109                FailingAsyncFib f = new FailingAsyncFib(8);
1110                try {
1111                    f.invoke();
1112                    shouldThrow();
1113                } catch (FJException success) {
1114                    checkCompletedAbnormally(f, success);
1115                }
1116            }};
1117        testInvokeOnPool(singletonPool(), a);
1118    }
1119
1120    /**
1121     * quietlyInvoke task returns when task completes abnormally
1122     */
1123    public void testAbnormalQuietlyInvokeSingleton() {
1124        RecursiveAction a = new CheckedRecursiveAction() {
1125            protected void realCompute() {
1126                FailingAsyncFib f = new FailingAsyncFib(8);
1127                f.quietlyInvoke();
1128                assertTrue(f.getException() instanceof FJException);
1129                checkCompletedAbnormally(f, f.getException());
1130            }};
1131        testInvokeOnPool(singletonPool(), a);
1132    }
1133
1134    /**
1135     * join of a forked task throws exception when task completes abnormally
1136     */
1137    public void testAbnormalForkJoinSingleton() {
1138        RecursiveAction a = new CheckedRecursiveAction() {
1139            protected void realCompute() {
1140                FailingAsyncFib f = new FailingAsyncFib(8);
1141                assertSame(f, f.fork());
1142                try {
1143                    f.join();
1144                    shouldThrow();
1145                } catch (FJException success) {
1146                    checkCompletedAbnormally(f, success);
1147                }
1148            }};
1149        testInvokeOnPool(singletonPool(), a);
1150    }
1151
1152    /**
1153     * get of a forked task throws exception when task completes abnormally
1154     */
1155    public void testAbnormalForkGetSingleton() {
1156        RecursiveAction a = new CheckedRecursiveAction() {
1157            protected void realCompute() throws Exception {
1158                FailingAsyncFib f = new FailingAsyncFib(8);
1159                assertSame(f, f.fork());
1160                try {
1161                    f.get();
1162                    shouldThrow();
1163                } catch (ExecutionException success) {
1164                    Throwable cause = success.getCause();
1165                    assertTrue(cause instanceof FJException);
1166                    checkCompletedAbnormally(f, cause);
1167                }
1168            }};
1169        testInvokeOnPool(singletonPool(), a);
1170    }
1171
1172    /**
1173     * timed get of a forked task throws exception when task completes abnormally
1174     */
1175    public void testAbnormalForkTimedGetSingleton() {
1176        RecursiveAction a = new CheckedRecursiveAction() {
1177            protected void realCompute() throws Exception {
1178                FailingAsyncFib f = new FailingAsyncFib(8);
1179                assertSame(f, f.fork());
1180                try {
1181                    f.get(LONG_DELAY_MS, MILLISECONDS);
1182                    shouldThrow();
1183                } catch (ExecutionException success) {
1184                    Throwable cause = success.getCause();
1185                    assertTrue(cause instanceof FJException);
1186                    checkCompletedAbnormally(f, cause);
1187                }
1188            }};
1189        testInvokeOnPool(singletonPool(), a);
1190    }
1191
1192    /**
1193     * quietlyJoin of a forked task returns when task completes abnormally
1194     */
1195    public void testAbnormalForkQuietlyJoinSingleton() {
1196        RecursiveAction a = new CheckedRecursiveAction() {
1197            protected void realCompute() {
1198                FailingAsyncFib f = new FailingAsyncFib(8);
1199                assertSame(f, f.fork());
1200                f.quietlyJoin();
1201                assertTrue(f.getException() instanceof FJException);
1202                checkCompletedAbnormally(f, f.getException());
1203            }};
1204        testInvokeOnPool(singletonPool(), a);
1205    }
1206
1207    /**
1208     * invoke task throws exception after invoking completeExceptionally
1209     */
1210    public void testCompleteExceptionallySingleton() {
1211        RecursiveAction a = new CheckedRecursiveAction() {
1212            protected void realCompute() {
1213                AsyncFib f = new AsyncFib(8);
1214                f.completeExceptionally(new FJException());
1215                try {
1216                    f.invoke();
1217                    shouldThrow();
1218                } catch (FJException success) {
1219                    checkCompletedAbnormally(f, success);
1220                }
1221            }};
1222        testInvokeOnPool(singletonPool(), a);
1223    }
1224
1225    /**
1226     * invokeAll(t1, t2) invokes all task arguments
1227     */
1228    public void testInvokeAll2Singleton() {
1229        RecursiveAction a = new CheckedRecursiveAction() {
1230            protected void realCompute() {
1231                AsyncFib f = new AsyncFib(8);
1232                AsyncFib g = new AsyncFib(9);
1233                invokeAll(f, g);
1234                assertEquals(21, f.number);
1235                assertEquals(34, g.number);
1236                checkCompletedNormally(f);
1237                checkCompletedNormally(g);
1238            }};
1239        testInvokeOnPool(singletonPool(), a);
1240    }
1241
1242    /**
1243     * invokeAll(tasks) with 1 argument invokes task
1244     */
1245    public void testInvokeAll1Singleton() {
1246        RecursiveAction a = new CheckedRecursiveAction() {
1247            protected void realCompute() {
1248                AsyncFib f = new AsyncFib(8);
1249                invokeAll(f);
1250                checkCompletedNormally(f);
1251                assertEquals(21, f.number);
1252            }};
1253        testInvokeOnPool(singletonPool(), a);
1254    }
1255
1256    /**
1257     * invokeAll(tasks) with > 2 argument invokes tasks
1258     */
1259    public void testInvokeAll3Singleton() {
1260        RecursiveAction a = new CheckedRecursiveAction() {
1261            protected void realCompute() {
1262                AsyncFib f = new AsyncFib(8);
1263                AsyncFib g = new AsyncFib(9);
1264                AsyncFib h = new AsyncFib(7);
1265                invokeAll(f, g, h);
1266                assertEquals(21, f.number);
1267                assertEquals(34, g.number);
1268                assertEquals(13, h.number);
1269                checkCompletedNormally(f);
1270                checkCompletedNormally(g);
1271                checkCompletedNormally(h);
1272            }};
1273        testInvokeOnPool(singletonPool(), a);
1274    }
1275
1276    /**
1277     * invokeAll(collection) invokes all tasks in the collection
1278     */
1279    public void testInvokeAllCollectionSingleton() {
1280        RecursiveAction a = new CheckedRecursiveAction() {
1281            protected void realCompute() {
1282                AsyncFib f = new AsyncFib(8);
1283                AsyncFib g = new AsyncFib(9);
1284                AsyncFib h = new AsyncFib(7);
1285                HashSet set = new HashSet();
1286                set.add(f);
1287                set.add(g);
1288                set.add(h);
1289                invokeAll(set);
1290                assertEquals(21, f.number);
1291                assertEquals(34, g.number);
1292                assertEquals(13, h.number);
1293                checkCompletedNormally(f);
1294                checkCompletedNormally(g);
1295                checkCompletedNormally(h);
1296            }};
1297        testInvokeOnPool(singletonPool(), a);
1298    }
1299
1300    /**
1301     * invokeAll(tasks) with any null task throws NPE
1302     */
1303    public void testInvokeAllNPESingleton() {
1304        RecursiveAction a = new CheckedRecursiveAction() {
1305            protected void realCompute() {
1306                AsyncFib f = new AsyncFib(8);
1307                AsyncFib g = new AsyncFib(9);
1308                AsyncFib h = null;
1309                try {
1310                    invokeAll(f, g, h);
1311                    shouldThrow();
1312                } catch (NullPointerException success) {}
1313            }};
1314        testInvokeOnPool(singletonPool(), a);
1315    }
1316
1317    /**
1318     * invokeAll(t1, t2) throw exception if any task does
1319     */
1320    public void testAbnormalInvokeAll2Singleton() {
1321        RecursiveAction a = new CheckedRecursiveAction() {
1322            protected void realCompute() {
1323                AsyncFib f = new AsyncFib(8);
1324                FailingAsyncFib g = new FailingAsyncFib(9);
1325                try {
1326                    invokeAll(f, g);
1327                    shouldThrow();
1328                } catch (FJException success) {
1329                    checkCompletedAbnormally(g, success);
1330                }
1331            }};
1332        testInvokeOnPool(singletonPool(), a);
1333    }
1334
1335    /**
1336     * invokeAll(tasks) with 1 argument throws exception if task does
1337     */
1338    public void testAbnormalInvokeAll1Singleton() {
1339        RecursiveAction a = new CheckedRecursiveAction() {
1340            protected void realCompute() {
1341                FailingAsyncFib g = new FailingAsyncFib(9);
1342                try {
1343                    invokeAll(g);
1344                    shouldThrow();
1345                } catch (FJException success) {
1346                    checkCompletedAbnormally(g, success);
1347                }
1348            }};
1349        testInvokeOnPool(singletonPool(), a);
1350    }
1351
1352    /**
1353     * invokeAll(tasks) with > 2 argument throws exception if any task does
1354     */
1355    public void testAbnormalInvokeAll3Singleton() {
1356        RecursiveAction a = new CheckedRecursiveAction() {
1357            protected void realCompute() {
1358                AsyncFib f = new AsyncFib(8);
1359                FailingAsyncFib g = new FailingAsyncFib(9);
1360                AsyncFib h = new AsyncFib(7);
1361                try {
1362                    invokeAll(f, g, h);
1363                    shouldThrow();
1364                } catch (FJException success) {
1365                    checkCompletedAbnormally(g, success);
1366                }
1367            }};
1368        testInvokeOnPool(singletonPool(), a);
1369    }
1370
1371    /**
1372     * invokeAll(collection) throws exception if any task does
1373     */
1374    public void testAbnormalInvokeAllCollectionSingleton() {
1375        RecursiveAction a = new CheckedRecursiveAction() {
1376            protected void realCompute() {
1377                FailingAsyncFib f = new FailingAsyncFib(8);
1378                AsyncFib g = new AsyncFib(9);
1379                AsyncFib h = new AsyncFib(7);
1380                HashSet set = new HashSet();
1381                set.add(f);
1382                set.add(g);
1383                set.add(h);
1384                try {
1385                    invokeAll(set);
1386                    shouldThrow();
1387                } catch (FJException success) {
1388                    checkCompletedAbnormally(f, success);
1389                }
1390            }};
1391        testInvokeOnPool(singletonPool(), a);
1392    }
1393
1128      /**
1129       * ForkJoinTask.quietlyComplete returns when task completes
1130       * normally without setting a value. The most recent value

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines