ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
Revision: 1.63
Committed: Fri Jun 6 16:54:16 2014 UTC (9 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.62: +164 -242 lines
Log Message:
improve tests for acceptEither

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