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

Comparing jsr166/src/test/tck/ForkJoinTaskTest.java (file contents):
Revision 1.32 by jsr166, Fri May 27 17:15:48 2011 UTC vs.
Revision 1.54 by jsr166, Sat Oct 21 06:52:36 2017 UTC

# Line 3 | Line 3
3   * Expert Group and released to the public domain, as explained at
4   * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6 < import java.util.concurrent.ExecutionException;
6 >
7 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
8 >
9 > import java.util.Arrays;
10 > import java.util.HashSet;
11 > import java.util.concurrent.Callable;
12   import java.util.concurrent.CancellationException;
13 + import java.util.concurrent.ExecutionException;
14   import java.util.concurrent.ForkJoinPool;
15   import java.util.concurrent.ForkJoinTask;
10 import java.util.concurrent.ForkJoinWorkerThread;
16   import java.util.concurrent.RecursiveAction;
12 import java.util.concurrent.TimeUnit;
17   import java.util.concurrent.TimeoutException;
18   import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
19 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
20 < import static java.util.concurrent.TimeUnit.SECONDS;
21 < import java.util.HashSet;
18 < import junit.framework.*;
19 >
20 > import junit.framework.Test;
21 > import junit.framework.TestSuite;
22  
23   public class ForkJoinTaskTest extends JSR166TestCase {
24  
25      public static void main(String[] args) {
26 <        junit.textui.TestRunner.run(suite());
26 >        main(suite(), args);
27      }
28  
29      public static Test suite() {
# Line 46 | Line 49 | public class ForkJoinTaskTest extends JS
49      }
50  
51      private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
52 <        try {
52 >        try (PoolCleaner cleaner = cleaner(pool)) {
53              assertFalse(a.isDone());
54              assertFalse(a.isCompletedNormally());
55              assertFalse(a.isCompletedAbnormally());
# Line 62 | Line 65 | public class ForkJoinTaskTest extends JS
65              assertFalse(a.isCancelled());
66              assertNull(a.getException());
67              assertNull(a.getRawResult());
65        } finally {
66            joinPool(pool);
68          }
69      }
70  
# Line 76 | Line 77 | public class ForkJoinTaskTest extends JS
77          assertNull(a.getRawResult());
78  
79          try {
80 <            a.get(0L, SECONDS);
80 >            a.get(randomExpiredTimeout(), randomTimeUnit());
81              shouldThrow();
82          } catch (TimeoutException success) {
83          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 96 | Line 97 | public class ForkJoinTaskTest extends JS
97  
98          {
99              Thread.currentThread().interrupt();
100 <            long t0 = System.nanoTime();
100 >            long startTime = System.nanoTime();
101              assertSame(expected, a.join());
102 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
102 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
103              Thread.interrupted();
104          }
105  
106          {
107              Thread.currentThread().interrupt();
108 <            long t0 = System.nanoTime();
108 >            long startTime = System.nanoTime();
109              a.quietlyJoin();        // should be no-op
110 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
110 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
111              Thread.interrupted();
112          }
113  
# Line 114 | Line 115 | public class ForkJoinTaskTest extends JS
115          assertFalse(a.cancel(true));
116          try {
117              assertSame(expected, a.get());
118 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
119 <        try {
119 <            assertSame(expected, a.get(5L, SECONDS));
120 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
118 >            assertSame(expected, a.get(randomTimeout(), randomTimeUnit()));
119 >        } catch (Exception fail) { threadUnexpectedException(fail); }
120      }
121  
122      void checkCancelled(ForkJoinTask a) {
# Line 139 | Line 138 | public class ForkJoinTaskTest extends JS
138          Thread.interrupted();
139  
140          {
141 <            long t0 = System.nanoTime();
141 >            long startTime = System.nanoTime();
142              a.quietlyJoin();        // should be no-op
143 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
143 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
144          }
145  
146          try {
# Line 151 | Line 150 | public class ForkJoinTaskTest extends JS
150          } catch (Throwable fail) { threadUnexpectedException(fail); }
151  
152          try {
153 <            a.get(5L, SECONDS);
153 >            a.get(randomTimeout(), randomTimeUnit());
154              shouldThrow();
155          } catch (CancellationException success) {
156          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 177 | Line 176 | public class ForkJoinTaskTest extends JS
176          Thread.interrupted();
177  
178          {
179 <            long t0 = System.nanoTime();
179 >            long startTime = System.nanoTime();
180              a.quietlyJoin();        // should be no-op
181 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
181 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
182          }
183  
184          try {
# Line 190 | Line 189 | public class ForkJoinTaskTest extends JS
189          } catch (Throwable fail) { threadUnexpectedException(fail); }
190  
191          try {
192 <            a.get(5L, SECONDS);
192 >            a.get(randomTimeout(), randomTimeUnit());
193              shouldThrow();
194          } catch (ExecutionException success) {
195              assertSame(t.getClass(), success.getCause().getClass());
# Line 216 | Line 215 | public class ForkJoinTaskTest extends JS
215              AtomicIntegerFieldUpdater.newUpdater(BinaryAsyncAction.class,
216                                                   "controlState");
217  
218 <        private BinaryAsyncAction parent;
218 >        private volatile BinaryAsyncAction parent;
219  
220 <        private BinaryAsyncAction sibling;
220 >        private volatile BinaryAsyncAction sibling;
221  
222          protected BinaryAsyncAction() {
223          }
# Line 253 | Line 252 | public class ForkJoinTaskTest extends JS
252              super.completeExceptionally(ex);
253          }
254  
255 +        public boolean cancel(boolean mayInterruptIfRunning) {
256 +            if (super.cancel(mayInterruptIfRunning)) {
257 +                completeExceptionally(new FJException());
258 +                return true;
259 +            }
260 +            return false;
261 +        }
262 +
263          public final void complete() {
264              BinaryAsyncAction a = this;
265              for (;;) {
# Line 274 | Line 281 | public class ForkJoinTaskTest extends JS
281          }
282  
283          public final void completeExceptionally(Throwable ex) {
284 <            BinaryAsyncAction a = this;
278 <            while (!a.isCompletedAbnormally()) {
284 >            for (BinaryAsyncAction a = this;;) {
285                  a.completeThisExceptionally(ex);
286                  BinaryAsyncAction s = a.sibling;
287 <                if (s != null)
288 <                    s.cancel(false);
289 <                if (!a.onException() || (a = a.parent) == null)
287 >                if (s != null && !s.isDone())
288 >                    s.completeExceptionally(ex);
289 >                if ((a = a.parent) == null)
290                      break;
291              }
292          }
# Line 330 | Line 336 | public class ForkJoinTaskTest extends JS
336          public final boolean exec() {
337              AsyncFib f = this;
338              int n = f.number;
339 <            if (n > 1) {
340 <                while (n > 1) {
341 <                    AsyncFib p = f;
342 <                    AsyncFib r = new AsyncFib(n - 2);
343 <                    f = new AsyncFib(--n);
344 <                    p.linkSubtasks(r, f);
339 <                    r.fork();
340 <                }
341 <                f.number = n;
339 >            while (n > 1) {
340 >                AsyncFib p = f;
341 >                AsyncFib r = new AsyncFib(n - 2);
342 >                f = new AsyncFib(--n);
343 >                p.linkSubtasks(r, f);
344 >                r.fork();
345              }
346              f.complete();
347              return false;
# Line 349 | Line 352 | public class ForkJoinTaskTest extends JS
352          }
353      }
354  
352
355      static final class FailingAsyncFib extends BinaryAsyncAction {
356          int number;
357          public FailingAsyncFib(int n) {
# Line 359 | Line 361 | public class ForkJoinTaskTest extends JS
361          public final boolean exec() {
362              FailingAsyncFib f = this;
363              int n = f.number;
364 <            if (n > 1) {
365 <                while (n > 1) {
366 <                    FailingAsyncFib p = f;
367 <                    FailingAsyncFib r = new FailingAsyncFib(n - 2);
368 <                    f = new FailingAsyncFib(--n);
369 <                    p.linkSubtasks(r, f);
368 <                    r.fork();
369 <                }
370 <                f.number = n;
364 >            while (n > 1) {
365 >                FailingAsyncFib p = f;
366 >                FailingAsyncFib r = new FailingAsyncFib(n - 2);
367 >                f = new FailingAsyncFib(--n);
368 >                p.linkSubtasks(r, f);
369 >                r.fork();
370              }
371              f.complete();
372              return false;
# Line 385 | Line 384 | public class ForkJoinTaskTest extends JS
384       */
385      public void testInvoke() {
386          RecursiveAction a = new CheckedRecursiveAction() {
387 <            public void realCompute() {
387 >            protected void realCompute() {
388                  AsyncFib f = new AsyncFib(8);
389                  assertNull(f.invoke());
390                  assertEquals(21, f.number);
# Line 401 | Line 400 | public class ForkJoinTaskTest extends JS
400       */
401      public void testQuietlyInvoke() {
402          RecursiveAction a = new CheckedRecursiveAction() {
403 <            public void realCompute() {
403 >            protected void realCompute() {
404                  AsyncFib f = new AsyncFib(8);
405                  f.quietlyInvoke();
406                  assertEquals(21, f.number);
# Line 415 | Line 414 | public class ForkJoinTaskTest extends JS
414       */
415      public void testForkJoin() {
416          RecursiveAction a = new CheckedRecursiveAction() {
417 <            public void realCompute() {
417 >            protected void realCompute() {
418                  AsyncFib f = new AsyncFib(8);
419                  assertSame(f, f.fork());
420                  assertNull(f.join());
# Line 430 | Line 429 | public class ForkJoinTaskTest extends JS
429       */
430      public void testForkGet() {
431          RecursiveAction a = new CheckedRecursiveAction() {
432 <            public void realCompute() throws Exception {
432 >            protected void realCompute() throws Exception {
433                  AsyncFib f = new AsyncFib(8);
434                  assertSame(f, f.fork());
435                  assertNull(f.get());
# Line 445 | Line 444 | public class ForkJoinTaskTest extends JS
444       */
445      public void testForkTimedGet() {
446          RecursiveAction a = new CheckedRecursiveAction() {
447 <            public void realCompute() throws Exception {
447 >            protected void realCompute() throws Exception {
448                  AsyncFib f = new AsyncFib(8);
449                  assertSame(f, f.fork());
450                  assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
# Line 460 | Line 459 | public class ForkJoinTaskTest extends JS
459       */
460      public void testForkTimedGetNPE() {
461          RecursiveAction a = new CheckedRecursiveAction() {
462 <            public void realCompute() throws Exception {
462 >            protected void realCompute() throws Exception {
463                  AsyncFib f = new AsyncFib(8);
464                  assertSame(f, f.fork());
465                  try {
466 <                    f.get(5L, null);
466 >                    f.get(randomTimeout(), null);
467                      shouldThrow();
468                  } catch (NullPointerException success) {}
469              }};
# Line 476 | Line 475 | public class ForkJoinTaskTest extends JS
475       */
476      public void testForkQuietlyJoin() {
477          RecursiveAction a = new CheckedRecursiveAction() {
478 <            public void realCompute() {
478 >            protected void realCompute() {
479                  AsyncFib f = new AsyncFib(8);
480                  assertSame(f, f.fork());
481                  f.quietlyJoin();
# Line 486 | Line 485 | public class ForkJoinTaskTest extends JS
485          testInvokeOnPool(mainPool(), a);
486      }
487  
489
488      /**
489       * helpQuiesce returns when tasks are complete.
490       * getQueuedTaskCount returns 0 when quiescent
491       */
492      public void testForkHelpQuiesce() {
493          RecursiveAction a = new CheckedRecursiveAction() {
494 <            public void realCompute() {
494 >            protected void realCompute() {
495                  AsyncFib f = new AsyncFib(8);
496                  assertSame(f, f.fork());
497                  helpQuiesce();
# Line 504 | Line 502 | public class ForkJoinTaskTest extends JS
502          testInvokeOnPool(mainPool(), a);
503      }
504  
507
505      /**
506       * invoke task throws exception when task completes abnormally
507       */
508      public void testAbnormalInvoke() {
509          RecursiveAction a = new CheckedRecursiveAction() {
510 <            public void realCompute() {
510 >            protected void realCompute() {
511                  FailingAsyncFib f = new FailingAsyncFib(8);
512                  try {
513                      f.invoke();
# Line 527 | Line 524 | public class ForkJoinTaskTest extends JS
524       */
525      public void testAbnormalQuietlyInvoke() {
526          RecursiveAction a = new CheckedRecursiveAction() {
527 <            public void realCompute() {
527 >            protected void realCompute() {
528                  FailingAsyncFib f = new FailingAsyncFib(8);
529                  f.quietlyInvoke();
530                  assertTrue(f.getException() instanceof FJException);
# Line 541 | Line 538 | public class ForkJoinTaskTest extends JS
538       */
539      public void testAbnormalForkJoin() {
540          RecursiveAction a = new CheckedRecursiveAction() {
541 <            public void realCompute() {
541 >            protected void realCompute() {
542                  FailingAsyncFib f = new FailingAsyncFib(8);
543                  assertSame(f, f.fork());
544                  try {
# Line 559 | Line 556 | public class ForkJoinTaskTest extends JS
556       */
557      public void testAbnormalForkGet() {
558          RecursiveAction a = new CheckedRecursiveAction() {
559 <            public void realCompute() throws Exception {
559 >            protected void realCompute() throws Exception {
560                  FailingAsyncFib f = new FailingAsyncFib(8);
561                  assertSame(f, f.fork());
562                  try {
# Line 579 | Line 576 | public class ForkJoinTaskTest extends JS
576       */
577      public void testAbnormalForkTimedGet() {
578          RecursiveAction a = new CheckedRecursiveAction() {
579 <            public void realCompute() throws Exception {
579 >            protected void realCompute() throws Exception {
580                  FailingAsyncFib f = new FailingAsyncFib(8);
581                  assertSame(f, f.fork());
582                  try {
# Line 599 | Line 596 | public class ForkJoinTaskTest extends JS
596       */
597      public void testAbnormalForkQuietlyJoin() {
598          RecursiveAction a = new CheckedRecursiveAction() {
599 <            public void realCompute() {
599 >            protected void realCompute() {
600                  FailingAsyncFib f = new FailingAsyncFib(8);
601                  assertSame(f, f.fork());
602                  f.quietlyJoin();
# Line 614 | Line 611 | public class ForkJoinTaskTest extends JS
611       */
612      public void testCancelledInvoke() {
613          RecursiveAction a = new CheckedRecursiveAction() {
614 <            public void realCompute() {
614 >            protected void realCompute() {
615                  AsyncFib f = new AsyncFib(8);
616                  assertTrue(f.cancel(true));
617                  try {
# Line 632 | Line 629 | public class ForkJoinTaskTest extends JS
629       */
630      public void testCancelledForkJoin() {
631          RecursiveAction a = new CheckedRecursiveAction() {
632 <            public void realCompute() {
632 >            protected void realCompute() {
633                  AsyncFib f = new AsyncFib(8);
634                  assertTrue(f.cancel(true));
635                  assertSame(f, f.fork());
# Line 651 | Line 648 | public class ForkJoinTaskTest extends JS
648       */
649      public void testCancelledForkGet() {
650          RecursiveAction a = new CheckedRecursiveAction() {
651 <            public void realCompute() throws Exception {
651 >            protected void realCompute() throws Exception {
652                  AsyncFib f = new AsyncFib(8);
653                  assertTrue(f.cancel(true));
654                  assertSame(f, f.fork());
# Line 670 | Line 667 | public class ForkJoinTaskTest extends JS
667       */
668      public void testCancelledForkTimedGet() throws Exception {
669          RecursiveAction a = new CheckedRecursiveAction() {
670 <            public void realCompute() throws Exception {
670 >            protected void realCompute() throws Exception {
671                  AsyncFib f = new AsyncFib(8);
672                  assertTrue(f.cancel(true));
673                  assertSame(f, f.fork());
# Line 689 | Line 686 | public class ForkJoinTaskTest extends JS
686       */
687      public void testCancelledForkQuietlyJoin() {
688          RecursiveAction a = new CheckedRecursiveAction() {
689 <            public void realCompute() {
689 >            protected void realCompute() {
690                  AsyncFib f = new AsyncFib(8);
691                  assertTrue(f.cancel(true));
692                  assertSame(f, f.fork());
# Line 705 | Line 702 | public class ForkJoinTaskTest extends JS
702      public void testGetPool() {
703          final ForkJoinPool mainPool = mainPool();
704          RecursiveAction a = new CheckedRecursiveAction() {
705 <            public void realCompute() {
705 >            protected void realCompute() {
706                  assertSame(mainPool, getPool());
707              }};
708          testInvokeOnPool(mainPool, a);
# Line 716 | Line 713 | public class ForkJoinTaskTest extends JS
713       */
714      public void testGetPool2() {
715          RecursiveAction a = new CheckedRecursiveAction() {
716 <            public void realCompute() {
716 >            protected void realCompute() {
717                  assertNull(getPool());
718              }};
719          assertNull(a.invoke());
# Line 727 | Line 724 | public class ForkJoinTaskTest extends JS
724       */
725      public void testInForkJoinPool() {
726          RecursiveAction a = new CheckedRecursiveAction() {
727 <            public void realCompute() {
727 >            protected void realCompute() {
728                  assertTrue(inForkJoinPool());
729              }};
730          testInvokeOnPool(mainPool(), a);
# Line 738 | Line 735 | public class ForkJoinTaskTest extends JS
735       */
736      public void testInForkJoinPool2() {
737          RecursiveAction a = new CheckedRecursiveAction() {
738 <            public void realCompute() {
738 >            protected void realCompute() {
739                  assertFalse(inForkJoinPool());
740              }};
741          assertNull(a.invoke());
# Line 749 | Line 746 | public class ForkJoinTaskTest extends JS
746       */
747      public void testSetRawResult() {
748          RecursiveAction a = new CheckedRecursiveAction() {
749 <            public void realCompute() {
749 >            protected void realCompute() {
750                  setRawResult(null);
751                  assertNull(getRawResult());
752              }};
# Line 761 | Line 758 | public class ForkJoinTaskTest extends JS
758       */
759      public void testCompleteExceptionally() {
760          RecursiveAction a = new CheckedRecursiveAction() {
761 <            public void realCompute() {
761 >            protected void realCompute() {
762                  AsyncFib f = new AsyncFib(8);
763                  f.completeExceptionally(new FJException());
764                  try {
# Line 775 | Line 772 | public class ForkJoinTaskTest extends JS
772      }
773  
774      /**
775 +     * completeExceptionally(null) surprisingly has the same effect as
776 +     * completeExceptionally(new RuntimeException())
777 +     */
778 +    public void testCompleteExceptionally_null() {
779 +        RecursiveAction a = new CheckedRecursiveAction() {
780 +            protected void realCompute() {
781 +                AsyncFib f = new AsyncFib(8);
782 +                f.completeExceptionally(null);
783 +                try {
784 +                    f.invoke();
785 +                    shouldThrow();
786 +                } catch (RuntimeException success) {
787 +                    assertSame(success.getClass(), RuntimeException.class);
788 +                    assertNull(success.getCause());
789 +                    checkCompletedAbnormally(f, success);
790 +                }
791 +            }};
792 +        testInvokeOnPool(mainPool(), a);
793 +    }
794 +
795 +    /**
796       * invokeAll(t1, t2) invokes all task arguments
797       */
798      public void testInvokeAll2() {
799          RecursiveAction a = new CheckedRecursiveAction() {
800 <            public void realCompute() {
800 >            protected void realCompute() {
801                  AsyncFib f = new AsyncFib(8);
802                  AsyncFib g = new AsyncFib(9);
803                  invokeAll(f, g);
# Line 796 | Line 814 | public class ForkJoinTaskTest extends JS
814       */
815      public void testInvokeAll1() {
816          RecursiveAction a = new CheckedRecursiveAction() {
817 <            public void realCompute() {
817 >            protected void realCompute() {
818                  AsyncFib f = new AsyncFib(8);
819                  invokeAll(f);
820                  checkCompletedNormally(f);
# Line 810 | Line 828 | public class ForkJoinTaskTest extends JS
828       */
829      public void testInvokeAll3() {
830          RecursiveAction a = new CheckedRecursiveAction() {
831 <            public void realCompute() {
831 >            protected void realCompute() {
832                  AsyncFib f = new AsyncFib(8);
833                  AsyncFib g = new AsyncFib(9);
834                  AsyncFib h = new AsyncFib(7);
# Line 830 | Line 848 | public class ForkJoinTaskTest extends JS
848       */
849      public void testInvokeAllCollection() {
850          RecursiveAction a = new CheckedRecursiveAction() {
851 <            public void realCompute() {
851 >            protected void realCompute() {
852                  AsyncFib f = new AsyncFib(8);
853                  AsyncFib g = new AsyncFib(9);
854                  AsyncFib h = new AsyncFib(7);
# Line 849 | Line 867 | public class ForkJoinTaskTest extends JS
867          testInvokeOnPool(mainPool(), a);
868      }
869  
852
870      /**
871       * invokeAll(tasks) with any null task throws NPE
872       */
873      public void testInvokeAllNPE() {
874          RecursiveAction a = new CheckedRecursiveAction() {
875 <            public void realCompute() {
875 >            protected void realCompute() {
876                  AsyncFib f = new AsyncFib(8);
877                  AsyncFib g = new AsyncFib(9);
878                  AsyncFib h = null;
# Line 872 | Line 889 | public class ForkJoinTaskTest extends JS
889       */
890      public void testAbnormalInvokeAll2() {
891          RecursiveAction a = new CheckedRecursiveAction() {
892 <            public void realCompute() {
892 >            protected void realCompute() {
893                  AsyncFib f = new AsyncFib(8);
894                  FailingAsyncFib g = new FailingAsyncFib(9);
895 +                ForkJoinTask[] tasks = { f, g };
896 +                shuffle(tasks);
897                  try {
898 <                    invokeAll(f, g);
898 >                    invokeAll(tasks);
899                      shouldThrow();
900                  } catch (FJException success) {
901                      checkCompletedAbnormally(g, success);
# Line 890 | Line 909 | public class ForkJoinTaskTest extends JS
909       */
910      public void testAbnormalInvokeAll1() {
911          RecursiveAction a = new CheckedRecursiveAction() {
912 <            public void realCompute() {
912 >            protected void realCompute() {
913                  FailingAsyncFib g = new FailingAsyncFib(9);
914                  try {
915                      invokeAll(g);
# Line 907 | Line 926 | public class ForkJoinTaskTest extends JS
926       */
927      public void testAbnormalInvokeAll3() {
928          RecursiveAction a = new CheckedRecursiveAction() {
929 <            public void realCompute() {
929 >            protected void realCompute() {
930                  AsyncFib f = new AsyncFib(8);
931                  FailingAsyncFib g = new FailingAsyncFib(9);
932                  AsyncFib h = new AsyncFib(7);
933 +                ForkJoinTask[] tasks = { f, g, h };
934 +                shuffle(tasks);
935                  try {
936 <                    invokeAll(f, g, h);
936 >                    invokeAll(tasks);
937                      shouldThrow();
938                  } catch (FJException success) {
939                      checkCompletedAbnormally(g, success);
# Line 922 | Line 943 | public class ForkJoinTaskTest extends JS
943      }
944  
945      /**
946 <     * invokeAll(collection)  throws exception if any task does
946 >     * invokeAll(collection) throws exception if any task does
947       */
948      public void testAbnormalInvokeAllCollection() {
949          RecursiveAction a = new CheckedRecursiveAction() {
950 <            public void realCompute() {
950 >            protected void realCompute() {
951                  FailingAsyncFib f = new FailingAsyncFib(8);
952                  AsyncFib g = new AsyncFib(9);
953                  AsyncFib h = new AsyncFib(7);
954 <                HashSet set = new HashSet();
955 <                set.add(f);
935 <                set.add(g);
936 <                set.add(h);
954 >                ForkJoinTask[] tasks = { f, g, h };
955 >                shuffle(tasks);
956                  try {
957 <                    invokeAll(set);
957 >                    invokeAll(Arrays.asList(tasks));
958                      shouldThrow();
959                  } catch (FJException success) {
960                      checkCompletedAbnormally(f, success);
# Line 950 | Line 969 | public class ForkJoinTaskTest extends JS
969       */
970      public void testTryUnfork() {
971          RecursiveAction a = new CheckedRecursiveAction() {
972 <            public void realCompute() {
972 >            protected void realCompute() {
973                  AsyncFib g = new AsyncFib(9);
974                  assertSame(g, g.fork());
975                  AsyncFib f = new AsyncFib(8);
# Line 969 | Line 988 | public class ForkJoinTaskTest extends JS
988       */
989      public void testGetSurplusQueuedTaskCount() {
990          RecursiveAction a = new CheckedRecursiveAction() {
991 <            public void realCompute() {
991 >            protected void realCompute() {
992                  AsyncFib h = new AsyncFib(7);
993                  assertSame(h, h.fork());
994                  AsyncFib g = new AsyncFib(9);
# Line 991 | Line 1010 | public class ForkJoinTaskTest extends JS
1010       */
1011      public void testPeekNextLocalTask() {
1012          RecursiveAction a = new CheckedRecursiveAction() {
1013 <            public void realCompute() {
1013 >            protected void realCompute() {
1014                  AsyncFib g = new AsyncFib(9);
1015                  assertSame(g, g.fork());
1016                  AsyncFib f = new AsyncFib(8);
# Line 1011 | Line 1030 | public class ForkJoinTaskTest extends JS
1030       */
1031      public void testPollNextLocalTask() {
1032          RecursiveAction a = new CheckedRecursiveAction() {
1033 <            public void realCompute() {
1033 >            protected void realCompute() {
1034                  AsyncFib g = new AsyncFib(9);
1035                  assertSame(g, g.fork());
1036                  AsyncFib f = new AsyncFib(8);
# Line 1030 | Line 1049 | public class ForkJoinTaskTest extends JS
1049       */
1050      public void testPollTask() {
1051          RecursiveAction a = new CheckedRecursiveAction() {
1052 <            public void realCompute() {
1052 >            protected void realCompute() {
1053                  AsyncFib g = new AsyncFib(9);
1054                  assertSame(g, g.fork());
1055                  AsyncFib f = new AsyncFib(8);
# Line 1048 | Line 1067 | public class ForkJoinTaskTest extends JS
1067       */
1068      public void testPeekNextLocalTaskAsync() {
1069          RecursiveAction a = new CheckedRecursiveAction() {
1070 <            public void realCompute() {
1070 >            protected void realCompute() {
1071                  AsyncFib g = new AsyncFib(9);
1072                  assertSame(g, g.fork());
1073                  AsyncFib f = new AsyncFib(8);
# Line 1069 | Line 1088 | public class ForkJoinTaskTest extends JS
1088       */
1089      public void testPollNextLocalTaskAsync() {
1090          RecursiveAction a = new CheckedRecursiveAction() {
1091 <            public void realCompute() {
1091 >            protected void realCompute() {
1092                  AsyncFib g = new AsyncFib(9);
1093                  assertSame(g, g.fork());
1094                  AsyncFib f = new AsyncFib(8);
# Line 1089 | Line 1108 | public class ForkJoinTaskTest extends JS
1108       */
1109      public void testPollTaskAsync() {
1110          RecursiveAction a = new CheckedRecursiveAction() {
1111 <            public void realCompute() {
1111 >            protected void realCompute() {
1112                  AsyncFib g = new AsyncFib(9);
1113                  assertSame(g, g.fork());
1114                  AsyncFib f = new AsyncFib(8);
# Line 1112 | Line 1131 | public class ForkJoinTaskTest extends JS
1131       */
1132      public void testInvokeSingleton() {
1133          RecursiveAction a = new CheckedRecursiveAction() {
1134 <            public void realCompute() {
1134 >            protected void realCompute() {
1135                  AsyncFib f = new AsyncFib(8);
1136                  assertNull(f.invoke());
1137                  assertEquals(21, f.number);
# Line 1128 | Line 1147 | public class ForkJoinTaskTest extends JS
1147       */
1148      public void testQuietlyInvokeSingleton() {
1149          RecursiveAction a = new CheckedRecursiveAction() {
1150 <            public void realCompute() {
1150 >            protected void realCompute() {
1151                  AsyncFib f = new AsyncFib(8);
1152                  f.quietlyInvoke();
1153                  assertEquals(21, f.number);
# Line 1142 | Line 1161 | public class ForkJoinTaskTest extends JS
1161       */
1162      public void testForkJoinSingleton() {
1163          RecursiveAction a = new CheckedRecursiveAction() {
1164 <            public void realCompute() {
1164 >            protected void realCompute() {
1165                  AsyncFib f = new AsyncFib(8);
1166                  assertSame(f, f.fork());
1167                  assertNull(f.join());
# Line 1157 | Line 1176 | public class ForkJoinTaskTest extends JS
1176       */
1177      public void testForkGetSingleton() {
1178          RecursiveAction a = new CheckedRecursiveAction() {
1179 <            public void realCompute() throws Exception {
1179 >            protected void realCompute() throws Exception {
1180                  AsyncFib f = new AsyncFib(8);
1181                  assertSame(f, f.fork());
1182                  assertNull(f.get());
# Line 1172 | Line 1191 | public class ForkJoinTaskTest extends JS
1191       */
1192      public void testForkTimedGetSingleton() {
1193          RecursiveAction a = new CheckedRecursiveAction() {
1194 <            public void realCompute() throws Exception {
1194 >            protected void realCompute() throws Exception {
1195                  AsyncFib f = new AsyncFib(8);
1196                  assertSame(f, f.fork());
1197                  assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
# Line 1187 | Line 1206 | public class ForkJoinTaskTest extends JS
1206       */
1207      public void testForkTimedGetNPESingleton() {
1208          RecursiveAction a = new CheckedRecursiveAction() {
1209 <            public void realCompute() throws Exception {
1209 >            protected void realCompute() throws Exception {
1210                  AsyncFib f = new AsyncFib(8);
1211                  assertSame(f, f.fork());
1212                  try {
1213 <                    f.get(5L, null);
1213 >                    f.get(randomTimeout(), null);
1214                      shouldThrow();
1215                  } catch (NullPointerException success) {}
1216              }};
# Line 1203 | Line 1222 | public class ForkJoinTaskTest extends JS
1222       */
1223      public void testForkQuietlyJoinSingleton() {
1224          RecursiveAction a = new CheckedRecursiveAction() {
1225 <            public void realCompute() {
1225 >            protected void realCompute() {
1226                  AsyncFib f = new AsyncFib(8);
1227                  assertSame(f, f.fork());
1228                  f.quietlyJoin();
# Line 1213 | Line 1232 | public class ForkJoinTaskTest extends JS
1232          testInvokeOnPool(singletonPool(), a);
1233      }
1234  
1216
1235      /**
1236       * helpQuiesce returns when tasks are complete.
1237       * getQueuedTaskCount returns 0 when quiescent
1238       */
1239      public void testForkHelpQuiesceSingleton() {
1240          RecursiveAction a = new CheckedRecursiveAction() {
1241 <            public void realCompute() {
1241 >            protected void realCompute() {
1242                  AsyncFib f = new AsyncFib(8);
1243                  assertSame(f, f.fork());
1244                  helpQuiesce();
# Line 1231 | Line 1249 | public class ForkJoinTaskTest extends JS
1249          testInvokeOnPool(singletonPool(), a);
1250      }
1251  
1234
1252      /**
1253       * invoke task throws exception when task completes abnormally
1254       */
1255      public void testAbnormalInvokeSingleton() {
1256          RecursiveAction a = new CheckedRecursiveAction() {
1257 <            public void realCompute() {
1257 >            protected void realCompute() {
1258                  FailingAsyncFib f = new FailingAsyncFib(8);
1259                  try {
1260                      f.invoke();
# Line 1254 | Line 1271 | public class ForkJoinTaskTest extends JS
1271       */
1272      public void testAbnormalQuietlyInvokeSingleton() {
1273          RecursiveAction a = new CheckedRecursiveAction() {
1274 <            public void realCompute() {
1274 >            protected void realCompute() {
1275                  FailingAsyncFib f = new FailingAsyncFib(8);
1276                  f.quietlyInvoke();
1277                  assertTrue(f.getException() instanceof FJException);
# Line 1268 | Line 1285 | public class ForkJoinTaskTest extends JS
1285       */
1286      public void testAbnormalForkJoinSingleton() {
1287          RecursiveAction a = new CheckedRecursiveAction() {
1288 <            public void realCompute() {
1288 >            protected void realCompute() {
1289                  FailingAsyncFib f = new FailingAsyncFib(8);
1290                  assertSame(f, f.fork());
1291                  try {
# Line 1286 | Line 1303 | public class ForkJoinTaskTest extends JS
1303       */
1304      public void testAbnormalForkGetSingleton() {
1305          RecursiveAction a = new CheckedRecursiveAction() {
1306 <            public void realCompute() throws Exception {
1306 >            protected void realCompute() throws Exception {
1307                  FailingAsyncFib f = new FailingAsyncFib(8);
1308                  assertSame(f, f.fork());
1309                  try {
# Line 1306 | Line 1323 | public class ForkJoinTaskTest extends JS
1323       */
1324      public void testAbnormalForkTimedGetSingleton() {
1325          RecursiveAction a = new CheckedRecursiveAction() {
1326 <            public void realCompute() throws Exception {
1326 >            protected void realCompute() throws Exception {
1327                  FailingAsyncFib f = new FailingAsyncFib(8);
1328                  assertSame(f, f.fork());
1329                  try {
# Line 1326 | Line 1343 | public class ForkJoinTaskTest extends JS
1343       */
1344      public void testAbnormalForkQuietlyJoinSingleton() {
1345          RecursiveAction a = new CheckedRecursiveAction() {
1346 <            public void realCompute() {
1346 >            protected void realCompute() {
1347                  FailingAsyncFib f = new FailingAsyncFib(8);
1348                  assertSame(f, f.fork());
1349                  f.quietlyJoin();
# Line 1341 | Line 1358 | public class ForkJoinTaskTest extends JS
1358       */
1359      public void testCancelledInvokeSingleton() {
1360          RecursiveAction a = new CheckedRecursiveAction() {
1361 <            public void realCompute() {
1361 >            protected void realCompute() {
1362                  AsyncFib f = new AsyncFib(8);
1363                  assertTrue(f.cancel(true));
1364                  try {
# Line 1359 | Line 1376 | public class ForkJoinTaskTest extends JS
1376       */
1377      public void testCancelledForkJoinSingleton() {
1378          RecursiveAction a = new CheckedRecursiveAction() {
1379 <            public void realCompute() {
1379 >            protected void realCompute() {
1380                  AsyncFib f = new AsyncFib(8);
1381                  assertTrue(f.cancel(true));
1382                  assertSame(f, f.fork());
# Line 1378 | Line 1395 | public class ForkJoinTaskTest extends JS
1395       */
1396      public void testCancelledForkGetSingleton() {
1397          RecursiveAction a = new CheckedRecursiveAction() {
1398 <            public void realCompute() throws Exception {
1398 >            protected void realCompute() throws Exception {
1399                  AsyncFib f = new AsyncFib(8);
1400                  assertTrue(f.cancel(true));
1401                  assertSame(f, f.fork());
# Line 1397 | Line 1414 | public class ForkJoinTaskTest extends JS
1414       */
1415      public void testCancelledForkTimedGetSingleton() throws Exception {
1416          RecursiveAction a = new CheckedRecursiveAction() {
1417 <            public void realCompute() throws Exception {
1417 >            protected void realCompute() throws Exception {
1418                  AsyncFib f = new AsyncFib(8);
1419                  assertTrue(f.cancel(true));
1420                  assertSame(f, f.fork());
# Line 1416 | Line 1433 | public class ForkJoinTaskTest extends JS
1433       */
1434      public void testCancelledForkQuietlyJoinSingleton() {
1435          RecursiveAction a = new CheckedRecursiveAction() {
1436 <            public void realCompute() {
1436 >            protected void realCompute() {
1437                  AsyncFib f = new AsyncFib(8);
1438                  assertTrue(f.cancel(true));
1439                  assertSame(f, f.fork());
# Line 1431 | Line 1448 | public class ForkJoinTaskTest extends JS
1448       */
1449      public void testCompleteExceptionallySingleton() {
1450          RecursiveAction a = new CheckedRecursiveAction() {
1451 <            public void realCompute() {
1451 >            protected void realCompute() {
1452                  AsyncFib f = new AsyncFib(8);
1453                  f.completeExceptionally(new FJException());
1454                  try {
# Line 1449 | Line 1466 | public class ForkJoinTaskTest extends JS
1466       */
1467      public void testInvokeAll2Singleton() {
1468          RecursiveAction a = new CheckedRecursiveAction() {
1469 <            public void realCompute() {
1469 >            protected void realCompute() {
1470                  AsyncFib f = new AsyncFib(8);
1471                  AsyncFib g = new AsyncFib(9);
1472                  invokeAll(f, g);
# Line 1466 | Line 1483 | public class ForkJoinTaskTest extends JS
1483       */
1484      public void testInvokeAll1Singleton() {
1485          RecursiveAction a = new CheckedRecursiveAction() {
1486 <            public void realCompute() {
1486 >            protected void realCompute() {
1487                  AsyncFib f = new AsyncFib(8);
1488                  invokeAll(f);
1489                  checkCompletedNormally(f);
# Line 1480 | Line 1497 | public class ForkJoinTaskTest extends JS
1497       */
1498      public void testInvokeAll3Singleton() {
1499          RecursiveAction a = new CheckedRecursiveAction() {
1500 <            public void realCompute() {
1500 >            protected void realCompute() {
1501                  AsyncFib f = new AsyncFib(8);
1502                  AsyncFib g = new AsyncFib(9);
1503                  AsyncFib h = new AsyncFib(7);
# Line 1500 | Line 1517 | public class ForkJoinTaskTest extends JS
1517       */
1518      public void testInvokeAllCollectionSingleton() {
1519          RecursiveAction a = new CheckedRecursiveAction() {
1520 <            public void realCompute() {
1520 >            protected void realCompute() {
1521                  AsyncFib f = new AsyncFib(8);
1522                  AsyncFib g = new AsyncFib(9);
1523                  AsyncFib h = new AsyncFib(7);
# Line 1519 | Line 1536 | public class ForkJoinTaskTest extends JS
1536          testInvokeOnPool(singletonPool(), a);
1537      }
1538  
1522
1539      /**
1540       * invokeAll(tasks) with any null task throws NPE
1541       */
1542      public void testInvokeAllNPESingleton() {
1543          RecursiveAction a = new CheckedRecursiveAction() {
1544 <            public void realCompute() {
1544 >            protected void realCompute() {
1545                  AsyncFib f = new AsyncFib(8);
1546                  AsyncFib g = new AsyncFib(9);
1547                  AsyncFib h = null;
# Line 1542 | Line 1558 | public class ForkJoinTaskTest extends JS
1558       */
1559      public void testAbnormalInvokeAll2Singleton() {
1560          RecursiveAction a = new CheckedRecursiveAction() {
1561 <            public void realCompute() {
1561 >            protected void realCompute() {
1562                  AsyncFib f = new AsyncFib(8);
1563                  FailingAsyncFib g = new FailingAsyncFib(9);
1564 +                ForkJoinTask[] tasks = { f, g };
1565 +                shuffle(tasks);
1566                  try {
1567 <                    invokeAll(f, g);
1567 >                    invokeAll(tasks);
1568                      shouldThrow();
1569                  } catch (FJException success) {
1570                      checkCompletedAbnormally(g, success);
# Line 1560 | Line 1578 | public class ForkJoinTaskTest extends JS
1578       */
1579      public void testAbnormalInvokeAll1Singleton() {
1580          RecursiveAction a = new CheckedRecursiveAction() {
1581 <            public void realCompute() {
1581 >            protected void realCompute() {
1582                  FailingAsyncFib g = new FailingAsyncFib(9);
1583                  try {
1584                      invokeAll(g);
# Line 1577 | Line 1595 | public class ForkJoinTaskTest extends JS
1595       */
1596      public void testAbnormalInvokeAll3Singleton() {
1597          RecursiveAction a = new CheckedRecursiveAction() {
1598 <            public void realCompute() {
1598 >            protected void realCompute() {
1599                  AsyncFib f = new AsyncFib(8);
1600                  FailingAsyncFib g = new FailingAsyncFib(9);
1601                  AsyncFib h = new AsyncFib(7);
1602 +                ForkJoinTask[] tasks = { f, g, h };
1603 +                shuffle(tasks);
1604                  try {
1605 <                    invokeAll(f, g, h);
1605 >                    invokeAll(tasks);
1606                      shouldThrow();
1607                  } catch (FJException success) {
1608                      checkCompletedAbnormally(g, success);
# Line 1592 | Line 1612 | public class ForkJoinTaskTest extends JS
1612      }
1613  
1614      /**
1615 <     * invokeAll(collection)  throws exception if any task does
1615 >     * invokeAll(collection) throws exception if any task does
1616       */
1617      public void testAbnormalInvokeAllCollectionSingleton() {
1618          RecursiveAction a = new CheckedRecursiveAction() {
1619 <            public void realCompute() {
1619 >            protected void realCompute() {
1620                  FailingAsyncFib f = new FailingAsyncFib(8);
1621                  AsyncFib g = new AsyncFib(9);
1622                  AsyncFib h = new AsyncFib(7);
1623 <                HashSet set = new HashSet();
1624 <                set.add(f);
1605 <                set.add(g);
1606 <                set.add(h);
1623 >                ForkJoinTask[] tasks = { f, g, h };
1624 >                shuffle(tasks);
1625                  try {
1626 <                    invokeAll(set);
1626 >                    invokeAll(Arrays.asList(tasks));
1627                      shouldThrow();
1628                  } catch (FJException success) {
1629                      checkCompletedAbnormally(f, success);
# Line 1614 | Line 1632 | public class ForkJoinTaskTest extends JS
1632          testInvokeOnPool(singletonPool(), a);
1633      }
1634  
1635 +    /**
1636 +     * ForkJoinTask.quietlyComplete returns when task completes
1637 +     * normally without setting a value. The most recent value
1638 +     * established by setRawResult(V) (or null by default) is returned
1639 +     * from invoke.
1640 +     */
1641 +    public void testQuietlyComplete() {
1642 +        RecursiveAction a = new CheckedRecursiveAction() {
1643 +                protected void realCompute() {
1644 +                    AsyncFib f = new AsyncFib(8);
1645 +                    f.quietlyComplete();
1646 +                    assertEquals(8, f.number);
1647 +                    checkCompletedNormally(f);
1648 +                }};
1649 +        testInvokeOnPool(mainPool(), a);
1650 +    }
1651 +
1652 +    /**
1653 +     * adapt(runnable).toString() contains toString of wrapped task
1654 +     */
1655 +    public void testAdapt_Runnable_toString() {
1656 +        if (testImplementationDetails) {
1657 +            Runnable r = () -> {};
1658 +            ForkJoinTask<?> task = ForkJoinTask.adapt(r);
1659 +            assertEquals(
1660 +                identityString(task) + "[Wrapped task = " + r.toString() + "]",
1661 +                task.toString());
1662 +        }
1663 +    }
1664 +
1665 +    /**
1666 +     * adapt(runnable, x).toString() contains toString of wrapped task
1667 +     */
1668 +    public void testAdapt_Runnable_withResult_toString() {
1669 +        if (testImplementationDetails) {
1670 +            Runnable r = () -> {};
1671 +            ForkJoinTask<String> task = ForkJoinTask.adapt(r, "");
1672 +            assertEquals(
1673 +                identityString(task) + "[Wrapped task = " + r.toString() + "]",
1674 +                task.toString());
1675 +        }
1676 +    }
1677 +
1678 +    /**
1679 +     * adapt(callable).toString() contains toString of wrapped task
1680 +     */
1681 +    public void testAdapt_Callable_toString() {
1682 +        if (testImplementationDetails) {
1683 +            Callable<String> c = () -> "";
1684 +            ForkJoinTask<String> task = ForkJoinTask.adapt(c);
1685 +            assertEquals(
1686 +                identityString(task) + "[Wrapped task = " + c.toString() + "]",
1687 +                task.toString());
1688 +        }
1689 +    }
1690   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines