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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines