ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
Revision: 1.92
Committed: Tue Jun 17 20:50:01 2014 UTC (9 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.91: +53 -39 lines
Log Message:
add tests with 2 identical sources

File Contents

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