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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines