ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
Revision: 1.7
Committed: Fri Mar 22 16:29:42 2013 UTC (11 years, 1 month ago) by jsr166
Branch: MAIN
Changes since 1.6: +10 -8 lines
Log Message:
typos

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 completion 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 * thenComposeAsync result completes normally after normal
1675 * completion of source
1676 */
1677 public void testThenComposeAsync() {
1678 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1679 CompletableFutureInc r = new CompletableFutureInc();
1680 CompletableFuture<Integer> g = f.thenComposeAsync(r);
1681 f.complete(one);
1682 checkCompletedNormally(g, two);
1683 }
1684
1685 /**
1686 * thenComposeAsync result completes exceptionally after
1687 * exceptional completion of source
1688 */
1689 public void testThenComposeAsync2() {
1690 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1691 CompletableFutureInc r = new CompletableFutureInc();
1692 CompletableFuture<Integer> g = f.thenComposeAsync(r);
1693 f.completeExceptionally(new CFException());
1694 checkCompletedWithWrappedCFException(g);
1695 }
1696
1697 /**
1698 * thenComposeAsync result completes exceptionally if action does
1699 */
1700 public void testThenComposeAsync3() {
1701 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1702 FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
1703 CompletableFuture<Integer> g = f.thenComposeAsync(r);
1704 f.complete(one);
1705 checkCompletedWithWrappedCFException(g);
1706 }
1707
1708 /**
1709 * thenComposeAsync result completes exceptionally if source cancelled
1710 */
1711 public void testThenComposeAsync4() {
1712 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1713 CompletableFutureInc r = new CompletableFutureInc();
1714 CompletableFuture<Integer> g = f.thenComposeAsync(r);
1715 assertTrue(f.cancel(true));
1716 checkCompletedWithWrappedCancellationException(g);
1717 }
1718
1719
1720 // async with explicit executors
1721
1722 /**
1723 * thenRunAsync result completes normally after normal completion of source
1724 */
1725 public void testThenRunAsyncE() {
1726 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1727 Noop r = new Noop();
1728 CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
1729 f.complete(null);
1730 checkCompletedNormally(g, null);
1731
1732 // reordered version
1733 f = new CompletableFuture<Integer>();
1734 f.complete(null);
1735 r = new Noop();
1736 g = f.thenRunAsync(r, new ThreadExecutor());
1737 checkCompletedNormally(g, null);
1738 }
1739
1740 /**
1741 * thenRunAsync result completes exceptionally after exceptional
1742 * completion of source
1743 */
1744 public void testThenRunAsync2E() {
1745 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1746 Noop r = new Noop();
1747 CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
1748 f.completeExceptionally(new CFException());
1749 try {
1750 g.join();
1751 shouldThrow();
1752 } catch (Exception ok) {
1753 }
1754 checkCompletedWithWrappedCFException(g);
1755 }
1756
1757 /**
1758 * thenRunAsync result completes exceptionally if action does
1759 */
1760 public void testThenRunAsync3E() {
1761 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1762 FailingNoop r = new FailingNoop();
1763 CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
1764 f.complete(null);
1765 checkCompletedWithWrappedCFException(g);
1766 }
1767
1768 /**
1769 * thenRunAsync result completes exceptionally if source cancelled
1770 */
1771 public void testThenRunAsync4E() {
1772 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1773 Noop r = new Noop();
1774 CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
1775 assertTrue(f.cancel(true));
1776 checkCompletedWithWrappedCancellationException(g);
1777 }
1778
1779 /**
1780 * thenApplyAsync result completes normally after normal completion of source
1781 */
1782 public void testThenApplyAsyncE() {
1783 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1784 CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
1785 f.complete(one);
1786 checkCompletedNormally(g, two);
1787 }
1788
1789 /**
1790 * thenApplyAsync result completes exceptionally after exceptional
1791 * completion of source
1792 */
1793 public void testThenApplyAsync2E() {
1794 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1795 CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
1796 f.completeExceptionally(new CFException());
1797 checkCompletedWithWrappedCFException(g);
1798 }
1799
1800 /**
1801 * thenApplyAsync result completes exceptionally if action does
1802 */
1803 public void testThenApplyAsync3E() {
1804 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1805 FailingFunction r = new FailingFunction();
1806 CompletableFuture<Integer> g = f.thenApplyAsync(r, new ThreadExecutor());
1807 f.complete(null);
1808 checkCompletedWithWrappedCFException(g);
1809 }
1810
1811 /**
1812 * thenApplyAsync result completes exceptionally if source cancelled
1813 */
1814 public void testThenApplyAsync4E() {
1815 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1816 CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
1817 assertTrue(f.cancel(true));
1818 checkCompletedWithWrappedCancellationException(g);
1819 }
1820
1821 /**
1822 * thenAcceptAsync result completes normally after normal
1823 * completion of source
1824 */
1825 public void testThenAcceptAsyncE() {
1826 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1827 IncAction r = new IncAction();
1828 CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
1829 f.complete(one);
1830 checkCompletedNormally(g, null);
1831 assertEquals(r.value, 2);
1832 }
1833
1834 /**
1835 * thenAcceptAsync result completes exceptionally after exceptional
1836 * completion of source
1837 */
1838 public void testThenAcceptAsync2E() {
1839 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1840 IncAction r = new IncAction();
1841 CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
1842 f.completeExceptionally(new CFException());
1843 checkCompletedWithWrappedCFException(g);
1844 }
1845
1846 /**
1847 * thenAcceptAsync result completes exceptionally if action does
1848 */
1849 public void testThenAcceptAsync3E() {
1850 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1851 FailingConsumer r = new FailingConsumer();
1852 CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
1853 f.complete(null);
1854 checkCompletedWithWrappedCFException(g);
1855 }
1856
1857 /**
1858 * thenAcceptAsync result completes exceptionally if source cancelled
1859 */
1860 public void testThenAcceptAsync4E() {
1861 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1862 IncAction r = new IncAction();
1863 CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
1864 assertTrue(f.cancel(true));
1865 checkCompletedWithWrappedCancellationException(g);
1866 }
1867 /**
1868 * thenCombineAsync result completes normally after normal
1869 * completion of sources
1870 */
1871 public void testThenCombineAsyncE() {
1872 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1873 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1874 CompletableFuture<Integer> g = f.thenCombineAsync(f2, add, new ThreadExecutor());
1875 f.complete(one);
1876 checkIncomplete(g);
1877 f2.complete(two);
1878 checkCompletedNormally(g, three);
1879 }
1880
1881 /**
1882 * thenCombineAsync result completes exceptionally after exceptional
1883 * completion of source
1884 */
1885 public void testThenCombineAsync2E() {
1886 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1887 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1888 CompletableFuture<Integer> g = f.thenCombineAsync(f2, add, new ThreadExecutor());
1889 f.completeExceptionally(new CFException());
1890 f2.complete(two);
1891 checkCompletedWithWrappedCFException(g);
1892
1893 f = new CompletableFuture<Integer>();
1894 f2 = new CompletableFuture<Integer>();
1895 g = f.thenCombineAsync(f2, add, new ThreadExecutor());
1896 f.complete(one);
1897 f2.completeExceptionally(new CFException());
1898 checkCompletedWithWrappedCFException(g);
1899 }
1900
1901 /**
1902 * thenCombineAsync result completes exceptionally if action does
1903 */
1904 public void testThenCombineAsync3E() {
1905 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1906 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1907 FailingBiFunction r = new FailingBiFunction();
1908 CompletableFuture<Integer> g = f.thenCombineAsync(f2, r, new ThreadExecutor());
1909 f.complete(one);
1910 checkIncomplete(g);
1911 f2.complete(two);
1912 checkCompletedWithWrappedCFException(g);
1913 }
1914
1915 /**
1916 * thenCombineAsync result completes exceptionally if either source cancelled
1917 */
1918 public void testThenCombineAsync4E() {
1919 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1920 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1921 CompletableFuture<Integer> g = f.thenCombineAsync(f2, add, new ThreadExecutor());
1922 assertTrue(f.cancel(true));
1923 f2.complete(two);
1924 checkCompletedWithWrappedCancellationException(g);
1925
1926 f = new CompletableFuture<Integer>();
1927 f2 = new CompletableFuture<Integer>();
1928 g = f.thenCombineAsync(f2, add, new ThreadExecutor());
1929 f.complete(one);
1930 assertTrue(f2.cancel(true));
1931 checkCompletedWithWrappedCancellationException(g);
1932 }
1933
1934 /**
1935 * thenAcceptBothAsync result completes normally after normal
1936 * completion of sources
1937 */
1938 public void testThenAcceptBothAsyncE() {
1939 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1940 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1941 AddAction r = new AddAction();
1942 CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
1943 f.complete(one);
1944 checkIncomplete(g);
1945 f2.complete(two);
1946 checkCompletedNormally(g, null);
1947 assertEquals(r.value, 3);
1948 }
1949
1950 /**
1951 * thenAcceptBothAsync result completes exceptionally after exceptional
1952 * completion of source
1953 */
1954 public void testThenAcceptBothAsync2E() {
1955 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1956 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1957 AddAction r = new AddAction();
1958 CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
1959 f.completeExceptionally(new CFException());
1960 f2.complete(two);
1961 checkCompletedWithWrappedCFException(g);
1962
1963 r = new AddAction();
1964 f = new CompletableFuture<Integer>();
1965 f2 = new CompletableFuture<Integer>();
1966 g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
1967 f.complete(one);
1968 f2.completeExceptionally(new CFException());
1969 checkCompletedWithWrappedCFException(g);
1970 }
1971
1972 /**
1973 * thenAcceptBothAsync result completes exceptionally if action does
1974 */
1975 public void testThenAcceptBothAsync3E() {
1976 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1977 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1978 FailingBiConsumer r = new FailingBiConsumer();
1979 CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
1980 f.complete(one);
1981 checkIncomplete(g);
1982 f2.complete(two);
1983 checkCompletedWithWrappedCFException(g);
1984 }
1985
1986 /**
1987 * thenAcceptBothAsync result completes exceptionally if either source cancelled
1988 */
1989 public void testThenAcceptBothAsync4E() {
1990 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1991 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1992 AddAction r = new AddAction();
1993 CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
1994 assertTrue(f.cancel(true));
1995 f2.complete(two);
1996 checkCompletedWithWrappedCancellationException(g);
1997
1998 r = new AddAction();
1999 f = new CompletableFuture<Integer>();
2000 f2 = new CompletableFuture<Integer>();
2001 g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
2002 f.complete(one);
2003 assertTrue(f2.cancel(true));
2004 checkCompletedWithWrappedCancellationException(g);
2005 }
2006
2007 /**
2008 * runAfterBothAsync result completes normally after normal
2009 * completion of sources
2010 */
2011 public void testRunAfterBothAsyncE() {
2012 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2013 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2014 Noop r = new Noop();
2015 CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2016 f.complete(one);
2017 checkIncomplete(g);
2018 f2.complete(two);
2019 checkCompletedNormally(g, null);
2020 assertTrue(r.ran);
2021 }
2022
2023 /**
2024 * runAfterBothAsync result completes exceptionally after exceptional
2025 * completion of source
2026 */
2027 public void testRunAfterBothAsync2E() {
2028 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2029 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2030 Noop r = new Noop();
2031 CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2032 f.completeExceptionally(new CFException());
2033 f2.complete(two);
2034 checkCompletedWithWrappedCFException(g);
2035
2036 r = new Noop();
2037 f = new CompletableFuture<Integer>();
2038 f2 = new CompletableFuture<Integer>();
2039 g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2040 f.complete(one);
2041 f2.completeExceptionally(new CFException());
2042 checkCompletedWithWrappedCFException(g);
2043 }
2044
2045 /**
2046 * runAfterBothAsync result completes exceptionally if action does
2047 */
2048 public void testRunAfterBothAsync3E() {
2049 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2050 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2051 FailingNoop r = new FailingNoop();
2052 CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2053 f.complete(one);
2054 checkIncomplete(g);
2055 f2.complete(two);
2056 checkCompletedWithWrappedCFException(g);
2057 }
2058
2059 /**
2060 * runAfterBothAsync result completes exceptionally if either source cancelled
2061 */
2062 public void testRunAfterBothAsync4E() {
2063 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2064 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2065 Noop r = new Noop();
2066 CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2067 assertTrue(f.cancel(true));
2068 f2.complete(two);
2069 checkCompletedWithWrappedCancellationException(g);
2070
2071 r = new Noop();
2072 f = new CompletableFuture<Integer>();
2073 f2 = new CompletableFuture<Integer>();
2074 g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2075 f.complete(one);
2076 assertTrue(f2.cancel(true));
2077 checkCompletedWithWrappedCancellationException(g);
2078 }
2079
2080 /**
2081 * applyToEitherAsync result completes normally after normal
2082 * completion of sources
2083 */
2084 public void testApplyToEitherAsyncE() {
2085 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2086 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2087 CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2088 f.complete(one);
2089 checkCompletedNormally(g, two);
2090
2091 f = new CompletableFuture<Integer>();
2092 f.complete(one);
2093 f2 = new CompletableFuture<Integer>();
2094 g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2095 checkCompletedNormally(g, two);
2096 }
2097
2098 /**
2099 * applyToEitherAsync result completes exceptionally after exceptional
2100 * completion of source
2101 */
2102 public void testApplyToEitherAsync2E() {
2103 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2104 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2105 CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2106 f.completeExceptionally(new CFException());
2107 checkCompletedWithWrappedCFException(g);
2108
2109 f = new CompletableFuture<Integer>();
2110 f2 = new CompletableFuture<Integer>();
2111 f2.completeExceptionally(new CFException());
2112 g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2113 f.complete(one);
2114 checkCompletedWithWrappedCFException(g);
2115 }
2116
2117 /**
2118 * applyToEitherAsync result completes exceptionally if action does
2119 */
2120 public void testApplyToEitherAsync3E() {
2121 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2122 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2123 FailingFunction r = new FailingFunction();
2124 CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor());
2125 f.complete(one);
2126 checkCompletedWithWrappedCFException(g);
2127 }
2128
2129 /**
2130 * applyToEitherAsync result completes exceptionally if either source cancelled
2131 */
2132 public void testApplyToEitherAsync4E() {
2133 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2134 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2135 CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2136 assertTrue(f.cancel(true));
2137 checkCompletedWithWrappedCancellationException(g);
2138
2139 f = new CompletableFuture<Integer>();
2140 f2 = new CompletableFuture<Integer>();
2141 assertTrue(f2.cancel(true));
2142 g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2143 checkCompletedWithWrappedCancellationException(g);
2144 }
2145
2146 /**
2147 * acceptEitherAsync result completes normally after normal
2148 * completion of sources
2149 */
2150 public void testAcceptEitherAsyncE() {
2151 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2152 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2153 IncAction r = new IncAction();
2154 CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2155 f.complete(one);
2156 checkCompletedNormally(g, null);
2157 assertEquals(r.value, 2);
2158
2159 r = new IncAction();
2160 f = new CompletableFuture<Integer>();
2161 f.complete(one);
2162 f2 = new CompletableFuture<Integer>();
2163 g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2164 checkCompletedNormally(g, null);
2165 assertEquals(r.value, 2);
2166 }
2167
2168 /**
2169 * acceptEitherAsync result completes exceptionally after exceptional
2170 * completion of source
2171 */
2172 public void testAcceptEitherAsync2E() {
2173 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2174 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2175 IncAction r = new IncAction();
2176 CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2177 f.completeExceptionally(new CFException());
2178 checkCompletedWithWrappedCFException(g);
2179
2180 r = new IncAction();
2181 f = new CompletableFuture<Integer>();
2182 f2 = new CompletableFuture<Integer>();
2183 f2.completeExceptionally(new CFException());
2184 g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2185 f.complete(one);
2186 checkCompletedWithWrappedCFException(g);
2187 }
2188
2189 /**
2190 * acceptEitherAsync result completes exceptionally if action does
2191 */
2192 public void testAcceptEitherAsync3E() {
2193 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2194 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2195 FailingConsumer r = new FailingConsumer();
2196 CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2197 f.complete(one);
2198 checkCompletedWithWrappedCFException(g);
2199 }
2200
2201 /**
2202 * acceptEitherAsync result completes exceptionally if either
2203 * source cancelled
2204 */
2205 public void testAcceptEitherAsync4E() {
2206 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2207 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2208 IncAction r = new IncAction();
2209 CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2210 assertTrue(f.cancel(true));
2211 checkCompletedWithWrappedCancellationException(g);
2212
2213 r = new IncAction();
2214 f = new CompletableFuture<Integer>();
2215 f2 = new CompletableFuture<Integer>();
2216 assertTrue(f2.cancel(true));
2217 g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2218 checkCompletedWithWrappedCancellationException(g);
2219 }
2220
2221 /**
2222 * runAfterEitherAsync result completes normally after normal
2223 * completion of sources
2224 */
2225 public void testRunAfterEitherAsyncE() {
2226 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2227 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2228 Noop r = new Noop();
2229 CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2230 f.complete(one);
2231 checkCompletedNormally(g, null);
2232 assertTrue(r.ran);
2233
2234 r = new Noop();
2235 f = new CompletableFuture<Integer>();
2236 f.complete(one);
2237 f2 = new CompletableFuture<Integer>();
2238 g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2239 checkCompletedNormally(g, null);
2240 assertTrue(r.ran);
2241 }
2242
2243 /**
2244 * runAfterEitherAsync result completes exceptionally after exceptional
2245 * completion of source
2246 */
2247 public void testRunAfterEitherAsync2E() {
2248 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2249 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2250 Noop r = new Noop();
2251 CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2252 f.completeExceptionally(new CFException());
2253 checkCompletedWithWrappedCFException(g);
2254
2255 r = new Noop();
2256 f = new CompletableFuture<Integer>();
2257 f2 = new CompletableFuture<Integer>();
2258 f2.completeExceptionally(new CFException());
2259 g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2260 f.complete(one);
2261 checkCompletedWithWrappedCFException(g);
2262 }
2263
2264 /**
2265 * runAfterEitherAsync result completes exceptionally if action does
2266 */
2267 public void testRunAfterEitherAsync3E() {
2268 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2269 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2270 FailingNoop r = new FailingNoop();
2271 CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2272 f.complete(one);
2273 checkCompletedWithWrappedCFException(g);
2274 }
2275
2276 /**
2277 * runAfterEitherAsync result completes exceptionally if either
2278 * source cancelled
2279 */
2280 public void testRunAfterEitherAsync4E() {
2281 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2282 CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2283 Noop r = new Noop();
2284 CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2285 assertTrue(f.cancel(true));
2286 checkCompletedWithWrappedCancellationException(g);
2287
2288 r = new Noop();
2289 f = new CompletableFuture<Integer>();
2290 f2 = new CompletableFuture<Integer>();
2291 assertTrue(f2.cancel(true));
2292 g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2293 checkCompletedWithWrappedCancellationException(g);
2294 }
2295
2296 /**
2297 * thenComposeAsync result completes normally after normal
2298 * completion of source
2299 */
2300 public void testThenComposeAsyncE() {
2301 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2302 CompletableFutureInc r = new CompletableFutureInc();
2303 CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2304 f.complete(one);
2305 checkCompletedNormally(g, two);
2306 }
2307
2308 /**
2309 * thenComposeAsync result completes exceptionally after
2310 * exceptional completion of source
2311 */
2312 public void testThenComposeAsync2E() {
2313 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2314 CompletableFutureInc r = new CompletableFutureInc();
2315 CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2316 f.completeExceptionally(new CFException());
2317 checkCompletedWithWrappedCFException(g);
2318 }
2319
2320 /**
2321 * thenComposeAsync result completes exceptionally if action does
2322 */
2323 public void testThenComposeAsync3E() {
2324 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2325 FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
2326 CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2327 f.complete(one);
2328 checkCompletedWithWrappedCFException(g);
2329 }
2330
2331 /**
2332 * thenComposeAsync result completes exceptionally if source cancelled
2333 */
2334 public void testThenComposeAsync4E() {
2335 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2336 CompletableFutureInc r = new CompletableFutureInc();
2337 CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2338 assertTrue(f.cancel(true));
2339 checkCompletedWithWrappedCancellationException(g);
2340 }
2341
2342 // other static methods
2343
2344 /**
2345 * allOf(no component futures) returns a future completed normally
2346 * with the value null
2347 */
2348 public void testAllOf_empty() throws Exception {
2349 CompletableFuture<?> f = CompletableFuture.allOf();
2350 checkCompletedNormally(f, null);
2351 }
2352
2353 /**
2354 * allOf returns a future completed when all components complete
2355 */
2356 public void testAllOf() throws Exception {
2357 for (int k = 1; k < 20; ++k) {
2358 CompletableFuture[] fs = new CompletableFuture[k];
2359 for (int i = 0; i < k; ++i)
2360 fs[i] = new CompletableFuture<Integer>();
2361 CompletableFuture<?> f = CompletableFuture.allOf(fs);
2362 for (int i = 0; i < k; ++i) {
2363 checkIncomplete(f);
2364 fs[i].complete(one);
2365 }
2366 assertTrue(f.isDone());
2367 assertFalse(f.isCancelled());
2368 }
2369 }
2370
2371 /**
2372 * anyOf(no component futures) returns an incomplete future
2373 */
2374 public void testAnyOf_empty() throws Exception {
2375 CompletableFuture<?> f = CompletableFuture.anyOf();
2376 checkIncomplete(f);
2377 }
2378
2379 /**
2380 * allOf returns a future completed when any components complete
2381 */
2382 public void testAnyOf() throws Exception {
2383 for (int k = 1; k < 20; ++k) {
2384 CompletableFuture[] fs = new CompletableFuture[k];
2385 for (int i = 0; i < k; ++i)
2386 fs[i] = new CompletableFuture<Integer>();
2387 CompletableFuture<?> f = CompletableFuture.anyOf(fs);
2388 checkIncomplete(f);
2389 for (int i = 0; i < k; ++i) {
2390 fs[i].complete(one);
2391 assertTrue(f.isDone());
2392 }
2393 }
2394 }
2395
2396 /**
2397 * Completion methods throw NullPointerException with null arguments
2398 */
2399 public void testNPE() {
2400 CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2401 CompletableFuture<Integer> g = new CompletableFuture<Integer>();
2402 CompletableFuture h;
2403 try { h = f.thenApply(null); } catch (NullPointerException ok) {}
2404 try { h = f.thenAccept(null); } catch (NullPointerException ok) {}
2405 try { h = f.thenRun(null); } catch (NullPointerException ok) {}
2406 try { h = f.thenCombine(g, null); } catch (NullPointerException ok) {}
2407 try { h = f.thenCombine(null, null); } catch (NullPointerException ok) {}
2408 try { h = f.applyToEither(g, null); } catch (NullPointerException ok) {}
2409 try { h = f.applyToEither(null, null); } catch (NullPointerException ok) {}
2410 try { h = f.thenAcceptBoth(g, null); } catch (NullPointerException ok) {}
2411 try { h = f.thenAcceptBoth(null, null); } catch (NullPointerException ok) {}
2412 try { h = f.runAfterEither(g, null); } catch (NullPointerException ok) {}
2413 try { h = f.runAfterEither(null, null); } catch (NullPointerException ok) {}
2414 try { h = f.runAfterBoth(g, null); } catch (NullPointerException ok) {}
2415 try { h = f.runAfterBoth(null, null); } catch (NullPointerException ok) {}
2416 try { h = f.exceptionally(null); } catch (NullPointerException ok) {}
2417 try { h = f.handle(null); } catch (NullPointerException ok) {}
2418 try { h = f.thenCompose(null); } catch (NullPointerException ok) {}
2419
2420 try { h = f.thenApplyAsync(null); } catch (NullPointerException ok) {}
2421 try { h = f.thenAcceptAsync(null); } catch (NullPointerException ok) {}
2422 try { h = f.thenRunAsync(null); } catch (NullPointerException ok) {}
2423 try { h = f.thenCombineAsync(g, null); } catch (NullPointerException ok) {}
2424 try { h = f.thenCombineAsync(null, null); } catch (NullPointerException ok) {}
2425 try { h = f.applyToEitherAsync(g, null); } catch (NullPointerException ok) {}
2426 try { h = f.applyToEitherAsync(null, null); } catch (NullPointerException ok) {}
2427 try { h = f.thenAcceptBothAsync(g, null); } catch (NullPointerException ok) {}
2428 try { h = f.thenAcceptBothAsync(null, null); } catch (NullPointerException ok) {}
2429 try { h = f.runAfterEitherAsync(g, null); } catch (NullPointerException ok) {}
2430 try { h = f.runAfterEitherAsync(null, null); } catch (NullPointerException ok) {}
2431 try { h = f.runAfterBothAsync(g, null); } catch (NullPointerException ok) {}
2432 try { h = f.runAfterBothAsync(null, null); } catch (NullPointerException ok) {}
2433
2434 }
2435
2436
2437 }