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.14 by jsr166, Mon Sep 13 07:51:18 2010 UTC vs.
Revision 1.50 by jsr166, Wed Aug 10 01:28:14 2016 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 +    // 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();
40 >        return new ForkJoinPool(mainPoolSize);
41      }
42  
43      private static ForkJoinPool singletonPool() {
# Line 39 | Line 51 | public class ForkJoinTaskTest extends JS
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 <            assertTrue(pool.invoke(a) == null);
83 <        } finally {
84 <            joinPool(pool);
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      /*
# Line 54 | Line 208 | public class ForkJoinTaskTest extends JS
208       * differently than supplied Recursive forms.
209       */
210  
211 <    static final class FJException extends RuntimeException {
211 >    public static final class FJException extends RuntimeException {
212          FJException() { super(); }
213      }
214  
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 102 | 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 123 | Line 285 | public class ForkJoinTaskTest extends JS
285          }
286  
287          public final void completeExceptionally(Throwable ex) {
288 <            BinaryAsyncAction a = this;
127 <            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 179 | Line 340 | public class ForkJoinTaskTest extends JS
340          public final boolean exec() {
341              AsyncFib f = this;
342              int n = f.number;
343 <            if (n > 1) {
344 <                while (n > 1) {
345 <                    AsyncFib p = f;
346 <                    AsyncFib r = new AsyncFib(n - 2);
347 <                    f = new AsyncFib(--n);
348 <                    p.linkSubtasks(r, f);
188 <                    r.fork();
189 <                }
190 <                f.number = n;
343 >            while (n > 1) {
344 >                AsyncFib p = f;
345 >                AsyncFib r = new AsyncFib(n - 2);
346 >                f = new AsyncFib(--n);
347 >                p.linkSubtasks(r, f);
348 >                r.fork();
349              }
350              f.complete();
351              return false;
# Line 198 | Line 356 | public class ForkJoinTaskTest extends JS
356          }
357      }
358  
201
359      static final class FailingAsyncFib extends BinaryAsyncAction {
360          int number;
361          public FailingAsyncFib(int n) {
# Line 208 | Line 365 | public class ForkJoinTaskTest extends JS
365          public final boolean exec() {
366              FailingAsyncFib f = this;
367              int n = f.number;
368 <            if (n > 1) {
369 <                while (n > 1) {
370 <                    FailingAsyncFib p = f;
371 <                    FailingAsyncFib r = new FailingAsyncFib(n - 2);
372 <                    f = new FailingAsyncFib(--n);
373 <                    p.linkSubtasks(r, f);
217 <                    r.fork();
218 <                }
219 <                f.number = n;
368 >            while (n > 1) {
369 >                FailingAsyncFib p = f;
370 >                FailingAsyncFib r = new FailingAsyncFib(n - 2);
371 >                f = new FailingAsyncFib(--n);
372 >                p.linkSubtasks(r, f);
373 >                r.fork();
374              }
375              f.complete();
376              return false;
# Line 230 | Line 384 | public class ForkJoinTaskTest extends JS
384      /**
385       * invoke returns when task completes normally.
386       * isCompletedAbnormally and isCancelled return false for normally
387 <     * completed tasks. getRawResult of a RecursiveAction returns null;
387 >     * completed tasks; getRawResult returns null.
388       */
389      public void testInvoke() {
390 <        RecursiveAction a = new RecursiveAction() {
391 <            public void compute() {
390 >        RecursiveAction a = new CheckedRecursiveAction() {
391 >            protected void realCompute() {
392                  AsyncFib f = new AsyncFib(8);
393 <                f.invoke();
394 <                threadAssertTrue(f.number == 21);
395 <                threadAssertTrue(f.isDone());
242 <                threadAssertFalse(f.isCancelled());
243 <                threadAssertFalse(f.isCompletedAbnormally());
244 <                threadAssertTrue(f.getRawResult() == null);
393 >                assertNull(f.invoke());
394 >                assertEquals(21, f.number);
395 >                checkCompletedNormally(f);
396              }};
397          testInvokeOnPool(mainPool(), a);
398      }
# Line 252 | Line 403 | public class ForkJoinTaskTest extends JS
403       * completed tasks
404       */
405      public void testQuietlyInvoke() {
406 <        RecursiveAction a = new RecursiveAction() {
407 <            public void compute() {
406 >        RecursiveAction a = new CheckedRecursiveAction() {
407 >            protected void realCompute() {
408                  AsyncFib f = new AsyncFib(8);
409                  f.quietlyInvoke();
410 <                threadAssertTrue(f.number == 21);
411 <                threadAssertTrue(f.isDone());
261 <                threadAssertFalse(f.isCancelled());
262 <                threadAssertFalse(f.isCompletedAbnormally());
263 <                threadAssertTrue(f.getRawResult() == null);
410 >                assertEquals(21, f.number);
411 >                checkCompletedNormally(f);
412              }};
413          testInvokeOnPool(mainPool(), a);
414      }
# Line 269 | Line 417 | public class ForkJoinTaskTest extends JS
417       * join of a forked task returns when task completes
418       */
419      public void testForkJoin() {
420 <        RecursiveAction a = new RecursiveAction() {
421 <            public void compute() {
420 >        RecursiveAction a = new CheckedRecursiveAction() {
421 >            protected void realCompute() {
422                  AsyncFib f = new AsyncFib(8);
423 <                f.fork();
424 <                f.join();
425 <                threadAssertTrue(f.number == 21);
426 <                threadAssertTrue(f.isDone());
279 <                threadAssertTrue(f.getRawResult() == null);
423 >                assertSame(f, f.fork());
424 >                assertNull(f.join());
425 >                assertEquals(21, f.number);
426 >                checkCompletedNormally(f);
427              }};
428          testInvokeOnPool(mainPool(), a);
429      }
# Line 285 | Line 432 | public class ForkJoinTaskTest extends JS
432       * get of a forked task returns when task completes
433       */
434      public void testForkGet() {
435 <        RecursiveAction a = new RecursiveAction() {
436 <            public void compute() {
437 <                try {
438 <                    AsyncFib f = new AsyncFib(8);
439 <                    f.fork();
440 <                    f.get();
441 <                    threadAssertTrue(f.number == 21);
295 <                    threadAssertTrue(f.isDone());
296 <                } catch (Exception ex) {
297 <                    unexpectedException(ex);
298 <                }
435 >        RecursiveAction a = new CheckedRecursiveAction() {
436 >            protected void realCompute() throws Exception {
437 >                AsyncFib f = new AsyncFib(8);
438 >                assertSame(f, f.fork());
439 >                assertNull(f.get());
440 >                assertEquals(21, f.number);
441 >                checkCompletedNormally(f);
442              }};
443          testInvokeOnPool(mainPool(), a);
444      }
# Line 304 | Line 447 | public class ForkJoinTaskTest extends JS
447       * timed get of a forked task returns when task completes
448       */
449      public void testForkTimedGet() {
450 <        RecursiveAction a = new RecursiveAction() {
451 <            public void compute() {
452 <                try {
453 <                    AsyncFib f = new AsyncFib(8);
454 <                    f.fork();
455 <                    f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
456 <                    threadAssertTrue(f.number == 21);
314 <                    threadAssertTrue(f.isDone());
315 <                } catch (Exception ex) {
316 <                    unexpectedException(ex);
317 <                }
450 >        RecursiveAction a = new CheckedRecursiveAction() {
451 >            protected void realCompute() throws Exception {
452 >                AsyncFib f = new AsyncFib(8);
453 >                assertSame(f, f.fork());
454 >                assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
455 >                assertEquals(21, f.number);
456 >                checkCompletedNormally(f);
457              }};
458          testInvokeOnPool(mainPool(), a);
459      }
# Line 323 | Line 462 | public class ForkJoinTaskTest extends JS
462       * timed get with null time unit throws NPE
463       */
464      public void testForkTimedGetNPE() {
465 <        RecursiveAction a = new RecursiveAction() {
466 <            public void compute() {
465 >        RecursiveAction a = new CheckedRecursiveAction() {
466 >            protected void realCompute() throws Exception {
467 >                AsyncFib f = new AsyncFib(8);
468 >                assertSame(f, f.fork());
469                  try {
329                    AsyncFib f = new AsyncFib(8);
330                    f.fork();
470                      f.get(5L, null);
471                      shouldThrow();
472 <                } catch (NullPointerException success) {
334 <                } catch (Exception ex) {
335 <                    unexpectedException(ex);
336 <                }
472 >                } catch (NullPointerException success) {}
473              }};
474          testInvokeOnPool(mainPool(), a);
475      }
# Line 342 | Line 478 | public class ForkJoinTaskTest extends JS
478       * quietlyJoin of a forked task returns when task completes
479       */
480      public void testForkQuietlyJoin() {
481 <        RecursiveAction a = new RecursiveAction() {
482 <            public void compute() {
481 >        RecursiveAction a = new CheckedRecursiveAction() {
482 >            protected void realCompute() {
483                  AsyncFib f = new AsyncFib(8);
484 <                f.fork();
484 >                assertSame(f, f.fork());
485                  f.quietlyJoin();
486 <                threadAssertTrue(f.number == 21);
487 <                threadAssertTrue(f.isDone());
486 >                assertEquals(21, f.number);
487 >                checkCompletedNormally(f);
488              }};
489          testInvokeOnPool(mainPool(), a);
490      }
491  
356
492      /**
493       * helpQuiesce returns when tasks are complete.
494       * getQueuedTaskCount returns 0 when quiescent
495       */
496      public void testForkHelpQuiesce() {
497 <        RecursiveAction a = new RecursiveAction() {
498 <            public void compute() {
497 >        RecursiveAction a = new CheckedRecursiveAction() {
498 >            protected void realCompute() {
499                  AsyncFib f = new AsyncFib(8);
500 <                f.fork();
501 <                f.helpQuiesce();
502 <                threadAssertTrue(f.number == 21);
503 <                threadAssertTrue(f.isDone());
504 <                threadAssertTrue(getQueuedTaskCount() == 0);
500 >                assertSame(f, f.fork());
501 >                helpQuiesce();
502 >                assertEquals(21, f.number);
503 >                assertEquals(0, getQueuedTaskCount());
504 >                checkCompletedNormally(f);
505              }};
506          testInvokeOnPool(mainPool(), a);
507      }
508  
374
509      /**
510       * invoke task throws exception when task completes abnormally
511       */
512      public void testAbnormalInvoke() {
513 <        RecursiveAction a = new RecursiveAction() {
514 <            public void compute() {
513 >        RecursiveAction a = new CheckedRecursiveAction() {
514 >            protected void realCompute() {
515 >                FailingAsyncFib f = new FailingAsyncFib(8);
516                  try {
382                    FailingAsyncFib f = new FailingAsyncFib(8);
517                      f.invoke();
518                      shouldThrow();
519                  } catch (FJException success) {
520 +                    checkCompletedAbnormally(f, success);
521                  }
522              }};
523          testInvokeOnPool(mainPool(), a);
# Line 392 | Line 527 | public class ForkJoinTaskTest extends JS
527       * quietlyInvoke task returns when task completes abnormally
528       */
529      public void testAbnormalQuietlyInvoke() {
530 <        RecursiveAction a = new RecursiveAction() {
531 <            public void compute() {
530 >        RecursiveAction a = new CheckedRecursiveAction() {
531 >            protected void realCompute() {
532                  FailingAsyncFib f = new FailingAsyncFib(8);
533                  f.quietlyInvoke();
534 <                threadAssertTrue(f.isDone());
534 >                assertTrue(f.getException() instanceof FJException);
535 >                checkCompletedAbnormally(f, f.getException());
536              }};
537          testInvokeOnPool(mainPool(), a);
538      }
# Line 405 | Line 541 | public class ForkJoinTaskTest extends JS
541       * join of a forked task throws exception when task completes abnormally
542       */
543      public void testAbnormalForkJoin() {
544 <        RecursiveAction a = new RecursiveAction() {
545 <            public void compute() {
544 >        RecursiveAction a = new CheckedRecursiveAction() {
545 >            protected void realCompute() {
546 >                FailingAsyncFib f = new FailingAsyncFib(8);
547 >                assertSame(f, f.fork());
548                  try {
411                    FailingAsyncFib f = new FailingAsyncFib(8);
412                    f.fork();
549                      f.join();
550                      shouldThrow();
551                  } catch (FJException success) {
552 +                    checkCompletedAbnormally(f, success);
553                  }
554              }};
555          testInvokeOnPool(mainPool(), a);
# Line 422 | Line 559 | public class ForkJoinTaskTest extends JS
559       * get of a forked task throws exception when task completes abnormally
560       */
561      public void testAbnormalForkGet() {
562 <        RecursiveAction a = new RecursiveAction() {
563 <            public void compute() {
562 >        RecursiveAction a = new CheckedRecursiveAction() {
563 >            protected void realCompute() throws Exception {
564 >                FailingAsyncFib f = new FailingAsyncFib(8);
565 >                assertSame(f, f.fork());
566                  try {
428                    FailingAsyncFib f = new FailingAsyncFib(8);
429                    f.fork();
567                      f.get();
568                      shouldThrow();
569                  } catch (ExecutionException success) {
570 <                } catch (Exception ex) {
571 <                    unexpectedException(ex);
570 >                    Throwable cause = success.getCause();
571 >                    assertTrue(cause instanceof FJException);
572 >                    checkCompletedAbnormally(f, cause);
573                  }
574              }};
575          testInvokeOnPool(mainPool(), a);
# Line 441 | Line 579 | public class ForkJoinTaskTest extends JS
579       * timed get of a forked task throws exception when task completes abnormally
580       */
581      public void testAbnormalForkTimedGet() {
582 <        RecursiveAction a = new RecursiveAction() {
583 <            public void compute() {
582 >        RecursiveAction a = new CheckedRecursiveAction() {
583 >            protected void realCompute() throws Exception {
584 >                FailingAsyncFib f = new FailingAsyncFib(8);
585 >                assertSame(f, f.fork());
586                  try {
587 <                    FailingAsyncFib f = new FailingAsyncFib(8);
448 <                    f.fork();
449 <                    f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
587 >                    f.get(LONG_DELAY_MS, MILLISECONDS);
588                      shouldThrow();
589                  } catch (ExecutionException success) {
590 <                } catch (Exception ex) {
591 <                    unexpectedException(ex);
590 >                    Throwable cause = success.getCause();
591 >                    assertTrue(cause instanceof FJException);
592 >                    checkCompletedAbnormally(f, cause);
593                  }
594              }};
595          testInvokeOnPool(mainPool(), a);
# Line 460 | Line 599 | public class ForkJoinTaskTest extends JS
599       * quietlyJoin of a forked task returns when task completes abnormally
600       */
601      public void testAbnormalForkQuietlyJoin() {
602 <        RecursiveAction a = new RecursiveAction() {
603 <            public void compute() {
602 >        RecursiveAction a = new CheckedRecursiveAction() {
603 >            protected void realCompute() {
604                  FailingAsyncFib f = new FailingAsyncFib(8);
605 <                f.fork();
605 >                assertSame(f, f.fork());
606                  f.quietlyJoin();
607 <                threadAssertTrue(f.isDone());
608 <                threadAssertTrue(f.isCompletedAbnormally());
470 <                threadAssertTrue(f.getException() instanceof FJException);
607 >                assertTrue(f.getException() instanceof FJException);
608 >                checkCompletedAbnormally(f, f.getException());
609              }};
610          testInvokeOnPool(mainPool(), a);
611      }
# Line 476 | Line 614 | public class ForkJoinTaskTest extends JS
614       * invoke task throws exception when task cancelled
615       */
616      public void testCancelledInvoke() {
617 <        RecursiveAction a = new RecursiveAction() {
618 <            public void compute() {
617 >        RecursiveAction a = new CheckedRecursiveAction() {
618 >            protected void realCompute() {
619 >                AsyncFib f = new AsyncFib(8);
620 >                assertTrue(f.cancel(true));
621                  try {
482                    AsyncFib f = new AsyncFib(8);
483                    f.cancel(true);
622                      f.invoke();
623                      shouldThrow();
624                  } catch (CancellationException success) {
625 +                    checkCancelled(f);
626                  }
627              }};
628          testInvokeOnPool(mainPool(), a);
# Line 493 | Line 632 | public class ForkJoinTaskTest extends JS
632       * join of a forked task throws exception when task cancelled
633       */
634      public void testCancelledForkJoin() {
635 <        RecursiveAction a = new RecursiveAction() {
636 <            public void compute() {
635 >        RecursiveAction a = new CheckedRecursiveAction() {
636 >            protected void realCompute() {
637 >                AsyncFib f = new AsyncFib(8);
638 >                assertTrue(f.cancel(true));
639 >                assertSame(f, f.fork());
640                  try {
499                    AsyncFib f = new AsyncFib(8);
500                    f.cancel(true);
501                    f.fork();
641                      f.join();
642                      shouldThrow();
643                  } catch (CancellationException success) {
644 +                    checkCancelled(f);
645                  }
646              }};
647          testInvokeOnPool(mainPool(), a);
# Line 511 | Line 651 | public class ForkJoinTaskTest extends JS
651       * get of a forked task throws exception when task cancelled
652       */
653      public void testCancelledForkGet() {
654 <        RecursiveAction a = new RecursiveAction() {
655 <            public void compute() {
654 >        RecursiveAction a = new CheckedRecursiveAction() {
655 >            protected void realCompute() throws Exception {
656 >                AsyncFib f = new AsyncFib(8);
657 >                assertTrue(f.cancel(true));
658 >                assertSame(f, f.fork());
659                  try {
517                    AsyncFib f = new AsyncFib(8);
518                    f.cancel(true);
519                    f.fork();
660                      f.get();
661                      shouldThrow();
662                  } catch (CancellationException success) {
663 <                } catch (Exception ex) {
524 <                    unexpectedException(ex);
663 >                    checkCancelled(f);
664                  }
665              }};
666          testInvokeOnPool(mainPool(), a);
# Line 530 | Line 669 | public class ForkJoinTaskTest extends JS
669      /**
670       * timed get of a forked task throws exception when task cancelled
671       */
672 <    public void testCancelledForkTimedGet() {
673 <        RecursiveAction a = new RecursiveAction() {
674 <            public void compute() {
672 >    public void testCancelledForkTimedGet() throws Exception {
673 >        RecursiveAction a = new CheckedRecursiveAction() {
674 >            protected void realCompute() throws Exception {
675 >                AsyncFib f = new AsyncFib(8);
676 >                assertTrue(f.cancel(true));
677 >                assertSame(f, f.fork());
678                  try {
679 <                    AsyncFib f = new AsyncFib(8);
538 <                    f.cancel(true);
539 <                    f.fork();
540 <                    f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
679 >                    f.get(LONG_DELAY_MS, MILLISECONDS);
680                      shouldThrow();
681                  } catch (CancellationException success) {
682 <                } catch (Exception ex) {
544 <                    unexpectedException(ex);
682 >                    checkCancelled(f);
683                  }
684              }};
685          testInvokeOnPool(mainPool(), a);
# Line 551 | Line 689 | public class ForkJoinTaskTest extends JS
689       * quietlyJoin of a forked task returns when task cancelled
690       */
691      public void testCancelledForkQuietlyJoin() {
692 <        RecursiveAction a = new RecursiveAction() {
693 <            public void compute() {
692 >        RecursiveAction a = new CheckedRecursiveAction() {
693 >            protected void realCompute() {
694                  AsyncFib f = new AsyncFib(8);
695 <                f.cancel(true);
696 <                f.fork();
695 >                assertTrue(f.cancel(true));
696 >                assertSame(f, f.fork());
697                  f.quietlyJoin();
698 <                threadAssertTrue(f.isDone());
561 <                threadAssertTrue(f.isCompletedAbnormally());
562 <                threadAssertTrue(f.getException() instanceof CancellationException);
698 >                checkCancelled(f);
699              }};
700          testInvokeOnPool(mainPool(), a);
701      }
# Line 569 | Line 705 | public class ForkJoinTaskTest extends JS
705       */
706      public void testGetPool() {
707          final ForkJoinPool mainPool = mainPool();
708 <        RecursiveAction a = new RecursiveAction() {
709 <            public void compute() {
710 <                threadAssertTrue(getPool() == mainPool);
708 >        RecursiveAction a = new CheckedRecursiveAction() {
709 >            protected void realCompute() {
710 >                assertSame(mainPool, getPool());
711              }};
712          testInvokeOnPool(mainPool, a);
713      }
# Line 580 | Line 716 | public class ForkJoinTaskTest extends JS
716       * getPool of non-FJ task returns null
717       */
718      public void testGetPool2() {
719 <        RecursiveAction a = new RecursiveAction() {
720 <            public void compute() {
721 <                threadAssertTrue(getPool() == null);
719 >        RecursiveAction a = new CheckedRecursiveAction() {
720 >            protected void realCompute() {
721 >                assertNull(getPool());
722              }};
723 <        a.invoke();
723 >        assertNull(a.invoke());
724      }
725  
726      /**
727       * inForkJoinPool of executing task returns true
728       */
729      public void testInForkJoinPool() {
730 <        RecursiveAction a = new RecursiveAction() {
731 <            public void compute() {
732 <                threadAssertTrue(inForkJoinPool());
730 >        RecursiveAction a = new CheckedRecursiveAction() {
731 >            protected void realCompute() {
732 >                assertTrue(inForkJoinPool());
733              }};
734          testInvokeOnPool(mainPool(), a);
735      }
# Line 602 | Line 738 | public class ForkJoinTaskTest extends JS
738       * inForkJoinPool of non-FJ task returns false
739       */
740      public void testInForkJoinPool2() {
741 <        RecursiveAction a = new RecursiveAction() {
742 <            public void compute() {
743 <                threadAssertTrue(!inForkJoinPool());
741 >        RecursiveAction a = new CheckedRecursiveAction() {
742 >            protected void realCompute() {
743 >                assertFalse(inForkJoinPool());
744              }};
745 <        a.invoke();
745 >        assertNull(a.invoke());
746      }
747  
748      /**
749       * setRawResult(null) succeeds
750       */
751      public void testSetRawResult() {
752 <        RecursiveAction a = new RecursiveAction() {
753 <            public void compute() {
752 >        RecursiveAction a = new CheckedRecursiveAction() {
753 >            protected void realCompute() {
754                  setRawResult(null);
755 +                assertNull(getRawResult());
756              }};
757 <        a.invoke();
757 >        assertNull(a.invoke());
758      }
759  
760      /**
761       * invoke task throws exception after invoking completeExceptionally
762       */
763      public void testCompleteExceptionally() {
764 <        RecursiveAction a = new RecursiveAction() {
765 <            public void compute() {
764 >        RecursiveAction a = new CheckedRecursiveAction() {
765 >            protected void realCompute() {
766 >                AsyncFib f = new AsyncFib(8);
767 >                f.completeExceptionally(new FJException());
768                  try {
630                    AsyncFib f = new AsyncFib(8);
631                    f.completeExceptionally(new FJException());
769                      f.invoke();
770                      shouldThrow();
771                  } catch (FJException success) {
772 +                    checkCompletedAbnormally(f, success);
773 +                }
774 +            }};
775 +        testInvokeOnPool(mainPool(), a);
776 +    }
777 +
778 +    /**
779 +     * completeExceptionally(null) surprisingly has the same effect as
780 +     * completeExceptionally(new RuntimeException())
781 +     */
782 +    public void testCompleteExceptionally_null() {
783 +        RecursiveAction a = new CheckedRecursiveAction() {
784 +            protected void realCompute() {
785 +                AsyncFib f = new AsyncFib(8);
786 +                f.completeExceptionally(null);
787 +                try {
788 +                    f.invoke();
789 +                    shouldThrow();
790 +                } catch (RuntimeException success) {
791 +                    assertSame(success.getClass(), RuntimeException.class);
792 +                    assertNull(success.getCause());
793 +                    checkCompletedAbnormally(f, success);
794                  }
795              }};
796          testInvokeOnPool(mainPool(), a);
# Line 641 | Line 800 | public class ForkJoinTaskTest extends JS
800       * invokeAll(t1, t2) invokes all task arguments
801       */
802      public void testInvokeAll2() {
803 <        RecursiveAction a = new RecursiveAction() {
804 <            public void compute() {
803 >        RecursiveAction a = new CheckedRecursiveAction() {
804 >            protected void realCompute() {
805                  AsyncFib f = new AsyncFib(8);
806                  AsyncFib g = new AsyncFib(9);
807                  invokeAll(f, g);
808 <                threadAssertTrue(f.isDone());
809 <                threadAssertTrue(f.number == 21);
810 <                threadAssertTrue(g.isDone());
811 <                threadAssertTrue(g.number == 34);
808 >                assertEquals(21, f.number);
809 >                assertEquals(34, g.number);
810 >                checkCompletedNormally(f);
811 >                checkCompletedNormally(g);
812              }};
813          testInvokeOnPool(mainPool(), a);
814      }
# Line 658 | Line 817 | public class ForkJoinTaskTest extends JS
817       * invokeAll(tasks) with 1 argument invokes task
818       */
819      public void testInvokeAll1() {
820 <        RecursiveAction a = new RecursiveAction() {
821 <            public void compute() {
820 >        RecursiveAction a = new CheckedRecursiveAction() {
821 >            protected void realCompute() {
822                  AsyncFib f = new AsyncFib(8);
823                  invokeAll(f);
824 <                threadAssertTrue(f.isDone());
825 <                threadAssertTrue(f.number == 21);
824 >                checkCompletedNormally(f);
825 >                assertEquals(21, f.number);
826              }};
827          testInvokeOnPool(mainPool(), a);
828      }
# Line 672 | Line 831 | public class ForkJoinTaskTest extends JS
831       * invokeAll(tasks) with > 2 argument invokes tasks
832       */
833      public void testInvokeAll3() {
834 <        RecursiveAction a = new RecursiveAction() {
835 <            public void compute() {
834 >        RecursiveAction a = new CheckedRecursiveAction() {
835 >            protected void realCompute() {
836                  AsyncFib f = new AsyncFib(8);
837                  AsyncFib g = new AsyncFib(9);
838                  AsyncFib h = new AsyncFib(7);
839                  invokeAll(f, g, h);
840 <                threadAssertTrue(f.isDone());
841 <                threadAssertTrue(f.number == 21);
842 <                threadAssertTrue(g.isDone());
843 <                threadAssertTrue(g.number == 34);
844 <                threadAssertTrue(h.isDone());
845 <                threadAssertTrue(h.number == 13);
840 >                assertEquals(21, f.number);
841 >                assertEquals(34, g.number);
842 >                assertEquals(13, h.number);
843 >                checkCompletedNormally(f);
844 >                checkCompletedNormally(g);
845 >                checkCompletedNormally(h);
846              }};
847          testInvokeOnPool(mainPool(), a);
848      }
# Line 692 | Line 851 | public class ForkJoinTaskTest extends JS
851       * invokeAll(collection) invokes all tasks in the collection
852       */
853      public void testInvokeAllCollection() {
854 <        RecursiveAction a = new RecursiveAction() {
855 <            public void compute() {
854 >        RecursiveAction a = new CheckedRecursiveAction() {
855 >            protected void realCompute() {
856                  AsyncFib f = new AsyncFib(8);
857                  AsyncFib g = new AsyncFib(9);
858                  AsyncFib h = new AsyncFib(7);
# Line 702 | Line 861 | public class ForkJoinTaskTest extends JS
861                  set.add(g);
862                  set.add(h);
863                  invokeAll(set);
864 <                threadAssertTrue(f.isDone());
865 <                threadAssertTrue(f.number == 21);
866 <                threadAssertTrue(g.isDone());
867 <                threadAssertTrue(g.number == 34);
868 <                threadAssertTrue(h.isDone());
869 <                threadAssertTrue(h.number == 13);
864 >                assertEquals(21, f.number);
865 >                assertEquals(34, g.number);
866 >                assertEquals(13, h.number);
867 >                checkCompletedNormally(f);
868 >                checkCompletedNormally(g);
869 >                checkCompletedNormally(h);
870              }};
871          testInvokeOnPool(mainPool(), a);
872      }
873  
715
874      /**
875       * invokeAll(tasks) with any null task throws NPE
876       */
877      public void testInvokeAllNPE() {
878 <        RecursiveAction a = new RecursiveAction() {
879 <            public void compute() {
878 >        RecursiveAction a = new CheckedRecursiveAction() {
879 >            protected void realCompute() {
880 >                AsyncFib f = new AsyncFib(8);
881 >                AsyncFib g = new AsyncFib(9);
882 >                AsyncFib h = null;
883                  try {
723                    AsyncFib f = new AsyncFib(8);
724                    AsyncFib g = new AsyncFib(9);
725                    AsyncFib h = null;
884                      invokeAll(f, g, h);
885                      shouldThrow();
886 <                } catch (NullPointerException success) {
729 <                }
886 >                } catch (NullPointerException success) {}
887              }};
888          testInvokeOnPool(mainPool(), a);
889      }
# Line 735 | Line 892 | public class ForkJoinTaskTest extends JS
892       * invokeAll(t1, t2) throw exception if any task does
893       */
894      public void testAbnormalInvokeAll2() {
895 <        RecursiveAction a = new RecursiveAction() {
896 <            public void compute() {
895 >        RecursiveAction a = new CheckedRecursiveAction() {
896 >            protected void realCompute() {
897 >                AsyncFib f = new AsyncFib(8);
898 >                FailingAsyncFib g = new FailingAsyncFib(9);
899 >                ForkJoinTask[] tasks = { f, g };
900 >                shuffle(tasks);
901                  try {
902 <                    AsyncFib f = new AsyncFib(8);
742 <                    FailingAsyncFib g = new FailingAsyncFib(9);
743 <                    invokeAll(f, g);
902 >                    invokeAll(tasks);
903                      shouldThrow();
904                  } catch (FJException success) {
905 +                    checkCompletedAbnormally(g, success);
906                  }
907              }};
908          testInvokeOnPool(mainPool(), a);
# Line 752 | Line 912 | public class ForkJoinTaskTest extends JS
912       * invokeAll(tasks) with 1 argument throws exception if task does
913       */
914      public void testAbnormalInvokeAll1() {
915 <        RecursiveAction a = new RecursiveAction() {
916 <            public void compute() {
915 >        RecursiveAction a = new CheckedRecursiveAction() {
916 >            protected void realCompute() {
917 >                FailingAsyncFib g = new FailingAsyncFib(9);
918                  try {
758                    FailingAsyncFib g = new FailingAsyncFib(9);
919                      invokeAll(g);
920                      shouldThrow();
921                  } catch (FJException success) {
922 +                    checkCompletedAbnormally(g, success);
923                  }
924              }};
925          testInvokeOnPool(mainPool(), a);
# Line 768 | Line 929 | public class ForkJoinTaskTest extends JS
929       * invokeAll(tasks) with > 2 argument throws exception if any task does
930       */
931      public void testAbnormalInvokeAll3() {
932 <        RecursiveAction a = new RecursiveAction() {
933 <            public void compute() {
932 >        RecursiveAction a = new CheckedRecursiveAction() {
933 >            protected void realCompute() {
934 >                AsyncFib f = new AsyncFib(8);
935 >                FailingAsyncFib g = new FailingAsyncFib(9);
936 >                AsyncFib h = new AsyncFib(7);
937 >                ForkJoinTask[] tasks = { f, g, h };
938 >                shuffle(tasks);
939                  try {
940 <                    AsyncFib f = new AsyncFib(8);
775 <                    FailingAsyncFib g = new FailingAsyncFib(9);
776 <                    AsyncFib h = new AsyncFib(7);
777 <                    invokeAll(f, g, h);
940 >                    invokeAll(tasks);
941                      shouldThrow();
942                  } catch (FJException success) {
943 +                    checkCompletedAbnormally(g, success);
944                  }
945              }};
946          testInvokeOnPool(mainPool(), a);
947      }
948  
949      /**
950 <     * invokeAll(collection)  throws exception if any task does
950 >     * invokeAll(collection) throws exception if any task does
951       */
952      public void testAbnormalInvokeAllCollection() {
953 <        RecursiveAction a = new RecursiveAction() {
954 <            public void compute() {
953 >        RecursiveAction a = new CheckedRecursiveAction() {
954 >            protected void realCompute() {
955 >                FailingAsyncFib f = new FailingAsyncFib(8);
956 >                AsyncFib g = new AsyncFib(9);
957 >                AsyncFib h = new AsyncFib(7);
958 >                ForkJoinTask[] tasks = { f, g, h };
959 >                shuffle(tasks);
960                  try {
961 <                    FailingAsyncFib f = new FailingAsyncFib(8);
793 <                    AsyncFib g = new AsyncFib(9);
794 <                    AsyncFib h = new AsyncFib(7);
795 <                    HashSet set = new HashSet();
796 <                    set.add(f);
797 <                    set.add(g);
798 <                    set.add(h);
799 <                    invokeAll(set);
961 >                    invokeAll(Arrays.asList(tasks));
962                      shouldThrow();
963                  } catch (FJException success) {
964 +                    checkCompletedAbnormally(f, success);
965                  }
966              }};
967          testInvokeOnPool(mainPool(), a);
# Line 809 | Line 972 | public class ForkJoinTaskTest extends JS
972       * and suppresses execution
973       */
974      public void testTryUnfork() {
975 <        RecursiveAction a = new RecursiveAction() {
976 <            public void compute() {
975 >        RecursiveAction a = new CheckedRecursiveAction() {
976 >            protected void realCompute() {
977                  AsyncFib g = new AsyncFib(9);
978 <                g.fork();
978 >                assertSame(g, g.fork());
979                  AsyncFib f = new AsyncFib(8);
980 <                f.fork();
981 <                threadAssertTrue(f.tryUnfork());
980 >                assertSame(f, f.fork());
981 >                assertTrue(f.tryUnfork());
982                  helpQuiesce();
983 <                threadAssertFalse(f.isDone());
984 <                threadAssertTrue(g.isDone());
983 >                checkNotDone(f);
984 >                checkCompletedNormally(g);
985              }};
986          testInvokeOnPool(singletonPool(), a);
987      }
# Line 828 | Line 991 | public class ForkJoinTaskTest extends JS
991       * there are more tasks than threads
992       */
993      public void testGetSurplusQueuedTaskCount() {
994 <        RecursiveAction a = new RecursiveAction() {
995 <            public void compute() {
994 >        RecursiveAction a = new CheckedRecursiveAction() {
995 >            protected void realCompute() {
996                  AsyncFib h = new AsyncFib(7);
997 <                h.fork();
997 >                assertSame(h, h.fork());
998                  AsyncFib g = new AsyncFib(9);
999 <                g.fork();
999 >                assertSame(g, g.fork());
1000                  AsyncFib f = new AsyncFib(8);
1001 <                f.fork();
1002 <                threadAssertTrue(getSurplusQueuedTaskCount() > 0);
1001 >                assertSame(f, f.fork());
1002 >                assertTrue(getSurplusQueuedTaskCount() > 0);
1003                  helpQuiesce();
1004 +                assertEquals(0, getSurplusQueuedTaskCount());
1005 +                checkCompletedNormally(f);
1006 +                checkCompletedNormally(g);
1007 +                checkCompletedNormally(h);
1008              }};
1009          testInvokeOnPool(singletonPool(), a);
1010      }
# Line 846 | Line 1013 | public class ForkJoinTaskTest extends JS
1013       * peekNextLocalTask returns most recent unexecuted task.
1014       */
1015      public void testPeekNextLocalTask() {
1016 <        RecursiveAction a = new RecursiveAction() {
1017 <            public void compute() {
1016 >        RecursiveAction a = new CheckedRecursiveAction() {
1017 >            protected void realCompute() {
1018                  AsyncFib g = new AsyncFib(9);
1019 <                g.fork();
1019 >                assertSame(g, g.fork());
1020                  AsyncFib f = new AsyncFib(8);
1021 <                f.fork();
1022 <                threadAssertTrue(peekNextLocalTask() == f);
1023 <                f.join();
1024 <                threadAssertTrue(f.isDone());
1021 >                assertSame(f, f.fork());
1022 >                assertSame(f, peekNextLocalTask());
1023 >                assertNull(f.join());
1024 >                checkCompletedNormally(f);
1025                  helpQuiesce();
1026 +                checkCompletedNormally(g);
1027              }};
1028          testInvokeOnPool(singletonPool(), a);
1029      }
1030  
1031      /**
1032 <     * pollNextLocalTask returns most recent unexecuted task
1033 <     * without executing it
1032 >     * pollNextLocalTask returns most recent unexecuted task without
1033 >     * executing it
1034       */
1035      public void testPollNextLocalTask() {
1036 <        RecursiveAction a = new RecursiveAction() {
1037 <            public void compute() {
1036 >        RecursiveAction a = new CheckedRecursiveAction() {
1037 >            protected void realCompute() {
1038                  AsyncFib g = new AsyncFib(9);
1039 <                g.fork();
1039 >                assertSame(g, g.fork());
1040                  AsyncFib f = new AsyncFib(8);
1041 <                f.fork();
1042 <                threadAssertTrue(pollNextLocalTask() == f);
1041 >                assertSame(f, f.fork());
1042 >                assertSame(f, pollNextLocalTask());
1043                  helpQuiesce();
1044 <                threadAssertFalse(f.isDone());
1044 >                checkNotDone(f);
1045 >                assertEquals(34, g.number);
1046 >                checkCompletedNormally(g);
1047              }};
1048          testInvokeOnPool(singletonPool(), a);
1049      }
1050  
1051      /**
1052 <     * pollTask returns an unexecuted task
883 <     * without executing it
1052 >     * pollTask returns an unexecuted task without executing it
1053       */
1054      public void testPollTask() {
1055 <        RecursiveAction a = new RecursiveAction() {
1056 <            public void compute() {
1055 >        RecursiveAction a = new CheckedRecursiveAction() {
1056 >            protected void realCompute() {
1057                  AsyncFib g = new AsyncFib(9);
1058 <                g.fork();
1058 >                assertSame(g, g.fork());
1059                  AsyncFib f = new AsyncFib(8);
1060 <                f.fork();
1061 <                threadAssertTrue(pollTask() == f);
1060 >                assertSame(f, f.fork());
1061 >                assertSame(f, pollTask());
1062                  helpQuiesce();
1063 <                threadAssertFalse(f.isDone());
1064 <                threadAssertTrue(g.isDone());
1063 >                checkNotDone(f);
1064 >                checkCompletedNormally(g);
1065              }};
1066          testInvokeOnPool(singletonPool(), a);
1067      }
# Line 901 | Line 1070 | public class ForkJoinTaskTest extends JS
1070       * peekNextLocalTask returns least recent unexecuted task in async mode
1071       */
1072      public void testPeekNextLocalTaskAsync() {
1073 <        RecursiveAction a = new RecursiveAction() {
1074 <            public void compute() {
1073 >        RecursiveAction a = new CheckedRecursiveAction() {
1074 >            protected void realCompute() {
1075                  AsyncFib g = new AsyncFib(9);
1076 <                g.fork();
1076 >                assertSame(g, g.fork());
1077                  AsyncFib f = new AsyncFib(8);
1078 <                f.fork();
1079 <                threadAssertTrue(peekNextLocalTask() == g);
1080 <                f.join();
1078 >                assertSame(f, f.fork());
1079 >                assertSame(g, peekNextLocalTask());
1080 >                assertNull(f.join());
1081                  helpQuiesce();
1082 <                threadAssertTrue(f.isDone());
1082 >                checkCompletedNormally(f);
1083 >                assertEquals(34, g.number);
1084 >                checkCompletedNormally(g);
1085              }};
1086          testInvokeOnPool(asyncSingletonPool(), a);
1087      }
1088  
1089      /**
1090 <     * pollNextLocalTask returns least recent unexecuted task
1091 <     * without executing it, in async mode
1090 >     * pollNextLocalTask returns least recent unexecuted task without
1091 >     * executing it, in async mode
1092       */
1093      public void testPollNextLocalTaskAsync() {
1094 <        RecursiveAction a = new RecursiveAction() {
1095 <            public void compute() {
1094 >        RecursiveAction a = new CheckedRecursiveAction() {
1095 >            protected void realCompute() {
1096                  AsyncFib g = new AsyncFib(9);
1097 <                g.fork();
1097 >                assertSame(g, g.fork());
1098                  AsyncFib f = new AsyncFib(8);
1099 <                f.fork();
1100 <                threadAssertTrue(pollNextLocalTask() == g);
1099 >                assertSame(f, f.fork());
1100 >                assertSame(g, pollNextLocalTask());
1101                  helpQuiesce();
1102 <                threadAssertTrue(f.isDone());
1103 <                threadAssertFalse(g.isDone());
1102 >                assertEquals(21, f.number);
1103 >                checkCompletedNormally(f);
1104 >                checkNotDone(g);
1105              }};
1106          testInvokeOnPool(asyncSingletonPool(), a);
1107      }
1108  
1109      /**
1110 <     * pollTask returns an unexecuted task
1111 <     * without executing it, in async mode
1110 >     * pollTask returns an unexecuted task without executing it, in
1111 >     * async mode
1112       */
1113      public void testPollTaskAsync() {
1114 <        RecursiveAction a = new RecursiveAction() {
1115 <            public void compute() {
1114 >        RecursiveAction a = new CheckedRecursiveAction() {
1115 >            protected void realCompute() {
1116                  AsyncFib g = new AsyncFib(9);
1117 <                g.fork();
1117 >                assertSame(g, g.fork());
1118                  AsyncFib f = new AsyncFib(8);
1119 <                f.fork();
1120 <                threadAssertTrue(pollTask() == g);
1119 >                assertSame(f, f.fork());
1120 >                assertSame(g, pollTask());
1121                  helpQuiesce();
1122 <                threadAssertTrue(f.isDone());
1123 <                threadAssertFalse(g.isDone());
1122 >                assertEquals(21, f.number);
1123 >                checkCompletedNormally(f);
1124 >                checkNotDone(g);
1125              }};
1126          testInvokeOnPool(asyncSingletonPool(), a);
1127      }
1128 +
1129 +    // versions for singleton pools
1130 +
1131 +    /**
1132 +     * invoke returns when task completes normally.
1133 +     * isCompletedAbnormally and isCancelled return false for normally
1134 +     * completed tasks; getRawResult returns null.
1135 +     */
1136 +    public void testInvokeSingleton() {
1137 +        RecursiveAction a = new CheckedRecursiveAction() {
1138 +            protected void realCompute() {
1139 +                AsyncFib f = new AsyncFib(8);
1140 +                assertNull(f.invoke());
1141 +                assertEquals(21, f.number);
1142 +                checkCompletedNormally(f);
1143 +            }};
1144 +        testInvokeOnPool(singletonPool(), a);
1145 +    }
1146 +
1147 +    /**
1148 +     * quietlyInvoke task returns when task completes normally.
1149 +     * isCompletedAbnormally and isCancelled return false for normally
1150 +     * completed tasks
1151 +     */
1152 +    public void testQuietlyInvokeSingleton() {
1153 +        RecursiveAction a = new CheckedRecursiveAction() {
1154 +            protected void realCompute() {
1155 +                AsyncFib f = new AsyncFib(8);
1156 +                f.quietlyInvoke();
1157 +                assertEquals(21, f.number);
1158 +                checkCompletedNormally(f);
1159 +            }};
1160 +        testInvokeOnPool(singletonPool(), a);
1161 +    }
1162 +
1163 +    /**
1164 +     * join of a forked task returns when task completes
1165 +     */
1166 +    public void testForkJoinSingleton() {
1167 +        RecursiveAction a = new CheckedRecursiveAction() {
1168 +            protected void realCompute() {
1169 +                AsyncFib f = new AsyncFib(8);
1170 +                assertSame(f, f.fork());
1171 +                assertNull(f.join());
1172 +                assertEquals(21, f.number);
1173 +                checkCompletedNormally(f);
1174 +            }};
1175 +        testInvokeOnPool(singletonPool(), a);
1176 +    }
1177 +
1178 +    /**
1179 +     * get of a forked task returns when task completes
1180 +     */
1181 +    public void testForkGetSingleton() {
1182 +        RecursiveAction a = new CheckedRecursiveAction() {
1183 +            protected void realCompute() throws Exception {
1184 +                AsyncFib f = new AsyncFib(8);
1185 +                assertSame(f, f.fork());
1186 +                assertNull(f.get());
1187 +                assertEquals(21, f.number);
1188 +                checkCompletedNormally(f);
1189 +            }};
1190 +        testInvokeOnPool(singletonPool(), a);
1191 +    }
1192 +
1193 +    /**
1194 +     * timed get of a forked task returns when task completes
1195 +     */
1196 +    public void testForkTimedGetSingleton() {
1197 +        RecursiveAction a = new CheckedRecursiveAction() {
1198 +            protected void realCompute() throws Exception {
1199 +                AsyncFib f = new AsyncFib(8);
1200 +                assertSame(f, f.fork());
1201 +                assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
1202 +                assertEquals(21, f.number);
1203 +                checkCompletedNormally(f);
1204 +            }};
1205 +        testInvokeOnPool(singletonPool(), a);
1206 +    }
1207 +
1208 +    /**
1209 +     * timed get with null time unit throws NPE
1210 +     */
1211 +    public void testForkTimedGetNPESingleton() {
1212 +        RecursiveAction a = new CheckedRecursiveAction() {
1213 +            protected void realCompute() throws Exception {
1214 +                AsyncFib f = new AsyncFib(8);
1215 +                assertSame(f, f.fork());
1216 +                try {
1217 +                    f.get(5L, null);
1218 +                    shouldThrow();
1219 +                } catch (NullPointerException success) {}
1220 +            }};
1221 +        testInvokeOnPool(singletonPool(), a);
1222 +    }
1223 +
1224 +    /**
1225 +     * quietlyJoin of a forked task returns when task completes
1226 +     */
1227 +    public void testForkQuietlyJoinSingleton() {
1228 +        RecursiveAction a = new CheckedRecursiveAction() {
1229 +            protected void realCompute() {
1230 +                AsyncFib f = new AsyncFib(8);
1231 +                assertSame(f, f.fork());
1232 +                f.quietlyJoin();
1233 +                assertEquals(21, f.number);
1234 +                checkCompletedNormally(f);
1235 +            }};
1236 +        testInvokeOnPool(singletonPool(), a);
1237 +    }
1238 +
1239 +    /**
1240 +     * helpQuiesce returns when tasks are complete.
1241 +     * getQueuedTaskCount returns 0 when quiescent
1242 +     */
1243 +    public void testForkHelpQuiesceSingleton() {
1244 +        RecursiveAction a = new CheckedRecursiveAction() {
1245 +            protected void realCompute() {
1246 +                AsyncFib f = new AsyncFib(8);
1247 +                assertSame(f, f.fork());
1248 +                helpQuiesce();
1249 +                assertEquals(0, getQueuedTaskCount());
1250 +                assertEquals(21, f.number);
1251 +                checkCompletedNormally(f);
1252 +            }};
1253 +        testInvokeOnPool(singletonPool(), a);
1254 +    }
1255 +
1256 +    /**
1257 +     * invoke task throws exception when task completes abnormally
1258 +     */
1259 +    public void testAbnormalInvokeSingleton() {
1260 +        RecursiveAction a = new CheckedRecursiveAction() {
1261 +            protected void realCompute() {
1262 +                FailingAsyncFib f = new FailingAsyncFib(8);
1263 +                try {
1264 +                    f.invoke();
1265 +                    shouldThrow();
1266 +                } catch (FJException success) {
1267 +                    checkCompletedAbnormally(f, success);
1268 +                }
1269 +            }};
1270 +        testInvokeOnPool(singletonPool(), a);
1271 +    }
1272 +
1273 +    /**
1274 +     * quietlyInvoke task returns when task completes abnormally
1275 +     */
1276 +    public void testAbnormalQuietlyInvokeSingleton() {
1277 +        RecursiveAction a = new CheckedRecursiveAction() {
1278 +            protected void realCompute() {
1279 +                FailingAsyncFib f = new FailingAsyncFib(8);
1280 +                f.quietlyInvoke();
1281 +                assertTrue(f.getException() instanceof FJException);
1282 +                checkCompletedAbnormally(f, f.getException());
1283 +            }};
1284 +        testInvokeOnPool(singletonPool(), a);
1285 +    }
1286 +
1287 +    /**
1288 +     * join of a forked task throws exception when task completes abnormally
1289 +     */
1290 +    public void testAbnormalForkJoinSingleton() {
1291 +        RecursiveAction a = new CheckedRecursiveAction() {
1292 +            protected void realCompute() {
1293 +                FailingAsyncFib f = new FailingAsyncFib(8);
1294 +                assertSame(f, f.fork());
1295 +                try {
1296 +                    f.join();
1297 +                    shouldThrow();
1298 +                } catch (FJException success) {
1299 +                    checkCompletedAbnormally(f, success);
1300 +                }
1301 +            }};
1302 +        testInvokeOnPool(singletonPool(), a);
1303 +    }
1304 +
1305 +    /**
1306 +     * get of a forked task throws exception when task completes abnormally
1307 +     */
1308 +    public void testAbnormalForkGetSingleton() {
1309 +        RecursiveAction a = new CheckedRecursiveAction() {
1310 +            protected void realCompute() throws Exception {
1311 +                FailingAsyncFib f = new FailingAsyncFib(8);
1312 +                assertSame(f, f.fork());
1313 +                try {
1314 +                    f.get();
1315 +                    shouldThrow();
1316 +                } catch (ExecutionException success) {
1317 +                    Throwable cause = success.getCause();
1318 +                    assertTrue(cause instanceof FJException);
1319 +                    checkCompletedAbnormally(f, cause);
1320 +                }
1321 +            }};
1322 +        testInvokeOnPool(singletonPool(), a);
1323 +    }
1324 +
1325 +    /**
1326 +     * timed get of a forked task throws exception when task completes abnormally
1327 +     */
1328 +    public void testAbnormalForkTimedGetSingleton() {
1329 +        RecursiveAction a = new CheckedRecursiveAction() {
1330 +            protected void realCompute() throws Exception {
1331 +                FailingAsyncFib f = new FailingAsyncFib(8);
1332 +                assertSame(f, f.fork());
1333 +                try {
1334 +                    f.get(LONG_DELAY_MS, MILLISECONDS);
1335 +                    shouldThrow();
1336 +                } catch (ExecutionException success) {
1337 +                    Throwable cause = success.getCause();
1338 +                    assertTrue(cause instanceof FJException);
1339 +                    checkCompletedAbnormally(f, cause);
1340 +                }
1341 +            }};
1342 +        testInvokeOnPool(singletonPool(), a);
1343 +    }
1344 +
1345 +    /**
1346 +     * quietlyJoin of a forked task returns when task completes abnormally
1347 +     */
1348 +    public void testAbnormalForkQuietlyJoinSingleton() {
1349 +        RecursiveAction a = new CheckedRecursiveAction() {
1350 +            protected void realCompute() {
1351 +                FailingAsyncFib f = new FailingAsyncFib(8);
1352 +                assertSame(f, f.fork());
1353 +                f.quietlyJoin();
1354 +                assertTrue(f.getException() instanceof FJException);
1355 +                checkCompletedAbnormally(f, f.getException());
1356 +            }};
1357 +        testInvokeOnPool(singletonPool(), a);
1358 +    }
1359 +
1360 +    /**
1361 +     * invoke task throws exception when task cancelled
1362 +     */
1363 +    public void testCancelledInvokeSingleton() {
1364 +        RecursiveAction a = new CheckedRecursiveAction() {
1365 +            protected void realCompute() {
1366 +                AsyncFib f = new AsyncFib(8);
1367 +                assertTrue(f.cancel(true));
1368 +                try {
1369 +                    f.invoke();
1370 +                    shouldThrow();
1371 +                } catch (CancellationException success) {
1372 +                    checkCancelled(f);
1373 +                }
1374 +            }};
1375 +        testInvokeOnPool(singletonPool(), a);
1376 +    }
1377 +
1378 +    /**
1379 +     * join of a forked task throws exception when task cancelled
1380 +     */
1381 +    public void testCancelledForkJoinSingleton() {
1382 +        RecursiveAction a = new CheckedRecursiveAction() {
1383 +            protected void realCompute() {
1384 +                AsyncFib f = new AsyncFib(8);
1385 +                assertTrue(f.cancel(true));
1386 +                assertSame(f, f.fork());
1387 +                try {
1388 +                    f.join();
1389 +                    shouldThrow();
1390 +                } catch (CancellationException success) {
1391 +                    checkCancelled(f);
1392 +                }
1393 +            }};
1394 +        testInvokeOnPool(singletonPool(), a);
1395 +    }
1396 +
1397 +    /**
1398 +     * get of a forked task throws exception when task cancelled
1399 +     */
1400 +    public void testCancelledForkGetSingleton() {
1401 +        RecursiveAction a = new CheckedRecursiveAction() {
1402 +            protected void realCompute() throws Exception {
1403 +                AsyncFib f = new AsyncFib(8);
1404 +                assertTrue(f.cancel(true));
1405 +                assertSame(f, f.fork());
1406 +                try {
1407 +                    f.get();
1408 +                    shouldThrow();
1409 +                } catch (CancellationException success) {
1410 +                    checkCancelled(f);
1411 +                }
1412 +            }};
1413 +        testInvokeOnPool(singletonPool(), a);
1414 +    }
1415 +
1416 +    /**
1417 +     * timed get of a forked task throws exception when task cancelled
1418 +     */
1419 +    public void testCancelledForkTimedGetSingleton() throws Exception {
1420 +        RecursiveAction a = new CheckedRecursiveAction() {
1421 +            protected void realCompute() throws Exception {
1422 +                AsyncFib f = new AsyncFib(8);
1423 +                assertTrue(f.cancel(true));
1424 +                assertSame(f, f.fork());
1425 +                try {
1426 +                    f.get(LONG_DELAY_MS, MILLISECONDS);
1427 +                    shouldThrow();
1428 +                } catch (CancellationException success) {
1429 +                    checkCancelled(f);
1430 +                }
1431 +            }};
1432 +        testInvokeOnPool(singletonPool(), a);
1433 +    }
1434 +
1435 +    /**
1436 +     * quietlyJoin of a forked task returns when task cancelled
1437 +     */
1438 +    public void testCancelledForkQuietlyJoinSingleton() {
1439 +        RecursiveAction a = new CheckedRecursiveAction() {
1440 +            protected void realCompute() {
1441 +                AsyncFib f = new AsyncFib(8);
1442 +                assertTrue(f.cancel(true));
1443 +                assertSame(f, f.fork());
1444 +                f.quietlyJoin();
1445 +                checkCancelled(f);
1446 +            }};
1447 +        testInvokeOnPool(singletonPool(), a);
1448 +    }
1449 +
1450 +    /**
1451 +     * invoke task throws exception after invoking completeExceptionally
1452 +     */
1453 +    public void testCompleteExceptionallySingleton() {
1454 +        RecursiveAction a = new CheckedRecursiveAction() {
1455 +            protected void realCompute() {
1456 +                AsyncFib f = new AsyncFib(8);
1457 +                f.completeExceptionally(new FJException());
1458 +                try {
1459 +                    f.invoke();
1460 +                    shouldThrow();
1461 +                } catch (FJException success) {
1462 +                    checkCompletedAbnormally(f, success);
1463 +                }
1464 +            }};
1465 +        testInvokeOnPool(singletonPool(), a);
1466 +    }
1467 +
1468 +    /**
1469 +     * invokeAll(t1, t2) invokes all task arguments
1470 +     */
1471 +    public void testInvokeAll2Singleton() {
1472 +        RecursiveAction a = new CheckedRecursiveAction() {
1473 +            protected void realCompute() {
1474 +                AsyncFib f = new AsyncFib(8);
1475 +                AsyncFib g = new AsyncFib(9);
1476 +                invokeAll(f, g);
1477 +                assertEquals(21, f.number);
1478 +                assertEquals(34, g.number);
1479 +                checkCompletedNormally(f);
1480 +                checkCompletedNormally(g);
1481 +            }};
1482 +        testInvokeOnPool(singletonPool(), a);
1483 +    }
1484 +
1485 +    /**
1486 +     * invokeAll(tasks) with 1 argument invokes task
1487 +     */
1488 +    public void testInvokeAll1Singleton() {
1489 +        RecursiveAction a = new CheckedRecursiveAction() {
1490 +            protected void realCompute() {
1491 +                AsyncFib f = new AsyncFib(8);
1492 +                invokeAll(f);
1493 +                checkCompletedNormally(f);
1494 +                assertEquals(21, f.number);
1495 +            }};
1496 +        testInvokeOnPool(singletonPool(), a);
1497 +    }
1498 +
1499 +    /**
1500 +     * invokeAll(tasks) with > 2 argument invokes tasks
1501 +     */
1502 +    public void testInvokeAll3Singleton() {
1503 +        RecursiveAction a = new CheckedRecursiveAction() {
1504 +            protected void realCompute() {
1505 +                AsyncFib f = new AsyncFib(8);
1506 +                AsyncFib g = new AsyncFib(9);
1507 +                AsyncFib h = new AsyncFib(7);
1508 +                invokeAll(f, g, h);
1509 +                assertEquals(21, f.number);
1510 +                assertEquals(34, g.number);
1511 +                assertEquals(13, h.number);
1512 +                checkCompletedNormally(f);
1513 +                checkCompletedNormally(g);
1514 +                checkCompletedNormally(h);
1515 +            }};
1516 +        testInvokeOnPool(singletonPool(), a);
1517 +    }
1518 +
1519 +    /**
1520 +     * invokeAll(collection) invokes all tasks in the collection
1521 +     */
1522 +    public void testInvokeAllCollectionSingleton() {
1523 +        RecursiveAction a = new CheckedRecursiveAction() {
1524 +            protected void realCompute() {
1525 +                AsyncFib f = new AsyncFib(8);
1526 +                AsyncFib g = new AsyncFib(9);
1527 +                AsyncFib h = new AsyncFib(7);
1528 +                HashSet set = new HashSet();
1529 +                set.add(f);
1530 +                set.add(g);
1531 +                set.add(h);
1532 +                invokeAll(set);
1533 +                assertEquals(21, f.number);
1534 +                assertEquals(34, g.number);
1535 +                assertEquals(13, h.number);
1536 +                checkCompletedNormally(f);
1537 +                checkCompletedNormally(g);
1538 +                checkCompletedNormally(h);
1539 +            }};
1540 +        testInvokeOnPool(singletonPool(), a);
1541 +    }
1542 +
1543 +    /**
1544 +     * invokeAll(tasks) with any null task throws NPE
1545 +     */
1546 +    public void testInvokeAllNPESingleton() {
1547 +        RecursiveAction a = new CheckedRecursiveAction() {
1548 +            protected void realCompute() {
1549 +                AsyncFib f = new AsyncFib(8);
1550 +                AsyncFib g = new AsyncFib(9);
1551 +                AsyncFib h = null;
1552 +                try {
1553 +                    invokeAll(f, g, h);
1554 +                    shouldThrow();
1555 +                } catch (NullPointerException success) {}
1556 +            }};
1557 +        testInvokeOnPool(singletonPool(), a);
1558 +    }
1559 +
1560 +    /**
1561 +     * invokeAll(t1, t2) throw exception if any task does
1562 +     */
1563 +    public void testAbnormalInvokeAll2Singleton() {
1564 +        RecursiveAction a = new CheckedRecursiveAction() {
1565 +            protected void realCompute() {
1566 +                AsyncFib f = new AsyncFib(8);
1567 +                FailingAsyncFib g = new FailingAsyncFib(9);
1568 +                ForkJoinTask[] tasks = { f, g };
1569 +                shuffle(tasks);
1570 +                try {
1571 +                    invokeAll(tasks);
1572 +                    shouldThrow();
1573 +                } catch (FJException success) {
1574 +                    checkCompletedAbnormally(g, success);
1575 +                }
1576 +            }};
1577 +        testInvokeOnPool(singletonPool(), a);
1578 +    }
1579 +
1580 +    /**
1581 +     * invokeAll(tasks) with 1 argument throws exception if task does
1582 +     */
1583 +    public void testAbnormalInvokeAll1Singleton() {
1584 +        RecursiveAction a = new CheckedRecursiveAction() {
1585 +            protected void realCompute() {
1586 +                FailingAsyncFib g = new FailingAsyncFib(9);
1587 +                try {
1588 +                    invokeAll(g);
1589 +                    shouldThrow();
1590 +                } catch (FJException success) {
1591 +                    checkCompletedAbnormally(g, success);
1592 +                }
1593 +            }};
1594 +        testInvokeOnPool(singletonPool(), a);
1595 +    }
1596 +
1597 +    /**
1598 +     * invokeAll(tasks) with > 2 argument throws exception if any task does
1599 +     */
1600 +    public void testAbnormalInvokeAll3Singleton() {
1601 +        RecursiveAction a = new CheckedRecursiveAction() {
1602 +            protected void realCompute() {
1603 +                AsyncFib f = new AsyncFib(8);
1604 +                FailingAsyncFib g = new FailingAsyncFib(9);
1605 +                AsyncFib h = new AsyncFib(7);
1606 +                ForkJoinTask[] tasks = { f, g, h };
1607 +                shuffle(tasks);
1608 +                try {
1609 +                    invokeAll(tasks);
1610 +                    shouldThrow();
1611 +                } catch (FJException success) {
1612 +                    checkCompletedAbnormally(g, success);
1613 +                }
1614 +            }};
1615 +        testInvokeOnPool(singletonPool(), a);
1616 +    }
1617 +
1618 +    /**
1619 +     * invokeAll(collection) throws exception if any task does
1620 +     */
1621 +    public void testAbnormalInvokeAllCollectionSingleton() {
1622 +        RecursiveAction a = new CheckedRecursiveAction() {
1623 +            protected void realCompute() {
1624 +                FailingAsyncFib f = new FailingAsyncFib(8);
1625 +                AsyncFib g = new AsyncFib(9);
1626 +                AsyncFib h = new AsyncFib(7);
1627 +                ForkJoinTask[] tasks = { f, g, h };
1628 +                shuffle(tasks);
1629 +                try {
1630 +                    invokeAll(Arrays.asList(tasks));
1631 +                    shouldThrow();
1632 +                } catch (FJException success) {
1633 +                    checkCompletedAbnormally(f, success);
1634 +                }
1635 +            }};
1636 +        testInvokeOnPool(singletonPool(), a);
1637 +    }
1638 +
1639 +    /**
1640 +     * ForkJoinTask.quietlyComplete returns when task completes
1641 +     * normally without setting a value. The most recent value
1642 +     * established by setRawResult(V) (or null by default) is returned
1643 +     * from invoke.
1644 +     */
1645 +    public void testQuietlyComplete() {
1646 +        RecursiveAction a = new CheckedRecursiveAction() {
1647 +                protected void realCompute() {
1648 +                    AsyncFib f = new AsyncFib(8);
1649 +                    f.quietlyComplete();
1650 +                    assertEquals(8, f.number);
1651 +                    checkCompletedNormally(f);
1652 +                }};
1653 +        testInvokeOnPool(mainPool(), a);
1654 +    }
1655 +
1656   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines