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.41 by jsr166, Sat Apr 25 04:55:30 2015 UTC vs.
Revision 1.49 by jsr166, Sun Oct 18 16:40:57 2015 UTC

# Line 7 | Line 7
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.Collections;
12   import java.util.HashSet;
13 + import java.util.List;
14   import java.util.concurrent.CancellationException;
15   import java.util.concurrent.ExecutionException;
16   import java.util.concurrent.ForkJoinPool;
# Line 48 | Line 51 | public class ForkJoinTaskTest extends JS
51      }
52  
53      private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
54 <        try {
54 >        try (PoolCleaner cleaner = cleaner(pool)) {
55              assertFalse(a.isDone());
56              assertFalse(a.isCompletedNormally());
57              assertFalse(a.isCompletedAbnormally());
# Line 64 | Line 67 | public class ForkJoinTaskTest extends JS
67              assertFalse(a.isCancelled());
68              assertNull(a.getException());
69              assertNull(a.getRawResult());
67        } finally {
68            joinPool(pool);
70          }
71      }
72  
# Line 98 | Line 99 | public class ForkJoinTaskTest extends JS
99  
100          {
101              Thread.currentThread().interrupt();
102 <            long t0 = System.nanoTime();
102 >            long startTime = System.nanoTime();
103              assertSame(expected, a.join());
104 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
104 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
105              Thread.interrupted();
106          }
107  
108          {
109              Thread.currentThread().interrupt();
110 <            long t0 = System.nanoTime();
110 >            long startTime = System.nanoTime();
111              a.quietlyJoin();        // should be no-op
112 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
112 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
113              Thread.interrupted();
114          }
115  
# Line 141 | Line 142 | public class ForkJoinTaskTest extends JS
142          Thread.interrupted();
143  
144          {
145 <            long t0 = System.nanoTime();
145 >            long startTime = System.nanoTime();
146              a.quietlyJoin();        // should be no-op
147 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
147 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
148          }
149  
150          try {
# Line 179 | Line 180 | public class ForkJoinTaskTest extends JS
180          Thread.interrupted();
181  
182          {
183 <            long t0 = System.nanoTime();
183 >            long startTime = System.nanoTime();
184              a.quietlyJoin();        // should be no-op
185 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
185 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
186          }
187  
188          try {
# Line 218 | Line 219 | public class ForkJoinTaskTest extends JS
219              AtomicIntegerFieldUpdater.newUpdater(BinaryAsyncAction.class,
220                                                   "controlState");
221  
222 <        private BinaryAsyncAction parent;
222 >        private volatile BinaryAsyncAction parent;
223  
224 <        private BinaryAsyncAction sibling;
224 >        private volatile BinaryAsyncAction sibling;
225  
226          protected BinaryAsyncAction() {
227          }
# Line 255 | Line 256 | public class ForkJoinTaskTest extends JS
256              super.completeExceptionally(ex);
257          }
258  
259 +        public boolean cancel(boolean mayInterruptIfRunning) {
260 +            if (super.cancel(mayInterruptIfRunning)) {
261 +                completeExceptionally(new FJException());
262 +                return true;
263 +            }
264 +            return false;
265 +        }
266 +
267          public final void complete() {
268              BinaryAsyncAction a = this;
269              for (;;) {
# Line 276 | Line 285 | public class ForkJoinTaskTest extends JS
285          }
286  
287          public final void completeExceptionally(Throwable ex) {
288 <            BinaryAsyncAction a = this;
280 <            while (!a.isCompletedAbnormally()) {
288 >            for (BinaryAsyncAction a = this;;) {
289                  a.completeThisExceptionally(ex);
290                  BinaryAsyncAction s = a.sibling;
291 <                if (s != null)
292 <                    s.cancel(false);
293 <                if (!a.onException() || (a = a.parent) == null)
291 >                if (s != null && !s.isDone())
292 >                    s.completeExceptionally(ex);
293 >                if ((a = a.parent) == null)
294                      break;
295              }
296          }
# Line 332 | Line 340 | public class ForkJoinTaskTest extends JS
340          public final boolean exec() {
341              AsyncFib f = this;
342              int n = f.number;
343 <            if (n > 1) {
344 <                while (n > 1) {
345 <                    AsyncFib p = f;
346 <                    AsyncFib r = new AsyncFib(n - 2);
347 <                    f = new AsyncFib(--n);
348 <                    p.linkSubtasks(r, f);
341 <                    r.fork();
342 <                }
343 <                f.number = n;
343 >            while (n > 1) {
344 >                AsyncFib p = f;
345 >                AsyncFib r = new AsyncFib(n - 2);
346 >                f = new AsyncFib(--n);
347 >                p.linkSubtasks(r, f);
348 >                r.fork();
349              }
350              f.complete();
351              return false;
# Line 360 | Line 365 | public class ForkJoinTaskTest extends JS
365          public final boolean exec() {
366              FailingAsyncFib f = this;
367              int n = f.number;
368 <            if (n > 1) {
369 <                while (n > 1) {
370 <                    FailingAsyncFib p = f;
371 <                    FailingAsyncFib r = new FailingAsyncFib(n - 2);
372 <                    f = new FailingAsyncFib(--n);
373 <                    p.linkSubtasks(r, f);
369 <                    r.fork();
370 <                }
371 <                f.number = n;
368 >            while (n > 1) {
369 >                FailingAsyncFib p = f;
370 >                FailingAsyncFib r = new FailingAsyncFib(n - 2);
371 >                f = new FailingAsyncFib(--n);
372 >                p.linkSubtasks(r, f);
373 >                r.fork();
374              }
375              f.complete();
376              return false;
# Line 774 | Line 776 | public class ForkJoinTaskTest extends JS
776      }
777  
778      /**
779 +     * completeExceptionally(null) surprisingly has the same effect as
780 +     * completeExceptionally(new RuntimeException())
781 +     */
782 +    public void testCompleteExceptionally_null() {
783 +        RecursiveAction a = new CheckedRecursiveAction() {
784 +            protected void realCompute() {
785 +                AsyncFib f = new AsyncFib(8);
786 +                f.completeExceptionally(null);
787 +                try {
788 +                    f.invoke();
789 +                    shouldThrow();
790 +                } catch (RuntimeException success) {
791 +                    assertSame(success.getClass(), RuntimeException.class);
792 +                    assertNull(success.getCause());
793 +                    checkCompletedAbnormally(f, success);
794 +                }
795 +            }};
796 +        testInvokeOnPool(mainPool(), a);
797 +    }
798 +
799 +    /**
800       * invokeAll(t1, t2) invokes all task arguments
801       */
802      public void testInvokeAll2() {
# Line 873 | Line 896 | public class ForkJoinTaskTest extends JS
896              protected void realCompute() {
897                  AsyncFib f = new AsyncFib(8);
898                  FailingAsyncFib g = new FailingAsyncFib(9);
899 +                ForkJoinTask[] tasks = { f, g };
900 +                Collections.shuffle(Arrays.asList(tasks));
901                  try {
902 <                    invokeAll(f, g);
902 >                    invokeAll(tasks);
903                      shouldThrow();
904                  } catch (FJException success) {
905                      checkCompletedAbnormally(g, success);
# Line 909 | Line 934 | public class ForkJoinTaskTest extends JS
934                  AsyncFib f = new AsyncFib(8);
935                  FailingAsyncFib g = new FailingAsyncFib(9);
936                  AsyncFib h = new AsyncFib(7);
937 +                ForkJoinTask[] tasks = { f, g, h };
938 +                Collections.shuffle(Arrays.asList(tasks));
939                  try {
940 <                    invokeAll(f, g, h);
940 >                    invokeAll(tasks);
941                      shouldThrow();
942                  } catch (FJException success) {
943                      checkCompletedAbnormally(g, success);
# Line 928 | Line 955 | public class ForkJoinTaskTest extends JS
955                  FailingAsyncFib f = new FailingAsyncFib(8);
956                  AsyncFib g = new AsyncFib(9);
957                  AsyncFib h = new AsyncFib(7);
958 <                HashSet set = new HashSet();
959 <                set.add(f);
960 <                set.add(g);
934 <                set.add(h);
958 >                ForkJoinTask[] tasks = { f, g, h };
959 >                List taskList = Arrays.asList(tasks);
960 >                Collections.shuffle(taskList);
961                  try {
962 <                    invokeAll(set);
962 >                    invokeAll(taskList);
963                      shouldThrow();
964                  } catch (FJException success) {
965                      checkCompletedAbnormally(f, success);
# Line 1540 | Line 1566 | public class ForkJoinTaskTest extends JS
1566              protected void realCompute() {
1567                  AsyncFib f = new AsyncFib(8);
1568                  FailingAsyncFib g = new FailingAsyncFib(9);
1569 +                ForkJoinTask[] tasks = { f, g };
1570 +                Collections.shuffle(Arrays.asList(tasks));
1571                  try {
1572 <                    invokeAll(f, g);
1572 >                    invokeAll(tasks);
1573                      shouldThrow();
1574                  } catch (FJException success) {
1575                      checkCompletedAbnormally(g, success);
# Line 1576 | Line 1604 | public class ForkJoinTaskTest extends JS
1604                  AsyncFib f = new AsyncFib(8);
1605                  FailingAsyncFib g = new FailingAsyncFib(9);
1606                  AsyncFib h = new AsyncFib(7);
1607 +                ForkJoinTask[] tasks = { f, g, h };
1608 +                Collections.shuffle(Arrays.asList(tasks));
1609                  try {
1610 <                    invokeAll(f, g, h);
1610 >                    invokeAll(tasks);
1611                      shouldThrow();
1612                  } catch (FJException success) {
1613                      checkCompletedAbnormally(g, success);
# Line 1595 | Line 1625 | public class ForkJoinTaskTest extends JS
1625                  FailingAsyncFib f = new FailingAsyncFib(8);
1626                  AsyncFib g = new AsyncFib(9);
1627                  AsyncFib h = new AsyncFib(7);
1628 <                HashSet set = new HashSet();
1629 <                set.add(f);
1630 <                set.add(g);
1601 <                set.add(h);
1628 >                ForkJoinTask[] tasks = { f, g, h };
1629 >                List taskList = Arrays.asList(tasks);
1630 >                Collections.shuffle(taskList);
1631                  try {
1632 <                    invokeAll(set);
1632 >                    invokeAll(taskList);
1633                      shouldThrow();
1634                  } catch (FJException success) {
1635                      checkCompletedAbnormally(f, success);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines