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.9 by jsr166, Wed Aug 25 00:07:03 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;
227 <     *
227 >     *
228       */
229      public void testInvoke() {
230          RecursiveAction a = new RecursiveAction() {
# Line 226 | Line 241 | public class ForkJoinTaskTest extends JS
241          mainPool.invoke(a);
242      }
243  
244 <    /**
244 >    /**
245       * quietlyInvoke task returns when task completes normally.
246       * isCompletedAbnormally and isCancelled return false for normally
247       * completed tasks
# Line 246 | Line 261 | public class ForkJoinTaskTest extends JS
261          mainPool.invoke(a);
262      }
263  
264 <    /**
264 >    /**
265       * join of a forked task returns when task completes
266       */
267      public void testForkJoin() {
# Line 263 | Line 278 | public class ForkJoinTaskTest extends JS
278          mainPool.invoke(a);
279      }
280  
281 <    /**
281 >    /**
282       * get of a forked task returns when task completes
283       */
284      public void testForkGet() {
# Line 275 | Line 290 | public class ForkJoinTaskTest extends JS
290                          f.get();
291                          threadAssertTrue(f.number == 21);
292                          threadAssertTrue(f.isDone());
293 <                    } catch(Exception ex) {
294 <                        unexpectedException();
293 >                    } catch (Exception ex) {
294 >                        unexpectedException(ex);
295                      }
296                  }
297              };
298          mainPool.invoke(a);
299      }
300  
301 <    /**
301 >    /**
302       * timed get of a forked task returns when task completes
303       */
304      public void testForkTimedGet() {
# 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();
313 >                    } catch (Exception ex) {
314 >                        unexpectedException(ex);
315                      }
316                  }
317              };
318          mainPool.invoke(a);
319      }
320  
321 <    /**
321 >    /**
322       * timed get with null time unit throws NPE
323       */
324      public void testForkTimedGetNPE() {
# Line 313 | Line 328 | public class ForkJoinTaskTest extends JS
328                          AsyncFib f = new AsyncFib(8);
329                          f.fork();
330                          f.get(5L, null);
331 <                    } catch(NullPointerException success) {
332 <                    } catch(Exception ex) {
333 <                        unexpectedException();
331 >                        shouldThrow();
332 >                    } catch (NullPointerException success) {
333 >                    } catch (Exception ex) {
334 >                        unexpectedException(ex);
335                      }
336                  }
337              };
338          mainPool.invoke(a);
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 <    /**
341 >    /**
342       * quietlyJoin of a forked task returns when task completes
343       */
344      public void testForkQuietlyJoin() {
# Line 355 | Line 355 | public class ForkJoinTaskTest extends JS
355      }
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 <    /**
358 >    /**
359       * helpQuiesce returns when tasks are complete.
360       * getQueuedTaskCount returns 0 when quiescent
361       */
# Line 391 | Line 374 | public class ForkJoinTaskTest extends JS
374      }
375  
376  
377 <    /**
377 >    /**
378       * invoke task throws exception when task completes abnormally
379       */
380      public void testAbnormalInvoke() {
# Line 401 | Line 384 | public class ForkJoinTaskTest extends JS
384                          FailingAsyncFib f = new FailingAsyncFib(8);
385                          f.invoke();
386                          shouldThrow();
387 <                    } catch(FJException success) {
387 >                    } catch (FJException success) {
388                      }
389                  }
390              };
391          mainPool.invoke(a);
392      }
393  
394 <    /**
395 <     * quietelyInvoke task returns when task completes abnormally
394 >    /**
395 >     * quietlyInvoke task returns when task completes abnormally
396       */
397      public void testAbnormalQuietlyInvoke() {
398          RecursiveAction a = new RecursiveAction() {
# Line 422 | Line 405 | public class ForkJoinTaskTest extends JS
405          mainPool.invoke(a);
406      }
407  
408 <    /**
408 >    /**
409       * join of a forked task throws exception when task completes abnormally
410       */
411      public void testAbnormalForkJoin() {
# Line 433 | Line 416 | public class ForkJoinTaskTest extends JS
416                          f.fork();
417                          f.join();
418                          shouldThrow();
419 <                    } catch(FJException success) {
419 >                    } catch (FJException success) {
420                      }
421                  }
422              };
423          mainPool.invoke(a);
424      }
425  
426 <    /**
426 >    /**
427       * get of a forked task throws exception when task completes abnormally
428       */
429      public void testAbnormalForkGet() {
# 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              };
443          mainPool.invoke(a);
444      }
445  
446 <    /**
446 >    /**
447       * timed get of a forked task throws exception when task completes abnormally
448       */
449      public void testAbnormalForkTimedGet() {
# 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              };
463          mainPool.invoke(a);
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 <    /**
466 >    /**
467       * quietlyJoin of a forked task returns when task completes abnormally
468       */
469      public void testAbnormalForkQuietlyJoin() {
# Line 532 | Line 480 | public class ForkJoinTaskTest extends JS
480          mainPool.invoke(a);
481      }
482  
483 <    /**
483 >    /**
484       * invoke task throws exception when task cancelled
485       */
486      public void testCancelledInvoke() {
# Line 543 | Line 491 | public class ForkJoinTaskTest extends JS
491                          f.cancel(true);
492                          f.invoke();
493                          shouldThrow();
494 <                    } catch(CancellationException success) {
494 >                    } catch (CancellationException success) {
495                      }
496                  }
497              };
498          mainPool.invoke(a);
499      }
500  
501 <    /**
501 >    /**
502       * join of a forked task throws exception when task cancelled
503       */
504      public void testCancelledForkJoin() {
# Line 562 | Line 510 | public class ForkJoinTaskTest extends JS
510                          f.fork();
511                          f.join();
512                          shouldThrow();
513 <                    } catch(CancellationException success) {
513 >                    } catch (CancellationException success) {
514                      }
515                  }
516              };
517          mainPool.invoke(a);
518      }
519  
520 <    /**
520 >    /**
521       * get of a forked task throws exception when task cancelled
522       */
523      public void testCancelledForkGet() {
# 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              };
538          mainPool.invoke(a);
539      }
540  
541 <    /**
541 >    /**
542       * timed get of a forked task throws exception when task cancelled
543       */
544      public void testCancelledForkTimedGet() {
# 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) {
553 >                    } catch (CancellationException success) {
554 >                    } catch (Exception ex) {
555 >                        unexpectedException(ex);
556                      }
557                  }
558              };
559          mainPool.invoke(a);
560      }
561  
562 <    /**
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 <    /**
562 >    /**
563       * quietlyJoin of a forked task returns when task cancelled
564       */
565      public void testCancelledForkQuietlyJoin() {
# Line 726 | Line 637 | public class ForkJoinTaskTest extends JS
637          a.invoke();
638      }
639  
640 <    /**
640 >    /**
641       * invoke task throws exception after invoking completeExceptionally
642       */
643      public void testCompleteExceptionally() {
# Line 737 | Line 648 | public class ForkJoinTaskTest extends JS
648                          f.completeExceptionally(new FJException());
649                          f.invoke();
650                          shouldThrow();
651 <                    } catch(FJException success) {
651 >                    } catch (FJException success) {
652                      }
653                  }
654              };
655          mainPool.invoke(a);
656      }
657  
658 <    /**
658 >    /**
659       * invokeAll(t1, t2) invokes all task arguments
660       */
661      public void testInvokeAll2() {
# Line 762 | Line 673 | public class ForkJoinTaskTest extends JS
673          mainPool.invoke(a);
674      }
675  
676 <    /**
676 >    /**
677       * invokeAll(tasks) with 1 argument invokes task
678       */
679      public void testInvokeAll1() {
# Line 777 | Line 688 | public class ForkJoinTaskTest extends JS
688          mainPool.invoke(a);
689      }
690  
691 <    /**
691 >    /**
692       * invokeAll(tasks) with > 2 argument invokes tasks
693       */
694      public void testInvokeAll3() {
# Line 798 | Line 709 | public class ForkJoinTaskTest extends JS
709          mainPool.invoke(a);
710      }
711  
712 <    /**
712 >    /**
713       * invokeAll(collection) invokes all tasks in the collection
714       */
715      public void testInvokeAllCollection() {
# Line 824 | Line 735 | public class ForkJoinTaskTest extends JS
735      }
736  
737  
738 <    /**
738 >    /**
739       * invokeAll(tasks) with any null task throws NPE
740       */
741      public void testInvokeAllNPE() {
# Line 843 | Line 754 | public class ForkJoinTaskTest extends JS
754          mainPool.invoke(a);
755      }
756  
757 <    /**
757 >    /**
758       * invokeAll(t1, t2) throw exception if any task does
759       */
760      public void testAbnormalInvokeAll2() {
# Line 854 | Line 765 | public class ForkJoinTaskTest extends JS
765                          FailingAsyncFib g = new FailingAsyncFib(9);
766                          invokeAll(f, g);
767                          shouldThrow();
768 <                    } catch(FJException success) {
768 >                    } catch (FJException success) {
769                      }
770                  }
771              };
772          mainPool.invoke(a);
773      }
774  
775 <    /**
775 >    /**
776       * invokeAll(tasks) with 1 argument throws exception if task does
777       */
778      public void testAbnormalInvokeAll1() {
# Line 871 | Line 782 | public class ForkJoinTaskTest extends JS
782                          FailingAsyncFib g = new FailingAsyncFib(9);
783                          invokeAll(g);
784                          shouldThrow();
785 <                    } catch(FJException success) {
785 >                    } catch (FJException success) {
786                      }
787                  }
788              };
789          mainPool.invoke(a);
790      }
791  
792 <    /**
792 >    /**
793       * invokeAll(tasks) with > 2 argument throws exception if any task does
794       */
795      public void testAbnormalInvokeAll3() {
# Line 890 | Line 801 | public class ForkJoinTaskTest extends JS
801                          AsyncFib h = new AsyncFib(7);
802                          invokeAll(f, g, h);
803                          shouldThrow();
804 <                    } catch(FJException success) {
804 >                    } catch (FJException success) {
805                      }
806                  }
807              };
808          mainPool.invoke(a);
809      }
810  
811 <    /**
811 >    /**
812       * invokeAll(collection)  throws exception if any task does
813       */
814      public void testAbnormalInvokeAllCollection() {
# Line 913 | Line 824 | public class ForkJoinTaskTest extends JS
824                          set.add(h);
825                          invokeAll(set);
826                          shouldThrow();
827 <                    } catch(FJException success) {
827 >                    } catch (FJException success) {
828                      }
829                  }
830              };
831          mainPool.invoke(a);
832      }
833  
834 <    /**
834 >    /**
835       * tryUnfork returns true for most recent unexecuted task,
836       * and suppresses execution
837       */
# Line 940 | Line 851 | public class ForkJoinTaskTest extends JS
851          singletonPool.invoke(a);
852      }
853  
854 <    /**
854 >    /**
855       * getSurplusQueuedTaskCount returns > 0 when
856       * there are more tasks than threads
857       */
# Line 960 | Line 871 | public class ForkJoinTaskTest extends JS
871          singletonPool.invoke(a);
872      }
873  
874 <    /**
874 >    /**
875       * peekNextLocalTask returns most recent unexecuted task.
876       */
877      public void testPeekNextLocalTask() {
# Line 979 | Line 890 | public class ForkJoinTaskTest extends JS
890          singletonPool.invoke(a);
891      }
892  
893 <    /**
893 >    /**
894       * pollNextLocalTask returns most recent unexecuted task
895       * without executing it
896       */
# Line 998 | Line 909 | public class ForkJoinTaskTest extends JS
909          singletonPool.invoke(a);
910      }
911  
912 <    /**
912 >    /**
913       * pollTask returns an unexecuted task
914       * without executing it
915       */
# Line 1018 | Line 929 | public class ForkJoinTaskTest extends JS
929          singletonPool.invoke(a);
930      }
931  
932 <    /**
932 >    /**
933       * peekNextLocalTask returns least recent unexecuted task in async mode
934       */
935      public void testPeekNextLocalTaskAsync() {
# Line 1037 | Line 948 | public class ForkJoinTaskTest extends JS
948          asyncSingletonPool.invoke(a);
949      }
950  
951 <    /**
951 >    /**
952       * pollNextLocalTask returns least recent unexecuted task
953       * without executing it, in async mode
954       */
# Line 1057 | Line 968 | public class ForkJoinTaskTest extends JS
968          asyncSingletonPool.invoke(a);
969      }
970  
971 <    /**
971 >    /**
972       * pollTask returns an unexecuted task
973       * without executing it, in async mode
974       */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines