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.33 by jsr166, Fri Jun 24 18:49:56 2011 UTC vs.
Revision 1.40 by jsr166, Wed Dec 31 19:05:43 2014 UTC

# Line 4 | Line 4
4   * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7 < import junit.framework.*;
7 > import static java.util.concurrent.TimeUnit.SECONDS;
8 >
9 > import java.util.Arrays;
10 > import java.util.HashSet;
11   import java.util.concurrent.CancellationException;
9 import java.util.concurrent.SynchronousQueue;
12   import java.util.concurrent.ExecutionException;
13   import java.util.concurrent.ForkJoinPool;
14   import java.util.concurrent.ForkJoinTask;
15   import java.util.concurrent.ForkJoinWorkerThread;
16   import java.util.concurrent.RecursiveAction;
17 < import java.util.concurrent.TimeUnit;
17 > import java.util.concurrent.SynchronousQueue;
18 > import java.util.concurrent.ThreadLocalRandom;
19   import java.util.concurrent.TimeoutException;
20 < import static java.util.concurrent.TimeUnit.SECONDS;
21 < import java.util.HashSet;
20 > import java.util.concurrent.TimeUnit;
21 >
22 > import junit.framework.Test;
23 > import junit.framework.TestSuite;
24  
25   public class RecursiveActionTest extends JSR166TestCase {
26  
# Line 171 | Line 176 | public class RecursiveActionTest extends
176          final int number;
177          int result;
178          FibAction(int n) { number = n; }
179 <        public void realCompute() {
179 >        protected void realCompute() {
180              int n = number;
181              if (n <= 1)
182                  result = n;
# Line 209 | Line 214 | public class RecursiveActionTest extends
214       */
215      public void testInvoke() {
216          RecursiveAction a = new CheckedRecursiveAction() {
217 <            public void realCompute() {
217 >            protected void realCompute() {
218                  FibAction f = new FibAction(8);
219                  assertNull(f.invoke());
220                  assertEquals(21, f.result);
# Line 225 | Line 230 | public class RecursiveActionTest extends
230       */
231      public void testQuietlyInvoke() {
232          RecursiveAction a = new CheckedRecursiveAction() {
233 <            public void realCompute() {
233 >            protected void realCompute() {
234                  FibAction f = new FibAction(8);
235                  f.quietlyInvoke();
236                  assertEquals(21, f.result);
# Line 239 | Line 244 | public class RecursiveActionTest extends
244       */
245      public void testForkJoin() {
246          RecursiveAction a = new CheckedRecursiveAction() {
247 <            public void realCompute() {
247 >            protected void realCompute() {
248                  FibAction f = new FibAction(8);
249                  assertSame(f, f.fork());
250                  assertNull(f.join());
# Line 254 | Line 259 | public class RecursiveActionTest extends
259       */
260      public void testJoinIgnoresInterrupts() {
261          RecursiveAction a = new CheckedRecursiveAction() {
262 <            public void realCompute() {
262 >            protected void realCompute() {
263                  FibAction f = new FibAction(8);
264                  final Thread myself = Thread.currentThread();
265  
# Line 334 | Line 339 | public class RecursiveActionTest extends
339          final SynchronousQueue<FibAction[]> sq =
340              new SynchronousQueue<FibAction[]>();
341          RecursiveAction a = new CheckedRecursiveAction() {
342 <            public void realCompute() throws InterruptedException {
342 >            protected void realCompute() throws InterruptedException {
343                  FibAction[] fibActions = new FibAction[6];
344                  for (int i = 0; i < fibActions.length; i++)
345                      fibActions[i] = new FibAction(8);
# Line 434 | Line 439 | public class RecursiveActionTest extends
439       */
440      public void testForkGet() {
441          RecursiveAction a = new CheckedRecursiveAction() {
442 <            public void realCompute() throws Exception {
442 >            protected void realCompute() throws Exception {
443                  FibAction f = new FibAction(8);
444                  assertSame(f, f.fork());
445                  assertNull(f.get());
# Line 449 | Line 454 | public class RecursiveActionTest extends
454       */
455      public void testForkTimedGet() {
456          RecursiveAction a = new CheckedRecursiveAction() {
457 <            public void realCompute() throws Exception {
457 >            protected void realCompute() throws Exception {
458                  FibAction f = new FibAction(8);
459                  assertSame(f, f.fork());
460                  assertNull(f.get(5L, SECONDS));
# Line 464 | Line 469 | public class RecursiveActionTest extends
469       */
470      public void testForkTimedGetNPE() {
471          RecursiveAction a = new CheckedRecursiveAction() {
472 <            public void realCompute() throws Exception {
472 >            protected void realCompute() throws Exception {
473                  FibAction f = new FibAction(8);
474                  assertSame(f, f.fork());
475                  try {
# Line 480 | Line 485 | public class RecursiveActionTest extends
485       */
486      public void testForkQuietlyJoin() {
487          RecursiveAction a = new CheckedRecursiveAction() {
488 <            public void realCompute() {
488 >            protected void realCompute() {
489                  FibAction f = new FibAction(8);
490                  assertSame(f, f.fork());
491                  f.quietlyJoin();
# Line 496 | Line 501 | public class RecursiveActionTest extends
501       */
502      public void testForkHelpQuiesce() {
503          RecursiveAction a = new CheckedRecursiveAction() {
504 <            public void realCompute() {
504 >            protected void realCompute() {
505                  FibAction f = new FibAction(8);
506                  assertSame(f, f.fork());
507                  helpQuiesce();
# Line 512 | Line 517 | public class RecursiveActionTest extends
517       */
518      public void testAbnormalInvoke() {
519          RecursiveAction a = new CheckedRecursiveAction() {
520 <            public void realCompute() {
520 >            protected void realCompute() {
521                  FailingFibAction f = new FailingFibAction(8);
522                  try {
523                      f.invoke();
# Line 529 | Line 534 | public class RecursiveActionTest extends
534       */
535      public void testAbnormalQuietlyInvoke() {
536          RecursiveAction a = new CheckedRecursiveAction() {
537 <            public void realCompute() {
537 >            protected void realCompute() {
538                  FailingFibAction f = new FailingFibAction(8);
539                  f.quietlyInvoke();
540                  assertTrue(f.getException() instanceof FJException);
# Line 543 | Line 548 | public class RecursiveActionTest extends
548       */
549      public void testAbnormalForkJoin() {
550          RecursiveAction a = new CheckedRecursiveAction() {
551 <            public void realCompute() {
551 >            protected void realCompute() {
552                  FailingFibAction f = new FailingFibAction(8);
553                  assertSame(f, f.fork());
554                  try {
# Line 561 | Line 566 | public class RecursiveActionTest extends
566       */
567      public void testAbnormalForkGet() {
568          RecursiveAction a = new CheckedRecursiveAction() {
569 <            public void realCompute() throws Exception {
569 >            protected void realCompute() throws Exception {
570                  FailingFibAction f = new FailingFibAction(8);
571                  assertSame(f, f.fork());
572                  try {
# Line 581 | Line 586 | public class RecursiveActionTest extends
586       */
587      public void testAbnormalForkTimedGet() {
588          RecursiveAction a = new CheckedRecursiveAction() {
589 <            public void realCompute() throws Exception {
589 >            protected void realCompute() throws Exception {
590                  FailingFibAction f = new FailingFibAction(8);
591                  assertSame(f, f.fork());
592                  try {
# Line 601 | Line 606 | public class RecursiveActionTest extends
606       */
607      public void testAbnormalForkQuietlyJoin() {
608          RecursiveAction a = new CheckedRecursiveAction() {
609 <            public void realCompute() {
609 >            protected void realCompute() {
610                  FailingFibAction f = new FailingFibAction(8);
611                  assertSame(f, f.fork());
612                  f.quietlyJoin();
# Line 616 | Line 621 | public class RecursiveActionTest extends
621       */
622      public void testCancelledInvoke() {
623          RecursiveAction a = new CheckedRecursiveAction() {
624 <            public void realCompute() {
624 >            protected void realCompute() {
625                  FibAction f = new FibAction(8);
626                  assertTrue(f.cancel(true));
627                  try {
# Line 634 | Line 639 | public class RecursiveActionTest extends
639       */
640      public void testCancelledForkJoin() {
641          RecursiveAction a = new CheckedRecursiveAction() {
642 <            public void realCompute() {
642 >            protected void realCompute() {
643                  FibAction f = new FibAction(8);
644                  assertTrue(f.cancel(true));
645                  assertSame(f, f.fork());
# Line 653 | Line 658 | public class RecursiveActionTest extends
658       */
659      public void testCancelledForkGet() {
660          RecursiveAction a = new CheckedRecursiveAction() {
661 <            public void realCompute() throws Exception {
661 >            protected void realCompute() throws Exception {
662                  FibAction f = new FibAction(8);
663                  assertTrue(f.cancel(true));
664                  assertSame(f, f.fork());
# Line 672 | Line 677 | public class RecursiveActionTest extends
677       */
678      public void testCancelledForkTimedGet() {
679          RecursiveAction a = new CheckedRecursiveAction() {
680 <            public void realCompute() throws Exception {
680 >            protected void realCompute() throws Exception {
681                  FibAction f = new FibAction(8);
682                  assertTrue(f.cancel(true));
683                  assertSame(f, f.fork());
# Line 691 | Line 696 | public class RecursiveActionTest extends
696       */
697      public void testCancelledForkQuietlyJoin() {
698          RecursiveAction a = new CheckedRecursiveAction() {
699 <            public void realCompute() {
699 >            protected void realCompute() {
700                  FibAction f = new FibAction(8);
701                  assertTrue(f.cancel(true));
702                  assertSame(f, f.fork());
# Line 707 | Line 712 | public class RecursiveActionTest extends
712      public void testGetPool() {
713          final ForkJoinPool mainPool = mainPool();
714          RecursiveAction a = new CheckedRecursiveAction() {
715 <            public void realCompute() {
715 >            protected void realCompute() {
716                  assertSame(mainPool, getPool());
717              }};
718          testInvokeOnPool(mainPool, a);
# Line 718 | Line 723 | public class RecursiveActionTest extends
723       */
724      public void testGetPool2() {
725          RecursiveAction a = new CheckedRecursiveAction() {
726 <            public void realCompute() {
726 >            protected void realCompute() {
727                  assertNull(getPool());
728              }};
729          assertNull(a.invoke());
# Line 729 | Line 734 | public class RecursiveActionTest extends
734       */
735      public void testInForkJoinPool() {
736          RecursiveAction a = new CheckedRecursiveAction() {
737 <            public void realCompute() {
737 >            protected void realCompute() {
738                  assertTrue(inForkJoinPool());
739              }};
740          testInvokeOnPool(mainPool(), a);
# Line 740 | Line 745 | public class RecursiveActionTest extends
745       */
746      public void testInForkJoinPool2() {
747          RecursiveAction a = new CheckedRecursiveAction() {
748 <            public void realCompute() {
748 >            protected void realCompute() {
749                  assertFalse(inForkJoinPool());
750              }};
751          assertNull(a.invoke());
# Line 752 | Line 757 | public class RecursiveActionTest extends
757      public void testWorkerGetPool() {
758          final ForkJoinPool mainPool = mainPool();
759          RecursiveAction a = new CheckedRecursiveAction() {
760 <            public void realCompute() {
760 >            protected void realCompute() {
761                  ForkJoinWorkerThread w =
762                      (ForkJoinWorkerThread) Thread.currentThread();
763                  assertSame(mainPool, w.getPool());
# Line 766 | Line 771 | public class RecursiveActionTest extends
771      public void testWorkerGetPoolIndex() {
772          final ForkJoinPool mainPool = mainPool();
773          RecursiveAction a = new CheckedRecursiveAction() {
774 <            public void realCompute() {
774 >            protected void realCompute() {
775                  ForkJoinWorkerThread w =
776                      (ForkJoinWorkerThread) Thread.currentThread();
777                  assertTrue(w.getPoolIndex() >= 0);
# Line 781 | Line 786 | public class RecursiveActionTest extends
786       */
787      public void testSetRawResult() {
788          RecursiveAction a = new CheckedRecursiveAction() {
789 <            public void realCompute() {
789 >            protected void realCompute() {
790                  setRawResult(null);
791                  assertNull(getRawResult());
792              }};
# Line 793 | Line 798 | public class RecursiveActionTest extends
798       */
799      public void testReinitialize() {
800          RecursiveAction a = new CheckedRecursiveAction() {
801 <            public void realCompute() {
801 >            protected void realCompute() {
802                  FibAction f = new FibAction(8);
803                  checkNotDone(f);
804  
# Line 813 | Line 818 | public class RecursiveActionTest extends
818       */
819      public void testReinitializeAbnormal() {
820          RecursiveAction a = new CheckedRecursiveAction() {
821 <            public void realCompute() {
821 >            protected void realCompute() {
822                  FailingFibAction f = new FailingFibAction(8);
823                  checkNotDone(f);
824  
# Line 836 | Line 841 | public class RecursiveActionTest extends
841       */
842      public void testCompleteExceptionally() {
843          RecursiveAction a = new CheckedRecursiveAction() {
844 <            public void realCompute() {
844 >            protected void realCompute() {
845                  FibAction f = new FibAction(8);
846                  f.completeExceptionally(new FJException());
847                  try {
# Line 854 | Line 859 | public class RecursiveActionTest extends
859       */
860      public void testComplete() {
861          RecursiveAction a = new CheckedRecursiveAction() {
862 <            public void realCompute() {
862 >            protected void realCompute() {
863                  FibAction f = new FibAction(8);
864                  f.complete(null);
865                  assertNull(f.invoke());
# Line 869 | Line 874 | public class RecursiveActionTest extends
874       */
875      public void testInvokeAll2() {
876          RecursiveAction a = new CheckedRecursiveAction() {
877 <            public void realCompute() {
877 >            protected void realCompute() {
878                  FibAction f = new FibAction(8);
879                  FibAction g = new FibAction(9);
880                  invokeAll(f, g);
# Line 886 | Line 891 | public class RecursiveActionTest extends
891       */
892      public void testInvokeAll1() {
893          RecursiveAction a = new CheckedRecursiveAction() {
894 <            public void realCompute() {
894 >            protected void realCompute() {
895                  FibAction f = new FibAction(8);
896                  invokeAll(f);
897                  checkCompletedNormally(f);
# Line 900 | Line 905 | public class RecursiveActionTest extends
905       */
906      public void testInvokeAll3() {
907          RecursiveAction a = new CheckedRecursiveAction() {
908 <            public void realCompute() {
908 >            protected void realCompute() {
909                  FibAction f = new FibAction(8);
910                  FibAction g = new FibAction(9);
911                  FibAction h = new FibAction(7);
# Line 923 | Line 928 | public class RecursiveActionTest extends
928       */
929      public void testInvokeAllCollection() {
930          RecursiveAction a = new CheckedRecursiveAction() {
931 <            public void realCompute() {
931 >            protected void realCompute() {
932                  FibAction f = new FibAction(8);
933                  FibAction g = new FibAction(9);
934                  FibAction h = new FibAction(7);
# Line 950 | Line 955 | public class RecursiveActionTest extends
955       */
956      public void testInvokeAllNPE() {
957          RecursiveAction a = new CheckedRecursiveAction() {
958 <            public void realCompute() {
958 >            protected void realCompute() {
959                  FibAction f = new FibAction(8);
960                  FibAction g = new FibAction(9);
961                  FibAction h = null;
# Line 967 | Line 972 | public class RecursiveActionTest extends
972       */
973      public void testAbnormalInvokeAll2() {
974          RecursiveAction a = new CheckedRecursiveAction() {
975 <            public void realCompute() {
975 >            protected void realCompute() {
976                  FibAction f = new FibAction(8);
977                  FailingFibAction g = new FailingFibAction(9);
978                  try {
# Line 985 | Line 990 | public class RecursiveActionTest extends
990       */
991      public void testAbnormalInvokeAll1() {
992          RecursiveAction a = new CheckedRecursiveAction() {
993 <            public void realCompute() {
993 >            protected void realCompute() {
994                  FailingFibAction g = new FailingFibAction(9);
995                  try {
996                      invokeAll(g);
# Line 1002 | Line 1007 | public class RecursiveActionTest extends
1007       */
1008      public void testAbnormalInvokeAll3() {
1009          RecursiveAction a = new CheckedRecursiveAction() {
1010 <            public void realCompute() {
1010 >            protected void realCompute() {
1011                  FibAction f = new FibAction(8);
1012                  FailingFibAction g = new FailingFibAction(9);
1013                  FibAction h = new FibAction(7);
# Line 1021 | Line 1026 | public class RecursiveActionTest extends
1026       */
1027      public void testAbnormalInvokeAllCollection() {
1028          RecursiveAction a = new CheckedRecursiveAction() {
1029 <            public void realCompute() {
1029 >            protected void realCompute() {
1030                  FailingFibAction f = new FailingFibAction(8);
1031                  FibAction g = new FibAction(9);
1032                  FibAction h = new FibAction(7);
# Line 1045 | Line 1050 | public class RecursiveActionTest extends
1050       */
1051      public void testTryUnfork() {
1052          RecursiveAction a = new CheckedRecursiveAction() {
1053 <            public void realCompute() {
1053 >            protected void realCompute() {
1054                  FibAction g = new FibAction(9);
1055                  assertSame(g, g.fork());
1056                  FibAction f = new FibAction(8);
# Line 1064 | Line 1069 | public class RecursiveActionTest extends
1069       */
1070      public void testGetSurplusQueuedTaskCount() {
1071          RecursiveAction a = new CheckedRecursiveAction() {
1072 <            public void realCompute() {
1072 >            protected void realCompute() {
1073                  FibAction h = new FibAction(7);
1074                  assertSame(h, h.fork());
1075                  FibAction g = new FibAction(9);
# Line 1086 | Line 1091 | public class RecursiveActionTest extends
1091       */
1092      public void testPeekNextLocalTask() {
1093          RecursiveAction a = new CheckedRecursiveAction() {
1094 <            public void realCompute() {
1094 >            protected void realCompute() {
1095                  FibAction g = new FibAction(9);
1096                  assertSame(g, g.fork());
1097                  FibAction f = new FibAction(8);
# Line 1107 | Line 1112 | public class RecursiveActionTest extends
1112       */
1113      public void testPollNextLocalTask() {
1114          RecursiveAction a = new CheckedRecursiveAction() {
1115 <            public void realCompute() {
1115 >            protected void realCompute() {
1116                  FibAction g = new FibAction(9);
1117                  assertSame(g, g.fork());
1118                  FibAction f = new FibAction(8);
# Line 1125 | Line 1130 | public class RecursiveActionTest extends
1130       */
1131      public void testPollTask() {
1132          RecursiveAction a = new CheckedRecursiveAction() {
1133 <            public void realCompute() {
1133 >            protected void realCompute() {
1134                  FibAction g = new FibAction(9);
1135                  assertSame(g, g.fork());
1136                  FibAction f = new FibAction(8);
# Line 1143 | Line 1148 | public class RecursiveActionTest extends
1148       */
1149      public void testPeekNextLocalTaskAsync() {
1150          RecursiveAction a = new CheckedRecursiveAction() {
1151 <            public void realCompute() {
1151 >            protected void realCompute() {
1152                  FibAction g = new FibAction(9);
1153                  assertSame(g, g.fork());
1154                  FibAction f = new FibAction(8);
# Line 1163 | Line 1168 | public class RecursiveActionTest extends
1168       */
1169      public void testPollNextLocalTaskAsync() {
1170          RecursiveAction a = new CheckedRecursiveAction() {
1171 <            public void realCompute() {
1171 >            protected void realCompute() {
1172                  FibAction g = new FibAction(9);
1173                  assertSame(g, g.fork());
1174                  FibAction f = new FibAction(8);
# Line 1182 | Line 1187 | public class RecursiveActionTest extends
1187       */
1188      public void testPollTaskAsync() {
1189          RecursiveAction a = new CheckedRecursiveAction() {
1190 <            public void realCompute() {
1190 >            protected void realCompute() {
1191                  FibAction g = new FibAction(9);
1192                  assertSame(g, g.fork());
1193                  FibAction f = new FibAction(8);
# Line 1195 | Line 1200 | public class RecursiveActionTest extends
1200          testInvokeOnPool(asyncSingletonPool(), a);
1201      }
1202  
1203 +    /** Demo from RecursiveAction javadoc */
1204 +    static class SortTask extends RecursiveAction {
1205 +        final long[] array; final int lo, hi;
1206 +        SortTask(long[] array, int lo, int hi) {
1207 +            this.array = array; this.lo = lo; this.hi = hi;
1208 +        }
1209 +        SortTask(long[] array) { this(array, 0, array.length); }
1210 +        protected void compute() {
1211 +            if (hi - lo < THRESHOLD)
1212 +                sortSequentially(lo, hi);
1213 +            else {
1214 +                int mid = (lo + hi) >>> 1;
1215 +                invokeAll(new SortTask(array, lo, mid),
1216 +                          new SortTask(array, mid, hi));
1217 +                merge(lo, mid, hi);
1218 +            }
1219 +        }
1220 +        // implementation details follow:
1221 +        static final int THRESHOLD = 100;
1222 +        void sortSequentially(int lo, int hi) {
1223 +            Arrays.sort(array, lo, hi);
1224 +        }
1225 +        void merge(int lo, int mid, int hi) {
1226 +            long[] buf = Arrays.copyOfRange(array, lo, mid);
1227 +            for (int i = 0, j = lo, k = mid; i < buf.length; j++)
1228 +                array[j] = (k == hi || buf[i] < array[k]) ?
1229 +                    buf[i++] : array[k++];
1230 +        }
1231 +    }
1232 +
1233 +    /**
1234 +     * SortTask demo works as advertised
1235 +     */
1236 +    public void testSortTaskDemo() {
1237 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
1238 +        long[] array = new long[1007];
1239 +        for (int i = 0; i < array.length; i++)
1240 +            array[i] = rnd.nextLong();
1241 +        long[] arrayClone = array.clone();
1242 +        testInvokeOnPool(mainPool(), new SortTask(array));
1243 +        Arrays.sort(arrayClone);
1244 +        assertTrue(Arrays.equals(array, arrayClone));
1245 +    }
1246   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines