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.13 by jsr166, Sat Sep 11 07:31:52 2010 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) {
# Line 18 | Line 18 | public class RecursiveActionTest extends
18          return new TestSuite(RecursiveActionTest.class);
19      }
20  
21 <    static final ForkJoinPool mainPool = new ForkJoinPool();
22 <    static final ForkJoinPool singletonPool = new ForkJoinPool(1);
23 <    static final ForkJoinPool asyncSingletonPool =
24 <        new ForkJoinPool(1, ForkJoinPool.defaultForkJoinWorkerThreadFactory,
25 <                         null, true);
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 >    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 {
44          FJException() { super(); }
# Line 80 | 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 99 | 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 115 | 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 134 | Line 150 | public class RecursiveActionTest extends
150                      unexpectedException(ex);
151                  }
152              }};
153 <        mainPool.invoke(a);
153 >        testInvokeOnPool(mainPool(), a);
154      }
155  
156      /**
# Line 153 | Line 169 | public class RecursiveActionTest extends
169                      unexpectedException(ex);
170                  }
171              }};
172 <        mainPool.invoke(a);
172 >        testInvokeOnPool(mainPool(), a);
173      }
174  
175      /**
# Line 172 | Line 188 | public class RecursiveActionTest extends
188                      unexpectedException(ex);
189                  }
190              }};
191 <        mainPool.invoke(a);
191 >        testInvokeOnPool(mainPool(), a);
192      }
193  
194      /**
# Line 187 | Line 203 | public class RecursiveActionTest extends
203                  threadAssertTrue(f.result == 21);
204                  threadAssertTrue(f.isDone());
205              }};
206 <        mainPool.invoke(a);
206 >        testInvokeOnPool(mainPool(), a);
207      }
208  
209  
# Line 205 | 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 222 | Line 238 | public class RecursiveActionTest extends
238                  } catch (FJException success) {
239                  }
240              }};
241 <        mainPool.invoke(a);
241 >        testInvokeOnPool(mainPool(), a);
242      }
243  
244      /**
# Line 235 | 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 252 | Line 268 | public class RecursiveActionTest extends
268                  } catch (FJException success) {
269                  }
270              }};
271 <        mainPool.invoke(a);
271 >        testInvokeOnPool(mainPool(), a);
272      }
273  
274      /**
# Line 271 | Line 287 | public class RecursiveActionTest extends
287                      unexpectedException(ex);
288                  }
289              }};
290 <        mainPool.invoke(a);
290 >        testInvokeOnPool(mainPool(), a);
291      }
292  
293      /**
# Line 290 | Line 306 | public class RecursiveActionTest extends
306                      unexpectedException(ex);
307                  }
308              }};
309 <        mainPool.invoke(a);
309 >        testInvokeOnPool(mainPool(), a);
310      }
311  
312      /**
# Line 306 | 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 323 | Line 339 | public class RecursiveActionTest extends
339                  } catch (CancellationException success) {
340                  }
341              }};
342 <        mainPool.invoke(a);
342 >        testInvokeOnPool(mainPool(), a);
343      }
344  
345      /**
# Line 341 | Line 357 | public class RecursiveActionTest extends
357                  } catch (CancellationException success) {
358                  }
359              }};
360 <        mainPool.invoke(a);
360 >        testInvokeOnPool(mainPool(), a);
361      }
362  
363      /**
# Line 361 | Line 377 | public class RecursiveActionTest extends
377                      unexpectedException(ex);
378                  }
379              }};
380 <        mainPool.invoke(a);
380 >        testInvokeOnPool(mainPool(), a);
381      }
382  
383      /**
# Line 381 | Line 397 | public class RecursiveActionTest extends
397                      unexpectedException(ex);
398                  }
399              }};
400 <        mainPool.invoke(a);
400 >        testInvokeOnPool(mainPool(), a);
401      }
402  
403      /**
# Line 398 | 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 431 | 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 449 | 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
481       */
482      public void testWorkerGetPoolIndex() {
483 +        final ForkJoinPool mainPool = mainPool();
484          RecursiveAction a = new RecursiveAction() {
485              public void compute() {
486                  ForkJoinWorkerThread w =
# Line 470 | 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 501 | 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 518 | Line 537 | public class RecursiveActionTest extends
537                  } catch (FJException success) {
538                  }
539              }};
540 <        mainPool.invoke(a);
540 >        testInvokeOnPool(mainPool(), a);
541      }
542  
543      /**
# Line 533 | 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 550 | 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 564 | 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 584 | 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 608 | 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 627 | Line 646 | public class RecursiveActionTest extends
646                  } catch (NullPointerException success) {
647                  }
648              }};
649 <        mainPool.invoke(a);
649 >        testInvokeOnPool(mainPool(), a);
650      }
651  
652      /**
# Line 644 | Line 663 | public class RecursiveActionTest extends
663                  } catch (FJException success) {
664                  }
665              }};
666 <        mainPool.invoke(a);
666 >        testInvokeOnPool(mainPool(), a);
667      }
668  
669      /**
# Line 660 | Line 679 | public class RecursiveActionTest extends
679                  } catch (FJException success) {
680                  }
681              }};
682 <        mainPool.invoke(a);
682 >        testInvokeOnPool(mainPool(), a);
683      }
684  
685      /**
# Line 678 | Line 697 | public class RecursiveActionTest extends
697                  } catch (FJException success) {
698                  }
699              }};
700 <        mainPool.invoke(a);
700 >        testInvokeOnPool(mainPool(), a);
701      }
702  
703      /**
# Line 700 | Line 719 | public class RecursiveActionTest extends
719                  } catch (FJException success) {
720                  }
721              }};
722 <        mainPool.invoke(a);
722 >        testInvokeOnPool(mainPool(), a);
723      }
724  
725      /**
# Line 719 | 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 738 | 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 756 | 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 774 | 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 793 | 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 811 | 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 830 | 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 849 | 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