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.33 by jsr166, Sun Jun 1 20:40:13 2014 UTC vs.
Revision 1.188 by jsr166, Wed Aug 16 17:18:34 2017 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines