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.58 by jsr166, Mon Jun 2 23:10:08 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 332 | Line 334 | public class CompletableFutureTest exten
334          return (x == null) ? null : x + 1;
335      }
336  
337 <    static final Supplier<Integer> supplyOne =
338 <        () -> Integer.valueOf(1);
339 <    static final Function<Integer, Integer> inc =
340 <        (Integer x) -> Integer.valueOf(x.intValue() + 1);
341 <    static final BiFunction<Integer, Integer, Integer> subtract =
342 <        (Integer x, Integer y) -> subtract(x, y);
337 >    static final class IntegerSupplier implements Supplier<Integer> {
338 >        final ExecutionMode m;
339 >        int invocationCount = 0;
340 >        final Integer value;
341 >        IntegerSupplier(ExecutionMode m, Integer value) {
342 >            this.m = m;
343 >            this.value = value;
344 >        }
345 >        public Integer get() {
346 >            m.checkExecutionMode();
347 >            invocationCount++;
348 >            return value;
349 >        }
350 >    }
351 >        
352      static final class IncAction implements Consumer<Integer> {
353          int invocationCount = 0;
354          Integer value;
# Line 347 | Line 358 | public class CompletableFutureTest exten
358          }
359      }
360      static final class IncFunction implements Function<Integer,Integer> {
361 +        final ExecutionMode m;
362          int invocationCount = 0;
363          Integer value;
364 +        IncFunction(ExecutionMode m) { this.m = m; }
365          public Integer apply(Integer x) {
366 +            m.checkExecutionMode();
367              invocationCount++;
368              return value = inc(x);
369          }
370      }
371      static final class SubtractAction implements BiConsumer<Integer, Integer> {
372 +        final ExecutionMode m;
373          int invocationCount = 0;
374          Integer value;
375          // Check this action was invoked exactly once when result is computed.
376 +        SubtractAction(ExecutionMode m) { this.m = m; }
377          public void accept(Integer x, Integer y) {
378 +            m.checkExecutionMode();
379              invocationCount++;
380              value = subtract(x, y);
381          }
382      }
383      static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> {
384 +        final ExecutionMode m;
385          int invocationCount = 0;
386          Integer value;
387          // Check this action was invoked exactly once when result is computed.
388 +        SubtractFunction(ExecutionMode m) { this.m = m; }
389          public Integer apply(Integer x, Integer y) {
390 +            m.checkExecutionMode();
391              invocationCount++;
392              return value = subtract(x, y);
393          }
394      }
395      static final class Noop implements Runnable {
396 +        final ExecutionMode m;
397          int invocationCount = 0;
398 +        Noop(ExecutionMode m) { this.m = m; }
399          public void run() {
400 +            m.checkExecutionMode();
401              invocationCount++;
402          }
403      }
404  
405      static final class FailingSupplier implements Supplier<Integer> {
406 +        final ExecutionMode m;
407          int invocationCount = 0;
408 +        FailingSupplier(ExecutionMode m) { this.m = m; }
409          public Integer get() {
410 +            m.checkExecutionMode();
411              invocationCount++;
412              throw new CFException();
413          }
414      }
415      static final class FailingConsumer implements Consumer<Integer> {
416 +        final ExecutionMode m;
417          int invocationCount = 0;
418 +        FailingConsumer(ExecutionMode m) { this.m = m; }
419          public void accept(Integer x) {
420 +            m.checkExecutionMode();
421              invocationCount++;
422              throw new CFException();
423          }
424      }
425      static final class FailingBiConsumer implements BiConsumer<Integer, Integer> {
426 +        final ExecutionMode m;
427          int invocationCount = 0;
428 +        FailingBiConsumer(ExecutionMode m) { this.m = m; }
429          public void accept(Integer x, Integer y) {
430 +            m.checkExecutionMode();
431              invocationCount++;
432              throw new CFException();
433          }
434      }
435      static final class FailingFunction implements Function<Integer, Integer> {
436 +        final ExecutionMode m;
437          int invocationCount = 0;
438 +        FailingFunction(ExecutionMode m) { this.m = m; }
439          public Integer apply(Integer x) {
440 +            m.checkExecutionMode();
441              invocationCount++;
442              throw new CFException();
443          }
444      }
445      static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> {
446 +        final ExecutionMode m;
447          int invocationCount = 0;
448 +        FailingBiFunction(ExecutionMode m) { this.m = m; }
449          public Integer apply(Integer x, Integer y) {
450 +            m.checkExecutionMode();
451              invocationCount++;
452              throw new CFException();
453          }
454      }
455 <    static final class FailingNoop implements Runnable {
455 >    static final class FailingRunnable implements Runnable {
456 >        final ExecutionMode m;
457          int invocationCount = 0;
458 +        FailingRunnable(ExecutionMode m) { this.m = m; }
459          public void run() {
460 +            m.checkExecutionMode();
461              invocationCount++;
462              throw new CFException();
463          }
# Line 424 | Line 465 | public class CompletableFutureTest exten
465  
466      static final class CompletableFutureInc
467          implements Function<Integer, CompletableFuture<Integer>> {
468 +        final ExecutionMode m;
469          int invocationCount = 0;
470 +        CompletableFutureInc(ExecutionMode m) { this.m = m; }
471          public CompletableFuture<Integer> apply(Integer x) {
472 +            m.checkExecutionMode();
473              invocationCount++;
474              CompletableFuture<Integer> f = new CompletableFuture<>();
475              f.complete(inc(x));
# Line 435 | Line 479 | public class CompletableFutureTest exten
479  
480      static final class FailingCompletableFutureFunction
481          implements Function<Integer, CompletableFuture<Integer>> {
482 +        final ExecutionMode m;
483          int invocationCount = 0;
484 +        FailingCompletableFutureFunction(ExecutionMode m) { this.m = m; }
485          public CompletableFuture<Integer> apply(Integer x) {
486 +            m.checkExecutionMode();
487              invocationCount++;
488              throw new CFException();
489          }
# Line 444 | Line 491 | public class CompletableFutureTest exten
491  
492      // Used for explicit executor tests
493      static final class ThreadExecutor implements Executor {
494 <        AtomicInteger count = new AtomicInteger(0);
494 >        final AtomicInteger count = new AtomicInteger(0);
495 >        static final ThreadGroup tg = new ThreadGroup("ThreadExecutor");
496 >        static boolean startedCurrentThread() {
497 >            return Thread.currentThread().getThreadGroup() == tg;
498 >        }
499  
500          public void execute(Runnable r) {
501              count.getAndIncrement();
502 <            new Thread(r).start();
502 >            new Thread(tg, r).start();
503          }
504      }
505  
# Line 458 | Line 509 | public class CompletableFutureTest exten
509       */
510      enum ExecutionMode {
511          DEFAULT {
512 +            public void checkExecutionMode() {
513 +                assertNull(ForkJoinTask.getPool());
514 +            }
515 +            public CompletableFuture<Void> runAsync(Runnable a) {
516 +                throw new UnsupportedOperationException();
517 +            }
518 +            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
519 +                throw new UnsupportedOperationException();
520 +            }
521              public <T> CompletableFuture<Void> thenRun
522                  (CompletableFuture<T> f, Runnable a) {
523                  return f.thenRun(a);
# Line 521 | Line 581 | public class CompletableFutureTest exten
581              }
582          },
583  
584 <        DEFAULT_ASYNC {
584 >        ASYNC {
585 >            public void checkExecutionMode() {
586 >                assertSame(ForkJoinPool.commonPool(),
587 >                           ForkJoinTask.getPool());
588 >            }
589 >            public CompletableFuture<Void> runAsync(Runnable a) {
590 >                return CompletableFuture.runAsync(a);
591 >            }
592 >            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
593 >                return CompletableFuture.supplyAsync(a);
594 >            }
595              public <T> CompletableFuture<Void> thenRun
596                  (CompletableFuture<T> f, Runnable a) {
597                  return f.thenRunAsync(a);
# Line 586 | Line 656 | public class CompletableFutureTest exten
656          },
657  
658          EXECUTOR {
659 +            public void checkExecutionMode() {
660 +                assertTrue(ThreadExecutor.startedCurrentThread());
661 +            }
662 +            public CompletableFuture<Void> runAsync(Runnable a) {
663 +                return CompletableFuture.runAsync(a, new ThreadExecutor());
664 +            }
665 +            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
666 +                return CompletableFuture.supplyAsync(a, new ThreadExecutor());
667 +            }
668              public <T> CompletableFuture<Void> thenRun
669                  (CompletableFuture<T> f, Runnable a) {
670                  return f.thenRunAsync(a, new ThreadExecutor());
# Line 649 | Line 728 | public class CompletableFutureTest exten
728              }
729          };
730  
731 +        public abstract void checkExecutionMode();
732 +        public abstract CompletableFuture<Void> runAsync(Runnable a);
733 +        public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a);
734          public abstract <T> CompletableFuture<Void> thenRun
735              (CompletableFuture<T> f, Runnable a);
736          public abstract <T> CompletableFuture<Void> thenAccept
# Line 727 | Line 809 | public class CompletableFutureTest exten
809          if (!createIncomplete) f.completeExceptionally(ex);
810          final CompletableFuture<Integer> g = f.exceptionally
811              ((Throwable t) -> {
812 +                ExecutionMode.DEFAULT.checkExecutionMode();
813                  threadAssertSame(t, ex);
814                  a.getAndIncrement();
815                  return v1;
# Line 748 | Line 831 | public class CompletableFutureTest exten
831          if (!createIncomplete) f.completeExceptionally(ex1);
832          final CompletableFuture<Integer> g = f.exceptionally
833              ((Throwable t) -> {
834 +                ExecutionMode.DEFAULT.checkExecutionMode();
835                  threadAssertSame(t, ex1);
836                  a.getAndIncrement();
837                  throw ex2;
# Line 773 | Line 857 | public class CompletableFutureTest exten
857          final CompletableFuture<Integer> g = m.handle
858              (f,
859               (Integer x, Throwable t) -> {
860 +                m.checkExecutionMode();
861                  threadAssertSame(x, v1);
862                  threadAssertNull(t);
863                  a.getAndIncrement();
# Line 801 | Line 886 | public class CompletableFutureTest exten
886          final CompletableFuture<Integer> g = m.handle
887              (f,
888               (Integer x, Throwable t) -> {
889 +                m.checkExecutionMode();
890                  threadAssertNull(x);
891                  threadAssertSame(t, ex);
892                  a.getAndIncrement();
# Line 829 | Line 915 | public class CompletableFutureTest exten
915          final CompletableFuture<Integer> g = m.handle
916              (f,
917               (Integer x, Throwable t) -> {
918 +                m.checkExecutionMode();
919                  threadAssertNull(x);
920                  threadAssertTrue(t instanceof CancellationException);
921                  a.getAndIncrement();
# Line 856 | Line 943 | public class CompletableFutureTest exten
943          final CompletableFuture<Integer> g = m.handle
944              (f,
945               (Integer x, Throwable t) -> {
946 +                m.checkExecutionMode();
947                  threadAssertNull(x);
948                  threadAssertSame(ex1, t);
949                  a.getAndIncrement();
# Line 880 | Line 968 | public class CompletableFutureTest exten
968          final CompletableFuture<Integer> g = m.handle
969              (f,
970               (Integer x, Throwable t) -> {
971 +                m.checkExecutionMode();
972                  threadAssertSame(x, v1);
973                  threadAssertNull(t);
974                  a.getAndIncrement();
# Line 895 | Line 984 | public class CompletableFutureTest exten
984      /**
985       * runAsync completes after running Runnable
986       */
987 <    public void testRunAsync() {
988 <        Noop r = new Noop();
989 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
987 >    public void testRunAsync_normalCompletion() {
988 >        ExecutionMode[] executionModes = {
989 >            ExecutionMode.ASYNC,
990 >            ExecutionMode.EXECUTOR,
991 >        };
992 >        for (ExecutionMode m : executionModes)
993 >    {
994 >        final Noop r = new Noop(m);
995 >        final CompletableFuture<Void> f = m.runAsync(r);
996          assertNull(f.join());
902        assertEquals(1, r.invocationCount);
997          checkCompletedNormally(f, null);
904    }
905
906    /**
907     * runAsync with executor completes after running Runnable
908     */
909    public void testRunAsync2() {
910        Noop r = new Noop();
911        ThreadExecutor exec = new ThreadExecutor();
912        CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec);
913        assertNull(f.join());
998          assertEquals(1, r.invocationCount);
999 <        checkCompletedNormally(f, null);
916 <        assertEquals(1, exec.count.get());
917 <    }
999 >    }}
1000  
1001      /**
1002       * failing runAsync completes exceptionally after running Runnable
1003       */
1004 <    public void testRunAsync3() {
1005 <        FailingNoop r = new FailingNoop();
1006 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
1004 >    public void testRunAsync_exceptionalCompletion() {
1005 >        ExecutionMode[] executionModes = {
1006 >            ExecutionMode.ASYNC,
1007 >            ExecutionMode.EXECUTOR,
1008 >        };
1009 >        for (ExecutionMode m : executionModes)
1010 >    {
1011 >        final FailingRunnable r = new FailingRunnable(m);
1012 >        final CompletableFuture<Void> f = m.runAsync(r);
1013          checkCompletedWithWrappedCFException(f);
1014          assertEquals(1, r.invocationCount);
1015 <    }
1015 >    }}
1016  
1017      /**
1018       * supplyAsync completes with result of supplier
1019       */
1020 <    public void testSupplyAsync() {
1021 <        CompletableFuture<Integer> f;
1022 <        f = CompletableFuture.supplyAsync(supplyOne);
1023 <        assertEquals(f.join(), one);
1024 <        checkCompletedNormally(f, one);
1025 <    }
1026 <
1027 <    /**
1028 <     * supplyAsync with executor completes with result of supplier
1029 <     */
1030 <    public void testSupplyAsync2() {
1031 <        CompletableFuture<Integer> f;
1032 <        f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor());
1033 <        assertEquals(f.join(), one);
946 <        checkCompletedNormally(f, one);
947 <    }
1020 >    public void testSupplyAsync_normalCompletion() {
1021 >        ExecutionMode[] executionModes = {
1022 >            ExecutionMode.ASYNC,
1023 >            ExecutionMode.EXECUTOR,
1024 >        };
1025 >        for (ExecutionMode m : executionModes)
1026 >        for (Integer v1 : new Integer[] { 1, null })
1027 >    {
1028 >        final IntegerSupplier r = new IntegerSupplier(m, v1);
1029 >        final CompletableFuture<Integer> f = m.supplyAsync(r);
1030 >        assertSame(v1, f.join());
1031 >        checkCompletedNormally(f, v1);
1032 >        assertEquals(1, r.invocationCount);
1033 >    }}
1034  
1035      /**
1036       * Failing supplyAsync completes exceptionally
1037       */
1038 <    public void testSupplyAsync3() {
1039 <        FailingSupplier r = new FailingSupplier();
1040 <        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r);
1038 >    public void testSupplyAsync_exceptionalCompletion() {
1039 >        ExecutionMode[] executionModes = {
1040 >            ExecutionMode.ASYNC,
1041 >            ExecutionMode.EXECUTOR,
1042 >        };
1043 >        for (ExecutionMode m : executionModes)
1044 >    {
1045 >        FailingSupplier r = new FailingSupplier(m);
1046 >        CompletableFuture<Integer> f = m.supplyAsync(r);
1047          checkCompletedWithWrappedCFException(f);
1048          assertEquals(1, r.invocationCount);
1049 <    }
1049 >    }}
1050  
1051      // seq completion methods
1052  
1053      /**
1054       * thenRun result completes normally after normal completion of source
1055       */
1056 <    public void testThenRun() {
1057 <        CompletableFuture<Integer> f;
1058 <        CompletableFuture<Void> g;
1059 <        Noop r;
1060 <
1061 <        f = new CompletableFuture<>();
1062 <        g = f.thenRun(r = new Noop());
1063 <        f.complete(null);
1064 <        checkCompletedNormally(g, null);
1065 <        assertEquals(1, r.invocationCount);
1056 >    public void testThenRun_normalCompletion() {
1057 >        for (ExecutionMode m : ExecutionMode.values())
1058 >        for (boolean createIncomplete : new boolean[] { true, false })
1059 >        for (Integer v1 : new Integer[] { 1, null })
1060 >    {
1061 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1062 >        final Noop r = new Noop(m);
1063 >        if (!createIncomplete) f.complete(v1);
1064 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1065 >        if (createIncomplete) {
1066 >            checkIncomplete(g);
1067 >            f.complete(v1);
1068 >        }
1069  
975        f = new CompletableFuture<>();
976        f.complete(null);
977        g = f.thenRun(r = new Noop());
1070          checkCompletedNormally(g, null);
1071 +        checkCompletedNormally(f, v1);
1072          assertEquals(1, r.invocationCount);
1073 <    }
1073 >    }}
1074  
1075      /**
1076       * thenRun result completes exceptionally after exceptional
1077       * completion of source
1078       */
1079 <    public void testThenRun2() {
1080 <        CompletableFuture<Integer> f;
1081 <        CompletableFuture<Void> g;
1082 <        Noop r;
1083 <
1084 <        f = new CompletableFuture<>();
1085 <        g = f.thenRun(r = new Noop());
1086 <        f.completeExceptionally(new CFException());
1087 <        checkCompletedWithWrappedCFException(g);
1088 <        assertEquals(0, r.invocationCount);
1079 >    public void testThenRun_exceptionalCompletion() {
1080 >        for (ExecutionMode m : ExecutionMode.values())
1081 >        for (boolean createIncomplete : new boolean[] { true, false })
1082 >    {
1083 >        final CFException ex = new CFException();
1084 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1085 >        final Noop r = new Noop(m);
1086 >        if (!createIncomplete) f.completeExceptionally(ex);
1087 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1088 >        if (createIncomplete) {
1089 >            checkIncomplete(g);
1090 >            f.completeExceptionally(ex);
1091 >        }
1092  
1093 <        f = new CompletableFuture<>();
1094 <        f.completeExceptionally(new CFException());
999 <        g = f.thenRun(r = new Noop());
1000 <        checkCompletedWithWrappedCFException(g);
1093 >        checkCompletedWithWrappedCFException(g, ex);
1094 >        checkCompletedWithWrappedCFException(f, ex);
1095          assertEquals(0, r.invocationCount);
1096 <    }
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 <    }
1096 >    }}
1097  
1098      /**
1099       * thenRun result completes exceptionally if source cancelled
1100       */
1101 <    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 })
1101 >    public void testThenRun_sourceCancelled() {
1102          for (ExecutionMode m : ExecutionMode.values())
1103 <        for (Integer v1 : new Integer[] { 1, null })
1104 <        for (Integer v2 : new Integer[] { 2, null })
1103 >        for (boolean createIncomplete : new boolean[] { true, false })
1104 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1105      {
1106          final CompletableFuture<Integer> f = new CompletableFuture<>();
1107 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1108 <        final SubtractFunction r = new SubtractFunction();
1109 <        CompletableFuture<Integer> h = null;
1110 <        if (createIncomplete) h = m.thenCombine(f, g, r);
1107 >        final Noop r = new Noop(m);
1108 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1109 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1110 >        if (createIncomplete) {
1111 >            checkIncomplete(g);
1112 >            assertTrue(f.cancel(mayInterruptIfRunning));
1113 >        }
1114  
1115 <        if (fFirst)
1116 <            f.complete(v1);
1149 <        else
1150 <            g.complete(v2);
1151 <        if (createIncomplete) checkIncomplete(h);
1115 >        checkCompletedWithWrappedCancellationException(g);
1116 >        checkCancelled(f);
1117          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);
1118      }}
1119  
1120      /**
1121 <     * thenCombine result completes exceptionally after exceptional
1167 <     * completion of either source
1121 >     * thenRun result completes exceptionally if action does
1122       */
1123 <    public void testThenCombine_exceptionalCompletion1() {
1123 >    public void testThenRun_actionFailed() {
1124          for (ExecutionMode m : ExecutionMode.values())
1125 +        for (boolean createIncomplete : new boolean[] { true, false })
1126          for (Integer v1 : new Integer[] { 1, null })
1127      {
1128          final CompletableFuture<Integer> f = new CompletableFuture<>();
1129 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1130 <        final SubtractFunction r = new SubtractFunction();
1131 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1132 <        final CFException ex = new CFException();
1133 <
1134 <        f.completeExceptionally(ex);
1135 <        checkIncomplete(h);
1181 <        g.complete(v1);
1129 >        final FailingRunnable r = new FailingRunnable(m);
1130 >        if (!createIncomplete) f.complete(v1);
1131 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1132 >        if (createIncomplete) {
1133 >            checkIncomplete(g);
1134 >            f.complete(v1);
1135 >        }
1136  
1137 <        checkCompletedWithWrappedCFException(h, ex);
1138 <        checkCompletedWithWrappedCFException(f, ex);
1185 <        assertEquals(0, r.invocationCount);
1186 <        checkCompletedNormally(g, v1);
1137 >        checkCompletedWithWrappedCFException(g);
1138 >        checkCompletedNormally(f, v1);
1139      }}
1140  
1141 <    public void testThenCombine_exceptionalCompletion2() {
1141 >    /**
1142 >     * thenApply result completes normally after normal completion of source
1143 >     */
1144 >    public void testThenApply_normalCompletion() {
1145          for (ExecutionMode m : ExecutionMode.values())
1146 +        for (boolean createIncomplete : new boolean[] { true, false })
1147          for (Integer v1 : new Integer[] { 1, null })
1148      {
1149          final CompletableFuture<Integer> f = new CompletableFuture<>();
1150 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1151 <        final SubtractFunction r = new SubtractFunction();
1152 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1153 <        final CFException ex = new CFException();
1154 <
1155 <        g.completeExceptionally(ex);
1156 <        checkIncomplete(h);
1201 <        f.complete(v1);
1150 >        final IncFunction r = new IncFunction(m);
1151 >        if (!createIncomplete) f.complete(v1);
1152 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1153 >        if (createIncomplete) {
1154 >            checkIncomplete(g);
1155 >            f.complete(v1);
1156 >        }
1157  
1158 <        checkCompletedWithWrappedCFException(h, ex);
1204 <        checkCompletedWithWrappedCFException(g, ex);
1205 <        assertEquals(0, r.invocationCount);
1158 >        checkCompletedNormally(g, inc(v1));
1159          checkCompletedNormally(f, v1);
1160 +        assertEquals(1, r.invocationCount);
1161      }}
1162  
1163 <    public void testThenCombine_exceptionalCompletion3() {
1163 >    /**
1164 >     * thenApply result completes exceptionally after exceptional
1165 >     * completion of source
1166 >     */
1167 >    public void testThenApply_exceptionalCompletion() {
1168          for (ExecutionMode m : ExecutionMode.values())
1169 <        for (Integer v1 : new Integer[] { 1, null })
1169 >        for (boolean createIncomplete : new boolean[] { true, false })
1170      {
1213        final CompletableFuture<Integer> f = new CompletableFuture<>();
1214        final CompletableFuture<Integer> g = new CompletableFuture<>();
1215        final SubtractFunction r = new SubtractFunction();
1171          final CFException ex = new CFException();
1172 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1173 +        final IncFunction r = new IncFunction(m);
1174 +        if (!createIncomplete) f.completeExceptionally(ex);
1175 +        final CompletableFuture<Integer> g = m.thenApply(f, r);
1176 +        if (createIncomplete) {
1177 +            checkIncomplete(g);
1178 +            f.completeExceptionally(ex);
1179 +        }
1180  
1218        g.completeExceptionally(ex);
1219        f.complete(v1);
1220        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1221
1222        checkCompletedWithWrappedCFException(h, ex);
1181          checkCompletedWithWrappedCFException(g, ex);
1182 +        checkCompletedWithWrappedCFException(f, ex);
1183          assertEquals(0, r.invocationCount);
1225        checkCompletedNormally(f, v1);
1184      }}
1185  
1186 <    public void testThenCombine_exceptionalCompletion4() {
1186 >    /**
1187 >     * thenApply result completes exceptionally if source cancelled
1188 >     */
1189 >    public void testThenApply_sourceCancelled() {
1190          for (ExecutionMode m : ExecutionMode.values())
1191 <        for (Integer v1 : new Integer[] { 1, null })
1191 >        for (boolean createIncomplete : new boolean[] { true, false })
1192 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1193      {
1194          final CompletableFuture<Integer> f = new CompletableFuture<>();
1195 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1196 <        final SubtractFunction r = new SubtractFunction();
1197 <        final CFException ex = new CFException();
1198 <
1199 <        f.completeExceptionally(ex);
1200 <        g.complete(v1);
1201 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1195 >        final IncFunction r = new IncFunction(m);
1196 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1197 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1198 >        if (createIncomplete) {
1199 >            checkIncomplete(g);
1200 >            assertTrue(f.cancel(mayInterruptIfRunning));
1201 >        }
1202  
1203 <        checkCompletedWithWrappedCFException(h, ex);
1204 <        checkCompletedWithWrappedCFException(f, ex);
1203 >        checkCompletedWithWrappedCancellationException(g);
1204 >        checkCancelled(f);
1205          assertEquals(0, r.invocationCount);
1244        checkCompletedNormally(g, v1);
1206      }}
1207  
1208      /**
1209 <     * thenCombine result completes exceptionally if action does
1209 >     * thenApply result completes exceptionally if action does
1210       */
1211 <    public void testThenCombine_actionFailed1() {
1211 >    public void testThenApply_actionFailed() {
1212          for (ExecutionMode m : ExecutionMode.values())
1213 +        for (boolean createIncomplete : new boolean[] { true, false })
1214          for (Integer v1 : new Integer[] { 1, null })
1253        for (Integer v2 : new Integer[] { 2, null })
1215      {
1216          final CompletableFuture<Integer> f = new CompletableFuture<>();
1217 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1218 <        final FailingBiFunction r = new FailingBiFunction();
1219 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1220 <
1221 <        f.complete(v1);
1222 <        checkIncomplete(h);
1223 <        g.complete(v2);
1217 >        final FailingFunction r = new FailingFunction(m);
1218 >        if (!createIncomplete) f.complete(v1);
1219 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1220 >        if (createIncomplete) {
1221 >            checkIncomplete(g);
1222 >            f.complete(v1);
1223 >        }
1224  
1225 <        checkCompletedWithWrappedCFException(h);
1225 >        checkCompletedWithWrappedCFException(g);
1226          checkCompletedNormally(f, v1);
1266        checkCompletedNormally(g, v2);
1227      }}
1228  
1229 <    public void testThenCombine_actionFailed2() {
1229 >    /**
1230 >     * thenAccept result completes normally after normal completion of source
1231 >     */
1232 >    public void testThenAccept_normalCompletion() {
1233          for (ExecutionMode m : ExecutionMode.values())
1234 +        for (boolean createIncomplete : new boolean[] { true, false })
1235          for (Integer v1 : new Integer[] { 1, null })
1272        for (Integer v2 : new Integer[] { 2, null })
1236      {
1237          final CompletableFuture<Integer> f = new CompletableFuture<>();
1238 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1239 <        final FailingBiFunction r = new FailingBiFunction();
1240 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1241 <
1242 <        g.complete(v2);
1243 <        checkIncomplete(h);
1244 <        f.complete(v1);
1238 >        final IncAction r = new IncAction();
1239 >        if (!createIncomplete) f.complete(v1);
1240 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1241 >        if (createIncomplete) {
1242 >            checkIncomplete(g);
1243 >            f.complete(v1);
1244 >        }
1245  
1246 <        checkCompletedWithWrappedCFException(h);
1246 >        checkCompletedNormally(g, null);
1247          checkCompletedNormally(f, v1);
1248 <        checkCompletedNormally(g, v2);
1248 >        assertEquals(1, r.invocationCount);
1249 >        assertEquals(inc(v1), r.value);
1250      }}
1251  
1252      /**
1253 <     * thenCombine result completes exceptionally if either source cancelled
1253 >     * thenAccept result completes exceptionally after exceptional
1254 >     * completion of source
1255       */
1256 <    public void testThenCombine_sourceCancelled1() {
1256 >    public void testThenAccept_exceptionalCompletion() {
1257          for (ExecutionMode m : ExecutionMode.values())
1258 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1294 <        for (Integer v1 : new Integer[] { 1, null })
1258 >        for (boolean createIncomplete : new boolean[] { true, false })
1259      {
1260 +        final CFException ex = new CFException();
1261          final CompletableFuture<Integer> f = new CompletableFuture<>();
1262 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1263 <        final SubtractFunction r = new SubtractFunction();
1264 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1265 <
1266 <        assertTrue(f.cancel(mayInterruptIfRunning));
1267 <        checkIncomplete(h);
1268 <        g.complete(v1);
1262 >        final IncAction r = new IncAction();
1263 >        if (!createIncomplete) f.completeExceptionally(ex);
1264 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1265 >        if (createIncomplete) {
1266 >            checkIncomplete(g);
1267 >            f.completeExceptionally(ex);
1268 >        }
1269  
1270 <        checkCompletedWithWrappedCancellationException(h);
1271 <        checkCancelled(f);
1270 >        checkCompletedWithWrappedCFException(g, ex);
1271 >        checkCompletedWithWrappedCFException(f, ex);
1272          assertEquals(0, r.invocationCount);
1308        checkCompletedNormally(g, v1);
1273      }}
1274  
1275 <    public void testThenCombine_sourceCancelled2() {
1275 >    /**
1276 >     * thenAccept result completes exceptionally if action does
1277 >     */
1278 >    public void testThenAccept_actionFailed() {
1279          for (ExecutionMode m : ExecutionMode.values())
1280 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1280 >        for (boolean createIncomplete : new boolean[] { true, false })
1281          for (Integer v1 : new Integer[] { 1, null })
1282      {
1283          final CompletableFuture<Integer> f = new CompletableFuture<>();
1284 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1285 <        final SubtractFunction r = new SubtractFunction();
1286 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1287 <
1288 <        assertTrue(g.cancel(mayInterruptIfRunning));
1289 <        checkIncomplete(h);
1290 <        f.complete(v1);
1284 >        final FailingConsumer r = new FailingConsumer(m);
1285 >        if (!createIncomplete) f.complete(v1);
1286 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1287 >        if (createIncomplete) {
1288 >            checkIncomplete(g);
1289 >            f.complete(v1);
1290 >        }
1291  
1292 <        checkCompletedWithWrappedCancellationException(h);
1326 <        checkCancelled(g);
1327 <        assertEquals(0, r.invocationCount);
1292 >        checkCompletedWithWrappedCFException(g);
1293          checkCompletedNormally(f, v1);
1294      }}
1295  
1296 <    public void testThenCombine_sourceCancelled3() {
1296 >    /**
1297 >     * thenAccept result completes exceptionally if source cancelled
1298 >     */
1299 >    public void testThenAccept_sourceCancelled() {
1300          for (ExecutionMode m : ExecutionMode.values())
1301 +        for (boolean createIncomplete : new boolean[] { true, false })
1302          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1334        for (Integer v1 : new Integer[] { 1, null })
1303      {
1304          final CompletableFuture<Integer> f = new CompletableFuture<>();
1305 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1306 <        final SubtractFunction r = new SubtractFunction();
1307 <
1308 <        assertTrue(g.cancel(mayInterruptIfRunning));
1309 <        f.complete(v1);
1310 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1305 >        final IncAction r = new IncAction();
1306 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1307 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1308 >        if (createIncomplete) {
1309 >            checkIncomplete(g);
1310 >            assertTrue(f.cancel(mayInterruptIfRunning));
1311 >        }
1312  
1313 <        checkCompletedWithWrappedCancellationException(h);
1314 <        checkCancelled(g);
1313 >        checkCompletedWithWrappedCancellationException(g);
1314 >        checkCancelled(f);
1315          assertEquals(0, r.invocationCount);
1347        checkCompletedNormally(f, v1);
1316      }}
1317  
1318 <    public void testThenCombine_sourceCancelled4() {
1318 >    /**
1319 >     * thenCombine result completes normally after normal completion
1320 >     * of sources
1321 >     */
1322 >    public void testThenCombine_normalCompletion() {
1323          for (ExecutionMode m : ExecutionMode.values())
1324 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1324 >        for (boolean createIncomplete : new boolean[] { true, false })
1325 >        for (boolean fFirst : new boolean[] { true, false })
1326          for (Integer v1 : new Integer[] { 1, null })
1327 +        for (Integer v2 : new Integer[] { 2, null })
1328      {
1329          final CompletableFuture<Integer> f = new CompletableFuture<>();
1330          final CompletableFuture<Integer> g = new CompletableFuture<>();
1331 <        final SubtractFunction r = new SubtractFunction();
1331 >        final SubtractFunction r = new SubtractFunction(m);
1332  
1333 <        assertTrue(f.cancel(mayInterruptIfRunning));
1334 <        g.complete(v1);
1333 >        if (fFirst) f.complete(v1); else g.complete(v2);
1334 >        if (!createIncomplete)
1335 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1336          final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1337 +        if (createIncomplete) {
1338 +            checkIncomplete(h);
1339 +            assertEquals(0, r.invocationCount);
1340 +            if (!fFirst) f.complete(v1); else g.complete(v2);
1341 +        }
1342  
1343 <        checkCompletedWithWrappedCancellationException(h);
1344 <        checkCancelled(f);
1345 <        assertEquals(0, r.invocationCount);
1346 <        checkCompletedNormally(g, v1);
1343 >        checkCompletedNormally(h, subtract(v1, v2));
1344 >        checkCompletedNormally(f, v1);
1345 >        checkCompletedNormally(g, v2);
1346 >        assertEquals(1, r.invocationCount);
1347      }}
1348  
1349      /**
1350 <     * thenAcceptBoth result completes normally after normal
1351 <     * completion of sources
1350 >     * thenCombine result completes exceptionally after exceptional
1351 >     * completion of either source
1352       */
1353 <    public void testThenAcceptBoth_normalCompletion1() {
1353 >    public void testThenCombine_exceptionalCompletion() {
1354          for (ExecutionMode m : ExecutionMode.values())
1355 +        for (boolean createIncomplete : new boolean[] { true, false })
1356 +        for (boolean fFirst : new boolean[] { true, false })
1357          for (Integer v1 : new Integer[] { 1, null })
1376        for (Integer v2 : new Integer[] { 2, null })
1358      {
1359          final CompletableFuture<Integer> f = new CompletableFuture<>();
1360          final CompletableFuture<Integer> g = new CompletableFuture<>();
1361 <        final SubtractAction r = new SubtractAction();
1362 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1361 >        final CFException ex = new CFException();
1362 >        final SubtractFunction r = new SubtractFunction(m);
1363  
1364 <        f.complete(v1);
1365 <        checkIncomplete(h);
1366 <        assertEquals(0, r.invocationCount);
1367 <        g.complete(v2);
1364 >        (fFirst ? f : g).complete(v1);
1365 >        if (!createIncomplete)
1366 >            (!fFirst ? f : g).completeExceptionally(ex);
1367 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1368 >        if (createIncomplete) {
1369 >            checkIncomplete(h);
1370 >            (!fFirst ? f : g).completeExceptionally(ex);
1371 >        }
1372  
1373 <        checkCompletedNormally(h, null);
1374 <        assertEquals(subtract(v1, v2), r.value);
1375 <        checkCompletedNormally(f, v1);
1376 <        checkCompletedNormally(g, v2);
1373 >        checkCompletedWithWrappedCFException(h, ex);
1374 >        assertEquals(0, r.invocationCount);
1375 >        checkCompletedNormally(fFirst ? f : g, v1);
1376 >        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1377      }}
1378  
1379 <    public void testThenAcceptBoth_normalCompletion2() {
1379 >    /**
1380 >     * thenCombine result completes exceptionally if action does
1381 >     */
1382 >    public void testThenCombine_actionFailed() {
1383          for (ExecutionMode m : ExecutionMode.values())
1384 +        for (boolean fFirst : new boolean[] { true, false })
1385          for (Integer v1 : new Integer[] { 1, null })
1386          for (Integer v2 : new Integer[] { 2, null })
1387      {
1388          final CompletableFuture<Integer> f = new CompletableFuture<>();
1389          final CompletableFuture<Integer> g = new CompletableFuture<>();
1390 <        final SubtractAction r = new SubtractAction();
1391 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1390 >        final FailingBiFunction r = new FailingBiFunction(m);
1391 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1392  
1393 <        g.complete(v2);
1394 <        checkIncomplete(h);
1395 <        assertEquals(0, r.invocationCount);
1396 <        f.complete(v1);
1393 >        if (fFirst) {
1394 >            f.complete(v1);
1395 >            g.complete(v2);
1396 >        } else {
1397 >            g.complete(v2);
1398 >            f.complete(v1);
1399 >        }
1400  
1401 <        checkCompletedNormally(h, null);
1410 <        assertEquals(subtract(v1, v2), r.value);
1401 >        checkCompletedWithWrappedCFException(h);
1402          checkCompletedNormally(f, v1);
1403          checkCompletedNormally(g, v2);
1404      }}
1405  
1406 <    public void testThenAcceptBoth_normalCompletion3() {
1406 >    /**
1407 >     * thenCombine result completes exceptionally if either source cancelled
1408 >     */
1409 >    public void testThenCombine_sourceCancelled() {
1410          for (ExecutionMode m : ExecutionMode.values())
1411 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1412 +        for (boolean createIncomplete : new boolean[] { true, false })
1413 +        for (boolean fFirst : new boolean[] { true, false })
1414          for (Integer v1 : new Integer[] { 1, null })
1418        for (Integer v2 : new Integer[] { 2, null })
1415      {
1416          final CompletableFuture<Integer> f = new CompletableFuture<>();
1417          final CompletableFuture<Integer> g = new CompletableFuture<>();
1418 <        final SubtractAction r = new SubtractAction();
1418 >        final SubtractFunction r = new SubtractFunction(m);
1419  
1420 <        g.complete(v2);
1421 <        f.complete(v1);
1422 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1420 >        (fFirst ? f : g).complete(v1);
1421 >        if (!createIncomplete)
1422 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1423 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1424 >        if (createIncomplete) {
1425 >            checkIncomplete(h);
1426 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1427 >        }
1428  
1429 <        checkCompletedNormally(h, null);
1430 <        assertEquals(subtract(v1, v2), r.value);
1431 <        checkCompletedNormally(f, v1);
1432 <        checkCompletedNormally(g, v2);
1429 >        checkCompletedWithWrappedCancellationException(h);
1430 >        checkCancelled(!fFirst ? f : g);
1431 >        assertEquals(0, r.invocationCount);
1432 >        checkCompletedNormally(fFirst ? f : g, v1);
1433      }}
1434  
1435 <    public void testThenAcceptBoth_normalCompletion4() {
1435 >    /**
1436 >     * thenAcceptBoth result completes normally after normal
1437 >     * completion of sources
1438 >     */
1439 >    public void testThenAcceptBoth_normalCompletion() {
1440          for (ExecutionMode m : ExecutionMode.values())
1441 +        for (boolean createIncomplete : new boolean[] { true, false })
1442 +        for (boolean fFirst : new boolean[] { true, false })
1443          for (Integer v1 : new Integer[] { 1, null })
1444          for (Integer v2 : new Integer[] { 2, null })
1445      {
1446          final CompletableFuture<Integer> f = new CompletableFuture<>();
1447          final CompletableFuture<Integer> g = new CompletableFuture<>();
1448 <        final SubtractAction r = new SubtractAction();
1448 >        final SubtractAction r = new SubtractAction(m);
1449  
1450 <        f.complete(v1);
1451 <        g.complete(v2);
1450 >        if (fFirst) f.complete(v1); else g.complete(v2);
1451 >        if (!createIncomplete)
1452 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1453          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1454 +        if (createIncomplete) {
1455 +            checkIncomplete(h);
1456 +            assertEquals(0, r.invocationCount);
1457 +            if (!fFirst) f.complete(v1); else g.complete(v2);
1458 +        }
1459  
1460          checkCompletedNormally(h, null);
1461          assertEquals(subtract(v1, v2), r.value);
# Line 1454 | Line 1467 | public class CompletableFutureTest exten
1467       * thenAcceptBoth result completes exceptionally after exceptional
1468       * completion of either source
1469       */
1470 <    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() {
1498 <        for (ExecutionMode m : ExecutionMode.values())
1499 <        for (Integer v1 : new Integer[] { 1, null })
1500 <    {
1501 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1502 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1503 <        final SubtractAction r = new SubtractAction();
1504 <        final CFException ex = new CFException();
1505 <
1506 <        g.completeExceptionally(ex);
1507 <        f.complete(v1);
1508 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1509 <
1510 <        checkCompletedWithWrappedCFException(h, ex);
1511 <        checkCompletedWithWrappedCFException(g, ex);
1512 <        assertEquals(0, r.invocationCount);
1513 <        checkCompletedNormally(f, v1);
1514 <    }}
1515 <
1516 <    public void testThenAcceptBoth_exceptionalCompletion4() {
1470 >    public void testThenAcceptBoth_exceptionalCompletion() {
1471          for (ExecutionMode m : ExecutionMode.values())
1472 +        for (boolean createIncomplete : new boolean[] { true, false })
1473 +        for (boolean fFirst : new boolean[] { true, false })
1474          for (Integer v1 : new Integer[] { 1, null })
1475      {
1476          final CompletableFuture<Integer> f = new CompletableFuture<>();
1477          final CompletableFuture<Integer> g = new CompletableFuture<>();
1522        final SubtractAction r = new SubtractAction();
1478          final CFException ex = new CFException();
1479 +        final SubtractAction r = new SubtractAction(m);
1480  
1481 <        f.completeExceptionally(ex);
1482 <        g.complete(v1);
1481 >        (fFirst ? f : g).complete(v1);
1482 >        if (!createIncomplete)
1483 >            (!fFirst ? f : g).completeExceptionally(ex);
1484          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1485 +        if (createIncomplete) {
1486 +            checkIncomplete(h);
1487 +            (!fFirst ? f : g).completeExceptionally(ex);
1488 +        }
1489  
1490          checkCompletedWithWrappedCFException(h, ex);
1530        checkCompletedWithWrappedCFException(f, ex);
1491          assertEquals(0, r.invocationCount);
1492 <        checkCompletedNormally(g, v1);
1492 >        checkCompletedNormally(fFirst ? f : g, v1);
1493 >        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1494      }}
1495  
1496      /**
1497       * thenAcceptBoth result completes exceptionally if action does
1498       */
1499 <    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() {
1499 >    public void testThenAcceptBoth_actionFailed() {
1500          for (ExecutionMode m : ExecutionMode.values())
1501 +        for (boolean fFirst : new boolean[] { true, false })
1502          for (Integer v1 : new Integer[] { 1, null })
1503          for (Integer v2 : new Integer[] { 2, null })
1504      {
1505          final CompletableFuture<Integer> f = new CompletableFuture<>();
1506          final CompletableFuture<Integer> g = new CompletableFuture<>();
1507 <        final FailingBiConsumer r = new FailingBiConsumer();
1507 >        final FailingBiConsumer r = new FailingBiConsumer(m);
1508          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1509  
1510 <        g.complete(v2);
1511 <        checkIncomplete(h);
1512 <        f.complete(v1);
1510 >        if (fFirst) {
1511 >            f.complete(v1);
1512 >            g.complete(v2);
1513 >        } else {
1514 >            g.complete(v2);
1515 >            f.complete(v1);
1516 >        }
1517  
1518          checkCompletedWithWrappedCFException(h);
1519          checkCompletedNormally(f, v1);
# Line 1576 | Line 1523 | public class CompletableFutureTest exten
1523      /**
1524       * thenAcceptBoth result completes exceptionally if either source cancelled
1525       */
1526 <    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() {
1526 >    public void testThenAcceptBoth_sourceCancelled() {
1527          for (ExecutionMode m : ExecutionMode.values())
1528          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1529 +        for (boolean createIncomplete : new boolean[] { true, false })
1530 +        for (boolean fFirst : new boolean[] { true, false })
1531          for (Integer v1 : new Integer[] { 1, null })
1532      {
1533          final CompletableFuture<Integer> f = new CompletableFuture<>();
1534          final CompletableFuture<Integer> g = new CompletableFuture<>();
1535 <        final SubtractAction r = new SubtractAction();
1535 >        final SubtractAction r = new SubtractAction(m);
1536  
1537 <        assertTrue(f.cancel(mayInterruptIfRunning));
1538 <        g.complete(v1);
1537 >        (fFirst ? f : g).complete(v1);
1538 >        if (!createIncomplete)
1539 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1540          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1541 +        if (createIncomplete) {
1542 +            checkIncomplete(h);
1543 +            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1544 +        }
1545  
1546          checkCompletedWithWrappedCancellationException(h);
1547 <        checkCancelled(f);
1547 >        checkCancelled(!fFirst ? f : g);
1548          assertEquals(0, r.invocationCount);
1549 <        checkCompletedNormally(g, v1);
1549 >        checkCompletedNormally(fFirst ? f : g, v1);
1550      }}
1551  
1552      /**
1553       * runAfterBoth result completes normally after normal
1554       * completion of sources
1555       */
1556 <    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() {
1556 >    public void testRunAfterBoth_normalCompletion() {
1557          for (ExecutionMode m : ExecutionMode.values())
1558 +        for (boolean createIncomplete : new boolean[] { true, false })
1559 +        for (boolean fFirst : new boolean[] { true, false })
1560          for (Integer v1 : new Integer[] { 1, null })
1561          for (Integer v2 : new Integer[] { 2, null })
1562      {
1563          final CompletableFuture<Integer> f = new CompletableFuture<>();
1564          final CompletableFuture<Integer> g = new CompletableFuture<>();
1565 <        final Noop r = new Noop();
1565 >        final Noop r = new Noop(m);
1566  
1567 <        f.complete(v1);
1568 <        g.complete(v2);
1567 >        if (fFirst) f.complete(v1); else g.complete(v2);
1568 >        if (!createIncomplete)
1569 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1570          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1571 +        if (createIncomplete) {
1572 +            checkIncomplete(h);
1573 +            assertEquals(0, r.invocationCount);
1574 +            if (!fFirst) f.complete(v1); else g.complete(v2);
1575 +        }
1576  
1577          checkCompletedNormally(h, null);
1578          assertEquals(1, r.invocationCount);
# Line 1742 | Line 1584 | public class CompletableFutureTest exten
1584       * runAfterBoth result completes exceptionally after exceptional
1585       * completion of either source
1586       */
1587 <    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() {
1587 >    public void testRunAfterBoth_exceptionalCompletion() {
1588          for (ExecutionMode m : ExecutionMode.values())
1589 +        for (boolean createIncomplete : new boolean[] { true, false })
1590 +        for (boolean fFirst : new boolean[] { true, false })
1591          for (Integer v1 : new Integer[] { 1, null })
1592      {
1593          final CompletableFuture<Integer> f = new CompletableFuture<>();
1594          final CompletableFuture<Integer> g = new CompletableFuture<>();
1810        final Noop r = new Noop();
1595          final CFException ex = new CFException();
1596 +        final Noop r = new Noop(m);
1597  
1598 <        f.completeExceptionally(ex);
1599 <        g.complete(v1);
1598 >        (fFirst ? f : g).complete(v1);
1599 >        if (!createIncomplete)
1600 >            (!fFirst ? f : g).completeExceptionally(ex);
1601          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1602 +        if (createIncomplete) {
1603 +            checkIncomplete(h);
1604 +            (!fFirst ? f : g).completeExceptionally(ex);
1605 +        }
1606  
1607          checkCompletedWithWrappedCFException(h, ex);
1818        checkCompletedWithWrappedCFException(f, ex);
1608          assertEquals(0, r.invocationCount);
1609 <        checkCompletedNormally(g, v1);
1609 >        checkCompletedNormally(fFirst ? f : g, v1);
1610 >        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1611      }}
1612  
1613      /**
1614       * runAfterBoth result completes exceptionally if action does
1615       */
1616 <    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() {
1616 >    public void testRunAfterBoth_actionFailed() {
1617          for (ExecutionMode m : ExecutionMode.values())
1618 +        for (boolean fFirst : new boolean[] { true, false })
1619          for (Integer v1 : new Integer[] { 1, null })
1620          for (Integer v2 : new Integer[] { 2, null })
1621      {
1622          final CompletableFuture<Integer> f = new CompletableFuture<>();
1623          final CompletableFuture<Integer> g = new CompletableFuture<>();
1624 <        final FailingNoop r = new FailingNoop();
1853 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1624 >        final FailingRunnable r = new FailingRunnable(m);
1625  
1626 <        g.complete(v2);
1627 <        checkIncomplete(h);
1628 <        f.complete(v1);
1626 >        CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r);
1627 >        if (fFirst) {
1628 >            f.complete(v1);
1629 >            g.complete(v2);
1630 >        } else {
1631 >            g.complete(v2);
1632 >            f.complete(v1);
1633 >        }
1634 >        CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r);
1635  
1636 <        checkCompletedWithWrappedCFException(h);
1636 >        checkCompletedWithWrappedCFException(h1);
1637 >        checkCompletedWithWrappedCFException(h2);
1638          checkCompletedNormally(f, v1);
1639          checkCompletedNormally(g, v2);
1640      }}
# Line 1864 | Line 1642 | public class CompletableFutureTest exten
1642      /**
1643       * runAfterBoth result completes exceptionally if either source cancelled
1644       */
1645 <    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() {
1645 >    public void testRunAfterBoth_sourceCancelled() {
1646          for (ExecutionMode m : ExecutionMode.values())
1647          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1648 +        for (boolean createIncomplete : new boolean[] { true, false })
1649 +        for (boolean fFirst : new boolean[] { true, false })
1650          for (Integer v1 : new Integer[] { 1, null })
1651      {
1652          final CompletableFuture<Integer> f = new CompletableFuture<>();
1653          final CompletableFuture<Integer> g = new CompletableFuture<>();
1654 <        final Noop r = new Noop();
1895 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1654 >        final Noop r = new Noop(m);
1655  
1897        assertTrue(g.cancel(mayInterruptIfRunning));
1898        checkIncomplete(h);
1899        f.complete(v1);
1656  
1657 <        checkCompletedWithWrappedCancellationException(h);
1658 <        checkCancelled(g);
1659 <        assertEquals(0, r.invocationCount);
1904 <        checkCompletedNormally(f, v1);
1905 <    }}
1906 <
1907 <    public void testRunAfterBoth_sourceCancelled3() {
1908 <        for (ExecutionMode m : ExecutionMode.values())
1909 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1910 <        for (Integer v1 : new Integer[] { 1, null })
1911 <    {
1912 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1913 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1914 <        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);
1919 <
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();
1934 <
1935 <        assertTrue(f.cancel(mayInterruptIfRunning));
1936 <        g.complete(v1);
1657 >        (fFirst ? f : g).complete(v1);
1658 >        if (!createIncomplete)
1659 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1660          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1661 +        if (createIncomplete) {
1662 +            checkIncomplete(h);
1663 +            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1664 +        }
1665  
1666          checkCompletedWithWrappedCancellationException(h);
1667 <        checkCancelled(f);
1667 >        checkCancelled(!fFirst ? f : g);
1668          assertEquals(0, r.invocationCount);
1669 <        checkCompletedNormally(g, v1);
1669 >        checkCompletedNormally(fFirst ? f : g, v1);
1670      }}
1671  
1672      /**
1673       * applyToEither result completes normally after normal completion
1674       * of either source
1675       */
1676 <    public void testApplyToEither_normalCompletion1() {
1676 >    public void testApplyToEither_normalCompletion() {
1677          for (ExecutionMode m : ExecutionMode.values())
1678 +        for (boolean createIncomplete : new boolean[] { true, false })
1679 +        for (boolean fFirst : new boolean[] { true, false })
1680          for (Integer v1 : new Integer[] { 1, null })
1681          for (Integer v2 : new Integer[] { 2, null })
1682      {
1683          final CompletableFuture<Integer> f = new CompletableFuture<>();
1684          final CompletableFuture<Integer> g = new CompletableFuture<>();
1685 <        final IncFunction r = new IncFunction();
1957 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1685 >        final IncFunction r = new IncFunction(m);
1686  
1687 <        f.complete(v1);
1688 <        checkCompletedNormally(h, inc(v1));
1689 <        g.complete(v2);
1687 >        if (!createIncomplete)
1688 >            if (fFirst) f.complete(v1); else g.complete(v2);
1689 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1690 >        if (createIncomplete) {
1691 >            checkIncomplete(h);
1692 >            assertEquals(0, r.invocationCount);
1693 >            if (fFirst) f.complete(v1); else g.complete(v2);
1694 >        }
1695 >        checkCompletedNormally(h, inc(fFirst ? v1 : v2));
1696 >        if (!fFirst) f.complete(v1); else g.complete(v2);
1697  
1698          checkCompletedNormally(f, v1);
1699          checkCompletedNormally(g, v2);
1700 <        checkCompletedNormally(h, inc(v1));
1700 >        checkCompletedNormally(h, inc(fFirst ? v1 : v2));
1701      }}
1702  
1703 <    public void testApplyToEither_normalCompletion2() {
1703 >    public void testApplyToEither_normalCompletionBothAvailable() {
1704          for (ExecutionMode m : ExecutionMode.values())
1705 +        for (boolean fFirst : new boolean[] { true, false })
1706          for (Integer v1 : new Integer[] { 1, null })
1707          for (Integer v2 : new Integer[] { 2, null })
1708      {
1709          final CompletableFuture<Integer> f = new CompletableFuture<>();
1710          final CompletableFuture<Integer> g = new CompletableFuture<>();
1711 <        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);
1711 >        final IncFunction r = new IncFunction(m);
1712  
1713 <        checkCompletedNormally(f, v1);
1714 <        checkCompletedNormally(g, v2);
1715 <        checkCompletedNormally(h, inc(v2));
1716 <        }}
1717 <
1718 <    public void testApplyToEither_normalCompletion3() {
1719 <        for (ExecutionMode m : ExecutionMode.values())
1989 <        for (Integer v1 : new Integer[] { 1, null })
1990 <        for (Integer v2 : new Integer[] { 2, null })
1991 <    {
1992 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1993 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1994 <        final IncFunction r = new IncFunction();
1713 >        if (fFirst) {
1714 >            f.complete(v1);
1715 >            g.complete(v2);
1716 >        } else {
1717 >            g.complete(v2);
1718 >            f.complete(v1);
1719 >        }
1720  
1996        f.complete(v1);
1997        g.complete(v2);
1721          final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1722  
1723          checkCompletedNormally(f, v1);
# Line 2012 | Line 1735 | public class CompletableFutureTest exten
1735       */
1736      public void testApplyToEither_exceptionalCompletion1() {
1737          for (ExecutionMode m : ExecutionMode.values())
1738 +        for (boolean createIncomplete : new boolean[] { true, false })
1739 +        for (boolean fFirst : new boolean[] { true, false })
1740          for (Integer v1 : new Integer[] { 1, null })
1741      {
1742          final CompletableFuture<Integer> f = new CompletableFuture<>();
1743          final CompletableFuture<Integer> g = new CompletableFuture<>();
2019        final IncFunction r = new IncFunction();
2020        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1744          final CFException ex = new CFException();
1745 +        final IncFunction r = new IncFunction(m);
1746  
1747 <        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();
1747 >        if (!createIncomplete) (fFirst ? f : g).completeExceptionally(ex);
1748          final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1749 <        final CFException ex = new CFException();
1749 >        if (createIncomplete) {
1750 >            checkIncomplete(h);
1751 >            assertEquals(0, r.invocationCount);
1752 >            (fFirst ? f : g).completeExceptionally(ex);
1753 >        }
1754  
2043        g.completeExceptionally(ex);
1755          checkCompletedWithWrappedCFException(h, ex);
1756 <        f.complete(v1);
1756 >        (!fFirst ? f : g).complete(v1);
1757  
1758          assertEquals(0, r.invocationCount);
1759 <        checkCompletedNormally(f, v1);
1760 <        checkCompletedWithWrappedCFException(g, ex);
1759 >        checkCompletedNormally(!fFirst ? f : g, v1);
1760 >        checkCompletedWithWrappedCFException(fFirst ? f : g, ex);
1761          checkCompletedWithWrappedCFException(h, ex);
1762      }}
1763  
1764 <    public void testApplyToEither_exceptionalCompletion3() {
1764 >    public void testApplyToEither_exceptionalCompletion2() {
1765          for (ExecutionMode m : ExecutionMode.values())
1766 +        for (boolean reverseArgs : new boolean[] { true, false })
1767 +        for (boolean fFirst : new boolean[] { true, false })
1768          for (Integer v1 : new Integer[] { 1, null })
1769      {
1770          final CompletableFuture<Integer> f = new CompletableFuture<>();
1771          final CompletableFuture<Integer> g = new CompletableFuture<>();
1772 <        final IncFunction r = new IncFunction();
1772 >        final IncFunction r1 = new IncFunction(m);
1773 >        final IncFunction r2 = new IncFunction(m);
1774          final CFException ex = new CFException();
1775 <
1776 <        g.completeExceptionally(ex);
1777 <        f.complete(v1);
1778 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1775 >        final CompletableFuture<Integer> j = (reverseArgs ? g : f);
1776 >        final CompletableFuture<Integer> k = (reverseArgs ? f : g);
1777 >        final CompletableFuture<Integer> h1 = m.applyToEither(j, k, r1);
1778 >        if (fFirst) {
1779 >            f.complete(v1);
1780 >            g.completeExceptionally(ex);
1781 >        } else {
1782 >            g.completeExceptionally(ex);
1783 >            f.complete(v1);
1784 >        }
1785 >        final CompletableFuture<Integer> h2 = m.applyToEither(j, k, r2);
1786  
1787          // unspecified behavior
2067        Integer v;
1788          try {
1789 <            assertEquals(inc(v1), h.join());
1790 <            assertEquals(1, r.invocationCount);
1789 >            assertEquals(inc(v1), h1.join());
1790 >            assertEquals(1, r1.invocationCount);
1791          } catch (CompletionException ok) {
1792 <            checkCompletedWithWrappedCFException(h, ex);
1793 <            assertEquals(0, r.invocationCount);
1792 >            checkCompletedWithWrappedCFException(h1, ex);
1793 >            assertEquals(0, r1.invocationCount);
1794          }
1795  
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;
1796          try {
1797 <            assertEquals(inc(v1), h.join());
1798 <            assertEquals(1, r.invocationCount);
1797 >            assertEquals(inc(v1), h2.join());
1798 >            assertEquals(1, r2.invocationCount);
1799          } catch (CompletionException ok) {
1800 <            checkCompletedWithWrappedCFException(h, ex);
1801 <            assertEquals(0, r.invocationCount);
1800 >            checkCompletedWithWrappedCFException(h2, ex);
1801 >            assertEquals(0, r2.invocationCount);
1802          }
1803  
1804 <        checkCompletedWithWrappedCFException(f, ex);
1805 <        checkCompletedNormally(g, v1);
1804 >        checkCompletedWithWrappedCFException(g, ex);
1805 >        checkCompletedNormally(f, v1);
1806      }}
1807  
1808      /**
# Line 2114 | Line 1815 | public class CompletableFutureTest exten
1815      {
1816          final CompletableFuture<Integer> f = new CompletableFuture<>();
1817          final CompletableFuture<Integer> g = new CompletableFuture<>();
1818 <        final FailingFunction r = new FailingFunction();
1818 >        final FailingFunction r = new FailingFunction(m);
1819          final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1820  
1821          f.complete(v1);
# Line 2131 | Line 1832 | public class CompletableFutureTest exten
1832      {
1833          final CompletableFuture<Integer> f = new CompletableFuture<>();
1834          final CompletableFuture<Integer> g = new CompletableFuture<>();
1835 <        final FailingFunction r = new FailingFunction();
1835 >        final FailingFunction r = new FailingFunction(m);
1836          final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1837  
1838          g.complete(v2);
# Line 2147 | Line 1848 | public class CompletableFutureTest exten
1848      public void testApplyToEither_sourceCancelled1() {
1849          for (ExecutionMode m : ExecutionMode.values())
1850          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1851 +        for (boolean createIncomplete : new boolean[] { true, false })
1852 +        for (boolean fFirst : new boolean[] { true, false })
1853          for (Integer v1 : new Integer[] { 1, null })
1854      {
1855          final CompletableFuture<Integer> f = new CompletableFuture<>();
1856          final CompletableFuture<Integer> g = new CompletableFuture<>();
1857 <        final IncFunction r = new IncFunction();
2155 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2156 <
2157 <        assertTrue(f.cancel(mayInterruptIfRunning));
2158 <        checkCompletedWithWrappedCancellationException(h);
2159 <        g.complete(v1);
1857 >        final IncFunction r = new IncFunction(m);
1858  
1859 <        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();
1859 >        if (!createIncomplete) assertTrue((fFirst ? f : g).cancel(mayInterruptIfRunning));
1860          final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1861 +        if (createIncomplete) {
1862 +            checkIncomplete(h);
1863 +            assertEquals(0, r.invocationCount);
1864 +            assertTrue((fFirst ? f : g).cancel(mayInterruptIfRunning));
1865 +        }
1866  
2177        assertTrue(g.cancel(mayInterruptIfRunning));
1867          checkCompletedWithWrappedCancellationException(h);
1868 <        f.complete(v1);
1868 >        (!fFirst ? f : g).complete(v1);
1869  
2181        checkCancelled(g);
1870          assertEquals(0, r.invocationCount);
1871 <        checkCompletedNormally(f, v1);
1871 >        checkCompletedNormally(!fFirst ? f : g, v1);
1872 >        checkCancelled(fFirst ? f : g);
1873          checkCompletedWithWrappedCancellationException(h);
1874      }}
1875  
1876 <    public void testApplyToEither_sourceCancelled3() {
1876 >    public void testApplyToEither_sourceCancelled2() {
1877          for (ExecutionMode m : ExecutionMode.values())
1878          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1879 +        for (boolean reverseArgs : new boolean[] { true, false })
1880 +        for (boolean fFirst : new boolean[] { true, false })
1881          for (Integer v1 : new Integer[] { 1, null })
1882      {
1883          final CompletableFuture<Integer> f = new CompletableFuture<>();
1884          final CompletableFuture<Integer> g = new CompletableFuture<>();
1885 <        final IncFunction r = new IncFunction();
1885 >        final IncFunction r1 = new IncFunction(m);
1886 >        final IncFunction r2 = new IncFunction(m);
1887 >        final CFException ex = new CFException();
1888 >        final CompletableFuture<Integer> j = (reverseArgs ? g : f);
1889 >        final CompletableFuture<Integer> k = (reverseArgs ? f : g);
1890  
1891 <        assertTrue(g.cancel(mayInterruptIfRunning));
1892 <        f.complete(v1);
1893 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1891 >        final CompletableFuture<Integer> h1 = m.applyToEither(j, k, r1);
1892 >        if (fFirst) {
1893 >            f.complete(v1);
1894 >            assertTrue(g.cancel(mayInterruptIfRunning));
1895 >        } else {
1896 >            assertTrue(g.cancel(mayInterruptIfRunning));
1897 >            f.complete(v1);
1898 >        }
1899 >        final CompletableFuture<Integer> h2 = m.applyToEither(j, k, r2);
1900  
1901          // unspecified behavior
2201        Integer v;
1902          try {
1903 <            assertEquals(inc(v1), h.join());
1904 <            assertEquals(1, r.invocationCount);
1903 >            assertEquals(inc(v1), h1.join());
1904 >            assertEquals(1, r1.invocationCount);
1905          } catch (CompletionException ok) {
1906 <            checkCompletedWithWrappedCancellationException(h);
1907 <            assertEquals(0, r.invocationCount);
1906 >            checkCompletedWithWrappedCancellationException(h1);
1907 >            assertEquals(0, r1.invocationCount);
1908          }
1909  
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;
1910          try {
1911 <            assertEquals(inc(v1), h.join());
1912 <            assertEquals(1, r.invocationCount);
1911 >            assertEquals(inc(v1), h2.join());
1912 >            assertEquals(1, r2.invocationCount);
1913          } catch (CompletionException ok) {
1914 <            checkCompletedWithWrappedCancellationException(h);
1915 <            assertEquals(0, r.invocationCount);
1914 >            checkCompletedWithWrappedCancellationException(h2);
1915 >            assertEquals(0, r2.invocationCount);
1916          }
1917  
1918 <        checkCancelled(f);
1919 <        checkCompletedNormally(g, v1);
1918 >        checkCancelled(g);
1919 >        checkCompletedNormally(f, v1);
1920      }}
1921  
1922      /**
# Line 2414 | Line 2095 | public class CompletableFutureTest exten
2095      {
2096          final CompletableFuture<Integer> f = new CompletableFuture<>();
2097          final CompletableFuture<Integer> g = new CompletableFuture<>();
2098 <        final FailingConsumer r = new FailingConsumer();
2098 >        final FailingConsumer r = new FailingConsumer(m);
2099          final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2100  
2101          f.complete(v1);
# Line 2431 | Line 2112 | public class CompletableFutureTest exten
2112      {
2113          final CompletableFuture<Integer> f = new CompletableFuture<>();
2114          final CompletableFuture<Integer> g = new CompletableFuture<>();
2115 <        final FailingConsumer r = new FailingConsumer();
2115 >        final FailingConsumer r = new FailingConsumer(m);
2116          final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2117  
2118          g.complete(v2);
# Line 2551 | Line 2232 | public class CompletableFutureTest exten
2232      {
2233          final CompletableFuture<Integer> f = new CompletableFuture<>();
2234          final CompletableFuture<Integer> g = new CompletableFuture<>();
2235 <        final Noop r = new Noop();
2235 >        final Noop r = new Noop(m);
2236          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2237  
2238          f.complete(v1);
# Line 2572 | Line 2253 | public class CompletableFutureTest exten
2253      {
2254          final CompletableFuture<Integer> f = new CompletableFuture<>();
2255          final CompletableFuture<Integer> g = new CompletableFuture<>();
2256 <        final Noop r = new Noop();
2256 >        final Noop r = new Noop(m);
2257          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2258  
2259          g.complete(v2);
# Line 2593 | Line 2274 | public class CompletableFutureTest exten
2274      {
2275          final CompletableFuture<Integer> f = new CompletableFuture<>();
2276          final CompletableFuture<Integer> g = new CompletableFuture<>();
2277 <        final Noop r = new Noop();
2277 >        final Noop r = new Noop(m);
2278  
2279          f.complete(v1);
2280          g.complete(v2);
# Line 2615 | Line 2296 | public class CompletableFutureTest exten
2296      {
2297          final CompletableFuture<Integer> f = new CompletableFuture<>();
2298          final CompletableFuture<Integer> g = new CompletableFuture<>();
2299 <        final Noop r = new Noop();
2299 >        final Noop r = new Noop(m);
2300          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2301          final CFException ex = new CFException();
2302  
# Line 2635 | Line 2316 | public class CompletableFutureTest exten
2316      {
2317          final CompletableFuture<Integer> f = new CompletableFuture<>();
2318          final CompletableFuture<Integer> g = new CompletableFuture<>();
2319 <        final Noop r = new Noop();
2319 >        final Noop r = new Noop(m);
2320          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2321          final CFException ex = new CFException();
2322  
# Line 2655 | Line 2336 | public class CompletableFutureTest exten
2336      {
2337          final CompletableFuture<Integer> f = new CompletableFuture<>();
2338          final CompletableFuture<Integer> g = new CompletableFuture<>();
2339 <        final Noop r = new Noop();
2339 >        final Noop r = new Noop(m);
2340          final CFException ex = new CFException();
2341  
2342          g.completeExceptionally(ex);
# Line 2682 | Line 2363 | public class CompletableFutureTest exten
2363      {
2364          final CompletableFuture<Integer> f = new CompletableFuture<>();
2365          final CompletableFuture<Integer> g = new CompletableFuture<>();
2366 <        final Noop r = new Noop();
2366 >        final Noop r = new Noop(m);
2367          final CFException ex = new CFException();
2368  
2369          f.completeExceptionally(ex);
# Line 2713 | Line 2394 | public class CompletableFutureTest exten
2394      {
2395          final CompletableFuture<Integer> f = new CompletableFuture<>();
2396          final CompletableFuture<Integer> g = new CompletableFuture<>();
2397 <        final FailingNoop r = new FailingNoop();
2397 >        final FailingRunnable r = new FailingRunnable(m);
2398          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2399  
2400          f.complete(v1);
# Line 2730 | Line 2411 | public class CompletableFutureTest exten
2411      {
2412          final CompletableFuture<Integer> f = new CompletableFuture<>();
2413          final CompletableFuture<Integer> g = new CompletableFuture<>();
2414 <        final FailingNoop r = new FailingNoop();
2414 >        final FailingRunnable r = new FailingRunnable(m);
2415          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2416  
2417          g.complete(v2);
# Line 2750 | Line 2431 | public class CompletableFutureTest exten
2431      {
2432          final CompletableFuture<Integer> f = new CompletableFuture<>();
2433          final CompletableFuture<Integer> g = new CompletableFuture<>();
2434 <        final Noop r = new Noop();
2434 >        final Noop r = new Noop(m);
2435          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2436  
2437          assertTrue(f.cancel(mayInterruptIfRunning));
# Line 2770 | Line 2451 | public class CompletableFutureTest exten
2451      {
2452          final CompletableFuture<Integer> f = new CompletableFuture<>();
2453          final CompletableFuture<Integer> g = new CompletableFuture<>();
2454 <        final Noop r = new Noop();
2454 >        final Noop r = new Noop(m);
2455          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2456  
2457          assertTrue(g.cancel(mayInterruptIfRunning));
# Line 2790 | Line 2471 | public class CompletableFutureTest exten
2471      {
2472          final CompletableFuture<Integer> f = new CompletableFuture<>();
2473          final CompletableFuture<Integer> g = new CompletableFuture<>();
2474 <        final Noop r = new Noop();
2474 >        final Noop r = new Noop(m);
2475  
2476          assertTrue(g.cancel(mayInterruptIfRunning));
2477          f.complete(v1);
# Line 2817 | Line 2498 | public class CompletableFutureTest exten
2498      {
2499          final CompletableFuture<Integer> f = new CompletableFuture<>();
2500          final CompletableFuture<Integer> g = new CompletableFuture<>();
2501 <        final Noop r = new Noop();
2501 >        final Noop r = new Noop(m);
2502  
2503          assertTrue(f.cancel(mayInterruptIfRunning));
2504          g.complete(v1);
# Line 2840 | Line 2521 | public class CompletableFutureTest exten
2521      /**
2522       * thenCompose result completes normally after normal completion of source
2523       */
2524 <    public void testThenCompose_normalCompletion1() {
2524 >    public void testThenCompose_normalCompletion() {
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 CompletableFutureInc r = new CompletableFutureInc();
2531 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2532 <        f.complete(v1);
2533 <        checkCompletedNormally(g, inc(v1));
2852 <        checkCompletedNormally(f, v1);
2853 <        assertEquals(1, r.invocationCount);
2854 <    }}
2530 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2531 >        if (!createIncomplete) f.complete(v1);
2532 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2533 >        if (createIncomplete) f.complete(v1);
2534  
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);
2535          checkCompletedNormally(g, inc(v1));
2536          checkCompletedNormally(f, v1);
2537          assertEquals(1, r.invocationCount);
# Line 2870 | Line 2541 | public class CompletableFutureTest exten
2541       * thenCompose result completes exceptionally after exceptional
2542       * completion of source
2543       */
2544 <    public void testThenCompose_exceptionalCompletion1() {
2544 >    public void testThenCompose_exceptionalCompletion() {
2545          for (ExecutionMode m : ExecutionMode.values())
2546 +        for (boolean createIncomplete : new boolean[] { true, false })
2547      {
2548          final CFException ex = new CFException();
2549 <        final CompletableFutureInc r = new CompletableFutureInc();
2549 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2550          final CompletableFuture<Integer> f = new CompletableFuture<>();
2551 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2552 <        f.completeExceptionally(ex);
2553 <        checkCompletedWithWrappedCFException(g, ex);
2882 <        checkCompletedWithWrappedCFException(f, ex);
2883 <    }}
2551 >        if (!createIncomplete) f.completeExceptionally(ex);
2552 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2553 >        if (createIncomplete) f.completeExceptionally(ex);
2554  
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);
2555          checkCompletedWithWrappedCFException(g, ex);
2556          checkCompletedWithWrappedCFException(f, ex);
2557 +        assertEquals(0, r.invocationCount);
2558      }}
2559  
2560      /**
2561       * thenCompose result completes exceptionally if action does
2562       */
2563 <    public void testThenCompose_actionFailed1() {
2563 >    public void testThenCompose_actionFailed() {
2564          for (ExecutionMode m : ExecutionMode.values())
2565 +        for (boolean createIncomplete : new boolean[] { true, false })
2566          for (Integer v1 : new Integer[] { 1, null })
2567      {
2568          final CompletableFuture<Integer> f = new CompletableFuture<>();
2569          final FailingCompletableFutureFunction r
2570 <            = new FailingCompletableFutureFunction();
2571 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2572 <        f.complete(v1);
2573 <        checkCompletedWithWrappedCFException(g);
2910 <        checkCompletedNormally(f, v1);
2911 <    }}
2570 >            = new FailingCompletableFutureFunction(m);
2571 >        if (!createIncomplete) f.complete(v1);
2572 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2573 >        if (createIncomplete) f.complete(v1);
2574  
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);
2575          checkCompletedWithWrappedCFException(g);
2576          checkCompletedNormally(f, v1);
2577      }}
# Line 2926 | Line 2579 | public class CompletableFutureTest exten
2579      /**
2580       * thenCompose result completes exceptionally if source cancelled
2581       */
2582 <    public void testThenCompose_sourceCancelled1() {
2582 >    public void testThenCompose_sourceCancelled() {
2583          for (ExecutionMode m : ExecutionMode.values())
2584 +        for (boolean createIncomplete : new boolean[] { true, false })
2585          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2586      {
2587          final CompletableFuture<Integer> f = new CompletableFuture<>();
2588 <        final CompletableFutureInc r = new CompletableFutureInc();
2589 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2590 <        assertTrue(f.cancel(mayInterruptIfRunning));
2591 <        checkCompletedWithWrappedCancellationException(g);
2592 <        checkCancelled(f);
2593 <    }}
2588 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2589 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2590 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2591 >        if (createIncomplete) {
2592 >            checkIncomplete(g);
2593 >            assertTrue(f.cancel(mayInterruptIfRunning));
2594 >        }
2595  
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);
2596          checkCompletedWithWrappedCancellationException(g);
2597          checkCancelled(f);
2598      }}
2599  
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
2600      // other static methods
2601  
2602      /**
# Line 3333 | Line 2686 | public class CompletableFutureTest exten
2686          Runnable[] throwingActions = {
2687              () -> CompletableFuture.supplyAsync(null),
2688              () -> CompletableFuture.supplyAsync(null, exec),
2689 <            () -> CompletableFuture.supplyAsync(supplyOne, null),
2689 >            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null),
2690  
2691              () -> CompletableFuture.runAsync(null),
2692              () -> CompletableFuture.runAsync(null, exec),
# Line 3406 | Line 2759 | public class CompletableFutureTest exten
2759  
2760              () -> f.thenCompose(null),
2761              () -> f.thenComposeAsync(null),
2762 <            () -> f.thenComposeAsync(new CompletableFutureInc(), null),
2762 >            () -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null),
2763              () -> f.thenComposeAsync(null, exec),
2764  
2765              () -> f.exceptionally(null),

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines