ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
Revision: 1.18
Committed: Sun Apr 7 14:54:06 2013 UTC (11 years, 1 month ago) by jsr166
Branch: MAIN
Changes since 1.17: +251 -94 lines
Log Message:
improve tests of combiners

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