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.34 by jsr166, Sun Jun 1 21:17:05 2014 UTC vs.
Revision 1.135 by jsr166, Sun Nov 15 20:03:08 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines