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

Comparing jsr166/src/test/tck/CompletableFutureTest.java (file contents):
Revision 1.47 by jsr166, Mon Jun 2 18:21:34 2014 UTC vs.
Revision 1.72 by jsr166, Fri Jun 6 21:10:34 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 318 | Line 355 | public class CompletableFutureTest exten
355          checkCompletedNormally(f, "test");
356      }
357  
358 <    // Choose non-commutative actions for better coverage
358 >    abstract class CheckedAction {
359 >        int invocationCount = 0;
360 >        final ExecutionMode m;
361 >        CheckedAction(ExecutionMode m) { this.m = m; }
362 >        void invoked() {
363 >            m.checkExecutionMode();
364 >            assertEquals(0, invocationCount++);
365 >        }
366 >        void assertNotInvoked() { assertEquals(0, invocationCount); }
367 >        void assertInvoked() { assertEquals(1, invocationCount); }
368 >    }
369  
370 <    // A non-commutative function that handles and produces null values as well.
371 <    static Integer subtract(Integer x, Integer y) {
372 <        return (x == null && y == null) ? null :
373 <            ((x == null) ? 42 : x.intValue())
374 <            - ((y == null) ? 99 : y.intValue());
370 >    abstract class CheckedIntegerAction extends CheckedAction {
371 >        Integer value;
372 >        CheckedIntegerAction(ExecutionMode m) { super(m); }
373 >        void assertValue(Integer expected) {
374 >            assertInvoked();
375 >            assertEquals(expected, value);
376 >        }
377 >    }
378 >
379 >    class IntegerSupplier extends CheckedAction
380 >        implements Supplier<Integer>
381 >    {
382 >        final Integer value;
383 >        IntegerSupplier(ExecutionMode m, Integer value) {
384 >            super(m);
385 >            this.value = value;
386 >        }
387 >        public Integer get() {
388 >            invoked();
389 >            return value;
390 >        }
391      }
392  
393      // A function that handles and produces null values as well.
# Line 332 | Line 395 | public class CompletableFutureTest exten
395          return (x == null) ? null : x + 1;
396      }
397  
398 <    static final Supplier<Integer> supplyOne =
399 <        () -> Integer.valueOf(1);
400 <    static final Function<Integer, Integer> inc =
401 <        (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;
398 >    class NoopConsumer extends CheckedIntegerAction
399 >        implements Consumer<Integer>
400 >    {
401 >        NoopConsumer(ExecutionMode m) { super(m); }
402          public void accept(Integer x) {
403 <            invocationCount++;
404 <            value = inc(x);
403 >            invoked();
404 >            value = x;
405          }
406      }
407 <    static final class IncFunction implements Function<Integer,Integer> {
408 <        int invocationCount = 0;
409 <        Integer value;
407 >
408 >    class IncFunction extends CheckedIntegerAction
409 >        implements Function<Integer,Integer>
410 >    {
411 >        IncFunction(ExecutionMode m) { super(m); }
412          public Integer apply(Integer x) {
413 <            invocationCount++;
413 >            invoked();
414              return value = inc(x);
415          }
416      }
417 <    static final class SubtractAction implements BiConsumer<Integer, Integer> {
418 <        int invocationCount = 0;
419 <        Integer value;
420 <        // Check this action was invoked exactly once when result is computed.
417 >
418 >    // Choose non-commutative actions for better coverage
419 >    // A non-commutative function that handles and produces null values as well.
420 >    static Integer subtract(Integer x, Integer y) {
421 >        return (x == null && y == null) ? null :
422 >            ((x == null) ? 42 : x.intValue())
423 >            - ((y == null) ? 99 : y.intValue());
424 >    }
425 >
426 >    class SubtractAction extends CheckedIntegerAction
427 >        implements BiConsumer<Integer, Integer>
428 >    {
429 >        SubtractAction(ExecutionMode m) { super(m); }
430          public void accept(Integer x, Integer y) {
431 <            invocationCount++;
431 >            invoked();
432              value = subtract(x, y);
433          }
434      }
435 <    static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> {
436 <        int invocationCount = 0;
437 <        Integer value;
438 <        // Check this action was invoked exactly once when result is computed.
435 >
436 >    class SubtractFunction extends CheckedIntegerAction
437 >        implements BiFunction<Integer, Integer, Integer>
438 >    {
439 >        SubtractFunction(ExecutionMode m) { super(m); }
440          public Integer apply(Integer x, Integer y) {
441 <            invocationCount++;
441 >            invoked();
442              return value = subtract(x, y);
443          }
444      }
445 <    static final class Noop implements Runnable {
446 <        int invocationCount = 0;
445 >
446 >    class Noop extends CheckedAction implements Runnable {
447 >        Noop(ExecutionMode m) { super(m); }
448          public void run() {
449 <            invocationCount++;
449 >            invoked();
450          }
451      }
452  
453 <    static final class FailingSupplier implements Supplier<Integer> {
454 <        int invocationCount = 0;
453 >    class FailingSupplier extends CheckedAction
454 >        implements Supplier<Integer>
455 >    {
456 >        FailingSupplier(ExecutionMode m) { super(m); }
457          public Integer get() {
458 <            invocationCount++;
458 >            invoked();
459              throw new CFException();
460          }
461      }
462 <    static final class FailingConsumer implements Consumer<Integer> {
463 <        int invocationCount = 0;
462 >
463 >    class FailingConsumer extends CheckedIntegerAction
464 >        implements Consumer<Integer>
465 >    {
466 >        FailingConsumer(ExecutionMode m) { super(m); }
467          public void accept(Integer x) {
468 <            invocationCount++;
468 >            invoked();
469 >            value = x;
470              throw new CFException();
471          }
472      }
473 <    static final class FailingBiConsumer implements BiConsumer<Integer, Integer> {
474 <        int invocationCount = 0;
473 >
474 >    class FailingBiConsumer extends CheckedIntegerAction
475 >        implements BiConsumer<Integer, Integer>
476 >    {
477 >        FailingBiConsumer(ExecutionMode m) { super(m); }
478          public void accept(Integer x, Integer y) {
479 <            invocationCount++;
479 >            invoked();
480 >            value = subtract(x, y);
481              throw new CFException();
482          }
483      }
484 <    static final class FailingFunction implements Function<Integer, Integer> {
485 <        int invocationCount = 0;
484 >
485 >    class FailingFunction extends CheckedIntegerAction
486 >        implements Function<Integer, Integer>
487 >    {
488 >        FailingFunction(ExecutionMode m) { super(m); }
489          public Integer apply(Integer x) {
490 <            invocationCount++;
490 >            invoked();
491 >            value = x;
492              throw new CFException();
493          }
494      }
495 <    static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> {
496 <        int invocationCount = 0;
495 >
496 >    class FailingBiFunction extends CheckedIntegerAction
497 >        implements BiFunction<Integer, Integer, Integer>
498 >    {
499 >        FailingBiFunction(ExecutionMode m) { super(m); }
500          public Integer apply(Integer x, Integer y) {
501 <            invocationCount++;
501 >            invoked();
502 >            value = subtract(x, y);
503              throw new CFException();
504          }
505      }
506 <    static final class FailingNoop implements Runnable {
507 <        int invocationCount = 0;
506 >
507 >    class FailingRunnable extends CheckedAction implements Runnable {
508 >        FailingRunnable(ExecutionMode m) { super(m); }
509          public void run() {
510 <            invocationCount++;
510 >            invoked();
511              throw new CFException();
512          }
513      }
514  
515 <    static final class CompletableFutureInc
516 <        implements Function<Integer, CompletableFuture<Integer>> {
517 <        int invocationCount = 0;
515 >
516 >    class CompletableFutureInc extends CheckedIntegerAction
517 >        implements Function<Integer, CompletableFuture<Integer>>
518 >    {
519 >        CompletableFutureInc(ExecutionMode m) { super(m); }
520          public CompletableFuture<Integer> apply(Integer x) {
521 <            invocationCount++;
521 >            invoked();
522 >            value = x;
523              CompletableFuture<Integer> f = new CompletableFuture<>();
524              f.complete(inc(x));
525              return f;
526          }
527      }
528  
529 <    static final class FailingCompletableFutureFunction
530 <        implements Function<Integer, CompletableFuture<Integer>> {
531 <        int invocationCount = 0;
529 >    class FailingCompletableFutureFunction extends CheckedIntegerAction
530 >        implements Function<Integer, CompletableFuture<Integer>>
531 >    {
532 >        FailingCompletableFutureFunction(ExecutionMode m) { super(m); }
533          public CompletableFuture<Integer> apply(Integer x) {
534 <            invocationCount++;
534 >            invoked();
535 >            value = x;
536              throw new CFException();
537          }
538      }
539  
540      // Used for explicit executor tests
541      static final class ThreadExecutor implements Executor {
542 <        AtomicInteger count = new AtomicInteger(0);
542 >        final AtomicInteger count = new AtomicInteger(0);
543 >        static final ThreadGroup tg = new ThreadGroup("ThreadExecutor");
544 >        static boolean startedCurrentThread() {
545 >            return Thread.currentThread().getThreadGroup() == tg;
546 >        }
547  
548          public void execute(Runnable r) {
549              count.getAndIncrement();
550 <            new Thread(r).start();
550 >            new Thread(tg, r).start();
551          }
552      }
553  
554      /**
555       * Permits the testing of parallel code for the 3 different
556 <     * execution modes without repeating all the testing code.
556 >     * execution modes without copy/pasting all the test methods.
557       */
558      enum ExecutionMode {
559          DEFAULT {
560 +            public void checkExecutionMode() {
561 +                assertFalse(ThreadExecutor.startedCurrentThread());
562 +                assertNull(ForkJoinTask.getPool());
563 +            }
564 +            public CompletableFuture<Void> runAsync(Runnable a) {
565 +                throw new UnsupportedOperationException();
566 +            }
567 +            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
568 +                throw new UnsupportedOperationException();
569 +            }
570              public <T> CompletableFuture<Void> thenRun
571                  (CompletableFuture<T> f, Runnable a) {
572                  return f.thenRun(a);
# Line 521 | Line 630 | public class CompletableFutureTest exten
630              }
631          },
632  
633 <        DEFAULT_ASYNC {
633 >        ASYNC {
634 >            public void checkExecutionMode() {
635 >                assertSame(ForkJoinPool.commonPool(),
636 >                           ForkJoinTask.getPool());
637 >            }
638 >            public CompletableFuture<Void> runAsync(Runnable a) {
639 >                return CompletableFuture.runAsync(a);
640 >            }
641 >            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
642 >                return CompletableFuture.supplyAsync(a);
643 >            }
644              public <T> CompletableFuture<Void> thenRun
645                  (CompletableFuture<T> f, Runnable a) {
646                  return f.thenRunAsync(a);
# Line 586 | Line 705 | public class CompletableFutureTest exten
705          },
706  
707          EXECUTOR {
708 +            public void checkExecutionMode() {
709 +                assertTrue(ThreadExecutor.startedCurrentThread());
710 +            }
711 +            public CompletableFuture<Void> runAsync(Runnable a) {
712 +                return CompletableFuture.runAsync(a, new ThreadExecutor());
713 +            }
714 +            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
715 +                return CompletableFuture.supplyAsync(a, new ThreadExecutor());
716 +            }
717              public <T> CompletableFuture<Void> thenRun
718                  (CompletableFuture<T> f, Runnable a) {
719                  return f.thenRunAsync(a, new ThreadExecutor());
# Line 649 | Line 777 | public class CompletableFutureTest exten
777              }
778          };
779  
780 +        public abstract void checkExecutionMode();
781 +        public abstract CompletableFuture<Void> runAsync(Runnable a);
782 +        public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a);
783          public abstract <T> CompletableFuture<Void> thenRun
784              (CompletableFuture<T> f, Runnable a);
785          public abstract <T> CompletableFuture<Void> thenAccept
# Line 727 | Line 858 | public class CompletableFutureTest exten
858          if (!createIncomplete) f.completeExceptionally(ex);
859          final CompletableFuture<Integer> g = f.exceptionally
860              ((Throwable t) -> {
861 +                ExecutionMode.DEFAULT.checkExecutionMode();
862                  threadAssertSame(t, ex);
863                  a.getAndIncrement();
864                  return v1;
# Line 748 | Line 880 | public class CompletableFutureTest exten
880          if (!createIncomplete) f.completeExceptionally(ex1);
881          final CompletableFuture<Integer> g = f.exceptionally
882              ((Throwable t) -> {
883 +                ExecutionMode.DEFAULT.checkExecutionMode();
884                  threadAssertSame(t, ex1);
885                  a.getAndIncrement();
886                  throw ex2;
887              });
888          if (createIncomplete) f.completeExceptionally(ex1);
889  
890 <        checkCompletedWithWrappedCFException(g, ex2);
890 >        checkCompletedWithWrappedException(g, ex2);
891          assertEquals(1, a.get());
892      }}
893  
# Line 773 | Line 906 | public class CompletableFutureTest exten
906          final CompletableFuture<Integer> g = m.handle
907              (f,
908               (Integer x, Throwable t) -> {
909 +                m.checkExecutionMode();
910                  threadAssertSame(x, v1);
911                  threadAssertNull(t);
912                  a.getAndIncrement();
# Line 801 | Line 935 | public class CompletableFutureTest exten
935          final CompletableFuture<Integer> g = m.handle
936              (f,
937               (Integer x, Throwable t) -> {
938 +                m.checkExecutionMode();
939                  threadAssertNull(x);
940                  threadAssertSame(t, ex);
941                  a.getAndIncrement();
# Line 809 | Line 944 | public class CompletableFutureTest exten
944          if (createIncomplete) f.completeExceptionally(ex);
945  
946          checkCompletedNormally(g, v1);
947 <        checkCompletedWithWrappedCFException(f, ex);
947 >        checkCompletedExceptionally(f, ex);
948          assertEquals(1, a.get());
949      }}
950  
# Line 829 | Line 964 | public class CompletableFutureTest exten
964          final CompletableFuture<Integer> g = m.handle
965              (f,
966               (Integer x, Throwable t) -> {
967 +                m.checkExecutionMode();
968                  threadAssertNull(x);
969                  threadAssertTrue(t instanceof CancellationException);
970                  a.getAndIncrement();
# Line 856 | Line 992 | public class CompletableFutureTest exten
992          final CompletableFuture<Integer> g = m.handle
993              (f,
994               (Integer x, Throwable t) -> {
995 +                m.checkExecutionMode();
996                  threadAssertNull(x);
997                  threadAssertSame(ex1, t);
998                  a.getAndIncrement();
# Line 863 | Line 1000 | public class CompletableFutureTest exten
1000              });
1001          if (createIncomplete) f.completeExceptionally(ex1);
1002  
1003 <        checkCompletedWithWrappedCFException(g, ex2);
1004 <        checkCompletedWithWrappedCFException(f, ex1);
1003 >        checkCompletedWithWrappedException(g, ex2);
1004 >        checkCompletedExceptionally(f, ex1);
1005          assertEquals(1, a.get());
1006      }}
1007  
# Line 880 | Line 1017 | public class CompletableFutureTest exten
1017          final CompletableFuture<Integer> g = m.handle
1018              (f,
1019               (Integer x, Throwable t) -> {
1020 +                m.checkExecutionMode();
1021                  threadAssertSame(x, v1);
1022                  threadAssertNull(t);
1023                  a.getAndIncrement();
# Line 887 | Line 1025 | public class CompletableFutureTest exten
1025              });
1026          if (createIncomplete) f.complete(v1);
1027  
1028 <        checkCompletedWithWrappedCFException(g, ex);
1028 >        checkCompletedWithWrappedException(g, ex);
1029          checkCompletedNormally(f, v1);
1030          assertEquals(1, a.get());
1031      }}
# Line 895 | Line 1033 | public class CompletableFutureTest exten
1033      /**
1034       * runAsync completes after running Runnable
1035       */
1036 <    public void testRunAsync() {
1037 <        Noop r = new Noop();
1038 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
1039 <        assertNull(f.join());
1040 <        assertEquals(1, r.invocationCount);
1041 <        checkCompletedNormally(f, null);
1042 <    }
1043 <
1044 <    /**
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);
1036 >    public void testRunAsync_normalCompletion() {
1037 >        ExecutionMode[] executionModes = {
1038 >            ExecutionMode.ASYNC,
1039 >            ExecutionMode.EXECUTOR,
1040 >        };
1041 >        for (ExecutionMode m : executionModes)
1042 >    {
1043 >        final Noop r = new Noop(m);
1044 >        final CompletableFuture<Void> f = m.runAsync(r);
1045          assertNull(f.join());
914        assertEquals(1, r.invocationCount);
1046          checkCompletedNormally(f, null);
1047 <        assertEquals(1, exec.count.get());
1048 <    }
1047 >        r.assertInvoked();
1048 >    }}
1049  
1050      /**
1051       * failing runAsync completes exceptionally after running Runnable
1052       */
1053 <    public void testRunAsync3() {
1054 <        FailingNoop r = new FailingNoop();
1055 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
1053 >    public void testRunAsync_exceptionalCompletion() {
1054 >        ExecutionMode[] executionModes = {
1055 >            ExecutionMode.ASYNC,
1056 >            ExecutionMode.EXECUTOR,
1057 >        };
1058 >        for (ExecutionMode m : executionModes)
1059 >    {
1060 >        final FailingRunnable r = new FailingRunnable(m);
1061 >        final CompletableFuture<Void> f = m.runAsync(r);
1062          checkCompletedWithWrappedCFException(f);
1063 <        assertEquals(1, r.invocationCount);
1064 <    }
1063 >        r.assertInvoked();
1064 >    }}
1065  
1066      /**
1067       * supplyAsync completes with result of supplier
1068       */
1069 <    public void testSupplyAsync() {
1070 <        CompletableFuture<Integer> f;
1071 <        f = CompletableFuture.supplyAsync(supplyOne);
1072 <        assertEquals(f.join(), one);
1073 <        checkCompletedNormally(f, one);
1074 <    }
1075 <
1076 <    /**
1077 <     * supplyAsync with executor completes with result of supplier
1078 <     */
1079 <    public void testSupplyAsync2() {
1080 <        CompletableFuture<Integer> f;
1081 <        f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor());
1082 <        assertEquals(f.join(), one);
946 <        checkCompletedNormally(f, one);
947 <    }
1069 >    public void testSupplyAsync_normalCompletion() {
1070 >        ExecutionMode[] executionModes = {
1071 >            ExecutionMode.ASYNC,
1072 >            ExecutionMode.EXECUTOR,
1073 >        };
1074 >        for (ExecutionMode m : executionModes)
1075 >        for (Integer v1 : new Integer[] { 1, null })
1076 >    {
1077 >        final IntegerSupplier r = new IntegerSupplier(m, v1);
1078 >        final CompletableFuture<Integer> f = m.supplyAsync(r);
1079 >        assertSame(v1, f.join());
1080 >        checkCompletedNormally(f, v1);
1081 >        r.assertInvoked();
1082 >    }}
1083  
1084      /**
1085       * Failing supplyAsync completes exceptionally
1086       */
1087 <    public void testSupplyAsync3() {
1088 <        FailingSupplier r = new FailingSupplier();
1089 <        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r);
1087 >    public void testSupplyAsync_exceptionalCompletion() {
1088 >        ExecutionMode[] executionModes = {
1089 >            ExecutionMode.ASYNC,
1090 >            ExecutionMode.EXECUTOR,
1091 >        };
1092 >        for (ExecutionMode m : executionModes)
1093 >    {
1094 >        FailingSupplier r = new FailingSupplier(m);
1095 >        CompletableFuture<Integer> f = m.supplyAsync(r);
1096          checkCompletedWithWrappedCFException(f);
1097 <        assertEquals(1, r.invocationCount);
1098 <    }
1097 >        r.assertInvoked();
1098 >    }}
1099  
1100      // seq completion methods
1101  
1102      /**
1103       * thenRun result completes normally after normal completion of source
1104       */
1105 <    public void testThenRun() {
965 <        CompletableFuture<Integer> f;
966 <        CompletableFuture<Void> g;
967 <        Noop r;
968 <
969 <        f = new CompletableFuture<>();
970 <        g = f.thenRun(r = new Noop());
971 <        f.complete(null);
972 <        checkCompletedNormally(g, null);
973 <        assertEquals(1, r.invocationCount);
974 <
975 <        f = new CompletableFuture<>();
976 <        f.complete(null);
977 <        g = f.thenRun(r = new Noop());
978 <        checkCompletedNormally(g, null);
979 <        assertEquals(1, r.invocationCount);
980 <    }
981 <
982 <    /**
983 <     * thenRun result completes exceptionally after exceptional
984 <     * completion of source
985 <     */
986 <    public void testThenRun2() {
987 <        CompletableFuture<Integer> f;
988 <        CompletableFuture<Void> g;
989 <        Noop r;
990 <
991 <        f = new CompletableFuture<>();
992 <        g = f.thenRun(r = new Noop());
993 <        f.completeExceptionally(new CFException());
994 <        checkCompletedWithWrappedCFException(g);
995 <        assertEquals(0, r.invocationCount);
996 <
997 <        f = new CompletableFuture<>();
998 <        f.completeExceptionally(new CFException());
999 <        g = f.thenRun(r = new Noop());
1000 <        checkCompletedWithWrappedCFException(g);
1001 <        assertEquals(0, r.invocationCount);
1002 <    }
1003 <
1004 <    /**
1005 <     * thenRun result completes exceptionally if action does
1006 <     */
1007 <    public void testThenRun3() {
1008 <        CompletableFuture<Integer> f;
1009 <        CompletableFuture<Void> g;
1010 <        FailingNoop r;
1011 <
1012 <        f = new CompletableFuture<>();
1013 <        g = f.thenRun(r = new FailingNoop());
1014 <        f.complete(null);
1015 <        checkCompletedWithWrappedCFException(g);
1016 <
1017 <        f = new CompletableFuture<>();
1018 <        f.complete(null);
1019 <        g = f.thenRun(r = new FailingNoop());
1020 <        checkCompletedWithWrappedCFException(g);
1021 <    }
1022 <
1023 <    /**
1024 <     * thenRun result completes exceptionally if source cancelled
1025 <     */
1026 <    public void testThenRun4() {
1027 <        CompletableFuture<Integer> f;
1028 <        CompletableFuture<Void> g;
1029 <        Noop r;
1030 <
1031 <        f = new CompletableFuture<>();
1032 <        g = f.thenRun(r = new Noop());
1033 <        assertTrue(f.cancel(true));
1034 <        checkCompletedWithWrappedCancellationException(g);
1035 <
1036 <        f = new CompletableFuture<>();
1037 <        assertTrue(f.cancel(true));
1038 <        g = f.thenRun(r = new Noop());
1039 <        checkCompletedWithWrappedCancellationException(g);
1040 <    }
1041 <
1042 <    /**
1043 <     * thenApply result completes normally after normal completion of source
1044 <     */
1045 <    public void testThenApply() {
1046 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1047 <        CompletableFuture<Integer> g = f.thenApply(inc);
1048 <        f.complete(one);
1049 <        checkCompletedNormally(g, two);
1050 <    }
1051 <
1052 <    /**
1053 <     * thenApply result completes exceptionally after exceptional
1054 <     * completion of source
1055 <     */
1056 <    public void testThenApply2() {
1057 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1058 <        CompletableFuture<Integer> g = f.thenApply(inc);
1059 <        f.completeExceptionally(new CFException());
1060 <        checkCompletedWithWrappedCFException(g);
1061 <    }
1062 <
1063 <    /**
1064 <     * thenApply result completes exceptionally if action does
1065 <     */
1066 <    public void testThenApply3() {
1067 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1068 <        CompletableFuture<Integer> g = f.thenApply(new FailingFunction());
1069 <        f.complete(one);
1070 <        checkCompletedWithWrappedCFException(g);
1071 <    }
1072 <
1073 <    /**
1074 <     * thenApply result completes exceptionally if source cancelled
1075 <     */
1076 <    public void testThenApply4() {
1077 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1078 <        CompletableFuture<Integer> g = f.thenApply(inc);
1079 <        assertTrue(f.cancel(true));
1080 <        checkCompletedWithWrappedCancellationException(g);
1081 <    }
1082 <
1083 <    /**
1084 <     * thenAccept result completes normally after normal completion of source
1085 <     */
1086 <    public void testThenAccept() {
1087 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1088 <        IncAction r = new IncAction();
1089 <        CompletableFuture<Void> g = f.thenAccept(r);
1090 <        f.complete(one);
1091 <        checkCompletedNormally(g, null);
1092 <        assertEquals(r.value, (Integer) 2);
1093 <    }
1094 <
1095 <    /**
1096 <     * thenAccept result completes exceptionally after exceptional
1097 <     * completion of source
1098 <     */
1099 <    public void testThenAccept2() {
1100 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1101 <        IncAction r = new IncAction();
1102 <        CompletableFuture<Void> g = f.thenAccept(r);
1103 <        f.completeExceptionally(new CFException());
1104 <        checkCompletedWithWrappedCFException(g);
1105 <    }
1106 <
1107 <    /**
1108 <     * thenAccept result completes exceptionally if action does
1109 <     */
1110 <    public void testThenAccept3() {
1111 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1112 <        FailingConsumer r = new FailingConsumer();
1113 <        CompletableFuture<Void> g = f.thenAccept(r);
1114 <        f.complete(one);
1115 <        checkCompletedWithWrappedCFException(g);
1116 <        assertEquals(1, r.invocationCount);
1117 <    }
1118 <
1119 <    /**
1120 <     * thenAccept result completes exceptionally if source cancelled
1121 <     */
1122 <    public void testThenAccept4() {
1123 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1124 <        IncAction r = new IncAction();
1125 <        CompletableFuture<Void> g = f.thenAccept(r);
1126 <        assertTrue(f.cancel(true));
1127 <        checkCompletedWithWrappedCancellationException(g);
1128 <    }
1129 <
1130 <    /**
1131 <     * thenCombine result completes normally after normal completion
1132 <     * of sources
1133 <     */
1134 <    public void testThenCombine_normalCompletion1() {
1135 <        for (boolean createIncomplete : new boolean[] { true, false })
1136 <        for (boolean fFirst : new boolean[] { true, false })
1105 >    public void testThenRun_normalCompletion() {
1106          for (ExecutionMode m : ExecutionMode.values())
1107 +        for (boolean createIncomplete : new boolean[] { true, false })
1108          for (Integer v1 : new Integer[] { 1, null })
1139        for (Integer v2 : new Integer[] { 2, null })
1109      {
1110          final CompletableFuture<Integer> f = new CompletableFuture<>();
1111 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1112 <        final SubtractFunction r = new SubtractFunction();
1113 <        CompletableFuture<Integer> h = null;
1114 <        if (createIncomplete) h = m.thenCombine(f, g, r);
1115 <
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)
1111 >        final Noop r = new Noop(m);
1112 >        if (!createIncomplete) f.complete(v1);
1113 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1114 >        if (createIncomplete) {
1115 >            checkIncomplete(g);
1116              f.complete(v1);
1117 <        else
1156 <            g.complete(v2);
1157 <        if (!createIncomplete) h = m.thenCombine(f, g, r);
1117 >        }
1118  
1119 <        checkCompletedNormally(h, subtract(v1, v2));
1119 >        checkCompletedNormally(g, null);
1120          checkCompletedNormally(f, v1);
1121 <        checkCompletedNormally(g, v2);
1162 <        assertEquals(1, r.invocationCount);
1121 >        r.assertInvoked();
1122      }}
1123  
1124      /**
1125 <     * thenCombine result completes exceptionally after exceptional
1126 <     * completion of either source
1125 >     * thenRun result completes exceptionally after exceptional
1126 >     * completion of source
1127       */
1128 <    public void testThenCombine_exceptionalCompletion1() {
1128 >    public void testThenRun_exceptionalCompletion() {
1129          for (ExecutionMode m : ExecutionMode.values())
1130 <        for (Integer v1 : new Integer[] { 1, null })
1130 >        for (boolean createIncomplete : new boolean[] { true, false })
1131      {
1173        final CompletableFuture<Integer> f = new CompletableFuture<>();
1174        final CompletableFuture<Integer> g = new CompletableFuture<>();
1175        final SubtractFunction r = new SubtractFunction();
1176        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1132          final CFException ex = new CFException();
1178
1179        f.completeExceptionally(ex);
1180        checkIncomplete(h);
1181        g.complete(v1);
1182
1183        checkCompletedWithWrappedCFException(h, ex);
1184        checkCompletedWithWrappedCFException(f, ex);
1185        assertEquals(0, r.invocationCount);
1186        checkCompletedNormally(g, v1);
1187    }}
1188
1189    public void testThenCombine_exceptionalCompletion2() {
1190        for (ExecutionMode m : ExecutionMode.values())
1191        for (Integer v1 : new Integer[] { 1, null })
1192    {
1133          final CompletableFuture<Integer> f = new CompletableFuture<>();
1134 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1135 <        final SubtractFunction r = new SubtractFunction();
1136 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1137 <        final CFException ex = new CFException();
1138 <
1139 <        g.completeExceptionally(ex);
1140 <        checkIncomplete(h);
1201 <        f.complete(v1);
1134 >        final Noop r = new Noop(m);
1135 >        if (!createIncomplete) f.completeExceptionally(ex);
1136 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1137 >        if (createIncomplete) {
1138 >            checkIncomplete(g);
1139 >            f.completeExceptionally(ex);
1140 >        }
1141  
1142 <        checkCompletedWithWrappedCFException(h, ex);
1143 <        checkCompletedWithWrappedCFException(g, ex);
1144 <        assertEquals(0, r.invocationCount);
1206 <        checkCompletedNormally(f, v1);
1142 >        checkCompletedWithWrappedException(g, ex);
1143 >        checkCompletedExceptionally(f, ex);
1144 >        r.assertNotInvoked();
1145      }}
1146  
1147 <    public void testThenCombine_exceptionalCompletion3() {
1147 >    /**
1148 >     * thenRun result completes exceptionally if source cancelled
1149 >     */
1150 >    public void testThenRun_sourceCancelled() {
1151          for (ExecutionMode m : ExecutionMode.values())
1152 <        for (Integer v1 : new Integer[] { 1, null })
1152 >        for (boolean createIncomplete : new boolean[] { true, false })
1153 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1154      {
1155          final CompletableFuture<Integer> f = new CompletableFuture<>();
1156 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1157 <        final SubtractFunction r = new SubtractFunction();
1158 <        final CFException ex = new CFException();
1159 <
1160 <        g.completeExceptionally(ex);
1161 <        f.complete(v1);
1162 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1156 >        final Noop r = new Noop(m);
1157 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1158 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1159 >        if (createIncomplete) {
1160 >            checkIncomplete(g);
1161 >            assertTrue(f.cancel(mayInterruptIfRunning));
1162 >        }
1163  
1164 <        checkCompletedWithWrappedCFException(h, ex);
1165 <        checkCompletedWithWrappedCFException(g, ex);
1166 <        assertEquals(0, r.invocationCount);
1225 <        checkCompletedNormally(f, v1);
1164 >        checkCompletedWithWrappedCancellationException(g);
1165 >        checkCancelled(f);
1166 >        r.assertNotInvoked();
1167      }}
1168  
1169 <    public void testThenCombine_exceptionalCompletion4() {
1169 >    /**
1170 >     * thenRun result completes exceptionally if action does
1171 >     */
1172 >    public void testThenRun_actionFailed() {
1173          for (ExecutionMode m : ExecutionMode.values())
1174 +        for (boolean createIncomplete : new boolean[] { true, false })
1175          for (Integer v1 : new Integer[] { 1, null })
1176      {
1177          final CompletableFuture<Integer> f = new CompletableFuture<>();
1178 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1179 <        final SubtractFunction r = new SubtractFunction();
1180 <        final CFException ex = new CFException();
1181 <
1182 <        f.completeExceptionally(ex);
1183 <        g.complete(v1);
1184 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1178 >        final FailingRunnable r = new FailingRunnable(m);
1179 >        if (!createIncomplete) f.complete(v1);
1180 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1181 >        if (createIncomplete) {
1182 >            checkIncomplete(g);
1183 >            f.complete(v1);
1184 >        }
1185  
1186 <        checkCompletedWithWrappedCFException(h, ex);
1187 <        checkCompletedWithWrappedCFException(f, ex);
1243 <        assertEquals(0, r.invocationCount);
1244 <        checkCompletedNormally(g, v1);
1186 >        checkCompletedWithWrappedCFException(g);
1187 >        checkCompletedNormally(f, v1);
1188      }}
1189  
1190      /**
1191 <     * thenCombine result completes exceptionally if action does
1191 >     * thenApply result completes normally after normal completion of source
1192       */
1193 <    public void testThenCombine_actionFailed1() {
1193 >    public void testThenApply_normalCompletion() {
1194          for (ExecutionMode m : ExecutionMode.values())
1195 +        for (boolean createIncomplete : new boolean[] { true, false })
1196          for (Integer v1 : new Integer[] { 1, null })
1253        for (Integer v2 : new Integer[] { 2, null })
1197      {
1198          final CompletableFuture<Integer> f = new CompletableFuture<>();
1199 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1200 <        final FailingBiFunction r = new FailingBiFunction();
1201 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1202 <
1203 <        f.complete(v1);
1204 <        checkIncomplete(h);
1205 <        g.complete(v2);
1199 >        final IncFunction r = new IncFunction(m);
1200 >        if (!createIncomplete) f.complete(v1);
1201 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1202 >        if (createIncomplete) {
1203 >            checkIncomplete(g);
1204 >            f.complete(v1);
1205 >        }
1206  
1207 <        checkCompletedWithWrappedCFException(h);
1207 >        checkCompletedNormally(g, inc(v1));
1208          checkCompletedNormally(f, v1);
1209 <        checkCompletedNormally(g, v2);
1209 >        r.assertValue(inc(v1));
1210      }}
1211  
1212 <    public void testThenCombine_actionFailed2() {
1212 >    /**
1213 >     * thenApply result completes exceptionally after exceptional
1214 >     * completion of source
1215 >     */
1216 >    public void testThenApply_exceptionalCompletion() {
1217          for (ExecutionMode m : ExecutionMode.values())
1218 <        for (Integer v1 : new Integer[] { 1, null })
1272 <        for (Integer v2 : new Integer[] { 2, null })
1218 >        for (boolean createIncomplete : new boolean[] { true, false })
1219      {
1220 +        final CFException ex = new CFException();
1221          final CompletableFuture<Integer> f = new CompletableFuture<>();
1222 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1223 <        final FailingBiFunction r = new FailingBiFunction();
1224 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1225 <
1226 <        g.complete(v2);
1227 <        checkIncomplete(h);
1228 <        f.complete(v1);
1222 >        final IncFunction r = new IncFunction(m);
1223 >        if (!createIncomplete) f.completeExceptionally(ex);
1224 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1225 >        if (createIncomplete) {
1226 >            checkIncomplete(g);
1227 >            f.completeExceptionally(ex);
1228 >        }
1229  
1230 <        checkCompletedWithWrappedCFException(h);
1231 <        checkCompletedNormally(f, v1);
1232 <        checkCompletedNormally(g, v2);
1230 >        checkCompletedWithWrappedException(g, ex);
1231 >        checkCompletedExceptionally(f, ex);
1232 >        r.assertNotInvoked();
1233      }}
1234  
1235      /**
1236 <     * thenCombine result completes exceptionally if either source cancelled
1236 >     * thenApply result completes exceptionally if source cancelled
1237       */
1238 <    public void testThenCombine_sourceCancelled1() {
1238 >    public void testThenApply_sourceCancelled() {
1239          for (ExecutionMode m : ExecutionMode.values())
1240 +        for (boolean createIncomplete : new boolean[] { true, false })
1241          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1294        for (Integer v1 : new Integer[] { 1, null })
1242      {
1243          final CompletableFuture<Integer> f = new CompletableFuture<>();
1244 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1245 <        final SubtractFunction r = new SubtractFunction();
1246 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1247 <
1248 <        assertTrue(f.cancel(mayInterruptIfRunning));
1249 <        checkIncomplete(h);
1250 <        g.complete(v1);
1244 >        final IncFunction r = new IncFunction(m);
1245 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1246 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1247 >        if (createIncomplete) {
1248 >            checkIncomplete(g);
1249 >            assertTrue(f.cancel(mayInterruptIfRunning));
1250 >        }
1251  
1252 <        checkCompletedWithWrappedCancellationException(h);
1252 >        checkCompletedWithWrappedCancellationException(g);
1253          checkCancelled(f);
1254 <        assertEquals(0, r.invocationCount);
1308 <        checkCompletedNormally(g, v1);
1254 >        r.assertNotInvoked();
1255      }}
1256  
1257 <    public void testThenCombine_sourceCancelled2() {
1257 >    /**
1258 >     * thenApply result completes exceptionally if action does
1259 >     */
1260 >    public void testThenApply_actionFailed() {
1261          for (ExecutionMode m : ExecutionMode.values())
1262 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1262 >        for (boolean createIncomplete : new boolean[] { true, false })
1263          for (Integer v1 : new Integer[] { 1, null })
1264      {
1265          final CompletableFuture<Integer> f = new CompletableFuture<>();
1266 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1267 <        final SubtractFunction r = new SubtractFunction();
1268 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1269 <
1270 <        assertTrue(g.cancel(mayInterruptIfRunning));
1271 <        checkIncomplete(h);
1272 <        f.complete(v1);
1266 >        final FailingFunction r = new FailingFunction(m);
1267 >        if (!createIncomplete) f.complete(v1);
1268 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1269 >        if (createIncomplete) {
1270 >            checkIncomplete(g);
1271 >            f.complete(v1);
1272 >        }
1273  
1274 <        checkCompletedWithWrappedCancellationException(h);
1326 <        checkCancelled(g);
1327 <        assertEquals(0, r.invocationCount);
1274 >        checkCompletedWithWrappedCFException(g);
1275          checkCompletedNormally(f, v1);
1276      }}
1277  
1278 <    public void testThenCombine_sourceCancelled3() {
1278 >    /**
1279 >     * thenAccept result completes normally after normal completion of source
1280 >     */
1281 >    public void testThenAccept_normalCompletion() {
1282          for (ExecutionMode m : ExecutionMode.values())
1283 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1283 >        for (boolean createIncomplete : new boolean[] { true, false })
1284          for (Integer v1 : new Integer[] { 1, null })
1285      {
1286          final CompletableFuture<Integer> f = new CompletableFuture<>();
1287 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1288 <        final SubtractFunction r = new SubtractFunction();
1289 <
1290 <        assertTrue(g.cancel(mayInterruptIfRunning));
1291 <        f.complete(v1);
1292 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1287 >        final NoopConsumer r = new NoopConsumer(m);
1288 >        if (!createIncomplete) f.complete(v1);
1289 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1290 >        if (createIncomplete) {
1291 >            checkIncomplete(g);
1292 >            f.complete(v1);
1293 >        }
1294  
1295 <        checkCompletedWithWrappedCancellationException(h);
1296 <        checkCancelled(g);
1346 <        assertEquals(0, r.invocationCount);
1295 >        checkCompletedNormally(g, null);
1296 >        r.assertValue(v1);
1297          checkCompletedNormally(f, v1);
1298      }}
1299  
1350    public void testThenCombine_sourceCancelled4() {
1351        for (ExecutionMode m : ExecutionMode.values())
1352        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1353        for (Integer v1 : new Integer[] { 1, null })
1354    {
1355        final CompletableFuture<Integer> f = new CompletableFuture<>();
1356        final CompletableFuture<Integer> g = new CompletableFuture<>();
1357        final SubtractFunction r = new SubtractFunction();
1358
1359        assertTrue(f.cancel(mayInterruptIfRunning));
1360        g.complete(v1);
1361        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1362
1363        checkCompletedWithWrappedCancellationException(h);
1364        checkCancelled(f);
1365        assertEquals(0, r.invocationCount);
1366        checkCompletedNormally(g, v1);
1367    }}
1368
1300      /**
1301 <     * thenAcceptBoth result completes normally after normal
1302 <     * completion of sources
1301 >     * thenAccept result completes exceptionally after exceptional
1302 >     * completion of source
1303       */
1304 <    public void testThenAcceptBoth_normalCompletion1() {
1304 >    public void testThenAccept_exceptionalCompletion() {
1305          for (ExecutionMode m : ExecutionMode.values())
1306 <        for (Integer v1 : new Integer[] { 1, null })
1376 <        for (Integer v2 : new Integer[] { 2, null })
1306 >        for (boolean createIncomplete : new boolean[] { true, false })
1307      {
1308 +        final CFException ex = new CFException();
1309          final CompletableFuture<Integer> f = new CompletableFuture<>();
1310 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1311 <        final SubtractAction r = new SubtractAction();
1312 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1313 <
1314 <        f.complete(v1);
1315 <        checkIncomplete(h);
1316 <        assertEquals(0, r.invocationCount);
1386 <        g.complete(v2);
1310 >        final NoopConsumer r = new NoopConsumer(m);
1311 >        if (!createIncomplete) f.completeExceptionally(ex);
1312 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1313 >        if (createIncomplete) {
1314 >            checkIncomplete(g);
1315 >            f.completeExceptionally(ex);
1316 >        }
1317  
1318 <        checkCompletedNormally(h, null);
1319 <        assertEquals(subtract(v1, v2), r.value);
1320 <        checkCompletedNormally(f, v1);
1391 <        checkCompletedNormally(g, v2);
1318 >        checkCompletedWithWrappedException(g, ex);
1319 >        checkCompletedExceptionally(f, ex);
1320 >        r.assertNotInvoked();
1321      }}
1322  
1323 <    public void testThenAcceptBoth_normalCompletion2() {
1323 >    /**
1324 >     * thenAccept result completes exceptionally if source cancelled
1325 >     */
1326 >    public void testThenAccept_sourceCancelled() {
1327          for (ExecutionMode m : ExecutionMode.values())
1328 <        for (Integer v1 : new Integer[] { 1, null })
1329 <        for (Integer v2 : new Integer[] { 2, null })
1328 >        for (boolean createIncomplete : new boolean[] { true, false })
1329 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1330      {
1331          final CompletableFuture<Integer> f = new CompletableFuture<>();
1332 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1333 <        final SubtractAction r = new SubtractAction();
1334 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1335 <
1336 <        g.complete(v2);
1337 <        checkIncomplete(h);
1338 <        assertEquals(0, r.invocationCount);
1407 <        f.complete(v1);
1332 >        final NoopConsumer r = new NoopConsumer(m);
1333 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1334 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1335 >        if (createIncomplete) {
1336 >            checkIncomplete(g);
1337 >            assertTrue(f.cancel(mayInterruptIfRunning));
1338 >        }
1339  
1340 <        checkCompletedNormally(h, null);
1341 <        assertEquals(subtract(v1, v2), r.value);
1342 <        checkCompletedNormally(f, v1);
1412 <        checkCompletedNormally(g, v2);
1340 >        checkCompletedWithWrappedCancellationException(g);
1341 >        checkCancelled(f);
1342 >        r.assertNotInvoked();
1343      }}
1344  
1345 <    public void testThenAcceptBoth_normalCompletion3() {
1345 >    /**
1346 >     * thenAccept result completes exceptionally if action does
1347 >     */
1348 >    public void testThenAccept_actionFailed() {
1349          for (ExecutionMode m : ExecutionMode.values())
1350 +        for (boolean createIncomplete : new boolean[] { true, false })
1351          for (Integer v1 : new Integer[] { 1, null })
1418        for (Integer v2 : new Integer[] { 2, null })
1352      {
1353          final CompletableFuture<Integer> f = new CompletableFuture<>();
1354 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1355 <        final SubtractAction r = new SubtractAction();
1356 <
1357 <        g.complete(v2);
1358 <        f.complete(v1);
1359 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1354 >        final FailingConsumer r = new FailingConsumer(m);
1355 >        if (!createIncomplete) f.complete(v1);
1356 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1357 >        if (createIncomplete) {
1358 >            checkIncomplete(g);
1359 >            f.complete(v1);
1360 >        }
1361  
1362 <        checkCompletedNormally(h, null);
1429 <        assertEquals(subtract(v1, v2), r.value);
1362 >        checkCompletedWithWrappedCFException(g);
1363          checkCompletedNormally(f, v1);
1431        checkCompletedNormally(g, v2);
1364      }}
1365  
1366 <    public void testThenAcceptBoth_normalCompletion4() {
1366 >    /**
1367 >     * thenCombine result completes normally after normal completion
1368 >     * of sources
1369 >     */
1370 >    public void testThenCombine_normalCompletion() {
1371          for (ExecutionMode m : ExecutionMode.values())
1372 +        for (boolean createIncomplete : new boolean[] { true, false })
1373 +        for (boolean fFirst : new boolean[] { true, false })
1374          for (Integer v1 : new Integer[] { 1, null })
1375          for (Integer v2 : new Integer[] { 2, null })
1376      {
1377          final CompletableFuture<Integer> f = new CompletableFuture<>();
1378          final CompletableFuture<Integer> g = new CompletableFuture<>();
1379 <        final SubtractAction r = new SubtractAction();
1379 >        final SubtractFunction r = new SubtractFunction(m);
1380  
1381 <        f.complete(v1);
1382 <        g.complete(v2);
1383 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1381 >        if (fFirst) f.complete(v1); else g.complete(v2);
1382 >        if (!createIncomplete)
1383 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1384 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1385 >        if (createIncomplete) {
1386 >            checkIncomplete(h);
1387 >            r.assertNotInvoked();
1388 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1389 >        }
1390  
1391 <        checkCompletedNormally(h, null);
1448 <        assertEquals(subtract(v1, v2), r.value);
1391 >        checkCompletedNormally(h, subtract(v1, v2));
1392          checkCompletedNormally(f, v1);
1393          checkCompletedNormally(g, v2);
1394 +        r.assertValue(subtract(v1, v2));
1395      }}
1396  
1397      /**
1398 <     * thenAcceptBoth result completes exceptionally after exceptional
1398 >     * thenCombine result completes exceptionally after exceptional
1399       * completion of either source
1400       */
1401 <    public void testThenAcceptBoth_exceptionalCompletion1() {
1458 <        for (ExecutionMode m : ExecutionMode.values())
1459 <        for (Integer v1 : new Integer[] { 1, null })
1460 <    {
1461 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1462 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1463 <        final SubtractAction r = new SubtractAction();
1464 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1465 <        final CFException ex = new CFException();
1466 <
1467 <        f.completeExceptionally(ex);
1468 <        checkIncomplete(h);
1469 <        g.complete(v1);
1470 <
1471 <        checkCompletedWithWrappedCFException(h, ex);
1472 <        checkCompletedWithWrappedCFException(f, ex);
1473 <        assertEquals(0, r.invocationCount);
1474 <        checkCompletedNormally(g, v1);
1475 <    }}
1476 <
1477 <    public void testThenAcceptBoth_exceptionalCompletion2() {
1478 <        for (ExecutionMode m : ExecutionMode.values())
1479 <        for (Integer v1 : new Integer[] { 1, null })
1480 <    {
1481 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1482 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1483 <        final SubtractAction r = new SubtractAction();
1484 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1485 <        final CFException ex = new CFException();
1486 <
1487 <        g.completeExceptionally(ex);
1488 <        checkIncomplete(h);
1489 <        f.complete(v1);
1490 <
1491 <        checkCompletedWithWrappedCFException(h, ex);
1492 <        checkCompletedWithWrappedCFException(g, ex);
1493 <        assertEquals(0, r.invocationCount);
1494 <        checkCompletedNormally(f, v1);
1495 <    }}
1496 <
1497 <    public void testThenAcceptBoth_exceptionalCompletion3() {
1401 >    public void testThenCombine_exceptionalCompletion() {
1402          for (ExecutionMode m : ExecutionMode.values())
1403 +        for (boolean createIncomplete : new boolean[] { true, false })
1404 +        for (boolean fFirst : new boolean[] { true, false })
1405          for (Integer v1 : new Integer[] { 1, null })
1406      {
1407          final CompletableFuture<Integer> f = new CompletableFuture<>();
1408          final CompletableFuture<Integer> g = new CompletableFuture<>();
1503        final SubtractAction r = new SubtractAction();
1409          final CFException ex = new CFException();
1410 +        final SubtractFunction r = new SubtractFunction(m);
1411  
1412 <        g.completeExceptionally(ex);
1413 <        f.complete(v1);
1414 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1412 >        (fFirst ? f : g).complete(v1);
1413 >        if (!createIncomplete)
1414 >            (!fFirst ? f : g).completeExceptionally(ex);
1415 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1416 >        if (createIncomplete) {
1417 >            checkIncomplete(h);
1418 >            (!fFirst ? f : g).completeExceptionally(ex);
1419 >        }
1420  
1421 <        checkCompletedWithWrappedCFException(h, ex);
1422 <        checkCompletedWithWrappedCFException(g, ex);
1423 <        assertEquals(0, r.invocationCount);
1424 <        checkCompletedNormally(f, v1);
1421 >        checkCompletedWithWrappedException(h, ex);
1422 >        r.assertNotInvoked();
1423 >        checkCompletedNormally(fFirst ? f : g, v1);
1424 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1425      }}
1426  
1427 <    public void testThenAcceptBoth_exceptionalCompletion4() {
1427 >    /**
1428 >     * thenCombine result completes exceptionally if either source cancelled
1429 >     */
1430 >    public void testThenCombine_sourceCancelled() {
1431          for (ExecutionMode m : ExecutionMode.values())
1432 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1433 +        for (boolean createIncomplete : new boolean[] { true, false })
1434 +        for (boolean fFirst : new boolean[] { true, false })
1435          for (Integer v1 : new Integer[] { 1, null })
1436      {
1437          final CompletableFuture<Integer> f = new CompletableFuture<>();
1438          final CompletableFuture<Integer> g = new CompletableFuture<>();
1439 <        final SubtractAction r = new SubtractAction();
1523 <        final CFException ex = new CFException();
1439 >        final SubtractFunction r = new SubtractFunction(m);
1440  
1441 <        f.completeExceptionally(ex);
1442 <        g.complete(v1);
1443 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1441 >        (fFirst ? f : g).complete(v1);
1442 >        if (!createIncomplete)
1443 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1444 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1445 >        if (createIncomplete) {
1446 >            checkIncomplete(h);
1447 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1448 >        }
1449  
1450 <        checkCompletedWithWrappedCFException(h, ex);
1451 <        checkCompletedWithWrappedCFException(f, ex);
1452 <        assertEquals(0, r.invocationCount);
1453 <        checkCompletedNormally(g, v1);
1450 >        checkCompletedWithWrappedCancellationException(h);
1451 >        checkCancelled(!fFirst ? f : g);
1452 >        r.assertNotInvoked();
1453 >        checkCompletedNormally(fFirst ? f : g, v1);
1454      }}
1455  
1456      /**
1457 <     * thenAcceptBoth result completes exceptionally if action does
1457 >     * thenCombine result completes exceptionally if action does
1458       */
1459 <    public void testThenAcceptBoth_actionFailed1() {
1459 >    public void testThenCombine_actionFailed() {
1460          for (ExecutionMode m : ExecutionMode.values())
1461 +        for (boolean fFirst : new boolean[] { true, false })
1462          for (Integer v1 : new Integer[] { 1, null })
1463          for (Integer v2 : new Integer[] { 2, null })
1464      {
1465          final CompletableFuture<Integer> f = new CompletableFuture<>();
1466          final CompletableFuture<Integer> g = new CompletableFuture<>();
1467 <        final FailingBiConsumer r = new FailingBiConsumer();
1468 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1467 >        final FailingBiFunction r = new FailingBiFunction(m);
1468 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1469  
1470 <        f.complete(v1);
1471 <        checkIncomplete(h);
1472 <        g.complete(v2);
1470 >        if (fFirst) {
1471 >            f.complete(v1);
1472 >            g.complete(v2);
1473 >        } else {
1474 >            g.complete(v2);
1475 >            f.complete(v1);
1476 >        }
1477  
1478          checkCompletedWithWrappedCFException(h);
1479          checkCompletedNormally(f, v1);
1480          checkCompletedNormally(g, v2);
1481      }}
1482  
1483 <    public void testThenAcceptBoth_actionFailed2() {
1483 >    /**
1484 >     * thenAcceptBoth result completes normally after normal
1485 >     * completion of sources
1486 >     */
1487 >    public void testThenAcceptBoth_normalCompletion() {
1488          for (ExecutionMode m : ExecutionMode.values())
1489 +        for (boolean createIncomplete : new boolean[] { true, false })
1490 +        for (boolean fFirst : new boolean[] { true, false })
1491          for (Integer v1 : new Integer[] { 1, null })
1492          for (Integer v2 : new Integer[] { 2, null })
1493      {
1494          final CompletableFuture<Integer> f = new CompletableFuture<>();
1495          final CompletableFuture<Integer> g = new CompletableFuture<>();
1496 <        final FailingBiConsumer r = new FailingBiConsumer();
1565 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1496 >        final SubtractAction r = new SubtractAction(m);
1497  
1498 <        g.complete(v2);
1499 <        checkIncomplete(h);
1500 <        f.complete(v1);
1498 >        if (fFirst) f.complete(v1); else g.complete(v2);
1499 >        if (!createIncomplete)
1500 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1501 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1502 >        if (createIncomplete) {
1503 >            checkIncomplete(h);
1504 >            r.assertNotInvoked();
1505 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1506 >        }
1507  
1508 <        checkCompletedWithWrappedCFException(h);
1508 >        checkCompletedNormally(h, null);
1509 >        r.assertValue(subtract(v1, v2));
1510          checkCompletedNormally(f, v1);
1511          checkCompletedNormally(g, v2);
1512      }}
1513  
1514      /**
1515 <     * thenAcceptBoth result completes exceptionally if either source cancelled
1515 >     * thenAcceptBoth result completes exceptionally after exceptional
1516 >     * completion of either source
1517       */
1518 <    public void testThenAcceptBoth_sourceCancelled1() {
1580 <        for (ExecutionMode m : ExecutionMode.values())
1581 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1582 <        for (Integer v1 : new Integer[] { 1, null })
1583 <    {
1584 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1585 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1586 <        final SubtractAction r = new SubtractAction();
1587 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1588 <
1589 <        assertTrue(f.cancel(mayInterruptIfRunning));
1590 <        checkIncomplete(h);
1591 <        g.complete(v1);
1592 <
1593 <        checkCompletedWithWrappedCancellationException(h);
1594 <        checkCancelled(f);
1595 <        assertEquals(0, r.invocationCount);
1596 <        checkCompletedNormally(g, v1);
1597 <    }}
1598 <
1599 <    public void testThenAcceptBoth_sourceCancelled2() {
1518 >    public void testThenAcceptBoth_exceptionalCompletion() {
1519          for (ExecutionMode m : ExecutionMode.values())
1520 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1521 <        for (Integer v1 : new Integer[] { 1, null })
1603 <    {
1604 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1605 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1606 <        final SubtractAction r = new SubtractAction();
1607 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1608 <
1609 <        assertTrue(g.cancel(mayInterruptIfRunning));
1610 <        checkIncomplete(h);
1611 <        f.complete(v1);
1612 <
1613 <        checkCompletedWithWrappedCancellationException(h);
1614 <        checkCancelled(g);
1615 <        assertEquals(0, r.invocationCount);
1616 <        checkCompletedNormally(f, v1);
1617 <    }}
1618 <
1619 <    public void testThenAcceptBoth_sourceCancelled3() {
1620 <        for (ExecutionMode m : ExecutionMode.values())
1621 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1520 >        for (boolean createIncomplete : new boolean[] { true, false })
1521 >        for (boolean fFirst : new boolean[] { true, false })
1522          for (Integer v1 : new Integer[] { 1, null })
1523      {
1524          final CompletableFuture<Integer> f = new CompletableFuture<>();
1525          final CompletableFuture<Integer> g = new CompletableFuture<>();
1526 <        final SubtractAction r = new SubtractAction();
1526 >        final CFException ex = new CFException();
1527 >        final SubtractAction r = new SubtractAction(m);
1528  
1529 <        assertTrue(g.cancel(mayInterruptIfRunning));
1530 <        f.complete(v1);
1529 >        (fFirst ? f : g).complete(v1);
1530 >        if (!createIncomplete)
1531 >            (!fFirst ? f : g).completeExceptionally(ex);
1532          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1533 +        if (createIncomplete) {
1534 +            checkIncomplete(h);
1535 +            (!fFirst ? f : g).completeExceptionally(ex);
1536 +        }
1537  
1538 <        checkCompletedWithWrappedCancellationException(h);
1539 <        checkCancelled(g);
1540 <        assertEquals(0, r.invocationCount);
1541 <        checkCompletedNormally(f, v1);
1538 >        checkCompletedWithWrappedException(h, ex);
1539 >        r.assertNotInvoked();
1540 >        checkCompletedNormally(fFirst ? f : g, v1);
1541 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1542      }}
1543  
1544 <    public void testThenAcceptBoth_sourceCancelled4() {
1544 >    /**
1545 >     * thenAcceptBoth result completes exceptionally if either source cancelled
1546 >     */
1547 >    public void testThenAcceptBoth_sourceCancelled() {
1548          for (ExecutionMode m : ExecutionMode.values())
1549          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1550 +        for (boolean createIncomplete : new boolean[] { true, false })
1551 +        for (boolean fFirst : new boolean[] { true, false })
1552          for (Integer v1 : new Integer[] { 1, null })
1553      {
1554          final CompletableFuture<Integer> f = new CompletableFuture<>();
1555          final CompletableFuture<Integer> g = new CompletableFuture<>();
1556 <        final SubtractAction r = new SubtractAction();
1556 >        final SubtractAction r = new SubtractAction(m);
1557  
1558 <        assertTrue(f.cancel(mayInterruptIfRunning));
1559 <        g.complete(v1);
1558 >        (fFirst ? f : g).complete(v1);
1559 >        if (!createIncomplete)
1560 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1561          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1562 +        if (createIncomplete) {
1563 +            checkIncomplete(h);
1564 +            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1565 +        }
1566  
1567          checkCompletedWithWrappedCancellationException(h);
1568 <        checkCancelled(f);
1569 <        assertEquals(0, r.invocationCount);
1570 <        checkCompletedNormally(g, v1);
1568 >        checkCancelled(!fFirst ? f : g);
1569 >        r.assertNotInvoked();
1570 >        checkCompletedNormally(fFirst ? f : g, v1);
1571      }}
1572  
1573      /**
1574 <     * runAfterBoth result completes normally after normal
1659 <     * completion of sources
1574 >     * thenAcceptBoth result completes exceptionally if action does
1575       */
1576 <    public void testRunAfterBoth_normalCompletion1() {
1662 <        for (ExecutionMode m : ExecutionMode.values())
1663 <        for (Integer v1 : new Integer[] { 1, null })
1664 <        for (Integer v2 : new Integer[] { 2, null })
1665 <    {
1666 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1667 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1668 <        final Noop r = new Noop();
1669 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1670 <
1671 <        f.complete(v1);
1672 <        checkIncomplete(h);
1673 <        assertEquals(0, r.invocationCount);
1674 <        g.complete(v2);
1675 <
1676 <        checkCompletedNormally(h, null);
1677 <        assertEquals(1, r.invocationCount);
1678 <        checkCompletedNormally(f, v1);
1679 <        checkCompletedNormally(g, v2);
1680 <    }}
1681 <
1682 <    public void testRunAfterBoth_normalCompletion2() {
1683 <        for (ExecutionMode m : ExecutionMode.values())
1684 <        for (Integer v1 : new Integer[] { 1, null })
1685 <        for (Integer v2 : new Integer[] { 2, null })
1686 <    {
1687 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1688 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1689 <        final Noop r = new Noop();
1690 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1691 <
1692 <        g.complete(v2);
1693 <        checkIncomplete(h);
1694 <        assertEquals(0, r.invocationCount);
1695 <        f.complete(v1);
1696 <
1697 <        checkCompletedNormally(h, null);
1698 <        assertEquals(1, r.invocationCount);
1699 <        checkCompletedNormally(f, v1);
1700 <        checkCompletedNormally(g, v2);
1701 <    }}
1702 <
1703 <    public void testRunAfterBoth_normalCompletion3() {
1576 >    public void testThenAcceptBoth_actionFailed() {
1577          for (ExecutionMode m : ExecutionMode.values())
1578 +        for (boolean fFirst : new boolean[] { true, false })
1579          for (Integer v1 : new Integer[] { 1, null })
1580          for (Integer v2 : new Integer[] { 2, null })
1581      {
1582          final CompletableFuture<Integer> f = new CompletableFuture<>();
1583          final CompletableFuture<Integer> g = new CompletableFuture<>();
1584 <        final Noop r = new Noop();
1584 >        final FailingBiConsumer r = new FailingBiConsumer(m);
1585 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1586  
1587 <        g.complete(v2);
1588 <        f.complete(v1);
1589 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1587 >        if (fFirst) {
1588 >            f.complete(v1);
1589 >            g.complete(v2);
1590 >        } else {
1591 >            g.complete(v2);
1592 >            f.complete(v1);
1593 >        }
1594  
1595 <        checkCompletedNormally(h, null);
1717 <        assertEquals(1, r.invocationCount);
1595 >        checkCompletedWithWrappedCFException(h);
1596          checkCompletedNormally(f, v1);
1597          checkCompletedNormally(g, v2);
1598      }}
1599  
1600 <    public void testRunAfterBoth_normalCompletion4() {
1600 >    /**
1601 >     * runAfterBoth result completes normally after normal
1602 >     * completion of sources
1603 >     */
1604 >    public void testRunAfterBoth_normalCompletion() {
1605          for (ExecutionMode m : ExecutionMode.values())
1606 +        for (boolean createIncomplete : new boolean[] { true, false })
1607 +        for (boolean fFirst : new boolean[] { true, false })
1608          for (Integer v1 : new Integer[] { 1, null })
1609          for (Integer v2 : new Integer[] { 2, null })
1610      {
1611          final CompletableFuture<Integer> f = new CompletableFuture<>();
1612          final CompletableFuture<Integer> g = new CompletableFuture<>();
1613 <        final Noop r = new Noop();
1613 >        final Noop r = new Noop(m);
1614  
1615 <        f.complete(v1);
1616 <        g.complete(v2);
1615 >        if (fFirst) f.complete(v1); else g.complete(v2);
1616 >        if (!createIncomplete)
1617 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1618          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1619 +        if (createIncomplete) {
1620 +            checkIncomplete(h);
1621 +            r.assertNotInvoked();
1622 +            if (!fFirst) f.complete(v1); else g.complete(v2);
1623 +        }
1624  
1625          checkCompletedNormally(h, null);
1626 <        assertEquals(1, r.invocationCount);
1626 >        r.assertInvoked();
1627          checkCompletedNormally(f, v1);
1628          checkCompletedNormally(g, v2);
1629      }}
# Line 1742 | Line 1632 | public class CompletableFutureTest exten
1632       * runAfterBoth result completes exceptionally after exceptional
1633       * completion of either source
1634       */
1635 <    public void testRunAfterBoth_exceptionalCompletion1() {
1746 <        for (ExecutionMode m : ExecutionMode.values())
1747 <        for (Integer v1 : new Integer[] { 1, null })
1748 <    {
1749 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1750 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1751 <        final Noop r = new Noop();
1752 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1753 <        final CFException ex = new CFException();
1754 <
1755 <        f.completeExceptionally(ex);
1756 <        checkIncomplete(h);
1757 <        g.complete(v1);
1758 <
1759 <        checkCompletedWithWrappedCFException(h, ex);
1760 <        checkCompletedWithWrappedCFException(f, ex);
1761 <        assertEquals(0, r.invocationCount);
1762 <        checkCompletedNormally(g, v1);
1763 <    }}
1764 <
1765 <    public void testRunAfterBoth_exceptionalCompletion2() {
1766 <        for (ExecutionMode m : ExecutionMode.values())
1767 <        for (Integer v1 : new Integer[] { 1, null })
1768 <    {
1769 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1770 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1771 <        final Noop r = new Noop();
1772 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1773 <        final CFException ex = new CFException();
1774 <
1775 <        g.completeExceptionally(ex);
1776 <        checkIncomplete(h);
1777 <        f.complete(v1);
1778 <
1779 <        checkCompletedWithWrappedCFException(h, ex);
1780 <        checkCompletedWithWrappedCFException(g, ex);
1781 <        assertEquals(0, r.invocationCount);
1782 <        checkCompletedNormally(f, v1);
1783 <    }}
1784 <
1785 <    public void testRunAfterBoth_exceptionalCompletion3() {
1786 <        for (ExecutionMode m : ExecutionMode.values())
1787 <        for (Integer v1 : new Integer[] { 1, null })
1788 <    {
1789 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1790 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1791 <        final Noop r = new Noop();
1792 <        final CFException ex = new CFException();
1793 <
1794 <        g.completeExceptionally(ex);
1795 <        f.complete(v1);
1796 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1797 <
1798 <        checkCompletedWithWrappedCFException(h, ex);
1799 <        checkCompletedWithWrappedCFException(g, ex);
1800 <        assertEquals(0, r.invocationCount);
1801 <        checkCompletedNormally(f, v1);
1802 <    }}
1803 <
1804 <    public void testRunAfterBoth_exceptionalCompletion4() {
1635 >    public void testRunAfterBoth_exceptionalCompletion() {
1636          for (ExecutionMode m : ExecutionMode.values())
1637 +        for (boolean createIncomplete : new boolean[] { true, false })
1638 +        for (boolean fFirst : new boolean[] { true, false })
1639          for (Integer v1 : new Integer[] { 1, null })
1640      {
1641          final CompletableFuture<Integer> f = new CompletableFuture<>();
1642          final CompletableFuture<Integer> g = new CompletableFuture<>();
1810        final Noop r = new Noop();
1643          final CFException ex = new CFException();
1644 +        final Noop r = new Noop(m);
1645  
1646 <        f.completeExceptionally(ex);
1647 <        g.complete(v1);
1648 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1816 <
1817 <        checkCompletedWithWrappedCFException(h, ex);
1818 <        checkCompletedWithWrappedCFException(f, ex);
1819 <        assertEquals(0, r.invocationCount);
1820 <        checkCompletedNormally(g, v1);
1821 <    }}
1822 <
1823 <    /**
1824 <     * runAfterBoth result completes exceptionally if action does
1825 <     */
1826 <    public void testRunAfterBoth_actionFailed1() {
1827 <        for (ExecutionMode m : ExecutionMode.values())
1828 <        for (Integer v1 : new Integer[] { 1, null })
1829 <        for (Integer v2 : new Integer[] { 2, null })
1830 <    {
1831 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1832 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1833 <        final FailingNoop r = new FailingNoop();
1834 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1835 <
1836 <        f.complete(v1);
1837 <        checkIncomplete(h);
1838 <        g.complete(v2);
1839 <
1840 <        checkCompletedWithWrappedCFException(h);
1841 <        checkCompletedNormally(f, v1);
1842 <        checkCompletedNormally(g, v2);
1843 <    }}
1844 <
1845 <    public void testRunAfterBoth_actionFailed2() {
1846 <        for (ExecutionMode m : ExecutionMode.values())
1847 <        for (Integer v1 : new Integer[] { 1, null })
1848 <        for (Integer v2 : new Integer[] { 2, null })
1849 <    {
1850 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1851 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1852 <        final FailingNoop r = new FailingNoop();
1646 >        (fFirst ? f : g).complete(v1);
1647 >        if (!createIncomplete)
1648 >            (!fFirst ? f : g).completeExceptionally(ex);
1649          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1650 +        if (createIncomplete) {
1651 +            checkIncomplete(h);
1652 +            (!fFirst ? f : g).completeExceptionally(ex);
1653 +        }
1654  
1655 <        g.complete(v2);
1656 <        checkIncomplete(h);
1657 <        f.complete(v1);
1658 <
1859 <        checkCompletedWithWrappedCFException(h);
1860 <        checkCompletedNormally(f, v1);
1861 <        checkCompletedNormally(g, v2);
1655 >        checkCompletedWithWrappedException(h, ex);
1656 >        r.assertNotInvoked();
1657 >        checkCompletedNormally(fFirst ? f : g, v1);
1658 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1659      }}
1660  
1661      /**
1662       * runAfterBoth result completes exceptionally if either source cancelled
1663       */
1664 <    public void testRunAfterBoth_sourceCancelled1() {
1664 >    public void testRunAfterBoth_sourceCancelled() {
1665          for (ExecutionMode m : ExecutionMode.values())
1666          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1667 +        for (boolean createIncomplete : new boolean[] { true, false })
1668 +        for (boolean fFirst : new boolean[] { true, false })
1669          for (Integer v1 : new Integer[] { 1, null })
1670      {
1671          final CompletableFuture<Integer> f = new CompletableFuture<>();
1672          final CompletableFuture<Integer> g = new CompletableFuture<>();
1673 <        final Noop r = new Noop();
1875 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1876 <
1877 <        assertTrue(f.cancel(mayInterruptIfRunning));
1878 <        checkIncomplete(h);
1879 <        g.complete(v1);
1673 >        final Noop r = new Noop(m);
1674  
1881        checkCompletedWithWrappedCancellationException(h);
1882        checkCancelled(f);
1883        assertEquals(0, r.invocationCount);
1884        checkCompletedNormally(g, v1);
1885    }}
1675  
1676 <    public void testRunAfterBoth_sourceCancelled2() {
1677 <        for (ExecutionMode m : ExecutionMode.values())
1678 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1890 <        for (Integer v1 : new Integer[] { 1, null })
1891 <    {
1892 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1893 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1894 <        final Noop r = new Noop();
1676 >        (fFirst ? f : g).complete(v1);
1677 >        if (!createIncomplete)
1678 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1679          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1680 <
1681 <        assertTrue(g.cancel(mayInterruptIfRunning));
1682 <        checkIncomplete(h);
1683 <        f.complete(v1);
1680 >        if (createIncomplete) {
1681 >            checkIncomplete(h);
1682 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1683 >        }
1684  
1685          checkCompletedWithWrappedCancellationException(h);
1686 <        checkCancelled(g);
1687 <        assertEquals(0, r.invocationCount);
1688 <        checkCompletedNormally(f, v1);
1686 >        checkCancelled(!fFirst ? f : g);
1687 >        r.assertNotInvoked();
1688 >        checkCompletedNormally(fFirst ? f : g, v1);
1689      }}
1690  
1691 <    public void testRunAfterBoth_sourceCancelled3() {
1691 >    /**
1692 >     * runAfterBoth result completes exceptionally if action does
1693 >     */
1694 >    public void testRunAfterBoth_actionFailed() {
1695          for (ExecutionMode m : ExecutionMode.values())
1696 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1696 >        for (boolean fFirst : new boolean[] { true, false })
1697          for (Integer v1 : new Integer[] { 1, null })
1698 +        for (Integer v2 : new Integer[] { 2, null })
1699      {
1700          final CompletableFuture<Integer> f = new CompletableFuture<>();
1701          final CompletableFuture<Integer> g = new CompletableFuture<>();
1702 <        final Noop r = new Noop();
1702 >        final FailingRunnable r1 = new FailingRunnable(m);
1703 >        final FailingRunnable r2 = new FailingRunnable(m);
1704  
1705 <        assertTrue(g.cancel(mayInterruptIfRunning));
1706 <        f.complete(v1);
1707 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1705 >        CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1706 >        if (fFirst) {
1707 >            f.complete(v1);
1708 >            g.complete(v2);
1709 >        } else {
1710 >            g.complete(v2);
1711 >            f.complete(v1);
1712 >        }
1713 >        CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1714  
1715 <        checkCompletedWithWrappedCancellationException(h);
1716 <        checkCancelled(g);
1922 <        assertEquals(0, r.invocationCount);
1715 >        checkCompletedWithWrappedCFException(h1);
1716 >        checkCompletedWithWrappedCFException(h2);
1717          checkCompletedNormally(f, v1);
1718 <    }}
1925 <
1926 <    public void testRunAfterBoth_sourceCancelled4() {
1927 <        for (ExecutionMode m : ExecutionMode.values())
1928 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1929 <        for (Integer v1 : new Integer[] { 1, null })
1930 <    {
1931 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1932 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1933 <        final Noop r = new Noop();
1934 <
1935 <        assertTrue(f.cancel(mayInterruptIfRunning));
1936 <        g.complete(v1);
1937 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1938 <
1939 <        checkCompletedWithWrappedCancellationException(h);
1940 <        checkCancelled(f);
1941 <        assertEquals(0, r.invocationCount);
1942 <        checkCompletedNormally(g, v1);
1718 >        checkCompletedNormally(g, v2);
1719      }}
1720  
1721      /**
1722       * applyToEither result completes normally after normal completion
1723       * of either source
1724       */
1725 <    public void testApplyToEither_normalCompletion1() {
1725 >    public void testApplyToEither_normalCompletion() {
1726          for (ExecutionMode m : ExecutionMode.values())
1727          for (Integer v1 : new Integer[] { 1, null })
1728          for (Integer v2 : new Integer[] { 2, null })
1729      {
1730          final CompletableFuture<Integer> f = new CompletableFuture<>();
1731          final CompletableFuture<Integer> g = new CompletableFuture<>();
1732 <        final IncFunction r = new IncFunction();
1733 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1732 >        final IncFunction[] rs = new IncFunction[6];
1733 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1734  
1735 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1736 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1737 +        checkIncomplete(h0);
1738 +        checkIncomplete(h1);
1739 +        rs[0].assertNotInvoked();
1740 +        rs[1].assertNotInvoked();
1741          f.complete(v1);
1742 <        checkCompletedNormally(h, inc(v1));
1742 >        checkCompletedNormally(h0, inc(v1));
1743 >        checkCompletedNormally(h1, inc(v1));
1744 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1745 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1746 >        checkCompletedNormally(h2, inc(v1));
1747 >        checkCompletedNormally(h3, inc(v1));
1748          g.complete(v2);
1749  
1750 <        checkCompletedNormally(f, v1);
1751 <        checkCompletedNormally(g, v2);
1752 <        checkCompletedNormally(h, inc(v1));
1753 <    }}
1754 <
1755 <    public void testApplyToEither_normalCompletion2() {
1756 <        for (ExecutionMode m : ExecutionMode.values())
1757 <        for (Integer v1 : new Integer[] { 1, null })
1758 <        for (Integer v2 : new Integer[] { 2, null })
1972 <    {
1973 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1974 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1975 <        final IncFunction r = new IncFunction();
1976 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1977 <
1978 <        g.complete(v2);
1979 <        checkCompletedNormally(h, inc(v2));
1980 <        f.complete(v1);
1750 >        // unspecified behavior - both source completions available
1751 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
1752 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
1753 >        rs[4].assertValue(h4.join());
1754 >        rs[5].assertValue(h5.join());
1755 >        assertTrue(Objects.equals(inc(v1), h4.join()) ||
1756 >                   Objects.equals(inc(v2), h4.join()));
1757 >        assertTrue(Objects.equals(inc(v1), h5.join()) ||
1758 >                   Objects.equals(inc(v2), h5.join()));
1759  
1760          checkCompletedNormally(f, v1);
1761          checkCompletedNormally(g, v2);
1762 <        checkCompletedNormally(h, inc(v2));
1763 <        }}
1764 <
1765 <    public void testApplyToEither_normalCompletion3() {
1766 <        for (ExecutionMode m : ExecutionMode.values())
1989 <        for (Integer v1 : new Integer[] { 1, null })
1990 <        for (Integer v2 : new Integer[] { 2, null })
1991 <    {
1992 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1993 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1994 <        final IncFunction r = new IncFunction();
1995 <
1996 <        f.complete(v1);
1997 <        g.complete(v2);
1998 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1999 <
2000 <        checkCompletedNormally(f, v1);
2001 <        checkCompletedNormally(g, v2);
2002 <
2003 <        // unspecified behavior
2004 <        assertTrue(Objects.equals(h.join(), inc(v1)) ||
2005 <                   Objects.equals(h.join(), inc(v2)));
2006 <        assertEquals(1, r.invocationCount);
1762 >        checkCompletedNormally(h0, inc(v1));
1763 >        checkCompletedNormally(h1, inc(v1));
1764 >        checkCompletedNormally(h2, inc(v1));
1765 >        checkCompletedNormally(h3, inc(v1));
1766 >        for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1));
1767      }}
1768  
1769      /**
1770       * applyToEither result completes exceptionally after exceptional
1771       * completion of either source
1772       */
1773 <    public void testApplyToEither_exceptionalCompletion1() {
1773 >    public void testApplyToEither_exceptionalCompletion() {
1774          for (ExecutionMode m : ExecutionMode.values())
1775          for (Integer v1 : new Integer[] { 1, null })
1776      {
1777          final CompletableFuture<Integer> f = new CompletableFuture<>();
1778          final CompletableFuture<Integer> g = new CompletableFuture<>();
2019        final IncFunction r = new IncFunction();
2020        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1779          final CFException ex = new CFException();
1780 +        final IncFunction[] rs = new IncFunction[6];
1781 +        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1782  
1783 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1784 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1785 +        checkIncomplete(h0);
1786 +        checkIncomplete(h1);
1787 +        rs[0].assertNotInvoked();
1788 +        rs[1].assertNotInvoked();
1789          f.completeExceptionally(ex);
1790 <        checkCompletedWithWrappedCFException(h, ex);
1790 >        checkCompletedWithWrappedException(h0, ex);
1791 >        checkCompletedWithWrappedException(h1, ex);
1792 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1793 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1794 >        checkCompletedWithWrappedException(h2, ex);
1795 >        checkCompletedWithWrappedException(h3, ex);
1796          g.complete(v1);
1797  
1798 <        assertEquals(0, r.invocationCount);
1798 >        // unspecified behavior - both source completions available
1799 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
1800 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
1801 >        try {
1802 >            assertEquals(inc(v1), h4.join());
1803 >            rs[4].assertValue(inc(v1));
1804 >        } catch (CompletionException ok) {
1805 >            checkCompletedWithWrappedException(h4, ex);
1806 >            rs[4].assertNotInvoked();
1807 >        }
1808 >        try {
1809 >            assertEquals(inc(v1), h5.join());
1810 >            rs[5].assertValue(inc(v1));
1811 >        } catch (CompletionException ok) {
1812 >            checkCompletedWithWrappedException(h5, ex);
1813 >            rs[5].assertNotInvoked();
1814 >        }
1815 >
1816 >        checkCompletedExceptionally(f, ex);
1817          checkCompletedNormally(g, v1);
1818 <        checkCompletedWithWrappedCFException(f, ex);
1819 <        checkCompletedWithWrappedCFException(h, ex);
1818 >        checkCompletedWithWrappedException(h0, ex);
1819 >        checkCompletedWithWrappedException(h1, ex);
1820 >        checkCompletedWithWrappedException(h2, ex);
1821 >        checkCompletedWithWrappedException(h3, ex);
1822 >        checkCompletedWithWrappedException(h4, ex);
1823 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
1824      }}
1825  
1826      public void testApplyToEither_exceptionalCompletion2() {
1827          for (ExecutionMode m : ExecutionMode.values())
1828 +        for (boolean fFirst : new boolean[] { true, false })
1829          for (Integer v1 : new Integer[] { 1, null })
1830      {
1831          final CompletableFuture<Integer> f = new CompletableFuture<>();
1832          final CompletableFuture<Integer> g = new CompletableFuture<>();
2039        final IncFunction r = new IncFunction();
2040        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2041        final CFException ex = new CFException();
2042
2043        g.completeExceptionally(ex);
2044        checkCompletedWithWrappedCFException(h, ex);
2045        f.complete(v1);
2046
2047        assertEquals(0, r.invocationCount);
2048        checkCompletedNormally(f, v1);
2049        checkCompletedWithWrappedCFException(g, ex);
2050        checkCompletedWithWrappedCFException(h, ex);
2051    }}
2052
2053    public void testApplyToEither_exceptionalCompletion3() {
2054        for (ExecutionMode m : ExecutionMode.values())
2055        for (Integer v1 : new Integer[] { 1, null })
2056    {
2057        final CompletableFuture<Integer> f = new CompletableFuture<>();
2058        final CompletableFuture<Integer> g = new CompletableFuture<>();
2059        final IncFunction r = new IncFunction();
1833          final CFException ex = new CFException();
1834 +        final IncFunction[] rs = new IncFunction[6];
1835 +        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1836  
1837 <        g.completeExceptionally(ex);
1838 <        f.complete(v1);
1839 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1837 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1838 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1839 >        if (fFirst) {
1840 >            f.complete(v1);
1841 >            g.completeExceptionally(ex);
1842 >        } else {
1843 >            g.completeExceptionally(ex);
1844 >            f.complete(v1);
1845 >        }
1846 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1847 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1848  
1849 <        // unspecified behavior
2067 <        Integer v;
1849 >        // unspecified behavior - both source completions available
1850          try {
1851 <            assertEquals(inc(v1), h.join());
1852 <            assertEquals(1, r.invocationCount);
1851 >            assertEquals(inc(v1), h0.join());
1852 >            rs[0].assertValue(inc(v1));
1853          } catch (CompletionException ok) {
1854 <            checkCompletedWithWrappedCFException(h, ex);
1855 <            assertEquals(0, r.invocationCount);
1854 >            checkCompletedWithWrappedException(h0, ex);
1855 >            rs[0].assertNotInvoked();
1856          }
2075
2076        checkCompletedWithWrappedCFException(g, ex);
2077        checkCompletedNormally(f, v1);
2078    }}
2079
2080    public void testApplyToEither_exceptionalCompletion4() {
2081        for (ExecutionMode m : ExecutionMode.values())
2082        for (Integer v1 : new Integer[] { 1, null })
2083    {
2084        final CompletableFuture<Integer> f = new CompletableFuture<>();
2085        final CompletableFuture<Integer> g = new CompletableFuture<>();
2086        final IncFunction r = new IncFunction();
2087        final CFException ex = new CFException();
2088
2089        f.completeExceptionally(ex);
2090        g.complete(v1);
2091        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2092
2093        // unspecified behavior
2094        Integer v;
1857          try {
1858 <            assertEquals(inc(v1), h.join());
1859 <            assertEquals(1, r.invocationCount);
1858 >            assertEquals(inc(v1), h1.join());
1859 >            rs[1].assertValue(inc(v1));
1860          } catch (CompletionException ok) {
1861 <            checkCompletedWithWrappedCFException(h, ex);
1862 <            assertEquals(0, r.invocationCount);
1861 >            checkCompletedWithWrappedException(h1, ex);
1862 >            rs[1].assertNotInvoked();
1863 >        }
1864 >        try {
1865 >            assertEquals(inc(v1), h2.join());
1866 >            rs[2].assertValue(inc(v1));
1867 >        } catch (CompletionException ok) {
1868 >            checkCompletedWithWrappedException(h2, ex);
1869 >            rs[2].assertNotInvoked();
1870 >        }
1871 >        try {
1872 >            assertEquals(inc(v1), h3.join());
1873 >            rs[3].assertValue(inc(v1));
1874 >        } catch (CompletionException ok) {
1875 >            checkCompletedWithWrappedException(h3, ex);
1876 >            rs[3].assertNotInvoked();
1877          }
1878  
2103        checkCompletedWithWrappedCFException(f, ex);
2104        checkCompletedNormally(g, v1);
2105    }}
2106
2107    /**
2108     * applyToEither result completes exceptionally if action does
2109     */
2110    public void testApplyToEither_actionFailed1() {
2111        for (ExecutionMode m : ExecutionMode.values())
2112        for (Integer v1 : new Integer[] { 1, null })
2113        for (Integer v2 : new Integer[] { 2, null })
2114    {
2115        final CompletableFuture<Integer> f = new CompletableFuture<>();
2116        final CompletableFuture<Integer> g = new CompletableFuture<>();
2117        final FailingFunction r = new FailingFunction();
2118        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2119
2120        f.complete(v1);
2121        checkCompletedWithWrappedCFException(h);
2122        g.complete(v2);
2123        checkCompletedNormally(f, v1);
2124        checkCompletedNormally(g, v2);
2125    }}
2126
2127    public void testApplyToEither_actionFailed2() {
2128        for (ExecutionMode m : ExecutionMode.values())
2129        for (Integer v1 : new Integer[] { 1, null })
2130        for (Integer v2 : new Integer[] { 2, null })
2131    {
2132        final CompletableFuture<Integer> f = new CompletableFuture<>();
2133        final CompletableFuture<Integer> g = new CompletableFuture<>();
2134        final FailingFunction r = new FailingFunction();
2135        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2136
2137        g.complete(v2);
2138        checkCompletedWithWrappedCFException(h);
2139        f.complete(v1);
1879          checkCompletedNormally(f, v1);
1880 <        checkCompletedNormally(g, v2);
1880 >        checkCompletedExceptionally(g, ex);
1881      }}
1882  
1883      /**
1884       * applyToEither result completes exceptionally if either source cancelled
1885       */
1886 <    public void testApplyToEither_sourceCancelled1() {
1886 >    public void testApplyToEither_sourceCancelled() {
1887          for (ExecutionMode m : ExecutionMode.values())
1888          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1889          for (Integer v1 : new Integer[] { 1, null })
1890      {
1891          final CompletableFuture<Integer> f = new CompletableFuture<>();
1892          final CompletableFuture<Integer> g = new CompletableFuture<>();
1893 <        final IncFunction r = new IncFunction();
1894 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1893 >        final IncFunction[] rs = new IncFunction[6];
1894 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1895  
1896 <        assertTrue(f.cancel(mayInterruptIfRunning));
1897 <        checkCompletedWithWrappedCancellationException(h);
1896 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1897 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1898 >        checkIncomplete(h0);
1899 >        checkIncomplete(h1);
1900 >        rs[0].assertNotInvoked();
1901 >        rs[1].assertNotInvoked();
1902 >        f.cancel(mayInterruptIfRunning);
1903 >        checkCompletedWithWrappedCancellationException(h0);
1904 >        checkCompletedWithWrappedCancellationException(h1);
1905 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1906 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1907 >        checkCompletedWithWrappedCancellationException(h2);
1908 >        checkCompletedWithWrappedCancellationException(h3);
1909          g.complete(v1);
1910  
1911 +        // unspecified behavior - both source completions available
1912 +        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
1913 +        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
1914 +        try {
1915 +            assertEquals(inc(v1), h4.join());
1916 +            rs[4].assertValue(inc(v1));
1917 +        } catch (CompletionException ok) {
1918 +            checkCompletedWithWrappedCancellationException(h4);
1919 +            rs[4].assertNotInvoked();
1920 +        }
1921 +        try {
1922 +            assertEquals(inc(v1), h5.join());
1923 +            rs[5].assertValue(inc(v1));
1924 +        } catch (CompletionException ok) {
1925 +            checkCompletedWithWrappedCancellationException(h5);
1926 +            rs[5].assertNotInvoked();
1927 +        }
1928 +
1929          checkCancelled(f);
2162        assertEquals(0, r.invocationCount);
1930          checkCompletedNormally(g, v1);
1931 <        checkCompletedWithWrappedCancellationException(h);
1931 >        checkCompletedWithWrappedCancellationException(h0);
1932 >        checkCompletedWithWrappedCancellationException(h1);
1933 >        checkCompletedWithWrappedCancellationException(h2);
1934 >        checkCompletedWithWrappedCancellationException(h3);
1935 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
1936      }}
1937  
1938      public void testApplyToEither_sourceCancelled2() {
1939          for (ExecutionMode m : ExecutionMode.values())
1940          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1941 +        for (boolean fFirst : new boolean[] { true, false })
1942          for (Integer v1 : new Integer[] { 1, null })
1943      {
1944          final CompletableFuture<Integer> f = new CompletableFuture<>();
1945          final CompletableFuture<Integer> g = new CompletableFuture<>();
1946 <        final IncFunction r = new IncFunction();
1947 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2176 <
2177 <        assertTrue(g.cancel(mayInterruptIfRunning));
2178 <        checkCompletedWithWrappedCancellationException(h);
2179 <        f.complete(v1);
2180 <
2181 <        checkCancelled(g);
2182 <        assertEquals(0, r.invocationCount);
2183 <        checkCompletedNormally(f, v1);
2184 <        checkCompletedWithWrappedCancellationException(h);
2185 <    }}
2186 <
2187 <    public void testApplyToEither_sourceCancelled3() {
2188 <        for (ExecutionMode m : ExecutionMode.values())
2189 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2190 <        for (Integer v1 : new Integer[] { 1, null })
2191 <    {
2192 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2193 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2194 <        final IncFunction r = new IncFunction();
1946 >        final IncFunction[] rs = new IncFunction[6];
1947 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1948  
1949 <        assertTrue(g.cancel(mayInterruptIfRunning));
1950 <        f.complete(v1);
1951 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1949 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1950 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1951 >        if (fFirst) {
1952 >            f.complete(v1);
1953 >            g.cancel(mayInterruptIfRunning);
1954 >        } else {
1955 >            g.cancel(mayInterruptIfRunning);
1956 >            f.complete(v1);
1957 >        }
1958 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1959 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1960  
1961 <        // unspecified behavior
2201 <        Integer v;
1961 >        // unspecified behavior - both source completions available
1962          try {
1963 <            assertEquals(inc(v1), h.join());
1964 <            assertEquals(1, r.invocationCount);
1963 >            assertEquals(inc(v1), h0.join());
1964 >            rs[0].assertValue(inc(v1));
1965          } catch (CompletionException ok) {
1966 <            checkCompletedWithWrappedCancellationException(h);
1967 <            assertEquals(0, r.invocationCount);
1966 >            checkCompletedWithWrappedCancellationException(h0);
1967 >            rs[0].assertNotInvoked();
1968 >        }
1969 >        try {
1970 >            assertEquals(inc(v1), h1.join());
1971 >            rs[1].assertValue(inc(v1));
1972 >        } catch (CompletionException ok) {
1973 >            checkCompletedWithWrappedCancellationException(h1);
1974 >            rs[1].assertNotInvoked();
1975 >        }
1976 >        try {
1977 >            assertEquals(inc(v1), h2.join());
1978 >            rs[2].assertValue(inc(v1));
1979 >        } catch (CompletionException ok) {
1980 >            checkCompletedWithWrappedCancellationException(h2);
1981 >            rs[2].assertNotInvoked();
1982          }
2209
2210        checkCancelled(g);
2211        checkCompletedNormally(f, v1);
2212    }}
2213
2214    public void testApplyToEither_sourceCancelled4() {
2215        for (ExecutionMode m : ExecutionMode.values())
2216        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2217        for (Integer v1 : new Integer[] { 1, null })
2218    {
2219        final CompletableFuture<Integer> f = new CompletableFuture<>();
2220        final CompletableFuture<Integer> g = new CompletableFuture<>();
2221        final IncFunction r = new IncFunction();
2222
2223        assertTrue(f.cancel(mayInterruptIfRunning));
2224        g.complete(v1);
2225        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2226
2227        // unspecified behavior
2228        Integer v;
1983          try {
1984 <            assertEquals(inc(v1), h.join());
1985 <            assertEquals(1, r.invocationCount);
1984 >            assertEquals(inc(v1), h3.join());
1985 >            rs[3].assertValue(inc(v1));
1986          } catch (CompletionException ok) {
1987 <            checkCompletedWithWrappedCancellationException(h);
1988 <            assertEquals(0, r.invocationCount);
1987 >            checkCompletedWithWrappedCancellationException(h3);
1988 >            rs[3].assertNotInvoked();
1989          }
1990  
1991 <        checkCancelled(f);
1992 <        checkCompletedNormally(g, v1);
1991 >        checkCompletedNormally(f, v1);
1992 >        checkCancelled(g);
1993      }}
1994  
1995      /**
1996 <     * acceptEither result completes normally after normal completion
2243 <     * of either source
1996 >     * applyToEither result completes exceptionally if action does
1997       */
1998 <    public void testAcceptEither_normalCompletion1() {
1998 >    public void testApplyToEither_actionFailed() {
1999          for (ExecutionMode m : ExecutionMode.values())
2000          for (Integer v1 : new Integer[] { 1, null })
2001          for (Integer v2 : new Integer[] { 2, null })
2002      {
2003          final CompletableFuture<Integer> f = new CompletableFuture<>();
2004          final CompletableFuture<Integer> g = new CompletableFuture<>();
2005 <        final IncAction r = new IncAction();
2006 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2005 >        final FailingFunction[] rs = new FailingFunction[6];
2006 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
2007  
2008 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2009 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2010          f.complete(v1);
2011 <        checkCompletedNormally(h, null);
2012 <        assertEquals(inc(v1), r.value);
2011 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2012 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2013 >        checkCompletedWithWrappedCFException(h0);
2014 >        checkCompletedWithWrappedCFException(h1);
2015 >        checkCompletedWithWrappedCFException(h2);
2016 >        checkCompletedWithWrappedCFException(h3);
2017 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2018 >
2019          g.complete(v2);
2020  
2021 <        checkCompletedNormally(f, v1);
2022 <        checkCompletedNormally(g, v2);
2023 <        checkCompletedNormally(h, null);
2263 <    }}
2021 >        // unspecified behavior - both source completions available
2022 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2023 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2024  
2025 <    public void testAcceptEither_normalCompletion2() {
2026 <        for (ExecutionMode m : ExecutionMode.values())
2027 <        for (Integer v1 : new Integer[] { 1, null })
2028 <        for (Integer v2 : new Integer[] { 2, null })
2029 <    {
2030 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2271 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2272 <        final IncAction r = new IncAction();
2273 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2274 <
2275 <        g.complete(v2);
2276 <        checkCompletedNormally(h, null);
2277 <        assertEquals(inc(v2), r.value);
2278 <        f.complete(v1);
2025 >        checkCompletedWithWrappedCFException(h4);
2026 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2027 >                   Objects.equals(v2, rs[4].value));
2028 >        checkCompletedWithWrappedCFException(h5);
2029 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2030 >                   Objects.equals(v2, rs[5].value));
2031  
2032          checkCompletedNormally(f, v1);
2033          checkCompletedNormally(g, v2);
2282        checkCompletedNormally(h, null);
2034      }}
2035  
2036 <    public void testAcceptEither_normalCompletion3() {
2036 >    /**
2037 >     * acceptEither result completes normally after normal completion
2038 >     * of either source
2039 >     */
2040 >    public void testAcceptEither_normalCompletion() {
2041          for (ExecutionMode m : ExecutionMode.values())
2042          for (Integer v1 : new Integer[] { 1, null })
2043          for (Integer v2 : new Integer[] { 2, null })
2044      {
2045          final CompletableFuture<Integer> f = new CompletableFuture<>();
2046          final CompletableFuture<Integer> g = new CompletableFuture<>();
2047 <        final IncAction r = new IncAction();
2047 >        final NoopConsumer[] rs = new NoopConsumer[6];
2048 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2049  
2050 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2051 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2052 +        checkIncomplete(h0);
2053 +        checkIncomplete(h1);
2054 +        rs[0].assertNotInvoked();
2055 +        rs[1].assertNotInvoked();
2056          f.complete(v1);
2057 +        checkCompletedNormally(h0, null);
2058 +        checkCompletedNormally(h1, null);
2059 +        rs[0].assertValue(v1);
2060 +        rs[1].assertValue(v1);
2061 +        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2062 +        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2063 +        checkCompletedNormally(h2, null);
2064 +        checkCompletedNormally(h3, null);
2065 +        rs[2].assertValue(v1);
2066 +        rs[3].assertValue(v1);
2067          g.complete(v2);
2296        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2068  
2069 <        checkCompletedNormally(h, null);
2069 >        // unspecified behavior - both source completions available
2070 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2071 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2072 >        checkCompletedNormally(h4, null);
2073 >        checkCompletedNormally(h5, null);
2074 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2075 >                   Objects.equals(v2, rs[4].value));
2076 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2077 >                   Objects.equals(v2, rs[5].value));
2078 >
2079          checkCompletedNormally(f, v1);
2080          checkCompletedNormally(g, v2);
2081 <
2082 <        // unspecified behavior
2083 <        assertTrue(Objects.equals(r.value, inc(v1)) ||
2084 <                   Objects.equals(r.value, inc(v2)));
2081 >        checkCompletedNormally(h0, null);
2082 >        checkCompletedNormally(h1, null);
2083 >        checkCompletedNormally(h2, null);
2084 >        checkCompletedNormally(h3, null);
2085 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2086      }}
2087  
2088      /**
2089       * acceptEither result completes exceptionally after exceptional
2090       * completion of either source
2091       */
2092 <    public void testAcceptEither_exceptionalCompletion1() {
2092 >    public void testAcceptEither_exceptionalCompletion() {
2093          for (ExecutionMode m : ExecutionMode.values())
2094          for (Integer v1 : new Integer[] { 1, null })
2095      {
2096          final CompletableFuture<Integer> f = new CompletableFuture<>();
2097          final CompletableFuture<Integer> g = new CompletableFuture<>();
2317        final IncAction r = new IncAction();
2318        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2098          final CFException ex = new CFException();
2099 +        final NoopConsumer[] rs = new NoopConsumer[6];
2100 +        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2101  
2102 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2103 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2104 +        checkIncomplete(h0);
2105 +        checkIncomplete(h1);
2106 +        rs[0].assertNotInvoked();
2107 +        rs[1].assertNotInvoked();
2108          f.completeExceptionally(ex);
2109 <        checkCompletedWithWrappedCFException(h, ex);
2109 >        checkCompletedWithWrappedException(h0, ex);
2110 >        checkCompletedWithWrappedException(h1, ex);
2111 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2112 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2113 >        checkCompletedWithWrappedException(h2, ex);
2114 >        checkCompletedWithWrappedException(h3, ex);
2115 >
2116          g.complete(v1);
2117  
2118 <        assertEquals(0, r.invocationCount);
2118 >        // unspecified behavior - both source completions available
2119 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2120 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2121 >        try {
2122 >            assertNull(h4.join());
2123 >            rs[4].assertValue(v1);
2124 >        } catch (CompletionException ok) {
2125 >            checkCompletedWithWrappedException(h4, ex);
2126 >            rs[4].assertNotInvoked();
2127 >        }
2128 >        try {
2129 >            assertNull(h5.join());
2130 >            rs[5].assertValue(v1);
2131 >        } catch (CompletionException ok) {
2132 >            checkCompletedWithWrappedException(h5, ex);
2133 >            rs[5].assertNotInvoked();
2134 >        }
2135 >
2136 >        checkCompletedExceptionally(f, ex);
2137          checkCompletedNormally(g, v1);
2138 <        checkCompletedWithWrappedCFException(f, ex);
2139 <        checkCompletedWithWrappedCFException(h, ex);
2138 >        checkCompletedWithWrappedException(h0, ex);
2139 >        checkCompletedWithWrappedException(h1, ex);
2140 >        checkCompletedWithWrappedException(h2, ex);
2141 >        checkCompletedWithWrappedException(h3, ex);
2142 >        checkCompletedWithWrappedException(h4, ex);
2143 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2144      }}
2145  
2146      public void testAcceptEither_exceptionalCompletion2() {
2147          for (ExecutionMode m : ExecutionMode.values())
2148 +        for (boolean fFirst : new boolean[] { true, false })
2149          for (Integer v1 : new Integer[] { 1, null })
2150      {
2151          final CompletableFuture<Integer> f = new CompletableFuture<>();
2152          final CompletableFuture<Integer> g = new CompletableFuture<>();
2337        final IncAction r = new IncAction();
2338        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2339        final CFException ex = new CFException();
2340
2341        g.completeExceptionally(ex);
2342        checkCompletedWithWrappedCFException(h, ex);
2343        f.complete(v1);
2344
2345        assertEquals(0, r.invocationCount);
2346        checkCompletedNormally(f, v1);
2347        checkCompletedWithWrappedCFException(g, ex);
2348        checkCompletedWithWrappedCFException(h, ex);
2349    }}
2350
2351    public void testAcceptEither_exceptionalCompletion3() {
2352        for (ExecutionMode m : ExecutionMode.values())
2353        for (Integer v1 : new Integer[] { 1, null })
2354    {
2355        final CompletableFuture<Integer> f = new CompletableFuture<>();
2356        final CompletableFuture<Integer> g = new CompletableFuture<>();
2357        final IncAction r = new IncAction();
2153          final CFException ex = new CFException();
2154 +        final NoopConsumer[] rs = new NoopConsumer[6];
2155 +        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2156  
2157 <        g.completeExceptionally(ex);
2158 <        f.complete(v1);
2159 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2157 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2158 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2159 >        if (fFirst) {
2160 >            f.complete(v1);
2161 >            g.completeExceptionally(ex);
2162 >        } else {
2163 >            g.completeExceptionally(ex);
2164 >            f.complete(v1);
2165 >        }
2166 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2167 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2168  
2169 <        // unspecified behavior
2365 <        Integer v;
2169 >        // unspecified behavior - both source completions available
2170          try {
2171 <            assertNull(h.join());
2172 <            assertEquals(1, r.invocationCount);
2369 <            assertEquals(inc(v1), r.value);
2171 >            assertEquals(null, h0.join());
2172 >            rs[0].assertValue(v1);
2173          } catch (CompletionException ok) {
2174 <            checkCompletedWithWrappedCFException(h, ex);
2175 <            assertEquals(0, r.invocationCount);
2174 >            checkCompletedWithWrappedException(h0, ex);
2175 >            rs[0].assertNotInvoked();
2176          }
2374
2375        checkCompletedWithWrappedCFException(g, ex);
2376        checkCompletedNormally(f, v1);
2377    }}
2378
2379    public void testAcceptEither_exceptionalCompletion4() {
2380        for (ExecutionMode m : ExecutionMode.values())
2381        for (Integer v1 : new Integer[] { 1, null })
2382    {
2383        final CompletableFuture<Integer> f = new CompletableFuture<>();
2384        final CompletableFuture<Integer> g = new CompletableFuture<>();
2385        final IncAction r = new IncAction();
2386        final CFException ex = new CFException();
2387
2388        f.completeExceptionally(ex);
2389        g.complete(v1);
2390        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2391
2392        // unspecified behavior
2393        Integer v;
2177          try {
2178 <            assertNull(h.join());
2179 <            assertEquals(1, r.invocationCount);
2397 <            assertEquals(inc(v1), r.value);
2178 >            assertEquals(null, h1.join());
2179 >            rs[1].assertValue(v1);
2180          } catch (CompletionException ok) {
2181 <            checkCompletedWithWrappedCFException(h, ex);
2182 <            assertEquals(0, r.invocationCount);
2181 >            checkCompletedWithWrappedException(h1, ex);
2182 >            rs[1].assertNotInvoked();
2183 >        }
2184 >        try {
2185 >            assertEquals(null, h2.join());
2186 >            rs[2].assertValue(v1);
2187 >        } catch (CompletionException ok) {
2188 >            checkCompletedWithWrappedException(h2, ex);
2189 >            rs[2].assertNotInvoked();
2190 >        }
2191 >        try {
2192 >            assertEquals(null, h3.join());
2193 >            rs[3].assertValue(v1);
2194 >        } catch (CompletionException ok) {
2195 >            checkCompletedWithWrappedException(h3, ex);
2196 >            rs[3].assertNotInvoked();
2197          }
2198  
2403        checkCompletedWithWrappedCFException(f, ex);
2404        checkCompletedNormally(g, v1);
2405    }}
2406
2407    /**
2408     * acceptEither result completes exceptionally if action does
2409     */
2410    public void testAcceptEither_actionFailed1() {
2411        for (ExecutionMode m : ExecutionMode.values())
2412        for (Integer v1 : new Integer[] { 1, null })
2413        for (Integer v2 : new Integer[] { 2, null })
2414    {
2415        final CompletableFuture<Integer> f = new CompletableFuture<>();
2416        final CompletableFuture<Integer> g = new CompletableFuture<>();
2417        final FailingConsumer r = new FailingConsumer();
2418        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2419
2420        f.complete(v1);
2421        checkCompletedWithWrappedCFException(h);
2422        g.complete(v2);
2423        checkCompletedNormally(f, v1);
2424        checkCompletedNormally(g, v2);
2425    }}
2426
2427    public void testAcceptEither_actionFailed2() {
2428        for (ExecutionMode m : ExecutionMode.values())
2429        for (Integer v1 : new Integer[] { 1, null })
2430        for (Integer v2 : new Integer[] { 2, null })
2431    {
2432        final CompletableFuture<Integer> f = new CompletableFuture<>();
2433        final CompletableFuture<Integer> g = new CompletableFuture<>();
2434        final FailingConsumer r = new FailingConsumer();
2435        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2436
2437        g.complete(v2);
2438        checkCompletedWithWrappedCFException(h);
2439        f.complete(v1);
2199          checkCompletedNormally(f, v1);
2200 <        checkCompletedNormally(g, v2);
2200 >        checkCompletedExceptionally(g, ex);
2201      }}
2202  
2203      /**
2204       * acceptEither result completes exceptionally if either source cancelled
2205       */
2206 <    public void testAcceptEither_sourceCancelled1() {
2206 >    public void testAcceptEither_sourceCancelled() {
2207          for (ExecutionMode m : ExecutionMode.values())
2208          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2209          for (Integer v1 : new Integer[] { 1, null })
2210      {
2211          final CompletableFuture<Integer> f = new CompletableFuture<>();
2212          final CompletableFuture<Integer> g = new CompletableFuture<>();
2213 <        final IncAction r = new IncAction();
2214 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2213 >        final NoopConsumer[] rs = new NoopConsumer[6];
2214 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2215  
2216 <        assertTrue(f.cancel(mayInterruptIfRunning));
2217 <        checkCompletedWithWrappedCancellationException(h);
2218 <        g.complete(v1);
2216 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2217 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2218 >        checkIncomplete(h0);
2219 >        checkIncomplete(h1);
2220 >        rs[0].assertNotInvoked();
2221 >        rs[1].assertNotInvoked();
2222 >        f.cancel(mayInterruptIfRunning);
2223 >        checkCompletedWithWrappedCancellationException(h0);
2224 >        checkCompletedWithWrappedCancellationException(h1);
2225 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2226 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2227 >        checkCompletedWithWrappedCancellationException(h2);
2228 >        checkCompletedWithWrappedCancellationException(h3);
2229  
2230 <        checkCancelled(f);
2462 <        assertEquals(0, r.invocationCount);
2463 <        checkCompletedNormally(g, v1);
2464 <        checkCompletedWithWrappedCancellationException(h);
2465 <    }}
2466 <
2467 <    public void testAcceptEither_sourceCancelled2() {
2468 <        for (ExecutionMode m : ExecutionMode.values())
2469 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2470 <        for (Integer v1 : new Integer[] { 1, null })
2471 <    {
2472 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2473 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2474 <        final IncAction r = new IncAction();
2475 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2476 <
2477 <        assertTrue(g.cancel(mayInterruptIfRunning));
2478 <        checkCompletedWithWrappedCancellationException(h);
2479 <        f.complete(v1);
2480 <
2481 <        checkCancelled(g);
2482 <        assertEquals(0, r.invocationCount);
2483 <        checkCompletedNormally(f, v1);
2484 <        checkCompletedWithWrappedCancellationException(h);
2485 <    }}
2486 <
2487 <    public void testAcceptEither_sourceCancelled3() {
2488 <        for (ExecutionMode m : ExecutionMode.values())
2489 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2490 <        for (Integer v1 : new Integer[] { 1, null })
2491 <    {
2492 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2493 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2494 <        final IncAction r = new IncAction();
2495 <
2496 <        assertTrue(g.cancel(mayInterruptIfRunning));
2497 <        f.complete(v1);
2498 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2230 >        g.complete(v1);
2231  
2232 <        // unspecified behavior
2233 <        Integer v;
2232 >        // unspecified behavior - both source completions available
2233 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2234 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2235          try {
2236 <            assertNull(h.join());
2237 <            assertEquals(1, r.invocationCount);
2505 <            assertEquals(inc(v1), r.value);
2236 >            assertNull(h4.join());
2237 >            rs[4].assertValue(v1);
2238          } catch (CompletionException ok) {
2239 <            checkCompletedWithWrappedCancellationException(h);
2240 <            assertEquals(0, r.invocationCount);
2239 >            checkCompletedWithWrappedCancellationException(h4);
2240 >            rs[4].assertNotInvoked();
2241          }
2510
2511        checkCancelled(g);
2512        checkCompletedNormally(f, v1);
2513    }}
2514
2515    public void testAcceptEither_sourceCancelled4() {
2516        for (ExecutionMode m : ExecutionMode.values())
2517        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2518        for (Integer v1 : new Integer[] { 1, null })
2519    {
2520        final CompletableFuture<Integer> f = new CompletableFuture<>();
2521        final CompletableFuture<Integer> g = new CompletableFuture<>();
2522        final IncAction r = new IncAction();
2523
2524        assertTrue(f.cancel(mayInterruptIfRunning));
2525        g.complete(v1);
2526        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2527
2528        // unspecified behavior
2529        Integer v;
2242          try {
2243 <            assertNull(h.join());
2244 <            assertEquals(1, r.invocationCount);
2533 <            assertEquals(inc(v1), r.value);
2243 >            assertNull(h5.join());
2244 >            rs[5].assertValue(v1);
2245          } catch (CompletionException ok) {
2246 <            checkCompletedWithWrappedCancellationException(h);
2247 <            assertEquals(0, r.invocationCount);
2246 >            checkCompletedWithWrappedCancellationException(h5);
2247 >            rs[5].assertNotInvoked();
2248          }
2249  
2250          checkCancelled(f);
2251          checkCompletedNormally(g, v1);
2252 +        checkCompletedWithWrappedCancellationException(h0);
2253 +        checkCompletedWithWrappedCancellationException(h1);
2254 +        checkCompletedWithWrappedCancellationException(h2);
2255 +        checkCompletedWithWrappedCancellationException(h3);
2256 +        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2257      }}
2258  
2259      /**
2260 <     * runAfterEither result completes normally after normal completion
2545 <     * of either source
2260 >     * acceptEither result completes exceptionally if action does
2261       */
2262 <    public void testRunAfterEither_normalCompletion1() {
2262 >    public void testAcceptEither_actionFailed() {
2263          for (ExecutionMode m : ExecutionMode.values())
2264          for (Integer v1 : new Integer[] { 1, null })
2265          for (Integer v2 : new Integer[] { 2, null })
2266      {
2267          final CompletableFuture<Integer> f = new CompletableFuture<>();
2268          final CompletableFuture<Integer> g = new CompletableFuture<>();
2269 <        final Noop r = new Noop();
2270 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2269 >        final FailingConsumer[] rs = new FailingConsumer[6];
2270 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
2271  
2272 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2273 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2274          f.complete(v1);
2275 <        checkCompletedNormally(h, null);
2276 <        assertEquals(1, r.invocationCount);
2277 <        g.complete(v2);
2275 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2276 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2277 >        checkCompletedWithWrappedCFException(h0);
2278 >        checkCompletedWithWrappedCFException(h1);
2279 >        checkCompletedWithWrappedCFException(h2);
2280 >        checkCompletedWithWrappedCFException(h3);
2281 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2282  
2283 <        checkCompletedNormally(f, v1);
2563 <        checkCompletedNormally(g, v2);
2564 <        checkCompletedNormally(h, null);
2565 <        assertEquals(1, r.invocationCount);
2566 <    }}
2283 >        g.complete(v2);
2284  
2285 <    public void testRunAfterEither_normalCompletion2() {
2286 <        for (ExecutionMode m : ExecutionMode.values())
2287 <        for (Integer v1 : new Integer[] { 1, null })
2571 <        for (Integer v2 : new Integer[] { 2, null })
2572 <    {
2573 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2574 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2575 <        final Noop r = new Noop();
2576 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2285 >        // unspecified behavior - both source completions available
2286 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2287 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2288  
2289 <        g.complete(v2);
2290 <        checkCompletedNormally(h, null);
2291 <        assertEquals(1, r.invocationCount);
2292 <        f.complete(v1);
2289 >        checkCompletedWithWrappedCFException(h4);
2290 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2291 >                   Objects.equals(v2, rs[4].value));
2292 >        checkCompletedWithWrappedCFException(h5);
2293 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2294 >                   Objects.equals(v2, rs[5].value));
2295  
2296          checkCompletedNormally(f, v1);
2297          checkCompletedNormally(g, v2);
2298 <        checkCompletedNormally(h, null);
2586 <        assertEquals(1, r.invocationCount);
2587 <        }}
2298 >    }}
2299  
2300 <    public void testRunAfterEither_normalCompletion3() {
2300 >    /**
2301 >     * runAfterEither result completes normally after normal completion
2302 >     * of either source
2303 >     */
2304 >    public void testRunAfterEither_normalCompletion() {
2305          for (ExecutionMode m : ExecutionMode.values())
2306          for (Integer v1 : new Integer[] { 1, null })
2307          for (Integer v2 : new Integer[] { 2, null })
2308      {
2309          final CompletableFuture<Integer> f = new CompletableFuture<>();
2310          final CompletableFuture<Integer> g = new CompletableFuture<>();
2311 <        final Noop r = new Noop();
2311 >        final Noop[] rs = new Noop[6];
2312 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2313  
2314 +        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2315 +        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2316 +        checkIncomplete(h0);
2317 +        checkIncomplete(h1);
2318 +        rs[0].assertNotInvoked();
2319 +        rs[1].assertNotInvoked();
2320          f.complete(v1);
2321 +        checkCompletedNormally(h0, null);
2322 +        checkCompletedNormally(h1, null);
2323 +        rs[0].assertInvoked();
2324 +        rs[1].assertInvoked();
2325 +        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2326 +        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2327 +        checkCompletedNormally(h2, null);
2328 +        checkCompletedNormally(h3, null);
2329 +        rs[2].assertInvoked();
2330 +        rs[3].assertInvoked();
2331 +
2332          g.complete(v2);
2600        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2333  
2334 <        checkCompletedNormally(h, null);
2334 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2335 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2336 >
2337          checkCompletedNormally(f, v1);
2338          checkCompletedNormally(g, v2);
2339 <        assertEquals(1, r.invocationCount);
2339 >        checkCompletedNormally(h0, null);
2340 >        checkCompletedNormally(h1, null);
2341 >        checkCompletedNormally(h2, null);
2342 >        checkCompletedNormally(h3, null);
2343 >        checkCompletedNormally(h4, null);
2344 >        checkCompletedNormally(h5, null);
2345 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2346      }}
2347  
2348      /**
2349       * runAfterEither result completes exceptionally after exceptional
2350       * completion of either source
2351       */
2352 <    public void testRunAfterEither_exceptionalCompletion1() {
2352 >    public void testRunAfterEither_exceptionalCompletion() {
2353          for (ExecutionMode m : ExecutionMode.values())
2354          for (Integer v1 : new Integer[] { 1, null })
2355      {
2356          final CompletableFuture<Integer> f = new CompletableFuture<>();
2357          final CompletableFuture<Integer> g = new CompletableFuture<>();
2618        final Noop r = new Noop();
2619        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2358          final CFException ex = new CFException();
2359 +        final Noop[] rs = new Noop[6];
2360 +        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2361  
2362 +        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2363 +        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2364 +        checkIncomplete(h0);
2365 +        checkIncomplete(h1);
2366 +        rs[0].assertNotInvoked();
2367 +        rs[1].assertNotInvoked();
2368          f.completeExceptionally(ex);
2369 <        checkCompletedWithWrappedCFException(h, ex);
2369 >        checkCompletedWithWrappedException(h0, ex);
2370 >        checkCompletedWithWrappedException(h1, ex);
2371 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2372 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2373 >        checkCompletedWithWrappedException(h2, ex);
2374 >        checkCompletedWithWrappedException(h3, ex);
2375 >
2376          g.complete(v1);
2377  
2378 <        assertEquals(0, r.invocationCount);
2378 >        // unspecified behavior - both source completions available
2379 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2380 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2381 >        try {
2382 >            assertNull(h4.join());
2383 >            rs[4].assertInvoked();
2384 >        } catch (CompletionException ok) {
2385 >            checkCompletedWithWrappedException(h4, ex);
2386 >            rs[4].assertNotInvoked();
2387 >        }
2388 >        try {
2389 >            assertNull(h5.join());
2390 >            rs[5].assertInvoked();
2391 >        } catch (CompletionException ok) {
2392 >            checkCompletedWithWrappedException(h5, ex);
2393 >            rs[5].assertNotInvoked();
2394 >        }
2395 >
2396 >        checkCompletedExceptionally(f, ex);
2397          checkCompletedNormally(g, v1);
2398 <        checkCompletedWithWrappedCFException(f, ex);
2399 <        checkCompletedWithWrappedCFException(h, ex);
2398 >        checkCompletedWithWrappedException(h0, ex);
2399 >        checkCompletedWithWrappedException(h1, ex);
2400 >        checkCompletedWithWrappedException(h2, ex);
2401 >        checkCompletedWithWrappedException(h3, ex);
2402 >        checkCompletedWithWrappedException(h4, ex);
2403 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2404      }}
2405  
2406      public void testRunAfterEither_exceptionalCompletion2() {
2407          for (ExecutionMode m : ExecutionMode.values())
2408 +        for (boolean fFirst : new boolean[] { true, false })
2409          for (Integer v1 : new Integer[] { 1, null })
2410      {
2411          final CompletableFuture<Integer> f = new CompletableFuture<>();
2412          final CompletableFuture<Integer> g = new CompletableFuture<>();
2638        final Noop r = new Noop();
2639        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2640        final CFException ex = new CFException();
2641
2642        g.completeExceptionally(ex);
2643        checkCompletedWithWrappedCFException(h, ex);
2644        f.complete(v1);
2645
2646        assertEquals(0, r.invocationCount);
2647        checkCompletedNormally(f, v1);
2648        checkCompletedWithWrappedCFException(g, ex);
2649        checkCompletedWithWrappedCFException(h, ex);
2650    }}
2651
2652    public void testRunAfterEither_exceptionalCompletion3() {
2653        for (ExecutionMode m : ExecutionMode.values())
2654        for (Integer v1 : new Integer[] { 1, null })
2655    {
2656        final CompletableFuture<Integer> f = new CompletableFuture<>();
2657        final CompletableFuture<Integer> g = new CompletableFuture<>();
2658        final Noop r = new Noop();
2413          final CFException ex = new CFException();
2414 +        final Noop[] rs = new Noop[6];
2415 +        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2416  
2417 <        g.completeExceptionally(ex);
2418 <        f.complete(v1);
2419 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2417 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2418 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2419 >        if (fFirst) {
2420 >            f.complete(v1);
2421 >            g.completeExceptionally(ex);
2422 >        } else {
2423 >            g.completeExceptionally(ex);
2424 >            f.complete(v1);
2425 >        }
2426 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2427 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2428  
2429 <        // unspecified behavior
2666 <        Integer v;
2429 >        // unspecified behavior - both source completions available
2430          try {
2431 <            assertNull(h.join());
2432 <            assertEquals(1, r.invocationCount);
2431 >            assertEquals(null, h0.join());
2432 >            rs[0].assertInvoked();
2433          } catch (CompletionException ok) {
2434 <            checkCompletedWithWrappedCFException(h, ex);
2435 <            assertEquals(0, r.invocationCount);
2434 >            checkCompletedWithWrappedException(h0, ex);
2435 >            rs[0].assertNotInvoked();
2436          }
2674
2675        checkCompletedWithWrappedCFException(g, ex);
2676        checkCompletedNormally(f, v1);
2677    }}
2678
2679    public void testRunAfterEither_exceptionalCompletion4() {
2680        for (ExecutionMode m : ExecutionMode.values())
2681        for (Integer v1 : new Integer[] { 1, null })
2682    {
2683        final CompletableFuture<Integer> f = new CompletableFuture<>();
2684        final CompletableFuture<Integer> g = new CompletableFuture<>();
2685        final Noop r = new Noop();
2686        final CFException ex = new CFException();
2687
2688        f.completeExceptionally(ex);
2689        g.complete(v1);
2690        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2691
2692        // unspecified behavior
2693        Integer v;
2437          try {
2438 <            assertNull(h.join());
2439 <            assertEquals(1, r.invocationCount);
2438 >            assertEquals(null, h1.join());
2439 >            rs[1].assertInvoked();
2440          } catch (CompletionException ok) {
2441 <            checkCompletedWithWrappedCFException(h, ex);
2442 <            assertEquals(0, r.invocationCount);
2441 >            checkCompletedWithWrappedException(h1, ex);
2442 >            rs[1].assertNotInvoked();
2443 >        }
2444 >        try {
2445 >            assertEquals(null, h2.join());
2446 >            rs[2].assertInvoked();
2447 >        } catch (CompletionException ok) {
2448 >            checkCompletedWithWrappedException(h2, ex);
2449 >            rs[2].assertNotInvoked();
2450 >        }
2451 >        try {
2452 >            assertEquals(null, h3.join());
2453 >            rs[3].assertInvoked();
2454 >        } catch (CompletionException ok) {
2455 >            checkCompletedWithWrappedException(h3, ex);
2456 >            rs[3].assertNotInvoked();
2457          }
2458  
2702        checkCompletedWithWrappedCFException(f, ex);
2703        checkCompletedNormally(g, v1);
2704    }}
2705
2706    /**
2707     * runAfterEither result completes exceptionally if action does
2708     */
2709    public void testRunAfterEither_actionFailed1() {
2710        for (ExecutionMode m : ExecutionMode.values())
2711        for (Integer v1 : new Integer[] { 1, null })
2712        for (Integer v2 : new Integer[] { 2, null })
2713    {
2714        final CompletableFuture<Integer> f = new CompletableFuture<>();
2715        final CompletableFuture<Integer> g = new CompletableFuture<>();
2716        final FailingNoop r = new FailingNoop();
2717        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2718
2719        f.complete(v1);
2720        checkCompletedWithWrappedCFException(h);
2721        g.complete(v2);
2722        checkCompletedNormally(f, v1);
2723        checkCompletedNormally(g, v2);
2724    }}
2725
2726    public void testRunAfterEither_actionFailed2() {
2727        for (ExecutionMode m : ExecutionMode.values())
2728        for (Integer v1 : new Integer[] { 1, null })
2729        for (Integer v2 : new Integer[] { 2, null })
2730    {
2731        final CompletableFuture<Integer> f = new CompletableFuture<>();
2732        final CompletableFuture<Integer> g = new CompletableFuture<>();
2733        final FailingNoop r = new FailingNoop();
2734        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2735
2736        g.complete(v2);
2737        checkCompletedWithWrappedCFException(h);
2738        f.complete(v1);
2459          checkCompletedNormally(f, v1);
2460 <        checkCompletedNormally(g, v2);
2460 >        checkCompletedExceptionally(g, ex);
2461      }}
2462  
2463      /**
2464       * runAfterEither result completes exceptionally if either source cancelled
2465       */
2466 <    public void testRunAfterEither_sourceCancelled1() {
2466 >    public void testRunAfterEither_sourceCancelled() {
2467          for (ExecutionMode m : ExecutionMode.values())
2468          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2469          for (Integer v1 : new Integer[] { 1, null })
2470      {
2471          final CompletableFuture<Integer> f = new CompletableFuture<>();
2472          final CompletableFuture<Integer> g = new CompletableFuture<>();
2473 <        final Noop r = new Noop();
2474 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2473 >        final Noop[] rs = new Noop[6];
2474 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2475  
2476 <        assertTrue(f.cancel(mayInterruptIfRunning));
2477 <        checkCompletedWithWrappedCancellationException(h);
2478 <        g.complete(v1);
2476 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2477 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2478 >        checkIncomplete(h0);
2479 >        checkIncomplete(h1);
2480 >        rs[0].assertNotInvoked();
2481 >        rs[1].assertNotInvoked();
2482 >        f.cancel(mayInterruptIfRunning);
2483 >        checkCompletedWithWrappedCancellationException(h0);
2484 >        checkCompletedWithWrappedCancellationException(h1);
2485 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2486 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2487 >        checkCompletedWithWrappedCancellationException(h2);
2488 >        checkCompletedWithWrappedCancellationException(h3);
2489  
2490 <        checkCancelled(f);
2761 <        assertEquals(0, r.invocationCount);
2762 <        checkCompletedNormally(g, v1);
2763 <        checkCompletedWithWrappedCancellationException(h);
2764 <    }}
2765 <
2766 <    public void testRunAfterEither_sourceCancelled2() {
2767 <        for (ExecutionMode m : ExecutionMode.values())
2768 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2769 <        for (Integer v1 : new Integer[] { 1, null })
2770 <    {
2771 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2772 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2773 <        final Noop r = new Noop();
2774 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2775 <
2776 <        assertTrue(g.cancel(mayInterruptIfRunning));
2777 <        checkCompletedWithWrappedCancellationException(h);
2778 <        f.complete(v1);
2779 <
2780 <        checkCancelled(g);
2781 <        assertEquals(0, r.invocationCount);
2782 <        checkCompletedNormally(f, v1);
2783 <        checkCompletedWithWrappedCancellationException(h);
2784 <    }}
2785 <
2786 <    public void testRunAfterEither_sourceCancelled3() {
2787 <        for (ExecutionMode m : ExecutionMode.values())
2788 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2789 <        for (Integer v1 : new Integer[] { 1, null })
2790 <    {
2791 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2792 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2793 <        final Noop r = new Noop();
2794 <
2795 <        assertTrue(g.cancel(mayInterruptIfRunning));
2796 <        f.complete(v1);
2797 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2490 >        g.complete(v1);
2491  
2492 <        // unspecified behavior
2493 <        Integer v;
2492 >        // unspecified behavior - both source completions available
2493 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2494 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2495          try {
2496 <            assertNull(h.join());
2497 <            assertEquals(1, r.invocationCount);
2496 >            assertNull(h4.join());
2497 >            rs[4].assertInvoked();
2498          } catch (CompletionException ok) {
2499 <            checkCompletedWithWrappedCancellationException(h);
2500 <            assertEquals(0, r.invocationCount);
2499 >            checkCompletedWithWrappedCancellationException(h4);
2500 >            rs[4].assertNotInvoked();
2501          }
2808
2809        checkCancelled(g);
2810        checkCompletedNormally(f, v1);
2811    }}
2812
2813    public void testRunAfterEither_sourceCancelled4() {
2814        for (ExecutionMode m : ExecutionMode.values())
2815        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2816        for (Integer v1 : new Integer[] { 1, null })
2817    {
2818        final CompletableFuture<Integer> f = new CompletableFuture<>();
2819        final CompletableFuture<Integer> g = new CompletableFuture<>();
2820        final Noop r = new Noop();
2821
2822        assertTrue(f.cancel(mayInterruptIfRunning));
2823        g.complete(v1);
2824        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2825
2826        // unspecified behavior
2827        Integer v;
2502          try {
2503 <            assertNull(h.join());
2504 <            assertEquals(1, r.invocationCount);
2503 >            assertNull(h5.join());
2504 >            rs[5].assertInvoked();
2505          } catch (CompletionException ok) {
2506 <            checkCompletedWithWrappedCancellationException(h);
2507 <            assertEquals(0, r.invocationCount);
2506 >            checkCompletedWithWrappedCancellationException(h5);
2507 >            rs[5].assertNotInvoked();
2508          }
2509  
2510          checkCancelled(f);
2511          checkCompletedNormally(g, v1);
2512 +        checkCompletedWithWrappedCancellationException(h0);
2513 +        checkCompletedWithWrappedCancellationException(h1);
2514 +        checkCompletedWithWrappedCancellationException(h2);
2515 +        checkCompletedWithWrappedCancellationException(h3);
2516 +        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2517      }}
2518  
2519      /**
2520 <     * thenCompose result completes normally after normal completion of source
2520 >     * runAfterEither result completes exceptionally if action does
2521       */
2522 <    public void testThenCompose_normalCompletion1() {
2522 >    public void testRunAfterEither_actionFailed() {
2523          for (ExecutionMode m : ExecutionMode.values())
2524          for (Integer v1 : new Integer[] { 1, null })
2525 +        for (Integer v2 : new Integer[] { 2, null })
2526      {
2527          final CompletableFuture<Integer> f = new CompletableFuture<>();
2528 <        final CompletableFutureInc r = new CompletableFutureInc();
2529 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2528 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2529 >        final FailingRunnable[] rs = new FailingRunnable[6];
2530 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
2531 >
2532 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2533 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2534          f.complete(v1);
2535 <        checkCompletedNormally(g, inc(v1));
2535 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2536 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2537 >        checkCompletedWithWrappedCFException(h0);
2538 >        checkCompletedWithWrappedCFException(h1);
2539 >        checkCompletedWithWrappedCFException(h2);
2540 >        checkCompletedWithWrappedCFException(h3);
2541 >        for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2542 >        g.complete(v2);
2543 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2544 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2545 >        checkCompletedWithWrappedCFException(h4);
2546 >        checkCompletedWithWrappedCFException(h5);
2547 >
2548          checkCompletedNormally(f, v1);
2549 <        assertEquals(1, r.invocationCount);
2549 >        checkCompletedNormally(g, v2);
2550 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2551      }}
2552  
2553 <    public void testThenCompose_normalCompletion2() {
2553 >    /**
2554 >     * thenCompose result completes normally after normal completion of source
2555 >     */
2556 >    public void testThenCompose_normalCompletion() {
2557          for (ExecutionMode m : ExecutionMode.values())
2558 +        for (boolean createIncomplete : new boolean[] { true, false })
2559          for (Integer v1 : new Integer[] { 1, null })
2560      {
2561          final CompletableFuture<Integer> f = new CompletableFuture<>();
2562 <        final CompletableFutureInc r = new CompletableFutureInc();
2563 <        f.complete(v1);
2564 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2562 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2563 >        if (!createIncomplete) f.complete(v1);
2564 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2565 >        if (createIncomplete) f.complete(v1);
2566 >
2567          checkCompletedNormally(g, inc(v1));
2568          checkCompletedNormally(f, v1);
2569 <        assertEquals(1, r.invocationCount);
2569 >        r.assertValue(v1);
2570      }}
2571  
2572      /**
2573       * thenCompose result completes exceptionally after exceptional
2574       * completion of source
2575       */
2576 <    public void testThenCompose_exceptionalCompletion1() {
2576 >    public void testThenCompose_exceptionalCompletion() {
2577          for (ExecutionMode m : ExecutionMode.values())
2578 +        for (boolean createIncomplete : new boolean[] { true, false })
2579      {
2580          final CFException ex = new CFException();
2581 <        final CompletableFutureInc r = new CompletableFutureInc();
2581 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2582          final CompletableFuture<Integer> f = new CompletableFuture<>();
2583 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2584 <        f.completeExceptionally(ex);
2585 <        checkCompletedWithWrappedCFException(g, ex);
2882 <        checkCompletedWithWrappedCFException(f, ex);
2883 <    }}
2583 >        if (!createIncomplete) f.completeExceptionally(ex);
2584 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2585 >        if (createIncomplete) f.completeExceptionally(ex);
2586  
2587 <    public void testThenCompose_exceptionalCompletion2() {
2588 <        for (ExecutionMode m : ExecutionMode.values())
2589 <    {
2888 <        final CFException ex = new CFException();
2889 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2890 <        f.completeExceptionally(ex);
2891 <        final CompletableFutureInc r = new CompletableFutureInc();
2892 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2893 <        checkCompletedWithWrappedCFException(g, ex);
2894 <        checkCompletedWithWrappedCFException(f, ex);
2587 >        checkCompletedWithWrappedException(g, ex);
2588 >        checkCompletedExceptionally(f, ex);
2589 >        r.assertNotInvoked();
2590      }}
2591  
2592      /**
2593       * thenCompose result completes exceptionally if action does
2594       */
2595 <    public void testThenCompose_actionFailed1() {
2595 >    public void testThenCompose_actionFailed() {
2596          for (ExecutionMode m : ExecutionMode.values())
2597 +        for (boolean createIncomplete : new boolean[] { true, false })
2598          for (Integer v1 : new Integer[] { 1, null })
2599      {
2600          final CompletableFuture<Integer> f = new CompletableFuture<>();
2601          final FailingCompletableFutureFunction r
2602 <            = new FailingCompletableFutureFunction();
2603 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2604 <        f.complete(v1);
2605 <        checkCompletedWithWrappedCFException(g);
2910 <        checkCompletedNormally(f, v1);
2911 <    }}
2602 >            = new FailingCompletableFutureFunction(m);
2603 >        if (!createIncomplete) f.complete(v1);
2604 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2605 >        if (createIncomplete) f.complete(v1);
2606  
2913    public void testThenCompose_actionFailed2() {
2914        for (ExecutionMode m : ExecutionMode.values())
2915        for (Integer v1 : new Integer[] { 1, null })
2916    {
2917        final CompletableFuture<Integer> f = new CompletableFuture<>();
2918        f.complete(v1);
2919        final FailingCompletableFutureFunction r
2920            = new FailingCompletableFutureFunction();
2921        final CompletableFuture<Integer> g = f.thenCompose(r);
2607          checkCompletedWithWrappedCFException(g);
2608          checkCompletedNormally(f, v1);
2609      }}
# Line 2926 | Line 2611 | public class CompletableFutureTest exten
2611      /**
2612       * thenCompose result completes exceptionally if source cancelled
2613       */
2614 <    public void testThenCompose_sourceCancelled1() {
2614 >    public void testThenCompose_sourceCancelled() {
2615          for (ExecutionMode m : ExecutionMode.values())
2616 +        for (boolean createIncomplete : new boolean[] { true, false })
2617          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2618      {
2619          final CompletableFuture<Integer> f = new CompletableFuture<>();
2620 <        final CompletableFutureInc r = new CompletableFutureInc();
2621 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2622 <        assertTrue(f.cancel(mayInterruptIfRunning));
2623 <        checkCompletedWithWrappedCancellationException(g);
2624 <        checkCancelled(f);
2625 <    }}
2620 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2621 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2622 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2623 >        if (createIncomplete) {
2624 >            checkIncomplete(g);
2625 >            assertTrue(f.cancel(mayInterruptIfRunning));
2626 >        }
2627  
2941    public void testThenCompose_sourceCancelled2() {
2942        for (ExecutionMode m : ExecutionMode.values())
2943        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2944    {
2945        final CompletableFuture<Integer> f = new CompletableFuture<>();
2946        assertTrue(f.cancel(mayInterruptIfRunning));
2947        final CompletableFutureInc r = new CompletableFutureInc();
2948        final CompletableFuture<Integer> g = f.thenCompose(r);
2628          checkCompletedWithWrappedCancellationException(g);
2629          checkCancelled(f);
2630      }}
2631  
2953    // asyncs
2954
2955    /**
2956     * thenRunAsync result completes normally after normal completion of source
2957     */
2958    public void testThenRunAsync() {
2959        CompletableFuture<Integer> f = new CompletableFuture<>();
2960        Noop r = new Noop();
2961        CompletableFuture<Void> g = f.thenRunAsync(r);
2962        f.complete(null);
2963        checkCompletedNormally(g, null);
2964
2965        // reordered version
2966        f = new CompletableFuture<>();
2967        f.complete(null);
2968        r = new Noop();
2969        g = f.thenRunAsync(r);
2970        checkCompletedNormally(g, null);
2971    }
2972
2973    /**
2974     * thenRunAsync result completes exceptionally after exceptional
2975     * completion of source
2976     */
2977    public void testThenRunAsync2() {
2978        CompletableFuture<Integer> f = new CompletableFuture<>();
2979        Noop r = new Noop();
2980        CompletableFuture<Void> g = f.thenRunAsync(r);
2981        f.completeExceptionally(new CFException());
2982        try {
2983            g.join();
2984            shouldThrow();
2985        } catch (CompletionException success) {}
2986        checkCompletedWithWrappedCFException(g);
2987    }
2988
2989    /**
2990     * thenRunAsync result completes exceptionally if action does
2991     */
2992    public void testThenRunAsync3() {
2993        CompletableFuture<Integer> f = new CompletableFuture<>();
2994        FailingNoop r = new FailingNoop();
2995        CompletableFuture<Void> g = f.thenRunAsync(r);
2996        f.complete(null);
2997        checkCompletedWithWrappedCFException(g);
2998    }
2999
3000    /**
3001     * thenRunAsync result completes exceptionally if source cancelled
3002     */
3003    public void testThenRunAsync4() {
3004        CompletableFuture<Integer> f = new CompletableFuture<>();
3005        Noop r = new Noop();
3006        CompletableFuture<Void> g = f.thenRunAsync(r);
3007        assertTrue(f.cancel(true));
3008        checkCompletedWithWrappedCancellationException(g);
3009    }
3010
3011    /**
3012     * thenApplyAsync result completes normally after normal completion of source
3013     */
3014    public void testThenApplyAsync() {
3015        CompletableFuture<Integer> f = new CompletableFuture<>();
3016        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
3017        f.complete(one);
3018        checkCompletedNormally(g, two);
3019    }
3020
3021    /**
3022     * thenApplyAsync result completes exceptionally after exceptional
3023     * completion of source
3024     */
3025    public void testThenApplyAsync2() {
3026        CompletableFuture<Integer> f = new CompletableFuture<>();
3027        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
3028        f.completeExceptionally(new CFException());
3029        checkCompletedWithWrappedCFException(g);
3030    }
3031
3032    /**
3033     * thenApplyAsync result completes exceptionally if action does
3034     */
3035    public void testThenApplyAsync3() {
3036        CompletableFuture<Integer> f = new CompletableFuture<>();
3037        FailingFunction r = new FailingFunction();
3038        CompletableFuture<Integer> g = f.thenApplyAsync(r);
3039        f.complete(null);
3040        checkCompletedWithWrappedCFException(g);
3041    }
3042
3043    /**
3044     * thenApplyAsync result completes exceptionally if source cancelled
3045     */
3046    public void testThenApplyAsync4() {
3047        CompletableFuture<Integer> f = new CompletableFuture<>();
3048        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
3049        assertTrue(f.cancel(true));
3050        checkCompletedWithWrappedCancellationException(g);
3051    }
3052
3053    /**
3054     * thenAcceptAsync result completes normally after normal
3055     * completion of source
3056     */
3057    public void testThenAcceptAsync() {
3058        CompletableFuture<Integer> f = new CompletableFuture<>();
3059        IncAction r = new IncAction();
3060        CompletableFuture<Void> g = f.thenAcceptAsync(r);
3061        f.complete(one);
3062        checkCompletedNormally(g, null);
3063        assertEquals(r.value, (Integer) 2);
3064    }
3065
3066    /**
3067     * thenAcceptAsync result completes exceptionally after exceptional
3068     * completion of source
3069     */
3070    public void testThenAcceptAsync2() {
3071        CompletableFuture<Integer> f = new CompletableFuture<>();
3072        IncAction r = new IncAction();
3073        CompletableFuture<Void> g = f.thenAcceptAsync(r);
3074        f.completeExceptionally(new CFException());
3075        checkCompletedWithWrappedCFException(g);
3076    }
3077
3078    /**
3079     * thenAcceptAsync result completes exceptionally if action does
3080     */
3081    public void testThenAcceptAsync3() {
3082        CompletableFuture<Integer> f = new CompletableFuture<>();
3083        FailingConsumer r = new FailingConsumer();
3084        CompletableFuture<Void> g = f.thenAcceptAsync(r);
3085        f.complete(null);
3086        checkCompletedWithWrappedCFException(g);
3087    }
3088
3089    /**
3090     * thenAcceptAsync result completes exceptionally if source cancelled
3091     */
3092    public void testThenAcceptAsync4() {
3093        CompletableFuture<Integer> f = new CompletableFuture<>();
3094        IncAction r = new IncAction();
3095        CompletableFuture<Void> g = f.thenAcceptAsync(r);
3096        assertTrue(f.cancel(true));
3097        checkCompletedWithWrappedCancellationException(g);
3098    }
3099
3100    // async with explicit executors
3101
3102    /**
3103     * thenRunAsync result completes normally after normal completion of source
3104     */
3105    public void testThenRunAsyncE() {
3106        CompletableFuture<Integer> f = new CompletableFuture<>();
3107        Noop r = new Noop();
3108        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3109        f.complete(null);
3110        checkCompletedNormally(g, null);
3111
3112        // reordered version
3113        f = new CompletableFuture<>();
3114        f.complete(null);
3115        r = new Noop();
3116        g = f.thenRunAsync(r, new ThreadExecutor());
3117        checkCompletedNormally(g, null);
3118    }
3119
3120    /**
3121     * thenRunAsync result completes exceptionally after exceptional
3122     * completion of source
3123     */
3124    public void testThenRunAsync2E() {
3125        CompletableFuture<Integer> f = new CompletableFuture<>();
3126        Noop r = new Noop();
3127        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3128        f.completeExceptionally(new CFException());
3129        try {
3130            g.join();
3131            shouldThrow();
3132        } catch (CompletionException success) {}
3133        checkCompletedWithWrappedCFException(g);
3134    }
3135
3136    /**
3137     * thenRunAsync result completes exceptionally if action does
3138     */
3139    public void testThenRunAsync3E() {
3140        CompletableFuture<Integer> f = new CompletableFuture<>();
3141        FailingNoop r = new FailingNoop();
3142        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3143        f.complete(null);
3144        checkCompletedWithWrappedCFException(g);
3145    }
3146
3147    /**
3148     * thenRunAsync result completes exceptionally if source cancelled
3149     */
3150    public void testThenRunAsync4E() {
3151        CompletableFuture<Integer> f = new CompletableFuture<>();
3152        Noop r = new Noop();
3153        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3154        assertTrue(f.cancel(true));
3155        checkCompletedWithWrappedCancellationException(g);
3156    }
3157
3158    /**
3159     * thenApplyAsync result completes normally after normal completion of source
3160     */
3161    public void testThenApplyAsyncE() {
3162        CompletableFuture<Integer> f = new CompletableFuture<>();
3163        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
3164        f.complete(one);
3165        checkCompletedNormally(g, two);
3166    }
3167
3168    /**
3169     * thenApplyAsync result completes exceptionally after exceptional
3170     * completion of source
3171     */
3172    public void testThenApplyAsync2E() {
3173        CompletableFuture<Integer> f = new CompletableFuture<>();
3174        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
3175        f.completeExceptionally(new CFException());
3176        checkCompletedWithWrappedCFException(g);
3177    }
3178
3179    /**
3180     * thenApplyAsync result completes exceptionally if action does
3181     */
3182    public void testThenApplyAsync3E() {
3183        CompletableFuture<Integer> f = new CompletableFuture<>();
3184        FailingFunction r = new FailingFunction();
3185        CompletableFuture<Integer> g = f.thenApplyAsync(r, new ThreadExecutor());
3186        f.complete(null);
3187        checkCompletedWithWrappedCFException(g);
3188    }
3189
3190    /**
3191     * thenApplyAsync result completes exceptionally if source cancelled
3192     */
3193    public void testThenApplyAsync4E() {
3194        CompletableFuture<Integer> f = new CompletableFuture<>();
3195        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
3196        assertTrue(f.cancel(true));
3197        checkCompletedWithWrappedCancellationException(g);
3198    }
3199
3200    /**
3201     * thenAcceptAsync result completes normally after normal
3202     * completion of source
3203     */
3204    public void testThenAcceptAsyncE() {
3205        CompletableFuture<Integer> f = new CompletableFuture<>();
3206        IncAction r = new IncAction();
3207        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3208        f.complete(one);
3209        checkCompletedNormally(g, null);
3210        assertEquals(r.value, (Integer) 2);
3211    }
3212
3213    /**
3214     * thenAcceptAsync result completes exceptionally after exceptional
3215     * completion of source
3216     */
3217    public void testThenAcceptAsync2E() {
3218        CompletableFuture<Integer> f = new CompletableFuture<>();
3219        IncAction r = new IncAction();
3220        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3221        f.completeExceptionally(new CFException());
3222        checkCompletedWithWrappedCFException(g);
3223    }
3224
3225    /**
3226     * thenAcceptAsync result completes exceptionally if action does
3227     */
3228    public void testThenAcceptAsync3E() {
3229        CompletableFuture<Integer> f = new CompletableFuture<>();
3230        FailingConsumer r = new FailingConsumer();
3231        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3232        f.complete(null);
3233        checkCompletedWithWrappedCFException(g);
3234    }
3235
3236    /**
3237     * thenAcceptAsync result completes exceptionally if source cancelled
3238     */
3239    public void testThenAcceptAsync4E() {
3240        CompletableFuture<Integer> f = new CompletableFuture<>();
3241        IncAction r = new IncAction();
3242        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3243        assertTrue(f.cancel(true));
3244        checkCompletedWithWrappedCancellationException(g);
3245    }
3246
2632      // other static methods
2633  
2634      /**
# Line 3261 | Line 2646 | public class CompletableFutureTest exten
2646       */
2647      public void testAllOf_normal() throws Exception {
2648          for (int k = 1; k < 20; ++k) {
2649 <            CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2649 >            CompletableFuture<Integer>[] fs
2650 >                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2651              for (int i = 0; i < k; ++i)
2652                  fs[i] = new CompletableFuture<>();
2653              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
# Line 3275 | Line 2661 | public class CompletableFutureTest exten
2661          }
2662      }
2663  
2664 +    public void testAllOf_backwards() throws Exception {
2665 +        for (int k = 1; k < 20; ++k) {
2666 +            CompletableFuture<Integer>[] fs
2667 +                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2668 +            for (int i = 0; i < k; ++i)
2669 +                fs[i] = new CompletableFuture<>();
2670 +            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
2671 +            for (int i = k - 1; i >= 0; i--) {
2672 +                checkIncomplete(f);
2673 +                checkIncomplete(CompletableFuture.allOf(fs));
2674 +                fs[i].complete(one);
2675 +            }
2676 +            checkCompletedNormally(f, null);
2677 +            checkCompletedNormally(CompletableFuture.allOf(fs), null);
2678 +        }
2679 +    }
2680 +
2681      /**
2682       * anyOf(no component futures) returns an incomplete future
2683       */
# Line 3333 | Line 2736 | public class CompletableFutureTest exten
2736          Runnable[] throwingActions = {
2737              () -> CompletableFuture.supplyAsync(null),
2738              () -> CompletableFuture.supplyAsync(null, exec),
2739 <            () -> CompletableFuture.supplyAsync(supplyOne, null),
2739 >            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null),
2740  
2741              () -> CompletableFuture.runAsync(null),
2742              () -> CompletableFuture.runAsync(null, exec),
# Line 3406 | Line 2809 | public class CompletableFutureTest exten
2809  
2810              () -> f.thenCompose(null),
2811              () -> f.thenComposeAsync(null),
2812 <            () -> f.thenComposeAsync(new CompletableFutureInc(), null),
2812 >            () -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null),
2813              () -> f.thenComposeAsync(null, exec),
2814  
2815              () -> f.exceptionally(null),
# Line 3453 | Line 2856 | public class CompletableFutureTest exten
2856          final CompletableFuture<Integer> g = m.whenComplete
2857              (f,
2858               (Integer x, Throwable t) -> {
2859 +                m.checkExecutionMode();
2860                  threadAssertSame(x, v1);
2861                  threadAssertNull(t);
2862                  a.getAndIncrement();
# Line 3480 | Line 2884 | public class CompletableFutureTest exten
2884          final CompletableFuture<Integer> g = m.whenComplete
2885              (f,
2886               (Integer x, Throwable t) -> {
2887 +                m.checkExecutionMode();
2888                  threadAssertNull(x);
2889                  threadAssertSame(t, ex);
2890                  a.getAndIncrement();
2891              });
2892          if (createIncomplete) f.completeExceptionally(ex);
2893 <        checkCompletedWithWrappedCFException(f, ex);
2894 <        checkCompletedWithWrappedCFException(g, ex);
2893 >        checkCompletedExceptionally(f, ex);
2894 >        checkCompletedWithWrappedException(g, ex);
2895          assertEquals(1, a.get());
2896      }}
2897  
# Line 3505 | Line 2910 | public class CompletableFutureTest exten
2910          final CompletableFuture<Integer> g = m.whenComplete
2911              (f,
2912               (Integer x, Throwable t) -> {
2913 +                m.checkExecutionMode();
2914                  threadAssertNull(x);
2915                  threadAssertTrue(t instanceof CancellationException);
2916                  a.getAndIncrement();
2917              });
2918          if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2919  
3514        //try { g.join(); } catch (Throwable t) { throw new Error(t); }
2920          checkCompletedWithWrappedCancellationException(g);
2921          checkCancelled(f);
2922          assertEquals(1, a.get());
# Line 3533 | Line 2938 | public class CompletableFutureTest exten
2938          final CompletableFuture<Integer> g = m.whenComplete
2939              (f,
2940               (Integer x, Throwable t) -> {
2941 +                m.checkExecutionMode();
2942                  threadAssertSame(x, v1);
2943                  threadAssertNull(t);
2944                  a.getAndIncrement();
# Line 3540 | Line 2946 | public class CompletableFutureTest exten
2946              });
2947          if (createIncomplete) f.complete(v1);
2948          checkCompletedNormally(f, v1);
2949 <        checkCompletedWithWrappedCFException(g, ex);
2949 >        checkCompletedWithWrappedException(g, ex);
2950          assertEquals(1, a.get());
2951      }}
2952  
# Line 3563 | Line 2969 | public class CompletableFutureTest exten
2969          final CompletableFuture<Integer> g = m.whenComplete
2970              (f,
2971               (Integer x, Throwable t) -> {
2972 +                m.checkExecutionMode();
2973                  threadAssertSame(t, ex1);
2974                  threadAssertNull(x);
2975                  a.getAndIncrement();
# Line 3570 | Line 2977 | public class CompletableFutureTest exten
2977              });
2978          if (createIncomplete) f.completeExceptionally(ex1);
2979  
2980 <        checkCompletedWithWrappedCFException(f, ex1);
2981 <        checkCompletedWithWrappedCFException(g, ex1);
2980 >        checkCompletedExceptionally(f, ex1);
2981 >        checkCompletedWithWrappedException(g, ex1);
2982          assertEquals(1, a.get());
2983      }}
2984  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines