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.1 by dl, Fri Jul 31 23:02:49 2009 UTC vs.
Revision 1.11 by jsr166, Sat Sep 11 07:31:52 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      /**
38       * Testing coverage notes:
39 <     *
39 >     *
40       * To test extension methods and overrides, most tests use
41       * BinaryAsyncAction extension class that processes joins
42       * differently than supplied Recursive forms.
43 <     */    
43 >     */
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 40 | Line 55 | public class ForkJoinTaskTest extends JS
55          private volatile int controlState;
56  
57          static final AtomicIntegerFieldUpdater<BinaryAsyncAction> controlStateUpdater =
58 <            AtomicIntegerFieldUpdater.newUpdater(BinaryAsyncAction.class,
58 >            AtomicIntegerFieldUpdater.newUpdater(BinaryAsyncAction.class,
59                                                   "controlState");
60  
61          private BinaryAsyncAction parent;
# Line 92 | Line 107 | public class ForkJoinTaskTest extends JS
107                      break;
108                  try {
109                      p.onComplete(a, s);
110 <                } catch(Throwable rex) {
110 >                } catch (Throwable rex) {
111                      p.completeExceptionally(rex);
112                      return;
113                  }
# Line 129 | Line 144 | public class ForkJoinTaskTest extends JS
144              return controlState;
145          }
146  
147 <        protected final boolean compareAndSetControlState(int expect,
147 >        protected final boolean compareAndSetControlState(int expect,
148                                                            int update) {
149              return controlStateUpdater.compareAndSet(this, expect, update);
150          }
# 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) {
168 >        public AsyncFib(int n) {
169              this.number = n;
170          }
171 <        
171 >
172          public final boolean exec() {
173              AsyncFib f = this;
174              int n = f.number;
# 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) {
197 >        public FailingAsyncFib(int n) {
198              this.number = n;
199          }
200 <        
200 >
201          public final boolean exec() {
202              FailingAsyncFib f = this;
203              int n = f.number;
# Line 205 | Line 220 | public class ForkJoinTaskTest extends JS
220          }
221      }
222  
223 <    /**
223 >    /**
224       * invoke returns when task completes normally.
225       * isCompletedAbnormally and isCancelled return false for normally
226       * completed tasks. getRawResult of a RecursiveAction returns null;
212     *
227       */
228      public void testInvoke() {
229          RecursiveAction a = new RecursiveAction() {
# Line 226 | Line 240 | public class ForkJoinTaskTest extends JS
240          mainPool.invoke(a);
241      }
242  
243 <    /**
243 >    /**
244       * quietlyInvoke task returns when task completes normally.
245       * isCompletedAbnormally and isCancelled return false for normally
246       * completed tasks
# Line 246 | Line 260 | public class ForkJoinTaskTest extends JS
260          mainPool.invoke(a);
261      }
262  
263 <    /**
263 >    /**
264       * join of a forked task returns when task completes
265       */
266      public void testForkJoin() {
# Line 263 | Line 277 | public class ForkJoinTaskTest extends JS
277          mainPool.invoke(a);
278      }
279  
280 <    /**
280 >    /**
281       * get of a forked task returns when task completes
282       */
283      public void testForkGet() {
# Line 275 | Line 289 | public class ForkJoinTaskTest extends JS
289                          f.get();
290                          threadAssertTrue(f.number == 21);
291                          threadAssertTrue(f.isDone());
292 <                    } catch(Exception ex) {
293 <                        unexpectedException();
292 >                    } catch (Exception ex) {
293 >                        unexpectedException(ex);
294                      }
295                  }
296              };
297          mainPool.invoke(a);
298      }
299  
300 <    /**
300 >    /**
301       * timed get of a forked task returns when task completes
302       */
303      public void testForkTimedGet() {
# Line 292 | Line 306 | public class ForkJoinTaskTest extends JS
306                      try {
307                          AsyncFib f = new AsyncFib(8);
308                          f.fork();
309 <                        f.get(5L, TimeUnit.SECONDS);
309 >                        f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
310                          threadAssertTrue(f.number == 21);
311                          threadAssertTrue(f.isDone());
312 <                    } catch(Exception ex) {
313 <                        unexpectedException();
312 >                    } catch (Exception ex) {
313 >                        unexpectedException(ex);
314                      }
315                  }
316              };
317          mainPool.invoke(a);
318      }
319  
320 <    /**
320 >    /**
321       * timed get with null time unit throws NPE
322       */
323      public void testForkTimedGetNPE() {
# Line 313 | Line 327 | public class ForkJoinTaskTest extends JS
327                          AsyncFib f = new AsyncFib(8);
328                          f.fork();
329                          f.get(5L, null);
330 <                    } catch(NullPointerException success) {
331 <                    } catch(Exception ex) {
332 <                        unexpectedException();
330 >                        shouldThrow();
331 >                    } catch (NullPointerException success) {
332 >                    } catch (Exception ex) {
333 >                        unexpectedException(ex);
334                      }
335                  }
336              };
337          mainPool.invoke(a);
338      }
339  
340 <    /**
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 <    /**
340 >    /**
341       * quietlyJoin of a forked task returns when task completes
342       */
343      public void testForkQuietlyJoin() {
# Line 355 | Line 354 | public class ForkJoinTaskTest extends JS
354      }
355  
356  
357 <    /**
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 <    /**
357 >    /**
358       * helpQuiesce returns when tasks are complete.
359       * getQueuedTaskCount returns 0 when quiescent
360       */
# Line 391 | Line 373 | public class ForkJoinTaskTest extends JS
373      }
374  
375  
376 <    /**
376 >    /**
377       * invoke task throws exception when task completes abnormally
378       */
379      public void testAbnormalInvoke() {
# Line 401 | Line 383 | public class ForkJoinTaskTest extends JS
383                          FailingAsyncFib f = new FailingAsyncFib(8);
384                          f.invoke();
385                          shouldThrow();
386 <                    } catch(FJException success) {
386 >                    } catch (FJException success) {
387                      }
388                  }
389              };
390          mainPool.invoke(a);
391      }
392  
393 <    /**
394 <     * quietelyInvoke task returns when task completes abnormally
393 >    /**
394 >     * quietlyInvoke task returns when task completes abnormally
395       */
396      public void testAbnormalQuietlyInvoke() {
397          RecursiveAction a = new RecursiveAction() {
# Line 422 | Line 404 | public class ForkJoinTaskTest extends JS
404          mainPool.invoke(a);
405      }
406  
407 <    /**
407 >    /**
408       * join of a forked task throws exception when task completes abnormally
409       */
410      public void testAbnormalForkJoin() {
# Line 433 | Line 415 | public class ForkJoinTaskTest extends JS
415                          f.fork();
416                          f.join();
417                          shouldThrow();
418 <                    } catch(FJException success) {
418 >                    } catch (FJException success) {
419                      }
420                  }
421              };
422          mainPool.invoke(a);
423      }
424  
425 <    /**
425 >    /**
426       * get of a forked task throws exception when task completes abnormally
427       */
428      public void testAbnormalForkGet() {
# Line 451 | Line 433 | public class ForkJoinTaskTest extends JS
433                          f.fork();
434                          f.get();
435                          shouldThrow();
436 <                    } catch(Exception success) {
436 >                    } catch (ExecutionException success) {
437 >                    } catch (Exception ex) {
438 >                        unexpectedException(ex);
439                      }
440                  }
441              };
442          mainPool.invoke(a);
443      }
444  
445 <    /**
445 >    /**
446       * timed get of a forked task throws exception when task completes abnormally
447       */
448      public void testAbnormalForkTimedGet() {
# Line 467 | Line 451 | public class ForkJoinTaskTest extends JS
451                      try {
452                          FailingAsyncFib f = new FailingAsyncFib(8);
453                          f.fork();
454 <                        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();
454 >                        f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
455                          shouldThrow();
456 <                    } catch(FJException success) {
456 >                    } catch (ExecutionException success) {
457 >                    } catch (Exception ex) {
458 >                        unexpectedException(ex);
459                      }
460                  }
461              };
462          mainPool.invoke(a);
463      }
464  
465 <    /**
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 <    /**
465 >    /**
466       * quietlyJoin of a forked task returns when task completes abnormally
467       */
468      public void testAbnormalForkQuietlyJoin() {
# Line 532 | Line 479 | public class ForkJoinTaskTest extends JS
479          mainPool.invoke(a);
480      }
481  
482 <    /**
482 >    /**
483       * invoke task throws exception when task cancelled
484       */
485      public void testCancelledInvoke() {
# Line 543 | Line 490 | public class ForkJoinTaskTest extends JS
490                          f.cancel(true);
491                          f.invoke();
492                          shouldThrow();
493 <                    } catch(CancellationException success) {
493 >                    } catch (CancellationException success) {
494                      }
495                  }
496              };
497          mainPool.invoke(a);
498      }
499  
500 <    /**
500 >    /**
501       * join of a forked task throws exception when task cancelled
502       */
503      public void testCancelledForkJoin() {
# Line 562 | Line 509 | public class ForkJoinTaskTest extends JS
509                          f.fork();
510                          f.join();
511                          shouldThrow();
512 <                    } catch(CancellationException success) {
512 >                    } catch (CancellationException success) {
513                      }
514                  }
515              };
516          mainPool.invoke(a);
517      }
518  
519 <    /**
519 >    /**
520       * get of a forked task throws exception when task cancelled
521       */
522      public void testCancelledForkGet() {
# Line 581 | Line 528 | public class ForkJoinTaskTest extends JS
528                          f.fork();
529                          f.get();
530                          shouldThrow();
531 <                    } catch(Exception success) {
531 >                    } catch (CancellationException success) {
532 >                    } catch (Exception ex) {
533 >                        unexpectedException(ex);
534                      }
535                  }
536              };
537          mainPool.invoke(a);
538      }
539  
540 <    /**
540 >    /**
541       * timed get of a forked task throws exception when task cancelled
542       */
543      public void testCancelledForkTimedGet() {
# Line 598 | Line 547 | public class ForkJoinTaskTest extends JS
547                          AsyncFib f = new AsyncFib(8);
548                          f.cancel(true);
549                          f.fork();
550 <                        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();
550 >                        f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
551                          shouldThrow();
552 <                    } catch(CancellationException success) {
552 >                    } catch (CancellationException success) {
553 >                    } catch (Exception ex) {
554 >                        unexpectedException(ex);
555                      }
556                  }
557              };
558          mainPool.invoke(a);
559      }
560  
561 <    /**
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);
649 <    }
650 <
651 <    /**
561 >    /**
562       * quietlyJoin of a forked task returns when task cancelled
563       */
564      public void testCancelledForkQuietlyJoin() {
# Line 726 | Line 636 | public class ForkJoinTaskTest extends JS
636          a.invoke();
637      }
638  
639 <    /**
639 >    /**
640       * invoke task throws exception after invoking completeExceptionally
641       */
642      public void testCompleteExceptionally() {
# Line 737 | Line 647 | public class ForkJoinTaskTest extends JS
647                          f.completeExceptionally(new FJException());
648                          f.invoke();
649                          shouldThrow();
650 <                    } catch(FJException success) {
650 >                    } catch (FJException success) {
651                      }
652                  }
653              };
654          mainPool.invoke(a);
655      }
656  
657 <    /**
657 >    /**
658       * invokeAll(t1, t2) invokes all task arguments
659       */
660      public void testInvokeAll2() {
# Line 762 | Line 672 | public class ForkJoinTaskTest extends JS
672          mainPool.invoke(a);
673      }
674  
675 <    /**
675 >    /**
676       * invokeAll(tasks) with 1 argument invokes task
677       */
678      public void testInvokeAll1() {
# Line 777 | Line 687 | public class ForkJoinTaskTest extends JS
687          mainPool.invoke(a);
688      }
689  
690 <    /**
690 >    /**
691       * invokeAll(tasks) with > 2 argument invokes tasks
692       */
693      public void testInvokeAll3() {
# Line 798 | Line 708 | public class ForkJoinTaskTest extends JS
708          mainPool.invoke(a);
709      }
710  
711 <    /**
711 >    /**
712       * invokeAll(collection) invokes all tasks in the collection
713       */
714      public void testInvokeAllCollection() {
# Line 824 | Line 734 | public class ForkJoinTaskTest extends JS
734      }
735  
736  
737 <    /**
737 >    /**
738       * invokeAll(tasks) with any null task throws NPE
739       */
740      public void testInvokeAllNPE() {
# Line 843 | Line 753 | public class ForkJoinTaskTest extends JS
753          mainPool.invoke(a);
754      }
755  
756 <    /**
756 >    /**
757       * invokeAll(t1, t2) throw exception if any task does
758       */
759      public void testAbnormalInvokeAll2() {
# Line 854 | Line 764 | public class ForkJoinTaskTest extends JS
764                          FailingAsyncFib g = new FailingAsyncFib(9);
765                          invokeAll(f, g);
766                          shouldThrow();
767 <                    } catch(FJException success) {
767 >                    } catch (FJException success) {
768                      }
769                  }
770              };
771          mainPool.invoke(a);
772      }
773  
774 <    /**
774 >    /**
775       * invokeAll(tasks) with 1 argument throws exception if task does
776       */
777      public void testAbnormalInvokeAll1() {
# Line 871 | Line 781 | public class ForkJoinTaskTest extends JS
781                          FailingAsyncFib g = new FailingAsyncFib(9);
782                          invokeAll(g);
783                          shouldThrow();
784 <                    } catch(FJException success) {
784 >                    } catch (FJException success) {
785                      }
786                  }
787              };
788          mainPool.invoke(a);
789      }
790  
791 <    /**
791 >    /**
792       * invokeAll(tasks) with > 2 argument throws exception if any task does
793       */
794      public void testAbnormalInvokeAll3() {
# Line 890 | Line 800 | public class ForkJoinTaskTest extends JS
800                          AsyncFib h = new AsyncFib(7);
801                          invokeAll(f, g, h);
802                          shouldThrow();
803 <                    } catch(FJException success) {
803 >                    } catch (FJException success) {
804                      }
805                  }
806              };
807          mainPool.invoke(a);
808      }
809  
810 <    /**
810 >    /**
811       * invokeAll(collection)  throws exception if any task does
812       */
813      public void testAbnormalInvokeAllCollection() {
# Line 913 | Line 823 | public class ForkJoinTaskTest extends JS
823                          set.add(h);
824                          invokeAll(set);
825                          shouldThrow();
826 <                    } catch(FJException success) {
826 >                    } catch (FJException success) {
827                      }
828                  }
829              };
830          mainPool.invoke(a);
831      }
832  
833 <    /**
833 >    /**
834       * tryUnfork returns true for most recent unexecuted task,
835       * and suppresses execution
836       */
# Line 940 | Line 850 | public class ForkJoinTaskTest extends JS
850          singletonPool.invoke(a);
851      }
852  
853 <    /**
853 >    /**
854       * getSurplusQueuedTaskCount returns > 0 when
855       * there are more tasks than threads
856       */
# Line 960 | Line 870 | public class ForkJoinTaskTest extends JS
870          singletonPool.invoke(a);
871      }
872  
873 <    /**
873 >    /**
874       * peekNextLocalTask returns most recent unexecuted task.
875       */
876      public void testPeekNextLocalTask() {
# Line 979 | Line 889 | public class ForkJoinTaskTest extends JS
889          singletonPool.invoke(a);
890      }
891  
892 <    /**
892 >    /**
893       * pollNextLocalTask returns most recent unexecuted task
894       * without executing it
895       */
# Line 998 | Line 908 | public class ForkJoinTaskTest extends JS
908          singletonPool.invoke(a);
909      }
910  
911 <    /**
911 >    /**
912       * pollTask returns an unexecuted task
913       * without executing it
914       */
# Line 1018 | Line 928 | public class ForkJoinTaskTest extends JS
928          singletonPool.invoke(a);
929      }
930  
931 <    /**
931 >    /**
932       * peekNextLocalTask returns least recent unexecuted task in async mode
933       */
934      public void testPeekNextLocalTaskAsync() {
# Line 1037 | Line 947 | public class ForkJoinTaskTest extends JS
947          asyncSingletonPool.invoke(a);
948      }
949  
950 <    /**
950 >    /**
951       * pollNextLocalTask returns least recent unexecuted task
952       * without executing it, in async mode
953       */
# Line 1057 | Line 967 | public class ForkJoinTaskTest extends JS
967          asyncSingletonPool.invoke(a);
968      }
969  
970 <    /**
970 >    /**
971       * pollTask returns an unexecuted task
972       * without executing it, in async mode
973       */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines