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.48 by dl, Sun Oct 11 19:53:59 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 873 | Line 875 | public class ForkJoinTaskTest extends JS
875              protected void realCompute() {
876                  AsyncFib f = new AsyncFib(8);
877                  FailingAsyncFib g = new FailingAsyncFib(9);
878 +                ForkJoinTask[] tasks = { f, g };
879 +                Collections.shuffle(Arrays.asList(tasks));
880                  try {
881 <                    invokeAll(f, g);
881 >                    invokeAll(tasks);
882                      shouldThrow();
883                  } catch (FJException success) {
884                      checkCompletedAbnormally(g, success);
# Line 909 | Line 913 | public class ForkJoinTaskTest extends JS
913                  AsyncFib f = new AsyncFib(8);
914                  FailingAsyncFib g = new FailingAsyncFib(9);
915                  AsyncFib h = new AsyncFib(7);
916 +                ForkJoinTask[] tasks = { f, g, h };
917 +                Collections.shuffle(Arrays.asList(tasks));
918                  try {
919 <                    invokeAll(f, g, h);
919 >                    invokeAll(tasks);
920                      shouldThrow();
921                  } catch (FJException success) {
922                      checkCompletedAbnormally(g, success);
# Line 928 | Line 934 | public class ForkJoinTaskTest extends JS
934                  FailingAsyncFib f = new FailingAsyncFib(8);
935                  AsyncFib g = new AsyncFib(9);
936                  AsyncFib h = new AsyncFib(7);
937 <                HashSet set = new HashSet();
938 <                set.add(f);
939 <                set.add(g);
934 <                set.add(h);
937 >                ForkJoinTask[] tasks = { f, g, h };
938 >                List taskList = Arrays.asList(tasks);
939 >                Collections.shuffle(taskList);
940                  try {
941 <                    invokeAll(set);
941 >                    invokeAll(taskList);
942                      shouldThrow();
943                  } catch (FJException success) {
944                      checkCompletedAbnormally(f, success);
# Line 1540 | Line 1545 | public class ForkJoinTaskTest extends JS
1545              protected void realCompute() {
1546                  AsyncFib f = new AsyncFib(8);
1547                  FailingAsyncFib g = new FailingAsyncFib(9);
1548 +                ForkJoinTask[] tasks = { f, g };
1549 +                Collections.shuffle(Arrays.asList(tasks));
1550                  try {
1551 <                    invokeAll(f, g);
1551 >                    invokeAll(tasks);
1552                      shouldThrow();
1553                  } catch (FJException success) {
1554                      checkCompletedAbnormally(g, success);
# Line 1576 | Line 1583 | public class ForkJoinTaskTest extends JS
1583                  AsyncFib f = new AsyncFib(8);
1584                  FailingAsyncFib g = new FailingAsyncFib(9);
1585                  AsyncFib h = new AsyncFib(7);
1586 +                ForkJoinTask[] tasks = { f, g, h };
1587 +                Collections.shuffle(Arrays.asList(tasks));
1588                  try {
1589 <                    invokeAll(f, g, h);
1589 >                    invokeAll(tasks);
1590                      shouldThrow();
1591                  } catch (FJException success) {
1592                      checkCompletedAbnormally(g, success);
# Line 1595 | Line 1604 | public class ForkJoinTaskTest extends JS
1604                  FailingAsyncFib f = new FailingAsyncFib(8);
1605                  AsyncFib g = new AsyncFib(9);
1606                  AsyncFib h = new AsyncFib(7);
1607 <                HashSet set = new HashSet();
1608 <                set.add(f);
1609 <                set.add(g);
1601 <                set.add(h);
1607 >                ForkJoinTask[] tasks = { f, g, h };
1608 >                List taskList = Arrays.asList(tasks);
1609 >                Collections.shuffle(taskList);
1610                  try {
1611 <                    invokeAll(set);
1611 >                    invokeAll(taskList);
1612                      shouldThrow();
1613                  } catch (FJException success) {
1614                      checkCompletedAbnormally(f, success);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines