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.28 by jsr166, Mon Nov 22 22:45:49 2010 UTC vs.
Revision 1.51 by jsr166, Wed Aug 24 22:22:39 2016 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6 < import java.util.concurrent.ExecutionException;
6 >
7 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
8 > import static java.util.concurrent.TimeUnit.SECONDS;
9 >
10 > import java.util.Arrays;
11 > import java.util.HashSet;
12   import java.util.concurrent.CancellationException;
13 + import java.util.concurrent.ExecutionException;
14   import java.util.concurrent.ForkJoinPool;
15   import java.util.concurrent.ForkJoinTask;
10 import java.util.concurrent.ForkJoinWorkerThread;
16   import java.util.concurrent.RecursiveAction;
12 import java.util.concurrent.TimeUnit;
17   import java.util.concurrent.TimeoutException;
18   import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
19 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
20 < import static java.util.concurrent.TimeUnit.SECONDS;
21 < import java.util.HashSet;
18 < import junit.framework.*;
19 >
20 > import junit.framework.Test;
21 > import junit.framework.TestSuite;
22  
23   public class ForkJoinTaskTest extends JSR166TestCase {
24  
25      public static void main(String[] args) {
26 <        junit.textui.TestRunner.run(suite());
26 >        main(suite(), args);
27      }
28  
29      public static Test suite() {
# Line 46 | Line 49 | public class ForkJoinTaskTest extends JS
49      }
50  
51      private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
52 <        try {
52 >        try (PoolCleaner cleaner = cleaner(pool)) {
53              assertFalse(a.isDone());
54              assertFalse(a.isCompletedNormally());
55              assertFalse(a.isCompletedAbnormally());
# Line 62 | Line 65 | public class ForkJoinTaskTest extends JS
65              assertFalse(a.isCancelled());
66              assertNull(a.getException());
67              assertNull(a.getRawResult());
65        } finally {
66            joinPool(pool);
68          }
69      }
70  
# Line 96 | Line 97 | public class ForkJoinTaskTest extends JS
97  
98          {
99              Thread.currentThread().interrupt();
100 <            long t0 = System.nanoTime();
100 >            long startTime = System.nanoTime();
101              assertSame(expected, a.join());
102 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
103 <            assertTrue(Thread.interrupted());
102 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
103 >            Thread.interrupted();
104          }
105  
106          {
107              Thread.currentThread().interrupt();
108 <            long t0 = System.nanoTime();
108 >            long startTime = System.nanoTime();
109              a.quietlyJoin();        // should be no-op
110 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
111 <            assertTrue(Thread.interrupted());
110 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
111 >            Thread.interrupted();
112          }
113  
114          assertFalse(a.cancel(false));
# Line 135 | Line 136 | public class ForkJoinTaskTest extends JS
136              a.join();
137              shouldThrow();
138          } catch (CancellationException success) {
138            assertTrue(Thread.interrupted());
139          } catch (Throwable fail) { threadUnexpectedException(fail); }
140 +        Thread.interrupted();
141  
142          {
143 <            long t0 = System.nanoTime();
143 >            long startTime = System.nanoTime();
144              a.quietlyJoin();        // should be no-op
145 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
145 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
146          }
147  
148          try {
# Line 162 | Line 163 | public class ForkJoinTaskTest extends JS
163          assertFalse(a.isCancelled());
164          assertFalse(a.isCompletedNormally());
165          assertTrue(a.isCompletedAbnormally());
166 <        assertSame(t, a.getException());
166 >        assertSame(t.getClass(), a.getException().getClass());
167          assertNull(a.getRawResult());
168          assertFalse(a.cancel(false));
169          assertFalse(a.cancel(true));
# Line 172 | Line 173 | public class ForkJoinTaskTest extends JS
173              a.join();
174              shouldThrow();
175          } catch (Throwable expected) {
176 <            assertTrue(Thread.interrupted());
176 <            assertSame(t, expected);
176 >            assertSame(t.getClass(), expected.getClass());
177          }
178 +        Thread.interrupted();
179  
180          {
181 <            long t0 = System.nanoTime();
181 >            long startTime = System.nanoTime();
182              a.quietlyJoin();        // should be no-op
183 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
183 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
184          }
185  
186          try {
187              a.get();
188              shouldThrow();
189          } catch (ExecutionException success) {
190 <            assertSame(t, success.getCause());
190 >            assertSame(t.getClass(), success.getCause().getClass());
191          } catch (Throwable fail) { threadUnexpectedException(fail); }
192  
193          try {
194              a.get(5L, SECONDS);
195              shouldThrow();
196          } catch (ExecutionException success) {
197 <            assertSame(t, success.getCause());
197 >            assertSame(t.getClass(), success.getCause().getClass());
198          } catch (Throwable fail) { threadUnexpectedException(fail); }
199      }
200  
# Line 205 | Line 206 | public class ForkJoinTaskTest extends JS
206       * differently than supplied Recursive forms.
207       */
208  
209 <    static final class FJException extends RuntimeException {
209 >    public static final class FJException extends RuntimeException {
210          FJException() { super(); }
211      }
212  
# Line 216 | Line 217 | public class ForkJoinTaskTest extends JS
217              AtomicIntegerFieldUpdater.newUpdater(BinaryAsyncAction.class,
218                                                   "controlState");
219  
220 <        private BinaryAsyncAction parent;
220 >        private volatile BinaryAsyncAction parent;
221  
222 <        private BinaryAsyncAction sibling;
222 >        private volatile BinaryAsyncAction sibling;
223  
224          protected BinaryAsyncAction() {
225          }
# Line 253 | Line 254 | public class ForkJoinTaskTest extends JS
254              super.completeExceptionally(ex);
255          }
256  
257 +        public boolean cancel(boolean mayInterruptIfRunning) {
258 +            if (super.cancel(mayInterruptIfRunning)) {
259 +                completeExceptionally(new FJException());
260 +                return true;
261 +            }
262 +            return false;
263 +        }
264 +
265          public final void complete() {
266              BinaryAsyncAction a = this;
267              for (;;) {
# Line 274 | Line 283 | public class ForkJoinTaskTest extends JS
283          }
284  
285          public final void completeExceptionally(Throwable ex) {
286 <            BinaryAsyncAction a = this;
278 <            while (!a.isCompletedAbnormally()) {
286 >            for (BinaryAsyncAction a = this;;) {
287                  a.completeThisExceptionally(ex);
288                  BinaryAsyncAction s = a.sibling;
289 <                if (s != null)
290 <                    s.cancel(false);
291 <                if (!a.onException() || (a = a.parent) == null)
289 >                if (s != null && !s.isDone())
290 >                    s.completeExceptionally(ex);
291 >                if ((a = a.parent) == null)
292                      break;
293              }
294          }
# Line 330 | Line 338 | public class ForkJoinTaskTest extends JS
338          public final boolean exec() {
339              AsyncFib f = this;
340              int n = f.number;
341 <            if (n > 1) {
342 <                while (n > 1) {
343 <                    AsyncFib p = f;
344 <                    AsyncFib r = new AsyncFib(n - 2);
345 <                    f = new AsyncFib(--n);
346 <                    p.linkSubtasks(r, f);
339 <                    r.fork();
340 <                }
341 <                f.number = n;
341 >            while (n > 1) {
342 >                AsyncFib p = f;
343 >                AsyncFib r = new AsyncFib(n - 2);
344 >                f = new AsyncFib(--n);
345 >                p.linkSubtasks(r, f);
346 >                r.fork();
347              }
348              f.complete();
349              return false;
# Line 349 | Line 354 | public class ForkJoinTaskTest extends JS
354          }
355      }
356  
352
357      static final class FailingAsyncFib extends BinaryAsyncAction {
358          int number;
359          public FailingAsyncFib(int n) {
# Line 359 | Line 363 | public class ForkJoinTaskTest extends JS
363          public final boolean exec() {
364              FailingAsyncFib f = this;
365              int n = f.number;
366 <            if (n > 1) {
367 <                while (n > 1) {
368 <                    FailingAsyncFib p = f;
369 <                    FailingAsyncFib r = new FailingAsyncFib(n - 2);
370 <                    f = new FailingAsyncFib(--n);
371 <                    p.linkSubtasks(r, f);
368 <                    r.fork();
369 <                }
370 <                f.number = n;
366 >            while (n > 1) {
367 >                FailingAsyncFib p = f;
368 >                FailingAsyncFib r = new FailingAsyncFib(n - 2);
369 >                f = new FailingAsyncFib(--n);
370 >                p.linkSubtasks(r, f);
371 >                r.fork();
372              }
373              f.complete();
374              return false;
# Line 385 | Line 386 | public class ForkJoinTaskTest extends JS
386       */
387      public void testInvoke() {
388          RecursiveAction a = new CheckedRecursiveAction() {
389 <            public void realCompute() {
389 >            protected void realCompute() {
390                  AsyncFib f = new AsyncFib(8);
391                  assertNull(f.invoke());
392                  assertEquals(21, f.number);
# Line 401 | Line 402 | public class ForkJoinTaskTest extends JS
402       */
403      public void testQuietlyInvoke() {
404          RecursiveAction a = new CheckedRecursiveAction() {
405 <            public void realCompute() {
405 >            protected void realCompute() {
406                  AsyncFib f = new AsyncFib(8);
407                  f.quietlyInvoke();
408                  assertEquals(21, f.number);
# Line 415 | Line 416 | public class ForkJoinTaskTest extends JS
416       */
417      public void testForkJoin() {
418          RecursiveAction a = new CheckedRecursiveAction() {
419 <            public void realCompute() {
419 >            protected void realCompute() {
420                  AsyncFib f = new AsyncFib(8);
421                  assertSame(f, f.fork());
422                  assertNull(f.join());
# Line 430 | Line 431 | public class ForkJoinTaskTest extends JS
431       */
432      public void testForkGet() {
433          RecursiveAction a = new CheckedRecursiveAction() {
434 <            public void realCompute() throws Exception {
434 >            protected void realCompute() throws Exception {
435                  AsyncFib f = new AsyncFib(8);
436                  assertSame(f, f.fork());
437                  assertNull(f.get());
# Line 445 | Line 446 | public class ForkJoinTaskTest extends JS
446       */
447      public void testForkTimedGet() {
448          RecursiveAction a = new CheckedRecursiveAction() {
449 <            public void realCompute() throws Exception {
449 >            protected void realCompute() throws Exception {
450                  AsyncFib f = new AsyncFib(8);
451                  assertSame(f, f.fork());
452                  assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
# Line 460 | Line 461 | public class ForkJoinTaskTest extends JS
461       */
462      public void testForkTimedGetNPE() {
463          RecursiveAction a = new CheckedRecursiveAction() {
464 <            public void realCompute() throws Exception {
464 >            protected void realCompute() throws Exception {
465                  AsyncFib f = new AsyncFib(8);
466                  assertSame(f, f.fork());
467                  try {
# Line 476 | Line 477 | public class ForkJoinTaskTest extends JS
477       */
478      public void testForkQuietlyJoin() {
479          RecursiveAction a = new CheckedRecursiveAction() {
480 <            public void realCompute() {
480 >            protected void realCompute() {
481                  AsyncFib f = new AsyncFib(8);
482                  assertSame(f, f.fork());
483                  f.quietlyJoin();
# Line 486 | Line 487 | public class ForkJoinTaskTest extends JS
487          testInvokeOnPool(mainPool(), a);
488      }
489  
489
490      /**
491       * helpQuiesce returns when tasks are complete.
492       * getQueuedTaskCount returns 0 when quiescent
493       */
494      public void testForkHelpQuiesce() {
495          RecursiveAction a = new CheckedRecursiveAction() {
496 <            public void realCompute() {
496 >            protected void realCompute() {
497                  AsyncFib f = new AsyncFib(8);
498                  assertSame(f, f.fork());
499 <                f.helpQuiesce();
499 >                helpQuiesce();
500                  assertEquals(21, f.number);
501                  assertEquals(0, getQueuedTaskCount());
502                  checkCompletedNormally(f);
# Line 504 | Line 504 | public class ForkJoinTaskTest extends JS
504          testInvokeOnPool(mainPool(), a);
505      }
506  
507
507      /**
508       * invoke task throws exception when task completes abnormally
509       */
510      public void testAbnormalInvoke() {
511          RecursiveAction a = new CheckedRecursiveAction() {
512 <            public void realCompute() {
512 >            protected void realCompute() {
513                  FailingAsyncFib f = new FailingAsyncFib(8);
514                  try {
515                      f.invoke();
# Line 527 | Line 526 | public class ForkJoinTaskTest extends JS
526       */
527      public void testAbnormalQuietlyInvoke() {
528          RecursiveAction a = new CheckedRecursiveAction() {
529 <            public void realCompute() {
529 >            protected void realCompute() {
530                  FailingAsyncFib f = new FailingAsyncFib(8);
531                  f.quietlyInvoke();
532                  assertTrue(f.getException() instanceof FJException);
# Line 541 | Line 540 | public class ForkJoinTaskTest extends JS
540       */
541      public void testAbnormalForkJoin() {
542          RecursiveAction a = new CheckedRecursiveAction() {
543 <            public void realCompute() {
543 >            protected void realCompute() {
544                  FailingAsyncFib f = new FailingAsyncFib(8);
545                  assertSame(f, f.fork());
546                  try {
# Line 559 | Line 558 | public class ForkJoinTaskTest extends JS
558       */
559      public void testAbnormalForkGet() {
560          RecursiveAction a = new CheckedRecursiveAction() {
561 <            public void realCompute() throws Exception {
561 >            protected void realCompute() throws Exception {
562                  FailingAsyncFib f = new FailingAsyncFib(8);
563                  assertSame(f, f.fork());
564                  try {
# Line 579 | Line 578 | public class ForkJoinTaskTest extends JS
578       */
579      public void testAbnormalForkTimedGet() {
580          RecursiveAction a = new CheckedRecursiveAction() {
581 <            public void realCompute() throws Exception {
581 >            protected void realCompute() throws Exception {
582                  FailingAsyncFib f = new FailingAsyncFib(8);
583                  assertSame(f, f.fork());
584                  try {
# Line 599 | Line 598 | public class ForkJoinTaskTest extends JS
598       */
599      public void testAbnormalForkQuietlyJoin() {
600          RecursiveAction a = new CheckedRecursiveAction() {
601 <            public void realCompute() {
601 >            protected void realCompute() {
602                  FailingAsyncFib f = new FailingAsyncFib(8);
603                  assertSame(f, f.fork());
604                  f.quietlyJoin();
# Line 614 | Line 613 | public class ForkJoinTaskTest extends JS
613       */
614      public void testCancelledInvoke() {
615          RecursiveAction a = new CheckedRecursiveAction() {
616 <            public void realCompute() {
616 >            protected void realCompute() {
617                  AsyncFib f = new AsyncFib(8);
618                  assertTrue(f.cancel(true));
619                  try {
# Line 632 | Line 631 | public class ForkJoinTaskTest extends JS
631       */
632      public void testCancelledForkJoin() {
633          RecursiveAction a = new CheckedRecursiveAction() {
634 <            public void realCompute() {
634 >            protected void realCompute() {
635                  AsyncFib f = new AsyncFib(8);
636                  assertTrue(f.cancel(true));
637                  assertSame(f, f.fork());
# Line 651 | Line 650 | public class ForkJoinTaskTest extends JS
650       */
651      public void testCancelledForkGet() {
652          RecursiveAction a = new CheckedRecursiveAction() {
653 <            public void realCompute() throws Exception {
653 >            protected void realCompute() throws Exception {
654                  AsyncFib f = new AsyncFib(8);
655                  assertTrue(f.cancel(true));
656                  assertSame(f, f.fork());
# Line 670 | Line 669 | public class ForkJoinTaskTest extends JS
669       */
670      public void testCancelledForkTimedGet() throws Exception {
671          RecursiveAction a = new CheckedRecursiveAction() {
672 <            public void realCompute() throws Exception {
672 >            protected void realCompute() throws Exception {
673                  AsyncFib f = new AsyncFib(8);
674                  assertTrue(f.cancel(true));
675                  assertSame(f, f.fork());
# Line 689 | Line 688 | public class ForkJoinTaskTest extends JS
688       */
689      public void testCancelledForkQuietlyJoin() {
690          RecursiveAction a = new CheckedRecursiveAction() {
691 <            public void realCompute() {
691 >            protected void realCompute() {
692                  AsyncFib f = new AsyncFib(8);
693                  assertTrue(f.cancel(true));
694                  assertSame(f, f.fork());
# Line 705 | Line 704 | public class ForkJoinTaskTest extends JS
704      public void testGetPool() {
705          final ForkJoinPool mainPool = mainPool();
706          RecursiveAction a = new CheckedRecursiveAction() {
707 <            public void realCompute() {
707 >            protected void realCompute() {
708                  assertSame(mainPool, getPool());
709              }};
710          testInvokeOnPool(mainPool, a);
# Line 716 | Line 715 | public class ForkJoinTaskTest extends JS
715       */
716      public void testGetPool2() {
717          RecursiveAction a = new CheckedRecursiveAction() {
718 <            public void realCompute() {
718 >            protected void realCompute() {
719                  assertNull(getPool());
720              }};
721          assertNull(a.invoke());
# Line 727 | Line 726 | public class ForkJoinTaskTest extends JS
726       */
727      public void testInForkJoinPool() {
728          RecursiveAction a = new CheckedRecursiveAction() {
729 <            public void realCompute() {
729 >            protected void realCompute() {
730                  assertTrue(inForkJoinPool());
731              }};
732          testInvokeOnPool(mainPool(), a);
# Line 738 | Line 737 | public class ForkJoinTaskTest extends JS
737       */
738      public void testInForkJoinPool2() {
739          RecursiveAction a = new CheckedRecursiveAction() {
740 <            public void realCompute() {
740 >            protected void realCompute() {
741                  assertFalse(inForkJoinPool());
742              }};
743          assertNull(a.invoke());
# Line 749 | Line 748 | public class ForkJoinTaskTest extends JS
748       */
749      public void testSetRawResult() {
750          RecursiveAction a = new CheckedRecursiveAction() {
751 <            public void realCompute() {
751 >            protected void realCompute() {
752                  setRawResult(null);
753                  assertNull(getRawResult());
754              }};
# Line 761 | Line 760 | public class ForkJoinTaskTest extends JS
760       */
761      public void testCompleteExceptionally() {
762          RecursiveAction a = new CheckedRecursiveAction() {
763 <            public void realCompute() {
763 >            protected void realCompute() {
764                  AsyncFib f = new AsyncFib(8);
765                  f.completeExceptionally(new FJException());
766                  try {
# Line 775 | Line 774 | public class ForkJoinTaskTest extends JS
774      }
775  
776      /**
777 +     * completeExceptionally(null) surprisingly has the same effect as
778 +     * completeExceptionally(new RuntimeException())
779 +     */
780 +    public void testCompleteExceptionally_null() {
781 +        RecursiveAction a = new CheckedRecursiveAction() {
782 +            protected void realCompute() {
783 +                AsyncFib f = new AsyncFib(8);
784 +                f.completeExceptionally(null);
785 +                try {
786 +                    f.invoke();
787 +                    shouldThrow();
788 +                } catch (RuntimeException success) {
789 +                    assertSame(success.getClass(), RuntimeException.class);
790 +                    assertNull(success.getCause());
791 +                    checkCompletedAbnormally(f, success);
792 +                }
793 +            }};
794 +        testInvokeOnPool(mainPool(), a);
795 +    }
796 +
797 +    /**
798       * invokeAll(t1, t2) invokes all task arguments
799       */
800      public void testInvokeAll2() {
801          RecursiveAction a = new CheckedRecursiveAction() {
802 <            public void realCompute() {
802 >            protected void realCompute() {
803                  AsyncFib f = new AsyncFib(8);
804                  AsyncFib g = new AsyncFib(9);
805                  invokeAll(f, g);
# Line 796 | Line 816 | public class ForkJoinTaskTest extends JS
816       */
817      public void testInvokeAll1() {
818          RecursiveAction a = new CheckedRecursiveAction() {
819 <            public void realCompute() {
819 >            protected void realCompute() {
820                  AsyncFib f = new AsyncFib(8);
821                  invokeAll(f);
822                  checkCompletedNormally(f);
# Line 810 | Line 830 | public class ForkJoinTaskTest extends JS
830       */
831      public void testInvokeAll3() {
832          RecursiveAction a = new CheckedRecursiveAction() {
833 <            public void realCompute() {
833 >            protected void realCompute() {
834                  AsyncFib f = new AsyncFib(8);
835                  AsyncFib g = new AsyncFib(9);
836                  AsyncFib h = new AsyncFib(7);
# Line 830 | Line 850 | public class ForkJoinTaskTest extends JS
850       */
851      public void testInvokeAllCollection() {
852          RecursiveAction a = new CheckedRecursiveAction() {
853 <            public void realCompute() {
853 >            protected void realCompute() {
854                  AsyncFib f = new AsyncFib(8);
855                  AsyncFib g = new AsyncFib(9);
856                  AsyncFib h = new AsyncFib(7);
# Line 849 | Line 869 | public class ForkJoinTaskTest extends JS
869          testInvokeOnPool(mainPool(), a);
870      }
871  
852
872      /**
873       * invokeAll(tasks) with any null task throws NPE
874       */
875      public void testInvokeAllNPE() {
876          RecursiveAction a = new CheckedRecursiveAction() {
877 <            public void realCompute() {
877 >            protected void realCompute() {
878                  AsyncFib f = new AsyncFib(8);
879                  AsyncFib g = new AsyncFib(9);
880                  AsyncFib h = null;
# Line 872 | Line 891 | public class ForkJoinTaskTest extends JS
891       */
892      public void testAbnormalInvokeAll2() {
893          RecursiveAction a = new CheckedRecursiveAction() {
894 <            public void realCompute() {
894 >            protected void realCompute() {
895                  AsyncFib f = new AsyncFib(8);
896                  FailingAsyncFib g = new FailingAsyncFib(9);
897 +                ForkJoinTask[] tasks = { f, g };
898 +                shuffle(tasks);
899                  try {
900 <                    invokeAll(f, g);
900 >                    invokeAll(tasks);
901                      shouldThrow();
902                  } catch (FJException success) {
903                      checkCompletedAbnormally(g, success);
# Line 890 | Line 911 | public class ForkJoinTaskTest extends JS
911       */
912      public void testAbnormalInvokeAll1() {
913          RecursiveAction a = new CheckedRecursiveAction() {
914 <            public void realCompute() {
914 >            protected void realCompute() {
915                  FailingAsyncFib g = new FailingAsyncFib(9);
916                  try {
917                      invokeAll(g);
# Line 907 | Line 928 | public class ForkJoinTaskTest extends JS
928       */
929      public void testAbnormalInvokeAll3() {
930          RecursiveAction a = new CheckedRecursiveAction() {
931 <            public void realCompute() {
931 >            protected void realCompute() {
932                  AsyncFib f = new AsyncFib(8);
933                  FailingAsyncFib g = new FailingAsyncFib(9);
934                  AsyncFib h = new AsyncFib(7);
935 +                ForkJoinTask[] tasks = { f, g, h };
936 +                shuffle(tasks);
937                  try {
938 <                    invokeAll(f, g, h);
938 >                    invokeAll(tasks);
939                      shouldThrow();
940                  } catch (FJException success) {
941                      checkCompletedAbnormally(g, success);
# Line 922 | Line 945 | public class ForkJoinTaskTest extends JS
945      }
946  
947      /**
948 <     * invokeAll(collection)  throws exception if any task does
948 >     * invokeAll(collection) throws exception if any task does
949       */
950      public void testAbnormalInvokeAllCollection() {
951          RecursiveAction a = new CheckedRecursiveAction() {
952 <            public void realCompute() {
952 >            protected void realCompute() {
953                  FailingAsyncFib f = new FailingAsyncFib(8);
954                  AsyncFib g = new AsyncFib(9);
955                  AsyncFib h = new AsyncFib(7);
956 <                HashSet set = new HashSet();
957 <                set.add(f);
935 <                set.add(g);
936 <                set.add(h);
956 >                ForkJoinTask[] tasks = { f, g, h };
957 >                shuffle(tasks);
958                  try {
959 <                    invokeAll(set);
959 >                    invokeAll(Arrays.asList(tasks));
960                      shouldThrow();
961                  } catch (FJException success) {
962                      checkCompletedAbnormally(f, success);
# Line 950 | Line 971 | public class ForkJoinTaskTest extends JS
971       */
972      public void testTryUnfork() {
973          RecursiveAction a = new CheckedRecursiveAction() {
974 <            public void realCompute() {
974 >            protected void realCompute() {
975                  AsyncFib g = new AsyncFib(9);
976                  assertSame(g, g.fork());
977                  AsyncFib f = new AsyncFib(8);
# Line 969 | Line 990 | public class ForkJoinTaskTest extends JS
990       */
991      public void testGetSurplusQueuedTaskCount() {
992          RecursiveAction a = new CheckedRecursiveAction() {
993 <            public void realCompute() {
993 >            protected void realCompute() {
994                  AsyncFib h = new AsyncFib(7);
995                  assertSame(h, h.fork());
996                  AsyncFib g = new AsyncFib(9);
# Line 991 | Line 1012 | public class ForkJoinTaskTest extends JS
1012       */
1013      public void testPeekNextLocalTask() {
1014          RecursiveAction a = new CheckedRecursiveAction() {
1015 <            public void realCompute() {
1015 >            protected void realCompute() {
1016                  AsyncFib g = new AsyncFib(9);
1017                  assertSame(g, g.fork());
1018                  AsyncFib f = new AsyncFib(8);
# Line 1011 | Line 1032 | public class ForkJoinTaskTest extends JS
1032       */
1033      public void testPollNextLocalTask() {
1034          RecursiveAction a = new CheckedRecursiveAction() {
1035 <            public void realCompute() {
1035 >            protected void realCompute() {
1036                  AsyncFib g = new AsyncFib(9);
1037                  assertSame(g, g.fork());
1038                  AsyncFib f = new AsyncFib(8);
# Line 1030 | Line 1051 | public class ForkJoinTaskTest extends JS
1051       */
1052      public void testPollTask() {
1053          RecursiveAction a = new CheckedRecursiveAction() {
1054 <            public void realCompute() {
1054 >            protected void realCompute() {
1055                  AsyncFib g = new AsyncFib(9);
1056                  assertSame(g, g.fork());
1057                  AsyncFib f = new AsyncFib(8);
# Line 1048 | Line 1069 | public class ForkJoinTaskTest extends JS
1069       */
1070      public void testPeekNextLocalTaskAsync() {
1071          RecursiveAction a = new CheckedRecursiveAction() {
1072 <            public void realCompute() {
1072 >            protected void realCompute() {
1073                  AsyncFib g = new AsyncFib(9);
1074                  assertSame(g, g.fork());
1075                  AsyncFib f = new AsyncFib(8);
# Line 1069 | Line 1090 | public class ForkJoinTaskTest extends JS
1090       */
1091      public void testPollNextLocalTaskAsync() {
1092          RecursiveAction a = new CheckedRecursiveAction() {
1093 <            public void realCompute() {
1093 >            protected void realCompute() {
1094                  AsyncFib g = new AsyncFib(9);
1095                  assertSame(g, g.fork());
1096                  AsyncFib f = new AsyncFib(8);
# Line 1089 | Line 1110 | public class ForkJoinTaskTest extends JS
1110       */
1111      public void testPollTaskAsync() {
1112          RecursiveAction a = new CheckedRecursiveAction() {
1113 <            public void realCompute() {
1113 >            protected void realCompute() {
1114                  AsyncFib g = new AsyncFib(9);
1115                  assertSame(g, g.fork());
1116                  AsyncFib f = new AsyncFib(8);
# Line 1112 | Line 1133 | public class ForkJoinTaskTest extends JS
1133       */
1134      public void testInvokeSingleton() {
1135          RecursiveAction a = new CheckedRecursiveAction() {
1136 <            public void realCompute() {
1136 >            protected void realCompute() {
1137                  AsyncFib f = new AsyncFib(8);
1138                  assertNull(f.invoke());
1139                  assertEquals(21, f.number);
# Line 1128 | Line 1149 | public class ForkJoinTaskTest extends JS
1149       */
1150      public void testQuietlyInvokeSingleton() {
1151          RecursiveAction a = new CheckedRecursiveAction() {
1152 <            public void realCompute() {
1152 >            protected void realCompute() {
1153                  AsyncFib f = new AsyncFib(8);
1154                  f.quietlyInvoke();
1155                  assertEquals(21, f.number);
# Line 1142 | Line 1163 | public class ForkJoinTaskTest extends JS
1163       */
1164      public void testForkJoinSingleton() {
1165          RecursiveAction a = new CheckedRecursiveAction() {
1166 <            public void realCompute() {
1166 >            protected void realCompute() {
1167                  AsyncFib f = new AsyncFib(8);
1168                  assertSame(f, f.fork());
1169                  assertNull(f.join());
# Line 1157 | Line 1178 | public class ForkJoinTaskTest extends JS
1178       */
1179      public void testForkGetSingleton() {
1180          RecursiveAction a = new CheckedRecursiveAction() {
1181 <            public void realCompute() throws Exception {
1181 >            protected void realCompute() throws Exception {
1182                  AsyncFib f = new AsyncFib(8);
1183                  assertSame(f, f.fork());
1184                  assertNull(f.get());
# Line 1172 | Line 1193 | public class ForkJoinTaskTest extends JS
1193       */
1194      public void testForkTimedGetSingleton() {
1195          RecursiveAction a = new CheckedRecursiveAction() {
1196 <            public void realCompute() throws Exception {
1196 >            protected void realCompute() throws Exception {
1197                  AsyncFib f = new AsyncFib(8);
1198                  assertSame(f, f.fork());
1199                  assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
# Line 1187 | Line 1208 | public class ForkJoinTaskTest extends JS
1208       */
1209      public void testForkTimedGetNPESingleton() {
1210          RecursiveAction a = new CheckedRecursiveAction() {
1211 <            public void realCompute() throws Exception {
1211 >            protected void realCompute() throws Exception {
1212                  AsyncFib f = new AsyncFib(8);
1213                  assertSame(f, f.fork());
1214                  try {
# Line 1203 | Line 1224 | public class ForkJoinTaskTest extends JS
1224       */
1225      public void testForkQuietlyJoinSingleton() {
1226          RecursiveAction a = new CheckedRecursiveAction() {
1227 <            public void realCompute() {
1227 >            protected void realCompute() {
1228                  AsyncFib f = new AsyncFib(8);
1229                  assertSame(f, f.fork());
1230                  f.quietlyJoin();
# Line 1213 | Line 1234 | public class ForkJoinTaskTest extends JS
1234          testInvokeOnPool(singletonPool(), a);
1235      }
1236  
1216
1237      /**
1238       * helpQuiesce returns when tasks are complete.
1239       * getQueuedTaskCount returns 0 when quiescent
1240       */
1241      public void testForkHelpQuiesceSingleton() {
1242          RecursiveAction a = new CheckedRecursiveAction() {
1243 <            public void realCompute() {
1243 >            protected void realCompute() {
1244                  AsyncFib f = new AsyncFib(8);
1245                  assertSame(f, f.fork());
1246 <                f.helpQuiesce();
1246 >                helpQuiesce();
1247                  assertEquals(0, getQueuedTaskCount());
1248                  assertEquals(21, f.number);
1249                  checkCompletedNormally(f);
# Line 1231 | Line 1251 | public class ForkJoinTaskTest extends JS
1251          testInvokeOnPool(singletonPool(), a);
1252      }
1253  
1234
1254      /**
1255       * invoke task throws exception when task completes abnormally
1256       */
1257      public void testAbnormalInvokeSingleton() {
1258          RecursiveAction a = new CheckedRecursiveAction() {
1259 <            public void realCompute() {
1259 >            protected void realCompute() {
1260                  FailingAsyncFib f = new FailingAsyncFib(8);
1261                  try {
1262                      f.invoke();
# Line 1254 | Line 1273 | public class ForkJoinTaskTest extends JS
1273       */
1274      public void testAbnormalQuietlyInvokeSingleton() {
1275          RecursiveAction a = new CheckedRecursiveAction() {
1276 <            public void realCompute() {
1276 >            protected void realCompute() {
1277                  FailingAsyncFib f = new FailingAsyncFib(8);
1278                  f.quietlyInvoke();
1279                  assertTrue(f.getException() instanceof FJException);
# Line 1268 | Line 1287 | public class ForkJoinTaskTest extends JS
1287       */
1288      public void testAbnormalForkJoinSingleton() {
1289          RecursiveAction a = new CheckedRecursiveAction() {
1290 <            public void realCompute() {
1290 >            protected void realCompute() {
1291                  FailingAsyncFib f = new FailingAsyncFib(8);
1292                  assertSame(f, f.fork());
1293                  try {
# Line 1286 | Line 1305 | public class ForkJoinTaskTest extends JS
1305       */
1306      public void testAbnormalForkGetSingleton() {
1307          RecursiveAction a = new CheckedRecursiveAction() {
1308 <            public void realCompute() throws Exception {
1308 >            protected void realCompute() throws Exception {
1309                  FailingAsyncFib f = new FailingAsyncFib(8);
1310                  assertSame(f, f.fork());
1311                  try {
# Line 1306 | Line 1325 | public class ForkJoinTaskTest extends JS
1325       */
1326      public void testAbnormalForkTimedGetSingleton() {
1327          RecursiveAction a = new CheckedRecursiveAction() {
1328 <            public void realCompute() throws Exception {
1328 >            protected void realCompute() throws Exception {
1329                  FailingAsyncFib f = new FailingAsyncFib(8);
1330                  assertSame(f, f.fork());
1331                  try {
# Line 1326 | Line 1345 | public class ForkJoinTaskTest extends JS
1345       */
1346      public void testAbnormalForkQuietlyJoinSingleton() {
1347          RecursiveAction a = new CheckedRecursiveAction() {
1348 <            public void realCompute() {
1348 >            protected void realCompute() {
1349                  FailingAsyncFib f = new FailingAsyncFib(8);
1350                  assertSame(f, f.fork());
1351                  f.quietlyJoin();
# Line 1341 | Line 1360 | public class ForkJoinTaskTest extends JS
1360       */
1361      public void testCancelledInvokeSingleton() {
1362          RecursiveAction a = new CheckedRecursiveAction() {
1363 <            public void realCompute() {
1363 >            protected void realCompute() {
1364                  AsyncFib f = new AsyncFib(8);
1365                  assertTrue(f.cancel(true));
1366                  try {
# Line 1359 | Line 1378 | public class ForkJoinTaskTest extends JS
1378       */
1379      public void testCancelledForkJoinSingleton() {
1380          RecursiveAction a = new CheckedRecursiveAction() {
1381 <            public void realCompute() {
1381 >            protected void realCompute() {
1382                  AsyncFib f = new AsyncFib(8);
1383                  assertTrue(f.cancel(true));
1384                  assertSame(f, f.fork());
# Line 1378 | Line 1397 | public class ForkJoinTaskTest extends JS
1397       */
1398      public void testCancelledForkGetSingleton() {
1399          RecursiveAction a = new CheckedRecursiveAction() {
1400 <            public void realCompute() throws Exception {
1400 >            protected void realCompute() throws Exception {
1401                  AsyncFib f = new AsyncFib(8);
1402                  assertTrue(f.cancel(true));
1403                  assertSame(f, f.fork());
# Line 1397 | Line 1416 | public class ForkJoinTaskTest extends JS
1416       */
1417      public void testCancelledForkTimedGetSingleton() throws Exception {
1418          RecursiveAction a = new CheckedRecursiveAction() {
1419 <            public void realCompute() throws Exception {
1419 >            protected void realCompute() throws Exception {
1420                  AsyncFib f = new AsyncFib(8);
1421                  assertTrue(f.cancel(true));
1422                  assertSame(f, f.fork());
# Line 1416 | Line 1435 | public class ForkJoinTaskTest extends JS
1435       */
1436      public void testCancelledForkQuietlyJoinSingleton() {
1437          RecursiveAction a = new CheckedRecursiveAction() {
1438 <            public void realCompute() {
1438 >            protected void realCompute() {
1439                  AsyncFib f = new AsyncFib(8);
1440                  assertTrue(f.cancel(true));
1441                  assertSame(f, f.fork());
# Line 1431 | Line 1450 | public class ForkJoinTaskTest extends JS
1450       */
1451      public void testCompleteExceptionallySingleton() {
1452          RecursiveAction a = new CheckedRecursiveAction() {
1453 <            public void realCompute() {
1453 >            protected void realCompute() {
1454                  AsyncFib f = new AsyncFib(8);
1455                  f.completeExceptionally(new FJException());
1456                  try {
# Line 1449 | Line 1468 | public class ForkJoinTaskTest extends JS
1468       */
1469      public void testInvokeAll2Singleton() {
1470          RecursiveAction a = new CheckedRecursiveAction() {
1471 <            public void realCompute() {
1471 >            protected void realCompute() {
1472                  AsyncFib f = new AsyncFib(8);
1473                  AsyncFib g = new AsyncFib(9);
1474                  invokeAll(f, g);
# Line 1466 | Line 1485 | public class ForkJoinTaskTest extends JS
1485       */
1486      public void testInvokeAll1Singleton() {
1487          RecursiveAction a = new CheckedRecursiveAction() {
1488 <            public void realCompute() {
1488 >            protected void realCompute() {
1489                  AsyncFib f = new AsyncFib(8);
1490                  invokeAll(f);
1491                  checkCompletedNormally(f);
# Line 1480 | Line 1499 | public class ForkJoinTaskTest extends JS
1499       */
1500      public void testInvokeAll3Singleton() {
1501          RecursiveAction a = new CheckedRecursiveAction() {
1502 <            public void realCompute() {
1502 >            protected void realCompute() {
1503                  AsyncFib f = new AsyncFib(8);
1504                  AsyncFib g = new AsyncFib(9);
1505                  AsyncFib h = new AsyncFib(7);
# Line 1500 | Line 1519 | public class ForkJoinTaskTest extends JS
1519       */
1520      public void testInvokeAllCollectionSingleton() {
1521          RecursiveAction a = new CheckedRecursiveAction() {
1522 <            public void realCompute() {
1522 >            protected void realCompute() {
1523                  AsyncFib f = new AsyncFib(8);
1524                  AsyncFib g = new AsyncFib(9);
1525                  AsyncFib h = new AsyncFib(7);
# Line 1519 | Line 1538 | public class ForkJoinTaskTest extends JS
1538          testInvokeOnPool(singletonPool(), a);
1539      }
1540  
1522
1541      /**
1542       * invokeAll(tasks) with any null task throws NPE
1543       */
1544      public void testInvokeAllNPESingleton() {
1545          RecursiveAction a = new CheckedRecursiveAction() {
1546 <            public void realCompute() {
1546 >            protected void realCompute() {
1547                  AsyncFib f = new AsyncFib(8);
1548                  AsyncFib g = new AsyncFib(9);
1549                  AsyncFib h = null;
# Line 1542 | Line 1560 | public class ForkJoinTaskTest extends JS
1560       */
1561      public void testAbnormalInvokeAll2Singleton() {
1562          RecursiveAction a = new CheckedRecursiveAction() {
1563 <            public void realCompute() {
1563 >            protected void realCompute() {
1564                  AsyncFib f = new AsyncFib(8);
1565                  FailingAsyncFib g = new FailingAsyncFib(9);
1566 +                ForkJoinTask[] tasks = { f, g };
1567 +                shuffle(tasks);
1568                  try {
1569 <                    invokeAll(f, g);
1569 >                    invokeAll(tasks);
1570                      shouldThrow();
1571                  } catch (FJException success) {
1572                      checkCompletedAbnormally(g, success);
# Line 1560 | Line 1580 | public class ForkJoinTaskTest extends JS
1580       */
1581      public void testAbnormalInvokeAll1Singleton() {
1582          RecursiveAction a = new CheckedRecursiveAction() {
1583 <            public void realCompute() {
1583 >            protected void realCompute() {
1584                  FailingAsyncFib g = new FailingAsyncFib(9);
1585                  try {
1586                      invokeAll(g);
# Line 1577 | Line 1597 | public class ForkJoinTaskTest extends JS
1597       */
1598      public void testAbnormalInvokeAll3Singleton() {
1599          RecursiveAction a = new CheckedRecursiveAction() {
1600 <            public void realCompute() {
1600 >            protected void realCompute() {
1601                  AsyncFib f = new AsyncFib(8);
1602                  FailingAsyncFib g = new FailingAsyncFib(9);
1603                  AsyncFib h = new AsyncFib(7);
1604 +                ForkJoinTask[] tasks = { f, g, h };
1605 +                shuffle(tasks);
1606                  try {
1607 <                    invokeAll(f, g, h);
1607 >                    invokeAll(tasks);
1608                      shouldThrow();
1609                  } catch (FJException success) {
1610                      checkCompletedAbnormally(g, success);
# Line 1592 | Line 1614 | public class ForkJoinTaskTest extends JS
1614      }
1615  
1616      /**
1617 <     * invokeAll(collection)  throws exception if any task does
1617 >     * invokeAll(collection) throws exception if any task does
1618       */
1619      public void testAbnormalInvokeAllCollectionSingleton() {
1620          RecursiveAction a = new CheckedRecursiveAction() {
1621 <            public void realCompute() {
1621 >            protected void realCompute() {
1622                  FailingAsyncFib f = new FailingAsyncFib(8);
1623                  AsyncFib g = new AsyncFib(9);
1624                  AsyncFib h = new AsyncFib(7);
1625 <                HashSet set = new HashSet();
1626 <                set.add(f);
1605 <                set.add(g);
1606 <                set.add(h);
1625 >                ForkJoinTask[] tasks = { f, g, h };
1626 >                shuffle(tasks);
1627                  try {
1628 <                    invokeAll(set);
1628 >                    invokeAll(Arrays.asList(tasks));
1629                      shouldThrow();
1630                  } catch (FJException success) {
1631                      checkCompletedAbnormally(f, success);
# Line 1614 | Line 1634 | public class ForkJoinTaskTest extends JS
1634          testInvokeOnPool(singletonPool(), a);
1635      }
1636  
1637 +    /**
1638 +     * ForkJoinTask.quietlyComplete returns when task completes
1639 +     * normally without setting a value. The most recent value
1640 +     * established by setRawResult(V) (or null by default) is returned
1641 +     * from invoke.
1642 +     */
1643 +    public void testQuietlyComplete() {
1644 +        RecursiveAction a = new CheckedRecursiveAction() {
1645 +                protected void realCompute() {
1646 +                    AsyncFib f = new AsyncFib(8);
1647 +                    f.quietlyComplete();
1648 +                    assertEquals(8, f.number);
1649 +                    checkCompletedNormally(f);
1650 +                }};
1651 +        testInvokeOnPool(mainPool(), a);
1652 +    }
1653 +
1654   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines