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.4 by jsr166, Sun Feb 10 21:18:25 2013 UTC vs.
Revision 1.33 by jsr166, Sun Jun 1 20:40:13 2014 UTC

# Line 7 | Line 7
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 27 | 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());
# Line 42 | Line 53 | public class CompletableFutureTest exten
53          catch (Throwable fail) { threadUnexpectedException(fail); }
54      }
55  
56 <    void checkCompletedNormally(CompletableFuture<?> f, Object value) {
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 <            assertSame(value, f.join());
138 >            f.get(LONG_DELAY_MS, MILLISECONDS);
139 >            shouldThrow();
140 >        } catch (CancellationException success) {
141          } catch (Throwable fail) { threadUnexpectedException(fail); }
142          try {
143 <            assertSame(value, f.getNow(null));
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 <            assertSame(value, f.get());
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 <            assertSame(value, f.get(0L, SECONDS));
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 <    // XXXX Just a skeleton implementation for now.
277 <    public void testTODO() {
278 <        fail("Please add some real tests!");
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  
# Line 80 | Line 308 | public class CompletableFutureTest exten
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 +     * runAfterBoth result completes normally after normal
951 +     * completion of sources
952 +     */
953 +    public void testRunAfterBoth_normalCompletion1() {
954 +        for (Integer v1 : new Integer[] { 1, null })
955 +        for (Integer v2 : new Integer[] { 2, null }) {
956 +
957 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
958 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
959 +        final Noop r = new Noop();
960 +        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
961 +
962 +        f.complete(v1);
963 +        checkIncomplete(h);
964 +        assertFalse(r.ran);
965 +        g.complete(v2);
966 +
967 +        checkCompletedNormally(h, null);
968 +        assertTrue(r.ran);
969 +        checkCompletedNormally(f, v1);
970 +        checkCompletedNormally(g, v2);
971 +        }
972 +    }
973 +
974 +    public void testRunAfterBoth_normalCompletion2() {
975 +        for (Integer v1 : new Integer[] { 1, null })
976 +        for (Integer v2 : new Integer[] { 2, null }) {
977 +
978 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
979 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
980 +        final Noop r = new Noop();
981 +        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
982 +
983 +        g.complete(v2);
984 +        checkIncomplete(h);
985 +        assertFalse(r.ran);
986 +        f.complete(v1);
987 +
988 +        checkCompletedNormally(h, null);
989 +        assertTrue(r.ran);
990 +        checkCompletedNormally(f, v1);
991 +        checkCompletedNormally(g, v2);
992 +        }
993 +    }
994 +
995 +    public void testRunAfterBoth_normalCompletion3() {
996 +        for (Integer v1 : new Integer[] { 1, null })
997 +        for (Integer v2 : new Integer[] { 2, null }) {
998 +
999 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1000 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1001 +        final Noop r = new Noop();
1002 +
1003 +        g.complete(v2);
1004 +        f.complete(v1);
1005 +        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1006 +
1007 +        checkCompletedNormally(h, null);
1008 +        assertTrue(r.ran);
1009 +        checkCompletedNormally(f, v1);
1010 +        checkCompletedNormally(g, v2);
1011 +        }
1012 +    }
1013 +
1014 +    public void testRunAfterBoth_normalCompletion4() {
1015 +        for (Integer v1 : new Integer[] { 1, null })
1016 +        for (Integer v2 : new Integer[] { 2, null }) {
1017 +
1018 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1019 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1020 +        final Noop r = new Noop();
1021 +
1022 +        f.complete(v1);
1023 +        g.complete(v2);
1024 +        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1025 +
1026 +        checkCompletedNormally(h, null);
1027 +        assertTrue(r.ran);
1028 +        checkCompletedNormally(f, v1);
1029 +        checkCompletedNormally(g, v2);
1030 +        }
1031 +    }
1032 +
1033 +    /**
1034 +     * runAfterBoth result completes exceptionally after exceptional
1035 +     * completion of either source
1036 +     */
1037 +    public void testRunAfterBoth_exceptionalCompletion1() {
1038 +        for (Integer v1 : new Integer[] { 1, null }) {
1039 +
1040 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1041 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1042 +        final Noop r = new Noop();
1043 +        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1044 +        final CFException ex = new CFException();
1045 +
1046 +        f.completeExceptionally(ex);
1047 +        checkIncomplete(h);
1048 +        g.complete(v1);
1049 +
1050 +        checkCompletedWithWrappedCFException(h, ex);
1051 +        checkCompletedWithWrappedCFException(f, ex);
1052 +        assertFalse(r.ran);
1053 +        checkCompletedNormally(g, v1);
1054 +        }
1055 +    }
1056 +
1057 +    public void testRunAfterBoth_exceptionalCompletion2() {
1058 +        for (Integer v1 : new Integer[] { 1, null }) {
1059 +
1060 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1061 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1062 +        final Noop r = new Noop();
1063 +        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1064 +        final CFException ex = new CFException();
1065 +
1066 +        g.completeExceptionally(ex);
1067 +        checkIncomplete(h);
1068 +        f.complete(v1);
1069 +
1070 +        checkCompletedWithWrappedCFException(h, ex);
1071 +        checkCompletedWithWrappedCFException(g, ex);
1072 +        assertFalse(r.ran);
1073 +        checkCompletedNormally(f, v1);
1074 +        }
1075 +    }
1076 +
1077 +    public void testRunAfterBoth_exceptionalCompletion3() {
1078 +        for (Integer v1 : new Integer[] { 1, null }) {
1079 +
1080 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1081 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1082 +        final Noop r = new Noop();
1083 +        final CFException ex = new CFException();
1084 +
1085 +        g.completeExceptionally(ex);
1086 +        f.complete(v1);
1087 +        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1088 +
1089 +        checkCompletedWithWrappedCFException(h, ex);
1090 +        checkCompletedWithWrappedCFException(g, ex);
1091 +        assertFalse(r.ran);
1092 +        checkCompletedNormally(f, v1);
1093 +        }
1094 +    }
1095 +
1096 +    public void testRunAfterBoth_exceptionalCompletion4() {
1097 +        for (Integer v1 : new Integer[] { 1, null }) {
1098 +
1099 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1100 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1101 +        final Noop r = new Noop();
1102 +        final CFException ex = new CFException();
1103 +
1104 +        f.completeExceptionally(ex);
1105 +        g.complete(v1);
1106 +        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1107 +
1108 +        checkCompletedWithWrappedCFException(h, ex);
1109 +        checkCompletedWithWrappedCFException(f, ex);
1110 +        assertFalse(r.ran);
1111 +        checkCompletedNormally(g, v1);
1112 +        }
1113 +    }
1114 +
1115 +    /**
1116 +     * runAfterBoth result completes exceptionally if action does
1117 +     */
1118 +    public void testRunAfterBoth_actionFailed1() {
1119 +        for (Integer v1 : new Integer[] { 1, null })
1120 +        for (Integer v2 : new Integer[] { 2, null }) {
1121 +
1122 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1123 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1124 +        final FailingNoop r = new FailingNoop();
1125 +        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1126 +
1127 +        f.complete(v1);
1128 +        checkIncomplete(h);
1129 +        g.complete(v2);
1130 +
1131 +        checkCompletedWithWrappedCFException(h);
1132 +        checkCompletedNormally(f, v1);
1133 +        checkCompletedNormally(g, v2);
1134 +        }
1135 +    }
1136 +
1137 +    public void testRunAfterBoth_actionFailed2() {
1138 +        for (Integer v1 : new Integer[] { 1, null })
1139 +        for (Integer v2 : new Integer[] { 2, null }) {
1140 +
1141 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1142 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1143 +        final FailingNoop r = new FailingNoop();
1144 +        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1145 +
1146 +        g.complete(v2);
1147 +        checkIncomplete(h);
1148 +        f.complete(v1);
1149 +
1150 +        checkCompletedWithWrappedCFException(h);
1151 +        checkCompletedNormally(f, v1);
1152 +        checkCompletedNormally(g, v2);
1153 +        }
1154 +    }
1155 +
1156 +    /**
1157 +     * runAfterBoth result completes exceptionally if either source cancelled
1158 +     */
1159 +    public void testRunAfterBoth_sourceCancelled1() {
1160 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1161 +        for (Integer v1 : new Integer[] { 1, null }) {
1162 +
1163 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1164 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1165 +        final Noop r = new Noop();
1166 +        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1167 +
1168 +        assertTrue(f.cancel(mayInterruptIfRunning));
1169 +        checkIncomplete(h);
1170 +        g.complete(v1);
1171 +
1172 +        checkCompletedWithWrappedCancellationException(h);
1173 +        checkCancelled(f);
1174 +        assertFalse(r.ran);
1175 +        checkCompletedNormally(g, v1);
1176 +        }
1177 +    }
1178 +
1179 +    public void testRunAfterBoth_sourceCancelled2() {
1180 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1181 +        for (Integer v1 : new Integer[] { 1, null }) {
1182 +
1183 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1184 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1185 +        final Noop r = new Noop();
1186 +        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1187 +
1188 +        assertTrue(g.cancel(mayInterruptIfRunning));
1189 +        checkIncomplete(h);
1190 +        f.complete(v1);
1191 +
1192 +        checkCompletedWithWrappedCancellationException(h);
1193 +        checkCancelled(g);
1194 +        assertFalse(r.ran);
1195 +        checkCompletedNormally(f, v1);
1196 +        }
1197 +    }
1198 +
1199 +    public void testRunAfterBoth_sourceCancelled3() {
1200 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1201 +        for (Integer v1 : new Integer[] { 1, null }) {
1202 +
1203 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1204 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1205 +        final Noop r = new Noop();
1206 +
1207 +        assertTrue(g.cancel(mayInterruptIfRunning));
1208 +        f.complete(v1);
1209 +        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1210 +
1211 +        checkCompletedWithWrappedCancellationException(h);
1212 +        checkCancelled(g);
1213 +        assertFalse(r.ran);
1214 +        checkCompletedNormally(f, v1);
1215 +        }
1216 +    }
1217 +
1218 +    public void testRunAfterBoth_sourceCancelled4() {
1219 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1220 +        for (Integer v1 : new Integer[] { 1, null }) {
1221 +
1222 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1223 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1224 +        final Noop r = new Noop();
1225 +
1226 +        assertTrue(f.cancel(mayInterruptIfRunning));
1227 +        g.complete(v1);
1228 +        final CompletableFuture<Void> h = f.runAfterBoth(g, r);
1229 +
1230 +        checkCompletedWithWrappedCancellationException(h);
1231 +        checkCancelled(f);
1232 +        assertFalse(r.ran);
1233 +        checkCompletedNormally(g, v1);
1234 +        }
1235 +    }
1236 +
1237 +    /**
1238 +     * applyToEither result completes normally after normal completion
1239 +     * of either source
1240 +     */
1241 +    public void testApplyToEither() {
1242 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1243 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1244 +        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1245 +        f.complete(one);
1246 +        checkCompletedNormally(g, two);
1247 +        f2.complete(one);
1248 +        checkCompletedNormally(g, two);
1249 +
1250 +        f = new CompletableFuture<>();
1251 +        f.complete(one);
1252 +        f2 = new CompletableFuture<>();
1253 +        g = f.applyToEither(f2, inc);
1254 +        checkCompletedNormally(g, two);
1255 +    }
1256 +
1257 +    /**
1258 +     * applyToEither result completes exceptionally after exceptional
1259 +     * completion of either source
1260 +     */
1261 +    public void testApplyToEither2() {
1262 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1263 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1264 +        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1265 +        f.completeExceptionally(new CFException());
1266 +        f2.complete(one);
1267 +        checkCompletedWithWrappedCFException(g);
1268 +
1269 +        f = new CompletableFuture<>();
1270 +        f2 = new CompletableFuture<>();
1271 +        f2.completeExceptionally(new CFException());
1272 +        g = f.applyToEither(f2, inc);
1273 +        checkCompletedWithWrappedCFException(g);
1274 +    }
1275 +
1276 +    /**
1277 +     * applyToEither result completes exceptionally if action does
1278 +     */
1279 +    public void testApplyToEither3() {
1280 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1281 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1282 +        FailingFunction r = new FailingFunction();
1283 +        CompletableFuture<Integer> g = f.applyToEither(f2, r);
1284 +        f2.complete(two);
1285 +        checkCompletedWithWrappedCFException(g);
1286 +    }
1287 +
1288 +    /**
1289 +     * applyToEither result completes exceptionally if either source cancelled
1290 +     */
1291 +    public void testApplyToEither4() {
1292 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1293 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1294 +        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1295 +        assertTrue(f.cancel(true));
1296 +        checkCompletedWithWrappedCancellationException(g);
1297 +        f = new CompletableFuture<>();
1298 +        f2 = new CompletableFuture<>();
1299 +        assertTrue(f2.cancel(true));
1300 +        checkCompletedWithWrappedCancellationException(g);
1301 +    }
1302 +
1303 +    /**
1304 +     * acceptEither result completes normally after normal completion
1305 +     * of either source
1306 +     */
1307 +    public void testAcceptEither() {
1308 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1309 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1310 +        IncAction r = new IncAction();
1311 +        CompletableFuture<Void> g = f.acceptEither(f2, r);
1312 +        f.complete(one);
1313 +        checkCompletedNormally(g, null);
1314 +        f2.complete(one);
1315 +        checkCompletedNormally(g, null);
1316 +        assertEquals(r.value, 2);
1317 +
1318 +        r = new IncAction();
1319 +        f = new CompletableFuture<>();
1320 +        f.complete(one);
1321 +        f2 = new CompletableFuture<>();
1322 +        g = f.acceptEither(f2, r);
1323 +        checkCompletedNormally(g, null);
1324 +        assertEquals(r.value, 2);
1325 +    }
1326 +
1327 +    /**
1328 +     * acceptEither result completes exceptionally after exceptional
1329 +     * completion of either source
1330 +     */
1331 +    public void testAcceptEither2() {
1332 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1333 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1334 +        IncAction r = new IncAction();
1335 +        CompletableFuture<Void> g = f.acceptEither(f2, r);
1336 +        f.completeExceptionally(new CFException());
1337 +        f2.complete(one);
1338 +        checkCompletedWithWrappedCFException(g);
1339 +
1340 +        r = new IncAction();
1341 +        f = new CompletableFuture<>();
1342 +        f2 = new CompletableFuture<>();
1343 +        f2.completeExceptionally(new CFException());
1344 +        g = f.acceptEither(f2, r);
1345 +        checkCompletedWithWrappedCFException(g);
1346 +    }
1347 +
1348 +    /**
1349 +     * acceptEither result completes exceptionally if action does
1350 +     */
1351 +    public void testAcceptEither3() {
1352 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1353 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1354 +        FailingConsumer r = new FailingConsumer();
1355 +        CompletableFuture<Void> g = f.acceptEither(f2, r);
1356 +        f2.complete(two);
1357 +        checkCompletedWithWrappedCFException(g);
1358 +    }
1359 +
1360 +    /**
1361 +     * acceptEither result completes exceptionally if either source cancelled
1362 +     */
1363 +    public void testAcceptEither4() {
1364 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1365 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1366 +        IncAction r = new IncAction();
1367 +        CompletableFuture<Void> g = f.acceptEither(f2, r);
1368 +        assertTrue(f.cancel(true));
1369 +        checkCompletedWithWrappedCancellationException(g);
1370 +        f = new CompletableFuture<>();
1371 +        f2 = new CompletableFuture<>();
1372 +        assertTrue(f2.cancel(true));
1373 +        checkCompletedWithWrappedCancellationException(g);
1374 +    }
1375 +
1376 +    /**
1377 +     * runAfterEither result completes normally after normal completion
1378 +     * of either source
1379 +     */
1380 +    public void testRunAfterEither() {
1381 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1382 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1383 +        Noop r = new Noop();
1384 +        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1385 +        f.complete(one);
1386 +        checkCompletedNormally(g, null);
1387 +        f2.complete(one);
1388 +        checkCompletedNormally(g, null);
1389 +        assertTrue(r.ran);
1390 +
1391 +        r = new Noop();
1392 +        f = new CompletableFuture<>();
1393 +        f.complete(one);
1394 +        f2 = new CompletableFuture<>();
1395 +        g = f.runAfterEither(f2, r);
1396 +        checkCompletedNormally(g, null);
1397 +        assertTrue(r.ran);
1398 +    }
1399 +
1400 +    /**
1401 +     * runAfterEither result completes exceptionally after exceptional
1402 +     * completion of either source
1403 +     */
1404 +    public void testRunAfterEither2() {
1405 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1406 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1407 +        Noop r = new Noop();
1408 +        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1409 +        f.completeExceptionally(new CFException());
1410 +        f2.complete(one);
1411 +        checkCompletedWithWrappedCFException(g);
1412 +
1413 +        r = new Noop();
1414 +        f = new CompletableFuture<>();
1415 +        f2 = new CompletableFuture<>();
1416 +        f2.completeExceptionally(new CFException());
1417 +        g = f.runAfterEither(f2, r);
1418 +        checkCompletedWithWrappedCFException(g);
1419 +    }
1420 +
1421 +    /**
1422 +     * runAfterEither result completes exceptionally if action does
1423 +     */
1424 +    public void testRunAfterEither3() {
1425 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1426 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1427 +        FailingNoop r = new FailingNoop();
1428 +        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1429 +        f2.complete(two);
1430 +        checkCompletedWithWrappedCFException(g);
1431 +    }
1432 +
1433 +    /**
1434 +     * runAfterEither result completes exceptionally if either source cancelled
1435 +     */
1436 +    public void testRunAfterEither4() {
1437 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1438 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1439 +        Noop r = new Noop();
1440 +        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1441 +        assertTrue(f.cancel(true));
1442 +        checkCompletedWithWrappedCancellationException(g);
1443 +        f = new CompletableFuture<>();
1444 +        f2 = new CompletableFuture<>();
1445 +        assertTrue(f2.cancel(true));
1446 +        checkCompletedWithWrappedCancellationException(g);
1447 +    }
1448 +
1449 +    /**
1450 +     * thenCompose result completes normally after normal completion of source
1451 +     */
1452 +    public void testThenCompose() {
1453 +        CompletableFuture<Integer> f, g;
1454 +        CompletableFutureInc r;
1455 +
1456 +        f = new CompletableFuture<>();
1457 +        g = f.thenCompose(r = new CompletableFutureInc());
1458 +        f.complete(one);
1459 +        checkCompletedNormally(g, two);
1460 +        assertTrue(r.ran);
1461 +
1462 +        f = new CompletableFuture<>();
1463 +        f.complete(one);
1464 +        g = f.thenCompose(r = new CompletableFutureInc());
1465 +        checkCompletedNormally(g, two);
1466 +        assertTrue(r.ran);
1467 +    }
1468 +
1469 +    /**
1470 +     * thenCompose result completes exceptionally after exceptional
1471 +     * completion of source
1472 +     */
1473 +    public void testThenCompose2() {
1474 +        CompletableFuture<Integer> f, g;
1475 +        CompletableFutureInc r;
1476 +
1477 +        f = new CompletableFuture<>();
1478 +        g = f.thenCompose(r = new CompletableFutureInc());
1479 +        f.completeExceptionally(new CFException());
1480 +        checkCompletedWithWrappedCFException(g);
1481 +
1482 +        f = new CompletableFuture<>();
1483 +        f.completeExceptionally(new CFException());
1484 +        g = f.thenCompose(r = new CompletableFutureInc());
1485 +        checkCompletedWithWrappedCFException(g);
1486 +    }
1487 +
1488 +    /**
1489 +     * thenCompose result completes exceptionally if action does
1490 +     */
1491 +    public void testThenCompose3() {
1492 +        CompletableFuture<Integer> f, g;
1493 +        FailingCompletableFutureFunction r;
1494 +
1495 +        f = new CompletableFuture<>();
1496 +        g = f.thenCompose(r = new FailingCompletableFutureFunction());
1497 +        f.complete(one);
1498 +        checkCompletedWithWrappedCFException(g);
1499 +
1500 +        f = new CompletableFuture<>();
1501 +        f.complete(one);
1502 +        g = f.thenCompose(r = new FailingCompletableFutureFunction());
1503 +        checkCompletedWithWrappedCFException(g);
1504 +    }
1505 +
1506 +    /**
1507 +     * thenCompose result completes exceptionally if source cancelled
1508 +     */
1509 +    public void testThenCompose4() {
1510 +        CompletableFuture<Integer> f, g;
1511 +        CompletableFutureInc r;
1512 +
1513 +        f = new CompletableFuture<>();
1514 +        g = f.thenCompose(r = new CompletableFutureInc());
1515 +        assertTrue(f.cancel(true));
1516 +        checkCompletedWithWrappedCancellationException(g);
1517 +
1518 +        f = new CompletableFuture<>();
1519 +        assertTrue(f.cancel(true));
1520 +        g = f.thenCompose(r = new CompletableFutureInc());
1521 +        checkCompletedWithWrappedCancellationException(g);
1522 +    }
1523 +
1524 +    // asyncs
1525 +
1526 +    /**
1527 +     * thenRunAsync result completes normally after normal completion of source
1528 +     */
1529 +    public void testThenRunAsync() {
1530 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1531 +        Noop r = new Noop();
1532 +        CompletableFuture<Void> g = f.thenRunAsync(r);
1533 +        f.complete(null);
1534 +        checkCompletedNormally(g, null);
1535 +
1536 +        // reordered version
1537 +        f = new CompletableFuture<>();
1538 +        f.complete(null);
1539 +        r = new Noop();
1540 +        g = f.thenRunAsync(r);
1541 +        checkCompletedNormally(g, null);
1542 +    }
1543 +
1544 +    /**
1545 +     * thenRunAsync result completes exceptionally after exceptional
1546 +     * completion of source
1547 +     */
1548 +    public void testThenRunAsync2() {
1549 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1550 +        Noop r = new Noop();
1551 +        CompletableFuture<Void> g = f.thenRunAsync(r);
1552 +        f.completeExceptionally(new CFException());
1553 +        try {
1554 +            g.join();
1555 +            shouldThrow();
1556 +        } catch (CompletionException success) {}
1557 +        checkCompletedWithWrappedCFException(g);
1558 +    }
1559 +
1560 +    /**
1561 +     * thenRunAsync result completes exceptionally if action does
1562 +     */
1563 +    public void testThenRunAsync3() {
1564 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1565 +        FailingNoop r = new FailingNoop();
1566 +        CompletableFuture<Void> g = f.thenRunAsync(r);
1567 +        f.complete(null);
1568 +        checkCompletedWithWrappedCFException(g);
1569 +    }
1570 +
1571 +    /**
1572 +     * thenRunAsync result completes exceptionally if source cancelled
1573 +     */
1574 +    public void testThenRunAsync4() {
1575 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1576 +        Noop r = new Noop();
1577 +        CompletableFuture<Void> g = f.thenRunAsync(r);
1578 +        assertTrue(f.cancel(true));
1579 +        checkCompletedWithWrappedCancellationException(g);
1580 +    }
1581 +
1582 +    /**
1583 +     * thenApplyAsync result completes normally after normal completion of source
1584 +     */
1585 +    public void testThenApplyAsync() {
1586 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1587 +        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1588 +        f.complete(one);
1589 +        checkCompletedNormally(g, two);
1590 +    }
1591 +
1592 +    /**
1593 +     * thenApplyAsync result completes exceptionally after exceptional
1594 +     * completion of source
1595 +     */
1596 +    public void testThenApplyAsync2() {
1597 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1598 +        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1599 +        f.completeExceptionally(new CFException());
1600 +        checkCompletedWithWrappedCFException(g);
1601 +    }
1602 +
1603 +    /**
1604 +     * thenApplyAsync result completes exceptionally if action does
1605 +     */
1606 +    public void testThenApplyAsync3() {
1607 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1608 +        FailingFunction r = new FailingFunction();
1609 +        CompletableFuture<Integer> g = f.thenApplyAsync(r);
1610 +        f.complete(null);
1611 +        checkCompletedWithWrappedCFException(g);
1612 +    }
1613 +
1614 +    /**
1615 +     * thenApplyAsync result completes exceptionally if source cancelled
1616 +     */
1617 +    public void testThenApplyAsync4() {
1618 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1619 +        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1620 +        assertTrue(f.cancel(true));
1621 +        checkCompletedWithWrappedCancellationException(g);
1622 +    }
1623 +
1624 +    /**
1625 +     * thenAcceptAsync result completes normally after normal
1626 +     * completion of source
1627 +     */
1628 +    public void testThenAcceptAsync() {
1629 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1630 +        IncAction r = new IncAction();
1631 +        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1632 +        f.complete(one);
1633 +        checkCompletedNormally(g, null);
1634 +        assertEquals(r.value, 2);
1635 +    }
1636 +
1637 +    /**
1638 +     * thenAcceptAsync result completes exceptionally after exceptional
1639 +     * completion of source
1640 +     */
1641 +    public void testThenAcceptAsync2() {
1642 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1643 +        IncAction r = new IncAction();
1644 +        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1645 +        f.completeExceptionally(new CFException());
1646 +        checkCompletedWithWrappedCFException(g);
1647 +    }
1648 +
1649 +    /**
1650 +     * thenAcceptAsync result completes exceptionally if action does
1651 +     */
1652 +    public void testThenAcceptAsync3() {
1653 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1654 +        FailingConsumer r = new FailingConsumer();
1655 +        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1656 +        f.complete(null);
1657 +        checkCompletedWithWrappedCFException(g);
1658 +    }
1659 +
1660 +    /**
1661 +     * thenAcceptAsync result completes exceptionally if source cancelled
1662 +     */
1663 +    public void testThenAcceptAsync4() {
1664 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1665 +        IncAction r = new IncAction();
1666 +        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1667 +        assertTrue(f.cancel(true));
1668 +        checkCompletedWithWrappedCancellationException(g);
1669 +    }
1670 +
1671 +    /**
1672 +     * thenCombineAsync result completes normally after normal
1673 +     * completion of sources
1674 +     */
1675 +    public void testThenCombineAsync() {
1676 +        CompletableFuture<Integer> f, g, h;
1677 +
1678 +        f = new CompletableFuture<>();
1679 +        g = new CompletableFuture<>();
1680 +        h = f.thenCombineAsync(g, subtract);
1681 +        f.complete(3);
1682 +        checkIncomplete(h);
1683 +        g.complete(1);
1684 +        checkCompletedNormally(h, 2);
1685 +
1686 +        f = new CompletableFuture<>();
1687 +        g = new CompletableFuture<>();
1688 +        h = f.thenCombineAsync(g, subtract);
1689 +        g.complete(1);
1690 +        checkIncomplete(h);
1691 +        f.complete(3);
1692 +        checkCompletedNormally(h, 2);
1693 +
1694 +        f = new CompletableFuture<>();
1695 +        g = new CompletableFuture<>();
1696 +        g.complete(1);
1697 +        f.complete(3);
1698 +        h = f.thenCombineAsync(g, subtract);
1699 +        checkCompletedNormally(h, 2);
1700 +    }
1701 +
1702 +    /**
1703 +     * thenCombineAsync result completes exceptionally after exceptional
1704 +     * completion of either source
1705 +     */
1706 +    public void testThenCombineAsync2() {
1707 +        CompletableFuture<Integer> f, g, h;
1708 +
1709 +        f = new CompletableFuture<>();
1710 +        g = new CompletableFuture<>();
1711 +        h = f.thenCombineAsync(g, subtract);
1712 +        f.completeExceptionally(new CFException());
1713 +        checkIncomplete(h);
1714 +        g.complete(1);
1715 +        checkCompletedWithWrappedCFException(h);
1716 +
1717 +        f = new CompletableFuture<>();
1718 +        g = new CompletableFuture<>();
1719 +        h = f.thenCombineAsync(g, subtract);
1720 +        g.completeExceptionally(new CFException());
1721 +        checkIncomplete(h);
1722 +        f.complete(3);
1723 +        checkCompletedWithWrappedCFException(h);
1724 +
1725 +        f = new CompletableFuture<>();
1726 +        g = new CompletableFuture<>();
1727 +        g.completeExceptionally(new CFException());
1728 +        f.complete(3);
1729 +        h = f.thenCombineAsync(g, subtract);
1730 +        checkCompletedWithWrappedCFException(h);
1731 +    }
1732 +
1733 +    /**
1734 +     * thenCombineAsync result completes exceptionally if action does
1735 +     */
1736 +    public void testThenCombineAsync3() {
1737 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1738 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1739 +        FailingBiFunction r = new FailingBiFunction();
1740 +        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r);
1741 +        f.complete(one);
1742 +        checkIncomplete(g);
1743 +        assertFalse(r.ran);
1744 +        f2.complete(two);
1745 +        checkCompletedWithWrappedCFException(g);
1746 +        assertTrue(r.ran);
1747 +    }
1748 +
1749 +    /**
1750 +     * thenCombineAsync result completes exceptionally if either source cancelled
1751 +     */
1752 +    public void testThenCombineAsync4() {
1753 +        CompletableFuture<Integer> f, g, h;
1754 +
1755 +        f = new CompletableFuture<>();
1756 +        g = new CompletableFuture<>();
1757 +        h = f.thenCombineAsync(g, subtract);
1758 +        assertTrue(f.cancel(true));
1759 +        checkIncomplete(h);
1760 +        g.complete(1);
1761 +        checkCompletedWithWrappedCancellationException(h);
1762 +
1763 +        f = new CompletableFuture<>();
1764 +        g = new CompletableFuture<>();
1765 +        h = f.thenCombineAsync(g, subtract);
1766 +        assertTrue(g.cancel(true));
1767 +        checkIncomplete(h);
1768 +        f.complete(3);
1769 +        checkCompletedWithWrappedCancellationException(h);
1770 +
1771 +        f = new CompletableFuture<>();
1772 +        g = new CompletableFuture<>();
1773 +        g.complete(3);
1774 +        assertTrue(f.cancel(true));
1775 +        h = f.thenCombineAsync(g, subtract);
1776 +        checkCompletedWithWrappedCancellationException(h);
1777 +
1778 +        f = new CompletableFuture<>();
1779 +        g = new CompletableFuture<>();
1780 +        f.complete(3);
1781 +        assertTrue(g.cancel(true));
1782 +        h = f.thenCombineAsync(g, subtract);
1783 +        checkCompletedWithWrappedCancellationException(h);
1784 +    }
1785 +
1786 +    /**
1787 +     * thenAcceptBothAsync result completes normally after normal
1788 +     * completion of sources
1789 +     */
1790 +    public void testThenAcceptBothAsync() {
1791 +        CompletableFuture<Integer> f, g;
1792 +        CompletableFuture<Void> h;
1793 +        SubtractAction r;
1794 +
1795 +        f = new CompletableFuture<>();
1796 +        g = new CompletableFuture<>();
1797 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1798 +        f.complete(3);
1799 +        checkIncomplete(h);
1800 +        g.complete(1);
1801 +        checkCompletedNormally(h, null);
1802 +        assertEquals(r.value, 2);
1803 +
1804 +        f = new CompletableFuture<>();
1805 +        g = new CompletableFuture<>();
1806 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1807 +        g.complete(1);
1808 +        checkIncomplete(h);
1809 +        f.complete(3);
1810 +        checkCompletedNormally(h, null);
1811 +        assertEquals(r.value, 2);
1812 +
1813 +        f = new CompletableFuture<>();
1814 +        g = new CompletableFuture<>();
1815 +        g.complete(1);
1816 +        f.complete(3);
1817 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1818 +        checkCompletedNormally(h, null);
1819 +        assertEquals(r.value, 2);
1820 +    }
1821 +
1822 +    /**
1823 +     * thenAcceptBothAsync result completes exceptionally after exceptional
1824 +     * completion of source
1825 +     */
1826 +    public void testThenAcceptBothAsync2() {
1827 +        CompletableFuture<Integer> f, g;
1828 +        CompletableFuture<Void> h;
1829 +        SubtractAction r;
1830 +
1831 +        f = new CompletableFuture<>();
1832 +        g = new CompletableFuture<>();
1833 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1834 +        f.completeExceptionally(new CFException());
1835 +        checkIncomplete(h);
1836 +        g.complete(1);
1837 +        checkCompletedWithWrappedCFException(h);
1838 +
1839 +        f = new CompletableFuture<>();
1840 +        g = new CompletableFuture<>();
1841 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1842 +        g.completeExceptionally(new CFException());
1843 +        checkIncomplete(h);
1844 +        f.complete(3);
1845 +        checkCompletedWithWrappedCFException(h);
1846 +
1847 +        f = new CompletableFuture<>();
1848 +        g = new CompletableFuture<>();
1849 +        f.complete(3);
1850 +        g.completeExceptionally(new CFException());
1851 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1852 +        checkCompletedWithWrappedCFException(h);
1853 +
1854 +        f = new CompletableFuture<>();
1855 +        g = new CompletableFuture<>();
1856 +        f.completeExceptionally(new CFException());
1857 +        g.complete(3);
1858 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1859 +        checkCompletedWithWrappedCFException(h);
1860 +    }
1861 +
1862 +    /**
1863 +     * thenAcceptBothAsync result completes exceptionally if action does
1864 +     */
1865 +    public void testThenAcceptBothAsync3() {
1866 +        CompletableFuture<Integer> f, g;
1867 +        CompletableFuture<Void> h;
1868 +        FailingBiConsumer r;
1869 +
1870 +        f = new CompletableFuture<>();
1871 +        g = new CompletableFuture<>();
1872 +        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer());
1873 +        f.complete(3);
1874 +        checkIncomplete(h);
1875 +        g.complete(1);
1876 +        checkCompletedWithWrappedCFException(h);
1877 +
1878 +        f = new CompletableFuture<>();
1879 +        g = new CompletableFuture<>();
1880 +        f.complete(3);
1881 +        g.complete(1);
1882 +        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer());
1883 +        checkCompletedWithWrappedCFException(h);
1884 +    }
1885 +
1886 +    /**
1887 +     * thenAcceptBothAsync result completes exceptionally if either source cancelled
1888 +     */
1889 +    public void testThenAcceptBothAsync4() {
1890 +        CompletableFuture<Integer> f, g;
1891 +        CompletableFuture<Void> h;
1892 +        SubtractAction r;
1893 +
1894 +        f = new CompletableFuture<>();
1895 +        g = new CompletableFuture<>();
1896 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1897 +        assertTrue(f.cancel(true));
1898 +        checkIncomplete(h);
1899 +        g.complete(1);
1900 +        checkCompletedWithWrappedCancellationException(h);
1901 +
1902 +        f = new CompletableFuture<>();
1903 +        g = new CompletableFuture<>();
1904 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1905 +        assertTrue(g.cancel(true));
1906 +        checkIncomplete(h);
1907 +        f.complete(3);
1908 +        checkCompletedWithWrappedCancellationException(h);
1909 +
1910 +        f = new CompletableFuture<>();
1911 +        g = new CompletableFuture<>();
1912 +        f.complete(3);
1913 +        assertTrue(g.cancel(true));
1914 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1915 +        checkCompletedWithWrappedCancellationException(h);
1916 +
1917 +        f = new CompletableFuture<>();
1918 +        g = new CompletableFuture<>();
1919 +        assertTrue(f.cancel(true));
1920 +        g.complete(3);
1921 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1922 +        checkCompletedWithWrappedCancellationException(h);
1923 +    }
1924 +
1925 +    /**
1926 +     * runAfterBothAsync result completes normally after normal
1927 +     * completion of sources
1928 +     */
1929 +    public void testRunAfterBothAsync() {
1930 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1931 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1932 +        Noop r = new Noop();
1933 +        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1934 +        f.complete(one);
1935 +        checkIncomplete(g);
1936 +        f2.complete(two);
1937 +        checkCompletedNormally(g, null);
1938 +        assertTrue(r.ran);
1939 +    }
1940 +
1941 +    /**
1942 +     * runAfterBothAsync result completes exceptionally after exceptional
1943 +     * completion of source
1944 +     */
1945 +    public void testRunAfterBothAsync2() {
1946 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1947 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1948 +        Noop r = new Noop();
1949 +        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1950 +        f.completeExceptionally(new CFException());
1951 +        f2.complete(two);
1952 +        checkCompletedWithWrappedCFException(g);
1953 +
1954 +        r = new Noop();
1955 +        f = new CompletableFuture<>();
1956 +        f2 = new CompletableFuture<>();
1957 +        g = f.runAfterBothAsync(f2, r);
1958 +        f.complete(one);
1959 +        f2.completeExceptionally(new CFException());
1960 +        checkCompletedWithWrappedCFException(g);
1961 +    }
1962 +
1963 +    /**
1964 +     * runAfterBothAsync result completes exceptionally if action does
1965 +     */
1966 +    public void testRunAfterBothAsync3() {
1967 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1968 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1969 +        FailingNoop r = new FailingNoop();
1970 +        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1971 +        f.complete(one);
1972 +        checkIncomplete(g);
1973 +        f2.complete(two);
1974 +        checkCompletedWithWrappedCFException(g);
1975 +    }
1976 +
1977 +    /**
1978 +     * runAfterBothAsync result completes exceptionally if either source cancelled
1979 +     */
1980 +    public void testRunAfterBothAsync4() {
1981 +        CompletableFuture<Integer> f = new CompletableFuture<>();
1982 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1983 +        Noop r = new Noop();
1984 +        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1985 +        assertTrue(f.cancel(true));
1986 +        f2.complete(two);
1987 +        checkCompletedWithWrappedCancellationException(g);
1988 +
1989 +        r = new Noop();
1990 +        f = new CompletableFuture<>();
1991 +        f2 = new CompletableFuture<>();
1992 +        g = f.runAfterBothAsync(f2, r);
1993 +        f.complete(one);
1994 +        assertTrue(f2.cancel(true));
1995 +        checkCompletedWithWrappedCancellationException(g);
1996 +    }
1997 +
1998 +    /**
1999 +     * applyToEitherAsync result completes normally after normal
2000 +     * completion of sources
2001 +     */
2002 +    public void testApplyToEitherAsync() {
2003 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2004 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2005 +        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2006 +        f.complete(one);
2007 +        checkCompletedNormally(g, two);
2008 +
2009 +        f = new CompletableFuture<>();
2010 +        f.complete(one);
2011 +        f2 = new CompletableFuture<>();
2012 +        g = f.applyToEitherAsync(f2, inc);
2013 +        checkCompletedNormally(g, two);
2014 +    }
2015 +
2016 +    /**
2017 +     * applyToEitherAsync result completes exceptionally after exceptional
2018 +     * completion of source
2019 +     */
2020 +    public void testApplyToEitherAsync2() {
2021 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2022 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2023 +        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2024 +        f.completeExceptionally(new CFException());
2025 +        checkCompletedWithWrappedCFException(g);
2026 +
2027 +        f = new CompletableFuture<>();
2028 +        f2 = new CompletableFuture<>();
2029 +        f2.completeExceptionally(new CFException());
2030 +        g = f.applyToEitherAsync(f2, inc);
2031 +        f.complete(one);
2032 +        checkCompletedWithWrappedCFException(g);
2033 +    }
2034 +
2035 +    /**
2036 +     * applyToEitherAsync result completes exceptionally if action does
2037 +     */
2038 +    public void testApplyToEitherAsync3() {
2039 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2040 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2041 +        FailingFunction r = new FailingFunction();
2042 +        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r);
2043 +        f.complete(one);
2044 +        checkCompletedWithWrappedCFException(g);
2045 +    }
2046 +
2047 +    /**
2048 +     * applyToEitherAsync result completes exceptionally if either source cancelled
2049 +     */
2050 +    public void testApplyToEitherAsync4() {
2051 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2052 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2053 +        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2054 +        assertTrue(f.cancel(true));
2055 +        checkCompletedWithWrappedCancellationException(g);
2056 +
2057 +        f = new CompletableFuture<>();
2058 +        f2 = new CompletableFuture<>();
2059 +        assertTrue(f2.cancel(true));
2060 +        g = f.applyToEitherAsync(f2, inc);
2061 +        checkCompletedWithWrappedCancellationException(g);
2062 +    }
2063 +
2064 +    /**
2065 +     * acceptEitherAsync result completes normally after normal
2066 +     * completion of sources
2067 +     */
2068 +    public void testAcceptEitherAsync() {
2069 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2070 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2071 +        IncAction r = new IncAction();
2072 +        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2073 +        f.complete(one);
2074 +        checkCompletedNormally(g, null);
2075 +        assertEquals(r.value, 2);
2076 +
2077 +        r = new IncAction();
2078 +        f = new CompletableFuture<>();
2079 +        f.complete(one);
2080 +        f2 = new CompletableFuture<>();
2081 +        g = f.acceptEitherAsync(f2, r);
2082 +        checkCompletedNormally(g, null);
2083 +        assertEquals(r.value, 2);
2084 +    }
2085 +
2086 +    /**
2087 +     * acceptEitherAsync result completes exceptionally after exceptional
2088 +     * completion of source
2089 +     */
2090 +    public void testAcceptEitherAsync2() {
2091 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2092 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2093 +        IncAction r = new IncAction();
2094 +        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2095 +        f.completeExceptionally(new CFException());
2096 +        checkCompletedWithWrappedCFException(g);
2097 +
2098 +        r = new IncAction();
2099 +        f = new CompletableFuture<>();
2100 +        f2 = new CompletableFuture<>();
2101 +        f2.completeExceptionally(new CFException());
2102 +        g = f.acceptEitherAsync(f2, r);
2103 +        f.complete(one);
2104 +        checkCompletedWithWrappedCFException(g);
2105 +    }
2106 +
2107 +    /**
2108 +     * acceptEitherAsync result completes exceptionally if action does
2109 +     */
2110 +    public void testAcceptEitherAsync3() {
2111 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2112 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2113 +        FailingConsumer r = new FailingConsumer();
2114 +        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2115 +        f.complete(one);
2116 +        checkCompletedWithWrappedCFException(g);
2117 +    }
2118 +
2119 +    /**
2120 +     * acceptEitherAsync result completes exceptionally if either
2121 +     * source cancelled
2122 +     */
2123 +    public void testAcceptEitherAsync4() {
2124 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2125 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2126 +        IncAction r = new IncAction();
2127 +        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2128 +        assertTrue(f.cancel(true));
2129 +        checkCompletedWithWrappedCancellationException(g);
2130 +
2131 +        r = new IncAction();
2132 +        f = new CompletableFuture<>();
2133 +        f2 = new CompletableFuture<>();
2134 +        assertTrue(f2.cancel(true));
2135 +        g = f.acceptEitherAsync(f2, r);
2136 +        checkCompletedWithWrappedCancellationException(g);
2137 +    }
2138 +
2139 +    /**
2140 +     * runAfterEitherAsync result completes normally after normal
2141 +     * completion of sources
2142 +     */
2143 +    public void testRunAfterEitherAsync() {
2144 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2145 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2146 +        Noop r = new Noop();
2147 +        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2148 +        f.complete(one);
2149 +        checkCompletedNormally(g, null);
2150 +        assertTrue(r.ran);
2151 +
2152 +        r = new Noop();
2153 +        f = new CompletableFuture<>();
2154 +        f.complete(one);
2155 +        f2 = new CompletableFuture<>();
2156 +        g = f.runAfterEitherAsync(f2, r);
2157 +        checkCompletedNormally(g, null);
2158 +        assertTrue(r.ran);
2159 +    }
2160 +
2161 +    /**
2162 +     * runAfterEitherAsync result completes exceptionally after exceptional
2163 +     * completion of source
2164 +     */
2165 +    public void testRunAfterEitherAsync2() {
2166 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2167 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2168 +        Noop r = new Noop();
2169 +        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2170 +        f.completeExceptionally(new CFException());
2171 +        checkCompletedWithWrappedCFException(g);
2172 +
2173 +        r = new Noop();
2174 +        f = new CompletableFuture<>();
2175 +        f2 = new CompletableFuture<>();
2176 +        f2.completeExceptionally(new CFException());
2177 +        g = f.runAfterEitherAsync(f2, r);
2178 +        f.complete(one);
2179 +        checkCompletedWithWrappedCFException(g);
2180 +    }
2181 +
2182 +    /**
2183 +     * runAfterEitherAsync result completes exceptionally if action does
2184 +     */
2185 +    public void testRunAfterEitherAsync3() {
2186 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2187 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2188 +        FailingNoop r = new FailingNoop();
2189 +        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2190 +        f.complete(one);
2191 +        checkCompletedWithWrappedCFException(g);
2192 +    }
2193 +
2194 +    /**
2195 +     * runAfterEitherAsync result completes exceptionally if either
2196 +     * source cancelled
2197 +     */
2198 +    public void testRunAfterEitherAsync4() {
2199 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2200 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2201 +        Noop r = new Noop();
2202 +        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2203 +        assertTrue(f.cancel(true));
2204 +        checkCompletedWithWrappedCancellationException(g);
2205 +
2206 +        r = new Noop();
2207 +        f = new CompletableFuture<>();
2208 +        f2 = new CompletableFuture<>();
2209 +        assertTrue(f2.cancel(true));
2210 +        g = f.runAfterEitherAsync(f2, r);
2211 +        checkCompletedWithWrappedCancellationException(g);
2212 +    }
2213 +
2214 +    /**
2215 +     * thenComposeAsync result completes normally after normal
2216 +     * completion of source
2217 +     */
2218 +    public void testThenComposeAsync() {
2219 +        CompletableFuture<Integer> f, g;
2220 +        CompletableFutureInc r;
2221 +
2222 +        f = new CompletableFuture<>();
2223 +        g = f.thenComposeAsync(r = new CompletableFutureInc());
2224 +        f.complete(one);
2225 +        checkCompletedNormally(g, two);
2226 +
2227 +        f = new CompletableFuture<>();
2228 +        f.complete(one);
2229 +        g = f.thenComposeAsync(r = new CompletableFutureInc());
2230 +        checkCompletedNormally(g, two);
2231 +    }
2232 +
2233 +    /**
2234 +     * thenComposeAsync result completes exceptionally after
2235 +     * exceptional completion of source
2236 +     */
2237 +    public void testThenComposeAsync2() {
2238 +        CompletableFuture<Integer> f, g;
2239 +        CompletableFutureInc r;
2240 +
2241 +        f = new CompletableFuture<>();
2242 +        g = f.thenComposeAsync(r = new CompletableFutureInc());
2243 +        f.completeExceptionally(new CFException());
2244 +        checkCompletedWithWrappedCFException(g);
2245 +        assertFalse(r.ran);
2246 +
2247 +        f = new CompletableFuture<>();
2248 +        f.completeExceptionally(new CFException());
2249 +        g = f.thenComposeAsync(r = new CompletableFutureInc());
2250 +        checkCompletedWithWrappedCFException(g);
2251 +        assertFalse(r.ran);
2252 +    }
2253 +
2254 +    /**
2255 +     * thenComposeAsync result completes exceptionally if action does
2256 +     */
2257 +    public void testThenComposeAsync3() {
2258 +        CompletableFuture<Integer> f, g;
2259 +        FailingCompletableFutureFunction r;
2260 +
2261 +        f = new CompletableFuture<>();
2262 +        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
2263 +        f.complete(one);
2264 +        checkCompletedWithWrappedCFException(g);
2265 +
2266 +        f = new CompletableFuture<>();
2267 +        f.complete(one);
2268 +        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
2269 +        checkCompletedWithWrappedCFException(g);
2270 +    }
2271 +
2272 +    /**
2273 +     * thenComposeAsync result completes exceptionally if source cancelled
2274 +     */
2275 +    public void testThenComposeAsync4() {
2276 +        CompletableFuture<Integer> f, g;
2277 +        CompletableFutureInc r;
2278 +
2279 +        f = new CompletableFuture<>();
2280 +        g = f.thenComposeAsync(r = new CompletableFutureInc());
2281 +        assertTrue(f.cancel(true));
2282 +        checkCompletedWithWrappedCancellationException(g);
2283 +
2284 +        f = new CompletableFuture<>();
2285 +        assertTrue(f.cancel(true));
2286 +        g = f.thenComposeAsync(r = new CompletableFutureInc());
2287 +        checkCompletedWithWrappedCancellationException(g);
2288 +    }
2289 +
2290 +    // async with explicit executors
2291 +
2292 +    /**
2293 +     * thenRunAsync result completes normally after normal completion of source
2294 +     */
2295 +    public void testThenRunAsyncE() {
2296 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2297 +        Noop r = new Noop();
2298 +        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2299 +        f.complete(null);
2300 +        checkCompletedNormally(g, null);
2301 +
2302 +        // reordered version
2303 +        f = new CompletableFuture<>();
2304 +        f.complete(null);
2305 +        r = new Noop();
2306 +        g = f.thenRunAsync(r, new ThreadExecutor());
2307 +        checkCompletedNormally(g, null);
2308 +    }
2309 +
2310 +    /**
2311 +     * thenRunAsync result completes exceptionally after exceptional
2312 +     * completion of source
2313 +     */
2314 +    public void testThenRunAsync2E() {
2315 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2316 +        Noop r = new Noop();
2317 +        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2318 +        f.completeExceptionally(new CFException());
2319 +        try {
2320 +            g.join();
2321 +            shouldThrow();
2322 +        } catch (CompletionException success) {}
2323 +        checkCompletedWithWrappedCFException(g);
2324 +    }
2325 +
2326 +    /**
2327 +     * thenRunAsync result completes exceptionally if action does
2328 +     */
2329 +    public void testThenRunAsync3E() {
2330 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2331 +        FailingNoop r = new FailingNoop();
2332 +        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2333 +        f.complete(null);
2334 +        checkCompletedWithWrappedCFException(g);
2335 +    }
2336 +
2337 +    /**
2338 +     * thenRunAsync result completes exceptionally if source cancelled
2339 +     */
2340 +    public void testThenRunAsync4E() {
2341 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2342 +        Noop r = new Noop();
2343 +        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2344 +        assertTrue(f.cancel(true));
2345 +        checkCompletedWithWrappedCancellationException(g);
2346 +    }
2347 +
2348 +    /**
2349 +     * thenApplyAsync result completes normally after normal completion of source
2350 +     */
2351 +    public void testThenApplyAsyncE() {
2352 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2353 +        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
2354 +        f.complete(one);
2355 +        checkCompletedNormally(g, two);
2356 +    }
2357 +
2358 +    /**
2359 +     * thenApplyAsync result completes exceptionally after exceptional
2360 +     * completion of source
2361 +     */
2362 +    public void testThenApplyAsync2E() {
2363 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2364 +        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
2365 +        f.completeExceptionally(new CFException());
2366 +        checkCompletedWithWrappedCFException(g);
2367 +    }
2368 +
2369 +    /**
2370 +     * thenApplyAsync result completes exceptionally if action does
2371 +     */
2372 +    public void testThenApplyAsync3E() {
2373 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2374 +        FailingFunction r = new FailingFunction();
2375 +        CompletableFuture<Integer> g = f.thenApplyAsync(r, new ThreadExecutor());
2376 +        f.complete(null);
2377 +        checkCompletedWithWrappedCFException(g);
2378 +    }
2379 +
2380 +    /**
2381 +     * thenApplyAsync result completes exceptionally if source cancelled
2382 +     */
2383 +    public void testThenApplyAsync4E() {
2384 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2385 +        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
2386 +        assertTrue(f.cancel(true));
2387 +        checkCompletedWithWrappedCancellationException(g);
2388 +    }
2389 +
2390 +    /**
2391 +     * thenAcceptAsync result completes normally after normal
2392 +     * completion of source
2393 +     */
2394 +    public void testThenAcceptAsyncE() {
2395 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2396 +        IncAction r = new IncAction();
2397 +        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2398 +        f.complete(one);
2399 +        checkCompletedNormally(g, null);
2400 +        assertEquals(r.value, 2);
2401 +    }
2402 +
2403 +    /**
2404 +     * thenAcceptAsync result completes exceptionally after exceptional
2405 +     * completion of source
2406 +     */
2407 +    public void testThenAcceptAsync2E() {
2408 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2409 +        IncAction r = new IncAction();
2410 +        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2411 +        f.completeExceptionally(new CFException());
2412 +        checkCompletedWithWrappedCFException(g);
2413 +    }
2414 +
2415 +    /**
2416 +     * thenAcceptAsync result completes exceptionally if action does
2417 +     */
2418 +    public void testThenAcceptAsync3E() {
2419 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2420 +        FailingConsumer r = new FailingConsumer();
2421 +        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2422 +        f.complete(null);
2423 +        checkCompletedWithWrappedCFException(g);
2424 +    }
2425 +
2426 +    /**
2427 +     * thenAcceptAsync result completes exceptionally if source cancelled
2428 +     */
2429 +    public void testThenAcceptAsync4E() {
2430 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2431 +        IncAction r = new IncAction();
2432 +        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2433 +        assertTrue(f.cancel(true));
2434 +        checkCompletedWithWrappedCancellationException(g);
2435 +    }
2436 +
2437 +    /**
2438 +     * thenCombineAsync result completes normally after normal
2439 +     * completion of sources
2440 +     */
2441 +    public void testThenCombineAsyncE() {
2442 +        CompletableFuture<Integer> f, g, h;
2443 +        ThreadExecutor e = new ThreadExecutor();
2444 +        int count = 0;
2445 +
2446 +        f = new CompletableFuture<>();
2447 +        g = new CompletableFuture<>();
2448 +        h = f.thenCombineAsync(g, subtract, e);
2449 +        f.complete(3);
2450 +        checkIncomplete(h);
2451 +        g.complete(1);
2452 +        checkCompletedNormally(h, 2);
2453 +        assertEquals(++count, e.count.get());
2454 +
2455 +        f = new CompletableFuture<>();
2456 +        g = new CompletableFuture<>();
2457 +        h = f.thenCombineAsync(g, subtract, e);
2458 +        g.complete(1);
2459 +        checkIncomplete(h);
2460 +        f.complete(3);
2461 +        checkCompletedNormally(h, 2);
2462 +        assertEquals(++count, e.count.get());
2463 +
2464 +        f = new CompletableFuture<>();
2465 +        g = new CompletableFuture<>();
2466 +        g.complete(1);
2467 +        f.complete(3);
2468 +        h = f.thenCombineAsync(g, subtract, e);
2469 +        checkCompletedNormally(h, 2);
2470 +        assertEquals(++count, e.count.get());
2471 +    }
2472 +
2473 +    /**
2474 +     * thenCombineAsync result completes exceptionally after exceptional
2475 +     * completion of either source
2476 +     */
2477 +    public void testThenCombineAsync2E() {
2478 +        CompletableFuture<Integer> f, g, h;
2479 +        ThreadExecutor e = new ThreadExecutor();
2480 +        int count = 0;
2481 +
2482 +        f = new CompletableFuture<>();
2483 +        g = new CompletableFuture<>();
2484 +        h = f.thenCombineAsync(g, subtract, e);
2485 +        f.completeExceptionally(new CFException());
2486 +        checkIncomplete(h);
2487 +        g.complete(1);
2488 +        checkCompletedWithWrappedCFException(h);
2489 +
2490 +        f = new CompletableFuture<>();
2491 +        g = new CompletableFuture<>();
2492 +        h = f.thenCombineAsync(g, subtract, e);
2493 +        g.completeExceptionally(new CFException());
2494 +        checkIncomplete(h);
2495 +        f.complete(3);
2496 +        checkCompletedWithWrappedCFException(h);
2497 +
2498 +        f = new CompletableFuture<>();
2499 +        g = new CompletableFuture<>();
2500 +        g.completeExceptionally(new CFException());
2501 +        h = f.thenCombineAsync(g, subtract, e);
2502 +        checkIncomplete(h);
2503 +        f.complete(3);
2504 +        checkCompletedWithWrappedCFException(h);
2505 +
2506 +        assertEquals(0, e.count.get());
2507 +    }
2508 +
2509 +    /**
2510 +     * thenCombineAsync result completes exceptionally if action does
2511 +     */
2512 +    public void testThenCombineAsync3E() {
2513 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2514 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2515 +        FailingBiFunction r = new FailingBiFunction();
2516 +        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r, new ThreadExecutor());
2517 +        f.complete(one);
2518 +        checkIncomplete(g);
2519 +        assertFalse(r.ran);
2520 +        f2.complete(two);
2521 +        checkCompletedWithWrappedCFException(g);
2522 +        assertTrue(r.ran);
2523 +    }
2524 +
2525 +    /**
2526 +     * thenCombineAsync result completes exceptionally if either source cancelled
2527 +     */
2528 +    public void testThenCombineAsync4E() {
2529 +        CompletableFuture<Integer> f, g, h;
2530 +        ThreadExecutor e = new ThreadExecutor();
2531 +
2532 +        f = new CompletableFuture<>();
2533 +        g = new CompletableFuture<>();
2534 +        h = f.thenCombineAsync(g, subtract, e);
2535 +        assertTrue(f.cancel(true));
2536 +        checkIncomplete(h);
2537 +        g.complete(1);
2538 +        checkCompletedWithWrappedCancellationException(h);
2539 +
2540 +        f = new CompletableFuture<>();
2541 +        g = new CompletableFuture<>();
2542 +        h = f.thenCombineAsync(g, subtract, e);
2543 +        assertTrue(g.cancel(true));
2544 +        checkIncomplete(h);
2545 +        f.complete(3);
2546 +        checkCompletedWithWrappedCancellationException(h);
2547 +
2548 +        f = new CompletableFuture<>();
2549 +        g = new CompletableFuture<>();
2550 +        assertTrue(g.cancel(true));
2551 +        h = f.thenCombineAsync(g, subtract, e);
2552 +        checkIncomplete(h);
2553 +        f.complete(3);
2554 +        checkCompletedWithWrappedCancellationException(h);
2555 +
2556 +        f = new CompletableFuture<>();
2557 +        g = new CompletableFuture<>();
2558 +        assertTrue(f.cancel(true));
2559 +        assertTrue(g.cancel(true));
2560 +        h = f.thenCombineAsync(g, subtract, e);
2561 +        checkCompletedWithWrappedCancellationException(h);
2562 +
2563 +        assertEquals(0, e.count.get());
2564 +    }
2565 +
2566 +    /**
2567 +     * thenAcceptBothAsync result completes normally after normal
2568 +     * completion of sources
2569 +     */
2570 +    public void testThenAcceptBothAsyncE() {
2571 +        CompletableFuture<Integer> f, g;
2572 +        CompletableFuture<Void> h;
2573 +        SubtractAction r;
2574 +        ThreadExecutor e = new ThreadExecutor();
2575 +
2576 +        f = new CompletableFuture<>();
2577 +        g = new CompletableFuture<>();
2578 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2579 +        f.complete(3);
2580 +        checkIncomplete(h);
2581 +        g.complete(1);
2582 +        checkCompletedNormally(h, null);
2583 +        assertEquals(r.value, 2);
2584 +
2585 +        f = new CompletableFuture<>();
2586 +        g = new CompletableFuture<>();
2587 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2588 +        g.complete(1);
2589 +        checkIncomplete(h);
2590 +        f.complete(3);
2591 +        checkCompletedNormally(h, null);
2592 +        assertEquals(r.value, 2);
2593 +
2594 +        f = new CompletableFuture<>();
2595 +        g = new CompletableFuture<>();
2596 +        g.complete(1);
2597 +        f.complete(3);
2598 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2599 +        checkCompletedNormally(h, null);
2600 +        assertEquals(r.value, 2);
2601 +
2602 +        assertEquals(3, e.count.get());
2603 +    }
2604 +
2605 +    /**
2606 +     * thenAcceptBothAsync result completes exceptionally after exceptional
2607 +     * completion of source
2608 +     */
2609 +    public void testThenAcceptBothAsync2E() {
2610 +        CompletableFuture<Integer> f, g;
2611 +        CompletableFuture<Void> h;
2612 +        SubtractAction r;
2613 +        ThreadExecutor e = new ThreadExecutor();
2614 +
2615 +        f = new CompletableFuture<>();
2616 +        g = new CompletableFuture<>();
2617 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2618 +        f.completeExceptionally(new CFException());
2619 +        checkIncomplete(h);
2620 +        g.complete(1);
2621 +        checkCompletedWithWrappedCFException(h);
2622 +
2623 +        f = new CompletableFuture<>();
2624 +        g = new CompletableFuture<>();
2625 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2626 +        g.completeExceptionally(new CFException());
2627 +        checkIncomplete(h);
2628 +        f.complete(3);
2629 +        checkCompletedWithWrappedCFException(h);
2630 +
2631 +        f = new CompletableFuture<>();
2632 +        g = new CompletableFuture<>();
2633 +        f.complete(3);
2634 +        g.completeExceptionally(new CFException());
2635 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2636 +        checkCompletedWithWrappedCFException(h);
2637 +
2638 +        f = new CompletableFuture<>();
2639 +        g = new CompletableFuture<>();
2640 +        f.completeExceptionally(new CFException());
2641 +        g.complete(3);
2642 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2643 +        checkCompletedWithWrappedCFException(h);
2644 +
2645 +        assertEquals(0, e.count.get());
2646 +    }
2647 +
2648 +    /**
2649 +     * thenAcceptBothAsync result completes exceptionally if action does
2650 +     */
2651 +    public void testThenAcceptBothAsync3E() {
2652 +        CompletableFuture<Integer> f, g;
2653 +        CompletableFuture<Void> h;
2654 +        FailingBiConsumer r;
2655 +        ThreadExecutor e = new ThreadExecutor();
2656 +
2657 +        f = new CompletableFuture<>();
2658 +        g = new CompletableFuture<>();
2659 +        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer(), e);
2660 +        f.complete(3);
2661 +        checkIncomplete(h);
2662 +        g.complete(1);
2663 +        checkCompletedWithWrappedCFException(h);
2664 +
2665 +        f = new CompletableFuture<>();
2666 +        g = new CompletableFuture<>();
2667 +        f.complete(3);
2668 +        g.complete(1);
2669 +        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer(), e);
2670 +        checkCompletedWithWrappedCFException(h);
2671 +
2672 +        assertEquals(2, e.count.get());
2673 +    }
2674 +
2675 +    /**
2676 +     * thenAcceptBothAsync result completes exceptionally if either source cancelled
2677 +     */
2678 +    public void testThenAcceptBothAsync4E() {
2679 +        CompletableFuture<Integer> f, g;
2680 +        CompletableFuture<Void> h;
2681 +        SubtractAction r;
2682 +        ThreadExecutor e = new ThreadExecutor();
2683 +
2684 +        f = new CompletableFuture<>();
2685 +        g = new CompletableFuture<>();
2686 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2687 +        assertTrue(f.cancel(true));
2688 +        checkIncomplete(h);
2689 +        g.complete(1);
2690 +        checkCompletedWithWrappedCancellationException(h);
2691 +
2692 +        f = new CompletableFuture<>();
2693 +        g = new CompletableFuture<>();
2694 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2695 +        assertTrue(g.cancel(true));
2696 +        checkIncomplete(h);
2697 +        f.complete(3);
2698 +        checkCompletedWithWrappedCancellationException(h);
2699 +
2700 +        f = new CompletableFuture<>();
2701 +        g = new CompletableFuture<>();
2702 +        f.complete(3);
2703 +        assertTrue(g.cancel(true));
2704 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2705 +        checkCompletedWithWrappedCancellationException(h);
2706 +
2707 +        f = new CompletableFuture<>();
2708 +        g = new CompletableFuture<>();
2709 +        assertTrue(f.cancel(true));
2710 +        g.complete(3);
2711 +        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2712 +        checkCompletedWithWrappedCancellationException(h);
2713 +
2714 +        assertEquals(0, e.count.get());
2715 +    }
2716 +
2717 +    /**
2718 +     * runAfterBothAsync result completes normally after normal
2719 +     * completion of sources
2720 +     */
2721 +    public void testRunAfterBothAsyncE() {
2722 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2723 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2724 +        Noop r = new Noop();
2725 +        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2726 +        f.complete(one);
2727 +        checkIncomplete(g);
2728 +        f2.complete(two);
2729 +        checkCompletedNormally(g, null);
2730 +        assertTrue(r.ran);
2731 +    }
2732 +
2733 +    /**
2734 +     * runAfterBothAsync result completes exceptionally after exceptional
2735 +     * completion of source
2736 +     */
2737 +    public void testRunAfterBothAsync2E() {
2738 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2739 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2740 +        Noop r = new Noop();
2741 +        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2742 +        f.completeExceptionally(new CFException());
2743 +        f2.complete(two);
2744 +        checkCompletedWithWrappedCFException(g);
2745 +
2746 +        r = new Noop();
2747 +        f = new CompletableFuture<>();
2748 +        f2 = new CompletableFuture<>();
2749 +        g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2750 +        f.complete(one);
2751 +        f2.completeExceptionally(new CFException());
2752 +        checkCompletedWithWrappedCFException(g);
2753 +    }
2754 +
2755 +    /**
2756 +     * runAfterBothAsync result completes exceptionally if action does
2757 +     */
2758 +    public void testRunAfterBothAsync3E() {
2759 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2760 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2761 +        FailingNoop r = new FailingNoop();
2762 +        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2763 +        f.complete(one);
2764 +        checkIncomplete(g);
2765 +        f2.complete(two);
2766 +        checkCompletedWithWrappedCFException(g);
2767 +    }
2768 +
2769 +    /**
2770 +     * runAfterBothAsync result completes exceptionally if either source cancelled
2771 +     */
2772 +    public void testRunAfterBothAsync4E() {
2773 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2774 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2775 +        Noop r = new Noop();
2776 +        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2777 +        assertTrue(f.cancel(true));
2778 +        f2.complete(two);
2779 +        checkCompletedWithWrappedCancellationException(g);
2780 +
2781 +        r = new Noop();
2782 +        f = new CompletableFuture<>();
2783 +        f2 = new CompletableFuture<>();
2784 +        g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2785 +        f.complete(one);
2786 +        assertTrue(f2.cancel(true));
2787 +        checkCompletedWithWrappedCancellationException(g);
2788 +    }
2789 +
2790 +    /**
2791 +     * applyToEitherAsync result completes normally after normal
2792 +     * completion of sources
2793 +     */
2794 +    public void testApplyToEitherAsyncE() {
2795 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2796 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2797 +        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2798 +        f.complete(one);
2799 +        checkCompletedNormally(g, two);
2800 +
2801 +        f = new CompletableFuture<>();
2802 +        f.complete(one);
2803 +        f2 = new CompletableFuture<>();
2804 +        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2805 +        checkCompletedNormally(g, two);
2806 +    }
2807 +
2808 +    /**
2809 +     * applyToEitherAsync result completes exceptionally after exceptional
2810 +     * completion of source
2811 +     */
2812 +    public void testApplyToEitherAsync2E() {
2813 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2814 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2815 +        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2816 +        f.completeExceptionally(new CFException());
2817 +        checkCompletedWithWrappedCFException(g);
2818 +
2819 +        f = new CompletableFuture<>();
2820 +        f2 = new CompletableFuture<>();
2821 +        f2.completeExceptionally(new CFException());
2822 +        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2823 +        f.complete(one);
2824 +        checkCompletedWithWrappedCFException(g);
2825 +    }
2826 +
2827 +    /**
2828 +     * applyToEitherAsync result completes exceptionally if action does
2829 +     */
2830 +    public void testApplyToEitherAsync3E() {
2831 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2832 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2833 +        FailingFunction r = new FailingFunction();
2834 +        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor());
2835 +        f.complete(one);
2836 +        checkCompletedWithWrappedCFException(g);
2837 +    }
2838 +
2839 +    /**
2840 +     * applyToEitherAsync result completes exceptionally if either source cancelled
2841 +     */
2842 +    public void testApplyToEitherAsync4E() {
2843 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2844 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2845 +        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2846 +        assertTrue(f.cancel(true));
2847 +        checkCompletedWithWrappedCancellationException(g);
2848 +
2849 +        f = new CompletableFuture<>();
2850 +        f2 = new CompletableFuture<>();
2851 +        assertTrue(f2.cancel(true));
2852 +        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2853 +        checkCompletedWithWrappedCancellationException(g);
2854 +    }
2855 +
2856 +    /**
2857 +     * acceptEitherAsync result completes normally after normal
2858 +     * completion of sources
2859 +     */
2860 +    public void testAcceptEitherAsyncE() {
2861 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2862 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2863 +        IncAction r = new IncAction();
2864 +        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2865 +        f.complete(one);
2866 +        checkCompletedNormally(g, null);
2867 +        assertEquals(r.value, 2);
2868 +
2869 +        r = new IncAction();
2870 +        f = new CompletableFuture<>();
2871 +        f.complete(one);
2872 +        f2 = new CompletableFuture<>();
2873 +        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2874 +        checkCompletedNormally(g, null);
2875 +        assertEquals(r.value, 2);
2876 +    }
2877 +
2878 +    /**
2879 +     * acceptEitherAsync result completes exceptionally after exceptional
2880 +     * completion of source
2881 +     */
2882 +    public void testAcceptEitherAsync2E() {
2883 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2884 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2885 +        IncAction r = new IncAction();
2886 +        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2887 +        f.completeExceptionally(new CFException());
2888 +        checkCompletedWithWrappedCFException(g);
2889 +
2890 +        r = new IncAction();
2891 +        f = new CompletableFuture<>();
2892 +        f2 = new CompletableFuture<>();
2893 +        f2.completeExceptionally(new CFException());
2894 +        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2895 +        f.complete(one);
2896 +        checkCompletedWithWrappedCFException(g);
2897 +    }
2898 +
2899 +    /**
2900 +     * acceptEitherAsync result completes exceptionally if action does
2901 +     */
2902 +    public void testAcceptEitherAsync3E() {
2903 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2904 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2905 +        FailingConsumer r = new FailingConsumer();
2906 +        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2907 +        f.complete(one);
2908 +        checkCompletedWithWrappedCFException(g);
2909 +    }
2910 +
2911 +    /**
2912 +     * acceptEitherAsync result completes exceptionally if either
2913 +     * source cancelled
2914 +     */
2915 +    public void testAcceptEitherAsync4E() {
2916 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2917 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2918 +        IncAction r = new IncAction();
2919 +        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2920 +        assertTrue(f.cancel(true));
2921 +        checkCompletedWithWrappedCancellationException(g);
2922 +
2923 +        r = new IncAction();
2924 +        f = new CompletableFuture<>();
2925 +        f2 = new CompletableFuture<>();
2926 +        assertTrue(f2.cancel(true));
2927 +        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2928 +        checkCompletedWithWrappedCancellationException(g);
2929 +    }
2930 +
2931 +    /**
2932 +     * runAfterEitherAsync result completes normally after normal
2933 +     * completion of sources
2934 +     */
2935 +    public void testRunAfterEitherAsyncE() {
2936 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2937 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2938 +        Noop r = new Noop();
2939 +        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2940 +        f.complete(one);
2941 +        checkCompletedNormally(g, null);
2942 +        assertTrue(r.ran);
2943 +
2944 +        r = new Noop();
2945 +        f = new CompletableFuture<>();
2946 +        f.complete(one);
2947 +        f2 = new CompletableFuture<>();
2948 +        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2949 +        checkCompletedNormally(g, null);
2950 +        assertTrue(r.ran);
2951 +    }
2952 +
2953 +    /**
2954 +     * runAfterEitherAsync result completes exceptionally after exceptional
2955 +     * completion of source
2956 +     */
2957 +    public void testRunAfterEitherAsync2E() {
2958 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2959 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2960 +        Noop r = new Noop();
2961 +        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2962 +        f.completeExceptionally(new CFException());
2963 +        checkCompletedWithWrappedCFException(g);
2964 +
2965 +        r = new Noop();
2966 +        f = new CompletableFuture<>();
2967 +        f2 = new CompletableFuture<>();
2968 +        f2.completeExceptionally(new CFException());
2969 +        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2970 +        f.complete(one);
2971 +        checkCompletedWithWrappedCFException(g);
2972 +    }
2973 +
2974 +    /**
2975 +     * runAfterEitherAsync result completes exceptionally if action does
2976 +     */
2977 +    public void testRunAfterEitherAsync3E() {
2978 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2979 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2980 +        FailingNoop r = new FailingNoop();
2981 +        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2982 +        f.complete(one);
2983 +        checkCompletedWithWrappedCFException(g);
2984 +    }
2985 +
2986 +    /**
2987 +     * runAfterEitherAsync result completes exceptionally if either
2988 +     * source cancelled
2989 +     */
2990 +    public void testRunAfterEitherAsync4E() {
2991 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2992 +        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2993 +        Noop r = new Noop();
2994 +        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2995 +        assertTrue(f.cancel(true));
2996 +        checkCompletedWithWrappedCancellationException(g);
2997 +
2998 +        r = new Noop();
2999 +        f = new CompletableFuture<>();
3000 +        f2 = new CompletableFuture<>();
3001 +        assertTrue(f2.cancel(true));
3002 +        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
3003 +        checkCompletedWithWrappedCancellationException(g);
3004 +    }
3005 +
3006 +    /**
3007 +     * thenComposeAsync result completes normally after normal
3008 +     * completion of source
3009 +     */
3010 +    public void testThenComposeAsyncE() {
3011 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3012 +        CompletableFutureInc r = new CompletableFutureInc();
3013 +        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
3014 +        f.complete(one);
3015 +        checkCompletedNormally(g, two);
3016 +    }
3017 +
3018 +    /**
3019 +     * thenComposeAsync result completes exceptionally after
3020 +     * exceptional completion of source
3021 +     */
3022 +    public void testThenComposeAsync2E() {
3023 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3024 +        CompletableFutureInc r = new CompletableFutureInc();
3025 +        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
3026 +        f.completeExceptionally(new CFException());
3027 +        checkCompletedWithWrappedCFException(g);
3028 +    }
3029 +
3030 +    /**
3031 +     * thenComposeAsync result completes exceptionally if action does
3032 +     */
3033 +    public void testThenComposeAsync3E() {
3034 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3035 +        FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
3036 +        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
3037 +        f.complete(one);
3038 +        checkCompletedWithWrappedCFException(g);
3039 +    }
3040 +
3041 +    /**
3042 +     * thenComposeAsync result completes exceptionally if source cancelled
3043 +     */
3044 +    public void testThenComposeAsync4E() {
3045 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3046 +        CompletableFutureInc r = new CompletableFutureInc();
3047 +        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
3048 +        assertTrue(f.cancel(true));
3049 +        checkCompletedWithWrappedCancellationException(g);
3050 +    }
3051 +
3052 +    // other static methods
3053 +
3054 +    /**
3055       * allOf(no component futures) returns a future completed normally
3056       * with the value null
3057       */
3058      public void testAllOf_empty() throws Exception {
3059 <        CompletableFuture<?> f = CompletableFuture.allOf();
3059 >        CompletableFuture<Void> f = CompletableFuture.allOf();
3060          checkCompletedNormally(f, null);
3061      }
3062  
3063      /**
3064 +     * allOf returns a future completed normally with the value null
3065 +     * when all components complete normally
3066 +     */
3067 +    public void testAllOf_normal() throws Exception {
3068 +        for (int k = 1; k < 20; ++k) {
3069 +            CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3070 +            for (int i = 0; i < k; ++i)
3071 +                fs[i] = new CompletableFuture<>();
3072 +            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3073 +            for (int i = 0; i < k; ++i) {
3074 +                checkIncomplete(f);
3075 +                checkIncomplete(CompletableFuture.allOf(fs));
3076 +                fs[i].complete(one);
3077 +            }
3078 +            checkCompletedNormally(f, null);
3079 +            checkCompletedNormally(CompletableFuture.allOf(fs), null);
3080 +        }
3081 +    }
3082 +
3083 +    /**
3084       * anyOf(no component futures) returns an incomplete future
3085       */
3086      public void testAnyOf_empty() throws Exception {
3087 <        CompletableFuture<?> f = CompletableFuture.anyOf();
3087 >        CompletableFuture<Object> f = CompletableFuture.anyOf();
3088          checkIncomplete(f);
3089      }
3090 +
3091 +    /**
3092 +     * anyOf returns a future completed normally with a value when
3093 +     * a component future does
3094 +     */
3095 +    public void testAnyOf_normal() throws Exception {
3096 +        for (int k = 0; k < 10; ++k) {
3097 +            CompletableFuture[] fs = new CompletableFuture[k];
3098 +            for (int i = 0; i < k; ++i)
3099 +                fs[i] = new CompletableFuture<>();
3100 +            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3101 +            checkIncomplete(f);
3102 +            for (int i = 0; i < k; ++i) {
3103 +                fs[i].complete(one);
3104 +                checkCompletedNormally(f, one);
3105 +                checkCompletedNormally(CompletableFuture.anyOf(fs), one);
3106 +            }
3107 +        }
3108 +    }
3109 +
3110 +    /**
3111 +     * anyOf result completes exceptionally when any component does.
3112 +     */
3113 +    public void testAnyOf_exceptional() throws Exception {
3114 +        for (int k = 0; k < 10; ++k) {
3115 +            CompletableFuture[] fs = new CompletableFuture[k];
3116 +            for (int i = 0; i < k; ++i)
3117 +                fs[i] = new CompletableFuture<>();
3118 +            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3119 +            checkIncomplete(f);
3120 +            for (int i = 0; i < k; ++i) {
3121 +                fs[i].completeExceptionally(new CFException());
3122 +                checkCompletedWithWrappedCFException(f);
3123 +                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3124 +            }
3125 +        }
3126 +    }
3127 +
3128 +    /**
3129 +     * Completion methods throw NullPointerException with null arguments
3130 +     */
3131 +    public void testNPE() {
3132 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3133 +        CompletableFuture<Integer> g = new CompletableFuture<>();
3134 +        CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null;
3135 +        CompletableFuture<?> h;
3136 +        ThreadExecutor exec = new ThreadExecutor();
3137 +
3138 +        Runnable[] throwingActions = {
3139 +            () -> CompletableFuture.supplyAsync(null),
3140 +            () -> CompletableFuture.supplyAsync(null, exec),
3141 +            () -> CompletableFuture.supplyAsync(supplyOne, null),
3142 +
3143 +            () -> CompletableFuture.runAsync(null),
3144 +            () -> CompletableFuture.runAsync(null, exec),
3145 +            () -> CompletableFuture.runAsync(() -> {}, null),
3146 +
3147 +            () -> f.completeExceptionally(null),
3148 +
3149 +            () -> f.thenApply(null),
3150 +            () -> f.thenApplyAsync(null),
3151 +            () -> f.thenApplyAsync((x) -> x, null),
3152 +            () -> f.thenApplyAsync(null, exec),
3153 +
3154 +            () -> f.thenAccept(null),
3155 +            () -> f.thenAcceptAsync(null),
3156 +            () -> f.thenAcceptAsync((x) -> {} , null),
3157 +            () -> f.thenAcceptAsync(null, exec),
3158 +
3159 +            () -> f.thenRun(null),
3160 +            () -> f.thenRunAsync(null),
3161 +            () -> f.thenRunAsync(() -> {} , null),
3162 +            () -> f.thenRunAsync(null, exec),
3163 +
3164 +            () -> f.thenCombine(g, null),
3165 +            () -> f.thenCombineAsync(g, null),
3166 +            () -> f.thenCombineAsync(g, null, exec),
3167 +            () -> f.thenCombine(nullFuture, (x, y) -> x),
3168 +            () -> f.thenCombineAsync(nullFuture, (x, y) -> x),
3169 +            () -> f.thenCombineAsync(nullFuture, (x, y) -> x, exec),
3170 +            () -> f.thenCombineAsync(g, (x, y) -> x, null),
3171 +
3172 +            () -> f.thenAcceptBoth(g, null),
3173 +            () -> f.thenAcceptBothAsync(g, null),
3174 +            () -> f.thenAcceptBothAsync(g, null, exec),
3175 +            () -> f.thenAcceptBoth(nullFuture, (x, y) -> {}),
3176 +            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}),
3177 +            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec),
3178 +            () -> f.thenAcceptBothAsync(g, (x, y) -> {}, null),
3179 +
3180 +            () -> f.runAfterBoth(g, null),
3181 +            () -> f.runAfterBothAsync(g, null),
3182 +            () -> f.runAfterBothAsync(g, null, exec),
3183 +            () -> f.runAfterBoth(nullFuture, () -> {}),
3184 +            () -> f.runAfterBothAsync(nullFuture, () -> {}),
3185 +            () -> f.runAfterBothAsync(nullFuture, () -> {}, exec),
3186 +            () -> f.runAfterBothAsync(g, () -> {}, null),
3187 +
3188 +            () -> f.applyToEither(g, null),
3189 +            () -> f.applyToEitherAsync(g, null),
3190 +            () -> f.applyToEitherAsync(g, null, exec),
3191 +            () -> f.applyToEither(nullFuture, (x) -> x),
3192 +            () -> f.applyToEitherAsync(nullFuture, (x) -> x),
3193 +            () -> f.applyToEitherAsync(nullFuture, (x) -> x, exec),
3194 +            () -> f.applyToEitherAsync(g, (x) -> x, null),
3195 +
3196 +            () -> f.acceptEither(g, null),
3197 +            () -> f.acceptEitherAsync(g, null),
3198 +            () -> f.acceptEitherAsync(g, null, exec),
3199 +            () -> f.acceptEither(nullFuture, (x) -> {}),
3200 +            () -> f.acceptEitherAsync(nullFuture, (x) -> {}),
3201 +            () -> f.acceptEitherAsync(nullFuture, (x) -> {}, exec),
3202 +            () -> f.acceptEitherAsync(g, (x) -> {}, null),
3203 +
3204 +            () -> f.runAfterEither(g, null),
3205 +            () -> f.runAfterEitherAsync(g, null),
3206 +            () -> f.runAfterEitherAsync(g, null, exec),
3207 +            () -> f.runAfterEither(nullFuture, () -> {}),
3208 +            () -> f.runAfterEitherAsync(nullFuture, () -> {}),
3209 +            () -> f.runAfterEitherAsync(nullFuture, () -> {}, exec),
3210 +            () -> f.runAfterEitherAsync(g, () -> {}, null),
3211 +
3212 +            () -> f.thenCompose(null),
3213 +            () -> f.thenComposeAsync(null),
3214 +            () -> f.thenComposeAsync(new CompletableFutureInc(), null),
3215 +            () -> f.thenComposeAsync(null, exec),
3216 +
3217 +            () -> f.exceptionally(null),
3218 +
3219 +            () -> f.handle(null),
3220 +
3221 +            () -> CompletableFuture.allOf((CompletableFuture<?>)null),
3222 +            () -> CompletableFuture.allOf((CompletableFuture<?>[])null),
3223 +            () -> CompletableFuture.allOf(f, null),
3224 +            () -> CompletableFuture.allOf(null, f),
3225 +
3226 +            () -> CompletableFuture.anyOf((CompletableFuture<?>)null),
3227 +            () -> CompletableFuture.anyOf((CompletableFuture<?>[])null),
3228 +            () -> CompletableFuture.anyOf(f, null),
3229 +            () -> CompletableFuture.anyOf(null, f),
3230 +
3231 +            () -> f.obtrudeException(null),
3232 +        };
3233 +
3234 +        assertThrows(NullPointerException.class, throwingActions);
3235 +        assertEquals(0, exec.count.get());
3236 +    }
3237 +
3238 +    /**
3239 +     * toCompletableFuture returns this CompletableFuture.
3240 +     */
3241 +    public void testToCompletableFuture() {
3242 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3243 +        assertSame(f, f.toCompletableFuture());
3244 +    }
3245 +
3246 +    /**
3247 +     * whenComplete action executes on normal completion, propagating
3248 +     * source result.
3249 +     */
3250 +    public void testWhenComplete1() {
3251 +        final AtomicInteger a = new AtomicInteger();
3252 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3253 +        CompletableFuture<Integer> g =
3254 +            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3255 +        f.complete(three);
3256 +        checkCompletedNormally(f, three);
3257 +        checkCompletedNormally(g, three);
3258 +        assertEquals(a.get(), 1);
3259 +    }
3260 +
3261 +    /**
3262 +     * whenComplete action executes on exceptional completion, propagating
3263 +     * source result.
3264 +     */
3265 +    public void testWhenComplete2() {
3266 +        final AtomicInteger a = new AtomicInteger();
3267 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3268 +        CompletableFuture<Integer> g =
3269 +            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3270 +        f.completeExceptionally(new CFException());
3271 +        assertTrue(f.isCompletedExceptionally());
3272 +        assertTrue(g.isCompletedExceptionally());
3273 +        assertEquals(a.get(), 1);
3274 +    }
3275 +
3276 +    /**
3277 +     * If a whenComplete action throws an exception when triggered by
3278 +     * a normal completion, it completes exceptionally
3279 +     */
3280 +    public void testWhenComplete3() {
3281 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3282 +        CompletableFuture<Integer> g =
3283 +            f.whenComplete((Integer x, Throwable t) ->
3284 +                           { throw new CFException(); } );
3285 +        f.complete(three);
3286 +        checkCompletedNormally(f, three);
3287 +        assertTrue(g.isCompletedExceptionally());
3288 +        checkCompletedWithWrappedCFException(g);
3289 +    }
3290 +
3291 +    /**
3292 +     * whenCompleteAsync action executes on normal completion, propagating
3293 +     * source result.
3294 +     */
3295 +    public void testWhenCompleteAsync1() {
3296 +        final AtomicInteger a = new AtomicInteger();
3297 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3298 +        CompletableFuture<Integer> g =
3299 +            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3300 +        f.complete(three);
3301 +        checkCompletedNormally(f, three);
3302 +        checkCompletedNormally(g, three);
3303 +        assertEquals(a.get(), 1);
3304 +    }
3305 +
3306 +    /**
3307 +     * whenCompleteAsync action executes on exceptional completion, propagating
3308 +     * source result.
3309 +     */
3310 +    public void testWhenCompleteAsync2() {
3311 +        final AtomicInteger a = new AtomicInteger();
3312 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3313 +        CompletableFuture<Integer> g =
3314 +            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3315 +        f.completeExceptionally(new CFException());
3316 +        checkCompletedWithWrappedCFException(f);
3317 +        checkCompletedWithWrappedCFException(g);
3318 +    }
3319 +
3320 +    /**
3321 +     * If a whenCompleteAsync action throws an exception when
3322 +     * triggered by a normal completion, it completes exceptionally
3323 +     */
3324 +    public void testWhenCompleteAsync3() {
3325 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3326 +        CompletableFuture<Integer> g =
3327 +            f.whenCompleteAsync((Integer x, Throwable t) ->
3328 +                           { throw new CFException(); } );
3329 +        f.complete(three);
3330 +        checkCompletedNormally(f, three);
3331 +        checkCompletedWithWrappedCFException(g);
3332 +    }
3333 +
3334 +    /**
3335 +     * whenCompleteAsync action executes on normal completion, propagating
3336 +     * source result.
3337 +     */
3338 +    public void testWhenCompleteAsync1e() {
3339 +        final AtomicInteger a = new AtomicInteger();
3340 +        ThreadExecutor exec = new ThreadExecutor();
3341 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3342 +        CompletableFuture<Integer> g =
3343 +            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3344 +                                exec);
3345 +        f.complete(three);
3346 +        checkCompletedNormally(f, three);
3347 +        checkCompletedNormally(g, three);
3348 +        assertEquals(a.get(), 1);
3349 +    }
3350 +
3351 +    /**
3352 +     * whenCompleteAsync action executes on exceptional completion, propagating
3353 +     * source result.
3354 +     */
3355 +    public void testWhenCompleteAsync2e() {
3356 +        final AtomicInteger a = new AtomicInteger();
3357 +        ThreadExecutor exec = new ThreadExecutor();
3358 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3359 +        CompletableFuture<Integer> g =
3360 +            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3361 +                                exec);
3362 +        f.completeExceptionally(new CFException());
3363 +        checkCompletedWithWrappedCFException(f);
3364 +        checkCompletedWithWrappedCFException(g);
3365 +    }
3366 +
3367 +    /**
3368 +     * If a whenCompleteAsync action throws an exception when triggered
3369 +     * by a normal completion, it completes exceptionally
3370 +     */
3371 +    public void testWhenCompleteAsync3e() {
3372 +        ThreadExecutor exec = new ThreadExecutor();
3373 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3374 +        CompletableFuture<Integer> g =
3375 +            f.whenCompleteAsync((Integer x, Throwable t) ->
3376 +                                { throw new CFException(); },
3377 +                                exec);
3378 +        f.complete(three);
3379 +        checkCompletedNormally(f, three);
3380 +        checkCompletedWithWrappedCFException(g);
3381 +    }
3382 +
3383 +    /**
3384 +     * handleAsync action completes normally with function value on
3385 +     * either normal or exceptional completion of source
3386 +     */
3387 +    public void testHandleAsync() {
3388 +        CompletableFuture<Integer> f, g;
3389 +        IntegerHandler r;
3390 +
3391 +        f = new CompletableFuture<>();
3392 +        g = f.handleAsync(r = new IntegerHandler());
3393 +        assertFalse(r.ran);
3394 +        f.completeExceptionally(new CFException());
3395 +        checkCompletedWithWrappedCFException(f);
3396 +        checkCompletedNormally(g, three);
3397 +        assertTrue(r.ran);
3398 +
3399 +        f = new CompletableFuture<>();
3400 +        g = f.handleAsync(r = new IntegerHandler());
3401 +        assertFalse(r.ran);
3402 +        f.completeExceptionally(new CFException());
3403 +        checkCompletedWithWrappedCFException(f);
3404 +        checkCompletedNormally(g, three);
3405 +        assertTrue(r.ran);
3406 +
3407 +        f = new CompletableFuture<>();
3408 +        g = f.handleAsync(r = new IntegerHandler());
3409 +        assertFalse(r.ran);
3410 +        f.complete(one);
3411 +        checkCompletedNormally(f, one);
3412 +        checkCompletedNormally(g, two);
3413 +        assertTrue(r.ran);
3414 +
3415 +        f = new CompletableFuture<>();
3416 +        g = f.handleAsync(r = new IntegerHandler());
3417 +        assertFalse(r.ran);
3418 +        f.complete(one);
3419 +        checkCompletedNormally(f, one);
3420 +        checkCompletedNormally(g, two);
3421 +        assertTrue(r.ran);
3422 +    }
3423 +
3424 +    /**
3425 +     * handleAsync action with Executor completes normally with
3426 +     * function value on either normal or exceptional completion of
3427 +     * source
3428 +     */
3429 +    public void testHandleAsync2() {
3430 +        CompletableFuture<Integer> f, g;
3431 +        ThreadExecutor exec = new ThreadExecutor();
3432 +        IntegerHandler r;
3433 +
3434 +        f = new CompletableFuture<>();
3435 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3436 +        assertFalse(r.ran);
3437 +        f.completeExceptionally(new CFException());
3438 +        checkCompletedWithWrappedCFException(f);
3439 +        checkCompletedNormally(g, three);
3440 +        assertTrue(r.ran);
3441 +
3442 +        f = new CompletableFuture<>();
3443 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3444 +        assertFalse(r.ran);
3445 +        f.completeExceptionally(new CFException());
3446 +        checkCompletedWithWrappedCFException(f);
3447 +        checkCompletedNormally(g, three);
3448 +        assertTrue(r.ran);
3449 +
3450 +        f = new CompletableFuture<>();
3451 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3452 +        assertFalse(r.ran);
3453 +        f.complete(one);
3454 +        checkCompletedNormally(f, one);
3455 +        checkCompletedNormally(g, two);
3456 +        assertTrue(r.ran);
3457 +
3458 +        f = new CompletableFuture<>();
3459 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3460 +        assertFalse(r.ran);
3461 +        f.complete(one);
3462 +        checkCompletedNormally(f, one);
3463 +        checkCompletedNormally(g, two);
3464 +        assertTrue(r.ran);
3465 +    }
3466 +
3467   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines