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.46 by jsr166, Mon Jun 2 17:41:22 2014 UTC vs.
Revision 1.57 by jsr166, Mon Jun 2 22:48:50 2014 UTC

# Line 17 | Line 17 | import java.util.concurrent.Future;
17   import java.util.concurrent.CompletableFuture;
18   import java.util.concurrent.CompletionException;
19   import java.util.concurrent.CompletionStage;
20 + import java.util.concurrent.ForkJoinPool;
21 + import java.util.concurrent.ForkJoinTask;
22   import java.util.concurrent.TimeoutException;
23   import java.util.concurrent.atomic.AtomicInteger;
24   import static java.util.concurrent.TimeUnit.MILLISECONDS;
# Line 282 | Line 284 | public class CompletableFutureTest exten
284      public void testGetNumberOfDependents() {
285          CompletableFuture<Integer> f = new CompletableFuture<>();
286          assertEquals(0, f.getNumberOfDependents());
287 <        CompletableFuture g = f.thenRun(new Noop());
287 >        CompletableFuture g = f.thenRun(new Noop(ExecutionMode.DEFAULT));
288          assertEquals(1, f.getNumberOfDependents());
289          assertEquals(0, g.getNumberOfDependents());
290 <        CompletableFuture h = f.thenRun(new Noop());
290 >        CompletableFuture h = f.thenRun(new Noop(ExecutionMode.DEFAULT));
291          assertEquals(2, f.getNumberOfDependents());
292          f.complete(1);
293          checkCompletedNormally(g, null);
# Line 347 | Line 349 | public class CompletableFutureTest exten
349          }
350      }
351      static final class IncFunction implements Function<Integer,Integer> {
352 +        final ExecutionMode m;
353          int invocationCount = 0;
354          Integer value;
355 +        IncFunction(ExecutionMode m) { this.m = m; }
356          public Integer apply(Integer x) {
357 +            m.checkExecutionMode();
358              invocationCount++;
359              return value = inc(x);
360          }
361      }
362      static final class SubtractAction implements BiConsumer<Integer, Integer> {
363 +        final ExecutionMode m;
364          int invocationCount = 0;
365          Integer value;
366          // Check this action was invoked exactly once when result is computed.
367 +        SubtractAction(ExecutionMode m) { this.m = m; }
368          public void accept(Integer x, Integer y) {
369 +            m.checkExecutionMode();
370              invocationCount++;
371              value = subtract(x, y);
372          }
373      }
374      static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> {
375 +        final ExecutionMode m;
376          int invocationCount = 0;
377          Integer value;
378          // Check this action was invoked exactly once when result is computed.
379 +        SubtractFunction(ExecutionMode m) { this.m = m; }
380          public Integer apply(Integer x, Integer y) {
381 +            m.checkExecutionMode();
382              invocationCount++;
383              return value = subtract(x, y);
384          }
385      }
386      static final class Noop implements Runnable {
387 +        final ExecutionMode m;
388          int invocationCount = 0;
389 +        Noop(ExecutionMode m) { this.m = m; }
390          public void run() {
391 +            m.checkExecutionMode();
392              invocationCount++;
393          }
394      }
395  
396      static final class FailingSupplier implements Supplier<Integer> {
397 +        final ExecutionMode m;
398          int invocationCount = 0;
399 +        FailingSupplier(ExecutionMode m) { this.m = m; }
400          public Integer get() {
401 +            m.checkExecutionMode();
402              invocationCount++;
403              throw new CFException();
404          }
405      }
406      static final class FailingConsumer implements Consumer<Integer> {
407 +        final ExecutionMode m;
408          int invocationCount = 0;
409 +        FailingConsumer(ExecutionMode m) { this.m = m; }
410          public void accept(Integer x) {
411 +            m.checkExecutionMode();
412              invocationCount++;
413              throw new CFException();
414          }
415      }
416      static final class FailingBiConsumer implements BiConsumer<Integer, Integer> {
417 +        final ExecutionMode m;
418          int invocationCount = 0;
419 +        FailingBiConsumer(ExecutionMode m) { this.m = m; }
420          public void accept(Integer x, Integer y) {
421 +            m.checkExecutionMode();
422              invocationCount++;
423              throw new CFException();
424          }
425      }
426      static final class FailingFunction implements Function<Integer, Integer> {
427 +        final ExecutionMode m;
428          int invocationCount = 0;
429 +        FailingFunction(ExecutionMode m) { this.m = m; }
430          public Integer apply(Integer x) {
431 +            m.checkExecutionMode();
432              invocationCount++;
433              throw new CFException();
434          }
435      }
436      static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> {
437 +        final ExecutionMode m;
438          int invocationCount = 0;
439 +        FailingBiFunction(ExecutionMode m) { this.m = m; }
440          public Integer apply(Integer x, Integer y) {
441 +            m.checkExecutionMode();
442              invocationCount++;
443              throw new CFException();
444          }
445      }
446 <    static final class FailingNoop implements Runnable {
446 >    static final class FailingRunnable implements Runnable {
447 >        final ExecutionMode m;
448          int invocationCount = 0;
449 +        FailingRunnable(ExecutionMode m) { this.m = m; }
450          public void run() {
451 +            m.checkExecutionMode();
452              invocationCount++;
453              throw new CFException();
454          }
# Line 424 | Line 456 | public class CompletableFutureTest exten
456  
457      static final class CompletableFutureInc
458          implements Function<Integer, CompletableFuture<Integer>> {
459 +        final ExecutionMode m;
460          int invocationCount = 0;
461 +        CompletableFutureInc(ExecutionMode m) { this.m = m; }
462          public CompletableFuture<Integer> apply(Integer x) {
463 +            m.checkExecutionMode();
464              invocationCount++;
465              CompletableFuture<Integer> f = new CompletableFuture<>();
466              f.complete(inc(x));
# Line 435 | Line 470 | public class CompletableFutureTest exten
470  
471      static final class FailingCompletableFutureFunction
472          implements Function<Integer, CompletableFuture<Integer>> {
473 +        final ExecutionMode m;
474          int invocationCount = 0;
475 +        FailingCompletableFutureFunction(ExecutionMode m) { this.m = m; }
476          public CompletableFuture<Integer> apply(Integer x) {
477 +            m.checkExecutionMode();
478              invocationCount++;
479              throw new CFException();
480          }
# Line 444 | Line 482 | public class CompletableFutureTest exten
482  
483      // Used for explicit executor tests
484      static final class ThreadExecutor implements Executor {
485 <        AtomicInteger count = new AtomicInteger(0);
485 >        final AtomicInteger count = new AtomicInteger(0);
486 >        static final ThreadGroup tg = new ThreadGroup("ThreadExecutor");
487 >        static boolean startedCurrentThread() {
488 >            return Thread.currentThread().getThreadGroup() == tg;
489 >        }
490  
491          public void execute(Runnable r) {
492              count.getAndIncrement();
493 <            new Thread(r).start();
493 >            new Thread(tg, r).start();
494          }
495      }
496  
# Line 458 | Line 500 | public class CompletableFutureTest exten
500       */
501      enum ExecutionMode {
502          DEFAULT {
503 +            public void checkExecutionMode() {
504 +                assertNull(ForkJoinTask.getPool());
505 +            }
506 +            public CompletableFuture<Void> runAsync(Runnable a) {
507 +                throw new UnsupportedOperationException();
508 +            }
509              public <T> CompletableFuture<Void> thenRun
510                  (CompletableFuture<T> f, Runnable a) {
511                  return f.thenRun(a);
# Line 521 | Line 569 | public class CompletableFutureTest exten
569              }
570          },
571  
572 <        DEFAULT_ASYNC {
572 >        ASYNC {
573 >            public void checkExecutionMode() {
574 >                assertSame(ForkJoinPool.commonPool(),
575 >                           ForkJoinTask.getPool());
576 >            }
577 >            public CompletableFuture<Void> runAsync(Runnable a) {
578 >                return CompletableFuture.runAsync(a);
579 >            }
580              public <T> CompletableFuture<Void> thenRun
581                  (CompletableFuture<T> f, Runnable a) {
582                  return f.thenRunAsync(a);
# Line 586 | Line 641 | public class CompletableFutureTest exten
641          },
642  
643          EXECUTOR {
644 +            public void checkExecutionMode() {
645 +                assertTrue(ThreadExecutor.startedCurrentThread());
646 +            }
647 +            public CompletableFuture<Void> runAsync(Runnable a) {
648 +                return CompletableFuture.runAsync(a, new ThreadExecutor());
649 +            }
650              public <T> CompletableFuture<Void> thenRun
651                  (CompletableFuture<T> f, Runnable a) {
652                  return f.thenRunAsync(a, new ThreadExecutor());
# Line 649 | Line 710 | public class CompletableFutureTest exten
710              }
711          };
712  
713 +        public abstract void checkExecutionMode();
714 +        public abstract CompletableFuture<Void> runAsync(Runnable a);
715          public abstract <T> CompletableFuture<Void> thenRun
716              (CompletableFuture<T> f, Runnable a);
717          public abstract <T> CompletableFuture<Void> thenAccept
# Line 727 | Line 790 | public class CompletableFutureTest exten
790          if (!createIncomplete) f.completeExceptionally(ex);
791          final CompletableFuture<Integer> g = f.exceptionally
792              ((Throwable t) -> {
793 +                ExecutionMode.DEFAULT.checkExecutionMode();
794                  threadAssertSame(t, ex);
795                  a.getAndIncrement();
796                  return v1;
# Line 748 | Line 812 | public class CompletableFutureTest exten
812          if (!createIncomplete) f.completeExceptionally(ex1);
813          final CompletableFuture<Integer> g = f.exceptionally
814              ((Throwable t) -> {
815 +                ExecutionMode.DEFAULT.checkExecutionMode();
816                  threadAssertSame(t, ex1);
817                  a.getAndIncrement();
818                  throw ex2;
# Line 773 | Line 838 | public class CompletableFutureTest exten
838          final CompletableFuture<Integer> g = m.handle
839              (f,
840               (Integer x, Throwable t) -> {
841 +                m.checkExecutionMode();
842                  threadAssertSame(x, v1);
843                  threadAssertNull(t);
844                  a.getAndIncrement();
# Line 801 | Line 867 | public class CompletableFutureTest exten
867          final CompletableFuture<Integer> g = m.handle
868              (f,
869               (Integer x, Throwable t) -> {
870 +                m.checkExecutionMode();
871                  threadAssertNull(x);
872                  threadAssertSame(t, ex);
873                  a.getAndIncrement();
# Line 829 | Line 896 | public class CompletableFutureTest exten
896          final CompletableFuture<Integer> g = m.handle
897              (f,
898               (Integer x, Throwable t) -> {
899 +                m.checkExecutionMode();
900                  threadAssertNull(x);
901                  threadAssertTrue(t instanceof CancellationException);
902                  a.getAndIncrement();
# Line 856 | Line 924 | public class CompletableFutureTest exten
924          final CompletableFuture<Integer> g = m.handle
925              (f,
926               (Integer x, Throwable t) -> {
927 +                m.checkExecutionMode();
928                  threadAssertNull(x);
929                  threadAssertSame(ex1, t);
930                  a.getAndIncrement();
# Line 880 | Line 949 | public class CompletableFutureTest exten
949          final CompletableFuture<Integer> g = m.handle
950              (f,
951               (Integer x, Throwable t) -> {
952 +                m.checkExecutionMode();
953                  threadAssertSame(x, v1);
954                  threadAssertNull(t);
955                  a.getAndIncrement();
# Line 895 | Line 965 | public class CompletableFutureTest exten
965      /**
966       * runAsync completes after running Runnable
967       */
968 <    public void testRunAsync() {
969 <        Noop r = new Noop();
970 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
968 >    public void testRunAsync_normalCompletion() {
969 >        ExecutionMode[] executionModes = {
970 >            ExecutionMode.ASYNC,
971 >            ExecutionMode.EXECUTOR,
972 >        };
973 >        for (ExecutionMode m : executionModes)
974 >    {
975 >        final Noop r = new Noop(m);
976 >        final CompletableFuture<Void> f = m.runAsync(r);
977          assertNull(f.join());
902        assertEquals(1, r.invocationCount);
978          checkCompletedNormally(f, null);
904    }
905
906    /**
907     * runAsync with executor completes after running Runnable
908     */
909    public void testRunAsync2() {
910        Noop r = new Noop();
911        ThreadExecutor exec = new ThreadExecutor();
912        CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec);
913        assertNull(f.join());
979          assertEquals(1, r.invocationCount);
980 <        checkCompletedNormally(f, null);
916 <        assertEquals(1, exec.count.get());
917 <    }
980 >    }}
981  
982      /**
983       * failing runAsync completes exceptionally after running Runnable
984       */
985 <    public void testRunAsync3() {
986 <        FailingNoop r = new FailingNoop();
987 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
985 >    public void testRunAsync_exceptionalCompletion() {
986 >        ExecutionMode[] executionModes = {
987 >            ExecutionMode.ASYNC,
988 >            ExecutionMode.EXECUTOR,
989 >        };
990 >        for (ExecutionMode m : executionModes)
991 >    {
992 >        final FailingRunnable r = new FailingRunnable(m);
993 >        final CompletableFuture<Void> f = m.runAsync(r);
994          checkCompletedWithWrappedCFException(f);
995          assertEquals(1, r.invocationCount);
996 <    }
996 >    }}
997  
998      /**
999       * supplyAsync completes with result of supplier
# Line 950 | Line 1019 | public class CompletableFutureTest exten
1019       * Failing supplyAsync completes exceptionally
1020       */
1021      public void testSupplyAsync3() {
1022 <        FailingSupplier r = new FailingSupplier();
1022 >        FailingSupplier r = new FailingSupplier(ExecutionMode.ASYNC);
1023          CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r);
1024          checkCompletedWithWrappedCFException(f);
1025          assertEquals(1, r.invocationCount);
# Line 961 | Line 1030 | public class CompletableFutureTest exten
1030      /**
1031       * thenRun result completes normally after normal completion of source
1032       */
1033 <    public void testThenRun() {
1034 <        CompletableFuture<Integer> f;
1035 <        CompletableFuture<Void> g;
1036 <        Noop r;
1037 <
1038 <        f = new CompletableFuture<>();
1039 <        g = f.thenRun(r = new Noop());
1040 <        f.complete(null);
1041 <        checkCompletedNormally(g, null);
1042 <        assertEquals(1, r.invocationCount);
1033 >    public void testThenRun_normalCompletion() {
1034 >        for (ExecutionMode m : ExecutionMode.values())
1035 >        for (boolean createIncomplete : new boolean[] { true, false })
1036 >        for (Integer v1 : new Integer[] { 1, null })
1037 >    {
1038 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1039 >        final Noop r = new Noop(m);
1040 >        if (!createIncomplete) f.complete(v1);
1041 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1042 >        if (createIncomplete) {
1043 >            checkIncomplete(g);
1044 >            f.complete(v1);
1045 >        }
1046  
975        f = new CompletableFuture<>();
976        f.complete(null);
977        g = f.thenRun(r = new Noop());
1047          checkCompletedNormally(g, null);
1048 +        checkCompletedNormally(f, v1);
1049          assertEquals(1, r.invocationCount);
1050 <    }
1050 >    }}
1051  
1052      /**
1053       * thenRun result completes exceptionally after exceptional
1054       * completion of source
1055       */
1056 <    public void testThenRun2() {
1057 <        CompletableFuture<Integer> f;
1058 <        CompletableFuture<Void> g;
1059 <        Noop r;
1060 <
1061 <        f = new CompletableFuture<>();
1062 <        g = f.thenRun(r = new Noop());
1063 <        f.completeExceptionally(new CFException());
1064 <        checkCompletedWithWrappedCFException(g);
1065 <        assertEquals(0, r.invocationCount);
1056 >    public void testThenRun_exceptionalCompletion() {
1057 >        for (ExecutionMode m : ExecutionMode.values())
1058 >        for (boolean createIncomplete : new boolean[] { true, false })
1059 >    {
1060 >        final CFException ex = new CFException();
1061 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1062 >        final Noop r = new Noop(m);
1063 >        if (!createIncomplete) f.completeExceptionally(ex);
1064 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1065 >        if (createIncomplete) {
1066 >            checkIncomplete(g);
1067 >            f.completeExceptionally(ex);
1068 >        }
1069  
1070 <        f = new CompletableFuture<>();
1071 <        f.completeExceptionally(new CFException());
999 <        g = f.thenRun(r = new Noop());
1000 <        checkCompletedWithWrappedCFException(g);
1070 >        checkCompletedWithWrappedCFException(g, ex);
1071 >        checkCompletedWithWrappedCFException(f, ex);
1072          assertEquals(0, r.invocationCount);
1073 <    }
1073 >    }}
1074  
1075      /**
1076 <     * thenRun result completes exceptionally if action does
1076 >     * thenRun result completes exceptionally if source cancelled
1077       */
1078 <    public void testThenRun3() {
1079 <        CompletableFuture<Integer> f;
1080 <        CompletableFuture<Void> g;
1081 <        FailingNoop r;
1082 <
1083 <        f = new CompletableFuture<>();
1084 <        g = f.thenRun(r = new FailingNoop());
1085 <        f.complete(null);
1086 <        checkCompletedWithWrappedCFException(g);
1078 >    public void testThenRun_sourceCancelled() {
1079 >        for (ExecutionMode m : ExecutionMode.values())
1080 >        for (boolean createIncomplete : new boolean[] { true, false })
1081 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1082 >    {
1083 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1084 >        final Noop r = new Noop(m);
1085 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1086 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1087 >        if (createIncomplete) {
1088 >            checkIncomplete(g);
1089 >            assertTrue(f.cancel(mayInterruptIfRunning));
1090 >        }
1091  
1092 <        f = new CompletableFuture<>();
1093 <        f.complete(null);
1094 <        g = f.thenRun(r = new FailingNoop());
1095 <        checkCompletedWithWrappedCFException(g);
1021 <    }
1092 >        checkCompletedWithWrappedCancellationException(g);
1093 >        checkCancelled(f);
1094 >        assertEquals(0, r.invocationCount);
1095 >    }}
1096  
1097      /**
1098 <     * thenRun result completes exceptionally if source cancelled
1098 >     * thenRun result completes exceptionally if action does
1099       */
1100 <    public void testThenRun4() {
1101 <        CompletableFuture<Integer> f;
1102 <        CompletableFuture<Void> g;
1103 <        Noop r;
1104 <
1105 <        f = new CompletableFuture<>();
1106 <        g = f.thenRun(r = new Noop());
1107 <        assertTrue(f.cancel(true));
1108 <        checkCompletedWithWrappedCancellationException(g);
1100 >    public void testThenRun_actionFailed() {
1101 >        for (ExecutionMode m : ExecutionMode.values())
1102 >        for (boolean createIncomplete : new boolean[] { true, false })
1103 >        for (Integer v1 : new Integer[] { 1, null })
1104 >    {
1105 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1106 >        final FailingRunnable r = new FailingRunnable(m);
1107 >        if (!createIncomplete) f.complete(v1);
1108 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1109 >        if (createIncomplete) {
1110 >            checkIncomplete(g);
1111 >            f.complete(v1);
1112 >        }
1113  
1114 <        f = new CompletableFuture<>();
1115 <        assertTrue(f.cancel(true));
1116 <        g = f.thenRun(r = new Noop());
1039 <        checkCompletedWithWrappedCancellationException(g);
1040 <    }
1114 >        checkCompletedWithWrappedCFException(g);
1115 >        checkCompletedNormally(f, v1);
1116 >    }}
1117  
1118      /**
1119       * thenApply result completes normally after normal completion of source
1120       */
1121 <    public void testThenApply() {
1122 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1123 <        CompletableFuture<Integer> g = f.thenApply(inc);
1124 <        f.complete(one);
1125 <        checkCompletedNormally(g, two);
1126 <    }
1121 >    public void testThenApply_normalCompletion() {
1122 >        for (ExecutionMode m : ExecutionMode.values())
1123 >        for (boolean createIncomplete : new boolean[] { true, false })
1124 >        for (Integer v1 : new Integer[] { 1, null })
1125 >    {
1126 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1127 >        final IncFunction r = new IncFunction(m);
1128 >        if (!createIncomplete) f.complete(v1);
1129 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1130 >        if (createIncomplete) {
1131 >            checkIncomplete(g);
1132 >            f.complete(v1);
1133 >        }
1134 >
1135 >        checkCompletedNormally(g, inc(v1));
1136 >        checkCompletedNormally(f, v1);
1137 >        assertEquals(1, r.invocationCount);
1138 >    }}
1139  
1140      /**
1141       * thenApply result completes exceptionally after exceptional
1142       * completion of source
1143       */
1144 <    public void testThenApply2() {
1145 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1146 <        CompletableFuture<Integer> g = f.thenApply(inc);
1147 <        f.completeExceptionally(new CFException());
1148 <        checkCompletedWithWrappedCFException(g);
1149 <    }
1144 >    public void testThenApply_exceptionalCompletion() {
1145 >        for (ExecutionMode m : ExecutionMode.values())
1146 >        for (boolean createIncomplete : new boolean[] { true, false })
1147 >    {
1148 >        final CFException ex = new CFException();
1149 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1150 >        final IncFunction r = new IncFunction(m);
1151 >        if (!createIncomplete) f.completeExceptionally(ex);
1152 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1153 >        if (createIncomplete) {
1154 >            checkIncomplete(g);
1155 >            f.completeExceptionally(ex);
1156 >        }
1157  
1158 <    /**
1159 <     * thenApply result completes exceptionally if action does
1160 <     */
1161 <    public void testThenApply3() {
1067 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1068 <        CompletableFuture<Integer> g = f.thenApply(new FailingFunction());
1069 <        f.complete(one);
1070 <        checkCompletedWithWrappedCFException(g);
1071 <    }
1158 >        checkCompletedWithWrappedCFException(g, ex);
1159 >        checkCompletedWithWrappedCFException(f, ex);
1160 >        assertEquals(0, r.invocationCount);
1161 >    }}
1162  
1163      /**
1164       * thenApply result completes exceptionally if source cancelled
1165       */
1166 <    public void testThenApply4() {
1167 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1168 <        CompletableFuture<Integer> g = f.thenApply(inc);
1169 <        assertTrue(f.cancel(true));
1166 >    public void testThenApply_sourceCancelled() {
1167 >        for (ExecutionMode m : ExecutionMode.values())
1168 >        for (boolean createIncomplete : new boolean[] { true, false })
1169 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1170 >    {
1171 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1172 >        final IncFunction r = new IncFunction(m);
1173 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1174 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1175 >        if (createIncomplete) {
1176 >            checkIncomplete(g);
1177 >            assertTrue(f.cancel(mayInterruptIfRunning));
1178 >        }
1179 >
1180          checkCompletedWithWrappedCancellationException(g);
1181 <    }
1181 >        checkCancelled(f);
1182 >        assertEquals(0, r.invocationCount);
1183 >    }}
1184 >
1185 >    /**
1186 >     * thenApply result completes exceptionally if action does
1187 >     */
1188 >    public void testThenApply_actionFailed() {
1189 >        for (ExecutionMode m : ExecutionMode.values())
1190 >        for (boolean createIncomplete : new boolean[] { true, false })
1191 >        for (Integer v1 : new Integer[] { 1, null })
1192 >    {
1193 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1194 >        final FailingFunction r = new FailingFunction(m);
1195 >        if (!createIncomplete) f.complete(v1);
1196 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1197 >        if (createIncomplete) {
1198 >            checkIncomplete(g);
1199 >            f.complete(v1);
1200 >        }
1201 >
1202 >        checkCompletedWithWrappedCFException(g);
1203 >        checkCompletedNormally(f, v1);
1204 >    }}
1205  
1206      /**
1207       * thenAccept result completes normally after normal completion of source
1208       */
1209 <    public void testThenAccept() {
1210 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1211 <        IncAction r = new IncAction();
1212 <        CompletableFuture<Void> g = f.thenAccept(r);
1213 <        f.complete(one);
1209 >    public void testThenAccept_normalCompletion() {
1210 >        for (ExecutionMode m : ExecutionMode.values())
1211 >        for (boolean createIncomplete : new boolean[] { true, false })
1212 >        for (Integer v1 : new Integer[] { 1, null })
1213 >    {
1214 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1215 >        final IncAction r = new IncAction();
1216 >        if (!createIncomplete) f.complete(v1);
1217 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1218 >        if (createIncomplete) {
1219 >            checkIncomplete(g);
1220 >            f.complete(v1);
1221 >        }
1222 >
1223          checkCompletedNormally(g, null);
1224 <        assertEquals(r.value, (Integer) 2);
1225 <    }
1224 >        checkCompletedNormally(f, v1);
1225 >        assertEquals(1, r.invocationCount);
1226 >        assertEquals(inc(v1), r.value);
1227 >    }}
1228  
1229      /**
1230       * thenAccept result completes exceptionally after exceptional
1231       * completion of source
1232       */
1233 <    public void testThenAccept2() {
1234 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1235 <        IncAction r = new IncAction();
1236 <        CompletableFuture<Void> g = f.thenAccept(r);
1237 <        f.completeExceptionally(new CFException());
1238 <        checkCompletedWithWrappedCFException(g);
1239 <    }
1233 >    public void testThenAccept_exceptionalCompletion() {
1234 >        for (ExecutionMode m : ExecutionMode.values())
1235 >        for (boolean createIncomplete : new boolean[] { true, false })
1236 >    {
1237 >        final CFException ex = new CFException();
1238 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1239 >        final IncAction r = new IncAction();
1240 >        if (!createIncomplete) f.completeExceptionally(ex);
1241 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1242 >        if (createIncomplete) {
1243 >            checkIncomplete(g);
1244 >            f.completeExceptionally(ex);
1245 >        }
1246 >
1247 >        checkCompletedWithWrappedCFException(g, ex);
1248 >        checkCompletedWithWrappedCFException(f, ex);
1249 >        assertEquals(0, r.invocationCount);
1250 >    }}
1251  
1252      /**
1253       * thenAccept result completes exceptionally if action does
1254       */
1255 <    public void testThenAccept3() {
1256 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1257 <        FailingConsumer r = new FailingConsumer();
1258 <        CompletableFuture<Void> g = f.thenAccept(r);
1259 <        f.complete(one);
1255 >    public void testThenAccept_actionFailed() {
1256 >        for (ExecutionMode m : ExecutionMode.values())
1257 >        for (boolean createIncomplete : new boolean[] { true, false })
1258 >        for (Integer v1 : new Integer[] { 1, null })
1259 >    {
1260 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1261 >        final FailingConsumer r = new FailingConsumer(m);
1262 >        if (!createIncomplete) f.complete(v1);
1263 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1264 >        if (createIncomplete) {
1265 >            checkIncomplete(g);
1266 >            f.complete(v1);
1267 >        }
1268 >
1269          checkCompletedWithWrappedCFException(g);
1270 <        assertEquals(1, r.invocationCount);
1271 <    }
1270 >        checkCompletedNormally(f, v1);
1271 >    }}
1272  
1273      /**
1274       * thenAccept result completes exceptionally if source cancelled
1275       */
1276 <    public void testThenAccept4() {
1277 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1278 <        IncAction r = new IncAction();
1279 <        CompletableFuture<Void> g = f.thenAccept(r);
1280 <        assertTrue(f.cancel(true));
1276 >    public void testThenAccept_sourceCancelled() {
1277 >        for (ExecutionMode m : ExecutionMode.values())
1278 >        for (boolean createIncomplete : new boolean[] { true, false })
1279 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1280 >    {
1281 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1282 >        final IncAction r = new IncAction();
1283 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1284 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1285 >        if (createIncomplete) {
1286 >            checkIncomplete(g);
1287 >            assertTrue(f.cancel(mayInterruptIfRunning));
1288 >        }
1289 >
1290          checkCompletedWithWrappedCancellationException(g);
1291 <    }
1291 >        checkCancelled(f);
1292 >        assertEquals(0, r.invocationCount);
1293 >    }}
1294  
1295      /**
1296       * thenCombine result completes normally after normal completion
1297       * of sources
1298       */
1299 <    public void testThenCombine_normalCompletion1() {
1299 >    public void testThenCombine_normalCompletion() {
1300 >        for (ExecutionMode m : ExecutionMode.values())
1301          for (boolean createIncomplete : new boolean[] { true, false })
1302          for (boolean fFirst : new boolean[] { true, false })
1137        for (ExecutionMode m : ExecutionMode.values())
1303          for (Integer v1 : new Integer[] { 1, null })
1304 <        for (Integer v2 : new Integer[] { 2, null }) {
1305 <
1304 >        for (Integer v2 : new Integer[] { 2, null })
1305 >    {
1306          final CompletableFuture<Integer> f = new CompletableFuture<>();
1307          final CompletableFuture<Integer> g = new CompletableFuture<>();
1308 <        final SubtractFunction r = new SubtractFunction();
1144 <        CompletableFuture<Integer> h = null;
1145 <        if (createIncomplete) h = m.thenCombine(f, g, r);
1308 >        final SubtractFunction r = new SubtractFunction(m);
1309  
1310 <        if (fFirst)
1311 <            f.complete(v1);
1312 <        else
1313 <            g.complete(v2);
1314 <        if (createIncomplete) checkIncomplete(h);
1315 <        assertEquals(0, r.invocationCount);
1316 <        if (!fFirst)
1317 <            f.complete(v1);
1318 <        else
1156 <            g.complete(v2);
1157 <        if (!createIncomplete) h = m.thenCombine(f, g, r);
1310 >        if (fFirst) f.complete(v1); else g.complete(v2);
1311 >        if (!createIncomplete)
1312 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1313 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1314 >        if (createIncomplete) {
1315 >            checkIncomplete(h);
1316 >            assertEquals(0, r.invocationCount);
1317 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1318 >        }
1319  
1320          checkCompletedNormally(h, subtract(v1, v2));
1321          checkCompletedNormally(f, v1);
1322          checkCompletedNormally(g, v2);
1323          assertEquals(1, r.invocationCount);
1324 <        }
1164 <    }
1324 >    }}
1325  
1326      /**
1327       * thenCombine result completes exceptionally after exceptional
1328       * completion of either source
1329       */
1330 <    public void testThenCombine_exceptionalCompletion1() {
1171 <        for (ExecutionMode m : ExecutionMode.values())
1172 <        for (Integer v1 : new Integer[] { 1, null }) {
1173 <
1174 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1175 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1176 <        final SubtractFunction r = new SubtractFunction();
1177 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1178 <        final CFException ex = new CFException();
1179 <
1180 <        f.completeExceptionally(ex);
1181 <        checkIncomplete(h);
1182 <        g.complete(v1);
1183 <
1184 <        checkCompletedWithWrappedCFException(h, ex);
1185 <        checkCompletedWithWrappedCFException(f, ex);
1186 <        assertEquals(0, r.invocationCount);
1187 <        checkCompletedNormally(g, v1);
1188 <        }
1189 <    }
1190 <
1191 <    public void testThenCombine_exceptionalCompletion2() {
1330 >    public void testThenCombine_exceptionalCompletion() {
1331          for (ExecutionMode m : ExecutionMode.values())
1332 <        for (Integer v1 : new Integer[] { 1, null }) {
1333 <
1334 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1335 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1197 <        final SubtractFunction r = new SubtractFunction();
1198 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1199 <        final CFException ex = new CFException();
1200 <
1201 <        g.completeExceptionally(ex);
1202 <        checkIncomplete(h);
1203 <        f.complete(v1);
1204 <
1205 <        checkCompletedWithWrappedCFException(h, ex);
1206 <        checkCompletedWithWrappedCFException(g, ex);
1207 <        assertEquals(0, r.invocationCount);
1208 <        checkCompletedNormally(f, v1);
1209 <        }
1210 <    }
1211 <
1212 <    public void testThenCombine_exceptionalCompletion3() {
1213 <        for (ExecutionMode m : ExecutionMode.values())
1214 <        for (Integer v1 : new Integer[] { 1, null }) {
1215 <
1332 >        for (boolean createIncomplete : new boolean[] { true, false })
1333 >        for (boolean fFirst : new boolean[] { true, false })
1334 >        for (Integer v1 : new Integer[] { 1, null })
1335 >    {
1336          final CompletableFuture<Integer> f = new CompletableFuture<>();
1337          final CompletableFuture<Integer> g = new CompletableFuture<>();
1218        final SubtractFunction r = new SubtractFunction();
1338          final CFException ex = new CFException();
1339 +        final SubtractFunction r = new SubtractFunction(m);
1340  
1341 <        g.completeExceptionally(ex);
1342 <        f.complete(v1);
1341 >        (fFirst ? f : g).complete(v1);
1342 >        if (!createIncomplete)
1343 >            (!fFirst ? f : g).completeExceptionally(ex);
1344          final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1345 <
1346 <        checkCompletedWithWrappedCFException(h, ex);
1347 <        checkCompletedWithWrappedCFException(g, ex);
1227 <        assertEquals(0, r.invocationCount);
1228 <        checkCompletedNormally(f, v1);
1345 >        if (createIncomplete) {
1346 >            checkIncomplete(h);
1347 >            (!fFirst ? f : g).completeExceptionally(ex);
1348          }
1230    }
1231
1232    public void testThenCombine_exceptionalCompletion4() {
1233        for (ExecutionMode m : ExecutionMode.values())
1234        for (Integer v1 : new Integer[] { 1, null }) {
1235
1236        final CompletableFuture<Integer> f = new CompletableFuture<>();
1237        final CompletableFuture<Integer> g = new CompletableFuture<>();
1238        final SubtractFunction r = new SubtractFunction();
1239        final CFException ex = new CFException();
1240
1241        f.completeExceptionally(ex);
1242        g.complete(v1);
1243        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1349  
1350          checkCompletedWithWrappedCFException(h, ex);
1246        checkCompletedWithWrappedCFException(f, ex);
1351          assertEquals(0, r.invocationCount);
1352 <        checkCompletedNormally(g, v1);
1353 <        }
1354 <    }
1352 >        checkCompletedNormally(fFirst ? f : g, v1);
1353 >        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1354 >    }}
1355  
1356      /**
1357       * thenCombine result completes exceptionally if action does
1358       */
1359 <    public void testThenCombine_actionFailed1() {
1359 >    public void testThenCombine_actionFailed() {
1360          for (ExecutionMode m : ExecutionMode.values())
1361 +        for (boolean fFirst : new boolean[] { true, false })
1362          for (Integer v1 : new Integer[] { 1, null })
1363 <        for (Integer v2 : new Integer[] { 2, null }) {
1364 <
1363 >        for (Integer v2 : new Integer[] { 2, null })
1364 >    {
1365          final CompletableFuture<Integer> f = new CompletableFuture<>();
1366          final CompletableFuture<Integer> g = new CompletableFuture<>();
1367 <        final FailingBiFunction r = new FailingBiFunction();
1367 >        final FailingBiFunction r = new FailingBiFunction(m);
1368          final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1369  
1370 <        f.complete(v1);
1371 <        checkIncomplete(h);
1372 <        g.complete(v2);
1373 <
1374 <        checkCompletedWithWrappedCFException(h);
1375 <        checkCompletedNormally(f, v1);
1271 <        checkCompletedNormally(g, v2);
1370 >        if (fFirst) {
1371 >            f.complete(v1);
1372 >            g.complete(v2);
1373 >        } else {
1374 >            g.complete(v2);
1375 >            f.complete(v1);
1376          }
1273    }
1274
1275    public void testThenCombine_actionFailed2() {
1276        for (ExecutionMode m : ExecutionMode.values())
1277        for (Integer v1 : new Integer[] { 1, null })
1278        for (Integer v2 : new Integer[] { 2, null }) {
1279
1280        final CompletableFuture<Integer> f = new CompletableFuture<>();
1281        final CompletableFuture<Integer> g = new CompletableFuture<>();
1282        final FailingBiFunction r = new FailingBiFunction();
1283        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1284
1285        g.complete(v2);
1286        checkIncomplete(h);
1287        f.complete(v1);
1377  
1378          checkCompletedWithWrappedCFException(h);
1379          checkCompletedNormally(f, v1);
1380          checkCompletedNormally(g, v2);
1381 <        }
1293 <    }
1381 >    }}
1382  
1383      /**
1384       * thenCombine result completes exceptionally if either source cancelled
1385       */
1386 <    public void testThenCombine_sourceCancelled1() {
1299 <        for (ExecutionMode m : ExecutionMode.values())
1300 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1301 <        for (Integer v1 : new Integer[] { 1, null }) {
1302 <
1303 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1304 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1305 <        final SubtractFunction r = new SubtractFunction();
1306 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1307 <
1308 <        assertTrue(f.cancel(mayInterruptIfRunning));
1309 <        checkIncomplete(h);
1310 <        g.complete(v1);
1311 <
1312 <        checkCompletedWithWrappedCancellationException(h);
1313 <        checkCancelled(f);
1314 <        assertEquals(0, r.invocationCount);
1315 <        checkCompletedNormally(g, v1);
1316 <        }
1317 <    }
1318 <
1319 <    public void testThenCombine_sourceCancelled2() {
1320 <        for (ExecutionMode m : ExecutionMode.values())
1321 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1322 <        for (Integer v1 : new Integer[] { 1, null }) {
1323 <
1324 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1325 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1326 <        final SubtractFunction r = new SubtractFunction();
1327 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1328 <
1329 <        assertTrue(g.cancel(mayInterruptIfRunning));
1330 <        checkIncomplete(h);
1331 <        f.complete(v1);
1332 <
1333 <        checkCompletedWithWrappedCancellationException(h);
1334 <        checkCancelled(g);
1335 <        assertEquals(0, r.invocationCount);
1336 <        checkCompletedNormally(f, v1);
1337 <        }
1338 <    }
1339 <
1340 <    public void testThenCombine_sourceCancelled3() {
1386 >    public void testThenCombine_sourceCancelled() {
1387          for (ExecutionMode m : ExecutionMode.values())
1388          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1389 <        for (Integer v1 : new Integer[] { 1, null }) {
1390 <
1389 >        for (boolean createIncomplete : new boolean[] { true, false })
1390 >        for (boolean fFirst : new boolean[] { true, false })
1391 >        for (Integer v1 : new Integer[] { 1, null })
1392 >    {
1393          final CompletableFuture<Integer> f = new CompletableFuture<>();
1394          final CompletableFuture<Integer> g = new CompletableFuture<>();
1395 <        final SubtractFunction r = new SubtractFunction();
1395 >        final SubtractFunction r = new SubtractFunction(m);
1396  
1397 <        assertTrue(g.cancel(mayInterruptIfRunning));
1398 <        f.complete(v1);
1397 >        (fFirst ? f : g).complete(v1);
1398 >        if (!createIncomplete)
1399 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1400          final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1401 <
1402 <        checkCompletedWithWrappedCancellationException(h);
1403 <        checkCancelled(g);
1355 <        assertEquals(0, r.invocationCount);
1356 <        checkCompletedNormally(f, v1);
1401 >        if (createIncomplete) {
1402 >            checkIncomplete(h);
1403 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1404          }
1358    }
1359
1360    public void testThenCombine_sourceCancelled4() {
1361        for (ExecutionMode m : ExecutionMode.values())
1362        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1363        for (Integer v1 : new Integer[] { 1, null }) {
1364
1365        final CompletableFuture<Integer> f = new CompletableFuture<>();
1366        final CompletableFuture<Integer> g = new CompletableFuture<>();
1367        final SubtractFunction r = new SubtractFunction();
1368
1369        assertTrue(f.cancel(mayInterruptIfRunning));
1370        g.complete(v1);
1371        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1405  
1406          checkCompletedWithWrappedCancellationException(h);
1407 <        checkCancelled(f);
1407 >        checkCancelled(!fFirst ? f : g);
1408          assertEquals(0, r.invocationCount);
1409 <        checkCompletedNormally(g, v1);
1410 <        }
1378 <    }
1409 >        checkCompletedNormally(fFirst ? f : g, v1);
1410 >    }}
1411  
1412      /**
1413       * thenAcceptBoth result completes normally after normal
1414       * completion of sources
1415       */
1416 <    public void testThenAcceptBoth_normalCompletion1() {
1416 >    public void testThenAcceptBoth_normalCompletion() {
1417          for (ExecutionMode m : ExecutionMode.values())
1418 +        for (boolean createIncomplete : new boolean[] { true, false })
1419 +        for (boolean fFirst : new boolean[] { true, false })
1420          for (Integer v1 : new Integer[] { 1, null })
1421 <        for (Integer v2 : new Integer[] { 2, null }) {
1422 <
1421 >        for (Integer v2 : new Integer[] { 2, null })
1422 >    {
1423          final CompletableFuture<Integer> f = new CompletableFuture<>();
1424          final CompletableFuture<Integer> g = new CompletableFuture<>();
1425 <        final SubtractAction r = new SubtractAction();
1392 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1393 <
1394 <        f.complete(v1);
1395 <        checkIncomplete(h);
1396 <        assertEquals(0, r.invocationCount);
1397 <        g.complete(v2);
1425 >        final SubtractAction r = new SubtractAction(m);
1426  
1427 <        checkCompletedNormally(h, null);
1428 <        assertEquals(subtract(v1, v2), r.value);
1429 <        checkCompletedNormally(f, v1);
1402 <        checkCompletedNormally(g, v2);
1403 <        }
1404 <    }
1405 <
1406 <    public void testThenAcceptBoth_normalCompletion2() {
1407 <        for (ExecutionMode m : ExecutionMode.values())
1408 <        for (Integer v1 : new Integer[] { 1, null })
1409 <        for (Integer v2 : new Integer[] { 2, null }) {
1410 <
1411 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1412 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1413 <        final SubtractAction r = new SubtractAction();
1427 >        if (fFirst) f.complete(v1); else g.complete(v2);
1428 >        if (!createIncomplete)
1429 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1430          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1431 <
1432 <        g.complete(v2);
1433 <        checkIncomplete(h);
1434 <        assertEquals(0, r.invocationCount);
1419 <        f.complete(v1);
1420 <
1421 <        checkCompletedNormally(h, null);
1422 <        assertEquals(subtract(v1, v2), r.value);
1423 <        checkCompletedNormally(f, v1);
1424 <        checkCompletedNormally(g, v2);
1431 >        if (createIncomplete) {
1432 >            checkIncomplete(h);
1433 >            assertEquals(0, r.invocationCount);
1434 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1435          }
1426    }
1427
1428    public void testThenAcceptBoth_normalCompletion3() {
1429        for (ExecutionMode m : ExecutionMode.values())
1430        for (Integer v1 : new Integer[] { 1, null })
1431        for (Integer v2 : new Integer[] { 2, null }) {
1432
1433        final CompletableFuture<Integer> f = new CompletableFuture<>();
1434        final CompletableFuture<Integer> g = new CompletableFuture<>();
1435        final SubtractAction r = new SubtractAction();
1436
1437        g.complete(v2);
1438        f.complete(v1);
1439        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1436  
1437          checkCompletedNormally(h, null);
1438          assertEquals(subtract(v1, v2), r.value);
1439          checkCompletedNormally(f, v1);
1440          checkCompletedNormally(g, v2);
1441 <        }
1446 <    }
1447 <
1448 <    public void testThenAcceptBoth_normalCompletion4() {
1449 <        for (ExecutionMode m : ExecutionMode.values())
1450 <        for (Integer v1 : new Integer[] { 1, null })
1451 <        for (Integer v2 : new Integer[] { 2, null }) {
1452 <
1453 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1454 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1455 <        final SubtractAction r = new SubtractAction();
1456 <
1457 <        f.complete(v1);
1458 <        g.complete(v2);
1459 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1460 <
1461 <        checkCompletedNormally(h, null);
1462 <        assertEquals(subtract(v1, v2), r.value);
1463 <        checkCompletedNormally(f, v1);
1464 <        checkCompletedNormally(g, v2);
1465 <        }
1466 <    }
1441 >    }}
1442  
1443      /**
1444       * thenAcceptBoth result completes exceptionally after exceptional
1445       * completion of either source
1446       */
1447 <    public void testThenAcceptBoth_exceptionalCompletion1() {
1447 >    public void testThenAcceptBoth_exceptionalCompletion() {
1448          for (ExecutionMode m : ExecutionMode.values())
1449 <        for (Integer v1 : new Integer[] { 1, null }) {
1450 <
1451 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1452 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1478 <        final SubtractAction r = new SubtractAction();
1479 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1480 <        final CFException ex = new CFException();
1481 <
1482 <        f.completeExceptionally(ex);
1483 <        checkIncomplete(h);
1484 <        g.complete(v1);
1485 <
1486 <        checkCompletedWithWrappedCFException(h, ex);
1487 <        checkCompletedWithWrappedCFException(f, ex);
1488 <        assertEquals(0, r.invocationCount);
1489 <        checkCompletedNormally(g, v1);
1490 <        }
1491 <    }
1492 <
1493 <    public void testThenAcceptBoth_exceptionalCompletion2() {
1494 <        for (ExecutionMode m : ExecutionMode.values())
1495 <        for (Integer v1 : new Integer[] { 1, null }) {
1496 <
1497 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1498 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1499 <        final SubtractAction r = new SubtractAction();
1500 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1501 <        final CFException ex = new CFException();
1502 <
1503 <        g.completeExceptionally(ex);
1504 <        checkIncomplete(h);
1505 <        f.complete(v1);
1506 <
1507 <        checkCompletedWithWrappedCFException(h, ex);
1508 <        checkCompletedWithWrappedCFException(g, ex);
1509 <        assertEquals(0, r.invocationCount);
1510 <        checkCompletedNormally(f, v1);
1511 <        }
1512 <    }
1513 <
1514 <    public void testThenAcceptBoth_exceptionalCompletion3() {
1515 <        for (ExecutionMode m : ExecutionMode.values())
1516 <        for (Integer v1 : new Integer[] { 1, null }) {
1517 <
1449 >        for (boolean createIncomplete : new boolean[] { true, false })
1450 >        for (boolean fFirst : new boolean[] { true, false })
1451 >        for (Integer v1 : new Integer[] { 1, null })
1452 >    {
1453          final CompletableFuture<Integer> f = new CompletableFuture<>();
1454          final CompletableFuture<Integer> g = new CompletableFuture<>();
1520        final SubtractAction r = new SubtractAction();
1455          final CFException ex = new CFException();
1456 +        final SubtractAction r = new SubtractAction(m);
1457  
1458 <        g.completeExceptionally(ex);
1459 <        f.complete(v1);
1458 >        (fFirst ? f : g).complete(v1);
1459 >        if (!createIncomplete)
1460 >            (!fFirst ? f : g).completeExceptionally(ex);
1461          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1462 <
1463 <        checkCompletedWithWrappedCFException(h, ex);
1464 <        checkCompletedWithWrappedCFException(g, ex);
1529 <        assertEquals(0, r.invocationCount);
1530 <        checkCompletedNormally(f, v1);
1462 >        if (createIncomplete) {
1463 >            checkIncomplete(h);
1464 >            (!fFirst ? f : g).completeExceptionally(ex);
1465          }
1532    }
1533
1534    public void testThenAcceptBoth_exceptionalCompletion4() {
1535        for (ExecutionMode m : ExecutionMode.values())
1536        for (Integer v1 : new Integer[] { 1, null }) {
1537
1538        final CompletableFuture<Integer> f = new CompletableFuture<>();
1539        final CompletableFuture<Integer> g = new CompletableFuture<>();
1540        final SubtractAction r = new SubtractAction();
1541        final CFException ex = new CFException();
1542
1543        f.completeExceptionally(ex);
1544        g.complete(v1);
1545        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1466  
1467          checkCompletedWithWrappedCFException(h, ex);
1548        checkCompletedWithWrappedCFException(f, ex);
1468          assertEquals(0, r.invocationCount);
1469 <        checkCompletedNormally(g, v1);
1470 <        }
1471 <    }
1469 >        checkCompletedNormally(fFirst ? f : g, v1);
1470 >        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1471 >    }}
1472  
1473      /**
1474       * thenAcceptBoth result completes exceptionally if action does
1475       */
1476 <    public void testThenAcceptBoth_actionFailed1() {
1476 >    public void testThenAcceptBoth_actionFailed() {
1477          for (ExecutionMode m : ExecutionMode.values())
1478 +        for (boolean fFirst : new boolean[] { true, false })
1479          for (Integer v1 : new Integer[] { 1, null })
1480 <        for (Integer v2 : new Integer[] { 2, null }) {
1481 <
1480 >        for (Integer v2 : new Integer[] { 2, null })
1481 >    {
1482          final CompletableFuture<Integer> f = new CompletableFuture<>();
1483          final CompletableFuture<Integer> g = new CompletableFuture<>();
1484 <        final FailingBiConsumer r = new FailingBiConsumer();
1484 >        final FailingBiConsumer r = new FailingBiConsumer(m);
1485          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1486  
1487 <        f.complete(v1);
1488 <        checkIncomplete(h);
1489 <        g.complete(v2);
1490 <
1491 <        checkCompletedWithWrappedCFException(h);
1492 <        checkCompletedNormally(f, v1);
1573 <        checkCompletedNormally(g, v2);
1487 >        if (fFirst) {
1488 >            f.complete(v1);
1489 >            g.complete(v2);
1490 >        } else {
1491 >            g.complete(v2);
1492 >            f.complete(v1);
1493          }
1575    }
1576
1577    public void testThenAcceptBoth_actionFailed2() {
1578        for (ExecutionMode m : ExecutionMode.values())
1579        for (Integer v1 : new Integer[] { 1, null })
1580        for (Integer v2 : new Integer[] { 2, null }) {
1581
1582        final CompletableFuture<Integer> f = new CompletableFuture<>();
1583        final CompletableFuture<Integer> g = new CompletableFuture<>();
1584        final FailingBiConsumer r = new FailingBiConsumer();
1585        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1586
1587        g.complete(v2);
1588        checkIncomplete(h);
1589        f.complete(v1);
1494  
1495          checkCompletedWithWrappedCFException(h);
1496          checkCompletedNormally(f, v1);
1497          checkCompletedNormally(g, v2);
1498 <        }
1595 <    }
1498 >    }}
1499  
1500      /**
1501       * thenAcceptBoth result completes exceptionally if either source cancelled
1502       */
1503 <    public void testThenAcceptBoth_sourceCancelled1() {
1601 <        for (ExecutionMode m : ExecutionMode.values())
1602 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1603 <        for (Integer v1 : new Integer[] { 1, null }) {
1604 <
1605 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1606 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1607 <        final SubtractAction r = new SubtractAction();
1608 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1609 <
1610 <        assertTrue(f.cancel(mayInterruptIfRunning));
1611 <        checkIncomplete(h);
1612 <        g.complete(v1);
1613 <
1614 <        checkCompletedWithWrappedCancellationException(h);
1615 <        checkCancelled(f);
1616 <        assertEquals(0, r.invocationCount);
1617 <        checkCompletedNormally(g, v1);
1618 <        }
1619 <    }
1620 <
1621 <    public void testThenAcceptBoth_sourceCancelled2() {
1622 <        for (ExecutionMode m : ExecutionMode.values())
1623 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1624 <        for (Integer v1 : new Integer[] { 1, null }) {
1625 <
1626 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1627 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1628 <        final SubtractAction r = new SubtractAction();
1629 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1630 <
1631 <        assertTrue(g.cancel(mayInterruptIfRunning));
1632 <        checkIncomplete(h);
1633 <        f.complete(v1);
1634 <
1635 <        checkCompletedWithWrappedCancellationException(h);
1636 <        checkCancelled(g);
1637 <        assertEquals(0, r.invocationCount);
1638 <        checkCompletedNormally(f, v1);
1639 <        }
1640 <    }
1641 <
1642 <    public void testThenAcceptBoth_sourceCancelled3() {
1503 >    public void testThenAcceptBoth_sourceCancelled() {
1504          for (ExecutionMode m : ExecutionMode.values())
1505          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1506 <        for (Integer v1 : new Integer[] { 1, null }) {
1507 <
1506 >        for (boolean createIncomplete : new boolean[] { true, false })
1507 >        for (boolean fFirst : new boolean[] { true, false })
1508 >        for (Integer v1 : new Integer[] { 1, null })
1509 >    {
1510          final CompletableFuture<Integer> f = new CompletableFuture<>();
1511          final CompletableFuture<Integer> g = new CompletableFuture<>();
1512 <        final SubtractAction r = new SubtractAction();
1512 >        final SubtractAction r = new SubtractAction(m);
1513  
1514 <        assertTrue(g.cancel(mayInterruptIfRunning));
1515 <        f.complete(v1);
1514 >        (fFirst ? f : g).complete(v1);
1515 >        if (!createIncomplete)
1516 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1517          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1518 <
1519 <        checkCompletedWithWrappedCancellationException(h);
1520 <        checkCancelled(g);
1657 <        assertEquals(0, r.invocationCount);
1658 <        checkCompletedNormally(f, v1);
1518 >        if (createIncomplete) {
1519 >            checkIncomplete(h);
1520 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1521          }
1660    }
1661
1662    public void testThenAcceptBoth_sourceCancelled4() {
1663        for (ExecutionMode m : ExecutionMode.values())
1664        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1665        for (Integer v1 : new Integer[] { 1, null }) {
1666
1667        final CompletableFuture<Integer> f = new CompletableFuture<>();
1668        final CompletableFuture<Integer> g = new CompletableFuture<>();
1669        final SubtractAction r = new SubtractAction();
1670
1671        assertTrue(f.cancel(mayInterruptIfRunning));
1672        g.complete(v1);
1673        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1522  
1523          checkCompletedWithWrappedCancellationException(h);
1524 <        checkCancelled(f);
1524 >        checkCancelled(!fFirst ? f : g);
1525          assertEquals(0, r.invocationCount);
1526 <        checkCompletedNormally(g, v1);
1527 <        }
1680 <    }
1526 >        checkCompletedNormally(fFirst ? f : g, v1);
1527 >    }}
1528  
1529      /**
1530       * runAfterBoth result completes normally after normal
1531       * completion of sources
1532       */
1533 <    public void testRunAfterBoth_normalCompletion1() {
1687 <        for (ExecutionMode m : ExecutionMode.values())
1688 <        for (Integer v1 : new Integer[] { 1, null })
1689 <        for (Integer v2 : new Integer[] { 2, null }) {
1690 <
1691 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1692 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1693 <        final Noop r = new Noop();
1694 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1695 <
1696 <        f.complete(v1);
1697 <        checkIncomplete(h);
1698 <        assertEquals(0, r.invocationCount);
1699 <        g.complete(v2);
1700 <
1701 <        checkCompletedNormally(h, null);
1702 <        assertEquals(1, r.invocationCount);
1703 <        checkCompletedNormally(f, v1);
1704 <        checkCompletedNormally(g, v2);
1705 <        }
1706 <    }
1707 <
1708 <    public void testRunAfterBoth_normalCompletion2() {
1709 <        for (ExecutionMode m : ExecutionMode.values())
1710 <        for (Integer v1 : new Integer[] { 1, null })
1711 <        for (Integer v2 : new Integer[] { 2, null }) {
1712 <
1713 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1714 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1715 <        final Noop r = new Noop();
1716 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1717 <
1718 <        g.complete(v2);
1719 <        checkIncomplete(h);
1720 <        assertEquals(0, r.invocationCount);
1721 <        f.complete(v1);
1722 <
1723 <        checkCompletedNormally(h, null);
1724 <        assertEquals(1, r.invocationCount);
1725 <        checkCompletedNormally(f, v1);
1726 <        checkCompletedNormally(g, v2);
1727 <        }
1728 <    }
1729 <
1730 <    public void testRunAfterBoth_normalCompletion3() {
1533 >    public void testRunAfterBoth_normalCompletion() {
1534          for (ExecutionMode m : ExecutionMode.values())
1535 +        for (boolean createIncomplete : new boolean[] { true, false })
1536 +        for (boolean fFirst : new boolean[] { true, false })
1537          for (Integer v1 : new Integer[] { 1, null })
1538 <        for (Integer v2 : new Integer[] { 2, null }) {
1539 <
1538 >        for (Integer v2 : new Integer[] { 2, null })
1539 >    {
1540          final CompletableFuture<Integer> f = new CompletableFuture<>();
1541          final CompletableFuture<Integer> g = new CompletableFuture<>();
1542 <        final Noop r = new Noop();
1542 >        final Noop r = new Noop(m);
1543  
1544 <        g.complete(v2);
1545 <        f.complete(v1);
1544 >        if (fFirst) f.complete(v1); else g.complete(v2);
1545 >        if (!createIncomplete)
1546 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1547          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1548 <
1549 <        checkCompletedNormally(h, null);
1550 <        assertEquals(1, r.invocationCount);
1551 <        checkCompletedNormally(f, v1);
1746 <        checkCompletedNormally(g, v2);
1548 >        if (createIncomplete) {
1549 >            checkIncomplete(h);
1550 >            assertEquals(0, r.invocationCount);
1551 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1552          }
1748    }
1749
1750    public void testRunAfterBoth_normalCompletion4() {
1751        for (ExecutionMode m : ExecutionMode.values())
1752        for (Integer v1 : new Integer[] { 1, null })
1753        for (Integer v2 : new Integer[] { 2, null }) {
1754
1755        final CompletableFuture<Integer> f = new CompletableFuture<>();
1756        final CompletableFuture<Integer> g = new CompletableFuture<>();
1757        final Noop r = new Noop();
1758
1759        f.complete(v1);
1760        g.complete(v2);
1761        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1553  
1554          checkCompletedNormally(h, null);
1555          assertEquals(1, r.invocationCount);
1556          checkCompletedNormally(f, v1);
1557          checkCompletedNormally(g, v2);
1558 <        }
1768 <    }
1558 >    }}
1559  
1560      /**
1561       * runAfterBoth result completes exceptionally after exceptional
1562       * completion of either source
1563       */
1564 <    public void testRunAfterBoth_exceptionalCompletion1() {
1775 <        for (ExecutionMode m : ExecutionMode.values())
1776 <        for (Integer v1 : new Integer[] { 1, null }) {
1777 <
1778 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1779 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1780 <        final Noop r = new Noop();
1781 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1782 <        final CFException ex = new CFException();
1783 <
1784 <        f.completeExceptionally(ex);
1785 <        checkIncomplete(h);
1786 <        g.complete(v1);
1787 <
1788 <        checkCompletedWithWrappedCFException(h, ex);
1789 <        checkCompletedWithWrappedCFException(f, ex);
1790 <        assertEquals(0, r.invocationCount);
1791 <        checkCompletedNormally(g, v1);
1792 <        }
1793 <    }
1794 <
1795 <    public void testRunAfterBoth_exceptionalCompletion2() {
1796 <        for (ExecutionMode m : ExecutionMode.values())
1797 <        for (Integer v1 : new Integer[] { 1, null }) {
1798 <
1799 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1800 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1801 <        final Noop r = new Noop();
1802 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1803 <        final CFException ex = new CFException();
1804 <
1805 <        g.completeExceptionally(ex);
1806 <        checkIncomplete(h);
1807 <        f.complete(v1);
1808 <
1809 <        checkCompletedWithWrappedCFException(h, ex);
1810 <        checkCompletedWithWrappedCFException(g, ex);
1811 <        assertEquals(0, r.invocationCount);
1812 <        checkCompletedNormally(f, v1);
1813 <        }
1814 <    }
1815 <
1816 <    public void testRunAfterBoth_exceptionalCompletion3() {
1564 >    public void testRunAfterBoth_exceptionalCompletion() {
1565          for (ExecutionMode m : ExecutionMode.values())
1566 <        for (Integer v1 : new Integer[] { 1, null }) {
1567 <
1566 >        for (boolean createIncomplete : new boolean[] { true, false })
1567 >        for (boolean fFirst : new boolean[] { true, false })
1568 >        for (Integer v1 : new Integer[] { 1, null })
1569 >    {
1570          final CompletableFuture<Integer> f = new CompletableFuture<>();
1571          final CompletableFuture<Integer> g = new CompletableFuture<>();
1822        final Noop r = new Noop();
1572          final CFException ex = new CFException();
1573 +        final Noop r = new Noop(m);
1574  
1575 <        g.completeExceptionally(ex);
1576 <        f.complete(v1);
1575 >        (fFirst ? f : g).complete(v1);
1576 >        if (!createIncomplete)
1577 >            (!fFirst ? f : g).completeExceptionally(ex);
1578          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1579 <
1580 <        checkCompletedWithWrappedCFException(h, ex);
1581 <        checkCompletedWithWrappedCFException(g, ex);
1831 <        assertEquals(0, r.invocationCount);
1832 <        checkCompletedNormally(f, v1);
1579 >        if (createIncomplete) {
1580 >            checkIncomplete(h);
1581 >            (!fFirst ? f : g).completeExceptionally(ex);
1582          }
1834    }
1835
1836    public void testRunAfterBoth_exceptionalCompletion4() {
1837        for (ExecutionMode m : ExecutionMode.values())
1838        for (Integer v1 : new Integer[] { 1, null }) {
1839
1840        final CompletableFuture<Integer> f = new CompletableFuture<>();
1841        final CompletableFuture<Integer> g = new CompletableFuture<>();
1842        final Noop r = new Noop();
1843        final CFException ex = new CFException();
1844
1845        f.completeExceptionally(ex);
1846        g.complete(v1);
1847        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1583  
1584          checkCompletedWithWrappedCFException(h, ex);
1850        checkCompletedWithWrappedCFException(f, ex);
1585          assertEquals(0, r.invocationCount);
1586 <        checkCompletedNormally(g, v1);
1587 <        }
1588 <    }
1586 >        checkCompletedNormally(fFirst ? f : g, v1);
1587 >        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1588 >    }}
1589  
1590      /**
1591       * runAfterBoth result completes exceptionally if action does
1592       */
1593 <    public void testRunAfterBoth_actionFailed1() {
1593 >    public void testRunAfterBoth_actionFailed() {
1594          for (ExecutionMode m : ExecutionMode.values())
1595 +        for (boolean fFirst : new boolean[] { true, false })
1596          for (Integer v1 : new Integer[] { 1, null })
1597 <        for (Integer v2 : new Integer[] { 2, null }) {
1598 <
1597 >        for (Integer v2 : new Integer[] { 2, null })
1598 >    {
1599          final CompletableFuture<Integer> f = new CompletableFuture<>();
1600          final CompletableFuture<Integer> g = new CompletableFuture<>();
1601 <        final FailingNoop r = new FailingNoop();
1867 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1868 <
1869 <        f.complete(v1);
1870 <        checkIncomplete(h);
1871 <        g.complete(v2);
1601 >        final FailingRunnable r = new FailingRunnable(m);
1602  
1603 <        checkCompletedWithWrappedCFException(h);
1604 <        checkCompletedNormally(f, v1);
1605 <        checkCompletedNormally(g, v2);
1603 >        CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r);
1604 >        if (fFirst) {
1605 >            f.complete(v1);
1606 >            g.complete(v2);
1607 >        } else {
1608 >            g.complete(v2);
1609 >            f.complete(v1);
1610          }
1611 <    }
1878 <
1879 <    public void testRunAfterBoth_actionFailed2() {
1880 <        for (ExecutionMode m : ExecutionMode.values())
1881 <        for (Integer v1 : new Integer[] { 1, null })
1882 <        for (Integer v2 : new Integer[] { 2, null }) {
1883 <
1884 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1885 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1886 <        final FailingNoop r = new FailingNoop();
1887 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1888 <
1889 <        g.complete(v2);
1890 <        checkIncomplete(h);
1891 <        f.complete(v1);
1611 >        CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r);
1612  
1613 <        checkCompletedWithWrappedCFException(h);
1613 >        checkCompletedWithWrappedCFException(h1);
1614 >        checkCompletedWithWrappedCFException(h2);
1615          checkCompletedNormally(f, v1);
1616          checkCompletedNormally(g, v2);
1617 <        }
1897 <    }
1617 >    }}
1618  
1619      /**
1620       * runAfterBoth result completes exceptionally if either source cancelled
1621       */
1622 <    public void testRunAfterBoth_sourceCancelled1() {
1622 >    public void testRunAfterBoth_sourceCancelled() {
1623          for (ExecutionMode m : ExecutionMode.values())
1624          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1625 <        for (Integer v1 : new Integer[] { 1, null }) {
1626 <
1625 >        for (boolean createIncomplete : new boolean[] { true, false })
1626 >        for (boolean fFirst : new boolean[] { true, false })
1627 >        for (Integer v1 : new Integer[] { 1, null })
1628 >    {
1629          final CompletableFuture<Integer> f = new CompletableFuture<>();
1630          final CompletableFuture<Integer> g = new CompletableFuture<>();
1631 <        final Noop r = new Noop();
1910 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1631 >        final Noop r = new Noop(m);
1632  
1912        assertTrue(f.cancel(mayInterruptIfRunning));
1913        checkIncomplete(h);
1914        g.complete(v1);
1915
1916        checkCompletedWithWrappedCancellationException(h);
1917        checkCancelled(f);
1918        assertEquals(0, r.invocationCount);
1919        checkCompletedNormally(g, v1);
1920        }
1921    }
1922
1923    public void testRunAfterBoth_sourceCancelled2() {
1924        for (ExecutionMode m : ExecutionMode.values())
1925        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1926        for (Integer v1 : new Integer[] { 1, null }) {
1633  
1634 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1635 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1636 <        final Noop r = new Noop();
1634 >        (fFirst ? f : g).complete(v1);
1635 >        if (!createIncomplete)
1636 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1637          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1638 <
1639 <        assertTrue(g.cancel(mayInterruptIfRunning));
1640 <        checkIncomplete(h);
1935 <        f.complete(v1);
1936 <
1937 <        checkCompletedWithWrappedCancellationException(h);
1938 <        checkCancelled(g);
1939 <        assertEquals(0, r.invocationCount);
1940 <        checkCompletedNormally(f, v1);
1638 >        if (createIncomplete) {
1639 >            checkIncomplete(h);
1640 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1641          }
1942    }
1943
1944    public void testRunAfterBoth_sourceCancelled3() {
1945        for (ExecutionMode m : ExecutionMode.values())
1946        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1947        for (Integer v1 : new Integer[] { 1, null }) {
1948
1949        final CompletableFuture<Integer> f = new CompletableFuture<>();
1950        final CompletableFuture<Integer> g = new CompletableFuture<>();
1951        final Noop r = new Noop();
1952
1953        assertTrue(g.cancel(mayInterruptIfRunning));
1954        f.complete(v1);
1955        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1642  
1643          checkCompletedWithWrappedCancellationException(h);
1644 <        checkCancelled(g);
1644 >        checkCancelled(!fFirst ? f : g);
1645          assertEquals(0, r.invocationCount);
1646 <        checkCompletedNormally(f, v1);
1647 <        }
1962 <    }
1963 <
1964 <    public void testRunAfterBoth_sourceCancelled4() {
1965 <        for (ExecutionMode m : ExecutionMode.values())
1966 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1967 <        for (Integer v1 : new Integer[] { 1, null }) {
1968 <
1969 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1970 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1971 <        final Noop r = new Noop();
1972 <
1973 <        assertTrue(f.cancel(mayInterruptIfRunning));
1974 <        g.complete(v1);
1975 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1976 <
1977 <        checkCompletedWithWrappedCancellationException(h);
1978 <        checkCancelled(f);
1979 <        assertEquals(0, r.invocationCount);
1980 <        checkCompletedNormally(g, v1);
1981 <        }
1982 <    }
1646 >        checkCompletedNormally(fFirst ? f : g, v1);
1647 >    }}
1648  
1649      /**
1650       * applyToEither result completes normally after normal completion
1651       * of either source
1652       */
1653 <    public void testApplyToEither_normalCompletion1() {
1653 >    public void testApplyToEither_normalCompletion() {
1654          for (ExecutionMode m : ExecutionMode.values())
1655 +        for (boolean createIncomplete : new boolean[] { true, false })
1656 +        for (boolean fFirst : new boolean[] { true, false })
1657          for (Integer v1 : new Integer[] { 1, null })
1658 <        for (Integer v2 : new Integer[] { 2, null }) {
1659 <
1658 >        for (Integer v2 : new Integer[] { 2, null })
1659 >    {
1660          final CompletableFuture<Integer> f = new CompletableFuture<>();
1661          final CompletableFuture<Integer> g = new CompletableFuture<>();
1662 <        final IncFunction r = new IncFunction();
1996 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1662 >        final IncFunction r = new IncFunction(m);
1663  
1664 <        f.complete(v1);
1665 <        checkCompletedNormally(h, inc(v1));
1666 <        g.complete(v2);
1664 >        if (!createIncomplete)
1665 >            if (fFirst) f.complete(v1); else g.complete(v2);
1666 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1667 >        if (createIncomplete) {
1668 >            checkIncomplete(h);
1669 >            assertEquals(0, r.invocationCount);
1670 >            if (fFirst) f.complete(v1); else g.complete(v2);
1671 >        }
1672 >        checkCompletedNormally(h, inc(fFirst ? v1 : v2));
1673 >        if (!fFirst) f.complete(v1); else g.complete(v2);
1674  
1675          checkCompletedNormally(f, v1);
1676          checkCompletedNormally(g, v2);
1677 <        checkCompletedNormally(h, inc(v1));
1678 <        }
2006 <    }
1677 >        checkCompletedNormally(h, inc(fFirst ? v1 : v2));
1678 >    }}
1679  
1680 <    public void testApplyToEither_normalCompletion2() {
1680 >    public void testApplyToEither_normalCompletionBothAvailable() {
1681          for (ExecutionMode m : ExecutionMode.values())
1682 +        for (boolean fFirst : new boolean[] { true, false })
1683          for (Integer v1 : new Integer[] { 1, null })
1684 <        for (Integer v2 : new Integer[] { 2, null }) {
1685 <
1684 >        for (Integer v2 : new Integer[] { 2, null })
1685 >    {
1686          final CompletableFuture<Integer> f = new CompletableFuture<>();
1687          final CompletableFuture<Integer> g = new CompletableFuture<>();
1688 <        final IncFunction r = new IncFunction();
2016 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1688 >        final IncFunction r = new IncFunction(m);
1689  
1690 <        g.complete(v2);
1691 <        checkCompletedNormally(h, inc(v2));
1692 <        f.complete(v1);
1693 <
1694 <        checkCompletedNormally(f, v1);
1695 <        checkCompletedNormally(g, v2);
2024 <        checkCompletedNormally(h, inc(v2));
1690 >        if (fFirst) {
1691 >            f.complete(v1);
1692 >            g.complete(v2);
1693 >        } else {
1694 >            g.complete(v2);
1695 >            f.complete(v1);
1696          }
2026    }
2027    public void testApplyToEither_normalCompletion3() {
2028        for (ExecutionMode m : ExecutionMode.values())
2029        for (Integer v1 : new Integer[] { 1, null })
2030        for (Integer v2 : new Integer[] { 2, null }) {
1697  
2032        final CompletableFuture<Integer> f = new CompletableFuture<>();
2033        final CompletableFuture<Integer> g = new CompletableFuture<>();
2034        final IncFunction r = new IncFunction();
2035
2036        f.complete(v1);
2037        g.complete(v2);
1698          final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1699  
1700          checkCompletedNormally(f, v1);
# Line 2044 | Line 1704 | public class CompletableFutureTest exten
1704          assertTrue(Objects.equals(h.join(), inc(v1)) ||
1705                     Objects.equals(h.join(), inc(v2)));
1706          assertEquals(1, r.invocationCount);
1707 <        }
2048 <    }
1707 >    }}
1708  
1709      /**
1710       * applyToEither result completes exceptionally after exceptional
# Line 2053 | Line 1712 | public class CompletableFutureTest exten
1712       */
1713      public void testApplyToEither_exceptionalCompletion1() {
1714          for (ExecutionMode m : ExecutionMode.values())
1715 <        for (Integer v1 : new Integer[] { 1, null }) {
1716 <
1715 >        for (boolean createIncomplete : new boolean[] { true, false })
1716 >        for (boolean fFirst : new boolean[] { true, false })
1717 >        for (Integer v1 : new Integer[] { 1, null })
1718 >    {
1719          final CompletableFuture<Integer> f = new CompletableFuture<>();
1720          final CompletableFuture<Integer> g = new CompletableFuture<>();
2060        final IncFunction r = new IncFunction();
2061        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1721          final CFException ex = new CFException();
1722 +        final IncFunction r = new IncFunction(m);
1723  
1724 <        f.completeExceptionally(ex);
2065 <        checkCompletedWithWrappedCFException(h, ex);
2066 <        g.complete(v1);
2067 <
2068 <        assertEquals(0, r.invocationCount);
2069 <        checkCompletedNormally(g, v1);
2070 <        checkCompletedWithWrappedCFException(f, ex);
2071 <        checkCompletedWithWrappedCFException(h, ex);
2072 <        }
2073 <    }
2074 <
2075 <    public void testApplyToEither_exceptionalCompletion2() {
2076 <        for (ExecutionMode m : ExecutionMode.values())
2077 <        for (Integer v1 : new Integer[] { 1, null }) {
2078 <
2079 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2080 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2081 <        final IncFunction r = new IncFunction();
1724 >        if (!createIncomplete) (fFirst ? f : g).completeExceptionally(ex);
1725          final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1726 <        final CFException ex = new CFException();
1726 >        if (createIncomplete) {
1727 >            checkIncomplete(h);
1728 >            assertEquals(0, r.invocationCount);
1729 >            (fFirst ? f : g).completeExceptionally(ex);
1730 >        }
1731  
2085        g.completeExceptionally(ex);
1732          checkCompletedWithWrappedCFException(h, ex);
1733 <        f.complete(v1);
1733 >        (!fFirst ? f : g).complete(v1);
1734  
1735          assertEquals(0, r.invocationCount);
1736 <        checkCompletedNormally(f, v1);
1737 <        checkCompletedWithWrappedCFException(g, ex);
1736 >        checkCompletedNormally(!fFirst ? f : g, v1);
1737 >        checkCompletedWithWrappedCFException(fFirst ? f : g, ex);
1738          checkCompletedWithWrappedCFException(h, ex);
1739 <        }
2094 <    }
1739 >    }}
1740  
1741 <    public void testApplyToEither_exceptionalCompletion3() {
1741 >    public void testApplyToEither_exceptionalCompletion2() {
1742          for (ExecutionMode m : ExecutionMode.values())
1743 <        for (Integer v1 : new Integer[] { 1, null }) {
1744 <
1743 >        for (boolean reverseArgs : new boolean[] { true, false })
1744 >        for (boolean fFirst : new boolean[] { true, false })
1745 >        for (Integer v1 : new Integer[] { 1, null })
1746 >    {
1747          final CompletableFuture<Integer> f = new CompletableFuture<>();
1748          final CompletableFuture<Integer> g = new CompletableFuture<>();
1749 <        final IncFunction r = new IncFunction();
1749 >        final IncFunction r1 = new IncFunction(m);
1750 >        final IncFunction r2 = new IncFunction(m);
1751          final CFException ex = new CFException();
1752 <
1753 <        g.completeExceptionally(ex);
1754 <        f.complete(v1);
1755 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1752 >        final CompletableFuture<Integer> j = (reverseArgs ? g : f);
1753 >        final CompletableFuture<Integer> k = (reverseArgs ? f : g);
1754 >        final CompletableFuture<Integer> h1 = m.applyToEither(j, k, r1);
1755 >        if (fFirst) {
1756 >            f.complete(v1);
1757 >            g.completeExceptionally(ex);
1758 >        } else {
1759 >            g.completeExceptionally(ex);
1760 >            f.complete(v1);
1761 >        }
1762 >        final CompletableFuture<Integer> h2 = m.applyToEither(j, k, r2);
1763  
1764          // unspecified behavior
2110        Integer v;
1765          try {
1766 <            assertEquals(inc(v1), h.join());
1767 <            assertEquals(1, r.invocationCount);
1766 >            assertEquals(inc(v1), h1.join());
1767 >            assertEquals(1, r1.invocationCount);
1768          } catch (CompletionException ok) {
1769 <            checkCompletedWithWrappedCFException(h, ex);
1770 <            assertEquals(0, r.invocationCount);
1769 >            checkCompletedWithWrappedCFException(h1, ex);
1770 >            assertEquals(0, r1.invocationCount);
1771          }
1772  
2119        checkCompletedWithWrappedCFException(g, ex);
2120        checkCompletedNormally(f, v1);
2121        }
2122    }
2123
2124    public void testApplyToEither_exceptionalCompletion4() {
2125        for (ExecutionMode m : ExecutionMode.values())
2126        for (Integer v1 : new Integer[] { 1, null }) {
2127
2128        final CompletableFuture<Integer> f = new CompletableFuture<>();
2129        final CompletableFuture<Integer> g = new CompletableFuture<>();
2130        final IncFunction r = new IncFunction();
2131        final CFException ex = new CFException();
2132
2133        f.completeExceptionally(ex);
2134        g.complete(v1);
2135        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2136
2137        // unspecified behavior
2138        Integer v;
1773          try {
1774 <            assertEquals(inc(v1), h.join());
1775 <            assertEquals(1, r.invocationCount);
1774 >            assertEquals(inc(v1), h2.join());
1775 >            assertEquals(1, r2.invocationCount);
1776          } catch (CompletionException ok) {
1777 <            checkCompletedWithWrappedCFException(h, ex);
1778 <            assertEquals(0, r.invocationCount);
1777 >            checkCompletedWithWrappedCFException(h2, ex);
1778 >            assertEquals(0, r2.invocationCount);
1779          }
1780  
1781 <        checkCompletedWithWrappedCFException(f, ex);
1782 <        checkCompletedNormally(g, v1);
1783 <        }
2150 <    }
1781 >        checkCompletedWithWrappedCFException(g, ex);
1782 >        checkCompletedNormally(f, v1);
1783 >    }}
1784  
1785      /**
1786       * applyToEither result completes exceptionally if action does
# Line 2155 | Line 1788 | public class CompletableFutureTest exten
1788      public void testApplyToEither_actionFailed1() {
1789          for (ExecutionMode m : ExecutionMode.values())
1790          for (Integer v1 : new Integer[] { 1, null })
1791 <        for (Integer v2 : new Integer[] { 2, null }) {
1792 <
1791 >        for (Integer v2 : new Integer[] { 2, null })
1792 >    {
1793          final CompletableFuture<Integer> f = new CompletableFuture<>();
1794          final CompletableFuture<Integer> g = new CompletableFuture<>();
1795 <        final FailingFunction r = new FailingFunction();
1795 >        final FailingFunction r = new FailingFunction(m);
1796          final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1797  
1798          f.complete(v1);
# Line 2167 | Line 1800 | public class CompletableFutureTest exten
1800          g.complete(v2);
1801          checkCompletedNormally(f, v1);
1802          checkCompletedNormally(g, v2);
1803 <        }
2171 <    }
1803 >    }}
1804  
1805      public void testApplyToEither_actionFailed2() {
1806          for (ExecutionMode m : ExecutionMode.values())
1807          for (Integer v1 : new Integer[] { 1, null })
1808 <        for (Integer v2 : new Integer[] { 2, null }) {
1809 <
1808 >        for (Integer v2 : new Integer[] { 2, null })
1809 >    {
1810          final CompletableFuture<Integer> f = new CompletableFuture<>();
1811          final CompletableFuture<Integer> g = new CompletableFuture<>();
1812 <        final FailingFunction r = new FailingFunction();
1812 >        final FailingFunction r = new FailingFunction(m);
1813          final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1814  
1815          g.complete(v2);
# Line 2185 | Line 1817 | public class CompletableFutureTest exten
1817          f.complete(v1);
1818          checkCompletedNormally(f, v1);
1819          checkCompletedNormally(g, v2);
1820 <        }
2189 <    }
1820 >    }}
1821  
1822      /**
1823       * applyToEither result completes exceptionally if either source cancelled
# Line 2194 | Line 1825 | public class CompletableFutureTest exten
1825      public void testApplyToEither_sourceCancelled1() {
1826          for (ExecutionMode m : ExecutionMode.values())
1827          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1828 <        for (Integer v1 : new Integer[] { 1, null }) {
1829 <
1828 >        for (boolean createIncomplete : new boolean[] { true, false })
1829 >        for (boolean fFirst : new boolean[] { true, false })
1830 >        for (Integer v1 : new Integer[] { 1, null })
1831 >    {
1832          final CompletableFuture<Integer> f = new CompletableFuture<>();
1833          final CompletableFuture<Integer> g = new CompletableFuture<>();
1834 <        final IncFunction r = new IncFunction();
1834 >        final IncFunction r = new IncFunction(m);
1835 >
1836 >        if (!createIncomplete) assertTrue((fFirst ? f : g).cancel(mayInterruptIfRunning));
1837          final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1838 +        if (createIncomplete) {
1839 +            checkIncomplete(h);
1840 +            assertEquals(0, r.invocationCount);
1841 +            assertTrue((fFirst ? f : g).cancel(mayInterruptIfRunning));
1842 +        }
1843  
2204        assertTrue(f.cancel(mayInterruptIfRunning));
1844          checkCompletedWithWrappedCancellationException(h);
1845 <        g.complete(v1);
1845 >        (!fFirst ? f : g).complete(v1);
1846  
2208        checkCancelled(f);
1847          assertEquals(0, r.invocationCount);
1848 <        checkCompletedNormally(g, v1);
1848 >        checkCompletedNormally(!fFirst ? f : g, v1);
1849 >        checkCancelled(fFirst ? f : g);
1850          checkCompletedWithWrappedCancellationException(h);
1851 <        }
2213 <    }
1851 >    }}
1852  
1853      public void testApplyToEither_sourceCancelled2() {
1854          for (ExecutionMode m : ExecutionMode.values())
1855          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1856 <        for (Integer v1 : new Integer[] { 1, null }) {
1857 <
1856 >        for (boolean reverseArgs : new boolean[] { true, false })
1857 >        for (boolean fFirst : new boolean[] { true, false })
1858 >        for (Integer v1 : new Integer[] { 1, null })
1859 >    {
1860          final CompletableFuture<Integer> f = new CompletableFuture<>();
1861          final CompletableFuture<Integer> g = new CompletableFuture<>();
1862 <        final IncFunction r = new IncFunction();
1863 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1864 <
1865 <        assertTrue(g.cancel(mayInterruptIfRunning));
1866 <        checkCompletedWithWrappedCancellationException(h);
2227 <        f.complete(v1);
1862 >        final IncFunction r1 = new IncFunction(m);
1863 >        final IncFunction r2 = new IncFunction(m);
1864 >        final CFException ex = new CFException();
1865 >        final CompletableFuture<Integer> j = (reverseArgs ? g : f);
1866 >        final CompletableFuture<Integer> k = (reverseArgs ? f : g);
1867  
1868 <        checkCancelled(g);
1869 <        assertEquals(0, r.invocationCount);
1870 <        checkCompletedNormally(f, v1);
1871 <        checkCompletedWithWrappedCancellationException(h);
1868 >        final CompletableFuture<Integer> h1 = m.applyToEither(j, k, r1);
1869 >        if (fFirst) {
1870 >            f.complete(v1);
1871 >            assertTrue(g.cancel(mayInterruptIfRunning));
1872 >        } else {
1873 >            assertTrue(g.cancel(mayInterruptIfRunning));
1874 >            f.complete(v1);
1875          }
1876 <    }
2235 <
2236 <    public void testApplyToEither_sourceCancelled3() {
2237 <        for (ExecutionMode m : ExecutionMode.values())
2238 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2239 <        for (Integer v1 : new Integer[] { 1, null }) {
2240 <
2241 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2242 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2243 <        final IncFunction r = new IncFunction();
2244 <
2245 <        assertTrue(g.cancel(mayInterruptIfRunning));
2246 <        f.complete(v1);
2247 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1876 >        final CompletableFuture<Integer> h2 = m.applyToEither(j, k, r2);
1877  
1878          // unspecified behavior
2250        Integer v;
1879          try {
1880 <            assertEquals(inc(v1), h.join());
1881 <            assertEquals(1, r.invocationCount);
1880 >            assertEquals(inc(v1), h1.join());
1881 >            assertEquals(1, r1.invocationCount);
1882          } catch (CompletionException ok) {
1883 <            checkCompletedWithWrappedCancellationException(h);
1884 <            assertEquals(0, r.invocationCount);
1883 >            checkCompletedWithWrappedCancellationException(h1);
1884 >            assertEquals(0, r1.invocationCount);
1885          }
1886  
2259        checkCancelled(g);
2260        checkCompletedNormally(f, v1);
2261        }
2262    }
2263
2264    public void testApplyToEither_sourceCancelled4() {
2265        for (ExecutionMode m : ExecutionMode.values())
2266        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2267        for (Integer v1 : new Integer[] { 1, null }) {
2268
2269        final CompletableFuture<Integer> f = new CompletableFuture<>();
2270        final CompletableFuture<Integer> g = new CompletableFuture<>();
2271        final IncFunction r = new IncFunction();
2272
2273        assertTrue(f.cancel(mayInterruptIfRunning));
2274        g.complete(v1);
2275        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2276
2277        // unspecified behavior
2278        Integer v;
1887          try {
1888 <            assertEquals(inc(v1), h.join());
1889 <            assertEquals(1, r.invocationCount);
1888 >            assertEquals(inc(v1), h2.join());
1889 >            assertEquals(1, r2.invocationCount);
1890          } catch (CompletionException ok) {
1891 <            checkCompletedWithWrappedCancellationException(h);
1892 <            assertEquals(0, r.invocationCount);
1891 >            checkCompletedWithWrappedCancellationException(h2);
1892 >            assertEquals(0, r2.invocationCount);
1893          }
1894  
1895 <        checkCancelled(f);
1896 <        checkCompletedNormally(g, v1);
1897 <        }
2290 <    }
1895 >        checkCancelled(g);
1896 >        checkCompletedNormally(f, v1);
1897 >    }}
1898  
1899      /**
1900       * acceptEither result completes normally after normal completion
# Line 2296 | Line 1903 | public class CompletableFutureTest exten
1903      public void testAcceptEither_normalCompletion1() {
1904          for (ExecutionMode m : ExecutionMode.values())
1905          for (Integer v1 : new Integer[] { 1, null })
1906 <        for (Integer v2 : new Integer[] { 2, null }) {
1907 <
1906 >        for (Integer v2 : new Integer[] { 2, null })
1907 >    {
1908          final CompletableFuture<Integer> f = new CompletableFuture<>();
1909          final CompletableFuture<Integer> g = new CompletableFuture<>();
1910          final IncAction r = new IncAction();
# Line 2311 | Line 1918 | public class CompletableFutureTest exten
1918          checkCompletedNormally(f, v1);
1919          checkCompletedNormally(g, v2);
1920          checkCompletedNormally(h, null);
1921 <        }
2315 <    }
1921 >    }}
1922  
1923      public void testAcceptEither_normalCompletion2() {
1924          for (ExecutionMode m : ExecutionMode.values())
1925          for (Integer v1 : new Integer[] { 1, null })
1926 <        for (Integer v2 : new Integer[] { 2, null }) {
1927 <
1926 >        for (Integer v2 : new Integer[] { 2, null })
1927 >    {
1928          final CompletableFuture<Integer> f = new CompletableFuture<>();
1929          final CompletableFuture<Integer> g = new CompletableFuture<>();
1930          final IncAction r = new IncAction();
# Line 2332 | Line 1938 | public class CompletableFutureTest exten
1938          checkCompletedNormally(f, v1);
1939          checkCompletedNormally(g, v2);
1940          checkCompletedNormally(h, null);
1941 <        }
1942 <    }
1941 >    }}
1942 >
1943      public void testAcceptEither_normalCompletion3() {
1944          for (ExecutionMode m : ExecutionMode.values())
1945          for (Integer v1 : new Integer[] { 1, null })
1946 <        for (Integer v2 : new Integer[] { 2, null }) {
1947 <
1946 >        for (Integer v2 : new Integer[] { 2, null })
1947 >    {
1948          final CompletableFuture<Integer> f = new CompletableFuture<>();
1949          final CompletableFuture<Integer> g = new CompletableFuture<>();
1950          final IncAction r = new IncAction();
# Line 2354 | Line 1960 | public class CompletableFutureTest exten
1960          // unspecified behavior
1961          assertTrue(Objects.equals(r.value, inc(v1)) ||
1962                     Objects.equals(r.value, inc(v2)));
1963 <        }
2358 <    }
1963 >    }}
1964  
1965      /**
1966       * acceptEither result completes exceptionally after exceptional
# Line 2363 | Line 1968 | public class CompletableFutureTest exten
1968       */
1969      public void testAcceptEither_exceptionalCompletion1() {
1970          for (ExecutionMode m : ExecutionMode.values())
1971 <        for (Integer v1 : new Integer[] { 1, null }) {
1972 <
1971 >        for (Integer v1 : new Integer[] { 1, null })
1972 >    {
1973          final CompletableFuture<Integer> f = new CompletableFuture<>();
1974          final CompletableFuture<Integer> g = new CompletableFuture<>();
1975          final IncAction r = new IncAction();
# Line 2379 | Line 1984 | public class CompletableFutureTest exten
1984          checkCompletedNormally(g, v1);
1985          checkCompletedWithWrappedCFException(f, ex);
1986          checkCompletedWithWrappedCFException(h, ex);
1987 <        }
2383 <    }
1987 >    }}
1988  
1989      public void testAcceptEither_exceptionalCompletion2() {
1990          for (ExecutionMode m : ExecutionMode.values())
1991 <        for (Integer v1 : new Integer[] { 1, null }) {
1992 <
1991 >        for (Integer v1 : new Integer[] { 1, null })
1992 >    {
1993          final CompletableFuture<Integer> f = new CompletableFuture<>();
1994          final CompletableFuture<Integer> g = new CompletableFuture<>();
1995          final IncAction r = new IncAction();
# Line 2400 | Line 2004 | public class CompletableFutureTest exten
2004          checkCompletedNormally(f, v1);
2005          checkCompletedWithWrappedCFException(g, ex);
2006          checkCompletedWithWrappedCFException(h, ex);
2007 <        }
2404 <    }
2007 >    }}
2008  
2009      public void testAcceptEither_exceptionalCompletion3() {
2010          for (ExecutionMode m : ExecutionMode.values())
2011 <        for (Integer v1 : new Integer[] { 1, null }) {
2012 <
2011 >        for (Integer v1 : new Integer[] { 1, null })
2012 >    {
2013          final CompletableFuture<Integer> f = new CompletableFuture<>();
2014          final CompletableFuture<Integer> g = new CompletableFuture<>();
2015          final IncAction r = new IncAction();
# Line 2429 | Line 2032 | public class CompletableFutureTest exten
2032  
2033          checkCompletedWithWrappedCFException(g, ex);
2034          checkCompletedNormally(f, v1);
2035 <        }
2433 <    }
2035 >    }}
2036  
2037      public void testAcceptEither_exceptionalCompletion4() {
2038          for (ExecutionMode m : ExecutionMode.values())
2039 <        for (Integer v1 : new Integer[] { 1, null }) {
2040 <
2039 >        for (Integer v1 : new Integer[] { 1, null })
2040 >    {
2041          final CompletableFuture<Integer> f = new CompletableFuture<>();
2042          final CompletableFuture<Integer> g = new CompletableFuture<>();
2043          final IncAction r = new IncAction();
# Line 2458 | Line 2060 | public class CompletableFutureTest exten
2060  
2061          checkCompletedWithWrappedCFException(f, ex);
2062          checkCompletedNormally(g, v1);
2063 <        }
2462 <    }
2063 >    }}
2064  
2065      /**
2066       * acceptEither result completes exceptionally if action does
# Line 2467 | Line 2068 | public class CompletableFutureTest exten
2068      public void testAcceptEither_actionFailed1() {
2069          for (ExecutionMode m : ExecutionMode.values())
2070          for (Integer v1 : new Integer[] { 1, null })
2071 <        for (Integer v2 : new Integer[] { 2, null }) {
2072 <
2071 >        for (Integer v2 : new Integer[] { 2, null })
2072 >    {
2073          final CompletableFuture<Integer> f = new CompletableFuture<>();
2074          final CompletableFuture<Integer> g = new CompletableFuture<>();
2075 <        final FailingConsumer r = new FailingConsumer();
2075 >        final FailingConsumer r = new FailingConsumer(m);
2076          final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2077  
2078          f.complete(v1);
# Line 2479 | Line 2080 | public class CompletableFutureTest exten
2080          g.complete(v2);
2081          checkCompletedNormally(f, v1);
2082          checkCompletedNormally(g, v2);
2083 <        }
2483 <    }
2083 >    }}
2084  
2085      public void testAcceptEither_actionFailed2() {
2086          for (ExecutionMode m : ExecutionMode.values())
2087          for (Integer v1 : new Integer[] { 1, null })
2088 <        for (Integer v2 : new Integer[] { 2, null }) {
2089 <
2088 >        for (Integer v2 : new Integer[] { 2, null })
2089 >    {
2090          final CompletableFuture<Integer> f = new CompletableFuture<>();
2091          final CompletableFuture<Integer> g = new CompletableFuture<>();
2092 <        final FailingConsumer r = new FailingConsumer();
2092 >        final FailingConsumer r = new FailingConsumer(m);
2093          final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2094  
2095          g.complete(v2);
# Line 2497 | Line 2097 | public class CompletableFutureTest exten
2097          f.complete(v1);
2098          checkCompletedNormally(f, v1);
2099          checkCompletedNormally(g, v2);
2100 <        }
2501 <    }
2100 >    }}
2101  
2102      /**
2103       * acceptEither result completes exceptionally if either source cancelled
# Line 2506 | Line 2105 | public class CompletableFutureTest exten
2105      public void testAcceptEither_sourceCancelled1() {
2106          for (ExecutionMode m : ExecutionMode.values())
2107          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2108 <        for (Integer v1 : new Integer[] { 1, null }) {
2109 <
2108 >        for (Integer v1 : new Integer[] { 1, null })
2109 >    {
2110          final CompletableFuture<Integer> f = new CompletableFuture<>();
2111          final CompletableFuture<Integer> g = new CompletableFuture<>();
2112          final IncAction r = new IncAction();
# Line 2521 | Line 2120 | public class CompletableFutureTest exten
2120          assertEquals(0, r.invocationCount);
2121          checkCompletedNormally(g, v1);
2122          checkCompletedWithWrappedCancellationException(h);
2123 <        }
2525 <    }
2123 >    }}
2124  
2125      public void testAcceptEither_sourceCancelled2() {
2126          for (ExecutionMode m : ExecutionMode.values())
2127          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2128 <        for (Integer v1 : new Integer[] { 1, null }) {
2129 <
2128 >        for (Integer v1 : new Integer[] { 1, null })
2129 >    {
2130          final CompletableFuture<Integer> f = new CompletableFuture<>();
2131          final CompletableFuture<Integer> g = new CompletableFuture<>();
2132          final IncAction r = new IncAction();
# Line 2542 | Line 2140 | public class CompletableFutureTest exten
2140          assertEquals(0, r.invocationCount);
2141          checkCompletedNormally(f, v1);
2142          checkCompletedWithWrappedCancellationException(h);
2143 <        }
2546 <    }
2143 >    }}
2144  
2145      public void testAcceptEither_sourceCancelled3() {
2146          for (ExecutionMode m : ExecutionMode.values())
2147          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2148 <        for (Integer v1 : new Integer[] { 1, null }) {
2149 <
2148 >        for (Integer v1 : new Integer[] { 1, null })
2149 >    {
2150          final CompletableFuture<Integer> f = new CompletableFuture<>();
2151          final CompletableFuture<Integer> g = new CompletableFuture<>();
2152          final IncAction r = new IncAction();
# Line 2571 | Line 2168 | public class CompletableFutureTest exten
2168  
2169          checkCancelled(g);
2170          checkCompletedNormally(f, v1);
2171 <        }
2575 <    }
2171 >    }}
2172  
2173      public void testAcceptEither_sourceCancelled4() {
2174          for (ExecutionMode m : ExecutionMode.values())
2175          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2176 <        for (Integer v1 : new Integer[] { 1, null }) {
2177 <
2176 >        for (Integer v1 : new Integer[] { 1, null })
2177 >    {
2178          final CompletableFuture<Integer> f = new CompletableFuture<>();
2179          final CompletableFuture<Integer> g = new CompletableFuture<>();
2180          final IncAction r = new IncAction();
# Line 2600 | Line 2196 | public class CompletableFutureTest exten
2196  
2197          checkCancelled(f);
2198          checkCompletedNormally(g, v1);
2199 <        }
2604 <    }
2199 >    }}
2200  
2201      /**
2202       * runAfterEither result completes normally after normal completion
# Line 2610 | Line 2205 | public class CompletableFutureTest exten
2205      public void testRunAfterEither_normalCompletion1() {
2206          for (ExecutionMode m : ExecutionMode.values())
2207          for (Integer v1 : new Integer[] { 1, null })
2208 <        for (Integer v2 : new Integer[] { 2, null }) {
2209 <
2208 >        for (Integer v2 : new Integer[] { 2, null })
2209 >    {
2210          final CompletableFuture<Integer> f = new CompletableFuture<>();
2211          final CompletableFuture<Integer> g = new CompletableFuture<>();
2212 <        final Noop r = new Noop();
2212 >        final Noop r = new Noop(m);
2213          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2214  
2215          f.complete(v1);
# Line 2626 | Line 2221 | public class CompletableFutureTest exten
2221          checkCompletedNormally(g, v2);
2222          checkCompletedNormally(h, null);
2223          assertEquals(1, r.invocationCount);
2224 <        }
2630 <    }
2224 >    }}
2225  
2226      public void testRunAfterEither_normalCompletion2() {
2227          for (ExecutionMode m : ExecutionMode.values())
2228          for (Integer v1 : new Integer[] { 1, null })
2229 <        for (Integer v2 : new Integer[] { 2, null }) {
2230 <
2229 >        for (Integer v2 : new Integer[] { 2, null })
2230 >    {
2231          final CompletableFuture<Integer> f = new CompletableFuture<>();
2232          final CompletableFuture<Integer> g = new CompletableFuture<>();
2233 <        final Noop r = new Noop();
2233 >        final Noop r = new Noop(m);
2234          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2235  
2236          g.complete(v2);
# Line 2648 | Line 2242 | public class CompletableFutureTest exten
2242          checkCompletedNormally(g, v2);
2243          checkCompletedNormally(h, null);
2244          assertEquals(1, r.invocationCount);
2245 <        }
2246 <    }
2245 >        }}
2246 >
2247      public void testRunAfterEither_normalCompletion3() {
2248          for (ExecutionMode m : ExecutionMode.values())
2249          for (Integer v1 : new Integer[] { 1, null })
2250 <        for (Integer v2 : new Integer[] { 2, null }) {
2251 <
2250 >        for (Integer v2 : new Integer[] { 2, null })
2251 >    {
2252          final CompletableFuture<Integer> f = new CompletableFuture<>();
2253          final CompletableFuture<Integer> g = new CompletableFuture<>();
2254 <        final Noop r = new Noop();
2254 >        final Noop r = new Noop(m);
2255  
2256          f.complete(v1);
2257          g.complete(v2);
# Line 2667 | Line 2261 | public class CompletableFutureTest exten
2261          checkCompletedNormally(f, v1);
2262          checkCompletedNormally(g, v2);
2263          assertEquals(1, r.invocationCount);
2264 <        }
2671 <    }
2264 >    }}
2265  
2266      /**
2267       * runAfterEither result completes exceptionally after exceptional
# Line 2676 | Line 2269 | public class CompletableFutureTest exten
2269       */
2270      public void testRunAfterEither_exceptionalCompletion1() {
2271          for (ExecutionMode m : ExecutionMode.values())
2272 <        for (Integer v1 : new Integer[] { 1, null }) {
2273 <
2272 >        for (Integer v1 : new Integer[] { 1, null })
2273 >    {
2274          final CompletableFuture<Integer> f = new CompletableFuture<>();
2275          final CompletableFuture<Integer> g = new CompletableFuture<>();
2276 <        final Noop r = new Noop();
2276 >        final Noop r = new Noop(m);
2277          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2278          final CFException ex = new CFException();
2279  
# Line 2692 | Line 2285 | public class CompletableFutureTest exten
2285          checkCompletedNormally(g, v1);
2286          checkCompletedWithWrappedCFException(f, ex);
2287          checkCompletedWithWrappedCFException(h, ex);
2288 <        }
2696 <    }
2288 >    }}
2289  
2290      public void testRunAfterEither_exceptionalCompletion2() {
2291          for (ExecutionMode m : ExecutionMode.values())
2292 <        for (Integer v1 : new Integer[] { 1, null }) {
2293 <
2292 >        for (Integer v1 : new Integer[] { 1, null })
2293 >    {
2294          final CompletableFuture<Integer> f = new CompletableFuture<>();
2295          final CompletableFuture<Integer> g = new CompletableFuture<>();
2296 <        final Noop r = new Noop();
2296 >        final Noop r = new Noop(m);
2297          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2298          final CFException ex = new CFException();
2299  
# Line 2713 | Line 2305 | public class CompletableFutureTest exten
2305          checkCompletedNormally(f, v1);
2306          checkCompletedWithWrappedCFException(g, ex);
2307          checkCompletedWithWrappedCFException(h, ex);
2308 <        }
2717 <    }
2308 >    }}
2309  
2310      public void testRunAfterEither_exceptionalCompletion3() {
2311          for (ExecutionMode m : ExecutionMode.values())
2312 <        for (Integer v1 : new Integer[] { 1, null }) {
2313 <
2312 >        for (Integer v1 : new Integer[] { 1, null })
2313 >    {
2314          final CompletableFuture<Integer> f = new CompletableFuture<>();
2315          final CompletableFuture<Integer> g = new CompletableFuture<>();
2316 <        final Noop r = new Noop();
2316 >        final Noop r = new Noop(m);
2317          final CFException ex = new CFException();
2318  
2319          g.completeExceptionally(ex);
# Line 2741 | Line 2332 | public class CompletableFutureTest exten
2332  
2333          checkCompletedWithWrappedCFException(g, ex);
2334          checkCompletedNormally(f, v1);
2335 <        }
2745 <    }
2335 >    }}
2336  
2337      public void testRunAfterEither_exceptionalCompletion4() {
2338          for (ExecutionMode m : ExecutionMode.values())
2339 <        for (Integer v1 : new Integer[] { 1, null }) {
2340 <
2339 >        for (Integer v1 : new Integer[] { 1, null })
2340 >    {
2341          final CompletableFuture<Integer> f = new CompletableFuture<>();
2342          final CompletableFuture<Integer> g = new CompletableFuture<>();
2343 <        final Noop r = new Noop();
2343 >        final Noop r = new Noop(m);
2344          final CFException ex = new CFException();
2345  
2346          f.completeExceptionally(ex);
# Line 2769 | Line 2359 | public class CompletableFutureTest exten
2359  
2360          checkCompletedWithWrappedCFException(f, ex);
2361          checkCompletedNormally(g, v1);
2362 <        }
2773 <    }
2362 >    }}
2363  
2364      /**
2365       * runAfterEither result completes exceptionally if action does
# Line 2778 | Line 2367 | public class CompletableFutureTest exten
2367      public void testRunAfterEither_actionFailed1() {
2368          for (ExecutionMode m : ExecutionMode.values())
2369          for (Integer v1 : new Integer[] { 1, null })
2370 <        for (Integer v2 : new Integer[] { 2, null }) {
2371 <
2370 >        for (Integer v2 : new Integer[] { 2, null })
2371 >    {
2372          final CompletableFuture<Integer> f = new CompletableFuture<>();
2373          final CompletableFuture<Integer> g = new CompletableFuture<>();
2374 <        final FailingNoop r = new FailingNoop();
2374 >        final FailingRunnable r = new FailingRunnable(m);
2375          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2376  
2377          f.complete(v1);
# Line 2790 | Line 2379 | public class CompletableFutureTest exten
2379          g.complete(v2);
2380          checkCompletedNormally(f, v1);
2381          checkCompletedNormally(g, v2);
2382 <        }
2794 <    }
2382 >    }}
2383  
2384      public void testRunAfterEither_actionFailed2() {
2385          for (ExecutionMode m : ExecutionMode.values())
2386          for (Integer v1 : new Integer[] { 1, null })
2387 <        for (Integer v2 : new Integer[] { 2, null }) {
2388 <
2387 >        for (Integer v2 : new Integer[] { 2, null })
2388 >    {
2389          final CompletableFuture<Integer> f = new CompletableFuture<>();
2390          final CompletableFuture<Integer> g = new CompletableFuture<>();
2391 <        final FailingNoop r = new FailingNoop();
2391 >        final FailingRunnable r = new FailingRunnable(m);
2392          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2393  
2394          g.complete(v2);
# Line 2808 | Line 2396 | public class CompletableFutureTest exten
2396          f.complete(v1);
2397          checkCompletedNormally(f, v1);
2398          checkCompletedNormally(g, v2);
2399 <        }
2812 <    }
2399 >    }}
2400  
2401      /**
2402       * runAfterEither result completes exceptionally if either source cancelled
# Line 2817 | Line 2404 | public class CompletableFutureTest exten
2404      public void testRunAfterEither_sourceCancelled1() {
2405          for (ExecutionMode m : ExecutionMode.values())
2406          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2407 <        for (Integer v1 : new Integer[] { 1, null }) {
2408 <
2407 >        for (Integer v1 : new Integer[] { 1, null })
2408 >    {
2409          final CompletableFuture<Integer> f = new CompletableFuture<>();
2410          final CompletableFuture<Integer> g = new CompletableFuture<>();
2411 <        final Noop r = new Noop();
2411 >        final Noop r = new Noop(m);
2412          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2413  
2414          assertTrue(f.cancel(mayInterruptIfRunning));
# Line 2832 | Line 2419 | public class CompletableFutureTest exten
2419          assertEquals(0, r.invocationCount);
2420          checkCompletedNormally(g, v1);
2421          checkCompletedWithWrappedCancellationException(h);
2422 <        }
2836 <    }
2422 >    }}
2423  
2424      public void testRunAfterEither_sourceCancelled2() {
2425          for (ExecutionMode m : ExecutionMode.values())
2426          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2427 <        for (Integer v1 : new Integer[] { 1, null }) {
2428 <
2427 >        for (Integer v1 : new Integer[] { 1, null })
2428 >    {
2429          final CompletableFuture<Integer> f = new CompletableFuture<>();
2430          final CompletableFuture<Integer> g = new CompletableFuture<>();
2431 <        final Noop r = new Noop();
2431 >        final Noop r = new Noop(m);
2432          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2433  
2434          assertTrue(g.cancel(mayInterruptIfRunning));
# Line 2853 | Line 2439 | public class CompletableFutureTest exten
2439          assertEquals(0, r.invocationCount);
2440          checkCompletedNormally(f, v1);
2441          checkCompletedWithWrappedCancellationException(h);
2442 <        }
2857 <    }
2442 >    }}
2443  
2444      public void testRunAfterEither_sourceCancelled3() {
2445          for (ExecutionMode m : ExecutionMode.values())
2446          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2447 <        for (Integer v1 : new Integer[] { 1, null }) {
2448 <
2447 >        for (Integer v1 : new Integer[] { 1, null })
2448 >    {
2449          final CompletableFuture<Integer> f = new CompletableFuture<>();
2450          final CompletableFuture<Integer> g = new CompletableFuture<>();
2451 <        final Noop r = new Noop();
2451 >        final Noop r = new Noop(m);
2452  
2453          assertTrue(g.cancel(mayInterruptIfRunning));
2454          f.complete(v1);
# Line 2881 | Line 2466 | public class CompletableFutureTest exten
2466  
2467          checkCancelled(g);
2468          checkCompletedNormally(f, v1);
2469 <        }
2885 <    }
2469 >    }}
2470  
2471      public void testRunAfterEither_sourceCancelled4() {
2472          for (ExecutionMode m : ExecutionMode.values())
2473          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2474 <        for (Integer v1 : new Integer[] { 1, null }) {
2475 <
2474 >        for (Integer v1 : new Integer[] { 1, null })
2475 >    {
2476          final CompletableFuture<Integer> f = new CompletableFuture<>();
2477          final CompletableFuture<Integer> g = new CompletableFuture<>();
2478 <        final Noop r = new Noop();
2478 >        final Noop r = new Noop(m);
2479  
2480          assertTrue(f.cancel(mayInterruptIfRunning));
2481          g.complete(v1);
# Line 2909 | Line 2493 | public class CompletableFutureTest exten
2493  
2494          checkCancelled(f);
2495          checkCompletedNormally(g, v1);
2496 <        }
2913 <    }
2496 >    }}
2497  
2498      /**
2499       * thenCompose result completes normally after normal completion of source
2500       */
2501 <    public void testThenCompose_normalCompletion1() {
2501 >    public void testThenCompose_normalCompletion() {
2502          for (ExecutionMode m : ExecutionMode.values())
2503 <        for (Integer v1 : new Integer[] { 1, null }) {
2504 <
2503 >        for (boolean createIncomplete : new boolean[] { true, false })
2504 >        for (Integer v1 : new Integer[] { 1, null })
2505 >    {
2506          final CompletableFuture<Integer> f = new CompletableFuture<>();
2507 <        final CompletableFutureInc r = new CompletableFutureInc();
2508 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2509 <        f.complete(v1);
2510 <        checkCompletedNormally(g, inc(v1));
2927 <        checkCompletedNormally(f, v1);
2928 <        assertEquals(1, r.invocationCount);
2929 <        }
2930 <    }
2931 <
2932 <    public void testThenCompose_normalCompletion2() {
2933 <        for (ExecutionMode m : ExecutionMode.values())
2934 <        for (Integer v1 : new Integer[] { 1, null }) {
2507 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2508 >        if (!createIncomplete) f.complete(v1);
2509 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2510 >        if (createIncomplete) f.complete(v1);
2511  
2936        final CompletableFuture<Integer> f = new CompletableFuture<>();
2937        final CompletableFutureInc r = new CompletableFutureInc();
2938        f.complete(v1);
2939        final CompletableFuture<Integer> g = f.thenCompose(r);
2512          checkCompletedNormally(g, inc(v1));
2513          checkCompletedNormally(f, v1);
2514          assertEquals(1, r.invocationCount);
2515 <        }
2944 <    }
2515 >    }}
2516  
2517      /**
2518       * thenCompose result completes exceptionally after exceptional
2519       * completion of source
2520       */
2521 <    public void testThenCompose_exceptionalCompletion1() {
2522 <        for (ExecutionMode m : ExecutionMode.values()) {
2523 <
2521 >    public void testThenCompose_exceptionalCompletion() {
2522 >        for (ExecutionMode m : ExecutionMode.values())
2523 >        for (boolean createIncomplete : new boolean[] { true, false })
2524 >    {
2525          final CFException ex = new CFException();
2526 <        final CompletableFutureInc r = new CompletableFutureInc();
2526 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2527          final CompletableFuture<Integer> f = new CompletableFuture<>();
2528 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2529 <        f.completeExceptionally(ex);
2530 <        checkCompletedWithWrappedCFException(g, ex);
2959 <        checkCompletedWithWrappedCFException(f, ex);
2960 <        }
2961 <    }
2962 <
2963 <    public void testThenCompose_exceptionalCompletion2() {
2964 <        for (ExecutionMode m : ExecutionMode.values()) {
2528 >        if (!createIncomplete) f.completeExceptionally(ex);
2529 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2530 >        if (createIncomplete) f.completeExceptionally(ex);
2531  
2966        final CFException ex = new CFException();
2967        final CompletableFuture<Integer> f = new CompletableFuture<>();
2968        f.completeExceptionally(ex);
2969        final CompletableFutureInc r = new CompletableFutureInc();
2970        final CompletableFuture<Integer> g = f.thenCompose(r);
2532          checkCompletedWithWrappedCFException(g, ex);
2533          checkCompletedWithWrappedCFException(f, ex);
2534 <        }
2535 <    }
2534 >        assertEquals(0, r.invocationCount);
2535 >    }}
2536  
2537      /**
2538       * thenCompose result completes exceptionally if action does
2539       */
2540 <    public void testThenCompose_actionFailed1() {
2540 >    public void testThenCompose_actionFailed() {
2541          for (ExecutionMode m : ExecutionMode.values())
2542 <        for (Integer v1 : new Integer[] { 1, null }) {
2543 <
2542 >        for (boolean createIncomplete : new boolean[] { true, false })
2543 >        for (Integer v1 : new Integer[] { 1, null })
2544 >    {
2545          final CompletableFuture<Integer> f = new CompletableFuture<>();
2546          final FailingCompletableFutureFunction r
2547 <            = new FailingCompletableFutureFunction();
2548 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2549 <        f.complete(v1);
2550 <        checkCompletedWithWrappedCFException(g);
2989 <        checkCompletedNormally(f, v1);
2990 <        }
2991 <    }
2992 <
2993 <    public void testThenCompose_actionFailed2() {
2994 <        for (ExecutionMode m : ExecutionMode.values())
2995 <        for (Integer v1 : new Integer[] { 1, null }) {
2547 >            = new FailingCompletableFutureFunction(m);
2548 >        if (!createIncomplete) f.complete(v1);
2549 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2550 >        if (createIncomplete) f.complete(v1);
2551  
2997        final CompletableFuture<Integer> f = new CompletableFuture<>();
2998        f.complete(v1);
2999        final FailingCompletableFutureFunction r
3000            = new FailingCompletableFutureFunction();
3001        final CompletableFuture<Integer> g = f.thenCompose(r);
2552          checkCompletedWithWrappedCFException(g);
2553          checkCompletedNormally(f, v1);
2554 <        }
3005 <    }
2554 >    }}
2555  
2556      /**
2557       * thenCompose result completes exceptionally if source cancelled
2558       */
2559 <    public void testThenCompose_sourceCancelled1() {
2559 >    public void testThenCompose_sourceCancelled() {
2560          for (ExecutionMode m : ExecutionMode.values())
2561 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
2562 <
2561 >        for (boolean createIncomplete : new boolean[] { true, false })
2562 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2563 >    {
2564          final CompletableFuture<Integer> f = new CompletableFuture<>();
2565 <        final CompletableFutureInc r = new CompletableFutureInc();
2566 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2567 <        assertTrue(f.cancel(mayInterruptIfRunning));
2568 <        checkCompletedWithWrappedCancellationException(g);
2569 <        checkCancelled(f);
2565 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2566 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2567 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2568 >        if (createIncomplete) {
2569 >            checkIncomplete(g);
2570 >            assertTrue(f.cancel(mayInterruptIfRunning));
2571          }
3021    }
3022
3023    public void testThenCompose_sourceCancelled2() {
3024        for (ExecutionMode m : ExecutionMode.values())
3025        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
2572  
3027        final CompletableFuture<Integer> f = new CompletableFuture<>();
3028        assertTrue(f.cancel(mayInterruptIfRunning));
3029        final CompletableFutureInc r = new CompletableFutureInc();
3030        final CompletableFuture<Integer> g = f.thenCompose(r);
2573          checkCompletedWithWrappedCancellationException(g);
2574          checkCancelled(f);
2575 <        }
3034 <    }
3035 <
3036 <    // asyncs
3037 <
3038 <    /**
3039 <     * thenRunAsync result completes normally after normal completion of source
3040 <     */
3041 <    public void testThenRunAsync() {
3042 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3043 <        Noop r = new Noop();
3044 <        CompletableFuture<Void> g = f.thenRunAsync(r);
3045 <        f.complete(null);
3046 <        checkCompletedNormally(g, null);
3047 <
3048 <        // reordered version
3049 <        f = new CompletableFuture<>();
3050 <        f.complete(null);
3051 <        r = new Noop();
3052 <        g = f.thenRunAsync(r);
3053 <        checkCompletedNormally(g, null);
3054 <    }
3055 <
3056 <    /**
3057 <     * thenRunAsync result completes exceptionally after exceptional
3058 <     * completion of source
3059 <     */
3060 <    public void testThenRunAsync2() {
3061 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3062 <        Noop r = new Noop();
3063 <        CompletableFuture<Void> g = f.thenRunAsync(r);
3064 <        f.completeExceptionally(new CFException());
3065 <        try {
3066 <            g.join();
3067 <            shouldThrow();
3068 <        } catch (CompletionException success) {}
3069 <        checkCompletedWithWrappedCFException(g);
3070 <    }
3071 <
3072 <    /**
3073 <     * thenRunAsync result completes exceptionally if action does
3074 <     */
3075 <    public void testThenRunAsync3() {
3076 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3077 <        FailingNoop r = new FailingNoop();
3078 <        CompletableFuture<Void> g = f.thenRunAsync(r);
3079 <        f.complete(null);
3080 <        checkCompletedWithWrappedCFException(g);
3081 <    }
3082 <
3083 <    /**
3084 <     * thenRunAsync result completes exceptionally if source cancelled
3085 <     */
3086 <    public void testThenRunAsync4() {
3087 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3088 <        Noop r = new Noop();
3089 <        CompletableFuture<Void> g = f.thenRunAsync(r);
3090 <        assertTrue(f.cancel(true));
3091 <        checkCompletedWithWrappedCancellationException(g);
3092 <    }
3093 <
3094 <    /**
3095 <     * thenApplyAsync result completes normally after normal completion of source
3096 <     */
3097 <    public void testThenApplyAsync() {
3098 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3099 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
3100 <        f.complete(one);
3101 <        checkCompletedNormally(g, two);
3102 <    }
3103 <
3104 <    /**
3105 <     * thenApplyAsync result completes exceptionally after exceptional
3106 <     * completion of source
3107 <     */
3108 <    public void testThenApplyAsync2() {
3109 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3110 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
3111 <        f.completeExceptionally(new CFException());
3112 <        checkCompletedWithWrappedCFException(g);
3113 <    }
3114 <
3115 <    /**
3116 <     * thenApplyAsync result completes exceptionally if action does
3117 <     */
3118 <    public void testThenApplyAsync3() {
3119 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3120 <        FailingFunction r = new FailingFunction();
3121 <        CompletableFuture<Integer> g = f.thenApplyAsync(r);
3122 <        f.complete(null);
3123 <        checkCompletedWithWrappedCFException(g);
3124 <    }
3125 <
3126 <    /**
3127 <     * thenApplyAsync result completes exceptionally if source cancelled
3128 <     */
3129 <    public void testThenApplyAsync4() {
3130 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3131 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
3132 <        assertTrue(f.cancel(true));
3133 <        checkCompletedWithWrappedCancellationException(g);
3134 <    }
3135 <
3136 <    /**
3137 <     * thenAcceptAsync result completes normally after normal
3138 <     * completion of source
3139 <     */
3140 <    public void testThenAcceptAsync() {
3141 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3142 <        IncAction r = new IncAction();
3143 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
3144 <        f.complete(one);
3145 <        checkCompletedNormally(g, null);
3146 <        assertEquals(r.value, (Integer) 2);
3147 <    }
3148 <
3149 <    /**
3150 <     * thenAcceptAsync result completes exceptionally after exceptional
3151 <     * completion of source
3152 <     */
3153 <    public void testThenAcceptAsync2() {
3154 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3155 <        IncAction r = new IncAction();
3156 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
3157 <        f.completeExceptionally(new CFException());
3158 <        checkCompletedWithWrappedCFException(g);
3159 <    }
3160 <
3161 <    /**
3162 <     * thenAcceptAsync result completes exceptionally if action does
3163 <     */
3164 <    public void testThenAcceptAsync3() {
3165 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3166 <        FailingConsumer r = new FailingConsumer();
3167 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
3168 <        f.complete(null);
3169 <        checkCompletedWithWrappedCFException(g);
3170 <    }
3171 <
3172 <    /**
3173 <     * thenAcceptAsync result completes exceptionally if source cancelled
3174 <     */
3175 <    public void testThenAcceptAsync4() {
3176 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3177 <        IncAction r = new IncAction();
3178 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
3179 <        assertTrue(f.cancel(true));
3180 <        checkCompletedWithWrappedCancellationException(g);
3181 <    }
3182 <
3183 <    // async with explicit executors
3184 <
3185 <    /**
3186 <     * thenRunAsync result completes normally after normal completion of source
3187 <     */
3188 <    public void testThenRunAsyncE() {
3189 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3190 <        Noop r = new Noop();
3191 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3192 <        f.complete(null);
3193 <        checkCompletedNormally(g, null);
3194 <
3195 <        // reordered version
3196 <        f = new CompletableFuture<>();
3197 <        f.complete(null);
3198 <        r = new Noop();
3199 <        g = f.thenRunAsync(r, new ThreadExecutor());
3200 <        checkCompletedNormally(g, null);
3201 <    }
3202 <
3203 <    /**
3204 <     * thenRunAsync result completes exceptionally after exceptional
3205 <     * completion of source
3206 <     */
3207 <    public void testThenRunAsync2E() {
3208 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3209 <        Noop r = new Noop();
3210 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3211 <        f.completeExceptionally(new CFException());
3212 <        try {
3213 <            g.join();
3214 <            shouldThrow();
3215 <        } catch (CompletionException success) {}
3216 <        checkCompletedWithWrappedCFException(g);
3217 <    }
3218 <
3219 <    /**
3220 <     * thenRunAsync result completes exceptionally if action does
3221 <     */
3222 <    public void testThenRunAsync3E() {
3223 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3224 <        FailingNoop r = new FailingNoop();
3225 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3226 <        f.complete(null);
3227 <        checkCompletedWithWrappedCFException(g);
3228 <    }
3229 <
3230 <    /**
3231 <     * thenRunAsync result completes exceptionally if source cancelled
3232 <     */
3233 <    public void testThenRunAsync4E() {
3234 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3235 <        Noop r = new Noop();
3236 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3237 <        assertTrue(f.cancel(true));
3238 <        checkCompletedWithWrappedCancellationException(g);
3239 <    }
3240 <
3241 <    /**
3242 <     * thenApplyAsync result completes normally after normal completion of source
3243 <     */
3244 <    public void testThenApplyAsyncE() {
3245 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3246 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
3247 <        f.complete(one);
3248 <        checkCompletedNormally(g, two);
3249 <    }
3250 <
3251 <    /**
3252 <     * thenApplyAsync result completes exceptionally after exceptional
3253 <     * completion of source
3254 <     */
3255 <    public void testThenApplyAsync2E() {
3256 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3257 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
3258 <        f.completeExceptionally(new CFException());
3259 <        checkCompletedWithWrappedCFException(g);
3260 <    }
3261 <
3262 <    /**
3263 <     * thenApplyAsync result completes exceptionally if action does
3264 <     */
3265 <    public void testThenApplyAsync3E() {
3266 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3267 <        FailingFunction r = new FailingFunction();
3268 <        CompletableFuture<Integer> g = f.thenApplyAsync(r, new ThreadExecutor());
3269 <        f.complete(null);
3270 <        checkCompletedWithWrappedCFException(g);
3271 <    }
3272 <
3273 <    /**
3274 <     * thenApplyAsync result completes exceptionally if source cancelled
3275 <     */
3276 <    public void testThenApplyAsync4E() {
3277 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3278 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
3279 <        assertTrue(f.cancel(true));
3280 <        checkCompletedWithWrappedCancellationException(g);
3281 <    }
3282 <
3283 <    /**
3284 <     * thenAcceptAsync result completes normally after normal
3285 <     * completion of source
3286 <     */
3287 <    public void testThenAcceptAsyncE() {
3288 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3289 <        IncAction r = new IncAction();
3290 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3291 <        f.complete(one);
3292 <        checkCompletedNormally(g, null);
3293 <        assertEquals(r.value, (Integer) 2);
3294 <    }
3295 <
3296 <    /**
3297 <     * thenAcceptAsync result completes exceptionally after exceptional
3298 <     * completion of source
3299 <     */
3300 <    public void testThenAcceptAsync2E() {
3301 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3302 <        IncAction r = new IncAction();
3303 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3304 <        f.completeExceptionally(new CFException());
3305 <        checkCompletedWithWrappedCFException(g);
3306 <    }
3307 <
3308 <    /**
3309 <     * thenAcceptAsync result completes exceptionally if action does
3310 <     */
3311 <    public void testThenAcceptAsync3E() {
3312 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3313 <        FailingConsumer r = new FailingConsumer();
3314 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3315 <        f.complete(null);
3316 <        checkCompletedWithWrappedCFException(g);
3317 <    }
3318 <
3319 <    /**
3320 <     * thenAcceptAsync result completes exceptionally if source cancelled
3321 <     */
3322 <    public void testThenAcceptAsync4E() {
3323 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3324 <        IncAction r = new IncAction();
3325 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3326 <        assertTrue(f.cancel(true));
3327 <        checkCompletedWithWrappedCancellationException(g);
3328 <    }
2575 >    }}
2576  
2577      // other static methods
2578  
# Line 3489 | Line 2736 | public class CompletableFutureTest exten
2736  
2737              () -> f.thenCompose(null),
2738              () -> f.thenComposeAsync(null),
2739 <            () -> f.thenComposeAsync(new CompletableFutureInc(), null),
2739 >            () -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null),
2740              () -> f.thenComposeAsync(null, exec),
2741  
2742              () -> f.exceptionally(null),
# Line 3607 | Line 2854 | public class CompletableFutureTest exten
2854      public void testWhenComplete_actionFailed() {
2855          for (boolean createIncomplete : new boolean[] { true, false })
2856          for (ExecutionMode m : ExecutionMode.values())
2857 <        for (Integer v1 : new Integer[] { 1, null }) {
2858 <
2857 >        for (Integer v1 : new Integer[] { 1, null })
2858 >    {
2859          final AtomicInteger a = new AtomicInteger(0);
2860          final CFException ex = new CFException();
2861          final CompletableFuture<Integer> f = new CompletableFuture<>();
# Line 3625 | Line 2872 | public class CompletableFutureTest exten
2872          checkCompletedNormally(f, v1);
2873          checkCompletedWithWrappedCFException(g, ex);
2874          assertEquals(1, a.get());
2875 <        }
3629 <    }
2875 >    }}
2876  
2877      /**
2878       * If a whenComplete action throws an exception when triggered by
# Line 3636 | Line 2882 | public class CompletableFutureTest exten
2882      public void testWhenComplete_actionFailedSourceFailed() {
2883          for (boolean createIncomplete : new boolean[] { true, false })
2884          for (ExecutionMode m : ExecutionMode.values())
2885 <        for (Integer v1 : new Integer[] { 1, null }) {
2886 <
2885 >        for (Integer v1 : new Integer[] { 1, null })
2886 >    {
2887          final AtomicInteger a = new AtomicInteger(0);
2888          final CFException ex1 = new CFException();
2889          final CFException ex2 = new CFException();
# Line 3657 | Line 2903 | public class CompletableFutureTest exten
2903          checkCompletedWithWrappedCFException(f, ex1);
2904          checkCompletedWithWrappedCFException(g, ex1);
2905          assertEquals(1, a.get());
2906 <        }
3661 <    }
2906 >    }}
2907  
2908   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines