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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines