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.74 by jsr166, Fri Jun 6 21:19:22 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
# Line 282 | Line 319 | public class CompletableFutureTest exten
319      public void testGetNumberOfDependents() {
320          CompletableFuture<Integer> f = new CompletableFuture<>();
321          assertEquals(0, f.getNumberOfDependents());
322 <        CompletableFuture g = f.thenRun(new Noop());
322 >        CompletableFuture g = f.thenRun(new Noop(ExecutionMode.DEFAULT));
323          assertEquals(1, f.getNumberOfDependents());
324          assertEquals(0, g.getNumberOfDependents());
325 <        CompletableFuture h = f.thenRun(new Noop());
325 >        CompletableFuture h = f.thenRun(new Noop(ExecutionMode.DEFAULT));
326          assertEquals(2, f.getNumberOfDependents());
327          f.complete(1);
328          checkCompletedNormally(g, null);
# Line 308 | Line 345 | public class CompletableFutureTest exten
345          f = new CompletableFuture<String>();
346          f.completeExceptionally(new IndexOutOfBoundsException());
347          assertTrue(f.toString().contains("[Completed exceptionally]"));
348 +
349 +        f = new CompletableFuture<String>();
350 +        f.cancel(true);
351 +        assertTrue(f.toString().contains("[Completed exceptionally]"));
352 +
353 +        f = new CompletableFuture<String>();
354 +        f.cancel(false);
355 +        assertTrue(f.toString().contains("[Completed exceptionally]"));
356      }
357  
358      /**
# Line 318 | Line 363 | public class CompletableFutureTest exten
363          checkCompletedNormally(f, "test");
364      }
365  
366 <    // Choose non-commutative actions for better coverage
366 >    abstract class CheckedAction {
367 >        int invocationCount = 0;
368 >        final ExecutionMode m;
369 >        CheckedAction(ExecutionMode m) { this.m = m; }
370 >        void invoked() {
371 >            m.checkExecutionMode();
372 >            assertEquals(0, invocationCount++);
373 >        }
374 >        void assertNotInvoked() { assertEquals(0, invocationCount); }
375 >        void assertInvoked() { assertEquals(1, invocationCount); }
376 >    }
377  
378 <    // A non-commutative function that handles and produces null values as well.
379 <    static Integer subtract(Integer x, Integer y) {
380 <        return (x == null && y == null) ? null :
381 <            ((x == null) ? 42 : x.intValue())
382 <            - ((y == null) ? 99 : y.intValue());
378 >    abstract class CheckedIntegerAction extends CheckedAction {
379 >        Integer value;
380 >        CheckedIntegerAction(ExecutionMode m) { super(m); }
381 >        void assertValue(Integer expected) {
382 >            assertInvoked();
383 >            assertEquals(expected, value);
384 >        }
385 >    }
386 >
387 >    class IntegerSupplier extends CheckedAction
388 >        implements Supplier<Integer>
389 >    {
390 >        final Integer value;
391 >        IntegerSupplier(ExecutionMode m, Integer value) {
392 >            super(m);
393 >            this.value = value;
394 >        }
395 >        public Integer get() {
396 >            invoked();
397 >            return value;
398 >        }
399      }
400  
401      // A function that handles and produces null values as well.
# Line 332 | Line 403 | public class CompletableFutureTest exten
403          return (x == null) ? null : x + 1;
404      }
405  
406 <    static final Supplier<Integer> supplyOne =
407 <        () -> Integer.valueOf(1);
408 <    static final Function<Integer, Integer> inc =
409 <        (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;
406 >    class NoopConsumer extends CheckedIntegerAction
407 >        implements Consumer<Integer>
408 >    {
409 >        NoopConsumer(ExecutionMode m) { super(m); }
410          public void accept(Integer x) {
411 <            invocationCount++;
412 <            value = inc(x);
411 >            invoked();
412 >            value = x;
413          }
414      }
415 <    static final class IncFunction implements Function<Integer,Integer> {
416 <        int invocationCount = 0;
417 <        Integer value;
415 >
416 >    class IncFunction extends CheckedIntegerAction
417 >        implements Function<Integer,Integer>
418 >    {
419 >        IncFunction(ExecutionMode m) { super(m); }
420          public Integer apply(Integer x) {
421 <            invocationCount++;
421 >            invoked();
422              return value = inc(x);
423          }
424      }
425 <    static final class SubtractAction implements BiConsumer<Integer, Integer> {
426 <        int invocationCount = 0;
427 <        Integer value;
428 <        // Check this action was invoked exactly once when result is computed.
425 >
426 >    // Choose non-commutative actions for better coverage
427 >    // A non-commutative function that handles and produces null values as well.
428 >    static Integer subtract(Integer x, Integer y) {
429 >        return (x == null && y == null) ? null :
430 >            ((x == null) ? 42 : x.intValue())
431 >            - ((y == null) ? 99 : y.intValue());
432 >    }
433 >
434 >    class SubtractAction extends CheckedIntegerAction
435 >        implements BiConsumer<Integer, Integer>
436 >    {
437 >        SubtractAction(ExecutionMode m) { super(m); }
438          public void accept(Integer x, Integer y) {
439 <            invocationCount++;
439 >            invoked();
440              value = subtract(x, y);
441          }
442      }
443 <    static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> {
444 <        int invocationCount = 0;
445 <        Integer value;
446 <        // Check this action was invoked exactly once when result is computed.
443 >
444 >    class SubtractFunction extends CheckedIntegerAction
445 >        implements BiFunction<Integer, Integer, Integer>
446 >    {
447 >        SubtractFunction(ExecutionMode m) { super(m); }
448          public Integer apply(Integer x, Integer y) {
449 <            invocationCount++;
449 >            invoked();
450              return value = subtract(x, y);
451          }
452      }
453 <    static final class Noop implements Runnable {
454 <        int invocationCount = 0;
453 >
454 >    class Noop extends CheckedAction implements Runnable {
455 >        Noop(ExecutionMode m) { super(m); }
456          public void run() {
457 <            invocationCount++;
457 >            invoked();
458          }
459      }
460  
461 <    static final class FailingSupplier implements Supplier<Integer> {
462 <        int invocationCount = 0;
461 >    class FailingSupplier extends CheckedAction
462 >        implements Supplier<Integer>
463 >    {
464 >        FailingSupplier(ExecutionMode m) { super(m); }
465          public Integer get() {
466 <            invocationCount++;
466 >            invoked();
467              throw new CFException();
468          }
469      }
470 <    static final class FailingConsumer implements Consumer<Integer> {
471 <        int invocationCount = 0;
470 >
471 >    class FailingConsumer extends CheckedIntegerAction
472 >        implements Consumer<Integer>
473 >    {
474 >        FailingConsumer(ExecutionMode m) { super(m); }
475          public void accept(Integer x) {
476 <            invocationCount++;
476 >            invoked();
477 >            value = x;
478              throw new CFException();
479          }
480      }
481 <    static final class FailingBiConsumer implements BiConsumer<Integer, Integer> {
482 <        int invocationCount = 0;
481 >
482 >    class FailingBiConsumer extends CheckedIntegerAction
483 >        implements BiConsumer<Integer, Integer>
484 >    {
485 >        FailingBiConsumer(ExecutionMode m) { super(m); }
486          public void accept(Integer x, Integer y) {
487 <            invocationCount++;
487 >            invoked();
488 >            value = subtract(x, y);
489              throw new CFException();
490          }
491      }
492 <    static final class FailingFunction implements Function<Integer, Integer> {
493 <        int invocationCount = 0;
492 >
493 >    class FailingFunction extends CheckedIntegerAction
494 >        implements Function<Integer, Integer>
495 >    {
496 >        FailingFunction(ExecutionMode m) { super(m); }
497          public Integer apply(Integer x) {
498 <            invocationCount++;
498 >            invoked();
499 >            value = x;
500              throw new CFException();
501          }
502      }
503 <    static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> {
504 <        int invocationCount = 0;
503 >
504 >    class FailingBiFunction extends CheckedIntegerAction
505 >        implements BiFunction<Integer, Integer, Integer>
506 >    {
507 >        FailingBiFunction(ExecutionMode m) { super(m); }
508          public Integer apply(Integer x, Integer y) {
509 <            invocationCount++;
509 >            invoked();
510 >            value = subtract(x, y);
511              throw new CFException();
512          }
513      }
514 <    static final class FailingNoop implements Runnable {
515 <        int invocationCount = 0;
514 >
515 >    class FailingRunnable extends CheckedAction implements Runnable {
516 >        FailingRunnable(ExecutionMode m) { super(m); }
517          public void run() {
518 <            invocationCount++;
518 >            invoked();
519              throw new CFException();
520          }
521      }
522  
523 <    static final class CompletableFutureInc
524 <        implements Function<Integer, CompletableFuture<Integer>> {
525 <        int invocationCount = 0;
523 >
524 >    class CompletableFutureInc extends CheckedIntegerAction
525 >        implements Function<Integer, CompletableFuture<Integer>>
526 >    {
527 >        CompletableFutureInc(ExecutionMode m) { super(m); }
528          public CompletableFuture<Integer> apply(Integer x) {
529 <            invocationCount++;
529 >            invoked();
530 >            value = x;
531              CompletableFuture<Integer> f = new CompletableFuture<>();
532              f.complete(inc(x));
533              return f;
534          }
535      }
536  
537 <    static final class FailingCompletableFutureFunction
538 <        implements Function<Integer, CompletableFuture<Integer>> {
539 <        int invocationCount = 0;
537 >    class FailingCompletableFutureFunction extends CheckedIntegerAction
538 >        implements Function<Integer, CompletableFuture<Integer>>
539 >    {
540 >        FailingCompletableFutureFunction(ExecutionMode m) { super(m); }
541          public CompletableFuture<Integer> apply(Integer x) {
542 <            invocationCount++;
542 >            invoked();
543 >            value = x;
544              throw new CFException();
545          }
546      }
547  
548      // Used for explicit executor tests
549      static final class ThreadExecutor implements Executor {
550 <        AtomicInteger count = new AtomicInteger(0);
550 >        final AtomicInteger count = new AtomicInteger(0);
551 >        static final ThreadGroup tg = new ThreadGroup("ThreadExecutor");
552 >        static boolean startedCurrentThread() {
553 >            return Thread.currentThread().getThreadGroup() == tg;
554 >        }
555  
556          public void execute(Runnable r) {
557              count.getAndIncrement();
558 <            new Thread(r).start();
558 >            new Thread(tg, r).start();
559          }
560      }
561  
562      /**
563       * Permits the testing of parallel code for the 3 different
564 <     * execution modes without repeating all the testing code.
564 >     * execution modes without copy/pasting all the test methods.
565       */
566      enum ExecutionMode {
567          DEFAULT {
568 +            public void checkExecutionMode() {
569 +                assertFalse(ThreadExecutor.startedCurrentThread());
570 +                assertNull(ForkJoinTask.getPool());
571 +            }
572 +            public CompletableFuture<Void> runAsync(Runnable a) {
573 +                throw new UnsupportedOperationException();
574 +            }
575 +            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
576 +                throw new UnsupportedOperationException();
577 +            }
578              public <T> CompletableFuture<Void> thenRun
579                  (CompletableFuture<T> f, Runnable a) {
580                  return f.thenRun(a);
# Line 521 | Line 638 | public class CompletableFutureTest exten
638              }
639          },
640  
641 <        DEFAULT_ASYNC {
641 >        ASYNC {
642 >            public void checkExecutionMode() {
643 >                assertSame(ForkJoinPool.commonPool(),
644 >                           ForkJoinTask.getPool());
645 >            }
646 >            public CompletableFuture<Void> runAsync(Runnable a) {
647 >                return CompletableFuture.runAsync(a);
648 >            }
649 >            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
650 >                return CompletableFuture.supplyAsync(a);
651 >            }
652              public <T> CompletableFuture<Void> thenRun
653                  (CompletableFuture<T> f, Runnable a) {
654                  return f.thenRunAsync(a);
# Line 586 | Line 713 | public class CompletableFutureTest exten
713          },
714  
715          EXECUTOR {
716 +            public void checkExecutionMode() {
717 +                assertTrue(ThreadExecutor.startedCurrentThread());
718 +            }
719 +            public CompletableFuture<Void> runAsync(Runnable a) {
720 +                return CompletableFuture.runAsync(a, new ThreadExecutor());
721 +            }
722 +            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
723 +                return CompletableFuture.supplyAsync(a, new ThreadExecutor());
724 +            }
725              public <T> CompletableFuture<Void> thenRun
726                  (CompletableFuture<T> f, Runnable a) {
727                  return f.thenRunAsync(a, new ThreadExecutor());
# Line 649 | Line 785 | public class CompletableFutureTest exten
785              }
786          };
787  
788 +        public abstract void checkExecutionMode();
789 +        public abstract CompletableFuture<Void> runAsync(Runnable a);
790 +        public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a);
791          public abstract <T> CompletableFuture<Void> thenRun
792              (CompletableFuture<T> f, Runnable a);
793          public abstract <T> CompletableFuture<Void> thenAccept
# Line 727 | Line 866 | public class CompletableFutureTest exten
866          if (!createIncomplete) f.completeExceptionally(ex);
867          final CompletableFuture<Integer> g = f.exceptionally
868              ((Throwable t) -> {
869 +                ExecutionMode.DEFAULT.checkExecutionMode();
870                  threadAssertSame(t, ex);
871                  a.getAndIncrement();
872                  return v1;
# Line 748 | Line 888 | public class CompletableFutureTest exten
888          if (!createIncomplete) f.completeExceptionally(ex1);
889          final CompletableFuture<Integer> g = f.exceptionally
890              ((Throwable t) -> {
891 +                ExecutionMode.DEFAULT.checkExecutionMode();
892                  threadAssertSame(t, ex1);
893                  a.getAndIncrement();
894                  throw ex2;
895              });
896          if (createIncomplete) f.completeExceptionally(ex1);
897  
898 <        checkCompletedWithWrappedCFException(g, ex2);
898 >        checkCompletedWithWrappedException(g, ex2);
899          assertEquals(1, a.get());
900      }}
901  
# Line 773 | Line 914 | public class CompletableFutureTest exten
914          final CompletableFuture<Integer> g = m.handle
915              (f,
916               (Integer x, Throwable t) -> {
917 +                m.checkExecutionMode();
918                  threadAssertSame(x, v1);
919                  threadAssertNull(t);
920                  a.getAndIncrement();
# Line 801 | Line 943 | public class CompletableFutureTest exten
943          final CompletableFuture<Integer> g = m.handle
944              (f,
945               (Integer x, Throwable t) -> {
946 +                m.checkExecutionMode();
947                  threadAssertNull(x);
948                  threadAssertSame(t, ex);
949                  a.getAndIncrement();
# Line 809 | Line 952 | public class CompletableFutureTest exten
952          if (createIncomplete) f.completeExceptionally(ex);
953  
954          checkCompletedNormally(g, v1);
955 <        checkCompletedWithWrappedCFException(f, ex);
955 >        checkCompletedExceptionally(f, ex);
956          assertEquals(1, a.get());
957      }}
958  
# Line 829 | Line 972 | public class CompletableFutureTest exten
972          final CompletableFuture<Integer> g = m.handle
973              (f,
974               (Integer x, Throwable t) -> {
975 +                m.checkExecutionMode();
976                  threadAssertNull(x);
977                  threadAssertTrue(t instanceof CancellationException);
978                  a.getAndIncrement();
# Line 856 | Line 1000 | public class CompletableFutureTest exten
1000          final CompletableFuture<Integer> g = m.handle
1001              (f,
1002               (Integer x, Throwable t) -> {
1003 +                m.checkExecutionMode();
1004                  threadAssertNull(x);
1005                  threadAssertSame(ex1, t);
1006                  a.getAndIncrement();
# Line 863 | Line 1008 | public class CompletableFutureTest exten
1008              });
1009          if (createIncomplete) f.completeExceptionally(ex1);
1010  
1011 <        checkCompletedWithWrappedCFException(g, ex2);
1012 <        checkCompletedWithWrappedCFException(f, ex1);
1011 >        checkCompletedWithWrappedException(g, ex2);
1012 >        checkCompletedExceptionally(f, ex1);
1013          assertEquals(1, a.get());
1014      }}
1015  
# Line 880 | Line 1025 | public class CompletableFutureTest exten
1025          final CompletableFuture<Integer> g = m.handle
1026              (f,
1027               (Integer x, Throwable t) -> {
1028 +                m.checkExecutionMode();
1029                  threadAssertSame(x, v1);
1030                  threadAssertNull(t);
1031                  a.getAndIncrement();
# Line 887 | Line 1033 | public class CompletableFutureTest exten
1033              });
1034          if (createIncomplete) f.complete(v1);
1035  
1036 <        checkCompletedWithWrappedCFException(g, ex);
1036 >        checkCompletedWithWrappedException(g, ex);
1037          checkCompletedNormally(f, v1);
1038          assertEquals(1, a.get());
1039      }}
# Line 895 | Line 1041 | public class CompletableFutureTest exten
1041      /**
1042       * runAsync completes after running Runnable
1043       */
1044 <    public void testRunAsync() {
1045 <        Noop r = new Noop();
1046 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
1047 <        assertNull(f.join());
1048 <        assertEquals(1, r.invocationCount);
1049 <        checkCompletedNormally(f, null);
1050 <    }
1051 <
1052 <    /**
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);
1044 >    public void testRunAsync_normalCompletion() {
1045 >        ExecutionMode[] executionModes = {
1046 >            ExecutionMode.ASYNC,
1047 >            ExecutionMode.EXECUTOR,
1048 >        };
1049 >        for (ExecutionMode m : executionModes)
1050 >    {
1051 >        final Noop r = new Noop(m);
1052 >        final CompletableFuture<Void> f = m.runAsync(r);
1053          assertNull(f.join());
914        assertEquals(1, r.invocationCount);
1054          checkCompletedNormally(f, null);
1055 <        assertEquals(1, exec.count.get());
1056 <    }
1055 >        r.assertInvoked();
1056 >    }}
1057  
1058      /**
1059       * failing runAsync completes exceptionally after running Runnable
1060       */
1061 <    public void testRunAsync3() {
1062 <        FailingNoop r = new FailingNoop();
1063 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
1061 >    public void testRunAsync_exceptionalCompletion() {
1062 >        ExecutionMode[] executionModes = {
1063 >            ExecutionMode.ASYNC,
1064 >            ExecutionMode.EXECUTOR,
1065 >        };
1066 >        for (ExecutionMode m : executionModes)
1067 >    {
1068 >        final FailingRunnable r = new FailingRunnable(m);
1069 >        final CompletableFuture<Void> f = m.runAsync(r);
1070          checkCompletedWithWrappedCFException(f);
1071 <        assertEquals(1, r.invocationCount);
1072 <    }
1071 >        r.assertInvoked();
1072 >    }}
1073  
1074      /**
1075       * supplyAsync completes with result of supplier
1076       */
1077 <    public void testSupplyAsync() {
1078 <        CompletableFuture<Integer> f;
1079 <        f = CompletableFuture.supplyAsync(supplyOne);
1080 <        assertEquals(f.join(), one);
1081 <        checkCompletedNormally(f, one);
1082 <    }
1083 <
1084 <    /**
1085 <     * supplyAsync with executor completes with result of supplier
1086 <     */
1087 <    public void testSupplyAsync2() {
1088 <        CompletableFuture<Integer> f;
1089 <        f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor());
1090 <        assertEquals(f.join(), one);
946 <        checkCompletedNormally(f, one);
947 <    }
1077 >    public void testSupplyAsync_normalCompletion() {
1078 >        ExecutionMode[] executionModes = {
1079 >            ExecutionMode.ASYNC,
1080 >            ExecutionMode.EXECUTOR,
1081 >        };
1082 >        for (ExecutionMode m : executionModes)
1083 >        for (Integer v1 : new Integer[] { 1, null })
1084 >    {
1085 >        final IntegerSupplier r = new IntegerSupplier(m, v1);
1086 >        final CompletableFuture<Integer> f = m.supplyAsync(r);
1087 >        assertSame(v1, f.join());
1088 >        checkCompletedNormally(f, v1);
1089 >        r.assertInvoked();
1090 >    }}
1091  
1092      /**
1093       * Failing supplyAsync completes exceptionally
1094       */
1095 <    public void testSupplyAsync3() {
1096 <        FailingSupplier r = new FailingSupplier();
1097 <        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r);
1095 >    public void testSupplyAsync_exceptionalCompletion() {
1096 >        ExecutionMode[] executionModes = {
1097 >            ExecutionMode.ASYNC,
1098 >            ExecutionMode.EXECUTOR,
1099 >        };
1100 >        for (ExecutionMode m : executionModes)
1101 >    {
1102 >        FailingSupplier r = new FailingSupplier(m);
1103 >        CompletableFuture<Integer> f = m.supplyAsync(r);
1104          checkCompletedWithWrappedCFException(f);
1105 <        assertEquals(1, r.invocationCount);
1106 <    }
1105 >        r.assertInvoked();
1106 >    }}
1107  
1108      // seq completion methods
1109  
1110      /**
1111       * thenRun result completes normally after normal completion of source
1112       */
1113 <    public void testThenRun() {
1114 <        CompletableFuture<Integer> f;
1115 <        CompletableFuture<Void> g;
1116 <        Noop r;
1117 <
1118 <        f = new CompletableFuture<>();
1119 <        g = f.thenRun(r = new Noop());
1120 <        f.complete(null);
1121 <        checkCompletedNormally(g, null);
1122 <        assertEquals(1, r.invocationCount);
1113 >    public void testThenRun_normalCompletion() {
1114 >        for (ExecutionMode m : ExecutionMode.values())
1115 >        for (boolean createIncomplete : new boolean[] { true, false })
1116 >        for (Integer v1 : new Integer[] { 1, null })
1117 >    {
1118 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1119 >        final Noop r = new Noop(m);
1120 >        if (!createIncomplete) f.complete(v1);
1121 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1122 >        if (createIncomplete) {
1123 >            checkIncomplete(g);
1124 >            f.complete(v1);
1125 >        }
1126  
975        f = new CompletableFuture<>();
976        f.complete(null);
977        g = f.thenRun(r = new Noop());
1127          checkCompletedNormally(g, null);
1128 <        assertEquals(1, r.invocationCount);
1129 <    }
1128 >        checkCompletedNormally(f, v1);
1129 >        r.assertInvoked();
1130 >    }}
1131  
1132      /**
1133       * thenRun result completes exceptionally after exceptional
1134       * completion of source
1135       */
1136 <    public void testThenRun2() {
1137 <        CompletableFuture<Integer> f;
1138 <        CompletableFuture<Void> g;
1139 <        Noop r;
1140 <
1141 <        f = new CompletableFuture<>();
1142 <        g = f.thenRun(r = new Noop());
1143 <        f.completeExceptionally(new CFException());
1144 <        checkCompletedWithWrappedCFException(g);
1145 <        assertEquals(0, r.invocationCount);
1146 <
1147 <        f = new CompletableFuture<>();
1148 <        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);
1136 >    public void testThenRun_exceptionalCompletion() {
1137 >        for (ExecutionMode m : ExecutionMode.values())
1138 >        for (boolean createIncomplete : new boolean[] { true, false })
1139 >    {
1140 >        final CFException ex = new CFException();
1141 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1142 >        final Noop r = new Noop(m);
1143 >        if (!createIncomplete) f.completeExceptionally(ex);
1144 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1145 >        if (createIncomplete) {
1146 >            checkIncomplete(g);
1147 >            f.completeExceptionally(ex);
1148 >        }
1149  
1150 <        f = new CompletableFuture<>();
1151 <        f.complete(null);
1152 <        g = f.thenRun(r = new FailingNoop());
1153 <        checkCompletedWithWrappedCFException(g);
1021 <    }
1150 >        checkCompletedWithWrappedException(g, ex);
1151 >        checkCompletedExceptionally(f, ex);
1152 >        r.assertNotInvoked();
1153 >    }}
1154  
1155      /**
1156       * thenRun result completes exceptionally if source cancelled
1157       */
1158 <    public void testThenRun4() {
1159 <        CompletableFuture<Integer> f;
1160 <        CompletableFuture<Void> g;
1161 <        Noop r;
1162 <
1163 <        f = new CompletableFuture<>();
1164 <        g = f.thenRun(r = new Noop());
1165 <        assertTrue(f.cancel(true));
1166 <        checkCompletedWithWrappedCancellationException(g);
1167 <
1168 <        f = new CompletableFuture<>();
1169 <        assertTrue(f.cancel(true));
1170 <        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 <    }
1158 >    public void testThenRun_sourceCancelled() {
1159 >        for (ExecutionMode m : ExecutionMode.values())
1160 >        for (boolean createIncomplete : new boolean[] { true, false })
1161 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1162 >    {
1163 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1164 >        final Noop r = new Noop(m);
1165 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1166 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1167 >        if (createIncomplete) {
1168 >            checkIncomplete(g);
1169 >            assertTrue(f.cancel(mayInterruptIfRunning));
1170 >        }
1171  
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));
1172          checkCompletedWithWrappedCancellationException(g);
1173 <    }
1174 <
1175 <    /**
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 <    }
1173 >        checkCancelled(f);
1174 >        r.assertNotInvoked();
1175 >    }}
1176  
1177      /**
1178 <     * thenAccept result completes exceptionally after exceptional
1097 <     * completion of source
1178 >     * thenRun result completes exceptionally if action does
1179       */
1180 <    public void testThenAccept2() {
1181 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1182 <        IncAction r = new IncAction();
1183 <        CompletableFuture<Void> g = f.thenAccept(r);
1184 <        f.completeExceptionally(new CFException());
1185 <        checkCompletedWithWrappedCFException(g);
1186 <    }
1180 >    public void testThenRun_actionFailed() {
1181 >        for (ExecutionMode m : ExecutionMode.values())
1182 >        for (boolean createIncomplete : new boolean[] { true, false })
1183 >        for (Integer v1 : new Integer[] { 1, null })
1184 >    {
1185 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1186 >        final FailingRunnable r = new FailingRunnable(m);
1187 >        if (!createIncomplete) f.complete(v1);
1188 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1189 >        if (createIncomplete) {
1190 >            checkIncomplete(g);
1191 >            f.complete(v1);
1192 >        }
1193  
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);
1194          checkCompletedWithWrappedCFException(g);
1195 <        assertEquals(1, r.invocationCount);
1196 <    }
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 <    }
1195 >        checkCompletedNormally(f, v1);
1196 >    }}
1197  
1198      /**
1199 <     * thenCombine result completes normally after normal completion
1132 <     * of sources
1199 >     * thenApply result completes normally after normal completion of source
1200       */
1201 <    public void testThenCombine_normalCompletion1() {
1135 <        for (boolean createIncomplete : new boolean[] { true, false })
1136 <        for (boolean fFirst : new boolean[] { true, false })
1201 >    public void testThenApply_normalCompletion() {
1202          for (ExecutionMode m : ExecutionMode.values())
1203 +        for (boolean createIncomplete : new boolean[] { true, false })
1204          for (Integer v1 : new Integer[] { 1, null })
1205 <        for (Integer v2 : new Integer[] { 2, null }) {
1140 <
1205 >    {
1206          final CompletableFuture<Integer> f = new CompletableFuture<>();
1207 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1208 <        final SubtractFunction r = new SubtractFunction();
1209 <        CompletableFuture<Integer> h = null;
1210 <        if (createIncomplete) h = m.thenCombine(f, g, r);
1211 <
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)
1207 >        final IncFunction r = new IncFunction(m);
1208 >        if (!createIncomplete) f.complete(v1);
1209 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1210 >        if (createIncomplete) {
1211 >            checkIncomplete(g);
1212              f.complete(v1);
1213 <        else
1156 <            g.complete(v2);
1157 <        if (!createIncomplete) h = m.thenCombine(f, g, r);
1213 >        }
1214  
1215 <        checkCompletedNormally(h, subtract(v1, v2));
1215 >        checkCompletedNormally(g, inc(v1));
1216          checkCompletedNormally(f, v1);
1217 <        checkCompletedNormally(g, v2);
1218 <        assertEquals(1, r.invocationCount);
1163 <        }
1164 <    }
1217 >        r.assertValue(inc(v1));
1218 >    }}
1219  
1220      /**
1221 <     * thenCombine result completes exceptionally after exceptional
1222 <     * completion of either source
1221 >     * thenApply result completes exceptionally after exceptional
1222 >     * completion of source
1223       */
1224 <    public void testThenCombine_exceptionalCompletion1() {
1224 >    public void testThenApply_exceptionalCompletion() {
1225          for (ExecutionMode m : ExecutionMode.values())
1226 <        for (Integer v1 : new Integer[] { 1, null }) {
1227 <
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);
1226 >        for (boolean createIncomplete : new boolean[] { true, false })
1227 >    {
1228          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
1229          final CompletableFuture<Integer> f = new CompletableFuture<>();
1230 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1231 <        final SubtractFunction r = new SubtractFunction();
1232 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1233 <        final CFException ex = new CFException();
1234 <
1235 <        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);
1230 >        final IncFunction r = new IncFunction(m);
1231 >        if (!createIncomplete) f.completeExceptionally(ex);
1232 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1233 >        if (createIncomplete) {
1234 >            checkIncomplete(g);
1235 >            f.completeExceptionally(ex);
1236          }
1210    }
1237  
1238 <    public void testThenCombine_exceptionalCompletion3() {
1239 <        for (ExecutionMode m : ExecutionMode.values())
1240 <        for (Integer v1 : new Integer[] { 1, null }) {
1241 <
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);
1224 <
1225 <        checkCompletedWithWrappedCFException(h, ex);
1226 <        checkCompletedWithWrappedCFException(g, ex);
1227 <        assertEquals(0, r.invocationCount);
1228 <        checkCompletedNormally(f, v1);
1229 <        }
1230 <    }
1238 >        checkCompletedWithWrappedException(g, ex);
1239 >        checkCompletedExceptionally(f, ex);
1240 >        r.assertNotInvoked();
1241 >    }}
1242  
1243 <    public void testThenCombine_exceptionalCompletion4() {
1243 >    /**
1244 >     * thenApply result completes exceptionally if source cancelled
1245 >     */
1246 >    public void testThenApply_sourceCancelled() {
1247          for (ExecutionMode m : ExecutionMode.values())
1248 <        for (Integer v1 : new Integer[] { 1, null }) {
1249 <
1248 >        for (boolean createIncomplete : new boolean[] { true, false })
1249 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1250 >    {
1251          final CompletableFuture<Integer> f = new CompletableFuture<>();
1252 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1253 <        final SubtractFunction r = new SubtractFunction();
1254 <        final CFException ex = new CFException();
1255 <
1256 <        f.completeExceptionally(ex);
1257 <        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);
1252 >        final IncFunction r = new IncFunction(m);
1253 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1254 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1255 >        if (createIncomplete) {
1256 >            checkIncomplete(g);
1257 >            assertTrue(f.cancel(mayInterruptIfRunning));
1258          }
1259 <    }
1259 >
1260 >        checkCompletedWithWrappedCancellationException(g);
1261 >        checkCancelled(f);
1262 >        r.assertNotInvoked();
1263 >    }}
1264  
1265      /**
1266 <     * thenCombine result completes exceptionally if action does
1266 >     * thenApply result completes exceptionally if action does
1267       */
1268 <    public void testThenCombine_actionFailed1() {
1268 >    public void testThenApply_actionFailed() {
1269          for (ExecutionMode m : ExecutionMode.values())
1270 +        for (boolean createIncomplete : new boolean[] { true, false })
1271          for (Integer v1 : new Integer[] { 1, null })
1272 <        for (Integer v2 : new Integer[] { 2, null }) {
1259 <
1272 >    {
1273          final CompletableFuture<Integer> f = new CompletableFuture<>();
1274 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1275 <        final FailingBiFunction r = new FailingBiFunction();
1276 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1277 <
1278 <        f.complete(v1);
1279 <        checkIncomplete(h);
1280 <        g.complete(v2);
1274 >        final FailingFunction r = new FailingFunction(m);
1275 >        if (!createIncomplete) f.complete(v1);
1276 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1277 >        if (createIncomplete) {
1278 >            checkIncomplete(g);
1279 >            f.complete(v1);
1280 >        }
1281  
1282 <        checkCompletedWithWrappedCFException(h);
1282 >        checkCompletedWithWrappedCFException(g);
1283          checkCompletedNormally(f, v1);
1284 <        checkCompletedNormally(g, v2);
1272 <        }
1273 <    }
1284 >    }}
1285  
1286 <    public void testThenCombine_actionFailed2() {
1286 >    /**
1287 >     * thenAccept result completes normally after normal completion of source
1288 >     */
1289 >    public void testThenAccept_normalCompletion() {
1290          for (ExecutionMode m : ExecutionMode.values())
1291 +        for (boolean createIncomplete : new boolean[] { true, false })
1292          for (Integer v1 : new Integer[] { 1, null })
1293 <        for (Integer v2 : new Integer[] { 2, null }) {
1279 <
1293 >    {
1294          final CompletableFuture<Integer> f = new CompletableFuture<>();
1295 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1296 <        final FailingBiFunction r = new FailingBiFunction();
1297 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1298 <
1299 <        g.complete(v2);
1300 <        checkIncomplete(h);
1301 <        f.complete(v1);
1295 >        final NoopConsumer r = new NoopConsumer(m);
1296 >        if (!createIncomplete) f.complete(v1);
1297 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1298 >        if (createIncomplete) {
1299 >            checkIncomplete(g);
1300 >            f.complete(v1);
1301 >        }
1302  
1303 <        checkCompletedWithWrappedCFException(h);
1303 >        checkCompletedNormally(g, null);
1304 >        r.assertValue(v1);
1305          checkCompletedNormally(f, v1);
1306 <        checkCompletedNormally(g, v2);
1292 <        }
1293 <    }
1306 >    }}
1307  
1308      /**
1309 <     * thenCombine result completes exceptionally if either source cancelled
1309 >     * thenAccept result completes exceptionally after exceptional
1310 >     * completion of source
1311       */
1312 <    public void testThenCombine_sourceCancelled1() {
1312 >    public void testThenAccept_exceptionalCompletion() {
1313          for (ExecutionMode m : ExecutionMode.values())
1314 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1315 <        for (Integer v1 : new Integer[] { 1, null }) {
1316 <
1314 >        for (boolean createIncomplete : new boolean[] { true, false })
1315 >    {
1316 >        final CFException ex = new CFException();
1317          final CompletableFuture<Integer> f = new CompletableFuture<>();
1318 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1319 <        final SubtractFunction r = new SubtractFunction();
1320 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1321 <
1322 <        assertTrue(f.cancel(mayInterruptIfRunning));
1323 <        checkIncomplete(h);
1310 <        g.complete(v1);
1311 <
1312 <        checkCompletedWithWrappedCancellationException(h);
1313 <        checkCancelled(f);
1314 <        assertEquals(0, r.invocationCount);
1315 <        checkCompletedNormally(g, v1);
1318 >        final NoopConsumer r = new NoopConsumer(m);
1319 >        if (!createIncomplete) f.completeExceptionally(ex);
1320 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1321 >        if (createIncomplete) {
1322 >            checkIncomplete(g);
1323 >            f.completeExceptionally(ex);
1324          }
1317    }
1318
1319    public void testThenCombine_sourceCancelled2() {
1320        for (ExecutionMode m : ExecutionMode.values())
1321        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1322        for (Integer v1 : new Integer[] { 1, null }) {
1323
1324        final CompletableFuture<Integer> f = new CompletableFuture<>();
1325        final CompletableFuture<Integer> g = new CompletableFuture<>();
1326        final SubtractFunction r = new SubtractFunction();
1327        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1328
1329        assertTrue(g.cancel(mayInterruptIfRunning));
1330        checkIncomplete(h);
1331        f.complete(v1);
1325  
1326 <        checkCompletedWithWrappedCancellationException(h);
1327 <        checkCancelled(g);
1328 <        assertEquals(0, r.invocationCount);
1329 <        checkCompletedNormally(f, v1);
1337 <        }
1338 <    }
1326 >        checkCompletedWithWrappedException(g, ex);
1327 >        checkCompletedExceptionally(f, ex);
1328 >        r.assertNotInvoked();
1329 >    }}
1330  
1331 <    public void testThenCombine_sourceCancelled3() {
1331 >    /**
1332 >     * thenAccept result completes exceptionally if source cancelled
1333 >     */
1334 >    public void testThenAccept_sourceCancelled() {
1335          for (ExecutionMode m : ExecutionMode.values())
1336 +        for (boolean createIncomplete : new boolean[] { true, false })
1337          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1338 <        for (Integer v1 : new Integer[] { 1, null }) {
1344 <
1338 >    {
1339          final CompletableFuture<Integer> f = new CompletableFuture<>();
1340 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1341 <        final SubtractFunction r = new SubtractFunction();
1342 <
1343 <        assertTrue(g.cancel(mayInterruptIfRunning));
1344 <        f.complete(v1);
1345 <        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);
1340 >        final NoopConsumer r = new NoopConsumer(m);
1341 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1342 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1343 >        if (createIncomplete) {
1344 >            checkIncomplete(g);
1345 >            assertTrue(f.cancel(mayInterruptIfRunning));
1346          }
1358    }
1347  
1348 <    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);
1372 <
1373 <        checkCompletedWithWrappedCancellationException(h);
1348 >        checkCompletedWithWrappedCancellationException(g);
1349          checkCancelled(f);
1350 <        assertEquals(0, r.invocationCount);
1351 <        checkCompletedNormally(g, v1);
1377 <        }
1378 <    }
1350 >        r.assertNotInvoked();
1351 >    }}
1352  
1353      /**
1354 <     * thenAcceptBoth result completes normally after normal
1382 <     * completion of sources
1354 >     * thenAccept result completes exceptionally if action does
1355       */
1356 <    public void testThenAcceptBoth_normalCompletion1() {
1356 >    public void testThenAccept_actionFailed() {
1357          for (ExecutionMode m : ExecutionMode.values())
1358 +        for (boolean createIncomplete : new boolean[] { true, false })
1359          for (Integer v1 : new Integer[] { 1, null })
1360 <        for (Integer v2 : new Integer[] { 2, null }) {
1388 <
1360 >    {
1361          final CompletableFuture<Integer> f = new CompletableFuture<>();
1362 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1363 <        final SubtractAction r = new SubtractAction();
1364 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1365 <
1366 <        f.complete(v1);
1367 <        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);
1362 >        final FailingConsumer r = new FailingConsumer(m);
1363 >        if (!createIncomplete) f.complete(v1);
1364 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1365 >        if (createIncomplete) {
1366 >            checkIncomplete(g);
1367 >            f.complete(v1);
1368          }
1404    }
1369  
1370 <    public void testThenAcceptBoth_normalCompletion2() {
1407 <        for (ExecutionMode m : ExecutionMode.values())
1408 <        for (Integer v1 : new Integer[] { 1, null })
1409 <        for (Integer v2 : new Integer[] { 2, null }) {
1410 <
1411 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1412 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1413 <        final SubtractAction r = new SubtractAction();
1414 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1415 <
1416 <        g.complete(v2);
1417 <        checkIncomplete(h);
1418 <        assertEquals(0, r.invocationCount);
1419 <        f.complete(v1);
1420 <
1421 <        checkCompletedNormally(h, null);
1422 <        assertEquals(subtract(v1, v2), r.value);
1370 >        checkCompletedWithWrappedCFException(g);
1371          checkCompletedNormally(f, v1);
1372 <        checkCompletedNormally(g, v2);
1425 <        }
1426 <    }
1372 >    }}
1373  
1374 <    public void testThenAcceptBoth_normalCompletion3() {
1374 >    /**
1375 >     * thenCombine result completes normally after normal completion
1376 >     * of sources
1377 >     */
1378 >    public void testThenCombine_normalCompletion() {
1379          for (ExecutionMode m : ExecutionMode.values())
1380 +        for (boolean createIncomplete : new boolean[] { true, false })
1381 +        for (boolean fFirst : new boolean[] { true, false })
1382          for (Integer v1 : new Integer[] { 1, null })
1383 <        for (Integer v2 : new Integer[] { 2, null }) {
1384 <
1383 >        for (Integer v2 : new Integer[] { 2, null })
1384 >    {
1385          final CompletableFuture<Integer> f = new CompletableFuture<>();
1386          final CompletableFuture<Integer> g = new CompletableFuture<>();
1387 <        final SubtractAction r = new SubtractAction();
1387 >        final SubtractFunction r = new SubtractFunction(m);
1388  
1389 <        g.complete(v2);
1390 <        f.complete(v1);
1391 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1392 <
1393 <        checkCompletedNormally(h, null);
1394 <        assertEquals(subtract(v1, v2), r.value);
1395 <        checkCompletedNormally(f, v1);
1396 <        checkCompletedNormally(g, v2);
1389 >        if (fFirst) f.complete(v1); else g.complete(v2);
1390 >        if (!createIncomplete)
1391 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1392 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1393 >        if (createIncomplete) {
1394 >            checkIncomplete(h);
1395 >            r.assertNotInvoked();
1396 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1397          }
1446    }
1398  
1399 <    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);
1399 >        checkCompletedNormally(h, subtract(v1, v2));
1400          checkCompletedNormally(f, v1);
1401          checkCompletedNormally(g, v2);
1402 <        }
1403 <    }
1402 >        r.assertValue(subtract(v1, v2));
1403 >    }}
1404  
1405      /**
1406 <     * thenAcceptBoth result completes exceptionally after exceptional
1406 >     * thenCombine result completes exceptionally after exceptional
1407       * completion of either source
1408       */
1409 <    public void testThenAcceptBoth_exceptionalCompletion1() {
1409 >    public void testThenCombine_exceptionalCompletion() {
1410          for (ExecutionMode m : ExecutionMode.values())
1411 <        for (Integer v1 : new Integer[] { 1, null }) {
1412 <
1411 >        for (boolean createIncomplete : new boolean[] { true, false })
1412 >        for (boolean fFirst : new boolean[] { true, false })
1413 >        for (Integer v1 : new Integer[] { 1, null })
1414 >    {
1415          final CompletableFuture<Integer> f = new CompletableFuture<>();
1416          final CompletableFuture<Integer> g = new CompletableFuture<>();
1478        final SubtractAction r = new SubtractAction();
1479        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1417          final CFException ex = new CFException();
1418 +        final SubtractFunction r = new SubtractFunction(m);
1419  
1420 <        f.completeExceptionally(ex);
1421 <        checkIncomplete(h);
1422 <        g.complete(v1);
1423 <
1424 <        checkCompletedWithWrappedCFException(h, ex);
1425 <        checkCompletedWithWrappedCFException(f, ex);
1426 <        assertEquals(0, r.invocationCount);
1489 <        checkCompletedNormally(g, v1);
1420 >        (fFirst ? f : g).complete(v1);
1421 >        if (!createIncomplete)
1422 >            (!fFirst ? f : g).completeExceptionally(ex);
1423 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1424 >        if (createIncomplete) {
1425 >            checkIncomplete(h);
1426 >            (!fFirst ? f : g).completeExceptionally(ex);
1427          }
1491    }
1492
1493    public void testThenAcceptBoth_exceptionalCompletion2() {
1494        for (ExecutionMode m : ExecutionMode.values())
1495        for (Integer v1 : new Integer[] { 1, null }) {
1496
1497        final CompletableFuture<Integer> f = new CompletableFuture<>();
1498        final CompletableFuture<Integer> g = new CompletableFuture<>();
1499        final SubtractAction r = new SubtractAction();
1500        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1501        final CFException ex = new CFException();
1502
1503        g.completeExceptionally(ex);
1504        checkIncomplete(h);
1505        f.complete(v1);
1428  
1429 <        checkCompletedWithWrappedCFException(h, ex);
1430 <        checkCompletedWithWrappedCFException(g, ex);
1431 <        assertEquals(0, r.invocationCount);
1432 <        checkCompletedNormally(f, v1);
1433 <        }
1512 <    }
1429 >        checkCompletedWithWrappedException(h, ex);
1430 >        r.assertNotInvoked();
1431 >        checkCompletedNormally(fFirst ? f : g, v1);
1432 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1433 >    }}
1434  
1435 <    public void testThenAcceptBoth_exceptionalCompletion3() {
1435 >    /**
1436 >     * thenCombine result completes exceptionally if either source cancelled
1437 >     */
1438 >    public void testThenCombine_sourceCancelled() {
1439          for (ExecutionMode m : ExecutionMode.values())
1440 <        for (Integer v1 : new Integer[] { 1, null }) {
1441 <
1440 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1441 >        for (boolean createIncomplete : new boolean[] { true, false })
1442 >        for (boolean fFirst : new boolean[] { true, false })
1443 >        for (Integer v1 : new Integer[] { 1, null })
1444 >    {
1445          final CompletableFuture<Integer> f = new CompletableFuture<>();
1446          final CompletableFuture<Integer> g = new CompletableFuture<>();
1447 <        final SubtractAction r = new SubtractAction();
1521 <        final CFException ex = new CFException();
1522 <
1523 <        g.completeExceptionally(ex);
1524 <        f.complete(v1);
1525 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1447 >        final SubtractFunction r = new SubtractFunction(m);
1448  
1449 <        checkCompletedWithWrappedCFException(h, ex);
1450 <        checkCompletedWithWrappedCFException(g, ex);
1451 <        assertEquals(0, r.invocationCount);
1452 <        checkCompletedNormally(f, v1);
1449 >        (fFirst ? f : g).complete(v1);
1450 >        if (!createIncomplete)
1451 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1452 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1453 >        if (createIncomplete) {
1454 >            checkIncomplete(h);
1455 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1456          }
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();
1457  
1458 <        f.completeExceptionally(ex);
1459 <        g.complete(v1);
1460 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1461 <
1462 <        checkCompletedWithWrappedCFException(h, ex);
1548 <        checkCompletedWithWrappedCFException(f, ex);
1549 <        assertEquals(0, r.invocationCount);
1550 <        checkCompletedNormally(g, v1);
1551 <        }
1552 <    }
1458 >        checkCompletedWithWrappedCancellationException(h);
1459 >        checkCancelled(!fFirst ? f : g);
1460 >        r.assertNotInvoked();
1461 >        checkCompletedNormally(fFirst ? f : g, v1);
1462 >    }}
1463  
1464      /**
1465 <     * thenAcceptBoth result completes exceptionally if action does
1465 >     * thenCombine result completes exceptionally if action does
1466       */
1467 <    public void testThenAcceptBoth_actionFailed1() {
1467 >    public void testThenCombine_actionFailed() {
1468          for (ExecutionMode m : ExecutionMode.values())
1469 +        for (boolean fFirst : new boolean[] { true, false })
1470          for (Integer v1 : new Integer[] { 1, null })
1471 <        for (Integer v2 : new Integer[] { 2, null }) {
1472 <
1471 >        for (Integer v2 : new Integer[] { 2, null })
1472 >    {
1473          final CompletableFuture<Integer> f = new CompletableFuture<>();
1474          final CompletableFuture<Integer> g = new CompletableFuture<>();
1475 <        final FailingBiConsumer r = new FailingBiConsumer();
1476 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1475 >        final FailingBiFunction r = new FailingBiFunction(m);
1476 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1477  
1478 <        f.complete(v1);
1479 <        checkIncomplete(h);
1480 <        g.complete(v2);
1478 >        if (fFirst) {
1479 >            f.complete(v1);
1480 >            g.complete(v2);
1481 >        } else {
1482 >            g.complete(v2);
1483 >            f.complete(v1);
1484 >        }
1485  
1486          checkCompletedWithWrappedCFException(h);
1487          checkCompletedNormally(f, v1);
1488          checkCompletedNormally(g, v2);
1489 <        }
1575 <    }
1489 >    }}
1490  
1491 <    public void testThenAcceptBoth_actionFailed2() {
1491 >    /**
1492 >     * thenAcceptBoth result completes normally after normal
1493 >     * completion of sources
1494 >     */
1495 >    public void testThenAcceptBoth_normalCompletion() {
1496          for (ExecutionMode m : ExecutionMode.values())
1497 +        for (boolean createIncomplete : new boolean[] { true, false })
1498 +        for (boolean fFirst : new boolean[] { true, false })
1499          for (Integer v1 : new Integer[] { 1, null })
1500 <        for (Integer v2 : new Integer[] { 2, null }) {
1501 <
1500 >        for (Integer v2 : new Integer[] { 2, null })
1501 >    {
1502          final CompletableFuture<Integer> f = new CompletableFuture<>();
1503          final CompletableFuture<Integer> g = new CompletableFuture<>();
1504 <        final FailingBiConsumer r = new FailingBiConsumer();
1585 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1504 >        final SubtractAction r = new SubtractAction(m);
1505  
1506 <        g.complete(v2);
1507 <        checkIncomplete(h);
1508 <        f.complete(v1);
1506 >        if (fFirst) f.complete(v1); else g.complete(v2);
1507 >        if (!createIncomplete)
1508 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1509 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1510 >        if (createIncomplete) {
1511 >            checkIncomplete(h);
1512 >            r.assertNotInvoked();
1513 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1514 >        }
1515  
1516 <        checkCompletedWithWrappedCFException(h);
1516 >        checkCompletedNormally(h, null);
1517 >        r.assertValue(subtract(v1, v2));
1518          checkCompletedNormally(f, v1);
1519          checkCompletedNormally(g, v2);
1520 <        }
1595 <    }
1520 >    }}
1521  
1522      /**
1523 <     * thenAcceptBoth result completes exceptionally if either source cancelled
1523 >     * thenAcceptBoth result completes exceptionally after exceptional
1524 >     * completion of either source
1525       */
1526 <    public void testThenAcceptBoth_sourceCancelled1() {
1526 >    public void testThenAcceptBoth_exceptionalCompletion() {
1527          for (ExecutionMode m : ExecutionMode.values())
1528 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1529 <        for (Integer v1 : new Integer[] { 1, null }) {
1530 <
1528 >        for (boolean createIncomplete : new boolean[] { true, false })
1529 >        for (boolean fFirst : new boolean[] { true, false })
1530 >        for (Integer v1 : new Integer[] { 1, null })
1531 >    {
1532          final CompletableFuture<Integer> f = new CompletableFuture<>();
1533          final CompletableFuture<Integer> g = new CompletableFuture<>();
1534 <        final SubtractAction r = new SubtractAction();
1535 <        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 }) {
1534 >        final CFException ex = new CFException();
1535 >        final SubtractAction r = new SubtractAction(m);
1536  
1537 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1538 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1539 <        final SubtractAction r = new SubtractAction();
1537 >        (fFirst ? f : g).complete(v1);
1538 >        if (!createIncomplete)
1539 >            (!fFirst ? f : g).completeExceptionally(ex);
1540          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1541 <
1542 <        assertTrue(g.cancel(mayInterruptIfRunning));
1543 <        checkIncomplete(h);
1633 <        f.complete(v1);
1634 <
1635 <        checkCompletedWithWrappedCancellationException(h);
1636 <        checkCancelled(g);
1637 <        assertEquals(0, r.invocationCount);
1638 <        checkCompletedNormally(f, v1);
1541 >        if (createIncomplete) {
1542 >            checkIncomplete(h);
1543 >            (!fFirst ? f : g).completeExceptionally(ex);
1544          }
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 }) {
1545  
1546 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1547 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1548 <        final SubtractAction r = new SubtractAction();
1549 <
1550 <        assertTrue(g.cancel(mayInterruptIfRunning));
1652 <        f.complete(v1);
1653 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1654 <
1655 <        checkCompletedWithWrappedCancellationException(h);
1656 <        checkCancelled(g);
1657 <        assertEquals(0, r.invocationCount);
1658 <        checkCompletedNormally(f, v1);
1659 <        }
1660 <    }
1546 >        checkCompletedWithWrappedException(h, ex);
1547 >        r.assertNotInvoked();
1548 >        checkCompletedNormally(fFirst ? f : g, v1);
1549 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1550 >    }}
1551  
1552 <    public void testThenAcceptBoth_sourceCancelled4() {
1552 >    /**
1553 >     * thenAcceptBoth result completes exceptionally if either source cancelled
1554 >     */
1555 >    public void testThenAcceptBoth_sourceCancelled() {
1556          for (ExecutionMode m : ExecutionMode.values())
1557          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
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<>();
1564 <        final SubtractAction r = new SubtractAction();
1564 >        final SubtractAction r = new SubtractAction(m);
1565  
1566 <        assertTrue(f.cancel(mayInterruptIfRunning));
1567 <        g.complete(v1);
1566 >        (fFirst ? f : g).complete(v1);
1567 >        if (!createIncomplete)
1568 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1569          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1570 +        if (createIncomplete) {
1571 +            checkIncomplete(h);
1572 +            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1573 +        }
1574  
1575          checkCompletedWithWrappedCancellationException(h);
1576 <        checkCancelled(f);
1577 <        assertEquals(0, r.invocationCount);
1578 <        checkCompletedNormally(g, v1);
1579 <        }
1680 <    }
1576 >        checkCancelled(!fFirst ? f : g);
1577 >        r.assertNotInvoked();
1578 >        checkCompletedNormally(fFirst ? f : g, v1);
1579 >    }}
1580  
1581      /**
1582 <     * runAfterBoth result completes normally after normal
1684 <     * completion of sources
1582 >     * thenAcceptBoth result completes exceptionally if action does
1583       */
1584 <    public void testRunAfterBoth_normalCompletion1() {
1584 >    public void testThenAcceptBoth_actionFailed() {
1585          for (ExecutionMode m : ExecutionMode.values())
1586 +        for (boolean fFirst : new boolean[] { true, false })
1587          for (Integer v1 : new Integer[] { 1, null })
1588 <        for (Integer v2 : new Integer[] { 2, null }) {
1589 <
1588 >        for (Integer v2 : new Integer[] { 2, null })
1589 >    {
1590          final CompletableFuture<Integer> f = new CompletableFuture<>();
1591          final CompletableFuture<Integer> g = new CompletableFuture<>();
1592 <        final Noop r = new Noop();
1593 <        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);
1592 >        final FailingBiConsumer r = new FailingBiConsumer(m);
1593 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1594  
1595 <        checkCompletedNormally(h, null);
1596 <        assertEquals(1, r.invocationCount);
1597 <        checkCompletedNormally(f, v1);
1598 <        checkCompletedNormally(g, v2);
1595 >        if (fFirst) {
1596 >            f.complete(v1);
1597 >            g.complete(v2);
1598 >        } else {
1599 >            g.complete(v2);
1600 >            f.complete(v1);
1601          }
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 }) {
1602  
1603 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1714 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1715 <        final Noop r = new Noop();
1716 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1717 <
1718 <        g.complete(v2);
1719 <        checkIncomplete(h);
1720 <        assertEquals(0, r.invocationCount);
1721 <        f.complete(v1);
1722 <
1723 <        checkCompletedNormally(h, null);
1724 <        assertEquals(1, r.invocationCount);
1603 >        checkCompletedWithWrappedCFException(h);
1604          checkCompletedNormally(f, v1);
1605          checkCompletedNormally(g, v2);
1606 <        }
1728 <    }
1606 >    }}
1607  
1608 <    public void testRunAfterBoth_normalCompletion3() {
1608 >    /**
1609 >     * runAfterBoth result completes normally after normal
1610 >     * completion of sources
1611 >     */
1612 >    public void testRunAfterBoth_normalCompletion() {
1613          for (ExecutionMode m : ExecutionMode.values())
1614 +        for (boolean createIncomplete : new boolean[] { true, false })
1615 +        for (boolean fFirst : new boolean[] { true, false })
1616          for (Integer v1 : new Integer[] { 1, null })
1617 <        for (Integer v2 : new Integer[] { 2, null }) {
1618 <
1617 >        for (Integer v2 : new Integer[] { 2, null })
1618 >    {
1619          final CompletableFuture<Integer> f = new CompletableFuture<>();
1620          final CompletableFuture<Integer> g = new CompletableFuture<>();
1621 <        final Noop r = new Noop();
1621 >        final Noop r = new Noop(m);
1622  
1623 <        g.complete(v2);
1624 <        f.complete(v1);
1623 >        if (fFirst) f.complete(v1); else g.complete(v2);
1624 >        if (!createIncomplete)
1625 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1626          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1627 <
1628 <        checkCompletedNormally(h, null);
1629 <        assertEquals(1, r.invocationCount);
1630 <        checkCompletedNormally(f, v1);
1746 <        checkCompletedNormally(g, v2);
1627 >        if (createIncomplete) {
1628 >            checkIncomplete(h);
1629 >            r.assertNotInvoked();
1630 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1631          }
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);
1632  
1633          checkCompletedNormally(h, null);
1634 <        assertEquals(1, r.invocationCount);
1634 >        r.assertInvoked();
1635          checkCompletedNormally(f, v1);
1636          checkCompletedNormally(g, v2);
1637 <        }
1768 <    }
1637 >    }}
1638  
1639      /**
1640       * runAfterBoth result completes exceptionally after exceptional
1641       * completion of either source
1642       */
1643 <    public void testRunAfterBoth_exceptionalCompletion1() {
1643 >    public void testRunAfterBoth_exceptionalCompletion() {
1644          for (ExecutionMode m : ExecutionMode.values())
1645 <        for (Integer v1 : new Integer[] { 1, null }) {
1646 <
1647 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1648 <        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() {
1837 <        for (ExecutionMode m : ExecutionMode.values())
1838 <        for (Integer v1 : new Integer[] { 1, null }) {
1839 <
1645 >        for (boolean createIncomplete : new boolean[] { true, false })
1646 >        for (boolean fFirst : new boolean[] { true, false })
1647 >        for (Integer v1 : new Integer[] { 1, null })
1648 >    {
1649          final CompletableFuture<Integer> f = new CompletableFuture<>();
1650          final CompletableFuture<Integer> g = new CompletableFuture<>();
1842        final Noop r = new Noop();
1651          final CFException ex = new CFException();
1652 +        final Noop r = new Noop(m);
1653  
1654 <        f.completeExceptionally(ex);
1655 <        g.complete(v1);
1654 >        (fFirst ? f : g).complete(v1);
1655 >        if (!createIncomplete)
1656 >            (!fFirst ? f : g).completeExceptionally(ex);
1657          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1658 <
1659 <        checkCompletedWithWrappedCFException(h, ex);
1660 <        checkCompletedWithWrappedCFException(f, ex);
1851 <        assertEquals(0, r.invocationCount);
1852 <        checkCompletedNormally(g, v1);
1658 >        if (createIncomplete) {
1659 >            checkIncomplete(h);
1660 >            (!fFirst ? f : g).completeExceptionally(ex);
1661          }
1854    }
1855
1856    /**
1857     * runAfterBoth result completes exceptionally if action does
1858     */
1859    public void testRunAfterBoth_actionFailed1() {
1860        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 }) {
1662  
1663 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1664 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1665 <        final FailingNoop r = new FailingNoop();
1666 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1667 <
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 <    }
1663 >        checkCompletedWithWrappedException(h, ex);
1664 >        r.assertNotInvoked();
1665 >        checkCompletedNormally(fFirst ? f : g, v1);
1666 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1667 >    }}
1668  
1669      /**
1670       * runAfterBoth result completes exceptionally if either source cancelled
1671       */
1672 <    public void testRunAfterBoth_sourceCancelled1() {
1903 <        for (ExecutionMode m : ExecutionMode.values())
1904 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1905 <        for (Integer v1 : new Integer[] { 1, null }) {
1906 <
1907 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1908 <        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() {
1672 >    public void testRunAfterBoth_sourceCancelled() {
1673          for (ExecutionMode m : ExecutionMode.values())
1674          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1675 <        for (Integer v1 : new Integer[] { 1, null }) {
1676 <
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 Noop r = new Noop();
1931 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1681 >        final Noop r = new Noop(m);
1682  
1933        assertTrue(g.cancel(mayInterruptIfRunning));
1934        checkIncomplete(h);
1935        f.complete(v1);
1683  
1684 <        checkCompletedWithWrappedCancellationException(h);
1685 <        checkCancelled(g);
1686 <        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 <
1949 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1950 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1951 <        final Noop r = new Noop();
1952 <
1953 <        assertTrue(g.cancel(mayInterruptIfRunning));
1954 <        f.complete(v1);
1684 >        (fFirst ? f : g).complete(v1);
1685 >        if (!createIncomplete)
1686 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1687          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1688 <
1689 <        checkCompletedWithWrappedCancellationException(h);
1690 <        checkCancelled(g);
1959 <        assertEquals(0, r.invocationCount);
1960 <        checkCompletedNormally(f, v1);
1688 >        if (createIncomplete) {
1689 >            checkIncomplete(h);
1690 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1691          }
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);
1692  
1693          checkCompletedWithWrappedCancellationException(h);
1694 <        checkCancelled(f);
1695 <        assertEquals(0, r.invocationCount);
1696 <        checkCompletedNormally(g, v1);
1697 <        }
1982 <    }
1694 >        checkCancelled(!fFirst ? f : g);
1695 >        r.assertNotInvoked();
1696 >        checkCompletedNormally(fFirst ? f : g, v1);
1697 >    }}
1698  
1699      /**
1700 <     * applyToEither result completes normally after normal completion
1986 <     * of either source
1700 >     * runAfterBoth result completes exceptionally if action does
1701       */
1702 <    public void testApplyToEither_normalCompletion1() {
1702 >    public void testRunAfterBoth_actionFailed() {
1703          for (ExecutionMode m : ExecutionMode.values())
1704 +        for (boolean fFirst : new boolean[] { true, false })
1705          for (Integer v1 : new Integer[] { 1, null })
1706 <        for (Integer v2 : new Integer[] { 2, null }) {
1707 <
1706 >        for (Integer v2 : new Integer[] { 2, null })
1707 >    {
1708          final CompletableFuture<Integer> f = new CompletableFuture<>();
1709          final CompletableFuture<Integer> g = new CompletableFuture<>();
1710 <        final IncFunction r = new IncFunction();
1711 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1710 >        final FailingRunnable r1 = new FailingRunnable(m);
1711 >        final FailingRunnable r2 = new FailingRunnable(m);
1712  
1713 <        f.complete(v1);
1714 <        checkCompletedNormally(h, inc(v1));
1715 <        g.complete(v2);
1716 <
1717 <        checkCompletedNormally(f, v1);
1718 <        checkCompletedNormally(g, v2);
1719 <        checkCompletedNormally(h, inc(v1));
1713 >        CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1714 >        if (fFirst) {
1715 >            f.complete(v1);
1716 >            g.complete(v2);
1717 >        } else {
1718 >            g.complete(v2);
1719 >            f.complete(v1);
1720          }
1721 <    }
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);
1721 >        CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1722  
1723 +        checkCompletedWithWrappedCFException(h1);
1724 +        checkCompletedWithWrappedCFException(h2);
1725          checkCompletedNormally(f, v1);
1726          checkCompletedNormally(g, v2);
1727 <        checkCompletedNormally(h, inc(v2));
1728 <        }
1729 <    }
1730 <    public void testApplyToEither_normalCompletion3() {
1727 >    }}
1728 >
1729 >    /**
1730 >     * applyToEither result completes normally after normal completion
1731 >     * of either source
1732 >     */
1733 >    public void testApplyToEither_normalCompletion() {
1734          for (ExecutionMode m : ExecutionMode.values())
1735          for (Integer v1 : new Integer[] { 1, null })
1736 <        for (Integer v2 : new Integer[] { 2, null }) {
1737 <
1736 >        for (Integer v2 : new Integer[] { 2, null })
1737 >    {
1738          final CompletableFuture<Integer> f = new CompletableFuture<>();
1739          final CompletableFuture<Integer> g = new CompletableFuture<>();
1740 <        final IncFunction r = new IncFunction();
1740 >        final IncFunction[] rs = new IncFunction[6];
1741 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1742  
1743 <        f.complete(v1);
1744 <        g.complete(v2);
1745 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1743 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1744 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1745 >        checkIncomplete(h0);
1746 >        checkIncomplete(h1);
1747 >        rs[0].assertNotInvoked();
1748 >        rs[1].assertNotInvoked();
1749 >        f.complete(v1);
1750 >        checkCompletedNormally(h0, inc(v1));
1751 >        checkCompletedNormally(h1, inc(v1));
1752 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1753 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1754 >        checkCompletedNormally(h2, inc(v1));
1755 >        checkCompletedNormally(h3, inc(v1));
1756 >        g.complete(v2);
1757 >
1758 >        // unspecified behavior - both source completions available
1759 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
1760 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
1761 >        rs[4].assertValue(h4.join());
1762 >        rs[5].assertValue(h5.join());
1763 >        assertTrue(Objects.equals(inc(v1), h4.join()) ||
1764 >                   Objects.equals(inc(v2), h4.join()));
1765 >        assertTrue(Objects.equals(inc(v1), h5.join()) ||
1766 >                   Objects.equals(inc(v2), h5.join()));
1767  
1768          checkCompletedNormally(f, v1);
1769          checkCompletedNormally(g, v2);
1770 <
1771 <        // unspecified behavior
1772 <        assertTrue(Objects.equals(h.join(), inc(v1)) ||
1773 <                   Objects.equals(h.join(), inc(v2)));
1774 <        assertEquals(1, r.invocationCount);
1775 <        }
2048 <    }
1770 >        checkCompletedNormally(h0, inc(v1));
1771 >        checkCompletedNormally(h1, inc(v1));
1772 >        checkCompletedNormally(h2, inc(v1));
1773 >        checkCompletedNormally(h3, inc(v1));
1774 >        for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1));
1775 >    }}
1776  
1777      /**
1778       * applyToEither result completes exceptionally after exceptional
1779       * completion of either source
1780       */
1781 <    public void testApplyToEither_exceptionalCompletion1() {
1781 >    public void testApplyToEither_exceptionalCompletion() {
1782          for (ExecutionMode m : ExecutionMode.values())
1783 <        for (Integer v1 : new Integer[] { 1, null }) {
1784 <
1783 >        for (Integer v1 : new Integer[] { 1, null })
1784 >    {
1785          final CompletableFuture<Integer> f = new CompletableFuture<>();
1786          final CompletableFuture<Integer> g = new CompletableFuture<>();
2060        final IncFunction r = new IncFunction();
2061        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1787          final CFException ex = new CFException();
1788 +        final IncFunction[] rs = new IncFunction[6];
1789 +        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1790  
1791 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1792 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1793 +        checkIncomplete(h0);
1794 +        checkIncomplete(h1);
1795 +        rs[0].assertNotInvoked();
1796 +        rs[1].assertNotInvoked();
1797          f.completeExceptionally(ex);
1798 <        checkCompletedWithWrappedCFException(h, ex);
1798 >        checkCompletedWithWrappedException(h0, ex);
1799 >        checkCompletedWithWrappedException(h1, ex);
1800 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1801 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1802 >        checkCompletedWithWrappedException(h2, ex);
1803 >        checkCompletedWithWrappedException(h3, ex);
1804          g.complete(v1);
1805  
1806 <        assertEquals(0, r.invocationCount);
1807 <        checkCompletedNormally(g, v1);
1808 <        checkCompletedWithWrappedCFException(f, ex);
1809 <        checkCompletedWithWrappedCFException(h, ex);
1806 >        // unspecified behavior - both source completions available
1807 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
1808 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
1809 >        try {
1810 >            assertEquals(inc(v1), h4.join());
1811 >            rs[4].assertValue(inc(v1));
1812 >        } catch (CompletionException ok) {
1813 >            checkCompletedWithWrappedException(h4, ex);
1814 >            rs[4].assertNotInvoked();
1815          }
1816 <    }
1816 >        try {
1817 >            assertEquals(inc(v1), h5.join());
1818 >            rs[5].assertValue(inc(v1));
1819 >        } catch (CompletionException ok) {
1820 >            checkCompletedWithWrappedException(h5, ex);
1821 >            rs[5].assertNotInvoked();
1822 >        }
1823 >
1824 >        checkCompletedExceptionally(f, ex);
1825 >        checkCompletedNormally(g, v1);
1826 >        checkCompletedWithWrappedException(h0, ex);
1827 >        checkCompletedWithWrappedException(h1, ex);
1828 >        checkCompletedWithWrappedException(h2, ex);
1829 >        checkCompletedWithWrappedException(h3, ex);
1830 >        checkCompletedWithWrappedException(h4, ex);
1831 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
1832 >    }}
1833  
1834      public void testApplyToEither_exceptionalCompletion2() {
1835          for (ExecutionMode m : ExecutionMode.values())
1836 <        for (Integer v1 : new Integer[] { 1, null }) {
1837 <
1836 >        for (boolean fFirst : new boolean[] { true, false })
1837 >        for (Integer v1 : new Integer[] { 1, null })
1838 >    {
1839          final CompletableFuture<Integer> f = new CompletableFuture<>();
1840          final CompletableFuture<Integer> g = new CompletableFuture<>();
2081        final IncFunction r = new IncFunction();
2082        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1841          final CFException ex = new CFException();
1842 +        final IncFunction[] rs = new IncFunction[6];
1843 +        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1844  
1845 <        g.completeExceptionally(ex);
1846 <        checkCompletedWithWrappedCFException(h, ex);
1847 <        f.complete(v1);
1848 <
1849 <        assertEquals(0, r.invocationCount);
1850 <        checkCompletedNormally(f, v1);
1851 <        checkCompletedWithWrappedCFException(g, ex);
1852 <        checkCompletedWithWrappedCFException(h, ex);
1845 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1846 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1847 >        if (fFirst) {
1848 >            f.complete(v1);
1849 >            g.completeExceptionally(ex);
1850 >        } else {
1851 >            g.completeExceptionally(ex);
1852 >            f.complete(v1);
1853          }
1854 <    }
1855 <
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);
1854 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1855 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1856  
1857 <        // unspecified behavior
2110 <        Integer v;
1857 >        // unspecified behavior - both source completions available
1858          try {
1859 <            assertEquals(inc(v1), h.join());
1860 <            assertEquals(1, r.invocationCount);
1859 >            assertEquals(inc(v1), h0.join());
1860 >            rs[0].assertValue(inc(v1));
1861          } catch (CompletionException ok) {
1862 <            checkCompletedWithWrappedCFException(h, ex);
1863 <            assertEquals(0, r.invocationCount);
1862 >            checkCompletedWithWrappedException(h0, ex);
1863 >            rs[0].assertNotInvoked();
1864          }
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;
1865          try {
1866 <            assertEquals(inc(v1), h.join());
1867 <            assertEquals(1, r.invocationCount);
1866 >            assertEquals(inc(v1), h1.join());
1867 >            rs[1].assertValue(inc(v1));
1868          } catch (CompletionException ok) {
1869 <            checkCompletedWithWrappedCFException(h, ex);
1870 <            assertEquals(0, r.invocationCount);
1869 >            checkCompletedWithWrappedException(h1, ex);
1870 >            rs[1].assertNotInvoked();
1871          }
1872 <
1873 <        checkCompletedWithWrappedCFException(f, ex);
1874 <        checkCompletedNormally(g, v1);
1872 >        try {
1873 >            assertEquals(inc(v1), h2.join());
1874 >            rs[2].assertValue(inc(v1));
1875 >        } catch (CompletionException ok) {
1876 >            checkCompletedWithWrappedException(h2, ex);
1877 >            rs[2].assertNotInvoked();
1878          }
1879 <    }
1880 <
1881 <    /**
1882 <     * applyToEither result completes exceptionally if action does
1883 <     */
1884 <    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);
1879 >        try {
1880 >            assertEquals(inc(v1), h3.join());
1881 >            rs[3].assertValue(inc(v1));
1882 >        } catch (CompletionException ok) {
1883 >            checkCompletedWithWrappedException(h3, ex);
1884 >            rs[3].assertNotInvoked();
1885          }
2171    }
2172
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 }) {
1886  
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);
1887          checkCompletedNormally(f, v1);
1888 <        checkCompletedNormally(g, v2);
1889 <        }
2189 <    }
1888 >        checkCompletedExceptionally(g, ex);
1889 >    }}
1890  
1891      /**
1892       * applyToEither result completes exceptionally if either source cancelled
1893       */
1894 <    public void testApplyToEither_sourceCancelled1() {
1894 >    public void testApplyToEither_sourceCancelled() {
1895          for (ExecutionMode m : ExecutionMode.values())
1896          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1897 <        for (Integer v1 : new Integer[] { 1, null }) {
1898 <
1897 >        for (Integer v1 : new Integer[] { 1, null })
1898 >    {
1899          final CompletableFuture<Integer> f = new CompletableFuture<>();
1900          final CompletableFuture<Integer> g = new CompletableFuture<>();
1901 <        final IncFunction r = new IncFunction();
1902 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1901 >        final IncFunction[] rs = new IncFunction[6];
1902 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1903  
1904 <        assertTrue(f.cancel(mayInterruptIfRunning));
1905 <        checkCompletedWithWrappedCancellationException(h);
1906 <        g.complete(v1);
1904 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1905 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1906 >        checkIncomplete(h0);
1907 >        checkIncomplete(h1);
1908 >        rs[0].assertNotInvoked();
1909 >        rs[1].assertNotInvoked();
1910 >        f.cancel(mayInterruptIfRunning);
1911 >        checkCompletedWithWrappedCancellationException(h0);
1912 >        checkCompletedWithWrappedCancellationException(h1);
1913 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1914 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1915 >        checkCompletedWithWrappedCancellationException(h2);
1916 >        checkCompletedWithWrappedCancellationException(h3);
1917 >        g.complete(v1);
1918 >
1919 >        // unspecified behavior - both source completions available
1920 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
1921 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
1922 >        try {
1923 >            assertEquals(inc(v1), h4.join());
1924 >            rs[4].assertValue(inc(v1));
1925 >        } catch (CompletionException ok) {
1926 >            checkCompletedWithWrappedCancellationException(h4);
1927 >            rs[4].assertNotInvoked();
1928 >        }
1929 >        try {
1930 >            assertEquals(inc(v1), h5.join());
1931 >            rs[5].assertValue(inc(v1));
1932 >        } catch (CompletionException ok) {
1933 >            checkCompletedWithWrappedCancellationException(h5);
1934 >            rs[5].assertNotInvoked();
1935 >        }
1936  
1937          checkCancelled(f);
2209        assertEquals(0, r.invocationCount);
1938          checkCompletedNormally(g, v1);
1939 <        checkCompletedWithWrappedCancellationException(h);
1940 <        }
1941 <    }
1939 >        checkCompletedWithWrappedCancellationException(h0);
1940 >        checkCompletedWithWrappedCancellationException(h1);
1941 >        checkCompletedWithWrappedCancellationException(h2);
1942 >        checkCompletedWithWrappedCancellationException(h3);
1943 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
1944 >    }}
1945  
1946      public void testApplyToEither_sourceCancelled2() {
1947          for (ExecutionMode m : ExecutionMode.values())
1948          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1949 <        for (Integer v1 : new Integer[] { 1, null }) {
1950 <
1949 >        for (boolean fFirst : new boolean[] { true, false })
1950 >        for (Integer v1 : new Integer[] { 1, null })
1951 >    {
1952          final CompletableFuture<Integer> f = new CompletableFuture<>();
1953          final CompletableFuture<Integer> g = new CompletableFuture<>();
1954 <        final IncFunction r = new IncFunction();
1955 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2224 <
2225 <        assertTrue(g.cancel(mayInterruptIfRunning));
2226 <        checkCompletedWithWrappedCancellationException(h);
2227 <        f.complete(v1);
1954 >        final IncFunction[] rs = new IncFunction[6];
1955 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1956  
1957 <        checkCancelled(g);
1958 <        assertEquals(0, r.invocationCount);
1959 <        checkCompletedNormally(f, v1);
1960 <        checkCompletedWithWrappedCancellationException(h);
1957 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1958 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1959 >        if (fFirst) {
1960 >            f.complete(v1);
1961 >            g.cancel(mayInterruptIfRunning);
1962 >        } else {
1963 >            g.cancel(mayInterruptIfRunning);
1964 >            f.complete(v1);
1965          }
1966 <    }
1967 <
2236 <    public void testApplyToEither_sourceCancelled3() {
2237 <        for (ExecutionMode m : ExecutionMode.values())
2238 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2239 <        for (Integer v1 : new Integer[] { 1, null }) {
2240 <
2241 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2242 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2243 <        final IncFunction r = new IncFunction();
2244 <
2245 <        assertTrue(g.cancel(mayInterruptIfRunning));
2246 <        f.complete(v1);
2247 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1966 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1967 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1968  
1969 <        // unspecified behavior
2250 <        Integer v;
1969 >        // unspecified behavior - both source completions available
1970          try {
1971 <            assertEquals(inc(v1), h.join());
1972 <            assertEquals(1, r.invocationCount);
1971 >            assertEquals(inc(v1), h0.join());
1972 >            rs[0].assertValue(inc(v1));
1973          } catch (CompletionException ok) {
1974 <            checkCompletedWithWrappedCancellationException(h);
1975 <            assertEquals(0, r.invocationCount);
1974 >            checkCompletedWithWrappedCancellationException(h0);
1975 >            rs[0].assertNotInvoked();
1976          }
1977 <
1978 <        checkCancelled(g);
1979 <        checkCompletedNormally(f, v1);
1977 >        try {
1978 >            assertEquals(inc(v1), h1.join());
1979 >            rs[1].assertValue(inc(v1));
1980 >        } catch (CompletionException ok) {
1981 >            checkCompletedWithWrappedCancellationException(h1);
1982 >            rs[1].assertNotInvoked();
1983          }
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;
1984          try {
1985 <            assertEquals(inc(v1), h.join());
1986 <            assertEquals(1, r.invocationCount);
1985 >            assertEquals(inc(v1), h2.join());
1986 >            rs[2].assertValue(inc(v1));
1987          } catch (CompletionException ok) {
1988 <            checkCompletedWithWrappedCancellationException(h);
1989 <            assertEquals(0, r.invocationCount);
1988 >            checkCompletedWithWrappedCancellationException(h2);
1989 >            rs[2].assertNotInvoked();
1990          }
1991 <
1992 <        checkCancelled(f);
1993 <        checkCompletedNormally(g, v1);
1991 >        try {
1992 >            assertEquals(inc(v1), h3.join());
1993 >            rs[3].assertValue(inc(v1));
1994 >        } catch (CompletionException ok) {
1995 >            checkCompletedWithWrappedCancellationException(h3);
1996 >            rs[3].assertNotInvoked();
1997          }
1998 <    }
1998 >
1999 >        checkCompletedNormally(f, v1);
2000 >        checkCancelled(g);
2001 >    }}
2002  
2003      /**
2004 <     * acceptEither result completes normally after normal completion
2294 <     * of either source
2004 >     * applyToEither result completes exceptionally if action does
2005       */
2006 <    public void testAcceptEither_normalCompletion1() {
2006 >    public void testApplyToEither_actionFailed() {
2007          for (ExecutionMode m : ExecutionMode.values())
2008          for (Integer v1 : new Integer[] { 1, null })
2009 <        for (Integer v2 : new Integer[] { 2, null }) {
2010 <
2009 >        for (Integer v2 : new Integer[] { 2, null })
2010 >    {
2011          final CompletableFuture<Integer> f = new CompletableFuture<>();
2012          final CompletableFuture<Integer> g = new CompletableFuture<>();
2013 <        final IncAction r = new IncAction();
2014 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2013 >        final FailingFunction[] rs = new FailingFunction[6];
2014 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
2015  
2016 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2017 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2018          f.complete(v1);
2019 <        checkCompletedNormally(h, null);
2020 <        assertEquals(inc(v1), r.value);
2021 <        g.complete(v2);
2022 <
2023 <        checkCompletedNormally(f, v1);
2024 <        checkCompletedNormally(g, v2);
2025 <        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);
2019 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2020 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2021 >        checkCompletedWithWrappedCFException(h0);
2022 >        checkCompletedWithWrappedCFException(h1);
2023 >        checkCompletedWithWrappedCFException(h2);
2024 >        checkCompletedWithWrappedCFException(h3);
2025 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2026  
2027          g.complete(v2);
2028 <        checkCompletedNormally(h, null);
2029 <        assertEquals(inc(v2), r.value);
2030 <        f.complete(v1);
2028 >
2029 >        // unspecified behavior - both source completions available
2030 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2031 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2032 >
2033 >        checkCompletedWithWrappedCFException(h4);
2034 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2035 >                   Objects.equals(v2, rs[4].value));
2036 >        checkCompletedWithWrappedCFException(h5);
2037 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2038 >                   Objects.equals(v2, rs[5].value));
2039  
2040          checkCompletedNormally(f, v1);
2041          checkCompletedNormally(g, v2);
2042 <        checkCompletedNormally(h, null);
2043 <        }
2044 <    }
2045 <    public void testAcceptEither_normalCompletion3() {
2042 >    }}
2043 >
2044 >    /**
2045 >     * acceptEither result completes normally after normal completion
2046 >     * of either source
2047 >     */
2048 >    public void testAcceptEither_normalCompletion() {
2049          for (ExecutionMode m : ExecutionMode.values())
2050          for (Integer v1 : new Integer[] { 1, null })
2051 <        for (Integer v2 : new Integer[] { 2, null }) {
2052 <
2051 >        for (Integer v2 : new Integer[] { 2, null })
2052 >    {
2053          final CompletableFuture<Integer> f = new CompletableFuture<>();
2054          final CompletableFuture<Integer> g = new CompletableFuture<>();
2055 <        final IncAction r = new IncAction();
2055 >        final NoopConsumer[] rs = new NoopConsumer[6];
2056 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2057  
2058 <        f.complete(v1);
2059 <        g.complete(v2);
2060 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2058 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2059 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2060 >        checkIncomplete(h0);
2061 >        checkIncomplete(h1);
2062 >        rs[0].assertNotInvoked();
2063 >        rs[1].assertNotInvoked();
2064 >        f.complete(v1);
2065 >        checkCompletedNormally(h0, null);
2066 >        checkCompletedNormally(h1, null);
2067 >        rs[0].assertValue(v1);
2068 >        rs[1].assertValue(v1);
2069 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2070 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2071 >        checkCompletedNormally(h2, null);
2072 >        checkCompletedNormally(h3, null);
2073 >        rs[2].assertValue(v1);
2074 >        rs[3].assertValue(v1);
2075 >        g.complete(v2);
2076 >
2077 >        // unspecified behavior - both source completions available
2078 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2079 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2080 >        checkCompletedNormally(h4, null);
2081 >        checkCompletedNormally(h5, null);
2082 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2083 >                   Objects.equals(v2, rs[4].value));
2084 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2085 >                   Objects.equals(v2, rs[5].value));
2086  
2350        checkCompletedNormally(h, null);
2087          checkCompletedNormally(f, v1);
2088          checkCompletedNormally(g, v2);
2089 <
2090 <        // unspecified behavior
2091 <        assertTrue(Objects.equals(r.value, inc(v1)) ||
2092 <                   Objects.equals(r.value, inc(v2)));
2093 <        }
2094 <    }
2089 >        checkCompletedNormally(h0, null);
2090 >        checkCompletedNormally(h1, null);
2091 >        checkCompletedNormally(h2, null);
2092 >        checkCompletedNormally(h3, null);
2093 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2094 >    }}
2095  
2096      /**
2097       * acceptEither result completes exceptionally after exceptional
2098       * completion of either source
2099       */
2100 <    public void testAcceptEither_exceptionalCompletion1() {
2100 >    public void testAcceptEither_exceptionalCompletion() {
2101          for (ExecutionMode m : ExecutionMode.values())
2102 <        for (Integer v1 : new Integer[] { 1, null }) {
2103 <
2102 >        for (Integer v1 : new Integer[] { 1, null })
2103 >    {
2104          final CompletableFuture<Integer> f = new CompletableFuture<>();
2105          final CompletableFuture<Integer> g = new CompletableFuture<>();
2370        final IncAction r = new IncAction();
2371        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2106          final CFException ex = new CFException();
2107 +        final NoopConsumer[] rs = new NoopConsumer[6];
2108 +        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2109  
2110 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2111 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2112 +        checkIncomplete(h0);
2113 +        checkIncomplete(h1);
2114 +        rs[0].assertNotInvoked();
2115 +        rs[1].assertNotInvoked();
2116          f.completeExceptionally(ex);
2117 <        checkCompletedWithWrappedCFException(h, ex);
2117 >        checkCompletedWithWrappedException(h0, ex);
2118 >        checkCompletedWithWrappedException(h1, ex);
2119 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2120 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2121 >        checkCompletedWithWrappedException(h2, ex);
2122 >        checkCompletedWithWrappedException(h3, ex);
2123 >
2124          g.complete(v1);
2125  
2126 <        assertEquals(0, r.invocationCount);
2127 <        checkCompletedNormally(g, v1);
2128 <        checkCompletedWithWrappedCFException(f, ex);
2129 <        checkCompletedWithWrappedCFException(h, ex);
2126 >        // unspecified behavior - both source completions available
2127 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2128 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2129 >        try {
2130 >            assertNull(h4.join());
2131 >            rs[4].assertValue(v1);
2132 >        } catch (CompletionException ok) {
2133 >            checkCompletedWithWrappedException(h4, ex);
2134 >            rs[4].assertNotInvoked();
2135 >        }
2136 >        try {
2137 >            assertNull(h5.join());
2138 >            rs[5].assertValue(v1);
2139 >        } catch (CompletionException ok) {
2140 >            checkCompletedWithWrappedException(h5, ex);
2141 >            rs[5].assertNotInvoked();
2142          }
2143 <    }
2143 >
2144 >        checkCompletedExceptionally(f, ex);
2145 >        checkCompletedNormally(g, v1);
2146 >        checkCompletedWithWrappedException(h0, ex);
2147 >        checkCompletedWithWrappedException(h1, ex);
2148 >        checkCompletedWithWrappedException(h2, ex);
2149 >        checkCompletedWithWrappedException(h3, ex);
2150 >        checkCompletedWithWrappedException(h4, ex);
2151 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2152 >    }}
2153  
2154      public void testAcceptEither_exceptionalCompletion2() {
2155          for (ExecutionMode m : ExecutionMode.values())
2156 <        for (Integer v1 : new Integer[] { 1, null }) {
2157 <
2156 >        for (boolean fFirst : new boolean[] { true, false })
2157 >        for (Integer v1 : new Integer[] { 1, null })
2158 >    {
2159          final CompletableFuture<Integer> f = new CompletableFuture<>();
2160          final CompletableFuture<Integer> g = new CompletableFuture<>();
2391        final IncAction r = new IncAction();
2392        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2161          final CFException ex = new CFException();
2162 +        final NoopConsumer[] rs = new NoopConsumer[6];
2163 +        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2164  
2165 <        g.completeExceptionally(ex);
2166 <        checkCompletedWithWrappedCFException(h, ex);
2167 <        f.complete(v1);
2168 <
2169 <        assertEquals(0, r.invocationCount);
2170 <        checkCompletedNormally(f, v1);
2171 <        checkCompletedWithWrappedCFException(g, ex);
2172 <        checkCompletedWithWrappedCFException(h, ex);
2165 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2166 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2167 >        if (fFirst) {
2168 >            f.complete(v1);
2169 >            g.completeExceptionally(ex);
2170 >        } else {
2171 >            g.completeExceptionally(ex);
2172 >            f.complete(v1);
2173          }
2174 <    }
2175 <
2406 <    public void testAcceptEither_exceptionalCompletion3() {
2407 <        for (ExecutionMode m : ExecutionMode.values())
2408 <        for (Integer v1 : new Integer[] { 1, null }) {
2174 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2175 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2176  
2177 <        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);
2418 <
2419 <        // unspecified behavior
2420 <        Integer v;
2177 >        // unspecified behavior - both source completions available
2178          try {
2179 <            assertNull(h.join());
2180 <            assertEquals(1, r.invocationCount);
2424 <            assertEquals(inc(v1), r.value);
2179 >            assertEquals(null, h0.join());
2180 >            rs[0].assertValue(v1);
2181          } catch (CompletionException ok) {
2182 <            checkCompletedWithWrappedCFException(h, ex);
2183 <            assertEquals(0, r.invocationCount);
2428 <        }
2429 <
2430 <        checkCompletedWithWrappedCFException(g, ex);
2431 <        checkCompletedNormally(f, v1);
2182 >            checkCompletedWithWrappedException(h0, ex);
2183 >            rs[0].assertNotInvoked();
2184          }
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;
2185          try {
2186 <            assertNull(h.join());
2187 <            assertEquals(1, r.invocationCount);
2453 <            assertEquals(inc(v1), r.value);
2186 >            assertEquals(null, h1.join());
2187 >            rs[1].assertValue(v1);
2188          } catch (CompletionException ok) {
2189 <            checkCompletedWithWrappedCFException(h, ex);
2190 <            assertEquals(0, r.invocationCount);
2189 >            checkCompletedWithWrappedException(h1, ex);
2190 >            rs[1].assertNotInvoked();
2191          }
2192 <
2193 <        checkCompletedWithWrappedCFException(f, ex);
2194 <        checkCompletedNormally(g, v1);
2192 >        try {
2193 >            assertEquals(null, h2.join());
2194 >            rs[2].assertValue(v1);
2195 >        } catch (CompletionException ok) {
2196 >            checkCompletedWithWrappedException(h2, ex);
2197 >            rs[2].assertNotInvoked();
2198          }
2199 <    }
2200 <
2201 <    /**
2202 <     * acceptEither result completes exceptionally if action does
2203 <     */
2204 <    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);
2199 >        try {
2200 >            assertEquals(null, h3.join());
2201 >            rs[3].assertValue(v1);
2202 >        } catch (CompletionException ok) {
2203 >            checkCompletedWithWrappedException(h3, ex);
2204 >            rs[3].assertNotInvoked();
2205          }
2483    }
2484
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);
2206  
2495        g.complete(v2);
2496        checkCompletedWithWrappedCFException(h);
2497        f.complete(v1);
2207          checkCompletedNormally(f, v1);
2208 <        checkCompletedNormally(g, v2);
2209 <        }
2501 <    }
2208 >        checkCompletedExceptionally(g, ex);
2209 >    }}
2210  
2211      /**
2212       * acceptEither result completes exceptionally if either source cancelled
2213       */
2214 <    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() {
2214 >    public void testAcceptEither_sourceCancelled() {
2215          for (ExecutionMode m : ExecutionMode.values())
2216          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2217 <        for (Integer v1 : new Integer[] { 1, null }) {
2218 <
2217 >        for (Integer v1 : new Integer[] { 1, null })
2218 >    {
2219          final CompletableFuture<Integer> f = new CompletableFuture<>();
2220          final CompletableFuture<Integer> g = new CompletableFuture<>();
2221 <        final IncAction r = new IncAction();
2222 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2536 <
2537 <        assertTrue(g.cancel(mayInterruptIfRunning));
2538 <        checkCompletedWithWrappedCancellationException(h);
2539 <        f.complete(v1);
2540 <
2541 <        checkCancelled(g);
2542 <        assertEquals(0, r.invocationCount);
2543 <        checkCompletedNormally(f, v1);
2544 <        checkCompletedWithWrappedCancellationException(h);
2545 <        }
2546 <    }
2221 >        final NoopConsumer[] rs = new NoopConsumer[6];
2222 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2223  
2224 <    public void testAcceptEither_sourceCancelled3() {
2225 <        for (ExecutionMode m : ExecutionMode.values())
2226 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2227 <        for (Integer v1 : new Integer[] { 1, null }) {
2228 <
2229 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2230 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2231 <        final IncAction r = new IncAction();
2224 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2225 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2226 >        checkIncomplete(h0);
2227 >        checkIncomplete(h1);
2228 >        rs[0].assertNotInvoked();
2229 >        rs[1].assertNotInvoked();
2230 >        f.cancel(mayInterruptIfRunning);
2231 >        checkCompletedWithWrappedCancellationException(h0);
2232 >        checkCompletedWithWrappedCancellationException(h1);
2233 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2234 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2235 >        checkCompletedWithWrappedCancellationException(h2);
2236 >        checkCompletedWithWrappedCancellationException(h3);
2237  
2238 <        assertTrue(g.cancel(mayInterruptIfRunning));
2558 <        f.complete(v1);
2559 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2238 >        g.complete(v1);
2239  
2240 <        // unspecified behavior
2241 <        Integer v;
2240 >        // unspecified behavior - both source completions available
2241 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2242 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2243          try {
2244 <            assertNull(h.join());
2245 <            assertEquals(1, r.invocationCount);
2566 <            assertEquals(inc(v1), r.value);
2244 >            assertNull(h4.join());
2245 >            rs[4].assertValue(v1);
2246          } catch (CompletionException ok) {
2247 <            checkCompletedWithWrappedCancellationException(h);
2248 <            assertEquals(0, r.invocationCount);
2247 >            checkCompletedWithWrappedCancellationException(h4);
2248 >            rs[4].assertNotInvoked();
2249          }
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;
2250          try {
2251 <            assertNull(h.join());
2252 <            assertEquals(1, r.invocationCount);
2595 <            assertEquals(inc(v1), r.value);
2251 >            assertNull(h5.join());
2252 >            rs[5].assertValue(v1);
2253          } catch (CompletionException ok) {
2254 <            checkCompletedWithWrappedCancellationException(h);
2255 <            assertEquals(0, r.invocationCount);
2254 >            checkCompletedWithWrappedCancellationException(h5);
2255 >            rs[5].assertNotInvoked();
2256          }
2257  
2258          checkCancelled(f);
2259          checkCompletedNormally(g, v1);
2260 <        }
2261 <    }
2260 >        checkCompletedWithWrappedCancellationException(h0);
2261 >        checkCompletedWithWrappedCancellationException(h1);
2262 >        checkCompletedWithWrappedCancellationException(h2);
2263 >        checkCompletedWithWrappedCancellationException(h3);
2264 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2265 >    }}
2266  
2267      /**
2268 <     * runAfterEither result completes normally after normal completion
2608 <     * of either source
2268 >     * acceptEither result completes exceptionally if action does
2269       */
2270 <    public void testRunAfterEither_normalCompletion1() {
2270 >    public void testAcceptEither_actionFailed() {
2271          for (ExecutionMode m : ExecutionMode.values())
2272          for (Integer v1 : new Integer[] { 1, null })
2273 <        for (Integer v2 : new Integer[] { 2, null }) {
2274 <
2273 >        for (Integer v2 : new Integer[] { 2, null })
2274 >    {
2275          final CompletableFuture<Integer> f = new CompletableFuture<>();
2276          final CompletableFuture<Integer> g = new CompletableFuture<>();
2277 <        final Noop r = new Noop();
2278 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2277 >        final FailingConsumer[] rs = new FailingConsumer[6];
2278 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
2279  
2280 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2281 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2282          f.complete(v1);
2283 <        checkCompletedNormally(h, null);
2284 <        assertEquals(1, r.invocationCount);
2283 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2284 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2285 >        checkCompletedWithWrappedCFException(h0);
2286 >        checkCompletedWithWrappedCFException(h1);
2287 >        checkCompletedWithWrappedCFException(h2);
2288 >        checkCompletedWithWrappedCFException(h3);
2289 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2290 >
2291          g.complete(v2);
2292  
2293 +        // unspecified behavior - both source completions available
2294 +        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2295 +        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2296 +
2297 +        checkCompletedWithWrappedCFException(h4);
2298 +        assertTrue(Objects.equals(v1, rs[4].value) ||
2299 +                   Objects.equals(v2, rs[4].value));
2300 +        checkCompletedWithWrappedCFException(h5);
2301 +        assertTrue(Objects.equals(v1, rs[5].value) ||
2302 +                   Objects.equals(v2, rs[5].value));
2303 +
2304          checkCompletedNormally(f, v1);
2305          checkCompletedNormally(g, v2);
2306 <        checkCompletedNormally(h, null);
2628 <        assertEquals(1, r.invocationCount);
2629 <        }
2630 <    }
2306 >    }}
2307  
2308 <    public void testRunAfterEither_normalCompletion2() {
2308 >    /**
2309 >     * runAfterEither result completes normally after normal completion
2310 >     * of either source
2311 >     */
2312 >    public void testRunAfterEither_normalCompletion() {
2313          for (ExecutionMode m : ExecutionMode.values())
2314          for (Integer v1 : new Integer[] { 1, null })
2315 <        for (Integer v2 : new Integer[] { 2, null }) {
2316 <
2315 >        for (Integer v2 : new Integer[] { 2, null })
2316 >    {
2317          final CompletableFuture<Integer> f = new CompletableFuture<>();
2318          final CompletableFuture<Integer> g = new CompletableFuture<>();
2319 <        final Noop r = new Noop();
2320 <        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 }) {
2319 >        final Noop[] rs = new Noop[6];
2320 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2321  
2322 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2323 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2324 <        final Noop r = new Noop();
2322 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2323 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2324 >        checkIncomplete(h0);
2325 >        checkIncomplete(h1);
2326 >        rs[0].assertNotInvoked();
2327 >        rs[1].assertNotInvoked();
2328 >        f.complete(v1);
2329 >        checkCompletedNormally(h0, null);
2330 >        checkCompletedNormally(h1, null);
2331 >        rs[0].assertInvoked();
2332 >        rs[1].assertInvoked();
2333 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2334 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2335 >        checkCompletedNormally(h2, null);
2336 >        checkCompletedNormally(h3, null);
2337 >        rs[2].assertInvoked();
2338 >        rs[3].assertInvoked();
2339  
2662        f.complete(v1);
2340          g.complete(v2);
2664        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2341  
2342 <        checkCompletedNormally(h, null);
2342 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2343 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2344 >
2345          checkCompletedNormally(f, v1);
2346          checkCompletedNormally(g, v2);
2347 <        assertEquals(1, r.invocationCount);
2348 <        }
2349 <    }
2347 >        checkCompletedNormally(h0, null);
2348 >        checkCompletedNormally(h1, null);
2349 >        checkCompletedNormally(h2, null);
2350 >        checkCompletedNormally(h3, null);
2351 >        checkCompletedNormally(h4, null);
2352 >        checkCompletedNormally(h5, null);
2353 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2354 >    }}
2355  
2356      /**
2357       * runAfterEither result completes exceptionally after exceptional
2358       * completion of either source
2359       */
2360 <    public void testRunAfterEither_exceptionalCompletion1() {
2360 >    public void testRunAfterEither_exceptionalCompletion() {
2361          for (ExecutionMode m : ExecutionMode.values())
2362 <        for (Integer v1 : new Integer[] { 1, null }) {
2363 <
2362 >        for (Integer v1 : new Integer[] { 1, null })
2363 >    {
2364          final CompletableFuture<Integer> f = new CompletableFuture<>();
2365          final CompletableFuture<Integer> g = new CompletableFuture<>();
2683        final Noop r = new Noop();
2684        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2366          final CFException ex = new CFException();
2367 +        final Noop[] rs = new Noop[6];
2368 +        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2369  
2370 +        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2371 +        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2372 +        checkIncomplete(h0);
2373 +        checkIncomplete(h1);
2374 +        rs[0].assertNotInvoked();
2375 +        rs[1].assertNotInvoked();
2376          f.completeExceptionally(ex);
2377 <        checkCompletedWithWrappedCFException(h, ex);
2377 >        checkCompletedWithWrappedException(h0, ex);
2378 >        checkCompletedWithWrappedException(h1, ex);
2379 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2380 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2381 >        checkCompletedWithWrappedException(h2, ex);
2382 >        checkCompletedWithWrappedException(h3, ex);
2383 >
2384          g.complete(v1);
2385  
2386 <        assertEquals(0, r.invocationCount);
2387 <        checkCompletedNormally(g, v1);
2388 <        checkCompletedWithWrappedCFException(f, ex);
2389 <        checkCompletedWithWrappedCFException(h, ex);
2386 >        // unspecified behavior - both source completions available
2387 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2388 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2389 >        try {
2390 >            assertNull(h4.join());
2391 >            rs[4].assertInvoked();
2392 >        } catch (CompletionException ok) {
2393 >            checkCompletedWithWrappedException(h4, ex);
2394 >            rs[4].assertNotInvoked();
2395          }
2396 <    }
2396 >        try {
2397 >            assertNull(h5.join());
2398 >            rs[5].assertInvoked();
2399 >        } catch (CompletionException ok) {
2400 >            checkCompletedWithWrappedException(h5, ex);
2401 >            rs[5].assertNotInvoked();
2402 >        }
2403 >
2404 >        checkCompletedExceptionally(f, ex);
2405 >        checkCompletedNormally(g, v1);
2406 >        checkCompletedWithWrappedException(h0, ex);
2407 >        checkCompletedWithWrappedException(h1, ex);
2408 >        checkCompletedWithWrappedException(h2, ex);
2409 >        checkCompletedWithWrappedException(h3, ex);
2410 >        checkCompletedWithWrappedException(h4, ex);
2411 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2412 >    }}
2413  
2414      public void testRunAfterEither_exceptionalCompletion2() {
2415          for (ExecutionMode m : ExecutionMode.values())
2416 <        for (Integer v1 : new Integer[] { 1, null }) {
2417 <
2416 >        for (boolean fFirst : new boolean[] { true, false })
2417 >        for (Integer v1 : new Integer[] { 1, null })
2418 >    {
2419          final CompletableFuture<Integer> f = new CompletableFuture<>();
2420          final CompletableFuture<Integer> g = new CompletableFuture<>();
2704        final Noop r = new Noop();
2705        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2421          final CFException ex = new CFException();
2422 +        final Noop[] rs = new Noop[6];
2423 +        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2424  
2425 <        g.completeExceptionally(ex);
2426 <        checkCompletedWithWrappedCFException(h, ex);
2427 <        f.complete(v1);
2428 <
2429 <        assertEquals(0, r.invocationCount);
2430 <        checkCompletedNormally(f, v1);
2431 <        checkCompletedWithWrappedCFException(g, ex);
2432 <        checkCompletedWithWrappedCFException(h, ex);
2425 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2426 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2427 >        if (fFirst) {
2428 >            f.complete(v1);
2429 >            g.completeExceptionally(ex);
2430 >        } else {
2431 >            g.completeExceptionally(ex);
2432 >            f.complete(v1);
2433          }
2434 <    }
2435 <
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);
2434 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2435 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2436  
2437 <        // unspecified behavior
2733 <        Integer v;
2437 >        // unspecified behavior - both source completions available
2438          try {
2439 <            assertNull(h.join());
2440 <            assertEquals(1, r.invocationCount);
2439 >            assertEquals(null, h0.join());
2440 >            rs[0].assertInvoked();
2441          } catch (CompletionException ok) {
2442 <            checkCompletedWithWrappedCFException(h, ex);
2443 <            assertEquals(0, r.invocationCount);
2442 >            checkCompletedWithWrappedException(h0, ex);
2443 >            rs[0].assertNotInvoked();
2444          }
2741
2742        checkCompletedWithWrappedCFException(g, ex);
2743        checkCompletedNormally(f, v1);
2744        }
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;
2445          try {
2446 <            assertNull(h.join());
2447 <            assertEquals(1, r.invocationCount);
2446 >            assertEquals(null, h1.join());
2447 >            rs[1].assertInvoked();
2448          } catch (CompletionException ok) {
2449 <            checkCompletedWithWrappedCFException(h, ex);
2450 <            assertEquals(0, r.invocationCount);
2449 >            checkCompletedWithWrappedException(h1, ex);
2450 >            rs[1].assertNotInvoked();
2451          }
2452 <
2453 <        checkCompletedWithWrappedCFException(f, ex);
2454 <        checkCompletedNormally(g, v1);
2452 >        try {
2453 >            assertEquals(null, h2.join());
2454 >            rs[2].assertInvoked();
2455 >        } catch (CompletionException ok) {
2456 >            checkCompletedWithWrappedException(h2, ex);
2457 >            rs[2].assertNotInvoked();
2458          }
2459 <    }
2460 <
2461 <    /**
2462 <     * runAfterEither result completes exceptionally if action does
2463 <     */
2464 <    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);
2459 >        try {
2460 >            assertEquals(null, h3.join());
2461 >            rs[3].assertInvoked();
2462 >        } catch (CompletionException ok) {
2463 >            checkCompletedWithWrappedException(h3, ex);
2464 >            rs[3].assertNotInvoked();
2465          }
2794    }
2466  
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);
2805
2806        g.complete(v2);
2807        checkCompletedWithWrappedCFException(h);
2808        f.complete(v1);
2467          checkCompletedNormally(f, v1);
2468 <        checkCompletedNormally(g, v2);
2469 <        }
2812 <    }
2468 >        checkCompletedExceptionally(g, ex);
2469 >    }}
2470  
2471      /**
2472       * runAfterEither result completes exceptionally if either source cancelled
2473       */
2474 <    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() {
2474 >    public void testRunAfterEither_sourceCancelled() {
2475          for (ExecutionMode m : ExecutionMode.values())
2476          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2477 <        for (Integer v1 : new Integer[] { 1, null }) {
2478 <
2477 >        for (Integer v1 : new Integer[] { 1, null })
2478 >    {
2479          final CompletableFuture<Integer> f = new CompletableFuture<>();
2480          final CompletableFuture<Integer> g = new CompletableFuture<>();
2481 <        final Noop r = new Noop();
2482 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2847 <
2848 <        assertTrue(g.cancel(mayInterruptIfRunning));
2849 <        checkCompletedWithWrappedCancellationException(h);
2850 <        f.complete(v1);
2481 >        final Noop[] rs = new Noop[6];
2482 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2483  
2484 <        checkCancelled(g);
2485 <        assertEquals(0, r.invocationCount);
2486 <        checkCompletedNormally(f, v1);
2487 <        checkCompletedWithWrappedCancellationException(h);
2488 <        }
2489 <    }
2484 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2485 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2486 >        checkIncomplete(h0);
2487 >        checkIncomplete(h1);
2488 >        rs[0].assertNotInvoked();
2489 >        rs[1].assertNotInvoked();
2490 >        f.cancel(mayInterruptIfRunning);
2491 >        checkCompletedWithWrappedCancellationException(h0);
2492 >        checkCompletedWithWrappedCancellationException(h1);
2493 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2494 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2495 >        checkCompletedWithWrappedCancellationException(h2);
2496 >        checkCompletedWithWrappedCancellationException(h3);
2497  
2498 <    public void testRunAfterEither_sourceCancelled3() {
2860 <        for (ExecutionMode m : ExecutionMode.values())
2861 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2862 <        for (Integer v1 : new Integer[] { 1, null }) {
2863 <
2864 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2865 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2866 <        final Noop r = new Noop();
2867 <
2868 <        assertTrue(g.cancel(mayInterruptIfRunning));
2869 <        f.complete(v1);
2870 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2498 >        g.complete(v1);
2499  
2500 <        // unspecified behavior
2501 <        Integer v;
2500 >        // unspecified behavior - both source completions available
2501 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2502 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2503          try {
2504 <            assertNull(h.join());
2505 <            assertEquals(1, r.invocationCount);
2504 >            assertNull(h4.join());
2505 >            rs[4].assertInvoked();
2506          } catch (CompletionException ok) {
2507 <            checkCompletedWithWrappedCancellationException(h);
2508 <            assertEquals(0, r.invocationCount);
2507 >            checkCompletedWithWrappedCancellationException(h4);
2508 >            rs[4].assertNotInvoked();
2509          }
2881
2882        checkCancelled(g);
2883        checkCompletedNormally(f, v1);
2884        }
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;
2510          try {
2511 <            assertNull(h.join());
2512 <            assertEquals(1, r.invocationCount);
2511 >            assertNull(h5.join());
2512 >            rs[5].assertInvoked();
2513          } catch (CompletionException ok) {
2514 <            checkCompletedWithWrappedCancellationException(h);
2515 <            assertEquals(0, r.invocationCount);
2514 >            checkCompletedWithWrappedCancellationException(h5);
2515 >            rs[5].assertNotInvoked();
2516          }
2517  
2518          checkCancelled(f);
2519          checkCompletedNormally(g, v1);
2520 <        }
2521 <    }
2520 >        checkCompletedWithWrappedCancellationException(h0);
2521 >        checkCompletedWithWrappedCancellationException(h1);
2522 >        checkCompletedWithWrappedCancellationException(h2);
2523 >        checkCompletedWithWrappedCancellationException(h3);
2524 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2525 >    }}
2526  
2527      /**
2528 <     * thenCompose result completes normally after normal completion of source
2528 >     * runAfterEither result completes exceptionally if action does
2529       */
2530 <    public void testThenCompose_normalCompletion1() {
2530 >    public void testRunAfterEither_actionFailed() {
2531          for (ExecutionMode m : ExecutionMode.values())
2532 <        for (Integer v1 : new Integer[] { 1, null }) {
2533 <
2532 >        for (Integer v1 : new Integer[] { 1, null })
2533 >        for (Integer v2 : new Integer[] { 2, null })
2534 >    {
2535          final CompletableFuture<Integer> f = new CompletableFuture<>();
2536 <        final CompletableFutureInc r = new CompletableFutureInc();
2537 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2536 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2537 >        final FailingRunnable[] rs = new FailingRunnable[6];
2538 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
2539 >
2540 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2541 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2542          f.complete(v1);
2543 <        checkCompletedNormally(g, inc(v1));
2543 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2544 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2545 >        checkCompletedWithWrappedCFException(h0);
2546 >        checkCompletedWithWrappedCFException(h1);
2547 >        checkCompletedWithWrappedCFException(h2);
2548 >        checkCompletedWithWrappedCFException(h3);
2549 >        for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2550 >        g.complete(v2);
2551 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2552 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2553 >        checkCompletedWithWrappedCFException(h4);
2554 >        checkCompletedWithWrappedCFException(h5);
2555 >
2556          checkCompletedNormally(f, v1);
2557 <        assertEquals(1, r.invocationCount);
2558 <        }
2559 <    }
2557 >        checkCompletedNormally(g, v2);
2558 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2559 >    }}
2560  
2561 <    public void testThenCompose_normalCompletion2() {
2561 >    /**
2562 >     * thenCompose result completes normally after normal completion of source
2563 >     */
2564 >    public void testThenCompose_normalCompletion() {
2565          for (ExecutionMode m : ExecutionMode.values())
2566 <        for (Integer v1 : new Integer[] { 1, null }) {
2567 <
2566 >        for (boolean createIncomplete : new boolean[] { true, false })
2567 >        for (Integer v1 : new Integer[] { 1, null })
2568 >    {
2569          final CompletableFuture<Integer> f = new CompletableFuture<>();
2570 <        final CompletableFutureInc r = new CompletableFutureInc();
2571 <        f.complete(v1);
2572 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2570 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2571 >        if (!createIncomplete) f.complete(v1);
2572 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2573 >        if (createIncomplete) f.complete(v1);
2574 >
2575          checkCompletedNormally(g, inc(v1));
2576          checkCompletedNormally(f, v1);
2577 <        assertEquals(1, r.invocationCount);
2578 <        }
2944 <    }
2577 >        r.assertValue(v1);
2578 >    }}
2579  
2580      /**
2581       * thenCompose result completes exceptionally after exceptional
2582       * completion of source
2583       */
2584 <    public void testThenCompose_exceptionalCompletion1() {
2585 <        for (ExecutionMode m : ExecutionMode.values()) {
2586 <
2584 >    public void testThenCompose_exceptionalCompletion() {
2585 >        for (ExecutionMode m : ExecutionMode.values())
2586 >        for (boolean createIncomplete : new boolean[] { true, false })
2587 >    {
2588          final CFException ex = new CFException();
2589 <        final CompletableFutureInc r = new CompletableFutureInc();
2589 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2590          final CompletableFuture<Integer> f = new CompletableFuture<>();
2591 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2592 <        f.completeExceptionally(ex);
2593 <        checkCompletedWithWrappedCFException(g, ex);
2959 <        checkCompletedWithWrappedCFException(f, ex);
2960 <        }
2961 <    }
2962 <
2963 <    public void testThenCompose_exceptionalCompletion2() {
2964 <        for (ExecutionMode m : ExecutionMode.values()) {
2591 >        if (!createIncomplete) f.completeExceptionally(ex);
2592 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2593 >        if (createIncomplete) f.completeExceptionally(ex);
2594  
2595 <        final CFException ex = new CFException();
2596 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2597 <        f.completeExceptionally(ex);
2598 <        final CompletableFutureInc r = new CompletableFutureInc();
2970 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2971 <        checkCompletedWithWrappedCFException(g, ex);
2972 <        checkCompletedWithWrappedCFException(f, ex);
2973 <        }
2974 <    }
2595 >        checkCompletedWithWrappedException(g, ex);
2596 >        checkCompletedExceptionally(f, ex);
2597 >        r.assertNotInvoked();
2598 >    }}
2599  
2600      /**
2601       * thenCompose result completes exceptionally if action does
2602       */
2603 <    public void testThenCompose_actionFailed1() {
2603 >    public void testThenCompose_actionFailed() {
2604          for (ExecutionMode m : ExecutionMode.values())
2605 <        for (Integer v1 : new Integer[] { 1, null }) {
2606 <
2605 >        for (boolean createIncomplete : new boolean[] { true, false })
2606 >        for (Integer v1 : new Integer[] { 1, null })
2607 >    {
2608          final CompletableFuture<Integer> f = new CompletableFuture<>();
2609          final FailingCompletableFutureFunction r
2610 <            = new FailingCompletableFutureFunction();
2611 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2612 <        f.complete(v1);
2613 <        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 }) {
2610 >            = new FailingCompletableFutureFunction(m);
2611 >        if (!createIncomplete) f.complete(v1);
2612 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2613 >        if (createIncomplete) f.complete(v1);
2614  
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);
2615          checkCompletedWithWrappedCFException(g);
2616          checkCompletedNormally(f, v1);
2617 <        }
3005 <    }
2617 >    }}
2618  
2619      /**
2620       * thenCompose result completes exceptionally if source cancelled
2621       */
2622 <    public void testThenCompose_sourceCancelled1() {
2622 >    public void testThenCompose_sourceCancelled() {
2623          for (ExecutionMode m : ExecutionMode.values())
2624 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
2625 <
2624 >        for (boolean createIncomplete : new boolean[] { true, false })
2625 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2626 >    {
2627          final CompletableFuture<Integer> f = new CompletableFuture<>();
2628 <        final CompletableFutureInc r = new CompletableFutureInc();
2629 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2630 <        assertTrue(f.cancel(mayInterruptIfRunning));
2631 <        checkCompletedWithWrappedCancellationException(g);
2632 <        checkCancelled(f);
2628 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2629 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2630 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2631 >        if (createIncomplete) {
2632 >            checkIncomplete(g);
2633 >            assertTrue(f.cancel(mayInterruptIfRunning));
2634          }
3021    }
2635  
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);
2636          checkCompletedWithWrappedCancellationException(g);
2637          checkCancelled(f);
2638 <        }
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 <    }
2638 >    }}
2639  
2640      // other static methods
2641  
# Line 3344 | Line 2654 | public class CompletableFutureTest exten
2654       */
2655      public void testAllOf_normal() throws Exception {
2656          for (int k = 1; k < 20; ++k) {
2657 <            CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2657 >            CompletableFuture<Integer>[] fs
2658 >                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2659              for (int i = 0; i < k; ++i)
2660                  fs[i] = new CompletableFuture<>();
2661              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
# Line 3358 | Line 2669 | public class CompletableFutureTest exten
2669          }
2670      }
2671  
2672 +    public void testAllOf_backwards() throws Exception {
2673 +        for (int k = 1; k < 20; ++k) {
2674 +            CompletableFuture<Integer>[] fs
2675 +                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2676 +            for (int i = 0; i < k; ++i)
2677 +                fs[i] = new CompletableFuture<>();
2678 +            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
2679 +            for (int i = k - 1; i >= 0; i--) {
2680 +                checkIncomplete(f);
2681 +                checkIncomplete(CompletableFuture.allOf(fs));
2682 +                fs[i].complete(one);
2683 +            }
2684 +            checkCompletedNormally(f, null);
2685 +            checkCompletedNormally(CompletableFuture.allOf(fs), null);
2686 +        }
2687 +    }
2688 +
2689      /**
2690       * anyOf(no component futures) returns an incomplete future
2691       */
# Line 3416 | Line 2744 | public class CompletableFutureTest exten
2744          Runnable[] throwingActions = {
2745              () -> CompletableFuture.supplyAsync(null),
2746              () -> CompletableFuture.supplyAsync(null, exec),
2747 <            () -> CompletableFuture.supplyAsync(supplyOne, null),
2747 >            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null),
2748  
2749              () -> CompletableFuture.runAsync(null),
2750              () -> CompletableFuture.runAsync(null, exec),
# Line 3489 | Line 2817 | public class CompletableFutureTest exten
2817  
2818              () -> f.thenCompose(null),
2819              () -> f.thenComposeAsync(null),
2820 <            () -> f.thenComposeAsync(new CompletableFutureInc(), null),
2820 >            () -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null),
2821              () -> f.thenComposeAsync(null, exec),
2822  
2823              () -> f.exceptionally(null),
# Line 3536 | Line 2864 | public class CompletableFutureTest exten
2864          final CompletableFuture<Integer> g = m.whenComplete
2865              (f,
2866               (Integer x, Throwable t) -> {
2867 +                m.checkExecutionMode();
2868                  threadAssertSame(x, v1);
2869                  threadAssertNull(t);
2870                  a.getAndIncrement();
# Line 3563 | Line 2892 | public class CompletableFutureTest exten
2892          final CompletableFuture<Integer> g = m.whenComplete
2893              (f,
2894               (Integer x, Throwable t) -> {
2895 +                m.checkExecutionMode();
2896                  threadAssertNull(x);
2897                  threadAssertSame(t, ex);
2898                  a.getAndIncrement();
2899              });
2900          if (createIncomplete) f.completeExceptionally(ex);
2901 <        checkCompletedWithWrappedCFException(f, ex);
2902 <        checkCompletedWithWrappedCFException(g, ex);
2901 >        checkCompletedExceptionally(f, ex);
2902 >        checkCompletedWithWrappedException(g, ex);
2903          assertEquals(1, a.get());
2904      }}
2905  
# Line 3588 | Line 2918 | public class CompletableFutureTest exten
2918          final CompletableFuture<Integer> g = m.whenComplete
2919              (f,
2920               (Integer x, Throwable t) -> {
2921 +                m.checkExecutionMode();
2922                  threadAssertNull(x);
2923                  threadAssertTrue(t instanceof CancellationException);
2924                  a.getAndIncrement();
2925              });
2926          if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2927  
3597        //try { g.join(); } catch (Throwable t) { throw new Error(t); }
2928          checkCompletedWithWrappedCancellationException(g);
2929          checkCancelled(f);
2930          assertEquals(1, a.get());
# Line 3607 | Line 2937 | public class CompletableFutureTest exten
2937      public void testWhenComplete_actionFailed() {
2938          for (boolean createIncomplete : new boolean[] { true, false })
2939          for (ExecutionMode m : ExecutionMode.values())
2940 <        for (Integer v1 : new Integer[] { 1, null }) {
2941 <
2940 >        for (Integer v1 : new Integer[] { 1, null })
2941 >    {
2942          final AtomicInteger a = new AtomicInteger(0);
2943          final CFException ex = new CFException();
2944          final CompletableFuture<Integer> f = new CompletableFuture<>();
# Line 3616 | Line 2946 | public class CompletableFutureTest exten
2946          final CompletableFuture<Integer> g = m.whenComplete
2947              (f,
2948               (Integer x, Throwable t) -> {
2949 +                m.checkExecutionMode();
2950                  threadAssertSame(x, v1);
2951                  threadAssertNull(t);
2952                  a.getAndIncrement();
# Line 3623 | Line 2954 | public class CompletableFutureTest exten
2954              });
2955          if (createIncomplete) f.complete(v1);
2956          checkCompletedNormally(f, v1);
2957 <        checkCompletedWithWrappedCFException(g, ex);
2957 >        checkCompletedWithWrappedException(g, ex);
2958          assertEquals(1, a.get());
2959 <        }
3629 <    }
2959 >    }}
2960  
2961      /**
2962       * If a whenComplete action throws an exception when triggered by
# Line 3636 | Line 2966 | public class CompletableFutureTest exten
2966      public void testWhenComplete_actionFailedSourceFailed() {
2967          for (boolean createIncomplete : new boolean[] { true, false })
2968          for (ExecutionMode m : ExecutionMode.values())
2969 <        for (Integer v1 : new Integer[] { 1, null }) {
2970 <
2969 >        for (Integer v1 : new Integer[] { 1, null })
2970 >    {
2971          final AtomicInteger a = new AtomicInteger(0);
2972          final CFException ex1 = new CFException();
2973          final CFException ex2 = new CFException();
# Line 3647 | Line 2977 | public class CompletableFutureTest exten
2977          final CompletableFuture<Integer> g = m.whenComplete
2978              (f,
2979               (Integer x, Throwable t) -> {
2980 +                m.checkExecutionMode();
2981                  threadAssertSame(t, ex1);
2982                  threadAssertNull(x);
2983                  a.getAndIncrement();
# Line 3654 | Line 2985 | public class CompletableFutureTest exten
2985              });
2986          if (createIncomplete) f.completeExceptionally(ex1);
2987  
2988 <        checkCompletedWithWrappedCFException(f, ex1);
2989 <        checkCompletedWithWrappedCFException(g, ex1);
2988 >        checkCompletedExceptionally(f, ex1);
2989 >        checkCompletedWithWrappedException(g, ex1);
2990          assertEquals(1, a.get());
2991 <        }
3661 <    }
2991 >    }}
2992  
2993   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines