ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
Revision: 1.8
Committed: Fri Mar 22 22:27:04 2013 UTC (11 years, 1 month ago) by jsr166
Branch: MAIN
Changes since 1.7: +26 -40 lines
Log Message:
consistent exception handling

File Contents

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