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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines