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.33 by jsr166, Sun Jun 1 20:40:13 2014 UTC vs.
Revision 1.133 by jsr166, Sun Nov 15 19:39:25 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 > import static java.util.concurrent.CompletableFuture.completedFuture;
11 > import static java.util.concurrent.CompletableFuture.failedFuture;
12 >
13 > import java.lang.reflect.Method;
14 > import java.lang.reflect.Modifier;
15 >
16 > import java.util.stream.Collectors;
17 > import java.util.stream.Stream;
18 >
19 > import java.util.ArrayList;
20 > import java.util.Arrays;
21 > import java.util.List;
22 > import java.util.Objects;
23 > import java.util.Set;
24   import java.util.concurrent.Callable;
10 import java.util.concurrent.Executor;
11 import java.util.concurrent.ExecutorService;
12 import java.util.concurrent.Executors;
25   import java.util.concurrent.CancellationException;
14 import java.util.concurrent.CountDownLatch;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.Future;
26   import java.util.concurrent.CompletableFuture;
27   import java.util.concurrent.CompletionException;
28 + import java.util.concurrent.CompletionStage;
29 + import java.util.concurrent.ExecutionException;
30 + import java.util.concurrent.Executor;
31 + import java.util.concurrent.ForkJoinPool;
32 + import java.util.concurrent.ForkJoinTask;
33   import java.util.concurrent.TimeoutException;
34 + import java.util.concurrent.TimeUnit;
35   import java.util.concurrent.atomic.AtomicInteger;
36 < 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;
36 > import java.util.concurrent.atomic.AtomicReference;
37   import java.util.function.BiConsumer;
27 import java.util.function.Function;
38   import java.util.function.BiFunction;
39 + import java.util.function.Consumer;
40 + import java.util.function.Function;
41 + import java.util.function.Predicate;
42 + import java.util.function.Supplier;
43 +
44 + import junit.framework.AssertionFailedError;
45 + import junit.framework.Test;
46 + import junit.framework.TestSuite;
47  
48   public class CompletableFutureTest extends JSR166TestCase {
49  
50      public static void main(String[] args) {
51 <        junit.textui.TestRunner.run(suite());
51 >        main(suite(), args);
52      }
53      public static Test suite() {
54          return new TestSuite(CompletableFutureTest.class);
# Line 41 | Line 59 | public class CompletableFutureTest exten
59      void checkIncomplete(CompletableFuture<?> f) {
60          assertFalse(f.isDone());
61          assertFalse(f.isCancelled());
62 <        assertTrue(f.toString().contains("[Not completed]"));
62 >        assertTrue(f.toString().contains("Not completed"));
63          try {
64              assertNull(f.getNow(null));
65          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 54 | Line 72 | public class CompletableFutureTest exten
72      }
73  
74      <T> void checkCompletedNormally(CompletableFuture<T> f, T value) {
75 <        try {
76 <            assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS));
59 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
75 >        checkTimedGet(f, value);
76 >
77          try {
78              assertEquals(value, f.join());
79          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 72 | Line 89 | public class CompletableFutureTest exten
89          assertTrue(f.toString().contains("[Completed normally]"));
90      }
91  
92 <    void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
93 <        try {
94 <            f.get(LONG_DELAY_MS, MILLISECONDS);
95 <            shouldThrow();
96 <        } catch (ExecutionException success) {
97 <            assertTrue(success.getCause() instanceof CFException);
98 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
99 <        try {
100 <            f.join();
101 <            shouldThrow();
102 <        } catch (CompletionException success) {
103 <            assertTrue(success.getCause() instanceof CFException);
104 <        }
105 <        try {
106 <            f.getNow(null);
107 <            shouldThrow();
108 <        } catch (CompletionException success) {
92 <            assertTrue(success.getCause() instanceof CFException);
92 >    /**
93 >     * Returns the "raw" internal exceptional completion of f,
94 >     * without any additional wrapping with CompletionException.
95 >     */
96 >    <U> Throwable exceptionalCompletion(CompletableFuture<U> f) {
97 >        // handle (and whenComplete) can distinguish between "direct"
98 >        // and "wrapped" exceptional completion
99 >        return f.handle((U u, Throwable t) -> t).join();
100 >    }
101 >
102 >    void checkCompletedExceptionally(CompletableFuture<?> f,
103 >                                     boolean wrapped,
104 >                                     Consumer<Throwable> checker) {
105 >        Throwable cause = exceptionalCompletion(f);
106 >        if (wrapped) {
107 >            assertTrue(cause instanceof CompletionException);
108 >            cause = cause.getCause();
109          }
110 <        try {
95 <            f.get();
96 <            shouldThrow();
97 <        } catch (ExecutionException success) {
98 <            assertTrue(success.getCause() instanceof CFException);
99 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
100 <        assertTrue(f.isDone());
101 <        assertFalse(f.isCancelled());
102 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
103 <    }
110 >        checker.accept(cause);
111  
112 <    void checkCompletedWithWrappedCFException(CompletableFuture<?> f,
106 <                                              CFException ex) {
112 >        long startTime = System.nanoTime();
113          try {
114              f.get(LONG_DELAY_MS, MILLISECONDS);
115              shouldThrow();
116          } catch (ExecutionException success) {
117 <            assertSame(ex, success.getCause());
117 >            assertSame(cause, success.getCause());
118          } catch (Throwable fail) { threadUnexpectedException(fail); }
119 +        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
120 +
121          try {
122              f.join();
123              shouldThrow();
124          } catch (CompletionException success) {
125 <            assertSame(ex, success.getCause());
126 <        }
125 >            assertSame(cause, success.getCause());
126 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
127 >
128          try {
129              f.getNow(null);
130              shouldThrow();
131          } catch (CompletionException success) {
132 <            assertSame(ex, success.getCause());
133 <        }
132 >            assertSame(cause, success.getCause());
133 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
134 >
135          try {
136              f.get();
137              shouldThrow();
138          } catch (ExecutionException success) {
139 <            assertSame(ex, success.getCause());
139 >            assertSame(cause, success.getCause());
140          } catch (Throwable fail) { threadUnexpectedException(fail); }
141 <        assertTrue(f.isDone());
141 >
142          assertFalse(f.isCancelled());
143 +        assertTrue(f.isDone());
144 +        assertTrue(f.isCompletedExceptionally());
145          assertTrue(f.toString().contains("[Completed exceptionally]"));
146      }
147  
148 +    void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
149 +        checkCompletedExceptionally(f, true,
150 +            (t) -> assertTrue(t instanceof CFException));
151 +    }
152 +
153 +    void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
154 +        checkCompletedExceptionally(f, true,
155 +            (t) -> assertTrue(t instanceof CancellationException));
156 +    }
157 +
158 +    void checkCompletedWithTimeoutException(CompletableFuture<?> f) {
159 +        checkCompletedExceptionally(f, false,
160 +            (t) -> assertTrue(t instanceof TimeoutException));
161 +    }
162 +
163 +    void checkCompletedWithWrappedException(CompletableFuture<?> f,
164 +                                            Throwable ex) {
165 +        checkCompletedExceptionally(f, true, (t) -> assertSame(t, ex));
166 +    }
167 +
168 +    void checkCompletedExceptionally(CompletableFuture<?> f, Throwable ex) {
169 +        checkCompletedExceptionally(f, false, (t) -> assertSame(t, ex));
170 +    }
171 +
172      void checkCancelled(CompletableFuture<?> f) {
173 +        long startTime = System.nanoTime();
174          try {
175              f.get(LONG_DELAY_MS, MILLISECONDS);
176              shouldThrow();
177          } catch (CancellationException success) {
178          } catch (Throwable fail) { threadUnexpectedException(fail); }
179 +        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
180 +
181          try {
182              f.join();
183              shouldThrow();
# Line 152 | Line 191 | public class CompletableFutureTest exten
191              shouldThrow();
192          } catch (CancellationException success) {
193          } catch (Throwable fail) { threadUnexpectedException(fail); }
155        assertTrue(f.isDone());
156        assertTrue(f.isCompletedExceptionally());
157        assertTrue(f.isCancelled());
158        assertTrue(f.toString().contains("[Completed exceptionally]"));
159    }
194  
195 <    void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
196 <        try {
163 <            f.get(LONG_DELAY_MS, MILLISECONDS);
164 <            shouldThrow();
165 <        } catch (ExecutionException success) {
166 <            assertTrue(success.getCause() instanceof CancellationException);
167 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
168 <        try {
169 <            f.join();
170 <            shouldThrow();
171 <        } catch (CompletionException success) {
172 <            assertTrue(success.getCause() instanceof CancellationException);
173 <        }
174 <        try {
175 <            f.getNow(null);
176 <            shouldThrow();
177 <        } catch (CompletionException success) {
178 <            assertTrue(success.getCause() instanceof CancellationException);
179 <        }
180 <        try {
181 <            f.get();
182 <            shouldThrow();
183 <        } catch (ExecutionException success) {
184 <            assertTrue(success.getCause() instanceof CancellationException);
185 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
195 >        assertTrue(exceptionalCompletion(f) instanceof CancellationException);
196 >
197          assertTrue(f.isDone());
187        assertFalse(f.isCancelled());
198          assertTrue(f.isCompletedExceptionally());
199 +        assertTrue(f.isCancelled());
200          assertTrue(f.toString().contains("[Completed exceptionally]"));
201      }
202  
# Line 203 | Line 214 | public class CompletableFutureTest exten
214       * isCancelled, join, get, and getNow
215       */
216      public void testComplete() {
217 +        for (Integer v1 : new Integer[] { 1, null })
218 +    {
219          CompletableFuture<Integer> f = new CompletableFuture<>();
220          checkIncomplete(f);
221 <        f.complete(one);
222 <        checkCompletedNormally(f, one);
223 <    }
221 >        assertTrue(f.complete(v1));
222 >        assertFalse(f.complete(v1));
223 >        checkCompletedNormally(f, v1);
224 >    }}
225  
226      /**
227       * completeExceptionally completes exceptionally, as indicated by
# Line 215 | Line 229 | public class CompletableFutureTest exten
229       */
230      public void testCompleteExceptionally() {
231          CompletableFuture<Integer> f = new CompletableFuture<>();
232 +        CFException ex = new CFException();
233          checkIncomplete(f);
234 <        f.completeExceptionally(new CFException());
235 <        checkCompletedWithWrappedCFException(f);
234 >        f.completeExceptionally(ex);
235 >        checkCompletedExceptionally(f, ex);
236      }
237  
238      /**
# Line 225 | Line 240 | public class CompletableFutureTest exten
240       * methods isDone, isCancelled, join, get, and getNow
241       */
242      public void testCancel() {
243 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
244 +    {
245          CompletableFuture<Integer> f = new CompletableFuture<>();
246          checkIncomplete(f);
247 <        assertTrue(f.cancel(true));
247 >        assertTrue(f.cancel(mayInterruptIfRunning));
248 >        assertTrue(f.cancel(mayInterruptIfRunning));
249 >        assertTrue(f.cancel(!mayInterruptIfRunning));
250          checkCancelled(f);
251 <    }
251 >    }}
252  
253      /**
254       * obtrudeValue forces completion with given value
# Line 237 | Line 256 | public class CompletableFutureTest exten
256      public void testObtrudeValue() {
257          CompletableFuture<Integer> f = new CompletableFuture<>();
258          checkIncomplete(f);
259 <        f.complete(one);
259 >        assertTrue(f.complete(one));
260          checkCompletedNormally(f, one);
261          f.obtrudeValue(three);
262          checkCompletedNormally(f, three);
# Line 246 | Line 265 | public class CompletableFutureTest exten
265          f = new CompletableFuture<>();
266          f.obtrudeValue(three);
267          checkCompletedNormally(f, three);
268 +        f.obtrudeValue(null);
269 +        checkCompletedNormally(f, null);
270          f = new CompletableFuture<>();
271          f.completeExceptionally(new CFException());
272          f.obtrudeValue(four);
# Line 256 | Line 277 | public class CompletableFutureTest exten
277       * obtrudeException forces completion with given exception
278       */
279      public void testObtrudeException() {
280 <        CompletableFuture<Integer> f = new CompletableFuture<>();
281 <        checkIncomplete(f);
282 <        f.complete(one);
283 <        checkCompletedNormally(f, one);
284 <        f.obtrudeException(new CFException());
264 <        checkCompletedWithWrappedCFException(f);
280 >        for (Integer v1 : new Integer[] { 1, null })
281 >    {
282 >        CFException ex;
283 >        CompletableFuture<Integer> f;
284 >
285          f = new CompletableFuture<>();
286 <        f.obtrudeException(new CFException());
287 <        checkCompletedWithWrappedCFException(f);
286 >        assertTrue(f.complete(v1));
287 >        for (int i = 0; i < 2; i++) {
288 >            f.obtrudeException(ex = new CFException());
289 >            checkCompletedExceptionally(f, ex);
290 >        }
291 >
292          f = new CompletableFuture<>();
293 +        for (int i = 0; i < 2; i++) {
294 +            f.obtrudeException(ex = new CFException());
295 +            checkCompletedExceptionally(f, ex);
296 +        }
297 +
298 +        f = new CompletableFuture<>();
299 +        f.completeExceptionally(ex = new CFException());
300 +        f.obtrudeValue(v1);
301 +        checkCompletedNormally(f, v1);
302 +        f.obtrudeException(ex = new CFException());
303 +        checkCompletedExceptionally(f, ex);
304          f.completeExceptionally(new CFException());
305 <        f.obtrudeValue(four);
306 <        checkCompletedNormally(f, four);
307 <        f.obtrudeException(new CFException());
308 <        checkCompletedWithWrappedCFException(f);
274 <    }
305 >        checkCompletedExceptionally(f, ex);
306 >        assertFalse(f.complete(v1));
307 >        checkCompletedExceptionally(f, ex);
308 >    }}
309  
310      /**
311       * getNumberOfDependents returns number of dependent tasks
312       */
313      public void testGetNumberOfDependents() {
314 +        for (ExecutionMode m : ExecutionMode.values())
315 +        for (Integer v1 : new Integer[] { 1, null })
316 +    {
317          CompletableFuture<Integer> f = new CompletableFuture<>();
318 <        assertEquals(f.getNumberOfDependents(), 0);
319 <        CompletableFuture g = f.thenRun(new Noop());
320 <        assertEquals(f.getNumberOfDependents(), 1);
321 <        assertEquals(g.getNumberOfDependents(), 0);
322 <        CompletableFuture h = f.thenRun(new Noop());
323 <        assertEquals(f.getNumberOfDependents(), 2);
324 <        f.complete(1);
318 >        assertEquals(0, f.getNumberOfDependents());
319 >        final CompletableFuture<Void> g = m.thenRun(f, new Noop(m));
320 >        assertEquals(1, f.getNumberOfDependents());
321 >        assertEquals(0, g.getNumberOfDependents());
322 >        final CompletableFuture<Void> h = m.thenRun(f, new Noop(m));
323 >        assertEquals(2, f.getNumberOfDependents());
324 >        assertEquals(0, h.getNumberOfDependents());
325 >        assertTrue(f.complete(v1));
326          checkCompletedNormally(g, null);
327 <        assertEquals(f.getNumberOfDependents(), 0);
328 <        assertEquals(g.getNumberOfDependents(), 0);
329 <    }
327 >        checkCompletedNormally(h, null);
328 >        assertEquals(0, f.getNumberOfDependents());
329 >        assertEquals(0, g.getNumberOfDependents());
330 >        assertEquals(0, h.getNumberOfDependents());
331 >    }}
332  
333      /**
334       * toString indicates current completion state
# Line 299 | Line 339 | public class CompletableFutureTest exten
339          f = new CompletableFuture<String>();
340          assertTrue(f.toString().contains("[Not completed]"));
341  
342 <        f.complete("foo");
342 >        assertTrue(f.complete("foo"));
343          assertTrue(f.toString().contains("[Completed normally]"));
344  
345          f = new CompletableFuture<String>();
346 <        f.completeExceptionally(new IndexOutOfBoundsException());
346 >        assertTrue(f.completeExceptionally(new IndexOutOfBoundsException()));
347          assertTrue(f.toString().contains("[Completed exceptionally]"));
348 +
349 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
350 +            f = new CompletableFuture<String>();
351 +            assertTrue(f.cancel(mayInterruptIfRunning));
352 +            assertTrue(f.toString().contains("[Completed exceptionally]"));
353 +        }
354      }
355  
356      /**
# Line 315 | Line 361 | public class CompletableFutureTest exten
361          checkCompletedNormally(f, "test");
362      }
363  
364 <    // Choose non-commutative actions for better coverage
365 <
366 <    static final Supplier<Integer> supplyOne =
367 <        () -> Integer.valueOf(1);
368 <    static final Function<Integer, Integer> inc =
369 <        (Integer x) -> Integer.valueOf(x.intValue() + 1);
370 <    static final BiFunction<Integer, Integer, Integer> subtract =
325 <        (Integer x, Integer y) -> Integer.valueOf(x.intValue() - y.intValue());
326 <    static final class IncAction implements Consumer<Integer> {
327 <        int value;
328 <        public void accept(Integer x) { value = x.intValue() + 1; }
329 <    }
330 <    static final class SubtractAction implements BiConsumer<Integer, Integer> {
331 <        int value;
332 <        public void accept(Integer x, Integer y) {
333 <            value = x.intValue() - y.intValue();
364 >    abstract class CheckedAction {
365 >        int invocationCount = 0;
366 >        final ExecutionMode m;
367 >        CheckedAction(ExecutionMode m) { this.m = m; }
368 >        void invoked() {
369 >            m.checkExecutionMode();
370 >            assertEquals(0, invocationCount++);
371          }
372 <    }
373 <    static final class Noop implements Runnable {
337 <        boolean ran;
338 <        public void run() { ran = true; }
372 >        void assertNotInvoked() { assertEquals(0, invocationCount); }
373 >        void assertInvoked() { assertEquals(1, invocationCount); }
374      }
375  
376 <    static final class FailingSupplier implements Supplier<Integer> {
377 <        boolean ran;
378 <        public Integer get() { ran = true; throw new CFException(); }
379 <    }
380 <    static final class FailingConsumer implements Consumer<Integer> {
381 <        boolean ran;
347 <        public void accept(Integer x) { ran = true; throw new CFException(); }
348 <    }
349 <    static final class FailingBiConsumer implements BiConsumer<Integer, Integer> {
350 <        boolean ran;
351 <        public void accept(Integer x, Integer y) { ran = true; throw new CFException(); }
352 <    }
353 <    static final class FailingFunction implements Function<Integer, Integer> {
354 <        boolean ran;
355 <        public Integer apply(Integer x) { ran = true; throw new CFException(); }
356 <    }
357 <    static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> {
358 <        boolean ran;
359 <        public Integer apply(Integer x, Integer y) { ran = true; throw new CFException(); }
360 <    }
361 <    static final class FailingNoop implements Runnable {
362 <        boolean ran;
363 <        public void run() { ran = true; throw new CFException(); }
364 <    }
365 <
366 <    static final class CompletableFutureInc
367 <        implements Function<Integer, CompletableFuture<Integer>> {
368 <        boolean ran;
369 <        public CompletableFuture<Integer> apply(Integer x) {
370 <            ran = true;
371 <            CompletableFuture<Integer> f = new CompletableFuture<>();
372 <            f.complete(Integer.valueOf(x.intValue() + 1));
373 <            return f;
376 >    abstract class CheckedIntegerAction extends CheckedAction {
377 >        Integer value;
378 >        CheckedIntegerAction(ExecutionMode m) { super(m); }
379 >        void assertValue(Integer expected) {
380 >            assertInvoked();
381 >            assertEquals(expected, value);
382          }
383      }
384  
385 <    static final class FailingCompletableFutureFunction
386 <        implements Function<Integer, CompletableFuture<Integer>> {
387 <        boolean ran;
388 <        public CompletableFuture<Integer> apply(Integer x) {
389 <            ran = true; throw new CFException();
385 >    class IntegerSupplier extends CheckedAction
386 >        implements Supplier<Integer>
387 >    {
388 >        final Integer value;
389 >        IntegerSupplier(ExecutionMode m, Integer value) {
390 >            super(m);
391 >            this.value = value;
392          }
393 <    }
394 <
395 <    // Used for explicit executor tests
386 <    static final class ThreadExecutor implements Executor {
387 <        AtomicInteger count = new AtomicInteger(0);
388 <
389 <        public void execute(Runnable r) {
390 <            count.getAndIncrement();
391 <            new Thread(r).start();
393 >        public Integer get() {
394 >            invoked();
395 >            return value;
396          }
397      }
398  
399 <    static final class ExceptionToInteger implements Function<Throwable, Integer> {
400 <        public Integer apply(Throwable x) { return Integer.valueOf(3); }
399 >    // A function that handles and produces null values as well.
400 >    static Integer inc(Integer x) {
401 >        return (x == null) ? null : x + 1;
402      }
403  
404 <    static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> {
405 <        boolean ran;
406 <        public Integer apply(Integer x, Throwable t) {
407 <            ran = true;
408 <            return (t == null) ? two : three;
404 >    class NoopConsumer extends CheckedIntegerAction
405 >        implements Consumer<Integer>
406 >    {
407 >        NoopConsumer(ExecutionMode m) { super(m); }
408 >        public void accept(Integer x) {
409 >            invoked();
410 >            value = x;
411          }
412      }
413  
414 <    /**
415 <     * exceptionally action completes with function value on source
416 <     * exception; otherwise with source value
417 <     */
418 <    public void testExceptionally() {
419 <        CompletableFuture<Integer> f = new CompletableFuture<>();
420 <        ExceptionToInteger r = new ExceptionToInteger();
421 <        CompletableFuture<Integer> g = f.exceptionally(r);
415 <        f.completeExceptionally(new CFException());
416 <        checkCompletedNormally(g, three);
417 <
418 <        f = new CompletableFuture<>();
419 <        r = new ExceptionToInteger();
420 <        g = f.exceptionally(r);
421 <        f.complete(one);
422 <        checkCompletedNormally(g, one);
423 <    }
424 <
425 <    /**
426 <     * handle action completes normally with function value on either
427 <     * normal or exceptional completion of source
428 <     */
429 <    public void testHandle() {
430 <        CompletableFuture<Integer> f, g;
431 <        IntegerHandler r;
432 <
433 <        f = new CompletableFuture<>();
434 <        f.completeExceptionally(new CFException());
435 <        g = f.handle(r = new IntegerHandler());
436 <        assertTrue(r.ran);
437 <        checkCompletedNormally(g, three);
438 <
439 <        f = new CompletableFuture<>();
440 <        g = f.handle(r = new IntegerHandler());
441 <        assertFalse(r.ran);
442 <        f.completeExceptionally(new CFException());
443 <        checkCompletedNormally(g, three);
444 <        assertTrue(r.ran);
445 <
446 <        f = new CompletableFuture<>();
447 <        f.complete(one);
448 <        g = f.handle(r = new IntegerHandler());
449 <        assertTrue(r.ran);
450 <        checkCompletedNormally(g, two);
451 <
452 <        f = new CompletableFuture<>();
453 <        g = f.handle(r = new IntegerHandler());
454 <        assertFalse(r.ran);
455 <        f.complete(one);
456 <        assertTrue(r.ran);
457 <        checkCompletedNormally(g, two);
458 <    }
459 <
460 <    /**
461 <     * runAsync completes after running Runnable
462 <     */
463 <    public void testRunAsync() {
464 <        Noop r = new Noop();
465 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
466 <        assertNull(f.join());
467 <        assertTrue(r.ran);
468 <        checkCompletedNormally(f, null);
469 <    }
470 <
471 <    /**
472 <     * runAsync with executor completes after running Runnable
473 <     */
474 <    public void testRunAsync2() {
475 <        Noop r = new Noop();
476 <        ThreadExecutor exec = new ThreadExecutor();
477 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec);
478 <        assertNull(f.join());
479 <        assertTrue(r.ran);
480 <        checkCompletedNormally(f, null);
481 <        assertEquals(1, exec.count.get());
482 <    }
483 <
484 <    /**
485 <     * failing runAsync completes exceptionally after running Runnable
486 <     */
487 <    public void testRunAsync3() {
488 <        FailingNoop r = new FailingNoop();
489 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
490 <        checkCompletedWithWrappedCFException(f);
491 <        assertTrue(r.ran);
492 <    }
493 <
494 <    /**
495 <     * supplyAsync completes with result of supplier
496 <     */
497 <    public void testSupplyAsync() {
498 <        CompletableFuture<Integer> f;
499 <        f = CompletableFuture.supplyAsync(supplyOne);
500 <        assertEquals(f.join(), one);
501 <        checkCompletedNormally(f, one);
502 <    }
503 <
504 <    /**
505 <     * supplyAsync with executor completes with result of supplier
506 <     */
507 <    public void testSupplyAsync2() {
508 <        CompletableFuture<Integer> f;
509 <        f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor());
510 <        assertEquals(f.join(), one);
511 <        checkCompletedNormally(f, one);
512 <    }
513 <
514 <    /**
515 <     * Failing supplyAsync completes exceptionally
516 <     */
517 <    public void testSupplyAsync3() {
518 <        FailingSupplier r = new FailingSupplier();
519 <        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r);
520 <        checkCompletedWithWrappedCFException(f);
521 <        assertTrue(r.ran);
522 <    }
523 <
524 <    // seq completion methods
525 <
526 <    /**
527 <     * thenRun result completes normally after normal completion of source
528 <     */
529 <    public void testThenRun() {
530 <        CompletableFuture<Integer> f;
531 <        CompletableFuture<Void> g;
532 <        Noop r;
533 <
534 <        f = new CompletableFuture<>();
535 <        g = f.thenRun(r = new Noop());
536 <        f.complete(null);
537 <        checkCompletedNormally(g, null);
538 <        assertTrue(r.ran);
539 <
540 <        f = new CompletableFuture<>();
541 <        f.complete(null);
542 <        g = f.thenRun(r = new Noop());
543 <        checkCompletedNormally(g, null);
544 <        assertTrue(r.ran);
545 <    }
546 <
547 <    /**
548 <     * thenRun result completes exceptionally after exceptional
549 <     * completion of source
550 <     */
551 <    public void testThenRun2() {
552 <        CompletableFuture<Integer> f;
553 <        CompletableFuture<Void> g;
554 <        Noop r;
555 <
556 <        f = new CompletableFuture<>();
557 <        g = f.thenRun(r = new Noop());
558 <        f.completeExceptionally(new CFException());
559 <        checkCompletedWithWrappedCFException(g);
560 <        assertFalse(r.ran);
561 <
562 <        f = new CompletableFuture<>();
563 <        f.completeExceptionally(new CFException());
564 <        g = f.thenRun(r = new Noop());
565 <        checkCompletedWithWrappedCFException(g);
566 <        assertFalse(r.ran);
567 <    }
568 <
569 <    /**
570 <     * thenRun result completes exceptionally if action does
571 <     */
572 <    public void testThenRun3() {
573 <        CompletableFuture<Integer> f;
574 <        CompletableFuture<Void> g;
575 <        FailingNoop r;
576 <
577 <        f = new CompletableFuture<>();
578 <        g = f.thenRun(r = new FailingNoop());
579 <        f.complete(null);
580 <        checkCompletedWithWrappedCFException(g);
581 <
582 <        f = new CompletableFuture<>();
583 <        f.complete(null);
584 <        g = f.thenRun(r = new FailingNoop());
585 <        checkCompletedWithWrappedCFException(g);
586 <    }
587 <
588 <    /**
589 <     * thenRun result completes exceptionally if source cancelled
590 <     */
591 <    public void testThenRun4() {
592 <        CompletableFuture<Integer> f;
593 <        CompletableFuture<Void> g;
594 <        Noop r;
595 <
596 <        f = new CompletableFuture<>();
597 <        g = f.thenRun(r = new Noop());
598 <        assertTrue(f.cancel(true));
599 <        checkCompletedWithWrappedCancellationException(g);
600 <
601 <        f = new CompletableFuture<>();
602 <        assertTrue(f.cancel(true));
603 <        g = f.thenRun(r = new Noop());
604 <        checkCompletedWithWrappedCancellationException(g);
605 <    }
606 <
607 <    /**
608 <     * thenApply result completes normally after normal completion of source
609 <     */
610 <    public void testThenApply() {
611 <        CompletableFuture<Integer> f = new CompletableFuture<>();
612 <        CompletableFuture<Integer> g = f.thenApply(inc);
613 <        f.complete(one);
614 <        checkCompletedNormally(g, two);
414 >    class IncFunction extends CheckedIntegerAction
415 >        implements Function<Integer,Integer>
416 >    {
417 >        IncFunction(ExecutionMode m) { super(m); }
418 >        public Integer apply(Integer x) {
419 >            invoked();
420 >            return value = inc(x);
421 >        }
422      }
423  
424 <    /**
425 <     * thenApply result completes exceptionally after exceptional
426 <     * completion of source
427 <     */
428 <    public void testThenApply2() {
429 <        CompletableFuture<Integer> f = new CompletableFuture<>();
623 <        CompletableFuture<Integer> g = f.thenApply(inc);
624 <        f.completeExceptionally(new CFException());
625 <        checkCompletedWithWrappedCFException(g);
424 >    // Choose non-commutative actions for better coverage
425 >    // A non-commutative function that handles and produces null values as well.
426 >    static Integer subtract(Integer x, Integer y) {
427 >        return (x == null && y == null) ? null :
428 >            ((x == null) ? 42 : x.intValue())
429 >            - ((y == null) ? 99 : y.intValue());
430      }
431  
432 <    /**
433 <     * thenApply result completes exceptionally if action does
434 <     */
435 <    public void testThenApply3() {
436 <        CompletableFuture<Integer> f = new CompletableFuture<>();
437 <        CompletableFuture<Integer> g = f.thenApply(new FailingFunction());
438 <        f.complete(one);
439 <        checkCompletedWithWrappedCFException(g);
432 >    class SubtractAction extends CheckedIntegerAction
433 >        implements BiConsumer<Integer, Integer>
434 >    {
435 >        SubtractAction(ExecutionMode m) { super(m); }
436 >        public void accept(Integer x, Integer y) {
437 >            invoked();
438 >            value = subtract(x, y);
439 >        }
440      }
441  
442 <    /**
443 <     * thenApply result completes exceptionally if source cancelled
444 <     */
445 <    public void testThenApply4() {
446 <        CompletableFuture<Integer> f = new CompletableFuture<>();
447 <        CompletableFuture<Integer> g = f.thenApply(inc);
448 <        assertTrue(f.cancel(true));
449 <        checkCompletedWithWrappedCancellationException(g);
442 >    class SubtractFunction extends CheckedIntegerAction
443 >        implements BiFunction<Integer, Integer, Integer>
444 >    {
445 >        SubtractFunction(ExecutionMode m) { super(m); }
446 >        public Integer apply(Integer x, Integer y) {
447 >            invoked();
448 >            return value = subtract(x, y);
449 >        }
450      }
451  
452 <    /**
453 <     * thenAccept result completes normally after normal completion of source
454 <     */
455 <    public void testThenAccept() {
456 <        CompletableFuture<Integer> f = new CompletableFuture<>();
653 <        IncAction r = new IncAction();
654 <        CompletableFuture<Void> g = f.thenAccept(r);
655 <        f.complete(one);
656 <        checkCompletedNormally(g, null);
657 <        assertEquals(r.value, 2);
452 >    class Noop extends CheckedAction implements Runnable {
453 >        Noop(ExecutionMode m) { super(m); }
454 >        public void run() {
455 >            invoked();
456 >        }
457      }
458  
459 <    /**
460 <     * thenAccept result completes exceptionally after exceptional
461 <     * completion of source
462 <     */
463 <    public void testThenAccept2() {
464 <        CompletableFuture<Integer> f = new CompletableFuture<>();
465 <        IncAction r = new IncAction();
466 <        CompletableFuture<Void> g = f.thenAccept(r);
668 <        f.completeExceptionally(new CFException());
669 <        checkCompletedWithWrappedCFException(g);
459 >    class FailingSupplier extends CheckedAction
460 >        implements Supplier<Integer>
461 >    {
462 >        FailingSupplier(ExecutionMode m) { super(m); }
463 >        public Integer get() {
464 >            invoked();
465 >            throw new CFException();
466 >        }
467      }
468  
469 <    /**
470 <     * thenAccept result completes exceptionally if action does
471 <     */
472 <    public void testThenAccept3() {
473 <        CompletableFuture<Integer> f = new CompletableFuture<>();
474 <        FailingConsumer r = new FailingConsumer();
475 <        CompletableFuture<Void> g = f.thenAccept(r);
476 <        f.complete(one);
477 <        checkCompletedWithWrappedCFException(g);
681 <        assertTrue(r.ran);
469 >    class FailingConsumer extends CheckedIntegerAction
470 >        implements Consumer<Integer>
471 >    {
472 >        FailingConsumer(ExecutionMode m) { super(m); }
473 >        public void accept(Integer x) {
474 >            invoked();
475 >            value = x;
476 >            throw new CFException();
477 >        }
478      }
479  
480 <    /**
481 <     * thenAccept result completes exceptionally if source cancelled
482 <     */
483 <    public void testThenAccept4() {
484 <        CompletableFuture<Integer> f = new CompletableFuture<>();
485 <        IncAction r = new IncAction();
486 <        CompletableFuture<Void> g = f.thenAccept(r);
487 <        assertTrue(f.cancel(true));
488 <        checkCompletedWithWrappedCancellationException(g);
480 >    class FailingBiConsumer extends CheckedIntegerAction
481 >        implements BiConsumer<Integer, Integer>
482 >    {
483 >        FailingBiConsumer(ExecutionMode m) { super(m); }
484 >        public void accept(Integer x, Integer y) {
485 >            invoked();
486 >            value = subtract(x, y);
487 >            throw new CFException();
488 >        }
489      }
490  
491 <    /**
492 <     * thenCombine result completes normally after normal completion
493 <     * of sources
494 <     */
495 <    public void testThenCombine() {
496 <        CompletableFuture<Integer> f, g, h;
497 <
498 <        f = new CompletableFuture<>();
499 <        g = new CompletableFuture<>();
704 <        h = f.thenCombine(g, subtract);
705 <        f.complete(3);
706 <        checkIncomplete(h);
707 <        g.complete(1);
708 <        checkCompletedNormally(h, 2);
709 <
710 <        f = new CompletableFuture<>();
711 <        g = new CompletableFuture<>();
712 <        h = f.thenCombine(g, subtract);
713 <        g.complete(1);
714 <        checkIncomplete(h);
715 <        f.complete(3);
716 <        checkCompletedNormally(h, 2);
717 <
718 <        f = new CompletableFuture<>();
719 <        g = new CompletableFuture<>();
720 <        g.complete(1);
721 <        f.complete(3);
722 <        h = f.thenCombine(g, subtract);
723 <        checkCompletedNormally(h, 2);
491 >    class FailingFunction extends CheckedIntegerAction
492 >        implements Function<Integer, Integer>
493 >    {
494 >        FailingFunction(ExecutionMode m) { super(m); }
495 >        public Integer apply(Integer x) {
496 >            invoked();
497 >            value = x;
498 >            throw new CFException();
499 >        }
500      }
501  
502 <    /**
503 <     * thenCombine result completes exceptionally after exceptional
504 <     * completion of either source
505 <     */
506 <    public void testThenCombine2() {
507 <        CompletableFuture<Integer> f, g, h;
508 <
509 <        f = new CompletableFuture<>();
510 <        g = new CompletableFuture<>();
735 <        h = f.thenCombine(g, subtract);
736 <        f.completeExceptionally(new CFException());
737 <        checkIncomplete(h);
738 <        g.complete(1);
739 <        checkCompletedWithWrappedCFException(h);
740 <
741 <        f = new CompletableFuture<>();
742 <        g = new CompletableFuture<>();
743 <        h = f.thenCombine(g, subtract);
744 <        g.completeExceptionally(new CFException());
745 <        checkIncomplete(h);
746 <        f.complete(3);
747 <        checkCompletedWithWrappedCFException(h);
748 <
749 <        f = new CompletableFuture<>();
750 <        g = new CompletableFuture<>();
751 <        f.complete(3);
752 <        g.completeExceptionally(new CFException());
753 <        h = f.thenCombine(g, subtract);
754 <        checkCompletedWithWrappedCFException(h);
755 <
756 <        f = new CompletableFuture<>();
757 <        g = new CompletableFuture<>();
758 <        f.completeExceptionally(new CFException());
759 <        g.complete(3);
760 <        h = f.thenCombine(g, subtract);
761 <        checkCompletedWithWrappedCFException(h);
502 >    class FailingBiFunction extends CheckedIntegerAction
503 >        implements BiFunction<Integer, Integer, Integer>
504 >    {
505 >        FailingBiFunction(ExecutionMode m) { super(m); }
506 >        public Integer apply(Integer x, Integer y) {
507 >            invoked();
508 >            value = subtract(x, y);
509 >            throw new CFException();
510 >        }
511      }
512  
513 <    /**
514 <     * thenCombine result completes exceptionally if action does
515 <     */
516 <    public void testThenCombine3() {
517 <        CompletableFuture<Integer> f = new CompletableFuture<>();
518 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
770 <        FailingBiFunction r = new FailingBiFunction();
771 <        CompletableFuture<Integer> g = f.thenCombine(f2, r);
772 <        f.complete(one);
773 <        checkIncomplete(g);
774 <        assertFalse(r.ran);
775 <        f2.complete(two);
776 <        checkCompletedWithWrappedCFException(g);
777 <        assertTrue(r.ran);
513 >    class FailingRunnable extends CheckedAction implements Runnable {
514 >        FailingRunnable(ExecutionMode m) { super(m); }
515 >        public void run() {
516 >            invoked();
517 >            throw new CFException();
518 >        }
519      }
520  
521 <    /**
522 <     * thenCombine result completes exceptionally if either source cancelled
523 <     */
524 <    public void testThenCombine4() {
525 <        CompletableFuture<Integer> f, g, h;
526 <
527 <        f = new CompletableFuture<>();
528 <        g = new CompletableFuture<>();
529 <        h = f.thenCombine(g, subtract);
530 <        assertTrue(f.cancel(true));
531 <        checkIncomplete(h);
791 <        g.complete(1);
792 <        checkCompletedWithWrappedCancellationException(h);
793 <
794 <        f = new CompletableFuture<>();
795 <        g = new CompletableFuture<>();
796 <        h = f.thenCombine(g, subtract);
797 <        assertTrue(g.cancel(true));
798 <        checkIncomplete(h);
799 <        f.complete(3);
800 <        checkCompletedWithWrappedCancellationException(h);
801 <
802 <        f = new CompletableFuture<>();
803 <        g = new CompletableFuture<>();
804 <        assertTrue(f.cancel(true));
805 <        assertTrue(g.cancel(true));
806 <        h = f.thenCombine(g, subtract);
807 <        checkCompletedWithWrappedCancellationException(h);
521 >    class CompletableFutureInc extends CheckedIntegerAction
522 >        implements Function<Integer, CompletableFuture<Integer>>
523 >    {
524 >        CompletableFutureInc(ExecutionMode m) { super(m); }
525 >        public CompletableFuture<Integer> apply(Integer x) {
526 >            invoked();
527 >            value = x;
528 >            CompletableFuture<Integer> f = new CompletableFuture<>();
529 >            assertTrue(f.complete(inc(x)));
530 >            return f;
531 >        }
532      }
533  
534 <    /**
535 <     * thenAcceptBoth result completes normally after normal
536 <     * completion of sources
537 <     */
538 <    public void testThenAcceptBoth() {
539 <        CompletableFuture<Integer> f, g;
540 <        CompletableFuture<Void> h;
541 <        SubtractAction r;
542 <
819 <        f = new CompletableFuture<>();
820 <        g = new CompletableFuture<>();
821 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
822 <        f.complete(3);
823 <        checkIncomplete(h);
824 <        g.complete(1);
825 <        checkCompletedNormally(h, null);
826 <        assertEquals(r.value, 2);
827 <
828 <        f = new CompletableFuture<>();
829 <        g = new CompletableFuture<>();
830 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
831 <        g.complete(1);
832 <        checkIncomplete(h);
833 <        f.complete(3);
834 <        checkCompletedNormally(h, null);
835 <        assertEquals(r.value, 2);
836 <
837 <        f = new CompletableFuture<>();
838 <        g = new CompletableFuture<>();
839 <        g.complete(1);
840 <        f.complete(3);
841 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
842 <        checkCompletedNormally(h, null);
843 <        assertEquals(r.value, 2);
534 >    class FailingCompletableFutureFunction extends CheckedIntegerAction
535 >        implements Function<Integer, CompletableFuture<Integer>>
536 >    {
537 >        FailingCompletableFutureFunction(ExecutionMode m) { super(m); }
538 >        public CompletableFuture<Integer> apply(Integer x) {
539 >            invoked();
540 >            value = x;
541 >            throw new CFException();
542 >        }
543      }
544  
545 <    /**
546 <     * thenAcceptBoth result completes exceptionally after exceptional
547 <     * completion of either source
548 <     */
549 <    public void testThenAcceptBoth2() {
550 <        CompletableFuture<Integer> f, g;
551 <        CompletableFuture<Void> h;
853 <        SubtractAction r;
854 <
855 <        f = new CompletableFuture<>();
856 <        g = new CompletableFuture<>();
857 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
858 <        f.completeExceptionally(new CFException());
859 <        checkIncomplete(h);
860 <        g.complete(1);
861 <        checkCompletedWithWrappedCFException(h);
862 <
863 <        f = new CompletableFuture<>();
864 <        g = new CompletableFuture<>();
865 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
866 <        g.completeExceptionally(new CFException());
867 <        checkIncomplete(h);
868 <        f.complete(3);
869 <        checkCompletedWithWrappedCFException(h);
870 <
871 <        f = new CompletableFuture<>();
872 <        g = new CompletableFuture<>();
873 <        f.complete(3);
874 <        g.completeExceptionally(new CFException());
875 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
876 <        checkCompletedWithWrappedCFException(h);
545 >    // Used for explicit executor tests
546 >    static final class ThreadExecutor implements Executor {
547 >        final AtomicInteger count = new AtomicInteger(0);
548 >        static final ThreadGroup tg = new ThreadGroup("ThreadExecutor");
549 >        static boolean startedCurrentThread() {
550 >            return Thread.currentThread().getThreadGroup() == tg;
551 >        }
552  
553 <        f = new CompletableFuture<>();
554 <        g = new CompletableFuture<>();
555 <        f.completeExceptionally(new CFException());
556 <        g.complete(3);
882 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
883 <        checkCompletedWithWrappedCFException(h);
553 >        public void execute(Runnable r) {
554 >            count.getAndIncrement();
555 >            new Thread(tg, r).start();
556 >        }
557      }
558  
559 <    /**
560 <     * thenAcceptBoth result completes exceptionally if action does
888 <     */
889 <    public void testThenAcceptBoth3() {
890 <        CompletableFuture<Integer> f, g;
891 <        CompletableFuture<Void> h;
892 <        FailingBiConsumer r;
893 <
894 <        f = new CompletableFuture<>();
895 <        g = new CompletableFuture<>();
896 <        h = f.thenAcceptBoth(g, r = new FailingBiConsumer());
897 <        f.complete(3);
898 <        checkIncomplete(h);
899 <        g.complete(1);
900 <        checkCompletedWithWrappedCFException(h);
901 <
902 <        f = new CompletableFuture<>();
903 <        g = new CompletableFuture<>();
904 <        f.complete(3);
905 <        g.complete(1);
906 <        h = f.thenAcceptBoth(g, r = new FailingBiConsumer());
907 <        checkCompletedWithWrappedCFException(h);
908 <    }
559 >    static final boolean defaultExecutorIsCommonPool
560 >        = ForkJoinPool.getCommonPoolParallelism() > 1;
561  
562      /**
563 <     * thenAcceptBoth result completes exceptionally if either source cancelled
563 >     * Permits the testing of parallel code for the 3 different
564 >     * execution modes without copy/pasting all the test methods.
565       */
566 <    public void testThenAcceptBoth4() {
567 <        CompletableFuture<Integer> f, g;
568 <        CompletableFuture<Void> h;
569 <        SubtractAction r;
570 <
571 <        f = new CompletableFuture<>();
572 <        g = new CompletableFuture<>();
573 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
574 <        assertTrue(f.cancel(true));
575 <        checkIncomplete(h);
576 <        g.complete(1);
577 <        checkCompletedWithWrappedCancellationException(h);
566 >    enum ExecutionMode {
567 >        SYNC {
568 >            public void checkExecutionMode() {
569 >                assertFalse(ThreadExecutor.startedCurrentThread());
570 >                assertNull(ForkJoinTask.getPool());
571 >            }
572 >            public CompletableFuture<Void> runAsync(Runnable a) {
573 >                throw new UnsupportedOperationException();
574 >            }
575 >            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
576 >                throw new UnsupportedOperationException();
577 >            }
578 >            public <T> CompletableFuture<Void> thenRun
579 >                (CompletableFuture<T> f, Runnable a) {
580 >                return f.thenRun(a);
581 >            }
582 >            public <T> CompletableFuture<Void> thenAccept
583 >                (CompletableFuture<T> f, Consumer<? super T> a) {
584 >                return f.thenAccept(a);
585 >            }
586 >            public <T,U> CompletableFuture<U> thenApply
587 >                (CompletableFuture<T> f, Function<? super T,U> a) {
588 >                return f.thenApply(a);
589 >            }
590 >            public <T,U> CompletableFuture<U> thenCompose
591 >                (CompletableFuture<T> f,
592 >                 Function<? super T,? extends CompletionStage<U>> a) {
593 >                return f.thenCompose(a);
594 >            }
595 >            public <T,U> CompletableFuture<U> handle
596 >                (CompletableFuture<T> f,
597 >                 BiFunction<? super T,Throwable,? extends U> a) {
598 >                return f.handle(a);
599 >            }
600 >            public <T> CompletableFuture<T> whenComplete
601 >                (CompletableFuture<T> f,
602 >                 BiConsumer<? super T,? super Throwable> a) {
603 >                return f.whenComplete(a);
604 >            }
605 >            public <T,U> CompletableFuture<Void> runAfterBoth
606 >                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
607 >                return f.runAfterBoth(g, a);
608 >            }
609 >            public <T,U> CompletableFuture<Void> thenAcceptBoth
610 >                (CompletableFuture<T> f,
611 >                 CompletionStage<? extends U> g,
612 >                 BiConsumer<? super T,? super U> a) {
613 >                return f.thenAcceptBoth(g, a);
614 >            }
615 >            public <T,U,V> CompletableFuture<V> thenCombine
616 >                (CompletableFuture<T> f,
617 >                 CompletionStage<? extends U> g,
618 >                 BiFunction<? super T,? super U,? extends V> a) {
619 >                return f.thenCombine(g, a);
620 >            }
621 >            public <T> CompletableFuture<Void> runAfterEither
622 >                (CompletableFuture<T> f,
623 >                 CompletionStage<?> g,
624 >                 java.lang.Runnable a) {
625 >                return f.runAfterEither(g, a);
626 >            }
627 >            public <T> CompletableFuture<Void> acceptEither
628 >                (CompletableFuture<T> f,
629 >                 CompletionStage<? extends T> g,
630 >                 Consumer<? super T> a) {
631 >                return f.acceptEither(g, a);
632 >            }
633 >            public <T,U> CompletableFuture<U> applyToEither
634 >                (CompletableFuture<T> f,
635 >                 CompletionStage<? extends T> g,
636 >                 Function<? super T,U> a) {
637 >                return f.applyToEither(g, a);
638 >            }
639 >        },
640  
641 <        f = new CompletableFuture<>();
642 <        g = new CompletableFuture<>();
643 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
644 <        assertTrue(g.cancel(true));
645 <        checkIncomplete(h);
646 <        f.complete(3);
647 <        checkCompletedWithWrappedCancellationException(h);
641 >        ASYNC {
642 >            public void checkExecutionMode() {
643 >                assertEquals(defaultExecutorIsCommonPool,
644 >                             (ForkJoinPool.commonPool() == ForkJoinTask.getPool()));
645 >            }
646 >            public CompletableFuture<Void> runAsync(Runnable a) {
647 >                return CompletableFuture.runAsync(a);
648 >            }
649 >            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
650 >                return CompletableFuture.supplyAsync(a);
651 >            }
652 >            public <T> CompletableFuture<Void> thenRun
653 >                (CompletableFuture<T> f, Runnable a) {
654 >                return f.thenRunAsync(a);
655 >            }
656 >            public <T> CompletableFuture<Void> thenAccept
657 >                (CompletableFuture<T> f, Consumer<? super T> a) {
658 >                return f.thenAcceptAsync(a);
659 >            }
660 >            public <T,U> CompletableFuture<U> thenApply
661 >                (CompletableFuture<T> f, Function<? super T,U> a) {
662 >                return f.thenApplyAsync(a);
663 >            }
664 >            public <T,U> CompletableFuture<U> thenCompose
665 >                (CompletableFuture<T> f,
666 >                 Function<? super T,? extends CompletionStage<U>> a) {
667 >                return f.thenComposeAsync(a);
668 >            }
669 >            public <T,U> CompletableFuture<U> handle
670 >                (CompletableFuture<T> f,
671 >                 BiFunction<? super T,Throwable,? extends U> a) {
672 >                return f.handleAsync(a);
673 >            }
674 >            public <T> CompletableFuture<T> whenComplete
675 >                (CompletableFuture<T> f,
676 >                 BiConsumer<? super T,? super Throwable> a) {
677 >                return f.whenCompleteAsync(a);
678 >            }
679 >            public <T,U> CompletableFuture<Void> runAfterBoth
680 >                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
681 >                return f.runAfterBothAsync(g, a);
682 >            }
683 >            public <T,U> CompletableFuture<Void> thenAcceptBoth
684 >                (CompletableFuture<T> f,
685 >                 CompletionStage<? extends U> g,
686 >                 BiConsumer<? super T,? super U> a) {
687 >                return f.thenAcceptBothAsync(g, a);
688 >            }
689 >            public <T,U,V> CompletableFuture<V> thenCombine
690 >                (CompletableFuture<T> f,
691 >                 CompletionStage<? extends U> g,
692 >                 BiFunction<? super T,? super U,? extends V> a) {
693 >                return f.thenCombineAsync(g, a);
694 >            }
695 >            public <T> CompletableFuture<Void> runAfterEither
696 >                (CompletableFuture<T> f,
697 >                 CompletionStage<?> g,
698 >                 java.lang.Runnable a) {
699 >                return f.runAfterEitherAsync(g, a);
700 >            }
701 >            public <T> CompletableFuture<Void> acceptEither
702 >                (CompletableFuture<T> f,
703 >                 CompletionStage<? extends T> g,
704 >                 Consumer<? super T> a) {
705 >                return f.acceptEitherAsync(g, a);
706 >            }
707 >            public <T,U> CompletableFuture<U> applyToEither
708 >                (CompletableFuture<T> f,
709 >                 CompletionStage<? extends T> g,
710 >                 Function<? super T,U> a) {
711 >                return f.applyToEitherAsync(g, a);
712 >            }
713 >        },
714  
715 <        f = new CompletableFuture<>();
716 <        g = new CompletableFuture<>();
717 <        f.complete(3);
718 <        assertTrue(g.cancel(true));
719 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
720 <        checkCompletedWithWrappedCancellationException(h);
715 >        EXECUTOR {
716 >            public void checkExecutionMode() {
717 >                assertTrue(ThreadExecutor.startedCurrentThread());
718 >            }
719 >            public CompletableFuture<Void> runAsync(Runnable a) {
720 >                return CompletableFuture.runAsync(a, new ThreadExecutor());
721 >            }
722 >            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
723 >                return CompletableFuture.supplyAsync(a, new ThreadExecutor());
724 >            }
725 >            public <T> CompletableFuture<Void> thenRun
726 >                (CompletableFuture<T> f, Runnable a) {
727 >                return f.thenRunAsync(a, new ThreadExecutor());
728 >            }
729 >            public <T> CompletableFuture<Void> thenAccept
730 >                (CompletableFuture<T> f, Consumer<? super T> a) {
731 >                return f.thenAcceptAsync(a, new ThreadExecutor());
732 >            }
733 >            public <T,U> CompletableFuture<U> thenApply
734 >                (CompletableFuture<T> f, Function<? super T,U> a) {
735 >                return f.thenApplyAsync(a, new ThreadExecutor());
736 >            }
737 >            public <T,U> CompletableFuture<U> thenCompose
738 >                (CompletableFuture<T> f,
739 >                 Function<? super T,? extends CompletionStage<U>> a) {
740 >                return f.thenComposeAsync(a, new ThreadExecutor());
741 >            }
742 >            public <T,U> CompletableFuture<U> handle
743 >                (CompletableFuture<T> f,
744 >                 BiFunction<? super T,Throwable,? extends U> a) {
745 >                return f.handleAsync(a, new ThreadExecutor());
746 >            }
747 >            public <T> CompletableFuture<T> whenComplete
748 >                (CompletableFuture<T> f,
749 >                 BiConsumer<? super T,? super Throwable> a) {
750 >                return f.whenCompleteAsync(a, new ThreadExecutor());
751 >            }
752 >            public <T,U> CompletableFuture<Void> runAfterBoth
753 >                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
754 >                return f.runAfterBothAsync(g, a, new ThreadExecutor());
755 >            }
756 >            public <T,U> CompletableFuture<Void> thenAcceptBoth
757 >                (CompletableFuture<T> f,
758 >                 CompletionStage<? extends U> g,
759 >                 BiConsumer<? super T,? super U> a) {
760 >                return f.thenAcceptBothAsync(g, a, new ThreadExecutor());
761 >            }
762 >            public <T,U,V> CompletableFuture<V> thenCombine
763 >                (CompletableFuture<T> f,
764 >                 CompletionStage<? extends U> g,
765 >                 BiFunction<? super T,? super U,? extends V> a) {
766 >                return f.thenCombineAsync(g, a, new ThreadExecutor());
767 >            }
768 >            public <T> CompletableFuture<Void> runAfterEither
769 >                (CompletableFuture<T> f,
770 >                 CompletionStage<?> g,
771 >                 java.lang.Runnable a) {
772 >                return f.runAfterEitherAsync(g, a, new ThreadExecutor());
773 >            }
774 >            public <T> CompletableFuture<Void> acceptEither
775 >                (CompletableFuture<T> f,
776 >                 CompletionStage<? extends T> g,
777 >                 Consumer<? super T> a) {
778 >                return f.acceptEitherAsync(g, a, new ThreadExecutor());
779 >            }
780 >            public <T,U> CompletableFuture<U> applyToEither
781 >                (CompletableFuture<T> f,
782 >                 CompletionStage<? extends T> g,
783 >                 Function<? super T,U> a) {
784 >                return f.applyToEitherAsync(g, a, new ThreadExecutor());
785 >            }
786 >        };
787  
788 <        f = new CompletableFuture<>();
789 <        g = new CompletableFuture<>();
790 <        assertTrue(f.cancel(true));
791 <        g.complete(3);
792 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
793 <        checkCompletedWithWrappedCancellationException(h);
788 >        public abstract void checkExecutionMode();
789 >        public abstract CompletableFuture<Void> runAsync(Runnable a);
790 >        public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a);
791 >        public abstract <T> CompletableFuture<Void> thenRun
792 >            (CompletableFuture<T> f, Runnable a);
793 >        public abstract <T> CompletableFuture<Void> thenAccept
794 >            (CompletableFuture<T> f, Consumer<? super T> a);
795 >        public abstract <T,U> CompletableFuture<U> thenApply
796 >            (CompletableFuture<T> f, Function<? super T,U> a);
797 >        public abstract <T,U> CompletableFuture<U> thenCompose
798 >            (CompletableFuture<T> f,
799 >             Function<? super T,? extends CompletionStage<U>> a);
800 >        public abstract <T,U> CompletableFuture<U> handle
801 >            (CompletableFuture<T> f,
802 >             BiFunction<? super T,Throwable,? extends U> a);
803 >        public abstract <T> CompletableFuture<T> whenComplete
804 >            (CompletableFuture<T> f,
805 >             BiConsumer<? super T,? super Throwable> a);
806 >        public abstract <T,U> CompletableFuture<Void> runAfterBoth
807 >            (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a);
808 >        public abstract <T,U> CompletableFuture<Void> thenAcceptBoth
809 >            (CompletableFuture<T> f,
810 >             CompletionStage<? extends U> g,
811 >             BiConsumer<? super T,? super U> a);
812 >        public abstract <T,U,V> CompletableFuture<V> thenCombine
813 >            (CompletableFuture<T> f,
814 >             CompletionStage<? extends U> g,
815 >             BiFunction<? super T,? super U,? extends V> a);
816 >        public abstract <T> CompletableFuture<Void> runAfterEither
817 >            (CompletableFuture<T> f,
818 >             CompletionStage<?> g,
819 >             java.lang.Runnable a);
820 >        public abstract <T> CompletableFuture<Void> acceptEither
821 >            (CompletableFuture<T> f,
822 >             CompletionStage<? extends T> g,
823 >             Consumer<? super T> a);
824 >        public abstract <T,U> CompletableFuture<U> applyToEither
825 >            (CompletableFuture<T> f,
826 >             CompletionStage<? extends T> g,
827 >             Function<? super T,U> a);
828      }
829  
830      /**
831 <     * runAfterBoth result completes normally after normal
832 <     * completion of sources
831 >     * exceptionally action is not invoked when source completes
832 >     * normally, and source result is propagated
833       */
834 <    public void testRunAfterBoth_normalCompletion1() {
835 <        for (Integer v1 : new Integer[] { 1, null })
955 <        for (Integer v2 : new Integer[] { 2, null }) {
956 <
957 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
958 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
959 <        final Noop r = new Noop();
960 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
961 <
962 <        f.complete(v1);
963 <        checkIncomplete(h);
964 <        assertFalse(r.ran);
965 <        g.complete(v2);
966 <
967 <        checkCompletedNormally(h, null);
968 <        assertTrue(r.ran);
969 <        checkCompletedNormally(f, v1);
970 <        checkCompletedNormally(g, v2);
971 <        }
972 <    }
973 <
974 <    public void testRunAfterBoth_normalCompletion2() {
834 >    public void testExceptionally_normalCompletion() {
835 >        for (boolean createIncomplete : new boolean[] { true, false })
836          for (Integer v1 : new Integer[] { 1, null })
837 <        for (Integer v2 : new Integer[] { 2, null }) {
838 <
837 >    {
838 >        final AtomicInteger a = new AtomicInteger(0);
839          final CompletableFuture<Integer> f = new CompletableFuture<>();
840 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
841 <        final Noop r = new Noop();
842 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
843 <
844 <        g.complete(v2);
845 <        checkIncomplete(h);
846 <        assertFalse(r.ran);
847 <        f.complete(v1);
840 >        if (!createIncomplete) assertTrue(f.complete(v1));
841 >        final CompletableFuture<Integer> g = f.exceptionally
842 >            ((Throwable t) -> {
843 >                a.getAndIncrement();
844 >                threadFail("should not be called");
845 >                return null;            // unreached
846 >            });
847 >        if (createIncomplete) assertTrue(f.complete(v1));
848  
849 <        checkCompletedNormally(h, null);
989 <        assertTrue(r.ran);
849 >        checkCompletedNormally(g, v1);
850          checkCompletedNormally(f, v1);
851 <        checkCompletedNormally(g, v2);
852 <        }
993 <    }
851 >        assertEquals(0, a.get());
852 >    }}
853  
854 <    public void testRunAfterBoth_normalCompletion3() {
854 >    /**
855 >     * exceptionally action completes with function value on source
856 >     * exception
857 >     */
858 >    public void testExceptionally_exceptionalCompletion() {
859 >        for (boolean createIncomplete : new boolean[] { true, false })
860          for (Integer v1 : new Integer[] { 1, null })
861 <        for (Integer v2 : new Integer[] { 2, null }) {
862 <
861 >    {
862 >        final AtomicInteger a = new AtomicInteger(0);
863 >        final CFException ex = new CFException();
864          final CompletableFuture<Integer> f = new CompletableFuture<>();
865 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
866 <        final Noop r = new Noop();
867 <
868 <        g.complete(v2);
869 <        f.complete(v1);
870 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
871 <
872 <        checkCompletedNormally(h, null);
873 <        assertTrue(r.ran);
1009 <        checkCompletedNormally(f, v1);
1010 <        checkCompletedNormally(g, v2);
1011 <        }
1012 <    }
865 >        if (!createIncomplete) f.completeExceptionally(ex);
866 >        final CompletableFuture<Integer> g = f.exceptionally
867 >            ((Throwable t) -> {
868 >                ExecutionMode.SYNC.checkExecutionMode();
869 >                threadAssertSame(t, ex);
870 >                a.getAndIncrement();
871 >                return v1;
872 >            });
873 >        if (createIncomplete) f.completeExceptionally(ex);
874  
875 <    public void testRunAfterBoth_normalCompletion4() {
876 <        for (Integer v1 : new Integer[] { 1, null })
877 <        for (Integer v2 : new Integer[] { 2, null }) {
875 >        checkCompletedNormally(g, v1);
876 >        assertEquals(1, a.get());
877 >    }}
878  
879 +    public void testExceptionally_exceptionalCompletionActionFailed() {
880 +        for (boolean createIncomplete : new boolean[] { true, false })
881 +    {
882 +        final AtomicInteger a = new AtomicInteger(0);
883 +        final CFException ex1 = new CFException();
884 +        final CFException ex2 = new CFException();
885          final CompletableFuture<Integer> f = new CompletableFuture<>();
886 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
887 <        final Noop r = new Noop();
888 <
889 <        f.complete(v1);
890 <        g.complete(v2);
891 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
892 <
893 <        checkCompletedNormally(h, null);
894 <        assertTrue(r.ran);
895 <        checkCompletedNormally(f, v1);
896 <        checkCompletedNormally(g, v2);
897 <        }
898 <    }
886 >        if (!createIncomplete) f.completeExceptionally(ex1);
887 >        final CompletableFuture<Integer> g = f.exceptionally
888 >            ((Throwable t) -> {
889 >                ExecutionMode.SYNC.checkExecutionMode();
890 >                threadAssertSame(t, ex1);
891 >                a.getAndIncrement();
892 >                throw ex2;
893 >            });
894 >        if (createIncomplete) f.completeExceptionally(ex1);
895 >
896 >        checkCompletedWithWrappedException(g, ex2);
897 >        assertEquals(1, a.get());
898 >    }}
899  
900      /**
901 <     * runAfterBoth result completes exceptionally after exceptional
902 <     * completion of either source
901 >     * whenComplete action executes on normal completion, propagating
902 >     * source result.
903       */
904 <    public void testRunAfterBoth_exceptionalCompletion1() {
905 <        for (Integer v1 : new Integer[] { 1, null }) {
906 <
904 >    public void testWhenComplete_normalCompletion() {
905 >        for (ExecutionMode m : ExecutionMode.values())
906 >        for (boolean createIncomplete : new boolean[] { true, false })
907 >        for (Integer v1 : new Integer[] { 1, null })
908 >    {
909 >        final AtomicInteger a = new AtomicInteger(0);
910          final CompletableFuture<Integer> f = new CompletableFuture<>();
911 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
912 <        final Noop r = new Noop();
913 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
914 <        final CFException ex = new CFException();
911 >        if (!createIncomplete) assertTrue(f.complete(v1));
912 >        final CompletableFuture<Integer> g = m.whenComplete
913 >            (f,
914 >             (Integer x, Throwable t) -> {
915 >                m.checkExecutionMode();
916 >                threadAssertSame(x, v1);
917 >                threadAssertNull(t);
918 >                a.getAndIncrement();
919 >            });
920 >        if (createIncomplete) assertTrue(f.complete(v1));
921  
1046        f.completeExceptionally(ex);
1047        checkIncomplete(h);
1048        g.complete(v1);
1049
1050        checkCompletedWithWrappedCFException(h, ex);
1051        checkCompletedWithWrappedCFException(f, ex);
1052        assertFalse(r.ran);
922          checkCompletedNormally(g, v1);
1054        }
1055    }
1056
1057    public void testRunAfterBoth_exceptionalCompletion2() {
1058        for (Integer v1 : new Integer[] { 1, null }) {
1059
1060        final CompletableFuture<Integer> f = new CompletableFuture<>();
1061        final CompletableFuture<Integer> g = new CompletableFuture<>();
1062        final Noop r = new Noop();
1063        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1064        final CFException ex = new CFException();
1065
1066        g.completeExceptionally(ex);
1067        checkIncomplete(h);
1068        f.complete(v1);
1069
1070        checkCompletedWithWrappedCFException(h, ex);
1071        checkCompletedWithWrappedCFException(g, ex);
1072        assertFalse(r.ran);
923          checkCompletedNormally(f, v1);
924 <        }
925 <    }
1076 <
1077 <    public void testRunAfterBoth_exceptionalCompletion3() {
1078 <        for (Integer v1 : new Integer[] { 1, null }) {
924 >        assertEquals(1, a.get());
925 >    }}
926  
927 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
928 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
929 <        final Noop r = new Noop();
927 >    /**
928 >     * whenComplete action executes on exceptional completion, propagating
929 >     * source result.
930 >     */
931 >    public void testWhenComplete_exceptionalCompletion() {
932 >        for (ExecutionMode m : ExecutionMode.values())
933 >        for (boolean createIncomplete : new boolean[] { true, false })
934 >    {
935 >        final AtomicInteger a = new AtomicInteger(0);
936          final CFException ex = new CFException();
1084
1085        g.completeExceptionally(ex);
1086        f.complete(v1);
1087        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1088
1089        checkCompletedWithWrappedCFException(h, ex);
1090        checkCompletedWithWrappedCFException(g, ex);
1091        assertFalse(r.ran);
1092        checkCompletedNormally(f, v1);
1093        }
1094    }
1095
1096    public void testRunAfterBoth_exceptionalCompletion4() {
1097        for (Integer v1 : new Integer[] { 1, null }) {
1098
937          final CompletableFuture<Integer> f = new CompletableFuture<>();
938 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
939 <        final Noop r = new Noop();
940 <        final CFException ex = new CFException();
941 <
942 <        f.completeExceptionally(ex);
943 <        g.complete(v1);
944 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
945 <
946 <        checkCompletedWithWrappedCFException(h, ex);
947 <        checkCompletedWithWrappedCFException(f, ex);
948 <        assertFalse(r.ran);
949 <        checkCompletedNormally(g, v1);
950 <        }
951 <    }
938 >        if (!createIncomplete) f.completeExceptionally(ex);
939 >        final CompletableFuture<Integer> g = m.whenComplete
940 >            (f,
941 >             (Integer x, Throwable t) -> {
942 >                m.checkExecutionMode();
943 >                threadAssertNull(x);
944 >                threadAssertSame(t, ex);
945 >                a.getAndIncrement();
946 >            });
947 >        if (createIncomplete) f.completeExceptionally(ex);
948 >
949 >        checkCompletedWithWrappedException(g, ex);
950 >        checkCompletedExceptionally(f, ex);
951 >        assertEquals(1, a.get());
952 >    }}
953  
954      /**
955 <     * runAfterBoth result completes exceptionally if action does
955 >     * whenComplete action executes on cancelled source, propagating
956 >     * CancellationException.
957       */
958 <    public void testRunAfterBoth_actionFailed1() {
959 <        for (Integer v1 : new Integer[] { 1, null })
960 <        for (Integer v2 : new Integer[] { 2, null }) {
961 <
958 >    public void testWhenComplete_sourceCancelled() {
959 >        for (ExecutionMode m : ExecutionMode.values())
960 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
961 >        for (boolean createIncomplete : new boolean[] { true, false })
962 >    {
963 >        final AtomicInteger a = new AtomicInteger(0);
964          final CompletableFuture<Integer> f = new CompletableFuture<>();
965 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
966 <        final FailingNoop r = new FailingNoop();
967 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
965 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
966 >        final CompletableFuture<Integer> g = m.whenComplete
967 >            (f,
968 >             (Integer x, Throwable t) -> {
969 >                m.checkExecutionMode();
970 >                threadAssertNull(x);
971 >                threadAssertTrue(t instanceof CancellationException);
972 >                a.getAndIncrement();
973 >            });
974 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
975  
976 <        f.complete(v1);
977 <        checkIncomplete(h);
978 <        g.complete(v2);
979 <
1131 <        checkCompletedWithWrappedCFException(h);
1132 <        checkCompletedNormally(f, v1);
1133 <        checkCompletedNormally(g, v2);
1134 <        }
1135 <    }
976 >        checkCompletedWithWrappedCancellationException(g);
977 >        checkCancelled(f);
978 >        assertEquals(1, a.get());
979 >    }}
980  
981 <    public void testRunAfterBoth_actionFailed2() {
981 >    /**
982 >     * If a whenComplete action throws an exception when triggered by
983 >     * a normal completion, it completes exceptionally
984 >     */
985 >    public void testWhenComplete_sourceCompletedNormallyActionFailed() {
986 >        for (boolean createIncomplete : new boolean[] { true, false })
987 >        for (ExecutionMode m : ExecutionMode.values())
988          for (Integer v1 : new Integer[] { 1, null })
989 <        for (Integer v2 : new Integer[] { 2, null }) {
990 <
989 >    {
990 >        final AtomicInteger a = new AtomicInteger(0);
991 >        final CFException ex = new CFException();
992          final CompletableFuture<Integer> f = new CompletableFuture<>();
993 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
994 <        final FailingNoop r = new FailingNoop();
995 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
996 <
997 <        g.complete(v2);
998 <        checkIncomplete(h);
999 <        f.complete(v1);
993 >        if (!createIncomplete) assertTrue(f.complete(v1));
994 >        final CompletableFuture<Integer> g = m.whenComplete
995 >            (f,
996 >             (Integer x, Throwable t) -> {
997 >                m.checkExecutionMode();
998 >                threadAssertSame(x, v1);
999 >                threadAssertNull(t);
1000 >                a.getAndIncrement();
1001 >                throw ex;
1002 >            });
1003 >        if (createIncomplete) assertTrue(f.complete(v1));
1004  
1005 <        checkCompletedWithWrappedCFException(h);
1005 >        checkCompletedWithWrappedException(g, ex);
1006          checkCompletedNormally(f, v1);
1007 <        checkCompletedNormally(g, v2);
1008 <        }
1154 <    }
1007 >        assertEquals(1, a.get());
1008 >    }}
1009  
1010      /**
1011 <     * runAfterBoth result completes exceptionally if either source cancelled
1011 >     * If a whenComplete action throws an exception when triggered by
1012 >     * a source completion that also throws an exception, the source
1013 >     * exception takes precedence.
1014       */
1015 <    public void testRunAfterBoth_sourceCancelled1() {
1016 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1017 <        for (Integer v1 : new Integer[] { 1, null }) {
1018 <
1019 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1020 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1021 <        final Noop r = new Noop();
1166 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1167 <
1168 <        assertTrue(f.cancel(mayInterruptIfRunning));
1169 <        checkIncomplete(h);
1170 <        g.complete(v1);
1171 <
1172 <        checkCompletedWithWrappedCancellationException(h);
1173 <        checkCancelled(f);
1174 <        assertFalse(r.ran);
1175 <        checkCompletedNormally(g, v1);
1176 <        }
1177 <    }
1178 <
1179 <    public void testRunAfterBoth_sourceCancelled2() {
1180 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1181 <        for (Integer v1 : new Integer[] { 1, null }) {
1182 <
1015 >    public void testWhenComplete_actionFailedSourceFailed() {
1016 >        for (boolean createIncomplete : new boolean[] { true, false })
1017 >        for (ExecutionMode m : ExecutionMode.values())
1018 >    {
1019 >        final AtomicInteger a = new AtomicInteger(0);
1020 >        final CFException ex1 = new CFException();
1021 >        final CFException ex2 = new CFException();
1022          final CompletableFuture<Integer> f = new CompletableFuture<>();
1184        final CompletableFuture<Integer> g = new CompletableFuture<>();
1185        final Noop r = new Noop();
1186        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1187
1188        assertTrue(g.cancel(mayInterruptIfRunning));
1189        checkIncomplete(h);
1190        f.complete(v1);
1191
1192        checkCompletedWithWrappedCancellationException(h);
1193        checkCancelled(g);
1194        assertFalse(r.ran);
1195        checkCompletedNormally(f, v1);
1196        }
1197    }
1198
1199    public void testRunAfterBoth_sourceCancelled3() {
1200        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1201        for (Integer v1 : new Integer[] { 1, null }) {
1023  
1024 +        if (!createIncomplete) f.completeExceptionally(ex1);
1025 +        final CompletableFuture<Integer> g = m.whenComplete
1026 +            (f,
1027 +             (Integer x, Throwable t) -> {
1028 +                m.checkExecutionMode();
1029 +                threadAssertSame(t, ex1);
1030 +                threadAssertNull(x);
1031 +                a.getAndIncrement();
1032 +                throw ex2;
1033 +            });
1034 +        if (createIncomplete) f.completeExceptionally(ex1);
1035 +
1036 +        checkCompletedWithWrappedException(g, ex1);
1037 +        checkCompletedExceptionally(f, ex1);
1038 +        assertEquals(1, a.get());
1039 +    }}
1040 +
1041 +    /**
1042 +     * handle action completes normally with function value on normal
1043 +     * completion of source
1044 +     */
1045 +    public void testHandle_normalCompletion() {
1046 +        for (ExecutionMode m : ExecutionMode.values())
1047 +        for (boolean createIncomplete : new boolean[] { true, false })
1048 +        for (Integer v1 : new Integer[] { 1, null })
1049 +    {
1050          final CompletableFuture<Integer> f = new CompletableFuture<>();
1051 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1052 <        final Noop r = new Noop();
1053 <
1054 <        assertTrue(g.cancel(mayInterruptIfRunning));
1055 <        f.complete(v1);
1056 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1051 >        final AtomicInteger a = new AtomicInteger(0);
1052 >        if (!createIncomplete) assertTrue(f.complete(v1));
1053 >        final CompletableFuture<Integer> g = m.handle
1054 >            (f,
1055 >             (Integer x, Throwable t) -> {
1056 >                m.checkExecutionMode();
1057 >                threadAssertSame(x, v1);
1058 >                threadAssertNull(t);
1059 >                a.getAndIncrement();
1060 >                return inc(v1);
1061 >            });
1062 >        if (createIncomplete) assertTrue(f.complete(v1));
1063  
1064 <        checkCompletedWithWrappedCancellationException(h);
1212 <        checkCancelled(g);
1213 <        assertFalse(r.ran);
1064 >        checkCompletedNormally(g, inc(v1));
1065          checkCompletedNormally(f, v1);
1066 <        }
1067 <    }
1217 <
1218 <    public void testRunAfterBoth_sourceCancelled4() {
1219 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1220 <        for (Integer v1 : new Integer[] { 1, null }) {
1221 <
1222 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1223 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1224 <        final Noop r = new Noop();
1225 <
1226 <        assertTrue(f.cancel(mayInterruptIfRunning));
1227 <        g.complete(v1);
1228 <        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1229 <
1230 <        checkCompletedWithWrappedCancellationException(h);
1231 <        checkCancelled(f);
1232 <        assertFalse(r.ran);
1233 <        checkCompletedNormally(g, v1);
1234 <        }
1235 <    }
1236 <
1237 <    /**
1238 <     * applyToEither result completes normally after normal completion
1239 <     * of either source
1240 <     */
1241 <    public void testApplyToEither() {
1242 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1243 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1244 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1245 <        f.complete(one);
1246 <        checkCompletedNormally(g, two);
1247 <        f2.complete(one);
1248 <        checkCompletedNormally(g, two);
1249 <
1250 <        f = new CompletableFuture<>();
1251 <        f.complete(one);
1252 <        f2 = new CompletableFuture<>();
1253 <        g = f.applyToEither(f2, inc);
1254 <        checkCompletedNormally(g, two);
1255 <    }
1256 <
1257 <    /**
1258 <     * applyToEither result completes exceptionally after exceptional
1259 <     * completion of either source
1260 <     */
1261 <    public void testApplyToEither2() {
1262 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1263 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1264 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1265 <        f.completeExceptionally(new CFException());
1266 <        f2.complete(one);
1267 <        checkCompletedWithWrappedCFException(g);
1268 <
1269 <        f = new CompletableFuture<>();
1270 <        f2 = new CompletableFuture<>();
1271 <        f2.completeExceptionally(new CFException());
1272 <        g = f.applyToEither(f2, inc);
1273 <        checkCompletedWithWrappedCFException(g);
1274 <    }
1275 <
1276 <    /**
1277 <     * applyToEither result completes exceptionally if action does
1278 <     */
1279 <    public void testApplyToEither3() {
1280 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1281 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1282 <        FailingFunction r = new FailingFunction();
1283 <        CompletableFuture<Integer> g = f.applyToEither(f2, r);
1284 <        f2.complete(two);
1285 <        checkCompletedWithWrappedCFException(g);
1286 <    }
1287 <
1288 <    /**
1289 <     * applyToEither result completes exceptionally if either source cancelled
1290 <     */
1291 <    public void testApplyToEither4() {
1292 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1293 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1294 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1295 <        assertTrue(f.cancel(true));
1296 <        checkCompletedWithWrappedCancellationException(g);
1297 <        f = new CompletableFuture<>();
1298 <        f2 = new CompletableFuture<>();
1299 <        assertTrue(f2.cancel(true));
1300 <        checkCompletedWithWrappedCancellationException(g);
1301 <    }
1302 <
1303 <    /**
1304 <     * acceptEither result completes normally after normal completion
1305 <     * of either source
1306 <     */
1307 <    public void testAcceptEither() {
1308 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1309 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1310 <        IncAction r = new IncAction();
1311 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1312 <        f.complete(one);
1313 <        checkCompletedNormally(g, null);
1314 <        f2.complete(one);
1315 <        checkCompletedNormally(g, null);
1316 <        assertEquals(r.value, 2);
1317 <
1318 <        r = new IncAction();
1319 <        f = new CompletableFuture<>();
1320 <        f.complete(one);
1321 <        f2 = new CompletableFuture<>();
1322 <        g = f.acceptEither(f2, r);
1323 <        checkCompletedNormally(g, null);
1324 <        assertEquals(r.value, 2);
1325 <    }
1066 >        assertEquals(1, a.get());
1067 >    }}
1068  
1069      /**
1070 <     * acceptEither result completes exceptionally after exceptional
1071 <     * completion of either source
1330 <     */
1331 <    public void testAcceptEither2() {
1332 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1333 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1334 <        IncAction r = new IncAction();
1335 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1336 <        f.completeExceptionally(new CFException());
1337 <        f2.complete(one);
1338 <        checkCompletedWithWrappedCFException(g);
1339 <
1340 <        r = new IncAction();
1341 <        f = new CompletableFuture<>();
1342 <        f2 = new CompletableFuture<>();
1343 <        f2.completeExceptionally(new CFException());
1344 <        g = f.acceptEither(f2, r);
1345 <        checkCompletedWithWrappedCFException(g);
1346 <    }
1347 <
1348 <    /**
1349 <     * acceptEither result completes exceptionally if action does
1070 >     * handle action completes normally with function value on
1071 >     * exceptional completion of source
1072       */
1073 <    public void testAcceptEither3() {
1074 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1075 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1076 <        FailingConsumer r = new FailingConsumer();
1077 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1078 <        f2.complete(two);
1079 <        checkCompletedWithWrappedCFException(g);
1080 <    }
1073 >    public void testHandle_exceptionalCompletion() {
1074 >        for (ExecutionMode m : ExecutionMode.values())
1075 >        for (boolean createIncomplete : new boolean[] { true, false })
1076 >        for (Integer v1 : new Integer[] { 1, null })
1077 >    {
1078 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1079 >        final AtomicInteger a = new AtomicInteger(0);
1080 >        final CFException ex = new CFException();
1081 >        if (!createIncomplete) f.completeExceptionally(ex);
1082 >        final CompletableFuture<Integer> g = m.handle
1083 >            (f,
1084 >             (Integer x, Throwable t) -> {
1085 >                m.checkExecutionMode();
1086 >                threadAssertNull(x);
1087 >                threadAssertSame(t, ex);
1088 >                a.getAndIncrement();
1089 >                return v1;
1090 >            });
1091 >        if (createIncomplete) f.completeExceptionally(ex);
1092  
1093 <    /**
1094 <     * acceptEither result completes exceptionally if either source cancelled
1095 <     */
1096 <    public void testAcceptEither4() {
1364 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1365 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1366 <        IncAction r = new IncAction();
1367 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1368 <        assertTrue(f.cancel(true));
1369 <        checkCompletedWithWrappedCancellationException(g);
1370 <        f = new CompletableFuture<>();
1371 <        f2 = new CompletableFuture<>();
1372 <        assertTrue(f2.cancel(true));
1373 <        checkCompletedWithWrappedCancellationException(g);
1374 <    }
1093 >        checkCompletedNormally(g, v1);
1094 >        checkCompletedExceptionally(f, ex);
1095 >        assertEquals(1, a.get());
1096 >    }}
1097  
1098      /**
1099 <     * runAfterEither result completes normally after normal completion
1100 <     * of either source
1099 >     * handle action completes normally with function value on
1100 >     * cancelled source
1101       */
1102 <    public void testRunAfterEither() {
1103 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1104 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1105 <        Noop r = new Noop();
1106 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1107 <        f.complete(one);
1108 <        checkCompletedNormally(g, null);
1109 <        f2.complete(one);
1110 <        checkCompletedNormally(g, null);
1111 <        assertTrue(r.ran);
1102 >    public void testHandle_sourceCancelled() {
1103 >        for (ExecutionMode m : ExecutionMode.values())
1104 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1105 >        for (boolean createIncomplete : new boolean[] { true, false })
1106 >        for (Integer v1 : new Integer[] { 1, null })
1107 >    {
1108 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1109 >        final AtomicInteger a = new AtomicInteger(0);
1110 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1111 >        final CompletableFuture<Integer> g = m.handle
1112 >            (f,
1113 >             (Integer x, Throwable t) -> {
1114 >                m.checkExecutionMode();
1115 >                threadAssertNull(x);
1116 >                threadAssertTrue(t instanceof CancellationException);
1117 >                a.getAndIncrement();
1118 >                return v1;
1119 >            });
1120 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1121  
1122 <        r = new Noop();
1123 <        f = new CompletableFuture<>();
1124 <        f.complete(one);
1125 <        f2 = new CompletableFuture<>();
1395 <        g = f.runAfterEither(f2, r);
1396 <        checkCompletedNormally(g, null);
1397 <        assertTrue(r.ran);
1398 <    }
1122 >        checkCompletedNormally(g, v1);
1123 >        checkCancelled(f);
1124 >        assertEquals(1, a.get());
1125 >    }}
1126  
1127      /**
1128 <     * runAfterEither result completes exceptionally after exceptional
1402 <     * completion of either source
1128 >     * handle result completes exceptionally if action does
1129       */
1130 <    public void testRunAfterEither2() {
1131 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1132 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1133 <        Noop r = new Noop();
1134 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1135 <        f.completeExceptionally(new CFException());
1136 <        f2.complete(one);
1137 <        checkCompletedWithWrappedCFException(g);
1138 <
1139 <        r = new Noop();
1140 <        f = new CompletableFuture<>();
1141 <        f2 = new CompletableFuture<>();
1142 <        f2.completeExceptionally(new CFException());
1143 <        g = f.runAfterEither(f2, r);
1144 <        checkCompletedWithWrappedCFException(g);
1145 <    }
1130 >    public void testHandle_sourceFailedActionFailed() {
1131 >        for (ExecutionMode m : ExecutionMode.values())
1132 >        for (boolean createIncomplete : new boolean[] { true, false })
1133 >    {
1134 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1135 >        final AtomicInteger a = new AtomicInteger(0);
1136 >        final CFException ex1 = new CFException();
1137 >        final CFException ex2 = new CFException();
1138 >        if (!createIncomplete) f.completeExceptionally(ex1);
1139 >        final CompletableFuture<Integer> g = m.handle
1140 >            (f,
1141 >             (Integer x, Throwable t) -> {
1142 >                m.checkExecutionMode();
1143 >                threadAssertNull(x);
1144 >                threadAssertSame(ex1, t);
1145 >                a.getAndIncrement();
1146 >                throw ex2;
1147 >            });
1148 >        if (createIncomplete) f.completeExceptionally(ex1);
1149 >
1150 >        checkCompletedWithWrappedException(g, ex2);
1151 >        checkCompletedExceptionally(f, ex1);
1152 >        assertEquals(1, a.get());
1153 >    }}
1154  
1155      /**
1156 <     * runAfterEither result completes exceptionally if action does
1156 >     * If a "handle action" throws an exception when triggered by
1157 >     * a normal completion, it completes exceptionally
1158       */
1159 <    public void testRunAfterEither3() {
1160 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1161 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1162 <        FailingNoop r = new FailingNoop();
1163 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1164 <        f2.complete(two);
1165 <        checkCompletedWithWrappedCFException(g);
1166 <    }
1159 >    public void testHandle_sourceCompletedNormallyActionFailed() {
1160 >        for (ExecutionMode m : ExecutionMode.values())
1161 >        for (boolean createIncomplete : new boolean[] { true, false })
1162 >        for (Integer v1 : new Integer[] { 1, null })
1163 >    {
1164 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1165 >        final AtomicInteger a = new AtomicInteger(0);
1166 >        final CFException ex = new CFException();
1167 >        if (!createIncomplete) assertTrue(f.complete(v1));
1168 >        final CompletableFuture<Integer> g = m.handle
1169 >            (f,
1170 >             (Integer x, Throwable t) -> {
1171 >                m.checkExecutionMode();
1172 >                threadAssertSame(x, v1);
1173 >                threadAssertNull(t);
1174 >                a.getAndIncrement();
1175 >                throw ex;
1176 >            });
1177 >        if (createIncomplete) assertTrue(f.complete(v1));
1178  
1179 <    /**
1180 <     * runAfterEither result completes exceptionally if either source cancelled
1181 <     */
1182 <    public void testRunAfterEither4() {
1437 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1438 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1439 <        Noop r = new Noop();
1440 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1441 <        assertTrue(f.cancel(true));
1442 <        checkCompletedWithWrappedCancellationException(g);
1443 <        f = new CompletableFuture<>();
1444 <        f2 = new CompletableFuture<>();
1445 <        assertTrue(f2.cancel(true));
1446 <        checkCompletedWithWrappedCancellationException(g);
1447 <    }
1179 >        checkCompletedWithWrappedException(g, ex);
1180 >        checkCompletedNormally(f, v1);
1181 >        assertEquals(1, a.get());
1182 >    }}
1183  
1184      /**
1185 <     * thenCompose result completes normally after normal completion of source
1185 >     * runAsync completes after running Runnable
1186       */
1187 <    public void testThenCompose() {
1188 <        CompletableFuture<Integer> f, g;
1189 <        CompletableFutureInc r;
1190 <
1191 <        f = new CompletableFuture<>();
1192 <        g = f.thenCompose(r = new CompletableFutureInc());
1193 <        f.complete(one);
1194 <        checkCompletedNormally(g, two);
1195 <        assertTrue(r.ran);
1196 <
1197 <        f = new CompletableFuture<>();
1198 <        f.complete(one);
1199 <        g = f.thenCompose(r = new CompletableFutureInc());
1465 <        checkCompletedNormally(g, two);
1466 <        assertTrue(r.ran);
1467 <    }
1187 >    public void testRunAsync_normalCompletion() {
1188 >        ExecutionMode[] executionModes = {
1189 >            ExecutionMode.ASYNC,
1190 >            ExecutionMode.EXECUTOR,
1191 >        };
1192 >        for (ExecutionMode m : executionModes)
1193 >    {
1194 >        final Noop r = new Noop(m);
1195 >        final CompletableFuture<Void> f = m.runAsync(r);
1196 >        assertNull(f.join());
1197 >        checkCompletedNormally(f, null);
1198 >        r.assertInvoked();
1199 >    }}
1200  
1201      /**
1202 <     * thenCompose result completes exceptionally after exceptional
1471 <     * completion of source
1202 >     * failing runAsync completes exceptionally after running Runnable
1203       */
1204 <    public void testThenCompose2() {
1205 <        CompletableFuture<Integer> f, g;
1206 <        CompletableFutureInc r;
1207 <
1208 <        f = new CompletableFuture<>();
1209 <        g = f.thenCompose(r = new CompletableFutureInc());
1210 <        f.completeExceptionally(new CFException());
1211 <        checkCompletedWithWrappedCFException(g);
1212 <
1213 <        f = new CompletableFuture<>();
1214 <        f.completeExceptionally(new CFException());
1215 <        g = f.thenCompose(r = new CompletableFutureInc());
1485 <        checkCompletedWithWrappedCFException(g);
1486 <    }
1204 >    public void testRunAsync_exceptionalCompletion() {
1205 >        ExecutionMode[] executionModes = {
1206 >            ExecutionMode.ASYNC,
1207 >            ExecutionMode.EXECUTOR,
1208 >        };
1209 >        for (ExecutionMode m : executionModes)
1210 >    {
1211 >        final FailingRunnable r = new FailingRunnable(m);
1212 >        final CompletableFuture<Void> f = m.runAsync(r);
1213 >        checkCompletedWithWrappedCFException(f);
1214 >        r.assertInvoked();
1215 >    }}
1216  
1217      /**
1218 <     * thenCompose result completes exceptionally if action does
1218 >     * supplyAsync completes with result of supplier
1219       */
1220 <    public void testThenCompose3() {
1221 <        CompletableFuture<Integer> f, g;
1222 <        FailingCompletableFutureFunction r;
1223 <
1224 <        f = new CompletableFuture<>();
1225 <        g = f.thenCompose(r = new FailingCompletableFutureFunction());
1226 <        f.complete(one);
1227 <        checkCompletedWithWrappedCFException(g);
1228 <
1229 <        f = new CompletableFuture<>();
1230 <        f.complete(one);
1231 <        g = f.thenCompose(r = new FailingCompletableFutureFunction());
1232 <        checkCompletedWithWrappedCFException(g);
1233 <    }
1220 >    public void testSupplyAsync_normalCompletion() {
1221 >        ExecutionMode[] executionModes = {
1222 >            ExecutionMode.ASYNC,
1223 >            ExecutionMode.EXECUTOR,
1224 >        };
1225 >        for (ExecutionMode m : executionModes)
1226 >        for (Integer v1 : new Integer[] { 1, null })
1227 >    {
1228 >        final IntegerSupplier r = new IntegerSupplier(m, v1);
1229 >        final CompletableFuture<Integer> f = m.supplyAsync(r);
1230 >        assertSame(v1, f.join());
1231 >        checkCompletedNormally(f, v1);
1232 >        r.assertInvoked();
1233 >    }}
1234  
1235      /**
1236 <     * thenCompose result completes exceptionally if source cancelled
1236 >     * Failing supplyAsync completes exceptionally
1237       */
1238 <    public void testThenCompose4() {
1239 <        CompletableFuture<Integer> f, g;
1240 <        CompletableFutureInc r;
1241 <
1242 <        f = new CompletableFuture<>();
1243 <        g = f.thenCompose(r = new CompletableFutureInc());
1244 <        assertTrue(f.cancel(true));
1245 <        checkCompletedWithWrappedCancellationException(g);
1246 <
1247 <        f = new CompletableFuture<>();
1248 <        assertTrue(f.cancel(true));
1249 <        g = f.thenCompose(r = new CompletableFutureInc());
1521 <        checkCompletedWithWrappedCancellationException(g);
1522 <    }
1238 >    public void testSupplyAsync_exceptionalCompletion() {
1239 >        ExecutionMode[] executionModes = {
1240 >            ExecutionMode.ASYNC,
1241 >            ExecutionMode.EXECUTOR,
1242 >        };
1243 >        for (ExecutionMode m : executionModes)
1244 >    {
1245 >        FailingSupplier r = new FailingSupplier(m);
1246 >        CompletableFuture<Integer> f = m.supplyAsync(r);
1247 >        checkCompletedWithWrappedCFException(f);
1248 >        r.assertInvoked();
1249 >    }}
1250  
1251 <    // asyncs
1251 >    // seq completion methods
1252  
1253      /**
1254 <     * thenRunAsync result completes normally after normal completion of source
1254 >     * thenRun result completes normally after normal completion of source
1255       */
1256 <    public void testThenRunAsync() {
1257 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1258 <        Noop r = new Noop();
1259 <        CompletableFuture<Void> g = f.thenRunAsync(r);
1260 <        f.complete(null);
1261 <        checkCompletedNormally(g, null);
1256 >    public void testThenRun_normalCompletion() {
1257 >        for (ExecutionMode m : ExecutionMode.values())
1258 >        for (Integer v1 : new Integer[] { 1, null })
1259 >    {
1260 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1261 >        final Noop[] rs = new Noop[6];
1262 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1263  
1264 <        // reordered version
1265 <        f = new CompletableFuture<>();
1266 <        f.complete(null);
1267 <        r = new Noop();
1268 <        g = f.thenRunAsync(r);
1269 <        checkCompletedNormally(g, null);
1270 <    }
1264 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1265 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1266 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1267 >        checkIncomplete(h0);
1268 >        checkIncomplete(h1);
1269 >        checkIncomplete(h2);
1270 >        assertTrue(f.complete(v1));
1271 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1272 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1273 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1274 >
1275 >        checkCompletedNormally(h0, null);
1276 >        checkCompletedNormally(h1, null);
1277 >        checkCompletedNormally(h2, null);
1278 >        checkCompletedNormally(h3, null);
1279 >        checkCompletedNormally(h4, null);
1280 >        checkCompletedNormally(h5, null);
1281 >        checkCompletedNormally(f, v1);
1282 >        for (Noop r : rs) r.assertInvoked();
1283 >    }}
1284  
1285      /**
1286 <     * thenRunAsync result completes exceptionally after exceptional
1286 >     * thenRun result completes exceptionally after exceptional
1287       * completion of source
1288       */
1289 <    public void testThenRunAsync2() {
1290 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1291 <        Noop r = new Noop();
1292 <        CompletableFuture<Void> g = f.thenRunAsync(r);
1293 <        f.completeExceptionally(new CFException());
1294 <        try {
1295 <            g.join();
1555 <            shouldThrow();
1556 <        } catch (CompletionException success) {}
1557 <        checkCompletedWithWrappedCFException(g);
1558 <    }
1289 >    public void testThenRun_exceptionalCompletion() {
1290 >        for (ExecutionMode m : ExecutionMode.values())
1291 >    {
1292 >        final CFException ex = new CFException();
1293 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1294 >        final Noop[] rs = new Noop[6];
1295 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1296  
1297 <    /**
1298 <     * thenRunAsync result completes exceptionally if action does
1299 <     */
1300 <    public void testThenRunAsync3() {
1301 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1302 <        FailingNoop r = new FailingNoop();
1303 <        CompletableFuture<Void> g = f.thenRunAsync(r);
1304 <        f.complete(null);
1305 <        checkCompletedWithWrappedCFException(g);
1306 <    }
1297 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1298 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1299 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1300 >        checkIncomplete(h0);
1301 >        checkIncomplete(h1);
1302 >        checkIncomplete(h2);
1303 >        assertTrue(f.completeExceptionally(ex));
1304 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1305 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1306 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1307 >
1308 >        checkCompletedWithWrappedException(h0, ex);
1309 >        checkCompletedWithWrappedException(h1, ex);
1310 >        checkCompletedWithWrappedException(h2, ex);
1311 >        checkCompletedWithWrappedException(h3, ex);
1312 >        checkCompletedWithWrappedException(h4, ex);
1313 >        checkCompletedWithWrappedException(h5, ex);
1314 >        checkCompletedExceptionally(f, ex);
1315 >        for (Noop r : rs) r.assertNotInvoked();
1316 >    }}
1317  
1318      /**
1319 <     * thenRunAsync result completes exceptionally if source cancelled
1319 >     * thenRun result completes exceptionally if source cancelled
1320       */
1321 <    public void testThenRunAsync4() {
1322 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1323 <        Noop r = new Noop();
1324 <        CompletableFuture<Void> g = f.thenRunAsync(r);
1325 <        assertTrue(f.cancel(true));
1326 <        checkCompletedWithWrappedCancellationException(g);
1327 <    }
1321 >    public void testThenRun_sourceCancelled() {
1322 >        for (ExecutionMode m : ExecutionMode.values())
1323 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1324 >    {
1325 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1326 >        final Noop[] rs = new Noop[6];
1327 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1328  
1329 <    /**
1330 <     * thenApplyAsync result completes normally after normal completion of source
1331 <     */
1332 <    public void testThenApplyAsync() {
1333 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1334 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1335 <        f.complete(one);
1336 <        checkCompletedNormally(g, two);
1337 <    }
1329 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1330 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1331 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1332 >        checkIncomplete(h0);
1333 >        checkIncomplete(h1);
1334 >        checkIncomplete(h2);
1335 >        assertTrue(f.cancel(mayInterruptIfRunning));
1336 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1337 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1338 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1339 >
1340 >        checkCompletedWithWrappedCancellationException(h0);
1341 >        checkCompletedWithWrappedCancellationException(h1);
1342 >        checkCompletedWithWrappedCancellationException(h2);
1343 >        checkCompletedWithWrappedCancellationException(h3);
1344 >        checkCompletedWithWrappedCancellationException(h4);
1345 >        checkCompletedWithWrappedCancellationException(h5);
1346 >        checkCancelled(f);
1347 >        for (Noop r : rs) r.assertNotInvoked();
1348 >    }}
1349  
1350      /**
1351 <     * thenApplyAsync result completes exceptionally after exceptional
1594 <     * completion of source
1351 >     * thenRun result completes exceptionally if action does
1352       */
1353 <    public void testThenApplyAsync2() {
1354 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1355 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1356 <        f.completeExceptionally(new CFException());
1357 <        checkCompletedWithWrappedCFException(g);
1358 <    }
1353 >    public void testThenRun_actionFailed() {
1354 >        for (ExecutionMode m : ExecutionMode.values())
1355 >        for (Integer v1 : new Integer[] { 1, null })
1356 >    {
1357 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1358 >        final FailingRunnable[] rs = new FailingRunnable[6];
1359 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
1360  
1361 <    /**
1362 <     * thenApplyAsync result completes exceptionally if action does
1363 <     */
1364 <    public void testThenApplyAsync3() {
1365 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1366 <        FailingFunction r = new FailingFunction();
1367 <        CompletableFuture<Integer> g = f.thenApplyAsync(r);
1368 <        f.complete(null);
1369 <        checkCompletedWithWrappedCFException(g);
1370 <    }
1361 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1362 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1363 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1364 >        assertTrue(f.complete(v1));
1365 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1366 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1367 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1368 >
1369 >        checkCompletedWithWrappedCFException(h0);
1370 >        checkCompletedWithWrappedCFException(h1);
1371 >        checkCompletedWithWrappedCFException(h2);
1372 >        checkCompletedWithWrappedCFException(h3);
1373 >        checkCompletedWithWrappedCFException(h4);
1374 >        checkCompletedWithWrappedCFException(h5);
1375 >        checkCompletedNormally(f, v1);
1376 >    }}
1377  
1378      /**
1379 <     * thenApplyAsync result completes exceptionally if source cancelled
1379 >     * thenApply result completes normally after normal completion of source
1380       */
1381 <    public void testThenApplyAsync4() {
1382 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1383 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1384 <        assertTrue(f.cancel(true));
1385 <        checkCompletedWithWrappedCancellationException(g);
1386 <    }
1381 >    public void testThenApply_normalCompletion() {
1382 >        for (ExecutionMode m : ExecutionMode.values())
1383 >        for (Integer v1 : new Integer[] { 1, null })
1384 >    {
1385 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1386 >        final IncFunction[] rs = new IncFunction[4];
1387 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1388  
1389 <    /**
1390 <     * thenAcceptAsync result completes normally after normal
1391 <     * completion of source
1392 <     */
1393 <    public void testThenAcceptAsync() {
1394 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1395 <        IncAction r = new IncAction();
1396 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1397 <        f.complete(one);
1398 <        checkCompletedNormally(g, null);
1399 <        assertEquals(r.value, 2);
1400 <    }
1389 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1390 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1391 >        checkIncomplete(h0);
1392 >        checkIncomplete(h1);
1393 >        assertTrue(f.complete(v1));
1394 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1395 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1396 >
1397 >        checkCompletedNormally(h0, inc(v1));
1398 >        checkCompletedNormally(h1, inc(v1));
1399 >        checkCompletedNormally(h2, inc(v1));
1400 >        checkCompletedNormally(h3, inc(v1));
1401 >        checkCompletedNormally(f, v1);
1402 >        for (IncFunction r : rs) r.assertValue(inc(v1));
1403 >    }}
1404  
1405      /**
1406 <     * thenAcceptAsync result completes exceptionally after exceptional
1406 >     * thenApply result completes exceptionally after exceptional
1407       * completion of source
1408       */
1409 <    public void testThenAcceptAsync2() {
1410 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1411 <        IncAction r = new IncAction();
1412 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1413 <        f.completeExceptionally(new CFException());
1414 <        checkCompletedWithWrappedCFException(g);
1415 <    }
1648 <
1649 <    /**
1650 <     * thenAcceptAsync result completes exceptionally if action does
1651 <     */
1652 <    public void testThenAcceptAsync3() {
1653 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1654 <        FailingConsumer r = new FailingConsumer();
1655 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1656 <        f.complete(null);
1657 <        checkCompletedWithWrappedCFException(g);
1658 <    }
1659 <
1660 <    /**
1661 <     * thenAcceptAsync result completes exceptionally if source cancelled
1662 <     */
1663 <    public void testThenAcceptAsync4() {
1664 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1665 <        IncAction r = new IncAction();
1666 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1667 <        assertTrue(f.cancel(true));
1668 <        checkCompletedWithWrappedCancellationException(g);
1669 <    }
1670 <
1671 <    /**
1672 <     * thenCombineAsync result completes normally after normal
1673 <     * completion of sources
1674 <     */
1675 <    public void testThenCombineAsync() {
1676 <        CompletableFuture<Integer> f, g, h;
1677 <
1678 <        f = new CompletableFuture<>();
1679 <        g = new CompletableFuture<>();
1680 <        h = f.thenCombineAsync(g, subtract);
1681 <        f.complete(3);
1682 <        checkIncomplete(h);
1683 <        g.complete(1);
1684 <        checkCompletedNormally(h, 2);
1685 <
1686 <        f = new CompletableFuture<>();
1687 <        g = new CompletableFuture<>();
1688 <        h = f.thenCombineAsync(g, subtract);
1689 <        g.complete(1);
1690 <        checkIncomplete(h);
1691 <        f.complete(3);
1692 <        checkCompletedNormally(h, 2);
1409 >    public void testThenApply_exceptionalCompletion() {
1410 >        for (ExecutionMode m : ExecutionMode.values())
1411 >    {
1412 >        final CFException ex = new CFException();
1413 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1414 >        final IncFunction[] rs = new IncFunction[4];
1415 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1416  
1417 <        f = new CompletableFuture<>();
1418 <        g = new CompletableFuture<>();
1419 <        g.complete(1);
1420 <        f.complete(3);
1421 <        h = f.thenCombineAsync(g, subtract);
1422 <        checkCompletedNormally(h, 2);
1423 <    }
1417 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1418 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1419 >        assertTrue(f.completeExceptionally(ex));
1420 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1421 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1422 >
1423 >        checkCompletedWithWrappedException(h0, ex);
1424 >        checkCompletedWithWrappedException(h1, ex);
1425 >        checkCompletedWithWrappedException(h2, ex);
1426 >        checkCompletedWithWrappedException(h3, ex);
1427 >        checkCompletedExceptionally(f, ex);
1428 >        for (IncFunction r : rs) r.assertNotInvoked();
1429 >    }}
1430  
1431      /**
1432 <     * thenCombineAsync result completes exceptionally after exceptional
1704 <     * completion of either source
1432 >     * thenApply result completes exceptionally if source cancelled
1433       */
1434 <    public void testThenCombineAsync2() {
1435 <        CompletableFuture<Integer> f, g, h;
1436 <
1437 <        f = new CompletableFuture<>();
1438 <        g = new CompletableFuture<>();
1439 <        h = f.thenCombineAsync(g, subtract);
1440 <        f.completeExceptionally(new CFException());
1713 <        checkIncomplete(h);
1714 <        g.complete(1);
1715 <        checkCompletedWithWrappedCFException(h);
1716 <
1717 <        f = new CompletableFuture<>();
1718 <        g = new CompletableFuture<>();
1719 <        h = f.thenCombineAsync(g, subtract);
1720 <        g.completeExceptionally(new CFException());
1721 <        checkIncomplete(h);
1722 <        f.complete(3);
1723 <        checkCompletedWithWrappedCFException(h);
1434 >    public void testThenApply_sourceCancelled() {
1435 >        for (ExecutionMode m : ExecutionMode.values())
1436 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1437 >    {
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 <        f = new CompletableFuture<>();
1443 <        g = new CompletableFuture<>();
1444 <        g.completeExceptionally(new CFException());
1445 <        f.complete(3);
1446 <        h = f.thenCombineAsync(g, subtract);
1730 <        checkCompletedWithWrappedCFException(h);
1731 <    }
1442 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1443 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1444 >        assertTrue(f.cancel(mayInterruptIfRunning));
1445 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1446 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1447  
1448 <    /**
1449 <     * thenCombineAsync result completes exceptionally if action does
1450 <     */
1451 <    public void testThenCombineAsync3() {
1452 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1453 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1454 <        FailingBiFunction r = new FailingBiFunction();
1740 <        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r);
1741 <        f.complete(one);
1742 <        checkIncomplete(g);
1743 <        assertFalse(r.ran);
1744 <        f2.complete(two);
1745 <        checkCompletedWithWrappedCFException(g);
1746 <        assertTrue(r.ran);
1747 <    }
1448 >        checkCompletedWithWrappedCancellationException(h0);
1449 >        checkCompletedWithWrappedCancellationException(h1);
1450 >        checkCompletedWithWrappedCancellationException(h2);
1451 >        checkCompletedWithWrappedCancellationException(h3);
1452 >        checkCancelled(f);
1453 >        for (IncFunction r : rs) r.assertNotInvoked();
1454 >    }}
1455  
1456      /**
1457 <     * thenCombineAsync result completes exceptionally if either source cancelled
1457 >     * thenApply result completes exceptionally if action does
1458       */
1459 <    public void testThenCombineAsync4() {
1460 <        CompletableFuture<Integer> f, g, h;
1461 <
1462 <        f = new CompletableFuture<>();
1463 <        g = new CompletableFuture<>();
1464 <        h = f.thenCombineAsync(g, subtract);
1465 <        assertTrue(f.cancel(true));
1759 <        checkIncomplete(h);
1760 <        g.complete(1);
1761 <        checkCompletedWithWrappedCancellationException(h);
1762 <
1763 <        f = new CompletableFuture<>();
1764 <        g = new CompletableFuture<>();
1765 <        h = f.thenCombineAsync(g, subtract);
1766 <        assertTrue(g.cancel(true));
1767 <        checkIncomplete(h);
1768 <        f.complete(3);
1769 <        checkCompletedWithWrappedCancellationException(h);
1770 <
1771 <        f = new CompletableFuture<>();
1772 <        g = new CompletableFuture<>();
1773 <        g.complete(3);
1774 <        assertTrue(f.cancel(true));
1775 <        h = f.thenCombineAsync(g, subtract);
1776 <        checkCompletedWithWrappedCancellationException(h);
1459 >    public void testThenApply_actionFailed() {
1460 >        for (ExecutionMode m : ExecutionMode.values())
1461 >        for (Integer v1 : new Integer[] { 1, null })
1462 >    {
1463 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1464 >        final FailingFunction[] rs = new FailingFunction[4];
1465 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
1466  
1467 <        f = new CompletableFuture<>();
1468 <        g = new CompletableFuture<>();
1469 <        f.complete(3);
1470 <        assertTrue(g.cancel(true));
1471 <        h = f.thenCombineAsync(g, subtract);
1472 <        checkCompletedWithWrappedCancellationException(h);
1473 <    }
1467 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1468 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1469 >        assertTrue(f.complete(v1));
1470 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1471 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1472 >
1473 >        checkCompletedWithWrappedCFException(h0);
1474 >        checkCompletedWithWrappedCFException(h1);
1475 >        checkCompletedWithWrappedCFException(h2);
1476 >        checkCompletedWithWrappedCFException(h3);
1477 >        checkCompletedNormally(f, v1);
1478 >    }}
1479  
1480      /**
1481 <     * thenAcceptBothAsync result completes normally after normal
1788 <     * completion of sources
1481 >     * thenAccept result completes normally after normal completion of source
1482       */
1483 <    public void testThenAcceptBothAsync() {
1484 <        CompletableFuture<Integer> f, g;
1485 <        CompletableFuture<Void> h;
1486 <        SubtractAction r;
1487 <
1488 <        f = new CompletableFuture<>();
1489 <        g = new CompletableFuture<>();
1797 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1798 <        f.complete(3);
1799 <        checkIncomplete(h);
1800 <        g.complete(1);
1801 <        checkCompletedNormally(h, null);
1802 <        assertEquals(r.value, 2);
1803 <
1804 <        f = new CompletableFuture<>();
1805 <        g = new CompletableFuture<>();
1806 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1807 <        g.complete(1);
1808 <        checkIncomplete(h);
1809 <        f.complete(3);
1810 <        checkCompletedNormally(h, null);
1811 <        assertEquals(r.value, 2);
1483 >    public void testThenAccept_normalCompletion() {
1484 >        for (ExecutionMode m : ExecutionMode.values())
1485 >        for (Integer v1 : new Integer[] { 1, null })
1486 >    {
1487 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1488 >        final NoopConsumer[] rs = new NoopConsumer[4];
1489 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1490  
1491 <        f = new CompletableFuture<>();
1492 <        g = new CompletableFuture<>();
1493 <        g.complete(1);
1494 <        f.complete(3);
1495 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1496 <        checkCompletedNormally(h, null);
1497 <        assertEquals(r.value, 2);
1498 <    }
1491 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1492 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1493 >        checkIncomplete(h0);
1494 >        checkIncomplete(h1);
1495 >        assertTrue(f.complete(v1));
1496 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1497 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1498 >
1499 >        checkCompletedNormally(h0, null);
1500 >        checkCompletedNormally(h1, null);
1501 >        checkCompletedNormally(h2, null);
1502 >        checkCompletedNormally(h3, null);
1503 >        checkCompletedNormally(f, v1);
1504 >        for (NoopConsumer r : rs) r.assertValue(v1);
1505 >    }}
1506  
1507      /**
1508 <     * thenAcceptBothAsync result completes exceptionally after exceptional
1508 >     * thenAccept result completes exceptionally after exceptional
1509       * completion of source
1510       */
1511 <    public void testThenAcceptBothAsync2() {
1512 <        CompletableFuture<Integer> f, g;
1513 <        CompletableFuture<Void> h;
1514 <        SubtractAction r;
1515 <
1516 <        f = new CompletableFuture<>();
1517 <        g = new CompletableFuture<>();
1833 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1834 <        f.completeExceptionally(new CFException());
1835 <        checkIncomplete(h);
1836 <        g.complete(1);
1837 <        checkCompletedWithWrappedCFException(h);
1838 <
1839 <        f = new CompletableFuture<>();
1840 <        g = new CompletableFuture<>();
1841 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1842 <        g.completeExceptionally(new CFException());
1843 <        checkIncomplete(h);
1844 <        f.complete(3);
1845 <        checkCompletedWithWrappedCFException(h);
1846 <
1847 <        f = new CompletableFuture<>();
1848 <        g = new CompletableFuture<>();
1849 <        f.complete(3);
1850 <        g.completeExceptionally(new CFException());
1851 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1852 <        checkCompletedWithWrappedCFException(h);
1853 <
1854 <        f = new CompletableFuture<>();
1855 <        g = new CompletableFuture<>();
1856 <        f.completeExceptionally(new CFException());
1857 <        g.complete(3);
1858 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1859 <        checkCompletedWithWrappedCFException(h);
1860 <    }
1861 <
1862 <    /**
1863 <     * thenAcceptBothAsync result completes exceptionally if action does
1864 <     */
1865 <    public void testThenAcceptBothAsync3() {
1866 <        CompletableFuture<Integer> f, g;
1867 <        CompletableFuture<Void> h;
1868 <        FailingBiConsumer r;
1869 <
1870 <        f = new CompletableFuture<>();
1871 <        g = new CompletableFuture<>();
1872 <        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer());
1873 <        f.complete(3);
1874 <        checkIncomplete(h);
1875 <        g.complete(1);
1876 <        checkCompletedWithWrappedCFException(h);
1511 >    public void testThenAccept_exceptionalCompletion() {
1512 >        for (ExecutionMode m : ExecutionMode.values())
1513 >    {
1514 >        final CFException ex = new CFException();
1515 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1516 >        final NoopConsumer[] rs = new NoopConsumer[4];
1517 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1518  
1519 <        f = new CompletableFuture<>();
1520 <        g = new CompletableFuture<>();
1521 <        f.complete(3);
1522 <        g.complete(1);
1523 <        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer());
1524 <        checkCompletedWithWrappedCFException(h);
1525 <    }
1519 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1520 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1521 >        assertTrue(f.completeExceptionally(ex));
1522 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1523 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1524 >
1525 >        checkCompletedWithWrappedException(h0, ex);
1526 >        checkCompletedWithWrappedException(h1, ex);
1527 >        checkCompletedWithWrappedException(h2, ex);
1528 >        checkCompletedWithWrappedException(h3, ex);
1529 >        checkCompletedExceptionally(f, ex);
1530 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1531 >    }}
1532  
1533      /**
1534 <     * thenAcceptBothAsync result completes exceptionally if either source cancelled
1534 >     * thenAccept result completes exceptionally if source cancelled
1535       */
1536 <    public void testThenAcceptBothAsync4() {
1537 <        CompletableFuture<Integer> f, g;
1538 <        CompletableFuture<Void> h;
1539 <        SubtractAction r;
1540 <
1541 <        f = new CompletableFuture<>();
1542 <        g = new CompletableFuture<>();
1896 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1897 <        assertTrue(f.cancel(true));
1898 <        checkIncomplete(h);
1899 <        g.complete(1);
1900 <        checkCompletedWithWrappedCancellationException(h);
1901 <
1902 <        f = new CompletableFuture<>();
1903 <        g = new CompletableFuture<>();
1904 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1905 <        assertTrue(g.cancel(true));
1906 <        checkIncomplete(h);
1907 <        f.complete(3);
1908 <        checkCompletedWithWrappedCancellationException(h);
1909 <
1910 <        f = new CompletableFuture<>();
1911 <        g = new CompletableFuture<>();
1912 <        f.complete(3);
1913 <        assertTrue(g.cancel(true));
1914 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1915 <        checkCompletedWithWrappedCancellationException(h);
1536 >    public void testThenAccept_sourceCancelled() {
1537 >        for (ExecutionMode m : ExecutionMode.values())
1538 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1539 >    {
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 <        f = new CompletableFuture<>();
1545 <        g = new CompletableFuture<>();
1546 <        assertTrue(f.cancel(true));
1547 <        g.complete(3);
1548 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1922 <        checkCompletedWithWrappedCancellationException(h);
1923 <    }
1544 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1545 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1546 >        assertTrue(f.cancel(mayInterruptIfRunning));
1547 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1548 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1549  
1550 <    /**
1551 <     * runAfterBothAsync result completes normally after normal
1552 <     * completion of sources
1553 <     */
1554 <    public void testRunAfterBothAsync() {
1555 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1556 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1932 <        Noop r = new Noop();
1933 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1934 <        f.complete(one);
1935 <        checkIncomplete(g);
1936 <        f2.complete(two);
1937 <        checkCompletedNormally(g, null);
1938 <        assertTrue(r.ran);
1939 <    }
1550 >        checkCompletedWithWrappedCancellationException(h0);
1551 >        checkCompletedWithWrappedCancellationException(h1);
1552 >        checkCompletedWithWrappedCancellationException(h2);
1553 >        checkCompletedWithWrappedCancellationException(h3);
1554 >        checkCancelled(f);
1555 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1556 >    }}
1557  
1558      /**
1559 <     * runAfterBothAsync result completes exceptionally after exceptional
1943 <     * completion of source
1559 >     * thenAccept result completes exceptionally if action does
1560       */
1561 <    public void testRunAfterBothAsync2() {
1562 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1563 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1564 <        Noop r = new Noop();
1565 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1566 <        f.completeExceptionally(new CFException());
1567 <        f2.complete(two);
1952 <        checkCompletedWithWrappedCFException(g);
1953 <
1954 <        r = new Noop();
1955 <        f = new CompletableFuture<>();
1956 <        f2 = new CompletableFuture<>();
1957 <        g = f.runAfterBothAsync(f2, r);
1958 <        f.complete(one);
1959 <        f2.completeExceptionally(new CFException());
1960 <        checkCompletedWithWrappedCFException(g);
1961 <    }
1561 >    public void testThenAccept_actionFailed() {
1562 >        for (ExecutionMode m : ExecutionMode.values())
1563 >        for (Integer v1 : new Integer[] { 1, null })
1564 >    {
1565 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1566 >        final FailingConsumer[] rs = new FailingConsumer[4];
1567 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
1568  
1569 <    /**
1570 <     * runAfterBothAsync result completes exceptionally if action does
1571 <     */
1572 <    public void testRunAfterBothAsync3() {
1573 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1574 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1575 <        FailingNoop r = new FailingNoop();
1576 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1577 <        f.complete(one);
1578 <        checkIncomplete(g);
1579 <        f2.complete(two);
1580 <        checkCompletedWithWrappedCFException(g);
1975 <    }
1569 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1570 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1571 >        assertTrue(f.complete(v1));
1572 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1573 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1574 >
1575 >        checkCompletedWithWrappedCFException(h0);
1576 >        checkCompletedWithWrappedCFException(h1);
1577 >        checkCompletedWithWrappedCFException(h2);
1578 >        checkCompletedWithWrappedCFException(h3);
1579 >        checkCompletedNormally(f, v1);
1580 >    }}
1581  
1582      /**
1583 <     * runAfterBothAsync result completes exceptionally if either source cancelled
1583 >     * thenCombine result completes normally after normal completion
1584 >     * of sources
1585       */
1586 <    public void testRunAfterBothAsync4() {
1587 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1588 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1589 <        Noop r = new Noop();
1590 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1591 <        assertTrue(f.cancel(true));
1592 <        f2.complete(two);
1593 <        checkCompletedWithWrappedCancellationException(g);
1594 <
1595 <        r = new Noop();
1990 <        f = new CompletableFuture<>();
1991 <        f2 = new CompletableFuture<>();
1992 <        g = f.runAfterBothAsync(f2, r);
1993 <        f.complete(one);
1994 <        assertTrue(f2.cancel(true));
1995 <        checkCompletedWithWrappedCancellationException(g);
1996 <    }
1586 >    public void testThenCombine_normalCompletion() {
1587 >        for (ExecutionMode m : ExecutionMode.values())
1588 >        for (boolean fFirst : new boolean[] { true, false })
1589 >        for (Integer v1 : new Integer[] { 1, null })
1590 >        for (Integer v2 : new Integer[] { 2, null })
1591 >    {
1592 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1593 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1594 >        final SubtractFunction[] rs = new SubtractFunction[6];
1595 >        for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m);
1596  
1597 <    /**
1598 <     * applyToEitherAsync result completes normally after normal
1599 <     * completion of sources
1600 <     */
1601 <    public void testApplyToEitherAsync() {
1602 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1603 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1604 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1605 <        f.complete(one);
1606 <        checkCompletedNormally(g, two);
1597 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1598 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1599 >        final Integer w1 =  fFirst ? v1 : v2;
1600 >        final Integer w2 = !fFirst ? v1 : v2;
1601 >
1602 >        final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]);
1603 >        final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]);
1604 >        assertTrue(fst.complete(w1));
1605 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]);
1606 >        final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]);
1607 >        checkIncomplete(h0); rs[0].assertNotInvoked();
1608 >        checkIncomplete(h2); rs[2].assertNotInvoked();
1609 >        checkCompletedNormally(h1, subtract(w1, w1));
1610 >        checkCompletedNormally(h3, subtract(w1, w1));
1611 >        rs[1].assertValue(subtract(w1, w1));
1612 >        rs[3].assertValue(subtract(w1, w1));
1613 >        assertTrue(snd.complete(w2));
1614 >        final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]);
1615 >
1616 >        checkCompletedNormally(h0, subtract(v1, v2));
1617 >        checkCompletedNormally(h2, subtract(v1, v2));
1618 >        checkCompletedNormally(h4, subtract(v1, v2));
1619 >        rs[0].assertValue(subtract(v1, v2));
1620 >        rs[2].assertValue(subtract(v1, v2));
1621 >        rs[4].assertValue(subtract(v1, v2));
1622  
1623 <        f = new CompletableFuture<>();
1624 <        f.complete(one);
1625 <        f2 = new CompletableFuture<>();
2012 <        g = f.applyToEitherAsync(f2, inc);
2013 <        checkCompletedNormally(g, two);
2014 <    }
1623 >        checkCompletedNormally(f, v1);
1624 >        checkCompletedNormally(g, v2);
1625 >    }}
1626  
1627      /**
1628 <     * applyToEitherAsync result completes exceptionally after exceptional
1629 <     * completion of source
1628 >     * thenCombine result completes exceptionally after exceptional
1629 >     * completion of either source
1630       */
1631 <    public void testApplyToEitherAsync2() {
1632 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1633 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1634 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1635 <        f.completeExceptionally(new CFException());
1636 <        checkCompletedWithWrappedCFException(g);
1637 <
1638 <        f = new CompletableFuture<>();
1639 <        f2 = new CompletableFuture<>();
1640 <        f2.completeExceptionally(new CFException());
1641 <        g = f.applyToEitherAsync(f2, inc);
1642 <        f.complete(one);
1643 <        checkCompletedWithWrappedCFException(g);
1644 <    }
1631 >    public void testThenCombine_exceptionalCompletion() throws Throwable {
1632 >        for (ExecutionMode m : ExecutionMode.values())
1633 >        for (boolean fFirst : new boolean[] { true, false })
1634 >        for (boolean failFirst : new boolean[] { true, false })
1635 >        for (Integer v1 : new Integer[] { 1, null })
1636 >    {
1637 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1638 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1639 >        final CFException ex = new CFException();
1640 >        final SubtractFunction r1 = new SubtractFunction(m);
1641 >        final SubtractFunction r2 = new SubtractFunction(m);
1642 >        final SubtractFunction r3 = new SubtractFunction(m);
1643 >
1644 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1645 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1646 >        final Callable<Boolean> complete1 = failFirst ?
1647 >            () -> fst.completeExceptionally(ex) :
1648 >            () -> fst.complete(v1);
1649 >        final Callable<Boolean> complete2 = failFirst ?
1650 >            () -> snd.complete(v1) :
1651 >            () -> snd.completeExceptionally(ex);
1652 >
1653 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1654 >        assertTrue(complete1.call());
1655 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1656 >        checkIncomplete(h1);
1657 >        checkIncomplete(h2);
1658 >        assertTrue(complete2.call());
1659 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1660 >
1661 >        checkCompletedWithWrappedException(h1, ex);
1662 >        checkCompletedWithWrappedException(h2, ex);
1663 >        checkCompletedWithWrappedException(h3, ex);
1664 >        r1.assertNotInvoked();
1665 >        r2.assertNotInvoked();
1666 >        r3.assertNotInvoked();
1667 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1668 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1669 >    }}
1670  
1671      /**
1672 <     * applyToEitherAsync result completes exceptionally if action does
1672 >     * thenCombine result completes exceptionally if either source cancelled
1673       */
1674 <    public void testApplyToEitherAsync3() {
1675 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1676 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1677 <        FailingFunction r = new FailingFunction();
1678 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r);
1679 <        f.complete(one);
1680 <        checkCompletedWithWrappedCFException(g);
1681 <    }
1674 >    public void testThenCombine_sourceCancelled() throws Throwable {
1675 >        for (ExecutionMode m : ExecutionMode.values())
1676 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1677 >        for (boolean fFirst : new boolean[] { true, false })
1678 >        for (boolean failFirst : new boolean[] { true, false })
1679 >        for (Integer v1 : new Integer[] { 1, null })
1680 >    {
1681 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1682 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1683 >        final SubtractFunction r1 = new SubtractFunction(m);
1684 >        final SubtractFunction r2 = new SubtractFunction(m);
1685 >        final SubtractFunction r3 = new SubtractFunction(m);
1686 >
1687 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1688 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1689 >        final Callable<Boolean> complete1 = failFirst ?
1690 >            () -> fst.cancel(mayInterruptIfRunning) :
1691 >            () -> fst.complete(v1);
1692 >        final Callable<Boolean> complete2 = failFirst ?
1693 >            () -> snd.complete(v1) :
1694 >            () -> snd.cancel(mayInterruptIfRunning);
1695 >
1696 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1697 >        assertTrue(complete1.call());
1698 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1699 >        checkIncomplete(h1);
1700 >        checkIncomplete(h2);
1701 >        assertTrue(complete2.call());
1702 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1703 >
1704 >        checkCompletedWithWrappedCancellationException(h1);
1705 >        checkCompletedWithWrappedCancellationException(h2);
1706 >        checkCompletedWithWrappedCancellationException(h3);
1707 >        r1.assertNotInvoked();
1708 >        r2.assertNotInvoked();
1709 >        r3.assertNotInvoked();
1710 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1711 >        checkCancelled(failFirst ? fst : snd);
1712 >    }}
1713  
1714      /**
1715 <     * applyToEitherAsync result completes exceptionally if either source cancelled
1715 >     * thenCombine result completes exceptionally if action does
1716       */
1717 <    public void testApplyToEitherAsync4() {
1718 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1719 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1720 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1721 <        assertTrue(f.cancel(true));
1722 <        checkCompletedWithWrappedCancellationException(g);
1723 <
1724 <        f = new CompletableFuture<>();
1725 <        f2 = new CompletableFuture<>();
1726 <        assertTrue(f2.cancel(true));
1727 <        g = f.applyToEitherAsync(f2, inc);
1728 <        checkCompletedWithWrappedCancellationException(g);
1729 <    }
1717 >    public void testThenCombine_actionFailed() {
1718 >        for (ExecutionMode m : ExecutionMode.values())
1719 >        for (boolean fFirst : new boolean[] { true, false })
1720 >        for (Integer v1 : new Integer[] { 1, null })
1721 >        for (Integer v2 : new Integer[] { 2, null })
1722 >    {
1723 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1724 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1725 >        final FailingBiFunction r1 = new FailingBiFunction(m);
1726 >        final FailingBiFunction r2 = new FailingBiFunction(m);
1727 >        final FailingBiFunction r3 = new FailingBiFunction(m);
1728 >
1729 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1730 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1731 >        final Integer w1 =  fFirst ? v1 : v2;
1732 >        final Integer w2 = !fFirst ? v1 : v2;
1733 >
1734 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1735 >        assertTrue(fst.complete(w1));
1736 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1737 >        assertTrue(snd.complete(w2));
1738 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1739 >
1740 >        checkCompletedWithWrappedCFException(h1);
1741 >        checkCompletedWithWrappedCFException(h2);
1742 >        checkCompletedWithWrappedCFException(h3);
1743 >        r1.assertInvoked();
1744 >        r2.assertInvoked();
1745 >        r3.assertInvoked();
1746 >        checkCompletedNormally(f, v1);
1747 >        checkCompletedNormally(g, v2);
1748 >    }}
1749  
1750      /**
1751 <     * acceptEitherAsync result completes normally after normal
1751 >     * thenAcceptBoth result completes normally after normal
1752       * completion of sources
1753       */
1754 <    public void testAcceptEitherAsync() {
1755 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1756 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1757 <        IncAction r = new IncAction();
1758 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1759 <        f.complete(one);
1760 <        checkCompletedNormally(g, null);
1761 <        assertEquals(r.value, 2);
1762 <
1763 <        r = new IncAction();
1764 <        f = new CompletableFuture<>();
1765 <        f.complete(one);
1766 <        f2 = new CompletableFuture<>();
1767 <        g = f.acceptEitherAsync(f2, r);
1768 <        checkCompletedNormally(g, null);
1769 <        assertEquals(r.value, 2);
1770 <    }
1754 >    public void testThenAcceptBoth_normalCompletion() {
1755 >        for (ExecutionMode m : ExecutionMode.values())
1756 >        for (boolean fFirst : new boolean[] { true, false })
1757 >        for (Integer v1 : new Integer[] { 1, null })
1758 >        for (Integer v2 : new Integer[] { 2, null })
1759 >    {
1760 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1761 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1762 >        final SubtractAction r1 = new SubtractAction(m);
1763 >        final SubtractAction r2 = new SubtractAction(m);
1764 >        final SubtractAction r3 = new SubtractAction(m);
1765 >
1766 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1767 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1768 >        final Integer w1 =  fFirst ? v1 : v2;
1769 >        final Integer w2 = !fFirst ? v1 : v2;
1770 >
1771 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1772 >        assertTrue(fst.complete(w1));
1773 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1774 >        checkIncomplete(h1);
1775 >        checkIncomplete(h2);
1776 >        r1.assertNotInvoked();
1777 >        r2.assertNotInvoked();
1778 >        assertTrue(snd.complete(w2));
1779 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1780 >
1781 >        checkCompletedNormally(h1, null);
1782 >        checkCompletedNormally(h2, null);
1783 >        checkCompletedNormally(h3, null);
1784 >        r1.assertValue(subtract(v1, v2));
1785 >        r2.assertValue(subtract(v1, v2));
1786 >        r3.assertValue(subtract(v1, v2));
1787 >        checkCompletedNormally(f, v1);
1788 >        checkCompletedNormally(g, v2);
1789 >    }}
1790  
1791      /**
1792 <     * acceptEitherAsync result completes exceptionally after exceptional
1793 <     * completion of source
1792 >     * thenAcceptBoth result completes exceptionally after exceptional
1793 >     * completion of either source
1794       */
1795 <    public void testAcceptEitherAsync2() {
1796 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1797 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1798 <        IncAction r = new IncAction();
1799 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1800 <        f.completeExceptionally(new CFException());
1801 <        checkCompletedWithWrappedCFException(g);
1802 <
1803 <        r = new IncAction();
1804 <        f = new CompletableFuture<>();
1805 <        f2 = new CompletableFuture<>();
1806 <        f2.completeExceptionally(new CFException());
1807 <        g = f.acceptEitherAsync(f2, r);
1808 <        f.complete(one);
1809 <        checkCompletedWithWrappedCFException(g);
1810 <    }
1795 >    public void testThenAcceptBoth_exceptionalCompletion() throws Throwable {
1796 >        for (ExecutionMode m : ExecutionMode.values())
1797 >        for (boolean fFirst : new boolean[] { true, false })
1798 >        for (boolean failFirst : new boolean[] { true, false })
1799 >        for (Integer v1 : new Integer[] { 1, null })
1800 >    {
1801 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1802 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1803 >        final CFException ex = new CFException();
1804 >        final SubtractAction r1 = new SubtractAction(m);
1805 >        final SubtractAction r2 = new SubtractAction(m);
1806 >        final SubtractAction r3 = new SubtractAction(m);
1807 >
1808 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1809 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1810 >        final Callable<Boolean> complete1 = failFirst ?
1811 >            () -> fst.completeExceptionally(ex) :
1812 >            () -> fst.complete(v1);
1813 >        final Callable<Boolean> complete2 = failFirst ?
1814 >            () -> snd.complete(v1) :
1815 >            () -> snd.completeExceptionally(ex);
1816 >
1817 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1818 >        assertTrue(complete1.call());
1819 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1820 >        checkIncomplete(h1);
1821 >        checkIncomplete(h2);
1822 >        assertTrue(complete2.call());
1823 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1824 >
1825 >        checkCompletedWithWrappedException(h1, ex);
1826 >        checkCompletedWithWrappedException(h2, ex);
1827 >        checkCompletedWithWrappedException(h3, ex);
1828 >        r1.assertNotInvoked();
1829 >        r2.assertNotInvoked();
1830 >        r3.assertNotInvoked();
1831 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1832 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1833 >    }}
1834  
1835      /**
1836 <     * acceptEitherAsync result completes exceptionally if action does
1836 >     * thenAcceptBoth result completes exceptionally if either source cancelled
1837       */
1838 <    public void testAcceptEitherAsync3() {
1839 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1840 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1841 <        FailingConsumer r = new FailingConsumer();
1842 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1843 <        f.complete(one);
1844 <        checkCompletedWithWrappedCFException(g);
1845 <    }
1838 >    public void testThenAcceptBoth_sourceCancelled() throws Throwable {
1839 >        for (ExecutionMode m : ExecutionMode.values())
1840 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1841 >        for (boolean fFirst : new boolean[] { true, false })
1842 >        for (boolean failFirst : new boolean[] { true, false })
1843 >        for (Integer v1 : new Integer[] { 1, null })
1844 >    {
1845 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1846 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1847 >        final SubtractAction r1 = new SubtractAction(m);
1848 >        final SubtractAction r2 = new SubtractAction(m);
1849 >        final SubtractAction r3 = new SubtractAction(m);
1850 >
1851 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1852 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1853 >        final Callable<Boolean> complete1 = failFirst ?
1854 >            () -> fst.cancel(mayInterruptIfRunning) :
1855 >            () -> fst.complete(v1);
1856 >        final Callable<Boolean> complete2 = failFirst ?
1857 >            () -> snd.complete(v1) :
1858 >            () -> snd.cancel(mayInterruptIfRunning);
1859 >
1860 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1861 >        assertTrue(complete1.call());
1862 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1863 >        checkIncomplete(h1);
1864 >        checkIncomplete(h2);
1865 >        assertTrue(complete2.call());
1866 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1867 >
1868 >        checkCompletedWithWrappedCancellationException(h1);
1869 >        checkCompletedWithWrappedCancellationException(h2);
1870 >        checkCompletedWithWrappedCancellationException(h3);
1871 >        r1.assertNotInvoked();
1872 >        r2.assertNotInvoked();
1873 >        r3.assertNotInvoked();
1874 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1875 >        checkCancelled(failFirst ? fst : snd);
1876 >    }}
1877  
1878      /**
1879 <     * acceptEitherAsync result completes exceptionally if either
2121 <     * source cancelled
1879 >     * thenAcceptBoth result completes exceptionally if action does
1880       */
1881 <    public void testAcceptEitherAsync4() {
1882 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1883 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1884 <        IncAction r = new IncAction();
1885 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1886 <        assertTrue(f.cancel(true));
1887 <        checkCompletedWithWrappedCancellationException(g);
1888 <
1889 <        r = new IncAction();
1890 <        f = new CompletableFuture<>();
1891 <        f2 = new CompletableFuture<>();
1892 <        assertTrue(f2.cancel(true));
1893 <        g = f.acceptEitherAsync(f2, r);
1894 <        checkCompletedWithWrappedCancellationException(g);
1895 <    }
1881 >    public void testThenAcceptBoth_actionFailed() {
1882 >        for (ExecutionMode m : ExecutionMode.values())
1883 >        for (boolean fFirst : new boolean[] { true, false })
1884 >        for (Integer v1 : new Integer[] { 1, null })
1885 >        for (Integer v2 : new Integer[] { 2, null })
1886 >    {
1887 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1888 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1889 >        final FailingBiConsumer r1 = new FailingBiConsumer(m);
1890 >        final FailingBiConsumer r2 = new FailingBiConsumer(m);
1891 >        final FailingBiConsumer r3 = new FailingBiConsumer(m);
1892 >
1893 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1894 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1895 >        final Integer w1 =  fFirst ? v1 : v2;
1896 >        final Integer w2 = !fFirst ? v1 : v2;
1897 >
1898 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1899 >        assertTrue(fst.complete(w1));
1900 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1901 >        assertTrue(snd.complete(w2));
1902 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1903 >
1904 >        checkCompletedWithWrappedCFException(h1);
1905 >        checkCompletedWithWrappedCFException(h2);
1906 >        checkCompletedWithWrappedCFException(h3);
1907 >        r1.assertInvoked();
1908 >        r2.assertInvoked();
1909 >        r3.assertInvoked();
1910 >        checkCompletedNormally(f, v1);
1911 >        checkCompletedNormally(g, v2);
1912 >    }}
1913  
1914      /**
1915 <     * runAfterEitherAsync result completes normally after normal
1915 >     * runAfterBoth result completes normally after normal
1916       * completion of sources
1917       */
1918 <    public void testRunAfterEitherAsync() {
1919 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1920 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1921 <        Noop r = new Noop();
1922 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1923 <        f.complete(one);
1924 <        checkCompletedNormally(g, null);
1925 <        assertTrue(r.ran);
1926 <
1927 <        r = new Noop();
1928 <        f = new CompletableFuture<>();
1929 <        f.complete(one);
1930 <        f2 = new CompletableFuture<>();
1931 <        g = f.runAfterEitherAsync(f2, r);
1932 <        checkCompletedNormally(g, null);
1933 <        assertTrue(r.ran);
1934 <    }
1918 >    public void testRunAfterBoth_normalCompletion() {
1919 >        for (ExecutionMode m : ExecutionMode.values())
1920 >        for (boolean fFirst : new boolean[] { true, false })
1921 >        for (Integer v1 : new Integer[] { 1, null })
1922 >        for (Integer v2 : new Integer[] { 2, null })
1923 >    {
1924 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1925 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1926 >        final Noop r1 = new Noop(m);
1927 >        final Noop r2 = new Noop(m);
1928 >        final Noop r3 = new Noop(m);
1929 >
1930 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1931 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1932 >        final Integer w1 =  fFirst ? v1 : v2;
1933 >        final Integer w2 = !fFirst ? v1 : v2;
1934 >
1935 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1936 >        assertTrue(fst.complete(w1));
1937 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1938 >        checkIncomplete(h1);
1939 >        checkIncomplete(h2);
1940 >        r1.assertNotInvoked();
1941 >        r2.assertNotInvoked();
1942 >        assertTrue(snd.complete(w2));
1943 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
1944 >
1945 >        checkCompletedNormally(h1, null);
1946 >        checkCompletedNormally(h2, null);
1947 >        checkCompletedNormally(h3, null);
1948 >        r1.assertInvoked();
1949 >        r2.assertInvoked();
1950 >        r3.assertInvoked();
1951 >        checkCompletedNormally(f, v1);
1952 >        checkCompletedNormally(g, v2);
1953 >    }}
1954  
1955      /**
1956 <     * runAfterEitherAsync result completes exceptionally after exceptional
1957 <     * completion of source
1956 >     * runAfterBoth result completes exceptionally after exceptional
1957 >     * completion of either source
1958       */
1959 <    public void testRunAfterEitherAsync2() {
1960 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1961 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1962 <        Noop r = new Noop();
1963 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1964 <        f.completeExceptionally(new CFException());
1965 <        checkCompletedWithWrappedCFException(g);
1966 <
1967 <        r = new Noop();
1968 <        f = new CompletableFuture<>();
1969 <        f2 = new CompletableFuture<>();
1970 <        f2.completeExceptionally(new CFException());
1971 <        g = f.runAfterEitherAsync(f2, r);
1972 <        f.complete(one);
1973 <        checkCompletedWithWrappedCFException(g);
1974 <    }
1959 >    public void testRunAfterBoth_exceptionalCompletion() throws Throwable {
1960 >        for (ExecutionMode m : ExecutionMode.values())
1961 >        for (boolean fFirst : new boolean[] { true, false })
1962 >        for (boolean failFirst : new boolean[] { true, false })
1963 >        for (Integer v1 : new Integer[] { 1, null })
1964 >    {
1965 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1966 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1967 >        final CFException ex = new CFException();
1968 >        final Noop r1 = new Noop(m);
1969 >        final Noop r2 = new Noop(m);
1970 >        final Noop r3 = new Noop(m);
1971 >
1972 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1973 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1974 >        final Callable<Boolean> complete1 = failFirst ?
1975 >            () -> fst.completeExceptionally(ex) :
1976 >            () -> fst.complete(v1);
1977 >        final Callable<Boolean> complete2 = failFirst ?
1978 >            () -> snd.complete(v1) :
1979 >            () -> snd.completeExceptionally(ex);
1980 >
1981 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1982 >        assertTrue(complete1.call());
1983 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1984 >        checkIncomplete(h1);
1985 >        checkIncomplete(h2);
1986 >        assertTrue(complete2.call());
1987 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
1988 >
1989 >        checkCompletedWithWrappedException(h1, ex);
1990 >        checkCompletedWithWrappedException(h2, ex);
1991 >        checkCompletedWithWrappedException(h3, ex);
1992 >        r1.assertNotInvoked();
1993 >        r2.assertNotInvoked();
1994 >        r3.assertNotInvoked();
1995 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1996 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1997 >    }}
1998  
1999      /**
2000 <     * runAfterEitherAsync result completes exceptionally if action does
2000 >     * runAfterBoth result completes exceptionally if either source cancelled
2001       */
2002 <    public void testRunAfterEitherAsync3() {
2003 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2004 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2005 <        FailingNoop r = new FailingNoop();
2006 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2007 <        f.complete(one);
2008 <        checkCompletedWithWrappedCFException(g);
2009 <    }
2002 >    public void testRunAfterBoth_sourceCancelled() throws Throwable {
2003 >        for (ExecutionMode m : ExecutionMode.values())
2004 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2005 >        for (boolean fFirst : new boolean[] { true, false })
2006 >        for (boolean failFirst : new boolean[] { true, false })
2007 >        for (Integer v1 : new Integer[] { 1, null })
2008 >    {
2009 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2010 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2011 >        final Noop r1 = new Noop(m);
2012 >        final Noop r2 = new Noop(m);
2013 >        final Noop r3 = new Noop(m);
2014 >
2015 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2016 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2017 >        final Callable<Boolean> complete1 = failFirst ?
2018 >            () -> fst.cancel(mayInterruptIfRunning) :
2019 >            () -> fst.complete(v1);
2020 >        final Callable<Boolean> complete2 = failFirst ?
2021 >            () -> snd.complete(v1) :
2022 >            () -> snd.cancel(mayInterruptIfRunning);
2023 >
2024 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2025 >        assertTrue(complete1.call());
2026 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2027 >        checkIncomplete(h1);
2028 >        checkIncomplete(h2);
2029 >        assertTrue(complete2.call());
2030 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2031 >
2032 >        checkCompletedWithWrappedCancellationException(h1);
2033 >        checkCompletedWithWrappedCancellationException(h2);
2034 >        checkCompletedWithWrappedCancellationException(h3);
2035 >        r1.assertNotInvoked();
2036 >        r2.assertNotInvoked();
2037 >        r3.assertNotInvoked();
2038 >        checkCompletedNormally(failFirst ? snd : fst, v1);
2039 >        checkCancelled(failFirst ? fst : snd);
2040 >    }}
2041  
2042      /**
2043 <     * runAfterEitherAsync result completes exceptionally if either
2196 <     * source cancelled
2043 >     * runAfterBoth result completes exceptionally if action does
2044       */
2045 <    public void testRunAfterEitherAsync4() {
2046 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2047 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2048 <        Noop r = new Noop();
2049 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2050 <        assertTrue(f.cancel(true));
2051 <        checkCompletedWithWrappedCancellationException(g);
2052 <
2053 <        r = new Noop();
2054 <        f = new CompletableFuture<>();
2055 <        f2 = new CompletableFuture<>();
2056 <        assertTrue(f2.cancel(true));
2057 <        g = f.runAfterEitherAsync(f2, r);
2058 <        checkCompletedWithWrappedCancellationException(g);
2059 <    }
2045 >    public void testRunAfterBoth_actionFailed() {
2046 >        for (ExecutionMode m : ExecutionMode.values())
2047 >        for (boolean fFirst : new boolean[] { true, false })
2048 >        for (Integer v1 : new Integer[] { 1, null })
2049 >        for (Integer v2 : new Integer[] { 2, null })
2050 >    {
2051 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2052 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2053 >        final FailingRunnable r1 = new FailingRunnable(m);
2054 >        final FailingRunnable r2 = new FailingRunnable(m);
2055 >        final FailingRunnable r3 = new FailingRunnable(m);
2056 >
2057 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2058 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2059 >        final Integer w1 =  fFirst ? v1 : v2;
2060 >        final Integer w2 = !fFirst ? v1 : v2;
2061 >
2062 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2063 >        assertTrue(fst.complete(w1));
2064 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2065 >        assertTrue(snd.complete(w2));
2066 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2067 >
2068 >        checkCompletedWithWrappedCFException(h1);
2069 >        checkCompletedWithWrappedCFException(h2);
2070 >        checkCompletedWithWrappedCFException(h3);
2071 >        r1.assertInvoked();
2072 >        r2.assertInvoked();
2073 >        r3.assertInvoked();
2074 >        checkCompletedNormally(f, v1);
2075 >        checkCompletedNormally(g, v2);
2076 >    }}
2077  
2078      /**
2079 <     * thenComposeAsync result completes normally after normal
2080 <     * completion of source
2079 >     * applyToEither result completes normally after normal completion
2080 >     * of either source
2081       */
2082 <    public void testThenComposeAsync() {
2083 <        CompletableFuture<Integer> f, g;
2084 <        CompletableFutureInc r;
2085 <
2086 <        f = new CompletableFuture<>();
2087 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2088 <        f.complete(one);
2089 <        checkCompletedNormally(g, two);
2090 <
2227 <        f = new CompletableFuture<>();
2228 <        f.complete(one);
2229 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2230 <        checkCompletedNormally(g, two);
2231 <    }
2082 >    public void testApplyToEither_normalCompletion() {
2083 >        for (ExecutionMode m : ExecutionMode.values())
2084 >        for (Integer v1 : new Integer[] { 1, null })
2085 >        for (Integer v2 : new Integer[] { 2, null })
2086 >    {
2087 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2088 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2089 >        final IncFunction[] rs = new IncFunction[6];
2090 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2091  
2092 <    /**
2093 <     * thenComposeAsync result completes exceptionally after
2094 <     * exceptional completion of source
2095 <     */
2096 <    public void testThenComposeAsync2() {
2097 <        CompletableFuture<Integer> f, g;
2098 <        CompletableFutureInc r;
2092 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2093 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2094 >        checkIncomplete(h0);
2095 >        checkIncomplete(h1);
2096 >        rs[0].assertNotInvoked();
2097 >        rs[1].assertNotInvoked();
2098 >        f.complete(v1);
2099 >        checkCompletedNormally(h0, inc(v1));
2100 >        checkCompletedNormally(h1, inc(v1));
2101 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2102 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2103 >        checkCompletedNormally(h2, inc(v1));
2104 >        checkCompletedNormally(h3, inc(v1));
2105 >        g.complete(v2);
2106  
2107 <        f = new CompletableFuture<>();
2108 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2109 <        f.completeExceptionally(new CFException());
2110 <        checkCompletedWithWrappedCFException(g);
2111 <        assertFalse(r.ran);
2107 >        // unspecified behavior - both source completions available
2108 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2109 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2110 >        rs[4].assertValue(h4.join());
2111 >        rs[5].assertValue(h5.join());
2112 >        assertTrue(Objects.equals(inc(v1), h4.join()) ||
2113 >                   Objects.equals(inc(v2), h4.join()));
2114 >        assertTrue(Objects.equals(inc(v1), h5.join()) ||
2115 >                   Objects.equals(inc(v2), h5.join()));
2116  
2117 <        f = new CompletableFuture<>();
2118 <        f.completeExceptionally(new CFException());
2119 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2120 <        checkCompletedWithWrappedCFException(g);
2121 <        assertFalse(r.ran);
2122 <    }
2117 >        checkCompletedNormally(f, v1);
2118 >        checkCompletedNormally(g, v2);
2119 >        checkCompletedNormally(h0, inc(v1));
2120 >        checkCompletedNormally(h1, inc(v1));
2121 >        checkCompletedNormally(h2, inc(v1));
2122 >        checkCompletedNormally(h3, inc(v1));
2123 >        for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1));
2124 >    }}
2125  
2126      /**
2127 <     * thenComposeAsync result completes exceptionally if action does
2127 >     * applyToEither result completes exceptionally after exceptional
2128 >     * completion of either source
2129       */
2130 <    public void testThenComposeAsync3() {
2131 <        CompletableFuture<Integer> f, g;
2132 <        FailingCompletableFutureFunction r;
2133 <
2134 <        f = new CompletableFuture<>();
2135 <        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
2136 <        f.complete(one);
2137 <        checkCompletedWithWrappedCFException(g);
2130 >    public void testApplyToEither_exceptionalCompletion() {
2131 >        for (ExecutionMode m : ExecutionMode.values())
2132 >        for (Integer v1 : new Integer[] { 1, null })
2133 >    {
2134 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2135 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2136 >        final CFException ex = new CFException();
2137 >        final IncFunction[] rs = new IncFunction[6];
2138 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2139  
2140 <        f = new CompletableFuture<>();
2141 <        f.complete(one);
2142 <        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
2143 <        checkCompletedWithWrappedCFException(g);
2144 <    }
2140 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2141 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2142 >        checkIncomplete(h0);
2143 >        checkIncomplete(h1);
2144 >        rs[0].assertNotInvoked();
2145 >        rs[1].assertNotInvoked();
2146 >        f.completeExceptionally(ex);
2147 >        checkCompletedWithWrappedException(h0, ex);
2148 >        checkCompletedWithWrappedException(h1, ex);
2149 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2150 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2151 >        checkCompletedWithWrappedException(h2, ex);
2152 >        checkCompletedWithWrappedException(h3, ex);
2153 >        g.complete(v1);
2154  
2155 <    /**
2156 <     * thenComposeAsync result completes exceptionally if source cancelled
2157 <     */
2158 <    public void testThenComposeAsync4() {
2159 <        CompletableFuture<Integer> f, g;
2160 <        CompletableFutureInc r;
2155 >        // unspecified behavior - both source completions available
2156 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2157 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2158 >        try {
2159 >            assertEquals(inc(v1), h4.join());
2160 >            rs[4].assertValue(inc(v1));
2161 >        } catch (CompletionException ok) {
2162 >            checkCompletedWithWrappedException(h4, ex);
2163 >            rs[4].assertNotInvoked();
2164 >        }
2165 >        try {
2166 >            assertEquals(inc(v1), h5.join());
2167 >            rs[5].assertValue(inc(v1));
2168 >        } catch (CompletionException ok) {
2169 >            checkCompletedWithWrappedException(h5, ex);
2170 >            rs[5].assertNotInvoked();
2171 >        }
2172  
2173 <        f = new CompletableFuture<>();
2174 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2175 <        assertTrue(f.cancel(true));
2176 <        checkCompletedWithWrappedCancellationException(g);
2173 >        checkCompletedExceptionally(f, ex);
2174 >        checkCompletedNormally(g, v1);
2175 >        checkCompletedWithWrappedException(h0, ex);
2176 >        checkCompletedWithWrappedException(h1, ex);
2177 >        checkCompletedWithWrappedException(h2, ex);
2178 >        checkCompletedWithWrappedException(h3, ex);
2179 >        checkCompletedWithWrappedException(h4, ex);
2180 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2181 >    }}
2182 >
2183 >    public void testApplyToEither_exceptionalCompletion2() {
2184 >        for (ExecutionMode m : ExecutionMode.values())
2185 >        for (boolean fFirst : new boolean[] { true, false })
2186 >        for (Integer v1 : new Integer[] { 1, null })
2187 >    {
2188 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2189 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2190 >        final CFException ex = new CFException();
2191 >        final IncFunction[] rs = new IncFunction[6];
2192 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2193  
2194 <        f = new CompletableFuture<>();
2195 <        assertTrue(f.cancel(true));
2196 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2197 <        checkCompletedWithWrappedCancellationException(g);
2198 <    }
2194 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2195 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2196 >        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2197 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2198 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2199 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2200 >
2201 >        // unspecified behavior - both source completions available
2202 >        try {
2203 >            assertEquals(inc(v1), h0.join());
2204 >            rs[0].assertValue(inc(v1));
2205 >        } catch (CompletionException ok) {
2206 >            checkCompletedWithWrappedException(h0, ex);
2207 >            rs[0].assertNotInvoked();
2208 >        }
2209 >        try {
2210 >            assertEquals(inc(v1), h1.join());
2211 >            rs[1].assertValue(inc(v1));
2212 >        } catch (CompletionException ok) {
2213 >            checkCompletedWithWrappedException(h1, ex);
2214 >            rs[1].assertNotInvoked();
2215 >        }
2216 >        try {
2217 >            assertEquals(inc(v1), h2.join());
2218 >            rs[2].assertValue(inc(v1));
2219 >        } catch (CompletionException ok) {
2220 >            checkCompletedWithWrappedException(h2, ex);
2221 >            rs[2].assertNotInvoked();
2222 >        }
2223 >        try {
2224 >            assertEquals(inc(v1), h3.join());
2225 >            rs[3].assertValue(inc(v1));
2226 >        } catch (CompletionException ok) {
2227 >            checkCompletedWithWrappedException(h3, ex);
2228 >            rs[3].assertNotInvoked();
2229 >        }
2230  
2231 <    // async with explicit executors
2231 >        checkCompletedNormally(f, v1);
2232 >        checkCompletedExceptionally(g, ex);
2233 >    }}
2234  
2235      /**
2236 <     * thenRunAsync result completes normally after normal completion of source
2236 >     * applyToEither result completes exceptionally if either source cancelled
2237       */
2238 <    public void testThenRunAsyncE() {
2239 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2240 <        Noop r = new Noop();
2241 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2242 <        f.complete(null);
2243 <        checkCompletedNormally(g, null);
2244 <
2245 <        // reordered version
2246 <        f = new CompletableFuture<>();
2304 <        f.complete(null);
2305 <        r = new Noop();
2306 <        g = f.thenRunAsync(r, new ThreadExecutor());
2307 <        checkCompletedNormally(g, null);
2308 <    }
2238 >    public void testApplyToEither_sourceCancelled() {
2239 >        for (ExecutionMode m : ExecutionMode.values())
2240 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2241 >        for (Integer v1 : new Integer[] { 1, null })
2242 >    {
2243 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2244 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2245 >        final IncFunction[] rs = new IncFunction[6];
2246 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2247  
2248 <    /**
2249 <     * thenRunAsync result completes exceptionally after exceptional
2250 <     * completion of source
2251 <     */
2252 <    public void testThenRunAsync2E() {
2253 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2254 <        Noop r = new Noop();
2255 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2256 <        f.completeExceptionally(new CFException());
2257 <        try {
2258 <            g.join();
2259 <            shouldThrow();
2260 <        } catch (CompletionException success) {}
2261 <        checkCompletedWithWrappedCFException(g);
2324 <    }
2248 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2249 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2250 >        checkIncomplete(h0);
2251 >        checkIncomplete(h1);
2252 >        rs[0].assertNotInvoked();
2253 >        rs[1].assertNotInvoked();
2254 >        f.cancel(mayInterruptIfRunning);
2255 >        checkCompletedWithWrappedCancellationException(h0);
2256 >        checkCompletedWithWrappedCancellationException(h1);
2257 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2258 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2259 >        checkCompletedWithWrappedCancellationException(h2);
2260 >        checkCompletedWithWrappedCancellationException(h3);
2261 >        g.complete(v1);
2262  
2263 <    /**
2264 <     * thenRunAsync result completes exceptionally if action does
2265 <     */
2266 <    public void testThenRunAsync3E() {
2267 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2268 <        FailingNoop r = new FailingNoop();
2269 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2270 <        f.complete(null);
2271 <        checkCompletedWithWrappedCFException(g);
2272 <    }
2263 >        // unspecified behavior - both source completions available
2264 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2265 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2266 >        try {
2267 >            assertEquals(inc(v1), h4.join());
2268 >            rs[4].assertValue(inc(v1));
2269 >        } catch (CompletionException ok) {
2270 >            checkCompletedWithWrappedCancellationException(h4);
2271 >            rs[4].assertNotInvoked();
2272 >        }
2273 >        try {
2274 >            assertEquals(inc(v1), h5.join());
2275 >            rs[5].assertValue(inc(v1));
2276 >        } catch (CompletionException ok) {
2277 >            checkCompletedWithWrappedCancellationException(h5);
2278 >            rs[5].assertNotInvoked();
2279 >        }
2280  
2281 <    /**
2282 <     * thenRunAsync result completes exceptionally if source cancelled
2283 <     */
2284 <    public void testThenRunAsync4E() {
2285 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2286 <        Noop r = new Noop();
2287 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2288 <        assertTrue(f.cancel(true));
2345 <        checkCompletedWithWrappedCancellationException(g);
2346 <    }
2281 >        checkCancelled(f);
2282 >        checkCompletedNormally(g, v1);
2283 >        checkCompletedWithWrappedCancellationException(h0);
2284 >        checkCompletedWithWrappedCancellationException(h1);
2285 >        checkCompletedWithWrappedCancellationException(h2);
2286 >        checkCompletedWithWrappedCancellationException(h3);
2287 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2288 >    }}
2289  
2290 <    /**
2291 <     * thenApplyAsync result completes normally after normal completion of source
2292 <     */
2293 <    public void testThenApplyAsyncE() {
2294 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2295 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
2296 <        f.complete(one);
2297 <        checkCompletedNormally(g, two);
2298 <    }
2290 >    public void testApplyToEither_sourceCancelled2() {
2291 >        for (ExecutionMode m : ExecutionMode.values())
2292 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2293 >        for (boolean fFirst : new boolean[] { true, false })
2294 >        for (Integer v1 : new Integer[] { 1, null })
2295 >    {
2296 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2297 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2298 >        final IncFunction[] rs = new IncFunction[6];
2299 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2300  
2301 <    /**
2302 <     * thenApplyAsync result completes exceptionally after exceptional
2303 <     * completion of source
2304 <     */
2305 <    public void testThenApplyAsync2E() {
2306 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2307 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
2308 <        f.completeExceptionally(new CFException());
2309 <        checkCompletedWithWrappedCFException(g);
2310 <    }
2301 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2302 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2303 >        assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2304 >        assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2305 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2306 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2307 >
2308 >        // unspecified behavior - both source completions available
2309 >        try {
2310 >            assertEquals(inc(v1), h0.join());
2311 >            rs[0].assertValue(inc(v1));
2312 >        } catch (CompletionException ok) {
2313 >            checkCompletedWithWrappedCancellationException(h0);
2314 >            rs[0].assertNotInvoked();
2315 >        }
2316 >        try {
2317 >            assertEquals(inc(v1), h1.join());
2318 >            rs[1].assertValue(inc(v1));
2319 >        } catch (CompletionException ok) {
2320 >            checkCompletedWithWrappedCancellationException(h1);
2321 >            rs[1].assertNotInvoked();
2322 >        }
2323 >        try {
2324 >            assertEquals(inc(v1), h2.join());
2325 >            rs[2].assertValue(inc(v1));
2326 >        } catch (CompletionException ok) {
2327 >            checkCompletedWithWrappedCancellationException(h2);
2328 >            rs[2].assertNotInvoked();
2329 >        }
2330 >        try {
2331 >            assertEquals(inc(v1), h3.join());
2332 >            rs[3].assertValue(inc(v1));
2333 >        } catch (CompletionException ok) {
2334 >            checkCompletedWithWrappedCancellationException(h3);
2335 >            rs[3].assertNotInvoked();
2336 >        }
2337  
2338 <    /**
2339 <     * thenApplyAsync result completes exceptionally if action does
2340 <     */
2372 <    public void testThenApplyAsync3E() {
2373 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2374 <        FailingFunction r = new FailingFunction();
2375 <        CompletableFuture<Integer> g = f.thenApplyAsync(r, new ThreadExecutor());
2376 <        f.complete(null);
2377 <        checkCompletedWithWrappedCFException(g);
2378 <    }
2338 >        checkCompletedNormally(f, v1);
2339 >        checkCancelled(g);
2340 >    }}
2341  
2342      /**
2343 <     * thenApplyAsync result completes exceptionally if source cancelled
2343 >     * applyToEither result completes exceptionally if action does
2344       */
2345 <    public void testThenApplyAsync4E() {
2346 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2347 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
2348 <        assertTrue(f.cancel(true));
2349 <        checkCompletedWithWrappedCancellationException(g);
2350 <    }
2345 >    public void testApplyToEither_actionFailed() {
2346 >        for (ExecutionMode m : ExecutionMode.values())
2347 >        for (Integer v1 : new Integer[] { 1, null })
2348 >        for (Integer v2 : new Integer[] { 2, null })
2349 >    {
2350 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2351 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2352 >        final FailingFunction[] rs = new FailingFunction[6];
2353 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
2354  
2355 <    /**
2356 <     * thenAcceptAsync result completes normally after normal
2357 <     * completion of source
2358 <     */
2359 <    public void testThenAcceptAsyncE() {
2360 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2361 <        IncAction r = new IncAction();
2362 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2363 <        f.complete(one);
2364 <        checkCompletedNormally(g, null);
2400 <        assertEquals(r.value, 2);
2401 <    }
2355 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2356 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2357 >        f.complete(v1);
2358 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2359 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2360 >        checkCompletedWithWrappedCFException(h0);
2361 >        checkCompletedWithWrappedCFException(h1);
2362 >        checkCompletedWithWrappedCFException(h2);
2363 >        checkCompletedWithWrappedCFException(h3);
2364 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2365  
2366 <    /**
2404 <     * thenAcceptAsync result completes exceptionally after exceptional
2405 <     * completion of source
2406 <     */
2407 <    public void testThenAcceptAsync2E() {
2408 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2409 <        IncAction r = new IncAction();
2410 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2411 <        f.completeExceptionally(new CFException());
2412 <        checkCompletedWithWrappedCFException(g);
2413 <    }
2366 >        g.complete(v2);
2367  
2368 <    /**
2369 <     * thenAcceptAsync result completes exceptionally if action does
2370 <     */
2371 <    public void testThenAcceptAsync3E() {
2372 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2373 <        FailingConsumer r = new FailingConsumer();
2374 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2375 <        f.complete(null);
2376 <        checkCompletedWithWrappedCFException(g);
2377 <    }
2368 >        // unspecified behavior - both source completions available
2369 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2370 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2371 >
2372 >        checkCompletedWithWrappedCFException(h4);
2373 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2374 >                   Objects.equals(v2, rs[4].value));
2375 >        checkCompletedWithWrappedCFException(h5);
2376 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2377 >                   Objects.equals(v2, rs[5].value));
2378  
2379 <    /**
2380 <     * thenAcceptAsync result completes exceptionally if source cancelled
2381 <     */
2429 <    public void testThenAcceptAsync4E() {
2430 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2431 <        IncAction r = new IncAction();
2432 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2433 <        assertTrue(f.cancel(true));
2434 <        checkCompletedWithWrappedCancellationException(g);
2435 <    }
2379 >        checkCompletedNormally(f, v1);
2380 >        checkCompletedNormally(g, v2);
2381 >    }}
2382  
2383      /**
2384 <     * thenCombineAsync result completes normally after normal
2385 <     * completion of sources
2384 >     * acceptEither result completes normally after normal completion
2385 >     * of either source
2386       */
2387 <    public void testThenCombineAsyncE() {
2388 <        CompletableFuture<Integer> f, g, h;
2389 <        ThreadExecutor e = new ThreadExecutor();
2390 <        int count = 0;
2387 >    public void testAcceptEither_normalCompletion() {
2388 >        for (ExecutionMode m : ExecutionMode.values())
2389 >        for (Integer v1 : new Integer[] { 1, null })
2390 >        for (Integer v2 : new Integer[] { 2, null })
2391 >    {
2392 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2393 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2394 >        final NoopConsumer[] rs = new NoopConsumer[6];
2395 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2396  
2397 <        f = new CompletableFuture<>();
2398 <        g = new CompletableFuture<>();
2399 <        h = f.thenCombineAsync(g, subtract, e);
2400 <        f.complete(3);
2401 <        checkIncomplete(h);
2402 <        g.complete(1);
2403 <        checkCompletedNormally(h, 2);
2404 <        assertEquals(++count, e.count.get());
2397 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2398 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2399 >        checkIncomplete(h0);
2400 >        checkIncomplete(h1);
2401 >        rs[0].assertNotInvoked();
2402 >        rs[1].assertNotInvoked();
2403 >        f.complete(v1);
2404 >        checkCompletedNormally(h0, null);
2405 >        checkCompletedNormally(h1, null);
2406 >        rs[0].assertValue(v1);
2407 >        rs[1].assertValue(v1);
2408 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2409 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2410 >        checkCompletedNormally(h2, null);
2411 >        checkCompletedNormally(h3, null);
2412 >        rs[2].assertValue(v1);
2413 >        rs[3].assertValue(v1);
2414 >        g.complete(v2);
2415  
2416 <        f = new CompletableFuture<>();
2417 <        g = new CompletableFuture<>();
2418 <        h = f.thenCombineAsync(g, subtract, e);
2419 <        g.complete(1);
2420 <        checkIncomplete(h);
2421 <        f.complete(3);
2422 <        checkCompletedNormally(h, 2);
2423 <        assertEquals(++count, e.count.get());
2416 >        // unspecified behavior - both source completions available
2417 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2418 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2419 >        checkCompletedNormally(h4, null);
2420 >        checkCompletedNormally(h5, null);
2421 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2422 >                   Objects.equals(v2, rs[4].value));
2423 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2424 >                   Objects.equals(v2, rs[5].value));
2425  
2426 <        f = new CompletableFuture<>();
2427 <        g = new CompletableFuture<>();
2428 <        g.complete(1);
2429 <        f.complete(3);
2430 <        h = f.thenCombineAsync(g, subtract, e);
2431 <        checkCompletedNormally(h, 2);
2432 <        assertEquals(++count, e.count.get());
2433 <    }
2426 >        checkCompletedNormally(f, v1);
2427 >        checkCompletedNormally(g, v2);
2428 >        checkCompletedNormally(h0, null);
2429 >        checkCompletedNormally(h1, null);
2430 >        checkCompletedNormally(h2, null);
2431 >        checkCompletedNormally(h3, null);
2432 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2433 >    }}
2434  
2435      /**
2436 <     * thenCombineAsync result completes exceptionally after exceptional
2436 >     * acceptEither result completes exceptionally after exceptional
2437       * completion of either source
2438       */
2439 <    public void testThenCombineAsync2E() {
2440 <        CompletableFuture<Integer> f, g, h;
2441 <        ThreadExecutor e = new ThreadExecutor();
2442 <        int count = 0;
2443 <
2444 <        f = new CompletableFuture<>();
2445 <        g = new CompletableFuture<>();
2446 <        h = f.thenCombineAsync(g, subtract, e);
2447 <        f.completeExceptionally(new CFException());
2486 <        checkIncomplete(h);
2487 <        g.complete(1);
2488 <        checkCompletedWithWrappedCFException(h);
2489 <
2490 <        f = new CompletableFuture<>();
2491 <        g = new CompletableFuture<>();
2492 <        h = f.thenCombineAsync(g, subtract, e);
2493 <        g.completeExceptionally(new CFException());
2494 <        checkIncomplete(h);
2495 <        f.complete(3);
2496 <        checkCompletedWithWrappedCFException(h);
2497 <
2498 <        f = new CompletableFuture<>();
2499 <        g = new CompletableFuture<>();
2500 <        g.completeExceptionally(new CFException());
2501 <        h = f.thenCombineAsync(g, subtract, e);
2502 <        checkIncomplete(h);
2503 <        f.complete(3);
2504 <        checkCompletedWithWrappedCFException(h);
2505 <
2506 <        assertEquals(0, e.count.get());
2507 <    }
2508 <
2509 <    /**
2510 <     * thenCombineAsync result completes exceptionally if action does
2511 <     */
2512 <    public void testThenCombineAsync3E() {
2513 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2514 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2515 <        FailingBiFunction r = new FailingBiFunction();
2516 <        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r, new ThreadExecutor());
2517 <        f.complete(one);
2518 <        checkIncomplete(g);
2519 <        assertFalse(r.ran);
2520 <        f2.complete(two);
2521 <        checkCompletedWithWrappedCFException(g);
2522 <        assertTrue(r.ran);
2523 <    }
2439 >    public void testAcceptEither_exceptionalCompletion() {
2440 >        for (ExecutionMode m : ExecutionMode.values())
2441 >        for (Integer v1 : new Integer[] { 1, null })
2442 >    {
2443 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2444 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2445 >        final CFException ex = new CFException();
2446 >        final NoopConsumer[] rs = new NoopConsumer[6];
2447 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2448  
2449 <    /**
2450 <     * thenCombineAsync result completes exceptionally if either source cancelled
2451 <     */
2452 <    public void testThenCombineAsync4E() {
2453 <        CompletableFuture<Integer> f, g, h;
2454 <        ThreadExecutor e = new ThreadExecutor();
2449 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2450 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2451 >        checkIncomplete(h0);
2452 >        checkIncomplete(h1);
2453 >        rs[0].assertNotInvoked();
2454 >        rs[1].assertNotInvoked();
2455 >        f.completeExceptionally(ex);
2456 >        checkCompletedWithWrappedException(h0, ex);
2457 >        checkCompletedWithWrappedException(h1, ex);
2458 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2459 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2460 >        checkCompletedWithWrappedException(h2, ex);
2461 >        checkCompletedWithWrappedException(h3, ex);
2462  
2463 <        f = new CompletableFuture<>();
2533 <        g = new CompletableFuture<>();
2534 <        h = f.thenCombineAsync(g, subtract, e);
2535 <        assertTrue(f.cancel(true));
2536 <        checkIncomplete(h);
2537 <        g.complete(1);
2538 <        checkCompletedWithWrappedCancellationException(h);
2463 >        g.complete(v1);
2464  
2465 <        f = new CompletableFuture<>();
2466 <        g = new CompletableFuture<>();
2467 <        h = f.thenCombineAsync(g, subtract, e);
2468 <        assertTrue(g.cancel(true));
2469 <        checkIncomplete(h);
2470 <        f.complete(3);
2471 <        checkCompletedWithWrappedCancellationException(h);
2465 >        // unspecified behavior - both source completions available
2466 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2467 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2468 >        try {
2469 >            assertNull(h4.join());
2470 >            rs[4].assertValue(v1);
2471 >        } catch (CompletionException ok) {
2472 >            checkCompletedWithWrappedException(h4, ex);
2473 >            rs[4].assertNotInvoked();
2474 >        }
2475 >        try {
2476 >            assertNull(h5.join());
2477 >            rs[5].assertValue(v1);
2478 >        } catch (CompletionException ok) {
2479 >            checkCompletedWithWrappedException(h5, ex);
2480 >            rs[5].assertNotInvoked();
2481 >        }
2482  
2483 <        f = new CompletableFuture<>();
2484 <        g = new CompletableFuture<>();
2485 <        assertTrue(g.cancel(true));
2486 <        h = f.thenCombineAsync(g, subtract, e);
2487 <        checkIncomplete(h);
2488 <        f.complete(3);
2489 <        checkCompletedWithWrappedCancellationException(h);
2483 >        checkCompletedExceptionally(f, ex);
2484 >        checkCompletedNormally(g, v1);
2485 >        checkCompletedWithWrappedException(h0, ex);
2486 >        checkCompletedWithWrappedException(h1, ex);
2487 >        checkCompletedWithWrappedException(h2, ex);
2488 >        checkCompletedWithWrappedException(h3, ex);
2489 >        checkCompletedWithWrappedException(h4, ex);
2490 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2491 >    }}
2492 >
2493 >    public void testAcceptEither_exceptionalCompletion2() {
2494 >        for (ExecutionMode m : ExecutionMode.values())
2495 >        for (boolean fFirst : new boolean[] { true, false })
2496 >        for (Integer v1 : new Integer[] { 1, null })
2497 >    {
2498 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2499 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2500 >        final CFException ex = new CFException();
2501 >        final NoopConsumer[] rs = new NoopConsumer[6];
2502 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2503  
2504 <        f = new CompletableFuture<>();
2505 <        g = new CompletableFuture<>();
2506 <        assertTrue(f.cancel(true));
2507 <        assertTrue(g.cancel(true));
2508 <        h = f.thenCombineAsync(g, subtract, e);
2509 <        checkCompletedWithWrappedCancellationException(h);
2504 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2505 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2506 >        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2507 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2508 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2509 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2510 >
2511 >        // unspecified behavior - both source completions available
2512 >        try {
2513 >            assertEquals(null, h0.join());
2514 >            rs[0].assertValue(v1);
2515 >        } catch (CompletionException ok) {
2516 >            checkCompletedWithWrappedException(h0, ex);
2517 >            rs[0].assertNotInvoked();
2518 >        }
2519 >        try {
2520 >            assertEquals(null, h1.join());
2521 >            rs[1].assertValue(v1);
2522 >        } catch (CompletionException ok) {
2523 >            checkCompletedWithWrappedException(h1, ex);
2524 >            rs[1].assertNotInvoked();
2525 >        }
2526 >        try {
2527 >            assertEquals(null, h2.join());
2528 >            rs[2].assertValue(v1);
2529 >        } catch (CompletionException ok) {
2530 >            checkCompletedWithWrappedException(h2, ex);
2531 >            rs[2].assertNotInvoked();
2532 >        }
2533 >        try {
2534 >            assertEquals(null, h3.join());
2535 >            rs[3].assertValue(v1);
2536 >        } catch (CompletionException ok) {
2537 >            checkCompletedWithWrappedException(h3, ex);
2538 >            rs[3].assertNotInvoked();
2539 >        }
2540  
2541 <        assertEquals(0, e.count.get());
2542 <    }
2541 >        checkCompletedNormally(f, v1);
2542 >        checkCompletedExceptionally(g, ex);
2543 >    }}
2544  
2545      /**
2546 <     * thenAcceptBothAsync result completes normally after normal
2568 <     * completion of sources
2546 >     * acceptEither result completes exceptionally if either source cancelled
2547       */
2548 <    public void testThenAcceptBothAsyncE() {
2549 <        CompletableFuture<Integer> f, g;
2550 <        CompletableFuture<Void> h;
2551 <        SubtractAction r;
2552 <        ThreadExecutor e = new ThreadExecutor();
2548 >    public void testAcceptEither_sourceCancelled() {
2549 >        for (ExecutionMode m : ExecutionMode.values())
2550 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2551 >        for (Integer v1 : new Integer[] { 1, null })
2552 >    {
2553 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2554 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2555 >        final NoopConsumer[] rs = new NoopConsumer[6];
2556 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2557  
2558 <        f = new CompletableFuture<>();
2559 <        g = new CompletableFuture<>();
2560 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2561 <        f.complete(3);
2562 <        checkIncomplete(h);
2563 <        g.complete(1);
2564 <        checkCompletedNormally(h, null);
2565 <        assertEquals(r.value, 2);
2558 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2559 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2560 >        checkIncomplete(h0);
2561 >        checkIncomplete(h1);
2562 >        rs[0].assertNotInvoked();
2563 >        rs[1].assertNotInvoked();
2564 >        f.cancel(mayInterruptIfRunning);
2565 >        checkCompletedWithWrappedCancellationException(h0);
2566 >        checkCompletedWithWrappedCancellationException(h1);
2567 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2568 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2569 >        checkCompletedWithWrappedCancellationException(h2);
2570 >        checkCompletedWithWrappedCancellationException(h3);
2571  
2572 <        f = new CompletableFuture<>();
2586 <        g = new CompletableFuture<>();
2587 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2588 <        g.complete(1);
2589 <        checkIncomplete(h);
2590 <        f.complete(3);
2591 <        checkCompletedNormally(h, null);
2592 <        assertEquals(r.value, 2);
2572 >        g.complete(v1);
2573  
2574 <        f = new CompletableFuture<>();
2575 <        g = new CompletableFuture<>();
2576 <        g.complete(1);
2577 <        f.complete(3);
2578 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2579 <        checkCompletedNormally(h, null);
2580 <        assertEquals(r.value, 2);
2574 >        // unspecified behavior - both source completions available
2575 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2576 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2577 >        try {
2578 >            assertNull(h4.join());
2579 >            rs[4].assertValue(v1);
2580 >        } catch (CompletionException ok) {
2581 >            checkCompletedWithWrappedCancellationException(h4);
2582 >            rs[4].assertNotInvoked();
2583 >        }
2584 >        try {
2585 >            assertNull(h5.join());
2586 >            rs[5].assertValue(v1);
2587 >        } catch (CompletionException ok) {
2588 >            checkCompletedWithWrappedCancellationException(h5);
2589 >            rs[5].assertNotInvoked();
2590 >        }
2591  
2592 <        assertEquals(3, e.count.get());
2593 <    }
2592 >        checkCancelled(f);
2593 >        checkCompletedNormally(g, v1);
2594 >        checkCompletedWithWrappedCancellationException(h0);
2595 >        checkCompletedWithWrappedCancellationException(h1);
2596 >        checkCompletedWithWrappedCancellationException(h2);
2597 >        checkCompletedWithWrappedCancellationException(h3);
2598 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2599 >    }}
2600  
2601      /**
2602 <     * thenAcceptBothAsync result completes exceptionally after exceptional
2607 <     * completion of source
2602 >     * acceptEither result completes exceptionally if action does
2603       */
2604 <    public void testThenAcceptBothAsync2E() {
2605 <        CompletableFuture<Integer> f, g;
2606 <        CompletableFuture<Void> h;
2607 <        SubtractAction r;
2608 <        ThreadExecutor e = new ThreadExecutor();
2609 <
2610 <        f = new CompletableFuture<>();
2611 <        g = new CompletableFuture<>();
2612 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2618 <        f.completeExceptionally(new CFException());
2619 <        checkIncomplete(h);
2620 <        g.complete(1);
2621 <        checkCompletedWithWrappedCFException(h);
2622 <
2623 <        f = new CompletableFuture<>();
2624 <        g = new CompletableFuture<>();
2625 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2626 <        g.completeExceptionally(new CFException());
2627 <        checkIncomplete(h);
2628 <        f.complete(3);
2629 <        checkCompletedWithWrappedCFException(h);
2630 <
2631 <        f = new CompletableFuture<>();
2632 <        g = new CompletableFuture<>();
2633 <        f.complete(3);
2634 <        g.completeExceptionally(new CFException());
2635 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2636 <        checkCompletedWithWrappedCFException(h);
2637 <
2638 <        f = new CompletableFuture<>();
2639 <        g = new CompletableFuture<>();
2640 <        f.completeExceptionally(new CFException());
2641 <        g.complete(3);
2642 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2643 <        checkCompletedWithWrappedCFException(h);
2644 <
2645 <        assertEquals(0, e.count.get());
2646 <    }
2604 >    public void testAcceptEither_actionFailed() {
2605 >        for (ExecutionMode m : ExecutionMode.values())
2606 >        for (Integer v1 : new Integer[] { 1, null })
2607 >        for (Integer v2 : new Integer[] { 2, null })
2608 >    {
2609 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2610 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2611 >        final FailingConsumer[] rs = new FailingConsumer[6];
2612 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
2613  
2614 <    /**
2615 <     * thenAcceptBothAsync result completes exceptionally if action does
2616 <     */
2617 <    public void testThenAcceptBothAsync3E() {
2618 <        CompletableFuture<Integer> f, g;
2619 <        CompletableFuture<Void> h;
2620 <        FailingBiConsumer r;
2621 <        ThreadExecutor e = new ThreadExecutor();
2614 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2615 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2616 >        f.complete(v1);
2617 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2618 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2619 >        checkCompletedWithWrappedCFException(h0);
2620 >        checkCompletedWithWrappedCFException(h1);
2621 >        checkCompletedWithWrappedCFException(h2);
2622 >        checkCompletedWithWrappedCFException(h3);
2623 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2624  
2625 <        f = new CompletableFuture<>();
2658 <        g = new CompletableFuture<>();
2659 <        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer(), e);
2660 <        f.complete(3);
2661 <        checkIncomplete(h);
2662 <        g.complete(1);
2663 <        checkCompletedWithWrappedCFException(h);
2625 >        g.complete(v2);
2626  
2627 <        f = new CompletableFuture<>();
2628 <        g = new CompletableFuture<>();
2629 <        f.complete(3);
2630 <        g.complete(1);
2631 <        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer(), e);
2632 <        checkCompletedWithWrappedCFException(h);
2627 >        // unspecified behavior - both source completions available
2628 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2629 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2630 >
2631 >        checkCompletedWithWrappedCFException(h4);
2632 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2633 >                   Objects.equals(v2, rs[4].value));
2634 >        checkCompletedWithWrappedCFException(h5);
2635 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2636 >                   Objects.equals(v2, rs[5].value));
2637  
2638 <        assertEquals(2, e.count.get());
2639 <    }
2638 >        checkCompletedNormally(f, v1);
2639 >        checkCompletedNormally(g, v2);
2640 >    }}
2641  
2642      /**
2643 <     * thenAcceptBothAsync result completes exceptionally if either source cancelled
2643 >     * runAfterEither result completes normally after normal completion
2644 >     * of either source
2645       */
2646 <    public void testThenAcceptBothAsync4E() {
2647 <        CompletableFuture<Integer> f, g;
2648 <        CompletableFuture<Void> h;
2649 <        SubtractAction r;
2650 <        ThreadExecutor e = new ThreadExecutor();
2651 <
2652 <        f = new CompletableFuture<>();
2653 <        g = new CompletableFuture<>();
2654 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2687 <        assertTrue(f.cancel(true));
2688 <        checkIncomplete(h);
2689 <        g.complete(1);
2690 <        checkCompletedWithWrappedCancellationException(h);
2691 <
2692 <        f = new CompletableFuture<>();
2693 <        g = new CompletableFuture<>();
2694 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2695 <        assertTrue(g.cancel(true));
2696 <        checkIncomplete(h);
2697 <        f.complete(3);
2698 <        checkCompletedWithWrappedCancellationException(h);
2646 >    public void testRunAfterEither_normalCompletion() {
2647 >        for (ExecutionMode m : ExecutionMode.values())
2648 >        for (Integer v1 : new Integer[] { 1, null })
2649 >        for (Integer v2 : new Integer[] { 2, null })
2650 >    {
2651 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2652 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2653 >        final Noop[] rs = new Noop[6];
2654 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2655  
2656 <        f = new CompletableFuture<>();
2657 <        g = new CompletableFuture<>();
2658 <        f.complete(3);
2659 <        assertTrue(g.cancel(true));
2660 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2661 <        checkCompletedWithWrappedCancellationException(h);
2656 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2657 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2658 >        checkIncomplete(h0);
2659 >        checkIncomplete(h1);
2660 >        rs[0].assertNotInvoked();
2661 >        rs[1].assertNotInvoked();
2662 >        f.complete(v1);
2663 >        checkCompletedNormally(h0, null);
2664 >        checkCompletedNormally(h1, null);
2665 >        rs[0].assertInvoked();
2666 >        rs[1].assertInvoked();
2667 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2668 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2669 >        checkCompletedNormally(h2, null);
2670 >        checkCompletedNormally(h3, null);
2671 >        rs[2].assertInvoked();
2672 >        rs[3].assertInvoked();
2673  
2674 <        f = new CompletableFuture<>();
2708 <        g = new CompletableFuture<>();
2709 <        assertTrue(f.cancel(true));
2710 <        g.complete(3);
2711 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2712 <        checkCompletedWithWrappedCancellationException(h);
2674 >        g.complete(v2);
2675  
2676 <        assertEquals(0, e.count.get());
2677 <    }
2676 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2677 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2678  
2679 <    /**
2680 <     * runAfterBothAsync result completes normally after normal
2681 <     * completion of sources
2682 <     */
2683 <    public void testRunAfterBothAsyncE() {
2684 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2685 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2686 <        Noop r = new Noop();
2687 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2688 <        f.complete(one);
2727 <        checkIncomplete(g);
2728 <        f2.complete(two);
2729 <        checkCompletedNormally(g, null);
2730 <        assertTrue(r.ran);
2731 <    }
2679 >        checkCompletedNormally(f, v1);
2680 >        checkCompletedNormally(g, v2);
2681 >        checkCompletedNormally(h0, null);
2682 >        checkCompletedNormally(h1, null);
2683 >        checkCompletedNormally(h2, null);
2684 >        checkCompletedNormally(h3, null);
2685 >        checkCompletedNormally(h4, null);
2686 >        checkCompletedNormally(h5, null);
2687 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2688 >    }}
2689  
2690      /**
2691 <     * runAfterBothAsync result completes exceptionally after exceptional
2692 <     * completion of source
2691 >     * runAfterEither result completes exceptionally after exceptional
2692 >     * completion of either source
2693       */
2694 <    public void testRunAfterBothAsync2E() {
2695 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2696 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2697 <        Noop r = new Noop();
2698 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2699 <        f.completeExceptionally(new CFException());
2700 <        f2.complete(two);
2701 <        checkCompletedWithWrappedCFException(g);
2694 >    public void testRunAfterEither_exceptionalCompletion() {
2695 >        for (ExecutionMode m : ExecutionMode.values())
2696 >        for (Integer v1 : new Integer[] { 1, null })
2697 >    {
2698 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2699 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2700 >        final CFException ex = new CFException();
2701 >        final Noop[] rs = new Noop[6];
2702 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2703  
2704 <        r = new Noop();
2705 <        f = new CompletableFuture<>();
2706 <        f2 = new CompletableFuture<>();
2707 <        g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2708 <        f.complete(one);
2709 <        f2.completeExceptionally(new CFException());
2710 <        checkCompletedWithWrappedCFException(g);
2711 <    }
2704 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2705 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2706 >        checkIncomplete(h0);
2707 >        checkIncomplete(h1);
2708 >        rs[0].assertNotInvoked();
2709 >        rs[1].assertNotInvoked();
2710 >        assertTrue(f.completeExceptionally(ex));
2711 >        checkCompletedWithWrappedException(h0, ex);
2712 >        checkCompletedWithWrappedException(h1, ex);
2713 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2714 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2715 >        checkCompletedWithWrappedException(h2, ex);
2716 >        checkCompletedWithWrappedException(h3, ex);
2717 >
2718 >        assertTrue(g.complete(v1));
2719 >
2720 >        // unspecified behavior - both source completions available
2721 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2722 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2723 >        try {
2724 >            assertNull(h4.join());
2725 >            rs[4].assertInvoked();
2726 >        } catch (CompletionException ok) {
2727 >            checkCompletedWithWrappedException(h4, ex);
2728 >            rs[4].assertNotInvoked();
2729 >        }
2730 >        try {
2731 >            assertNull(h5.join());
2732 >            rs[5].assertInvoked();
2733 >        } catch (CompletionException ok) {
2734 >            checkCompletedWithWrappedException(h5, ex);
2735 >            rs[5].assertNotInvoked();
2736 >        }
2737  
2738 <    /**
2739 <     * runAfterBothAsync result completes exceptionally if action does
2740 <     */
2741 <    public void testRunAfterBothAsync3E() {
2742 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2743 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2744 <        FailingNoop r = new FailingNoop();
2745 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2746 <        f.complete(one);
2747 <        checkIncomplete(g);
2748 <        f2.complete(two);
2749 <        checkCompletedWithWrappedCFException(g);
2750 <    }
2738 >        checkCompletedExceptionally(f, ex);
2739 >        checkCompletedNormally(g, v1);
2740 >        checkCompletedWithWrappedException(h0, ex);
2741 >        checkCompletedWithWrappedException(h1, ex);
2742 >        checkCompletedWithWrappedException(h2, ex);
2743 >        checkCompletedWithWrappedException(h3, ex);
2744 >        checkCompletedWithWrappedException(h4, ex);
2745 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2746 >    }}
2747 >
2748 >    public void testRunAfterEither_exceptionalCompletion2() {
2749 >        for (ExecutionMode m : ExecutionMode.values())
2750 >        for (boolean fFirst : new boolean[] { true, false })
2751 >        for (Integer v1 : new Integer[] { 1, null })
2752 >    {
2753 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2754 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2755 >        final CFException ex = new CFException();
2756 >        final Noop[] rs = new Noop[6];
2757 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2758  
2759 <    /**
2760 <     * runAfterBothAsync result completes exceptionally if either source cancelled
2761 <     */
2762 <    public void testRunAfterBothAsync4E() {
2763 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2764 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2765 <        Noop r = new Noop();
2766 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2767 <        assertTrue(f.cancel(true));
2768 <        f2.complete(two);
2769 <        checkCompletedWithWrappedCancellationException(g);
2759 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2760 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2761 >        assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2762 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2763 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2764 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2765 >
2766 >        // unspecified behavior - both source completions available
2767 >        try {
2768 >            assertEquals(null, h0.join());
2769 >            rs[0].assertInvoked();
2770 >        } catch (CompletionException ok) {
2771 >            checkCompletedWithWrappedException(h0, ex);
2772 >            rs[0].assertNotInvoked();
2773 >        }
2774 >        try {
2775 >            assertEquals(null, h1.join());
2776 >            rs[1].assertInvoked();
2777 >        } catch (CompletionException ok) {
2778 >            checkCompletedWithWrappedException(h1, ex);
2779 >            rs[1].assertNotInvoked();
2780 >        }
2781 >        try {
2782 >            assertEquals(null, h2.join());
2783 >            rs[2].assertInvoked();
2784 >        } catch (CompletionException ok) {
2785 >            checkCompletedWithWrappedException(h2, ex);
2786 >            rs[2].assertNotInvoked();
2787 >        }
2788 >        try {
2789 >            assertEquals(null, h3.join());
2790 >            rs[3].assertInvoked();
2791 >        } catch (CompletionException ok) {
2792 >            checkCompletedWithWrappedException(h3, ex);
2793 >            rs[3].assertNotInvoked();
2794 >        }
2795  
2796 <        r = new Noop();
2797 <        f = new CompletableFuture<>();
2798 <        f2 = new CompletableFuture<>();
2784 <        g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2785 <        f.complete(one);
2786 <        assertTrue(f2.cancel(true));
2787 <        checkCompletedWithWrappedCancellationException(g);
2788 <    }
2796 >        checkCompletedNormally(f, v1);
2797 >        checkCompletedExceptionally(g, ex);
2798 >    }}
2799  
2800      /**
2801 <     * applyToEitherAsync result completes normally after normal
2792 <     * completion of sources
2801 >     * runAfterEither result completes exceptionally if either source cancelled
2802       */
2803 <    public void testApplyToEitherAsyncE() {
2804 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2805 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2806 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2807 <        f.complete(one);
2808 <        checkCompletedNormally(g, two);
2809 <
2810 <        f = new CompletableFuture<>();
2811 <        f.complete(one);
2803 <        f2 = new CompletableFuture<>();
2804 <        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2805 <        checkCompletedNormally(g, two);
2806 <    }
2803 >    public void testRunAfterEither_sourceCancelled() {
2804 >        for (ExecutionMode m : ExecutionMode.values())
2805 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2806 >        for (Integer v1 : new Integer[] { 1, null })
2807 >    {
2808 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2809 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2810 >        final Noop[] rs = new Noop[6];
2811 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2812  
2813 <    /**
2814 <     * applyToEitherAsync result completes exceptionally after exceptional
2815 <     * completion of source
2816 <     */
2817 <    public void testApplyToEitherAsync2E() {
2818 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2819 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2820 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2821 <        f.completeExceptionally(new CFException());
2822 <        checkCompletedWithWrappedCFException(g);
2813 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2814 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2815 >        checkIncomplete(h0);
2816 >        checkIncomplete(h1);
2817 >        rs[0].assertNotInvoked();
2818 >        rs[1].assertNotInvoked();
2819 >        f.cancel(mayInterruptIfRunning);
2820 >        checkCompletedWithWrappedCancellationException(h0);
2821 >        checkCompletedWithWrappedCancellationException(h1);
2822 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2823 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2824 >        checkCompletedWithWrappedCancellationException(h2);
2825 >        checkCompletedWithWrappedCancellationException(h3);
2826 >
2827 >        assertTrue(g.complete(v1));
2828 >
2829 >        // unspecified behavior - both source completions available
2830 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2831 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2832 >        try {
2833 >            assertNull(h4.join());
2834 >            rs[4].assertInvoked();
2835 >        } catch (CompletionException ok) {
2836 >            checkCompletedWithWrappedCancellationException(h4);
2837 >            rs[4].assertNotInvoked();
2838 >        }
2839 >        try {
2840 >            assertNull(h5.join());
2841 >            rs[5].assertInvoked();
2842 >        } catch (CompletionException ok) {
2843 >            checkCompletedWithWrappedCancellationException(h5);
2844 >            rs[5].assertNotInvoked();
2845 >        }
2846  
2847 <        f = new CompletableFuture<>();
2848 <        f2 = new CompletableFuture<>();
2849 <        f2.completeExceptionally(new CFException());
2850 <        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2851 <        f.complete(one);
2852 <        checkCompletedWithWrappedCFException(g);
2853 <    }
2847 >        checkCancelled(f);
2848 >        checkCompletedNormally(g, v1);
2849 >        checkCompletedWithWrappedCancellationException(h0);
2850 >        checkCompletedWithWrappedCancellationException(h1);
2851 >        checkCompletedWithWrappedCancellationException(h2);
2852 >        checkCompletedWithWrappedCancellationException(h3);
2853 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2854 >    }}
2855  
2856      /**
2857 <     * applyToEitherAsync result completes exceptionally if action does
2857 >     * runAfterEither result completes exceptionally if action does
2858       */
2859 <    public void testApplyToEitherAsync3E() {
2860 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2861 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2862 <        FailingFunction r = new FailingFunction();
2863 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor());
2864 <        f.complete(one);
2865 <        checkCompletedWithWrappedCFException(g);
2866 <    }
2859 >    public void testRunAfterEither_actionFailed() {
2860 >        for (ExecutionMode m : ExecutionMode.values())
2861 >        for (Integer v1 : new Integer[] { 1, null })
2862 >        for (Integer v2 : new Integer[] { 2, null })
2863 >    {
2864 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2865 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2866 >        final FailingRunnable[] rs = new FailingRunnable[6];
2867 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
2868  
2869 <    /**
2870 <     * applyToEitherAsync result completes exceptionally if either source cancelled
2871 <     */
2872 <    public void testApplyToEitherAsync4E() {
2873 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2874 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2875 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2876 <        assertTrue(f.cancel(true));
2877 <        checkCompletedWithWrappedCancellationException(g);
2869 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2870 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2871 >        assertTrue(f.complete(v1));
2872 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2873 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2874 >        checkCompletedWithWrappedCFException(h0);
2875 >        checkCompletedWithWrappedCFException(h1);
2876 >        checkCompletedWithWrappedCFException(h2);
2877 >        checkCompletedWithWrappedCFException(h3);
2878 >        for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2879 >        assertTrue(g.complete(v2));
2880 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2881 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2882 >        checkCompletedWithWrappedCFException(h4);
2883 >        checkCompletedWithWrappedCFException(h5);
2884  
2885 <        f = new CompletableFuture<>();
2886 <        f2 = new CompletableFuture<>();
2887 <        assertTrue(f2.cancel(true));
2888 <        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2853 <        checkCompletedWithWrappedCancellationException(g);
2854 <    }
2885 >        checkCompletedNormally(f, v1);
2886 >        checkCompletedNormally(g, v2);
2887 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2888 >    }}
2889  
2890      /**
2891 <     * acceptEitherAsync result completes normally after normal
2858 <     * completion of sources
2891 >     * thenCompose result completes normally after normal completion of source
2892       */
2893 <    public void testAcceptEitherAsyncE() {
2894 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2895 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2896 <        IncAction r = new IncAction();
2897 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2898 <        f.complete(one);
2899 <        checkCompletedNormally(g, null);
2900 <        assertEquals(r.value, 2);
2893 >    public void testThenCompose_normalCompletion() {
2894 >        for (ExecutionMode m : ExecutionMode.values())
2895 >        for (boolean createIncomplete : new boolean[] { true, false })
2896 >        for (Integer v1 : new Integer[] { 1, null })
2897 >    {
2898 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2899 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2900 >        if (!createIncomplete) assertTrue(f.complete(v1));
2901 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2902 >        if (createIncomplete) assertTrue(f.complete(v1));
2903  
2904 <        r = new IncAction();
2905 <        f = new CompletableFuture<>();
2906 <        f.complete(one);
2907 <        f2 = new CompletableFuture<>();
2873 <        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2874 <        checkCompletedNormally(g, null);
2875 <        assertEquals(r.value, 2);
2876 <    }
2904 >        checkCompletedNormally(g, inc(v1));
2905 >        checkCompletedNormally(f, v1);
2906 >        r.assertValue(v1);
2907 >    }}
2908  
2909      /**
2910 <     * acceptEitherAsync result completes exceptionally after exceptional
2910 >     * thenCompose result completes exceptionally after exceptional
2911       * completion of source
2912       */
2913 <    public void testAcceptEitherAsync2E() {
2914 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2915 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2916 <        IncAction r = new IncAction();
2917 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2918 <        f.completeExceptionally(new CFException());
2919 <        checkCompletedWithWrappedCFException(g);
2920 <
2921 <        r = new IncAction();
2922 <        f = new CompletableFuture<>();
2923 <        f2 = new CompletableFuture<>();
2924 <        f2.completeExceptionally(new CFException());
2925 <        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2926 <        f.complete(one);
2927 <        checkCompletedWithWrappedCFException(g);
2897 <    }
2898 <
2899 <    /**
2900 <     * acceptEitherAsync result completes exceptionally if action does
2901 <     */
2902 <    public void testAcceptEitherAsync3E() {
2903 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2904 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2905 <        FailingConsumer r = new FailingConsumer();
2906 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2907 <        f.complete(one);
2908 <        checkCompletedWithWrappedCFException(g);
2909 <    }
2910 <
2911 <    /**
2912 <     * acceptEitherAsync result completes exceptionally if either
2913 <     * source cancelled
2914 <     */
2915 <    public void testAcceptEitherAsync4E() {
2916 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2917 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2918 <        IncAction r = new IncAction();
2919 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2920 <        assertTrue(f.cancel(true));
2921 <        checkCompletedWithWrappedCancellationException(g);
2922 <
2923 <        r = new IncAction();
2924 <        f = new CompletableFuture<>();
2925 <        f2 = new CompletableFuture<>();
2926 <        assertTrue(f2.cancel(true));
2927 <        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2928 <        checkCompletedWithWrappedCancellationException(g);
2929 <    }
2930 <
2931 <    /**
2932 <     * runAfterEitherAsync result completes normally after normal
2933 <     * completion of sources
2934 <     */
2935 <    public void testRunAfterEitherAsyncE() {
2936 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2937 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2938 <        Noop r = new Noop();
2939 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2940 <        f.complete(one);
2941 <        checkCompletedNormally(g, null);
2942 <        assertTrue(r.ran);
2943 <
2944 <        r = new Noop();
2945 <        f = new CompletableFuture<>();
2946 <        f.complete(one);
2947 <        f2 = new CompletableFuture<>();
2948 <        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2949 <        checkCompletedNormally(g, null);
2950 <        assertTrue(r.ran);
2951 <    }
2913 >    public void testThenCompose_exceptionalCompletion() {
2914 >        for (ExecutionMode m : ExecutionMode.values())
2915 >        for (boolean createIncomplete : new boolean[] { true, false })
2916 >    {
2917 >        final CFException ex = new CFException();
2918 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2919 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2920 >        if (!createIncomplete) f.completeExceptionally(ex);
2921 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2922 >        if (createIncomplete) f.completeExceptionally(ex);
2923 >
2924 >        checkCompletedWithWrappedException(g, ex);
2925 >        checkCompletedExceptionally(f, ex);
2926 >        r.assertNotInvoked();
2927 >    }}
2928  
2929      /**
2930 <     * runAfterEitherAsync result completes exceptionally after exceptional
2955 <     * completion of source
2930 >     * thenCompose result completes exceptionally if action does
2931       */
2932 <    public void testRunAfterEitherAsync2E() {
2933 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2934 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2935 <        Noop r = new Noop();
2936 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2937 <        f.completeExceptionally(new CFException());
2938 <        checkCompletedWithWrappedCFException(g);
2932 >    public void testThenCompose_actionFailed() {
2933 >        for (ExecutionMode m : ExecutionMode.values())
2934 >        for (boolean createIncomplete : new boolean[] { true, false })
2935 >        for (Integer v1 : new Integer[] { 1, null })
2936 >    {
2937 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2938 >        final FailingCompletableFutureFunction r
2939 >            = new FailingCompletableFutureFunction(m);
2940 >        if (!createIncomplete) assertTrue(f.complete(v1));
2941 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2942 >        if (createIncomplete) assertTrue(f.complete(v1));
2943  
2965        r = new Noop();
2966        f = new CompletableFuture<>();
2967        f2 = new CompletableFuture<>();
2968        f2.completeExceptionally(new CFException());
2969        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2970        f.complete(one);
2944          checkCompletedWithWrappedCFException(g);
2945 <    }
2946 <
2974 <    /**
2975 <     * runAfterEitherAsync result completes exceptionally if action does
2976 <     */
2977 <    public void testRunAfterEitherAsync3E() {
2978 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2979 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2980 <        FailingNoop r = new FailingNoop();
2981 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2982 <        f.complete(one);
2983 <        checkCompletedWithWrappedCFException(g);
2984 <    }
2945 >        checkCompletedNormally(f, v1);
2946 >    }}
2947  
2948      /**
2949 <     * runAfterEitherAsync result completes exceptionally if either
2988 <     * source cancelled
2949 >     * thenCompose result completes exceptionally if source cancelled
2950       */
2951 <    public void testRunAfterEitherAsync4E() {
2952 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2953 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2954 <        Noop r = new Noop();
2955 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2956 <        assertTrue(f.cancel(true));
2957 <        checkCompletedWithWrappedCancellationException(g);
2951 >    public void testThenCompose_sourceCancelled() {
2952 >        for (ExecutionMode m : ExecutionMode.values())
2953 >        for (boolean createIncomplete : new boolean[] { true, false })
2954 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2955 >    {
2956 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2957 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2958 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2959 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2960 >        if (createIncomplete) {
2961 >            checkIncomplete(g);
2962 >            assertTrue(f.cancel(mayInterruptIfRunning));
2963 >        }
2964  
2998        r = new Noop();
2999        f = new CompletableFuture<>();
3000        f2 = new CompletableFuture<>();
3001        assertTrue(f2.cancel(true));
3002        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2965          checkCompletedWithWrappedCancellationException(g);
2966 <    }
2967 <
3006 <    /**
3007 <     * thenComposeAsync result completes normally after normal
3008 <     * completion of source
3009 <     */
3010 <    public void testThenComposeAsyncE() {
3011 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3012 <        CompletableFutureInc r = new CompletableFutureInc();
3013 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
3014 <        f.complete(one);
3015 <        checkCompletedNormally(g, two);
3016 <    }
3017 <
3018 <    /**
3019 <     * thenComposeAsync result completes exceptionally after
3020 <     * exceptional completion of source
3021 <     */
3022 <    public void testThenComposeAsync2E() {
3023 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3024 <        CompletableFutureInc r = new CompletableFutureInc();
3025 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
3026 <        f.completeExceptionally(new CFException());
3027 <        checkCompletedWithWrappedCFException(g);
3028 <    }
2966 >        checkCancelled(f);
2967 >    }}
2968  
2969      /**
2970 <     * thenComposeAsync result completes exceptionally if action does
2970 >     * thenCompose result completes exceptionally if the result of the action does
2971       */
2972 <    public void testThenComposeAsync3E() {
2973 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2974 <        FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
2975 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2976 <        f.complete(one);
2977 <        checkCompletedWithWrappedCFException(g);
2978 <    }
2972 >    public void testThenCompose_actionReturnsFailingFuture() {
2973 >        for (ExecutionMode m : ExecutionMode.values())
2974 >        for (int order = 0; order < 6; order++)
2975 >        for (Integer v1 : new Integer[] { 1, null })
2976 >    {
2977 >        final CFException ex = new CFException();
2978 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2979 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2980 >        final CompletableFuture<Integer> h;
2981 >        // Test all permutations of orders
2982 >        switch (order) {
2983 >        case 0:
2984 >            assertTrue(f.complete(v1));
2985 >            assertTrue(g.completeExceptionally(ex));
2986 >            h = m.thenCompose(f, (x -> g));
2987 >            break;
2988 >        case 1:
2989 >            assertTrue(f.complete(v1));
2990 >            h = m.thenCompose(f, (x -> g));
2991 >            assertTrue(g.completeExceptionally(ex));
2992 >            break;
2993 >        case 2:
2994 >            assertTrue(g.completeExceptionally(ex));
2995 >            assertTrue(f.complete(v1));
2996 >            h = m.thenCompose(f, (x -> g));
2997 >            break;
2998 >        case 3:
2999 >            assertTrue(g.completeExceptionally(ex));
3000 >            h = m.thenCompose(f, (x -> g));
3001 >            assertTrue(f.complete(v1));
3002 >            break;
3003 >        case 4:
3004 >            h = m.thenCompose(f, (x -> g));
3005 >            assertTrue(f.complete(v1));
3006 >            assertTrue(g.completeExceptionally(ex));
3007 >            break;
3008 >        case 5:
3009 >            h = m.thenCompose(f, (x -> g));
3010 >            assertTrue(f.complete(v1));
3011 >            assertTrue(g.completeExceptionally(ex));
3012 >            break;
3013 >        default: throw new AssertionError();
3014 >        }
3015  
3016 <    /**
3017 <     * thenComposeAsync result completes exceptionally if source cancelled
3018 <     */
3019 <    public void testThenComposeAsync4E() {
3045 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3046 <        CompletableFutureInc r = new CompletableFutureInc();
3047 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
3048 <        assertTrue(f.cancel(true));
3049 <        checkCompletedWithWrappedCancellationException(g);
3050 <    }
3016 >        checkCompletedExceptionally(g, ex);
3017 >        checkCompletedWithWrappedException(h, ex);
3018 >        checkCompletedNormally(f, v1);
3019 >    }}
3020  
3021      // other static methods
3022  
# Line 3065 | Line 3034 | public class CompletableFutureTest exten
3034       * when all components complete normally
3035       */
3036      public void testAllOf_normal() throws Exception {
3037 <        for (int k = 1; k < 20; ++k) {
3038 <            CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3039 <            for (int i = 0; i < k; ++i)
3037 >        for (int k = 1; k < 10; k++) {
3038 >            CompletableFuture<Integer>[] fs
3039 >                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3040 >            for (int i = 0; i < k; i++)
3041                  fs[i] = new CompletableFuture<>();
3042              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3043 <            for (int i = 0; i < k; ++i) {
3043 >            for (int i = 0; i < k; i++) {
3044                  checkIncomplete(f);
3045                  checkIncomplete(CompletableFuture.allOf(fs));
3046                  fs[i].complete(one);
# Line 3080 | Line 3050 | public class CompletableFutureTest exten
3050          }
3051      }
3052  
3053 +    public void testAllOf_backwards() throws Exception {
3054 +        for (int k = 1; k < 10; k++) {
3055 +            CompletableFuture<Integer>[] fs
3056 +                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3057 +            for (int i = 0; i < k; i++)
3058 +                fs[i] = new CompletableFuture<>();
3059 +            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3060 +            for (int i = k - 1; i >= 0; i--) {
3061 +                checkIncomplete(f);
3062 +                checkIncomplete(CompletableFuture.allOf(fs));
3063 +                fs[i].complete(one);
3064 +            }
3065 +            checkCompletedNormally(f, null);
3066 +            checkCompletedNormally(CompletableFuture.allOf(fs), null);
3067 +        }
3068 +    }
3069 +
3070 +    public void testAllOf_exceptional() throws Exception {
3071 +        for (int k = 1; k < 10; k++) {
3072 +            CompletableFuture<Integer>[] fs
3073 +                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3074 +            CFException ex = new CFException();
3075 +            for (int i = 0; i < k; i++)
3076 +                fs[i] = new CompletableFuture<>();
3077 +            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3078 +            for (int i = 0; i < k; i++) {
3079 +                checkIncomplete(f);
3080 +                checkIncomplete(CompletableFuture.allOf(fs));
3081 +                if (i != k / 2) {
3082 +                    fs[i].complete(i);
3083 +                    checkCompletedNormally(fs[i], i);
3084 +                } else {
3085 +                    fs[i].completeExceptionally(ex);
3086 +                    checkCompletedExceptionally(fs[i], ex);
3087 +                }
3088 +            }
3089 +            checkCompletedWithWrappedException(f, ex);
3090 +            checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex);
3091 +        }
3092 +    }
3093 +
3094      /**
3095       * anyOf(no component futures) returns an incomplete future
3096       */
3097      public void testAnyOf_empty() throws Exception {
3098 +        for (Integer v1 : new Integer[] { 1, null })
3099 +    {
3100          CompletableFuture<Object> f = CompletableFuture.anyOf();
3101          checkIncomplete(f);
3102 <    }
3102 >
3103 >        f.complete(v1);
3104 >        checkCompletedNormally(f, v1);
3105 >    }}
3106  
3107      /**
3108       * anyOf returns a future completed normally with a value when
3109       * a component future does
3110       */
3111      public void testAnyOf_normal() throws Exception {
3112 <        for (int k = 0; k < 10; ++k) {
3112 >        for (int k = 0; k < 10; k++) {
3113              CompletableFuture[] fs = new CompletableFuture[k];
3114 <            for (int i = 0; i < k; ++i)
3114 >            for (int i = 0; i < k; i++)
3115                  fs[i] = new CompletableFuture<>();
3116              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3117              checkIncomplete(f);
3118 <            for (int i = 0; i < k; ++i) {
3119 <                fs[i].complete(one);
3120 <                checkCompletedNormally(f, one);
3121 <                checkCompletedNormally(CompletableFuture.anyOf(fs), one);
3118 >            for (int i = 0; i < k; i++) {
3119 >                fs[i].complete(i);
3120 >                checkCompletedNormally(f, 0);
3121 >                int x = (int) CompletableFuture.anyOf(fs).join();
3122 >                assertTrue(0 <= x && x <= i);
3123 >            }
3124 >        }
3125 >    }
3126 >    public void testAnyOf_normal_backwards() throws Exception {
3127 >        for (int k = 0; k < 10; k++) {
3128 >            CompletableFuture[] fs = new CompletableFuture[k];
3129 >            for (int i = 0; i < k; i++)
3130 >                fs[i] = new CompletableFuture<>();
3131 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3132 >            checkIncomplete(f);
3133 >            for (int i = k - 1; i >= 0; i--) {
3134 >                fs[i].complete(i);
3135 >                checkCompletedNormally(f, k - 1);
3136 >                int x = (int) CompletableFuture.anyOf(fs).join();
3137 >                assertTrue(i <= x && x <= k - 1);
3138              }
3139          }
3140      }
# Line 3111 | Line 3143 | public class CompletableFutureTest exten
3143       * anyOf result completes exceptionally when any component does.
3144       */
3145      public void testAnyOf_exceptional() throws Exception {
3146 <        for (int k = 0; k < 10; ++k) {
3146 >        for (int k = 0; k < 10; k++) {
3147 >            CompletableFuture[] fs = new CompletableFuture[k];
3148 >            CFException[] exs = new CFException[k];
3149 >            for (int i = 0; i < k; i++) {
3150 >                fs[i] = new CompletableFuture<>();
3151 >                exs[i] = new CFException();
3152 >            }
3153 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3154 >            checkIncomplete(f);
3155 >            for (int i = 0; i < k; i++) {
3156 >                fs[i].completeExceptionally(exs[i]);
3157 >                checkCompletedWithWrappedException(f, exs[0]);
3158 >                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3159 >            }
3160 >        }
3161 >    }
3162 >
3163 >    public void testAnyOf_exceptional_backwards() throws Exception {
3164 >        for (int k = 0; k < 10; k++) {
3165              CompletableFuture[] fs = new CompletableFuture[k];
3166 <            for (int i = 0; i < k; ++i)
3166 >            CFException[] exs = new CFException[k];
3167 >            for (int i = 0; i < k; i++) {
3168                  fs[i] = new CompletableFuture<>();
3169 +                exs[i] = new CFException();
3170 +            }
3171              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3172              checkIncomplete(f);
3173 <            for (int i = 0; i < k; ++i) {
3174 <                fs[i].completeExceptionally(new CFException());
3175 <                checkCompletedWithWrappedCFException(f);
3173 >            for (int i = k - 1; i >= 0; i--) {
3174 >                fs[i].completeExceptionally(exs[i]);
3175 >                checkCompletedWithWrappedException(f, exs[k - 1]);
3176                  checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3177              }
3178          }
# Line 3132 | Line 3185 | public class CompletableFutureTest exten
3185          CompletableFuture<Integer> f = new CompletableFuture<>();
3186          CompletableFuture<Integer> g = new CompletableFuture<>();
3187          CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null;
3135        CompletableFuture<?> h;
3188          ThreadExecutor exec = new ThreadExecutor();
3189  
3190          Runnable[] throwingActions = {
3191              () -> CompletableFuture.supplyAsync(null),
3192              () -> CompletableFuture.supplyAsync(null, exec),
3193 <            () -> CompletableFuture.supplyAsync(supplyOne, null),
3193 >            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null),
3194  
3195              () -> CompletableFuture.runAsync(null),
3196              () -> CompletableFuture.runAsync(null, exec),
# Line 3211 | Line 3263 | public class CompletableFutureTest exten
3263  
3264              () -> f.thenCompose(null),
3265              () -> f.thenComposeAsync(null),
3266 <            () -> f.thenComposeAsync(new CompletableFutureInc(), null),
3266 >            () -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null),
3267              () -> f.thenComposeAsync(null, exec),
3268  
3269              () -> f.exceptionally(null),
# Line 3229 | Line 3281 | public class CompletableFutureTest exten
3281              () -> CompletableFuture.anyOf(null, f),
3282  
3283              () -> f.obtrudeException(null),
3284 +
3285 +            () -> CompletableFuture.delayedExecutor(1L, SECONDS, null),
3286 +            () -> CompletableFuture.delayedExecutor(1L, null, new ThreadExecutor()),
3287 +            () -> CompletableFuture.delayedExecutor(1L, null),
3288 +
3289 +            () -> f.orTimeout(1L, null),
3290 +            () -> f.completeOnTimeout(42, 1L, null),
3291 +
3292 +            () -> CompletableFuture.failedFuture(null),
3293 +            () -> CompletableFuture.failedStage(null),
3294          };
3295  
3296          assertThrows(NullPointerException.class, throwingActions);
# Line 3243 | Line 3305 | public class CompletableFutureTest exten
3305          assertSame(f, f.toCompletableFuture());
3306      }
3307  
3308 +    // jdk9
3309 +
3310      /**
3311 <     * whenComplete action executes on normal completion, propagating
3248 <     * source result.
3311 >     * newIncompleteFuture returns an incomplete CompletableFuture
3312       */
3313 <    public void testWhenComplete1() {
3314 <        final AtomicInteger a = new AtomicInteger();
3313 >    public void testNewIncompleteFuture() {
3314 >        for (Integer v1 : new Integer[] { 1, null })
3315 >    {
3316          CompletableFuture<Integer> f = new CompletableFuture<>();
3317 <        CompletableFuture<Integer> g =
3318 <            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3319 <        f.complete(three);
3320 <        checkCompletedNormally(f, three);
3321 <        checkCompletedNormally(g, three);
3322 <        assertEquals(a.get(), 1);
3317 >        CompletableFuture<Integer> g = f.newIncompleteFuture();
3318 >        checkIncomplete(f);
3319 >        checkIncomplete(g);
3320 >        f.complete(v1);
3321 >        checkCompletedNormally(f, v1);
3322 >        checkIncomplete(g);
3323 >        g.complete(v1);
3324 >        checkCompletedNormally(g, v1);
3325 >        assertSame(g.getClass(), CompletableFuture.class);
3326 >    }}
3327 >
3328 >    /**
3329 >     * completedStage returns a completed CompletionStage
3330 >     */
3331 >    public void testCompletedStage() {
3332 >        AtomicInteger x = new AtomicInteger(0);
3333 >        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3334 >        CompletionStage<Integer> f = CompletableFuture.completedStage(1);
3335 >        f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3336 >        assertEquals(x.get(), 1);
3337 >        assertNull(r.get());
3338      }
3339  
3340      /**
3341 <     * whenComplete action executes on exceptional completion, propagating
3342 <     * source result.
3341 >     * defaultExecutor by default returns the commonPool if
3342 >     * it supports more than one thread.
3343       */
3344 <    public void testWhenComplete2() {
3266 <        final AtomicInteger a = new AtomicInteger();
3344 >    public void testDefaultExecutor() {
3345          CompletableFuture<Integer> f = new CompletableFuture<>();
3346 <        CompletableFuture<Integer> g =
3347 <            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3348 <        f.completeExceptionally(new CFException());
3349 <        assertTrue(f.isCompletedExceptionally());
3350 <        assertTrue(g.isCompletedExceptionally());
3351 <        assertEquals(a.get(), 1);
3346 >        Executor e = f.defaultExecutor();
3347 >        Executor c = ForkJoinPool.commonPool();
3348 >        if (ForkJoinPool.getCommonPoolParallelism() > 1)
3349 >            assertSame(e, c);
3350 >        else
3351 >            assertNotSame(e, c);
3352      }
3353  
3354      /**
3355 <     * If a whenComplete action throws an exception when triggered by
3356 <     * a normal completion, it completes exceptionally
3355 >     * failedFuture returns a CompletableFuture completed
3356 >     * exceptionally with the given Exception
3357       */
3358 <    public void testWhenComplete3() {
3359 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3360 <        CompletableFuture<Integer> g =
3361 <            f.whenComplete((Integer x, Throwable t) ->
3284 <                           { throw new CFException(); } );
3285 <        f.complete(three);
3286 <        checkCompletedNormally(f, three);
3287 <        assertTrue(g.isCompletedExceptionally());
3288 <        checkCompletedWithWrappedCFException(g);
3358 >    public void testFailedFuture() {
3359 >        CFException ex = new CFException();
3360 >        CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex);
3361 >        checkCompletedExceptionally(f, ex);
3362      }
3363  
3364      /**
3365 <     * whenCompleteAsync action executes on normal completion, propagating
3293 <     * source result.
3365 >     * failedFuture(null) throws NPE
3366       */
3367 <    public void testWhenCompleteAsync1() {
3368 <        final AtomicInteger a = new AtomicInteger();
3369 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3370 <        CompletableFuture<Integer> g =
3371 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3300 <        f.complete(three);
3301 <        checkCompletedNormally(f, three);
3302 <        checkCompletedNormally(g, three);
3303 <        assertEquals(a.get(), 1);
3367 >    public void testFailedFuture_null() {
3368 >        try {
3369 >            CompletableFuture<Integer> f = CompletableFuture.failedFuture(null);
3370 >            shouldThrow();
3371 >        } catch (NullPointerException success) {}
3372      }
3373  
3374      /**
3375 <     * whenCompleteAsync action executes on exceptional completion, propagating
3376 <     * source result.
3375 >     * copy returns a CompletableFuture that is completed normally,
3376 >     * with the same value, when source is.
3377       */
3378 <    public void testWhenCompleteAsync2() {
3311 <        final AtomicInteger a = new AtomicInteger();
3378 >    public void testCopy() {
3379          CompletableFuture<Integer> f = new CompletableFuture<>();
3380 <        CompletableFuture<Integer> g =
3381 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3382 <        f.completeExceptionally(new CFException());
3383 <        checkCompletedWithWrappedCFException(f);
3384 <        checkCompletedWithWrappedCFException(g);
3380 >        CompletableFuture<Integer> g = f.copy();
3381 >        checkIncomplete(f);
3382 >        checkIncomplete(g);
3383 >        f.complete(1);
3384 >        checkCompletedNormally(f, 1);
3385 >        checkCompletedNormally(g, 1);
3386      }
3387  
3388      /**
3389 <     * If a whenCompleteAsync action throws an exception when
3390 <     * triggered by a normal completion, it completes exceptionally
3389 >     * copy returns a CompletableFuture that is completed exceptionally
3390 >     * when source is.
3391       */
3392 <    public void testWhenCompleteAsync3() {
3392 >    public void testCopy2() {
3393          CompletableFuture<Integer> f = new CompletableFuture<>();
3394 <        CompletableFuture<Integer> g =
3395 <            f.whenCompleteAsync((Integer x, Throwable t) ->
3396 <                           { throw new CFException(); } );
3397 <        f.complete(three);
3398 <        checkCompletedNormally(f, three);
3399 <        checkCompletedWithWrappedCFException(g);
3394 >        CompletableFuture<Integer> g = f.copy();
3395 >        checkIncomplete(f);
3396 >        checkIncomplete(g);
3397 >        CFException ex = new CFException();
3398 >        f.completeExceptionally(ex);
3399 >        checkCompletedExceptionally(f, ex);
3400 >        checkCompletedWithWrappedException(g, ex);
3401      }
3402  
3403      /**
3404 <     * whenCompleteAsync action executes on normal completion, propagating
3405 <     * source result.
3404 >     * minimalCompletionStage returns a CompletableFuture that is
3405 >     * completed normally, with the same value, when source is.
3406       */
3407 <    public void testWhenCompleteAsync1e() {
3339 <        final AtomicInteger a = new AtomicInteger();
3340 <        ThreadExecutor exec = new ThreadExecutor();
3407 >    public void testMinimalCompletionStage() {
3408          CompletableFuture<Integer> f = new CompletableFuture<>();
3409 <        CompletableFuture<Integer> g =
3410 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3411 <                                exec);
3412 <        f.complete(three);
3413 <        checkCompletedNormally(f, three);
3414 <        checkCompletedNormally(g, three);
3415 <        assertEquals(a.get(), 1);
3409 >        CompletionStage<Integer> g = f.minimalCompletionStage();
3410 >        AtomicInteger x = new AtomicInteger(0);
3411 >        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3412 >        checkIncomplete(f);
3413 >        g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3414 >        f.complete(1);
3415 >        checkCompletedNormally(f, 1);
3416 >        assertEquals(x.get(), 1);
3417 >        assertNull(r.get());
3418      }
3419  
3420      /**
3421 <     * whenCompleteAsync action executes on exceptional completion, propagating
3422 <     * source result.
3421 >     * minimalCompletionStage returns a CompletableFuture that is
3422 >     * completed exceptionally when source is.
3423       */
3424 <    public void testWhenCompleteAsync2e() {
3356 <        final AtomicInteger a = new AtomicInteger();
3357 <        ThreadExecutor exec = new ThreadExecutor();
3424 >    public void testMinimalCompletionStage2() {
3425          CompletableFuture<Integer> f = new CompletableFuture<>();
3426 <        CompletableFuture<Integer> g =
3427 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3428 <                                exec);
3429 <        f.completeExceptionally(new CFException());
3430 <        checkCompletedWithWrappedCFException(f);
3431 <        checkCompletedWithWrappedCFException(g);
3426 >        CompletionStage<Integer> g = f.minimalCompletionStage();
3427 >        AtomicInteger x = new AtomicInteger(0);
3428 >        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3429 >        g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3430 >        checkIncomplete(f);
3431 >        CFException ex = new CFException();
3432 >        f.completeExceptionally(ex);
3433 >        checkCompletedExceptionally(f, ex);
3434 >        assertEquals(x.get(), 0);
3435 >        assertEquals(r.get().getCause(), ex);
3436      }
3437  
3438      /**
3439 <     * If a whenCompleteAsync action throws an exception when triggered
3440 <     * by a normal completion, it completes exceptionally
3439 >     * failedStage returns a CompletionStage completed
3440 >     * exceptionally with the given Exception
3441       */
3442 <    public void testWhenCompleteAsync3e() {
3443 <        ThreadExecutor exec = new ThreadExecutor();
3442 >    public void testFailedStage() {
3443 >        CFException ex = new CFException();
3444 >        CompletionStage<Integer> f = CompletableFuture.failedStage(ex);
3445 >        AtomicInteger x = new AtomicInteger(0);
3446 >        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3447 >        f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3448 >        assertEquals(x.get(), 0);
3449 >        assertEquals(r.get(), ex);
3450 >    }
3451 >
3452 >    /**
3453 >     * completeAsync completes with value of given supplier
3454 >     */
3455 >    public void testCompleteAsync() {
3456 >        for (Integer v1 : new Integer[] { 1, null })
3457 >    {
3458          CompletableFuture<Integer> f = new CompletableFuture<>();
3459 <        CompletableFuture<Integer> g =
3460 <            f.whenCompleteAsync((Integer x, Throwable t) ->
3461 <                                { throw new CFException(); },
3462 <                                exec);
3463 <        f.complete(three);
3464 <        checkCompletedNormally(f, three);
3465 <        checkCompletedWithWrappedCFException(g);
3459 >        f.completeAsync(() -> v1);
3460 >        f.join();
3461 >        checkCompletedNormally(f, v1);
3462 >    }}
3463 >
3464 >    /**
3465 >     * completeAsync completes exceptionally if given supplier throws
3466 >     */
3467 >    public void testCompleteAsync2() {
3468 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3469 >        CFException ex = new CFException();
3470 >        f.completeAsync(() -> {if (true) throw ex; return 1;});
3471 >        try {
3472 >            f.join();
3473 >            shouldThrow();
3474 >        } catch (CompletionException success) {}
3475 >        checkCompletedWithWrappedException(f, ex);
3476      }
3477  
3478      /**
3479 <     * handleAsync action completes normally with function value on
3385 <     * either normal or exceptional completion of source
3479 >     * completeAsync with given executor completes with value of given supplier
3480       */
3481 <    public void testHandleAsync() {
3482 <        CompletableFuture<Integer> f, g;
3483 <        IntegerHandler r;
3481 >    public void testCompleteAsync3() {
3482 >        for (Integer v1 : new Integer[] { 1, null })
3483 >    {
3484 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3485 >        ThreadExecutor executor = new ThreadExecutor();
3486 >        f.completeAsync(() -> v1, executor);
3487 >        assertSame(v1, f.join());
3488 >        checkCompletedNormally(f, v1);
3489 >        assertEquals(1, executor.count.get());
3490 >    }}
3491  
3492 <        f = new CompletableFuture<>();
3493 <        g = f.handleAsync(r = new IntegerHandler());
3494 <        assertFalse(r.ran);
3495 <        f.completeExceptionally(new CFException());
3496 <        checkCompletedWithWrappedCFException(f);
3497 <        checkCompletedNormally(g, three);
3498 <        assertTrue(r.ran);
3492 >    /**
3493 >     * completeAsync with given executor completes exceptionally if
3494 >     * given supplier throws
3495 >     */
3496 >    public void testCompleteAsync4() {
3497 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3498 >        CFException ex = new CFException();
3499 >        ThreadExecutor executor = new ThreadExecutor();
3500 >        f.completeAsync(() -> {if (true) throw ex; return 1;}, executor);
3501 >        try {
3502 >            f.join();
3503 >            shouldThrow();
3504 >        } catch (CompletionException success) {}
3505 >        checkCompletedWithWrappedException(f, ex);
3506 >        assertEquals(1, executor.count.get());
3507 >    }
3508  
3509 <        f = new CompletableFuture<>();
3510 <        g = f.handleAsync(r = new IntegerHandler());
3511 <        assertFalse(r.ran);
3512 <        f.completeExceptionally(new CFException());
3513 <        checkCompletedWithWrappedCFException(f);
3514 <        checkCompletedNormally(g, three);
3515 <        assertTrue(r.ran);
3509 >    /**
3510 >     * orTimeout completes with TimeoutException if not complete
3511 >     */
3512 >    public void testOrTimeout_timesOut() {
3513 >        long timeoutMillis = timeoutMillis();
3514 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3515 >        long startTime = System.nanoTime();
3516 >        f.orTimeout(timeoutMillis, MILLISECONDS);
3517 >        checkCompletedWithTimeoutException(f);
3518 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
3519 >    }
3520  
3521 <        f = new CompletableFuture<>();
3522 <        g = f.handleAsync(r = new IntegerHandler());
3523 <        assertFalse(r.ran);
3524 <        f.complete(one);
3525 <        checkCompletedNormally(f, one);
3526 <        checkCompletedNormally(g, two);
3527 <        assertTrue(r.ran);
3521 >    /**
3522 >     * orTimeout completes normally if completed before timeout
3523 >     */
3524 >    public void testOrTimeout_completed() {
3525 >        for (Integer v1 : new Integer[] { 1, null })
3526 >    {
3527 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3528 >        CompletableFuture<Integer> g = new CompletableFuture<>();
3529 >        long startTime = System.nanoTime();
3530 >        f.complete(v1);
3531 >        f.orTimeout(LONG_DELAY_MS, MILLISECONDS);
3532 >        g.orTimeout(LONG_DELAY_MS, MILLISECONDS);
3533 >        g.complete(v1);
3534 >        checkCompletedNormally(f, v1);
3535 >        checkCompletedNormally(g, v1);
3536 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
3537 >    }}
3538  
3539 <        f = new CompletableFuture<>();
3540 <        g = f.handleAsync(r = new IntegerHandler());
3541 <        assertFalse(r.ran);
3542 <        f.complete(one);
3543 <        checkCompletedNormally(f, one);
3544 <        checkCompletedNormally(g, two);
3545 <        assertTrue(r.ran);
3539 >    /**
3540 >     * completeOnTimeout completes with given value if not complete
3541 >     */
3542 >    public void testCompleteOnTimeout_timesOut() {
3543 >        testInParallel(() -> testCompleteOnTimeout_timesOut(42),
3544 >                       () -> testCompleteOnTimeout_timesOut(null));
3545 >    }
3546 >
3547 >    public void testCompleteOnTimeout_timesOut(Integer v) {
3548 >        long timeoutMillis = timeoutMillis();
3549 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3550 >        long startTime = System.nanoTime();
3551 >        f.completeOnTimeout(v, timeoutMillis, MILLISECONDS);
3552 >        assertSame(v, f.join());
3553 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
3554 >        f.complete(99);         // should have no effect
3555 >        checkCompletedNormally(f, v);
3556      }
3557  
3558      /**
3559 <     * handleAsync action with Executor completes normally with
3426 <     * function value on either normal or exceptional completion of
3427 <     * source
3559 >     * completeOnTimeout has no effect if completed within timeout
3560       */
3561 <    public void testHandleAsync2() {
3562 <        CompletableFuture<Integer> f, g;
3563 <        ThreadExecutor exec = new ThreadExecutor();
3564 <        IntegerHandler r;
3561 >    public void testCompleteOnTimeout_completed() {
3562 >        for (Integer v1 : new Integer[] { 1, null })
3563 >    {
3564 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3565 >        CompletableFuture<Integer> g = new CompletableFuture<>();
3566 >        long startTime = System.nanoTime();
3567 >        f.complete(v1);
3568 >        f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS);
3569 >        g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS);
3570 >        g.complete(v1);
3571 >        checkCompletedNormally(f, v1);
3572 >        checkCompletedNormally(g, v1);
3573 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
3574 >    }}
3575  
3576 <        f = new CompletableFuture<>();
3577 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3578 <        assertFalse(r.ran);
3579 <        f.completeExceptionally(new CFException());
3580 <        checkCompletedWithWrappedCFException(f);
3581 <        checkCompletedNormally(g, three);
3582 <        assertTrue(r.ran);
3576 >    /**
3577 >     * delayedExecutor returns an executor that delays submission
3578 >     */
3579 >    public void testDelayedExecutor() {
3580 >        testInParallel(() -> testDelayedExecutor(null, null),
3581 >                       () -> testDelayedExecutor(null, 1),
3582 >                       () -> testDelayedExecutor(new ThreadExecutor(), 1),
3583 >                       () -> testDelayedExecutor(new ThreadExecutor(), 1));
3584 >    }
3585 >
3586 >    public void testDelayedExecutor(Executor executor, Integer v) throws Exception {
3587 >        long timeoutMillis = timeoutMillis();
3588 >        // Use an "unreasonably long" long timeout to catch lingering threads
3589 >        long longTimeoutMillis = 1000 * 60 * 60 * 24;
3590 >        final Executor delayer, longDelayer;
3591 >        if (executor == null) {
3592 >            delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS);
3593 >            longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS);
3594 >        } else {
3595 >            delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS, executor);
3596 >            longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS, executor);
3597 >        }
3598 >        long startTime = System.nanoTime();
3599 >        CompletableFuture<Integer> f =
3600 >            CompletableFuture.supplyAsync(() -> v, delayer);
3601 >        CompletableFuture<Integer> g =
3602 >            CompletableFuture.supplyAsync(() -> v, longDelayer);
3603  
3604 <        f = new CompletableFuture<>();
3443 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3444 <        assertFalse(r.ran);
3445 <        f.completeExceptionally(new CFException());
3446 <        checkCompletedWithWrappedCFException(f);
3447 <        checkCompletedNormally(g, three);
3448 <        assertTrue(r.ran);
3604 >        assertNull(g.getNow(null));
3605  
3606 <        f = new CompletableFuture<>();
3607 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3608 <        assertFalse(r.ran);
3609 <        f.complete(one);
3454 <        checkCompletedNormally(f, one);
3455 <        checkCompletedNormally(g, two);
3456 <        assertTrue(r.ran);
3606 >        assertSame(v, f.get(LONG_DELAY_MS, MILLISECONDS));
3607 >        long millisElapsed = millisElapsedSince(startTime);
3608 >        assertTrue(millisElapsed >= timeoutMillis);
3609 >        assertTrue(millisElapsed < LONG_DELAY_MS / 2);
3610  
3611 <        f = new CompletableFuture<>();
3612 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3613 <        assertFalse(r.ran);
3614 <        f.complete(one);
3462 <        checkCompletedNormally(f, one);
3463 <        checkCompletedNormally(g, two);
3464 <        assertTrue(r.ran);
3611 >        checkCompletedNormally(f, v);
3612 >
3613 >        checkIncomplete(g);
3614 >        assertTrue(g.cancel(true));
3615      }
3616  
3617 +    //--- tests of implementation details; not part of official tck ---
3618 +
3619 +    Object resultOf(CompletableFuture<?> f) {
3620 +        try {
3621 +            java.lang.reflect.Field resultField
3622 +                = CompletableFuture.class.getDeclaredField("result");
3623 +            resultField.setAccessible(true);
3624 +            return resultField.get(f);
3625 +        } catch (Throwable t) { throw new AssertionError(t); }
3626 +    }
3627 +
3628 +    public void testExceptionPropagationReusesResultObject() {
3629 +        if (!testImplementationDetails) return;
3630 +        for (ExecutionMode m : ExecutionMode.values())
3631 +    {
3632 +        final CFException ex = new CFException();
3633 +        final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42);
3634 +        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3635 +
3636 +        List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs
3637 +            = new ArrayList<>();
3638 +
3639 +        funs.add((y) -> m.thenRun(y, new Noop(m)));
3640 +        funs.add((y) -> m.thenAccept(y, new NoopConsumer(m)));
3641 +        funs.add((y) -> m.thenApply(y, new IncFunction(m)));
3642 +
3643 +        funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m)));
3644 +        funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m)));
3645 +        funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m)));
3646 +
3647 +        funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m)));
3648 +        funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
3649 +        funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m)));
3650 +
3651 +        funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {}));
3652 +
3653 +        funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m)));
3654 +
3655 +        funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42}));
3656 +        funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete}));
3657 +
3658 +        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3659 +                 fun : funs) {
3660 +            CompletableFuture<Integer> f = new CompletableFuture<>();
3661 +            f.completeExceptionally(ex);
3662 +            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3663 +            checkCompletedWithWrappedException(src, ex);
3664 +            CompletableFuture<?> dep = fun.apply(src);
3665 +            checkCompletedWithWrappedException(dep, ex);
3666 +            assertSame(resultOf(src), resultOf(dep));
3667 +        }
3668 +
3669 +        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3670 +                 fun : funs) {
3671 +            CompletableFuture<Integer> f = new CompletableFuture<>();
3672 +            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3673 +            CompletableFuture<?> dep = fun.apply(src);
3674 +            f.completeExceptionally(ex);
3675 +            checkCompletedWithWrappedException(src, ex);
3676 +            checkCompletedWithWrappedException(dep, ex);
3677 +            assertSame(resultOf(src), resultOf(dep));
3678 +        }
3679 +
3680 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3681 +        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3682 +                 fun : funs) {
3683 +            CompletableFuture<Integer> f = new CompletableFuture<>();
3684 +            f.cancel(mayInterruptIfRunning);
3685 +            checkCancelled(f);
3686 +            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3687 +            checkCompletedWithWrappedCancellationException(src);
3688 +            CompletableFuture<?> dep = fun.apply(src);
3689 +            checkCompletedWithWrappedCancellationException(dep);
3690 +            assertSame(resultOf(src), resultOf(dep));
3691 +        }
3692 +
3693 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3694 +        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3695 +                 fun : funs) {
3696 +            CompletableFuture<Integer> f = new CompletableFuture<>();
3697 +            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3698 +            CompletableFuture<?> dep = fun.apply(src);
3699 +            f.cancel(mayInterruptIfRunning);
3700 +            checkCancelled(f);
3701 +            checkCompletedWithWrappedCancellationException(src);
3702 +            checkCompletedWithWrappedCancellationException(dep);
3703 +            assertSame(resultOf(src), resultOf(dep));
3704 +        }
3705 +    }}
3706 +
3707 +    /**
3708 +     * Minimal completion stages throw UOE for all non-CompletionStage methods
3709 +     */
3710 +    public void testMinimalCompletionStage_minimality() {
3711 +        if (!testImplementationDetails) return;
3712 +        Function<Method, String> toSignature =
3713 +            (method) -> method.getName() + Arrays.toString(method.getParameterTypes());
3714 +        Predicate<Method> isNotStatic =
3715 +            (method) -> (method.getModifiers() & Modifier.STATIC) == 0;
3716 +        List<Method> minimalMethods =
3717 +            Stream.of(Object.class, CompletionStage.class)
3718 +            .flatMap((klazz) -> Stream.of(klazz.getMethods()))
3719 +            .filter(isNotStatic)
3720 +            .collect(Collectors.toList());
3721 +        // Methods from CompletableFuture permitted NOT to throw UOE
3722 +        String[] signatureWhitelist = {
3723 +            "newIncompleteFuture[]",
3724 +            "defaultExecutor[]",
3725 +            "minimalCompletionStage[]",
3726 +            "copy[]",
3727 +        };
3728 +        Set<String> permittedMethodSignatures =
3729 +            Stream.concat(minimalMethods.stream().map(toSignature),
3730 +                          Stream.of(signatureWhitelist))
3731 +            .collect(Collectors.toSet());
3732 +        List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods())
3733 +            .filter(isNotStatic)
3734 +            .filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method)))
3735 +            .collect(Collectors.toList());
3736 +
3737 +        CompletionStage<Integer> minimalStage =
3738 +            new CompletableFuture<Integer>().minimalCompletionStage();
3739 +
3740 +        List<Method> bugs = new ArrayList<>();
3741 +        for (Method method : allMethods) {
3742 +            Class<?>[] parameterTypes = method.getParameterTypes();
3743 +            Object[] args = new Object[parameterTypes.length];
3744 +            // Manufacture boxed primitives for primitive params
3745 +            for (int i = 0; i < args.length; i++) {
3746 +                Class<?> type = parameterTypes[i];
3747 +                if (parameterTypes[i] == boolean.class)
3748 +                    args[i] = false;
3749 +                else if (parameterTypes[i] == int.class)
3750 +                    args[i] = 0;
3751 +                else if (parameterTypes[i] == long.class)
3752 +                    args[i] = 0L;
3753 +            }
3754 +            try {
3755 +                method.invoke(minimalStage, args);
3756 +                bugs.add(method);
3757 +            }
3758 +            catch (java.lang.reflect.InvocationTargetException expected) {
3759 +                if (! (expected.getCause() instanceof UnsupportedOperationException)) {
3760 +                    bugs.add(method);
3761 +                    // expected.getCause().printStackTrace();
3762 +                }
3763 +            }
3764 +            catch (ReflectiveOperationException bad) { throw new Error(bad); }
3765 +        }
3766 +        if (!bugs.isEmpty())
3767 +            throw new Error("Methods did not throw UOE: " + bugs.toString());
3768 +    }
3769 +
3770 +    static class Monad {
3771 +        static class ZeroException extends RuntimeException {
3772 +            public ZeroException() { super("monadic zero"); }
3773 +        }
3774 +        // "return", "unit"
3775 +        static <T> CompletableFuture<T> unit(T value) {
3776 +            return completedFuture(value);
3777 +        }
3778 +        // monadic zero ?
3779 +        static <T> CompletableFuture<T> zero() {
3780 +            return failedFuture(new ZeroException());
3781 +        }
3782 +        // >=>
3783 +        static <T,U,V> Function<T, CompletableFuture<V>> compose
3784 +            (Function<T, CompletableFuture<U>> f,
3785 +             Function<U, CompletableFuture<V>> g) {
3786 +            return (x) -> f.apply(x).thenCompose(g);
3787 +        }
3788 +
3789 +        static void assertZero(CompletableFuture<?> f) {
3790 +            try {
3791 +                f.getNow(null);
3792 +                throw new AssertionFailedError("should throw");
3793 +            } catch (CompletionException success) {
3794 +                assertTrue(success.getCause() instanceof ZeroException);
3795 +            }
3796 +        }
3797 +
3798 +        static <T> void assertFutureEquals(CompletableFuture<T> f,
3799 +                                           CompletableFuture<T> g) {
3800 +            T fval = null, gval = null;
3801 +            Throwable fex = null, gex = null;
3802 +
3803 +            try { fval = f.get(); }
3804 +            catch (ExecutionException ex) { fex = ex.getCause(); }
3805 +            catch (Throwable ex) { fex = ex; }
3806 +
3807 +            try { gval = g.get(); }
3808 +            catch (ExecutionException ex) { gex = ex.getCause(); }
3809 +            catch (Throwable ex) { gex = ex; }
3810 +
3811 +            if (fex != null || gex != null)
3812 +                assertSame(fex.getClass(), gex.getClass());
3813 +            else
3814 +                assertEquals(fval, gval);
3815 +        }
3816 +
3817 +        static class PlusFuture<T> extends CompletableFuture<T> {
3818 +            AtomicReference<Throwable> firstFailure = new AtomicReference<>(null);
3819 +        }
3820 +
3821 +        // Monadic "plus"
3822 +        static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f,
3823 +                                             CompletableFuture<? extends T> g) {
3824 +            PlusFuture<T> plus = new PlusFuture<T>();
3825 +            BiConsumer<T, Throwable> action = (T result, Throwable ex) -> {
3826 +                if (ex == null) {
3827 +                    if (plus.complete(result))
3828 +                        if (plus.firstFailure.get() != null)
3829 +                            plus.firstFailure.set(null);
3830 +                }
3831 +                else if (plus.firstFailure.compareAndSet(null, ex)) {
3832 +                    if (plus.isDone())
3833 +                        plus.firstFailure.set(null);
3834 +                }
3835 +                else {
3836 +                    // first failure has precedence
3837 +                    Throwable first = plus.firstFailure.getAndSet(null);
3838 +
3839 +                    // may fail with "Self-suppression not permitted"
3840 +                    try { first.addSuppressed(ex); }
3841 +                    catch (Exception ignored) {}
3842 +
3843 +                    plus.completeExceptionally(first);
3844 +                }
3845 +            };
3846 +            f.whenComplete(action);
3847 +            g.whenComplete(action);
3848 +            return plus;
3849 +        }
3850 +    }
3851 +
3852 +    /**
3853 +     * CompletableFuture is an additive monad - sort of.
3854 +     * https://en.wikipedia.org/wiki/Monad_(functional_programming)#Additive_monads
3855 +     */
3856 +    public void testAdditiveMonad() throws Throwable {
3857 +        Function<Long, CompletableFuture<Long>> unit = Monad::unit;
3858 +        CompletableFuture<Long> zero = Monad.zero();
3859 +
3860 +        // Some mutually non-commutative functions
3861 +        Function<Long, CompletableFuture<Long>> triple
3862 +            = (x) -> Monad.unit(3 * x);
3863 +        Function<Long, CompletableFuture<Long>> inc
3864 +            = (x) -> Monad.unit(x + 1);
3865 +
3866 +        // unit is a right identity: m >>= unit === m
3867 +        Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit),
3868 +                                 inc.apply(5L));
3869 +        // unit is a left identity: (unit x) >>= f === f x
3870 +        Monad.assertFutureEquals(unit.apply(5L).thenCompose(inc),
3871 +                                 inc.apply(5L));
3872 +
3873 +        // associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) )
3874 +        Monad.assertFutureEquals(
3875 +            unit.apply(5L).thenCompose(inc).thenCompose(triple),
3876 +            unit.apply(5L).thenCompose((x) -> inc.apply(x).thenCompose(triple)));
3877 +
3878 +        // The case for CompletableFuture as an additive monad is weaker...
3879 +
3880 +        // zero is a monadic zero
3881 +        Monad.assertZero(zero);
3882 +
3883 +        // left zero: zero >>= f === zero
3884 +        Monad.assertZero(zero.thenCompose(inc));
3885 +        // right zero: f >>= (\x -> zero) === zero
3886 +        Monad.assertZero(inc.apply(5L).thenCompose((x) -> zero));
3887 +
3888 +        // f plus zero === f
3889 +        Monad.assertFutureEquals(Monad.unit(5L),
3890 +                                 Monad.plus(Monad.unit(5L), zero));
3891 +        // zero plus f === f
3892 +        Monad.assertFutureEquals(Monad.unit(5L),
3893 +                                 Monad.plus(zero, Monad.unit(5L)));
3894 +        // zero plus zero === zero
3895 +        Monad.assertZero(Monad.plus(zero, zero));
3896 +        {
3897 +            CompletableFuture<Long> f = Monad.plus(Monad.unit(5L),
3898 +                                                   Monad.unit(8L));
3899 +            // non-determinism
3900 +            assertTrue(f.get() == 5L || f.get() == 8L);
3901 +        }
3902 +
3903 +        CompletableFuture<Long> godot = new CompletableFuture<>();
3904 +        // f plus godot === f (doesn't wait for godot)
3905 +        Monad.assertFutureEquals(Monad.unit(5L),
3906 +                                 Monad.plus(Monad.unit(5L), godot));
3907 +        // godot plus f === f (doesn't wait for godot)
3908 +        Monad.assertFutureEquals(Monad.unit(5L),
3909 +                                 Monad.plus(godot, Monad.unit(5L)));
3910 +    }
3911 +
3912 + //     static <U> U join(CompletionStage<U> stage) {
3913 + //         CompletableFuture<U> f = new CompletableFuture<>();
3914 + //         stage.whenComplete((v, ex) -> {
3915 + //             if (ex != null) f.completeExceptionally(ex); else f.complete(v);
3916 + //         });
3917 + //         return f.join();
3918 + //     }
3919 +
3920 + //     static <U> boolean isDone(CompletionStage<U> stage) {
3921 + //         CompletableFuture<U> f = new CompletableFuture<>();
3922 + //         stage.whenComplete((v, ex) -> {
3923 + //             if (ex != null) f.completeExceptionally(ex); else f.complete(v);
3924 + //         });
3925 + //         return f.isDone();
3926 + //     }
3927 +
3928 + //     static <U> U join2(CompletionStage<U> stage) {
3929 + //         return stage.toCompletableFuture().copy().join();
3930 + //     }
3931 +
3932 + //     static <U> boolean isDone2(CompletionStage<U> stage) {
3933 + //         return stage.toCompletableFuture().copy().isDone();
3934 + //     }
3935 +
3936   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines