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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines