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.12 by jsr166, Mon Sep 13 07:22:29 2010 UTC vs.
Revision 1.16 by jsr166, Mon Sep 13 23:23:44 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;
6   import java.util.concurrent.ExecutionException;
7   import java.util.concurrent.CancellationException;
16 import java.util.concurrent.RejectedExecutionException;
8   import java.util.concurrent.ForkJoinPool;
9   import java.util.concurrent.ForkJoinTask;
10   import java.util.concurrent.ForkJoinWorkerThread;
11   import java.util.concurrent.RecursiveAction;
21 import java.util.concurrent.RecursiveTask;
12   import java.util.concurrent.TimeUnit;
13 + import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
14 + import java.util.HashSet;
15   import junit.framework.*;
24 import java.util.concurrent.TimeUnit;
25 import java.util.concurrent.atomic.*;
26 import java.util.*;
16  
17   public class ForkJoinTaskTest extends JSR166TestCase {
18  
# Line 35 | Line 24 | public class ForkJoinTaskTest extends JS
24          return new TestSuite(ForkJoinTaskTest.class);
25      }
26  
27 <    /**
27 >    private static ForkJoinPool mainPool() {
28 >        return new ForkJoinPool();
29 >    }
30 >
31 >    private static ForkJoinPool singletonPool() {
32 >        return new ForkJoinPool(1);
33 >    }
34 >
35 >    private static ForkJoinPool asyncSingletonPool() {
36 >        return new ForkJoinPool(1,
37 >                                ForkJoinPool.defaultForkJoinWorkerThreadFactory,
38 >                                null, true);
39 >    }
40 >
41 >    private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
42 >        try {
43 >            assertTrue(pool.invoke(a) == null);
44 >        } finally {
45 >            joinPool(pool);
46 >        }
47 >    }
48 >
49 >    /*
50       * Testing coverage notes:
51       *
52       * To test extension methods and overrides, most tests use
# Line 43 | Line 54 | public class ForkJoinTaskTest extends JS
54       * differently than supplied Recursive forms.
55       */
56  
46    static final ForkJoinPool mainPool = new ForkJoinPool();
47    static final ForkJoinPool singletonPool = new ForkJoinPool(1);
48    static final ForkJoinPool asyncSingletonPool =
49        new ForkJoinPool(1, ForkJoinPool.defaultForkJoinWorkerThreadFactory,
50                         null, true);
57      static final class FJException extends RuntimeException {
58          FJException() { super(); }
59      }
# Line 230 | Line 236 | public class ForkJoinTaskTest extends JS
236          RecursiveAction a = new RecursiveAction() {
237              public void compute() {
238                  AsyncFib f = new AsyncFib(8);
239 <                f.invoke();
239 >                threadAssertNull(f.invoke());
240                  threadAssertTrue(f.number == 21);
241                  threadAssertTrue(f.isDone());
242                  threadAssertFalse(f.isCancelled());
243                  threadAssertFalse(f.isCompletedAbnormally());
244                  threadAssertTrue(f.getRawResult() == null);
245              }};
246 <        mainPool.invoke(a);
246 >        testInvokeOnPool(mainPool(), a);
247      }
248  
249      /**
# Line 256 | Line 262 | public class ForkJoinTaskTest extends JS
262                  threadAssertFalse(f.isCompletedAbnormally());
263                  threadAssertTrue(f.getRawResult() == null);
264              }};
265 <        mainPool.invoke(a);
265 >        testInvokeOnPool(mainPool(), a);
266      }
267  
268      /**
# Line 266 | Line 272 | public class ForkJoinTaskTest extends JS
272          RecursiveAction a = new RecursiveAction() {
273              public void compute() {
274                  AsyncFib f = new AsyncFib(8);
275 <                f.fork();
276 <                f.join();
275 >                threadAssertSame(f, f.fork());
276 >                threadAssertNull(f.join());
277                  threadAssertTrue(f.number == 21);
278                  threadAssertTrue(f.isDone());
279                  threadAssertTrue(f.getRawResult() == null);
280              }};
281 <        mainPool.invoke(a);
281 >        testInvokeOnPool(mainPool(), a);
282      }
283  
284      /**
# Line 283 | Line 289 | public class ForkJoinTaskTest extends JS
289              public void compute() {
290                  try {
291                      AsyncFib f = new AsyncFib(8);
292 <                    f.fork();
293 <                    f.get();
292 >                    threadAssertSame(f, f.fork());
293 >                    threadAssertNull(f.get());
294                      threadAssertTrue(f.number == 21);
295                      threadAssertTrue(f.isDone());
296                  } catch (Exception ex) {
297                      unexpectedException(ex);
298                  }
299              }};
300 <        mainPool.invoke(a);
300 >        testInvokeOnPool(mainPool(), a);
301      }
302  
303      /**
# Line 302 | Line 308 | public class ForkJoinTaskTest extends JS
308              public void compute() {
309                  try {
310                      AsyncFib f = new AsyncFib(8);
311 <                    f.fork();
312 <                    f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
311 >                    threadAssertSame(f, f.fork());
312 >                    threadAssertNull(f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
313                      threadAssertTrue(f.number == 21);
314                      threadAssertTrue(f.isDone());
315                  } catch (Exception ex) {
316                      unexpectedException(ex);
317                  }
318              }};
319 <        mainPool.invoke(a);
319 >        testInvokeOnPool(mainPool(), a);
320      }
321  
322      /**
# Line 321 | Line 327 | public class ForkJoinTaskTest extends JS
327              public void compute() {
328                  try {
329                      AsyncFib f = new AsyncFib(8);
330 <                    f.fork();
330 >                    threadAssertSame(f, f.fork());
331                      f.get(5L, null);
332                      shouldThrow();
333                  } catch (NullPointerException success) {
# Line 329 | Line 335 | public class ForkJoinTaskTest extends JS
335                      unexpectedException(ex);
336                  }
337              }};
338 <        mainPool.invoke(a);
338 >        testInvokeOnPool(mainPool(), a);
339      }
340  
341      /**
# Line 339 | Line 345 | public class ForkJoinTaskTest extends JS
345          RecursiveAction a = new RecursiveAction() {
346              public void compute() {
347                  AsyncFib f = new AsyncFib(8);
348 <                f.fork();
348 >                threadAssertSame(f, f.fork());
349                  f.quietlyJoin();
350                  threadAssertTrue(f.number == 21);
351                  threadAssertTrue(f.isDone());
352              }};
353 <        mainPool.invoke(a);
353 >        testInvokeOnPool(mainPool(), a);
354      }
355  
356  
# Line 356 | Line 362 | public class ForkJoinTaskTest extends JS
362          RecursiveAction a = new RecursiveAction() {
363              public void compute() {
364                  AsyncFib f = new AsyncFib(8);
365 <                f.fork();
365 >                threadAssertSame(f, f.fork());
366                  f.helpQuiesce();
367                  threadAssertTrue(f.number == 21);
368                  threadAssertTrue(f.isDone());
369                  threadAssertTrue(getQueuedTaskCount() == 0);
370              }};
371 <        mainPool.invoke(a);
371 >        testInvokeOnPool(mainPool(), a);
372      }
373  
374  
# Line 379 | Line 385 | public class ForkJoinTaskTest extends JS
385                  } catch (FJException success) {
386                  }
387              }};
388 <        mainPool.invoke(a);
388 >        testInvokeOnPool(mainPool(), a);
389      }
390  
391      /**
# Line 392 | Line 398 | public class ForkJoinTaskTest extends JS
398                  f.quietlyInvoke();
399                  threadAssertTrue(f.isDone());
400              }};
401 <        mainPool.invoke(a);
401 >        testInvokeOnPool(mainPool(), a);
402      }
403  
404      /**
# Line 403 | Line 409 | public class ForkJoinTaskTest extends JS
409              public void compute() {
410                  try {
411                      FailingAsyncFib f = new FailingAsyncFib(8);
412 <                    f.fork();
412 >                    threadAssertSame(f, f.fork());
413                      f.join();
414                      shouldThrow();
415                  } catch (FJException success) {
416                  }
417              }};
418 <        mainPool.invoke(a);
418 >        testInvokeOnPool(mainPool(), a);
419      }
420  
421      /**
# Line 420 | Line 426 | public class ForkJoinTaskTest extends JS
426              public void compute() {
427                  try {
428                      FailingAsyncFib f = new FailingAsyncFib(8);
429 <                    f.fork();
429 >                    threadAssertSame(f, f.fork());
430                      f.get();
431                      shouldThrow();
432                  } catch (ExecutionException success) {
# Line 428 | Line 434 | public class ForkJoinTaskTest extends JS
434                      unexpectedException(ex);
435                  }
436              }};
437 <        mainPool.invoke(a);
437 >        testInvokeOnPool(mainPool(), a);
438      }
439  
440      /**
# Line 439 | Line 445 | public class ForkJoinTaskTest extends JS
445              public void compute() {
446                  try {
447                      FailingAsyncFib f = new FailingAsyncFib(8);
448 <                    f.fork();
448 >                    threadAssertSame(f, f.fork());
449                      f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
450                      shouldThrow();
451                  } catch (ExecutionException success) {
# Line 447 | Line 453 | public class ForkJoinTaskTest extends JS
453                      unexpectedException(ex);
454                  }
455              }};
456 <        mainPool.invoke(a);
456 >        testInvokeOnPool(mainPool(), a);
457      }
458  
459      /**
# Line 457 | Line 463 | public class ForkJoinTaskTest extends JS
463          RecursiveAction a = new RecursiveAction() {
464              public void compute() {
465                  FailingAsyncFib f = new FailingAsyncFib(8);
466 <                f.fork();
466 >                threadAssertSame(f, f.fork());
467                  f.quietlyJoin();
468                  threadAssertTrue(f.isDone());
469                  threadAssertTrue(f.isCompletedAbnormally());
470                  threadAssertTrue(f.getException() instanceof FJException);
471              }};
472 <        mainPool.invoke(a);
472 >        testInvokeOnPool(mainPool(), a);
473      }
474  
475      /**
# Line 474 | Line 480 | public class ForkJoinTaskTest extends JS
480              public void compute() {
481                  try {
482                      AsyncFib f = new AsyncFib(8);
483 <                    f.cancel(true);
483 >                    threadAssertTrue(f.cancel(true));
484                      f.invoke();
485                      shouldThrow();
486                  } catch (CancellationException success) {
487                  }
488              }};
489 <        mainPool.invoke(a);
489 >        testInvokeOnPool(mainPool(), a);
490      }
491  
492      /**
# Line 491 | Line 497 | public class ForkJoinTaskTest extends JS
497              public void compute() {
498                  try {
499                      AsyncFib f = new AsyncFib(8);
500 <                    f.cancel(true);
501 <                    f.fork();
500 >                    threadAssertTrue(f.cancel(true));
501 >                    threadAssertSame(f, f.fork());
502                      f.join();
503                      shouldThrow();
504                  } catch (CancellationException success) {
505                  }
506              }};
507 <        mainPool.invoke(a);
507 >        testInvokeOnPool(mainPool(), a);
508      }
509  
510      /**
# Line 509 | Line 515 | public class ForkJoinTaskTest extends JS
515              public void compute() {
516                  try {
517                      AsyncFib f = new AsyncFib(8);
518 <                    f.cancel(true);
519 <                    f.fork();
518 >                    threadAssertTrue(f.cancel(true));
519 >                    threadAssertSame(f, f.fork());
520                      f.get();
521                      shouldThrow();
522                  } catch (CancellationException success) {
# Line 518 | Line 524 | public class ForkJoinTaskTest extends JS
524                      unexpectedException(ex);
525                  }
526              }};
527 <        mainPool.invoke(a);
527 >        testInvokeOnPool(mainPool(), a);
528      }
529  
530      /**
# Line 529 | Line 535 | public class ForkJoinTaskTest extends JS
535              public void compute() {
536                  try {
537                      AsyncFib f = new AsyncFib(8);
538 <                    f.cancel(true);
539 <                    f.fork();
538 >                    threadAssertTrue(f.cancel(true));
539 >                    threadAssertSame(f, f.fork());
540                      f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
541                      shouldThrow();
542                  } catch (CancellationException success) {
# Line 538 | Line 544 | public class ForkJoinTaskTest extends JS
544                      unexpectedException(ex);
545                  }
546              }};
547 <        mainPool.invoke(a);
547 >        testInvokeOnPool(mainPool(), a);
548      }
549  
550      /**
# Line 548 | Line 554 | public class ForkJoinTaskTest extends JS
554          RecursiveAction a = new RecursiveAction() {
555              public void compute() {
556                  AsyncFib f = new AsyncFib(8);
557 <                f.cancel(true);
558 <                f.fork();
557 >                threadAssertTrue(f.cancel(true));
558 >                threadAssertSame(f, f.fork());
559                  f.quietlyJoin();
560                  threadAssertTrue(f.isDone());
561                  threadAssertTrue(f.isCompletedAbnormally());
562                  threadAssertTrue(f.getException() instanceof CancellationException);
563              }};
564 <        mainPool.invoke(a);
564 >        testInvokeOnPool(mainPool(), a);
565      }
566  
567      /**
568       * getPool of executing task returns its pool
569       */
570      public void testGetPool() {
571 +        final ForkJoinPool mainPool = mainPool();
572          RecursiveAction a = new RecursiveAction() {
573              public void compute() {
574                  threadAssertTrue(getPool() == mainPool);
575              }};
576 <        mainPool.invoke(a);
576 >        testInvokeOnPool(mainPool, a);
577      }
578  
579      /**
# Line 577 | Line 584 | public class ForkJoinTaskTest extends JS
584              public void compute() {
585                  threadAssertTrue(getPool() == null);
586              }};
587 <        a.invoke();
587 >        assertNull(a.invoke());
588      }
589  
590      /**
# Line 588 | Line 595 | public class ForkJoinTaskTest extends JS
595              public void compute() {
596                  threadAssertTrue(inForkJoinPool());
597              }};
598 <        mainPool.invoke(a);
598 >        testInvokeOnPool(mainPool(), a);
599      }
600  
601      /**
# Line 599 | Line 606 | public class ForkJoinTaskTest extends JS
606              public void compute() {
607                  threadAssertTrue(!inForkJoinPool());
608              }};
609 <        a.invoke();
609 >        assertNull(a.invoke());
610      }
611  
612      /**
# Line 610 | Line 617 | public class ForkJoinTaskTest extends JS
617              public void compute() {
618                  setRawResult(null);
619              }};
620 <        a.invoke();
620 >        assertNull(a.invoke());
621      }
622  
623      /**
# Line 627 | Line 634 | public class ForkJoinTaskTest extends JS
634                  } catch (FJException success) {
635                  }
636              }};
637 <        mainPool.invoke(a);
637 >        testInvokeOnPool(mainPool(), a);
638      }
639  
640      /**
# Line 644 | Line 651 | public class ForkJoinTaskTest extends JS
651                  threadAssertTrue(g.isDone());
652                  threadAssertTrue(g.number == 34);
653              }};
654 <        mainPool.invoke(a);
654 >        testInvokeOnPool(mainPool(), a);
655      }
656  
657      /**
# Line 658 | Line 665 | public class ForkJoinTaskTest extends JS
665                  threadAssertTrue(f.isDone());
666                  threadAssertTrue(f.number == 21);
667              }};
668 <        mainPool.invoke(a);
668 >        testInvokeOnPool(mainPool(), a);
669      }
670  
671      /**
# Line 678 | Line 685 | public class ForkJoinTaskTest extends JS
685                  threadAssertTrue(h.isDone());
686                  threadAssertTrue(h.number == 13);
687              }};
688 <        mainPool.invoke(a);
688 >        testInvokeOnPool(mainPool(), a);
689      }
690  
691      /**
# Line 702 | Line 709 | public class ForkJoinTaskTest extends JS
709                  threadAssertTrue(h.isDone());
710                  threadAssertTrue(h.number == 13);
711              }};
712 <        mainPool.invoke(a);
712 >        testInvokeOnPool(mainPool(), a);
713      }
714  
715  
# Line 721 | Line 728 | public class ForkJoinTaskTest extends JS
728                  } catch (NullPointerException success) {
729                  }
730              }};
731 <        mainPool.invoke(a);
731 >        testInvokeOnPool(mainPool(), a);
732      }
733  
734      /**
# Line 738 | Line 745 | public class ForkJoinTaskTest extends JS
745                  } catch (FJException success) {
746                  }
747              }};
748 <        mainPool.invoke(a);
748 >        testInvokeOnPool(mainPool(), a);
749      }
750  
751      /**
# Line 754 | Line 761 | public class ForkJoinTaskTest extends JS
761                  } catch (FJException success) {
762                  }
763              }};
764 <        mainPool.invoke(a);
764 >        testInvokeOnPool(mainPool(), a);
765      }
766  
767      /**
# Line 772 | Line 779 | public class ForkJoinTaskTest extends JS
779                  } catch (FJException success) {
780                  }
781              }};
782 <        mainPool.invoke(a);
782 >        testInvokeOnPool(mainPool(), a);
783      }
784  
785      /**
# Line 794 | Line 801 | public class ForkJoinTaskTest extends JS
801                  } catch (FJException success) {
802                  }
803              }};
804 <        mainPool.invoke(a);
804 >        testInvokeOnPool(mainPool(), a);
805      }
806  
807      /**
# Line 805 | Line 812 | public class ForkJoinTaskTest extends JS
812          RecursiveAction a = new RecursiveAction() {
813              public void compute() {
814                  AsyncFib g = new AsyncFib(9);
815 <                g.fork();
815 >                threadAssertSame(g, g.fork());
816                  AsyncFib f = new AsyncFib(8);
817 <                f.fork();
817 >                threadAssertSame(f, f.fork());
818                  threadAssertTrue(f.tryUnfork());
819                  helpQuiesce();
820                  threadAssertFalse(f.isDone());
821                  threadAssertTrue(g.isDone());
822              }};
823 <        singletonPool.invoke(a);
823 >        testInvokeOnPool(singletonPool(), a);
824      }
825  
826      /**
# Line 824 | Line 831 | public class ForkJoinTaskTest extends JS
831          RecursiveAction a = new RecursiveAction() {
832              public void compute() {
833                  AsyncFib h = new AsyncFib(7);
834 <                h.fork();
834 >                threadAssertSame(h, h.fork());
835                  AsyncFib g = new AsyncFib(9);
836 <                g.fork();
836 >                threadAssertSame(g, g.fork());
837                  AsyncFib f = new AsyncFib(8);
838 <                f.fork();
838 >                threadAssertSame(f, f.fork());
839                  threadAssertTrue(getSurplusQueuedTaskCount() > 0);
840                  helpQuiesce();
841              }};
842 <        singletonPool.invoke(a);
842 >        testInvokeOnPool(singletonPool(), a);
843      }
844  
845      /**
# Line 842 | Line 849 | public class ForkJoinTaskTest extends JS
849          RecursiveAction a = new RecursiveAction() {
850              public void compute() {
851                  AsyncFib g = new AsyncFib(9);
852 <                g.fork();
852 >                threadAssertSame(g, g.fork());
853                  AsyncFib f = new AsyncFib(8);
854 <                f.fork();
854 >                threadAssertSame(f, f.fork());
855                  threadAssertTrue(peekNextLocalTask() == f);
856 <                f.join();
856 >                threadAssertNull(f.join());
857                  threadAssertTrue(f.isDone());
858                  helpQuiesce();
859              }};
860 <        singletonPool.invoke(a);
860 >        testInvokeOnPool(singletonPool(), a);
861      }
862  
863      /**
# Line 861 | Line 868 | public class ForkJoinTaskTest extends JS
868          RecursiveAction a = new RecursiveAction() {
869              public void compute() {
870                  AsyncFib g = new AsyncFib(9);
871 <                g.fork();
871 >                threadAssertSame(g, g.fork());
872                  AsyncFib f = new AsyncFib(8);
873 <                f.fork();
873 >                threadAssertSame(f, f.fork());
874                  threadAssertTrue(pollNextLocalTask() == f);
875                  helpQuiesce();
876                  threadAssertFalse(f.isDone());
877              }};
878 <        singletonPool.invoke(a);
878 >        testInvokeOnPool(singletonPool(), a);
879      }
880  
881      /**
# Line 879 | Line 886 | public class ForkJoinTaskTest extends JS
886          RecursiveAction a = new RecursiveAction() {
887              public void compute() {
888                  AsyncFib g = new AsyncFib(9);
889 <                g.fork();
889 >                threadAssertSame(g, g.fork());
890                  AsyncFib f = new AsyncFib(8);
891 <                f.fork();
891 >                threadAssertSame(f, f.fork());
892                  threadAssertTrue(pollTask() == f);
893                  helpQuiesce();
894                  threadAssertFalse(f.isDone());
895                  threadAssertTrue(g.isDone());
896              }};
897 <        singletonPool.invoke(a);
897 >        testInvokeOnPool(singletonPool(), a);
898      }
899  
900      /**
# Line 897 | Line 904 | public class ForkJoinTaskTest extends JS
904          RecursiveAction a = new RecursiveAction() {
905              public void compute() {
906                  AsyncFib g = new AsyncFib(9);
907 <                g.fork();
907 >                threadAssertSame(g, g.fork());
908                  AsyncFib f = new AsyncFib(8);
909 <                f.fork();
909 >                threadAssertSame(f, f.fork());
910                  threadAssertTrue(peekNextLocalTask() == g);
911 <                f.join();
911 >                threadAssertNull(f.join());
912                  helpQuiesce();
913                  threadAssertTrue(f.isDone());
914              }};
915 <        asyncSingletonPool.invoke(a);
915 >        testInvokeOnPool(asyncSingletonPool(), a);
916      }
917  
918      /**
# Line 916 | Line 923 | public class ForkJoinTaskTest extends JS
923          RecursiveAction a = new RecursiveAction() {
924              public void compute() {
925                  AsyncFib g = new AsyncFib(9);
926 <                g.fork();
926 >                threadAssertSame(g, g.fork());
927                  AsyncFib f = new AsyncFib(8);
928 <                f.fork();
928 >                threadAssertSame(f, f.fork());
929                  threadAssertTrue(pollNextLocalTask() == g);
930                  helpQuiesce();
931                  threadAssertTrue(f.isDone());
932                  threadAssertFalse(g.isDone());
933              }};
934 <        asyncSingletonPool.invoke(a);
934 >        testInvokeOnPool(asyncSingletonPool(), a);
935      }
936  
937      /**
# Line 935 | Line 942 | public class ForkJoinTaskTest extends JS
942          RecursiveAction a = new RecursiveAction() {
943              public void compute() {
944                  AsyncFib g = new AsyncFib(9);
945 <                g.fork();
945 >                threadAssertSame(g, g.fork());
946                  AsyncFib f = new AsyncFib(8);
947 <                f.fork();
947 >                threadAssertSame(f, f.fork());
948                  threadAssertTrue(pollTask() == g);
949                  helpQuiesce();
950                  threadAssertTrue(f.isDone());
951                  threadAssertFalse(g.isDone());
952              }};
953 <        asyncSingletonPool.invoke(a);
953 >        testInvokeOnPool(asyncSingletonPool(), a);
954      }
955   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines