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.47 by jsr166, Mon Jun 2 18:21:34 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 <    }
1003 <
1004 <    /**
1005 <     * thenRun result completes exceptionally if action does
1006 <     */
1007 <    public void testThenRun3() {
1008 <        CompletableFuture<Integer> f;
1009 <        CompletableFuture<Void> g;
1010 <        FailingNoop r;
1011 <
1012 <        f = new CompletableFuture<>();
1013 <        g = f.thenRun(r = new FailingNoop());
1014 <        f.complete(null);
1015 <        checkCompletedWithWrappedCFException(g);
1016 <
1017 <        f = new CompletableFuture<>();
1018 <        f.complete(null);
1019 <        g = f.thenRun(r = new FailingNoop());
1020 <        checkCompletedWithWrappedCFException(g);
1021 <    }
1057 >    }}
1058  
1059      /**
1060       * thenRun result completes exceptionally if source cancelled
1061       */
1062 <    public void testThenRun4() {
1027 <        CompletableFuture<Integer> f;
1028 <        CompletableFuture<Void> g;
1029 <        Noop r;
1030 <
1031 <        f = new CompletableFuture<>();
1032 <        g = f.thenRun(r = new Noop());
1033 <        assertTrue(f.cancel(true));
1034 <        checkCompletedWithWrappedCancellationException(g);
1035 <
1036 <        f = new CompletableFuture<>();
1037 <        assertTrue(f.cancel(true));
1038 <        g = f.thenRun(r = new Noop());
1039 <        checkCompletedWithWrappedCancellationException(g);
1040 <    }
1041 <
1042 <    /**
1043 <     * thenApply result completes normally after normal completion of source
1044 <     */
1045 <    public void testThenApply() {
1046 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1047 <        CompletableFuture<Integer> g = f.thenApply(inc);
1048 <        f.complete(one);
1049 <        checkCompletedNormally(g, two);
1050 <    }
1051 <
1052 <    /**
1053 <     * thenApply result completes exceptionally after exceptional
1054 <     * completion of source
1055 <     */
1056 <    public void testThenApply2() {
1057 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1058 <        CompletableFuture<Integer> g = f.thenApply(inc);
1059 <        f.completeExceptionally(new CFException());
1060 <        checkCompletedWithWrappedCFException(g);
1061 <    }
1062 <
1063 <    /**
1064 <     * thenApply result completes exceptionally if action does
1065 <     */
1066 <    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 <    }
1072 <
1073 <    /**
1074 <     * thenApply result completes exceptionally if source cancelled
1075 <     */
1076 <    public void testThenApply4() {
1077 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1078 <        CompletableFuture<Integer> g = f.thenApply(inc);
1079 <        assertTrue(f.cancel(true));
1080 <        checkCompletedWithWrappedCancellationException(g);
1081 <    }
1082 <
1083 <    /**
1084 <     * thenAccept result completes normally after normal completion of source
1085 <     */
1086 <    public void testThenAccept() {
1087 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1088 <        IncAction r = new IncAction();
1089 <        CompletableFuture<Void> g = f.thenAccept(r);
1090 <        f.complete(one);
1091 <        checkCompletedNormally(g, null);
1092 <        assertEquals(r.value, (Integer) 2);
1093 <    }
1094 <
1095 <    /**
1096 <     * thenAccept result completes exceptionally after exceptional
1097 <     * completion of source
1098 <     */
1099 <    public void testThenAccept2() {
1100 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1101 <        IncAction r = new IncAction();
1102 <        CompletableFuture<Void> g = f.thenAccept(r);
1103 <        f.completeExceptionally(new CFException());
1104 <        checkCompletedWithWrappedCFException(g);
1105 <    }
1106 <
1107 <    /**
1108 <     * thenAccept result completes exceptionally if action does
1109 <     */
1110 <    public void testThenAccept3() {
1111 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1112 <        FailingConsumer r = new FailingConsumer();
1113 <        CompletableFuture<Void> g = f.thenAccept(r);
1114 <        f.complete(one);
1115 <        checkCompletedWithWrappedCFException(g);
1116 <        assertEquals(1, r.invocationCount);
1117 <    }
1118 <
1119 <    /**
1120 <     * thenAccept result completes exceptionally if source cancelled
1121 <     */
1122 <    public void testThenAccept4() {
1123 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1124 <        IncAction r = new IncAction();
1125 <        CompletableFuture<Void> g = f.thenAccept(r);
1126 <        assertTrue(f.cancel(true));
1127 <        checkCompletedWithWrappedCancellationException(g);
1128 <    }
1129 <
1130 <    /**
1131 <     * thenCombine result completes normally after normal completion
1132 <     * of sources
1133 <     */
1134 <    public void testThenCombine_normalCompletion1() {
1135 <        for (boolean createIncomplete : new boolean[] { true, false })
1136 <        for (boolean fFirst : new boolean[] { true, false })
1062 >    public void testThenRun_sourceCancelled() {
1063          for (ExecutionMode m : ExecutionMode.values())
1064 <        for (Integer v1 : new Integer[] { 1, null })
1065 <        for (Integer v2 : new Integer[] { 2, null })
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 CompletableFuture<Integer> g = new CompletableFuture<>();
1069 <        final SubtractFunction r = new SubtractFunction();
1070 <        CompletableFuture<Integer> h = null;
1071 <        if (createIncomplete) h = m.thenCombine(f, g, r);
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 <        if (fFirst)
1077 <            f.complete(v1);
1149 <        else
1150 <            g.complete(v2);
1151 <        if (createIncomplete) checkIncomplete(h);
1076 >        checkCompletedWithWrappedCancellationException(g);
1077 >        checkCancelled(f);
1078          assertEquals(0, r.invocationCount);
1153        if (!fFirst)
1154            f.complete(v1);
1155        else
1156            g.complete(v2);
1157        if (!createIncomplete) h = m.thenCombine(f, g, r);
1158
1159        checkCompletedNormally(h, subtract(v1, v2));
1160        checkCompletedNormally(f, v1);
1161        checkCompletedNormally(g, v2);
1162        assertEquals(1, r.invocationCount);
1079      }}
1080  
1081      /**
1082 <     * thenCombine result completes exceptionally after exceptional
1167 <     * completion of either source
1082 >     * thenRun result completes exceptionally if action does
1083       */
1084 <    public void testThenCombine_exceptionalCompletion1() {
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 CompletableFuture<Integer> g = new CompletableFuture<>();
1091 <        final SubtractFunction r = new SubtractFunction();
1092 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1093 <        final CFException ex = new CFException();
1094 <
1095 <        f.completeExceptionally(ex);
1096 <        checkIncomplete(h);
1181 <        g.complete(v1);
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 <        checkCompletedWithWrappedCFException(h, ex);
1099 <        checkCompletedWithWrappedCFException(f, ex);
1185 <        assertEquals(0, r.invocationCount);
1186 <        checkCompletedNormally(g, v1);
1098 >        checkCompletedWithWrappedCFException(g);
1099 >        checkCompletedNormally(f, v1);
1100      }}
1101  
1102 <    public void testThenCombine_exceptionalCompletion2() {
1102 >    /**
1103 >     * thenApply result completes normally after normal completion of source
1104 >     */
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 CompletableFuture<Integer> g = new CompletableFuture<>();
1112 <        final SubtractFunction r = new SubtractFunction();
1113 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1114 <        final CFException ex = new CFException();
1115 <
1116 <        g.completeExceptionally(ex);
1117 <        checkIncomplete(h);
1201 <        f.complete(v1);
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 <        checkCompletedWithWrappedCFException(h, ex);
1204 <        checkCompletedWithWrappedCFException(g, ex);
1205 <        assertEquals(0, r.invocationCount);
1119 >        checkCompletedNormally(g, inc(v1));
1120          checkCompletedNormally(f, v1);
1121 +        assertEquals(1, r.invocationCount);
1122      }}
1123  
1124 <    public void testThenCombine_exceptionalCompletion3() {
1124 >    /**
1125 >     * thenApply result completes exceptionally after exceptional
1126 >     * completion of source
1127 >     */
1128 >    public void testThenApply_exceptionalCompletion() {
1129          for (ExecutionMode m : ExecutionMode.values())
1130 <        for (Integer v1 : new Integer[] { 1, null })
1130 >        for (boolean createIncomplete : new boolean[] { true, false })
1131      {
1213        final CompletableFuture<Integer> f = new CompletableFuture<>();
1214        final CompletableFuture<Integer> g = new CompletableFuture<>();
1215        final SubtractFunction r = new SubtractFunction();
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  
1218        g.completeExceptionally(ex);
1219        f.complete(v1);
1220        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1221
1222        checkCompletedWithWrappedCFException(h, ex);
1142          checkCompletedWithWrappedCFException(g, ex);
1143 +        checkCompletedWithWrappedCFException(f, ex);
1144          assertEquals(0, r.invocationCount);
1225        checkCompletedNormally(f, v1);
1145      }}
1146  
1147 <    public void testThenCombine_exceptionalCompletion4() {
1147 >    /**
1148 >     * thenApply result completes exceptionally if source cancelled
1149 >     */
1150 >    public void testThenApply_sourceCancelled() {
1151          for (ExecutionMode m : ExecutionMode.values())
1152 <        for (Integer v1 : new Integer[] { 1, null })
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 CompletableFuture<Integer> g = new CompletableFuture<>();
1157 <        final SubtractFunction r = new SubtractFunction();
1158 <        final CFException ex = new CFException();
1159 <
1160 <        f.completeExceptionally(ex);
1161 <        g.complete(v1);
1162 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
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 <        checkCompletedWithWrappedCFException(h, ex);
1165 <        checkCompletedWithWrappedCFException(f, ex);
1164 >        checkCompletedWithWrappedCancellationException(g);
1165 >        checkCancelled(f);
1166          assertEquals(0, r.invocationCount);
1244        checkCompletedNormally(g, v1);
1167      }}
1168  
1169      /**
1170 <     * thenCombine result completes exceptionally if action does
1170 >     * thenApply result completes exceptionally if action does
1171       */
1172 <    public void testThenCombine_actionFailed1() {
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 })
1253        for (Integer v2 : new Integer[] { 2, null })
1176      {
1177          final CompletableFuture<Integer> f = new CompletableFuture<>();
1178 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1179 <        final FailingBiFunction r = new FailingBiFunction();
1180 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1181 <
1182 <        f.complete(v1);
1183 <        checkIncomplete(h);
1184 <        g.complete(v2);
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(h);
1186 >        checkCompletedWithWrappedCFException(g);
1187          checkCompletedNormally(f, v1);
1266        checkCompletedNormally(g, v2);
1188      }}
1189  
1190 <    public void testThenCombine_actionFailed2() {
1190 >    /**
1191 >     * thenAccept result completes normally after normal completion of source
1192 >     */
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 })
1272        for (Integer v2 : new Integer[] { 2, null })
1197      {
1198          final CompletableFuture<Integer> f = new CompletableFuture<>();
1199 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1200 <        final FailingBiFunction r = new FailingBiFunction();
1201 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1202 <
1203 <        g.complete(v2);
1204 <        checkIncomplete(h);
1205 <        f.complete(v1);
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 <        checkCompletedWithWrappedCFException(h);
1207 >        checkCompletedNormally(g, null);
1208          checkCompletedNormally(f, v1);
1209 <        checkCompletedNormally(g, v2);
1209 >        assertEquals(1, r.invocationCount);
1210 >        assertEquals(inc(v1), r.value);
1211      }}
1212  
1213      /**
1214 <     * thenCombine result completes exceptionally if either source cancelled
1214 >     * thenAccept result completes exceptionally after exceptional
1215 >     * completion of source
1216       */
1217 <    public void testThenCombine_sourceCancelled1() {
1217 >    public void testThenAccept_exceptionalCompletion() {
1218          for (ExecutionMode m : ExecutionMode.values())
1219 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1294 <        for (Integer v1 : new Integer[] { 1, null })
1219 >        for (boolean createIncomplete : new boolean[] { true, false })
1220      {
1221 +        final CFException ex = new CFException();
1222          final CompletableFuture<Integer> f = new CompletableFuture<>();
1223 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1224 <        final SubtractFunction r = new SubtractFunction();
1225 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1226 <
1227 <        assertTrue(f.cancel(mayInterruptIfRunning));
1228 <        checkIncomplete(h);
1229 <        g.complete(v1);
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 <        checkCompletedWithWrappedCancellationException(h);
1232 <        checkCancelled(f);
1231 >        checkCompletedWithWrappedCFException(g, ex);
1232 >        checkCompletedWithWrappedCFException(f, ex);
1233          assertEquals(0, r.invocationCount);
1308        checkCompletedNormally(g, v1);
1234      }}
1235  
1236 <    public void testThenCombine_sourceCancelled2() {
1236 >    /**
1237 >     * thenAccept result completes exceptionally if action does
1238 >     */
1239 >    public void testThenAccept_actionFailed() {
1240          for (ExecutionMode m : ExecutionMode.values())
1241 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
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 CompletableFuture<Integer> g = new CompletableFuture<>();
1246 <        final SubtractFunction r = new SubtractFunction();
1247 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1248 <
1249 <        assertTrue(g.cancel(mayInterruptIfRunning));
1250 <        checkIncomplete(h);
1251 <        f.complete(v1);
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 <        checkCompletedWithWrappedCancellationException(h);
1326 <        checkCancelled(g);
1327 <        assertEquals(0, r.invocationCount);
1253 >        checkCompletedWithWrappedCFException(g);
1254          checkCompletedNormally(f, v1);
1255      }}
1256  
1257 <    public void testThenCombine_sourceCancelled3() {
1257 >    /**
1258 >     * thenAccept result completes exceptionally if source cancelled
1259 >     */
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 })
1334        for (Integer v1 : new Integer[] { 1, null })
1264      {
1265          final CompletableFuture<Integer> f = new CompletableFuture<>();
1266 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1267 <        final SubtractFunction r = new SubtractFunction();
1268 <
1269 <        assertTrue(g.cancel(mayInterruptIfRunning));
1270 <        f.complete(v1);
1271 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
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(h);
1275 <        checkCancelled(g);
1274 >        checkCompletedWithWrappedCancellationException(g);
1275 >        checkCancelled(f);
1276          assertEquals(0, r.invocationCount);
1347        checkCompletedNormally(f, v1);
1277      }}
1278  
1279 <    public void testThenCombine_sourceCancelled4() {
1279 >    /**
1280 >     * thenCombine result completes normally after normal completion
1281 >     * of sources
1282 >     */
1283 >    public void testThenCombine_normalCompletion() {
1284          for (ExecutionMode m : ExecutionMode.values())
1285 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1285 >        for (boolean createIncomplete : new boolean[] { true, false })
1286 >        for (boolean fFirst : new boolean[] { true, false })
1287          for (Integer v1 : new Integer[] { 1, null })
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();
1292 >        final SubtractFunction r = new SubtractFunction(m);
1293  
1294 <        assertTrue(f.cancel(mayInterruptIfRunning));
1295 <        g.complete(v1);
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 <        checkCompletedWithWrappedCancellationException(h);
1305 <        checkCancelled(f);
1306 <        assertEquals(0, r.invocationCount);
1307 <        checkCompletedNormally(g, v1);
1304 >        checkCompletedNormally(h, subtract(v1, v2));
1305 >        checkCompletedNormally(f, v1);
1306 >        checkCompletedNormally(g, v2);
1307 >        assertEquals(1, r.invocationCount);
1308      }}
1309  
1310      /**
1311 <     * thenAcceptBoth result completes normally after normal
1312 <     * completion of sources
1311 >     * thenCombine result completes exceptionally after exceptional
1312 >     * completion of either source
1313       */
1314 <    public void testThenAcceptBoth_normalCompletion1() {
1314 >    public void testThenCombine_exceptionalCompletion() {
1315          for (ExecutionMode m : ExecutionMode.values())
1316 +        for (boolean createIncomplete : new boolean[] { true, false })
1317 +        for (boolean fFirst : new boolean[] { true, false })
1318          for (Integer v1 : new Integer[] { 1, null })
1376        for (Integer v2 : new Integer[] { 2, null })
1319      {
1320          final CompletableFuture<Integer> f = new CompletableFuture<>();
1321          final CompletableFuture<Integer> g = new CompletableFuture<>();
1322 <        final SubtractAction r = new SubtractAction();
1323 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1322 >        final CFException ex = new CFException();
1323 >        final SubtractFunction r = new SubtractFunction(m);
1324  
1325 <        f.complete(v1);
1326 <        checkIncomplete(h);
1327 <        assertEquals(0, r.invocationCount);
1328 <        g.complete(v2);
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 >        if (createIncomplete) {
1330 >            checkIncomplete(h);
1331 >            (!fFirst ? f : g).completeExceptionally(ex);
1332 >        }
1333  
1334 <        checkCompletedNormally(h, null);
1335 <        assertEquals(subtract(v1, v2), r.value);
1336 <        checkCompletedNormally(f, v1);
1337 <        checkCompletedNormally(g, v2);
1334 >        checkCompletedWithWrappedCFException(h, ex);
1335 >        assertEquals(0, r.invocationCount);
1336 >        checkCompletedNormally(fFirst ? f : g, v1);
1337 >        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1338      }}
1339  
1340 <    public void testThenAcceptBoth_normalCompletion2() {
1340 >    /**
1341 >     * thenCombine result completes exceptionally if action does
1342 >     */
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      {
1349          final CompletableFuture<Integer> f = new CompletableFuture<>();
1350          final CompletableFuture<Integer> g = new CompletableFuture<>();
1351 <        final SubtractAction r = new SubtractAction();
1352 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1351 >        final FailingBiFunction r = new FailingBiFunction(m);
1352 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1353  
1354 <        g.complete(v2);
1355 <        checkIncomplete(h);
1356 <        assertEquals(0, r.invocationCount);
1357 <        f.complete(v1);
1354 >        if (fFirst) {
1355 >            f.complete(v1);
1356 >            g.complete(v2);
1357 >        } else {
1358 >            g.complete(v2);
1359 >            f.complete(v1);
1360 >        }
1361  
1362 <        checkCompletedNormally(h, null);
1410 <        assertEquals(subtract(v1, v2), r.value);
1362 >        checkCompletedWithWrappedCFException(h);
1363          checkCompletedNormally(f, v1);
1364          checkCompletedNormally(g, v2);
1365      }}
1366  
1367 <    public void testThenAcceptBoth_normalCompletion3() {
1367 >    /**
1368 >     * thenCombine result completes exceptionally if either source cancelled
1369 >     */
1370 >    public void testThenCombine_sourceCancelled() {
1371          for (ExecutionMode m : ExecutionMode.values())
1372 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1373 +        for (boolean createIncomplete : new boolean[] { true, false })
1374 +        for (boolean fFirst : new boolean[] { true, false })
1375          for (Integer v1 : new Integer[] { 1, null })
1418        for (Integer v2 : new Integer[] { 2, null })
1376      {
1377          final CompletableFuture<Integer> f = new CompletableFuture<>();
1378          final CompletableFuture<Integer> g = new CompletableFuture<>();
1379 <        final SubtractAction r = new SubtractAction();
1379 >        final SubtractFunction r = new SubtractFunction(m);
1380  
1381 <        g.complete(v2);
1382 <        f.complete(v1);
1383 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
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 >        if (createIncomplete) {
1386 >            checkIncomplete(h);
1387 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1388 >        }
1389  
1390 <        checkCompletedNormally(h, null);
1391 <        assertEquals(subtract(v1, v2), r.value);
1392 <        checkCompletedNormally(f, v1);
1393 <        checkCompletedNormally(g, v2);
1390 >        checkCompletedWithWrappedCancellationException(h);
1391 >        checkCancelled(!fFirst ? f : g);
1392 >        assertEquals(0, r.invocationCount);
1393 >        checkCompletedNormally(fFirst ? f : g, v1);
1394      }}
1395  
1396 <    public void testThenAcceptBoth_normalCompletion4() {
1396 >    /**
1397 >     * thenAcceptBoth result completes normally after normal
1398 >     * completion of sources
1399 >     */
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      {
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 <        f.complete(v1);
1412 <        g.complete(v2);
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 +        if (createIncomplete) {
1416 +            checkIncomplete(h);
1417 +            assertEquals(0, r.invocationCount);
1418 +            if (!fFirst) f.complete(v1); else g.complete(v2);
1419 +        }
1420  
1421          checkCompletedNormally(h, null);
1422          assertEquals(subtract(v1, v2), r.value);
# Line 1454 | Line 1428 | public class CompletableFutureTest exten
1428       * thenAcceptBoth result completes exceptionally after exceptional
1429       * completion of either source
1430       */
1431 <    public void testThenAcceptBoth_exceptionalCompletion1() {
1458 <        for (ExecutionMode m : ExecutionMode.values())
1459 <        for (Integer v1 : new Integer[] { 1, null })
1460 <    {
1461 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1462 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1463 <        final SubtractAction r = new SubtractAction();
1464 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1465 <        final CFException ex = new CFException();
1466 <
1467 <        f.completeExceptionally(ex);
1468 <        checkIncomplete(h);
1469 <        g.complete(v1);
1470 <
1471 <        checkCompletedWithWrappedCFException(h, ex);
1472 <        checkCompletedWithWrappedCFException(f, ex);
1473 <        assertEquals(0, r.invocationCount);
1474 <        checkCompletedNormally(g, v1);
1475 <    }}
1476 <
1477 <    public void testThenAcceptBoth_exceptionalCompletion2() {
1478 <        for (ExecutionMode m : ExecutionMode.values())
1479 <        for (Integer v1 : new Integer[] { 1, null })
1480 <    {
1481 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1482 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1483 <        final SubtractAction r = new SubtractAction();
1484 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1485 <        final CFException ex = new CFException();
1486 <
1487 <        g.completeExceptionally(ex);
1488 <        checkIncomplete(h);
1489 <        f.complete(v1);
1490 <
1491 <        checkCompletedWithWrappedCFException(h, ex);
1492 <        checkCompletedWithWrappedCFException(g, ex);
1493 <        assertEquals(0, r.invocationCount);
1494 <        checkCompletedNormally(f, v1);
1495 <    }}
1496 <
1497 <    public void testThenAcceptBoth_exceptionalCompletion3() {
1431 >    public void testThenAcceptBoth_exceptionalCompletion() {
1432          for (ExecutionMode m : ExecutionMode.values())
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<>();
1503        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 +        if (createIncomplete) {
1447 +            checkIncomplete(h);
1448 +            (!fFirst ? f : g).completeExceptionally(ex);
1449 +        }
1450  
1451          checkCompletedWithWrappedCFException(h, ex);
1511        checkCompletedWithWrappedCFException(g, ex);
1452          assertEquals(0, r.invocationCount);
1453 <        checkCompletedNormally(f, v1);
1454 <    }}
1515 <
1516 <    public void testThenAcceptBoth_exceptionalCompletion4() {
1517 <        for (ExecutionMode m : ExecutionMode.values())
1518 <        for (Integer v1 : new Integer[] { 1, null })
1519 <    {
1520 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1521 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1522 <        final SubtractAction r = new SubtractAction();
1523 <        final CFException ex = new CFException();
1524 <
1525 <        f.completeExceptionally(ex);
1526 <        g.complete(v1);
1527 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1528 <
1529 <        checkCompletedWithWrappedCFException(h, ex);
1530 <        checkCompletedWithWrappedCFException(f, ex);
1531 <        assertEquals(0, r.invocationCount);
1532 <        checkCompletedNormally(g, v1);
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() {
1539 <        for (ExecutionMode m : ExecutionMode.values())
1540 <        for (Integer v1 : new Integer[] { 1, null })
1541 <        for (Integer v2 : new Integer[] { 2, null })
1542 <    {
1543 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1544 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1545 <        final FailingBiConsumer r = new FailingBiConsumer();
1546 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1547 <
1548 <        f.complete(v1);
1549 <        checkIncomplete(h);
1550 <        g.complete(v2);
1551 <
1552 <        checkCompletedWithWrappedCFException(h);
1553 <        checkCompletedNormally(f, v1);
1554 <        checkCompletedNormally(g, v2);
1555 <    }}
1556 <
1557 <    public void testThenAcceptBoth_actionFailed2() {
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      {
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 <        g.complete(v2);
1472 <        checkIncomplete(h);
1473 <        f.complete(v1);
1471 >        if (fFirst) {
1472 >            f.complete(v1);
1473 >            g.complete(v2);
1474 >        } else {
1475 >            g.complete(v2);
1476 >            f.complete(v1);
1477 >        }
1478  
1479          checkCompletedWithWrappedCFException(h);
1480          checkCompletedNormally(f, v1);
# Line 1576 | Line 1484 | public class CompletableFutureTest exten
1484      /**
1485       * thenAcceptBoth result completes exceptionally if either source cancelled
1486       */
1487 <    public void testThenAcceptBoth_sourceCancelled1() {
1580 <        for (ExecutionMode m : ExecutionMode.values())
1581 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1582 <        for (Integer v1 : new Integer[] { 1, null })
1583 <    {
1584 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1585 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1586 <        final SubtractAction r = new SubtractAction();
1587 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1588 <
1589 <        assertTrue(f.cancel(mayInterruptIfRunning));
1590 <        checkIncomplete(h);
1591 <        g.complete(v1);
1592 <
1593 <        checkCompletedWithWrappedCancellationException(h);
1594 <        checkCancelled(f);
1595 <        assertEquals(0, r.invocationCount);
1596 <        checkCompletedNormally(g, v1);
1597 <    }}
1598 <
1599 <    public void testThenAcceptBoth_sourceCancelled2() {
1600 <        for (ExecutionMode m : ExecutionMode.values())
1601 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1602 <        for (Integer v1 : new Integer[] { 1, null })
1603 <    {
1604 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1605 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1606 <        final SubtractAction r = new SubtractAction();
1607 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1608 <
1609 <        assertTrue(g.cancel(mayInterruptIfRunning));
1610 <        checkIncomplete(h);
1611 <        f.complete(v1);
1612 <
1613 <        checkCompletedWithWrappedCancellationException(h);
1614 <        checkCancelled(g);
1615 <        assertEquals(0, r.invocationCount);
1616 <        checkCompletedNormally(f, v1);
1617 <    }}
1618 <
1619 <    public void testThenAcceptBoth_sourceCancelled3() {
1620 <        for (ExecutionMode m : ExecutionMode.values())
1621 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1622 <        for (Integer v1 : new Integer[] { 1, null })
1623 <    {
1624 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1625 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1626 <        final SubtractAction r = new SubtractAction();
1627 <
1628 <        assertTrue(g.cancel(mayInterruptIfRunning));
1629 <        f.complete(v1);
1630 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1631 <
1632 <        checkCompletedWithWrappedCancellationException(h);
1633 <        checkCancelled(g);
1634 <        assertEquals(0, r.invocationCount);
1635 <        checkCompletedNormally(f, v1);
1636 <    }}
1637 <
1638 <    public void testThenAcceptBoth_sourceCancelled4() {
1487 >    public void testThenAcceptBoth_sourceCancelled() {
1488          for (ExecutionMode m : ExecutionMode.values())
1489          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
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(f.cancel(mayInterruptIfRunning));
1499 <        g.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 +        if (createIncomplete) {
1503 +            checkIncomplete(h);
1504 +            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1505 +        }
1506  
1507          checkCompletedWithWrappedCancellationException(h);
1508 <        checkCancelled(f);
1508 >        checkCancelled(!fFirst ? f : g);
1509          assertEquals(0, r.invocationCount);
1510 <        checkCompletedNormally(g, v1);
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() {
1662 <        for (ExecutionMode m : ExecutionMode.values())
1663 <        for (Integer v1 : new Integer[] { 1, null })
1664 <        for (Integer v2 : new Integer[] { 2, null })
1665 <    {
1666 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1667 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1668 <        final Noop r = new Noop();
1669 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1670 <
1671 <        f.complete(v1);
1672 <        checkIncomplete(h);
1673 <        assertEquals(0, r.invocationCount);
1674 <        g.complete(v2);
1675 <
1676 <        checkCompletedNormally(h, null);
1677 <        assertEquals(1, r.invocationCount);
1678 <        checkCompletedNormally(f, v1);
1679 <        checkCompletedNormally(g, v2);
1680 <    }}
1681 <
1682 <    public void testRunAfterBoth_normalCompletion2() {
1683 <        for (ExecutionMode m : ExecutionMode.values())
1684 <        for (Integer v1 : new Integer[] { 1, null })
1685 <        for (Integer v2 : new Integer[] { 2, null })
1686 <    {
1687 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1688 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1689 <        final Noop r = new Noop();
1690 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1691 <
1692 <        g.complete(v2);
1693 <        checkIncomplete(h);
1694 <        assertEquals(0, r.invocationCount);
1695 <        f.complete(v1);
1696 <
1697 <        checkCompletedNormally(h, null);
1698 <        assertEquals(1, r.invocationCount);
1699 <        checkCompletedNormally(f, v1);
1700 <        checkCompletedNormally(g, v2);
1701 <    }}
1702 <
1703 <    public void testRunAfterBoth_normalCompletion3() {
1704 <        for (ExecutionMode m : ExecutionMode.values())
1705 <        for (Integer v1 : new Integer[] { 1, null })
1706 <        for (Integer v2 : new Integer[] { 2, null })
1707 <    {
1708 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1709 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1710 <        final Noop r = new Noop();
1711 <
1712 <        g.complete(v2);
1713 <        f.complete(v1);
1714 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1715 <
1716 <        checkCompletedNormally(h, null);
1717 <        assertEquals(1, r.invocationCount);
1718 <        checkCompletedNormally(f, v1);
1719 <        checkCompletedNormally(g, v2);
1720 <    }}
1721 <
1722 <    public void testRunAfterBoth_normalCompletion4() {
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      {
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 <        f.complete(v1);
1529 <        g.complete(v2);
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 +        if (createIncomplete) {
1533 +            checkIncomplete(h);
1534 +            assertEquals(0, r.invocationCount);
1535 +            if (!fFirst) f.complete(v1); else g.complete(v2);
1536 +        }
1537  
1538          checkCompletedNormally(h, null);
1539          assertEquals(1, r.invocationCount);
# Line 1742 | Line 1545 | public class CompletableFutureTest exten
1545       * runAfterBoth result completes exceptionally after exceptional
1546       * completion of either source
1547       */
1548 <    public void testRunAfterBoth_exceptionalCompletion1() {
1746 <        for (ExecutionMode m : ExecutionMode.values())
1747 <        for (Integer v1 : new Integer[] { 1, null })
1748 <    {
1749 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1750 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1751 <        final Noop r = new Noop();
1752 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1753 <        final CFException ex = new CFException();
1754 <
1755 <        f.completeExceptionally(ex);
1756 <        checkIncomplete(h);
1757 <        g.complete(v1);
1758 <
1759 <        checkCompletedWithWrappedCFException(h, ex);
1760 <        checkCompletedWithWrappedCFException(f, ex);
1761 <        assertEquals(0, r.invocationCount);
1762 <        checkCompletedNormally(g, v1);
1763 <    }}
1764 <
1765 <    public void testRunAfterBoth_exceptionalCompletion2() {
1766 <        for (ExecutionMode m : ExecutionMode.values())
1767 <        for (Integer v1 : new Integer[] { 1, null })
1768 <    {
1769 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1770 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1771 <        final Noop r = new Noop();
1772 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1773 <        final CFException ex = new CFException();
1774 <
1775 <        g.completeExceptionally(ex);
1776 <        checkIncomplete(h);
1777 <        f.complete(v1);
1778 <
1779 <        checkCompletedWithWrappedCFException(h, ex);
1780 <        checkCompletedWithWrappedCFException(g, ex);
1781 <        assertEquals(0, r.invocationCount);
1782 <        checkCompletedNormally(f, v1);
1783 <    }}
1784 <
1785 <    public void testRunAfterBoth_exceptionalCompletion3() {
1786 <        for (ExecutionMode m : ExecutionMode.values())
1787 <        for (Integer v1 : new Integer[] { 1, null })
1788 <    {
1789 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1790 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1791 <        final Noop r = new Noop();
1792 <        final CFException ex = new CFException();
1793 <
1794 <        g.completeExceptionally(ex);
1795 <        f.complete(v1);
1796 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1797 <
1798 <        checkCompletedWithWrappedCFException(h, ex);
1799 <        checkCompletedWithWrappedCFException(g, ex);
1800 <        assertEquals(0, r.invocationCount);
1801 <        checkCompletedNormally(f, v1);
1802 <    }}
1803 <
1804 <    public void testRunAfterBoth_exceptionalCompletion4() {
1548 >    public void testRunAfterBoth_exceptionalCompletion() {
1549          for (ExecutionMode m : ExecutionMode.values())
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<>();
1810        final Noop r = new Noop();
1556          final CFException ex = new CFException();
1557 +        final Noop r = new Noop(m);
1558  
1559 <        f.completeExceptionally(ex);
1560 <        g.complete(v1);
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 +        if (createIncomplete) {
1564 +            checkIncomplete(h);
1565 +            (!fFirst ? f : g).completeExceptionally(ex);
1566 +        }
1567  
1568          checkCompletedWithWrappedCFException(h, ex);
1818        checkCompletedWithWrappedCFException(f, ex);
1569          assertEquals(0, r.invocationCount);
1570 <        checkCompletedNormally(g, v1);
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() {
1827 <        for (ExecutionMode m : ExecutionMode.values())
1828 <        for (Integer v1 : new Integer[] { 1, null })
1829 <        for (Integer v2 : new Integer[] { 2, null })
1830 <    {
1831 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1832 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1833 <        final FailingNoop r = new FailingNoop();
1834 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1835 <
1836 <        f.complete(v1);
1837 <        checkIncomplete(h);
1838 <        g.complete(v2);
1839 <
1840 <        checkCompletedWithWrappedCFException(h);
1841 <        checkCompletedNormally(f, v1);
1842 <        checkCompletedNormally(g, v2);
1843 <    }}
1844 <
1845 <    public void testRunAfterBoth_actionFailed2() {
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      {
1583          final CompletableFuture<Integer> f = new CompletableFuture<>();
1584          final CompletableFuture<Integer> g = new CompletableFuture<>();
1585 <        final FailingNoop r = new FailingNoop();
1853 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1585 >        final FailingRunnable r = new FailingRunnable(m);
1586  
1587 <        g.complete(v2);
1588 <        checkIncomplete(h);
1589 <        f.complete(v1);
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 >        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      }}
# Line 1864 | Line 1603 | public class CompletableFutureTest exten
1603      /**
1604       * runAfterBoth result completes exceptionally if either source cancelled
1605       */
1606 <    public void testRunAfterBoth_sourceCancelled1() {
1868 <        for (ExecutionMode m : ExecutionMode.values())
1869 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1870 <        for (Integer v1 : new Integer[] { 1, null })
1871 <    {
1872 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1873 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1874 <        final Noop r = new Noop();
1875 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1876 <
1877 <        assertTrue(f.cancel(mayInterruptIfRunning));
1878 <        checkIncomplete(h);
1879 <        g.complete(v1);
1880 <
1881 <        checkCompletedWithWrappedCancellationException(h);
1882 <        checkCancelled(f);
1883 <        assertEquals(0, r.invocationCount);
1884 <        checkCompletedNormally(g, v1);
1885 <    }}
1886 <
1887 <    public void testRunAfterBoth_sourceCancelled2() {
1888 <        for (ExecutionMode m : ExecutionMode.values())
1889 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1890 <        for (Integer v1 : new Integer[] { 1, null })
1891 <    {
1892 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1893 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1894 <        final Noop r = new Noop();
1895 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1896 <
1897 <        assertTrue(g.cancel(mayInterruptIfRunning));
1898 <        checkIncomplete(h);
1899 <        f.complete(v1);
1900 <
1901 <        checkCompletedWithWrappedCancellationException(h);
1902 <        checkCancelled(g);
1903 <        assertEquals(0, r.invocationCount);
1904 <        checkCompletedNormally(f, v1);
1905 <    }}
1906 <
1907 <    public void testRunAfterBoth_sourceCancelled3() {
1606 >    public void testRunAfterBoth_sourceCancelled() {
1607          for (ExecutionMode m : ExecutionMode.values())
1608          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
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();
1915 <
1916 <        assertTrue(g.cancel(mayInterruptIfRunning));
1917 <        f.complete(v1);
1918 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1615 >        final Noop r = new Noop(m);
1616  
1920        checkCompletedWithWrappedCancellationException(h);
1921        checkCancelled(g);
1922        assertEquals(0, r.invocationCount);
1923        checkCompletedNormally(f, v1);
1924    }}
1925
1926    public void testRunAfterBoth_sourceCancelled4() {
1927        for (ExecutionMode m : ExecutionMode.values())
1928        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1929        for (Integer v1 : new Integer[] { 1, null })
1930    {
1931        final CompletableFuture<Integer> f = new CompletableFuture<>();
1932        final CompletableFuture<Integer> g = new CompletableFuture<>();
1933        final Noop r = new Noop();
1617  
1618 <        assertTrue(f.cancel(mayInterruptIfRunning));
1619 <        g.complete(v1);
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 +        if (createIncomplete) {
1623 +            checkIncomplete(h);
1624 +            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1625 +        }
1626  
1627          checkCompletedWithWrappedCancellationException(h);
1628 <        checkCancelled(f);
1628 >        checkCancelled(!fFirst ? f : g);
1629          assertEquals(0, r.invocationCount);
1630 <        checkCompletedNormally(g, v1);
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      {
1644          final CompletableFuture<Integer> f = new CompletableFuture<>();
1645          final CompletableFuture<Integer> g = new CompletableFuture<>();
1646 <        final IncFunction r = new IncFunction();
1957 <        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));
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      {
1670          final CompletableFuture<Integer> f = new CompletableFuture<>();
1671          final CompletableFuture<Integer> g = new CompletableFuture<>();
1672 <        final IncFunction r = new IncFunction();
1976 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1977 <
1978 <        g.complete(v2);
1979 <        checkCompletedNormally(h, inc(v2));
1980 <        f.complete(v1);
1981 <
1982 <        checkCompletedNormally(f, v1);
1983 <        checkCompletedNormally(g, v2);
1984 <        checkCompletedNormally(h, inc(v2));
1985 <        }}
1672 >        final IncFunction r = new IncFunction(m);
1673  
1674 <    public void testApplyToEither_normalCompletion3() {
1675 <        for (ExecutionMode m : ExecutionMode.values())
1676 <        for (Integer v1 : new Integer[] { 1, null })
1677 <        for (Integer v2 : new Integer[] { 2, null })
1678 <    {
1679 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1680 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1994 <        final IncFunction r = new IncFunction();
1674 >        if (fFirst) {
1675 >            f.complete(v1);
1676 >            g.complete(v2);
1677 >        } else {
1678 >            g.complete(v2);
1679 >            f.complete(v1);
1680 >        }
1681  
1996        f.complete(v1);
1997        g.complete(v2);
1682          final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1683  
1684          checkCompletedNormally(f, v1);
# Line 2012 | Line 1696 | public class CompletableFutureTest exten
1696       */
1697      public void testApplyToEither_exceptionalCompletion1() {
1698          for (ExecutionMode m : ExecutionMode.values())
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<>();
2019        final IncFunction r = new IncFunction();
2020        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);
2024 <        checkCompletedWithWrappedCFException(h, ex);
2025 <        g.complete(v1);
2026 <
2027 <        assertEquals(0, r.invocationCount);
2028 <        checkCompletedNormally(g, v1);
2029 <        checkCompletedWithWrappedCFException(f, ex);
2030 <        checkCompletedWithWrappedCFException(h, ex);
2031 <    }}
2032 <
2033 <    public void testApplyToEither_exceptionalCompletion2() {
2034 <        for (ExecutionMode m : ExecutionMode.values())
2035 <        for (Integer v1 : new Integer[] { 1, null })
2036 <    {
2037 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2038 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2039 <        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  
2043        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      }}
1724  
1725 <    public void testApplyToEither_exceptionalCompletion3() {
1725 >    public void testApplyToEither_exceptionalCompletion2() {
1726          for (ExecutionMode m : ExecutionMode.values())
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
2067        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);
1753 >            checkCompletedWithWrappedCFException(h1, ex);
1754 >            assertEquals(0, r1.invocationCount);
1755          }
1756  
2076        checkCompletedWithWrappedCFException(g, ex);
2077        checkCompletedNormally(f, v1);
2078    }}
2079
2080    public void testApplyToEither_exceptionalCompletion4() {
2081        for (ExecutionMode m : ExecutionMode.values())
2082        for (Integer v1 : new Integer[] { 1, null })
2083    {
2084        final CompletableFuture<Integer> f = new CompletableFuture<>();
2085        final CompletableFuture<Integer> g = new CompletableFuture<>();
2086        final IncFunction r = new IncFunction();
2087        final CFException ex = new CFException();
2088
2089        f.completeExceptionally(ex);
2090        g.complete(v1);
2091        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2092
2093        // unspecified behavior
2094        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);
1765 >        checkCompletedWithWrappedCFException(g, ex);
1766 >        checkCompletedNormally(f, v1);
1767      }}
1768  
1769      /**
# Line 2114 | Line 1776 | public class CompletableFutureTest exten
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 2131 | Line 1793 | public class CompletableFutureTest exten
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 2147 | 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 (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();
2155 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1818 >        final IncFunction r = new IncFunction(m);
1819  
1820 <        assertTrue(f.cancel(mayInterruptIfRunning));
2158 <        checkCompletedWithWrappedCancellationException(h);
2159 <        g.complete(v1);
2160 <
2161 <        checkCancelled(f);
2162 <        assertEquals(0, r.invocationCount);
2163 <        checkCompletedNormally(g, v1);
2164 <        checkCompletedWithWrappedCancellationException(h);
2165 <    }}
2166 <
2167 <    public void testApplyToEither_sourceCancelled2() {
2168 <        for (ExecutionMode m : ExecutionMode.values())
2169 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2170 <        for (Integer v1 : new Integer[] { 1, null })
2171 <    {
2172 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2173 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2174 <        final IncFunction r = new IncFunction();
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  
2177        assertTrue(g.cancel(mayInterruptIfRunning));
1828          checkCompletedWithWrappedCancellationException(h);
1829 <        f.complete(v1);
1829 >        (!fFirst ? f : g).complete(v1);
1830  
2181        checkCancelled(g);
1831          assertEquals(0, r.invocationCount);
1832 <        checkCompletedNormally(f, v1);
1832 >        checkCompletedNormally(!fFirst ? f : g, v1);
1833 >        checkCancelled(fFirst ? f : g);
1834          checkCompletedWithWrappedCancellationException(h);
1835      }}
1836  
1837 <    public void testApplyToEither_sourceCancelled3() {
1837 >    public void testApplyToEither_sourceCancelled2() {
1838          for (ExecutionMode m : ExecutionMode.values())
1839          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
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();
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 <        assertTrue(g.cancel(mayInterruptIfRunning));
1853 <        f.complete(v1);
1854 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
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 >        final CompletableFuture<Integer> h2 = m.applyToEither(j, k, r2);
1861  
1862          // unspecified behavior
2201        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  
2210        checkCancelled(g);
2211        checkCompletedNormally(f, v1);
2212    }}
2213
2214    public void testApplyToEither_sourceCancelled4() {
2215        for (ExecutionMode m : ExecutionMode.values())
2216        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2217        for (Integer v1 : new Integer[] { 1, null })
2218    {
2219        final CompletableFuture<Integer> f = new CompletableFuture<>();
2220        final CompletableFuture<Integer> g = new CompletableFuture<>();
2221        final IncFunction r = new IncFunction();
2222
2223        assertTrue(f.cancel(mayInterruptIfRunning));
2224        g.complete(v1);
2225        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2226
2227        // unspecified behavior
2228        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);
1879 >        checkCancelled(g);
1880 >        checkCompletedNormally(f, v1);
1881      }}
1882  
1883      /**
# Line 2414 | Line 2056 | public class CompletableFutureTest exten
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 2431 | Line 2073 | public class CompletableFutureTest exten
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 2551 | Line 2193 | public class CompletableFutureTest exten
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 2572 | Line 2214 | public class CompletableFutureTest exten
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 2593 | Line 2235 | public class CompletableFutureTest exten
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 2615 | Line 2257 | public class CompletableFutureTest exten
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 2635 | Line 2277 | public class CompletableFutureTest exten
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 2655 | Line 2297 | public class CompletableFutureTest exten
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 2682 | Line 2324 | public class CompletableFutureTest exten
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 2713 | Line 2355 | public class CompletableFutureTest exten
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 2730 | Line 2372 | public class CompletableFutureTest exten
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 2750 | Line 2392 | public class CompletableFutureTest exten
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 2770 | Line 2412 | public class CompletableFutureTest exten
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 2790 | Line 2432 | public class CompletableFutureTest exten
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 2817 | Line 2459 | public class CompletableFutureTest exten
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 2840 | Line 2482 | public class CompletableFutureTest exten
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 (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));
2852 <        checkCompletedNormally(f, v1);
2853 <        assertEquals(1, r.invocationCount);
2854 <    }}
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  
2856    public void testThenCompose_normalCompletion2() {
2857        for (ExecutionMode m : ExecutionMode.values())
2858        for (Integer v1 : new Integer[] { 1, null })
2859    {
2860        final CompletableFuture<Integer> f = new CompletableFuture<>();
2861        final CompletableFutureInc r = new CompletableFutureInc();
2862        f.complete(v1);
2863        final CompletableFuture<Integer> g = f.thenCompose(r);
2496          checkCompletedNormally(g, inc(v1));
2497          checkCompletedNormally(f, v1);
2498          assertEquals(1, r.invocationCount);
# Line 2870 | Line 2502 | public class CompletableFutureTest exten
2502       * thenCompose result completes exceptionally after exceptional
2503       * completion of source
2504       */
2505 <    public void testThenCompose_exceptionalCompletion1() {
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);
2882 <        checkCompletedWithWrappedCFException(f, ex);
2883 <    }}
2512 >        if (!createIncomplete) f.completeExceptionally(ex);
2513 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2514 >        if (createIncomplete) f.completeExceptionally(ex);
2515  
2885    public void testThenCompose_exceptionalCompletion2() {
2886        for (ExecutionMode m : ExecutionMode.values())
2887    {
2888        final CFException ex = new CFException();
2889        final CompletableFuture<Integer> f = new CompletableFuture<>();
2890        f.completeExceptionally(ex);
2891        final CompletableFutureInc r = new CompletableFutureInc();
2892        final CompletableFuture<Integer> g = f.thenCompose(r);
2516          checkCompletedWithWrappedCFException(g, ex);
2517          checkCompletedWithWrappedCFException(f, ex);
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 (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);
2910 <        checkCompletedNormally(f, v1);
2911 <    }}
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  
2913    public void testThenCompose_actionFailed2() {
2914        for (ExecutionMode m : ExecutionMode.values())
2915        for (Integer v1 : new Integer[] { 1, null })
2916    {
2917        final CompletableFuture<Integer> f = new CompletableFuture<>();
2918        f.complete(v1);
2919        final FailingCompletableFutureFunction r
2920            = new FailingCompletableFutureFunction();
2921        final CompletableFuture<Integer> g = f.thenCompose(r);
2536          checkCompletedWithWrappedCFException(g);
2537          checkCompletedNormally(f, v1);
2538      }}
# Line 2926 | Line 2540 | public class CompletableFutureTest exten
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 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);
2554 <    }}
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 >        }
2556  
2941    public void testThenCompose_sourceCancelled2() {
2942        for (ExecutionMode m : ExecutionMode.values())
2943        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2944    {
2945        final CompletableFuture<Integer> f = new CompletableFuture<>();
2946        assertTrue(f.cancel(mayInterruptIfRunning));
2947        final CompletableFutureInc r = new CompletableFutureInc();
2948        final CompletableFuture<Integer> g = f.thenCompose(r);
2557          checkCompletedWithWrappedCancellationException(g);
2558          checkCancelled(f);
2559      }}
2560  
2953    // asyncs
2954
2955    /**
2956     * thenRunAsync result completes normally after normal completion of source
2957     */
2958    public void testThenRunAsync() {
2959        CompletableFuture<Integer> f = new CompletableFuture<>();
2960        Noop r = new Noop();
2961        CompletableFuture<Void> g = f.thenRunAsync(r);
2962        f.complete(null);
2963        checkCompletedNormally(g, null);
2964
2965        // reordered version
2966        f = new CompletableFuture<>();
2967        f.complete(null);
2968        r = new Noop();
2969        g = f.thenRunAsync(r);
2970        checkCompletedNormally(g, null);
2971    }
2972
2973    /**
2974     * thenRunAsync result completes exceptionally after exceptional
2975     * completion of source
2976     */
2977    public void testThenRunAsync2() {
2978        CompletableFuture<Integer> f = new CompletableFuture<>();
2979        Noop r = new Noop();
2980        CompletableFuture<Void> g = f.thenRunAsync(r);
2981        f.completeExceptionally(new CFException());
2982        try {
2983            g.join();
2984            shouldThrow();
2985        } catch (CompletionException success) {}
2986        checkCompletedWithWrappedCFException(g);
2987    }
2988
2989    /**
2990     * thenRunAsync result completes exceptionally if action does
2991     */
2992    public void testThenRunAsync3() {
2993        CompletableFuture<Integer> f = new CompletableFuture<>();
2994        FailingNoop r = new FailingNoop();
2995        CompletableFuture<Void> g = f.thenRunAsync(r);
2996        f.complete(null);
2997        checkCompletedWithWrappedCFException(g);
2998    }
2999
3000    /**
3001     * thenRunAsync result completes exceptionally if source cancelled
3002     */
3003    public void testThenRunAsync4() {
3004        CompletableFuture<Integer> f = new CompletableFuture<>();
3005        Noop r = new Noop();
3006        CompletableFuture<Void> g = f.thenRunAsync(r);
3007        assertTrue(f.cancel(true));
3008        checkCompletedWithWrappedCancellationException(g);
3009    }
3010
3011    /**
3012     * thenApplyAsync result completes normally after normal completion of source
3013     */
3014    public void testThenApplyAsync() {
3015        CompletableFuture<Integer> f = new CompletableFuture<>();
3016        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
3017        f.complete(one);
3018        checkCompletedNormally(g, two);
3019    }
3020
3021    /**
3022     * thenApplyAsync result completes exceptionally after exceptional
3023     * completion of source
3024     */
3025    public void testThenApplyAsync2() {
3026        CompletableFuture<Integer> f = new CompletableFuture<>();
3027        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
3028        f.completeExceptionally(new CFException());
3029        checkCompletedWithWrappedCFException(g);
3030    }
3031
3032    /**
3033     * thenApplyAsync result completes exceptionally if action does
3034     */
3035    public void testThenApplyAsync3() {
3036        CompletableFuture<Integer> f = new CompletableFuture<>();
3037        FailingFunction r = new FailingFunction();
3038        CompletableFuture<Integer> g = f.thenApplyAsync(r);
3039        f.complete(null);
3040        checkCompletedWithWrappedCFException(g);
3041    }
3042
3043    /**
3044     * thenApplyAsync result completes exceptionally if source cancelled
3045     */
3046    public void testThenApplyAsync4() {
3047        CompletableFuture<Integer> f = new CompletableFuture<>();
3048        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
3049        assertTrue(f.cancel(true));
3050        checkCompletedWithWrappedCancellationException(g);
3051    }
3052
3053    /**
3054     * thenAcceptAsync result completes normally after normal
3055     * completion of source
3056     */
3057    public void testThenAcceptAsync() {
3058        CompletableFuture<Integer> f = new CompletableFuture<>();
3059        IncAction r = new IncAction();
3060        CompletableFuture<Void> g = f.thenAcceptAsync(r);
3061        f.complete(one);
3062        checkCompletedNormally(g, null);
3063        assertEquals(r.value, (Integer) 2);
3064    }
3065
3066    /**
3067     * thenAcceptAsync result completes exceptionally after exceptional
3068     * completion of source
3069     */
3070    public void testThenAcceptAsync2() {
3071        CompletableFuture<Integer> f = new CompletableFuture<>();
3072        IncAction r = new IncAction();
3073        CompletableFuture<Void> g = f.thenAcceptAsync(r);
3074        f.completeExceptionally(new CFException());
3075        checkCompletedWithWrappedCFException(g);
3076    }
3077
3078    /**
3079     * thenAcceptAsync result completes exceptionally if action does
3080     */
3081    public void testThenAcceptAsync3() {
3082        CompletableFuture<Integer> f = new CompletableFuture<>();
3083        FailingConsumer r = new FailingConsumer();
3084        CompletableFuture<Void> g = f.thenAcceptAsync(r);
3085        f.complete(null);
3086        checkCompletedWithWrappedCFException(g);
3087    }
3088
3089    /**
3090     * thenAcceptAsync result completes exceptionally if source cancelled
3091     */
3092    public void testThenAcceptAsync4() {
3093        CompletableFuture<Integer> f = new CompletableFuture<>();
3094        IncAction r = new IncAction();
3095        CompletableFuture<Void> g = f.thenAcceptAsync(r);
3096        assertTrue(f.cancel(true));
3097        checkCompletedWithWrappedCancellationException(g);
3098    }
3099
3100    // async with explicit executors
3101
3102    /**
3103     * thenRunAsync result completes normally after normal completion of source
3104     */
3105    public void testThenRunAsyncE() {
3106        CompletableFuture<Integer> f = new CompletableFuture<>();
3107        Noop r = new Noop();
3108        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3109        f.complete(null);
3110        checkCompletedNormally(g, null);
3111
3112        // reordered version
3113        f = new CompletableFuture<>();
3114        f.complete(null);
3115        r = new Noop();
3116        g = f.thenRunAsync(r, new ThreadExecutor());
3117        checkCompletedNormally(g, null);
3118    }
3119
3120    /**
3121     * thenRunAsync result completes exceptionally after exceptional
3122     * completion of source
3123     */
3124    public void testThenRunAsync2E() {
3125        CompletableFuture<Integer> f = new CompletableFuture<>();
3126        Noop r = new Noop();
3127        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3128        f.completeExceptionally(new CFException());
3129        try {
3130            g.join();
3131            shouldThrow();
3132        } catch (CompletionException success) {}
3133        checkCompletedWithWrappedCFException(g);
3134    }
3135
3136    /**
3137     * thenRunAsync result completes exceptionally if action does
3138     */
3139    public void testThenRunAsync3E() {
3140        CompletableFuture<Integer> f = new CompletableFuture<>();
3141        FailingNoop r = new FailingNoop();
3142        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3143        f.complete(null);
3144        checkCompletedWithWrappedCFException(g);
3145    }
3146
3147    /**
3148     * thenRunAsync result completes exceptionally if source cancelled
3149     */
3150    public void testThenRunAsync4E() {
3151        CompletableFuture<Integer> f = new CompletableFuture<>();
3152        Noop r = new Noop();
3153        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3154        assertTrue(f.cancel(true));
3155        checkCompletedWithWrappedCancellationException(g);
3156    }
3157
3158    /**
3159     * thenApplyAsync result completes normally after normal completion of source
3160     */
3161    public void testThenApplyAsyncE() {
3162        CompletableFuture<Integer> f = new CompletableFuture<>();
3163        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
3164        f.complete(one);
3165        checkCompletedNormally(g, two);
3166    }
3167
3168    /**
3169     * thenApplyAsync result completes exceptionally after exceptional
3170     * completion of source
3171     */
3172    public void testThenApplyAsync2E() {
3173        CompletableFuture<Integer> f = new CompletableFuture<>();
3174        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
3175        f.completeExceptionally(new CFException());
3176        checkCompletedWithWrappedCFException(g);
3177    }
3178
3179    /**
3180     * thenApplyAsync result completes exceptionally if action does
3181     */
3182    public void testThenApplyAsync3E() {
3183        CompletableFuture<Integer> f = new CompletableFuture<>();
3184        FailingFunction r = new FailingFunction();
3185        CompletableFuture<Integer> g = f.thenApplyAsync(r, new ThreadExecutor());
3186        f.complete(null);
3187        checkCompletedWithWrappedCFException(g);
3188    }
3189
3190    /**
3191     * thenApplyAsync result completes exceptionally if source cancelled
3192     */
3193    public void testThenApplyAsync4E() {
3194        CompletableFuture<Integer> f = new CompletableFuture<>();
3195        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
3196        assertTrue(f.cancel(true));
3197        checkCompletedWithWrappedCancellationException(g);
3198    }
3199
3200    /**
3201     * thenAcceptAsync result completes normally after normal
3202     * completion of source
3203     */
3204    public void testThenAcceptAsyncE() {
3205        CompletableFuture<Integer> f = new CompletableFuture<>();
3206        IncAction r = new IncAction();
3207        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3208        f.complete(one);
3209        checkCompletedNormally(g, null);
3210        assertEquals(r.value, (Integer) 2);
3211    }
3212
3213    /**
3214     * thenAcceptAsync result completes exceptionally after exceptional
3215     * completion of source
3216     */
3217    public void testThenAcceptAsync2E() {
3218        CompletableFuture<Integer> f = new CompletableFuture<>();
3219        IncAction r = new IncAction();
3220        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3221        f.completeExceptionally(new CFException());
3222        checkCompletedWithWrappedCFException(g);
3223    }
3224
3225    /**
3226     * thenAcceptAsync result completes exceptionally if action does
3227     */
3228    public void testThenAcceptAsync3E() {
3229        CompletableFuture<Integer> f = new CompletableFuture<>();
3230        FailingConsumer r = new FailingConsumer();
3231        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3232        f.complete(null);
3233        checkCompletedWithWrappedCFException(g);
3234    }
3235
3236    /**
3237     * thenAcceptAsync result completes exceptionally if source cancelled
3238     */
3239    public void testThenAcceptAsync4E() {
3240        CompletableFuture<Integer> f = new CompletableFuture<>();
3241        IncAction r = new IncAction();
3242        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3243        assertTrue(f.cancel(true));
3244        checkCompletedWithWrappedCancellationException(g);
3245    }
3246
2561      // other static methods
2562  
2563      /**
# Line 3406 | 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),

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines