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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines