ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
Revision: 1.25
Committed: Sat Apr 20 23:28:35 2013 UTC (11 years, 1 month ago) by jsr166
Branch: MAIN
Changes since 1.24: +5 -3 lines
Log Message:
wording improvements

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