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

Comparing jsr166/src/test/tck/RecursiveActionTest.java (file contents):
Revision 1.10 by dl, Wed Aug 11 19:50:02 2010 UTC vs.
Revision 1.21 by jsr166, Sun Nov 21 19:06:53 2010 UTC

# Line 3 | Line 3
3   * Expert Group and released to the public domain, as explained at
4   * http://creativecommons.org/licenses/publicdomain
5   */
6 import junit.framework.*;
7 import java.util.concurrent.*;
8 import java.util.*;
6  
7 + import junit.framework.*;
8 + import java.util.concurrent.CancellationException;
9 + import java.util.concurrent.ExecutionException;
10 + import java.util.concurrent.ForkJoinPool;
11 + import java.util.concurrent.ForkJoinWorkerThread;
12 + import java.util.concurrent.RecursiveAction;
13 + import java.util.concurrent.TimeUnit;
14 + import java.util.concurrent.TimeoutException;
15 + import static java.util.concurrent.TimeUnit.SECONDS;
16 + import java.util.HashSet;
17  
18   public class RecursiveActionTest extends JSR166TestCase {
19  
20      public static void main(String[] args) {
21 <        junit.textui.TestRunner.run (suite());
21 >        junit.textui.TestRunner.run(suite());
22      }
23 +
24      public static Test suite() {
25          return new TestSuite(RecursiveActionTest.class);
26      }
27  
28 <    static final ForkJoinPool mainPool = new ForkJoinPool();
29 <    static final ForkJoinPool singletonPool = new ForkJoinPool(1);
30 <    static final ForkJoinPool asyncSingletonPool =
31 <        new ForkJoinPool(1, ForkJoinPool.defaultForkJoinWorkerThreadFactory,
32 <                         null, true);
28 >    private static ForkJoinPool mainPool() {
29 >        return new ForkJoinPool();
30 >    }
31 >
32 >    private static ForkJoinPool singletonPool() {
33 >        return new ForkJoinPool(1);
34 >    }
35 >
36 >    private static ForkJoinPool asyncSingletonPool() {
37 >        return new ForkJoinPool(1,
38 >                                ForkJoinPool.defaultForkJoinWorkerThreadFactory,
39 >                                null, true);
40 >    }
41 >
42 >    private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
43 >        try {
44 >            checkNotDone(a);
45 >
46 >            assertNull(pool.invoke(a));
47 >
48 >            checkCompletedNormally(a);
49 >        } finally {
50 >            joinPool(pool);
51 >        }
52 >    }
53 >
54 >    void checkNotDone(RecursiveAction a) {
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 >        if (! (Thread.currentThread() instanceof ForkJoinWorkerThread)) {
63 >            Thread.currentThread().interrupt();
64 >            try {
65 >                a.get();
66 >                shouldThrow();
67 >            } catch (InterruptedException success) {
68 >            } catch (Throwable fail) { threadUnexpectedException(fail); }
69 >
70 >            Thread.currentThread().interrupt();
71 >            try {
72 >                a.get(5L, SECONDS);
73 >                shouldThrow();
74 >            } catch (InterruptedException success) {
75 >            } catch (Throwable fail) { threadUnexpectedException(fail); }
76 >        }
77 >
78 >        try {
79 >            a.get(0L, SECONDS);
80 >            shouldThrow();
81 >        } catch (TimeoutException success) {
82 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
83 >    }
84 >
85 >    void checkCompletedNormally(RecursiveAction a) {
86 >        assertTrue(a.isDone());
87 >        assertFalse(a.isCancelled());
88 >        assertTrue(a.isCompletedNormally());
89 >        assertFalse(a.isCompletedAbnormally());
90 >        assertNull(a.getException());
91 >        assertNull(a.getRawResult());
92 >        assertNull(a.join());
93 >        try {
94 >            assertNull(a.get());
95 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
96 >        try {
97 >            assertNull(a.get(5L, SECONDS));
98 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
99 >    }
100 >
101 >    void checkCancelled(RecursiveAction a) {
102 >        assertTrue(a.isDone());
103 >        assertTrue(a.isCancelled());
104 >        assertFalse(a.isCompletedNormally());
105 >        assertTrue(a.isCompletedAbnormally());
106 >        assertTrue(a.getException() instanceof CancellationException);
107 >        assertNull(a.getRawResult());
108 >
109 >        try {
110 >            a.join();
111 >            shouldThrow();
112 >        } catch (CancellationException success) {
113 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
114 >
115 >        try {
116 >            a.get();
117 >            shouldThrow();
118 >        } catch (CancellationException success) {
119 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
120 >
121 >        try {
122 >            a.get(5L, SECONDS);
123 >            shouldThrow();
124 >        } catch (CancellationException success) {
125 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
126 >    }
127 >
128 >    void checkCompletedAbnormally(RecursiveAction a, Throwable t) {
129 >        assertTrue(a.isDone());
130 >        assertFalse(a.isCancelled());
131 >        assertFalse(a.isCompletedNormally());
132 >        assertTrue(a.isCompletedAbnormally());
133 >        assertSame(t, a.getException());
134 >        assertNull(a.getRawResult());
135 >
136 >        try {
137 >            a.join();
138 >            shouldThrow();
139 >        } catch (Throwable expected) {
140 >            assertSame(t, expected);
141 >        }
142 >
143 >        try {
144 >            a.get();
145 >            shouldThrow();
146 >        } catch (ExecutionException success) {
147 >            assertSame(t, success.getCause());
148 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
149 >
150 >        try {
151 >            a.get(5L, SECONDS);
152 >            shouldThrow();
153 >        } catch (ExecutionException success) {
154 >            assertSame(t, success.getCause());
155 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
156 >    }
157  
158      static final class FJException extends RuntimeException {
159          FJException() { super(); }
160      }
161  
162      // A simple recursive action for testing
163 <    static final class FibAction extends RecursiveAction {
163 >    final class FibAction extends CheckedRecursiveAction {
164          final int number;
165          int result;
166          FibAction(int n) { number = n; }
167 <        public void compute() {
167 >        public void realCompute() {
168              int n = number;
169              if (n <= 1)
170                  result = n;
# Line 67 | Line 199 | public class RecursiveActionTest extends
199       * invoke returns when task completes normally.
200       * isCompletedAbnormally and isCancelled return false for normally
201       * completed tasks. getRawResult of a RecursiveAction returns null;
70     *
202       */
203      public void testInvoke() {
204 <        RecursiveAction a = new RecursiveAction() {
205 <            public void compute() {
204 >        RecursiveAction a = new CheckedRecursiveAction() {
205 >            public void realCompute() {
206                  FibAction f = new FibAction(8);
207 <                f.invoke();
208 <                threadAssertTrue(f.result == 21);
209 <                threadAssertTrue(f.isDone());
79 <                threadAssertFalse(f.isCancelled());
80 <                threadAssertFalse(f.isCompletedAbnormally());
81 <                threadAssertTrue(f.getRawResult() == null);
207 >                assertNull(f.invoke());
208 >                assertEquals(21, f.result);
209 >                checkCompletedNormally(f);
210              }};
211 <        mainPool.invoke(a);
211 >        testInvokeOnPool(mainPool(), a);
212      }
213  
214      /**
# Line 89 | Line 217 | public class RecursiveActionTest extends
217       * completed tasks
218       */
219      public void testQuietlyInvoke() {
220 <        RecursiveAction a = new RecursiveAction() {
221 <            public void compute() {
220 >        RecursiveAction a = new CheckedRecursiveAction() {
221 >            public void realCompute() {
222                  FibAction f = new FibAction(8);
223                  f.quietlyInvoke();
224 <                threadAssertTrue(f.result == 21);
225 <                threadAssertTrue(f.isDone());
98 <                threadAssertFalse(f.isCancelled());
99 <                threadAssertFalse(f.isCompletedAbnormally());
100 <                threadAssertTrue(f.getRawResult() == null);
224 >                assertEquals(21, f.result);
225 >                checkCompletedNormally(f);
226              }};
227 <        mainPool.invoke(a);
227 >        testInvokeOnPool(mainPool(), a);
228      }
229  
230      /**
231       * join of a forked task returns when task completes
232       */
233      public void testForkJoin() {
234 <        RecursiveAction a = new RecursiveAction() {
235 <            public void compute() {
234 >        RecursiveAction a = new CheckedRecursiveAction() {
235 >            public void realCompute() {
236                  FibAction f = new FibAction(8);
237 <                f.fork();
238 <                f.join();
239 <                threadAssertTrue(f.result == 21);
240 <                threadAssertTrue(f.isDone());
116 <                threadAssertTrue(f.getRawResult() == null);
237 >                assertSame(f, f.fork());
238 >                assertNull(f.join());
239 >                assertEquals(21, f.result);
240 >                checkCompletedNormally(f);
241              }};
242 <        mainPool.invoke(a);
242 >        testInvokeOnPool(mainPool(), a);
243      }
244  
245      /**
246       * get of a forked task returns when task completes
247       */
248      public void testForkGet() {
249 <        RecursiveAction a = new RecursiveAction() {
250 <            public void compute() {
251 <                try {
252 <                    FibAction f = new FibAction(8);
253 <                    f.fork();
254 <                    f.get();
255 <                    threadAssertTrue(f.result == 21);
132 <                    threadAssertTrue(f.isDone());
133 <                } catch (Exception ex) {
134 <                    unexpectedException(ex);
135 <                }
249 >        RecursiveAction a = new CheckedRecursiveAction() {
250 >            public void realCompute() throws Exception {
251 >                FibAction f = new FibAction(8);
252 >                assertSame(f, f.fork());
253 >                assertNull(f.get());
254 >                assertEquals(21, f.result);
255 >                checkCompletedNormally(f);
256              }};
257 <        mainPool.invoke(a);
257 >        testInvokeOnPool(mainPool(), a);
258      }
259  
260      /**
261       * timed get of a forked task returns when task completes
262       */
263      public void testForkTimedGet() {
264 <        RecursiveAction a = new RecursiveAction() {
265 <            public void compute() {
266 <                try {
267 <                    FibAction f = new FibAction(8);
268 <                    f.fork();
269 <                    f.get(5L, TimeUnit.SECONDS);
270 <                    threadAssertTrue(f.result == 21);
151 <                    threadAssertTrue(f.isDone());
152 <                } catch (Exception ex) {
153 <                    unexpectedException(ex);
154 <                }
264 >        RecursiveAction a = new CheckedRecursiveAction() {
265 >            public void realCompute() throws Exception {
266 >                FibAction f = new FibAction(8);
267 >                assertSame(f, f.fork());
268 >                assertNull(f.get(5L, SECONDS));
269 >                assertEquals(21, f.result);
270 >                checkCompletedNormally(f);
271              }};
272 <        mainPool.invoke(a);
272 >        testInvokeOnPool(mainPool(), a);
273      }
274  
275      /**
276       * timed get with null time unit throws NPE
277       */
278      public void testForkTimedGetNPE() {
279 <        RecursiveAction a = new RecursiveAction() {
280 <            public void compute() {
279 >        RecursiveAction a = new CheckedRecursiveAction() {
280 >            public void realCompute() throws Exception {
281 >                FibAction f = new FibAction(8);
282 >                assertSame(f, f.fork());
283                  try {
166                    FibAction f = new FibAction(8);
167                    f.fork();
284                      f.get(5L, null);
285                      shouldThrow();
286 <                } catch (NullPointerException success) {
171 <                } catch (Exception ex) {
172 <                    unexpectedException(ex);
173 <                }
286 >                } catch (NullPointerException success) {}
287              }};
288 <        mainPool.invoke(a);
288 >        testInvokeOnPool(mainPool(), a);
289      }
290  
291      /**
292       * quietlyJoin of a forked task returns when task completes
293       */
294      public void testForkQuietlyJoin() {
295 <        RecursiveAction a = new RecursiveAction() {
296 <            public void compute() {
295 >        RecursiveAction a = new CheckedRecursiveAction() {
296 >            public void realCompute() {
297                  FibAction f = new FibAction(8);
298 <                f.fork();
298 >                assertSame(f, f.fork());
299                  f.quietlyJoin();
300 <                threadAssertTrue(f.result == 21);
301 <                threadAssertTrue(f.isDone());
300 >                assertEquals(21, f.result);
301 >                checkCompletedNormally(f);
302              }};
303 <        mainPool.invoke(a);
303 >        testInvokeOnPool(mainPool(), a);
304      }
305  
306  
# Line 196 | Line 309 | public class RecursiveActionTest extends
309       * getQueuedTaskCount returns 0 when quiescent
310       */
311      public void testForkHelpQuiesce() {
312 <        RecursiveAction a = new RecursiveAction() {
313 <            public void compute() {
312 >        RecursiveAction a = new CheckedRecursiveAction() {
313 >            public void realCompute() {
314                  FibAction f = new FibAction(8);
315 <                f.fork();
315 >                assertSame(f, f.fork());
316                  f.helpQuiesce();
317 <                threadAssertTrue(f.result == 21);
318 <                threadAssertTrue(f.isDone());
319 <                threadAssertTrue(getQueuedTaskCount() == 0);
317 >                assertEquals(21, f.result);
318 >                assertEquals(0, getQueuedTaskCount());
319 >                checkCompletedNormally(f);
320              }};
321 <        mainPool.invoke(a);
321 >        testInvokeOnPool(mainPool(), a);
322      }
323  
324  
# Line 213 | Line 326 | public class RecursiveActionTest extends
326       * invoke task throws exception when task completes abnormally
327       */
328      public void testAbnormalInvoke() {
329 <        RecursiveAction a = new RecursiveAction() {
330 <            public void compute() {
329 >        RecursiveAction a = new CheckedRecursiveAction() {
330 >            public void realCompute() {
331 >                FailingFibAction f = new FailingFibAction(8);
332                  try {
219                    FailingFibAction f = new FailingFibAction(8);
333                      f.invoke();
334                      shouldThrow();
335                  } catch (FJException success) {
336 +                    checkCompletedAbnormally(f, success);
337                  }
338              }};
339 <        mainPool.invoke(a);
339 >        testInvokeOnPool(mainPool(), a);
340      }
341  
342      /**
343       * quietlyInvoke task returns when task completes abnormally
344       */
345      public void testAbnormalQuietlyInvoke() {
346 <        RecursiveAction a = new RecursiveAction() {
347 <            public void compute() {
346 >        RecursiveAction a = new CheckedRecursiveAction() {
347 >            public void realCompute() {
348                  FailingFibAction f = new FailingFibAction(8);
349                  f.quietlyInvoke();
350 <                threadAssertTrue(f.isDone());
350 >                assertTrue(f.getException() instanceof FJException);
351 >                checkCompletedAbnormally(f, f.getException());
352              }};
353 <        mainPool.invoke(a);
353 >        testInvokeOnPool(mainPool(), a);
354      }
355  
356      /**
357       * join of a forked task throws exception when task completes abnormally
358       */
359      public void testAbnormalForkJoin() {
360 <        RecursiveAction a = new RecursiveAction() {
361 <            public void compute() {
360 >        RecursiveAction a = new CheckedRecursiveAction() {
361 >            public void realCompute() {
362 >                FailingFibAction f = new FailingFibAction(8);
363 >                assertSame(f, f.fork());
364                  try {
248                    FailingFibAction f = new FailingFibAction(8);
249                    f.fork();
365                      f.join();
366                      shouldThrow();
367                  } catch (FJException success) {
368 +                    checkCompletedAbnormally(f, success);
369                  }
370              }};
371 <        mainPool.invoke(a);
371 >        testInvokeOnPool(mainPool(), a);
372      }
373  
374      /**
375       * get of a forked task throws exception when task completes abnormally
376       */
377      public void testAbnormalForkGet() {
378 <        RecursiveAction a = new RecursiveAction() {
379 <            public void compute() {
378 >        RecursiveAction a = new CheckedRecursiveAction() {
379 >            public void realCompute() throws Exception {
380 >                FailingFibAction f = new FailingFibAction(8);
381 >                assertSame(f, f.fork());
382                  try {
265                    FailingFibAction f = new FailingFibAction(8);
266                    f.fork();
383                      f.get();
384                      shouldThrow();
385                  } catch (ExecutionException success) {
386 <                } catch (Exception ex) {
387 <                    unexpectedException(ex);
386 >                    Throwable cause = success.getCause();
387 >                    assertTrue(cause instanceof FJException);
388 >                    checkCompletedAbnormally(f, cause);
389                  }
390              }};
391 <        mainPool.invoke(a);
391 >        testInvokeOnPool(mainPool(), a);
392      }
393  
394      /**
395       * timed get of a forked task throws exception when task completes abnormally
396       */
397      public void testAbnormalForkTimedGet() {
398 <        RecursiveAction a = new RecursiveAction() {
399 <            public void compute() {
398 >        RecursiveAction a = new CheckedRecursiveAction() {
399 >            public void realCompute() throws Exception {
400 >                FailingFibAction f = new FailingFibAction(8);
401 >                assertSame(f, f.fork());
402                  try {
284                    FailingFibAction f = new FailingFibAction(8);
285                    f.fork();
403                      f.get(5L, TimeUnit.SECONDS);
404                      shouldThrow();
405                  } catch (ExecutionException success) {
406 <                } catch (Exception ex) {
407 <                    unexpectedException(ex);
406 >                    Throwable cause = success.getCause();
407 >                    assertTrue(cause instanceof FJException);
408 >                    checkCompletedAbnormally(f, cause);
409                  }
410              }};
411 <        mainPool.invoke(a);
411 >        testInvokeOnPool(mainPool(), a);
412      }
413  
414      /**
415       * quietlyJoin of a forked task returns when task completes abnormally
416       */
417      public void testAbnormalForkQuietlyJoin() {
418 <        RecursiveAction a = new RecursiveAction() {
419 <            public void compute() {
418 >        RecursiveAction a = new CheckedRecursiveAction() {
419 >            public void realCompute() {
420                  FailingFibAction f = new FailingFibAction(8);
421 <                f.fork();
421 >                assertSame(f, f.fork());
422                  f.quietlyJoin();
423 <                threadAssertTrue(f.isDone());
424 <                threadAssertTrue(f.isCompletedAbnormally());
307 <                threadAssertTrue(f.getException() instanceof FJException);
423 >                assertTrue(f.getException() instanceof FJException);
424 >                checkCompletedAbnormally(f, f.getException());
425              }};
426 <        mainPool.invoke(a);
426 >        testInvokeOnPool(mainPool(), a);
427      }
428  
429      /**
430       * invoke task throws exception when task cancelled
431       */
432      public void testCancelledInvoke() {
433 <        RecursiveAction a = new RecursiveAction() {
434 <            public void compute() {
433 >        RecursiveAction a = new CheckedRecursiveAction() {
434 >            public void realCompute() {
435 >                FibAction f = new FibAction(8);
436 >                assertTrue(f.cancel(true));
437                  try {
319                    FibAction f = new FibAction(8);
320                    f.cancel(true);
438                      f.invoke();
439                      shouldThrow();
440                  } catch (CancellationException success) {
441 +                    checkCancelled(f);
442                  }
443              }};
444 <        mainPool.invoke(a);
444 >        testInvokeOnPool(mainPool(), a);
445      }
446  
447      /**
448       * join of a forked task throws exception when task cancelled
449       */
450      public void testCancelledForkJoin() {
451 <        RecursiveAction a = new RecursiveAction() {
452 <            public void compute() {
451 >        RecursiveAction a = new CheckedRecursiveAction() {
452 >            public void realCompute() {
453 >                FibAction f = new FibAction(8);
454 >                assertTrue(f.cancel(true));
455 >                assertSame(f, f.fork());
456                  try {
336                    FibAction f = new FibAction(8);
337                    f.cancel(true);
338                    f.fork();
457                      f.join();
458                      shouldThrow();
459                  } catch (CancellationException success) {
460 +                    checkCancelled(f);
461                  }
462              }};
463 <        mainPool.invoke(a);
463 >        testInvokeOnPool(mainPool(), a);
464      }
465  
466      /**
467       * get of a forked task throws exception when task cancelled
468       */
469      public void testCancelledForkGet() {
470 <        RecursiveAction a = new RecursiveAction() {
471 <            public void compute() {
470 >        RecursiveAction a = new CheckedRecursiveAction() {
471 >            public void realCompute() throws Exception {
472 >                FibAction f = new FibAction(8);
473 >                assertTrue(f.cancel(true));
474 >                assertSame(f, f.fork());
475                  try {
354                    FibAction f = new FibAction(8);
355                    f.cancel(true);
356                    f.fork();
476                      f.get();
477                      shouldThrow();
478                  } catch (CancellationException success) {
479 <                } catch (Exception ex) {
361 <                    unexpectedException(ex);
479 >                    checkCancelled(f);
480                  }
481              }};
482 <        mainPool.invoke(a);
482 >        testInvokeOnPool(mainPool(), a);
483      }
484  
485      /**
486       * timed get of a forked task throws exception when task cancelled
487       */
488      public void testCancelledForkTimedGet() {
489 <        RecursiveAction a = new RecursiveAction() {
490 <            public void compute() {
489 >        RecursiveAction a = new CheckedRecursiveAction() {
490 >            public void realCompute() throws Exception {
491 >                FibAction f = new FibAction(8);
492 >                assertTrue(f.cancel(true));
493 >                assertSame(f, f.fork());
494                  try {
495 <                    FibAction f = new FibAction(8);
375 <                    f.cancel(true);
376 <                    f.fork();
377 <                    f.get(5L, TimeUnit.SECONDS);
495 >                    f.get(5L, SECONDS);
496                      shouldThrow();
497                  } catch (CancellationException success) {
498 <                } catch (Exception ex) {
381 <                    unexpectedException(ex);
498 >                    checkCancelled(f);
499                  }
500              }};
501 <        mainPool.invoke(a);
501 >        testInvokeOnPool(mainPool(), a);
502      }
503  
504      /**
505       * quietlyJoin of a forked task returns when task cancelled
506       */
507      public void testCancelledForkQuietlyJoin() {
508 <        RecursiveAction a = new RecursiveAction() {
509 <            public void compute() {
508 >        RecursiveAction a = new CheckedRecursiveAction() {
509 >            public void realCompute() {
510                  FibAction f = new FibAction(8);
511 <                f.cancel(true);
512 <                f.fork();
511 >                assertTrue(f.cancel(true));
512 >                assertSame(f, f.fork());
513                  f.quietlyJoin();
514 <                threadAssertTrue(f.isDone());
398 <                threadAssertTrue(f.isCompletedAbnormally());
399 <                threadAssertTrue(f.getException() instanceof CancellationException);
514 >                checkCancelled(f);
515              }};
516 <        mainPool.invoke(a);
516 >        testInvokeOnPool(mainPool(), a);
517      }
518  
519      /**
520       * getPool of executing task returns its pool
521       */
522      public void testGetPool() {
523 <        RecursiveAction a = new RecursiveAction() {
524 <            public void compute() {
525 <                threadAssertTrue(getPool() == mainPool);
523 >        final ForkJoinPool mainPool = mainPool();
524 >        RecursiveAction a = new CheckedRecursiveAction() {
525 >            public void realCompute() {
526 >                assertSame(mainPool, getPool());
527              }};
528 <        mainPool.invoke(a);
528 >        testInvokeOnPool(mainPool, a);
529      }
530  
531      /**
532       * getPool of non-FJ task returns null
533       */
534      public void testGetPool2() {
535 <        RecursiveAction a = new RecursiveAction() {
536 <            public void compute() {
537 <                threadAssertTrue(getPool() == null);
535 >        RecursiveAction a = new CheckedRecursiveAction() {
536 >            public void realCompute() {
537 >                assertNull(getPool());
538              }};
539 <        a.invoke();
539 >        assertNull(a.invoke());
540      }
541  
542      /**
543       * inForkJoinPool of executing task returns true
544       */
545      public void testInForkJoinPool() {
546 <        RecursiveAction a = new RecursiveAction() {
547 <            public void compute() {
548 <                threadAssertTrue(inForkJoinPool());
546 >        RecursiveAction a = new CheckedRecursiveAction() {
547 >            public void realCompute() {
548 >                assertTrue(inForkJoinPool());
549              }};
550 <        mainPool.invoke(a);
550 >        testInvokeOnPool(mainPool(), a);
551      }
552  
553      /**
554       * inForkJoinPool of non-FJ task returns false
555       */
556      public void testInForkJoinPool2() {
557 <        RecursiveAction a = new RecursiveAction() {
558 <            public void compute() {
559 <                threadAssertTrue(!inForkJoinPool());
557 >        RecursiveAction a = new CheckedRecursiveAction() {
558 >            public void realCompute() {
559 >                assertFalse(inForkJoinPool());
560              }};
561 <        a.invoke();
561 >        assertNull(a.invoke());
562      }
563  
564      /**
565       * getPool of current thread in pool returns its pool
566       */
567      public void testWorkerGetPool() {
568 <        RecursiveAction a = new RecursiveAction() {
569 <            public void compute() {
568 >        final ForkJoinPool mainPool = mainPool();
569 >        RecursiveAction a = new CheckedRecursiveAction() {
570 >            public void realCompute() {
571                  ForkJoinWorkerThread w =
572 <                    (ForkJoinWorkerThread)(Thread.currentThread());
573 <                threadAssertTrue(w.getPool() == mainPool);
572 >                    (ForkJoinWorkerThread) Thread.currentThread();
573 >                assertSame(mainPool, w.getPool());
574              }};
575 <        mainPool.invoke(a);
575 >        testInvokeOnPool(mainPool, a);
576      }
577  
578      /**
579       * getPoolIndex of current thread in pool returns 0 <= value < poolSize
463     *
580       */
581      public void testWorkerGetPoolIndex() {
582 <        RecursiveAction a = new RecursiveAction() {
583 <            public void compute() {
582 >        final ForkJoinPool mainPool = mainPool();
583 >        RecursiveAction a = new CheckedRecursiveAction() {
584 >            public void realCompute() {
585                  ForkJoinWorkerThread w =
586                      (ForkJoinWorkerThread)(Thread.currentThread());
587 <                int idx = w.getPoolIndex();
588 <                threadAssertTrue(idx >= 0);
472 <                threadAssertTrue(idx < mainPool.getPoolSize());
587 >                assertTrue(w.getPoolIndex() >= 0);
588 >                assertTrue(w.getPoolIndex() < mainPool.getPoolSize());
589              }};
590 <        mainPool.invoke(a);
590 >        testInvokeOnPool(mainPool, a);
591      }
592  
593  
# Line 479 | Line 595 | public class RecursiveActionTest extends
595       * setRawResult(null) succeeds
596       */
597      public void testSetRawResult() {
598 <        RecursiveAction a = new RecursiveAction() {
599 <            public void compute() {
598 >        RecursiveAction a = new CheckedRecursiveAction() {
599 >            public void realCompute() {
600                  setRawResult(null);
601              }};
602 <        a.invoke();
602 >        assertNull(a.invoke());
603      }
604  
605      /**
606       * A reinitialized task may be re-invoked
607       */
608      public void testReinitialize() {
609 <        RecursiveAction a = new RecursiveAction() {
610 <            public void compute() {
609 >        RecursiveAction a = new CheckedRecursiveAction() {
610 >            public void realCompute() {
611                  FibAction f = new FibAction(8);
612 <                f.invoke();
613 <                threadAssertTrue(f.result == 21);
614 <                threadAssertTrue(f.isDone());
615 <                threadAssertFalse(f.isCancelled());
616 <                threadAssertFalse(f.isCompletedAbnormally());
617 <                f.reinitialize();
618 <                f.invoke();
619 <                threadAssertTrue(f.result == 21);
612 >                checkNotDone(f);
613 >
614 >                for (int i = 0; i < 3; i++) {
615 >                    assertNull(f.invoke());
616 >                    assertEquals(21, f.result);
617 >                    checkCompletedNormally(f);
618 >                    f.reinitialize();
619 >                    checkNotDone(f);
620 >                }
621              }};
622 <        mainPool.invoke(a);
622 >        testInvokeOnPool(mainPool(), a);
623      }
624  
625      /**
626       * invoke task throws exception after invoking completeExceptionally
627       */
628      public void testCompleteExceptionally() {
629 <        RecursiveAction a = new RecursiveAction() {
630 <            public void compute() {
629 >        RecursiveAction a = new CheckedRecursiveAction() {
630 >            public void realCompute() {
631 >                FibAction f = new FibAction(8);
632 >                f.completeExceptionally(new FJException());
633                  try {
515                    FibAction f = new FibAction(8);
516                    f.completeExceptionally(new FJException());
634                      f.invoke();
635                      shouldThrow();
636                  } catch (FJException success) {
637 +                    checkCompletedAbnormally(f, success);
638                  }
639              }};
640 <        mainPool.invoke(a);
640 >        testInvokeOnPool(mainPool(), a);
641      }
642  
643      /**
644       * invoke task suppresses execution invoking complete
645       */
646      public void testComplete() {
647 <        RecursiveAction a = new RecursiveAction() {
648 <            public void compute() {
647 >        RecursiveAction a = new CheckedRecursiveAction() {
648 >            public void realCompute() {
649                  FibAction f = new FibAction(8);
650                  f.complete(null);
651 <                f.invoke();
652 <                threadAssertTrue(f.isDone());
653 <                threadAssertTrue(f.result == 0);
651 >                assertNull(f.invoke());
652 >                assertEquals(0, f.result);
653 >                checkCompletedNormally(f);
654              }};
655 <        mainPool.invoke(a);
655 >        testInvokeOnPool(mainPool(), a);
656      }
657  
658      /**
659       * invokeAll(t1, t2) invokes all task arguments
660       */
661      public void testInvokeAll2() {
662 <        RecursiveAction a = new RecursiveAction() {
663 <            public void compute() {
662 >        RecursiveAction a = new CheckedRecursiveAction() {
663 >            public void realCompute() {
664                  FibAction f = new FibAction(8);
665                  FibAction g = new FibAction(9);
666                  invokeAll(f, g);
667 <                threadAssertTrue(f.isDone());
668 <                threadAssertTrue(f.result == 21);
669 <                threadAssertTrue(g.isDone());
670 <                threadAssertTrue(g.result == 34);
667 >                checkCompletedNormally(f);
668 >                assertEquals(21, f.result);
669 >                checkCompletedNormally(g);
670 >                assertEquals(34, g.result);
671              }};
672 <        mainPool.invoke(a);
672 >        testInvokeOnPool(mainPool(), a);
673      }
674  
675      /**
676       * invokeAll(tasks) with 1 argument invokes task
677       */
678      public void testInvokeAll1() {
679 <        RecursiveAction a = new RecursiveAction() {
680 <            public void compute() {
679 >        RecursiveAction a = new CheckedRecursiveAction() {
680 >            public void realCompute() {
681                  FibAction f = new FibAction(8);
682                  invokeAll(f);
683 <                threadAssertTrue(f.isDone());
684 <                threadAssertTrue(f.result == 21);
683 >                checkCompletedNormally(f);
684 >                assertEquals(21, f.result);
685              }};
686 <        mainPool.invoke(a);
686 >        testInvokeOnPool(mainPool(), a);
687      }
688  
689      /**
690       * invokeAll(tasks) with > 2 argument invokes tasks
691       */
692      public void testInvokeAll3() {
693 <        RecursiveAction a = new RecursiveAction() {
694 <            public void compute() {
693 >        RecursiveAction a = new CheckedRecursiveAction() {
694 >            public void realCompute() {
695                  FibAction f = new FibAction(8);
696                  FibAction g = new FibAction(9);
697                  FibAction h = new FibAction(7);
698                  invokeAll(f, g, h);
699 <                threadAssertTrue(f.isDone());
700 <                threadAssertTrue(f.result == 21);
701 <                threadAssertTrue(g.isDone());
702 <                threadAssertTrue(g.result == 34);
703 <                threadAssertTrue(h.isDone());
704 <                threadAssertTrue(h.result == 13);
699 >                assertTrue(f.isDone());
700 >                assertTrue(g.isDone());
701 >                assertTrue(h.isDone());
702 >                checkCompletedNormally(f);
703 >                assertEquals(21, f.result);
704 >                checkCompletedNormally(g);
705 >                assertEquals(34, g.result);
706 >                checkCompletedNormally(g);
707 >                assertEquals(13, h.result);
708              }};
709 <        mainPool.invoke(a);
709 >        testInvokeOnPool(mainPool(), a);
710      }
711  
712      /**
713       * invokeAll(collection) invokes all tasks in the collection
714       */
715      public void testInvokeAllCollection() {
716 <        RecursiveAction a = new RecursiveAction() {
717 <            public void compute() {
716 >        RecursiveAction a = new CheckedRecursiveAction() {
717 >            public void realCompute() {
718                  FibAction f = new FibAction(8);
719                  FibAction g = new FibAction(9);
720                  FibAction h = new FibAction(7);
# Line 602 | Line 723 | public class RecursiveActionTest extends
723                  set.add(g);
724                  set.add(h);
725                  invokeAll(set);
726 <                threadAssertTrue(f.isDone());
727 <                threadAssertTrue(f.result == 21);
728 <                threadAssertTrue(g.isDone());
729 <                threadAssertTrue(g.result == 34);
730 <                threadAssertTrue(h.isDone());
731 <                threadAssertTrue(h.result == 13);
726 >                assertTrue(f.isDone());
727 >                assertTrue(g.isDone());
728 >                assertTrue(h.isDone());
729 >                checkCompletedNormally(f);
730 >                assertEquals(21, f.result);
731 >                checkCompletedNormally(g);
732 >                assertEquals(34, g.result);
733 >                checkCompletedNormally(g);
734 >                assertEquals(13, h.result);
735              }};
736 <        mainPool.invoke(a);
736 >        testInvokeOnPool(mainPool(), a);
737      }
738  
739  
# Line 617 | Line 741 | public class RecursiveActionTest extends
741       * invokeAll(tasks) with any null task throws NPE
742       */
743      public void testInvokeAllNPE() {
744 <        RecursiveAction a = new RecursiveAction() {
745 <            public void compute() {
744 >        RecursiveAction a = new CheckedRecursiveAction() {
745 >            public void realCompute() {
746 >                FibAction f = new FibAction(8);
747 >                FibAction g = new FibAction(9);
748 >                FibAction h = null;
749                  try {
623                    FibAction f = new FibAction(8);
624                    FibAction g = new FibAction(9);
625                    FibAction h = null;
750                      invokeAll(f, g, h);
751                      shouldThrow();
752 <                } catch (NullPointerException success) {
629 <                }
752 >                } catch (NullPointerException success) {}
753              }};
754 <        mainPool.invoke(a);
754 >        testInvokeOnPool(mainPool(), a);
755      }
756  
757      /**
758       * invokeAll(t1, t2) throw exception if any task does
759       */
760      public void testAbnormalInvokeAll2() {
761 <        RecursiveAction a = new RecursiveAction() {
762 <            public void compute() {
761 >        RecursiveAction a = new CheckedRecursiveAction() {
762 >            public void realCompute() {
763 >                FibAction f = new FibAction(8);
764 >                FailingFibAction g = new FailingFibAction(9);
765                  try {
641                    FibAction f = new FibAction(8);
642                    FailingFibAction g = new FailingFibAction(9);
766                      invokeAll(f, g);
767                      shouldThrow();
768                  } catch (FJException success) {
769 +                    checkCompletedAbnormally(g, success);
770                  }
771              }};
772 <        mainPool.invoke(a);
772 >        testInvokeOnPool(mainPool(), a);
773      }
774  
775      /**
776       * invokeAll(tasks) with 1 argument throws exception if task does
777       */
778      public void testAbnormalInvokeAll1() {
779 <        RecursiveAction a = new RecursiveAction() {
780 <            public void compute() {
779 >        RecursiveAction a = new CheckedRecursiveAction() {
780 >            public void realCompute() {
781 >                FailingFibAction g = new FailingFibAction(9);
782                  try {
658                    FailingFibAction g = new FailingFibAction(9);
783                      invokeAll(g);
784                      shouldThrow();
785                  } catch (FJException success) {
786 +                    checkCompletedAbnormally(g, success);
787                  }
788              }};
789 <        mainPool.invoke(a);
789 >        testInvokeOnPool(mainPool(), a);
790      }
791  
792      /**
793       * invokeAll(tasks) with > 2 argument throws exception if any task does
794       */
795      public void testAbnormalInvokeAll3() {
796 <        RecursiveAction a = new RecursiveAction() {
797 <            public void compute() {
796 >        RecursiveAction a = new CheckedRecursiveAction() {
797 >            public void realCompute() {
798 >                FibAction f = new FibAction(8);
799 >                FailingFibAction g = new FailingFibAction(9);
800 >                FibAction h = new FibAction(7);
801                  try {
674                    FibAction f = new FibAction(8);
675                    FailingFibAction g = new FailingFibAction(9);
676                    FibAction h = new FibAction(7);
802                      invokeAll(f, g, h);
803                      shouldThrow();
804                  } catch (FJException success) {
805 +                    checkCompletedAbnormally(g, success);
806                  }
807              }};
808 <        mainPool.invoke(a);
808 >        testInvokeOnPool(mainPool(), a);
809      }
810  
811      /**
812       * invokeAll(collection) throws exception if any task does
813       */
814      public void testAbnormalInvokeAllCollection() {
815 <        RecursiveAction a = new RecursiveAction() {
816 <            public void compute() {
815 >        RecursiveAction a = new CheckedRecursiveAction() {
816 >            public void realCompute() {
817 >                FailingFibAction f = new FailingFibAction(8);
818 >                FibAction g = new FibAction(9);
819 >                FibAction h = new FibAction(7);
820 >                HashSet set = new HashSet();
821 >                set.add(f);
822 >                set.add(g);
823 >                set.add(h);
824                  try {
692                    FailingFibAction f = new FailingFibAction(8);
693                    FibAction g = new FibAction(9);
694                    FibAction h = new FibAction(7);
695                    HashSet set = new HashSet();
696                    set.add(f);
697                    set.add(g);
698                    set.add(h);
825                      invokeAll(set);
826                      shouldThrow();
827                  } catch (FJException success) {
828 +                    checkCompletedAbnormally(f, success);
829                  }
830              }};
831 <        mainPool.invoke(a);
831 >        testInvokeOnPool(mainPool(), a);
832      }
833  
834      /**
# Line 709 | Line 836 | public class RecursiveActionTest extends
836       * and suppresses execution
837       */
838      public void testTryUnfork() {
839 <        RecursiveAction a = new RecursiveAction() {
840 <            public void compute() {
839 >        RecursiveAction a = new CheckedRecursiveAction() {
840 >            public void realCompute() {
841                  FibAction g = new FibAction(9);
842 <                g.fork();
842 >                assertSame(g, g.fork());
843                  FibAction f = new FibAction(8);
844 <                f.fork();
845 <                threadAssertTrue(f.tryUnfork());
844 >                assertSame(f, f.fork());
845 >                assertTrue(f.tryUnfork());
846                  helpQuiesce();
847 <                threadAssertFalse(f.isDone());
848 <                threadAssertTrue(g.isDone());
847 >                checkNotDone(f);
848 >                checkCompletedNormally(g);
849              }};
850 <        singletonPool.invoke(a);
850 >        testInvokeOnPool(singletonPool(), a);
851      }
852  
853      /**
# Line 728 | Line 855 | public class RecursiveActionTest extends
855       * there are more tasks than threads
856       */
857      public void testGetSurplusQueuedTaskCount() {
858 <        RecursiveAction a = new RecursiveAction() {
859 <            public void compute() {
858 >        RecursiveAction a = new CheckedRecursiveAction() {
859 >            public void realCompute() {
860                  FibAction h = new FibAction(7);
861 <                h.fork();
861 >                assertSame(h, h.fork());
862                  FibAction g = new FibAction(9);
863 <                g.fork();
863 >                assertSame(g, g.fork());
864                  FibAction f = new FibAction(8);
865 <                f.fork();
866 <                threadAssertTrue(getSurplusQueuedTaskCount() > 0);
865 >                assertSame(f, f.fork());
866 >                assertTrue(getSurplusQueuedTaskCount() > 0);
867                  helpQuiesce();
868 +                assertEquals(0, getSurplusQueuedTaskCount());
869 +                checkCompletedNormally(f);
870 +                checkCompletedNormally(g);
871 +                checkCompletedNormally(h);
872              }};
873 <        singletonPool.invoke(a);
873 >        testInvokeOnPool(singletonPool(), a);
874      }
875  
876      /**
877       * peekNextLocalTask returns most recent unexecuted task.
878       */
879      public void testPeekNextLocalTask() {
880 <        RecursiveAction a = new RecursiveAction() {
881 <            public void compute() {
880 >        RecursiveAction a = new CheckedRecursiveAction() {
881 >            public void realCompute() {
882                  FibAction g = new FibAction(9);
883 <                g.fork();
883 >                assertSame(g, g.fork());
884                  FibAction f = new FibAction(8);
885 <                f.fork();
886 <                threadAssertTrue(peekNextLocalTask() == f);
887 <                f.join();
888 <                threadAssertTrue(f.isDone());
885 >                assertSame(f, f.fork());
886 >                assertSame(f, peekNextLocalTask());
887 >                assertNull(f.join());
888 >                checkCompletedNormally(f);
889                  helpQuiesce();
890 +                checkCompletedNormally(f);
891 +                checkCompletedNormally(g);
892              }};
893 <        singletonPool.invoke(a);
893 >        testInvokeOnPool(singletonPool(), a);
894      }
895  
896      /**
# Line 765 | Line 898 | public class RecursiveActionTest extends
898       * without executing it
899       */
900      public void testPollNextLocalTask() {
901 <        RecursiveAction a = new RecursiveAction() {
902 <            public void compute() {
901 >        RecursiveAction a = new CheckedRecursiveAction() {
902 >            public void realCompute() {
903                  FibAction g = new FibAction(9);
904 <                g.fork();
904 >                assertSame(g, g.fork());
905                  FibAction f = new FibAction(8);
906 <                f.fork();
907 <                threadAssertTrue(pollNextLocalTask() == f);
906 >                assertSame(f, f.fork());
907 >                assertSame(f, pollNextLocalTask());
908                  helpQuiesce();
909 <                threadAssertFalse(f.isDone());
909 >                checkNotDone(f);
910 >                checkCompletedNormally(g);
911              }};
912 <        singletonPool.invoke(a);
912 >        testInvokeOnPool(singletonPool(), a);
913      }
914  
915      /**
916 <     * pollTask returns an unexecuted task
783 <     * without executing it
916 >     * pollTask returns an unexecuted task without executing it
917       */
918      public void testPollTask() {
919 <        RecursiveAction a = new RecursiveAction() {
920 <            public void compute() {
919 >        RecursiveAction a = new CheckedRecursiveAction() {
920 >            public void realCompute() {
921                  FibAction g = new FibAction(9);
922 <                g.fork();
922 >                assertSame(g, g.fork());
923                  FibAction f = new FibAction(8);
924 <                f.fork();
925 <                threadAssertTrue(pollTask() == f);
924 >                assertSame(f, f.fork());
925 >                assertSame(f, pollTask());
926                  helpQuiesce();
927 <                threadAssertFalse(f.isDone());
928 <                threadAssertTrue(g.isDone());
927 >                checkNotDone(f);
928 >                checkCompletedNormally(g);
929              }};
930 <        singletonPool.invoke(a);
930 >        testInvokeOnPool(singletonPool(), a);
931      }
932  
933      /**
934       * peekNextLocalTask returns least recent unexecuted task in async mode
935       */
936      public void testPeekNextLocalTaskAsync() {
937 <        RecursiveAction a = new RecursiveAction() {
938 <            public void compute() {
937 >        RecursiveAction a = new CheckedRecursiveAction() {
938 >            public void realCompute() {
939                  FibAction g = new FibAction(9);
940 <                g.fork();
940 >                assertSame(g, g.fork());
941                  FibAction f = new FibAction(8);
942 <                f.fork();
943 <                threadAssertTrue(peekNextLocalTask() == g);
944 <                f.join();
942 >                assertSame(f, f.fork());
943 >                assertSame(g, peekNextLocalTask());
944 >                assertNull(f.join());
945                  helpQuiesce();
946 <                threadAssertTrue(f.isDone());
946 >                checkCompletedNormally(f);
947 >                checkCompletedNormally(g);
948              }};
949 <        asyncSingletonPool.invoke(a);
949 >        testInvokeOnPool(asyncSingletonPool(), a);
950      }
951  
952      /**
953 <     * pollNextLocalTask returns least recent unexecuted task
954 <     * without executing it, in async mode
953 >     * pollNextLocalTask returns least recent unexecuted task without
954 >     * executing it, in async mode
955       */
956      public void testPollNextLocalTaskAsync() {
957 <        RecursiveAction a = new RecursiveAction() {
958 <            public void compute() {
957 >        RecursiveAction a = new CheckedRecursiveAction() {
958 >            public void realCompute() {
959                  FibAction g = new FibAction(9);
960 <                g.fork();
960 >                assertSame(g, g.fork());
961                  FibAction f = new FibAction(8);
962 <                f.fork();
963 <                threadAssertTrue(pollNextLocalTask() == g);
962 >                assertSame(f, f.fork());
963 >                assertSame(g, pollNextLocalTask());
964                  helpQuiesce();
965 <                threadAssertTrue(f.isDone());
966 <                threadAssertFalse(g.isDone());
965 >                checkCompletedNormally(f);
966 >                checkNotDone(g);
967              }};
968 <        asyncSingletonPool.invoke(a);
968 >        testInvokeOnPool(asyncSingletonPool(), a);
969      }
970  
971      /**
972 <     * pollTask returns an unexecuted task
973 <     * without executing it, in async mode
972 >     * pollTask returns an unexecuted task without executing it, in
973 >     * async mode
974       */
975      public void testPollTaskAsync() {
976 <        RecursiveAction a = new RecursiveAction() {
977 <            public void compute() {
976 >        RecursiveAction a = new CheckedRecursiveAction() {
977 >            public void realCompute() {
978                  FibAction g = new FibAction(9);
979 <                g.fork();
979 >                assertSame(g, g.fork());
980                  FibAction f = new FibAction(8);
981 <                f.fork();
982 <                threadAssertTrue(pollTask() == g);
981 >                assertSame(f, f.fork());
982 >                assertSame(g, pollTask());
983                  helpQuiesce();
984 <                threadAssertTrue(f.isDone());
985 <                threadAssertFalse(g.isDone());
984 >                checkCompletedNormally(f);
985 >                checkNotDone(g);
986              }};
987 <        asyncSingletonPool.invoke(a);
987 >        testInvokeOnPool(asyncSingletonPool(), a);
988      }
989  
990   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines