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.36 by jsr166, Mon Jul 22 18:01:03 2013 UTC vs.
Revision 1.51 by jsr166, Wed Aug 24 22:22:39 2016 UTC

# Line 3 | Line 3
3   * Expert Group and released to the public domain, as explained at
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);
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);
110 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
111              Thread.interrupted();
112          }
113  
# Line 139 | Line 140 | public class ForkJoinTaskTest extends JS
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 177 | Line 178 | public class ForkJoinTaskTest extends JS
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 {
# 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 358 | 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);
367 <                    r.fork();
368 <                }
369 <                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 772 | 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() {
# Line 871 | Line 894 | public class ForkJoinTaskTest extends JS
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 907 | Line 932 | public class ForkJoinTaskTest extends JS
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 918 | 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() {
# Line 926 | Line 953 | public class ForkJoinTaskTest extends JS
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);
931 <                set.add(g);
932 <                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 1538 | Line 1563 | public class ForkJoinTaskTest extends JS
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 1574 | Line 1601 | public class ForkJoinTaskTest extends JS
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 1585 | 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() {
# Line 1593 | Line 1622 | public class ForkJoinTaskTest extends JS
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);
1598 <                set.add(g);
1599 <                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);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines