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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines