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.13 by jsr166, Mon Sep 13 07:26:30 2010 UTC vs.
Revision 1.46 by dl, Sun Oct 11 13:30:30 2015 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 > import static java.util.concurrent.TimeUnit.SECONDS;
9 >
10 > import java.util.Arrays;
11 > import java.util.Collections;
12 > import java.util.HashSet;
13 > import java.util.List;
14   import java.util.concurrent.CancellationException;
15 + import java.util.concurrent.ExecutionException;
16   import java.util.concurrent.ForkJoinPool;
17   import java.util.concurrent.ForkJoinTask;
10 import java.util.concurrent.ForkJoinWorkerThread;
18   import java.util.concurrent.RecursiveAction;
19 < import java.util.concurrent.TimeUnit;
20 < import java.util.concurrent.atomic.*;
21 < import java.util.*;
22 < import junit.framework.*;
19 > import java.util.concurrent.TimeoutException;
20 > import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
21 >
22 > import junit.framework.Test;
23 > import junit.framework.TestSuite;
24  
25   public class ForkJoinTaskTest extends JSR166TestCase {
26  
27      public static void main(String[] args) {
28 <        junit.textui.TestRunner.run(suite());
28 >        main(suite(), args);
29      }
30  
31      public static Test suite() {
32          return new TestSuite(ForkJoinTaskTest.class);
33      }
34  
35 <    /**
35 >    // Runs with "mainPool" use > 1 thread. singletonPool tests use 1
36 >    static final int mainPoolSize =
37 >        Math.max(2, Runtime.getRuntime().availableProcessors());
38 >
39 >    private static ForkJoinPool mainPool() {
40 >        return new ForkJoinPool(mainPoolSize);
41 >    }
42 >
43 >    private static ForkJoinPool singletonPool() {
44 >        return new ForkJoinPool(1);
45 >    }
46 >
47 >    private static ForkJoinPool asyncSingletonPool() {
48 >        return new ForkJoinPool(1,
49 >                                ForkJoinPool.defaultForkJoinWorkerThreadFactory,
50 >                                null, true);
51 >    }
52 >
53 >    private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
54 >        try (PoolCleaner cleaner = cleaner(pool)) {
55 >            assertFalse(a.isDone());
56 >            assertFalse(a.isCompletedNormally());
57 >            assertFalse(a.isCompletedAbnormally());
58 >            assertFalse(a.isCancelled());
59 >            assertNull(a.getException());
60 >            assertNull(a.getRawResult());
61 >
62 >            assertNull(pool.invoke(a));
63 >
64 >            assertTrue(a.isDone());
65 >            assertTrue(a.isCompletedNormally());
66 >            assertFalse(a.isCompletedAbnormally());
67 >            assertFalse(a.isCancelled());
68 >            assertNull(a.getException());
69 >            assertNull(a.getRawResult());
70 >        }
71 >    }
72 >
73 >    void checkNotDone(ForkJoinTask a) {
74 >        assertFalse(a.isDone());
75 >        assertFalse(a.isCompletedNormally());
76 >        assertFalse(a.isCompletedAbnormally());
77 >        assertFalse(a.isCancelled());
78 >        assertNull(a.getException());
79 >        assertNull(a.getRawResult());
80 >
81 >        try {
82 >            a.get(0L, SECONDS);
83 >            shouldThrow();
84 >        } catch (TimeoutException success) {
85 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
86 >    }
87 >
88 >    <T> void checkCompletedNormally(ForkJoinTask<T> a) {
89 >        checkCompletedNormally(a, null);
90 >    }
91 >
92 >    <T> void checkCompletedNormally(ForkJoinTask<T> a, T expected) {
93 >        assertTrue(a.isDone());
94 >        assertFalse(a.isCancelled());
95 >        assertTrue(a.isCompletedNormally());
96 >        assertFalse(a.isCompletedAbnormally());
97 >        assertNull(a.getException());
98 >        assertSame(expected, a.getRawResult());
99 >
100 >        {
101 >            Thread.currentThread().interrupt();
102 >            long startTime = System.nanoTime();
103 >            assertSame(expected, a.join());
104 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
105 >            Thread.interrupted();
106 >        }
107 >
108 >        {
109 >            Thread.currentThread().interrupt();
110 >            long startTime = System.nanoTime();
111 >            a.quietlyJoin();        // should be no-op
112 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
113 >            Thread.interrupted();
114 >        }
115 >
116 >        assertFalse(a.cancel(false));
117 >        assertFalse(a.cancel(true));
118 >        try {
119 >            assertSame(expected, a.get());
120 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
121 >        try {
122 >            assertSame(expected, a.get(5L, SECONDS));
123 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
124 >    }
125 >
126 >    void checkCancelled(ForkJoinTask a) {
127 >        assertTrue(a.isDone());
128 >        assertTrue(a.isCancelled());
129 >        assertFalse(a.isCompletedNormally());
130 >        assertTrue(a.isCompletedAbnormally());
131 >        assertTrue(a.getException() instanceof CancellationException);
132 >        assertNull(a.getRawResult());
133 >        assertTrue(a.cancel(false));
134 >        assertTrue(a.cancel(true));
135 >
136 >        try {
137 >            Thread.currentThread().interrupt();
138 >            a.join();
139 >            shouldThrow();
140 >        } catch (CancellationException success) {
141 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
142 >        Thread.interrupted();
143 >
144 >        {
145 >            long startTime = System.nanoTime();
146 >            a.quietlyJoin();        // should be no-op
147 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
148 >        }
149 >
150 >        try {
151 >            a.get();
152 >            shouldThrow();
153 >        } catch (CancellationException success) {
154 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
155 >
156 >        try {
157 >            a.get(5L, SECONDS);
158 >            shouldThrow();
159 >        } catch (CancellationException success) {
160 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
161 >    }
162 >
163 >    void checkCompletedAbnormally(ForkJoinTask a, Throwable t) {
164 >        assertTrue(a.isDone());
165 >        assertFalse(a.isCancelled());
166 >        assertFalse(a.isCompletedNormally());
167 >        assertTrue(a.isCompletedAbnormally());
168 >        assertSame(t.getClass(), a.getException().getClass());
169 >        assertNull(a.getRawResult());
170 >        assertFalse(a.cancel(false));
171 >        assertFalse(a.cancel(true));
172 >
173 >        try {
174 >            Thread.currentThread().interrupt();
175 >            a.join();
176 >            shouldThrow();
177 >        } catch (Throwable expected) {
178 >            assertSame(t.getClass(), expected.getClass());
179 >        }
180 >        Thread.interrupted();
181 >
182 >        {
183 >            long startTime = System.nanoTime();
184 >            a.quietlyJoin();        // should be no-op
185 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
186 >        }
187 >
188 >        try {
189 >            a.get();
190 >            shouldThrow();
191 >        } catch (ExecutionException success) {
192 >            assertSame(t.getClass(), success.getCause().getClass());
193 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
194 >
195 >        try {
196 >            a.get(5L, SECONDS);
197 >            shouldThrow();
198 >        } catch (ExecutionException success) {
199 >            assertSame(t.getClass(), success.getCause().getClass());
200 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
201 >    }
202 >
203 >    /*
204       * Testing coverage notes:
205       *
206       * To test extension methods and overrides, most tests use
# Line 32 | Line 208 | public class ForkJoinTaskTest extends JS
208       * differently than supplied Recursive forms.
209       */
210  
211 <    static final ForkJoinPool mainPool = new ForkJoinPool();
36 <    static final ForkJoinPool singletonPool = new ForkJoinPool(1);
37 <    static final ForkJoinPool asyncSingletonPool =
38 <        new ForkJoinPool(1, ForkJoinPool.defaultForkJoinWorkerThreadFactory,
39 <                         null, true);
40 <    static final class FJException extends RuntimeException {
211 >    public static final class FJException extends RuntimeException {
212          FJException() { super(); }
213      }
214  
215 <    static abstract class BinaryAsyncAction extends ForkJoinTask<Void> {
215 >    abstract static class BinaryAsyncAction extends ForkJoinTask<Void> {
216          private volatile int controlState;
217  
218          static final AtomicIntegerFieldUpdater<BinaryAsyncAction> controlStateUpdater =
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 85 | 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 106 | Line 285 | public class ForkJoinTaskTest extends JS
285          }
286  
287          public final void completeExceptionally(Throwable ex) {
288 <            BinaryAsyncAction a = this;
110 <            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 181 | Line 359 | public class ForkJoinTaskTest extends JS
359          }
360      }
361  
184
362      static final class FailingAsyncFib extends BinaryAsyncAction {
363          int number;
364          public FailingAsyncFib(int n) {
# Line 213 | Line 390 | public class ForkJoinTaskTest extends JS
390      /**
391       * invoke returns when task completes normally.
392       * isCompletedAbnormally and isCancelled return false for normally
393 <     * completed tasks. getRawResult of a RecursiveAction returns null;
393 >     * completed tasks; getRawResult returns null.
394       */
395      public void testInvoke() {
396 <        RecursiveAction a = new RecursiveAction() {
397 <            public void compute() {
396 >        RecursiveAction a = new CheckedRecursiveAction() {
397 >            protected void realCompute() {
398                  AsyncFib f = new AsyncFib(8);
399 <                f.invoke();
400 <                threadAssertTrue(f.number == 21);
401 <                threadAssertTrue(f.isDone());
225 <                threadAssertFalse(f.isCancelled());
226 <                threadAssertFalse(f.isCompletedAbnormally());
227 <                threadAssertTrue(f.getRawResult() == null);
399 >                assertNull(f.invoke());
400 >                assertEquals(21, f.number);
401 >                checkCompletedNormally(f);
402              }};
403 <        mainPool.invoke(a);
403 >        testInvokeOnPool(mainPool(), a);
404      }
405  
406      /**
# Line 235 | Line 409 | public class ForkJoinTaskTest extends JS
409       * completed tasks
410       */
411      public void testQuietlyInvoke() {
412 <        RecursiveAction a = new RecursiveAction() {
413 <            public void compute() {
412 >        RecursiveAction a = new CheckedRecursiveAction() {
413 >            protected void realCompute() {
414                  AsyncFib f = new AsyncFib(8);
415                  f.quietlyInvoke();
416 <                threadAssertTrue(f.number == 21);
417 <                threadAssertTrue(f.isDone());
244 <                threadAssertFalse(f.isCancelled());
245 <                threadAssertFalse(f.isCompletedAbnormally());
246 <                threadAssertTrue(f.getRawResult() == null);
416 >                assertEquals(21, f.number);
417 >                checkCompletedNormally(f);
418              }};
419 <        mainPool.invoke(a);
419 >        testInvokeOnPool(mainPool(), a);
420      }
421  
422      /**
423       * join of a forked task returns when task completes
424       */
425      public void testForkJoin() {
426 <        RecursiveAction a = new RecursiveAction() {
427 <            public void compute() {
426 >        RecursiveAction a = new CheckedRecursiveAction() {
427 >            protected void realCompute() {
428                  AsyncFib f = new AsyncFib(8);
429 <                f.fork();
430 <                f.join();
431 <                threadAssertTrue(f.number == 21);
432 <                threadAssertTrue(f.isDone());
262 <                threadAssertTrue(f.getRawResult() == null);
429 >                assertSame(f, f.fork());
430 >                assertNull(f.join());
431 >                assertEquals(21, f.number);
432 >                checkCompletedNormally(f);
433              }};
434 <        mainPool.invoke(a);
434 >        testInvokeOnPool(mainPool(), a);
435      }
436  
437      /**
438       * get of a forked task returns when task completes
439       */
440      public void testForkGet() {
441 <        RecursiveAction a = new RecursiveAction() {
442 <            public void compute() {
443 <                try {
444 <                    AsyncFib f = new AsyncFib(8);
445 <                    f.fork();
446 <                    f.get();
447 <                    threadAssertTrue(f.number == 21);
278 <                    threadAssertTrue(f.isDone());
279 <                } catch (Exception ex) {
280 <                    unexpectedException(ex);
281 <                }
441 >        RecursiveAction a = new CheckedRecursiveAction() {
442 >            protected void realCompute() throws Exception {
443 >                AsyncFib f = new AsyncFib(8);
444 >                assertSame(f, f.fork());
445 >                assertNull(f.get());
446 >                assertEquals(21, f.number);
447 >                checkCompletedNormally(f);
448              }};
449 <        mainPool.invoke(a);
449 >        testInvokeOnPool(mainPool(), a);
450      }
451  
452      /**
453       * timed get of a forked task returns when task completes
454       */
455      public void testForkTimedGet() {
456 <        RecursiveAction a = new RecursiveAction() {
457 <            public void compute() {
458 <                try {
459 <                    AsyncFib f = new AsyncFib(8);
460 <                    f.fork();
461 <                    f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
462 <                    threadAssertTrue(f.number == 21);
297 <                    threadAssertTrue(f.isDone());
298 <                } catch (Exception ex) {
299 <                    unexpectedException(ex);
300 <                }
456 >        RecursiveAction a = new CheckedRecursiveAction() {
457 >            protected void realCompute() throws Exception {
458 >                AsyncFib f = new AsyncFib(8);
459 >                assertSame(f, f.fork());
460 >                assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
461 >                assertEquals(21, f.number);
462 >                checkCompletedNormally(f);
463              }};
464 <        mainPool.invoke(a);
464 >        testInvokeOnPool(mainPool(), a);
465      }
466  
467      /**
468       * timed get with null time unit throws NPE
469       */
470      public void testForkTimedGetNPE() {
471 <        RecursiveAction a = new RecursiveAction() {
472 <            public void compute() {
471 >        RecursiveAction a = new CheckedRecursiveAction() {
472 >            protected void realCompute() throws Exception {
473 >                AsyncFib f = new AsyncFib(8);
474 >                assertSame(f, f.fork());
475                  try {
312                    AsyncFib f = new AsyncFib(8);
313                    f.fork();
476                      f.get(5L, null);
477                      shouldThrow();
478 <                } catch (NullPointerException success) {
317 <                } catch (Exception ex) {
318 <                    unexpectedException(ex);
319 <                }
478 >                } catch (NullPointerException success) {}
479              }};
480 <        mainPool.invoke(a);
480 >        testInvokeOnPool(mainPool(), a);
481      }
482  
483      /**
484       * quietlyJoin of a forked task returns when task completes
485       */
486      public void testForkQuietlyJoin() {
487 <        RecursiveAction a = new RecursiveAction() {
488 <            public void compute() {
487 >        RecursiveAction a = new CheckedRecursiveAction() {
488 >            protected void realCompute() {
489                  AsyncFib f = new AsyncFib(8);
490 <                f.fork();
490 >                assertSame(f, f.fork());
491                  f.quietlyJoin();
492 <                threadAssertTrue(f.number == 21);
493 <                threadAssertTrue(f.isDone());
492 >                assertEquals(21, f.number);
493 >                checkCompletedNormally(f);
494              }};
495 <        mainPool.invoke(a);
495 >        testInvokeOnPool(mainPool(), a);
496      }
497  
339
498      /**
499       * helpQuiesce returns when tasks are complete.
500       * getQueuedTaskCount returns 0 when quiescent
501       */
502      public void testForkHelpQuiesce() {
503 <        RecursiveAction a = new RecursiveAction() {
504 <            public void compute() {
503 >        RecursiveAction a = new CheckedRecursiveAction() {
504 >            protected void realCompute() {
505                  AsyncFib f = new AsyncFib(8);
506 <                f.fork();
507 <                f.helpQuiesce();
508 <                threadAssertTrue(f.number == 21);
509 <                threadAssertTrue(f.isDone());
510 <                threadAssertTrue(getQueuedTaskCount() == 0);
506 >                assertSame(f, f.fork());
507 >                helpQuiesce();
508 >                assertEquals(21, f.number);
509 >                assertEquals(0, getQueuedTaskCount());
510 >                checkCompletedNormally(f);
511              }};
512 <        mainPool.invoke(a);
512 >        testInvokeOnPool(mainPool(), a);
513      }
514  
357
515      /**
516       * invoke task throws exception when task completes abnormally
517       */
518      public void testAbnormalInvoke() {
519 <        RecursiveAction a = new RecursiveAction() {
520 <            public void compute() {
519 >        RecursiveAction a = new CheckedRecursiveAction() {
520 >            protected void realCompute() {
521 >                FailingAsyncFib f = new FailingAsyncFib(8);
522                  try {
365                    FailingAsyncFib f = new FailingAsyncFib(8);
523                      f.invoke();
524                      shouldThrow();
525                  } catch (FJException success) {
526 +                    checkCompletedAbnormally(f, success);
527                  }
528              }};
529 <        mainPool.invoke(a);
529 >        testInvokeOnPool(mainPool(), a);
530      }
531  
532      /**
533       * quietlyInvoke task returns when task completes abnormally
534       */
535      public void testAbnormalQuietlyInvoke() {
536 <        RecursiveAction a = new RecursiveAction() {
537 <            public void compute() {
536 >        RecursiveAction a = new CheckedRecursiveAction() {
537 >            protected void realCompute() {
538                  FailingAsyncFib f = new FailingAsyncFib(8);
539                  f.quietlyInvoke();
540 <                threadAssertTrue(f.isDone());
540 >                assertTrue(f.getException() instanceof FJException);
541 >                checkCompletedAbnormally(f, f.getException());
542              }};
543 <        mainPool.invoke(a);
543 >        testInvokeOnPool(mainPool(), a);
544      }
545  
546      /**
547       * join of a forked task throws exception when task completes abnormally
548       */
549      public void testAbnormalForkJoin() {
550 <        RecursiveAction a = new RecursiveAction() {
551 <            public void compute() {
550 >        RecursiveAction a = new CheckedRecursiveAction() {
551 >            protected void realCompute() {
552 >                FailingAsyncFib f = new FailingAsyncFib(8);
553 >                assertSame(f, f.fork());
554                  try {
394                    FailingAsyncFib f = new FailingAsyncFib(8);
395                    f.fork();
555                      f.join();
556                      shouldThrow();
557                  } catch (FJException success) {
558 +                    checkCompletedAbnormally(f, success);
559                  }
560              }};
561 <        mainPool.invoke(a);
561 >        testInvokeOnPool(mainPool(), a);
562      }
563  
564      /**
565       * get of a forked task throws exception when task completes abnormally
566       */
567      public void testAbnormalForkGet() {
568 <        RecursiveAction a = new RecursiveAction() {
569 <            public void compute() {
568 >        RecursiveAction a = new CheckedRecursiveAction() {
569 >            protected void realCompute() throws Exception {
570 >                FailingAsyncFib f = new FailingAsyncFib(8);
571 >                assertSame(f, f.fork());
572                  try {
411                    FailingAsyncFib f = new FailingAsyncFib(8);
412                    f.fork();
573                      f.get();
574                      shouldThrow();
575                  } catch (ExecutionException success) {
576 <                } catch (Exception ex) {
577 <                    unexpectedException(ex);
576 >                    Throwable cause = success.getCause();
577 >                    assertTrue(cause instanceof FJException);
578 >                    checkCompletedAbnormally(f, cause);
579                  }
580              }};
581 <        mainPool.invoke(a);
581 >        testInvokeOnPool(mainPool(), a);
582      }
583  
584      /**
585       * timed get of a forked task throws exception when task completes abnormally
586       */
587      public void testAbnormalForkTimedGet() {
588 <        RecursiveAction a = new RecursiveAction() {
589 <            public void compute() {
588 >        RecursiveAction a = new CheckedRecursiveAction() {
589 >            protected void realCompute() throws Exception {
590 >                FailingAsyncFib f = new FailingAsyncFib(8);
591 >                assertSame(f, f.fork());
592                  try {
593 <                    FailingAsyncFib f = new FailingAsyncFib(8);
431 <                    f.fork();
432 <                    f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
593 >                    f.get(LONG_DELAY_MS, MILLISECONDS);
594                      shouldThrow();
595                  } catch (ExecutionException success) {
596 <                } catch (Exception ex) {
597 <                    unexpectedException(ex);
596 >                    Throwable cause = success.getCause();
597 >                    assertTrue(cause instanceof FJException);
598 >                    checkCompletedAbnormally(f, cause);
599                  }
600              }};
601 <        mainPool.invoke(a);
601 >        testInvokeOnPool(mainPool(), a);
602      }
603  
604      /**
605       * quietlyJoin of a forked task returns when task completes abnormally
606       */
607      public void testAbnormalForkQuietlyJoin() {
608 <        RecursiveAction a = new RecursiveAction() {
609 <            public void compute() {
608 >        RecursiveAction a = new CheckedRecursiveAction() {
609 >            protected void realCompute() {
610                  FailingAsyncFib f = new FailingAsyncFib(8);
611 <                f.fork();
611 >                assertSame(f, f.fork());
612                  f.quietlyJoin();
613 <                threadAssertTrue(f.isDone());
614 <                threadAssertTrue(f.isCompletedAbnormally());
453 <                threadAssertTrue(f.getException() instanceof FJException);
613 >                assertTrue(f.getException() instanceof FJException);
614 >                checkCompletedAbnormally(f, f.getException());
615              }};
616 <        mainPool.invoke(a);
616 >        testInvokeOnPool(mainPool(), a);
617      }
618  
619      /**
620       * invoke task throws exception when task cancelled
621       */
622      public void testCancelledInvoke() {
623 <        RecursiveAction a = new RecursiveAction() {
624 <            public void compute() {
623 >        RecursiveAction a = new CheckedRecursiveAction() {
624 >            protected void realCompute() {
625 >                AsyncFib f = new AsyncFib(8);
626 >                assertTrue(f.cancel(true));
627                  try {
465                    AsyncFib f = new AsyncFib(8);
466                    f.cancel(true);
628                      f.invoke();
629                      shouldThrow();
630                  } catch (CancellationException success) {
631 +                    checkCancelled(f);
632                  }
633              }};
634 <        mainPool.invoke(a);
634 >        testInvokeOnPool(mainPool(), a);
635      }
636  
637      /**
638       * join of a forked task throws exception when task cancelled
639       */
640      public void testCancelledForkJoin() {
641 <        RecursiveAction a = new RecursiveAction() {
642 <            public void compute() {
641 >        RecursiveAction a = new CheckedRecursiveAction() {
642 >            protected void realCompute() {
643 >                AsyncFib f = new AsyncFib(8);
644 >                assertTrue(f.cancel(true));
645 >                assertSame(f, f.fork());
646                  try {
482                    AsyncFib f = new AsyncFib(8);
483                    f.cancel(true);
484                    f.fork();
647                      f.join();
648                      shouldThrow();
649                  } catch (CancellationException success) {
650 +                    checkCancelled(f);
651                  }
652              }};
653 <        mainPool.invoke(a);
653 >        testInvokeOnPool(mainPool(), a);
654      }
655  
656      /**
657       * get of a forked task throws exception when task cancelled
658       */
659      public void testCancelledForkGet() {
660 <        RecursiveAction a = new RecursiveAction() {
661 <            public void compute() {
660 >        RecursiveAction a = new CheckedRecursiveAction() {
661 >            protected void realCompute() throws Exception {
662 >                AsyncFib f = new AsyncFib(8);
663 >                assertTrue(f.cancel(true));
664 >                assertSame(f, f.fork());
665                  try {
500                    AsyncFib f = new AsyncFib(8);
501                    f.cancel(true);
502                    f.fork();
666                      f.get();
667                      shouldThrow();
668                  } catch (CancellationException success) {
669 <                } catch (Exception ex) {
507 <                    unexpectedException(ex);
669 >                    checkCancelled(f);
670                  }
671              }};
672 <        mainPool.invoke(a);
672 >        testInvokeOnPool(mainPool(), a);
673      }
674  
675      /**
676       * timed get of a forked task throws exception when task cancelled
677       */
678 <    public void testCancelledForkTimedGet() {
679 <        RecursiveAction a = new RecursiveAction() {
680 <            public void compute() {
678 >    public void testCancelledForkTimedGet() throws Exception {
679 >        RecursiveAction a = new CheckedRecursiveAction() {
680 >            protected void realCompute() throws Exception {
681 >                AsyncFib f = new AsyncFib(8);
682 >                assertTrue(f.cancel(true));
683 >                assertSame(f, f.fork());
684                  try {
685 <                    AsyncFib f = new AsyncFib(8);
521 <                    f.cancel(true);
522 <                    f.fork();
523 <                    f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
685 >                    f.get(LONG_DELAY_MS, MILLISECONDS);
686                      shouldThrow();
687                  } catch (CancellationException success) {
688 <                } catch (Exception ex) {
527 <                    unexpectedException(ex);
688 >                    checkCancelled(f);
689                  }
690              }};
691 <        mainPool.invoke(a);
691 >        testInvokeOnPool(mainPool(), a);
692      }
693  
694      /**
695       * quietlyJoin of a forked task returns when task cancelled
696       */
697      public void testCancelledForkQuietlyJoin() {
698 <        RecursiveAction a = new RecursiveAction() {
699 <            public void compute() {
698 >        RecursiveAction a = new CheckedRecursiveAction() {
699 >            protected void realCompute() {
700                  AsyncFib f = new AsyncFib(8);
701 <                f.cancel(true);
702 <                f.fork();
701 >                assertTrue(f.cancel(true));
702 >                assertSame(f, f.fork());
703                  f.quietlyJoin();
704 <                threadAssertTrue(f.isDone());
544 <                threadAssertTrue(f.isCompletedAbnormally());
545 <                threadAssertTrue(f.getException() instanceof CancellationException);
704 >                checkCancelled(f);
705              }};
706 <        mainPool.invoke(a);
706 >        testInvokeOnPool(mainPool(), a);
707      }
708  
709      /**
710       * getPool of executing task returns its pool
711       */
712      public void testGetPool() {
713 <        RecursiveAction a = new RecursiveAction() {
714 <            public void compute() {
715 <                threadAssertTrue(getPool() == mainPool);
713 >        final ForkJoinPool mainPool = mainPool();
714 >        RecursiveAction a = new CheckedRecursiveAction() {
715 >            protected void realCompute() {
716 >                assertSame(mainPool, getPool());
717              }};
718 <        mainPool.invoke(a);
718 >        testInvokeOnPool(mainPool, a);
719      }
720  
721      /**
722       * getPool of non-FJ task returns null
723       */
724      public void testGetPool2() {
725 <        RecursiveAction a = new RecursiveAction() {
726 <            public void compute() {
727 <                threadAssertTrue(getPool() == null);
725 >        RecursiveAction a = new CheckedRecursiveAction() {
726 >            protected void realCompute() {
727 >                assertNull(getPool());
728              }};
729 <        a.invoke();
729 >        assertNull(a.invoke());
730      }
731  
732      /**
733       * inForkJoinPool of executing task returns true
734       */
735      public void testInForkJoinPool() {
736 <        RecursiveAction a = new RecursiveAction() {
737 <            public void compute() {
738 <                threadAssertTrue(inForkJoinPool());
736 >        RecursiveAction a = new CheckedRecursiveAction() {
737 >            protected void realCompute() {
738 >                assertTrue(inForkJoinPool());
739              }};
740 <        mainPool.invoke(a);
740 >        testInvokeOnPool(mainPool(), a);
741      }
742  
743      /**
744       * inForkJoinPool of non-FJ task returns false
745       */
746      public void testInForkJoinPool2() {
747 <        RecursiveAction a = new RecursiveAction() {
748 <            public void compute() {
749 <                threadAssertTrue(!inForkJoinPool());
747 >        RecursiveAction a = new CheckedRecursiveAction() {
748 >            protected void realCompute() {
749 >                assertFalse(inForkJoinPool());
750              }};
751 <        a.invoke();
751 >        assertNull(a.invoke());
752      }
753  
754      /**
755       * setRawResult(null) succeeds
756       */
757      public void testSetRawResult() {
758 <        RecursiveAction a = new RecursiveAction() {
759 <            public void compute() {
758 >        RecursiveAction a = new CheckedRecursiveAction() {
759 >            protected void realCompute() {
760                  setRawResult(null);
761 +                assertNull(getRawResult());
762              }};
763 <        a.invoke();
763 >        assertNull(a.invoke());
764      }
765  
766      /**
767       * invoke task throws exception after invoking completeExceptionally
768       */
769      public void testCompleteExceptionally() {
770 <        RecursiveAction a = new RecursiveAction() {
771 <            public void compute() {
770 >        RecursiveAction a = new CheckedRecursiveAction() {
771 >            protected void realCompute() {
772 >                AsyncFib f = new AsyncFib(8);
773 >                f.completeExceptionally(new FJException());
774                  try {
612                    AsyncFib f = new AsyncFib(8);
613                    f.completeExceptionally(new FJException());
775                      f.invoke();
776                      shouldThrow();
777                  } catch (FJException success) {
778 +                    checkCompletedAbnormally(f, success);
779                  }
780              }};
781 <        mainPool.invoke(a);
781 >        testInvokeOnPool(mainPool(), a);
782      }
783  
784      /**
785       * invokeAll(t1, t2) invokes all task arguments
786       */
787      public void testInvokeAll2() {
788 <        RecursiveAction a = new RecursiveAction() {
789 <            public void compute() {
788 >        RecursiveAction a = new CheckedRecursiveAction() {
789 >            protected void realCompute() {
790                  AsyncFib f = new AsyncFib(8);
791                  AsyncFib g = new AsyncFib(9);
792                  invokeAll(f, g);
793 <                threadAssertTrue(f.isDone());
794 <                threadAssertTrue(f.number == 21);
795 <                threadAssertTrue(g.isDone());
796 <                threadAssertTrue(g.number == 34);
793 >                assertEquals(21, f.number);
794 >                assertEquals(34, g.number);
795 >                checkCompletedNormally(f);
796 >                checkCompletedNormally(g);
797              }};
798 <        mainPool.invoke(a);
798 >        testInvokeOnPool(mainPool(), a);
799      }
800  
801      /**
802       * invokeAll(tasks) with 1 argument invokes task
803       */
804      public void testInvokeAll1() {
805 <        RecursiveAction a = new RecursiveAction() {
806 <            public void compute() {
805 >        RecursiveAction a = new CheckedRecursiveAction() {
806 >            protected void realCompute() {
807                  AsyncFib f = new AsyncFib(8);
808                  invokeAll(f);
809 <                threadAssertTrue(f.isDone());
810 <                threadAssertTrue(f.number == 21);
809 >                checkCompletedNormally(f);
810 >                assertEquals(21, f.number);
811              }};
812 <        mainPool.invoke(a);
812 >        testInvokeOnPool(mainPool(), a);
813      }
814  
815      /**
816       * invokeAll(tasks) with > 2 argument invokes tasks
817       */
818      public void testInvokeAll3() {
819 <        RecursiveAction a = new RecursiveAction() {
820 <            public void compute() {
819 >        RecursiveAction a = new CheckedRecursiveAction() {
820 >            protected void realCompute() {
821                  AsyncFib f = new AsyncFib(8);
822                  AsyncFib g = new AsyncFib(9);
823                  AsyncFib h = new AsyncFib(7);
824                  invokeAll(f, g, h);
825 <                threadAssertTrue(f.isDone());
826 <                threadAssertTrue(f.number == 21);
827 <                threadAssertTrue(g.isDone());
828 <                threadAssertTrue(g.number == 34);
829 <                threadAssertTrue(h.isDone());
830 <                threadAssertTrue(h.number == 13);
825 >                assertEquals(21, f.number);
826 >                assertEquals(34, g.number);
827 >                assertEquals(13, h.number);
828 >                checkCompletedNormally(f);
829 >                checkCompletedNormally(g);
830 >                checkCompletedNormally(h);
831              }};
832 <        mainPool.invoke(a);
832 >        testInvokeOnPool(mainPool(), a);
833      }
834  
835      /**
836       * invokeAll(collection) invokes all tasks in the collection
837       */
838      public void testInvokeAllCollection() {
839 <        RecursiveAction a = new RecursiveAction() {
840 <            public void compute() {
839 >        RecursiveAction a = new CheckedRecursiveAction() {
840 >            protected void realCompute() {
841                  AsyncFib f = new AsyncFib(8);
842                  AsyncFib g = new AsyncFib(9);
843                  AsyncFib h = new AsyncFib(7);
# Line 684 | Line 846 | public class ForkJoinTaskTest extends JS
846                  set.add(g);
847                  set.add(h);
848                  invokeAll(set);
849 <                threadAssertTrue(f.isDone());
850 <                threadAssertTrue(f.number == 21);
851 <                threadAssertTrue(g.isDone());
852 <                threadAssertTrue(g.number == 34);
853 <                threadAssertTrue(h.isDone());
854 <                threadAssertTrue(h.number == 13);
849 >                assertEquals(21, f.number);
850 >                assertEquals(34, g.number);
851 >                assertEquals(13, h.number);
852 >                checkCompletedNormally(f);
853 >                checkCompletedNormally(g);
854 >                checkCompletedNormally(h);
855              }};
856 <        mainPool.invoke(a);
856 >        testInvokeOnPool(mainPool(), a);
857      }
858  
697
859      /**
860       * invokeAll(tasks) with any null task throws NPE
861       */
862      public void testInvokeAllNPE() {
863 <        RecursiveAction a = new RecursiveAction() {
864 <            public void compute() {
863 >        RecursiveAction a = new CheckedRecursiveAction() {
864 >            protected void realCompute() {
865 >                AsyncFib f = new AsyncFib(8);
866 >                AsyncFib g = new AsyncFib(9);
867 >                AsyncFib h = null;
868                  try {
705                    AsyncFib f = new AsyncFib(8);
706                    AsyncFib g = new AsyncFib(9);
707                    AsyncFib h = null;
869                      invokeAll(f, g, h);
870                      shouldThrow();
871 <                } catch (NullPointerException success) {
711 <                }
871 >                } catch (NullPointerException success) {}
872              }};
873 <        mainPool.invoke(a);
873 >        testInvokeOnPool(mainPool(), a);
874      }
875  
876      /**
877       * invokeAll(t1, t2) throw exception if any task does
878       */
879      public void testAbnormalInvokeAll2() {
880 <        RecursiveAction a = new RecursiveAction() {
881 <            public void compute() {
880 >        RecursiveAction a = new CheckedRecursiveAction() {
881 >            protected void realCompute() {
882 >                AsyncFib f = new AsyncFib(8);
883 >                FailingAsyncFib g = new FailingAsyncFib(9);
884 >                ForkJoinTask[] tasks = { f, g };
885 >                Collections.shuffle(Arrays.asList(tasks));
886                  try {
887 <                    AsyncFib f = new AsyncFib(8);
724 <                    FailingAsyncFib g = new FailingAsyncFib(9);
725 <                    invokeAll(f, g);
887 >                    invokeAll(tasks);
888                      shouldThrow();
889                  } catch (FJException success) {
890 +                    checkCompletedAbnormally(g, success);
891                  }
892              }};
893 <        mainPool.invoke(a);
893 >        testInvokeOnPool(mainPool(), a);
894      }
895  
896      /**
897       * invokeAll(tasks) with 1 argument throws exception if task does
898       */
899      public void testAbnormalInvokeAll1() {
900 <        RecursiveAction a = new RecursiveAction() {
901 <            public void compute() {
900 >        RecursiveAction a = new CheckedRecursiveAction() {
901 >            protected void realCompute() {
902 >                FailingAsyncFib g = new FailingAsyncFib(9);
903                  try {
740                    FailingAsyncFib g = new FailingAsyncFib(9);
904                      invokeAll(g);
905                      shouldThrow();
906                  } catch (FJException success) {
907 +                    checkCompletedAbnormally(g, success);
908                  }
909              }};
910 <        mainPool.invoke(a);
910 >        testInvokeOnPool(mainPool(), a);
911      }
912  
913      /**
914       * invokeAll(tasks) with > 2 argument throws exception if any task does
915       */
916      public void testAbnormalInvokeAll3() {
917 <        RecursiveAction a = new RecursiveAction() {
918 <            public void compute() {
917 >        RecursiveAction a = new CheckedRecursiveAction() {
918 >            protected void realCompute() {
919 >                AsyncFib f = new AsyncFib(8);
920 >                FailingAsyncFib g = new FailingAsyncFib(9);
921 >                AsyncFib h = new AsyncFib(7);
922 >                ForkJoinTask[] tasks = { f, g, h };
923 >                Collections.shuffle(Arrays.asList(tasks));
924                  try {
925 <                    AsyncFib f = new AsyncFib(8);
757 <                    FailingAsyncFib g = new FailingAsyncFib(9);
758 <                    AsyncFib h = new AsyncFib(7);
759 <                    invokeAll(f, g, h);
925 >                    invokeAll(tasks);
926                      shouldThrow();
927                  } catch (FJException success) {
928 +                    checkCompletedAbnormally(g, success);
929                  }
930              }};
931 <        mainPool.invoke(a);
931 >        testInvokeOnPool(mainPool(), a);
932      }
933  
934      /**
935 <     * invokeAll(collection)  throws exception if any task does
935 >     * invokeAll(collection) throws exception if any task does
936       */
937      public void testAbnormalInvokeAllCollection() {
938 <        RecursiveAction a = new RecursiveAction() {
939 <            public void compute() {
938 >        RecursiveAction a = new CheckedRecursiveAction() {
939 >            protected void realCompute() {
940 >                FailingAsyncFib f = new FailingAsyncFib(8);
941 >                AsyncFib g = new AsyncFib(9);
942 >                AsyncFib h = new AsyncFib(7);
943 >                ForkJoinTask[] tasks = { f, g, h };
944 >                List taskList = Arrays.asList(tasks);
945 >                Collections.shuffle(taskList);
946                  try {
947 <                    FailingAsyncFib f = new FailingAsyncFib(8);
775 <                    AsyncFib g = new AsyncFib(9);
776 <                    AsyncFib h = new AsyncFib(7);
777 <                    HashSet set = new HashSet();
778 <                    set.add(f);
779 <                    set.add(g);
780 <                    set.add(h);
781 <                    invokeAll(set);
947 >                    invokeAll(taskList);
948                      shouldThrow();
949                  } catch (FJException success) {
950 +                    checkCompletedAbnormally(f, success);
951                  }
952              }};
953 <        mainPool.invoke(a);
953 >        testInvokeOnPool(mainPool(), a);
954      }
955  
956      /**
# Line 791 | Line 958 | public class ForkJoinTaskTest extends JS
958       * and suppresses execution
959       */
960      public void testTryUnfork() {
961 <        RecursiveAction a = new RecursiveAction() {
962 <            public void compute() {
961 >        RecursiveAction a = new CheckedRecursiveAction() {
962 >            protected void realCompute() {
963                  AsyncFib g = new AsyncFib(9);
964 <                g.fork();
964 >                assertSame(g, g.fork());
965                  AsyncFib f = new AsyncFib(8);
966 <                f.fork();
967 <                threadAssertTrue(f.tryUnfork());
966 >                assertSame(f, f.fork());
967 >                assertTrue(f.tryUnfork());
968                  helpQuiesce();
969 <                threadAssertFalse(f.isDone());
970 <                threadAssertTrue(g.isDone());
969 >                checkNotDone(f);
970 >                checkCompletedNormally(g);
971              }};
972 <        singletonPool.invoke(a);
972 >        testInvokeOnPool(singletonPool(), a);
973      }
974  
975      /**
# Line 810 | Line 977 | public class ForkJoinTaskTest extends JS
977       * there are more tasks than threads
978       */
979      public void testGetSurplusQueuedTaskCount() {
980 <        RecursiveAction a = new RecursiveAction() {
981 <            public void compute() {
980 >        RecursiveAction a = new CheckedRecursiveAction() {
981 >            protected void realCompute() {
982                  AsyncFib h = new AsyncFib(7);
983 <                h.fork();
983 >                assertSame(h, h.fork());
984                  AsyncFib g = new AsyncFib(9);
985 <                g.fork();
985 >                assertSame(g, g.fork());
986                  AsyncFib f = new AsyncFib(8);
987 <                f.fork();
988 <                threadAssertTrue(getSurplusQueuedTaskCount() > 0);
987 >                assertSame(f, f.fork());
988 >                assertTrue(getSurplusQueuedTaskCount() > 0);
989                  helpQuiesce();
990 +                assertEquals(0, getSurplusQueuedTaskCount());
991 +                checkCompletedNormally(f);
992 +                checkCompletedNormally(g);
993 +                checkCompletedNormally(h);
994              }};
995 <        singletonPool.invoke(a);
995 >        testInvokeOnPool(singletonPool(), a);
996      }
997  
998      /**
999       * peekNextLocalTask returns most recent unexecuted task.
1000       */
1001      public void testPeekNextLocalTask() {
1002 <        RecursiveAction a = new RecursiveAction() {
1003 <            public void compute() {
1002 >        RecursiveAction a = new CheckedRecursiveAction() {
1003 >            protected void realCompute() {
1004                  AsyncFib g = new AsyncFib(9);
1005 <                g.fork();
1005 >                assertSame(g, g.fork());
1006                  AsyncFib f = new AsyncFib(8);
1007 <                f.fork();
1008 <                threadAssertTrue(peekNextLocalTask() == f);
1009 <                f.join();
1010 <                threadAssertTrue(f.isDone());
1007 >                assertSame(f, f.fork());
1008 >                assertSame(f, peekNextLocalTask());
1009 >                assertNull(f.join());
1010 >                checkCompletedNormally(f);
1011                  helpQuiesce();
1012 +                checkCompletedNormally(g);
1013              }};
1014 <        singletonPool.invoke(a);
1014 >        testInvokeOnPool(singletonPool(), a);
1015      }
1016  
1017      /**
1018 <     * pollNextLocalTask returns most recent unexecuted task
1019 <     * without executing it
1018 >     * pollNextLocalTask returns most recent unexecuted task without
1019 >     * executing it
1020       */
1021      public void testPollNextLocalTask() {
1022 <        RecursiveAction a = new RecursiveAction() {
1023 <            public void compute() {
1022 >        RecursiveAction a = new CheckedRecursiveAction() {
1023 >            protected void realCompute() {
1024                  AsyncFib g = new AsyncFib(9);
1025 <                g.fork();
1025 >                assertSame(g, g.fork());
1026                  AsyncFib f = new AsyncFib(8);
1027 <                f.fork();
1028 <                threadAssertTrue(pollNextLocalTask() == f);
1027 >                assertSame(f, f.fork());
1028 >                assertSame(f, pollNextLocalTask());
1029                  helpQuiesce();
1030 <                threadAssertFalse(f.isDone());
1030 >                checkNotDone(f);
1031 >                assertEquals(34, g.number);
1032 >                checkCompletedNormally(g);
1033              }};
1034 <        singletonPool.invoke(a);
1034 >        testInvokeOnPool(singletonPool(), a);
1035      }
1036  
1037      /**
1038 <     * pollTask returns an unexecuted task
865 <     * without executing it
1038 >     * pollTask returns an unexecuted task without executing it
1039       */
1040      public void testPollTask() {
1041 <        RecursiveAction a = new RecursiveAction() {
1042 <            public void compute() {
1041 >        RecursiveAction a = new CheckedRecursiveAction() {
1042 >            protected void realCompute() {
1043                  AsyncFib g = new AsyncFib(9);
1044 <                g.fork();
1044 >                assertSame(g, g.fork());
1045                  AsyncFib f = new AsyncFib(8);
1046 <                f.fork();
1047 <                threadAssertTrue(pollTask() == f);
1046 >                assertSame(f, f.fork());
1047 >                assertSame(f, pollTask());
1048                  helpQuiesce();
1049 <                threadAssertFalse(f.isDone());
1050 <                threadAssertTrue(g.isDone());
1049 >                checkNotDone(f);
1050 >                checkCompletedNormally(g);
1051              }};
1052 <        singletonPool.invoke(a);
1052 >        testInvokeOnPool(singletonPool(), a);
1053      }
1054  
1055      /**
1056       * peekNextLocalTask returns least recent unexecuted task in async mode
1057       */
1058      public void testPeekNextLocalTaskAsync() {
1059 <        RecursiveAction a = new RecursiveAction() {
1060 <            public void compute() {
1059 >        RecursiveAction a = new CheckedRecursiveAction() {
1060 >            protected void realCompute() {
1061                  AsyncFib g = new AsyncFib(9);
1062 <                g.fork();
1062 >                assertSame(g, g.fork());
1063                  AsyncFib f = new AsyncFib(8);
1064 <                f.fork();
1065 <                threadAssertTrue(peekNextLocalTask() == g);
1066 <                f.join();
1064 >                assertSame(f, f.fork());
1065 >                assertSame(g, peekNextLocalTask());
1066 >                assertNull(f.join());
1067                  helpQuiesce();
1068 <                threadAssertTrue(f.isDone());
1068 >                checkCompletedNormally(f);
1069 >                assertEquals(34, g.number);
1070 >                checkCompletedNormally(g);
1071              }};
1072 <        asyncSingletonPool.invoke(a);
1072 >        testInvokeOnPool(asyncSingletonPool(), a);
1073      }
1074  
1075      /**
1076 <     * pollNextLocalTask returns least recent unexecuted task
1077 <     * without executing it, in async mode
1076 >     * pollNextLocalTask returns least recent unexecuted task without
1077 >     * executing it, in async mode
1078       */
1079      public void testPollNextLocalTaskAsync() {
1080 <        RecursiveAction a = new RecursiveAction() {
1081 <            public void compute() {
1080 >        RecursiveAction a = new CheckedRecursiveAction() {
1081 >            protected void realCompute() {
1082                  AsyncFib g = new AsyncFib(9);
1083 <                g.fork();
1083 >                assertSame(g, g.fork());
1084                  AsyncFib f = new AsyncFib(8);
1085 <                f.fork();
1086 <                threadAssertTrue(pollNextLocalTask() == g);
1085 >                assertSame(f, f.fork());
1086 >                assertSame(g, pollNextLocalTask());
1087                  helpQuiesce();
1088 <                threadAssertTrue(f.isDone());
1089 <                threadAssertFalse(g.isDone());
1088 >                assertEquals(21, f.number);
1089 >                checkCompletedNormally(f);
1090 >                checkNotDone(g);
1091              }};
1092 <        asyncSingletonPool.invoke(a);
1092 >        testInvokeOnPool(asyncSingletonPool(), a);
1093      }
1094  
1095      /**
1096 <     * pollTask returns an unexecuted task
1097 <     * without executing it, in async mode
1096 >     * pollTask returns an unexecuted task without executing it, in
1097 >     * async mode
1098       */
1099      public void testPollTaskAsync() {
1100 <        RecursiveAction a = new RecursiveAction() {
1101 <            public void compute() {
1100 >        RecursiveAction a = new CheckedRecursiveAction() {
1101 >            protected void realCompute() {
1102                  AsyncFib g = new AsyncFib(9);
1103 <                g.fork();
1103 >                assertSame(g, g.fork());
1104                  AsyncFib f = new AsyncFib(8);
1105 <                f.fork();
1106 <                threadAssertTrue(pollTask() == g);
1105 >                assertSame(f, f.fork());
1106 >                assertSame(g, pollTask());
1107                  helpQuiesce();
1108 <                threadAssertTrue(f.isDone());
1109 <                threadAssertFalse(g.isDone());
1108 >                assertEquals(21, f.number);
1109 >                checkCompletedNormally(f);
1110 >                checkNotDone(g);
1111              }};
1112 <        asyncSingletonPool.invoke(a);
1112 >        testInvokeOnPool(asyncSingletonPool(), a);
1113      }
1114 +
1115 +    // versions for singleton pools
1116 +
1117 +    /**
1118 +     * invoke returns when task completes normally.
1119 +     * isCompletedAbnormally and isCancelled return false for normally
1120 +     * completed tasks; getRawResult returns null.
1121 +     */
1122 +    public void testInvokeSingleton() {
1123 +        RecursiveAction a = new CheckedRecursiveAction() {
1124 +            protected void realCompute() {
1125 +                AsyncFib f = new AsyncFib(8);
1126 +                assertNull(f.invoke());
1127 +                assertEquals(21, f.number);
1128 +                checkCompletedNormally(f);
1129 +            }};
1130 +        testInvokeOnPool(singletonPool(), a);
1131 +    }
1132 +
1133 +    /**
1134 +     * quietlyInvoke task returns when task completes normally.
1135 +     * isCompletedAbnormally and isCancelled return false for normally
1136 +     * completed tasks
1137 +     */
1138 +    public void testQuietlyInvokeSingleton() {
1139 +        RecursiveAction a = new CheckedRecursiveAction() {
1140 +            protected void realCompute() {
1141 +                AsyncFib f = new AsyncFib(8);
1142 +                f.quietlyInvoke();
1143 +                assertEquals(21, f.number);
1144 +                checkCompletedNormally(f);
1145 +            }};
1146 +        testInvokeOnPool(singletonPool(), a);
1147 +    }
1148 +
1149 +    /**
1150 +     * join of a forked task returns when task completes
1151 +     */
1152 +    public void testForkJoinSingleton() {
1153 +        RecursiveAction a = new CheckedRecursiveAction() {
1154 +            protected void realCompute() {
1155 +                AsyncFib f = new AsyncFib(8);
1156 +                assertSame(f, f.fork());
1157 +                assertNull(f.join());
1158 +                assertEquals(21, f.number);
1159 +                checkCompletedNormally(f);
1160 +            }};
1161 +        testInvokeOnPool(singletonPool(), a);
1162 +    }
1163 +
1164 +    /**
1165 +     * get of a forked task returns when task completes
1166 +     */
1167 +    public void testForkGetSingleton() {
1168 +        RecursiveAction a = new CheckedRecursiveAction() {
1169 +            protected void realCompute() throws Exception {
1170 +                AsyncFib f = new AsyncFib(8);
1171 +                assertSame(f, f.fork());
1172 +                assertNull(f.get());
1173 +                assertEquals(21, f.number);
1174 +                checkCompletedNormally(f);
1175 +            }};
1176 +        testInvokeOnPool(singletonPool(), a);
1177 +    }
1178 +
1179 +    /**
1180 +     * timed get of a forked task returns when task completes
1181 +     */
1182 +    public void testForkTimedGetSingleton() {
1183 +        RecursiveAction a = new CheckedRecursiveAction() {
1184 +            protected void realCompute() throws Exception {
1185 +                AsyncFib f = new AsyncFib(8);
1186 +                assertSame(f, f.fork());
1187 +                assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
1188 +                assertEquals(21, f.number);
1189 +                checkCompletedNormally(f);
1190 +            }};
1191 +        testInvokeOnPool(singletonPool(), a);
1192 +    }
1193 +
1194 +    /**
1195 +     * timed get with null time unit throws NPE
1196 +     */
1197 +    public void testForkTimedGetNPESingleton() {
1198 +        RecursiveAction a = new CheckedRecursiveAction() {
1199 +            protected void realCompute() throws Exception {
1200 +                AsyncFib f = new AsyncFib(8);
1201 +                assertSame(f, f.fork());
1202 +                try {
1203 +                    f.get(5L, null);
1204 +                    shouldThrow();
1205 +                } catch (NullPointerException success) {}
1206 +            }};
1207 +        testInvokeOnPool(singletonPool(), a);
1208 +    }
1209 +
1210 +    /**
1211 +     * quietlyJoin of a forked task returns when task completes
1212 +     */
1213 +    public void testForkQuietlyJoinSingleton() {
1214 +        RecursiveAction a = new CheckedRecursiveAction() {
1215 +            protected void realCompute() {
1216 +                AsyncFib f = new AsyncFib(8);
1217 +                assertSame(f, f.fork());
1218 +                f.quietlyJoin();
1219 +                assertEquals(21, f.number);
1220 +                checkCompletedNormally(f);
1221 +            }};
1222 +        testInvokeOnPool(singletonPool(), a);
1223 +    }
1224 +
1225 +    /**
1226 +     * helpQuiesce returns when tasks are complete.
1227 +     * getQueuedTaskCount returns 0 when quiescent
1228 +     */
1229 +    public void testForkHelpQuiesceSingleton() {
1230 +        RecursiveAction a = new CheckedRecursiveAction() {
1231 +            protected void realCompute() {
1232 +                AsyncFib f = new AsyncFib(8);
1233 +                assertSame(f, f.fork());
1234 +                helpQuiesce();
1235 +                assertEquals(0, getQueuedTaskCount());
1236 +                assertEquals(21, f.number);
1237 +                checkCompletedNormally(f);
1238 +            }};
1239 +        testInvokeOnPool(singletonPool(), a);
1240 +    }
1241 +
1242 +    /**
1243 +     * invoke task throws exception when task completes abnormally
1244 +     */
1245 +    public void testAbnormalInvokeSingleton() {
1246 +        RecursiveAction a = new CheckedRecursiveAction() {
1247 +            protected void realCompute() {
1248 +                FailingAsyncFib f = new FailingAsyncFib(8);
1249 +                try {
1250 +                    f.invoke();
1251 +                    shouldThrow();
1252 +                } catch (FJException success) {
1253 +                    checkCompletedAbnormally(f, success);
1254 +                }
1255 +            }};
1256 +        testInvokeOnPool(singletonPool(), a);
1257 +    }
1258 +
1259 +    /**
1260 +     * quietlyInvoke task returns when task completes abnormally
1261 +     */
1262 +    public void testAbnormalQuietlyInvokeSingleton() {
1263 +        RecursiveAction a = new CheckedRecursiveAction() {
1264 +            protected void realCompute() {
1265 +                FailingAsyncFib f = new FailingAsyncFib(8);
1266 +                f.quietlyInvoke();
1267 +                assertTrue(f.getException() instanceof FJException);
1268 +                checkCompletedAbnormally(f, f.getException());
1269 +            }};
1270 +        testInvokeOnPool(singletonPool(), a);
1271 +    }
1272 +
1273 +    /**
1274 +     * join of a forked task throws exception when task completes abnormally
1275 +     */
1276 +    public void testAbnormalForkJoinSingleton() {
1277 +        RecursiveAction a = new CheckedRecursiveAction() {
1278 +            protected void realCompute() {
1279 +                FailingAsyncFib f = new FailingAsyncFib(8);
1280 +                assertSame(f, f.fork());
1281 +                try {
1282 +                    f.join();
1283 +                    shouldThrow();
1284 +                } catch (FJException success) {
1285 +                    checkCompletedAbnormally(f, success);
1286 +                }
1287 +            }};
1288 +        testInvokeOnPool(singletonPool(), a);
1289 +    }
1290 +
1291 +    /**
1292 +     * get of a forked task throws exception when task completes abnormally
1293 +     */
1294 +    public void testAbnormalForkGetSingleton() {
1295 +        RecursiveAction a = new CheckedRecursiveAction() {
1296 +            protected void realCompute() throws Exception {
1297 +                FailingAsyncFib f = new FailingAsyncFib(8);
1298 +                assertSame(f, f.fork());
1299 +                try {
1300 +                    f.get();
1301 +                    shouldThrow();
1302 +                } catch (ExecutionException success) {
1303 +                    Throwable cause = success.getCause();
1304 +                    assertTrue(cause instanceof FJException);
1305 +                    checkCompletedAbnormally(f, cause);
1306 +                }
1307 +            }};
1308 +        testInvokeOnPool(singletonPool(), a);
1309 +    }
1310 +
1311 +    /**
1312 +     * timed get of a forked task throws exception when task completes abnormally
1313 +     */
1314 +    public void testAbnormalForkTimedGetSingleton() {
1315 +        RecursiveAction a = new CheckedRecursiveAction() {
1316 +            protected void realCompute() throws Exception {
1317 +                FailingAsyncFib f = new FailingAsyncFib(8);
1318 +                assertSame(f, f.fork());
1319 +                try {
1320 +                    f.get(LONG_DELAY_MS, MILLISECONDS);
1321 +                    shouldThrow();
1322 +                } catch (ExecutionException success) {
1323 +                    Throwable cause = success.getCause();
1324 +                    assertTrue(cause instanceof FJException);
1325 +                    checkCompletedAbnormally(f, cause);
1326 +                }
1327 +            }};
1328 +        testInvokeOnPool(singletonPool(), a);
1329 +    }
1330 +
1331 +    /**
1332 +     * quietlyJoin of a forked task returns when task completes abnormally
1333 +     */
1334 +    public void testAbnormalForkQuietlyJoinSingleton() {
1335 +        RecursiveAction a = new CheckedRecursiveAction() {
1336 +            protected void realCompute() {
1337 +                FailingAsyncFib f = new FailingAsyncFib(8);
1338 +                assertSame(f, f.fork());
1339 +                f.quietlyJoin();
1340 +                assertTrue(f.getException() instanceof FJException);
1341 +                checkCompletedAbnormally(f, f.getException());
1342 +            }};
1343 +        testInvokeOnPool(singletonPool(), a);
1344 +    }
1345 +
1346 +    /**
1347 +     * invoke task throws exception when task cancelled
1348 +     */
1349 +    public void testCancelledInvokeSingleton() {
1350 +        RecursiveAction a = new CheckedRecursiveAction() {
1351 +            protected void realCompute() {
1352 +                AsyncFib f = new AsyncFib(8);
1353 +                assertTrue(f.cancel(true));
1354 +                try {
1355 +                    f.invoke();
1356 +                    shouldThrow();
1357 +                } catch (CancellationException success) {
1358 +                    checkCancelled(f);
1359 +                }
1360 +            }};
1361 +        testInvokeOnPool(singletonPool(), a);
1362 +    }
1363 +
1364 +    /**
1365 +     * join of a forked task throws exception when task cancelled
1366 +     */
1367 +    public void testCancelledForkJoinSingleton() {
1368 +        RecursiveAction a = new CheckedRecursiveAction() {
1369 +            protected void realCompute() {
1370 +                AsyncFib f = new AsyncFib(8);
1371 +                assertTrue(f.cancel(true));
1372 +                assertSame(f, f.fork());
1373 +                try {
1374 +                    f.join();
1375 +                    shouldThrow();
1376 +                } catch (CancellationException success) {
1377 +                    checkCancelled(f);
1378 +                }
1379 +            }};
1380 +        testInvokeOnPool(singletonPool(), a);
1381 +    }
1382 +
1383 +    /**
1384 +     * get of a forked task throws exception when task cancelled
1385 +     */
1386 +    public void testCancelledForkGetSingleton() {
1387 +        RecursiveAction a = new CheckedRecursiveAction() {
1388 +            protected void realCompute() throws Exception {
1389 +                AsyncFib f = new AsyncFib(8);
1390 +                assertTrue(f.cancel(true));
1391 +                assertSame(f, f.fork());
1392 +                try {
1393 +                    f.get();
1394 +                    shouldThrow();
1395 +                } catch (CancellationException success) {
1396 +                    checkCancelled(f);
1397 +                }
1398 +            }};
1399 +        testInvokeOnPool(singletonPool(), a);
1400 +    }
1401 +
1402 +    /**
1403 +     * timed get of a forked task throws exception when task cancelled
1404 +     */
1405 +    public void testCancelledForkTimedGetSingleton() throws Exception {
1406 +        RecursiveAction a = new CheckedRecursiveAction() {
1407 +            protected void realCompute() throws Exception {
1408 +                AsyncFib f = new AsyncFib(8);
1409 +                assertTrue(f.cancel(true));
1410 +                assertSame(f, f.fork());
1411 +                try {
1412 +                    f.get(LONG_DELAY_MS, MILLISECONDS);
1413 +                    shouldThrow();
1414 +                } catch (CancellationException success) {
1415 +                    checkCancelled(f);
1416 +                }
1417 +            }};
1418 +        testInvokeOnPool(singletonPool(), a);
1419 +    }
1420 +
1421 +    /**
1422 +     * quietlyJoin of a forked task returns when task cancelled
1423 +     */
1424 +    public void testCancelledForkQuietlyJoinSingleton() {
1425 +        RecursiveAction a = new CheckedRecursiveAction() {
1426 +            protected void realCompute() {
1427 +                AsyncFib f = new AsyncFib(8);
1428 +                assertTrue(f.cancel(true));
1429 +                assertSame(f, f.fork());
1430 +                f.quietlyJoin();
1431 +                checkCancelled(f);
1432 +            }};
1433 +        testInvokeOnPool(singletonPool(), a);
1434 +    }
1435 +
1436 +    /**
1437 +     * invoke task throws exception after invoking completeExceptionally
1438 +     */
1439 +    public void testCompleteExceptionallySingleton() {
1440 +        RecursiveAction a = new CheckedRecursiveAction() {
1441 +            protected void realCompute() {
1442 +                AsyncFib f = new AsyncFib(8);
1443 +                f.completeExceptionally(new FJException());
1444 +                try {
1445 +                    f.invoke();
1446 +                    shouldThrow();
1447 +                } catch (FJException success) {
1448 +                    checkCompletedAbnormally(f, success);
1449 +                }
1450 +            }};
1451 +        testInvokeOnPool(singletonPool(), a);
1452 +    }
1453 +
1454 +    /**
1455 +     * invokeAll(t1, t2) invokes all task arguments
1456 +     */
1457 +    public void testInvokeAll2Singleton() {
1458 +        RecursiveAction a = new CheckedRecursiveAction() {
1459 +            protected void realCompute() {
1460 +                AsyncFib f = new AsyncFib(8);
1461 +                AsyncFib g = new AsyncFib(9);
1462 +                invokeAll(f, g);
1463 +                assertEquals(21, f.number);
1464 +                assertEquals(34, g.number);
1465 +                checkCompletedNormally(f);
1466 +                checkCompletedNormally(g);
1467 +            }};
1468 +        testInvokeOnPool(singletonPool(), a);
1469 +    }
1470 +
1471 +    /**
1472 +     * invokeAll(tasks) with 1 argument invokes task
1473 +     */
1474 +    public void testInvokeAll1Singleton() {
1475 +        RecursiveAction a = new CheckedRecursiveAction() {
1476 +            protected void realCompute() {
1477 +                AsyncFib f = new AsyncFib(8);
1478 +                invokeAll(f);
1479 +                checkCompletedNormally(f);
1480 +                assertEquals(21, f.number);
1481 +            }};
1482 +        testInvokeOnPool(singletonPool(), a);
1483 +    }
1484 +
1485 +    /**
1486 +     * invokeAll(tasks) with > 2 argument invokes tasks
1487 +     */
1488 +    public void testInvokeAll3Singleton() {
1489 +        RecursiveAction a = new CheckedRecursiveAction() {
1490 +            protected void realCompute() {
1491 +                AsyncFib f = new AsyncFib(8);
1492 +                AsyncFib g = new AsyncFib(9);
1493 +                AsyncFib h = new AsyncFib(7);
1494 +                invokeAll(f, g, h);
1495 +                assertEquals(21, f.number);
1496 +                assertEquals(34, g.number);
1497 +                assertEquals(13, h.number);
1498 +                checkCompletedNormally(f);
1499 +                checkCompletedNormally(g);
1500 +                checkCompletedNormally(h);
1501 +            }};
1502 +        testInvokeOnPool(singletonPool(), a);
1503 +    }
1504 +
1505 +    /**
1506 +     * invokeAll(collection) invokes all tasks in the collection
1507 +     */
1508 +    public void testInvokeAllCollectionSingleton() {
1509 +        RecursiveAction a = new CheckedRecursiveAction() {
1510 +            protected void realCompute() {
1511 +                AsyncFib f = new AsyncFib(8);
1512 +                AsyncFib g = new AsyncFib(9);
1513 +                AsyncFib h = new AsyncFib(7);
1514 +                HashSet set = new HashSet();
1515 +                set.add(f);
1516 +                set.add(g);
1517 +                set.add(h);
1518 +                invokeAll(set);
1519 +                assertEquals(21, f.number);
1520 +                assertEquals(34, g.number);
1521 +                assertEquals(13, h.number);
1522 +                checkCompletedNormally(f);
1523 +                checkCompletedNormally(g);
1524 +                checkCompletedNormally(h);
1525 +            }};
1526 +        testInvokeOnPool(singletonPool(), a);
1527 +    }
1528 +
1529 +    /**
1530 +     * invokeAll(tasks) with any null task throws NPE
1531 +     */
1532 +    public void testInvokeAllNPESingleton() {
1533 +        RecursiveAction a = new CheckedRecursiveAction() {
1534 +            protected void realCompute() {
1535 +                AsyncFib f = new AsyncFib(8);
1536 +                AsyncFib g = new AsyncFib(9);
1537 +                AsyncFib h = null;
1538 +                try {
1539 +                    invokeAll(f, g, h);
1540 +                    shouldThrow();
1541 +                } catch (NullPointerException success) {}
1542 +            }};
1543 +        testInvokeOnPool(singletonPool(), a);
1544 +    }
1545 +
1546 +    /**
1547 +     * invokeAll(t1, t2) throw exception if any task does
1548 +     */
1549 +    public void testAbnormalInvokeAll2Singleton() {
1550 +        RecursiveAction a = new CheckedRecursiveAction() {
1551 +            protected void realCompute() {
1552 +                AsyncFib f = new AsyncFib(8);
1553 +                FailingAsyncFib g = new FailingAsyncFib(9);
1554 +                ForkJoinTask[] tasks = { f, g };
1555 +                Collections.shuffle(Arrays.asList(tasks));
1556 +                try {
1557 +                    invokeAll(tasks);
1558 +                    shouldThrow();
1559 +                } catch (FJException success) {
1560 +                    checkCompletedAbnormally(g, success);
1561 +                }
1562 +            }};
1563 +        testInvokeOnPool(singletonPool(), a);
1564 +    }
1565 +
1566 +    /**
1567 +     * invokeAll(tasks) with 1 argument throws exception if task does
1568 +     */
1569 +    public void testAbnormalInvokeAll1Singleton() {
1570 +        RecursiveAction a = new CheckedRecursiveAction() {
1571 +            protected void realCompute() {
1572 +                FailingAsyncFib g = new FailingAsyncFib(9);
1573 +                try {
1574 +                    invokeAll(g);
1575 +                    shouldThrow();
1576 +                } catch (FJException success) {
1577 +                    checkCompletedAbnormally(g, success);
1578 +                }
1579 +            }};
1580 +        testInvokeOnPool(singletonPool(), a);
1581 +    }
1582 +
1583 +    /**
1584 +     * invokeAll(tasks) with > 2 argument throws exception if any task does
1585 +     */
1586 +    public void testAbnormalInvokeAll3Singleton() {
1587 +        RecursiveAction a = new CheckedRecursiveAction() {
1588 +            protected void realCompute() {
1589 +                AsyncFib f = new AsyncFib(8);
1590 +                FailingAsyncFib g = new FailingAsyncFib(9);
1591 +                AsyncFib h = new AsyncFib(7);
1592 +                ForkJoinTask[] tasks = { f, g, h };
1593 +                Collections.shuffle(Arrays.asList(tasks));
1594 +                try {
1595 +                    invokeAll(tasks);
1596 +                    shouldThrow();
1597 +                } catch (FJException success) {
1598 +                    checkCompletedAbnormally(g, success);
1599 +                }
1600 +            }};
1601 +        testInvokeOnPool(singletonPool(), a);
1602 +    }
1603 +
1604 +    /**
1605 +     * invokeAll(collection) throws exception if any task does
1606 +     */
1607 +    public void testAbnormalInvokeAllCollectionSingleton() {
1608 +        RecursiveAction a = new CheckedRecursiveAction() {
1609 +            protected void realCompute() {
1610 +                FailingAsyncFib f = new FailingAsyncFib(8);
1611 +                AsyncFib g = new AsyncFib(9);
1612 +                AsyncFib h = new AsyncFib(7);
1613 +                ForkJoinTask[] tasks = { f, g, h };
1614 +                List taskList = Arrays.asList(tasks);
1615 +                Collections.shuffle(taskList);
1616 +                try {
1617 +                    invokeAll(taskList);
1618 +                    shouldThrow();
1619 +                } catch (FJException success) {
1620 +                    checkCompletedAbnormally(f, success);
1621 +                }
1622 +            }};
1623 +        testInvokeOnPool(singletonPool(), a);
1624 +    }
1625 +
1626 +    /**
1627 +     * ForkJoinTask.quietlyComplete returns when task completes
1628 +     * normally without setting a value. The most recent value
1629 +     * established by setRawResult(V) (or null by default) is returned
1630 +     * from invoke.
1631 +     */
1632 +    public void testQuietlyComplete() {
1633 +        RecursiveAction a = new CheckedRecursiveAction() {
1634 +                protected void realCompute() {
1635 +                    AsyncFib f = new AsyncFib(8);
1636 +                    f.quietlyComplete();
1637 +                    assertEquals(8, f.number);
1638 +                    checkCompletedNormally(f);
1639 +                }};
1640 +        testInvokeOnPool(mainPool(), a);
1641 +    }
1642 +
1643   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines