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.17 by dl, Wed Sep 15 12:46:57 2010 UTC vs.
Revision 1.20 by jsr166, Sun Nov 21 08:35:40 2010 UTC

# Line 11 | Line 11 | 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 {
# Line 39 | Line 41 | public class RecursiveActionTest extends
41  
42      private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
43          try {
44 <            assertTrue(pool.invoke(a) == null);
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 checkTaskThrew(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 91 | Line 201 | public class RecursiveActionTest extends
201       * completed tasks. getRawResult of a RecursiveAction returns null;
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 <                threadAssertNull(f.invoke());
208 <                threadAssertTrue(f.result == 21);
209 <                threadAssertTrue(f.isDone());
100 <                threadAssertFalse(f.isCancelled());
101 <                threadAssertFalse(f.isCompletedAbnormally());
102 <                threadAssertTrue(f.getRawResult() == null);
207 >                assertNull(f.invoke());
208 >                assertEquals(21, f.result);
209 >                checkCompletedNormally(f);
210              }};
211          testInvokeOnPool(mainPool(), a);
212      }
# Line 110 | 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());
119 <                threadAssertFalse(f.isCancelled());
120 <                threadAssertFalse(f.isCompletedAbnormally());
121 <                threadAssertTrue(f.getRawResult() == null);
224 >                assertEquals(21, f.result);
225 >                checkCompletedNormally(f);
226              }};
227          testInvokeOnPool(mainPool(), a);
228      }
# Line 127 | Line 231 | public class RecursiveActionTest extends
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 <                threadAssertSame(f, f.fork());
238 <                threadAssertNull(f.join());
239 <                threadAssertTrue(f.result == 21);
240 <                threadAssertTrue(f.isDone());
137 <                threadAssertTrue(f.getRawResult() == null);
237 >                assertSame(f, f.fork());
238 >                assertNull(f.join());
239 >                assertEquals(21, f.result);
240 >                checkCompletedNormally(f);
241              }};
242          testInvokeOnPool(mainPool(), a);
243      }
# Line 143 | Line 246 | public class RecursiveActionTest extends
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 <                    threadAssertSame(f, f.fork());
254 <                    threadAssertNull(f.get());
255 <                    threadAssertTrue(f.result == 21);
153 <                    threadAssertTrue(f.isDone());
154 <                } catch (Exception ex) {
155 <                    unexpectedException(ex);
156 <                }
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          testInvokeOnPool(mainPool(), a);
258      }
# Line 162 | Line 261 | public class RecursiveActionTest extends
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 <                    threadAssertSame(f, f.fork());
269 <                    threadAssertNull(f.get(5L, TimeUnit.SECONDS));
270 <                    threadAssertTrue(f.result == 21);
172 <                    threadAssertTrue(f.isDone());
173 <                } catch (Exception ex) {
174 <                    unexpectedException(ex);
175 <                }
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          testInvokeOnPool(mainPool(), a);
273      }
# Line 181 | Line 276 | public class RecursiveActionTest extends
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 {
187                    FibAction f = new FibAction(8);
188                    threadAssertSame(f, f.fork());
284                      f.get(5L, null);
285                      shouldThrow();
286 <                } catch (NullPointerException success) {
192 <                } catch (Exception ex) {
193 <                    unexpectedException(ex);
194 <                }
286 >                } catch (NullPointerException success) {}
287              }};
288          testInvokeOnPool(mainPool(), a);
289      }
# Line 200 | Line 292 | public class RecursiveActionTest extends
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 <                threadAssertSame(f, 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          testInvokeOnPool(mainPool(), a);
304      }
# Line 217 | 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 <                threadAssertSame(f, 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          testInvokeOnPool(mainPool(), a);
322      }
# Line 234 | 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 {
240                    FailingFibAction f = new FailingFibAction(8);
333                      f.invoke();
334                      shouldThrow();
335                  } catch (FJException success) {
336 +                    checkTaskThrew(f, success);
337                  }
338              }};
339          testInvokeOnPool(mainPool(), a);
# Line 250 | Line 343 | public class RecursiveActionTest extends
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 >                checkTaskThrew(f, f.getException());
352              }};
353          testInvokeOnPool(mainPool(), a);
354      }
# Line 263 | Line 357 | public class RecursiveActionTest extends
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 {
269                    FailingFibAction f = new FailingFibAction(8);
270                    threadAssertSame(f, f.fork());
365                      f.join();
366                      shouldThrow();
367                  } catch (FJException success) {
368 +                    checkTaskThrew(f, success);
369                  }
370              }};
371          testInvokeOnPool(mainPool(), a);
# Line 280 | Line 375 | public class RecursiveActionTest extends
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 {
286                    FailingFibAction f = new FailingFibAction(8);
287                    threadAssertSame(f, f.fork());
383                      f.get();
384                      shouldThrow();
385                  } catch (ExecutionException success) {
386 <                } catch (Exception ex) {
292 <                    unexpectedException(ex);
386 >                    checkTaskThrew(f, success.getCause());
387                  }
388              }};
389          testInvokeOnPool(mainPool(), a);
# Line 299 | Line 393 | public class RecursiveActionTest extends
393       * timed get of a forked task throws exception when task completes abnormally
394       */
395      public void testAbnormalForkTimedGet() {
396 <        RecursiveAction a = new RecursiveAction() {
397 <            public void compute() {
396 >        RecursiveAction a = new CheckedRecursiveAction() {
397 >            public void realCompute() throws Exception {
398 >                FailingFibAction f = new FailingFibAction(8);
399 >                assertSame(f, f.fork());
400                  try {
305                    FailingFibAction f = new FailingFibAction(8);
306                    threadAssertSame(f, f.fork());
401                      f.get(5L, TimeUnit.SECONDS);
402                      shouldThrow();
403                  } catch (ExecutionException success) {
404 <                } catch (Exception ex) {
311 <                    unexpectedException(ex);
404 >                    checkTaskThrew(f, success.getCause());
405                  }
406              }};
407          testInvokeOnPool(mainPool(), a);
# Line 318 | Line 411 | public class RecursiveActionTest extends
411       * quietlyJoin of a forked task returns when task completes abnormally
412       */
413      public void testAbnormalForkQuietlyJoin() {
414 <        RecursiveAction a = new RecursiveAction() {
415 <            public void compute() {
414 >        RecursiveAction a = new CheckedRecursiveAction() {
415 >            public void realCompute() {
416                  FailingFibAction f = new FailingFibAction(8);
417 <                threadAssertSame(f, f.fork());
417 >                assertSame(f, f.fork());
418                  f.quietlyJoin();
419 <                threadAssertTrue(f.isDone());
420 <                threadAssertTrue(f.isCompletedAbnormally());
328 <                threadAssertTrue(f.getException() instanceof FJException);
419 >                assertTrue(f.getException() instanceof FJException);
420 >                checkTaskThrew(f, f.getException());
421              }};
422          testInvokeOnPool(mainPool(), a);
423      }
# Line 334 | Line 426 | public class RecursiveActionTest extends
426       * invoke task throws exception when task cancelled
427       */
428      public void testCancelledInvoke() {
429 <        RecursiveAction a = new RecursiveAction() {
430 <            public void compute() {
429 >        RecursiveAction a = new CheckedRecursiveAction() {
430 >            public void realCompute() {
431 >                FibAction f = new FibAction(8);
432 >                assertTrue(f.cancel(true));
433                  try {
340                    FibAction f = new FibAction(8);
341                    threadAssertTrue(f.cancel(true));
434                      f.invoke();
435                      shouldThrow();
436                  } catch (CancellationException success) {
437 +                    checkCancelled(f);
438                  }
439              }};
440          testInvokeOnPool(mainPool(), a);
# Line 351 | Line 444 | public class RecursiveActionTest extends
444       * join of a forked task throws exception when task cancelled
445       */
446      public void testCancelledForkJoin() {
447 <        RecursiveAction a = new RecursiveAction() {
448 <            public void compute() {
447 >        RecursiveAction a = new CheckedRecursiveAction() {
448 >            public void realCompute() {
449 >                FibAction f = new FibAction(8);
450 >                assertTrue(f.cancel(true));
451 >                assertSame(f, f.fork());
452                  try {
357                    FibAction f = new FibAction(8);
358                    threadAssertTrue(f.cancel(true));
359                    threadAssertSame(f, f.fork());
453                      f.join();
454                      shouldThrow();
455                  } catch (CancellationException success) {
456 +                    checkCancelled(f);
457                  }
458              }};
459          testInvokeOnPool(mainPool(), a);
# Line 369 | Line 463 | public class RecursiveActionTest extends
463       * get of a forked task throws exception when task cancelled
464       */
465      public void testCancelledForkGet() {
466 <        RecursiveAction a = new RecursiveAction() {
467 <            public void compute() {
466 >        RecursiveAction a = new CheckedRecursiveAction() {
467 >            public void realCompute() throws Exception {
468 >                FibAction f = new FibAction(8);
469 >                assertTrue(f.cancel(true));
470 >                assertSame(f, f.fork());
471                  try {
375                    FibAction f = new FibAction(8);
376                    threadAssertTrue(f.cancel(true));
377                    threadAssertSame(f, f.fork());
472                      f.get();
473                      shouldThrow();
474                  } catch (CancellationException success) {
475 <                } catch (Exception ex) {
382 <                    unexpectedException(ex);
475 >                    checkCancelled(f);
476                  }
477              }};
478          testInvokeOnPool(mainPool(), a);
# Line 389 | Line 482 | public class RecursiveActionTest extends
482       * timed get of a forked task throws exception when task cancelled
483       */
484      public void testCancelledForkTimedGet() {
485 <        RecursiveAction a = new RecursiveAction() {
486 <            public void compute() {
485 >        RecursiveAction a = new CheckedRecursiveAction() {
486 >            public void realCompute() throws Exception {
487 >                FibAction f = new FibAction(8);
488 >                assertTrue(f.cancel(true));
489 >                assertSame(f, f.fork());
490                  try {
491 <                    FibAction f = new FibAction(8);
396 <                    threadAssertTrue(f.cancel(true));
397 <                    threadAssertSame(f, f.fork());
398 <                    f.get(5L, TimeUnit.SECONDS);
491 >                    f.get(5L, SECONDS);
492                      shouldThrow();
493                  } catch (CancellationException success) {
494 <                } catch (Exception ex) {
402 <                    unexpectedException(ex);
494 >                    checkCancelled(f);
495                  }
496              }};
497          testInvokeOnPool(mainPool(), a);
# Line 409 | Line 501 | public class RecursiveActionTest extends
501       * quietlyJoin of a forked task returns when task cancelled
502       */
503      public void testCancelledForkQuietlyJoin() {
504 <        RecursiveAction a = new RecursiveAction() {
505 <            public void compute() {
504 >        RecursiveAction a = new CheckedRecursiveAction() {
505 >            public void realCompute() {
506                  FibAction f = new FibAction(8);
507 <                threadAssertTrue(f.cancel(true));
508 <                threadAssertSame(f, f.fork());
507 >                assertTrue(f.cancel(true));
508 >                assertSame(f, f.fork());
509                  f.quietlyJoin();
510 <                threadAssertTrue(f.isDone());
419 <                threadAssertTrue(f.isCompletedAbnormally());
420 <                threadAssertTrue(f.getException() instanceof CancellationException);
510 >                checkCancelled(f);
511              }};
512          testInvokeOnPool(mainPool(), a);
513      }
# Line 427 | Line 517 | public class RecursiveActionTest extends
517       */
518      public void testGetPool() {
519          final ForkJoinPool mainPool = mainPool();
520 <        RecursiveAction a = new RecursiveAction() {
521 <            final ForkJoinPool p = mainPool;
522 <            public void compute() {
433 <                threadAssertTrue(getPool() == p);
520 >        RecursiveAction a = new CheckedRecursiveAction() {
521 >            public void realCompute() {
522 >                assertSame(mainPool, getPool());
523              }};
524          testInvokeOnPool(mainPool, a);
525      }
# Line 439 | Line 528 | public class RecursiveActionTest extends
528       * getPool of non-FJ task returns null
529       */
530      public void testGetPool2() {
531 <        RecursiveAction a = new RecursiveAction() {
532 <            public void compute() {
533 <                threadAssertTrue(getPool() == null);
531 >        RecursiveAction a = new CheckedRecursiveAction() {
532 >            public void realCompute() {
533 >                assertNull(getPool());
534              }};
535          assertNull(a.invoke());
536      }
# Line 450 | Line 539 | public class RecursiveActionTest extends
539       * inForkJoinPool of executing task returns true
540       */
541      public void testInForkJoinPool() {
542 <        RecursiveAction a = new RecursiveAction() {
543 <            public void compute() {
544 <                threadAssertTrue(inForkJoinPool());
542 >        RecursiveAction a = new CheckedRecursiveAction() {
543 >            public void realCompute() {
544 >                assertTrue(inForkJoinPool());
545              }};
546          testInvokeOnPool(mainPool(), a);
547      }
# Line 461 | Line 550 | public class RecursiveActionTest extends
550       * inForkJoinPool of non-FJ task returns false
551       */
552      public void testInForkJoinPool2() {
553 <        RecursiveAction a = new RecursiveAction() {
554 <            public void compute() {
555 <                threadAssertTrue(!inForkJoinPool());
553 >        RecursiveAction a = new CheckedRecursiveAction() {
554 >            public void realCompute() {
555 >                assertFalse(inForkJoinPool());
556              }};
557          assertNull(a.invoke());
558      }
# Line 473 | Line 562 | public class RecursiveActionTest extends
562       */
563      public void testWorkerGetPool() {
564          final ForkJoinPool mainPool = mainPool();
565 <        RecursiveAction a = new RecursiveAction() {
566 <            public void compute() {
565 >        RecursiveAction a = new CheckedRecursiveAction() {
566 >            public void realCompute() {
567                  ForkJoinWorkerThread w =
568                      (ForkJoinWorkerThread) Thread.currentThread();
569 <                threadAssertTrue(w.getPool() == mainPool);
569 >                assertSame(mainPool, w.getPool());
570              }};
571          testInvokeOnPool(mainPool, a);
572      }
# Line 487 | Line 576 | public class RecursiveActionTest extends
576       */
577      public void testWorkerGetPoolIndex() {
578          final ForkJoinPool mainPool = mainPool();
579 <        RecursiveAction a = new RecursiveAction() {
580 <            public void compute() {
579 >        RecursiveAction a = new CheckedRecursiveAction() {
580 >            public void realCompute() {
581                  ForkJoinWorkerThread w =
582                      (ForkJoinWorkerThread)(Thread.currentThread());
583 <                int idx = w.getPoolIndex();
584 <                threadAssertTrue(idx >= 0);
496 <                threadAssertTrue(idx < mainPool.getPoolSize());
583 >                assertTrue(w.getPoolIndex() >= 0);
584 >                assertTrue(w.getPoolIndex() < mainPool.getPoolSize());
585              }};
586          testInvokeOnPool(mainPool, a);
587      }
# Line 503 | Line 591 | public class RecursiveActionTest extends
591       * setRawResult(null) succeeds
592       */
593      public void testSetRawResult() {
594 <        RecursiveAction a = new RecursiveAction() {
595 <            public void compute() {
594 >        RecursiveAction a = new CheckedRecursiveAction() {
595 >            public void realCompute() {
596                  setRawResult(null);
597              }};
598          assertNull(a.invoke());
# Line 514 | Line 602 | public class RecursiveActionTest extends
602       * A reinitialized task may be re-invoked
603       */
604      public void testReinitialize() {
605 <        RecursiveAction a = new RecursiveAction() {
606 <            public void compute() {
605 >        RecursiveAction a = new CheckedRecursiveAction() {
606 >            public void realCompute() {
607                  FibAction f = new FibAction(8);
608 <                threadAssertNull(f.invoke());
609 <                threadAssertTrue(f.result == 21);
610 <                threadAssertTrue(f.isDone());
611 <                threadAssertFalse(f.isCancelled());
612 <                threadAssertFalse(f.isCompletedAbnormally());
613 <                f.reinitialize();
614 <                threadAssertNull(f.invoke());
615 <                threadAssertTrue(f.result == 21);
608 >                checkNotDone(f);
609 >
610 >                for (int i = 0; i < 3; i++) {
611 >                    assertNull(f.invoke());
612 >                    assertEquals(21, f.result);
613 >                    checkCompletedNormally(f);
614 >                    f.reinitialize();
615 >                    checkNotDone(f);
616 >                }
617              }};
618          testInvokeOnPool(mainPool(), a);
619      }
# Line 533 | Line 622 | public class RecursiveActionTest extends
622       * invoke task throws exception after invoking completeExceptionally
623       */
624      public void testCompleteExceptionally() {
625 <        RecursiveAction a = new RecursiveAction() {
626 <            public void compute() {
625 >        RecursiveAction a = new CheckedRecursiveAction() {
626 >            public void realCompute() {
627 >                FibAction f = new FibAction(8);
628 >                f.completeExceptionally(new FJException());
629                  try {
539                    FibAction f = new FibAction(8);
540                    f.completeExceptionally(new FJException());
630                      f.invoke();
631                      shouldThrow();
632                  } catch (FJException success) {
633 +                    checkTaskThrew(f, success);
634                  }
635              }};
636          testInvokeOnPool(mainPool(), a);
# Line 550 | Line 640 | public class RecursiveActionTest extends
640       * invoke task suppresses execution invoking complete
641       */
642      public void testComplete() {
643 <        RecursiveAction a = new RecursiveAction() {
644 <            public void compute() {
643 >        RecursiveAction a = new CheckedRecursiveAction() {
644 >            public void realCompute() {
645                  FibAction f = new FibAction(8);
646                  f.complete(null);
647 <                threadAssertNull(f.invoke());
648 <                threadAssertTrue(f.isDone());
649 <                threadAssertTrue(f.result == 0);
647 >                assertNull(f.invoke());
648 >                assertEquals(0, f.result);
649 >                checkCompletedNormally(f);
650              }};
651          testInvokeOnPool(mainPool(), a);
652      }
# Line 565 | Line 655 | public class RecursiveActionTest extends
655       * invokeAll(t1, t2) invokes all task arguments
656       */
657      public void testInvokeAll2() {
658 <        RecursiveAction a = new RecursiveAction() {
659 <            public void compute() {
658 >        RecursiveAction a = new CheckedRecursiveAction() {
659 >            public void realCompute() {
660                  FibAction f = new FibAction(8);
661                  FibAction g = new FibAction(9);
662                  invokeAll(f, g);
663 <                threadAssertTrue(f.isDone());
664 <                threadAssertTrue(f.result == 21);
665 <                threadAssertTrue(g.isDone());
666 <                threadAssertTrue(g.result == 34);
663 >                checkCompletedNormally(f);
664 >                assertEquals(21, f.result);
665 >                checkCompletedNormally(g);
666 >                assertEquals(34, g.result);
667              }};
668          testInvokeOnPool(mainPool(), a);
669      }
# Line 582 | Line 672 | public class RecursiveActionTest extends
672       * invokeAll(tasks) with 1 argument invokes task
673       */
674      public void testInvokeAll1() {
675 <        RecursiveAction a = new RecursiveAction() {
676 <            public void compute() {
675 >        RecursiveAction a = new CheckedRecursiveAction() {
676 >            public void realCompute() {
677                  FibAction f = new FibAction(8);
678                  invokeAll(f);
679 <                threadAssertTrue(f.isDone());
680 <                threadAssertTrue(f.result == 21);
679 >                checkCompletedNormally(f);
680 >                assertEquals(21, f.result);
681              }};
682          testInvokeOnPool(mainPool(), a);
683      }
# Line 596 | Line 686 | public class RecursiveActionTest extends
686       * invokeAll(tasks) with > 2 argument invokes tasks
687       */
688      public void testInvokeAll3() {
689 <        RecursiveAction a = new RecursiveAction() {
690 <            public void compute() {
689 >        RecursiveAction a = new CheckedRecursiveAction() {
690 >            public void realCompute() {
691                  FibAction f = new FibAction(8);
692                  FibAction g = new FibAction(9);
693                  FibAction h = new FibAction(7);
694                  invokeAll(f, g, h);
695 <                threadAssertTrue(f.isDone());
696 <                threadAssertTrue(f.result == 21);
697 <                threadAssertTrue(g.isDone());
698 <                threadAssertTrue(g.result == 34);
699 <                threadAssertTrue(h.isDone());
700 <                threadAssertTrue(h.result == 13);
695 >                assertTrue(f.isDone());
696 >                assertTrue(g.isDone());
697 >                assertTrue(h.isDone());
698 >                checkCompletedNormally(f);
699 >                assertEquals(21, f.result);
700 >                checkCompletedNormally(g);
701 >                assertEquals(34, g.result);
702 >                checkCompletedNormally(g);
703 >                assertEquals(13, h.result);
704              }};
705          testInvokeOnPool(mainPool(), a);
706      }
# Line 616 | Line 709 | public class RecursiveActionTest extends
709       * invokeAll(collection) invokes all tasks in the collection
710       */
711      public void testInvokeAllCollection() {
712 <        RecursiveAction a = new RecursiveAction() {
713 <            public void compute() {
712 >        RecursiveAction a = new CheckedRecursiveAction() {
713 >            public void realCompute() {
714                  FibAction f = new FibAction(8);
715                  FibAction g = new FibAction(9);
716                  FibAction h = new FibAction(7);
# Line 626 | Line 719 | public class RecursiveActionTest extends
719                  set.add(g);
720                  set.add(h);
721                  invokeAll(set);
722 <                threadAssertTrue(f.isDone());
723 <                threadAssertTrue(f.result == 21);
724 <                threadAssertTrue(g.isDone());
725 <                threadAssertTrue(g.result == 34);
726 <                threadAssertTrue(h.isDone());
727 <                threadAssertTrue(h.result == 13);
722 >                assertTrue(f.isDone());
723 >                assertTrue(g.isDone());
724 >                assertTrue(h.isDone());
725 >                checkCompletedNormally(f);
726 >                assertEquals(21, f.result);
727 >                checkCompletedNormally(g);
728 >                assertEquals(34, g.result);
729 >                checkCompletedNormally(g);
730 >                assertEquals(13, h.result);
731              }};
732          testInvokeOnPool(mainPool(), a);
733      }
# Line 641 | Line 737 | public class RecursiveActionTest extends
737       * invokeAll(tasks) with any null task throws NPE
738       */
739      public void testInvokeAllNPE() {
740 <        RecursiveAction a = new RecursiveAction() {
741 <            public void compute() {
740 >        RecursiveAction a = new CheckedRecursiveAction() {
741 >            public void realCompute() {
742 >                FibAction f = new FibAction(8);
743 >                FibAction g = new FibAction(9);
744 >                FibAction h = null;
745                  try {
647                    FibAction f = new FibAction(8);
648                    FibAction g = new FibAction(9);
649                    FibAction h = null;
746                      invokeAll(f, g, h);
747                      shouldThrow();
748 <                } catch (NullPointerException success) {
653 <                }
748 >                } catch (NullPointerException success) {}
749              }};
750          testInvokeOnPool(mainPool(), a);
751      }
# Line 659 | Line 754 | public class RecursiveActionTest extends
754       * invokeAll(t1, t2) throw exception if any task does
755       */
756      public void testAbnormalInvokeAll2() {
757 <        RecursiveAction a = new RecursiveAction() {
758 <            public void compute() {
757 >        RecursiveAction a = new CheckedRecursiveAction() {
758 >            public void realCompute() {
759 >                FibAction f = new FibAction(8);
760 >                FailingFibAction g = new FailingFibAction(9);
761                  try {
665                    FibAction f = new FibAction(8);
666                    FailingFibAction g = new FailingFibAction(9);
762                      invokeAll(f, g);
763                      shouldThrow();
764                  } catch (FJException success) {
765 +                    checkTaskThrew(g, success);
766                  }
767              }};
768          testInvokeOnPool(mainPool(), a);
# Line 676 | Line 772 | public class RecursiveActionTest extends
772       * invokeAll(tasks) with 1 argument throws exception if task does
773       */
774      public void testAbnormalInvokeAll1() {
775 <        RecursiveAction a = new RecursiveAction() {
776 <            public void compute() {
775 >        RecursiveAction a = new CheckedRecursiveAction() {
776 >            public void realCompute() {
777 >                FailingFibAction g = new FailingFibAction(9);
778                  try {
682                    FailingFibAction g = new FailingFibAction(9);
779                      invokeAll(g);
780                      shouldThrow();
781                  } catch (FJException success) {
782 +                    checkTaskThrew(g, success);
783                  }
784              }};
785          testInvokeOnPool(mainPool(), a);
# Line 692 | Line 789 | public class RecursiveActionTest extends
789       * invokeAll(tasks) with > 2 argument throws exception if any task does
790       */
791      public void testAbnormalInvokeAll3() {
792 <        RecursiveAction a = new RecursiveAction() {
793 <            public void compute() {
792 >        RecursiveAction a = new CheckedRecursiveAction() {
793 >            public void realCompute() {
794 >                FibAction f = new FibAction(8);
795 >                FailingFibAction g = new FailingFibAction(9);
796 >                FibAction h = new FibAction(7);
797                  try {
698                    FibAction f = new FibAction(8);
699                    FailingFibAction g = new FailingFibAction(9);
700                    FibAction h = new FibAction(7);
798                      invokeAll(f, g, h);
799                      shouldThrow();
800                  } catch (FJException success) {
801 +                    checkTaskThrew(g, success);
802                  }
803              }};
804          testInvokeOnPool(mainPool(), a);
# Line 710 | Line 808 | public class RecursiveActionTest extends
808       * invokeAll(collection) throws exception if any task does
809       */
810      public void testAbnormalInvokeAllCollection() {
811 <        RecursiveAction a = new RecursiveAction() {
812 <            public void compute() {
811 >        RecursiveAction a = new CheckedRecursiveAction() {
812 >            public void realCompute() {
813 >                FailingFibAction f = new FailingFibAction(8);
814 >                FibAction g = new FibAction(9);
815 >                FibAction h = new FibAction(7);
816 >                HashSet set = new HashSet();
817 >                set.add(f);
818 >                set.add(g);
819 >                set.add(h);
820                  try {
716                    FailingFibAction f = new FailingFibAction(8);
717                    FibAction g = new FibAction(9);
718                    FibAction h = new FibAction(7);
719                    HashSet set = new HashSet();
720                    set.add(f);
721                    set.add(g);
722                    set.add(h);
821                      invokeAll(set);
822                      shouldThrow();
823                  } catch (FJException success) {
824 +                    checkTaskThrew(f, success);
825                  }
826              }};
827          testInvokeOnPool(mainPool(), a);
# Line 733 | Line 832 | public class RecursiveActionTest extends
832       * and suppresses execution
833       */
834      public void testTryUnfork() {
835 <        RecursiveAction a = new RecursiveAction() {
836 <            public void compute() {
835 >        RecursiveAction a = new CheckedRecursiveAction() {
836 >            public void realCompute() {
837                  FibAction g = new FibAction(9);
838 <                threadAssertSame(g, g.fork());
838 >                assertSame(g, g.fork());
839                  FibAction f = new FibAction(8);
840 <                threadAssertSame(f, f.fork());
841 <                threadAssertTrue(f.tryUnfork());
840 >                assertSame(f, f.fork());
841 >                assertTrue(f.tryUnfork());
842                  helpQuiesce();
843 <                threadAssertFalse(f.isDone());
844 <                threadAssertTrue(g.isDone());
843 >                checkNotDone(f);
844 >                checkCompletedNormally(g);
845              }};
846          testInvokeOnPool(singletonPool(), a);
847      }
# Line 752 | Line 851 | public class RecursiveActionTest extends
851       * there are more tasks than threads
852       */
853      public void testGetSurplusQueuedTaskCount() {
854 <        RecursiveAction a = new RecursiveAction() {
855 <            public void compute() {
854 >        RecursiveAction a = new CheckedRecursiveAction() {
855 >            public void realCompute() {
856                  FibAction h = new FibAction(7);
857 <                threadAssertSame(h, h.fork());
857 >                assertSame(h, h.fork());
858                  FibAction g = new FibAction(9);
859 <                threadAssertSame(g, g.fork());
859 >                assertSame(g, g.fork());
860                  FibAction f = new FibAction(8);
861 <                threadAssertSame(f, f.fork());
862 <                threadAssertTrue(getSurplusQueuedTaskCount() > 0);
861 >                assertSame(f, f.fork());
862 >                assertTrue(getSurplusQueuedTaskCount() > 0);
863                  helpQuiesce();
864 +                checkCompletedNormally(f);
865 +                checkCompletedNormally(g);
866 +                checkCompletedNormally(h);
867              }};
868          testInvokeOnPool(singletonPool(), a);
869      }
# Line 770 | Line 872 | public class RecursiveActionTest extends
872       * peekNextLocalTask returns most recent unexecuted task.
873       */
874      public void testPeekNextLocalTask() {
875 <        RecursiveAction a = new RecursiveAction() {
876 <            public void compute() {
875 >        RecursiveAction a = new CheckedRecursiveAction() {
876 >            public void realCompute() {
877                  FibAction g = new FibAction(9);
878 <                threadAssertSame(g, g.fork());
878 >                assertSame(g, g.fork());
879                  FibAction f = new FibAction(8);
880 <                threadAssertSame(f, f.fork());
881 <                threadAssertTrue(peekNextLocalTask() == f);
882 <                threadAssertNull(f.join());
883 <                threadAssertTrue(f.isDone());
880 >                assertSame(f, f.fork());
881 >                assertSame(f, peekNextLocalTask());
882 >                assertNull(f.join());
883 >                checkCompletedNormally(f);
884                  helpQuiesce();
885 +                checkCompletedNormally(f);
886 +                checkCompletedNormally(g);
887              }};
888          testInvokeOnPool(singletonPool(), a);
889      }
# Line 789 | Line 893 | public class RecursiveActionTest extends
893       * without executing it
894       */
895      public void testPollNextLocalTask() {
896 <        RecursiveAction a = new RecursiveAction() {
897 <            public void compute() {
896 >        RecursiveAction a = new CheckedRecursiveAction() {
897 >            public void realCompute() {
898                  FibAction g = new FibAction(9);
899 <                threadAssertSame(g, g.fork());
899 >                assertSame(g, g.fork());
900                  FibAction f = new FibAction(8);
901 <                threadAssertSame(f, f.fork());
902 <                threadAssertTrue(pollNextLocalTask() == f);
901 >                assertSame(f, f.fork());
902 >                assertSame(f, pollNextLocalTask());
903                  helpQuiesce();
904 <                threadAssertFalse(f.isDone());
904 >                checkNotDone(f);
905 >                checkCompletedNormally(g);
906              }};
907          testInvokeOnPool(singletonPool(), a);
908      }
909  
910      /**
911 <     * pollTask returns an unexecuted task
807 <     * without executing it
911 >     * pollTask returns an unexecuted task without executing it
912       */
913      public void testPollTask() {
914 <        RecursiveAction a = new RecursiveAction() {
915 <            public void compute() {
914 >        RecursiveAction a = new CheckedRecursiveAction() {
915 >            public void realCompute() {
916                  FibAction g = new FibAction(9);
917 <                threadAssertSame(g, g.fork());
917 >                assertSame(g, g.fork());
918                  FibAction f = new FibAction(8);
919 <                threadAssertSame(f, f.fork());
920 <                threadAssertTrue(pollTask() == f);
919 >                assertSame(f, f.fork());
920 >                assertSame(f, pollTask());
921                  helpQuiesce();
922 <                threadAssertFalse(f.isDone());
923 <                threadAssertTrue(g.isDone());
922 >                checkNotDone(f);
923 >                checkCompletedNormally(g);
924              }};
925          testInvokeOnPool(singletonPool(), a);
926      }
# Line 825 | Line 929 | public class RecursiveActionTest extends
929       * peekNextLocalTask returns least recent unexecuted task in async mode
930       */
931      public void testPeekNextLocalTaskAsync() {
932 <        RecursiveAction a = new RecursiveAction() {
933 <            public void compute() {
932 >        RecursiveAction a = new CheckedRecursiveAction() {
933 >            public void realCompute() {
934                  FibAction g = new FibAction(9);
935 <                threadAssertSame(g, g.fork());
935 >                assertSame(g, g.fork());
936                  FibAction f = new FibAction(8);
937 <                threadAssertSame(f, f.fork());
938 <                threadAssertTrue(peekNextLocalTask() == g);
939 <                threadAssertNull(f.join());
937 >                assertSame(f, f.fork());
938 >                assertSame(g, peekNextLocalTask());
939 >                assertNull(f.join());
940                  helpQuiesce();
941 <                threadAssertTrue(f.isDone());
941 >                checkCompletedNormally(f);
942 >                checkCompletedNormally(g);
943              }};
944          testInvokeOnPool(asyncSingletonPool(), a);
945      }
946  
947      /**
948 <     * pollNextLocalTask returns least recent unexecuted task
949 <     * without executing it, in async mode
948 >     * pollNextLocalTask returns least recent unexecuted task without
949 >     * executing it, in async mode
950       */
951      public void testPollNextLocalTaskAsync() {
952 <        RecursiveAction a = new RecursiveAction() {
953 <            public void compute() {
952 >        RecursiveAction a = new CheckedRecursiveAction() {
953 >            public void realCompute() {
954                  FibAction g = new FibAction(9);
955 <                threadAssertSame(g, g.fork());
955 >                assertSame(g, g.fork());
956                  FibAction f = new FibAction(8);
957 <                threadAssertSame(f, f.fork());
958 <                threadAssertTrue(pollNextLocalTask() == g);
957 >                assertSame(f, f.fork());
958 >                assertSame(g, pollNextLocalTask());
959                  helpQuiesce();
960 <                threadAssertTrue(f.isDone());
961 <                threadAssertFalse(g.isDone());
960 >                checkCompletedNormally(f);
961 >                checkNotDone(g);
962              }};
963          testInvokeOnPool(asyncSingletonPool(), a);
964      }
965  
966      /**
967 <     * pollTask returns an unexecuted task
968 <     * without executing it, in async mode
967 >     * pollTask returns an unexecuted task without executing it, in
968 >     * async mode
969       */
970      public void testPollTaskAsync() {
971 <        RecursiveAction a = new RecursiveAction() {
972 <            public void compute() {
971 >        RecursiveAction a = new CheckedRecursiveAction() {
972 >            public void realCompute() {
973                  FibAction g = new FibAction(9);
974 <                threadAssertSame(g, g.fork());
974 >                assertSame(g, g.fork());
975                  FibAction f = new FibAction(8);
976 <                threadAssertSame(f, f.fork());
977 <                threadAssertTrue(pollTask() == g);
976 >                assertSame(f, f.fork());
977 >                assertSame(g, pollTask());
978                  helpQuiesce();
979 <                threadAssertTrue(f.isDone());
980 <                threadAssertFalse(g.isDone());
979 >                checkCompletedNormally(f);
980 >                checkNotDone(g);
981              }};
982          testInvokeOnPool(asyncSingletonPool(), a);
983      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines