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

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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines