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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines