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

Comparing jsr166/src/test/tck/CompletableFutureTest.java (file contents):
Revision 1.34 by jsr166, Sun Jun 1 21:17:05 2014 UTC vs.
Revision 1.201 by jsr166, Sat Sep 22 21:04:48 2018 UTC

# Line 5 | Line 5
5   * http://creativecommons.org/publicdomain/zero/1.0/
6   */
7  
8 < import junit.framework.*;
8 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
9 > import static java.util.concurrent.TimeUnit.SECONDS;
10 > import static java.util.concurrent.CompletableFuture.completedFuture;
11 > import static java.util.concurrent.CompletableFuture.failedFuture;
12 >
13 > import java.lang.reflect.Method;
14 > import java.lang.reflect.Modifier;
15 >
16 > import java.util.stream.Collectors;
17 > import java.util.stream.Stream;
18 >
19 > import java.util.ArrayList;
20 > import java.util.Arrays;
21 > import java.util.List;
22 > import java.util.Objects;
23 > import java.util.Set;
24   import java.util.concurrent.Callable;
10 import java.util.concurrent.Executor;
11 import java.util.concurrent.ExecutorService;
12 import java.util.concurrent.Executors;
25   import java.util.concurrent.CancellationException;
14 import java.util.concurrent.CountDownLatch;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.Future;
26   import java.util.concurrent.CompletableFuture;
27   import java.util.concurrent.CompletionException;
28 + import java.util.concurrent.CompletionStage;
29 + import java.util.concurrent.ExecutionException;
30 + import java.util.concurrent.Executor;
31 + import java.util.concurrent.ForkJoinPool;
32 + import java.util.concurrent.ForkJoinTask;
33 + import java.util.concurrent.RejectedExecutionException;
34   import java.util.concurrent.TimeoutException;
35   import java.util.concurrent.atomic.AtomicInteger;
36 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
22 < import static java.util.concurrent.TimeUnit.SECONDS;
23 < import java.util.*;
24 < import java.util.function.Supplier;
25 < import java.util.function.Consumer;
36 > import java.util.concurrent.atomic.AtomicReference;
37   import java.util.function.BiConsumer;
27 import java.util.function.Function;
38   import java.util.function.BiFunction;
39 + import java.util.function.Consumer;
40 + import java.util.function.Function;
41 + import java.util.function.Predicate;
42 + import java.util.function.Supplier;
43 +
44 + import junit.framework.Test;
45 + import junit.framework.TestSuite;
46  
47   public class CompletableFutureTest extends JSR166TestCase {
48  
49      public static void main(String[] args) {
50 <        junit.textui.TestRunner.run(suite());
50 >        main(suite(), args);
51      }
52      public static Test suite() {
53          return new TestSuite(CompletableFutureTest.class);
# Line 41 | Line 58 | public class CompletableFutureTest exten
58      void checkIncomplete(CompletableFuture<?> f) {
59          assertFalse(f.isDone());
60          assertFalse(f.isCancelled());
61 <        assertTrue(f.toString().contains("[Not completed]"));
61 >        assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]"));
62 >
63 >        Object result = null;
64          try {
65 <            assertNull(f.getNow(null));
65 >            result = f.getNow(null);
66          } catch (Throwable fail) { threadUnexpectedException(fail); }
67 +        assertNull(result);
68 +
69          try {
70 <            f.get(0L, SECONDS);
70 >            f.get(randomExpiredTimeout(), randomTimeUnit());
71              shouldThrow();
72          }
73          catch (TimeoutException success) {}
74          catch (Throwable fail) { threadUnexpectedException(fail); }
75      }
76  
77 <    <T> void checkCompletedNormally(CompletableFuture<T> f, T value) {
78 <        try {
79 <            assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS));
80 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
81 <        try {
82 <            assertEquals(value, f.join());
83 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
63 <        try {
64 <            assertEquals(value, f.getNow(null));
65 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
77 >    <T> void checkCompletedNormally(CompletableFuture<T> f, T expectedValue) {
78 >        checkTimedGet(f, expectedValue);
79 >
80 >        assertEquals(expectedValue, f.join());
81 >        assertEquals(expectedValue, f.getNow(null));
82 >
83 >        T result = null;
84          try {
85 <            assertEquals(value, f.get());
85 >            result = f.get();
86          } catch (Throwable fail) { threadUnexpectedException(fail); }
87 +        assertEquals(expectedValue, result);
88 +
89          assertTrue(f.isDone());
90          assertFalse(f.isCancelled());
91          assertFalse(f.isCompletedExceptionally());
92 <        assertTrue(f.toString().contains("[Completed normally]"));
92 >        assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]"));
93      }
94  
95 <    void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
96 <        try {
97 <            f.get(LONG_DELAY_MS, MILLISECONDS);
98 <            shouldThrow();
99 <        } catch (ExecutionException success) {
100 <            assertTrue(success.getCause() instanceof CFException);
101 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
102 <        try {
83 <            f.join();
84 <            shouldThrow();
85 <        } catch (CompletionException success) {
86 <            assertTrue(success.getCause() instanceof CFException);
87 <        }
88 <        try {
89 <            f.getNow(null);
90 <            shouldThrow();
91 <        } catch (CompletionException success) {
92 <            assertTrue(success.getCause() instanceof CFException);
93 <        }
94 <        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]"));
95 >    /**
96 >     * Returns the "raw" internal exceptional completion of f,
97 >     * without any additional wrapping with CompletionException.
98 >     */
99 >    Throwable exceptionalCompletion(CompletableFuture<?> f) {
100 >        // handle (and whenComplete and exceptionally) can distinguish
101 >        // between "direct" and "wrapped" exceptional completion
102 >        return f.handle((u, t) -> t).join();
103      }
104  
105 <    void checkCompletedWithWrappedCFException(CompletableFuture<?> f,
106 <                                              CFException ex) {
105 >    void checkCompletedExceptionally(CompletableFuture<?> f,
106 >                                     boolean wrapped,
107 >                                     Consumer<Throwable> checker) {
108 >        Throwable cause = exceptionalCompletion(f);
109 >        if (wrapped) {
110 >            assertTrue(cause instanceof CompletionException);
111 >            cause = cause.getCause();
112 >        }
113 >        checker.accept(cause);
114 >
115 >        long startTime = System.nanoTime();
116          try {
117              f.get(LONG_DELAY_MS, MILLISECONDS);
118              shouldThrow();
119          } catch (ExecutionException success) {
120 <            assertSame(ex, success.getCause());
120 >            assertSame(cause, success.getCause());
121          } catch (Throwable fail) { threadUnexpectedException(fail); }
122 +        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
123 +
124          try {
125              f.join();
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.getNow(null);
133              shouldThrow();
134          } catch (CompletionException success) {
135 <            assertSame(ex, success.getCause());
136 <        }
135 >            assertSame(cause, success.getCause());
136 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
137 >
138          try {
139              f.get();
140              shouldThrow();
141          } catch (ExecutionException success) {
142 <            assertSame(ex, success.getCause());
142 >            assertSame(cause, success.getCause());
143          } catch (Throwable fail) { threadUnexpectedException(fail); }
144 <        assertTrue(f.isDone());
144 >
145          assertFalse(f.isCancelled());
146 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
146 >        assertTrue(f.isDone());
147 >        assertTrue(f.isCompletedExceptionally());
148 >        assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
149 >    }
150 >
151 >    void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
152 >        checkCompletedExceptionally(f, true,
153 >            t -> assertTrue(t instanceof CFException));
154 >    }
155 >
156 >    void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
157 >        checkCompletedExceptionally(f, true,
158 >            t -> assertTrue(t instanceof CancellationException));
159 >    }
160 >
161 >    void checkCompletedWithTimeoutException(CompletableFuture<?> f) {
162 >        checkCompletedExceptionally(f, false,
163 >            t -> assertTrue(t instanceof TimeoutException));
164 >    }
165 >
166 >    void checkCompletedWithWrappedException(CompletableFuture<?> f,
167 >                                            Throwable ex) {
168 >        checkCompletedExceptionally(f, true, t -> assertSame(t, ex));
169 >    }
170 >
171 >    void checkCompletedExceptionally(CompletableFuture<?> f, Throwable ex) {
172 >        checkCompletedExceptionally(f, false, t -> assertSame(t, ex));
173      }
174  
175      void checkCancelled(CompletableFuture<?> f) {
176 +        long startTime = System.nanoTime();
177          try {
178              f.get(LONG_DELAY_MS, MILLISECONDS);
179              shouldThrow();
180          } catch (CancellationException success) {
181          } catch (Throwable fail) { threadUnexpectedException(fail); }
182 +        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
183 +
184          try {
185              f.join();
186              shouldThrow();
# Line 152 | Line 194 | public class CompletableFutureTest exten
194              shouldThrow();
195          } catch (CancellationException success) {
196          } 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    }
197  
198 <    void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
199 <        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); }
198 >        assertTrue(exceptionalCompletion(f) instanceof CancellationException);
199 >
200          assertTrue(f.isDone());
187        assertFalse(f.isCancelled());
201          assertTrue(f.isCompletedExceptionally());
202 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
202 >        assertTrue(f.isCancelled());
203 >        assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
204      }
205  
206      /**
# Line 203 | Line 217 | public class CompletableFutureTest exten
217       * isCancelled, join, get, and getNow
218       */
219      public void testComplete() {
220 +        for (Integer v1 : new Integer[] { 1, null })
221 +    {
222          CompletableFuture<Integer> f = new CompletableFuture<>();
223          checkIncomplete(f);
224 <        f.complete(one);
225 <        checkCompletedNormally(f, one);
226 <    }
224 >        assertTrue(f.complete(v1));
225 >        assertFalse(f.complete(v1));
226 >        checkCompletedNormally(f, v1);
227 >    }}
228  
229      /**
230       * completeExceptionally completes exceptionally, as indicated by
# Line 215 | Line 232 | public class CompletableFutureTest exten
232       */
233      public void testCompleteExceptionally() {
234          CompletableFuture<Integer> f = new CompletableFuture<>();
235 +        CFException ex = new CFException();
236          checkIncomplete(f);
237 <        f.completeExceptionally(new CFException());
238 <        checkCompletedWithWrappedCFException(f);
237 >        f.completeExceptionally(ex);
238 >        checkCompletedExceptionally(f, ex);
239      }
240  
241      /**
# Line 225 | Line 243 | public class CompletableFutureTest exten
243       * methods isDone, isCancelled, join, get, and getNow
244       */
245      public void testCancel() {
246 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
247 +    {
248          CompletableFuture<Integer> f = new CompletableFuture<>();
249          checkIncomplete(f);
250 <        assertTrue(f.cancel(true));
250 >        assertTrue(f.cancel(mayInterruptIfRunning));
251 >        assertTrue(f.cancel(mayInterruptIfRunning));
252 >        assertTrue(f.cancel(!mayInterruptIfRunning));
253          checkCancelled(f);
254 <    }
254 >    }}
255  
256      /**
257       * obtrudeValue forces completion with given value
# Line 237 | Line 259 | public class CompletableFutureTest exten
259      public void testObtrudeValue() {
260          CompletableFuture<Integer> f = new CompletableFuture<>();
261          checkIncomplete(f);
262 <        f.complete(one);
262 >        assertTrue(f.complete(one));
263          checkCompletedNormally(f, one);
264          f.obtrudeValue(three);
265          checkCompletedNormally(f, three);
# Line 246 | Line 268 | public class CompletableFutureTest exten
268          f = new CompletableFuture<>();
269          f.obtrudeValue(three);
270          checkCompletedNormally(f, three);
271 +        f.obtrudeValue(null);
272 +        checkCompletedNormally(f, null);
273          f = new CompletableFuture<>();
274          f.completeExceptionally(new CFException());
275          f.obtrudeValue(four);
# Line 256 | Line 280 | public class CompletableFutureTest exten
280       * obtrudeException forces completion with given exception
281       */
282      public void testObtrudeException() {
283 <        CompletableFuture<Integer> f = new CompletableFuture<>();
284 <        checkIncomplete(f);
285 <        f.complete(one);
286 <        checkCompletedNormally(f, one);
287 <        f.obtrudeException(new CFException());
288 <        checkCompletedWithWrappedCFException(f);
283 >        for (Integer v1 : new Integer[] { 1, null })
284 >    {
285 >        CFException ex;
286 >        CompletableFuture<Integer> f;
287 >
288 >        f = new CompletableFuture<>();
289 >        assertTrue(f.complete(v1));
290 >        for (int i = 0; i < 2; i++) {
291 >            f.obtrudeException(ex = new CFException());
292 >            checkCompletedExceptionally(f, ex);
293 >        }
294 >
295          f = new CompletableFuture<>();
296 <        f.obtrudeException(new CFException());
297 <        checkCompletedWithWrappedCFException(f);
296 >        for (int i = 0; i < 2; i++) {
297 >            f.obtrudeException(ex = new CFException());
298 >            checkCompletedExceptionally(f, ex);
299 >        }
300 >
301          f = new CompletableFuture<>();
302          f.completeExceptionally(new CFException());
303 <        f.obtrudeValue(four);
304 <        checkCompletedNormally(f, four);
305 <        f.obtrudeException(new CFException());
306 <        checkCompletedWithWrappedCFException(f);
307 <    }
303 >        f.obtrudeValue(v1);
304 >        checkCompletedNormally(f, v1);
305 >        f.obtrudeException(ex = new CFException());
306 >        checkCompletedExceptionally(f, ex);
307 >        f.completeExceptionally(new CFException());
308 >        checkCompletedExceptionally(f, ex);
309 >        assertFalse(f.complete(v1));
310 >        checkCompletedExceptionally(f, ex);
311 >    }}
312  
313      /**
314       * getNumberOfDependents returns number of dependent tasks
315       */
316      public void testGetNumberOfDependents() {
317 +        for (ExecutionMode m : ExecutionMode.values())
318 +        for (Integer v1 : new Integer[] { 1, null })
319 +    {
320          CompletableFuture<Integer> f = new CompletableFuture<>();
321 <        assertEquals(f.getNumberOfDependents(), 0);
322 <        CompletableFuture g = f.thenRun(new Noop());
323 <        assertEquals(f.getNumberOfDependents(), 1);
324 <        assertEquals(g.getNumberOfDependents(), 0);
325 <        CompletableFuture h = f.thenRun(new Noop());
326 <        assertEquals(f.getNumberOfDependents(), 2);
327 <        f.complete(1);
321 >        assertEquals(0, f.getNumberOfDependents());
322 >        final CompletableFuture<Void> g = m.thenRun(f, new Noop(m));
323 >        assertEquals(1, f.getNumberOfDependents());
324 >        assertEquals(0, g.getNumberOfDependents());
325 >        final CompletableFuture<Void> h = m.thenRun(f, new Noop(m));
326 >        assertEquals(2, f.getNumberOfDependents());
327 >        assertEquals(0, h.getNumberOfDependents());
328 >        assertTrue(f.complete(v1));
329          checkCompletedNormally(g, null);
330 <        assertEquals(f.getNumberOfDependents(), 0);
331 <        assertEquals(g.getNumberOfDependents(), 0);
332 <    }
330 >        checkCompletedNormally(h, null);
331 >        assertEquals(0, f.getNumberOfDependents());
332 >        assertEquals(0, g.getNumberOfDependents());
333 >        assertEquals(0, h.getNumberOfDependents());
334 >    }}
335  
336      /**
337       * toString indicates current completion state
338       */
339 <    public void testToString() {
340 <        CompletableFuture<String> f;
341 <
342 <        f = new CompletableFuture<String>();
343 <        assertTrue(f.toString().contains("[Not completed]"));
344 <
345 <        f.complete("foo");
346 <        assertTrue(f.toString().contains("[Completed normally]"));
347 <
348 <        f = new CompletableFuture<String>();
349 <        f.completeExceptionally(new IndexOutOfBoundsException());
350 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
339 >    public void testToString_incomplete() {
340 >        CompletableFuture<String> f = new CompletableFuture<>();
341 >        assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]"));
342 >        if (testImplementationDetails)
343 >            assertEquals(identityString(f) + "[Not completed]",
344 >                         f.toString());
345 >    }
346 >
347 >    public void testToString_normal() {
348 >        CompletableFuture<String> f = new CompletableFuture<>();
349 >        assertTrue(f.complete("foo"));
350 >        assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]"));
351 >        if (testImplementationDetails)
352 >            assertEquals(identityString(f) + "[Completed normally]",
353 >                         f.toString());
354 >    }
355 >
356 >    public void testToString_exception() {
357 >        CompletableFuture<String> f = new CompletableFuture<>();
358 >        assertTrue(f.completeExceptionally(new IndexOutOfBoundsException()));
359 >        assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
360 >        if (testImplementationDetails)
361 >            assertTrue(f.toString().startsWith(
362 >                               identityString(f) + "[Completed exceptionally: "));
363 >    }
364 >
365 >    public void testToString_cancelled() {
366 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
367 >            CompletableFuture<String> f = new CompletableFuture<>();
368 >            assertTrue(f.cancel(mayInterruptIfRunning));
369 >            assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
370 >            if (testImplementationDetails)
371 >                assertTrue(f.toString().startsWith(
372 >                                   identityString(f) + "[Completed exceptionally: "));
373 >        }
374      }
375  
376      /**
# Line 315 | Line 381 | public class CompletableFutureTest exten
381          checkCompletedNormally(f, "test");
382      }
383  
384 <    // Choose non-commutative actions for better coverage
384 >    abstract static class CheckedAction {
385 >        int invocationCount = 0;
386 >        final ExecutionMode m;
387 >        CheckedAction(ExecutionMode m) { this.m = m; }
388 >        void invoked() {
389 >            m.checkExecutionMode();
390 >            assertEquals(0, invocationCount++);
391 >        }
392 >        void assertNotInvoked() { assertEquals(0, invocationCount); }
393 >        void assertInvoked() { assertEquals(1, invocationCount); }
394 >    }
395  
396 <    static final Supplier<Integer> supplyOne =
397 <        () -> Integer.valueOf(1);
398 <    static final Function<Integer, Integer> inc =
399 <        (Integer x) -> Integer.valueOf(x.intValue() + 1);
400 <    static final BiFunction<Integer, Integer, Integer> subtract =
401 <        (Integer x, Integer y) -> Integer.valueOf(x.intValue() - y.intValue());
402 <    static final class IncAction implements Consumer<Integer> {
327 <        int value;
328 <        public void accept(Integer x) { value = x.intValue() + 1; }
396 >    abstract static class CheckedIntegerAction extends CheckedAction {
397 >        Integer value;
398 >        CheckedIntegerAction(ExecutionMode m) { super(m); }
399 >        void assertValue(Integer expected) {
400 >            assertInvoked();
401 >            assertEquals(expected, value);
402 >        }
403      }
404 <    static final class SubtractAction implements BiConsumer<Integer, Integer> {
405 <        int value;
404 >
405 >    static class IntegerSupplier extends CheckedAction
406 >        implements Supplier<Integer>
407 >    {
408 >        final Integer value;
409 >        IntegerSupplier(ExecutionMode m, Integer value) {
410 >            super(m);
411 >            this.value = value;
412 >        }
413 >        public Integer get() {
414 >            invoked();
415 >            return value;
416 >        }
417 >    }
418 >
419 >    // A function that handles and produces null values as well.
420 >    static Integer inc(Integer x) {
421 >        return (x == null) ? null : x + 1;
422 >    }
423 >
424 >    static class NoopConsumer extends CheckedIntegerAction
425 >        implements Consumer<Integer>
426 >    {
427 >        NoopConsumer(ExecutionMode m) { super(m); }
428 >        public void accept(Integer x) {
429 >            invoked();
430 >            value = x;
431 >        }
432 >    }
433 >
434 >    static class IncFunction extends CheckedIntegerAction
435 >        implements Function<Integer,Integer>
436 >    {
437 >        IncFunction(ExecutionMode m) { super(m); }
438 >        public Integer apply(Integer x) {
439 >            invoked();
440 >            return value = inc(x);
441 >        }
442 >    }
443 >
444 >    // Choose non-commutative actions for better coverage
445 >    // A non-commutative function that handles and produces null values as well.
446 >    static Integer subtract(Integer x, Integer y) {
447 >        return (x == null && y == null) ? null :
448 >            ((x == null) ? 42 : x.intValue())
449 >            - ((y == null) ? 99 : y.intValue());
450 >    }
451 >
452 >    static class SubtractAction extends CheckedIntegerAction
453 >        implements BiConsumer<Integer, Integer>
454 >    {
455 >        SubtractAction(ExecutionMode m) { super(m); }
456          public void accept(Integer x, Integer y) {
457 <            value = x.intValue() - y.intValue();
457 >            invoked();
458 >            value = subtract(x, y);
459          }
460      }
461 <    static final class Noop implements Runnable {
462 <        boolean ran;
463 <        public void run() { ran = true; }
461 >
462 >    static class SubtractFunction extends CheckedIntegerAction
463 >        implements BiFunction<Integer, Integer, Integer>
464 >    {
465 >        SubtractFunction(ExecutionMode m) { super(m); }
466 >        public Integer apply(Integer x, Integer y) {
467 >            invoked();
468 >            return value = subtract(x, y);
469 >        }
470 >    }
471 >
472 >    static class Noop extends CheckedAction implements Runnable {
473 >        Noop(ExecutionMode m) { super(m); }
474 >        public void run() {
475 >            invoked();
476 >        }
477      }
478  
479 <    static final class FailingSupplier implements Supplier<Integer> {
480 <        boolean ran;
481 <        public Integer get() { ran = true; throw new CFException(); }
479 >    static class FailingSupplier extends CheckedAction
480 >        implements Supplier<Integer>
481 >    {
482 >        final CFException ex;
483 >        FailingSupplier(ExecutionMode m) { super(m); ex = new CFException(); }
484 >        public Integer get() {
485 >            invoked();
486 >            throw ex;
487 >        }
488      }
489 <    static final class FailingConsumer implements Consumer<Integer> {
490 <        boolean ran;
491 <        public void accept(Integer x) { ran = true; throw new CFException(); }
489 >
490 >    static class FailingConsumer extends CheckedIntegerAction
491 >        implements Consumer<Integer>
492 >    {
493 >        final CFException ex;
494 >        FailingConsumer(ExecutionMode m) { super(m); ex = new CFException(); }
495 >        public void accept(Integer x) {
496 >            invoked();
497 >            value = x;
498 >            throw ex;
499 >        }
500      }
501 <    static final class FailingBiConsumer implements BiConsumer<Integer, Integer> {
502 <        boolean ran;
503 <        public void accept(Integer x, Integer y) { ran = true; throw new CFException(); }
501 >
502 >    static class FailingBiConsumer extends CheckedIntegerAction
503 >        implements BiConsumer<Integer, Integer>
504 >    {
505 >        final CFException ex;
506 >        FailingBiConsumer(ExecutionMode m) { super(m); ex = new CFException(); }
507 >        public void accept(Integer x, Integer y) {
508 >            invoked();
509 >            value = subtract(x, y);
510 >            throw ex;
511 >        }
512      }
513 <    static final class FailingFunction implements Function<Integer, Integer> {
514 <        boolean ran;
515 <        public Integer apply(Integer x) { ran = true; throw new CFException(); }
513 >
514 >    static class FailingFunction extends CheckedIntegerAction
515 >        implements Function<Integer, Integer>
516 >    {
517 >        final CFException ex;
518 >        FailingFunction(ExecutionMode m) { super(m); ex = new CFException(); }
519 >        public Integer apply(Integer x) {
520 >            invoked();
521 >            value = x;
522 >            throw ex;
523 >        }
524      }
525 <    static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> {
526 <        boolean ran;
527 <        public Integer apply(Integer x, Integer y) { ran = true; throw new CFException(); }
525 >
526 >    static class FailingBiFunction extends CheckedIntegerAction
527 >        implements BiFunction<Integer, Integer, Integer>
528 >    {
529 >        final CFException ex;
530 >        FailingBiFunction(ExecutionMode m) { super(m); ex = new CFException(); }
531 >        public Integer apply(Integer x, Integer y) {
532 >            invoked();
533 >            value = subtract(x, y);
534 >            throw ex;
535 >        }
536      }
537 <    static final class FailingNoop implements Runnable {
538 <        boolean ran;
539 <        public void run() { ran = true; throw new CFException(); }
537 >
538 >    static class FailingRunnable extends CheckedAction implements Runnable {
539 >        final CFException ex;
540 >        FailingRunnable(ExecutionMode m) { super(m); ex = new CFException(); }
541 >        public void run() {
542 >            invoked();
543 >            throw ex;
544 >        }
545      }
546  
547 <    static final class CompletableFutureInc
548 <        implements Function<Integer, CompletableFuture<Integer>> {
549 <        boolean ran;
547 >    static class CompletableFutureInc extends CheckedIntegerAction
548 >        implements Function<Integer, CompletableFuture<Integer>>
549 >    {
550 >        CompletableFutureInc(ExecutionMode m) { super(m); }
551          public CompletableFuture<Integer> apply(Integer x) {
552 <            ran = true;
552 >            invoked();
553 >            value = x;
554              CompletableFuture<Integer> f = new CompletableFuture<>();
555 <            f.complete(Integer.valueOf(x.intValue() + 1));
555 >            assertTrue(f.complete(inc(x)));
556              return f;
557          }
558      }
559  
560 <    static final class FailingCompletableFutureFunction
561 <        implements Function<Integer, CompletableFuture<Integer>> {
562 <        boolean ran;
563 <        public CompletableFuture<Integer> apply(Integer x) {
564 <            ran = true; throw new CFException();
560 >    static class FailingExceptionalCompletableFutureFunction extends CheckedAction
561 >        implements Function<Throwable, CompletableFuture<Integer>>
562 >    {
563 >        final CFException ex;
564 >        FailingExceptionalCompletableFutureFunction(ExecutionMode m) { super(m); ex = new CFException(); }
565 >        public CompletableFuture<Integer> apply(Throwable x) {
566 >            invoked();
567 >            throw ex;
568          }
569      }
570  
571 <    // Used for explicit executor tests
572 <    static final class ThreadExecutor implements Executor {
573 <        AtomicInteger count = new AtomicInteger(0);
574 <
575 <        public void execute(Runnable r) {
576 <            count.getAndIncrement();
577 <            new Thread(r).start();
571 >    static class ExceptionalCompletableFutureFunction extends CheckedAction
572 >        implements Function<Throwable, CompletionStage<Integer>> {
573 >        final Integer value = 3;
574 >        ExceptionalCompletableFutureFunction(ExecutionMode m) { super(m); }
575 >        public CompletionStage<Integer> apply(Throwable x) {
576 >            invoked();
577 >            CompletableFuture<Integer> d = new CompletableFuture<Integer>();
578 >            d.complete(value);
579 >            return d;
580          }
581      }
582  
583 <    static final class ExceptionToInteger implements Function<Throwable, Integer> {
584 <        public Integer apply(Throwable x) { return Integer.valueOf(3); }
583 >    static class FailingCompletableFutureFunction extends CheckedIntegerAction
584 >        implements Function<Integer, CompletableFuture<Integer>>
585 >    {
586 >        final CFException ex;
587 >        FailingCompletableFutureFunction(ExecutionMode m) { super(m); ex = new CFException(); }
588 >        public CompletableFuture<Integer> apply(Integer x) {
589 >            invoked();
590 >            value = x;
591 >            throw ex;
592 >        }
593      }
594  
595 <    static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> {
596 <        boolean ran;
597 <        public Integer apply(Integer x, Throwable t) {
598 <            ran = true;
599 <            return (t == null) ? two : three;
595 >    static class CountingRejectingExecutor implements Executor {
596 >        final RejectedExecutionException ex = new RejectedExecutionException();
597 >        final AtomicInteger count = new AtomicInteger(0);
598 >        public void execute(Runnable r) {
599 >            count.getAndIncrement();
600 >            throw ex;
601          }
602      }
603  
604 <    /**
605 <     * exceptionally action completes with function value on source
606 <     * exception; otherwise with source value
607 <     */
608 <    public void testExceptionally() {
609 <        CompletableFuture<Integer> f = new CompletableFuture<>();
610 <        ExceptionToInteger r = new ExceptionToInteger();
414 <        CompletableFuture<Integer> g = f.exceptionally(r);
415 <        f.completeExceptionally(new CFException());
416 <        checkCompletedNormally(g, three);
604 >    // Used for explicit executor tests
605 >    static final class ThreadExecutor implements Executor {
606 >        final AtomicInteger count = new AtomicInteger(0);
607 >        static final ThreadGroup tg = new ThreadGroup("ThreadExecutor");
608 >        static boolean startedCurrentThread() {
609 >            return Thread.currentThread().getThreadGroup() == tg;
610 >        }
611  
612 <        f = new CompletableFuture<>();
613 <        r = new ExceptionToInteger();
614 <        g = f.exceptionally(r);
615 <        f.complete(one);
422 <        checkCompletedNormally(g, one);
612 >        public void execute(Runnable r) {
613 >            count.getAndIncrement();
614 >            new Thread(tg, r).start();
615 >        }
616      }
617  
618 +    static final boolean defaultExecutorIsCommonPool
619 +        = ForkJoinPool.getCommonPoolParallelism() > 1;
620 +
621      /**
622 <     * handle action completes normally with function value on either
623 <     * normal or exceptional completion of source
622 >     * Permits the testing of parallel code for the 3 different
623 >     * execution modes without copy/pasting all the test methods.
624       */
625 <    public void testHandle() {
626 <        CompletableFuture<Integer> f, g;
627 <        IntegerHandler r;
625 >    enum ExecutionMode {
626 >        SYNC {
627 >            public void checkExecutionMode() {
628 >                assertFalse(ThreadExecutor.startedCurrentThread());
629 >                assertNull(ForkJoinTask.getPool());
630 >            }
631 >            public CompletableFuture<Void> runAsync(Runnable a) {
632 >                throw new UnsupportedOperationException();
633 >            }
634 >            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
635 >                throw new UnsupportedOperationException();
636 >            }
637 >            public <T> CompletableFuture<Void> thenRun
638 >                (CompletableFuture<T> f, Runnable a) {
639 >                return f.thenRun(a);
640 >            }
641 >            public <T> CompletableFuture<Void> thenAccept
642 >                (CompletableFuture<T> f, Consumer<? super T> a) {
643 >                return f.thenAccept(a);
644 >            }
645 >            public <T,U> CompletableFuture<U> thenApply
646 >                (CompletableFuture<T> f, Function<? super T,U> a) {
647 >                return f.thenApply(a);
648 >            }
649 >            public <T,U> CompletableFuture<U> thenCompose
650 >                (CompletableFuture<T> f,
651 >                 Function<? super T,? extends CompletionStage<U>> a) {
652 >                return f.thenCompose(a);
653 >            }
654 >            public <T,U> CompletableFuture<U> handle
655 >                (CompletableFuture<T> f,
656 >                 BiFunction<? super T,Throwable,? extends U> a) {
657 >                return f.handle(a);
658 >            }
659 >            public <T> CompletableFuture<T> whenComplete
660 >                (CompletableFuture<T> f,
661 >                 BiConsumer<? super T,? super Throwable> a) {
662 >                return f.whenComplete(a);
663 >            }
664 >            public <T,U> CompletableFuture<Void> runAfterBoth
665 >                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
666 >                return f.runAfterBoth(g, a);
667 >            }
668 >            public <T,U> CompletableFuture<Void> thenAcceptBoth
669 >                (CompletableFuture<T> f,
670 >                 CompletionStage<? extends U> g,
671 >                 BiConsumer<? super T,? super U> a) {
672 >                return f.thenAcceptBoth(g, a);
673 >            }
674 >            public <T,U,V> CompletableFuture<V> thenCombine
675 >                (CompletableFuture<T> f,
676 >                 CompletionStage<? extends U> g,
677 >                 BiFunction<? super T,? super U,? extends V> a) {
678 >                return f.thenCombine(g, a);
679 >            }
680 >            public <T> CompletableFuture<Void> runAfterEither
681 >                (CompletableFuture<T> f,
682 >                 CompletionStage<?> g,
683 >                 java.lang.Runnable a) {
684 >                return f.runAfterEither(g, a);
685 >            }
686 >            public <T> CompletableFuture<Void> acceptEither
687 >                (CompletableFuture<T> f,
688 >                 CompletionStage<? extends T> g,
689 >                 Consumer<? super T> a) {
690 >                return f.acceptEither(g, a);
691 >            }
692 >            public <T,U> CompletableFuture<U> applyToEither
693 >                (CompletableFuture<T> f,
694 >                 CompletionStage<? extends T> g,
695 >                 Function<? super T,U> a) {
696 >                return f.applyToEither(g, a);
697 >            }
698 >            public <T> CompletableFuture<T> exceptionally
699 >                (CompletableFuture<T> f,
700 >                 Function<Throwable, ? extends T> fn) {
701 >                return f.exceptionally(fn);
702 >            }
703 >            public <T> CompletableFuture<T> exceptionallyCompose
704 >                (CompletableFuture<T> f, Function<Throwable, ? extends CompletionStage<T>> fn) {
705 >                return f.exceptionallyCompose(fn);
706 >            }
707 >        },
708 >        ASYNC {
709 >            public void checkExecutionMode() {
710 >                assertEquals(defaultExecutorIsCommonPool,
711 >                             (ForkJoinPool.commonPool() == ForkJoinTask.getPool()));
712 >            }
713 >            public CompletableFuture<Void> runAsync(Runnable a) {
714 >                return CompletableFuture.runAsync(a);
715 >            }
716 >            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
717 >                return CompletableFuture.supplyAsync(a);
718 >            }
719 >            public <T> CompletableFuture<Void> thenRun
720 >                (CompletableFuture<T> f, Runnable a) {
721 >                return f.thenRunAsync(a);
722 >            }
723 >            public <T> CompletableFuture<Void> thenAccept
724 >                (CompletableFuture<T> f, Consumer<? super T> a) {
725 >                return f.thenAcceptAsync(a);
726 >            }
727 >            public <T,U> CompletableFuture<U> thenApply
728 >                (CompletableFuture<T> f, Function<? super T,U> a) {
729 >                return f.thenApplyAsync(a);
730 >            }
731 >            public <T,U> CompletableFuture<U> thenCompose
732 >                (CompletableFuture<T> f,
733 >                 Function<? super T,? extends CompletionStage<U>> a) {
734 >                return f.thenComposeAsync(a);
735 >            }
736 >            public <T,U> CompletableFuture<U> handle
737 >                (CompletableFuture<T> f,
738 >                 BiFunction<? super T,Throwable,? extends U> a) {
739 >                return f.handleAsync(a);
740 >            }
741 >            public <T> CompletableFuture<T> whenComplete
742 >                (CompletableFuture<T> f,
743 >                 BiConsumer<? super T,? super Throwable> a) {
744 >                return f.whenCompleteAsync(a);
745 >            }
746 >            public <T,U> CompletableFuture<Void> runAfterBoth
747 >                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
748 >                return f.runAfterBothAsync(g, a);
749 >            }
750 >            public <T,U> CompletableFuture<Void> thenAcceptBoth
751 >                (CompletableFuture<T> f,
752 >                 CompletionStage<? extends U> g,
753 >                 BiConsumer<? super T,? super U> a) {
754 >                return f.thenAcceptBothAsync(g, a);
755 >            }
756 >            public <T,U,V> CompletableFuture<V> thenCombine
757 >                (CompletableFuture<T> f,
758 >                 CompletionStage<? extends U> g,
759 >                 BiFunction<? super T,? super U,? extends V> a) {
760 >                return f.thenCombineAsync(g, a);
761 >            }
762 >            public <T> CompletableFuture<Void> runAfterEither
763 >                (CompletableFuture<T> f,
764 >                 CompletionStage<?> g,
765 >                 java.lang.Runnable a) {
766 >                return f.runAfterEitherAsync(g, a);
767 >            }
768 >            public <T> CompletableFuture<Void> acceptEither
769 >                (CompletableFuture<T> f,
770 >                 CompletionStage<? extends T> g,
771 >                 Consumer<? super T> a) {
772 >                return f.acceptEitherAsync(g, a);
773 >            }
774 >            public <T,U> CompletableFuture<U> applyToEither
775 >                (CompletableFuture<T> f,
776 >                 CompletionStage<? extends T> g,
777 >                 Function<? super T,U> a) {
778 >                return f.applyToEitherAsync(g, a);
779 >            }
780 >            public <T> CompletableFuture<T> exceptionally
781 >                (CompletableFuture<T> f,
782 >                 Function<Throwable, ? extends T> fn) {
783 >                return f.exceptionallyAsync(fn);
784 >            }
785  
786 <        f = new CompletableFuture<>();
787 <        f.completeExceptionally(new CFException());
788 <        g = f.handle(r = new IntegerHandler());
789 <        assertTrue(r.ran);
437 <        checkCompletedNormally(g, three);
786 >            public <T> CompletableFuture<T> exceptionallyCompose
787 >                (CompletableFuture<T> f, Function<Throwable, ? extends CompletionStage<T>> fn) {
788 >                return f.exceptionallyComposeAsync(fn);
789 >            }
790  
791 <        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);
791 >        },
792  
793 <        f = new CompletableFuture<>();
794 <        f.complete(one);
795 <        g = f.handle(r = new IntegerHandler());
796 <        assertTrue(r.ran);
797 <        checkCompletedNormally(g, two);
793 >        EXECUTOR {
794 >            public void checkExecutionMode() {
795 >                assertTrue(ThreadExecutor.startedCurrentThread());
796 >            }
797 >            public CompletableFuture<Void> runAsync(Runnable a) {
798 >                return CompletableFuture.runAsync(a, new ThreadExecutor());
799 >            }
800 >            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
801 >                return CompletableFuture.supplyAsync(a, new ThreadExecutor());
802 >            }
803 >            public <T> CompletableFuture<Void> thenRun
804 >                (CompletableFuture<T> f, Runnable a) {
805 >                return f.thenRunAsync(a, new ThreadExecutor());
806 >            }
807 >            public <T> CompletableFuture<Void> thenAccept
808 >                (CompletableFuture<T> f, Consumer<? super T> a) {
809 >                return f.thenAcceptAsync(a, new ThreadExecutor());
810 >            }
811 >            public <T,U> CompletableFuture<U> thenApply
812 >                (CompletableFuture<T> f, Function<? super T,U> a) {
813 >                return f.thenApplyAsync(a, new ThreadExecutor());
814 >            }
815 >            public <T,U> CompletableFuture<U> thenCompose
816 >                (CompletableFuture<T> f,
817 >                 Function<? super T,? extends CompletionStage<U>> a) {
818 >                return f.thenComposeAsync(a, new ThreadExecutor());
819 >            }
820 >            public <T,U> CompletableFuture<U> handle
821 >                (CompletableFuture<T> f,
822 >                 BiFunction<? super T,Throwable,? extends U> a) {
823 >                return f.handleAsync(a, new ThreadExecutor());
824 >            }
825 >            public <T> CompletableFuture<T> whenComplete
826 >                (CompletableFuture<T> f,
827 >                 BiConsumer<? super T,? super Throwable> a) {
828 >                return f.whenCompleteAsync(a, new ThreadExecutor());
829 >            }
830 >            public <T,U> CompletableFuture<Void> runAfterBoth
831 >                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
832 >                return f.runAfterBothAsync(g, a, new ThreadExecutor());
833 >            }
834 >            public <T,U> CompletableFuture<Void> thenAcceptBoth
835 >                (CompletableFuture<T> f,
836 >                 CompletionStage<? extends U> g,
837 >                 BiConsumer<? super T,? super U> a) {
838 >                return f.thenAcceptBothAsync(g, a, new ThreadExecutor());
839 >            }
840 >            public <T,U,V> CompletableFuture<V> thenCombine
841 >                (CompletableFuture<T> f,
842 >                 CompletionStage<? extends U> g,
843 >                 BiFunction<? super T,? super U,? extends V> a) {
844 >                return f.thenCombineAsync(g, a, new ThreadExecutor());
845 >            }
846 >            public <T> CompletableFuture<Void> runAfterEither
847 >                (CompletableFuture<T> f,
848 >                 CompletionStage<?> g,
849 >                 java.lang.Runnable a) {
850 >                return f.runAfterEitherAsync(g, a, new ThreadExecutor());
851 >            }
852 >            public <T> CompletableFuture<Void> acceptEither
853 >                (CompletableFuture<T> f,
854 >                 CompletionStage<? extends T> g,
855 >                 Consumer<? super T> a) {
856 >                return f.acceptEitherAsync(g, a, new ThreadExecutor());
857 >            }
858 >            public <T,U> CompletableFuture<U> applyToEither
859 >                (CompletableFuture<T> f,
860 >                 CompletionStage<? extends T> g,
861 >                 Function<? super T,U> a) {
862 >                return f.applyToEitherAsync(g, a, new ThreadExecutor());
863 >            }
864 >            public <T> CompletableFuture<T> exceptionally
865 >                (CompletableFuture<T> f,
866 >                 Function<Throwable, ? extends T> fn) {
867 >                return f.exceptionallyAsync(fn, new ThreadExecutor());
868 >            }
869 >            public <T> CompletableFuture<T> exceptionallyCompose
870 >                (CompletableFuture<T> f, Function<Throwable, ? extends CompletionStage<T>> fn) {
871 >                return f.exceptionallyComposeAsync(fn, new ThreadExecutor());
872 >            }
873  
874 <        f = new CompletableFuture<>();
875 <        g = f.handle(r = new IntegerHandler());
876 <        assertFalse(r.ran);
877 <        f.complete(one);
878 <        assertTrue(r.ran);
879 <        checkCompletedNormally(g, two);
874 >        };
875 >
876 >        public abstract void checkExecutionMode();
877 >        public abstract CompletableFuture<Void> runAsync(Runnable a);
878 >        public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a);
879 >        public abstract <T> CompletableFuture<Void> thenRun
880 >            (CompletableFuture<T> f, Runnable a);
881 >        public abstract <T> CompletableFuture<Void> thenAccept
882 >            (CompletableFuture<T> f, Consumer<? super T> a);
883 >        public abstract <T,U> CompletableFuture<U> thenApply
884 >            (CompletableFuture<T> f, Function<? super T,U> a);
885 >        public abstract <T,U> CompletableFuture<U> thenCompose
886 >            (CompletableFuture<T> f,
887 >             Function<? super T,? extends CompletionStage<U>> a);
888 >        public abstract <T,U> CompletableFuture<U> handle
889 >            (CompletableFuture<T> f,
890 >             BiFunction<? super T,Throwable,? extends U> a);
891 >        public abstract <T> CompletableFuture<T> whenComplete
892 >            (CompletableFuture<T> f,
893 >             BiConsumer<? super T,? super Throwable> a);
894 >        public abstract <T,U> CompletableFuture<Void> runAfterBoth
895 >            (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a);
896 >        public abstract <T,U> CompletableFuture<Void> thenAcceptBoth
897 >            (CompletableFuture<T> f,
898 >             CompletionStage<? extends U> g,
899 >             BiConsumer<? super T,? super U> a);
900 >        public abstract <T,U,V> CompletableFuture<V> thenCombine
901 >            (CompletableFuture<T> f,
902 >             CompletionStage<? extends U> g,
903 >             BiFunction<? super T,? super U,? extends V> a);
904 >        public abstract <T> CompletableFuture<Void> runAfterEither
905 >            (CompletableFuture<T> f,
906 >             CompletionStage<?> g,
907 >             java.lang.Runnable a);
908 >        public abstract <T> CompletableFuture<Void> acceptEither
909 >            (CompletableFuture<T> f,
910 >             CompletionStage<? extends T> g,
911 >             Consumer<? super T> a);
912 >        public abstract <T,U> CompletableFuture<U> applyToEither
913 >            (CompletableFuture<T> f,
914 >             CompletionStage<? extends T> g,
915 >             Function<? super T,U> a);
916 >        public abstract <T> CompletableFuture<T> exceptionally
917 >            (CompletableFuture<T> f,
918 >             Function<Throwable, ? extends T> fn);
919 >        public abstract <T> CompletableFuture<T> exceptionallyCompose
920 >            (CompletableFuture<T> f,
921 >             Function<Throwable, ? extends CompletionStage<T>> fn);
922      }
923  
924      /**
925 <     * runAsync completes after running Runnable
925 >     * exceptionally action is not invoked when source completes
926 >     * normally, and source result is propagated
927       */
928 <    public void testRunAsync() {
929 <        Noop r = new Noop();
930 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
931 <        assertNull(f.join());
932 <        assertTrue(r.ran);
933 <        checkCompletedNormally(f, null);
934 <    }
928 >    public void testExceptionally_normalCompletion() {
929 >        for (ExecutionMode m : ExecutionMode.values())
930 >        for (boolean createIncomplete : new boolean[] { true, false })
931 >        for (Integer v1 : new Integer[] { 1, null })
932 >    {
933 >        final AtomicInteger a = new AtomicInteger(0);
934 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
935 >        if (!createIncomplete) assertTrue(f.complete(v1));
936 >        final CompletableFuture<Integer> g = m.exceptionally
937 >            (f, (Throwable t) -> {
938 >                a.getAndIncrement();
939 >                threadFail("should not be called");
940 >                return null;            // unreached
941 >            });
942 >        if (createIncomplete) assertTrue(f.complete(v1));
943 >
944 >        checkCompletedNormally(g, v1);
945 >        checkCompletedNormally(f, v1);
946 >        assertEquals(0, a.get());
947 >    }}
948  
949      /**
950 <     * runAsync with executor completes after running Runnable
950 >     * exceptionally action completes with function value on source
951 >     * exception
952       */
953 <    public void testRunAsync2() {
954 <        Noop r = new Noop();
955 <        ThreadExecutor exec = new ThreadExecutor();
956 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec);
957 <        assertNull(f.join());
958 <        assertTrue(r.ran);
959 <        checkCompletedNormally(f, null);
960 <        assertEquals(1, exec.count.get());
961 <    }
953 >    public void testExceptionally_exceptionalCompletion() {
954 >        for (ExecutionMode m : ExecutionMode.values())
955 >        for (boolean createIncomplete : new boolean[] { true, false })
956 >        for (Integer v1 : new Integer[] { 1, null })
957 >    {
958 >        final AtomicInteger a = new AtomicInteger(0);
959 >        final CFException ex = new CFException();
960 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
961 >        if (!createIncomplete) f.completeExceptionally(ex);
962 >        final CompletableFuture<Integer> g = m.exceptionally
963 >            (f, (Throwable t) -> {
964 >                m.checkExecutionMode();
965 >                threadAssertSame(t, ex);
966 >                a.getAndIncrement();
967 >                return v1;
968 >            });
969 >        if (createIncomplete) f.completeExceptionally(ex);
970 >
971 >        checkCompletedNormally(g, v1);
972 >        assertEquals(1, a.get());
973 >    }}
974  
975      /**
976 <     * failing runAsync completes exceptionally after running Runnable
976 >     * If an "exceptionally action" throws an exception, it completes
977 >     * exceptionally with that exception
978       */
979 <    public void testRunAsync3() {
980 <        FailingNoop r = new FailingNoop();
981 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
982 <        checkCompletedWithWrappedCFException(f);
983 <        assertTrue(r.ran);
984 <    }
979 >    public void testExceptionally_exceptionalCompletionActionFailed() {
980 >        for (ExecutionMode m : ExecutionMode.values())
981 >        for (boolean createIncomplete : new boolean[] { true, false })
982 >    {
983 >        final AtomicInteger a = new AtomicInteger(0);
984 >        final CFException ex1 = new CFException();
985 >        final CFException ex2 = new CFException();
986 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
987 >        if (!createIncomplete) f.completeExceptionally(ex1);
988 >        final CompletableFuture<Integer> g = m.exceptionally
989 >            (f, (Throwable t) -> {
990 >                m.checkExecutionMode();
991 >                threadAssertSame(t, ex1);
992 >                a.getAndIncrement();
993 >                throw ex2;
994 >            });
995 >        if (createIncomplete) f.completeExceptionally(ex1);
996 >
997 >        checkCompletedWithWrappedException(g, ex2);
998 >        checkCompletedExceptionally(f, ex1);
999 >        assertEquals(1, a.get());
1000 >    }}
1001  
1002      /**
1003 <     * supplyAsync completes with result of supplier
1003 >     * whenComplete action executes on normal completion, propagating
1004 >     * source result.
1005       */
1006 <    public void testSupplyAsync() {
1007 <        CompletableFuture<Integer> f;
1008 <        f = CompletableFuture.supplyAsync(supplyOne);
1009 <        assertEquals(f.join(), one);
1010 <        checkCompletedNormally(f, one);
1011 <    }
1006 >    public void testWhenComplete_normalCompletion() {
1007 >        for (ExecutionMode m : ExecutionMode.values())
1008 >        for (boolean createIncomplete : new boolean[] { true, false })
1009 >        for (Integer v1 : new Integer[] { 1, null })
1010 >    {
1011 >        final AtomicInteger a = new AtomicInteger(0);
1012 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1013 >        if (!createIncomplete) assertTrue(f.complete(v1));
1014 >        final CompletableFuture<Integer> g = m.whenComplete
1015 >            (f,
1016 >             (Integer result, Throwable t) -> {
1017 >                m.checkExecutionMode();
1018 >                threadAssertSame(result, v1);
1019 >                threadAssertNull(t);
1020 >                a.getAndIncrement();
1021 >            });
1022 >        if (createIncomplete) assertTrue(f.complete(v1));
1023 >
1024 >        checkCompletedNormally(g, v1);
1025 >        checkCompletedNormally(f, v1);
1026 >        assertEquals(1, a.get());
1027 >    }}
1028  
1029      /**
1030 <     * supplyAsync with executor completes with result of supplier
1030 >     * whenComplete action executes on exceptional completion, propagating
1031 >     * source result.
1032       */
1033 <    public void testSupplyAsync2() {
1034 <        CompletableFuture<Integer> f;
1035 <        f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor());
1036 <        assertEquals(f.join(), one);
1037 <        checkCompletedNormally(f, one);
1038 <    }
1033 >    public void testWhenComplete_exceptionalCompletion() {
1034 >        for (ExecutionMode m : ExecutionMode.values())
1035 >        for (boolean createIncomplete : new boolean[] { true, false })
1036 >    {
1037 >        final AtomicInteger a = new AtomicInteger(0);
1038 >        final CFException ex = new CFException();
1039 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1040 >        if (!createIncomplete) f.completeExceptionally(ex);
1041 >        final CompletableFuture<Integer> g = m.whenComplete
1042 >            (f,
1043 >             (Integer result, Throwable t) -> {
1044 >                m.checkExecutionMode();
1045 >                threadAssertNull(result);
1046 >                threadAssertSame(t, ex);
1047 >                a.getAndIncrement();
1048 >            });
1049 >        if (createIncomplete) f.completeExceptionally(ex);
1050 >
1051 >        checkCompletedWithWrappedException(g, ex);
1052 >        checkCompletedExceptionally(f, ex);
1053 >        assertEquals(1, a.get());
1054 >    }}
1055  
1056      /**
1057 <     * Failing supplyAsync completes exceptionally
1057 >     * whenComplete action executes on cancelled source, propagating
1058 >     * CancellationException.
1059       */
1060 <    public void testSupplyAsync3() {
1061 <        FailingSupplier r = new FailingSupplier();
1062 <        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r);
1063 <        checkCompletedWithWrappedCFException(f);
1064 <        assertTrue(r.ran);
1065 <    }
1060 >    public void testWhenComplete_sourceCancelled() {
1061 >        for (ExecutionMode m : ExecutionMode.values())
1062 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1063 >        for (boolean createIncomplete : new boolean[] { true, false })
1064 >    {
1065 >        final AtomicInteger a = new AtomicInteger(0);
1066 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1067 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1068 >        final CompletableFuture<Integer> g = m.whenComplete
1069 >            (f,
1070 >             (Integer result, Throwable t) -> {
1071 >                m.checkExecutionMode();
1072 >                threadAssertNull(result);
1073 >                threadAssertTrue(t instanceof CancellationException);
1074 >                a.getAndIncrement();
1075 >            });
1076 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1077  
1078 <    // seq completion methods
1078 >        checkCompletedWithWrappedCancellationException(g);
1079 >        checkCancelled(f);
1080 >        assertEquals(1, a.get());
1081 >    }}
1082  
1083      /**
1084 <     * thenRun result completes normally after normal completion of source
1084 >     * If a whenComplete action throws an exception when triggered by
1085 >     * a normal completion, it completes exceptionally
1086       */
1087 <    public void testThenRun() {
1088 <        CompletableFuture<Integer> f;
1089 <        CompletableFuture<Void> g;
1090 <        Noop r;
1091 <
1092 <        f = new CompletableFuture<>();
1093 <        g = f.thenRun(r = new Noop());
1094 <        f.complete(null);
1095 <        checkCompletedNormally(g, null);
1096 <        assertTrue(r.ran);
1087 >    public void testWhenComplete_sourceCompletedNormallyActionFailed() {
1088 >        for (boolean createIncomplete : new boolean[] { true, false })
1089 >        for (ExecutionMode m : ExecutionMode.values())
1090 >        for (Integer v1 : new Integer[] { 1, null })
1091 >    {
1092 >        final AtomicInteger a = new AtomicInteger(0);
1093 >        final CFException ex = new CFException();
1094 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1095 >        if (!createIncomplete) assertTrue(f.complete(v1));
1096 >        final CompletableFuture<Integer> g = m.whenComplete
1097 >            (f,
1098 >             (Integer result, Throwable t) -> {
1099 >                m.checkExecutionMode();
1100 >                threadAssertSame(result, v1);
1101 >                threadAssertNull(t);
1102 >                a.getAndIncrement();
1103 >                throw ex;
1104 >            });
1105 >        if (createIncomplete) assertTrue(f.complete(v1));
1106  
1107 <        f = new CompletableFuture<>();
1108 <        f.complete(null);
1109 <        g = f.thenRun(r = new Noop());
1110 <        checkCompletedNormally(g, null);
544 <        assertTrue(r.ran);
545 <    }
1107 >        checkCompletedWithWrappedException(g, ex);
1108 >        checkCompletedNormally(f, v1);
1109 >        assertEquals(1, a.get());
1110 >    }}
1111  
1112      /**
1113 <     * thenRun result completes exceptionally after exceptional
1114 <     * completion of source
1113 >     * If a whenComplete action throws an exception when triggered by
1114 >     * a source completion that also throws an exception, the source
1115 >     * exception takes precedence (unlike handle)
1116       */
1117 <    public void testThenRun2() {
1118 <        CompletableFuture<Integer> f;
1119 <        CompletableFuture<Void> g;
1120 <        Noop r;
1121 <
1122 <        f = new CompletableFuture<>();
1123 <        g = f.thenRun(r = new Noop());
1124 <        f.completeExceptionally(new CFException());
559 <        checkCompletedWithWrappedCFException(g);
560 <        assertFalse(r.ran);
1117 >    public void testWhenComplete_sourceFailedActionFailed() {
1118 >        for (boolean createIncomplete : new boolean[] { true, false })
1119 >        for (ExecutionMode m : ExecutionMode.values())
1120 >    {
1121 >        final AtomicInteger a = new AtomicInteger(0);
1122 >        final CFException ex1 = new CFException();
1123 >        final CFException ex2 = new CFException();
1124 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1125  
1126 <        f = new CompletableFuture<>();
1127 <        f.completeExceptionally(new CFException());
1128 <        g = f.thenRun(r = new Noop());
1129 <        checkCompletedWithWrappedCFException(g);
1130 <        assertFalse(r.ran);
1131 <    }
1126 >        if (!createIncomplete) f.completeExceptionally(ex1);
1127 >        final CompletableFuture<Integer> g = m.whenComplete
1128 >            (f,
1129 >             (Integer result, Throwable t) -> {
1130 >                m.checkExecutionMode();
1131 >                threadAssertSame(t, ex1);
1132 >                threadAssertNull(result);
1133 >                a.getAndIncrement();
1134 >                throw ex2;
1135 >            });
1136 >        if (createIncomplete) f.completeExceptionally(ex1);
1137 >
1138 >        checkCompletedWithWrappedException(g, ex1);
1139 >        checkCompletedExceptionally(f, ex1);
1140 >        if (testImplementationDetails) {
1141 >            assertEquals(1, ex1.getSuppressed().length);
1142 >            assertSame(ex2, ex1.getSuppressed()[0]);
1143 >        }
1144 >        assertEquals(1, a.get());
1145 >    }}
1146  
1147      /**
1148 <     * thenRun result completes exceptionally if action does
1148 >     * handle action completes normally with function value on normal
1149 >     * completion of source
1150       */
1151 <    public void testThenRun3() {
1152 <        CompletableFuture<Integer> f;
1153 <        CompletableFuture<Void> g;
1154 <        FailingNoop r;
1155 <
1156 <        f = new CompletableFuture<>();
1157 <        g = f.thenRun(r = new FailingNoop());
1158 <        f.complete(null);
1159 <        checkCompletedWithWrappedCFException(g);
1151 >    public void testHandle_normalCompletion() {
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 AtomicInteger a = new AtomicInteger(0);
1158 >        if (!createIncomplete) assertTrue(f.complete(v1));
1159 >        final CompletableFuture<Integer> g = m.handle
1160 >            (f,
1161 >             (Integer result, Throwable t) -> {
1162 >                m.checkExecutionMode();
1163 >                threadAssertSame(result, v1);
1164 >                threadAssertNull(t);
1165 >                a.getAndIncrement();
1166 >                return inc(v1);
1167 >            });
1168 >        if (createIncomplete) assertTrue(f.complete(v1));
1169  
1170 <        f = new CompletableFuture<>();
1171 <        f.complete(null);
1172 <        g = f.thenRun(r = new FailingNoop());
1173 <        checkCompletedWithWrappedCFException(g);
586 <    }
1170 >        checkCompletedNormally(g, inc(v1));
1171 >        checkCompletedNormally(f, v1);
1172 >        assertEquals(1, a.get());
1173 >    }}
1174  
1175      /**
1176 <     * thenRun result completes exceptionally if source cancelled
1176 >     * handle action completes normally with function value on
1177 >     * exceptional completion of source
1178       */
1179 <    public void testThenRun4() {
1180 <        CompletableFuture<Integer> f;
1181 <        CompletableFuture<Void> g;
1182 <        Noop r;
1183 <
1184 <        f = new CompletableFuture<>();
1185 <        g = f.thenRun(r = new Noop());
1186 <        assertTrue(f.cancel(true));
1187 <        checkCompletedWithWrappedCancellationException(g);
1179 >    public void testHandle_exceptionalCompletion() {
1180 >        for (ExecutionMode m : ExecutionMode.values())
1181 >        for (boolean createIncomplete : new boolean[] { true, false })
1182 >        for (Integer v1 : new Integer[] { 1, null })
1183 >    {
1184 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1185 >        final AtomicInteger a = new AtomicInteger(0);
1186 >        final CFException ex = new CFException();
1187 >        if (!createIncomplete) f.completeExceptionally(ex);
1188 >        final CompletableFuture<Integer> g = m.handle
1189 >            (f,
1190 >             (Integer result, Throwable t) -> {
1191 >                m.checkExecutionMode();
1192 >                threadAssertNull(result);
1193 >                threadAssertSame(t, ex);
1194 >                a.getAndIncrement();
1195 >                return v1;
1196 >            });
1197 >        if (createIncomplete) f.completeExceptionally(ex);
1198  
1199 <        f = new CompletableFuture<>();
1200 <        assertTrue(f.cancel(true));
1201 <        g = f.thenRun(r = new Noop());
1202 <        checkCompletedWithWrappedCancellationException(g);
605 <    }
1199 >        checkCompletedNormally(g, v1);
1200 >        checkCompletedExceptionally(f, ex);
1201 >        assertEquals(1, a.get());
1202 >    }}
1203  
1204      /**
1205 <     * thenApply result completes normally after normal completion of source
1205 >     * handle action completes normally with function value on
1206 >     * cancelled source
1207       */
1208 <    public void testThenApply() {
1209 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1210 <        CompletableFuture<Integer> g = f.thenApply(inc);
1211 <        f.complete(one);
1212 <        checkCompletedNormally(g, two);
1213 <    }
1208 >    public void testHandle_sourceCancelled() {
1209 >        for (ExecutionMode m : ExecutionMode.values())
1210 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1211 >        for (boolean createIncomplete : new boolean[] { true, false })
1212 >        for (Integer v1 : new Integer[] { 1, null })
1213 >    {
1214 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1215 >        final AtomicInteger a = new AtomicInteger(0);
1216 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1217 >        final CompletableFuture<Integer> g = m.handle
1218 >            (f,
1219 >             (Integer result, Throwable t) -> {
1220 >                m.checkExecutionMode();
1221 >                threadAssertNull(result);
1222 >                threadAssertTrue(t instanceof CancellationException);
1223 >                a.getAndIncrement();
1224 >                return v1;
1225 >            });
1226 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1227 >
1228 >        checkCompletedNormally(g, v1);
1229 >        checkCancelled(f);
1230 >        assertEquals(1, a.get());
1231 >    }}
1232  
1233      /**
1234 <     * thenApply result completes exceptionally after exceptional
1235 <     * completion of source
1234 >     * If a "handle action" throws an exception when triggered by
1235 >     * a normal completion, it completes exceptionally
1236       */
1237 <    public void testThenApply2() {
1238 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1239 <        CompletableFuture<Integer> g = f.thenApply(inc);
1240 <        f.completeExceptionally(new CFException());
1241 <        checkCompletedWithWrappedCFException(g);
1242 <    }
1237 >    public void testHandle_sourceCompletedNormallyActionFailed() {
1238 >        for (ExecutionMode m : ExecutionMode.values())
1239 >        for (boolean createIncomplete : new boolean[] { true, false })
1240 >        for (Integer v1 : new Integer[] { 1, null })
1241 >    {
1242 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1243 >        final AtomicInteger a = new AtomicInteger(0);
1244 >        final CFException ex = new CFException();
1245 >        if (!createIncomplete) assertTrue(f.complete(v1));
1246 >        final CompletableFuture<Integer> g = m.handle
1247 >            (f,
1248 >             (Integer result, Throwable t) -> {
1249 >                m.checkExecutionMode();
1250 >                threadAssertSame(result, v1);
1251 >                threadAssertNull(t);
1252 >                a.getAndIncrement();
1253 >                throw ex;
1254 >            });
1255 >        if (createIncomplete) assertTrue(f.complete(v1));
1256 >
1257 >        checkCompletedWithWrappedException(g, ex);
1258 >        checkCompletedNormally(f, v1);
1259 >        assertEquals(1, a.get());
1260 >    }}
1261  
1262      /**
1263 <     * thenApply result completes exceptionally if action does
1263 >     * If a "handle action" throws an exception when triggered by
1264 >     * a source completion that also throws an exception, the action
1265 >     * exception takes precedence (unlike whenComplete)
1266       */
1267 <    public void testThenApply3() {
1268 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1269 <        CompletableFuture<Integer> g = f.thenApply(new FailingFunction());
1270 <        f.complete(one);
1271 <        checkCompletedWithWrappedCFException(g);
1272 <    }
1267 >    public void testHandle_sourceFailedActionFailed() {
1268 >        for (boolean createIncomplete : new boolean[] { true, false })
1269 >        for (ExecutionMode m : ExecutionMode.values())
1270 >    {
1271 >        final AtomicInteger a = new AtomicInteger(0);
1272 >        final CFException ex1 = new CFException();
1273 >        final CFException ex2 = new CFException();
1274 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1275 >
1276 >        if (!createIncomplete) f.completeExceptionally(ex1);
1277 >        final CompletableFuture<Integer> g = m.handle
1278 >            (f,
1279 >             (Integer result, Throwable t) -> {
1280 >                m.checkExecutionMode();
1281 >                threadAssertNull(result);
1282 >                threadAssertSame(ex1, t);
1283 >                a.getAndIncrement();
1284 >                throw ex2;
1285 >            });
1286 >        if (createIncomplete) f.completeExceptionally(ex1);
1287 >
1288 >        checkCompletedWithWrappedException(g, ex2);
1289 >        checkCompletedExceptionally(f, ex1);
1290 >        assertEquals(1, a.get());
1291 >    }}
1292  
1293      /**
1294 <     * thenApply result completes exceptionally if source cancelled
1294 >     * runAsync completes after running Runnable
1295       */
1296 <    public void testThenApply4() {
1297 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1298 <        CompletableFuture<Integer> g = f.thenApply(inc);
1299 <        assertTrue(f.cancel(true));
1300 <        checkCompletedWithWrappedCancellationException(g);
1301 <    }
1296 >    public void testRunAsync_normalCompletion() {
1297 >        ExecutionMode[] executionModes = {
1298 >            ExecutionMode.ASYNC,
1299 >            ExecutionMode.EXECUTOR,
1300 >        };
1301 >        for (ExecutionMode m : executionModes)
1302 >    {
1303 >        final Noop r = new Noop(m);
1304 >        final CompletableFuture<Void> f = m.runAsync(r);
1305 >        assertNull(f.join());
1306 >        checkCompletedNormally(f, null);
1307 >        r.assertInvoked();
1308 >    }}
1309  
1310      /**
1311 <     * thenAccept result completes normally after normal completion of source
1311 >     * failing runAsync completes exceptionally after running Runnable
1312       */
1313 <    public void testThenAccept() {
1314 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1315 <        IncAction r = new IncAction();
1316 <        CompletableFuture<Void> g = f.thenAccept(r);
1317 <        f.complete(one);
1318 <        checkCompletedNormally(g, null);
1319 <        assertEquals(r.value, 2);
1313 >    public void testRunAsync_exceptionalCompletion() {
1314 >        ExecutionMode[] executionModes = {
1315 >            ExecutionMode.ASYNC,
1316 >            ExecutionMode.EXECUTOR,
1317 >        };
1318 >        for (ExecutionMode m : executionModes)
1319 >    {
1320 >        final FailingRunnable r = new FailingRunnable(m);
1321 >        final CompletableFuture<Void> f = m.runAsync(r);
1322 >        checkCompletedWithWrappedException(f, r.ex);
1323 >        r.assertInvoked();
1324 >    }}
1325 >
1326 >    @SuppressWarnings("FutureReturnValueIgnored")
1327 >    public void testRunAsync_rejectingExecutor() {
1328 >        CountingRejectingExecutor e = new CountingRejectingExecutor();
1329 >        try {
1330 >            CompletableFuture.runAsync(() -> {}, e);
1331 >            shouldThrow();
1332 >        } catch (Throwable t) {
1333 >            assertSame(e.ex, t);
1334 >        }
1335 >
1336 >        assertEquals(1, e.count.get());
1337      }
1338  
1339      /**
1340 <     * thenAccept result completes exceptionally after exceptional
662 <     * completion of source
1340 >     * supplyAsync completes with result of supplier
1341       */
1342 <    public void testThenAccept2() {
1343 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1344 <        IncAction r = new IncAction();
1345 <        CompletableFuture<Void> g = f.thenAccept(r);
1346 <        f.completeExceptionally(new CFException());
1347 <        checkCompletedWithWrappedCFException(g);
1348 <    }
1342 >    public void testSupplyAsync_normalCompletion() {
1343 >        ExecutionMode[] executionModes = {
1344 >            ExecutionMode.ASYNC,
1345 >            ExecutionMode.EXECUTOR,
1346 >        };
1347 >        for (ExecutionMode m : executionModes)
1348 >        for (Integer v1 : new Integer[] { 1, null })
1349 >    {
1350 >        final IntegerSupplier r = new IntegerSupplier(m, v1);
1351 >        final CompletableFuture<Integer> f = m.supplyAsync(r);
1352 >        assertSame(v1, f.join());
1353 >        checkCompletedNormally(f, v1);
1354 >        r.assertInvoked();
1355 >    }}
1356  
1357      /**
1358 <     * thenAccept result completes exceptionally if action does
1358 >     * Failing supplyAsync completes exceptionally
1359       */
1360 <    public void testThenAccept3() {
1361 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1362 <        FailingConsumer r = new FailingConsumer();
1363 <        CompletableFuture<Void> g = f.thenAccept(r);
1364 <        f.complete(one);
1365 <        checkCompletedWithWrappedCFException(g);
1366 <        assertTrue(r.ran);
1360 >    public void testSupplyAsync_exceptionalCompletion() {
1361 >        ExecutionMode[] executionModes = {
1362 >            ExecutionMode.ASYNC,
1363 >            ExecutionMode.EXECUTOR,
1364 >        };
1365 >        for (ExecutionMode m : executionModes)
1366 >    {
1367 >        FailingSupplier r = new FailingSupplier(m);
1368 >        CompletableFuture<Integer> f = m.supplyAsync(r);
1369 >        checkCompletedWithWrappedException(f, r.ex);
1370 >        r.assertInvoked();
1371 >    }}
1372 >
1373 >    @SuppressWarnings("FutureReturnValueIgnored")
1374 >    public void testSupplyAsync_rejectingExecutor() {
1375 >        CountingRejectingExecutor e = new CountingRejectingExecutor();
1376 >        try {
1377 >            CompletableFuture.supplyAsync(() -> null, e);
1378 >            shouldThrow();
1379 >        } catch (Throwable t) {
1380 >            assertSame(e.ex, t);
1381 >        }
1382 >
1383 >        assertEquals(1, e.count.get());
1384      }
1385  
1386 +    // seq completion methods
1387 +
1388      /**
1389 <     * thenAccept result completes exceptionally if source cancelled
1389 >     * thenRun result completes normally after normal completion of source
1390       */
1391 <    public void testThenAccept4() {
1392 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1393 <        IncAction r = new IncAction();
1394 <        CompletableFuture<Void> g = f.thenAccept(r);
1395 <        assertTrue(f.cancel(true));
1396 <        checkCompletedWithWrappedCancellationException(g);
1397 <    }
1391 >    public void testThenRun_normalCompletion() {
1392 >        for (ExecutionMode m : ExecutionMode.values())
1393 >        for (Integer v1 : new Integer[] { 1, null })
1394 >    {
1395 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1396 >        final Noop[] rs = new Noop[6];
1397 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1398 >
1399 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1400 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1401 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1402 >        checkIncomplete(h0);
1403 >        checkIncomplete(h1);
1404 >        checkIncomplete(h2);
1405 >        assertTrue(f.complete(v1));
1406 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1407 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1408 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1409 >
1410 >        checkCompletedNormally(h0, null);
1411 >        checkCompletedNormally(h1, null);
1412 >        checkCompletedNormally(h2, null);
1413 >        checkCompletedNormally(h3, null);
1414 >        checkCompletedNormally(h4, null);
1415 >        checkCompletedNormally(h5, null);
1416 >        checkCompletedNormally(f, v1);
1417 >        for (Noop r : rs) r.assertInvoked();
1418 >    }}
1419  
1420      /**
1421 <     * thenCombine result completes normally after normal completion
1422 <     * of sources
1421 >     * thenRun result completes exceptionally after exceptional
1422 >     * completion of source
1423       */
1424 <    public void testThenCombine() {
1425 <        CompletableFuture<Integer> f, g, h;
1424 >    public void testThenRun_exceptionalCompletion() {
1425 >        for (ExecutionMode m : ExecutionMode.values())
1426 >    {
1427 >        final CFException ex = new CFException();
1428 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1429 >        final Noop[] rs = new Noop[6];
1430 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1431  
1432 <        f = new CompletableFuture<>();
1433 <        g = new CompletableFuture<>();
1434 <        h = f.thenCombine(g, subtract);
1435 <        f.complete(3);
1436 <        checkIncomplete(h);
1437 <        g.complete(1);
1438 <        checkCompletedNormally(h, 2);
1432 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1433 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1434 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1435 >        checkIncomplete(h0);
1436 >        checkIncomplete(h1);
1437 >        checkIncomplete(h2);
1438 >        assertTrue(f.completeExceptionally(ex));
1439 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1440 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1441 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1442 >
1443 >        checkCompletedWithWrappedException(h0, ex);
1444 >        checkCompletedWithWrappedException(h1, ex);
1445 >        checkCompletedWithWrappedException(h2, ex);
1446 >        checkCompletedWithWrappedException(h3, ex);
1447 >        checkCompletedWithWrappedException(h4, ex);
1448 >        checkCompletedWithWrappedException(h5, ex);
1449 >        checkCompletedExceptionally(f, ex);
1450 >        for (Noop r : rs) r.assertNotInvoked();
1451 >    }}
1452  
1453 <        f = new CompletableFuture<>();
1454 <        g = new CompletableFuture<>();
1455 <        h = f.thenCombine(g, subtract);
1456 <        g.complete(1);
1457 <        checkIncomplete(h);
1458 <        f.complete(3);
1459 <        checkCompletedNormally(h, 2);
1453 >    /**
1454 >     * thenRun result completes exceptionally if source cancelled
1455 >     */
1456 >    public void testThenRun_sourceCancelled() {
1457 >        for (ExecutionMode m : ExecutionMode.values())
1458 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1459 >    {
1460 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1461 >        final Noop[] rs = new Noop[6];
1462 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1463  
1464 <        f = new CompletableFuture<>();
1465 <        g = new CompletableFuture<>();
1466 <        g.complete(1);
1467 <        f.complete(3);
1468 <        h = f.thenCombine(g, subtract);
1469 <        checkCompletedNormally(h, 2);
1470 <    }
1464 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1465 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1466 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1467 >        checkIncomplete(h0);
1468 >        checkIncomplete(h1);
1469 >        checkIncomplete(h2);
1470 >        assertTrue(f.cancel(mayInterruptIfRunning));
1471 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1472 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1473 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1474 >
1475 >        checkCompletedWithWrappedCancellationException(h0);
1476 >        checkCompletedWithWrappedCancellationException(h1);
1477 >        checkCompletedWithWrappedCancellationException(h2);
1478 >        checkCompletedWithWrappedCancellationException(h3);
1479 >        checkCompletedWithWrappedCancellationException(h4);
1480 >        checkCompletedWithWrappedCancellationException(h5);
1481 >        checkCancelled(f);
1482 >        for (Noop r : rs) r.assertNotInvoked();
1483 >    }}
1484  
1485      /**
1486 <     * thenCombine result completes exceptionally after exceptional
728 <     * completion of either source
1486 >     * thenRun result completes exceptionally if action does
1487       */
1488 <    public void testThenCombine2() {
1489 <        CompletableFuture<Integer> f, g, h;
1490 <
1491 <        f = new CompletableFuture<>();
1492 <        g = new CompletableFuture<>();
1493 <        h = f.thenCombine(g, subtract);
1494 <        f.completeExceptionally(new CFException());
737 <        checkIncomplete(h);
738 <        g.complete(1);
739 <        checkCompletedWithWrappedCFException(h);
1488 >    public void testThenRun_actionFailed() {
1489 >        for (ExecutionMode m : ExecutionMode.values())
1490 >        for (Integer v1 : new Integer[] { 1, null })
1491 >    {
1492 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1493 >        final FailingRunnable[] rs = new FailingRunnable[6];
1494 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
1495  
1496 <        f = new CompletableFuture<>();
1497 <        g = new CompletableFuture<>();
1498 <        h = f.thenCombine(g, subtract);
1499 <        g.completeExceptionally(new CFException());
1500 <        checkIncomplete(h);
1501 <        f.complete(3);
1502 <        checkCompletedWithWrappedCFException(h);
1496 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1497 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1498 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1499 >        assertTrue(f.complete(v1));
1500 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1501 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1502 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1503 >
1504 >        checkCompletedWithWrappedException(h0, rs[0].ex);
1505 >        checkCompletedWithWrappedException(h1, rs[1].ex);
1506 >        checkCompletedWithWrappedException(h2, rs[2].ex);
1507 >        checkCompletedWithWrappedException(h3, rs[3].ex);
1508 >        checkCompletedWithWrappedException(h4, rs[4].ex);
1509 >        checkCompletedWithWrappedException(h5, rs[5].ex);
1510 >        checkCompletedNormally(f, v1);
1511 >    }}
1512  
1513 <        f = new CompletableFuture<>();
1514 <        g = new CompletableFuture<>();
1515 <        f.complete(3);
1516 <        g.completeExceptionally(new CFException());
1517 <        h = f.thenCombine(g, subtract);
1518 <        checkCompletedWithWrappedCFException(h);
1513 >    /**
1514 >     * thenApply result completes normally after normal completion of source
1515 >     */
1516 >    public void testThenApply_normalCompletion() {
1517 >        for (ExecutionMode m : ExecutionMode.values())
1518 >        for (Integer v1 : new Integer[] { 1, null })
1519 >    {
1520 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1521 >        final IncFunction[] rs = new IncFunction[4];
1522 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1523  
1524 <        f = new CompletableFuture<>();
1525 <        g = new CompletableFuture<>();
1526 <        f.completeExceptionally(new CFException());
1527 <        g.complete(3);
1528 <        h = f.thenCombine(g, subtract);
1529 <        checkCompletedWithWrappedCFException(h);
1530 <    }
1524 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1525 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1526 >        checkIncomplete(h0);
1527 >        checkIncomplete(h1);
1528 >        assertTrue(f.complete(v1));
1529 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1530 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1531 >
1532 >        checkCompletedNormally(h0, inc(v1));
1533 >        checkCompletedNormally(h1, inc(v1));
1534 >        checkCompletedNormally(h2, inc(v1));
1535 >        checkCompletedNormally(h3, inc(v1));
1536 >        checkCompletedNormally(f, v1);
1537 >        for (IncFunction r : rs) r.assertValue(inc(v1));
1538 >    }}
1539  
1540      /**
1541 <     * thenCombine result completes exceptionally if action does
1541 >     * thenApply result completes exceptionally after exceptional
1542 >     * completion of source
1543       */
1544 <    public void testThenCombine3() {
1545 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1546 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1547 <        FailingBiFunction r = new FailingBiFunction();
1548 <        CompletableFuture<Integer> g = f.thenCombine(f2, r);
1549 <        f.complete(one);
1550 <        checkIncomplete(g);
1551 <        assertFalse(r.ran);
1552 <        f2.complete(two);
1553 <        checkCompletedWithWrappedCFException(g);
1554 <        assertTrue(r.ran);
1555 <    }
1544 >    public void testThenApply_exceptionalCompletion() {
1545 >        for (ExecutionMode m : ExecutionMode.values())
1546 >    {
1547 >        final CFException ex = new CFException();
1548 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1549 >        final IncFunction[] rs = new IncFunction[4];
1550 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1551 >
1552 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1553 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1554 >        assertTrue(f.completeExceptionally(ex));
1555 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1556 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1557 >
1558 >        checkCompletedWithWrappedException(h0, ex);
1559 >        checkCompletedWithWrappedException(h1, ex);
1560 >        checkCompletedWithWrappedException(h2, ex);
1561 >        checkCompletedWithWrappedException(h3, ex);
1562 >        checkCompletedExceptionally(f, ex);
1563 >        for (IncFunction r : rs) r.assertNotInvoked();
1564 >    }}
1565  
1566      /**
1567 <     * thenCombine result completes exceptionally if either source cancelled
1567 >     * thenApply result completes exceptionally if source cancelled
1568       */
1569 <    public void testThenCombine4() {
1570 <        CompletableFuture<Integer> f, g, h;
1571 <
1572 <        f = new CompletableFuture<>();
1573 <        g = new CompletableFuture<>();
1574 <        h = f.thenCombine(g, subtract);
1575 <        assertTrue(f.cancel(true));
790 <        checkIncomplete(h);
791 <        g.complete(1);
792 <        checkCompletedWithWrappedCancellationException(h);
1569 >    public void testThenApply_sourceCancelled() {
1570 >        for (ExecutionMode m : ExecutionMode.values())
1571 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1572 >    {
1573 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1574 >        final IncFunction[] rs = new IncFunction[4];
1575 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1576  
1577 <        f = new CompletableFuture<>();
1578 <        g = new CompletableFuture<>();
1579 <        h = f.thenCombine(g, subtract);
1580 <        assertTrue(g.cancel(true));
1581 <        checkIncomplete(h);
799 <        f.complete(3);
800 <        checkCompletedWithWrappedCancellationException(h);
1577 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1578 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1579 >        assertTrue(f.cancel(mayInterruptIfRunning));
1580 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1581 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1582  
1583 <        f = new CompletableFuture<>();
1584 <        g = new CompletableFuture<>();
1585 <        assertTrue(f.cancel(true));
1586 <        assertTrue(g.cancel(true));
1587 <        h = f.thenCombine(g, subtract);
1588 <        checkCompletedWithWrappedCancellationException(h);
1589 <    }
1583 >        checkCompletedWithWrappedCancellationException(h0);
1584 >        checkCompletedWithWrappedCancellationException(h1);
1585 >        checkCompletedWithWrappedCancellationException(h2);
1586 >        checkCompletedWithWrappedCancellationException(h3);
1587 >        checkCancelled(f);
1588 >        for (IncFunction r : rs) r.assertNotInvoked();
1589 >    }}
1590  
1591      /**
1592 <     * thenAcceptBoth result completes normally after normal
812 <     * completion of sources
1592 >     * thenApply result completes exceptionally if action does
1593       */
1594 <    public void testThenAcceptBoth() {
1595 <        CompletableFuture<Integer> f, g;
1596 <        CompletableFuture<Void> h;
1597 <        SubtractAction r;
1598 <
1599 <        f = new CompletableFuture<>();
1600 <        g = new CompletableFuture<>();
821 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
822 <        f.complete(3);
823 <        checkIncomplete(h);
824 <        g.complete(1);
825 <        checkCompletedNormally(h, null);
826 <        assertEquals(r.value, 2);
827 <
828 <        f = new CompletableFuture<>();
829 <        g = new CompletableFuture<>();
830 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
831 <        g.complete(1);
832 <        checkIncomplete(h);
833 <        f.complete(3);
834 <        checkCompletedNormally(h, null);
835 <        assertEquals(r.value, 2);
1594 >    public void testThenApply_actionFailed() {
1595 >        for (ExecutionMode m : ExecutionMode.values())
1596 >        for (Integer v1 : new Integer[] { 1, null })
1597 >    {
1598 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1599 >        final FailingFunction[] rs = new FailingFunction[4];
1600 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
1601  
1602 <        f = new CompletableFuture<>();
1603 <        g = new CompletableFuture<>();
1604 <        g.complete(1);
1605 <        f.complete(3);
1606 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1607 <        checkCompletedNormally(h, null);
1608 <        assertEquals(r.value, 2);
1609 <    }
1602 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1603 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1604 >        assertTrue(f.complete(v1));
1605 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1606 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1607 >
1608 >        checkCompletedWithWrappedException(h0, rs[0].ex);
1609 >        checkCompletedWithWrappedException(h1, rs[1].ex);
1610 >        checkCompletedWithWrappedException(h2, rs[2].ex);
1611 >        checkCompletedWithWrappedException(h3, rs[3].ex);
1612 >        checkCompletedNormally(f, v1);
1613 >    }}
1614  
1615      /**
1616 <     * thenAcceptBoth result completes exceptionally after exceptional
848 <     * completion of either source
1616 >     * thenAccept result completes normally after normal completion of source
1617       */
1618 <    public void testThenAcceptBoth2() {
1619 <        CompletableFuture<Integer> f, g;
1620 <        CompletableFuture<Void> h;
1621 <        SubtractAction r;
1622 <
1623 <        f = new CompletableFuture<>();
1624 <        g = new CompletableFuture<>();
857 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
858 <        f.completeExceptionally(new CFException());
859 <        checkIncomplete(h);
860 <        g.complete(1);
861 <        checkCompletedWithWrappedCFException(h);
1618 >    public void testThenAccept_normalCompletion() {
1619 >        for (ExecutionMode m : ExecutionMode.values())
1620 >        for (Integer v1 : new Integer[] { 1, null })
1621 >    {
1622 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1623 >        final NoopConsumer[] rs = new NoopConsumer[4];
1624 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1625  
1626 <        f = new CompletableFuture<>();
1627 <        g = new CompletableFuture<>();
1628 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1629 <        g.completeExceptionally(new CFException());
1630 <        checkIncomplete(h);
1631 <        f.complete(3);
1632 <        checkCompletedWithWrappedCFException(h);
1626 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1627 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1628 >        checkIncomplete(h0);
1629 >        checkIncomplete(h1);
1630 >        assertTrue(f.complete(v1));
1631 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1632 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1633 >
1634 >        checkCompletedNormally(h0, null);
1635 >        checkCompletedNormally(h1, null);
1636 >        checkCompletedNormally(h2, null);
1637 >        checkCompletedNormally(h3, null);
1638 >        checkCompletedNormally(f, v1);
1639 >        for (NoopConsumer r : rs) r.assertValue(v1);
1640 >    }}
1641  
1642 <        f = new CompletableFuture<>();
1643 <        g = new CompletableFuture<>();
1644 <        f.complete(3);
1645 <        g.completeExceptionally(new CFException());
1646 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1647 <        checkCompletedWithWrappedCFException(h);
1642 >    /**
1643 >     * thenAccept result completes exceptionally after exceptional
1644 >     * completion of source
1645 >     */
1646 >    public void testThenAccept_exceptionalCompletion() {
1647 >        for (ExecutionMode m : ExecutionMode.values())
1648 >    {
1649 >        final CFException ex = new CFException();
1650 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1651 >        final NoopConsumer[] rs = new NoopConsumer[4];
1652 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1653  
1654 <        f = new CompletableFuture<>();
1655 <        g = new CompletableFuture<>();
1656 <        f.completeExceptionally(new CFException());
1657 <        g.complete(3);
1658 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1659 <        checkCompletedWithWrappedCFException(h);
1660 <    }
1654 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1655 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1656 >        assertTrue(f.completeExceptionally(ex));
1657 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1658 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1659 >
1660 >        checkCompletedWithWrappedException(h0, ex);
1661 >        checkCompletedWithWrappedException(h1, ex);
1662 >        checkCompletedWithWrappedException(h2, ex);
1663 >        checkCompletedWithWrappedException(h3, ex);
1664 >        checkCompletedExceptionally(f, ex);
1665 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1666 >    }}
1667  
1668      /**
1669 <     * thenAcceptBoth result completes exceptionally if action does
1669 >     * thenAccept result completes exceptionally if source cancelled
1670       */
1671 <    public void testThenAcceptBoth3() {
1672 <        CompletableFuture<Integer> f, g;
1673 <        CompletableFuture<Void> h;
1674 <        FailingBiConsumer r;
1671 >    public void testThenAccept_sourceCancelled() {
1672 >        for (ExecutionMode m : ExecutionMode.values())
1673 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1674 >    {
1675 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1676 >        final NoopConsumer[] rs = new NoopConsumer[4];
1677 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1678  
1679 <        f = new CompletableFuture<>();
1680 <        g = new CompletableFuture<>();
1681 <        h = f.thenAcceptBoth(g, r = new FailingBiConsumer());
1682 <        f.complete(3);
1683 <        checkIncomplete(h);
899 <        g.complete(1);
900 <        checkCompletedWithWrappedCFException(h);
1679 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1680 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1681 >        assertTrue(f.cancel(mayInterruptIfRunning));
1682 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1683 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1684  
1685 <        f = new CompletableFuture<>();
1686 <        g = new CompletableFuture<>();
1687 <        f.complete(3);
1688 <        g.complete(1);
1689 <        h = f.thenAcceptBoth(g, r = new FailingBiConsumer());
1690 <        checkCompletedWithWrappedCFException(h);
1691 <    }
1685 >        checkCompletedWithWrappedCancellationException(h0);
1686 >        checkCompletedWithWrappedCancellationException(h1);
1687 >        checkCompletedWithWrappedCancellationException(h2);
1688 >        checkCompletedWithWrappedCancellationException(h3);
1689 >        checkCancelled(f);
1690 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1691 >    }}
1692  
1693      /**
1694 <     * thenAcceptBoth result completes exceptionally if either source cancelled
1694 >     * thenAccept result completes exceptionally if action does
1695       */
1696 <    public void testThenAcceptBoth4() {
1697 <        CompletableFuture<Integer> f, g;
1698 <        CompletableFuture<Void> h;
1699 <        SubtractAction r;
1696 >    public void testThenAccept_actionFailed() {
1697 >        for (ExecutionMode m : ExecutionMode.values())
1698 >        for (Integer v1 : new Integer[] { 1, null })
1699 >    {
1700 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1701 >        final FailingConsumer[] rs = new FailingConsumer[4];
1702 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
1703  
1704 <        f = new CompletableFuture<>();
1705 <        g = new CompletableFuture<>();
1706 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1707 <        assertTrue(f.cancel(true));
1708 <        checkIncomplete(h);
1709 <        g.complete(1);
1710 <        checkCompletedWithWrappedCancellationException(h);
1704 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1705 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1706 >        assertTrue(f.complete(v1));
1707 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1708 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1709 >
1710 >        checkCompletedWithWrappedException(h0, rs[0].ex);
1711 >        checkCompletedWithWrappedException(h1, rs[1].ex);
1712 >        checkCompletedWithWrappedException(h2, rs[2].ex);
1713 >        checkCompletedWithWrappedException(h3, rs[3].ex);
1714 >        checkCompletedNormally(f, v1);
1715 >    }}
1716  
1717 <        f = new CompletableFuture<>();
1718 <        g = new CompletableFuture<>();
1719 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1720 <        assertTrue(g.cancel(true));
1721 <        checkIncomplete(h);
1722 <        f.complete(3);
1723 <        checkCompletedWithWrappedCancellationException(h);
1717 >    /**
1718 >     * thenCombine result completes normally after normal completion
1719 >     * of sources
1720 >     */
1721 >    public void testThenCombine_normalCompletion() {
1722 >        for (ExecutionMode m : ExecutionMode.values())
1723 >        for (boolean fFirst : new boolean[] { true, false })
1724 >        for (Integer v1 : new Integer[] { 1, null })
1725 >        for (Integer v2 : new Integer[] { 2, null })
1726 >    {
1727 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1728 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1729 >        final SubtractFunction[] rs = new SubtractFunction[6];
1730 >        for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m);
1731  
1732 <        f = new CompletableFuture<>();
1733 <        g = new CompletableFuture<>();
1734 <        f.complete(3);
1735 <        assertTrue(g.cancel(true));
1736 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1737 <        checkCompletedWithWrappedCancellationException(h);
1732 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1733 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1734 >        final Integer w1 =  fFirst ? v1 : v2;
1735 >        final Integer w2 = !fFirst ? v1 : v2;
1736 >
1737 >        final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]);
1738 >        final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]);
1739 >        assertTrue(fst.complete(w1));
1740 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]);
1741 >        final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]);
1742 >        checkIncomplete(h0); rs[0].assertNotInvoked();
1743 >        checkIncomplete(h2); rs[2].assertNotInvoked();
1744 >        checkCompletedNormally(h1, subtract(w1, w1));
1745 >        checkCompletedNormally(h3, subtract(w1, w1));
1746 >        rs[1].assertValue(subtract(w1, w1));
1747 >        rs[3].assertValue(subtract(w1, w1));
1748 >        assertTrue(snd.complete(w2));
1749 >        final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]);
1750 >
1751 >        checkCompletedNormally(h0, subtract(v1, v2));
1752 >        checkCompletedNormally(h2, subtract(v1, v2));
1753 >        checkCompletedNormally(h4, subtract(v1, v2));
1754 >        rs[0].assertValue(subtract(v1, v2));
1755 >        rs[2].assertValue(subtract(v1, v2));
1756 >        rs[4].assertValue(subtract(v1, v2));
1757  
1758 <        f = new CompletableFuture<>();
1759 <        g = new CompletableFuture<>();
1760 <        assertTrue(f.cancel(true));
944 <        g.complete(3);
945 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
946 <        checkCompletedWithWrappedCancellationException(h);
947 <    }
1758 >        checkCompletedNormally(f, v1);
1759 >        checkCompletedNormally(g, v2);
1760 >    }}
1761  
1762      /**
1763 <     * Permits the testing of parallel code for the 3 different
1764 <     * execution modes without repeating all the testing code.
1763 >     * thenCombine result completes exceptionally after exceptional
1764 >     * completion of either source
1765       */
1766 <    enum ExecutionMode {
1767 <        DEFAULT {
1768 <            public <T,U> CompletableFuture<Void> runAfterBoth
1769 <                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable r) {
1770 <                return f.runAfterBoth(g, r);
1771 <            }
1772 <        },
1773 <
1774 <        DEFAULT_ASYNC {
1775 <            public <T,U> CompletableFuture<Void> runAfterBoth
1776 <                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable r) {
1777 <                return f.runAfterBothAsync(g, r);
1778 <            }
1779 <        },
1766 >    public void testThenCombine_exceptionalCompletion() throws Throwable {
1767 >        for (ExecutionMode m : ExecutionMode.values())
1768 >        for (boolean fFirst : new boolean[] { true, false })
1769 >        for (boolean failFirst : new boolean[] { true, false })
1770 >        for (Integer v1 : new Integer[] { 1, null })
1771 >    {
1772 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1773 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1774 >        final CFException ex = new CFException();
1775 >        final SubtractFunction r1 = new SubtractFunction(m);
1776 >        final SubtractFunction r2 = new SubtractFunction(m);
1777 >        final SubtractFunction r3 = new SubtractFunction(m);
1778 >
1779 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1780 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1781 >        final Callable<Boolean> complete1 = failFirst ?
1782 >            () -> fst.completeExceptionally(ex) :
1783 >            () -> fst.complete(v1);
1784 >        final Callable<Boolean> complete2 = failFirst ?
1785 >            () -> snd.complete(v1) :
1786 >            () -> snd.completeExceptionally(ex);
1787 >
1788 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1789 >        assertTrue(complete1.call());
1790 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1791 >        checkIncomplete(h1);
1792 >        checkIncomplete(h2);
1793 >        assertTrue(complete2.call());
1794 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1795 >
1796 >        checkCompletedWithWrappedException(h1, ex);
1797 >        checkCompletedWithWrappedException(h2, ex);
1798 >        checkCompletedWithWrappedException(h3, ex);
1799 >        r1.assertNotInvoked();
1800 >        r2.assertNotInvoked();
1801 >        r3.assertNotInvoked();
1802 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1803 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1804 >    }}
1805  
1806 <        EXECUTOR {
1807 <            public <T,U> CompletableFuture<Void> runAfterBoth
1808 <                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable r) {
1809 <                return f.runAfterBothAsync(g, r, new ThreadExecutor());
1810 <            }
1811 <        };
1806 >    /**
1807 >     * thenCombine result completes exceptionally if either source cancelled
1808 >     */
1809 >    public void testThenCombine_sourceCancelled() throws Throwable {
1810 >        for (ExecutionMode m : ExecutionMode.values())
1811 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1812 >        for (boolean fFirst : new boolean[] { true, false })
1813 >        for (boolean failFirst : new boolean[] { true, false })
1814 >        for (Integer v1 : new Integer[] { 1, null })
1815 >    {
1816 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1817 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1818 >        final SubtractFunction r1 = new SubtractFunction(m);
1819 >        final SubtractFunction r2 = new SubtractFunction(m);
1820 >        final SubtractFunction r3 = new SubtractFunction(m);
1821 >
1822 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1823 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1824 >        final Callable<Boolean> complete1 = failFirst ?
1825 >            () -> fst.cancel(mayInterruptIfRunning) :
1826 >            () -> fst.complete(v1);
1827 >        final Callable<Boolean> complete2 = failFirst ?
1828 >            () -> snd.complete(v1) :
1829 >            () -> snd.cancel(mayInterruptIfRunning);
1830 >
1831 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1832 >        assertTrue(complete1.call());
1833 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1834 >        checkIncomplete(h1);
1835 >        checkIncomplete(h2);
1836 >        assertTrue(complete2.call());
1837 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1838 >
1839 >        checkCompletedWithWrappedCancellationException(h1);
1840 >        checkCompletedWithWrappedCancellationException(h2);
1841 >        checkCompletedWithWrappedCancellationException(h3);
1842 >        r1.assertNotInvoked();
1843 >        r2.assertNotInvoked();
1844 >        r3.assertNotInvoked();
1845 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1846 >        checkCancelled(failFirst ? fst : snd);
1847 >    }}
1848  
1849 <        public abstract <T,U> CompletableFuture<Void> runAfterBoth
1850 <            (CompletableFuture<T> f, CompletableFuture<U> g, Runnable r);
1851 <    }
1849 >    /**
1850 >     * thenCombine result completes exceptionally if action does
1851 >     */
1852 >    public void testThenCombine_actionFailed() {
1853 >        for (ExecutionMode m : ExecutionMode.values())
1854 >        for (boolean fFirst : new boolean[] { true, false })
1855 >        for (Integer v1 : new Integer[] { 1, null })
1856 >        for (Integer v2 : new Integer[] { 2, null })
1857 >    {
1858 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1859 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1860 >        final FailingBiFunction r1 = new FailingBiFunction(m);
1861 >        final FailingBiFunction r2 = new FailingBiFunction(m);
1862 >        final FailingBiFunction r3 = new FailingBiFunction(m);
1863 >
1864 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1865 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1866 >        final Integer w1 =  fFirst ? v1 : v2;
1867 >        final Integer w2 = !fFirst ? v1 : v2;
1868 >
1869 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1870 >        assertTrue(fst.complete(w1));
1871 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1872 >        assertTrue(snd.complete(w2));
1873 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1874 >
1875 >        checkCompletedWithWrappedException(h1, r1.ex);
1876 >        checkCompletedWithWrappedException(h2, r2.ex);
1877 >        checkCompletedWithWrappedException(h3, r3.ex);
1878 >        r1.assertInvoked();
1879 >        r2.assertInvoked();
1880 >        r3.assertInvoked();
1881 >        checkCompletedNormally(f, v1);
1882 >        checkCompletedNormally(g, v2);
1883 >    }}
1884  
1885      /**
1886 <     * runAfterBoth result completes normally after normal
1886 >     * thenAcceptBoth result completes normally after normal
1887       * completion of sources
1888       */
1889 <    public void testRunAfterBoth_normalCompletion1() {
1889 >    public void testThenAcceptBoth_normalCompletion() {
1890          for (ExecutionMode m : ExecutionMode.values())
1891 +        for (boolean fFirst : new boolean[] { true, false })
1892          for (Integer v1 : new Integer[] { 1, null })
1893 <        for (Integer v2 : new Integer[] { 2, null }) {
1894 <
1893 >        for (Integer v2 : new Integer[] { 2, null })
1894 >    {
1895          final CompletableFuture<Integer> f = new CompletableFuture<>();
1896          final CompletableFuture<Integer> g = new CompletableFuture<>();
1897 <        final Noop r = new Noop();
1898 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1899 <
1900 <        f.complete(v1);
1901 <        checkIncomplete(h);
1902 <        assertFalse(r.ran);
1903 <        g.complete(v2);
1904 <
1905 <        checkCompletedNormally(h, null);
1906 <        assertTrue(r.ran);
1897 >        final SubtractAction r1 = new SubtractAction(m);
1898 >        final SubtractAction r2 = new SubtractAction(m);
1899 >        final SubtractAction r3 = new SubtractAction(m);
1900 >
1901 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1902 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1903 >        final Integer w1 =  fFirst ? v1 : v2;
1904 >        final Integer w2 = !fFirst ? v1 : v2;
1905 >
1906 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1907 >        assertTrue(fst.complete(w1));
1908 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1909 >        checkIncomplete(h1);
1910 >        checkIncomplete(h2);
1911 >        r1.assertNotInvoked();
1912 >        r2.assertNotInvoked();
1913 >        assertTrue(snd.complete(w2));
1914 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1915 >
1916 >        checkCompletedNormally(h1, null);
1917 >        checkCompletedNormally(h2, null);
1918 >        checkCompletedNormally(h3, null);
1919 >        r1.assertValue(subtract(v1, v2));
1920 >        r2.assertValue(subtract(v1, v2));
1921 >        r3.assertValue(subtract(v1, v2));
1922          checkCompletedNormally(f, v1);
1923          checkCompletedNormally(g, v2);
1924 <        }
1003 <    }
1924 >    }}
1925  
1926 <    public void testRunAfterBoth_normalCompletion2() {
1926 >    /**
1927 >     * thenAcceptBoth result completes exceptionally after exceptional
1928 >     * completion of either source
1929 >     */
1930 >    public void testThenAcceptBoth_exceptionalCompletion() throws Throwable {
1931          for (ExecutionMode m : ExecutionMode.values())
1932 +        for (boolean fFirst : new boolean[] { true, false })
1933 +        for (boolean failFirst : new boolean[] { true, false })
1934          for (Integer v1 : new Integer[] { 1, null })
1935 <        for (Integer v2 : new Integer[] { 2, null }) {
1009 <
1935 >    {
1936          final CompletableFuture<Integer> f = new CompletableFuture<>();
1937          final CompletableFuture<Integer> g = new CompletableFuture<>();
1938 <        final Noop r = new Noop();
1939 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1938 >        final CFException ex = new CFException();
1939 >        final SubtractAction r1 = new SubtractAction(m);
1940 >        final SubtractAction r2 = new SubtractAction(m);
1941 >        final SubtractAction r3 = new SubtractAction(m);
1942 >
1943 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1944 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1945 >        final Callable<Boolean> complete1 = failFirst ?
1946 >            () -> fst.completeExceptionally(ex) :
1947 >            () -> fst.complete(v1);
1948 >        final Callable<Boolean> complete2 = failFirst ?
1949 >            () -> snd.complete(v1) :
1950 >            () -> snd.completeExceptionally(ex);
1951 >
1952 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1953 >        assertTrue(complete1.call());
1954 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1955 >        checkIncomplete(h1);
1956 >        checkIncomplete(h2);
1957 >        assertTrue(complete2.call());
1958 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1959 >
1960 >        checkCompletedWithWrappedException(h1, ex);
1961 >        checkCompletedWithWrappedException(h2, ex);
1962 >        checkCompletedWithWrappedException(h3, ex);
1963 >        r1.assertNotInvoked();
1964 >        r2.assertNotInvoked();
1965 >        r3.assertNotInvoked();
1966 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1967 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1968 >    }}
1969  
1970 <        g.complete(v2);
1971 <        checkIncomplete(h);
1972 <        assertFalse(r.ran);
1973 <        f.complete(v1);
1970 >    /**
1971 >     * thenAcceptBoth result completes exceptionally if either source cancelled
1972 >     */
1973 >    public void testThenAcceptBoth_sourceCancelled() throws Throwable {
1974 >        for (ExecutionMode m : ExecutionMode.values())
1975 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1976 >        for (boolean fFirst : new boolean[] { true, false })
1977 >        for (boolean failFirst : new boolean[] { true, false })
1978 >        for (Integer v1 : new Integer[] { 1, null })
1979 >    {
1980 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1981 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1982 >        final SubtractAction r1 = new SubtractAction(m);
1983 >        final SubtractAction r2 = new SubtractAction(m);
1984 >        final SubtractAction r3 = new SubtractAction(m);
1985 >
1986 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1987 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1988 >        final Callable<Boolean> complete1 = failFirst ?
1989 >            () -> fst.cancel(mayInterruptIfRunning) :
1990 >            () -> fst.complete(v1);
1991 >        final Callable<Boolean> complete2 = failFirst ?
1992 >            () -> snd.complete(v1) :
1993 >            () -> snd.cancel(mayInterruptIfRunning);
1994 >
1995 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1996 >        assertTrue(complete1.call());
1997 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1998 >        checkIncomplete(h1);
1999 >        checkIncomplete(h2);
2000 >        assertTrue(complete2.call());
2001 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
2002 >
2003 >        checkCompletedWithWrappedCancellationException(h1);
2004 >        checkCompletedWithWrappedCancellationException(h2);
2005 >        checkCompletedWithWrappedCancellationException(h3);
2006 >        r1.assertNotInvoked();
2007 >        r2.assertNotInvoked();
2008 >        r3.assertNotInvoked();
2009 >        checkCompletedNormally(failFirst ? snd : fst, v1);
2010 >        checkCancelled(failFirst ? fst : snd);
2011 >    }}
2012  
2013 <        checkCompletedNormally(h, null);
2014 <        assertTrue(r.ran);
2013 >    /**
2014 >     * thenAcceptBoth result completes exceptionally if action does
2015 >     */
2016 >    public void testThenAcceptBoth_actionFailed() {
2017 >        for (ExecutionMode m : ExecutionMode.values())
2018 >        for (boolean fFirst : new boolean[] { true, false })
2019 >        for (Integer v1 : new Integer[] { 1, null })
2020 >        for (Integer v2 : new Integer[] { 2, null })
2021 >    {
2022 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2023 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2024 >        final FailingBiConsumer r1 = new FailingBiConsumer(m);
2025 >        final FailingBiConsumer r2 = new FailingBiConsumer(m);
2026 >        final FailingBiConsumer r3 = new FailingBiConsumer(m);
2027 >
2028 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2029 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2030 >        final Integer w1 =  fFirst ? v1 : v2;
2031 >        final Integer w2 = !fFirst ? v1 : v2;
2032 >
2033 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
2034 >        assertTrue(fst.complete(w1));
2035 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
2036 >        assertTrue(snd.complete(w2));
2037 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
2038 >
2039 >        checkCompletedWithWrappedException(h1, r1.ex);
2040 >        checkCompletedWithWrappedException(h2, r2.ex);
2041 >        checkCompletedWithWrappedException(h3, r3.ex);
2042 >        r1.assertInvoked();
2043 >        r2.assertInvoked();
2044 >        r3.assertInvoked();
2045          checkCompletedNormally(f, v1);
2046          checkCompletedNormally(g, v2);
2047 <        }
1025 <    }
2047 >    }}
2048  
2049 <    public void testRunAfterBoth_normalCompletion3() {
2049 >    /**
2050 >     * runAfterBoth result completes normally after normal
2051 >     * completion of sources
2052 >     */
2053 >    public void testRunAfterBoth_normalCompletion() {
2054          for (ExecutionMode m : ExecutionMode.values())
2055 +        for (boolean fFirst : new boolean[] { true, false })
2056          for (Integer v1 : new Integer[] { 1, null })
2057 <        for (Integer v2 : new Integer[] { 2, null }) {
2057 >        for (Integer v2 : new Integer[] { 2, null })
2058 >    {
2059 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2060 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2061 >        final Noop r1 = new Noop(m);
2062 >        final Noop r2 = new Noop(m);
2063 >        final Noop r3 = new Noop(m);
2064 >
2065 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2066 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2067 >        final Integer w1 =  fFirst ? v1 : v2;
2068 >        final Integer w2 = !fFirst ? v1 : v2;
2069 >
2070 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2071 >        assertTrue(fst.complete(w1));
2072 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2073 >        checkIncomplete(h1);
2074 >        checkIncomplete(h2);
2075 >        r1.assertNotInvoked();
2076 >        r2.assertNotInvoked();
2077 >        assertTrue(snd.complete(w2));
2078 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2079 >
2080 >        checkCompletedNormally(h1, null);
2081 >        checkCompletedNormally(h2, null);
2082 >        checkCompletedNormally(h3, null);
2083 >        r1.assertInvoked();
2084 >        r2.assertInvoked();
2085 >        r3.assertInvoked();
2086 >        checkCompletedNormally(f, v1);
2087 >        checkCompletedNormally(g, v2);
2088 >    }}
2089  
2090 +    /**
2091 +     * runAfterBoth result completes exceptionally after exceptional
2092 +     * completion of either source
2093 +     */
2094 +    public void testRunAfterBoth_exceptionalCompletion() throws Throwable {
2095 +        for (ExecutionMode m : ExecutionMode.values())
2096 +        for (boolean fFirst : new boolean[] { true, false })
2097 +        for (boolean failFirst : new boolean[] { true, false })
2098 +        for (Integer v1 : new Integer[] { 1, null })
2099 +    {
2100          final CompletableFuture<Integer> f = new CompletableFuture<>();
2101          final CompletableFuture<Integer> g = new CompletableFuture<>();
2102 <        final Noop r = new Noop();
2102 >        final CFException ex = new CFException();
2103 >        final Noop r1 = new Noop(m);
2104 >        final Noop r2 = new Noop(m);
2105 >        final Noop r3 = new Noop(m);
2106 >
2107 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2108 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2109 >        final Callable<Boolean> complete1 = failFirst ?
2110 >            () -> fst.completeExceptionally(ex) :
2111 >            () -> fst.complete(v1);
2112 >        final Callable<Boolean> complete2 = failFirst ?
2113 >            () -> snd.complete(v1) :
2114 >            () -> snd.completeExceptionally(ex);
2115 >
2116 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2117 >        assertTrue(complete1.call());
2118 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2119 >        checkIncomplete(h1);
2120 >        checkIncomplete(h2);
2121 >        assertTrue(complete2.call());
2122 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2123 >
2124 >        checkCompletedWithWrappedException(h1, ex);
2125 >        checkCompletedWithWrappedException(h2, ex);
2126 >        checkCompletedWithWrappedException(h3, ex);
2127 >        r1.assertNotInvoked();
2128 >        r2.assertNotInvoked();
2129 >        r3.assertNotInvoked();
2130 >        checkCompletedNormally(failFirst ? snd : fst, v1);
2131 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
2132 >    }}
2133  
2134 <        g.complete(v2);
2135 <        f.complete(v1);
2136 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2134 >    /**
2135 >     * runAfterBoth result completes exceptionally if either source cancelled
2136 >     */
2137 >    public void testRunAfterBoth_sourceCancelled() throws Throwable {
2138 >        for (ExecutionMode m : ExecutionMode.values())
2139 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2140 >        for (boolean fFirst : new boolean[] { true, false })
2141 >        for (boolean failFirst : new boolean[] { true, false })
2142 >        for (Integer v1 : new Integer[] { 1, null })
2143 >    {
2144 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2145 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2146 >        final Noop r1 = new Noop(m);
2147 >        final Noop r2 = new Noop(m);
2148 >        final Noop r3 = new Noop(m);
2149 >
2150 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2151 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2152 >        final Callable<Boolean> complete1 = failFirst ?
2153 >            () -> fst.cancel(mayInterruptIfRunning) :
2154 >            () -> fst.complete(v1);
2155 >        final Callable<Boolean> complete2 = failFirst ?
2156 >            () -> snd.complete(v1) :
2157 >            () -> snd.cancel(mayInterruptIfRunning);
2158 >
2159 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2160 >        assertTrue(complete1.call());
2161 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2162 >        checkIncomplete(h1);
2163 >        checkIncomplete(h2);
2164 >        assertTrue(complete2.call());
2165 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2166 >
2167 >        checkCompletedWithWrappedCancellationException(h1);
2168 >        checkCompletedWithWrappedCancellationException(h2);
2169 >        checkCompletedWithWrappedCancellationException(h3);
2170 >        r1.assertNotInvoked();
2171 >        r2.assertNotInvoked();
2172 >        r3.assertNotInvoked();
2173 >        checkCompletedNormally(failFirst ? snd : fst, v1);
2174 >        checkCancelled(failFirst ? fst : snd);
2175 >    }}
2176  
2177 <        checkCompletedNormally(h, null);
2178 <        assertTrue(r.ran);
2177 >    /**
2178 >     * runAfterBoth result completes exceptionally if action does
2179 >     */
2180 >    public void testRunAfterBoth_actionFailed() {
2181 >        for (ExecutionMode m : ExecutionMode.values())
2182 >        for (boolean fFirst : new boolean[] { true, false })
2183 >        for (Integer v1 : new Integer[] { 1, null })
2184 >        for (Integer v2 : new Integer[] { 2, null })
2185 >    {
2186 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2187 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2188 >        final FailingRunnable r1 = new FailingRunnable(m);
2189 >        final FailingRunnable r2 = new FailingRunnable(m);
2190 >        final FailingRunnable r3 = new FailingRunnable(m);
2191 >
2192 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2193 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2194 >        final Integer w1 =  fFirst ? v1 : v2;
2195 >        final Integer w2 = !fFirst ? v1 : v2;
2196 >
2197 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2198 >        assertTrue(fst.complete(w1));
2199 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2200 >        assertTrue(snd.complete(w2));
2201 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2202 >
2203 >        checkCompletedWithWrappedException(h1, r1.ex);
2204 >        checkCompletedWithWrappedException(h2, r2.ex);
2205 >        checkCompletedWithWrappedException(h3, r3.ex);
2206 >        r1.assertInvoked();
2207 >        r2.assertInvoked();
2208 >        r3.assertInvoked();
2209          checkCompletedNormally(f, v1);
2210          checkCompletedNormally(g, v2);
2211 <        }
1045 <    }
2211 >    }}
2212  
2213 <    public void testRunAfterBoth_normalCompletion4() {
2213 >    /**
2214 >     * applyToEither result completes normally after normal completion
2215 >     * of either source
2216 >     */
2217 >    public void testApplyToEither_normalCompletion() {
2218          for (ExecutionMode m : ExecutionMode.values())
2219          for (Integer v1 : new Integer[] { 1, null })
2220 <        for (Integer v2 : new Integer[] { 2, null }) {
2221 <
2220 >        for (Integer v2 : new Integer[] { 2, null })
2221 >    {
2222          final CompletableFuture<Integer> f = new CompletableFuture<>();
2223          final CompletableFuture<Integer> g = new CompletableFuture<>();
2224 <        final Noop r = new Noop();
2224 >        final IncFunction[] rs = new IncFunction[6];
2225 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2226  
2227 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2228 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2229 +        checkIncomplete(h0);
2230 +        checkIncomplete(h1);
2231 +        rs[0].assertNotInvoked();
2232 +        rs[1].assertNotInvoked();
2233          f.complete(v1);
2234 +        checkCompletedNormally(h0, inc(v1));
2235 +        checkCompletedNormally(h1, inc(v1));
2236 +        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2237 +        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2238 +        checkCompletedNormally(h2, inc(v1));
2239 +        checkCompletedNormally(h3, inc(v1));
2240          g.complete(v2);
1058        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2241  
2242 <        checkCompletedNormally(h, null);
2243 <        assertTrue(r.ran);
2242 >        // unspecified behavior - both source completions available
2243 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2244 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2245 >        rs[4].assertValue(h4.join());
2246 >        rs[5].assertValue(h5.join());
2247 >        assertTrue(Objects.equals(inc(v1), h4.join()) ||
2248 >                   Objects.equals(inc(v2), h4.join()));
2249 >        assertTrue(Objects.equals(inc(v1), h5.join()) ||
2250 >                   Objects.equals(inc(v2), h5.join()));
2251 >
2252          checkCompletedNormally(f, v1);
2253          checkCompletedNormally(g, v2);
2254 <        }
2255 <    }
2254 >        checkCompletedNormally(h0, inc(v1));
2255 >        checkCompletedNormally(h1, inc(v1));
2256 >        checkCompletedNormally(h2, inc(v1));
2257 >        checkCompletedNormally(h3, inc(v1));
2258 >        for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1));
2259 >    }}
2260  
2261      /**
2262 <     * runAfterBoth result completes exceptionally after exceptional
2262 >     * applyToEither result completes exceptionally after exceptional
2263       * completion of either source
2264       */
2265 <    public void testRunAfterBoth_exceptionalCompletion1() {
2265 >    public void testApplyToEither_exceptionalCompletion() {
2266          for (ExecutionMode m : ExecutionMode.values())
2267 <        for (Integer v1 : new Integer[] { 1, null }) {
2268 <
2267 >        for (Integer v1 : new Integer[] { 1, null })
2268 >    {
2269          final CompletableFuture<Integer> f = new CompletableFuture<>();
2270          final CompletableFuture<Integer> g = new CompletableFuture<>();
1077        final Noop r = new Noop();
1078        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2271          final CFException ex = new CFException();
2272 +        final IncFunction[] rs = new IncFunction[6];
2273 +        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2274  
2275 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2276 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2277 +        checkIncomplete(h0);
2278 +        checkIncomplete(h1);
2279 +        rs[0].assertNotInvoked();
2280 +        rs[1].assertNotInvoked();
2281          f.completeExceptionally(ex);
2282 <        checkIncomplete(h);
2282 >        checkCompletedWithWrappedException(h0, ex);
2283 >        checkCompletedWithWrappedException(h1, ex);
2284 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2285 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2286 >        checkCompletedWithWrappedException(h2, ex);
2287 >        checkCompletedWithWrappedException(h3, ex);
2288          g.complete(v1);
2289  
2290 <        checkCompletedWithWrappedCFException(h, ex);
2291 <        checkCompletedWithWrappedCFException(f, ex);
2292 <        assertFalse(r.ran);
2293 <        checkCompletedNormally(g, v1);
2290 >        // unspecified behavior - both source completions available
2291 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2292 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2293 >        try {
2294 >            assertEquals(inc(v1), h4.join());
2295 >            rs[4].assertValue(inc(v1));
2296 >        } catch (CompletionException ok) {
2297 >            checkCompletedWithWrappedException(h4, ex);
2298 >            rs[4].assertNotInvoked();
2299 >        }
2300 >        try {
2301 >            assertEquals(inc(v1), h5.join());
2302 >            rs[5].assertValue(inc(v1));
2303 >        } catch (CompletionException ok) {
2304 >            checkCompletedWithWrappedException(h5, ex);
2305 >            rs[5].assertNotInvoked();
2306          }
1090    }
2307  
2308 <    public void testRunAfterBoth_exceptionalCompletion2() {
2309 <        for (ExecutionMode m : ExecutionMode.values())
2310 <        for (Integer v1 : new Integer[] { 1, null }) {
2308 >        checkCompletedExceptionally(f, ex);
2309 >        checkCompletedNormally(g, v1);
2310 >        checkCompletedWithWrappedException(h0, ex);
2311 >        checkCompletedWithWrappedException(h1, ex);
2312 >        checkCompletedWithWrappedException(h2, ex);
2313 >        checkCompletedWithWrappedException(h3, ex);
2314 >        checkCompletedWithWrappedException(h4, ex);
2315 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2316 >    }}
2317  
2318 +    public void testApplyToEither_exceptionalCompletion2() {
2319 +        for (ExecutionMode m : ExecutionMode.values())
2320 +        for (boolean fFirst : new boolean[] { true, false })
2321 +        for (Integer v1 : new Integer[] { 1, null })
2322 +    {
2323          final CompletableFuture<Integer> f = new CompletableFuture<>();
2324          final CompletableFuture<Integer> g = new CompletableFuture<>();
1098        final Noop r = new Noop();
1099        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2325          final CFException ex = new CFException();
2326 +        final IncFunction[] rs = new IncFunction[6];
2327 +        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2328  
2329 <        g.completeExceptionally(ex);
2330 <        checkIncomplete(h);
2331 <        f.complete(v1);
2329 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2330 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2331 >        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2332 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2333 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2334 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2335  
2336 <        checkCompletedWithWrappedCFException(h, ex);
2337 <        checkCompletedWithWrappedCFException(g, ex);
2338 <        assertFalse(r.ran);
2339 <        checkCompletedNormally(f, v1);
2336 >        // unspecified behavior - both source completions available
2337 >        try {
2338 >            assertEquals(inc(v1), h0.join());
2339 >            rs[0].assertValue(inc(v1));
2340 >        } catch (CompletionException ok) {
2341 >            checkCompletedWithWrappedException(h0, ex);
2342 >            rs[0].assertNotInvoked();
2343 >        }
2344 >        try {
2345 >            assertEquals(inc(v1), h1.join());
2346 >            rs[1].assertValue(inc(v1));
2347 >        } catch (CompletionException ok) {
2348 >            checkCompletedWithWrappedException(h1, ex);
2349 >            rs[1].assertNotInvoked();
2350 >        }
2351 >        try {
2352 >            assertEquals(inc(v1), h2.join());
2353 >            rs[2].assertValue(inc(v1));
2354 >        } catch (CompletionException ok) {
2355 >            checkCompletedWithWrappedException(h2, ex);
2356 >            rs[2].assertNotInvoked();
2357 >        }
2358 >        try {
2359 >            assertEquals(inc(v1), h3.join());
2360 >            rs[3].assertValue(inc(v1));
2361 >        } catch (CompletionException ok) {
2362 >            checkCompletedWithWrappedException(h3, ex);
2363 >            rs[3].assertNotInvoked();
2364          }
1111    }
2365  
2366 <    public void testRunAfterBoth_exceptionalCompletion3() {
2367 <        for (ExecutionMode m : ExecutionMode.values())
2368 <        for (Integer v1 : new Integer[] { 1, null }) {
2366 >        checkCompletedNormally(f, v1);
2367 >        checkCompletedExceptionally(g, ex);
2368 >    }}
2369  
2370 +    /**
2371 +     * applyToEither result completes exceptionally if either source cancelled
2372 +     */
2373 +    public void testApplyToEither_sourceCancelled() {
2374 +        for (ExecutionMode m : ExecutionMode.values())
2375 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2376 +        for (Integer v1 : new Integer[] { 1, null })
2377 +    {
2378          final CompletableFuture<Integer> f = new CompletableFuture<>();
2379          final CompletableFuture<Integer> g = new CompletableFuture<>();
2380 <        final Noop r = new Noop();
2381 <        final CFException ex = new CFException();
2380 >        final IncFunction[] rs = new IncFunction[6];
2381 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2382  
2383 <        g.completeExceptionally(ex);
2384 <        f.complete(v1);
2385 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2383 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2384 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2385 >        checkIncomplete(h0);
2386 >        checkIncomplete(h1);
2387 >        rs[0].assertNotInvoked();
2388 >        rs[1].assertNotInvoked();
2389 >        f.cancel(mayInterruptIfRunning);
2390 >        checkCompletedWithWrappedCancellationException(h0);
2391 >        checkCompletedWithWrappedCancellationException(h1);
2392 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2393 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2394 >        checkCompletedWithWrappedCancellationException(h2);
2395 >        checkCompletedWithWrappedCancellationException(h3);
2396 >        g.complete(v1);
2397  
2398 <        checkCompletedWithWrappedCFException(h, ex);
2399 <        checkCompletedWithWrappedCFException(g, ex);
2400 <        assertFalse(r.ran);
2401 <        checkCompletedNormally(f, v1);
2398 >        // unspecified behavior - both source completions available
2399 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2400 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2401 >        try {
2402 >            assertEquals(inc(v1), h4.join());
2403 >            rs[4].assertValue(inc(v1));
2404 >        } catch (CompletionException ok) {
2405 >            checkCompletedWithWrappedCancellationException(h4);
2406 >            rs[4].assertNotInvoked();
2407 >        }
2408 >        try {
2409 >            assertEquals(inc(v1), h5.join());
2410 >            rs[5].assertValue(inc(v1));
2411 >        } catch (CompletionException ok) {
2412 >            checkCompletedWithWrappedCancellationException(h5);
2413 >            rs[5].assertNotInvoked();
2414          }
1131    }
2415  
2416 <    public void testRunAfterBoth_exceptionalCompletion4() {
2417 <        for (ExecutionMode m : ExecutionMode.values())
2418 <        for (Integer v1 : new Integer[] { 1, null }) {
2416 >        checkCancelled(f);
2417 >        checkCompletedNormally(g, v1);
2418 >        checkCompletedWithWrappedCancellationException(h0);
2419 >        checkCompletedWithWrappedCancellationException(h1);
2420 >        checkCompletedWithWrappedCancellationException(h2);
2421 >        checkCompletedWithWrappedCancellationException(h3);
2422 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2423 >    }}
2424  
2425 +    public void testApplyToEither_sourceCancelled2() {
2426 +        for (ExecutionMode m : ExecutionMode.values())
2427 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2428 +        for (boolean fFirst : new boolean[] { true, false })
2429 +        for (Integer v1 : new Integer[] { 1, null })
2430 +    {
2431          final CompletableFuture<Integer> f = new CompletableFuture<>();
2432          final CompletableFuture<Integer> g = new CompletableFuture<>();
2433 <        final Noop r = new Noop();
2434 <        final CFException ex = new CFException();
2433 >        final IncFunction[] rs = new IncFunction[6];
2434 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2435  
2436 <        f.completeExceptionally(ex);
2437 <        g.complete(v1);
2438 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2436 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2437 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2438 >        assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2439 >        assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2440 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2441 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2442  
2443 <        checkCompletedWithWrappedCFException(h, ex);
2444 <        checkCompletedWithWrappedCFException(f, ex);
2445 <        assertFalse(r.ran);
2446 <        checkCompletedNormally(g, v1);
2443 >        // unspecified behavior - both source completions available
2444 >        try {
2445 >            assertEquals(inc(v1), h0.join());
2446 >            rs[0].assertValue(inc(v1));
2447 >        } catch (CompletionException ok) {
2448 >            checkCompletedWithWrappedCancellationException(h0);
2449 >            rs[0].assertNotInvoked();
2450          }
2451 <    }
2451 >        try {
2452 >            assertEquals(inc(v1), h1.join());
2453 >            rs[1].assertValue(inc(v1));
2454 >        } catch (CompletionException ok) {
2455 >            checkCompletedWithWrappedCancellationException(h1);
2456 >            rs[1].assertNotInvoked();
2457 >        }
2458 >        try {
2459 >            assertEquals(inc(v1), h2.join());
2460 >            rs[2].assertValue(inc(v1));
2461 >        } catch (CompletionException ok) {
2462 >            checkCompletedWithWrappedCancellationException(h2);
2463 >            rs[2].assertNotInvoked();
2464 >        }
2465 >        try {
2466 >            assertEquals(inc(v1), h3.join());
2467 >            rs[3].assertValue(inc(v1));
2468 >        } catch (CompletionException ok) {
2469 >            checkCompletedWithWrappedCancellationException(h3);
2470 >            rs[3].assertNotInvoked();
2471 >        }
2472 >
2473 >        checkCompletedNormally(f, v1);
2474 >        checkCancelled(g);
2475 >    }}
2476  
2477      /**
2478 <     * runAfterBoth result completes exceptionally if action does
2478 >     * applyToEither result completes exceptionally if action does
2479       */
2480 <    public void testRunAfterBoth_actionFailed1() {
2480 >    public void testApplyToEither_actionFailed() {
2481          for (ExecutionMode m : ExecutionMode.values())
2482          for (Integer v1 : new Integer[] { 1, null })
2483 <        for (Integer v2 : new Integer[] { 2, null }) {
2484 <
2483 >        for (Integer v2 : new Integer[] { 2, null })
2484 >    {
2485          final CompletableFuture<Integer> f = new CompletableFuture<>();
2486          final CompletableFuture<Integer> g = new CompletableFuture<>();
2487 <        final FailingNoop r = new FailingNoop();
2488 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2487 >        final FailingFunction[] rs = new FailingFunction[6];
2488 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
2489  
2490 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2491 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2492          f.complete(v1);
2493 <        checkIncomplete(h);
2493 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2494 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2495 >        checkCompletedWithWrappedException(h0, rs[0].ex);
2496 >        checkCompletedWithWrappedException(h1, rs[1].ex);
2497 >        checkCompletedWithWrappedException(h2, rs[2].ex);
2498 >        checkCompletedWithWrappedException(h3, rs[3].ex);
2499 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2500 >
2501          g.complete(v2);
2502  
2503 <        checkCompletedWithWrappedCFException(h);
2503 >        // unspecified behavior - both source completions available
2504 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2505 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2506 >
2507 >        checkCompletedWithWrappedException(h4, rs[4].ex);
2508 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2509 >                   Objects.equals(v2, rs[4].value));
2510 >        checkCompletedWithWrappedException(h5, rs[5].ex);
2511 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2512 >                   Objects.equals(v2, rs[5].value));
2513 >
2514          checkCompletedNormally(f, v1);
2515          checkCompletedNormally(g, v2);
2516 <        }
1174 <    }
2516 >    }}
2517  
2518 <    public void testRunAfterBoth_actionFailed2() {
2518 >    /**
2519 >     * acceptEither result completes normally after normal completion
2520 >     * of either source
2521 >     */
2522 >    public void testAcceptEither_normalCompletion() {
2523          for (ExecutionMode m : ExecutionMode.values())
2524          for (Integer v1 : new Integer[] { 1, null })
2525 <        for (Integer v2 : new Integer[] { 2, null }) {
2526 <
2525 >        for (Integer v2 : new Integer[] { 2, null })
2526 >    {
2527          final CompletableFuture<Integer> f = new CompletableFuture<>();
2528          final CompletableFuture<Integer> g = new CompletableFuture<>();
2529 <        final FailingNoop r = new FailingNoop();
2530 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2529 >        final NoopConsumer[] rs = new NoopConsumer[6];
2530 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2531  
2532 <        g.complete(v2);
2533 <        checkIncomplete(h);
2532 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2533 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2534 >        checkIncomplete(h0);
2535 >        checkIncomplete(h1);
2536 >        rs[0].assertNotInvoked();
2537 >        rs[1].assertNotInvoked();
2538          f.complete(v1);
2539 +        checkCompletedNormally(h0, null);
2540 +        checkCompletedNormally(h1, null);
2541 +        rs[0].assertValue(v1);
2542 +        rs[1].assertValue(v1);
2543 +        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2544 +        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2545 +        checkCompletedNormally(h2, null);
2546 +        checkCompletedNormally(h3, null);
2547 +        rs[2].assertValue(v1);
2548 +        rs[3].assertValue(v1);
2549 +        g.complete(v2);
2550 +
2551 +        // unspecified behavior - both source completions available
2552 +        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2553 +        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2554 +        checkCompletedNormally(h4, null);
2555 +        checkCompletedNormally(h5, null);
2556 +        assertTrue(Objects.equals(v1, rs[4].value) ||
2557 +                   Objects.equals(v2, rs[4].value));
2558 +        assertTrue(Objects.equals(v1, rs[5].value) ||
2559 +                   Objects.equals(v2, rs[5].value));
2560  
1190        checkCompletedWithWrappedCFException(h);
2561          checkCompletedNormally(f, v1);
2562          checkCompletedNormally(g, v2);
2563 <        }
2564 <    }
2563 >        checkCompletedNormally(h0, null);
2564 >        checkCompletedNormally(h1, null);
2565 >        checkCompletedNormally(h2, null);
2566 >        checkCompletedNormally(h3, null);
2567 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2568 >    }}
2569  
2570      /**
2571 <     * runAfterBoth result completes exceptionally if either source cancelled
2571 >     * acceptEither result completes exceptionally after exceptional
2572 >     * completion of either source
2573       */
2574 <    public void testRunAfterBoth_sourceCancelled1() {
2574 >    public void testAcceptEither_exceptionalCompletion() {
2575          for (ExecutionMode m : ExecutionMode.values())
2576 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2577 <        for (Integer v1 : new Integer[] { 1, null }) {
1203 <
2576 >        for (Integer v1 : new Integer[] { 1, null })
2577 >    {
2578          final CompletableFuture<Integer> f = new CompletableFuture<>();
2579          final CompletableFuture<Integer> g = new CompletableFuture<>();
2580 <        final Noop r = new Noop();
2581 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2580 >        final CFException ex = new CFException();
2581 >        final NoopConsumer[] rs = new NoopConsumer[6];
2582 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2583 >
2584 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2585 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2586 >        checkIncomplete(h0);
2587 >        checkIncomplete(h1);
2588 >        rs[0].assertNotInvoked();
2589 >        rs[1].assertNotInvoked();
2590 >        f.completeExceptionally(ex);
2591 >        checkCompletedWithWrappedException(h0, ex);
2592 >        checkCompletedWithWrappedException(h1, ex);
2593 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2594 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2595 >        checkCompletedWithWrappedException(h2, ex);
2596 >        checkCompletedWithWrappedException(h3, ex);
2597  
1209        assertTrue(f.cancel(mayInterruptIfRunning));
1210        checkIncomplete(h);
2598          g.complete(v1);
2599  
2600 <        checkCompletedWithWrappedCancellationException(h);
2601 <        checkCancelled(f);
2602 <        assertFalse(r.ran);
2603 <        checkCompletedNormally(g, v1);
2600 >        // unspecified behavior - both source completions available
2601 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2602 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2603 >        try {
2604 >            assertNull(h4.join());
2605 >            rs[4].assertValue(v1);
2606 >        } catch (CompletionException ok) {
2607 >            checkCompletedWithWrappedException(h4, ex);
2608 >            rs[4].assertNotInvoked();
2609 >        }
2610 >        try {
2611 >            assertNull(h5.join());
2612 >            rs[5].assertValue(v1);
2613 >        } catch (CompletionException ok) {
2614 >            checkCompletedWithWrappedException(h5, ex);
2615 >            rs[5].assertNotInvoked();
2616          }
1218    }
2617  
2618 <    public void testRunAfterBoth_sourceCancelled2() {
2619 <        for (ExecutionMode m : ExecutionMode.values())
2620 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2621 <        for (Integer v1 : new Integer[] { 1, null }) {
2618 >        checkCompletedExceptionally(f, ex);
2619 >        checkCompletedNormally(g, v1);
2620 >        checkCompletedWithWrappedException(h0, ex);
2621 >        checkCompletedWithWrappedException(h1, ex);
2622 >        checkCompletedWithWrappedException(h2, ex);
2623 >        checkCompletedWithWrappedException(h3, ex);
2624 >        checkCompletedWithWrappedException(h4, ex);
2625 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2626 >    }}
2627  
2628 +    public void testAcceptEither_exceptionalCompletion2() {
2629 +        for (ExecutionMode m : ExecutionMode.values())
2630 +        for (boolean fFirst : new boolean[] { true, false })
2631 +        for (Integer v1 : new Integer[] { 1, null })
2632 +    {
2633          final CompletableFuture<Integer> f = new CompletableFuture<>();
2634          final CompletableFuture<Integer> g = new CompletableFuture<>();
2635 <        final Noop r = new Noop();
2636 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2635 >        final CFException ex = new CFException();
2636 >        final NoopConsumer[] rs = new NoopConsumer[6];
2637 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2638  
2639 <        assertTrue(g.cancel(mayInterruptIfRunning));
2640 <        checkIncomplete(h);
2641 <        f.complete(v1);
2639 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2640 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2641 >        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2642 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2643 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2644 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2645  
2646 <        checkCompletedWithWrappedCancellationException(h);
2647 <        checkCancelled(g);
2648 <        assertFalse(r.ran);
2649 <        checkCompletedNormally(f, v1);
2646 >        // unspecified behavior - both source completions available
2647 >        try {
2648 >            assertNull(h0.join());
2649 >            rs[0].assertValue(v1);
2650 >        } catch (CompletionException ok) {
2651 >            checkCompletedWithWrappedException(h0, ex);
2652 >            rs[0].assertNotInvoked();
2653 >        }
2654 >        try {
2655 >            assertNull(h1.join());
2656 >            rs[1].assertValue(v1);
2657 >        } catch (CompletionException ok) {
2658 >            checkCompletedWithWrappedException(h1, ex);
2659 >            rs[1].assertNotInvoked();
2660 >        }
2661 >        try {
2662 >            assertNull(h2.join());
2663 >            rs[2].assertValue(v1);
2664 >        } catch (CompletionException ok) {
2665 >            checkCompletedWithWrappedException(h2, ex);
2666 >            rs[2].assertNotInvoked();
2667 >        }
2668 >        try {
2669 >            assertNull(h3.join());
2670 >            rs[3].assertValue(v1);
2671 >        } catch (CompletionException ok) {
2672 >            checkCompletedWithWrappedException(h3, ex);
2673 >            rs[3].assertNotInvoked();
2674          }
1239    }
1240
1241    public void testRunAfterBoth_sourceCancelled3() {
1242        for (ExecutionMode m : ExecutionMode.values())
1243        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1244        for (Integer v1 : new Integer[] { 1, null }) {
1245
1246        final CompletableFuture<Integer> f = new CompletableFuture<>();
1247        final CompletableFuture<Integer> g = new CompletableFuture<>();
1248        final Noop r = new Noop();
1249
1250        assertTrue(g.cancel(mayInterruptIfRunning));
1251        f.complete(v1);
1252        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2675  
1254        checkCompletedWithWrappedCancellationException(h);
1255        checkCancelled(g);
1256        assertFalse(r.ran);
2676          checkCompletedNormally(f, v1);
2677 <        }
2678 <    }
2677 >        checkCompletedExceptionally(g, ex);
2678 >    }}
2679  
2680 <    public void testRunAfterBoth_sourceCancelled4() {
2680 >    /**
2681 >     * acceptEither result completes exceptionally if either source cancelled
2682 >     */
2683 >    public void testAcceptEither_sourceCancelled() {
2684          for (ExecutionMode m : ExecutionMode.values())
2685          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2686 <        for (Integer v1 : new Integer[] { 1, null }) {
2687 <
2686 >        for (Integer v1 : new Integer[] { 1, null })
2687 >    {
2688          final CompletableFuture<Integer> f = new CompletableFuture<>();
2689          final CompletableFuture<Integer> g = new CompletableFuture<>();
2690 <        final Noop r = new Noop();
2690 >        final NoopConsumer[] rs = new NoopConsumer[6];
2691 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2692 >
2693 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2694 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2695 >        checkIncomplete(h0);
2696 >        checkIncomplete(h1);
2697 >        rs[0].assertNotInvoked();
2698 >        rs[1].assertNotInvoked();
2699 >        f.cancel(mayInterruptIfRunning);
2700 >        checkCompletedWithWrappedCancellationException(h0);
2701 >        checkCompletedWithWrappedCancellationException(h1);
2702 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2703 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2704 >        checkCompletedWithWrappedCancellationException(h2);
2705 >        checkCompletedWithWrappedCancellationException(h3);
2706  
1270        assertTrue(f.cancel(mayInterruptIfRunning));
2707          g.complete(v1);
1272        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2708  
2709 <        checkCompletedWithWrappedCancellationException(h);
2709 >        // unspecified behavior - both source completions available
2710 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2711 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2712 >        try {
2713 >            assertNull(h4.join());
2714 >            rs[4].assertValue(v1);
2715 >        } catch (CompletionException ok) {
2716 >            checkCompletedWithWrappedCancellationException(h4);
2717 >            rs[4].assertNotInvoked();
2718 >        }
2719 >        try {
2720 >            assertNull(h5.join());
2721 >            rs[5].assertValue(v1);
2722 >        } catch (CompletionException ok) {
2723 >            checkCompletedWithWrappedCancellationException(h5);
2724 >            rs[5].assertNotInvoked();
2725 >        }
2726 >
2727          checkCancelled(f);
1276        assertFalse(r.ran);
2728          checkCompletedNormally(g, v1);
2729 <        }
2730 <    }
2729 >        checkCompletedWithWrappedCancellationException(h0);
2730 >        checkCompletedWithWrappedCancellationException(h1);
2731 >        checkCompletedWithWrappedCancellationException(h2);
2732 >        checkCompletedWithWrappedCancellationException(h3);
2733 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2734 >    }}
2735  
2736      /**
2737 <     * applyToEither result completes normally after normal completion
1283 <     * of either source
2737 >     * acceptEither result completes exceptionally if action does
2738       */
2739 <    public void testApplyToEither() {
2740 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2741 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2742 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
2743 <        f.complete(one);
2744 <        checkCompletedNormally(g, two);
2745 <        f2.complete(one);
2746 <        checkCompletedNormally(g, two);
2747 <
1294 <        f = new CompletableFuture<>();
1295 <        f.complete(one);
1296 <        f2 = new CompletableFuture<>();
1297 <        g = f.applyToEither(f2, inc);
1298 <        checkCompletedNormally(g, two);
1299 <    }
2739 >    public void testAcceptEither_actionFailed() {
2740 >        for (ExecutionMode m : ExecutionMode.values())
2741 >        for (Integer v1 : new Integer[] { 1, null })
2742 >        for (Integer v2 : new Integer[] { 2, null })
2743 >    {
2744 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2745 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2746 >        final FailingConsumer[] rs = new FailingConsumer[6];
2747 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
2748  
2749 <    /**
2750 <     * applyToEither result completes exceptionally after exceptional
2751 <     * completion of either source
2752 <     */
2753 <    public void testApplyToEither2() {
2754 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2755 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2756 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
2757 <        f.completeExceptionally(new CFException());
2758 <        f2.complete(one);
1311 <        checkCompletedWithWrappedCFException(g);
2749 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2750 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2751 >        f.complete(v1);
2752 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2753 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2754 >        checkCompletedWithWrappedException(h0, rs[0].ex);
2755 >        checkCompletedWithWrappedException(h1, rs[1].ex);
2756 >        checkCompletedWithWrappedException(h2, rs[2].ex);
2757 >        checkCompletedWithWrappedException(h3, rs[3].ex);
2758 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2759  
2760 <        f = new CompletableFuture<>();
1314 <        f2 = new CompletableFuture<>();
1315 <        f2.completeExceptionally(new CFException());
1316 <        g = f.applyToEither(f2, inc);
1317 <        checkCompletedWithWrappedCFException(g);
1318 <    }
2760 >        g.complete(v2);
2761  
2762 <    /**
2763 <     * applyToEither result completes exceptionally if action does
2764 <     */
2765 <    public void testApplyToEither3() {
2766 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2767 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2768 <        FailingFunction r = new FailingFunction();
2769 <        CompletableFuture<Integer> g = f.applyToEither(f2, r);
2770 <        f2.complete(two);
2771 <        checkCompletedWithWrappedCFException(g);
1330 <    }
2762 >        // unspecified behavior - both source completions available
2763 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2764 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2765 >
2766 >        checkCompletedWithWrappedException(h4, rs[4].ex);
2767 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2768 >                   Objects.equals(v2, rs[4].value));
2769 >        checkCompletedWithWrappedException(h5, rs[5].ex);
2770 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2771 >                   Objects.equals(v2, rs[5].value));
2772  
2773 <    /**
2774 <     * applyToEither result completes exceptionally if either source cancelled
2775 <     */
1335 <    public void testApplyToEither4() {
1336 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1337 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1338 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1339 <        assertTrue(f.cancel(true));
1340 <        checkCompletedWithWrappedCancellationException(g);
1341 <        f = new CompletableFuture<>();
1342 <        f2 = new CompletableFuture<>();
1343 <        assertTrue(f2.cancel(true));
1344 <        checkCompletedWithWrappedCancellationException(g);
1345 <    }
2773 >        checkCompletedNormally(f, v1);
2774 >        checkCompletedNormally(g, v2);
2775 >    }}
2776  
2777      /**
2778 <     * acceptEither result completes normally after normal completion
2778 >     * runAfterEither result completes normally after normal completion
2779       * of either source
2780       */
2781 <    public void testAcceptEither() {
2782 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2783 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2784 <        IncAction r = new IncAction();
2785 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2786 <        f.complete(one);
2787 <        checkCompletedNormally(g, null);
2788 <        f2.complete(one);
2789 <        checkCompletedNormally(g, null);
2790 <        assertEquals(r.value, 2);
2781 >    public void testRunAfterEither_normalCompletion() {
2782 >        for (ExecutionMode m : ExecutionMode.values())
2783 >        for (Integer v1 : new Integer[] { 1, null })
2784 >        for (Integer v2 : new Integer[] { 2, null })
2785 >        for (boolean pushNop : new boolean[] { true, false })
2786 >    {
2787 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2788 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2789 >        final Noop[] rs = new Noop[6];
2790 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2791  
2792 <        r = new IncAction();
2793 <        f = new CompletableFuture<>();
2794 <        f.complete(one);
2795 <        f2 = new CompletableFuture<>();
2796 <        g = f.acceptEither(f2, r);
2797 <        checkCompletedNormally(g, null);
2798 <        assertEquals(r.value, 2);
2799 <    }
2792 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2793 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2794 >        checkIncomplete(h0);
2795 >        checkIncomplete(h1);
2796 >        rs[0].assertNotInvoked();
2797 >        rs[1].assertNotInvoked();
2798 >        if (pushNop) {          // ad hoc test of intra-completion interference
2799 >            m.thenRun(f, () -> {});
2800 >            m.thenRun(g, () -> {});
2801 >        }
2802 >        f.complete(v1);
2803 >        checkCompletedNormally(h0, null);
2804 >        checkCompletedNormally(h1, null);
2805 >        rs[0].assertInvoked();
2806 >        rs[1].assertInvoked();
2807 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2808 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2809 >        checkCompletedNormally(h2, null);
2810 >        checkCompletedNormally(h3, null);
2811 >        rs[2].assertInvoked();
2812 >        rs[3].assertInvoked();
2813 >
2814 >        g.complete(v2);
2815 >
2816 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2817 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2818 >
2819 >        checkCompletedNormally(f, v1);
2820 >        checkCompletedNormally(g, v2);
2821 >        checkCompletedNormally(h0, null);
2822 >        checkCompletedNormally(h1, null);
2823 >        checkCompletedNormally(h2, null);
2824 >        checkCompletedNormally(h3, null);
2825 >        checkCompletedNormally(h4, null);
2826 >        checkCompletedNormally(h5, null);
2827 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2828 >    }}
2829  
2830      /**
2831 <     * acceptEither result completes exceptionally after exceptional
2831 >     * runAfterEither result completes exceptionally after exceptional
2832       * completion of either source
2833       */
2834 <    public void testAcceptEither2() {
2835 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2836 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2837 <        IncAction r = new IncAction();
2838 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2839 <        f.completeExceptionally(new CFException());
2840 <        f2.complete(one);
2841 <        checkCompletedWithWrappedCFException(g);
2834 >    public void testRunAfterEither_exceptionalCompletion() {
2835 >        for (ExecutionMode m : ExecutionMode.values())
2836 >        for (Integer v1 : new Integer[] { 1, null })
2837 >    {
2838 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2839 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2840 >        final CFException ex = new CFException();
2841 >        final Noop[] rs = new Noop[6];
2842 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2843  
2844 <        r = new IncAction();
2845 <        f = new CompletableFuture<>();
2846 <        f2 = new CompletableFuture<>();
2847 <        f2.completeExceptionally(new CFException());
2848 <        g = f.acceptEither(f2, r);
2849 <        checkCompletedWithWrappedCFException(g);
2850 <    }
2844 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2845 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2846 >        checkIncomplete(h0);
2847 >        checkIncomplete(h1);
2848 >        rs[0].assertNotInvoked();
2849 >        rs[1].assertNotInvoked();
2850 >        assertTrue(f.completeExceptionally(ex));
2851 >        checkCompletedWithWrappedException(h0, ex);
2852 >        checkCompletedWithWrappedException(h1, ex);
2853 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2854 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2855 >        checkCompletedWithWrappedException(h2, ex);
2856 >        checkCompletedWithWrappedException(h3, ex);
2857 >
2858 >        assertTrue(g.complete(v1));
2859 >
2860 >        // unspecified behavior - both source completions available
2861 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2862 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2863 >        try {
2864 >            assertNull(h4.join());
2865 >            rs[4].assertInvoked();
2866 >        } catch (CompletionException ok) {
2867 >            checkCompletedWithWrappedException(h4, ex);
2868 >            rs[4].assertNotInvoked();
2869 >        }
2870 >        try {
2871 >            assertNull(h5.join());
2872 >            rs[5].assertInvoked();
2873 >        } catch (CompletionException ok) {
2874 >            checkCompletedWithWrappedException(h5, ex);
2875 >            rs[5].assertNotInvoked();
2876 >        }
2877  
2878 <    /**
2879 <     * acceptEither result completes exceptionally if action does
2880 <     */
2881 <    public void testAcceptEither3() {
2882 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2883 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2884 <        FailingConsumer r = new FailingConsumer();
2885 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2886 <        f2.complete(two);
1401 <        checkCompletedWithWrappedCFException(g);
1402 <    }
2878 >        checkCompletedExceptionally(f, ex);
2879 >        checkCompletedNormally(g, v1);
2880 >        checkCompletedWithWrappedException(h0, ex);
2881 >        checkCompletedWithWrappedException(h1, ex);
2882 >        checkCompletedWithWrappedException(h2, ex);
2883 >        checkCompletedWithWrappedException(h3, ex);
2884 >        checkCompletedWithWrappedException(h4, ex);
2885 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2886 >    }}
2887  
2888 <    /**
2889 <     * acceptEither result completes exceptionally if either source cancelled
2890 <     */
2891 <    public void testAcceptEither4() {
2892 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2893 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2894 <        IncAction r = new IncAction();
2895 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2896 <        assertTrue(f.cancel(true));
2897 <        checkCompletedWithWrappedCancellationException(g);
1414 <        f = new CompletableFuture<>();
1415 <        f2 = new CompletableFuture<>();
1416 <        assertTrue(f2.cancel(true));
1417 <        checkCompletedWithWrappedCancellationException(g);
1418 <    }
2888 >    public void testRunAfterEither_exceptionalCompletion2() {
2889 >        for (ExecutionMode m : ExecutionMode.values())
2890 >        for (boolean fFirst : new boolean[] { true, false })
2891 >        for (Integer v1 : new Integer[] { 1, null })
2892 >    {
2893 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2894 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2895 >        final CFException ex = new CFException();
2896 >        final Noop[] rs = new Noop[6];
2897 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2898  
2899 <    /**
2900 <     * runAfterEither result completes normally after normal completion
2901 <     * of either source
2902 <     */
2903 <    public void testRunAfterEither() {
2904 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1426 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1427 <        Noop r = new Noop();
1428 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1429 <        f.complete(one);
1430 <        checkCompletedNormally(g, null);
1431 <        f2.complete(one);
1432 <        checkCompletedNormally(g, null);
1433 <        assertTrue(r.ran);
2899 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2900 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2901 >        assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2902 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2903 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2904 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2905  
2906 <        r = new Noop();
2907 <        f = new CompletableFuture<>();
2908 <        f.complete(one);
2909 <        f2 = new CompletableFuture<>();
2910 <        g = f.runAfterEither(f2, r);
2911 <        checkCompletedNormally(g, null);
2912 <        assertTrue(r.ran);
2913 <    }
2906 >        // unspecified behavior - both source completions available
2907 >        try {
2908 >            assertNull(h0.join());
2909 >            rs[0].assertInvoked();
2910 >        } catch (CompletionException ok) {
2911 >            checkCompletedWithWrappedException(h0, ex);
2912 >            rs[0].assertNotInvoked();
2913 >        }
2914 >        try {
2915 >            assertNull(h1.join());
2916 >            rs[1].assertInvoked();
2917 >        } catch (CompletionException ok) {
2918 >            checkCompletedWithWrappedException(h1, ex);
2919 >            rs[1].assertNotInvoked();
2920 >        }
2921 >        try {
2922 >            assertNull(h2.join());
2923 >            rs[2].assertInvoked();
2924 >        } catch (CompletionException ok) {
2925 >            checkCompletedWithWrappedException(h2, ex);
2926 >            rs[2].assertNotInvoked();
2927 >        }
2928 >        try {
2929 >            assertNull(h3.join());
2930 >            rs[3].assertInvoked();
2931 >        } catch (CompletionException ok) {
2932 >            checkCompletedWithWrappedException(h3, ex);
2933 >            rs[3].assertNotInvoked();
2934 >        }
2935 >
2936 >        checkCompletedNormally(f, v1);
2937 >        checkCompletedExceptionally(g, ex);
2938 >    }}
2939  
2940      /**
2941 <     * runAfterEither result completes exceptionally after exceptional
1446 <     * completion of either source
2941 >     * runAfterEither result completes exceptionally if either source cancelled
2942       */
2943 <    public void testRunAfterEither2() {
2944 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2945 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2946 <        Noop r = new Noop();
2947 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2948 <        f.completeExceptionally(new CFException());
2949 <        f2.complete(one);
2950 <        checkCompletedWithWrappedCFException(g);
2943 >    public void testRunAfterEither_sourceCancelled() {
2944 >        for (ExecutionMode m : ExecutionMode.values())
2945 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2946 >        for (Integer v1 : new Integer[] { 1, null })
2947 >    {
2948 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2949 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2950 >        final Noop[] rs = new Noop[6];
2951 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2952  
2953 <        r = new Noop();
2954 <        f = new CompletableFuture<>();
2955 <        f2 = new CompletableFuture<>();
2956 <        f2.completeExceptionally(new CFException());
2957 <        g = f.runAfterEither(f2, r);
2958 <        checkCompletedWithWrappedCFException(g);
2959 <    }
2953 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2954 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2955 >        checkIncomplete(h0);
2956 >        checkIncomplete(h1);
2957 >        rs[0].assertNotInvoked();
2958 >        rs[1].assertNotInvoked();
2959 >        f.cancel(mayInterruptIfRunning);
2960 >        checkCompletedWithWrappedCancellationException(h0);
2961 >        checkCompletedWithWrappedCancellationException(h1);
2962 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2963 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2964 >        checkCompletedWithWrappedCancellationException(h2);
2965 >        checkCompletedWithWrappedCancellationException(h3);
2966 >
2967 >        assertTrue(g.complete(v1));
2968 >
2969 >        // unspecified behavior - both source completions available
2970 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2971 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2972 >        try {
2973 >            assertNull(h4.join());
2974 >            rs[4].assertInvoked();
2975 >        } catch (CompletionException ok) {
2976 >            checkCompletedWithWrappedCancellationException(h4);
2977 >            rs[4].assertNotInvoked();
2978 >        }
2979 >        try {
2980 >            assertNull(h5.join());
2981 >            rs[5].assertInvoked();
2982 >        } catch (CompletionException ok) {
2983 >            checkCompletedWithWrappedCancellationException(h5);
2984 >            rs[5].assertNotInvoked();
2985 >        }
2986 >
2987 >        checkCancelled(f);
2988 >        checkCompletedNormally(g, v1);
2989 >        checkCompletedWithWrappedCancellationException(h0);
2990 >        checkCompletedWithWrappedCancellationException(h1);
2991 >        checkCompletedWithWrappedCancellationException(h2);
2992 >        checkCompletedWithWrappedCancellationException(h3);
2993 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2994 >    }}
2995  
2996      /**
2997       * runAfterEither result completes exceptionally if action does
2998       */
2999 <    public void testRunAfterEither3() {
3000 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3001 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
3002 <        FailingNoop r = new FailingNoop();
3003 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
3004 <        f2.complete(two);
3005 <        checkCompletedWithWrappedCFException(g);
3006 <    }
2999 >    public void testRunAfterEither_actionFailed() {
3000 >        for (ExecutionMode m : ExecutionMode.values())
3001 >        for (Integer v1 : new Integer[] { 1, null })
3002 >        for (Integer v2 : new Integer[] { 2, null })
3003 >    {
3004 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3005 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
3006 >        final FailingRunnable[] rs = new FailingRunnable[6];
3007 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
3008  
3009 <    /**
3010 <     * runAfterEither result completes exceptionally if either source cancelled
3011 <     */
3012 <    public void testRunAfterEither4() {
3013 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3014 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
3015 <        Noop r = new Noop();
3016 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
3017 <        assertTrue(f.cancel(true));
3018 <        checkCompletedWithWrappedCancellationException(g);
3019 <        f = new CompletableFuture<>();
3020 <        f2 = new CompletableFuture<>();
3021 <        assertTrue(f2.cancel(true));
3022 <        checkCompletedWithWrappedCancellationException(g);
3023 <    }
3009 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
3010 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
3011 >        assertTrue(f.complete(v1));
3012 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
3013 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
3014 >        checkCompletedWithWrappedException(h0, rs[0].ex);
3015 >        checkCompletedWithWrappedException(h1, rs[1].ex);
3016 >        checkCompletedWithWrappedException(h2, rs[2].ex);
3017 >        checkCompletedWithWrappedException(h3, rs[3].ex);
3018 >        for (int i = 0; i < 4; i++) rs[i].assertInvoked();
3019 >        assertTrue(g.complete(v2));
3020 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
3021 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
3022 >        checkCompletedWithWrappedException(h4, rs[4].ex);
3023 >        checkCompletedWithWrappedException(h5, rs[5].ex);
3024 >
3025 >        checkCompletedNormally(f, v1);
3026 >        checkCompletedNormally(g, v2);
3027 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
3028 >    }}
3029  
3030      /**
3031       * thenCompose result completes normally after normal completion of source
3032       */
3033 <    public void testThenCompose() {
3034 <        CompletableFuture<Integer> f, g;
3035 <        CompletableFutureInc r;
3036 <
3037 <        f = new CompletableFuture<>();
3038 <        g = f.thenCompose(r = new CompletableFutureInc());
3039 <        f.complete(one);
3040 <        checkCompletedNormally(g, two);
3041 <        assertTrue(r.ran);
3033 >    public void testThenCompose_normalCompletion() {
3034 >        for (ExecutionMode m : ExecutionMode.values())
3035 >        for (boolean createIncomplete : new boolean[] { true, false })
3036 >        for (Integer v1 : new Integer[] { 1, null })
3037 >    {
3038 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3039 >        final CompletableFutureInc r = new CompletableFutureInc(m);
3040 >        if (!createIncomplete) assertTrue(f.complete(v1));
3041 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
3042 >        if (createIncomplete) assertTrue(f.complete(v1));
3043  
3044 <        f = new CompletableFuture<>();
3045 <        f.complete(one);
3046 <        g = f.thenCompose(r = new CompletableFutureInc());
3047 <        checkCompletedNormally(g, two);
1510 <        assertTrue(r.ran);
1511 <    }
3044 >        checkCompletedNormally(g, inc(v1));
3045 >        checkCompletedNormally(f, v1);
3046 >        r.assertValue(v1);
3047 >    }}
3048  
3049      /**
3050       * thenCompose result completes exceptionally after exceptional
3051       * completion of source
3052       */
3053 <    public void testThenCompose2() {
3054 <        CompletableFuture<Integer> f, g;
3055 <        CompletableFutureInc r;
3056 <
3057 <        f = new CompletableFuture<>();
3058 <        g = f.thenCompose(r = new CompletableFutureInc());
3059 <        f.completeExceptionally(new CFException());
3060 <        checkCompletedWithWrappedCFException(g);
3061 <
3062 <        f = new CompletableFuture<>();
3063 <        f.completeExceptionally(new CFException());
3064 <        g = f.thenCompose(r = new CompletableFutureInc());
3065 <        checkCompletedWithWrappedCFException(g);
3066 <    }
3053 >    public void testThenCompose_exceptionalCompletion() {
3054 >        for (ExecutionMode m : ExecutionMode.values())
3055 >        for (boolean createIncomplete : new boolean[] { true, false })
3056 >    {
3057 >        final CFException ex = new CFException();
3058 >        final CompletableFutureInc r = new CompletableFutureInc(m);
3059 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3060 >        if (!createIncomplete) f.completeExceptionally(ex);
3061 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
3062 >        if (createIncomplete) f.completeExceptionally(ex);
3063 >
3064 >        checkCompletedWithWrappedException(g, ex);
3065 >        checkCompletedExceptionally(f, ex);
3066 >        r.assertNotInvoked();
3067 >    }}
3068  
3069      /**
3070       * thenCompose result completes exceptionally if action does
3071       */
3072 <    public void testThenCompose3() {
3073 <        CompletableFuture<Integer> f, g;
3074 <        FailingCompletableFutureFunction r;
3075 <
3076 <        f = new CompletableFuture<>();
3077 <        g = f.thenCompose(r = new FailingCompletableFutureFunction());
3078 <        f.complete(one);
3079 <        checkCompletedWithWrappedCFException(g);
3072 >    public void testThenCompose_actionFailed() {
3073 >        for (ExecutionMode m : ExecutionMode.values())
3074 >        for (boolean createIncomplete : new boolean[] { true, false })
3075 >        for (Integer v1 : new Integer[] { 1, null })
3076 >    {
3077 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3078 >        final FailingCompletableFutureFunction r
3079 >            = new FailingCompletableFutureFunction(m);
3080 >        if (!createIncomplete) assertTrue(f.complete(v1));
3081 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
3082 >        if (createIncomplete) assertTrue(f.complete(v1));
3083  
3084 <        f = new CompletableFuture<>();
3085 <        f.complete(one);
3086 <        g = f.thenCompose(r = new FailingCompletableFutureFunction());
1547 <        checkCompletedWithWrappedCFException(g);
1548 <    }
3084 >        checkCompletedWithWrappedException(g, r.ex);
3085 >        checkCompletedNormally(f, v1);
3086 >    }}
3087  
3088      /**
3089       * thenCompose result completes exceptionally if source cancelled
3090       */
3091 <    public void testThenCompose4() {
3092 <        CompletableFuture<Integer> f, g;
3093 <        CompletableFutureInc r;
3094 <
3095 <        f = new CompletableFuture<>();
3096 <        g = f.thenCompose(r = new CompletableFutureInc());
3097 <        assertTrue(f.cancel(true));
3098 <        checkCompletedWithWrappedCancellationException(g);
3091 >    public void testThenCompose_sourceCancelled() {
3092 >        for (ExecutionMode m : ExecutionMode.values())
3093 >        for (boolean createIncomplete : new boolean[] { true, false })
3094 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3095 >    {
3096 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3097 >        final CompletableFutureInc r = new CompletableFutureInc(m);
3098 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
3099 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
3100 >        if (createIncomplete) {
3101 >            checkIncomplete(g);
3102 >            assertTrue(f.cancel(mayInterruptIfRunning));
3103 >        }
3104  
1562        f = new CompletableFuture<>();
1563        assertTrue(f.cancel(true));
1564        g = f.thenCompose(r = new CompletableFutureInc());
3105          checkCompletedWithWrappedCancellationException(g);
3106 <    }
3107 <
1568 <    // asyncs
3106 >        checkCancelled(f);
3107 >    }}
3108  
3109      /**
3110 <     * thenRunAsync result completes normally after normal completion of source
3110 >     * thenCompose result completes exceptionally if the result of the action does
3111       */
3112 <    public void testThenRunAsync() {
3113 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3114 <        Noop r = new Noop();
3115 <        CompletableFuture<Void> g = f.thenRunAsync(r);
3116 <        f.complete(null);
3117 <        checkCompletedNormally(g, null);
3112 >    public void testThenCompose_actionReturnsFailingFuture() {
3113 >        for (ExecutionMode m : ExecutionMode.values())
3114 >        for (int order = 0; order < 6; order++)
3115 >        for (Integer v1 : new Integer[] { 1, null })
3116 >    {
3117 >        final CFException ex = new CFException();
3118 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3119 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
3120 >        final CompletableFuture<Integer> h;
3121 >        // Test all permutations of orders
3122 >        switch (order) {
3123 >        case 0:
3124 >            assertTrue(f.complete(v1));
3125 >            assertTrue(g.completeExceptionally(ex));
3126 >            h = m.thenCompose(f, (x -> g));
3127 >            break;
3128 >        case 1:
3129 >            assertTrue(f.complete(v1));
3130 >            h = m.thenCompose(f, (x -> g));
3131 >            assertTrue(g.completeExceptionally(ex));
3132 >            break;
3133 >        case 2:
3134 >            assertTrue(g.completeExceptionally(ex));
3135 >            assertTrue(f.complete(v1));
3136 >            h = m.thenCompose(f, (x -> g));
3137 >            break;
3138 >        case 3:
3139 >            assertTrue(g.completeExceptionally(ex));
3140 >            h = m.thenCompose(f, (x -> g));
3141 >            assertTrue(f.complete(v1));
3142 >            break;
3143 >        case 4:
3144 >            h = m.thenCompose(f, (x -> g));
3145 >            assertTrue(f.complete(v1));
3146 >            assertTrue(g.completeExceptionally(ex));
3147 >            break;
3148 >        case 5:
3149 >            h = m.thenCompose(f, (x -> g));
3150 >            assertTrue(f.complete(v1));
3151 >            assertTrue(g.completeExceptionally(ex));
3152 >            break;
3153 >        default: throw new AssertionError();
3154 >        }
3155  
3156 <        // reordered version
3157 <        f = new CompletableFuture<>();
3158 <        f.complete(null);
3159 <        r = new Noop();
1584 <        g = f.thenRunAsync(r);
1585 <        checkCompletedNormally(g, null);
1586 <    }
3156 >        checkCompletedExceptionally(g, ex);
3157 >        checkCompletedWithWrappedException(h, ex);
3158 >        checkCompletedNormally(f, v1);
3159 >    }}
3160  
3161      /**
3162 <     * thenRunAsync result completes exceptionally after exceptional
3162 >     * exceptionallyCompose result completes normally after normal
3163       * completion of source
3164       */
3165 <    public void testThenRunAsync2() {
3166 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3167 <        Noop r = new Noop();
3168 <        CompletableFuture<Void> g = f.thenRunAsync(r);
3169 <        f.completeExceptionally(new CFException());
3170 <        try {
3171 <            g.join();
3172 <            shouldThrow();
3173 <        } catch (CompletionException success) {}
3174 <        checkCompletedWithWrappedCFException(g);
3175 <    }
3165 >    public void testExceptionallyCompose_normalCompletion() {
3166 >        for (ExecutionMode m : ExecutionMode.values())
3167 >        for (boolean createIncomplete : new boolean[] { true, false })
3168 >        for (Integer v1 : new Integer[] { 1, null })
3169 >    {
3170 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3171 >        final ExceptionalCompletableFutureFunction r =
3172 >            new ExceptionalCompletableFutureFunction(m);
3173 >        if (!createIncomplete) assertTrue(f.complete(v1));
3174 >        final CompletableFuture<Integer> g = m.exceptionallyCompose(f, r);
3175 >        if (createIncomplete) assertTrue(f.complete(v1));
3176 >
3177 >        checkCompletedNormally(f, v1);
3178 >        checkCompletedNormally(g, v1);
3179 >        r.assertNotInvoked();
3180 >    }}
3181  
3182      /**
3183 <     * thenRunAsync result completes exceptionally if action does
3183 >     * exceptionallyCompose result completes normally after exceptional
3184 >     * completion of source
3185       */
3186 <    public void testThenRunAsync3() {
3187 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3188 <        FailingNoop r = new FailingNoop();
3189 <        CompletableFuture<Void> g = f.thenRunAsync(r);
3190 <        f.complete(null);
3191 <        checkCompletedWithWrappedCFException(g);
3192 <    }
3186 >    public void testExceptionallyCompose_exceptionalCompletion() {
3187 >        for (ExecutionMode m : ExecutionMode.values())
3188 >        for (boolean createIncomplete : new boolean[] { true, false })
3189 >    {
3190 >        final CFException ex = new CFException();
3191 >        final ExceptionalCompletableFutureFunction r =
3192 >            new ExceptionalCompletableFutureFunction(m);
3193 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3194 >        if (!createIncomplete) f.completeExceptionally(ex);
3195 >        final CompletableFuture<Integer> g = m.exceptionallyCompose(f, r);
3196 >        if (createIncomplete) f.completeExceptionally(ex);
3197 >
3198 >        checkCompletedExceptionally(f, ex);
3199 >        checkCompletedNormally(g, r.value);
3200 >        r.assertInvoked();
3201 >    }}
3202  
3203      /**
3204 <     * thenRunAsync result completes exceptionally if source cancelled
3204 >     * exceptionallyCompose completes exceptionally on exception if action does
3205       */
3206 <    public void testThenRunAsync4() {
3207 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3208 <        Noop r = new Noop();
3209 <        CompletableFuture<Void> g = f.thenRunAsync(r);
3210 <        assertTrue(f.cancel(true));
3211 <        checkCompletedWithWrappedCancellationException(g);
3212 <    }
3206 >    public void testExceptionallyCompose_actionFailed() {
3207 >        for (ExecutionMode m : ExecutionMode.values())
3208 >        for (boolean createIncomplete : new boolean[] { true, false })
3209 >        for (Integer v1 : new Integer[] { 1, null })
3210 >    {
3211 >        final CFException ex = new CFException();
3212 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3213 >        final FailingExceptionalCompletableFutureFunction r
3214 >            = new FailingExceptionalCompletableFutureFunction(m);
3215 >        if (!createIncomplete) f.completeExceptionally(ex);
3216 >        final CompletableFuture<Integer> g = m.exceptionallyCompose(f, r);
3217 >        if (createIncomplete) f.completeExceptionally(ex);
3218 >
3219 >        checkCompletedExceptionally(f, ex);
3220 >        checkCompletedWithWrappedException(g, r.ex);
3221 >        r.assertInvoked();
3222 >    }}
3223 >
3224 >
3225 >    // other static methods
3226  
3227      /**
3228 <     * thenApplyAsync result completes normally after normal completion of source
3228 >     * allOf(no component futures) returns a future completed normally
3229 >     * with the value null
3230       */
3231 <    public void testThenApplyAsync() {
3232 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3233 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1632 <        f.complete(one);
1633 <        checkCompletedNormally(g, two);
3231 >    public void testAllOf_empty() throws Exception {
3232 >        CompletableFuture<Void> f = CompletableFuture.allOf();
3233 >        checkCompletedNormally(f, null);
3234      }
3235  
3236      /**
3237 <     * thenApplyAsync result completes exceptionally after exceptional
3238 <     * completion of source
3237 >     * allOf returns a future completed normally with the value null
3238 >     * when all components complete normally
3239       */
3240 <    public void testThenApplyAsync2() {
3241 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3242 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
3243 <        f.completeExceptionally(new CFException());
3244 <        checkCompletedWithWrappedCFException(g);
3240 >    public void testAllOf_normal() throws Exception {
3241 >        for (int k = 1; k < 10; k++) {
3242 >            CompletableFuture<Integer>[] fs
3243 >                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3244 >            for (int i = 0; i < k; i++)
3245 >                fs[i] = new CompletableFuture<>();
3246 >            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3247 >            for (int i = 0; i < k; i++) {
3248 >                checkIncomplete(f);
3249 >                checkIncomplete(CompletableFuture.allOf(fs));
3250 >                fs[i].complete(one);
3251 >            }
3252 >            checkCompletedNormally(f, null);
3253 >            checkCompletedNormally(CompletableFuture.allOf(fs), null);
3254 >        }
3255      }
3256  
3257 <    /**
3258 <     * thenApplyAsync result completes exceptionally if action does
3259 <     */
3260 <    public void testThenApplyAsync3() {
3261 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3262 <        FailingFunction r = new FailingFunction();
3263 <        CompletableFuture<Integer> g = f.thenApplyAsync(r);
3264 <        f.complete(null);
3265 <        checkCompletedWithWrappedCFException(g);
3257 >    public void testAllOf_normal_backwards() throws Exception {
3258 >        for (int k = 1; k < 10; k++) {
3259 >            CompletableFuture<Integer>[] fs
3260 >                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3261 >            for (int i = 0; i < k; i++)
3262 >                fs[i] = new CompletableFuture<>();
3263 >            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3264 >            for (int i = k - 1; i >= 0; i--) {
3265 >                checkIncomplete(f);
3266 >                checkIncomplete(CompletableFuture.allOf(fs));
3267 >                fs[i].complete(one);
3268 >            }
3269 >            checkCompletedNormally(f, null);
3270 >            checkCompletedNormally(CompletableFuture.allOf(fs), null);
3271 >        }
3272      }
3273  
3274 <    /**
3275 <     * thenApplyAsync result completes exceptionally if source cancelled
3276 <     */
3277 <    public void testThenApplyAsync4() {
3278 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3279 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
3280 <        assertTrue(f.cancel(true));
3281 <        checkCompletedWithWrappedCancellationException(g);
3274 >    public void testAllOf_exceptional() throws Exception {
3275 >        for (int k = 1; k < 10; k++) {
3276 >            CompletableFuture<Integer>[] fs
3277 >                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3278 >            CFException ex = new CFException();
3279 >            for (int i = 0; i < k; i++)
3280 >                fs[i] = new CompletableFuture<>();
3281 >            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3282 >            for (int i = 0; i < k; i++) {
3283 >                checkIncomplete(f);
3284 >                checkIncomplete(CompletableFuture.allOf(fs));
3285 >                if (i != k / 2) {
3286 >                    fs[i].complete(i);
3287 >                    checkCompletedNormally(fs[i], i);
3288 >                } else {
3289 >                    fs[i].completeExceptionally(ex);
3290 >                    checkCompletedExceptionally(fs[i], ex);
3291 >                }
3292 >            }
3293 >            checkCompletedWithWrappedException(f, ex);
3294 >            checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex);
3295 >        }
3296      }
3297  
3298      /**
3299 <     * thenAcceptAsync result completes normally after normal
1670 <     * completion of source
3299 >     * anyOf(no component futures) returns an incomplete future
3300       */
3301 <    public void testThenAcceptAsync() {
3302 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3303 <        IncAction r = new IncAction();
3304 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
3305 <        f.complete(one);
3306 <        checkCompletedNormally(g, null);
3307 <        assertEquals(r.value, 2);
3308 <    }
3301 >    public void testAnyOf_empty() throws Exception {
3302 >        for (Integer v1 : new Integer[] { 1, null })
3303 >    {
3304 >        CompletableFuture<Object> f = CompletableFuture.anyOf();
3305 >        checkIncomplete(f);
3306 >
3307 >        f.complete(v1);
3308 >        checkCompletedNormally(f, v1);
3309 >    }}
3310  
3311      /**
3312 <     * thenAcceptAsync result completes exceptionally after exceptional
3313 <     * completion of source
3312 >     * anyOf returns a future completed normally with a value when
3313 >     * a component future does
3314       */
3315 <    public void testThenAcceptAsync2() {
3316 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3317 <        IncAction r = new IncAction();
3318 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
3319 <        f.completeExceptionally(new CFException());
3320 <        checkCompletedWithWrappedCFException(g);
3315 >    public void testAnyOf_normal() throws Exception {
3316 >        for (int k = 0; k < 10; k++) {
3317 >            CompletableFuture[] fs = new CompletableFuture[k];
3318 >            for (int i = 0; i < k; i++)
3319 >                fs[i] = new CompletableFuture<>();
3320 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3321 >            checkIncomplete(f);
3322 >            for (int i = 0; i < k; i++) {
3323 >                fs[i].complete(i);
3324 >                checkCompletedNormally(f, 0);
3325 >                int x = (int) CompletableFuture.anyOf(fs).join();
3326 >                assertTrue(0 <= x && x <= i);
3327 >            }
3328 >        }
3329 >    }
3330 >    public void testAnyOf_normal_backwards() throws Exception {
3331 >        for (int k = 0; k < 10; k++) {
3332 >            CompletableFuture[] fs = new CompletableFuture[k];
3333 >            for (int i = 0; i < k; i++)
3334 >                fs[i] = new CompletableFuture<>();
3335 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3336 >            checkIncomplete(f);
3337 >            for (int i = k - 1; i >= 0; i--) {
3338 >                fs[i].complete(i);
3339 >                checkCompletedNormally(f, k - 1);
3340 >                int x = (int) CompletableFuture.anyOf(fs).join();
3341 >                assertTrue(i <= x && x <= k - 1);
3342 >            }
3343 >        }
3344      }
3345  
3346      /**
3347 <     * thenAcceptAsync result completes exceptionally if action does
3347 >     * anyOf result completes exceptionally when any component does.
3348       */
3349 <    public void testThenAcceptAsync3() {
3350 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3351 <        FailingConsumer r = new FailingConsumer();
3352 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
3353 <        f.complete(null);
3354 <        checkCompletedWithWrappedCFException(g);
3349 >    public void testAnyOf_exceptional() throws Exception {
3350 >        for (int k = 0; k < 10; k++) {
3351 >            CompletableFuture[] fs = new CompletableFuture[k];
3352 >            CFException[] exs = new CFException[k];
3353 >            for (int i = 0; i < k; i++) {
3354 >                fs[i] = new CompletableFuture<>();
3355 >                exs[i] = new CFException();
3356 >            }
3357 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3358 >            checkIncomplete(f);
3359 >            for (int i = 0; i < k; i++) {
3360 >                fs[i].completeExceptionally(exs[i]);
3361 >                checkCompletedWithWrappedException(f, exs[0]);
3362 >                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3363 >            }
3364 >        }
3365      }
3366  
3367 <    /**
3368 <     * thenAcceptAsync result completes exceptionally if source cancelled
3369 <     */
3370 <    public void testThenAcceptAsync4() {
3371 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3372 <        IncAction r = new IncAction();
3373 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
3374 <        assertTrue(f.cancel(true));
3375 <        checkCompletedWithWrappedCancellationException(g);
3367 >    public void testAnyOf_exceptional_backwards() throws Exception {
3368 >        for (int k = 0; k < 10; k++) {
3369 >            CompletableFuture[] fs = new CompletableFuture[k];
3370 >            CFException[] exs = new CFException[k];
3371 >            for (int i = 0; i < k; i++) {
3372 >                fs[i] = new CompletableFuture<>();
3373 >                exs[i] = new CFException();
3374 >            }
3375 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3376 >            checkIncomplete(f);
3377 >            for (int i = k - 1; i >= 0; i--) {
3378 >                fs[i].completeExceptionally(exs[i]);
3379 >                checkCompletedWithWrappedException(f, exs[k - 1]);
3380 >                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3381 >            }
3382 >        }
3383      }
3384  
3385      /**
3386 <     * thenCombineAsync result completes normally after normal
1717 <     * completion of sources
3386 >     * Completion methods throw NullPointerException with null arguments
3387       */
3388 <    public void testThenCombineAsync() {
3389 <        CompletableFuture<Integer> f, g, h;
3388 >    @SuppressWarnings("FutureReturnValueIgnored")
3389 >    public void testNPE() {
3390 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3391 >        CompletableFuture<Integer> g = new CompletableFuture<>();
3392 >        CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null;
3393 >        ThreadExecutor exec = new ThreadExecutor();
3394  
3395 <        f = new CompletableFuture<>();
3396 <        g = new CompletableFuture<>();
3397 <        h = f.thenCombineAsync(g, subtract);
3398 <        f.complete(3);
1726 <        checkIncomplete(h);
1727 <        g.complete(1);
1728 <        checkCompletedNormally(h, 2);
3395 >        Runnable[] throwingActions = {
3396 >            () -> CompletableFuture.supplyAsync(null),
3397 >            () -> CompletableFuture.supplyAsync(null, exec),
3398 >            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null),
3399  
3400 <        f = new CompletableFuture<>();
3401 <        g = new CompletableFuture<>();
3402 <        h = f.thenCombineAsync(g, subtract);
1733 <        g.complete(1);
1734 <        checkIncomplete(h);
1735 <        f.complete(3);
1736 <        checkCompletedNormally(h, 2);
3400 >            () -> CompletableFuture.runAsync(null),
3401 >            () -> CompletableFuture.runAsync(null, exec),
3402 >            () -> CompletableFuture.runAsync(() -> {}, null),
3403  
3404 <        f = new CompletableFuture<>();
1739 <        g = new CompletableFuture<>();
1740 <        g.complete(1);
1741 <        f.complete(3);
1742 <        h = f.thenCombineAsync(g, subtract);
1743 <        checkCompletedNormally(h, 2);
1744 <    }
3404 >            () -> f.completeExceptionally(null),
3405  
3406 <    /**
3407 <     * thenCombineAsync result completes exceptionally after exceptional
3408 <     * completion of either source
3409 <     */
1750 <    public void testThenCombineAsync2() {
1751 <        CompletableFuture<Integer> f, g, h;
3406 >            () -> f.thenApply(null),
3407 >            () -> f.thenApplyAsync(null),
3408 >            () -> f.thenApplyAsync(x -> x, null),
3409 >            () -> f.thenApplyAsync(null, exec),
3410  
3411 <        f = new CompletableFuture<>();
3412 <        g = new CompletableFuture<>();
3413 <        h = f.thenCombineAsync(g, subtract);
3414 <        f.completeExceptionally(new CFException());
1757 <        checkIncomplete(h);
1758 <        g.complete(1);
1759 <        checkCompletedWithWrappedCFException(h);
3411 >            () -> f.thenAccept(null),
3412 >            () -> f.thenAcceptAsync(null),
3413 >            () -> f.thenAcceptAsync(x -> {} , null),
3414 >            () -> f.thenAcceptAsync(null, exec),
3415  
3416 <        f = new CompletableFuture<>();
3417 <        g = new CompletableFuture<>();
3418 <        h = f.thenCombineAsync(g, subtract);
3419 <        g.completeExceptionally(new CFException());
1765 <        checkIncomplete(h);
1766 <        f.complete(3);
1767 <        checkCompletedWithWrappedCFException(h);
3416 >            () -> f.thenRun(null),
3417 >            () -> f.thenRunAsync(null),
3418 >            () -> f.thenRunAsync(() -> {} , null),
3419 >            () -> f.thenRunAsync(null, exec),
3420  
3421 <        f = new CompletableFuture<>();
3422 <        g = new CompletableFuture<>();
3423 <        g.completeExceptionally(new CFException());
3424 <        f.complete(3);
3425 <        h = f.thenCombineAsync(g, subtract);
3426 <        checkCompletedWithWrappedCFException(h);
3427 <    }
3421 >            () -> f.thenCombine(g, null),
3422 >            () -> f.thenCombineAsync(g, null),
3423 >            () -> f.thenCombineAsync(g, null, exec),
3424 >            () -> f.thenCombine(nullFuture, (x, y) -> x),
3425 >            () -> f.thenCombineAsync(nullFuture, (x, y) -> x),
3426 >            () -> f.thenCombineAsync(nullFuture, (x, y) -> x, exec),
3427 >            () -> f.thenCombineAsync(g, (x, y) -> x, null),
3428  
3429 <    /**
3430 <     * thenCombineAsync result completes exceptionally if action does
3431 <     */
3432 <    public void testThenCombineAsync3() {
3433 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3434 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
3435 <        FailingBiFunction r = new FailingBiFunction();
1784 <        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r);
1785 <        f.complete(one);
1786 <        checkIncomplete(g);
1787 <        assertFalse(r.ran);
1788 <        f2.complete(two);
1789 <        checkCompletedWithWrappedCFException(g);
1790 <        assertTrue(r.ran);
1791 <    }
3429 >            () -> f.thenAcceptBoth(g, null),
3430 >            () -> f.thenAcceptBothAsync(g, null),
3431 >            () -> f.thenAcceptBothAsync(g, null, exec),
3432 >            () -> f.thenAcceptBoth(nullFuture, (x, y) -> {}),
3433 >            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}),
3434 >            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec),
3435 >            () -> f.thenAcceptBothAsync(g, (x, y) -> {}, null),
3436  
3437 <    /**
3438 <     * thenCombineAsync result completes exceptionally if either source cancelled
3439 <     */
3440 <    public void testThenCombineAsync4() {
3441 <        CompletableFuture<Integer> f, g, h;
3437 >            () -> f.runAfterBoth(g, null),
3438 >            () -> f.runAfterBothAsync(g, null),
3439 >            () -> f.runAfterBothAsync(g, null, exec),
3440 >            () -> f.runAfterBoth(nullFuture, () -> {}),
3441 >            () -> f.runAfterBothAsync(nullFuture, () -> {}),
3442 >            () -> f.runAfterBothAsync(nullFuture, () -> {}, exec),
3443 >            () -> f.runAfterBothAsync(g, () -> {}, null),
3444  
3445 <        f = new CompletableFuture<>();
3446 <        g = new CompletableFuture<>();
3447 <        h = f.thenCombineAsync(g, subtract);
3448 <        assertTrue(f.cancel(true));
3449 <        checkIncomplete(h);
3450 <        g.complete(1);
3451 <        checkCompletedWithWrappedCancellationException(h);
3445 >            () -> f.applyToEither(g, null),
3446 >            () -> f.applyToEitherAsync(g, null),
3447 >            () -> f.applyToEitherAsync(g, null, exec),
3448 >            () -> f.applyToEither(nullFuture, x -> x),
3449 >            () -> f.applyToEitherAsync(nullFuture, x -> x),
3450 >            () -> f.applyToEitherAsync(nullFuture, x -> x, exec),
3451 >            () -> f.applyToEitherAsync(g, x -> x, null),
3452  
3453 <        f = new CompletableFuture<>();
3454 <        g = new CompletableFuture<>();
3455 <        h = f.thenCombineAsync(g, subtract);
3456 <        assertTrue(g.cancel(true));
3457 <        checkIncomplete(h);
3458 <        f.complete(3);
3459 <        checkCompletedWithWrappedCancellationException(h);
3453 >            () -> f.acceptEither(g, null),
3454 >            () -> f.acceptEitherAsync(g, null),
3455 >            () -> f.acceptEitherAsync(g, null, exec),
3456 >            () -> f.acceptEither(nullFuture, x -> {}),
3457 >            () -> f.acceptEitherAsync(nullFuture, x -> {}),
3458 >            () -> f.acceptEitherAsync(nullFuture, x -> {}, exec),
3459 >            () -> f.acceptEitherAsync(g, x -> {}, null),
3460  
3461 <        f = new CompletableFuture<>();
3462 <        g = new CompletableFuture<>();
3463 <        g.complete(3);
3464 <        assertTrue(f.cancel(true));
3465 <        h = f.thenCombineAsync(g, subtract);
3466 <        checkCompletedWithWrappedCancellationException(h);
3461 >            () -> f.runAfterEither(g, null),
3462 >            () -> f.runAfterEitherAsync(g, null),
3463 >            () -> f.runAfterEitherAsync(g, null, exec),
3464 >            () -> f.runAfterEither(nullFuture, () -> {}),
3465 >            () -> f.runAfterEitherAsync(nullFuture, () -> {}),
3466 >            () -> f.runAfterEitherAsync(nullFuture, () -> {}, exec),
3467 >            () -> f.runAfterEitherAsync(g, () -> {}, null),
3468  
3469 <        f = new CompletableFuture<>();
3470 <        g = new CompletableFuture<>();
3471 <        f.complete(3);
3472 <        assertTrue(g.cancel(true));
1826 <        h = f.thenCombineAsync(g, subtract);
1827 <        checkCompletedWithWrappedCancellationException(h);
1828 <    }
3469 >            () -> f.thenCompose(null),
3470 >            () -> f.thenComposeAsync(null),
3471 >            () -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null),
3472 >            () -> f.thenComposeAsync(null, exec),
3473  
3474 <    /**
1831 <     * thenAcceptBothAsync result completes normally after normal
1832 <     * completion of sources
1833 <     */
1834 <    public void testThenAcceptBothAsync() {
1835 <        CompletableFuture<Integer> f, g;
1836 <        CompletableFuture<Void> h;
1837 <        SubtractAction r;
3474 >            () -> f.exceptionally(null),
3475  
3476 <        f = new CompletableFuture<>();
1840 <        g = new CompletableFuture<>();
1841 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1842 <        f.complete(3);
1843 <        checkIncomplete(h);
1844 <        g.complete(1);
1845 <        checkCompletedNormally(h, null);
1846 <        assertEquals(r.value, 2);
3476 >            () -> f.handle(null),
3477  
3478 <        f = new CompletableFuture<>();
3479 <        g = new CompletableFuture<>();
3480 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
3481 <        g.complete(1);
1852 <        checkIncomplete(h);
1853 <        f.complete(3);
1854 <        checkCompletedNormally(h, null);
1855 <        assertEquals(r.value, 2);
3478 >            () -> CompletableFuture.allOf((CompletableFuture<?>)null),
3479 >            () -> CompletableFuture.allOf((CompletableFuture<?>[])null),
3480 >            () -> CompletableFuture.allOf(f, null),
3481 >            () -> CompletableFuture.allOf(null, f),
3482  
3483 <        f = new CompletableFuture<>();
3484 <        g = new CompletableFuture<>();
3485 <        g.complete(1);
3486 <        f.complete(3);
1861 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1862 <        checkCompletedNormally(h, null);
1863 <        assertEquals(r.value, 2);
1864 <    }
3483 >            () -> CompletableFuture.anyOf((CompletableFuture<?>)null),
3484 >            () -> CompletableFuture.anyOf((CompletableFuture<?>[])null),
3485 >            () -> CompletableFuture.anyOf(f, null),
3486 >            () -> CompletableFuture.anyOf(null, f),
3487  
3488 <    /**
1867 <     * thenAcceptBothAsync result completes exceptionally after exceptional
1868 <     * completion of source
1869 <     */
1870 <    public void testThenAcceptBothAsync2() {
1871 <        CompletableFuture<Integer> f, g;
1872 <        CompletableFuture<Void> h;
1873 <        SubtractAction r;
3488 >            () -> f.obtrudeException(null),
3489  
3490 <        f = new CompletableFuture<>();
3491 <        g = new CompletableFuture<>();
3492 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1878 <        f.completeExceptionally(new CFException());
1879 <        checkIncomplete(h);
1880 <        g.complete(1);
1881 <        checkCompletedWithWrappedCFException(h);
3490 >            () -> CompletableFuture.delayedExecutor(1L, SECONDS, null),
3491 >            () -> CompletableFuture.delayedExecutor(1L, null, exec),
3492 >            () -> CompletableFuture.delayedExecutor(1L, null),
3493  
3494 <        f = new CompletableFuture<>();
3495 <        g = new CompletableFuture<>();
1885 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1886 <        g.completeExceptionally(new CFException());
1887 <        checkIncomplete(h);
1888 <        f.complete(3);
1889 <        checkCompletedWithWrappedCFException(h);
3494 >            () -> f.orTimeout(1L, null),
3495 >            () -> f.completeOnTimeout(42, 1L, null),
3496  
3497 <        f = new CompletableFuture<>();
3498 <        g = new CompletableFuture<>();
3499 <        f.complete(3);
1894 <        g.completeExceptionally(new CFException());
1895 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1896 <        checkCompletedWithWrappedCFException(h);
3497 >            () -> CompletableFuture.failedFuture(null),
3498 >            () -> CompletableFuture.failedStage(null),
3499 >        };
3500  
3501 <        f = new CompletableFuture<>();
3502 <        g = new CompletableFuture<>();
1900 <        f.completeExceptionally(new CFException());
1901 <        g.complete(3);
1902 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1903 <        checkCompletedWithWrappedCFException(h);
3501 >        assertThrows(NullPointerException.class, throwingActions);
3502 >        assertEquals(0, exec.count.get());
3503      }
3504  
3505      /**
3506 <     * thenAcceptBothAsync result completes exceptionally if action does
3506 >     * Test submissions to an executor that rejects all tasks.
3507       */
3508 <    public void testThenAcceptBothAsync3() {
3509 <        CompletableFuture<Integer> f, g;
3510 <        CompletableFuture<Void> h;
3511 <        FailingBiConsumer r;
3508 >    public void testRejectingExecutor() {
3509 >        for (Integer v : new Integer[] { 1, null })
3510 >    {
3511 >        final CountingRejectingExecutor e = new CountingRejectingExecutor();
3512 >
3513 >        final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v);
3514 >        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3515 >
3516 >        List<CompletableFuture<?>> futures = new ArrayList<>();
3517 >
3518 >        List<CompletableFuture<Integer>> srcs = new ArrayList<>();
3519 >        srcs.add(complete);
3520 >        srcs.add(incomplete);
3521 >
3522 >        for (CompletableFuture<Integer> src : srcs) {
3523 >            List<CompletableFuture<?>> fs = new ArrayList<>();
3524 >            fs.add(src.thenRunAsync(() -> {}, e));
3525 >            fs.add(src.thenAcceptAsync(z -> {}, e));
3526 >            fs.add(src.thenApplyAsync(z -> z, e));
3527 >
3528 >            fs.add(src.thenCombineAsync(src, (x, y) -> x, e));
3529 >            fs.add(src.thenAcceptBothAsync(src, (x, y) -> {}, e));
3530 >            fs.add(src.runAfterBothAsync(src, () -> {}, e));
3531 >
3532 >            fs.add(src.applyToEitherAsync(src, z -> z, e));
3533 >            fs.add(src.acceptEitherAsync(src, z -> {}, e));
3534 >            fs.add(src.runAfterEitherAsync(src, () -> {}, e));
3535 >
3536 >            fs.add(src.thenComposeAsync(z -> null, e));
3537 >            fs.add(src.whenCompleteAsync((z, t) -> {}, e));
3538 >            fs.add(src.handleAsync((z, t) -> null, e));
3539 >
3540 >            for (CompletableFuture<?> future : fs) {
3541 >                if (src.isDone())
3542 >                    checkCompletedWithWrappedException(future, e.ex);
3543 >                else
3544 >                    checkIncomplete(future);
3545 >            }
3546 >            futures.addAll(fs);
3547 >        }
3548  
3549 <        f = new CompletableFuture<>();
3550 <        g = new CompletableFuture<>();
1916 <        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer());
1917 <        f.complete(3);
1918 <        checkIncomplete(h);
1919 <        g.complete(1);
1920 <        checkCompletedWithWrappedCFException(h);
3549 >        {
3550 >            List<CompletableFuture<?>> fs = new ArrayList<>();
3551  
3552 <        f = new CompletableFuture<>();
3553 <        g = new CompletableFuture<>();
1924 <        f.complete(3);
1925 <        g.complete(1);
1926 <        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer());
1927 <        checkCompletedWithWrappedCFException(h);
1928 <    }
3552 >            fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e));
3553 >            fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e));
3554  
3555 <    /**
3556 <     * thenAcceptBothAsync result completes exceptionally if either source cancelled
1932 <     */
1933 <    public void testThenAcceptBothAsync4() {
1934 <        CompletableFuture<Integer> f, g;
1935 <        CompletableFuture<Void> h;
1936 <        SubtractAction r;
3555 >            fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e));
3556 >            fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e));
3557  
3558 <        f = new CompletableFuture<>();
3559 <        g = new CompletableFuture<>();
1940 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1941 <        assertTrue(f.cancel(true));
1942 <        checkIncomplete(h);
1943 <        g.complete(1);
1944 <        checkCompletedWithWrappedCancellationException(h);
3558 >            fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e));
3559 >            fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e));
3560  
3561 <        f = new CompletableFuture<>();
3562 <        g = new CompletableFuture<>();
3563 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
3564 <        assertTrue(g.cancel(true));
1950 <        checkIncomplete(h);
1951 <        f.complete(3);
1952 <        checkCompletedWithWrappedCancellationException(h);
3561 >            for (CompletableFuture<?> future : fs)
3562 >                checkIncomplete(future);
3563 >            futures.addAll(fs);
3564 >        }
3565  
3566 <        f = new CompletableFuture<>();
3567 <        g = new CompletableFuture<>();
1956 <        f.complete(3);
1957 <        assertTrue(g.cancel(true));
1958 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1959 <        checkCompletedWithWrappedCancellationException(h);
3566 >        {
3567 >            List<CompletableFuture<?>> fs = new ArrayList<>();
3568  
3569 <        f = new CompletableFuture<>();
3570 <        g = new CompletableFuture<>();
1963 <        assertTrue(f.cancel(true));
1964 <        g.complete(3);
1965 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1966 <        checkCompletedWithWrappedCancellationException(h);
1967 <    }
3569 >            fs.add(complete.applyToEitherAsync(incomplete, z -> z, e));
3570 >            fs.add(incomplete.applyToEitherAsync(complete, z -> z, e));
3571  
3572 <    /**
3573 <     * applyToEitherAsync result completes normally after normal
1971 <     * completion of sources
1972 <     */
1973 <    public void testApplyToEitherAsync() {
1974 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1975 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1976 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1977 <        f.complete(one);
1978 <        checkCompletedNormally(g, two);
3572 >            fs.add(complete.acceptEitherAsync(incomplete, z -> {}, e));
3573 >            fs.add(incomplete.acceptEitherAsync(complete, z -> {}, e));
3574  
3575 <        f = new CompletableFuture<>();
3576 <        f.complete(one);
1982 <        f2 = new CompletableFuture<>();
1983 <        g = f.applyToEitherAsync(f2, inc);
1984 <        checkCompletedNormally(g, two);
1985 <    }
3575 >            fs.add(complete.runAfterEitherAsync(incomplete, () -> {}, e));
3576 >            fs.add(incomplete.runAfterEitherAsync(complete, () -> {}, e));
3577  
3578 <    /**
3579 <     * applyToEitherAsync result completes exceptionally after exceptional
3580 <     * completion of source
3581 <     */
1991 <    public void testApplyToEitherAsync2() {
1992 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1993 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1994 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1995 <        f.completeExceptionally(new CFException());
1996 <        checkCompletedWithWrappedCFException(g);
3578 >            for (CompletableFuture<?> future : fs)
3579 >                checkCompletedWithWrappedException(future, e.ex);
3580 >            futures.addAll(fs);
3581 >        }
3582  
3583 <        f = new CompletableFuture<>();
1999 <        f2 = new CompletableFuture<>();
2000 <        f2.completeExceptionally(new CFException());
2001 <        g = f.applyToEitherAsync(f2, inc);
2002 <        f.complete(one);
2003 <        checkCompletedWithWrappedCFException(g);
2004 <    }
3583 >        incomplete.complete(v);
3584  
3585 <    /**
3586 <     * applyToEitherAsync result completes exceptionally if action does
3587 <     */
3588 <    public void testApplyToEitherAsync3() {
3589 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2011 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2012 <        FailingFunction r = new FailingFunction();
2013 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r);
2014 <        f.complete(one);
2015 <        checkCompletedWithWrappedCFException(g);
2016 <    }
3585 >        for (CompletableFuture<?> future : futures)
3586 >            checkCompletedWithWrappedException(future, e.ex);
3587 >
3588 >        assertEquals(futures.size(), e.count.get());
3589 >    }}
3590  
3591      /**
3592 <     * applyToEitherAsync result completes exceptionally if either source cancelled
3592 >     * Test submissions to an executor that rejects all tasks, but
3593 >     * should never be invoked because the dependent future is
3594 >     * explicitly completed.
3595       */
3596 <    public void testApplyToEitherAsync4() {
3597 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3598 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
3599 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2025 <        assertTrue(f.cancel(true));
2026 <        checkCompletedWithWrappedCancellationException(g);
3596 >    public void testRejectingExecutorNeverInvoked() {
3597 >        for (Integer v : new Integer[] { 1, null })
3598 >    {
3599 >        final CountingRejectingExecutor e = new CountingRejectingExecutor();
3600  
3601 <        f = new CompletableFuture<>();
3602 <        f2 = new CompletableFuture<>();
2030 <        assertTrue(f2.cancel(true));
2031 <        g = f.applyToEitherAsync(f2, inc);
2032 <        checkCompletedWithWrappedCancellationException(g);
2033 <    }
3601 >        final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v);
3602 >        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3603  
3604 <    /**
2036 <     * acceptEitherAsync result completes normally after normal
2037 <     * completion of sources
2038 <     */
2039 <    public void testAcceptEitherAsync() {
2040 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2041 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2042 <        IncAction r = new IncAction();
2043 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2044 <        f.complete(one);
2045 <        checkCompletedNormally(g, null);
2046 <        assertEquals(r.value, 2);
3604 >        List<CompletableFuture<?>> futures = new ArrayList<>();
3605  
3606 <        r = new IncAction();
3607 <        f = new CompletableFuture<>();
3608 <        f.complete(one);
2051 <        f2 = new CompletableFuture<>();
2052 <        g = f.acceptEitherAsync(f2, r);
2053 <        checkCompletedNormally(g, null);
2054 <        assertEquals(r.value, 2);
2055 <    }
3606 >        List<CompletableFuture<Integer>> srcs = new ArrayList<>();
3607 >        srcs.add(complete);
3608 >        srcs.add(incomplete);
3609  
3610 <    /**
3611 <     * acceptEitherAsync result completes exceptionally after exceptional
3612 <     * completion of source
3613 <     */
2061 <    public void testAcceptEitherAsync2() {
2062 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2063 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2064 <        IncAction r = new IncAction();
2065 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2066 <        f.completeExceptionally(new CFException());
2067 <        checkCompletedWithWrappedCFException(g);
3610 >        List<CompletableFuture<?>> fs = new ArrayList<>();
3611 >        fs.add(incomplete.thenRunAsync(() -> {}, e));
3612 >        fs.add(incomplete.thenAcceptAsync(z -> {}, e));
3613 >        fs.add(incomplete.thenApplyAsync(z -> z, e));
3614  
3615 <        r = new IncAction();
3616 <        f = new CompletableFuture<>();
3617 <        f2 = new CompletableFuture<>();
2072 <        f2.completeExceptionally(new CFException());
2073 <        g = f.acceptEitherAsync(f2, r);
2074 <        f.complete(one);
2075 <        checkCompletedWithWrappedCFException(g);
2076 <    }
3615 >        fs.add(incomplete.thenCombineAsync(incomplete, (x, y) -> x, e));
3616 >        fs.add(incomplete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e));
3617 >        fs.add(incomplete.runAfterBothAsync(incomplete, () -> {}, e));
3618  
3619 <    /**
3620 <     * acceptEitherAsync result completes exceptionally if action does
3621 <     */
2081 <    public void testAcceptEitherAsync3() {
2082 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2083 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2084 <        FailingConsumer r = new FailingConsumer();
2085 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2086 <        f.complete(one);
2087 <        checkCompletedWithWrappedCFException(g);
2088 <    }
3619 >        fs.add(incomplete.applyToEitherAsync(incomplete, z -> z, e));
3620 >        fs.add(incomplete.acceptEitherAsync(incomplete, z -> {}, e));
3621 >        fs.add(incomplete.runAfterEitherAsync(incomplete, () -> {}, e));
3622  
3623 <    /**
3624 <     * acceptEitherAsync result completes exceptionally if either
3625 <     * source cancelled
2093 <     */
2094 <    public void testAcceptEitherAsync4() {
2095 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2096 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2097 <        IncAction r = new IncAction();
2098 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2099 <        assertTrue(f.cancel(true));
2100 <        checkCompletedWithWrappedCancellationException(g);
3623 >        fs.add(incomplete.thenComposeAsync(z -> null, e));
3624 >        fs.add(incomplete.whenCompleteAsync((z, t) -> {}, e));
3625 >        fs.add(incomplete.handleAsync((z, t) -> null, e));
3626  
3627 <        r = new IncAction();
3628 <        f = new CompletableFuture<>();
2104 <        f2 = new CompletableFuture<>();
2105 <        assertTrue(f2.cancel(true));
2106 <        g = f.acceptEitherAsync(f2, r);
2107 <        checkCompletedWithWrappedCancellationException(g);
2108 <    }
3627 >        fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e));
3628 >        fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e));
3629  
3630 <    /**
3631 <     * runAfterEitherAsync result completes normally after normal
2112 <     * completion of sources
2113 <     */
2114 <    public void testRunAfterEitherAsync() {
2115 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2116 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2117 <        Noop r = new Noop();
2118 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2119 <        f.complete(one);
2120 <        checkCompletedNormally(g, null);
2121 <        assertTrue(r.ran);
3630 >        fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e));
3631 >        fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e));
3632  
3633 <        r = new Noop();
3634 <        f = new CompletableFuture<>();
2125 <        f.complete(one);
2126 <        f2 = new CompletableFuture<>();
2127 <        g = f.runAfterEitherAsync(f2, r);
2128 <        checkCompletedNormally(g, null);
2129 <        assertTrue(r.ran);
2130 <    }
3633 >        fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e));
3634 >        fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e));
3635  
3636 <    /**
3637 <     * runAfterEitherAsync result completes exceptionally after exceptional
2134 <     * completion of source
2135 <     */
2136 <    public void testRunAfterEitherAsync2() {
2137 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2138 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2139 <        Noop r = new Noop();
2140 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2141 <        f.completeExceptionally(new CFException());
2142 <        checkCompletedWithWrappedCFException(g);
3636 >        for (CompletableFuture<?> future : fs)
3637 >            checkIncomplete(future);
3638  
3639 <        r = new Noop();
3640 <        f = new CompletableFuture<>();
3641 <        f2 = new CompletableFuture<>();
3642 <        f2.completeExceptionally(new CFException());
3643 <        g = f.runAfterEitherAsync(f2, r);
3644 <        f.complete(one);
3645 <        checkCompletedWithWrappedCFException(g);
3646 <    }
3639 >        for (CompletableFuture<?> future : fs)
3640 >            future.complete(null);
3641 >
3642 >        incomplete.complete(v);
3643 >
3644 >        for (CompletableFuture<?> future : fs)
3645 >            checkCompletedNormally(future, null);
3646 >
3647 >        assertEquals(0, e.count.get());
3648 >    }}
3649  
3650      /**
3651 <     * runAfterEitherAsync result completes exceptionally if action does
3651 >     * toCompletableFuture returns this CompletableFuture.
3652       */
3653 <    public void testRunAfterEitherAsync3() {
3653 >    public void testToCompletableFuture() {
3654          CompletableFuture<Integer> f = new CompletableFuture<>();
3655 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2159 <        FailingNoop r = new FailingNoop();
2160 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2161 <        f.complete(one);
2162 <        checkCompletedWithWrappedCFException(g);
3655 >        assertSame(f, f.toCompletableFuture());
3656      }
3657  
3658 +    // jdk9
3659 +
3660      /**
3661 <     * runAfterEitherAsync result completes exceptionally if either
2167 <     * source cancelled
3661 >     * newIncompleteFuture returns an incomplete CompletableFuture
3662       */
3663 <    public void testRunAfterEitherAsync4() {
3663 >    public void testNewIncompleteFuture() {
3664 >        for (Integer v1 : new Integer[] { 1, null })
3665 >    {
3666          CompletableFuture<Integer> f = new CompletableFuture<>();
3667 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
3668 <        Noop r = new Noop();
3669 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
3670 <        assertTrue(f.cancel(true));
3671 <        checkCompletedWithWrappedCancellationException(g);
3672 <
3673 <        r = new Noop();
3674 <        f = new CompletableFuture<>();
3675 <        f2 = new CompletableFuture<>();
3676 <        assertTrue(f2.cancel(true));
2181 <        g = f.runAfterEitherAsync(f2, r);
2182 <        checkCompletedWithWrappedCancellationException(g);
2183 <    }
3667 >        CompletableFuture<Integer> g = f.newIncompleteFuture();
3668 >        checkIncomplete(f);
3669 >        checkIncomplete(g);
3670 >        f.complete(v1);
3671 >        checkCompletedNormally(f, v1);
3672 >        checkIncomplete(g);
3673 >        g.complete(v1);
3674 >        checkCompletedNormally(g, v1);
3675 >        assertSame(g.getClass(), CompletableFuture.class);
3676 >    }}
3677  
3678      /**
3679 <     * thenComposeAsync result completes normally after normal
2187 <     * completion of source
3679 >     * completedStage returns a completed CompletionStage
3680       */
3681 <    public void testThenComposeAsync() {
3682 <        CompletableFuture<Integer> f, g;
3683 <        CompletableFutureInc r;
3684 <
3685 <        f = new CompletableFuture<>();
3686 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
3687 <        f.complete(one);
2196 <        checkCompletedNormally(g, two);
2197 <
2198 <        f = new CompletableFuture<>();
2199 <        f.complete(one);
2200 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2201 <        checkCompletedNormally(g, two);
3681 >    public void testCompletedStage() {
3682 >        AtomicInteger x = new AtomicInteger(0);
3683 >        AtomicReference<Throwable> r = new AtomicReference<>();
3684 >        CompletionStage<Integer> f = CompletableFuture.completedStage(1);
3685 >        f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3686 >        assertEquals(x.get(), 1);
3687 >        assertNull(r.get());
3688      }
3689  
3690      /**
3691 <     * thenComposeAsync result completes exceptionally after
3692 <     * exceptional completion of source
3691 >     * defaultExecutor by default returns the commonPool if
3692 >     * it supports more than one thread.
3693       */
3694 <    public void testThenComposeAsync2() {
3695 <        CompletableFuture<Integer> f, g;
3696 <        CompletableFutureInc r;
3697 <
3698 <        f = new CompletableFuture<>();
3699 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
3700 <        f.completeExceptionally(new CFException());
3701 <        checkCompletedWithWrappedCFException(g);
2216 <        assertFalse(r.ran);
2217 <
2218 <        f = new CompletableFuture<>();
2219 <        f.completeExceptionally(new CFException());
2220 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2221 <        checkCompletedWithWrappedCFException(g);
2222 <        assertFalse(r.ran);
3694 >    public void testDefaultExecutor() {
3695 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3696 >        Executor e = f.defaultExecutor();
3697 >        Executor c = ForkJoinPool.commonPool();
3698 >        if (ForkJoinPool.getCommonPoolParallelism() > 1)
3699 >            assertSame(e, c);
3700 >        else
3701 >            assertNotSame(e, c);
3702      }
3703  
3704      /**
3705 <     * thenComposeAsync result completes exceptionally if action does
3705 >     * failedFuture returns a CompletableFuture completed
3706 >     * exceptionally with the given Exception
3707       */
3708 <    public void testThenComposeAsync3() {
3709 <        CompletableFuture<Integer> f, g;
3710 <        FailingCompletableFutureFunction r;
3711 <
2232 <        f = new CompletableFuture<>();
2233 <        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
2234 <        f.complete(one);
2235 <        checkCompletedWithWrappedCFException(g);
2236 <
2237 <        f = new CompletableFuture<>();
2238 <        f.complete(one);
2239 <        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
2240 <        checkCompletedWithWrappedCFException(g);
3708 >    public void testFailedFuture() {
3709 >        CFException ex = new CFException();
3710 >        CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex);
3711 >        checkCompletedExceptionally(f, ex);
3712      }
3713  
3714      /**
3715 <     * thenComposeAsync result completes exceptionally if source cancelled
3715 >     * failedFuture(null) throws NPE
3716       */
3717 <    public void testThenComposeAsync4() {
3718 <        CompletableFuture<Integer> f, g;
3719 <        CompletableFutureInc r;
3720 <
3721 <        f = new CompletableFuture<>();
2251 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2252 <        assertTrue(f.cancel(true));
2253 <        checkCompletedWithWrappedCancellationException(g);
2254 <
2255 <        f = new CompletableFuture<>();
2256 <        assertTrue(f.cancel(true));
2257 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2258 <        checkCompletedWithWrappedCancellationException(g);
3717 >    public void testFailedFuture_null() {
3718 >        try {
3719 >            CompletableFuture<Integer> f = CompletableFuture.failedFuture(null);
3720 >            shouldThrow();
3721 >        } catch (NullPointerException success) {}
3722      }
3723  
2261    // async with explicit executors
2262
3724      /**
3725 <     * thenRunAsync result completes normally after normal completion of source
3725 >     * copy returns a CompletableFuture that is completed normally,
3726 >     * with the same value, when source is.
3727       */
3728 <    public void testThenRunAsyncE() {
3728 >    public void testCopy_normalCompletion() {
3729 >        for (boolean createIncomplete : new boolean[] { true, false })
3730 >        for (Integer v1 : new Integer[] { 1, null })
3731 >    {
3732          CompletableFuture<Integer> f = new CompletableFuture<>();
3733 <        Noop r = new Noop();
3734 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3735 <        f.complete(null);
3736 <        checkCompletedNormally(g, null);
3737 <
3738 <        // reordered version
3739 <        f = new CompletableFuture<>();
3740 <        f.complete(null);
3741 <        r = new Noop();
3742 <        g = f.thenRunAsync(r, new ThreadExecutor());
2278 <        checkCompletedNormally(g, null);
2279 <    }
3733 >        if (!createIncomplete) assertTrue(f.complete(v1));
3734 >        CompletableFuture<Integer> g = f.copy();
3735 >        if (createIncomplete) {
3736 >            checkIncomplete(f);
3737 >            checkIncomplete(g);
3738 >            assertTrue(f.complete(v1));
3739 >        }
3740 >        checkCompletedNormally(f, v1);
3741 >        checkCompletedNormally(g, v1);
3742 >    }}
3743  
3744      /**
3745 <     * thenRunAsync result completes exceptionally after exceptional
3746 <     * completion of source
3745 >     * copy returns a CompletableFuture that is completed exceptionally
3746 >     * when source is.
3747       */
3748 <    public void testThenRunAsync2E() {
3748 >    public void testCopy_exceptionalCompletion() {
3749 >        for (boolean createIncomplete : new boolean[] { true, false })
3750 >    {
3751 >        CFException ex = new CFException();
3752          CompletableFuture<Integer> f = new CompletableFuture<>();
3753 <        Noop r = new Noop();
3754 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3755 <        f.completeExceptionally(new CFException());
3756 <        try {
3757 <            g.join();
3758 <            shouldThrow();
3759 <        } catch (CompletionException success) {}
3760 <        checkCompletedWithWrappedCFException(g);
3761 <    }
3753 >        if (!createIncomplete) f.completeExceptionally(ex);
3754 >        CompletableFuture<Integer> g = f.copy();
3755 >        if (createIncomplete) {
3756 >            checkIncomplete(f);
3757 >            checkIncomplete(g);
3758 >            f.completeExceptionally(ex);
3759 >        }
3760 >        checkCompletedExceptionally(f, ex);
3761 >        checkCompletedWithWrappedException(g, ex);
3762 >    }}
3763  
3764      /**
3765 <     * thenRunAsync result completes exceptionally if action does
3765 >     * Completion of a copy does not complete its source.
3766       */
3767 <    public void testThenRunAsync3E() {
3767 >    public void testCopy_oneWayPropagation() {
3768          CompletableFuture<Integer> f = new CompletableFuture<>();
3769 <        FailingNoop r = new FailingNoop();
3770 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3771 <        f.complete(null);
3772 <        checkCompletedWithWrappedCFException(g);
3769 >        assertTrue(f.copy().complete(1));
3770 >        assertTrue(f.copy().complete(null));
3771 >        assertTrue(f.copy().cancel(true));
3772 >        assertTrue(f.copy().cancel(false));
3773 >        assertTrue(f.copy().completeExceptionally(new CFException()));
3774 >        checkIncomplete(f);
3775      }
3776  
3777      /**
3778 <     * thenRunAsync result completes exceptionally if source cancelled
3778 >     * minimalCompletionStage returns a CompletableFuture that is
3779 >     * completed normally, with the same value, when source is.
3780       */
3781 <    public void testThenRunAsync4E() {
3781 >    public void testMinimalCompletionStage() {
3782          CompletableFuture<Integer> f = new CompletableFuture<>();
3783 <        Noop r = new Noop();
3784 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3785 <        assertTrue(f.cancel(true));
3786 <        checkCompletedWithWrappedCancellationException(g);
3783 >        CompletionStage<Integer> g = f.minimalCompletionStage();
3784 >        AtomicInteger x = new AtomicInteger(0);
3785 >        AtomicReference<Throwable> r = new AtomicReference<>();
3786 >        checkIncomplete(f);
3787 >        g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3788 >        f.complete(1);
3789 >        checkCompletedNormally(f, 1);
3790 >        assertEquals(x.get(), 1);
3791 >        assertNull(r.get());
3792      }
3793  
3794      /**
3795 <     * thenApplyAsync result completes normally after normal completion of source
3795 >     * minimalCompletionStage returns a CompletableFuture that is
3796 >     * completed exceptionally when source is.
3797       */
3798 <    public void testThenApplyAsyncE() {
3798 >    public void testMinimalCompletionStage2() {
3799          CompletableFuture<Integer> f = new CompletableFuture<>();
3800 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
3801 <        f.complete(one);
3802 <        checkCompletedNormally(g, two);
3800 >        CompletionStage<Integer> g = f.minimalCompletionStage();
3801 >        AtomicInteger x = new AtomicInteger(0);
3802 >        AtomicReference<Throwable> r = new AtomicReference<>();
3803 >        g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3804 >        checkIncomplete(f);
3805 >        CFException ex = new CFException();
3806 >        f.completeExceptionally(ex);
3807 >        checkCompletedExceptionally(f, ex);
3808 >        assertEquals(x.get(), 0);
3809 >        assertEquals(r.get().getCause(), ex);
3810      }
3811  
3812      /**
3813 <     * thenApplyAsync result completes exceptionally after exceptional
3814 <     * completion of source
3813 >     * failedStage returns a CompletionStage completed
3814 >     * exceptionally with the given Exception
3815       */
3816 <    public void testThenApplyAsync2E() {
3817 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3818 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
3819 <        f.completeExceptionally(new CFException());
3820 <        checkCompletedWithWrappedCFException(g);
3816 >    public void testFailedStage() {
3817 >        CFException ex = new CFException();
3818 >        CompletionStage<Integer> f = CompletableFuture.failedStage(ex);
3819 >        AtomicInteger x = new AtomicInteger(0);
3820 >        AtomicReference<Throwable> r = new AtomicReference<>();
3821 >        f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3822 >        assertEquals(x.get(), 0);
3823 >        assertEquals(r.get(), ex);
3824      }
3825  
3826      /**
3827 <     * thenApplyAsync result completes exceptionally if action does
3827 >     * completeAsync completes with value of given supplier
3828       */
3829 <    public void testThenApplyAsync3E() {
3829 >    public void testCompleteAsync() {
3830 >        for (Integer v1 : new Integer[] { 1, null })
3831 >    {
3832          CompletableFuture<Integer> f = new CompletableFuture<>();
3833 <        FailingFunction r = new FailingFunction();
3834 <        CompletableFuture<Integer> g = f.thenApplyAsync(r, new ThreadExecutor());
3835 <        f.complete(null);
3836 <        checkCompletedWithWrappedCFException(g);
2349 <    }
3833 >        f.completeAsync(() -> v1);
3834 >        f.join();
3835 >        checkCompletedNormally(f, v1);
3836 >    }}
3837  
3838      /**
3839 <     * thenApplyAsync result completes exceptionally if source cancelled
3839 >     * completeAsync completes exceptionally if given supplier throws
3840       */
3841 <    public void testThenApplyAsync4E() {
3841 >    public void testCompleteAsync2() {
3842          CompletableFuture<Integer> f = new CompletableFuture<>();
3843 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
3844 <        assertTrue(f.cancel(true));
3845 <        checkCompletedWithWrappedCancellationException(g);
3843 >        CFException ex = new CFException();
3844 >        f.completeAsync(() -> { throw ex; });
3845 >        try {
3846 >            f.join();
3847 >            shouldThrow();
3848 >        } catch (CompletionException success) {}
3849 >        checkCompletedWithWrappedException(f, ex);
3850      }
3851  
3852      /**
3853 <     * thenAcceptAsync result completes normally after normal
2363 <     * completion of source
3853 >     * completeAsync with given executor completes with value of given supplier
3854       */
3855 <    public void testThenAcceptAsyncE() {
3855 >    public void testCompleteAsync3() {
3856 >        for (Integer v1 : new Integer[] { 1, null })
3857 >    {
3858          CompletableFuture<Integer> f = new CompletableFuture<>();
3859 <        IncAction r = new IncAction();
3860 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3861 <        f.complete(one);
3862 <        checkCompletedNormally(g, null);
3863 <        assertEquals(r.value, 2);
3864 <    }
3859 >        ThreadExecutor executor = new ThreadExecutor();
3860 >        f.completeAsync(() -> v1, executor);
3861 >        assertSame(v1, f.join());
3862 >        checkCompletedNormally(f, v1);
3863 >        assertEquals(1, executor.count.get());
3864 >    }}
3865  
3866      /**
3867 <     * thenAcceptAsync result completes exceptionally after exceptional
3868 <     * completion of source
3867 >     * completeAsync with given executor completes exceptionally if
3868 >     * given supplier throws
3869       */
3870 <    public void testThenAcceptAsync2E() {
3870 >    public void testCompleteAsync4() {
3871          CompletableFuture<Integer> f = new CompletableFuture<>();
3872 <        IncAction r = new IncAction();
3873 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3874 <        f.completeExceptionally(new CFException());
3875 <        checkCompletedWithWrappedCFException(g);
3872 >        CFException ex = new CFException();
3873 >        ThreadExecutor executor = new ThreadExecutor();
3874 >        f.completeAsync(() -> { throw ex; }, executor);
3875 >        try {
3876 >            f.join();
3877 >            shouldThrow();
3878 >        } catch (CompletionException success) {}
3879 >        checkCompletedWithWrappedException(f, ex);
3880 >        assertEquals(1, executor.count.get());
3881      }
3882  
3883      /**
3884 <     * thenAcceptAsync result completes exceptionally if action does
3884 >     * orTimeout completes with TimeoutException if not complete
3885       */
3886 <    public void testThenAcceptAsync3E() {
3886 >    public void testOrTimeout_timesOut() {
3887 >        long timeoutMillis = timeoutMillis();
3888          CompletableFuture<Integer> f = new CompletableFuture<>();
3889 <        FailingConsumer r = new FailingConsumer();
3890 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3891 <        f.complete(null);
3892 <        checkCompletedWithWrappedCFException(g);
3889 >        long startTime = System.nanoTime();
3890 >        assertSame(f, f.orTimeout(timeoutMillis, MILLISECONDS));
3891 >        checkCompletedWithTimeoutException(f);
3892 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
3893      }
3894  
3895      /**
3896 <     * thenAcceptAsync result completes exceptionally if source cancelled
3896 >     * orTimeout completes normally if completed before timeout
3897       */
3898 <    public void testThenAcceptAsync4E() {
3898 >    public void testOrTimeout_completed() {
3899 >        for (Integer v1 : new Integer[] { 1, null })
3900 >    {
3901          CompletableFuture<Integer> f = new CompletableFuture<>();
3902 <        IncAction r = new IncAction();
3903 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3904 <        assertTrue(f.cancel(true));
3905 <        checkCompletedWithWrappedCancellationException(g);
3906 <    }
3902 >        CompletableFuture<Integer> g = new CompletableFuture<>();
3903 >        long startTime = System.nanoTime();
3904 >        f.complete(v1);
3905 >        assertSame(f, f.orTimeout(LONG_DELAY_MS, MILLISECONDS));
3906 >        assertSame(g, g.orTimeout(LONG_DELAY_MS, MILLISECONDS));
3907 >        g.complete(v1);
3908 >        checkCompletedNormally(f, v1);
3909 >        checkCompletedNormally(g, v1);
3910 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
3911 >    }}
3912  
3913      /**
3914 <     * thenCombineAsync result completes normally after normal
2410 <     * completion of sources
3914 >     * completeOnTimeout completes with given value if not complete
3915       */
3916 <    public void testThenCombineAsyncE() {
3917 <        CompletableFuture<Integer> f, g, h;
3918 <        ThreadExecutor e = new ThreadExecutor();
2415 <        int count = 0;
2416 <
2417 <        f = new CompletableFuture<>();
2418 <        g = new CompletableFuture<>();
2419 <        h = f.thenCombineAsync(g, subtract, e);
2420 <        f.complete(3);
2421 <        checkIncomplete(h);
2422 <        g.complete(1);
2423 <        checkCompletedNormally(h, 2);
2424 <        assertEquals(++count, e.count.get());
2425 <
2426 <        f = new CompletableFuture<>();
2427 <        g = new CompletableFuture<>();
2428 <        h = f.thenCombineAsync(g, subtract, e);
2429 <        g.complete(1);
2430 <        checkIncomplete(h);
2431 <        f.complete(3);
2432 <        checkCompletedNormally(h, 2);
2433 <        assertEquals(++count, e.count.get());
2434 <
2435 <        f = new CompletableFuture<>();
2436 <        g = new CompletableFuture<>();
2437 <        g.complete(1);
2438 <        f.complete(3);
2439 <        h = f.thenCombineAsync(g, subtract, e);
2440 <        checkCompletedNormally(h, 2);
2441 <        assertEquals(++count, e.count.get());
3916 >    public void testCompleteOnTimeout_timesOut() {
3917 >        testInParallel(() -> testCompleteOnTimeout_timesOut(42),
3918 >                       () -> testCompleteOnTimeout_timesOut(null));
3919      }
3920  
3921      /**
3922 <     * thenCombineAsync result completes exceptionally after exceptional
2446 <     * completion of either source
3922 >     * completeOnTimeout completes with given value if not complete
3923       */
3924 <    public void testThenCombineAsync2E() {
3925 <        CompletableFuture<Integer> f, g, h;
3926 <        ThreadExecutor e = new ThreadExecutor();
3927 <        int count = 0;
3928 <
3929 <        f = new CompletableFuture<>();
3930 <        g = new CompletableFuture<>();
3931 <        h = f.thenCombineAsync(g, subtract, e);
3932 <        f.completeExceptionally(new CFException());
2457 <        checkIncomplete(h);
2458 <        g.complete(1);
2459 <        checkCompletedWithWrappedCFException(h);
2460 <
2461 <        f = new CompletableFuture<>();
2462 <        g = new CompletableFuture<>();
2463 <        h = f.thenCombineAsync(g, subtract, e);
2464 <        g.completeExceptionally(new CFException());
2465 <        checkIncomplete(h);
2466 <        f.complete(3);
2467 <        checkCompletedWithWrappedCFException(h);
2468 <
2469 <        f = new CompletableFuture<>();
2470 <        g = new CompletableFuture<>();
2471 <        g.completeExceptionally(new CFException());
2472 <        h = f.thenCombineAsync(g, subtract, e);
2473 <        checkIncomplete(h);
2474 <        f.complete(3);
2475 <        checkCompletedWithWrappedCFException(h);
2476 <
2477 <        assertEquals(0, e.count.get());
3924 >    public void testCompleteOnTimeout_timesOut(Integer v) {
3925 >        long timeoutMillis = timeoutMillis();
3926 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3927 >        long startTime = System.nanoTime();
3928 >        assertSame(f, f.completeOnTimeout(v, timeoutMillis, MILLISECONDS));
3929 >        assertSame(v, f.join());
3930 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
3931 >        f.complete(99);         // should have no effect
3932 >        checkCompletedNormally(f, v);
3933      }
3934  
3935      /**
3936 <     * thenCombineAsync result completes exceptionally if action does
3936 >     * completeOnTimeout has no effect if completed within timeout
3937       */
3938 <    public void testThenCombineAsync3E() {
3938 >    public void testCompleteOnTimeout_completed() {
3939 >        for (Integer v1 : new Integer[] { 1, null })
3940 >    {
3941          CompletableFuture<Integer> f = new CompletableFuture<>();
3942 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
3943 <        FailingBiFunction r = new FailingBiFunction();
3944 <        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r, new ThreadExecutor());
3945 <        f.complete(one);
3946 <        checkIncomplete(g);
3947 <        assertFalse(r.ran);
3948 <        f2.complete(two);
3949 <        checkCompletedWithWrappedCFException(g);
3950 <        assertTrue(r.ran);
3951 <    }
3942 >        CompletableFuture<Integer> g = new CompletableFuture<>();
3943 >        long startTime = System.nanoTime();
3944 >        f.complete(v1);
3945 >        assertSame(f, f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS));
3946 >        assertSame(g, g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS));
3947 >        g.complete(v1);
3948 >        checkCompletedNormally(f, v1);
3949 >        checkCompletedNormally(g, v1);
3950 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
3951 >    }}
3952  
3953      /**
3954 <     * thenCombineAsync result completes exceptionally if either source cancelled
3954 >     * delayedExecutor returns an executor that delays submission
3955       */
3956 <    public void testThenCombineAsync4E() {
3957 <        CompletableFuture<Integer> f, g, h;
3958 <        ThreadExecutor e = new ThreadExecutor();
3956 >    public void testDelayedExecutor() {
3957 >        testInParallel(() -> testDelayedExecutor(null, null),
3958 >                       () -> testDelayedExecutor(null, 1),
3959 >                       () -> testDelayedExecutor(new ThreadExecutor(), 1),
3960 >                       () -> testDelayedExecutor(new ThreadExecutor(), 1));
3961 >    }
3962 >
3963 >    public void testDelayedExecutor(Executor executor, Integer v) throws Exception {
3964 >        long timeoutMillis = timeoutMillis();
3965 >        // Use an "unreasonably long" long timeout to catch lingering threads
3966 >        long longTimeoutMillis = 1000 * 60 * 60 * 24;
3967 >        final Executor delayer, longDelayer;
3968 >        if (executor == null) {
3969 >            delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS);
3970 >            longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS);
3971 >        } else {
3972 >            delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS, executor);
3973 >            longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS, executor);
3974 >        }
3975 >        long startTime = System.nanoTime();
3976 >        CompletableFuture<Integer> f =
3977 >            CompletableFuture.supplyAsync(() -> v, delayer);
3978 >        CompletableFuture<Integer> g =
3979 >            CompletableFuture.supplyAsync(() -> v, longDelayer);
3980  
3981 <        f = new CompletableFuture<>();
2504 <        g = new CompletableFuture<>();
2505 <        h = f.thenCombineAsync(g, subtract, e);
2506 <        assertTrue(f.cancel(true));
2507 <        checkIncomplete(h);
2508 <        g.complete(1);
2509 <        checkCompletedWithWrappedCancellationException(h);
3981 >        assertNull(g.getNow(null));
3982  
3983 <        f = new CompletableFuture<>();
3984 <        g = new CompletableFuture<>();
3985 <        h = f.thenCombineAsync(g, subtract, e);
3986 <        assertTrue(g.cancel(true));
2515 <        checkIncomplete(h);
2516 <        f.complete(3);
2517 <        checkCompletedWithWrappedCancellationException(h);
3983 >        assertSame(v, f.get(LONG_DELAY_MS, MILLISECONDS));
3984 >        long millisElapsed = millisElapsedSince(startTime);
3985 >        assertTrue(millisElapsed >= timeoutMillis);
3986 >        assertTrue(millisElapsed < LONG_DELAY_MS / 2);
3987  
3988 <        f = new CompletableFuture<>();
2520 <        g = new CompletableFuture<>();
2521 <        assertTrue(g.cancel(true));
2522 <        h = f.thenCombineAsync(g, subtract, e);
2523 <        checkIncomplete(h);
2524 <        f.complete(3);
2525 <        checkCompletedWithWrappedCancellationException(h);
3988 >        checkCompletedNormally(f, v);
3989  
3990 <        f = new CompletableFuture<>();
2528 <        g = new CompletableFuture<>();
2529 <        assertTrue(f.cancel(true));
3990 >        checkIncomplete(g);
3991          assertTrue(g.cancel(true));
2531        h = f.thenCombineAsync(g, subtract, e);
2532        checkCompletedWithWrappedCancellationException(h);
2533
2534        assertEquals(0, e.count.get());
2535    }
2536
2537    /**
2538     * thenAcceptBothAsync result completes normally after normal
2539     * completion of sources
2540     */
2541    public void testThenAcceptBothAsyncE() {
2542        CompletableFuture<Integer> f, g;
2543        CompletableFuture<Void> h;
2544        SubtractAction r;
2545        ThreadExecutor e = new ThreadExecutor();
2546
2547        f = new CompletableFuture<>();
2548        g = new CompletableFuture<>();
2549        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2550        f.complete(3);
2551        checkIncomplete(h);
2552        g.complete(1);
2553        checkCompletedNormally(h, null);
2554        assertEquals(r.value, 2);
2555
2556        f = new CompletableFuture<>();
2557        g = new CompletableFuture<>();
2558        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2559        g.complete(1);
2560        checkIncomplete(h);
2561        f.complete(3);
2562        checkCompletedNormally(h, null);
2563        assertEquals(r.value, 2);
2564
2565        f = new CompletableFuture<>();
2566        g = new CompletableFuture<>();
2567        g.complete(1);
2568        f.complete(3);
2569        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2570        checkCompletedNormally(h, null);
2571        assertEquals(r.value, 2);
2572
2573        assertEquals(3, e.count.get());
2574    }
2575
2576    /**
2577     * thenAcceptBothAsync result completes exceptionally after exceptional
2578     * completion of source
2579     */
2580    public void testThenAcceptBothAsync2E() {
2581        CompletableFuture<Integer> f, g;
2582        CompletableFuture<Void> h;
2583        SubtractAction r;
2584        ThreadExecutor e = new ThreadExecutor();
2585
2586        f = new CompletableFuture<>();
2587        g = new CompletableFuture<>();
2588        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2589        f.completeExceptionally(new CFException());
2590        checkIncomplete(h);
2591        g.complete(1);
2592        checkCompletedWithWrappedCFException(h);
2593
2594        f = new CompletableFuture<>();
2595        g = new CompletableFuture<>();
2596        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2597        g.completeExceptionally(new CFException());
2598        checkIncomplete(h);
2599        f.complete(3);
2600        checkCompletedWithWrappedCFException(h);
2601
2602        f = new CompletableFuture<>();
2603        g = new CompletableFuture<>();
2604        f.complete(3);
2605        g.completeExceptionally(new CFException());
2606        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2607        checkCompletedWithWrappedCFException(h);
2608
2609        f = new CompletableFuture<>();
2610        g = new CompletableFuture<>();
2611        f.completeExceptionally(new CFException());
2612        g.complete(3);
2613        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2614        checkCompletedWithWrappedCFException(h);
2615
2616        assertEquals(0, e.count.get());
3992      }
3993  
3994 <    /**
2620 <     * thenAcceptBothAsync result completes exceptionally if action does
2621 <     */
2622 <    public void testThenAcceptBothAsync3E() {
2623 <        CompletableFuture<Integer> f, g;
2624 <        CompletableFuture<Void> h;
2625 <        FailingBiConsumer r;
2626 <        ThreadExecutor e = new ThreadExecutor();
3994 >    //--- tests of implementation details; not part of official tck ---
3995  
3996 <        f = new CompletableFuture<>();
3997 <        g = new CompletableFuture<>();
3998 <        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer(), e);
3999 <        f.complete(3);
4000 <        checkIncomplete(h);
4001 <        g.complete(1);
4002 <        checkCompletedWithWrappedCFException(h);
4003 <
4004 <        f = new CompletableFuture<>();
2637 <        g = new CompletableFuture<>();
2638 <        f.complete(3);
2639 <        g.complete(1);
2640 <        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer(), e);
2641 <        checkCompletedWithWrappedCFException(h);
3996 >    Object resultOf(CompletableFuture<?> f) {
3997 >        SecurityManager sm = System.getSecurityManager();
3998 >        if (sm != null) {
3999 >            try {
4000 >                System.setSecurityManager(null);
4001 >            } catch (SecurityException giveUp) {
4002 >                return "Reflection not available";
4003 >            }
4004 >        }
4005  
4006 <        assertEquals(2, e.count.get());
4006 >        try {
4007 >            java.lang.reflect.Field resultField
4008 >                = CompletableFuture.class.getDeclaredField("result");
4009 >            resultField.setAccessible(true);
4010 >            return resultField.get(f);
4011 >        } catch (Throwable t) {
4012 >            throw new AssertionError(t);
4013 >        } finally {
4014 >            if (sm != null) System.setSecurityManager(sm);
4015 >        }
4016      }
4017  
4018 <    /**
4019 <     * thenAcceptBothAsync result completes exceptionally if either source cancelled
4020 <     */
4021 <    public void testThenAcceptBothAsync4E() {
4022 <        CompletableFuture<Integer> f, g;
4023 <        CompletableFuture<Void> h;
4024 <        SubtractAction r;
2653 <        ThreadExecutor e = new ThreadExecutor();
2654 <
2655 <        f = new CompletableFuture<>();
2656 <        g = new CompletableFuture<>();
2657 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2658 <        assertTrue(f.cancel(true));
2659 <        checkIncomplete(h);
2660 <        g.complete(1);
2661 <        checkCompletedWithWrappedCancellationException(h);
2662 <
2663 <        f = new CompletableFuture<>();
2664 <        g = new CompletableFuture<>();
2665 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2666 <        assertTrue(g.cancel(true));
2667 <        checkIncomplete(h);
2668 <        f.complete(3);
2669 <        checkCompletedWithWrappedCancellationException(h);
2670 <
2671 <        f = new CompletableFuture<>();
2672 <        g = new CompletableFuture<>();
2673 <        f.complete(3);
2674 <        assertTrue(g.cancel(true));
2675 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2676 <        checkCompletedWithWrappedCancellationException(h);
2677 <
2678 <        f = new CompletableFuture<>();
2679 <        g = new CompletableFuture<>();
2680 <        assertTrue(f.cancel(true));
2681 <        g.complete(3);
2682 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2683 <        checkCompletedWithWrappedCancellationException(h);
4018 >    public void testExceptionPropagationReusesResultObject() {
4019 >        if (!testImplementationDetails) return;
4020 >        for (ExecutionMode m : ExecutionMode.values())
4021 >    {
4022 >        final CFException ex = new CFException();
4023 >        final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42);
4024 >        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
4025  
4026 <        assertEquals(0, e.count.get());
4027 <    }
4026 >        final Runnable noopRunnable = new Noop(m);
4027 >        final Consumer<Integer> noopConsumer = new NoopConsumer(m);
4028 >        final Function<Integer, Integer> incFunction = new IncFunction(m);
4029 >
4030 >        List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs
4031 >            = new ArrayList<>();
4032 >
4033 >        funs.add(y -> m.thenRun(y, noopRunnable));
4034 >        funs.add(y -> m.thenAccept(y, noopConsumer));
4035 >        funs.add(y -> m.thenApply(y, incFunction));
4036 >
4037 >        funs.add(y -> m.runAfterEither(y, incomplete, noopRunnable));
4038 >        funs.add(y -> m.acceptEither(y, incomplete, noopConsumer));
4039 >        funs.add(y -> m.applyToEither(y, incomplete, incFunction));
4040 >
4041 >        funs.add(y -> m.runAfterBoth(y, v42, noopRunnable));
4042 >        funs.add(y -> m.runAfterBoth(v42, y, noopRunnable));
4043 >        funs.add(y -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
4044 >        funs.add(y -> m.thenAcceptBoth(v42, y, new SubtractAction(m)));
4045 >        funs.add(y -> m.thenCombine(y, v42, new SubtractFunction(m)));
4046 >        funs.add(y -> m.thenCombine(v42, y, new SubtractFunction(m)));
4047 >
4048 >        funs.add(y -> m.whenComplete(y, (Integer r, Throwable t) -> {}));
4049 >
4050 >        funs.add(y -> m.thenCompose(y, new CompletableFutureInc(m)));
4051 >
4052 >        funs.add(y -> CompletableFuture.allOf(y));
4053 >        funs.add(y -> CompletableFuture.allOf(y, v42));
4054 >        funs.add(y -> CompletableFuture.allOf(v42, y));
4055 >        funs.add(y -> CompletableFuture.anyOf(y));
4056 >        funs.add(y -> CompletableFuture.anyOf(y, incomplete));
4057 >        funs.add(y -> CompletableFuture.anyOf(incomplete, y));
4058  
4059 <    /**
4060 <     * applyToEitherAsync result completes normally after normal
4061 <     * completion of sources
4062 <     */
4063 <    public void testApplyToEitherAsyncE() {
4064 <        CompletableFuture<Integer> f = new CompletableFuture<>();
4065 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
4066 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
4067 <        f.complete(one);
4068 <        checkCompletedNormally(g, two);
2698 <
2699 <        f = new CompletableFuture<>();
2700 <        f.complete(one);
2701 <        f2 = new CompletableFuture<>();
2702 <        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2703 <        checkCompletedNormally(g, two);
2704 <    }
4059 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
4060 >                 fun : funs) {
4061 >            CompletableFuture<Integer> f = new CompletableFuture<>();
4062 >            f.completeExceptionally(ex);
4063 >            CompletableFuture<Integer> src = m.thenApply(f, incFunction);
4064 >            checkCompletedWithWrappedException(src, ex);
4065 >            CompletableFuture<?> dep = fun.apply(src);
4066 >            checkCompletedWithWrappedException(dep, ex);
4067 >            assertSame(resultOf(src), resultOf(dep));
4068 >        }
4069  
4070 <    /**
4071 <     * applyToEitherAsync result completes exceptionally after exceptional
4072 <     * completion of source
4073 <     */
4074 <    public void testApplyToEitherAsync2E() {
4075 <        CompletableFuture<Integer> f = new CompletableFuture<>();
4076 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
4077 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
4078 <        f.completeExceptionally(new CFException());
4079 <        checkCompletedWithWrappedCFException(g);
4070 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
4071 >                 fun : funs) {
4072 >            CompletableFuture<Integer> f = new CompletableFuture<>();
4073 >            CompletableFuture<Integer> src = m.thenApply(f, incFunction);
4074 >            CompletableFuture<?> dep = fun.apply(src);
4075 >            f.completeExceptionally(ex);
4076 >            checkCompletedWithWrappedException(src, ex);
4077 >            checkCompletedWithWrappedException(dep, ex);
4078 >            assertSame(resultOf(src), resultOf(dep));
4079 >        }
4080  
4081 <        f = new CompletableFuture<>();
4082 <        f2 = new CompletableFuture<>();
4083 <        f2.completeExceptionally(new CFException());
4084 <        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
4085 <        f.complete(one);
4086 <        checkCompletedWithWrappedCFException(g);
4087 <    }
4081 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
4082 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
4083 >                 fun : funs) {
4084 >            CompletableFuture<Integer> f = new CompletableFuture<>();
4085 >            f.cancel(mayInterruptIfRunning);
4086 >            checkCancelled(f);
4087 >            CompletableFuture<Integer> src = m.thenApply(f, incFunction);
4088 >            checkCompletedWithWrappedCancellationException(src);
4089 >            CompletableFuture<?> dep = fun.apply(src);
4090 >            checkCompletedWithWrappedCancellationException(dep);
4091 >            assertSame(resultOf(src), resultOf(dep));
4092 >        }
4093  
4094 <    /**
4095 <     * applyToEitherAsync result completes exceptionally if action does
4096 <     */
4097 <    public void testApplyToEitherAsync3E() {
4098 <        CompletableFuture<Integer> f = new CompletableFuture<>();
4099 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
4100 <        FailingFunction r = new FailingFunction();
4101 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor());
4102 <        f.complete(one);
4103 <        checkCompletedWithWrappedCFException(g);
4104 <    }
4094 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
4095 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
4096 >                 fun : funs) {
4097 >            CompletableFuture<Integer> f = new CompletableFuture<>();
4098 >            CompletableFuture<Integer> src = m.thenApply(f, incFunction);
4099 >            CompletableFuture<?> dep = fun.apply(src);
4100 >            f.cancel(mayInterruptIfRunning);
4101 >            checkCancelled(f);
4102 >            checkCompletedWithWrappedCancellationException(src);
4103 >            checkCompletedWithWrappedCancellationException(dep);
4104 >            assertSame(resultOf(src), resultOf(dep));
4105 >        }
4106 >    }}
4107  
4108      /**
4109 <     * applyToEitherAsync result completes exceptionally if either source cancelled
4109 >     * Minimal completion stages throw UOE for most non-CompletionStage methods
4110       */
4111 <    public void testApplyToEitherAsync4E() {
4112 <        CompletableFuture<Integer> f = new CompletableFuture<>();
4113 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
4114 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
4115 <        assertTrue(f.cancel(true));
4116 <        checkCompletedWithWrappedCancellationException(g);
4117 <
4118 <        f = new CompletableFuture<>();
4119 <        f2 = new CompletableFuture<>();
4120 <        assertTrue(f2.cancel(true));
4121 <        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
4122 <        checkCompletedWithWrappedCancellationException(g);
4111 >    public void testMinimalCompletionStage_minimality() {
4112 >        if (!testImplementationDetails) return;
4113 >        Function<Method, String> toSignature =
4114 >            method -> method.getName() + Arrays.toString(method.getParameterTypes());
4115 >        Predicate<Method> isNotStatic =
4116 >            method -> (method.getModifiers() & Modifier.STATIC) == 0;
4117 >        List<Method> minimalMethods =
4118 >            Stream.of(Object.class, CompletionStage.class)
4119 >            .flatMap(klazz -> Stream.of(klazz.getMethods()))
4120 >            .filter(isNotStatic)
4121 >            .collect(Collectors.toList());
4122 >        // Methods from CompletableFuture permitted NOT to throw UOE
4123 >        String[] signatureWhitelist = {
4124 >            "newIncompleteFuture[]",
4125 >            "defaultExecutor[]",
4126 >            "minimalCompletionStage[]",
4127 >            "copy[]",
4128 >        };
4129 >        Set<String> permittedMethodSignatures =
4130 >            Stream.concat(minimalMethods.stream().map(toSignature),
4131 >                          Stream.of(signatureWhitelist))
4132 >            .collect(Collectors.toSet());
4133 >        List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods())
4134 >            .filter(isNotStatic)
4135 >            .filter(method -> !permittedMethodSignatures.contains(toSignature.apply(method)))
4136 >            .collect(Collectors.toList());
4137 >
4138 >        List<CompletionStage<Integer>> stages = new ArrayList<>();
4139 >        CompletionStage<Integer> min =
4140 >            new CompletableFuture<Integer>().minimalCompletionStage();
4141 >        stages.add(min);
4142 >        stages.add(min.thenApply(x -> x));
4143 >        stages.add(CompletableFuture.completedStage(1));
4144 >        stages.add(CompletableFuture.failedStage(new CFException()));
4145 >
4146 >        List<Method> bugs = new ArrayList<>();
4147 >        for (Method method : allMethods) {
4148 >            Class<?>[] parameterTypes = method.getParameterTypes();
4149 >            Object[] args = new Object[parameterTypes.length];
4150 >            // Manufacture boxed primitives for primitive params
4151 >            for (int i = 0; i < args.length; i++) {
4152 >                Class<?> type = parameterTypes[i];
4153 >                if (parameterTypes[i] == boolean.class)
4154 >                    args[i] = false;
4155 >                else if (parameterTypes[i] == int.class)
4156 >                    args[i] = 0;
4157 >                else if (parameterTypes[i] == long.class)
4158 >                    args[i] = 0L;
4159 >            }
4160 >            for (CompletionStage<Integer> stage : stages) {
4161 >                try {
4162 >                    method.invoke(stage, args);
4163 >                    bugs.add(method);
4164 >                }
4165 >                catch (java.lang.reflect.InvocationTargetException expected) {
4166 >                    if (! (expected.getCause() instanceof UnsupportedOperationException)) {
4167 >                        bugs.add(method);
4168 >                        // expected.getCause().printStackTrace();
4169 >                    }
4170 >                }
4171 >                catch (ReflectiveOperationException bad) { throw new Error(bad); }
4172 >            }
4173 >        }
4174 >        if (!bugs.isEmpty())
4175 >            throw new Error("Methods did not throw UOE: " + bugs);
4176      }
4177  
4178      /**
4179 <     * acceptEitherAsync result completes normally after normal
4180 <     * completion of sources
4179 >     * minimalStage.toCompletableFuture() returns a CompletableFuture that
4180 >     * is completed normally, with the same value, when source is.
4181       */
4182 <    public void testAcceptEitherAsyncE() {
4182 >    public void testMinimalCompletionStage_toCompletableFuture_normalCompletion() {
4183 >        for (boolean createIncomplete : new boolean[] { true, false })
4184 >        for (Integer v1 : new Integer[] { 1, null })
4185 >    {
4186          CompletableFuture<Integer> f = new CompletableFuture<>();
4187 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
4188 <        IncAction r = new IncAction();
4189 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
4190 <        f.complete(one);
4191 <        checkCompletedNormally(g, null);
4192 <        assertEquals(r.value, 2);
4193 <
4194 <        r = new IncAction();
4195 <        f = new CompletableFuture<>();
4196 <        f.complete(one);
4197 <        f2 = new CompletableFuture<>();
2771 <        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2772 <        checkCompletedNormally(g, null);
2773 <        assertEquals(r.value, 2);
2774 <    }
4187 >        CompletionStage<Integer> minimal = f.minimalCompletionStage();
4188 >        if (!createIncomplete) assertTrue(f.complete(v1));
4189 >        CompletableFuture<Integer> g = minimal.toCompletableFuture();
4190 >        if (createIncomplete) {
4191 >            checkIncomplete(f);
4192 >            checkIncomplete(g);
4193 >            assertTrue(f.complete(v1));
4194 >        }
4195 >        checkCompletedNormally(f, v1);
4196 >        checkCompletedNormally(g, v1);
4197 >    }}
4198  
4199      /**
4200 <     * acceptEitherAsync result completes exceptionally after exceptional
4201 <     * completion of source
4200 >     * minimalStage.toCompletableFuture() returns a CompletableFuture that
4201 >     * is completed exceptionally when source is.
4202       */
4203 <    public void testAcceptEitherAsync2E() {
4203 >    public void testMinimalCompletionStage_toCompletableFuture_exceptionalCompletion() {
4204 >        for (boolean createIncomplete : new boolean[] { true, false })
4205 >    {
4206 >        CFException ex = new CFException();
4207          CompletableFuture<Integer> f = new CompletableFuture<>();
4208 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
4209 <        IncAction r = new IncAction();
4210 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
4211 <        f.completeExceptionally(new CFException());
4212 <        checkCompletedWithWrappedCFException(g);
4213 <
4214 <        r = new IncAction();
4215 <        f = new CompletableFuture<>();
4216 <        f2 = new CompletableFuture<>();
4217 <        f2.completeExceptionally(new CFException());
4218 <        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2793 <        f.complete(one);
2794 <        checkCompletedWithWrappedCFException(g);
2795 <    }
4208 >        CompletionStage<Integer> minimal = f.minimalCompletionStage();
4209 >        if (!createIncomplete) f.completeExceptionally(ex);
4210 >        CompletableFuture<Integer> g = minimal.toCompletableFuture();
4211 >        if (createIncomplete) {
4212 >            checkIncomplete(f);
4213 >            checkIncomplete(g);
4214 >            f.completeExceptionally(ex);
4215 >        }
4216 >        checkCompletedExceptionally(f, ex);
4217 >        checkCompletedWithWrappedException(g, ex);
4218 >    }}
4219  
4220      /**
4221 <     * acceptEitherAsync result completes exceptionally if action does
4221 >     * minimalStage.toCompletableFuture() gives mutable CompletableFuture
4222       */
4223 <    public void testAcceptEitherAsync3E() {
4223 >    public void testMinimalCompletionStage_toCompletableFuture_mutable() {
4224 >        for (Integer v1 : new Integer[] { 1, null })
4225 >    {
4226          CompletableFuture<Integer> f = new CompletableFuture<>();
4227 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
4228 <        FailingConsumer r = new FailingConsumer();
4229 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
4230 <        f.complete(one);
4231 <        checkCompletedWithWrappedCFException(g);
4232 <    }
4227 >        CompletionStage minimal = f.minimalCompletionStage();
4228 >        CompletableFuture<Integer> g = minimal.toCompletableFuture();
4229 >        assertTrue(g.complete(v1));
4230 >        checkCompletedNormally(g, v1);
4231 >        checkIncomplete(f);
4232 >        checkIncomplete(minimal.toCompletableFuture());
4233 >    }}
4234  
4235      /**
4236 <     * acceptEitherAsync result completes exceptionally if either
2811 <     * source cancelled
4236 >     * minimalStage.toCompletableFuture().join() awaits completion
4237       */
4238 <    public void testAcceptEitherAsync4E() {
4238 >    public void testMinimalCompletionStage_toCompletableFuture_join() throws Exception {
4239 >        for (boolean createIncomplete : new boolean[] { true, false })
4240 >        for (Integer v1 : new Integer[] { 1, null })
4241 >    {
4242          CompletableFuture<Integer> f = new CompletableFuture<>();
4243 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
4244 <        IncAction r = new IncAction();
4245 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
4246 <        assertTrue(f.cancel(true));
4247 <        checkCompletedWithWrappedCancellationException(g);
4248 <
4249 <        r = new IncAction();
4250 <        f = new CompletableFuture<>();
4251 <        f2 = new CompletableFuture<>();
4252 <        assertTrue(f2.cancel(true));
4253 <        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
4254 <        checkCompletedWithWrappedCancellationException(g);
4243 >        if (!createIncomplete) assertTrue(f.complete(v1));
4244 >        CompletionStage<Integer> minimal = f.minimalCompletionStage();
4245 >        if (createIncomplete) assertTrue(f.complete(v1));
4246 >        assertEquals(v1, minimal.toCompletableFuture().join());
4247 >        assertEquals(v1, minimal.toCompletableFuture().get());
4248 >        checkCompletedNormally(minimal.toCompletableFuture(), v1);
4249 >    }}
4250 >
4251 >    /**
4252 >     * Completion of a toCompletableFuture copy of a minimal stage
4253 >     * does not complete its source.
4254 >     */
4255 >    public void testMinimalCompletionStage_toCompletableFuture_oneWayPropagation() {
4256 >        CompletableFuture<Integer> f = new CompletableFuture<>();
4257 >        CompletionStage<Integer> g = f.minimalCompletionStage();
4258 >        assertTrue(g.toCompletableFuture().complete(1));
4259 >        assertTrue(g.toCompletableFuture().complete(null));
4260 >        assertTrue(g.toCompletableFuture().cancel(true));
4261 >        assertTrue(g.toCompletableFuture().cancel(false));
4262 >        assertTrue(g.toCompletableFuture().completeExceptionally(new CFException()));
4263 >        checkIncomplete(g.toCompletableFuture());
4264 >        f.complete(1);
4265 >        checkCompletedNormally(g.toCompletableFuture(), 1);
4266      }
4267  
4268 <    /**
4269 <     * runAfterEitherAsync result completes normally after normal
4270 <     * completion of sources
4271 <     */
4272 <    public void testRunAfterEitherAsyncE() {
4273 <        CompletableFuture<Integer> f = new CompletableFuture<>();
4274 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
4275 <        Noop r = new Noop();
4276 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2838 <        f.complete(one);
2839 <        checkCompletedNormally(g, null);
2840 <        assertTrue(r.ran);
2841 <
2842 <        r = new Noop();
2843 <        f = new CompletableFuture<>();
2844 <        f.complete(one);
2845 <        f2 = new CompletableFuture<>();
2846 <        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2847 <        checkCompletedNormally(g, null);
2848 <        assertTrue(r.ran);
4268 >    /** Demo utility method for external reliable toCompletableFuture */
4269 >    static <T> CompletableFuture<T> toCompletableFuture(CompletionStage<T> stage) {
4270 >        CompletableFuture<T> f = new CompletableFuture<>();
4271 >        stage.handle((T t, Throwable ex) -> {
4272 >                         if (ex != null) f.completeExceptionally(ex);
4273 >                         else f.complete(t);
4274 >                         return null;
4275 >                     });
4276 >        return f;
4277      }
4278  
4279 <    /**
4280 <     * runAfterEitherAsync result completes exceptionally after exceptional
4281 <     * completion of source
2854 <     */
2855 <    public void testRunAfterEitherAsync2E() {
2856 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2857 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2858 <        Noop r = new Noop();
2859 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2860 <        f.completeExceptionally(new CFException());
2861 <        checkCompletedWithWrappedCFException(g);
2862 <
2863 <        r = new Noop();
2864 <        f = new CompletableFuture<>();
2865 <        f2 = new CompletableFuture<>();
2866 <        f2.completeExceptionally(new CFException());
2867 <        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2868 <        f.complete(one);
2869 <        checkCompletedWithWrappedCFException(g);
4279 >    /** Demo utility method to join a CompletionStage */
4280 >    static <T> T join(CompletionStage<T> stage) {
4281 >        return toCompletableFuture(stage).join();
4282      }
4283  
4284      /**
4285 <     * runAfterEitherAsync result completes exceptionally if action does
4285 >     * Joining a minimal stage "by hand" works
4286       */
4287 <    public void testRunAfterEitherAsync3E() {
4287 >    public void testMinimalCompletionStage_join_by_hand() {
4288 >        for (boolean createIncomplete : new boolean[] { true, false })
4289 >        for (Integer v1 : new Integer[] { 1, null })
4290 >    {
4291          CompletableFuture<Integer> f = new CompletableFuture<>();
4292 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
4293 <        FailingNoop r = new FailingNoop();
4294 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
4295 <        f.complete(one);
4296 <        checkCompletedWithWrappedCFException(g);
4297 <    }
4292 >        CompletionStage<Integer> minimal = f.minimalCompletionStage();
4293 >        CompletableFuture<Integer> g = new CompletableFuture<>();
4294 >        if (!createIncomplete) assertTrue(f.complete(v1));
4295 >        minimal.thenAccept(x -> g.complete(x));
4296 >        if (createIncomplete) assertTrue(f.complete(v1));
4297 >        g.join();
4298 >        checkCompletedNormally(g, v1);
4299 >        checkCompletedNormally(f, v1);
4300 >        assertEquals(v1, join(minimal));
4301 >    }}
4302  
4303 <    /**
4304 <     * runAfterEitherAsync result completes exceptionally if either
4305 <     * source cancelled
4306 <     */
4307 <    public void testRunAfterEitherAsync4E() {
4308 <        CompletableFuture<Integer> f = new CompletableFuture<>();
4309 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
4310 <        Noop r = new Noop();
4311 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
4312 <        assertTrue(f.cancel(true));
4313 <        checkCompletedWithWrappedCancellationException(g);
4303 >    static class Monad {
4304 >        static class ZeroException extends RuntimeException {
4305 >            public ZeroException() { super("monadic zero"); }
4306 >        }
4307 >        // "return", "unit"
4308 >        static <T> CompletableFuture<T> unit(T value) {
4309 >            return completedFuture(value);
4310 >        }
4311 >        // monadic zero ?
4312 >        static <T> CompletableFuture<T> zero() {
4313 >            return failedFuture(new ZeroException());
4314 >        }
4315 >        // >=>
4316 >        static <T,U,V> Function<T, CompletableFuture<V>> compose
4317 >            (Function<T, CompletableFuture<U>> f,
4318 >             Function<U, CompletableFuture<V>> g) {
4319 >            return x -> f.apply(x).thenCompose(g);
4320 >        }
4321  
4322 <        r = new Noop();
4323 <        f = new CompletableFuture<>();
4324 <        f2 = new CompletableFuture<>();
4325 <        assertTrue(f2.cancel(true));
4326 <        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
4327 <        checkCompletedWithWrappedCancellationException(g);
4328 <    }
4322 >        static void assertZero(CompletableFuture<?> f) {
4323 >            try {
4324 >                f.getNow(null);
4325 >                throw new AssertionError("should throw");
4326 >            } catch (CompletionException success) {
4327 >                assertTrue(success.getCause() instanceof ZeroException);
4328 >            }
4329 >        }
4330  
4331 <    /**
4332 <     * thenComposeAsync result completes normally after normal
4333 <     * completion of source
4334 <     */
4335 <    public void testThenComposeAsyncE() {
4336 <        CompletableFuture<Integer> f = new CompletableFuture<>();
4337 <        CompletableFutureInc r = new CompletableFutureInc();
4338 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
4339 <        f.complete(one);
4340 <        checkCompletedNormally(g, two);
4341 <    }
4331 >        static <T> void assertFutureEquals(CompletableFuture<T> f,
4332 >                                           CompletableFuture<T> g) {
4333 >            T fval = null, gval = null;
4334 >            Throwable fex = null, gex = null;
4335 >
4336 >            try { fval = f.get(); }
4337 >            catch (ExecutionException ex) { fex = ex.getCause(); }
4338 >            catch (Throwable ex) { fex = ex; }
4339 >
4340 >            try { gval = g.get(); }
4341 >            catch (ExecutionException ex) { gex = ex.getCause(); }
4342 >            catch (Throwable ex) { gex = ex; }
4343 >
4344 >            if (fex != null || gex != null)
4345 >                assertSame(fex.getClass(), gex.getClass());
4346 >            else
4347 >                assertEquals(fval, gval);
4348 >        }
4349  
4350 <    /**
4351 <     * thenComposeAsync result completes exceptionally after
4352 <     * exceptional completion of source
2919 <     */
2920 <    public void testThenComposeAsync2E() {
2921 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2922 <        CompletableFutureInc r = new CompletableFutureInc();
2923 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2924 <        f.completeExceptionally(new CFException());
2925 <        checkCompletedWithWrappedCFException(g);
2926 <    }
4350 >        static class PlusFuture<T> extends CompletableFuture<T> {
4351 >            AtomicReference<Throwable> firstFailure = new AtomicReference<>(null);
4352 >        }
4353  
4354 <    /**
4355 <     * thenComposeAsync result completes exceptionally if action does
4356 <     */
4357 <    public void testThenComposeAsync3E() {
4358 <        CompletableFuture<Integer> f = new CompletableFuture<>();
4359 <        FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
4360 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
4361 <        f.complete(one);
4362 <        checkCompletedWithWrappedCFException(g);
4354 >        /** Implements "monadic plus". */
4355 >        static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f,
4356 >                                             CompletableFuture<? extends T> g) {
4357 >            PlusFuture<T> plus = new PlusFuture<T>();
4358 >            BiConsumer<T, Throwable> action = (T result, Throwable ex) -> {
4359 >                try {
4360 >                    if (ex == null) {
4361 >                        if (plus.complete(result))
4362 >                            if (plus.firstFailure.get() != null)
4363 >                                plus.firstFailure.set(null);
4364 >                    }
4365 >                    else if (plus.firstFailure.compareAndSet(null, ex)) {
4366 >                        if (plus.isDone())
4367 >                            plus.firstFailure.set(null);
4368 >                    }
4369 >                    else {
4370 >                        // first failure has precedence
4371 >                        Throwable first = plus.firstFailure.getAndSet(null);
4372 >
4373 >                        // may fail with "Self-suppression not permitted"
4374 >                        try { first.addSuppressed(ex); }
4375 >                        catch (Exception ignored) {}
4376 >
4377 >                        plus.completeExceptionally(first);
4378 >                    }
4379 >                } catch (Throwable unexpected) {
4380 >                    plus.completeExceptionally(unexpected);
4381 >                }
4382 >            };
4383 >            f.whenComplete(action);
4384 >            g.whenComplete(action);
4385 >            return plus;
4386 >        }
4387      }
4388  
4389      /**
4390 <     * thenComposeAsync result completes exceptionally if source cancelled
4390 >     * CompletableFuture is an additive monad - sort of.
4391 >     * https://en.wikipedia.org/wiki/Monad_(functional_programming)#Additive_monads
4392       */
4393 <    public void testThenComposeAsync4E() {
4394 <        CompletableFuture<Integer> f = new CompletableFuture<>();
4395 <        CompletableFutureInc r = new CompletableFutureInc();
4396 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
4397 <        assertTrue(f.cancel(true));
4398 <        checkCompletedWithWrappedCancellationException(g);
4399 <    }
4400 <
4401 <    // other static methods
4393 >    public void testAdditiveMonad() throws Throwable {
4394 >        Function<Long, CompletableFuture<Long>> unit = Monad::unit;
4395 >        CompletableFuture<Long> zero = Monad.zero();
4396 >
4397 >        // Some mutually non-commutative functions
4398 >        Function<Long, CompletableFuture<Long>> triple
4399 >            = x -> Monad.unit(3 * x);
4400 >        Function<Long, CompletableFuture<Long>> inc
4401 >            = x -> Monad.unit(x + 1);
4402 >
4403 >        // unit is a right identity: m >>= unit === m
4404 >        Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit),
4405 >                                 inc.apply(5L));
4406 >        // unit is a left identity: (unit x) >>= f === f x
4407 >        Monad.assertFutureEquals(unit.apply(5L).thenCompose(inc),
4408 >                                 inc.apply(5L));
4409 >
4410 >        // associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) )
4411 >        Monad.assertFutureEquals(
4412 >            unit.apply(5L).thenCompose(inc).thenCompose(triple),
4413 >            unit.apply(5L).thenCompose(x -> inc.apply(x).thenCompose(triple)));
4414 >
4415 >        // The case for CompletableFuture as an additive monad is weaker...
4416 >
4417 >        // zero is a monadic zero
4418 >        Monad.assertZero(zero);
4419 >
4420 >        // left zero: zero >>= f === zero
4421 >        Monad.assertZero(zero.thenCompose(inc));
4422 >        // right zero: f >>= (\x -> zero) === zero
4423 >        Monad.assertZero(inc.apply(5L).thenCompose(x -> zero));
4424 >
4425 >        // f plus zero === f
4426 >        Monad.assertFutureEquals(Monad.unit(5L),
4427 >                                 Monad.plus(Monad.unit(5L), zero));
4428 >        // zero plus f === f
4429 >        Monad.assertFutureEquals(Monad.unit(5L),
4430 >                                 Monad.plus(zero, Monad.unit(5L)));
4431 >        // zero plus zero === zero
4432 >        Monad.assertZero(Monad.plus(zero, zero));
4433 >        {
4434 >            CompletableFuture<Long> f = Monad.plus(Monad.unit(5L),
4435 >                                                   Monad.unit(8L));
4436 >            // non-determinism
4437 >            assertTrue(f.get() == 5L || f.get() == 8L);
4438 >        }
4439  
4440 <    /**
4441 <     * allOf(no component futures) returns a future completed normally
4442 <     * with the value null
4443 <     */
4444 <    public void testAllOf_empty() throws Exception {
4445 <        CompletableFuture<Void> f = CompletableFuture.allOf();
4446 <        checkCompletedNormally(f, null);
4440 >        CompletableFuture<Long> godot = new CompletableFuture<>();
4441 >        // f plus godot === f (doesn't wait for godot)
4442 >        Monad.assertFutureEquals(Monad.unit(5L),
4443 >                                 Monad.plus(Monad.unit(5L), godot));
4444 >        // godot plus f === f (doesn't wait for godot)
4445 >        Monad.assertFutureEquals(Monad.unit(5L),
4446 >                                 Monad.plus(godot, Monad.unit(5L)));
4447      }
4448  
4449 <    /**
4450 <     * allOf returns a future completed normally with the value null
4451 <     * when all components complete normally
4452 <     */
4453 <    public void testAllOf_normal() throws Exception {
4454 <        for (int k = 1; k < 20; ++k) {
4455 <            CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k];
4456 <            for (int i = 0; i < k; ++i)
4457 <                fs[i] = new CompletableFuture<>();
4458 <            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
4459 <            for (int i = 0; i < k; ++i) {
4460 <                checkIncomplete(f);
4461 <                checkIncomplete(CompletableFuture.allOf(fs));
4462 <                fs[i].complete(one);
4463 <            }
4464 <            checkCompletedNormally(f, null);
4465 <            checkCompletedNormally(CompletableFuture.allOf(fs), null);
4449 >    /** Test long recursive chains of CompletableFutures with cascading completions */
4450 >    @SuppressWarnings("FutureReturnValueIgnored")
4451 >    public void testRecursiveChains() throws Throwable {
4452 >        for (ExecutionMode m : ExecutionMode.values())
4453 >        for (boolean addDeadEnds : new boolean[] { true, false })
4454 >    {
4455 >        final int val = 42;
4456 >        final int n = expensiveTests ? 1_000 : 2;
4457 >        CompletableFuture<Integer> head = new CompletableFuture<>();
4458 >        CompletableFuture<Integer> tail = head;
4459 >        for (int i = 0; i < n; i++) {
4460 >            if (addDeadEnds) m.thenApply(tail, v -> v + 1);
4461 >            tail = m.thenApply(tail, v -> v + 1);
4462 >            if (addDeadEnds) m.applyToEither(tail, tail, v -> v + 1);
4463 >            tail = m.applyToEither(tail, tail, v -> v + 1);
4464 >            if (addDeadEnds) m.thenCombine(tail, tail, (v, w) -> v + 1);
4465 >            tail = m.thenCombine(tail, tail, (v, w) -> v + 1);
4466          }
4467 +        head.complete(val);
4468 +        assertEquals(val + 3 * n, (int) tail.join());
4469 +    }}
4470 +
4471 +    /**
4472 +     * A single CompletableFuture with many dependents.
4473 +     * A demo of scalability - runtime is O(n).
4474 +     */
4475 +    @SuppressWarnings("FutureReturnValueIgnored")
4476 +    public void testManyDependents() throws Throwable {
4477 +        final int n = expensiveTests ? 1_000_000 : 10;
4478 +        final CompletableFuture<Void> head = new CompletableFuture<>();
4479 +        final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null);
4480 +        final AtomicInteger count = new AtomicInteger(0);
4481 +        for (int i = 0; i < n; i++) {
4482 +            head.thenRun(() -> count.getAndIncrement());
4483 +            head.thenAccept(x -> count.getAndIncrement());
4484 +            head.thenApply(x -> count.getAndIncrement());
4485 +
4486 +            head.runAfterBoth(complete, () -> count.getAndIncrement());
4487 +            head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement());
4488 +            head.thenCombine(complete, (x, y) -> count.getAndIncrement());
4489 +            complete.runAfterBoth(head, () -> count.getAndIncrement());
4490 +            complete.thenAcceptBoth(head, (x, y) -> count.getAndIncrement());
4491 +            complete.thenCombine(head, (x, y) -> count.getAndIncrement());
4492 +
4493 +            head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement());
4494 +            head.acceptEither(new CompletableFuture<Void>(), x -> count.getAndIncrement());
4495 +            head.applyToEither(new CompletableFuture<Void>(), x -> count.getAndIncrement());
4496 +            new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement());
4497 +            new CompletableFuture<Void>().acceptEither(head, x -> count.getAndIncrement());
4498 +            new CompletableFuture<Void>().applyToEither(head, x -> count.getAndIncrement());
4499 +        }
4500 +        head.complete(null);
4501 +        assertEquals(5 * 3 * n, count.get());
4502      }
4503  
4504 <    /**
4505 <     * anyOf(no component futures) returns an incomplete future
4506 <     */
4507 <    public void testAnyOf_empty() throws Exception {
4508 <        CompletableFuture<Object> f = CompletableFuture.anyOf();
4509 <        checkIncomplete(f);
4510 <    }
4511 <
4512 <    /**
4513 <     * anyOf returns a future completed normally with a value when
4514 <     * a component future does
4515 <     */
4516 <    public void testAnyOf_normal() throws Exception {
4517 <        for (int k = 0; k < 10; ++k) {
4518 <            CompletableFuture[] fs = new CompletableFuture[k];
4519 <            for (int i = 0; i < k; ++i)
4520 <                fs[i] = new CompletableFuture<>();
4521 <            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
4522 <            checkIncomplete(f);
4523 <            for (int i = 0; i < k; ++i) {
4524 <                fs[i].complete(one);
4525 <                checkCompletedNormally(f, one);
3003 <                checkCompletedNormally(CompletableFuture.anyOf(fs), one);
3004 <            }
4504 >    /** ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck */
4505 >    @SuppressWarnings("FutureReturnValueIgnored")
4506 >    public void testCoCompletionGarbageRetention() throws Throwable {
4507 >        final int n = expensiveTests ? 1_000_000 : 10;
4508 >        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
4509 >        CompletableFuture<Integer> f;
4510 >        for (int i = 0; i < n; i++) {
4511 >            f = new CompletableFuture<>();
4512 >            f.runAfterEither(incomplete, () -> {});
4513 >            f.complete(null);
4514 >
4515 >            f = new CompletableFuture<>();
4516 >            f.acceptEither(incomplete, x -> {});
4517 >            f.complete(null);
4518 >
4519 >            f = new CompletableFuture<>();
4520 >            f.applyToEither(incomplete, x -> x);
4521 >            f.complete(null);
4522 >
4523 >            f = new CompletableFuture<>();
4524 >            CompletableFuture.anyOf(f, incomplete);
4525 >            f.complete(null);
4526          }
3006    }
4527  
4528 <    /**
4529 <     * anyOf result completes exceptionally when any component does.
4530 <     */
4531 <    public void testAnyOf_exceptional() throws Exception {
4532 <        for (int k = 0; k < 10; ++k) {
4533 <            CompletableFuture[] fs = new CompletableFuture[k];
4534 <            for (int i = 0; i < k; ++i)
4535 <                fs[i] = new CompletableFuture<>();
4536 <            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
4537 <            checkIncomplete(f);
4538 <            for (int i = 0; i < k; ++i) {
4539 <                fs[i].completeExceptionally(new CFException());
4540 <                checkCompletedWithWrappedCFException(f);
4541 <                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
4542 <            }
4528 >        for (int i = 0; i < n; i++) {
4529 >            f = new CompletableFuture<>();
4530 >            incomplete.runAfterEither(f, () -> {});
4531 >            f.complete(null);
4532 >
4533 >            f = new CompletableFuture<>();
4534 >            incomplete.acceptEither(f, x -> {});
4535 >            f.complete(null);
4536 >
4537 >            f = new CompletableFuture<>();
4538 >            incomplete.applyToEither(f, x -> x);
4539 >            f.complete(null);
4540 >
4541 >            f = new CompletableFuture<>();
4542 >            CompletableFuture.anyOf(incomplete, f);
4543 >            f.complete(null);
4544          }
4545      }
4546  
4547      /**
4548 <     * Completion methods throw NullPointerException with null arguments
4549 <     */
4550 <    public void testNPE() {
4551 <        CompletableFuture<Integer> f = new CompletableFuture<>();
4552 <        CompletableFuture<Integer> g = new CompletableFuture<>();
4553 <        CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null;
4554 <        CompletableFuture<?> h;
4555 <        ThreadExecutor exec = new ThreadExecutor();
4556 <
4557 <        Runnable[] throwingActions = {
4558 <            () -> CompletableFuture.supplyAsync(null),
4559 <            () -> CompletableFuture.supplyAsync(null, exec),
4560 <            () -> CompletableFuture.supplyAsync(supplyOne, null),
4561 <
4562 <            () -> CompletableFuture.runAsync(null),
4563 <            () -> CompletableFuture.runAsync(null, exec),
4564 <            () -> CompletableFuture.runAsync(() -> {}, null),
4565 <
4566 <            () -> f.completeExceptionally(null),
4567 <
4568 <            () -> f.thenApply(null),
4569 <            () -> f.thenApplyAsync(null),
4570 <            () -> f.thenApplyAsync((x) -> x, null),
4571 <            () -> f.thenApplyAsync(null, exec),
4572 <
4573 <            () -> f.thenAccept(null),
4574 <            () -> f.thenAcceptAsync(null),
4575 <            () -> f.thenAcceptAsync((x) -> {} , null),
4576 <            () -> f.thenAcceptAsync(null, exec),
4577 <
4578 <            () -> f.thenRun(null),
4579 <            () -> f.thenRunAsync(null),
4580 <            () -> f.thenRunAsync(() -> {} , null),
4581 <            () -> f.thenRunAsync(null, exec),
4582 <
4583 <            () -> f.thenCombine(g, null),
4584 <            () -> f.thenCombineAsync(g, null),
4585 <            () -> f.thenCombineAsync(g, null, exec),
4586 <            () -> f.thenCombine(nullFuture, (x, y) -> x),
4587 <            () -> f.thenCombineAsync(nullFuture, (x, y) -> x),
4588 <            () -> f.thenCombineAsync(nullFuture, (x, y) -> x, exec),
4589 <            () -> f.thenCombineAsync(g, (x, y) -> x, null),
4590 <
4591 <            () -> f.thenAcceptBoth(g, null),
4592 <            () -> f.thenAcceptBothAsync(g, null),
4593 <            () -> f.thenAcceptBothAsync(g, null, exec),
4594 <            () -> f.thenAcceptBoth(nullFuture, (x, y) -> {}),
4595 <            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}),
4596 <            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec),
4597 <            () -> f.thenAcceptBothAsync(g, (x, y) -> {}, null),
4598 <
4599 <            () -> f.runAfterBoth(g, null),
4600 <            () -> f.runAfterBothAsync(g, null),
4601 <            () -> f.runAfterBothAsync(g, null, exec),
4602 <            () -> f.runAfterBoth(nullFuture, () -> {}),
4603 <            () -> f.runAfterBothAsync(nullFuture, () -> {}),
4604 <            () -> f.runAfterBothAsync(nullFuture, () -> {}, exec),
4605 <            () -> f.runAfterBothAsync(g, () -> {}, null),
4606 <
4607 <            () -> f.applyToEither(g, null),
4608 <            () -> f.applyToEitherAsync(g, null),
4609 <            () -> f.applyToEitherAsync(g, null, exec),
4610 <            () -> f.applyToEither(nullFuture, (x) -> x),
4611 <            () -> f.applyToEitherAsync(nullFuture, (x) -> x),
4612 <            () -> f.applyToEitherAsync(nullFuture, (x) -> x, exec),
4613 <            () -> f.applyToEitherAsync(g, (x) -> x, null),
4614 <
4615 <            () -> f.acceptEither(g, null),
4616 <            () -> f.acceptEitherAsync(g, null),
4617 <            () -> f.acceptEitherAsync(g, null, exec),
4618 <            () -> f.acceptEither(nullFuture, (x) -> {}),
4619 <            () -> f.acceptEitherAsync(nullFuture, (x) -> {}),
4620 <            () -> f.acceptEitherAsync(nullFuture, (x) -> {}, exec),
4621 <            () -> f.acceptEitherAsync(g, (x) -> {}, null),
4622 <
4623 <            () -> f.runAfterEither(g, null),
4624 <            () -> f.runAfterEitherAsync(g, null),
4625 <            () -> f.runAfterEitherAsync(g, null, exec),
4626 <            () -> f.runAfterEither(nullFuture, () -> {}),
4627 <            () -> f.runAfterEitherAsync(nullFuture, () -> {}),
4628 <            () -> f.runAfterEitherAsync(nullFuture, () -> {}, exec),
4629 <            () -> f.runAfterEitherAsync(g, () -> {}, null),
4630 <
4631 <            () -> f.thenCompose(null),
4632 <            () -> f.thenComposeAsync(null),
4633 <            () -> f.thenComposeAsync(new CompletableFutureInc(), null),
4634 <            () -> f.thenComposeAsync(null, exec),
4635 <
4636 <            () -> f.exceptionally(null),
4637 <
4638 <            () -> f.handle(null),
4639 <
4640 <            () -> CompletableFuture.allOf((CompletableFuture<?>)null),
4641 <            () -> CompletableFuture.allOf((CompletableFuture<?>[])null),
4642 <            () -> CompletableFuture.allOf(f, null),
4643 <            () -> CompletableFuture.allOf(null, f),
4644 <
4645 <            () -> CompletableFuture.anyOf((CompletableFuture<?>)null),
4646 <            () -> CompletableFuture.anyOf((CompletableFuture<?>[])null),
4647 <            () -> CompletableFuture.anyOf(f, null),
4648 <            () -> CompletableFuture.anyOf(null, f),
4649 <
4650 <            () -> f.obtrudeException(null),
4651 <        };
4652 <
4653 <        assertThrows(NullPointerException.class, throwingActions);
4654 <        assertEquals(0, exec.count.get());
4548 >     * Reproduction recipe for:
4549 >     * 8160402: Garbage retention with CompletableFuture.anyOf
4550 >     * cvs update -D '2016-05-01' ./src/main/java/util/concurrent/CompletableFuture.java && ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testAnyOfGarbageRetention tck; cvs update -A
4551 >     */
4552 >    public void testAnyOfGarbageRetention() throws Throwable {
4553 >        for (Integer v : new Integer[] { 1, null })
4554 >    {
4555 >        final int n = expensiveTests ? 100_000 : 10;
4556 >        CompletableFuture<Integer>[] fs
4557 >            = (CompletableFuture<Integer>[]) new CompletableFuture<?>[100];
4558 >        for (int i = 0; i < fs.length; i++)
4559 >            fs[i] = new CompletableFuture<>();
4560 >        fs[fs.length - 1].complete(v);
4561 >        for (int i = 0; i < n; i++)
4562 >            checkCompletedNormally(CompletableFuture.anyOf(fs), v);
4563 >    }}
4564 >
4565 >    /**
4566 >     * Checks for garbage retention with allOf.
4567 >     *
4568 >     * As of 2016-07, fails with OOME:
4569 >     * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledAllOfGarbageRetention tck
4570 >     */
4571 >    public void testCancelledAllOfGarbageRetention() throws Throwable {
4572 >        final int n = expensiveTests ? 100_000 : 10;
4573 >        CompletableFuture<Integer>[] fs
4574 >            = (CompletableFuture<Integer>[]) new CompletableFuture<?>[100];
4575 >        for (int i = 0; i < fs.length; i++)
4576 >            fs[i] = new CompletableFuture<>();
4577 >        for (int i = 0; i < n; i++)
4578 >            assertTrue(CompletableFuture.allOf(fs).cancel(false));
4579 >    }
4580 >
4581 >    /**
4582 >     * Checks for garbage retention when a dependent future is
4583 >     * cancelled and garbage-collected.
4584 >     * 8161600: Garbage retention when source CompletableFutures are never completed
4585 >     *
4586 >     * As of 2016-07, fails with OOME:
4587 >     * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledGarbageRetention tck
4588 >     */
4589 >    public void testCancelledGarbageRetention() throws Throwable {
4590 >        final int n = expensiveTests ? 100_000 : 10;
4591 >        CompletableFuture<Integer> neverCompleted = new CompletableFuture<>();
4592 >        for (int i = 0; i < n; i++)
4593 >            assertTrue(neverCompleted.thenRun(() -> {}).cancel(true));
4594 >    }
4595 >
4596 >    /**
4597 >     * Checks for garbage retention when MinimalStage.toCompletableFuture()
4598 >     * is invoked many times.
4599 >     * 8161600: Garbage retention when source CompletableFutures are never completed
4600 >     *
4601 >     * As of 2016-07, fails with OOME:
4602 >     * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testToCompletableFutureGarbageRetention tck
4603 >     */
4604 >    public void testToCompletableFutureGarbageRetention() throws Throwable {
4605 >        final int n = expensiveTests ? 900_000 : 10;
4606 >        CompletableFuture<Integer> neverCompleted = new CompletableFuture<>();
4607 >        CompletionStage minimal = neverCompleted.minimalCompletionStage();
4608 >        for (int i = 0; i < n; i++)
4609 >            assertTrue(minimal.toCompletableFuture().cancel(true));
4610 >    }
4611 >
4612 > //     static <U> U join(CompletionStage<U> stage) {
4613 > //         CompletableFuture<U> f = new CompletableFuture<>();
4614 > //         stage.whenComplete((v, ex) -> {
4615 > //             if (ex != null) f.completeExceptionally(ex); else f.complete(v);
4616 > //         });
4617 > //         return f.join();
4618 > //     }
4619 >
4620 > //     static <U> boolean isDone(CompletionStage<U> stage) {
4621 > //         CompletableFuture<U> f = new CompletableFuture<>();
4622 > //         stage.whenComplete((v, ex) -> {
4623 > //             if (ex != null) f.completeExceptionally(ex); else f.complete(v);
4624 > //         });
4625 > //         return f.isDone();
4626 > //     }
4627 >
4628 > //     static <U> U join2(CompletionStage<U> stage) {
4629 > //         return stage.toCompletableFuture().copy().join();
4630 > //     }
4631 >
4632 > //     static <U> boolean isDone2(CompletionStage<U> stage) {
4633 > //         return stage.toCompletableFuture().copy().isDone();
4634 > //     }
4635 >
4636 >    // For testing default implementations
4637 >    // Only non-default interface methods defined.
4638 >    static final class DelegatedCompletionStage<T> implements CompletionStage<T> {
4639 >        final CompletableFuture<T> cf;
4640 >        DelegatedCompletionStage(CompletableFuture<T> cf) { this.cf = cf; }
4641 >        public CompletableFuture<T> toCompletableFuture() {
4642 >            return cf; }
4643 >        public CompletionStage<Void> thenRun
4644 >            (Runnable action) {
4645 >            return cf.thenRun(action); }
4646 >        public CompletionStage<Void> thenRunAsync
4647 >            (Runnable action) {
4648 >            return cf.thenRunAsync(action); }
4649 >        public CompletionStage<Void> thenRunAsync
4650 >            (Runnable action,
4651 >             Executor executor) {
4652 >            return cf.thenRunAsync(action, executor); }
4653 >        public CompletionStage<Void> thenAccept
4654 >            (Consumer<? super T> action) {
4655 >            return cf.thenAccept(action); }
4656 >        public CompletionStage<Void> thenAcceptAsync
4657 >            (Consumer<? super T> action) {
4658 >            return cf.thenAcceptAsync(action); }
4659 >        public CompletionStage<Void> thenAcceptAsync
4660 >            (Consumer<? super T> action,
4661 >             Executor executor) {
4662 >            return cf.thenAcceptAsync(action, executor); }
4663 >        public <U> CompletionStage<U> thenApply
4664 >            (Function<? super T,? extends U> a) {
4665 >            return cf.thenApply(a); }
4666 >        public <U> CompletionStage<U> thenApplyAsync
4667 >            (Function<? super T,? extends U> fn) {
4668 >            return cf.thenApplyAsync(fn); }
4669 >        public <U> CompletionStage<U> thenApplyAsync
4670 >            (Function<? super T,? extends U> fn,
4671 >             Executor executor) {
4672 >            return cf.thenApplyAsync(fn, executor); }
4673 >        public <U,V> CompletionStage<V> thenCombine
4674 >            (CompletionStage<? extends U> other,
4675 >             BiFunction<? super T,? super U,? extends V> fn) {
4676 >            return cf.thenCombine(other, fn); }
4677 >        public <U,V> CompletionStage<V> thenCombineAsync
4678 >            (CompletionStage<? extends U> other,
4679 >             BiFunction<? super T,? super U,? extends V> fn) {
4680 >            return cf.thenCombineAsync(other, fn); }
4681 >        public <U,V> CompletionStage<V> thenCombineAsync
4682 >            (CompletionStage<? extends U> other,
4683 >             BiFunction<? super T,? super U,? extends V> fn,
4684 >             Executor executor) {
4685 >            return cf.thenCombineAsync(other, fn, executor); }
4686 >        public <U> CompletionStage<Void> thenAcceptBoth
4687 >            (CompletionStage<? extends U> other,
4688 >             BiConsumer<? super T, ? super U> action) {
4689 >            return cf.thenAcceptBoth(other, action); }
4690 >        public <U> CompletionStage<Void> thenAcceptBothAsync
4691 >            (CompletionStage<? extends U> other,
4692 >             BiConsumer<? super T, ? super U> action) {
4693 >            return cf.thenAcceptBothAsync(other, action); }
4694 >        public <U> CompletionStage<Void> thenAcceptBothAsync
4695 >            (CompletionStage<? extends U> other,
4696 >             BiConsumer<? super T, ? super U> action,
4697 >             Executor executor) {
4698 >            return cf.thenAcceptBothAsync(other, action, executor); }
4699 >        public CompletionStage<Void> runAfterBoth
4700 >            (CompletionStage<?> other,
4701 >             Runnable action) {
4702 >            return cf.runAfterBoth(other, action); }
4703 >        public CompletionStage<Void> runAfterBothAsync
4704 >            (CompletionStage<?> other,
4705 >             Runnable action) {
4706 >            return cf.runAfterBothAsync(other, action); }
4707 >        public CompletionStage<Void> runAfterBothAsync
4708 >            (CompletionStage<?> other,
4709 >             Runnable action,
4710 >             Executor executor) {
4711 >            return cf.runAfterBothAsync(other, action, executor); }
4712 >        public <U> CompletionStage<U> applyToEither
4713 >            (CompletionStage<? extends T> other,
4714 >             Function<? super T, U> fn) {
4715 >            return cf.applyToEither(other, fn); }
4716 >        public <U> CompletionStage<U> applyToEitherAsync
4717 >            (CompletionStage<? extends T> other,
4718 >             Function<? super T, U> fn) {
4719 >            return cf.applyToEitherAsync(other, fn); }
4720 >        public <U> CompletionStage<U> applyToEitherAsync
4721 >            (CompletionStage<? extends T> other,
4722 >             Function<? super T, U> fn,
4723 >             Executor executor) {
4724 >            return cf.applyToEitherAsync(other, fn, executor); }
4725 >        public CompletionStage<Void> acceptEither
4726 >            (CompletionStage<? extends T> other,
4727 >             Consumer<? super T> action) {
4728 >            return cf.acceptEither(other, action); }
4729 >        public CompletionStage<Void> acceptEitherAsync
4730 >            (CompletionStage<? extends T> other,
4731 >             Consumer<? super T> action) {
4732 >            return cf.acceptEitherAsync(other, action); }
4733 >        public CompletionStage<Void> acceptEitherAsync
4734 >            (CompletionStage<? extends T> other,
4735 >             Consumer<? super T> action,
4736 >             Executor executor) {
4737 >            return cf.acceptEitherAsync(other, action, executor); }
4738 >        public CompletionStage<Void> runAfterEither
4739 >            (CompletionStage<?> other,
4740 >             Runnable action) {
4741 >            return cf.runAfterEither(other, action); }
4742 >        public CompletionStage<Void> runAfterEitherAsync
4743 >            (CompletionStage<?> other,
4744 >             Runnable action) {
4745 >            return cf.runAfterEitherAsync(other, action); }
4746 >        public CompletionStage<Void> runAfterEitherAsync
4747 >            (CompletionStage<?> other,
4748 >             Runnable action,
4749 >             Executor executor) {
4750 >            return cf.runAfterEitherAsync(other, action, executor); }
4751 >        public <U> CompletionStage<U> thenCompose
4752 >            (Function<? super T, ? extends CompletionStage<U>> fn) {
4753 >            return cf.thenCompose(fn); }
4754 >        public <U> CompletionStage<U> thenComposeAsync
4755 >            (Function<? super T, ? extends CompletionStage<U>> fn) {
4756 >            return cf.thenComposeAsync(fn); }
4757 >        public <U> CompletionStage<U> thenComposeAsync
4758 >            (Function<? super T, ? extends CompletionStage<U>> fn,
4759 >             Executor executor) {
4760 >            return cf.thenComposeAsync(fn, executor); }
4761 >        public <U> CompletionStage<U> handle
4762 >            (BiFunction<? super T, Throwable, ? extends U> fn) {
4763 >            return cf.handle(fn); }
4764 >        public <U> CompletionStage<U> handleAsync
4765 >            (BiFunction<? super T, Throwable, ? extends U> fn) {
4766 >            return cf.handleAsync(fn); }
4767 >        public <U> CompletionStage<U> handleAsync
4768 >            (BiFunction<? super T, Throwable, ? extends U> fn,
4769 >             Executor executor) {
4770 >            return cf.handleAsync(fn, executor); }
4771 >        public CompletionStage<T> whenComplete
4772 >            (BiConsumer<? super T, ? super Throwable> action) {
4773 >            return cf.whenComplete(action); }
4774 >        public CompletionStage<T> whenCompleteAsync
4775 >            (BiConsumer<? super T, ? super Throwable> action) {
4776 >            return cf.whenCompleteAsync(action); }
4777 >        public CompletionStage<T> whenCompleteAsync
4778 >            (BiConsumer<? super T, ? super Throwable> action,
4779 >             Executor executor) {
4780 >            return cf.whenCompleteAsync(action, executor); }
4781 >        public CompletionStage<T> exceptionally
4782 >            (Function<Throwable, ? extends T> fn) {
4783 >            return cf.exceptionally(fn); }
4784      }
4785  
4786      /**
4787 <     * toCompletableFuture returns this CompletableFuture.
4787 >     * default-implemented exceptionallyAsync action completes with
4788 >     * function value on source exception
4789       */
4790 <    public void testToCompletableFuture() {
4791 <        CompletableFuture<Integer> f = new CompletableFuture<>();
4792 <        assertSame(f, f.toCompletableFuture());
4793 <    }
4790 >    public void testDefaulExceptionallyAsync_exceptionalCompletion() {
4791 >        for (boolean createIncomplete : new boolean[] { true, false })
4792 >        for (Integer v1 : new Integer[] { 1, null })
4793 >    {
4794 >        final AtomicInteger a = new AtomicInteger(0);
4795 >        final CFException ex = new CFException();
4796 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
4797 >        final DelegatedCompletionStage<Integer> d =
4798 >            new DelegatedCompletionStage<Integer>(f);
4799 >        if (!createIncomplete) f.completeExceptionally(ex);
4800 >        final CompletionStage<Integer> g = d.exceptionallyAsync
4801 >            ((Throwable t) -> {
4802 >                threadAssertSame(t, ex);
4803 >                a.getAndIncrement();
4804 >                return v1;
4805 >            });
4806 >        if (createIncomplete) f.completeExceptionally(ex);
4807 >
4808 >        checkCompletedNormally(g.toCompletableFuture(), v1);
4809 >        assertEquals(1, a.get());
4810 >    }}
4811 >
4812 >    /**
4813 >     * Under default implementation, if an "exceptionally action"
4814 >     * throws an exception, it completes exceptionally with that
4815 >     * exception
4816 >     */
4817 >    public void testDefaulExceptionallyAsync_exceptionalCompletionActionFailed() {
4818 >        for (boolean createIncomplete : new boolean[] { true, false })
4819 >    {
4820 >        final AtomicInteger a = new AtomicInteger(0);
4821 >        final CFException ex1 = new CFException();
4822 >        final CFException ex2 = new CFException();
4823 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
4824 >        final DelegatedCompletionStage<Integer> d =
4825 >            new DelegatedCompletionStage<Integer>(f);
4826 >        if (!createIncomplete) f.completeExceptionally(ex1);
4827 >        final CompletionStage<Integer> g = d.exceptionallyAsync
4828 >            ((Throwable t) -> {
4829 >                threadAssertSame(t, ex1);
4830 >                a.getAndIncrement();
4831 >                throw ex2;
4832 >            });
4833 >        if (createIncomplete) f.completeExceptionally(ex1);
4834 >
4835 >        checkCompletedWithWrappedException(g.toCompletableFuture(), ex2);
4836 >        checkCompletedExceptionally(f, ex1);
4837 >        checkCompletedExceptionally(d.toCompletableFuture(), ex1);
4838 >        assertEquals(1, a.get());
4839 >    }}
4840  
4841      /**
4842 <     * whenComplete action executes on normal completion, propagating
4843 <     * source result.
4842 >     * default exceptionallyCompose result completes normally after normal
4843 >     * completion of source
4844       */
4845 <    public void testWhenComplete1() {
4846 <        final AtomicInteger a = new AtomicInteger();
4847 <        CompletableFuture<Integer> f = new CompletableFuture<>();
4848 <        CompletableFuture<Integer> g =
4849 <            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
4850 <        f.complete(three);
4851 <        checkCompletedNormally(f, three);
4852 <        checkCompletedNormally(g, three);
4853 <        assertEquals(a.get(), 1);
4854 <    }
4845 >    public void testDefaultExceptionallyCompose_normalCompletion() {
4846 >        for (boolean createIncomplete : new boolean[] { true, false })
4847 >        for (Integer v1 : new Integer[] { 1, null })
4848 >    {
4849 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
4850 >        final ExceptionalCompletableFutureFunction r =
4851 >            new ExceptionalCompletableFutureFunction(ExecutionMode.SYNC);
4852 >        final DelegatedCompletionStage<Integer> d =
4853 >            new DelegatedCompletionStage<Integer>(f);
4854 >        if (!createIncomplete) assertTrue(f.complete(v1));
4855 >        final CompletionStage<Integer> g = d.exceptionallyCompose(r);
4856 >        if (createIncomplete) assertTrue(f.complete(v1));
4857  
4858 <    /**
4859 <     * whenComplete action executes on exceptional completion, propagating
4860 <     * source result.
4861 <     */
3163 <    public void testWhenComplete2() {
3164 <        final AtomicInteger a = new AtomicInteger();
3165 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3166 <        CompletableFuture<Integer> g =
3167 <            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3168 <        f.completeExceptionally(new CFException());
3169 <        assertTrue(f.isCompletedExceptionally());
3170 <        assertTrue(g.isCompletedExceptionally());
3171 <        assertEquals(a.get(), 1);
3172 <    }
4858 >        checkCompletedNormally(f, v1);
4859 >        checkCompletedNormally(g.toCompletableFuture(), v1);
4860 >        r.assertNotInvoked();
4861 >    }}
4862  
4863      /**
4864 <     * If a whenComplete action throws an exception when triggered by
4865 <     * a normal completion, it completes exceptionally
4864 >     * default-implemented exceptionallyCompose result completes
4865 >     * normally after exceptional completion of source
4866       */
4867 <    public void testWhenComplete3() {
4868 <        CompletableFuture<Integer> f = new CompletableFuture<>();
4869 <        CompletableFuture<Integer> g =
4870 <            f.whenComplete((Integer x, Throwable t) ->
4871 <                           { throw new CFException(); } );
4872 <        f.complete(three);
4873 <        checkCompletedNormally(f, three);
4874 <        assertTrue(g.isCompletedExceptionally());
4875 <        checkCompletedWithWrappedCFException(g);
4876 <    }
4867 >    public void testDefaultExceptionallyCompose_exceptionalCompletion() {
4868 >        for (boolean createIncomplete : new boolean[] { true, false })
4869 >    {
4870 >        final CFException ex = new CFException();
4871 >        final ExceptionalCompletableFutureFunction r =
4872 >            new ExceptionalCompletableFutureFunction(ExecutionMode.SYNC);
4873 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
4874 >        final DelegatedCompletionStage<Integer> d =
4875 >            new DelegatedCompletionStage<Integer>(f);
4876 >        if (!createIncomplete) f.completeExceptionally(ex);
4877 >        final CompletionStage<Integer> g = d.exceptionallyCompose(r);
4878 >        if (createIncomplete) f.completeExceptionally(ex);
4879 >
4880 >        checkCompletedExceptionally(f, ex);
4881 >        checkCompletedNormally(g.toCompletableFuture(), r.value);
4882 >        r.assertInvoked();
4883 >    }}
4884  
4885      /**
4886 <     * whenCompleteAsync action executes on normal completion, propagating
4887 <     * source result.
4886 >     * default-implemented exceptionallyCompose completes
4887 >     * exceptionally on exception if action does
4888       */
4889 <    public void testWhenCompleteAsync1() {
4890 <        final AtomicInteger a = new AtomicInteger();
4891 <        CompletableFuture<Integer> f = new CompletableFuture<>();
4892 <        CompletableFuture<Integer> g =
4893 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
4894 <        f.complete(three);
4895 <        checkCompletedNormally(f, three);
4896 <        checkCompletedNormally(g, three);
4897 <        assertEquals(a.get(), 1);
4898 <    }
4889 >    public void testDefaultExceptionallyCompose_actionFailed() {
4890 >        for (boolean createIncomplete : new boolean[] { true, false })
4891 >        for (Integer v1 : new Integer[] { 1, null })
4892 >    {
4893 >        final CFException ex = new CFException();
4894 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
4895 >        final FailingExceptionalCompletableFutureFunction r
4896 >            = new FailingExceptionalCompletableFutureFunction(ExecutionMode.SYNC);
4897 >        final DelegatedCompletionStage<Integer> d =
4898 >            new DelegatedCompletionStage<Integer>(f);
4899 >        if (!createIncomplete) f.completeExceptionally(ex);
4900 >        final CompletionStage<Integer> g = d.exceptionallyCompose(r);
4901 >        if (createIncomplete) f.completeExceptionally(ex);
4902 >
4903 >        checkCompletedExceptionally(f, ex);
4904 >        checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex);
4905 >        r.assertInvoked();
4906 >    }}
4907  
4908      /**
4909 <     * whenCompleteAsync action executes on exceptional completion, propagating
4910 <     * source result.
4909 >     * default exceptionallyComposeAsync result completes normally after normal
4910 >     * completion of source
4911       */
4912 <    public void testWhenCompleteAsync2() {
4913 <        final AtomicInteger a = new AtomicInteger();
4914 <        CompletableFuture<Integer> f = new CompletableFuture<>();
4915 <        CompletableFuture<Integer> g =
4916 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
4917 <        f.completeExceptionally(new CFException());
4918 <        checkCompletedWithWrappedCFException(f);
4919 <        checkCompletedWithWrappedCFException(g);
4920 <    }
4912 >    public void testDefaultExceptionallyComposeAsync_normalCompletion() {
4913 >        for (boolean createIncomplete : new boolean[] { true, false })
4914 >        for (Integer v1 : new Integer[] { 1, null })
4915 >    {
4916 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
4917 >        final ExceptionalCompletableFutureFunction r =
4918 >            new ExceptionalCompletableFutureFunction(ExecutionMode.ASYNC);
4919 >        final DelegatedCompletionStage<Integer> d =
4920 >            new DelegatedCompletionStage<Integer>(f);
4921 >        if (!createIncomplete) assertTrue(f.complete(v1));
4922 >        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r);
4923 >        if (createIncomplete) assertTrue(f.complete(v1));
4924  
4925 <    /**
4926 <     * If a whenCompleteAsync action throws an exception when
4927 <     * triggered by a normal completion, it completes exceptionally
4928 <     */
3222 <    public void testWhenCompleteAsync3() {
3223 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3224 <        CompletableFuture<Integer> g =
3225 <            f.whenCompleteAsync((Integer x, Throwable t) ->
3226 <                           { throw new CFException(); } );
3227 <        f.complete(three);
3228 <        checkCompletedNormally(f, three);
3229 <        checkCompletedWithWrappedCFException(g);
3230 <    }
4925 >        checkCompletedNormally(f, v1);
4926 >        checkCompletedNormally(g.toCompletableFuture(), v1);
4927 >        r.assertNotInvoked();
4928 >    }}
4929  
4930      /**
4931 <     * whenCompleteAsync action executes on normal completion, propagating
4932 <     * source result.
4931 >     * default-implemented exceptionallyComposeAsync result completes
4932 >     * normally after exceptional completion of source
4933       */
4934 <    public void testWhenCompleteAsync1e() {
4935 <        final AtomicInteger a = new AtomicInteger();
4936 <        ThreadExecutor exec = new ThreadExecutor();
4937 <        CompletableFuture<Integer> f = new CompletableFuture<>();
4938 <        CompletableFuture<Integer> g =
4939 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
4940 <                                exec);
4941 <        f.complete(three);
4942 <        checkCompletedNormally(f, three);
4943 <        checkCompletedNormally(g, three);
4944 <        assertEquals(a.get(), 1);
4945 <    }
4934 >    public void testDefaultExceptionallyComposeAsync_exceptionalCompletion() {
4935 >        for (boolean createIncomplete : new boolean[] { true, false })
4936 >    {
4937 >        final CFException ex = new CFException();
4938 >        final ExceptionalCompletableFutureFunction r =
4939 >            new ExceptionalCompletableFutureFunction(ExecutionMode.ASYNC);
4940 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
4941 >        final DelegatedCompletionStage<Integer> d =
4942 >            new DelegatedCompletionStage<Integer>(f);
4943 >        if (!createIncomplete) f.completeExceptionally(ex);
4944 >        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r);
4945 >        if (createIncomplete) f.completeExceptionally(ex);
4946 >
4947 >        checkCompletedExceptionally(f, ex);
4948 >        checkCompletedNormally(g.toCompletableFuture(), r.value);
4949 >        r.assertInvoked();
4950 >    }}
4951  
4952      /**
4953 <     * whenCompleteAsync action executes on exceptional completion, propagating
4954 <     * source result.
4953 >     * default-implemented exceptionallyComposeAsync completes
4954 >     * exceptionally on exception if action does
4955       */
4956 <    public void testWhenCompleteAsync2e() {
4957 <        final AtomicInteger a = new AtomicInteger();
4958 <        ThreadExecutor exec = new ThreadExecutor();
4959 <        CompletableFuture<Integer> f = new CompletableFuture<>();
4960 <        CompletableFuture<Integer> g =
4961 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
4962 <                                exec);
4963 <        f.completeExceptionally(new CFException());
4964 <        checkCompletedWithWrappedCFException(f);
4965 <        checkCompletedWithWrappedCFException(g);
4966 <    }
4956 >    public void testDefaultExceptionallyComposeAsync_actionFailed() {
4957 >        for (boolean createIncomplete : new boolean[] { true, false })
4958 >        for (Integer v1 : new Integer[] { 1, null })
4959 >    {
4960 >        final CFException ex = new CFException();
4961 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
4962 >        final FailingExceptionalCompletableFutureFunction r
4963 >            = new FailingExceptionalCompletableFutureFunction(ExecutionMode.ASYNC);
4964 >        final DelegatedCompletionStage<Integer> d =
4965 >            new DelegatedCompletionStage<Integer>(f);
4966 >        if (!createIncomplete) f.completeExceptionally(ex);
4967 >        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r);
4968 >        if (createIncomplete) f.completeExceptionally(ex);
4969 >
4970 >        checkCompletedExceptionally(f, ex);
4971 >        checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex);
4972 >        r.assertInvoked();
4973 >    }}
4974  
3265    /**
3266     * If a whenCompleteAsync action throws an exception when triggered
3267     * by a normal completion, it completes exceptionally
3268     */
3269    public void testWhenCompleteAsync3e() {
3270        ThreadExecutor exec = new ThreadExecutor();
3271        CompletableFuture<Integer> f = new CompletableFuture<>();
3272        CompletableFuture<Integer> g =
3273            f.whenCompleteAsync((Integer x, Throwable t) ->
3274                                { throw new CFException(); },
3275                                exec);
3276        f.complete(three);
3277        checkCompletedNormally(f, three);
3278        checkCompletedWithWrappedCFException(g);
3279    }
4975  
4976      /**
4977 <     * handleAsync action completes normally with function value on
4978 <     * either normal or exceptional completion of source
4977 >     * default exceptionallyComposeAsync result completes normally after normal
4978 >     * completion of source
4979       */
4980 <    public void testHandleAsync() {
4981 <        CompletableFuture<Integer> f, g;
4982 <        IntegerHandler r;
4983 <
4984 <        f = new CompletableFuture<>();
4985 <        g = f.handleAsync(r = new IntegerHandler());
4986 <        assertFalse(r.ran);
4987 <        f.completeExceptionally(new CFException());
4988 <        checkCompletedWithWrappedCFException(f);
4989 <        checkCompletedNormally(g, three);
4990 <        assertTrue(r.ran);
4991 <
3297 <        f = new CompletableFuture<>();
3298 <        g = f.handleAsync(r = new IntegerHandler());
3299 <        assertFalse(r.ran);
3300 <        f.completeExceptionally(new CFException());
3301 <        checkCompletedWithWrappedCFException(f);
3302 <        checkCompletedNormally(g, three);
3303 <        assertTrue(r.ran);
3304 <
3305 <        f = new CompletableFuture<>();
3306 <        g = f.handleAsync(r = new IntegerHandler());
3307 <        assertFalse(r.ran);
3308 <        f.complete(one);
3309 <        checkCompletedNormally(f, one);
3310 <        checkCompletedNormally(g, two);
3311 <        assertTrue(r.ran);
4980 >    public void testDefaultExceptionallyComposeAsyncExecutor_normalCompletion() {
4981 >        for (boolean createIncomplete : new boolean[] { true, false })
4982 >        for (Integer v1 : new Integer[] { 1, null })
4983 >    {
4984 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
4985 >        final ExceptionalCompletableFutureFunction r =
4986 >            new ExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR);
4987 >        final DelegatedCompletionStage<Integer> d =
4988 >            new DelegatedCompletionStage<Integer>(f);
4989 >        if (!createIncomplete) assertTrue(f.complete(v1));
4990 >        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r,  new ThreadExecutor());
4991 >        if (createIncomplete) assertTrue(f.complete(v1));
4992  
4993 <        f = new CompletableFuture<>();
4994 <        g = f.handleAsync(r = new IntegerHandler());
4995 <        assertFalse(r.ran);
4996 <        f.complete(one);
3317 <        checkCompletedNormally(f, one);
3318 <        checkCompletedNormally(g, two);
3319 <        assertTrue(r.ran);
3320 <    }
4993 >        checkCompletedNormally(f, v1);
4994 >        checkCompletedNormally(g.toCompletableFuture(), v1);
4995 >        r.assertNotInvoked();
4996 >    }}
4997  
4998      /**
4999 <     * handleAsync action with Executor completes normally with
5000 <     * function value on either normal or exceptional completion of
3325 <     * source
4999 >     * default-implemented exceptionallyComposeAsync result completes
5000 >     * normally after exceptional completion of source
5001       */
5002 <    public void testHandleAsync2() {
5003 <        CompletableFuture<Integer> f, g;
5004 <        ThreadExecutor exec = new ThreadExecutor();
5005 <        IntegerHandler r;
5006 <
5007 <        f = new CompletableFuture<>();
5008 <        g = f.handleAsync(r = new IntegerHandler(), exec);
5009 <        assertFalse(r.ran);
5010 <        f.completeExceptionally(new CFException());
5011 <        checkCompletedWithWrappedCFException(f);
5012 <        checkCompletedNormally(g, three);
5013 <        assertTrue(r.ran);
5014 <
5015 <        f = new CompletableFuture<>();
5016 <        g = f.handleAsync(r = new IntegerHandler(), exec);
5017 <        assertFalse(r.ran);
5018 <        f.completeExceptionally(new CFException());
3344 <        checkCompletedWithWrappedCFException(f);
3345 <        checkCompletedNormally(g, three);
3346 <        assertTrue(r.ran);
3347 <
3348 <        f = new CompletableFuture<>();
3349 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3350 <        assertFalse(r.ran);
3351 <        f.complete(one);
3352 <        checkCompletedNormally(f, one);
3353 <        checkCompletedNormally(g, two);
3354 <        assertTrue(r.ran);
5002 >    public void testDefaultExceptionallyComposeAsyncExecutor_exceptionalCompletion() {
5003 >        for (boolean createIncomplete : new boolean[] { true, false })
5004 >    {
5005 >        final CFException ex = new CFException();
5006 >        final ExceptionalCompletableFutureFunction r =
5007 >            new ExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR);
5008 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
5009 >        final DelegatedCompletionStage<Integer> d =
5010 >            new DelegatedCompletionStage<Integer>(f);
5011 >        if (!createIncomplete) f.completeExceptionally(ex);
5012 >        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r,  new ThreadExecutor());
5013 >        if (createIncomplete) f.completeExceptionally(ex);
5014 >
5015 >        checkCompletedExceptionally(f, ex);
5016 >        checkCompletedNormally(g.toCompletableFuture(), r.value);
5017 >        r.assertInvoked();
5018 >    }}
5019  
5020 <        f = new CompletableFuture<>();
5021 <        g = f.handleAsync(r = new IntegerHandler(), exec);
5022 <        assertFalse(r.ran);
5023 <        f.complete(one);
5024 <        checkCompletedNormally(f, one);
5025 <        checkCompletedNormally(g, two);
5026 <        assertTrue(r.ran);
5027 <    }
5020 >    /**
5021 >     * default-implemented exceptionallyComposeAsync completes
5022 >     * exceptionally on exception if action does
5023 >     */
5024 >    public void testDefaultExceptionallyComposeAsyncExecutor_actionFailed() {
5025 >        for (boolean createIncomplete : new boolean[] { true, false })
5026 >        for (Integer v1 : new Integer[] { 1, null })
5027 >    {
5028 >        final CFException ex = new CFException();
5029 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
5030 >        final FailingExceptionalCompletableFutureFunction r
5031 >            = new FailingExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR);
5032 >        final DelegatedCompletionStage<Integer> d =
5033 >            new DelegatedCompletionStage<Integer>(f);
5034 >        if (!createIncomplete) f.completeExceptionally(ex);
5035 >        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r,  new ThreadExecutor());
5036 >        if (createIncomplete) f.completeExceptionally(ex);
5037 >
5038 >        checkCompletedExceptionally(f, ex);
5039 >        checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex);
5040 >        r.assertInvoked();
5041 >    }}
5042  
5043   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines