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.5 by jsr166, Sat Nov 21 02:07:26 2009 UTC

# Line 11 | Line 11 | import java.util.*;
11   public class ForkJoinTaskTest extends JSR166TestCase {
12  
13      public static void main(String[] args) {
14 <        junit.textui.TestRunner.run (suite());  
14 >        junit.textui.TestRunner.run (suite());
15      }
16      public static Test suite() {
17 <        return new TestSuite(ForkJoinTaskTest.class);
17 >        return new TestSuite(ForkJoinTaskTest.class);
18      }
19  
20      /**
21       * Testing coverage notes:
22 <     *
22 >     *
23       * To test extension methods and overrides, most tests use
24       * BinaryAsyncAction extension class that processes joins
25       * differently than supplied Recursive forms.
26 <     */    
26 >     */
27  
28      static final ForkJoinPool mainPool = new ForkJoinPool();
29      static final ForkJoinPool singletonPool = new ForkJoinPool(1);
# Line 40 | Line 40 | public class ForkJoinTaskTest extends JS
40          private volatile int controlState;
41  
42          static final AtomicIntegerFieldUpdater<BinaryAsyncAction> controlStateUpdater =
43 <            AtomicIntegerFieldUpdater.newUpdater(BinaryAsyncAction.class,
43 >            AtomicIntegerFieldUpdater.newUpdater(BinaryAsyncAction.class,
44                                                   "controlState");
45  
46          private BinaryAsyncAction parent;
# Line 92 | Line 92 | public class ForkJoinTaskTest extends JS
92                      break;
93                  try {
94                      p.onComplete(a, s);
95 <                } catch(Throwable rex) {
95 >                } catch (Throwable rex) {
96                      p.completeExceptionally(rex);
97                      return;
98                  }
# Line 129 | Line 129 | public class ForkJoinTaskTest extends JS
129              return controlState;
130          }
131  
132 <        protected final boolean compareAndSetControlState(int expect,
132 >        protected final boolean compareAndSetControlState(int expect,
133                                                            int update) {
134              return controlStateUpdater.compareAndSet(this, expect, update);
135          }
# Line 150 | Line 150 | public class ForkJoinTaskTest extends JS
150  
151      static final class AsyncFib  extends BinaryAsyncAction {
152          int number;
153 <        public AsyncFib(int n) {
153 >        public AsyncFib(int n) {
154              this.number = n;
155          }
156 <        
156 >
157          public final boolean exec() {
158              AsyncFib f = this;
159              int n = f.number;
# Line 179 | Line 179 | public class ForkJoinTaskTest extends JS
179  
180      static final class FailingAsyncFib  extends BinaryAsyncAction {
181          int number;
182 <        public FailingAsyncFib(int n) {
182 >        public FailingAsyncFib(int n) {
183              this.number = n;
184          }
185 <        
185 >
186          public final boolean exec() {
187              FailingAsyncFib f = this;
188              int n = f.number;
# Line 205 | Line 205 | public class ForkJoinTaskTest extends JS
205          }
206      }
207  
208 <    /**
208 >    /**
209       * invoke returns when task completes normally.
210       * isCompletedAbnormally and isCancelled return false for normally
211       * completed tasks. getRawResult of a RecursiveAction returns null;
212 <     *
212 >     *
213       */
214      public void testInvoke() {
215          RecursiveAction a = new RecursiveAction() {
# Line 226 | Line 226 | public class ForkJoinTaskTest extends JS
226          mainPool.invoke(a);
227      }
228  
229 <    /**
229 >    /**
230       * quietlyInvoke task returns when task completes normally.
231       * isCompletedAbnormally and isCancelled return false for normally
232       * completed tasks
# Line 246 | Line 246 | public class ForkJoinTaskTest extends JS
246          mainPool.invoke(a);
247      }
248  
249 <    /**
249 >    /**
250       * join of a forked task returns when task completes
251       */
252      public void testForkJoin() {
# Line 263 | Line 263 | public class ForkJoinTaskTest extends JS
263          mainPool.invoke(a);
264      }
265  
266 <    /**
266 >    /**
267       * get of a forked task returns when task completes
268       */
269      public void testForkGet() {
# Line 275 | Line 275 | public class ForkJoinTaskTest extends JS
275                          f.get();
276                          threadAssertTrue(f.number == 21);
277                          threadAssertTrue(f.isDone());
278 <                    } catch(Exception ex) {
279 <                        unexpectedException();
278 >                    } catch (Exception ex) {
279 >                        unexpectedException(ex);
280                      }
281                  }
282              };
283          mainPool.invoke(a);
284      }
285  
286 <    /**
286 >    /**
287       * timed get of a forked task returns when task completes
288       */
289      public void testForkTimedGet() {
# Line 295 | Line 295 | public class ForkJoinTaskTest extends JS
295                          f.get(5L, TimeUnit.SECONDS);
296                          threadAssertTrue(f.number == 21);
297                          threadAssertTrue(f.isDone());
298 <                    } catch(Exception ex) {
299 <                        unexpectedException();
298 >                    } catch (Exception ex) {
299 >                        unexpectedException(ex);
300                      }
301                  }
302              };
303          mainPool.invoke(a);
304      }
305  
306 <    /**
306 >    /**
307       * timed get with null time unit throws NPE
308       */
309      public void testForkTimedGetNPE() {
# Line 313 | Line 313 | public class ForkJoinTaskTest extends JS
313                          AsyncFib f = new AsyncFib(8);
314                          f.fork();
315                          f.get(5L, null);
316 <                    } catch(NullPointerException success) {
317 <                    } catch(Exception ex) {
318 <                        unexpectedException();
316 >                        shouldThrow();
317 >                    } catch (NullPointerException success) {
318 >                    } catch (Exception ex) {
319 >                        unexpectedException(ex);
320                      }
321                  }
322              };
323          mainPool.invoke(a);
324      }
325  
326 <    /**
326 >    /**
327       * helpJoin of a forked task returns when task completes
328       */
329      public void testForkHelpJoin() {
# Line 338 | Line 339 | public class ForkJoinTaskTest extends JS
339          mainPool.invoke(a);
340      }
341  
342 <    /**
342 >    /**
343       * quietlyJoin of a forked task returns when task completes
344       */
345      public void testForkQuietlyJoin() {
# Line 355 | Line 356 | public class ForkJoinTaskTest extends JS
356      }
357  
358  
359 <    /**
359 >    /**
360       * quietlyHelpJoin of a forked task returns when task completes
361       */
362      public void testForkQuietlyHelpJoin() {
# Line 372 | Line 373 | public class ForkJoinTaskTest extends JS
373      }
374  
375  
376 <    /**
376 >    /**
377       * helpQuiesce returns when tasks are complete.
378       * getQueuedTaskCount returns 0 when quiescent
379       */
# Line 391 | Line 392 | public class ForkJoinTaskTest extends JS
392      }
393  
394  
395 <    /**
395 >    /**
396       * invoke task throws exception when task completes abnormally
397       */
398      public void testAbnormalInvoke() {
# Line 401 | Line 402 | public class ForkJoinTaskTest extends JS
402                          FailingAsyncFib f = new FailingAsyncFib(8);
403                          f.invoke();
404                          shouldThrow();
405 <                    } catch(FJException success) {
405 >                    } catch (FJException success) {
406                      }
407                  }
408              };
409          mainPool.invoke(a);
410      }
411  
412 <    /**
413 <     * quietelyInvoke task returns when task completes abnormally
412 >    /**
413 >     * quietlyInvoke task returns when task completes abnormally
414       */
415      public void testAbnormalQuietlyInvoke() {
416          RecursiveAction a = new RecursiveAction() {
# Line 422 | Line 423 | public class ForkJoinTaskTest extends JS
423          mainPool.invoke(a);
424      }
425  
426 <    /**
426 >    /**
427       * join of a forked task throws exception when task completes abnormally
428       */
429      public void testAbnormalForkJoin() {
# Line 433 | Line 434 | public class ForkJoinTaskTest extends JS
434                          f.fork();
435                          f.join();
436                          shouldThrow();
437 <                    } catch(FJException success) {
437 >                    } catch (FJException success) {
438                      }
439                  }
440              };
441          mainPool.invoke(a);
442      }
443  
444 <    /**
444 >    /**
445       * get of a forked task throws exception when task completes abnormally
446       */
447      public void testAbnormalForkGet() {
# Line 451 | Line 452 | public class ForkJoinTaskTest extends JS
452                          f.fork();
453                          f.get();
454                          shouldThrow();
455 <                    } catch(Exception success) {
455 >                    } catch (ExecutionException success) {
456 >                    } catch (Exception ex) {
457 >                        unexpectedException(ex);
458                      }
459                  }
460              };
461          mainPool.invoke(a);
462      }
463  
464 <    /**
464 >    /**
465       * timed get of a forked task throws exception when task completes abnormally
466       */
467      public void testAbnormalForkTimedGet() {
# Line 469 | Line 472 | public class ForkJoinTaskTest extends JS
472                          f.fork();
473                          f.get(5L, TimeUnit.SECONDS);
474                          shouldThrow();
475 <                    } catch(Exception success) {
475 >                    } catch (ExecutionException success) {
476 >                    } catch (Exception ex) {
477 >                        unexpectedException(ex);
478                      }
479                  }
480              };
481          mainPool.invoke(a);
482      }
483  
484 <    /**
484 >    /**
485       * join of a forked task throws exception when task completes abnormally
486       */
487      public void testAbnormalForkHelpJoin() {
# Line 487 | Line 492 | public class ForkJoinTaskTest extends JS
492                          f.fork();
493                          f.helpJoin();
494                          shouldThrow();
495 <                    } catch(FJException success) {
495 >                    } catch (FJException success) {
496                      }
497                  }
498              };
499          mainPool.invoke(a);
500      }
501  
502 <    /**
502 >    /**
503       * quietlyHelpJoin of a forked task returns when task completes abnormally.
504       * getException of failed task returns its exception.
505       * isCompletedAbnormally of a failed task returns true.
# Line 515 | Line 520 | public class ForkJoinTaskTest extends JS
520          mainPool.invoke(a);
521      }
522  
523 <    /**
523 >    /**
524       * quietlyJoin of a forked task returns when task completes abnormally
525       */
526      public void testAbnormalForkQuietlyJoin() {
# Line 532 | Line 537 | public class ForkJoinTaskTest extends JS
537          mainPool.invoke(a);
538      }
539  
540 <    /**
540 >    /**
541       * invoke task throws exception when task cancelled
542       */
543      public void testCancelledInvoke() {
# Line 543 | Line 548 | public class ForkJoinTaskTest extends JS
548                          f.cancel(true);
549                          f.invoke();
550                          shouldThrow();
551 <                    } catch(CancellationException success) {
551 >                    } catch (CancellationException success) {
552                      }
553                  }
554              };
555          mainPool.invoke(a);
556      }
557  
558 <    /**
558 >    /**
559       * join of a forked task throws exception when task cancelled
560       */
561      public void testCancelledForkJoin() {
# Line 562 | Line 567 | public class ForkJoinTaskTest extends JS
567                          f.fork();
568                          f.join();
569                          shouldThrow();
570 <                    } catch(CancellationException success) {
570 >                    } catch (CancellationException success) {
571                      }
572                  }
573              };
574          mainPool.invoke(a);
575      }
576  
577 <    /**
577 >    /**
578       * get of a forked task throws exception when task cancelled
579       */
580      public void testCancelledForkGet() {
# Line 581 | Line 586 | public class ForkJoinTaskTest extends JS
586                          f.fork();
587                          f.get();
588                          shouldThrow();
589 <                    } catch(Exception success) {
589 >                    } catch (CancellationException success) {
590 >                    } catch (Exception ex) {
591 >                        unexpectedException(ex);
592                      }
593                  }
594              };
595          mainPool.invoke(a);
596      }
597  
598 <    /**
598 >    /**
599       * timed get of a forked task throws exception when task cancelled
600       */
601      public void testCancelledForkTimedGet() {
# Line 600 | Line 607 | public class ForkJoinTaskTest extends JS
607                          f.fork();
608                          f.get(5L, TimeUnit.SECONDS);
609                          shouldThrow();
610 <                    } catch(Exception success) {
610 >                    } catch (CancellationException success) {
611 >                    } catch (Exception ex) {
612 >                        unexpectedException(ex);
613                      }
614                  }
615              };
616          mainPool.invoke(a);
617      }
618  
619 <    /**
619 >    /**
620       * join of a forked task throws exception when task cancelled
621       */
622      public void testCancelledForkHelpJoin() {
# Line 619 | Line 628 | public class ForkJoinTaskTest extends JS
628                          f.fork();
629                          f.helpJoin();
630                          shouldThrow();
631 <                    } catch(CancellationException success) {
631 >                    } catch (CancellationException success) {
632                      }
633                  }
634              };
635          mainPool.invoke(a);
636      }
637  
638 <    /**
638 >    /**
639       * quietlyHelpJoin of a forked task returns when task cancelled.
640       * getException of cancelled task returns its exception.
641       * isCompletedAbnormally of a cancelled task returns true.
# Line 648 | Line 657 | public class ForkJoinTaskTest extends JS
657          mainPool.invoke(a);
658      }
659  
660 <    /**
660 >    /**
661       * quietlyJoin of a forked task returns when task cancelled
662       */
663      public void testCancelledForkQuietlyJoin() {
# Line 726 | Line 735 | public class ForkJoinTaskTest extends JS
735          a.invoke();
736      }
737  
738 <    /**
738 >    /**
739       * invoke task throws exception after invoking completeExceptionally
740       */
741      public void testCompleteExceptionally() {
# Line 737 | Line 746 | public class ForkJoinTaskTest extends JS
746                          f.completeExceptionally(new FJException());
747                          f.invoke();
748                          shouldThrow();
749 <                    } catch(FJException success) {
749 >                    } catch (FJException success) {
750                      }
751                  }
752              };
753          mainPool.invoke(a);
754      }
755  
756 <    /**
756 >    /**
757       * invokeAll(t1, t2) invokes all task arguments
758       */
759      public void testInvokeAll2() {
# Line 762 | Line 771 | public class ForkJoinTaskTest extends JS
771          mainPool.invoke(a);
772      }
773  
774 <    /**
774 >    /**
775       * invokeAll(tasks) with 1 argument invokes task
776       */
777      public void testInvokeAll1() {
# Line 777 | Line 786 | public class ForkJoinTaskTest extends JS
786          mainPool.invoke(a);
787      }
788  
789 <    /**
789 >    /**
790       * invokeAll(tasks) with > 2 argument invokes tasks
791       */
792      public void testInvokeAll3() {
# Line 798 | Line 807 | public class ForkJoinTaskTest extends JS
807          mainPool.invoke(a);
808      }
809  
810 <    /**
810 >    /**
811       * invokeAll(collection) invokes all tasks in the collection
812       */
813      public void testInvokeAllCollection() {
# Line 824 | Line 833 | public class ForkJoinTaskTest extends JS
833      }
834  
835  
836 <    /**
836 >    /**
837       * invokeAll(tasks) with any null task throws NPE
838       */
839      public void testInvokeAllNPE() {
# Line 843 | Line 852 | public class ForkJoinTaskTest extends JS
852          mainPool.invoke(a);
853      }
854  
855 <    /**
855 >    /**
856       * invokeAll(t1, t2) throw exception if any task does
857       */
858      public void testAbnormalInvokeAll2() {
# Line 854 | Line 863 | public class ForkJoinTaskTest extends JS
863                          FailingAsyncFib g = new FailingAsyncFib(9);
864                          invokeAll(f, g);
865                          shouldThrow();
866 <                    } catch(FJException success) {
866 >                    } catch (FJException success) {
867                      }
868                  }
869              };
870          mainPool.invoke(a);
871      }
872  
873 <    /**
873 >    /**
874       * invokeAll(tasks) with 1 argument throws exception if task does
875       */
876      public void testAbnormalInvokeAll1() {
# Line 871 | Line 880 | public class ForkJoinTaskTest extends JS
880                          FailingAsyncFib g = new FailingAsyncFib(9);
881                          invokeAll(g);
882                          shouldThrow();
883 <                    } catch(FJException success) {
883 >                    } catch (FJException success) {
884                      }
885                  }
886              };
887          mainPool.invoke(a);
888      }
889  
890 <    /**
890 >    /**
891       * invokeAll(tasks) with > 2 argument throws exception if any task does
892       */
893      public void testAbnormalInvokeAll3() {
# Line 890 | Line 899 | public class ForkJoinTaskTest extends JS
899                          AsyncFib h = new AsyncFib(7);
900                          invokeAll(f, g, h);
901                          shouldThrow();
902 <                    } catch(FJException success) {
902 >                    } catch (FJException success) {
903                      }
904                  }
905              };
906          mainPool.invoke(a);
907      }
908  
909 <    /**
909 >    /**
910       * invokeAll(collection)  throws exception if any task does
911       */
912      public void testAbnormalInvokeAllCollection() {
# Line 913 | Line 922 | public class ForkJoinTaskTest extends JS
922                          set.add(h);
923                          invokeAll(set);
924                          shouldThrow();
925 <                    } catch(FJException success) {
925 >                    } catch (FJException success) {
926                      }
927                  }
928              };
929          mainPool.invoke(a);
930      }
931  
932 <    /**
932 >    /**
933       * tryUnfork returns true for most recent unexecuted task,
934       * and suppresses execution
935       */
# Line 940 | Line 949 | public class ForkJoinTaskTest extends JS
949          singletonPool.invoke(a);
950      }
951  
952 <    /**
952 >    /**
953       * getSurplusQueuedTaskCount returns > 0 when
954       * there are more tasks than threads
955       */
# Line 960 | Line 969 | public class ForkJoinTaskTest extends JS
969          singletonPool.invoke(a);
970      }
971  
972 <    /**
972 >    /**
973       * peekNextLocalTask returns most recent unexecuted task.
974       */
975      public void testPeekNextLocalTask() {
# Line 979 | Line 988 | public class ForkJoinTaskTest extends JS
988          singletonPool.invoke(a);
989      }
990  
991 <    /**
991 >    /**
992       * pollNextLocalTask returns most recent unexecuted task
993       * without executing it
994       */
# Line 998 | Line 1007 | public class ForkJoinTaskTest extends JS
1007          singletonPool.invoke(a);
1008      }
1009  
1010 <    /**
1010 >    /**
1011       * pollTask returns an unexecuted task
1012       * without executing it
1013       */
# Line 1018 | Line 1027 | public class ForkJoinTaskTest extends JS
1027          singletonPool.invoke(a);
1028      }
1029  
1030 <    /**
1030 >    /**
1031       * peekNextLocalTask returns least recent unexecuted task in async mode
1032       */
1033      public void testPeekNextLocalTaskAsync() {
# Line 1037 | Line 1046 | public class ForkJoinTaskTest extends JS
1046          asyncSingletonPool.invoke(a);
1047      }
1048  
1049 <    /**
1049 >    /**
1050       * pollNextLocalTask returns least recent unexecuted task
1051       * without executing it, in async mode
1052       */
# Line 1057 | Line 1066 | public class ForkJoinTaskTest extends JS
1066          asyncSingletonPool.invoke(a);
1067      }
1068  
1069 <    /**
1069 >    /**
1070       * pollTask returns an unexecuted task
1071       * without executing it, in async mode
1072       */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines