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.1 by jsr166, Wed Feb 6 19:55:06 2013 UTC vs.
Revision 1.34 by jsr166, Sun Jun 1 21:17:05 2014 UTC

# Line 1 | Line 1
1   /*
2 < * Written by Doug Lea with assistance from members of JCP JSR-166
3 < * Expert Group and released to the public domain, as explained at
2 > * Written by Doug Lea and Martin Buchholz with assistance from
3 > * members of JCP JSR-166 Expert Group and released to the public
4 > * domain, as explained at
5   * http://creativecommons.org/publicdomain/zero/1.0/
5 * Other contributors include Andrew Wright, Jeffrey Hayes,
6 * Pat Fisher, Mike Judd.
6   */
7  
8   import junit.framework.*;
9   import java.util.concurrent.Callable;
10 + import java.util.concurrent.Executor;
11 + import java.util.concurrent.ExecutorService;
12 + import java.util.concurrent.Executors;
13   import java.util.concurrent.CancellationException;
14   import java.util.concurrent.CountDownLatch;
15   import java.util.concurrent.ExecutionException;
16   import java.util.concurrent.Future;
17   import java.util.concurrent.CompletableFuture;
18 + import java.util.concurrent.CompletionException;
19   import java.util.concurrent.TimeoutException;
20   import java.util.concurrent.atomic.AtomicInteger;
21   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;
26 + import java.util.function.BiConsumer;
27 + import java.util.function.Function;
28 + import java.util.function.BiFunction;
29  
30   public class CompletableFutureTest extends JSR166TestCase {
31  
# Line 28 | Line 36 | public class CompletableFutureTest exten
36          return new TestSuite(CompletableFutureTest.class);
37      }
38  
39 +    static class CFException extends RuntimeException {}
40 +
41 +    void checkIncomplete(CompletableFuture<?> f) {
42 +        assertFalse(f.isDone());
43 +        assertFalse(f.isCancelled());
44 +        assertTrue(f.toString().contains("[Not completed]"));
45 +        try {
46 +            assertNull(f.getNow(null));
47 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
48 +        try {
49 +            f.get(0L, SECONDS);
50 +            shouldThrow();
51 +        }
52 +        catch (TimeoutException success) {}
53 +        catch (Throwable fail) { threadUnexpectedException(fail); }
54 +    }
55 +
56 +    <T> void checkCompletedNormally(CompletableFuture<T> f, T value) {
57 +        try {
58 +            assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS));
59 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
60 +        try {
61 +            assertEquals(value, f.join());
62 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
63 +        try {
64 +            assertEquals(value, f.getNow(null));
65 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
66 +        try {
67 +            assertEquals(value, f.get());
68 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
69 +        assertTrue(f.isDone());
70 +        assertFalse(f.isCancelled());
71 +        assertFalse(f.isCompletedExceptionally());
72 +        assertTrue(f.toString().contains("[Completed normally]"));
73 +    }
74 +
75 +    void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
76 +        try {
77 +            f.get(LONG_DELAY_MS, MILLISECONDS);
78 +            shouldThrow();
79 +        } catch (ExecutionException success) {
80 +            assertTrue(success.getCause() instanceof CFException);
81 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
82 +        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]"));
103 +    }
104 +
105 +    void checkCompletedWithWrappedCFException(CompletableFuture<?> f,
106 +                                              CFException ex) {
107 +        try {
108 +            f.get(LONG_DELAY_MS, MILLISECONDS);
109 +            shouldThrow();
110 +        } catch (ExecutionException success) {
111 +            assertSame(ex, success.getCause());
112 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
113 +        try {
114 +            f.join();
115 +            shouldThrow();
116 +        } catch (CompletionException success) {
117 +            assertSame(ex, success.getCause());
118 +        }
119 +        try {
120 +            f.getNow(null);
121 +            shouldThrow();
122 +        } catch (CompletionException success) {
123 +            assertSame(ex, success.getCause());
124 +        }
125 +        try {
126 +            f.get();
127 +            shouldThrow();
128 +        } catch (ExecutionException success) {
129 +            assertSame(ex, success.getCause());
130 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
131 +        assertTrue(f.isDone());
132 +        assertFalse(f.isCancelled());
133 +        assertTrue(f.toString().contains("[Completed exceptionally]"));
134 +    }
135 +
136 +    void checkCancelled(CompletableFuture<?> f) {
137 +        try {
138 +            f.get(LONG_DELAY_MS, MILLISECONDS);
139 +            shouldThrow();
140 +        } catch (CancellationException success) {
141 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
142 +        try {
143 +            f.join();
144 +            shouldThrow();
145 +        } catch (CancellationException success) {}
146 +        try {
147 +            f.getNow(null);
148 +            shouldThrow();
149 +        } catch (CancellationException success) {}
150 +        try {
151 +            f.get();
152 +            shouldThrow();
153 +        } catch (CancellationException success) {
154 +        } 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 +    }
160 +
161 +    void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
162 +        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); }
186 +        assertTrue(f.isDone());
187 +        assertFalse(f.isCancelled());
188 +        assertTrue(f.isCompletedExceptionally());
189 +        assertTrue(f.toString().contains("[Completed exceptionally]"));
190 +    }
191 +
192 +    /**
193 +     * A newly constructed CompletableFuture is incomplete, as indicated
194 +     * by methods isDone, isCancelled, and getNow
195 +     */
196 +    public void testConstructor() {
197 +        CompletableFuture<Integer> f = new CompletableFuture<>();
198 +        checkIncomplete(f);
199 +    }
200 +
201 +    /**
202 +     * complete completes normally, as indicated by methods isDone,
203 +     * isCancelled, join, get, and getNow
204 +     */
205 +    public void testComplete() {
206 +        CompletableFuture<Integer> f = new CompletableFuture<>();
207 +        checkIncomplete(f);
208 +        f.complete(one);
209 +        checkCompletedNormally(f, one);
210 +    }
211 +
212 +    /**
213 +     * completeExceptionally completes exceptionally, as indicated by
214 +     * methods isDone, isCancelled, join, get, and getNow
215 +     */
216 +    public void testCompleteExceptionally() {
217 +        CompletableFuture<Integer> f = new CompletableFuture<>();
218 +        checkIncomplete(f);
219 +        f.completeExceptionally(new CFException());
220 +        checkCompletedWithWrappedCFException(f);
221 +    }
222 +
223 +    /**
224 +     * cancel completes exceptionally and reports cancelled, as indicated by
225 +     * methods isDone, isCancelled, join, get, and getNow
226 +     */
227 +    public void testCancel() {
228 +        CompletableFuture<Integer> f = new CompletableFuture<>();
229 +        checkIncomplete(f);
230 +        assertTrue(f.cancel(true));
231 +        checkCancelled(f);
232 +    }
233 +
234 +    /**
235 +     * obtrudeValue forces completion with given value
236 +     */
237 +    public void testObtrudeValue() {
238 +        CompletableFuture<Integer> f = new CompletableFuture<>();
239 +        checkIncomplete(f);
240 +        f.complete(one);
241 +        checkCompletedNormally(f, one);
242 +        f.obtrudeValue(three);
243 +        checkCompletedNormally(f, three);
244 +        f.obtrudeValue(two);
245 +        checkCompletedNormally(f, two);
246 +        f = new CompletableFuture<>();
247 +        f.obtrudeValue(three);
248 +        checkCompletedNormally(f, three);
249 +        f = new CompletableFuture<>();
250 +        f.completeExceptionally(new CFException());
251 +        f.obtrudeValue(four);
252 +        checkCompletedNormally(f, four);
253 +    }
254 +
255 +    /**
256 +     * obtrudeException forces completion with given exception
257 +     */
258 +    public void testObtrudeException() {
259 +        CompletableFuture<Integer> f = new CompletableFuture<>();
260 +        checkIncomplete(f);
261 +        f.complete(one);
262 +        checkCompletedNormally(f, one);
263 +        f.obtrudeException(new CFException());
264 +        checkCompletedWithWrappedCFException(f);
265 +        f = new CompletableFuture<>();
266 +        f.obtrudeException(new CFException());
267 +        checkCompletedWithWrappedCFException(f);
268 +        f = new CompletableFuture<>();
269 +        f.completeExceptionally(new CFException());
270 +        f.obtrudeValue(four);
271 +        checkCompletedNormally(f, four);
272 +        f.obtrudeException(new CFException());
273 +        checkCompletedWithWrappedCFException(f);
274 +    }
275 +
276 +    /**
277 +     * getNumberOfDependents returns number of dependent tasks
278 +     */
279 +    public void testGetNumberOfDependents() {
280 +        CompletableFuture<Integer> f = new CompletableFuture<>();
281 +        assertEquals(f.getNumberOfDependents(), 0);
282 +        CompletableFuture g = f.thenRun(new Noop());
283 +        assertEquals(f.getNumberOfDependents(), 1);
284 +        assertEquals(g.getNumberOfDependents(), 0);
285 +        CompletableFuture h = f.thenRun(new Noop());
286 +        assertEquals(f.getNumberOfDependents(), 2);
287 +        f.complete(1);
288 +        checkCompletedNormally(g, null);
289 +        assertEquals(f.getNumberOfDependents(), 0);
290 +        assertEquals(g.getNumberOfDependents(), 0);
291 +    }
292 +
293 +    /**
294 +     * toString indicates current completion state
295 +     */
296      public void testToString() {
297          CompletableFuture<String> f;
298 <        assertTrue(new CompletableFuture<String>().toString()
34 <                   .contains("[Not completed]"));
298 >
299          f = new CompletableFuture<String>();
300 +        assertTrue(f.toString().contains("[Not completed]"));
301 +
302          f.complete("foo");
303          assertTrue(f.toString().contains("[Completed normally]"));
304 +
305          f = new CompletableFuture<String>();
306          f.completeExceptionally(new IndexOutOfBoundsException());
307          assertTrue(f.toString().contains("[Completed exceptionally]"));
308      }
309 +
310 +    /**
311 +     * completedFuture returns a completed CompletableFuture with given value
312 +     */
313 +    public void testCompletedFuture() {
314 +        CompletableFuture<String> f = CompletableFuture.completedFuture("test");
315 +        checkCompletedNormally(f, "test");
316 +    }
317 +
318 +    // Choose non-commutative actions for better coverage
319 +
320 +    static final Supplier<Integer> supplyOne =
321 +        () -> Integer.valueOf(1);
322 +    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; }
329 +    }
330 +    static final class SubtractAction implements BiConsumer<Integer, Integer> {
331 +        int value;
332 +        public void accept(Integer x, Integer y) {
333 +            value = x.intValue() - y.intValue();
334 +        }
335 +    }
336 +    static final class Noop implements Runnable {
337 +        boolean ran;
338 +        public void run() { ran = true; }
339 +    }
340 +
341 +    static final class FailingSupplier implements Supplier<Integer> {
342 +        boolean ran;
343 +        public Integer get() { ran = true; throw new CFException(); }
344 +    }
345 +    static final class FailingConsumer implements Consumer<Integer> {
346 +        boolean ran;
347 +        public void accept(Integer x) { ran = true; throw new CFException(); }
348 +    }
349 +    static final class FailingBiConsumer implements BiConsumer<Integer, Integer> {
350 +        boolean ran;
351 +        public void accept(Integer x, Integer y) { ran = true; throw new CFException(); }
352 +    }
353 +    static final class FailingFunction implements Function<Integer, Integer> {
354 +        boolean ran;
355 +        public Integer apply(Integer x) { ran = true; throw new CFException(); }
356 +    }
357 +    static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> {
358 +        boolean ran;
359 +        public Integer apply(Integer x, Integer y) { ran = true; throw new CFException(); }
360 +    }
361 +    static final class FailingNoop implements Runnable {
362 +        boolean ran;
363 +        public void run() { ran = true; throw new CFException(); }
364 +    }
365 +
366 +    static final class CompletableFutureInc
367 +        implements Function<Integer, CompletableFuture<Integer>> {
368 +        boolean ran;
369 +        public CompletableFuture<Integer> apply(Integer x) {
370 +            ran = true;
371 +            CompletableFuture<Integer> f = new CompletableFuture<>();
372 +            f.complete(Integer.valueOf(x.intValue() + 1));
373 +            return f;
374 +        }
375 +    }
376 +
377 +    static final class FailingCompletableFutureFunction
378 +        implements Function<Integer, CompletableFuture<Integer>> {
379 +        boolean ran;
380 +        public CompletableFuture<Integer> apply(Integer x) {
381 +            ran = true; throw new CFException();
382 +        }
383 +    }
384 +
385 +    // Used for explicit executor tests
386 +    static final class ThreadExecutor implements Executor {
387 +        AtomicInteger count = new AtomicInteger(0);
388 +
389 +        public void execute(Runnable r) {
390 +            count.getAndIncrement();
391 +            new Thread(r).start();
392 +        }
393 +    }
394 +
395 +    static final class ExceptionToInteger implements Function<Throwable, Integer> {
396 +        public Integer apply(Throwable x) { return Integer.valueOf(3); }
397 +    }
398 +
399 +    static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> {
400 +        boolean ran;
401 +        public Integer apply(Integer x, Throwable t) {
402 +            ran = true;
403 +            return (t == null) ? two : three;
404 +        }
405 +    }
406 +
407 +    /**
408 +     * exceptionally action completes with function value on source
409 +     * exception; otherwise with source value
410 +     */
411 +    public void testExceptionally() {
412 +        CompletableFuture<Integer> f = new CompletableFuture<>();
413 +        ExceptionToInteger r = new ExceptionToInteger();
414 +        CompletableFuture<Integer> g = f.exceptionally(r);
415 +        f.completeExceptionally(new CFException());
416 +        checkCompletedNormally(g, three);
417 +
418 +        f = new CompletableFuture<>();
419 +        r = new ExceptionToInteger();
420 +        g = f.exceptionally(r);
421 +        f.complete(one);
422 +        checkCompletedNormally(g, one);
423 +    }
424 +
425 +    /**
426 +     * handle action completes normally with function value on either
427 +     * normal or exceptional completion of source
428 +     */
429 +    public void testHandle() {
430 +        CompletableFuture<Integer> f, g;
431 +        IntegerHandler r;
432 +
433 +        f = new CompletableFuture<>();
434 +        f.completeExceptionally(new CFException());
435 +        g = f.handle(r = new IntegerHandler());
436 +        assertTrue(r.ran);
437 +        checkCompletedNormally(g, three);
438 +
439 +        f = new CompletableFuture<>();
440 +        g = f.handle(r = new IntegerHandler());
441 +        assertFalse(r.ran);
442 +        f.completeExceptionally(new CFException());
443 +        checkCompletedNormally(g, three);
444 +        assertTrue(r.ran);
445 +
446 +        f = new CompletableFuture<>();
447 +        f.complete(one);
448 +        g = f.handle(r = new IntegerHandler());
449 +        assertTrue(r.ran);
450 +        checkCompletedNormally(g, two);
451 +
452 +        f = new CompletableFuture<>();
453 +        g = f.handle(r = new IntegerHandler());
454 +        assertFalse(r.ran);
455 +        f.complete(one);
456 +        assertTrue(r.ran);
457 +        checkCompletedNormally(g, two);
458 +    }
459 +
460 +    /**
461 +     * runAsync completes after running Runnable
462 +     */
463 +    public void testRunAsync() {
464 +        Noop r = new Noop();
465 +        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
466 +        assertNull(f.join());
467 +        assertTrue(r.ran);
468 +        checkCompletedNormally(f, null);
469 +    }
470 +
471 +    /**
472 +     * runAsync with executor completes after running Runnable
473 +     */
474 +    public void testRunAsync2() {
475 +        Noop r = new Noop();
476 +        ThreadExecutor exec = new ThreadExecutor();
477 +        CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec);
478 +        assertNull(f.join());
479 +        assertTrue(r.ran);
480 +        checkCompletedNormally(f, null);
481 +        assertEquals(1, exec.count.get());
482 +    }
483 +
484 +    /**
485 +     * failing runAsync completes exceptionally after running Runnable
486 +     */
487 +    public void testRunAsync3() {
488 +        FailingNoop r = new FailingNoop();
489 +        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
490 +        checkCompletedWithWrappedCFException(f);
491 +        assertTrue(r.ran);
492 +    }
493 +
494 +    /**
495 +     * supplyAsync completes with result of supplier
496 +     */
497 +    public void testSupplyAsync() {
498 +        CompletableFuture<Integer> f;
499 +        f = CompletableFuture.supplyAsync(supplyOne);
500 +        assertEquals(f.join(), one);
501 +        checkCompletedNormally(f, one);
502 +    }
503 +
504 +    /**
505 +     * supplyAsync with executor completes with result of supplier
506 +     */
507 +    public void testSupplyAsync2() {
508 +        CompletableFuture<Integer> f;
509 +        f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor());
510 +        assertEquals(f.join(), one);
511 +        checkCompletedNormally(f, one);
512 +    }
513 +
514 +    /**
515 +     * Failing supplyAsync completes exceptionally
516 +     */
517 +    public void testSupplyAsync3() {
518 +        FailingSupplier r = new FailingSupplier();
519 +        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r);
520 +        checkCompletedWithWrappedCFException(f);
521 +        assertTrue(r.ran);
522 +    }
523 +
524 +    // seq completion methods
525 +
526 +    /**
527 +     * thenRun result completes normally after normal completion of source
528 +     */
529 +    public void testThenRun() {
530 +        CompletableFuture<Integer> f;
531 +        CompletableFuture<Void> g;
532 +        Noop r;
533 +
534 +        f = new CompletableFuture<>();
535 +        g = f.thenRun(r = new Noop());
536 +        f.complete(null);
537 +        checkCompletedNormally(g, null);
538 +        assertTrue(r.ran);
539 +
540 +        f = new CompletableFuture<>();
541 +        f.complete(null);
542 +        g = f.thenRun(r = new Noop());
543 +        checkCompletedNormally(g, null);
544 +        assertTrue(r.ran);
545 +    }
546 +
547 +    /**
548 +     * thenRun result completes exceptionally after exceptional
549 +     * completion of source
550 +     */
551 +    public void testThenRun2() {
552 +        CompletableFuture<Integer> f;
553 +        CompletableFuture<Void> g;
554 +        Noop r;
555 +
556 +        f = new CompletableFuture<>();
557 +        g = f.thenRun(r = new Noop());
558 +        f.completeExceptionally(new CFException());
559 +        checkCompletedWithWrappedCFException(g);
560 +        assertFalse(r.ran);
561 +
562 +        f = new CompletableFuture<>();
563 +        f.completeExceptionally(new CFException());
564 +        g = f.thenRun(r = new Noop());
565 +        checkCompletedWithWrappedCFException(g);
566 +        assertFalse(r.ran);
567 +    }
568 +
569 +    /**
570 +     * thenRun result completes exceptionally if action does
571 +     */
572 +    public void testThenRun3() {
573 +        CompletableFuture<Integer> f;
574 +        CompletableFuture<Void> g;
575 +        FailingNoop r;
576 +
577 +        f = new CompletableFuture<>();
578 +        g = f.thenRun(r = new FailingNoop());
579 +        f.complete(null);
580 +        checkCompletedWithWrappedCFException(g);
581 +
582 +        f = new CompletableFuture<>();
583 +        f.complete(null);
584 +        g = f.thenRun(r = new FailingNoop());
585 +        checkCompletedWithWrappedCFException(g);
586 +    }
587 +
588 +    /**
589 +     * thenRun result completes exceptionally if source cancelled
590 +     */
591 +    public void testThenRun4() {
592 +        CompletableFuture<Integer> f;
593 +        CompletableFuture<Void> g;
594 +        Noop r;
595 +
596 +        f = new CompletableFuture<>();
597 +        g = f.thenRun(r = new Noop());
598 +        assertTrue(f.cancel(true));
599 +        checkCompletedWithWrappedCancellationException(g);
600 +
601 +        f = new CompletableFuture<>();
602 +        assertTrue(f.cancel(true));
603 +        g = f.thenRun(r = new Noop());
604 +        checkCompletedWithWrappedCancellationException(g);
605 +    }
606 +
607 +    /**
608 +     * thenApply result completes normally after normal completion of source
609 +     */
610 +    public void testThenApply() {
611 +        CompletableFuture<Integer> f = new CompletableFuture<>();
612 +        CompletableFuture<Integer> g = f.thenApply(inc);
613 +        f.complete(one);
614 +        checkCompletedNormally(g, two);
615 +    }
616 +
617 +    /**
618 +     * thenApply result completes exceptionally after exceptional
619 +     * completion of source
620 +     */
621 +    public void testThenApply2() {
622 +        CompletableFuture<Integer> f = new CompletableFuture<>();
623 +        CompletableFuture<Integer> g = f.thenApply(inc);
624 +        f.completeExceptionally(new CFException());
625 +        checkCompletedWithWrappedCFException(g);
626 +    }
627 +
628 +    /**
629 +     * thenApply result completes exceptionally if action does
630 +     */
631 +    public void testThenApply3() {
632 +        CompletableFuture<Integer> f = new CompletableFuture<>();
633 +        CompletableFuture<Integer> g = f.thenApply(new FailingFunction());
634 +        f.complete(one);
635 +        checkCompletedWithWrappedCFException(g);
636 +    }
637 +
638 +    /**
639 +     * thenApply result completes exceptionally if source cancelled
640 +     */
641 +    public void testThenApply4() {
642 +        CompletableFuture<Integer> f = new CompletableFuture<>();
643 +        CompletableFuture<Integer> g = f.thenApply(inc);
644 +        assertTrue(f.cancel(true));
645 +        checkCompletedWithWrappedCancellationException(g);
646 +    }
647 +
648 +    /**
649 +     * thenAccept result completes normally after normal completion of source
650 +     */
651 +    public void testThenAccept() {
652 +        CompletableFuture<Integer> f = new CompletableFuture<>();
653 +        IncAction r = new IncAction();
654 +        CompletableFuture<Void> g = f.thenAccept(r);
655 +        f.complete(one);
656 +        checkCompletedNormally(g, null);
657 +        assertEquals(r.value, 2);
658 +    }
659 +
660 +    /**
661 +     * thenAccept result completes exceptionally after exceptional
662 +     * completion of source
663 +     */
664 +    public void testThenAccept2() {
665 +        CompletableFuture<Integer> f = new CompletableFuture<>();
666 +        IncAction r = new IncAction();
667 +        CompletableFuture<Void> g = f.thenAccept(r);
668 +        f.completeExceptionally(new CFException());
669 +        checkCompletedWithWrappedCFException(g);
670 +    }
671 +
672 +    /**
673 +     * thenAccept result completes exceptionally if action does
674 +     */
675 +    public void testThenAccept3() {
676 +        CompletableFuture<Integer> f = new CompletableFuture<>();
677 +        FailingConsumer r = new FailingConsumer();
678 +        CompletableFuture<Void> g = f.thenAccept(r);
679 +        f.complete(one);
680 +        checkCompletedWithWrappedCFException(g);
681 +        assertTrue(r.ran);
682 +    }
683 +
684 +    /**
685 +     * thenAccept result completes exceptionally if source cancelled
686 +     */
687 +    public void testThenAccept4() {
688 +        CompletableFuture<Integer> f = new CompletableFuture<>();
689 +        IncAction r = new IncAction();
690 +        CompletableFuture<Void> g = f.thenAccept(r);
691 +        assertTrue(f.cancel(true));
692 +        checkCompletedWithWrappedCancellationException(g);
693 +    }
694 +
695 +    /**
696 +     * thenCombine result completes normally after normal completion
697 +     * of sources
698 +     */
699 +    public void testThenCombine() {
700 +        CompletableFuture<Integer> f, g, h;
701 +
702 +        f = new CompletableFuture<>();
703 +        g = new CompletableFuture<>();
704 +        h = f.thenCombine(g, subtract);
705 +        f.complete(3);
706 +        checkIncomplete(h);
707 +        g.complete(1);
708 +        checkCompletedNormally(h, 2);
709 +
710 +        f = new CompletableFuture<>();
711 +        g = new CompletableFuture<>();
712 +        h = f.thenCombine(g, subtract);
713 +        g.complete(1);
714 +        checkIncomplete(h);
715 +        f.complete(3);
716 +        checkCompletedNormally(h, 2);
717 +
718 +        f = new CompletableFuture<>();
719 +        g = new CompletableFuture<>();
720 +        g.complete(1);
721 +        f.complete(3);
722 +        h = f.thenCombine(g, subtract);
723 +        checkCompletedNormally(h, 2);
724 +    }
725 +
726 +    /**
727 +     * thenCombine result completes exceptionally after exceptional
728 +     * completion of either source
729 +     */
730 +    public void testThenCombine2() {
731 +        CompletableFuture<Integer> f, g, h;
732 +
733 +        f = new CompletableFuture<>();
734 +        g = new CompletableFuture<>();
735 +        h = f.thenCombine(g, subtract);
736 +        f.completeExceptionally(new CFException());
737 +        checkIncomplete(h);
738 +        g.complete(1);
739 +        checkCompletedWithWrappedCFException(h);
740 +
741 +        f = new CompletableFuture<>();
742 +        g = new CompletableFuture<>();
743 +        h = f.thenCombine(g, subtract);
744 +        g.completeExceptionally(new CFException());
745 +        checkIncomplete(h);
746 +        f.complete(3);
747 +        checkCompletedWithWrappedCFException(h);
748 +
749 +        f = new CompletableFuture<>();
750 +        g = new CompletableFuture<>();
751 +        f.complete(3);
752 +        g.completeExceptionally(new CFException());
753 +        h = f.thenCombine(g, subtract);
754 +        checkCompletedWithWrappedCFException(h);
755 +
756 +        f = new CompletableFuture<>();
757 +        g = new CompletableFuture<>();
758 +        f.completeExceptionally(new CFException());
759 +        g.complete(3);
760 +        h = f.thenCombine(g, subtract);
761 +        checkCompletedWithWrappedCFException(h);
762 +    }
763 +
764 +    /**
765 +     * thenCombine result completes exceptionally if action does
766 +     */
767 +    public void testThenCombine3() {
768 +        CompletableFuture<Integer> f = new CompletableFuture<>();
769 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
770 +        FailingBiFunction r = new FailingBiFunction();
771 +        CompletableFuture<Integer> g = f.thenCombine(f2, r);
772 +        f.complete(one);
773 +        checkIncomplete(g);
774 +        assertFalse(r.ran);
775 +        f2.complete(two);
776 +        checkCompletedWithWrappedCFException(g);
777 +        assertTrue(r.ran);
778 +    }
779 +
780 +    /**
781 +     * thenCombine result completes exceptionally if either source cancelled
782 +     */
783 +    public void testThenCombine4() {
784 +        CompletableFuture<Integer> f, g, h;
785 +
786 +        f = new CompletableFuture<>();
787 +        g = new CompletableFuture<>();
788 +        h = f.thenCombine(g, subtract);
789 +        assertTrue(f.cancel(true));
790 +        checkIncomplete(h);
791 +        g.complete(1);
792 +        checkCompletedWithWrappedCancellationException(h);
793 +
794 +        f = new CompletableFuture<>();
795 +        g = new CompletableFuture<>();
796 +        h = f.thenCombine(g, subtract);
797 +        assertTrue(g.cancel(true));
798 +        checkIncomplete(h);
799 +        f.complete(3);
800 +        checkCompletedWithWrappedCancellationException(h);
801 +
802 +        f = new CompletableFuture<>();
803 +        g = new CompletableFuture<>();
804 +        assertTrue(f.cancel(true));
805 +        assertTrue(g.cancel(true));
806 +        h = f.thenCombine(g, subtract);
807 +        checkCompletedWithWrappedCancellationException(h);
808 +    }
809 +
810 +    /**
811 +     * thenAcceptBoth result completes normally after normal
812 +     * completion of sources
813 +     */
814 +    public void testThenAcceptBoth() {
815 +        CompletableFuture<Integer> f, g;
816 +        CompletableFuture<Void> h;
817 +        SubtractAction r;
818 +
819 +        f = new CompletableFuture<>();
820 +        g = new CompletableFuture<>();
821 +        h = f.thenAcceptBoth(g, r = new SubtractAction());
822 +        f.complete(3);
823 +        checkIncomplete(h);
824 +        g.complete(1);
825 +        checkCompletedNormally(h, null);
826 +        assertEquals(r.value, 2);
827 +
828 +        f = new CompletableFuture<>();
829 +        g = new CompletableFuture<>();
830 +        h = f.thenAcceptBoth(g, r = new SubtractAction());
831 +        g.complete(1);
832 +        checkIncomplete(h);
833 +        f.complete(3);
834 +        checkCompletedNormally(h, null);
835 +        assertEquals(r.value, 2);
836 +
837 +        f = new CompletableFuture<>();
838 +        g = new CompletableFuture<>();
839 +        g.complete(1);
840 +        f.complete(3);
841 +        h = f.thenAcceptBoth(g, r = new SubtractAction());
842 +        checkCompletedNormally(h, null);
843 +        assertEquals(r.value, 2);
844 +    }
845 +
846 +    /**
847 +     * thenAcceptBoth result completes exceptionally after exceptional
848 +     * completion of either source
849 +     */
850 +    public void testThenAcceptBoth2() {
851 +        CompletableFuture<Integer> f, g;
852 +        CompletableFuture<Void> h;
853 +        SubtractAction r;
854 +
855 +        f = new CompletableFuture<>();
856 +        g = new CompletableFuture<>();
857 +        h = f.thenAcceptBoth(g, r = new SubtractAction());
858 +        f.completeExceptionally(new CFException());
859 +        checkIncomplete(h);
860 +        g.complete(1);
861 +        checkCompletedWithWrappedCFException(h);
862 +
863 +        f = new CompletableFuture<>();
864 +        g = new CompletableFuture<>();
865 +        h = f.thenAcceptBoth(g, r = new SubtractAction());
866 +        g.completeExceptionally(new CFException());
867 +        checkIncomplete(h);
868 +        f.complete(3);
869 +        checkCompletedWithWrappedCFException(h);
870 +
871 +        f = new CompletableFuture<>();
872 +        g = new CompletableFuture<>();
873 +        f.complete(3);
874 +        g.completeExceptionally(new CFException());
875 +        h = f.thenAcceptBoth(g, r = new SubtractAction());
876 +        checkCompletedWithWrappedCFException(h);
877 +
878 +        f = new CompletableFuture<>();
879 +        g = new CompletableFuture<>();
880 +        f.completeExceptionally(new CFException());
881 +        g.complete(3);
882 +        h = f.thenAcceptBoth(g, r = new SubtractAction());
883 +        checkCompletedWithWrappedCFException(h);
884 +    }
885 +
886 +    /**
887 +     * thenAcceptBoth result completes exceptionally if action does
888 +     */
889 +    public void testThenAcceptBoth3() {
890 +        CompletableFuture<Integer> f, g;
891 +        CompletableFuture<Void> h;
892 +        FailingBiConsumer r;
893 +
894 +        f = new CompletableFuture<>();
895 +        g = new CompletableFuture<>();
896 +        h = f.thenAcceptBoth(g, r = new FailingBiConsumer());
897 +        f.complete(3);
898 +        checkIncomplete(h);
899 +        g.complete(1);
900 +        checkCompletedWithWrappedCFException(h);
901 +
902 +        f = new CompletableFuture<>();
903 +        g = new CompletableFuture<>();
904 +        f.complete(3);
905 +        g.complete(1);
906 +        h = f.thenAcceptBoth(g, r = new FailingBiConsumer());
907 +        checkCompletedWithWrappedCFException(h);
908 +    }
909 +
910 +    /**
911 +     * thenAcceptBoth result completes exceptionally if either source cancelled
912 +     */
913 +    public void testThenAcceptBoth4() {
914 +        CompletableFuture<Integer> f, g;
915 +        CompletableFuture<Void> h;
916 +        SubtractAction r;
917 +
918 +        f = new CompletableFuture<>();
919 +        g = new CompletableFuture<>();
920 +        h = f.thenAcceptBoth(g, r = new SubtractAction());
921 +        assertTrue(f.cancel(true));
922 +        checkIncomplete(h);
923 +        g.complete(1);
924 +        checkCompletedWithWrappedCancellationException(h);
925 +
926 +        f = new CompletableFuture<>();
927 +        g = new CompletableFuture<>();
928 +        h = f.thenAcceptBoth(g, r = new SubtractAction());
929 +        assertTrue(g.cancel(true));
930 +        checkIncomplete(h);
931 +        f.complete(3);
932 +        checkCompletedWithWrappedCancellationException(h);
933 +
934 +        f = new CompletableFuture<>();
935 +        g = new CompletableFuture<>();
936 +        f.complete(3);
937 +        assertTrue(g.cancel(true));
938 +        h = f.thenAcceptBoth(g, r = new SubtractAction());
939 +        checkCompletedWithWrappedCancellationException(h);
940 +
941 +        f = new CompletableFuture<>();
942 +        g = new CompletableFuture<>();
943 +        assertTrue(f.cancel(true));
944 +        g.complete(3);
945 +        h = f.thenAcceptBoth(g, r = new SubtractAction());
946 +        checkCompletedWithWrappedCancellationException(h);
947 +    }
948 +
949 +    /**
950 +     * Permits the testing of parallel code for the 3 different
951 +     * execution modes without repeating all the testing code.
952 +     */
953 +    enum ExecutionMode {
954 +        DEFAULT {
955 +            public <T,U> CompletableFuture<Void> runAfterBoth
956 +                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable r) {
957 +                return f.runAfterBoth(g, r);
958 +            }
959 +        },
960 +
961 +        DEFAULT_ASYNC {
962 +            public <T,U> CompletableFuture<Void> runAfterBoth
963 +                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable r) {
964 +                return f.runAfterBothAsync(g, r);
965 +            }
966 +        },
967 +
968 +        EXECUTOR {
969 +            public <T,U> CompletableFuture<Void> runAfterBoth
970 +                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable r) {
971 +                return f.runAfterBothAsync(g, r, new ThreadExecutor());
972 +            }
973 +        };
974 +
975 +        public abstract <T,U> CompletableFuture<Void> runAfterBoth
976 +            (CompletableFuture<T> f, CompletableFuture<U> g, Runnable r);
977 +    }
978 +
979 +    /**
980 +     * runAfterBoth result completes normally after normal
981 +     * completion of sources
982 +     */
983 +    public void testRunAfterBoth_normalCompletion1() {
984 +        for (ExecutionMode m : ExecutionMode.values())
985 +        for (Integer v1 : new Integer[] { 1, null })
986 +        for (Integer v2 : new Integer[] { 2, null }) {
987 +
988 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
989 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
990 +        final Noop r = new Noop();
991 +        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
992 +
993 +        f.complete(v1);
994 +        checkIncomplete(h);
995 +        assertFalse(r.ran);
996 +        g.complete(v2);
997 +
998 +        checkCompletedNormally(h, null);
999 +        assertTrue(r.ran);
1000 +        checkCompletedNormally(f, v1);
1001 +        checkCompletedNormally(g, v2);
1002 +        }
1003 +    }
1004 +
1005 +    public void testRunAfterBoth_normalCompletion2() {
1006 +        for (ExecutionMode m : ExecutionMode.values())
1007 +        for (Integer v1 : new Integer[] { 1, null })
1008 +        for (Integer v2 : new Integer[] { 2, null }) {
1009 +
1010 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1011 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1012 +        final Noop r = new Noop();
1013 +        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1014 +
1015 +        g.complete(v2);
1016 +        checkIncomplete(h);
1017 +        assertFalse(r.ran);
1018 +        f.complete(v1);
1019 +
1020 +        checkCompletedNormally(h, null);
1021 +        assertTrue(r.ran);
1022 +        checkCompletedNormally(f, v1);
1023 +        checkCompletedNormally(g, v2);
1024 +        }
1025 +    }
1026 +
1027 +    public void testRunAfterBoth_normalCompletion3() {
1028 +        for (ExecutionMode m : ExecutionMode.values())
1029 +        for (Integer v1 : new Integer[] { 1, null })
1030 +        for (Integer v2 : new Integer[] { 2, null }) {
1031 +
1032 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1033 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1034 +        final Noop r = new Noop();
1035 +
1036 +        g.complete(v2);
1037 +        f.complete(v1);
1038 +        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1039 +
1040 +        checkCompletedNormally(h, null);
1041 +        assertTrue(r.ran);
1042 +        checkCompletedNormally(f, v1);
1043 +        checkCompletedNormally(g, v2);
1044 +        }
1045 +    }
1046 +
1047 +    public void testRunAfterBoth_normalCompletion4() {
1048 +        for (ExecutionMode m : ExecutionMode.values())
1049 +        for (Integer v1 : new Integer[] { 1, null })
1050 +        for (Integer v2 : new Integer[] { 2, null }) {
1051 +
1052 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1053 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1054 +        final Noop r = new Noop();
1055 +
1056 +        f.complete(v1);
1057 +        g.complete(v2);
1058 +        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1059 +
1060 +        checkCompletedNormally(h, null);
1061 +        assertTrue(r.ran);
1062 +        checkCompletedNormally(f, v1);
1063 +        checkCompletedNormally(g, v2);
1064 +        }
1065 +    }
1066 +
1067 +    /**
1068 +     * runAfterBoth result completes exceptionally after exceptional
1069 +     * completion of either source
1070 +     */
1071 +    public void testRunAfterBoth_exceptionalCompletion1() {
1072 +        for (ExecutionMode m : ExecutionMode.values())
1073 +        for (Integer v1 : new Integer[] { 1, null }) {
1074 +
1075 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1076 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1077 +        final Noop r = new Noop();
1078 +        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1079 +        final CFException ex = new CFException();
1080 +
1081 +        f.completeExceptionally(ex);
1082 +        checkIncomplete(h);
1083 +        g.complete(v1);
1084 +
1085 +        checkCompletedWithWrappedCFException(h, ex);
1086 +        checkCompletedWithWrappedCFException(f, ex);
1087 +        assertFalse(r.ran);
1088 +        checkCompletedNormally(g, v1);
1089 +        }
1090 +    }
1091 +
1092 +    public void testRunAfterBoth_exceptionalCompletion2() {
1093 +        for (ExecutionMode m : ExecutionMode.values())
1094 +        for (Integer v1 : new Integer[] { 1, null }) {
1095 +
1096 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1097 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1098 +        final Noop r = new Noop();
1099 +        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1100 +        final CFException ex = new CFException();
1101 +
1102 +        g.completeExceptionally(ex);
1103 +        checkIncomplete(h);
1104 +        f.complete(v1);
1105 +
1106 +        checkCompletedWithWrappedCFException(h, ex);
1107 +        checkCompletedWithWrappedCFException(g, ex);
1108 +        assertFalse(r.ran);
1109 +        checkCompletedNormally(f, v1);
1110 +        }
1111 +    }
1112 +
1113 +    public void testRunAfterBoth_exceptionalCompletion3() {
1114 +        for (ExecutionMode m : ExecutionMode.values())
1115 +        for (Integer v1 : new Integer[] { 1, null }) {
1116 +
1117 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1118 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1119 +        final Noop r = new Noop();
1120 +        final CFException ex = new CFException();
1121 +
1122 +        g.completeExceptionally(ex);
1123 +        f.complete(v1);
1124 +        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1125 +
1126 +        checkCompletedWithWrappedCFException(h, ex);
1127 +        checkCompletedWithWrappedCFException(g, ex);
1128 +        assertFalse(r.ran);
1129 +        checkCompletedNormally(f, v1);
1130 +        }
1131 +    }
1132 +
1133 +    public void testRunAfterBoth_exceptionalCompletion4() {
1134 +        for (ExecutionMode m : ExecutionMode.values())
1135 +        for (Integer v1 : new Integer[] { 1, null }) {
1136 +
1137 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1138 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1139 +        final Noop r = new Noop();
1140 +        final CFException ex = new CFException();
1141 +
1142 +        f.completeExceptionally(ex);
1143 +        g.complete(v1);
1144 +        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1145 +
1146 +        checkCompletedWithWrappedCFException(h, ex);
1147 +        checkCompletedWithWrappedCFException(f, ex);
1148 +        assertFalse(r.ran);
1149 +        checkCompletedNormally(g, v1);
1150 +        }
1151 +    }
1152 +
1153 +    /**
1154 +     * runAfterBoth result completes exceptionally if action does
1155 +     */
1156 +    public void testRunAfterBoth_actionFailed1() {
1157 +        for (ExecutionMode m : ExecutionMode.values())
1158 +        for (Integer v1 : new Integer[] { 1, null })
1159 +        for (Integer v2 : new Integer[] { 2, null }) {
1160 +
1161 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1162 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1163 +        final FailingNoop r = new FailingNoop();
1164 +        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1165 +
1166 +        f.complete(v1);
1167 +        checkIncomplete(h);
1168 +        g.complete(v2);
1169 +
1170 +        checkCompletedWithWrappedCFException(h);
1171 +        checkCompletedNormally(f, v1);
1172 +        checkCompletedNormally(g, v2);
1173 +        }
1174 +    }
1175 +
1176 +    public void testRunAfterBoth_actionFailed2() {
1177 +        for (ExecutionMode m : ExecutionMode.values())
1178 +        for (Integer v1 : new Integer[] { 1, null })
1179 +        for (Integer v2 : new Integer[] { 2, null }) {
1180 +
1181 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1182 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1183 +        final FailingNoop r = new FailingNoop();
1184 +        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1185 +
1186 +        g.complete(v2);
1187 +        checkIncomplete(h);
1188 +        f.complete(v1);
1189 +
1190 +        checkCompletedWithWrappedCFException(h);
1191 +        checkCompletedNormally(f, v1);
1192 +        checkCompletedNormally(g, v2);
1193 +        }
1194 +    }
1195 +
1196 +    /**
1197 +     * runAfterBoth result completes exceptionally if either source cancelled
1198 +     */
1199 +    public void testRunAfterBoth_sourceCancelled1() {
1200 +        for (ExecutionMode m : ExecutionMode.values())
1201 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1202 +        for (Integer v1 : new Integer[] { 1, null }) {
1203 +
1204 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1205 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1206 +        final Noop r = new Noop();
1207 +        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1208 +
1209 +        assertTrue(f.cancel(mayInterruptIfRunning));
1210 +        checkIncomplete(h);
1211 +        g.complete(v1);
1212 +
1213 +        checkCompletedWithWrappedCancellationException(h);
1214 +        checkCancelled(f);
1215 +        assertFalse(r.ran);
1216 +        checkCompletedNormally(g, v1);
1217 +        }
1218 +    }
1219 +
1220 +    public void testRunAfterBoth_sourceCancelled2() {
1221 +        for (ExecutionMode m : ExecutionMode.values())
1222 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1223 +        for (Integer v1 : new Integer[] { 1, null }) {
1224 +
1225 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1226 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1227 +        final Noop r = new Noop();
1228 +        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1229 +
1230 +        assertTrue(g.cancel(mayInterruptIfRunning));
1231 +        checkIncomplete(h);
1232 +        f.complete(v1);
1233 +
1234 +        checkCompletedWithWrappedCancellationException(h);
1235 +        checkCancelled(g);
1236 +        assertFalse(r.ran);
1237 +        checkCompletedNormally(f, v1);
1238 +        }
1239 +    }
1240 +
1241 +    public void testRunAfterBoth_sourceCancelled3() {
1242 +        for (ExecutionMode m : ExecutionMode.values())
1243 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1244 +        for (Integer v1 : new Integer[] { 1, null }) {
1245 +
1246 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1247 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1248 +        final Noop r = new Noop();
1249 +
1250 +        assertTrue(g.cancel(mayInterruptIfRunning));
1251 +        f.complete(v1);
1252 +        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1253 +
1254 +        checkCompletedWithWrappedCancellationException(h);
1255 +        checkCancelled(g);
1256 +        assertFalse(r.ran);
1257 +        checkCompletedNormally(f, v1);
1258 +        }
1259 +    }
1260 +
1261 +    public void testRunAfterBoth_sourceCancelled4() {
1262 +        for (ExecutionMode m : ExecutionMode.values())
1263 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1264 +        for (Integer v1 : new Integer[] { 1, null }) {
1265 +
1266 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1267 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1268 +        final Noop r = new Noop();
1269 +
1270 +        assertTrue(f.cancel(mayInterruptIfRunning));
1271 +        g.complete(v1);
1272 +        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1273 +
1274 +        checkCompletedWithWrappedCancellationException(h);
1275 +        checkCancelled(f);
1276 +        assertFalse(r.ran);
1277 +        checkCompletedNormally(g, v1);
1278 +        }
1279 +    }
1280 +
1281 +    /**
1282 +     * applyToEither result completes normally after normal completion
1283 +     * of either source
1284 +     */
1285 +    public void testApplyToEither() {
1286 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1287 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1288 +        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1289 +        f.complete(one);
1290 +        checkCompletedNormally(g, two);
1291 +        f2.complete(one);
1292 +        checkCompletedNormally(g, two);
1293 +
1294 +        f = new CompletableFuture<>();
1295 +        f.complete(one);
1296 +        f2 = new CompletableFuture<>();
1297 +        g = f.applyToEither(f2, inc);
1298 +        checkCompletedNormally(g, two);
1299 +    }
1300 +
1301 +    /**
1302 +     * applyToEither result completes exceptionally after exceptional
1303 +     * completion of either source
1304 +     */
1305 +    public void testApplyToEither2() {
1306 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1307 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1308 +        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1309 +        f.completeExceptionally(new CFException());
1310 +        f2.complete(one);
1311 +        checkCompletedWithWrappedCFException(g);
1312 +
1313 +        f = new CompletableFuture<>();
1314 +        f2 = new CompletableFuture<>();
1315 +        f2.completeExceptionally(new CFException());
1316 +        g = f.applyToEither(f2, inc);
1317 +        checkCompletedWithWrappedCFException(g);
1318 +    }
1319 +
1320 +    /**
1321 +     * applyToEither result completes exceptionally if action does
1322 +     */
1323 +    public void testApplyToEither3() {
1324 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1325 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1326 +        FailingFunction r = new FailingFunction();
1327 +        CompletableFuture<Integer> g = f.applyToEither(f2, r);
1328 +        f2.complete(two);
1329 +        checkCompletedWithWrappedCFException(g);
1330 +    }
1331 +
1332 +    /**
1333 +     * applyToEither result completes exceptionally if either source cancelled
1334 +     */
1335 +    public void testApplyToEither4() {
1336 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1337 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1338 +        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1339 +        assertTrue(f.cancel(true));
1340 +        checkCompletedWithWrappedCancellationException(g);
1341 +        f = new CompletableFuture<>();
1342 +        f2 = new CompletableFuture<>();
1343 +        assertTrue(f2.cancel(true));
1344 +        checkCompletedWithWrappedCancellationException(g);
1345 +    }
1346 +
1347 +    /**
1348 +     * acceptEither result completes normally after normal completion
1349 +     * of either source
1350 +     */
1351 +    public void testAcceptEither() {
1352 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1353 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1354 +        IncAction r = new IncAction();
1355 +        CompletableFuture<Void> g = f.acceptEither(f2, r);
1356 +        f.complete(one);
1357 +        checkCompletedNormally(g, null);
1358 +        f2.complete(one);
1359 +        checkCompletedNormally(g, null);
1360 +        assertEquals(r.value, 2);
1361 +
1362 +        r = new IncAction();
1363 +        f = new CompletableFuture<>();
1364 +        f.complete(one);
1365 +        f2 = new CompletableFuture<>();
1366 +        g = f.acceptEither(f2, r);
1367 +        checkCompletedNormally(g, null);
1368 +        assertEquals(r.value, 2);
1369 +    }
1370 +
1371 +    /**
1372 +     * acceptEither result completes exceptionally after exceptional
1373 +     * completion of either source
1374 +     */
1375 +    public void testAcceptEither2() {
1376 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1377 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1378 +        IncAction r = new IncAction();
1379 +        CompletableFuture<Void> g = f.acceptEither(f2, r);
1380 +        f.completeExceptionally(new CFException());
1381 +        f2.complete(one);
1382 +        checkCompletedWithWrappedCFException(g);
1383 +
1384 +        r = new IncAction();
1385 +        f = new CompletableFuture<>();
1386 +        f2 = new CompletableFuture<>();
1387 +        f2.completeExceptionally(new CFException());
1388 +        g = f.acceptEither(f2, r);
1389 +        checkCompletedWithWrappedCFException(g);
1390 +    }
1391 +
1392 +    /**
1393 +     * acceptEither result completes exceptionally if action does
1394 +     */
1395 +    public void testAcceptEither3() {
1396 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1397 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1398 +        FailingConsumer r = new FailingConsumer();
1399 +        CompletableFuture<Void> g = f.acceptEither(f2, r);
1400 +        f2.complete(two);
1401 +        checkCompletedWithWrappedCFException(g);
1402 +    }
1403 +
1404 +    /**
1405 +     * acceptEither result completes exceptionally if either source cancelled
1406 +     */
1407 +    public void testAcceptEither4() {
1408 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1409 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1410 +        IncAction r = new IncAction();
1411 +        CompletableFuture<Void> g = f.acceptEither(f2, r);
1412 +        assertTrue(f.cancel(true));
1413 +        checkCompletedWithWrappedCancellationException(g);
1414 +        f = new CompletableFuture<>();
1415 +        f2 = new CompletableFuture<>();
1416 +        assertTrue(f2.cancel(true));
1417 +        checkCompletedWithWrappedCancellationException(g);
1418 +    }
1419 +
1420 +    /**
1421 +     * runAfterEither result completes normally after normal completion
1422 +     * of either source
1423 +     */
1424 +    public void testRunAfterEither() {
1425 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1426 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1427 +        Noop r = new Noop();
1428 +        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1429 +        f.complete(one);
1430 +        checkCompletedNormally(g, null);
1431 +        f2.complete(one);
1432 +        checkCompletedNormally(g, null);
1433 +        assertTrue(r.ran);
1434 +
1435 +        r = new Noop();
1436 +        f = new CompletableFuture<>();
1437 +        f.complete(one);
1438 +        f2 = new CompletableFuture<>();
1439 +        g = f.runAfterEither(f2, r);
1440 +        checkCompletedNormally(g, null);
1441 +        assertTrue(r.ran);
1442 +    }
1443 +
1444 +    /**
1445 +     * runAfterEither result completes exceptionally after exceptional
1446 +     * completion of either source
1447 +     */
1448 +    public void testRunAfterEither2() {
1449 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1450 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1451 +        Noop r = new Noop();
1452 +        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1453 +        f.completeExceptionally(new CFException());
1454 +        f2.complete(one);
1455 +        checkCompletedWithWrappedCFException(g);
1456 +
1457 +        r = new Noop();
1458 +        f = new CompletableFuture<>();
1459 +        f2 = new CompletableFuture<>();
1460 +        f2.completeExceptionally(new CFException());
1461 +        g = f.runAfterEither(f2, r);
1462 +        checkCompletedWithWrappedCFException(g);
1463 +    }
1464 +
1465 +    /**
1466 +     * runAfterEither result completes exceptionally if action does
1467 +     */
1468 +    public void testRunAfterEither3() {
1469 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1470 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1471 +        FailingNoop r = new FailingNoop();
1472 +        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1473 +        f2.complete(two);
1474 +        checkCompletedWithWrappedCFException(g);
1475 +    }
1476 +
1477 +    /**
1478 +     * runAfterEither result completes exceptionally if either source cancelled
1479 +     */
1480 +    public void testRunAfterEither4() {
1481 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1482 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1483 +        Noop r = new Noop();
1484 +        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1485 +        assertTrue(f.cancel(true));
1486 +        checkCompletedWithWrappedCancellationException(g);
1487 +        f = new CompletableFuture<>();
1488 +        f2 = new CompletableFuture<>();
1489 +        assertTrue(f2.cancel(true));
1490 +        checkCompletedWithWrappedCancellationException(g);
1491 +    }
1492 +
1493 +    /**
1494 +     * thenCompose result completes normally after normal completion of source
1495 +     */
1496 +    public void testThenCompose() {
1497 +        CompletableFuture<Integer> f, g;
1498 +        CompletableFutureInc r;
1499 +
1500 +        f = new CompletableFuture<>();
1501 +        g = f.thenCompose(r = new CompletableFutureInc());
1502 +        f.complete(one);
1503 +        checkCompletedNormally(g, two);
1504 +        assertTrue(r.ran);
1505 +
1506 +        f = new CompletableFuture<>();
1507 +        f.complete(one);
1508 +        g = f.thenCompose(r = new CompletableFutureInc());
1509 +        checkCompletedNormally(g, two);
1510 +        assertTrue(r.ran);
1511 +    }
1512 +
1513 +    /**
1514 +     * thenCompose result completes exceptionally after exceptional
1515 +     * completion of source
1516 +     */
1517 +    public void testThenCompose2() {
1518 +        CompletableFuture<Integer> f, g;
1519 +        CompletableFutureInc r;
1520 +
1521 +        f = new CompletableFuture<>();
1522 +        g = f.thenCompose(r = new CompletableFutureInc());
1523 +        f.completeExceptionally(new CFException());
1524 +        checkCompletedWithWrappedCFException(g);
1525 +
1526 +        f = new CompletableFuture<>();
1527 +        f.completeExceptionally(new CFException());
1528 +        g = f.thenCompose(r = new CompletableFutureInc());
1529 +        checkCompletedWithWrappedCFException(g);
1530 +    }
1531 +
1532 +    /**
1533 +     * thenCompose result completes exceptionally if action does
1534 +     */
1535 +    public void testThenCompose3() {
1536 +        CompletableFuture<Integer> f, g;
1537 +        FailingCompletableFutureFunction r;
1538 +
1539 +        f = new CompletableFuture<>();
1540 +        g = f.thenCompose(r = new FailingCompletableFutureFunction());
1541 +        f.complete(one);
1542 +        checkCompletedWithWrappedCFException(g);
1543 +
1544 +        f = new CompletableFuture<>();
1545 +        f.complete(one);
1546 +        g = f.thenCompose(r = new FailingCompletableFutureFunction());
1547 +        checkCompletedWithWrappedCFException(g);
1548 +    }
1549 +
1550 +    /**
1551 +     * thenCompose result completes exceptionally if source cancelled
1552 +     */
1553 +    public void testThenCompose4() {
1554 +        CompletableFuture<Integer> f, g;
1555 +        CompletableFutureInc r;
1556 +
1557 +        f = new CompletableFuture<>();
1558 +        g = f.thenCompose(r = new CompletableFutureInc());
1559 +        assertTrue(f.cancel(true));
1560 +        checkCompletedWithWrappedCancellationException(g);
1561 +
1562 +        f = new CompletableFuture<>();
1563 +        assertTrue(f.cancel(true));
1564 +        g = f.thenCompose(r = new CompletableFutureInc());
1565 +        checkCompletedWithWrappedCancellationException(g);
1566 +    }
1567 +
1568 +    // asyncs
1569 +
1570 +    /**
1571 +     * thenRunAsync result completes normally after normal completion of source
1572 +     */
1573 +    public void testThenRunAsync() {
1574 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1575 +        Noop r = new Noop();
1576 +        CompletableFuture<Void> g = f.thenRunAsync(r);
1577 +        f.complete(null);
1578 +        checkCompletedNormally(g, null);
1579 +
1580 +        // reordered version
1581 +        f = new CompletableFuture<>();
1582 +        f.complete(null);
1583 +        r = new Noop();
1584 +        g = f.thenRunAsync(r);
1585 +        checkCompletedNormally(g, null);
1586 +    }
1587 +
1588 +    /**
1589 +     * thenRunAsync result completes exceptionally after exceptional
1590 +     * completion of source
1591 +     */
1592 +    public void testThenRunAsync2() {
1593 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1594 +        Noop r = new Noop();
1595 +        CompletableFuture<Void> g = f.thenRunAsync(r);
1596 +        f.completeExceptionally(new CFException());
1597 +        try {
1598 +            g.join();
1599 +            shouldThrow();
1600 +        } catch (CompletionException success) {}
1601 +        checkCompletedWithWrappedCFException(g);
1602 +    }
1603 +
1604 +    /**
1605 +     * thenRunAsync result completes exceptionally if action does
1606 +     */
1607 +    public void testThenRunAsync3() {
1608 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1609 +        FailingNoop r = new FailingNoop();
1610 +        CompletableFuture<Void> g = f.thenRunAsync(r);
1611 +        f.complete(null);
1612 +        checkCompletedWithWrappedCFException(g);
1613 +    }
1614 +
1615 +    /**
1616 +     * thenRunAsync result completes exceptionally if source cancelled
1617 +     */
1618 +    public void testThenRunAsync4() {
1619 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1620 +        Noop r = new Noop();
1621 +        CompletableFuture<Void> g = f.thenRunAsync(r);
1622 +        assertTrue(f.cancel(true));
1623 +        checkCompletedWithWrappedCancellationException(g);
1624 +    }
1625 +
1626 +    /**
1627 +     * thenApplyAsync result completes normally after normal completion of source
1628 +     */
1629 +    public void testThenApplyAsync() {
1630 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1631 +        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1632 +        f.complete(one);
1633 +        checkCompletedNormally(g, two);
1634 +    }
1635 +
1636 +    /**
1637 +     * thenApplyAsync result completes exceptionally after exceptional
1638 +     * completion of source
1639 +     */
1640 +    public void testThenApplyAsync2() {
1641 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1642 +        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1643 +        f.completeExceptionally(new CFException());
1644 +        checkCompletedWithWrappedCFException(g);
1645 +    }
1646 +
1647 +    /**
1648 +     * thenApplyAsync result completes exceptionally if action does
1649 +     */
1650 +    public void testThenApplyAsync3() {
1651 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1652 +        FailingFunction r = new FailingFunction();
1653 +        CompletableFuture<Integer> g = f.thenApplyAsync(r);
1654 +        f.complete(null);
1655 +        checkCompletedWithWrappedCFException(g);
1656 +    }
1657 +
1658 +    /**
1659 +     * thenApplyAsync result completes exceptionally if source cancelled
1660 +     */
1661 +    public void testThenApplyAsync4() {
1662 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1663 +        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1664 +        assertTrue(f.cancel(true));
1665 +        checkCompletedWithWrappedCancellationException(g);
1666 +    }
1667 +
1668 +    /**
1669 +     * thenAcceptAsync result completes normally after normal
1670 +     * completion of source
1671 +     */
1672 +    public void testThenAcceptAsync() {
1673 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1674 +        IncAction r = new IncAction();
1675 +        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1676 +        f.complete(one);
1677 +        checkCompletedNormally(g, null);
1678 +        assertEquals(r.value, 2);
1679 +    }
1680 +
1681 +    /**
1682 +     * thenAcceptAsync result completes exceptionally after exceptional
1683 +     * completion of source
1684 +     */
1685 +    public void testThenAcceptAsync2() {
1686 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1687 +        IncAction r = new IncAction();
1688 +        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1689 +        f.completeExceptionally(new CFException());
1690 +        checkCompletedWithWrappedCFException(g);
1691 +    }
1692 +
1693 +    /**
1694 +     * thenAcceptAsync result completes exceptionally if action does
1695 +     */
1696 +    public void testThenAcceptAsync3() {
1697 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1698 +        FailingConsumer r = new FailingConsumer();
1699 +        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1700 +        f.complete(null);
1701 +        checkCompletedWithWrappedCFException(g);
1702 +    }
1703 +
1704 +    /**
1705 +     * thenAcceptAsync result completes exceptionally if source cancelled
1706 +     */
1707 +    public void testThenAcceptAsync4() {
1708 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1709 +        IncAction r = new IncAction();
1710 +        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1711 +        assertTrue(f.cancel(true));
1712 +        checkCompletedWithWrappedCancellationException(g);
1713 +    }
1714 +
1715 +    /**
1716 +     * thenCombineAsync result completes normally after normal
1717 +     * completion of sources
1718 +     */
1719 +    public void testThenCombineAsync() {
1720 +        CompletableFuture<Integer> f, g, h;
1721 +
1722 +        f = new CompletableFuture<>();
1723 +        g = new CompletableFuture<>();
1724 +        h = f.thenCombineAsync(g, subtract);
1725 +        f.complete(3);
1726 +        checkIncomplete(h);
1727 +        g.complete(1);
1728 +        checkCompletedNormally(h, 2);
1729 +
1730 +        f = new CompletableFuture<>();
1731 +        g = new CompletableFuture<>();
1732 +        h = f.thenCombineAsync(g, subtract);
1733 +        g.complete(1);
1734 +        checkIncomplete(h);
1735 +        f.complete(3);
1736 +        checkCompletedNormally(h, 2);
1737 +
1738 +        f = new CompletableFuture<>();
1739 +        g = new CompletableFuture<>();
1740 +        g.complete(1);
1741 +        f.complete(3);
1742 +        h = f.thenCombineAsync(g, subtract);
1743 +        checkCompletedNormally(h, 2);
1744 +    }
1745 +
1746 +    /**
1747 +     * thenCombineAsync result completes exceptionally after exceptional
1748 +     * completion of either source
1749 +     */
1750 +    public void testThenCombineAsync2() {
1751 +        CompletableFuture<Integer> f, g, h;
1752 +
1753 +        f = new CompletableFuture<>();
1754 +        g = new CompletableFuture<>();
1755 +        h = f.thenCombineAsync(g, subtract);
1756 +        f.completeExceptionally(new CFException());
1757 +        checkIncomplete(h);
1758 +        g.complete(1);
1759 +        checkCompletedWithWrappedCFException(h);
1760 +
1761 +        f = new CompletableFuture<>();
1762 +        g = new CompletableFuture<>();
1763 +        h = f.thenCombineAsync(g, subtract);
1764 +        g.completeExceptionally(new CFException());
1765 +        checkIncomplete(h);
1766 +        f.complete(3);
1767 +        checkCompletedWithWrappedCFException(h);
1768 +
1769 +        f = new CompletableFuture<>();
1770 +        g = new CompletableFuture<>();
1771 +        g.completeExceptionally(new CFException());
1772 +        f.complete(3);
1773 +        h = f.thenCombineAsync(g, subtract);
1774 +        checkCompletedWithWrappedCFException(h);
1775 +    }
1776 +
1777 +    /**
1778 +     * thenCombineAsync result completes exceptionally if action does
1779 +     */
1780 +    public void testThenCombineAsync3() {
1781 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1782 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1783 +        FailingBiFunction r = new FailingBiFunction();
1784 +        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r);
1785 +        f.complete(one);
1786 +        checkIncomplete(g);
1787 +        assertFalse(r.ran);
1788 +        f2.complete(two);
1789 +        checkCompletedWithWrappedCFException(g);
1790 +        assertTrue(r.ran);
1791 +    }
1792 +
1793 +    /**
1794 +     * thenCombineAsync result completes exceptionally if either source cancelled
1795 +     */
1796 +    public void testThenCombineAsync4() {
1797 +        CompletableFuture<Integer> f, g, h;
1798 +
1799 +        f = new CompletableFuture<>();
1800 +        g = new CompletableFuture<>();
1801 +        h = f.thenCombineAsync(g, subtract);
1802 +        assertTrue(f.cancel(true));
1803 +        checkIncomplete(h);
1804 +        g.complete(1);
1805 +        checkCompletedWithWrappedCancellationException(h);
1806 +
1807 +        f = new CompletableFuture<>();
1808 +        g = new CompletableFuture<>();
1809 +        h = f.thenCombineAsync(g, subtract);
1810 +        assertTrue(g.cancel(true));
1811 +        checkIncomplete(h);
1812 +        f.complete(3);
1813 +        checkCompletedWithWrappedCancellationException(h);
1814 +
1815 +        f = new CompletableFuture<>();
1816 +        g = new CompletableFuture<>();
1817 +        g.complete(3);
1818 +        assertTrue(f.cancel(true));
1819 +        h = f.thenCombineAsync(g, subtract);
1820 +        checkCompletedWithWrappedCancellationException(h);
1821 +
1822 +        f = new CompletableFuture<>();
1823 +        g = new CompletableFuture<>();
1824 +        f.complete(3);
1825 +        assertTrue(g.cancel(true));
1826 +        h = f.thenCombineAsync(g, subtract);
1827 +        checkCompletedWithWrappedCancellationException(h);
1828 +    }
1829 +
1830 +    /**
1831 +     * thenAcceptBothAsync result completes normally after normal
1832 +     * completion of sources
1833 +     */
1834 +    public void testThenAcceptBothAsync() {
1835 +        CompletableFuture<Integer> f, g;
1836 +        CompletableFuture<Void> h;
1837 +        SubtractAction r;
1838 +
1839 +        f = new CompletableFuture<>();
1840 +        g = new CompletableFuture<>();
1841 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1842 +        f.complete(3);
1843 +        checkIncomplete(h);
1844 +        g.complete(1);
1845 +        checkCompletedNormally(h, null);
1846 +        assertEquals(r.value, 2);
1847 +
1848 +        f = new CompletableFuture<>();
1849 +        g = new CompletableFuture<>();
1850 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1851 +        g.complete(1);
1852 +        checkIncomplete(h);
1853 +        f.complete(3);
1854 +        checkCompletedNormally(h, null);
1855 +        assertEquals(r.value, 2);
1856 +
1857 +        f = new CompletableFuture<>();
1858 +        g = new CompletableFuture<>();
1859 +        g.complete(1);
1860 +        f.complete(3);
1861 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1862 +        checkCompletedNormally(h, null);
1863 +        assertEquals(r.value, 2);
1864 +    }
1865 +
1866 +    /**
1867 +     * thenAcceptBothAsync result completes exceptionally after exceptional
1868 +     * completion of source
1869 +     */
1870 +    public void testThenAcceptBothAsync2() {
1871 +        CompletableFuture<Integer> f, g;
1872 +        CompletableFuture<Void> h;
1873 +        SubtractAction r;
1874 +
1875 +        f = new CompletableFuture<>();
1876 +        g = new CompletableFuture<>();
1877 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1878 +        f.completeExceptionally(new CFException());
1879 +        checkIncomplete(h);
1880 +        g.complete(1);
1881 +        checkCompletedWithWrappedCFException(h);
1882 +
1883 +        f = new CompletableFuture<>();
1884 +        g = new CompletableFuture<>();
1885 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1886 +        g.completeExceptionally(new CFException());
1887 +        checkIncomplete(h);
1888 +        f.complete(3);
1889 +        checkCompletedWithWrappedCFException(h);
1890 +
1891 +        f = new CompletableFuture<>();
1892 +        g = new CompletableFuture<>();
1893 +        f.complete(3);
1894 +        g.completeExceptionally(new CFException());
1895 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1896 +        checkCompletedWithWrappedCFException(h);
1897 +
1898 +        f = new CompletableFuture<>();
1899 +        g = new CompletableFuture<>();
1900 +        f.completeExceptionally(new CFException());
1901 +        g.complete(3);
1902 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1903 +        checkCompletedWithWrappedCFException(h);
1904 +    }
1905 +
1906 +    /**
1907 +     * thenAcceptBothAsync result completes exceptionally if action does
1908 +     */
1909 +    public void testThenAcceptBothAsync3() {
1910 +        CompletableFuture<Integer> f, g;
1911 +        CompletableFuture<Void> h;
1912 +        FailingBiConsumer r;
1913 +
1914 +        f = new CompletableFuture<>();
1915 +        g = new CompletableFuture<>();
1916 +        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer());
1917 +        f.complete(3);
1918 +        checkIncomplete(h);
1919 +        g.complete(1);
1920 +        checkCompletedWithWrappedCFException(h);
1921 +
1922 +        f = new CompletableFuture<>();
1923 +        g = new CompletableFuture<>();
1924 +        f.complete(3);
1925 +        g.complete(1);
1926 +        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer());
1927 +        checkCompletedWithWrappedCFException(h);
1928 +    }
1929 +
1930 +    /**
1931 +     * thenAcceptBothAsync result completes exceptionally if either source cancelled
1932 +     */
1933 +    public void testThenAcceptBothAsync4() {
1934 +        CompletableFuture<Integer> f, g;
1935 +        CompletableFuture<Void> h;
1936 +        SubtractAction r;
1937 +
1938 +        f = new CompletableFuture<>();
1939 +        g = new CompletableFuture<>();
1940 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1941 +        assertTrue(f.cancel(true));
1942 +        checkIncomplete(h);
1943 +        g.complete(1);
1944 +        checkCompletedWithWrappedCancellationException(h);
1945 +
1946 +        f = new CompletableFuture<>();
1947 +        g = new CompletableFuture<>();
1948 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1949 +        assertTrue(g.cancel(true));
1950 +        checkIncomplete(h);
1951 +        f.complete(3);
1952 +        checkCompletedWithWrappedCancellationException(h);
1953 +
1954 +        f = new CompletableFuture<>();
1955 +        g = new CompletableFuture<>();
1956 +        f.complete(3);
1957 +        assertTrue(g.cancel(true));
1958 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1959 +        checkCompletedWithWrappedCancellationException(h);
1960 +
1961 +        f = new CompletableFuture<>();
1962 +        g = new CompletableFuture<>();
1963 +        assertTrue(f.cancel(true));
1964 +        g.complete(3);
1965 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1966 +        checkCompletedWithWrappedCancellationException(h);
1967 +    }
1968 +
1969 +    /**
1970 +     * applyToEitherAsync result completes normally after normal
1971 +     * completion of sources
1972 +     */
1973 +    public void testApplyToEitherAsync() {
1974 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1975 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1976 +        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1977 +        f.complete(one);
1978 +        checkCompletedNormally(g, two);
1979 +
1980 +        f = new CompletableFuture<>();
1981 +        f.complete(one);
1982 +        f2 = new CompletableFuture<>();
1983 +        g = f.applyToEitherAsync(f2, inc);
1984 +        checkCompletedNormally(g, two);
1985 +    }
1986 +
1987 +    /**
1988 +     * applyToEitherAsync result completes exceptionally after exceptional
1989 +     * completion of source
1990 +     */
1991 +    public void testApplyToEitherAsync2() {
1992 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1993 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1994 +        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1995 +        f.completeExceptionally(new CFException());
1996 +        checkCompletedWithWrappedCFException(g);
1997 +
1998 +        f = new CompletableFuture<>();
1999 +        f2 = new CompletableFuture<>();
2000 +        f2.completeExceptionally(new CFException());
2001 +        g = f.applyToEitherAsync(f2, inc);
2002 +        f.complete(one);
2003 +        checkCompletedWithWrappedCFException(g);
2004 +    }
2005 +
2006 +    /**
2007 +     * applyToEitherAsync result completes exceptionally if action does
2008 +     */
2009 +    public void testApplyToEitherAsync3() {
2010 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2011 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2012 +        FailingFunction r = new FailingFunction();
2013 +        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r);
2014 +        f.complete(one);
2015 +        checkCompletedWithWrappedCFException(g);
2016 +    }
2017 +
2018 +    /**
2019 +     * applyToEitherAsync result completes exceptionally if either source cancelled
2020 +     */
2021 +    public void testApplyToEitherAsync4() {
2022 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2023 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2024 +        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2025 +        assertTrue(f.cancel(true));
2026 +        checkCompletedWithWrappedCancellationException(g);
2027 +
2028 +        f = new CompletableFuture<>();
2029 +        f2 = new CompletableFuture<>();
2030 +        assertTrue(f2.cancel(true));
2031 +        g = f.applyToEitherAsync(f2, inc);
2032 +        checkCompletedWithWrappedCancellationException(g);
2033 +    }
2034 +
2035 +    /**
2036 +     * acceptEitherAsync result completes normally after normal
2037 +     * completion of sources
2038 +     */
2039 +    public void testAcceptEitherAsync() {
2040 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2041 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2042 +        IncAction r = new IncAction();
2043 +        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2044 +        f.complete(one);
2045 +        checkCompletedNormally(g, null);
2046 +        assertEquals(r.value, 2);
2047 +
2048 +        r = new IncAction();
2049 +        f = new CompletableFuture<>();
2050 +        f.complete(one);
2051 +        f2 = new CompletableFuture<>();
2052 +        g = f.acceptEitherAsync(f2, r);
2053 +        checkCompletedNormally(g, null);
2054 +        assertEquals(r.value, 2);
2055 +    }
2056 +
2057 +    /**
2058 +     * acceptEitherAsync result completes exceptionally after exceptional
2059 +     * completion of source
2060 +     */
2061 +    public void testAcceptEitherAsync2() {
2062 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2063 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2064 +        IncAction r = new IncAction();
2065 +        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2066 +        f.completeExceptionally(new CFException());
2067 +        checkCompletedWithWrappedCFException(g);
2068 +
2069 +        r = new IncAction();
2070 +        f = new CompletableFuture<>();
2071 +        f2 = new CompletableFuture<>();
2072 +        f2.completeExceptionally(new CFException());
2073 +        g = f.acceptEitherAsync(f2, r);
2074 +        f.complete(one);
2075 +        checkCompletedWithWrappedCFException(g);
2076 +    }
2077 +
2078 +    /**
2079 +     * acceptEitherAsync result completes exceptionally if action does
2080 +     */
2081 +    public void testAcceptEitherAsync3() {
2082 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2083 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2084 +        FailingConsumer r = new FailingConsumer();
2085 +        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2086 +        f.complete(one);
2087 +        checkCompletedWithWrappedCFException(g);
2088 +    }
2089 +
2090 +    /**
2091 +     * acceptEitherAsync result completes exceptionally if either
2092 +     * source cancelled
2093 +     */
2094 +    public void testAcceptEitherAsync4() {
2095 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2096 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2097 +        IncAction r = new IncAction();
2098 +        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2099 +        assertTrue(f.cancel(true));
2100 +        checkCompletedWithWrappedCancellationException(g);
2101 +
2102 +        r = new IncAction();
2103 +        f = new CompletableFuture<>();
2104 +        f2 = new CompletableFuture<>();
2105 +        assertTrue(f2.cancel(true));
2106 +        g = f.acceptEitherAsync(f2, r);
2107 +        checkCompletedWithWrappedCancellationException(g);
2108 +    }
2109 +
2110 +    /**
2111 +     * runAfterEitherAsync result completes normally after normal
2112 +     * completion of sources
2113 +     */
2114 +    public void testRunAfterEitherAsync() {
2115 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2116 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2117 +        Noop r = new Noop();
2118 +        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2119 +        f.complete(one);
2120 +        checkCompletedNormally(g, null);
2121 +        assertTrue(r.ran);
2122 +
2123 +        r = new Noop();
2124 +        f = new CompletableFuture<>();
2125 +        f.complete(one);
2126 +        f2 = new CompletableFuture<>();
2127 +        g = f.runAfterEitherAsync(f2, r);
2128 +        checkCompletedNormally(g, null);
2129 +        assertTrue(r.ran);
2130 +    }
2131 +
2132 +    /**
2133 +     * runAfterEitherAsync result completes exceptionally after exceptional
2134 +     * completion of source
2135 +     */
2136 +    public void testRunAfterEitherAsync2() {
2137 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2138 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2139 +        Noop r = new Noop();
2140 +        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2141 +        f.completeExceptionally(new CFException());
2142 +        checkCompletedWithWrappedCFException(g);
2143 +
2144 +        r = new Noop();
2145 +        f = new CompletableFuture<>();
2146 +        f2 = new CompletableFuture<>();
2147 +        f2.completeExceptionally(new CFException());
2148 +        g = f.runAfterEitherAsync(f2, r);
2149 +        f.complete(one);
2150 +        checkCompletedWithWrappedCFException(g);
2151 +    }
2152 +
2153 +    /**
2154 +     * runAfterEitherAsync result completes exceptionally if action does
2155 +     */
2156 +    public void testRunAfterEitherAsync3() {
2157 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2158 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2159 +        FailingNoop r = new FailingNoop();
2160 +        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2161 +        f.complete(one);
2162 +        checkCompletedWithWrappedCFException(g);
2163 +    }
2164 +
2165 +    /**
2166 +     * runAfterEitherAsync result completes exceptionally if either
2167 +     * source cancelled
2168 +     */
2169 +    public void testRunAfterEitherAsync4() {
2170 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2171 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2172 +        Noop r = new Noop();
2173 +        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2174 +        assertTrue(f.cancel(true));
2175 +        checkCompletedWithWrappedCancellationException(g);
2176 +
2177 +        r = new Noop();
2178 +        f = new CompletableFuture<>();
2179 +        f2 = new CompletableFuture<>();
2180 +        assertTrue(f2.cancel(true));
2181 +        g = f.runAfterEitherAsync(f2, r);
2182 +        checkCompletedWithWrappedCancellationException(g);
2183 +    }
2184 +
2185 +    /**
2186 +     * thenComposeAsync result completes normally after normal
2187 +     * completion of source
2188 +     */
2189 +    public void testThenComposeAsync() {
2190 +        CompletableFuture<Integer> f, g;
2191 +        CompletableFutureInc r;
2192 +
2193 +        f = new CompletableFuture<>();
2194 +        g = f.thenComposeAsync(r = new CompletableFutureInc());
2195 +        f.complete(one);
2196 +        checkCompletedNormally(g, two);
2197 +
2198 +        f = new CompletableFuture<>();
2199 +        f.complete(one);
2200 +        g = f.thenComposeAsync(r = new CompletableFutureInc());
2201 +        checkCompletedNormally(g, two);
2202 +    }
2203 +
2204 +    /**
2205 +     * thenComposeAsync result completes exceptionally after
2206 +     * exceptional completion of source
2207 +     */
2208 +    public void testThenComposeAsync2() {
2209 +        CompletableFuture<Integer> f, g;
2210 +        CompletableFutureInc r;
2211 +
2212 +        f = new CompletableFuture<>();
2213 +        g = f.thenComposeAsync(r = new CompletableFutureInc());
2214 +        f.completeExceptionally(new CFException());
2215 +        checkCompletedWithWrappedCFException(g);
2216 +        assertFalse(r.ran);
2217 +
2218 +        f = new CompletableFuture<>();
2219 +        f.completeExceptionally(new CFException());
2220 +        g = f.thenComposeAsync(r = new CompletableFutureInc());
2221 +        checkCompletedWithWrappedCFException(g);
2222 +        assertFalse(r.ran);
2223 +    }
2224 +
2225 +    /**
2226 +     * thenComposeAsync result completes exceptionally if action does
2227 +     */
2228 +    public void testThenComposeAsync3() {
2229 +        CompletableFuture<Integer> f, g;
2230 +        FailingCompletableFutureFunction r;
2231 +
2232 +        f = new CompletableFuture<>();
2233 +        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
2234 +        f.complete(one);
2235 +        checkCompletedWithWrappedCFException(g);
2236 +
2237 +        f = new CompletableFuture<>();
2238 +        f.complete(one);
2239 +        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
2240 +        checkCompletedWithWrappedCFException(g);
2241 +    }
2242 +
2243 +    /**
2244 +     * thenComposeAsync result completes exceptionally if source cancelled
2245 +     */
2246 +    public void testThenComposeAsync4() {
2247 +        CompletableFuture<Integer> f, g;
2248 +        CompletableFutureInc r;
2249 +
2250 +        f = new CompletableFuture<>();
2251 +        g = f.thenComposeAsync(r = new CompletableFutureInc());
2252 +        assertTrue(f.cancel(true));
2253 +        checkCompletedWithWrappedCancellationException(g);
2254 +
2255 +        f = new CompletableFuture<>();
2256 +        assertTrue(f.cancel(true));
2257 +        g = f.thenComposeAsync(r = new CompletableFutureInc());
2258 +        checkCompletedWithWrappedCancellationException(g);
2259 +    }
2260 +
2261 +    // async with explicit executors
2262 +
2263 +    /**
2264 +     * thenRunAsync result completes normally after normal completion of source
2265 +     */
2266 +    public void testThenRunAsyncE() {
2267 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2268 +        Noop r = new Noop();
2269 +        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2270 +        f.complete(null);
2271 +        checkCompletedNormally(g, null);
2272 +
2273 +        // reordered version
2274 +        f = new CompletableFuture<>();
2275 +        f.complete(null);
2276 +        r = new Noop();
2277 +        g = f.thenRunAsync(r, new ThreadExecutor());
2278 +        checkCompletedNormally(g, null);
2279 +    }
2280 +
2281 +    /**
2282 +     * thenRunAsync result completes exceptionally after exceptional
2283 +     * completion of source
2284 +     */
2285 +    public void testThenRunAsync2E() {
2286 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2287 +        Noop r = new Noop();
2288 +        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2289 +        f.completeExceptionally(new CFException());
2290 +        try {
2291 +            g.join();
2292 +            shouldThrow();
2293 +        } catch (CompletionException success) {}
2294 +        checkCompletedWithWrappedCFException(g);
2295 +    }
2296 +
2297 +    /**
2298 +     * thenRunAsync result completes exceptionally if action does
2299 +     */
2300 +    public void testThenRunAsync3E() {
2301 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2302 +        FailingNoop r = new FailingNoop();
2303 +        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2304 +        f.complete(null);
2305 +        checkCompletedWithWrappedCFException(g);
2306 +    }
2307 +
2308 +    /**
2309 +     * thenRunAsync result completes exceptionally if source cancelled
2310 +     */
2311 +    public void testThenRunAsync4E() {
2312 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2313 +        Noop r = new Noop();
2314 +        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2315 +        assertTrue(f.cancel(true));
2316 +        checkCompletedWithWrappedCancellationException(g);
2317 +    }
2318 +
2319 +    /**
2320 +     * thenApplyAsync result completes normally after normal completion of source
2321 +     */
2322 +    public void testThenApplyAsyncE() {
2323 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2324 +        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
2325 +        f.complete(one);
2326 +        checkCompletedNormally(g, two);
2327 +    }
2328 +
2329 +    /**
2330 +     * thenApplyAsync result completes exceptionally after exceptional
2331 +     * completion of source
2332 +     */
2333 +    public void testThenApplyAsync2E() {
2334 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2335 +        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
2336 +        f.completeExceptionally(new CFException());
2337 +        checkCompletedWithWrappedCFException(g);
2338 +    }
2339 +
2340 +    /**
2341 +     * thenApplyAsync result completes exceptionally if action does
2342 +     */
2343 +    public void testThenApplyAsync3E() {
2344 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2345 +        FailingFunction r = new FailingFunction();
2346 +        CompletableFuture<Integer> g = f.thenApplyAsync(r, new ThreadExecutor());
2347 +        f.complete(null);
2348 +        checkCompletedWithWrappedCFException(g);
2349 +    }
2350 +
2351 +    /**
2352 +     * thenApplyAsync result completes exceptionally if source cancelled
2353 +     */
2354 +    public void testThenApplyAsync4E() {
2355 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2356 +        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
2357 +        assertTrue(f.cancel(true));
2358 +        checkCompletedWithWrappedCancellationException(g);
2359 +    }
2360 +
2361 +    /**
2362 +     * thenAcceptAsync result completes normally after normal
2363 +     * completion of source
2364 +     */
2365 +    public void testThenAcceptAsyncE() {
2366 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2367 +        IncAction r = new IncAction();
2368 +        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2369 +        f.complete(one);
2370 +        checkCompletedNormally(g, null);
2371 +        assertEquals(r.value, 2);
2372 +    }
2373 +
2374 +    /**
2375 +     * thenAcceptAsync result completes exceptionally after exceptional
2376 +     * completion of source
2377 +     */
2378 +    public void testThenAcceptAsync2E() {
2379 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2380 +        IncAction r = new IncAction();
2381 +        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2382 +        f.completeExceptionally(new CFException());
2383 +        checkCompletedWithWrappedCFException(g);
2384 +    }
2385 +
2386 +    /**
2387 +     * thenAcceptAsync result completes exceptionally if action does
2388 +     */
2389 +    public void testThenAcceptAsync3E() {
2390 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2391 +        FailingConsumer r = new FailingConsumer();
2392 +        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2393 +        f.complete(null);
2394 +        checkCompletedWithWrappedCFException(g);
2395 +    }
2396 +
2397 +    /**
2398 +     * thenAcceptAsync result completes exceptionally if source cancelled
2399 +     */
2400 +    public void testThenAcceptAsync4E() {
2401 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2402 +        IncAction r = new IncAction();
2403 +        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2404 +        assertTrue(f.cancel(true));
2405 +        checkCompletedWithWrappedCancellationException(g);
2406 +    }
2407 +
2408 +    /**
2409 +     * thenCombineAsync result completes normally after normal
2410 +     * completion of sources
2411 +     */
2412 +    public void testThenCombineAsyncE() {
2413 +        CompletableFuture<Integer> f, g, h;
2414 +        ThreadExecutor e = new ThreadExecutor();
2415 +        int count = 0;
2416 +
2417 +        f = new CompletableFuture<>();
2418 +        g = new CompletableFuture<>();
2419 +        h = f.thenCombineAsync(g, subtract, e);
2420 +        f.complete(3);
2421 +        checkIncomplete(h);
2422 +        g.complete(1);
2423 +        checkCompletedNormally(h, 2);
2424 +        assertEquals(++count, e.count.get());
2425 +
2426 +        f = new CompletableFuture<>();
2427 +        g = new CompletableFuture<>();
2428 +        h = f.thenCombineAsync(g, subtract, e);
2429 +        g.complete(1);
2430 +        checkIncomplete(h);
2431 +        f.complete(3);
2432 +        checkCompletedNormally(h, 2);
2433 +        assertEquals(++count, e.count.get());
2434 +
2435 +        f = new CompletableFuture<>();
2436 +        g = new CompletableFuture<>();
2437 +        g.complete(1);
2438 +        f.complete(3);
2439 +        h = f.thenCombineAsync(g, subtract, e);
2440 +        checkCompletedNormally(h, 2);
2441 +        assertEquals(++count, e.count.get());
2442 +    }
2443 +
2444 +    /**
2445 +     * thenCombineAsync result completes exceptionally after exceptional
2446 +     * completion of either source
2447 +     */
2448 +    public void testThenCombineAsync2E() {
2449 +        CompletableFuture<Integer> f, g, h;
2450 +        ThreadExecutor e = new ThreadExecutor();
2451 +        int count = 0;
2452 +
2453 +        f = new CompletableFuture<>();
2454 +        g = new CompletableFuture<>();
2455 +        h = f.thenCombineAsync(g, subtract, e);
2456 +        f.completeExceptionally(new CFException());
2457 +        checkIncomplete(h);
2458 +        g.complete(1);
2459 +        checkCompletedWithWrappedCFException(h);
2460 +
2461 +        f = new CompletableFuture<>();
2462 +        g = new CompletableFuture<>();
2463 +        h = f.thenCombineAsync(g, subtract, e);
2464 +        g.completeExceptionally(new CFException());
2465 +        checkIncomplete(h);
2466 +        f.complete(3);
2467 +        checkCompletedWithWrappedCFException(h);
2468 +
2469 +        f = new CompletableFuture<>();
2470 +        g = new CompletableFuture<>();
2471 +        g.completeExceptionally(new CFException());
2472 +        h = f.thenCombineAsync(g, subtract, e);
2473 +        checkIncomplete(h);
2474 +        f.complete(3);
2475 +        checkCompletedWithWrappedCFException(h);
2476 +
2477 +        assertEquals(0, e.count.get());
2478 +    }
2479 +
2480 +    /**
2481 +     * thenCombineAsync result completes exceptionally if action does
2482 +     */
2483 +    public void testThenCombineAsync3E() {
2484 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2485 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2486 +        FailingBiFunction r = new FailingBiFunction();
2487 +        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r, new ThreadExecutor());
2488 +        f.complete(one);
2489 +        checkIncomplete(g);
2490 +        assertFalse(r.ran);
2491 +        f2.complete(two);
2492 +        checkCompletedWithWrappedCFException(g);
2493 +        assertTrue(r.ran);
2494 +    }
2495 +
2496 +    /**
2497 +     * thenCombineAsync result completes exceptionally if either source cancelled
2498 +     */
2499 +    public void testThenCombineAsync4E() {
2500 +        CompletableFuture<Integer> f, g, h;
2501 +        ThreadExecutor e = new ThreadExecutor();
2502 +
2503 +        f = new CompletableFuture<>();
2504 +        g = new CompletableFuture<>();
2505 +        h = f.thenCombineAsync(g, subtract, e);
2506 +        assertTrue(f.cancel(true));
2507 +        checkIncomplete(h);
2508 +        g.complete(1);
2509 +        checkCompletedWithWrappedCancellationException(h);
2510 +
2511 +        f = new CompletableFuture<>();
2512 +        g = new CompletableFuture<>();
2513 +        h = f.thenCombineAsync(g, subtract, e);
2514 +        assertTrue(g.cancel(true));
2515 +        checkIncomplete(h);
2516 +        f.complete(3);
2517 +        checkCompletedWithWrappedCancellationException(h);
2518 +
2519 +        f = new CompletableFuture<>();
2520 +        g = new CompletableFuture<>();
2521 +        assertTrue(g.cancel(true));
2522 +        h = f.thenCombineAsync(g, subtract, e);
2523 +        checkIncomplete(h);
2524 +        f.complete(3);
2525 +        checkCompletedWithWrappedCancellationException(h);
2526 +
2527 +        f = new CompletableFuture<>();
2528 +        g = new CompletableFuture<>();
2529 +        assertTrue(f.cancel(true));
2530 +        assertTrue(g.cancel(true));
2531 +        h = f.thenCombineAsync(g, subtract, e);
2532 +        checkCompletedWithWrappedCancellationException(h);
2533 +
2534 +        assertEquals(0, e.count.get());
2535 +    }
2536 +
2537 +    /**
2538 +     * thenAcceptBothAsync result completes normally after normal
2539 +     * completion of sources
2540 +     */
2541 +    public void testThenAcceptBothAsyncE() {
2542 +        CompletableFuture<Integer> f, g;
2543 +        CompletableFuture<Void> h;
2544 +        SubtractAction r;
2545 +        ThreadExecutor e = new ThreadExecutor();
2546 +
2547 +        f = new CompletableFuture<>();
2548 +        g = new CompletableFuture<>();
2549 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2550 +        f.complete(3);
2551 +        checkIncomplete(h);
2552 +        g.complete(1);
2553 +        checkCompletedNormally(h, null);
2554 +        assertEquals(r.value, 2);
2555 +
2556 +        f = new CompletableFuture<>();
2557 +        g = new CompletableFuture<>();
2558 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2559 +        g.complete(1);
2560 +        checkIncomplete(h);
2561 +        f.complete(3);
2562 +        checkCompletedNormally(h, null);
2563 +        assertEquals(r.value, 2);
2564 +
2565 +        f = new CompletableFuture<>();
2566 +        g = new CompletableFuture<>();
2567 +        g.complete(1);
2568 +        f.complete(3);
2569 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2570 +        checkCompletedNormally(h, null);
2571 +        assertEquals(r.value, 2);
2572 +
2573 +        assertEquals(3, e.count.get());
2574 +    }
2575 +
2576 +    /**
2577 +     * thenAcceptBothAsync result completes exceptionally after exceptional
2578 +     * completion of source
2579 +     */
2580 +    public void testThenAcceptBothAsync2E() {
2581 +        CompletableFuture<Integer> f, g;
2582 +        CompletableFuture<Void> h;
2583 +        SubtractAction r;
2584 +        ThreadExecutor e = new ThreadExecutor();
2585 +
2586 +        f = new CompletableFuture<>();
2587 +        g = new CompletableFuture<>();
2588 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2589 +        f.completeExceptionally(new CFException());
2590 +        checkIncomplete(h);
2591 +        g.complete(1);
2592 +        checkCompletedWithWrappedCFException(h);
2593 +
2594 +        f = new CompletableFuture<>();
2595 +        g = new CompletableFuture<>();
2596 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2597 +        g.completeExceptionally(new CFException());
2598 +        checkIncomplete(h);
2599 +        f.complete(3);
2600 +        checkCompletedWithWrappedCFException(h);
2601 +
2602 +        f = new CompletableFuture<>();
2603 +        g = new CompletableFuture<>();
2604 +        f.complete(3);
2605 +        g.completeExceptionally(new CFException());
2606 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2607 +        checkCompletedWithWrappedCFException(h);
2608 +
2609 +        f = new CompletableFuture<>();
2610 +        g = new CompletableFuture<>();
2611 +        f.completeExceptionally(new CFException());
2612 +        g.complete(3);
2613 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2614 +        checkCompletedWithWrappedCFException(h);
2615 +
2616 +        assertEquals(0, e.count.get());
2617 +    }
2618 +
2619 +    /**
2620 +     * thenAcceptBothAsync result completes exceptionally if action does
2621 +     */
2622 +    public void testThenAcceptBothAsync3E() {
2623 +        CompletableFuture<Integer> f, g;
2624 +        CompletableFuture<Void> h;
2625 +        FailingBiConsumer r;
2626 +        ThreadExecutor e = new ThreadExecutor();
2627 +
2628 +        f = new CompletableFuture<>();
2629 +        g = new CompletableFuture<>();
2630 +        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer(), e);
2631 +        f.complete(3);
2632 +        checkIncomplete(h);
2633 +        g.complete(1);
2634 +        checkCompletedWithWrappedCFException(h);
2635 +
2636 +        f = new CompletableFuture<>();
2637 +        g = new CompletableFuture<>();
2638 +        f.complete(3);
2639 +        g.complete(1);
2640 +        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer(), e);
2641 +        checkCompletedWithWrappedCFException(h);
2642 +
2643 +        assertEquals(2, e.count.get());
2644 +    }
2645 +
2646 +    /**
2647 +     * thenAcceptBothAsync result completes exceptionally if either source cancelled
2648 +     */
2649 +    public void testThenAcceptBothAsync4E() {
2650 +        CompletableFuture<Integer> f, g;
2651 +        CompletableFuture<Void> h;
2652 +        SubtractAction r;
2653 +        ThreadExecutor e = new ThreadExecutor();
2654 +
2655 +        f = new CompletableFuture<>();
2656 +        g = new CompletableFuture<>();
2657 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2658 +        assertTrue(f.cancel(true));
2659 +        checkIncomplete(h);
2660 +        g.complete(1);
2661 +        checkCompletedWithWrappedCancellationException(h);
2662 +
2663 +        f = new CompletableFuture<>();
2664 +        g = new CompletableFuture<>();
2665 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2666 +        assertTrue(g.cancel(true));
2667 +        checkIncomplete(h);
2668 +        f.complete(3);
2669 +        checkCompletedWithWrappedCancellationException(h);
2670 +
2671 +        f = new CompletableFuture<>();
2672 +        g = new CompletableFuture<>();
2673 +        f.complete(3);
2674 +        assertTrue(g.cancel(true));
2675 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2676 +        checkCompletedWithWrappedCancellationException(h);
2677 +
2678 +        f = new CompletableFuture<>();
2679 +        g = new CompletableFuture<>();
2680 +        assertTrue(f.cancel(true));
2681 +        g.complete(3);
2682 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2683 +        checkCompletedWithWrappedCancellationException(h);
2684 +
2685 +        assertEquals(0, e.count.get());
2686 +    }
2687 +
2688 +    /**
2689 +     * applyToEitherAsync result completes normally after normal
2690 +     * completion of sources
2691 +     */
2692 +    public void testApplyToEitherAsyncE() {
2693 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2694 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2695 +        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2696 +        f.complete(one);
2697 +        checkCompletedNormally(g, two);
2698 +
2699 +        f = new CompletableFuture<>();
2700 +        f.complete(one);
2701 +        f2 = new CompletableFuture<>();
2702 +        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2703 +        checkCompletedNormally(g, two);
2704 +    }
2705 +
2706 +    /**
2707 +     * applyToEitherAsync result completes exceptionally after exceptional
2708 +     * completion of source
2709 +     */
2710 +    public void testApplyToEitherAsync2E() {
2711 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2712 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2713 +        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2714 +        f.completeExceptionally(new CFException());
2715 +        checkCompletedWithWrappedCFException(g);
2716 +
2717 +        f = new CompletableFuture<>();
2718 +        f2 = new CompletableFuture<>();
2719 +        f2.completeExceptionally(new CFException());
2720 +        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2721 +        f.complete(one);
2722 +        checkCompletedWithWrappedCFException(g);
2723 +    }
2724 +
2725 +    /**
2726 +     * applyToEitherAsync result completes exceptionally if action does
2727 +     */
2728 +    public void testApplyToEitherAsync3E() {
2729 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2730 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2731 +        FailingFunction r = new FailingFunction();
2732 +        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor());
2733 +        f.complete(one);
2734 +        checkCompletedWithWrappedCFException(g);
2735 +    }
2736 +
2737 +    /**
2738 +     * applyToEitherAsync result completes exceptionally if either source cancelled
2739 +     */
2740 +    public void testApplyToEitherAsync4E() {
2741 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2742 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2743 +        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2744 +        assertTrue(f.cancel(true));
2745 +        checkCompletedWithWrappedCancellationException(g);
2746 +
2747 +        f = new CompletableFuture<>();
2748 +        f2 = new CompletableFuture<>();
2749 +        assertTrue(f2.cancel(true));
2750 +        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2751 +        checkCompletedWithWrappedCancellationException(g);
2752 +    }
2753 +
2754 +    /**
2755 +     * acceptEitherAsync result completes normally after normal
2756 +     * completion of sources
2757 +     */
2758 +    public void testAcceptEitherAsyncE() {
2759 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2760 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2761 +        IncAction r = new IncAction();
2762 +        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2763 +        f.complete(one);
2764 +        checkCompletedNormally(g, null);
2765 +        assertEquals(r.value, 2);
2766 +
2767 +        r = new IncAction();
2768 +        f = new CompletableFuture<>();
2769 +        f.complete(one);
2770 +        f2 = new CompletableFuture<>();
2771 +        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2772 +        checkCompletedNormally(g, null);
2773 +        assertEquals(r.value, 2);
2774 +    }
2775 +
2776 +    /**
2777 +     * acceptEitherAsync result completes exceptionally after exceptional
2778 +     * completion of source
2779 +     */
2780 +    public void testAcceptEitherAsync2E() {
2781 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2782 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2783 +        IncAction r = new IncAction();
2784 +        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2785 +        f.completeExceptionally(new CFException());
2786 +        checkCompletedWithWrappedCFException(g);
2787 +
2788 +        r = new IncAction();
2789 +        f = new CompletableFuture<>();
2790 +        f2 = new CompletableFuture<>();
2791 +        f2.completeExceptionally(new CFException());
2792 +        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2793 +        f.complete(one);
2794 +        checkCompletedWithWrappedCFException(g);
2795 +    }
2796 +
2797 +    /**
2798 +     * acceptEitherAsync result completes exceptionally if action does
2799 +     */
2800 +    public void testAcceptEitherAsync3E() {
2801 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2802 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2803 +        FailingConsumer r = new FailingConsumer();
2804 +        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2805 +        f.complete(one);
2806 +        checkCompletedWithWrappedCFException(g);
2807 +    }
2808 +
2809 +    /**
2810 +     * acceptEitherAsync result completes exceptionally if either
2811 +     * source cancelled
2812 +     */
2813 +    public void testAcceptEitherAsync4E() {
2814 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2815 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2816 +        IncAction r = new IncAction();
2817 +        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2818 +        assertTrue(f.cancel(true));
2819 +        checkCompletedWithWrappedCancellationException(g);
2820 +
2821 +        r = new IncAction();
2822 +        f = new CompletableFuture<>();
2823 +        f2 = new CompletableFuture<>();
2824 +        assertTrue(f2.cancel(true));
2825 +        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2826 +        checkCompletedWithWrappedCancellationException(g);
2827 +    }
2828 +
2829 +    /**
2830 +     * runAfterEitherAsync result completes normally after normal
2831 +     * completion of sources
2832 +     */
2833 +    public void testRunAfterEitherAsyncE() {
2834 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2835 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2836 +        Noop r = new Noop();
2837 +        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2838 +        f.complete(one);
2839 +        checkCompletedNormally(g, null);
2840 +        assertTrue(r.ran);
2841 +
2842 +        r = new Noop();
2843 +        f = new CompletableFuture<>();
2844 +        f.complete(one);
2845 +        f2 = new CompletableFuture<>();
2846 +        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2847 +        checkCompletedNormally(g, null);
2848 +        assertTrue(r.ran);
2849 +    }
2850 +
2851 +    /**
2852 +     * runAfterEitherAsync result completes exceptionally after exceptional
2853 +     * completion of source
2854 +     */
2855 +    public void testRunAfterEitherAsync2E() {
2856 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2857 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2858 +        Noop r = new Noop();
2859 +        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2860 +        f.completeExceptionally(new CFException());
2861 +        checkCompletedWithWrappedCFException(g);
2862 +
2863 +        r = new Noop();
2864 +        f = new CompletableFuture<>();
2865 +        f2 = new CompletableFuture<>();
2866 +        f2.completeExceptionally(new CFException());
2867 +        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2868 +        f.complete(one);
2869 +        checkCompletedWithWrappedCFException(g);
2870 +    }
2871 +
2872 +    /**
2873 +     * runAfterEitherAsync result completes exceptionally if action does
2874 +     */
2875 +    public void testRunAfterEitherAsync3E() {
2876 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2877 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2878 +        FailingNoop r = new FailingNoop();
2879 +        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2880 +        f.complete(one);
2881 +        checkCompletedWithWrappedCFException(g);
2882 +    }
2883 +
2884 +    /**
2885 +     * runAfterEitherAsync result completes exceptionally if either
2886 +     * source cancelled
2887 +     */
2888 +    public void testRunAfterEitherAsync4E() {
2889 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2890 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2891 +        Noop r = new Noop();
2892 +        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2893 +        assertTrue(f.cancel(true));
2894 +        checkCompletedWithWrappedCancellationException(g);
2895 +
2896 +        r = new Noop();
2897 +        f = new CompletableFuture<>();
2898 +        f2 = new CompletableFuture<>();
2899 +        assertTrue(f2.cancel(true));
2900 +        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2901 +        checkCompletedWithWrappedCancellationException(g);
2902 +    }
2903 +
2904 +    /**
2905 +     * thenComposeAsync result completes normally after normal
2906 +     * completion of source
2907 +     */
2908 +    public void testThenComposeAsyncE() {
2909 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2910 +        CompletableFutureInc r = new CompletableFutureInc();
2911 +        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2912 +        f.complete(one);
2913 +        checkCompletedNormally(g, two);
2914 +    }
2915 +
2916 +    /**
2917 +     * thenComposeAsync result completes exceptionally after
2918 +     * exceptional completion of source
2919 +     */
2920 +    public void testThenComposeAsync2E() {
2921 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2922 +        CompletableFutureInc r = new CompletableFutureInc();
2923 +        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2924 +        f.completeExceptionally(new CFException());
2925 +        checkCompletedWithWrappedCFException(g);
2926 +    }
2927 +
2928 +    /**
2929 +     * thenComposeAsync result completes exceptionally if action does
2930 +     */
2931 +    public void testThenComposeAsync3E() {
2932 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2933 +        FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
2934 +        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2935 +        f.complete(one);
2936 +        checkCompletedWithWrappedCFException(g);
2937 +    }
2938 +
2939 +    /**
2940 +     * thenComposeAsync result completes exceptionally if source cancelled
2941 +     */
2942 +    public void testThenComposeAsync4E() {
2943 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2944 +        CompletableFutureInc r = new CompletableFutureInc();
2945 +        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2946 +        assertTrue(f.cancel(true));
2947 +        checkCompletedWithWrappedCancellationException(g);
2948 +    }
2949 +
2950 +    // other static methods
2951 +
2952 +    /**
2953 +     * allOf(no component futures) returns a future completed normally
2954 +     * with the value null
2955 +     */
2956 +    public void testAllOf_empty() throws Exception {
2957 +        CompletableFuture<Void> f = CompletableFuture.allOf();
2958 +        checkCompletedNormally(f, null);
2959 +    }
2960 +
2961 +    /**
2962 +     * allOf returns a future completed normally with the value null
2963 +     * when all components complete normally
2964 +     */
2965 +    public void testAllOf_normal() throws Exception {
2966 +        for (int k = 1; k < 20; ++k) {
2967 +            CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2968 +            for (int i = 0; i < k; ++i)
2969 +                fs[i] = new CompletableFuture<>();
2970 +            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
2971 +            for (int i = 0; i < k; ++i) {
2972 +                checkIncomplete(f);
2973 +                checkIncomplete(CompletableFuture.allOf(fs));
2974 +                fs[i].complete(one);
2975 +            }
2976 +            checkCompletedNormally(f, null);
2977 +            checkCompletedNormally(CompletableFuture.allOf(fs), null);
2978 +        }
2979 +    }
2980 +
2981 +    /**
2982 +     * anyOf(no component futures) returns an incomplete future
2983 +     */
2984 +    public void testAnyOf_empty() throws Exception {
2985 +        CompletableFuture<Object> f = CompletableFuture.anyOf();
2986 +        checkIncomplete(f);
2987 +    }
2988 +
2989 +    /**
2990 +     * anyOf returns a future completed normally with a value when
2991 +     * a component future does
2992 +     */
2993 +    public void testAnyOf_normal() throws Exception {
2994 +        for (int k = 0; k < 10; ++k) {
2995 +            CompletableFuture[] fs = new CompletableFuture[k];
2996 +            for (int i = 0; i < k; ++i)
2997 +                fs[i] = new CompletableFuture<>();
2998 +            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
2999 +            checkIncomplete(f);
3000 +            for (int i = 0; i < k; ++i) {
3001 +                fs[i].complete(one);
3002 +                checkCompletedNormally(f, one);
3003 +                checkCompletedNormally(CompletableFuture.anyOf(fs), one);
3004 +            }
3005 +        }
3006 +    }
3007 +
3008 +    /**
3009 +     * anyOf result completes exceptionally when any component does.
3010 +     */
3011 +    public void testAnyOf_exceptional() throws Exception {
3012 +        for (int k = 0; k < 10; ++k) {
3013 +            CompletableFuture[] fs = new CompletableFuture[k];
3014 +            for (int i = 0; i < k; ++i)
3015 +                fs[i] = new CompletableFuture<>();
3016 +            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3017 +            checkIncomplete(f);
3018 +            for (int i = 0; i < k; ++i) {
3019 +                fs[i].completeExceptionally(new CFException());
3020 +                checkCompletedWithWrappedCFException(f);
3021 +                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3022 +            }
3023 +        }
3024 +    }
3025 +
3026 +    /**
3027 +     * Completion methods throw NullPointerException with null arguments
3028 +     */
3029 +    public void testNPE() {
3030 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3031 +        CompletableFuture<Integer> g = new CompletableFuture<>();
3032 +        CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null;
3033 +        CompletableFuture<?> h;
3034 +        ThreadExecutor exec = new ThreadExecutor();
3035 +
3036 +        Runnable[] throwingActions = {
3037 +            () -> CompletableFuture.supplyAsync(null),
3038 +            () -> CompletableFuture.supplyAsync(null, exec),
3039 +            () -> CompletableFuture.supplyAsync(supplyOne, null),
3040 +
3041 +            () -> CompletableFuture.runAsync(null),
3042 +            () -> CompletableFuture.runAsync(null, exec),
3043 +            () -> CompletableFuture.runAsync(() -> {}, null),
3044 +
3045 +            () -> f.completeExceptionally(null),
3046 +
3047 +            () -> f.thenApply(null),
3048 +            () -> f.thenApplyAsync(null),
3049 +            () -> f.thenApplyAsync((x) -> x, null),
3050 +            () -> f.thenApplyAsync(null, exec),
3051 +
3052 +            () -> f.thenAccept(null),
3053 +            () -> f.thenAcceptAsync(null),
3054 +            () -> f.thenAcceptAsync((x) -> {} , null),
3055 +            () -> f.thenAcceptAsync(null, exec),
3056 +
3057 +            () -> f.thenRun(null),
3058 +            () -> f.thenRunAsync(null),
3059 +            () -> f.thenRunAsync(() -> {} , null),
3060 +            () -> f.thenRunAsync(null, exec),
3061 +
3062 +            () -> f.thenCombine(g, null),
3063 +            () -> f.thenCombineAsync(g, null),
3064 +            () -> f.thenCombineAsync(g, null, exec),
3065 +            () -> f.thenCombine(nullFuture, (x, y) -> x),
3066 +            () -> f.thenCombineAsync(nullFuture, (x, y) -> x),
3067 +            () -> f.thenCombineAsync(nullFuture, (x, y) -> x, exec),
3068 +            () -> f.thenCombineAsync(g, (x, y) -> x, null),
3069 +
3070 +            () -> f.thenAcceptBoth(g, null),
3071 +            () -> f.thenAcceptBothAsync(g, null),
3072 +            () -> f.thenAcceptBothAsync(g, null, exec),
3073 +            () -> f.thenAcceptBoth(nullFuture, (x, y) -> {}),
3074 +            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}),
3075 +            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec),
3076 +            () -> f.thenAcceptBothAsync(g, (x, y) -> {}, null),
3077 +
3078 +            () -> f.runAfterBoth(g, null),
3079 +            () -> f.runAfterBothAsync(g, null),
3080 +            () -> f.runAfterBothAsync(g, null, exec),
3081 +            () -> f.runAfterBoth(nullFuture, () -> {}),
3082 +            () -> f.runAfterBothAsync(nullFuture, () -> {}),
3083 +            () -> f.runAfterBothAsync(nullFuture, () -> {}, exec),
3084 +            () -> f.runAfterBothAsync(g, () -> {}, null),
3085 +
3086 +            () -> f.applyToEither(g, null),
3087 +            () -> f.applyToEitherAsync(g, null),
3088 +            () -> f.applyToEitherAsync(g, null, exec),
3089 +            () -> f.applyToEither(nullFuture, (x) -> x),
3090 +            () -> f.applyToEitherAsync(nullFuture, (x) -> x),
3091 +            () -> f.applyToEitherAsync(nullFuture, (x) -> x, exec),
3092 +            () -> f.applyToEitherAsync(g, (x) -> x, null),
3093 +
3094 +            () -> f.acceptEither(g, null),
3095 +            () -> f.acceptEitherAsync(g, null),
3096 +            () -> f.acceptEitherAsync(g, null, exec),
3097 +            () -> f.acceptEither(nullFuture, (x) -> {}),
3098 +            () -> f.acceptEitherAsync(nullFuture, (x) -> {}),
3099 +            () -> f.acceptEitherAsync(nullFuture, (x) -> {}, exec),
3100 +            () -> f.acceptEitherAsync(g, (x) -> {}, null),
3101 +
3102 +            () -> f.runAfterEither(g, null),
3103 +            () -> f.runAfterEitherAsync(g, null),
3104 +            () -> f.runAfterEitherAsync(g, null, exec),
3105 +            () -> f.runAfterEither(nullFuture, () -> {}),
3106 +            () -> f.runAfterEitherAsync(nullFuture, () -> {}),
3107 +            () -> f.runAfterEitherAsync(nullFuture, () -> {}, exec),
3108 +            () -> f.runAfterEitherAsync(g, () -> {}, null),
3109 +
3110 +            () -> f.thenCompose(null),
3111 +            () -> f.thenComposeAsync(null),
3112 +            () -> f.thenComposeAsync(new CompletableFutureInc(), null),
3113 +            () -> f.thenComposeAsync(null, exec),
3114 +
3115 +            () -> f.exceptionally(null),
3116 +
3117 +            () -> f.handle(null),
3118 +
3119 +            () -> CompletableFuture.allOf((CompletableFuture<?>)null),
3120 +            () -> CompletableFuture.allOf((CompletableFuture<?>[])null),
3121 +            () -> CompletableFuture.allOf(f, null),
3122 +            () -> CompletableFuture.allOf(null, f),
3123 +
3124 +            () -> CompletableFuture.anyOf((CompletableFuture<?>)null),
3125 +            () -> CompletableFuture.anyOf((CompletableFuture<?>[])null),
3126 +            () -> CompletableFuture.anyOf(f, null),
3127 +            () -> CompletableFuture.anyOf(null, f),
3128 +
3129 +            () -> f.obtrudeException(null),
3130 +        };
3131 +
3132 +        assertThrows(NullPointerException.class, throwingActions);
3133 +        assertEquals(0, exec.count.get());
3134 +    }
3135 +
3136 +    /**
3137 +     * toCompletableFuture returns this CompletableFuture.
3138 +     */
3139 +    public void testToCompletableFuture() {
3140 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3141 +        assertSame(f, f.toCompletableFuture());
3142 +    }
3143 +
3144 +    /**
3145 +     * whenComplete action executes on normal completion, propagating
3146 +     * source result.
3147 +     */
3148 +    public void testWhenComplete1() {
3149 +        final AtomicInteger a = new AtomicInteger();
3150 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3151 +        CompletableFuture<Integer> g =
3152 +            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3153 +        f.complete(three);
3154 +        checkCompletedNormally(f, three);
3155 +        checkCompletedNormally(g, three);
3156 +        assertEquals(a.get(), 1);
3157 +    }
3158 +
3159 +    /**
3160 +     * whenComplete action executes on exceptional completion, propagating
3161 +     * source result.
3162 +     */
3163 +    public void testWhenComplete2() {
3164 +        final AtomicInteger a = new AtomicInteger();
3165 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3166 +        CompletableFuture<Integer> g =
3167 +            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3168 +        f.completeExceptionally(new CFException());
3169 +        assertTrue(f.isCompletedExceptionally());
3170 +        assertTrue(g.isCompletedExceptionally());
3171 +        assertEquals(a.get(), 1);
3172 +    }
3173 +
3174 +    /**
3175 +     * If a whenComplete action throws an exception when triggered by
3176 +     * a normal completion, it completes exceptionally
3177 +     */
3178 +    public void testWhenComplete3() {
3179 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3180 +        CompletableFuture<Integer> g =
3181 +            f.whenComplete((Integer x, Throwable t) ->
3182 +                           { throw new CFException(); } );
3183 +        f.complete(three);
3184 +        checkCompletedNormally(f, three);
3185 +        assertTrue(g.isCompletedExceptionally());
3186 +        checkCompletedWithWrappedCFException(g);
3187 +    }
3188 +
3189 +    /**
3190 +     * whenCompleteAsync action executes on normal completion, propagating
3191 +     * source result.
3192 +     */
3193 +    public void testWhenCompleteAsync1() {
3194 +        final AtomicInteger a = new AtomicInteger();
3195 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3196 +        CompletableFuture<Integer> g =
3197 +            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3198 +        f.complete(three);
3199 +        checkCompletedNormally(f, three);
3200 +        checkCompletedNormally(g, three);
3201 +        assertEquals(a.get(), 1);
3202 +    }
3203 +
3204 +    /**
3205 +     * whenCompleteAsync action executes on exceptional completion, propagating
3206 +     * source result.
3207 +     */
3208 +    public void testWhenCompleteAsync2() {
3209 +        final AtomicInteger a = new AtomicInteger();
3210 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3211 +        CompletableFuture<Integer> g =
3212 +            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3213 +        f.completeExceptionally(new CFException());
3214 +        checkCompletedWithWrappedCFException(f);
3215 +        checkCompletedWithWrappedCFException(g);
3216 +    }
3217 +
3218 +    /**
3219 +     * If a whenCompleteAsync action throws an exception when
3220 +     * triggered by a normal completion, it completes exceptionally
3221 +     */
3222 +    public void testWhenCompleteAsync3() {
3223 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3224 +        CompletableFuture<Integer> g =
3225 +            f.whenCompleteAsync((Integer x, Throwable t) ->
3226 +                           { throw new CFException(); } );
3227 +        f.complete(three);
3228 +        checkCompletedNormally(f, three);
3229 +        checkCompletedWithWrappedCFException(g);
3230 +    }
3231 +
3232 +    /**
3233 +     * whenCompleteAsync action executes on normal completion, propagating
3234 +     * source result.
3235 +     */
3236 +    public void testWhenCompleteAsync1e() {
3237 +        final AtomicInteger a = new AtomicInteger();
3238 +        ThreadExecutor exec = new ThreadExecutor();
3239 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3240 +        CompletableFuture<Integer> g =
3241 +            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3242 +                                exec);
3243 +        f.complete(three);
3244 +        checkCompletedNormally(f, three);
3245 +        checkCompletedNormally(g, three);
3246 +        assertEquals(a.get(), 1);
3247 +    }
3248 +
3249 +    /**
3250 +     * whenCompleteAsync action executes on exceptional completion, propagating
3251 +     * source result.
3252 +     */
3253 +    public void testWhenCompleteAsync2e() {
3254 +        final AtomicInteger a = new AtomicInteger();
3255 +        ThreadExecutor exec = new ThreadExecutor();
3256 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3257 +        CompletableFuture<Integer> g =
3258 +            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3259 +                                exec);
3260 +        f.completeExceptionally(new CFException());
3261 +        checkCompletedWithWrappedCFException(f);
3262 +        checkCompletedWithWrappedCFException(g);
3263 +    }
3264 +
3265 +    /**
3266 +     * If a whenCompleteAsync action throws an exception when triggered
3267 +     * by a normal completion, it completes exceptionally
3268 +     */
3269 +    public void testWhenCompleteAsync3e() {
3270 +        ThreadExecutor exec = new ThreadExecutor();
3271 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3272 +        CompletableFuture<Integer> g =
3273 +            f.whenCompleteAsync((Integer x, Throwable t) ->
3274 +                                { throw new CFException(); },
3275 +                                exec);
3276 +        f.complete(three);
3277 +        checkCompletedNormally(f, three);
3278 +        checkCompletedWithWrappedCFException(g);
3279 +    }
3280 +
3281 +    /**
3282 +     * handleAsync action completes normally with function value on
3283 +     * either normal or exceptional completion of source
3284 +     */
3285 +    public void testHandleAsync() {
3286 +        CompletableFuture<Integer> f, g;
3287 +        IntegerHandler r;
3288 +
3289 +        f = new CompletableFuture<>();
3290 +        g = f.handleAsync(r = new IntegerHandler());
3291 +        assertFalse(r.ran);
3292 +        f.completeExceptionally(new CFException());
3293 +        checkCompletedWithWrappedCFException(f);
3294 +        checkCompletedNormally(g, three);
3295 +        assertTrue(r.ran);
3296 +
3297 +        f = new CompletableFuture<>();
3298 +        g = f.handleAsync(r = new IntegerHandler());
3299 +        assertFalse(r.ran);
3300 +        f.completeExceptionally(new CFException());
3301 +        checkCompletedWithWrappedCFException(f);
3302 +        checkCompletedNormally(g, three);
3303 +        assertTrue(r.ran);
3304 +
3305 +        f = new CompletableFuture<>();
3306 +        g = f.handleAsync(r = new IntegerHandler());
3307 +        assertFalse(r.ran);
3308 +        f.complete(one);
3309 +        checkCompletedNormally(f, one);
3310 +        checkCompletedNormally(g, two);
3311 +        assertTrue(r.ran);
3312 +
3313 +        f = new CompletableFuture<>();
3314 +        g = f.handleAsync(r = new IntegerHandler());
3315 +        assertFalse(r.ran);
3316 +        f.complete(one);
3317 +        checkCompletedNormally(f, one);
3318 +        checkCompletedNormally(g, two);
3319 +        assertTrue(r.ran);
3320 +    }
3321 +
3322 +    /**
3323 +     * handleAsync action with Executor completes normally with
3324 +     * function value on either normal or exceptional completion of
3325 +     * source
3326 +     */
3327 +    public void testHandleAsync2() {
3328 +        CompletableFuture<Integer> f, g;
3329 +        ThreadExecutor exec = new ThreadExecutor();
3330 +        IntegerHandler r;
3331 +
3332 +        f = new CompletableFuture<>();
3333 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3334 +        assertFalse(r.ran);
3335 +        f.completeExceptionally(new CFException());
3336 +        checkCompletedWithWrappedCFException(f);
3337 +        checkCompletedNormally(g, three);
3338 +        assertTrue(r.ran);
3339 +
3340 +        f = new CompletableFuture<>();
3341 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3342 +        assertFalse(r.ran);
3343 +        f.completeExceptionally(new CFException());
3344 +        checkCompletedWithWrappedCFException(f);
3345 +        checkCompletedNormally(g, three);
3346 +        assertTrue(r.ran);
3347 +
3348 +        f = new CompletableFuture<>();
3349 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3350 +        assertFalse(r.ran);
3351 +        f.complete(one);
3352 +        checkCompletedNormally(f, one);
3353 +        checkCompletedNormally(g, two);
3354 +        assertTrue(r.ran);
3355 +
3356 +        f = new CompletableFuture<>();
3357 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3358 +        assertFalse(r.ran);
3359 +        f.complete(one);
3360 +        checkCompletedNormally(f, one);
3361 +        checkCompletedNormally(g, two);
3362 +        assertTrue(r.ran);
3363 +    }
3364 +
3365   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines