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

Comparing jsr166/src/test/tck/ForkJoinTaskTest.java (file contents):
Revision 1.9 by jsr166, Wed Aug 25 00:07:03 2010 UTC vs.
Revision 1.17 by jsr166, Thu Sep 16 00:52:49 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 java.util.*;
7 import java.util.concurrent.Executor;
8 import java.util.concurrent.Executors;
9 import java.util.concurrent.ExecutorService;
10 import java.util.concurrent.AbstractExecutorService;
11 import java.util.concurrent.CountDownLatch;
12 import java.util.concurrent.Callable;
13 import java.util.concurrent.Future;
6   import java.util.concurrent.ExecutionException;
7   import java.util.concurrent.CancellationException;
16 import java.util.concurrent.RejectedExecutionException;
8   import java.util.concurrent.ForkJoinPool;
9   import java.util.concurrent.ForkJoinTask;
10   import java.util.concurrent.ForkJoinWorkerThread;
11   import java.util.concurrent.RecursiveAction;
21 import java.util.concurrent.RecursiveTask;
12   import java.util.concurrent.TimeUnit;
13 + import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
14 + import java.util.HashSet;
15   import junit.framework.*;
24 import java.util.concurrent.TimeUnit;
25 import java.util.concurrent.atomic.*;
26 import java.util.*;
16  
17   public class ForkJoinTaskTest extends JSR166TestCase {
18  
19      public static void main(String[] args) {
20          junit.textui.TestRunner.run(suite());
21      }
22 +
23      public static Test suite() {
24          return new TestSuite(ForkJoinTaskTest.class);
25      }
26  
27 <    /**
27 >    private static ForkJoinPool mainPool() {
28 >        return new ForkJoinPool();
29 >    }
30 >
31 >    private static ForkJoinPool singletonPool() {
32 >        return new ForkJoinPool(1);
33 >    }
34 >
35 >    private static ForkJoinPool asyncSingletonPool() {
36 >        return new ForkJoinPool(1,
37 >                                ForkJoinPool.defaultForkJoinWorkerThreadFactory,
38 >                                null, true);
39 >    }
40 >
41 >    private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
42 >        try {
43 >            assertFalse(a.isDone());
44 >            assertFalse(a.isCompletedNormally());
45 >            assertFalse(a.isCompletedAbnormally());
46 >            assertFalse(a.isCancelled());
47 >            assertNull(a.getException());
48 >
49 >            assertNull(pool.invoke(a));
50 >
51 >            assertTrue(a.isDone());
52 >            assertTrue(a.isCompletedNormally());
53 >            assertFalse(a.isCompletedAbnormally());
54 >            assertFalse(a.isCancelled());
55 >            assertNull(a.getException());
56 >        } finally {
57 >            joinPool(pool);
58 >        }
59 >    }
60 >
61 >    /*
62       * Testing coverage notes:
63       *
64       * To test extension methods and overrides, most tests use
# Line 42 | Line 66 | public class ForkJoinTaskTest extends JS
66       * differently than supplied Recursive forms.
67       */
68  
45    static final ForkJoinPool mainPool = new ForkJoinPool();
46    static final ForkJoinPool singletonPool = new ForkJoinPool(1);
47    static final ForkJoinPool asyncSingletonPool =
48        new ForkJoinPool(1, ForkJoinPool.defaultForkJoinWorkerThreadFactory,
49                         null, true);
69      static final class FJException extends RuntimeException {
70          FJException() { super(); }
71      }
# Line 224 | Line 243 | public class ForkJoinTaskTest extends JS
243       * invoke returns when task completes normally.
244       * isCompletedAbnormally and isCancelled return false for normally
245       * completed tasks. getRawResult of a RecursiveAction returns null;
227     *
246       */
247      public void testInvoke() {
248 <        RecursiveAction a = new RecursiveAction() {
249 <                public void compute() {
250 <                    AsyncFib f = new AsyncFib(8);
251 <                    f.invoke();
252 <                    threadAssertTrue(f.number == 21);
253 <                    threadAssertTrue(f.isDone());
254 <                    threadAssertFalse(f.isCancelled());
255 <                    threadAssertFalse(f.isCompletedAbnormally());
256 <                    threadAssertTrue(f.getRawResult() == null);
257 <                }
258 <            };
241 <        mainPool.invoke(a);
248 >        RecursiveAction a = new CheckedRecursiveAction() {
249 >            public void realCompute() {
250 >                AsyncFib f = new AsyncFib(8);
251 >                assertNull(f.invoke());
252 >                assertEquals(21, f.number);
253 >                assertTrue(f.isDone());
254 >                assertFalse(f.isCancelled());
255 >                assertFalse(f.isCompletedAbnormally());
256 >                assertNull(f.getRawResult());
257 >            }};
258 >        testInvokeOnPool(mainPool(), a);
259      }
260  
261      /**
# Line 247 | Line 264 | public class ForkJoinTaskTest extends JS
264       * completed tasks
265       */
266      public void testQuietlyInvoke() {
267 <        RecursiveAction a = new RecursiveAction() {
268 <                public void compute() {
269 <                    AsyncFib f = new AsyncFib(8);
270 <                    f.quietlyInvoke();
271 <                    threadAssertTrue(f.number == 21);
272 <                    threadAssertTrue(f.isDone());
273 <                    threadAssertFalse(f.isCancelled());
274 <                    threadAssertFalse(f.isCompletedAbnormally());
275 <                    threadAssertTrue(f.getRawResult() == null);
276 <                }
277 <            };
261 <        mainPool.invoke(a);
267 >        RecursiveAction a = new CheckedRecursiveAction() {
268 >            public void realCompute() {
269 >                AsyncFib f = new AsyncFib(8);
270 >                f.quietlyInvoke();
271 >                assertEquals(21, f.number);
272 >                assertTrue(f.isDone());
273 >                assertFalse(f.isCancelled());
274 >                assertFalse(f.isCompletedAbnormally());
275 >                assertNull(f.getRawResult());
276 >            }};
277 >        testInvokeOnPool(mainPool(), a);
278      }
279  
280      /**
281       * join of a forked task returns when task completes
282       */
283      public void testForkJoin() {
284 <        RecursiveAction a = new RecursiveAction() {
285 <                public void compute() {
286 <                    AsyncFib f = new AsyncFib(8);
287 <                    f.fork();
288 <                    f.join();
289 <                    threadAssertTrue(f.number == 21);
290 <                    threadAssertTrue(f.isDone());
291 <                    threadAssertTrue(f.getRawResult() == null);
292 <                }
293 <            };
278 <        mainPool.invoke(a);
284 >        RecursiveAction a = new CheckedRecursiveAction() {
285 >            public void realCompute() {
286 >                AsyncFib f = new AsyncFib(8);
287 >                assertSame(f, f.fork());
288 >                assertNull(f.join());
289 >                assertEquals(21, f.number);
290 >                assertTrue(f.isDone());
291 >                assertNull(f.getRawResult());
292 >            }};
293 >        testInvokeOnPool(mainPool(), a);
294      }
295  
296      /**
297       * get of a forked task returns when task completes
298       */
299      public void testForkGet() {
300 <        RecursiveAction a = new RecursiveAction() {
301 <                public void compute() {
302 <                    try {
303 <                        AsyncFib f = new AsyncFib(8);
304 <                        f.fork();
305 <                        f.get();
306 <                        threadAssertTrue(f.number == 21);
307 <                        threadAssertTrue(f.isDone());
308 <                    } catch (Exception ex) {
294 <                        unexpectedException(ex);
295 <                    }
296 <                }
297 <            };
298 <        mainPool.invoke(a);
300 >        RecursiveAction a = new CheckedRecursiveAction() {
301 >            public void realCompute() throws Exception {
302 >                AsyncFib f = new AsyncFib(8);
303 >                assertSame(f, f.fork());
304 >                assertNull(f.get());
305 >                assertEquals(21, f.number);
306 >                assertTrue(f.isDone());
307 >            }};
308 >        testInvokeOnPool(mainPool(), a);
309      }
310  
311      /**
312       * timed get of a forked task returns when task completes
313       */
314      public void testForkTimedGet() {
315 <        RecursiveAction a = new RecursiveAction() {
316 <                public void compute() {
317 <                    try {
318 <                        AsyncFib f = new AsyncFib(8);
319 <                        f.fork();
320 <                        f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
321 <                        threadAssertTrue(f.number == 21);
322 <                        threadAssertTrue(f.isDone());
323 <                    } catch (Exception ex) {
314 <                        unexpectedException(ex);
315 <                    }
316 <                }
317 <            };
318 <        mainPool.invoke(a);
315 >        RecursiveAction a = new CheckedRecursiveAction() {
316 >            public void realCompute() throws Exception {
317 >                AsyncFib f = new AsyncFib(8);
318 >                assertSame(f, f.fork());
319 >                assertNull(f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
320 >                assertEquals(21, f.number);
321 >                assertTrue(f.isDone());
322 >            }};
323 >        testInvokeOnPool(mainPool(), a);
324      }
325  
326      /**
327       * timed get with null time unit throws NPE
328       */
329      public void testForkTimedGetNPE() {
330 <        RecursiveAction a = new RecursiveAction() {
331 <                public void compute() {
332 <                    try {
333 <                        AsyncFib f = new AsyncFib(8);
334 <                        f.fork();
335 <                        f.get(5L, null);
336 <                        shouldThrow();
337 <                    } catch (NullPointerException success) {
338 <                    } catch (Exception ex) {
339 <                        unexpectedException(ex);
335 <                    }
336 <                }
337 <            };
338 <        mainPool.invoke(a);
330 >        RecursiveAction a = new CheckedRecursiveAction() {
331 >            public void realCompute() throws Exception {
332 >                AsyncFib f = new AsyncFib(8);
333 >                assertSame(f, f.fork());
334 >                try {
335 >                    f.get(5L, null);
336 >                    shouldThrow();
337 >                } catch (NullPointerException success) {}
338 >            }};
339 >        testInvokeOnPool(mainPool(), a);
340      }
341  
342      /**
343       * quietlyJoin of a forked task returns when task completes
344       */
345      public void testForkQuietlyJoin() {
346 <        RecursiveAction a = new RecursiveAction() {
347 <                public void compute() {
348 <                    AsyncFib f = new AsyncFib(8);
349 <                    f.fork();
350 <                    f.quietlyJoin();
351 <                    threadAssertTrue(f.number == 21);
352 <                    threadAssertTrue(f.isDone());
353 <                }
354 <            };
354 <        mainPool.invoke(a);
346 >        RecursiveAction a = new CheckedRecursiveAction() {
347 >            public void realCompute() {
348 >                AsyncFib f = new AsyncFib(8);
349 >                assertSame(f, f.fork());
350 >                f.quietlyJoin();
351 >                assertEquals(21, f.number);
352 >                assertTrue(f.isDone());
353 >            }};
354 >        testInvokeOnPool(mainPool(), a);
355      }
356  
357  
# Line 360 | Line 360 | public class ForkJoinTaskTest extends JS
360       * getQueuedTaskCount returns 0 when quiescent
361       */
362      public void testForkHelpQuiesce() {
363 <        RecursiveAction a = new RecursiveAction() {
364 <                public void compute() {
365 <                    AsyncFib f = new AsyncFib(8);
366 <                    f.fork();
367 <                    f.helpQuiesce();
368 <                    threadAssertTrue(f.number == 21);
369 <                    threadAssertTrue(f.isDone());
370 <                    threadAssertTrue(getQueuedTaskCount() == 0);
371 <                }
372 <            };
373 <        mainPool.invoke(a);
363 >        RecursiveAction a = new CheckedRecursiveAction() {
364 >            public void realCompute() {
365 >                AsyncFib f = new AsyncFib(8);
366 >                assertSame(f, f.fork());
367 >                f.helpQuiesce();
368 >                assertEquals(21, f.number);
369 >                assertTrue(f.isDone());
370 >                assertEquals(0, getQueuedTaskCount());
371 >            }};
372 >        testInvokeOnPool(mainPool(), a);
373      }
374  
375  
# Line 378 | Line 377 | public class ForkJoinTaskTest extends JS
377       * invoke task throws exception when task completes abnormally
378       */
379      public void testAbnormalInvoke() {
380 <        RecursiveAction a = new RecursiveAction() {
381 <                public void compute() {
382 <                    try {
383 <                        FailingAsyncFib f = new FailingAsyncFib(8);
384 <                        f.invoke();
385 <                        shouldThrow();
386 <                    } catch (FJException success) {
387 <                    }
388 <                }
390 <            };
391 <        mainPool.invoke(a);
380 >        RecursiveAction a = new CheckedRecursiveAction() {
381 >            public void realCompute() {
382 >                FailingAsyncFib f = new FailingAsyncFib(8);
383 >                try {
384 >                    f.invoke();
385 >                    shouldThrow();
386 >                } catch (FJException success) {}
387 >            }};
388 >        testInvokeOnPool(mainPool(), a);
389      }
390  
391      /**
392       * quietlyInvoke task returns when task completes abnormally
393       */
394      public void testAbnormalQuietlyInvoke() {
395 <        RecursiveAction a = new RecursiveAction() {
396 <                public void compute() {
397 <                    FailingAsyncFib f = new FailingAsyncFib(8);
398 <                    f.quietlyInvoke();
399 <                    threadAssertTrue(f.isDone());
400 <                }
401 <            };
405 <        mainPool.invoke(a);
395 >        RecursiveAction a = new CheckedRecursiveAction() {
396 >            public void realCompute() {
397 >                FailingAsyncFib f = new FailingAsyncFib(8);
398 >                f.quietlyInvoke();
399 >                assertTrue(f.isDone());
400 >            }};
401 >        testInvokeOnPool(mainPool(), a);
402      }
403  
404      /**
405       * join of a forked task throws exception when task completes abnormally
406       */
407      public void testAbnormalForkJoin() {
408 <        RecursiveAction a = new RecursiveAction() {
409 <                public void compute() {
410 <                    try {
411 <                        FailingAsyncFib f = new FailingAsyncFib(8);
412 <                        f.fork();
413 <                        f.join();
414 <                        shouldThrow();
415 <                    } catch (FJException success) {
416 <                    }
417 <                }
422 <            };
423 <        mainPool.invoke(a);
408 >        RecursiveAction a = new CheckedRecursiveAction() {
409 >            public void realCompute() {
410 >                FailingAsyncFib f = new FailingAsyncFib(8);
411 >                assertSame(f, f.fork());
412 >                try {
413 >                    f.join();
414 >                    shouldThrow();
415 >                } catch (FJException success) {}
416 >            }};
417 >        testInvokeOnPool(mainPool(), a);
418      }
419  
420      /**
421       * get of a forked task throws exception when task completes abnormally
422       */
423      public void testAbnormalForkGet() {
424 <        RecursiveAction a = new RecursiveAction() {
425 <                public void compute() {
426 <                    try {
427 <                        FailingAsyncFib f = new FailingAsyncFib(8);
428 <                        f.fork();
429 <                        f.get();
430 <                        shouldThrow();
431 <                    } catch (ExecutionException success) {
432 <                    } catch (Exception ex) {
433 <                        unexpectedException(ex);
440 <                    }
441 <                }
442 <            };
443 <        mainPool.invoke(a);
424 >        RecursiveAction a = new CheckedRecursiveAction() {
425 >            public void realCompute() throws Exception {
426 >                FailingAsyncFib f = new FailingAsyncFib(8);
427 >                assertSame(f, f.fork());
428 >                try {
429 >                    f.get();
430 >                    shouldThrow();
431 >                } catch (ExecutionException success) {}
432 >            }};
433 >        testInvokeOnPool(mainPool(), a);
434      }
435  
436      /**
437       * timed get of a forked task throws exception when task completes abnormally
438       */
439      public void testAbnormalForkTimedGet() {
440 <        RecursiveAction a = new RecursiveAction() {
441 <                public void compute() {
442 <                    try {
443 <                        FailingAsyncFib f = new FailingAsyncFib(8);
444 <                        f.fork();
445 <                        f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
446 <                        shouldThrow();
447 <                    } catch (ExecutionException success) {
448 <                    } catch (Exception ex) {
449 <                        unexpectedException(ex);
460 <                    }
461 <                }
462 <            };
463 <        mainPool.invoke(a);
440 >        RecursiveAction a = new CheckedRecursiveAction() {
441 >            public void realCompute() throws Exception {
442 >                FailingAsyncFib f = new FailingAsyncFib(8);
443 >                assertSame(f, f.fork());
444 >                try {
445 >                    f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
446 >                    shouldThrow();
447 >                } catch (ExecutionException success) {}
448 >            }};
449 >        testInvokeOnPool(mainPool(), a);
450      }
451  
452      /**
453       * quietlyJoin of a forked task returns when task completes abnormally
454       */
455      public void testAbnormalForkQuietlyJoin() {
456 <        RecursiveAction a = new RecursiveAction() {
457 <                public void compute() {
458 <                    FailingAsyncFib f = new FailingAsyncFib(8);
459 <                    f.fork();
460 <                    f.quietlyJoin();
461 <                    threadAssertTrue(f.isDone());
462 <                    threadAssertTrue(f.isCompletedAbnormally());
463 <                    threadAssertTrue(f.getException() instanceof FJException);
464 <                }
465 <            };
480 <        mainPool.invoke(a);
456 >        RecursiveAction a = new CheckedRecursiveAction() {
457 >            public void realCompute() {
458 >                FailingAsyncFib f = new FailingAsyncFib(8);
459 >                assertSame(f, f.fork());
460 >                f.quietlyJoin();
461 >                assertTrue(f.isDone());
462 >                assertTrue(f.isCompletedAbnormally());
463 >                assertTrue(f.getException() instanceof FJException);
464 >            }};
465 >        testInvokeOnPool(mainPool(), a);
466      }
467  
468      /**
469       * invoke task throws exception when task cancelled
470       */
471      public void testCancelledInvoke() {
472 <        RecursiveAction a = new RecursiveAction() {
473 <                public void compute() {
474 <                    try {
475 <                        AsyncFib f = new AsyncFib(8);
476 <                        f.cancel(true);
477 <                        f.invoke();
478 <                        shouldThrow();
479 <                    } catch (CancellationException success) {
480 <                    }
481 <                }
497 <            };
498 <        mainPool.invoke(a);
472 >        RecursiveAction a = new CheckedRecursiveAction() {
473 >            public void realCompute() {
474 >                AsyncFib f = new AsyncFib(8);
475 >                assertTrue(f.cancel(true));
476 >                try {
477 >                    f.invoke();
478 >                    shouldThrow();
479 >                } catch (CancellationException success) {}
480 >            }};
481 >        testInvokeOnPool(mainPool(), a);
482      }
483  
484      /**
485       * join of a forked task throws exception when task cancelled
486       */
487      public void testCancelledForkJoin() {
488 <        RecursiveAction a = new RecursiveAction() {
489 <                public void compute() {
490 <                    try {
491 <                        AsyncFib f = new AsyncFib(8);
492 <                        f.cancel(true);
493 <                        f.fork();
494 <                        f.join();
495 <                        shouldThrow();
496 <                    } catch (CancellationException success) {
497 <                    }
498 <                }
516 <            };
517 <        mainPool.invoke(a);
488 >        RecursiveAction a = new CheckedRecursiveAction() {
489 >            public void realCompute() {
490 >                AsyncFib f = new AsyncFib(8);
491 >                assertTrue(f.cancel(true));
492 >                assertSame(f, f.fork());
493 >                try {
494 >                    f.join();
495 >                    shouldThrow();
496 >                } catch (CancellationException success) {}
497 >            }};
498 >        testInvokeOnPool(mainPool(), a);
499      }
500  
501      /**
502       * get of a forked task throws exception when task cancelled
503       */
504      public void testCancelledForkGet() {
505 <        RecursiveAction a = new RecursiveAction() {
506 <                public void compute() {
507 <                    try {
508 <                        AsyncFib f = new AsyncFib(8);
509 <                        f.cancel(true);
510 <                        f.fork();
511 <                        f.get();
512 <                        shouldThrow();
513 <                    } catch (CancellationException success) {
514 <                    } catch (Exception ex) {
515 <                        unexpectedException(ex);
535 <                    }
536 <                }
537 <            };
538 <        mainPool.invoke(a);
505 >        RecursiveAction a = new CheckedRecursiveAction() {
506 >            public void realCompute() throws Exception {
507 >                AsyncFib f = new AsyncFib(8);
508 >                assertTrue(f.cancel(true));
509 >                assertSame(f, f.fork());
510 >                try {
511 >                    f.get();
512 >                    shouldThrow();
513 >                } catch (CancellationException success) {}
514 >            }};
515 >        testInvokeOnPool(mainPool(), a);
516      }
517  
518      /**
519       * timed get of a forked task throws exception when task cancelled
520       */
521 <    public void testCancelledForkTimedGet() {
522 <        RecursiveAction a = new RecursiveAction() {
523 <                public void compute() {
524 <                    try {
525 <                        AsyncFib f = new AsyncFib(8);
526 <                        f.cancel(true);
527 <                        f.fork();
528 <                        f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
529 <                        shouldThrow();
530 <                    } catch (CancellationException success) {
531 <                    } catch (Exception ex) {
532 <                        unexpectedException(ex);
556 <                    }
557 <                }
558 <            };
559 <        mainPool.invoke(a);
521 >    public void testCancelledForkTimedGet() throws Exception {
522 >        RecursiveAction a = new CheckedRecursiveAction() {
523 >            public void realCompute() throws Exception {
524 >                AsyncFib f = new AsyncFib(8);
525 >                assertTrue(f.cancel(true));
526 >                assertSame(f, f.fork());
527 >                try {
528 >                    f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
529 >                    shouldThrow();
530 >                } catch (CancellationException success) {}
531 >            }};
532 >        testInvokeOnPool(mainPool(), a);
533      }
534  
535      /**
536       * quietlyJoin of a forked task returns when task cancelled
537       */
538      public void testCancelledForkQuietlyJoin() {
539 <        RecursiveAction a = new RecursiveAction() {
540 <                public void compute() {
541 <                    AsyncFib f = new AsyncFib(8);
542 <                    f.cancel(true);
543 <                    f.fork();
544 <                    f.quietlyJoin();
545 <                    threadAssertTrue(f.isDone());
546 <                    threadAssertTrue(f.isCompletedAbnormally());
547 <                    threadAssertTrue(f.getException() instanceof CancellationException);
548 <                }
549 <            };
550 <        mainPool.invoke(a);
539 >        RecursiveAction a = new CheckedRecursiveAction() {
540 >            public void realCompute() {
541 >                AsyncFib f = new AsyncFib(8);
542 >                assertTrue(f.cancel(true));
543 >                assertSame(f, f.fork());
544 >                f.quietlyJoin();
545 >                assertTrue(f.isDone());
546 >                assertTrue(f.isCompletedAbnormally());
547 >                assertTrue(f.isCancelled());
548 >                assertTrue(f.getException() instanceof CancellationException);
549 >            }};
550 >        testInvokeOnPool(mainPool(), a);
551      }
552  
553      /**
554       * getPool of executing task returns its pool
555       */
556      public void testGetPool() {
557 <        RecursiveAction a = new RecursiveAction() {
558 <                public void compute() {
559 <                    threadAssertTrue(getPool() == mainPool);
560 <                }
561 <            };
562 <        mainPool.invoke(a);
557 >        final ForkJoinPool mainPool = mainPool();
558 >        RecursiveAction a = new CheckedRecursiveAction() {
559 >            public void realCompute() {
560 >                assertSame(mainPool, getPool());
561 >            }};
562 >        testInvokeOnPool(mainPool, a);
563      }
564  
565      /**
566       * getPool of non-FJ task returns null
567       */
568      public void testGetPool2() {
569 <        RecursiveAction a = new RecursiveAction() {
570 <                public void compute() {
571 <                    threadAssertTrue(getPool() == null);
572 <                }
573 <            };
601 <        a.invoke();
569 >        RecursiveAction a = new CheckedRecursiveAction() {
570 >            public void realCompute() {
571 >                assertNull(getPool());
572 >            }};
573 >        assertNull(a.invoke());
574      }
575  
576      /**
577       * inForkJoinPool of executing task returns true
578       */
579      public void testInForkJoinPool() {
580 <        RecursiveAction a = new RecursiveAction() {
581 <                public void compute() {
582 <                    threadAssertTrue(inForkJoinPool());
583 <                }
584 <            };
613 <        mainPool.invoke(a);
580 >        RecursiveAction a = new CheckedRecursiveAction() {
581 >            public void realCompute() {
582 >                assertTrue(inForkJoinPool());
583 >            }};
584 >        testInvokeOnPool(mainPool(), a);
585      }
586  
587      /**
588       * inForkJoinPool of non-FJ task returns false
589       */
590      public void testInForkJoinPool2() {
591 <        RecursiveAction a = new RecursiveAction() {
592 <                public void compute() {
593 <                    threadAssertTrue(!inForkJoinPool());
594 <                }
595 <            };
625 <        a.invoke();
591 >        RecursiveAction a = new CheckedRecursiveAction() {
592 >            public void realCompute() {
593 >                assertTrue(!inForkJoinPool());
594 >            }};
595 >        assertNull(a.invoke());
596      }
597  
598      /**
599       * setRawResult(null) succeeds
600       */
601      public void testSetRawResult() {
602 <        RecursiveAction a = new RecursiveAction() {
603 <                public void compute() {
604 <                    setRawResult(null);
605 <                }
606 <            };
637 <        a.invoke();
602 >        RecursiveAction a = new CheckedRecursiveAction() {
603 >            public void realCompute() {
604 >                setRawResult(null);
605 >            }};
606 >        assertNull(a.invoke());
607      }
608  
609      /**
610       * invoke task throws exception after invoking completeExceptionally
611       */
612      public void testCompleteExceptionally() {
613 <        RecursiveAction a = new RecursiveAction() {
614 <                public void compute() {
615 <                    try {
616 <                        AsyncFib f = new AsyncFib(8);
617 <                        f.completeExceptionally(new FJException());
618 <                        f.invoke();
619 <                        shouldThrow();
620 <                    } catch (FJException success) {
621 <                    }
622 <                }
654 <            };
655 <        mainPool.invoke(a);
613 >        RecursiveAction a = new CheckedRecursiveAction() {
614 >            public void realCompute() {
615 >                AsyncFib f = new AsyncFib(8);
616 >                f.completeExceptionally(new FJException());
617 >                try {
618 >                    f.invoke();
619 >                    shouldThrow();
620 >                } catch (FJException success) {}
621 >            }};
622 >        testInvokeOnPool(mainPool(), a);
623      }
624  
625      /**
626       * invokeAll(t1, t2) invokes all task arguments
627       */
628      public void testInvokeAll2() {
629 <        RecursiveAction a = new RecursiveAction() {
630 <                public void compute() {
631 <                    AsyncFib f = new AsyncFib(8);
632 <                    AsyncFib g = new AsyncFib(9);
633 <                    invokeAll(f, g);
634 <                    threadAssertTrue(f.isDone());
635 <                    threadAssertTrue(f.number == 21);
636 <                    threadAssertTrue(g.isDone());
637 <                    threadAssertTrue(g.number == 34);
638 <                }
639 <            };
673 <        mainPool.invoke(a);
629 >        RecursiveAction a = new CheckedRecursiveAction() {
630 >            public void realCompute() {
631 >                AsyncFib f = new AsyncFib(8);
632 >                AsyncFib g = new AsyncFib(9);
633 >                invokeAll(f, g);
634 >                assertTrue(f.isDone());
635 >                assertEquals(21, f.number);
636 >                assertTrue(g.isDone());
637 >                assertEquals(34, g.number);
638 >            }};
639 >        testInvokeOnPool(mainPool(), a);
640      }
641  
642      /**
643       * invokeAll(tasks) with 1 argument invokes task
644       */
645      public void testInvokeAll1() {
646 <        RecursiveAction a = new RecursiveAction() {
647 <                public void compute() {
648 <                    AsyncFib f = new AsyncFib(8);
649 <                    invokeAll(f);
650 <                    threadAssertTrue(f.isDone());
651 <                    threadAssertTrue(f.number == 21);
652 <                }
653 <            };
688 <        mainPool.invoke(a);
646 >        RecursiveAction a = new CheckedRecursiveAction() {
647 >            public void realCompute() {
648 >                AsyncFib f = new AsyncFib(8);
649 >                invokeAll(f);
650 >                assertTrue(f.isDone());
651 >                assertEquals(21, f.number);
652 >            }};
653 >        testInvokeOnPool(mainPool(), a);
654      }
655  
656      /**
657       * invokeAll(tasks) with > 2 argument invokes tasks
658       */
659      public void testInvokeAll3() {
660 <        RecursiveAction a = new RecursiveAction() {
661 <                public void compute() {
662 <                    AsyncFib f = new AsyncFib(8);
663 <                    AsyncFib g = new AsyncFib(9);
664 <                    AsyncFib h = new AsyncFib(7);
665 <                    invokeAll(f, g, h);
666 <                    threadAssertTrue(f.isDone());
667 <                    threadAssertTrue(f.number == 21);
668 <                    threadAssertTrue(g.isDone());
669 <                    threadAssertTrue(g.number == 34);
670 <                    threadAssertTrue(h.isDone());
671 <                    threadAssertTrue(h.number == 13);
672 <                }
673 <            };
709 <        mainPool.invoke(a);
660 >        RecursiveAction a = new CheckedRecursiveAction() {
661 >            public void realCompute() {
662 >                AsyncFib f = new AsyncFib(8);
663 >                AsyncFib g = new AsyncFib(9);
664 >                AsyncFib h = new AsyncFib(7);
665 >                invokeAll(f, g, h);
666 >                assertTrue(f.isDone());
667 >                assertEquals(21, f.number);
668 >                assertTrue(g.isDone());
669 >                assertEquals(34, g.number);
670 >                assertTrue(h.isDone());
671 >                assertEquals(13, h.number);
672 >            }};
673 >        testInvokeOnPool(mainPool(), a);
674      }
675  
676      /**
677       * invokeAll(collection) invokes all tasks in the collection
678       */
679      public void testInvokeAllCollection() {
680 <        RecursiveAction a = new RecursiveAction() {
681 <                public void compute() {
682 <                    AsyncFib f = new AsyncFib(8);
683 <                    AsyncFib g = new AsyncFib(9);
684 <                    AsyncFib h = new AsyncFib(7);
685 <                    HashSet set = new HashSet();
686 <                    set.add(f);
687 <                    set.add(g);
688 <                    set.add(h);
689 <                    invokeAll(set);
690 <                    threadAssertTrue(f.isDone());
691 <                    threadAssertTrue(f.number == 21);
692 <                    threadAssertTrue(g.isDone());
693 <                    threadAssertTrue(g.number == 34);
694 <                    threadAssertTrue(h.isDone());
695 <                    threadAssertTrue(h.number == 13);
696 <                }
697 <            };
734 <        mainPool.invoke(a);
680 >        RecursiveAction a = new CheckedRecursiveAction() {
681 >            public void realCompute() {
682 >                AsyncFib f = new AsyncFib(8);
683 >                AsyncFib g = new AsyncFib(9);
684 >                AsyncFib h = new AsyncFib(7);
685 >                HashSet set = new HashSet();
686 >                set.add(f);
687 >                set.add(g);
688 >                set.add(h);
689 >                invokeAll(set);
690 >                assertTrue(f.isDone());
691 >                assertEquals(21, f.number);
692 >                assertTrue(g.isDone());
693 >                assertEquals(34, g.number);
694 >                assertTrue(h.isDone());
695 >                assertEquals(13, h.number);
696 >            }};
697 >        testInvokeOnPool(mainPool(), a);
698      }
699  
700  
# Line 739 | Line 702 | public class ForkJoinTaskTest extends JS
702       * invokeAll(tasks) with any null task throws NPE
703       */
704      public void testInvokeAllNPE() {
705 <        RecursiveAction a = new RecursiveAction() {
706 <                public void compute() {
707 <                    try {
708 <                        AsyncFib f = new AsyncFib(8);
709 <                        AsyncFib g = new AsyncFib(9);
710 <                        AsyncFib h = null;
711 <                        invokeAll(f, g, h);
712 <                        shouldThrow();
713 <                    } catch (NullPointerException success) {
714 <                    }
715 <                }
753 <            };
754 <        mainPool.invoke(a);
705 >        RecursiveAction a = new CheckedRecursiveAction() {
706 >            public void realCompute() {
707 >                AsyncFib f = new AsyncFib(8);
708 >                AsyncFib g = new AsyncFib(9);
709 >                AsyncFib h = null;
710 >                try {
711 >                    invokeAll(f, g, h);
712 >                    shouldThrow();
713 >                } catch (NullPointerException success) {}
714 >            }};
715 >        testInvokeOnPool(mainPool(), a);
716      }
717  
718      /**
719       * invokeAll(t1, t2) throw exception if any task does
720       */
721      public void testAbnormalInvokeAll2() {
722 <        RecursiveAction a = new RecursiveAction() {
723 <                public void compute() {
724 <                    try {
725 <                        AsyncFib f = new AsyncFib(8);
726 <                        FailingAsyncFib g = new FailingAsyncFib(9);
727 <                        invokeAll(f, g);
728 <                        shouldThrow();
729 <                    } catch (FJException success) {
730 <                    }
731 <                }
771 <            };
772 <        mainPool.invoke(a);
722 >        RecursiveAction a = new CheckedRecursiveAction() {
723 >            public void realCompute() {
724 >                AsyncFib f = new AsyncFib(8);
725 >                FailingAsyncFib g = new FailingAsyncFib(9);
726 >                try {
727 >                    invokeAll(f, g);
728 >                    shouldThrow();
729 >                } catch (FJException success) {}
730 >            }};
731 >        testInvokeOnPool(mainPool(), a);
732      }
733  
734      /**
735       * invokeAll(tasks) with 1 argument throws exception if task does
736       */
737      public void testAbnormalInvokeAll1() {
738 <        RecursiveAction a = new RecursiveAction() {
739 <                public void compute() {
740 <                    try {
741 <                        FailingAsyncFib g = new FailingAsyncFib(9);
742 <                        invokeAll(g);
743 <                        shouldThrow();
744 <                    } catch (FJException success) {
745 <                    }
746 <                }
788 <            };
789 <        mainPool.invoke(a);
738 >        RecursiveAction a = new CheckedRecursiveAction() {
739 >            public void realCompute() {
740 >                FailingAsyncFib g = new FailingAsyncFib(9);
741 >                try {
742 >                    invokeAll(g);
743 >                    shouldThrow();
744 >                } catch (FJException success) {}
745 >            }};
746 >        testInvokeOnPool(mainPool(), a);
747      }
748  
749      /**
750       * invokeAll(tasks) with > 2 argument throws exception if any task does
751       */
752      public void testAbnormalInvokeAll3() {
753 <        RecursiveAction a = new RecursiveAction() {
754 <                public void compute() {
755 <                    try {
756 <                        AsyncFib f = new AsyncFib(8);
757 <                        FailingAsyncFib g = new FailingAsyncFib(9);
758 <                        AsyncFib h = new AsyncFib(7);
759 <                        invokeAll(f, g, h);
760 <                        shouldThrow();
761 <                    } catch (FJException success) {
762 <                    }
763 <                }
807 <            };
808 <        mainPool.invoke(a);
753 >        RecursiveAction a = new CheckedRecursiveAction() {
754 >            public void realCompute() {
755 >                AsyncFib f = new AsyncFib(8);
756 >                FailingAsyncFib g = new FailingAsyncFib(9);
757 >                AsyncFib h = new AsyncFib(7);
758 >                try {
759 >                    invokeAll(f, g, h);
760 >                    shouldThrow();
761 >                } catch (FJException success) {}
762 >            }};
763 >        testInvokeOnPool(mainPool(), a);
764      }
765  
766      /**
767       * invokeAll(collection)  throws exception if any task does
768       */
769      public void testAbnormalInvokeAllCollection() {
770 <        RecursiveAction a = new RecursiveAction() {
771 <                public void compute() {
772 <                    try {
773 <                        FailingAsyncFib f = new FailingAsyncFib(8);
774 <                        AsyncFib g = new AsyncFib(9);
775 <                        AsyncFib h = new AsyncFib(7);
776 <                        HashSet set = new HashSet();
777 <                        set.add(f);
778 <                        set.add(g);
779 <                        set.add(h);
780 <                        invokeAll(set);
781 <                        shouldThrow();
782 <                    } catch (FJException success) {
783 <                    }
784 <                }
830 <            };
831 <        mainPool.invoke(a);
770 >        RecursiveAction a = new CheckedRecursiveAction() {
771 >            public void realCompute() {
772 >                FailingAsyncFib f = new FailingAsyncFib(8);
773 >                AsyncFib g = new AsyncFib(9);
774 >                AsyncFib h = new AsyncFib(7);
775 >                HashSet set = new HashSet();
776 >                set.add(f);
777 >                set.add(g);
778 >                set.add(h);
779 >                try {
780 >                    invokeAll(set);
781 >                    shouldThrow();
782 >                } catch (FJException success) {}
783 >            }};
784 >        testInvokeOnPool(mainPool(), a);
785      }
786  
787      /**
# Line 836 | Line 789 | public class ForkJoinTaskTest extends JS
789       * and suppresses execution
790       */
791      public void testTryUnfork() {
792 <        RecursiveAction a = new RecursiveAction() {
793 <                public void compute() {
794 <                    AsyncFib g = new AsyncFib(9);
795 <                    g.fork();
796 <                    AsyncFib f = new AsyncFib(8);
797 <                    f.fork();
798 <                    threadAssertTrue(f.tryUnfork());
799 <                    helpQuiesce();
800 <                    threadAssertFalse(f.isDone());
801 <                    threadAssertTrue(g.isDone());
802 <                }
803 <            };
851 <        singletonPool.invoke(a);
792 >        RecursiveAction a = new CheckedRecursiveAction() {
793 >            public void realCompute() {
794 >                AsyncFib g = new AsyncFib(9);
795 >                assertSame(g, g.fork());
796 >                AsyncFib f = new AsyncFib(8);
797 >                assertSame(f, f.fork());
798 >                assertTrue(f.tryUnfork());
799 >                helpQuiesce();
800 >                assertFalse(f.isDone());
801 >                assertTrue(g.isDone());
802 >            }};
803 >        testInvokeOnPool(singletonPool(), a);
804      }
805  
806      /**
# Line 856 | Line 808 | public class ForkJoinTaskTest extends JS
808       * there are more tasks than threads
809       */
810      public void testGetSurplusQueuedTaskCount() {
811 <        RecursiveAction a = new RecursiveAction() {
812 <                public void compute() {
813 <                    AsyncFib h = new AsyncFib(7);
814 <                    h.fork();
815 <                    AsyncFib g = new AsyncFib(9);
816 <                    g.fork();
817 <                    AsyncFib f = new AsyncFib(8);
818 <                    f.fork();
819 <                    threadAssertTrue(getSurplusQueuedTaskCount() > 0);
820 <                    helpQuiesce();
821 <                }
822 <            };
871 <        singletonPool.invoke(a);
811 >        RecursiveAction a = new CheckedRecursiveAction() {
812 >            public void realCompute() {
813 >                AsyncFib h = new AsyncFib(7);
814 >                assertSame(h, h.fork());
815 >                AsyncFib g = new AsyncFib(9);
816 >                assertSame(g, g.fork());
817 >                AsyncFib f = new AsyncFib(8);
818 >                assertSame(f, f.fork());
819 >                assertTrue(getSurplusQueuedTaskCount() > 0);
820 >                helpQuiesce();
821 >            }};
822 >        testInvokeOnPool(singletonPool(), a);
823      }
824  
825      /**
826       * peekNextLocalTask returns most recent unexecuted task.
827       */
828      public void testPeekNextLocalTask() {
829 <        RecursiveAction a = new RecursiveAction() {
830 <                public void compute() {
831 <                    AsyncFib g = new AsyncFib(9);
832 <                    g.fork();
833 <                    AsyncFib f = new AsyncFib(8);
834 <                    f.fork();
835 <                    threadAssertTrue(peekNextLocalTask() == f);
836 <                    f.join();
837 <                    threadAssertTrue(f.isDone());
838 <                    helpQuiesce();
839 <                }
840 <            };
890 <        singletonPool.invoke(a);
829 >        RecursiveAction a = new CheckedRecursiveAction() {
830 >            public void realCompute() {
831 >                AsyncFib g = new AsyncFib(9);
832 >                assertSame(g, g.fork());
833 >                AsyncFib f = new AsyncFib(8);
834 >                assertSame(f, f.fork());
835 >                assertSame(f, peekNextLocalTask());
836 >                assertNull(f.join());
837 >                assertTrue(f.isDone());
838 >                helpQuiesce();
839 >            }};
840 >        testInvokeOnPool(singletonPool(), a);
841      }
842  
843      /**
# Line 895 | Line 845 | public class ForkJoinTaskTest extends JS
845       * without executing it
846       */
847      public void testPollNextLocalTask() {
848 <        RecursiveAction a = new RecursiveAction() {
849 <                public void compute() {
850 <                    AsyncFib g = new AsyncFib(9);
851 <                    g.fork();
852 <                    AsyncFib f = new AsyncFib(8);
853 <                    f.fork();
854 <                    threadAssertTrue(pollNextLocalTask() == f);
855 <                    helpQuiesce();
856 <                    threadAssertFalse(f.isDone());
857 <                }
858 <            };
909 <        singletonPool.invoke(a);
848 >        RecursiveAction a = new CheckedRecursiveAction() {
849 >            public void realCompute() {
850 >                AsyncFib g = new AsyncFib(9);
851 >                assertSame(g, g.fork());
852 >                AsyncFib f = new AsyncFib(8);
853 >                assertSame(f, f.fork());
854 >                assertSame(f, pollNextLocalTask());
855 >                helpQuiesce();
856 >                assertFalse(f.isDone());
857 >            }};
858 >        testInvokeOnPool(singletonPool(), a);
859      }
860  
861      /**
# Line 914 | Line 863 | public class ForkJoinTaskTest extends JS
863       * without executing it
864       */
865      public void testPollTask() {
866 <        RecursiveAction a = new RecursiveAction() {
867 <                public void compute() {
868 <                    AsyncFib g = new AsyncFib(9);
869 <                    g.fork();
870 <                    AsyncFib f = new AsyncFib(8);
871 <                    f.fork();
872 <                    threadAssertTrue(pollTask() == f);
873 <                    helpQuiesce();
874 <                    threadAssertFalse(f.isDone());
875 <                    threadAssertTrue(g.isDone());
876 <                }
877 <            };
929 <        singletonPool.invoke(a);
866 >        RecursiveAction a = new CheckedRecursiveAction() {
867 >            public void realCompute() {
868 >                AsyncFib g = new AsyncFib(9);
869 >                assertSame(g, g.fork());
870 >                AsyncFib f = new AsyncFib(8);
871 >                assertSame(f, f.fork());
872 >                assertSame(f, pollTask());
873 >                helpQuiesce();
874 >                assertFalse(f.isDone());
875 >                assertTrue(g.isDone());
876 >            }};
877 >        testInvokeOnPool(singletonPool(), a);
878      }
879  
880      /**
881       * peekNextLocalTask returns least recent unexecuted task in async mode
882       */
883      public void testPeekNextLocalTaskAsync() {
884 <        RecursiveAction a = new RecursiveAction() {
885 <                public void compute() {
886 <                    AsyncFib g = new AsyncFib(9);
887 <                    g.fork();
888 <                    AsyncFib f = new AsyncFib(8);
889 <                    f.fork();
890 <                    threadAssertTrue(peekNextLocalTask() == g);
891 <                    f.join();
892 <                    helpQuiesce();
893 <                    threadAssertTrue(f.isDone());
894 <                }
895 <            };
948 <        asyncSingletonPool.invoke(a);
884 >        RecursiveAction a = new CheckedRecursiveAction() {
885 >            public void realCompute() {
886 >                AsyncFib g = new AsyncFib(9);
887 >                assertSame(g, g.fork());
888 >                AsyncFib f = new AsyncFib(8);
889 >                assertSame(f, f.fork());
890 >                assertSame(g, peekNextLocalTask());
891 >                assertNull(f.join());
892 >                helpQuiesce();
893 >                assertTrue(f.isDone());
894 >            }};
895 >        testInvokeOnPool(asyncSingletonPool(), a);
896      }
897  
898      /**
# Line 953 | Line 900 | public class ForkJoinTaskTest extends JS
900       * without executing it, in async mode
901       */
902      public void testPollNextLocalTaskAsync() {
903 <        RecursiveAction a = new RecursiveAction() {
904 <                public void compute() {
905 <                    AsyncFib g = new AsyncFib(9);
906 <                    g.fork();
907 <                    AsyncFib f = new AsyncFib(8);
908 <                    f.fork();
909 <                    threadAssertTrue(pollNextLocalTask() == g);
910 <                    helpQuiesce();
911 <                    threadAssertTrue(f.isDone());
912 <                    threadAssertFalse(g.isDone());
913 <                }
914 <            };
968 <        asyncSingletonPool.invoke(a);
903 >        RecursiveAction a = new CheckedRecursiveAction() {
904 >            public void realCompute() {
905 >                AsyncFib g = new AsyncFib(9);
906 >                assertSame(g, g.fork());
907 >                AsyncFib f = new AsyncFib(8);
908 >                assertSame(f, f.fork());
909 >                assertSame(g, pollNextLocalTask());
910 >                helpQuiesce();
911 >                assertTrue(f.isDone());
912 >                assertFalse(g.isDone());
913 >            }};
914 >        testInvokeOnPool(asyncSingletonPool(), a);
915      }
916  
917      /**
# Line 973 | Line 919 | public class ForkJoinTaskTest extends JS
919       * without executing it, in async mode
920       */
921      public void testPollTaskAsync() {
922 <        RecursiveAction a = new RecursiveAction() {
923 <                public void compute() {
924 <                    AsyncFib g = new AsyncFib(9);
925 <                    g.fork();
926 <                    AsyncFib f = new AsyncFib(8);
927 <                    f.fork();
928 <                    threadAssertTrue(pollTask() == g);
929 <                    helpQuiesce();
930 <                    threadAssertTrue(f.isDone());
931 <                    threadAssertFalse(g.isDone());
932 <                }
933 <            };
988 <        asyncSingletonPool.invoke(a);
922 >        RecursiveAction a = new CheckedRecursiveAction() {
923 >            public void realCompute() {
924 >                AsyncFib g = new AsyncFib(9);
925 >                assertSame(g, g.fork());
926 >                AsyncFib f = new AsyncFib(8);
927 >                assertSame(f, f.fork());
928 >                assertSame(g, pollTask());
929 >                helpQuiesce();
930 >                assertTrue(f.isDone());
931 >                assertFalse(g.isDone());
932 >            }};
933 >        testInvokeOnPool(asyncSingletonPool(), a);
934      }
935   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines