ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
Revision: 1.54
Committed: Mon Jun 2 21:32:24 2014 UTC (9 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.53: +147 -361 lines
Log Message:
improve tests for thenAcceptEither

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