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