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.45 by jsr166, Mon Jun 2 06:07:34 2014 UTC vs.
Revision 1.101 by jsr166, Sun Feb 22 04:34:44 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines