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.28 by jsr166, Sun Jul 14 16:34:49 2013 UTC vs.
Revision 1.101 by jsr166, Sun Feb 22 04:34:44 2015 UTC

# Line 5 | Line 5
5   * http://creativecommons.org/publicdomain/zero/1.0/
6   */
7  
8 < import junit.framework.*;
8 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
9 > import static java.util.concurrent.TimeUnit.SECONDS;
10 >
11 > import java.util.ArrayList;
12 > import java.util.List;
13 > import java.util.Objects;
14   import java.util.concurrent.Callable;
10 import java.util.concurrent.Executor;
11 import java.util.concurrent.ExecutorService;
12 import java.util.concurrent.Executors;
15   import java.util.concurrent.CancellationException;
14 import java.util.concurrent.CountDownLatch;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.Future;
16   import java.util.concurrent.CompletableFuture;
17   import java.util.concurrent.CompletionException;
18 + import java.util.concurrent.CompletionStage;
19 + import java.util.concurrent.ExecutionException;
20 + import java.util.concurrent.Executor;
21 + import java.util.concurrent.ForkJoinPool;
22 + import java.util.concurrent.ForkJoinTask;
23   import java.util.concurrent.TimeoutException;
24   import java.util.concurrent.atomic.AtomicInteger;
21 import static java.util.concurrent.TimeUnit.MILLISECONDS;
22 import static java.util.concurrent.TimeUnit.SECONDS;
23 import java.util.*;
24 import java.util.function.Supplier;
25 import java.util.function.Consumer;
25   import java.util.function.BiConsumer;
27 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 54 | 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));
59 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
61 >        checkTimedGet(f, value);
62 >
63          try {
64              assertEquals(value, f.join());
65          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 73 | 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 102 | Line 109 | public class CompletableFutureTest exten
109          assertTrue(f.toString().contains("[Completed exceptionally]"));
110      }
111  
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(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();
127 +        } catch (CompletionException success) {
128 +            assertSame(ex, success.getCause());
129 +        }
130 +        try {
131 +            f.getNow(null);
132 +            shouldThrow();
133 +        } catch (CompletionException success) {
134 +            assertSame(ex, success.getCause());
135 +        }
136 +        try {
137 +            f.get();
138 +            shouldThrow();
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 128 | 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 172 | 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 184 | 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 194 | Line 269 | public class CompletableFutureTest exten
269       * methods isDone, isCancelled, join, get, and getNow
270       */
271      public void testCancel() {
272 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
273 +    {
274          CompletableFuture<Integer> f = new CompletableFuture<>();
275          checkIncomplete(f);
276 <        assertTrue(f.cancel(true));
276 >        assertTrue(f.cancel(mayInterruptIfRunning));
277 >        assertTrue(f.cancel(mayInterruptIfRunning));
278 >        assertTrue(f.cancel(!mayInterruptIfRunning));
279          checkCancelled(f);
280 <    }
280 >    }}
281  
282      /**
283       * obtrudeValue forces completion with given value
# Line 206 | Line 285 | public class CompletableFutureTest exten
285      public void testObtrudeValue() {
286          CompletableFuture<Integer> f = new CompletableFuture<>();
287          checkIncomplete(f);
288 <        f.complete(one);
288 >        assertTrue(f.complete(one));
289          checkCompletedNormally(f, one);
290          f.obtrudeValue(three);
291          checkCompletedNormally(f, three);
# Line 215 | Line 294 | public class CompletableFutureTest exten
294          f = new CompletableFuture<>();
295          f.obtrudeValue(three);
296          checkCompletedNormally(f, three);
297 +        f.obtrudeValue(null);
298 +        checkCompletedNormally(f, null);
299          f = new CompletableFuture<>();
300          f.completeExceptionally(new CFException());
301          f.obtrudeValue(four);
# Line 225 | Line 306 | public class CompletableFutureTest exten
306       * obtrudeException forces completion with given exception
307       */
308      public void testObtrudeException() {
309 <        CompletableFuture<Integer> f = new CompletableFuture<>();
310 <        checkIncomplete(f);
311 <        f.complete(one);
312 <        checkCompletedNormally(f, one);
313 <        f.obtrudeException(new CFException());
233 <        checkCompletedWithWrappedCFException(f);
309 >        for (Integer v1 : new Integer[] { 1, null })
310 >    {
311 >        CFException ex;
312 >        CompletableFuture<Integer> f;
313 >
314          f = new CompletableFuture<>();
315 <        f.obtrudeException(new CFException());
316 <        checkCompletedWithWrappedCFException(f);
315 >        assertTrue(f.complete(v1));
316 >        for (int i = 0; i < 2; i++) {
317 >            f.obtrudeException(ex = new CFException());
318 >            checkCompletedExceptionally(f, ex);
319 >        }
320 >
321          f = new CompletableFuture<>();
322 <        f.completeExceptionally(new CFException());
323 <        f.obtrudeValue(four);
324 <        checkCompletedNormally(f, four);
325 <        f.obtrudeException(new CFException());
326 <        checkCompletedWithWrappedCFException(f);
327 <    }
322 >        for (int i = 0; i < 2; i++) {
323 >            f.obtrudeException(ex = new CFException());
324 >            checkCompletedExceptionally(f, ex);
325 >        }
326 >
327 >        f = new CompletableFuture<>();
328 >        f.completeExceptionally(ex = new CFException());
329 >        f.obtrudeValue(v1);
330 >        checkCompletedNormally(f, v1);
331 >        f.obtrudeException(ex = new CFException());
332 >        checkCompletedExceptionally(f, ex);
333 >        f.completeExceptionally(new CFException());
334 >        checkCompletedExceptionally(f, ex);
335 >        assertFalse(f.complete(v1));
336 >        checkCompletedExceptionally(f, ex);
337 >    }}
338  
339      /**
340       * getNumberOfDependents returns number of dependent tasks
341       */
342      public void testGetNumberOfDependents() {
343 <        CompletableFuture<Integer> f = new CompletableFuture<>();
344 <        assertEquals(f.getNumberOfDependents(), 0);
345 <        CompletableFuture g = f.thenRun(new Noop());
346 <        assertEquals(f.getNumberOfDependents(), 1);
347 <        assertEquals(g.getNumberOfDependents(), 0);
348 <        CompletableFuture h = f.thenRun(new Noop());
349 <        assertEquals(f.getNumberOfDependents(), 2);
350 <        f.complete(1);
343 >        for (ExecutionMode m : ExecutionMode.values())
344 >        for (Integer v1 : new Integer[] { 1, null })
345 >    {
346 >        CompletableFuture<Integer> f = new CompletableFuture<>();
347 >        assertEquals(0, f.getNumberOfDependents());
348 >        final CompletableFuture<Void> g = m.thenRun(f, new Noop(m));
349 >        assertEquals(1, f.getNumberOfDependents());
350 >        assertEquals(0, g.getNumberOfDependents());
351 >        final CompletableFuture<Void> h = m.thenRun(f, new Noop(m));
352 >        assertEquals(2, f.getNumberOfDependents());
353 >        assertEquals(0, h.getNumberOfDependents());
354 >        assertTrue(f.complete(v1));
355          checkCompletedNormally(g, null);
356 <        assertEquals(f.getNumberOfDependents(), 0);
357 <        assertEquals(g.getNumberOfDependents(), 0);
358 <    }
356 >        checkCompletedNormally(h, null);
357 >        assertEquals(0, f.getNumberOfDependents());
358 >        assertEquals(0, g.getNumberOfDependents());
359 >        assertEquals(0, h.getNumberOfDependents());
360 >    }}
361  
362      /**
363       * toString indicates current completion state
# Line 268 | Line 368 | public class CompletableFutureTest exten
368          f = new CompletableFuture<String>();
369          assertTrue(f.toString().contains("[Not completed]"));
370  
371 <        f.complete("foo");
371 >        assertTrue(f.complete("foo"));
372          assertTrue(f.toString().contains("[Completed normally]"));
373  
374          f = new CompletableFuture<String>();
375 <        f.completeExceptionally(new IndexOutOfBoundsException());
375 >        assertTrue(f.completeExceptionally(new IndexOutOfBoundsException()));
376          assertTrue(f.toString().contains("[Completed exceptionally]"));
377 +
378 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
379 +            f = new CompletableFuture<String>();
380 +            assertTrue(f.cancel(mayInterruptIfRunning));
381 +            assertTrue(f.toString().contains("[Completed exceptionally]"));
382 +        }
383      }
384  
385      /**
# Line 284 | Line 390 | public class CompletableFutureTest exten
390          checkCompletedNormally(f, "test");
391      }
392  
393 <    // Choose non-commutative actions for better coverage
393 >    abstract class CheckedAction {
394 >        int invocationCount = 0;
395 >        final ExecutionMode m;
396 >        CheckedAction(ExecutionMode m) { this.m = m; }
397 >        void invoked() {
398 >            m.checkExecutionMode();
399 >            assertEquals(0, invocationCount++);
400 >        }
401 >        void assertNotInvoked() { assertEquals(0, invocationCount); }
402 >        void assertInvoked() { assertEquals(1, invocationCount); }
403 >    }
404  
405 <    static final Supplier<Integer> supplyOne =
406 <        () -> Integer.valueOf(1);
407 <    static final Function<Integer, Integer> inc =
408 <        (Integer x) -> Integer.valueOf(x.intValue() + 1);
409 <    static final BiFunction<Integer, Integer, Integer> subtract =
410 <        (Integer x, Integer y) -> Integer.valueOf(x.intValue() - y.intValue());
411 <    static final class IncAction implements Consumer<Integer> {
296 <        int value;
297 <        public void accept(Integer x) { value = x.intValue() + 1; }
405 >    abstract class CheckedIntegerAction extends CheckedAction {
406 >        Integer value;
407 >        CheckedIntegerAction(ExecutionMode m) { super(m); }
408 >        void assertValue(Integer expected) {
409 >            assertInvoked();
410 >            assertEquals(expected, value);
411 >        }
412      }
413 <    static final class SubtractAction implements BiConsumer<Integer, Integer> {
414 <        int value;
413 >
414 >    class IntegerSupplier extends CheckedAction
415 >        implements Supplier<Integer>
416 >    {
417 >        final Integer value;
418 >        IntegerSupplier(ExecutionMode m, Integer value) {
419 >            super(m);
420 >            this.value = value;
421 >        }
422 >        public Integer get() {
423 >            invoked();
424 >            return value;
425 >        }
426 >    }
427 >
428 >    // A function that handles and produces null values as well.
429 >    static Integer inc(Integer x) {
430 >        return (x == null) ? null : x + 1;
431 >    }
432 >
433 >    class NoopConsumer extends CheckedIntegerAction
434 >        implements Consumer<Integer>
435 >    {
436 >        NoopConsumer(ExecutionMode m) { super(m); }
437 >        public void accept(Integer x) {
438 >            invoked();
439 >            value = x;
440 >        }
441 >    }
442 >
443 >    class IncFunction extends CheckedIntegerAction
444 >        implements Function<Integer,Integer>
445 >    {
446 >        IncFunction(ExecutionMode m) { super(m); }
447 >        public Integer apply(Integer x) {
448 >            invoked();
449 >            return value = inc(x);
450 >        }
451 >    }
452 >
453 >    // Choose non-commutative actions for better coverage
454 >    // A non-commutative function that handles and produces null values as well.
455 >    static Integer subtract(Integer x, Integer y) {
456 >        return (x == null && y == null) ? null :
457 >            ((x == null) ? 42 : x.intValue())
458 >            - ((y == null) ? 99 : y.intValue());
459 >    }
460 >
461 >    class SubtractAction extends CheckedIntegerAction
462 >        implements BiConsumer<Integer, Integer>
463 >    {
464 >        SubtractAction(ExecutionMode m) { super(m); }
465          public void accept(Integer x, Integer y) {
466 <            value = x.intValue() - y.intValue();
466 >            invoked();
467 >            value = subtract(x, y);
468          }
469      }
470 <    static final class Noop implements Runnable {
471 <        boolean ran;
472 <        public void run() { ran = true; }
470 >
471 >    class SubtractFunction extends CheckedIntegerAction
472 >        implements BiFunction<Integer, Integer, Integer>
473 >    {
474 >        SubtractFunction(ExecutionMode m) { super(m); }
475 >        public Integer apply(Integer x, Integer y) {
476 >            invoked();
477 >            return value = subtract(x, y);
478 >        }
479      }
480  
481 <    static final class FailingSupplier implements Supplier<Integer> {
482 <        boolean ran;
483 <        public Integer get() { ran = true; throw new CFException(); }
481 >    class Noop extends CheckedAction implements Runnable {
482 >        Noop(ExecutionMode m) { super(m); }
483 >        public void run() {
484 >            invoked();
485 >        }
486      }
487 <    static final class FailingConsumer implements Consumer<Integer> {
488 <        boolean ran;
489 <        public void accept(Integer x) { ran = true; throw new CFException(); }
487 >
488 >    class FailingSupplier extends CheckedAction
489 >        implements Supplier<Integer>
490 >    {
491 >        FailingSupplier(ExecutionMode m) { super(m); }
492 >        public Integer get() {
493 >            invoked();
494 >            throw new CFException();
495 >        }
496 >    }
497 >
498 >    class FailingConsumer extends CheckedIntegerAction
499 >        implements Consumer<Integer>
500 >    {
501 >        FailingConsumer(ExecutionMode m) { super(m); }
502 >        public void accept(Integer x) {
503 >            invoked();
504 >            value = x;
505 >            throw new CFException();
506 >        }
507      }
508 <    static final class FailingBiConsumer implements BiConsumer<Integer, Integer> {
509 <        boolean ran;
510 <        public void accept(Integer x, Integer y) { ran = true; throw new CFException(); }
508 >
509 >    class FailingBiConsumer extends CheckedIntegerAction
510 >        implements BiConsumer<Integer, Integer>
511 >    {
512 >        FailingBiConsumer(ExecutionMode m) { super(m); }
513 >        public void accept(Integer x, Integer y) {
514 >            invoked();
515 >            value = subtract(x, y);
516 >            throw new CFException();
517 >        }
518      }
519 <    static final class FailingFunction implements Function<Integer, Integer> {
520 <        boolean ran;
521 <        public Integer apply(Integer x) { ran = true; throw new CFException(); }
519 >
520 >    class FailingFunction extends CheckedIntegerAction
521 >        implements Function<Integer, Integer>
522 >    {
523 >        FailingFunction(ExecutionMode m) { super(m); }
524 >        public Integer apply(Integer x) {
525 >            invoked();
526 >            value = x;
527 >            throw new CFException();
528 >        }
529      }
530 <    static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> {
531 <        boolean ran;
532 <        public Integer apply(Integer x, Integer y) { ran = true; throw new CFException(); }
530 >
531 >    class FailingBiFunction extends CheckedIntegerAction
532 >        implements BiFunction<Integer, Integer, Integer>
533 >    {
534 >        FailingBiFunction(ExecutionMode m) { super(m); }
535 >        public Integer apply(Integer x, Integer y) {
536 >            invoked();
537 >            value = subtract(x, y);
538 >            throw new CFException();
539 >        }
540      }
541 <    static final class FailingNoop implements Runnable {
542 <        boolean ran;
543 <        public void run() { ran = true; throw new CFException(); }
541 >
542 >    class FailingRunnable extends CheckedAction implements Runnable {
543 >        FailingRunnable(ExecutionMode m) { super(m); }
544 >        public void run() {
545 >            invoked();
546 >            throw new CFException();
547 >        }
548      }
549  
550 <    static final class CompletableFutureInc
551 <        implements Function<Integer, CompletableFuture<Integer>> {
552 <        boolean ran;
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); }
366 <    }
588 >    static final boolean defaultExecutorIsCommonPool
589 >        = ForkJoinPool.getCommonPoolParallelism() > 1;
590  
591 <    static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> {
592 <        boolean ran;
593 <        public Integer apply(Integer x, Throwable t) {
594 <            ran = true;
595 <            return (t == null) ? two : three;
596 <        }
597 <    }
591 >    /**
592 >     * Permits the testing of parallel code for the 3 different
593 >     * execution modes without copy/pasting all the test methods.
594 >     */
595 >    enum ExecutionMode {
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);
637 >            }
638 >            public <T,U> CompletableFuture<Void> thenAcceptBoth
639 >                (CompletableFuture<T> f,
640 >                 CompletionStage<? extends U> g,
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 >        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);
711 >            }
712 >            public <T,U> CompletableFuture<Void> thenAcceptBoth
713 >                (CompletableFuture<T> f,
714 >                 CompletionStage<? extends U> g,
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 >
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());
784 >            }
785 >            public <T,U> CompletableFuture<Void> thenAcceptBoth
786 >                (CompletableFuture<T> f,
787 >                 CompletionStage<? extends U> g,
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 is not invoked when source completes
861 >     * normally, and source result is propagated
862 >     */
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 >        checkCompletedNormally(g, v1);
879 >        checkCompletedNormally(f, v1);
880 >        assertEquals(0, a.get());
881 >    }}
882  
883      /**
884       * exceptionally action completes with function value on source
885 <     * exception;  otherwise with source value
885 >     * exception
886       */
887 <    public void testExceptionally() {
888 <        CompletableFuture<Integer> f = new CompletableFuture<>();
889 <        ExceptionToInteger r = new ExceptionToInteger();
890 <        CompletableFuture<Integer> g = f.exceptionally(r);
891 <        f.completeExceptionally(new CFException());
892 <        checkCompletedNormally(g, three);
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 >        checkCompletedNormally(g, v1);
905 >        assertEquals(1, a.get());
906 >    }}
907 >
908 >    public void testExceptionally_exceptionalCompletionActionFailed() {
909 >        for (boolean createIncomplete : new boolean[] { true, false })
910 >    {
911 >        final AtomicInteger a = new AtomicInteger(0);
912 >        final CFException ex1 = new CFException();
913 >        final CFException ex2 = new CFException();
914 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
915 >        if (!createIncomplete) f.completeExceptionally(ex1);
916 >        final CompletableFuture<Integer> g = f.exceptionally
917 >            ((Throwable t) -> {
918 >                ExecutionMode.SYNC.checkExecutionMode();
919 >                threadAssertSame(t, ex1);
920 >                a.getAndIncrement();
921 >                throw ex2;
922 >            });
923 >        if (createIncomplete) f.completeExceptionally(ex1);
924 >
925 >        checkCompletedWithWrappedException(g, ex2);
926 >        assertEquals(1, a.get());
927 >    }}
928  
929 <        f = new CompletableFuture<>();
930 <        r = new ExceptionToInteger();
931 <        g = f.exceptionally(r);
932 <        f.complete(one);
933 <        checkCompletedNormally(g, one);
934 <    }
929 >    /**
930 >     * whenComplete action executes on normal completion, propagating
931 >     * source result.
932 >     */
933 >    public void testWhenComplete_normalCompletion1() {
934 >        for (ExecutionMode m : ExecutionMode.values())
935 >        for (boolean createIncomplete : new boolean[] { true, false })
936 >        for (Integer v1 : new Integer[] { 1, null })
937 >    {
938 >        final AtomicInteger a = new AtomicInteger(0);
939 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
940 >        if (!createIncomplete) assertTrue(f.complete(v1));
941 >        final CompletableFuture<Integer> g = m.whenComplete
942 >            (f,
943 >             (Integer x, Throwable t) -> {
944 >                m.checkExecutionMode();
945 >                threadAssertSame(x, v1);
946 >                threadAssertNull(t);
947 >                a.getAndIncrement();
948 >            });
949 >        if (createIncomplete) assertTrue(f.complete(v1));
950 >
951 >        checkCompletedNormally(g, v1);
952 >        checkCompletedNormally(f, v1);
953 >        assertEquals(1, a.get());
954 >    }}
955  
956      /**
957 <     * handle action completes normally with function value on either
958 <     * normal or exceptional completion of source
957 >     * whenComplete action executes on exceptional completion, propagating
958 >     * source result.
959       */
960 <    public void testHandle() {
961 <        CompletableFuture<Integer> f, g;
962 <        IntegerHandler r;
960 >    public void testWhenComplete_exceptionalCompletion() {
961 >        for (ExecutionMode m : ExecutionMode.values())
962 >        for (boolean createIncomplete : new boolean[] { true, false })
963 >    {
964 >        final AtomicInteger a = new AtomicInteger(0);
965 >        final CFException ex = new CFException();
966 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
967 >        if (!createIncomplete) f.completeExceptionally(ex);
968 >        final CompletableFuture<Integer> g = m.whenComplete
969 >            (f,
970 >             (Integer x, Throwable t) -> {
971 >                m.checkExecutionMode();
972 >                threadAssertNull(x);
973 >                threadAssertSame(t, ex);
974 >                a.getAndIncrement();
975 >            });
976 >        if (createIncomplete) f.completeExceptionally(ex);
977 >
978 >        checkCompletedWithWrappedException(g, ex);
979 >        checkCompletedExceptionally(f, ex);
980 >        assertEquals(1, a.get());
981 >    }}
982 >
983 >    /**
984 >     * whenComplete action executes on cancelled source, propagating
985 >     * CancellationException.
986 >     */
987 >    public void testWhenComplete_sourceCancelled() {
988 >        for (ExecutionMode m : ExecutionMode.values())
989 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
990 >        for (boolean createIncomplete : new boolean[] { true, false })
991 >    {
992 >        final AtomicInteger a = new AtomicInteger(0);
993 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
994 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
995 >        final CompletableFuture<Integer> g = m.whenComplete
996 >            (f,
997 >             (Integer x, Throwable t) -> {
998 >                m.checkExecutionMode();
999 >                threadAssertNull(x);
1000 >                threadAssertTrue(t instanceof CancellationException);
1001 >                a.getAndIncrement();
1002 >            });
1003 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1004  
1005 <        f = new CompletableFuture<>();
1006 <        f.completeExceptionally(new CFException());
1007 <        g = f.handle(r = new IntegerHandler());
1008 <        assertTrue(r.ran);
406 <        checkCompletedNormally(g, three);
1005 >        checkCompletedWithWrappedCancellationException(g);
1006 >        checkCancelled(f);
1007 >        assertEquals(1, a.get());
1008 >    }}
1009  
1010 <        f = new CompletableFuture<>();
1011 <        g = f.handle(r = new IntegerHandler());
1012 <        assertFalse(r.ran);
1013 <        f.completeExceptionally(new CFException());
1014 <        checkCompletedNormally(g, three);
1015 <        assertTrue(r.ran);
1010 >    /**
1011 >     * If a whenComplete action throws an exception when triggered by
1012 >     * a normal completion, it completes exceptionally
1013 >     */
1014 >    public void testWhenComplete_actionFailed() {
1015 >        for (boolean createIncomplete : new boolean[] { true, false })
1016 >        for (ExecutionMode m : ExecutionMode.values())
1017 >        for (Integer v1 : new Integer[] { 1, null })
1018 >    {
1019 >        final AtomicInteger a = new AtomicInteger(0);
1020 >        final CFException ex = new CFException();
1021 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1022 >        if (!createIncomplete) assertTrue(f.complete(v1));
1023 >        final CompletableFuture<Integer> g = m.whenComplete
1024 >            (f,
1025 >             (Integer x, Throwable t) -> {
1026 >                m.checkExecutionMode();
1027 >                threadAssertSame(x, v1);
1028 >                threadAssertNull(t);
1029 >                a.getAndIncrement();
1030 >                throw ex;
1031 >            });
1032 >        if (createIncomplete) assertTrue(f.complete(v1));
1033 >
1034 >        checkCompletedWithWrappedException(g, ex);
1035 >        checkCompletedNormally(f, v1);
1036 >        assertEquals(1, a.get());
1037 >    }}
1038  
1039 <        f = new CompletableFuture<>();
1040 <        f.complete(one);
1041 <        g = f.handle(r = new IntegerHandler());
1042 <        assertTrue(r.ran);
1043 <        checkCompletedNormally(g, two);
1039 >    /**
1040 >     * If a whenComplete action throws an exception when triggered by
1041 >     * a source completion that also throws an exception, the source
1042 >     * exception takes precedence.
1043 >     */
1044 >    public void testWhenComplete_actionFailedSourceFailed() {
1045 >        for (boolean createIncomplete : new boolean[] { true, false })
1046 >        for (ExecutionMode m : ExecutionMode.values())
1047 >    {
1048 >        final AtomicInteger a = new AtomicInteger(0);
1049 >        final CFException ex1 = new CFException();
1050 >        final CFException ex2 = new CFException();
1051 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1052 >
1053 >        if (!createIncomplete) f.completeExceptionally(ex1);
1054 >        final CompletableFuture<Integer> g = m.whenComplete
1055 >            (f,
1056 >             (Integer x, Throwable t) -> {
1057 >                m.checkExecutionMode();
1058 >                threadAssertSame(t, ex1);
1059 >                threadAssertNull(x);
1060 >                a.getAndIncrement();
1061 >                throw ex2;
1062 >            });
1063 >        if (createIncomplete) f.completeExceptionally(ex1);
1064 >
1065 >        checkCompletedWithWrappedException(g, ex1);
1066 >        checkCompletedExceptionally(f, ex1);
1067 >        assertEquals(1, a.get());
1068 >    }}
1069 >
1070 >    /**
1071 >     * handle action completes normally with function value on normal
1072 >     * completion of source
1073 >     */
1074 >    public void testHandle_normalCompletion() {
1075 >        for (ExecutionMode m : ExecutionMode.values())
1076 >        for (boolean createIncomplete : new boolean[] { true, false })
1077 >        for (Integer v1 : new Integer[] { 1, null })
1078 >    {
1079 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1080 >        final AtomicInteger a = new AtomicInteger(0);
1081 >        if (!createIncomplete) assertTrue(f.complete(v1));
1082 >        final CompletableFuture<Integer> g = m.handle
1083 >            (f,
1084 >             (Integer x, Throwable t) -> {
1085 >                m.checkExecutionMode();
1086 >                threadAssertSame(x, v1);
1087 >                threadAssertNull(t);
1088 >                a.getAndIncrement();
1089 >                return inc(v1);
1090 >            });
1091 >        if (createIncomplete) assertTrue(f.complete(v1));
1092 >
1093 >        checkCompletedNormally(g, inc(v1));
1094 >        checkCompletedNormally(f, v1);
1095 >        assertEquals(1, a.get());
1096 >    }}
1097  
1098 <        f = new CompletableFuture<>();
1099 <        g = f.handle(r = new IntegerHandler());
1100 <        assertFalse(r.ran);
1101 <        f.complete(one);
1102 <        assertTrue(r.ran);
1103 <        checkCompletedNormally(g, two);
1104 <    }
1098 >    /**
1099 >     * handle action completes normally with function value on
1100 >     * exceptional completion of source
1101 >     */
1102 >    public void testHandle_exceptionalCompletion() {
1103 >        for (ExecutionMode m : ExecutionMode.values())
1104 >        for (boolean createIncomplete : new boolean[] { true, false })
1105 >        for (Integer v1 : new Integer[] { 1, null })
1106 >    {
1107 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1108 >        final AtomicInteger a = new AtomicInteger(0);
1109 >        final CFException ex = new CFException();
1110 >        if (!createIncomplete) f.completeExceptionally(ex);
1111 >        final CompletableFuture<Integer> g = m.handle
1112 >            (f,
1113 >             (Integer x, Throwable t) -> {
1114 >                m.checkExecutionMode();
1115 >                threadAssertNull(x);
1116 >                threadAssertSame(t, ex);
1117 >                a.getAndIncrement();
1118 >                return v1;
1119 >            });
1120 >        if (createIncomplete) f.completeExceptionally(ex);
1121 >
1122 >        checkCompletedNormally(g, v1);
1123 >        checkCompletedExceptionally(f, ex);
1124 >        assertEquals(1, a.get());
1125 >    }}
1126 >
1127 >    /**
1128 >     * handle action completes normally with function value on
1129 >     * cancelled source
1130 >     */
1131 >    public void testHandle_sourceCancelled() {
1132 >        for (ExecutionMode m : ExecutionMode.values())
1133 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1134 >        for (boolean createIncomplete : new boolean[] { true, false })
1135 >        for (Integer v1 : new Integer[] { 1, null })
1136 >    {
1137 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1138 >        final AtomicInteger a = new AtomicInteger(0);
1139 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1140 >        final CompletableFuture<Integer> g = m.handle
1141 >            (f,
1142 >             (Integer x, Throwable t) -> {
1143 >                m.checkExecutionMode();
1144 >                threadAssertNull(x);
1145 >                threadAssertTrue(t instanceof CancellationException);
1146 >                a.getAndIncrement();
1147 >                return v1;
1148 >            });
1149 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1150 >
1151 >        checkCompletedNormally(g, v1);
1152 >        checkCancelled(f);
1153 >        assertEquals(1, a.get());
1154 >    }}
1155  
1156      /**
1157 <     * runAsync completes after running Runnable
1157 >     * handle result completes exceptionally if action does
1158       */
1159 <    public void testRunAsync() {
1160 <        Noop r = new Noop();
1161 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
1162 <        assertNull(f.join());
1163 <        assertTrue(r.ran);
1164 <        checkCompletedNormally(f, null);
1165 <    }
1159 >    public void testHandle_sourceFailedActionFailed() {
1160 >        for (ExecutionMode m : ExecutionMode.values())
1161 >        for (boolean createIncomplete : new boolean[] { true, false })
1162 >    {
1163 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1164 >        final AtomicInteger a = new AtomicInteger(0);
1165 >        final CFException ex1 = new CFException();
1166 >        final CFException ex2 = new CFException();
1167 >        if (!createIncomplete) f.completeExceptionally(ex1);
1168 >        final CompletableFuture<Integer> g = m.handle
1169 >            (f,
1170 >             (Integer x, Throwable t) -> {
1171 >                m.checkExecutionMode();
1172 >                threadAssertNull(x);
1173 >                threadAssertSame(ex1, t);
1174 >                a.getAndIncrement();
1175 >                throw ex2;
1176 >            });
1177 >        if (createIncomplete) f.completeExceptionally(ex1);
1178 >
1179 >        checkCompletedWithWrappedException(g, ex2);
1180 >        checkCompletedExceptionally(f, ex1);
1181 >        assertEquals(1, a.get());
1182 >    }}
1183 >
1184 >    public void testHandle_sourceCompletedNormallyActionFailed() {
1185 >        for (ExecutionMode m : ExecutionMode.values())
1186 >        for (boolean createIncomplete : new boolean[] { true, false })
1187 >        for (Integer v1 : new Integer[] { 1, null })
1188 >    {
1189 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1190 >        final AtomicInteger a = new AtomicInteger(0);
1191 >        final CFException ex = new CFException();
1192 >        if (!createIncomplete) assertTrue(f.complete(v1));
1193 >        final CompletableFuture<Integer> g = m.handle
1194 >            (f,
1195 >             (Integer x, Throwable t) -> {
1196 >                m.checkExecutionMode();
1197 >                threadAssertSame(x, v1);
1198 >                threadAssertNull(t);
1199 >                a.getAndIncrement();
1200 >                throw ex;
1201 >            });
1202 >        if (createIncomplete) assertTrue(f.complete(v1));
1203 >
1204 >        checkCompletedWithWrappedException(g, ex);
1205 >        checkCompletedNormally(f, v1);
1206 >        assertEquals(1, a.get());
1207 >    }}
1208  
1209      /**
1210 <     * runAsync with executor completes after running Runnable
1210 >     * runAsync completes after running Runnable
1211       */
1212 <    public void testRunAsync2() {
1213 <        Noop r = new Noop();
1214 <        ThreadExecutor exec = new ThreadExecutor();
1215 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec);
1212 >    public void testRunAsync_normalCompletion() {
1213 >        ExecutionMode[] executionModes = {
1214 >            ExecutionMode.ASYNC,
1215 >            ExecutionMode.EXECUTOR,
1216 >        };
1217 >        for (ExecutionMode m : executionModes)
1218 >    {
1219 >        final Noop r = new Noop(m);
1220 >        final CompletableFuture<Void> f = m.runAsync(r);
1221          assertNull(f.join());
448        assertTrue(r.ran);
1222          checkCompletedNormally(f, null);
1223 <        assertEquals(1, exec.count.get());
1224 <    }
1223 >        r.assertInvoked();
1224 >    }}
1225  
1226      /**
1227       * failing runAsync completes exceptionally after running Runnable
1228       */
1229 <    public void testRunAsync3() {
1230 <        FailingNoop r = new FailingNoop();
1231 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
1229 >    public void testRunAsync_exceptionalCompletion() {
1230 >        ExecutionMode[] executionModes = {
1231 >            ExecutionMode.ASYNC,
1232 >            ExecutionMode.EXECUTOR,
1233 >        };
1234 >        for (ExecutionMode m : executionModes)
1235 >    {
1236 >        final FailingRunnable r = new FailingRunnable(m);
1237 >        final CompletableFuture<Void> f = m.runAsync(r);
1238          checkCompletedWithWrappedCFException(f);
1239 <        assertTrue(r.ran);
1240 <    }
1239 >        r.assertInvoked();
1240 >    }}
1241  
1242      /**
1243       * supplyAsync completes with result of supplier
1244       */
1245 <    public void testSupplyAsync() {
1246 <        CompletableFuture<Integer> f;
1247 <        f = CompletableFuture.supplyAsync(supplyOne);
1248 <        assertEquals(f.join(), one);
1249 <        checkCompletedNormally(f, one);
1250 <    }
1251 <
1252 <    /**
1253 <     * supplyAsync with executor completes with result of supplier
1254 <     */
1255 <    public void testSupplyAsync2() {
1256 <        CompletableFuture<Integer> f;
1257 <        f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor());
1258 <        assertEquals(f.join(), one);
480 <        checkCompletedNormally(f, one);
481 <    }
1245 >    public void testSupplyAsync_normalCompletion() {
1246 >        ExecutionMode[] executionModes = {
1247 >            ExecutionMode.ASYNC,
1248 >            ExecutionMode.EXECUTOR,
1249 >        };
1250 >        for (ExecutionMode m : executionModes)
1251 >        for (Integer v1 : new Integer[] { 1, null })
1252 >    {
1253 >        final IntegerSupplier r = new IntegerSupplier(m, v1);
1254 >        final CompletableFuture<Integer> f = m.supplyAsync(r);
1255 >        assertSame(v1, f.join());
1256 >        checkCompletedNormally(f, v1);
1257 >        r.assertInvoked();
1258 >    }}
1259  
1260      /**
1261       * Failing supplyAsync completes exceptionally
1262       */
1263 <    public void testSupplyAsync3() {
1264 <        FailingSupplier r = new FailingSupplier();
1265 <        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r);
1263 >    public void testSupplyAsync_exceptionalCompletion() {
1264 >        ExecutionMode[] executionModes = {
1265 >            ExecutionMode.ASYNC,
1266 >            ExecutionMode.EXECUTOR,
1267 >        };
1268 >        for (ExecutionMode m : executionModes)
1269 >    {
1270 >        FailingSupplier r = new FailingSupplier(m);
1271 >        CompletableFuture<Integer> f = m.supplyAsync(r);
1272          checkCompletedWithWrappedCFException(f);
1273 <        assertTrue(r.ran);
1274 <    }
1273 >        r.assertInvoked();
1274 >    }}
1275  
1276      // seq completion methods
1277  
1278      /**
1279       * thenRun result completes normally after normal completion of source
1280       */
1281 <    public void testThenRun() {
1282 <        CompletableFuture<Integer> f;
1283 <        CompletableFuture<Void> g;
1284 <        Noop r;
1285 <
1286 <        f = new CompletableFuture<>();
1287 <        g = f.thenRun(r = new Noop());
1288 <        f.complete(null);
1289 <        checkCompletedNormally(g, null);
1290 <        assertTrue(r.ran);
1291 <
1292 <        f = new CompletableFuture<>();
1293 <        f.complete(null);
1294 <        g = f.thenRun(r = new Noop());
1295 <        checkCompletedNormally(g, null);
1296 <        assertTrue(r.ran);
1297 <    }
1281 >    public void testThenRun_normalCompletion() {
1282 >        for (ExecutionMode m : ExecutionMode.values())
1283 >        for (Integer v1 : new Integer[] { 1, null })
1284 >    {
1285 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1286 >        final Noop[] rs = new Noop[6];
1287 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1288 >
1289 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1290 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1291 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1292 >        checkIncomplete(h0);
1293 >        checkIncomplete(h1);
1294 >        checkIncomplete(h2);
1295 >        assertTrue(f.complete(v1));
1296 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1297 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1298 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1299 >
1300 >        checkCompletedNormally(h0, null);
1301 >        checkCompletedNormally(h1, null);
1302 >        checkCompletedNormally(h2, null);
1303 >        checkCompletedNormally(h3, null);
1304 >        checkCompletedNormally(h4, null);
1305 >        checkCompletedNormally(h5, null);
1306 >        checkCompletedNormally(f, v1);
1307 >        for (Noop r : rs) r.assertInvoked();
1308 >    }}
1309  
1310      /**
1311       * thenRun result completes exceptionally after exceptional
1312       * completion of source
1313       */
1314 <    public void testThenRun2() {
1315 <        CompletableFuture<Integer> f;
1316 <        CompletableFuture<Void> g;
1317 <        Noop r;
1318 <
1319 <        f = new CompletableFuture<>();
1320 <        g = f.thenRun(r = new Noop());
1321 <        f.completeExceptionally(new CFException());
1322 <        checkCompletedWithWrappedCFException(g);
1323 <        assertFalse(r.ran);
1324 <
1325 <        f = new CompletableFuture<>();
1326 <        f.completeExceptionally(new CFException());
1327 <        g = f.thenRun(r = new Noop());
1328 <        checkCompletedWithWrappedCFException(g);
1329 <        assertFalse(r.ran);
1330 <    }
1314 >    public void testThenRun_exceptionalCompletion() {
1315 >        for (ExecutionMode m : ExecutionMode.values())
1316 >    {
1317 >        final CFException ex = new CFException();
1318 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1319 >        final Noop[] rs = new Noop[6];
1320 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1321 >
1322 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1323 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1324 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1325 >        checkIncomplete(h0);
1326 >        checkIncomplete(h1);
1327 >        checkIncomplete(h2);
1328 >        assertTrue(f.completeExceptionally(ex));
1329 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1330 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1331 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1332 >
1333 >        checkCompletedWithWrappedException(h0, ex);
1334 >        checkCompletedWithWrappedException(h1, ex);
1335 >        checkCompletedWithWrappedException(h2, ex);
1336 >        checkCompletedWithWrappedException(h3, ex);
1337 >        checkCompletedWithWrappedException(h4, ex);
1338 >        checkCompletedWithWrappedException(h5, ex);
1339 >        checkCompletedExceptionally(f, ex);
1340 >        for (Noop r : rs) r.assertNotInvoked();
1341 >    }}
1342  
1343      /**
1344 <     * thenRun result completes exceptionally if action does
1344 >     * thenRun result completes exceptionally if source cancelled
1345       */
1346 <    public void testThenRun3() {
1347 <        CompletableFuture<Integer> f;
1348 <        CompletableFuture<Void> g;
1349 <        FailingNoop r;
1350 <
1351 <        f = new CompletableFuture<>();
1352 <        g = f.thenRun(r = new FailingNoop());
1353 <        f.complete(null);
1354 <        checkCompletedWithWrappedCFException(g);
1355 <
1356 <        f = new CompletableFuture<>();
1357 <        f.complete(null);
1358 <        g = f.thenRun(r = new FailingNoop());
1359 <        checkCompletedWithWrappedCFException(g);
1360 <    }
1346 >    public void testThenRun_sourceCancelled() {
1347 >        for (ExecutionMode m : ExecutionMode.values())
1348 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1349 >    {
1350 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1351 >        final Noop[] rs = new Noop[6];
1352 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1353 >
1354 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1355 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1356 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1357 >        checkIncomplete(h0);
1358 >        checkIncomplete(h1);
1359 >        checkIncomplete(h2);
1360 >        assertTrue(f.cancel(mayInterruptIfRunning));
1361 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1362 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1363 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1364 >
1365 >        checkCompletedWithWrappedCancellationException(h0);
1366 >        checkCompletedWithWrappedCancellationException(h1);
1367 >        checkCompletedWithWrappedCancellationException(h2);
1368 >        checkCompletedWithWrappedCancellationException(h3);
1369 >        checkCompletedWithWrappedCancellationException(h4);
1370 >        checkCompletedWithWrappedCancellationException(h5);
1371 >        checkCancelled(f);
1372 >        for (Noop r : rs) r.assertNotInvoked();
1373 >    }}
1374  
1375      /**
1376 <     * thenRun result completes exceptionally if source cancelled
1376 >     * thenRun result completes exceptionally if action does
1377       */
1378 <    public void testThenRun4() {
1379 <        CompletableFuture<Integer> f;
1380 <        CompletableFuture<Void> g;
1381 <        Noop r;
1382 <
1383 <        f = new CompletableFuture<>();
1384 <        g = f.thenRun(r = new Noop());
1385 <        assertTrue(f.cancel(true));
1386 <        checkCompletedWithWrappedCancellationException(g);
1387 <
1388 <        f = new CompletableFuture<>();
1389 <        assertTrue(f.cancel(true));
1390 <        g = f.thenRun(r = new Noop());
1391 <        checkCompletedWithWrappedCancellationException(g);
1392 <    }
1378 >    public void testThenRun_actionFailed() {
1379 >        for (ExecutionMode m : ExecutionMode.values())
1380 >        for (Integer v1 : new Integer[] { 1, null })
1381 >    {
1382 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1383 >        final FailingRunnable[] rs = new FailingRunnable[6];
1384 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
1385 >
1386 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1387 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1388 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1389 >        assertTrue(f.complete(v1));
1390 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1391 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1392 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1393 >
1394 >        checkCompletedWithWrappedCFException(h0);
1395 >        checkCompletedWithWrappedCFException(h1);
1396 >        checkCompletedWithWrappedCFException(h2);
1397 >        checkCompletedWithWrappedCFException(h3);
1398 >        checkCompletedWithWrappedCFException(h4);
1399 >        checkCompletedWithWrappedCFException(h5);
1400 >        checkCompletedNormally(f, v1);
1401 >    }}
1402  
1403      /**
1404       * thenApply result completes normally after normal completion of source
1405       */
1406 <    public void testThenApply() {
1407 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1408 <        CompletableFuture<Integer> g = f.thenApply(inc);
1409 <        f.complete(one);
1410 <        checkCompletedNormally(g, two);
1411 <    }
1406 >    public void testThenApply_normalCompletion() {
1407 >        for (ExecutionMode m : ExecutionMode.values())
1408 >        for (Integer v1 : new Integer[] { 1, null })
1409 >    {
1410 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1411 >        final IncFunction[] rs = new IncFunction[4];
1412 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1413 >
1414 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1415 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1416 >        checkIncomplete(h0);
1417 >        checkIncomplete(h1);
1418 >        assertTrue(f.complete(v1));
1419 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1420 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1421 >
1422 >        checkCompletedNormally(h0, inc(v1));
1423 >        checkCompletedNormally(h1, inc(v1));
1424 >        checkCompletedNormally(h2, inc(v1));
1425 >        checkCompletedNormally(h3, inc(v1));
1426 >        checkCompletedNormally(f, v1);
1427 >        for (IncFunction r : rs) r.assertValue(inc(v1));
1428 >    }}
1429  
1430      /**
1431       * thenApply result completes exceptionally after exceptional
1432       * completion of source
1433       */
1434 <    public void testThenApply2() {
1435 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1436 <        CompletableFuture<Integer> g = f.thenApply(inc);
1437 <        f.completeExceptionally(new CFException());
1438 <        checkCompletedWithWrappedCFException(g);
1439 <    }
1434 >    public void testThenApply_exceptionalCompletion() {
1435 >        for (ExecutionMode m : ExecutionMode.values())
1436 >    {
1437 >        final CFException ex = new CFException();
1438 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1439 >        final IncFunction[] rs = new IncFunction[4];
1440 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1441 >
1442 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1443 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1444 >        assertTrue(f.completeExceptionally(ex));
1445 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1446 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1447 >
1448 >        checkCompletedWithWrappedException(h0, ex);
1449 >        checkCompletedWithWrappedException(h1, ex);
1450 >        checkCompletedWithWrappedException(h2, ex);
1451 >        checkCompletedWithWrappedException(h3, ex);
1452 >        checkCompletedExceptionally(f, ex);
1453 >        for (IncFunction r : rs) r.assertNotInvoked();
1454 >    }}
1455  
1456      /**
1457 <     * thenApply result completes exceptionally if action does
1457 >     * thenApply result completes exceptionally if source cancelled
1458       */
1459 <    public void testThenApply3() {
1460 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1461 <        CompletableFuture<Integer> g = f.thenApply(new FailingFunction());
1462 <        f.complete(one);
1463 <        checkCompletedWithWrappedCFException(g);
1464 <    }
1459 >    public void testThenApply_sourceCancelled() {
1460 >        for (ExecutionMode m : ExecutionMode.values())
1461 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1462 >    {
1463 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1464 >        final IncFunction[] rs = new IncFunction[4];
1465 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1466 >
1467 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1468 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1469 >        assertTrue(f.cancel(mayInterruptIfRunning));
1470 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1471 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1472 >
1473 >        checkCompletedWithWrappedCancellationException(h0);
1474 >        checkCompletedWithWrappedCancellationException(h1);
1475 >        checkCompletedWithWrappedCancellationException(h2);
1476 >        checkCompletedWithWrappedCancellationException(h3);
1477 >        checkCancelled(f);
1478 >        for (IncFunction r : rs) r.assertNotInvoked();
1479 >    }}
1480  
1481      /**
1482 <     * thenApply result completes exceptionally if source cancelled
1482 >     * thenApply result completes exceptionally if action does
1483       */
1484 <    public void testThenApply4() {
1485 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1486 <        CompletableFuture<Integer> g = f.thenApply(inc);
1487 <        assertTrue(f.cancel(true));
1488 <        checkCompletedWithWrappedCancellationException(g);
1489 <    }
1484 >    public void testThenApply_actionFailed() {
1485 >        for (ExecutionMode m : ExecutionMode.values())
1486 >        for (Integer v1 : new Integer[] { 1, null })
1487 >    {
1488 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1489 >        final FailingFunction[] rs = new FailingFunction[4];
1490 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
1491 >
1492 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1493 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1494 >        assertTrue(f.complete(v1));
1495 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1496 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1497 >
1498 >        checkCompletedWithWrappedCFException(h0);
1499 >        checkCompletedWithWrappedCFException(h1);
1500 >        checkCompletedWithWrappedCFException(h2);
1501 >        checkCompletedWithWrappedCFException(h3);
1502 >        checkCompletedNormally(f, v1);
1503 >    }}
1504  
1505      /**
1506       * thenAccept result completes normally after normal completion of source
1507       */
1508 <    public void testThenAccept() {
1509 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1510 <        IncAction r = new IncAction();
1511 <        CompletableFuture<Void> g = f.thenAccept(r);
1512 <        f.complete(one);
1513 <        checkCompletedNormally(g, null);
1514 <        assertEquals(r.value, 2);
1515 <    }
1508 >    public void testThenAccept_normalCompletion() {
1509 >        for (ExecutionMode m : ExecutionMode.values())
1510 >        for (Integer v1 : new Integer[] { 1, null })
1511 >    {
1512 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1513 >        final NoopConsumer[] rs = new NoopConsumer[4];
1514 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1515 >
1516 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1517 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1518 >        checkIncomplete(h0);
1519 >        checkIncomplete(h1);
1520 >        assertTrue(f.complete(v1));
1521 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1522 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1523 >
1524 >        checkCompletedNormally(h0, null);
1525 >        checkCompletedNormally(h1, null);
1526 >        checkCompletedNormally(h2, null);
1527 >        checkCompletedNormally(h3, null);
1528 >        checkCompletedNormally(f, v1);
1529 >        for (NoopConsumer r : rs) r.assertValue(v1);
1530 >    }}
1531  
1532      /**
1533       * thenAccept result completes exceptionally after exceptional
1534       * completion of source
1535       */
1536 <    public void testThenAccept2() {
1537 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1538 <        IncAction r = new IncAction();
1539 <        CompletableFuture<Void> g = f.thenAccept(r);
1540 <        f.completeExceptionally(new CFException());
1541 <        checkCompletedWithWrappedCFException(g);
1542 <    }
1536 >    public void testThenAccept_exceptionalCompletion() {
1537 >        for (ExecutionMode m : ExecutionMode.values())
1538 >    {
1539 >        final CFException ex = new CFException();
1540 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1541 >        final NoopConsumer[] rs = new NoopConsumer[4];
1542 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1543 >
1544 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1545 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1546 >        assertTrue(f.completeExceptionally(ex));
1547 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1548 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1549 >
1550 >        checkCompletedWithWrappedException(h0, ex);
1551 >        checkCompletedWithWrappedException(h1, ex);
1552 >        checkCompletedWithWrappedException(h2, ex);
1553 >        checkCompletedWithWrappedException(h3, ex);
1554 >        checkCompletedExceptionally(f, ex);
1555 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1556 >    }}
1557  
1558      /**
1559 <     * thenAccept result completes exceptionally if action does
1559 >     * thenAccept result completes exceptionally if source cancelled
1560       */
1561 <    public void testThenAccept3() {
1562 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1563 <        FailingConsumer r = new FailingConsumer();
1564 <        CompletableFuture<Void> g = f.thenAccept(r);
1565 <        f.complete(one);
1566 <        checkCompletedWithWrappedCFException(g);
1567 <        assertTrue(r.ran);
1568 <    }
1561 >    public void testThenAccept_sourceCancelled() {
1562 >        for (ExecutionMode m : ExecutionMode.values())
1563 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1564 >    {
1565 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1566 >        final NoopConsumer[] rs = new NoopConsumer[4];
1567 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1568 >
1569 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1570 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1571 >        assertTrue(f.cancel(mayInterruptIfRunning));
1572 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1573 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1574 >
1575 >        checkCompletedWithWrappedCancellationException(h0);
1576 >        checkCompletedWithWrappedCancellationException(h1);
1577 >        checkCompletedWithWrappedCancellationException(h2);
1578 >        checkCompletedWithWrappedCancellationException(h3);
1579 >        checkCancelled(f);
1580 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1581 >    }}
1582  
1583      /**
1584 <     * thenAccept result completes exceptionally if source cancelled
1584 >     * thenAccept result completes exceptionally if action does
1585       */
1586 <    public void testThenAccept4() {
1587 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1588 <        IncAction r = new IncAction();
1589 <        CompletableFuture<Void> g = f.thenAccept(r);
1590 <        assertTrue(f.cancel(true));
1591 <        checkCompletedWithWrappedCancellationException(g);
1592 <    }
1586 >    public void testThenAccept_actionFailed() {
1587 >        for (ExecutionMode m : ExecutionMode.values())
1588 >        for (Integer v1 : new Integer[] { 1, null })
1589 >    {
1590 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1591 >        final FailingConsumer[] rs = new FailingConsumer[4];
1592 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
1593 >
1594 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1595 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1596 >        assertTrue(f.complete(v1));
1597 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1598 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1599 >
1600 >        checkCompletedWithWrappedCFException(h0);
1601 >        checkCompletedWithWrappedCFException(h1);
1602 >        checkCompletedWithWrappedCFException(h2);
1603 >        checkCompletedWithWrappedCFException(h3);
1604 >        checkCompletedNormally(f, v1);
1605 >    }}
1606  
1607      /**
1608       * thenCombine result completes normally after normal completion
1609       * of sources
1610       */
1611 <    public void testThenCombine() {
1612 <        CompletableFuture<Integer> f, g, h;
1613 <
1614 <        f = new CompletableFuture<>();
1615 <        g = new CompletableFuture<>();
1616 <        h = f.thenCombine(g, subtract);
1617 <        f.complete(3);
1618 <        checkIncomplete(h);
1619 <        g.complete(1);
1620 <        checkCompletedNormally(h, 2);
1621 <
1622 <        f = new CompletableFuture<>();
1623 <        g = new CompletableFuture<>();
1624 <        h = f.thenCombine(g, subtract);
1625 <        g.complete(1);
1626 <        checkIncomplete(h);
1627 <        f.complete(3);
1628 <        checkCompletedNormally(h, 2);
1629 <
1630 <        f = new CompletableFuture<>();
1631 <        g = new CompletableFuture<>();
1632 <        g.complete(1);
1633 <        f.complete(3);
1634 <        h = f.thenCombine(g, subtract);
1635 <        checkCompletedNormally(h, 2);
1636 <    }
1611 >    public void testThenCombine_normalCompletion() {
1612 >        for (ExecutionMode m : ExecutionMode.values())
1613 >        for (boolean fFirst : new boolean[] { true, false })
1614 >        for (Integer v1 : new Integer[] { 1, null })
1615 >        for (Integer v2 : new Integer[] { 2, null })
1616 >    {
1617 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1618 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1619 >        final SubtractFunction[] rs = new SubtractFunction[6];
1620 >        for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m);
1621 >
1622 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1623 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1624 >        final Integer w1 =  fFirst ? v1 : v2;
1625 >        final Integer w2 = !fFirst ? v1 : v2;
1626 >
1627 >        final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]);
1628 >        final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]);
1629 >        assertTrue(fst.complete(w1));
1630 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]);
1631 >        final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]);
1632 >        checkIncomplete(h0); rs[0].assertNotInvoked();
1633 >        checkIncomplete(h2); rs[2].assertNotInvoked();
1634 >        checkCompletedNormally(h1, subtract(w1, w1));
1635 >        checkCompletedNormally(h3, subtract(w1, w1));
1636 >        rs[1].assertValue(subtract(w1, w1));
1637 >        rs[3].assertValue(subtract(w1, w1));
1638 >        assertTrue(snd.complete(w2));
1639 >        final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]);
1640 >
1641 >        checkCompletedNormally(h0, subtract(v1, v2));
1642 >        checkCompletedNormally(h2, subtract(v1, v2));
1643 >        checkCompletedNormally(h4, subtract(v1, v2));
1644 >        rs[0].assertValue(subtract(v1, v2));
1645 >        rs[2].assertValue(subtract(v1, v2));
1646 >        rs[4].assertValue(subtract(v1, v2));
1647 >
1648 >        checkCompletedNormally(f, v1);
1649 >        checkCompletedNormally(g, v2);
1650 >    }}
1651  
1652      /**
1653       * thenCombine result completes exceptionally after exceptional
1654       * completion of either source
1655       */
1656 <    public void testThenCombine2() {
1657 <        CompletableFuture<Integer> f, g, h;
1658 <
1659 <        f = new CompletableFuture<>();
1660 <        g = new CompletableFuture<>();
1661 <        h = f.thenCombine(g, subtract);
1662 <        f.completeExceptionally(new CFException());
1663 <        checkIncomplete(h);
1664 <        g.complete(1);
1665 <        checkCompletedWithWrappedCFException(h);
1666 <
1667 <        f = new CompletableFuture<>();
1668 <        g = new CompletableFuture<>();
1669 <        h = f.thenCombine(g, subtract);
1670 <        g.completeExceptionally(new CFException());
1671 <        checkIncomplete(h);
1672 <        f.complete(3);
1673 <        checkCompletedWithWrappedCFException(h);
1674 <
1675 <        f = new CompletableFuture<>();
1676 <        g = new CompletableFuture<>();
1677 <        f.complete(3);
1678 <        g.completeExceptionally(new CFException());
1679 <        h = f.thenCombine(g, subtract);
1680 <        checkCompletedWithWrappedCFException(h);
1681 <
1682 <        f = new CompletableFuture<>();
1683 <        g = new CompletableFuture<>();
1684 <        f.completeExceptionally(new CFException());
1685 <        g.complete(3);
1686 <        h = f.thenCombine(g, subtract);
1687 <        checkCompletedWithWrappedCFException(h);
1688 <    }
1656 >    public void testThenCombine_exceptionalCompletion() throws Throwable {
1657 >        for (ExecutionMode m : ExecutionMode.values())
1658 >        for (boolean fFirst : new boolean[] { true, false })
1659 >        for (boolean failFirst : new boolean[] { true, false })
1660 >        for (Integer v1 : new Integer[] { 1, null })
1661 >    {
1662 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1663 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1664 >        final CFException ex = new CFException();
1665 >        final SubtractFunction r1 = new SubtractFunction(m);
1666 >        final SubtractFunction r2 = new SubtractFunction(m);
1667 >        final SubtractFunction r3 = new SubtractFunction(m);
1668 >
1669 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1670 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1671 >        final Callable<Boolean> complete1 = failFirst ?
1672 >            () -> fst.completeExceptionally(ex) :
1673 >            () -> fst.complete(v1);
1674 >        final Callable<Boolean> complete2 = failFirst ?
1675 >            () -> snd.complete(v1) :
1676 >            () -> snd.completeExceptionally(ex);
1677 >
1678 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1679 >        assertTrue(complete1.call());
1680 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1681 >        checkIncomplete(h1);
1682 >        checkIncomplete(h2);
1683 >        assertTrue(complete2.call());
1684 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1685 >
1686 >        checkCompletedWithWrappedException(h1, ex);
1687 >        checkCompletedWithWrappedException(h2, ex);
1688 >        checkCompletedWithWrappedException(h3, ex);
1689 >        r1.assertNotInvoked();
1690 >        r2.assertNotInvoked();
1691 >        r3.assertNotInvoked();
1692 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1693 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1694 >    }}
1695  
1696      /**
1697 <     * thenCombine result completes exceptionally if action does
1697 >     * thenCombine result completes exceptionally if either source cancelled
1698       */
1699 <    public void testThenCombine3() {
1700 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1701 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1702 <        FailingBiFunction r = new FailingBiFunction();
1703 <        CompletableFuture<Integer> g = f.thenCombine(f2, r);
1704 <        f.complete(one);
1705 <        checkIncomplete(g);
1706 <        assertFalse(r.ran);
1707 <        f2.complete(two);
1708 <        checkCompletedWithWrappedCFException(g);
1709 <        assertTrue(r.ran);
1710 <    }
1699 >    public void testThenCombine_sourceCancelled() throws Throwable {
1700 >        for (ExecutionMode m : ExecutionMode.values())
1701 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1702 >        for (boolean fFirst : new boolean[] { true, false })
1703 >        for (boolean failFirst : new boolean[] { true, false })
1704 >        for (Integer v1 : new Integer[] { 1, null })
1705 >    {
1706 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1707 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1708 >        final SubtractFunction r1 = new SubtractFunction(m);
1709 >        final SubtractFunction r2 = new SubtractFunction(m);
1710 >        final SubtractFunction r3 = new SubtractFunction(m);
1711 >
1712 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1713 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1714 >        final Callable<Boolean> complete1 = failFirst ?
1715 >            () -> fst.cancel(mayInterruptIfRunning) :
1716 >            () -> fst.complete(v1);
1717 >        final Callable<Boolean> complete2 = failFirst ?
1718 >            () -> snd.complete(v1) :
1719 >            () -> snd.cancel(mayInterruptIfRunning);
1720 >
1721 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1722 >        assertTrue(complete1.call());
1723 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1724 >        checkIncomplete(h1);
1725 >        checkIncomplete(h2);
1726 >        assertTrue(complete2.call());
1727 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1728 >
1729 >        checkCompletedWithWrappedCancellationException(h1);
1730 >        checkCompletedWithWrappedCancellationException(h2);
1731 >        checkCompletedWithWrappedCancellationException(h3);
1732 >        r1.assertNotInvoked();
1733 >        r2.assertNotInvoked();
1734 >        r3.assertNotInvoked();
1735 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1736 >        checkCancelled(failFirst ? fst : snd);
1737 >    }}
1738  
1739      /**
1740 <     * thenCombine result completes exceptionally if either source cancelled
1740 >     * thenCombine result completes exceptionally if action does
1741       */
1742 <    public void testThenCombine4() {
1743 <        CompletableFuture<Integer> f, g, h;
1744 <
1745 <        f = new CompletableFuture<>();
1746 <        g = new CompletableFuture<>();
1747 <        h = f.thenCombine(g, subtract);
1748 <        assertTrue(f.cancel(true));
1749 <        checkIncomplete(h);
1750 <        g.complete(1);
1751 <        checkCompletedWithWrappedCancellationException(h);
1752 <
1753 <        f = new CompletableFuture<>();
1754 <        g = new CompletableFuture<>();
1755 <        h = f.thenCombine(g, subtract);
1756 <        assertTrue(g.cancel(true));
1757 <        checkIncomplete(h);
1758 <        f.complete(3);
1759 <        checkCompletedWithWrappedCancellationException(h);
1760 <
1761 <        f = new CompletableFuture<>();
1762 <        g = new CompletableFuture<>();
1763 <        assertTrue(f.cancel(true));
1764 <        assertTrue(g.cancel(true));
1765 <        h = f.thenCombine(g, subtract);
1766 <        checkCompletedWithWrappedCancellationException(h);
1767 <    }
1742 >    public void testThenCombine_actionFailed() {
1743 >        for (ExecutionMode m : ExecutionMode.values())
1744 >        for (boolean fFirst : new boolean[] { true, false })
1745 >        for (Integer v1 : new Integer[] { 1, null })
1746 >        for (Integer v2 : new Integer[] { 2, null })
1747 >    {
1748 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1749 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1750 >        final FailingBiFunction r1 = new FailingBiFunction(m);
1751 >        final FailingBiFunction r2 = new FailingBiFunction(m);
1752 >        final FailingBiFunction r3 = new FailingBiFunction(m);
1753 >
1754 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1755 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1756 >        final Integer w1 =  fFirst ? v1 : v2;
1757 >        final Integer w2 = !fFirst ? v1 : v2;
1758 >
1759 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1760 >        assertTrue(fst.complete(w1));
1761 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1762 >        assertTrue(snd.complete(w2));
1763 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1764 >
1765 >        checkCompletedWithWrappedCFException(h1);
1766 >        checkCompletedWithWrappedCFException(h2);
1767 >        checkCompletedWithWrappedCFException(h3);
1768 >        r1.assertInvoked();
1769 >        r2.assertInvoked();
1770 >        r3.assertInvoked();
1771 >        checkCompletedNormally(f, v1);
1772 >        checkCompletedNormally(g, v2);
1773 >    }}
1774  
1775      /**
1776       * thenAcceptBoth result completes normally after normal
1777       * completion of sources
1778       */
1779 <    public void testThenAcceptBoth() {
1780 <        CompletableFuture<Integer> f, g;
1781 <        CompletableFuture<Void> h;
1782 <        SubtractAction r;
1783 <
1784 <        f = new CompletableFuture<>();
1785 <        g = new CompletableFuture<>();
1786 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1787 <        f.complete(3);
1788 <        checkIncomplete(h);
1789 <        g.complete(1);
1790 <        checkCompletedNormally(h, null);
1791 <        assertEquals(r.value, 2);
1792 <
1793 <        f = new CompletableFuture<>();
1794 <        g = new CompletableFuture<>();
1795 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1796 <        g.complete(1);
1797 <        checkIncomplete(h);
1798 <        f.complete(3);
1799 <        checkCompletedNormally(h, null);
1800 <        assertEquals(r.value, 2);
1801 <
1802 <        f = new CompletableFuture<>();
1803 <        g = new CompletableFuture<>();
1804 <        g.complete(1);
1805 <        f.complete(3);
1806 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1807 <        checkCompletedNormally(h, null);
1808 <        assertEquals(r.value, 2);
1809 <    }
1779 >    public void testThenAcceptBoth_normalCompletion() {
1780 >        for (ExecutionMode m : ExecutionMode.values())
1781 >        for (boolean fFirst : new boolean[] { true, false })
1782 >        for (Integer v1 : new Integer[] { 1, null })
1783 >        for (Integer v2 : new Integer[] { 2, null })
1784 >    {
1785 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1786 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1787 >        final SubtractAction r1 = new SubtractAction(m);
1788 >        final SubtractAction r2 = new SubtractAction(m);
1789 >        final SubtractAction r3 = new SubtractAction(m);
1790 >
1791 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1792 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1793 >        final Integer w1 =  fFirst ? v1 : v2;
1794 >        final Integer w2 = !fFirst ? v1 : v2;
1795 >
1796 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1797 >        assertTrue(fst.complete(w1));
1798 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1799 >        checkIncomplete(h1);
1800 >        checkIncomplete(h2);
1801 >        r1.assertNotInvoked();
1802 >        r2.assertNotInvoked();
1803 >        assertTrue(snd.complete(w2));
1804 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1805 >
1806 >        checkCompletedNormally(h1, null);
1807 >        checkCompletedNormally(h2, null);
1808 >        checkCompletedNormally(h3, null);
1809 >        r1.assertValue(subtract(v1, v2));
1810 >        r2.assertValue(subtract(v1, v2));
1811 >        r3.assertValue(subtract(v1, v2));
1812 >        checkCompletedNormally(f, v1);
1813 >        checkCompletedNormally(g, v2);
1814 >    }}
1815  
1816      /**
1817       * thenAcceptBoth result completes exceptionally after exceptional
1818       * completion of either source
1819       */
1820 <    public void testThenAcceptBoth2() {
1821 <        CompletableFuture<Integer> f, g;
1822 <        CompletableFuture<Void> h;
1823 <        SubtractAction r;
1824 <
1825 <        f = new CompletableFuture<>();
1826 <        g = new CompletableFuture<>();
1827 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1828 <        f.completeExceptionally(new CFException());
1829 <        checkIncomplete(h);
1830 <        g.complete(1);
1831 <        checkCompletedWithWrappedCFException(h);
1832 <
1833 <        f = new CompletableFuture<>();
1834 <        g = new CompletableFuture<>();
1835 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1836 <        g.completeExceptionally(new CFException());
1837 <        checkIncomplete(h);
1838 <        f.complete(3);
1839 <        checkCompletedWithWrappedCFException(h);
1840 <
1841 <        f = new CompletableFuture<>();
1842 <        g = new CompletableFuture<>();
1843 <        f.complete(3);
1844 <        g.completeExceptionally(new CFException());
1845 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1846 <        checkCompletedWithWrappedCFException(h);
1847 <
1848 <        f = new CompletableFuture<>();
1849 <        g = new CompletableFuture<>();
1850 <        f.completeExceptionally(new CFException());
1851 <        g.complete(3);
1852 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1853 <        checkCompletedWithWrappedCFException(h);
1854 <    }
1820 >    public void testThenAcceptBoth_exceptionalCompletion() throws Throwable {
1821 >        for (ExecutionMode m : ExecutionMode.values())
1822 >        for (boolean fFirst : new boolean[] { true, false })
1823 >        for (boolean failFirst : new boolean[] { true, false })
1824 >        for (Integer v1 : new Integer[] { 1, null })
1825 >    {
1826 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1827 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1828 >        final CFException ex = new CFException();
1829 >        final SubtractAction r1 = new SubtractAction(m);
1830 >        final SubtractAction r2 = new SubtractAction(m);
1831 >        final SubtractAction r3 = new SubtractAction(m);
1832 >
1833 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1834 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1835 >        final Callable<Boolean> complete1 = failFirst ?
1836 >            () -> fst.completeExceptionally(ex) :
1837 >            () -> fst.complete(v1);
1838 >        final Callable<Boolean> complete2 = failFirst ?
1839 >            () -> snd.complete(v1) :
1840 >            () -> snd.completeExceptionally(ex);
1841 >
1842 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1843 >        assertTrue(complete1.call());
1844 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1845 >        checkIncomplete(h1);
1846 >        checkIncomplete(h2);
1847 >        assertTrue(complete2.call());
1848 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1849 >
1850 >        checkCompletedWithWrappedException(h1, ex);
1851 >        checkCompletedWithWrappedException(h2, ex);
1852 >        checkCompletedWithWrappedException(h3, ex);
1853 >        r1.assertNotInvoked();
1854 >        r2.assertNotInvoked();
1855 >        r3.assertNotInvoked();
1856 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1857 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1858 >    }}
1859  
1860      /**
1861 <     * thenAcceptBoth result completes exceptionally if action does
1861 >     * thenAcceptBoth result completes exceptionally if either source cancelled
1862       */
1863 <    public void testThenAcceptBoth3() {
1864 <        CompletableFuture<Integer> f, g;
1865 <        CompletableFuture<Void> h;
1866 <        FailingBiConsumer r;
1867 <
1868 <        f = new CompletableFuture<>();
1869 <        g = new CompletableFuture<>();
1870 <        h = f.thenAcceptBoth(g, r = new FailingBiConsumer());
1871 <        f.complete(3);
1872 <        checkIncomplete(h);
1873 <        g.complete(1);
1874 <        checkCompletedWithWrappedCFException(h);
1875 <
1876 <        f = new CompletableFuture<>();
1877 <        g = new CompletableFuture<>();
1878 <        f.complete(3);
1879 <        g.complete(1);
1880 <        h = f.thenAcceptBoth(g, r = new FailingBiConsumer());
1881 <        checkCompletedWithWrappedCFException(h);
1882 <    }
1863 >    public void testThenAcceptBoth_sourceCancelled() throws Throwable {
1864 >        for (ExecutionMode m : ExecutionMode.values())
1865 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1866 >        for (boolean fFirst : new boolean[] { true, false })
1867 >        for (boolean failFirst : new boolean[] { true, false })
1868 >        for (Integer v1 : new Integer[] { 1, null })
1869 >    {
1870 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1871 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1872 >        final SubtractAction r1 = new SubtractAction(m);
1873 >        final SubtractAction r2 = new SubtractAction(m);
1874 >        final SubtractAction r3 = new SubtractAction(m);
1875 >
1876 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1877 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1878 >        final Callable<Boolean> complete1 = failFirst ?
1879 >            () -> fst.cancel(mayInterruptIfRunning) :
1880 >            () -> fst.complete(v1);
1881 >        final Callable<Boolean> complete2 = failFirst ?
1882 >            () -> snd.complete(v1) :
1883 >            () -> snd.cancel(mayInterruptIfRunning);
1884 >
1885 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1886 >        assertTrue(complete1.call());
1887 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1888 >        checkIncomplete(h1);
1889 >        checkIncomplete(h2);
1890 >        assertTrue(complete2.call());
1891 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1892 >
1893 >        checkCompletedWithWrappedCancellationException(h1);
1894 >        checkCompletedWithWrappedCancellationException(h2);
1895 >        checkCompletedWithWrappedCancellationException(h3);
1896 >        r1.assertNotInvoked();
1897 >        r2.assertNotInvoked();
1898 >        r3.assertNotInvoked();
1899 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1900 >        checkCancelled(failFirst ? fst : snd);
1901 >    }}
1902  
1903      /**
1904 <     * thenAcceptBoth result completes exceptionally if either source cancelled
1904 >     * thenAcceptBoth result completes exceptionally if action does
1905       */
1906 <    public void testThenAcceptBoth4() {
1907 <        CompletableFuture<Integer> f, g;
1908 <        CompletableFuture<Void> h;
1909 <        SubtractAction r;
1910 <
1911 <        f = new CompletableFuture<>();
1912 <        g = new CompletableFuture<>();
1913 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1914 <        assertTrue(f.cancel(true));
1915 <        checkIncomplete(h);
1916 <        g.complete(1);
1917 <        checkCompletedWithWrappedCancellationException(h);
1918 <
1919 <        f = new CompletableFuture<>();
1920 <        g = new CompletableFuture<>();
1921 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1922 <        assertTrue(g.cancel(true));
1923 <        checkIncomplete(h);
1924 <        f.complete(3);
1925 <        checkCompletedWithWrappedCancellationException(h);
1926 <
1927 <        f = new CompletableFuture<>();
1928 <        g = new CompletableFuture<>();
1929 <        f.complete(3);
1930 <        assertTrue(g.cancel(true));
1931 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1932 <        checkCompletedWithWrappedCancellationException(h);
1933 <
1934 <        f = new CompletableFuture<>();
1935 <        g = new CompletableFuture<>();
1936 <        assertTrue(f.cancel(true));
1937 <        g.complete(3);
914 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
915 <        checkCompletedWithWrappedCancellationException(h);
916 <    }
1906 >    public void testThenAcceptBoth_actionFailed() {
1907 >        for (ExecutionMode m : ExecutionMode.values())
1908 >        for (boolean fFirst : new boolean[] { true, false })
1909 >        for (Integer v1 : new Integer[] { 1, null })
1910 >        for (Integer v2 : new Integer[] { 2, null })
1911 >    {
1912 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1913 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1914 >        final FailingBiConsumer r1 = new FailingBiConsumer(m);
1915 >        final FailingBiConsumer r2 = new FailingBiConsumer(m);
1916 >        final FailingBiConsumer r3 = new FailingBiConsumer(m);
1917 >
1918 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1919 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1920 >        final Integer w1 =  fFirst ? v1 : v2;
1921 >        final Integer w2 = !fFirst ? v1 : v2;
1922 >
1923 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1924 >        assertTrue(fst.complete(w1));
1925 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1926 >        assertTrue(snd.complete(w2));
1927 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1928 >
1929 >        checkCompletedWithWrappedCFException(h1);
1930 >        checkCompletedWithWrappedCFException(h2);
1931 >        checkCompletedWithWrappedCFException(h3);
1932 >        r1.assertInvoked();
1933 >        r2.assertInvoked();
1934 >        r3.assertInvoked();
1935 >        checkCompletedNormally(f, v1);
1936 >        checkCompletedNormally(g, v2);
1937 >    }}
1938  
1939      /**
1940       * runAfterBoth result completes normally after normal
1941       * completion of sources
1942       */
1943 <    public void testRunAfterBoth() {
1944 <        CompletableFuture<Integer> f, g;
1945 <        CompletableFuture<Void> h;
1946 <        Noop r;
1947 <
1948 <        f = new CompletableFuture<>();
1949 <        g = new CompletableFuture<>();
1950 <        h = f.runAfterBoth(g, r = new Noop());
1951 <        f.complete(3);
1952 <        checkIncomplete(h);
1953 <        g.complete(1);
1954 <        checkCompletedNormally(h, null);
1955 <        assertTrue(r.ran);
1956 <
1957 <        f = new CompletableFuture<>();
1958 <        g = new CompletableFuture<>();
1959 <        h = f.runAfterBoth(g, r = new Noop());
1960 <        g.complete(1);
1961 <        checkIncomplete(h);
1962 <        f.complete(3);
1963 <        checkCompletedNormally(h, null);
1964 <        assertTrue(r.ran);
1965 <
1966 <        f = new CompletableFuture<>();
1967 <        g = new CompletableFuture<>();
1968 <        g.complete(1);
1969 <        f.complete(3);
1970 <        h = f.runAfterBoth(g, r = new Noop());
1971 <        checkCompletedNormally(h, null);
1972 <        assertTrue(r.ran);
1973 <    }
1943 >    public void testRunAfterBoth_normalCompletion() {
1944 >        for (ExecutionMode m : ExecutionMode.values())
1945 >        for (boolean fFirst : new boolean[] { true, false })
1946 >        for (Integer v1 : new Integer[] { 1, null })
1947 >        for (Integer v2 : new Integer[] { 2, null })
1948 >    {
1949 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1950 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1951 >        final Noop r1 = new Noop(m);
1952 >        final Noop r2 = new Noop(m);
1953 >        final Noop r3 = new Noop(m);
1954 >
1955 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1956 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1957 >        final Integer w1 =  fFirst ? v1 : v2;
1958 >        final Integer w2 = !fFirst ? v1 : v2;
1959 >
1960 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1961 >        assertTrue(fst.complete(w1));
1962 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1963 >        checkIncomplete(h1);
1964 >        checkIncomplete(h2);
1965 >        r1.assertNotInvoked();
1966 >        r2.assertNotInvoked();
1967 >        assertTrue(snd.complete(w2));
1968 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
1969 >
1970 >        checkCompletedNormally(h1, null);
1971 >        checkCompletedNormally(h2, null);
1972 >        checkCompletedNormally(h3, null);
1973 >        r1.assertInvoked();
1974 >        r2.assertInvoked();
1975 >        r3.assertInvoked();
1976 >        checkCompletedNormally(f, v1);
1977 >        checkCompletedNormally(g, v2);
1978 >    }}
1979  
1980      /**
1981       * runAfterBoth result completes exceptionally after exceptional
1982       * completion of either source
1983       */
1984 <    public void testRunAfterBoth2() {
1985 <        CompletableFuture<Integer> f, g;
1986 <        CompletableFuture<Void> h;
1987 <        Noop r;
1988 <
1989 <        f = new CompletableFuture<>();
1990 <        g = new CompletableFuture<>();
1991 <        h = f.runAfterBoth(g, r = new Noop());
1992 <        f.completeExceptionally(new CFException());
1993 <        checkIncomplete(h);
1994 <        g.complete(1);
1995 <        checkCompletedWithWrappedCFException(h);
1996 <        assertFalse(r.ran);
1997 <
1998 <        f = new CompletableFuture<>();
1999 <        g = new CompletableFuture<>();
2000 <        h = f.runAfterBoth(g, r = new Noop());
2001 <        g.completeExceptionally(new CFException());
2002 <        checkIncomplete(h);
2003 <        f.complete(3);
2004 <        checkCompletedWithWrappedCFException(h);
2005 <        assertFalse(r.ran);
2006 <
2007 <        f = new CompletableFuture<>();
2008 <        g = new CompletableFuture<>();
2009 <        g.completeExceptionally(new CFException());
2010 <        f.complete(3);
2011 <        h = f.runAfterBoth(g, r = new Noop());
2012 <        checkCompletedWithWrappedCFException(h);
2013 <        assertFalse(r.ran);
2014 <
2015 <        f = new CompletableFuture<>();
2016 <        g = new CompletableFuture<>();
2017 <        f.completeExceptionally(new CFException());
2018 <        g.complete(1);
2019 <        h = f.runAfterBoth(g, r = new Noop());
2020 <        checkCompletedWithWrappedCFException(h);
2021 <        assertFalse(r.ran);
2022 <    }
1984 >    public void testRunAfterBoth_exceptionalCompletion() throws Throwable {
1985 >        for (ExecutionMode m : ExecutionMode.values())
1986 >        for (boolean fFirst : new boolean[] { true, false })
1987 >        for (boolean failFirst : new boolean[] { true, false })
1988 >        for (Integer v1 : new Integer[] { 1, null })
1989 >    {
1990 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1991 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1992 >        final CFException ex = new CFException();
1993 >        final Noop r1 = new Noop(m);
1994 >        final Noop r2 = new Noop(m);
1995 >        final Noop r3 = new Noop(m);
1996 >
1997 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1998 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1999 >        final Callable<Boolean> complete1 = failFirst ?
2000 >            () -> fst.completeExceptionally(ex) :
2001 >            () -> fst.complete(v1);
2002 >        final Callable<Boolean> complete2 = failFirst ?
2003 >            () -> snd.complete(v1) :
2004 >            () -> snd.completeExceptionally(ex);
2005 >
2006 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2007 >        assertTrue(complete1.call());
2008 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2009 >        checkIncomplete(h1);
2010 >        checkIncomplete(h2);
2011 >        assertTrue(complete2.call());
2012 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2013 >
2014 >        checkCompletedWithWrappedException(h1, ex);
2015 >        checkCompletedWithWrappedException(h2, ex);
2016 >        checkCompletedWithWrappedException(h3, ex);
2017 >        r1.assertNotInvoked();
2018 >        r2.assertNotInvoked();
2019 >        r3.assertNotInvoked();
2020 >        checkCompletedNormally(failFirst ? snd : fst, v1);
2021 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
2022 >    }}
2023  
2024      /**
2025 <     * runAfterBoth result completes exceptionally if action does
2025 >     * runAfterBoth result completes exceptionally if either source cancelled
2026       */
2027 <    public void testRunAfterBoth3() {
2028 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2029 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2030 <        FailingNoop r = new FailingNoop();
2031 <        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
2032 <        f.complete(one);
2033 <        checkIncomplete(g);
2034 <        f2.complete(two);
2035 <        checkCompletedWithWrappedCFException(g);
2036 <    }
2027 >    public void testRunAfterBoth_sourceCancelled() throws Throwable {
2028 >        for (ExecutionMode m : ExecutionMode.values())
2029 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2030 >        for (boolean fFirst : new boolean[] { true, false })
2031 >        for (boolean failFirst : new boolean[] { true, false })
2032 >        for (Integer v1 : new Integer[] { 1, null })
2033 >    {
2034 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2035 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2036 >        final Noop r1 = new Noop(m);
2037 >        final Noop r2 = new Noop(m);
2038 >        final Noop r3 = new Noop(m);
2039 >
2040 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2041 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2042 >        final Callable<Boolean> complete1 = failFirst ?
2043 >            () -> fst.cancel(mayInterruptIfRunning) :
2044 >            () -> fst.complete(v1);
2045 >        final Callable<Boolean> complete2 = failFirst ?
2046 >            () -> snd.complete(v1) :
2047 >            () -> snd.cancel(mayInterruptIfRunning);
2048 >
2049 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2050 >        assertTrue(complete1.call());
2051 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2052 >        checkIncomplete(h1);
2053 >        checkIncomplete(h2);
2054 >        assertTrue(complete2.call());
2055 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2056 >
2057 >        checkCompletedWithWrappedCancellationException(h1);
2058 >        checkCompletedWithWrappedCancellationException(h2);
2059 >        checkCompletedWithWrappedCancellationException(h3);
2060 >        r1.assertNotInvoked();
2061 >        r2.assertNotInvoked();
2062 >        r3.assertNotInvoked();
2063 >        checkCompletedNormally(failFirst ? snd : fst, v1);
2064 >        checkCancelled(failFirst ? fst : snd);
2065 >    }}
2066  
2067      /**
2068 <     * runAfterBoth result completes exceptionally if either source cancelled
2068 >     * runAfterBoth result completes exceptionally if action does
2069       */
2070 <    public void testRunAfterBoth4() {
2071 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2072 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2073 <        Noop r = new Noop();
2074 <        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
2075 <        assertTrue(f.cancel(true));
2076 <        f2.complete(two);
2077 <        checkCompletedWithWrappedCancellationException(g);
2078 <        f = new CompletableFuture<>();
2079 <        f2 = new CompletableFuture<>();
2080 <        r = new Noop();
2081 <        g = f.runAfterBoth(f2, r);
2082 <        f.complete(one);
2083 <        assertTrue(f2.cancel(true));
2084 <        checkCompletedWithWrappedCancellationException(g);
2085 <    }
2070 >    public void testRunAfterBoth_actionFailed() {
2071 >        for (ExecutionMode m : ExecutionMode.values())
2072 >        for (boolean fFirst : new boolean[] { true, false })
2073 >        for (Integer v1 : new Integer[] { 1, null })
2074 >        for (Integer v2 : new Integer[] { 2, null })
2075 >    {
2076 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2077 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2078 >        final FailingRunnable r1 = new FailingRunnable(m);
2079 >        final FailingRunnable r2 = new FailingRunnable(m);
2080 >        final FailingRunnable r3 = new FailingRunnable(m);
2081 >
2082 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2083 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2084 >        final Integer w1 =  fFirst ? v1 : v2;
2085 >        final Integer w2 = !fFirst ? v1 : v2;
2086 >
2087 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2088 >        assertTrue(fst.complete(w1));
2089 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2090 >        assertTrue(snd.complete(w2));
2091 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2092 >
2093 >        checkCompletedWithWrappedCFException(h1);
2094 >        checkCompletedWithWrappedCFException(h2);
2095 >        checkCompletedWithWrappedCFException(h3);
2096 >        r1.assertInvoked();
2097 >        r2.assertInvoked();
2098 >        r3.assertInvoked();
2099 >        checkCompletedNormally(f, v1);
2100 >        checkCompletedNormally(g, v2);
2101 >    }}
2102  
2103      /**
2104       * applyToEither result completes normally after normal completion
2105       * of either source
2106       */
2107 <    public void testApplyToEither() {
2108 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2109 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2110 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
2111 <        f.complete(one);
2112 <        checkCompletedNormally(g, two);
2113 <        f2.complete(one);
2114 <        checkCompletedNormally(g, two);
2115 <
2116 <        f = new CompletableFuture<>();
2117 <        f.complete(one);
2118 <        f2 = new CompletableFuture<>();
2119 <        g = f.applyToEither(f2, inc);
2120 <        checkCompletedNormally(g, two);
2121 <    }
2107 >    public void testApplyToEither_normalCompletion() {
2108 >        for (ExecutionMode m : ExecutionMode.values())
2109 >        for (Integer v1 : new Integer[] { 1, null })
2110 >        for (Integer v2 : new Integer[] { 2, null })
2111 >    {
2112 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2113 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2114 >        final IncFunction[] rs = new IncFunction[6];
2115 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2116 >
2117 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2118 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2119 >        checkIncomplete(h0);
2120 >        checkIncomplete(h1);
2121 >        rs[0].assertNotInvoked();
2122 >        rs[1].assertNotInvoked();
2123 >        f.complete(v1);
2124 >        checkCompletedNormally(h0, inc(v1));
2125 >        checkCompletedNormally(h1, inc(v1));
2126 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2127 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2128 >        checkCompletedNormally(h2, inc(v1));
2129 >        checkCompletedNormally(h3, inc(v1));
2130 >        g.complete(v2);
2131 >
2132 >        // unspecified behavior - both source completions available
2133 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2134 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2135 >        rs[4].assertValue(h4.join());
2136 >        rs[5].assertValue(h5.join());
2137 >        assertTrue(Objects.equals(inc(v1), h4.join()) ||
2138 >                   Objects.equals(inc(v2), h4.join()));
2139 >        assertTrue(Objects.equals(inc(v1), h5.join()) ||
2140 >                   Objects.equals(inc(v2), h5.join()));
2141 >
2142 >        checkCompletedNormally(f, v1);
2143 >        checkCompletedNormally(g, v2);
2144 >        checkCompletedNormally(h0, inc(v1));
2145 >        checkCompletedNormally(h1, inc(v1));
2146 >        checkCompletedNormally(h2, inc(v1));
2147 >        checkCompletedNormally(h3, inc(v1));
2148 >        for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1));
2149 >    }}
2150  
2151      /**
2152       * applyToEither result completes exceptionally after exceptional
2153       * completion of either source
2154       */
2155 <    public void testApplyToEither2() {
2156 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2157 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2158 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
2159 <        f.completeExceptionally(new CFException());
2160 <        f2.complete(one);
2161 <        checkCompletedWithWrappedCFException(g);
2155 >    public void testApplyToEither_exceptionalCompletion() {
2156 >        for (ExecutionMode m : ExecutionMode.values())
2157 >        for (Integer v1 : new Integer[] { 1, null })
2158 >    {
2159 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2160 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2161 >        final CFException ex = new CFException();
2162 >        final IncFunction[] rs = new IncFunction[6];
2163 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2164 >
2165 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2166 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2167 >        checkIncomplete(h0);
2168 >        checkIncomplete(h1);
2169 >        rs[0].assertNotInvoked();
2170 >        rs[1].assertNotInvoked();
2171 >        f.completeExceptionally(ex);
2172 >        checkCompletedWithWrappedException(h0, ex);
2173 >        checkCompletedWithWrappedException(h1, ex);
2174 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2175 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2176 >        checkCompletedWithWrappedException(h2, ex);
2177 >        checkCompletedWithWrappedException(h3, ex);
2178 >        g.complete(v1);
2179 >
2180 >        // unspecified behavior - both source completions available
2181 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2182 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2183 >        try {
2184 >            assertEquals(inc(v1), h4.join());
2185 >            rs[4].assertValue(inc(v1));
2186 >        } catch (CompletionException ok) {
2187 >            checkCompletedWithWrappedException(h4, ex);
2188 >            rs[4].assertNotInvoked();
2189 >        }
2190 >        try {
2191 >            assertEquals(inc(v1), h5.join());
2192 >            rs[5].assertValue(inc(v1));
2193 >        } catch (CompletionException ok) {
2194 >            checkCompletedWithWrappedException(h5, ex);
2195 >            rs[5].assertNotInvoked();
2196 >        }
2197  
2198 <        f = new CompletableFuture<>();
2199 <        f2 = new CompletableFuture<>();
2200 <        f2.completeExceptionally(new CFException());
2201 <        g = f.applyToEither(f2, inc);
2202 <        checkCompletedWithWrappedCFException(g);
2203 <    }
2198 >        checkCompletedExceptionally(f, ex);
2199 >        checkCompletedNormally(g, v1);
2200 >        checkCompletedWithWrappedException(h0, ex);
2201 >        checkCompletedWithWrappedException(h1, ex);
2202 >        checkCompletedWithWrappedException(h2, ex);
2203 >        checkCompletedWithWrappedException(h3, ex);
2204 >        checkCompletedWithWrappedException(h4, ex);
2205 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2206 >    }}
2207 >
2208 >    public void testApplyToEither_exceptionalCompletion2() {
2209 >        for (ExecutionMode m : ExecutionMode.values())
2210 >        for (boolean fFirst : new boolean[] { true, false })
2211 >        for (Integer v1 : new Integer[] { 1, null })
2212 >    {
2213 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2214 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2215 >        final CFException ex = new CFException();
2216 >        final IncFunction[] rs = new IncFunction[6];
2217 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2218 >
2219 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2220 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2221 >        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2222 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2223 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2224 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2225  
2226 <    /**
2227 <     * applyToEither result completes exceptionally if action does
2228 <     */
2229 <    public void testApplyToEither3() {
2230 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2231 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2232 <        FailingFunction r = new FailingFunction();
2233 <        CompletableFuture<Integer> g = f.applyToEither(f2, r);
2234 <        f2.complete(two);
2235 <        checkCompletedWithWrappedCFException(g);
2236 <    }
2226 >        // unspecified behavior - both source completions available
2227 >        try {
2228 >            assertEquals(inc(v1), h0.join());
2229 >            rs[0].assertValue(inc(v1));
2230 >        } catch (CompletionException ok) {
2231 >            checkCompletedWithWrappedException(h0, ex);
2232 >            rs[0].assertNotInvoked();
2233 >        }
2234 >        try {
2235 >            assertEquals(inc(v1), h1.join());
2236 >            rs[1].assertValue(inc(v1));
2237 >        } catch (CompletionException ok) {
2238 >            checkCompletedWithWrappedException(h1, ex);
2239 >            rs[1].assertNotInvoked();
2240 >        }
2241 >        try {
2242 >            assertEquals(inc(v1), h2.join());
2243 >            rs[2].assertValue(inc(v1));
2244 >        } catch (CompletionException ok) {
2245 >            checkCompletedWithWrappedException(h2, ex);
2246 >            rs[2].assertNotInvoked();
2247 >        }
2248 >        try {
2249 >            assertEquals(inc(v1), h3.join());
2250 >            rs[3].assertValue(inc(v1));
2251 >        } catch (CompletionException ok) {
2252 >            checkCompletedWithWrappedException(h3, ex);
2253 >            rs[3].assertNotInvoked();
2254 >        }
2255  
2256 <    /**
2257 <     * applyToEither result completes exceptionally if either source cancelled
2258 <     */
1086 <    public void testApplyToEither4() {
1087 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1088 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1089 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1090 <        assertTrue(f.cancel(true));
1091 <        checkCompletedWithWrappedCancellationException(g);
1092 <        f = new CompletableFuture<>();
1093 <        f2 = new CompletableFuture<>();
1094 <        assertTrue(f2.cancel(true));
1095 <        checkCompletedWithWrappedCancellationException(g);
1096 <    }
2256 >        checkCompletedNormally(f, v1);
2257 >        checkCompletedExceptionally(g, ex);
2258 >    }}
2259  
2260      /**
2261 <     * acceptEither result completes normally after normal completion
1100 <     * of either source
2261 >     * applyToEither result completes exceptionally if either source cancelled
2262       */
2263 <    public void testAcceptEither() {
2264 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2265 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2266 <        IncAction r = new IncAction();
2267 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2268 <        f.complete(one);
2269 <        checkCompletedNormally(g, null);
2270 <        f2.complete(one);
2271 <        checkCompletedNormally(g, null);
2272 <        assertEquals(r.value, 2);
2273 <
2274 <        r = new IncAction();
2275 <        f = new CompletableFuture<>();
2276 <        f.complete(one);
2277 <        f2 = new CompletableFuture<>();
2278 <        g = f.acceptEither(f2, r);
2279 <        checkCompletedNormally(g, null);
2280 <        assertEquals(r.value, 2);
2281 <    }
2263 >    public void testApplyToEither_sourceCancelled() {
2264 >        for (ExecutionMode m : ExecutionMode.values())
2265 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2266 >        for (Integer v1 : new Integer[] { 1, null })
2267 >    {
2268 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2269 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2270 >        final IncFunction[] rs = new IncFunction[6];
2271 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2272 >
2273 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2274 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2275 >        checkIncomplete(h0);
2276 >        checkIncomplete(h1);
2277 >        rs[0].assertNotInvoked();
2278 >        rs[1].assertNotInvoked();
2279 >        f.cancel(mayInterruptIfRunning);
2280 >        checkCompletedWithWrappedCancellationException(h0);
2281 >        checkCompletedWithWrappedCancellationException(h1);
2282 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2283 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2284 >        checkCompletedWithWrappedCancellationException(h2);
2285 >        checkCompletedWithWrappedCancellationException(h3);
2286 >        g.complete(v1);
2287 >
2288 >        // unspecified behavior - both source completions available
2289 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2290 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2291 >        try {
2292 >            assertEquals(inc(v1), h4.join());
2293 >            rs[4].assertValue(inc(v1));
2294 >        } catch (CompletionException ok) {
2295 >            checkCompletedWithWrappedCancellationException(h4);
2296 >            rs[4].assertNotInvoked();
2297 >        }
2298 >        try {
2299 >            assertEquals(inc(v1), h5.join());
2300 >            rs[5].assertValue(inc(v1));
2301 >        } catch (CompletionException ok) {
2302 >            checkCompletedWithWrappedCancellationException(h5);
2303 >            rs[5].assertNotInvoked();
2304 >        }
2305  
2306 <    /**
2307 <     * acceptEither result completes exceptionally after exceptional
2308 <     * completion of either source
2309 <     */
2310 <    public void testAcceptEither2() {
2311 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2312 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2313 <        IncAction r = new IncAction();
2314 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2315 <        f.completeExceptionally(new CFException());
2316 <        f2.complete(one);
2317 <        checkCompletedWithWrappedCFException(g);
2306 >        checkCancelled(f);
2307 >        checkCompletedNormally(g, v1);
2308 >        checkCompletedWithWrappedCancellationException(h0);
2309 >        checkCompletedWithWrappedCancellationException(h1);
2310 >        checkCompletedWithWrappedCancellationException(h2);
2311 >        checkCompletedWithWrappedCancellationException(h3);
2312 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2313 >    }}
2314 >
2315 >    public void testApplyToEither_sourceCancelled2() {
2316 >        for (ExecutionMode m : ExecutionMode.values())
2317 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2318 >        for (boolean fFirst : new boolean[] { true, false })
2319 >        for (Integer v1 : new Integer[] { 1, null })
2320 >    {
2321 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2322 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2323 >        final IncFunction[] rs = new IncFunction[6];
2324 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2325 >
2326 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2327 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2328 >        assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2329 >        assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2330 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2331 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2332  
2333 <        r = new IncAction();
2334 <        f = new CompletableFuture<>();
2335 <        f2 = new CompletableFuture<>();
2336 <        f2.completeExceptionally(new CFException());
2337 <        g = f.acceptEither(f2, r);
2338 <        checkCompletedWithWrappedCFException(g);
2339 <    }
2333 >        // unspecified behavior - both source completions available
2334 >        try {
2335 >            assertEquals(inc(v1), h0.join());
2336 >            rs[0].assertValue(inc(v1));
2337 >        } catch (CompletionException ok) {
2338 >            checkCompletedWithWrappedCancellationException(h0);
2339 >            rs[0].assertNotInvoked();
2340 >        }
2341 >        try {
2342 >            assertEquals(inc(v1), h1.join());
2343 >            rs[1].assertValue(inc(v1));
2344 >        } catch (CompletionException ok) {
2345 >            checkCompletedWithWrappedCancellationException(h1);
2346 >            rs[1].assertNotInvoked();
2347 >        }
2348 >        try {
2349 >            assertEquals(inc(v1), h2.join());
2350 >            rs[2].assertValue(inc(v1));
2351 >        } catch (CompletionException ok) {
2352 >            checkCompletedWithWrappedCancellationException(h2);
2353 >            rs[2].assertNotInvoked();
2354 >        }
2355 >        try {
2356 >            assertEquals(inc(v1), h3.join());
2357 >            rs[3].assertValue(inc(v1));
2358 >        } catch (CompletionException ok) {
2359 >            checkCompletedWithWrappedCancellationException(h3);
2360 >            rs[3].assertNotInvoked();
2361 >        }
2362  
2363 <    /**
2364 <     * acceptEither result completes exceptionally if action does
2365 <     */
1146 <    public void testAcceptEither3() {
1147 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1148 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1149 <        FailingConsumer r = new FailingConsumer();
1150 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1151 <        f2.complete(two);
1152 <        checkCompletedWithWrappedCFException(g);
1153 <    }
2363 >        checkCompletedNormally(f, v1);
2364 >        checkCancelled(g);
2365 >    }}
2366  
2367      /**
2368 <     * acceptEither result completes exceptionally if either source cancelled
2368 >     * applyToEither result completes exceptionally if action does
2369       */
2370 <    public void testAcceptEither4() {
2371 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2372 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2373 <        IncAction r = new IncAction();
2374 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2375 <        assertTrue(f.cancel(true));
2376 <        checkCompletedWithWrappedCancellationException(g);
2377 <        f = new CompletableFuture<>();
2378 <        f2 = new CompletableFuture<>();
2379 <        assertTrue(f2.cancel(true));
2380 <        checkCompletedWithWrappedCancellationException(g);
2381 <    }
2370 >    public void testApplyToEither_actionFailed() {
2371 >        for (ExecutionMode m : ExecutionMode.values())
2372 >        for (Integer v1 : new Integer[] { 1, null })
2373 >        for (Integer v2 : new Integer[] { 2, null })
2374 >    {
2375 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2376 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2377 >        final FailingFunction[] rs = new FailingFunction[6];
2378 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
2379 >
2380 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2381 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2382 >        f.complete(v1);
2383 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2384 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2385 >        checkCompletedWithWrappedCFException(h0);
2386 >        checkCompletedWithWrappedCFException(h1);
2387 >        checkCompletedWithWrappedCFException(h2);
2388 >        checkCompletedWithWrappedCFException(h3);
2389 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2390 >
2391 >        g.complete(v2);
2392 >
2393 >        // unspecified behavior - both source completions available
2394 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2395 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2396 >
2397 >        checkCompletedWithWrappedCFException(h4);
2398 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2399 >                   Objects.equals(v2, rs[4].value));
2400 >        checkCompletedWithWrappedCFException(h5);
2401 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2402 >                   Objects.equals(v2, rs[5].value));
2403 >
2404 >        checkCompletedNormally(f, v1);
2405 >        checkCompletedNormally(g, v2);
2406 >    }}
2407  
2408      /**
2409 <     * runAfterEither result completes normally after normal completion
2409 >     * acceptEither result completes normally after normal completion
2410       * of either source
2411       */
2412 <    public void testRunAfterEither() {
2413 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2414 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2415 <        Noop r = new Noop();
2416 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2417 <        f.complete(one);
2418 <        checkCompletedNormally(g, null);
2419 <        f2.complete(one);
2420 <        checkCompletedNormally(g, null);
2421 <        assertTrue(r.ran);
2422 <
2423 <        r = new Noop();
2424 <        f = new CompletableFuture<>();
2425 <        f.complete(one);
2426 <        f2 = new CompletableFuture<>();
2427 <        g = f.runAfterEither(f2, r);
2428 <        checkCompletedNormally(g, null);
2429 <        assertTrue(r.ran);
2430 <    }
2412 >    public void testAcceptEither_normalCompletion() {
2413 >        for (ExecutionMode m : ExecutionMode.values())
2414 >        for (Integer v1 : new Integer[] { 1, null })
2415 >        for (Integer v2 : new Integer[] { 2, null })
2416 >    {
2417 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2418 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2419 >        final NoopConsumer[] rs = new NoopConsumer[6];
2420 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2421 >
2422 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2423 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2424 >        checkIncomplete(h0);
2425 >        checkIncomplete(h1);
2426 >        rs[0].assertNotInvoked();
2427 >        rs[1].assertNotInvoked();
2428 >        f.complete(v1);
2429 >        checkCompletedNormally(h0, null);
2430 >        checkCompletedNormally(h1, null);
2431 >        rs[0].assertValue(v1);
2432 >        rs[1].assertValue(v1);
2433 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2434 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2435 >        checkCompletedNormally(h2, null);
2436 >        checkCompletedNormally(h3, null);
2437 >        rs[2].assertValue(v1);
2438 >        rs[3].assertValue(v1);
2439 >        g.complete(v2);
2440 >
2441 >        // unspecified behavior - both source completions available
2442 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2443 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2444 >        checkCompletedNormally(h4, null);
2445 >        checkCompletedNormally(h5, null);
2446 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2447 >                   Objects.equals(v2, rs[4].value));
2448 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2449 >                   Objects.equals(v2, rs[5].value));
2450 >
2451 >        checkCompletedNormally(f, v1);
2452 >        checkCompletedNormally(g, v2);
2453 >        checkCompletedNormally(h0, null);
2454 >        checkCompletedNormally(h1, null);
2455 >        checkCompletedNormally(h2, null);
2456 >        checkCompletedNormally(h3, null);
2457 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2458 >    }}
2459  
2460      /**
2461 <     * runAfterEither result completes exceptionally after exceptional
2461 >     * acceptEither result completes exceptionally after exceptional
2462       * completion of either source
2463       */
2464 <    public void testRunAfterEither2() {
2465 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2466 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2467 <        Noop r = new Noop();
2468 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2469 <        f.completeExceptionally(new CFException());
2470 <        f2.complete(one);
2471 <        checkCompletedWithWrappedCFException(g);
2472 <
2473 <        r = new Noop();
2474 <        f = new CompletableFuture<>();
2475 <        f2 = new CompletableFuture<>();
2476 <        f2.completeExceptionally(new CFException());
2477 <        g = f.runAfterEither(f2, r);
2478 <        checkCompletedWithWrappedCFException(g);
2479 <    }
2480 <
2481 <    /**
2482 <     * runAfterEither result completes exceptionally if action does
2483 <     */
2484 <    public void testRunAfterEither3() {
2485 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2486 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2487 <        FailingNoop r = new FailingNoop();
2488 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2489 <        f2.complete(two);
2490 <        checkCompletedWithWrappedCFException(g);
2491 <    }
2492 <
1228 <    /**
1229 <     * runAfterEither result completes exceptionally if either source cancelled
1230 <     */
1231 <    public void testRunAfterEither4() {
1232 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1233 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1234 <        Noop r = new Noop();
1235 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1236 <        assertTrue(f.cancel(true));
1237 <        checkCompletedWithWrappedCancellationException(g);
1238 <        f = new CompletableFuture<>();
1239 <        f2 = new CompletableFuture<>();
1240 <        assertTrue(f2.cancel(true));
1241 <        checkCompletedWithWrappedCancellationException(g);
1242 <    }
1243 <
1244 <    /**
1245 <     * thenCompose result completes normally after normal completion of source
1246 <     */
1247 <    public void testThenCompose() {
1248 <        CompletableFuture<Integer> f, g;
1249 <        CompletableFutureInc r;
1250 <
1251 <        f = new CompletableFuture<>();
1252 <        g = f.thenCompose(r = new CompletableFutureInc());
1253 <        f.complete(one);
1254 <        checkCompletedNormally(g, two);
1255 <        assertTrue(r.ran);
1256 <
1257 <        f = new CompletableFuture<>();
1258 <        f.complete(one);
1259 <        g = f.thenCompose(r = new CompletableFutureInc());
1260 <        checkCompletedNormally(g, two);
1261 <        assertTrue(r.ran);
1262 <    }
1263 <
1264 <    /**
1265 <     * thenCompose result completes exceptionally after exceptional
1266 <     * completion of source
1267 <     */
1268 <    public void testThenCompose2() {
1269 <        CompletableFuture<Integer> f, g;
1270 <        CompletableFutureInc r;
1271 <
1272 <        f = new CompletableFuture<>();
1273 <        g = f.thenCompose(r = new CompletableFutureInc());
1274 <        f.completeExceptionally(new CFException());
1275 <        checkCompletedWithWrappedCFException(g);
1276 <
1277 <        f = new CompletableFuture<>();
1278 <        f.completeExceptionally(new CFException());
1279 <        g = f.thenCompose(r = new CompletableFutureInc());
1280 <        checkCompletedWithWrappedCFException(g);
1281 <    }
1282 <
1283 <    /**
1284 <     * thenCompose result completes exceptionally if action does
1285 <     */
1286 <    public void testThenCompose3() {
1287 <        CompletableFuture<Integer> f, g;
1288 <        FailingCompletableFutureFunction r;
1289 <
1290 <        f = new CompletableFuture<>();
1291 <        g = f.thenCompose(r = new FailingCompletableFutureFunction());
1292 <        f.complete(one);
1293 <        checkCompletedWithWrappedCFException(g);
1294 <
1295 <        f = new CompletableFuture<>();
1296 <        f.complete(one);
1297 <        g = f.thenCompose(r = new FailingCompletableFutureFunction());
1298 <        checkCompletedWithWrappedCFException(g);
1299 <    }
1300 <
1301 <    /**
1302 <     * thenCompose result completes exceptionally if source cancelled
1303 <     */
1304 <    public void testThenCompose4() {
1305 <        CompletableFuture<Integer> f, g;
1306 <        CompletableFutureInc r;
1307 <
1308 <        f = new CompletableFuture<>();
1309 <        g = f.thenCompose(r = new CompletableFutureInc());
1310 <        assertTrue(f.cancel(true));
1311 <        checkCompletedWithWrappedCancellationException(g);
1312 <
1313 <        f = new CompletableFuture<>();
1314 <        assertTrue(f.cancel(true));
1315 <        g = f.thenCompose(r = new CompletableFutureInc());
1316 <        checkCompletedWithWrappedCancellationException(g);
1317 <    }
1318 <
1319 <    // asyncs
1320 <
1321 <    /**
1322 <     * thenRunAsync result completes normally after normal completion of source
1323 <     */
1324 <    public void testThenRunAsync() {
1325 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1326 <        Noop r = new Noop();
1327 <        CompletableFuture<Void> g = f.thenRunAsync(r);
1328 <        f.complete(null);
1329 <        checkCompletedNormally(g, null);
1330 <
1331 <        // reordered version
1332 <        f = new CompletableFuture<>();
1333 <        f.complete(null);
1334 <        r = new Noop();
1335 <        g = f.thenRunAsync(r);
1336 <        checkCompletedNormally(g, null);
1337 <    }
1338 <
1339 <    /**
1340 <     * thenRunAsync result completes exceptionally after exceptional
1341 <     * completion of source
1342 <     */
1343 <    public void testThenRunAsync2() {
1344 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1345 <        Noop r = new Noop();
1346 <        CompletableFuture<Void> g = f.thenRunAsync(r);
1347 <        f.completeExceptionally(new CFException());
2464 >    public void testAcceptEither_exceptionalCompletion() {
2465 >        for (ExecutionMode m : ExecutionMode.values())
2466 >        for (Integer v1 : new Integer[] { 1, null })
2467 >    {
2468 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2469 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2470 >        final CFException ex = new CFException();
2471 >        final NoopConsumer[] rs = new NoopConsumer[6];
2472 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2473 >
2474 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2475 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2476 >        checkIncomplete(h0);
2477 >        checkIncomplete(h1);
2478 >        rs[0].assertNotInvoked();
2479 >        rs[1].assertNotInvoked();
2480 >        f.completeExceptionally(ex);
2481 >        checkCompletedWithWrappedException(h0, ex);
2482 >        checkCompletedWithWrappedException(h1, ex);
2483 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2484 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2485 >        checkCompletedWithWrappedException(h2, ex);
2486 >        checkCompletedWithWrappedException(h3, ex);
2487 >
2488 >        g.complete(v1);
2489 >
2490 >        // unspecified behavior - both source completions available
2491 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2492 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2493          try {
2494 <            g.join();
2495 <            shouldThrow();
2496 <        } catch (Exception ok) {
2494 >            assertNull(h4.join());
2495 >            rs[4].assertValue(v1);
2496 >        } catch (CompletionException ok) {
2497 >            checkCompletedWithWrappedException(h4, ex);
2498 >            rs[4].assertNotInvoked();
2499          }
1353        checkCompletedWithWrappedCFException(g);
1354    }
1355
1356    /**
1357     * thenRunAsync result completes exceptionally if action does
1358     */
1359    public void testThenRunAsync3() {
1360        CompletableFuture<Integer> f = new CompletableFuture<>();
1361        FailingNoop r = new FailingNoop();
1362        CompletableFuture<Void> g = f.thenRunAsync(r);
1363        f.complete(null);
1364        checkCompletedWithWrappedCFException(g);
1365    }
1366
1367    /**
1368     * thenRunAsync result completes exceptionally if source cancelled
1369     */
1370    public void testThenRunAsync4() {
1371        CompletableFuture<Integer> f = new CompletableFuture<>();
1372        Noop r = new Noop();
1373        CompletableFuture<Void> g = f.thenRunAsync(r);
1374        assertTrue(f.cancel(true));
1375        checkCompletedWithWrappedCancellationException(g);
1376    }
1377
1378    /**
1379     * thenApplyAsync result completes normally after normal completion of source
1380     */
1381    public void testThenApplyAsync() {
1382        CompletableFuture<Integer> f = new CompletableFuture<>();
1383        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1384        f.complete(one);
1385        checkCompletedNormally(g, two);
1386    }
1387
1388    /**
1389     * thenApplyAsync result completes exceptionally after exceptional
1390     * completion of source
1391     */
1392    public void testThenApplyAsync2() {
1393        CompletableFuture<Integer> f = new CompletableFuture<>();
1394        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1395        f.completeExceptionally(new CFException());
1396        checkCompletedWithWrappedCFException(g);
1397    }
1398
1399    /**
1400     * thenApplyAsync result completes exceptionally if action does
1401     */
1402    public void testThenApplyAsync3() {
1403        CompletableFuture<Integer> f = new CompletableFuture<>();
1404        FailingFunction r = new FailingFunction();
1405        CompletableFuture<Integer> g = f.thenApplyAsync(r);
1406        f.complete(null);
1407        checkCompletedWithWrappedCFException(g);
1408    }
1409
1410    /**
1411     * thenApplyAsync result completes exceptionally if source cancelled
1412     */
1413    public void testThenApplyAsync4() {
1414        CompletableFuture<Integer> f = new CompletableFuture<>();
1415        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1416        assertTrue(f.cancel(true));
1417        checkCompletedWithWrappedCancellationException(g);
1418    }
1419
1420    /**
1421     * thenAcceptAsync result completes normally after normal
1422     * completion of source
1423     */
1424    public void testThenAcceptAsync() {
1425        CompletableFuture<Integer> f = new CompletableFuture<>();
1426        IncAction r = new IncAction();
1427        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1428        f.complete(one);
1429        checkCompletedNormally(g, null);
1430        assertEquals(r.value, 2);
1431    }
1432
1433    /**
1434     * thenAcceptAsync result completes exceptionally after exceptional
1435     * completion of source
1436     */
1437    public void testThenAcceptAsync2() {
1438        CompletableFuture<Integer> f = new CompletableFuture<>();
1439        IncAction r = new IncAction();
1440        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1441        f.completeExceptionally(new CFException());
1442        checkCompletedWithWrappedCFException(g);
1443    }
1444
1445    /**
1446     * thenAcceptAsync result completes exceptionally if action does
1447     */
1448    public void testThenAcceptAsync3() {
1449        CompletableFuture<Integer> f = new CompletableFuture<>();
1450        FailingConsumer r = new FailingConsumer();
1451        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1452        f.complete(null);
1453        checkCompletedWithWrappedCFException(g);
1454    }
1455
1456    /**
1457     * thenAcceptAsync result completes exceptionally if source cancelled
1458     */
1459    public void testThenAcceptAsync4() {
1460        CompletableFuture<Integer> f = new CompletableFuture<>();
1461        IncAction r = new IncAction();
1462        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1463        assertTrue(f.cancel(true));
1464        checkCompletedWithWrappedCancellationException(g);
1465    }
1466
1467    /**
1468     * thenCombineAsync result completes normally after normal
1469     * completion of sources
1470     */
1471    public void testThenCombineAsync() {
1472        CompletableFuture<Integer> f, g, h;
1473
1474        f = new CompletableFuture<>();
1475        g = new CompletableFuture<>();
1476        h = f.thenCombineAsync(g, subtract);
1477        f.complete(3);
1478        checkIncomplete(h);
1479        g.complete(1);
1480        checkCompletedNormally(h, 2);
1481
1482        f = new CompletableFuture<>();
1483        g = new CompletableFuture<>();
1484        h = f.thenCombineAsync(g, subtract);
1485        g.complete(1);
1486        checkIncomplete(h);
1487        f.complete(3);
1488        checkCompletedNormally(h, 2);
1489
1490        f = new CompletableFuture<>();
1491        g = new CompletableFuture<>();
1492        g.complete(1);
1493        f.complete(3);
1494        h = f.thenCombineAsync(g, subtract);
1495        checkCompletedNormally(h, 2);
1496    }
1497
1498    /**
1499     * thenCombineAsync result completes exceptionally after exceptional
1500     * completion of either source
1501     */
1502    public void testThenCombineAsync2() {
1503        CompletableFuture<Integer> f, g, h;
1504
1505        f = new CompletableFuture<>();
1506        g = new CompletableFuture<>();
1507        h = f.thenCombineAsync(g, subtract);
1508        f.completeExceptionally(new CFException());
1509        checkIncomplete(h);
1510        g.complete(1);
1511        checkCompletedWithWrappedCFException(h);
1512
1513        f = new CompletableFuture<>();
1514        g = new CompletableFuture<>();
1515        h = f.thenCombineAsync(g, subtract);
1516        g.completeExceptionally(new CFException());
1517        checkIncomplete(h);
1518        f.complete(3);
1519        checkCompletedWithWrappedCFException(h);
1520
1521        f = new CompletableFuture<>();
1522        g = new CompletableFuture<>();
1523        g.completeExceptionally(new CFException());
1524        f.complete(3);
1525        h = f.thenCombineAsync(g, subtract);
1526        checkCompletedWithWrappedCFException(h);
1527    }
1528
1529    /**
1530     * thenCombineAsync result completes exceptionally if action does
1531     */
1532    public void testThenCombineAsync3() {
1533        CompletableFuture<Integer> f = new CompletableFuture<>();
1534        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1535        FailingBiFunction r = new FailingBiFunction();
1536        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r);
1537        f.complete(one);
1538        checkIncomplete(g);
1539        assertFalse(r.ran);
1540        f2.complete(two);
1541        checkCompletedWithWrappedCFException(g);
1542        assertTrue(r.ran);
1543    }
1544
1545    /**
1546     * thenCombineAsync result completes exceptionally if either source cancelled
1547     */
1548    public void testThenCombineAsync4() {
1549        CompletableFuture<Integer> f, g, h;
1550
1551        f = new CompletableFuture<>();
1552        g = new CompletableFuture<>();
1553        h = f.thenCombineAsync(g, subtract);
1554        assertTrue(f.cancel(true));
1555        checkIncomplete(h);
1556        g.complete(1);
1557        checkCompletedWithWrappedCancellationException(h);
1558
1559        f = new CompletableFuture<>();
1560        g = new CompletableFuture<>();
1561        h = f.thenCombineAsync(g, subtract);
1562        assertTrue(g.cancel(true));
1563        checkIncomplete(h);
1564        f.complete(3);
1565        checkCompletedWithWrappedCancellationException(h);
1566
1567        f = new CompletableFuture<>();
1568        g = new CompletableFuture<>();
1569        g.complete(3);
1570        assertTrue(f.cancel(true));
1571        h = f.thenCombineAsync(g, subtract);
1572        checkCompletedWithWrappedCancellationException(h);
1573
1574        f = new CompletableFuture<>();
1575        g = new CompletableFuture<>();
1576        f.complete(3);
1577        assertTrue(g.cancel(true));
1578        h = f.thenCombineAsync(g, subtract);
1579        checkCompletedWithWrappedCancellationException(h);
1580    }
1581
1582    /**
1583     * thenAcceptBothAsync result completes normally after normal
1584     * completion of sources
1585     */
1586    public void testThenAcceptBothAsync() {
1587        CompletableFuture<Integer> f, g;
1588        CompletableFuture<Void> h;
1589        SubtractAction r;
1590
1591        f = new CompletableFuture<>();
1592        g = new CompletableFuture<>();
1593        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1594        f.complete(3);
1595        checkIncomplete(h);
1596        g.complete(1);
1597        checkCompletedNormally(h, null);
1598        assertEquals(r.value, 2);
1599
1600        f = new CompletableFuture<>();
1601        g = new CompletableFuture<>();
1602        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1603        g.complete(1);
1604        checkIncomplete(h);
1605        f.complete(3);
1606        checkCompletedNormally(h, null);
1607        assertEquals(r.value, 2);
1608
1609        f = new CompletableFuture<>();
1610        g = new CompletableFuture<>();
1611        g.complete(1);
1612        f.complete(3);
1613        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1614        checkCompletedNormally(h, null);
1615        assertEquals(r.value, 2);
1616    }
1617
1618    /**
1619     * thenAcceptBothAsync result completes exceptionally after exceptional
1620     * completion of source
1621     */
1622    public void testThenAcceptBothAsync2() {
1623        CompletableFuture<Integer> f, g;
1624        CompletableFuture<Void> h;
1625        SubtractAction r;
1626
1627        f = new CompletableFuture<>();
1628        g = new CompletableFuture<>();
1629        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1630        f.completeExceptionally(new CFException());
1631        checkIncomplete(h);
1632        g.complete(1);
1633        checkCompletedWithWrappedCFException(h);
1634
1635        f = new CompletableFuture<>();
1636        g = new CompletableFuture<>();
1637        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1638        g.completeExceptionally(new CFException());
1639        checkIncomplete(h);
1640        f.complete(3);
1641        checkCompletedWithWrappedCFException(h);
1642
1643        f = new CompletableFuture<>();
1644        g = new CompletableFuture<>();
1645        f.complete(3);
1646        g.completeExceptionally(new CFException());
1647        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1648        checkCompletedWithWrappedCFException(h);
1649
1650        f = new CompletableFuture<>();
1651        g = new CompletableFuture<>();
1652        f.completeExceptionally(new CFException());
1653        g.complete(3);
1654        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1655        checkCompletedWithWrappedCFException(h);
1656    }
1657
1658    /**
1659     * thenAcceptBothAsync result completes exceptionally if action does
1660     */
1661    public void testThenAcceptBothAsync3() {
1662        CompletableFuture<Integer> f, g;
1663        CompletableFuture<Void> h;
1664        FailingBiConsumer r;
1665
1666        f = new CompletableFuture<>();
1667        g = new CompletableFuture<>();
1668        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer());
1669        f.complete(3);
1670        checkIncomplete(h);
1671        g.complete(1);
1672        checkCompletedWithWrappedCFException(h);
1673
1674        f = new CompletableFuture<>();
1675        g = new CompletableFuture<>();
1676        f.complete(3);
1677        g.complete(1);
1678        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer());
1679        checkCompletedWithWrappedCFException(h);
1680    }
1681
1682    /**
1683     * thenAcceptBothAsync result completes exceptionally if either source cancelled
1684     */
1685    public void testThenAcceptBothAsync4() {
1686        CompletableFuture<Integer> f, g;
1687        CompletableFuture<Void> h;
1688        SubtractAction r;
1689
1690        f = new CompletableFuture<>();
1691        g = new CompletableFuture<>();
1692        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1693        assertTrue(f.cancel(true));
1694        checkIncomplete(h);
1695        g.complete(1);
1696        checkCompletedWithWrappedCancellationException(h);
1697
1698        f = new CompletableFuture<>();
1699        g = new CompletableFuture<>();
1700        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1701        assertTrue(g.cancel(true));
1702        checkIncomplete(h);
1703        f.complete(3);
1704        checkCompletedWithWrappedCancellationException(h);
1705
1706        f = new CompletableFuture<>();
1707        g = new CompletableFuture<>();
1708        f.complete(3);
1709        assertTrue(g.cancel(true));
1710        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1711        checkCompletedWithWrappedCancellationException(h);
1712
1713        f = new CompletableFuture<>();
1714        g = new CompletableFuture<>();
1715        assertTrue(f.cancel(true));
1716        g.complete(3);
1717        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1718        checkCompletedWithWrappedCancellationException(h);
1719    }
1720
1721    /**
1722     * runAfterBothAsync result completes normally after normal
1723     * completion of sources
1724     */
1725    public void testRunAfterBothAsync() {
1726        CompletableFuture<Integer> f = new CompletableFuture<>();
1727        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1728        Noop r = new Noop();
1729        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1730        f.complete(one);
1731        checkIncomplete(g);
1732        f2.complete(two);
1733        checkCompletedNormally(g, null);
1734        assertTrue(r.ran);
1735    }
1736
1737    /**
1738     * runAfterBothAsync result completes exceptionally after exceptional
1739     * completion of source
1740     */
1741    public void testRunAfterBothAsync2() {
1742        CompletableFuture<Integer> f = new CompletableFuture<>();
1743        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1744        Noop r = new Noop();
1745        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1746        f.completeExceptionally(new CFException());
1747        f2.complete(two);
1748        checkCompletedWithWrappedCFException(g);
1749
1750        r = new Noop();
1751        f = new CompletableFuture<>();
1752        f2 = new CompletableFuture<>();
1753        g = f.runAfterBothAsync(f2, r);
1754        f.complete(one);
1755        f2.completeExceptionally(new CFException());
1756        checkCompletedWithWrappedCFException(g);
1757    }
1758
1759    /**
1760     * runAfterBothAsync result completes exceptionally if action does
1761     */
1762    public void testRunAfterBothAsync3() {
1763        CompletableFuture<Integer> f = new CompletableFuture<>();
1764        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1765        FailingNoop r = new FailingNoop();
1766        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1767        f.complete(one);
1768        checkIncomplete(g);
1769        f2.complete(two);
1770        checkCompletedWithWrappedCFException(g);
1771    }
1772
1773    /**
1774     * runAfterBothAsync result completes exceptionally if either source cancelled
1775     */
1776    public void testRunAfterBothAsync4() {
1777        CompletableFuture<Integer> f = new CompletableFuture<>();
1778        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1779        Noop r = new Noop();
1780        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1781        assertTrue(f.cancel(true));
1782        f2.complete(two);
1783        checkCompletedWithWrappedCancellationException(g);
1784
1785        r = new Noop();
1786        f = new CompletableFuture<>();
1787        f2 = new CompletableFuture<>();
1788        g = f.runAfterBothAsync(f2, r);
1789        f.complete(one);
1790        assertTrue(f2.cancel(true));
1791        checkCompletedWithWrappedCancellationException(g);
1792    }
1793
1794    /**
1795     * applyToEitherAsync result completes normally after normal
1796     * completion of sources
1797     */
1798    public void testApplyToEitherAsync() {
1799        CompletableFuture<Integer> f = new CompletableFuture<>();
1800        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1801        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1802        f.complete(one);
1803        checkCompletedNormally(g, two);
1804
1805        f = new CompletableFuture<>();
1806        f.complete(one);
1807        f2 = new CompletableFuture<>();
1808        g = f.applyToEitherAsync(f2, inc);
1809        checkCompletedNormally(g, two);
1810    }
1811
1812    /**
1813     * applyToEitherAsync result completes exceptionally after exceptional
1814     * completion of source
1815     */
1816    public void testApplyToEitherAsync2() {
1817        CompletableFuture<Integer> f = new CompletableFuture<>();
1818        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1819        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1820        f.completeExceptionally(new CFException());
1821        checkCompletedWithWrappedCFException(g);
1822
1823        f = new CompletableFuture<>();
1824        f2 = new CompletableFuture<>();
1825        f2.completeExceptionally(new CFException());
1826        g = f.applyToEitherAsync(f2, inc);
1827        f.complete(one);
1828        checkCompletedWithWrappedCFException(g);
1829    }
1830
1831    /**
1832     * applyToEitherAsync result completes exceptionally if action does
1833     */
1834    public void testApplyToEitherAsync3() {
1835        CompletableFuture<Integer> f = new CompletableFuture<>();
1836        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1837        FailingFunction r = new FailingFunction();
1838        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r);
1839        f.complete(one);
1840        checkCompletedWithWrappedCFException(g);
1841    }
1842
1843    /**
1844     * applyToEitherAsync result completes exceptionally if either source cancelled
1845     */
1846    public void testApplyToEitherAsync4() {
1847        CompletableFuture<Integer> f = new CompletableFuture<>();
1848        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1849        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1850        assertTrue(f.cancel(true));
1851        checkCompletedWithWrappedCancellationException(g);
1852
1853        f = new CompletableFuture<>();
1854        f2 = new CompletableFuture<>();
1855        assertTrue(f2.cancel(true));
1856        g = f.applyToEitherAsync(f2, inc);
1857        checkCompletedWithWrappedCancellationException(g);
1858    }
1859
1860    /**
1861     * acceptEitherAsync result completes normally after normal
1862     * completion of sources
1863     */
1864    public void testAcceptEitherAsync() {
1865        CompletableFuture<Integer> f = new CompletableFuture<>();
1866        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1867        IncAction r = new IncAction();
1868        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1869        f.complete(one);
1870        checkCompletedNormally(g, null);
1871        assertEquals(r.value, 2);
1872
1873        r = new IncAction();
1874        f = new CompletableFuture<>();
1875        f.complete(one);
1876        f2 = new CompletableFuture<>();
1877        g = f.acceptEitherAsync(f2, r);
1878        checkCompletedNormally(g, null);
1879        assertEquals(r.value, 2);
1880    }
1881
1882    /**
1883     * acceptEitherAsync result completes exceptionally after exceptional
1884     * completion of source
1885     */
1886    public void testAcceptEitherAsync2() {
1887        CompletableFuture<Integer> f = new CompletableFuture<>();
1888        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1889        IncAction r = new IncAction();
1890        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1891        f.completeExceptionally(new CFException());
1892        checkCompletedWithWrappedCFException(g);
1893
1894        r = new IncAction();
1895        f = new CompletableFuture<>();
1896        f2 = new CompletableFuture<>();
1897        f2.completeExceptionally(new CFException());
1898        g = f.acceptEitherAsync(f2, r);
1899        f.complete(one);
1900        checkCompletedWithWrappedCFException(g);
1901    }
1902
1903    /**
1904     * acceptEitherAsync result completes exceptionally if action does
1905     */
1906    public void testAcceptEitherAsync3() {
1907        CompletableFuture<Integer> f = new CompletableFuture<>();
1908        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1909        FailingConsumer r = new FailingConsumer();
1910        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1911        f.complete(one);
1912        checkCompletedWithWrappedCFException(g);
1913    }
1914
1915    /**
1916     * acceptEitherAsync result completes exceptionally if either
1917     * source cancelled
1918     */
1919    public void testAcceptEitherAsync4() {
1920        CompletableFuture<Integer> f = new CompletableFuture<>();
1921        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1922        IncAction r = new IncAction();
1923        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1924        assertTrue(f.cancel(true));
1925        checkCompletedWithWrappedCancellationException(g);
1926
1927        r = new IncAction();
1928        f = new CompletableFuture<>();
1929        f2 = new CompletableFuture<>();
1930        assertTrue(f2.cancel(true));
1931        g = f.acceptEitherAsync(f2, r);
1932        checkCompletedWithWrappedCancellationException(g);
1933    }
1934
1935    /**
1936     * runAfterEitherAsync result completes normally after normal
1937     * completion of sources
1938     */
1939    public void testRunAfterEitherAsync() {
1940        CompletableFuture<Integer> f = new CompletableFuture<>();
1941        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1942        Noop r = new Noop();
1943        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1944        f.complete(one);
1945        checkCompletedNormally(g, null);
1946        assertTrue(r.ran);
1947
1948        r = new Noop();
1949        f = new CompletableFuture<>();
1950        f.complete(one);
1951        f2 = new CompletableFuture<>();
1952        g = f.runAfterEitherAsync(f2, r);
1953        checkCompletedNormally(g, null);
1954        assertTrue(r.ran);
1955    }
1956
1957    /**
1958     * runAfterEitherAsync result completes exceptionally after exceptional
1959     * completion of source
1960     */
1961    public void testRunAfterEitherAsync2() {
1962        CompletableFuture<Integer> f = new CompletableFuture<>();
1963        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1964        Noop r = new Noop();
1965        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1966        f.completeExceptionally(new CFException());
1967        checkCompletedWithWrappedCFException(g);
1968
1969        r = new Noop();
1970        f = new CompletableFuture<>();
1971        f2 = new CompletableFuture<>();
1972        f2.completeExceptionally(new CFException());
1973        g = f.runAfterEitherAsync(f2, r);
1974        f.complete(one);
1975        checkCompletedWithWrappedCFException(g);
1976    }
1977
1978    /**
1979     * runAfterEitherAsync result completes exceptionally if action does
1980     */
1981    public void testRunAfterEitherAsync3() {
1982        CompletableFuture<Integer> f = new CompletableFuture<>();
1983        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1984        FailingNoop r = new FailingNoop();
1985        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1986        f.complete(one);
1987        checkCompletedWithWrappedCFException(g);
1988    }
1989
1990    /**
1991     * runAfterEitherAsync result completes exceptionally if either
1992     * source cancelled
1993     */
1994    public void testRunAfterEitherAsync4() {
1995        CompletableFuture<Integer> f = new CompletableFuture<>();
1996        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1997        Noop r = new Noop();
1998        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1999        assertTrue(f.cancel(true));
2000        checkCompletedWithWrappedCancellationException(g);
2001
2002        r = new Noop();
2003        f = new CompletableFuture<>();
2004        f2 = new CompletableFuture<>();
2005        assertTrue(f2.cancel(true));
2006        g = f.runAfterEitherAsync(f2, r);
2007        checkCompletedWithWrappedCancellationException(g);
2008    }
2009
2010    /**
2011     * thenComposeAsync result completes normally after normal
2012     * completion of source
2013     */
2014    public void testThenComposeAsync() {
2015        CompletableFuture<Integer> f, g;
2016        CompletableFutureInc r;
2017
2018        f = new CompletableFuture<>();
2019        g = f.thenComposeAsync(r = new CompletableFutureInc());
2020        f.complete(one);
2021        checkCompletedNormally(g, two);
2022
2023        f = new CompletableFuture<>();
2024        f.complete(one);
2025        g = f.thenComposeAsync(r = new CompletableFutureInc());
2026        checkCompletedNormally(g, two);
2027    }
2028
2029    /**
2030     * thenComposeAsync result completes exceptionally after
2031     * exceptional completion of source
2032     */
2033    public void testThenComposeAsync2() {
2034        CompletableFuture<Integer> f, g;
2035        CompletableFutureInc r;
2036
2037        f = new CompletableFuture<>();
2038        g = f.thenComposeAsync(r = new CompletableFutureInc());
2039        f.completeExceptionally(new CFException());
2040        checkCompletedWithWrappedCFException(g);
2041        assertFalse(r.ran);
2042
2043        f = new CompletableFuture<>();
2044        f.completeExceptionally(new CFException());
2045        g = f.thenComposeAsync(r = new CompletableFutureInc());
2046        checkCompletedWithWrappedCFException(g);
2047        assertFalse(r.ran);
2048    }
2049
2050    /**
2051     * thenComposeAsync result completes exceptionally if action does
2052     */
2053    public void testThenComposeAsync3() {
2054        CompletableFuture<Integer> f, g;
2055        FailingCompletableFutureFunction r;
2056
2057        f = new CompletableFuture<>();
2058        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
2059        f.complete(one);
2060        checkCompletedWithWrappedCFException(g);
2061
2062        f = new CompletableFuture<>();
2063        f.complete(one);
2064        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
2065        checkCompletedWithWrappedCFException(g);
2066    }
2067
2068    /**
2069     * thenComposeAsync result completes exceptionally if source cancelled
2070     */
2071    public void testThenComposeAsync4() {
2072        CompletableFuture<Integer> f, g;
2073        CompletableFutureInc r;
2074
2075        f = new CompletableFuture<>();
2076        g = f.thenComposeAsync(r = new CompletableFutureInc());
2077        assertTrue(f.cancel(true));
2078        checkCompletedWithWrappedCancellationException(g);
2079
2080        f = new CompletableFuture<>();
2081        assertTrue(f.cancel(true));
2082        g = f.thenComposeAsync(r = new CompletableFutureInc());
2083        checkCompletedWithWrappedCancellationException(g);
2084    }
2085
2086    // async with explicit executors
2087
2088    /**
2089     * thenRunAsync result completes normally after normal completion of source
2090     */
2091    public void testThenRunAsyncE() {
2092        CompletableFuture<Integer> f = new CompletableFuture<>();
2093        Noop r = new Noop();
2094        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2095        f.complete(null);
2096        checkCompletedNormally(g, null);
2097
2098        // reordered version
2099        f = new CompletableFuture<>();
2100        f.complete(null);
2101        r = new Noop();
2102        g = f.thenRunAsync(r, new ThreadExecutor());
2103        checkCompletedNormally(g, null);
2104    }
2105
2106    /**
2107     * thenRunAsync result completes exceptionally after exceptional
2108     * completion of source
2109     */
2110    public void testThenRunAsync2E() {
2111        CompletableFuture<Integer> f = new CompletableFuture<>();
2112        Noop r = new Noop();
2113        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2114        f.completeExceptionally(new CFException());
2500          try {
2501 <            g.join();
2502 <            shouldThrow();
2503 <        } catch (Exception ok) {
2501 >            assertNull(h5.join());
2502 >            rs[5].assertValue(v1);
2503 >        } catch (CompletionException ok) {
2504 >            checkCompletedWithWrappedException(h5, ex);
2505 >            rs[5].assertNotInvoked();
2506          }
2120        checkCompletedWithWrappedCFException(g);
2121    }
2122
2123    /**
2124     * thenRunAsync result completes exceptionally if action does
2125     */
2126    public void testThenRunAsync3E() {
2127        CompletableFuture<Integer> f = new CompletableFuture<>();
2128        FailingNoop r = new FailingNoop();
2129        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2130        f.complete(null);
2131        checkCompletedWithWrappedCFException(g);
2132    }
2507  
2508 <    /**
2509 <     * thenRunAsync result completes exceptionally if source cancelled
2510 <     */
2511 <    public void testThenRunAsync4E() {
2512 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2513 <        Noop r = new Noop();
2514 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2515 <        assertTrue(f.cancel(true));
2516 <        checkCompletedWithWrappedCancellationException(g);
2517 <    }
2518 <
2519 <    /**
2520 <     * thenApplyAsync result completes normally after normal completion of source
2521 <     */
2522 <    public void testThenApplyAsyncE() {
2523 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2524 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
2525 <        f.complete(one);
2526 <        checkCompletedNormally(g, two);
2527 <    }
2528 <
2529 <    /**
2530 <     * thenApplyAsync result completes exceptionally after exceptional
2531 <     * completion of source
2532 <     */
2533 <    public void testThenApplyAsync2E() {
2534 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2161 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
2162 <        f.completeExceptionally(new CFException());
2163 <        checkCompletedWithWrappedCFException(g);
2164 <    }
2165 <
2166 <    /**
2167 <     * thenApplyAsync result completes exceptionally if action does
2168 <     */
2169 <    public void testThenApplyAsync3E() {
2170 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2171 <        FailingFunction r = new FailingFunction();
2172 <        CompletableFuture<Integer> g = f.thenApplyAsync(r, new ThreadExecutor());
2173 <        f.complete(null);
2174 <        checkCompletedWithWrappedCFException(g);
2175 <    }
2508 >        checkCompletedExceptionally(f, ex);
2509 >        checkCompletedNormally(g, v1);
2510 >        checkCompletedWithWrappedException(h0, ex);
2511 >        checkCompletedWithWrappedException(h1, ex);
2512 >        checkCompletedWithWrappedException(h2, ex);
2513 >        checkCompletedWithWrappedException(h3, ex);
2514 >        checkCompletedWithWrappedException(h4, ex);
2515 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2516 >    }}
2517 >
2518 >    public void testAcceptEither_exceptionalCompletion2() {
2519 >        for (ExecutionMode m : ExecutionMode.values())
2520 >        for (boolean fFirst : new boolean[] { true, false })
2521 >        for (Integer v1 : new Integer[] { 1, null })
2522 >    {
2523 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2524 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2525 >        final CFException ex = new CFException();
2526 >        final NoopConsumer[] rs = new NoopConsumer[6];
2527 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2528 >
2529 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2530 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2531 >        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2532 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2533 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2534 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2535  
2536 <    /**
2537 <     * thenApplyAsync result completes exceptionally if source cancelled
2538 <     */
2539 <    public void testThenApplyAsync4E() {
2540 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2541 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
2542 <        assertTrue(f.cancel(true));
2543 <        checkCompletedWithWrappedCancellationException(g);
2544 <    }
2536 >        // unspecified behavior - both source completions available
2537 >        try {
2538 >            assertEquals(null, h0.join());
2539 >            rs[0].assertValue(v1);
2540 >        } catch (CompletionException ok) {
2541 >            checkCompletedWithWrappedException(h0, ex);
2542 >            rs[0].assertNotInvoked();
2543 >        }
2544 >        try {
2545 >            assertEquals(null, h1.join());
2546 >            rs[1].assertValue(v1);
2547 >        } catch (CompletionException ok) {
2548 >            checkCompletedWithWrappedException(h1, ex);
2549 >            rs[1].assertNotInvoked();
2550 >        }
2551 >        try {
2552 >            assertEquals(null, h2.join());
2553 >            rs[2].assertValue(v1);
2554 >        } catch (CompletionException ok) {
2555 >            checkCompletedWithWrappedException(h2, ex);
2556 >            rs[2].assertNotInvoked();
2557 >        }
2558 >        try {
2559 >            assertEquals(null, h3.join());
2560 >            rs[3].assertValue(v1);
2561 >        } catch (CompletionException ok) {
2562 >            checkCompletedWithWrappedException(h3, ex);
2563 >            rs[3].assertNotInvoked();
2564 >        }
2565  
2566 <    /**
2567 <     * thenAcceptAsync result completes normally after normal
2568 <     * completion of source
2190 <     */
2191 <    public void testThenAcceptAsyncE() {
2192 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2193 <        IncAction r = new IncAction();
2194 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2195 <        f.complete(one);
2196 <        checkCompletedNormally(g, null);
2197 <        assertEquals(r.value, 2);
2198 <    }
2566 >        checkCompletedNormally(f, v1);
2567 >        checkCompletedExceptionally(g, ex);
2568 >    }}
2569  
2570      /**
2571 <     * thenAcceptAsync result completes exceptionally after exceptional
2202 <     * completion of source
2571 >     * acceptEither result completes exceptionally if either source cancelled
2572       */
2573 <    public void testThenAcceptAsync2E() {
2574 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2575 <        IncAction r = new IncAction();
2576 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2577 <        f.completeExceptionally(new CFException());
2578 <        checkCompletedWithWrappedCFException(g);
2579 <    }
2573 >    public void testAcceptEither_sourceCancelled() {
2574 >        for (ExecutionMode m : ExecutionMode.values())
2575 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2576 >        for (Integer v1 : new Integer[] { 1, null })
2577 >    {
2578 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2579 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2580 >        final NoopConsumer[] rs = new NoopConsumer[6];
2581 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2582 >
2583 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2584 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2585 >        checkIncomplete(h0);
2586 >        checkIncomplete(h1);
2587 >        rs[0].assertNotInvoked();
2588 >        rs[1].assertNotInvoked();
2589 >        f.cancel(mayInterruptIfRunning);
2590 >        checkCompletedWithWrappedCancellationException(h0);
2591 >        checkCompletedWithWrappedCancellationException(h1);
2592 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2593 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2594 >        checkCompletedWithWrappedCancellationException(h2);
2595 >        checkCompletedWithWrappedCancellationException(h3);
2596 >
2597 >        g.complete(v1);
2598 >
2599 >        // unspecified behavior - both source completions available
2600 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2601 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2602 >        try {
2603 >            assertNull(h4.join());
2604 >            rs[4].assertValue(v1);
2605 >        } catch (CompletionException ok) {
2606 >            checkCompletedWithWrappedCancellationException(h4);
2607 >            rs[4].assertNotInvoked();
2608 >        }
2609 >        try {
2610 >            assertNull(h5.join());
2611 >            rs[5].assertValue(v1);
2612 >        } catch (CompletionException ok) {
2613 >            checkCompletedWithWrappedCancellationException(h5);
2614 >            rs[5].assertNotInvoked();
2615 >        }
2616  
2617 <    /**
2618 <     * thenAcceptAsync result completes exceptionally if action does
2619 <     */
2620 <    public void testThenAcceptAsync3E() {
2621 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2622 <        FailingConsumer r = new FailingConsumer();
2623 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2624 <        f.complete(null);
2220 <        checkCompletedWithWrappedCFException(g);
2221 <    }
2617 >        checkCancelled(f);
2618 >        checkCompletedNormally(g, v1);
2619 >        checkCompletedWithWrappedCancellationException(h0);
2620 >        checkCompletedWithWrappedCancellationException(h1);
2621 >        checkCompletedWithWrappedCancellationException(h2);
2622 >        checkCompletedWithWrappedCancellationException(h3);
2623 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2624 >    }}
2625  
2626      /**
2627 <     * thenAcceptAsync result completes exceptionally if source cancelled
2627 >     * acceptEither result completes exceptionally if action does
2628       */
2629 <    public void testThenAcceptAsync4E() {
2630 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2631 <        IncAction r = new IncAction();
2632 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2633 <        assertTrue(f.cancel(true));
2634 <        checkCompletedWithWrappedCancellationException(g);
2635 <    }
2629 >    public void testAcceptEither_actionFailed() {
2630 >        for (ExecutionMode m : ExecutionMode.values())
2631 >        for (Integer v1 : new Integer[] { 1, null })
2632 >        for (Integer v2 : new Integer[] { 2, null })
2633 >    {
2634 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2635 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2636 >        final FailingConsumer[] rs = new FailingConsumer[6];
2637 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
2638 >
2639 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2640 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2641 >        f.complete(v1);
2642 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2643 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2644 >        checkCompletedWithWrappedCFException(h0);
2645 >        checkCompletedWithWrappedCFException(h1);
2646 >        checkCompletedWithWrappedCFException(h2);
2647 >        checkCompletedWithWrappedCFException(h3);
2648 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2649 >
2650 >        g.complete(v2);
2651 >
2652 >        // unspecified behavior - both source completions available
2653 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2654 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2655 >
2656 >        checkCompletedWithWrappedCFException(h4);
2657 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2658 >                   Objects.equals(v2, rs[4].value));
2659 >        checkCompletedWithWrappedCFException(h5);
2660 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2661 >                   Objects.equals(v2, rs[5].value));
2662 >
2663 >        checkCompletedNormally(f, v1);
2664 >        checkCompletedNormally(g, v2);
2665 >    }}
2666  
2667      /**
2668 <     * thenCombineAsync result completes normally after normal
2669 <     * completion of sources
2668 >     * runAfterEither result completes normally after normal completion
2669 >     * of either source
2670       */
2671 <    public void testThenCombineAsyncE() {
2672 <        CompletableFuture<Integer> f, g, h;
2673 <        ThreadExecutor e = new ThreadExecutor();
2674 <        int count = 0;
2675 <
2676 <        f = new CompletableFuture<>();
2677 <        g = new CompletableFuture<>();
2678 <        h = f.thenCombineAsync(g, subtract, e);
2679 <        f.complete(3);
2680 <        checkIncomplete(h);
2681 <        g.complete(1);
2682 <        checkCompletedNormally(h, 2);
2683 <        assertEquals(++count, e.count.get());
2684 <
2685 <        f = new CompletableFuture<>();
2686 <        g = new CompletableFuture<>();
2687 <        h = f.thenCombineAsync(g, subtract, e);
2688 <        g.complete(1);
2689 <        checkIncomplete(h);
2690 <        f.complete(3);
2691 <        checkCompletedNormally(h, 2);
2692 <        assertEquals(++count, e.count.get());
2693 <
2694 <        f = new CompletableFuture<>();
2695 <        g = new CompletableFuture<>();
2696 <        g.complete(1);
2697 <        f.complete(3);
2698 <        h = f.thenCombineAsync(g, subtract, e);
2699 <        checkCompletedNormally(h, 2);
2700 <        assertEquals(++count, e.count.get());
2701 <    }
2671 >    public void testRunAfterEither_normalCompletion() {
2672 >        for (ExecutionMode m : ExecutionMode.values())
2673 >        for (Integer v1 : new Integer[] { 1, null })
2674 >        for (Integer v2 : new Integer[] { 2, null })
2675 >    {
2676 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2677 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2678 >        final Noop[] rs = new Noop[6];
2679 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2680 >
2681 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2682 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2683 >        checkIncomplete(h0);
2684 >        checkIncomplete(h1);
2685 >        rs[0].assertNotInvoked();
2686 >        rs[1].assertNotInvoked();
2687 >        f.complete(v1);
2688 >        checkCompletedNormally(h0, null);
2689 >        checkCompletedNormally(h1, null);
2690 >        rs[0].assertInvoked();
2691 >        rs[1].assertInvoked();
2692 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2693 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2694 >        checkCompletedNormally(h2, null);
2695 >        checkCompletedNormally(h3, null);
2696 >        rs[2].assertInvoked();
2697 >        rs[3].assertInvoked();
2698 >
2699 >        g.complete(v2);
2700 >
2701 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2702 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2703 >
2704 >        checkCompletedNormally(f, v1);
2705 >        checkCompletedNormally(g, v2);
2706 >        checkCompletedNormally(h0, null);
2707 >        checkCompletedNormally(h1, null);
2708 >        checkCompletedNormally(h2, null);
2709 >        checkCompletedNormally(h3, null);
2710 >        checkCompletedNormally(h4, null);
2711 >        checkCompletedNormally(h5, null);
2712 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2713 >    }}
2714  
2715      /**
2716 <     * thenCombineAsync result completes exceptionally after exceptional
2716 >     * runAfterEither result completes exceptionally after exceptional
2717       * completion of either source
2718       */
2719 <    public void testThenCombineAsync2E() {
2720 <        CompletableFuture<Integer> f, g, h;
2721 <        ThreadExecutor e = new ThreadExecutor();
2722 <        int count = 0;
2723 <
2724 <        f = new CompletableFuture<>();
2725 <        g = new CompletableFuture<>();
2726 <        h = f.thenCombineAsync(g, subtract, e);
2727 <        f.completeExceptionally(new CFException());
2728 <        checkIncomplete(h);
2729 <        g.complete(1);
2730 <        checkCompletedWithWrappedCFException(h);
2731 <
2732 <        f = new CompletableFuture<>();
2733 <        g = new CompletableFuture<>();
2734 <        h = f.thenCombineAsync(g, subtract, e);
2735 <        g.completeExceptionally(new CFException());
2736 <        checkIncomplete(h);
2737 <        f.complete(3);
2738 <        checkCompletedWithWrappedCFException(h);
2739 <
2740 <        f = new CompletableFuture<>();
2741 <        g = new CompletableFuture<>();
2742 <        g.completeExceptionally(new CFException());
2743 <        h = f.thenCombineAsync(g, subtract, e);
2744 <        checkIncomplete(h);
2745 <        f.complete(3);
2746 <        checkCompletedWithWrappedCFException(h);
2747 <
2748 <        assertEquals(0, e.count.get());
2749 <    }
2750 <
2751 <    /**
2752 <     * thenCombineAsync result completes exceptionally if action does
2753 <     */
2754 <    public void testThenCombineAsync3E() {
2755 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2756 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2757 <        FailingBiFunction r = new FailingBiFunction();
2758 <        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r, new ThreadExecutor());
2759 <        f.complete(one);
2760 <        checkIncomplete(g);
2761 <        assertFalse(r.ran);
2317 <        f2.complete(two);
2318 <        checkCompletedWithWrappedCFException(g);
2319 <        assertTrue(r.ran);
2320 <    }
2321 <
2322 <    /**
2323 <     * thenCombineAsync result completes exceptionally if either source cancelled
2324 <     */
2325 <    public void testThenCombineAsync4E() {
2326 <        CompletableFuture<Integer> f, g, h;
2327 <        ThreadExecutor e = new ThreadExecutor();
2328 <
2329 <        f = new CompletableFuture<>();
2330 <        g = new CompletableFuture<>();
2331 <        h = f.thenCombineAsync(g, subtract, e);
2332 <        assertTrue(f.cancel(true));
2333 <        checkIncomplete(h);
2334 <        g.complete(1);
2335 <        checkCompletedWithWrappedCancellationException(h);
2336 <
2337 <        f = new CompletableFuture<>();
2338 <        g = new CompletableFuture<>();
2339 <        h = f.thenCombineAsync(g, subtract, e);
2340 <        assertTrue(g.cancel(true));
2341 <        checkIncomplete(h);
2342 <        f.complete(3);
2343 <        checkCompletedWithWrappedCancellationException(h);
2344 <
2345 <        f = new CompletableFuture<>();
2346 <        g = new CompletableFuture<>();
2347 <        assertTrue(g.cancel(true));
2348 <        h = f.thenCombineAsync(g, subtract, e);
2349 <        checkIncomplete(h);
2350 <        f.complete(3);
2351 <        checkCompletedWithWrappedCancellationException(h);
2352 <
2353 <        f = new CompletableFuture<>();
2354 <        g = new CompletableFuture<>();
2355 <        assertTrue(f.cancel(true));
2356 <        assertTrue(g.cancel(true));
2357 <        h = f.thenCombineAsync(g, subtract, e);
2358 <        checkCompletedWithWrappedCancellationException(h);
2359 <
2360 <        assertEquals(0, e.count.get());
2361 <    }
2362 <
2363 <    /**
2364 <     * thenAcceptBothAsync result completes normally after normal
2365 <     * completion of sources
2366 <     */
2367 <    public void testThenAcceptBothAsyncE() {
2368 <        CompletableFuture<Integer> f, g;
2369 <        CompletableFuture<Void> h;
2370 <        SubtractAction r;
2371 <        ThreadExecutor e = new ThreadExecutor();
2372 <
2373 <        f = new CompletableFuture<>();
2374 <        g = new CompletableFuture<>();
2375 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2376 <        f.complete(3);
2377 <        checkIncomplete(h);
2378 <        g.complete(1);
2379 <        checkCompletedNormally(h, null);
2380 <        assertEquals(r.value, 2);
2381 <
2382 <        f = new CompletableFuture<>();
2383 <        g = new CompletableFuture<>();
2384 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2385 <        g.complete(1);
2386 <        checkIncomplete(h);
2387 <        f.complete(3);
2388 <        checkCompletedNormally(h, null);
2389 <        assertEquals(r.value, 2);
2390 <
2391 <        f = new CompletableFuture<>();
2392 <        g = new CompletableFuture<>();
2393 <        g.complete(1);
2394 <        f.complete(3);
2395 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2396 <        checkCompletedNormally(h, null);
2397 <        assertEquals(r.value, 2);
2398 <
2399 <        assertEquals(3, e.count.get());
2400 <    }
2401 <
2402 <    /**
2403 <     * thenAcceptBothAsync result completes exceptionally after exceptional
2404 <     * completion of source
2405 <     */
2406 <    public void testThenAcceptBothAsync2E() {
2407 <        CompletableFuture<Integer> f, g;
2408 <        CompletableFuture<Void> h;
2409 <        SubtractAction r;
2410 <        ThreadExecutor e = new ThreadExecutor();
2411 <
2412 <        f = new CompletableFuture<>();
2413 <        g = new CompletableFuture<>();
2414 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2415 <        f.completeExceptionally(new CFException());
2416 <        checkIncomplete(h);
2417 <        g.complete(1);
2418 <        checkCompletedWithWrappedCFException(h);
2419 <
2420 <        f = new CompletableFuture<>();
2421 <        g = new CompletableFuture<>();
2422 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2423 <        g.completeExceptionally(new CFException());
2424 <        checkIncomplete(h);
2425 <        f.complete(3);
2426 <        checkCompletedWithWrappedCFException(h);
2427 <
2428 <        f = new CompletableFuture<>();
2429 <        g = new CompletableFuture<>();
2430 <        f.complete(3);
2431 <        g.completeExceptionally(new CFException());
2432 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2433 <        checkCompletedWithWrappedCFException(h);
2434 <
2435 <        f = new CompletableFuture<>();
2436 <        g = new CompletableFuture<>();
2437 <        f.completeExceptionally(new CFException());
2438 <        g.complete(3);
2439 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2440 <        checkCompletedWithWrappedCFException(h);
2441 <
2442 <        assertEquals(0, e.count.get());
2443 <    }
2444 <
2445 <    /**
2446 <     * thenAcceptBothAsync result completes exceptionally if action does
2447 <     */
2448 <    public void testThenAcceptBothAsync3E() {
2449 <        CompletableFuture<Integer> f, g;
2450 <        CompletableFuture<Void> h;
2451 <        FailingBiConsumer r;
2452 <        ThreadExecutor e = new ThreadExecutor();
2453 <
2454 <        f = new CompletableFuture<>();
2455 <        g = new CompletableFuture<>();
2456 <        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer(), e);
2457 <        f.complete(3);
2458 <        checkIncomplete(h);
2459 <        g.complete(1);
2460 <        checkCompletedWithWrappedCFException(h);
2461 <
2462 <        f = new CompletableFuture<>();
2463 <        g = new CompletableFuture<>();
2464 <        f.complete(3);
2465 <        g.complete(1);
2466 <        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer(), e);
2467 <        checkCompletedWithWrappedCFException(h);
2468 <
2469 <        assertEquals(2, e.count.get());
2470 <    }
2471 <
2472 <    /**
2473 <     * thenAcceptBothAsync result completes exceptionally if either source cancelled
2474 <     */
2475 <    public void testThenAcceptBothAsync4E() {
2476 <        CompletableFuture<Integer> f, g;
2477 <        CompletableFuture<Void> h;
2478 <        SubtractAction r;
2479 <        ThreadExecutor e = new ThreadExecutor();
2480 <
2481 <        f = new CompletableFuture<>();
2482 <        g = new CompletableFuture<>();
2483 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2484 <        assertTrue(f.cancel(true));
2485 <        checkIncomplete(h);
2486 <        g.complete(1);
2487 <        checkCompletedWithWrappedCancellationException(h);
2488 <
2489 <        f = new CompletableFuture<>();
2490 <        g = new CompletableFuture<>();
2491 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2492 <        assertTrue(g.cancel(true));
2493 <        checkIncomplete(h);
2494 <        f.complete(3);
2495 <        checkCompletedWithWrappedCancellationException(h);
2496 <
2497 <        f = new CompletableFuture<>();
2498 <        g = new CompletableFuture<>();
2499 <        f.complete(3);
2500 <        assertTrue(g.cancel(true));
2501 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2502 <        checkCompletedWithWrappedCancellationException(h);
2503 <
2504 <        f = new CompletableFuture<>();
2505 <        g = new CompletableFuture<>();
2506 <        assertTrue(f.cancel(true));
2507 <        g.complete(3);
2508 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2509 <        checkCompletedWithWrappedCancellationException(h);
2510 <
2511 <        assertEquals(0, e.count.get());
2512 <    }
2513 <
2514 <    /**
2515 <     * runAfterBothAsync result completes normally after normal
2516 <     * completion of sources
2517 <     */
2518 <    public void testRunAfterBothAsyncE() {
2519 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2520 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2521 <        Noop r = new Noop();
2522 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2523 <        f.complete(one);
2524 <        checkIncomplete(g);
2525 <        f2.complete(two);
2526 <        checkCompletedNormally(g, null);
2527 <        assertTrue(r.ran);
2528 <    }
2529 <
2530 <    /**
2531 <     * runAfterBothAsync result completes exceptionally after exceptional
2532 <     * completion of source
2533 <     */
2534 <    public void testRunAfterBothAsync2E() {
2535 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2536 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2537 <        Noop r = new Noop();
2538 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2539 <        f.completeExceptionally(new CFException());
2540 <        f2.complete(two);
2541 <        checkCompletedWithWrappedCFException(g);
2542 <
2543 <        r = new Noop();
2544 <        f = new CompletableFuture<>();
2545 <        f2 = new CompletableFuture<>();
2546 <        g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2547 <        f.complete(one);
2548 <        f2.completeExceptionally(new CFException());
2549 <        checkCompletedWithWrappedCFException(g);
2550 <    }
2551 <
2552 <    /**
2553 <     * runAfterBothAsync result completes exceptionally if action does
2554 <     */
2555 <    public void testRunAfterBothAsync3E() {
2556 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2557 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2558 <        FailingNoop r = new FailingNoop();
2559 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2560 <        f.complete(one);
2561 <        checkIncomplete(g);
2562 <        f2.complete(two);
2563 <        checkCompletedWithWrappedCFException(g);
2564 <    }
2565 <
2566 <    /**
2567 <     * runAfterBothAsync result completes exceptionally if either source cancelled
2568 <     */
2569 <    public void testRunAfterBothAsync4E() {
2570 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2571 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2572 <        Noop r = new Noop();
2573 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2574 <        assertTrue(f.cancel(true));
2575 <        f2.complete(two);
2576 <        checkCompletedWithWrappedCancellationException(g);
2719 >    public void testRunAfterEither_exceptionalCompletion() {
2720 >        for (ExecutionMode m : ExecutionMode.values())
2721 >        for (Integer v1 : new Integer[] { 1, null })
2722 >    {
2723 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2724 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2725 >        final CFException ex = new CFException();
2726 >        final Noop[] rs = new Noop[6];
2727 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2728 >
2729 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2730 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2731 >        checkIncomplete(h0);
2732 >        checkIncomplete(h1);
2733 >        rs[0].assertNotInvoked();
2734 >        rs[1].assertNotInvoked();
2735 >        assertTrue(f.completeExceptionally(ex));
2736 >        checkCompletedWithWrappedException(h0, ex);
2737 >        checkCompletedWithWrappedException(h1, ex);
2738 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2739 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2740 >        checkCompletedWithWrappedException(h2, ex);
2741 >        checkCompletedWithWrappedException(h3, ex);
2742 >
2743 >        assertTrue(g.complete(v1));
2744 >
2745 >        // unspecified behavior - both source completions available
2746 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2747 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2748 >        try {
2749 >            assertNull(h4.join());
2750 >            rs[4].assertInvoked();
2751 >        } catch (CompletionException ok) {
2752 >            checkCompletedWithWrappedException(h4, ex);
2753 >            rs[4].assertNotInvoked();
2754 >        }
2755 >        try {
2756 >            assertNull(h5.join());
2757 >            rs[5].assertInvoked();
2758 >        } catch (CompletionException ok) {
2759 >            checkCompletedWithWrappedException(h5, ex);
2760 >            rs[5].assertNotInvoked();
2761 >        }
2762  
2763 <        r = new Noop();
2764 <        f = new CompletableFuture<>();
2765 <        f2 = new CompletableFuture<>();
2766 <        g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2767 <        f.complete(one);
2768 <        assertTrue(f2.cancel(true));
2769 <        checkCompletedWithWrappedCancellationException(g);
2770 <    }
2763 >        checkCompletedExceptionally(f, ex);
2764 >        checkCompletedNormally(g, v1);
2765 >        checkCompletedWithWrappedException(h0, ex);
2766 >        checkCompletedWithWrappedException(h1, ex);
2767 >        checkCompletedWithWrappedException(h2, ex);
2768 >        checkCompletedWithWrappedException(h3, ex);
2769 >        checkCompletedWithWrappedException(h4, ex);
2770 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2771 >    }}
2772 >
2773 >    public void testRunAfterEither_exceptionalCompletion2() {
2774 >        for (ExecutionMode m : ExecutionMode.values())
2775 >        for (boolean fFirst : new boolean[] { true, false })
2776 >        for (Integer v1 : new Integer[] { 1, null })
2777 >    {
2778 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2779 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2780 >        final CFException ex = new CFException();
2781 >        final Noop[] rs = new Noop[6];
2782 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2783 >
2784 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2785 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2786 >        assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2787 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2788 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2789 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2790  
2791 <    /**
2792 <     * applyToEitherAsync result completes normally after normal
2793 <     * completion of sources
2794 <     */
2795 <    public void testApplyToEitherAsyncE() {
2796 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2797 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2798 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2799 <        f.complete(one);
2800 <        checkCompletedNormally(g, two);
2791 >        // unspecified behavior - both source completions available
2792 >        try {
2793 >            assertEquals(null, h0.join());
2794 >            rs[0].assertInvoked();
2795 >        } catch (CompletionException ok) {
2796 >            checkCompletedWithWrappedException(h0, ex);
2797 >            rs[0].assertNotInvoked();
2798 >        }
2799 >        try {
2800 >            assertEquals(null, h1.join());
2801 >            rs[1].assertInvoked();
2802 >        } catch (CompletionException ok) {
2803 >            checkCompletedWithWrappedException(h1, ex);
2804 >            rs[1].assertNotInvoked();
2805 >        }
2806 >        try {
2807 >            assertEquals(null, h2.join());
2808 >            rs[2].assertInvoked();
2809 >        } catch (CompletionException ok) {
2810 >            checkCompletedWithWrappedException(h2, ex);
2811 >            rs[2].assertNotInvoked();
2812 >        }
2813 >        try {
2814 >            assertEquals(null, h3.join());
2815 >            rs[3].assertInvoked();
2816 >        } catch (CompletionException ok) {
2817 >            checkCompletedWithWrappedException(h3, ex);
2818 >            rs[3].assertNotInvoked();
2819 >        }
2820  
2821 <        f = new CompletableFuture<>();
2822 <        f.complete(one);
2823 <        f2 = new CompletableFuture<>();
2601 <        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2602 <        checkCompletedNormally(g, two);
2603 <    }
2821 >        checkCompletedNormally(f, v1);
2822 >        checkCompletedExceptionally(g, ex);
2823 >    }}
2824  
2825      /**
2826 <     * applyToEitherAsync result completes exceptionally after exceptional
2607 <     * completion of source
2826 >     * runAfterEither result completes exceptionally if either source cancelled
2827       */
2828 <    public void testApplyToEitherAsync2E() {
2829 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2830 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2831 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2832 <        f.completeExceptionally(new CFException());
2833 <        checkCompletedWithWrappedCFException(g);
2834 <
2835 <        f = new CompletableFuture<>();
2836 <        f2 = new CompletableFuture<>();
2837 <        f2.completeExceptionally(new CFException());
2838 <        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2839 <        f.complete(one);
2840 <        checkCompletedWithWrappedCFException(g);
2841 <    }
2828 >    public void testRunAfterEither_sourceCancelled() {
2829 >        for (ExecutionMode m : ExecutionMode.values())
2830 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2831 >        for (Integer v1 : new Integer[] { 1, null })
2832 >    {
2833 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2834 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2835 >        final Noop[] rs = new Noop[6];
2836 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2837 >
2838 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2839 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2840 >        checkIncomplete(h0);
2841 >        checkIncomplete(h1);
2842 >        rs[0].assertNotInvoked();
2843 >        rs[1].assertNotInvoked();
2844 >        f.cancel(mayInterruptIfRunning);
2845 >        checkCompletedWithWrappedCancellationException(h0);
2846 >        checkCompletedWithWrappedCancellationException(h1);
2847 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2848 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2849 >        checkCompletedWithWrappedCancellationException(h2);
2850 >        checkCompletedWithWrappedCancellationException(h3);
2851 >
2852 >        assertTrue(g.complete(v1));
2853 >
2854 >        // unspecified behavior - both source completions available
2855 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2856 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2857 >        try {
2858 >            assertNull(h4.join());
2859 >            rs[4].assertInvoked();
2860 >        } catch (CompletionException ok) {
2861 >            checkCompletedWithWrappedCancellationException(h4);
2862 >            rs[4].assertNotInvoked();
2863 >        }
2864 >        try {
2865 >            assertNull(h5.join());
2866 >            rs[5].assertInvoked();
2867 >        } catch (CompletionException ok) {
2868 >            checkCompletedWithWrappedCancellationException(h5);
2869 >            rs[5].assertNotInvoked();
2870 >        }
2871  
2872 <    /**
2873 <     * applyToEitherAsync result completes exceptionally if action does
2874 <     */
2875 <    public void testApplyToEitherAsync3E() {
2876 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2877 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2878 <        FailingFunction r = new FailingFunction();
2879 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor());
2632 <        f.complete(one);
2633 <        checkCompletedWithWrappedCFException(g);
2634 <    }
2872 >        checkCancelled(f);
2873 >        checkCompletedNormally(g, v1);
2874 >        checkCompletedWithWrappedCancellationException(h0);
2875 >        checkCompletedWithWrappedCancellationException(h1);
2876 >        checkCompletedWithWrappedCancellationException(h2);
2877 >        checkCompletedWithWrappedCancellationException(h3);
2878 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2879 >    }}
2880  
2881      /**
2882 <     * applyToEitherAsync result completes exceptionally if either source cancelled
2882 >     * runAfterEither result completes exceptionally if action does
2883       */
2884 <    public void testApplyToEitherAsync4E() {
2885 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2886 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2887 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2888 <        assertTrue(f.cancel(true));
2889 <        checkCompletedWithWrappedCancellationException(g);
2890 <
2891 <        f = new CompletableFuture<>();
2892 <        f2 = new CompletableFuture<>();
2893 <        assertTrue(f2.cancel(true));
2894 <        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2895 <        checkCompletedWithWrappedCancellationException(g);
2896 <    }
2884 >    public void testRunAfterEither_actionFailed() {
2885 >        for (ExecutionMode m : ExecutionMode.values())
2886 >        for (Integer v1 : new Integer[] { 1, null })
2887 >        for (Integer v2 : new Integer[] { 2, null })
2888 >    {
2889 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2890 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2891 >        final FailingRunnable[] rs = new FailingRunnable[6];
2892 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
2893 >
2894 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2895 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2896 >        assertTrue(f.complete(v1));
2897 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2898 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2899 >        checkCompletedWithWrappedCFException(h0);
2900 >        checkCompletedWithWrappedCFException(h1);
2901 >        checkCompletedWithWrappedCFException(h2);
2902 >        checkCompletedWithWrappedCFException(h3);
2903 >        for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2904 >        assertTrue(g.complete(v2));
2905 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2906 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2907 >        checkCompletedWithWrappedCFException(h4);
2908 >        checkCompletedWithWrappedCFException(h5);
2909 >
2910 >        checkCompletedNormally(f, v1);
2911 >        checkCompletedNormally(g, v2);
2912 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2913 >    }}
2914  
2915      /**
2916 <     * acceptEitherAsync result completes normally after normal
2655 <     * completion of sources
2916 >     * thenCompose result completes normally after normal completion of source
2917       */
2918 <    public void testAcceptEitherAsyncE() {
2919 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2920 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2921 <        IncAction r = new IncAction();
2922 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2923 <        f.complete(one);
2924 <        checkCompletedNormally(g, null);
2925 <        assertEquals(r.value, 2);
2926 <
2927 <        r = new IncAction();
2928 <        f = new CompletableFuture<>();
2929 <        f.complete(one);
2930 <        f2 = new CompletableFuture<>();
2931 <        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2932 <        checkCompletedNormally(g, null);
2672 <        assertEquals(r.value, 2);
2673 <    }
2918 >    public void testThenCompose_normalCompletion() {
2919 >        for (ExecutionMode m : ExecutionMode.values())
2920 >        for (boolean createIncomplete : new boolean[] { true, false })
2921 >        for (Integer v1 : new Integer[] { 1, null })
2922 >    {
2923 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2924 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2925 >        if (!createIncomplete) assertTrue(f.complete(v1));
2926 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2927 >        if (createIncomplete) assertTrue(f.complete(v1));
2928 >
2929 >        checkCompletedNormally(g, inc(v1));
2930 >        checkCompletedNormally(f, v1);
2931 >        r.assertValue(v1);
2932 >    }}
2933  
2934      /**
2935 <     * acceptEitherAsync result completes exceptionally after exceptional
2935 >     * thenCompose result completes exceptionally after exceptional
2936       * completion of source
2937       */
2938 <    public void testAcceptEitherAsync2E() {
2939 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2940 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2941 <        IncAction r = new IncAction();
2942 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2943 <        f.completeExceptionally(new CFException());
2944 <        checkCompletedWithWrappedCFException(g);
2945 <
2946 <        r = new IncAction();
2947 <        f = new CompletableFuture<>();
2948 <        f2 = new CompletableFuture<>();
2949 <        f2.completeExceptionally(new CFException());
2950 <        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2951 <        f.complete(one);
2952 <        checkCompletedWithWrappedCFException(g);
2694 <    }
2695 <
2696 <    /**
2697 <     * acceptEitherAsync result completes exceptionally if action does
2698 <     */
2699 <    public void testAcceptEitherAsync3E() {
2700 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2701 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2702 <        FailingConsumer r = new FailingConsumer();
2703 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2704 <        f.complete(one);
2705 <        checkCompletedWithWrappedCFException(g);
2706 <    }
2707 <
2708 <    /**
2709 <     * acceptEitherAsync result completes exceptionally if either
2710 <     * source cancelled
2711 <     */
2712 <    public void testAcceptEitherAsync4E() {
2713 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2714 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2715 <        IncAction r = new IncAction();
2716 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2717 <        assertTrue(f.cancel(true));
2718 <        checkCompletedWithWrappedCancellationException(g);
2719 <
2720 <        r = new IncAction();
2721 <        f = new CompletableFuture<>();
2722 <        f2 = new CompletableFuture<>();
2723 <        assertTrue(f2.cancel(true));
2724 <        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2725 <        checkCompletedWithWrappedCancellationException(g);
2726 <    }
2727 <
2728 <    /**
2729 <     * runAfterEitherAsync result completes normally after normal
2730 <     * completion of sources
2731 <     */
2732 <    public void testRunAfterEitherAsyncE() {
2733 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2734 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2735 <        Noop r = new Noop();
2736 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2737 <        f.complete(one);
2738 <        checkCompletedNormally(g, null);
2739 <        assertTrue(r.ran);
2740 <
2741 <        r = new Noop();
2742 <        f = new CompletableFuture<>();
2743 <        f.complete(one);
2744 <        f2 = new CompletableFuture<>();
2745 <        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2746 <        checkCompletedNormally(g, null);
2747 <        assertTrue(r.ran);
2748 <    }
2938 >    public void testThenCompose_exceptionalCompletion() {
2939 >        for (ExecutionMode m : ExecutionMode.values())
2940 >        for (boolean createIncomplete : new boolean[] { true, false })
2941 >    {
2942 >        final CFException ex = new CFException();
2943 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2944 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2945 >        if (!createIncomplete) f.completeExceptionally(ex);
2946 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2947 >        if (createIncomplete) f.completeExceptionally(ex);
2948 >
2949 >        checkCompletedWithWrappedException(g, ex);
2950 >        checkCompletedExceptionally(f, ex);
2951 >        r.assertNotInvoked();
2952 >    }}
2953  
2954      /**
2955 <     * runAfterEitherAsync result completes exceptionally after exceptional
2752 <     * completion of source
2955 >     * thenCompose result completes exceptionally if action does
2956       */
2957 <    public void testRunAfterEitherAsync2E() {
2958 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2959 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2960 <        Noop r = new Noop();
2961 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2962 <        f.completeExceptionally(new CFException());
2963 <        checkCompletedWithWrappedCFException(g);
2957 >    public void testThenCompose_actionFailed() {
2958 >        for (ExecutionMode m : ExecutionMode.values())
2959 >        for (boolean createIncomplete : new boolean[] { true, false })
2960 >        for (Integer v1 : new Integer[] { 1, null })
2961 >    {
2962 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2963 >        final FailingCompletableFutureFunction r
2964 >            = new FailingCompletableFutureFunction(m);
2965 >        if (!createIncomplete) assertTrue(f.complete(v1));
2966 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2967 >        if (createIncomplete) assertTrue(f.complete(v1));
2968  
2762        r = new Noop();
2763        f = new CompletableFuture<>();
2764        f2 = new CompletableFuture<>();
2765        f2.completeExceptionally(new CFException());
2766        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2767        f.complete(one);
2969          checkCompletedWithWrappedCFException(g);
2970 <    }
2970 >        checkCompletedNormally(f, v1);
2971 >    }}
2972  
2973      /**
2974 <     * runAfterEitherAsync result completes exceptionally if action does
2773 <     */
2774 <    public void testRunAfterEitherAsync3E() {
2775 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2776 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2777 <        FailingNoop r = new FailingNoop();
2778 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2779 <        f.complete(one);
2780 <        checkCompletedWithWrappedCFException(g);
2781 <    }
2782 <
2783 <    /**
2784 <     * runAfterEitherAsync result completes exceptionally if either
2785 <     * source cancelled
2974 >     * thenCompose result completes exceptionally if source cancelled
2975       */
2976 <    public void testRunAfterEitherAsync4E() {
2977 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2978 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2979 <        Noop r = new Noop();
2980 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2981 <        assertTrue(f.cancel(true));
2982 <        checkCompletedWithWrappedCancellationException(g);
2976 >    public void testThenCompose_sourceCancelled() {
2977 >        for (ExecutionMode m : ExecutionMode.values())
2978 >        for (boolean createIncomplete : new boolean[] { true, false })
2979 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2980 >    {
2981 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2982 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2983 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2984 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2985 >        if (createIncomplete) {
2986 >            checkIncomplete(g);
2987 >            assertTrue(f.cancel(mayInterruptIfRunning));
2988 >        }
2989  
2795        r = new Noop();
2796        f = new CompletableFuture<>();
2797        f2 = new CompletableFuture<>();
2798        assertTrue(f2.cancel(true));
2799        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2990          checkCompletedWithWrappedCancellationException(g);
2991 <    }
2992 <
2803 <    /**
2804 <     * thenComposeAsync result completes normally after normal
2805 <     * completion of source
2806 <     */
2807 <    public void testThenComposeAsyncE() {
2808 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2809 <        CompletableFutureInc r = new CompletableFutureInc();
2810 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2811 <        f.complete(one);
2812 <        checkCompletedNormally(g, two);
2813 <    }
2814 <
2815 <    /**
2816 <     * thenComposeAsync result completes exceptionally after
2817 <     * exceptional completion of source
2818 <     */
2819 <    public void testThenComposeAsync2E() {
2820 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2821 <        CompletableFutureInc r = new CompletableFutureInc();
2822 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2823 <        f.completeExceptionally(new CFException());
2824 <        checkCompletedWithWrappedCFException(g);
2825 <    }
2991 >        checkCancelled(f);
2992 >    }}
2993  
2994      /**
2995 <     * thenComposeAsync result completes exceptionally if action does
2995 >     * thenCompose result completes exceptionally if the result of the action does
2996       */
2997 <    public void testThenComposeAsync3E() {
2998 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2999 <        FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
3000 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
3001 <        f.complete(one);
3002 <        checkCompletedWithWrappedCFException(g);
3003 <    }
2997 >    public void testThenCompose_actionReturnsFailingFuture() {
2998 >        for (ExecutionMode m : ExecutionMode.values())
2999 >        for (int order = 0; order < 6; order++)
3000 >        for (Integer v1 : new Integer[] { 1, null })
3001 >    {
3002 >        final CFException ex = new CFException();
3003 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3004 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
3005 >        final CompletableFuture<Integer> h;
3006 >        // Test all permutations of orders
3007 >        switch (order) {
3008 >        case 0:
3009 >            assertTrue(f.complete(v1));
3010 >            assertTrue(g.completeExceptionally(ex));
3011 >            h = m.thenCompose(f, (x -> g));
3012 >            break;
3013 >        case 1:
3014 >            assertTrue(f.complete(v1));
3015 >            h = m.thenCompose(f, (x -> g));
3016 >            assertTrue(g.completeExceptionally(ex));
3017 >            break;
3018 >        case 2:
3019 >            assertTrue(g.completeExceptionally(ex));
3020 >            assertTrue(f.complete(v1));
3021 >            h = m.thenCompose(f, (x -> g));
3022 >            break;
3023 >        case 3:
3024 >            assertTrue(g.completeExceptionally(ex));
3025 >            h = m.thenCompose(f, (x -> g));
3026 >            assertTrue(f.complete(v1));
3027 >            break;
3028 >        case 4:
3029 >            h = m.thenCompose(f, (x -> g));
3030 >            assertTrue(f.complete(v1));
3031 >            assertTrue(g.completeExceptionally(ex));
3032 >            break;
3033 >        case 5:
3034 >            h = m.thenCompose(f, (x -> g));
3035 >            assertTrue(f.complete(v1));
3036 >            assertTrue(g.completeExceptionally(ex));
3037 >            break;
3038 >        default: throw new AssertionError();
3039 >        }
3040  
3041 <    /**
3042 <     * thenComposeAsync result completes exceptionally if source cancelled
3043 <     */
3044 <    public void testThenComposeAsync4E() {
2842 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2843 <        CompletableFutureInc r = new CompletableFutureInc();
2844 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2845 <        assertTrue(f.cancel(true));
2846 <        checkCompletedWithWrappedCancellationException(g);
2847 <    }
3041 >        checkCompletedExceptionally(g, ex);
3042 >        checkCompletedWithWrappedException(h, ex);
3043 >        checkCompletedNormally(f, v1);
3044 >    }}
3045  
3046      // other static methods
3047  
# Line 2862 | Line 3059 | public class CompletableFutureTest exten
3059       * when all components complete normally
3060       */
3061      public void testAllOf_normal() throws Exception {
3062 <        for (int k = 1; k < 20; ++k) {
3063 <            CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3064 <            for (int i = 0; i < k; ++i)
3062 >        for (int k = 1; k < 10; k++) {
3063 >            CompletableFuture<Integer>[] fs
3064 >                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3065 >            for (int i = 0; i < k; i++)
3066 >                fs[i] = new CompletableFuture<>();
3067 >            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3068 >            for (int i = 0; i < k; i++) {
3069 >                checkIncomplete(f);
3070 >                checkIncomplete(CompletableFuture.allOf(fs));
3071 >                fs[i].complete(one);
3072 >            }
3073 >            checkCompletedNormally(f, null);
3074 >            checkCompletedNormally(CompletableFuture.allOf(fs), null);
3075 >        }
3076 >    }
3077 >
3078 >    public void testAllOf_backwards() throws Exception {
3079 >        for (int k = 1; k < 10; k++) {
3080 >            CompletableFuture<Integer>[] fs
3081 >                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3082 >            for (int i = 0; i < k; i++)
3083                  fs[i] = new CompletableFuture<>();
3084              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3085 <            for (int i = 0; i < k; ++i) {
3085 >            for (int i = k - 1; i >= 0; i--) {
3086                  checkIncomplete(f);
3087                  checkIncomplete(CompletableFuture.allOf(fs));
3088                  fs[i].complete(one);
# Line 2877 | Line 3092 | public class CompletableFutureTest exten
3092          }
3093      }
3094  
3095 +    public void testAllOf_exceptional() throws Exception {
3096 +        for (int k = 1; k < 10; k++) {
3097 +            CompletableFuture<Integer>[] fs
3098 +                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3099 +            CFException ex = new CFException();
3100 +            for (int i = 0; i < k; i++)
3101 +                fs[i] = new CompletableFuture<>();
3102 +            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3103 +            for (int i = 0; i < k; i++) {
3104 +                checkIncomplete(f);
3105 +                checkIncomplete(CompletableFuture.allOf(fs));
3106 +                if (i != k/2) {
3107 +                    fs[i].complete(i);
3108 +                    checkCompletedNormally(fs[i], i);
3109 +                } else {
3110 +                    fs[i].completeExceptionally(ex);
3111 +                    checkCompletedExceptionally(fs[i], ex);
3112 +                }
3113 +            }
3114 +            checkCompletedWithWrappedException(f, ex);
3115 +            checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex);
3116 +        }
3117 +    }
3118 +
3119      /**
3120       * anyOf(no component futures) returns an incomplete future
3121       */
3122      public void testAnyOf_empty() throws Exception {
3123 +        for (Integer v1 : new Integer[] { 1, null })
3124 +    {
3125          CompletableFuture<Object> f = CompletableFuture.anyOf();
3126          checkIncomplete(f);
3127 <    }
3127 >
3128 >        f.complete(v1);
3129 >        checkCompletedNormally(f, v1);
3130 >    }}
3131  
3132      /**
3133       * anyOf returns a future completed normally with a value when
3134       * a component future does
3135       */
3136      public void testAnyOf_normal() throws Exception {
3137 <        for (int k = 0; k < 10; ++k) {
3137 >        for (int k = 0; k < 10; k++) {
3138              CompletableFuture[] fs = new CompletableFuture[k];
3139 <            for (int i = 0; i < k; ++i)
3139 >            for (int i = 0; i < k; i++)
3140                  fs[i] = new CompletableFuture<>();
3141              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3142              checkIncomplete(f);
3143 <            for (int i = 0; i < k; ++i) {
3144 <                fs[i].complete(one);
3145 <                checkCompletedNormally(f, one);
3146 <                checkCompletedNormally(CompletableFuture.anyOf(fs), one);
3143 >            for (int i = 0; i < k; i++) {
3144 >                fs[i].complete(i);
3145 >                checkCompletedNormally(f, 0);
3146 >                int x = (int) CompletableFuture.anyOf(fs).join();
3147 >                assertTrue(0 <= x && x <= i);
3148 >            }
3149 >        }
3150 >    }
3151 >    public void testAnyOf_normal_backwards() throws Exception {
3152 >        for (int k = 0; k < 10; k++) {
3153 >            CompletableFuture[] fs = new CompletableFuture[k];
3154 >            for (int i = 0; i < k; i++)
3155 >                fs[i] = new CompletableFuture<>();
3156 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3157 >            checkIncomplete(f);
3158 >            for (int i = k - 1; i >= 0; i--) {
3159 >                fs[i].complete(i);
3160 >                checkCompletedNormally(f, k - 1);
3161 >                int x = (int) CompletableFuture.anyOf(fs).join();
3162 >                assertTrue(i <= x && x <= k - 1);
3163              }
3164          }
3165      }
# Line 2908 | Line 3168 | public class CompletableFutureTest exten
3168       * anyOf result completes exceptionally when any component does.
3169       */
3170      public void testAnyOf_exceptional() throws Exception {
3171 <        for (int k = 0; k < 10; ++k) {
3171 >        for (int k = 0; k < 10; k++) {
3172 >            CompletableFuture[] fs = new CompletableFuture[k];
3173 >            CFException[] exs = new CFException[k];
3174 >            for (int i = 0; i < k; i++) {
3175 >                fs[i] = new CompletableFuture<>();
3176 >                exs[i] = new CFException();
3177 >            }
3178 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3179 >            checkIncomplete(f);
3180 >            for (int i = 0; i < k; i++) {
3181 >                fs[i].completeExceptionally(exs[i]);
3182 >                checkCompletedWithWrappedException(f, exs[0]);
3183 >                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3184 >            }
3185 >        }
3186 >    }
3187 >
3188 >    public void testAnyOf_exceptional_backwards() throws Exception {
3189 >        for (int k = 0; k < 10; k++) {
3190              CompletableFuture[] fs = new CompletableFuture[k];
3191 <            for (int i = 0; i < k; ++i)
3191 >            CFException[] exs = new CFException[k];
3192 >            for (int i = 0; i < k; i++) {
3193                  fs[i] = new CompletableFuture<>();
3194 +                exs[i] = new CFException();
3195 +            }
3196              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3197              checkIncomplete(f);
3198 <            for (int i = 0; i < k; ++i) {
3199 <                fs[i].completeExceptionally(new CFException());
3200 <                checkCompletedWithWrappedCFException(f);
3198 >            for (int i = k - 1; i >= 0; i--) {
3199 >                fs[i].completeExceptionally(exs[i]);
3200 >                checkCompletedWithWrappedException(f, exs[k - 1]);
3201                  checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3202              }
3203          }
# Line 2929 | Line 3210 | public class CompletableFutureTest exten
3210          CompletableFuture<Integer> f = new CompletableFuture<>();
3211          CompletableFuture<Integer> g = new CompletableFuture<>();
3212          CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null;
2932        CompletableFuture<?> h;
3213          ThreadExecutor exec = new ThreadExecutor();
3214  
3215          Runnable[] throwingActions = {
3216 <            () -> { CompletableFuture.supplyAsync(null); },
3217 <            () -> { CompletableFuture.supplyAsync(null, exec); },
3218 <            () -> { CompletableFuture.supplyAsync(supplyOne, null); },
3219 <
3220 <            () -> { CompletableFuture.runAsync(null); },
3221 <            () -> { CompletableFuture.runAsync(null, exec); },
3222 <            () -> { CompletableFuture.runAsync(() -> {}, null); },
3223 <
3224 <            () -> { f.completeExceptionally(null); },
3225 <
3226 <            () -> { f.thenApply(null); },
3227 <            () -> { f.thenApplyAsync(null); },
3228 <            () -> { f.thenApplyAsync((x) -> x, null); },
3229 <            () -> { f.thenApplyAsync(null, exec); },
3230 <
3231 <            () -> { f.thenAccept(null); },
3232 <            () -> { f.thenAcceptAsync(null); },
3233 <            () -> { f.thenAcceptAsync((x) -> { ; }, null); },
3234 <            () -> { f.thenAcceptAsync(null, exec); },
3235 <
3236 <            () -> { f.thenRun(null); },
3237 <            () -> { f.thenRunAsync(null); },
3238 <            () -> { f.thenRunAsync(() -> { ; }, null); },
3239 <            () -> { f.thenRunAsync(null, exec); },
3240 <
3241 <            () -> { f.thenCombine(g, null); },
3242 <            () -> { f.thenCombineAsync(g, null); },
3243 <            () -> { f.thenCombineAsync(g, null, exec); },
3244 <            () -> { f.thenCombine(nullFuture, (x, y) -> x); },
3245 <            () -> { f.thenCombineAsync(nullFuture, (x, y) -> x); },
3246 <            () -> { f.thenCombineAsync(nullFuture, (x, y) -> x, exec); },
3247 <            () -> { f.thenCombineAsync(g, (x, y) -> x, null); },
3248 <
3249 <            () -> { f.thenAcceptBoth(g, null); },
3250 <            () -> { f.thenAcceptBothAsync(g, null); },
3251 <            () -> { f.thenAcceptBothAsync(g, null, exec); },
3252 <            () -> { f.thenAcceptBoth(nullFuture, (x, y) -> {}); },
3253 <            () -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}); },
3254 <            () -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec); },
3255 <            () -> { f.thenAcceptBothAsync(g, (x, y) -> {}, null); },
3256 <
3257 <            () -> { f.runAfterBoth(g, null); },
3258 <            () -> { f.runAfterBothAsync(g, null); },
3259 <            () -> { f.runAfterBothAsync(g, null, exec); },
3260 <            () -> { f.runAfterBoth(nullFuture, () -> {}); },
3261 <            () -> { f.runAfterBothAsync(nullFuture, () -> {}); },
3262 <            () -> { f.runAfterBothAsync(nullFuture, () -> {}, exec); },
3263 <            () -> { f.runAfterBothAsync(g, () -> {}, null); },
3264 <
3265 <            () -> { f.applyToEither(g, null); },
3266 <            () -> { f.applyToEitherAsync(g, null); },
3267 <            () -> { f.applyToEitherAsync(g, null, exec); },
3268 <            () -> { f.applyToEither(nullFuture, (x) -> x); },
3269 <            () -> { f.applyToEitherAsync(nullFuture, (x) -> x); },
3270 <            () -> { f.applyToEitherAsync(nullFuture, (x) -> x, exec); },
3271 <            () -> { f.applyToEitherAsync(g, (x) -> x, null); },
3272 <
3273 <            () -> { f.acceptEither(g, null); },
3274 <            () -> { f.acceptEitherAsync(g, null); },
3275 <            () -> { f.acceptEitherAsync(g, null, exec); },
3276 <            () -> { f.acceptEither(nullFuture, (x) -> {}); },
3277 <            () -> { f.acceptEitherAsync(nullFuture, (x) -> {}); },
3278 <            () -> { f.acceptEitherAsync(nullFuture, (x) -> {}, exec); },
3279 <            () -> { f.acceptEitherAsync(g, (x) -> {}, null); },
3280 <
3281 <            () -> { f.runAfterEither(g, null); },
3282 <            () -> { f.runAfterEitherAsync(g, null); },
3283 <            () -> { f.runAfterEitherAsync(g, null, exec); },
3284 <            () -> { f.runAfterEither(nullFuture, () -> {}); },
3285 <            () -> { f.runAfterEitherAsync(nullFuture, () -> {}); },
3286 <            () -> { f.runAfterEitherAsync(nullFuture, () -> {}, exec); },
3287 <            () -> { f.runAfterEitherAsync(g, () -> {}, null); },
3288 <
3289 <            () -> { f.thenCompose(null); },
3290 <            () -> { f.thenComposeAsync(null); },
3291 <            () -> { f.thenComposeAsync(new CompletableFutureInc(), null); },
3292 <            () -> { f.thenComposeAsync(null, exec); },
3293 <
3294 <            () -> { f.exceptionally(null); },
3295 <
3296 <            () -> { f.handle(null); },
3297 <
3298 <            () -> { CompletableFuture.allOf((CompletableFuture<?>)null); },
3299 <            () -> { CompletableFuture.allOf((CompletableFuture<?>[])null); },
3300 <            () -> { CompletableFuture.allOf(f, null); },
3301 <            () -> { CompletableFuture.allOf(null, f); },
3302 <
3303 <            () -> { CompletableFuture.anyOf((CompletableFuture<?>)null); },
3304 <            () -> { CompletableFuture.anyOf((CompletableFuture<?>[])null); },
3305 <            () -> { CompletableFuture.anyOf(f, null); },
3306 <            () -> { CompletableFuture.anyOf(null, f); },
3216 >            () -> CompletableFuture.supplyAsync(null),
3217 >            () -> CompletableFuture.supplyAsync(null, exec),
3218 >            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null),
3219 >
3220 >            () -> CompletableFuture.runAsync(null),
3221 >            () -> CompletableFuture.runAsync(null, exec),
3222 >            () -> CompletableFuture.runAsync(() -> {}, null),
3223 >
3224 >            () -> f.completeExceptionally(null),
3225 >
3226 >            () -> f.thenApply(null),
3227 >            () -> f.thenApplyAsync(null),
3228 >            () -> f.thenApplyAsync((x) -> x, null),
3229 >            () -> f.thenApplyAsync(null, exec),
3230 >
3231 >            () -> f.thenAccept(null),
3232 >            () -> f.thenAcceptAsync(null),
3233 >            () -> f.thenAcceptAsync((x) -> {} , null),
3234 >            () -> f.thenAcceptAsync(null, exec),
3235 >
3236 >            () -> f.thenRun(null),
3237 >            () -> f.thenRunAsync(null),
3238 >            () -> f.thenRunAsync(() -> {} , null),
3239 >            () -> f.thenRunAsync(null, exec),
3240 >
3241 >            () -> f.thenCombine(g, null),
3242 >            () -> f.thenCombineAsync(g, null),
3243 >            () -> f.thenCombineAsync(g, null, exec),
3244 >            () -> f.thenCombine(nullFuture, (x, y) -> x),
3245 >            () -> f.thenCombineAsync(nullFuture, (x, y) -> x),
3246 >            () -> f.thenCombineAsync(nullFuture, (x, y) -> x, exec),
3247 >            () -> f.thenCombineAsync(g, (x, y) -> x, null),
3248 >
3249 >            () -> f.thenAcceptBoth(g, null),
3250 >            () -> f.thenAcceptBothAsync(g, null),
3251 >            () -> f.thenAcceptBothAsync(g, null, exec),
3252 >            () -> f.thenAcceptBoth(nullFuture, (x, y) -> {}),
3253 >            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}),
3254 >            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec),
3255 >            () -> f.thenAcceptBothAsync(g, (x, y) -> {}, null),
3256 >
3257 >            () -> f.runAfterBoth(g, null),
3258 >            () -> f.runAfterBothAsync(g, null),
3259 >            () -> f.runAfterBothAsync(g, null, exec),
3260 >            () -> f.runAfterBoth(nullFuture, () -> {}),
3261 >            () -> f.runAfterBothAsync(nullFuture, () -> {}),
3262 >            () -> f.runAfterBothAsync(nullFuture, () -> {}, exec),
3263 >            () -> f.runAfterBothAsync(g, () -> {}, null),
3264 >
3265 >            () -> f.applyToEither(g, null),
3266 >            () -> f.applyToEitherAsync(g, null),
3267 >            () -> f.applyToEitherAsync(g, null, exec),
3268 >            () -> f.applyToEither(nullFuture, (x) -> x),
3269 >            () -> f.applyToEitherAsync(nullFuture, (x) -> x),
3270 >            () -> f.applyToEitherAsync(nullFuture, (x) -> x, exec),
3271 >            () -> f.applyToEitherAsync(g, (x) -> x, null),
3272 >
3273 >            () -> f.acceptEither(g, null),
3274 >            () -> f.acceptEitherAsync(g, null),
3275 >            () -> f.acceptEitherAsync(g, null, exec),
3276 >            () -> f.acceptEither(nullFuture, (x) -> {}),
3277 >            () -> f.acceptEitherAsync(nullFuture, (x) -> {}),
3278 >            () -> f.acceptEitherAsync(nullFuture, (x) -> {}, exec),
3279 >            () -> f.acceptEitherAsync(g, (x) -> {}, null),
3280 >
3281 >            () -> f.runAfterEither(g, null),
3282 >            () -> f.runAfterEitherAsync(g, null),
3283 >            () -> f.runAfterEitherAsync(g, null, exec),
3284 >            () -> f.runAfterEither(nullFuture, () -> {}),
3285 >            () -> f.runAfterEitherAsync(nullFuture, () -> {}),
3286 >            () -> f.runAfterEitherAsync(nullFuture, () -> {}, exec),
3287 >            () -> f.runAfterEitherAsync(g, () -> {}, null),
3288 >
3289 >            () -> f.thenCompose(null),
3290 >            () -> f.thenComposeAsync(null),
3291 >            () -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null),
3292 >            () -> f.thenComposeAsync(null, exec),
3293 >
3294 >            () -> f.exceptionally(null),
3295 >
3296 >            () -> f.handle(null),
3297 >
3298 >            () -> CompletableFuture.allOf((CompletableFuture<?>)null),
3299 >            () -> CompletableFuture.allOf((CompletableFuture<?>[])null),
3300 >            () -> CompletableFuture.allOf(f, null),
3301 >            () -> CompletableFuture.allOf(null, f),
3302 >
3303 >            () -> CompletableFuture.anyOf((CompletableFuture<?>)null),
3304 >            () -> CompletableFuture.anyOf((CompletableFuture<?>[])null),
3305 >            () -> CompletableFuture.anyOf(f, null),
3306 >            () -> CompletableFuture.anyOf(null, f),
3307 >
3308 >            () -> f.obtrudeException(null),
3309          };
3310  
3311          assertThrows(NullPointerException.class, throwingActions);
# Line 3038 | Line 3320 | public class CompletableFutureTest exten
3320          assertSame(f, f.toCompletableFuture());
3321      }
3322  
3323 <    /**
3042 <     * whenComplete action executes on normal completion, propagating
3043 <     * source result.
3044 <     */
3045 <    public void testWhenComplete1() {
3046 <        final AtomicInteger a = new AtomicInteger();
3047 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3048 <        CompletableFuture<Integer> g =
3049 <            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3050 <        f.complete(three);
3051 <        checkCompletedNormally(f, three);
3052 <        checkCompletedNormally(g, three);
3053 <        assertEquals(a.get(), 1);
3054 <    }
3055 <
3056 <    /**
3057 <     * whenComplete action executes on exceptional completion, propagating
3058 <     * source result.
3059 <     */
3060 <    public void testWhenComplete2() {
3061 <        final AtomicInteger a = new AtomicInteger();
3062 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3063 <        CompletableFuture<Integer> g =
3064 <            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3065 <        f.completeExceptionally(new CFException());
3066 <        assertTrue(f.isCompletedExceptionally());
3067 <        assertTrue(g.isCompletedExceptionally());
3068 <        assertEquals(a.get(), 1);
3069 <    }
3070 <
3071 <    /**
3072 <     * If a whenComplete action throws an exception when triggered by
3073 <     * a normal completion, it completes exceptionally
3074 <     */
3075 <    public void testWhenComplete3() {
3076 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3077 <        CompletableFuture<Integer> g =
3078 <            f.whenComplete((Integer x, Throwable t) ->
3079 <                           { throw new CFException(); } );
3080 <        f.complete(three);
3081 <        checkCompletedNormally(f, three);
3082 <        assertTrue(g.isCompletedExceptionally());
3083 <        checkCompletedWithWrappedCFException(g);
3084 <    }
3085 <
3086 <    /**
3087 <     * whenCompleteAsync action executes on normal completion, propagating
3088 <     * source result.
3089 <     */
3090 <    public void testWhenCompleteAsync1() {
3091 <        final AtomicInteger a = new AtomicInteger();
3092 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3093 <        CompletableFuture<Integer> g =
3094 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3095 <        f.complete(three);
3096 <        checkCompletedNormally(f, three);
3097 <        checkCompletedNormally(g, three);
3098 <        assertEquals(a.get(), 1);
3099 <    }
3100 <
3101 <    /**
3102 <     * whenCompleteAsync action executes on exceptional completion, propagating
3103 <     * source result.
3104 <     */
3105 <    public void testWhenCompleteAsync2() {
3106 <        final AtomicInteger a = new AtomicInteger();
3107 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3108 <        CompletableFuture<Integer> g =
3109 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3110 <        f.completeExceptionally(new CFException());
3111 <        checkCompletedWithWrappedCFException(f);
3112 <        checkCompletedWithWrappedCFException(g);
3113 <    }
3114 <
3115 <    /**
3116 <     * If a whenCompleteAsync action throws an exception when
3117 <     * triggered by a normal completion, it completes exceptionally
3118 <     */
3119 <    public void testWhenCompleteAsync3() {
3120 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3121 <        CompletableFuture<Integer> g =
3122 <            f.whenCompleteAsync((Integer x, Throwable t) ->
3123 <                           { throw new CFException(); } );
3124 <        f.complete(three);
3125 <        checkCompletedNormally(f, three);
3126 <        checkCompletedWithWrappedCFException(g);
3127 <    }
3323 >    //--- tests of implementation details; not part of official tck ---
3324  
3325 <    /**
3326 <     * whenCompleteAsync action executes on normal completion, propagating
3327 <     * source result.
3328 <     */
3329 <    public void testWhenCompleteAsync1e() {
3330 <        final AtomicInteger a = new AtomicInteger();
3331 <        ThreadExecutor exec = new ThreadExecutor();
3136 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3137 <        CompletableFuture<Integer> g =
3138 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3139 <                                exec);
3140 <        f.complete(three);
3141 <        checkCompletedNormally(f, three);
3142 <        checkCompletedNormally(g, three);
3143 <        assertEquals(a.get(), 1);
3325 >    Object resultOf(CompletableFuture<?> f) {
3326 >        try {
3327 >            java.lang.reflect.Field resultField
3328 >                = CompletableFuture.class.getDeclaredField("result");
3329 >            resultField.setAccessible(true);
3330 >            return resultField.get(f);
3331 >        } catch (Throwable t) { throw new AssertionError(t); }
3332      }
3333  
3334 <    /**
3335 <     * whenCompleteAsync action executes on exceptional completion, propagating
3336 <     * source result.
3337 <     */
3338 <    public void testWhenCompleteAsync2e() {
3339 <        final AtomicInteger a = new AtomicInteger();
3340 <        ThreadExecutor exec = new ThreadExecutor();
3153 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3154 <        CompletableFuture<Integer> g =
3155 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3156 <                                exec);
3157 <        f.completeExceptionally(new CFException());
3158 <        checkCompletedWithWrappedCFException(f);
3159 <        checkCompletedWithWrappedCFException(g);
3160 <    }
3334 >    public void testExceptionPropagationReusesResultObject() {
3335 >        if (!testImplementationDetails) return;
3336 >        for (ExecutionMode m : ExecutionMode.values())
3337 >    {
3338 >        final CFException ex = new CFException();
3339 >        final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42);
3340 >        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3341  
3342 <    /**
3343 <     * If a whenCompleteAsync action throws an exception when triggered
3164 <     * by a normal completion, it completes exceptionally
3165 <     */
3166 <    public void testWhenCompleteAsync3e() {
3167 <        ThreadExecutor exec = new ThreadExecutor();
3168 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3169 <        CompletableFuture<Integer> g =
3170 <            f.whenCompleteAsync((Integer x, Throwable t) ->
3171 <                                { throw new CFException(); },
3172 <                                exec);
3173 <        f.complete(three);
3174 <        checkCompletedNormally(f, three);
3175 <        checkCompletedWithWrappedCFException(g);
3176 <    }
3342 >        List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs
3343 >            = new ArrayList<>();
3344  
3345 <    /**
3346 <     * handleAsync action completes normally with function value on
3347 <     * either normal or exceptional completion of source
3181 <     */
3182 <    public void testHandleAsync() {
3183 <        CompletableFuture<Integer> f, g;
3184 <        IntegerHandler r;
3345 >        funs.add((y) -> m.thenRun(y, new Noop(m)));
3346 >        funs.add((y) -> m.thenAccept(y, new NoopConsumer(m)));
3347 >        funs.add((y) -> m.thenApply(y, new IncFunction(m)));
3348  
3349 <        f = new CompletableFuture<>();
3350 <        g = f.handleAsync(r = new IntegerHandler());
3351 <        assertFalse(r.ran);
3189 <        f.completeExceptionally(new CFException());
3190 <        checkCompletedWithWrappedCFException(f);
3191 <        checkCompletedNormally(g, three);
3192 <        assertTrue(r.ran);
3349 >        funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m)));
3350 >        funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m)));
3351 >        funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m)));
3352  
3353 <        f = new CompletableFuture<>();
3354 <        g = f.handleAsync(r = new IntegerHandler());
3355 <        assertFalse(r.ran);
3197 <        f.completeExceptionally(new CFException());
3198 <        checkCompletedWithWrappedCFException(f);
3199 <        checkCompletedNormally(g, three);
3200 <        assertTrue(r.ran);
3353 >        funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m)));
3354 >        funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
3355 >        funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m)));
3356  
3357 <        f = new CompletableFuture<>();
3203 <        g = f.handleAsync(r = new IntegerHandler());
3204 <        assertFalse(r.ran);
3205 <        f.complete(one);
3206 <        checkCompletedNormally(f, one);
3207 <        checkCompletedNormally(g, two);
3208 <        assertTrue(r.ran);
3357 >        funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {}));
3358  
3359 <        f = new CompletableFuture<>();
3211 <        g = f.handleAsync(r = new IntegerHandler());
3212 <        assertFalse(r.ran);
3213 <        f.complete(one);
3214 <        checkCompletedNormally(f, one);
3215 <        checkCompletedNormally(g, two);
3216 <        assertTrue(r.ran);
3217 <    }
3359 >        funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m)));
3360  
3361 <    /**
3362 <     * handleAsync action with Executor completes normally with
3221 <     * function value on either normal or exceptional completion of
3222 <     * source
3223 <     */
3224 <    public void testHandleAsync2() {
3225 <        CompletableFuture<Integer> f, g;
3226 <        ThreadExecutor exec = new ThreadExecutor();
3227 <        IntegerHandler r;
3361 >        funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42}));
3362 >        funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete}));
3363  
3364 <        f = new CompletableFuture<>();
3365 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3366 <        assertFalse(r.ran);
3367 <        f.completeExceptionally(new CFException());
3368 <        checkCompletedWithWrappedCFException(f);
3369 <        checkCompletedNormally(g, three);
3370 <        assertTrue(r.ran);
3364 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3365 >                 fun : funs) {
3366 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3367 >            f.completeExceptionally(ex);
3368 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3369 >            checkCompletedWithWrappedException(src, ex);
3370 >            CompletableFuture<?> dep = fun.apply(src);
3371 >            checkCompletedWithWrappedException(dep, ex);
3372 >            assertSame(resultOf(src), resultOf(dep));
3373 >        }
3374  
3375 <        f = new CompletableFuture<>();
3376 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3377 <        assertFalse(r.ran);
3378 <        f.completeExceptionally(new CFException());
3379 <        checkCompletedWithWrappedCFException(f);
3380 <        checkCompletedNormally(g, three);
3381 <        assertTrue(r.ran);
3375 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3376 >                 fun : funs) {
3377 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3378 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3379 >            CompletableFuture<?> dep = fun.apply(src);
3380 >            f.completeExceptionally(ex);
3381 >            checkCompletedWithWrappedException(src, ex);
3382 >            checkCompletedWithWrappedException(dep, ex);
3383 >            assertSame(resultOf(src), resultOf(dep));
3384 >        }
3385  
3386 <        f = new CompletableFuture<>();
3387 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3388 <        assertFalse(r.ran);
3389 <        f.complete(one);
3390 <        checkCompletedNormally(f, one);
3391 <        checkCompletedNormally(g, two);
3392 <        assertTrue(r.ran);
3386 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3387 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3388 >                 fun : funs) {
3389 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3390 >            f.cancel(mayInterruptIfRunning);
3391 >            checkCancelled(f);
3392 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3393 >            checkCompletedWithWrappedCancellationException(src);
3394 >            CompletableFuture<?> dep = fun.apply(src);
3395 >            checkCompletedWithWrappedCancellationException(dep);
3396 >            assertSame(resultOf(src), resultOf(dep));
3397 >        }
3398  
3399 <        f = new CompletableFuture<>();
3400 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3401 <        assertFalse(r.ran);
3402 <        f.complete(one);
3403 <        checkCompletedNormally(f, one);
3404 <        checkCompletedNormally(g, two);
3405 <        assertTrue(r.ran);
3406 <    }
3399 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3400 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3401 >                 fun : funs) {
3402 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3403 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3404 >            CompletableFuture<?> dep = fun.apply(src);
3405 >            f.cancel(mayInterruptIfRunning);
3406 >            checkCancelled(f);
3407 >            checkCompletedWithWrappedCancellationException(src);
3408 >            checkCompletedWithWrappedCancellationException(dep);
3409 >            assertSame(resultOf(src), resultOf(dep));
3410 >        }
3411 >    }}
3412  
3413   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines