ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck-jsr166e/CompletableFutureTest.java
Revision: 1.4
Committed: Thu Feb 26 06:53:34 2015 UTC (9 years, 2 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +0 -5 lines
Log Message:
delete unused imports

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