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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines