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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines