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

Comparing jsr166/src/test/tck/RecursiveActionTest.java (file contents):
Revision 1.8 by jsr166, Tue Nov 17 12:31:23 2009 UTC vs.
Revision 1.15 by jsr166, Mon Sep 13 20:48:58 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 junit.framework.*;
7 import java.util.concurrent.*;
8 import java.util.*;
6  
7 + import junit.framework.*;
8 + import java.util.concurrent.CancellationException;
9 + import java.util.concurrent.ExecutionException;
10 + import java.util.concurrent.ForkJoinPool;
11 + import java.util.concurrent.ForkJoinWorkerThread;
12 + import java.util.concurrent.RecursiveAction;
13 + import java.util.concurrent.TimeUnit;
14 + import java.util.HashSet;
15  
16   public class RecursiveActionTest extends JSR166TestCase {
17  
18      public static void main(String[] args) {
19 <        junit.textui.TestRunner.run (suite());
19 >        junit.textui.TestRunner.run(suite());
20      }
21 +
22      public static Test suite() {
23 <        return new TestSuite(RecursiveActionTest.class);
23 >        return new TestSuite(RecursiveActionTest.class);
24 >    }
25 >
26 >    private static ForkJoinPool mainPool() {
27 >        return new ForkJoinPool();
28 >    }
29 >
30 >    private static ForkJoinPool singletonPool() {
31 >        return new ForkJoinPool(1);
32 >    }
33 >
34 >    private static ForkJoinPool asyncSingletonPool() {
35 >        return new ForkJoinPool(1,
36 >                                ForkJoinPool.defaultForkJoinWorkerThreadFactory,
37 >                                null, true);
38      }
39  
40 <    static final ForkJoinPool mainPool = new ForkJoinPool();
41 <    static final ForkJoinPool singletonPool = new ForkJoinPool(1);
42 <    static final ForkJoinPool asyncSingletonPool = new ForkJoinPool(1);
43 <    static {
44 <        asyncSingletonPool.setAsyncMode(true);
40 >    private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
41 >        try {
42 >            assertTrue(pool.invoke(a) == null);
43 >        } finally {
44 >            joinPool(pool);
45 >        }
46      }
47  
48      static final class FJException extends RuntimeException {
# Line 68 | Line 89 | public class RecursiveActionTest extends
89       * invoke returns when task completes normally.
90       * isCompletedAbnormally and isCancelled return false for normally
91       * completed tasks. getRawResult of a RecursiveAction returns null;
71     *
92       */
93      public void testInvoke() {
94          RecursiveAction a = new RecursiveAction() {
# Line 81 | Line 101 | public class RecursiveActionTest extends
101                  threadAssertFalse(f.isCompletedAbnormally());
102                  threadAssertTrue(f.getRawResult() == null);
103              }};
104 <        mainPool.invoke(a);
104 >        testInvokeOnPool(mainPool(), a);
105      }
106  
107      /**
# Line 100 | Line 120 | public class RecursiveActionTest extends
120                  threadAssertFalse(f.isCompletedAbnormally());
121                  threadAssertTrue(f.getRawResult() == null);
122              }};
123 <        mainPool.invoke(a);
123 >        testInvokeOnPool(mainPool(), a);
124      }
125  
126      /**
# Line 116 | Line 136 | public class RecursiveActionTest extends
136                  threadAssertTrue(f.isDone());
137                  threadAssertTrue(f.getRawResult() == null);
138              }};
139 <        mainPool.invoke(a);
139 >        testInvokeOnPool(mainPool(), a);
140      }
141  
142      /**
# Line 135 | Line 155 | public class RecursiveActionTest extends
155                      unexpectedException(ex);
156                  }
157              }};
158 <        mainPool.invoke(a);
158 >        testInvokeOnPool(mainPool(), a);
159      }
160  
161      /**
# Line 154 | Line 174 | public class RecursiveActionTest extends
174                      unexpectedException(ex);
175                  }
176              }};
177 <        mainPool.invoke(a);
177 >        testInvokeOnPool(mainPool(), a);
178      }
179  
180      /**
# Line 173 | Line 193 | public class RecursiveActionTest extends
193                      unexpectedException(ex);
194                  }
195              }};
196 <        mainPool.invoke(a);
177 <    }
178 <
179 <    /**
180 <     * helpJoin of a forked task returns when task completes
181 <     */
182 <    public void testForkHelpJoin() {
183 <        RecursiveAction a = new RecursiveAction() {
184 <            public void compute() {
185 <                FibAction f = new FibAction(8);
186 <                f.fork();
187 <                f.helpJoin();
188 <                threadAssertTrue(f.result == 21);
189 <                threadAssertTrue(f.isDone());
190 <            }};
191 <        mainPool.invoke(a);
196 >        testInvokeOnPool(mainPool(), a);
197      }
198  
199      /**
# Line 203 | Line 208 | public class RecursiveActionTest extends
208                  threadAssertTrue(f.result == 21);
209                  threadAssertTrue(f.isDone());
210              }};
211 <        mainPool.invoke(a);
207 <    }
208 <
209 <
210 <    /**
211 <     * quietlyHelpJoin of a forked task returns when task completes
212 <     */
213 <    public void testForkQuietlyHelpJoin() {
214 <        RecursiveAction a = new RecursiveAction() {
215 <            public void compute() {
216 <                FibAction f = new FibAction(8);
217 <                f.fork();
218 <                f.quietlyHelpJoin();
219 <                threadAssertTrue(f.result == 21);
220 <                threadAssertTrue(f.isDone());
221 <            }};
222 <        mainPool.invoke(a);
211 >        testInvokeOnPool(mainPool(), a);
212      }
213  
214  
# Line 237 | Line 226 | public class RecursiveActionTest extends
226                  threadAssertTrue(f.isDone());
227                  threadAssertTrue(getQueuedTaskCount() == 0);
228              }};
229 <        mainPool.invoke(a);
229 >        testInvokeOnPool(mainPool(), a);
230      }
231  
232  
# Line 254 | Line 243 | public class RecursiveActionTest extends
243                  } catch (FJException success) {
244                  }
245              }};
246 <        mainPool.invoke(a);
246 >        testInvokeOnPool(mainPool(), a);
247      }
248  
249      /**
# Line 267 | Line 256 | public class RecursiveActionTest extends
256                  f.quietlyInvoke();
257                  threadAssertTrue(f.isDone());
258              }};
259 <        mainPool.invoke(a);
259 >        testInvokeOnPool(mainPool(), a);
260      }
261  
262      /**
# Line 284 | Line 273 | public class RecursiveActionTest extends
273                  } catch (FJException success) {
274                  }
275              }};
276 <        mainPool.invoke(a);
276 >        testInvokeOnPool(mainPool(), a);
277      }
278  
279      /**
# Line 303 | Line 292 | public class RecursiveActionTest extends
292                      unexpectedException(ex);
293                  }
294              }};
295 <        mainPool.invoke(a);
295 >        testInvokeOnPool(mainPool(), a);
296      }
297  
298      /**
# Line 322 | Line 311 | public class RecursiveActionTest extends
311                      unexpectedException(ex);
312                  }
313              }};
314 <        mainPool.invoke(a);
326 <    }
327 <
328 <    /**
329 <     * join of a forked task throws exception when task completes abnormally
330 <     */
331 <    public void testAbnormalForkHelpJoin() {
332 <        RecursiveAction a = new RecursiveAction() {
333 <            public void compute() {
334 <                try {
335 <                    FailingFibAction f = new FailingFibAction(8);
336 <                    f.fork();
337 <                    f.helpJoin();
338 <                    shouldThrow();
339 <                } catch (FJException success) {
340 <                }
341 <            }};
342 <        mainPool.invoke(a);
343 <    }
344 <
345 <    /**
346 <     * quietlyHelpJoin of a forked task returns when task completes abnormally.
347 <     * getException of failed task returns its exception.
348 <     * isCompletedAbnormally of a failed task returns true.
349 <     * isCancelled of a failed uncancelled task returns false
350 <     */
351 <    public void testAbnormalForkQuietlyHelpJoin() {
352 <        RecursiveAction a = new RecursiveAction() {
353 <            public void compute() {
354 <                FailingFibAction f = new FailingFibAction(8);
355 <                f.fork();
356 <                f.quietlyHelpJoin();
357 <                threadAssertTrue(f.isDone());
358 <                threadAssertTrue(f.isCompletedAbnormally());
359 <                threadAssertFalse(f.isCancelled());
360 <                threadAssertTrue(f.getException() instanceof FJException);
361 <            }};
362 <        mainPool.invoke(a);
314 >        testInvokeOnPool(mainPool(), a);
315      }
316  
317      /**
# Line 375 | Line 327 | public class RecursiveActionTest extends
327                  threadAssertTrue(f.isCompletedAbnormally());
328                  threadAssertTrue(f.getException() instanceof FJException);
329              }};
330 <        mainPool.invoke(a);
330 >        testInvokeOnPool(mainPool(), a);
331      }
332  
333      /**
# Line 392 | Line 344 | public class RecursiveActionTest extends
344                  } catch (CancellationException success) {
345                  }
346              }};
347 <        mainPool.invoke(a);
347 >        testInvokeOnPool(mainPool(), a);
348      }
349  
350      /**
# Line 410 | Line 362 | public class RecursiveActionTest extends
362                  } catch (CancellationException success) {
363                  }
364              }};
365 <        mainPool.invoke(a);
365 >        testInvokeOnPool(mainPool(), a);
366      }
367  
368      /**
# Line 430 | Line 382 | public class RecursiveActionTest extends
382                      unexpectedException(ex);
383                  }
384              }};
385 <        mainPool.invoke(a);
385 >        testInvokeOnPool(mainPool(), a);
386      }
387  
388      /**
# Line 450 | Line 402 | public class RecursiveActionTest extends
402                      unexpectedException(ex);
403                  }
404              }};
405 <        mainPool.invoke(a);
454 <    }
455 <
456 <    /**
457 <     * join of a forked task throws exception when task cancelled
458 <     */
459 <    public void testCancelledForkHelpJoin() {
460 <        RecursiveAction a = new RecursiveAction() {
461 <            public void compute() {
462 <                try {
463 <                    FibAction f = new FibAction(8);
464 <                    f.cancel(true);
465 <                    f.fork();
466 <                    f.helpJoin();
467 <                    shouldThrow();
468 <                } catch (CancellationException success) {
469 <                }
470 <            }};
471 <        mainPool.invoke(a);
472 <    }
473 <
474 <    /**
475 <     * quietlyHelpJoin of a forked task returns when task cancelled.
476 <     * getException of cancelled task returns its exception.
477 <     * isCompletedAbnormally of a cancelled task returns true.
478 <     * isCancelled of a cancelled task returns true
479 <     */
480 <    public void testCancelledForkQuietlyHelpJoin() {
481 <        RecursiveAction a = new RecursiveAction() {
482 <            public void compute() {
483 <                FibAction f = new FibAction(8);
484 <                f.cancel(true);
485 <                f.fork();
486 <                f.quietlyHelpJoin();
487 <                threadAssertTrue(f.isDone());
488 <                threadAssertTrue(f.isCompletedAbnormally());
489 <                threadAssertTrue(f.isCancelled());
490 <                threadAssertTrue(f.getException() instanceof CancellationException);
491 <            }};
492 <        mainPool.invoke(a);
405 >        testInvokeOnPool(mainPool(), a);
406      }
407  
408      /**
# Line 506 | Line 419 | public class RecursiveActionTest extends
419                  threadAssertTrue(f.isCompletedAbnormally());
420                  threadAssertTrue(f.getException() instanceof CancellationException);
421              }};
422 <        mainPool.invoke(a);
422 >        testInvokeOnPool(mainPool(), a);
423      }
424  
425      /**
426       * getPool of executing task returns its pool
427       */
428      public void testGetPool() {
429 +        final ForkJoinPool mainPool = mainPool();
430          RecursiveAction a = new RecursiveAction() {
431              public void compute() {
432                  threadAssertTrue(getPool() == mainPool);
433              }};
434 <        mainPool.invoke(a);
434 >        testInvokeOnPool(mainPool, a);
435      }
436  
437      /**
# Line 539 | Line 453 | public class RecursiveActionTest extends
453              public void compute() {
454                  threadAssertTrue(inForkJoinPool());
455              }};
456 <        mainPool.invoke(a);
456 >        testInvokeOnPool(mainPool(), a);
457      }
458  
459      /**
# Line 557 | Line 471 | public class RecursiveActionTest extends
471       * getPool of current thread in pool returns its pool
472       */
473      public void testWorkerGetPool() {
474 +        final ForkJoinPool mainPool = mainPool();
475          RecursiveAction a = new RecursiveAction() {
476              public void compute() {
477                  ForkJoinWorkerThread w =
478 <                    (ForkJoinWorkerThread)(Thread.currentThread());
478 >                    (ForkJoinWorkerThread) Thread.currentThread();
479                  threadAssertTrue(w.getPool() == mainPool);
480              }};
481 <        mainPool.invoke(a);
481 >        testInvokeOnPool(mainPool, a);
482      }
483  
484      /**
485       * getPoolIndex of current thread in pool returns 0 <= value < poolSize
571     *
486       */
487      public void testWorkerGetPoolIndex() {
488 +        final ForkJoinPool mainPool = mainPool();
489          RecursiveAction a = new RecursiveAction() {
490              public void compute() {
491                  ForkJoinWorkerThread w =
# Line 579 | Line 494 | public class RecursiveActionTest extends
494                  threadAssertTrue(idx >= 0);
495                  threadAssertTrue(idx < mainPool.getPoolSize());
496              }};
497 <        mainPool.invoke(a);
497 >        testInvokeOnPool(mainPool, a);
498      }
499  
500  
# Line 610 | Line 525 | public class RecursiveActionTest extends
525                  f.invoke();
526                  threadAssertTrue(f.result == 21);
527              }};
528 <        mainPool.invoke(a);
528 >        testInvokeOnPool(mainPool(), a);
529      }
530  
531      /**
# Line 627 | Line 542 | public class RecursiveActionTest extends
542                  } catch (FJException success) {
543                  }
544              }};
545 <        mainPool.invoke(a);
545 >        testInvokeOnPool(mainPool(), a);
546      }
547  
548      /**
# Line 642 | Line 557 | public class RecursiveActionTest extends
557                  threadAssertTrue(f.isDone());
558                  threadAssertTrue(f.result == 0);
559              }};
560 <        mainPool.invoke(a);
560 >        testInvokeOnPool(mainPool(), a);
561      }
562  
563      /**
# Line 659 | Line 574 | public class RecursiveActionTest extends
574                  threadAssertTrue(g.isDone());
575                  threadAssertTrue(g.result == 34);
576              }};
577 <        mainPool.invoke(a);
577 >        testInvokeOnPool(mainPool(), a);
578      }
579  
580      /**
# Line 673 | Line 588 | public class RecursiveActionTest extends
588                  threadAssertTrue(f.isDone());
589                  threadAssertTrue(f.result == 21);
590              }};
591 <        mainPool.invoke(a);
591 >        testInvokeOnPool(mainPool(), a);
592      }
593  
594      /**
# Line 693 | Line 608 | public class RecursiveActionTest extends
608                  threadAssertTrue(h.isDone());
609                  threadAssertTrue(h.result == 13);
610              }};
611 <        mainPool.invoke(a);
611 >        testInvokeOnPool(mainPool(), a);
612      }
613  
614      /**
# Line 717 | Line 632 | public class RecursiveActionTest extends
632                  threadAssertTrue(h.isDone());
633                  threadAssertTrue(h.result == 13);
634              }};
635 <        mainPool.invoke(a);
635 >        testInvokeOnPool(mainPool(), a);
636      }
637  
638  
# Line 736 | Line 651 | public class RecursiveActionTest extends
651                  } catch (NullPointerException success) {
652                  }
653              }};
654 <        mainPool.invoke(a);
654 >        testInvokeOnPool(mainPool(), a);
655      }
656  
657      /**
# Line 753 | Line 668 | public class RecursiveActionTest extends
668                  } catch (FJException success) {
669                  }
670              }};
671 <        mainPool.invoke(a);
671 >        testInvokeOnPool(mainPool(), a);
672      }
673  
674      /**
# Line 769 | Line 684 | public class RecursiveActionTest extends
684                  } catch (FJException success) {
685                  }
686              }};
687 <        mainPool.invoke(a);
687 >        testInvokeOnPool(mainPool(), a);
688      }
689  
690      /**
# Line 787 | Line 702 | public class RecursiveActionTest extends
702                  } catch (FJException success) {
703                  }
704              }};
705 <        mainPool.invoke(a);
705 >        testInvokeOnPool(mainPool(), a);
706      }
707  
708      /**
# Line 809 | Line 724 | public class RecursiveActionTest extends
724                  } catch (FJException success) {
725                  }
726              }};
727 <        mainPool.invoke(a);
727 >        testInvokeOnPool(mainPool(), a);
728      }
729  
730      /**
# Line 828 | Line 743 | public class RecursiveActionTest extends
743                  threadAssertFalse(f.isDone());
744                  threadAssertTrue(g.isDone());
745              }};
746 <        singletonPool.invoke(a);
746 >        testInvokeOnPool(singletonPool(), a);
747      }
748  
749      /**
# Line 847 | Line 762 | public class RecursiveActionTest extends
762                  threadAssertTrue(getSurplusQueuedTaskCount() > 0);
763                  helpQuiesce();
764              }};
765 <        singletonPool.invoke(a);
765 >        testInvokeOnPool(singletonPool(), a);
766      }
767  
768      /**
# Line 865 | Line 780 | public class RecursiveActionTest extends
780                  threadAssertTrue(f.isDone());
781                  helpQuiesce();
782              }};
783 <        singletonPool.invoke(a);
783 >        testInvokeOnPool(singletonPool(), a);
784      }
785  
786      /**
# Line 883 | Line 798 | public class RecursiveActionTest extends
798                  helpQuiesce();
799                  threadAssertFalse(f.isDone());
800              }};
801 <        singletonPool.invoke(a);
801 >        testInvokeOnPool(singletonPool(), a);
802      }
803  
804      /**
# Line 902 | Line 817 | public class RecursiveActionTest extends
817                  threadAssertFalse(f.isDone());
818                  threadAssertTrue(g.isDone());
819              }};
820 <        singletonPool.invoke(a);
820 >        testInvokeOnPool(singletonPool(), a);
821      }
822  
823      /**
# Line 920 | Line 835 | public class RecursiveActionTest extends
835                  helpQuiesce();
836                  threadAssertTrue(f.isDone());
837              }};
838 <        asyncSingletonPool.invoke(a);
838 >        testInvokeOnPool(asyncSingletonPool(), a);
839      }
840  
841      /**
# Line 939 | Line 854 | public class RecursiveActionTest extends
854                  threadAssertTrue(f.isDone());
855                  threadAssertFalse(g.isDone());
856              }};
857 <        asyncSingletonPool.invoke(a);
857 >        testInvokeOnPool(asyncSingletonPool(), a);
858      }
859  
860      /**
# Line 958 | Line 873 | public class RecursiveActionTest extends
873                  threadAssertTrue(f.isDone());
874                  threadAssertFalse(g.isDone());
875              }};
876 <        asyncSingletonPool.invoke(a);
876 >        testInvokeOnPool(asyncSingletonPool(), a);
877      }
878  
879   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines