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.14 by jsr166, Mon Sep 13 07:51:18 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 +
7   import junit.framework.*;
8   import java.util.concurrent.*;
9   import java.util.*;
10  
10
11   public class RecursiveActionTest extends JSR166TestCase {
12  
13      public static void main(String[] args) {
14 <        junit.textui.TestRunner.run (suite());
14 >        junit.textui.TestRunner.run(suite());
15      }
16 +
17      public static Test suite() {
18 <        return new TestSuite(RecursiveActionTest.class);
18 >        return new TestSuite(RecursiveActionTest.class);
19 >    }
20 >
21 >    private static ForkJoinPool mainPool() {
22 >        return new ForkJoinPool();
23 >    }
24 >
25 >    private static ForkJoinPool singletonPool() {
26 >        return new ForkJoinPool(1);
27 >    }
28 >
29 >    private static ForkJoinPool asyncSingletonPool() {
30 >        return new ForkJoinPool(1,
31 >                                ForkJoinPool.defaultForkJoinWorkerThreadFactory,
32 >                                null, true);
33      }
34  
35 <    static final ForkJoinPool mainPool = new ForkJoinPool();
36 <    static final ForkJoinPool singletonPool = new ForkJoinPool(1);
37 <    static final ForkJoinPool asyncSingletonPool = new ForkJoinPool(1);
38 <    static {
39 <        asyncSingletonPool.setAsyncMode(true);
35 >    private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
36 >        try {
37 >            assertTrue(pool.invoke(a) == null);
38 >        } finally {
39 >            joinPool(pool);
40 >        }
41      }
42  
43      static final class FJException extends RuntimeException {
# Line 68 | Line 84 | public class RecursiveActionTest extends
84       * invoke returns when task completes normally.
85       * isCompletedAbnormally and isCancelled return false for normally
86       * completed tasks. getRawResult of a RecursiveAction returns null;
71     *
87       */
88      public void testInvoke() {
89          RecursiveAction a = new RecursiveAction() {
# Line 81 | Line 96 | public class RecursiveActionTest extends
96                  threadAssertFalse(f.isCompletedAbnormally());
97                  threadAssertTrue(f.getRawResult() == null);
98              }};
99 <        mainPool.invoke(a);
99 >        testInvokeOnPool(mainPool(), a);
100      }
101  
102      /**
# Line 100 | Line 115 | public class RecursiveActionTest extends
115                  threadAssertFalse(f.isCompletedAbnormally());
116                  threadAssertTrue(f.getRawResult() == null);
117              }};
118 <        mainPool.invoke(a);
118 >        testInvokeOnPool(mainPool(), a);
119      }
120  
121      /**
# Line 116 | Line 131 | public class RecursiveActionTest extends
131                  threadAssertTrue(f.isDone());
132                  threadAssertTrue(f.getRawResult() == null);
133              }};
134 <        mainPool.invoke(a);
134 >        testInvokeOnPool(mainPool(), a);
135      }
136  
137      /**
# Line 135 | Line 150 | public class RecursiveActionTest extends
150                      unexpectedException(ex);
151                  }
152              }};
153 <        mainPool.invoke(a);
153 >        testInvokeOnPool(mainPool(), a);
154      }
155  
156      /**
# Line 154 | Line 169 | public class RecursiveActionTest extends
169                      unexpectedException(ex);
170                  }
171              }};
172 <        mainPool.invoke(a);
172 >        testInvokeOnPool(mainPool(), a);
173      }
174  
175      /**
# Line 173 | Line 188 | public class RecursiveActionTest extends
188                      unexpectedException(ex);
189                  }
190              }};
191 <        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);
191 >        testInvokeOnPool(mainPool(), a);
192      }
193  
194      /**
# Line 203 | Line 203 | public class RecursiveActionTest extends
203                  threadAssertTrue(f.result == 21);
204                  threadAssertTrue(f.isDone());
205              }};
206 <        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);
206 >        testInvokeOnPool(mainPool(), a);
207      }
208  
209  
# Line 237 | Line 221 | public class RecursiveActionTest extends
221                  threadAssertTrue(f.isDone());
222                  threadAssertTrue(getQueuedTaskCount() == 0);
223              }};
224 <        mainPool.invoke(a);
224 >        testInvokeOnPool(mainPool(), a);
225      }
226  
227  
# Line 254 | Line 238 | public class RecursiveActionTest extends
238                  } catch (FJException success) {
239                  }
240              }};
241 <        mainPool.invoke(a);
241 >        testInvokeOnPool(mainPool(), a);
242      }
243  
244      /**
# Line 267 | Line 251 | public class RecursiveActionTest extends
251                  f.quietlyInvoke();
252                  threadAssertTrue(f.isDone());
253              }};
254 <        mainPool.invoke(a);
254 >        testInvokeOnPool(mainPool(), a);
255      }
256  
257      /**
# Line 284 | Line 268 | public class RecursiveActionTest extends
268                  } catch (FJException success) {
269                  }
270              }};
271 <        mainPool.invoke(a);
271 >        testInvokeOnPool(mainPool(), a);
272      }
273  
274      /**
# Line 303 | Line 287 | public class RecursiveActionTest extends
287                      unexpectedException(ex);
288                  }
289              }};
290 <        mainPool.invoke(a);
290 >        testInvokeOnPool(mainPool(), a);
291      }
292  
293      /**
# Line 322 | Line 306 | public class RecursiveActionTest extends
306                      unexpectedException(ex);
307                  }
308              }};
309 <        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);
309 >        testInvokeOnPool(mainPool(), a);
310      }
311  
312      /**
# Line 375 | Line 322 | public class RecursiveActionTest extends
322                  threadAssertTrue(f.isCompletedAbnormally());
323                  threadAssertTrue(f.getException() instanceof FJException);
324              }};
325 <        mainPool.invoke(a);
325 >        testInvokeOnPool(mainPool(), a);
326      }
327  
328      /**
# Line 392 | Line 339 | public class RecursiveActionTest extends
339                  } catch (CancellationException success) {
340                  }
341              }};
342 <        mainPool.invoke(a);
342 >        testInvokeOnPool(mainPool(), a);
343      }
344  
345      /**
# Line 410 | Line 357 | public class RecursiveActionTest extends
357                  } catch (CancellationException success) {
358                  }
359              }};
360 <        mainPool.invoke(a);
360 >        testInvokeOnPool(mainPool(), a);
361      }
362  
363      /**
# Line 430 | Line 377 | public class RecursiveActionTest extends
377                      unexpectedException(ex);
378                  }
379              }};
380 <        mainPool.invoke(a);
380 >        testInvokeOnPool(mainPool(), a);
381      }
382  
383      /**
# Line 450 | Line 397 | public class RecursiveActionTest extends
397                      unexpectedException(ex);
398                  }
399              }};
400 <        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);
400 >        testInvokeOnPool(mainPool(), a);
401      }
402  
403      /**
# Line 506 | Line 414 | public class RecursiveActionTest extends
414                  threadAssertTrue(f.isCompletedAbnormally());
415                  threadAssertTrue(f.getException() instanceof CancellationException);
416              }};
417 <        mainPool.invoke(a);
417 >        testInvokeOnPool(mainPool(), a);
418      }
419  
420      /**
421       * getPool of executing task returns its pool
422       */
423      public void testGetPool() {
424 +        final ForkJoinPool mainPool = mainPool();
425          RecursiveAction a = new RecursiveAction() {
426              public void compute() {
427                  threadAssertTrue(getPool() == mainPool);
428              }};
429 <        mainPool.invoke(a);
429 >        testInvokeOnPool(mainPool, a);
430      }
431  
432      /**
# Line 539 | Line 448 | public class RecursiveActionTest extends
448              public void compute() {
449                  threadAssertTrue(inForkJoinPool());
450              }};
451 <        mainPool.invoke(a);
451 >        testInvokeOnPool(mainPool(), a);
452      }
453  
454      /**
# Line 557 | Line 466 | public class RecursiveActionTest extends
466       * getPool of current thread in pool returns its pool
467       */
468      public void testWorkerGetPool() {
469 +        final ForkJoinPool mainPool = mainPool();
470          RecursiveAction a = new RecursiveAction() {
471              public void compute() {
472                  ForkJoinWorkerThread w =
473 <                    (ForkJoinWorkerThread)(Thread.currentThread());
473 >                    (ForkJoinWorkerThread) Thread.currentThread();
474                  threadAssertTrue(w.getPool() == mainPool);
475              }};
476 <        mainPool.invoke(a);
476 >        testInvokeOnPool(mainPool, a);
477      }
478  
479      /**
480       * getPoolIndex of current thread in pool returns 0 <= value < poolSize
571     *
481       */
482      public void testWorkerGetPoolIndex() {
483 +        final ForkJoinPool mainPool = mainPool();
484          RecursiveAction a = new RecursiveAction() {
485              public void compute() {
486                  ForkJoinWorkerThread w =
# Line 579 | Line 489 | public class RecursiveActionTest extends
489                  threadAssertTrue(idx >= 0);
490                  threadAssertTrue(idx < mainPool.getPoolSize());
491              }};
492 <        mainPool.invoke(a);
492 >        testInvokeOnPool(mainPool, a);
493      }
494  
495  
# Line 610 | Line 520 | public class RecursiveActionTest extends
520                  f.invoke();
521                  threadAssertTrue(f.result == 21);
522              }};
523 <        mainPool.invoke(a);
523 >        testInvokeOnPool(mainPool(), a);
524      }
525  
526      /**
# Line 627 | Line 537 | public class RecursiveActionTest extends
537                  } catch (FJException success) {
538                  }
539              }};
540 <        mainPool.invoke(a);
540 >        testInvokeOnPool(mainPool(), a);
541      }
542  
543      /**
# Line 642 | Line 552 | public class RecursiveActionTest extends
552                  threadAssertTrue(f.isDone());
553                  threadAssertTrue(f.result == 0);
554              }};
555 <        mainPool.invoke(a);
555 >        testInvokeOnPool(mainPool(), a);
556      }
557  
558      /**
# Line 659 | Line 569 | public class RecursiveActionTest extends
569                  threadAssertTrue(g.isDone());
570                  threadAssertTrue(g.result == 34);
571              }};
572 <        mainPool.invoke(a);
572 >        testInvokeOnPool(mainPool(), a);
573      }
574  
575      /**
# Line 673 | Line 583 | public class RecursiveActionTest extends
583                  threadAssertTrue(f.isDone());
584                  threadAssertTrue(f.result == 21);
585              }};
586 <        mainPool.invoke(a);
586 >        testInvokeOnPool(mainPool(), a);
587      }
588  
589      /**
# Line 693 | Line 603 | public class RecursiveActionTest extends
603                  threadAssertTrue(h.isDone());
604                  threadAssertTrue(h.result == 13);
605              }};
606 <        mainPool.invoke(a);
606 >        testInvokeOnPool(mainPool(), a);
607      }
608  
609      /**
# Line 717 | Line 627 | public class RecursiveActionTest extends
627                  threadAssertTrue(h.isDone());
628                  threadAssertTrue(h.result == 13);
629              }};
630 <        mainPool.invoke(a);
630 >        testInvokeOnPool(mainPool(), a);
631      }
632  
633  
# Line 736 | Line 646 | public class RecursiveActionTest extends
646                  } catch (NullPointerException success) {
647                  }
648              }};
649 <        mainPool.invoke(a);
649 >        testInvokeOnPool(mainPool(), a);
650      }
651  
652      /**
# Line 753 | Line 663 | public class RecursiveActionTest extends
663                  } catch (FJException success) {
664                  }
665              }};
666 <        mainPool.invoke(a);
666 >        testInvokeOnPool(mainPool(), a);
667      }
668  
669      /**
# Line 769 | Line 679 | public class RecursiveActionTest extends
679                  } catch (FJException success) {
680                  }
681              }};
682 <        mainPool.invoke(a);
682 >        testInvokeOnPool(mainPool(), a);
683      }
684  
685      /**
# Line 787 | Line 697 | public class RecursiveActionTest extends
697                  } catch (FJException success) {
698                  }
699              }};
700 <        mainPool.invoke(a);
700 >        testInvokeOnPool(mainPool(), a);
701      }
702  
703      /**
# Line 809 | Line 719 | public class RecursiveActionTest extends
719                  } catch (FJException success) {
720                  }
721              }};
722 <        mainPool.invoke(a);
722 >        testInvokeOnPool(mainPool(), a);
723      }
724  
725      /**
# Line 828 | Line 738 | public class RecursiveActionTest extends
738                  threadAssertFalse(f.isDone());
739                  threadAssertTrue(g.isDone());
740              }};
741 <        singletonPool.invoke(a);
741 >        testInvokeOnPool(singletonPool(), a);
742      }
743  
744      /**
# Line 847 | Line 757 | public class RecursiveActionTest extends
757                  threadAssertTrue(getSurplusQueuedTaskCount() > 0);
758                  helpQuiesce();
759              }};
760 <        singletonPool.invoke(a);
760 >        testInvokeOnPool(singletonPool(), a);
761      }
762  
763      /**
# Line 865 | Line 775 | public class RecursiveActionTest extends
775                  threadAssertTrue(f.isDone());
776                  helpQuiesce();
777              }};
778 <        singletonPool.invoke(a);
778 >        testInvokeOnPool(singletonPool(), a);
779      }
780  
781      /**
# Line 883 | Line 793 | public class RecursiveActionTest extends
793                  helpQuiesce();
794                  threadAssertFalse(f.isDone());
795              }};
796 <        singletonPool.invoke(a);
796 >        testInvokeOnPool(singletonPool(), a);
797      }
798  
799      /**
# Line 902 | Line 812 | public class RecursiveActionTest extends
812                  threadAssertFalse(f.isDone());
813                  threadAssertTrue(g.isDone());
814              }};
815 <        singletonPool.invoke(a);
815 >        testInvokeOnPool(singletonPool(), a);
816      }
817  
818      /**
# Line 920 | Line 830 | public class RecursiveActionTest extends
830                  helpQuiesce();
831                  threadAssertTrue(f.isDone());
832              }};
833 <        asyncSingletonPool.invoke(a);
833 >        testInvokeOnPool(asyncSingletonPool(), a);
834      }
835  
836      /**
# Line 939 | Line 849 | public class RecursiveActionTest extends
849                  threadAssertTrue(f.isDone());
850                  threadAssertFalse(g.isDone());
851              }};
852 <        asyncSingletonPool.invoke(a);
852 >        testInvokeOnPool(asyncSingletonPool(), a);
853      }
854  
855      /**
# Line 958 | Line 868 | public class RecursiveActionTest extends
868                  threadAssertTrue(f.isDone());
869                  threadAssertFalse(g.isDone());
870              }};
871 <        asyncSingletonPool.invoke(a);
871 >        testInvokeOnPool(asyncSingletonPool(), a);
872      }
873  
874   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines