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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines