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.19 by jsr166, Mon Sep 27 19:15:16 2010 UTC vs.
Revision 1.53 by jsr166, Wed Aug 16 17:18:34 2017 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;
17 < 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 java.util.HashSet;
21 < 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() {
30          return new TestSuite(ForkJoinTaskTest.class);
31      }
32  
33 +    // Runs with "mainPool" use > 1 thread. singletonPool tests use 1
34 +    static final int mainPoolSize =
35 +        Math.max(2, Runtime.getRuntime().availableProcessors());
36 +
37      private static ForkJoinPool mainPool() {
38 <        return new ForkJoinPool();
38 >        return new ForkJoinPool(mainPoolSize);
39      }
40  
41      private static ForkJoinPool singletonPool() {
# Line 40 | 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());
56              assertFalse(a.isCancelled());
57              assertNull(a.getException());
58 +            assertNull(a.getRawResult());
59  
60              assertNull(pool.invoke(a));
61  
# Line 54 | Line 64 | public class ForkJoinTaskTest extends JS
64              assertFalse(a.isCompletedAbnormally());
65              assertFalse(a.isCancelled());
66              assertNull(a.getException());
67 <        } finally {
68 <            joinPool(pool);
67 >            assertNull(a.getRawResult());
68 >        }
69 >    }
70 >
71 >    void checkNotDone(ForkJoinTask a) {
72 >        assertFalse(a.isDone());
73 >        assertFalse(a.isCompletedNormally());
74 >        assertFalse(a.isCompletedAbnormally());
75 >        assertFalse(a.isCancelled());
76 >        assertNull(a.getException());
77 >        assertNull(a.getRawResult());
78 >
79 >        try {
80 >            a.get(randomExpiredTimeout(), randomTimeUnit());
81 >            shouldThrow();
82 >        } catch (TimeoutException success) {
83 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
84 >    }
85 >
86 >    <T> void checkCompletedNormally(ForkJoinTask<T> a) {
87 >        checkCompletedNormally(a, null);
88 >    }
89 >
90 >    <T> void checkCompletedNormally(ForkJoinTask<T> a, T expected) {
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());
97 >
98 >        {
99 >            Thread.currentThread().interrupt();
100 >            long startTime = System.nanoTime();
101 >            assertSame(expected, a.join());
102 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
103 >            Thread.interrupted();
104 >        }
105 >
106 >        {
107 >            Thread.currentThread().interrupt();
108 >            long startTime = System.nanoTime();
109 >            a.quietlyJoin();        // should be no-op
110 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
111 >            Thread.interrupted();
112          }
113 +
114 +        assertFalse(a.cancel(false));
115 +        assertFalse(a.cancel(true));
116 +        try {
117 +            assertSame(expected, a.get());
118 +            assertSame(expected, a.get(randomTimeout(), randomTimeUnit()));
119 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
120 +    }
121 +
122 +    void checkCancelled(ForkJoinTask a) {
123 +        assertTrue(a.isDone());
124 +        assertTrue(a.isCancelled());
125 +        assertFalse(a.isCompletedNormally());
126 +        assertTrue(a.isCompletedAbnormally());
127 +        assertTrue(a.getException() instanceof CancellationException);
128 +        assertNull(a.getRawResult());
129 +        assertTrue(a.cancel(false));
130 +        assertTrue(a.cancel(true));
131 +
132 +        try {
133 +            Thread.currentThread().interrupt();
134 +            a.join();
135 +            shouldThrow();
136 +        } catch (CancellationException success) {
137 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
138 +        Thread.interrupted();
139 +
140 +        {
141 +            long startTime = System.nanoTime();
142 +            a.quietlyJoin();        // should be no-op
143 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
144 +        }
145 +
146 +        try {
147 +            a.get();
148 +            shouldThrow();
149 +        } catch (CancellationException success) {
150 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
151 +
152 +        try {
153 +            a.get(randomTimeout(), randomTimeUnit());
154 +            shouldThrow();
155 +        } catch (CancellationException success) {
156 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
157 +    }
158 +
159 +    void checkCompletedAbnormally(ForkJoinTask a, Throwable t) {
160 +        assertTrue(a.isDone());
161 +        assertFalse(a.isCancelled());
162 +        assertFalse(a.isCompletedNormally());
163 +        assertTrue(a.isCompletedAbnormally());
164 +        assertSame(t.getClass(), a.getException().getClass());
165 +        assertNull(a.getRawResult());
166 +        assertFalse(a.cancel(false));
167 +        assertFalse(a.cancel(true));
168 +
169 +        try {
170 +            Thread.currentThread().interrupt();
171 +            a.join();
172 +            shouldThrow();
173 +        } catch (Throwable expected) {
174 +            assertSame(t.getClass(), expected.getClass());
175 +        }
176 +        Thread.interrupted();
177 +
178 +        {
179 +            long startTime = System.nanoTime();
180 +            a.quietlyJoin();        // should be no-op
181 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
182 +        }
183 +
184 +        try {
185 +            a.get();
186 +            shouldThrow();
187 +        } catch (ExecutionException success) {
188 +            assertSame(t.getClass(), success.getCause().getClass());
189 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
190 +
191 +        try {
192 +            a.get(randomTimeout(), randomTimeUnit());
193 +            shouldThrow();
194 +        } catch (ExecutionException success) {
195 +            assertSame(t.getClass(), success.getCause().getClass());
196 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
197      }
198  
199      /*
# Line 67 | Line 204 | public class ForkJoinTaskTest extends JS
204       * differently than supplied Recursive forms.
205       */
206  
207 <    static final class FJException extends RuntimeException {
207 >    public static final class FJException extends RuntimeException {
208          FJException() { super(); }
209      }
210  
# Line 78 | 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 115 | 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 136 | Line 281 | public class ForkJoinTaskTest extends JS
281          }
282  
283          public final void completeExceptionally(Throwable ex) {
284 <            BinaryAsyncAction a = this;
140 <            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 192 | 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);
201 <                    r.fork();
202 <                }
203 <                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 211 | Line 352 | public class ForkJoinTaskTest extends JS
352          }
353      }
354  
214
355      static final class FailingAsyncFib extends BinaryAsyncAction {
356          int number;
357          public FailingAsyncFib(int n) {
# Line 221 | 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);
230 <                    r.fork();
231 <                }
232 <                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 243 | Line 380 | public class ForkJoinTaskTest extends JS
380      /**
381       * invoke returns when task completes normally.
382       * isCompletedAbnormally and isCancelled return false for normally
383 <     * completed tasks. getRawResult of a RecursiveAction returns null;
383 >     * completed tasks; getRawResult returns null.
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);
391 <                assertTrue(f.isDone());
255 <                assertFalse(f.isCancelled());
256 <                assertFalse(f.isCompletedAbnormally());
257 <                assertNull(f.getRawResult());
391 >                checkCompletedNormally(f);
392              }};
393          testInvokeOnPool(mainPool(), a);
394      }
# Line 266 | 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);
407 <                assertTrue(f.isDone());
274 <                assertFalse(f.isCancelled());
275 <                assertFalse(f.isCompletedAbnormally());
276 <                assertNull(f.getRawResult());
407 >                checkCompletedNormally(f);
408              }};
409          testInvokeOnPool(mainPool(), a);
410      }
# Line 283 | 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());
421                  assertEquals(21, f.number);
422 <                assertTrue(f.isDone());
292 <                assertNull(f.getRawResult());
422 >                checkCompletedNormally(f);
423              }};
424          testInvokeOnPool(mainPool(), a);
425      }
# Line 299 | 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());
436                  assertEquals(21, f.number);
437 <                assertTrue(f.isDone());
437 >                checkCompletedNormally(f);
438              }};
439          testInvokeOnPool(mainPool(), a);
440      }
# Line 314 | 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));
451                  assertEquals(21, f.number);
452 <                assertTrue(f.isDone());
452 >                checkCompletedNormally(f);
453              }};
454          testInvokeOnPool(mainPool(), a);
455      }
# Line 329 | 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 345 | 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();
482                  assertEquals(21, f.number);
483 <                assertTrue(f.isDone());
483 >                checkCompletedNormally(f);
484              }};
485          testInvokeOnPool(mainPool(), a);
486      }
487  
358
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 <                f.helpQuiesce();
497 >                helpQuiesce();
498                  assertEquals(21, f.number);
370                assertTrue(f.isDone());
499                  assertEquals(0, getQueuedTaskCount());
500 +                checkCompletedNormally(f);
501              }};
502          testInvokeOnPool(mainPool(), a);
503      }
504  
376
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();
514                      shouldThrow();
515 <                } catch (FJException success) {}
515 >                } catch (FJException success) {
516 >                    checkCompletedAbnormally(f, success);
517 >                }
518              }};
519          testInvokeOnPool(mainPool(), a);
520      }
# Line 394 | 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.isDone());
530 >                assertTrue(f.getException() instanceof FJException);
531 >                checkCompletedAbnormally(f, f.getException());
532              }};
533          testInvokeOnPool(mainPool(), a);
534      }
# Line 407 | 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 {
545                      f.join();
546                      shouldThrow();
547 <                } catch (FJException success) {}
547 >                } catch (FJException success) {
548 >                    checkCompletedAbnormally(f, success);
549 >                }
550              }};
551          testInvokeOnPool(mainPool(), a);
552      }
# Line 423 | 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 432 | Line 565 | public class ForkJoinTaskTest extends JS
565                  } catch (ExecutionException success) {
566                      Throwable cause = success.getCause();
567                      assertTrue(cause instanceof FJException);
568 <                    assertTrue(f.isDone());
436 <                    assertTrue(f.isCompletedAbnormally());
437 <                    assertSame(cause, f.getException());
568 >                    checkCompletedAbnormally(f, cause);
569                  }
570              }};
571          testInvokeOnPool(mainPool(), a);
# Line 445 | 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 454 | Line 585 | public class ForkJoinTaskTest extends JS
585                  } catch (ExecutionException success) {
586                      Throwable cause = success.getCause();
587                      assertTrue(cause instanceof FJException);
588 <                    assertTrue(f.isDone());
458 <                    assertTrue(f.isCompletedAbnormally());
459 <                    assertSame(cause, f.getException());
588 >                    checkCompletedAbnormally(f, cause);
589                  }
590              }};
591          testInvokeOnPool(mainPool(), a);
# Line 467 | 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();
474                assertTrue(f.isDone());
475                assertTrue(f.isCompletedAbnormally());
603                  assertTrue(f.getException() instanceof FJException);
604 +                checkCompletedAbnormally(f, f.getException());
605              }};
606          testInvokeOnPool(mainPool(), a);
607      }
# Line 483 | 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 {
618                      f.invoke();
619                      shouldThrow();
620                  } catch (CancellationException success) {
621 <                    assertTrue(f.isDone());
494 <                    assertTrue(f.isCancelled());
495 <                    assertTrue(f.isCompletedAbnormally());
496 <                    assertTrue(f.getException() instanceof CancellationException);
621 >                    checkCancelled(f);
622                  }
623              }};
624          testInvokeOnPool(mainPool(), a);
# Line 504 | 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 512 | Line 637 | public class ForkJoinTaskTest extends JS
637                      f.join();
638                      shouldThrow();
639                  } catch (CancellationException success) {
640 <                    assertTrue(f.isDone());
516 <                    assertTrue(f.isCancelled());
517 <                    assertTrue(f.isCompletedAbnormally());
518 <                    assertTrue(f.getException() instanceof CancellationException);
640 >                    checkCancelled(f);
641                  }
642              }};
643          testInvokeOnPool(mainPool(), a);
# Line 526 | 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 534 | Line 656 | public class ForkJoinTaskTest extends JS
656                      f.get();
657                      shouldThrow();
658                  } catch (CancellationException success) {
659 <                    assertTrue(f.isDone());
538 <                    assertTrue(f.isCancelled());
539 <                    assertTrue(f.isCompletedAbnormally());
540 <                    assertTrue(f.getException() instanceof CancellationException);
659 >                    checkCancelled(f);
660                  }
661              }};
662          testInvokeOnPool(mainPool(), a);
# Line 548 | 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 556 | Line 675 | public class ForkJoinTaskTest extends JS
675                      f.get(LONG_DELAY_MS, MILLISECONDS);
676                      shouldThrow();
677                  } catch (CancellationException success) {
678 <                    assertTrue(f.isDone());
560 <                    assertTrue(f.isCancelled());
561 <                    assertTrue(f.isCompletedAbnormally());
562 <                    assertTrue(f.getException() instanceof CancellationException);
678 >                    checkCancelled(f);
679                  }
680              }};
681          testInvokeOnPool(mainPool(), a);
# Line 570 | 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());
693                  f.quietlyJoin();
694 <                assertTrue(f.isDone());
579 <                assertTrue(f.isCompletedAbnormally());
580 <                assertTrue(f.isCancelled());
581 <                assertTrue(f.getException() instanceof CancellationException);
694 >                checkCancelled(f);
695              }};
696          testInvokeOnPool(mainPool(), a);
697      }
# Line 589 | 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 600 | 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 611 | 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 622 | Line 735 | public class ForkJoinTaskTest extends JS
735       */
736      public void testInForkJoinPool2() {
737          RecursiveAction a = new CheckedRecursiveAction() {
738 <            public void realCompute() {
739 <                assertTrue(!inForkJoinPool());
738 >            protected void realCompute() {
739 >                assertFalse(inForkJoinPool());
740              }};
741          assertNull(a.invoke());
742      }
# Line 633 | 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              }};
753          assertNull(a.invoke());
754      }
# Line 644 | 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 {
765                      f.invoke();
766                      shouldThrow();
767 <                } catch (FJException success) {}
767 >                } catch (FJException success) {
768 >                    checkCompletedAbnormally(f, success);
769 >                }
770 >            }};
771 >        testInvokeOnPool(mainPool(), a);
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      }
# Line 660 | Line 797 | public class ForkJoinTaskTest extends JS
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);
667                assertTrue(f.isDone());
804                  assertEquals(21, f.number);
669                assertTrue(g.isDone());
805                  assertEquals(34, g.number);
806 +                checkCompletedNormally(f);
807 +                checkCompletedNormally(g);
808              }};
809          testInvokeOnPool(mainPool(), a);
810      }
# Line 677 | 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 <                assertTrue(f.isDone());
820 >                checkCompletedNormally(f);
821                  assertEquals(21, f.number);
822              }};
823          testInvokeOnPool(mainPool(), a);
# Line 691 | 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);
835                  invokeAll(f, g, h);
699                assertTrue(f.isDone());
836                  assertEquals(21, f.number);
701                assertTrue(g.isDone());
837                  assertEquals(34, g.number);
703                assertTrue(h.isDone());
838                  assertEquals(13, h.number);
839 +                checkCompletedNormally(f);
840 +                checkCompletedNormally(g);
841 +                checkCompletedNormally(h);
842              }};
843          testInvokeOnPool(mainPool(), a);
844      }
# Line 711 | 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 720 | Line 857 | public class ForkJoinTaskTest extends JS
857                  set.add(g);
858                  set.add(h);
859                  invokeAll(set);
723                assertTrue(f.isDone());
860                  assertEquals(21, f.number);
725                assertTrue(g.isDone());
861                  assertEquals(34, g.number);
727                assertTrue(h.isDone());
862                  assertEquals(13, h.number);
863 +                checkCompletedNormally(f);
864 +                checkCompletedNormally(g);
865 +                checkCompletedNormally(h);
866              }};
867          testInvokeOnPool(mainPool(), a);
868      }
869  
733
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 753 | 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) {}
900 >                } catch (FJException success) {
901 >                    checkCompletedAbnormally(g, success);
902 >                }
903              }};
904          testInvokeOnPool(mainPool(), a);
905      }
# Line 769 | 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);
916                      shouldThrow();
917 <                } catch (FJException success) {}
917 >                } catch (FJException success) {
918 >                    checkCompletedAbnormally(g, success);
919 >                }
920              }};
921          testInvokeOnPool(mainPool(), a);
922      }
# Line 784 | 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) {}
938 >                } catch (FJException success) {
939 >                    checkCompletedAbnormally(g, success);
940 >                }
941              }};
942          testInvokeOnPool(mainPool(), a);
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);
810 <                set.add(g);
811 <                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) {}
959 >                } catch (FJException success) {
960 >                    checkCompletedAbnormally(f, success);
961 >                }
962              }};
963          testInvokeOnPool(mainPool(), a);
964      }
# Line 823 | 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);
976                  assertSame(f, f.fork());
977                  assertTrue(f.tryUnfork());
978                  helpQuiesce();
979 <                assertFalse(f.isDone());
980 <                assertTrue(g.isDone());
979 >                checkNotDone(f);
980 >                checkCompletedNormally(g);
981              }};
982          testInvokeOnPool(singletonPool(), a);
983      }
# Line 842 | 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 851 | Line 997 | public class ForkJoinTaskTest extends JS
997                  assertSame(f, f.fork());
998                  assertTrue(getSurplusQueuedTaskCount() > 0);
999                  helpQuiesce();
1000 +                assertEquals(0, getSurplusQueuedTaskCount());
1001 +                checkCompletedNormally(f);
1002 +                checkCompletedNormally(g);
1003 +                checkCompletedNormally(h);
1004              }};
1005          testInvokeOnPool(singletonPool(), a);
1006      }
# Line 860 | 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);
1017                  assertSame(f, f.fork());
1018                  assertSame(f, peekNextLocalTask());
1019                  assertNull(f.join());
1020 <                assertTrue(f.isDone());
1020 >                checkCompletedNormally(f);
1021                  helpQuiesce();
1022 +                checkCompletedNormally(g);
1023              }};
1024          testInvokeOnPool(singletonPool(), a);
1025      }
1026  
1027      /**
1028 <     * pollNextLocalTask returns most recent unexecuted task
1029 <     * without executing it
1028 >     * pollNextLocalTask returns most recent unexecuted task without
1029 >     * executing it
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);
1037                  assertSame(f, f.fork());
1038                  assertSame(f, pollNextLocalTask());
1039                  helpQuiesce();
1040 <                assertFalse(f.isDone());
1040 >                checkNotDone(f);
1041 >                assertEquals(34, g.number);
1042 >                checkCompletedNormally(g);
1043              }};
1044          testInvokeOnPool(singletonPool(), a);
1045      }
1046  
1047      /**
1048 <     * pollTask returns an unexecuted task
896 <     * without executing it
1048 >     * pollTask returns an unexecuted task without executing it
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);
1056                  assertSame(f, f.fork());
1057                  assertSame(f, pollTask());
1058                  helpQuiesce();
1059 <                assertFalse(f.isDone());
1060 <                assertTrue(g.isDone());
1059 >                checkNotDone(f);
1060 >                checkCompletedNormally(g);
1061              }};
1062          testInvokeOnPool(singletonPool(), a);
1063      }
# Line 915 | 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 923 | Line 1075 | public class ForkJoinTaskTest extends JS
1075                  assertSame(g, peekNextLocalTask());
1076                  assertNull(f.join());
1077                  helpQuiesce();
1078 <                assertTrue(f.isDone());
1078 >                checkCompletedNormally(f);
1079 >                assertEquals(34, g.number);
1080 >                checkCompletedNormally(g);
1081              }};
1082          testInvokeOnPool(asyncSingletonPool(), a);
1083      }
1084  
1085      /**
1086 <     * pollNextLocalTask returns least recent unexecuted task
1087 <     * without executing it, in async mode
1086 >     * pollNextLocalTask returns least recent unexecuted task without
1087 >     * executing it, in async mode
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);
1095                  assertSame(f, f.fork());
1096                  assertSame(g, pollNextLocalTask());
1097                  helpQuiesce();
1098 <                assertTrue(f.isDone());
1099 <                assertFalse(g.isDone());
1098 >                assertEquals(21, f.number);
1099 >                checkCompletedNormally(f);
1100 >                checkNotDone(g);
1101              }};
1102          testInvokeOnPool(asyncSingletonPool(), a);
1103      }
1104  
1105      /**
1106 <     * pollTask returns an unexecuted task
1107 <     * without executing it, in async mode
1106 >     * pollTask returns an unexecuted task without executing it, in
1107 >     * async mode
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);
1115                  assertSame(f, f.fork());
1116                  assertSame(g, pollTask());
1117                  helpQuiesce();
1118 <                assertTrue(f.isDone());
1119 <                assertFalse(g.isDone());
1118 >                assertEquals(21, f.number);
1119 >                checkCompletedNormally(f);
1120 >                checkNotDone(g);
1121              }};
1122          testInvokeOnPool(asyncSingletonPool(), a);
1123      }
1124 +
1125 +    // versions for singleton pools
1126 +
1127 +    /**
1128 +     * invoke returns when task completes normally.
1129 +     * isCompletedAbnormally and isCancelled return false for normally
1130 +     * completed tasks; getRawResult returns null.
1131 +     */
1132 +    public void testInvokeSingleton() {
1133 +        RecursiveAction a = new CheckedRecursiveAction() {
1134 +            protected void realCompute() {
1135 +                AsyncFib f = new AsyncFib(8);
1136 +                assertNull(f.invoke());
1137 +                assertEquals(21, f.number);
1138 +                checkCompletedNormally(f);
1139 +            }};
1140 +        testInvokeOnPool(singletonPool(), a);
1141 +    }
1142 +
1143 +    /**
1144 +     * quietlyInvoke task returns when task completes normally.
1145 +     * isCompletedAbnormally and isCancelled return false for normally
1146 +     * completed tasks
1147 +     */
1148 +    public void testQuietlyInvokeSingleton() {
1149 +        RecursiveAction a = new CheckedRecursiveAction() {
1150 +            protected void realCompute() {
1151 +                AsyncFib f = new AsyncFib(8);
1152 +                f.quietlyInvoke();
1153 +                assertEquals(21, f.number);
1154 +                checkCompletedNormally(f);
1155 +            }};
1156 +        testInvokeOnPool(singletonPool(), a);
1157 +    }
1158 +
1159 +    /**
1160 +     * join of a forked task returns when task completes
1161 +     */
1162 +    public void testForkJoinSingleton() {
1163 +        RecursiveAction a = new CheckedRecursiveAction() {
1164 +            protected void realCompute() {
1165 +                AsyncFib f = new AsyncFib(8);
1166 +                assertSame(f, f.fork());
1167 +                assertNull(f.join());
1168 +                assertEquals(21, f.number);
1169 +                checkCompletedNormally(f);
1170 +            }};
1171 +        testInvokeOnPool(singletonPool(), a);
1172 +    }
1173 +
1174 +    /**
1175 +     * get of a forked task returns when task completes
1176 +     */
1177 +    public void testForkGetSingleton() {
1178 +        RecursiveAction a = new CheckedRecursiveAction() {
1179 +            protected void realCompute() throws Exception {
1180 +                AsyncFib f = new AsyncFib(8);
1181 +                assertSame(f, f.fork());
1182 +                assertNull(f.get());
1183 +                assertEquals(21, f.number);
1184 +                checkCompletedNormally(f);
1185 +            }};
1186 +        testInvokeOnPool(singletonPool(), a);
1187 +    }
1188 +
1189 +    /**
1190 +     * timed get of a forked task returns when task completes
1191 +     */
1192 +    public void testForkTimedGetSingleton() {
1193 +        RecursiveAction a = new CheckedRecursiveAction() {
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));
1198 +                assertEquals(21, f.number);
1199 +                checkCompletedNormally(f);
1200 +            }};
1201 +        testInvokeOnPool(singletonPool(), a);
1202 +    }
1203 +
1204 +    /**
1205 +     * timed get with null time unit throws NPE
1206 +     */
1207 +    public void testForkTimedGetNPESingleton() {
1208 +        RecursiveAction a = new CheckedRecursiveAction() {
1209 +            protected void realCompute() throws Exception {
1210 +                AsyncFib f = new AsyncFib(8);
1211 +                assertSame(f, f.fork());
1212 +                try {
1213 +                    f.get(randomTimeout(), null);
1214 +                    shouldThrow();
1215 +                } catch (NullPointerException success) {}
1216 +            }};
1217 +        testInvokeOnPool(singletonPool(), a);
1218 +    }
1219 +
1220 +    /**
1221 +     * quietlyJoin of a forked task returns when task completes
1222 +     */
1223 +    public void testForkQuietlyJoinSingleton() {
1224 +        RecursiveAction a = new CheckedRecursiveAction() {
1225 +            protected void realCompute() {
1226 +                AsyncFib f = new AsyncFib(8);
1227 +                assertSame(f, f.fork());
1228 +                f.quietlyJoin();
1229 +                assertEquals(21, f.number);
1230 +                checkCompletedNormally(f);
1231 +            }};
1232 +        testInvokeOnPool(singletonPool(), a);
1233 +    }
1234 +
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 +            protected void realCompute() {
1242 +                AsyncFib f = new AsyncFib(8);
1243 +                assertSame(f, f.fork());
1244 +                helpQuiesce();
1245 +                assertEquals(0, getQueuedTaskCount());
1246 +                assertEquals(21, f.number);
1247 +                checkCompletedNormally(f);
1248 +            }};
1249 +        testInvokeOnPool(singletonPool(), a);
1250 +    }
1251 +
1252 +    /**
1253 +     * invoke task throws exception when task completes abnormally
1254 +     */
1255 +    public void testAbnormalInvokeSingleton() {
1256 +        RecursiveAction a = new CheckedRecursiveAction() {
1257 +            protected void realCompute() {
1258 +                FailingAsyncFib f = new FailingAsyncFib(8);
1259 +                try {
1260 +                    f.invoke();
1261 +                    shouldThrow();
1262 +                } catch (FJException success) {
1263 +                    checkCompletedAbnormally(f, success);
1264 +                }
1265 +            }};
1266 +        testInvokeOnPool(singletonPool(), a);
1267 +    }
1268 +
1269 +    /**
1270 +     * quietlyInvoke task returns when task completes abnormally
1271 +     */
1272 +    public void testAbnormalQuietlyInvokeSingleton() {
1273 +        RecursiveAction a = new CheckedRecursiveAction() {
1274 +            protected void realCompute() {
1275 +                FailingAsyncFib f = new FailingAsyncFib(8);
1276 +                f.quietlyInvoke();
1277 +                assertTrue(f.getException() instanceof FJException);
1278 +                checkCompletedAbnormally(f, f.getException());
1279 +            }};
1280 +        testInvokeOnPool(singletonPool(), a);
1281 +    }
1282 +
1283 +    /**
1284 +     * join of a forked task throws exception when task completes abnormally
1285 +     */
1286 +    public void testAbnormalForkJoinSingleton() {
1287 +        RecursiveAction a = new CheckedRecursiveAction() {
1288 +            protected void realCompute() {
1289 +                FailingAsyncFib f = new FailingAsyncFib(8);
1290 +                assertSame(f, f.fork());
1291 +                try {
1292 +                    f.join();
1293 +                    shouldThrow();
1294 +                } catch (FJException success) {
1295 +                    checkCompletedAbnormally(f, success);
1296 +                }
1297 +            }};
1298 +        testInvokeOnPool(singletonPool(), a);
1299 +    }
1300 +
1301 +    /**
1302 +     * get of a forked task throws exception when task completes abnormally
1303 +     */
1304 +    public void testAbnormalForkGetSingleton() {
1305 +        RecursiveAction a = new CheckedRecursiveAction() {
1306 +            protected void realCompute() throws Exception {
1307 +                FailingAsyncFib f = new FailingAsyncFib(8);
1308 +                assertSame(f, f.fork());
1309 +                try {
1310 +                    f.get();
1311 +                    shouldThrow();
1312 +                } catch (ExecutionException success) {
1313 +                    Throwable cause = success.getCause();
1314 +                    assertTrue(cause instanceof FJException);
1315 +                    checkCompletedAbnormally(f, cause);
1316 +                }
1317 +            }};
1318 +        testInvokeOnPool(singletonPool(), a);
1319 +    }
1320 +
1321 +    /**
1322 +     * timed get of a forked task throws exception when task completes abnormally
1323 +     */
1324 +    public void testAbnormalForkTimedGetSingleton() {
1325 +        RecursiveAction a = new CheckedRecursiveAction() {
1326 +            protected void realCompute() throws Exception {
1327 +                FailingAsyncFib f = new FailingAsyncFib(8);
1328 +                assertSame(f, f.fork());
1329 +                try {
1330 +                    f.get(LONG_DELAY_MS, MILLISECONDS);
1331 +                    shouldThrow();
1332 +                } catch (ExecutionException success) {
1333 +                    Throwable cause = success.getCause();
1334 +                    assertTrue(cause instanceof FJException);
1335 +                    checkCompletedAbnormally(f, cause);
1336 +                }
1337 +            }};
1338 +        testInvokeOnPool(singletonPool(), a);
1339 +    }
1340 +
1341 +    /**
1342 +     * quietlyJoin of a forked task returns when task completes abnormally
1343 +     */
1344 +    public void testAbnormalForkQuietlyJoinSingleton() {
1345 +        RecursiveAction a = new CheckedRecursiveAction() {
1346 +            protected void realCompute() {
1347 +                FailingAsyncFib f = new FailingAsyncFib(8);
1348 +                assertSame(f, f.fork());
1349 +                f.quietlyJoin();
1350 +                assertTrue(f.getException() instanceof FJException);
1351 +                checkCompletedAbnormally(f, f.getException());
1352 +            }};
1353 +        testInvokeOnPool(singletonPool(), a);
1354 +    }
1355 +
1356 +    /**
1357 +     * invoke task throws exception when task cancelled
1358 +     */
1359 +    public void testCancelledInvokeSingleton() {
1360 +        RecursiveAction a = new CheckedRecursiveAction() {
1361 +            protected void realCompute() {
1362 +                AsyncFib f = new AsyncFib(8);
1363 +                assertTrue(f.cancel(true));
1364 +                try {
1365 +                    f.invoke();
1366 +                    shouldThrow();
1367 +                } catch (CancellationException success) {
1368 +                    checkCancelled(f);
1369 +                }
1370 +            }};
1371 +        testInvokeOnPool(singletonPool(), a);
1372 +    }
1373 +
1374 +    /**
1375 +     * join of a forked task throws exception when task cancelled
1376 +     */
1377 +    public void testCancelledForkJoinSingleton() {
1378 +        RecursiveAction a = new CheckedRecursiveAction() {
1379 +            protected void realCompute() {
1380 +                AsyncFib f = new AsyncFib(8);
1381 +                assertTrue(f.cancel(true));
1382 +                assertSame(f, f.fork());
1383 +                try {
1384 +                    f.join();
1385 +                    shouldThrow();
1386 +                } catch (CancellationException success) {
1387 +                    checkCancelled(f);
1388 +                }
1389 +            }};
1390 +        testInvokeOnPool(singletonPool(), a);
1391 +    }
1392 +
1393 +    /**
1394 +     * get of a forked task throws exception when task cancelled
1395 +     */
1396 +    public void testCancelledForkGetSingleton() {
1397 +        RecursiveAction a = new CheckedRecursiveAction() {
1398 +            protected void realCompute() throws Exception {
1399 +                AsyncFib f = new AsyncFib(8);
1400 +                assertTrue(f.cancel(true));
1401 +                assertSame(f, f.fork());
1402 +                try {
1403 +                    f.get();
1404 +                    shouldThrow();
1405 +                } catch (CancellationException success) {
1406 +                    checkCancelled(f);
1407 +                }
1408 +            }};
1409 +        testInvokeOnPool(singletonPool(), a);
1410 +    }
1411 +
1412 +    /**
1413 +     * timed get of a forked task throws exception when task cancelled
1414 +     */
1415 +    public void testCancelledForkTimedGetSingleton() throws Exception {
1416 +        RecursiveAction a = new CheckedRecursiveAction() {
1417 +            protected void realCompute() throws Exception {
1418 +                AsyncFib f = new AsyncFib(8);
1419 +                assertTrue(f.cancel(true));
1420 +                assertSame(f, f.fork());
1421 +                try {
1422 +                    f.get(LONG_DELAY_MS, MILLISECONDS);
1423 +                    shouldThrow();
1424 +                } catch (CancellationException success) {
1425 +                    checkCancelled(f);
1426 +                }
1427 +            }};
1428 +        testInvokeOnPool(singletonPool(), a);
1429 +    }
1430 +
1431 +    /**
1432 +     * quietlyJoin of a forked task returns when task cancelled
1433 +     */
1434 +    public void testCancelledForkQuietlyJoinSingleton() {
1435 +        RecursiveAction a = new CheckedRecursiveAction() {
1436 +            protected void realCompute() {
1437 +                AsyncFib f = new AsyncFib(8);
1438 +                assertTrue(f.cancel(true));
1439 +                assertSame(f, f.fork());
1440 +                f.quietlyJoin();
1441 +                checkCancelled(f);
1442 +            }};
1443 +        testInvokeOnPool(singletonPool(), a);
1444 +    }
1445 +
1446 +    /**
1447 +     * invoke task throws exception after invoking completeExceptionally
1448 +     */
1449 +    public void testCompleteExceptionallySingleton() {
1450 +        RecursiveAction a = new CheckedRecursiveAction() {
1451 +            protected void realCompute() {
1452 +                AsyncFib f = new AsyncFib(8);
1453 +                f.completeExceptionally(new FJException());
1454 +                try {
1455 +                    f.invoke();
1456 +                    shouldThrow();
1457 +                } catch (FJException success) {
1458 +                    checkCompletedAbnormally(f, success);
1459 +                }
1460 +            }};
1461 +        testInvokeOnPool(singletonPool(), a);
1462 +    }
1463 +
1464 +    /**
1465 +     * invokeAll(t1, t2) invokes all task arguments
1466 +     */
1467 +    public void testInvokeAll2Singleton() {
1468 +        RecursiveAction a = new CheckedRecursiveAction() {
1469 +            protected void realCompute() {
1470 +                AsyncFib f = new AsyncFib(8);
1471 +                AsyncFib g = new AsyncFib(9);
1472 +                invokeAll(f, g);
1473 +                assertEquals(21, f.number);
1474 +                assertEquals(34, g.number);
1475 +                checkCompletedNormally(f);
1476 +                checkCompletedNormally(g);
1477 +            }};
1478 +        testInvokeOnPool(singletonPool(), a);
1479 +    }
1480 +
1481 +    /**
1482 +     * invokeAll(tasks) with 1 argument invokes task
1483 +     */
1484 +    public void testInvokeAll1Singleton() {
1485 +        RecursiveAction a = new CheckedRecursiveAction() {
1486 +            protected void realCompute() {
1487 +                AsyncFib f = new AsyncFib(8);
1488 +                invokeAll(f);
1489 +                checkCompletedNormally(f);
1490 +                assertEquals(21, f.number);
1491 +            }};
1492 +        testInvokeOnPool(singletonPool(), a);
1493 +    }
1494 +
1495 +    /**
1496 +     * invokeAll(tasks) with > 2 argument invokes tasks
1497 +     */
1498 +    public void testInvokeAll3Singleton() {
1499 +        RecursiveAction a = new CheckedRecursiveAction() {
1500 +            protected void realCompute() {
1501 +                AsyncFib f = new AsyncFib(8);
1502 +                AsyncFib g = new AsyncFib(9);
1503 +                AsyncFib h = new AsyncFib(7);
1504 +                invokeAll(f, g, h);
1505 +                assertEquals(21, f.number);
1506 +                assertEquals(34, g.number);
1507 +                assertEquals(13, h.number);
1508 +                checkCompletedNormally(f);
1509 +                checkCompletedNormally(g);
1510 +                checkCompletedNormally(h);
1511 +            }};
1512 +        testInvokeOnPool(singletonPool(), a);
1513 +    }
1514 +
1515 +    /**
1516 +     * invokeAll(collection) invokes all tasks in the collection
1517 +     */
1518 +    public void testInvokeAllCollectionSingleton() {
1519 +        RecursiveAction a = new CheckedRecursiveAction() {
1520 +            protected void realCompute() {
1521 +                AsyncFib f = new AsyncFib(8);
1522 +                AsyncFib g = new AsyncFib(9);
1523 +                AsyncFib h = new AsyncFib(7);
1524 +                HashSet set = new HashSet();
1525 +                set.add(f);
1526 +                set.add(g);
1527 +                set.add(h);
1528 +                invokeAll(set);
1529 +                assertEquals(21, f.number);
1530 +                assertEquals(34, g.number);
1531 +                assertEquals(13, h.number);
1532 +                checkCompletedNormally(f);
1533 +                checkCompletedNormally(g);
1534 +                checkCompletedNormally(h);
1535 +            }};
1536 +        testInvokeOnPool(singletonPool(), a);
1537 +    }
1538 +
1539 +    /**
1540 +     * invokeAll(tasks) with any null task throws NPE
1541 +     */
1542 +    public void testInvokeAllNPESingleton() {
1543 +        RecursiveAction a = new CheckedRecursiveAction() {
1544 +            protected void realCompute() {
1545 +                AsyncFib f = new AsyncFib(8);
1546 +                AsyncFib g = new AsyncFib(9);
1547 +                AsyncFib h = null;
1548 +                try {
1549 +                    invokeAll(f, g, h);
1550 +                    shouldThrow();
1551 +                } catch (NullPointerException success) {}
1552 +            }};
1553 +        testInvokeOnPool(singletonPool(), a);
1554 +    }
1555 +
1556 +    /**
1557 +     * invokeAll(t1, t2) throw exception if any task does
1558 +     */
1559 +    public void testAbnormalInvokeAll2Singleton() {
1560 +        RecursiveAction a = new CheckedRecursiveAction() {
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(tasks);
1568 +                    shouldThrow();
1569 +                } catch (FJException success) {
1570 +                    checkCompletedAbnormally(g, success);
1571 +                }
1572 +            }};
1573 +        testInvokeOnPool(singletonPool(), a);
1574 +    }
1575 +
1576 +    /**
1577 +     * invokeAll(tasks) with 1 argument throws exception if task does
1578 +     */
1579 +    public void testAbnormalInvokeAll1Singleton() {
1580 +        RecursiveAction a = new CheckedRecursiveAction() {
1581 +            protected void realCompute() {
1582 +                FailingAsyncFib g = new FailingAsyncFib(9);
1583 +                try {
1584 +                    invokeAll(g);
1585 +                    shouldThrow();
1586 +                } catch (FJException success) {
1587 +                    checkCompletedAbnormally(g, success);
1588 +                }
1589 +            }};
1590 +        testInvokeOnPool(singletonPool(), a);
1591 +    }
1592 +
1593 +    /**
1594 +     * invokeAll(tasks) with > 2 argument throws exception if any task does
1595 +     */
1596 +    public void testAbnormalInvokeAll3Singleton() {
1597 +        RecursiveAction a = new CheckedRecursiveAction() {
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(tasks);
1606 +                    shouldThrow();
1607 +                } catch (FJException success) {
1608 +                    checkCompletedAbnormally(g, success);
1609 +                }
1610 +            }};
1611 +        testInvokeOnPool(singletonPool(), a);
1612 +    }
1613 +
1614 +    /**
1615 +     * invokeAll(collection) throws exception if any task does
1616 +     */
1617 +    public void testAbnormalInvokeAllCollectionSingleton() {
1618 +        RecursiveAction a = new CheckedRecursiveAction() {
1619 +            protected void realCompute() {
1620 +                FailingAsyncFib f = new FailingAsyncFib(8);
1621 +                AsyncFib g = new AsyncFib(9);
1622 +                AsyncFib h = new AsyncFib(7);
1623 +                ForkJoinTask[] tasks = { f, g, h };
1624 +                shuffle(tasks);
1625 +                try {
1626 +                    invokeAll(Arrays.asList(tasks));
1627 +                    shouldThrow();
1628 +                } catch (FJException success) {
1629 +                    checkCompletedAbnormally(f, success);
1630 +                }
1631 +            }};
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