ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
Revision: 1.5
Committed: Thu Mar 21 16:26:43 2013 UTC (11 years, 1 month ago) by dl
Branch: MAIN
Changes since 1.4: +2356 -19 lines
Log Message:
CompletableFuture coverage

File Contents

# Content
1 /*
2 * Written by Doug Lea and Martin Buchholz with assistance from
3 * members of JCP JSR-166 Expert Group and released to the public
4 * domain, as explained at
5 * http://creativecommons.org/publicdomain/zero/1.0/
6 */
7
8 import junit.framework.*;
9 import java.util.concurrent.Callable;
10 import java.util.concurrent.Executor;
11 import java.util.concurrent.ExecutorService;
12 import java.util.concurrent.Executors;
13 import java.util.concurrent.CancellationException;
14 import java.util.concurrent.CountDownLatch;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.Future;
17 import java.util.concurrent.CompletableFuture;
18 import java.util.concurrent.CompletionException;
19 import java.util.concurrent.TimeoutException;
20 import java.util.concurrent.atomic.AtomicInteger;
21 import static java.util.concurrent.TimeUnit.MILLISECONDS;
22 import static java.util.concurrent.TimeUnit.SECONDS;
23 import java.util.*;
24 import java.util.function.Supplier;
25 import java.util.function.Consumer;
26 import java.util.function.BiConsumer;
27 import java.util.function.Function;
28 import java.util.function.BiFunction;
29
30 public class CompletableFutureTest extends JSR166TestCase {
31
32 public static void main(String[] args) {
33 junit.textui.TestRunner.run(suite());
34 }
35 public static Test suite() {
36 return new TestSuite(CompletableFutureTest.class);
37 }
38
39 static class CFException extends RuntimeException {}
40
41 void checkIncomplete(CompletableFuture<?> f) {
42 assertFalse(f.isDone());
43 assertFalse(f.isCancelled());
44 assertTrue(f.toString().contains("[Not completed]"));
45 try {
46 assertNull(f.getNow(null));
47 } catch (Throwable fail) { threadUnexpectedException(fail); }
48 try {
49 f.get(0L, SECONDS);
50 shouldThrow();
51 }
52 catch (TimeoutException success) {}
53 catch (Throwable fail) { threadUnexpectedException(fail); }
54 }
55
56 void checkCompletedNormally(CompletableFuture<?> f, Object 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 f.join();
77 shouldThrow();
78 } catch (Throwable ex) {
79 assertTrue(ex instanceof CompletionException &&
80 ((CompletionException)ex).getCause() instanceof CFException);
81 }
82 try {
83 f.getNow(null);
84 shouldThrow();
85 } catch (Throwable ex) {
86 assertTrue(ex instanceof CompletionException &&
87 ((CompletionException)ex).getCause() instanceof CFException);
88 }
89 try {
90 f.get();
91 shouldThrow();
92 } catch (Throwable ex) {
93 assertTrue(ex instanceof ExecutionException &&
94 ((ExecutionException)ex).getCause() instanceof CFException);
95 }
96 try {
97 f.get(0L, SECONDS);
98 shouldThrow();
99 } catch (Throwable ex) {
100 assertTrue(ex instanceof ExecutionException &&
101 ((ExecutionException)ex).getCause() instanceof CFException);
102 }
103 assertTrue(f.isDone());
104 assertFalse(f.isCancelled());
105 }
106
107 void checkCancelled(CompletableFuture<?> f) {
108 try {
109 f.join();
110 shouldThrow();
111 } catch (Throwable ex) {
112 assertTrue(ex instanceof CancellationException);
113 }
114 try {
115 f.getNow(null);
116 shouldThrow();
117 } catch (Throwable ex) {
118 assertTrue(ex instanceof CancellationException);
119 }
120 try {
121 f.get();
122 shouldThrow();
123 } catch (Throwable ex) {
124 assertTrue(ex instanceof CancellationException);
125 }
126 try {
127 f.get(0L, SECONDS);
128 shouldThrow();
129 } catch (Throwable ex) {
130 assertTrue(ex instanceof CancellationException);
131 }
132 assertTrue(f.isDone());
133 assertTrue(f.isCancelled());
134 }
135
136 void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
137 try {
138 f.join();
139 shouldThrow();
140 } catch (Throwable ex) {
141 assertTrue(ex instanceof CompletionException &&
142 ((CompletionException)ex).getCause() instanceof CancellationException);
143 }
144 try {
145 f.getNow(null);
146 shouldThrow();
147 } catch (Throwable ex) {
148 assertTrue(ex instanceof CompletionException &&
149 ((CompletionException)ex).getCause() instanceof CancellationException);
150 }
151 try {
152 f.get();
153 shouldThrow();
154 } catch (Throwable ex) {
155 assertTrue(ex instanceof ExecutionException &&
156 ((ExecutionException)ex).getCause() instanceof CancellationException);
157 }
158 try {
159 f.get(0L, SECONDS);
160 shouldThrow();
161 } catch (Throwable ex) {
162 assertTrue(ex instanceof ExecutionException &&
163 ((ExecutionException)ex).getCause() instanceof CancellationException);
164 }
165 assertTrue(f.isDone());
166 assertFalse(f.isCancelled());
167 }
168
169 /**
170 * A newly constructed CompletableFuture is incomplete, as indicated
171 * by methods isDone, isCancelled, and getNow
172 */
173 public void testConstructor() {
174 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
175 checkIncomplete(f);
176 }
177
178 /**
179 * complete completes normally, as indicated by methods isDone,
180 * isCancelled, join, get, and getNow
181 */
182 public void testComplete() {
183 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
184 checkIncomplete(f);
185 f.complete(one);
186 checkCompletedNormally(f, one);
187 }
188
189 /**
190 * completeExceptionally completes exceptionally, as indicated by
191 * methods isDone, isCancelled, join, get, and getNow
192 */
193 public void testCompleteExceptionally() {
194 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
195 checkIncomplete(f);
196 f.completeExceptionally(new CFException());
197 checkCompletedWithWrappedCFException(f);
198 }
199
200 /**
201 * cancel completes exceptionally and reports cancelled, as indicated by
202 * methods isDone, isCancelled, join, get, and getNow
203 */
204 public void testCancel() {
205 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
206 checkIncomplete(f);
207 assertTrue(f.cancel(true));
208 checkCancelled(f);
209 }
210
211 /**
212 * obtrudeValue forces completion with given value
213 */
214 public void testObtrudeValue() {
215 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
216 checkIncomplete(f);
217 f.complete(one);
218 checkCompletedNormally(f, one);
219 f.obtrudeValue(three);
220 checkCompletedNormally(f, three);
221 f.obtrudeValue(two);
222 checkCompletedNormally(f, two);
223 f = new CompletableFuture<Integer>();
224 f.obtrudeValue(three);
225 checkCompletedNormally(f, three);
226 f = new CompletableFuture<Integer>();
227 f.completeExceptionally(new CFException());
228 f.obtrudeValue(four);
229 checkCompletedNormally(f, four);
230 }
231
232 /**
233 * obtrudeException forces completion with given exception
234 */
235 public void testObtrudeException() {
236 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
237 checkIncomplete(f);
238 f.complete(one);
239 checkCompletedNormally(f, one);
240 f.obtrudeException(new CFException());
241 checkCompletedWithWrappedCFException(f);
242 f = new CompletableFuture<Integer>();
243 f.obtrudeException(new CFException());
244 checkCompletedWithWrappedCFException(f);
245 f = new CompletableFuture<Integer>();
246 f.completeExceptionally(new CFException());
247 f.obtrudeValue(four);
248 checkCompletedNormally(f, four);
249 f.obtrudeException(new CFException());
250 checkCompletedWithWrappedCFException(f);
251 }
252
253 /**
254 * getNumberOfDependents returns number of dependent tasks
255 */
256 public void testGetNumberOfDependents() {
257 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
258 assertEquals(f.getNumberOfDependents(), 0);
259 CompletableFuture g = f.thenRun(new Noop());
260 assertEquals(f.getNumberOfDependents(), 1);
261 assertEquals(g.getNumberOfDependents(), 0);
262 CompletableFuture h = f.thenRun(new Noop());
263 assertEquals(f.getNumberOfDependents(), 2);
264 f.complete(1);
265 checkCompletedNormally(g, null);
266 assertEquals(f.getNumberOfDependents(), 0);
267 assertEquals(g.getNumberOfDependents(), 0);
268 }
269
270
271 /**
272 * toString indicates current completion state
273 */
274 public void testToString() {
275 CompletableFuture<String> f;
276
277 f = new CompletableFuture<String>();
278 assertTrue(f.toString().contains("[Not completed]"));
279
280 f.complete("foo");
281 assertTrue(f.toString().contains("[Completed normally]"));
282
283 f = new CompletableFuture<String>();
284 f.completeExceptionally(new IndexOutOfBoundsException());
285 assertTrue(f.toString().contains("[Completed exceptionally]"));
286 }
287
288 static final Supplier<Integer> supplyOne =
289 () -> Integer.valueOf(1);
290 static final Function<Integer, Integer> inc =
291 (Integer x) -> Integer.valueOf(x.intValue() + 1);
292 static final BiFunction<Integer, Integer, Integer> add =
293 (Integer x, Integer y) -> Integer.valueOf(x.intValue() + y.intValue());
294 static final class IncAction implements Consumer<Integer> {
295 int value;
296 public void accept(Integer x) { value = x.intValue() + 1; }
297 }
298 static final class AddAction implements BiConsumer<Integer, Integer> {
299 int value;
300 public void accept(Integer x, Integer y) {
301 value = x.intValue() + y.intValue();
302 }
303 }
304 static final class Noop implements Runnable {
305 boolean ran;
306 public void run() { ran = true; }
307 }
308
309 static final class FailingSupplier implements Supplier<Integer> {
310 boolean ran;
311 public Integer get() { ran = true; throw new CFException(); }
312 }
313 static final class FailingConsumer implements Consumer<Integer> {
314 boolean ran;
315 public void accept(Integer x) { ran = true; throw new CFException(); }
316 }
317 static final class FailingBiConsumer implements BiConsumer<Integer, Integer> {
318 boolean ran;
319 public void accept(Integer x, Integer y) { ran = true; throw new CFException(); }
320 }
321 static final class FailingFunction implements Function<Integer, Integer> {
322 boolean ran;
323 public Integer apply(Integer x) { ran = true; throw new CFException(); }
324 }
325 static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> {
326 boolean ran;
327 public Integer apply(Integer x, Integer y) { ran = true; throw new CFException(); }
328 }
329 static final class FailingNoop implements Runnable {
330 boolean ran;
331 public void run() { ran = true; throw new CFException(); }
332 }
333
334 static final class CompletableFutureInc implements Function<Integer, CompletableFuture<Integer>> {
335 public CompletableFuture<Integer> apply(Integer x) {
336 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
337 f.complete(Integer.valueOf(x.intValue() + 1));
338 return f;
339 }
340 }
341
342 static final class FailingCompletableFutureFunction implements Function<Integer, CompletableFuture<Integer>> {
343 boolean ran;
344 public CompletableFuture<Integer> apply(Integer x) {
345 ran = true; throw new CFException();
346 }
347 }
348
349 // Used for explicit executor tests
350 static final class ThreadExecutor implements Executor {
351 public void execute(Runnable r) {
352 new Thread(r).start();
353 }
354 }
355
356 static final class ExceptionToInteger implements Function<Throwable, Integer> {
357 public Integer apply(Throwable x) { return Integer.valueOf(3); }
358 }
359
360 static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> {
361 public Integer apply(Integer x, Throwable t) {
362 return (t == null) ? two : three;
363 }
364 }
365
366
367 /**
368 * exceptionally action completes with function value on source
369 * exception; otherwise with source value
370 */
371 public void testExceptionally() {
372 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
373 ExceptionToInteger r = new ExceptionToInteger();
374 CompletableFuture<Integer> g = f.exceptionally(r);
375 f.completeExceptionally(new CFException());
376 checkCompletedNormally(g, three);
377
378 f = new CompletableFuture<Integer>();
379 r = new ExceptionToInteger();
380 g = f.exceptionally(r);
381 f.complete(one);
382 checkCompletedNormally(g, one);
383 }
384
385 /**
386 * handle action completes normally with function value on either
387 * normal or exceptional completion of source
388 */
389 public void testHandle() {
390 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
391 IntegerHandler r = new IntegerHandler();
392 CompletableFuture<Integer> g = f.handle(r);
393 f.completeExceptionally(new CFException());
394 checkCompletedNormally(g, three);
395
396 f = new CompletableFuture<Integer>();
397 r = new IntegerHandler();
398 g = f.handle(r);
399 f.complete(one);
400 checkCompletedNormally(g, two);
401 }
402
403 /**
404 * runAsync completes after running Runnable
405 */
406 public void testRunAsync() {
407 Noop r = new Noop();
408 CompletableFuture<Void> f = CompletableFuture.runAsync(r);
409 assertNull(f.join());
410 assertTrue(r.ran);
411 }
412
413 /**
414 * runAsync with executor completes after running Runnable
415 */
416 public void testRunAsync2() {
417 Noop r = new Noop();
418 CompletableFuture<Void> f = CompletableFuture.runAsync(r, new ThreadExecutor());
419 assertNull(f.join());
420 assertTrue(r.ran);
421 }
422
423 /**
424 * failing runAsync completes exceptionally after running Runnable
425 */
426 public void testRunAsync3() {
427 FailingNoop r = new FailingNoop();
428 CompletableFuture<Void> f = CompletableFuture.runAsync(r);
429 checkCompletedWithWrappedCFException(f);
430 assertTrue(r.ran);
431 }
432
433 /**
434 * supplyAsync completes with result of supplier
435 */
436 public void testSupplyAsync() {
437 CompletableFuture<Integer> f = CompletableFuture.supplyAsync(supplyOne);
438 assertEquals(f.join(), one);
439 }
440
441 /**
442 * supplyAsync with executor completes with result of supplier
443 */
444 public void testSupplyAsync2() {
445 CompletableFuture<Integer> f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor());
446 assertEquals(f.join(), one);
447 }
448
449 /**
450 * Failing supplyAsync completes exceptionally
451 */
452 public void testSupplyAsync3() {
453 FailingSupplier r = new FailingSupplier();
454 CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r);
455 checkCompletedWithWrappedCFException(f);
456 assertTrue(r.ran);
457 }
458
459 // seq conmpletion methods
460
461 /**
462 * thenRun result completes normally after normal completion of source
463 */
464 public void testThenRun() {
465 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
466 Noop r = new Noop();
467 CompletableFuture<Void> g = f.thenRun(r);
468 f.complete(null);
469 checkCompletedNormally(g, null);
470 // reordered version
471 f = new CompletableFuture<Integer>();
472 f.complete(null);
473 r = new Noop();
474 g = f.thenRun(r);
475 checkCompletedNormally(g, null);
476 }
477
478 /**
479 * thenRun result completes exceptionally after exceptional
480 * completion of source
481 */
482 public void testThenRun2() {
483 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
484 Noop r = new Noop();
485 CompletableFuture<Void> g = f.thenRun(r);
486 f.completeExceptionally(new CFException());
487 checkCompletedWithWrappedCFException(g);
488 }
489
490 /**
491 * thenRun result completes exceptionally if action does
492 */
493 public void testThenRun3() {
494 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
495 FailingNoop r = new FailingNoop();
496 CompletableFuture<Void> g = f.thenRun(r);
497 f.complete(null);
498 checkCompletedWithWrappedCFException(g);
499 }
500
501 /**
502 * thenRun result completes exceptionally if source cancelled
503 */
504 public void testThenRun4() {
505 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
506 Noop r = new Noop();
507 CompletableFuture<Void> g = f.thenRun(r);
508 assertTrue(f.cancel(true));
509 checkCompletedWithWrappedCancellationException(g);
510 }
511
512 /**
513 * thenApply result completes normally after normal completion of source
514 */
515 public void testThenApply() {
516 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
517 CompletableFuture<Integer> g = f.thenApply(inc);
518 f.complete(one);
519 checkCompletedNormally(g, two);
520 }
521
522 /**
523 * thenApply result completes exceptionally after exceptional
524 * completion of source
525 */
526 public void testThenApply2() {
527 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
528 CompletableFuture<Integer> g = f.thenApply(inc);
529 f.completeExceptionally(new CFException());
530 checkCompletedWithWrappedCFException(g);
531 }
532
533 /**
534 * thenApply result completes exceptionally if action does
535 */
536 public void testThenApply3() {
537 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
538 CompletableFuture<Integer> g = f.thenApply(new FailingFunction());
539 f.complete(one);
540 checkCompletedWithWrappedCFException(g);
541 }
542
543 /**
544 * thenApply result completes exceptionally if source cancelled
545 */
546 public void testThenApply4() {
547 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
548 CompletableFuture<Integer> g = f.thenApply(inc);
549 assertTrue(f.cancel(true));
550 checkCompletedWithWrappedCancellationException(g);
551 }
552
553 /**
554 * thenAccept result completes normally after normal completion of source
555 */
556 public void testThenAccept() {
557 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
558 IncAction r = new IncAction();
559 CompletableFuture<Void> g = f.thenAccept(r);
560 f.complete(one);
561 checkCompletedNormally(g, null);
562 assertEquals(r.value, 2);
563 }
564
565 /**
566 * thenAccept result completes exceptionally after exceptional
567 * completion of source
568 */
569 public void testThenAccept2() {
570 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
571 IncAction r = new IncAction();
572 CompletableFuture<Void> g = f.thenAccept(r);
573 f.completeExceptionally(new CFException());
574 checkCompletedWithWrappedCFException(g);
575 }
576
577 /**
578 * thenAccept result completes exceptionally if action does
579 */
580 public void testThenAccept3() {
581 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
582 FailingConsumer r = new FailingConsumer();
583 CompletableFuture<Void> g = f.thenAccept(r);
584 f.complete(one);
585 checkCompletedWithWrappedCFException(g);
586 assertTrue(r.ran);
587 }
588
589 /**
590 * thenAccept result completes exceptionally if source cancelled
591 */
592 public void testThenAccept4() {
593 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
594 IncAction r = new IncAction();
595 CompletableFuture<Void> g = f.thenAccept(r);
596 assertTrue(f.cancel(true));
597 checkCompletedWithWrappedCancellationException(g);
598 }
599
600
601 /**
602 * thenCombine result completes normally after normal completion of sources
603 */
604 public void testThenCombine() {
605 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
606 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
607 CompletableFuture<Integer> g = f.thenCombine(f2, add);
608 f.complete(one);
609 checkIncomplete(g);
610 f2.complete(two);
611 checkCompletedNormally(g, three);
612
613 f = new CompletableFuture<Integer>();
614 f.complete(one);
615 f2 = new CompletableFuture<Integer>();
616 g = f.thenCombine(f2, add);
617 checkIncomplete(g);
618 f2.complete(two);
619 checkCompletedNormally(g, three);
620 }
621
622 /**
623 * thenCombine result completes exceptionally after exceptional
624 * completion of either source
625 */
626 public void testThenCombine2() {
627 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
628 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
629 CompletableFuture<Integer> g = f.thenCombine(f2, add);
630 f.completeExceptionally(new CFException());
631 f2.complete(two);
632 checkCompletedWithWrappedCFException(g);
633
634 f = new CompletableFuture<Integer>();
635 f.complete(one);
636 f2 = new CompletableFuture<Integer>();
637 g = f.thenCombine(f2, add);
638 f2.completeExceptionally(new CFException());
639 checkCompletedWithWrappedCFException(g);
640 }
641
642 /**
643 * thenCombine result completes exceptionally if action does
644 */
645 public void testThenCombine3() {
646 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
647 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
648 FailingBiFunction r = new FailingBiFunction();
649 CompletableFuture<Integer> g = f.thenCombine(f2, r);
650 f.complete(one);
651 checkIncomplete(g);
652 f2.complete(two);
653 checkCompletedWithWrappedCFException(g);
654 }
655
656 /**
657 * thenCombine result completes exceptionally if either source cancelled
658 */
659 public void testThenCombine4() {
660 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
661 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
662 CompletableFuture<Integer> g = f.thenCombine(f2, add);
663 assertTrue(f.cancel(true));
664 f2.complete(two);
665 checkCompletedWithWrappedCancellationException(g);
666 f = new CompletableFuture<Integer>();
667 f2 = new CompletableFuture<Integer>();
668 g = f.thenCombine(f2, add);
669 f.complete(one);
670 assertTrue(f2.cancel(true));
671 checkCompletedWithWrappedCancellationException(g);
672 }
673
674 /**
675 * thenAcceptBoth result completes normally after normal
676 * completion of sources
677 */
678 public void testThenAcceptBoth() {
679 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
680 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
681 AddAction r = new AddAction();
682 CompletableFuture<Void> g = f.thenAcceptBoth(f2, r);
683 f.complete(one);
684 checkIncomplete(g);
685 f2.complete(two);
686 checkCompletedNormally(g, null);
687 assertEquals(r.value, 3);
688
689 r = new AddAction();
690 f = new CompletableFuture<Integer>();
691 f.complete(one);
692 f2 = new CompletableFuture<Integer>();
693 g = f.thenAcceptBoth(f2, r);
694 checkIncomplete(g);
695 f2.complete(two);
696 checkCompletedNormally(g, null);
697 assertEquals(r.value, 3);
698 }
699
700 /**
701 * thenAcceptBoth result completes exceptionally after exceptional
702 * completion of either source
703 */
704 public void testThenAcceptBoth2() {
705 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
706 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
707 AddAction r = new AddAction();
708 CompletableFuture<Void> g = f.thenAcceptBoth(f2, r);
709 f.completeExceptionally(new CFException());
710 f2.complete(two);
711 checkCompletedWithWrappedCFException(g);
712
713 r = new AddAction();
714 f = new CompletableFuture<Integer>();
715 f.complete(one);
716 f2 = new CompletableFuture<Integer>();
717 g = f.thenAcceptBoth(f2, r);
718 f2.completeExceptionally(new CFException());
719 checkCompletedWithWrappedCFException(g);
720 }
721
722 /**
723 * thenAcceptBoth result completes exceptionally if action does
724 */
725 public void testThenAcceptBoth3() {
726 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
727 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
728 FailingBiConsumer r = new FailingBiConsumer();
729 CompletableFuture<Void> g = f.thenAcceptBoth(f2, r);
730 f.complete(one);
731 checkIncomplete(g);
732 f2.complete(two);
733 checkCompletedWithWrappedCFException(g);
734 }
735
736 /**
737 * thenAcceptBoth result completes exceptionally if either source cancelled
738 */
739 public void testThenAcceptBoth4() {
740 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
741 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
742 AddAction r = new AddAction();
743 CompletableFuture<Void> g = f.thenAcceptBoth(f2, r);
744 assertTrue(f.cancel(true));
745 f2.complete(two);
746 checkCompletedWithWrappedCancellationException(g);
747 f = new CompletableFuture<Integer>();
748 f2 = new CompletableFuture<Integer>();
749 r = new AddAction();
750 g = f.thenAcceptBoth(f2, r);
751 f.complete(one);
752 assertTrue(f2.cancel(true));
753 checkCompletedWithWrappedCancellationException(g);
754 }
755
756 /**
757 * runAfterBoth result completes normally after normal
758 * completion of sources
759 */
760 public void testRunAfterBoth() {
761 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
762 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
763 Noop r = new Noop();
764 CompletableFuture<Void> g = f.runAfterBoth(f2, r);
765 f.complete(one);
766 checkIncomplete(g);
767 f2.complete(two);
768 checkCompletedNormally(g, null);
769 assertTrue(r.ran);
770
771 r = new Noop();
772 f = new CompletableFuture<Integer>();
773 f.complete(one);
774 f2 = new CompletableFuture<Integer>();
775 g = f.runAfterBoth(f2, r);
776 checkIncomplete(g);
777 f2.complete(two);
778 checkCompletedNormally(g, null);
779 assertTrue(r.ran);
780 }
781
782 /**
783 * runAfterBoth result completes exceptionally after exceptional
784 * completion of either source
785 */
786 public void testRunAfterBoth2() {
787 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
788 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
789 Noop r = new Noop();
790 CompletableFuture<Void> g = f.runAfterBoth(f2, r);
791 f.completeExceptionally(new CFException());
792 f2.complete(two);
793 checkCompletedWithWrappedCFException(g);
794
795 r = new Noop();
796 f = new CompletableFuture<Integer>();
797 f.complete(one);
798 f2 = new CompletableFuture<Integer>();
799 g = f.runAfterBoth(f2, r);
800 f2.completeExceptionally(new CFException());
801 checkCompletedWithWrappedCFException(g);
802 }
803
804 /**
805 * runAfterBoth result completes exceptionally if action does
806 */
807 public void testRunAfterBoth3() {
808 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
809 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
810 FailingNoop r = new FailingNoop();
811 CompletableFuture<Void> g = f.runAfterBoth(f2, r);
812 f.complete(one);
813 checkIncomplete(g);
814 f2.complete(two);
815 checkCompletedWithWrappedCFException(g);
816 }
817
818 /**
819 * runAfterBoth result completes exceptionally if either source cancelled
820 */
821 public void testRunAfterBoth4() {
822 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
823 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
824 Noop r = new Noop();
825 CompletableFuture<Void> g = f.runAfterBoth(f2, r);
826 assertTrue(f.cancel(true));
827 f2.complete(two);
828 checkCompletedWithWrappedCancellationException(g);
829 f = new CompletableFuture<Integer>();
830 f2 = new CompletableFuture<Integer>();
831 r = new Noop();
832 g = f.runAfterBoth(f2, r);
833 f.complete(one);
834 assertTrue(f2.cancel(true));
835 checkCompletedWithWrappedCancellationException(g);
836 }
837
838 /**
839 * applyToEither result completes normally after normal completion
840 * of either source
841 */
842 public void testApplyToEither() {
843 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
844 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
845 CompletableFuture<Integer> g = f.applyToEither(f2, inc);
846 f.complete(one);
847 checkCompletedNormally(g, two);
848 f2.complete(one);
849 checkCompletedNormally(g, two);
850
851 f = new CompletableFuture<Integer>();
852 f.complete(one);
853 f2 = new CompletableFuture<Integer>();
854 g = f.applyToEither(f2, inc);
855 checkCompletedNormally(g, two);
856 }
857
858 /**
859 * applyToEither result completes exceptionally after exceptional
860 * completion of either source
861 */
862 public void testApplyToEither2() {
863 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
864 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
865 CompletableFuture<Integer> g = f.applyToEither(f2, inc);
866 f.completeExceptionally(new CFException());
867 f2.complete(one);
868 checkCompletedWithWrappedCFException(g);
869
870 f = new CompletableFuture<Integer>();
871 f2 = new CompletableFuture<Integer>();
872 f2.completeExceptionally(new CFException());
873 g = f.applyToEither(f2, inc);
874 checkCompletedWithWrappedCFException(g);
875 }
876
877 /**
878 * applyToEither result completes exceptionally if action does
879 */
880 public void testApplyToEither3() {
881 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
882 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
883 FailingFunction r = new FailingFunction();
884 CompletableFuture<Integer> g = f.applyToEither(f2, r);
885 f2.complete(two);
886 checkCompletedWithWrappedCFException(g);
887 }
888
889 /**
890 * applyToEither result completes exceptionally if either source cancelled
891 */
892 public void testApplyToEither4() {
893 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
894 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
895 CompletableFuture<Integer> g = f.applyToEither(f2, inc);
896 assertTrue(f.cancel(true));
897 checkCompletedWithWrappedCancellationException(g);
898 f = new CompletableFuture<Integer>();
899 f2 = new CompletableFuture<Integer>();
900 assertTrue(f2.cancel(true));
901 checkCompletedWithWrappedCancellationException(g);
902 }
903
904 /**
905 * acceptEither result completes normally after normal completion
906 * of either source
907 */
908 public void testAcceptEither() {
909 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
910 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
911 IncAction r = new IncAction();
912 CompletableFuture<Void> g = f.acceptEither(f2, r);
913 f.complete(one);
914 checkCompletedNormally(g, null);
915 f2.complete(one);
916 checkCompletedNormally(g, null);
917 assertEquals(r.value, 2);
918
919 r = new IncAction();
920 f = new CompletableFuture<Integer>();
921 f.complete(one);
922 f2 = new CompletableFuture<Integer>();
923 g = f.acceptEither(f2, r);
924 checkCompletedNormally(g, null);
925 assertEquals(r.value, 2);
926 }
927
928 /**
929 * acceptEither result completes exceptionally after exceptional
930 * completion of either source
931 */
932 public void testAcceptEither2() {
933 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
934 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
935 IncAction r = new IncAction();
936 CompletableFuture<Void> g = f.acceptEither(f2, r);
937 f.completeExceptionally(new CFException());
938 f2.complete(one);
939 checkCompletedWithWrappedCFException(g);
940
941 r = new IncAction();
942 f = new CompletableFuture<Integer>();
943 f2 = new CompletableFuture<Integer>();
944 f2.completeExceptionally(new CFException());
945 g = f.acceptEither(f2, r);
946 checkCompletedWithWrappedCFException(g);
947 }
948
949 /**
950 * acceptEither result completes exceptionally if action does
951 */
952 public void testAcceptEither3() {
953 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
954 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
955 FailingConsumer r = new FailingConsumer();
956 CompletableFuture<Void> g = f.acceptEither(f2, r);
957 f2.complete(two);
958 checkCompletedWithWrappedCFException(g);
959 }
960
961 /**
962 * acceptEither result completes exceptionally if either source cancelled
963 */
964 public void testAcceptEither4() {
965 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
966 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
967 IncAction r = new IncAction();
968 CompletableFuture<Void> g = f.acceptEither(f2, r);
969 assertTrue(f.cancel(true));
970 checkCompletedWithWrappedCancellationException(g);
971 f = new CompletableFuture<Integer>();
972 f2 = new CompletableFuture<Integer>();
973 assertTrue(f2.cancel(true));
974 checkCompletedWithWrappedCancellationException(g);
975 }
976
977
978 /**
979 * runAfterEither result completes normally after normal completion
980 * of either source
981 */
982 public void testRunAfterEither() {
983 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
984 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
985 Noop r = new Noop();
986 CompletableFuture<Void> g = f.runAfterEither(f2, r);
987 f.complete(one);
988 checkCompletedNormally(g, null);
989 f2.complete(one);
990 checkCompletedNormally(g, null);
991 assertTrue(r.ran);
992
993 r = new Noop();
994 f = new CompletableFuture<Integer>();
995 f.complete(one);
996 f2 = new CompletableFuture<Integer>();
997 g = f.runAfterEither(f2, r);
998 checkCompletedNormally(g, null);
999 assertTrue(r.ran);
1000 }
1001
1002 /**
1003 * runAfterEither result completes exceptionally after exceptional
1004 * completion of either source
1005 */
1006 public void testRunAfterEither2() {
1007 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1008 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1009 Noop r = new Noop();
1010 CompletableFuture<Void> g = f.runAfterEither(f2, r);
1011 f.completeExceptionally(new CFException());
1012 f2.complete(one);
1013 checkCompletedWithWrappedCFException(g);
1014
1015 r = new Noop();
1016 f = new CompletableFuture<Integer>();
1017 f2 = new CompletableFuture<Integer>();
1018 f2.completeExceptionally(new CFException());
1019 g = f.runAfterEither(f2, r);
1020 checkCompletedWithWrappedCFException(g);
1021 }
1022
1023 /**
1024 * runAfterEither result completes exceptionally if action does
1025 */
1026 public void testRunAfterEither3() {
1027 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1028 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1029 FailingNoop r = new FailingNoop();
1030 CompletableFuture<Void> g = f.runAfterEither(f2, r);
1031 f2.complete(two);
1032 checkCompletedWithWrappedCFException(g);
1033 }
1034
1035 /**
1036 * runAfterEither result completes exceptionally if either source cancelled
1037 */
1038 public void testRunAfterEither4() {
1039 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1040 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1041 Noop r = new Noop();
1042 CompletableFuture<Void> g = f.runAfterEither(f2, r);
1043 assertTrue(f.cancel(true));
1044 checkCompletedWithWrappedCancellationException(g);
1045 f = new CompletableFuture<Integer>();
1046 f2 = new CompletableFuture<Integer>();
1047 assertTrue(f2.cancel(true));
1048 checkCompletedWithWrappedCancellationException(g);
1049 }
1050
1051 /**
1052 * thenCompose result completes normally after normal completion of source
1053 */
1054 public void testThenCompose() {
1055 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1056 CompletableFutureInc r = new CompletableFutureInc();
1057 CompletableFuture<Integer> g = f.thenCompose(r);
1058 f.complete(one);
1059 checkCompletedNormally(g, two);
1060 }
1061
1062 /**
1063 * thenCompose result completes exceptionally after exceptional
1064 * completion of source
1065 */
1066 public void testThenCompose2() {
1067 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1068 CompletableFutureInc r = new CompletableFutureInc();
1069 CompletableFuture<Integer> g = f.thenCompose(r);
1070 f.completeExceptionally(new CFException());
1071 checkCompletedWithWrappedCFException(g);
1072 }
1073
1074 /**
1075 * thenCompose result completes exceptionally if action does
1076 */
1077 public void testThenCompose3() {
1078 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1079 FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
1080 CompletableFuture<Integer> g = f.thenCompose(r);
1081 f.complete(one);
1082 checkCompletedWithWrappedCFException(g);
1083 }
1084
1085 /**
1086 * thenCompose result completes exceptionally if source cancelled
1087 */
1088 public void testThenCompose4() {
1089 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1090 CompletableFutureInc r = new CompletableFutureInc();
1091 CompletableFuture<Integer> g = f.thenCompose(r);
1092 assertTrue(f.cancel(true));
1093 checkCompletedWithWrappedCancellationException(g);
1094 }
1095
1096
1097 // asyncs
1098
1099 /**
1100 * thenRunAsync result completes normally after normal completion of source
1101 */
1102 public void testThenRunAsync() {
1103 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1104 Noop r = new Noop();
1105 CompletableFuture<Void> g = f.thenRunAsync(r);
1106 f.complete(null);
1107 checkCompletedNormally(g, null);
1108
1109 // reordered version
1110 f = new CompletableFuture<Integer>();
1111 f.complete(null);
1112 r = new Noop();
1113 g = f.thenRunAsync(r);
1114 checkCompletedNormally(g, null);
1115 }
1116
1117 /**
1118 * thenRunAsync result completes exceptionally after exceptional
1119 * completion of source
1120 */
1121 public void testThenRunAsync2() {
1122 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1123 Noop r = new Noop();
1124 CompletableFuture<Void> g = f.thenRunAsync(r);
1125 f.completeExceptionally(new CFException());
1126 try {
1127 g.join();
1128 shouldThrow();
1129 } catch(Exception ok) {
1130 }
1131 checkCompletedWithWrappedCFException(g);
1132 }
1133
1134 /**
1135 * thenRunAsync result completes exceptionally if action does
1136 */
1137 public void testThenRunAsync3() {
1138 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1139 FailingNoop r = new FailingNoop();
1140 CompletableFuture<Void> g = f.thenRunAsync(r);
1141 f.complete(null);
1142 checkCompletedWithWrappedCFException(g);
1143 }
1144
1145 /**
1146 * thenRunAsync result completes exceptionally if source cancelled
1147 */
1148 public void testThenRunAsync4() {
1149 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1150 Noop r = new Noop();
1151 CompletableFuture<Void> g = f.thenRunAsync(r);
1152 assertTrue(f.cancel(true));
1153 checkCompletedWithWrappedCancellationException(g);
1154 }
1155
1156 /**
1157 * thenApplyAsync result completes normally after normal completion of source
1158 */
1159 public void testThenApplyAsync() {
1160 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1161 CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1162 f.complete(one);
1163 checkCompletedNormally(g, two);
1164 }
1165
1166 /**
1167 * thenApplyAsync result completes exceptionally after exceptional
1168 * completion of source
1169 */
1170 public void testThenApplyAsync2() {
1171 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1172 CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1173 f.completeExceptionally(new CFException());
1174 checkCompletedWithWrappedCFException(g);
1175 }
1176
1177 /**
1178 * thenApplyAsync result completes exceptionally if action does
1179 */
1180 public void testThenApplyAsync3() {
1181 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1182 FailingFunction r = new FailingFunction();
1183 CompletableFuture<Integer> g = f.thenApplyAsync(r);
1184 f.complete(null);
1185 checkCompletedWithWrappedCFException(g);
1186 }
1187
1188 /**
1189 * thenApplyAsync result completes exceptionally if source cancelled
1190 */
1191 public void testThenApplyAsync4() {
1192 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1193 CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1194 assertTrue(f.cancel(true));
1195 checkCompletedWithWrappedCancellationException(g);
1196 }
1197
1198 /**
1199 * thenAcceptAsync result completes normally after normal
1200 * completion of source
1201 */
1202 public void testThenAcceptAsync() {
1203 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1204 IncAction r = new IncAction();
1205 CompletableFuture<Void> g = f.thenAcceptAsync(r);
1206 f.complete(one);
1207 checkCompletedNormally(g, null);
1208 assertEquals(r.value, 2);
1209 }
1210
1211 /**
1212 * thenAcceptAsync result completes exceptionally after exceptional
1213 * completion of source
1214 */
1215 public void testThenAcceptAsync2() {
1216 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1217 IncAction r = new IncAction();
1218 CompletableFuture<Void> g = f.thenAcceptAsync(r);
1219 f.completeExceptionally(new CFException());
1220 checkCompletedWithWrappedCFException(g);
1221 }
1222
1223 /**
1224 * thenAcceptAsync result completes exceptionally if action does
1225 */
1226 public void testThenAcceptAsync3() {
1227 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1228 FailingConsumer r = new FailingConsumer();
1229 CompletableFuture<Void> g = f.thenAcceptAsync(r);
1230 f.complete(null);
1231 checkCompletedWithWrappedCFException(g);
1232 }
1233
1234 /**
1235 * thenAcceptAsync result completes exceptionally if source cancelled
1236 */
1237 public void testThenAcceptAsync4() {
1238 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1239 IncAction r = new IncAction();
1240 CompletableFuture<Void> g = f.thenAcceptAsync(r);
1241 assertTrue(f.cancel(true));
1242 checkCompletedWithWrappedCancellationException(g);
1243 }
1244 /**
1245 * thenCombineAsync result completes normally after normal
1246 * completion of sources
1247 */
1248 public void testThenCombineAsync() {
1249 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1250 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1251 CompletableFuture<Integer> g = f.thenCombineAsync(f2, add);
1252 f.complete(one);
1253 checkIncomplete(g);
1254 f2.complete(two);
1255 checkCompletedNormally(g, three);
1256 }
1257
1258 /**
1259 * thenCombineAsync result completes exceptionally after exceptional
1260 * completion of source
1261 */
1262 public void testThenCombineAsync2() {
1263 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1264 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1265 CompletableFuture<Integer> g = f.thenCombineAsync(f2, add);
1266 f.completeExceptionally(new CFException());
1267 f2.complete(two);
1268 checkCompletedWithWrappedCFException(g);
1269
1270 f = new CompletableFuture<Integer>();
1271 f2 = new CompletableFuture<Integer>();
1272 g = f.thenCombineAsync(f2, add);
1273 f.complete(one);
1274 f2.completeExceptionally(new CFException());
1275 checkCompletedWithWrappedCFException(g);
1276 }
1277
1278 /**
1279 * thenCombineAsync result completes exceptionally if action does
1280 */
1281 public void testThenCombineAsync3() {
1282 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1283 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1284 FailingBiFunction r = new FailingBiFunction();
1285 CompletableFuture<Integer> g = f.thenCombineAsync(f2, r);
1286 f.complete(one);
1287 checkIncomplete(g);
1288 f2.complete(two);
1289 checkCompletedWithWrappedCFException(g);
1290 }
1291
1292 /**
1293 * thenCombineAsync result completes exceptionally if either source cancelled
1294 */
1295 public void testThenCombineAsync4() {
1296 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1297 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1298 CompletableFuture<Integer> g = f.thenCombineAsync(f2, add);
1299 assertTrue(f.cancel(true));
1300 f2.complete(two);
1301 checkCompletedWithWrappedCancellationException(g);
1302
1303 f = new CompletableFuture<Integer>();
1304 f2 = new CompletableFuture<Integer>();
1305 g = f.thenCombineAsync(f2, add);
1306 f.complete(one);
1307 assertTrue(f2.cancel(true));
1308 checkCompletedWithWrappedCancellationException(g);
1309 }
1310
1311 /**
1312 * thenAcceptBothAsync result completes normally after normal
1313 * completion of sources
1314 */
1315 public void testThenAcceptBothAsync() {
1316 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1317 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1318 AddAction r = new AddAction();
1319 CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r);
1320 f.complete(one);
1321 checkIncomplete(g);
1322 f2.complete(two);
1323 checkCompletedNormally(g, null);
1324 assertEquals(r.value, 3);
1325 }
1326
1327 /**
1328 * thenAcceptBothAsync result completes exceptionally after exceptional
1329 * completion of source
1330 */
1331 public void testThenAcceptBothAsync2() {
1332 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1333 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1334 AddAction r = new AddAction();
1335 CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r);
1336 f.completeExceptionally(new CFException());
1337 f2.complete(two);
1338 checkCompletedWithWrappedCFException(g);
1339
1340 r = new AddAction();
1341 f = new CompletableFuture<Integer>();
1342 f2 = new CompletableFuture<Integer>();
1343 g = f.thenAcceptBothAsync(f2, r);
1344 f.complete(one);
1345 f2.completeExceptionally(new CFException());
1346 checkCompletedWithWrappedCFException(g);
1347 }
1348
1349 /**
1350 * thenAcceptBothAsync result completes exceptionally if action does
1351 */
1352 public void testThenAcceptBothAsync3() {
1353 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1354 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1355 FailingBiConsumer r = new FailingBiConsumer();
1356 CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r);
1357 f.complete(one);
1358 checkIncomplete(g);
1359 f2.complete(two);
1360 checkCompletedWithWrappedCFException(g);
1361 }
1362
1363 /**
1364 * thenAcceptBothAsync result completes exceptionally if either source cancelled
1365 */
1366 public void testThenAcceptBothAsync4() {
1367 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1368 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1369 AddAction r = new AddAction();
1370 CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r);
1371 assertTrue(f.cancel(true));
1372 f2.complete(two);
1373 checkCompletedWithWrappedCancellationException(g);
1374
1375 r = new AddAction();
1376 f = new CompletableFuture<Integer>();
1377 f2 = new CompletableFuture<Integer>();
1378 g = f.thenAcceptBothAsync(f2, r);
1379 f.complete(one);
1380 assertTrue(f2.cancel(true));
1381 checkCompletedWithWrappedCancellationException(g);
1382 }
1383
1384 /**
1385 * runAfterBothAsync result completes normally after normal
1386 * completion of sources
1387 */
1388 public void testRunAfterBothAsync() {
1389 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1390 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1391 Noop r = new Noop();
1392 CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1393 f.complete(one);
1394 checkIncomplete(g);
1395 f2.complete(two);
1396 checkCompletedNormally(g, null);
1397 assertTrue(r.ran);
1398 }
1399
1400 /**
1401 * runAfterBothAsync result completes exceptionally after exceptional
1402 * completion of source
1403 */
1404 public void testRunAfterBothAsync2() {
1405 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1406 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1407 Noop r = new Noop();
1408 CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1409 f.completeExceptionally(new CFException());
1410 f2.complete(two);
1411 checkCompletedWithWrappedCFException(g);
1412
1413 r = new Noop();
1414 f = new CompletableFuture<Integer>();
1415 f2 = new CompletableFuture<Integer>();
1416 g = f.runAfterBothAsync(f2, r);
1417 f.complete(one);
1418 f2.completeExceptionally(new CFException());
1419 checkCompletedWithWrappedCFException(g);
1420 }
1421
1422 /**
1423 * runAfterBothAsync result completes exceptionally if action does
1424 */
1425 public void testRunAfterBothAsync3() {
1426 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1427 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1428 FailingNoop r = new FailingNoop();
1429 CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1430 f.complete(one);
1431 checkIncomplete(g);
1432 f2.complete(two);
1433 checkCompletedWithWrappedCFException(g);
1434 }
1435
1436 /**
1437 * runAfterBothAsync result completes exceptionally if either source cancelled
1438 */
1439 public void testRunAfterBothAsync4() {
1440 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1441 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1442 Noop r = new Noop();
1443 CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1444 assertTrue(f.cancel(true));
1445 f2.complete(two);
1446 checkCompletedWithWrappedCancellationException(g);
1447
1448 r = new Noop();
1449 f = new CompletableFuture<Integer>();
1450 f2 = new CompletableFuture<Integer>();
1451 g = f.runAfterBothAsync(f2, r);
1452 f.complete(one);
1453 assertTrue(f2.cancel(true));
1454 checkCompletedWithWrappedCancellationException(g);
1455 }
1456
1457 /**
1458 * applyToEitherAsync result completes normally after normal
1459 * completion of sources
1460 */
1461 public void testApplyToEitherAsync() {
1462 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1463 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1464 CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1465 f.complete(one);
1466 checkCompletedNormally(g, two);
1467
1468 f = new CompletableFuture<Integer>();
1469 f.complete(one);
1470 f2 = new CompletableFuture<Integer>();
1471 g = f.applyToEitherAsync(f2, inc);
1472 checkCompletedNormally(g, two);
1473 }
1474
1475 /**
1476 * applyToEitherAsync result completes exceptionally after exceptional
1477 * completion of source
1478 */
1479 public void testApplyToEitherAsync2() {
1480 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1481 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1482 CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1483 f.completeExceptionally(new CFException());
1484 checkCompletedWithWrappedCFException(g);
1485
1486 f = new CompletableFuture<Integer>();
1487 f2 = new CompletableFuture<Integer>();
1488 f2.completeExceptionally(new CFException());
1489 g = f.applyToEitherAsync(f2, inc);
1490 f.complete(one);
1491 checkCompletedWithWrappedCFException(g);
1492 }
1493
1494 /**
1495 * applyToEitherAsync result completes exceptionally if action does
1496 */
1497 public void testApplyToEitherAsync3() {
1498 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1499 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1500 FailingFunction r = new FailingFunction();
1501 CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r);
1502 f.complete(one);
1503 checkCompletedWithWrappedCFException(g);
1504 }
1505
1506 /**
1507 * applyToEitherAsync result completes exceptionally if either source cancelled
1508 */
1509 public void testApplyToEitherAsync4() {
1510 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1511 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1512 CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1513 assertTrue(f.cancel(true));
1514 checkCompletedWithWrappedCancellationException(g);
1515
1516 f = new CompletableFuture<Integer>();
1517 f2 = new CompletableFuture<Integer>();
1518 assertTrue(f2.cancel(true));
1519 g = f.applyToEitherAsync(f2, inc);
1520 checkCompletedWithWrappedCancellationException(g);
1521 }
1522
1523 /**
1524 * acceptEitherAsync result completes normally after normal
1525 * completion of sources
1526 */
1527 public void testAcceptEitherAsync() {
1528 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1529 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1530 IncAction r = new IncAction();
1531 CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1532 f.complete(one);
1533 checkCompletedNormally(g, null);
1534 assertEquals(r.value, 2);
1535
1536 r = new IncAction();
1537 f = new CompletableFuture<Integer>();
1538 f.complete(one);
1539 f2 = new CompletableFuture<Integer>();
1540 g = f.acceptEitherAsync(f2, r);
1541 checkCompletedNormally(g, null);
1542 assertEquals(r.value, 2);
1543 }
1544
1545 /**
1546 * acceptEitherAsync result completes exceptionally after exceptional
1547 * completion of source
1548 */
1549 public void testAcceptEitherAsync2() {
1550 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1551 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1552 IncAction r = new IncAction();
1553 CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1554 f.completeExceptionally(new CFException());
1555 checkCompletedWithWrappedCFException(g);
1556
1557 r = new IncAction();
1558 f = new CompletableFuture<Integer>();
1559 f2 = new CompletableFuture<Integer>();
1560 f2.completeExceptionally(new CFException());
1561 g = f.acceptEitherAsync(f2, r);
1562 f.complete(one);
1563 checkCompletedWithWrappedCFException(g);
1564 }
1565
1566 /**
1567 * acceptEitherAsync result completes exceptionally if action does
1568 */
1569 public void testAcceptEitherAsync3() {
1570 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1571 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1572 FailingConsumer r = new FailingConsumer();
1573 CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1574 f.complete(one);
1575 checkCompletedWithWrappedCFException(g);
1576 }
1577
1578 /**
1579 * acceptEitherAsync result completes exceptionally if either
1580 * source cancelled
1581 */
1582 public void testAcceptEitherAsync4() {
1583 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1584 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1585 IncAction r = new IncAction();
1586 CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1587 assertTrue(f.cancel(true));
1588 checkCompletedWithWrappedCancellationException(g);
1589
1590 r = new IncAction();
1591 f = new CompletableFuture<Integer>();
1592 f2 = new CompletableFuture<Integer>();
1593 assertTrue(f2.cancel(true));
1594 g = f.acceptEitherAsync(f2, r);
1595 checkCompletedWithWrappedCancellationException(g);
1596 }
1597
1598 /**
1599 * runAfterEitherAsync result completes normally after normal
1600 * completion of sources
1601 */
1602 public void testRunAfterEitherAsync() {
1603 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1604 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1605 Noop r = new Noop();
1606 CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1607 f.complete(one);
1608 checkCompletedNormally(g, null);
1609 assertTrue(r.ran);
1610
1611 r = new Noop();
1612 f = new CompletableFuture<Integer>();
1613 f.complete(one);
1614 f2 = new CompletableFuture<Integer>();
1615 g = f.runAfterEitherAsync(f2, r);
1616 checkCompletedNormally(g, null);
1617 assertTrue(r.ran);
1618 }
1619
1620 /**
1621 * runAfterEitherAsync result completes exceptionally after exceptional
1622 * completion of source
1623 */
1624 public void testRunAfterEitherAsync2() {
1625 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1626 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1627 Noop r = new Noop();
1628 CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1629 f.completeExceptionally(new CFException());
1630 checkCompletedWithWrappedCFException(g);
1631
1632 r = new Noop();
1633 f = new CompletableFuture<Integer>();
1634 f2 = new CompletableFuture<Integer>();
1635 f2.completeExceptionally(new CFException());
1636 g = f.runAfterEitherAsync(f2, r);
1637 f.complete(one);
1638 checkCompletedWithWrappedCFException(g);
1639 }
1640
1641 /**
1642 * runAfterEitherAsync result completes exceptionally if action does
1643 */
1644 public void testRunAfterEitherAsync3() {
1645 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1646 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1647 FailingNoop r = new FailingNoop();
1648 CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1649 f.complete(one);
1650 checkCompletedWithWrappedCFException(g);
1651 }
1652
1653 /**
1654 * runAfterEitherAsync result completes exceptionally if either
1655 * source cancelled
1656 */
1657 public void testRunAfterEitherAsync4() {
1658 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1659 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1660 Noop r = new Noop();
1661 CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1662 assertTrue(f.cancel(true));
1663 checkCompletedWithWrappedCancellationException(g);
1664
1665 r = new Noop();
1666 f = new CompletableFuture<Integer>();
1667 f2 = new CompletableFuture<Integer>();
1668 assertTrue(f2.cancel(true));
1669 g = f.runAfterEitherAsync(f2, r);
1670 checkCompletedWithWrappedCancellationException(g);
1671 }
1672
1673 /**
1674 * thenCompse result completes normally after normal 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 exceptional
1686 * 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 // aaync 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 * thenCompse result completes normally after normal completion of source
2297 */
2298 public void testThenComposeAsyncE() {
2299 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2300 CompletableFutureInc r = new CompletableFutureInc();
2301 CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2302 f.complete(one);
2303 checkCompletedNormally(g, two);
2304 }
2305
2306 /**
2307 * thenComposeAsync result completes exceptionally after exceptional
2308 * completion of source
2309 */
2310 public void testThenComposeAsync2E() {
2311 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2312 CompletableFutureInc r = new CompletableFutureInc();
2313 CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2314 f.completeExceptionally(new CFException());
2315 checkCompletedWithWrappedCFException(g);
2316 }
2317
2318 /**
2319 * thenComposeAsync result completes exceptionally if action does
2320 */
2321 public void testThenComposeAsync3E() {
2322 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2323 FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
2324 CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2325 f.complete(one);
2326 checkCompletedWithWrappedCFException(g);
2327 }
2328
2329 /**
2330 * thenComposeAsync result completes exceptionally if source cancelled
2331 */
2332 public void testThenComposeAsync4E() {
2333 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2334 CompletableFutureInc r = new CompletableFutureInc();
2335 CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2336 assertTrue(f.cancel(true));
2337 checkCompletedWithWrappedCancellationException(g);
2338 }
2339
2340 // other static methods
2341
2342 /**
2343 * allOf(no component futures) returns a future completed normally
2344 * with the value null
2345 */
2346 public void testAllOf_empty() throws Exception {
2347 CompletableFuture<?> f = CompletableFuture.allOf();
2348 checkCompletedNormally(f, null);
2349 }
2350
2351 /**
2352 * allOf returns a future completed when all components complete
2353 */
2354 public void testAllOf() throws Exception {
2355 for (int k = 1; k < 20; ++k) {
2356 CompletableFuture[] fs = new CompletableFuture[k];
2357 for (int i = 0; i < k; ++i)
2358 fs[i] = new CompletableFuture<Integer>();
2359 CompletableFuture<?> f = CompletableFuture.allOf(fs);
2360 for (int i = 0; i < k; ++i) {
2361 checkIncomplete(f);
2362 fs[i].complete(one);
2363 }
2364 assertTrue(f.isDone());
2365 assertFalse(f.isCancelled());
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 * allOf 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<?> f = CompletableFuture.anyOf(fs);
2386 checkIncomplete(f);
2387 for (int i = 0; i < k; ++i) {
2388 fs[i].complete(one);
2389 assertTrue(f.isDone());
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 h;
2401 try { h = f.thenApply(null); } catch (NullPointerException ok) {}
2402 try { h = f.thenAccept(null); } catch (NullPointerException ok) {}
2403 try { h = f.thenRun(null); } catch (NullPointerException ok) {}
2404 try { h = f.thenCombine(g, null); } catch (NullPointerException ok) {}
2405 try { h = f.thenCombine(null, null); } catch (NullPointerException ok) {}
2406 try { h = f.applyToEither(g, null); } catch (NullPointerException ok) {}
2407 try { h = f.applyToEither(null, null); } catch (NullPointerException ok) {}
2408 try { h = f.thenAcceptBoth(g, null); } catch (NullPointerException ok) {}
2409 try { h = f.thenAcceptBoth(null, null); } catch (NullPointerException ok) {}
2410 try { h = f.runAfterEither(g, null); } catch (NullPointerException ok) {}
2411 try { h = f.runAfterEither(null, null); } catch (NullPointerException ok) {}
2412 try { h = f.runAfterBoth(g, null); } catch (NullPointerException ok) {}
2413 try { h = f.runAfterBoth(null, null); } catch (NullPointerException ok) {}
2414 try { h = f.exceptionally(null); } catch (NullPointerException ok) {}
2415 try { h = f.handle(null); } catch (NullPointerException ok) {}
2416 try { h = f.thenCompose(null); } catch (NullPointerException ok) {}
2417
2418 try { h = f.thenApplyAsync(null); } catch (NullPointerException ok) {}
2419 try { h = f.thenAcceptAsync(null); } catch (NullPointerException ok) {}
2420 try { h = f.thenRunAsync(null); } catch (NullPointerException ok) {}
2421 try { h = f.thenCombineAsync(g, null); } catch (NullPointerException ok) {}
2422 try { h = f.thenCombineAsync(null, null); } catch (NullPointerException ok) {}
2423 try { h = f.applyToEitherAsync(g, null); } catch (NullPointerException ok) {}
2424 try { h = f.applyToEitherAsync(null, null); } catch (NullPointerException ok) {}
2425 try { h = f.thenAcceptBothAsync(g, null); } catch (NullPointerException ok) {}
2426 try { h = f.thenAcceptBothAsync(null, null); } catch (NullPointerException ok) {}
2427 try { h = f.runAfterEitherAsync(g, null); } catch (NullPointerException ok) {}
2428 try { h = f.runAfterEitherAsync(null, null); } catch (NullPointerException ok) {}
2429 try { h = f.runAfterBothAsync(g, null); } catch (NullPointerException ok) {}
2430 try { h = f.runAfterBothAsync(null, null); } catch (NullPointerException ok) {}
2431
2432 }
2433
2434
2435 }