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

Comparing jsr166/src/test/tck/CompletableFutureTest.java (file contents):
Revision 1.62 by jsr166, Fri Jun 6 01:19:22 2014 UTC vs.
Revision 1.77 by jsr166, Sat Jun 7 21:46:50 2014 UTC

# Line 105 | Line 105 | public class CompletableFutureTest exten
105          assertTrue(f.toString().contains("[Completed exceptionally]"));
106      }
107  
108 <    void checkCompletedWithWrappedCFException(CompletableFuture<?> f,
109 <                                              CFException ex) {
108 >    <U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f,
109 >                                                      Throwable ex) {
110          try {
111              f.get(LONG_DELAY_MS, MILLISECONDS);
112              shouldThrow();
# Line 131 | Line 131 | public class CompletableFutureTest exten
131          } catch (ExecutionException success) {
132              assertSame(ex, success.getCause());
133          } catch (Throwable fail) { threadUnexpectedException(fail); }
134 +
135          assertTrue(f.isDone());
136          assertFalse(f.isCancelled());
137          assertTrue(f.toString().contains("[Completed exceptionally]"));
138      }
139  
140 +    <U> void checkCompletedWithWrappedException(CompletableFuture<U> f,
141 +                                                Throwable ex) {
142 +        checkCompletedExceptionallyWithRootCause(f, ex);
143 +        try {
144 +            CompletableFuture<Throwable> spy = f.handle
145 +                ((U u, Throwable t) -> t);
146 +            assertTrue(spy.join() instanceof CompletionException);
147 +            assertSame(ex, spy.join().getCause());
148 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
149 +    }
150 +
151 +    <U> void checkCompletedExceptionally(CompletableFuture<U> f, Throwable ex) {
152 +        checkCompletedExceptionallyWithRootCause(f, ex);
153 +        try {
154 +            CompletableFuture<Throwable> spy = f.handle
155 +                ((U u, Throwable t) -> t);
156 +            assertSame(ex, spy.join());
157 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
158 +    }
159 +
160      void checkCancelled(CompletableFuture<?> f) {
161          try {
162              f.get(LONG_DELAY_MS, MILLISECONDS);
# Line 218 | Line 239 | public class CompletableFutureTest exten
239       */
240      public void testCompleteExceptionally() {
241          CompletableFuture<Integer> f = new CompletableFuture<>();
242 +        CFException ex = new CFException();
243          checkIncomplete(f);
244 <        f.completeExceptionally(new CFException());
245 <        checkCompletedWithWrappedCFException(f);
244 >        f.completeExceptionally(ex);
245 >        checkCompletedExceptionally(f, ex);
246      }
247  
248      /**
# Line 261 | Line 283 | public class CompletableFutureTest exten
283       * obtrudeException forces completion with given exception
284       */
285      public void testObtrudeException() {
286 <        CompletableFuture<Integer> f = new CompletableFuture<>();
287 <        checkIncomplete(f);
288 <        f.complete(one);
289 <        checkCompletedNormally(f, one);
290 <        f.obtrudeException(new CFException());
269 <        checkCompletedWithWrappedCFException(f);
286 >        for (Integer v1 : new Integer[] { 1, null })
287 >    {
288 >        CFException ex;
289 >        CompletableFuture<Integer> f;
290 >
291          f = new CompletableFuture<>();
292 <        f.obtrudeException(new CFException());
293 <        checkCompletedWithWrappedCFException(f);
292 >        f.complete(v1);
293 >        for (int i = 0; i < 2; i++) {
294 >            f.obtrudeException(ex = new CFException());
295 >            checkCompletedExceptionally(f, ex);
296 >        }
297 >
298 >        f = new CompletableFuture<>();
299 >        for (int i = 0; i < 2; i++) {
300 >            f.obtrudeException(ex = new CFException());
301 >            checkCompletedExceptionally(f, ex);
302 >        }
303 >
304          f = new CompletableFuture<>();
305 +        f.completeExceptionally(ex = new CFException());
306 +        f.obtrudeValue(v1);
307 +        checkCompletedNormally(f, v1);
308 +        f.obtrudeException(ex = new CFException());
309 +        checkCompletedExceptionally(f, ex);
310          f.completeExceptionally(new CFException());
311 <        f.obtrudeValue(four);
312 <        checkCompletedNormally(f, four);
313 <        f.obtrudeException(new CFException());
314 <        checkCompletedWithWrappedCFException(f);
279 <    }
311 >        checkCompletedExceptionally(f, ex);
312 >        f.complete(v1);
313 >        checkCompletedExceptionally(f, ex);
314 >    }}
315  
316      /**
317       * getNumberOfDependents returns number of dependent tasks
318       */
319      public void testGetNumberOfDependents() {
320 +        for (ExecutionMode m : ExecutionMode.values())
321 +    {
322          CompletableFuture<Integer> f = new CompletableFuture<>();
323          assertEquals(0, f.getNumberOfDependents());
324 <        CompletableFuture g = f.thenRun(new Noop(ExecutionMode.DEFAULT));
324 >        final CompletableFuture<Void> g = m.thenRun(f, new Noop(m));
325          assertEquals(1, f.getNumberOfDependents());
326          assertEquals(0, g.getNumberOfDependents());
327 <        CompletableFuture h = f.thenRun(new Noop(ExecutionMode.DEFAULT));
327 >        final CompletableFuture<Void> h = m.thenRun(f, new Noop(m));
328          assertEquals(2, f.getNumberOfDependents());
329 +        assertEquals(0, h.getNumberOfDependents());
330          f.complete(1);
331          checkCompletedNormally(g, null);
332 +        checkCompletedNormally(h, null);
333          assertEquals(0, f.getNumberOfDependents());
334          assertEquals(0, g.getNumberOfDependents());
335 <    }
335 >        assertEquals(0, h.getNumberOfDependents());
336 >    }}
337  
338      /**
339       * toString indicates current completion state
# Line 310 | Line 350 | public class CompletableFutureTest exten
350          f = new CompletableFuture<String>();
351          f.completeExceptionally(new IndexOutOfBoundsException());
352          assertTrue(f.toString().contains("[Completed exceptionally]"));
353 +
354 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
355 +            f = new CompletableFuture<String>();
356 +            f.cancel(mayInterruptIfRunning);
357 +            assertTrue(f.toString().contains("[Completed exceptionally]"));
358 +        }
359      }
360  
361      /**
# Line 360 | Line 406 | public class CompletableFutureTest exten
406          return (x == null) ? null : x + 1;
407      }
408  
409 <    class IncAction extends CheckedIntegerAction
409 >    class NoopConsumer extends CheckedIntegerAction
410          implements Consumer<Integer>
411      {
412 <        IncAction(ExecutionMode m) { super(m); }
412 >        NoopConsumer(ExecutionMode m) { super(m); }
413          public void accept(Integer x) {
414              invoked();
415 <            value = inc(x);
415 >            value = x;
416          }
417      }
418  
# Line 415 | Line 461 | public class CompletableFutureTest exten
461          }
462      }
463  
464 <    class FailingSupplier extends CheckedAction implements Supplier<Integer>
464 >    class FailingSupplier extends CheckedAction
465 >        implements Supplier<Integer>
466      {
467          FailingSupplier(ExecutionMode m) { super(m); }
468          public Integer get() {
# Line 424 | Line 471 | public class CompletableFutureTest exten
471          }
472      }
473  
474 <    class FailingConsumer extends CheckedAction implements Consumer<Integer> {
474 >    class FailingConsumer extends CheckedIntegerAction
475 >        implements Consumer<Integer>
476 >    {
477          FailingConsumer(ExecutionMode m) { super(m); }
478          public void accept(Integer x) {
479              invoked();
480 +            value = x;
481              throw new CFException();
482          }
483      }
484  
485 <    class FailingBiConsumer extends CheckedAction
485 >    class FailingBiConsumer extends CheckedIntegerAction
486          implements BiConsumer<Integer, Integer>
487      {
488          FailingBiConsumer(ExecutionMode m) { super(m); }
489          public void accept(Integer x, Integer y) {
490              invoked();
491 +            value = subtract(x, y);
492              throw new CFException();
493          }
494      }
495  
496 <    class FailingFunction extends CheckedAction
496 >    class FailingFunction extends CheckedIntegerAction
497          implements Function<Integer, Integer>
498      {
499          FailingFunction(ExecutionMode m) { super(m); }
500          public Integer apply(Integer x) {
501              invoked();
502 +            value = x;
503              throw new CFException();
504          }
505      }
506  
507 <    class FailingBiFunction extends CheckedAction
507 >    class FailingBiFunction extends CheckedIntegerAction
508          implements BiFunction<Integer, Integer, Integer>
509      {
510          FailingBiFunction(ExecutionMode m) { super(m); }
511          public Integer apply(Integer x, Integer y) {
512              invoked();
513 +            value = subtract(x, y);
514              throw new CFException();
515          }
516      }
# Line 471 | Line 524 | public class CompletableFutureTest exten
524      }
525  
526  
527 <    class CompletableFutureInc extends CheckedAction
527 >    class CompletableFutureInc extends CheckedIntegerAction
528          implements Function<Integer, CompletableFuture<Integer>>
529      {
530          CompletableFutureInc(ExecutionMode m) { super(m); }
531          public CompletableFuture<Integer> apply(Integer x) {
532              invoked();
533 +            value = x;
534              CompletableFuture<Integer> f = new CompletableFuture<>();
535              f.complete(inc(x));
536              return f;
537          }
538      }
539  
540 <    class FailingCompletableFutureFunction extends CheckedAction
540 >    class FailingCompletableFutureFunction extends CheckedIntegerAction
541          implements Function<Integer, CompletableFuture<Integer>>
542      {
543          FailingCompletableFutureFunction(ExecutionMode m) { super(m); }
544          public CompletableFuture<Integer> apply(Integer x) {
545              invoked();
546 +            value = x;
547              throw new CFException();
548          }
549      }
# Line 799 | Line 854 | public class CompletableFutureTest exten
854          assertEquals(0, a.get());
855      }}
856  
802
857      /**
858       * exceptionally action completes with function value on source
859       * exception
# Line 843 | Line 897 | public class CompletableFutureTest exten
897              });
898          if (createIncomplete) f.completeExceptionally(ex1);
899  
900 <        checkCompletedWithWrappedCFException(g, ex2);
900 >        checkCompletedWithWrappedException(g, ex2);
901 >        assertEquals(1, a.get());
902 >    }}
903 >
904 >    /**
905 >     * whenComplete action executes on normal completion, propagating
906 >     * source result.
907 >     */
908 >    public void testWhenComplete_normalCompletion1() {
909 >        for (ExecutionMode m : ExecutionMode.values())
910 >        for (boolean createIncomplete : new boolean[] { true, false })
911 >        for (Integer v1 : new Integer[] { 1, null })
912 >    {
913 >        final AtomicInteger a = new AtomicInteger(0);
914 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
915 >        if (!createIncomplete) f.complete(v1);
916 >        final CompletableFuture<Integer> g = m.whenComplete
917 >            (f,
918 >             (Integer x, Throwable t) -> {
919 >                m.checkExecutionMode();
920 >                threadAssertSame(x, v1);
921 >                threadAssertNull(t);
922 >                a.getAndIncrement();
923 >            });
924 >        if (createIncomplete) f.complete(v1);
925 >
926 >        checkCompletedNormally(g, v1);
927 >        checkCompletedNormally(f, v1);
928 >        assertEquals(1, a.get());
929 >    }}
930 >
931 >    /**
932 >     * whenComplete action executes on exceptional completion, propagating
933 >     * source result.
934 >     */
935 >    public void testWhenComplete_exceptionalCompletion() {
936 >        for (ExecutionMode m : ExecutionMode.values())
937 >        for (boolean createIncomplete : new boolean[] { true, false })
938 >        for (Integer v1 : new Integer[] { 1, null })
939 >    {
940 >        final AtomicInteger a = new AtomicInteger(0);
941 >        final CFException ex = new CFException();
942 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
943 >        if (!createIncomplete) f.completeExceptionally(ex);
944 >        final CompletableFuture<Integer> g = m.whenComplete
945 >            (f,
946 >             (Integer x, Throwable t) -> {
947 >                m.checkExecutionMode();
948 >                threadAssertNull(x);
949 >                threadAssertSame(t, ex);
950 >                a.getAndIncrement();
951 >            });
952 >        if (createIncomplete) f.completeExceptionally(ex);
953 >
954 >        checkCompletedWithWrappedException(g, ex);
955 >        checkCompletedExceptionally(f, ex);
956 >        assertEquals(1, a.get());
957 >    }}
958 >
959 >    /**
960 >     * whenComplete action executes on cancelled source, propagating
961 >     * CancellationException.
962 >     */
963 >    public void testWhenComplete_sourceCancelled() {
964 >        for (ExecutionMode m : ExecutionMode.values())
965 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
966 >        for (boolean createIncomplete : new boolean[] { true, false })
967 >    {
968 >        final AtomicInteger a = new AtomicInteger(0);
969 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
970 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
971 >        final CompletableFuture<Integer> g = m.whenComplete
972 >            (f,
973 >             (Integer x, Throwable t) -> {
974 >                m.checkExecutionMode();
975 >                threadAssertNull(x);
976 >                threadAssertTrue(t instanceof CancellationException);
977 >                a.getAndIncrement();
978 >            });
979 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
980 >
981 >        checkCompletedWithWrappedCancellationException(g);
982 >        checkCancelled(f);
983 >        assertEquals(1, a.get());
984 >    }}
985 >
986 >    /**
987 >     * If a whenComplete action throws an exception when triggered by
988 >     * a normal completion, it completes exceptionally
989 >     */
990 >    public void testWhenComplete_actionFailed() {
991 >        for (boolean createIncomplete : new boolean[] { true, false })
992 >        for (ExecutionMode m : ExecutionMode.values())
993 >        for (Integer v1 : new Integer[] { 1, null })
994 >    {
995 >        final AtomicInteger a = new AtomicInteger(0);
996 >        final CFException ex = new CFException();
997 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
998 >        if (!createIncomplete) f.complete(v1);
999 >        final CompletableFuture<Integer> g = m.whenComplete
1000 >            (f,
1001 >             (Integer x, Throwable t) -> {
1002 >                m.checkExecutionMode();
1003 >                threadAssertSame(x, v1);
1004 >                threadAssertNull(t);
1005 >                a.getAndIncrement();
1006 >                throw ex;
1007 >            });
1008 >        if (createIncomplete) f.complete(v1);
1009 >
1010 >        checkCompletedWithWrappedException(g, ex);
1011 >        checkCompletedNormally(f, v1);
1012 >        assertEquals(1, a.get());
1013 >    }}
1014 >
1015 >    /**
1016 >     * If a whenComplete action throws an exception when triggered by
1017 >     * a source completion that also throws an exception, the source
1018 >     * exception takes precedence.
1019 >     */
1020 >    public void testWhenComplete_actionFailedSourceFailed() {
1021 >        for (boolean createIncomplete : new boolean[] { true, false })
1022 >        for (ExecutionMode m : ExecutionMode.values())
1023 >        for (Integer v1 : new Integer[] { 1, null })
1024 >    {
1025 >        final AtomicInteger a = new AtomicInteger(0);
1026 >        final CFException ex1 = new CFException();
1027 >        final CFException ex2 = new CFException();
1028 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1029 >
1030 >        if (!createIncomplete) f.completeExceptionally(ex1);
1031 >        final CompletableFuture<Integer> g = m.whenComplete
1032 >            (f,
1033 >             (Integer x, Throwable t) -> {
1034 >                m.checkExecutionMode();
1035 >                threadAssertSame(t, ex1);
1036 >                threadAssertNull(x);
1037 >                a.getAndIncrement();
1038 >                throw ex2;
1039 >            });
1040 >        if (createIncomplete) f.completeExceptionally(ex1);
1041 >
1042 >        checkCompletedWithWrappedException(g, ex1);
1043 >        checkCompletedExceptionally(f, ex1);
1044          assertEquals(1, a.get());
1045      }}
1046  
# Line 900 | Line 1097 | public class CompletableFutureTest exten
1097          if (createIncomplete) f.completeExceptionally(ex);
1098  
1099          checkCompletedNormally(g, v1);
1100 <        checkCompletedWithWrappedCFException(f, ex);
1100 >        checkCompletedExceptionally(f, ex);
1101          assertEquals(1, a.get());
1102      }}
1103  
# Line 956 | Line 1153 | public class CompletableFutureTest exten
1153              });
1154          if (createIncomplete) f.completeExceptionally(ex1);
1155  
1156 <        checkCompletedWithWrappedCFException(g, ex2);
1157 <        checkCompletedWithWrappedCFException(f, ex1);
1156 >        checkCompletedWithWrappedException(g, ex2);
1157 >        checkCompletedExceptionally(f, ex1);
1158          assertEquals(1, a.get());
1159      }}
1160  
# Line 981 | Line 1178 | public class CompletableFutureTest exten
1178              });
1179          if (createIncomplete) f.complete(v1);
1180  
1181 <        checkCompletedWithWrappedCFException(g, ex);
1181 >        checkCompletedWithWrappedException(g, ex);
1182          checkCompletedNormally(f, v1);
1183          assertEquals(1, a.get());
1184      }}
# Line 1095 | Line 1292 | public class CompletableFutureTest exten
1292              f.completeExceptionally(ex);
1293          }
1294  
1295 <        checkCompletedWithWrappedCFException(g, ex);
1296 <        checkCompletedWithWrappedCFException(f, ex);
1295 >        checkCompletedWithWrappedException(g, ex);
1296 >        checkCompletedExceptionally(f, ex);
1297          r.assertNotInvoked();
1298      }}
1299  
# Line 1162 | Line 1359 | public class CompletableFutureTest exten
1359  
1360          checkCompletedNormally(g, inc(v1));
1361          checkCompletedNormally(f, v1);
1362 <        r.assertInvoked();
1362 >        r.assertValue(inc(v1));
1363      }}
1364  
1365      /**
# Line 1183 | Line 1380 | public class CompletableFutureTest exten
1380              f.completeExceptionally(ex);
1381          }
1382  
1383 <        checkCompletedWithWrappedCFException(g, ex);
1384 <        checkCompletedWithWrappedCFException(f, ex);
1383 >        checkCompletedWithWrappedException(g, ex);
1384 >        checkCompletedExceptionally(f, ex);
1385          r.assertNotInvoked();
1386      }}
1387  
# Line 1240 | Line 1437 | public class CompletableFutureTest exten
1437          for (Integer v1 : new Integer[] { 1, null })
1438      {
1439          final CompletableFuture<Integer> f = new CompletableFuture<>();
1440 <        final IncAction r = new IncAction(m);
1440 >        final NoopConsumer r = new NoopConsumer(m);
1441          if (!createIncomplete) f.complete(v1);
1442          final CompletableFuture<Void> g = m.thenAccept(f, r);
1443          if (createIncomplete) {
# Line 1249 | Line 1446 | public class CompletableFutureTest exten
1446          }
1447  
1448          checkCompletedNormally(g, null);
1449 +        r.assertValue(v1);
1450          checkCompletedNormally(f, v1);
1253        r.assertInvoked();
1254        r.assertValue(inc(v1));
1451      }}
1452  
1453      /**
# Line 1264 | Line 1460 | public class CompletableFutureTest exten
1460      {
1461          final CFException ex = new CFException();
1462          final CompletableFuture<Integer> f = new CompletableFuture<>();
1463 <        final IncAction r = new IncAction(m);
1463 >        final NoopConsumer r = new NoopConsumer(m);
1464          if (!createIncomplete) f.completeExceptionally(ex);
1465          final CompletableFuture<Void> g = m.thenAccept(f, r);
1466          if (createIncomplete) {
# Line 1272 | Line 1468 | public class CompletableFutureTest exten
1468              f.completeExceptionally(ex);
1469          }
1470  
1471 <        checkCompletedWithWrappedCFException(g, ex);
1472 <        checkCompletedWithWrappedCFException(f, ex);
1471 >        checkCompletedWithWrappedException(g, ex);
1472 >        checkCompletedExceptionally(f, ex);
1473          r.assertNotInvoked();
1474      }}
1475  
# Line 1286 | Line 1482 | public class CompletableFutureTest exten
1482          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1483      {
1484          final CompletableFuture<Integer> f = new CompletableFuture<>();
1485 <        final IncAction r = new IncAction(m);
1485 >        final NoopConsumer r = new NoopConsumer(m);
1486          if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1487          final CompletableFuture<Void> g = m.thenAccept(f, r);
1488          if (createIncomplete) {
# Line 1348 | Line 1544 | public class CompletableFutureTest exten
1544          checkCompletedNormally(h, subtract(v1, v2));
1545          checkCompletedNormally(f, v1);
1546          checkCompletedNormally(g, v2);
1547 <        r.assertInvoked();
1547 >        r.assertValue(subtract(v1, v2));
1548      }}
1549  
1550      /**
# Line 1375 | Line 1571 | public class CompletableFutureTest exten
1571              (!fFirst ? f : g).completeExceptionally(ex);
1572          }
1573  
1574 <        checkCompletedWithWrappedCFException(h, ex);
1574 >        checkCompletedWithWrappedException(h, ex);
1575          r.assertNotInvoked();
1576          checkCompletedNormally(fFirst ? f : g, v1);
1577 <        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1577 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1578      }}
1579  
1580      /**
# Line 1492 | Line 1688 | public class CompletableFutureTest exten
1688              (!fFirst ? f : g).completeExceptionally(ex);
1689          }
1690  
1691 <        checkCompletedWithWrappedCFException(h, ex);
1691 >        checkCompletedWithWrappedException(h, ex);
1692          r.assertNotInvoked();
1693          checkCompletedNormally(fFirst ? f : g, v1);
1694 <        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1694 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1695      }}
1696  
1697      /**
# Line 1609 | Line 1805 | public class CompletableFutureTest exten
1805              (!fFirst ? f : g).completeExceptionally(ex);
1806          }
1807  
1808 <        checkCompletedWithWrappedCFException(h, ex);
1808 >        checkCompletedWithWrappedException(h, ex);
1809          r.assertNotInvoked();
1810          checkCompletedNormally(fFirst ? f : g, v1);
1811 <        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1811 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1812      }}
1813  
1814      /**
# Line 1629 | Line 1825 | public class CompletableFutureTest exten
1825          final CompletableFuture<Integer> g = new CompletableFuture<>();
1826          final Noop r = new Noop(m);
1827  
1632
1828          (fFirst ? f : g).complete(v1);
1829          if (!createIncomplete)
1830              assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
# Line 1744 | Line 1939 | public class CompletableFutureTest exten
1939          rs[0].assertNotInvoked();
1940          rs[1].assertNotInvoked();
1941          f.completeExceptionally(ex);
1942 <        checkCompletedWithWrappedCFException(h0, ex);
1943 <        checkCompletedWithWrappedCFException(h1, ex);
1942 >        checkCompletedWithWrappedException(h0, ex);
1943 >        checkCompletedWithWrappedException(h1, ex);
1944          final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1945          final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1946 <        checkCompletedWithWrappedCFException(h2, ex);
1947 <        checkCompletedWithWrappedCFException(h3, ex);
1946 >        checkCompletedWithWrappedException(h2, ex);
1947 >        checkCompletedWithWrappedException(h3, ex);
1948          g.complete(v1);
1949  
1950          // unspecified behavior - both source completions available
# Line 1757 | Line 1952 | public class CompletableFutureTest exten
1952          final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
1953          try {
1954              assertEquals(inc(v1), h4.join());
1955 <            rs[4].assertInvoked();
1955 >            rs[4].assertValue(inc(v1));
1956          } catch (CompletionException ok) {
1957 <            checkCompletedWithWrappedCFException(h4, ex);
1957 >            checkCompletedWithWrappedException(h4, ex);
1958              rs[4].assertNotInvoked();
1959          }
1960          try {
1961              assertEquals(inc(v1), h5.join());
1962 <            rs[5].assertInvoked();
1962 >            rs[5].assertValue(inc(v1));
1963          } catch (CompletionException ok) {
1964 <            checkCompletedWithWrappedCFException(h5, ex);
1964 >            checkCompletedWithWrappedException(h5, ex);
1965              rs[5].assertNotInvoked();
1966          }
1967  
1968 <        checkCompletedWithWrappedCFException(f, ex);
1968 >        checkCompletedExceptionally(f, ex);
1969          checkCompletedNormally(g, v1);
1970 <        checkCompletedWithWrappedCFException(h0, ex);
1971 <        checkCompletedWithWrappedCFException(h1, ex);
1972 <        checkCompletedWithWrappedCFException(h2, ex);
1973 <        checkCompletedWithWrappedCFException(h3, ex);
1974 <        checkCompletedWithWrappedCFException(h4, ex);
1970 >        checkCompletedWithWrappedException(h0, ex);
1971 >        checkCompletedWithWrappedException(h1, ex);
1972 >        checkCompletedWithWrappedException(h2, ex);
1973 >        checkCompletedWithWrappedException(h3, ex);
1974 >        checkCompletedWithWrappedException(h4, ex);
1975          for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
1976      }}
1977  
1978 +    public void testApplyToEither_exceptionalCompletion2() {
1979 +        for (ExecutionMode m : ExecutionMode.values())
1980 +        for (boolean fFirst : new boolean[] { true, false })
1981 +        for (Integer v1 : new Integer[] { 1, null })
1982 +    {
1983 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1984 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1985 +        final CFException ex = new CFException();
1986 +        final IncFunction[] rs = new IncFunction[6];
1987 +        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1988 +
1989 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1990 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1991 +        if (fFirst) {
1992 +            f.complete(v1);
1993 +            g.completeExceptionally(ex);
1994 +        } else {
1995 +            g.completeExceptionally(ex);
1996 +            f.complete(v1);
1997 +        }
1998 +        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1999 +        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2000 +
2001 +        // unspecified behavior - both source completions available
2002 +        try {
2003 +            assertEquals(inc(v1), h0.join());
2004 +            rs[0].assertValue(inc(v1));
2005 +        } catch (CompletionException ok) {
2006 +            checkCompletedWithWrappedException(h0, ex);
2007 +            rs[0].assertNotInvoked();
2008 +        }
2009 +        try {
2010 +            assertEquals(inc(v1), h1.join());
2011 +            rs[1].assertValue(inc(v1));
2012 +        } catch (CompletionException ok) {
2013 +            checkCompletedWithWrappedException(h1, ex);
2014 +            rs[1].assertNotInvoked();
2015 +        }
2016 +        try {
2017 +            assertEquals(inc(v1), h2.join());
2018 +            rs[2].assertValue(inc(v1));
2019 +        } catch (CompletionException ok) {
2020 +            checkCompletedWithWrappedException(h2, ex);
2021 +            rs[2].assertNotInvoked();
2022 +        }
2023 +        try {
2024 +            assertEquals(inc(v1), h3.join());
2025 +            rs[3].assertValue(inc(v1));
2026 +        } catch (CompletionException ok) {
2027 +            checkCompletedWithWrappedException(h3, ex);
2028 +            rs[3].assertNotInvoked();
2029 +        }
2030 +
2031 +        checkCompletedNormally(f, v1);
2032 +        checkCompletedExceptionally(g, ex);
2033 +    }}
2034 +
2035      /**
2036       * applyToEither result completes exceptionally if either source cancelled
2037       */
# Line 1813 | Line 2065 | public class CompletableFutureTest exten
2065          final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2066          try {
2067              assertEquals(inc(v1), h4.join());
2068 <            rs[4].assertInvoked();
2068 >            rs[4].assertValue(inc(v1));
2069          } catch (CompletionException ok) {
2070              checkCompletedWithWrappedCancellationException(h4);
2071              rs[4].assertNotInvoked();
2072          }
2073          try {
2074              assertEquals(inc(v1), h5.join());
2075 <            rs[5].assertInvoked();
2075 >            rs[5].assertValue(inc(v1));
2076          } catch (CompletionException ok) {
2077              checkCompletedWithWrappedCancellationException(h5);
2078              rs[5].assertNotInvoked();
# Line 1835 | Line 2087 | public class CompletableFutureTest exten
2087          for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2088      }}
2089  
2090 <    /**
1839 <     * applyToEither result completes exceptionally if action does
1840 <     */
1841 <    public void testApplyToEither_actionFailed() {
2090 >    public void testApplyToEither_sourceCancelled2() {
2091          for (ExecutionMode m : ExecutionMode.values())
2092 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2093 +        for (boolean fFirst : new boolean[] { true, false })
2094          for (Integer v1 : new Integer[] { 1, null })
1844        for (Integer v2 : new Integer[] { 2, null })
2095      {
2096          final CompletableFuture<Integer> f = new CompletableFuture<>();
2097          final CompletableFuture<Integer> g = new CompletableFuture<>();
2098 <        final FailingFunction[] rs = new FailingFunction[6];
2099 <        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
2098 >        final IncFunction[] rs = new IncFunction[6];
2099 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2100  
2101          final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2102          final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2103 <        f.complete(v1);
2103 >        if (fFirst) {
2104 >            f.complete(v1);
2105 >            g.cancel(mayInterruptIfRunning);
2106 >        } else {
2107 >            g.cancel(mayInterruptIfRunning);
2108 >            f.complete(v1);
2109 >        }
2110          final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2111          final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2112 <        g.complete(v2);
2113 <        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2114 <        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2115 <        
2112 >
2113 >        // unspecified behavior - both source completions available
2114 >        try {
2115 >            assertEquals(inc(v1), h0.join());
2116 >            rs[0].assertValue(inc(v1));
2117 >        } catch (CompletionException ok) {
2118 >            checkCompletedWithWrappedCancellationException(h0);
2119 >            rs[0].assertNotInvoked();
2120 >        }
2121 >        try {
2122 >            assertEquals(inc(v1), h1.join());
2123 >            rs[1].assertValue(inc(v1));
2124 >        } catch (CompletionException ok) {
2125 >            checkCompletedWithWrappedCancellationException(h1);
2126 >            rs[1].assertNotInvoked();
2127 >        }
2128 >        try {
2129 >            assertEquals(inc(v1), h2.join());
2130 >            rs[2].assertValue(inc(v1));
2131 >        } catch (CompletionException ok) {
2132 >            checkCompletedWithWrappedCancellationException(h2);
2133 >            rs[2].assertNotInvoked();
2134 >        }
2135 >        try {
2136 >            assertEquals(inc(v1), h3.join());
2137 >            rs[3].assertValue(inc(v1));
2138 >        } catch (CompletionException ok) {
2139 >            checkCompletedWithWrappedCancellationException(h3);
2140 >            rs[3].assertNotInvoked();
2141 >        }
2142 >
2143          checkCompletedNormally(f, v1);
2144 <        checkCompletedNormally(g, v2);
1862 <        checkCompletedWithWrappedCFException(h0);
1863 <        checkCompletedWithWrappedCFException(h1);
1864 <        checkCompletedWithWrappedCFException(h2);
1865 <        checkCompletedWithWrappedCFException(h3);
1866 <        checkCompletedWithWrappedCFException(h4);
1867 <        checkCompletedWithWrappedCFException(h5);
1868 <        for (int i = 0; i < rs.length; i++) rs[i].assertInvoked();
2144 >        checkCancelled(g);
2145      }}
2146  
2147      /**
2148 <     * acceptEither result completes normally after normal completion
1873 <     * of either source
2148 >     * applyToEither result completes exceptionally if action does
2149       */
2150 <    public void testAcceptEither_normalCompletion1() {
2150 >    public void testApplyToEither_actionFailed() {
2151          for (ExecutionMode m : ExecutionMode.values())
2152          for (Integer v1 : new Integer[] { 1, null })
2153          for (Integer v2 : new Integer[] { 2, null })
2154      {
2155          final CompletableFuture<Integer> f = new CompletableFuture<>();
2156          final CompletableFuture<Integer> g = new CompletableFuture<>();
2157 <        final IncAction r = new IncAction(m);
2158 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2157 >        final FailingFunction[] rs = new FailingFunction[6];
2158 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
2159  
2160 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2161 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2162          f.complete(v1);
2163 <        checkCompletedNormally(h, null);
2164 <        assertEquals(inc(v1), r.value);
2165 <        g.complete(v2);
2163 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2164 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2165 >        checkCompletedWithWrappedCFException(h0);
2166 >        checkCompletedWithWrappedCFException(h1);
2167 >        checkCompletedWithWrappedCFException(h2);
2168 >        checkCompletedWithWrappedCFException(h3);
2169 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2170  
2171 <        checkCompletedNormally(f, v1);
1891 <        checkCompletedNormally(g, v2);
1892 <        checkCompletedNormally(h, null);
1893 <    }}
2171 >        g.complete(v2);
2172  
2173 <    public void testAcceptEither_normalCompletion2() {
2174 <        for (ExecutionMode m : ExecutionMode.values())
2175 <        for (Integer v1 : new Integer[] { 1, null })
1898 <        for (Integer v2 : new Integer[] { 2, null })
1899 <    {
1900 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1901 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1902 <        final IncAction r = new IncAction(m);
1903 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2173 >        // unspecified behavior - both source completions available
2174 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2175 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2176  
2177 <        g.complete(v2);
2178 <        checkCompletedNormally(h, null);
2179 <        assertEquals(inc(v2), r.value);
2180 <        f.complete(v1);
2177 >        checkCompletedWithWrappedCFException(h4);
2178 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2179 >                   Objects.equals(v2, rs[4].value));
2180 >        checkCompletedWithWrappedCFException(h5);
2181 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2182 >                   Objects.equals(v2, rs[5].value));
2183  
2184          checkCompletedNormally(f, v1);
2185          checkCompletedNormally(g, v2);
1912        checkCompletedNormally(h, null);
2186      }}
2187  
2188 <    public void testAcceptEither_normalCompletion3() {
2188 >    /**
2189 >     * acceptEither result completes normally after normal completion
2190 >     * of either source
2191 >     */
2192 >    public void testAcceptEither_normalCompletion() {
2193          for (ExecutionMode m : ExecutionMode.values())
2194          for (Integer v1 : new Integer[] { 1, null })
2195          for (Integer v2 : new Integer[] { 2, null })
2196      {
2197          final CompletableFuture<Integer> f = new CompletableFuture<>();
2198          final CompletableFuture<Integer> g = new CompletableFuture<>();
2199 <        final IncAction r = new IncAction(m);
2199 >        final NoopConsumer[] rs = new NoopConsumer[6];
2200 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2201  
2202 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2203 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2204 +        checkIncomplete(h0);
2205 +        checkIncomplete(h1);
2206 +        rs[0].assertNotInvoked();
2207 +        rs[1].assertNotInvoked();
2208          f.complete(v1);
2209 +        checkCompletedNormally(h0, null);
2210 +        checkCompletedNormally(h1, null);
2211 +        rs[0].assertValue(v1);
2212 +        rs[1].assertValue(v1);
2213 +        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2214 +        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2215 +        checkCompletedNormally(h2, null);
2216 +        checkCompletedNormally(h3, null);
2217 +        rs[2].assertValue(v1);
2218 +        rs[3].assertValue(v1);
2219          g.complete(v2);
1926        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2220  
2221 <        checkCompletedNormally(h, null);
2221 >        // unspecified behavior - both source completions available
2222 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2223 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2224 >        checkCompletedNormally(h4, null);
2225 >        checkCompletedNormally(h5, null);
2226 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2227 >                   Objects.equals(v2, rs[4].value));
2228 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2229 >                   Objects.equals(v2, rs[5].value));
2230 >
2231          checkCompletedNormally(f, v1);
2232          checkCompletedNormally(g, v2);
2233 <
2234 <        // unspecified behavior
2235 <        assertTrue(Objects.equals(r.value, inc(v1)) ||
2236 <                   Objects.equals(r.value, inc(v2)));
2233 >        checkCompletedNormally(h0, null);
2234 >        checkCompletedNormally(h1, null);
2235 >        checkCompletedNormally(h2, null);
2236 >        checkCompletedNormally(h3, null);
2237 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2238      }}
2239  
2240      /**
2241       * acceptEither result completes exceptionally after exceptional
2242       * completion of either source
2243       */
2244 <    public void testAcceptEither_exceptionalCompletion1() {
2244 >    public void testAcceptEither_exceptionalCompletion() {
2245          for (ExecutionMode m : ExecutionMode.values())
2246          for (Integer v1 : new Integer[] { 1, null })
2247      {
2248          final CompletableFuture<Integer> f = new CompletableFuture<>();
2249          final CompletableFuture<Integer> g = new CompletableFuture<>();
1947        final IncAction r = new IncAction(m);
1948        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2250          final CFException ex = new CFException();
2251 +        final NoopConsumer[] rs = new NoopConsumer[6];
2252 +        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2253  
2254 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2255 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2256 +        checkIncomplete(h0);
2257 +        checkIncomplete(h1);
2258 +        rs[0].assertNotInvoked();
2259 +        rs[1].assertNotInvoked();
2260          f.completeExceptionally(ex);
2261 <        checkCompletedWithWrappedCFException(h, ex);
2261 >        checkCompletedWithWrappedException(h0, ex);
2262 >        checkCompletedWithWrappedException(h1, ex);
2263 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2264 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2265 >        checkCompletedWithWrappedException(h2, ex);
2266 >        checkCompletedWithWrappedException(h3, ex);
2267 >
2268          g.complete(v1);
2269  
2270 <        r.assertNotInvoked();
2270 >        // unspecified behavior - both source completions available
2271 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2272 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2273 >        try {
2274 >            assertNull(h4.join());
2275 >            rs[4].assertValue(v1);
2276 >        } catch (CompletionException ok) {
2277 >            checkCompletedWithWrappedException(h4, ex);
2278 >            rs[4].assertNotInvoked();
2279 >        }
2280 >        try {
2281 >            assertNull(h5.join());
2282 >            rs[5].assertValue(v1);
2283 >        } catch (CompletionException ok) {
2284 >            checkCompletedWithWrappedException(h5, ex);
2285 >            rs[5].assertNotInvoked();
2286 >        }
2287 >
2288 >        checkCompletedExceptionally(f, ex);
2289          checkCompletedNormally(g, v1);
2290 <        checkCompletedWithWrappedCFException(f, ex);
2291 <        checkCompletedWithWrappedCFException(h, ex);
2290 >        checkCompletedWithWrappedException(h0, ex);
2291 >        checkCompletedWithWrappedException(h1, ex);
2292 >        checkCompletedWithWrappedException(h2, ex);
2293 >        checkCompletedWithWrappedException(h3, ex);
2294 >        checkCompletedWithWrappedException(h4, ex);
2295 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2296      }}
2297  
2298      public void testAcceptEither_exceptionalCompletion2() {
2299          for (ExecutionMode m : ExecutionMode.values())
2300 +        for (boolean fFirst : new boolean[] { true, false })
2301          for (Integer v1 : new Integer[] { 1, null })
2302      {
2303          final CompletableFuture<Integer> f = new CompletableFuture<>();
2304          final CompletableFuture<Integer> g = new CompletableFuture<>();
1967        final IncAction r = new IncAction(m);
1968        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
1969        final CFException ex = new CFException();
1970
1971        g.completeExceptionally(ex);
1972        checkCompletedWithWrappedCFException(h, ex);
1973        f.complete(v1);
1974
1975        r.assertNotInvoked();
1976        checkCompletedNormally(f, v1);
1977        checkCompletedWithWrappedCFException(g, ex);
1978        checkCompletedWithWrappedCFException(h, ex);
1979    }}
1980
1981    public void testAcceptEither_exceptionalCompletion3() {
1982        for (ExecutionMode m : ExecutionMode.values())
1983        for (Integer v1 : new Integer[] { 1, null })
1984    {
1985        final CompletableFuture<Integer> f = new CompletableFuture<>();
1986        final CompletableFuture<Integer> g = new CompletableFuture<>();
1987        final IncAction r = new IncAction(m);
2305          final CFException ex = new CFException();
2306 +        final NoopConsumer[] rs = new NoopConsumer[6];
2307 +        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2308  
2309 <        g.completeExceptionally(ex);
2310 <        f.complete(v1);
2311 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2309 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2310 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2311 >        if (fFirst) {
2312 >            f.complete(v1);
2313 >            g.completeExceptionally(ex);
2314 >        } else {
2315 >            g.completeExceptionally(ex);
2316 >            f.complete(v1);
2317 >        }
2318 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2319 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2320  
2321 <        // unspecified behavior
1995 <        Integer v;
2321 >        // unspecified behavior - both source completions available
2322          try {
2323 <            assertNull(h.join());
2324 <            r.assertInvoked();
1999 <            assertEquals(inc(v1), r.value);
2323 >            assertEquals(null, h0.join());
2324 >            rs[0].assertValue(v1);
2325          } catch (CompletionException ok) {
2326 <            checkCompletedWithWrappedCFException(h, ex);
2327 <            r.assertNotInvoked();
2326 >            checkCompletedWithWrappedException(h0, ex);
2327 >            rs[0].assertNotInvoked();
2328          }
2004
2005        checkCompletedWithWrappedCFException(g, ex);
2006        checkCompletedNormally(f, v1);
2007    }}
2008
2009    public void testAcceptEither_exceptionalCompletion4() {
2010        for (ExecutionMode m : ExecutionMode.values())
2011        for (Integer v1 : new Integer[] { 1, null })
2012    {
2013        final CompletableFuture<Integer> f = new CompletableFuture<>();
2014        final CompletableFuture<Integer> g = new CompletableFuture<>();
2015        final IncAction r = new IncAction(m);
2016        final CFException ex = new CFException();
2017
2018        f.completeExceptionally(ex);
2019        g.complete(v1);
2020        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2021
2022        // unspecified behavior
2023        Integer v;
2329          try {
2330 <            assertNull(h.join());
2331 <            r.assertInvoked();
2027 <            assertEquals(inc(v1), r.value);
2330 >            assertEquals(null, h1.join());
2331 >            rs[1].assertValue(v1);
2332          } catch (CompletionException ok) {
2333 <            checkCompletedWithWrappedCFException(h, ex);
2334 <            r.assertNotInvoked();
2333 >            checkCompletedWithWrappedException(h1, ex);
2334 >            rs[1].assertNotInvoked();
2335 >        }
2336 >        try {
2337 >            assertEquals(null, h2.join());
2338 >            rs[2].assertValue(v1);
2339 >        } catch (CompletionException ok) {
2340 >            checkCompletedWithWrappedException(h2, ex);
2341 >            rs[2].assertNotInvoked();
2342 >        }
2343 >        try {
2344 >            assertEquals(null, h3.join());
2345 >            rs[3].assertValue(v1);
2346 >        } catch (CompletionException ok) {
2347 >            checkCompletedWithWrappedException(h3, ex);
2348 >            rs[3].assertNotInvoked();
2349          }
2350  
2033        checkCompletedWithWrappedCFException(f, ex);
2034        checkCompletedNormally(g, v1);
2035    }}
2036
2037    /**
2038     * acceptEither result completes exceptionally if action does
2039     */
2040    public void testAcceptEither_actionFailed1() {
2041        for (ExecutionMode m : ExecutionMode.values())
2042        for (Integer v1 : new Integer[] { 1, null })
2043        for (Integer v2 : new Integer[] { 2, null })
2044    {
2045        final CompletableFuture<Integer> f = new CompletableFuture<>();
2046        final CompletableFuture<Integer> g = new CompletableFuture<>();
2047        final FailingConsumer r = new FailingConsumer(m);
2048        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2049
2050        f.complete(v1);
2051        checkCompletedWithWrappedCFException(h);
2052        g.complete(v2);
2053        checkCompletedNormally(f, v1);
2054        checkCompletedNormally(g, v2);
2055    }}
2056
2057    public void testAcceptEither_actionFailed2() {
2058        for (ExecutionMode m : ExecutionMode.values())
2059        for (Integer v1 : new Integer[] { 1, null })
2060        for (Integer v2 : new Integer[] { 2, null })
2061    {
2062        final CompletableFuture<Integer> f = new CompletableFuture<>();
2063        final CompletableFuture<Integer> g = new CompletableFuture<>();
2064        final FailingConsumer r = new FailingConsumer(m);
2065        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2066
2067        g.complete(v2);
2068        checkCompletedWithWrappedCFException(h);
2069        f.complete(v1);
2351          checkCompletedNormally(f, v1);
2352 <        checkCompletedNormally(g, v2);
2352 >        checkCompletedExceptionally(g, ex);
2353      }}
2354  
2355      /**
2356       * acceptEither result completes exceptionally if either source cancelled
2357       */
2358 <    public void testAcceptEither_sourceCancelled1() {
2358 >    public void testAcceptEither_sourceCancelled() {
2359          for (ExecutionMode m : ExecutionMode.values())
2360          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2361          for (Integer v1 : new Integer[] { 1, null })
2362      {
2363          final CompletableFuture<Integer> f = new CompletableFuture<>();
2364          final CompletableFuture<Integer> g = new CompletableFuture<>();
2365 <        final IncAction r = new IncAction(m);
2366 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2365 >        final NoopConsumer[] rs = new NoopConsumer[6];
2366 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2367  
2368 <        assertTrue(f.cancel(mayInterruptIfRunning));
2369 <        checkCompletedWithWrappedCancellationException(h);
2370 <        g.complete(v1);
2371 <
2372 <        checkCancelled(f);
2373 <        r.assertNotInvoked();
2374 <        checkCompletedNormally(g, v1);
2375 <        checkCompletedWithWrappedCancellationException(h);
2376 <    }}
2377 <
2378 <    public void testAcceptEither_sourceCancelled2() {
2379 <        for (ExecutionMode m : ExecutionMode.values())
2380 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2100 <        for (Integer v1 : new Integer[] { 1, null })
2101 <    {
2102 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2103 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2104 <        final IncAction r = new IncAction(m);
2105 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2106 <
2107 <        assertTrue(g.cancel(mayInterruptIfRunning));
2108 <        checkCompletedWithWrappedCancellationException(h);
2109 <        f.complete(v1);
2110 <
2111 <        checkCancelled(g);
2112 <        r.assertNotInvoked();
2113 <        checkCompletedNormally(f, v1);
2114 <        checkCompletedWithWrappedCancellationException(h);
2115 <    }}
2116 <
2117 <    public void testAcceptEither_sourceCancelled3() {
2118 <        for (ExecutionMode m : ExecutionMode.values())
2119 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2120 <        for (Integer v1 : new Integer[] { 1, null })
2121 <    {
2122 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2123 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2124 <        final IncAction r = new IncAction(m);
2368 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2369 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2370 >        checkIncomplete(h0);
2371 >        checkIncomplete(h1);
2372 >        rs[0].assertNotInvoked();
2373 >        rs[1].assertNotInvoked();
2374 >        f.cancel(mayInterruptIfRunning);
2375 >        checkCompletedWithWrappedCancellationException(h0);
2376 >        checkCompletedWithWrappedCancellationException(h1);
2377 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2378 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2379 >        checkCompletedWithWrappedCancellationException(h2);
2380 >        checkCompletedWithWrappedCancellationException(h3);
2381  
2382 <        assertTrue(g.cancel(mayInterruptIfRunning));
2127 <        f.complete(v1);
2128 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2382 >        g.complete(v1);
2383  
2384 <        // unspecified behavior
2385 <        Integer v;
2384 >        // unspecified behavior - both source completions available
2385 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2386 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2387          try {
2388 <            assertNull(h.join());
2389 <            r.assertInvoked();
2135 <            assertEquals(inc(v1), r.value);
2388 >            assertNull(h4.join());
2389 >            rs[4].assertValue(v1);
2390          } catch (CompletionException ok) {
2391 <            checkCompletedWithWrappedCancellationException(h);
2392 <            r.assertNotInvoked();
2391 >            checkCompletedWithWrappedCancellationException(h4);
2392 >            rs[4].assertNotInvoked();
2393          }
2140
2141        checkCancelled(g);
2142        checkCompletedNormally(f, v1);
2143    }}
2144
2145    public void testAcceptEither_sourceCancelled4() {
2146        for (ExecutionMode m : ExecutionMode.values())
2147        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2148        for (Integer v1 : new Integer[] { 1, null })
2149    {
2150        final CompletableFuture<Integer> f = new CompletableFuture<>();
2151        final CompletableFuture<Integer> g = new CompletableFuture<>();
2152        final IncAction r = new IncAction(m);
2153
2154        assertTrue(f.cancel(mayInterruptIfRunning));
2155        g.complete(v1);
2156        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2157
2158        // unspecified behavior
2159        Integer v;
2394          try {
2395 <            assertNull(h.join());
2396 <            r.assertInvoked();
2163 <            assertEquals(inc(v1), r.value);
2395 >            assertNull(h5.join());
2396 >            rs[5].assertValue(v1);
2397          } catch (CompletionException ok) {
2398 <            checkCompletedWithWrappedCancellationException(h);
2399 <            r.assertNotInvoked();
2398 >            checkCompletedWithWrappedCancellationException(h5);
2399 >            rs[5].assertNotInvoked();
2400          }
2401  
2402          checkCancelled(f);
2403          checkCompletedNormally(g, v1);
2404 +        checkCompletedWithWrappedCancellationException(h0);
2405 +        checkCompletedWithWrappedCancellationException(h1);
2406 +        checkCompletedWithWrappedCancellationException(h2);
2407 +        checkCompletedWithWrappedCancellationException(h3);
2408 +        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2409      }}
2410  
2411      /**
2412 <     * runAfterEither result completes normally after normal completion
2175 <     * of either source
2412 >     * acceptEither result completes exceptionally if action does
2413       */
2414 <    public void testRunAfterEither_normalCompletion1() {
2414 >    public void testAcceptEither_actionFailed() {
2415          for (ExecutionMode m : ExecutionMode.values())
2416          for (Integer v1 : new Integer[] { 1, null })
2417          for (Integer v2 : new Integer[] { 2, null })
2418      {
2419          final CompletableFuture<Integer> f = new CompletableFuture<>();
2420          final CompletableFuture<Integer> g = new CompletableFuture<>();
2421 <        final Noop r = new Noop(m);
2422 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2421 >        final FailingConsumer[] rs = new FailingConsumer[6];
2422 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
2423  
2424 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2425 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2426          f.complete(v1);
2427 <        checkCompletedNormally(h, null);
2428 <        r.assertInvoked();
2429 <        g.complete(v2);
2427 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2428 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2429 >        checkCompletedWithWrappedCFException(h0);
2430 >        checkCompletedWithWrappedCFException(h1);
2431 >        checkCompletedWithWrappedCFException(h2);
2432 >        checkCompletedWithWrappedCFException(h3);
2433 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2434  
2435 <        checkCompletedNormally(f, v1);
2193 <        checkCompletedNormally(g, v2);
2194 <        checkCompletedNormally(h, null);
2195 <        r.assertInvoked();
2196 <    }}
2435 >        g.complete(v2);
2436  
2437 <    public void testRunAfterEither_normalCompletion2() {
2438 <        for (ExecutionMode m : ExecutionMode.values())
2439 <        for (Integer v1 : new Integer[] { 1, null })
2201 <        for (Integer v2 : new Integer[] { 2, null })
2202 <    {
2203 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2204 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2205 <        final Noop r = new Noop(m);
2206 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2437 >        // unspecified behavior - both source completions available
2438 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2439 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2440  
2441 <        g.complete(v2);
2442 <        checkCompletedNormally(h, null);
2443 <        r.assertInvoked();
2444 <        f.complete(v1);
2441 >        checkCompletedWithWrappedCFException(h4);
2442 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2443 >                   Objects.equals(v2, rs[4].value));
2444 >        checkCompletedWithWrappedCFException(h5);
2445 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2446 >                   Objects.equals(v2, rs[5].value));
2447  
2448          checkCompletedNormally(f, v1);
2449          checkCompletedNormally(g, v2);
2450 <        checkCompletedNormally(h, null);
2216 <        r.assertInvoked();
2217 <        }}
2450 >    }}
2451  
2452 <    public void testRunAfterEither_normalCompletion3() {
2452 >    /**
2453 >     * runAfterEither result completes normally after normal completion
2454 >     * of either source
2455 >     */
2456 >    public void testRunAfterEither_normalCompletion() {
2457          for (ExecutionMode m : ExecutionMode.values())
2458          for (Integer v1 : new Integer[] { 1, null })
2459          for (Integer v2 : new Integer[] { 2, null })
2460      {
2461          final CompletableFuture<Integer> f = new CompletableFuture<>();
2462          final CompletableFuture<Integer> g = new CompletableFuture<>();
2463 <        final Noop r = new Noop(m);
2463 >        final Noop[] rs = new Noop[6];
2464 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2465  
2466 +        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2467 +        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2468 +        checkIncomplete(h0);
2469 +        checkIncomplete(h1);
2470 +        rs[0].assertNotInvoked();
2471 +        rs[1].assertNotInvoked();
2472          f.complete(v1);
2473 +        checkCompletedNormally(h0, null);
2474 +        checkCompletedNormally(h1, null);
2475 +        rs[0].assertInvoked();
2476 +        rs[1].assertInvoked();
2477 +        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2478 +        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2479 +        checkCompletedNormally(h2, null);
2480 +        checkCompletedNormally(h3, null);
2481 +        rs[2].assertInvoked();
2482 +        rs[3].assertInvoked();
2483 +
2484          g.complete(v2);
2230        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2485  
2486 <        checkCompletedNormally(h, null);
2486 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2487 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2488 >
2489          checkCompletedNormally(f, v1);
2490          checkCompletedNormally(g, v2);
2491 <        r.assertInvoked();
2491 >        checkCompletedNormally(h0, null);
2492 >        checkCompletedNormally(h1, null);
2493 >        checkCompletedNormally(h2, null);
2494 >        checkCompletedNormally(h3, null);
2495 >        checkCompletedNormally(h4, null);
2496 >        checkCompletedNormally(h5, null);
2497 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2498      }}
2499  
2500      /**
2501       * runAfterEither result completes exceptionally after exceptional
2502       * completion of either source
2503       */
2504 <    public void testRunAfterEither_exceptionalCompletion1() {
2504 >    public void testRunAfterEither_exceptionalCompletion() {
2505          for (ExecutionMode m : ExecutionMode.values())
2506          for (Integer v1 : new Integer[] { 1, null })
2507      {
2508          final CompletableFuture<Integer> f = new CompletableFuture<>();
2509          final CompletableFuture<Integer> g = new CompletableFuture<>();
2248        final Noop r = new Noop(m);
2249        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2510          final CFException ex = new CFException();
2511 +        final Noop[] rs = new Noop[6];
2512 +        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2513  
2514 +        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2515 +        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2516 +        checkIncomplete(h0);
2517 +        checkIncomplete(h1);
2518 +        rs[0].assertNotInvoked();
2519 +        rs[1].assertNotInvoked();
2520          f.completeExceptionally(ex);
2521 <        checkCompletedWithWrappedCFException(h, ex);
2521 >        checkCompletedWithWrappedException(h0, ex);
2522 >        checkCompletedWithWrappedException(h1, ex);
2523 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2524 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2525 >        checkCompletedWithWrappedException(h2, ex);
2526 >        checkCompletedWithWrappedException(h3, ex);
2527 >
2528          g.complete(v1);
2529  
2530 <        r.assertNotInvoked();
2530 >        // unspecified behavior - both source completions available
2531 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2532 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2533 >        try {
2534 >            assertNull(h4.join());
2535 >            rs[4].assertInvoked();
2536 >        } catch (CompletionException ok) {
2537 >            checkCompletedWithWrappedException(h4, ex);
2538 >            rs[4].assertNotInvoked();
2539 >        }
2540 >        try {
2541 >            assertNull(h5.join());
2542 >            rs[5].assertInvoked();
2543 >        } catch (CompletionException ok) {
2544 >            checkCompletedWithWrappedException(h5, ex);
2545 >            rs[5].assertNotInvoked();
2546 >        }
2547 >
2548 >        checkCompletedExceptionally(f, ex);
2549          checkCompletedNormally(g, v1);
2550 <        checkCompletedWithWrappedCFException(f, ex);
2551 <        checkCompletedWithWrappedCFException(h, ex);
2550 >        checkCompletedWithWrappedException(h0, ex);
2551 >        checkCompletedWithWrappedException(h1, ex);
2552 >        checkCompletedWithWrappedException(h2, ex);
2553 >        checkCompletedWithWrappedException(h3, ex);
2554 >        checkCompletedWithWrappedException(h4, ex);
2555 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2556      }}
2557  
2558      public void testRunAfterEither_exceptionalCompletion2() {
2559          for (ExecutionMode m : ExecutionMode.values())
2560 +        for (boolean fFirst : new boolean[] { true, false })
2561          for (Integer v1 : new Integer[] { 1, null })
2562      {
2563          final CompletableFuture<Integer> f = new CompletableFuture<>();
2564          final CompletableFuture<Integer> g = new CompletableFuture<>();
2268        final Noop r = new Noop(m);
2269        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2270        final CFException ex = new CFException();
2271
2272        g.completeExceptionally(ex);
2273        checkCompletedWithWrappedCFException(h, ex);
2274        f.complete(v1);
2275
2276        r.assertNotInvoked();
2277        checkCompletedNormally(f, v1);
2278        checkCompletedWithWrappedCFException(g, ex);
2279        checkCompletedWithWrappedCFException(h, ex);
2280    }}
2281
2282    public void testRunAfterEither_exceptionalCompletion3() {
2283        for (ExecutionMode m : ExecutionMode.values())
2284        for (Integer v1 : new Integer[] { 1, null })
2285    {
2286        final CompletableFuture<Integer> f = new CompletableFuture<>();
2287        final CompletableFuture<Integer> g = new CompletableFuture<>();
2288        final Noop r = new Noop(m);
2565          final CFException ex = new CFException();
2566 +        final Noop[] rs = new Noop[6];
2567 +        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2568  
2569 <        g.completeExceptionally(ex);
2570 <        f.complete(v1);
2571 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2569 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2570 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2571 >        if (fFirst) {
2572 >            f.complete(v1);
2573 >            g.completeExceptionally(ex);
2574 >        } else {
2575 >            g.completeExceptionally(ex);
2576 >            f.complete(v1);
2577 >        }
2578 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2579 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2580  
2581 <        // unspecified behavior
2296 <        Integer v;
2581 >        // unspecified behavior - both source completions available
2582          try {
2583 <            assertNull(h.join());
2584 <            r.assertInvoked();
2583 >            assertEquals(null, h0.join());
2584 >            rs[0].assertInvoked();
2585          } catch (CompletionException ok) {
2586 <            checkCompletedWithWrappedCFException(h, ex);
2587 <            r.assertNotInvoked();
2586 >            checkCompletedWithWrappedException(h0, ex);
2587 >            rs[0].assertNotInvoked();
2588 >        }
2589 >        try {
2590 >            assertEquals(null, h1.join());
2591 >            rs[1].assertInvoked();
2592 >        } catch (CompletionException ok) {
2593 >            checkCompletedWithWrappedException(h1, ex);
2594 >            rs[1].assertNotInvoked();
2595 >        }
2596 >        try {
2597 >            assertEquals(null, h2.join());
2598 >            rs[2].assertInvoked();
2599 >        } catch (CompletionException ok) {
2600 >            checkCompletedWithWrappedException(h2, ex);
2601 >            rs[2].assertNotInvoked();
2602 >        }
2603 >        try {
2604 >            assertEquals(null, h3.join());
2605 >            rs[3].assertInvoked();
2606 >        } catch (CompletionException ok) {
2607 >            checkCompletedWithWrappedException(h3, ex);
2608 >            rs[3].assertNotInvoked();
2609          }
2610  
2305        checkCompletedWithWrappedCFException(g, ex);
2611          checkCompletedNormally(f, v1);
2612 +        checkCompletedExceptionally(g, ex);
2613      }}
2614  
2615 <    public void testRunAfterEither_exceptionalCompletion4() {
2615 >    /**
2616 >     * runAfterEither result completes exceptionally if either source cancelled
2617 >     */
2618 >    public void testRunAfterEither_sourceCancelled() {
2619          for (ExecutionMode m : ExecutionMode.values())
2620 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2621          for (Integer v1 : new Integer[] { 1, null })
2622      {
2623          final CompletableFuture<Integer> f = new CompletableFuture<>();
2624          final CompletableFuture<Integer> g = new CompletableFuture<>();
2625 <        final Noop r = new Noop(m);
2626 <        final CFException ex = new CFException();
2625 >        final Noop[] rs = new Noop[6];
2626 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2627 >
2628 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2629 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2630 >        checkIncomplete(h0);
2631 >        checkIncomplete(h1);
2632 >        rs[0].assertNotInvoked();
2633 >        rs[1].assertNotInvoked();
2634 >        f.cancel(mayInterruptIfRunning);
2635 >        checkCompletedWithWrappedCancellationException(h0);
2636 >        checkCompletedWithWrappedCancellationException(h1);
2637 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2638 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2639 >        checkCompletedWithWrappedCancellationException(h2);
2640 >        checkCompletedWithWrappedCancellationException(h3);
2641  
2318        f.completeExceptionally(ex);
2642          g.complete(v1);
2320        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2643  
2644 <        // unspecified behavior
2645 <        Integer v;
2644 >        // unspecified behavior - both source completions available
2645 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2646 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2647          try {
2648 <            assertNull(h.join());
2649 <            r.assertInvoked();
2648 >            assertNull(h4.join());
2649 >            rs[4].assertInvoked();
2650          } catch (CompletionException ok) {
2651 <            checkCompletedWithWrappedCFException(h, ex);
2652 <            r.assertNotInvoked();
2651 >            checkCompletedWithWrappedCancellationException(h4);
2652 >            rs[4].assertNotInvoked();
2653 >        }
2654 >        try {
2655 >            assertNull(h5.join());
2656 >            rs[5].assertInvoked();
2657 >        } catch (CompletionException ok) {
2658 >            checkCompletedWithWrappedCancellationException(h5);
2659 >            rs[5].assertNotInvoked();
2660          }
2661  
2662 <        checkCompletedWithWrappedCFException(f, ex);
2662 >        checkCancelled(f);
2663          checkCompletedNormally(g, v1);
2664 +        checkCompletedWithWrappedCancellationException(h0);
2665 +        checkCompletedWithWrappedCancellationException(h1);
2666 +        checkCompletedWithWrappedCancellationException(h2);
2667 +        checkCompletedWithWrappedCancellationException(h3);
2668 +        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2669      }}
2670  
2671      /**
2672       * runAfterEither result completes exceptionally if action does
2673       */
2674 <    public void testRunAfterEither_actionFailed1() {
2674 >    public void testRunAfterEither_actionFailed() {
2675          for (ExecutionMode m : ExecutionMode.values())
2676          for (Integer v1 : new Integer[] { 1, null })
2677          for (Integer v2 : new Integer[] { 2, null })
2678      {
2679          final CompletableFuture<Integer> f = new CompletableFuture<>();
2680          final CompletableFuture<Integer> g = new CompletableFuture<>();
2681 <        final FailingRunnable r = new FailingRunnable(m);
2682 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2681 >        final FailingRunnable[] rs = new FailingRunnable[6];
2682 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
2683  
2684 +        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2685 +        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2686          f.complete(v1);
2687 <        checkCompletedWithWrappedCFException(h);
2687 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2688 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2689 >        checkCompletedWithWrappedCFException(h0);
2690 >        checkCompletedWithWrappedCFException(h1);
2691 >        checkCompletedWithWrappedCFException(h2);
2692 >        checkCompletedWithWrappedCFException(h3);
2693 >        for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2694          g.complete(v2);
2695 <        checkCompletedNormally(f, v1);
2696 <        checkCompletedNormally(g, v2);
2697 <    }}
2698 <
2356 <    public void testRunAfterEither_actionFailed2() {
2357 <        for (ExecutionMode m : ExecutionMode.values())
2358 <        for (Integer v1 : new Integer[] { 1, null })
2359 <        for (Integer v2 : new Integer[] { 2, null })
2360 <    {
2361 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2362 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2363 <        final FailingRunnable r = new FailingRunnable(m);
2364 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2695 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2696 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2697 >        checkCompletedWithWrappedCFException(h4);
2698 >        checkCompletedWithWrappedCFException(h5);
2699  
2366        g.complete(v2);
2367        checkCompletedWithWrappedCFException(h);
2368        f.complete(v1);
2700          checkCompletedNormally(f, v1);
2701          checkCompletedNormally(g, v2);
2702 <    }}
2372 <
2373 <    /**
2374 <     * runAfterEither result completes exceptionally if either source cancelled
2375 <     */
2376 <    public void testRunAfterEither_sourceCancelled1() {
2377 <        for (ExecutionMode m : ExecutionMode.values())
2378 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2379 <        for (Integer v1 : new Integer[] { 1, null })
2380 <    {
2381 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2382 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2383 <        final Noop r = new Noop(m);
2384 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2385 <
2386 <        assertTrue(f.cancel(mayInterruptIfRunning));
2387 <        checkCompletedWithWrappedCancellationException(h);
2388 <        g.complete(v1);
2389 <
2390 <        checkCancelled(f);
2391 <        r.assertNotInvoked();
2392 <        checkCompletedNormally(g, v1);
2393 <        checkCompletedWithWrappedCancellationException(h);
2394 <    }}
2395 <
2396 <    public void testRunAfterEither_sourceCancelled2() {
2397 <        for (ExecutionMode m : ExecutionMode.values())
2398 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2399 <        for (Integer v1 : new Integer[] { 1, null })
2400 <    {
2401 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2402 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2403 <        final Noop r = new Noop(m);
2404 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2405 <
2406 <        assertTrue(g.cancel(mayInterruptIfRunning));
2407 <        checkCompletedWithWrappedCancellationException(h);
2408 <        f.complete(v1);
2409 <
2410 <        checkCancelled(g);
2411 <        r.assertNotInvoked();
2412 <        checkCompletedNormally(f, v1);
2413 <        checkCompletedWithWrappedCancellationException(h);
2414 <    }}
2415 <
2416 <    public void testRunAfterEither_sourceCancelled3() {
2417 <        for (ExecutionMode m : ExecutionMode.values())
2418 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2419 <        for (Integer v1 : new Integer[] { 1, null })
2420 <    {
2421 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2422 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2423 <        final Noop r = new Noop(m);
2424 <
2425 <        assertTrue(g.cancel(mayInterruptIfRunning));
2426 <        f.complete(v1);
2427 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2428 <
2429 <        // unspecified behavior
2430 <        Integer v;
2431 <        try {
2432 <            assertNull(h.join());
2433 <            r.assertInvoked();
2434 <        } catch (CompletionException ok) {
2435 <            checkCompletedWithWrappedCancellationException(h);
2436 <            r.assertNotInvoked();
2437 <        }
2438 <
2439 <        checkCancelled(g);
2440 <        checkCompletedNormally(f, v1);
2441 <    }}
2442 <
2443 <    public void testRunAfterEither_sourceCancelled4() {
2444 <        for (ExecutionMode m : ExecutionMode.values())
2445 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2446 <        for (Integer v1 : new Integer[] { 1, null })
2447 <    {
2448 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2449 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2450 <        final Noop r = new Noop(m);
2451 <
2452 <        assertTrue(f.cancel(mayInterruptIfRunning));
2453 <        g.complete(v1);
2454 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2455 <
2456 <        // unspecified behavior
2457 <        Integer v;
2458 <        try {
2459 <            assertNull(h.join());
2460 <            r.assertInvoked();
2461 <        } catch (CompletionException ok) {
2462 <            checkCompletedWithWrappedCancellationException(h);
2463 <            r.assertNotInvoked();
2464 <        }
2465 <
2466 <        checkCancelled(f);
2467 <        checkCompletedNormally(g, v1);
2702 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2703      }}
2704  
2705      /**
# Line 2483 | Line 2718 | public class CompletableFutureTest exten
2718  
2719          checkCompletedNormally(g, inc(v1));
2720          checkCompletedNormally(f, v1);
2721 <        r.assertInvoked();
2721 >        r.assertValue(v1);
2722      }}
2723  
2724      /**
# Line 2501 | Line 2736 | public class CompletableFutureTest exten
2736          final CompletableFuture<Integer> g = m.thenCompose(f, r);
2737          if (createIncomplete) f.completeExceptionally(ex);
2738  
2739 <        checkCompletedWithWrappedCFException(g, ex);
2740 <        checkCompletedWithWrappedCFException(f, ex);
2739 >        checkCompletedWithWrappedException(g, ex);
2740 >        checkCompletedExceptionally(f, ex);
2741          r.assertNotInvoked();
2742      }}
2743  
# Line 2758 | Line 2993 | public class CompletableFutureTest exten
2993          assertSame(f, f.toCompletableFuture());
2994      }
2995  
2761    /**
2762     * whenComplete action executes on normal completion, propagating
2763     * source result.
2764     */
2765    public void testWhenComplete_normalCompletion1() {
2766        for (ExecutionMode m : ExecutionMode.values())
2767        for (boolean createIncomplete : new boolean[] { true, false })
2768        for (Integer v1 : new Integer[] { 1, null })
2769    {
2770        final AtomicInteger a = new AtomicInteger(0);
2771        final CompletableFuture<Integer> f = new CompletableFuture<>();
2772        if (!createIncomplete) f.complete(v1);
2773        final CompletableFuture<Integer> g = m.whenComplete
2774            (f,
2775             (Integer x, Throwable t) -> {
2776                threadAssertSame(x, v1);
2777                threadAssertNull(t);
2778                a.getAndIncrement();
2779            });
2780        if (createIncomplete) f.complete(v1);
2781
2782        checkCompletedNormally(g, v1);
2783        checkCompletedNormally(f, v1);
2784        assertEquals(1, a.get());
2785    }}
2786
2787    /**
2788     * whenComplete action executes on exceptional completion, propagating
2789     * source result.
2790     */
2791    public void testWhenComplete_exceptionalCompletion() {
2792        for (ExecutionMode m : ExecutionMode.values())
2793        for (boolean createIncomplete : new boolean[] { true, false })
2794        for (Integer v1 : new Integer[] { 1, null })
2795    {
2796        final AtomicInteger a = new AtomicInteger(0);
2797        final CFException ex = new CFException();
2798        final CompletableFuture<Integer> f = new CompletableFuture<>();
2799        if (!createIncomplete) f.completeExceptionally(ex);
2800        final CompletableFuture<Integer> g = m.whenComplete
2801            (f,
2802             (Integer x, Throwable t) -> {
2803                threadAssertNull(x);
2804                threadAssertSame(t, ex);
2805                a.getAndIncrement();
2806            });
2807        if (createIncomplete) f.completeExceptionally(ex);
2808        checkCompletedWithWrappedCFException(f, ex);
2809        checkCompletedWithWrappedCFException(g, ex);
2810        assertEquals(1, a.get());
2811    }}
2812
2813    /**
2814     * whenComplete action executes on cancelled source, propagating
2815     * CancellationException.
2816     */
2817    public void testWhenComplete_sourceCancelled() {
2818        for (ExecutionMode m : ExecutionMode.values())
2819        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2820        for (boolean createIncomplete : new boolean[] { true, false })
2821    {
2822        final AtomicInteger a = new AtomicInteger(0);
2823        final CompletableFuture<Integer> f = new CompletableFuture<>();
2824        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2825        final CompletableFuture<Integer> g = m.whenComplete
2826            (f,
2827             (Integer x, Throwable t) -> {
2828                threadAssertNull(x);
2829                threadAssertTrue(t instanceof CancellationException);
2830                a.getAndIncrement();
2831            });
2832        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2833
2834        //try { g.join(); } catch (Throwable t) { throw new Error(t); }
2835        checkCompletedWithWrappedCancellationException(g);
2836        checkCancelled(f);
2837        assertEquals(1, a.get());
2838    }}
2839
2840    /**
2841     * If a whenComplete action throws an exception when triggered by
2842     * a normal completion, it completes exceptionally
2843     */
2844    public void testWhenComplete_actionFailed() {
2845        for (boolean createIncomplete : new boolean[] { true, false })
2846        for (ExecutionMode m : ExecutionMode.values())
2847        for (Integer v1 : new Integer[] { 1, null })
2848    {
2849        final AtomicInteger a = new AtomicInteger(0);
2850        final CFException ex = new CFException();
2851        final CompletableFuture<Integer> f = new CompletableFuture<>();
2852        if (!createIncomplete) f.complete(v1);
2853        final CompletableFuture<Integer> g = m.whenComplete
2854            (f,
2855             (Integer x, Throwable t) -> {
2856                threadAssertSame(x, v1);
2857                threadAssertNull(t);
2858                a.getAndIncrement();
2859                throw ex;
2860            });
2861        if (createIncomplete) f.complete(v1);
2862        checkCompletedNormally(f, v1);
2863        checkCompletedWithWrappedCFException(g, ex);
2864        assertEquals(1, a.get());
2865    }}
2866
2867    /**
2868     * If a whenComplete action throws an exception when triggered by
2869     * a source completion that also throws an exception, the source
2870     * exception takes precedence.
2871     */
2872    public void testWhenComplete_actionFailedSourceFailed() {
2873        for (boolean createIncomplete : new boolean[] { true, false })
2874        for (ExecutionMode m : ExecutionMode.values())
2875        for (Integer v1 : new Integer[] { 1, null })
2876    {
2877        final AtomicInteger a = new AtomicInteger(0);
2878        final CFException ex1 = new CFException();
2879        final CFException ex2 = new CFException();
2880        final CompletableFuture<Integer> f = new CompletableFuture<>();
2881
2882        if (!createIncomplete) f.completeExceptionally(ex1);
2883        final CompletableFuture<Integer> g = m.whenComplete
2884            (f,
2885             (Integer x, Throwable t) -> {
2886                threadAssertSame(t, ex1);
2887                threadAssertNull(x);
2888                a.getAndIncrement();
2889                throw ex2;
2890            });
2891        if (createIncomplete) f.completeExceptionally(ex1);
2892
2893        checkCompletedWithWrappedCFException(f, ex1);
2894        checkCompletedWithWrappedCFException(g, ex1);
2895        assertEquals(1, a.get());
2896    }}
2897
2996   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines