ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
(Generate patch)

Comparing jsr166/src/test/tck/CompletableFutureTest.java (file contents):
Revision 1.33 by jsr166, Sun Jun 1 20:40:13 2014 UTC vs.
Revision 1.123 by jsr166, Tue Sep 8 19:45:35 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines