ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
Revision: 1.40
Committed: Mon Jun 2 01:11:53 2014 UTC (9 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.39: +291 -199 lines
Log Message:
improve tests for acceptEither

File Contents

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