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.36 by jsr166, Sun Jun 1 23:12:45 2014 UTC vs.
Revision 1.43 by jsr166, Mon Jun 2 04:13:54 2014 UTC

# Line 318 | Line 318 | public class CompletableFutureTest exten
318  
319      // Choose non-commutative actions for better coverage
320  
321 <    // A non-commutative function that handles null values as well
322 <    public static int subtract(Integer x, Integer y) {
323 <        return ((x == null) ? 42 : x.intValue())
321 >    // A non-commutative function that handles and produces null values as well.
322 >    static Integer subtract(Integer x, Integer y) {
323 >        return (x == null && y == null) ? null :
324 >            ((x == null) ? 42 : x.intValue())
325              - ((y == null) ? 99 : y.intValue());
326      }
327  
328 +    // A function that handles and produces null values as well.
329 +    static Integer inc(Integer x) {
330 +        return (x == null) ? null : x + 1;
331 +    }
332 +
333      static final Supplier<Integer> supplyOne =
334          () -> Integer.valueOf(1);
335      static final Function<Integer, Integer> inc =
# Line 331 | Line 337 | public class CompletableFutureTest exten
337      static final BiFunction<Integer, Integer, Integer> subtract =
338          (Integer x, Integer y) -> subtract(x, y);
339      static final class IncAction implements Consumer<Integer> {
340 <        int value;
341 <        public void accept(Integer x) { value = x.intValue() + 1; }
340 >        int invocationCount = 0;
341 >        Integer value;
342 >        public boolean ran() { return invocationCount == 1; }
343 >        public void accept(Integer x) {
344 >            invocationCount++;
345 >            value = inc(x);
346 >        }
347 >    }
348 >    static final class IncFunction implements Function<Integer,Integer> {
349 >        int invocationCount = 0;
350 >        Integer value;
351 >        public boolean ran() { return invocationCount == 1; }
352 >        public Integer apply(Integer x) {
353 >            invocationCount++;
354 >            return value = inc(x);
355 >        }
356      }
357      static final class SubtractAction implements BiConsumer<Integer, Integer> {
358 <        volatile int invocationCount = 0;
359 <        int value;
358 >        int invocationCount = 0;
359 >        Integer value;
360          // Check this action was invoked exactly once when result is computed.
361          public boolean ran() { return invocationCount == 1; }
362          public void accept(Integer x, Integer y) {
# Line 345 | Line 365 | public class CompletableFutureTest exten
365          }
366      }
367      static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> {
368 <        volatile int invocationCount = 0;
369 <        int value;
368 >        int invocationCount = 0;
369 >        Integer value;
370          // Check this action was invoked exactly once when result is computed.
371          public boolean ran() { return invocationCount == 1; }
372          public Integer apply(Integer x, Integer y) {
373              invocationCount++;
374 <            return subtract(x, y);
374 >            return value = subtract(x, y);
375          }
376      }
377      static final class Noop implements Runnable {
378 +        int invocationCount = 0;
379          boolean ran;
380 <        public void run() { ran = true; }
380 >        public void run() {
381 >            invocationCount++;
382 >            ran = true;
383 >        }
384      }
385  
386      static final class FailingSupplier implements Supplier<Integer> {
# Line 447 | Line 471 | public class CompletableFutureTest exten
471                   BiFunction<? super T,? super U,? extends V> a) {
472                  return f.thenCombine(g, a);
473              }
474 +            public <T,U> CompletableFuture<U> applyToEither
475 +                (CompletableFuture<T> f,
476 +                 CompletionStage<? extends T> g,
477 +                 Function<? super T,U> a) {
478 +                return f.applyToEither(g, a);
479 +            }
480 +            public <T> CompletableFuture<Void> acceptEither
481 +                (CompletableFuture<T> f,
482 +                 CompletionStage<? extends T> g,
483 +                 Consumer<? super T> a) {
484 +                return f.acceptEither(g, a);
485 +            }
486 +            public <T> CompletableFuture<Void> runAfterEither
487 +                (CompletableFuture<T> f,
488 +                 CompletionStage<?> g,
489 +                 java.lang.Runnable a) {
490 +                return f.runAfterEither(g, a);
491 +            }
492 +            public <T,U> CompletableFuture<U> thenCompose
493 +                (CompletableFuture<T> f,
494 +                 Function<? super T,? extends CompletionStage<U>> a) {
495 +                return f.thenCompose(a);
496 +            }
497 +            public <T> CompletableFuture<T> whenComplete
498 +                (CompletableFuture<T> f,
499 +                 BiConsumer<? super T,? super Throwable> a) {
500 +                return f.whenComplete(a);
501 +            }
502          },
503  
504   //             /** Experimental way to do more testing */
# Line 480 | Line 532 | public class CompletableFutureTest exten
532                   BiFunction<? super T,? super U,? extends V> a) {
533                  return f.thenCombineAsync(g, a);
534              }
535 +            public <T,U> CompletableFuture<U> applyToEither
536 +                (CompletableFuture<T> f,
537 +                 CompletionStage<? extends T> g,
538 +                 Function<? super T,U> a) {
539 +                return f.applyToEitherAsync(g, a);
540 +            }
541 +            public <T> CompletableFuture<Void> acceptEither
542 +                (CompletableFuture<T> f,
543 +                 CompletionStage<? extends T> g,
544 +                 Consumer<? super T> a) {
545 +                return f.acceptEitherAsync(g, a);
546 +            }
547 +            public <T> CompletableFuture<Void> runAfterEither
548 +                (CompletableFuture<T> f,
549 +                 CompletionStage<?> g,
550 +                 java.lang.Runnable a) {
551 +                return f.runAfterEitherAsync(g, a);
552 +            }
553 +            public <T,U> CompletableFuture<U> thenCompose
554 +                (CompletableFuture<T> f,
555 +                 Function<? super T,? extends CompletionStage<U>> a) {
556 +                return f.thenComposeAsync(a);
557 +            }
558 +            public <T> CompletableFuture<T> whenComplete
559 +                (CompletableFuture<T> f,
560 +                 BiConsumer<? super T,? super Throwable> a) {
561 +                return f.whenCompleteAsync(a);
562 +            }
563          },
564  
565   //         REVERSE_DEFAULT_ASYNC {
# Line 512 | Line 592 | public class CompletableFutureTest exten
592                   BiFunction<? super T,? super U,? extends V> a) {
593                  return f.thenCombineAsync(g, a, new ThreadExecutor());
594              }
595 +            public <T,U> CompletableFuture<U> applyToEither
596 +                (CompletableFuture<T> f,
597 +                 CompletionStage<? extends T> g,
598 +                 Function<? super T,U> a) {
599 +                return f.applyToEitherAsync(g, a, new ThreadExecutor());
600 +            }
601 +            public <T> CompletableFuture<Void> acceptEither
602 +                (CompletableFuture<T> f,
603 +                 CompletionStage<? extends T> g,
604 +                 Consumer<? super T> a) {
605 +                return f.acceptEitherAsync(g, a, new ThreadExecutor());
606 +            }
607 +            public <T> CompletableFuture<Void> runAfterEither
608 +                (CompletableFuture<T> f,
609 +                 CompletionStage<?> g,
610 +                 java.lang.Runnable a) {
611 +                return f.runAfterEitherAsync(g, a, new ThreadExecutor());
612 +            }
613 +            public <T,U> CompletableFuture<U> thenCompose
614 +                (CompletableFuture<T> f,
615 +                 Function<? super T,? extends CompletionStage<U>> a) {
616 +                return f.thenComposeAsync(a, new ThreadExecutor());
617 +            }
618 +            public <T> CompletableFuture<T> whenComplete
619 +                (CompletableFuture<T> f,
620 +                 BiConsumer<? super T,? super Throwable> a) {
621 +                return f.whenCompleteAsync(a, new ThreadExecutor());
622 +            }
623          };
624  
625          public abstract <T,U> CompletableFuture<Void> runAfterBoth
# Line 524 | Line 632 | public class CompletableFutureTest exten
632              (CompletableFuture<T> f,
633               CompletionStage<? extends U> g,
634               BiFunction<? super T,? super U,? extends V> a);
635 +        public abstract <T,U> CompletableFuture<U> applyToEither
636 +            (CompletableFuture<T> f,
637 +             CompletionStage<? extends T> g,
638 +             Function<? super T,U> a);
639 +        public abstract <T> CompletableFuture<Void> acceptEither
640 +            (CompletableFuture<T> f,
641 +             CompletionStage<? extends T> g,
642 +             Consumer<? super T> a);
643 +        public abstract <T> CompletableFuture<Void> runAfterEither
644 +            (CompletableFuture<T> f,
645 +             CompletionStage<?> g,
646 +             java.lang.Runnable a);
647 +        public abstract <T,U> CompletableFuture<U> thenCompose
648 +            (CompletableFuture<T> f,
649 +             Function<? super T,? extends CompletionStage<U>> a);
650 +        public abstract <T> CompletableFuture<T> whenComplete
651 +            (CompletableFuture<T> f,
652 +             BiConsumer<? super T,? super Throwable> a);
653 +
654 +
655      }
656  
657      /**
# Line 776 | Line 904 | public class CompletableFutureTest exten
904          CompletableFuture<Void> g = f.thenAccept(r);
905          f.complete(one);
906          checkCompletedNormally(g, null);
907 <        assertEquals(r.value, 2);
907 >        assertEquals(r.value, (Integer) 2);
908      }
909  
910      /**
# Line 819 | Line 947 | public class CompletableFutureTest exten
947       * of sources
948       */
949      public void testThenCombine_normalCompletion1() {
950 +        for (boolean createdIncomplete : new boolean[] { true, false })
951 +        for (boolean fFirst : new boolean[] { true, false })
952          for (ExecutionMode m : ExecutionMode.values())
953          for (Integer v1 : new Integer[] { 1, null })
954          for (Integer v2 : new Integer[] { 2, null }) {
# Line 826 | Line 956 | public class CompletableFutureTest exten
956          final CompletableFuture<Integer> f = new CompletableFuture<>();
957          final CompletableFuture<Integer> g = new CompletableFuture<>();
958          final SubtractFunction r = new SubtractFunction();
959 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
960 <
831 <        f.complete(v1);
832 <        checkIncomplete(h);
833 <        assertFalse(r.ran());
834 <        g.complete(v2);
835 <
836 <        checkCompletedNormally(h, subtract(v1, v2));
837 <        checkCompletedNormally(f, v1);
838 <        checkCompletedNormally(g, v2);
839 <        }
840 <    }
841 <
842 <    public void testThenCombine_normalCompletion2() {
843 <        for (ExecutionMode m : ExecutionMode.values())
844 <        for (Integer v1 : new Integer[] { 1, null })
845 <        for (Integer v2 : new Integer[] { 2, null }) {
846 <
847 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
848 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
849 <        final SubtractFunction r = new SubtractFunction();
850 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
959 >        CompletableFuture<Integer> h = null;
960 >        if (createdIncomplete) h = m.thenCombine(f, g, r);
961  
962 <        g.complete(v2);
963 <        checkIncomplete(h);
964 <        assertFalse(r.ran());
965 <        f.complete(v1);
966 <
967 <        checkCompletedNormally(h, subtract(v1, v2));
968 <        checkCompletedNormally(f, v1);
969 <        checkCompletedNormally(g, v2);
970 <        }
971 <    }
972 <
863 <    public void testThenCombine_normalCompletion3() {
864 <        for (ExecutionMode m : ExecutionMode.values())
865 <        for (Integer v1 : new Integer[] { 1, null })
866 <        for (Integer v2 : new Integer[] { 2, null }) {
867 <
868 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
869 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
870 <        final SubtractFunction r = new SubtractFunction();
871 <
872 <        g.complete(v2);
873 <        f.complete(v1);
874 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
875 <
876 <        checkCompletedNormally(h, subtract(v1, v2));
877 <        checkCompletedNormally(f, v1);
878 <        checkCompletedNormally(g, v2);
879 <        }
880 <    }
881 <
882 <    public void testThenCombine_normalCompletion4() {
883 <        for (ExecutionMode m : ExecutionMode.values())
884 <        for (Integer v1 : new Integer[] { 1, null })
885 <        for (Integer v2 : new Integer[] { 2, null }) {
886 <
887 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
888 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
889 <        final SubtractFunction r = new SubtractFunction();
890 <
891 <        f.complete(v1);
892 <        g.complete(v2);
893 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
962 >        if (fFirst)
963 >            f.complete(v1);
964 >        else
965 >            g.complete(v2);
966 >        if (createdIncomplete) checkIncomplete(h);
967 >        assertEquals(r.invocationCount, 0);
968 >        if (!fFirst)
969 >            f.complete(v1);
970 >        else
971 >            g.complete(v2);
972 >        if (!createdIncomplete) h = m.thenCombine(f, g, r);
973  
974          checkCompletedNormally(h, subtract(v1, v2));
975          checkCompletedNormally(f, v1);
976          checkCompletedNormally(g, v2);
977 +        assertEquals(r.invocationCount, 1);
978          }
979      }
980  
# Line 918 | Line 998 | public class CompletableFutureTest exten
998  
999          checkCompletedWithWrappedCFException(h, ex);
1000          checkCompletedWithWrappedCFException(f, ex);
1001 <        assertFalse(r.ran());
1001 >        assertEquals(r.invocationCount, 0);
1002          checkCompletedNormally(g, v1);
1003          }
1004      }
# Line 930 | Line 1010 | public class CompletableFutureTest exten
1010          final CompletableFuture<Integer> f = new CompletableFuture<>();
1011          final CompletableFuture<Integer> g = new CompletableFuture<>();
1012          final SubtractFunction r = new SubtractFunction();
1013 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, subtract);
1013 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1014          final CFException ex = new CFException();
1015  
1016          g.completeExceptionally(ex);
# Line 939 | Line 1019 | public class CompletableFutureTest exten
1019  
1020          checkCompletedWithWrappedCFException(h, ex);
1021          checkCompletedWithWrappedCFException(g, ex);
1022 <        assertFalse(r.ran());
1022 >        assertEquals(r.invocationCount, 0);
1023          checkCompletedNormally(f, v1);
1024          }
1025      }
# Line 959 | Line 1039 | public class CompletableFutureTest exten
1039  
1040          checkCompletedWithWrappedCFException(h, ex);
1041          checkCompletedWithWrappedCFException(g, ex);
1042 <        assertFalse(r.ran());
1042 >        assertEquals(r.invocationCount, 0);
1043          checkCompletedNormally(f, v1);
1044          }
1045      }
# Line 975 | Line 1055 | public class CompletableFutureTest exten
1055  
1056          f.completeExceptionally(ex);
1057          g.complete(v1);
1058 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, subtract);
1058 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1059  
1060          checkCompletedWithWrappedCFException(h, ex);
1061          checkCompletedWithWrappedCFException(f, ex);
1062 <        assertFalse(r.ran());
1062 >        assertEquals(r.invocationCount, 0);
1063          checkCompletedNormally(g, v1);
1064          }
1065      }
# Line 1046 | Line 1126 | public class CompletableFutureTest exten
1126  
1127          checkCompletedWithWrappedCancellationException(h);
1128          checkCancelled(f);
1129 <        assertFalse(r.ran());
1129 >        assertEquals(r.invocationCount, 0);
1130          checkCompletedNormally(g, v1);
1131          }
1132      }
# Line 1067 | Line 1147 | public class CompletableFutureTest exten
1147  
1148          checkCompletedWithWrappedCancellationException(h);
1149          checkCancelled(g);
1150 <        assertFalse(r.ran());
1150 >        assertEquals(r.invocationCount, 0);
1151          checkCompletedNormally(f, v1);
1152          }
1153      }
# Line 1087 | Line 1167 | public class CompletableFutureTest exten
1167  
1168          checkCompletedWithWrappedCancellationException(h);
1169          checkCancelled(g);
1170 <        assertFalse(r.ran());
1170 >        assertEquals(r.invocationCount, 0);
1171          checkCompletedNormally(f, v1);
1172          }
1173      }
# Line 1107 | Line 1187 | public class CompletableFutureTest exten
1187  
1188          checkCompletedWithWrappedCancellationException(h);
1189          checkCancelled(f);
1190 <        assertFalse(r.ran());
1190 >        assertEquals(r.invocationCount, 0);
1191          checkCompletedNormally(g, v1);
1192          }
1193      }
# Line 1128 | Line 1208 | public class CompletableFutureTest exten
1208  
1209          f.complete(v1);
1210          checkIncomplete(h);
1211 <        assertEquals(r.value, 0);
1211 >        assertFalse(r.ran());
1212          g.complete(v2);
1213  
1214          checkCompletedNormally(h, null);
# Line 1150 | Line 1230 | public class CompletableFutureTest exten
1230  
1231          g.complete(v2);
1232          checkIncomplete(h);
1233 <        assertEquals(r.value, 0);
1233 >        assertFalse(r.ran());
1234          f.complete(v1);
1235  
1236          checkCompletedNormally(h, null);
# Line 1220 | Line 1300 | public class CompletableFutureTest exten
1300  
1301          checkCompletedWithWrappedCFException(h, ex);
1302          checkCompletedWithWrappedCFException(f, ex);
1303 <        assertFalse(r.ran());
1303 >        assertEquals(r.invocationCount, 0);
1304          checkCompletedNormally(g, v1);
1305          }
1306      }
# Line 1241 | Line 1321 | public class CompletableFutureTest exten
1321  
1322          checkCompletedWithWrappedCFException(h, ex);
1323          checkCompletedWithWrappedCFException(g, ex);
1324 <        assertFalse(r.ran());
1324 >        assertEquals(r.invocationCount, 0);
1325          checkCompletedNormally(f, v1);
1326          }
1327      }
# Line 1261 | Line 1341 | public class CompletableFutureTest exten
1341  
1342          checkCompletedWithWrappedCFException(h, ex);
1343          checkCompletedWithWrappedCFException(g, ex);
1344 <        assertFalse(r.ran());
1344 >        assertEquals(r.invocationCount, 0);
1345          checkCompletedNormally(f, v1);
1346          }
1347      }
# Line 1348 | Line 1428 | public class CompletableFutureTest exten
1428  
1429          checkCompletedWithWrappedCancellationException(h);
1430          checkCancelled(f);
1431 <        assertFalse(r.ran());
1431 >        assertEquals(r.invocationCount, 0);
1432          checkCompletedNormally(g, v1);
1433          }
1434      }
# Line 1369 | Line 1449 | public class CompletableFutureTest exten
1449  
1450          checkCompletedWithWrappedCancellationException(h);
1451          checkCancelled(g);
1452 <        assertFalse(r.ran());
1452 >        assertEquals(r.invocationCount, 0);
1453          checkCompletedNormally(f, v1);
1454          }
1455      }
# Line 1389 | Line 1469 | public class CompletableFutureTest exten
1469  
1470          checkCompletedWithWrappedCancellationException(h);
1471          checkCancelled(g);
1472 <        assertFalse(r.ran());
1472 >        assertEquals(r.invocationCount, 0);
1473          checkCompletedNormally(f, v1);
1474          }
1475      }
# Line 1409 | Line 1489 | public class CompletableFutureTest exten
1489  
1490          checkCompletedWithWrappedCancellationException(h);
1491          checkCancelled(f);
1492 <        assertFalse(r.ran());
1492 >        assertEquals(r.invocationCount, 0);
1493          checkCompletedNormally(g, v1);
1494          }
1495      }
# Line 1434 | Line 1514 | public class CompletableFutureTest exten
1514          g.complete(v2);
1515  
1516          checkCompletedNormally(h, null);
1517 <        assertTrue(r.ran);
1517 >        assertEquals(r.invocationCount, 1);
1518          checkCompletedNormally(f, v1);
1519          checkCompletedNormally(g, v2);
1520          }
# Line 1456 | Line 1536 | public class CompletableFutureTest exten
1536          f.complete(v1);
1537  
1538          checkCompletedNormally(h, null);
1539 <        assertTrue(r.ran);
1539 >        assertEquals(r.invocationCount, 1);
1540          checkCompletedNormally(f, v1);
1541          checkCompletedNormally(g, v2);
1542          }
# Line 1496 | Line 1576 | public class CompletableFutureTest exten
1576          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1577  
1578          checkCompletedNormally(h, null);
1579 <        assertTrue(r.ran);
1579 >        assertEquals(r.invocationCount, 1);
1580          checkCompletedNormally(f, v1);
1581          checkCompletedNormally(g, v2);
1582          }
# Line 1522 | Line 1602 | public class CompletableFutureTest exten
1602  
1603          checkCompletedWithWrappedCFException(h, ex);
1604          checkCompletedWithWrappedCFException(f, ex);
1605 <        assertFalse(r.ran);
1605 >        assertEquals(r.invocationCount, 0);
1606          checkCompletedNormally(g, v1);
1607          }
1608      }
# Line 1543 | Line 1623 | public class CompletableFutureTest exten
1623  
1624          checkCompletedWithWrappedCFException(h, ex);
1625          checkCompletedWithWrappedCFException(g, ex);
1626 <        assertFalse(r.ran);
1626 >        assertEquals(r.invocationCount, 0);
1627          checkCompletedNormally(f, v1);
1628          }
1629      }
# Line 1563 | Line 1643 | public class CompletableFutureTest exten
1643  
1644          checkCompletedWithWrappedCFException(h, ex);
1645          checkCompletedWithWrappedCFException(g, ex);
1646 <        assertFalse(r.ran);
1646 >        assertEquals(r.invocationCount, 0);
1647          checkCompletedNormally(f, v1);
1648          }
1649      }
# Line 1583 | Line 1663 | public class CompletableFutureTest exten
1663  
1664          checkCompletedWithWrappedCFException(h, ex);
1665          checkCompletedWithWrappedCFException(f, ex);
1666 <        assertFalse(r.ran);
1666 >        assertEquals(r.invocationCount, 0);
1667          checkCompletedNormally(g, v1);
1668          }
1669      }
# Line 1650 | Line 1730 | public class CompletableFutureTest exten
1730  
1731          checkCompletedWithWrappedCancellationException(h);
1732          checkCancelled(f);
1733 <        assertFalse(r.ran);
1733 >        assertEquals(r.invocationCount, 0);
1734          checkCompletedNormally(g, v1);
1735          }
1736      }
# Line 1671 | Line 1751 | public class CompletableFutureTest exten
1751  
1752          checkCompletedWithWrappedCancellationException(h);
1753          checkCancelled(g);
1754 <        assertFalse(r.ran);
1754 >        assertEquals(r.invocationCount, 0);
1755          checkCompletedNormally(f, v1);
1756          }
1757      }
# Line 1691 | Line 1771 | public class CompletableFutureTest exten
1771  
1772          checkCompletedWithWrappedCancellationException(h);
1773          checkCancelled(g);
1774 <        assertFalse(r.ran);
1774 >        assertEquals(r.invocationCount, 0);
1775          checkCompletedNormally(f, v1);
1776          }
1777      }
# Line 1711 | Line 1791 | public class CompletableFutureTest exten
1791  
1792          checkCompletedWithWrappedCancellationException(h);
1793          checkCancelled(f);
1794 <        assertFalse(r.ran);
1794 >        assertEquals(r.invocationCount, 0);
1795          checkCompletedNormally(g, v1);
1796          }
1797      }
# Line 1720 | Line 1800 | public class CompletableFutureTest exten
1800       * applyToEither result completes normally after normal completion
1801       * of either source
1802       */
1803 <    public void testApplyToEither() {
1804 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1805 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1806 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1727 <        f.complete(one);
1728 <        checkCompletedNormally(g, two);
1729 <        f2.complete(one);
1730 <        checkCompletedNormally(g, two);
1803 >    public void testApplyToEither_normalCompletion1() {
1804 >        for (ExecutionMode m : ExecutionMode.values())
1805 >        for (Integer v1 : new Integer[] { 1, null })
1806 >        for (Integer v2 : new Integer[] { 2, null }) {
1807  
1808 <        f = new CompletableFuture<>();
1809 <        f.complete(one);
1810 <        f2 = new CompletableFuture<>();
1811 <        g = f.applyToEither(f2, inc);
1812 <        checkCompletedNormally(g, two);
1808 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1809 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1810 >        final IncFunction r = new IncFunction();
1811 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1812 >
1813 >        f.complete(v1);
1814 >        checkCompletedNormally(h, inc(v1));
1815 >        g.complete(v2);
1816 >
1817 >        checkCompletedNormally(f, v1);
1818 >        checkCompletedNormally(g, v2);
1819 >        checkCompletedNormally(h, inc(v1));
1820 >        }
1821 >    }
1822 >
1823 >    public void testApplyToEither_normalCompletion2() {
1824 >        for (ExecutionMode m : ExecutionMode.values())
1825 >        for (Integer v1 : new Integer[] { 1, null })
1826 >        for (Integer v2 : new Integer[] { 2, null }) {
1827 >
1828 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1829 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1830 >        final IncFunction r = new IncFunction();
1831 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1832 >
1833 >        g.complete(v2);
1834 >        checkCompletedNormally(h, inc(v2));
1835 >        f.complete(v1);
1836 >
1837 >        checkCompletedNormally(f, v1);
1838 >        checkCompletedNormally(g, v2);
1839 >        checkCompletedNormally(h, inc(v2));
1840 >        }
1841 >    }
1842 >    public void testApplyToEither_normalCompletion3() {
1843 >        for (ExecutionMode m : ExecutionMode.values())
1844 >        for (Integer v1 : new Integer[] { 1, null })
1845 >        for (Integer v2 : new Integer[] { 2, null }) {
1846 >
1847 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1848 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1849 >        final IncFunction r = new IncFunction();
1850 >
1851 >        f.complete(v1);
1852 >        g.complete(v2);
1853 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1854 >
1855 >        checkCompletedNormally(f, v1);
1856 >        checkCompletedNormally(g, v2);
1857 >
1858 >        // unspecified behavior
1859 >        assertTrue(Objects.equals(h.join(), inc(v1)) ||
1860 >                   Objects.equals(h.join(), inc(v2)));
1861 >        assertEquals(r.invocationCount, 1);
1862 >        }
1863      }
1864  
1865      /**
1866       * applyToEither result completes exceptionally after exceptional
1867       * completion of either source
1868       */
1869 <    public void testApplyToEither2() {
1870 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1871 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1746 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1747 <        f.completeExceptionally(new CFException());
1748 <        f2.complete(one);
1749 <        checkCompletedWithWrappedCFException(g);
1869 >    public void testApplyToEither_exceptionalCompletion1() {
1870 >        for (ExecutionMode m : ExecutionMode.values())
1871 >        for (Integer v1 : new Integer[] { 1, null }) {
1872  
1873 <        f = new CompletableFuture<>();
1874 <        f2 = new CompletableFuture<>();
1875 <        f2.completeExceptionally(new CFException());
1876 <        g = f.applyToEither(f2, inc);
1877 <        checkCompletedWithWrappedCFException(g);
1873 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1874 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1875 >        final IncFunction r = new IncFunction();
1876 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1877 >        final CFException ex = new CFException();
1878 >
1879 >        f.completeExceptionally(ex);
1880 >        checkCompletedWithWrappedCFException(h, ex);
1881 >        g.complete(v1);
1882 >
1883 >        assertEquals(r.invocationCount, 0);
1884 >        checkCompletedNormally(g, v1);
1885 >        checkCompletedWithWrappedCFException(f, ex);
1886 >        checkCompletedWithWrappedCFException(h, ex);
1887 >        }
1888 >    }
1889 >
1890 >    public void testApplyToEither_exceptionalCompletion2() {
1891 >        for (ExecutionMode m : ExecutionMode.values())
1892 >        for (Integer v1 : new Integer[] { 1, null }) {
1893 >
1894 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1895 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1896 >        final IncFunction r = new IncFunction();
1897 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1898 >        final CFException ex = new CFException();
1899 >
1900 >        g.completeExceptionally(ex);
1901 >        checkCompletedWithWrappedCFException(h, ex);
1902 >        f.complete(v1);
1903 >
1904 >        assertEquals(r.invocationCount, 0);
1905 >        checkCompletedNormally(f, v1);
1906 >        checkCompletedWithWrappedCFException(g, ex);
1907 >        checkCompletedWithWrappedCFException(h, ex);
1908 >        }
1909 >    }
1910 >
1911 >    public void testApplyToEither_exceptionalCompletion3() {
1912 >        for (ExecutionMode m : ExecutionMode.values())
1913 >        for (Integer v1 : new Integer[] { 1, null }) {
1914 >
1915 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1916 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1917 >        final IncFunction r = new IncFunction();
1918 >        final CFException ex = new CFException();
1919 >
1920 >        g.completeExceptionally(ex);
1921 >        f.complete(v1);
1922 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1923 >
1924 >        // unspecified behavior
1925 >        Integer v;
1926 >        try {
1927 >            assertEquals(h.join(), inc(v1));
1928 >            assertEquals(r.invocationCount, 1);
1929 >        } catch (CompletionException ok) {
1930 >            checkCompletedWithWrappedCFException(h, ex);
1931 >            assertEquals(r.invocationCount, 0);
1932 >        }
1933 >
1934 >        checkCompletedWithWrappedCFException(g, ex);
1935 >        checkCompletedNormally(f, v1);
1936 >        }
1937 >    }
1938 >
1939 >    public void testApplyToEither_exceptionalCompletion4() {
1940 >        for (ExecutionMode m : ExecutionMode.values())
1941 >        for (Integer v1 : new Integer[] { 1, null }) {
1942 >
1943 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1944 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1945 >        final IncFunction r = new IncFunction();
1946 >        final CFException ex = new CFException();
1947 >
1948 >        f.completeExceptionally(ex);
1949 >        g.complete(v1);
1950 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1951 >
1952 >        // unspecified behavior
1953 >        Integer v;
1954 >        try {
1955 >            assertEquals(h.join(), inc(v1));
1956 >            assertEquals(r.invocationCount, 1);
1957 >        } catch (CompletionException ok) {
1958 >            checkCompletedWithWrappedCFException(h, ex);
1959 >            assertEquals(r.invocationCount, 0);
1960 >        }
1961 >
1962 >        checkCompletedWithWrappedCFException(f, ex);
1963 >        checkCompletedNormally(g, v1);
1964 >        }
1965      }
1966  
1967      /**
1968       * applyToEither result completes exceptionally if action does
1969       */
1970 <    public void testApplyToEither3() {
1971 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1972 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1973 <        FailingFunction r = new FailingFunction();
1974 <        CompletableFuture<Integer> g = f.applyToEither(f2, r);
1975 <        f2.complete(two);
1976 <        checkCompletedWithWrappedCFException(g);
1970 >    public void testApplyToEither_actionFailed1() {
1971 >        for (ExecutionMode m : ExecutionMode.values())
1972 >        for (Integer v1 : new Integer[] { 1, null })
1973 >        for (Integer v2 : new Integer[] { 2, null }) {
1974 >
1975 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1976 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1977 >        final FailingFunction r = new FailingFunction();
1978 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1979 >
1980 >        f.complete(v1);
1981 >        checkCompletedWithWrappedCFException(h);
1982 >        g.complete(v2);
1983 >        checkCompletedNormally(f, v1);
1984 >        checkCompletedNormally(g, v2);
1985 >        }
1986 >    }
1987 >
1988 >    public void testApplyToEither_actionFailed2() {
1989 >        for (ExecutionMode m : ExecutionMode.values())
1990 >        for (Integer v1 : new Integer[] { 1, null })
1991 >        for (Integer v2 : new Integer[] { 2, null }) {
1992 >
1993 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1994 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1995 >        final FailingFunction r = new FailingFunction();
1996 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1997 >
1998 >        g.complete(v2);
1999 >        checkCompletedWithWrappedCFException(h);
2000 >        f.complete(v1);
2001 >        checkCompletedNormally(f, v1);
2002 >        checkCompletedNormally(g, v2);
2003 >        }
2004      }
2005  
2006      /**
2007       * applyToEither result completes exceptionally if either source cancelled
2008       */
2009 <    public void testApplyToEither4() {
2010 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2011 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2012 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
2013 <        assertTrue(f.cancel(true));
2014 <        checkCompletedWithWrappedCancellationException(g);
2015 <        f = new CompletableFuture<>();
2016 <        f2 = new CompletableFuture<>();
2017 <        assertTrue(f2.cancel(true));
2018 <        checkCompletedWithWrappedCancellationException(g);
2009 >    public void testApplyToEither_sourceCancelled1() {
2010 >        for (ExecutionMode m : ExecutionMode.values())
2011 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2012 >        for (Integer v1 : new Integer[] { 1, null }) {
2013 >
2014 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2015 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2016 >        final IncFunction r = new IncFunction();
2017 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2018 >
2019 >        assertTrue(f.cancel(mayInterruptIfRunning));
2020 >        checkCompletedWithWrappedCancellationException(h);
2021 >        g.complete(v1);
2022 >
2023 >        checkCancelled(f);
2024 >        assertEquals(r.invocationCount, 0);
2025 >        checkCompletedNormally(g, v1);
2026 >        checkCompletedWithWrappedCancellationException(h);
2027 >        }
2028 >    }
2029 >
2030 >    public void testApplyToEither_sourceCancelled2() {
2031 >        for (ExecutionMode m : ExecutionMode.values())
2032 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2033 >        for (Integer v1 : new Integer[] { 1, null }) {
2034 >
2035 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2036 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2037 >        final IncFunction r = new IncFunction();
2038 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2039 >
2040 >        assertTrue(g.cancel(mayInterruptIfRunning));
2041 >        checkCompletedWithWrappedCancellationException(h);
2042 >        f.complete(v1);
2043 >
2044 >        checkCancelled(g);
2045 >        assertEquals(r.invocationCount, 0);
2046 >        checkCompletedNormally(f, v1);
2047 >        checkCompletedWithWrappedCancellationException(h);
2048 >        }
2049 >    }
2050 >
2051 >    public void testApplyToEither_sourceCancelled3() {
2052 >        for (ExecutionMode m : ExecutionMode.values())
2053 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2054 >        for (Integer v1 : new Integer[] { 1, null }) {
2055 >
2056 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2057 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2058 >        final IncFunction r = new IncFunction();
2059 >
2060 >        assertTrue(g.cancel(mayInterruptIfRunning));
2061 >        f.complete(v1);
2062 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2063 >
2064 >        // unspecified behavior
2065 >        Integer v;
2066 >        try {
2067 >            assertEquals(h.join(), inc(v1));
2068 >            assertEquals(r.invocationCount, 1);
2069 >        } catch (CompletionException ok) {
2070 >            checkCompletedWithWrappedCancellationException(h);
2071 >            assertEquals(r.invocationCount, 0);
2072 >        }
2073 >
2074 >        checkCancelled(g);
2075 >        checkCompletedNormally(f, v1);
2076 >        }
2077 >    }
2078 >
2079 >    public void testApplyToEither_sourceCancelled4() {
2080 >        for (ExecutionMode m : ExecutionMode.values())
2081 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2082 >        for (Integer v1 : new Integer[] { 1, null }) {
2083 >
2084 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2085 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2086 >        final IncFunction r = new IncFunction();
2087 >
2088 >        assertTrue(f.cancel(mayInterruptIfRunning));
2089 >        g.complete(v1);
2090 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2091 >
2092 >        // unspecified behavior
2093 >        Integer v;
2094 >        try {
2095 >            assertEquals(h.join(), inc(v1));
2096 >            assertEquals(r.invocationCount, 1);
2097 >        } catch (CompletionException ok) {
2098 >            checkCompletedWithWrappedCancellationException(h);
2099 >            assertEquals(r.invocationCount, 0);
2100 >        }
2101 >
2102 >        checkCancelled(f);
2103 >        checkCompletedNormally(g, v1);
2104 >        }
2105      }
2106  
2107      /**
2108       * acceptEither result completes normally after normal completion
2109       * of either source
2110       */
2111 <    public void testAcceptEither() {
2112 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2113 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2114 <        IncAction r = new IncAction();
1793 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1794 <        f.complete(one);
1795 <        checkCompletedNormally(g, null);
1796 <        f2.complete(one);
1797 <        checkCompletedNormally(g, null);
1798 <        assertEquals(r.value, 2);
2111 >    public void testAcceptEither_normalCompletion1() {
2112 >        for (ExecutionMode m : ExecutionMode.values())
2113 >        for (Integer v1 : new Integer[] { 1, null })
2114 >        for (Integer v2 : new Integer[] { 2, null }) {
2115  
2116 <        r = new IncAction();
2117 <        f = new CompletableFuture<>();
2118 <        f.complete(one);
2119 <        f2 = new CompletableFuture<>();
2120 <        g = f.acceptEither(f2, r);
2121 <        checkCompletedNormally(g, null);
2122 <        assertEquals(r.value, 2);
2116 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2117 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2118 >        final IncAction r = new IncAction();
2119 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2120 >
2121 >        f.complete(v1);
2122 >        checkCompletedNormally(h, null);
2123 >        assertEquals(r.value, inc(v1));
2124 >        g.complete(v2);
2125 >
2126 >        checkCompletedNormally(f, v1);
2127 >        checkCompletedNormally(g, v2);
2128 >        checkCompletedNormally(h, null);
2129 >        }
2130 >    }
2131 >
2132 >    public void testAcceptEither_normalCompletion2() {
2133 >        for (ExecutionMode m : ExecutionMode.values())
2134 >        for (Integer v1 : new Integer[] { 1, null })
2135 >        for (Integer v2 : new Integer[] { 2, null }) {
2136 >
2137 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2138 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2139 >        final IncAction r = new IncAction();
2140 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2141 >
2142 >        g.complete(v2);
2143 >        checkCompletedNormally(h, null);
2144 >        assertEquals(r.value, inc(v2));
2145 >        f.complete(v1);
2146 >
2147 >        checkCompletedNormally(f, v1);
2148 >        checkCompletedNormally(g, v2);
2149 >        checkCompletedNormally(h, null);
2150 >        }
2151 >    }
2152 >    public void testAcceptEither_normalCompletion3() {
2153 >        for (ExecutionMode m : ExecutionMode.values())
2154 >        for (Integer v1 : new Integer[] { 1, null })
2155 >        for (Integer v2 : new Integer[] { 2, null }) {
2156 >
2157 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2158 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2159 >        final IncAction r = new IncAction();
2160 >
2161 >        f.complete(v1);
2162 >        g.complete(v2);
2163 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2164 >
2165 >        checkCompletedNormally(h, null);
2166 >        checkCompletedNormally(f, v1);
2167 >        checkCompletedNormally(g, v2);
2168 >
2169 >        // unspecified behavior
2170 >        assertTrue(Objects.equals(r.value, inc(v1)) ||
2171 >                   Objects.equals(r.value, inc(v2)));
2172 >        }
2173      }
2174  
2175      /**
2176       * acceptEither result completes exceptionally after exceptional
2177       * completion of either source
2178       */
2179 <    public void testAcceptEither2() {
2180 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2181 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1816 <        IncAction r = new IncAction();
1817 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1818 <        f.completeExceptionally(new CFException());
1819 <        f2.complete(one);
1820 <        checkCompletedWithWrappedCFException(g);
2179 >    public void testAcceptEither_exceptionalCompletion1() {
2180 >        for (ExecutionMode m : ExecutionMode.values())
2181 >        for (Integer v1 : new Integer[] { 1, null }) {
2182  
2183 <        r = new IncAction();
2184 <        f = new CompletableFuture<>();
2185 <        f2 = new CompletableFuture<>();
2186 <        f2.completeExceptionally(new CFException());
2187 <        g = f.acceptEither(f2, r);
2188 <        checkCompletedWithWrappedCFException(g);
2183 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2184 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2185 >        final IncAction r = new IncAction();
2186 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2187 >        final CFException ex = new CFException();
2188 >
2189 >        f.completeExceptionally(ex);
2190 >        checkCompletedWithWrappedCFException(h, ex);
2191 >        g.complete(v1);
2192 >
2193 >        assertEquals(r.invocationCount, 0);
2194 >        checkCompletedNormally(g, v1);
2195 >        checkCompletedWithWrappedCFException(f, ex);
2196 >        checkCompletedWithWrappedCFException(h, ex);
2197 >        }
2198 >    }
2199 >
2200 >    public void testAcceptEither_exceptionalCompletion2() {
2201 >        for (ExecutionMode m : ExecutionMode.values())
2202 >        for (Integer v1 : new Integer[] { 1, null }) {
2203 >
2204 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2205 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2206 >        final IncAction r = new IncAction();
2207 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2208 >        final CFException ex = new CFException();
2209 >
2210 >        g.completeExceptionally(ex);
2211 >        checkCompletedWithWrappedCFException(h, ex);
2212 >        f.complete(v1);
2213 >
2214 >        assertEquals(r.invocationCount, 0);
2215 >        checkCompletedNormally(f, v1);
2216 >        checkCompletedWithWrappedCFException(g, ex);
2217 >        checkCompletedWithWrappedCFException(h, ex);
2218 >        }
2219 >    }
2220 >
2221 >    public void testAcceptEither_exceptionalCompletion3() {
2222 >        for (ExecutionMode m : ExecutionMode.values())
2223 >        for (Integer v1 : new Integer[] { 1, null }) {
2224 >
2225 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2226 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2227 >        final IncAction r = new IncAction();
2228 >        final CFException ex = new CFException();
2229 >
2230 >        g.completeExceptionally(ex);
2231 >        f.complete(v1);
2232 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2233 >
2234 >        // unspecified behavior
2235 >        Integer v;
2236 >        try {
2237 >            assertEquals(h.join(), null);
2238 >            assertEquals(r.invocationCount, 1);
2239 >            assertEquals(inc(v1), r.value);
2240 >        } catch (CompletionException ok) {
2241 >            checkCompletedWithWrappedCFException(h, ex);
2242 >            assertEquals(r.invocationCount, 0);
2243 >        }
2244 >
2245 >        checkCompletedWithWrappedCFException(g, ex);
2246 >        checkCompletedNormally(f, v1);
2247 >        }
2248 >    }
2249 >
2250 >    public void testAcceptEither_exceptionalCompletion4() {
2251 >        for (ExecutionMode m : ExecutionMode.values())
2252 >        for (Integer v1 : new Integer[] { 1, null }) {
2253 >
2254 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2255 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2256 >        final IncAction r = new IncAction();
2257 >        final CFException ex = new CFException();
2258 >
2259 >        f.completeExceptionally(ex);
2260 >        g.complete(v1);
2261 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2262 >
2263 >        // unspecified behavior
2264 >        Integer v;
2265 >        try {
2266 >            assertEquals(h.join(), null);
2267 >            assertEquals(r.invocationCount, 1);
2268 >            assertEquals(inc(v1), r.value);
2269 >        } catch (CompletionException ok) {
2270 >            checkCompletedWithWrappedCFException(h, ex);
2271 >            assertEquals(r.invocationCount, 0);
2272 >        }
2273 >
2274 >        checkCompletedWithWrappedCFException(f, ex);
2275 >        checkCompletedNormally(g, v1);
2276 >        }
2277      }
2278  
2279      /**
2280       * acceptEither result completes exceptionally if action does
2281       */
2282 <    public void testAcceptEither3() {
2283 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2284 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2285 <        FailingConsumer r = new FailingConsumer();
2286 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2287 <        f2.complete(two);
2288 <        checkCompletedWithWrappedCFException(g);
2282 >    public void testAcceptEither_actionFailed1() {
2283 >        for (ExecutionMode m : ExecutionMode.values())
2284 >        for (Integer v1 : new Integer[] { 1, null })
2285 >        for (Integer v2 : new Integer[] { 2, null }) {
2286 >
2287 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2288 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2289 >        final FailingConsumer r = new FailingConsumer();
2290 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2291 >
2292 >        f.complete(v1);
2293 >        checkCompletedWithWrappedCFException(h);
2294 >        g.complete(v2);
2295 >        checkCompletedNormally(f, v1);
2296 >        checkCompletedNormally(g, v2);
2297 >        }
2298 >    }
2299 >
2300 >    public void testAcceptEither_actionFailed2() {
2301 >        for (ExecutionMode m : ExecutionMode.values())
2302 >        for (Integer v1 : new Integer[] { 1, null })
2303 >        for (Integer v2 : new Integer[] { 2, null }) {
2304 >
2305 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2306 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2307 >        final FailingConsumer r = new FailingConsumer();
2308 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2309 >
2310 >        g.complete(v2);
2311 >        checkCompletedWithWrappedCFException(h);
2312 >        f.complete(v1);
2313 >        checkCompletedNormally(f, v1);
2314 >        checkCompletedNormally(g, v2);
2315 >        }
2316      }
2317  
2318      /**
2319       * acceptEither result completes exceptionally if either source cancelled
2320       */
2321 <    public void testAcceptEither4() {
2322 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2323 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2324 <        IncAction r = new IncAction();
2325 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2326 <        assertTrue(f.cancel(true));
2327 <        checkCompletedWithWrappedCancellationException(g);
2328 <        f = new CompletableFuture<>();
2329 <        f2 = new CompletableFuture<>();
2330 <        assertTrue(f2.cancel(true));
2331 <        checkCompletedWithWrappedCancellationException(g);
2321 >    public void testAcceptEither_sourceCancelled1() {
2322 >        for (ExecutionMode m : ExecutionMode.values())
2323 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2324 >        for (Integer v1 : new Integer[] { 1, null }) {
2325 >
2326 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2327 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2328 >        final IncAction r = new IncAction();
2329 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2330 >
2331 >        assertTrue(f.cancel(mayInterruptIfRunning));
2332 >        checkCompletedWithWrappedCancellationException(h);
2333 >        g.complete(v1);
2334 >
2335 >        checkCancelled(f);
2336 >        assertEquals(r.invocationCount, 0);
2337 >        checkCompletedNormally(g, v1);
2338 >        checkCompletedWithWrappedCancellationException(h);
2339 >        }
2340 >    }
2341 >
2342 >    public void testAcceptEither_sourceCancelled2() {
2343 >        for (ExecutionMode m : ExecutionMode.values())
2344 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2345 >        for (Integer v1 : new Integer[] { 1, null }) {
2346 >
2347 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2348 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2349 >        final IncAction r = new IncAction();
2350 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2351 >
2352 >        assertTrue(g.cancel(mayInterruptIfRunning));
2353 >        checkCompletedWithWrappedCancellationException(h);
2354 >        f.complete(v1);
2355 >
2356 >        checkCancelled(g);
2357 >        assertEquals(r.invocationCount, 0);
2358 >        checkCompletedNormally(f, v1);
2359 >        checkCompletedWithWrappedCancellationException(h);
2360 >        }
2361 >    }
2362 >
2363 >    public void testAcceptEither_sourceCancelled3() {
2364 >        for (ExecutionMode m : ExecutionMode.values())
2365 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2366 >        for (Integer v1 : new Integer[] { 1, null }) {
2367 >
2368 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2369 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2370 >        final IncAction r = new IncAction();
2371 >
2372 >        assertTrue(g.cancel(mayInterruptIfRunning));
2373 >        f.complete(v1);
2374 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2375 >
2376 >        // unspecified behavior
2377 >        Integer v;
2378 >        try {
2379 >            assertEquals(h.join(), null);
2380 >            assertEquals(r.invocationCount, 1);
2381 >            assertEquals(inc(v1), r.value);
2382 >        } catch (CompletionException ok) {
2383 >            checkCompletedWithWrappedCancellationException(h);
2384 >            assertEquals(r.invocationCount, 0);
2385 >        }
2386 >
2387 >        checkCancelled(g);
2388 >        checkCompletedNormally(f, v1);
2389 >        }
2390 >    }
2391 >
2392 >    public void testAcceptEither_sourceCancelled4() {
2393 >        for (ExecutionMode m : ExecutionMode.values())
2394 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2395 >        for (Integer v1 : new Integer[] { 1, null }) {
2396 >
2397 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2398 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2399 >        final IncAction r = new IncAction();
2400 >
2401 >        assertTrue(f.cancel(mayInterruptIfRunning));
2402 >        g.complete(v1);
2403 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2404 >
2405 >        // unspecified behavior
2406 >        Integer v;
2407 >        try {
2408 >            assertEquals(h.join(), null);
2409 >            assertEquals(r.invocationCount, 1);
2410 >            assertEquals(inc(v1), r.value);
2411 >        } catch (CompletionException ok) {
2412 >            checkCompletedWithWrappedCancellationException(h);
2413 >            assertEquals(r.invocationCount, 0);
2414 >        }
2415 >
2416 >        checkCancelled(f);
2417 >        checkCompletedNormally(g, v1);
2418 >        }
2419      }
2420  
2421      /**
2422       * runAfterEither result completes normally after normal completion
2423       * of either source
2424       */
2425 <    public void testRunAfterEither() {
2426 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2427 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2428 <        Noop r = new Noop();
1866 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1867 <        f.complete(one);
1868 <        checkCompletedNormally(g, null);
1869 <        f2.complete(one);
1870 <        checkCompletedNormally(g, null);
1871 <        assertTrue(r.ran);
2425 >    public void testRunAfterEither_normalCompletion1() {
2426 >        for (ExecutionMode m : ExecutionMode.values())
2427 >        for (Integer v1 : new Integer[] { 1, null })
2428 >        for (Integer v2 : new Integer[] { 2, null }) {
2429  
2430 <        r = new Noop();
2431 <        f = new CompletableFuture<>();
2432 <        f.complete(one);
2433 <        f2 = new CompletableFuture<>();
2434 <        g = f.runAfterEither(f2, r);
2435 <        checkCompletedNormally(g, null);
2436 <        assertTrue(r.ran);
2430 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2431 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2432 >        final Noop r = new Noop();
2433 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2434 >
2435 >        f.complete(v1);
2436 >        checkCompletedNormally(h, null);
2437 >        assertEquals(r.invocationCount, 1);
2438 >        g.complete(v2);
2439 >
2440 >        checkCompletedNormally(f, v1);
2441 >        checkCompletedNormally(g, v2);
2442 >        checkCompletedNormally(h, null);
2443 >        assertEquals(r.invocationCount, 1);
2444 >        }
2445 >    }
2446 >
2447 >    public void testRunAfterEither_normalCompletion2() {
2448 >        for (ExecutionMode m : ExecutionMode.values())
2449 >        for (Integer v1 : new Integer[] { 1, null })
2450 >        for (Integer v2 : new Integer[] { 2, null }) {
2451 >
2452 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2453 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2454 >        final Noop r = new Noop();
2455 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2456 >
2457 >        g.complete(v2);
2458 >        checkCompletedNormally(h, null);
2459 >        assertEquals(r.invocationCount, 1);
2460 >        f.complete(v1);
2461 >
2462 >        checkCompletedNormally(f, v1);
2463 >        checkCompletedNormally(g, v2);
2464 >        checkCompletedNormally(h, null);
2465 >        assertEquals(r.invocationCount, 1);
2466 >        }
2467 >    }
2468 >    public void testRunAfterEither_normalCompletion3() {
2469 >        for (ExecutionMode m : ExecutionMode.values())
2470 >        for (Integer v1 : new Integer[] { 1, null })
2471 >        for (Integer v2 : new Integer[] { 2, null }) {
2472 >
2473 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2474 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2475 >        final Noop r = new Noop();
2476 >
2477 >        f.complete(v1);
2478 >        g.complete(v2);
2479 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2480 >
2481 >        checkCompletedNormally(h, null);
2482 >        checkCompletedNormally(f, v1);
2483 >        checkCompletedNormally(g, v2);
2484 >        assertEquals(r.invocationCount, 1);
2485 >        }
2486      }
2487  
2488      /**
2489       * runAfterEither result completes exceptionally after exceptional
2490       * completion of either source
2491       */
2492 <    public void testRunAfterEither2() {
2493 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2494 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1889 <        Noop r = new Noop();
1890 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1891 <        f.completeExceptionally(new CFException());
1892 <        f2.complete(one);
1893 <        checkCompletedWithWrappedCFException(g);
2492 >    public void testRunAfterEither_exceptionalCompletion1() {
2493 >        for (ExecutionMode m : ExecutionMode.values())
2494 >        for (Integer v1 : new Integer[] { 1, null }) {
2495  
2496 <        r = new Noop();
2497 <        f = new CompletableFuture<>();
2498 <        f2 = new CompletableFuture<>();
2499 <        f2.completeExceptionally(new CFException());
2500 <        g = f.runAfterEither(f2, r);
2501 <        checkCompletedWithWrappedCFException(g);
2496 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2497 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2498 >        final Noop r = new Noop();
2499 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2500 >        final CFException ex = new CFException();
2501 >
2502 >        f.completeExceptionally(ex);
2503 >        checkCompletedWithWrappedCFException(h, ex);
2504 >        g.complete(v1);
2505 >
2506 >        assertEquals(r.invocationCount, 0);
2507 >        checkCompletedNormally(g, v1);
2508 >        checkCompletedWithWrappedCFException(f, ex);
2509 >        checkCompletedWithWrappedCFException(h, ex);
2510 >        }
2511 >    }
2512 >
2513 >    public void testRunAfterEither_exceptionalCompletion2() {
2514 >        for (ExecutionMode m : ExecutionMode.values())
2515 >        for (Integer v1 : new Integer[] { 1, null }) {
2516 >
2517 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2518 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2519 >        final Noop r = new Noop();
2520 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2521 >        final CFException ex = new CFException();
2522 >
2523 >        g.completeExceptionally(ex);
2524 >        checkCompletedWithWrappedCFException(h, ex);
2525 >        f.complete(v1);
2526 >
2527 >        assertEquals(r.invocationCount, 0);
2528 >        checkCompletedNormally(f, v1);
2529 >        checkCompletedWithWrappedCFException(g, ex);
2530 >        checkCompletedWithWrappedCFException(h, ex);
2531 >        }
2532 >    }
2533 >
2534 >    public void testRunAfterEither_exceptionalCompletion3() {
2535 >        for (ExecutionMode m : ExecutionMode.values())
2536 >        for (Integer v1 : new Integer[] { 1, null }) {
2537 >
2538 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2539 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2540 >        final Noop r = new Noop();
2541 >        final CFException ex = new CFException();
2542 >
2543 >        g.completeExceptionally(ex);
2544 >        f.complete(v1);
2545 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2546 >
2547 >        // unspecified behavior
2548 >        Integer v;
2549 >        try {
2550 >            assertEquals(h.join(), null);
2551 >            assertEquals(r.invocationCount, 1);
2552 >        } catch (CompletionException ok) {
2553 >            checkCompletedWithWrappedCFException(h, ex);
2554 >            assertEquals(r.invocationCount, 0);
2555 >        }
2556 >
2557 >        checkCompletedWithWrappedCFException(g, ex);
2558 >        checkCompletedNormally(f, v1);
2559 >        }
2560 >    }
2561 >
2562 >    public void testRunAfterEither_exceptionalCompletion4() {
2563 >        for (ExecutionMode m : ExecutionMode.values())
2564 >        for (Integer v1 : new Integer[] { 1, null }) {
2565 >
2566 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2567 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2568 >        final Noop r = new Noop();
2569 >        final CFException ex = new CFException();
2570 >
2571 >        f.completeExceptionally(ex);
2572 >        g.complete(v1);
2573 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2574 >
2575 >        // unspecified behavior
2576 >        Integer v;
2577 >        try {
2578 >            assertEquals(h.join(), null);
2579 >            assertEquals(r.invocationCount, 1);
2580 >        } catch (CompletionException ok) {
2581 >            checkCompletedWithWrappedCFException(h, ex);
2582 >            assertEquals(r.invocationCount, 0);
2583 >        }
2584 >
2585 >        checkCompletedWithWrappedCFException(f, ex);
2586 >        checkCompletedNormally(g, v1);
2587 >        }
2588      }
2589  
2590      /**
2591       * runAfterEither result completes exceptionally if action does
2592       */
2593 <    public void testRunAfterEither3() {
2594 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2595 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2596 <        FailingNoop r = new FailingNoop();
2597 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2598 <        f2.complete(two);
2599 <        checkCompletedWithWrappedCFException(g);
2593 >    public void testRunAfterEither_actionFailed1() {
2594 >        for (ExecutionMode m : ExecutionMode.values())
2595 >        for (Integer v1 : new Integer[] { 1, null })
2596 >        for (Integer v2 : new Integer[] { 2, null }) {
2597 >
2598 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2599 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2600 >        final FailingNoop r = new FailingNoop();
2601 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2602 >
2603 >        f.complete(v1);
2604 >        checkCompletedWithWrappedCFException(h);
2605 >        g.complete(v2);
2606 >        checkCompletedNormally(f, v1);
2607 >        checkCompletedNormally(g, v2);
2608 >        }
2609 >    }
2610 >
2611 >    public void testRunAfterEither_actionFailed2() {
2612 >        for (ExecutionMode m : ExecutionMode.values())
2613 >        for (Integer v1 : new Integer[] { 1, null })
2614 >        for (Integer v2 : new Integer[] { 2, null }) {
2615 >
2616 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2617 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2618 >        final FailingNoop r = new FailingNoop();
2619 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2620 >
2621 >        g.complete(v2);
2622 >        checkCompletedWithWrappedCFException(h);
2623 >        f.complete(v1);
2624 >        checkCompletedNormally(f, v1);
2625 >        checkCompletedNormally(g, v2);
2626 >        }
2627      }
2628  
2629      /**
2630       * runAfterEither result completes exceptionally if either source cancelled
2631       */
2632 <    public void testRunAfterEither4() {
2633 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2634 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2635 <        Noop r = new Noop();
2636 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2637 <        assertTrue(f.cancel(true));
2638 <        checkCompletedWithWrappedCancellationException(g);
2639 <        f = new CompletableFuture<>();
2640 <        f2 = new CompletableFuture<>();
2641 <        assertTrue(f2.cancel(true));
2642 <        checkCompletedWithWrappedCancellationException(g);
2632 >    public void testRunAfterEither_sourceCancelled1() {
2633 >        for (ExecutionMode m : ExecutionMode.values())
2634 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2635 >        for (Integer v1 : new Integer[] { 1, null }) {
2636 >
2637 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2638 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2639 >        final Noop r = new Noop();
2640 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2641 >
2642 >        assertTrue(f.cancel(mayInterruptIfRunning));
2643 >        checkCompletedWithWrappedCancellationException(h);
2644 >        g.complete(v1);
2645 >
2646 >        checkCancelled(f);
2647 >        assertEquals(r.invocationCount, 0);
2648 >        checkCompletedNormally(g, v1);
2649 >        checkCompletedWithWrappedCancellationException(h);
2650 >        }
2651 >    }
2652 >
2653 >    public void testRunAfterEither_sourceCancelled2() {
2654 >        for (ExecutionMode m : ExecutionMode.values())
2655 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2656 >        for (Integer v1 : new Integer[] { 1, null }) {
2657 >
2658 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2659 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2660 >        final Noop r = new Noop();
2661 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2662 >
2663 >        assertTrue(g.cancel(mayInterruptIfRunning));
2664 >        checkCompletedWithWrappedCancellationException(h);
2665 >        f.complete(v1);
2666 >
2667 >        checkCancelled(g);
2668 >        assertEquals(r.invocationCount, 0);
2669 >        checkCompletedNormally(f, v1);
2670 >        checkCompletedWithWrappedCancellationException(h);
2671 >        }
2672 >    }
2673 >
2674 >    public void testRunAfterEither_sourceCancelled3() {
2675 >        for (ExecutionMode m : ExecutionMode.values())
2676 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2677 >        for (Integer v1 : new Integer[] { 1, null }) {
2678 >
2679 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2680 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2681 >        final Noop r = new Noop();
2682 >
2683 >        assertTrue(g.cancel(mayInterruptIfRunning));
2684 >        f.complete(v1);
2685 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2686 >
2687 >        // unspecified behavior
2688 >        Integer v;
2689 >        try {
2690 >            assertEquals(h.join(), null);
2691 >            assertEquals(r.invocationCount, 1);
2692 >        } catch (CompletionException ok) {
2693 >            checkCompletedWithWrappedCancellationException(h);
2694 >            assertEquals(r.invocationCount, 0);
2695 >        }
2696 >
2697 >        checkCancelled(g);
2698 >        checkCompletedNormally(f, v1);
2699 >        }
2700 >    }
2701 >
2702 >    public void testRunAfterEither_sourceCancelled4() {
2703 >        for (ExecutionMode m : ExecutionMode.values())
2704 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2705 >        for (Integer v1 : new Integer[] { 1, null }) {
2706 >
2707 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2708 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2709 >        final Noop r = new Noop();
2710 >
2711 >        assertTrue(f.cancel(mayInterruptIfRunning));
2712 >        g.complete(v1);
2713 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2714 >
2715 >        // unspecified behavior
2716 >        Integer v;
2717 >        try {
2718 >            assertEquals(h.join(), null);
2719 >            assertEquals(r.invocationCount, 1);
2720 >        } catch (CompletionException ok) {
2721 >            checkCompletedWithWrappedCancellationException(h);
2722 >            assertEquals(r.invocationCount, 0);
2723 >        }
2724 >
2725 >        checkCancelled(f);
2726 >        checkCompletedNormally(g, v1);
2727 >        }
2728      }
2729  
2730      /**
# Line 2113 | Line 2912 | public class CompletableFutureTest exten
2912          CompletableFuture<Void> g = f.thenAcceptAsync(r);
2913          f.complete(one);
2914          checkCompletedNormally(g, null);
2915 <        assertEquals(r.value, 2);
2915 >        assertEquals(r.value, (Integer) 2);
2916      }
2917  
2918      /**
# Line 2151 | Line 2950 | public class CompletableFutureTest exten
2950      }
2951  
2952      /**
2154     * applyToEitherAsync result completes normally after normal
2155     * completion of sources
2156     */
2157    public void testApplyToEitherAsync() {
2158        CompletableFuture<Integer> f = new CompletableFuture<>();
2159        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2160        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2161        f.complete(one);
2162        checkCompletedNormally(g, two);
2163
2164        f = new CompletableFuture<>();
2165        f.complete(one);
2166        f2 = new CompletableFuture<>();
2167        g = f.applyToEitherAsync(f2, inc);
2168        checkCompletedNormally(g, two);
2169    }
2170
2171    /**
2172     * applyToEitherAsync result completes exceptionally after exceptional
2173     * completion of source
2174     */
2175    public void testApplyToEitherAsync2() {
2176        CompletableFuture<Integer> f = new CompletableFuture<>();
2177        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2178        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2179        f.completeExceptionally(new CFException());
2180        checkCompletedWithWrappedCFException(g);
2181
2182        f = new CompletableFuture<>();
2183        f2 = new CompletableFuture<>();
2184        f2.completeExceptionally(new CFException());
2185        g = f.applyToEitherAsync(f2, inc);
2186        f.complete(one);
2187        checkCompletedWithWrappedCFException(g);
2188    }
2189
2190    /**
2191     * applyToEitherAsync result completes exceptionally if action does
2192     */
2193    public void testApplyToEitherAsync3() {
2194        CompletableFuture<Integer> f = new CompletableFuture<>();
2195        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2196        FailingFunction r = new FailingFunction();
2197        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r);
2198        f.complete(one);
2199        checkCompletedWithWrappedCFException(g);
2200    }
2201
2202    /**
2203     * applyToEitherAsync result completes exceptionally if either source cancelled
2204     */
2205    public void testApplyToEitherAsync4() {
2206        CompletableFuture<Integer> f = new CompletableFuture<>();
2207        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2208        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2209        assertTrue(f.cancel(true));
2210        checkCompletedWithWrappedCancellationException(g);
2211
2212        f = new CompletableFuture<>();
2213        f2 = new CompletableFuture<>();
2214        assertTrue(f2.cancel(true));
2215        g = f.applyToEitherAsync(f2, inc);
2216        checkCompletedWithWrappedCancellationException(g);
2217    }
2218
2219    /**
2220     * acceptEitherAsync result completes normally after normal
2221     * completion of sources
2222     */
2223    public void testAcceptEitherAsync() {
2224        CompletableFuture<Integer> f = new CompletableFuture<>();
2225        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2226        IncAction r = new IncAction();
2227        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2228        f.complete(one);
2229        checkCompletedNormally(g, null);
2230        assertEquals(r.value, 2);
2231
2232        r = new IncAction();
2233        f = new CompletableFuture<>();
2234        f.complete(one);
2235        f2 = new CompletableFuture<>();
2236        g = f.acceptEitherAsync(f2, r);
2237        checkCompletedNormally(g, null);
2238        assertEquals(r.value, 2);
2239    }
2240
2241    /**
2242     * acceptEitherAsync result completes exceptionally after exceptional
2243     * completion of source
2244     */
2245    public void testAcceptEitherAsync2() {
2246        CompletableFuture<Integer> f = new CompletableFuture<>();
2247        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2248        IncAction r = new IncAction();
2249        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2250        f.completeExceptionally(new CFException());
2251        checkCompletedWithWrappedCFException(g);
2252
2253        r = new IncAction();
2254        f = new CompletableFuture<>();
2255        f2 = new CompletableFuture<>();
2256        f2.completeExceptionally(new CFException());
2257        g = f.acceptEitherAsync(f2, r);
2258        f.complete(one);
2259        checkCompletedWithWrappedCFException(g);
2260    }
2261
2262    /**
2263     * acceptEitherAsync result completes exceptionally if action does
2264     */
2265    public void testAcceptEitherAsync3() {
2266        CompletableFuture<Integer> f = new CompletableFuture<>();
2267        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2268        FailingConsumer r = new FailingConsumer();
2269        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2270        f.complete(one);
2271        checkCompletedWithWrappedCFException(g);
2272    }
2273
2274    /**
2275     * acceptEitherAsync result completes exceptionally if either
2276     * source cancelled
2277     */
2278    public void testAcceptEitherAsync4() {
2279        CompletableFuture<Integer> f = new CompletableFuture<>();
2280        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2281        IncAction r = new IncAction();
2282        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2283        assertTrue(f.cancel(true));
2284        checkCompletedWithWrappedCancellationException(g);
2285
2286        r = new IncAction();
2287        f = new CompletableFuture<>();
2288        f2 = new CompletableFuture<>();
2289        assertTrue(f2.cancel(true));
2290        g = f.acceptEitherAsync(f2, r);
2291        checkCompletedWithWrappedCancellationException(g);
2292    }
2293
2294    /**
2295     * runAfterEitherAsync result completes normally after normal
2296     * completion of sources
2297     */
2298    public void testRunAfterEitherAsync() {
2299        CompletableFuture<Integer> f = new CompletableFuture<>();
2300        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2301        Noop r = new Noop();
2302        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2303        f.complete(one);
2304        checkCompletedNormally(g, null);
2305        assertTrue(r.ran);
2306
2307        r = new Noop();
2308        f = new CompletableFuture<>();
2309        f.complete(one);
2310        f2 = new CompletableFuture<>();
2311        g = f.runAfterEitherAsync(f2, r);
2312        checkCompletedNormally(g, null);
2313        assertTrue(r.ran);
2314    }
2315
2316    /**
2317     * runAfterEitherAsync result completes exceptionally after exceptional
2318     * completion of source
2319     */
2320    public void testRunAfterEitherAsync2() {
2321        CompletableFuture<Integer> f = new CompletableFuture<>();
2322        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2323        Noop r = new Noop();
2324        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2325        f.completeExceptionally(new CFException());
2326        checkCompletedWithWrappedCFException(g);
2327
2328        r = new Noop();
2329        f = new CompletableFuture<>();
2330        f2 = new CompletableFuture<>();
2331        f2.completeExceptionally(new CFException());
2332        g = f.runAfterEitherAsync(f2, r);
2333        f.complete(one);
2334        checkCompletedWithWrappedCFException(g);
2335    }
2336
2337    /**
2338     * runAfterEitherAsync result completes exceptionally if action does
2339     */
2340    public void testRunAfterEitherAsync3() {
2341        CompletableFuture<Integer> f = new CompletableFuture<>();
2342        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2343        FailingNoop r = new FailingNoop();
2344        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2345        f.complete(one);
2346        checkCompletedWithWrappedCFException(g);
2347    }
2348
2349    /**
2350     * runAfterEitherAsync result completes exceptionally if either
2351     * source cancelled
2352     */
2353    public void testRunAfterEitherAsync4() {
2354        CompletableFuture<Integer> f = new CompletableFuture<>();
2355        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2356        Noop r = new Noop();
2357        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2358        assertTrue(f.cancel(true));
2359        checkCompletedWithWrappedCancellationException(g);
2360
2361        r = new Noop();
2362        f = new CompletableFuture<>();
2363        f2 = new CompletableFuture<>();
2364        assertTrue(f2.cancel(true));
2365        g = f.runAfterEitherAsync(f2, r);
2366        checkCompletedWithWrappedCancellationException(g);
2367    }
2368
2369    /**
2953       * thenComposeAsync result completes normally after normal
2954       * completion of source
2955       */
# Line 2552 | Line 3135 | public class CompletableFutureTest exten
3135          CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3136          f.complete(one);
3137          checkCompletedNormally(g, null);
3138 <        assertEquals(r.value, 2);
3138 >        assertEquals(r.value, (Integer) 2);
3139      }
3140  
3141      /**
# Line 2590 | Line 3173 | public class CompletableFutureTest exten
3173      }
3174  
3175      /**
2593     * applyToEitherAsync result completes normally after normal
2594     * completion of sources
2595     */
2596    public void testApplyToEitherAsyncE() {
2597        CompletableFuture<Integer> f = new CompletableFuture<>();
2598        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2599        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2600        f.complete(one);
2601        checkCompletedNormally(g, two);
2602
2603        f = new CompletableFuture<>();
2604        f.complete(one);
2605        f2 = new CompletableFuture<>();
2606        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2607        checkCompletedNormally(g, two);
2608    }
2609
2610    /**
2611     * applyToEitherAsync result completes exceptionally after exceptional
2612     * completion of source
2613     */
2614    public void testApplyToEitherAsync2E() {
2615        CompletableFuture<Integer> f = new CompletableFuture<>();
2616        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2617        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2618        f.completeExceptionally(new CFException());
2619        checkCompletedWithWrappedCFException(g);
2620
2621        f = new CompletableFuture<>();
2622        f2 = new CompletableFuture<>();
2623        f2.completeExceptionally(new CFException());
2624        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2625        f.complete(one);
2626        checkCompletedWithWrappedCFException(g);
2627    }
2628
2629    /**
2630     * applyToEitherAsync result completes exceptionally if action does
2631     */
2632    public void testApplyToEitherAsync3E() {
2633        CompletableFuture<Integer> f = new CompletableFuture<>();
2634        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2635        FailingFunction r = new FailingFunction();
2636        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor());
2637        f.complete(one);
2638        checkCompletedWithWrappedCFException(g);
2639    }
2640
2641    /**
2642     * applyToEitherAsync result completes exceptionally if either source cancelled
2643     */
2644    public void testApplyToEitherAsync4E() {
2645        CompletableFuture<Integer> f = new CompletableFuture<>();
2646        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2647        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2648        assertTrue(f.cancel(true));
2649        checkCompletedWithWrappedCancellationException(g);
2650
2651        f = new CompletableFuture<>();
2652        f2 = new CompletableFuture<>();
2653        assertTrue(f2.cancel(true));
2654        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2655        checkCompletedWithWrappedCancellationException(g);
2656    }
2657
2658    /**
2659     * acceptEitherAsync result completes normally after normal
2660     * completion of sources
2661     */
2662    public void testAcceptEitherAsyncE() {
2663        CompletableFuture<Integer> f = new CompletableFuture<>();
2664        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2665        IncAction r = new IncAction();
2666        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2667        f.complete(one);
2668        checkCompletedNormally(g, null);
2669        assertEquals(r.value, 2);
2670
2671        r = new IncAction();
2672        f = new CompletableFuture<>();
2673        f.complete(one);
2674        f2 = new CompletableFuture<>();
2675        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2676        checkCompletedNormally(g, null);
2677        assertEquals(r.value, 2);
2678    }
2679
2680    /**
2681     * acceptEitherAsync result completes exceptionally after exceptional
2682     * completion of source
2683     */
2684    public void testAcceptEitherAsync2E() {
2685        CompletableFuture<Integer> f = new CompletableFuture<>();
2686        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2687        IncAction r = new IncAction();
2688        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2689        f.completeExceptionally(new CFException());
2690        checkCompletedWithWrappedCFException(g);
2691
2692        r = new IncAction();
2693        f = new CompletableFuture<>();
2694        f2 = new CompletableFuture<>();
2695        f2.completeExceptionally(new CFException());
2696        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2697        f.complete(one);
2698        checkCompletedWithWrappedCFException(g);
2699    }
2700
2701    /**
2702     * acceptEitherAsync result completes exceptionally if action does
2703     */
2704    public void testAcceptEitherAsync3E() {
2705        CompletableFuture<Integer> f = new CompletableFuture<>();
2706        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2707        FailingConsumer r = new FailingConsumer();
2708        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2709        f.complete(one);
2710        checkCompletedWithWrappedCFException(g);
2711    }
2712
2713    /**
2714     * acceptEitherAsync result completes exceptionally if either
2715     * source cancelled
2716     */
2717    public void testAcceptEitherAsync4E() {
2718        CompletableFuture<Integer> f = new CompletableFuture<>();
2719        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2720        IncAction r = new IncAction();
2721        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2722        assertTrue(f.cancel(true));
2723        checkCompletedWithWrappedCancellationException(g);
2724
2725        r = new IncAction();
2726        f = new CompletableFuture<>();
2727        f2 = new CompletableFuture<>();
2728        assertTrue(f2.cancel(true));
2729        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2730        checkCompletedWithWrappedCancellationException(g);
2731    }
2732
2733    /**
2734     * runAfterEitherAsync result completes normally after normal
2735     * completion of sources
2736     */
2737    public void testRunAfterEitherAsyncE() {
2738        CompletableFuture<Integer> f = new CompletableFuture<>();
2739        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2740        Noop r = new Noop();
2741        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2742        f.complete(one);
2743        checkCompletedNormally(g, null);
2744        assertTrue(r.ran);
2745
2746        r = new Noop();
2747        f = new CompletableFuture<>();
2748        f.complete(one);
2749        f2 = new CompletableFuture<>();
2750        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2751        checkCompletedNormally(g, null);
2752        assertTrue(r.ran);
2753    }
2754
2755    /**
2756     * runAfterEitherAsync result completes exceptionally after exceptional
2757     * completion of source
2758     */
2759    public void testRunAfterEitherAsync2E() {
2760        CompletableFuture<Integer> f = new CompletableFuture<>();
2761        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2762        Noop r = new Noop();
2763        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2764        f.completeExceptionally(new CFException());
2765        checkCompletedWithWrappedCFException(g);
2766
2767        r = new Noop();
2768        f = new CompletableFuture<>();
2769        f2 = new CompletableFuture<>();
2770        f2.completeExceptionally(new CFException());
2771        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2772        f.complete(one);
2773        checkCompletedWithWrappedCFException(g);
2774    }
2775
2776    /**
2777     * runAfterEitherAsync result completes exceptionally if action does
2778     */
2779    public void testRunAfterEitherAsync3E() {
2780        CompletableFuture<Integer> f = new CompletableFuture<>();
2781        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2782        FailingNoop r = new FailingNoop();
2783        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2784        f.complete(one);
2785        checkCompletedWithWrappedCFException(g);
2786    }
2787
2788    /**
2789     * runAfterEitherAsync result completes exceptionally if either
2790     * source cancelled
2791     */
2792    public void testRunAfterEitherAsync4E() {
2793        CompletableFuture<Integer> f = new CompletableFuture<>();
2794        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2795        Noop r = new Noop();
2796        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2797        assertTrue(f.cancel(true));
2798        checkCompletedWithWrappedCancellationException(g);
2799
2800        r = new Noop();
2801        f = new CompletableFuture<>();
2802        f2 = new CompletableFuture<>();
2803        assertTrue(f2.cancel(true));
2804        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2805        checkCompletedWithWrappedCancellationException(g);
2806    }
2807
2808    /**
3176       * thenComposeAsync result completes normally after normal
3177       * completion of source
3178       */
# Line 3049 | Line 3416 | public class CompletableFutureTest exten
3416       * whenComplete action executes on normal completion, propagating
3417       * source result.
3418       */
3419 <    public void testWhenComplete1() {
3419 >    public void testWhenComplete_normalCompletion1() {
3420 >        for (ExecutionMode m : ExecutionMode.values())
3421 >        for (Integer v1 : new Integer[] { 1, null }) {
3422 >
3423          final AtomicInteger a = new AtomicInteger();
3424 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3425 <        CompletableFuture<Integer> g =
3426 <            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3427 <        f.complete(three);
3428 <        checkCompletedNormally(f, three);
3429 <        checkCompletedNormally(g, three);
3424 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3425 >        final CompletableFuture<Integer> g =
3426 >            m.whenComplete(f,
3427 >                           (Integer x, Throwable t) -> {
3428 >                               threadAssertSame(x, v1);
3429 >                               threadAssertNull(t);
3430 >                               a.getAndIncrement();
3431 >                           });
3432 >        f.complete(v1);
3433 >        checkCompletedNormally(f, v1);
3434 >        checkCompletedNormally(g, v1);
3435          assertEquals(a.get(), 1);
3436 +        }
3437      }
3438  
3439      /**
3440       * whenComplete action executes on exceptional completion, propagating
3441       * source result.
3442       */
3443 <    public void testWhenComplete2() {
3443 >    public void testWhenComplete_exceptionalCompletion() {
3444 >        for (ExecutionMode m : ExecutionMode.values())
3445 >        for (Integer v1 : new Integer[] { 1, null }) {
3446 >
3447          final AtomicInteger a = new AtomicInteger();
3448 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3449 <        CompletableFuture<Integer> g =
3450 <            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3451 <        f.completeExceptionally(new CFException());
3452 <        assertTrue(f.isCompletedExceptionally());
3453 <        assertTrue(g.isCompletedExceptionally());
3448 >        final CFException ex = new CFException();
3449 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3450 >        final CompletableFuture<Integer> g = m.whenComplete
3451 >            (f,
3452 >             (Integer x, Throwable t) -> {
3453 >                threadAssertNull(x);
3454 >                threadAssertSame(t, ex);
3455 >                a.getAndIncrement();
3456 >            });
3457 >        f.completeExceptionally(ex);
3458 >        checkCompletedWithWrappedCFException(f, ex);
3459 >        checkCompletedWithWrappedCFException(g, ex);
3460          assertEquals(a.get(), 1);
3461 +        }
3462      }
3463  
3464      /**
3465       * If a whenComplete action throws an exception when triggered by
3466       * a normal completion, it completes exceptionally
3467       */
3468 <    public void testWhenComplete3() {
3469 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3470 <        CompletableFuture<Integer> g =
3085 <            f.whenComplete((Integer x, Throwable t) ->
3086 <                           { throw new CFException(); } );
3087 <        f.complete(three);
3088 <        checkCompletedNormally(f, three);
3089 <        assertTrue(g.isCompletedExceptionally());
3090 <        checkCompletedWithWrappedCFException(g);
3091 <    }
3092 <
3093 <    /**
3094 <     * whenCompleteAsync action executes on normal completion, propagating
3095 <     * source result.
3096 <     */
3097 <    public void testWhenCompleteAsync1() {
3098 <        final AtomicInteger a = new AtomicInteger();
3099 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3100 <        CompletableFuture<Integer> g =
3101 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3102 <        f.complete(three);
3103 <        checkCompletedNormally(f, three);
3104 <        checkCompletedNormally(g, three);
3105 <        assertEquals(a.get(), 1);
3106 <    }
3107 <
3108 <    /**
3109 <     * whenCompleteAsync action executes on exceptional completion, propagating
3110 <     * source result.
3111 <     */
3112 <    public void testWhenCompleteAsync2() {
3113 <        final AtomicInteger a = new AtomicInteger();
3114 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3115 <        CompletableFuture<Integer> g =
3116 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3117 <        f.completeExceptionally(new CFException());
3118 <        checkCompletedWithWrappedCFException(f);
3119 <        checkCompletedWithWrappedCFException(g);
3120 <    }
3121 <
3122 <    /**
3123 <     * If a whenCompleteAsync action throws an exception when
3124 <     * triggered by a normal completion, it completes exceptionally
3125 <     */
3126 <    public void testWhenCompleteAsync3() {
3127 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3128 <        CompletableFuture<Integer> g =
3129 <            f.whenCompleteAsync((Integer x, Throwable t) ->
3130 <                           { throw new CFException(); } );
3131 <        f.complete(three);
3132 <        checkCompletedNormally(f, three);
3133 <        checkCompletedWithWrappedCFException(g);
3134 <    }
3468 >    public void testWhenComplete_actionFailed() {
3469 >        for (ExecutionMode m : ExecutionMode.values())
3470 >        for (Integer v1 : new Integer[] { 1, null }) {
3471  
3472 <    /**
3473 <     * whenCompleteAsync action executes on normal completion, propagating
3474 <     * source result.
3475 <     */
3476 <    public void testWhenCompleteAsync1e() {
3477 <        final AtomicInteger a = new AtomicInteger();
3478 <        ThreadExecutor exec = new ThreadExecutor();
3479 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3480 <        CompletableFuture<Integer> g =
3481 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3482 <                                exec);
3483 <        f.complete(three);
3484 <        checkCompletedNormally(f, three);
3149 <        checkCompletedNormally(g, three);
3150 <        assertEquals(a.get(), 1);
3472 >        final CFException ex = new CFException();
3473 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3474 >        final CompletableFuture<Integer> g = m.whenComplete
3475 >            (f,
3476 >             (Integer x, Throwable t) -> {
3477 >                threadAssertSame(x, v1);
3478 >                threadAssertNull(t);
3479 >                throw ex;
3480 >            });
3481 >        f.complete(v1);
3482 >        checkCompletedNormally(f, v1);
3483 >        checkCompletedWithWrappedCFException(g, ex);
3484 >        }
3485      }
3486  
3487      /**
3488 <     * whenCompleteAsync action executes on exceptional completion, propagating
3489 <     * source result.
3488 >     * If a whenComplete action throws an exception when triggered by
3489 >     * a source completion that also throws an exception, the source
3490 >     * exception takes precedence.
3491       */
3492 <    public void testWhenCompleteAsync2e() {
3493 <        final AtomicInteger a = new AtomicInteger();
3494 <        ThreadExecutor exec = new ThreadExecutor();
3160 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3161 <        CompletableFuture<Integer> g =
3162 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3163 <                                exec);
3164 <        f.completeExceptionally(new CFException());
3165 <        checkCompletedWithWrappedCFException(f);
3166 <        checkCompletedWithWrappedCFException(g);
3167 <    }
3492 >    public void testWhenComplete_actionFailedSourceFailed() {
3493 >        for (ExecutionMode m : ExecutionMode.values())
3494 >        for (Integer v1 : new Integer[] { 1, null }) {
3495  
3496 <    /**
3497 <     * If a whenCompleteAsync action throws an exception when triggered
3498 <     * by a normal completion, it completes exceptionally
3499 <     */
3500 <    public void testWhenCompleteAsync3e() {
3501 <        ThreadExecutor exec = new ThreadExecutor();
3502 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3503 <        CompletableFuture<Integer> g =
3504 <            f.whenCompleteAsync((Integer x, Throwable t) ->
3505 <                                { throw new CFException(); },
3506 <                                exec);
3507 <        f.complete(three);
3508 <        checkCompletedNormally(f, three);
3509 <        checkCompletedWithWrappedCFException(g);
3496 >        final CFException ex1 = new CFException();
3497 >        final CFException ex2 = new CFException();
3498 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3499 >        final CompletableFuture<Integer> g = m.whenComplete
3500 >            (f,
3501 >             (Integer x, Throwable t) -> {
3502 >                threadAssertSame(t, ex1);
3503 >                threadAssertNull(x);
3504 >                throw ex2;
3505 >            });
3506 >        f.completeExceptionally(ex1);
3507 >        checkCompletedWithWrappedCFException(f, ex1);
3508 >        checkCompletedWithWrappedCFException(g, ex1);
3509 >        }
3510      }
3511  
3512      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines