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.56 by jsr166, Mon Jun 2 22:20:32 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 <T> CompletableFuture<Void> thenRun
507                  (CompletableFuture<T> f, Runnable a) {
508                  return f.thenRun(a);
# Line 521 | Line 566 | public class CompletableFutureTest exten
566              }
567          },
568  
569 <        DEFAULT_ASYNC {
569 >        ASYNC {
570 >            public void checkExecutionMode() {
571 >                assertSame(ForkJoinPool.commonPool(),
572 >                           ForkJoinTask.getPool());
573 >            }
574              public <T> CompletableFuture<Void> thenRun
575                  (CompletableFuture<T> f, Runnable a) {
576                  return f.thenRunAsync(a);
# Line 586 | Line 635 | public class CompletableFutureTest exten
635          },
636  
637          EXECUTOR {
638 +            public void checkExecutionMode() {
639 +                assertTrue(ThreadExecutor.startedCurrentThread());
640 +            }
641              public <T> CompletableFuture<Void> thenRun
642                  (CompletableFuture<T> f, Runnable a) {
643                  return f.thenRunAsync(a, new ThreadExecutor());
# Line 649 | Line 701 | public class CompletableFutureTest exten
701              }
702          };
703  
704 +        public abstract void checkExecutionMode();
705          public abstract <T> CompletableFuture<Void> thenRun
706              (CompletableFuture<T> f, Runnable a);
707          public abstract <T> CompletableFuture<Void> thenAccept
# Line 896 | Line 949 | public class CompletableFutureTest exten
949       * runAsync completes after running Runnable
950       */
951      public void testRunAsync() {
952 <        Noop r = new Noop();
952 >        Noop r = new Noop(ExecutionMode.ASYNC);
953          CompletableFuture<Void> f = CompletableFuture.runAsync(r);
954          assertNull(f.join());
955          assertEquals(1, r.invocationCount);
# Line 907 | Line 960 | public class CompletableFutureTest exten
960       * runAsync with executor completes after running Runnable
961       */
962      public void testRunAsync2() {
963 <        Noop r = new Noop();
963 >        Noop r = new Noop(ExecutionMode.EXECUTOR);
964          ThreadExecutor exec = new ThreadExecutor();
965          CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec);
966          assertNull(f.join());
# Line 920 | Line 973 | public class CompletableFutureTest exten
973       * failing runAsync completes exceptionally after running Runnable
974       */
975      public void testRunAsync3() {
976 <        FailingNoop r = new FailingNoop();
976 >        FailingRunnable r = new FailingRunnable(ExecutionMode.ASYNC);
977          CompletableFuture<Void> f = CompletableFuture.runAsync(r);
978          checkCompletedWithWrappedCFException(f);
979          assertEquals(1, r.invocationCount);
# Line 950 | Line 1003 | public class CompletableFutureTest exten
1003       * Failing supplyAsync completes exceptionally
1004       */
1005      public void testSupplyAsync3() {
1006 <        FailingSupplier r = new FailingSupplier();
1006 >        FailingSupplier r = new FailingSupplier(ExecutionMode.ASYNC);
1007          CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r);
1008          checkCompletedWithWrappedCFException(f);
1009          assertEquals(1, r.invocationCount);
# Line 961 | Line 1014 | public class CompletableFutureTest exten
1014      /**
1015       * thenRun result completes normally after normal completion of source
1016       */
1017 <    public void testThenRun() {
1018 <        CompletableFuture<Integer> f;
1019 <        CompletableFuture<Void> g;
1020 <        Noop r;
1021 <
1022 <        f = new CompletableFuture<>();
1023 <        g = f.thenRun(r = new Noop());
1024 <        f.complete(null);
1025 <        checkCompletedNormally(g, null);
1026 <        assertEquals(1, r.invocationCount);
1017 >    public void testThenRun_normalCompletion() {
1018 >        for (ExecutionMode m : ExecutionMode.values())
1019 >        for (boolean createIncomplete : new boolean[] { true, false })
1020 >        for (Integer v1 : new Integer[] { 1, null })
1021 >    {
1022 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1023 >        final Noop r = new Noop(m);
1024 >        if (!createIncomplete) f.complete(v1);
1025 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1026 >        if (createIncomplete) {
1027 >            checkIncomplete(g);
1028 >            f.complete(v1);
1029 >        }
1030  
975        f = new CompletableFuture<>();
976        f.complete(null);
977        g = f.thenRun(r = new Noop());
1031          checkCompletedNormally(g, null);
1032 +        checkCompletedNormally(f, v1);
1033          assertEquals(1, r.invocationCount);
1034 <    }
1034 >    }}
1035  
1036      /**
1037       * thenRun result completes exceptionally after exceptional
1038       * completion of source
1039       */
1040 <    public void testThenRun2() {
1041 <        CompletableFuture<Integer> f;
1042 <        CompletableFuture<Void> g;
1043 <        Noop r;
1044 <
1045 <        f = new CompletableFuture<>();
1046 <        g = f.thenRun(r = new Noop());
1047 <        f.completeExceptionally(new CFException());
1048 <        checkCompletedWithWrappedCFException(g);
1049 <        assertEquals(0, r.invocationCount);
1040 >    public void testThenRun_exceptionalCompletion() {
1041 >        for (ExecutionMode m : ExecutionMode.values())
1042 >        for (boolean createIncomplete : new boolean[] { true, false })
1043 >    {
1044 >        final CFException ex = new CFException();
1045 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1046 >        final Noop r = new Noop(m);
1047 >        if (!createIncomplete) f.completeExceptionally(ex);
1048 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1049 >        if (createIncomplete) {
1050 >            checkIncomplete(g);
1051 >            f.completeExceptionally(ex);
1052 >        }
1053  
1054 <        f = new CompletableFuture<>();
1055 <        f.completeExceptionally(new CFException());
999 <        g = f.thenRun(r = new Noop());
1000 <        checkCompletedWithWrappedCFException(g);
1054 >        checkCompletedWithWrappedCFException(g, ex);
1055 >        checkCompletedWithWrappedCFException(f, ex);
1056          assertEquals(0, r.invocationCount);
1057 <    }
1057 >    }}
1058  
1059      /**
1060 <     * thenRun result completes exceptionally if action does
1060 >     * thenRun result completes exceptionally if source cancelled
1061       */
1062 <    public void testThenRun3() {
1063 <        CompletableFuture<Integer> f;
1064 <        CompletableFuture<Void> g;
1065 <        FailingNoop r;
1066 <
1067 <        f = new CompletableFuture<>();
1068 <        g = f.thenRun(r = new FailingNoop());
1069 <        f.complete(null);
1070 <        checkCompletedWithWrappedCFException(g);
1062 >    public void testThenRun_sourceCancelled() {
1063 >        for (ExecutionMode m : ExecutionMode.values())
1064 >        for (boolean createIncomplete : new boolean[] { true, false })
1065 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1066 >    {
1067 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1068 >        final Noop r = new Noop(m);
1069 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1070 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1071 >        if (createIncomplete) {
1072 >            checkIncomplete(g);
1073 >            assertTrue(f.cancel(mayInterruptIfRunning));
1074 >        }
1075  
1076 <        f = new CompletableFuture<>();
1077 <        f.complete(null);
1078 <        g = f.thenRun(r = new FailingNoop());
1079 <        checkCompletedWithWrappedCFException(g);
1021 <    }
1076 >        checkCompletedWithWrappedCancellationException(g);
1077 >        checkCancelled(f);
1078 >        assertEquals(0, r.invocationCount);
1079 >    }}
1080  
1081      /**
1082 <     * thenRun result completes exceptionally if source cancelled
1082 >     * thenRun result completes exceptionally if action does
1083       */
1084 <    public void testThenRun4() {
1085 <        CompletableFuture<Integer> f;
1086 <        CompletableFuture<Void> g;
1087 <        Noop r;
1088 <
1089 <        f = new CompletableFuture<>();
1090 <        g = f.thenRun(r = new Noop());
1091 <        assertTrue(f.cancel(true));
1092 <        checkCompletedWithWrappedCancellationException(g);
1084 >    public void testThenRun_actionFailed() {
1085 >        for (ExecutionMode m : ExecutionMode.values())
1086 >        for (boolean createIncomplete : new boolean[] { true, false })
1087 >        for (Integer v1 : new Integer[] { 1, null })
1088 >    {
1089 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1090 >        final FailingRunnable r = new FailingRunnable(m);
1091 >        if (!createIncomplete) f.complete(v1);
1092 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1093 >        if (createIncomplete) {
1094 >            checkIncomplete(g);
1095 >            f.complete(v1);
1096 >        }
1097  
1098 <        f = new CompletableFuture<>();
1099 <        assertTrue(f.cancel(true));
1100 <        g = f.thenRun(r = new Noop());
1039 <        checkCompletedWithWrappedCancellationException(g);
1040 <    }
1098 >        checkCompletedWithWrappedCFException(g);
1099 >        checkCompletedNormally(f, v1);
1100 >    }}
1101  
1102      /**
1103       * thenApply result completes normally after normal completion of source
1104       */
1105 <    public void testThenApply() {
1106 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1107 <        CompletableFuture<Integer> g = f.thenApply(inc);
1108 <        f.complete(one);
1109 <        checkCompletedNormally(g, two);
1110 <    }
1105 >    public void testThenApply_normalCompletion() {
1106 >        for (ExecutionMode m : ExecutionMode.values())
1107 >        for (boolean createIncomplete : new boolean[] { true, false })
1108 >        for (Integer v1 : new Integer[] { 1, null })
1109 >    {
1110 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1111 >        final IncFunction r = new IncFunction(m);
1112 >        if (!createIncomplete) f.complete(v1);
1113 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1114 >        if (createIncomplete) {
1115 >            checkIncomplete(g);
1116 >            f.complete(v1);
1117 >        }
1118 >
1119 >        checkCompletedNormally(g, inc(v1));
1120 >        checkCompletedNormally(f, v1);
1121 >        assertEquals(1, r.invocationCount);
1122 >    }}
1123  
1124      /**
1125       * thenApply result completes exceptionally after exceptional
1126       * completion of source
1127       */
1128 <    public void testThenApply2() {
1129 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1130 <        CompletableFuture<Integer> g = f.thenApply(inc);
1131 <        f.completeExceptionally(new CFException());
1132 <        checkCompletedWithWrappedCFException(g);
1133 <    }
1128 >    public void testThenApply_exceptionalCompletion() {
1129 >        for (ExecutionMode m : ExecutionMode.values())
1130 >        for (boolean createIncomplete : new boolean[] { true, false })
1131 >    {
1132 >        final CFException ex = new CFException();
1133 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1134 >        final IncFunction r = new IncFunction(m);
1135 >        if (!createIncomplete) f.completeExceptionally(ex);
1136 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1137 >        if (createIncomplete) {
1138 >            checkIncomplete(g);
1139 >            f.completeExceptionally(ex);
1140 >        }
1141  
1142 <    /**
1143 <     * thenApply result completes exceptionally if action does
1144 <     */
1145 <    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 <    }
1142 >        checkCompletedWithWrappedCFException(g, ex);
1143 >        checkCompletedWithWrappedCFException(f, ex);
1144 >        assertEquals(0, r.invocationCount);
1145 >    }}
1146  
1147      /**
1148       * thenApply result completes exceptionally if source cancelled
1149       */
1150 <    public void testThenApply4() {
1151 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1152 <        CompletableFuture<Integer> g = f.thenApply(inc);
1153 <        assertTrue(f.cancel(true));
1150 >    public void testThenApply_sourceCancelled() {
1151 >        for (ExecutionMode m : ExecutionMode.values())
1152 >        for (boolean createIncomplete : new boolean[] { true, false })
1153 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1154 >    {
1155 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1156 >        final IncFunction r = new IncFunction(m);
1157 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1158 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1159 >        if (createIncomplete) {
1160 >            checkIncomplete(g);
1161 >            assertTrue(f.cancel(mayInterruptIfRunning));
1162 >        }
1163 >
1164          checkCompletedWithWrappedCancellationException(g);
1165 <    }
1165 >        checkCancelled(f);
1166 >        assertEquals(0, r.invocationCount);
1167 >    }}
1168 >
1169 >    /**
1170 >     * thenApply result completes exceptionally if action does
1171 >     */
1172 >    public void testThenApply_actionFailed() {
1173 >        for (ExecutionMode m : ExecutionMode.values())
1174 >        for (boolean createIncomplete : new boolean[] { true, false })
1175 >        for (Integer v1 : new Integer[] { 1, null })
1176 >    {
1177 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1178 >        final FailingFunction r = new FailingFunction(m);
1179 >        if (!createIncomplete) f.complete(v1);
1180 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1181 >        if (createIncomplete) {
1182 >            checkIncomplete(g);
1183 >            f.complete(v1);
1184 >        }
1185 >
1186 >        checkCompletedWithWrappedCFException(g);
1187 >        checkCompletedNormally(f, v1);
1188 >    }}
1189  
1190      /**
1191       * thenAccept result completes normally after normal completion of source
1192       */
1193 <    public void testThenAccept() {
1194 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1195 <        IncAction r = new IncAction();
1196 <        CompletableFuture<Void> g = f.thenAccept(r);
1197 <        f.complete(one);
1193 >    public void testThenAccept_normalCompletion() {
1194 >        for (ExecutionMode m : ExecutionMode.values())
1195 >        for (boolean createIncomplete : new boolean[] { true, false })
1196 >        for (Integer v1 : new Integer[] { 1, null })
1197 >    {
1198 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1199 >        final IncAction r = new IncAction();
1200 >        if (!createIncomplete) f.complete(v1);
1201 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1202 >        if (createIncomplete) {
1203 >            checkIncomplete(g);
1204 >            f.complete(v1);
1205 >        }
1206 >
1207          checkCompletedNormally(g, null);
1208 <        assertEquals(r.value, (Integer) 2);
1209 <    }
1208 >        checkCompletedNormally(f, v1);
1209 >        assertEquals(1, r.invocationCount);
1210 >        assertEquals(inc(v1), r.value);
1211 >    }}
1212  
1213      /**
1214       * thenAccept result completes exceptionally after exceptional
1215       * completion of source
1216       */
1217 <    public void testThenAccept2() {
1218 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1219 <        IncAction r = new IncAction();
1220 <        CompletableFuture<Void> g = f.thenAccept(r);
1221 <        f.completeExceptionally(new CFException());
1222 <        checkCompletedWithWrappedCFException(g);
1223 <    }
1217 >    public void testThenAccept_exceptionalCompletion() {
1218 >        for (ExecutionMode m : ExecutionMode.values())
1219 >        for (boolean createIncomplete : new boolean[] { true, false })
1220 >    {
1221 >        final CFException ex = new CFException();
1222 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1223 >        final IncAction r = new IncAction();
1224 >        if (!createIncomplete) f.completeExceptionally(ex);
1225 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1226 >        if (createIncomplete) {
1227 >            checkIncomplete(g);
1228 >            f.completeExceptionally(ex);
1229 >        }
1230 >
1231 >        checkCompletedWithWrappedCFException(g, ex);
1232 >        checkCompletedWithWrappedCFException(f, ex);
1233 >        assertEquals(0, r.invocationCount);
1234 >    }}
1235  
1236      /**
1237       * thenAccept result completes exceptionally if action does
1238       */
1239 <    public void testThenAccept3() {
1240 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1241 <        FailingConsumer r = new FailingConsumer();
1242 <        CompletableFuture<Void> g = f.thenAccept(r);
1243 <        f.complete(one);
1239 >    public void testThenAccept_actionFailed() {
1240 >        for (ExecutionMode m : ExecutionMode.values())
1241 >        for (boolean createIncomplete : new boolean[] { true, false })
1242 >        for (Integer v1 : new Integer[] { 1, null })
1243 >    {
1244 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1245 >        final FailingConsumer r = new FailingConsumer(m);
1246 >        if (!createIncomplete) f.complete(v1);
1247 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1248 >        if (createIncomplete) {
1249 >            checkIncomplete(g);
1250 >            f.complete(v1);
1251 >        }
1252 >
1253          checkCompletedWithWrappedCFException(g);
1254 <        assertEquals(1, r.invocationCount);
1255 <    }
1254 >        checkCompletedNormally(f, v1);
1255 >    }}
1256  
1257      /**
1258       * thenAccept result completes exceptionally if source cancelled
1259       */
1260 <    public void testThenAccept4() {
1261 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1262 <        IncAction r = new IncAction();
1263 <        CompletableFuture<Void> g = f.thenAccept(r);
1264 <        assertTrue(f.cancel(true));
1260 >    public void testThenAccept_sourceCancelled() {
1261 >        for (ExecutionMode m : ExecutionMode.values())
1262 >        for (boolean createIncomplete : new boolean[] { true, false })
1263 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1264 >    {
1265 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1266 >        final IncAction r = new IncAction();
1267 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1268 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1269 >        if (createIncomplete) {
1270 >            checkIncomplete(g);
1271 >            assertTrue(f.cancel(mayInterruptIfRunning));
1272 >        }
1273 >
1274          checkCompletedWithWrappedCancellationException(g);
1275 <    }
1275 >        checkCancelled(f);
1276 >        assertEquals(0, r.invocationCount);
1277 >    }}
1278  
1279      /**
1280       * thenCombine result completes normally after normal completion
1281       * of sources
1282       */
1283 <    public void testThenCombine_normalCompletion1() {
1283 >    public void testThenCombine_normalCompletion() {
1284 >        for (ExecutionMode m : ExecutionMode.values())
1285          for (boolean createIncomplete : new boolean[] { true, false })
1286          for (boolean fFirst : new boolean[] { true, false })
1137        for (ExecutionMode m : ExecutionMode.values())
1287          for (Integer v1 : new Integer[] { 1, null })
1288 <        for (Integer v2 : new Integer[] { 2, null }) {
1289 <
1288 >        for (Integer v2 : new Integer[] { 2, null })
1289 >    {
1290          final CompletableFuture<Integer> f = new CompletableFuture<>();
1291          final CompletableFuture<Integer> g = new CompletableFuture<>();
1292 <        final SubtractFunction r = new SubtractFunction();
1144 <        CompletableFuture<Integer> h = null;
1145 <        if (createIncomplete) h = m.thenCombine(f, g, r);
1292 >        final SubtractFunction r = new SubtractFunction(m);
1293  
1294 <        if (fFirst)
1295 <            f.complete(v1);
1296 <        else
1297 <            g.complete(v2);
1298 <        if (createIncomplete) checkIncomplete(h);
1299 <        assertEquals(0, r.invocationCount);
1300 <        if (!fFirst)
1301 <            f.complete(v1);
1302 <        else
1156 <            g.complete(v2);
1157 <        if (!createIncomplete) h = m.thenCombine(f, g, r);
1294 >        if (fFirst) f.complete(v1); else g.complete(v2);
1295 >        if (!createIncomplete)
1296 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1297 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1298 >        if (createIncomplete) {
1299 >            checkIncomplete(h);
1300 >            assertEquals(0, r.invocationCount);
1301 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1302 >        }
1303  
1304          checkCompletedNormally(h, subtract(v1, v2));
1305          checkCompletedNormally(f, v1);
1306          checkCompletedNormally(g, v2);
1307          assertEquals(1, r.invocationCount);
1308 <        }
1164 <    }
1308 >    }}
1309  
1310      /**
1311       * thenCombine result completes exceptionally after exceptional
1312       * completion of either source
1313       */
1314 <    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() {
1192 <        for (ExecutionMode m : ExecutionMode.values())
1193 <        for (Integer v1 : new Integer[] { 1, null }) {
1194 <
1195 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1196 <        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() {
1314 >    public void testThenCombine_exceptionalCompletion() {
1315          for (ExecutionMode m : ExecutionMode.values())
1316 <        for (Integer v1 : new Integer[] { 1, null }) {
1317 <
1316 >        for (boolean createIncomplete : new boolean[] { true, false })
1317 >        for (boolean fFirst : new boolean[] { true, false })
1318 >        for (Integer v1 : new Integer[] { 1, null })
1319 >    {
1320          final CompletableFuture<Integer> f = new CompletableFuture<>();
1321          final CompletableFuture<Integer> g = new CompletableFuture<>();
1218        final SubtractFunction r = new SubtractFunction();
1322          final CFException ex = new CFException();
1323 +        final SubtractFunction r = new SubtractFunction(m);
1324  
1325 <        g.completeExceptionally(ex);
1326 <        f.complete(v1);
1325 >        (fFirst ? f : g).complete(v1);
1326 >        if (!createIncomplete)
1327 >            (!fFirst ? f : g).completeExceptionally(ex);
1328          final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1329 <
1330 <        checkCompletedWithWrappedCFException(h, ex);
1331 <        checkCompletedWithWrappedCFException(g, ex);
1227 <        assertEquals(0, r.invocationCount);
1228 <        checkCompletedNormally(f, v1);
1329 >        if (createIncomplete) {
1330 >            checkIncomplete(h);
1331 >            (!fFirst ? f : g).completeExceptionally(ex);
1332          }
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);
1333  
1334          checkCompletedWithWrappedCFException(h, ex);
1246        checkCompletedWithWrappedCFException(f, ex);
1335          assertEquals(0, r.invocationCount);
1336 <        checkCompletedNormally(g, v1);
1337 <        }
1338 <    }
1336 >        checkCompletedNormally(fFirst ? f : g, v1);
1337 >        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1338 >    }}
1339  
1340      /**
1341       * thenCombine result completes exceptionally if action does
1342       */
1343 <    public void testThenCombine_actionFailed1() {
1343 >    public void testThenCombine_actionFailed() {
1344          for (ExecutionMode m : ExecutionMode.values())
1345 +        for (boolean fFirst : new boolean[] { true, false })
1346          for (Integer v1 : new Integer[] { 1, null })
1347 <        for (Integer v2 : new Integer[] { 2, null }) {
1348 <
1347 >        for (Integer v2 : new Integer[] { 2, null })
1348 >    {
1349          final CompletableFuture<Integer> f = new CompletableFuture<>();
1350          final CompletableFuture<Integer> g = new CompletableFuture<>();
1351 <        final FailingBiFunction r = new FailingBiFunction();
1351 >        final FailingBiFunction r = new FailingBiFunction(m);
1352          final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1353  
1354 <        f.complete(v1);
1355 <        checkIncomplete(h);
1356 <        g.complete(v2);
1357 <
1358 <        checkCompletedWithWrappedCFException(h);
1359 <        checkCompletedNormally(f, v1);
1271 <        checkCompletedNormally(g, v2);
1354 >        if (fFirst) {
1355 >            f.complete(v1);
1356 >            g.complete(v2);
1357 >        } else {
1358 >            g.complete(v2);
1359 >            f.complete(v1);
1360          }
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);
1361  
1362          checkCompletedWithWrappedCFException(h);
1363          checkCompletedNormally(f, v1);
1364          checkCompletedNormally(g, v2);
1365 <        }
1293 <    }
1365 >    }}
1366  
1367      /**
1368       * thenCombine result completes exceptionally if either source cancelled
1369       */
1370 <    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() {
1370 >    public void testThenCombine_sourceCancelled() {
1371          for (ExecutionMode m : ExecutionMode.values())
1372          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1373 <        for (Integer v1 : new Integer[] { 1, null }) {
1374 <
1373 >        for (boolean createIncomplete : new boolean[] { true, false })
1374 >        for (boolean fFirst : new boolean[] { true, false })
1375 >        for (Integer v1 : new Integer[] { 1, null })
1376 >    {
1377          final CompletableFuture<Integer> f = new CompletableFuture<>();
1378          final CompletableFuture<Integer> g = new CompletableFuture<>();
1379 <        final SubtractFunction r = new SubtractFunction();
1379 >        final SubtractFunction r = new SubtractFunction(m);
1380  
1381 <        assertTrue(g.cancel(mayInterruptIfRunning));
1382 <        f.complete(v1);
1381 >        (fFirst ? f : g).complete(v1);
1382 >        if (!createIncomplete)
1383 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1384          final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1385 <
1386 <        checkCompletedWithWrappedCancellationException(h);
1387 <        checkCancelled(g);
1355 <        assertEquals(0, r.invocationCount);
1356 <        checkCompletedNormally(f, v1);
1385 >        if (createIncomplete) {
1386 >            checkIncomplete(h);
1387 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1388          }
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);
1389  
1390          checkCompletedWithWrappedCancellationException(h);
1391 <        checkCancelled(f);
1391 >        checkCancelled(!fFirst ? f : g);
1392          assertEquals(0, r.invocationCount);
1393 <        checkCompletedNormally(g, v1);
1394 <        }
1378 <    }
1393 >        checkCompletedNormally(fFirst ? f : g, v1);
1394 >    }}
1395  
1396      /**
1397       * thenAcceptBoth result completes normally after normal
1398       * completion of sources
1399       */
1400 <    public void testThenAcceptBoth_normalCompletion1() {
1385 <        for (ExecutionMode m : ExecutionMode.values())
1386 <        for (Integer v1 : new Integer[] { 1, null })
1387 <        for (Integer v2 : new Integer[] { 2, null }) {
1388 <
1389 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1390 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1391 <        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);
1398 <
1399 <        checkCompletedNormally(h, null);
1400 <        assertEquals(subtract(v1, v2), r.value);
1401 <        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();
1414 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1415 <
1416 <        g.complete(v2);
1417 <        checkIncomplete(h);
1418 <        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);
1425 <        }
1426 <    }
1427 <
1428 <    public void testThenAcceptBoth_normalCompletion3() {
1400 >    public void testThenAcceptBoth_normalCompletion() {
1401          for (ExecutionMode m : ExecutionMode.values())
1402 +        for (boolean createIncomplete : new boolean[] { true, false })
1403 +        for (boolean fFirst : new boolean[] { true, false })
1404          for (Integer v1 : new Integer[] { 1, null })
1405 <        for (Integer v2 : new Integer[] { 2, null }) {
1406 <
1405 >        for (Integer v2 : new Integer[] { 2, null })
1406 >    {
1407          final CompletableFuture<Integer> f = new CompletableFuture<>();
1408          final CompletableFuture<Integer> g = new CompletableFuture<>();
1409 <        final SubtractAction r = new SubtractAction();
1409 >        final SubtractAction r = new SubtractAction(m);
1410  
1411 <        g.complete(v2);
1412 <        f.complete(v1);
1411 >        if (fFirst) f.complete(v1); else g.complete(v2);
1412 >        if (!createIncomplete)
1413 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1414          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1415 <
1416 <        checkCompletedNormally(h, null);
1417 <        assertEquals(subtract(v1, v2), r.value);
1418 <        checkCompletedNormally(f, v1);
1444 <        checkCompletedNormally(g, v2);
1415 >        if (createIncomplete) {
1416 >            checkIncomplete(h);
1417 >            assertEquals(0, r.invocationCount);
1418 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1419          }
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);
1420  
1421          checkCompletedNormally(h, null);
1422          assertEquals(subtract(v1, v2), r.value);
1423          checkCompletedNormally(f, v1);
1424          checkCompletedNormally(g, v2);
1425 <        }
1466 <    }
1425 >    }}
1426  
1427      /**
1428       * thenAcceptBoth result completes exceptionally after exceptional
1429       * completion of either source
1430       */
1431 <    public void testThenAcceptBoth_exceptionalCompletion1() {
1431 >    public void testThenAcceptBoth_exceptionalCompletion() {
1432          for (ExecutionMode m : ExecutionMode.values())
1433 <        for (Integer v1 : new Integer[] { 1, null }) {
1434 <
1435 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1436 <        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 <
1433 >        for (boolean createIncomplete : new boolean[] { true, false })
1434 >        for (boolean fFirst : new boolean[] { true, false })
1435 >        for (Integer v1 : new Integer[] { 1, null })
1436 >    {
1437          final CompletableFuture<Integer> f = new CompletableFuture<>();
1438          final CompletableFuture<Integer> g = new CompletableFuture<>();
1520        final SubtractAction r = new SubtractAction();
1439          final CFException ex = new CFException();
1440 +        final SubtractAction r = new SubtractAction(m);
1441  
1442 <        g.completeExceptionally(ex);
1443 <        f.complete(v1);
1442 >        (fFirst ? f : g).complete(v1);
1443 >        if (!createIncomplete)
1444 >            (!fFirst ? f : g).completeExceptionally(ex);
1445          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1446 <
1447 <        checkCompletedWithWrappedCFException(h, ex);
1448 <        checkCompletedWithWrappedCFException(g, ex);
1529 <        assertEquals(0, r.invocationCount);
1530 <        checkCompletedNormally(f, v1);
1446 >        if (createIncomplete) {
1447 >            checkIncomplete(h);
1448 >            (!fFirst ? f : g).completeExceptionally(ex);
1449          }
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);
1450  
1451          checkCompletedWithWrappedCFException(h, ex);
1548        checkCompletedWithWrappedCFException(f, ex);
1452          assertEquals(0, r.invocationCount);
1453 <        checkCompletedNormally(g, v1);
1454 <        }
1455 <    }
1453 >        checkCompletedNormally(fFirst ? f : g, v1);
1454 >        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1455 >    }}
1456  
1457      /**
1458       * thenAcceptBoth result completes exceptionally if action does
1459       */
1460 <    public void testThenAcceptBoth_actionFailed1() {
1460 >    public void testThenAcceptBoth_actionFailed() {
1461          for (ExecutionMode m : ExecutionMode.values())
1462 +        for (boolean fFirst : new boolean[] { true, false })
1463          for (Integer v1 : new Integer[] { 1, null })
1464 <        for (Integer v2 : new Integer[] { 2, null }) {
1465 <
1464 >        for (Integer v2 : new Integer[] { 2, null })
1465 >    {
1466          final CompletableFuture<Integer> f = new CompletableFuture<>();
1467          final CompletableFuture<Integer> g = new CompletableFuture<>();
1468 <        final FailingBiConsumer r = new FailingBiConsumer();
1468 >        final FailingBiConsumer r = new FailingBiConsumer(m);
1469          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1470  
1471 <        f.complete(v1);
1472 <        checkIncomplete(h);
1473 <        g.complete(v2);
1474 <
1475 <        checkCompletedWithWrappedCFException(h);
1476 <        checkCompletedNormally(f, v1);
1573 <        checkCompletedNormally(g, v2);
1471 >        if (fFirst) {
1472 >            f.complete(v1);
1473 >            g.complete(v2);
1474 >        } else {
1475 >            g.complete(v2);
1476 >            f.complete(v1);
1477          }
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);
1478  
1479          checkCompletedWithWrappedCFException(h);
1480          checkCompletedNormally(f, v1);
1481          checkCompletedNormally(g, v2);
1482 <        }
1595 <    }
1482 >    }}
1483  
1484      /**
1485       * thenAcceptBoth result completes exceptionally if either source cancelled
1486       */
1487 <    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() {
1487 >    public void testThenAcceptBoth_sourceCancelled() {
1488          for (ExecutionMode m : ExecutionMode.values())
1489          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1490 <        for (Integer v1 : new Integer[] { 1, null }) {
1491 <
1490 >        for (boolean createIncomplete : new boolean[] { true, false })
1491 >        for (boolean fFirst : new boolean[] { true, false })
1492 >        for (Integer v1 : new Integer[] { 1, null })
1493 >    {
1494          final CompletableFuture<Integer> f = new CompletableFuture<>();
1495          final CompletableFuture<Integer> g = new CompletableFuture<>();
1496 <        final SubtractAction r = new SubtractAction();
1496 >        final SubtractAction r = new SubtractAction(m);
1497  
1498 <        assertTrue(g.cancel(mayInterruptIfRunning));
1499 <        f.complete(v1);
1498 >        (fFirst ? f : g).complete(v1);
1499 >        if (!createIncomplete)
1500 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1501          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1502 <
1503 <        checkCompletedWithWrappedCancellationException(h);
1504 <        checkCancelled(g);
1657 <        assertEquals(0, r.invocationCount);
1658 <        checkCompletedNormally(f, v1);
1502 >        if (createIncomplete) {
1503 >            checkIncomplete(h);
1504 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1505          }
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);
1506  
1507          checkCompletedWithWrappedCancellationException(h);
1508 <        checkCancelled(f);
1508 >        checkCancelled(!fFirst ? f : g);
1509          assertEquals(0, r.invocationCount);
1510 <        checkCompletedNormally(g, v1);
1511 <        }
1680 <    }
1510 >        checkCompletedNormally(fFirst ? f : g, v1);
1511 >    }}
1512  
1513      /**
1514       * runAfterBoth result completes normally after normal
1515       * completion of sources
1516       */
1517 <    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() {
1517 >    public void testRunAfterBoth_normalCompletion() {
1518          for (ExecutionMode m : ExecutionMode.values())
1519 +        for (boolean createIncomplete : new boolean[] { true, false })
1520 +        for (boolean fFirst : new boolean[] { true, false })
1521          for (Integer v1 : new Integer[] { 1, null })
1522 <        for (Integer v2 : new Integer[] { 2, null }) {
1523 <
1522 >        for (Integer v2 : new Integer[] { 2, null })
1523 >    {
1524          final CompletableFuture<Integer> f = new CompletableFuture<>();
1525          final CompletableFuture<Integer> g = new CompletableFuture<>();
1526 <        final Noop r = new Noop();
1526 >        final Noop r = new Noop(m);
1527  
1528 <        g.complete(v2);
1529 <        f.complete(v1);
1528 >        if (fFirst) f.complete(v1); else g.complete(v2);
1529 >        if (!createIncomplete)
1530 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1531          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1532 <
1533 <        checkCompletedNormally(h, null);
1534 <        assertEquals(1, r.invocationCount);
1535 <        checkCompletedNormally(f, v1);
1746 <        checkCompletedNormally(g, v2);
1532 >        if (createIncomplete) {
1533 >            checkIncomplete(h);
1534 >            assertEquals(0, r.invocationCount);
1535 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1536          }
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);
1537  
1538          checkCompletedNormally(h, null);
1539          assertEquals(1, r.invocationCount);
1540          checkCompletedNormally(f, v1);
1541          checkCompletedNormally(g, v2);
1542 <        }
1768 <    }
1542 >    }}
1543  
1544      /**
1545       * runAfterBoth result completes exceptionally after exceptional
1546       * completion of either source
1547       */
1548 <    public void testRunAfterBoth_exceptionalCompletion1() {
1548 >    public void testRunAfterBoth_exceptionalCompletion() {
1549          for (ExecutionMode m : ExecutionMode.values())
1550 <        for (Integer v1 : new Integer[] { 1, null }) {
1551 <
1550 >        for (boolean createIncomplete : new boolean[] { true, false })
1551 >        for (boolean fFirst : new boolean[] { true, false })
1552 >        for (Integer v1 : new Integer[] { 1, null })
1553 >    {
1554          final CompletableFuture<Integer> f = new CompletableFuture<>();
1555          final CompletableFuture<Integer> g = new CompletableFuture<>();
1780        final Noop r = new Noop();
1781        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1556          final CFException ex = new CFException();
1557 +        final Noop r = new Noop(m);
1558  
1559 <        f.completeExceptionally(ex);
1560 <        checkIncomplete(h);
1561 <        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();
1559 >        (fFirst ? f : g).complete(v1);
1560 >        if (!createIncomplete)
1561 >            (!fFirst ? f : g).completeExceptionally(ex);
1562          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1563 <        final CFException ex = new CFException();
1564 <
1565 <        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);
1563 >        if (createIncomplete) {
1564 >            checkIncomplete(h);
1565 >            (!fFirst ? f : g).completeExceptionally(ex);
1566          }
1814    }
1815
1816    public void testRunAfterBoth_exceptionalCompletion3() {
1817        for (ExecutionMode m : ExecutionMode.values())
1818        for (Integer v1 : new Integer[] { 1, null }) {
1819
1820        final CompletableFuture<Integer> f = new CompletableFuture<>();
1821        final CompletableFuture<Integer> g = new CompletableFuture<>();
1822        final Noop r = new Noop();
1823        final CFException ex = new CFException();
1824
1825        g.completeExceptionally(ex);
1826        f.complete(v1);
1827        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1567  
1568          checkCompletedWithWrappedCFException(h, ex);
1830        checkCompletedWithWrappedCFException(g, ex);
1569          assertEquals(0, r.invocationCount);
1570 <        checkCompletedNormally(f, v1);
1571 <        }
1572 <    }
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);
1848 <
1849 <        checkCompletedWithWrappedCFException(h, ex);
1850 <        checkCompletedWithWrappedCFException(f, ex);
1851 <        assertEquals(0, r.invocationCount);
1852 <        checkCompletedNormally(g, v1);
1853 <        }
1854 <    }
1570 >        checkCompletedNormally(fFirst ? f : g, v1);
1571 >        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1572 >    }}
1573  
1574      /**
1575       * runAfterBoth result completes exceptionally if action does
1576       */
1577 <    public void testRunAfterBoth_actionFailed1() {
1577 >    public void testRunAfterBoth_actionFailed() {
1578          for (ExecutionMode m : ExecutionMode.values())
1579 +        for (boolean fFirst : new boolean[] { true, false })
1580          for (Integer v1 : new Integer[] { 1, null })
1581 <        for (Integer v2 : new Integer[] { 2, null }) {
1582 <
1581 >        for (Integer v2 : new Integer[] { 2, null })
1582 >    {
1583          final CompletableFuture<Integer> f = new CompletableFuture<>();
1584          final CompletableFuture<Integer> g = new CompletableFuture<>();
1585 <        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);
1585 >        final FailingRunnable r = new FailingRunnable(m);
1586  
1587 <        checkCompletedWithWrappedCFException(h);
1588 <        checkCompletedNormally(f, v1);
1589 <        checkCompletedNormally(g, v2);
1587 >        CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r);
1588 >        if (fFirst) {
1589 >            f.complete(v1);
1590 >            g.complete(v2);
1591 >        } else {
1592 >            g.complete(v2);
1593 >            f.complete(v1);
1594          }
1595 <    }
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);
1595 >        CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r);
1596  
1597 <        checkCompletedWithWrappedCFException(h);
1597 >        checkCompletedWithWrappedCFException(h1);
1598 >        checkCompletedWithWrappedCFException(h2);
1599          checkCompletedNormally(f, v1);
1600          checkCompletedNormally(g, v2);
1601 <        }
1897 <    }
1601 >    }}
1602  
1603      /**
1604       * runAfterBoth result completes exceptionally if either source cancelled
1605       */
1606 <    public void testRunAfterBoth_sourceCancelled1() {
1606 >    public void testRunAfterBoth_sourceCancelled() {
1607          for (ExecutionMode m : ExecutionMode.values())
1608          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1609 <        for (Integer v1 : new Integer[] { 1, null }) {
1610 <
1609 >        for (boolean createIncomplete : new boolean[] { true, false })
1610 >        for (boolean fFirst : new boolean[] { true, false })
1611 >        for (Integer v1 : new Integer[] { 1, null })
1612 >    {
1613          final CompletableFuture<Integer> f = new CompletableFuture<>();
1614          final CompletableFuture<Integer> g = new CompletableFuture<>();
1615 <        final Noop r = new Noop();
1910 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1911 <
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 <    }
1615 >        final Noop r = new Noop(m);
1616  
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 }) {
1617  
1618 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1619 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1620 <        final Noop r = new Noop();
1618 >        (fFirst ? f : g).complete(v1);
1619 >        if (!createIncomplete)
1620 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1621          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1622 <
1623 <        assertTrue(g.cancel(mayInterruptIfRunning));
1624 <        checkIncomplete(h);
1935 <        f.complete(v1);
1936 <
1937 <        checkCompletedWithWrappedCancellationException(h);
1938 <        checkCancelled(g);
1939 <        assertEquals(0, r.invocationCount);
1940 <        checkCompletedNormally(f, v1);
1622 >        if (createIncomplete) {
1623 >            checkIncomplete(h);
1624 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1625          }
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);
1626  
1627          checkCompletedWithWrappedCancellationException(h);
1628 <        checkCancelled(g);
1628 >        checkCancelled(!fFirst ? f : g);
1629          assertEquals(0, r.invocationCount);
1630 <        checkCompletedNormally(f, v1);
1631 <        }
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 <    }
1630 >        checkCompletedNormally(fFirst ? f : g, v1);
1631 >    }}
1632  
1633      /**
1634       * applyToEither result completes normally after normal completion
1635       * of either source
1636       */
1637 <    public void testApplyToEither_normalCompletion1() {
1637 >    public void testApplyToEither_normalCompletion() {
1638          for (ExecutionMode m : ExecutionMode.values())
1639 +        for (boolean createIncomplete : new boolean[] { true, false })
1640 +        for (boolean fFirst : new boolean[] { true, false })
1641          for (Integer v1 : new Integer[] { 1, null })
1642 <        for (Integer v2 : new Integer[] { 2, null }) {
1643 <
1642 >        for (Integer v2 : new Integer[] { 2, null })
1643 >    {
1644          final CompletableFuture<Integer> f = new CompletableFuture<>();
1645          final CompletableFuture<Integer> g = new CompletableFuture<>();
1646 <        final IncFunction r = new IncFunction();
1996 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1646 >        final IncFunction r = new IncFunction(m);
1647  
1648 <        f.complete(v1);
1649 <        checkCompletedNormally(h, inc(v1));
1650 <        g.complete(v2);
1648 >        if (!createIncomplete)
1649 >            if (fFirst) f.complete(v1); else g.complete(v2);
1650 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1651 >        if (createIncomplete) {
1652 >            checkIncomplete(h);
1653 >            assertEquals(0, r.invocationCount);
1654 >            if (fFirst) f.complete(v1); else g.complete(v2);
1655 >        }
1656 >        checkCompletedNormally(h, inc(fFirst ? v1 : v2));
1657 >        if (!fFirst) f.complete(v1); else g.complete(v2);
1658  
1659          checkCompletedNormally(f, v1);
1660          checkCompletedNormally(g, v2);
1661 <        checkCompletedNormally(h, inc(v1));
1662 <        }
2006 <    }
1661 >        checkCompletedNormally(h, inc(fFirst ? v1 : v2));
1662 >    }}
1663  
1664 <    public void testApplyToEither_normalCompletion2() {
1664 >    public void testApplyToEither_normalCompletionBothAvailable() {
1665          for (ExecutionMode m : ExecutionMode.values())
1666 +        for (boolean fFirst : new boolean[] { true, false })
1667          for (Integer v1 : new Integer[] { 1, null })
1668 <        for (Integer v2 : new Integer[] { 2, null }) {
1669 <
1668 >        for (Integer v2 : new Integer[] { 2, null })
1669 >    {
1670          final CompletableFuture<Integer> f = new CompletableFuture<>();
1671          final CompletableFuture<Integer> g = new CompletableFuture<>();
1672 <        final IncFunction r = new IncFunction();
2016 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1672 >        final IncFunction r = new IncFunction(m);
1673  
1674 <        g.complete(v2);
1675 <        checkCompletedNormally(h, inc(v2));
1676 <        f.complete(v1);
1677 <
1678 <        checkCompletedNormally(f, v1);
1679 <        checkCompletedNormally(g, v2);
2024 <        checkCompletedNormally(h, inc(v2));
1674 >        if (fFirst) {
1675 >            f.complete(v1);
1676 >            g.complete(v2);
1677 >        } else {
1678 >            g.complete(v2);
1679 >            f.complete(v1);
1680          }
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 }) {
2031
2032        final CompletableFuture<Integer> f = new CompletableFuture<>();
2033        final CompletableFuture<Integer> g = new CompletableFuture<>();
2034        final IncFunction r = new IncFunction();
1681  
2036        f.complete(v1);
2037        g.complete(v2);
1682          final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1683  
1684          checkCompletedNormally(f, v1);
# Line 2044 | Line 1688 | public class CompletableFutureTest exten
1688          assertTrue(Objects.equals(h.join(), inc(v1)) ||
1689                     Objects.equals(h.join(), inc(v2)));
1690          assertEquals(1, r.invocationCount);
1691 <        }
2048 <    }
1691 >    }}
1692  
1693      /**
1694       * applyToEither result completes exceptionally after exceptional
# Line 2053 | Line 1696 | public class CompletableFutureTest exten
1696       */
1697      public void testApplyToEither_exceptionalCompletion1() {
1698          for (ExecutionMode m : ExecutionMode.values())
1699 <        for (Integer v1 : new Integer[] { 1, null }) {
1700 <
1699 >        for (boolean createIncomplete : new boolean[] { true, false })
1700 >        for (boolean fFirst : new boolean[] { true, false })
1701 >        for (Integer v1 : new Integer[] { 1, null })
1702 >    {
1703          final CompletableFuture<Integer> f = new CompletableFuture<>();
1704          final CompletableFuture<Integer> g = new CompletableFuture<>();
2060        final IncFunction r = new IncFunction();
2061        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1705          final CFException ex = new CFException();
1706 +        final IncFunction r = new IncFunction(m);
1707  
1708 <        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();
1708 >        if (!createIncomplete) (fFirst ? f : g).completeExceptionally(ex);
1709          final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1710 <        final CFException ex = new CFException();
1710 >        if (createIncomplete) {
1711 >            checkIncomplete(h);
1712 >            assertEquals(0, r.invocationCount);
1713 >            (fFirst ? f : g).completeExceptionally(ex);
1714 >        }
1715  
2085        g.completeExceptionally(ex);
1716          checkCompletedWithWrappedCFException(h, ex);
1717 <        f.complete(v1);
1717 >        (!fFirst ? f : g).complete(v1);
1718  
1719          assertEquals(0, r.invocationCount);
1720 <        checkCompletedNormally(f, v1);
1721 <        checkCompletedWithWrappedCFException(g, ex);
1720 >        checkCompletedNormally(!fFirst ? f : g, v1);
1721 >        checkCompletedWithWrappedCFException(fFirst ? f : g, ex);
1722          checkCompletedWithWrappedCFException(h, ex);
1723 <        }
2094 <    }
1723 >    }}
1724  
1725 <    public void testApplyToEither_exceptionalCompletion3() {
1725 >    public void testApplyToEither_exceptionalCompletion2() {
1726          for (ExecutionMode m : ExecutionMode.values())
1727 <        for (Integer v1 : new Integer[] { 1, null }) {
1728 <
1727 >        for (boolean reverseArgs : new boolean[] { true, false })
1728 >        for (boolean fFirst : new boolean[] { true, false })
1729 >        for (Integer v1 : new Integer[] { 1, null })
1730 >    {
1731          final CompletableFuture<Integer> f = new CompletableFuture<>();
1732          final CompletableFuture<Integer> g = new CompletableFuture<>();
1733 <        final IncFunction r = new IncFunction();
1733 >        final IncFunction r1 = new IncFunction(m);
1734 >        final IncFunction r2 = new IncFunction(m);
1735          final CFException ex = new CFException();
1736 <
1737 <        g.completeExceptionally(ex);
1738 <        f.complete(v1);
1739 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1736 >        final CompletableFuture<Integer> j = (reverseArgs ? g : f);
1737 >        final CompletableFuture<Integer> k = (reverseArgs ? f : g);
1738 >        final CompletableFuture<Integer> h1 = m.applyToEither(j, k, r1);
1739 >        if (fFirst) {
1740 >            f.complete(v1);
1741 >            g.completeExceptionally(ex);
1742 >        } else {
1743 >            g.completeExceptionally(ex);
1744 >            f.complete(v1);
1745 >        }
1746 >        final CompletableFuture<Integer> h2 = m.applyToEither(j, k, r2);
1747  
1748          // unspecified behavior
2110        Integer v;
1749          try {
1750 <            assertEquals(inc(v1), h.join());
1751 <            assertEquals(1, r.invocationCount);
1750 >            assertEquals(inc(v1), h1.join());
1751 >            assertEquals(1, r1.invocationCount);
1752          } catch (CompletionException ok) {
1753 <            checkCompletedWithWrappedCFException(h, ex);
1754 <            assertEquals(0, r.invocationCount);
2117 <        }
2118 <
2119 <        checkCompletedWithWrappedCFException(g, ex);
2120 <        checkCompletedNormally(f, v1);
1753 >            checkCompletedWithWrappedCFException(h1, ex);
1754 >            assertEquals(0, r1.invocationCount);
1755          }
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);
1756  
2137        // unspecified behavior
2138        Integer v;
1757          try {
1758 <            assertEquals(inc(v1), h.join());
1759 <            assertEquals(1, r.invocationCount);
1758 >            assertEquals(inc(v1), h2.join());
1759 >            assertEquals(1, r2.invocationCount);
1760          } catch (CompletionException ok) {
1761 <            checkCompletedWithWrappedCFException(h, ex);
1762 <            assertEquals(0, r.invocationCount);
1761 >            checkCompletedWithWrappedCFException(h2, ex);
1762 >            assertEquals(0, r2.invocationCount);
1763          }
1764  
1765 <        checkCompletedWithWrappedCFException(f, ex);
1766 <        checkCompletedNormally(g, v1);
1767 <        }
2150 <    }
1765 >        checkCompletedWithWrappedCFException(g, ex);
1766 >        checkCompletedNormally(f, v1);
1767 >    }}
1768  
1769      /**
1770       * applyToEither result completes exceptionally if action does
# Line 2155 | Line 1772 | public class CompletableFutureTest exten
1772      public void testApplyToEither_actionFailed1() {
1773          for (ExecutionMode m : ExecutionMode.values())
1774          for (Integer v1 : new Integer[] { 1, null })
1775 <        for (Integer v2 : new Integer[] { 2, null }) {
1776 <
1775 >        for (Integer v2 : new Integer[] { 2, null })
1776 >    {
1777          final CompletableFuture<Integer> f = new CompletableFuture<>();
1778          final CompletableFuture<Integer> g = new CompletableFuture<>();
1779 <        final FailingFunction r = new FailingFunction();
1779 >        final FailingFunction r = new FailingFunction(m);
1780          final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1781  
1782          f.complete(v1);
# Line 2167 | Line 1784 | public class CompletableFutureTest exten
1784          g.complete(v2);
1785          checkCompletedNormally(f, v1);
1786          checkCompletedNormally(g, v2);
1787 <        }
2171 <    }
1787 >    }}
1788  
1789      public void testApplyToEither_actionFailed2() {
1790          for (ExecutionMode m : ExecutionMode.values())
1791          for (Integer v1 : new Integer[] { 1, null })
1792 <        for (Integer v2 : new Integer[] { 2, null }) {
1793 <
1792 >        for (Integer v2 : new Integer[] { 2, null })
1793 >    {
1794          final CompletableFuture<Integer> f = new CompletableFuture<>();
1795          final CompletableFuture<Integer> g = new CompletableFuture<>();
1796 <        final FailingFunction r = new FailingFunction();
1796 >        final FailingFunction r = new FailingFunction(m);
1797          final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1798  
1799          g.complete(v2);
# Line 2185 | Line 1801 | public class CompletableFutureTest exten
1801          f.complete(v1);
1802          checkCompletedNormally(f, v1);
1803          checkCompletedNormally(g, v2);
1804 <        }
2189 <    }
1804 >    }}
1805  
1806      /**
1807       * applyToEither result completes exceptionally if either source cancelled
# Line 2194 | Line 1809 | public class CompletableFutureTest exten
1809      public void testApplyToEither_sourceCancelled1() {
1810          for (ExecutionMode m : ExecutionMode.values())
1811          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1812 <        for (Integer v1 : new Integer[] { 1, null }) {
1813 <
1812 >        for (boolean createIncomplete : new boolean[] { true, false })
1813 >        for (boolean fFirst : new boolean[] { true, false })
1814 >        for (Integer v1 : new Integer[] { 1, null })
1815 >    {
1816          final CompletableFuture<Integer> f = new CompletableFuture<>();
1817          final CompletableFuture<Integer> g = new CompletableFuture<>();
1818 <        final IncFunction r = new IncFunction();
1818 >        final IncFunction r = new IncFunction(m);
1819 >
1820 >        if (!createIncomplete) assertTrue((fFirst ? f : g).cancel(mayInterruptIfRunning));
1821          final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1822 +        if (createIncomplete) {
1823 +            checkIncomplete(h);
1824 +            assertEquals(0, r.invocationCount);
1825 +            assertTrue((fFirst ? f : g).cancel(mayInterruptIfRunning));
1826 +        }
1827  
2204        assertTrue(f.cancel(mayInterruptIfRunning));
1828          checkCompletedWithWrappedCancellationException(h);
1829 <        g.complete(v1);
1829 >        (!fFirst ? f : g).complete(v1);
1830  
2208        checkCancelled(f);
1831          assertEquals(0, r.invocationCount);
1832 <        checkCompletedNormally(g, v1);
1832 >        checkCompletedNormally(!fFirst ? f : g, v1);
1833 >        checkCancelled(fFirst ? f : g);
1834          checkCompletedWithWrappedCancellationException(h);
1835 <        }
2213 <    }
1835 >    }}
1836  
1837      public void testApplyToEither_sourceCancelled2() {
1838          for (ExecutionMode m : ExecutionMode.values())
1839          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1840 <        for (Integer v1 : new Integer[] { 1, null }) {
1841 <
1840 >        for (boolean reverseArgs : new boolean[] { true, false })
1841 >        for (boolean fFirst : new boolean[] { true, false })
1842 >        for (Integer v1 : new Integer[] { 1, null })
1843 >    {
1844          final CompletableFuture<Integer> f = new CompletableFuture<>();
1845          final CompletableFuture<Integer> g = new CompletableFuture<>();
1846 <        final IncFunction r = new IncFunction();
1847 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1848 <
1849 <        assertTrue(g.cancel(mayInterruptIfRunning));
1850 <        checkCompletedWithWrappedCancellationException(h);
2227 <        f.complete(v1);
1846 >        final IncFunction r1 = new IncFunction(m);
1847 >        final IncFunction r2 = new IncFunction(m);
1848 >        final CFException ex = new CFException();
1849 >        final CompletableFuture<Integer> j = (reverseArgs ? g : f);
1850 >        final CompletableFuture<Integer> k = (reverseArgs ? f : g);
1851  
1852 <        checkCancelled(g);
1853 <        assertEquals(0, r.invocationCount);
1854 <        checkCompletedNormally(f, v1);
1855 <        checkCompletedWithWrappedCancellationException(h);
1852 >        final CompletableFuture<Integer> h1 = m.applyToEither(j, k, r1);
1853 >        if (fFirst) {
1854 >            f.complete(v1);
1855 >            assertTrue(g.cancel(mayInterruptIfRunning));
1856 >        } else {
1857 >            assertTrue(g.cancel(mayInterruptIfRunning));
1858 >            f.complete(v1);
1859          }
1860 <    }
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);
1860 >        final CompletableFuture<Integer> h2 = m.applyToEither(j, k, r2);
1861  
1862          // unspecified behavior
2250        Integer v;
1863          try {
1864 <            assertEquals(inc(v1), h.join());
1865 <            assertEquals(1, r.invocationCount);
1864 >            assertEquals(inc(v1), h1.join());
1865 >            assertEquals(1, r1.invocationCount);
1866          } catch (CompletionException ok) {
1867 <            checkCompletedWithWrappedCancellationException(h);
1868 <            assertEquals(0, r.invocationCount);
1867 >            checkCompletedWithWrappedCancellationException(h1);
1868 >            assertEquals(0, r1.invocationCount);
1869          }
1870  
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;
1871          try {
1872 <            assertEquals(inc(v1), h.join());
1873 <            assertEquals(1, r.invocationCount);
1872 >            assertEquals(inc(v1), h2.join());
1873 >            assertEquals(1, r2.invocationCount);
1874          } catch (CompletionException ok) {
1875 <            checkCompletedWithWrappedCancellationException(h);
1876 <            assertEquals(0, r.invocationCount);
1875 >            checkCompletedWithWrappedCancellationException(h2);
1876 >            assertEquals(0, r2.invocationCount);
1877          }
1878  
1879 <        checkCancelled(f);
1880 <        checkCompletedNormally(g, v1);
1881 <        }
2290 <    }
1879 >        checkCancelled(g);
1880 >        checkCompletedNormally(f, v1);
1881 >    }}
1882  
1883      /**
1884       * acceptEither result completes normally after normal completion
# Line 2296 | Line 1887 | public class CompletableFutureTest exten
1887      public void testAcceptEither_normalCompletion1() {
1888          for (ExecutionMode m : ExecutionMode.values())
1889          for (Integer v1 : new Integer[] { 1, null })
1890 <        for (Integer v2 : new Integer[] { 2, null }) {
1891 <
1890 >        for (Integer v2 : new Integer[] { 2, null })
1891 >    {
1892          final CompletableFuture<Integer> f = new CompletableFuture<>();
1893          final CompletableFuture<Integer> g = new CompletableFuture<>();
1894          final IncAction r = new IncAction();
# Line 2311 | Line 1902 | public class CompletableFutureTest exten
1902          checkCompletedNormally(f, v1);
1903          checkCompletedNormally(g, v2);
1904          checkCompletedNormally(h, null);
1905 <        }
2315 <    }
1905 >    }}
1906  
1907      public void testAcceptEither_normalCompletion2() {
1908          for (ExecutionMode m : ExecutionMode.values())
1909          for (Integer v1 : new Integer[] { 1, null })
1910 <        for (Integer v2 : new Integer[] { 2, null }) {
1911 <
1910 >        for (Integer v2 : new Integer[] { 2, null })
1911 >    {
1912          final CompletableFuture<Integer> f = new CompletableFuture<>();
1913          final CompletableFuture<Integer> g = new CompletableFuture<>();
1914          final IncAction r = new IncAction();
# Line 2332 | Line 1922 | public class CompletableFutureTest exten
1922          checkCompletedNormally(f, v1);
1923          checkCompletedNormally(g, v2);
1924          checkCompletedNormally(h, null);
1925 <        }
1926 <    }
1925 >    }}
1926 >
1927      public void testAcceptEither_normalCompletion3() {
1928          for (ExecutionMode m : ExecutionMode.values())
1929          for (Integer v1 : new Integer[] { 1, null })
1930 <        for (Integer v2 : new Integer[] { 2, null }) {
1931 <
1930 >        for (Integer v2 : new Integer[] { 2, null })
1931 >    {
1932          final CompletableFuture<Integer> f = new CompletableFuture<>();
1933          final CompletableFuture<Integer> g = new CompletableFuture<>();
1934          final IncAction r = new IncAction();
# Line 2354 | Line 1944 | public class CompletableFutureTest exten
1944          // unspecified behavior
1945          assertTrue(Objects.equals(r.value, inc(v1)) ||
1946                     Objects.equals(r.value, inc(v2)));
1947 <        }
2358 <    }
1947 >    }}
1948  
1949      /**
1950       * acceptEither result completes exceptionally after exceptional
# Line 2363 | Line 1952 | public class CompletableFutureTest exten
1952       */
1953      public void testAcceptEither_exceptionalCompletion1() {
1954          for (ExecutionMode m : ExecutionMode.values())
1955 <        for (Integer v1 : new Integer[] { 1, null }) {
1956 <
1955 >        for (Integer v1 : new Integer[] { 1, null })
1956 >    {
1957          final CompletableFuture<Integer> f = new CompletableFuture<>();
1958          final CompletableFuture<Integer> g = new CompletableFuture<>();
1959          final IncAction r = new IncAction();
# Line 2379 | Line 1968 | public class CompletableFutureTest exten
1968          checkCompletedNormally(g, v1);
1969          checkCompletedWithWrappedCFException(f, ex);
1970          checkCompletedWithWrappedCFException(h, ex);
1971 <        }
2383 <    }
1971 >    }}
1972  
1973      public void testAcceptEither_exceptionalCompletion2() {
1974          for (ExecutionMode m : ExecutionMode.values())
1975 <        for (Integer v1 : new Integer[] { 1, null }) {
1976 <
1975 >        for (Integer v1 : new Integer[] { 1, null })
1976 >    {
1977          final CompletableFuture<Integer> f = new CompletableFuture<>();
1978          final CompletableFuture<Integer> g = new CompletableFuture<>();
1979          final IncAction r = new IncAction();
# Line 2400 | Line 1988 | public class CompletableFutureTest exten
1988          checkCompletedNormally(f, v1);
1989          checkCompletedWithWrappedCFException(g, ex);
1990          checkCompletedWithWrappedCFException(h, ex);
1991 <        }
2404 <    }
1991 >    }}
1992  
1993      public void testAcceptEither_exceptionalCompletion3() {
1994          for (ExecutionMode m : ExecutionMode.values())
1995 <        for (Integer v1 : new Integer[] { 1, null }) {
1996 <
1995 >        for (Integer v1 : new Integer[] { 1, null })
1996 >    {
1997          final CompletableFuture<Integer> f = new CompletableFuture<>();
1998          final CompletableFuture<Integer> g = new CompletableFuture<>();
1999          final IncAction r = new IncAction();
# Line 2429 | Line 2016 | public class CompletableFutureTest exten
2016  
2017          checkCompletedWithWrappedCFException(g, ex);
2018          checkCompletedNormally(f, v1);
2019 <        }
2433 <    }
2019 >    }}
2020  
2021      public void testAcceptEither_exceptionalCompletion4() {
2022          for (ExecutionMode m : ExecutionMode.values())
2023 <        for (Integer v1 : new Integer[] { 1, null }) {
2024 <
2023 >        for (Integer v1 : new Integer[] { 1, null })
2024 >    {
2025          final CompletableFuture<Integer> f = new CompletableFuture<>();
2026          final CompletableFuture<Integer> g = new CompletableFuture<>();
2027          final IncAction r = new IncAction();
# Line 2458 | Line 2044 | public class CompletableFutureTest exten
2044  
2045          checkCompletedWithWrappedCFException(f, ex);
2046          checkCompletedNormally(g, v1);
2047 <        }
2462 <    }
2047 >    }}
2048  
2049      /**
2050       * acceptEither result completes exceptionally if action does
# Line 2467 | Line 2052 | public class CompletableFutureTest exten
2052      public void testAcceptEither_actionFailed1() {
2053          for (ExecutionMode m : ExecutionMode.values())
2054          for (Integer v1 : new Integer[] { 1, null })
2055 <        for (Integer v2 : new Integer[] { 2, null }) {
2056 <
2055 >        for (Integer v2 : new Integer[] { 2, null })
2056 >    {
2057          final CompletableFuture<Integer> f = new CompletableFuture<>();
2058          final CompletableFuture<Integer> g = new CompletableFuture<>();
2059 <        final FailingConsumer r = new FailingConsumer();
2059 >        final FailingConsumer r = new FailingConsumer(m);
2060          final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2061  
2062          f.complete(v1);
# Line 2479 | Line 2064 | public class CompletableFutureTest exten
2064          g.complete(v2);
2065          checkCompletedNormally(f, v1);
2066          checkCompletedNormally(g, v2);
2067 <        }
2483 <    }
2067 >    }}
2068  
2069      public void testAcceptEither_actionFailed2() {
2070          for (ExecutionMode m : ExecutionMode.values())
2071          for (Integer v1 : new Integer[] { 1, null })
2072 <        for (Integer v2 : new Integer[] { 2, null }) {
2073 <
2072 >        for (Integer v2 : new Integer[] { 2, null })
2073 >    {
2074          final CompletableFuture<Integer> f = new CompletableFuture<>();
2075          final CompletableFuture<Integer> g = new CompletableFuture<>();
2076 <        final FailingConsumer r = new FailingConsumer();
2076 >        final FailingConsumer r = new FailingConsumer(m);
2077          final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2078  
2079          g.complete(v2);
# Line 2497 | Line 2081 | public class CompletableFutureTest exten
2081          f.complete(v1);
2082          checkCompletedNormally(f, v1);
2083          checkCompletedNormally(g, v2);
2084 <        }
2501 <    }
2084 >    }}
2085  
2086      /**
2087       * acceptEither result completes exceptionally if either source cancelled
# Line 2506 | Line 2089 | public class CompletableFutureTest exten
2089      public void testAcceptEither_sourceCancelled1() {
2090          for (ExecutionMode m : ExecutionMode.values())
2091          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2092 <        for (Integer v1 : new Integer[] { 1, null }) {
2093 <
2092 >        for (Integer v1 : new Integer[] { 1, null })
2093 >    {
2094          final CompletableFuture<Integer> f = new CompletableFuture<>();
2095          final CompletableFuture<Integer> g = new CompletableFuture<>();
2096          final IncAction r = new IncAction();
# Line 2521 | Line 2104 | public class CompletableFutureTest exten
2104          assertEquals(0, r.invocationCount);
2105          checkCompletedNormally(g, v1);
2106          checkCompletedWithWrappedCancellationException(h);
2107 <        }
2525 <    }
2107 >    }}
2108  
2109      public void testAcceptEither_sourceCancelled2() {
2110          for (ExecutionMode m : ExecutionMode.values())
2111          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2112 <        for (Integer v1 : new Integer[] { 1, null }) {
2113 <
2112 >        for (Integer v1 : new Integer[] { 1, null })
2113 >    {
2114          final CompletableFuture<Integer> f = new CompletableFuture<>();
2115          final CompletableFuture<Integer> g = new CompletableFuture<>();
2116          final IncAction r = new IncAction();
# Line 2542 | Line 2124 | public class CompletableFutureTest exten
2124          assertEquals(0, r.invocationCount);
2125          checkCompletedNormally(f, v1);
2126          checkCompletedWithWrappedCancellationException(h);
2127 <        }
2546 <    }
2127 >    }}
2128  
2129      public void testAcceptEither_sourceCancelled3() {
2130          for (ExecutionMode m : ExecutionMode.values())
2131          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2132 <        for (Integer v1 : new Integer[] { 1, null }) {
2133 <
2132 >        for (Integer v1 : new Integer[] { 1, null })
2133 >    {
2134          final CompletableFuture<Integer> f = new CompletableFuture<>();
2135          final CompletableFuture<Integer> g = new CompletableFuture<>();
2136          final IncAction r = new IncAction();
# Line 2571 | Line 2152 | public class CompletableFutureTest exten
2152  
2153          checkCancelled(g);
2154          checkCompletedNormally(f, v1);
2155 <        }
2575 <    }
2155 >    }}
2156  
2157      public void testAcceptEither_sourceCancelled4() {
2158          for (ExecutionMode m : ExecutionMode.values())
2159          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2160 <        for (Integer v1 : new Integer[] { 1, null }) {
2161 <
2160 >        for (Integer v1 : new Integer[] { 1, null })
2161 >    {
2162          final CompletableFuture<Integer> f = new CompletableFuture<>();
2163          final CompletableFuture<Integer> g = new CompletableFuture<>();
2164          final IncAction r = new IncAction();
# Line 2600 | Line 2180 | public class CompletableFutureTest exten
2180  
2181          checkCancelled(f);
2182          checkCompletedNormally(g, v1);
2183 <        }
2604 <    }
2183 >    }}
2184  
2185      /**
2186       * runAfterEither result completes normally after normal completion
# Line 2610 | Line 2189 | public class CompletableFutureTest exten
2189      public void testRunAfterEither_normalCompletion1() {
2190          for (ExecutionMode m : ExecutionMode.values())
2191          for (Integer v1 : new Integer[] { 1, null })
2192 <        for (Integer v2 : new Integer[] { 2, null }) {
2193 <
2192 >        for (Integer v2 : new Integer[] { 2, null })
2193 >    {
2194          final CompletableFuture<Integer> f = new CompletableFuture<>();
2195          final CompletableFuture<Integer> g = new CompletableFuture<>();
2196 <        final Noop r = new Noop();
2196 >        final Noop r = new Noop(m);
2197          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2198  
2199          f.complete(v1);
# Line 2626 | Line 2205 | public class CompletableFutureTest exten
2205          checkCompletedNormally(g, v2);
2206          checkCompletedNormally(h, null);
2207          assertEquals(1, r.invocationCount);
2208 <        }
2630 <    }
2208 >    }}
2209  
2210      public void testRunAfterEither_normalCompletion2() {
2211          for (ExecutionMode m : ExecutionMode.values())
2212          for (Integer v1 : new Integer[] { 1, null })
2213 <        for (Integer v2 : new Integer[] { 2, null }) {
2214 <
2213 >        for (Integer v2 : new Integer[] { 2, null })
2214 >    {
2215          final CompletableFuture<Integer> f = new CompletableFuture<>();
2216          final CompletableFuture<Integer> g = new CompletableFuture<>();
2217 <        final Noop r = new Noop();
2217 >        final Noop r = new Noop(m);
2218          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2219  
2220          g.complete(v2);
# Line 2648 | Line 2226 | public class CompletableFutureTest exten
2226          checkCompletedNormally(g, v2);
2227          checkCompletedNormally(h, null);
2228          assertEquals(1, r.invocationCount);
2229 <        }
2230 <    }
2229 >        }}
2230 >
2231      public void testRunAfterEither_normalCompletion3() {
2232          for (ExecutionMode m : ExecutionMode.values())
2233          for (Integer v1 : new Integer[] { 1, null })
2234 <        for (Integer v2 : new Integer[] { 2, null }) {
2235 <
2234 >        for (Integer v2 : new Integer[] { 2, null })
2235 >    {
2236          final CompletableFuture<Integer> f = new CompletableFuture<>();
2237          final CompletableFuture<Integer> g = new CompletableFuture<>();
2238 <        final Noop r = new Noop();
2238 >        final Noop r = new Noop(m);
2239  
2240          f.complete(v1);
2241          g.complete(v2);
# Line 2667 | Line 2245 | public class CompletableFutureTest exten
2245          checkCompletedNormally(f, v1);
2246          checkCompletedNormally(g, v2);
2247          assertEquals(1, r.invocationCount);
2248 <        }
2671 <    }
2248 >    }}
2249  
2250      /**
2251       * runAfterEither result completes exceptionally after exceptional
# Line 2676 | Line 2253 | public class CompletableFutureTest exten
2253       */
2254      public void testRunAfterEither_exceptionalCompletion1() {
2255          for (ExecutionMode m : ExecutionMode.values())
2256 <        for (Integer v1 : new Integer[] { 1, null }) {
2257 <
2256 >        for (Integer v1 : new Integer[] { 1, null })
2257 >    {
2258          final CompletableFuture<Integer> f = new CompletableFuture<>();
2259          final CompletableFuture<Integer> g = new CompletableFuture<>();
2260 <        final Noop r = new Noop();
2260 >        final Noop r = new Noop(m);
2261          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2262          final CFException ex = new CFException();
2263  
# Line 2692 | Line 2269 | public class CompletableFutureTest exten
2269          checkCompletedNormally(g, v1);
2270          checkCompletedWithWrappedCFException(f, ex);
2271          checkCompletedWithWrappedCFException(h, ex);
2272 <        }
2696 <    }
2272 >    }}
2273  
2274      public void testRunAfterEither_exceptionalCompletion2() {
2275          for (ExecutionMode m : ExecutionMode.values())
2276 <        for (Integer v1 : new Integer[] { 1, null }) {
2277 <
2276 >        for (Integer v1 : new Integer[] { 1, null })
2277 >    {
2278          final CompletableFuture<Integer> f = new CompletableFuture<>();
2279          final CompletableFuture<Integer> g = new CompletableFuture<>();
2280 <        final Noop r = new Noop();
2280 >        final Noop r = new Noop(m);
2281          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2282          final CFException ex = new CFException();
2283  
# Line 2713 | Line 2289 | public class CompletableFutureTest exten
2289          checkCompletedNormally(f, v1);
2290          checkCompletedWithWrappedCFException(g, ex);
2291          checkCompletedWithWrappedCFException(h, ex);
2292 <        }
2717 <    }
2292 >    }}
2293  
2294      public void testRunAfterEither_exceptionalCompletion3() {
2295          for (ExecutionMode m : ExecutionMode.values())
2296 <        for (Integer v1 : new Integer[] { 1, null }) {
2297 <
2296 >        for (Integer v1 : new Integer[] { 1, null })
2297 >    {
2298          final CompletableFuture<Integer> f = new CompletableFuture<>();
2299          final CompletableFuture<Integer> g = new CompletableFuture<>();
2300 <        final Noop r = new Noop();
2300 >        final Noop r = new Noop(m);
2301          final CFException ex = new CFException();
2302  
2303          g.completeExceptionally(ex);
# Line 2741 | Line 2316 | public class CompletableFutureTest exten
2316  
2317          checkCompletedWithWrappedCFException(g, ex);
2318          checkCompletedNormally(f, v1);
2319 <        }
2745 <    }
2319 >    }}
2320  
2321      public void testRunAfterEither_exceptionalCompletion4() {
2322          for (ExecutionMode m : ExecutionMode.values())
2323 <        for (Integer v1 : new Integer[] { 1, null }) {
2324 <
2323 >        for (Integer v1 : new Integer[] { 1, null })
2324 >    {
2325          final CompletableFuture<Integer> f = new CompletableFuture<>();
2326          final CompletableFuture<Integer> g = new CompletableFuture<>();
2327 <        final Noop r = new Noop();
2327 >        final Noop r = new Noop(m);
2328          final CFException ex = new CFException();
2329  
2330          f.completeExceptionally(ex);
# Line 2769 | Line 2343 | public class CompletableFutureTest exten
2343  
2344          checkCompletedWithWrappedCFException(f, ex);
2345          checkCompletedNormally(g, v1);
2346 <        }
2773 <    }
2346 >    }}
2347  
2348      /**
2349       * runAfterEither result completes exceptionally if action does
# Line 2778 | Line 2351 | public class CompletableFutureTest exten
2351      public void testRunAfterEither_actionFailed1() {
2352          for (ExecutionMode m : ExecutionMode.values())
2353          for (Integer v1 : new Integer[] { 1, null })
2354 <        for (Integer v2 : new Integer[] { 2, null }) {
2355 <
2354 >        for (Integer v2 : new Integer[] { 2, null })
2355 >    {
2356          final CompletableFuture<Integer> f = new CompletableFuture<>();
2357          final CompletableFuture<Integer> g = new CompletableFuture<>();
2358 <        final FailingNoop r = new FailingNoop();
2358 >        final FailingRunnable r = new FailingRunnable(m);
2359          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2360  
2361          f.complete(v1);
# Line 2790 | Line 2363 | public class CompletableFutureTest exten
2363          g.complete(v2);
2364          checkCompletedNormally(f, v1);
2365          checkCompletedNormally(g, v2);
2366 <        }
2794 <    }
2366 >    }}
2367  
2368      public void testRunAfterEither_actionFailed2() {
2369          for (ExecutionMode m : ExecutionMode.values())
2370          for (Integer v1 : new Integer[] { 1, null })
2371 <        for (Integer v2 : new Integer[] { 2, null }) {
2372 <
2371 >        for (Integer v2 : new Integer[] { 2, null })
2372 >    {
2373          final CompletableFuture<Integer> f = new CompletableFuture<>();
2374          final CompletableFuture<Integer> g = new CompletableFuture<>();
2375 <        final FailingNoop r = new FailingNoop();
2375 >        final FailingRunnable r = new FailingRunnable(m);
2376          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2377  
2378          g.complete(v2);
# Line 2808 | Line 2380 | public class CompletableFutureTest exten
2380          f.complete(v1);
2381          checkCompletedNormally(f, v1);
2382          checkCompletedNormally(g, v2);
2383 <        }
2812 <    }
2383 >    }}
2384  
2385      /**
2386       * runAfterEither result completes exceptionally if either source cancelled
# Line 2817 | Line 2388 | public class CompletableFutureTest exten
2388      public void testRunAfterEither_sourceCancelled1() {
2389          for (ExecutionMode m : ExecutionMode.values())
2390          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2391 <        for (Integer v1 : new Integer[] { 1, null }) {
2392 <
2391 >        for (Integer v1 : new Integer[] { 1, null })
2392 >    {
2393          final CompletableFuture<Integer> f = new CompletableFuture<>();
2394          final CompletableFuture<Integer> g = new CompletableFuture<>();
2395 <        final Noop r = new Noop();
2395 >        final Noop r = new Noop(m);
2396          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2397  
2398          assertTrue(f.cancel(mayInterruptIfRunning));
# Line 2832 | Line 2403 | public class CompletableFutureTest exten
2403          assertEquals(0, r.invocationCount);
2404          checkCompletedNormally(g, v1);
2405          checkCompletedWithWrappedCancellationException(h);
2406 <        }
2836 <    }
2406 >    }}
2407  
2408      public void testRunAfterEither_sourceCancelled2() {
2409          for (ExecutionMode m : ExecutionMode.values())
2410          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2411 <        for (Integer v1 : new Integer[] { 1, null }) {
2412 <
2411 >        for (Integer v1 : new Integer[] { 1, null })
2412 >    {
2413          final CompletableFuture<Integer> f = new CompletableFuture<>();
2414          final CompletableFuture<Integer> g = new CompletableFuture<>();
2415 <        final Noop r = new Noop();
2415 >        final Noop r = new Noop(m);
2416          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2417  
2418          assertTrue(g.cancel(mayInterruptIfRunning));
# Line 2853 | Line 2423 | public class CompletableFutureTest exten
2423          assertEquals(0, r.invocationCount);
2424          checkCompletedNormally(f, v1);
2425          checkCompletedWithWrappedCancellationException(h);
2426 <        }
2857 <    }
2426 >    }}
2427  
2428      public void testRunAfterEither_sourceCancelled3() {
2429          for (ExecutionMode m : ExecutionMode.values())
2430          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2431 <        for (Integer v1 : new Integer[] { 1, null }) {
2432 <
2431 >        for (Integer v1 : new Integer[] { 1, null })
2432 >    {
2433          final CompletableFuture<Integer> f = new CompletableFuture<>();
2434          final CompletableFuture<Integer> g = new CompletableFuture<>();
2435 <        final Noop r = new Noop();
2435 >        final Noop r = new Noop(m);
2436  
2437          assertTrue(g.cancel(mayInterruptIfRunning));
2438          f.complete(v1);
# Line 2881 | Line 2450 | public class CompletableFutureTest exten
2450  
2451          checkCancelled(g);
2452          checkCompletedNormally(f, v1);
2453 <        }
2885 <    }
2453 >    }}
2454  
2455      public void testRunAfterEither_sourceCancelled4() {
2456          for (ExecutionMode m : ExecutionMode.values())
2457          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2458 <        for (Integer v1 : new Integer[] { 1, null }) {
2459 <
2458 >        for (Integer v1 : new Integer[] { 1, null })
2459 >    {
2460          final CompletableFuture<Integer> f = new CompletableFuture<>();
2461          final CompletableFuture<Integer> g = new CompletableFuture<>();
2462 <        final Noop r = new Noop();
2462 >        final Noop r = new Noop(m);
2463  
2464          assertTrue(f.cancel(mayInterruptIfRunning));
2465          g.complete(v1);
# Line 2909 | Line 2477 | public class CompletableFutureTest exten
2477  
2478          checkCancelled(f);
2479          checkCompletedNormally(g, v1);
2480 <        }
2913 <    }
2480 >    }}
2481  
2482      /**
2483       * thenCompose result completes normally after normal completion of source
2484       */
2485 <    public void testThenCompose_normalCompletion1() {
2485 >    public void testThenCompose_normalCompletion() {
2486          for (ExecutionMode m : ExecutionMode.values())
2487 <        for (Integer v1 : new Integer[] { 1, null }) {
2488 <
2487 >        for (boolean createIncomplete : new boolean[] { true, false })
2488 >        for (Integer v1 : new Integer[] { 1, null })
2489 >    {
2490          final CompletableFuture<Integer> f = new CompletableFuture<>();
2491 <        final CompletableFutureInc r = new CompletableFutureInc();
2492 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2493 <        f.complete(v1);
2494 <        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 }) {
2491 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2492 >        if (!createIncomplete) f.complete(v1);
2493 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2494 >        if (createIncomplete) f.complete(v1);
2495  
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);
2496          checkCompletedNormally(g, inc(v1));
2497          checkCompletedNormally(f, v1);
2498          assertEquals(1, r.invocationCount);
2499 <        }
2944 <    }
2499 >    }}
2500  
2501      /**
2502       * thenCompose result completes exceptionally after exceptional
2503       * completion of source
2504       */
2505 <    public void testThenCompose_exceptionalCompletion1() {
2506 <        for (ExecutionMode m : ExecutionMode.values()) {
2507 <
2505 >    public void testThenCompose_exceptionalCompletion() {
2506 >        for (ExecutionMode m : ExecutionMode.values())
2507 >        for (boolean createIncomplete : new boolean[] { true, false })
2508 >    {
2509          final CFException ex = new CFException();
2510 <        final CompletableFutureInc r = new CompletableFutureInc();
2510 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2511          final CompletableFuture<Integer> f = new CompletableFuture<>();
2512 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2513 <        f.completeExceptionally(ex);
2514 <        checkCompletedWithWrappedCFException(g, ex);
2959 <        checkCompletedWithWrappedCFException(f, ex);
2960 <        }
2961 <    }
2962 <
2963 <    public void testThenCompose_exceptionalCompletion2() {
2964 <        for (ExecutionMode m : ExecutionMode.values()) {
2512 >        if (!createIncomplete) f.completeExceptionally(ex);
2513 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2514 >        if (createIncomplete) f.completeExceptionally(ex);
2515  
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);
2516          checkCompletedWithWrappedCFException(g, ex);
2517          checkCompletedWithWrappedCFException(f, ex);
2518 <        }
2519 <    }
2518 >        assertEquals(0, r.invocationCount);
2519 >    }}
2520  
2521      /**
2522       * thenCompose result completes exceptionally if action does
2523       */
2524 <    public void testThenCompose_actionFailed1() {
2524 >    public void testThenCompose_actionFailed() {
2525          for (ExecutionMode m : ExecutionMode.values())
2526 <        for (Integer v1 : new Integer[] { 1, null }) {
2527 <
2526 >        for (boolean createIncomplete : new boolean[] { true, false })
2527 >        for (Integer v1 : new Integer[] { 1, null })
2528 >    {
2529          final CompletableFuture<Integer> f = new CompletableFuture<>();
2530          final FailingCompletableFutureFunction r
2531 <            = new FailingCompletableFutureFunction();
2532 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2533 <        f.complete(v1);
2534 <        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 }) {
2531 >            = new FailingCompletableFutureFunction(m);
2532 >        if (!createIncomplete) f.complete(v1);
2533 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2534 >        if (createIncomplete) f.complete(v1);
2535  
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);
2536          checkCompletedWithWrappedCFException(g);
2537          checkCompletedNormally(f, v1);
2538 <        }
3005 <    }
2538 >    }}
2539  
2540      /**
2541       * thenCompose result completes exceptionally if source cancelled
2542       */
2543 <    public void testThenCompose_sourceCancelled1() {
2543 >    public void testThenCompose_sourceCancelled() {
2544          for (ExecutionMode m : ExecutionMode.values())
2545 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
2546 <
2545 >        for (boolean createIncomplete : new boolean[] { true, false })
2546 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2547 >    {
2548          final CompletableFuture<Integer> f = new CompletableFuture<>();
2549 <        final CompletableFutureInc r = new CompletableFutureInc();
2550 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2551 <        assertTrue(f.cancel(mayInterruptIfRunning));
2552 <        checkCompletedWithWrappedCancellationException(g);
2553 <        checkCancelled(f);
2549 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2550 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2551 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2552 >        if (createIncomplete) {
2553 >            checkIncomplete(g);
2554 >            assertTrue(f.cancel(mayInterruptIfRunning));
2555          }
3021    }
3022
3023    public void testThenCompose_sourceCancelled2() {
3024        for (ExecutionMode m : ExecutionMode.values())
3025        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
2556  
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);
2557          checkCompletedWithWrappedCancellationException(g);
2558          checkCancelled(f);
2559 <        }
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 <    }
2559 >    }}
2560  
2561      // other static methods
2562  
# Line 3489 | Line 2720 | public class CompletableFutureTest exten
2720  
2721              () -> f.thenCompose(null),
2722              () -> f.thenComposeAsync(null),
2723 <            () -> f.thenComposeAsync(new CompletableFutureInc(), null),
2723 >            () -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null),
2724              () -> f.thenComposeAsync(null, exec),
2725  
2726              () -> f.exceptionally(null),
# Line 3607 | Line 2838 | public class CompletableFutureTest exten
2838      public void testWhenComplete_actionFailed() {
2839          for (boolean createIncomplete : new boolean[] { true, false })
2840          for (ExecutionMode m : ExecutionMode.values())
2841 <        for (Integer v1 : new Integer[] { 1, null }) {
2842 <
2841 >        for (Integer v1 : new Integer[] { 1, null })
2842 >    {
2843          final AtomicInteger a = new AtomicInteger(0);
2844          final CFException ex = new CFException();
2845          final CompletableFuture<Integer> f = new CompletableFuture<>();
# Line 3625 | Line 2856 | public class CompletableFutureTest exten
2856          checkCompletedNormally(f, v1);
2857          checkCompletedWithWrappedCFException(g, ex);
2858          assertEquals(1, a.get());
2859 <        }
3629 <    }
2859 >    }}
2860  
2861      /**
2862       * If a whenComplete action throws an exception when triggered by
# Line 3636 | Line 2866 | public class CompletableFutureTest exten
2866      public void testWhenComplete_actionFailedSourceFailed() {
2867          for (boolean createIncomplete : new boolean[] { true, false })
2868          for (ExecutionMode m : ExecutionMode.values())
2869 <        for (Integer v1 : new Integer[] { 1, null }) {
2870 <
2869 >        for (Integer v1 : new Integer[] { 1, null })
2870 >    {
2871          final AtomicInteger a = new AtomicInteger(0);
2872          final CFException ex1 = new CFException();
2873          final CFException ex2 = new CFException();
# Line 3657 | Line 2887 | public class CompletableFutureTest exten
2887          checkCompletedWithWrappedCFException(f, ex1);
2888          checkCompletedWithWrappedCFException(g, ex1);
2889          assertEquals(1, a.get());
2890 <        }
3661 <    }
2890 >    }}
2891  
2892   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines