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.14 by jsr166, Mon Apr 1 20:06:25 2013 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.join());
59 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
60 >        try {
61 >            assertEquals(value, f.getNow(null));
62 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
63 >        try {
64 >            assertEquals(value, f.get());
65 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
66 >        try {
67 >            assertEquals(value, f.get(0L, SECONDS));
68 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
69          assertTrue(f.isDone());
70          assertFalse(f.isCancelled());
71          assertTrue(f.toString().contains("[Completed normally]"));
72 +    }
73 +
74 +    void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
75          try {
76 <            assertSame(value, f.join());
76 >            f.join();
77 >            shouldThrow();
78 >        } catch (CompletionException success) {
79 >            assertTrue(success.getCause() instanceof CFException);
80 >        }
81 >        try {
82 >            f.getNow(null);
83 >            shouldThrow();
84 >        } catch (CompletionException success) {
85 >            assertTrue(success.getCause() instanceof CFException);
86 >        }
87 >        try {
88 >            f.get();
89 >            shouldThrow();
90 >        } catch (ExecutionException success) {
91 >            assertTrue(success.getCause() instanceof CFException);
92          } catch (Throwable fail) { threadUnexpectedException(fail); }
93          try {
94 <            assertSame(value, f.getNow(null));
94 >            f.get(0L, SECONDS);
95 >            shouldThrow();
96 >        } catch (ExecutionException success) {
97 >            assertTrue(success.getCause() instanceof CFException);
98          } catch (Throwable fail) { threadUnexpectedException(fail); }
99 +        assertTrue(f.isDone());
100 +        assertFalse(f.isCancelled());
101 +        assertTrue(f.toString().contains("[Completed exceptionally]"));
102 +    }
103 +
104 +    void checkCancelled(CompletableFuture<?> f) {
105 +        try {
106 +            f.join();
107 +            shouldThrow();
108 +        } catch (CancellationException success) {}
109 +        try {
110 +            f.getNow(null);
111 +            shouldThrow();
112 +        } catch (CancellationException success) {}
113          try {
114 <            assertSame(value, f.get());
114 >            f.get();
115 >            shouldThrow();
116 >        } catch (CancellationException success) {
117          } catch (Throwable fail) { threadUnexpectedException(fail); }
118          try {
119 <            assertSame(value, f.get(0L, SECONDS));
119 >            f.get(0L, SECONDS);
120 >            shouldThrow();
121 >        } catch (CancellationException success) {
122          } catch (Throwable fail) { threadUnexpectedException(fail); }
123 +        assertTrue(f.isDone());
124 +        assertTrue(f.isCancelled());
125 +        assertTrue(f.toString().contains("[Completed exceptionally]"));
126      }
127  
128 <    // XXXX Just a skeleton implementation for now.
129 <    public void testTODO() {
130 <        fail("Please add some real tests!");
128 >    void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
129 >        try {
130 >            f.join();
131 >            shouldThrow();
132 >        } catch (CompletionException success) {
133 >            assertTrue(success.getCause() instanceof CancellationException);
134 >        }
135 >        try {
136 >            f.getNow(null);
137 >            shouldThrow();
138 >        } catch (CompletionException success) {
139 >            assertTrue(success.getCause() instanceof CancellationException);
140 >        }
141 >        try {
142 >            f.get();
143 >            shouldThrow();
144 >        } catch (ExecutionException success) {
145 >            assertTrue(success.getCause() instanceof CancellationException);
146 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
147 >        try {
148 >            f.get(0L, SECONDS);
149 >            shouldThrow();
150 >        } catch (ExecutionException success) {
151 >            assertTrue(success.getCause() instanceof CancellationException);
152 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
153 >        assertTrue(f.isDone());
154 >        assertFalse(f.isCancelled());
155 >        assertTrue(f.toString().contains("[Completed exceptionally]"));
156 >    }
157 >
158 >    /**
159 >     * A newly constructed CompletableFuture is incomplete, as indicated
160 >     * by methods isDone, isCancelled, and getNow
161 >     */
162 >    public void testConstructor() {
163 >        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
164 >        checkIncomplete(f);
165      }
166  
167 +    /**
168 +     * complete completes normally, as indicated by methods isDone,
169 +     * isCancelled, join, get, and getNow
170 +     */
171 +    public void testComplete() {
172 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
173 +        checkIncomplete(f);
174 +        f.complete(one);
175 +        checkCompletedNormally(f, one);
176 +    }
177 +
178 +    /**
179 +     * completeExceptionally completes exceptionally, as indicated by
180 +     * methods isDone, isCancelled, join, get, and getNow
181 +     */
182 +    public void testCompleteExceptionally() {
183 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
184 +        checkIncomplete(f);
185 +        f.completeExceptionally(new CFException());
186 +        checkCompletedWithWrappedCFException(f);
187 +    }
188 +
189 +    /**
190 +     * cancel completes exceptionally and reports cancelled, as indicated by
191 +     * methods isDone, isCancelled, join, get, and getNow
192 +     */
193 +    public void testCancel() {
194 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
195 +        checkIncomplete(f);
196 +        assertTrue(f.cancel(true));
197 +        checkCancelled(f);
198 +    }
199 +
200 +    /**
201 +     * obtrudeValue forces completion with given value
202 +     */
203 +    public void testObtrudeValue() {
204 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
205 +        checkIncomplete(f);
206 +        f.complete(one);
207 +        checkCompletedNormally(f, one);
208 +        f.obtrudeValue(three);
209 +        checkCompletedNormally(f, three);
210 +        f.obtrudeValue(two);
211 +        checkCompletedNormally(f, two);
212 +        f = new CompletableFuture<Integer>();
213 +        f.obtrudeValue(three);
214 +        checkCompletedNormally(f, three);
215 +        f = new CompletableFuture<Integer>();
216 +        f.completeExceptionally(new CFException());
217 +        f.obtrudeValue(four);
218 +        checkCompletedNormally(f, four);
219 +    }
220 +
221 +    /**
222 +     * obtrudeException forces completion with given exception
223 +     */
224 +    public void testObtrudeException() {
225 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
226 +        checkIncomplete(f);
227 +        f.complete(one);
228 +        checkCompletedNormally(f, one);
229 +        f.obtrudeException(new CFException());
230 +        checkCompletedWithWrappedCFException(f);
231 +        f = new CompletableFuture<Integer>();
232 +        f.obtrudeException(new CFException());
233 +        checkCompletedWithWrappedCFException(f);
234 +        f = new CompletableFuture<Integer>();
235 +        f.completeExceptionally(new CFException());
236 +        f.obtrudeValue(four);
237 +        checkCompletedNormally(f, four);
238 +        f.obtrudeException(new CFException());
239 +        checkCompletedWithWrappedCFException(f);
240 +    }
241 +
242 +    /**
243 +     * getNumberOfDependents returns number of dependent tasks
244 +     */
245 +    public void testGetNumberOfDependents() {
246 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
247 +        assertEquals(f.getNumberOfDependents(), 0);
248 +        CompletableFuture g = f.thenRun(new Noop());
249 +        assertEquals(f.getNumberOfDependents(), 1);
250 +        assertEquals(g.getNumberOfDependents(), 0);
251 +        CompletableFuture h = f.thenRun(new Noop());
252 +        assertEquals(f.getNumberOfDependents(), 2);
253 +        f.complete(1);
254 +        checkCompletedNormally(g, null);
255 +        assertEquals(f.getNumberOfDependents(), 0);
256 +        assertEquals(g.getNumberOfDependents(), 0);
257 +    }
258 +
259 +
260 +    /**
261 +     * toString indicates current completion state
262 +     */
263      public void testToString() {
264          CompletableFuture<String> f;
265  
# Line 80 | Line 275 | public class CompletableFutureTest exten
275      }
276  
277      /**
278 +     * completedFuture returns a completed CompletableFuture with given value
279 +     */
280 +    public void testCompletedFuture() {
281 +        CompletableFuture<String> f = CompletableFuture.completedFuture("test");
282 +        checkCompletedNormally(f, "test");
283 +    }
284 +
285 +    static final Supplier<Integer> supplyOne =
286 +        () -> Integer.valueOf(1);
287 +    static final Function<Integer, Integer> inc =
288 +        (Integer x) -> Integer.valueOf(x.intValue() + 1);
289 +    static final BiFunction<Integer, Integer, Integer> add =
290 +        (Integer x, Integer y) -> Integer.valueOf(x.intValue() + y.intValue());
291 +    static final class IncAction implements Consumer<Integer> {
292 +        int value;
293 +        public void accept(Integer x) { value = x.intValue() + 1; }
294 +    }
295 +    static final class AddAction implements BiConsumer<Integer, Integer> {
296 +        int value;
297 +        public void accept(Integer x, Integer y) {
298 +            value = x.intValue() + y.intValue();
299 +        }
300 +    }
301 +    static final class Noop implements Runnable {
302 +        boolean ran;
303 +        public void run() { ran = true; }
304 +    }
305 +
306 +    static final class FailingSupplier implements Supplier<Integer> {
307 +        boolean ran;
308 +        public Integer get() { ran = true; throw new CFException(); }
309 +    }
310 +    static final class FailingConsumer implements Consumer<Integer> {
311 +        boolean ran;
312 +        public void accept(Integer x) { ran = true; throw new CFException(); }
313 +    }
314 +    static final class FailingBiConsumer implements BiConsumer<Integer, Integer> {
315 +        boolean ran;
316 +        public void accept(Integer x, Integer y) { ran = true; throw new CFException(); }
317 +    }
318 +    static final class FailingFunction implements Function<Integer, Integer> {
319 +        boolean ran;
320 +        public Integer apply(Integer x) { ran = true; throw new CFException(); }
321 +    }
322 +    static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> {
323 +        boolean ran;
324 +        public Integer apply(Integer x, Integer y) { ran = true; throw new CFException(); }
325 +    }
326 +    static final class FailingNoop implements Runnable {
327 +        boolean ran;
328 +        public void run() { ran = true; throw new CFException(); }
329 +    }
330 +
331 +    static final class CompletableFutureInc implements Function<Integer, CompletableFuture<Integer>> {
332 +        public CompletableFuture<Integer> apply(Integer x) {
333 +            CompletableFuture<Integer> f = new CompletableFuture<Integer>();
334 +            f.complete(Integer.valueOf(x.intValue() + 1));
335 +            return f;
336 +        }
337 +    }
338 +
339 +    static final class FailingCompletableFutureFunction implements Function<Integer, CompletableFuture<Integer>> {
340 +        boolean ran;
341 +        public CompletableFuture<Integer> apply(Integer x) {
342 +            ran = true; throw new CFException();
343 +        }
344 +    }
345 +
346 +    // Used for explicit executor tests
347 +    static final class ThreadExecutor implements Executor {
348 +        public void execute(Runnable r) {
349 +            new Thread(r).start();
350 +        }
351 +    }
352 +
353 +    static final class ExceptionToInteger implements Function<Throwable, Integer> {
354 +        public Integer apply(Throwable x) { return Integer.valueOf(3); }
355 +    }
356 +
357 +    static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> {
358 +        public Integer apply(Integer x, Throwable t) {
359 +            return (t == null) ? two : three;
360 +        }
361 +    }
362 +
363 +
364 +    /**
365 +     * exceptionally action completes with function value on source
366 +     * exception;  otherwise with source value
367 +     */
368 +    public void testExceptionally() {
369 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
370 +        ExceptionToInteger r = new ExceptionToInteger();
371 +        CompletableFuture<Integer> g = f.exceptionally(r);
372 +        f.completeExceptionally(new CFException());
373 +        checkCompletedNormally(g, three);
374 +
375 +        f = new CompletableFuture<Integer>();
376 +        r = new ExceptionToInteger();
377 +        g = f.exceptionally(r);
378 +        f.complete(one);
379 +        checkCompletedNormally(g, one);
380 +    }
381 +
382 +    /**
383 +     * handle action completes normally with function value on either
384 +     * normal or exceptional completion of source
385 +     */
386 +    public void testHandle() {
387 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
388 +        IntegerHandler r = new IntegerHandler();
389 +        CompletableFuture<Integer> g = f.handle(r);
390 +        f.completeExceptionally(new CFException());
391 +        checkCompletedNormally(g, three);
392 +
393 +        f = new CompletableFuture<Integer>();
394 +        r = new IntegerHandler();
395 +        g = f.handle(r);
396 +        f.complete(one);
397 +        checkCompletedNormally(g, two);
398 +    }
399 +
400 +    /**
401 +     * runAsync completes after running Runnable
402 +     */
403 +    public void testRunAsync() {
404 +        Noop r = new Noop();
405 +        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
406 +        assertNull(f.join());
407 +        assertTrue(r.ran);
408 +        checkCompletedNormally(f, null);
409 +    }
410 +
411 +    /**
412 +     * runAsync with executor completes after running Runnable
413 +     */
414 +    public void testRunAsync2() {
415 +        Noop r = new Noop();
416 +        CompletableFuture<Void> f = CompletableFuture.runAsync(r, new ThreadExecutor());
417 +        assertNull(f.join());
418 +        assertTrue(r.ran);
419 +        checkCompletedNormally(f, null);
420 +    }
421 +
422 +    /**
423 +     * failing runAsync completes exceptionally after running Runnable
424 +     */
425 +    public void testRunAsync3() {
426 +        FailingNoop r = new FailingNoop();
427 +        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
428 +        checkCompletedWithWrappedCFException(f);
429 +        assertTrue(r.ran);
430 +    }
431 +
432 +    /**
433 +     * supplyAsync completes with result of supplier
434 +     */
435 +    public void testSupplyAsync() {
436 +        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(supplyOne);
437 +        assertEquals(f.join(), one);
438 +    }
439 +
440 +    /**
441 +     * supplyAsync with executor completes with result of supplier
442 +     */
443 +    public void testSupplyAsync2() {
444 +        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor());
445 +        assertEquals(f.join(), one);
446 +    }
447 +
448 +    /**
449 +     * Failing supplyAsync completes exceptionally
450 +     */
451 +    public void testSupplyAsync3() {
452 +        FailingSupplier r = new FailingSupplier();
453 +        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r);
454 +        checkCompletedWithWrappedCFException(f);
455 +        assertTrue(r.ran);
456 +    }
457 +
458 +    // seq completion methods
459 +
460 +    /**
461 +     * thenRun result completes normally after normal completion of source
462 +     */
463 +    public void testThenRun() {
464 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
465 +        Noop r = new Noop();
466 +        CompletableFuture<Void> g = f.thenRun(r);
467 +        f.complete(null);
468 +        checkCompletedNormally(g, null);
469 +        // reordered version
470 +        f = new CompletableFuture<Integer>();
471 +        f.complete(null);
472 +        r = new Noop();
473 +        g = f.thenRun(r);
474 +        checkCompletedNormally(g, null);
475 +    }
476 +
477 +    /**
478 +     * thenRun result completes exceptionally after exceptional
479 +     * completion of source
480 +     */
481 +    public void testThenRun2() {
482 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
483 +        Noop r = new Noop();
484 +        CompletableFuture<Void> g = f.thenRun(r);
485 +        f.completeExceptionally(new CFException());
486 +        checkCompletedWithWrappedCFException(g);
487 +    }
488 +
489 +    /**
490 +     * thenRun result completes exceptionally if action does
491 +     */
492 +    public void testThenRun3() {
493 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
494 +        FailingNoop r = new FailingNoop();
495 +        CompletableFuture<Void> g = f.thenRun(r);
496 +        f.complete(null);
497 +        checkCompletedWithWrappedCFException(g);
498 +    }
499 +
500 +    /**
501 +     * thenRun result completes exceptionally if source cancelled
502 +     */
503 +    public void testThenRun4() {
504 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
505 +        Noop r = new Noop();
506 +        CompletableFuture<Void> g = f.thenRun(r);
507 +        assertTrue(f.cancel(true));
508 +        checkCompletedWithWrappedCancellationException(g);
509 +    }
510 +
511 +    /**
512 +     * thenApply result completes normally after normal completion of source
513 +     */
514 +    public void testThenApply() {
515 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
516 +        CompletableFuture<Integer> g = f.thenApply(inc);
517 +        f.complete(one);
518 +        checkCompletedNormally(g, two);
519 +    }
520 +
521 +    /**
522 +     * thenApply result completes exceptionally after exceptional
523 +     * completion of source
524 +     */
525 +    public void testThenApply2() {
526 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
527 +        CompletableFuture<Integer> g = f.thenApply(inc);
528 +        f.completeExceptionally(new CFException());
529 +        checkCompletedWithWrappedCFException(g);
530 +    }
531 +
532 +    /**
533 +     * thenApply result completes exceptionally if action does
534 +     */
535 +    public void testThenApply3() {
536 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
537 +        CompletableFuture<Integer> g = f.thenApply(new FailingFunction());
538 +        f.complete(one);
539 +        checkCompletedWithWrappedCFException(g);
540 +    }
541 +
542 +    /**
543 +     * thenApply result completes exceptionally if source cancelled
544 +     */
545 +    public void testThenApply4() {
546 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
547 +        CompletableFuture<Integer> g = f.thenApply(inc);
548 +        assertTrue(f.cancel(true));
549 +        checkCompletedWithWrappedCancellationException(g);
550 +    }
551 +
552 +    /**
553 +     * thenAccept result completes normally after normal completion of source
554 +     */
555 +    public void testThenAccept() {
556 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
557 +        IncAction r = new IncAction();
558 +        CompletableFuture<Void> g = f.thenAccept(r);
559 +        f.complete(one);
560 +        checkCompletedNormally(g, null);
561 +        assertEquals(r.value, 2);
562 +    }
563 +
564 +    /**
565 +     * thenAccept result completes exceptionally after exceptional
566 +     * completion of source
567 +     */
568 +    public void testThenAccept2() {
569 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
570 +        IncAction r = new IncAction();
571 +        CompletableFuture<Void> g = f.thenAccept(r);
572 +        f.completeExceptionally(new CFException());
573 +        checkCompletedWithWrappedCFException(g);
574 +    }
575 +
576 +    /**
577 +     * thenAccept result completes exceptionally if action does
578 +     */
579 +    public void testThenAccept3() {
580 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
581 +        FailingConsumer r = new FailingConsumer();
582 +        CompletableFuture<Void> g = f.thenAccept(r);
583 +        f.complete(one);
584 +        checkCompletedWithWrappedCFException(g);
585 +        assertTrue(r.ran);
586 +    }
587 +
588 +    /**
589 +     * thenAccept result completes exceptionally if source cancelled
590 +     */
591 +    public void testThenAccept4() {
592 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
593 +        IncAction r = new IncAction();
594 +        CompletableFuture<Void> g = f.thenAccept(r);
595 +        assertTrue(f.cancel(true));
596 +        checkCompletedWithWrappedCancellationException(g);
597 +    }
598 +
599 +
600 +    /**
601 +     * thenCombine result completes normally after normal completion of sources
602 +     */
603 +    public void testThenCombine() {
604 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
605 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
606 +        CompletableFuture<Integer> g = f.thenCombine(f2, add);
607 +        f.complete(one);
608 +        checkIncomplete(g);
609 +        f2.complete(two);
610 +        checkCompletedNormally(g, three);
611 +
612 +        f = new CompletableFuture<Integer>();
613 +        f.complete(one);
614 +        f2 = new CompletableFuture<Integer>();
615 +        g = f.thenCombine(f2, add);
616 +        checkIncomplete(g);
617 +        f2.complete(two);
618 +        checkCompletedNormally(g, three);
619 +    }
620 +
621 +    /**
622 +     * thenCombine result completes exceptionally after exceptional
623 +     * completion of either source
624 +     */
625 +    public void testThenCombine2() {
626 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
627 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
628 +        CompletableFuture<Integer> g = f.thenCombine(f2, add);
629 +        f.completeExceptionally(new CFException());
630 +        f2.complete(two);
631 +        checkCompletedWithWrappedCFException(g);
632 +
633 +        f = new CompletableFuture<Integer>();
634 +        f.complete(one);
635 +        f2 = new CompletableFuture<Integer>();
636 +        g = f.thenCombine(f2, add);
637 +        f2.completeExceptionally(new CFException());
638 +        checkCompletedWithWrappedCFException(g);
639 +    }
640 +
641 +    /**
642 +     * thenCombine result completes exceptionally if action does
643 +     */
644 +    public void testThenCombine3() {
645 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
646 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
647 +        FailingBiFunction r = new FailingBiFunction();
648 +        CompletableFuture<Integer> g = f.thenCombine(f2, r);
649 +        f.complete(one);
650 +        checkIncomplete(g);
651 +        f2.complete(two);
652 +        checkCompletedWithWrappedCFException(g);
653 +    }
654 +
655 +    /**
656 +     * thenCombine result completes exceptionally if either source cancelled
657 +     */
658 +    public void testThenCombine4() {
659 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
660 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
661 +        CompletableFuture<Integer> g = f.thenCombine(f2, add);
662 +        assertTrue(f.cancel(true));
663 +        f2.complete(two);
664 +        checkCompletedWithWrappedCancellationException(g);
665 +        f = new CompletableFuture<Integer>();
666 +        f2 = new CompletableFuture<Integer>();
667 +        g = f.thenCombine(f2, add);
668 +        f.complete(one);
669 +        assertTrue(f2.cancel(true));
670 +        checkCompletedWithWrappedCancellationException(g);
671 +    }
672 +
673 +    /**
674 +     * thenAcceptBoth result completes normally after normal
675 +     * completion of sources
676 +     */
677 +    public void testThenAcceptBoth() {
678 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
679 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
680 +        AddAction r = new AddAction();
681 +        CompletableFuture<Void> g = f.thenAcceptBoth(f2, r);
682 +        f.complete(one);
683 +        checkIncomplete(g);
684 +        f2.complete(two);
685 +        checkCompletedNormally(g, null);
686 +        assertEquals(r.value, 3);
687 +
688 +        r = new AddAction();
689 +        f = new CompletableFuture<Integer>();
690 +        f.complete(one);
691 +        f2 = new CompletableFuture<Integer>();
692 +        g = f.thenAcceptBoth(f2, r);
693 +        checkIncomplete(g);
694 +        f2.complete(two);
695 +        checkCompletedNormally(g, null);
696 +        assertEquals(r.value, 3);
697 +    }
698 +
699 +    /**
700 +     * thenAcceptBoth result completes exceptionally after exceptional
701 +     * completion of either source
702 +     */
703 +    public void testThenAcceptBoth2() {
704 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
705 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
706 +        AddAction r = new AddAction();
707 +        CompletableFuture<Void> g = f.thenAcceptBoth(f2, r);
708 +        f.completeExceptionally(new CFException());
709 +        f2.complete(two);
710 +        checkCompletedWithWrappedCFException(g);
711 +
712 +        r = new AddAction();
713 +        f = new CompletableFuture<Integer>();
714 +        f.complete(one);
715 +        f2 = new CompletableFuture<Integer>();
716 +        g = f.thenAcceptBoth(f2, r);
717 +        f2.completeExceptionally(new CFException());
718 +        checkCompletedWithWrappedCFException(g);
719 +    }
720 +
721 +    /**
722 +     * thenAcceptBoth result completes exceptionally if action does
723 +     */
724 +    public void testThenAcceptBoth3() {
725 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
726 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
727 +        FailingBiConsumer r = new FailingBiConsumer();
728 +        CompletableFuture<Void> g = f.thenAcceptBoth(f2, r);
729 +        f.complete(one);
730 +        checkIncomplete(g);
731 +        f2.complete(two);
732 +        checkCompletedWithWrappedCFException(g);
733 +    }
734 +
735 +    /**
736 +     * thenAcceptBoth result completes exceptionally if either source cancelled
737 +     */
738 +    public void testThenAcceptBoth4() {
739 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
740 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
741 +        AddAction r = new AddAction();
742 +        CompletableFuture<Void> g = f.thenAcceptBoth(f2, r);
743 +        assertTrue(f.cancel(true));
744 +        f2.complete(two);
745 +        checkCompletedWithWrappedCancellationException(g);
746 +        f = new CompletableFuture<Integer>();
747 +        f2 = new CompletableFuture<Integer>();
748 +        r = new AddAction();
749 +        g = f.thenAcceptBoth(f2, r);
750 +        f.complete(one);
751 +        assertTrue(f2.cancel(true));
752 +        checkCompletedWithWrappedCancellationException(g);
753 +    }
754 +
755 +    /**
756 +     * runAfterBoth result completes normally after normal
757 +     * completion of sources
758 +     */
759 +    public void testRunAfterBoth() {
760 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
761 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
762 +        Noop r = new Noop();
763 +        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
764 +        f.complete(one);
765 +        checkIncomplete(g);
766 +        f2.complete(two);
767 +        checkCompletedNormally(g, null);
768 +        assertTrue(r.ran);
769 +
770 +        r = new Noop();
771 +        f = new CompletableFuture<Integer>();
772 +        f.complete(one);
773 +        f2 = new CompletableFuture<Integer>();
774 +        g = f.runAfterBoth(f2, r);
775 +        checkIncomplete(g);
776 +        f2.complete(two);
777 +        checkCompletedNormally(g, null);
778 +        assertTrue(r.ran);
779 +    }
780 +
781 +    /**
782 +     * runAfterBoth result completes exceptionally after exceptional
783 +     * completion of either source
784 +     */
785 +    public void testRunAfterBoth2() {
786 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
787 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
788 +        Noop r = new Noop();
789 +        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
790 +        f.completeExceptionally(new CFException());
791 +        f2.complete(two);
792 +        checkCompletedWithWrappedCFException(g);
793 +
794 +        r = new Noop();
795 +        f = new CompletableFuture<Integer>();
796 +        f.complete(one);
797 +        f2 = new CompletableFuture<Integer>();
798 +        g = f.runAfterBoth(f2, r);
799 +        f2.completeExceptionally(new CFException());
800 +        checkCompletedWithWrappedCFException(g);
801 +    }
802 +
803 +    /**
804 +     * runAfterBoth result completes exceptionally if action does
805 +     */
806 +    public void testRunAfterBoth3() {
807 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
808 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
809 +        FailingNoop r = new FailingNoop();
810 +        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
811 +        f.complete(one);
812 +        checkIncomplete(g);
813 +        f2.complete(two);
814 +        checkCompletedWithWrappedCFException(g);
815 +    }
816 +
817 +    /**
818 +     * runAfterBoth result completes exceptionally if either source cancelled
819 +     */
820 +    public void testRunAfterBoth4() {
821 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
822 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
823 +        Noop r = new Noop();
824 +        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
825 +        assertTrue(f.cancel(true));
826 +        f2.complete(two);
827 +        checkCompletedWithWrappedCancellationException(g);
828 +        f = new CompletableFuture<Integer>();
829 +        f2 = new CompletableFuture<Integer>();
830 +        r = new Noop();
831 +        g = f.runAfterBoth(f2, r);
832 +        f.complete(one);
833 +        assertTrue(f2.cancel(true));
834 +        checkCompletedWithWrappedCancellationException(g);
835 +    }
836 +
837 +    /**
838 +     * applyToEither result completes normally after normal completion
839 +     * of either source
840 +     */
841 +    public void testApplyToEither() {
842 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
843 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
844 +        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
845 +        f.complete(one);
846 +        checkCompletedNormally(g, two);
847 +        f2.complete(one);
848 +        checkCompletedNormally(g, two);
849 +
850 +        f = new CompletableFuture<Integer>();
851 +        f.complete(one);
852 +        f2 = new CompletableFuture<Integer>();
853 +        g = f.applyToEither(f2, inc);
854 +        checkCompletedNormally(g, two);
855 +    }
856 +
857 +    /**
858 +     * applyToEither result completes exceptionally after exceptional
859 +     * completion of either source
860 +     */
861 +    public void testApplyToEither2() {
862 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
863 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
864 +        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
865 +        f.completeExceptionally(new CFException());
866 +        f2.complete(one);
867 +        checkCompletedWithWrappedCFException(g);
868 +
869 +        f = new CompletableFuture<Integer>();
870 +        f2 = new CompletableFuture<Integer>();
871 +        f2.completeExceptionally(new CFException());
872 +        g = f.applyToEither(f2, inc);
873 +        checkCompletedWithWrappedCFException(g);
874 +    }
875 +
876 +    /**
877 +     * applyToEither result completes exceptionally if action does
878 +     */
879 +    public void testApplyToEither3() {
880 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
881 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
882 +        FailingFunction r = new FailingFunction();
883 +        CompletableFuture<Integer> g = f.applyToEither(f2, r);
884 +        f2.complete(two);
885 +        checkCompletedWithWrappedCFException(g);
886 +    }
887 +
888 +    /**
889 +     * applyToEither result completes exceptionally if either source cancelled
890 +     */
891 +    public void testApplyToEither4() {
892 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
893 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
894 +        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
895 +        assertTrue(f.cancel(true));
896 +        checkCompletedWithWrappedCancellationException(g);
897 +        f = new CompletableFuture<Integer>();
898 +        f2 = new CompletableFuture<Integer>();
899 +        assertTrue(f2.cancel(true));
900 +        checkCompletedWithWrappedCancellationException(g);
901 +    }
902 +
903 +    /**
904 +     * acceptEither result completes normally after normal completion
905 +     * of either source
906 +     */
907 +    public void testAcceptEither() {
908 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
909 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
910 +        IncAction r = new IncAction();
911 +        CompletableFuture<Void> g = f.acceptEither(f2, r);
912 +        f.complete(one);
913 +        checkCompletedNormally(g, null);
914 +        f2.complete(one);
915 +        checkCompletedNormally(g, null);
916 +        assertEquals(r.value, 2);
917 +
918 +        r = new IncAction();
919 +        f = new CompletableFuture<Integer>();
920 +        f.complete(one);
921 +        f2 = new CompletableFuture<Integer>();
922 +        g = f.acceptEither(f2, r);
923 +        checkCompletedNormally(g, null);
924 +        assertEquals(r.value, 2);
925 +    }
926 +
927 +    /**
928 +     * acceptEither result completes exceptionally after exceptional
929 +     * completion of either source
930 +     */
931 +    public void testAcceptEither2() {
932 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
933 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
934 +        IncAction r = new IncAction();
935 +        CompletableFuture<Void> g = f.acceptEither(f2, r);
936 +        f.completeExceptionally(new CFException());
937 +        f2.complete(one);
938 +        checkCompletedWithWrappedCFException(g);
939 +
940 +        r = new IncAction();
941 +        f = new CompletableFuture<Integer>();
942 +        f2 = new CompletableFuture<Integer>();
943 +        f2.completeExceptionally(new CFException());
944 +        g = f.acceptEither(f2, r);
945 +        checkCompletedWithWrappedCFException(g);
946 +    }
947 +
948 +    /**
949 +     * acceptEither result completes exceptionally if action does
950 +     */
951 +    public void testAcceptEither3() {
952 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
953 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
954 +        FailingConsumer r = new FailingConsumer();
955 +        CompletableFuture<Void> g = f.acceptEither(f2, r);
956 +        f2.complete(two);
957 +        checkCompletedWithWrappedCFException(g);
958 +    }
959 +
960 +    /**
961 +     * acceptEither result completes exceptionally if either source cancelled
962 +     */
963 +    public void testAcceptEither4() {
964 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
965 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
966 +        IncAction r = new IncAction();
967 +        CompletableFuture<Void> g = f.acceptEither(f2, r);
968 +        assertTrue(f.cancel(true));
969 +        checkCompletedWithWrappedCancellationException(g);
970 +        f = new CompletableFuture<Integer>();
971 +        f2 = new CompletableFuture<Integer>();
972 +        assertTrue(f2.cancel(true));
973 +        checkCompletedWithWrappedCancellationException(g);
974 +    }
975 +
976 +
977 +    /**
978 +     * runAfterEither result completes normally after normal completion
979 +     * of either source
980 +     */
981 +    public void testRunAfterEither() {
982 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
983 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
984 +        Noop r = new Noop();
985 +        CompletableFuture<Void> g = f.runAfterEither(f2, r);
986 +        f.complete(one);
987 +        checkCompletedNormally(g, null);
988 +        f2.complete(one);
989 +        checkCompletedNormally(g, null);
990 +        assertTrue(r.ran);
991 +
992 +        r = new Noop();
993 +        f = new CompletableFuture<Integer>();
994 +        f.complete(one);
995 +        f2 = new CompletableFuture<Integer>();
996 +        g = f.runAfterEither(f2, r);
997 +        checkCompletedNormally(g, null);
998 +        assertTrue(r.ran);
999 +    }
1000 +
1001 +    /**
1002 +     * runAfterEither result completes exceptionally after exceptional
1003 +     * completion of either source
1004 +     */
1005 +    public void testRunAfterEither2() {
1006 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1007 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1008 +        Noop r = new Noop();
1009 +        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1010 +        f.completeExceptionally(new CFException());
1011 +        f2.complete(one);
1012 +        checkCompletedWithWrappedCFException(g);
1013 +
1014 +        r = new Noop();
1015 +        f = new CompletableFuture<Integer>();
1016 +        f2 = new CompletableFuture<Integer>();
1017 +        f2.completeExceptionally(new CFException());
1018 +        g = f.runAfterEither(f2, r);
1019 +        checkCompletedWithWrappedCFException(g);
1020 +    }
1021 +
1022 +    /**
1023 +     * runAfterEither result completes exceptionally if action does
1024 +     */
1025 +    public void testRunAfterEither3() {
1026 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1027 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1028 +        FailingNoop r = new FailingNoop();
1029 +        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1030 +        f2.complete(two);
1031 +        checkCompletedWithWrappedCFException(g);
1032 +    }
1033 +
1034 +    /**
1035 +     * runAfterEither result completes exceptionally if either source cancelled
1036 +     */
1037 +    public void testRunAfterEither4() {
1038 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1039 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1040 +        Noop r = new Noop();
1041 +        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1042 +        assertTrue(f.cancel(true));
1043 +        checkCompletedWithWrappedCancellationException(g);
1044 +        f = new CompletableFuture<Integer>();
1045 +        f2 = new CompletableFuture<Integer>();
1046 +        assertTrue(f2.cancel(true));
1047 +        checkCompletedWithWrappedCancellationException(g);
1048 +    }
1049 +
1050 +    /**
1051 +     * thenCompose result completes normally after normal completion of source
1052 +     */
1053 +    public void testThenCompose() {
1054 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1055 +        CompletableFutureInc r = new CompletableFutureInc();
1056 +        CompletableFuture<Integer> g = f.thenCompose(r);
1057 +        f.complete(one);
1058 +        checkCompletedNormally(g, two);
1059 +    }
1060 +
1061 +    /**
1062 +     * thenCompose result completes exceptionally after exceptional
1063 +     * completion of source
1064 +     */
1065 +    public void testThenCompose2() {
1066 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1067 +        CompletableFutureInc r = new CompletableFutureInc();
1068 +        CompletableFuture<Integer> g = f.thenCompose(r);
1069 +        f.completeExceptionally(new CFException());
1070 +        checkCompletedWithWrappedCFException(g);
1071 +    }
1072 +
1073 +    /**
1074 +     * thenCompose result completes exceptionally if action does
1075 +     */
1076 +    public void testThenCompose3() {
1077 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1078 +        FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
1079 +        CompletableFuture<Integer> g = f.thenCompose(r);
1080 +        f.complete(one);
1081 +        checkCompletedWithWrappedCFException(g);
1082 +    }
1083 +
1084 +    /**
1085 +     * thenCompose result completes exceptionally if source cancelled
1086 +     */
1087 +    public void testThenCompose4() {
1088 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1089 +        CompletableFutureInc r = new CompletableFutureInc();
1090 +        CompletableFuture<Integer> g = f.thenCompose(r);
1091 +        assertTrue(f.cancel(true));
1092 +        checkCompletedWithWrappedCancellationException(g);
1093 +    }
1094 +
1095 +
1096 +    // asyncs
1097 +
1098 +    /**
1099 +     * thenRunAsync result completes normally after normal completion of source
1100 +     */
1101 +    public void testThenRunAsync() {
1102 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1103 +        Noop r = new Noop();
1104 +        CompletableFuture<Void> g = f.thenRunAsync(r);
1105 +        f.complete(null);
1106 +        checkCompletedNormally(g, null);
1107 +
1108 +        // reordered version
1109 +        f = new CompletableFuture<Integer>();
1110 +        f.complete(null);
1111 +        r = new Noop();
1112 +        g = f.thenRunAsync(r);
1113 +        checkCompletedNormally(g, null);
1114 +    }
1115 +
1116 +    /**
1117 +     * thenRunAsync result completes exceptionally after exceptional
1118 +     * completion of source
1119 +     */
1120 +    public void testThenRunAsync2() {
1121 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1122 +        Noop r = new Noop();
1123 +        CompletableFuture<Void> g = f.thenRunAsync(r);
1124 +        f.completeExceptionally(new CFException());
1125 +        try {
1126 +            g.join();
1127 +            shouldThrow();
1128 +        } catch (Exception ok) {
1129 +        }
1130 +        checkCompletedWithWrappedCFException(g);
1131 +    }
1132 +
1133 +    /**
1134 +     * thenRunAsync result completes exceptionally if action does
1135 +     */
1136 +    public void testThenRunAsync3() {
1137 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1138 +        FailingNoop r = new FailingNoop();
1139 +        CompletableFuture<Void> g = f.thenRunAsync(r);
1140 +        f.complete(null);
1141 +        checkCompletedWithWrappedCFException(g);
1142 +    }
1143 +
1144 +    /**
1145 +     * thenRunAsync result completes exceptionally if source cancelled
1146 +     */
1147 +    public void testThenRunAsync4() {
1148 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1149 +        Noop r = new Noop();
1150 +        CompletableFuture<Void> g = f.thenRunAsync(r);
1151 +        assertTrue(f.cancel(true));
1152 +        checkCompletedWithWrappedCancellationException(g);
1153 +    }
1154 +
1155 +    /**
1156 +     * thenApplyAsync result completes normally after normal completion of source
1157 +     */
1158 +    public void testThenApplyAsync() {
1159 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1160 +        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1161 +        f.complete(one);
1162 +        checkCompletedNormally(g, two);
1163 +    }
1164 +
1165 +    /**
1166 +     * thenApplyAsync result completes exceptionally after exceptional
1167 +     * completion of source
1168 +     */
1169 +    public void testThenApplyAsync2() {
1170 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1171 +        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1172 +        f.completeExceptionally(new CFException());
1173 +        checkCompletedWithWrappedCFException(g);
1174 +    }
1175 +
1176 +    /**
1177 +     * thenApplyAsync result completes exceptionally if action does
1178 +     */
1179 +    public void testThenApplyAsync3() {
1180 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1181 +        FailingFunction r = new FailingFunction();
1182 +        CompletableFuture<Integer> g = f.thenApplyAsync(r);
1183 +        f.complete(null);
1184 +        checkCompletedWithWrappedCFException(g);
1185 +    }
1186 +
1187 +    /**
1188 +     * thenApplyAsync result completes exceptionally if source cancelled
1189 +     */
1190 +    public void testThenApplyAsync4() {
1191 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1192 +        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1193 +        assertTrue(f.cancel(true));
1194 +        checkCompletedWithWrappedCancellationException(g);
1195 +    }
1196 +
1197 +    /**
1198 +     * thenAcceptAsync result completes normally after normal
1199 +     * completion of source
1200 +     */
1201 +    public void testThenAcceptAsync() {
1202 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1203 +        IncAction r = new IncAction();
1204 +        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1205 +        f.complete(one);
1206 +        checkCompletedNormally(g, null);
1207 +        assertEquals(r.value, 2);
1208 +    }
1209 +
1210 +    /**
1211 +     * thenAcceptAsync result completes exceptionally after exceptional
1212 +     * completion of source
1213 +     */
1214 +    public void testThenAcceptAsync2() {
1215 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1216 +        IncAction r = new IncAction();
1217 +        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1218 +        f.completeExceptionally(new CFException());
1219 +        checkCompletedWithWrappedCFException(g);
1220 +    }
1221 +
1222 +    /**
1223 +     * thenAcceptAsync result completes exceptionally if action does
1224 +     */
1225 +    public void testThenAcceptAsync3() {
1226 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1227 +        FailingConsumer r = new FailingConsumer();
1228 +        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1229 +        f.complete(null);
1230 +        checkCompletedWithWrappedCFException(g);
1231 +    }
1232 +
1233 +    /**
1234 +     * thenAcceptAsync result completes exceptionally if source cancelled
1235 +     */
1236 +    public void testThenAcceptAsync4() {
1237 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1238 +        IncAction r = new IncAction();
1239 +        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1240 +        assertTrue(f.cancel(true));
1241 +        checkCompletedWithWrappedCancellationException(g);
1242 +    }
1243 +    /**
1244 +     * thenCombineAsync result completes normally after normal
1245 +     * completion of sources
1246 +     */
1247 +    public void testThenCombineAsync() {
1248 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1249 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1250 +        CompletableFuture<Integer> g = f.thenCombineAsync(f2, add);
1251 +        f.complete(one);
1252 +        checkIncomplete(g);
1253 +        f2.complete(two);
1254 +        checkCompletedNormally(g, three);
1255 +    }
1256 +
1257 +    /**
1258 +     * thenCombineAsync result completes exceptionally after exceptional
1259 +     * completion of source
1260 +     */
1261 +    public void testThenCombineAsync2() {
1262 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1263 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1264 +        CompletableFuture<Integer> g = f.thenCombineAsync(f2, add);
1265 +        f.completeExceptionally(new CFException());
1266 +        f2.complete(two);
1267 +        checkCompletedWithWrappedCFException(g);
1268 +
1269 +        f = new CompletableFuture<Integer>();
1270 +        f2 = new CompletableFuture<Integer>();
1271 +        g = f.thenCombineAsync(f2, add);
1272 +        f.complete(one);
1273 +        f2.completeExceptionally(new CFException());
1274 +        checkCompletedWithWrappedCFException(g);
1275 +    }
1276 +
1277 +    /**
1278 +     * thenCombineAsync result completes exceptionally if action does
1279 +     */
1280 +    public void testThenCombineAsync3() {
1281 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1282 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1283 +        FailingBiFunction r = new FailingBiFunction();
1284 +        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r);
1285 +        f.complete(one);
1286 +        checkIncomplete(g);
1287 +        f2.complete(two);
1288 +        checkCompletedWithWrappedCFException(g);
1289 +    }
1290 +
1291 +    /**
1292 +     * thenCombineAsync result completes exceptionally if either source cancelled
1293 +     */
1294 +    public void testThenCombineAsync4() {
1295 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1296 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1297 +        CompletableFuture<Integer> g = f.thenCombineAsync(f2, add);
1298 +        assertTrue(f.cancel(true));
1299 +        f2.complete(two);
1300 +        checkCompletedWithWrappedCancellationException(g);
1301 +
1302 +        f = new CompletableFuture<Integer>();
1303 +        f2 = new CompletableFuture<Integer>();
1304 +        g = f.thenCombineAsync(f2, add);
1305 +        f.complete(one);
1306 +        assertTrue(f2.cancel(true));
1307 +        checkCompletedWithWrappedCancellationException(g);
1308 +    }
1309 +
1310 +    /**
1311 +     * thenAcceptBothAsync result completes normally after normal
1312 +     * completion of sources
1313 +     */
1314 +    public void testThenAcceptBothAsync() {
1315 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1316 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1317 +        AddAction r = new AddAction();
1318 +        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r);
1319 +        f.complete(one);
1320 +        checkIncomplete(g);
1321 +        f2.complete(two);
1322 +        checkCompletedNormally(g, null);
1323 +        assertEquals(r.value, 3);
1324 +    }
1325 +
1326 +    /**
1327 +     * thenAcceptBothAsync result completes exceptionally after exceptional
1328 +     * completion of source
1329 +     */
1330 +    public void testThenAcceptBothAsync2() {
1331 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1332 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1333 +        AddAction r = new AddAction();
1334 +        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r);
1335 +        f.completeExceptionally(new CFException());
1336 +        f2.complete(two);
1337 +        checkCompletedWithWrappedCFException(g);
1338 +
1339 +        r = new AddAction();
1340 +        f = new CompletableFuture<Integer>();
1341 +        f2 = new CompletableFuture<Integer>();
1342 +        g = f.thenAcceptBothAsync(f2, r);
1343 +        f.complete(one);
1344 +        f2.completeExceptionally(new CFException());
1345 +        checkCompletedWithWrappedCFException(g);
1346 +    }
1347 +
1348 +    /**
1349 +     * thenAcceptBothAsync result completes exceptionally if action does
1350 +     */
1351 +    public void testThenAcceptBothAsync3() {
1352 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1353 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1354 +        FailingBiConsumer r = new FailingBiConsumer();
1355 +        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r);
1356 +        f.complete(one);
1357 +        checkIncomplete(g);
1358 +        f2.complete(two);
1359 +        checkCompletedWithWrappedCFException(g);
1360 +    }
1361 +
1362 +    /**
1363 +     * thenAcceptBothAsync result completes exceptionally if either source cancelled
1364 +     */
1365 +    public void testThenAcceptBothAsync4() {
1366 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1367 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1368 +        AddAction r = new AddAction();
1369 +        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r);
1370 +        assertTrue(f.cancel(true));
1371 +        f2.complete(two);
1372 +        checkCompletedWithWrappedCancellationException(g);
1373 +
1374 +        r = new AddAction();
1375 +        f = new CompletableFuture<Integer>();
1376 +        f2 = new CompletableFuture<Integer>();
1377 +        g = f.thenAcceptBothAsync(f2, r);
1378 +        f.complete(one);
1379 +        assertTrue(f2.cancel(true));
1380 +        checkCompletedWithWrappedCancellationException(g);
1381 +    }
1382 +
1383 +    /**
1384 +     * runAfterBothAsync result completes normally after normal
1385 +     * completion of sources
1386 +     */
1387 +    public void testRunAfterBothAsync() {
1388 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1389 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1390 +        Noop r = new Noop();
1391 +        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1392 +        f.complete(one);
1393 +        checkIncomplete(g);
1394 +        f2.complete(two);
1395 +        checkCompletedNormally(g, null);
1396 +        assertTrue(r.ran);
1397 +    }
1398 +
1399 +    /**
1400 +     * runAfterBothAsync result completes exceptionally after exceptional
1401 +     * completion of source
1402 +     */
1403 +    public void testRunAfterBothAsync2() {
1404 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1405 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1406 +        Noop r = new Noop();
1407 +        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1408 +        f.completeExceptionally(new CFException());
1409 +        f2.complete(two);
1410 +        checkCompletedWithWrappedCFException(g);
1411 +
1412 +        r = new Noop();
1413 +        f = new CompletableFuture<Integer>();
1414 +        f2 = new CompletableFuture<Integer>();
1415 +        g = f.runAfterBothAsync(f2, r);
1416 +        f.complete(one);
1417 +        f2.completeExceptionally(new CFException());
1418 +        checkCompletedWithWrappedCFException(g);
1419 +    }
1420 +
1421 +    /**
1422 +     * runAfterBothAsync result completes exceptionally if action does
1423 +     */
1424 +    public void testRunAfterBothAsync3() {
1425 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1426 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1427 +        FailingNoop r = new FailingNoop();
1428 +        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1429 +        f.complete(one);
1430 +        checkIncomplete(g);
1431 +        f2.complete(two);
1432 +        checkCompletedWithWrappedCFException(g);
1433 +    }
1434 +
1435 +    /**
1436 +     * runAfterBothAsync result completes exceptionally if either source cancelled
1437 +     */
1438 +    public void testRunAfterBothAsync4() {
1439 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1440 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1441 +        Noop r = new Noop();
1442 +        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1443 +        assertTrue(f.cancel(true));
1444 +        f2.complete(two);
1445 +        checkCompletedWithWrappedCancellationException(g);
1446 +
1447 +        r = new Noop();
1448 +        f = new CompletableFuture<Integer>();
1449 +        f2 = new CompletableFuture<Integer>();
1450 +        g = f.runAfterBothAsync(f2, r);
1451 +        f.complete(one);
1452 +        assertTrue(f2.cancel(true));
1453 +        checkCompletedWithWrappedCancellationException(g);
1454 +    }
1455 +
1456 +    /**
1457 +     * applyToEitherAsync result completes normally after normal
1458 +     * completion of sources
1459 +     */
1460 +    public void testApplyToEitherAsync() {
1461 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1462 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1463 +        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1464 +        f.complete(one);
1465 +        checkCompletedNormally(g, two);
1466 +
1467 +        f = new CompletableFuture<Integer>();
1468 +        f.complete(one);
1469 +        f2 = new CompletableFuture<Integer>();
1470 +        g = f.applyToEitherAsync(f2, inc);
1471 +        checkCompletedNormally(g, two);
1472 +    }
1473 +
1474 +    /**
1475 +     * applyToEitherAsync result completes exceptionally after exceptional
1476 +     * completion of source
1477 +     */
1478 +    public void testApplyToEitherAsync2() {
1479 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1480 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1481 +        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1482 +        f.completeExceptionally(new CFException());
1483 +        checkCompletedWithWrappedCFException(g);
1484 +
1485 +        f = new CompletableFuture<Integer>();
1486 +        f2 = new CompletableFuture<Integer>();
1487 +        f2.completeExceptionally(new CFException());
1488 +        g = f.applyToEitherAsync(f2, inc);
1489 +        f.complete(one);
1490 +        checkCompletedWithWrappedCFException(g);
1491 +    }
1492 +
1493 +    /**
1494 +     * applyToEitherAsync result completes exceptionally if action does
1495 +     */
1496 +    public void testApplyToEitherAsync3() {
1497 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1498 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1499 +        FailingFunction r = new FailingFunction();
1500 +        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r);
1501 +        f.complete(one);
1502 +        checkCompletedWithWrappedCFException(g);
1503 +    }
1504 +
1505 +    /**
1506 +     * applyToEitherAsync result completes exceptionally if either source cancelled
1507 +     */
1508 +    public void testApplyToEitherAsync4() {
1509 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1510 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1511 +        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1512 +        assertTrue(f.cancel(true));
1513 +        checkCompletedWithWrappedCancellationException(g);
1514 +
1515 +        f = new CompletableFuture<Integer>();
1516 +        f2 = new CompletableFuture<Integer>();
1517 +        assertTrue(f2.cancel(true));
1518 +        g = f.applyToEitherAsync(f2, inc);
1519 +        checkCompletedWithWrappedCancellationException(g);
1520 +    }
1521 +
1522 +    /**
1523 +     * acceptEitherAsync result completes normally after normal
1524 +     * completion of sources
1525 +     */
1526 +    public void testAcceptEitherAsync() {
1527 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1528 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1529 +        IncAction r = new IncAction();
1530 +        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1531 +        f.complete(one);
1532 +        checkCompletedNormally(g, null);
1533 +        assertEquals(r.value, 2);
1534 +
1535 +        r = new IncAction();
1536 +        f = new CompletableFuture<Integer>();
1537 +        f.complete(one);
1538 +        f2 = new CompletableFuture<Integer>();
1539 +        g = f.acceptEitherAsync(f2, r);
1540 +        checkCompletedNormally(g, null);
1541 +        assertEquals(r.value, 2);
1542 +    }
1543 +
1544 +    /**
1545 +     * acceptEitherAsync result completes exceptionally after exceptional
1546 +     * completion of source
1547 +     */
1548 +    public void testAcceptEitherAsync2() {
1549 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1550 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1551 +        IncAction r = new IncAction();
1552 +        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1553 +        f.completeExceptionally(new CFException());
1554 +        checkCompletedWithWrappedCFException(g);
1555 +
1556 +        r = new IncAction();
1557 +        f = new CompletableFuture<Integer>();
1558 +        f2 = new CompletableFuture<Integer>();
1559 +        f2.completeExceptionally(new CFException());
1560 +        g = f.acceptEitherAsync(f2, r);
1561 +        f.complete(one);
1562 +        checkCompletedWithWrappedCFException(g);
1563 +    }
1564 +
1565 +    /**
1566 +     * acceptEitherAsync result completes exceptionally if action does
1567 +     */
1568 +    public void testAcceptEitherAsync3() {
1569 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1570 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1571 +        FailingConsumer r = new FailingConsumer();
1572 +        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1573 +        f.complete(one);
1574 +        checkCompletedWithWrappedCFException(g);
1575 +    }
1576 +
1577 +    /**
1578 +     * acceptEitherAsync result completes exceptionally if either
1579 +     * source cancelled
1580 +     */
1581 +    public void testAcceptEitherAsync4() {
1582 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1583 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1584 +        IncAction r = new IncAction();
1585 +        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1586 +        assertTrue(f.cancel(true));
1587 +        checkCompletedWithWrappedCancellationException(g);
1588 +
1589 +        r = new IncAction();
1590 +        f = new CompletableFuture<Integer>();
1591 +        f2 = new CompletableFuture<Integer>();
1592 +        assertTrue(f2.cancel(true));
1593 +        g = f.acceptEitherAsync(f2, r);
1594 +        checkCompletedWithWrappedCancellationException(g);
1595 +    }
1596 +
1597 +    /**
1598 +     * runAfterEitherAsync result completes normally after normal
1599 +     * completion of sources
1600 +     */
1601 +    public void testRunAfterEitherAsync() {
1602 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1603 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1604 +        Noop r = new Noop();
1605 +        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1606 +        f.complete(one);
1607 +        checkCompletedNormally(g, null);
1608 +        assertTrue(r.ran);
1609 +
1610 +        r = new Noop();
1611 +        f = new CompletableFuture<Integer>();
1612 +        f.complete(one);
1613 +        f2 = new CompletableFuture<Integer>();
1614 +        g = f.runAfterEitherAsync(f2, r);
1615 +        checkCompletedNormally(g, null);
1616 +        assertTrue(r.ran);
1617 +    }
1618 +
1619 +    /**
1620 +     * runAfterEitherAsync result completes exceptionally after exceptional
1621 +     * completion of source
1622 +     */
1623 +    public void testRunAfterEitherAsync2() {
1624 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1625 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1626 +        Noop r = new Noop();
1627 +        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1628 +        f.completeExceptionally(new CFException());
1629 +        checkCompletedWithWrappedCFException(g);
1630 +
1631 +        r = new Noop();
1632 +        f = new CompletableFuture<Integer>();
1633 +        f2 = new CompletableFuture<Integer>();
1634 +        f2.completeExceptionally(new CFException());
1635 +        g = f.runAfterEitherAsync(f2, r);
1636 +        f.complete(one);
1637 +        checkCompletedWithWrappedCFException(g);
1638 +    }
1639 +
1640 +    /**
1641 +     * runAfterEitherAsync result completes exceptionally if action does
1642 +     */
1643 +    public void testRunAfterEitherAsync3() {
1644 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1645 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1646 +        FailingNoop r = new FailingNoop();
1647 +        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1648 +        f.complete(one);
1649 +        checkCompletedWithWrappedCFException(g);
1650 +    }
1651 +
1652 +    /**
1653 +     * runAfterEitherAsync result completes exceptionally if either
1654 +     * source cancelled
1655 +     */
1656 +    public void testRunAfterEitherAsync4() {
1657 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1658 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1659 +        Noop r = new Noop();
1660 +        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1661 +        assertTrue(f.cancel(true));
1662 +        checkCompletedWithWrappedCancellationException(g);
1663 +
1664 +        r = new Noop();
1665 +        f = new CompletableFuture<Integer>();
1666 +        f2 = new CompletableFuture<Integer>();
1667 +        assertTrue(f2.cancel(true));
1668 +        g = f.runAfterEitherAsync(f2, r);
1669 +        checkCompletedWithWrappedCancellationException(g);
1670 +    }
1671 +
1672 +    /**
1673 +     * thenComposeAsync result completes normally after normal
1674 +     * completion of source
1675 +     */
1676 +    public void testThenComposeAsync() {
1677 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1678 +        CompletableFutureInc r = new CompletableFutureInc();
1679 +        CompletableFuture<Integer> g = f.thenComposeAsync(r);
1680 +        f.complete(one);
1681 +        checkCompletedNormally(g, two);
1682 +    }
1683 +
1684 +    /**
1685 +     * thenComposeAsync result completes exceptionally after
1686 +     * exceptional completion of source
1687 +     */
1688 +    public void testThenComposeAsync2() {
1689 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1690 +        CompletableFutureInc r = new CompletableFutureInc();
1691 +        CompletableFuture<Integer> g = f.thenComposeAsync(r);
1692 +        f.completeExceptionally(new CFException());
1693 +        checkCompletedWithWrappedCFException(g);
1694 +    }
1695 +
1696 +    /**
1697 +     * thenComposeAsync result completes exceptionally if action does
1698 +     */
1699 +    public void testThenComposeAsync3() {
1700 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1701 +        FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
1702 +        CompletableFuture<Integer> g = f.thenComposeAsync(r);
1703 +        f.complete(one);
1704 +        checkCompletedWithWrappedCFException(g);
1705 +    }
1706 +
1707 +    /**
1708 +     * thenComposeAsync result completes exceptionally if source cancelled
1709 +     */
1710 +    public void testThenComposeAsync4() {
1711 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1712 +        CompletableFutureInc r = new CompletableFutureInc();
1713 +        CompletableFuture<Integer> g = f.thenComposeAsync(r);
1714 +        assertTrue(f.cancel(true));
1715 +        checkCompletedWithWrappedCancellationException(g);
1716 +    }
1717 +
1718 +
1719 +    // async with explicit executors
1720 +
1721 +    /**
1722 +     * thenRunAsync result completes normally after normal completion of source
1723 +     */
1724 +    public void testThenRunAsyncE() {
1725 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1726 +        Noop r = new Noop();
1727 +        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
1728 +        f.complete(null);
1729 +        checkCompletedNormally(g, null);
1730 +
1731 +        // reordered version
1732 +        f = new CompletableFuture<Integer>();
1733 +        f.complete(null);
1734 +        r = new Noop();
1735 +        g = f.thenRunAsync(r, new ThreadExecutor());
1736 +        checkCompletedNormally(g, null);
1737 +    }
1738 +
1739 +    /**
1740 +     * thenRunAsync result completes exceptionally after exceptional
1741 +     * completion of source
1742 +     */
1743 +    public void testThenRunAsync2E() {
1744 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1745 +        Noop r = new Noop();
1746 +        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
1747 +        f.completeExceptionally(new CFException());
1748 +        try {
1749 +            g.join();
1750 +            shouldThrow();
1751 +        } catch (Exception ok) {
1752 +        }
1753 +        checkCompletedWithWrappedCFException(g);
1754 +    }
1755 +
1756 +    /**
1757 +     * thenRunAsync result completes exceptionally if action does
1758 +     */
1759 +    public void testThenRunAsync3E() {
1760 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1761 +        FailingNoop r = new FailingNoop();
1762 +        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
1763 +        f.complete(null);
1764 +        checkCompletedWithWrappedCFException(g);
1765 +    }
1766 +
1767 +    /**
1768 +     * thenRunAsync result completes exceptionally if source cancelled
1769 +     */
1770 +    public void testThenRunAsync4E() {
1771 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1772 +        Noop r = new Noop();
1773 +        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
1774 +        assertTrue(f.cancel(true));
1775 +        checkCompletedWithWrappedCancellationException(g);
1776 +    }
1777 +
1778 +    /**
1779 +     * thenApplyAsync result completes normally after normal completion of source
1780 +     */
1781 +    public void testThenApplyAsyncE() {
1782 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1783 +        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
1784 +        f.complete(one);
1785 +        checkCompletedNormally(g, two);
1786 +    }
1787 +
1788 +    /**
1789 +     * thenApplyAsync result completes exceptionally after exceptional
1790 +     * completion of source
1791 +     */
1792 +    public void testThenApplyAsync2E() {
1793 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1794 +        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
1795 +        f.completeExceptionally(new CFException());
1796 +        checkCompletedWithWrappedCFException(g);
1797 +    }
1798 +
1799 +    /**
1800 +     * thenApplyAsync result completes exceptionally if action does
1801 +     */
1802 +    public void testThenApplyAsync3E() {
1803 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1804 +        FailingFunction r = new FailingFunction();
1805 +        CompletableFuture<Integer> g = f.thenApplyAsync(r, new ThreadExecutor());
1806 +        f.complete(null);
1807 +        checkCompletedWithWrappedCFException(g);
1808 +    }
1809 +
1810 +    /**
1811 +     * thenApplyAsync result completes exceptionally if source cancelled
1812 +     */
1813 +    public void testThenApplyAsync4E() {
1814 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1815 +        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
1816 +        assertTrue(f.cancel(true));
1817 +        checkCompletedWithWrappedCancellationException(g);
1818 +    }
1819 +
1820 +    /**
1821 +     * thenAcceptAsync result completes normally after normal
1822 +     * completion of source
1823 +     */
1824 +    public void testThenAcceptAsyncE() {
1825 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1826 +        IncAction r = new IncAction();
1827 +        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
1828 +        f.complete(one);
1829 +        checkCompletedNormally(g, null);
1830 +        assertEquals(r.value, 2);
1831 +    }
1832 +
1833 +    /**
1834 +     * thenAcceptAsync result completes exceptionally after exceptional
1835 +     * completion of source
1836 +     */
1837 +    public void testThenAcceptAsync2E() {
1838 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1839 +        IncAction r = new IncAction();
1840 +        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
1841 +        f.completeExceptionally(new CFException());
1842 +        checkCompletedWithWrappedCFException(g);
1843 +    }
1844 +
1845 +    /**
1846 +     * thenAcceptAsync result completes exceptionally if action does
1847 +     */
1848 +    public void testThenAcceptAsync3E() {
1849 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1850 +        FailingConsumer r = new FailingConsumer();
1851 +        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
1852 +        f.complete(null);
1853 +        checkCompletedWithWrappedCFException(g);
1854 +    }
1855 +
1856 +    /**
1857 +     * thenAcceptAsync result completes exceptionally if source cancelled
1858 +     */
1859 +    public void testThenAcceptAsync4E() {
1860 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1861 +        IncAction r = new IncAction();
1862 +        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
1863 +        assertTrue(f.cancel(true));
1864 +        checkCompletedWithWrappedCancellationException(g);
1865 +    }
1866 +    /**
1867 +     * thenCombineAsync result completes normally after normal
1868 +     * completion of sources
1869 +     */
1870 +    public void testThenCombineAsyncE() {
1871 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1872 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1873 +        CompletableFuture<Integer> g = f.thenCombineAsync(f2, add, new ThreadExecutor());
1874 +        f.complete(one);
1875 +        checkIncomplete(g);
1876 +        f2.complete(two);
1877 +        checkCompletedNormally(g, three);
1878 +    }
1879 +
1880 +    /**
1881 +     * thenCombineAsync result completes exceptionally after exceptional
1882 +     * completion of source
1883 +     */
1884 +    public void testThenCombineAsync2E() {
1885 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1886 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1887 +        CompletableFuture<Integer> g = f.thenCombineAsync(f2, add, new ThreadExecutor());
1888 +        f.completeExceptionally(new CFException());
1889 +        f2.complete(two);
1890 +        checkCompletedWithWrappedCFException(g);
1891 +
1892 +        f = new CompletableFuture<Integer>();
1893 +        f2 = new CompletableFuture<Integer>();
1894 +        g = f.thenCombineAsync(f2, add, new ThreadExecutor());
1895 +        f.complete(one);
1896 +        f2.completeExceptionally(new CFException());
1897 +        checkCompletedWithWrappedCFException(g);
1898 +    }
1899 +
1900 +    /**
1901 +     * thenCombineAsync result completes exceptionally if action does
1902 +     */
1903 +    public void testThenCombineAsync3E() {
1904 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1905 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1906 +        FailingBiFunction r = new FailingBiFunction();
1907 +        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r, new ThreadExecutor());
1908 +        f.complete(one);
1909 +        checkIncomplete(g);
1910 +        f2.complete(two);
1911 +        checkCompletedWithWrappedCFException(g);
1912 +    }
1913 +
1914 +    /**
1915 +     * thenCombineAsync result completes exceptionally if either source cancelled
1916 +     */
1917 +    public void testThenCombineAsync4E() {
1918 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1919 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1920 +        CompletableFuture<Integer> g = f.thenCombineAsync(f2, add, new ThreadExecutor());
1921 +        assertTrue(f.cancel(true));
1922 +        f2.complete(two);
1923 +        checkCompletedWithWrappedCancellationException(g);
1924 +
1925 +        f = new CompletableFuture<Integer>();
1926 +        f2 = new CompletableFuture<Integer>();
1927 +        g = f.thenCombineAsync(f2, add, new ThreadExecutor());
1928 +        f.complete(one);
1929 +        assertTrue(f2.cancel(true));
1930 +        checkCompletedWithWrappedCancellationException(g);
1931 +    }
1932 +
1933 +    /**
1934 +     * thenAcceptBothAsync result completes normally after normal
1935 +     * completion of sources
1936 +     */
1937 +    public void testThenAcceptBothAsyncE() {
1938 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1939 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1940 +        AddAction r = new AddAction();
1941 +        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
1942 +        f.complete(one);
1943 +        checkIncomplete(g);
1944 +        f2.complete(two);
1945 +        checkCompletedNormally(g, null);
1946 +        assertEquals(r.value, 3);
1947 +    }
1948 +
1949 +    /**
1950 +     * thenAcceptBothAsync result completes exceptionally after exceptional
1951 +     * completion of source
1952 +     */
1953 +    public void testThenAcceptBothAsync2E() {
1954 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1955 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1956 +        AddAction r = new AddAction();
1957 +        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
1958 +        f.completeExceptionally(new CFException());
1959 +        f2.complete(two);
1960 +        checkCompletedWithWrappedCFException(g);
1961 +
1962 +        r = new AddAction();
1963 +        f = new CompletableFuture<Integer>();
1964 +        f2 = new CompletableFuture<Integer>();
1965 +        g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
1966 +        f.complete(one);
1967 +        f2.completeExceptionally(new CFException());
1968 +        checkCompletedWithWrappedCFException(g);
1969 +    }
1970 +
1971 +    /**
1972 +     * thenAcceptBothAsync result completes exceptionally if action does
1973 +     */
1974 +    public void testThenAcceptBothAsync3E() {
1975 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1976 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1977 +        FailingBiConsumer r = new FailingBiConsumer();
1978 +        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
1979 +        f.complete(one);
1980 +        checkIncomplete(g);
1981 +        f2.complete(two);
1982 +        checkCompletedWithWrappedCFException(g);
1983 +    }
1984 +
1985 +    /**
1986 +     * thenAcceptBothAsync result completes exceptionally if either source cancelled
1987 +     */
1988 +    public void testThenAcceptBothAsync4E() {
1989 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1990 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1991 +        AddAction r = new AddAction();
1992 +        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
1993 +        assertTrue(f.cancel(true));
1994 +        f2.complete(two);
1995 +        checkCompletedWithWrappedCancellationException(g);
1996 +
1997 +        r = new AddAction();
1998 +        f = new CompletableFuture<Integer>();
1999 +        f2 = new CompletableFuture<Integer>();
2000 +        g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
2001 +        f.complete(one);
2002 +        assertTrue(f2.cancel(true));
2003 +        checkCompletedWithWrappedCancellationException(g);
2004 +    }
2005 +
2006 +    /**
2007 +     * runAfterBothAsync result completes normally after normal
2008 +     * completion of sources
2009 +     */
2010 +    public void testRunAfterBothAsyncE() {
2011 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2012 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2013 +        Noop r = new Noop();
2014 +        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2015 +        f.complete(one);
2016 +        checkIncomplete(g);
2017 +        f2.complete(two);
2018 +        checkCompletedNormally(g, null);
2019 +        assertTrue(r.ran);
2020 +    }
2021 +
2022 +    /**
2023 +     * runAfterBothAsync result completes exceptionally after exceptional
2024 +     * completion of source
2025 +     */
2026 +    public void testRunAfterBothAsync2E() {
2027 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2028 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2029 +        Noop r = new Noop();
2030 +        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2031 +        f.completeExceptionally(new CFException());
2032 +        f2.complete(two);
2033 +        checkCompletedWithWrappedCFException(g);
2034 +
2035 +        r = new Noop();
2036 +        f = new CompletableFuture<Integer>();
2037 +        f2 = new CompletableFuture<Integer>();
2038 +        g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2039 +        f.complete(one);
2040 +        f2.completeExceptionally(new CFException());
2041 +        checkCompletedWithWrappedCFException(g);
2042 +    }
2043 +
2044 +    /**
2045 +     * runAfterBothAsync result completes exceptionally if action does
2046 +     */
2047 +    public void testRunAfterBothAsync3E() {
2048 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2049 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2050 +        FailingNoop r = new FailingNoop();
2051 +        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2052 +        f.complete(one);
2053 +        checkIncomplete(g);
2054 +        f2.complete(two);
2055 +        checkCompletedWithWrappedCFException(g);
2056 +    }
2057 +
2058 +    /**
2059 +     * runAfterBothAsync result completes exceptionally if either source cancelled
2060 +     */
2061 +    public void testRunAfterBothAsync4E() {
2062 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2063 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2064 +        Noop r = new Noop();
2065 +        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2066 +        assertTrue(f.cancel(true));
2067 +        f2.complete(two);
2068 +        checkCompletedWithWrappedCancellationException(g);
2069 +
2070 +        r = new Noop();
2071 +        f = new CompletableFuture<Integer>();
2072 +        f2 = new CompletableFuture<Integer>();
2073 +        g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2074 +        f.complete(one);
2075 +        assertTrue(f2.cancel(true));
2076 +        checkCompletedWithWrappedCancellationException(g);
2077 +    }
2078 +
2079 +    /**
2080 +     * applyToEitherAsync result completes normally after normal
2081 +     * completion of sources
2082 +     */
2083 +    public void testApplyToEitherAsyncE() {
2084 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2085 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2086 +        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2087 +        f.complete(one);
2088 +        checkCompletedNormally(g, two);
2089 +
2090 +        f = new CompletableFuture<Integer>();
2091 +        f.complete(one);
2092 +        f2 = new CompletableFuture<Integer>();
2093 +        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2094 +        checkCompletedNormally(g, two);
2095 +    }
2096 +
2097 +    /**
2098 +     * applyToEitherAsync result completes exceptionally after exceptional
2099 +     * completion of source
2100 +     */
2101 +    public void testApplyToEitherAsync2E() {
2102 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2103 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2104 +        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2105 +        f.completeExceptionally(new CFException());
2106 +        checkCompletedWithWrappedCFException(g);
2107 +
2108 +        f = new CompletableFuture<Integer>();
2109 +        f2 = new CompletableFuture<Integer>();
2110 +        f2.completeExceptionally(new CFException());
2111 +        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2112 +        f.complete(one);
2113 +        checkCompletedWithWrappedCFException(g);
2114 +    }
2115 +
2116 +    /**
2117 +     * applyToEitherAsync result completes exceptionally if action does
2118 +     */
2119 +    public void testApplyToEitherAsync3E() {
2120 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2121 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2122 +        FailingFunction r = new FailingFunction();
2123 +        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor());
2124 +        f.complete(one);
2125 +        checkCompletedWithWrappedCFException(g);
2126 +    }
2127 +
2128 +    /**
2129 +     * applyToEitherAsync result completes exceptionally if either source cancelled
2130 +     */
2131 +    public void testApplyToEitherAsync4E() {
2132 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2133 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2134 +        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2135 +        assertTrue(f.cancel(true));
2136 +        checkCompletedWithWrappedCancellationException(g);
2137 +
2138 +        f = new CompletableFuture<Integer>();
2139 +        f2 = new CompletableFuture<Integer>();
2140 +        assertTrue(f2.cancel(true));
2141 +        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2142 +        checkCompletedWithWrappedCancellationException(g);
2143 +    }
2144 +
2145 +    /**
2146 +     * acceptEitherAsync result completes normally after normal
2147 +     * completion of sources
2148 +     */
2149 +    public void testAcceptEitherAsyncE() {
2150 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2151 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2152 +        IncAction r = new IncAction();
2153 +        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2154 +        f.complete(one);
2155 +        checkCompletedNormally(g, null);
2156 +        assertEquals(r.value, 2);
2157 +
2158 +        r = new IncAction();
2159 +        f = new CompletableFuture<Integer>();
2160 +        f.complete(one);
2161 +        f2 = new CompletableFuture<Integer>();
2162 +        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2163 +        checkCompletedNormally(g, null);
2164 +        assertEquals(r.value, 2);
2165 +    }
2166 +
2167 +    /**
2168 +     * acceptEitherAsync result completes exceptionally after exceptional
2169 +     * completion of source
2170 +     */
2171 +    public void testAcceptEitherAsync2E() {
2172 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2173 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2174 +        IncAction r = new IncAction();
2175 +        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2176 +        f.completeExceptionally(new CFException());
2177 +        checkCompletedWithWrappedCFException(g);
2178 +
2179 +        r = new IncAction();
2180 +        f = new CompletableFuture<Integer>();
2181 +        f2 = new CompletableFuture<Integer>();
2182 +        f2.completeExceptionally(new CFException());
2183 +        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2184 +        f.complete(one);
2185 +        checkCompletedWithWrappedCFException(g);
2186 +    }
2187 +
2188 +    /**
2189 +     * acceptEitherAsync result completes exceptionally if action does
2190 +     */
2191 +    public void testAcceptEitherAsync3E() {
2192 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2193 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2194 +        FailingConsumer r = new FailingConsumer();
2195 +        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2196 +        f.complete(one);
2197 +        checkCompletedWithWrappedCFException(g);
2198 +    }
2199 +
2200 +    /**
2201 +     * acceptEitherAsync result completes exceptionally if either
2202 +     * source cancelled
2203 +     */
2204 +    public void testAcceptEitherAsync4E() {
2205 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2206 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2207 +        IncAction r = new IncAction();
2208 +        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2209 +        assertTrue(f.cancel(true));
2210 +        checkCompletedWithWrappedCancellationException(g);
2211 +
2212 +        r = new IncAction();
2213 +        f = new CompletableFuture<Integer>();
2214 +        f2 = new CompletableFuture<Integer>();
2215 +        assertTrue(f2.cancel(true));
2216 +        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2217 +        checkCompletedWithWrappedCancellationException(g);
2218 +    }
2219 +
2220 +    /**
2221 +     * runAfterEitherAsync result completes normally after normal
2222 +     * completion of sources
2223 +     */
2224 +    public void testRunAfterEitherAsyncE() {
2225 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2226 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2227 +        Noop r = new Noop();
2228 +        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2229 +        f.complete(one);
2230 +        checkCompletedNormally(g, null);
2231 +        assertTrue(r.ran);
2232 +
2233 +        r = new Noop();
2234 +        f = new CompletableFuture<Integer>();
2235 +        f.complete(one);
2236 +        f2 = new CompletableFuture<Integer>();
2237 +        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2238 +        checkCompletedNormally(g, null);
2239 +        assertTrue(r.ran);
2240 +    }
2241 +
2242 +    /**
2243 +     * runAfterEitherAsync result completes exceptionally after exceptional
2244 +     * completion of source
2245 +     */
2246 +    public void testRunAfterEitherAsync2E() {
2247 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2248 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2249 +        Noop r = new Noop();
2250 +        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2251 +        f.completeExceptionally(new CFException());
2252 +        checkCompletedWithWrappedCFException(g);
2253 +
2254 +        r = new Noop();
2255 +        f = new CompletableFuture<Integer>();
2256 +        f2 = new CompletableFuture<Integer>();
2257 +        f2.completeExceptionally(new CFException());
2258 +        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2259 +        f.complete(one);
2260 +        checkCompletedWithWrappedCFException(g);
2261 +    }
2262 +
2263 +    /**
2264 +     * runAfterEitherAsync result completes exceptionally if action does
2265 +     */
2266 +    public void testRunAfterEitherAsync3E() {
2267 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2268 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2269 +        FailingNoop r = new FailingNoop();
2270 +        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2271 +        f.complete(one);
2272 +        checkCompletedWithWrappedCFException(g);
2273 +    }
2274 +
2275 +    /**
2276 +     * runAfterEitherAsync result completes exceptionally if either
2277 +     * source cancelled
2278 +     */
2279 +    public void testRunAfterEitherAsync4E() {
2280 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2281 +        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2282 +        Noop r = new Noop();
2283 +        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2284 +        assertTrue(f.cancel(true));
2285 +        checkCompletedWithWrappedCancellationException(g);
2286 +
2287 +        r = new Noop();
2288 +        f = new CompletableFuture<Integer>();
2289 +        f2 = new CompletableFuture<Integer>();
2290 +        assertTrue(f2.cancel(true));
2291 +        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2292 +        checkCompletedWithWrappedCancellationException(g);
2293 +    }
2294 +
2295 +    /**
2296 +     * thenComposeAsync result completes normally after normal
2297 +     * completion of source
2298 +     */
2299 +    public void testThenComposeAsyncE() {
2300 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2301 +        CompletableFutureInc r = new CompletableFutureInc();
2302 +        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2303 +        f.complete(one);
2304 +        checkCompletedNormally(g, two);
2305 +    }
2306 +
2307 +    /**
2308 +     * thenComposeAsync result completes exceptionally after
2309 +     * exceptional completion of source
2310 +     */
2311 +    public void testThenComposeAsync2E() {
2312 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2313 +        CompletableFutureInc r = new CompletableFutureInc();
2314 +        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2315 +        f.completeExceptionally(new CFException());
2316 +        checkCompletedWithWrappedCFException(g);
2317 +    }
2318 +
2319 +    /**
2320 +     * thenComposeAsync result completes exceptionally if action does
2321 +     */
2322 +    public void testThenComposeAsync3E() {
2323 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2324 +        FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
2325 +        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2326 +        f.complete(one);
2327 +        checkCompletedWithWrappedCFException(g);
2328 +    }
2329 +
2330 +    /**
2331 +     * thenComposeAsync result completes exceptionally if source cancelled
2332 +     */
2333 +    public void testThenComposeAsync4E() {
2334 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2335 +        CompletableFutureInc r = new CompletableFutureInc();
2336 +        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2337 +        assertTrue(f.cancel(true));
2338 +        checkCompletedWithWrappedCancellationException(g);
2339 +    }
2340 +
2341 +    // other static methods
2342 +
2343 +    /**
2344       * allOf(no component futures) returns a future completed normally
2345       * with the value null
2346       */
# Line 89 | Line 2350 | public class CompletableFutureTest exten
2350      }
2351  
2352      /**
2353 +     * allOf returns a future completed when all components complete
2354 +     */
2355 +    public void testAllOf() throws Exception {
2356 +        for (int k = 1; k < 20; ++k) {
2357 +            CompletableFuture[] fs = new CompletableFuture[k];
2358 +            for (int i = 0; i < k; ++i)
2359 +                fs[i] = new CompletableFuture<Integer>();
2360 +            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
2361 +            for (int i = 0; i < k; ++i) {
2362 +                checkIncomplete(f);
2363 +                fs[i].complete(one);
2364 +            }
2365 +            checkCompletedNormally(f, null);
2366 +        }
2367 +    }
2368 +
2369 +    /**
2370       * anyOf(no component futures) returns an incomplete future
2371       */
2372      public void testAnyOf_empty() throws Exception {
2373          CompletableFuture<?> f = CompletableFuture.anyOf();
2374          checkIncomplete(f);
2375      }
2376 +
2377 +    /**
2378 +     * anyOf returns a future completed when any components complete
2379 +     */
2380 +    public void testAnyOf() throws Exception {
2381 +        for (int k = 1; k < 20; ++k) {
2382 +            CompletableFuture[] fs = new CompletableFuture[k];
2383 +            for (int i = 0; i < k; ++i)
2384 +                fs[i] = new CompletableFuture<Integer>();
2385 +            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
2386 +            checkIncomplete(f);
2387 +            for (int i = 0; i < k; ++i) {
2388 +                fs[i].complete(one);
2389 +                checkCompletedNormally(f, one);
2390 +            }
2391 +        }
2392 +    }
2393 +
2394 +    /**
2395 +     * Completion methods throw NullPointerException with null arguments
2396 +     */
2397 +    public void testNPE() {
2398 +        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2399 +        CompletableFuture<Integer> g = new CompletableFuture<Integer>();
2400 +        CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null;
2401 +        CompletableFuture<?> h;
2402 +        Executor exec = new ThreadExecutor();
2403 +
2404 +        Runnable[] throwingActions = {
2405 +            () -> { CompletableFuture.supplyAsync(null); },
2406 +            () -> { CompletableFuture.supplyAsync(null, exec); },
2407 +            () -> { CompletableFuture.supplyAsync(() -> one, null); },
2408 +
2409 +            () -> { CompletableFuture.runAsync(null); },
2410 +            () -> { CompletableFuture.runAsync(null, exec); },
2411 +            () -> { CompletableFuture.runAsync(() -> {}, null); },
2412 +
2413 +            () -> { f.completeExceptionally(null); },
2414 +
2415 +            () -> { f.thenApply(null); },
2416 +            () -> { f.thenApplyAsync(null); },
2417 +            () -> { f.thenApplyAsync((x) -> x, null); },
2418 +            () -> { f.thenApplyAsync(null, exec); },
2419 +
2420 +            () -> { f.thenAccept(null); },
2421 +            () -> { f.thenAcceptAsync(null); },
2422 +            () -> { f.thenAcceptAsync((x) -> { ; }, null); },
2423 +            () -> { f.thenAcceptAsync(null, exec); },
2424 +
2425 +            () -> { f.thenRun(null); },
2426 +            () -> { f.thenRunAsync(null); },
2427 +            () -> { f.thenRunAsync(() -> { ; }, null); },
2428 +            () -> { f.thenRunAsync(null, exec); },
2429 +
2430 +            () -> { f.thenCombine(g, null); },
2431 +            () -> { f.thenCombineAsync(g, null); },
2432 +            () -> { f.thenCombineAsync(g, null, exec); },
2433 +            () -> { f.thenCombine(nullFuture, (x, y) -> x); },
2434 +            () -> { f.thenCombineAsync(nullFuture, (x, y) -> x); },
2435 +            () -> { f.thenCombineAsync(nullFuture, (x, y) -> x, exec); },
2436 +            () -> { f.thenCombineAsync(g, (x, y) -> x, null); },
2437 +
2438 +            () -> { f.thenAcceptBoth(g, null); },
2439 +            () -> { f.thenAcceptBothAsync(g, null); },
2440 +            () -> { f.thenAcceptBothAsync(g, null, exec); },
2441 +            () -> { f.thenAcceptBoth(nullFuture, (x, y) -> {}); },
2442 +            () -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}); },
2443 +            () -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec); },
2444 +            () -> { f.thenAcceptBothAsync(g, (x, y) -> {}, null); },
2445 +
2446 +            () -> { f.runAfterBoth(g, null); },
2447 +            () -> { f.runAfterBothAsync(g, null); },
2448 +            () -> { f.runAfterBothAsync(g, null, exec); },
2449 +            () -> { f.runAfterBoth(nullFuture, () -> {}); },
2450 +            () -> { f.runAfterBothAsync(nullFuture, () -> {}); },
2451 +            () -> { f.runAfterBothAsync(nullFuture, () -> {}, exec); },
2452 +            () -> { f.runAfterBothAsync(g, () -> {}, null); },
2453 +
2454 +            () -> { f.applyToEither(g, null); },
2455 +            () -> { f.applyToEitherAsync(g, null); },
2456 +            () -> { f.applyToEitherAsync(g, null, exec); },
2457 +            () -> { f.applyToEither(nullFuture, (x) -> x); },
2458 +            () -> { f.applyToEitherAsync(nullFuture, (x) -> x); },
2459 +            () -> { f.applyToEitherAsync(nullFuture, (x) -> x, exec); },
2460 +            () -> { f.applyToEitherAsync(g, (x) -> x, null); },
2461 +
2462 +            () -> { f.acceptEither(g, null); },
2463 +            () -> { f.acceptEitherAsync(g, null); },
2464 +            () -> { f.acceptEitherAsync(g, null, exec); },
2465 +            () -> { f.acceptEither(nullFuture, (x) -> {}); },
2466 +            () -> { f.acceptEitherAsync(nullFuture, (x) -> {}); },
2467 +            () -> { f.acceptEitherAsync(nullFuture, (x) -> {}, exec); },
2468 +            () -> { f.acceptEitherAsync(g, (x) -> {}, null); },
2469 +
2470 +            () -> { f.runAfterEither(g, null); },
2471 +            () -> { f.runAfterEitherAsync(g, null); },
2472 +            () -> { f.runAfterEitherAsync(g, null, exec); },
2473 +            () -> { f.runAfterEither(nullFuture, () -> {}); },
2474 +            () -> { f.runAfterEitherAsync(nullFuture, () -> {}); },
2475 +            () -> { f.runAfterEitherAsync(nullFuture, () -> {}, exec); },
2476 +            () -> { f.runAfterEitherAsync(g, () -> {}, null); },
2477 +
2478 +            () -> { f.thenCompose(null); },
2479 +            () -> { f.thenComposeAsync(null); },
2480 +            () -> { f.thenComposeAsync(new CompletableFutureInc(), null); },
2481 +            () -> { f.thenComposeAsync(null, exec); },
2482 +
2483 +            () -> { f.exceptionally(null); },
2484 +
2485 +            () -> { f.handle(null); },
2486 +
2487 +            () -> { CompletableFuture.allOf((CompletableFuture<?>)null); },
2488 +            () -> { CompletableFuture.allOf((CompletableFuture<?>[])null); },
2489 +            () -> { CompletableFuture.allOf(f, null); },
2490 +            () -> { CompletableFuture.allOf(null, f); },
2491 +
2492 +            () -> { CompletableFuture.anyOf((CompletableFuture<?>)null); },
2493 +            () -> { CompletableFuture.anyOf((CompletableFuture<?>[])null); },
2494 +            () -> { CompletableFuture.anyOf(f, null); },
2495 +            () -> { CompletableFuture.anyOf(null, f); },
2496 +
2497 +            // TODO: Crashes javac with lambda-8-2013-03-31...
2498 +            //() -> { CompletableFuture<?> x = f.thenAccept(null); },
2499 +            //() -> { CompletableFuture<Void> x = f.thenRun(null); },
2500 +            //() -> { CompletableFuture<Integer> x = f.thenApply(() -> { ; }); },
2501 +        };
2502 +
2503 +        assertThrows(NullPointerException.class, throwingActions);
2504 +    }
2505 +
2506   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines