ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinTask8Test.java
(Generate patch)

Comparing jsr166/src/test/tck/ForkJoinTask8Test.java (file contents):
Revision 1.6 by jsr166, Wed Dec 31 17:00:58 2014 UTC vs.
Revision 1.12 by jsr166, Sat Feb 7 22:32:48 2015 UTC

# Line 3 | Line 3
3   * Expert Group and released to the public domain, as explained at
4   * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6 < import java.util.HashSet;
6 >
7 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
8 > import static java.util.concurrent.TimeUnit.SECONDS;
9 >
10 > import java.util.Arrays;
11   import java.util.concurrent.ExecutionException;
12   import java.util.concurrent.ForkJoinPool;
13   import java.util.concurrent.ForkJoinTask;
14   import java.util.concurrent.RecursiveAction;
15   import java.util.concurrent.TimeoutException;
16 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
17 < import static java.util.concurrent.TimeUnit.SECONDS;
18 < import junit.framework.*;
16 >
17 > import junit.framework.Test;
18 > import junit.framework.TestSuite;
19  
20   public class ForkJoinTask8Test extends JSR166TestCase {
21  
# Line 55 | 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 176 | Line 191 | public class ForkJoinTask8Test extends J
191          } catch (Throwable fail) { threadUnexpectedException(fail); }
192      }
193  
179
194      public static final class FJException extends RuntimeException {
195          FJException() { super(); }
196      }
# Line 276 | 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 308 | 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 344 | 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);
352 <                checkCompletedNormally(f);
378 >                f.checkCompletedNormally();
379              }};
380 <        testInvokeOnPool(mainPool(), a);
380 >        testInvokeOnPool(pool, a);
381      }
382  
383      /**
# Line 360 | 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);
368 <                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);
383 <                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);
398 <                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);
413 <                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 428 | 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);
444 <                checkCompletedNormally(f);
501 >                f.checkCompletedNormally();
502              }};
503 <        testInvokeOnPool(mainPool(), a);
503 >        testInvokeOnPool(pool, a);
504      }
505  
506      /**
# Line 451 | 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();
459                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 477 | 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 491 | 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 509 | 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 529 | 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 549 | 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 564 | 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 594 | 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 628 | 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 639 | 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);
654 <                assertEquals(21, f.number);
655 <                assertEquals(34, g.number);
656 <                checkCompletedNormally(f);
657 <                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);
690 <                checkCompletedNormally(g);
691 <                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);
710 <                assertEquals(21, f.number);
711 <                assertEquals(34, g.number);
712 <                assertEquals(13, h.number);
713 <                checkCompletedNormally(f);
714 <                checkCompletedNormally(g);
715 <                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 749 | 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 766 | 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 785 | 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();
801 <                set.add(f);
802 <                set.add(g);
803 <                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 825 | 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 846 | 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 865 | 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 886 | Line 1045 | public class ForkJoinTask8Test extends J
1045                  assertSame(f, pollNextLocalTask());
1046                  helpQuiesce();
1047                  checkNotDone(f);
1048 <                assertEquals(34, g.number);
890 <                checkCompletedNormally(g);
1048 >                g.checkCompletedNormally();
1049              }};
1050          testInvokeOnPool(singletonPool(), a);
1051      }
# Line 905 | 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 923 | 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);
928 <                checkCompletedNormally(g);
1084 >                f.checkCompletedNormally();
1085 >                g.checkCompletedNormally();
1086              }};
1087          testInvokeOnPool(asyncSingletonPool(), a);
1088      }
# Line 943 | Line 1100 | public class ForkJoinTask8Test extends J
1100                  assertSame(f, f.fork());
1101                  assertSame(g, pollNextLocalTask());
1102                  helpQuiesce();
1103 <                assertEquals(21, f.number);
947 <                checkCompletedNormally(f);
1103 >                f.checkCompletedNormally();
1104                  checkNotDone(g);
1105              }};
1106          testInvokeOnPool(asyncSingletonPool(), a);
# Line 963 | Line 1119 | public class ForkJoinTask8Test extends J
1119                  assertSame(f, f.fork());
1120                  assertSame(g, pollTask());
1121                  helpQuiesce();
1122 <                assertEquals(21, f.number);
967 <                checkCompletedNormally(f);
1122 >                f.checkCompletedNormally();
1123                  checkNotDone(g);
1124              }};
1125          testInvokeOnPool(asyncSingletonPool(), a);
1126      }
1127  
973    // versions for singleton pools
974
975    /**
976     * invoke returns when task completes normally.
977     * isCompletedAbnormally and isCancelled return false for normally
978     * completed tasks; getRawResult returns null.
979     */
980    public void testInvokeSingleton() {
981        RecursiveAction a = new CheckedRecursiveAction() {
982            protected void realCompute() {
983                AsyncFib f = new AsyncFib(8);
984                assertNull(f.invoke());
985                assertEquals(21, f.number);
986                checkCompletedNormally(f);
987            }};
988        testInvokeOnPool(singletonPool(), a);
989    }
990
991    /**
992     * quietlyInvoke task returns when task completes normally.
993     * isCompletedAbnormally and isCancelled return false for normally
994     * completed tasks
995     */
996    public void testQuietlyInvokeSingleton() {
997        RecursiveAction a = new CheckedRecursiveAction() {
998            protected void realCompute() {
999                AsyncFib f = new AsyncFib(8);
1000                f.quietlyInvoke();
1001                assertEquals(21, f.number);
1002                checkCompletedNormally(f);
1003            }};
1004        testInvokeOnPool(singletonPool(), a);
1005    }
1006
1007    /**
1008     * join of a forked task returns when task completes
1009     */
1010    public void testForkJoinSingleton() {
1011        RecursiveAction a = new CheckedRecursiveAction() {
1012            protected void realCompute() {
1013                AsyncFib f = new AsyncFib(8);
1014                assertSame(f, f.fork());
1015                assertNull(f.join());
1016                assertEquals(21, f.number);
1017                checkCompletedNormally(f);
1018            }};
1019        testInvokeOnPool(singletonPool(), a);
1020    }
1021
1022    /**
1023     * get of a forked task returns when task completes
1024     */
1025    public void testForkGetSingleton() {
1026        RecursiveAction a = new CheckedRecursiveAction() {
1027            protected void realCompute() throws Exception {
1028                AsyncFib f = new AsyncFib(8);
1029                assertSame(f, f.fork());
1030                assertNull(f.get());
1031                assertEquals(21, f.number);
1032                checkCompletedNormally(f);
1033            }};
1034        testInvokeOnPool(singletonPool(), a);
1035    }
1036
1037    /**
1038     * timed get of a forked task returns when task completes
1039     */
1040    public void testForkTimedGetSingleton() {
1041        RecursiveAction a = new CheckedRecursiveAction() {
1042            protected void realCompute() throws Exception {
1043                AsyncFib f = new AsyncFib(8);
1044                assertSame(f, f.fork());
1045                assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
1046                assertEquals(21, f.number);
1047                checkCompletedNormally(f);
1048            }};
1049        testInvokeOnPool(singletonPool(), a);
1050    }
1051
1052    /**
1053     * timed get with null time unit throws NPE
1054     */
1055    public void testForkTimedGetNPESingleton() {
1056        RecursiveAction a = new CheckedRecursiveAction() {
1057            protected void realCompute() throws Exception {
1058                AsyncFib f = new AsyncFib(8);
1059                assertSame(f, f.fork());
1060                try {
1061                    f.get(5L, null);
1062                    shouldThrow();
1063                } catch (NullPointerException success) {}
1064            }};
1065        testInvokeOnPool(singletonPool(), a);
1066    }
1067
1068    /**
1069     * quietlyJoin of a forked task returns when task completes
1070     */
1071    public void testForkQuietlyJoinSingleton() {
1072        RecursiveAction a = new CheckedRecursiveAction() {
1073            protected void realCompute() {
1074                AsyncFib f = new AsyncFib(8);
1075                assertSame(f, f.fork());
1076                f.quietlyJoin();
1077                assertEquals(21, f.number);
1078                checkCompletedNormally(f);
1079            }};
1080        testInvokeOnPool(singletonPool(), a);
1081    }
1082
1083    /**
1084     * helpQuiesce returns when tasks are complete.
1085     * getQueuedTaskCount returns 0 when quiescent
1086     */
1087    public void testForkHelpQuiesceSingleton() {
1088        RecursiveAction a = new CheckedRecursiveAction() {
1089            protected void realCompute() {
1090                AsyncFib f = new AsyncFib(8);
1091                assertSame(f, f.fork());
1092                helpQuiesce();
1093                assertEquals(0, getQueuedTaskCount());
1094                assertEquals(21, f.number);
1095                checkCompletedNormally(f);
1096            }};
1097        testInvokeOnPool(singletonPool(), a);
1098    }
1099
1100    /**
1101     * invoke task throws exception when task completes abnormally
1102     */
1103    public void testAbnormalInvokeSingleton() {
1104        RecursiveAction a = new CheckedRecursiveAction() {
1105            protected void realCompute() {
1106                FailingAsyncFib f = new FailingAsyncFib(8);
1107                try {
1108                    f.invoke();
1109                    shouldThrow();
1110                } catch (FJException success) {
1111                    checkCompletedAbnormally(f, success);
1112                }
1113            }};
1114        testInvokeOnPool(singletonPool(), a);
1115    }
1116
1117    /**
1118     * quietlyInvoke task returns when task completes abnormally
1119     */
1120    public void testAbnormalQuietlyInvokeSingleton() {
1121        RecursiveAction a = new CheckedRecursiveAction() {
1122            protected void realCompute() {
1123                FailingAsyncFib f = new FailingAsyncFib(8);
1124                f.quietlyInvoke();
1125                assertTrue(f.getException() instanceof FJException);
1126                checkCompletedAbnormally(f, f.getException());
1127            }};
1128        testInvokeOnPool(singletonPool(), a);
1129    }
1130
1131    /**
1132     * join of a forked task throws exception when task completes abnormally
1133     */
1134    public void testAbnormalForkJoinSingleton() {
1135        RecursiveAction a = new CheckedRecursiveAction() {
1136            protected void realCompute() {
1137                FailingAsyncFib f = new FailingAsyncFib(8);
1138                assertSame(f, f.fork());
1139                try {
1140                    f.join();
1141                    shouldThrow();
1142                } catch (FJException success) {
1143                    checkCompletedAbnormally(f, success);
1144                }
1145            }};
1146        testInvokeOnPool(singletonPool(), a);
1147    }
1148
1149    /**
1150     * get of a forked task throws exception when task completes abnormally
1151     */
1152    public void testAbnormalForkGetSingleton() {
1153        RecursiveAction a = new CheckedRecursiveAction() {
1154            protected void realCompute() throws Exception {
1155                FailingAsyncFib f = new FailingAsyncFib(8);
1156                assertSame(f, f.fork());
1157                try {
1158                    f.get();
1159                    shouldThrow();
1160                } catch (ExecutionException success) {
1161                    Throwable cause = success.getCause();
1162                    assertTrue(cause instanceof FJException);
1163                    checkCompletedAbnormally(f, cause);
1164                }
1165            }};
1166        testInvokeOnPool(singletonPool(), a);
1167    }
1168
1169    /**
1170     * timed get of a forked task throws exception when task completes abnormally
1171     */
1172    public void testAbnormalForkTimedGetSingleton() {
1173        RecursiveAction a = new CheckedRecursiveAction() {
1174            protected void realCompute() throws Exception {
1175                FailingAsyncFib f = new FailingAsyncFib(8);
1176                assertSame(f, f.fork());
1177                try {
1178                    f.get(LONG_DELAY_MS, MILLISECONDS);
1179                    shouldThrow();
1180                } catch (ExecutionException success) {
1181                    Throwable cause = success.getCause();
1182                    assertTrue(cause instanceof FJException);
1183                    checkCompletedAbnormally(f, cause);
1184                }
1185            }};
1186        testInvokeOnPool(singletonPool(), a);
1187    }
1188
1189    /**
1190     * quietlyJoin of a forked task returns when task completes abnormally
1191     */
1192    public void testAbnormalForkQuietlyJoinSingleton() {
1193        RecursiveAction a = new CheckedRecursiveAction() {
1194            protected void realCompute() {
1195                FailingAsyncFib f = new FailingAsyncFib(8);
1196                assertSame(f, f.fork());
1197                f.quietlyJoin();
1198                assertTrue(f.getException() instanceof FJException);
1199                checkCompletedAbnormally(f, f.getException());
1200            }};
1201        testInvokeOnPool(singletonPool(), a);
1202    }
1203
1204    /**
1205     * invoke task throws exception after invoking completeExceptionally
1206     */
1207    public void testCompleteExceptionallySingleton() {
1208        RecursiveAction a = new CheckedRecursiveAction() {
1209            protected void realCompute() {
1210                AsyncFib f = new AsyncFib(8);
1211                f.completeExceptionally(new FJException());
1212                try {
1213                    f.invoke();
1214                    shouldThrow();
1215                } catch (FJException success) {
1216                    checkCompletedAbnormally(f, success);
1217                }
1218            }};
1219        testInvokeOnPool(singletonPool(), a);
1220    }
1221
1222    /**
1223     * invokeAll(t1, t2) invokes all task arguments
1224     */
1225    public void testInvokeAll2Singleton() {
1226        RecursiveAction a = new CheckedRecursiveAction() {
1227            protected void realCompute() {
1228                AsyncFib f = new AsyncFib(8);
1229                AsyncFib g = new AsyncFib(9);
1230                invokeAll(f, g);
1231                assertEquals(21, f.number);
1232                assertEquals(34, g.number);
1233                checkCompletedNormally(f);
1234                checkCompletedNormally(g);
1235            }};
1236        testInvokeOnPool(singletonPool(), a);
1237    }
1238
1239    /**
1240     * invokeAll(tasks) with 1 argument invokes task
1241     */
1242    public void testInvokeAll1Singleton() {
1243        RecursiveAction a = new CheckedRecursiveAction() {
1244            protected void realCompute() {
1245                AsyncFib f = new AsyncFib(8);
1246                invokeAll(f);
1247                checkCompletedNormally(f);
1248                assertEquals(21, f.number);
1249            }};
1250        testInvokeOnPool(singletonPool(), a);
1251    }
1252
1253    /**
1254     * invokeAll(tasks) with > 2 argument invokes tasks
1255     */
1256    public void testInvokeAll3Singleton() {
1257        RecursiveAction a = new CheckedRecursiveAction() {
1258            protected void realCompute() {
1259                AsyncFib f = new AsyncFib(8);
1260                AsyncFib g = new AsyncFib(9);
1261                AsyncFib h = new AsyncFib(7);
1262                invokeAll(f, g, h);
1263                assertEquals(21, f.number);
1264                assertEquals(34, g.number);
1265                assertEquals(13, h.number);
1266                checkCompletedNormally(f);
1267                checkCompletedNormally(g);
1268                checkCompletedNormally(h);
1269            }};
1270        testInvokeOnPool(singletonPool(), a);
1271    }
1272
1273    /**
1274     * invokeAll(collection) invokes all tasks in the collection
1275     */
1276    public void testInvokeAllCollectionSingleton() {
1277        RecursiveAction a = new CheckedRecursiveAction() {
1278            protected void realCompute() {
1279                AsyncFib f = new AsyncFib(8);
1280                AsyncFib g = new AsyncFib(9);
1281                AsyncFib h = new AsyncFib(7);
1282                HashSet set = new HashSet();
1283                set.add(f);
1284                set.add(g);
1285                set.add(h);
1286                invokeAll(set);
1287                assertEquals(21, f.number);
1288                assertEquals(34, g.number);
1289                assertEquals(13, h.number);
1290                checkCompletedNormally(f);
1291                checkCompletedNormally(g);
1292                checkCompletedNormally(h);
1293            }};
1294        testInvokeOnPool(singletonPool(), a);
1295    }
1296
1297    /**
1298     * invokeAll(tasks) with any null task throws NPE
1299     */
1300    public void testInvokeAllNPESingleton() {
1301        RecursiveAction a = new CheckedRecursiveAction() {
1302            protected void realCompute() {
1303                AsyncFib f = new AsyncFib(8);
1304                AsyncFib g = new AsyncFib(9);
1305                AsyncFib h = null;
1306                try {
1307                    invokeAll(f, g, h);
1308                    shouldThrow();
1309                } catch (NullPointerException success) {}
1310            }};
1311        testInvokeOnPool(singletonPool(), a);
1312    }
1313
1314    /**
1315     * invokeAll(t1, t2) throw exception if any task does
1316     */
1317    public void testAbnormalInvokeAll2Singleton() {
1318        RecursiveAction a = new CheckedRecursiveAction() {
1319            protected void realCompute() {
1320                AsyncFib f = new AsyncFib(8);
1321                FailingAsyncFib g = new FailingAsyncFib(9);
1322                try {
1323                    invokeAll(f, g);
1324                    shouldThrow();
1325                } catch (FJException success) {
1326                    checkCompletedAbnormally(g, success);
1327                }
1328            }};
1329        testInvokeOnPool(singletonPool(), a);
1330    }
1331
1332    /**
1333     * invokeAll(tasks) with 1 argument throws exception if task does
1334     */
1335    public void testAbnormalInvokeAll1Singleton() {
1336        RecursiveAction a = new CheckedRecursiveAction() {
1337            protected void realCompute() {
1338                FailingAsyncFib g = new FailingAsyncFib(9);
1339                try {
1340                    invokeAll(g);
1341                    shouldThrow();
1342                } catch (FJException success) {
1343                    checkCompletedAbnormally(g, success);
1344                }
1345            }};
1346        testInvokeOnPool(singletonPool(), a);
1347    }
1348
1349    /**
1350     * invokeAll(tasks) with > 2 argument throws exception if any task does
1351     */
1352    public void testAbnormalInvokeAll3Singleton() {
1353        RecursiveAction a = new CheckedRecursiveAction() {
1354            protected void realCompute() {
1355                AsyncFib f = new AsyncFib(8);
1356                FailingAsyncFib g = new FailingAsyncFib(9);
1357                AsyncFib h = new AsyncFib(7);
1358                try {
1359                    invokeAll(f, g, h);
1360                    shouldThrow();
1361                } catch (FJException success) {
1362                    checkCompletedAbnormally(g, success);
1363                }
1364            }};
1365        testInvokeOnPool(singletonPool(), a);
1366    }
1367
1368    /**
1369     * invokeAll(collection) throws exception if any task does
1370     */
1371    public void testAbnormalInvokeAllCollectionSingleton() {
1372        RecursiveAction a = new CheckedRecursiveAction() {
1373            protected void realCompute() {
1374                FailingAsyncFib f = new FailingAsyncFib(8);
1375                AsyncFib g = new AsyncFib(9);
1376                AsyncFib h = new AsyncFib(7);
1377                HashSet set = new HashSet();
1378                set.add(f);
1379                set.add(g);
1380                set.add(h);
1381                try {
1382                    invokeAll(set);
1383                    shouldThrow();
1384                } catch (FJException success) {
1385                    checkCompletedAbnormally(f, success);
1386                }
1387            }};
1388        testInvokeOnPool(singletonPool(), a);
1389    }
1390
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