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.29 by jsr166, Tue Nov 23 06:33:26 2010 UTC vs.
Revision 1.56 by jsr166, Sun Jul 22 20:47:22 2018 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
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 86 | Line 87 | public class ForkJoinTaskTest extends JS
87          checkCompletedNormally(a, null);
88      }
89  
90 <    <T> void checkCompletedNormally(ForkJoinTask<T> a, T expected) {
90 >    <T> void checkCompletedNormally(ForkJoinTask<T> a, T expectedValue) {
91          assertTrue(a.isDone());
92          assertFalse(a.isCancelled());
93          assertTrue(a.isCompletedNormally());
94          assertFalse(a.isCompletedAbnormally());
95          assertNull(a.getException());
96 <        assertSame(expected, a.getRawResult());
96 >        assertSame(expectedValue, a.getRawResult());
97  
98          {
99              Thread.currentThread().interrupt();
100 <            long t0 = System.nanoTime();
101 <            assertSame(expected, a.join());
102 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
100 >            long startTime = System.nanoTime();
101 >            assertSame(expectedValue, a.join());
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  
114          assertFalse(a.cancel(false));
115          assertFalse(a.cancel(true));
116 +
117 +        T v1 = null, v2 = null;
118          try {
119 <            assertSame(expected, a.get());
120 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
118 <        try {
119 <            assertSame(expected, a.get(5L, SECONDS));
119 >            v1 = a.get();
120 >            v2 = a.get(randomTimeout(), randomTimeUnit());
121          } catch (Throwable fail) { threadUnexpectedException(fail); }
122 +        assertSame(expectedValue, v1);
123 +        assertSame(expectedValue, v2);
124      }
125  
126      void checkCancelled(ForkJoinTask a) {
# Line 139 | Line 142 | public class ForkJoinTaskTest extends JS
142          Thread.interrupted();
143  
144          {
145 <            long t0 = System.nanoTime();
145 >            long startTime = System.nanoTime();
146              a.quietlyJoin();        // should be no-op
147 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
147 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
148          }
149  
150          try {
# Line 151 | Line 154 | public class ForkJoinTaskTest extends JS
154          } catch (Throwable fail) { threadUnexpectedException(fail); }
155  
156          try {
157 <            a.get(5L, SECONDS);
157 >            a.get(randomTimeout(), randomTimeUnit());
158              shouldThrow();
159          } catch (CancellationException success) {
160          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 162 | Line 165 | public class ForkJoinTaskTest extends JS
165          assertFalse(a.isCancelled());
166          assertFalse(a.isCompletedNormally());
167          assertTrue(a.isCompletedAbnormally());
168 <        assertSame(t, a.getException());
168 >        assertSame(t.getClass(), a.getException().getClass());
169          assertNull(a.getRawResult());
170          assertFalse(a.cancel(false));
171          assertFalse(a.cancel(true));
# Line 172 | Line 175 | public class ForkJoinTaskTest extends JS
175              a.join();
176              shouldThrow();
177          } catch (Throwable expected) {
178 <            assertSame(t, expected);
178 >            assertSame(t.getClass(), expected.getClass());
179          }
180          Thread.interrupted();
181  
182          {
183 <            long t0 = System.nanoTime();
183 >            long startTime = System.nanoTime();
184              a.quietlyJoin();        // should be no-op
185 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
185 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
186          }
187  
188          try {
189              a.get();
190              shouldThrow();
191          } catch (ExecutionException success) {
192 <            assertSame(t, success.getCause());
192 >            assertSame(t.getClass(), success.getCause().getClass());
193          } catch (Throwable fail) { threadUnexpectedException(fail); }
194  
195          try {
196 <            a.get(5L, SECONDS);
196 >            a.get(randomTimeout(), randomTimeUnit());
197              shouldThrow();
198          } catch (ExecutionException success) {
199 <            assertSame(t, success.getCause());
199 >            assertSame(t.getClass(), success.getCause().getClass());
200          } catch (Throwable fail) { threadUnexpectedException(fail); }
201      }
202  
# Line 205 | Line 208 | public class ForkJoinTaskTest extends JS
208       * differently than supplied Recursive forms.
209       */
210  
211 <    static final class FJException extends RuntimeException {
211 >    public static final class FJException extends RuntimeException {
212          FJException() { super(); }
213      }
214  
# Line 216 | Line 219 | public class ForkJoinTaskTest extends JS
219              AtomicIntegerFieldUpdater.newUpdater(BinaryAsyncAction.class,
220                                                   "controlState");
221  
222 <        private BinaryAsyncAction parent;
222 >        private volatile BinaryAsyncAction parent;
223  
224 <        private BinaryAsyncAction sibling;
224 >        private volatile BinaryAsyncAction sibling;
225  
226          protected BinaryAsyncAction() {
227          }
# Line 253 | Line 256 | public class ForkJoinTaskTest extends JS
256              super.completeExceptionally(ex);
257          }
258  
259 +        public boolean cancel(boolean mayInterruptIfRunning) {
260 +            if (super.cancel(mayInterruptIfRunning)) {
261 +                completeExceptionally(new FJException());
262 +                return true;
263 +            }
264 +            return false;
265 +        }
266 +
267          public final void complete() {
268              BinaryAsyncAction a = this;
269              for (;;) {
# Line 274 | Line 285 | public class ForkJoinTaskTest extends JS
285          }
286  
287          public final void completeExceptionally(Throwable ex) {
288 <            BinaryAsyncAction a = this;
278 <            while (!a.isCompletedAbnormally()) {
288 >            for (BinaryAsyncAction a = this;;) {
289                  a.completeThisExceptionally(ex);
290                  BinaryAsyncAction s = a.sibling;
291 <                if (s != null)
292 <                    s.cancel(false);
293 <                if (!a.onException() || (a = a.parent) == null)
291 >                if (s != null && !s.isDone())
292 >                    s.completeExceptionally(ex);
293 >                if ((a = a.parent) == null)
294                      break;
295              }
296          }
# Line 330 | Line 340 | public class ForkJoinTaskTest extends JS
340          public final boolean exec() {
341              AsyncFib f = this;
342              int n = f.number;
343 <            if (n > 1) {
344 <                while (n > 1) {
345 <                    AsyncFib p = f;
346 <                    AsyncFib r = new AsyncFib(n - 2);
347 <                    f = new AsyncFib(--n);
348 <                    p.linkSubtasks(r, f);
339 <                    r.fork();
340 <                }
341 <                f.number = n;
343 >            while (n > 1) {
344 >                AsyncFib p = f;
345 >                AsyncFib r = new AsyncFib(n - 2);
346 >                f = new AsyncFib(--n);
347 >                p.linkSubtasks(r, f);
348 >                r.fork();
349              }
350              f.complete();
351              return false;
# Line 349 | Line 356 | public class ForkJoinTaskTest extends JS
356          }
357      }
358  
352
359      static final class FailingAsyncFib extends BinaryAsyncAction {
360          int number;
361          public FailingAsyncFib(int n) {
# Line 359 | Line 365 | public class ForkJoinTaskTest extends JS
365          public final boolean exec() {
366              FailingAsyncFib f = this;
367              int n = f.number;
368 <            if (n > 1) {
369 <                while (n > 1) {
370 <                    FailingAsyncFib p = f;
371 <                    FailingAsyncFib r = new FailingAsyncFib(n - 2);
372 <                    f = new FailingAsyncFib(--n);
373 <                    p.linkSubtasks(r, f);
368 <                    r.fork();
369 <                }
370 <                f.number = n;
368 >            while (n > 1) {
369 >                FailingAsyncFib p = f;
370 >                FailingAsyncFib r = new FailingAsyncFib(n - 2);
371 >                f = new FailingAsyncFib(--n);
372 >                p.linkSubtasks(r, f);
373 >                r.fork();
374              }
375              f.complete();
376              return false;
# Line 385 | Line 388 | public class ForkJoinTaskTest extends JS
388       */
389      public void testInvoke() {
390          RecursiveAction a = new CheckedRecursiveAction() {
391 <            public void realCompute() {
391 >            protected void realCompute() {
392                  AsyncFib f = new AsyncFib(8);
393                  assertNull(f.invoke());
394                  assertEquals(21, f.number);
# Line 401 | Line 404 | public class ForkJoinTaskTest extends JS
404       */
405      public void testQuietlyInvoke() {
406          RecursiveAction a = new CheckedRecursiveAction() {
407 <            public void realCompute() {
407 >            protected void realCompute() {
408                  AsyncFib f = new AsyncFib(8);
409                  f.quietlyInvoke();
410                  assertEquals(21, f.number);
# Line 415 | Line 418 | public class ForkJoinTaskTest extends JS
418       */
419      public void testForkJoin() {
420          RecursiveAction a = new CheckedRecursiveAction() {
421 <            public void realCompute() {
421 >            protected void realCompute() {
422                  AsyncFib f = new AsyncFib(8);
423                  assertSame(f, f.fork());
424                  assertNull(f.join());
# Line 430 | Line 433 | public class ForkJoinTaskTest extends JS
433       */
434      public void testForkGet() {
435          RecursiveAction a = new CheckedRecursiveAction() {
436 <            public void realCompute() throws Exception {
436 >            protected void realCompute() throws Exception {
437                  AsyncFib f = new AsyncFib(8);
438                  assertSame(f, f.fork());
439                  assertNull(f.get());
# Line 445 | Line 448 | public class ForkJoinTaskTest extends JS
448       */
449      public void testForkTimedGet() {
450          RecursiveAction a = new CheckedRecursiveAction() {
451 <            public void realCompute() throws Exception {
451 >            protected void realCompute() throws Exception {
452                  AsyncFib f = new AsyncFib(8);
453                  assertSame(f, f.fork());
454                  assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
# Line 460 | Line 463 | public class ForkJoinTaskTest extends JS
463       */
464      public void testForkTimedGetNPE() {
465          RecursiveAction a = new CheckedRecursiveAction() {
466 <            public void realCompute() throws Exception {
466 >            protected void realCompute() throws Exception {
467                  AsyncFib f = new AsyncFib(8);
468                  assertSame(f, f.fork());
469                  try {
470 <                    f.get(5L, null);
470 >                    f.get(randomTimeout(), null);
471                      shouldThrow();
472                  } catch (NullPointerException success) {}
473              }};
# Line 476 | Line 479 | public class ForkJoinTaskTest extends JS
479       */
480      public void testForkQuietlyJoin() {
481          RecursiveAction a = new CheckedRecursiveAction() {
482 <            public void realCompute() {
482 >            protected void realCompute() {
483                  AsyncFib f = new AsyncFib(8);
484                  assertSame(f, f.fork());
485                  f.quietlyJoin();
# Line 486 | Line 489 | public class ForkJoinTaskTest extends JS
489          testInvokeOnPool(mainPool(), a);
490      }
491  
489
492      /**
493       * helpQuiesce returns when tasks are complete.
494       * getQueuedTaskCount returns 0 when quiescent
495       */
496      public void testForkHelpQuiesce() {
497          RecursiveAction a = new CheckedRecursiveAction() {
498 <            public void realCompute() {
498 >            protected void realCompute() {
499                  AsyncFib f = new AsyncFib(8);
500                  assertSame(f, f.fork());
501 <                f.helpQuiesce();
501 >                helpQuiesce();
502                  assertEquals(21, f.number);
503                  assertEquals(0, getQueuedTaskCount());
504                  checkCompletedNormally(f);
# Line 504 | Line 506 | public class ForkJoinTaskTest extends JS
506          testInvokeOnPool(mainPool(), a);
507      }
508  
507
509      /**
510       * invoke task throws exception when task completes abnormally
511       */
512      public void testAbnormalInvoke() {
513          RecursiveAction a = new CheckedRecursiveAction() {
514 <            public void realCompute() {
514 >            protected void realCompute() {
515                  FailingAsyncFib f = new FailingAsyncFib(8);
516                  try {
517                      f.invoke();
# Line 527 | Line 528 | public class ForkJoinTaskTest extends JS
528       */
529      public void testAbnormalQuietlyInvoke() {
530          RecursiveAction a = new CheckedRecursiveAction() {
531 <            public void realCompute() {
531 >            protected void realCompute() {
532                  FailingAsyncFib f = new FailingAsyncFib(8);
533                  f.quietlyInvoke();
534                  assertTrue(f.getException() instanceof FJException);
# Line 541 | Line 542 | public class ForkJoinTaskTest extends JS
542       */
543      public void testAbnormalForkJoin() {
544          RecursiveAction a = new CheckedRecursiveAction() {
545 <            public void realCompute() {
545 >            protected void realCompute() {
546                  FailingAsyncFib f = new FailingAsyncFib(8);
547                  assertSame(f, f.fork());
548                  try {
# Line 559 | Line 560 | public class ForkJoinTaskTest extends JS
560       */
561      public void testAbnormalForkGet() {
562          RecursiveAction a = new CheckedRecursiveAction() {
563 <            public void realCompute() throws Exception {
563 >            protected void realCompute() throws Exception {
564                  FailingAsyncFib f = new FailingAsyncFib(8);
565                  assertSame(f, f.fork());
566                  try {
# Line 579 | Line 580 | public class ForkJoinTaskTest extends JS
580       */
581      public void testAbnormalForkTimedGet() {
582          RecursiveAction a = new CheckedRecursiveAction() {
583 <            public void realCompute() throws Exception {
583 >            protected void realCompute() throws Exception {
584                  FailingAsyncFib f = new FailingAsyncFib(8);
585                  assertSame(f, f.fork());
586                  try {
# Line 599 | Line 600 | public class ForkJoinTaskTest extends JS
600       */
601      public void testAbnormalForkQuietlyJoin() {
602          RecursiveAction a = new CheckedRecursiveAction() {
603 <            public void realCompute() {
603 >            protected void realCompute() {
604                  FailingAsyncFib f = new FailingAsyncFib(8);
605                  assertSame(f, f.fork());
606                  f.quietlyJoin();
# Line 614 | Line 615 | public class ForkJoinTaskTest extends JS
615       */
616      public void testCancelledInvoke() {
617          RecursiveAction a = new CheckedRecursiveAction() {
618 <            public void realCompute() {
618 >            protected void realCompute() {
619                  AsyncFib f = new AsyncFib(8);
620                  assertTrue(f.cancel(true));
621                  try {
# Line 632 | Line 633 | public class ForkJoinTaskTest extends JS
633       */
634      public void testCancelledForkJoin() {
635          RecursiveAction a = new CheckedRecursiveAction() {
636 <            public void realCompute() {
636 >            protected void realCompute() {
637                  AsyncFib f = new AsyncFib(8);
638                  assertTrue(f.cancel(true));
639                  assertSame(f, f.fork());
# Line 651 | Line 652 | public class ForkJoinTaskTest extends JS
652       */
653      public void testCancelledForkGet() {
654          RecursiveAction a = new CheckedRecursiveAction() {
655 <            public void realCompute() throws Exception {
655 >            protected void realCompute() throws Exception {
656                  AsyncFib f = new AsyncFib(8);
657                  assertTrue(f.cancel(true));
658                  assertSame(f, f.fork());
# Line 670 | Line 671 | public class ForkJoinTaskTest extends JS
671       */
672      public void testCancelledForkTimedGet() throws Exception {
673          RecursiveAction a = new CheckedRecursiveAction() {
674 <            public void realCompute() throws Exception {
674 >            protected void realCompute() throws Exception {
675                  AsyncFib f = new AsyncFib(8);
676                  assertTrue(f.cancel(true));
677                  assertSame(f, f.fork());
# Line 689 | Line 690 | public class ForkJoinTaskTest extends JS
690       */
691      public void testCancelledForkQuietlyJoin() {
692          RecursiveAction a = new CheckedRecursiveAction() {
693 <            public void realCompute() {
693 >            protected void realCompute() {
694                  AsyncFib f = new AsyncFib(8);
695                  assertTrue(f.cancel(true));
696                  assertSame(f, f.fork());
# Line 705 | Line 706 | public class ForkJoinTaskTest extends JS
706      public void testGetPool() {
707          final ForkJoinPool mainPool = mainPool();
708          RecursiveAction a = new CheckedRecursiveAction() {
709 <            public void realCompute() {
709 >            protected void realCompute() {
710                  assertSame(mainPool, getPool());
711              }};
712          testInvokeOnPool(mainPool, a);
# Line 716 | Line 717 | public class ForkJoinTaskTest extends JS
717       */
718      public void testGetPool2() {
719          RecursiveAction a = new CheckedRecursiveAction() {
720 <            public void realCompute() {
720 >            protected void realCompute() {
721                  assertNull(getPool());
722              }};
723          assertNull(a.invoke());
# Line 727 | Line 728 | public class ForkJoinTaskTest extends JS
728       */
729      public void testInForkJoinPool() {
730          RecursiveAction a = new CheckedRecursiveAction() {
731 <            public void realCompute() {
731 >            protected void realCompute() {
732                  assertTrue(inForkJoinPool());
733              }};
734          testInvokeOnPool(mainPool(), a);
# Line 738 | Line 739 | public class ForkJoinTaskTest extends JS
739       */
740      public void testInForkJoinPool2() {
741          RecursiveAction a = new CheckedRecursiveAction() {
742 <            public void realCompute() {
742 >            protected void realCompute() {
743                  assertFalse(inForkJoinPool());
744              }};
745          assertNull(a.invoke());
# Line 749 | Line 750 | public class ForkJoinTaskTest extends JS
750       */
751      public void testSetRawResult() {
752          RecursiveAction a = new CheckedRecursiveAction() {
753 <            public void realCompute() {
753 >            protected void realCompute() {
754                  setRawResult(null);
755                  assertNull(getRawResult());
756              }};
# Line 761 | Line 762 | public class ForkJoinTaskTest extends JS
762       */
763      public void testCompleteExceptionally() {
764          RecursiveAction a = new CheckedRecursiveAction() {
765 <            public void realCompute() {
765 >            protected void realCompute() {
766                  AsyncFib f = new AsyncFib(8);
767                  f.completeExceptionally(new FJException());
768                  try {
# Line 775 | Line 776 | public class ForkJoinTaskTest extends JS
776      }
777  
778      /**
779 +     * completeExceptionally(null) surprisingly has the same effect as
780 +     * completeExceptionally(new RuntimeException())
781 +     */
782 +    public void testCompleteExceptionally_null() {
783 +        RecursiveAction a = new CheckedRecursiveAction() {
784 +            protected void realCompute() {
785 +                AsyncFib f = new AsyncFib(8);
786 +                f.completeExceptionally(null);
787 +                try {
788 +                    f.invoke();
789 +                    shouldThrow();
790 +                } catch (RuntimeException success) {
791 +                    assertSame(success.getClass(), RuntimeException.class);
792 +                    assertNull(success.getCause());
793 +                    checkCompletedAbnormally(f, success);
794 +                }
795 +            }};
796 +        testInvokeOnPool(mainPool(), a);
797 +    }
798 +
799 +    /**
800       * invokeAll(t1, t2) invokes all task arguments
801       */
802      public void testInvokeAll2() {
803          RecursiveAction a = new CheckedRecursiveAction() {
804 <            public void realCompute() {
804 >            protected void realCompute() {
805                  AsyncFib f = new AsyncFib(8);
806                  AsyncFib g = new AsyncFib(9);
807                  invokeAll(f, g);
# Line 796 | Line 818 | public class ForkJoinTaskTest extends JS
818       */
819      public void testInvokeAll1() {
820          RecursiveAction a = new CheckedRecursiveAction() {
821 <            public void realCompute() {
821 >            protected void realCompute() {
822                  AsyncFib f = new AsyncFib(8);
823                  invokeAll(f);
824                  checkCompletedNormally(f);
# Line 810 | Line 832 | public class ForkJoinTaskTest extends JS
832       */
833      public void testInvokeAll3() {
834          RecursiveAction a = new CheckedRecursiveAction() {
835 <            public void realCompute() {
835 >            protected void realCompute() {
836                  AsyncFib f = new AsyncFib(8);
837                  AsyncFib g = new AsyncFib(9);
838                  AsyncFib h = new AsyncFib(7);
# Line 830 | Line 852 | public class ForkJoinTaskTest extends JS
852       */
853      public void testInvokeAllCollection() {
854          RecursiveAction a = new CheckedRecursiveAction() {
855 <            public void realCompute() {
855 >            protected void realCompute() {
856                  AsyncFib f = new AsyncFib(8);
857                  AsyncFib g = new AsyncFib(9);
858                  AsyncFib h = new AsyncFib(7);
# Line 849 | Line 871 | public class ForkJoinTaskTest extends JS
871          testInvokeOnPool(mainPool(), a);
872      }
873  
852
874      /**
875       * invokeAll(tasks) with any null task throws NPE
876       */
877      public void testInvokeAllNPE() {
878          RecursiveAction a = new CheckedRecursiveAction() {
879 <            public void realCompute() {
879 >            protected void realCompute() {
880                  AsyncFib f = new AsyncFib(8);
881                  AsyncFib g = new AsyncFib(9);
882                  AsyncFib h = null;
# Line 872 | Line 893 | public class ForkJoinTaskTest extends JS
893       */
894      public void testAbnormalInvokeAll2() {
895          RecursiveAction a = new CheckedRecursiveAction() {
896 <            public void realCompute() {
896 >            protected void realCompute() {
897                  AsyncFib f = new AsyncFib(8);
898                  FailingAsyncFib g = new FailingAsyncFib(9);
899 +                ForkJoinTask[] tasks = { f, g };
900 +                shuffle(tasks);
901                  try {
902 <                    invokeAll(f, g);
902 >                    invokeAll(tasks);
903                      shouldThrow();
904                  } catch (FJException success) {
905                      checkCompletedAbnormally(g, success);
# Line 890 | Line 913 | public class ForkJoinTaskTest extends JS
913       */
914      public void testAbnormalInvokeAll1() {
915          RecursiveAction a = new CheckedRecursiveAction() {
916 <            public void realCompute() {
916 >            protected void realCompute() {
917                  FailingAsyncFib g = new FailingAsyncFib(9);
918                  try {
919                      invokeAll(g);
# Line 907 | Line 930 | public class ForkJoinTaskTest extends JS
930       */
931      public void testAbnormalInvokeAll3() {
932          RecursiveAction a = new CheckedRecursiveAction() {
933 <            public void realCompute() {
933 >            protected void realCompute() {
934                  AsyncFib f = new AsyncFib(8);
935                  FailingAsyncFib g = new FailingAsyncFib(9);
936                  AsyncFib h = new AsyncFib(7);
937 +                ForkJoinTask[] tasks = { f, g, h };
938 +                shuffle(tasks);
939                  try {
940 <                    invokeAll(f, g, h);
940 >                    invokeAll(tasks);
941                      shouldThrow();
942                  } catch (FJException success) {
943                      checkCompletedAbnormally(g, success);
# Line 922 | Line 947 | public class ForkJoinTaskTest extends JS
947      }
948  
949      /**
950 <     * invokeAll(collection)  throws exception if any task does
950 >     * invokeAll(collection) throws exception if any task does
951       */
952      public void testAbnormalInvokeAllCollection() {
953          RecursiveAction a = new CheckedRecursiveAction() {
954 <            public void realCompute() {
954 >            protected void realCompute() {
955                  FailingAsyncFib f = new FailingAsyncFib(8);
956                  AsyncFib g = new AsyncFib(9);
957                  AsyncFib h = new AsyncFib(7);
958 <                HashSet set = new HashSet();
959 <                set.add(f);
935 <                set.add(g);
936 <                set.add(h);
958 >                ForkJoinTask[] tasks = { f, g, h };
959 >                shuffle(tasks);
960                  try {
961 <                    invokeAll(set);
961 >                    invokeAll(Arrays.asList(tasks));
962                      shouldThrow();
963                  } catch (FJException success) {
964                      checkCompletedAbnormally(f, success);
# Line 950 | Line 973 | public class ForkJoinTaskTest extends JS
973       */
974      public void testTryUnfork() {
975          RecursiveAction a = new CheckedRecursiveAction() {
976 <            public void realCompute() {
976 >            protected void realCompute() {
977                  AsyncFib g = new AsyncFib(9);
978                  assertSame(g, g.fork());
979                  AsyncFib f = new AsyncFib(8);
# Line 969 | Line 992 | public class ForkJoinTaskTest extends JS
992       */
993      public void testGetSurplusQueuedTaskCount() {
994          RecursiveAction a = new CheckedRecursiveAction() {
995 <            public void realCompute() {
995 >            protected void realCompute() {
996                  AsyncFib h = new AsyncFib(7);
997                  assertSame(h, h.fork());
998                  AsyncFib g = new AsyncFib(9);
# Line 991 | Line 1014 | public class ForkJoinTaskTest extends JS
1014       */
1015      public void testPeekNextLocalTask() {
1016          RecursiveAction a = new CheckedRecursiveAction() {
1017 <            public void realCompute() {
1017 >            protected void realCompute() {
1018                  AsyncFib g = new AsyncFib(9);
1019                  assertSame(g, g.fork());
1020                  AsyncFib f = new AsyncFib(8);
# Line 1011 | Line 1034 | public class ForkJoinTaskTest extends JS
1034       */
1035      public void testPollNextLocalTask() {
1036          RecursiveAction a = new CheckedRecursiveAction() {
1037 <            public void realCompute() {
1037 >            protected void realCompute() {
1038                  AsyncFib g = new AsyncFib(9);
1039                  assertSame(g, g.fork());
1040                  AsyncFib f = new AsyncFib(8);
# Line 1030 | Line 1053 | public class ForkJoinTaskTest extends JS
1053       */
1054      public void testPollTask() {
1055          RecursiveAction a = new CheckedRecursiveAction() {
1056 <            public void realCompute() {
1056 >            protected void realCompute() {
1057                  AsyncFib g = new AsyncFib(9);
1058                  assertSame(g, g.fork());
1059                  AsyncFib f = new AsyncFib(8);
# Line 1048 | Line 1071 | public class ForkJoinTaskTest extends JS
1071       */
1072      public void testPeekNextLocalTaskAsync() {
1073          RecursiveAction a = new CheckedRecursiveAction() {
1074 <            public void realCompute() {
1074 >            protected void realCompute() {
1075                  AsyncFib g = new AsyncFib(9);
1076                  assertSame(g, g.fork());
1077                  AsyncFib f = new AsyncFib(8);
# Line 1069 | Line 1092 | public class ForkJoinTaskTest extends JS
1092       */
1093      public void testPollNextLocalTaskAsync() {
1094          RecursiveAction a = new CheckedRecursiveAction() {
1095 <            public void realCompute() {
1095 >            protected void realCompute() {
1096                  AsyncFib g = new AsyncFib(9);
1097                  assertSame(g, g.fork());
1098                  AsyncFib f = new AsyncFib(8);
# Line 1089 | Line 1112 | public class ForkJoinTaskTest extends JS
1112       */
1113      public void testPollTaskAsync() {
1114          RecursiveAction a = new CheckedRecursiveAction() {
1115 <            public void realCompute() {
1115 >            protected void realCompute() {
1116                  AsyncFib g = new AsyncFib(9);
1117                  assertSame(g, g.fork());
1118                  AsyncFib f = new AsyncFib(8);
# Line 1112 | Line 1135 | public class ForkJoinTaskTest extends JS
1135       */
1136      public void testInvokeSingleton() {
1137          RecursiveAction a = new CheckedRecursiveAction() {
1138 <            public void realCompute() {
1138 >            protected void realCompute() {
1139                  AsyncFib f = new AsyncFib(8);
1140                  assertNull(f.invoke());
1141                  assertEquals(21, f.number);
# Line 1128 | Line 1151 | public class ForkJoinTaskTest extends JS
1151       */
1152      public void testQuietlyInvokeSingleton() {
1153          RecursiveAction a = new CheckedRecursiveAction() {
1154 <            public void realCompute() {
1154 >            protected void realCompute() {
1155                  AsyncFib f = new AsyncFib(8);
1156                  f.quietlyInvoke();
1157                  assertEquals(21, f.number);
# Line 1142 | Line 1165 | public class ForkJoinTaskTest extends JS
1165       */
1166      public void testForkJoinSingleton() {
1167          RecursiveAction a = new CheckedRecursiveAction() {
1168 <            public void realCompute() {
1168 >            protected void realCompute() {
1169                  AsyncFib f = new AsyncFib(8);
1170                  assertSame(f, f.fork());
1171                  assertNull(f.join());
# Line 1157 | Line 1180 | public class ForkJoinTaskTest extends JS
1180       */
1181      public void testForkGetSingleton() {
1182          RecursiveAction a = new CheckedRecursiveAction() {
1183 <            public void realCompute() throws Exception {
1183 >            protected void realCompute() throws Exception {
1184                  AsyncFib f = new AsyncFib(8);
1185                  assertSame(f, f.fork());
1186                  assertNull(f.get());
# Line 1172 | Line 1195 | public class ForkJoinTaskTest extends JS
1195       */
1196      public void testForkTimedGetSingleton() {
1197          RecursiveAction a = new CheckedRecursiveAction() {
1198 <            public void realCompute() throws Exception {
1198 >            protected void realCompute() throws Exception {
1199                  AsyncFib f = new AsyncFib(8);
1200                  assertSame(f, f.fork());
1201                  assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
# Line 1187 | Line 1210 | public class ForkJoinTaskTest extends JS
1210       */
1211      public void testForkTimedGetNPESingleton() {
1212          RecursiveAction a = new CheckedRecursiveAction() {
1213 <            public void realCompute() throws Exception {
1213 >            protected void realCompute() throws Exception {
1214                  AsyncFib f = new AsyncFib(8);
1215                  assertSame(f, f.fork());
1216                  try {
1217 <                    f.get(5L, null);
1217 >                    f.get(randomTimeout(), null);
1218                      shouldThrow();
1219                  } catch (NullPointerException success) {}
1220              }};
# Line 1203 | Line 1226 | public class ForkJoinTaskTest extends JS
1226       */
1227      public void testForkQuietlyJoinSingleton() {
1228          RecursiveAction a = new CheckedRecursiveAction() {
1229 <            public void realCompute() {
1229 >            protected void realCompute() {
1230                  AsyncFib f = new AsyncFib(8);
1231                  assertSame(f, f.fork());
1232                  f.quietlyJoin();
# Line 1213 | Line 1236 | public class ForkJoinTaskTest extends JS
1236          testInvokeOnPool(singletonPool(), a);
1237      }
1238  
1216
1239      /**
1240       * helpQuiesce returns when tasks are complete.
1241       * getQueuedTaskCount returns 0 when quiescent
1242       */
1243      public void testForkHelpQuiesceSingleton() {
1244          RecursiveAction a = new CheckedRecursiveAction() {
1245 <            public void realCompute() {
1245 >            protected void realCompute() {
1246                  AsyncFib f = new AsyncFib(8);
1247                  assertSame(f, f.fork());
1248 <                f.helpQuiesce();
1248 >                helpQuiesce();
1249                  assertEquals(0, getQueuedTaskCount());
1250                  assertEquals(21, f.number);
1251                  checkCompletedNormally(f);
# Line 1231 | Line 1253 | public class ForkJoinTaskTest extends JS
1253          testInvokeOnPool(singletonPool(), a);
1254      }
1255  
1234
1256      /**
1257       * invoke task throws exception when task completes abnormally
1258       */
1259      public void testAbnormalInvokeSingleton() {
1260          RecursiveAction a = new CheckedRecursiveAction() {
1261 <            public void realCompute() {
1261 >            protected void realCompute() {
1262                  FailingAsyncFib f = new FailingAsyncFib(8);
1263                  try {
1264                      f.invoke();
# Line 1254 | Line 1275 | public class ForkJoinTaskTest extends JS
1275       */
1276      public void testAbnormalQuietlyInvokeSingleton() {
1277          RecursiveAction a = new CheckedRecursiveAction() {
1278 <            public void realCompute() {
1278 >            protected void realCompute() {
1279                  FailingAsyncFib f = new FailingAsyncFib(8);
1280                  f.quietlyInvoke();
1281                  assertTrue(f.getException() instanceof FJException);
# Line 1268 | Line 1289 | public class ForkJoinTaskTest extends JS
1289       */
1290      public void testAbnormalForkJoinSingleton() {
1291          RecursiveAction a = new CheckedRecursiveAction() {
1292 <            public void realCompute() {
1292 >            protected void realCompute() {
1293                  FailingAsyncFib f = new FailingAsyncFib(8);
1294                  assertSame(f, f.fork());
1295                  try {
# Line 1286 | Line 1307 | public class ForkJoinTaskTest extends JS
1307       */
1308      public void testAbnormalForkGetSingleton() {
1309          RecursiveAction a = new CheckedRecursiveAction() {
1310 <            public void realCompute() throws Exception {
1310 >            protected void realCompute() throws Exception {
1311                  FailingAsyncFib f = new FailingAsyncFib(8);
1312                  assertSame(f, f.fork());
1313                  try {
# Line 1306 | Line 1327 | public class ForkJoinTaskTest extends JS
1327       */
1328      public void testAbnormalForkTimedGetSingleton() {
1329          RecursiveAction a = new CheckedRecursiveAction() {
1330 <            public void realCompute() throws Exception {
1330 >            protected void realCompute() throws Exception {
1331                  FailingAsyncFib f = new FailingAsyncFib(8);
1332                  assertSame(f, f.fork());
1333                  try {
# Line 1326 | Line 1347 | public class ForkJoinTaskTest extends JS
1347       */
1348      public void testAbnormalForkQuietlyJoinSingleton() {
1349          RecursiveAction a = new CheckedRecursiveAction() {
1350 <            public void realCompute() {
1350 >            protected void realCompute() {
1351                  FailingAsyncFib f = new FailingAsyncFib(8);
1352                  assertSame(f, f.fork());
1353                  f.quietlyJoin();
# Line 1341 | Line 1362 | public class ForkJoinTaskTest extends JS
1362       */
1363      public void testCancelledInvokeSingleton() {
1364          RecursiveAction a = new CheckedRecursiveAction() {
1365 <            public void realCompute() {
1365 >            protected void realCompute() {
1366                  AsyncFib f = new AsyncFib(8);
1367                  assertTrue(f.cancel(true));
1368                  try {
# Line 1359 | Line 1380 | public class ForkJoinTaskTest extends JS
1380       */
1381      public void testCancelledForkJoinSingleton() {
1382          RecursiveAction a = new CheckedRecursiveAction() {
1383 <            public void realCompute() {
1383 >            protected void realCompute() {
1384                  AsyncFib f = new AsyncFib(8);
1385                  assertTrue(f.cancel(true));
1386                  assertSame(f, f.fork());
# Line 1378 | Line 1399 | public class ForkJoinTaskTest extends JS
1399       */
1400      public void testCancelledForkGetSingleton() {
1401          RecursiveAction a = new CheckedRecursiveAction() {
1402 <            public void realCompute() throws Exception {
1402 >            protected void realCompute() throws Exception {
1403                  AsyncFib f = new AsyncFib(8);
1404                  assertTrue(f.cancel(true));
1405                  assertSame(f, f.fork());
# Line 1397 | Line 1418 | public class ForkJoinTaskTest extends JS
1418       */
1419      public void testCancelledForkTimedGetSingleton() throws Exception {
1420          RecursiveAction a = new CheckedRecursiveAction() {
1421 <            public void realCompute() throws Exception {
1421 >            protected void realCompute() throws Exception {
1422                  AsyncFib f = new AsyncFib(8);
1423                  assertTrue(f.cancel(true));
1424                  assertSame(f, f.fork());
# Line 1416 | Line 1437 | public class ForkJoinTaskTest extends JS
1437       */
1438      public void testCancelledForkQuietlyJoinSingleton() {
1439          RecursiveAction a = new CheckedRecursiveAction() {
1440 <            public void realCompute() {
1440 >            protected void realCompute() {
1441                  AsyncFib f = new AsyncFib(8);
1442                  assertTrue(f.cancel(true));
1443                  assertSame(f, f.fork());
# Line 1431 | Line 1452 | public class ForkJoinTaskTest extends JS
1452       */
1453      public void testCompleteExceptionallySingleton() {
1454          RecursiveAction a = new CheckedRecursiveAction() {
1455 <            public void realCompute() {
1455 >            protected void realCompute() {
1456                  AsyncFib f = new AsyncFib(8);
1457                  f.completeExceptionally(new FJException());
1458                  try {
# Line 1449 | Line 1470 | public class ForkJoinTaskTest extends JS
1470       */
1471      public void testInvokeAll2Singleton() {
1472          RecursiveAction a = new CheckedRecursiveAction() {
1473 <            public void realCompute() {
1473 >            protected void realCompute() {
1474                  AsyncFib f = new AsyncFib(8);
1475                  AsyncFib g = new AsyncFib(9);
1476                  invokeAll(f, g);
# Line 1466 | Line 1487 | public class ForkJoinTaskTest extends JS
1487       */
1488      public void testInvokeAll1Singleton() {
1489          RecursiveAction a = new CheckedRecursiveAction() {
1490 <            public void realCompute() {
1490 >            protected void realCompute() {
1491                  AsyncFib f = new AsyncFib(8);
1492                  invokeAll(f);
1493                  checkCompletedNormally(f);
# Line 1480 | Line 1501 | public class ForkJoinTaskTest extends JS
1501       */
1502      public void testInvokeAll3Singleton() {
1503          RecursiveAction a = new CheckedRecursiveAction() {
1504 <            public void realCompute() {
1504 >            protected void realCompute() {
1505                  AsyncFib f = new AsyncFib(8);
1506                  AsyncFib g = new AsyncFib(9);
1507                  AsyncFib h = new AsyncFib(7);
# Line 1500 | Line 1521 | public class ForkJoinTaskTest extends JS
1521       */
1522      public void testInvokeAllCollectionSingleton() {
1523          RecursiveAction a = new CheckedRecursiveAction() {
1524 <            public void realCompute() {
1524 >            protected void realCompute() {
1525                  AsyncFib f = new AsyncFib(8);
1526                  AsyncFib g = new AsyncFib(9);
1527                  AsyncFib h = new AsyncFib(7);
# Line 1519 | Line 1540 | public class ForkJoinTaskTest extends JS
1540          testInvokeOnPool(singletonPool(), a);
1541      }
1542  
1522
1543      /**
1544       * invokeAll(tasks) with any null task throws NPE
1545       */
1546      public void testInvokeAllNPESingleton() {
1547          RecursiveAction a = new CheckedRecursiveAction() {
1548 <            public void realCompute() {
1548 >            protected void realCompute() {
1549                  AsyncFib f = new AsyncFib(8);
1550                  AsyncFib g = new AsyncFib(9);
1551                  AsyncFib h = null;
# Line 1542 | Line 1562 | public class ForkJoinTaskTest extends JS
1562       */
1563      public void testAbnormalInvokeAll2Singleton() {
1564          RecursiveAction a = new CheckedRecursiveAction() {
1565 <            public void realCompute() {
1565 >            protected void realCompute() {
1566                  AsyncFib f = new AsyncFib(8);
1567                  FailingAsyncFib g = new FailingAsyncFib(9);
1568 +                ForkJoinTask[] tasks = { f, g };
1569 +                shuffle(tasks);
1570                  try {
1571 <                    invokeAll(f, g);
1571 >                    invokeAll(tasks);
1572                      shouldThrow();
1573                  } catch (FJException success) {
1574                      checkCompletedAbnormally(g, success);
# Line 1560 | Line 1582 | public class ForkJoinTaskTest extends JS
1582       */
1583      public void testAbnormalInvokeAll1Singleton() {
1584          RecursiveAction a = new CheckedRecursiveAction() {
1585 <            public void realCompute() {
1585 >            protected void realCompute() {
1586                  FailingAsyncFib g = new FailingAsyncFib(9);
1587                  try {
1588                      invokeAll(g);
# Line 1577 | Line 1599 | public class ForkJoinTaskTest extends JS
1599       */
1600      public void testAbnormalInvokeAll3Singleton() {
1601          RecursiveAction a = new CheckedRecursiveAction() {
1602 <            public void realCompute() {
1602 >            protected void realCompute() {
1603                  AsyncFib f = new AsyncFib(8);
1604                  FailingAsyncFib g = new FailingAsyncFib(9);
1605                  AsyncFib h = new AsyncFib(7);
1606 +                ForkJoinTask[] tasks = { f, g, h };
1607 +                shuffle(tasks);
1608                  try {
1609 <                    invokeAll(f, g, h);
1609 >                    invokeAll(tasks);
1610                      shouldThrow();
1611                  } catch (FJException success) {
1612                      checkCompletedAbnormally(g, success);
# Line 1592 | Line 1616 | public class ForkJoinTaskTest extends JS
1616      }
1617  
1618      /**
1619 <     * invokeAll(collection)  throws exception if any task does
1619 >     * invokeAll(collection) throws exception if any task does
1620       */
1621      public void testAbnormalInvokeAllCollectionSingleton() {
1622          RecursiveAction a = new CheckedRecursiveAction() {
1623 <            public void realCompute() {
1623 >            protected void realCompute() {
1624                  FailingAsyncFib f = new FailingAsyncFib(8);
1625                  AsyncFib g = new AsyncFib(9);
1626                  AsyncFib h = new AsyncFib(7);
1627 <                HashSet set = new HashSet();
1628 <                set.add(f);
1605 <                set.add(g);
1606 <                set.add(h);
1627 >                ForkJoinTask[] tasks = { f, g, h };
1628 >                shuffle(tasks);
1629                  try {
1630 <                    invokeAll(set);
1630 >                    invokeAll(Arrays.asList(tasks));
1631                      shouldThrow();
1632                  } catch (FJException success) {
1633                      checkCompletedAbnormally(f, success);
# Line 1614 | Line 1636 | public class ForkJoinTaskTest extends JS
1636          testInvokeOnPool(singletonPool(), a);
1637      }
1638  
1639 +    /**
1640 +     * ForkJoinTask.quietlyComplete returns when task completes
1641 +     * normally without setting a value. The most recent value
1642 +     * established by setRawResult(V) (or null by default) is returned
1643 +     * from invoke.
1644 +     */
1645 +    public void testQuietlyComplete() {
1646 +        RecursiveAction a = new CheckedRecursiveAction() {
1647 +                protected void realCompute() {
1648 +                    AsyncFib f = new AsyncFib(8);
1649 +                    f.quietlyComplete();
1650 +                    assertEquals(8, f.number);
1651 +                    checkCompletedNormally(f);
1652 +                }};
1653 +        testInvokeOnPool(mainPool(), a);
1654 +    }
1655 +
1656 +    /**
1657 +     * adapt(runnable).toString() contains toString of wrapped task
1658 +     */
1659 +    public void testAdapt_Runnable_toString() {
1660 +        if (testImplementationDetails) {
1661 +            Runnable r = () -> {};
1662 +            ForkJoinTask<?> task = ForkJoinTask.adapt(r);
1663 +            assertEquals(
1664 +                identityString(task) + "[Wrapped task = " + r.toString() + "]",
1665 +                task.toString());
1666 +        }
1667 +    }
1668 +
1669 +    /**
1670 +     * adapt(runnable, x).toString() contains toString of wrapped task
1671 +     */
1672 +    public void testAdapt_Runnable_withResult_toString() {
1673 +        if (testImplementationDetails) {
1674 +            Runnable r = () -> {};
1675 +            ForkJoinTask<String> task = ForkJoinTask.adapt(r, "");
1676 +            assertEquals(
1677 +                identityString(task) + "[Wrapped task = " + r.toString() + "]",
1678 +                task.toString());
1679 +        }
1680 +    }
1681 +
1682 +    /**
1683 +     * adapt(callable).toString() contains toString of wrapped task
1684 +     */
1685 +    public void testAdapt_Callable_toString() {
1686 +        if (testImplementationDetails) {
1687 +            Callable<String> c = () -> "";
1688 +            ForkJoinTask<String> task = ForkJoinTask.adapt(c);
1689 +            assertEquals(
1690 +                identityString(task) + "[Wrapped task = " + c.toString() + "]",
1691 +                task.toString());
1692 +        }
1693 +    }
1694   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines