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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines