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.2 by jsr166, Fri Jul 31 23:37:31 2009 UTC vs.
Revision 1.8 by dl, Wed Aug 11 19:50:02 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;
14 + import java.util.concurrent.ExecutionException;
15 + import java.util.concurrent.CancellationException;
16 + import java.util.concurrent.RejectedExecutionException;
17 + import java.util.concurrent.ForkJoinPool;
18 + import java.util.concurrent.ForkJoinTask;
19 + import java.util.concurrent.ForkJoinWorkerThread;
20 + import java.util.concurrent.RecursiveAction;
21 + import java.util.concurrent.RecursiveTask;
22 + import java.util.concurrent.TimeUnit;
23   import junit.framework.*;
24 < import java.util.concurrent.*;
24 > import java.util.concurrent.TimeUnit;
25   import java.util.concurrent.atomic.*;
26   import java.util.*;
27  
28   public class ForkJoinTaskTest extends JSR166TestCase {
29  
30      public static void main(String[] args) {
31 <        junit.textui.TestRunner.run (suite());
31 >        junit.textui.TestRunner.run (suite());
32      }
33      public static Test suite() {
34 <        return new TestSuite(ForkJoinTaskTest.class);
34 >        return new TestSuite(ForkJoinTaskTest.class);
35      }
36  
37      /**
# Line 27 | Line 44 | public class ForkJoinTaskTest extends JS
44  
45      static final ForkJoinPool mainPool = new ForkJoinPool();
46      static final ForkJoinPool singletonPool = new ForkJoinPool(1);
47 <    static final ForkJoinPool asyncSingletonPool = new ForkJoinPool(1);
48 <    static {
49 <        asyncSingletonPool.setAsyncMode(true);
33 <    }
34 <
47 >    static final ForkJoinPool asyncSingletonPool =
48 >        new ForkJoinPool(1, ForkJoinPool.defaultForkJoinWorkerThreadFactory,
49 >                         null, true);
50      static final class FJException extends RuntimeException {
51          FJException() { super(); }
52      }
# Line 148 | Line 163 | public class ForkJoinTaskTest extends JS
163  
164      }
165  
166 <    static final class AsyncFib  extends BinaryAsyncAction {
166 >    static final class AsyncFib extends BinaryAsyncAction {
167          int number;
168          public AsyncFib(int n) {
169              this.number = n;
# Line 177 | Line 192 | public class ForkJoinTaskTest extends JS
192      }
193  
194  
195 <    static final class FailingAsyncFib  extends BinaryAsyncAction {
195 >    static final class FailingAsyncFib extends BinaryAsyncAction {
196          int number;
197          public FailingAsyncFib(int n) {
198              this.number = n;
# Line 276 | Line 291 | public class ForkJoinTaskTest extends JS
291                          threadAssertTrue(f.number == 21);
292                          threadAssertTrue(f.isDone());
293                      } catch (Exception ex) {
294 <                        unexpectedException();
294 >                        unexpectedException(ex);
295                      }
296                  }
297              };
# Line 292 | Line 307 | public class ForkJoinTaskTest extends JS
307                      try {
308                          AsyncFib f = new AsyncFib(8);
309                          f.fork();
310 <                        f.get(5L, TimeUnit.SECONDS);
310 >                        f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
311                          threadAssertTrue(f.number == 21);
312                          threadAssertTrue(f.isDone());
313                      } catch (Exception ex) {
314 <                        unexpectedException();
314 >                        unexpectedException(ex);
315                      }
316                  }
317              };
# Line 313 | Line 328 | public class ForkJoinTaskTest extends JS
328                          AsyncFib f = new AsyncFib(8);
329                          f.fork();
330                          f.get(5L, null);
331 +                        shouldThrow();
332                      } catch (NullPointerException success) {
333                      } catch (Exception ex) {
334 <                        unexpectedException();
334 >                        unexpectedException(ex);
335                      }
336                  }
337              };
# Line 323 | Line 339 | public class ForkJoinTaskTest extends JS
339      }
340  
341      /**
326     * helpJoin of a forked task returns when task completes
327     */
328    public void testForkHelpJoin() {
329        RecursiveAction a = new RecursiveAction() {
330                public void compute() {
331                    AsyncFib f = new AsyncFib(8);
332                    f.fork();
333                    f.helpJoin();
334                    threadAssertTrue(f.number == 21);
335                    threadAssertTrue(f.isDone());
336                }
337            };
338        mainPool.invoke(a);
339    }
340
341    /**
342       * quietlyJoin of a forked task returns when task completes
343       */
344      public void testForkQuietlyJoin() {
# Line 356 | Line 356 | public class ForkJoinTaskTest extends JS
356  
357  
358      /**
359     * quietlyHelpJoin of a forked task returns when task completes
360     */
361    public void testForkQuietlyHelpJoin() {
362        RecursiveAction a = new RecursiveAction() {
363                public void compute() {
364                    AsyncFib f = new AsyncFib(8);
365                    f.fork();
366                    f.quietlyHelpJoin();
367                    threadAssertTrue(f.number == 21);
368                    threadAssertTrue(f.isDone());
369                }
370            };
371        mainPool.invoke(a);
372    }
373
374
375    /**
359       * helpQuiesce returns when tasks are complete.
360       * getQueuedTaskCount returns 0 when quiescent
361       */
# Line 409 | Line 392 | public class ForkJoinTaskTest extends JS
392      }
393  
394      /**
395 <     * quietelyInvoke task returns when task completes abnormally
395 >     * quietlyInvoke task returns when task completes abnormally
396       */
397      public void testAbnormalQuietlyInvoke() {
398          RecursiveAction a = new RecursiveAction() {
# Line 451 | Line 434 | public class ForkJoinTaskTest extends JS
434                          f.fork();
435                          f.get();
436                          shouldThrow();
437 <                    } catch (Exception success) {
437 >                    } catch (ExecutionException success) {
438 >                    } catch (Exception ex) {
439 >                        unexpectedException(ex);
440                      }
441                  }
442              };
# Line 467 | Line 452 | public class ForkJoinTaskTest extends JS
452                      try {
453                          FailingAsyncFib f = new FailingAsyncFib(8);
454                          f.fork();
455 <                        f.get(5L, TimeUnit.SECONDS);
471 <                        shouldThrow();
472 <                    } catch (Exception success) {
473 <                    }
474 <                }
475 <            };
476 <        mainPool.invoke(a);
477 <    }
478 <
479 <    /**
480 <     * join of a forked task throws exception when task completes abnormally
481 <     */
482 <    public void testAbnormalForkHelpJoin() {
483 <        RecursiveAction a = new RecursiveAction() {
484 <                public void compute() {
485 <                    try {
486 <                        FailingAsyncFib f = new FailingAsyncFib(8);
487 <                        f.fork();
488 <                        f.helpJoin();
455 >                        f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
456                          shouldThrow();
457 <                    } catch (FJException success) {
457 >                    } catch (ExecutionException success) {
458 >                    } catch (Exception ex) {
459 >                        unexpectedException(ex);
460                      }
461                  }
462              };
# Line 495 | Line 464 | public class ForkJoinTaskTest extends JS
464      }
465  
466      /**
498     * quietlyHelpJoin of a forked task returns when task completes abnormally.
499     * getException of failed task returns its exception.
500     * isCompletedAbnormally of a failed task returns true.
501     * isCancelled of a failed uncancelled task returns false
502     */
503    public void testAbnormalForkQuietlyHelpJoin() {
504        RecursiveAction a = new RecursiveAction() {
505                public void compute() {
506                    FailingAsyncFib f = new FailingAsyncFib(8);
507                    f.fork();
508                    f.quietlyHelpJoin();
509                    threadAssertTrue(f.isDone());
510                    threadAssertTrue(f.isCompletedAbnormally());
511                    threadAssertFalse(f.isCancelled());
512                    threadAssertTrue(f.getException() instanceof FJException);
513                }
514            };
515        mainPool.invoke(a);
516    }
517
518    /**
467       * quietlyJoin of a forked task returns when task completes abnormally
468       */
469      public void testAbnormalForkQuietlyJoin() {
# Line 581 | Line 529 | public class ForkJoinTaskTest extends JS
529                          f.fork();
530                          f.get();
531                          shouldThrow();
532 <                    } catch (Exception success) {
532 >                    } catch (CancellationException success) {
533 >                    } catch (Exception ex) {
534 >                        unexpectedException(ex);
535                      }
536                  }
537              };
# Line 598 | Line 548 | public class ForkJoinTaskTest extends JS
548                          AsyncFib f = new AsyncFib(8);
549                          f.cancel(true);
550                          f.fork();
551 <                        f.get(5L, TimeUnit.SECONDS);
602 <                        shouldThrow();
603 <                    } catch (Exception success) {
604 <                    }
605 <                }
606 <            };
607 <        mainPool.invoke(a);
608 <    }
609 <
610 <    /**
611 <     * join of a forked task throws exception when task cancelled
612 <     */
613 <    public void testCancelledForkHelpJoin() {
614 <        RecursiveAction a = new RecursiveAction() {
615 <                public void compute() {
616 <                    try {
617 <                        AsyncFib f = new AsyncFib(8);
618 <                        f.cancel(true);
619 <                        f.fork();
620 <                        f.helpJoin();
551 >                        f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
552                          shouldThrow();
553                      } catch (CancellationException success) {
554 +                    } catch (Exception ex) {
555 +                        unexpectedException(ex);
556                      }
557                  }
558              };
559          mainPool.invoke(a);
627    }
628
629    /**
630     * quietlyHelpJoin of a forked task returns when task cancelled.
631     * getException of cancelled task returns its exception.
632     * isCompletedAbnormally of a cancelled task returns true.
633     * isCancelled of a cancelled task returns true
634     */
635    public void testCancelledForkQuietlyHelpJoin() {
636        RecursiveAction a = new RecursiveAction() {
637                public void compute() {
638                    AsyncFib f = new AsyncFib(8);
639                    f.cancel(true);
640                    f.fork();
641                    f.quietlyHelpJoin();
642                    threadAssertTrue(f.isDone());
643                    threadAssertTrue(f.isCompletedAbnormally());
644                    threadAssertTrue(f.isCancelled());
645                    threadAssertTrue(f.getException() instanceof CancellationException);
646                }
647            };
648        mainPool.invoke(a);
560      }
561  
562      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines