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.67 by jsr166, Fri Jun 6 19:25:41 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 318 | Line 320 | public class CompletableFutureTest exten
320          checkCompletedNormally(f, "test");
321      }
322  
323 <    // Choose non-commutative actions for better coverage
323 >    abstract class CheckedAction {
324 >        int invocationCount = 0;
325 >        final ExecutionMode m;
326 >        CheckedAction(ExecutionMode m) { this.m = m; }
327 >        void invoked() {
328 >            m.checkExecutionMode();
329 >            assertEquals(0, invocationCount++);
330 >        }
331 >        void assertNotInvoked() { assertEquals(0, invocationCount); }
332 >        void assertInvoked() { assertEquals(1, invocationCount); }
333 >    }
334  
335 <    // A non-commutative function that handles and produces null values as well.
336 <    static Integer subtract(Integer x, Integer y) {
337 <        return (x == null && y == null) ? null :
338 <            ((x == null) ? 42 : x.intValue())
339 <            - ((y == null) ? 99 : y.intValue());
335 >    abstract class CheckedIntegerAction extends CheckedAction {
336 >        Integer value;
337 >        CheckedIntegerAction(ExecutionMode m) { super(m); }
338 >        void assertValue(Integer expected) {
339 >            assertInvoked();
340 >            assertEquals(expected, value);
341 >        }
342 >    }
343 >
344 >    class IntegerSupplier extends CheckedAction
345 >        implements Supplier<Integer>
346 >    {
347 >        final Integer value;
348 >        IntegerSupplier(ExecutionMode m, Integer value) {
349 >            super(m);
350 >            this.value = value;
351 >        }
352 >        public Integer get() {
353 >            invoked();
354 >            return value;
355 >        }
356      }
357  
358      // A function that handles and produces null values as well.
# Line 332 | Line 360 | public class CompletableFutureTest exten
360          return (x == null) ? null : x + 1;
361      }
362  
363 <    static final Supplier<Integer> supplyOne =
364 <        () -> Integer.valueOf(1);
365 <    static final Function<Integer, Integer> inc =
366 <        (Integer x) -> Integer.valueOf(x.intValue() + 1);
339 <    static final BiFunction<Integer, Integer, Integer> subtract =
340 <        (Integer x, Integer y) -> subtract(x, y);
341 <    static final class IncAction implements Consumer<Integer> {
342 <        int invocationCount = 0;
343 <        Integer value;
363 >    class NoopConsumer extends CheckedIntegerAction
364 >        implements Consumer<Integer>
365 >    {
366 >        NoopConsumer(ExecutionMode m) { super(m); }
367          public void accept(Integer x) {
368 <            invocationCount++;
369 <            value = inc(x);
368 >            invoked();
369 >            value = x;
370          }
371      }
372 <    static final class IncFunction implements Function<Integer,Integer> {
373 <        int invocationCount = 0;
374 <        Integer value;
372 >
373 >    class IncFunction extends CheckedIntegerAction
374 >        implements Function<Integer,Integer>
375 >    {
376 >        IncFunction(ExecutionMode m) { super(m); }
377          public Integer apply(Integer x) {
378 <            invocationCount++;
378 >            invoked();
379              return value = inc(x);
380          }
381      }
382 <    static final class SubtractAction implements BiConsumer<Integer, Integer> {
383 <        int invocationCount = 0;
384 <        Integer value;
385 <        // Check this action was invoked exactly once when result is computed.
382 >
383 >    // Choose non-commutative actions for better coverage
384 >    // A non-commutative function that handles and produces null values as well.
385 >    static Integer subtract(Integer x, Integer y) {
386 >        return (x == null && y == null) ? null :
387 >            ((x == null) ? 42 : x.intValue())
388 >            - ((y == null) ? 99 : y.intValue());
389 >    }
390 >
391 >    class SubtractAction extends CheckedIntegerAction
392 >        implements BiConsumer<Integer, Integer>
393 >    {
394 >        SubtractAction(ExecutionMode m) { super(m); }
395          public void accept(Integer x, Integer y) {
396 <            invocationCount++;
396 >            invoked();
397              value = subtract(x, y);
398          }
399      }
400 <    static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> {
401 <        int invocationCount = 0;
402 <        Integer value;
403 <        // Check this action was invoked exactly once when result is computed.
400 >
401 >    class SubtractFunction extends CheckedIntegerAction
402 >        implements BiFunction<Integer, Integer, Integer>
403 >    {
404 >        SubtractFunction(ExecutionMode m) { super(m); }
405          public Integer apply(Integer x, Integer y) {
406 <            invocationCount++;
406 >            invoked();
407              return value = subtract(x, y);
408          }
409      }
410 <    static final class Noop implements Runnable {
411 <        int invocationCount = 0;
410 >
411 >    class Noop extends CheckedAction implements Runnable {
412 >        Noop(ExecutionMode m) { super(m); }
413          public void run() {
414 <            invocationCount++;
414 >            invoked();
415          }
416      }
417  
418 <    static final class FailingSupplier implements Supplier<Integer> {
419 <        int invocationCount = 0;
418 >    class FailingSupplier extends CheckedAction
419 >        implements Supplier<Integer>
420 >    {
421 >        FailingSupplier(ExecutionMode m) { super(m); }
422          public Integer get() {
423 <            invocationCount++;
423 >            invoked();
424              throw new CFException();
425          }
426      }
427 <    static final class FailingConsumer implements Consumer<Integer> {
428 <        int invocationCount = 0;
427 >
428 >    class FailingConsumer extends CheckedIntegerAction
429 >        implements Consumer<Integer>
430 >    {
431 >        FailingConsumer(ExecutionMode m) { super(m); }
432          public void accept(Integer x) {
433 <            invocationCount++;
433 >            invoked();
434 >            value = x;
435              throw new CFException();
436          }
437      }
438 <    static final class FailingBiConsumer implements BiConsumer<Integer, Integer> {
439 <        int invocationCount = 0;
438 >
439 >    class FailingBiConsumer extends CheckedIntegerAction
440 >        implements BiConsumer<Integer, Integer>
441 >    {
442 >        FailingBiConsumer(ExecutionMode m) { super(m); }
443          public void accept(Integer x, Integer y) {
444 <            invocationCount++;
444 >            invoked();
445 >            value = subtract(x, y);
446              throw new CFException();
447          }
448      }
449 <    static final class FailingFunction implements Function<Integer, Integer> {
450 <        int invocationCount = 0;
449 >
450 >    class FailingFunction extends CheckedIntegerAction
451 >        implements Function<Integer, Integer>
452 >    {
453 >        FailingFunction(ExecutionMode m) { super(m); }
454          public Integer apply(Integer x) {
455 <            invocationCount++;
455 >            invoked();
456 >            value = x;
457              throw new CFException();
458          }
459      }
460 <    static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> {
461 <        int invocationCount = 0;
460 >
461 >    class FailingBiFunction extends CheckedIntegerAction
462 >        implements BiFunction<Integer, Integer, Integer>
463 >    {
464 >        FailingBiFunction(ExecutionMode m) { super(m); }
465          public Integer apply(Integer x, Integer y) {
466 <            invocationCount++;
466 >            invoked();
467 >            value = subtract(x, y);
468              throw new CFException();
469          }
470      }
471 <    static final class FailingNoop implements Runnable {
472 <        int invocationCount = 0;
471 >
472 >    class FailingRunnable extends CheckedAction implements Runnable {
473 >        FailingRunnable(ExecutionMode m) { super(m); }
474          public void run() {
475 <            invocationCount++;
475 >            invoked();
476              throw new CFException();
477          }
478      }
479  
480 <    static final class CompletableFutureInc
481 <        implements Function<Integer, CompletableFuture<Integer>> {
482 <        int invocationCount = 0;
480 >
481 >    class CompletableFutureInc extends CheckedIntegerAction
482 >        implements Function<Integer, CompletableFuture<Integer>>
483 >    {
484 >        CompletableFutureInc(ExecutionMode m) { super(m); }
485          public CompletableFuture<Integer> apply(Integer x) {
486 <            invocationCount++;
486 >            invoked();
487 >            value = x;
488              CompletableFuture<Integer> f = new CompletableFuture<>();
489              f.complete(inc(x));
490              return f;
491          }
492      }
493  
494 <    static final class FailingCompletableFutureFunction
495 <        implements Function<Integer, CompletableFuture<Integer>> {
496 <        int invocationCount = 0;
494 >    class FailingCompletableFutureFunction extends CheckedIntegerAction
495 >        implements Function<Integer, CompletableFuture<Integer>>
496 >    {
497 >        FailingCompletableFutureFunction(ExecutionMode m) { super(m); }
498          public CompletableFuture<Integer> apply(Integer x) {
499 <            invocationCount++;
499 >            invoked();
500 >            value = x;
501              throw new CFException();
502          }
503      }
504  
505      // Used for explicit executor tests
506      static final class ThreadExecutor implements Executor {
507 <        AtomicInteger count = new AtomicInteger(0);
507 >        final AtomicInteger count = new AtomicInteger(0);
508 >        static final ThreadGroup tg = new ThreadGroup("ThreadExecutor");
509 >        static boolean startedCurrentThread() {
510 >            return Thread.currentThread().getThreadGroup() == tg;
511 >        }
512  
513          public void execute(Runnable r) {
514              count.getAndIncrement();
515 <            new Thread(r).start();
515 >            new Thread(tg, r).start();
516          }
517      }
518  
519      /**
520       * Permits the testing of parallel code for the 3 different
521 <     * execution modes without repeating all the testing code.
521 >     * execution modes without copy/pasting all the test methods.
522       */
523      enum ExecutionMode {
524          DEFAULT {
525 +            public void checkExecutionMode() {
526 +                assertFalse(ThreadExecutor.startedCurrentThread());
527 +                assertNull(ForkJoinTask.getPool());
528 +            }
529 +            public CompletableFuture<Void> runAsync(Runnable a) {
530 +                throw new UnsupportedOperationException();
531 +            }
532 +            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
533 +                throw new UnsupportedOperationException();
534 +            }
535              public <T> CompletableFuture<Void> thenRun
536                  (CompletableFuture<T> f, Runnable a) {
537                  return f.thenRun(a);
# Line 521 | Line 595 | public class CompletableFutureTest exten
595              }
596          },
597  
598 <        DEFAULT_ASYNC {
598 >        ASYNC {
599 >            public void checkExecutionMode() {
600 >                assertSame(ForkJoinPool.commonPool(),
601 >                           ForkJoinTask.getPool());
602 >            }
603 >            public CompletableFuture<Void> runAsync(Runnable a) {
604 >                return CompletableFuture.runAsync(a);
605 >            }
606 >            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
607 >                return CompletableFuture.supplyAsync(a);
608 >            }
609              public <T> CompletableFuture<Void> thenRun
610                  (CompletableFuture<T> f, Runnable a) {
611                  return f.thenRunAsync(a);
# Line 586 | Line 670 | public class CompletableFutureTest exten
670          },
671  
672          EXECUTOR {
673 +            public void checkExecutionMode() {
674 +                assertTrue(ThreadExecutor.startedCurrentThread());
675 +            }
676 +            public CompletableFuture<Void> runAsync(Runnable a) {
677 +                return CompletableFuture.runAsync(a, new ThreadExecutor());
678 +            }
679 +            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
680 +                return CompletableFuture.supplyAsync(a, new ThreadExecutor());
681 +            }
682              public <T> CompletableFuture<Void> thenRun
683                  (CompletableFuture<T> f, Runnable a) {
684                  return f.thenRunAsync(a, new ThreadExecutor());
# Line 649 | Line 742 | public class CompletableFutureTest exten
742              }
743          };
744  
745 +        public abstract void checkExecutionMode();
746 +        public abstract CompletableFuture<Void> runAsync(Runnable a);
747 +        public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a);
748          public abstract <T> CompletableFuture<Void> thenRun
749              (CompletableFuture<T> f, Runnable a);
750          public abstract <T> CompletableFuture<Void> thenAccept
# Line 727 | Line 823 | public class CompletableFutureTest exten
823          if (!createIncomplete) f.completeExceptionally(ex);
824          final CompletableFuture<Integer> g = f.exceptionally
825              ((Throwable t) -> {
826 +                ExecutionMode.DEFAULT.checkExecutionMode();
827                  threadAssertSame(t, ex);
828                  a.getAndIncrement();
829                  return v1;
# Line 748 | Line 845 | public class CompletableFutureTest exten
845          if (!createIncomplete) f.completeExceptionally(ex1);
846          final CompletableFuture<Integer> g = f.exceptionally
847              ((Throwable t) -> {
848 +                ExecutionMode.DEFAULT.checkExecutionMode();
849                  threadAssertSame(t, ex1);
850                  a.getAndIncrement();
851                  throw ex2;
# Line 773 | Line 871 | public class CompletableFutureTest exten
871          final CompletableFuture<Integer> g = m.handle
872              (f,
873               (Integer x, Throwable t) -> {
874 +                m.checkExecutionMode();
875                  threadAssertSame(x, v1);
876                  threadAssertNull(t);
877                  a.getAndIncrement();
# Line 801 | Line 900 | public class CompletableFutureTest exten
900          final CompletableFuture<Integer> g = m.handle
901              (f,
902               (Integer x, Throwable t) -> {
903 +                m.checkExecutionMode();
904                  threadAssertNull(x);
905                  threadAssertSame(t, ex);
906                  a.getAndIncrement();
# Line 829 | Line 929 | public class CompletableFutureTest exten
929          final CompletableFuture<Integer> g = m.handle
930              (f,
931               (Integer x, Throwable t) -> {
932 +                m.checkExecutionMode();
933                  threadAssertNull(x);
934                  threadAssertTrue(t instanceof CancellationException);
935                  a.getAndIncrement();
# Line 856 | Line 957 | public class CompletableFutureTest exten
957          final CompletableFuture<Integer> g = m.handle
958              (f,
959               (Integer x, Throwable t) -> {
960 +                m.checkExecutionMode();
961                  threadAssertNull(x);
962                  threadAssertSame(ex1, t);
963                  a.getAndIncrement();
# Line 880 | Line 982 | public class CompletableFutureTest exten
982          final CompletableFuture<Integer> g = m.handle
983              (f,
984               (Integer x, Throwable t) -> {
985 +                m.checkExecutionMode();
986                  threadAssertSame(x, v1);
987                  threadAssertNull(t);
988                  a.getAndIncrement();
# Line 895 | Line 998 | public class CompletableFutureTest exten
998      /**
999       * runAsync completes after running Runnable
1000       */
1001 <    public void testRunAsync() {
1002 <        Noop r = new Noop();
1003 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
1004 <        assertNull(f.join());
1005 <        assertEquals(1, r.invocationCount);
1006 <        checkCompletedNormally(f, null);
1007 <    }
1008 <
1009 <    /**
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);
1001 >    public void testRunAsync_normalCompletion() {
1002 >        ExecutionMode[] executionModes = {
1003 >            ExecutionMode.ASYNC,
1004 >            ExecutionMode.EXECUTOR,
1005 >        };
1006 >        for (ExecutionMode m : executionModes)
1007 >    {
1008 >        final Noop r = new Noop(m);
1009 >        final CompletableFuture<Void> f = m.runAsync(r);
1010          assertNull(f.join());
914        assertEquals(1, r.invocationCount);
1011          checkCompletedNormally(f, null);
1012 <        assertEquals(1, exec.count.get());
1013 <    }
1012 >        r.assertInvoked();
1013 >    }}
1014  
1015      /**
1016       * failing runAsync completes exceptionally after running Runnable
1017       */
1018 <    public void testRunAsync3() {
1019 <        FailingNoop r = new FailingNoop();
1020 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
1018 >    public void testRunAsync_exceptionalCompletion() {
1019 >        ExecutionMode[] executionModes = {
1020 >            ExecutionMode.ASYNC,
1021 >            ExecutionMode.EXECUTOR,
1022 >        };
1023 >        for (ExecutionMode m : executionModes)
1024 >    {
1025 >        final FailingRunnable r = new FailingRunnable(m);
1026 >        final CompletableFuture<Void> f = m.runAsync(r);
1027          checkCompletedWithWrappedCFException(f);
1028 <        assertEquals(1, r.invocationCount);
1029 <    }
1028 >        r.assertInvoked();
1029 >    }}
1030  
1031      /**
1032       * supplyAsync completes with result of supplier
1033       */
1034 <    public void testSupplyAsync() {
1035 <        CompletableFuture<Integer> f;
1036 <        f = CompletableFuture.supplyAsync(supplyOne);
1037 <        assertEquals(f.join(), one);
1038 <        checkCompletedNormally(f, one);
1039 <    }
1040 <
1041 <    /**
1042 <     * supplyAsync with executor completes with result of supplier
1043 <     */
1044 <    public void testSupplyAsync2() {
1045 <        CompletableFuture<Integer> f;
1046 <        f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor());
1047 <        assertEquals(f.join(), one);
946 <        checkCompletedNormally(f, one);
947 <    }
1034 >    public void testSupplyAsync_normalCompletion() {
1035 >        ExecutionMode[] executionModes = {
1036 >            ExecutionMode.ASYNC,
1037 >            ExecutionMode.EXECUTOR,
1038 >        };
1039 >        for (ExecutionMode m : executionModes)
1040 >        for (Integer v1 : new Integer[] { 1, null })
1041 >    {
1042 >        final IntegerSupplier r = new IntegerSupplier(m, v1);
1043 >        final CompletableFuture<Integer> f = m.supplyAsync(r);
1044 >        assertSame(v1, f.join());
1045 >        checkCompletedNormally(f, v1);
1046 >        r.assertInvoked();
1047 >    }}
1048  
1049      /**
1050       * Failing supplyAsync completes exceptionally
1051       */
1052 <    public void testSupplyAsync3() {
1053 <        FailingSupplier r = new FailingSupplier();
1054 <        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r);
1052 >    public void testSupplyAsync_exceptionalCompletion() {
1053 >        ExecutionMode[] executionModes = {
1054 >            ExecutionMode.ASYNC,
1055 >            ExecutionMode.EXECUTOR,
1056 >        };
1057 >        for (ExecutionMode m : executionModes)
1058 >    {
1059 >        FailingSupplier r = new FailingSupplier(m);
1060 >        CompletableFuture<Integer> f = m.supplyAsync(r);
1061          checkCompletedWithWrappedCFException(f);
1062 <        assertEquals(1, r.invocationCount);
1063 <    }
1062 >        r.assertInvoked();
1063 >    }}
1064  
1065      // seq completion methods
1066  
1067      /**
1068       * thenRun result completes normally after normal completion of source
1069       */
1070 <    public void testThenRun() {
965 <        CompletableFuture<Integer> f;
966 <        CompletableFuture<Void> g;
967 <        Noop r;
968 <
969 <        f = new CompletableFuture<>();
970 <        g = f.thenRun(r = new Noop());
971 <        f.complete(null);
972 <        checkCompletedNormally(g, null);
973 <        assertEquals(1, r.invocationCount);
974 <
975 <        f = new CompletableFuture<>();
976 <        f.complete(null);
977 <        g = f.thenRun(r = new Noop());
978 <        checkCompletedNormally(g, null);
979 <        assertEquals(1, r.invocationCount);
980 <    }
981 <
982 <    /**
983 <     * thenRun result completes exceptionally after exceptional
984 <     * completion of source
985 <     */
986 <    public void testThenRun2() {
987 <        CompletableFuture<Integer> f;
988 <        CompletableFuture<Void> g;
989 <        Noop r;
990 <
991 <        f = new CompletableFuture<>();
992 <        g = f.thenRun(r = new Noop());
993 <        f.completeExceptionally(new CFException());
994 <        checkCompletedWithWrappedCFException(g);
995 <        assertEquals(0, r.invocationCount);
996 <
997 <        f = new CompletableFuture<>();
998 <        f.completeExceptionally(new CFException());
999 <        g = f.thenRun(r = new Noop());
1000 <        checkCompletedWithWrappedCFException(g);
1001 <        assertEquals(0, r.invocationCount);
1002 <    }
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 <    }
1022 <
1023 <    /**
1024 <     * thenRun result completes exceptionally if source cancelled
1025 <     */
1026 <    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 })
1070 >    public void testThenRun_normalCompletion() {
1071          for (ExecutionMode m : ExecutionMode.values())
1072 +        for (boolean createIncomplete : new boolean[] { true, false })
1073          for (Integer v1 : new Integer[] { 1, null })
1139        for (Integer v2 : new Integer[] { 2, null })
1074      {
1075          final CompletableFuture<Integer> f = new CompletableFuture<>();
1076 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1077 <        final SubtractFunction r = new SubtractFunction();
1078 <        CompletableFuture<Integer> h = null;
1079 <        if (createIncomplete) h = m.thenCombine(f, g, r);
1080 <
1147 <        if (fFirst)
1148 <            f.complete(v1);
1149 <        else
1150 <            g.complete(v2);
1151 <        if (createIncomplete) checkIncomplete(h);
1152 <        assertEquals(0, r.invocationCount);
1153 <        if (!fFirst)
1076 >        final Noop r = new Noop(m);
1077 >        if (!createIncomplete) f.complete(v1);
1078 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1079 >        if (createIncomplete) {
1080 >            checkIncomplete(g);
1081              f.complete(v1);
1082 <        else
1156 <            g.complete(v2);
1157 <        if (!createIncomplete) h = m.thenCombine(f, g, r);
1082 >        }
1083  
1084 <        checkCompletedNormally(h, subtract(v1, v2));
1084 >        checkCompletedNormally(g, null);
1085          checkCompletedNormally(f, v1);
1086 <        checkCompletedNormally(g, v2);
1162 <        assertEquals(1, r.invocationCount);
1086 >        r.assertInvoked();
1087      }}
1088  
1089      /**
1090 <     * thenCombine result completes exceptionally after exceptional
1091 <     * completion of either source
1090 >     * thenRun result completes exceptionally after exceptional
1091 >     * completion of source
1092       */
1093 <    public void testThenCombine_exceptionalCompletion1() {
1093 >    public void testThenRun_exceptionalCompletion() {
1094          for (ExecutionMode m : ExecutionMode.values())
1095 <        for (Integer v1 : new Integer[] { 1, null })
1095 >        for (boolean createIncomplete : new boolean[] { true, false })
1096      {
1173        final CompletableFuture<Integer> f = new CompletableFuture<>();
1174        final CompletableFuture<Integer> g = new CompletableFuture<>();
1175        final SubtractFunction r = new SubtractFunction();
1176        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1097          final CFException ex = new CFException();
1178
1179        f.completeExceptionally(ex);
1180        checkIncomplete(h);
1181        g.complete(v1);
1182
1183        checkCompletedWithWrappedCFException(h, ex);
1184        checkCompletedWithWrappedCFException(f, ex);
1185        assertEquals(0, r.invocationCount);
1186        checkCompletedNormally(g, v1);
1187    }}
1188
1189    public void testThenCombine_exceptionalCompletion2() {
1190        for (ExecutionMode m : ExecutionMode.values())
1191        for (Integer v1 : new Integer[] { 1, null })
1192    {
1098          final CompletableFuture<Integer> f = new CompletableFuture<>();
1099 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1100 <        final SubtractFunction r = new SubtractFunction();
1101 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1102 <        final CFException ex = new CFException();
1103 <
1104 <        g.completeExceptionally(ex);
1105 <        checkIncomplete(h);
1201 <        f.complete(v1);
1099 >        final Noop r = new Noop(m);
1100 >        if (!createIncomplete) f.completeExceptionally(ex);
1101 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1102 >        if (createIncomplete) {
1103 >            checkIncomplete(g);
1104 >            f.completeExceptionally(ex);
1105 >        }
1106  
1203        checkCompletedWithWrappedCFException(h, ex);
1107          checkCompletedWithWrappedCFException(g, ex);
1108 <        assertEquals(0, r.invocationCount);
1109 <        checkCompletedNormally(f, v1);
1108 >        checkCompletedWithWrappedCFException(f, ex);
1109 >        r.assertNotInvoked();
1110      }}
1111  
1112 <    public void testThenCombine_exceptionalCompletion3() {
1112 >    /**
1113 >     * thenRun result completes exceptionally if source cancelled
1114 >     */
1115 >    public void testThenRun_sourceCancelled() {
1116          for (ExecutionMode m : ExecutionMode.values())
1117 <        for (Integer v1 : new Integer[] { 1, null })
1117 >        for (boolean createIncomplete : new boolean[] { true, false })
1118 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1119      {
1120          final CompletableFuture<Integer> f = new CompletableFuture<>();
1121 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1122 <        final SubtractFunction r = new SubtractFunction();
1123 <        final CFException ex = new CFException();
1124 <
1125 <        g.completeExceptionally(ex);
1126 <        f.complete(v1);
1127 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1121 >        final Noop r = new Noop(m);
1122 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1123 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1124 >        if (createIncomplete) {
1125 >            checkIncomplete(g);
1126 >            assertTrue(f.cancel(mayInterruptIfRunning));
1127 >        }
1128  
1129 <        checkCompletedWithWrappedCFException(h, ex);
1130 <        checkCompletedWithWrappedCFException(g, ex);
1131 <        assertEquals(0, r.invocationCount);
1225 <        checkCompletedNormally(f, v1);
1129 >        checkCompletedWithWrappedCancellationException(g);
1130 >        checkCancelled(f);
1131 >        r.assertNotInvoked();
1132      }}
1133  
1134 <    public void testThenCombine_exceptionalCompletion4() {
1134 >    /**
1135 >     * thenRun result completes exceptionally if action does
1136 >     */
1137 >    public void testThenRun_actionFailed() {
1138          for (ExecutionMode m : ExecutionMode.values())
1139 +        for (boolean createIncomplete : new boolean[] { true, false })
1140          for (Integer v1 : new Integer[] { 1, null })
1141      {
1142          final CompletableFuture<Integer> f = new CompletableFuture<>();
1143 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1144 <        final SubtractFunction r = new SubtractFunction();
1145 <        final CFException ex = new CFException();
1146 <
1147 <        f.completeExceptionally(ex);
1148 <        g.complete(v1);
1149 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1143 >        final FailingRunnable r = new FailingRunnable(m);
1144 >        if (!createIncomplete) f.complete(v1);
1145 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1146 >        if (createIncomplete) {
1147 >            checkIncomplete(g);
1148 >            f.complete(v1);
1149 >        }
1150  
1151 <        checkCompletedWithWrappedCFException(h, ex);
1152 <        checkCompletedWithWrappedCFException(f, ex);
1243 <        assertEquals(0, r.invocationCount);
1244 <        checkCompletedNormally(g, v1);
1151 >        checkCompletedWithWrappedCFException(g);
1152 >        checkCompletedNormally(f, v1);
1153      }}
1154  
1155      /**
1156 <     * thenCombine result completes exceptionally if action does
1156 >     * thenApply result completes normally after normal completion of source
1157       */
1158 <    public void testThenCombine_actionFailed1() {
1158 >    public void testThenApply_normalCompletion() {
1159          for (ExecutionMode m : ExecutionMode.values())
1160 +        for (boolean createIncomplete : new boolean[] { true, false })
1161          for (Integer v1 : new Integer[] { 1, null })
1253        for (Integer v2 : new Integer[] { 2, null })
1162      {
1163          final CompletableFuture<Integer> f = new CompletableFuture<>();
1164 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1165 <        final FailingBiFunction r = new FailingBiFunction();
1166 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1167 <
1168 <        f.complete(v1);
1169 <        checkIncomplete(h);
1170 <        g.complete(v2);
1164 >        final IncFunction r = new IncFunction(m);
1165 >        if (!createIncomplete) f.complete(v1);
1166 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1167 >        if (createIncomplete) {
1168 >            checkIncomplete(g);
1169 >            f.complete(v1);
1170 >        }
1171  
1172 <        checkCompletedWithWrappedCFException(h);
1172 >        checkCompletedNormally(g, inc(v1));
1173          checkCompletedNormally(f, v1);
1174 <        checkCompletedNormally(g, v2);
1174 >        r.assertInvoked();
1175      }}
1176  
1177 <    public void testThenCombine_actionFailed2() {
1177 >    /**
1178 >     * thenApply result completes exceptionally after exceptional
1179 >     * completion of source
1180 >     */
1181 >    public void testThenApply_exceptionalCompletion() {
1182          for (ExecutionMode m : ExecutionMode.values())
1183 <        for (Integer v1 : new Integer[] { 1, null })
1272 <        for (Integer v2 : new Integer[] { 2, null })
1183 >        for (boolean createIncomplete : new boolean[] { true, false })
1184      {
1185 +        final CFException ex = new CFException();
1186          final CompletableFuture<Integer> f = new CompletableFuture<>();
1187 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1188 <        final FailingBiFunction r = new FailingBiFunction();
1189 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1190 <
1191 <        g.complete(v2);
1192 <        checkIncomplete(h);
1193 <        f.complete(v1);
1187 >        final IncFunction r = new IncFunction(m);
1188 >        if (!createIncomplete) f.completeExceptionally(ex);
1189 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1190 >        if (createIncomplete) {
1191 >            checkIncomplete(g);
1192 >            f.completeExceptionally(ex);
1193 >        }
1194  
1195 <        checkCompletedWithWrappedCFException(h);
1196 <        checkCompletedNormally(f, v1);
1197 <        checkCompletedNormally(g, v2);
1195 >        checkCompletedWithWrappedCFException(g, ex);
1196 >        checkCompletedWithWrappedCFException(f, ex);
1197 >        r.assertNotInvoked();
1198      }}
1199  
1200      /**
1201 <     * thenCombine result completes exceptionally if either source cancelled
1201 >     * thenApply result completes exceptionally if source cancelled
1202       */
1203 <    public void testThenCombine_sourceCancelled1() {
1203 >    public void testThenApply_sourceCancelled() {
1204          for (ExecutionMode m : ExecutionMode.values())
1205 +        for (boolean createIncomplete : new boolean[] { true, false })
1206          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1294        for (Integer v1 : new Integer[] { 1, null })
1207      {
1208          final CompletableFuture<Integer> f = new CompletableFuture<>();
1209 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1210 <        final SubtractFunction r = new SubtractFunction();
1211 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1212 <
1213 <        assertTrue(f.cancel(mayInterruptIfRunning));
1214 <        checkIncomplete(h);
1215 <        g.complete(v1);
1209 >        final IncFunction r = new IncFunction(m);
1210 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1211 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1212 >        if (createIncomplete) {
1213 >            checkIncomplete(g);
1214 >            assertTrue(f.cancel(mayInterruptIfRunning));
1215 >        }
1216  
1217 <        checkCompletedWithWrappedCancellationException(h);
1217 >        checkCompletedWithWrappedCancellationException(g);
1218          checkCancelled(f);
1219 <        assertEquals(0, r.invocationCount);
1308 <        checkCompletedNormally(g, v1);
1219 >        r.assertNotInvoked();
1220      }}
1221  
1222 <    public void testThenCombine_sourceCancelled2() {
1222 >    /**
1223 >     * thenApply result completes exceptionally if action does
1224 >     */
1225 >    public void testThenApply_actionFailed() {
1226          for (ExecutionMode m : ExecutionMode.values())
1227 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1227 >        for (boolean createIncomplete : new boolean[] { true, false })
1228          for (Integer v1 : new Integer[] { 1, null })
1229      {
1230          final CompletableFuture<Integer> f = new CompletableFuture<>();
1231 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1232 <        final SubtractFunction r = new SubtractFunction();
1233 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1234 <
1235 <        assertTrue(g.cancel(mayInterruptIfRunning));
1236 <        checkIncomplete(h);
1237 <        f.complete(v1);
1231 >        final FailingFunction r = new FailingFunction(m);
1232 >        if (!createIncomplete) f.complete(v1);
1233 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1234 >        if (createIncomplete) {
1235 >            checkIncomplete(g);
1236 >            f.complete(v1);
1237 >        }
1238  
1239 <        checkCompletedWithWrappedCancellationException(h);
1326 <        checkCancelled(g);
1327 <        assertEquals(0, r.invocationCount);
1239 >        checkCompletedWithWrappedCFException(g);
1240          checkCompletedNormally(f, v1);
1241      }}
1242  
1243 <    public void testThenCombine_sourceCancelled3() {
1243 >    /**
1244 >     * thenAccept result completes normally after normal completion of source
1245 >     */
1246 >    public void testThenAccept_normalCompletion() {
1247          for (ExecutionMode m : ExecutionMode.values())
1248 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1248 >        for (boolean createIncomplete : new boolean[] { true, false })
1249          for (Integer v1 : new Integer[] { 1, null })
1250      {
1251          final CompletableFuture<Integer> f = new CompletableFuture<>();
1252 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1253 <        final SubtractFunction r = new SubtractFunction();
1254 <
1255 <        assertTrue(g.cancel(mayInterruptIfRunning));
1256 <        f.complete(v1);
1257 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1252 >        final NoopConsumer r = new NoopConsumer(m);
1253 >        if (!createIncomplete) f.complete(v1);
1254 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1255 >        if (createIncomplete) {
1256 >            checkIncomplete(g);
1257 >            f.complete(v1);
1258 >        }
1259  
1260 <        checkCompletedWithWrappedCancellationException(h);
1261 <        checkCancelled(g);
1346 <        assertEquals(0, r.invocationCount);
1260 >        checkCompletedNormally(g, null);
1261 >        r.assertValue(v1);
1262          checkCompletedNormally(f, v1);
1263      }}
1264  
1350    public void testThenCombine_sourceCancelled4() {
1351        for (ExecutionMode m : ExecutionMode.values())
1352        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1353        for (Integer v1 : new Integer[] { 1, null })
1354    {
1355        final CompletableFuture<Integer> f = new CompletableFuture<>();
1356        final CompletableFuture<Integer> g = new CompletableFuture<>();
1357        final SubtractFunction r = new SubtractFunction();
1358
1359        assertTrue(f.cancel(mayInterruptIfRunning));
1360        g.complete(v1);
1361        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1362
1363        checkCompletedWithWrappedCancellationException(h);
1364        checkCancelled(f);
1365        assertEquals(0, r.invocationCount);
1366        checkCompletedNormally(g, v1);
1367    }}
1368
1265      /**
1266 <     * thenAcceptBoth result completes normally after normal
1267 <     * completion of sources
1266 >     * thenAccept result completes exceptionally after exceptional
1267 >     * completion of source
1268       */
1269 <    public void testThenAcceptBoth_normalCompletion1() {
1269 >    public void testThenAccept_exceptionalCompletion() {
1270          for (ExecutionMode m : ExecutionMode.values())
1271 <        for (Integer v1 : new Integer[] { 1, null })
1376 <        for (Integer v2 : new Integer[] { 2, null })
1271 >        for (boolean createIncomplete : new boolean[] { true, false })
1272      {
1273 +        final CFException ex = new CFException();
1274          final CompletableFuture<Integer> f = new CompletableFuture<>();
1275 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1276 <        final SubtractAction r = new SubtractAction();
1277 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1278 <
1279 <        f.complete(v1);
1280 <        checkIncomplete(h);
1281 <        assertEquals(0, r.invocationCount);
1386 <        g.complete(v2);
1275 >        final NoopConsumer r = new NoopConsumer(m);
1276 >        if (!createIncomplete) f.completeExceptionally(ex);
1277 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1278 >        if (createIncomplete) {
1279 >            checkIncomplete(g);
1280 >            f.completeExceptionally(ex);
1281 >        }
1282  
1283 <        checkCompletedNormally(h, null);
1284 <        assertEquals(subtract(v1, v2), r.value);
1285 <        checkCompletedNormally(f, v1);
1391 <        checkCompletedNormally(g, v2);
1283 >        checkCompletedWithWrappedCFException(g, ex);
1284 >        checkCompletedWithWrappedCFException(f, ex);
1285 >        r.assertNotInvoked();
1286      }}
1287  
1288 <    public void testThenAcceptBoth_normalCompletion2() {
1288 >    /**
1289 >     * thenAccept result completes exceptionally if source cancelled
1290 >     */
1291 >    public void testThenAccept_sourceCancelled() {
1292          for (ExecutionMode m : ExecutionMode.values())
1293 <        for (Integer v1 : new Integer[] { 1, null })
1294 <        for (Integer v2 : new Integer[] { 2, null })
1293 >        for (boolean createIncomplete : new boolean[] { true, false })
1294 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1295      {
1296          final CompletableFuture<Integer> f = new CompletableFuture<>();
1297 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1298 <        final SubtractAction r = new SubtractAction();
1299 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1300 <
1301 <        g.complete(v2);
1302 <        checkIncomplete(h);
1303 <        assertEquals(0, r.invocationCount);
1407 <        f.complete(v1);
1297 >        final NoopConsumer r = new NoopConsumer(m);
1298 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1299 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1300 >        if (createIncomplete) {
1301 >            checkIncomplete(g);
1302 >            assertTrue(f.cancel(mayInterruptIfRunning));
1303 >        }
1304  
1305 <        checkCompletedNormally(h, null);
1306 <        assertEquals(subtract(v1, v2), r.value);
1307 <        checkCompletedNormally(f, v1);
1412 <        checkCompletedNormally(g, v2);
1305 >        checkCompletedWithWrappedCancellationException(g);
1306 >        checkCancelled(f);
1307 >        r.assertNotInvoked();
1308      }}
1309  
1310 <    public void testThenAcceptBoth_normalCompletion3() {
1310 >    /**
1311 >     * thenAccept result completes exceptionally if action does
1312 >     */
1313 >    public void testThenAccept_actionFailed() {
1314          for (ExecutionMode m : ExecutionMode.values())
1315 +        for (boolean createIncomplete : new boolean[] { true, false })
1316          for (Integer v1 : new Integer[] { 1, null })
1418        for (Integer v2 : new Integer[] { 2, null })
1317      {
1318          final CompletableFuture<Integer> f = new CompletableFuture<>();
1319 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1320 <        final SubtractAction r = new SubtractAction();
1321 <
1322 <        g.complete(v2);
1323 <        f.complete(v1);
1324 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1319 >        final FailingConsumer r = new FailingConsumer(m);
1320 >        if (!createIncomplete) f.complete(v1);
1321 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1322 >        if (createIncomplete) {
1323 >            checkIncomplete(g);
1324 >            f.complete(v1);
1325 >        }
1326  
1327 <        checkCompletedNormally(h, null);
1429 <        assertEquals(subtract(v1, v2), r.value);
1327 >        checkCompletedWithWrappedCFException(g);
1328          checkCompletedNormally(f, v1);
1431        checkCompletedNormally(g, v2);
1329      }}
1330  
1331 <    public void testThenAcceptBoth_normalCompletion4() {
1331 >    /**
1332 >     * thenCombine result completes normally after normal completion
1333 >     * of sources
1334 >     */
1335 >    public void testThenCombine_normalCompletion() {
1336          for (ExecutionMode m : ExecutionMode.values())
1337 +        for (boolean createIncomplete : new boolean[] { true, false })
1338 +        for (boolean fFirst : new boolean[] { true, false })
1339          for (Integer v1 : new Integer[] { 1, null })
1340          for (Integer v2 : new Integer[] { 2, null })
1341      {
1342          final CompletableFuture<Integer> f = new CompletableFuture<>();
1343          final CompletableFuture<Integer> g = new CompletableFuture<>();
1344 <        final SubtractAction r = new SubtractAction();
1344 >        final SubtractFunction r = new SubtractFunction(m);
1345  
1346 <        f.complete(v1);
1347 <        g.complete(v2);
1348 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1346 >        if (fFirst) f.complete(v1); else g.complete(v2);
1347 >        if (!createIncomplete)
1348 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1349 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1350 >        if (createIncomplete) {
1351 >            checkIncomplete(h);
1352 >            r.assertNotInvoked();
1353 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1354 >        }
1355  
1356 <        checkCompletedNormally(h, null);
1448 <        assertEquals(subtract(v1, v2), r.value);
1356 >        checkCompletedNormally(h, subtract(v1, v2));
1357          checkCompletedNormally(f, v1);
1358          checkCompletedNormally(g, v2);
1359 +        r.assertInvoked();
1360      }}
1361  
1362      /**
1363 <     * thenAcceptBoth result completes exceptionally after exceptional
1363 >     * thenCombine result completes exceptionally after exceptional
1364       * completion of either source
1365       */
1366 <    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() {
1366 >    public void testThenCombine_exceptionalCompletion() {
1367          for (ExecutionMode m : ExecutionMode.values())
1368 +        for (boolean createIncomplete : new boolean[] { true, false })
1369 +        for (boolean fFirst : new boolean[] { true, false })
1370          for (Integer v1 : new Integer[] { 1, null })
1371      {
1372          final CompletableFuture<Integer> f = new CompletableFuture<>();
1373          final CompletableFuture<Integer> g = new CompletableFuture<>();
1503        final SubtractAction r = new SubtractAction();
1374          final CFException ex = new CFException();
1375 +        final SubtractFunction r = new SubtractFunction(m);
1376  
1377 <        g.completeExceptionally(ex);
1378 <        f.complete(v1);
1379 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1377 >        (fFirst ? f : g).complete(v1);
1378 >        if (!createIncomplete)
1379 >            (!fFirst ? f : g).completeExceptionally(ex);
1380 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1381 >        if (createIncomplete) {
1382 >            checkIncomplete(h);
1383 >            (!fFirst ? f : g).completeExceptionally(ex);
1384 >        }
1385  
1386          checkCompletedWithWrappedCFException(h, ex);
1387 <        checkCompletedWithWrappedCFException(g, ex);
1388 <        assertEquals(0, r.invocationCount);
1389 <        checkCompletedNormally(f, v1);
1387 >        r.assertNotInvoked();
1388 >        checkCompletedNormally(fFirst ? f : g, v1);
1389 >        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1390      }}
1391  
1392 <    public void testThenAcceptBoth_exceptionalCompletion4() {
1392 >    /**
1393 >     * thenCombine result completes exceptionally if either source cancelled
1394 >     */
1395 >    public void testThenCombine_sourceCancelled() {
1396          for (ExecutionMode m : ExecutionMode.values())
1397 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1398 +        for (boolean createIncomplete : new boolean[] { true, false })
1399 +        for (boolean fFirst : new boolean[] { true, false })
1400          for (Integer v1 : new Integer[] { 1, null })
1401      {
1402          final CompletableFuture<Integer> f = new CompletableFuture<>();
1403          final CompletableFuture<Integer> g = new CompletableFuture<>();
1404 <        final SubtractAction r = new SubtractAction();
1523 <        final CFException ex = new CFException();
1404 >        final SubtractFunction r = new SubtractFunction(m);
1405  
1406 <        f.completeExceptionally(ex);
1407 <        g.complete(v1);
1408 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1406 >        (fFirst ? f : g).complete(v1);
1407 >        if (!createIncomplete)
1408 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1409 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1410 >        if (createIncomplete) {
1411 >            checkIncomplete(h);
1412 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1413 >        }
1414  
1415 <        checkCompletedWithWrappedCFException(h, ex);
1416 <        checkCompletedWithWrappedCFException(f, ex);
1417 <        assertEquals(0, r.invocationCount);
1418 <        checkCompletedNormally(g, v1);
1415 >        checkCompletedWithWrappedCancellationException(h);
1416 >        checkCancelled(!fFirst ? f : g);
1417 >        r.assertNotInvoked();
1418 >        checkCompletedNormally(fFirst ? f : g, v1);
1419      }}
1420  
1421      /**
1422 <     * thenAcceptBoth result completes exceptionally if action does
1422 >     * thenCombine result completes exceptionally if action does
1423       */
1424 <    public void testThenAcceptBoth_actionFailed1() {
1424 >    public void testThenCombine_actionFailed() {
1425          for (ExecutionMode m : ExecutionMode.values())
1426 +        for (boolean fFirst : new boolean[] { true, false })
1427          for (Integer v1 : new Integer[] { 1, null })
1428          for (Integer v2 : new Integer[] { 2, null })
1429      {
1430          final CompletableFuture<Integer> f = new CompletableFuture<>();
1431          final CompletableFuture<Integer> g = new CompletableFuture<>();
1432 <        final FailingBiConsumer r = new FailingBiConsumer();
1433 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1432 >        final FailingBiFunction r = new FailingBiFunction(m);
1433 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1434  
1435 <        f.complete(v1);
1436 <        checkIncomplete(h);
1437 <        g.complete(v2);
1435 >        if (fFirst) {
1436 >            f.complete(v1);
1437 >            g.complete(v2);
1438 >        } else {
1439 >            g.complete(v2);
1440 >            f.complete(v1);
1441 >        }
1442  
1443          checkCompletedWithWrappedCFException(h);
1444          checkCompletedNormally(f, v1);
1445          checkCompletedNormally(g, v2);
1446      }}
1447  
1448 <    public void testThenAcceptBoth_actionFailed2() {
1448 >    /**
1449 >     * thenAcceptBoth result completes normally after normal
1450 >     * completion of sources
1451 >     */
1452 >    public void testThenAcceptBoth_normalCompletion() {
1453          for (ExecutionMode m : ExecutionMode.values())
1454 +        for (boolean createIncomplete : new boolean[] { true, false })
1455 +        for (boolean fFirst : new boolean[] { true, false })
1456          for (Integer v1 : new Integer[] { 1, null })
1457          for (Integer v2 : new Integer[] { 2, null })
1458      {
1459          final CompletableFuture<Integer> f = new CompletableFuture<>();
1460          final CompletableFuture<Integer> g = new CompletableFuture<>();
1461 <        final FailingBiConsumer r = new FailingBiConsumer();
1565 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1461 >        final SubtractAction r = new SubtractAction(m);
1462  
1463 <        g.complete(v2);
1464 <        checkIncomplete(h);
1465 <        f.complete(v1);
1463 >        if (fFirst) f.complete(v1); else g.complete(v2);
1464 >        if (!createIncomplete)
1465 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1466 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1467 >        if (createIncomplete) {
1468 >            checkIncomplete(h);
1469 >            r.assertNotInvoked();
1470 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1471 >        }
1472  
1473 <        checkCompletedWithWrappedCFException(h);
1473 >        checkCompletedNormally(h, null);
1474 >        r.assertValue(subtract(v1, v2));
1475          checkCompletedNormally(f, v1);
1476          checkCompletedNormally(g, v2);
1477      }}
1478  
1479      /**
1480 <     * thenAcceptBoth result completes exceptionally if either source cancelled
1480 >     * thenAcceptBoth result completes exceptionally after exceptional
1481 >     * completion of either source
1482       */
1483 <    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() {
1483 >    public void testThenAcceptBoth_exceptionalCompletion() {
1484          for (ExecutionMode m : ExecutionMode.values())
1485 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1486 <        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 })
1485 >        for (boolean createIncomplete : new boolean[] { true, false })
1486 >        for (boolean fFirst : new boolean[] { true, false })
1487          for (Integer v1 : new Integer[] { 1, null })
1488      {
1489          final CompletableFuture<Integer> f = new CompletableFuture<>();
1490          final CompletableFuture<Integer> g = new CompletableFuture<>();
1491 <        final SubtractAction r = new SubtractAction();
1491 >        final CFException ex = new CFException();
1492 >        final SubtractAction r = new SubtractAction(m);
1493  
1494 <        assertTrue(g.cancel(mayInterruptIfRunning));
1495 <        f.complete(v1);
1494 >        (fFirst ? f : g).complete(v1);
1495 >        if (!createIncomplete)
1496 >            (!fFirst ? f : g).completeExceptionally(ex);
1497          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1498 +        if (createIncomplete) {
1499 +            checkIncomplete(h);
1500 +            (!fFirst ? f : g).completeExceptionally(ex);
1501 +        }
1502  
1503 <        checkCompletedWithWrappedCancellationException(h);
1504 <        checkCancelled(g);
1505 <        assertEquals(0, r.invocationCount);
1506 <        checkCompletedNormally(f, v1);
1503 >        checkCompletedWithWrappedCFException(h, ex);
1504 >        r.assertNotInvoked();
1505 >        checkCompletedNormally(fFirst ? f : g, v1);
1506 >        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1507      }}
1508  
1509 <    public void testThenAcceptBoth_sourceCancelled4() {
1509 >    /**
1510 >     * thenAcceptBoth result completes exceptionally if either source cancelled
1511 >     */
1512 >    public void testThenAcceptBoth_sourceCancelled() {
1513          for (ExecutionMode m : ExecutionMode.values())
1514          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1515 +        for (boolean createIncomplete : new boolean[] { true, false })
1516 +        for (boolean fFirst : new boolean[] { true, false })
1517          for (Integer v1 : new Integer[] { 1, null })
1518      {
1519          final CompletableFuture<Integer> f = new CompletableFuture<>();
1520          final CompletableFuture<Integer> g = new CompletableFuture<>();
1521 <        final SubtractAction r = new SubtractAction();
1521 >        final SubtractAction r = new SubtractAction(m);
1522  
1523 <        assertTrue(f.cancel(mayInterruptIfRunning));
1524 <        g.complete(v1);
1523 >        (fFirst ? f : g).complete(v1);
1524 >        if (!createIncomplete)
1525 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1526          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1527 +        if (createIncomplete) {
1528 +            checkIncomplete(h);
1529 +            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1530 +        }
1531  
1532          checkCompletedWithWrappedCancellationException(h);
1533 <        checkCancelled(f);
1534 <        assertEquals(0, r.invocationCount);
1535 <        checkCompletedNormally(g, v1);
1533 >        checkCancelled(!fFirst ? f : g);
1534 >        r.assertNotInvoked();
1535 >        checkCompletedNormally(fFirst ? f : g, v1);
1536      }}
1537  
1538      /**
1539 <     * runAfterBoth result completes normally after normal
1659 <     * completion of sources
1539 >     * thenAcceptBoth result completes exceptionally if action does
1540       */
1541 <    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() {
1541 >    public void testThenAcceptBoth_actionFailed() {
1542          for (ExecutionMode m : ExecutionMode.values())
1543 +        for (boolean fFirst : new boolean[] { true, false })
1544          for (Integer v1 : new Integer[] { 1, null })
1545          for (Integer v2 : new Integer[] { 2, null })
1546      {
1547          final CompletableFuture<Integer> f = new CompletableFuture<>();
1548          final CompletableFuture<Integer> g = new CompletableFuture<>();
1549 <        final Noop r = new Noop();
1549 >        final FailingBiConsumer r = new FailingBiConsumer(m);
1550 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1551  
1552 <        g.complete(v2);
1553 <        f.complete(v1);
1554 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1552 >        if (fFirst) {
1553 >            f.complete(v1);
1554 >            g.complete(v2);
1555 >        } else {
1556 >            g.complete(v2);
1557 >            f.complete(v1);
1558 >        }
1559  
1560 <        checkCompletedNormally(h, null);
1717 <        assertEquals(1, r.invocationCount);
1560 >        checkCompletedWithWrappedCFException(h);
1561          checkCompletedNormally(f, v1);
1562          checkCompletedNormally(g, v2);
1563      }}
1564  
1565 <    public void testRunAfterBoth_normalCompletion4() {
1565 >    /**
1566 >     * runAfterBoth result completes normally after normal
1567 >     * completion of sources
1568 >     */
1569 >    public void testRunAfterBoth_normalCompletion() {
1570          for (ExecutionMode m : ExecutionMode.values())
1571 +        for (boolean createIncomplete : new boolean[] { true, false })
1572 +        for (boolean fFirst : new boolean[] { true, false })
1573          for (Integer v1 : new Integer[] { 1, null })
1574          for (Integer v2 : new Integer[] { 2, null })
1575      {
1576          final CompletableFuture<Integer> f = new CompletableFuture<>();
1577          final CompletableFuture<Integer> g = new CompletableFuture<>();
1578 <        final Noop r = new Noop();
1578 >        final Noop r = new Noop(m);
1579  
1580 <        f.complete(v1);
1581 <        g.complete(v2);
1580 >        if (fFirst) f.complete(v1); else g.complete(v2);
1581 >        if (!createIncomplete)
1582 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1583          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1584 +        if (createIncomplete) {
1585 +            checkIncomplete(h);
1586 +            r.assertNotInvoked();
1587 +            if (!fFirst) f.complete(v1); else g.complete(v2);
1588 +        }
1589  
1590          checkCompletedNormally(h, null);
1591 <        assertEquals(1, r.invocationCount);
1591 >        r.assertInvoked();
1592          checkCompletedNormally(f, v1);
1593          checkCompletedNormally(g, v2);
1594      }}
# Line 1742 | Line 1597 | public class CompletableFutureTest exten
1597       * runAfterBoth result completes exceptionally after exceptional
1598       * completion of either source
1599       */
1600 <    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() {
1600 >    public void testRunAfterBoth_exceptionalCompletion() {
1601          for (ExecutionMode m : ExecutionMode.values())
1602 +        for (boolean createIncomplete : new boolean[] { true, false })
1603 +        for (boolean fFirst : new boolean[] { true, false })
1604          for (Integer v1 : new Integer[] { 1, null })
1605      {
1606          final CompletableFuture<Integer> f = new CompletableFuture<>();
1607          final CompletableFuture<Integer> g = new CompletableFuture<>();
1791        final Noop r = new Noop();
1608          final CFException ex = new CFException();
1609 +        final Noop r = new Noop(m);
1610  
1611 <        g.completeExceptionally(ex);
1612 <        f.complete(v1);
1611 >        (fFirst ? f : g).complete(v1);
1612 >        if (!createIncomplete)
1613 >            (!fFirst ? f : g).completeExceptionally(ex);
1614          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1615 +        if (createIncomplete) {
1616 +            checkIncomplete(h);
1617 +            (!fFirst ? f : g).completeExceptionally(ex);
1618 +        }
1619  
1620          checkCompletedWithWrappedCFException(h, ex);
1621 <        checkCompletedWithWrappedCFException(g, ex);
1622 <        assertEquals(0, r.invocationCount);
1623 <        checkCompletedNormally(f, v1);
1802 <    }}
1803 <
1804 <    public void testRunAfterBoth_exceptionalCompletion4() {
1805 <        for (ExecutionMode m : ExecutionMode.values())
1806 <        for (Integer v1 : new Integer[] { 1, null })
1807 <    {
1808 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1809 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1810 <        final Noop r = new Noop();
1811 <        final CFException ex = new CFException();
1812 <
1813 <        f.completeExceptionally(ex);
1814 <        g.complete(v1);
1815 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1816 <
1817 <        checkCompletedWithWrappedCFException(h, ex);
1818 <        checkCompletedWithWrappedCFException(f, ex);
1819 <        assertEquals(0, r.invocationCount);
1820 <        checkCompletedNormally(g, v1);
1821 <    }}
1822 <
1823 <    /**
1824 <     * runAfterBoth result completes exceptionally if action does
1825 <     */
1826 <    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() {
1846 <        for (ExecutionMode m : ExecutionMode.values())
1847 <        for (Integer v1 : new Integer[] { 1, null })
1848 <        for (Integer v2 : new Integer[] { 2, null })
1849 <    {
1850 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1851 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1852 <        final FailingNoop r = new FailingNoop();
1853 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1854 <
1855 <        g.complete(v2);
1856 <        checkIncomplete(h);
1857 <        f.complete(v1);
1858 <
1859 <        checkCompletedWithWrappedCFException(h);
1860 <        checkCompletedNormally(f, v1);
1861 <        checkCompletedNormally(g, v2);
1621 >        r.assertNotInvoked();
1622 >        checkCompletedNormally(fFirst ? f : g, v1);
1623 >        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1624      }}
1625  
1626      /**
1627       * runAfterBoth result completes exceptionally if either source cancelled
1628       */
1629 <    public void testRunAfterBoth_sourceCancelled1() {
1629 >    public void testRunAfterBoth_sourceCancelled() {
1630          for (ExecutionMode m : ExecutionMode.values())
1631          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1632 +        for (boolean createIncomplete : new boolean[] { true, false })
1633 +        for (boolean fFirst : new boolean[] { true, false })
1634          for (Integer v1 : new Integer[] { 1, null })
1635      {
1636          final CompletableFuture<Integer> f = new CompletableFuture<>();
1637          final CompletableFuture<Integer> g = new CompletableFuture<>();
1638 <        final Noop r = new Noop();
1875 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1638 >        final Noop r = new Noop(m);
1639  
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    }}
1640  
1641 <    public void testRunAfterBoth_sourceCancelled2() {
1642 <        for (ExecutionMode m : ExecutionMode.values())
1643 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1890 <        for (Integer v1 : new Integer[] { 1, null })
1891 <    {
1892 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1893 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1894 <        final Noop r = new Noop();
1641 >        (fFirst ? f : g).complete(v1);
1642 >        if (!createIncomplete)
1643 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1644          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1645 <
1646 <        assertTrue(g.cancel(mayInterruptIfRunning));
1647 <        checkIncomplete(h);
1648 <        f.complete(v1);
1645 >        if (createIncomplete) {
1646 >            checkIncomplete(h);
1647 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1648 >        }
1649  
1650          checkCompletedWithWrappedCancellationException(h);
1651 <        checkCancelled(g);
1652 <        assertEquals(0, r.invocationCount);
1653 <        checkCompletedNormally(f, v1);
1651 >        checkCancelled(!fFirst ? f : g);
1652 >        r.assertNotInvoked();
1653 >        checkCompletedNormally(fFirst ? f : g, v1);
1654      }}
1655  
1656 <    public void testRunAfterBoth_sourceCancelled3() {
1656 >    /**
1657 >     * runAfterBoth result completes exceptionally if action does
1658 >     */
1659 >    public void testRunAfterBoth_actionFailed() {
1660          for (ExecutionMode m : ExecutionMode.values())
1661 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1661 >        for (boolean fFirst : new boolean[] { true, false })
1662          for (Integer v1 : new Integer[] { 1, null })
1663 +        for (Integer v2 : new Integer[] { 2, null })
1664      {
1665          final CompletableFuture<Integer> f = new CompletableFuture<>();
1666          final CompletableFuture<Integer> g = new CompletableFuture<>();
1667 <        final Noop r = new Noop();
1667 >        final FailingRunnable r1 = new FailingRunnable(m);
1668 >        final FailingRunnable r2 = new FailingRunnable(m);
1669  
1670 <        assertTrue(g.cancel(mayInterruptIfRunning));
1671 <        f.complete(v1);
1672 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1670 >        CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1671 >        if (fFirst) {
1672 >            f.complete(v1);
1673 >            g.complete(v2);
1674 >        } else {
1675 >            g.complete(v2);
1676 >            f.complete(v1);
1677 >        }
1678 >        CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1679  
1680 <        checkCompletedWithWrappedCancellationException(h);
1681 <        checkCancelled(g);
1922 <        assertEquals(0, r.invocationCount);
1680 >        checkCompletedWithWrappedCFException(h1);
1681 >        checkCompletedWithWrappedCFException(h2);
1682          checkCompletedNormally(f, v1);
1683 <    }}
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);
1937 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1938 <
1939 <        checkCompletedWithWrappedCancellationException(h);
1940 <        checkCancelled(f);
1941 <        assertEquals(0, r.invocationCount);
1942 <        checkCompletedNormally(g, v1);
1683 >        checkCompletedNormally(g, v2);
1684      }}
1685  
1686      /**
1687       * applyToEither result completes normally after normal completion
1688       * of either source
1689       */
1690 <    public void testApplyToEither_normalCompletion1() {
1690 >    public void testApplyToEither_normalCompletion() {
1691          for (ExecutionMode m : ExecutionMode.values())
1692          for (Integer v1 : new Integer[] { 1, null })
1693          for (Integer v2 : new Integer[] { 2, null })
1694      {
1695          final CompletableFuture<Integer> f = new CompletableFuture<>();
1696          final CompletableFuture<Integer> g = new CompletableFuture<>();
1697 <        final IncFunction r = new IncFunction();
1698 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1697 >        final IncFunction[] rs = new IncFunction[6];
1698 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1699  
1700 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1701 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1702 +        checkIncomplete(h0);
1703 +        checkIncomplete(h1);
1704 +        rs[0].assertNotInvoked();
1705 +        rs[1].assertNotInvoked();
1706          f.complete(v1);
1707 <        checkCompletedNormally(h, inc(v1));
1708 <        g.complete(v2);
1709 <
1710 <        checkCompletedNormally(f, v1);
1711 <        checkCompletedNormally(g, v2);
1712 <        checkCompletedNormally(h, inc(v1));
1966 <    }}
1967 <
1968 <    public void testApplyToEither_normalCompletion2() {
1969 <        for (ExecutionMode m : ExecutionMode.values())
1970 <        for (Integer v1 : new Integer[] { 1, null })
1971 <        for (Integer v2 : new Integer[] { 2, null })
1972 <    {
1973 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1974 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1975 <        final IncFunction r = new IncFunction();
1976 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1977 <
1707 >        checkCompletedNormally(h0, inc(v1));
1708 >        checkCompletedNormally(h1, inc(v1));
1709 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1710 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1711 >        checkCompletedNormally(h2, inc(v1));
1712 >        checkCompletedNormally(h3, inc(v1));
1713          g.complete(v2);
1979        checkCompletedNormally(h, inc(v2));
1980        f.complete(v1);
1981
1982        checkCompletedNormally(f, v1);
1983        checkCompletedNormally(g, v2);
1984        checkCompletedNormally(h, inc(v2));
1985        }}
1714  
1715 <    public void testApplyToEither_normalCompletion3() {
1716 <        for (ExecutionMode m : ExecutionMode.values())
1717 <        for (Integer v1 : new Integer[] { 1, null })
1718 <        for (Integer v2 : new Integer[] { 2, null })
1719 <    {
1720 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1721 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1722 <        final IncFunction r = new IncFunction();
1723 <
1996 <        f.complete(v1);
1997 <        g.complete(v2);
1998 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1715 >        // unspecified behavior - both source completions available
1716 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
1717 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
1718 >        rs[4].assertValue(h4.join());
1719 >        rs[5].assertValue(h5.join());
1720 >        assertTrue(Objects.equals(inc(v1), h4.join()) ||
1721 >                   Objects.equals(inc(v2), h4.join()));
1722 >        assertTrue(Objects.equals(inc(v1), h5.join()) ||
1723 >                   Objects.equals(inc(v2), h5.join()));
1724  
1725          checkCompletedNormally(f, v1);
1726          checkCompletedNormally(g, v2);
1727 <
1728 <        // unspecified behavior
1729 <        assertTrue(Objects.equals(h.join(), inc(v1)) ||
1730 <                   Objects.equals(h.join(), inc(v2)));
1731 <        assertEquals(1, r.invocationCount);
1727 >        checkCompletedNormally(h0, inc(v1));
1728 >        checkCompletedNormally(h1, inc(v1));
1729 >        checkCompletedNormally(h2, inc(v1));
1730 >        checkCompletedNormally(h3, inc(v1));
1731 >        for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1));
1732      }}
1733  
1734      /**
1735       * applyToEither result completes exceptionally after exceptional
1736       * completion of either source
1737       */
1738 <    public void testApplyToEither_exceptionalCompletion1() {
1738 >    public void testApplyToEither_exceptionalCompletion() {
1739          for (ExecutionMode m : ExecutionMode.values())
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[] rs = new IncFunction[6];
1746 +        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1747  
1748 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1749 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1750 +        checkIncomplete(h0);
1751 +        checkIncomplete(h1);
1752 +        rs[0].assertNotInvoked();
1753 +        rs[1].assertNotInvoked();
1754          f.completeExceptionally(ex);
1755 <        checkCompletedWithWrappedCFException(h, ex);
1755 >        checkCompletedWithWrappedCFException(h0, ex);
1756 >        checkCompletedWithWrappedCFException(h1, ex);
1757 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1758 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1759 >        checkCompletedWithWrappedCFException(h2, ex);
1760 >        checkCompletedWithWrappedCFException(h3, ex);
1761          g.complete(v1);
1762  
1763 <        assertEquals(0, r.invocationCount);
1764 <        checkCompletedNormally(g, v1);
1765 <        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();
2040 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2041 <        final CFException ex = new CFException();
2042 <
2043 <        g.completeExceptionally(ex);
2044 <        checkCompletedWithWrappedCFException(h, ex);
2045 <        f.complete(v1);
2046 <
2047 <        assertEquals(0, r.invocationCount);
2048 <        checkCompletedNormally(f, v1);
2049 <        checkCompletedWithWrappedCFException(g, ex);
2050 <        checkCompletedWithWrappedCFException(h, ex);
2051 <    }}
2052 <
2053 <    public void testApplyToEither_exceptionalCompletion3() {
2054 <        for (ExecutionMode m : ExecutionMode.values())
2055 <        for (Integer v1 : new Integer[] { 1, null })
2056 <    {
2057 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2058 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2059 <        final IncFunction r = new IncFunction();
2060 <        final CFException ex = new CFException();
2061 <
2062 <        g.completeExceptionally(ex);
2063 <        f.complete(v1);
2064 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2065 <
2066 <        // unspecified behavior
2067 <        Integer v;
1763 >        // unspecified behavior - both source completions available
1764 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
1765 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
1766          try {
1767 <            assertEquals(inc(v1), h.join());
1768 <            assertEquals(1, r.invocationCount);
1767 >            assertEquals(inc(v1), h4.join());
1768 >            rs[4].assertValue(inc(v1));
1769          } catch (CompletionException ok) {
1770 <            checkCompletedWithWrappedCFException(h, ex);
1771 <            assertEquals(0, r.invocationCount);
1770 >            checkCompletedWithWrappedCFException(h4, ex);
1771 >            rs[4].assertNotInvoked();
1772          }
2075
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;
1773          try {
1774 <            assertEquals(inc(v1), h.join());
1775 <            assertEquals(1, r.invocationCount);
1774 >            assertEquals(inc(v1), h5.join());
1775 >            rs[5].assertValue(inc(v1));
1776          } catch (CompletionException ok) {
1777 <            checkCompletedWithWrappedCFException(h, ex);
1778 <            assertEquals(0, r.invocationCount);
1777 >            checkCompletedWithWrappedCFException(h5, ex);
1778 >            rs[5].assertNotInvoked();
1779          }
1780  
1781          checkCompletedWithWrappedCFException(f, ex);
1782          checkCompletedNormally(g, v1);
1783 +        checkCompletedWithWrappedCFException(h0, ex);
1784 +        checkCompletedWithWrappedCFException(h1, ex);
1785 +        checkCompletedWithWrappedCFException(h2, ex);
1786 +        checkCompletedWithWrappedCFException(h3, ex);
1787 +        checkCompletedWithWrappedCFException(h4, ex);
1788 +        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
1789      }}
1790  
1791 <    /**
2108 <     * applyToEither result completes exceptionally if action does
2109 <     */
2110 <    public void testApplyToEither_actionFailed1() {
1791 >    public void testApplyToEither_exceptionalCompletion2() {
1792          for (ExecutionMode m : ExecutionMode.values())
1793 +        for (boolean fFirst : new boolean[] { true, false })
1794          for (Integer v1 : new Integer[] { 1, null })
2113        for (Integer v2 : new Integer[] { 2, null })
1795      {
1796          final CompletableFuture<Integer> f = new CompletableFuture<>();
1797          final CompletableFuture<Integer> g = new CompletableFuture<>();
1798 <        final FailingFunction r = new FailingFunction();
1799 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1798 >        final CFException ex = new CFException();
1799 >        final IncFunction[] rs = new IncFunction[6];
1800 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1801  
1802 <        f.complete(v1);
1803 <        checkCompletedWithWrappedCFException(h);
1804 <        g.complete(v2);
1805 <        checkCompletedNormally(f, v1);
1806 <        checkCompletedNormally(g, v2);
1807 <    }}
1802 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1803 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1804 >        if (fFirst) {
1805 >            f.complete(v1);
1806 >            g.completeExceptionally(ex);
1807 >        } else {
1808 >            g.completeExceptionally(ex);
1809 >            f.complete(v1);
1810 >        }
1811 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1812 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1813  
1814 <    public void testApplyToEither_actionFailed2() {
1815 <        for (ExecutionMode m : ExecutionMode.values())
1816 <        for (Integer v1 : new Integer[] { 1, null })
1817 <        for (Integer v2 : new Integer[] { 2, null })
1818 <    {
1819 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1820 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1821 <        final FailingFunction r = new FailingFunction();
1822 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1814 >        // unspecified behavior - both source completions available
1815 >        try {
1816 >            assertEquals(inc(v1), h0.join());
1817 >            rs[0].assertValue(inc(v1));
1818 >        } catch (CompletionException ok) {
1819 >            checkCompletedWithWrappedCFException(h0, ex);
1820 >            rs[0].assertNotInvoked();
1821 >        }
1822 >        try {
1823 >            assertEquals(inc(v1), h1.join());
1824 >            rs[1].assertValue(inc(v1));
1825 >        } catch (CompletionException ok) {
1826 >            checkCompletedWithWrappedCFException(h1, ex);
1827 >            rs[1].assertNotInvoked();
1828 >        }
1829 >        try {
1830 >            assertEquals(inc(v1), h2.join());
1831 >            rs[2].assertValue(inc(v1));
1832 >        } catch (CompletionException ok) {
1833 >            checkCompletedWithWrappedCFException(h2, ex);
1834 >            rs[2].assertNotInvoked();
1835 >        }
1836 >        try {
1837 >            assertEquals(inc(v1), h3.join());
1838 >            rs[3].assertValue(inc(v1));
1839 >        } catch (CompletionException ok) {
1840 >            checkCompletedWithWrappedCFException(h3, ex);
1841 >            rs[3].assertNotInvoked();
1842 >        }
1843  
2137        g.complete(v2);
2138        checkCompletedWithWrappedCFException(h);
2139        f.complete(v1);
1844          checkCompletedNormally(f, v1);
1845 <        checkCompletedNormally(g, v2);
1845 >        checkCompletedWithWrappedCFException(g, ex);
1846      }}
1847  
1848      /**
1849       * applyToEither result completes exceptionally if either source cancelled
1850       */
1851 <    public void testApplyToEither_sourceCancelled1() {
1851 >    public void testApplyToEither_sourceCancelled() {
1852          for (ExecutionMode m : ExecutionMode.values())
1853          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1854          for (Integer v1 : new Integer[] { 1, null })
1855      {
1856          final CompletableFuture<Integer> f = new CompletableFuture<>();
1857          final CompletableFuture<Integer> g = new CompletableFuture<>();
1858 <        final IncFunction r = new IncFunction();
1859 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1858 >        final IncFunction[] rs = new IncFunction[6];
1859 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1860  
1861 <        assertTrue(f.cancel(mayInterruptIfRunning));
1862 <        checkCompletedWithWrappedCancellationException(h);
1861 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1862 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1863 >        checkIncomplete(h0);
1864 >        checkIncomplete(h1);
1865 >        rs[0].assertNotInvoked();
1866 >        rs[1].assertNotInvoked();
1867 >        f.cancel(mayInterruptIfRunning);
1868 >        checkCompletedWithWrappedCancellationException(h0);
1869 >        checkCompletedWithWrappedCancellationException(h1);
1870 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1871 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1872 >        checkCompletedWithWrappedCancellationException(h2);
1873 >        checkCompletedWithWrappedCancellationException(h3);
1874          g.complete(v1);
1875  
1876 +        // unspecified behavior - both source completions available
1877 +        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
1878 +        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
1879 +        try {
1880 +            assertEquals(inc(v1), h4.join());
1881 +            rs[4].assertValue(inc(v1));
1882 +        } catch (CompletionException ok) {
1883 +            checkCompletedWithWrappedCancellationException(h4);
1884 +            rs[4].assertNotInvoked();
1885 +        }
1886 +        try {
1887 +            assertEquals(inc(v1), h5.join());
1888 +            rs[5].assertValue(inc(v1));
1889 +        } catch (CompletionException ok) {
1890 +            checkCompletedWithWrappedCancellationException(h5);
1891 +            rs[5].assertNotInvoked();
1892 +        }
1893 +
1894          checkCancelled(f);
2162        assertEquals(0, r.invocationCount);
1895          checkCompletedNormally(g, v1);
1896 <        checkCompletedWithWrappedCancellationException(h);
1896 >        checkCompletedWithWrappedCancellationException(h0);
1897 >        checkCompletedWithWrappedCancellationException(h1);
1898 >        checkCompletedWithWrappedCancellationException(h2);
1899 >        checkCompletedWithWrappedCancellationException(h3);
1900 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
1901      }}
1902  
1903      public void testApplyToEither_sourceCancelled2() {
1904          for (ExecutionMode m : ExecutionMode.values())
1905          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1906 +        for (boolean fFirst : new boolean[] { true, false })
1907          for (Integer v1 : new Integer[] { 1, null })
1908      {
1909          final CompletableFuture<Integer> f = new CompletableFuture<>();
1910          final CompletableFuture<Integer> g = new CompletableFuture<>();
1911 <        final IncFunction r = new IncFunction();
1912 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2176 <
2177 <        assertTrue(g.cancel(mayInterruptIfRunning));
2178 <        checkCompletedWithWrappedCancellationException(h);
2179 <        f.complete(v1);
2180 <
2181 <        checkCancelled(g);
2182 <        assertEquals(0, r.invocationCount);
2183 <        checkCompletedNormally(f, v1);
2184 <        checkCompletedWithWrappedCancellationException(h);
2185 <    }}
2186 <
2187 <    public void testApplyToEither_sourceCancelled3() {
2188 <        for (ExecutionMode m : ExecutionMode.values())
2189 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2190 <        for (Integer v1 : new Integer[] { 1, null })
2191 <    {
2192 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2193 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2194 <        final IncFunction r = new IncFunction();
1911 >        final IncFunction[] rs = new IncFunction[6];
1912 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1913  
1914 <        assertTrue(g.cancel(mayInterruptIfRunning));
1915 <        f.complete(v1);
1916 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1914 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1915 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1916 >        if (fFirst) {
1917 >            f.complete(v1);
1918 >            g.cancel(mayInterruptIfRunning);
1919 >        } else {
1920 >            g.cancel(mayInterruptIfRunning);
1921 >            f.complete(v1);
1922 >        }
1923 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1924 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1925  
1926 <        // unspecified behavior
2201 <        Integer v;
1926 >        // unspecified behavior - both source completions available
1927          try {
1928 <            assertEquals(inc(v1), h.join());
1929 <            assertEquals(1, r.invocationCount);
1928 >            assertEquals(inc(v1), h0.join());
1929 >            rs[0].assertValue(inc(v1));
1930          } catch (CompletionException ok) {
1931 <            checkCompletedWithWrappedCancellationException(h);
1932 <            assertEquals(0, r.invocationCount);
1931 >            checkCompletedWithWrappedCancellationException(h0);
1932 >            rs[0].assertNotInvoked();
1933 >        }
1934 >        try {
1935 >            assertEquals(inc(v1), h1.join());
1936 >            rs[1].assertValue(inc(v1));
1937 >        } catch (CompletionException ok) {
1938 >            checkCompletedWithWrappedCancellationException(h1);
1939 >            rs[1].assertNotInvoked();
1940          }
2209
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;
1941          try {
1942 <            assertEquals(inc(v1), h.join());
1943 <            assertEquals(1, r.invocationCount);
1942 >            assertEquals(inc(v1), h2.join());
1943 >            rs[2].assertValue(inc(v1));
1944          } catch (CompletionException ok) {
1945 <            checkCompletedWithWrappedCancellationException(h);
1946 <            assertEquals(0, r.invocationCount);
1945 >            checkCompletedWithWrappedCancellationException(h2);
1946 >            rs[2].assertNotInvoked();
1947 >        }
1948 >        try {
1949 >            assertEquals(inc(v1), h3.join());
1950 >            rs[3].assertValue(inc(v1));
1951 >        } catch (CompletionException ok) {
1952 >            checkCompletedWithWrappedCancellationException(h3);
1953 >            rs[3].assertNotInvoked();
1954          }
1955  
1956 <        checkCancelled(f);
1957 <        checkCompletedNormally(g, v1);
1956 >        checkCompletedNormally(f, v1);
1957 >        checkCancelled(g);
1958      }}
1959  
1960      /**
1961 <     * acceptEither result completes normally after normal completion
2243 <     * of either source
1961 >     * applyToEither result completes exceptionally if action does
1962       */
1963 <    public void testAcceptEither_normalCompletion1() {
1963 >    public void testApplyToEither_actionFailed() {
1964          for (ExecutionMode m : ExecutionMode.values())
1965          for (Integer v1 : new Integer[] { 1, null })
1966          for (Integer v2 : new Integer[] { 2, null })
1967      {
1968          final CompletableFuture<Integer> f = new CompletableFuture<>();
1969          final CompletableFuture<Integer> g = new CompletableFuture<>();
1970 <        final IncAction r = new IncAction();
1971 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
1970 >        final FailingFunction[] rs = new FailingFunction[6];
1971 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
1972  
1973 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1974 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1975          f.complete(v1);
1976 <        checkCompletedNormally(h, null);
1977 <        assertEquals(inc(v1), r.value);
1978 <        g.complete(v2);
1976 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1977 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1978 >        checkCompletedWithWrappedCFException(h0);
1979 >        checkCompletedWithWrappedCFException(h1);
1980 >        checkCompletedWithWrappedCFException(h2);
1981 >        checkCompletedWithWrappedCFException(h3);
1982 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
1983  
1984 <        checkCompletedNormally(f, v1);
2261 <        checkCompletedNormally(g, v2);
2262 <        checkCompletedNormally(h, null);
2263 <    }}
1984 >        g.complete(v2);
1985  
1986 <    public void testAcceptEither_normalCompletion2() {
1987 <        for (ExecutionMode m : ExecutionMode.values())
1988 <        for (Integer v1 : new Integer[] { 1, null })
2268 <        for (Integer v2 : new Integer[] { 2, null })
2269 <    {
2270 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2271 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2272 <        final IncAction r = new IncAction();
2273 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
1986 >        // unspecified behavior - both source completions available
1987 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
1988 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
1989  
1990 <        g.complete(v2);
1991 <        checkCompletedNormally(h, null);
1992 <        assertEquals(inc(v2), r.value);
1993 <        f.complete(v1);
1990 >        checkCompletedWithWrappedCFException(h4);
1991 >        assertTrue(Objects.equals(v1, rs[4].value) ||
1992 >                   Objects.equals(v2, rs[4].value));
1993 >        checkCompletedWithWrappedCFException(h5);
1994 >        assertTrue(Objects.equals(v1, rs[5].value) ||
1995 >                   Objects.equals(v2, rs[5].value));
1996  
1997          checkCompletedNormally(f, v1);
1998          checkCompletedNormally(g, v2);
2282        checkCompletedNormally(h, null);
1999      }}
2000  
2001 <    public void testAcceptEither_normalCompletion3() {
2001 >    /**
2002 >     * acceptEither result completes normally after normal completion
2003 >     * of either source
2004 >     */
2005 >    public void testAcceptEither_normalCompletion() {
2006          for (ExecutionMode m : ExecutionMode.values())
2007          for (Integer v1 : new Integer[] { 1, null })
2008          for (Integer v2 : new Integer[] { 2, null })
2009      {
2010          final CompletableFuture<Integer> f = new CompletableFuture<>();
2011          final CompletableFuture<Integer> g = new CompletableFuture<>();
2012 <        final IncAction r = new IncAction();
2012 >        final NoopConsumer[] rs = new NoopConsumer[6];
2013 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2014  
2015 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2016 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2017 +        checkIncomplete(h0);
2018 +        checkIncomplete(h1);
2019 +        rs[0].assertNotInvoked();
2020 +        rs[1].assertNotInvoked();
2021          f.complete(v1);
2022 +        checkCompletedNormally(h0, null);
2023 +        checkCompletedNormally(h1, null);
2024 +        rs[0].assertValue(v1);
2025 +        rs[1].assertValue(v1);
2026 +        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2027 +        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2028 +        checkCompletedNormally(h2, null);
2029 +        checkCompletedNormally(h3, null);
2030 +        rs[2].assertValue(v1);
2031 +        rs[3].assertValue(v1);
2032          g.complete(v2);
2296        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2033  
2034 <        checkCompletedNormally(h, null);
2034 >        // unspecified behavior - both source completions available
2035 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2036 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2037 >        checkCompletedNormally(h4, null);
2038 >        checkCompletedNormally(h5, null);
2039 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2040 >                   Objects.equals(v2, rs[4].value));
2041 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2042 >                   Objects.equals(v2, rs[5].value));
2043 >
2044          checkCompletedNormally(f, v1);
2045          checkCompletedNormally(g, v2);
2046 <
2047 <        // unspecified behavior
2048 <        assertTrue(Objects.equals(r.value, inc(v1)) ||
2049 <                   Objects.equals(r.value, inc(v2)));
2046 >        checkCompletedNormally(h0, null);
2047 >        checkCompletedNormally(h1, null);
2048 >        checkCompletedNormally(h2, null);
2049 >        checkCompletedNormally(h3, null);
2050 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2051      }}
2052  
2053      /**
2054       * acceptEither result completes exceptionally after exceptional
2055       * completion of either source
2056       */
2057 <    public void testAcceptEither_exceptionalCompletion1() {
2057 >    public void testAcceptEither_exceptionalCompletion() {
2058          for (ExecutionMode m : ExecutionMode.values())
2059          for (Integer v1 : new Integer[] { 1, null })
2060      {
2061          final CompletableFuture<Integer> f = new CompletableFuture<>();
2062          final CompletableFuture<Integer> g = new CompletableFuture<>();
2317        final IncAction r = new IncAction();
2318        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2063          final CFException ex = new CFException();
2064 +        final NoopConsumer[] rs = new NoopConsumer[6];
2065 +        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2066  
2067 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2068 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2069 +        checkIncomplete(h0);
2070 +        checkIncomplete(h1);
2071 +        rs[0].assertNotInvoked();
2072 +        rs[1].assertNotInvoked();
2073          f.completeExceptionally(ex);
2074 <        checkCompletedWithWrappedCFException(h, ex);
2075 <        g.complete(v1);
2076 <
2077 <        assertEquals(0, r.invocationCount);
2078 <        checkCompletedNormally(g, v1);
2079 <        checkCompletedWithWrappedCFException(f, ex);
2328 <        checkCompletedWithWrappedCFException(h, ex);
2329 <    }}
2330 <
2331 <    public void testAcceptEither_exceptionalCompletion2() {
2332 <        for (ExecutionMode m : ExecutionMode.values())
2333 <        for (Integer v1 : new Integer[] { 1, null })
2334 <    {
2335 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2336 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2337 <        final IncAction r = new IncAction();
2338 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2339 <        final CFException ex = new CFException();
2340 <
2341 <        g.completeExceptionally(ex);
2342 <        checkCompletedWithWrappedCFException(h, ex);
2343 <        f.complete(v1);
2344 <
2345 <        assertEquals(0, r.invocationCount);
2346 <        checkCompletedNormally(f, v1);
2347 <        checkCompletedWithWrappedCFException(g, ex);
2348 <        checkCompletedWithWrappedCFException(h, ex);
2349 <    }}
2074 >        checkCompletedWithWrappedCFException(h0, ex);
2075 >        checkCompletedWithWrappedCFException(h1, ex);
2076 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2077 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2078 >        checkCompletedWithWrappedCFException(h2, ex);
2079 >        checkCompletedWithWrappedCFException(h3, ex);
2080  
2081 <    public void testAcceptEither_exceptionalCompletion3() {
2352 <        for (ExecutionMode m : ExecutionMode.values())
2353 <        for (Integer v1 : new Integer[] { 1, null })
2354 <    {
2355 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2356 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2357 <        final IncAction r = new IncAction();
2358 <        final CFException ex = new CFException();
2359 <
2360 <        g.completeExceptionally(ex);
2361 <        f.complete(v1);
2362 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2081 >        g.complete(v1);
2082  
2083 <        // unspecified behavior
2084 <        Integer v;
2083 >        // unspecified behavior - both source completions available
2084 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2085 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2086          try {
2087 <            assertNull(h.join());
2088 <            assertEquals(1, r.invocationCount);
2369 <            assertEquals(inc(v1), r.value);
2087 >            assertNull(h4.join());
2088 >            rs[4].assertValue(v1);
2089          } catch (CompletionException ok) {
2090 <            checkCompletedWithWrappedCFException(h, ex);
2091 <            assertEquals(0, r.invocationCount);
2090 >            checkCompletedWithWrappedCFException(h4, ex);
2091 >            rs[4].assertNotInvoked();
2092          }
2374
2375        checkCompletedWithWrappedCFException(g, ex);
2376        checkCompletedNormally(f, v1);
2377    }}
2378
2379    public void testAcceptEither_exceptionalCompletion4() {
2380        for (ExecutionMode m : ExecutionMode.values())
2381        for (Integer v1 : new Integer[] { 1, null })
2382    {
2383        final CompletableFuture<Integer> f = new CompletableFuture<>();
2384        final CompletableFuture<Integer> g = new CompletableFuture<>();
2385        final IncAction r = new IncAction();
2386        final CFException ex = new CFException();
2387
2388        f.completeExceptionally(ex);
2389        g.complete(v1);
2390        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2391
2392        // unspecified behavior
2393        Integer v;
2093          try {
2094 <            assertNull(h.join());
2095 <            assertEquals(1, r.invocationCount);
2397 <            assertEquals(inc(v1), r.value);
2094 >            assertNull(h5.join());
2095 >            rs[5].assertValue(v1);
2096          } catch (CompletionException ok) {
2097 <            checkCompletedWithWrappedCFException(h, ex);
2098 <            assertEquals(0, r.invocationCount);
2097 >            checkCompletedWithWrappedCFException(h5, ex);
2098 >            rs[5].assertNotInvoked();
2099          }
2100  
2101          checkCompletedWithWrappedCFException(f, ex);
2102          checkCompletedNormally(g, v1);
2103 <    }}
2104 <
2105 <    /**
2106 <     * acceptEither result completes exceptionally if action does
2107 <     */
2108 <    public void testAcceptEither_actionFailed1() {
2411 <        for (ExecutionMode m : ExecutionMode.values())
2412 <        for (Integer v1 : new Integer[] { 1, null })
2413 <        for (Integer v2 : new Integer[] { 2, null })
2414 <    {
2415 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2416 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2417 <        final FailingConsumer r = new FailingConsumer();
2418 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2419 <
2420 <        f.complete(v1);
2421 <        checkCompletedWithWrappedCFException(h);
2422 <        g.complete(v2);
2423 <        checkCompletedNormally(f, v1);
2424 <        checkCompletedNormally(g, v2);
2425 <    }}
2426 <
2427 <    public void testAcceptEither_actionFailed2() {
2428 <        for (ExecutionMode m : ExecutionMode.values())
2429 <        for (Integer v1 : new Integer[] { 1, null })
2430 <        for (Integer v2 : new Integer[] { 2, null })
2431 <    {
2432 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2433 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2434 <        final FailingConsumer r = new FailingConsumer();
2435 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2436 <
2437 <        g.complete(v2);
2438 <        checkCompletedWithWrappedCFException(h);
2439 <        f.complete(v1);
2440 <        checkCompletedNormally(f, v1);
2441 <        checkCompletedNormally(g, v2);
2103 >        checkCompletedWithWrappedCFException(h0, ex);
2104 >        checkCompletedWithWrappedCFException(h1, ex);
2105 >        checkCompletedWithWrappedCFException(h2, ex);
2106 >        checkCompletedWithWrappedCFException(h3, ex);
2107 >        checkCompletedWithWrappedCFException(h4, ex);
2108 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2109      }}
2110  
2111      /**
2112       * acceptEither result completes exceptionally if either source cancelled
2113       */
2114 <    public void testAcceptEither_sourceCancelled1() {
2114 >    public void testAcceptEither_sourceCancelled() {
2115          for (ExecutionMode m : ExecutionMode.values())
2116          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2117          for (Integer v1 : new Integer[] { 1, null })
2118      {
2119          final CompletableFuture<Integer> f = new CompletableFuture<>();
2120          final CompletableFuture<Integer> g = new CompletableFuture<>();
2121 <        final IncAction r = new IncAction();
2122 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2456 <
2457 <        assertTrue(f.cancel(mayInterruptIfRunning));
2458 <        checkCompletedWithWrappedCancellationException(h);
2459 <        g.complete(v1);
2121 >        final NoopConsumer[] rs = new NoopConsumer[6];
2122 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2123  
2124 <        checkCancelled(f);
2125 <        assertEquals(0, r.invocationCount);
2126 <        checkCompletedNormally(g, v1);
2127 <        checkCompletedWithWrappedCancellationException(h);
2128 <    }}
2124 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2125 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2126 >        checkIncomplete(h0);
2127 >        checkIncomplete(h1);
2128 >        rs[0].assertNotInvoked();
2129 >        rs[1].assertNotInvoked();
2130 >        f.cancel(mayInterruptIfRunning);
2131 >        checkCompletedWithWrappedCancellationException(h0);
2132 >        checkCompletedWithWrappedCancellationException(h1);
2133 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2134 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2135 >        checkCompletedWithWrappedCancellationException(h2);
2136 >        checkCompletedWithWrappedCancellationException(h3);
2137  
2138 <    public void testAcceptEither_sourceCancelled2() {
2468 <        for (ExecutionMode m : ExecutionMode.values())
2469 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2470 <        for (Integer v1 : new Integer[] { 1, null })
2471 <    {
2472 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2473 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2474 <        final IncAction r = new IncAction();
2475 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2476 <
2477 <        assertTrue(g.cancel(mayInterruptIfRunning));
2478 <        checkCompletedWithWrappedCancellationException(h);
2479 <        f.complete(v1);
2480 <
2481 <        checkCancelled(g);
2482 <        assertEquals(0, r.invocationCount);
2483 <        checkCompletedNormally(f, v1);
2484 <        checkCompletedWithWrappedCancellationException(h);
2485 <    }}
2486 <
2487 <    public void testAcceptEither_sourceCancelled3() {
2488 <        for (ExecutionMode m : ExecutionMode.values())
2489 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2490 <        for (Integer v1 : new Integer[] { 1, null })
2491 <    {
2492 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2493 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2494 <        final IncAction r = new IncAction();
2495 <
2496 <        assertTrue(g.cancel(mayInterruptIfRunning));
2497 <        f.complete(v1);
2498 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2138 >        g.complete(v1);
2139  
2140 <        // unspecified behavior
2141 <        Integer v;
2140 >        // unspecified behavior - both source completions available
2141 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2142 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2143          try {
2144 <            assertNull(h.join());
2145 <            assertEquals(1, r.invocationCount);
2505 <            assertEquals(inc(v1), r.value);
2144 >            assertNull(h4.join());
2145 >            rs[4].assertValue(v1);
2146          } catch (CompletionException ok) {
2147 <            checkCompletedWithWrappedCancellationException(h);
2148 <            assertEquals(0, r.invocationCount);
2147 >            checkCompletedWithWrappedCancellationException(h4);
2148 >            rs[4].assertNotInvoked();
2149          }
2510
2511        checkCancelled(g);
2512        checkCompletedNormally(f, v1);
2513    }}
2514
2515    public void testAcceptEither_sourceCancelled4() {
2516        for (ExecutionMode m : ExecutionMode.values())
2517        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2518        for (Integer v1 : new Integer[] { 1, null })
2519    {
2520        final CompletableFuture<Integer> f = new CompletableFuture<>();
2521        final CompletableFuture<Integer> g = new CompletableFuture<>();
2522        final IncAction r = new IncAction();
2523
2524        assertTrue(f.cancel(mayInterruptIfRunning));
2525        g.complete(v1);
2526        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2527
2528        // unspecified behavior
2529        Integer v;
2150          try {
2151 <            assertNull(h.join());
2152 <            assertEquals(1, r.invocationCount);
2533 <            assertEquals(inc(v1), r.value);
2151 >            assertNull(h5.join());
2152 >            rs[5].assertValue(v1);
2153          } catch (CompletionException ok) {
2154 <            checkCompletedWithWrappedCancellationException(h);
2155 <            assertEquals(0, r.invocationCount);
2154 >            checkCompletedWithWrappedCancellationException(h5);
2155 >            rs[5].assertNotInvoked();
2156          }
2157  
2158          checkCancelled(f);
2159          checkCompletedNormally(g, v1);
2160 +        checkCompletedWithWrappedCancellationException(h0);
2161 +        checkCompletedWithWrappedCancellationException(h1);
2162 +        checkCompletedWithWrappedCancellationException(h2);
2163 +        checkCompletedWithWrappedCancellationException(h3);
2164 +        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2165      }}
2166  
2167      /**
2168 <     * runAfterEither result completes normally after normal completion
2545 <     * of either source
2168 >     * acceptEither result completes exceptionally if action does
2169       */
2170 <    public void testRunAfterEither_normalCompletion1() {
2170 >    public void testAcceptEither_actionFailed() {
2171          for (ExecutionMode m : ExecutionMode.values())
2172          for (Integer v1 : new Integer[] { 1, null })
2173          for (Integer v2 : new Integer[] { 2, null })
2174      {
2175          final CompletableFuture<Integer> f = new CompletableFuture<>();
2176          final CompletableFuture<Integer> g = new CompletableFuture<>();
2177 <        final Noop r = new Noop();
2178 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2177 >        final FailingConsumer[] rs = new FailingConsumer[6];
2178 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
2179  
2180 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2181 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2182          f.complete(v1);
2183 <        checkCompletedNormally(h, null);
2184 <        assertEquals(1, r.invocationCount);
2185 <        g.complete(v2);
2183 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2184 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2185 >        checkCompletedWithWrappedCFException(h0);
2186 >        checkCompletedWithWrappedCFException(h1);
2187 >        checkCompletedWithWrappedCFException(h2);
2188 >        checkCompletedWithWrappedCFException(h3);
2189 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2190  
2191 <        checkCompletedNormally(f, v1);
2563 <        checkCompletedNormally(g, v2);
2564 <        checkCompletedNormally(h, null);
2565 <        assertEquals(1, r.invocationCount);
2566 <    }}
2191 >        g.complete(v2);
2192  
2193 <    public void testRunAfterEither_normalCompletion2() {
2194 <        for (ExecutionMode m : ExecutionMode.values())
2195 <        for (Integer v1 : new Integer[] { 1, null })
2571 <        for (Integer v2 : new Integer[] { 2, null })
2572 <    {
2573 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2574 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2575 <        final Noop r = new Noop();
2576 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2193 >        // unspecified behavior - both source completions available
2194 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2195 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2196  
2197 <        g.complete(v2);
2198 <        checkCompletedNormally(h, null);
2199 <        assertEquals(1, r.invocationCount);
2200 <        f.complete(v1);
2197 >        checkCompletedWithWrappedCFException(h4);
2198 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2199 >                   Objects.equals(v2, rs[4].value));
2200 >        checkCompletedWithWrappedCFException(h5);
2201 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2202 >                   Objects.equals(v2, rs[5].value));
2203  
2204          checkCompletedNormally(f, v1);
2205          checkCompletedNormally(g, v2);
2206 <        checkCompletedNormally(h, null);
2586 <        assertEquals(1, r.invocationCount);
2587 <        }}
2206 >    }}
2207  
2208 <    public void testRunAfterEither_normalCompletion3() {
2208 >    /**
2209 >     * runAfterEither result completes normally after normal completion
2210 >     * of either source
2211 >     */
2212 >    public void testRunAfterEither_normalCompletion() {
2213          for (ExecutionMode m : ExecutionMode.values())
2214          for (Integer v1 : new Integer[] { 1, null })
2215          for (Integer v2 : new Integer[] { 2, null })
2216      {
2217          final CompletableFuture<Integer> f = new CompletableFuture<>();
2218          final CompletableFuture<Integer> g = new CompletableFuture<>();
2219 <        final Noop r = new Noop();
2219 >        final Noop[] rs = new Noop[6];
2220 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2221  
2222 +        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2223 +        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2224 +        checkIncomplete(h0);
2225 +        checkIncomplete(h1);
2226 +        rs[0].assertNotInvoked();
2227 +        rs[1].assertNotInvoked();
2228          f.complete(v1);
2229 +        checkCompletedNormally(h0, null);
2230 +        checkCompletedNormally(h1, null);
2231 +        rs[0].assertInvoked();
2232 +        rs[1].assertInvoked();
2233 +        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2234 +        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2235 +        checkCompletedNormally(h2, null);
2236 +        checkCompletedNormally(h3, null);
2237 +        rs[2].assertInvoked();
2238 +        rs[3].assertInvoked();
2239 +
2240          g.complete(v2);
2600        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2241  
2242 <        checkCompletedNormally(h, null);
2242 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2243 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2244 >
2245          checkCompletedNormally(f, v1);
2246          checkCompletedNormally(g, v2);
2247 <        assertEquals(1, r.invocationCount);
2247 >        checkCompletedNormally(h0, null);
2248 >        checkCompletedNormally(h1, null);
2249 >        checkCompletedNormally(h2, null);
2250 >        checkCompletedNormally(h3, null);
2251 >        checkCompletedNormally(h4, null);
2252 >        checkCompletedNormally(h5, null);
2253 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2254      }}
2255  
2256      /**
2257       * runAfterEither result completes exceptionally after exceptional
2258       * completion of either source
2259       */
2260 <    public void testRunAfterEither_exceptionalCompletion1() {
2260 >    public void testRunAfterEither_exceptionalCompletion() {
2261          for (ExecutionMode m : ExecutionMode.values())
2262          for (Integer v1 : new Integer[] { 1, null })
2263      {
2264          final CompletableFuture<Integer> f = new CompletableFuture<>();
2265          final CompletableFuture<Integer> g = new CompletableFuture<>();
2618        final Noop r = new Noop();
2619        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2266          final CFException ex = new CFException();
2267 +        final Noop[] rs = new Noop[6];
2268 +        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2269  
2270 +        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2271 +        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2272 +        checkIncomplete(h0);
2273 +        checkIncomplete(h1);
2274 +        rs[0].assertNotInvoked();
2275 +        rs[1].assertNotInvoked();
2276          f.completeExceptionally(ex);
2277 <        checkCompletedWithWrappedCFException(h, ex);
2278 <        g.complete(v1);
2279 <
2280 <        assertEquals(0, r.invocationCount);
2281 <        checkCompletedNormally(g, v1);
2282 <        checkCompletedWithWrappedCFException(f, ex);
2629 <        checkCompletedWithWrappedCFException(h, ex);
2630 <    }}
2631 <
2632 <    public void testRunAfterEither_exceptionalCompletion2() {
2633 <        for (ExecutionMode m : ExecutionMode.values())
2634 <        for (Integer v1 : new Integer[] { 1, null })
2635 <    {
2636 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2637 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2638 <        final Noop r = new Noop();
2639 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2640 <        final CFException ex = new CFException();
2277 >        checkCompletedWithWrappedCFException(h0, ex);
2278 >        checkCompletedWithWrappedCFException(h1, ex);
2279 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2280 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2281 >        checkCompletedWithWrappedCFException(h2, ex);
2282 >        checkCompletedWithWrappedCFException(h3, ex);
2283  
2284 <        g.completeExceptionally(ex);
2643 <        checkCompletedWithWrappedCFException(h, ex);
2644 <        f.complete(v1);
2645 <
2646 <        assertEquals(0, r.invocationCount);
2647 <        checkCompletedNormally(f, v1);
2648 <        checkCompletedWithWrappedCFException(g, ex);
2649 <        checkCompletedWithWrappedCFException(h, ex);
2650 <    }}
2651 <
2652 <    public void testRunAfterEither_exceptionalCompletion3() {
2653 <        for (ExecutionMode m : ExecutionMode.values())
2654 <        for (Integer v1 : new Integer[] { 1, null })
2655 <    {
2656 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2657 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2658 <        final Noop r = new Noop();
2659 <        final CFException ex = new CFException();
2660 <
2661 <        g.completeExceptionally(ex);
2662 <        f.complete(v1);
2663 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2284 >        g.complete(v1);
2285  
2286 <        // unspecified behavior
2287 <        Integer v;
2286 >        // unspecified behavior - both source completions available
2287 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2288 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2289          try {
2290 <            assertNull(h.join());
2291 <            assertEquals(1, r.invocationCount);
2290 >            assertNull(h4.join());
2291 >            rs[4].assertInvoked();
2292          } catch (CompletionException ok) {
2293 <            checkCompletedWithWrappedCFException(h, ex);
2294 <            assertEquals(0, r.invocationCount);
2293 >            checkCompletedWithWrappedCFException(h4, ex);
2294 >            rs[4].assertNotInvoked();
2295          }
2674
2675        checkCompletedWithWrappedCFException(g, ex);
2676        checkCompletedNormally(f, v1);
2677    }}
2678
2679    public void testRunAfterEither_exceptionalCompletion4() {
2680        for (ExecutionMode m : ExecutionMode.values())
2681        for (Integer v1 : new Integer[] { 1, null })
2682    {
2683        final CompletableFuture<Integer> f = new CompletableFuture<>();
2684        final CompletableFuture<Integer> g = new CompletableFuture<>();
2685        final Noop r = new Noop();
2686        final CFException ex = new CFException();
2687
2688        f.completeExceptionally(ex);
2689        g.complete(v1);
2690        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2691
2692        // unspecified behavior
2693        Integer v;
2296          try {
2297 <            assertNull(h.join());
2298 <            assertEquals(1, r.invocationCount);
2297 >            assertNull(h5.join());
2298 >            rs[5].assertInvoked();
2299          } catch (CompletionException ok) {
2300 <            checkCompletedWithWrappedCFException(h, ex);
2301 <            assertEquals(0, r.invocationCount);
2300 >            checkCompletedWithWrappedCFException(h5, ex);
2301 >            rs[5].assertNotInvoked();
2302          }
2303  
2304          checkCompletedWithWrappedCFException(f, ex);
2305          checkCompletedNormally(g, v1);
2306 <    }}
2307 <
2308 <    /**
2309 <     * runAfterEither result completes exceptionally if action does
2310 <     */
2311 <    public void testRunAfterEither_actionFailed1() {
2710 <        for (ExecutionMode m : ExecutionMode.values())
2711 <        for (Integer v1 : new Integer[] { 1, null })
2712 <        for (Integer v2 : new Integer[] { 2, null })
2713 <    {
2714 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2715 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2716 <        final FailingNoop r = new FailingNoop();
2717 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2718 <
2719 <        f.complete(v1);
2720 <        checkCompletedWithWrappedCFException(h);
2721 <        g.complete(v2);
2722 <        checkCompletedNormally(f, v1);
2723 <        checkCompletedNormally(g, v2);
2724 <    }}
2725 <
2726 <    public void testRunAfterEither_actionFailed2() {
2727 <        for (ExecutionMode m : ExecutionMode.values())
2728 <        for (Integer v1 : new Integer[] { 1, null })
2729 <        for (Integer v2 : new Integer[] { 2, null })
2730 <    {
2731 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2732 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2733 <        final FailingNoop r = new FailingNoop();
2734 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2735 <
2736 <        g.complete(v2);
2737 <        checkCompletedWithWrappedCFException(h);
2738 <        f.complete(v1);
2739 <        checkCompletedNormally(f, v1);
2740 <        checkCompletedNormally(g, v2);
2306 >        checkCompletedWithWrappedCFException(h0, ex);
2307 >        checkCompletedWithWrappedCFException(h1, ex);
2308 >        checkCompletedWithWrappedCFException(h2, ex);
2309 >        checkCompletedWithWrappedCFException(h3, ex);
2310 >        checkCompletedWithWrappedCFException(h4, ex);
2311 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2312      }}
2313  
2314      /**
2315       * runAfterEither result completes exceptionally if either source cancelled
2316       */
2317 <    public void testRunAfterEither_sourceCancelled1() {
2317 >    public void testRunAfterEither_sourceCancelled() {
2318          for (ExecutionMode m : ExecutionMode.values())
2319          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2320          for (Integer v1 : new Integer[] { 1, null })
2321      {
2322          final CompletableFuture<Integer> f = new CompletableFuture<>();
2323          final CompletableFuture<Integer> g = new CompletableFuture<>();
2324 <        final Noop r = new Noop();
2325 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2324 >        final Noop[] rs = new Noop[6];
2325 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2326  
2327 <        assertTrue(f.cancel(mayInterruptIfRunning));
2328 <        checkCompletedWithWrappedCancellationException(h);
2329 <        g.complete(v1);
2327 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2328 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2329 >        checkIncomplete(h0);
2330 >        checkIncomplete(h1);
2331 >        rs[0].assertNotInvoked();
2332 >        rs[1].assertNotInvoked();
2333 >        f.cancel(mayInterruptIfRunning);
2334 >        checkCompletedWithWrappedCancellationException(h0);
2335 >        checkCompletedWithWrappedCancellationException(h1);
2336 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2337 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2338 >        checkCompletedWithWrappedCancellationException(h2);
2339 >        checkCompletedWithWrappedCancellationException(h3);
2340  
2341 <        checkCancelled(f);
2761 <        assertEquals(0, r.invocationCount);
2762 <        checkCompletedNormally(g, v1);
2763 <        checkCompletedWithWrappedCancellationException(h);
2764 <    }}
2765 <
2766 <    public void testRunAfterEither_sourceCancelled2() {
2767 <        for (ExecutionMode m : ExecutionMode.values())
2768 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2769 <        for (Integer v1 : new Integer[] { 1, null })
2770 <    {
2771 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2772 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2773 <        final Noop r = new Noop();
2774 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2775 <
2776 <        assertTrue(g.cancel(mayInterruptIfRunning));
2777 <        checkCompletedWithWrappedCancellationException(h);
2778 <        f.complete(v1);
2779 <
2780 <        checkCancelled(g);
2781 <        assertEquals(0, r.invocationCount);
2782 <        checkCompletedNormally(f, v1);
2783 <        checkCompletedWithWrappedCancellationException(h);
2784 <    }}
2785 <
2786 <    public void testRunAfterEither_sourceCancelled3() {
2787 <        for (ExecutionMode m : ExecutionMode.values())
2788 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2789 <        for (Integer v1 : new Integer[] { 1, null })
2790 <    {
2791 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2792 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2793 <        final Noop r = new Noop();
2794 <
2795 <        assertTrue(g.cancel(mayInterruptIfRunning));
2796 <        f.complete(v1);
2797 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2341 >        g.complete(v1);
2342  
2343 <        // unspecified behavior
2344 <        Integer v;
2343 >        // unspecified behavior - both source completions available
2344 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2345 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2346          try {
2347 <            assertNull(h.join());
2348 <            assertEquals(1, r.invocationCount);
2347 >            assertNull(h4.join());
2348 >            rs[4].assertInvoked();
2349          } catch (CompletionException ok) {
2350 <            checkCompletedWithWrappedCancellationException(h);
2351 <            assertEquals(0, r.invocationCount);
2350 >            checkCompletedWithWrappedCancellationException(h4);
2351 >            rs[4].assertNotInvoked();
2352          }
2808
2809        checkCancelled(g);
2810        checkCompletedNormally(f, v1);
2811    }}
2812
2813    public void testRunAfterEither_sourceCancelled4() {
2814        for (ExecutionMode m : ExecutionMode.values())
2815        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2816        for (Integer v1 : new Integer[] { 1, null })
2817    {
2818        final CompletableFuture<Integer> f = new CompletableFuture<>();
2819        final CompletableFuture<Integer> g = new CompletableFuture<>();
2820        final Noop r = new Noop();
2821
2822        assertTrue(f.cancel(mayInterruptIfRunning));
2823        g.complete(v1);
2824        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2825
2826        // unspecified behavior
2827        Integer v;
2353          try {
2354 <            assertNull(h.join());
2355 <            assertEquals(1, r.invocationCount);
2354 >            assertNull(h5.join());
2355 >            rs[5].assertInvoked();
2356          } catch (CompletionException ok) {
2357 <            checkCompletedWithWrappedCancellationException(h);
2358 <            assertEquals(0, r.invocationCount);
2357 >            checkCompletedWithWrappedCancellationException(h5);
2358 >            rs[5].assertNotInvoked();
2359          }
2360  
2361          checkCancelled(f);
2362          checkCompletedNormally(g, v1);
2363 +        checkCompletedWithWrappedCancellationException(h0);
2364 +        checkCompletedWithWrappedCancellationException(h1);
2365 +        checkCompletedWithWrappedCancellationException(h2);
2366 +        checkCompletedWithWrappedCancellationException(h3);
2367 +        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2368      }}
2369  
2370      /**
2371 <     * thenCompose result completes normally after normal completion of source
2371 >     * runAfterEither result completes exceptionally if action does
2372       */
2373 <    public void testThenCompose_normalCompletion1() {
2373 >    public void testRunAfterEither_actionFailed() {
2374          for (ExecutionMode m : ExecutionMode.values())
2375          for (Integer v1 : new Integer[] { 1, null })
2376 +        for (Integer v2 : new Integer[] { 2, null })
2377      {
2378          final CompletableFuture<Integer> f = new CompletableFuture<>();
2379 <        final CompletableFutureInc r = new CompletableFutureInc();
2380 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2379 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2380 >        final FailingRunnable[] rs = new FailingRunnable[6];
2381 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
2382 >
2383 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2384 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2385          f.complete(v1);
2386 <        checkCompletedNormally(g, inc(v1));
2386 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2387 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2388 >        checkCompletedWithWrappedCFException(h0);
2389 >        checkCompletedWithWrappedCFException(h1);
2390 >        checkCompletedWithWrappedCFException(h2);
2391 >        checkCompletedWithWrappedCFException(h3);
2392 >        for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2393 >        g.complete(v2);
2394 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2395 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2396 >        checkCompletedWithWrappedCFException(h4);
2397 >        checkCompletedWithWrappedCFException(h5);
2398 >
2399          checkCompletedNormally(f, v1);
2400 <        assertEquals(1, r.invocationCount);
2400 >        checkCompletedNormally(g, v2);
2401 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2402      }}
2403  
2404 <    public void testThenCompose_normalCompletion2() {
2404 >    /**
2405 >     * thenCompose result completes normally after normal completion of source
2406 >     */
2407 >    public void testThenCompose_normalCompletion() {
2408          for (ExecutionMode m : ExecutionMode.values())
2409 +        for (boolean createIncomplete : new boolean[] { true, false })
2410          for (Integer v1 : new Integer[] { 1, null })
2411      {
2412          final CompletableFuture<Integer> f = new CompletableFuture<>();
2413 <        final CompletableFutureInc r = new CompletableFutureInc();
2414 <        f.complete(v1);
2415 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2413 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2414 >        if (!createIncomplete) f.complete(v1);
2415 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2416 >        if (createIncomplete) f.complete(v1);
2417 >
2418          checkCompletedNormally(g, inc(v1));
2419          checkCompletedNormally(f, v1);
2420 <        assertEquals(1, r.invocationCount);
2420 >        r.assertInvoked();
2421      }}
2422  
2423      /**
2424       * thenCompose result completes exceptionally after exceptional
2425       * completion of source
2426       */
2427 <    public void testThenCompose_exceptionalCompletion1() {
2427 >    public void testThenCompose_exceptionalCompletion() {
2428          for (ExecutionMode m : ExecutionMode.values())
2429 +        for (boolean createIncomplete : new boolean[] { true, false })
2430      {
2431          final CFException ex = new CFException();
2432 <        final CompletableFutureInc r = new CompletableFutureInc();
2432 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2433          final CompletableFuture<Integer> f = new CompletableFuture<>();
2434 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2435 <        f.completeExceptionally(ex);
2436 <        checkCompletedWithWrappedCFException(g, ex);
2882 <        checkCompletedWithWrappedCFException(f, ex);
2883 <    }}
2434 >        if (!createIncomplete) f.completeExceptionally(ex);
2435 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2436 >        if (createIncomplete) f.completeExceptionally(ex);
2437  
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);
2438          checkCompletedWithWrappedCFException(g, ex);
2439          checkCompletedWithWrappedCFException(f, ex);
2440 +        r.assertNotInvoked();
2441      }}
2442  
2443      /**
2444       * thenCompose result completes exceptionally if action does
2445       */
2446 <    public void testThenCompose_actionFailed1() {
2446 >    public void testThenCompose_actionFailed() {
2447          for (ExecutionMode m : ExecutionMode.values())
2448 +        for (boolean createIncomplete : new boolean[] { true, false })
2449          for (Integer v1 : new Integer[] { 1, null })
2450      {
2451          final CompletableFuture<Integer> f = new CompletableFuture<>();
2452          final FailingCompletableFutureFunction r
2453 <            = new FailingCompletableFutureFunction();
2454 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2455 <        f.complete(v1);
2456 <        checkCompletedWithWrappedCFException(g);
2910 <        checkCompletedNormally(f, v1);
2911 <    }}
2453 >            = new FailingCompletableFutureFunction(m);
2454 >        if (!createIncomplete) f.complete(v1);
2455 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2456 >        if (createIncomplete) f.complete(v1);
2457  
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);
2458          checkCompletedWithWrappedCFException(g);
2459          checkCompletedNormally(f, v1);
2460      }}
# Line 2926 | Line 2462 | public class CompletableFutureTest exten
2462      /**
2463       * thenCompose result completes exceptionally if source cancelled
2464       */
2465 <    public void testThenCompose_sourceCancelled1() {
2465 >    public void testThenCompose_sourceCancelled() {
2466          for (ExecutionMode m : ExecutionMode.values())
2467 +        for (boolean createIncomplete : new boolean[] { true, false })
2468          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2469      {
2470          final CompletableFuture<Integer> f = new CompletableFuture<>();
2471 <        final CompletableFutureInc r = new CompletableFutureInc();
2472 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2473 <        assertTrue(f.cancel(mayInterruptIfRunning));
2474 <        checkCompletedWithWrappedCancellationException(g);
2475 <        checkCancelled(f);
2476 <    }}
2471 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2472 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2473 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2474 >        if (createIncomplete) {
2475 >            checkIncomplete(g);
2476 >            assertTrue(f.cancel(mayInterruptIfRunning));
2477 >        }
2478  
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);
2479          checkCompletedWithWrappedCancellationException(g);
2480          checkCancelled(f);
2481      }}
2482  
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
2483      // other static methods
2484  
2485      /**
# Line 3261 | Line 2497 | public class CompletableFutureTest exten
2497       */
2498      public void testAllOf_normal() throws Exception {
2499          for (int k = 1; k < 20; ++k) {
2500 <            CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2500 >            CompletableFuture<Integer>[] fs
2501 >                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2502              for (int i = 0; i < k; ++i)
2503                  fs[i] = new CompletableFuture<>();
2504              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
# Line 3275 | Line 2512 | public class CompletableFutureTest exten
2512          }
2513      }
2514  
2515 +    public void testAllOf_backwards() throws Exception {
2516 +        for (int k = 1; k < 20; ++k) {
2517 +            CompletableFuture<Integer>[] fs
2518 +                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2519 +            for (int i = 0; i < k; ++i)
2520 +                fs[i] = new CompletableFuture<>();
2521 +            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
2522 +            for (int i = k - 1; i >= 0; i--) {
2523 +                checkIncomplete(f);
2524 +                checkIncomplete(CompletableFuture.allOf(fs));
2525 +                fs[i].complete(one);
2526 +            }
2527 +            checkCompletedNormally(f, null);
2528 +            checkCompletedNormally(CompletableFuture.allOf(fs), null);
2529 +        }
2530 +    }
2531 +
2532      /**
2533       * anyOf(no component futures) returns an incomplete future
2534       */
# Line 3333 | Line 2587 | public class CompletableFutureTest exten
2587          Runnable[] throwingActions = {
2588              () -> CompletableFuture.supplyAsync(null),
2589              () -> CompletableFuture.supplyAsync(null, exec),
2590 <            () -> CompletableFuture.supplyAsync(supplyOne, null),
2590 >            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null),
2591  
2592              () -> CompletableFuture.runAsync(null),
2593              () -> CompletableFuture.runAsync(null, exec),
# Line 3406 | Line 2660 | public class CompletableFutureTest exten
2660  
2661              () -> f.thenCompose(null),
2662              () -> f.thenComposeAsync(null),
2663 <            () -> f.thenComposeAsync(new CompletableFutureInc(), null),
2663 >            () -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null),
2664              () -> f.thenComposeAsync(null, exec),
2665  
2666              () -> f.exceptionally(null),

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines