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.26 by dl, Fri Jul 5 15:47:52 2013 UTC vs.
Revision 1.159 by jsr166, Mon Jun 27 21:39:37 2016 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines