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

Comparing jsr166/src/test/tck/ForkJoinPool8Test.java (file contents):
Revision 1.4 by dl, Thu Mar 21 19:06:54 2013 UTC vs.
Revision 1.36 by jsr166, Mon May 29 19:15:02 2017 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.MILLISECONDS;
8 >
9 > import java.util.HashSet;
10   import java.util.concurrent.CancellationException;
11 + import java.util.concurrent.CountedCompleter;
12   import java.util.concurrent.ExecutionException;
13   import java.util.concurrent.ForkJoinPool;
14   import java.util.concurrent.ForkJoinTask;
12 import java.util.concurrent.ForkJoinWorkerThread;
15   import java.util.concurrent.RecursiveAction;
14 import java.util.concurrent.CountedCompleter;
15 import java.util.concurrent.ThreadLocalRandom;
16 import java.util.concurrent.TimeUnit;
16   import java.util.concurrent.TimeoutException;
17 < import static java.util.concurrent.TimeUnit.SECONDS;
18 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
19 < import java.util.Arrays;
21 < import java.util.HashSet;
17 >
18 > import junit.framework.Test;
19 > import junit.framework.TestSuite;
20  
21   public class ForkJoinPool8Test extends JSR166TestCase {
22      public static void main(String[] args) {
23 <        junit.textui.TestRunner.run(suite());
23 >        main(suite(), args);
24      }
25  
26      public static Test suite() {
# Line 59 | Line 57 | public class ForkJoinPool8Test extends J
57       * RecursiveAction and CountedCompleter, but with all actions
58       * executed in the common pool, generally implicitly via
59       * checkInvoke.
60 <     */
60 >     */
61  
62      private void checkInvoke(ForkJoinTask a) {
63          checkNotDone(a);
# Line 85 | Line 83 | public class ForkJoinPool8Test extends J
83  
84              Thread.currentThread().interrupt();
85              try {
86 <                a.get(5L, SECONDS);
86 >                a.get(randomTimeout(), randomTimeUnit());
87                  shouldThrow();
88              } catch (InterruptedException success) {
89              } catch (Throwable fail) { threadUnexpectedException(fail); }
90          }
91  
92          try {
93 <            a.get(0L, SECONDS);
93 >            a.get(randomExpiredTimeout(), randomTimeUnit());
94              shouldThrow();
95          } catch (TimeoutException success) {
96          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 110 | Line 108 | public class ForkJoinPool8Test extends J
108          assertFalse(a.cancel(true));
109          try {
110              assertNull(a.get());
111 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
114 <        try {
115 <            assertNull(a.get(5L, SECONDS));
111 >            assertNull(a.get(randomTimeout(), randomTimeUnit()));
112          } catch (Throwable fail) { threadUnexpectedException(fail); }
113      }
114  
# Line 137 | Line 133 | public class ForkJoinPool8Test extends J
133          } catch (Throwable fail) { threadUnexpectedException(fail); }
134  
135          try {
136 <            a.get(5L, SECONDS);
136 >            a.get(randomTimeout(), randomTimeUnit());
137              shouldThrow();
138          } catch (CancellationException success) {
139          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 168 | Line 164 | public class ForkJoinPool8Test extends J
164          } catch (Throwable fail) { threadUnexpectedException(fail); }
165  
166          try {
167 <            a.get(5L, SECONDS);
167 >            a.get(randomTimeout(), randomTimeUnit());
168              shouldThrow();
169          } catch (ExecutionException success) {
170              assertSame(t.getClass(), success.getCause().getClass());
# Line 180 | Line 176 | public class ForkJoinPool8Test extends J
176          public FJException(Throwable cause) { super(cause); }
177      }
178  
179 <    // A simple recursive action for testing
179 >    /** A simple recursive action for testing. */
180      final class FibAction extends CheckedRecursiveAction {
181          final int number;
182          int result;
183          FibAction(int n) { number = n; }
184 <        public void realCompute() {
184 >        protected void realCompute() {
185              int n = number;
186              if (n <= 1)
187                  result = n;
# Line 198 | Line 194 | public class ForkJoinPool8Test extends J
194          }
195      }
196  
197 <    // A recursive action failing in base case
197 >    /** A recursive action failing in base case. */
198      static final class FailingFibAction extends RecursiveAction {
199          final int number;
200          int result;
# Line 223 | Line 219 | public class ForkJoinPool8Test extends J
219       */
220      public void testInvoke() {
221          RecursiveAction a = new CheckedRecursiveAction() {
222 <            public void realCompute() {
222 >            protected void realCompute() {
223                  FibAction f = new FibAction(8);
224                  assertNull(f.invoke());
225                  assertEquals(21, f.result);
# Line 239 | Line 235 | public class ForkJoinPool8Test extends J
235       */
236      public void testQuietlyInvoke() {
237          RecursiveAction a = new CheckedRecursiveAction() {
238 <            public void realCompute() {
238 >            protected void realCompute() {
239                  FibAction f = new FibAction(8);
240                  f.quietlyInvoke();
241                  assertEquals(21, f.result);
# Line 253 | Line 249 | public class ForkJoinPool8Test extends J
249       */
250      public void testForkJoin() {
251          RecursiveAction a = new CheckedRecursiveAction() {
252 <            public void realCompute() {
252 >            protected void realCompute() {
253                  FibAction f = new FibAction(8);
254                  assertSame(f, f.fork());
255                  assertNull(f.join());
# Line 268 | Line 264 | public class ForkJoinPool8Test extends J
264       */
265      public void testJoinIgnoresInterrupts() {
266          RecursiveAction a = new CheckedRecursiveAction() {
267 <            public void realCompute() {
267 >            protected void realCompute() {
268                  FibAction f = new FibAction(8);
269 <                final Thread myself = Thread.currentThread();
269 >                final Thread currentThread = Thread.currentThread();
270  
271                  // test join()
272                  assertSame(f, f.fork());
273 <                myself.interrupt();
278 <                assertTrue(myself.isInterrupted());
273 >                currentThread.interrupt();
274                  assertNull(f.join());
275                  Thread.interrupted();
276                  assertEquals(21, f.result);
# Line 284 | Line 279 | public class ForkJoinPool8Test extends J
279                  f = new FibAction(8);
280                  f.cancel(true);
281                  assertSame(f, f.fork());
282 <                myself.interrupt();
288 <                assertTrue(myself.isInterrupted());
282 >                currentThread.interrupt();
283                  try {
284                      f.join();
285                      shouldThrow();
# Line 297 | Line 291 | public class ForkJoinPool8Test extends J
291                  f = new FibAction(8);
292                  f.completeExceptionally(new FJException());
293                  assertSame(f, f.fork());
294 <                myself.interrupt();
301 <                assertTrue(myself.isInterrupted());
294 >                currentThread.interrupt();
295                  try {
296                      f.join();
297                      shouldThrow();
# Line 310 | Line 303 | public class ForkJoinPool8Test extends J
303                  // test quietlyJoin()
304                  f = new FibAction(8);
305                  assertSame(f, f.fork());
306 <                myself.interrupt();
314 <                assertTrue(myself.isInterrupted());
306 >                currentThread.interrupt();
307                  f.quietlyJoin();
308                  Thread.interrupted();
309                  assertEquals(21, f.result);
# Line 320 | Line 312 | public class ForkJoinPool8Test extends J
312                  f = new FibAction(8);
313                  f.cancel(true);
314                  assertSame(f, f.fork());
315 <                myself.interrupt();
324 <                assertTrue(myself.isInterrupted());
315 >                currentThread.interrupt();
316                  f.quietlyJoin();
317                  Thread.interrupted();
318                  checkCancelled(f);
# Line 329 | Line 320 | public class ForkJoinPool8Test extends J
320                  f = new FibAction(8);
321                  f.completeExceptionally(new FJException());
322                  assertSame(f, f.fork());
323 <                myself.interrupt();
333 <                assertTrue(myself.isInterrupted());
323 >                currentThread.interrupt();
324                  f.quietlyJoin();
325                  Thread.interrupted();
326                  checkCompletedAbnormally(f, f.getException());
# Line 340 | Line 330 | public class ForkJoinPool8Test extends J
330          checkInvoke(a);
331      }
332  
343
333      /**
334       * get of a forked task returns when task completes
335       */
336      public void testForkGet() {
337          RecursiveAction a = new CheckedRecursiveAction() {
338 <            public void realCompute() throws Exception {
338 >            protected void realCompute() throws Exception {
339                  FibAction f = new FibAction(8);
340                  assertSame(f, f.fork());
341                  assertNull(f.get());
# Line 361 | Line 350 | public class ForkJoinPool8Test extends J
350       */
351      public void testForkTimedGet() {
352          RecursiveAction a = new CheckedRecursiveAction() {
353 <            public void realCompute() throws Exception {
353 >            protected void realCompute() throws Exception {
354                  FibAction f = new FibAction(8);
355                  assertSame(f, f.fork());
356 <                assertNull(f.get(5L, SECONDS));
356 >                assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
357                  assertEquals(21, f.result);
358                  checkCompletedNormally(f);
359              }};
# Line 376 | Line 365 | public class ForkJoinPool8Test extends J
365       */
366      public void testForkTimedGetNPE() {
367          RecursiveAction a = new CheckedRecursiveAction() {
368 <            public void realCompute() throws Exception {
368 >            protected void realCompute() throws Exception {
369                  FibAction f = new FibAction(8);
370                  assertSame(f, f.fork());
371                  try {
372 <                    f.get(5L, null);
372 >                    f.get(randomTimeout(), null);
373                      shouldThrow();
374                  } catch (NullPointerException success) {}
375              }};
# Line 392 | Line 381 | public class ForkJoinPool8Test extends J
381       */
382      public void testForkQuietlyJoin() {
383          RecursiveAction a = new CheckedRecursiveAction() {
384 <            public void realCompute() {
384 >            protected void realCompute() {
385                  FibAction f = new FibAction(8);
386                  assertSame(f, f.fork());
387                  f.quietlyJoin();
# Line 403 | Line 392 | public class ForkJoinPool8Test extends J
392      }
393  
394      /**
406     * helpQuiesce returns when tasks are complete.
407     * getQueuedTaskCount returns 0 when quiescent
408     */
409    public void testForkHelpQuiesce() {
410        RecursiveAction a = new CheckedRecursiveAction() {
411            public void realCompute() {
412                FibAction f = new FibAction(8);
413                assertSame(f, f.fork());
414                helpQuiesce();
415                assertEquals(21, f.result);
416                assertEquals(0, getQueuedTaskCount());
417                checkCompletedNormally(f);
418            }};
419        checkInvoke(a);
420    }
421
422    /**
395       * invoke task throws exception when task completes abnormally
396       */
397      public void testAbnormalInvoke() {
398          RecursiveAction a = new CheckedRecursiveAction() {
399 <            public void realCompute() {
399 >            protected void realCompute() {
400                  FailingFibAction f = new FailingFibAction(8);
401                  try {
402                      f.invoke();
# Line 441 | Line 413 | public class ForkJoinPool8Test extends J
413       */
414      public void testAbnormalQuietlyInvoke() {
415          RecursiveAction a = new CheckedRecursiveAction() {
416 <            public void realCompute() {
416 >            protected void realCompute() {
417                  FailingFibAction f = new FailingFibAction(8);
418                  f.quietlyInvoke();
419                  assertTrue(f.getException() instanceof FJException);
# Line 455 | Line 427 | public class ForkJoinPool8Test extends J
427       */
428      public void testAbnormalForkJoin() {
429          RecursiveAction a = new CheckedRecursiveAction() {
430 <            public void realCompute() {
430 >            protected void realCompute() {
431                  FailingFibAction f = new FailingFibAction(8);
432                  assertSame(f, f.fork());
433                  try {
# Line 473 | Line 445 | public class ForkJoinPool8Test extends J
445       */
446      public void testAbnormalForkGet() {
447          RecursiveAction a = new CheckedRecursiveAction() {
448 <            public void realCompute() throws Exception {
448 >            protected void realCompute() throws Exception {
449                  FailingFibAction f = new FailingFibAction(8);
450                  assertSame(f, f.fork());
451                  try {
# Line 493 | Line 465 | public class ForkJoinPool8Test extends J
465       */
466      public void testAbnormalForkTimedGet() {
467          RecursiveAction a = new CheckedRecursiveAction() {
468 <            public void realCompute() throws Exception {
468 >            protected void realCompute() throws Exception {
469                  FailingFibAction f = new FailingFibAction(8);
470                  assertSame(f, f.fork());
471                  try {
472 <                    f.get(5L, TimeUnit.SECONDS);
472 >                    f.get(LONG_DELAY_MS, MILLISECONDS);
473                      shouldThrow();
474                  } catch (ExecutionException success) {
475                      Throwable cause = success.getCause();
# Line 513 | Line 485 | public class ForkJoinPool8Test extends J
485       */
486      public void testAbnormalForkQuietlyJoin() {
487          RecursiveAction a = new CheckedRecursiveAction() {
488 <            public void realCompute() {
488 >            protected void realCompute() {
489                  FailingFibAction f = new FailingFibAction(8);
490                  assertSame(f, f.fork());
491                  f.quietlyJoin();
# Line 528 | Line 500 | public class ForkJoinPool8Test extends J
500       */
501      public void testCancelledInvoke() {
502          RecursiveAction a = new CheckedRecursiveAction() {
503 <            public void realCompute() {
503 >            protected void realCompute() {
504                  FibAction f = new FibAction(8);
505                  assertTrue(f.cancel(true));
506                  try {
# Line 546 | Line 518 | public class ForkJoinPool8Test extends J
518       */
519      public void testCancelledForkJoin() {
520          RecursiveAction a = new CheckedRecursiveAction() {
521 <            public void realCompute() {
521 >            protected void realCompute() {
522                  FibAction f = new FibAction(8);
523                  assertTrue(f.cancel(true));
524                  assertSame(f, f.fork());
# Line 565 | Line 537 | public class ForkJoinPool8Test extends J
537       */
538      public void testCancelledForkGet() {
539          RecursiveAction a = new CheckedRecursiveAction() {
540 <            public void realCompute() throws Exception {
540 >            protected void realCompute() throws Exception {
541                  FibAction f = new FibAction(8);
542                  assertTrue(f.cancel(true));
543                  assertSame(f, f.fork());
# Line 584 | Line 556 | public class ForkJoinPool8Test extends J
556       */
557      public void testCancelledForkTimedGet() {
558          RecursiveAction a = new CheckedRecursiveAction() {
559 <            public void realCompute() throws Exception {
559 >            protected void realCompute() throws Exception {
560                  FibAction f = new FibAction(8);
561                  assertTrue(f.cancel(true));
562                  assertSame(f, f.fork());
563                  try {
564 <                    f.get(5L, SECONDS);
564 >                    f.get(LONG_DELAY_MS, MILLISECONDS);
565                      shouldThrow();
566                  } catch (CancellationException success) {
567                      checkCancelled(f);
# Line 603 | Line 575 | public class ForkJoinPool8Test extends J
575       */
576      public void testCancelledForkQuietlyJoin() {
577          RecursiveAction a = new CheckedRecursiveAction() {
578 <            public void realCompute() {
578 >            protected void realCompute() {
579                  FibAction f = new FibAction(8);
580                  assertTrue(f.cancel(true));
581                  assertSame(f, f.fork());
# Line 618 | Line 590 | public class ForkJoinPool8Test extends J
590       */
591      public void testInForkJoinPool2() {
592          RecursiveAction a = new CheckedRecursiveAction() {
593 <            public void realCompute() {
593 >            protected void realCompute() {
594                  assertFalse(inForkJoinPool());
595              }};
596          assertNull(a.invoke());
# Line 629 | Line 601 | public class ForkJoinPool8Test extends J
601       */
602      public void testReinitialize() {
603          RecursiveAction a = new CheckedRecursiveAction() {
604 <            public void realCompute() {
604 >            protected void realCompute() {
605                  FibAction f = new FibAction(8);
606                  checkNotDone(f);
607  
# Line 649 | Line 621 | public class ForkJoinPool8Test extends J
621       */
622      public void testReinitializeAbnormal() {
623          RecursiveAction a = new CheckedRecursiveAction() {
624 <            public void realCompute() {
624 >            protected void realCompute() {
625                  FailingFibAction f = new FailingFibAction(8);
626                  checkNotDone(f);
627  
# Line 672 | Line 644 | public class ForkJoinPool8Test extends J
644       */
645      public void testCompleteExceptionally() {
646          RecursiveAction a = new CheckedRecursiveAction() {
647 <            public void realCompute() {
647 >            protected void realCompute() {
648                  FibAction f = new FibAction(8);
649                  f.completeExceptionally(new FJException());
650                  try {
# Line 690 | Line 662 | public class ForkJoinPool8Test extends J
662       */
663      public void testComplete() {
664          RecursiveAction a = new CheckedRecursiveAction() {
665 <            public void realCompute() {
665 >            protected void realCompute() {
666                  FibAction f = new FibAction(8);
667                  f.complete(null);
668                  assertNull(f.invoke());
# Line 705 | Line 677 | public class ForkJoinPool8Test extends J
677       */
678      public void testInvokeAll2() {
679          RecursiveAction a = new CheckedRecursiveAction() {
680 <            public void realCompute() {
680 >            protected void realCompute() {
681                  FibAction f = new FibAction(8);
682                  FibAction g = new FibAction(9);
683                  invokeAll(f, g);
# Line 722 | Line 694 | public class ForkJoinPool8Test extends J
694       */
695      public void testInvokeAll1() {
696          RecursiveAction a = new CheckedRecursiveAction() {
697 <            public void realCompute() {
697 >            protected void realCompute() {
698                  FibAction f = new FibAction(8);
699                  invokeAll(f);
700                  checkCompletedNormally(f);
# Line 736 | Line 708 | public class ForkJoinPool8Test extends J
708       */
709      public void testInvokeAll3() {
710          RecursiveAction a = new CheckedRecursiveAction() {
711 <            public void realCompute() {
711 >            protected void realCompute() {
712                  FibAction f = new FibAction(8);
713                  FibAction g = new FibAction(9);
714                  FibAction h = new FibAction(7);
# Line 759 | Line 731 | public class ForkJoinPool8Test extends J
731       */
732      public void testInvokeAllCollection() {
733          RecursiveAction a = new CheckedRecursiveAction() {
734 <            public void realCompute() {
734 >            protected void realCompute() {
735                  FibAction f = new FibAction(8);
736                  FibAction g = new FibAction(9);
737                  FibAction h = new FibAction(7);
# Line 786 | Line 758 | public class ForkJoinPool8Test extends J
758       */
759      public void testInvokeAllNPE() {
760          RecursiveAction a = new CheckedRecursiveAction() {
761 <            public void realCompute() {
761 >            protected void realCompute() {
762                  FibAction f = new FibAction(8);
763                  FibAction g = new FibAction(9);
764                  FibAction h = null;
# Line 803 | Line 775 | public class ForkJoinPool8Test extends J
775       */
776      public void testAbnormalInvokeAll2() {
777          RecursiveAction a = new CheckedRecursiveAction() {
778 <            public void realCompute() {
778 >            protected void realCompute() {
779                  FibAction f = new FibAction(8);
780                  FailingFibAction g = new FailingFibAction(9);
781                  try {
# Line 821 | Line 793 | public class ForkJoinPool8Test extends J
793       */
794      public void testAbnormalInvokeAll1() {
795          RecursiveAction a = new CheckedRecursiveAction() {
796 <            public void realCompute() {
796 >            protected void realCompute() {
797                  FailingFibAction g = new FailingFibAction(9);
798                  try {
799                      invokeAll(g);
# Line 838 | Line 810 | public class ForkJoinPool8Test extends J
810       */
811      public void testAbnormalInvokeAll3() {
812          RecursiveAction a = new CheckedRecursiveAction() {
813 <            public void realCompute() {
813 >            protected void realCompute() {
814                  FibAction f = new FibAction(8);
815                  FailingFibAction g = new FailingFibAction(9);
816                  FibAction h = new FibAction(7);
# Line 857 | Line 829 | public class ForkJoinPool8Test extends J
829       */
830      public void testAbnormalInvokeAllCollection() {
831          RecursiveAction a = new CheckedRecursiveAction() {
832 <            public void realCompute() {
832 >            protected void realCompute() {
833                  FailingFibAction f = new FailingFibAction(8);
834                  FibAction g = new FibAction(9);
835                  FibAction h = new FibAction(7);
# Line 877 | Line 849 | public class ForkJoinPool8Test extends J
849  
850      // CountedCompleter versions
851  
852 <    public abstract class CheckedFJTask extends RecursiveAction {
881 <        protected abstract void realCompute() throws Throwable;
882 <
883 <        public final void compute() {
884 <            try {
885 <                realCompute();
886 <            } catch (Throwable t) {
887 <                threadUnexpectedException(t);
888 <            }
889 <        }
890 <    }
891 <
892 <    static abstract class CCF extends CountedCompleter {
852 >    abstract static class CCF extends CountedCompleter {
853          int number;
854          int rnumber;
855  
# Line 910 | Line 870 | public class ForkJoinPool8Test extends J
870              f.onCompletion(f);
871              if ((p = f.getCompleter()) != null)
872                  p.tryComplete();
873 <            else
874 <                f.quietlyComplete();
873 >            else
874 >                f.quietlyComplete();
875          }
876      }
877  
# Line 942 | Line 902 | public class ForkJoinPool8Test extends J
902          }
903      }
904  
905 <    // Version of CCF with forced failure in left completions
906 <    static abstract class FailingCCF extends CountedCompleter {
905 >    /** Version of CCF with forced failure in left completions. */
906 >    abstract static class FailingCCF extends CountedCompleter {
907          int number;
908          int rnumber;
909  
# Line 964 | Line 924 | public class ForkJoinPool8Test extends J
924              f.onCompletion(f);
925              if ((p = f.getCompleter()) != null)
926                  p.tryComplete();
927 <            else
928 <                f.quietlyComplete();
927 >            else
928 >                f.quietlyComplete();
929          }
930      }
931  
# Line 990 | Line 950 | public class ForkJoinPool8Test extends J
950              completeExceptionally(new FJException());
951          }
952      }
953 <    
953 >
954      /**
955       * invoke returns when task completes normally.
956       * isCompletedAbnormally and isCancelled return false for normally
957       * completed tasks; getRawResult returns null.
958       */
959      public void testInvokeCC() {
960 <       ForkJoinTask a =  new CheckedFJTask() {
961 <            public void realCompute() {
960 >        ForkJoinTask a = new CheckedRecursiveAction() {
961 >            protected void realCompute() {
962                  CCF f = new LCCF(null, 8);
963                  assertNull(f.invoke());
964                  assertEquals(21, f.number);
# Line 1013 | Line 973 | public class ForkJoinPool8Test extends J
973       * completed tasks
974       */
975      public void testQuietlyInvokeCC() {
976 <       ForkJoinTask a =  new CheckedFJTask() {
977 <            public void realCompute() {
976 >        ForkJoinTask a = new CheckedRecursiveAction() {
977 >            protected void realCompute() {
978                  CCF f = new LCCF(null, 8);
979                  f.quietlyInvoke();
980                  assertEquals(21, f.number);
# Line 1027 | Line 987 | public class ForkJoinPool8Test extends J
987       * join of a forked task returns when task completes
988       */
989      public void testForkJoinCC() {
990 <       ForkJoinTask a =  new CheckedFJTask() {
991 <            public void realCompute() {
990 >        ForkJoinTask a = new CheckedRecursiveAction() {
991 >            protected void realCompute() {
992                  CCF f = new LCCF(null, 8);
993                  assertSame(f, f.fork());
994                  assertNull(f.join());
# Line 1042 | Line 1002 | public class ForkJoinPool8Test extends J
1002       * get of a forked task returns when task completes
1003       */
1004      public void testForkGetCC() {
1005 <       ForkJoinTask a =  new CheckedFJTask() {
1006 <            public void realCompute() throws Exception {
1005 >        ForkJoinTask a = new CheckedRecursiveAction() {
1006 >            protected void realCompute() throws Exception {
1007                  CCF f = new LCCF(null, 8);
1008                  assertSame(f, f.fork());
1009                  assertNull(f.get());
# Line 1057 | Line 1017 | public class ForkJoinPool8Test extends J
1017       * timed get of a forked task returns when task completes
1018       */
1019      public void testForkTimedGetCC() {
1020 <       ForkJoinTask a =  new CheckedFJTask() {
1021 <            public void realCompute() throws Exception {
1020 >        ForkJoinTask a = new CheckedRecursiveAction() {
1021 >            protected void realCompute() throws Exception {
1022                  CCF f = new LCCF(null, 8);
1023                  assertSame(f, f.fork());
1024                  assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
# Line 1072 | Line 1032 | public class ForkJoinPool8Test extends J
1032       * timed get with null time unit throws NPE
1033       */
1034      public void testForkTimedGetNPECC() {
1035 <       ForkJoinTask a =  new CheckedFJTask() {
1036 <            public void realCompute() throws Exception {
1035 >        ForkJoinTask a = new CheckedRecursiveAction() {
1036 >            protected void realCompute() throws Exception {
1037                  CCF f = new LCCF(null, 8);
1038                  assertSame(f, f.fork());
1039                  try {
1040 <                    f.get(5L, null);
1040 >                    f.get(randomTimeout(), null);
1041                      shouldThrow();
1042                  } catch (NullPointerException success) {}
1043              }};
# Line 1088 | Line 1048 | public class ForkJoinPool8Test extends J
1048       * quietlyJoin of a forked task returns when task completes
1049       */
1050      public void testForkQuietlyJoinCC() {
1051 <       ForkJoinTask a =  new CheckedFJTask() {
1052 <            public void realCompute() {
1051 >        ForkJoinTask a = new CheckedRecursiveAction() {
1052 >            protected void realCompute() {
1053                  CCF f = new LCCF(null, 8);
1054                  assertSame(f, f.fork());
1055                  f.quietlyJoin();
# Line 1100 | Line 1060 | public class ForkJoinPool8Test extends J
1060      }
1061  
1062      /**
1103     * helpQuiesce returns when tasks are complete.
1104     * getQueuedTaskCount returns 0 when quiescent
1105     */
1106    public void testForkHelpQuiesceCC() {
1107       ForkJoinTask a =  new CheckedFJTask() {
1108            public void realCompute() {
1109                CCF f = new LCCF(null, 8);
1110                assertSame(f, f.fork());
1111                helpQuiesce();
1112                assertEquals(21, f.number);
1113                assertEquals(0, getQueuedTaskCount());
1114                checkCompletedNormally(f);
1115            }};
1116        checkInvoke(a);
1117    }
1118
1119    /**
1063       * invoke task throws exception when task completes abnormally
1064       */
1065      public void testAbnormalInvokeCC() {
1066 <       ForkJoinTask a =  new CheckedFJTask() {
1067 <            public void realCompute() {
1066 >        ForkJoinTask a = new CheckedRecursiveAction() {
1067 >            protected void realCompute() {
1068                  FailingCCF f = new LFCCF(null, 8);
1069                  try {
1070                      f.invoke();
# Line 1137 | Line 1080 | public class ForkJoinPool8Test extends J
1080       * quietlyInvoke task returns when task completes abnormally
1081       */
1082      public void testAbnormalQuietlyInvokeCC() {
1083 <       ForkJoinTask a =  new CheckedFJTask() {
1084 <            public void realCompute() {
1083 >        ForkJoinTask a = new CheckedRecursiveAction() {
1084 >            protected void realCompute() {
1085                  FailingCCF f = new LFCCF(null, 8);
1086                  f.quietlyInvoke();
1087                  assertTrue(f.getException() instanceof FJException);
# Line 1151 | Line 1094 | public class ForkJoinPool8Test extends J
1094       * join of a forked task throws exception when task completes abnormally
1095       */
1096      public void testAbnormalForkJoinCC() {
1097 <       ForkJoinTask a =  new CheckedFJTask() {
1098 <            public void realCompute() {
1097 >        ForkJoinTask a = new CheckedRecursiveAction() {
1098 >            protected void realCompute() {
1099                  FailingCCF f = new LFCCF(null, 8);
1100                  assertSame(f, f.fork());
1101                  try {
# Line 1169 | Line 1112 | public class ForkJoinPool8Test extends J
1112       * get of a forked task throws exception when task completes abnormally
1113       */
1114      public void testAbnormalForkGetCC() {
1115 <       ForkJoinTask a =  new CheckedFJTask() {
1116 <            public void realCompute() throws Exception {
1115 >        ForkJoinTask a = new CheckedRecursiveAction() {
1116 >            protected void realCompute() throws Exception {
1117                  FailingCCF f = new LFCCF(null, 8);
1118                  assertSame(f, f.fork());
1119                  try {
# Line 1189 | Line 1132 | public class ForkJoinPool8Test extends J
1132       * timed get of a forked task throws exception when task completes abnormally
1133       */
1134      public void testAbnormalForkTimedGetCC() {
1135 <       ForkJoinTask a =  new CheckedFJTask() {
1136 <            public void realCompute() throws Exception {
1135 >        ForkJoinTask a = new CheckedRecursiveAction() {
1136 >            protected void realCompute() throws Exception {
1137                  FailingCCF f = new LFCCF(null, 8);
1138                  assertSame(f, f.fork());
1139                  try {
# Line 1209 | Line 1152 | public class ForkJoinPool8Test extends J
1152       * quietlyJoin of a forked task returns when task completes abnormally
1153       */
1154      public void testAbnormalForkQuietlyJoinCC() {
1155 <       ForkJoinTask a =  new CheckedFJTask() {
1156 <            public void realCompute() {
1155 >        ForkJoinTask a = new CheckedRecursiveAction() {
1156 >            protected void realCompute() {
1157                  FailingCCF f = new LFCCF(null, 8);
1158                  assertSame(f, f.fork());
1159                  f.quietlyJoin();
# Line 1224 | Line 1167 | public class ForkJoinPool8Test extends J
1167       * invoke task throws exception when task cancelled
1168       */
1169      public void testCancelledInvokeCC() {
1170 <       ForkJoinTask a =  new CheckedFJTask() {
1171 <            public void realCompute() {
1170 >        ForkJoinTask a = new CheckedRecursiveAction() {
1171 >            protected void realCompute() {
1172                  CCF f = new LCCF(null, 8);
1173                  assertTrue(f.cancel(true));
1174                  try {
# Line 1242 | Line 1185 | public class ForkJoinPool8Test extends J
1185       * join of a forked task throws exception when task cancelled
1186       */
1187      public void testCancelledForkJoinCC() {
1188 <       ForkJoinTask a =  new CheckedFJTask() {
1189 <            public void realCompute() {
1188 >        ForkJoinTask a = new CheckedRecursiveAction() {
1189 >            protected void realCompute() {
1190                  CCF f = new LCCF(null, 8);
1191                  assertTrue(f.cancel(true));
1192                  assertSame(f, f.fork());
# Line 1261 | Line 1204 | public class ForkJoinPool8Test extends J
1204       * get of a forked task throws exception when task cancelled
1205       */
1206      public void testCancelledForkGetCC() {
1207 <       ForkJoinTask a =  new CheckedFJTask() {
1208 <            public void realCompute() throws Exception {
1207 >        ForkJoinTask a = new CheckedRecursiveAction() {
1208 >            protected void realCompute() throws Exception {
1209                  CCF f = new LCCF(null, 8);
1210                  assertTrue(f.cancel(true));
1211                  assertSame(f, f.fork());
# Line 1280 | Line 1223 | public class ForkJoinPool8Test extends J
1223       * timed get of a forked task throws exception when task cancelled
1224       */
1225      public void testCancelledForkTimedGetCC() throws Exception {
1226 <       ForkJoinTask a =  new CheckedFJTask() {
1227 <            public void realCompute() throws Exception {
1226 >        ForkJoinTask a = new CheckedRecursiveAction() {
1227 >            protected void realCompute() throws Exception {
1228                  CCF f = new LCCF(null, 8);
1229                  assertTrue(f.cancel(true));
1230                  assertSame(f, f.fork());
# Line 1299 | Line 1242 | public class ForkJoinPool8Test extends J
1242       * quietlyJoin of a forked task returns when task cancelled
1243       */
1244      public void testCancelledForkQuietlyJoinCC() {
1245 <       ForkJoinTask a =  new CheckedFJTask() {
1246 <            public void realCompute() {
1245 >        ForkJoinTask a = new CheckedRecursiveAction() {
1246 >            protected void realCompute() {
1247                  CCF f = new LCCF(null, 8);
1248                  assertTrue(f.cancel(true));
1249                  assertSame(f, f.fork());
# Line 1314 | Line 1257 | public class ForkJoinPool8Test extends J
1257       * getPool of non-FJ task returns null
1258       */
1259      public void testGetPool2CC() {
1260 <       ForkJoinTask a =  new CheckedFJTask() {
1261 <            public void realCompute() {
1260 >        ForkJoinTask a = new CheckedRecursiveAction() {
1261 >            protected void realCompute() {
1262                  assertNull(getPool());
1263              }};
1264          assertNull(a.invoke());
# Line 1325 | Line 1268 | public class ForkJoinPool8Test extends J
1268       * inForkJoinPool of non-FJ task returns false
1269       */
1270      public void testInForkJoinPool2CC() {
1271 <       ForkJoinTask a =  new CheckedFJTask() {
1272 <            public void realCompute() {
1271 >        ForkJoinTask a = new CheckedRecursiveAction() {
1272 >            protected void realCompute() {
1273                  assertFalse(inForkJoinPool());
1274              }};
1275          assertNull(a.invoke());
# Line 1336 | Line 1279 | public class ForkJoinPool8Test extends J
1279       * setRawResult(null) succeeds
1280       */
1281      public void testSetRawResultCC() {
1282 <       ForkJoinTask a =  new CheckedFJTask() {
1283 <            public void realCompute() {
1282 >        ForkJoinTask a = new CheckedRecursiveAction() {
1283 >            protected void realCompute() {
1284                  setRawResult(null);
1285                  assertNull(getRawResult());
1286              }};
# Line 1348 | Line 1291 | public class ForkJoinPool8Test extends J
1291       * invoke task throws exception after invoking completeExceptionally
1292       */
1293      public void testCompleteExceptionally2CC() {
1294 <       ForkJoinTask a =  new CheckedFJTask() {
1295 <            public void realCompute() {
1294 >        ForkJoinTask a = new CheckedRecursiveAction() {
1295 >            protected void realCompute() {
1296                  CCF f = new LCCF(null, 8);
1297                  f.completeExceptionally(new FJException());
1298                  try {
# Line 1366 | Line 1309 | public class ForkJoinPool8Test extends J
1309       * invokeAll(t1, t2) invokes all task arguments
1310       */
1311      public void testInvokeAll2CC() {
1312 <       ForkJoinTask a =  new CheckedFJTask() {
1313 <            public void realCompute() {
1312 >        ForkJoinTask a = new CheckedRecursiveAction() {
1313 >            protected void realCompute() {
1314                  CCF f = new LCCF(null, 8);
1315                  CCF g = new LCCF(null, 9);
1316                  invokeAll(f, g);
# Line 1383 | Line 1326 | public class ForkJoinPool8Test extends J
1326       * invokeAll(tasks) with 1 argument invokes task
1327       */
1328      public void testInvokeAll1CC() {
1329 <       ForkJoinTask a =  new CheckedFJTask() {
1330 <            public void realCompute() {
1329 >        ForkJoinTask a = new CheckedRecursiveAction() {
1330 >            protected void realCompute() {
1331                  CCF f = new LCCF(null, 8);
1332                  invokeAll(f);
1333                  checkCompletedNormally(f);
# Line 1397 | Line 1340 | public class ForkJoinPool8Test extends J
1340       * invokeAll(tasks) with > 2 argument invokes tasks
1341       */
1342      public void testInvokeAll3CC() {
1343 <       ForkJoinTask a =  new CheckedFJTask() {
1344 <            public void realCompute() {
1343 >        ForkJoinTask a = new CheckedRecursiveAction() {
1344 >            protected void realCompute() {
1345                  CCF f = new LCCF(null, 8);
1346                  CCF g = new LCCF(null, 9);
1347                  CCF h = new LCCF(null, 7);
# Line 1417 | Line 1360 | public class ForkJoinPool8Test extends J
1360       * invokeAll(collection) invokes all tasks in the collection
1361       */
1362      public void testInvokeAllCollectionCC() {
1363 <       ForkJoinTask a =  new CheckedFJTask() {
1364 <            public void realCompute() {
1363 >        ForkJoinTask a = new CheckedRecursiveAction() {
1364 >            protected void realCompute() {
1365                  CCF f = new LCCF(null, 8);
1366                  CCF g = new LCCF(null, 9);
1367                  CCF h = new LCCF(null, 7);
# Line 1441 | Line 1384 | public class ForkJoinPool8Test extends J
1384       * invokeAll(tasks) with any null task throws NPE
1385       */
1386      public void testInvokeAllNPECC() {
1387 <       ForkJoinTask a =  new CheckedFJTask() {
1388 <            public void realCompute() {
1387 >        ForkJoinTask a = new CheckedRecursiveAction() {
1388 >            protected void realCompute() {
1389                  CCF f = new LCCF(null, 8);
1390                  CCF g = new LCCF(null, 9);
1391                  CCF h = null;
# Line 1458 | Line 1401 | public class ForkJoinPool8Test extends J
1401       * invokeAll(t1, t2) throw exception if any task does
1402       */
1403      public void testAbnormalInvokeAll2CC() {
1404 <       ForkJoinTask a =  new CheckedFJTask() {
1405 <            public void realCompute() {
1404 >        ForkJoinTask a = new CheckedRecursiveAction() {
1405 >            protected void realCompute() {
1406                  CCF f = new LCCF(null, 8);
1407                  FailingCCF g = new LFCCF(null, 9);
1408                  try {
# Line 1476 | Line 1419 | public class ForkJoinPool8Test extends J
1419       * invokeAll(tasks) with 1 argument throws exception if task does
1420       */
1421      public void testAbnormalInvokeAll1CC() {
1422 <       ForkJoinTask a =  new CheckedFJTask() {
1423 <            public void realCompute() {
1422 >        ForkJoinTask a = new CheckedRecursiveAction() {
1423 >            protected void realCompute() {
1424                  FailingCCF g = new LFCCF(null, 9);
1425                  try {
1426                      invokeAll(g);
# Line 1493 | Line 1436 | public class ForkJoinPool8Test extends J
1436       * invokeAll(tasks) with > 2 argument throws exception if any task does
1437       */
1438      public void testAbnormalInvokeAll3CC() {
1439 <       ForkJoinTask a =  new CheckedFJTask() {
1440 <            public void realCompute() {
1439 >        ForkJoinTask a = new CheckedRecursiveAction() {
1440 >            protected void realCompute() {
1441                  CCF f = new LCCF(null, 8);
1442                  FailingCCF g = new LFCCF(null, 9);
1443                  CCF h = new LCCF(null, 7);
# Line 1509 | Line 1452 | public class ForkJoinPool8Test extends J
1452      }
1453  
1454      /**
1455 <     * invokeAll(collection)  throws exception if any task does
1455 >     * invokeAll(collection) throws exception if any task does
1456       */
1457      public void testAbnormalInvokeAllCollectionCC() {
1458 <       ForkJoinTask a =  new CheckedFJTask() {
1459 <            public void realCompute() {
1458 >        ForkJoinTask a = new CheckedRecursiveAction() {
1459 >            protected void realCompute() {
1460                  FailingCCF f = new LFCCF(null, 8);
1461                  CCF g = new LCCF(null, 9);
1462                  CCF h = new LCCF(null, 7);
# Line 1531 | Line 1474 | public class ForkJoinPool8Test extends J
1474          checkInvoke(a);
1475      }
1476  
1477 +    /**
1478 +     * awaitQuiescence by a worker is equivalent in effect to
1479 +     * ForkJoinTask.helpQuiesce()
1480 +     */
1481 +    public void testAwaitQuiescence1() throws Exception {
1482 +        final ForkJoinPool p = new ForkJoinPool();
1483 +        try (PoolCleaner cleaner = cleaner(p)) {
1484 +            final long startTime = System.nanoTime();
1485 +            assertTrue(p.isQuiescent());
1486 +            ForkJoinTask a = new CheckedRecursiveAction() {
1487 +                protected void realCompute() {
1488 +                    FibAction f = new FibAction(8);
1489 +                    assertSame(f, f.fork());
1490 +                    assertSame(p, ForkJoinTask.getPool());
1491 +                    boolean quiescent = p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS);
1492 +                    assertTrue(quiescent);
1493 +                    assertFalse(p.isQuiescent());
1494 +                    while (!f.isDone()) {
1495 +                        assertFalse(p.getAsyncMode());
1496 +                        assertFalse(p.isShutdown());
1497 +                        assertFalse(p.isTerminating());
1498 +                        assertFalse(p.isTerminated());
1499 +                        Thread.yield();
1500 +                    }
1501 +                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1502 +                    assertFalse(p.isQuiescent());
1503 +                    assertEquals(0, ForkJoinTask.getQueuedTaskCount());
1504 +                    assertEquals(21, f.result);
1505 +                }};
1506 +            p.execute(a);
1507 +            while (!a.isDone() || !p.isQuiescent()) {
1508 +                assertFalse(p.getAsyncMode());
1509 +                assertFalse(p.isShutdown());
1510 +                assertFalse(p.isTerminating());
1511 +                assertFalse(p.isTerminated());
1512 +                Thread.yield();
1513 +            }
1514 +            assertEquals(0, p.getQueuedTaskCount());
1515 +            assertFalse(p.getAsyncMode());
1516 +            assertEquals(0, p.getQueuedSubmissionCount());
1517 +            assertFalse(p.hasQueuedSubmissions());
1518 +            while (p.getActiveThreadCount() != 0
1519 +                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1520 +                Thread.yield();
1521 +            assertFalse(p.isShutdown());
1522 +            assertFalse(p.isTerminating());
1523 +            assertFalse(p.isTerminated());
1524 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1525 +        }
1526 +    }
1527 +
1528 +    /**
1529 +     * awaitQuiescence returns when pool isQuiescent() or the indicated
1530 +     * timeout elapsed
1531 +     */
1532 +    public void testAwaitQuiescence2() throws Exception {
1533 +        /**
1534 +         * """It is possible to disable or limit the use of threads in the
1535 +         * common pool by setting the parallelism property to zero. However
1536 +         * doing so may cause unjoined tasks to never be executed."""
1537 +         */
1538 +        if ("0".equals(System.getProperty(
1539 +             "java.util.concurrent.ForkJoinPool.common.parallelism")))
1540 +            return;
1541 +        final ForkJoinPool p = new ForkJoinPool();
1542 +        try (PoolCleaner cleaner = cleaner(p)) {
1543 +            assertTrue(p.isQuiescent());
1544 +            final long startTime = System.nanoTime();
1545 +            ForkJoinTask a = new CheckedRecursiveAction() {
1546 +                protected void realCompute() {
1547 +                    FibAction f = new FibAction(8);
1548 +                    assertSame(f, f.fork());
1549 +                    while (!f.isDone()
1550 +                           && millisElapsedSince(startTime) < LONG_DELAY_MS) {
1551 +                        assertFalse(p.getAsyncMode());
1552 +                        assertFalse(p.isShutdown());
1553 +                        assertFalse(p.isTerminating());
1554 +                        assertFalse(p.isTerminated());
1555 +                        Thread.yield();
1556 +                    }
1557 +                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1558 +                    assertEquals(0, ForkJoinTask.getQueuedTaskCount());
1559 +                    assertEquals(21, f.result);
1560 +                }};
1561 +            p.execute(a);
1562 +            assertTrue(p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS));
1563 +            assertTrue(p.isQuiescent());
1564 +            assertTrue(a.isDone());
1565 +            assertEquals(0, p.getQueuedTaskCount());
1566 +            assertFalse(p.getAsyncMode());
1567 +            assertEquals(0, p.getQueuedSubmissionCount());
1568 +            assertFalse(p.hasQueuedSubmissions());
1569 +            while (p.getActiveThreadCount() != 0
1570 +                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1571 +                Thread.yield();
1572 +            assertFalse(p.isShutdown());
1573 +            assertFalse(p.isTerminating());
1574 +            assertFalse(p.isTerminated());
1575 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1576 +        }
1577 +    }
1578 +
1579   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines