ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
(Generate patch)

Comparing jsr166/src/test/tck/CompletableFutureTest.java (file contents):
Revision 1.46 by jsr166, Mon Jun 2 17:41:22 2014 UTC vs.
Revision 1.76 by jsr166, Sat Jun 7 21:45:13 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 103 | Line 105 | public class CompletableFutureTest exten
105          assertTrue(f.toString().contains("[Completed exceptionally]"));
106      }
107  
108 <    void checkCompletedWithWrappedCFException(CompletableFuture<?> f,
109 <                                              CFException ex) {
108 >    <U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f,
109 >                                                      Throwable ex) {
110          try {
111              f.get(LONG_DELAY_MS, MILLISECONDS);
112              shouldThrow();
# Line 129 | Line 131 | public class CompletableFutureTest exten
131          } catch (ExecutionException success) {
132              assertSame(ex, success.getCause());
133          } catch (Throwable fail) { threadUnexpectedException(fail); }
134 +
135          assertTrue(f.isDone());
136          assertFalse(f.isCancelled());
137          assertTrue(f.toString().contains("[Completed exceptionally]"));
138      }
139  
140 +    <U> void checkCompletedWithWrappedException(CompletableFuture<U> f,
141 +                                                Throwable ex) {
142 +        checkCompletedExceptionallyWithRootCause(f, ex);
143 +        try {
144 +            CompletableFuture<Throwable> spy = f.handle
145 +                ((U u, Throwable t) -> t);
146 +            assertTrue(spy.join() instanceof CompletionException);
147 +            assertSame(ex, spy.join().getCause());
148 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
149 +    }
150 +
151 +    <U> void checkCompletedExceptionally(CompletableFuture<U> f, Throwable ex) {
152 +        checkCompletedExceptionallyWithRootCause(f, ex);
153 +        try {
154 +            CompletableFuture<Throwable> spy = f.handle
155 +                ((U u, Throwable t) -> t);
156 +            assertSame(ex, spy.join());
157 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
158 +    }
159 +
160      void checkCancelled(CompletableFuture<?> f) {
161          try {
162              f.get(LONG_DELAY_MS, MILLISECONDS);
# Line 216 | Line 239 | public class CompletableFutureTest exten
239       */
240      public void testCompleteExceptionally() {
241          CompletableFuture<Integer> f = new CompletableFuture<>();
242 +        CFException ex = new CFException();
243          checkIncomplete(f);
244 <        f.completeExceptionally(new CFException());
245 <        checkCompletedWithWrappedCFException(f);
244 >        f.completeExceptionally(ex);
245 >        checkCompletedExceptionally(f, ex);
246      }
247  
248      /**
# Line 259 | Line 283 | public class CompletableFutureTest exten
283       * obtrudeException forces completion with given exception
284       */
285      public void testObtrudeException() {
286 <        CompletableFuture<Integer> f = new CompletableFuture<>();
287 <        checkIncomplete(f);
288 <        f.complete(one);
289 <        checkCompletedNormally(f, one);
290 <        f.obtrudeException(new CFException());
267 <        checkCompletedWithWrappedCFException(f);
286 >        for (Integer v1 : new Integer[] { 1, null })
287 >    {
288 >        CFException ex;
289 >        CompletableFuture<Integer> f;
290 >
291          f = new CompletableFuture<>();
292 <        f.obtrudeException(new CFException());
293 <        checkCompletedWithWrappedCFException(f);
292 >        f.complete(v1);
293 >        for (int i = 0; i < 2; i++) {
294 >            f.obtrudeException(ex = new CFException());
295 >            checkCompletedExceptionally(f, ex);
296 >        }
297 >
298          f = new CompletableFuture<>();
299 +        for (int i = 0; i < 2; i++) {
300 +            f.obtrudeException(ex = new CFException());
301 +            checkCompletedExceptionally(f, ex);
302 +        }
303 +
304 +        f = new CompletableFuture<>();
305 +        f.completeExceptionally(ex = new CFException());
306 +        f.obtrudeValue(v1);
307 +        checkCompletedNormally(f, v1);
308 +        f.obtrudeException(ex = new CFException());
309 +        checkCompletedExceptionally(f, ex);
310          f.completeExceptionally(new CFException());
311 <        f.obtrudeValue(four);
312 <        checkCompletedNormally(f, four);
313 <        f.obtrudeException(new CFException());
314 <        checkCompletedWithWrappedCFException(f);
277 <    }
311 >        checkCompletedExceptionally(f, ex);
312 >        f.complete(v1);
313 >        checkCompletedExceptionally(f, ex);
314 >    }}
315  
316      /**
317       * getNumberOfDependents returns number of dependent tasks
318       */
319      public void testGetNumberOfDependents() {
320 +        for (ExecutionMode m : ExecutionMode.values())
321 +    {
322          CompletableFuture<Integer> f = new CompletableFuture<>();
323          assertEquals(0, f.getNumberOfDependents());
324 <        CompletableFuture g = f.thenRun(new Noop());
324 >        final CompletableFuture<Void> g = m.thenRun(f, new Noop(m));
325          assertEquals(1, f.getNumberOfDependents());
326          assertEquals(0, g.getNumberOfDependents());
327 <        CompletableFuture h = f.thenRun(new Noop());
327 >        final CompletableFuture<Void> h = m.thenRun(f, new Noop(m));
328          assertEquals(2, f.getNumberOfDependents());
329 +        assertEquals(0, h.getNumberOfDependents());
330          f.complete(1);
331          checkCompletedNormally(g, null);
332 +        checkCompletedNormally(h, null);
333          assertEquals(0, f.getNumberOfDependents());
334          assertEquals(0, g.getNumberOfDependents());
335 <    }
335 >        assertEquals(0, h.getNumberOfDependents());
336 >    }}
337  
338      /**
339       * toString indicates current completion state
# Line 308 | Line 350 | public class CompletableFutureTest exten
350          f = new CompletableFuture<String>();
351          f.completeExceptionally(new IndexOutOfBoundsException());
352          assertTrue(f.toString().contains("[Completed exceptionally]"));
353 +
354 +        f = new CompletableFuture<String>();
355 +        f.cancel(true);
356 +        assertTrue(f.toString().contains("[Completed exceptionally]"));
357 +
358 +        f = new CompletableFuture<String>();
359 +        f.cancel(false);
360 +        assertTrue(f.toString().contains("[Completed exceptionally]"));
361      }
362  
363      /**
# Line 318 | Line 368 | public class CompletableFutureTest exten
368          checkCompletedNormally(f, "test");
369      }
370  
371 <    // Choose non-commutative actions for better coverage
371 >    abstract class CheckedAction {
372 >        int invocationCount = 0;
373 >        final ExecutionMode m;
374 >        CheckedAction(ExecutionMode m) { this.m = m; }
375 >        void invoked() {
376 >            m.checkExecutionMode();
377 >            assertEquals(0, invocationCount++);
378 >        }
379 >        void assertNotInvoked() { assertEquals(0, invocationCount); }
380 >        void assertInvoked() { assertEquals(1, invocationCount); }
381 >    }
382  
383 <    // A non-commutative function that handles and produces null values as well.
384 <    static Integer subtract(Integer x, Integer y) {
385 <        return (x == null && y == null) ? null :
386 <            ((x == null) ? 42 : x.intValue())
387 <            - ((y == null) ? 99 : y.intValue());
383 >    abstract class CheckedIntegerAction extends CheckedAction {
384 >        Integer value;
385 >        CheckedIntegerAction(ExecutionMode m) { super(m); }
386 >        void assertValue(Integer expected) {
387 >            assertInvoked();
388 >            assertEquals(expected, value);
389 >        }
390 >    }
391 >
392 >    class IntegerSupplier extends CheckedAction
393 >        implements Supplier<Integer>
394 >    {
395 >        final Integer value;
396 >        IntegerSupplier(ExecutionMode m, Integer value) {
397 >            super(m);
398 >            this.value = value;
399 >        }
400 >        public Integer get() {
401 >            invoked();
402 >            return value;
403 >        }
404      }
405  
406      // A function that handles and produces null values as well.
# Line 332 | Line 408 | public class CompletableFutureTest exten
408          return (x == null) ? null : x + 1;
409      }
410  
411 <    static final Supplier<Integer> supplyOne =
412 <        () -> Integer.valueOf(1);
413 <    static final Function<Integer, Integer> inc =
414 <        (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;
411 >    class NoopConsumer extends CheckedIntegerAction
412 >        implements Consumer<Integer>
413 >    {
414 >        NoopConsumer(ExecutionMode m) { super(m); }
415          public void accept(Integer x) {
416 <            invocationCount++;
417 <            value = inc(x);
416 >            invoked();
417 >            value = x;
418          }
419      }
420 <    static final class IncFunction implements Function<Integer,Integer> {
421 <        int invocationCount = 0;
422 <        Integer value;
420 >
421 >    class IncFunction extends CheckedIntegerAction
422 >        implements Function<Integer,Integer>
423 >    {
424 >        IncFunction(ExecutionMode m) { super(m); }
425          public Integer apply(Integer x) {
426 <            invocationCount++;
426 >            invoked();
427              return value = inc(x);
428          }
429      }
430 <    static final class SubtractAction implements BiConsumer<Integer, Integer> {
431 <        int invocationCount = 0;
432 <        Integer value;
433 <        // Check this action was invoked exactly once when result is computed.
430 >
431 >    // Choose non-commutative actions for better coverage
432 >    // A non-commutative function that handles and produces null values as well.
433 >    static Integer subtract(Integer x, Integer y) {
434 >        return (x == null && y == null) ? null :
435 >            ((x == null) ? 42 : x.intValue())
436 >            - ((y == null) ? 99 : y.intValue());
437 >    }
438 >
439 >    class SubtractAction extends CheckedIntegerAction
440 >        implements BiConsumer<Integer, Integer>
441 >    {
442 >        SubtractAction(ExecutionMode m) { super(m); }
443          public void accept(Integer x, Integer y) {
444 <            invocationCount++;
444 >            invoked();
445              value = subtract(x, y);
446          }
447      }
448 <    static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> {
449 <        int invocationCount = 0;
450 <        Integer value;
451 <        // Check this action was invoked exactly once when result is computed.
448 >
449 >    class SubtractFunction extends CheckedIntegerAction
450 >        implements BiFunction<Integer, Integer, Integer>
451 >    {
452 >        SubtractFunction(ExecutionMode m) { super(m); }
453          public Integer apply(Integer x, Integer y) {
454 <            invocationCount++;
454 >            invoked();
455              return value = subtract(x, y);
456          }
457      }
458 <    static final class Noop implements Runnable {
459 <        int invocationCount = 0;
458 >
459 >    class Noop extends CheckedAction implements Runnable {
460 >        Noop(ExecutionMode m) { super(m); }
461          public void run() {
462 <            invocationCount++;
462 >            invoked();
463          }
464      }
465  
466 <    static final class FailingSupplier implements Supplier<Integer> {
467 <        int invocationCount = 0;
466 >    class FailingSupplier extends CheckedAction
467 >        implements Supplier<Integer>
468 >    {
469 >        FailingSupplier(ExecutionMode m) { super(m); }
470          public Integer get() {
471 <            invocationCount++;
471 >            invoked();
472              throw new CFException();
473          }
474      }
475 <    static final class FailingConsumer implements Consumer<Integer> {
476 <        int invocationCount = 0;
475 >
476 >    class FailingConsumer extends CheckedIntegerAction
477 >        implements Consumer<Integer>
478 >    {
479 >        FailingConsumer(ExecutionMode m) { super(m); }
480          public void accept(Integer x) {
481 <            invocationCount++;
481 >            invoked();
482 >            value = x;
483              throw new CFException();
484          }
485      }
486 <    static final class FailingBiConsumer implements BiConsumer<Integer, Integer> {
487 <        int invocationCount = 0;
486 >
487 >    class FailingBiConsumer extends CheckedIntegerAction
488 >        implements BiConsumer<Integer, Integer>
489 >    {
490 >        FailingBiConsumer(ExecutionMode m) { super(m); }
491          public void accept(Integer x, Integer y) {
492 <            invocationCount++;
492 >            invoked();
493 >            value = subtract(x, y);
494              throw new CFException();
495          }
496      }
497 <    static final class FailingFunction implements Function<Integer, Integer> {
498 <        int invocationCount = 0;
497 >
498 >    class FailingFunction extends CheckedIntegerAction
499 >        implements Function<Integer, Integer>
500 >    {
501 >        FailingFunction(ExecutionMode m) { super(m); }
502          public Integer apply(Integer x) {
503 <            invocationCount++;
503 >            invoked();
504 >            value = x;
505              throw new CFException();
506          }
507      }
508 <    static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> {
509 <        int invocationCount = 0;
508 >
509 >    class FailingBiFunction extends CheckedIntegerAction
510 >        implements BiFunction<Integer, Integer, Integer>
511 >    {
512 >        FailingBiFunction(ExecutionMode m) { super(m); }
513          public Integer apply(Integer x, Integer y) {
514 <            invocationCount++;
514 >            invoked();
515 >            value = subtract(x, y);
516              throw new CFException();
517          }
518      }
519 <    static final class FailingNoop implements Runnable {
520 <        int invocationCount = 0;
519 >
520 >    class FailingRunnable extends CheckedAction implements Runnable {
521 >        FailingRunnable(ExecutionMode m) { super(m); }
522          public void run() {
523 <            invocationCount++;
523 >            invoked();
524              throw new CFException();
525          }
526      }
527  
528 <    static final class CompletableFutureInc
529 <        implements Function<Integer, CompletableFuture<Integer>> {
530 <        int invocationCount = 0;
528 >
529 >    class CompletableFutureInc extends CheckedIntegerAction
530 >        implements Function<Integer, CompletableFuture<Integer>>
531 >    {
532 >        CompletableFutureInc(ExecutionMode m) { super(m); }
533          public CompletableFuture<Integer> apply(Integer x) {
534 <            invocationCount++;
534 >            invoked();
535 >            value = x;
536              CompletableFuture<Integer> f = new CompletableFuture<>();
537              f.complete(inc(x));
538              return f;
539          }
540      }
541  
542 <    static final class FailingCompletableFutureFunction
543 <        implements Function<Integer, CompletableFuture<Integer>> {
544 <        int invocationCount = 0;
542 >    class FailingCompletableFutureFunction extends CheckedIntegerAction
543 >        implements Function<Integer, CompletableFuture<Integer>>
544 >    {
545 >        FailingCompletableFutureFunction(ExecutionMode m) { super(m); }
546          public CompletableFuture<Integer> apply(Integer x) {
547 <            invocationCount++;
547 >            invoked();
548 >            value = x;
549              throw new CFException();
550          }
551      }
552  
553      // Used for explicit executor tests
554      static final class ThreadExecutor implements Executor {
555 <        AtomicInteger count = new AtomicInteger(0);
555 >        final AtomicInteger count = new AtomicInteger(0);
556 >        static final ThreadGroup tg = new ThreadGroup("ThreadExecutor");
557 >        static boolean startedCurrentThread() {
558 >            return Thread.currentThread().getThreadGroup() == tg;
559 >        }
560  
561          public void execute(Runnable r) {
562              count.getAndIncrement();
563 <            new Thread(r).start();
563 >            new Thread(tg, r).start();
564          }
565      }
566  
567      /**
568       * Permits the testing of parallel code for the 3 different
569 <     * execution modes without repeating all the testing code.
569 >     * execution modes without copy/pasting all the test methods.
570       */
571      enum ExecutionMode {
572          DEFAULT {
573 +            public void checkExecutionMode() {
574 +                assertFalse(ThreadExecutor.startedCurrentThread());
575 +                assertNull(ForkJoinTask.getPool());
576 +            }
577 +            public CompletableFuture<Void> runAsync(Runnable a) {
578 +                throw new UnsupportedOperationException();
579 +            }
580 +            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
581 +                throw new UnsupportedOperationException();
582 +            }
583              public <T> CompletableFuture<Void> thenRun
584                  (CompletableFuture<T> f, Runnable a) {
585                  return f.thenRun(a);
# Line 521 | Line 643 | public class CompletableFutureTest exten
643              }
644          },
645  
646 <        DEFAULT_ASYNC {
646 >        ASYNC {
647 >            public void checkExecutionMode() {
648 >                assertSame(ForkJoinPool.commonPool(),
649 >                           ForkJoinTask.getPool());
650 >            }
651 >            public CompletableFuture<Void> runAsync(Runnable a) {
652 >                return CompletableFuture.runAsync(a);
653 >            }
654 >            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
655 >                return CompletableFuture.supplyAsync(a);
656 >            }
657              public <T> CompletableFuture<Void> thenRun
658                  (CompletableFuture<T> f, Runnable a) {
659                  return f.thenRunAsync(a);
# Line 586 | Line 718 | public class CompletableFutureTest exten
718          },
719  
720          EXECUTOR {
721 +            public void checkExecutionMode() {
722 +                assertTrue(ThreadExecutor.startedCurrentThread());
723 +            }
724 +            public CompletableFuture<Void> runAsync(Runnable a) {
725 +                return CompletableFuture.runAsync(a, new ThreadExecutor());
726 +            }
727 +            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
728 +                return CompletableFuture.supplyAsync(a, new ThreadExecutor());
729 +            }
730              public <T> CompletableFuture<Void> thenRun
731                  (CompletableFuture<T> f, Runnable a) {
732                  return f.thenRunAsync(a, new ThreadExecutor());
# Line 649 | Line 790 | public class CompletableFutureTest exten
790              }
791          };
792  
793 +        public abstract void checkExecutionMode();
794 +        public abstract CompletableFuture<Void> runAsync(Runnable a);
795 +        public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a);
796          public abstract <T> CompletableFuture<Void> thenRun
797              (CompletableFuture<T> f, Runnable a);
798          public abstract <T> CompletableFuture<Void> thenAccept
# Line 712 | Line 856 | public class CompletableFutureTest exten
856          assertEquals(0, a.get());
857      }}
858  
715
859      /**
860       * exceptionally action completes with function value on source
861       * exception
# Line 727 | Line 870 | public class CompletableFutureTest exten
870          if (!createIncomplete) f.completeExceptionally(ex);
871          final CompletableFuture<Integer> g = f.exceptionally
872              ((Throwable t) -> {
873 +                ExecutionMode.DEFAULT.checkExecutionMode();
874                  threadAssertSame(t, ex);
875                  a.getAndIncrement();
876                  return v1;
# Line 748 | Line 892 | public class CompletableFutureTest exten
892          if (!createIncomplete) f.completeExceptionally(ex1);
893          final CompletableFuture<Integer> g = f.exceptionally
894              ((Throwable t) -> {
895 +                ExecutionMode.DEFAULT.checkExecutionMode();
896                  threadAssertSame(t, ex1);
897                  a.getAndIncrement();
898                  throw ex2;
899              });
900          if (createIncomplete) f.completeExceptionally(ex1);
901  
902 <        checkCompletedWithWrappedCFException(g, ex2);
902 >        checkCompletedWithWrappedException(g, ex2);
903 >        assertEquals(1, a.get());
904 >    }}
905 >
906 >    /**
907 >     * whenComplete action executes on normal completion, propagating
908 >     * source result.
909 >     */
910 >    public void testWhenComplete_normalCompletion1() {
911 >        for (ExecutionMode m : ExecutionMode.values())
912 >        for (boolean createIncomplete : new boolean[] { true, false })
913 >        for (Integer v1 : new Integer[] { 1, null })
914 >    {
915 >        final AtomicInteger a = new AtomicInteger(0);
916 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
917 >        if (!createIncomplete) f.complete(v1);
918 >        final CompletableFuture<Integer> g = m.whenComplete
919 >            (f,
920 >             (Integer x, Throwable t) -> {
921 >                m.checkExecutionMode();
922 >                threadAssertSame(x, v1);
923 >                threadAssertNull(t);
924 >                a.getAndIncrement();
925 >            });
926 >        if (createIncomplete) f.complete(v1);
927 >
928 >        checkCompletedNormally(g, v1);
929 >        checkCompletedNormally(f, v1);
930 >        assertEquals(1, a.get());
931 >    }}
932 >
933 >    /**
934 >     * whenComplete action executes on exceptional completion, propagating
935 >     * source result.
936 >     */
937 >    public void testWhenComplete_exceptionalCompletion() {
938 >        for (ExecutionMode m : ExecutionMode.values())
939 >        for (boolean createIncomplete : new boolean[] { true, false })
940 >        for (Integer v1 : new Integer[] { 1, null })
941 >    {
942 >        final AtomicInteger a = new AtomicInteger(0);
943 >        final CFException ex = new CFException();
944 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
945 >        if (!createIncomplete) f.completeExceptionally(ex);
946 >        final CompletableFuture<Integer> g = m.whenComplete
947 >            (f,
948 >             (Integer x, Throwable t) -> {
949 >                m.checkExecutionMode();
950 >                threadAssertNull(x);
951 >                threadAssertSame(t, ex);
952 >                a.getAndIncrement();
953 >            });
954 >        if (createIncomplete) f.completeExceptionally(ex);
955 >
956 >        checkCompletedWithWrappedException(g, ex);
957 >        checkCompletedExceptionally(f, ex);
958 >        assertEquals(1, a.get());
959 >    }}
960 >
961 >    /**
962 >     * whenComplete action executes on cancelled source, propagating
963 >     * CancellationException.
964 >     */
965 >    public void testWhenComplete_sourceCancelled() {
966 >        for (ExecutionMode m : ExecutionMode.values())
967 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
968 >        for (boolean createIncomplete : new boolean[] { true, false })
969 >    {
970 >        final AtomicInteger a = new AtomicInteger(0);
971 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
972 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
973 >        final CompletableFuture<Integer> g = m.whenComplete
974 >            (f,
975 >             (Integer x, Throwable t) -> {
976 >                m.checkExecutionMode();
977 >                threadAssertNull(x);
978 >                threadAssertTrue(t instanceof CancellationException);
979 >                a.getAndIncrement();
980 >            });
981 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
982 >
983 >        checkCompletedWithWrappedCancellationException(g);
984 >        checkCancelled(f);
985 >        assertEquals(1, a.get());
986 >    }}
987 >
988 >    /**
989 >     * If a whenComplete action throws an exception when triggered by
990 >     * a normal completion, it completes exceptionally
991 >     */
992 >    public void testWhenComplete_actionFailed() {
993 >        for (boolean createIncomplete : new boolean[] { true, false })
994 >        for (ExecutionMode m : ExecutionMode.values())
995 >        for (Integer v1 : new Integer[] { 1, null })
996 >    {
997 >        final AtomicInteger a = new AtomicInteger(0);
998 >        final CFException ex = new CFException();
999 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1000 >        if (!createIncomplete) f.complete(v1);
1001 >        final CompletableFuture<Integer> g = m.whenComplete
1002 >            (f,
1003 >             (Integer x, Throwable t) -> {
1004 >                m.checkExecutionMode();
1005 >                threadAssertSame(x, v1);
1006 >                threadAssertNull(t);
1007 >                a.getAndIncrement();
1008 >                throw ex;
1009 >            });
1010 >        if (createIncomplete) f.complete(v1);
1011 >
1012 >        checkCompletedWithWrappedException(g, ex);
1013 >        checkCompletedNormally(f, v1);
1014 >        assertEquals(1, a.get());
1015 >    }}
1016 >
1017 >    /**
1018 >     * If a whenComplete action throws an exception when triggered by
1019 >     * a source completion that also throws an exception, the source
1020 >     * exception takes precedence.
1021 >     */
1022 >    public void testWhenComplete_actionFailedSourceFailed() {
1023 >        for (boolean createIncomplete : new boolean[] { true, false })
1024 >        for (ExecutionMode m : ExecutionMode.values())
1025 >        for (Integer v1 : new Integer[] { 1, null })
1026 >    {
1027 >        final AtomicInteger a = new AtomicInteger(0);
1028 >        final CFException ex1 = new CFException();
1029 >        final CFException ex2 = new CFException();
1030 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1031 >
1032 >        if (!createIncomplete) f.completeExceptionally(ex1);
1033 >        final CompletableFuture<Integer> g = m.whenComplete
1034 >            (f,
1035 >             (Integer x, Throwable t) -> {
1036 >                m.checkExecutionMode();
1037 >                threadAssertSame(t, ex1);
1038 >                threadAssertNull(x);
1039 >                a.getAndIncrement();
1040 >                throw ex2;
1041 >            });
1042 >        if (createIncomplete) f.completeExceptionally(ex1);
1043 >
1044 >        checkCompletedWithWrappedException(g, ex1);
1045 >        checkCompletedExceptionally(f, ex1);
1046          assertEquals(1, a.get());
1047      }}
1048  
# Line 773 | Line 1061 | public class CompletableFutureTest exten
1061          final CompletableFuture<Integer> g = m.handle
1062              (f,
1063               (Integer x, Throwable t) -> {
1064 +                m.checkExecutionMode();
1065                  threadAssertSame(x, v1);
1066                  threadAssertNull(t);
1067                  a.getAndIncrement();
# Line 801 | Line 1090 | public class CompletableFutureTest exten
1090          final CompletableFuture<Integer> g = m.handle
1091              (f,
1092               (Integer x, Throwable t) -> {
1093 +                m.checkExecutionMode();
1094                  threadAssertNull(x);
1095                  threadAssertSame(t, ex);
1096                  a.getAndIncrement();
# Line 809 | Line 1099 | public class CompletableFutureTest exten
1099          if (createIncomplete) f.completeExceptionally(ex);
1100  
1101          checkCompletedNormally(g, v1);
1102 <        checkCompletedWithWrappedCFException(f, ex);
1102 >        checkCompletedExceptionally(f, ex);
1103          assertEquals(1, a.get());
1104      }}
1105  
# Line 829 | Line 1119 | public class CompletableFutureTest exten
1119          final CompletableFuture<Integer> g = m.handle
1120              (f,
1121               (Integer x, Throwable t) -> {
1122 +                m.checkExecutionMode();
1123                  threadAssertNull(x);
1124                  threadAssertTrue(t instanceof CancellationException);
1125                  a.getAndIncrement();
# Line 856 | Line 1147 | public class CompletableFutureTest exten
1147          final CompletableFuture<Integer> g = m.handle
1148              (f,
1149               (Integer x, Throwable t) -> {
1150 +                m.checkExecutionMode();
1151                  threadAssertNull(x);
1152                  threadAssertSame(ex1, t);
1153                  a.getAndIncrement();
# Line 863 | Line 1155 | public class CompletableFutureTest exten
1155              });
1156          if (createIncomplete) f.completeExceptionally(ex1);
1157  
1158 <        checkCompletedWithWrappedCFException(g, ex2);
1159 <        checkCompletedWithWrappedCFException(f, ex1);
1158 >        checkCompletedWithWrappedException(g, ex2);
1159 >        checkCompletedExceptionally(f, ex1);
1160          assertEquals(1, a.get());
1161      }}
1162  
# Line 880 | Line 1172 | public class CompletableFutureTest exten
1172          final CompletableFuture<Integer> g = m.handle
1173              (f,
1174               (Integer x, Throwable t) -> {
1175 +                m.checkExecutionMode();
1176                  threadAssertSame(x, v1);
1177                  threadAssertNull(t);
1178                  a.getAndIncrement();
# Line 887 | Line 1180 | public class CompletableFutureTest exten
1180              });
1181          if (createIncomplete) f.complete(v1);
1182  
1183 <        checkCompletedWithWrappedCFException(g, ex);
1183 >        checkCompletedWithWrappedException(g, ex);
1184          checkCompletedNormally(f, v1);
1185          assertEquals(1, a.get());
1186      }}
# Line 895 | Line 1188 | public class CompletableFutureTest exten
1188      /**
1189       * runAsync completes after running Runnable
1190       */
1191 <    public void testRunAsync() {
1192 <        Noop r = new Noop();
1193 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
1194 <        assertNull(f.join());
1195 <        assertEquals(1, r.invocationCount);
1196 <        checkCompletedNormally(f, null);
1197 <    }
1198 <
1199 <    /**
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);
1191 >    public void testRunAsync_normalCompletion() {
1192 >        ExecutionMode[] executionModes = {
1193 >            ExecutionMode.ASYNC,
1194 >            ExecutionMode.EXECUTOR,
1195 >        };
1196 >        for (ExecutionMode m : executionModes)
1197 >    {
1198 >        final Noop r = new Noop(m);
1199 >        final CompletableFuture<Void> f = m.runAsync(r);
1200          assertNull(f.join());
914        assertEquals(1, r.invocationCount);
1201          checkCompletedNormally(f, null);
1202 <        assertEquals(1, exec.count.get());
1203 <    }
1202 >        r.assertInvoked();
1203 >    }}
1204  
1205      /**
1206       * failing runAsync completes exceptionally after running Runnable
1207       */
1208 <    public void testRunAsync3() {
1209 <        FailingNoop r = new FailingNoop();
1210 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
1208 >    public void testRunAsync_exceptionalCompletion() {
1209 >        ExecutionMode[] executionModes = {
1210 >            ExecutionMode.ASYNC,
1211 >            ExecutionMode.EXECUTOR,
1212 >        };
1213 >        for (ExecutionMode m : executionModes)
1214 >    {
1215 >        final FailingRunnable r = new FailingRunnable(m);
1216 >        final CompletableFuture<Void> f = m.runAsync(r);
1217          checkCompletedWithWrappedCFException(f);
1218 <        assertEquals(1, r.invocationCount);
1219 <    }
1218 >        r.assertInvoked();
1219 >    }}
1220  
1221      /**
1222       * supplyAsync completes with result of supplier
1223       */
1224 <    public void testSupplyAsync() {
1225 <        CompletableFuture<Integer> f;
1226 <        f = CompletableFuture.supplyAsync(supplyOne);
1227 <        assertEquals(f.join(), one);
1228 <        checkCompletedNormally(f, one);
1229 <    }
1230 <
1231 <    /**
1232 <     * supplyAsync with executor completes with result of supplier
1233 <     */
1234 <    public void testSupplyAsync2() {
1235 <        CompletableFuture<Integer> f;
1236 <        f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor());
1237 <        assertEquals(f.join(), one);
946 <        checkCompletedNormally(f, one);
947 <    }
1224 >    public void testSupplyAsync_normalCompletion() {
1225 >        ExecutionMode[] executionModes = {
1226 >            ExecutionMode.ASYNC,
1227 >            ExecutionMode.EXECUTOR,
1228 >        };
1229 >        for (ExecutionMode m : executionModes)
1230 >        for (Integer v1 : new Integer[] { 1, null })
1231 >    {
1232 >        final IntegerSupplier r = new IntegerSupplier(m, v1);
1233 >        final CompletableFuture<Integer> f = m.supplyAsync(r);
1234 >        assertSame(v1, f.join());
1235 >        checkCompletedNormally(f, v1);
1236 >        r.assertInvoked();
1237 >    }}
1238  
1239      /**
1240       * Failing supplyAsync completes exceptionally
1241       */
1242 <    public void testSupplyAsync3() {
1243 <        FailingSupplier r = new FailingSupplier();
1244 <        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r);
1242 >    public void testSupplyAsync_exceptionalCompletion() {
1243 >        ExecutionMode[] executionModes = {
1244 >            ExecutionMode.ASYNC,
1245 >            ExecutionMode.EXECUTOR,
1246 >        };
1247 >        for (ExecutionMode m : executionModes)
1248 >    {
1249 >        FailingSupplier r = new FailingSupplier(m);
1250 >        CompletableFuture<Integer> f = m.supplyAsync(r);
1251          checkCompletedWithWrappedCFException(f);
1252 <        assertEquals(1, r.invocationCount);
1253 <    }
1252 >        r.assertInvoked();
1253 >    }}
1254  
1255      // seq completion methods
1256  
1257      /**
1258       * thenRun result completes normally after normal completion of source
1259       */
1260 <    public void testThenRun() {
1261 <        CompletableFuture<Integer> f;
1262 <        CompletableFuture<Void> g;
1263 <        Noop r;
1264 <
1265 <        f = new CompletableFuture<>();
1266 <        g = f.thenRun(r = new Noop());
1267 <        f.complete(null);
1268 <        checkCompletedNormally(g, null);
1269 <        assertEquals(1, r.invocationCount);
1260 >    public void testThenRun_normalCompletion() {
1261 >        for (ExecutionMode m : ExecutionMode.values())
1262 >        for (boolean createIncomplete : new boolean[] { true, false })
1263 >        for (Integer v1 : new Integer[] { 1, null })
1264 >    {
1265 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1266 >        final Noop r = new Noop(m);
1267 >        if (!createIncomplete) f.complete(v1);
1268 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1269 >        if (createIncomplete) {
1270 >            checkIncomplete(g);
1271 >            f.complete(v1);
1272 >        }
1273  
975        f = new CompletableFuture<>();
976        f.complete(null);
977        g = f.thenRun(r = new Noop());
1274          checkCompletedNormally(g, null);
1275 <        assertEquals(1, r.invocationCount);
1276 <    }
1275 >        checkCompletedNormally(f, v1);
1276 >        r.assertInvoked();
1277 >    }}
1278  
1279      /**
1280       * thenRun result completes exceptionally after exceptional
1281       * completion of source
1282       */
1283 <    public void testThenRun2() {
1284 <        CompletableFuture<Integer> f;
1285 <        CompletableFuture<Void> g;
1286 <        Noop r;
1287 <
1288 <        f = new CompletableFuture<>();
1289 <        g = f.thenRun(r = new Noop());
1290 <        f.completeExceptionally(new CFException());
1291 <        checkCompletedWithWrappedCFException(g);
1292 <        assertEquals(0, r.invocationCount);
1293 <
1294 <        f = new CompletableFuture<>();
1295 <        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);
1283 >    public void testThenRun_exceptionalCompletion() {
1284 >        for (ExecutionMode m : ExecutionMode.values())
1285 >        for (boolean createIncomplete : new boolean[] { true, false })
1286 >    {
1287 >        final CFException ex = new CFException();
1288 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1289 >        final Noop r = new Noop(m);
1290 >        if (!createIncomplete) f.completeExceptionally(ex);
1291 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1292 >        if (createIncomplete) {
1293 >            checkIncomplete(g);
1294 >            f.completeExceptionally(ex);
1295 >        }
1296  
1297 <        f = new CompletableFuture<>();
1298 <        f.complete(null);
1299 <        g = f.thenRun(r = new FailingNoop());
1300 <        checkCompletedWithWrappedCFException(g);
1021 <    }
1297 >        checkCompletedWithWrappedException(g, ex);
1298 >        checkCompletedExceptionally(f, ex);
1299 >        r.assertNotInvoked();
1300 >    }}
1301  
1302      /**
1303       * thenRun result completes exceptionally if source cancelled
1304       */
1305 <    public void testThenRun4() {
1306 <        CompletableFuture<Integer> f;
1307 <        CompletableFuture<Void> g;
1308 <        Noop r;
1309 <
1310 <        f = new CompletableFuture<>();
1311 <        g = f.thenRun(r = new Noop());
1312 <        assertTrue(f.cancel(true));
1313 <        checkCompletedWithWrappedCancellationException(g);
1314 <
1315 <        f = new CompletableFuture<>();
1316 <        assertTrue(f.cancel(true));
1317 <        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 <    }
1305 >    public void testThenRun_sourceCancelled() {
1306 >        for (ExecutionMode m : ExecutionMode.values())
1307 >        for (boolean createIncomplete : new boolean[] { true, false })
1308 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1309 >    {
1310 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1311 >        final Noop r = new Noop(m);
1312 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1313 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1314 >        if (createIncomplete) {
1315 >            checkIncomplete(g);
1316 >            assertTrue(f.cancel(mayInterruptIfRunning));
1317 >        }
1318  
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));
1319          checkCompletedWithWrappedCancellationException(g);
1320 <    }
1321 <
1322 <    /**
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 <    }
1320 >        checkCancelled(f);
1321 >        r.assertNotInvoked();
1322 >    }}
1323  
1324      /**
1325 <     * thenAccept result completes exceptionally after exceptional
1097 <     * completion of source
1325 >     * thenRun result completes exceptionally if action does
1326       */
1327 <    public void testThenAccept2() {
1328 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1329 <        IncAction r = new IncAction();
1330 <        CompletableFuture<Void> g = f.thenAccept(r);
1331 <        f.completeExceptionally(new CFException());
1332 <        checkCompletedWithWrappedCFException(g);
1333 <    }
1327 >    public void testThenRun_actionFailed() {
1328 >        for (ExecutionMode m : ExecutionMode.values())
1329 >        for (boolean createIncomplete : new boolean[] { true, false })
1330 >        for (Integer v1 : new Integer[] { 1, null })
1331 >    {
1332 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1333 >        final FailingRunnable r = new FailingRunnable(m);
1334 >        if (!createIncomplete) f.complete(v1);
1335 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1336 >        if (createIncomplete) {
1337 >            checkIncomplete(g);
1338 >            f.complete(v1);
1339 >        }
1340  
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);
1341          checkCompletedWithWrappedCFException(g);
1342 <        assertEquals(1, r.invocationCount);
1343 <    }
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 <    }
1342 >        checkCompletedNormally(f, v1);
1343 >    }}
1344  
1345      /**
1346 <     * thenCombine result completes normally after normal completion
1132 <     * of sources
1346 >     * thenApply result completes normally after normal completion of source
1347       */
1348 <    public void testThenCombine_normalCompletion1() {
1135 <        for (boolean createIncomplete : new boolean[] { true, false })
1136 <        for (boolean fFirst : new boolean[] { true, false })
1348 >    public void testThenApply_normalCompletion() {
1349          for (ExecutionMode m : ExecutionMode.values())
1350 +        for (boolean createIncomplete : new boolean[] { true, false })
1351          for (Integer v1 : new Integer[] { 1, null })
1352 <        for (Integer v2 : new Integer[] { 2, null }) {
1140 <
1352 >    {
1353          final CompletableFuture<Integer> f = new CompletableFuture<>();
1354 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1355 <        final SubtractFunction r = new SubtractFunction();
1356 <        CompletableFuture<Integer> h = null;
1357 <        if (createIncomplete) h = m.thenCombine(f, g, r);
1358 <
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)
1354 >        final IncFunction r = new IncFunction(m);
1355 >        if (!createIncomplete) f.complete(v1);
1356 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1357 >        if (createIncomplete) {
1358 >            checkIncomplete(g);
1359              f.complete(v1);
1360 <        else
1156 <            g.complete(v2);
1157 <        if (!createIncomplete) h = m.thenCombine(f, g, r);
1360 >        }
1361  
1362 <        checkCompletedNormally(h, subtract(v1, v2));
1362 >        checkCompletedNormally(g, inc(v1));
1363          checkCompletedNormally(f, v1);
1364 <        checkCompletedNormally(g, v2);
1365 <        assertEquals(1, r.invocationCount);
1163 <        }
1164 <    }
1364 >        r.assertValue(inc(v1));
1365 >    }}
1366  
1367      /**
1368 <     * thenCombine result completes exceptionally after exceptional
1369 <     * completion of either source
1368 >     * thenApply result completes exceptionally after exceptional
1369 >     * completion of source
1370       */
1371 <    public void testThenCombine_exceptionalCompletion1() {
1371 >    public void testThenApply_exceptionalCompletion() {
1372          for (ExecutionMode m : ExecutionMode.values())
1373 <        for (Integer v1 : new Integer[] { 1, null }) {
1374 <
1174 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1175 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1176 <        final SubtractFunction r = new SubtractFunction();
1177 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1373 >        for (boolean createIncomplete : new boolean[] { true, false })
1374 >    {
1375          final CFException ex = new CFException();
1179
1180        f.completeExceptionally(ex);
1181        checkIncomplete(h);
1182        g.complete(v1);
1183
1184        checkCompletedWithWrappedCFException(h, ex);
1185        checkCompletedWithWrappedCFException(f, ex);
1186        assertEquals(0, r.invocationCount);
1187        checkCompletedNormally(g, v1);
1188        }
1189    }
1190
1191    public void testThenCombine_exceptionalCompletion2() {
1192        for (ExecutionMode m : ExecutionMode.values())
1193        for (Integer v1 : new Integer[] { 1, null }) {
1194
1376          final CompletableFuture<Integer> f = new CompletableFuture<>();
1377 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1378 <        final SubtractFunction r = new SubtractFunction();
1379 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1380 <        final CFException ex = new CFException();
1381 <
1382 <        g.completeExceptionally(ex);
1202 <        checkIncomplete(h);
1203 <        f.complete(v1);
1204 <
1205 <        checkCompletedWithWrappedCFException(h, ex);
1206 <        checkCompletedWithWrappedCFException(g, ex);
1207 <        assertEquals(0, r.invocationCount);
1208 <        checkCompletedNormally(f, v1);
1377 >        final IncFunction r = new IncFunction(m);
1378 >        if (!createIncomplete) f.completeExceptionally(ex);
1379 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1380 >        if (createIncomplete) {
1381 >            checkIncomplete(g);
1382 >            f.completeExceptionally(ex);
1383          }
1210    }
1211
1212    public void testThenCombine_exceptionalCompletion3() {
1213        for (ExecutionMode m : ExecutionMode.values())
1214        for (Integer v1 : new Integer[] { 1, null }) {
1215
1216        final CompletableFuture<Integer> f = new CompletableFuture<>();
1217        final CompletableFuture<Integer> g = new CompletableFuture<>();
1218        final SubtractFunction r = new SubtractFunction();
1219        final CFException ex = new CFException();
1220
1221        g.completeExceptionally(ex);
1222        f.complete(v1);
1223        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1384  
1385 <        checkCompletedWithWrappedCFException(h, ex);
1386 <        checkCompletedWithWrappedCFException(g, ex);
1387 <        assertEquals(0, r.invocationCount);
1388 <        checkCompletedNormally(f, v1);
1229 <        }
1230 <    }
1385 >        checkCompletedWithWrappedException(g, ex);
1386 >        checkCompletedExceptionally(f, ex);
1387 >        r.assertNotInvoked();
1388 >    }}
1389  
1390 <    public void testThenCombine_exceptionalCompletion4() {
1390 >    /**
1391 >     * thenApply result completes exceptionally if source cancelled
1392 >     */
1393 >    public void testThenApply_sourceCancelled() {
1394          for (ExecutionMode m : ExecutionMode.values())
1395 <        for (Integer v1 : new Integer[] { 1, null }) {
1396 <
1395 >        for (boolean createIncomplete : new boolean[] { true, false })
1396 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1397 >    {
1398          final CompletableFuture<Integer> f = new CompletableFuture<>();
1399 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1400 <        final SubtractFunction r = new SubtractFunction();
1401 <        final CFException ex = new CFException();
1402 <
1403 <        f.completeExceptionally(ex);
1404 <        g.complete(v1);
1243 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1244 <
1245 <        checkCompletedWithWrappedCFException(h, ex);
1246 <        checkCompletedWithWrappedCFException(f, ex);
1247 <        assertEquals(0, r.invocationCount);
1248 <        checkCompletedNormally(g, v1);
1399 >        final IncFunction r = new IncFunction(m);
1400 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1401 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1402 >        if (createIncomplete) {
1403 >            checkIncomplete(g);
1404 >            assertTrue(f.cancel(mayInterruptIfRunning));
1405          }
1406 <    }
1406 >
1407 >        checkCompletedWithWrappedCancellationException(g);
1408 >        checkCancelled(f);
1409 >        r.assertNotInvoked();
1410 >    }}
1411  
1412      /**
1413 <     * thenCombine result completes exceptionally if action does
1413 >     * thenApply result completes exceptionally if action does
1414       */
1415 <    public void testThenCombine_actionFailed1() {
1415 >    public void testThenApply_actionFailed() {
1416          for (ExecutionMode m : ExecutionMode.values())
1417 +        for (boolean createIncomplete : new boolean[] { true, false })
1418          for (Integer v1 : new Integer[] { 1, null })
1419 <        for (Integer v2 : new Integer[] { 2, null }) {
1259 <
1419 >    {
1420          final CompletableFuture<Integer> f = new CompletableFuture<>();
1421 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1422 <        final FailingBiFunction r = new FailingBiFunction();
1423 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1424 <
1425 <        f.complete(v1);
1426 <        checkIncomplete(h);
1427 <        g.complete(v2);
1421 >        final FailingFunction r = new FailingFunction(m);
1422 >        if (!createIncomplete) f.complete(v1);
1423 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1424 >        if (createIncomplete) {
1425 >            checkIncomplete(g);
1426 >            f.complete(v1);
1427 >        }
1428  
1429 <        checkCompletedWithWrappedCFException(h);
1429 >        checkCompletedWithWrappedCFException(g);
1430          checkCompletedNormally(f, v1);
1431 <        checkCompletedNormally(g, v2);
1272 <        }
1273 <    }
1431 >    }}
1432  
1433 <    public void testThenCombine_actionFailed2() {
1433 >    /**
1434 >     * thenAccept result completes normally after normal completion of source
1435 >     */
1436 >    public void testThenAccept_normalCompletion() {
1437          for (ExecutionMode m : ExecutionMode.values())
1438 +        for (boolean createIncomplete : new boolean[] { true, false })
1439          for (Integer v1 : new Integer[] { 1, null })
1440 <        for (Integer v2 : new Integer[] { 2, null }) {
1279 <
1440 >    {
1441          final CompletableFuture<Integer> f = new CompletableFuture<>();
1442 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1443 <        final FailingBiFunction r = new FailingBiFunction();
1444 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1445 <
1446 <        g.complete(v2);
1447 <        checkIncomplete(h);
1448 <        f.complete(v1);
1442 >        final NoopConsumer r = new NoopConsumer(m);
1443 >        if (!createIncomplete) f.complete(v1);
1444 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1445 >        if (createIncomplete) {
1446 >            checkIncomplete(g);
1447 >            f.complete(v1);
1448 >        }
1449  
1450 <        checkCompletedWithWrappedCFException(h);
1450 >        checkCompletedNormally(g, null);
1451 >        r.assertValue(v1);
1452          checkCompletedNormally(f, v1);
1453 <        checkCompletedNormally(g, v2);
1292 <        }
1293 <    }
1453 >    }}
1454  
1455      /**
1456 <     * thenCombine result completes exceptionally if either source cancelled
1456 >     * thenAccept result completes exceptionally after exceptional
1457 >     * completion of source
1458       */
1459 <    public void testThenCombine_sourceCancelled1() {
1459 >    public void testThenAccept_exceptionalCompletion() {
1460          for (ExecutionMode m : ExecutionMode.values())
1461 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1462 <        for (Integer v1 : new Integer[] { 1, null }) {
1463 <
1461 >        for (boolean createIncomplete : new boolean[] { true, false })
1462 >    {
1463 >        final CFException ex = new CFException();
1464          final CompletableFuture<Integer> f = new CompletableFuture<>();
1465 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1466 <        final SubtractFunction r = new SubtractFunction();
1467 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1468 <
1469 <        assertTrue(f.cancel(mayInterruptIfRunning));
1470 <        checkIncomplete(h);
1310 <        g.complete(v1);
1311 <
1312 <        checkCompletedWithWrappedCancellationException(h);
1313 <        checkCancelled(f);
1314 <        assertEquals(0, r.invocationCount);
1315 <        checkCompletedNormally(g, v1);
1465 >        final NoopConsumer r = new NoopConsumer(m);
1466 >        if (!createIncomplete) f.completeExceptionally(ex);
1467 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1468 >        if (createIncomplete) {
1469 >            checkIncomplete(g);
1470 >            f.completeExceptionally(ex);
1471          }
1317    }
1318
1319    public void testThenCombine_sourceCancelled2() {
1320        for (ExecutionMode m : ExecutionMode.values())
1321        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1322        for (Integer v1 : new Integer[] { 1, null }) {
1472  
1473 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1474 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1475 <        final SubtractFunction r = new SubtractFunction();
1476 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1328 <
1329 <        assertTrue(g.cancel(mayInterruptIfRunning));
1330 <        checkIncomplete(h);
1331 <        f.complete(v1);
1332 <
1333 <        checkCompletedWithWrappedCancellationException(h);
1334 <        checkCancelled(g);
1335 <        assertEquals(0, r.invocationCount);
1336 <        checkCompletedNormally(f, v1);
1337 <        }
1338 <    }
1473 >        checkCompletedWithWrappedException(g, ex);
1474 >        checkCompletedExceptionally(f, ex);
1475 >        r.assertNotInvoked();
1476 >    }}
1477  
1478 <    public void testThenCombine_sourceCancelled3() {
1478 >    /**
1479 >     * thenAccept result completes exceptionally if source cancelled
1480 >     */
1481 >    public void testThenAccept_sourceCancelled() {
1482          for (ExecutionMode m : ExecutionMode.values())
1483 +        for (boolean createIncomplete : new boolean[] { true, false })
1484          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1485 <        for (Integer v1 : new Integer[] { 1, null }) {
1344 <
1485 >    {
1486          final CompletableFuture<Integer> f = new CompletableFuture<>();
1487 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1488 <        final SubtractFunction r = new SubtractFunction();
1489 <
1490 <        assertTrue(g.cancel(mayInterruptIfRunning));
1491 <        f.complete(v1);
1492 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1352 <
1353 <        checkCompletedWithWrappedCancellationException(h);
1354 <        checkCancelled(g);
1355 <        assertEquals(0, r.invocationCount);
1356 <        checkCompletedNormally(f, v1);
1487 >        final NoopConsumer r = new NoopConsumer(m);
1488 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1489 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1490 >        if (createIncomplete) {
1491 >            checkIncomplete(g);
1492 >            assertTrue(f.cancel(mayInterruptIfRunning));
1493          }
1358    }
1359
1360    public void testThenCombine_sourceCancelled4() {
1361        for (ExecutionMode m : ExecutionMode.values())
1362        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1363        for (Integer v1 : new Integer[] { 1, null }) {
1364
1365        final CompletableFuture<Integer> f = new CompletableFuture<>();
1366        final CompletableFuture<Integer> g = new CompletableFuture<>();
1367        final SubtractFunction r = new SubtractFunction();
1368
1369        assertTrue(f.cancel(mayInterruptIfRunning));
1370        g.complete(v1);
1371        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1494  
1495 <        checkCompletedWithWrappedCancellationException(h);
1495 >        checkCompletedWithWrappedCancellationException(g);
1496          checkCancelled(f);
1497 <        assertEquals(0, r.invocationCount);
1498 <        checkCompletedNormally(g, v1);
1377 <        }
1378 <    }
1497 >        r.assertNotInvoked();
1498 >    }}
1499  
1500      /**
1501 <     * thenAcceptBoth result completes normally after normal
1382 <     * completion of sources
1501 >     * thenAccept result completes exceptionally if action does
1502       */
1503 <    public void testThenAcceptBoth_normalCompletion1() {
1503 >    public void testThenAccept_actionFailed() {
1504          for (ExecutionMode m : ExecutionMode.values())
1505 +        for (boolean createIncomplete : new boolean[] { true, false })
1506          for (Integer v1 : new Integer[] { 1, null })
1507 <        for (Integer v2 : new Integer[] { 2, null }) {
1388 <
1507 >    {
1508          final CompletableFuture<Integer> f = new CompletableFuture<>();
1509 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1510 <        final SubtractAction r = new SubtractAction();
1511 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1512 <
1513 <        f.complete(v1);
1514 <        checkIncomplete(h);
1396 <        assertEquals(0, r.invocationCount);
1397 <        g.complete(v2);
1398 <
1399 <        checkCompletedNormally(h, null);
1400 <        assertEquals(subtract(v1, v2), r.value);
1401 <        checkCompletedNormally(f, v1);
1402 <        checkCompletedNormally(g, v2);
1509 >        final FailingConsumer r = new FailingConsumer(m);
1510 >        if (!createIncomplete) f.complete(v1);
1511 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1512 >        if (createIncomplete) {
1513 >            checkIncomplete(g);
1514 >            f.complete(v1);
1515          }
1404    }
1405
1406    public void testThenAcceptBoth_normalCompletion2() {
1407        for (ExecutionMode m : ExecutionMode.values())
1408        for (Integer v1 : new Integer[] { 1, null })
1409        for (Integer v2 : new Integer[] { 2, null }) {
1516  
1517 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1412 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1413 <        final SubtractAction r = new SubtractAction();
1414 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1415 <
1416 <        g.complete(v2);
1417 <        checkIncomplete(h);
1418 <        assertEquals(0, r.invocationCount);
1419 <        f.complete(v1);
1420 <
1421 <        checkCompletedNormally(h, null);
1422 <        assertEquals(subtract(v1, v2), r.value);
1517 >        checkCompletedWithWrappedCFException(g);
1518          checkCompletedNormally(f, v1);
1519 <        checkCompletedNormally(g, v2);
1425 <        }
1426 <    }
1519 >    }}
1520  
1521 <    public void testThenAcceptBoth_normalCompletion3() {
1521 >    /**
1522 >     * thenCombine result completes normally after normal completion
1523 >     * of sources
1524 >     */
1525 >    public void testThenCombine_normalCompletion() {
1526          for (ExecutionMode m : ExecutionMode.values())
1527 +        for (boolean createIncomplete : new boolean[] { true, false })
1528 +        for (boolean fFirst : new boolean[] { true, false })
1529          for (Integer v1 : new Integer[] { 1, null })
1530 <        for (Integer v2 : new Integer[] { 2, null }) {
1531 <
1530 >        for (Integer v2 : new Integer[] { 2, null })
1531 >    {
1532          final CompletableFuture<Integer> f = new CompletableFuture<>();
1533          final CompletableFuture<Integer> g = new CompletableFuture<>();
1534 <        final SubtractAction r = new SubtractAction();
1534 >        final SubtractFunction r = new SubtractFunction(m);
1535  
1536 <        g.complete(v2);
1537 <        f.complete(v1);
1538 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1539 <
1540 <        checkCompletedNormally(h, null);
1541 <        assertEquals(subtract(v1, v2), r.value);
1542 <        checkCompletedNormally(f, v1);
1543 <        checkCompletedNormally(g, v2);
1536 >        if (fFirst) f.complete(v1); else g.complete(v2);
1537 >        if (!createIncomplete)
1538 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1539 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1540 >        if (createIncomplete) {
1541 >            checkIncomplete(h);
1542 >            r.assertNotInvoked();
1543 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1544          }
1446    }
1545  
1546 <    public void testThenAcceptBoth_normalCompletion4() {
1449 <        for (ExecutionMode m : ExecutionMode.values())
1450 <        for (Integer v1 : new Integer[] { 1, null })
1451 <        for (Integer v2 : new Integer[] { 2, null }) {
1452 <
1453 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1454 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1455 <        final SubtractAction r = new SubtractAction();
1456 <
1457 <        f.complete(v1);
1458 <        g.complete(v2);
1459 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1460 <
1461 <        checkCompletedNormally(h, null);
1462 <        assertEquals(subtract(v1, v2), r.value);
1546 >        checkCompletedNormally(h, subtract(v1, v2));
1547          checkCompletedNormally(f, v1);
1548          checkCompletedNormally(g, v2);
1549 <        }
1550 <    }
1549 >        r.assertValue(subtract(v1, v2));
1550 >    }}
1551  
1552      /**
1553 <     * thenAcceptBoth result completes exceptionally after exceptional
1553 >     * thenCombine result completes exceptionally after exceptional
1554       * completion of either source
1555       */
1556 <    public void testThenAcceptBoth_exceptionalCompletion1() {
1556 >    public void testThenCombine_exceptionalCompletion() {
1557          for (ExecutionMode m : ExecutionMode.values())
1558 <        for (Integer v1 : new Integer[] { 1, null }) {
1559 <
1558 >        for (boolean createIncomplete : new boolean[] { true, false })
1559 >        for (boolean fFirst : new boolean[] { true, false })
1560 >        for (Integer v1 : new Integer[] { 1, null })
1561 >    {
1562          final CompletableFuture<Integer> f = new CompletableFuture<>();
1563          final CompletableFuture<Integer> g = new CompletableFuture<>();
1478        final SubtractAction r = new SubtractAction();
1479        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1564          final CFException ex = new CFException();
1565 +        final SubtractFunction r = new SubtractFunction(m);
1566  
1567 <        f.completeExceptionally(ex);
1568 <        checkIncomplete(h);
1569 <        g.complete(v1);
1570 <
1571 <        checkCompletedWithWrappedCFException(h, ex);
1572 <        checkCompletedWithWrappedCFException(f, ex);
1573 <        assertEquals(0, r.invocationCount);
1489 <        checkCompletedNormally(g, v1);
1567 >        (fFirst ? f : g).complete(v1);
1568 >        if (!createIncomplete)
1569 >            (!fFirst ? f : g).completeExceptionally(ex);
1570 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1571 >        if (createIncomplete) {
1572 >            checkIncomplete(h);
1573 >            (!fFirst ? f : g).completeExceptionally(ex);
1574          }
1491    }
1492
1493    public void testThenAcceptBoth_exceptionalCompletion2() {
1494        for (ExecutionMode m : ExecutionMode.values())
1495        for (Integer v1 : new Integer[] { 1, null }) {
1575  
1576 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1577 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1578 <        final SubtractAction r = new SubtractAction();
1579 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1580 <        final CFException ex = new CFException();
1502 <
1503 <        g.completeExceptionally(ex);
1504 <        checkIncomplete(h);
1505 <        f.complete(v1);
1506 <
1507 <        checkCompletedWithWrappedCFException(h, ex);
1508 <        checkCompletedWithWrappedCFException(g, ex);
1509 <        assertEquals(0, r.invocationCount);
1510 <        checkCompletedNormally(f, v1);
1511 <        }
1512 <    }
1576 >        checkCompletedWithWrappedException(h, ex);
1577 >        r.assertNotInvoked();
1578 >        checkCompletedNormally(fFirst ? f : g, v1);
1579 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1580 >    }}
1581  
1582 <    public void testThenAcceptBoth_exceptionalCompletion3() {
1582 >    /**
1583 >     * thenCombine result completes exceptionally if either source cancelled
1584 >     */
1585 >    public void testThenCombine_sourceCancelled() {
1586          for (ExecutionMode m : ExecutionMode.values())
1587 <        for (Integer v1 : new Integer[] { 1, null }) {
1588 <
1587 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1588 >        for (boolean createIncomplete : new boolean[] { true, false })
1589 >        for (boolean fFirst : new boolean[] { true, false })
1590 >        for (Integer v1 : new Integer[] { 1, null })
1591 >    {
1592          final CompletableFuture<Integer> f = new CompletableFuture<>();
1593          final CompletableFuture<Integer> g = new CompletableFuture<>();
1594 <        final SubtractAction r = new SubtractAction();
1521 <        final CFException ex = new CFException();
1594 >        final SubtractFunction r = new SubtractFunction(m);
1595  
1596 <        g.completeExceptionally(ex);
1597 <        f.complete(v1);
1598 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1599 <
1600 <        checkCompletedWithWrappedCFException(h, ex);
1601 <        checkCompletedWithWrappedCFException(g, ex);
1602 <        assertEquals(0, r.invocationCount);
1530 <        checkCompletedNormally(f, v1);
1596 >        (fFirst ? f : g).complete(v1);
1597 >        if (!createIncomplete)
1598 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1599 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1600 >        if (createIncomplete) {
1601 >            checkIncomplete(h);
1602 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1603          }
1532    }
1533
1534    public void testThenAcceptBoth_exceptionalCompletion4() {
1535        for (ExecutionMode m : ExecutionMode.values())
1536        for (Integer v1 : new Integer[] { 1, null }) {
1537
1538        final CompletableFuture<Integer> f = new CompletableFuture<>();
1539        final CompletableFuture<Integer> g = new CompletableFuture<>();
1540        final SubtractAction r = new SubtractAction();
1541        final CFException ex = new CFException();
1542
1543        f.completeExceptionally(ex);
1544        g.complete(v1);
1545        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1604  
1605 <        checkCompletedWithWrappedCFException(h, ex);
1606 <        checkCompletedWithWrappedCFException(f, ex);
1607 <        assertEquals(0, r.invocationCount);
1608 <        checkCompletedNormally(g, v1);
1609 <        }
1552 <    }
1605 >        checkCompletedWithWrappedCancellationException(h);
1606 >        checkCancelled(!fFirst ? f : g);
1607 >        r.assertNotInvoked();
1608 >        checkCompletedNormally(fFirst ? f : g, v1);
1609 >    }}
1610  
1611      /**
1612 <     * thenAcceptBoth result completes exceptionally if action does
1612 >     * thenCombine result completes exceptionally if action does
1613       */
1614 <    public void testThenAcceptBoth_actionFailed1() {
1614 >    public void testThenCombine_actionFailed() {
1615          for (ExecutionMode m : ExecutionMode.values())
1616 +        for (boolean fFirst : new boolean[] { true, false })
1617          for (Integer v1 : new Integer[] { 1, null })
1618 <        for (Integer v2 : new Integer[] { 2, null }) {
1619 <
1618 >        for (Integer v2 : new Integer[] { 2, null })
1619 >    {
1620          final CompletableFuture<Integer> f = new CompletableFuture<>();
1621          final CompletableFuture<Integer> g = new CompletableFuture<>();
1622 <        final FailingBiConsumer r = new FailingBiConsumer();
1623 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1622 >        final FailingBiFunction r = new FailingBiFunction(m);
1623 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1624  
1625 <        f.complete(v1);
1626 <        checkIncomplete(h);
1627 <        g.complete(v2);
1625 >        if (fFirst) {
1626 >            f.complete(v1);
1627 >            g.complete(v2);
1628 >        } else {
1629 >            g.complete(v2);
1630 >            f.complete(v1);
1631 >        }
1632  
1633          checkCompletedWithWrappedCFException(h);
1634          checkCompletedNormally(f, v1);
1635          checkCompletedNormally(g, v2);
1636 <        }
1575 <    }
1636 >    }}
1637  
1638 <    public void testThenAcceptBoth_actionFailed2() {
1638 >    /**
1639 >     * thenAcceptBoth result completes normally after normal
1640 >     * completion of sources
1641 >     */
1642 >    public void testThenAcceptBoth_normalCompletion() {
1643          for (ExecutionMode m : ExecutionMode.values())
1644 +        for (boolean createIncomplete : new boolean[] { true, false })
1645 +        for (boolean fFirst : new boolean[] { true, false })
1646          for (Integer v1 : new Integer[] { 1, null })
1647 <        for (Integer v2 : new Integer[] { 2, null }) {
1648 <
1647 >        for (Integer v2 : new Integer[] { 2, null })
1648 >    {
1649          final CompletableFuture<Integer> f = new CompletableFuture<>();
1650          final CompletableFuture<Integer> g = new CompletableFuture<>();
1651 <        final FailingBiConsumer r = new FailingBiConsumer();
1585 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1651 >        final SubtractAction r = new SubtractAction(m);
1652  
1653 <        g.complete(v2);
1654 <        checkIncomplete(h);
1655 <        f.complete(v1);
1653 >        if (fFirst) f.complete(v1); else g.complete(v2);
1654 >        if (!createIncomplete)
1655 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1656 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1657 >        if (createIncomplete) {
1658 >            checkIncomplete(h);
1659 >            r.assertNotInvoked();
1660 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1661 >        }
1662  
1663 <        checkCompletedWithWrappedCFException(h);
1663 >        checkCompletedNormally(h, null);
1664 >        r.assertValue(subtract(v1, v2));
1665          checkCompletedNormally(f, v1);
1666          checkCompletedNormally(g, v2);
1667 <        }
1595 <    }
1667 >    }}
1668  
1669      /**
1670 <     * thenAcceptBoth result completes exceptionally if either source cancelled
1670 >     * thenAcceptBoth result completes exceptionally after exceptional
1671 >     * completion of either source
1672       */
1673 <    public void testThenAcceptBoth_sourceCancelled1() {
1673 >    public void testThenAcceptBoth_exceptionalCompletion() {
1674          for (ExecutionMode m : ExecutionMode.values())
1675 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1676 <        for (Integer v1 : new Integer[] { 1, null }) {
1677 <
1675 >        for (boolean createIncomplete : new boolean[] { true, false })
1676 >        for (boolean fFirst : new boolean[] { true, false })
1677 >        for (Integer v1 : new Integer[] { 1, null })
1678 >    {
1679          final CompletableFuture<Integer> f = new CompletableFuture<>();
1680          final CompletableFuture<Integer> g = new CompletableFuture<>();
1681 <        final SubtractAction r = new SubtractAction();
1682 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1609 <
1610 <        assertTrue(f.cancel(mayInterruptIfRunning));
1611 <        checkIncomplete(h);
1612 <        g.complete(v1);
1613 <
1614 <        checkCompletedWithWrappedCancellationException(h);
1615 <        checkCancelled(f);
1616 <        assertEquals(0, r.invocationCount);
1617 <        checkCompletedNormally(g, v1);
1618 <        }
1619 <    }
1620 <
1621 <    public void testThenAcceptBoth_sourceCancelled2() {
1622 <        for (ExecutionMode m : ExecutionMode.values())
1623 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1624 <        for (Integer v1 : new Integer[] { 1, null }) {
1681 >        final CFException ex = new CFException();
1682 >        final SubtractAction r = new SubtractAction(m);
1683  
1684 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1685 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1686 <        final SubtractAction r = new SubtractAction();
1684 >        (fFirst ? f : g).complete(v1);
1685 >        if (!createIncomplete)
1686 >            (!fFirst ? f : g).completeExceptionally(ex);
1687          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1688 <
1689 <        assertTrue(g.cancel(mayInterruptIfRunning));
1690 <        checkIncomplete(h);
1633 <        f.complete(v1);
1634 <
1635 <        checkCompletedWithWrappedCancellationException(h);
1636 <        checkCancelled(g);
1637 <        assertEquals(0, r.invocationCount);
1638 <        checkCompletedNormally(f, v1);
1688 >        if (createIncomplete) {
1689 >            checkIncomplete(h);
1690 >            (!fFirst ? f : g).completeExceptionally(ex);
1691          }
1640    }
1641
1642    public void testThenAcceptBoth_sourceCancelled3() {
1643        for (ExecutionMode m : ExecutionMode.values())
1644        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1645        for (Integer v1 : new Integer[] { 1, null }) {
1646
1647        final CompletableFuture<Integer> f = new CompletableFuture<>();
1648        final CompletableFuture<Integer> g = new CompletableFuture<>();
1649        final SubtractAction r = new SubtractAction();
1692  
1693 <        assertTrue(g.cancel(mayInterruptIfRunning));
1694 <        f.complete(v1);
1695 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1696 <
1697 <        checkCompletedWithWrappedCancellationException(h);
1656 <        checkCancelled(g);
1657 <        assertEquals(0, r.invocationCount);
1658 <        checkCompletedNormally(f, v1);
1659 <        }
1660 <    }
1693 >        checkCompletedWithWrappedException(h, ex);
1694 >        r.assertNotInvoked();
1695 >        checkCompletedNormally(fFirst ? f : g, v1);
1696 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1697 >    }}
1698  
1699 <    public void testThenAcceptBoth_sourceCancelled4() {
1699 >    /**
1700 >     * thenAcceptBoth result completes exceptionally if either source cancelled
1701 >     */
1702 >    public void testThenAcceptBoth_sourceCancelled() {
1703          for (ExecutionMode m : ExecutionMode.values())
1704          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1705 <        for (Integer v1 : new Integer[] { 1, null }) {
1706 <
1705 >        for (boolean createIncomplete : new boolean[] { true, false })
1706 >        for (boolean fFirst : new boolean[] { true, false })
1707 >        for (Integer v1 : new Integer[] { 1, null })
1708 >    {
1709          final CompletableFuture<Integer> f = new CompletableFuture<>();
1710          final CompletableFuture<Integer> g = new CompletableFuture<>();
1711 <        final SubtractAction r = new SubtractAction();
1711 >        final SubtractAction r = new SubtractAction(m);
1712  
1713 <        assertTrue(f.cancel(mayInterruptIfRunning));
1714 <        g.complete(v1);
1713 >        (fFirst ? f : g).complete(v1);
1714 >        if (!createIncomplete)
1715 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1716          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1717 +        if (createIncomplete) {
1718 +            checkIncomplete(h);
1719 +            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1720 +        }
1721  
1722          checkCompletedWithWrappedCancellationException(h);
1723 <        checkCancelled(f);
1724 <        assertEquals(0, r.invocationCount);
1725 <        checkCompletedNormally(g, v1);
1726 <        }
1680 <    }
1723 >        checkCancelled(!fFirst ? f : g);
1724 >        r.assertNotInvoked();
1725 >        checkCompletedNormally(fFirst ? f : g, v1);
1726 >    }}
1727  
1728      /**
1729 <     * runAfterBoth result completes normally after normal
1684 <     * completion of sources
1729 >     * thenAcceptBoth result completes exceptionally if action does
1730       */
1731 <    public void testRunAfterBoth_normalCompletion1() {
1731 >    public void testThenAcceptBoth_actionFailed() {
1732          for (ExecutionMode m : ExecutionMode.values())
1733 +        for (boolean fFirst : new boolean[] { true, false })
1734          for (Integer v1 : new Integer[] { 1, null })
1735 <        for (Integer v2 : new Integer[] { 2, null }) {
1736 <
1735 >        for (Integer v2 : new Integer[] { 2, null })
1736 >    {
1737          final CompletableFuture<Integer> f = new CompletableFuture<>();
1738          final CompletableFuture<Integer> g = new CompletableFuture<>();
1739 <        final Noop r = new Noop();
1740 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1695 <
1696 <        f.complete(v1);
1697 <        checkIncomplete(h);
1698 <        assertEquals(0, r.invocationCount);
1699 <        g.complete(v2);
1739 >        final FailingBiConsumer r = new FailingBiConsumer(m);
1740 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1741  
1742 <        checkCompletedNormally(h, null);
1743 <        assertEquals(1, r.invocationCount);
1744 <        checkCompletedNormally(f, v1);
1745 <        checkCompletedNormally(g, v2);
1742 >        if (fFirst) {
1743 >            f.complete(v1);
1744 >            g.complete(v2);
1745 >        } else {
1746 >            g.complete(v2);
1747 >            f.complete(v1);
1748          }
1706    }
1707
1708    public void testRunAfterBoth_normalCompletion2() {
1709        for (ExecutionMode m : ExecutionMode.values())
1710        for (Integer v1 : new Integer[] { 1, null })
1711        for (Integer v2 : new Integer[] { 2, null }) {
1712
1713        final CompletableFuture<Integer> f = new CompletableFuture<>();
1714        final CompletableFuture<Integer> g = new CompletableFuture<>();
1715        final Noop r = new Noop();
1716        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1717
1718        g.complete(v2);
1719        checkIncomplete(h);
1720        assertEquals(0, r.invocationCount);
1721        f.complete(v1);
1749  
1750 <        checkCompletedNormally(h, null);
1724 <        assertEquals(1, r.invocationCount);
1750 >        checkCompletedWithWrappedCFException(h);
1751          checkCompletedNormally(f, v1);
1752          checkCompletedNormally(g, v2);
1753 <        }
1728 <    }
1753 >    }}
1754  
1755 <    public void testRunAfterBoth_normalCompletion3() {
1755 >    /**
1756 >     * runAfterBoth result completes normally after normal
1757 >     * completion of sources
1758 >     */
1759 >    public void testRunAfterBoth_normalCompletion() {
1760          for (ExecutionMode m : ExecutionMode.values())
1761 +        for (boolean createIncomplete : new boolean[] { true, false })
1762 +        for (boolean fFirst : new boolean[] { true, false })
1763          for (Integer v1 : new Integer[] { 1, null })
1764 <        for (Integer v2 : new Integer[] { 2, null }) {
1765 <
1764 >        for (Integer v2 : new Integer[] { 2, null })
1765 >    {
1766          final CompletableFuture<Integer> f = new CompletableFuture<>();
1767          final CompletableFuture<Integer> g = new CompletableFuture<>();
1768 <        final Noop r = new Noop();
1768 >        final Noop r = new Noop(m);
1769  
1770 <        g.complete(v2);
1771 <        f.complete(v1);
1770 >        if (fFirst) f.complete(v1); else g.complete(v2);
1771 >        if (!createIncomplete)
1772 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1773          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1774 <
1775 <        checkCompletedNormally(h, null);
1776 <        assertEquals(1, r.invocationCount);
1777 <        checkCompletedNormally(f, v1);
1746 <        checkCompletedNormally(g, v2);
1774 >        if (createIncomplete) {
1775 >            checkIncomplete(h);
1776 >            r.assertNotInvoked();
1777 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1778          }
1748    }
1749
1750    public void testRunAfterBoth_normalCompletion4() {
1751        for (ExecutionMode m : ExecutionMode.values())
1752        for (Integer v1 : new Integer[] { 1, null })
1753        for (Integer v2 : new Integer[] { 2, null }) {
1754
1755        final CompletableFuture<Integer> f = new CompletableFuture<>();
1756        final CompletableFuture<Integer> g = new CompletableFuture<>();
1757        final Noop r = new Noop();
1758
1759        f.complete(v1);
1760        g.complete(v2);
1761        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1779  
1780          checkCompletedNormally(h, null);
1781 <        assertEquals(1, r.invocationCount);
1781 >        r.assertInvoked();
1782          checkCompletedNormally(f, v1);
1783          checkCompletedNormally(g, v2);
1784 <        }
1768 <    }
1784 >    }}
1785  
1786      /**
1787       * runAfterBoth result completes exceptionally after exceptional
1788       * completion of either source
1789       */
1790 <    public void testRunAfterBoth_exceptionalCompletion1() {
1775 <        for (ExecutionMode m : ExecutionMode.values())
1776 <        for (Integer v1 : new Integer[] { 1, null }) {
1777 <
1778 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1779 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1780 <        final Noop r = new Noop();
1781 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1782 <        final CFException ex = new CFException();
1783 <
1784 <        f.completeExceptionally(ex);
1785 <        checkIncomplete(h);
1786 <        g.complete(v1);
1787 <
1788 <        checkCompletedWithWrappedCFException(h, ex);
1789 <        checkCompletedWithWrappedCFException(f, ex);
1790 <        assertEquals(0, r.invocationCount);
1791 <        checkCompletedNormally(g, v1);
1792 <        }
1793 <    }
1794 <
1795 <    public void testRunAfterBoth_exceptionalCompletion2() {
1796 <        for (ExecutionMode m : ExecutionMode.values())
1797 <        for (Integer v1 : new Integer[] { 1, null }) {
1798 <
1799 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1800 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1801 <        final Noop r = new Noop();
1802 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1803 <        final CFException ex = new CFException();
1804 <
1805 <        g.completeExceptionally(ex);
1806 <        checkIncomplete(h);
1807 <        f.complete(v1);
1808 <
1809 <        checkCompletedWithWrappedCFException(h, ex);
1810 <        checkCompletedWithWrappedCFException(g, ex);
1811 <        assertEquals(0, r.invocationCount);
1812 <        checkCompletedNormally(f, v1);
1813 <        }
1814 <    }
1815 <
1816 <    public void testRunAfterBoth_exceptionalCompletion3() {
1817 <        for (ExecutionMode m : ExecutionMode.values())
1818 <        for (Integer v1 : new Integer[] { 1, null }) {
1819 <
1820 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1821 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1822 <        final Noop r = new Noop();
1823 <        final CFException ex = new CFException();
1824 <
1825 <        g.completeExceptionally(ex);
1826 <        f.complete(v1);
1827 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1828 <
1829 <        checkCompletedWithWrappedCFException(h, ex);
1830 <        checkCompletedWithWrappedCFException(g, ex);
1831 <        assertEquals(0, r.invocationCount);
1832 <        checkCompletedNormally(f, v1);
1833 <        }
1834 <    }
1835 <
1836 <    public void testRunAfterBoth_exceptionalCompletion4() {
1790 >    public void testRunAfterBoth_exceptionalCompletion() {
1791          for (ExecutionMode m : ExecutionMode.values())
1792 <        for (Integer v1 : new Integer[] { 1, null }) {
1793 <
1792 >        for (boolean createIncomplete : new boolean[] { true, false })
1793 >        for (boolean fFirst : new boolean[] { true, false })
1794 >        for (Integer v1 : new Integer[] { 1, null })
1795 >    {
1796          final CompletableFuture<Integer> f = new CompletableFuture<>();
1797          final CompletableFuture<Integer> g = new CompletableFuture<>();
1842        final Noop r = new Noop();
1798          final CFException ex = new CFException();
1799 +        final Noop r = new Noop(m);
1800  
1801 <        f.completeExceptionally(ex);
1802 <        g.complete(v1);
1801 >        (fFirst ? f : g).complete(v1);
1802 >        if (!createIncomplete)
1803 >            (!fFirst ? f : g).completeExceptionally(ex);
1804          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1805 <
1806 <        checkCompletedWithWrappedCFException(h, ex);
1807 <        checkCompletedWithWrappedCFException(f, ex);
1851 <        assertEquals(0, r.invocationCount);
1852 <        checkCompletedNormally(g, v1);
1805 >        if (createIncomplete) {
1806 >            checkIncomplete(h);
1807 >            (!fFirst ? f : g).completeExceptionally(ex);
1808          }
1854    }
1809  
1810 <    /**
1811 <     * runAfterBoth result completes exceptionally if action does
1812 <     */
1813 <    public void testRunAfterBoth_actionFailed1() {
1814 <        for (ExecutionMode m : ExecutionMode.values())
1861 <        for (Integer v1 : new Integer[] { 1, null })
1862 <        for (Integer v2 : new Integer[] { 2, null }) {
1863 <
1864 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1865 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1866 <        final FailingNoop r = new FailingNoop();
1867 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1868 <
1869 <        f.complete(v1);
1870 <        checkIncomplete(h);
1871 <        g.complete(v2);
1872 <
1873 <        checkCompletedWithWrappedCFException(h);
1874 <        checkCompletedNormally(f, v1);
1875 <        checkCompletedNormally(g, v2);
1876 <        }
1877 <    }
1878 <
1879 <    public void testRunAfterBoth_actionFailed2() {
1880 <        for (ExecutionMode m : ExecutionMode.values())
1881 <        for (Integer v1 : new Integer[] { 1, null })
1882 <        for (Integer v2 : new Integer[] { 2, null }) {
1883 <
1884 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1885 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1886 <        final FailingNoop r = new FailingNoop();
1887 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1888 <
1889 <        g.complete(v2);
1890 <        checkIncomplete(h);
1891 <        f.complete(v1);
1892 <
1893 <        checkCompletedWithWrappedCFException(h);
1894 <        checkCompletedNormally(f, v1);
1895 <        checkCompletedNormally(g, v2);
1896 <        }
1897 <    }
1810 >        checkCompletedWithWrappedException(h, ex);
1811 >        r.assertNotInvoked();
1812 >        checkCompletedNormally(fFirst ? f : g, v1);
1813 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1814 >    }}
1815  
1816      /**
1817       * runAfterBoth result completes exceptionally if either source cancelled
1818       */
1819 <    public void testRunAfterBoth_sourceCancelled1() {
1819 >    public void testRunAfterBoth_sourceCancelled() {
1820          for (ExecutionMode m : ExecutionMode.values())
1821          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1822 <        for (Integer v1 : new Integer[] { 1, null }) {
1823 <
1824 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1825 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1909 <        final Noop r = new Noop();
1910 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1911 <
1912 <        assertTrue(f.cancel(mayInterruptIfRunning));
1913 <        checkIncomplete(h);
1914 <        g.complete(v1);
1915 <
1916 <        checkCompletedWithWrappedCancellationException(h);
1917 <        checkCancelled(f);
1918 <        assertEquals(0, r.invocationCount);
1919 <        checkCompletedNormally(g, v1);
1920 <        }
1921 <    }
1922 <
1923 <    public void testRunAfterBoth_sourceCancelled2() {
1924 <        for (ExecutionMode m : ExecutionMode.values())
1925 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1926 <        for (Integer v1 : new Integer[] { 1, null }) {
1927 <
1928 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1929 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1930 <        final Noop r = new Noop();
1931 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1932 <
1933 <        assertTrue(g.cancel(mayInterruptIfRunning));
1934 <        checkIncomplete(h);
1935 <        f.complete(v1);
1936 <
1937 <        checkCompletedWithWrappedCancellationException(h);
1938 <        checkCancelled(g);
1939 <        assertEquals(0, r.invocationCount);
1940 <        checkCompletedNormally(f, v1);
1941 <        }
1942 <    }
1943 <
1944 <    public void testRunAfterBoth_sourceCancelled3() {
1945 <        for (ExecutionMode m : ExecutionMode.values())
1946 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1947 <        for (Integer v1 : new Integer[] { 1, null }) {
1948 <
1822 >        for (boolean createIncomplete : new boolean[] { true, false })
1823 >        for (boolean fFirst : new boolean[] { true, false })
1824 >        for (Integer v1 : new Integer[] { 1, null })
1825 >    {
1826          final CompletableFuture<Integer> f = new CompletableFuture<>();
1827          final CompletableFuture<Integer> g = new CompletableFuture<>();
1828 <        final Noop r = new Noop();
1828 >        final Noop r = new Noop(m);
1829  
1830 <        assertTrue(g.cancel(mayInterruptIfRunning));
1831 <        f.complete(v1);
1830 >        (fFirst ? f : g).complete(v1);
1831 >        if (!createIncomplete)
1832 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1833          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1834 <
1835 <        checkCompletedWithWrappedCancellationException(h);
1836 <        checkCancelled(g);
1959 <        assertEquals(0, r.invocationCount);
1960 <        checkCompletedNormally(f, v1);
1834 >        if (createIncomplete) {
1835 >            checkIncomplete(h);
1836 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1837          }
1962    }
1963
1964    public void testRunAfterBoth_sourceCancelled4() {
1965        for (ExecutionMode m : ExecutionMode.values())
1966        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1967        for (Integer v1 : new Integer[] { 1, null }) {
1968
1969        final CompletableFuture<Integer> f = new CompletableFuture<>();
1970        final CompletableFuture<Integer> g = new CompletableFuture<>();
1971        final Noop r = new Noop();
1972
1973        assertTrue(f.cancel(mayInterruptIfRunning));
1974        g.complete(v1);
1975        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1838  
1839          checkCompletedWithWrappedCancellationException(h);
1840 <        checkCancelled(f);
1841 <        assertEquals(0, r.invocationCount);
1842 <        checkCompletedNormally(g, v1);
1843 <        }
1982 <    }
1840 >        checkCancelled(!fFirst ? f : g);
1841 >        r.assertNotInvoked();
1842 >        checkCompletedNormally(fFirst ? f : g, v1);
1843 >    }}
1844  
1845      /**
1846 <     * applyToEither result completes normally after normal completion
1986 <     * of either source
1846 >     * runAfterBoth result completes exceptionally if action does
1847       */
1848 <    public void testApplyToEither_normalCompletion1() {
1848 >    public void testRunAfterBoth_actionFailed() {
1849          for (ExecutionMode m : ExecutionMode.values())
1850 +        for (boolean fFirst : new boolean[] { true, false })
1851          for (Integer v1 : new Integer[] { 1, null })
1852 <        for (Integer v2 : new Integer[] { 2, null }) {
1853 <
1852 >        for (Integer v2 : new Integer[] { 2, null })
1853 >    {
1854          final CompletableFuture<Integer> f = new CompletableFuture<>();
1855          final CompletableFuture<Integer> g = new CompletableFuture<>();
1856 <        final IncFunction r = new IncFunction();
1857 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1856 >        final FailingRunnable r1 = new FailingRunnable(m);
1857 >        final FailingRunnable r2 = new FailingRunnable(m);
1858  
1859 <        f.complete(v1);
1860 <        checkCompletedNormally(h, inc(v1));
1861 <        g.complete(v2);
1862 <
1863 <        checkCompletedNormally(f, v1);
1864 <        checkCompletedNormally(g, v2);
1865 <        checkCompletedNormally(h, inc(v1));
1859 >        CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1860 >        if (fFirst) {
1861 >            f.complete(v1);
1862 >            g.complete(v2);
1863 >        } else {
1864 >            g.complete(v2);
1865 >            f.complete(v1);
1866          }
1867 <    }
2007 <
2008 <    public void testApplyToEither_normalCompletion2() {
2009 <        for (ExecutionMode m : ExecutionMode.values())
2010 <        for (Integer v1 : new Integer[] { 1, null })
2011 <        for (Integer v2 : new Integer[] { 2, null }) {
2012 <
2013 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2014 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2015 <        final IncFunction r = new IncFunction();
2016 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2017 <
2018 <        g.complete(v2);
2019 <        checkCompletedNormally(h, inc(v2));
2020 <        f.complete(v1);
1867 >        CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1868  
1869 +        checkCompletedWithWrappedCFException(h1);
1870 +        checkCompletedWithWrappedCFException(h2);
1871          checkCompletedNormally(f, v1);
1872          checkCompletedNormally(g, v2);
1873 <        checkCompletedNormally(h, inc(v2));
1874 <        }
1875 <    }
1876 <    public void testApplyToEither_normalCompletion3() {
1873 >    }}
1874 >
1875 >    /**
1876 >     * applyToEither result completes normally after normal completion
1877 >     * of either source
1878 >     */
1879 >    public void testApplyToEither_normalCompletion() {
1880          for (ExecutionMode m : ExecutionMode.values())
1881          for (Integer v1 : new Integer[] { 1, null })
1882 <        for (Integer v2 : new Integer[] { 2, null }) {
1883 <
1882 >        for (Integer v2 : new Integer[] { 2, null })
1883 >    {
1884          final CompletableFuture<Integer> f = new CompletableFuture<>();
1885          final CompletableFuture<Integer> g = new CompletableFuture<>();
1886 <        final IncFunction r = new IncFunction();
1886 >        final IncFunction[] rs = new IncFunction[6];
1887 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1888  
1889 <        f.complete(v1);
1890 <        g.complete(v2);
1891 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1889 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1890 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1891 >        checkIncomplete(h0);
1892 >        checkIncomplete(h1);
1893 >        rs[0].assertNotInvoked();
1894 >        rs[1].assertNotInvoked();
1895 >        f.complete(v1);
1896 >        checkCompletedNormally(h0, inc(v1));
1897 >        checkCompletedNormally(h1, inc(v1));
1898 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1899 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1900 >        checkCompletedNormally(h2, inc(v1));
1901 >        checkCompletedNormally(h3, inc(v1));
1902 >        g.complete(v2);
1903 >
1904 >        // unspecified behavior - both source completions available
1905 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
1906 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
1907 >        rs[4].assertValue(h4.join());
1908 >        rs[5].assertValue(h5.join());
1909 >        assertTrue(Objects.equals(inc(v1), h4.join()) ||
1910 >                   Objects.equals(inc(v2), h4.join()));
1911 >        assertTrue(Objects.equals(inc(v1), h5.join()) ||
1912 >                   Objects.equals(inc(v2), h5.join()));
1913  
1914          checkCompletedNormally(f, v1);
1915          checkCompletedNormally(g, v2);
1916 <
1917 <        // unspecified behavior
1918 <        assertTrue(Objects.equals(h.join(), inc(v1)) ||
1919 <                   Objects.equals(h.join(), inc(v2)));
1920 <        assertEquals(1, r.invocationCount);
1921 <        }
2048 <    }
1916 >        checkCompletedNormally(h0, inc(v1));
1917 >        checkCompletedNormally(h1, inc(v1));
1918 >        checkCompletedNormally(h2, inc(v1));
1919 >        checkCompletedNormally(h3, inc(v1));
1920 >        for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1));
1921 >    }}
1922  
1923      /**
1924       * applyToEither result completes exceptionally after exceptional
1925       * completion of either source
1926       */
1927 <    public void testApplyToEither_exceptionalCompletion1() {
1927 >    public void testApplyToEither_exceptionalCompletion() {
1928          for (ExecutionMode m : ExecutionMode.values())
1929 <        for (Integer v1 : new Integer[] { 1, null }) {
1930 <
1929 >        for (Integer v1 : new Integer[] { 1, null })
1930 >    {
1931          final CompletableFuture<Integer> f = new CompletableFuture<>();
1932          final CompletableFuture<Integer> g = new CompletableFuture<>();
2060        final IncFunction r = new IncFunction();
2061        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1933          final CFException ex = new CFException();
1934 +        final IncFunction[] rs = new IncFunction[6];
1935 +        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1936  
1937 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1938 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1939 +        checkIncomplete(h0);
1940 +        checkIncomplete(h1);
1941 +        rs[0].assertNotInvoked();
1942 +        rs[1].assertNotInvoked();
1943          f.completeExceptionally(ex);
1944 <        checkCompletedWithWrappedCFException(h, ex);
1944 >        checkCompletedWithWrappedException(h0, ex);
1945 >        checkCompletedWithWrappedException(h1, ex);
1946 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1947 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1948 >        checkCompletedWithWrappedException(h2, ex);
1949 >        checkCompletedWithWrappedException(h3, ex);
1950          g.complete(v1);
1951  
1952 <        assertEquals(0, r.invocationCount);
1953 <        checkCompletedNormally(g, v1);
1954 <        checkCompletedWithWrappedCFException(f, ex);
1955 <        checkCompletedWithWrappedCFException(h, ex);
1952 >        // unspecified behavior - both source completions available
1953 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
1954 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
1955 >        try {
1956 >            assertEquals(inc(v1), h4.join());
1957 >            rs[4].assertValue(inc(v1));
1958 >        } catch (CompletionException ok) {
1959 >            checkCompletedWithWrappedException(h4, ex);
1960 >            rs[4].assertNotInvoked();
1961          }
1962 <    }
1962 >        try {
1963 >            assertEquals(inc(v1), h5.join());
1964 >            rs[5].assertValue(inc(v1));
1965 >        } catch (CompletionException ok) {
1966 >            checkCompletedWithWrappedException(h5, ex);
1967 >            rs[5].assertNotInvoked();
1968 >        }
1969 >
1970 >        checkCompletedExceptionally(f, ex);
1971 >        checkCompletedNormally(g, v1);
1972 >        checkCompletedWithWrappedException(h0, ex);
1973 >        checkCompletedWithWrappedException(h1, ex);
1974 >        checkCompletedWithWrappedException(h2, ex);
1975 >        checkCompletedWithWrappedException(h3, ex);
1976 >        checkCompletedWithWrappedException(h4, ex);
1977 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
1978 >    }}
1979  
1980      public void testApplyToEither_exceptionalCompletion2() {
1981          for (ExecutionMode m : ExecutionMode.values())
1982 <        for (Integer v1 : new Integer[] { 1, null }) {
1983 <
1982 >        for (boolean fFirst : new boolean[] { true, false })
1983 >        for (Integer v1 : new Integer[] { 1, null })
1984 >    {
1985          final CompletableFuture<Integer> f = new CompletableFuture<>();
1986          final CompletableFuture<Integer> g = new CompletableFuture<>();
2081        final IncFunction r = new IncFunction();
2082        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1987          final CFException ex = new CFException();
1988 +        final IncFunction[] rs = new IncFunction[6];
1989 +        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1990  
1991 <        g.completeExceptionally(ex);
1992 <        checkCompletedWithWrappedCFException(h, ex);
1993 <        f.complete(v1);
1994 <
1995 <        assertEquals(0, r.invocationCount);
1996 <        checkCompletedNormally(f, v1);
1997 <        checkCompletedWithWrappedCFException(g, ex);
1998 <        checkCompletedWithWrappedCFException(h, ex);
1991 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1992 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1993 >        if (fFirst) {
1994 >            f.complete(v1);
1995 >            g.completeExceptionally(ex);
1996 >        } else {
1997 >            g.completeExceptionally(ex);
1998 >            f.complete(v1);
1999          }
2000 <    }
2001 <
2096 <    public void testApplyToEither_exceptionalCompletion3() {
2097 <        for (ExecutionMode m : ExecutionMode.values())
2098 <        for (Integer v1 : new Integer[] { 1, null }) {
2099 <
2100 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2101 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2102 <        final IncFunction r = new IncFunction();
2103 <        final CFException ex = new CFException();
2104 <
2105 <        g.completeExceptionally(ex);
2106 <        f.complete(v1);
2107 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2000 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2001 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2002  
2003 <        // unspecified behavior
2110 <        Integer v;
2003 >        // unspecified behavior - both source completions available
2004          try {
2005 <            assertEquals(inc(v1), h.join());
2006 <            assertEquals(1, r.invocationCount);
2005 >            assertEquals(inc(v1), h0.join());
2006 >            rs[0].assertValue(inc(v1));
2007          } catch (CompletionException ok) {
2008 <            checkCompletedWithWrappedCFException(h, ex);
2009 <            assertEquals(0, r.invocationCount);
2008 >            checkCompletedWithWrappedException(h0, ex);
2009 >            rs[0].assertNotInvoked();
2010          }
2118
2119        checkCompletedWithWrappedCFException(g, ex);
2120        checkCompletedNormally(f, v1);
2121        }
2122    }
2123
2124    public void testApplyToEither_exceptionalCompletion4() {
2125        for (ExecutionMode m : ExecutionMode.values())
2126        for (Integer v1 : new Integer[] { 1, null }) {
2127
2128        final CompletableFuture<Integer> f = new CompletableFuture<>();
2129        final CompletableFuture<Integer> g = new CompletableFuture<>();
2130        final IncFunction r = new IncFunction();
2131        final CFException ex = new CFException();
2132
2133        f.completeExceptionally(ex);
2134        g.complete(v1);
2135        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2136
2137        // unspecified behavior
2138        Integer v;
2011          try {
2012 <            assertEquals(inc(v1), h.join());
2013 <            assertEquals(1, r.invocationCount);
2012 >            assertEquals(inc(v1), h1.join());
2013 >            rs[1].assertValue(inc(v1));
2014          } catch (CompletionException ok) {
2015 <            checkCompletedWithWrappedCFException(h, ex);
2016 <            assertEquals(0, r.invocationCount);
2015 >            checkCompletedWithWrappedException(h1, ex);
2016 >            rs[1].assertNotInvoked();
2017          }
2018 <
2019 <        checkCompletedWithWrappedCFException(f, ex);
2020 <        checkCompletedNormally(g, v1);
2018 >        try {
2019 >            assertEquals(inc(v1), h2.join());
2020 >            rs[2].assertValue(inc(v1));
2021 >        } catch (CompletionException ok) {
2022 >            checkCompletedWithWrappedException(h2, ex);
2023 >            rs[2].assertNotInvoked();
2024          }
2025 <    }
2026 <
2027 <    /**
2028 <     * applyToEither result completes exceptionally if action does
2029 <     */
2030 <    public void testApplyToEither_actionFailed1() {
2156 <        for (ExecutionMode m : ExecutionMode.values())
2157 <        for (Integer v1 : new Integer[] { 1, null })
2158 <        for (Integer v2 : new Integer[] { 2, null }) {
2159 <
2160 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2161 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2162 <        final FailingFunction r = new FailingFunction();
2163 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2164 <
2165 <        f.complete(v1);
2166 <        checkCompletedWithWrappedCFException(h);
2167 <        g.complete(v2);
2168 <        checkCompletedNormally(f, v1);
2169 <        checkCompletedNormally(g, v2);
2025 >        try {
2026 >            assertEquals(inc(v1), h3.join());
2027 >            rs[3].assertValue(inc(v1));
2028 >        } catch (CompletionException ok) {
2029 >            checkCompletedWithWrappedException(h3, ex);
2030 >            rs[3].assertNotInvoked();
2031          }
2171    }
2032  
2173    public void testApplyToEither_actionFailed2() {
2174        for (ExecutionMode m : ExecutionMode.values())
2175        for (Integer v1 : new Integer[] { 1, null })
2176        for (Integer v2 : new Integer[] { 2, null }) {
2177
2178        final CompletableFuture<Integer> f = new CompletableFuture<>();
2179        final CompletableFuture<Integer> g = new CompletableFuture<>();
2180        final FailingFunction r = new FailingFunction();
2181        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2182
2183        g.complete(v2);
2184        checkCompletedWithWrappedCFException(h);
2185        f.complete(v1);
2033          checkCompletedNormally(f, v1);
2034 <        checkCompletedNormally(g, v2);
2035 <        }
2189 <    }
2034 >        checkCompletedExceptionally(g, ex);
2035 >    }}
2036  
2037      /**
2038       * applyToEither result completes exceptionally if either source cancelled
2039       */
2040 <    public void testApplyToEither_sourceCancelled1() {
2040 >    public void testApplyToEither_sourceCancelled() {
2041          for (ExecutionMode m : ExecutionMode.values())
2042          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2043 <        for (Integer v1 : new Integer[] { 1, null }) {
2044 <
2043 >        for (Integer v1 : new Integer[] { 1, null })
2044 >    {
2045          final CompletableFuture<Integer> f = new CompletableFuture<>();
2046          final CompletableFuture<Integer> g = new CompletableFuture<>();
2047 <        final IncFunction r = new IncFunction();
2048 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2047 >        final IncFunction[] rs = new IncFunction[6];
2048 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2049  
2050 <        assertTrue(f.cancel(mayInterruptIfRunning));
2051 <        checkCompletedWithWrappedCancellationException(h);
2052 <        g.complete(v1);
2050 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2051 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2052 >        checkIncomplete(h0);
2053 >        checkIncomplete(h1);
2054 >        rs[0].assertNotInvoked();
2055 >        rs[1].assertNotInvoked();
2056 >        f.cancel(mayInterruptIfRunning);
2057 >        checkCompletedWithWrappedCancellationException(h0);
2058 >        checkCompletedWithWrappedCancellationException(h1);
2059 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2060 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2061 >        checkCompletedWithWrappedCancellationException(h2);
2062 >        checkCompletedWithWrappedCancellationException(h3);
2063 >        g.complete(v1);
2064 >
2065 >        // unspecified behavior - both source completions available
2066 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2067 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2068 >        try {
2069 >            assertEquals(inc(v1), h4.join());
2070 >            rs[4].assertValue(inc(v1));
2071 >        } catch (CompletionException ok) {
2072 >            checkCompletedWithWrappedCancellationException(h4);
2073 >            rs[4].assertNotInvoked();
2074 >        }
2075 >        try {
2076 >            assertEquals(inc(v1), h5.join());
2077 >            rs[5].assertValue(inc(v1));
2078 >        } catch (CompletionException ok) {
2079 >            checkCompletedWithWrappedCancellationException(h5);
2080 >            rs[5].assertNotInvoked();
2081 >        }
2082  
2083          checkCancelled(f);
2209        assertEquals(0, r.invocationCount);
2084          checkCompletedNormally(g, v1);
2085 <        checkCompletedWithWrappedCancellationException(h);
2086 <        }
2087 <    }
2085 >        checkCompletedWithWrappedCancellationException(h0);
2086 >        checkCompletedWithWrappedCancellationException(h1);
2087 >        checkCompletedWithWrappedCancellationException(h2);
2088 >        checkCompletedWithWrappedCancellationException(h3);
2089 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2090 >    }}
2091  
2092      public void testApplyToEither_sourceCancelled2() {
2093          for (ExecutionMode m : ExecutionMode.values())
2094          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2095 <        for (Integer v1 : new Integer[] { 1, null }) {
2096 <
2095 >        for (boolean fFirst : new boolean[] { true, false })
2096 >        for (Integer v1 : new Integer[] { 1, null })
2097 >    {
2098          final CompletableFuture<Integer> f = new CompletableFuture<>();
2099          final CompletableFuture<Integer> g = new CompletableFuture<>();
2100 <        final IncFunction r = new IncFunction();
2101 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2100 >        final IncFunction[] rs = new IncFunction[6];
2101 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2102  
2103 <        assertTrue(g.cancel(mayInterruptIfRunning));
2104 <        checkCompletedWithWrappedCancellationException(h);
2105 <        f.complete(v1);
2106 <
2107 <        checkCancelled(g);
2108 <        assertEquals(0, r.invocationCount);
2109 <        checkCompletedNormally(f, v1);
2110 <        checkCompletedWithWrappedCancellationException(h);
2103 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2104 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2105 >        if (fFirst) {
2106 >            f.complete(v1);
2107 >            g.cancel(mayInterruptIfRunning);
2108 >        } else {
2109 >            g.cancel(mayInterruptIfRunning);
2110 >            f.complete(v1);
2111          }
2112 <    }
2113 <
2236 <    public void testApplyToEither_sourceCancelled3() {
2237 <        for (ExecutionMode m : ExecutionMode.values())
2238 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2239 <        for (Integer v1 : new Integer[] { 1, null }) {
2112 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2113 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2114  
2115 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2242 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2243 <        final IncFunction r = new IncFunction();
2244 <
2245 <        assertTrue(g.cancel(mayInterruptIfRunning));
2246 <        f.complete(v1);
2247 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2248 <
2249 <        // unspecified behavior
2250 <        Integer v;
2115 >        // unspecified behavior - both source completions available
2116          try {
2117 <            assertEquals(inc(v1), h.join());
2118 <            assertEquals(1, r.invocationCount);
2117 >            assertEquals(inc(v1), h0.join());
2118 >            rs[0].assertValue(inc(v1));
2119          } catch (CompletionException ok) {
2120 <            checkCompletedWithWrappedCancellationException(h);
2121 <            assertEquals(0, r.invocationCount);
2120 >            checkCompletedWithWrappedCancellationException(h0);
2121 >            rs[0].assertNotInvoked();
2122          }
2123 <
2124 <        checkCancelled(g);
2125 <        checkCompletedNormally(f, v1);
2123 >        try {
2124 >            assertEquals(inc(v1), h1.join());
2125 >            rs[1].assertValue(inc(v1));
2126 >        } catch (CompletionException ok) {
2127 >            checkCompletedWithWrappedCancellationException(h1);
2128 >            rs[1].assertNotInvoked();
2129          }
2262    }
2263
2264    public void testApplyToEither_sourceCancelled4() {
2265        for (ExecutionMode m : ExecutionMode.values())
2266        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2267        for (Integer v1 : new Integer[] { 1, null }) {
2268
2269        final CompletableFuture<Integer> f = new CompletableFuture<>();
2270        final CompletableFuture<Integer> g = new CompletableFuture<>();
2271        final IncFunction r = new IncFunction();
2272
2273        assertTrue(f.cancel(mayInterruptIfRunning));
2274        g.complete(v1);
2275        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2276
2277        // unspecified behavior
2278        Integer v;
2130          try {
2131 <            assertEquals(inc(v1), h.join());
2132 <            assertEquals(1, r.invocationCount);
2131 >            assertEquals(inc(v1), h2.join());
2132 >            rs[2].assertValue(inc(v1));
2133          } catch (CompletionException ok) {
2134 <            checkCompletedWithWrappedCancellationException(h);
2135 <            assertEquals(0, r.invocationCount);
2134 >            checkCompletedWithWrappedCancellationException(h2);
2135 >            rs[2].assertNotInvoked();
2136          }
2137 <
2138 <        checkCancelled(f);
2139 <        checkCompletedNormally(g, v1);
2137 >        try {
2138 >            assertEquals(inc(v1), h3.join());
2139 >            rs[3].assertValue(inc(v1));
2140 >        } catch (CompletionException ok) {
2141 >            checkCompletedWithWrappedCancellationException(h3);
2142 >            rs[3].assertNotInvoked();
2143          }
2144 <    }
2144 >
2145 >        checkCompletedNormally(f, v1);
2146 >        checkCancelled(g);
2147 >    }}
2148  
2149      /**
2150 <     * acceptEither result completes normally after normal completion
2294 <     * of either source
2150 >     * applyToEither result completes exceptionally if action does
2151       */
2152 <    public void testAcceptEither_normalCompletion1() {
2152 >    public void testApplyToEither_actionFailed() {
2153          for (ExecutionMode m : ExecutionMode.values())
2154          for (Integer v1 : new Integer[] { 1, null })
2155 <        for (Integer v2 : new Integer[] { 2, null }) {
2156 <
2155 >        for (Integer v2 : new Integer[] { 2, null })
2156 >    {
2157          final CompletableFuture<Integer> f = new CompletableFuture<>();
2158          final CompletableFuture<Integer> g = new CompletableFuture<>();
2159 <        final IncAction r = new IncAction();
2160 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2159 >        final FailingFunction[] rs = new FailingFunction[6];
2160 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
2161  
2162 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2163 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2164          f.complete(v1);
2165 <        checkCompletedNormally(h, null);
2166 <        assertEquals(inc(v1), r.value);
2167 <        g.complete(v2);
2168 <
2169 <        checkCompletedNormally(f, v1);
2170 <        checkCompletedNormally(g, v2);
2171 <        checkCompletedNormally(h, null);
2314 <        }
2315 <    }
2316 <
2317 <    public void testAcceptEither_normalCompletion2() {
2318 <        for (ExecutionMode m : ExecutionMode.values())
2319 <        for (Integer v1 : new Integer[] { 1, null })
2320 <        for (Integer v2 : new Integer[] { 2, null }) {
2321 <
2322 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2323 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2324 <        final IncAction r = new IncAction();
2325 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2165 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2166 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2167 >        checkCompletedWithWrappedCFException(h0);
2168 >        checkCompletedWithWrappedCFException(h1);
2169 >        checkCompletedWithWrappedCFException(h2);
2170 >        checkCompletedWithWrappedCFException(h3);
2171 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2172  
2173          g.complete(v2);
2174 <        checkCompletedNormally(h, null);
2175 <        assertEquals(inc(v2), r.value);
2176 <        f.complete(v1);
2174 >
2175 >        // unspecified behavior - both source completions available
2176 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2177 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2178 >
2179 >        checkCompletedWithWrappedCFException(h4);
2180 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2181 >                   Objects.equals(v2, rs[4].value));
2182 >        checkCompletedWithWrappedCFException(h5);
2183 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2184 >                   Objects.equals(v2, rs[5].value));
2185  
2186          checkCompletedNormally(f, v1);
2187          checkCompletedNormally(g, v2);
2188 <        checkCompletedNormally(h, null);
2189 <        }
2190 <    }
2191 <    public void testAcceptEither_normalCompletion3() {
2188 >    }}
2189 >
2190 >    /**
2191 >     * acceptEither result completes normally after normal completion
2192 >     * of either source
2193 >     */
2194 >    public void testAcceptEither_normalCompletion() {
2195          for (ExecutionMode m : ExecutionMode.values())
2196          for (Integer v1 : new Integer[] { 1, null })
2197 <        for (Integer v2 : new Integer[] { 2, null }) {
2198 <
2197 >        for (Integer v2 : new Integer[] { 2, null })
2198 >    {
2199          final CompletableFuture<Integer> f = new CompletableFuture<>();
2200          final CompletableFuture<Integer> g = new CompletableFuture<>();
2201 <        final IncAction r = new IncAction();
2201 >        final NoopConsumer[] rs = new NoopConsumer[6];
2202 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2203  
2204 <        f.complete(v1);
2205 <        g.complete(v2);
2206 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2204 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2205 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2206 >        checkIncomplete(h0);
2207 >        checkIncomplete(h1);
2208 >        rs[0].assertNotInvoked();
2209 >        rs[1].assertNotInvoked();
2210 >        f.complete(v1);
2211 >        checkCompletedNormally(h0, null);
2212 >        checkCompletedNormally(h1, null);
2213 >        rs[0].assertValue(v1);
2214 >        rs[1].assertValue(v1);
2215 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2216 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2217 >        checkCompletedNormally(h2, null);
2218 >        checkCompletedNormally(h3, null);
2219 >        rs[2].assertValue(v1);
2220 >        rs[3].assertValue(v1);
2221 >        g.complete(v2);
2222 >
2223 >        // unspecified behavior - both source completions available
2224 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2225 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2226 >        checkCompletedNormally(h4, null);
2227 >        checkCompletedNormally(h5, null);
2228 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2229 >                   Objects.equals(v2, rs[4].value));
2230 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2231 >                   Objects.equals(v2, rs[5].value));
2232  
2350        checkCompletedNormally(h, null);
2233          checkCompletedNormally(f, v1);
2234          checkCompletedNormally(g, v2);
2235 <
2236 <        // unspecified behavior
2237 <        assertTrue(Objects.equals(r.value, inc(v1)) ||
2238 <                   Objects.equals(r.value, inc(v2)));
2239 <        }
2240 <    }
2235 >        checkCompletedNormally(h0, null);
2236 >        checkCompletedNormally(h1, null);
2237 >        checkCompletedNormally(h2, null);
2238 >        checkCompletedNormally(h3, null);
2239 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2240 >    }}
2241  
2242      /**
2243       * acceptEither result completes exceptionally after exceptional
2244       * completion of either source
2245       */
2246 <    public void testAcceptEither_exceptionalCompletion1() {
2246 >    public void testAcceptEither_exceptionalCompletion() {
2247          for (ExecutionMode m : ExecutionMode.values())
2248 <        for (Integer v1 : new Integer[] { 1, null }) {
2249 <
2248 >        for (Integer v1 : new Integer[] { 1, null })
2249 >    {
2250          final CompletableFuture<Integer> f = new CompletableFuture<>();
2251          final CompletableFuture<Integer> g = new CompletableFuture<>();
2370        final IncAction r = new IncAction();
2371        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2252          final CFException ex = new CFException();
2253 +        final NoopConsumer[] rs = new NoopConsumer[6];
2254 +        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2255  
2256 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2257 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2258 +        checkIncomplete(h0);
2259 +        checkIncomplete(h1);
2260 +        rs[0].assertNotInvoked();
2261 +        rs[1].assertNotInvoked();
2262          f.completeExceptionally(ex);
2263 <        checkCompletedWithWrappedCFException(h, ex);
2263 >        checkCompletedWithWrappedException(h0, ex);
2264 >        checkCompletedWithWrappedException(h1, ex);
2265 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2266 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2267 >        checkCompletedWithWrappedException(h2, ex);
2268 >        checkCompletedWithWrappedException(h3, ex);
2269 >
2270          g.complete(v1);
2271  
2272 <        assertEquals(0, r.invocationCount);
2273 <        checkCompletedNormally(g, v1);
2274 <        checkCompletedWithWrappedCFException(f, ex);
2275 <        checkCompletedWithWrappedCFException(h, ex);
2272 >        // unspecified behavior - both source completions available
2273 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2274 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2275 >        try {
2276 >            assertNull(h4.join());
2277 >            rs[4].assertValue(v1);
2278 >        } catch (CompletionException ok) {
2279 >            checkCompletedWithWrappedException(h4, ex);
2280 >            rs[4].assertNotInvoked();
2281          }
2282 <    }
2282 >        try {
2283 >            assertNull(h5.join());
2284 >            rs[5].assertValue(v1);
2285 >        } catch (CompletionException ok) {
2286 >            checkCompletedWithWrappedException(h5, ex);
2287 >            rs[5].assertNotInvoked();
2288 >        }
2289 >
2290 >        checkCompletedExceptionally(f, ex);
2291 >        checkCompletedNormally(g, v1);
2292 >        checkCompletedWithWrappedException(h0, ex);
2293 >        checkCompletedWithWrappedException(h1, ex);
2294 >        checkCompletedWithWrappedException(h2, ex);
2295 >        checkCompletedWithWrappedException(h3, ex);
2296 >        checkCompletedWithWrappedException(h4, ex);
2297 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2298 >    }}
2299  
2300      public void testAcceptEither_exceptionalCompletion2() {
2301          for (ExecutionMode m : ExecutionMode.values())
2302 <        for (Integer v1 : new Integer[] { 1, null }) {
2303 <
2302 >        for (boolean fFirst : new boolean[] { true, false })
2303 >        for (Integer v1 : new Integer[] { 1, null })
2304 >    {
2305          final CompletableFuture<Integer> f = new CompletableFuture<>();
2306          final CompletableFuture<Integer> g = new CompletableFuture<>();
2391        final IncAction r = new IncAction();
2392        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2307          final CFException ex = new CFException();
2308 +        final NoopConsumer[] rs = new NoopConsumer[6];
2309 +        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2310  
2311 <        g.completeExceptionally(ex);
2312 <        checkCompletedWithWrappedCFException(h, ex);
2313 <        f.complete(v1);
2314 <
2315 <        assertEquals(0, r.invocationCount);
2316 <        checkCompletedNormally(f, v1);
2317 <        checkCompletedWithWrappedCFException(g, ex);
2318 <        checkCompletedWithWrappedCFException(h, ex);
2311 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2312 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2313 >        if (fFirst) {
2314 >            f.complete(v1);
2315 >            g.completeExceptionally(ex);
2316 >        } else {
2317 >            g.completeExceptionally(ex);
2318 >            f.complete(v1);
2319          }
2320 <    }
2321 <
2406 <    public void testAcceptEither_exceptionalCompletion3() {
2407 <        for (ExecutionMode m : ExecutionMode.values())
2408 <        for (Integer v1 : new Integer[] { 1, null }) {
2409 <
2410 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2411 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2412 <        final IncAction r = new IncAction();
2413 <        final CFException ex = new CFException();
2414 <
2415 <        g.completeExceptionally(ex);
2416 <        f.complete(v1);
2417 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2320 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2321 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2322  
2323 <        // unspecified behavior
2420 <        Integer v;
2323 >        // unspecified behavior - both source completions available
2324          try {
2325 <            assertNull(h.join());
2326 <            assertEquals(1, r.invocationCount);
2424 <            assertEquals(inc(v1), r.value);
2325 >            assertEquals(null, h0.join());
2326 >            rs[0].assertValue(v1);
2327          } catch (CompletionException ok) {
2328 <            checkCompletedWithWrappedCFException(h, ex);
2329 <            assertEquals(0, r.invocationCount);
2328 >            checkCompletedWithWrappedException(h0, ex);
2329 >            rs[0].assertNotInvoked();
2330          }
2429
2430        checkCompletedWithWrappedCFException(g, ex);
2431        checkCompletedNormally(f, v1);
2432        }
2433    }
2434
2435    public void testAcceptEither_exceptionalCompletion4() {
2436        for (ExecutionMode m : ExecutionMode.values())
2437        for (Integer v1 : new Integer[] { 1, null }) {
2438
2439        final CompletableFuture<Integer> f = new CompletableFuture<>();
2440        final CompletableFuture<Integer> g = new CompletableFuture<>();
2441        final IncAction r = new IncAction();
2442        final CFException ex = new CFException();
2443
2444        f.completeExceptionally(ex);
2445        g.complete(v1);
2446        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2447
2448        // unspecified behavior
2449        Integer v;
2331          try {
2332 <            assertNull(h.join());
2333 <            assertEquals(1, r.invocationCount);
2453 <            assertEquals(inc(v1), r.value);
2332 >            assertEquals(null, h1.join());
2333 >            rs[1].assertValue(v1);
2334          } catch (CompletionException ok) {
2335 <            checkCompletedWithWrappedCFException(h, ex);
2336 <            assertEquals(0, r.invocationCount);
2335 >            checkCompletedWithWrappedException(h1, ex);
2336 >            rs[1].assertNotInvoked();
2337          }
2338 <
2339 <        checkCompletedWithWrappedCFException(f, ex);
2340 <        checkCompletedNormally(g, v1);
2338 >        try {
2339 >            assertEquals(null, h2.join());
2340 >            rs[2].assertValue(v1);
2341 >        } catch (CompletionException ok) {
2342 >            checkCompletedWithWrappedException(h2, ex);
2343 >            rs[2].assertNotInvoked();
2344          }
2345 <    }
2346 <
2347 <    /**
2348 <     * acceptEither result completes exceptionally if action does
2349 <     */
2350 <    public void testAcceptEither_actionFailed1() {
2468 <        for (ExecutionMode m : ExecutionMode.values())
2469 <        for (Integer v1 : new Integer[] { 1, null })
2470 <        for (Integer v2 : new Integer[] { 2, null }) {
2471 <
2472 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2473 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2474 <        final FailingConsumer r = new FailingConsumer();
2475 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2476 <
2477 <        f.complete(v1);
2478 <        checkCompletedWithWrappedCFException(h);
2479 <        g.complete(v2);
2480 <        checkCompletedNormally(f, v1);
2481 <        checkCompletedNormally(g, v2);
2345 >        try {
2346 >            assertEquals(null, h3.join());
2347 >            rs[3].assertValue(v1);
2348 >        } catch (CompletionException ok) {
2349 >            checkCompletedWithWrappedException(h3, ex);
2350 >            rs[3].assertNotInvoked();
2351          }
2483    }
2352  
2485    public void testAcceptEither_actionFailed2() {
2486        for (ExecutionMode m : ExecutionMode.values())
2487        for (Integer v1 : new Integer[] { 1, null })
2488        for (Integer v2 : new Integer[] { 2, null }) {
2489
2490        final CompletableFuture<Integer> f = new CompletableFuture<>();
2491        final CompletableFuture<Integer> g = new CompletableFuture<>();
2492        final FailingConsumer r = new FailingConsumer();
2493        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2494
2495        g.complete(v2);
2496        checkCompletedWithWrappedCFException(h);
2497        f.complete(v1);
2353          checkCompletedNormally(f, v1);
2354 <        checkCompletedNormally(g, v2);
2355 <        }
2501 <    }
2354 >        checkCompletedExceptionally(g, ex);
2355 >    }}
2356  
2357      /**
2358       * acceptEither result completes exceptionally if either source cancelled
2359       */
2360 <    public void testAcceptEither_sourceCancelled1() {
2507 <        for (ExecutionMode m : ExecutionMode.values())
2508 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2509 <        for (Integer v1 : new Integer[] { 1, null }) {
2510 <
2511 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2512 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2513 <        final IncAction r = new IncAction();
2514 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2515 <
2516 <        assertTrue(f.cancel(mayInterruptIfRunning));
2517 <        checkCompletedWithWrappedCancellationException(h);
2518 <        g.complete(v1);
2519 <
2520 <        checkCancelled(f);
2521 <        assertEquals(0, r.invocationCount);
2522 <        checkCompletedNormally(g, v1);
2523 <        checkCompletedWithWrappedCancellationException(h);
2524 <        }
2525 <    }
2526 <
2527 <    public void testAcceptEither_sourceCancelled2() {
2360 >    public void testAcceptEither_sourceCancelled() {
2361          for (ExecutionMode m : ExecutionMode.values())
2362          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2363 <        for (Integer v1 : new Integer[] { 1, null }) {
2364 <
2363 >        for (Integer v1 : new Integer[] { 1, null })
2364 >    {
2365          final CompletableFuture<Integer> f = new CompletableFuture<>();
2366          final CompletableFuture<Integer> g = new CompletableFuture<>();
2367 <        final IncAction r = new IncAction();
2368 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2536 <
2537 <        assertTrue(g.cancel(mayInterruptIfRunning));
2538 <        checkCompletedWithWrappedCancellationException(h);
2539 <        f.complete(v1);
2367 >        final NoopConsumer[] rs = new NoopConsumer[6];
2368 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2369  
2370 <        checkCancelled(g);
2371 <        assertEquals(0, r.invocationCount);
2372 <        checkCompletedNormally(f, v1);
2373 <        checkCompletedWithWrappedCancellationException(h);
2374 <        }
2375 <    }
2370 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2371 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2372 >        checkIncomplete(h0);
2373 >        checkIncomplete(h1);
2374 >        rs[0].assertNotInvoked();
2375 >        rs[1].assertNotInvoked();
2376 >        f.cancel(mayInterruptIfRunning);
2377 >        checkCompletedWithWrappedCancellationException(h0);
2378 >        checkCompletedWithWrappedCancellationException(h1);
2379 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2380 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2381 >        checkCompletedWithWrappedCancellationException(h2);
2382 >        checkCompletedWithWrappedCancellationException(h3);
2383  
2384 <    public void testAcceptEither_sourceCancelled3() {
2549 <        for (ExecutionMode m : ExecutionMode.values())
2550 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2551 <        for (Integer v1 : new Integer[] { 1, null }) {
2552 <
2553 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2554 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2555 <        final IncAction r = new IncAction();
2556 <
2557 <        assertTrue(g.cancel(mayInterruptIfRunning));
2558 <        f.complete(v1);
2559 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2384 >        g.complete(v1);
2385  
2386 <        // unspecified behavior
2387 <        Integer v;
2386 >        // unspecified behavior - both source completions available
2387 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2388 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2389          try {
2390 <            assertNull(h.join());
2391 <            assertEquals(1, r.invocationCount);
2566 <            assertEquals(inc(v1), r.value);
2390 >            assertNull(h4.join());
2391 >            rs[4].assertValue(v1);
2392          } catch (CompletionException ok) {
2393 <            checkCompletedWithWrappedCancellationException(h);
2394 <            assertEquals(0, r.invocationCount);
2393 >            checkCompletedWithWrappedCancellationException(h4);
2394 >            rs[4].assertNotInvoked();
2395          }
2571
2572        checkCancelled(g);
2573        checkCompletedNormally(f, v1);
2574        }
2575    }
2576
2577    public void testAcceptEither_sourceCancelled4() {
2578        for (ExecutionMode m : ExecutionMode.values())
2579        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2580        for (Integer v1 : new Integer[] { 1, null }) {
2581
2582        final CompletableFuture<Integer> f = new CompletableFuture<>();
2583        final CompletableFuture<Integer> g = new CompletableFuture<>();
2584        final IncAction r = new IncAction();
2585
2586        assertTrue(f.cancel(mayInterruptIfRunning));
2587        g.complete(v1);
2588        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2589
2590        // unspecified behavior
2591        Integer v;
2396          try {
2397 <            assertNull(h.join());
2398 <            assertEquals(1, r.invocationCount);
2595 <            assertEquals(inc(v1), r.value);
2397 >            assertNull(h5.join());
2398 >            rs[5].assertValue(v1);
2399          } catch (CompletionException ok) {
2400 <            checkCompletedWithWrappedCancellationException(h);
2401 <            assertEquals(0, r.invocationCount);
2400 >            checkCompletedWithWrappedCancellationException(h5);
2401 >            rs[5].assertNotInvoked();
2402          }
2403  
2404          checkCancelled(f);
2405          checkCompletedNormally(g, v1);
2406 <        }
2407 <    }
2406 >        checkCompletedWithWrappedCancellationException(h0);
2407 >        checkCompletedWithWrappedCancellationException(h1);
2408 >        checkCompletedWithWrappedCancellationException(h2);
2409 >        checkCompletedWithWrappedCancellationException(h3);
2410 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2411 >    }}
2412  
2413      /**
2414 <     * runAfterEither result completes normally after normal completion
2608 <     * of either source
2414 >     * acceptEither result completes exceptionally if action does
2415       */
2416 <    public void testRunAfterEither_normalCompletion1() {
2416 >    public void testAcceptEither_actionFailed() {
2417          for (ExecutionMode m : ExecutionMode.values())
2418          for (Integer v1 : new Integer[] { 1, null })
2419 <        for (Integer v2 : new Integer[] { 2, null }) {
2420 <
2419 >        for (Integer v2 : new Integer[] { 2, null })
2420 >    {
2421          final CompletableFuture<Integer> f = new CompletableFuture<>();
2422          final CompletableFuture<Integer> g = new CompletableFuture<>();
2423 <        final Noop r = new Noop();
2424 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2423 >        final FailingConsumer[] rs = new FailingConsumer[6];
2424 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
2425  
2426 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2427 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2428          f.complete(v1);
2429 <        checkCompletedNormally(h, null);
2430 <        assertEquals(1, r.invocationCount);
2429 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2430 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2431 >        checkCompletedWithWrappedCFException(h0);
2432 >        checkCompletedWithWrappedCFException(h1);
2433 >        checkCompletedWithWrappedCFException(h2);
2434 >        checkCompletedWithWrappedCFException(h3);
2435 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2436 >
2437          g.complete(v2);
2438  
2439 +        // unspecified behavior - both source completions available
2440 +        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2441 +        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2442 +
2443 +        checkCompletedWithWrappedCFException(h4);
2444 +        assertTrue(Objects.equals(v1, rs[4].value) ||
2445 +                   Objects.equals(v2, rs[4].value));
2446 +        checkCompletedWithWrappedCFException(h5);
2447 +        assertTrue(Objects.equals(v1, rs[5].value) ||
2448 +                   Objects.equals(v2, rs[5].value));
2449 +
2450          checkCompletedNormally(f, v1);
2451          checkCompletedNormally(g, v2);
2452 <        checkCompletedNormally(h, null);
2628 <        assertEquals(1, r.invocationCount);
2629 <        }
2630 <    }
2452 >    }}
2453  
2454 <    public void testRunAfterEither_normalCompletion2() {
2454 >    /**
2455 >     * runAfterEither result completes normally after normal completion
2456 >     * of either source
2457 >     */
2458 >    public void testRunAfterEither_normalCompletion() {
2459          for (ExecutionMode m : ExecutionMode.values())
2460          for (Integer v1 : new Integer[] { 1, null })
2461 <        for (Integer v2 : new Integer[] { 2, null }) {
2462 <
2461 >        for (Integer v2 : new Integer[] { 2, null })
2462 >    {
2463          final CompletableFuture<Integer> f = new CompletableFuture<>();
2464          final CompletableFuture<Integer> g = new CompletableFuture<>();
2465 <        final Noop r = new Noop();
2466 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2641 <
2642 <        g.complete(v2);
2643 <        checkCompletedNormally(h, null);
2644 <        assertEquals(1, r.invocationCount);
2645 <        f.complete(v1);
2646 <
2647 <        checkCompletedNormally(f, v1);
2648 <        checkCompletedNormally(g, v2);
2649 <        checkCompletedNormally(h, null);
2650 <        assertEquals(1, r.invocationCount);
2651 <        }
2652 <    }
2653 <    public void testRunAfterEither_normalCompletion3() {
2654 <        for (ExecutionMode m : ExecutionMode.values())
2655 <        for (Integer v1 : new Integer[] { 1, null })
2656 <        for (Integer v2 : new Integer[] { 2, null }) {
2465 >        final Noop[] rs = new Noop[6];
2466 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2467  
2468 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2469 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2470 <        final Noop r = new Noop();
2468 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2469 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2470 >        checkIncomplete(h0);
2471 >        checkIncomplete(h1);
2472 >        rs[0].assertNotInvoked();
2473 >        rs[1].assertNotInvoked();
2474 >        f.complete(v1);
2475 >        checkCompletedNormally(h0, null);
2476 >        checkCompletedNormally(h1, null);
2477 >        rs[0].assertInvoked();
2478 >        rs[1].assertInvoked();
2479 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2480 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2481 >        checkCompletedNormally(h2, null);
2482 >        checkCompletedNormally(h3, null);
2483 >        rs[2].assertInvoked();
2484 >        rs[3].assertInvoked();
2485  
2662        f.complete(v1);
2486          g.complete(v2);
2664        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2487  
2488 <        checkCompletedNormally(h, null);
2488 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2489 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2490 >
2491          checkCompletedNormally(f, v1);
2492          checkCompletedNormally(g, v2);
2493 <        assertEquals(1, r.invocationCount);
2494 <        }
2495 <    }
2493 >        checkCompletedNormally(h0, null);
2494 >        checkCompletedNormally(h1, null);
2495 >        checkCompletedNormally(h2, null);
2496 >        checkCompletedNormally(h3, null);
2497 >        checkCompletedNormally(h4, null);
2498 >        checkCompletedNormally(h5, null);
2499 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2500 >    }}
2501  
2502      /**
2503       * runAfterEither result completes exceptionally after exceptional
2504       * completion of either source
2505       */
2506 <    public void testRunAfterEither_exceptionalCompletion1() {
2506 >    public void testRunAfterEither_exceptionalCompletion() {
2507          for (ExecutionMode m : ExecutionMode.values())
2508 <        for (Integer v1 : new Integer[] { 1, null }) {
2509 <
2508 >        for (Integer v1 : new Integer[] { 1, null })
2509 >    {
2510          final CompletableFuture<Integer> f = new CompletableFuture<>();
2511          final CompletableFuture<Integer> g = new CompletableFuture<>();
2683        final Noop r = new Noop();
2684        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2512          final CFException ex = new CFException();
2513 +        final Noop[] rs = new Noop[6];
2514 +        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2515  
2516 +        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2517 +        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2518 +        checkIncomplete(h0);
2519 +        checkIncomplete(h1);
2520 +        rs[0].assertNotInvoked();
2521 +        rs[1].assertNotInvoked();
2522          f.completeExceptionally(ex);
2523 <        checkCompletedWithWrappedCFException(h, ex);
2523 >        checkCompletedWithWrappedException(h0, ex);
2524 >        checkCompletedWithWrappedException(h1, ex);
2525 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2526 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2527 >        checkCompletedWithWrappedException(h2, ex);
2528 >        checkCompletedWithWrappedException(h3, ex);
2529 >
2530          g.complete(v1);
2531  
2532 <        assertEquals(0, r.invocationCount);
2533 <        checkCompletedNormally(g, v1);
2534 <        checkCompletedWithWrappedCFException(f, ex);
2535 <        checkCompletedWithWrappedCFException(h, ex);
2532 >        // unspecified behavior - both source completions available
2533 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2534 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2535 >        try {
2536 >            assertNull(h4.join());
2537 >            rs[4].assertInvoked();
2538 >        } catch (CompletionException ok) {
2539 >            checkCompletedWithWrappedException(h4, ex);
2540 >            rs[4].assertNotInvoked();
2541          }
2542 <    }
2542 >        try {
2543 >            assertNull(h5.join());
2544 >            rs[5].assertInvoked();
2545 >        } catch (CompletionException ok) {
2546 >            checkCompletedWithWrappedException(h5, ex);
2547 >            rs[5].assertNotInvoked();
2548 >        }
2549 >
2550 >        checkCompletedExceptionally(f, ex);
2551 >        checkCompletedNormally(g, v1);
2552 >        checkCompletedWithWrappedException(h0, ex);
2553 >        checkCompletedWithWrappedException(h1, ex);
2554 >        checkCompletedWithWrappedException(h2, ex);
2555 >        checkCompletedWithWrappedException(h3, ex);
2556 >        checkCompletedWithWrappedException(h4, ex);
2557 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2558 >    }}
2559  
2560      public void testRunAfterEither_exceptionalCompletion2() {
2561          for (ExecutionMode m : ExecutionMode.values())
2562 <        for (Integer v1 : new Integer[] { 1, null }) {
2563 <
2562 >        for (boolean fFirst : new boolean[] { true, false })
2563 >        for (Integer v1 : new Integer[] { 1, null })
2564 >    {
2565          final CompletableFuture<Integer> f = new CompletableFuture<>();
2566          final CompletableFuture<Integer> g = new CompletableFuture<>();
2704        final Noop r = new Noop();
2705        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2567          final CFException ex = new CFException();
2568 +        final Noop[] rs = new Noop[6];
2569 +        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2570  
2571 <        g.completeExceptionally(ex);
2572 <        checkCompletedWithWrappedCFException(h, ex);
2573 <        f.complete(v1);
2574 <
2575 <        assertEquals(0, r.invocationCount);
2576 <        checkCompletedNormally(f, v1);
2577 <        checkCompletedWithWrappedCFException(g, ex);
2578 <        checkCompletedWithWrappedCFException(h, ex);
2571 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2572 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2573 >        if (fFirst) {
2574 >            f.complete(v1);
2575 >            g.completeExceptionally(ex);
2576 >        } else {
2577 >            g.completeExceptionally(ex);
2578 >            f.complete(v1);
2579          }
2580 <    }
2581 <
2719 <    public void testRunAfterEither_exceptionalCompletion3() {
2720 <        for (ExecutionMode m : ExecutionMode.values())
2721 <        for (Integer v1 : new Integer[] { 1, null }) {
2722 <
2723 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2724 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2725 <        final Noop r = new Noop();
2726 <        final CFException ex = new CFException();
2727 <
2728 <        g.completeExceptionally(ex);
2729 <        f.complete(v1);
2730 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2580 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2581 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2582  
2583 <        // unspecified behavior
2733 <        Integer v;
2583 >        // unspecified behavior - both source completions available
2584          try {
2585 <            assertNull(h.join());
2586 <            assertEquals(1, r.invocationCount);
2585 >            assertEquals(null, h0.join());
2586 >            rs[0].assertInvoked();
2587          } catch (CompletionException ok) {
2588 <            checkCompletedWithWrappedCFException(h, ex);
2589 <            assertEquals(0, r.invocationCount);
2740 <        }
2741 <
2742 <        checkCompletedWithWrappedCFException(g, ex);
2743 <        checkCompletedNormally(f, v1);
2588 >            checkCompletedWithWrappedException(h0, ex);
2589 >            rs[0].assertNotInvoked();
2590          }
2745    }
2746
2747    public void testRunAfterEither_exceptionalCompletion4() {
2748        for (ExecutionMode m : ExecutionMode.values())
2749        for (Integer v1 : new Integer[] { 1, null }) {
2750
2751        final CompletableFuture<Integer> f = new CompletableFuture<>();
2752        final CompletableFuture<Integer> g = new CompletableFuture<>();
2753        final Noop r = new Noop();
2754        final CFException ex = new CFException();
2755
2756        f.completeExceptionally(ex);
2757        g.complete(v1);
2758        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2759
2760        // unspecified behavior
2761        Integer v;
2591          try {
2592 <            assertNull(h.join());
2593 <            assertEquals(1, r.invocationCount);
2592 >            assertEquals(null, h1.join());
2593 >            rs[1].assertInvoked();
2594          } catch (CompletionException ok) {
2595 <            checkCompletedWithWrappedCFException(h, ex);
2596 <            assertEquals(0, r.invocationCount);
2595 >            checkCompletedWithWrappedException(h1, ex);
2596 >            rs[1].assertNotInvoked();
2597          }
2598 <
2599 <        checkCompletedWithWrappedCFException(f, ex);
2600 <        checkCompletedNormally(g, v1);
2598 >        try {
2599 >            assertEquals(null, h2.join());
2600 >            rs[2].assertInvoked();
2601 >        } catch (CompletionException ok) {
2602 >            checkCompletedWithWrappedException(h2, ex);
2603 >            rs[2].assertNotInvoked();
2604          }
2605 <    }
2606 <
2607 <    /**
2608 <     * runAfterEither result completes exceptionally if action does
2609 <     */
2610 <    public void testRunAfterEither_actionFailed1() {
2779 <        for (ExecutionMode m : ExecutionMode.values())
2780 <        for (Integer v1 : new Integer[] { 1, null })
2781 <        for (Integer v2 : new Integer[] { 2, null }) {
2782 <
2783 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2784 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2785 <        final FailingNoop r = new FailingNoop();
2786 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2787 <
2788 <        f.complete(v1);
2789 <        checkCompletedWithWrappedCFException(h);
2790 <        g.complete(v2);
2791 <        checkCompletedNormally(f, v1);
2792 <        checkCompletedNormally(g, v2);
2605 >        try {
2606 >            assertEquals(null, h3.join());
2607 >            rs[3].assertInvoked();
2608 >        } catch (CompletionException ok) {
2609 >            checkCompletedWithWrappedException(h3, ex);
2610 >            rs[3].assertNotInvoked();
2611          }
2794    }
2795
2796    public void testRunAfterEither_actionFailed2() {
2797        for (ExecutionMode m : ExecutionMode.values())
2798        for (Integer v1 : new Integer[] { 1, null })
2799        for (Integer v2 : new Integer[] { 2, null }) {
2800
2801        final CompletableFuture<Integer> f = new CompletableFuture<>();
2802        final CompletableFuture<Integer> g = new CompletableFuture<>();
2803        final FailingNoop r = new FailingNoop();
2804        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2612  
2806        g.complete(v2);
2807        checkCompletedWithWrappedCFException(h);
2808        f.complete(v1);
2613          checkCompletedNormally(f, v1);
2614 <        checkCompletedNormally(g, v2);
2615 <        }
2812 <    }
2614 >        checkCompletedExceptionally(g, ex);
2615 >    }}
2616  
2617      /**
2618       * runAfterEither result completes exceptionally if either source cancelled
2619       */
2620 <    public void testRunAfterEither_sourceCancelled1() {
2818 <        for (ExecutionMode m : ExecutionMode.values())
2819 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2820 <        for (Integer v1 : new Integer[] { 1, null }) {
2821 <
2822 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2823 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2824 <        final Noop r = new Noop();
2825 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2826 <
2827 <        assertTrue(f.cancel(mayInterruptIfRunning));
2828 <        checkCompletedWithWrappedCancellationException(h);
2829 <        g.complete(v1);
2830 <
2831 <        checkCancelled(f);
2832 <        assertEquals(0, r.invocationCount);
2833 <        checkCompletedNormally(g, v1);
2834 <        checkCompletedWithWrappedCancellationException(h);
2835 <        }
2836 <    }
2837 <
2838 <    public void testRunAfterEither_sourceCancelled2() {
2620 >    public void testRunAfterEither_sourceCancelled() {
2621          for (ExecutionMode m : ExecutionMode.values())
2622          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2623 <        for (Integer v1 : new Integer[] { 1, null }) {
2624 <
2623 >        for (Integer v1 : new Integer[] { 1, null })
2624 >    {
2625          final CompletableFuture<Integer> f = new CompletableFuture<>();
2626          final CompletableFuture<Integer> g = new CompletableFuture<>();
2627 <        final Noop r = new Noop();
2628 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2847 <
2848 <        assertTrue(g.cancel(mayInterruptIfRunning));
2849 <        checkCompletedWithWrappedCancellationException(h);
2850 <        f.complete(v1);
2851 <
2852 <        checkCancelled(g);
2853 <        assertEquals(0, r.invocationCount);
2854 <        checkCompletedNormally(f, v1);
2855 <        checkCompletedWithWrappedCancellationException(h);
2856 <        }
2857 <    }
2627 >        final Noop[] rs = new Noop[6];
2628 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2629  
2630 <    public void testRunAfterEither_sourceCancelled3() {
2631 <        for (ExecutionMode m : ExecutionMode.values())
2632 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2633 <        for (Integer v1 : new Integer[] { 1, null }) {
2634 <
2635 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2636 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2637 <        final Noop r = new Noop();
2630 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2631 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2632 >        checkIncomplete(h0);
2633 >        checkIncomplete(h1);
2634 >        rs[0].assertNotInvoked();
2635 >        rs[1].assertNotInvoked();
2636 >        f.cancel(mayInterruptIfRunning);
2637 >        checkCompletedWithWrappedCancellationException(h0);
2638 >        checkCompletedWithWrappedCancellationException(h1);
2639 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2640 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2641 >        checkCompletedWithWrappedCancellationException(h2);
2642 >        checkCompletedWithWrappedCancellationException(h3);
2643  
2644 <        assertTrue(g.cancel(mayInterruptIfRunning));
2869 <        f.complete(v1);
2870 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2644 >        g.complete(v1);
2645  
2646 <        // unspecified behavior
2647 <        Integer v;
2646 >        // unspecified behavior - both source completions available
2647 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2648 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2649          try {
2650 <            assertNull(h.join());
2651 <            assertEquals(1, r.invocationCount);
2650 >            assertNull(h4.join());
2651 >            rs[4].assertInvoked();
2652          } catch (CompletionException ok) {
2653 <            checkCompletedWithWrappedCancellationException(h);
2654 <            assertEquals(0, r.invocationCount);
2880 <        }
2881 <
2882 <        checkCancelled(g);
2883 <        checkCompletedNormally(f, v1);
2653 >            checkCompletedWithWrappedCancellationException(h4);
2654 >            rs[4].assertNotInvoked();
2655          }
2885    }
2886
2887    public void testRunAfterEither_sourceCancelled4() {
2888        for (ExecutionMode m : ExecutionMode.values())
2889        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2890        for (Integer v1 : new Integer[] { 1, null }) {
2891
2892        final CompletableFuture<Integer> f = new CompletableFuture<>();
2893        final CompletableFuture<Integer> g = new CompletableFuture<>();
2894        final Noop r = new Noop();
2895
2896        assertTrue(f.cancel(mayInterruptIfRunning));
2897        g.complete(v1);
2898        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2899
2900        // unspecified behavior
2901        Integer v;
2656          try {
2657 <            assertNull(h.join());
2658 <            assertEquals(1, r.invocationCount);
2657 >            assertNull(h5.join());
2658 >            rs[5].assertInvoked();
2659          } catch (CompletionException ok) {
2660 <            checkCompletedWithWrappedCancellationException(h);
2661 <            assertEquals(0, r.invocationCount);
2660 >            checkCompletedWithWrappedCancellationException(h5);
2661 >            rs[5].assertNotInvoked();
2662          }
2663  
2664          checkCancelled(f);
2665          checkCompletedNormally(g, v1);
2666 <        }
2667 <    }
2666 >        checkCompletedWithWrappedCancellationException(h0);
2667 >        checkCompletedWithWrappedCancellationException(h1);
2668 >        checkCompletedWithWrappedCancellationException(h2);
2669 >        checkCompletedWithWrappedCancellationException(h3);
2670 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2671 >    }}
2672  
2673      /**
2674 <     * thenCompose result completes normally after normal completion of source
2674 >     * runAfterEither result completes exceptionally if action does
2675       */
2676 <    public void testThenCompose_normalCompletion1() {
2676 >    public void testRunAfterEither_actionFailed() {
2677          for (ExecutionMode m : ExecutionMode.values())
2678 <        for (Integer v1 : new Integer[] { 1, null }) {
2679 <
2678 >        for (Integer v1 : new Integer[] { 1, null })
2679 >        for (Integer v2 : new Integer[] { 2, null })
2680 >    {
2681          final CompletableFuture<Integer> f = new CompletableFuture<>();
2682 <        final CompletableFutureInc r = new CompletableFutureInc();
2683 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2682 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2683 >        final FailingRunnable[] rs = new FailingRunnable[6];
2684 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
2685 >
2686 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2687 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2688          f.complete(v1);
2689 <        checkCompletedNormally(g, inc(v1));
2689 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2690 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2691 >        checkCompletedWithWrappedCFException(h0);
2692 >        checkCompletedWithWrappedCFException(h1);
2693 >        checkCompletedWithWrappedCFException(h2);
2694 >        checkCompletedWithWrappedCFException(h3);
2695 >        for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2696 >        g.complete(v2);
2697 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2698 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2699 >        checkCompletedWithWrappedCFException(h4);
2700 >        checkCompletedWithWrappedCFException(h5);
2701 >
2702          checkCompletedNormally(f, v1);
2703 <        assertEquals(1, r.invocationCount);
2704 <        }
2705 <    }
2703 >        checkCompletedNormally(g, v2);
2704 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2705 >    }}
2706  
2707 <    public void testThenCompose_normalCompletion2() {
2707 >    /**
2708 >     * thenCompose result completes normally after normal completion of source
2709 >     */
2710 >    public void testThenCompose_normalCompletion() {
2711          for (ExecutionMode m : ExecutionMode.values())
2712 <        for (Integer v1 : new Integer[] { 1, null }) {
2713 <
2712 >        for (boolean createIncomplete : new boolean[] { true, false })
2713 >        for (Integer v1 : new Integer[] { 1, null })
2714 >    {
2715          final CompletableFuture<Integer> f = new CompletableFuture<>();
2716 <        final CompletableFutureInc r = new CompletableFutureInc();
2717 <        f.complete(v1);
2718 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2716 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2717 >        if (!createIncomplete) f.complete(v1);
2718 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2719 >        if (createIncomplete) f.complete(v1);
2720 >
2721          checkCompletedNormally(g, inc(v1));
2722          checkCompletedNormally(f, v1);
2723 <        assertEquals(1, r.invocationCount);
2724 <        }
2944 <    }
2723 >        r.assertValue(v1);
2724 >    }}
2725  
2726      /**
2727       * thenCompose result completes exceptionally after exceptional
2728       * completion of source
2729       */
2730 <    public void testThenCompose_exceptionalCompletion1() {
2731 <        for (ExecutionMode m : ExecutionMode.values()) {
2732 <
2730 >    public void testThenCompose_exceptionalCompletion() {
2731 >        for (ExecutionMode m : ExecutionMode.values())
2732 >        for (boolean createIncomplete : new boolean[] { true, false })
2733 >    {
2734          final CFException ex = new CFException();
2735 <        final CompletableFutureInc r = new CompletableFutureInc();
2735 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2736          final CompletableFuture<Integer> f = new CompletableFuture<>();
2737 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2738 <        f.completeExceptionally(ex);
2739 <        checkCompletedWithWrappedCFException(g, ex);
2959 <        checkCompletedWithWrappedCFException(f, ex);
2960 <        }
2961 <    }
2962 <
2963 <    public void testThenCompose_exceptionalCompletion2() {
2964 <        for (ExecutionMode m : ExecutionMode.values()) {
2737 >        if (!createIncomplete) f.completeExceptionally(ex);
2738 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2739 >        if (createIncomplete) f.completeExceptionally(ex);
2740  
2741 <        final CFException ex = new CFException();
2742 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2743 <        f.completeExceptionally(ex);
2744 <        final CompletableFutureInc r = new CompletableFutureInc();
2970 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2971 <        checkCompletedWithWrappedCFException(g, ex);
2972 <        checkCompletedWithWrappedCFException(f, ex);
2973 <        }
2974 <    }
2741 >        checkCompletedWithWrappedException(g, ex);
2742 >        checkCompletedExceptionally(f, ex);
2743 >        r.assertNotInvoked();
2744 >    }}
2745  
2746      /**
2747       * thenCompose result completes exceptionally if action does
2748       */
2749 <    public void testThenCompose_actionFailed1() {
2749 >    public void testThenCompose_actionFailed() {
2750          for (ExecutionMode m : ExecutionMode.values())
2751 <        for (Integer v1 : new Integer[] { 1, null }) {
2752 <
2751 >        for (boolean createIncomplete : new boolean[] { true, false })
2752 >        for (Integer v1 : new Integer[] { 1, null })
2753 >    {
2754          final CompletableFuture<Integer> f = new CompletableFuture<>();
2755          final FailingCompletableFutureFunction r
2756 <            = new FailingCompletableFutureFunction();
2757 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2758 <        f.complete(v1);
2759 <        checkCompletedWithWrappedCFException(g);
2989 <        checkCompletedNormally(f, v1);
2990 <        }
2991 <    }
2992 <
2993 <    public void testThenCompose_actionFailed2() {
2994 <        for (ExecutionMode m : ExecutionMode.values())
2995 <        for (Integer v1 : new Integer[] { 1, null }) {
2756 >            = new FailingCompletableFutureFunction(m);
2757 >        if (!createIncomplete) f.complete(v1);
2758 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2759 >        if (createIncomplete) f.complete(v1);
2760  
2997        final CompletableFuture<Integer> f = new CompletableFuture<>();
2998        f.complete(v1);
2999        final FailingCompletableFutureFunction r
3000            = new FailingCompletableFutureFunction();
3001        final CompletableFuture<Integer> g = f.thenCompose(r);
2761          checkCompletedWithWrappedCFException(g);
2762          checkCompletedNormally(f, v1);
2763 <        }
3005 <    }
2763 >    }}
2764  
2765      /**
2766       * thenCompose result completes exceptionally if source cancelled
2767       */
2768 <    public void testThenCompose_sourceCancelled1() {
2768 >    public void testThenCompose_sourceCancelled() {
2769          for (ExecutionMode m : ExecutionMode.values())
2770 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
2771 <
2770 >        for (boolean createIncomplete : new boolean[] { true, false })
2771 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2772 >    {
2773          final CompletableFuture<Integer> f = new CompletableFuture<>();
2774 <        final CompletableFutureInc r = new CompletableFutureInc();
2775 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2776 <        assertTrue(f.cancel(mayInterruptIfRunning));
2777 <        checkCompletedWithWrappedCancellationException(g);
2778 <        checkCancelled(f);
2774 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2775 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2776 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2777 >        if (createIncomplete) {
2778 >            checkIncomplete(g);
2779 >            assertTrue(f.cancel(mayInterruptIfRunning));
2780          }
3021    }
2781  
3023    public void testThenCompose_sourceCancelled2() {
3024        for (ExecutionMode m : ExecutionMode.values())
3025        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
3026
3027        final CompletableFuture<Integer> f = new CompletableFuture<>();
3028        assertTrue(f.cancel(mayInterruptIfRunning));
3029        final CompletableFutureInc r = new CompletableFutureInc();
3030        final CompletableFuture<Integer> g = f.thenCompose(r);
2782          checkCompletedWithWrappedCancellationException(g);
2783          checkCancelled(f);
2784 <        }
3034 <    }
3035 <
3036 <    // asyncs
3037 <
3038 <    /**
3039 <     * thenRunAsync result completes normally after normal completion of source
3040 <     */
3041 <    public void testThenRunAsync() {
3042 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3043 <        Noop r = new Noop();
3044 <        CompletableFuture<Void> g = f.thenRunAsync(r);
3045 <        f.complete(null);
3046 <        checkCompletedNormally(g, null);
3047 <
3048 <        // reordered version
3049 <        f = new CompletableFuture<>();
3050 <        f.complete(null);
3051 <        r = new Noop();
3052 <        g = f.thenRunAsync(r);
3053 <        checkCompletedNormally(g, null);
3054 <    }
3055 <
3056 <    /**
3057 <     * thenRunAsync result completes exceptionally after exceptional
3058 <     * completion of source
3059 <     */
3060 <    public void testThenRunAsync2() {
3061 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3062 <        Noop r = new Noop();
3063 <        CompletableFuture<Void> g = f.thenRunAsync(r);
3064 <        f.completeExceptionally(new CFException());
3065 <        try {
3066 <            g.join();
3067 <            shouldThrow();
3068 <        } catch (CompletionException success) {}
3069 <        checkCompletedWithWrappedCFException(g);
3070 <    }
3071 <
3072 <    /**
3073 <     * thenRunAsync result completes exceptionally if action does
3074 <     */
3075 <    public void testThenRunAsync3() {
3076 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3077 <        FailingNoop r = new FailingNoop();
3078 <        CompletableFuture<Void> g = f.thenRunAsync(r);
3079 <        f.complete(null);
3080 <        checkCompletedWithWrappedCFException(g);
3081 <    }
3082 <
3083 <    /**
3084 <     * thenRunAsync result completes exceptionally if source cancelled
3085 <     */
3086 <    public void testThenRunAsync4() {
3087 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3088 <        Noop r = new Noop();
3089 <        CompletableFuture<Void> g = f.thenRunAsync(r);
3090 <        assertTrue(f.cancel(true));
3091 <        checkCompletedWithWrappedCancellationException(g);
3092 <    }
3093 <
3094 <    /**
3095 <     * thenApplyAsync result completes normally after normal completion of source
3096 <     */
3097 <    public void testThenApplyAsync() {
3098 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3099 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
3100 <        f.complete(one);
3101 <        checkCompletedNormally(g, two);
3102 <    }
3103 <
3104 <    /**
3105 <     * thenApplyAsync result completes exceptionally after exceptional
3106 <     * completion of source
3107 <     */
3108 <    public void testThenApplyAsync2() {
3109 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3110 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
3111 <        f.completeExceptionally(new CFException());
3112 <        checkCompletedWithWrappedCFException(g);
3113 <    }
3114 <
3115 <    /**
3116 <     * thenApplyAsync result completes exceptionally if action does
3117 <     */
3118 <    public void testThenApplyAsync3() {
3119 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3120 <        FailingFunction r = new FailingFunction();
3121 <        CompletableFuture<Integer> g = f.thenApplyAsync(r);
3122 <        f.complete(null);
3123 <        checkCompletedWithWrappedCFException(g);
3124 <    }
3125 <
3126 <    /**
3127 <     * thenApplyAsync result completes exceptionally if source cancelled
3128 <     */
3129 <    public void testThenApplyAsync4() {
3130 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3131 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
3132 <        assertTrue(f.cancel(true));
3133 <        checkCompletedWithWrappedCancellationException(g);
3134 <    }
3135 <
3136 <    /**
3137 <     * thenAcceptAsync result completes normally after normal
3138 <     * completion of source
3139 <     */
3140 <    public void testThenAcceptAsync() {
3141 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3142 <        IncAction r = new IncAction();
3143 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
3144 <        f.complete(one);
3145 <        checkCompletedNormally(g, null);
3146 <        assertEquals(r.value, (Integer) 2);
3147 <    }
3148 <
3149 <    /**
3150 <     * thenAcceptAsync result completes exceptionally after exceptional
3151 <     * completion of source
3152 <     */
3153 <    public void testThenAcceptAsync2() {
3154 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3155 <        IncAction r = new IncAction();
3156 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
3157 <        f.completeExceptionally(new CFException());
3158 <        checkCompletedWithWrappedCFException(g);
3159 <    }
3160 <
3161 <    /**
3162 <     * thenAcceptAsync result completes exceptionally if action does
3163 <     */
3164 <    public void testThenAcceptAsync3() {
3165 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3166 <        FailingConsumer r = new FailingConsumer();
3167 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
3168 <        f.complete(null);
3169 <        checkCompletedWithWrappedCFException(g);
3170 <    }
3171 <
3172 <    /**
3173 <     * thenAcceptAsync result completes exceptionally if source cancelled
3174 <     */
3175 <    public void testThenAcceptAsync4() {
3176 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3177 <        IncAction r = new IncAction();
3178 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
3179 <        assertTrue(f.cancel(true));
3180 <        checkCompletedWithWrappedCancellationException(g);
3181 <    }
3182 <
3183 <    // async with explicit executors
3184 <
3185 <    /**
3186 <     * thenRunAsync result completes normally after normal completion of source
3187 <     */
3188 <    public void testThenRunAsyncE() {
3189 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3190 <        Noop r = new Noop();
3191 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3192 <        f.complete(null);
3193 <        checkCompletedNormally(g, null);
3194 <
3195 <        // reordered version
3196 <        f = new CompletableFuture<>();
3197 <        f.complete(null);
3198 <        r = new Noop();
3199 <        g = f.thenRunAsync(r, new ThreadExecutor());
3200 <        checkCompletedNormally(g, null);
3201 <    }
3202 <
3203 <    /**
3204 <     * thenRunAsync result completes exceptionally after exceptional
3205 <     * completion of source
3206 <     */
3207 <    public void testThenRunAsync2E() {
3208 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3209 <        Noop r = new Noop();
3210 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3211 <        f.completeExceptionally(new CFException());
3212 <        try {
3213 <            g.join();
3214 <            shouldThrow();
3215 <        } catch (CompletionException success) {}
3216 <        checkCompletedWithWrappedCFException(g);
3217 <    }
3218 <
3219 <    /**
3220 <     * thenRunAsync result completes exceptionally if action does
3221 <     */
3222 <    public void testThenRunAsync3E() {
3223 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3224 <        FailingNoop r = new FailingNoop();
3225 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3226 <        f.complete(null);
3227 <        checkCompletedWithWrappedCFException(g);
3228 <    }
3229 <
3230 <    /**
3231 <     * thenRunAsync result completes exceptionally if source cancelled
3232 <     */
3233 <    public void testThenRunAsync4E() {
3234 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3235 <        Noop r = new Noop();
3236 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3237 <        assertTrue(f.cancel(true));
3238 <        checkCompletedWithWrappedCancellationException(g);
3239 <    }
3240 <
3241 <    /**
3242 <     * thenApplyAsync result completes normally after normal completion of source
3243 <     */
3244 <    public void testThenApplyAsyncE() {
3245 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3246 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
3247 <        f.complete(one);
3248 <        checkCompletedNormally(g, two);
3249 <    }
3250 <
3251 <    /**
3252 <     * thenApplyAsync result completes exceptionally after exceptional
3253 <     * completion of source
3254 <     */
3255 <    public void testThenApplyAsync2E() {
3256 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3257 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
3258 <        f.completeExceptionally(new CFException());
3259 <        checkCompletedWithWrappedCFException(g);
3260 <    }
3261 <
3262 <    /**
3263 <     * thenApplyAsync result completes exceptionally if action does
3264 <     */
3265 <    public void testThenApplyAsync3E() {
3266 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3267 <        FailingFunction r = new FailingFunction();
3268 <        CompletableFuture<Integer> g = f.thenApplyAsync(r, new ThreadExecutor());
3269 <        f.complete(null);
3270 <        checkCompletedWithWrappedCFException(g);
3271 <    }
3272 <
3273 <    /**
3274 <     * thenApplyAsync result completes exceptionally if source cancelled
3275 <     */
3276 <    public void testThenApplyAsync4E() {
3277 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3278 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
3279 <        assertTrue(f.cancel(true));
3280 <        checkCompletedWithWrappedCancellationException(g);
3281 <    }
3282 <
3283 <    /**
3284 <     * thenAcceptAsync result completes normally after normal
3285 <     * completion of source
3286 <     */
3287 <    public void testThenAcceptAsyncE() {
3288 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3289 <        IncAction r = new IncAction();
3290 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3291 <        f.complete(one);
3292 <        checkCompletedNormally(g, null);
3293 <        assertEquals(r.value, (Integer) 2);
3294 <    }
3295 <
3296 <    /**
3297 <     * thenAcceptAsync result completes exceptionally after exceptional
3298 <     * completion of source
3299 <     */
3300 <    public void testThenAcceptAsync2E() {
3301 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3302 <        IncAction r = new IncAction();
3303 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3304 <        f.completeExceptionally(new CFException());
3305 <        checkCompletedWithWrappedCFException(g);
3306 <    }
3307 <
3308 <    /**
3309 <     * thenAcceptAsync result completes exceptionally if action does
3310 <     */
3311 <    public void testThenAcceptAsync3E() {
3312 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3313 <        FailingConsumer r = new FailingConsumer();
3314 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3315 <        f.complete(null);
3316 <        checkCompletedWithWrappedCFException(g);
3317 <    }
3318 <
3319 <    /**
3320 <     * thenAcceptAsync result completes exceptionally if source cancelled
3321 <     */
3322 <    public void testThenAcceptAsync4E() {
3323 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3324 <        IncAction r = new IncAction();
3325 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3326 <        assertTrue(f.cancel(true));
3327 <        checkCompletedWithWrappedCancellationException(g);
3328 <    }
2784 >    }}
2785  
2786      // other static methods
2787  
# Line 3344 | Line 2800 | public class CompletableFutureTest exten
2800       */
2801      public void testAllOf_normal() throws Exception {
2802          for (int k = 1; k < 20; ++k) {
2803 <            CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2803 >            CompletableFuture<Integer>[] fs
2804 >                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2805              for (int i = 0; i < k; ++i)
2806                  fs[i] = new CompletableFuture<>();
2807              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
# Line 3358 | Line 2815 | public class CompletableFutureTest exten
2815          }
2816      }
2817  
2818 +    public void testAllOf_backwards() throws Exception {
2819 +        for (int k = 1; k < 20; ++k) {
2820 +            CompletableFuture<Integer>[] fs
2821 +                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2822 +            for (int i = 0; i < k; ++i)
2823 +                fs[i] = new CompletableFuture<>();
2824 +            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
2825 +            for (int i = k - 1; i >= 0; i--) {
2826 +                checkIncomplete(f);
2827 +                checkIncomplete(CompletableFuture.allOf(fs));
2828 +                fs[i].complete(one);
2829 +            }
2830 +            checkCompletedNormally(f, null);
2831 +            checkCompletedNormally(CompletableFuture.allOf(fs), null);
2832 +        }
2833 +    }
2834 +
2835      /**
2836       * anyOf(no component futures) returns an incomplete future
2837       */
# Line 3416 | Line 2890 | public class CompletableFutureTest exten
2890          Runnable[] throwingActions = {
2891              () -> CompletableFuture.supplyAsync(null),
2892              () -> CompletableFuture.supplyAsync(null, exec),
2893 <            () -> CompletableFuture.supplyAsync(supplyOne, null),
2893 >            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null),
2894  
2895              () -> CompletableFuture.runAsync(null),
2896              () -> CompletableFuture.runAsync(null, exec),
# Line 3489 | Line 2963 | public class CompletableFutureTest exten
2963  
2964              () -> f.thenCompose(null),
2965              () -> f.thenComposeAsync(null),
2966 <            () -> f.thenComposeAsync(new CompletableFutureInc(), null),
2966 >            () -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null),
2967              () -> f.thenComposeAsync(null, exec),
2968  
2969              () -> f.exceptionally(null),
# Line 3521 | Line 2995 | public class CompletableFutureTest exten
2995          assertSame(f, f.toCompletableFuture());
2996      }
2997  
3524    /**
3525     * whenComplete action executes on normal completion, propagating
3526     * source result.
3527     */
3528    public void testWhenComplete_normalCompletion1() {
3529        for (ExecutionMode m : ExecutionMode.values())
3530        for (boolean createIncomplete : new boolean[] { true, false })
3531        for (Integer v1 : new Integer[] { 1, null })
3532    {
3533        final AtomicInteger a = new AtomicInteger(0);
3534        final CompletableFuture<Integer> f = new CompletableFuture<>();
3535        if (!createIncomplete) f.complete(v1);
3536        final CompletableFuture<Integer> g = m.whenComplete
3537            (f,
3538             (Integer x, Throwable t) -> {
3539                threadAssertSame(x, v1);
3540                threadAssertNull(t);
3541                a.getAndIncrement();
3542            });
3543        if (createIncomplete) f.complete(v1);
3544
3545        checkCompletedNormally(g, v1);
3546        checkCompletedNormally(f, v1);
3547        assertEquals(1, a.get());
3548    }}
3549
3550    /**
3551     * whenComplete action executes on exceptional completion, propagating
3552     * source result.
3553     */
3554    public void testWhenComplete_exceptionalCompletion() {
3555        for (ExecutionMode m : ExecutionMode.values())
3556        for (boolean createIncomplete : new boolean[] { true, false })
3557        for (Integer v1 : new Integer[] { 1, null })
3558    {
3559        final AtomicInteger a = new AtomicInteger(0);
3560        final CFException ex = new CFException();
3561        final CompletableFuture<Integer> f = new CompletableFuture<>();
3562        if (!createIncomplete) f.completeExceptionally(ex);
3563        final CompletableFuture<Integer> g = m.whenComplete
3564            (f,
3565             (Integer x, Throwable t) -> {
3566                threadAssertNull(x);
3567                threadAssertSame(t, ex);
3568                a.getAndIncrement();
3569            });
3570        if (createIncomplete) f.completeExceptionally(ex);
3571        checkCompletedWithWrappedCFException(f, ex);
3572        checkCompletedWithWrappedCFException(g, ex);
3573        assertEquals(1, a.get());
3574    }}
3575
3576    /**
3577     * whenComplete action executes on cancelled source, propagating
3578     * CancellationException.
3579     */
3580    public void testWhenComplete_sourceCancelled() {
3581        for (ExecutionMode m : ExecutionMode.values())
3582        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3583        for (boolean createIncomplete : new boolean[] { true, false })
3584    {
3585        final AtomicInteger a = new AtomicInteger(0);
3586        final CompletableFuture<Integer> f = new CompletableFuture<>();
3587        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
3588        final CompletableFuture<Integer> g = m.whenComplete
3589            (f,
3590             (Integer x, Throwable t) -> {
3591                threadAssertNull(x);
3592                threadAssertTrue(t instanceof CancellationException);
3593                a.getAndIncrement();
3594            });
3595        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
3596
3597        //try { g.join(); } catch (Throwable t) { throw new Error(t); }
3598        checkCompletedWithWrappedCancellationException(g);
3599        checkCancelled(f);
3600        assertEquals(1, a.get());
3601    }}
3602
3603    /**
3604     * If a whenComplete action throws an exception when triggered by
3605     * a normal completion, it completes exceptionally
3606     */
3607    public void testWhenComplete_actionFailed() {
3608        for (boolean createIncomplete : new boolean[] { true, false })
3609        for (ExecutionMode m : ExecutionMode.values())
3610        for (Integer v1 : new Integer[] { 1, null }) {
3611
3612        final AtomicInteger a = new AtomicInteger(0);
3613        final CFException ex = new CFException();
3614        final CompletableFuture<Integer> f = new CompletableFuture<>();
3615        if (!createIncomplete) f.complete(v1);
3616        final CompletableFuture<Integer> g = m.whenComplete
3617            (f,
3618             (Integer x, Throwable t) -> {
3619                threadAssertSame(x, v1);
3620                threadAssertNull(t);
3621                a.getAndIncrement();
3622                throw ex;
3623            });
3624        if (createIncomplete) f.complete(v1);
3625        checkCompletedNormally(f, v1);
3626        checkCompletedWithWrappedCFException(g, ex);
3627        assertEquals(1, a.get());
3628        }
3629    }
3630
3631    /**
3632     * If a whenComplete action throws an exception when triggered by
3633     * a source completion that also throws an exception, the source
3634     * exception takes precedence.
3635     */
3636    public void testWhenComplete_actionFailedSourceFailed() {
3637        for (boolean createIncomplete : new boolean[] { true, false })
3638        for (ExecutionMode m : ExecutionMode.values())
3639        for (Integer v1 : new Integer[] { 1, null }) {
3640
3641        final AtomicInteger a = new AtomicInteger(0);
3642        final CFException ex1 = new CFException();
3643        final CFException ex2 = new CFException();
3644        final CompletableFuture<Integer> f = new CompletableFuture<>();
3645
3646        if (!createIncomplete) f.completeExceptionally(ex1);
3647        final CompletableFuture<Integer> g = m.whenComplete
3648            (f,
3649             (Integer x, Throwable t) -> {
3650                threadAssertSame(t, ex1);
3651                threadAssertNull(x);
3652                a.getAndIncrement();
3653                throw ex2;
3654            });
3655        if (createIncomplete) f.completeExceptionally(ex1);
3656
3657        checkCompletedWithWrappedCFException(f, ex1);
3658        checkCompletedWithWrappedCFException(g, ex1);
3659        assertEquals(1, a.get());
3660        }
3661    }
3662
2998   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines