ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
Revision: 1.151
Committed: Sun Jun 26 17:45:35 2016 UTC (7 years, 10 months ago) by jsr166
Branch: MAIN
Changes since 1.150: +65 -58 lines
Log Message:
precisely test exception propagation from failing actions

File Contents

# User Rev Content
1 jsr166 1.1 /*
2 jsr166 1.4 * 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 jsr166 1.1 * http://creativecommons.org/publicdomain/zero/1.0/
6     */
7    
8 jsr166 1.98 import static java.util.concurrent.TimeUnit.MILLISECONDS;
9     import static java.util.concurrent.TimeUnit.SECONDS;
10 jsr166 1.128 import static java.util.concurrent.CompletableFuture.completedFuture;
11     import static java.util.concurrent.CompletableFuture.failedFuture;
12 jsr166 1.98
13 jsr166 1.123 import java.lang.reflect.Method;
14     import java.lang.reflect.Modifier;
15    
16     import java.util.stream.Collectors;
17     import java.util.stream.Stream;
18    
19 jsr166 1.98 import java.util.ArrayList;
20 jsr166 1.123 import java.util.Arrays;
21 jsr166 1.98 import java.util.List;
22     import java.util.Objects;
23 jsr166 1.123 import java.util.Set;
24 jsr166 1.1 import java.util.concurrent.Callable;
25     import java.util.concurrent.CancellationException;
26     import java.util.concurrent.CompletableFuture;
27 dl 1.5 import java.util.concurrent.CompletionException;
28 jsr166 1.35 import java.util.concurrent.CompletionStage;
29 jsr166 1.98 import java.util.concurrent.ExecutionException;
30     import java.util.concurrent.Executor;
31 jsr166 1.48 import java.util.concurrent.ForkJoinPool;
32     import java.util.concurrent.ForkJoinTask;
33 jsr166 1.1 import java.util.concurrent.TimeoutException;
34 dl 1.103 import java.util.concurrent.TimeUnit;
35 jsr166 1.1 import java.util.concurrent.atomic.AtomicInteger;
36 dl 1.103 import java.util.concurrent.atomic.AtomicReference;
37 jsr166 1.98 import java.util.function.BiConsumer;
38     import java.util.function.BiFunction;
39 dl 1.5 import java.util.function.Consumer;
40     import java.util.function.Function;
41 jsr166 1.124 import java.util.function.Predicate;
42 jsr166 1.98 import java.util.function.Supplier;
43    
44 jsr166 1.128 import junit.framework.AssertionFailedError;
45 jsr166 1.98 import junit.framework.Test;
46     import junit.framework.TestSuite;
47 jsr166 1.1
48     public class CompletableFutureTest extends JSR166TestCase {
49    
50     public static void main(String[] args) {
51 jsr166 1.102 main(suite(), args);
52 jsr166 1.1 }
53     public static Test suite() {
54     return new TestSuite(CompletableFutureTest.class);
55     }
56    
57 dl 1.5 static class CFException extends RuntimeException {}
58    
59 jsr166 1.4 void checkIncomplete(CompletableFuture<?> f) {
60     assertFalse(f.isDone());
61     assertFalse(f.isCancelled());
62 dl 1.103 assertTrue(f.toString().contains("Not completed"));
63 jsr166 1.4 try {
64     assertNull(f.getNow(null));
65     } catch (Throwable fail) { threadUnexpectedException(fail); }
66     try {
67     f.get(0L, SECONDS);
68     shouldThrow();
69     }
70     catch (TimeoutException success) {}
71     catch (Throwable fail) { threadUnexpectedException(fail); }
72     }
73    
74 jsr166 1.11 <T> void checkCompletedNormally(CompletableFuture<T> f, T value) {
75 jsr166 1.95 checkTimedGet(f, value);
76    
77 jsr166 1.20 try {
78 dl 1.5 assertEquals(value, f.join());
79 jsr166 1.4 } catch (Throwable fail) { threadUnexpectedException(fail); }
80     try {
81 dl 1.5 assertEquals(value, f.getNow(null));
82 jsr166 1.4 } catch (Throwable fail) { threadUnexpectedException(fail); }
83     try {
84 dl 1.5 assertEquals(value, f.get());
85 jsr166 1.4 } catch (Throwable fail) { threadUnexpectedException(fail); }
86 dl 1.5 assertTrue(f.isDone());
87     assertFalse(f.isCancelled());
88 dl 1.26 assertFalse(f.isCompletedExceptionally());
89 dl 1.5 assertTrue(f.toString().contains("[Completed normally]"));
90     }
91    
92 jsr166 1.121 /**
93     * Returns the "raw" internal exceptional completion of f,
94     * without any additional wrapping with CompletionException.
95     */
96     <U> Throwable exceptionalCompletion(CompletableFuture<U> f) {
97     // handle (and whenComplete) can distinguish between "direct"
98     // and "wrapped" exceptional completion
99     return f.handle((U u, Throwable t) -> t).join();
100     }
101    
102     void checkCompletedExceptionally(CompletableFuture<?> f,
103     boolean wrapped,
104     Consumer<Throwable> checker) {
105     Throwable cause = exceptionalCompletion(f);
106     if (wrapped) {
107     assertTrue(cause instanceof CompletionException);
108     cause = cause.getCause();
109     }
110     checker.accept(cause);
111    
112 jsr166 1.95 long startTime = System.nanoTime();
113 dl 1.5 try {
114 jsr166 1.121 f.get(LONG_DELAY_MS, MILLISECONDS);
115 jsr166 1.20 shouldThrow();
116     } catch (ExecutionException success) {
117 jsr166 1.121 assertSame(cause, success.getCause());
118 jsr166 1.20 } catch (Throwable fail) { threadUnexpectedException(fail); }
119 jsr166 1.121 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
120 jsr166 1.95
121 jsr166 1.20 try {
122 dl 1.5 f.join();
123     shouldThrow();
124 jsr166 1.8 } catch (CompletionException success) {
125 jsr166 1.121 assertSame(cause, success.getCause());
126     } catch (Throwable fail) { threadUnexpectedException(fail); }
127    
128 dl 1.5 try {
129     f.getNow(null);
130     shouldThrow();
131 jsr166 1.8 } catch (CompletionException success) {
132 jsr166 1.121 assertSame(cause, success.getCause());
133 jsr166 1.8 } catch (Throwable fail) { threadUnexpectedException(fail); }
134 dl 1.5
135 jsr166 1.33 try {
136     f.get();
137     shouldThrow();
138     } catch (ExecutionException success) {
139 jsr166 1.121 assertSame(cause, success.getCause());
140 jsr166 1.33 } catch (Throwable fail) { threadUnexpectedException(fail); }
141 jsr166 1.73
142 jsr166 1.121 assertFalse(f.isCancelled());
143 jsr166 1.33 assertTrue(f.isDone());
144 jsr166 1.121 assertTrue(f.isCompletedExceptionally());
145 jsr166 1.33 assertTrue(f.toString().contains("[Completed exceptionally]"));
146     }
147    
148 jsr166 1.121 void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
149     checkCompletedExceptionally(f, true,
150     (t) -> assertTrue(t instanceof CFException));
151     }
152 dl 1.103
153 jsr166 1.121 void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
154     checkCompletedExceptionally(f, true,
155     (t) -> assertTrue(t instanceof CancellationException));
156     }
157 dl 1.103
158 jsr166 1.121 void checkCompletedWithTimeoutException(CompletableFuture<?> f) {
159     checkCompletedExceptionally(f, false,
160     (t) -> assertTrue(t instanceof TimeoutException));
161 dl 1.103 }
162    
163 jsr166 1.121 void checkCompletedWithWrappedException(CompletableFuture<?> f,
164     Throwable ex) {
165     checkCompletedExceptionally(f, true, (t) -> assertSame(t, ex));
166 jsr166 1.72 }
167    
168 jsr166 1.121 void checkCompletedExceptionally(CompletableFuture<?> f, Throwable ex) {
169     checkCompletedExceptionally(f, false, (t) -> assertSame(t, ex));
170 jsr166 1.72 }
171    
172 dl 1.5 void checkCancelled(CompletableFuture<?> f) {
173 jsr166 1.95 long startTime = System.nanoTime();
174 dl 1.5 try {
175 jsr166 1.121 f.get(LONG_DELAY_MS, MILLISECONDS);
176 jsr166 1.20 shouldThrow();
177     } catch (CancellationException success) {
178     } catch (Throwable fail) { threadUnexpectedException(fail); }
179 jsr166 1.121 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
180 jsr166 1.95
181 jsr166 1.20 try {
182 dl 1.5 f.join();
183     shouldThrow();
184 jsr166 1.8 } catch (CancellationException success) {}
185 dl 1.5 try {
186     f.getNow(null);
187     shouldThrow();
188 jsr166 1.8 } catch (CancellationException success) {}
189 dl 1.5 try {
190     f.get();
191     shouldThrow();
192 jsr166 1.8 } catch (CancellationException success) {
193     } catch (Throwable fail) { threadUnexpectedException(fail); }
194 dl 1.5
195 jsr166 1.121 assertTrue(exceptionalCompletion(f) instanceof CancellationException);
196 jsr166 1.95
197 dl 1.5 assertTrue(f.isDone());
198 dl 1.26 assertTrue(f.isCompletedExceptionally());
199 jsr166 1.121 assertTrue(f.isCancelled());
200 jsr166 1.14 assertTrue(f.toString().contains("[Completed exceptionally]"));
201 dl 1.5 }
202    
203     /**
204     * A newly constructed CompletableFuture is incomplete, as indicated
205     * by methods isDone, isCancelled, and getNow
206     */
207     public void testConstructor() {
208 jsr166 1.22 CompletableFuture<Integer> f = new CompletableFuture<>();
209 dl 1.5 checkIncomplete(f);
210     }
211    
212     /**
213     * complete completes normally, as indicated by methods isDone,
214     * isCancelled, join, get, and getNow
215     */
216     public void testComplete() {
217 jsr166 1.78 for (Integer v1 : new Integer[] { 1, null })
218     {
219 jsr166 1.22 CompletableFuture<Integer> f = new CompletableFuture<>();
220 dl 1.5 checkIncomplete(f);
221 jsr166 1.78 assertTrue(f.complete(v1));
222     assertFalse(f.complete(v1));
223     checkCompletedNormally(f, v1);
224     }}
225 dl 1.5
226     /**
227     * completeExceptionally completes exceptionally, as indicated by
228     * methods isDone, isCancelled, join, get, and getNow
229     */
230     public void testCompleteExceptionally() {
231 jsr166 1.22 CompletableFuture<Integer> f = new CompletableFuture<>();
232 jsr166 1.72 CFException ex = new CFException();
233 dl 1.5 checkIncomplete(f);
234 jsr166 1.72 f.completeExceptionally(ex);
235     checkCompletedExceptionally(f, ex);
236 dl 1.5 }
237    
238     /**
239     * cancel completes exceptionally and reports cancelled, as indicated by
240     * methods isDone, isCancelled, join, get, and getNow
241     */
242     public void testCancel() {
243 jsr166 1.78 for (boolean mayInterruptIfRunning : new boolean[] { true, false })
244     {
245 jsr166 1.22 CompletableFuture<Integer> f = new CompletableFuture<>();
246 dl 1.5 checkIncomplete(f);
247 jsr166 1.101 assertTrue(f.cancel(mayInterruptIfRunning));
248     assertTrue(f.cancel(mayInterruptIfRunning));
249     assertTrue(f.cancel(!mayInterruptIfRunning));
250 dl 1.5 checkCancelled(f);
251 jsr166 1.78 }}
252 dl 1.5
253     /**
254     * obtrudeValue forces completion with given value
255     */
256     public void testObtrudeValue() {
257 jsr166 1.22 CompletableFuture<Integer> f = new CompletableFuture<>();
258 dl 1.5 checkIncomplete(f);
259 jsr166 1.78 assertTrue(f.complete(one));
260 dl 1.5 checkCompletedNormally(f, one);
261     f.obtrudeValue(three);
262     checkCompletedNormally(f, three);
263     f.obtrudeValue(two);
264     checkCompletedNormally(f, two);
265 jsr166 1.22 f = new CompletableFuture<>();
266 dl 1.5 f.obtrudeValue(three);
267     checkCompletedNormally(f, three);
268 jsr166 1.46 f.obtrudeValue(null);
269     checkCompletedNormally(f, null);
270 jsr166 1.22 f = new CompletableFuture<>();
271 dl 1.5 f.completeExceptionally(new CFException());
272     f.obtrudeValue(four);
273     checkCompletedNormally(f, four);
274 jsr166 1.4 }
275    
276 dl 1.5 /**
277     * obtrudeException forces completion with given exception
278     */
279     public void testObtrudeException() {
280 jsr166 1.72 for (Integer v1 : new Integer[] { 1, null })
281     {
282     CFException ex;
283     CompletableFuture<Integer> f;
284    
285     f = new CompletableFuture<>();
286 jsr166 1.78 assertTrue(f.complete(v1));
287 jsr166 1.72 for (int i = 0; i < 2; i++) {
288     f.obtrudeException(ex = new CFException());
289     checkCompletedExceptionally(f, ex);
290     }
291    
292 jsr166 1.22 f = new CompletableFuture<>();
293 jsr166 1.72 for (int i = 0; i < 2; i++) {
294     f.obtrudeException(ex = new CFException());
295     checkCompletedExceptionally(f, ex);
296     }
297    
298 jsr166 1.22 f = new CompletableFuture<>();
299 jsr166 1.72 f.completeExceptionally(ex = new CFException());
300     f.obtrudeValue(v1);
301     checkCompletedNormally(f, v1);
302     f.obtrudeException(ex = new CFException());
303     checkCompletedExceptionally(f, ex);
304 dl 1.5 f.completeExceptionally(new CFException());
305 jsr166 1.72 checkCompletedExceptionally(f, ex);
306 jsr166 1.78 assertFalse(f.complete(v1));
307 jsr166 1.72 checkCompletedExceptionally(f, ex);
308     }}
309 jsr166 1.6
310 dl 1.5 /**
311     * getNumberOfDependents returns number of dependent tasks
312     */
313     public void testGetNumberOfDependents() {
314 jsr166 1.76 for (ExecutionMode m : ExecutionMode.values())
315 jsr166 1.78 for (Integer v1 : new Integer[] { 1, null })
316 jsr166 1.76 {
317 jsr166 1.22 CompletableFuture<Integer> f = new CompletableFuture<>();
318 jsr166 1.46 assertEquals(0, f.getNumberOfDependents());
319 jsr166 1.76 final CompletableFuture<Void> g = m.thenRun(f, new Noop(m));
320 jsr166 1.46 assertEquals(1, f.getNumberOfDependents());
321     assertEquals(0, g.getNumberOfDependents());
322 jsr166 1.76 final CompletableFuture<Void> h = m.thenRun(f, new Noop(m));
323 jsr166 1.46 assertEquals(2, f.getNumberOfDependents());
324 jsr166 1.76 assertEquals(0, h.getNumberOfDependents());
325 jsr166 1.78 assertTrue(f.complete(v1));
326 dl 1.5 checkCompletedNormally(g, null);
327 jsr166 1.76 checkCompletedNormally(h, null);
328 jsr166 1.46 assertEquals(0, f.getNumberOfDependents());
329     assertEquals(0, g.getNumberOfDependents());
330 jsr166 1.76 assertEquals(0, h.getNumberOfDependents());
331     }}
332 jsr166 1.3
333 dl 1.5 /**
334     * toString indicates current completion state
335     */
336 jsr166 1.1 public void testToString() {
337     CompletableFuture<String> f;
338 jsr166 1.2
339 jsr166 1.1 f = new CompletableFuture<String>();
340 jsr166 1.2 assertTrue(f.toString().contains("[Not completed]"));
341    
342 jsr166 1.78 assertTrue(f.complete("foo"));
343 jsr166 1.1 assertTrue(f.toString().contains("[Completed normally]"));
344 jsr166 1.2
345 jsr166 1.1 f = new CompletableFuture<String>();
346 jsr166 1.78 assertTrue(f.completeExceptionally(new IndexOutOfBoundsException()));
347 jsr166 1.1 assertTrue(f.toString().contains("[Completed exceptionally]"));
348 jsr166 1.74
349 jsr166 1.77 for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
350     f = new CompletableFuture<String>();
351 jsr166 1.78 assertTrue(f.cancel(mayInterruptIfRunning));
352 jsr166 1.77 assertTrue(f.toString().contains("[Completed exceptionally]"));
353     }
354 jsr166 1.1 }
355 jsr166 1.4
356 dl 1.9 /**
357 jsr166 1.10 * completedFuture returns a completed CompletableFuture with given value
358 dl 1.9 */
359     public void testCompletedFuture() {
360     CompletableFuture<String> f = CompletableFuture.completedFuture("test");
361     checkCompletedNormally(f, "test");
362     }
363    
364 jsr166 1.62 abstract class CheckedAction {
365     int invocationCount = 0;
366 jsr166 1.58 final ExecutionMode m;
367 jsr166 1.62 CheckedAction(ExecutionMode m) { this.m = m; }
368     void invoked() {
369     m.checkExecutionMode();
370     assertEquals(0, invocationCount++);
371     }
372     void assertNotInvoked() { assertEquals(0, invocationCount); }
373     void assertInvoked() { assertEquals(1, invocationCount); }
374     }
375    
376     abstract class CheckedIntegerAction extends CheckedAction {
377     Integer value;
378     CheckedIntegerAction(ExecutionMode m) { super(m); }
379     void assertValue(Integer expected) {
380     assertInvoked();
381     assertEquals(expected, value);
382     }
383     }
384    
385     class IntegerSupplier extends CheckedAction
386     implements Supplier<Integer>
387     {
388 jsr166 1.58 final Integer value;
389     IntegerSupplier(ExecutionMode m, Integer value) {
390 jsr166 1.62 super(m);
391 jsr166 1.58 this.value = value;
392     }
393     public Integer get() {
394 jsr166 1.62 invoked();
395 jsr166 1.58 return value;
396     }
397     }
398 jsr166 1.60
399     // A function that handles and produces null values as well.
400     static Integer inc(Integer x) {
401     return (x == null) ? null : x + 1;
402     }
403    
404 jsr166 1.64 class NoopConsumer extends CheckedIntegerAction
405 jsr166 1.62 implements Consumer<Integer>
406     {
407 jsr166 1.64 NoopConsumer(ExecutionMode m) { super(m); }
408 jsr166 1.38 public void accept(Integer x) {
409 jsr166 1.62 invoked();
410 jsr166 1.64 value = x;
411 jsr166 1.38 }
412     }
413 jsr166 1.62
414     class IncFunction extends CheckedIntegerAction
415     implements Function<Integer,Integer>
416     {
417     IncFunction(ExecutionMode m) { super(m); }
418 jsr166 1.38 public Integer apply(Integer x) {
419 jsr166 1.62 invoked();
420 jsr166 1.38 return value = inc(x);
421     }
422 dl 1.5 }
423 jsr166 1.60
424     // Choose non-commutative actions for better coverage
425     // A non-commutative function that handles and produces null values as well.
426     static Integer subtract(Integer x, Integer y) {
427     return (x == null && y == null) ? null :
428     ((x == null) ? 42 : x.intValue())
429     - ((y == null) ? 99 : y.intValue());
430     }
431    
432 jsr166 1.62 class SubtractAction extends CheckedIntegerAction
433     implements BiConsumer<Integer, Integer>
434     {
435     SubtractAction(ExecutionMode m) { super(m); }
436 jsr166 1.6 public void accept(Integer x, Integer y) {
437 jsr166 1.62 invoked();
438 jsr166 1.35 value = subtract(x, y);
439 dl 1.5 }
440     }
441 jsr166 1.62
442     class SubtractFunction extends CheckedIntegerAction
443     implements BiFunction<Integer, Integer, Integer>
444     {
445     SubtractFunction(ExecutionMode m) { super(m); }
446 jsr166 1.36 public Integer apply(Integer x, Integer y) {
447 jsr166 1.62 invoked();
448 jsr166 1.37 return value = subtract(x, y);
449 jsr166 1.36 }
450     }
451 jsr166 1.60
452 jsr166 1.62 class Noop extends CheckedAction implements Runnable {
453     Noop(ExecutionMode m) { super(m); }
454 jsr166 1.41 public void run() {
455 jsr166 1.62 invoked();
456 jsr166 1.41 }
457 dl 1.5 }
458    
459 jsr166 1.63 class FailingSupplier extends CheckedAction
460     implements Supplier<Integer>
461 jsr166 1.62 {
462 jsr166 1.151 final CFException ex;
463     FailingSupplier(ExecutionMode m) { super(m); ex = new CFException(); }
464 jsr166 1.44 public Integer get() {
465 jsr166 1.62 invoked();
466 jsr166 1.151 throw ex;
467 jsr166 1.44 }
468 dl 1.5 }
469 jsr166 1.62
470 jsr166 1.63 class FailingConsumer extends CheckedIntegerAction
471     implements Consumer<Integer>
472     {
473 jsr166 1.151 final CFException ex;
474     FailingConsumer(ExecutionMode m) { super(m); ex = new CFException(); }
475 jsr166 1.44 public void accept(Integer x) {
476 jsr166 1.62 invoked();
477 jsr166 1.63 value = x;
478 jsr166 1.151 throw ex;
479 jsr166 1.44 }
480 dl 1.5 }
481 jsr166 1.62
482 jsr166 1.63 class FailingBiConsumer extends CheckedIntegerAction
483 jsr166 1.62 implements BiConsumer<Integer, Integer>
484     {
485 jsr166 1.151 final CFException ex;
486     FailingBiConsumer(ExecutionMode m) { super(m); ex = new CFException(); }
487 jsr166 1.44 public void accept(Integer x, Integer y) {
488 jsr166 1.62 invoked();
489 jsr166 1.63 value = subtract(x, y);
490 jsr166 1.151 throw ex;
491 jsr166 1.44 }
492 dl 1.5 }
493 jsr166 1.62
494 jsr166 1.63 class FailingFunction extends CheckedIntegerAction
495 jsr166 1.62 implements Function<Integer, Integer>
496     {
497 jsr166 1.151 final CFException ex;
498     FailingFunction(ExecutionMode m) { super(m); ex = new CFException(); }
499 jsr166 1.44 public Integer apply(Integer x) {
500 jsr166 1.62 invoked();
501 jsr166 1.63 value = x;
502 jsr166 1.151 throw ex;
503 jsr166 1.44 }
504 dl 1.5 }
505 jsr166 1.62
506 jsr166 1.63 class FailingBiFunction extends CheckedIntegerAction
507 jsr166 1.62 implements BiFunction<Integer, Integer, Integer>
508     {
509 jsr166 1.151 final CFException ex;
510     FailingBiFunction(ExecutionMode m) { super(m); ex = new CFException(); }
511 jsr166 1.44 public Integer apply(Integer x, Integer y) {
512 jsr166 1.62 invoked();
513 jsr166 1.63 value = subtract(x, y);
514 jsr166 1.151 throw ex;
515 jsr166 1.44 }
516 dl 1.5 }
517 jsr166 1.62
518     class FailingRunnable extends CheckedAction implements Runnable {
519 jsr166 1.151 final CFException ex;
520     FailingRunnable(ExecutionMode m) { super(m); ex = new CFException(); }
521 jsr166 1.44 public void run() {
522 jsr166 1.62 invoked();
523 jsr166 1.151 throw ex;
524 jsr166 1.44 }
525 dl 1.5 }
526    
527 jsr166 1.63 class CompletableFutureInc extends CheckedIntegerAction
528 jsr166 1.62 implements Function<Integer, CompletableFuture<Integer>>
529     {
530     CompletableFutureInc(ExecutionMode m) { super(m); }
531 dl 1.5 public CompletableFuture<Integer> apply(Integer x) {
532 jsr166 1.62 invoked();
533 jsr166 1.63 value = x;
534 jsr166 1.22 CompletableFuture<Integer> f = new CompletableFuture<>();
535 jsr166 1.78 assertTrue(f.complete(inc(x)));
536 dl 1.5 return f;
537     }
538     }
539    
540 jsr166 1.63 class FailingCompletableFutureFunction extends CheckedIntegerAction
541 jsr166 1.62 implements Function<Integer, CompletableFuture<Integer>>
542     {
543 jsr166 1.151 final CFException ex;
544     FailingCompletableFutureFunction(ExecutionMode m) { super(m); ex = new CFException(); }
545 dl 1.5 public CompletableFuture<Integer> apply(Integer x) {
546 jsr166 1.62 invoked();
547 jsr166 1.63 value = x;
548 jsr166 1.151 throw ex;
549 dl 1.5 }
550     }
551 jsr166 1.6
552 dl 1.5 // Used for explicit executor tests
553     static final class ThreadExecutor implements Executor {
554 jsr166 1.56 final AtomicInteger count = new AtomicInteger(0);
555     static final ThreadGroup tg = new ThreadGroup("ThreadExecutor");
556     static boolean startedCurrentThread() {
557     return Thread.currentThread().getThreadGroup() == tg;
558     }
559 jsr166 1.17
560 dl 1.5 public void execute(Runnable r) {
561 jsr166 1.17 count.getAndIncrement();
562 jsr166 1.56 new Thread(tg, r).start();
563 dl 1.5 }
564     }
565    
566 jsr166 1.96 static final boolean defaultExecutorIsCommonPool
567     = ForkJoinPool.getCommonPoolParallelism() > 1;
568    
569 dl 1.5 /**
570 jsr166 1.35 * Permits the testing of parallel code for the 3 different
571 jsr166 1.60 * execution modes without copy/pasting all the test methods.
572 jsr166 1.35 */
573     enum ExecutionMode {
574 jsr166 1.95 SYNC {
575 jsr166 1.48 public void checkExecutionMode() {
576 jsr166 1.60 assertFalse(ThreadExecutor.startedCurrentThread());
577 jsr166 1.48 assertNull(ForkJoinTask.getPool());
578     }
579 jsr166 1.57 public CompletableFuture<Void> runAsync(Runnable a) {
580     throw new UnsupportedOperationException();
581     }
582 jsr166 1.58 public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
583     throw new UnsupportedOperationException();
584     }
585 jsr166 1.46 public <T> CompletableFuture<Void> thenRun
586     (CompletableFuture<T> f, Runnable a) {
587     return f.thenRun(a);
588     }
589     public <T> CompletableFuture<Void> thenAccept
590     (CompletableFuture<T> f, Consumer<? super T> a) {
591     return f.thenAccept(a);
592     }
593     public <T,U> CompletableFuture<U> thenApply
594     (CompletableFuture<T> f, Function<? super T,U> a) {
595     return f.thenApply(a);
596     }
597     public <T,U> CompletableFuture<U> thenCompose
598     (CompletableFuture<T> f,
599     Function<? super T,? extends CompletionStage<U>> a) {
600     return f.thenCompose(a);
601     }
602     public <T,U> CompletableFuture<U> handle
603     (CompletableFuture<T> f,
604     BiFunction<? super T,Throwable,? extends U> a) {
605     return f.handle(a);
606     }
607     public <T> CompletableFuture<T> whenComplete
608     (CompletableFuture<T> f,
609     BiConsumer<? super T,? super Throwable> a) {
610     return f.whenComplete(a);
611     }
612 jsr166 1.35 public <T,U> CompletableFuture<Void> runAfterBoth
613     (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
614     return f.runAfterBoth(g, a);
615     }
616     public <T,U> CompletableFuture<Void> thenAcceptBoth
617     (CompletableFuture<T> f,
618     CompletionStage<? extends U> g,
619     BiConsumer<? super T,? super U> a) {
620     return f.thenAcceptBoth(g, a);
621     }
622 jsr166 1.36 public <T,U,V> CompletableFuture<V> thenCombine
623     (CompletableFuture<T> f,
624     CompletionStage<? extends U> g,
625     BiFunction<? super T,? super U,? extends V> a) {
626     return f.thenCombine(g, a);
627     }
628 jsr166 1.46 public <T> CompletableFuture<Void> runAfterEither
629 jsr166 1.38 (CompletableFuture<T> f,
630 jsr166 1.46 CompletionStage<?> g,
631     java.lang.Runnable a) {
632     return f.runAfterEither(g, a);
633 jsr166 1.38 }
634     public <T> CompletableFuture<Void> acceptEither
635     (CompletableFuture<T> f,
636     CompletionStage<? extends T> g,
637     Consumer<? super T> a) {
638     return f.acceptEither(g, a);
639     }
640 jsr166 1.46 public <T,U> CompletableFuture<U> applyToEither
641 jsr166 1.38 (CompletableFuture<T> f,
642 jsr166 1.46 CompletionStage<? extends T> g,
643     Function<? super T,U> a) {
644     return f.applyToEither(g, a);
645     }
646     },
647    
648 jsr166 1.48 ASYNC {
649     public void checkExecutionMode() {
650 jsr166 1.144 assertEquals(defaultExecutorIsCommonPool,
651     (ForkJoinPool.commonPool() == ForkJoinTask.getPool()));
652 jsr166 1.48 }
653 jsr166 1.57 public CompletableFuture<Void> runAsync(Runnable a) {
654     return CompletableFuture.runAsync(a);
655     }
656 jsr166 1.58 public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
657     return CompletableFuture.supplyAsync(a);
658     }
659 jsr166 1.46 public <T> CompletableFuture<Void> thenRun
660     (CompletableFuture<T> f, Runnable a) {
661     return f.thenRunAsync(a);
662     }
663     public <T> CompletableFuture<Void> thenAccept
664     (CompletableFuture<T> f, Consumer<? super T> a) {
665     return f.thenAcceptAsync(a);
666     }
667     public <T,U> CompletableFuture<U> thenApply
668     (CompletableFuture<T> f, Function<? super T,U> a) {
669     return f.thenApplyAsync(a);
670 jsr166 1.38 }
671     public <T,U> CompletableFuture<U> thenCompose
672     (CompletableFuture<T> f,
673     Function<? super T,? extends CompletionStage<U>> a) {
674 jsr166 1.46 return f.thenComposeAsync(a);
675     }
676     public <T,U> CompletableFuture<U> handle
677     (CompletableFuture<T> f,
678     BiFunction<? super T,Throwable,? extends U> a) {
679     return f.handleAsync(a);
680 jsr166 1.38 }
681     public <T> CompletableFuture<T> whenComplete
682     (CompletableFuture<T> f,
683     BiConsumer<? super T,? super Throwable> a) {
684 jsr166 1.46 return f.whenCompleteAsync(a);
685 jsr166 1.38 }
686 jsr166 1.35 public <T,U> CompletableFuture<Void> runAfterBoth
687     (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
688     return f.runAfterBothAsync(g, a);
689     }
690     public <T,U> CompletableFuture<Void> thenAcceptBoth
691     (CompletableFuture<T> f,
692     CompletionStage<? extends U> g,
693     BiConsumer<? super T,? super U> a) {
694     return f.thenAcceptBothAsync(g, a);
695     }
696 jsr166 1.36 public <T,U,V> CompletableFuture<V> thenCombine
697     (CompletableFuture<T> f,
698     CompletionStage<? extends U> g,
699     BiFunction<? super T,? super U,? extends V> a) {
700     return f.thenCombineAsync(g, a);
701     }
702 jsr166 1.46 public <T> CompletableFuture<Void> runAfterEither
703 jsr166 1.38 (CompletableFuture<T> f,
704 jsr166 1.46 CompletionStage<?> g,
705     java.lang.Runnable a) {
706     return f.runAfterEitherAsync(g, a);
707 jsr166 1.38 }
708     public <T> CompletableFuture<Void> acceptEither
709     (CompletableFuture<T> f,
710     CompletionStage<? extends T> g,
711     Consumer<? super T> a) {
712     return f.acceptEitherAsync(g, a);
713     }
714 jsr166 1.46 public <T,U> CompletableFuture<U> applyToEither
715 jsr166 1.38 (CompletableFuture<T> f,
716 jsr166 1.46 CompletionStage<? extends T> g,
717     Function<? super T,U> a) {
718     return f.applyToEitherAsync(g, a);
719     }
720     },
721    
722     EXECUTOR {
723 jsr166 1.48 public void checkExecutionMode() {
724 jsr166 1.56 assertTrue(ThreadExecutor.startedCurrentThread());
725 jsr166 1.48 }
726 jsr166 1.57 public CompletableFuture<Void> runAsync(Runnable a) {
727     return CompletableFuture.runAsync(a, new ThreadExecutor());
728     }
729 jsr166 1.58 public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
730     return CompletableFuture.supplyAsync(a, new ThreadExecutor());
731     }
732 jsr166 1.46 public <T> CompletableFuture<Void> thenRun
733     (CompletableFuture<T> f, Runnable a) {
734     return f.thenRunAsync(a, new ThreadExecutor());
735     }
736     public <T> CompletableFuture<Void> thenAccept
737     (CompletableFuture<T> f, Consumer<? super T> a) {
738     return f.thenAcceptAsync(a, new ThreadExecutor());
739     }
740     public <T,U> CompletableFuture<U> thenApply
741     (CompletableFuture<T> f, Function<? super T,U> a) {
742     return f.thenApplyAsync(a, new ThreadExecutor());
743 jsr166 1.38 }
744     public <T,U> CompletableFuture<U> thenCompose
745     (CompletableFuture<T> f,
746     Function<? super T,? extends CompletionStage<U>> a) {
747 jsr166 1.46 return f.thenComposeAsync(a, new ThreadExecutor());
748     }
749     public <T,U> CompletableFuture<U> handle
750     (CompletableFuture<T> f,
751     BiFunction<? super T,Throwable,? extends U> a) {
752     return f.handleAsync(a, new ThreadExecutor());
753 jsr166 1.38 }
754     public <T> CompletableFuture<T> whenComplete
755     (CompletableFuture<T> f,
756     BiConsumer<? super T,? super Throwable> a) {
757 jsr166 1.46 return f.whenCompleteAsync(a, new ThreadExecutor());
758 jsr166 1.38 }
759 jsr166 1.35 public <T,U> CompletableFuture<Void> runAfterBoth
760     (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
761     return f.runAfterBothAsync(g, a, new ThreadExecutor());
762     }
763     public <T,U> CompletableFuture<Void> thenAcceptBoth
764     (CompletableFuture<T> f,
765     CompletionStage<? extends U> g,
766     BiConsumer<? super T,? super U> a) {
767     return f.thenAcceptBothAsync(g, a, new ThreadExecutor());
768     }
769 jsr166 1.36 public <T,U,V> CompletableFuture<V> thenCombine
770     (CompletableFuture<T> f,
771     CompletionStage<? extends U> g,
772     BiFunction<? super T,? super U,? extends V> a) {
773     return f.thenCombineAsync(g, a, new ThreadExecutor());
774     }
775 jsr166 1.46 public <T> CompletableFuture<Void> runAfterEither
776 jsr166 1.38 (CompletableFuture<T> f,
777 jsr166 1.46 CompletionStage<?> g,
778     java.lang.Runnable a) {
779     return f.runAfterEitherAsync(g, a, new ThreadExecutor());
780 jsr166 1.38 }
781     public <T> CompletableFuture<Void> acceptEither
782     (CompletableFuture<T> f,
783     CompletionStage<? extends T> g,
784     Consumer<? super T> a) {
785     return f.acceptEitherAsync(g, a, new ThreadExecutor());
786     }
787 jsr166 1.46 public <T,U> CompletableFuture<U> applyToEither
788 jsr166 1.38 (CompletableFuture<T> f,
789 jsr166 1.46 CompletionStage<? extends T> g,
790     Function<? super T,U> a) {
791     return f.applyToEitherAsync(g, a, new ThreadExecutor());
792 jsr166 1.38 }
793 jsr166 1.35 };
794    
795 jsr166 1.48 public abstract void checkExecutionMode();
796 jsr166 1.57 public abstract CompletableFuture<Void> runAsync(Runnable a);
797 jsr166 1.58 public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a);
798 jsr166 1.46 public abstract <T> CompletableFuture<Void> thenRun
799     (CompletableFuture<T> f, Runnable a);
800     public abstract <T> CompletableFuture<Void> thenAccept
801     (CompletableFuture<T> f, Consumer<? super T> a);
802     public abstract <T,U> CompletableFuture<U> thenApply
803     (CompletableFuture<T> f, Function<? super T,U> a);
804     public abstract <T,U> CompletableFuture<U> thenCompose
805     (CompletableFuture<T> f,
806     Function<? super T,? extends CompletionStage<U>> a);
807     public abstract <T,U> CompletableFuture<U> handle
808     (CompletableFuture<T> f,
809     BiFunction<? super T,Throwable,? extends U> a);
810     public abstract <T> CompletableFuture<T> whenComplete
811     (CompletableFuture<T> f,
812     BiConsumer<? super T,? super Throwable> a);
813 jsr166 1.35 public abstract <T,U> CompletableFuture<Void> runAfterBoth
814     (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a);
815     public abstract <T,U> CompletableFuture<Void> thenAcceptBoth
816     (CompletableFuture<T> f,
817     CompletionStage<? extends U> g,
818     BiConsumer<? super T,? super U> a);
819 jsr166 1.36 public abstract <T,U,V> CompletableFuture<V> thenCombine
820     (CompletableFuture<T> f,
821     CompletionStage<? extends U> g,
822     BiFunction<? super T,? super U,? extends V> a);
823 jsr166 1.46 public abstract <T> CompletableFuture<Void> runAfterEither
824 jsr166 1.38 (CompletableFuture<T> f,
825 jsr166 1.46 CompletionStage<?> g,
826     java.lang.Runnable a);
827 jsr166 1.38 public abstract <T> CompletableFuture<Void> acceptEither
828     (CompletableFuture<T> f,
829     CompletionStage<? extends T> g,
830     Consumer<? super T> a);
831 jsr166 1.46 public abstract <T,U> CompletableFuture<U> applyToEither
832 jsr166 1.38 (CompletableFuture<T> f,
833 jsr166 1.46 CompletionStage<? extends T> g,
834     Function<? super T,U> a);
835     }
836    
837     /**
838     * exceptionally action is not invoked when source completes
839     * normally, and source result is propagated
840     */
841     public void testExceptionally_normalCompletion() {
842     for (boolean createIncomplete : new boolean[] { true, false })
843     for (Integer v1 : new Integer[] { 1, null })
844     {
845     final AtomicInteger a = new AtomicInteger(0);
846     final CompletableFuture<Integer> f = new CompletableFuture<>();
847 jsr166 1.78 if (!createIncomplete) assertTrue(f.complete(v1));
848 jsr166 1.46 final CompletableFuture<Integer> g = f.exceptionally
849     ((Throwable t) -> {
850     a.getAndIncrement();
851 jsr166 1.127 threadFail("should not be called");
852     return null; // unreached
853 jsr166 1.46 });
854 jsr166 1.78 if (createIncomplete) assertTrue(f.complete(v1));
855 jsr166 1.38
856 jsr166 1.46 checkCompletedNormally(g, v1);
857     checkCompletedNormally(f, v1);
858     assertEquals(0, a.get());
859     }}
860 jsr166 1.38
861 jsr166 1.35 /**
862 dl 1.5 * exceptionally action completes with function value on source
863 jsr166 1.46 * exception
864     */
865     public void testExceptionally_exceptionalCompletion() {
866     for (boolean createIncomplete : new boolean[] { true, false })
867     for (Integer v1 : new Integer[] { 1, null })
868     {
869     final AtomicInteger a = new AtomicInteger(0);
870     final CFException ex = new CFException();
871     final CompletableFuture<Integer> f = new CompletableFuture<>();
872     if (!createIncomplete) f.completeExceptionally(ex);
873     final CompletableFuture<Integer> g = f.exceptionally
874     ((Throwable t) -> {
875 jsr166 1.95 ExecutionMode.SYNC.checkExecutionMode();
876 jsr166 1.46 threadAssertSame(t, ex);
877     a.getAndIncrement();
878     return v1;
879     });
880     if (createIncomplete) f.completeExceptionally(ex);
881    
882     checkCompletedNormally(g, v1);
883     assertEquals(1, a.get());
884     }}
885    
886 jsr166 1.136 /**
887     * If an "exceptionally action" throws an exception, it completes
888     * exceptionally with that exception
889     */
890 jsr166 1.46 public void testExceptionally_exceptionalCompletionActionFailed() {
891     for (boolean createIncomplete : new boolean[] { true, false })
892     {
893     final AtomicInteger a = new AtomicInteger(0);
894     final CFException ex1 = new CFException();
895     final CFException ex2 = new CFException();
896     final CompletableFuture<Integer> f = new CompletableFuture<>();
897     if (!createIncomplete) f.completeExceptionally(ex1);
898     final CompletableFuture<Integer> g = f.exceptionally
899     ((Throwable t) -> {
900 jsr166 1.95 ExecutionMode.SYNC.checkExecutionMode();
901 jsr166 1.46 threadAssertSame(t, ex1);
902     a.getAndIncrement();
903     throw ex2;
904     });
905     if (createIncomplete) f.completeExceptionally(ex1);
906    
907 jsr166 1.72 checkCompletedWithWrappedException(g, ex2);
908 jsr166 1.136 checkCompletedExceptionally(f, ex1);
909 jsr166 1.46 assertEquals(1, a.get());
910     }}
911    
912     /**
913 jsr166 1.75 * whenComplete action executes on normal completion, propagating
914     * source result.
915     */
916 jsr166 1.126 public void testWhenComplete_normalCompletion() {
917 jsr166 1.75 for (ExecutionMode m : ExecutionMode.values())
918     for (boolean createIncomplete : new boolean[] { true, false })
919     for (Integer v1 : new Integer[] { 1, null })
920     {
921     final AtomicInteger a = new AtomicInteger(0);
922     final CompletableFuture<Integer> f = new CompletableFuture<>();
923 jsr166 1.78 if (!createIncomplete) assertTrue(f.complete(v1));
924 jsr166 1.75 final CompletableFuture<Integer> g = m.whenComplete
925     (f,
926 jsr166 1.135 (Integer result, Throwable t) -> {
927 jsr166 1.75 m.checkExecutionMode();
928 jsr166 1.135 threadAssertSame(result, v1);
929 jsr166 1.75 threadAssertNull(t);
930     a.getAndIncrement();
931     });
932 jsr166 1.78 if (createIncomplete) assertTrue(f.complete(v1));
933 jsr166 1.75
934     checkCompletedNormally(g, v1);
935     checkCompletedNormally(f, v1);
936     assertEquals(1, a.get());
937     }}
938    
939     /**
940     * whenComplete action executes on exceptional completion, propagating
941     * source result.
942     */
943     public void testWhenComplete_exceptionalCompletion() {
944     for (ExecutionMode m : ExecutionMode.values())
945     for (boolean createIncomplete : new boolean[] { true, false })
946     {
947     final AtomicInteger a = new AtomicInteger(0);
948     final CFException ex = new CFException();
949     final CompletableFuture<Integer> f = new CompletableFuture<>();
950     if (!createIncomplete) f.completeExceptionally(ex);
951     final CompletableFuture<Integer> g = m.whenComplete
952     (f,
953 jsr166 1.135 (Integer result, Throwable t) -> {
954 jsr166 1.75 m.checkExecutionMode();
955 jsr166 1.135 threadAssertNull(result);
956 jsr166 1.75 threadAssertSame(t, ex);
957     a.getAndIncrement();
958     });
959     if (createIncomplete) f.completeExceptionally(ex);
960    
961     checkCompletedWithWrappedException(g, ex);
962     checkCompletedExceptionally(f, ex);
963     assertEquals(1, a.get());
964     }}
965    
966     /**
967     * whenComplete action executes on cancelled source, propagating
968     * CancellationException.
969     */
970     public void testWhenComplete_sourceCancelled() {
971     for (ExecutionMode m : ExecutionMode.values())
972     for (boolean mayInterruptIfRunning : new boolean[] { true, false })
973     for (boolean createIncomplete : new boolean[] { true, false })
974     {
975     final AtomicInteger a = new AtomicInteger(0);
976     final CompletableFuture<Integer> f = new CompletableFuture<>();
977     if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
978     final CompletableFuture<Integer> g = m.whenComplete
979     (f,
980 jsr166 1.135 (Integer result, Throwable t) -> {
981 jsr166 1.75 m.checkExecutionMode();
982 jsr166 1.135 threadAssertNull(result);
983 jsr166 1.75 threadAssertTrue(t instanceof CancellationException);
984     a.getAndIncrement();
985     });
986     if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
987    
988     checkCompletedWithWrappedCancellationException(g);
989     checkCancelled(f);
990     assertEquals(1, a.get());
991     }}
992    
993     /**
994     * If a whenComplete action throws an exception when triggered by
995     * a normal completion, it completes exceptionally
996     */
997 jsr166 1.132 public void testWhenComplete_sourceCompletedNormallyActionFailed() {
998 jsr166 1.75 for (boolean createIncomplete : new boolean[] { true, false })
999     for (ExecutionMode m : ExecutionMode.values())
1000     for (Integer v1 : new Integer[] { 1, null })
1001     {
1002     final AtomicInteger a = new AtomicInteger(0);
1003     final CFException ex = new CFException();
1004     final CompletableFuture<Integer> f = new CompletableFuture<>();
1005 jsr166 1.78 if (!createIncomplete) assertTrue(f.complete(v1));
1006 jsr166 1.75 final CompletableFuture<Integer> g = m.whenComplete
1007     (f,
1008 jsr166 1.135 (Integer result, Throwable t) -> {
1009 jsr166 1.75 m.checkExecutionMode();
1010 jsr166 1.135 threadAssertSame(result, v1);
1011 jsr166 1.75 threadAssertNull(t);
1012     a.getAndIncrement();
1013     throw ex;
1014     });
1015 jsr166 1.78 if (createIncomplete) assertTrue(f.complete(v1));
1016 jsr166 1.75
1017     checkCompletedWithWrappedException(g, ex);
1018     checkCompletedNormally(f, v1);
1019     assertEquals(1, a.get());
1020     }}
1021    
1022     /**
1023     * If a whenComplete action throws an exception when triggered by
1024     * a source completion that also throws an exception, the source
1025 jsr166 1.134 * exception takes precedence (unlike handle)
1026 jsr166 1.75 */
1027 jsr166 1.134 public void testWhenComplete_sourceFailedActionFailed() {
1028 jsr166 1.75 for (boolean createIncomplete : new boolean[] { true, false })
1029     for (ExecutionMode m : ExecutionMode.values())
1030     {
1031     final AtomicInteger a = new AtomicInteger(0);
1032     final CFException ex1 = new CFException();
1033     final CFException ex2 = new CFException();
1034     final CompletableFuture<Integer> f = new CompletableFuture<>();
1035    
1036     if (!createIncomplete) f.completeExceptionally(ex1);
1037     final CompletableFuture<Integer> g = m.whenComplete
1038     (f,
1039 jsr166 1.135 (Integer result, Throwable t) -> {
1040 jsr166 1.75 m.checkExecutionMode();
1041     threadAssertSame(t, ex1);
1042 jsr166 1.135 threadAssertNull(result);
1043 jsr166 1.75 a.getAndIncrement();
1044     throw ex2;
1045     });
1046     if (createIncomplete) f.completeExceptionally(ex1);
1047    
1048     checkCompletedWithWrappedException(g, ex1);
1049     checkCompletedExceptionally(f, ex1);
1050 jsr166 1.139 if (testImplementationDetails) {
1051     assertEquals(1, ex1.getSuppressed().length);
1052     assertSame(ex2, ex1.getSuppressed()[0]);
1053     }
1054 jsr166 1.75 assertEquals(1, a.get());
1055     }}
1056    
1057     /**
1058 jsr166 1.46 * handle action completes normally with function value on normal
1059     * completion of source
1060     */
1061     public void testHandle_normalCompletion() {
1062     for (ExecutionMode m : ExecutionMode.values())
1063     for (boolean createIncomplete : new boolean[] { true, false })
1064     for (Integer v1 : new Integer[] { 1, null })
1065     {
1066     final CompletableFuture<Integer> f = new CompletableFuture<>();
1067     final AtomicInteger a = new AtomicInteger(0);
1068 jsr166 1.78 if (!createIncomplete) assertTrue(f.complete(v1));
1069 jsr166 1.46 final CompletableFuture<Integer> g = m.handle
1070     (f,
1071 jsr166 1.135 (Integer result, Throwable t) -> {
1072 jsr166 1.57 m.checkExecutionMode();
1073 jsr166 1.135 threadAssertSame(result, v1);
1074 jsr166 1.46 threadAssertNull(t);
1075     a.getAndIncrement();
1076     return inc(v1);
1077     });
1078 jsr166 1.78 if (createIncomplete) assertTrue(f.complete(v1));
1079 jsr166 1.46
1080     checkCompletedNormally(g, inc(v1));
1081     checkCompletedNormally(f, v1);
1082     assertEquals(1, a.get());
1083     }}
1084    
1085     /**
1086     * handle action completes normally with function value on
1087     * exceptional completion of source
1088 dl 1.5 */
1089 jsr166 1.46 public void testHandle_exceptionalCompletion() {
1090     for (ExecutionMode m : ExecutionMode.values())
1091     for (boolean createIncomplete : new boolean[] { true, false })
1092     for (Integer v1 : new Integer[] { 1, null })
1093     {
1094     final CompletableFuture<Integer> f = new CompletableFuture<>();
1095     final AtomicInteger a = new AtomicInteger(0);
1096     final CFException ex = new CFException();
1097     if (!createIncomplete) f.completeExceptionally(ex);
1098     final CompletableFuture<Integer> g = m.handle
1099     (f,
1100 jsr166 1.135 (Integer result, Throwable t) -> {
1101 jsr166 1.57 m.checkExecutionMode();
1102 jsr166 1.135 threadAssertNull(result);
1103 jsr166 1.46 threadAssertSame(t, ex);
1104     a.getAndIncrement();
1105     return v1;
1106     });
1107     if (createIncomplete) f.completeExceptionally(ex);
1108 dl 1.5
1109 jsr166 1.46 checkCompletedNormally(g, v1);
1110 jsr166 1.72 checkCompletedExceptionally(f, ex);
1111 jsr166 1.46 assertEquals(1, a.get());
1112     }}
1113 dl 1.5
1114     /**
1115 jsr166 1.46 * handle action completes normally with function value on
1116     * cancelled source
1117 dl 1.5 */
1118 jsr166 1.46 public void testHandle_sourceCancelled() {
1119     for (ExecutionMode m : ExecutionMode.values())
1120     for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1121     for (boolean createIncomplete : new boolean[] { true, false })
1122     for (Integer v1 : new Integer[] { 1, null })
1123     {
1124     final CompletableFuture<Integer> f = new CompletableFuture<>();
1125     final AtomicInteger a = new AtomicInteger(0);
1126     if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1127     final CompletableFuture<Integer> g = m.handle
1128     (f,
1129 jsr166 1.135 (Integer result, Throwable t) -> {
1130 jsr166 1.57 m.checkExecutionMode();
1131 jsr166 1.135 threadAssertNull(result);
1132 jsr166 1.46 threadAssertTrue(t instanceof CancellationException);
1133     a.getAndIncrement();
1134     return v1;
1135     });
1136     if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1137    
1138     checkCompletedNormally(g, v1);
1139     checkCancelled(f);
1140     assertEquals(1, a.get());
1141     }}
1142 jsr166 1.15
1143 jsr166 1.46 /**
1144 jsr166 1.134 * If a "handle action" throws an exception when triggered by
1145     * a normal completion, it completes exceptionally
1146 jsr166 1.46 */
1147 jsr166 1.134 public void testHandle_sourceCompletedNormallyActionFailed() {
1148 jsr166 1.46 for (ExecutionMode m : ExecutionMode.values())
1149     for (boolean createIncomplete : new boolean[] { true, false })
1150 jsr166 1.134 for (Integer v1 : new Integer[] { 1, null })
1151 jsr166 1.46 {
1152     final CompletableFuture<Integer> f = new CompletableFuture<>();
1153     final AtomicInteger a = new AtomicInteger(0);
1154 jsr166 1.134 final CFException ex = new CFException();
1155     if (!createIncomplete) assertTrue(f.complete(v1));
1156 jsr166 1.46 final CompletableFuture<Integer> g = m.handle
1157     (f,
1158 jsr166 1.135 (Integer result, Throwable t) -> {
1159 jsr166 1.57 m.checkExecutionMode();
1160 jsr166 1.135 threadAssertSame(result, v1);
1161 jsr166 1.134 threadAssertNull(t);
1162 jsr166 1.46 a.getAndIncrement();
1163 jsr166 1.134 throw ex;
1164 jsr166 1.46 });
1165 jsr166 1.134 if (createIncomplete) assertTrue(f.complete(v1));
1166 dl 1.5
1167 jsr166 1.134 checkCompletedWithWrappedException(g, ex);
1168     checkCompletedNormally(f, v1);
1169 jsr166 1.46 assertEquals(1, a.get());
1170     }}
1171 jsr166 1.15
1172 jsr166 1.133 /**
1173     * If a "handle action" throws an exception when triggered by
1174 jsr166 1.134 * a source completion that also throws an exception, the action
1175     * exception takes precedence (unlike whenComplete)
1176 jsr166 1.133 */
1177 jsr166 1.134 public void testHandle_sourceFailedActionFailed() {
1178     for (boolean createIncomplete : new boolean[] { true, false })
1179 jsr166 1.46 for (ExecutionMode m : ExecutionMode.values())
1180     {
1181 jsr166 1.134 final AtomicInteger a = new AtomicInteger(0);
1182     final CFException ex1 = new CFException();
1183     final CFException ex2 = new CFException();
1184 jsr166 1.46 final CompletableFuture<Integer> f = new CompletableFuture<>();
1185 jsr166 1.134
1186     if (!createIncomplete) f.completeExceptionally(ex1);
1187 jsr166 1.46 final CompletableFuture<Integer> g = m.handle
1188     (f,
1189 jsr166 1.135 (Integer result, Throwable t) -> {
1190 jsr166 1.57 m.checkExecutionMode();
1191 jsr166 1.135 threadAssertNull(result);
1192 jsr166 1.134 threadAssertSame(ex1, t);
1193 jsr166 1.46 a.getAndIncrement();
1194 jsr166 1.134 throw ex2;
1195 jsr166 1.46 });
1196 jsr166 1.134 if (createIncomplete) f.completeExceptionally(ex1);
1197 jsr166 1.15
1198 jsr166 1.134 checkCompletedWithWrappedException(g, ex2);
1199     checkCompletedExceptionally(f, ex1);
1200 jsr166 1.46 assertEquals(1, a.get());
1201     }}
1202 dl 1.5
1203     /**
1204     * runAsync completes after running Runnable
1205     */
1206 jsr166 1.57 public void testRunAsync_normalCompletion() {
1207     ExecutionMode[] executionModes = {
1208     ExecutionMode.ASYNC,
1209     ExecutionMode.EXECUTOR,
1210     };
1211     for (ExecutionMode m : executionModes)
1212     {
1213     final Noop r = new Noop(m);
1214     final CompletableFuture<Void> f = m.runAsync(r);
1215 dl 1.5 assertNull(f.join());
1216 jsr166 1.14 checkCompletedNormally(f, null);
1217 jsr166 1.62 r.assertInvoked();
1218 jsr166 1.57 }}
1219 dl 1.5
1220     /**
1221     * failing runAsync completes exceptionally after running Runnable
1222     */
1223 jsr166 1.57 public void testRunAsync_exceptionalCompletion() {
1224     ExecutionMode[] executionModes = {
1225     ExecutionMode.ASYNC,
1226     ExecutionMode.EXECUTOR,
1227     };
1228     for (ExecutionMode m : executionModes)
1229     {
1230     final FailingRunnable r = new FailingRunnable(m);
1231     final CompletableFuture<Void> f = m.runAsync(r);
1232 jsr166 1.151 checkCompletedWithWrappedException(f, r.ex);
1233 jsr166 1.62 r.assertInvoked();
1234 jsr166 1.57 }}
1235 dl 1.5
1236     /**
1237     * supplyAsync completes with result of supplier
1238     */
1239 jsr166 1.58 public void testSupplyAsync_normalCompletion() {
1240     ExecutionMode[] executionModes = {
1241     ExecutionMode.ASYNC,
1242     ExecutionMode.EXECUTOR,
1243     };
1244     for (ExecutionMode m : executionModes)
1245     for (Integer v1 : new Integer[] { 1, null })
1246     {
1247     final IntegerSupplier r = new IntegerSupplier(m, v1);
1248     final CompletableFuture<Integer> f = m.supplyAsync(r);
1249     assertSame(v1, f.join());
1250     checkCompletedNormally(f, v1);
1251 jsr166 1.62 r.assertInvoked();
1252 jsr166 1.58 }}
1253 dl 1.5
1254     /**
1255     * Failing supplyAsync completes exceptionally
1256     */
1257 jsr166 1.58 public void testSupplyAsync_exceptionalCompletion() {
1258     ExecutionMode[] executionModes = {
1259     ExecutionMode.ASYNC,
1260     ExecutionMode.EXECUTOR,
1261     };
1262     for (ExecutionMode m : executionModes)
1263     {
1264     FailingSupplier r = new FailingSupplier(m);
1265     CompletableFuture<Integer> f = m.supplyAsync(r);
1266 jsr166 1.151 checkCompletedWithWrappedException(f, r.ex);
1267 jsr166 1.62 r.assertInvoked();
1268 jsr166 1.58 }}
1269 dl 1.5
1270 jsr166 1.7 // seq completion methods
1271 jsr166 1.6
1272 dl 1.5 /**
1273     * thenRun result completes normally after normal completion of source
1274     */
1275 jsr166 1.48 public void testThenRun_normalCompletion() {
1276     for (ExecutionMode m : ExecutionMode.values())
1277     for (Integer v1 : new Integer[] { 1, null })
1278     {
1279     final CompletableFuture<Integer> f = new CompletableFuture<>();
1280 jsr166 1.90 final Noop[] rs = new Noop[6];
1281     for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1282    
1283     final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1284     final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1285     final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1286     checkIncomplete(h0);
1287     checkIncomplete(h1);
1288     checkIncomplete(h2);
1289     assertTrue(f.complete(v1));
1290     final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1291     final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1292     final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1293 jsr166 1.23
1294 jsr166 1.90 checkCompletedNormally(h0, null);
1295     checkCompletedNormally(h1, null);
1296     checkCompletedNormally(h2, null);
1297     checkCompletedNormally(h3, null);
1298     checkCompletedNormally(h4, null);
1299     checkCompletedNormally(h5, null);
1300 jsr166 1.48 checkCompletedNormally(f, v1);
1301 jsr166 1.90 for (Noop r : rs) r.assertInvoked();
1302 jsr166 1.48 }}
1303 dl 1.5
1304     /**
1305     * thenRun result completes exceptionally after exceptional
1306     * completion of source
1307     */
1308 jsr166 1.48 public void testThenRun_exceptionalCompletion() {
1309     for (ExecutionMode m : ExecutionMode.values())
1310     {
1311     final CFException ex = new CFException();
1312     final CompletableFuture<Integer> f = new CompletableFuture<>();
1313 jsr166 1.90 final Noop[] rs = new Noop[6];
1314     for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1315    
1316     final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1317     final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1318     final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1319     checkIncomplete(h0);
1320     checkIncomplete(h1);
1321     checkIncomplete(h2);
1322     assertTrue(f.completeExceptionally(ex));
1323     final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1324     final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1325     final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1326 jsr166 1.23
1327 jsr166 1.90 checkCompletedWithWrappedException(h0, ex);
1328     checkCompletedWithWrappedException(h1, ex);
1329     checkCompletedWithWrappedException(h2, ex);
1330     checkCompletedWithWrappedException(h3, ex);
1331     checkCompletedWithWrappedException(h4, ex);
1332     checkCompletedWithWrappedException(h5, ex);
1333 jsr166 1.72 checkCompletedExceptionally(f, ex);
1334 jsr166 1.90 for (Noop r : rs) r.assertNotInvoked();
1335 jsr166 1.48 }}
1336    
1337     /**
1338     * thenRun result completes exceptionally if source cancelled
1339     */
1340     public void testThenRun_sourceCancelled() {
1341     for (ExecutionMode m : ExecutionMode.values())
1342     for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1343     {
1344     final CompletableFuture<Integer> f = new CompletableFuture<>();
1345 jsr166 1.90 final Noop[] rs = new Noop[6];
1346     for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1347    
1348     final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1349     final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1350     final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1351     checkIncomplete(h0);
1352     checkIncomplete(h1);
1353     checkIncomplete(h2);
1354     assertTrue(f.cancel(mayInterruptIfRunning));
1355     final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1356     final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1357     final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1358 jsr166 1.23
1359 jsr166 1.90 checkCompletedWithWrappedCancellationException(h0);
1360     checkCompletedWithWrappedCancellationException(h1);
1361     checkCompletedWithWrappedCancellationException(h2);
1362     checkCompletedWithWrappedCancellationException(h3);
1363     checkCompletedWithWrappedCancellationException(h4);
1364     checkCompletedWithWrappedCancellationException(h5);
1365 jsr166 1.48 checkCancelled(f);
1366 jsr166 1.90 for (Noop r : rs) r.assertNotInvoked();
1367 jsr166 1.48 }}
1368 dl 1.5
1369     /**
1370     * thenRun result completes exceptionally if action does
1371     */
1372 jsr166 1.48 public void testThenRun_actionFailed() {
1373     for (ExecutionMode m : ExecutionMode.values())
1374     for (Integer v1 : new Integer[] { 1, null })
1375     {
1376     final CompletableFuture<Integer> f = new CompletableFuture<>();
1377 jsr166 1.90 final FailingRunnable[] rs = new FailingRunnable[6];
1378     for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
1379    
1380     final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1381     final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1382     final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1383     assertTrue(f.complete(v1));
1384     final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1385     final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1386     final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1387 jsr166 1.23
1388 jsr166 1.151 checkCompletedWithWrappedException(h0, rs[0].ex);
1389     checkCompletedWithWrappedException(h1, rs[1].ex);
1390     checkCompletedWithWrappedException(h2, rs[2].ex);
1391     checkCompletedWithWrappedException(h3, rs[3].ex);
1392     checkCompletedWithWrappedException(h4, rs[4].ex);
1393     checkCompletedWithWrappedException(h5, rs[5].ex);
1394 jsr166 1.48 checkCompletedNormally(f, v1);
1395     }}
1396 dl 1.5
1397     /**
1398     * thenApply result completes normally after normal completion of source
1399     */
1400 jsr166 1.49 public void testThenApply_normalCompletion() {
1401     for (ExecutionMode m : ExecutionMode.values())
1402     for (Integer v1 : new Integer[] { 1, null })
1403     {
1404     final CompletableFuture<Integer> f = new CompletableFuture<>();
1405 jsr166 1.91 final IncFunction[] rs = new IncFunction[4];
1406     for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1407    
1408     final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1409     final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1410     checkIncomplete(h0);
1411     checkIncomplete(h1);
1412     assertTrue(f.complete(v1));
1413     final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1414     final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1415 jsr166 1.49
1416 jsr166 1.91 checkCompletedNormally(h0, inc(v1));
1417     checkCompletedNormally(h1, inc(v1));
1418     checkCompletedNormally(h2, inc(v1));
1419     checkCompletedNormally(h3, inc(v1));
1420 jsr166 1.49 checkCompletedNormally(f, v1);
1421 jsr166 1.91 for (IncFunction r : rs) r.assertValue(inc(v1));
1422 jsr166 1.49 }}
1423 dl 1.5
1424     /**
1425     * thenApply result completes exceptionally after exceptional
1426     * completion of source
1427     */
1428 jsr166 1.49 public void testThenApply_exceptionalCompletion() {
1429     for (ExecutionMode m : ExecutionMode.values())
1430     {
1431     final CFException ex = new CFException();
1432     final CompletableFuture<Integer> f = new CompletableFuture<>();
1433 jsr166 1.91 final IncFunction[] rs = new IncFunction[4];
1434     for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1435    
1436     final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1437     final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1438     assertTrue(f.completeExceptionally(ex));
1439     final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1440     final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1441 jsr166 1.49
1442 jsr166 1.91 checkCompletedWithWrappedException(h0, ex);
1443     checkCompletedWithWrappedException(h1, ex);
1444     checkCompletedWithWrappedException(h2, ex);
1445     checkCompletedWithWrappedException(h3, ex);
1446 jsr166 1.72 checkCompletedExceptionally(f, ex);
1447 jsr166 1.91 for (IncFunction r : rs) r.assertNotInvoked();
1448 jsr166 1.49 }}
1449 dl 1.5
1450     /**
1451 jsr166 1.49 * thenApply result completes exceptionally if source cancelled
1452 dl 1.5 */
1453 jsr166 1.49 public void testThenApply_sourceCancelled() {
1454     for (ExecutionMode m : ExecutionMode.values())
1455     for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1456     {
1457     final CompletableFuture<Integer> f = new CompletableFuture<>();
1458 jsr166 1.91 final IncFunction[] rs = new IncFunction[4];
1459     for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1460    
1461     final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1462     final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1463     assertTrue(f.cancel(mayInterruptIfRunning));
1464     final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1465     final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1466 jsr166 1.49
1467 jsr166 1.91 checkCompletedWithWrappedCancellationException(h0);
1468     checkCompletedWithWrappedCancellationException(h1);
1469     checkCompletedWithWrappedCancellationException(h2);
1470     checkCompletedWithWrappedCancellationException(h3);
1471 jsr166 1.49 checkCancelled(f);
1472 jsr166 1.91 for (IncFunction r : rs) r.assertNotInvoked();
1473 jsr166 1.49 }}
1474 dl 1.5
1475     /**
1476 jsr166 1.49 * thenApply result completes exceptionally if action does
1477 dl 1.5 */
1478 jsr166 1.49 public void testThenApply_actionFailed() {
1479     for (ExecutionMode m : ExecutionMode.values())
1480     for (Integer v1 : new Integer[] { 1, null })
1481     {
1482     final CompletableFuture<Integer> f = new CompletableFuture<>();
1483 jsr166 1.91 final FailingFunction[] rs = new FailingFunction[4];
1484     for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
1485    
1486     final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1487     final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1488     assertTrue(f.complete(v1));
1489     final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1490     final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1491 jsr166 1.49
1492 jsr166 1.151 checkCompletedWithWrappedException(h0, rs[0].ex);
1493     checkCompletedWithWrappedException(h1, rs[1].ex);
1494     checkCompletedWithWrappedException(h2, rs[2].ex);
1495     checkCompletedWithWrappedException(h3, rs[3].ex);
1496 jsr166 1.49 checkCompletedNormally(f, v1);
1497     }}
1498 dl 1.5
1499     /**
1500     * thenAccept result completes normally after normal completion of source
1501     */
1502 jsr166 1.50 public void testThenAccept_normalCompletion() {
1503     for (ExecutionMode m : ExecutionMode.values())
1504     for (Integer v1 : new Integer[] { 1, null })
1505     {
1506     final CompletableFuture<Integer> f = new CompletableFuture<>();
1507 jsr166 1.92 final NoopConsumer[] rs = new NoopConsumer[4];
1508     for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1509    
1510     final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1511     final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1512     checkIncomplete(h0);
1513     checkIncomplete(h1);
1514     assertTrue(f.complete(v1));
1515     final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1516     final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1517 jsr166 1.50
1518 jsr166 1.92 checkCompletedNormally(h0, null);
1519     checkCompletedNormally(h1, null);
1520     checkCompletedNormally(h2, null);
1521     checkCompletedNormally(h3, null);
1522 jsr166 1.50 checkCompletedNormally(f, v1);
1523 jsr166 1.92 for (NoopConsumer r : rs) r.assertValue(v1);
1524 jsr166 1.50 }}
1525 dl 1.5
1526     /**
1527     * thenAccept result completes exceptionally after exceptional
1528     * completion of source
1529     */
1530 jsr166 1.50 public void testThenAccept_exceptionalCompletion() {
1531     for (ExecutionMode m : ExecutionMode.values())
1532     {
1533     final CFException ex = new CFException();
1534     final CompletableFuture<Integer> f = new CompletableFuture<>();
1535 jsr166 1.92 final NoopConsumer[] rs = new NoopConsumer[4];
1536     for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1537    
1538     final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1539     final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1540     assertTrue(f.completeExceptionally(ex));
1541     final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1542     final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1543 jsr166 1.50
1544 jsr166 1.92 checkCompletedWithWrappedException(h0, ex);
1545     checkCompletedWithWrappedException(h1, ex);
1546     checkCompletedWithWrappedException(h2, ex);
1547     checkCompletedWithWrappedException(h3, ex);
1548 jsr166 1.72 checkCompletedExceptionally(f, ex);
1549 jsr166 1.92 for (NoopConsumer r : rs) r.assertNotInvoked();
1550 jsr166 1.50 }}
1551 dl 1.5
1552     /**
1553 jsr166 1.61 * thenAccept result completes exceptionally if source cancelled
1554 dl 1.5 */
1555 jsr166 1.61 public void testThenAccept_sourceCancelled() {
1556 jsr166 1.50 for (ExecutionMode m : ExecutionMode.values())
1557 jsr166 1.61 for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1558 jsr166 1.50 {
1559     final CompletableFuture<Integer> f = new CompletableFuture<>();
1560 jsr166 1.92 final NoopConsumer[] rs = new NoopConsumer[4];
1561     for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1562    
1563     final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1564     final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1565     assertTrue(f.cancel(mayInterruptIfRunning));
1566     final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1567     final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1568 jsr166 1.50
1569 jsr166 1.92 checkCompletedWithWrappedCancellationException(h0);
1570     checkCompletedWithWrappedCancellationException(h1);
1571     checkCompletedWithWrappedCancellationException(h2);
1572     checkCompletedWithWrappedCancellationException(h3);
1573 jsr166 1.61 checkCancelled(f);
1574 jsr166 1.92 for (NoopConsumer r : rs) r.assertNotInvoked();
1575 jsr166 1.50 }}
1576 dl 1.5
1577     /**
1578 jsr166 1.61 * thenAccept result completes exceptionally if action does
1579 dl 1.5 */
1580 jsr166 1.61 public void testThenAccept_actionFailed() {
1581 jsr166 1.50 for (ExecutionMode m : ExecutionMode.values())
1582 jsr166 1.61 for (Integer v1 : new Integer[] { 1, null })
1583 jsr166 1.50 {
1584     final CompletableFuture<Integer> f = new CompletableFuture<>();
1585 jsr166 1.92 final FailingConsumer[] rs = new FailingConsumer[4];
1586     for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
1587    
1588     final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1589     final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1590     assertTrue(f.complete(v1));
1591     final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1592     final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1593 jsr166 1.50
1594 jsr166 1.151 checkCompletedWithWrappedException(h0, rs[0].ex);
1595     checkCompletedWithWrappedException(h1, rs[1].ex);
1596     checkCompletedWithWrappedException(h2, rs[2].ex);
1597     checkCompletedWithWrappedException(h3, rs[3].ex);
1598 jsr166 1.61 checkCompletedNormally(f, v1);
1599 jsr166 1.50 }}
1600 dl 1.5
1601     /**
1602 jsr166 1.18 * thenCombine result completes normally after normal completion
1603     * of sources
1604 dl 1.5 */
1605 jsr166 1.51 public void testThenCombine_normalCompletion() {
1606     for (ExecutionMode m : ExecutionMode.values())
1607 jsr166 1.43 for (boolean fFirst : new boolean[] { true, false })
1608 jsr166 1.36 for (Integer v1 : new Integer[] { 1, null })
1609 jsr166 1.47 for (Integer v2 : new Integer[] { 2, null })
1610     {
1611 jsr166 1.36 final CompletableFuture<Integer> f = new CompletableFuture<>();
1612     final CompletableFuture<Integer> g = new CompletableFuture<>();
1613 jsr166 1.93 final SubtractFunction[] rs = new SubtractFunction[6];
1614     for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m);
1615 jsr166 1.79
1616     final CompletableFuture<Integer> fst = fFirst ? f : g;
1617     final CompletableFuture<Integer> snd = !fFirst ? f : g;
1618     final Integer w1 = fFirst ? v1 : v2;
1619     final Integer w2 = !fFirst ? v1 : v2;
1620    
1621 jsr166 1.93 final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]);
1622     final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]);
1623 jsr166 1.79 assertTrue(fst.complete(w1));
1624 jsr166 1.93 final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]);
1625     final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]);
1626     checkIncomplete(h0); rs[0].assertNotInvoked();
1627     checkIncomplete(h2); rs[2].assertNotInvoked();
1628     checkCompletedNormally(h1, subtract(w1, w1));
1629     checkCompletedNormally(h3, subtract(w1, w1));
1630     rs[1].assertValue(subtract(w1, w1));
1631     rs[3].assertValue(subtract(w1, w1));
1632 jsr166 1.79 assertTrue(snd.complete(w2));
1633 jsr166 1.93 final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]);
1634 jsr166 1.79
1635 jsr166 1.93 checkCompletedNormally(h0, subtract(v1, v2));
1636 jsr166 1.79 checkCompletedNormally(h2, subtract(v1, v2));
1637 jsr166 1.93 checkCompletedNormally(h4, subtract(v1, v2));
1638     rs[0].assertValue(subtract(v1, v2));
1639     rs[2].assertValue(subtract(v1, v2));
1640     rs[4].assertValue(subtract(v1, v2));
1641 jsr166 1.94
1642 jsr166 1.36 checkCompletedNormally(f, v1);
1643     checkCompletedNormally(g, v2);
1644 jsr166 1.47 }}
1645 dl 1.5
1646     /**
1647     * thenCombine result completes exceptionally after exceptional
1648     * completion of either source
1649     */
1650 jsr166 1.79 public void testThenCombine_exceptionalCompletion() throws Throwable {
1651 jsr166 1.36 for (ExecutionMode m : ExecutionMode.values())
1652 jsr166 1.52 for (boolean fFirst : new boolean[] { true, false })
1653 jsr166 1.79 for (boolean failFirst : new boolean[] { true, false })
1654 jsr166 1.47 for (Integer v1 : new Integer[] { 1, null })
1655     {
1656 jsr166 1.36 final CompletableFuture<Integer> f = new CompletableFuture<>();
1657     final CompletableFuture<Integer> g = new CompletableFuture<>();
1658     final CFException ex = new CFException();
1659 jsr166 1.79 final SubtractFunction r1 = new SubtractFunction(m);
1660     final SubtractFunction r2 = new SubtractFunction(m);
1661     final SubtractFunction r3 = new SubtractFunction(m);
1662    
1663     final CompletableFuture<Integer> fst = fFirst ? f : g;
1664     final CompletableFuture<Integer> snd = !fFirst ? f : g;
1665     final Callable<Boolean> complete1 = failFirst ?
1666     () -> fst.completeExceptionally(ex) :
1667     () -> fst.complete(v1);
1668     final Callable<Boolean> complete2 = failFirst ?
1669     () -> snd.complete(v1) :
1670     () -> snd.completeExceptionally(ex);
1671    
1672     final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1673     assertTrue(complete1.call());
1674     final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1675     checkIncomplete(h1);
1676     checkIncomplete(h2);
1677     assertTrue(complete2.call());
1678     final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1679 jsr166 1.18
1680 jsr166 1.79 checkCompletedWithWrappedException(h1, ex);
1681     checkCompletedWithWrappedException(h2, ex);
1682     checkCompletedWithWrappedException(h3, ex);
1683     r1.assertNotInvoked();
1684     r2.assertNotInvoked();
1685     r3.assertNotInvoked();
1686     checkCompletedNormally(failFirst ? snd : fst, v1);
1687     checkCompletedExceptionally(failFirst ? fst : snd, ex);
1688 jsr166 1.47 }}
1689 dl 1.5
1690     /**
1691     * thenCombine result completes exceptionally if either source cancelled
1692     */
1693 jsr166 1.79 public void testThenCombine_sourceCancelled() throws Throwable {
1694 jsr166 1.36 for (ExecutionMode m : ExecutionMode.values())
1695     for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1696 jsr166 1.52 for (boolean fFirst : new boolean[] { true, false })
1697 jsr166 1.79 for (boolean failFirst : new boolean[] { true, false })
1698 jsr166 1.47 for (Integer v1 : new Integer[] { 1, null })
1699     {
1700 jsr166 1.36 final CompletableFuture<Integer> f = new CompletableFuture<>();
1701     final CompletableFuture<Integer> g = new CompletableFuture<>();
1702 jsr166 1.79 final SubtractFunction r1 = new SubtractFunction(m);
1703     final SubtractFunction r2 = new SubtractFunction(m);
1704     final SubtractFunction r3 = new SubtractFunction(m);
1705 jsr166 1.18
1706 jsr166 1.79 final CompletableFuture<Integer> fst = fFirst ? f : g;
1707     final CompletableFuture<Integer> snd = !fFirst ? f : g;
1708     final Callable<Boolean> complete1 = failFirst ?
1709     () -> fst.cancel(mayInterruptIfRunning) :
1710     () -> fst.complete(v1);
1711     final Callable<Boolean> complete2 = failFirst ?
1712     () -> snd.complete(v1) :
1713     () -> snd.cancel(mayInterruptIfRunning);
1714    
1715     final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1716     assertTrue(complete1.call());
1717     final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1718     checkIncomplete(h1);
1719     checkIncomplete(h2);
1720     assertTrue(complete2.call());
1721     final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1722 jsr166 1.36
1723 jsr166 1.79 checkCompletedWithWrappedCancellationException(h1);
1724     checkCompletedWithWrappedCancellationException(h2);
1725     checkCompletedWithWrappedCancellationException(h3);
1726     r1.assertNotInvoked();
1727     r2.assertNotInvoked();
1728     r3.assertNotInvoked();
1729     checkCompletedNormally(failFirst ? snd : fst, v1);
1730     checkCancelled(failFirst ? fst : snd);
1731 jsr166 1.47 }}
1732 dl 1.5
1733     /**
1734 jsr166 1.61 * thenCombine result completes exceptionally if action does
1735     */
1736     public void testThenCombine_actionFailed() {
1737     for (ExecutionMode m : ExecutionMode.values())
1738     for (boolean fFirst : new boolean[] { true, false })
1739     for (Integer v1 : new Integer[] { 1, null })
1740     for (Integer v2 : new Integer[] { 2, null })
1741     {
1742     final CompletableFuture<Integer> f = new CompletableFuture<>();
1743     final CompletableFuture<Integer> g = new CompletableFuture<>();
1744 jsr166 1.79 final FailingBiFunction r1 = new FailingBiFunction(m);
1745     final FailingBiFunction r2 = new FailingBiFunction(m);
1746     final FailingBiFunction r3 = new FailingBiFunction(m);
1747    
1748     final CompletableFuture<Integer> fst = fFirst ? f : g;
1749     final CompletableFuture<Integer> snd = !fFirst ? f : g;
1750     final Integer w1 = fFirst ? v1 : v2;
1751     final Integer w2 = !fFirst ? v1 : v2;
1752    
1753     final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1754     assertTrue(fst.complete(w1));
1755     final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1756     assertTrue(snd.complete(w2));
1757     final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1758 jsr166 1.61
1759 jsr166 1.151 checkCompletedWithWrappedException(h1, r1.ex);
1760     checkCompletedWithWrappedException(h2, r2.ex);
1761     checkCompletedWithWrappedException(h3, r3.ex);
1762 jsr166 1.81 r1.assertInvoked();
1763     r2.assertInvoked();
1764     r3.assertInvoked();
1765 jsr166 1.61 checkCompletedNormally(f, v1);
1766     checkCompletedNormally(g, v2);
1767     }}
1768    
1769     /**
1770 dl 1.5 * thenAcceptBoth result completes normally after normal
1771     * completion of sources
1772     */
1773 jsr166 1.53 public void testThenAcceptBoth_normalCompletion() {
1774 jsr166 1.35 for (ExecutionMode m : ExecutionMode.values())
1775 jsr166 1.53 for (boolean fFirst : new boolean[] { true, false })
1776 jsr166 1.35 for (Integer v1 : new Integer[] { 1, null })
1777 jsr166 1.47 for (Integer v2 : new Integer[] { 2, null })
1778     {
1779 jsr166 1.35 final CompletableFuture<Integer> f = new CompletableFuture<>();
1780     final CompletableFuture<Integer> g = new CompletableFuture<>();
1781 jsr166 1.80 final SubtractAction r1 = new SubtractAction(m);
1782     final SubtractAction r2 = new SubtractAction(m);
1783     final SubtractAction r3 = new SubtractAction(m);
1784    
1785     final CompletableFuture<Integer> fst = fFirst ? f : g;
1786     final CompletableFuture<Integer> snd = !fFirst ? f : g;
1787     final Integer w1 = fFirst ? v1 : v2;
1788     final Integer w2 = !fFirst ? v1 : v2;
1789    
1790     final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1791     assertTrue(fst.complete(w1));
1792     final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1793     checkIncomplete(h1);
1794     checkIncomplete(h2);
1795     r1.assertNotInvoked();
1796     r2.assertNotInvoked();
1797     assertTrue(snd.complete(w2));
1798     final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1799 jsr166 1.35
1800 jsr166 1.80 checkCompletedNormally(h1, null);
1801     checkCompletedNormally(h2, null);
1802     checkCompletedNormally(h3, null);
1803     r1.assertValue(subtract(v1, v2));
1804     r2.assertValue(subtract(v1, v2));
1805     r3.assertValue(subtract(v1, v2));
1806 jsr166 1.35 checkCompletedNormally(f, v1);
1807     checkCompletedNormally(g, v2);
1808 jsr166 1.47 }}
1809 dl 1.5
1810     /**
1811     * thenAcceptBoth result completes exceptionally after exceptional
1812     * completion of either source
1813     */
1814 jsr166 1.80 public void testThenAcceptBoth_exceptionalCompletion() throws Throwable {
1815 jsr166 1.35 for (ExecutionMode m : ExecutionMode.values())
1816 jsr166 1.53 for (boolean fFirst : new boolean[] { true, false })
1817 jsr166 1.80 for (boolean failFirst : new boolean[] { true, false })
1818 jsr166 1.47 for (Integer v1 : new Integer[] { 1, null })
1819     {
1820 jsr166 1.35 final CompletableFuture<Integer> f = new CompletableFuture<>();
1821     final CompletableFuture<Integer> g = new CompletableFuture<>();
1822     final CFException ex = new CFException();
1823 jsr166 1.80 final SubtractAction r1 = new SubtractAction(m);
1824     final SubtractAction r2 = new SubtractAction(m);
1825     final SubtractAction r3 = new SubtractAction(m);
1826    
1827     final CompletableFuture<Integer> fst = fFirst ? f : g;
1828     final CompletableFuture<Integer> snd = !fFirst ? f : g;
1829     final Callable<Boolean> complete1 = failFirst ?
1830     () -> fst.completeExceptionally(ex) :
1831     () -> fst.complete(v1);
1832     final Callable<Boolean> complete2 = failFirst ?
1833     () -> snd.complete(v1) :
1834     () -> snd.completeExceptionally(ex);
1835    
1836     final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1837     assertTrue(complete1.call());
1838     final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1839     checkIncomplete(h1);
1840     checkIncomplete(h2);
1841     assertTrue(complete2.call());
1842     final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1843 jsr166 1.35
1844 jsr166 1.80 checkCompletedWithWrappedException(h1, ex);
1845     checkCompletedWithWrappedException(h2, ex);
1846     checkCompletedWithWrappedException(h3, ex);
1847     r1.assertNotInvoked();
1848     r2.assertNotInvoked();
1849     r3.assertNotInvoked();
1850     checkCompletedNormally(failFirst ? snd : fst, v1);
1851     checkCompletedExceptionally(failFirst ? fst : snd, ex);
1852 jsr166 1.47 }}
1853 dl 1.5
1854     /**
1855     * thenAcceptBoth result completes exceptionally if either source cancelled
1856     */
1857 jsr166 1.80 public void testThenAcceptBoth_sourceCancelled() throws Throwable {
1858 jsr166 1.35 for (ExecutionMode m : ExecutionMode.values())
1859     for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1860 jsr166 1.53 for (boolean fFirst : new boolean[] { true, false })
1861 jsr166 1.80 for (boolean failFirst : new boolean[] { true, false })
1862 jsr166 1.47 for (Integer v1 : new Integer[] { 1, null })
1863     {
1864 jsr166 1.35 final CompletableFuture<Integer> f = new CompletableFuture<>();
1865     final CompletableFuture<Integer> g = new CompletableFuture<>();
1866 jsr166 1.80 final SubtractAction r1 = new SubtractAction(m);
1867     final SubtractAction r2 = new SubtractAction(m);
1868     final SubtractAction r3 = new SubtractAction(m);
1869    
1870     final CompletableFuture<Integer> fst = fFirst ? f : g;
1871     final CompletableFuture<Integer> snd = !fFirst ? f : g;
1872     final Callable<Boolean> complete1 = failFirst ?
1873     () -> fst.cancel(mayInterruptIfRunning) :
1874     () -> fst.complete(v1);
1875     final Callable<Boolean> complete2 = failFirst ?
1876     () -> snd.complete(v1) :
1877     () -> snd.cancel(mayInterruptIfRunning);
1878 jsr166 1.35
1879 jsr166 1.80 final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1880     assertTrue(complete1.call());
1881     final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1882     checkIncomplete(h1);
1883     checkIncomplete(h2);
1884     assertTrue(complete2.call());
1885     final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1886 jsr166 1.22
1887 jsr166 1.80 checkCompletedWithWrappedCancellationException(h1);
1888     checkCompletedWithWrappedCancellationException(h2);
1889     checkCompletedWithWrappedCancellationException(h3);
1890     r1.assertNotInvoked();
1891     r2.assertNotInvoked();
1892     r3.assertNotInvoked();
1893     checkCompletedNormally(failFirst ? snd : fst, v1);
1894     checkCancelled(failFirst ? fst : snd);
1895 jsr166 1.47 }}
1896 jsr166 1.34
1897     /**
1898 jsr166 1.61 * thenAcceptBoth result completes exceptionally if action does
1899     */
1900     public void testThenAcceptBoth_actionFailed() {
1901     for (ExecutionMode m : ExecutionMode.values())
1902     for (boolean fFirst : new boolean[] { true, false })
1903     for (Integer v1 : new Integer[] { 1, null })
1904     for (Integer v2 : new Integer[] { 2, null })
1905     {
1906     final CompletableFuture<Integer> f = new CompletableFuture<>();
1907     final CompletableFuture<Integer> g = new CompletableFuture<>();
1908 jsr166 1.80 final FailingBiConsumer r1 = new FailingBiConsumer(m);
1909     final FailingBiConsumer r2 = new FailingBiConsumer(m);
1910     final FailingBiConsumer r3 = new FailingBiConsumer(m);
1911    
1912     final CompletableFuture<Integer> fst = fFirst ? f : g;
1913     final CompletableFuture<Integer> snd = !fFirst ? f : g;
1914     final Integer w1 = fFirst ? v1 : v2;
1915     final Integer w2 = !fFirst ? v1 : v2;
1916    
1917     final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1918     assertTrue(fst.complete(w1));
1919     final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1920     assertTrue(snd.complete(w2));
1921     final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1922 jsr166 1.61
1923 jsr166 1.151 checkCompletedWithWrappedException(h1, r1.ex);
1924     checkCompletedWithWrappedException(h2, r2.ex);
1925     checkCompletedWithWrappedException(h3, r3.ex);
1926 jsr166 1.81 r1.assertInvoked();
1927     r2.assertInvoked();
1928     r3.assertInvoked();
1929 jsr166 1.61 checkCompletedNormally(f, v1);
1930     checkCompletedNormally(g, v2);
1931     }}
1932    
1933     /**
1934 dl 1.5 * runAfterBoth result completes normally after normal
1935     * completion of sources
1936     */
1937 jsr166 1.54 public void testRunAfterBoth_normalCompletion() {
1938 jsr166 1.34 for (ExecutionMode m : ExecutionMode.values())
1939 jsr166 1.54 for (boolean fFirst : new boolean[] { true, false })
1940 jsr166 1.33 for (Integer v1 : new Integer[] { 1, null })
1941 jsr166 1.47 for (Integer v2 : new Integer[] { 2, null })
1942     {
1943 jsr166 1.33 final CompletableFuture<Integer> f = new CompletableFuture<>();
1944     final CompletableFuture<Integer> g = new CompletableFuture<>();
1945 jsr166 1.81 final Noop r1 = new Noop(m);
1946     final Noop r2 = new Noop(m);
1947     final Noop r3 = new Noop(m);
1948    
1949     final CompletableFuture<Integer> fst = fFirst ? f : g;
1950     final CompletableFuture<Integer> snd = !fFirst ? f : g;
1951     final Integer w1 = fFirst ? v1 : v2;
1952     final Integer w2 = !fFirst ? v1 : v2;
1953 jsr166 1.33
1954 jsr166 1.81 final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1955     assertTrue(fst.complete(w1));
1956     final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1957     checkIncomplete(h1);
1958     checkIncomplete(h2);
1959     r1.assertNotInvoked();
1960     r2.assertNotInvoked();
1961     assertTrue(snd.complete(w2));
1962     final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
1963 dl 1.5
1964 jsr166 1.81 checkCompletedNormally(h1, null);
1965     checkCompletedNormally(h2, null);
1966     checkCompletedNormally(h3, null);
1967     r1.assertInvoked();
1968     r2.assertInvoked();
1969     r3.assertInvoked();
1970 jsr166 1.33 checkCompletedNormally(f, v1);
1971     checkCompletedNormally(g, v2);
1972 jsr166 1.47 }}
1973 dl 1.5
1974     /**
1975     * runAfterBoth result completes exceptionally after exceptional
1976     * completion of either source
1977     */
1978 jsr166 1.81 public void testRunAfterBoth_exceptionalCompletion() throws Throwable {
1979 jsr166 1.34 for (ExecutionMode m : ExecutionMode.values())
1980 jsr166 1.54 for (boolean fFirst : new boolean[] { true, false })
1981 jsr166 1.81 for (boolean failFirst : new boolean[] { true, false })
1982 jsr166 1.47 for (Integer v1 : new Integer[] { 1, null })
1983     {
1984 jsr166 1.33 final CompletableFuture<Integer> f = new CompletableFuture<>();
1985     final CompletableFuture<Integer> g = new CompletableFuture<>();
1986     final CFException ex = new CFException();
1987 jsr166 1.81 final Noop r1 = new Noop(m);
1988     final Noop r2 = new Noop(m);
1989     final Noop r3 = new Noop(m);
1990 jsr166 1.33
1991 jsr166 1.81 final CompletableFuture<Integer> fst = fFirst ? f : g;
1992     final CompletableFuture<Integer> snd = !fFirst ? f : g;
1993     final Callable<Boolean> complete1 = failFirst ?
1994     () -> fst.completeExceptionally(ex) :
1995     () -> fst.complete(v1);
1996     final Callable<Boolean> complete2 = failFirst ?
1997     () -> snd.complete(v1) :
1998     () -> snd.completeExceptionally(ex);
1999    
2000     final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2001     assertTrue(complete1.call());
2002     final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2003     checkIncomplete(h1);
2004     checkIncomplete(h2);
2005     assertTrue(complete2.call());
2006     final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2007 dl 1.5
2008 jsr166 1.81 checkCompletedWithWrappedException(h1, ex);
2009     checkCompletedWithWrappedException(h2, ex);
2010     checkCompletedWithWrappedException(h3, ex);
2011     r1.assertNotInvoked();
2012     r2.assertNotInvoked();
2013     r3.assertNotInvoked();
2014     checkCompletedNormally(failFirst ? snd : fst, v1);
2015     checkCompletedExceptionally(failFirst ? fst : snd, ex);
2016 jsr166 1.47 }}
2017 dl 1.5
2018 jsr166 1.4 /**
2019 dl 1.5 * runAfterBoth result completes exceptionally if either source cancelled
2020 jsr166 1.4 */
2021 jsr166 1.81 public void testRunAfterBoth_sourceCancelled() throws Throwable {
2022 jsr166 1.34 for (ExecutionMode m : ExecutionMode.values())
2023 jsr166 1.33 for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2024 jsr166 1.54 for (boolean fFirst : new boolean[] { true, false })
2025 jsr166 1.81 for (boolean failFirst : new boolean[] { true, false })
2026 jsr166 1.47 for (Integer v1 : new Integer[] { 1, null })
2027     {
2028 jsr166 1.33 final CompletableFuture<Integer> f = new CompletableFuture<>();
2029     final CompletableFuture<Integer> g = new CompletableFuture<>();
2030 jsr166 1.81 final Noop r1 = new Noop(m);
2031     final Noop r2 = new Noop(m);
2032     final Noop r3 = new Noop(m);
2033 jsr166 1.33
2034 jsr166 1.81 final CompletableFuture<Integer> fst = fFirst ? f : g;
2035     final CompletableFuture<Integer> snd = !fFirst ? f : g;
2036     final Callable<Boolean> complete1 = failFirst ?
2037     () -> fst.cancel(mayInterruptIfRunning) :
2038     () -> fst.complete(v1);
2039     final Callable<Boolean> complete2 = failFirst ?
2040     () -> snd.complete(v1) :
2041     () -> snd.cancel(mayInterruptIfRunning);
2042    
2043     final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2044     assertTrue(complete1.call());
2045     final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2046     checkIncomplete(h1);
2047     checkIncomplete(h2);
2048     assertTrue(complete2.call());
2049     final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2050 jsr166 1.33
2051 jsr166 1.81 checkCompletedWithWrappedCancellationException(h1);
2052     checkCompletedWithWrappedCancellationException(h2);
2053     checkCompletedWithWrappedCancellationException(h3);
2054     r1.assertNotInvoked();
2055     r2.assertNotInvoked();
2056     r3.assertNotInvoked();
2057     checkCompletedNormally(failFirst ? snd : fst, v1);
2058     checkCancelled(failFirst ? fst : snd);
2059 jsr166 1.47 }}
2060 dl 1.5
2061     /**
2062 jsr166 1.61 * runAfterBoth result completes exceptionally if action does
2063     */
2064     public void testRunAfterBoth_actionFailed() {
2065     for (ExecutionMode m : ExecutionMode.values())
2066     for (boolean fFirst : new boolean[] { true, false })
2067     for (Integer v1 : new Integer[] { 1, null })
2068     for (Integer v2 : new Integer[] { 2, null })
2069     {
2070     final CompletableFuture<Integer> f = new CompletableFuture<>();
2071     final CompletableFuture<Integer> g = new CompletableFuture<>();
2072 jsr166 1.62 final FailingRunnable r1 = new FailingRunnable(m);
2073     final FailingRunnable r2 = new FailingRunnable(m);
2074 jsr166 1.81 final FailingRunnable r3 = new FailingRunnable(m);
2075 jsr166 1.61
2076 jsr166 1.81 final CompletableFuture<Integer> fst = fFirst ? f : g;
2077     final CompletableFuture<Integer> snd = !fFirst ? f : g;
2078     final Integer w1 = fFirst ? v1 : v2;
2079     final Integer w2 = !fFirst ? v1 : v2;
2080    
2081     final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2082     assertTrue(fst.complete(w1));
2083     final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2084     assertTrue(snd.complete(w2));
2085     final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2086 jsr166 1.61
2087 jsr166 1.151 checkCompletedWithWrappedException(h1, r1.ex);
2088     checkCompletedWithWrappedException(h2, r2.ex);
2089     checkCompletedWithWrappedException(h3, r3.ex);
2090 jsr166 1.81 r1.assertInvoked();
2091     r2.assertInvoked();
2092     r3.assertInvoked();
2093 jsr166 1.61 checkCompletedNormally(f, v1);
2094     checkCompletedNormally(g, v2);
2095     }}
2096    
2097     /**
2098 dl 1.5 * applyToEither result completes normally after normal completion
2099     * of either source
2100     */
2101 jsr166 1.54 public void testApplyToEither_normalCompletion() {
2102 jsr166 1.39 for (ExecutionMode m : ExecutionMode.values())
2103     for (Integer v1 : new Integer[] { 1, null })
2104 jsr166 1.47 for (Integer v2 : new Integer[] { 2, null })
2105     {
2106 jsr166 1.39 final CompletableFuture<Integer> f = new CompletableFuture<>();
2107     final CompletableFuture<Integer> g = new CompletableFuture<>();
2108 jsr166 1.62 final IncFunction[] rs = new IncFunction[6];
2109     for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2110 jsr166 1.54
2111 jsr166 1.62 final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2112     final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2113     checkIncomplete(h0);
2114     checkIncomplete(h1);
2115     rs[0].assertNotInvoked();
2116     rs[1].assertNotInvoked();
2117     f.complete(v1);
2118     checkCompletedNormally(h0, inc(v1));
2119     checkCompletedNormally(h1, inc(v1));
2120     final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2121     final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2122     checkCompletedNormally(h2, inc(v1));
2123     checkCompletedNormally(h3, inc(v1));
2124     g.complete(v2);
2125 jsr166 1.39
2126 jsr166 1.62 // unspecified behavior - both source completions available
2127     final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2128     final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2129     rs[4].assertValue(h4.join());
2130     rs[5].assertValue(h5.join());
2131     assertTrue(Objects.equals(inc(v1), h4.join()) ||
2132     Objects.equals(inc(v2), h4.join()));
2133     assertTrue(Objects.equals(inc(v1), h5.join()) ||
2134     Objects.equals(inc(v2), h5.join()));
2135 jsr166 1.39
2136     checkCompletedNormally(f, v1);
2137     checkCompletedNormally(g, v2);
2138 jsr166 1.62 checkCompletedNormally(h0, inc(v1));
2139     checkCompletedNormally(h1, inc(v1));
2140     checkCompletedNormally(h2, inc(v1));
2141     checkCompletedNormally(h3, inc(v1));
2142     for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1));
2143 jsr166 1.47 }}
2144 dl 1.5
2145     /**
2146     * applyToEither result completes exceptionally after exceptional
2147     * completion of either source
2148     */
2149 jsr166 1.62 public void testApplyToEither_exceptionalCompletion() {
2150 jsr166 1.39 for (ExecutionMode m : ExecutionMode.values())
2151 jsr166 1.47 for (Integer v1 : new Integer[] { 1, null })
2152     {
2153 jsr166 1.39 final CompletableFuture<Integer> f = new CompletableFuture<>();
2154     final CompletableFuture<Integer> g = new CompletableFuture<>();
2155 jsr166 1.54 final CFException ex = new CFException();
2156 jsr166 1.62 final IncFunction[] rs = new IncFunction[6];
2157     for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2158    
2159     final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2160     final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2161     checkIncomplete(h0);
2162     checkIncomplete(h1);
2163     rs[0].assertNotInvoked();
2164     rs[1].assertNotInvoked();
2165     f.completeExceptionally(ex);
2166 jsr166 1.72 checkCompletedWithWrappedException(h0, ex);
2167     checkCompletedWithWrappedException(h1, ex);
2168 jsr166 1.62 final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2169     final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2170 jsr166 1.72 checkCompletedWithWrappedException(h2, ex);
2171     checkCompletedWithWrappedException(h3, ex);
2172 jsr166 1.62 g.complete(v1);
2173 jsr166 1.54
2174 jsr166 1.62 // unspecified behavior - both source completions available
2175     final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2176     final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2177     try {
2178     assertEquals(inc(v1), h4.join());
2179 jsr166 1.66 rs[4].assertValue(inc(v1));
2180 jsr166 1.62 } catch (CompletionException ok) {
2181 jsr166 1.72 checkCompletedWithWrappedException(h4, ex);
2182 jsr166 1.62 rs[4].assertNotInvoked();
2183     }
2184     try {
2185     assertEquals(inc(v1), h5.join());
2186 jsr166 1.66 rs[5].assertValue(inc(v1));
2187 jsr166 1.62 } catch (CompletionException ok) {
2188 jsr166 1.72 checkCompletedWithWrappedException(h5, ex);
2189 jsr166 1.62 rs[5].assertNotInvoked();
2190 jsr166 1.54 }
2191 jsr166 1.39
2192 jsr166 1.72 checkCompletedExceptionally(f, ex);
2193 jsr166 1.62 checkCompletedNormally(g, v1);
2194 jsr166 1.72 checkCompletedWithWrappedException(h0, ex);
2195     checkCompletedWithWrappedException(h1, ex);
2196     checkCompletedWithWrappedException(h2, ex);
2197     checkCompletedWithWrappedException(h3, ex);
2198     checkCompletedWithWrappedException(h4, ex);
2199 jsr166 1.62 for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2200 jsr166 1.47 }}
2201 jsr166 1.39
2202 jsr166 1.66 public void testApplyToEither_exceptionalCompletion2() {
2203     for (ExecutionMode m : ExecutionMode.values())
2204     for (boolean fFirst : new boolean[] { true, false })
2205     for (Integer v1 : new Integer[] { 1, null })
2206     {
2207     final CompletableFuture<Integer> f = new CompletableFuture<>();
2208     final CompletableFuture<Integer> g = new CompletableFuture<>();
2209     final CFException ex = new CFException();
2210     final IncFunction[] rs = new IncFunction[6];
2211     for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2212    
2213     final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2214     final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2215 jsr166 1.78 assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2216     assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2217 jsr166 1.66 final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2218     final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2219    
2220     // unspecified behavior - both source completions available
2221     try {
2222     assertEquals(inc(v1), h0.join());
2223     rs[0].assertValue(inc(v1));
2224     } catch (CompletionException ok) {
2225 jsr166 1.72 checkCompletedWithWrappedException(h0, ex);
2226 jsr166 1.66 rs[0].assertNotInvoked();
2227     }
2228     try {
2229     assertEquals(inc(v1), h1.join());
2230     rs[1].assertValue(inc(v1));
2231     } catch (CompletionException ok) {
2232 jsr166 1.72 checkCompletedWithWrappedException(h1, ex);
2233 jsr166 1.66 rs[1].assertNotInvoked();
2234     }
2235     try {
2236     assertEquals(inc(v1), h2.join());
2237     rs[2].assertValue(inc(v1));
2238     } catch (CompletionException ok) {
2239 jsr166 1.72 checkCompletedWithWrappedException(h2, ex);
2240 jsr166 1.66 rs[2].assertNotInvoked();
2241     }
2242     try {
2243     assertEquals(inc(v1), h3.join());
2244     rs[3].assertValue(inc(v1));
2245     } catch (CompletionException ok) {
2246 jsr166 1.72 checkCompletedWithWrappedException(h3, ex);
2247 jsr166 1.66 rs[3].assertNotInvoked();
2248     }
2249    
2250     checkCompletedNormally(f, v1);
2251 jsr166 1.72 checkCompletedExceptionally(g, ex);
2252 jsr166 1.66 }}
2253    
2254 jsr166 1.62 /**
2255     * applyToEither result completes exceptionally if either source cancelled
2256     */
2257     public void testApplyToEither_sourceCancelled() {
2258 jsr166 1.39 for (ExecutionMode m : ExecutionMode.values())
2259 jsr166 1.62 for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2260 jsr166 1.47 for (Integer v1 : new Integer[] { 1, null })
2261     {
2262 jsr166 1.39 final CompletableFuture<Integer> f = new CompletableFuture<>();
2263     final CompletableFuture<Integer> g = new CompletableFuture<>();
2264 jsr166 1.62 final IncFunction[] rs = new IncFunction[6];
2265     for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2266    
2267     final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2268     final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2269     checkIncomplete(h0);
2270     checkIncomplete(h1);
2271     rs[0].assertNotInvoked();
2272     rs[1].assertNotInvoked();
2273     f.cancel(mayInterruptIfRunning);
2274     checkCompletedWithWrappedCancellationException(h0);
2275     checkCompletedWithWrappedCancellationException(h1);
2276     final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2277     final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2278     checkCompletedWithWrappedCancellationException(h2);
2279     checkCompletedWithWrappedCancellationException(h3);
2280     g.complete(v1);
2281 jsr166 1.39
2282 jsr166 1.62 // unspecified behavior - both source completions available
2283     final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2284     final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2285 jsr166 1.39 try {
2286 jsr166 1.62 assertEquals(inc(v1), h4.join());
2287 jsr166 1.66 rs[4].assertValue(inc(v1));
2288 jsr166 1.39 } catch (CompletionException ok) {
2289 jsr166 1.62 checkCompletedWithWrappedCancellationException(h4);
2290     rs[4].assertNotInvoked();
2291 jsr166 1.39 }
2292     try {
2293 jsr166 1.62 assertEquals(inc(v1), h5.join());
2294 jsr166 1.66 rs[5].assertValue(inc(v1));
2295 jsr166 1.39 } catch (CompletionException ok) {
2296 jsr166 1.62 checkCompletedWithWrappedCancellationException(h5);
2297     rs[5].assertNotInvoked();
2298 jsr166 1.39 }
2299 dl 1.5
2300 jsr166 1.62 checkCancelled(f);
2301     checkCompletedNormally(g, v1);
2302     checkCompletedWithWrappedCancellationException(h0);
2303     checkCompletedWithWrappedCancellationException(h1);
2304     checkCompletedWithWrappedCancellationException(h2);
2305     checkCompletedWithWrappedCancellationException(h3);
2306     for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2307 jsr166 1.47 }}
2308 dl 1.5
2309 jsr166 1.67 public void testApplyToEither_sourceCancelled2() {
2310     for (ExecutionMode m : ExecutionMode.values())
2311     for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2312     for (boolean fFirst : new boolean[] { true, false })
2313     for (Integer v1 : new Integer[] { 1, null })
2314     {
2315     final CompletableFuture<Integer> f = new CompletableFuture<>();
2316     final CompletableFuture<Integer> g = new CompletableFuture<>();
2317     final IncFunction[] rs = new IncFunction[6];
2318     for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2319    
2320     final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2321     final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2322 jsr166 1.78 assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2323     assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2324 jsr166 1.67 final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2325     final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2326    
2327     // unspecified behavior - both source completions available
2328     try {
2329     assertEquals(inc(v1), h0.join());
2330     rs[0].assertValue(inc(v1));
2331     } catch (CompletionException ok) {
2332     checkCompletedWithWrappedCancellationException(h0);
2333     rs[0].assertNotInvoked();
2334     }
2335     try {
2336     assertEquals(inc(v1), h1.join());
2337     rs[1].assertValue(inc(v1));
2338     } catch (CompletionException ok) {
2339     checkCompletedWithWrappedCancellationException(h1);
2340     rs[1].assertNotInvoked();
2341     }
2342     try {
2343     assertEquals(inc(v1), h2.join());
2344     rs[2].assertValue(inc(v1));
2345     } catch (CompletionException ok) {
2346     checkCompletedWithWrappedCancellationException(h2);
2347     rs[2].assertNotInvoked();
2348     }
2349     try {
2350     assertEquals(inc(v1), h3.join());
2351     rs[3].assertValue(inc(v1));
2352     } catch (CompletionException ok) {
2353     checkCompletedWithWrappedCancellationException(h3);
2354     rs[3].assertNotInvoked();
2355     }
2356    
2357     checkCompletedNormally(f, v1);
2358     checkCancelled(g);
2359     }}
2360    
2361 dl 1.5 /**
2362     * applyToEither result completes exceptionally if action does
2363     */
2364 jsr166 1.62 public void testApplyToEither_actionFailed() {
2365 jsr166 1.39 for (ExecutionMode m : ExecutionMode.values())
2366     for (Integer v1 : new Integer[] { 1, null })
2367 jsr166 1.47 for (Integer v2 : new Integer[] { 2, null })
2368     {
2369 jsr166 1.39 final CompletableFuture<Integer> f = new CompletableFuture<>();
2370     final CompletableFuture<Integer> g = new CompletableFuture<>();
2371 jsr166 1.62 final FailingFunction[] rs = new FailingFunction[6];
2372     for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
2373 jsr166 1.39
2374 jsr166 1.62 final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2375     final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2376 jsr166 1.39 f.complete(v1);
2377 jsr166 1.62 final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2378     final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2379 jsr166 1.151 checkCompletedWithWrappedException(h0, rs[0].ex);
2380     checkCompletedWithWrappedException(h1, rs[1].ex);
2381     checkCompletedWithWrappedException(h2, rs[2].ex);
2382     checkCompletedWithWrappedException(h3, rs[3].ex);
2383 jsr166 1.63 for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2384    
2385     g.complete(v2);
2386    
2387     // unspecified behavior - both source completions available
2388     final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2389     final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2390    
2391 jsr166 1.151 checkCompletedWithWrappedException(h4, rs[4].ex);
2392 jsr166 1.63 assertTrue(Objects.equals(v1, rs[4].value) ||
2393     Objects.equals(v2, rs[4].value));
2394 jsr166 1.151 checkCompletedWithWrappedException(h5, rs[5].ex);
2395 jsr166 1.63 assertTrue(Objects.equals(v1, rs[5].value) ||
2396     Objects.equals(v2, rs[5].value));
2397    
2398     checkCompletedNormally(f, v1);
2399     checkCompletedNormally(g, v2);
2400 jsr166 1.47 }}
2401 dl 1.5
2402     /**
2403     * acceptEither result completes normally after normal completion
2404     * of either source
2405     */
2406 jsr166 1.63 public void testAcceptEither_normalCompletion() {
2407 jsr166 1.40 for (ExecutionMode m : ExecutionMode.values())
2408     for (Integer v1 : new Integer[] { 1, null })
2409 jsr166 1.47 for (Integer v2 : new Integer[] { 2, null })
2410     {
2411 jsr166 1.40 final CompletableFuture<Integer> f = new CompletableFuture<>();
2412     final CompletableFuture<Integer> g = new CompletableFuture<>();
2413 jsr166 1.64 final NoopConsumer[] rs = new NoopConsumer[6];
2414     for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2415 jsr166 1.40
2416 jsr166 1.63 final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2417     final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2418     checkIncomplete(h0);
2419     checkIncomplete(h1);
2420     rs[0].assertNotInvoked();
2421     rs[1].assertNotInvoked();
2422 jsr166 1.40 f.complete(v1);
2423 jsr166 1.63 checkCompletedNormally(h0, null);
2424     checkCompletedNormally(h1, null);
2425 jsr166 1.64 rs[0].assertValue(v1);
2426     rs[1].assertValue(v1);
2427 jsr166 1.63 final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2428     final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2429     checkCompletedNormally(h2, null);
2430     checkCompletedNormally(h3, null);
2431 jsr166 1.64 rs[2].assertValue(v1);
2432     rs[3].assertValue(v1);
2433 jsr166 1.40 g.complete(v2);
2434    
2435 jsr166 1.63 // unspecified behavior - both source completions available
2436     final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2437     final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2438     checkCompletedNormally(h4, null);
2439     checkCompletedNormally(h5, null);
2440 jsr166 1.64 assertTrue(Objects.equals(v1, rs[4].value) ||
2441     Objects.equals(v2, rs[4].value));
2442     assertTrue(Objects.equals(v1, rs[5].value) ||
2443     Objects.equals(v2, rs[5].value));
2444 jsr166 1.40
2445     checkCompletedNormally(f, v1);
2446     checkCompletedNormally(g, v2);
2447 jsr166 1.63 checkCompletedNormally(h0, null);
2448     checkCompletedNormally(h1, null);
2449     checkCompletedNormally(h2, null);
2450     checkCompletedNormally(h3, null);
2451 jsr166 1.64 for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2452 jsr166 1.47 }}
2453 dl 1.5
2454     /**
2455     * acceptEither result completes exceptionally after exceptional
2456     * completion of either source
2457     */
2458 jsr166 1.63 public void testAcceptEither_exceptionalCompletion() {
2459 jsr166 1.40 for (ExecutionMode m : ExecutionMode.values())
2460 jsr166 1.47 for (Integer v1 : new Integer[] { 1, null })
2461     {
2462 jsr166 1.40 final CompletableFuture<Integer> f = new CompletableFuture<>();
2463     final CompletableFuture<Integer> g = new CompletableFuture<>();
2464     final CFException ex = new CFException();
2465 jsr166 1.64 final NoopConsumer[] rs = new NoopConsumer[6];
2466     for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2467 jsr166 1.40
2468 jsr166 1.63 final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2469     final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2470     checkIncomplete(h0);
2471     checkIncomplete(h1);
2472     rs[0].assertNotInvoked();
2473     rs[1].assertNotInvoked();
2474 jsr166 1.40 f.completeExceptionally(ex);
2475 jsr166 1.72 checkCompletedWithWrappedException(h0, ex);
2476     checkCompletedWithWrappedException(h1, ex);
2477 jsr166 1.63 final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2478     final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2479 jsr166 1.72 checkCompletedWithWrappedException(h2, ex);
2480     checkCompletedWithWrappedException(h3, ex);
2481 jsr166 1.63
2482 jsr166 1.40 g.complete(v1);
2483    
2484 jsr166 1.63 // unspecified behavior - both source completions available
2485     final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2486     final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2487 jsr166 1.40 try {
2488 jsr166 1.63 assertNull(h4.join());
2489 jsr166 1.64 rs[4].assertValue(v1);
2490 jsr166 1.40 } catch (CompletionException ok) {
2491 jsr166 1.72 checkCompletedWithWrappedException(h4, ex);
2492 jsr166 1.63 rs[4].assertNotInvoked();
2493 jsr166 1.40 }
2494     try {
2495 jsr166 1.63 assertNull(h5.join());
2496 jsr166 1.64 rs[5].assertValue(v1);
2497 jsr166 1.40 } catch (CompletionException ok) {
2498 jsr166 1.72 checkCompletedWithWrappedException(h5, ex);
2499 jsr166 1.63 rs[5].assertNotInvoked();
2500 jsr166 1.40 }
2501 dl 1.5
2502 jsr166 1.72 checkCompletedExceptionally(f, ex);
2503 jsr166 1.40 checkCompletedNormally(g, v1);
2504 jsr166 1.72 checkCompletedWithWrappedException(h0, ex);
2505     checkCompletedWithWrappedException(h1, ex);
2506     checkCompletedWithWrappedException(h2, ex);
2507     checkCompletedWithWrappedException(h3, ex);
2508     checkCompletedWithWrappedException(h4, ex);
2509 jsr166 1.63 for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2510 jsr166 1.47 }}
2511 dl 1.5
2512 jsr166 1.68 public void testAcceptEither_exceptionalCompletion2() {
2513     for (ExecutionMode m : ExecutionMode.values())
2514     for (boolean fFirst : new boolean[] { true, false })
2515     for (Integer v1 : new Integer[] { 1, null })
2516     {
2517     final CompletableFuture<Integer> f = new CompletableFuture<>();
2518     final CompletableFuture<Integer> g = new CompletableFuture<>();
2519     final CFException ex = new CFException();
2520     final NoopConsumer[] rs = new NoopConsumer[6];
2521     for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2522    
2523     final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2524     final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2525 jsr166 1.78 assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2526     assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2527 jsr166 1.68 final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2528     final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2529    
2530     // unspecified behavior - both source completions available
2531     try {
2532     assertEquals(null, h0.join());
2533     rs[0].assertValue(v1);
2534     } catch (CompletionException ok) {
2535 jsr166 1.72 checkCompletedWithWrappedException(h0, ex);
2536 jsr166 1.68 rs[0].assertNotInvoked();
2537     }
2538     try {
2539     assertEquals(null, h1.join());
2540     rs[1].assertValue(v1);
2541     } catch (CompletionException ok) {
2542 jsr166 1.72 checkCompletedWithWrappedException(h1, ex);
2543 jsr166 1.68 rs[1].assertNotInvoked();
2544     }
2545     try {
2546     assertEquals(null, h2.join());
2547     rs[2].assertValue(v1);
2548     } catch (CompletionException ok) {
2549 jsr166 1.72 checkCompletedWithWrappedException(h2, ex);
2550 jsr166 1.68 rs[2].assertNotInvoked();
2551     }
2552     try {
2553     assertEquals(null, h3.join());
2554     rs[3].assertValue(v1);
2555     } catch (CompletionException ok) {
2556 jsr166 1.72 checkCompletedWithWrappedException(h3, ex);
2557 jsr166 1.68 rs[3].assertNotInvoked();
2558     }
2559    
2560     checkCompletedNormally(f, v1);
2561 jsr166 1.72 checkCompletedExceptionally(g, ex);
2562 jsr166 1.68 }}
2563    
2564 dl 1.5 /**
2565     * acceptEither result completes exceptionally if either source cancelled
2566     */
2567 jsr166 1.63 public void testAcceptEither_sourceCancelled() {
2568 jsr166 1.40 for (ExecutionMode m : ExecutionMode.values())
2569     for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2570 jsr166 1.47 for (Integer v1 : new Integer[] { 1, null })
2571     {
2572 jsr166 1.40 final CompletableFuture<Integer> f = new CompletableFuture<>();
2573     final CompletableFuture<Integer> g = new CompletableFuture<>();
2574 jsr166 1.64 final NoopConsumer[] rs = new NoopConsumer[6];
2575     for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2576 jsr166 1.63
2577     final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2578     final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2579     checkIncomplete(h0);
2580     checkIncomplete(h1);
2581     rs[0].assertNotInvoked();
2582     rs[1].assertNotInvoked();
2583     f.cancel(mayInterruptIfRunning);
2584     checkCompletedWithWrappedCancellationException(h0);
2585     checkCompletedWithWrappedCancellationException(h1);
2586     final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2587     final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2588     checkCompletedWithWrappedCancellationException(h2);
2589     checkCompletedWithWrappedCancellationException(h3);
2590 jsr166 1.40
2591     g.complete(v1);
2592    
2593 jsr166 1.63 // unspecified behavior - both source completions available
2594     final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2595     final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2596     try {
2597     assertNull(h4.join());
2598 jsr166 1.64 rs[4].assertValue(v1);
2599 jsr166 1.63 } catch (CompletionException ok) {
2600     checkCompletedWithWrappedCancellationException(h4);
2601     rs[4].assertNotInvoked();
2602     }
2603     try {
2604     assertNull(h5.join());
2605 jsr166 1.64 rs[5].assertValue(v1);
2606 jsr166 1.63 } catch (CompletionException ok) {
2607     checkCompletedWithWrappedCancellationException(h5);
2608     rs[5].assertNotInvoked();
2609     }
2610    
2611 jsr166 1.40 checkCancelled(f);
2612     checkCompletedNormally(g, v1);
2613 jsr166 1.63 checkCompletedWithWrappedCancellationException(h0);
2614     checkCompletedWithWrappedCancellationException(h1);
2615     checkCompletedWithWrappedCancellationException(h2);
2616     checkCompletedWithWrappedCancellationException(h3);
2617     for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2618 jsr166 1.47 }}
2619 jsr166 1.40
2620 jsr166 1.63 /**
2621     * acceptEither result completes exceptionally if action does
2622     */
2623     public void testAcceptEither_actionFailed() {
2624 jsr166 1.40 for (ExecutionMode m : ExecutionMode.values())
2625 jsr166 1.47 for (Integer v1 : new Integer[] { 1, null })
2626 jsr166 1.63 for (Integer v2 : new Integer[] { 2, null })
2627 jsr166 1.47 {
2628 jsr166 1.40 final CompletableFuture<Integer> f = new CompletableFuture<>();
2629     final CompletableFuture<Integer> g = new CompletableFuture<>();
2630 jsr166 1.63 final FailingConsumer[] rs = new FailingConsumer[6];
2631     for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
2632 jsr166 1.40
2633 jsr166 1.63 final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2634     final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2635 jsr166 1.40 f.complete(v1);
2636 jsr166 1.63 final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2637     final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2638 jsr166 1.151 checkCompletedWithWrappedException(h0, rs[0].ex);
2639     checkCompletedWithWrappedException(h1, rs[1].ex);
2640     checkCompletedWithWrappedException(h2, rs[2].ex);
2641     checkCompletedWithWrappedException(h3, rs[3].ex);
2642 jsr166 1.63 for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2643 jsr166 1.40
2644 jsr166 1.63 g.complete(v2);
2645 jsr166 1.40
2646 jsr166 1.63 // unspecified behavior - both source completions available
2647     final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2648     final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2649 jsr166 1.40
2650 jsr166 1.151 checkCompletedWithWrappedException(h4, rs[4].ex);
2651 jsr166 1.63 assertTrue(Objects.equals(v1, rs[4].value) ||
2652     Objects.equals(v2, rs[4].value));
2653 jsr166 1.151 checkCompletedWithWrappedException(h5, rs[5].ex);
2654 jsr166 1.63 assertTrue(Objects.equals(v1, rs[5].value) ||
2655     Objects.equals(v2, rs[5].value));
2656 jsr166 1.40
2657     checkCompletedNormally(f, v1);
2658 jsr166 1.63 checkCompletedNormally(g, v2);
2659 jsr166 1.47 }}
2660 dl 1.5
2661     /**
2662     * runAfterEither result completes normally after normal completion
2663     * of either source
2664     */
2665 jsr166 1.65 public void testRunAfterEither_normalCompletion() {
2666 jsr166 1.41 for (ExecutionMode m : ExecutionMode.values())
2667     for (Integer v1 : new Integer[] { 1, null })
2668 jsr166 1.47 for (Integer v2 : new Integer[] { 2, null })
2669     {
2670 jsr166 1.41 final CompletableFuture<Integer> f = new CompletableFuture<>();
2671     final CompletableFuture<Integer> g = new CompletableFuture<>();
2672 jsr166 1.65 final Noop[] rs = new Noop[6];
2673     for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2674 jsr166 1.41
2675 jsr166 1.65 final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2676     final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2677     checkIncomplete(h0);
2678     checkIncomplete(h1);
2679     rs[0].assertNotInvoked();
2680     rs[1].assertNotInvoked();
2681 jsr166 1.41 f.complete(v1);
2682 jsr166 1.65 checkCompletedNormally(h0, null);
2683     checkCompletedNormally(h1, null);
2684     rs[0].assertInvoked();
2685     rs[1].assertInvoked();
2686     final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2687     final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2688     checkCompletedNormally(h2, null);
2689     checkCompletedNormally(h3, null);
2690     rs[2].assertInvoked();
2691     rs[3].assertInvoked();
2692 jsr166 1.41
2693     g.complete(v2);
2694    
2695 jsr166 1.65 final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2696     final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2697 jsr166 1.47
2698 jsr166 1.41 checkCompletedNormally(f, v1);
2699     checkCompletedNormally(g, v2);
2700 jsr166 1.65 checkCompletedNormally(h0, null);
2701     checkCompletedNormally(h1, null);
2702     checkCompletedNormally(h2, null);
2703     checkCompletedNormally(h3, null);
2704     checkCompletedNormally(h4, null);
2705     checkCompletedNormally(h5, null);
2706     for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2707 jsr166 1.47 }}
2708 dl 1.5
2709     /**
2710     * runAfterEither result completes exceptionally after exceptional
2711     * completion of either source
2712     */
2713 jsr166 1.65 public void testRunAfterEither_exceptionalCompletion() {
2714 jsr166 1.41 for (ExecutionMode m : ExecutionMode.values())
2715 jsr166 1.47 for (Integer v1 : new Integer[] { 1, null })
2716     {
2717 jsr166 1.41 final CompletableFuture<Integer> f = new CompletableFuture<>();
2718     final CompletableFuture<Integer> g = new CompletableFuture<>();
2719     final CFException ex = new CFException();
2720 jsr166 1.65 final Noop[] rs = new Noop[6];
2721     for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2722 jsr166 1.41
2723 jsr166 1.65 final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2724     final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2725     checkIncomplete(h0);
2726     checkIncomplete(h1);
2727     rs[0].assertNotInvoked();
2728     rs[1].assertNotInvoked();
2729 jsr166 1.78 assertTrue(f.completeExceptionally(ex));
2730 jsr166 1.72 checkCompletedWithWrappedException(h0, ex);
2731     checkCompletedWithWrappedException(h1, ex);
2732 jsr166 1.65 final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2733     final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2734 jsr166 1.72 checkCompletedWithWrappedException(h2, ex);
2735     checkCompletedWithWrappedException(h3, ex);
2736 jsr166 1.65
2737 jsr166 1.78 assertTrue(g.complete(v1));
2738 jsr166 1.41
2739 jsr166 1.65 // unspecified behavior - both source completions available
2740     final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2741     final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2742     try {
2743     assertNull(h4.join());
2744     rs[4].assertInvoked();
2745     } catch (CompletionException ok) {
2746 jsr166 1.72 checkCompletedWithWrappedException(h4, ex);
2747 jsr166 1.65 rs[4].assertNotInvoked();
2748     }
2749     try {
2750     assertNull(h5.join());
2751     rs[5].assertInvoked();
2752     } catch (CompletionException ok) {
2753 jsr166 1.72 checkCompletedWithWrappedException(h5, ex);
2754 jsr166 1.65 rs[5].assertNotInvoked();
2755     }
2756    
2757 jsr166 1.72 checkCompletedExceptionally(f, ex);
2758 jsr166 1.41 checkCompletedNormally(g, v1);
2759 jsr166 1.72 checkCompletedWithWrappedException(h0, ex);
2760     checkCompletedWithWrappedException(h1, ex);
2761     checkCompletedWithWrappedException(h2, ex);
2762     checkCompletedWithWrappedException(h3, ex);
2763     checkCompletedWithWrappedException(h4, ex);
2764 jsr166 1.65 for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2765 jsr166 1.47 }}
2766 jsr166 1.41
2767 jsr166 1.69 public void testRunAfterEither_exceptionalCompletion2() {
2768     for (ExecutionMode m : ExecutionMode.values())
2769     for (boolean fFirst : new boolean[] { true, false })
2770     for (Integer v1 : new Integer[] { 1, null })
2771     {
2772     final CompletableFuture<Integer> f = new CompletableFuture<>();
2773     final CompletableFuture<Integer> g = new CompletableFuture<>();
2774     final CFException ex = new CFException();
2775     final Noop[] rs = new Noop[6];
2776     for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2777    
2778     final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2779     final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2780 jsr166 1.78 assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2781     assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2782 jsr166 1.69 final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2783     final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2784    
2785     // unspecified behavior - both source completions available
2786     try {
2787     assertEquals(null, h0.join());
2788     rs[0].assertInvoked();
2789     } catch (CompletionException ok) {
2790 jsr166 1.72 checkCompletedWithWrappedException(h0, ex);
2791 jsr166 1.69 rs[0].assertNotInvoked();
2792     }
2793     try {
2794     assertEquals(null, h1.join());
2795     rs[1].assertInvoked();
2796     } catch (CompletionException ok) {
2797 jsr166 1.72 checkCompletedWithWrappedException(h1, ex);
2798 jsr166 1.69 rs[1].assertNotInvoked();
2799     }
2800     try {
2801     assertEquals(null, h2.join());
2802     rs[2].assertInvoked();
2803     } catch (CompletionException ok) {
2804 jsr166 1.72 checkCompletedWithWrappedException(h2, ex);
2805 jsr166 1.69 rs[2].assertNotInvoked();
2806     }
2807     try {
2808     assertEquals(null, h3.join());
2809     rs[3].assertInvoked();
2810     } catch (CompletionException ok) {
2811 jsr166 1.72 checkCompletedWithWrappedException(h3, ex);
2812 jsr166 1.69 rs[3].assertNotInvoked();
2813     }
2814    
2815     checkCompletedNormally(f, v1);
2816 jsr166 1.72 checkCompletedExceptionally(g, ex);
2817 jsr166 1.69 }}
2818    
2819 jsr166 1.65 /**
2820     * runAfterEither result completes exceptionally if either source cancelled
2821     */
2822     public void testRunAfterEither_sourceCancelled() {
2823 jsr166 1.41 for (ExecutionMode m : ExecutionMode.values())
2824 jsr166 1.65 for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2825 jsr166 1.47 for (Integer v1 : new Integer[] { 1, null })
2826     {
2827 jsr166 1.41 final CompletableFuture<Integer> f = new CompletableFuture<>();
2828     final CompletableFuture<Integer> g = new CompletableFuture<>();
2829 jsr166 1.65 final Noop[] rs = new Noop[6];
2830     for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2831 jsr166 1.41
2832 jsr166 1.65 final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2833     final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2834     checkIncomplete(h0);
2835     checkIncomplete(h1);
2836     rs[0].assertNotInvoked();
2837     rs[1].assertNotInvoked();
2838     f.cancel(mayInterruptIfRunning);
2839     checkCompletedWithWrappedCancellationException(h0);
2840     checkCompletedWithWrappedCancellationException(h1);
2841     final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2842     final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2843     checkCompletedWithWrappedCancellationException(h2);
2844     checkCompletedWithWrappedCancellationException(h3);
2845 jsr166 1.41
2846 jsr166 1.78 assertTrue(g.complete(v1));
2847 jsr166 1.41
2848 jsr166 1.65 // unspecified behavior - both source completions available
2849     final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2850     final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2851 jsr166 1.41 try {
2852 jsr166 1.65 assertNull(h4.join());
2853     rs[4].assertInvoked();
2854 jsr166 1.41 } catch (CompletionException ok) {
2855 jsr166 1.65 checkCompletedWithWrappedCancellationException(h4);
2856     rs[4].assertNotInvoked();
2857 jsr166 1.41 }
2858     try {
2859 jsr166 1.65 assertNull(h5.join());
2860     rs[5].assertInvoked();
2861 jsr166 1.41 } catch (CompletionException ok) {
2862 jsr166 1.65 checkCompletedWithWrappedCancellationException(h5);
2863     rs[5].assertNotInvoked();
2864 jsr166 1.41 }
2865 dl 1.5
2866 jsr166 1.65 checkCancelled(f);
2867 jsr166 1.41 checkCompletedNormally(g, v1);
2868 jsr166 1.65 checkCompletedWithWrappedCancellationException(h0);
2869     checkCompletedWithWrappedCancellationException(h1);
2870     checkCompletedWithWrappedCancellationException(h2);
2871     checkCompletedWithWrappedCancellationException(h3);
2872     for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2873 jsr166 1.47 }}
2874 dl 1.5
2875     /**
2876     * runAfterEither result completes exceptionally if action does
2877     */
2878 jsr166 1.65 public void testRunAfterEither_actionFailed() {
2879 jsr166 1.41 for (ExecutionMode m : ExecutionMode.values())
2880     for (Integer v1 : new Integer[] { 1, null })
2881 jsr166 1.47 for (Integer v2 : new Integer[] { 2, null })
2882     {
2883 jsr166 1.41 final CompletableFuture<Integer> f = new CompletableFuture<>();
2884     final CompletableFuture<Integer> g = new CompletableFuture<>();
2885 jsr166 1.65 final FailingRunnable[] rs = new FailingRunnable[6];
2886     for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
2887 jsr166 1.41
2888 jsr166 1.65 final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2889     final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2890 jsr166 1.78 assertTrue(f.complete(v1));
2891 jsr166 1.65 final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2892     final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2893 jsr166 1.151 checkCompletedWithWrappedException(h0, rs[0].ex);
2894     checkCompletedWithWrappedException(h1, rs[1].ex);
2895     checkCompletedWithWrappedException(h2, rs[2].ex);
2896     checkCompletedWithWrappedException(h3, rs[3].ex);
2897 jsr166 1.65 for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2898 jsr166 1.78 assertTrue(g.complete(v2));
2899 jsr166 1.65 final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2900     final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2901 jsr166 1.151 checkCompletedWithWrappedException(h4, rs[4].ex);
2902     checkCompletedWithWrappedException(h5, rs[5].ex);
2903 jsr166 1.41
2904     checkCompletedNormally(f, v1);
2905     checkCompletedNormally(g, v2);
2906 jsr166 1.65 for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2907 jsr166 1.47 }}
2908 dl 1.5
2909     /**
2910     * thenCompose result completes normally after normal completion of source
2911     */
2912 jsr166 1.48 public void testThenCompose_normalCompletion() {
2913 jsr166 1.44 for (ExecutionMode m : ExecutionMode.values())
2914 jsr166 1.48 for (boolean createIncomplete : new boolean[] { true, false })
2915 jsr166 1.47 for (Integer v1 : new Integer[] { 1, null })
2916     {
2917 jsr166 1.44 final CompletableFuture<Integer> f = new CompletableFuture<>();
2918 jsr166 1.56 final CompletableFutureInc r = new CompletableFutureInc(m);
2919 jsr166 1.78 if (!createIncomplete) assertTrue(f.complete(v1));
2920 jsr166 1.56 final CompletableFuture<Integer> g = m.thenCompose(f, r);
2921 jsr166 1.78 if (createIncomplete) assertTrue(f.complete(v1));
2922 jsr166 1.21
2923 jsr166 1.44 checkCompletedNormally(g, inc(v1));
2924     checkCompletedNormally(f, v1);
2925 jsr166 1.70 r.assertValue(v1);
2926 jsr166 1.47 }}
2927 dl 1.5
2928     /**
2929     * thenCompose result completes exceptionally after exceptional
2930     * completion of source
2931     */
2932 jsr166 1.48 public void testThenCompose_exceptionalCompletion() {
2933 jsr166 1.47 for (ExecutionMode m : ExecutionMode.values())
2934 jsr166 1.48 for (boolean createIncomplete : new boolean[] { true, false })
2935 jsr166 1.47 {
2936 jsr166 1.44 final CFException ex = new CFException();
2937 jsr166 1.56 final CompletableFutureInc r = new CompletableFutureInc(m);
2938 jsr166 1.44 final CompletableFuture<Integer> f = new CompletableFuture<>();
2939 jsr166 1.48 if (!createIncomplete) f.completeExceptionally(ex);
2940 jsr166 1.56 final CompletableFuture<Integer> g = m.thenCompose(f, r);
2941 jsr166 1.48 if (createIncomplete) f.completeExceptionally(ex);
2942 jsr166 1.21
2943 jsr166 1.72 checkCompletedWithWrappedException(g, ex);
2944     checkCompletedExceptionally(f, ex);
2945 jsr166 1.62 r.assertNotInvoked();
2946 jsr166 1.47 }}
2947 dl 1.5
2948     /**
2949     * thenCompose result completes exceptionally if action does
2950     */
2951 jsr166 1.48 public void testThenCompose_actionFailed() {
2952 jsr166 1.44 for (ExecutionMode m : ExecutionMode.values())
2953 jsr166 1.48 for (boolean createIncomplete : new boolean[] { true, false })
2954 jsr166 1.47 for (Integer v1 : new Integer[] { 1, null })
2955     {
2956 jsr166 1.44 final CompletableFuture<Integer> f = new CompletableFuture<>();
2957     final FailingCompletableFutureFunction r
2958 jsr166 1.56 = new FailingCompletableFutureFunction(m);
2959 jsr166 1.78 if (!createIncomplete) assertTrue(f.complete(v1));
2960 jsr166 1.56 final CompletableFuture<Integer> g = m.thenCompose(f, r);
2961 jsr166 1.78 if (createIncomplete) assertTrue(f.complete(v1));
2962 jsr166 1.44
2963 jsr166 1.151 checkCompletedWithWrappedException(g, r.ex);
2964 jsr166 1.44 checkCompletedNormally(f, v1);
2965 jsr166 1.47 }}
2966 dl 1.5
2967     /**
2968     * thenCompose result completes exceptionally if source cancelled
2969     */
2970 jsr166 1.48 public void testThenCompose_sourceCancelled() {
2971 jsr166 1.44 for (ExecutionMode m : ExecutionMode.values())
2972 jsr166 1.48 for (boolean createIncomplete : new boolean[] { true, false })
2973 jsr166 1.47 for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2974     {
2975 jsr166 1.44 final CompletableFuture<Integer> f = new CompletableFuture<>();
2976 jsr166 1.56 final CompletableFutureInc r = new CompletableFutureInc(m);
2977 jsr166 1.48 if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2978 jsr166 1.56 final CompletableFuture<Integer> g = m.thenCompose(f, r);
2979 jsr166 1.50 if (createIncomplete) {
2980     checkIncomplete(g);
2981     assertTrue(f.cancel(mayInterruptIfRunning));
2982     }
2983 jsr166 1.44
2984 dl 1.5 checkCompletedWithWrappedCancellationException(g);
2985 jsr166 1.44 checkCancelled(f);
2986 jsr166 1.47 }}
2987 dl 1.5
2988 jsr166 1.99 /**
2989     * thenCompose result completes exceptionally if the result of the action does
2990     */
2991     public void testThenCompose_actionReturnsFailingFuture() {
2992     for (ExecutionMode m : ExecutionMode.values())
2993     for (int order = 0; order < 6; order++)
2994     for (Integer v1 : new Integer[] { 1, null })
2995     {
2996     final CFException ex = new CFException();
2997     final CompletableFuture<Integer> f = new CompletableFuture<>();
2998     final CompletableFuture<Integer> g = new CompletableFuture<>();
2999     final CompletableFuture<Integer> h;
3000     // Test all permutations of orders
3001     switch (order) {
3002     case 0:
3003     assertTrue(f.complete(v1));
3004     assertTrue(g.completeExceptionally(ex));
3005     h = m.thenCompose(f, (x -> g));
3006     break;
3007     case 1:
3008     assertTrue(f.complete(v1));
3009     h = m.thenCompose(f, (x -> g));
3010     assertTrue(g.completeExceptionally(ex));
3011     break;
3012     case 2:
3013     assertTrue(g.completeExceptionally(ex));
3014     assertTrue(f.complete(v1));
3015     h = m.thenCompose(f, (x -> g));
3016     break;
3017     case 3:
3018     assertTrue(g.completeExceptionally(ex));
3019     h = m.thenCompose(f, (x -> g));
3020     assertTrue(f.complete(v1));
3021     break;
3022     case 4:
3023     h = m.thenCompose(f, (x -> g));
3024     assertTrue(f.complete(v1));
3025     assertTrue(g.completeExceptionally(ex));
3026     break;
3027     case 5:
3028     h = m.thenCompose(f, (x -> g));
3029     assertTrue(f.complete(v1));
3030     assertTrue(g.completeExceptionally(ex));
3031     break;
3032     default: throw new AssertionError();
3033     }
3034    
3035     checkCompletedExceptionally(g, ex);
3036     checkCompletedWithWrappedException(h, ex);
3037     checkCompletedNormally(f, v1);
3038     }}
3039    
3040 jsr166 1.6 // other static methods
3041 dl 1.5
3042     /**
3043     * allOf(no component futures) returns a future completed normally
3044     * with the value null
3045     */
3046     public void testAllOf_empty() throws Exception {
3047 jsr166 1.24 CompletableFuture<Void> f = CompletableFuture.allOf();
3048 dl 1.5 checkCompletedNormally(f, null);
3049     }
3050    
3051     /**
3052 jsr166 1.25 * allOf returns a future completed normally with the value null
3053     * when all components complete normally
3054 dl 1.5 */
3055 jsr166 1.25 public void testAllOf_normal() throws Exception {
3056 jsr166 1.88 for (int k = 1; k < 10; k++) {
3057 jsr166 1.59 CompletableFuture<Integer>[] fs
3058     = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3059 jsr166 1.86 for (int i = 0; i < k; i++)
3060 jsr166 1.22 fs[i] = new CompletableFuture<>();
3061 dl 1.9 CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3062 jsr166 1.86 for (int i = 0; i < k; i++) {
3063 dl 1.5 checkIncomplete(f);
3064 jsr166 1.24 checkIncomplete(CompletableFuture.allOf(fs));
3065 dl 1.5 fs[i].complete(one);
3066     }
3067 dl 1.9 checkCompletedNormally(f, null);
3068 jsr166 1.24 checkCompletedNormally(CompletableFuture.allOf(fs), null);
3069 dl 1.5 }
3070     }
3071    
3072 jsr166 1.150 public void testAllOf_normal_backwards() throws Exception {
3073 jsr166 1.88 for (int k = 1; k < 10; k++) {
3074 jsr166 1.59 CompletableFuture<Integer>[] fs
3075     = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3076 jsr166 1.86 for (int i = 0; i < k; i++)
3077 jsr166 1.59 fs[i] = new CompletableFuture<>();
3078     CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3079     for (int i = k - 1; i >= 0; i--) {
3080     checkIncomplete(f);
3081     checkIncomplete(CompletableFuture.allOf(fs));
3082     fs[i].complete(one);
3083     }
3084     checkCompletedNormally(f, null);
3085     checkCompletedNormally(CompletableFuture.allOf(fs), null);
3086     }
3087     }
3088    
3089 jsr166 1.88 public void testAllOf_exceptional() throws Exception {
3090     for (int k = 1; k < 10; k++) {
3091     CompletableFuture<Integer>[] fs
3092     = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3093     CFException ex = new CFException();
3094     for (int i = 0; i < k; i++)
3095     fs[i] = new CompletableFuture<>();
3096     CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3097     for (int i = 0; i < k; i++) {
3098     checkIncomplete(f);
3099     checkIncomplete(CompletableFuture.allOf(fs));
3100 jsr166 1.121 if (i != k / 2) {
3101 jsr166 1.88 fs[i].complete(i);
3102     checkCompletedNormally(fs[i], i);
3103     } else {
3104     fs[i].completeExceptionally(ex);
3105     checkCompletedExceptionally(fs[i], ex);
3106     }
3107     }
3108     checkCompletedWithWrappedException(f, ex);
3109     checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex);
3110     }
3111     }
3112    
3113 dl 1.5 /**
3114     * anyOf(no component futures) returns an incomplete future
3115     */
3116     public void testAnyOf_empty() throws Exception {
3117 jsr166 1.86 for (Integer v1 : new Integer[] { 1, null })
3118     {
3119 jsr166 1.24 CompletableFuture<Object> f = CompletableFuture.anyOf();
3120 dl 1.5 checkIncomplete(f);
3121 jsr166 1.86
3122     f.complete(v1);
3123     checkCompletedNormally(f, v1);
3124     }}
3125 dl 1.5
3126     /**
3127 jsr166 1.25 * anyOf returns a future completed normally with a value when
3128     * a component future does
3129 dl 1.5 */
3130 jsr166 1.24 public void testAnyOf_normal() throws Exception {
3131 jsr166 1.86 for (int k = 0; k < 10; k++) {
3132 dl 1.5 CompletableFuture[] fs = new CompletableFuture[k];
3133 jsr166 1.86 for (int i = 0; i < k; i++)
3134 jsr166 1.22 fs[i] = new CompletableFuture<>();
3135 dl 1.9 CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3136 dl 1.5 checkIncomplete(f);
3137 jsr166 1.86 for (int i = 0; i < k; i++) {
3138     fs[i].complete(i);
3139     checkCompletedNormally(f, 0);
3140     int x = (int) CompletableFuture.anyOf(fs).join();
3141     assertTrue(0 <= x && x <= i);
3142     }
3143     }
3144     }
3145     public void testAnyOf_normal_backwards() throws Exception {
3146     for (int k = 0; k < 10; k++) {
3147     CompletableFuture[] fs = new CompletableFuture[k];
3148     for (int i = 0; i < k; i++)
3149     fs[i] = new CompletableFuture<>();
3150     CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3151     checkIncomplete(f);
3152     for (int i = k - 1; i >= 0; i--) {
3153     fs[i].complete(i);
3154     checkCompletedNormally(f, k - 1);
3155     int x = (int) CompletableFuture.anyOf(fs).join();
3156     assertTrue(i <= x && x <= k - 1);
3157 jsr166 1.24 }
3158     }
3159     }
3160    
3161     /**
3162     * anyOf result completes exceptionally when any component does.
3163     */
3164     public void testAnyOf_exceptional() throws Exception {
3165 jsr166 1.86 for (int k = 0; k < 10; k++) {
3166 jsr166 1.24 CompletableFuture[] fs = new CompletableFuture[k];
3167 jsr166 1.87 CFException[] exs = new CFException[k];
3168     for (int i = 0; i < k; i++) {
3169 jsr166 1.24 fs[i] = new CompletableFuture<>();
3170 jsr166 1.87 exs[i] = new CFException();
3171     }
3172 jsr166 1.24 CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3173     checkIncomplete(f);
3174 jsr166 1.86 for (int i = 0; i < k; i++) {
3175 jsr166 1.87 fs[i].completeExceptionally(exs[i]);
3176     checkCompletedWithWrappedException(f, exs[0]);
3177     checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3178     }
3179     }
3180     }
3181    
3182     public void testAnyOf_exceptional_backwards() throws Exception {
3183     for (int k = 0; k < 10; k++) {
3184     CompletableFuture[] fs = new CompletableFuture[k];
3185     CFException[] exs = new CFException[k];
3186     for (int i = 0; i < k; i++) {
3187     fs[i] = new CompletableFuture<>();
3188     exs[i] = new CFException();
3189     }
3190     CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3191     checkIncomplete(f);
3192     for (int i = k - 1; i >= 0; i--) {
3193     fs[i].completeExceptionally(exs[i]);
3194     checkCompletedWithWrappedException(f, exs[k - 1]);
3195 jsr166 1.24 checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3196 dl 1.5 }
3197     }
3198     }
3199    
3200     /**
3201     * Completion methods throw NullPointerException with null arguments
3202     */
3203     public void testNPE() {
3204 jsr166 1.22 CompletableFuture<Integer> f = new CompletableFuture<>();
3205     CompletableFuture<Integer> g = new CompletableFuture<>();
3206 jsr166 1.14 CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null;
3207 jsr166 1.17 ThreadExecutor exec = new ThreadExecutor();
3208 jsr166 1.14
3209     Runnable[] throwingActions = {
3210 jsr166 1.31 () -> CompletableFuture.supplyAsync(null),
3211     () -> CompletableFuture.supplyAsync(null, exec),
3212 jsr166 1.95 () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null),
3213 jsr166 1.31
3214     () -> CompletableFuture.runAsync(null),
3215     () -> CompletableFuture.runAsync(null, exec),
3216     () -> CompletableFuture.runAsync(() -> {}, null),
3217    
3218     () -> f.completeExceptionally(null),
3219    
3220     () -> f.thenApply(null),
3221     () -> f.thenApplyAsync(null),
3222     () -> f.thenApplyAsync((x) -> x, null),
3223     () -> f.thenApplyAsync(null, exec),
3224    
3225     () -> f.thenAccept(null),
3226     () -> f.thenAcceptAsync(null),
3227     () -> f.thenAcceptAsync((x) -> {} , null),
3228     () -> f.thenAcceptAsync(null, exec),
3229    
3230     () -> f.thenRun(null),
3231     () -> f.thenRunAsync(null),
3232     () -> f.thenRunAsync(() -> {} , null),
3233     () -> f.thenRunAsync(null, exec),
3234    
3235     () -> f.thenCombine(g, null),
3236     () -> f.thenCombineAsync(g, null),
3237     () -> f.thenCombineAsync(g, null, exec),
3238     () -> f.thenCombine(nullFuture, (x, y) -> x),
3239     () -> f.thenCombineAsync(nullFuture, (x, y) -> x),
3240     () -> f.thenCombineAsync(nullFuture, (x, y) -> x, exec),
3241     () -> f.thenCombineAsync(g, (x, y) -> x, null),
3242    
3243     () -> f.thenAcceptBoth(g, null),
3244     () -> f.thenAcceptBothAsync(g, null),
3245     () -> f.thenAcceptBothAsync(g, null, exec),
3246     () -> f.thenAcceptBoth(nullFuture, (x, y) -> {}),
3247     () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}),
3248     () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec),
3249     () -> f.thenAcceptBothAsync(g, (x, y) -> {}, null),
3250    
3251     () -> f.runAfterBoth(g, null),
3252     () -> f.runAfterBothAsync(g, null),
3253     () -> f.runAfterBothAsync(g, null, exec),
3254     () -> f.runAfterBoth(nullFuture, () -> {}),
3255     () -> f.runAfterBothAsync(nullFuture, () -> {}),
3256     () -> f.runAfterBothAsync(nullFuture, () -> {}, exec),
3257     () -> f.runAfterBothAsync(g, () -> {}, null),
3258    
3259     () -> f.applyToEither(g, null),
3260     () -> f.applyToEitherAsync(g, null),
3261     () -> f.applyToEitherAsync(g, null, exec),
3262     () -> f.applyToEither(nullFuture, (x) -> x),
3263     () -> f.applyToEitherAsync(nullFuture, (x) -> x),
3264     () -> f.applyToEitherAsync(nullFuture, (x) -> x, exec),
3265     () -> f.applyToEitherAsync(g, (x) -> x, null),
3266    
3267     () -> f.acceptEither(g, null),
3268     () -> f.acceptEitherAsync(g, null),
3269     () -> f.acceptEitherAsync(g, null, exec),
3270     () -> f.acceptEither(nullFuture, (x) -> {}),
3271     () -> f.acceptEitherAsync(nullFuture, (x) -> {}),
3272     () -> f.acceptEitherAsync(nullFuture, (x) -> {}, exec),
3273     () -> f.acceptEitherAsync(g, (x) -> {}, null),
3274    
3275     () -> f.runAfterEither(g, null),
3276     () -> f.runAfterEitherAsync(g, null),
3277     () -> f.runAfterEitherAsync(g, null, exec),
3278     () -> f.runAfterEither(nullFuture, () -> {}),
3279     () -> f.runAfterEitherAsync(nullFuture, () -> {}),
3280     () -> f.runAfterEitherAsync(nullFuture, () -> {}, exec),
3281     () -> f.runAfterEitherAsync(g, () -> {}, null),
3282    
3283     () -> f.thenCompose(null),
3284     () -> f.thenComposeAsync(null),
3285 jsr166 1.56 () -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null),
3286 jsr166 1.31 () -> f.thenComposeAsync(null, exec),
3287    
3288     () -> f.exceptionally(null),
3289    
3290     () -> f.handle(null),
3291 jsr166 1.107
3292 jsr166 1.31 () -> CompletableFuture.allOf((CompletableFuture<?>)null),
3293     () -> CompletableFuture.allOf((CompletableFuture<?>[])null),
3294     () -> CompletableFuture.allOf(f, null),
3295     () -> CompletableFuture.allOf(null, f),
3296    
3297     () -> CompletableFuture.anyOf((CompletableFuture<?>)null),
3298     () -> CompletableFuture.anyOf((CompletableFuture<?>[])null),
3299     () -> CompletableFuture.anyOf(f, null),
3300     () -> CompletableFuture.anyOf(null, f),
3301 jsr166 1.32
3302     () -> f.obtrudeException(null),
3303 jsr166 1.115
3304     () -> CompletableFuture.delayedExecutor(1L, SECONDS, null),
3305 jsr166 1.143 () -> CompletableFuture.delayedExecutor(1L, null, exec),
3306 jsr166 1.115 () -> CompletableFuture.delayedExecutor(1L, null),
3307 jsr166 1.116
3308     () -> f.orTimeout(1L, null),
3309     () -> f.completeOnTimeout(42, 1L, null),
3310 jsr166 1.121
3311     () -> CompletableFuture.failedFuture(null),
3312     () -> CompletableFuture.failedStage(null),
3313 jsr166 1.14 };
3314 dl 1.5
3315 jsr166 1.14 assertThrows(NullPointerException.class, throwingActions);
3316 jsr166 1.17 assertEquals(0, exec.count.get());
3317 dl 1.5 }
3318    
3319 dl 1.26 /**
3320     * toCompletableFuture returns this CompletableFuture.
3321     */
3322     public void testToCompletableFuture() {
3323     CompletableFuture<Integer> f = new CompletableFuture<>();
3324     assertSame(f, f.toCompletableFuture());
3325     }
3326    
3327 dl 1.103 // jdk9
3328 jsr166 1.104
3329 dl 1.103 /**
3330     * newIncompleteFuture returns an incomplete CompletableFuture
3331     */
3332     public void testNewIncompleteFuture() {
3333 jsr166 1.117 for (Integer v1 : new Integer[] { 1, null })
3334     {
3335 dl 1.103 CompletableFuture<Integer> f = new CompletableFuture<>();
3336     CompletableFuture<Integer> g = f.newIncompleteFuture();
3337     checkIncomplete(f);
3338     checkIncomplete(g);
3339 jsr166 1.117 f.complete(v1);
3340     checkCompletedNormally(f, v1);
3341     checkIncomplete(g);
3342     g.complete(v1);
3343     checkCompletedNormally(g, v1);
3344     assertSame(g.getClass(), CompletableFuture.class);
3345     }}
3346 dl 1.103
3347     /**
3348     * completedStage returns a completed CompletionStage
3349     */
3350     public void testCompletedStage() {
3351 jsr166 1.120 AtomicInteger x = new AtomicInteger(0);
3352 dl 1.103 AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3353     CompletionStage<Integer> f = CompletableFuture.completedStage(1);
3354     f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3355     assertEquals(x.get(), 1);
3356     assertNull(r.get());
3357     }
3358    
3359     /**
3360     * defaultExecutor by default returns the commonPool if
3361 dl 1.110 * it supports more than one thread.
3362 dl 1.103 */
3363     public void testDefaultExecutor() {
3364     CompletableFuture<Integer> f = new CompletableFuture<>();
3365     Executor e = f.defaultExecutor();
3366 jsr166 1.108 Executor c = ForkJoinPool.commonPool();
3367 dl 1.105 if (ForkJoinPool.getCommonPoolParallelism() > 1)
3368 dl 1.103 assertSame(e, c);
3369 jsr166 1.111 else
3370     assertNotSame(e, c);
3371 dl 1.103 }
3372    
3373     /**
3374     * failedFuture returns a CompletableFuture completed
3375     * exceptionally with the given Exception
3376     */
3377     public void testFailedFuture() {
3378     CFException ex = new CFException();
3379     CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex);
3380 jsr166 1.119 checkCompletedExceptionally(f, ex);
3381 dl 1.103 }
3382    
3383     /**
3384     * failedFuture(null) throws NPE
3385     */
3386 jsr166 1.118 public void testFailedFuture_null() {
3387 dl 1.103 try {
3388     CompletableFuture<Integer> f = CompletableFuture.failedFuture(null);
3389 jsr166 1.104 shouldThrow();
3390     } catch (NullPointerException success) {}
3391 dl 1.103 }
3392    
3393     /**
3394     * copy returns a CompletableFuture that is completed normally,
3395     * with the same value, when source is.
3396     */
3397     public void testCopy() {
3398     CompletableFuture<Integer> f = new CompletableFuture<>();
3399     CompletableFuture<Integer> g = f.copy();
3400     checkIncomplete(f);
3401     checkIncomplete(g);
3402     f.complete(1);
3403     checkCompletedNormally(f, 1);
3404     checkCompletedNormally(g, 1);
3405     }
3406    
3407     /**
3408     * copy returns a CompletableFuture that is completed exceptionally
3409     * when source is.
3410     */
3411     public void testCopy2() {
3412     CompletableFuture<Integer> f = new CompletableFuture<>();
3413     CompletableFuture<Integer> g = f.copy();
3414     checkIncomplete(f);
3415     checkIncomplete(g);
3416     CFException ex = new CFException();
3417     f.completeExceptionally(ex);
3418     checkCompletedExceptionally(f, ex);
3419 jsr166 1.121 checkCompletedWithWrappedException(g, ex);
3420 dl 1.103 }
3421    
3422     /**
3423     * minimalCompletionStage returns a CompletableFuture that is
3424     * completed normally, with the same value, when source is.
3425     */
3426     public void testMinimalCompletionStage() {
3427     CompletableFuture<Integer> f = new CompletableFuture<>();
3428     CompletionStage<Integer> g = f.minimalCompletionStage();
3429 jsr166 1.120 AtomicInteger x = new AtomicInteger(0);
3430 dl 1.103 AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3431     checkIncomplete(f);
3432     g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3433     f.complete(1);
3434     checkCompletedNormally(f, 1);
3435     assertEquals(x.get(), 1);
3436     assertNull(r.get());
3437     }
3438    
3439     /**
3440     * minimalCompletionStage returns a CompletableFuture that is
3441     * completed exceptionally when source is.
3442     */
3443     public void testMinimalCompletionStage2() {
3444     CompletableFuture<Integer> f = new CompletableFuture<>();
3445     CompletionStage<Integer> g = f.minimalCompletionStage();
3446 jsr166 1.120 AtomicInteger x = new AtomicInteger(0);
3447 dl 1.103 AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3448     g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3449     checkIncomplete(f);
3450     CFException ex = new CFException();
3451     f.completeExceptionally(ex);
3452     checkCompletedExceptionally(f, ex);
3453     assertEquals(x.get(), 0);
3454     assertEquals(r.get().getCause(), ex);
3455     }
3456    
3457     /**
3458 jsr166 1.109 * failedStage returns a CompletionStage completed
3459 dl 1.103 * exceptionally with the given Exception
3460     */
3461     public void testFailedStage() {
3462     CFException ex = new CFException();
3463     CompletionStage<Integer> f = CompletableFuture.failedStage(ex);
3464 jsr166 1.120 AtomicInteger x = new AtomicInteger(0);
3465 dl 1.103 AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3466     f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3467     assertEquals(x.get(), 0);
3468 jsr166 1.119 assertEquals(r.get(), ex);
3469 dl 1.103 }
3470    
3471     /**
3472     * completeAsync completes with value of given supplier
3473     */
3474     public void testCompleteAsync() {
3475 jsr166 1.121 for (Integer v1 : new Integer[] { 1, null })
3476     {
3477 dl 1.103 CompletableFuture<Integer> f = new CompletableFuture<>();
3478 jsr166 1.121 f.completeAsync(() -> v1);
3479 dl 1.103 f.join();
3480 jsr166 1.121 checkCompletedNormally(f, v1);
3481     }}
3482 dl 1.103
3483     /**
3484     * completeAsync completes exceptionally if given supplier throws
3485     */
3486     public void testCompleteAsync2() {
3487     CompletableFuture<Integer> f = new CompletableFuture<>();
3488     CFException ex = new CFException();
3489     f.completeAsync(() -> {if (true) throw ex; return 1;});
3490     try {
3491     f.join();
3492     shouldThrow();
3493 jsr166 1.121 } catch (CompletionException success) {}
3494     checkCompletedWithWrappedException(f, ex);
3495 dl 1.103 }
3496    
3497     /**
3498     * completeAsync with given executor completes with value of given supplier
3499     */
3500     public void testCompleteAsync3() {
3501 jsr166 1.121 for (Integer v1 : new Integer[] { 1, null })
3502     {
3503 dl 1.103 CompletableFuture<Integer> f = new CompletableFuture<>();
3504 jsr166 1.121 ThreadExecutor executor = new ThreadExecutor();
3505     f.completeAsync(() -> v1, executor);
3506     assertSame(v1, f.join());
3507     checkCompletedNormally(f, v1);
3508     assertEquals(1, executor.count.get());
3509     }}
3510 dl 1.103
3511     /**
3512     * completeAsync with given executor completes exceptionally if
3513     * given supplier throws
3514     */
3515     public void testCompleteAsync4() {
3516     CompletableFuture<Integer> f = new CompletableFuture<>();
3517     CFException ex = new CFException();
3518 jsr166 1.121 ThreadExecutor executor = new ThreadExecutor();
3519     f.completeAsync(() -> {if (true) throw ex; return 1;}, executor);
3520 dl 1.103 try {
3521     f.join();
3522     shouldThrow();
3523 jsr166 1.121 } catch (CompletionException success) {}
3524     checkCompletedWithWrappedException(f, ex);
3525     assertEquals(1, executor.count.get());
3526 dl 1.103 }
3527    
3528     /**
3529 jsr166 1.106 * orTimeout completes with TimeoutException if not complete
3530 dl 1.103 */
3531 jsr166 1.121 public void testOrTimeout_timesOut() {
3532     long timeoutMillis = timeoutMillis();
3533 dl 1.103 CompletableFuture<Integer> f = new CompletableFuture<>();
3534 jsr166 1.121 long startTime = System.nanoTime();
3535 jsr166 1.145 assertSame(f, f.orTimeout(timeoutMillis, MILLISECONDS));
3536 jsr166 1.121 checkCompletedWithTimeoutException(f);
3537     assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
3538 dl 1.103 }
3539    
3540     /**
3541 jsr166 1.106 * orTimeout completes normally if completed before timeout
3542 dl 1.103 */
3543 jsr166 1.121 public void testOrTimeout_completed() {
3544     for (Integer v1 : new Integer[] { 1, null })
3545     {
3546 dl 1.103 CompletableFuture<Integer> f = new CompletableFuture<>();
3547 jsr166 1.121 CompletableFuture<Integer> g = new CompletableFuture<>();
3548     long startTime = System.nanoTime();
3549     f.complete(v1);
3550 jsr166 1.145 assertSame(f, f.orTimeout(LONG_DELAY_MS, MILLISECONDS));
3551     assertSame(g, g.orTimeout(LONG_DELAY_MS, MILLISECONDS));
3552 jsr166 1.121 g.complete(v1);
3553     checkCompletedNormally(f, v1);
3554     checkCompletedNormally(g, v1);
3555     assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
3556     }}
3557 dl 1.103
3558     /**
3559 jsr166 1.106 * completeOnTimeout completes with given value if not complete
3560 dl 1.103 */
3561 jsr166 1.121 public void testCompleteOnTimeout_timesOut() {
3562     testInParallel(() -> testCompleteOnTimeout_timesOut(42),
3563     () -> testCompleteOnTimeout_timesOut(null));
3564     }
3565    
3566 jsr166 1.146 /**
3567     * completeOnTimeout completes with given value if not complete
3568     */
3569 jsr166 1.121 public void testCompleteOnTimeout_timesOut(Integer v) {
3570     long timeoutMillis = timeoutMillis();
3571 dl 1.103 CompletableFuture<Integer> f = new CompletableFuture<>();
3572 jsr166 1.121 long startTime = System.nanoTime();
3573 jsr166 1.145 assertSame(f, f.completeOnTimeout(v, timeoutMillis, MILLISECONDS));
3574 jsr166 1.121 assertSame(v, f.join());
3575     assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
3576     f.complete(99); // should have no effect
3577     checkCompletedNormally(f, v);
3578 dl 1.103 }
3579    
3580     /**
3581 jsr166 1.106 * completeOnTimeout has no effect if completed within timeout
3582 dl 1.103 */
3583 jsr166 1.121 public void testCompleteOnTimeout_completed() {
3584     for (Integer v1 : new Integer[] { 1, null })
3585     {
3586 dl 1.103 CompletableFuture<Integer> f = new CompletableFuture<>();
3587 jsr166 1.121 CompletableFuture<Integer> g = new CompletableFuture<>();
3588     long startTime = System.nanoTime();
3589     f.complete(v1);
3590 jsr166 1.145 assertSame(f, f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS));
3591     assertSame(g, g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS));
3592 jsr166 1.121 g.complete(v1);
3593     checkCompletedNormally(f, v1);
3594     checkCompletedNormally(g, v1);
3595     assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
3596     }}
3597 dl 1.103
3598     /**
3599     * delayedExecutor returns an executor that delays submission
3600     */
3601 jsr166 1.121 public void testDelayedExecutor() {
3602     testInParallel(() -> testDelayedExecutor(null, null),
3603     () -> testDelayedExecutor(null, 1),
3604     () -> testDelayedExecutor(new ThreadExecutor(), 1),
3605     () -> testDelayedExecutor(new ThreadExecutor(), 1));
3606 dl 1.103 }
3607    
3608 jsr166 1.121 public void testDelayedExecutor(Executor executor, Integer v) throws Exception {
3609     long timeoutMillis = timeoutMillis();
3610     // Use an "unreasonably long" long timeout to catch lingering threads
3611     long longTimeoutMillis = 1000 * 60 * 60 * 24;
3612     final Executor delayer, longDelayer;
3613     if (executor == null) {
3614     delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS);
3615     longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS);
3616     } else {
3617     delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS, executor);
3618     longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS, executor);
3619     }
3620 dl 1.103 long startTime = System.nanoTime();
3621 jsr166 1.121 CompletableFuture<Integer> f =
3622     CompletableFuture.supplyAsync(() -> v, delayer);
3623     CompletableFuture<Integer> g =
3624     CompletableFuture.supplyAsync(() -> v, longDelayer);
3625    
3626     assertNull(g.getNow(null));
3627    
3628     assertSame(v, f.get(LONG_DELAY_MS, MILLISECONDS));
3629     long millisElapsed = millisElapsedSince(startTime);
3630     assertTrue(millisElapsed >= timeoutMillis);
3631     assertTrue(millisElapsed < LONG_DELAY_MS / 2);
3632    
3633     checkCompletedNormally(f, v);
3634    
3635     checkIncomplete(g);
3636     assertTrue(g.cancel(true));
3637 dl 1.103 }
3638    
3639 jsr166 1.82 //--- tests of implementation details; not part of official tck ---
3640    
3641     Object resultOf(CompletableFuture<?> f) {
3642 jsr166 1.147 SecurityManager sm = System.getSecurityManager();
3643     if (sm != null) {
3644     try {
3645     System.setSecurityManager(null);
3646     } catch (SecurityException giveUp) {
3647     return "Reflection not available";
3648     }
3649     }
3650    
3651 jsr166 1.82 try {
3652     java.lang.reflect.Field resultField
3653     = CompletableFuture.class.getDeclaredField("result");
3654     resultField.setAccessible(true);
3655     return resultField.get(f);
3656 jsr166 1.147 } catch (Throwable t) {
3657     throw new AssertionError(t);
3658     } finally {
3659     if (sm != null) System.setSecurityManager(sm);
3660     }
3661 jsr166 1.82 }
3662    
3663     public void testExceptionPropagationReusesResultObject() {
3664     if (!testImplementationDetails) return;
3665     for (ExecutionMode m : ExecutionMode.values())
3666     {
3667 jsr166 1.83 final CFException ex = new CFException();
3668     final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42);
3669     final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3670    
3671 jsr166 1.89 List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs
3672 jsr166 1.83 = new ArrayList<>();
3673    
3674 jsr166 1.89 funs.add((y) -> m.thenRun(y, new Noop(m)));
3675     funs.add((y) -> m.thenAccept(y, new NoopConsumer(m)));
3676     funs.add((y) -> m.thenApply(y, new IncFunction(m)));
3677 jsr166 1.83
3678 jsr166 1.89 funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m)));
3679     funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m)));
3680     funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m)));
3681 jsr166 1.83
3682 jsr166 1.89 funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m)));
3683 jsr166 1.148 funs.add((y) -> m.runAfterBoth(v42, y, new Noop(m)));
3684 jsr166 1.89 funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
3685 jsr166 1.148 funs.add((y) -> m.thenAcceptBoth(v42, y, new SubtractAction(m)));
3686 jsr166 1.89 funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m)));
3687 jsr166 1.148 funs.add((y) -> m.thenCombine(v42, y, new SubtractFunction(m)));
3688 jsr166 1.83
3689 jsr166 1.135 funs.add((y) -> m.whenComplete(y, (Integer r, Throwable t) -> {}));
3690 jsr166 1.83
3691 jsr166 1.89 funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m)));
3692 jsr166 1.83
3693 jsr166 1.149 funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y}));
3694 jsr166 1.89 funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42}));
3695 jsr166 1.148 funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {v42, y}));
3696 jsr166 1.149 funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y}));
3697 jsr166 1.89 funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete}));
3698 jsr166 1.148 funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {incomplete, y}));
3699 jsr166 1.85
3700 jsr166 1.83 for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3701 jsr166 1.89 fun : funs) {
3702 jsr166 1.83 CompletableFuture<Integer> f = new CompletableFuture<>();
3703     f.completeExceptionally(ex);
3704     CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3705     checkCompletedWithWrappedException(src, ex);
3706 jsr166 1.89 CompletableFuture<?> dep = fun.apply(src);
3707 jsr166 1.83 checkCompletedWithWrappedException(dep, ex);
3708     assertSame(resultOf(src), resultOf(dep));
3709     }
3710    
3711     for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3712 jsr166 1.89 fun : funs) {
3713 jsr166 1.83 CompletableFuture<Integer> f = new CompletableFuture<>();
3714     CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3715 jsr166 1.89 CompletableFuture<?> dep = fun.apply(src);
3716 jsr166 1.83 f.completeExceptionally(ex);
3717     checkCompletedWithWrappedException(src, ex);
3718     checkCompletedWithWrappedException(dep, ex);
3719     assertSame(resultOf(src), resultOf(dep));
3720     }
3721    
3722     for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3723     for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3724 jsr166 1.89 fun : funs) {
3725 jsr166 1.83 CompletableFuture<Integer> f = new CompletableFuture<>();
3726     f.cancel(mayInterruptIfRunning);
3727     checkCancelled(f);
3728     CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3729     checkCompletedWithWrappedCancellationException(src);
3730 jsr166 1.89 CompletableFuture<?> dep = fun.apply(src);
3731 jsr166 1.83 checkCompletedWithWrappedCancellationException(dep);
3732     assertSame(resultOf(src), resultOf(dep));
3733     }
3734    
3735     for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3736     for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3737 jsr166 1.89 fun : funs) {
3738 jsr166 1.83 CompletableFuture<Integer> f = new CompletableFuture<>();
3739     CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3740 jsr166 1.89 CompletableFuture<?> dep = fun.apply(src);
3741 jsr166 1.83 f.cancel(mayInterruptIfRunning);
3742     checkCancelled(f);
3743     checkCompletedWithWrappedCancellationException(src);
3744     checkCompletedWithWrappedCancellationException(dep);
3745     assertSame(resultOf(src), resultOf(dep));
3746     }
3747 jsr166 1.82 }}
3748    
3749 jsr166 1.123 /**
3750     * Minimal completion stages throw UOE for all non-CompletionStage methods
3751     */
3752     public void testMinimalCompletionStage_minimality() {
3753     if (!testImplementationDetails) return;
3754     Function<Method, String> toSignature =
3755     (method) -> method.getName() + Arrays.toString(method.getParameterTypes());
3756 jsr166 1.124 Predicate<Method> isNotStatic =
3757     (method) -> (method.getModifiers() & Modifier.STATIC) == 0;
3758 jsr166 1.123 List<Method> minimalMethods =
3759     Stream.of(Object.class, CompletionStage.class)
3760 jsr166 1.125 .flatMap((klazz) -> Stream.of(klazz.getMethods()))
3761 jsr166 1.124 .filter(isNotStatic)
3762 jsr166 1.123 .collect(Collectors.toList());
3763     // Methods from CompletableFuture permitted NOT to throw UOE
3764     String[] signatureWhitelist = {
3765     "newIncompleteFuture[]",
3766     "defaultExecutor[]",
3767     "minimalCompletionStage[]",
3768     "copy[]",
3769     };
3770     Set<String> permittedMethodSignatures =
3771     Stream.concat(minimalMethods.stream().map(toSignature),
3772     Stream.of(signatureWhitelist))
3773     .collect(Collectors.toSet());
3774     List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods())
3775 jsr166 1.124 .filter(isNotStatic)
3776 jsr166 1.123 .filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method)))
3777     .collect(Collectors.toList());
3778    
3779     CompletionStage<Integer> minimalStage =
3780     new CompletableFuture<Integer>().minimalCompletionStage();
3781    
3782     List<Method> bugs = new ArrayList<>();
3783     for (Method method : allMethods) {
3784     Class<?>[] parameterTypes = method.getParameterTypes();
3785     Object[] args = new Object[parameterTypes.length];
3786     // Manufacture boxed primitives for primitive params
3787     for (int i = 0; i < args.length; i++) {
3788     Class<?> type = parameterTypes[i];
3789     if (parameterTypes[i] == boolean.class)
3790     args[i] = false;
3791     else if (parameterTypes[i] == int.class)
3792     args[i] = 0;
3793     else if (parameterTypes[i] == long.class)
3794     args[i] = 0L;
3795     }
3796     try {
3797     method.invoke(minimalStage, args);
3798     bugs.add(method);
3799     }
3800     catch (java.lang.reflect.InvocationTargetException expected) {
3801     if (! (expected.getCause() instanceof UnsupportedOperationException)) {
3802     bugs.add(method);
3803     // expected.getCause().printStackTrace();
3804     }
3805     }
3806     catch (ReflectiveOperationException bad) { throw new Error(bad); }
3807     }
3808     if (!bugs.isEmpty())
3809     throw new Error("Methods did not throw UOE: " + bugs.toString());
3810     }
3811    
3812 jsr166 1.128 static class Monad {
3813 jsr166 1.131 static class ZeroException extends RuntimeException {
3814     public ZeroException() { super("monadic zero"); }
3815 jsr166 1.128 }
3816     // "return", "unit"
3817     static <T> CompletableFuture<T> unit(T value) {
3818     return completedFuture(value);
3819     }
3820     // monadic zero ?
3821 jsr166 1.129 static <T> CompletableFuture<T> zero() {
3822 jsr166 1.131 return failedFuture(new ZeroException());
3823 jsr166 1.128 }
3824     // >=>
3825     static <T,U,V> Function<T, CompletableFuture<V>> compose
3826     (Function<T, CompletableFuture<U>> f,
3827     Function<U, CompletableFuture<V>> g) {
3828     return (x) -> f.apply(x).thenCompose(g);
3829     }
3830    
3831     static void assertZero(CompletableFuture<?> f) {
3832     try {
3833     f.getNow(null);
3834     throw new AssertionFailedError("should throw");
3835     } catch (CompletionException success) {
3836 jsr166 1.131 assertTrue(success.getCause() instanceof ZeroException);
3837 jsr166 1.128 }
3838     }
3839    
3840     static <T> void assertFutureEquals(CompletableFuture<T> f,
3841     CompletableFuture<T> g) {
3842     T fval = null, gval = null;
3843     Throwable fex = null, gex = null;
3844    
3845     try { fval = f.get(); }
3846     catch (ExecutionException ex) { fex = ex.getCause(); }
3847     catch (Throwable ex) { fex = ex; }
3848    
3849     try { gval = g.get(); }
3850     catch (ExecutionException ex) { gex = ex.getCause(); }
3851     catch (Throwable ex) { gex = ex; }
3852    
3853     if (fex != null || gex != null)
3854     assertSame(fex.getClass(), gex.getClass());
3855     else
3856     assertEquals(fval, gval);
3857     }
3858    
3859     static class PlusFuture<T> extends CompletableFuture<T> {
3860     AtomicReference<Throwable> firstFailure = new AtomicReference<>(null);
3861     }
3862    
3863 jsr166 1.137 /** Implements "monadic plus". */
3864 jsr166 1.128 static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f,
3865     CompletableFuture<? extends T> g) {
3866     PlusFuture<T> plus = new PlusFuture<T>();
3867 jsr166 1.129 BiConsumer<T, Throwable> action = (T result, Throwable ex) -> {
3868 jsr166 1.137 try {
3869     if (ex == null) {
3870     if (plus.complete(result))
3871     if (plus.firstFailure.get() != null)
3872     plus.firstFailure.set(null);
3873     }
3874     else if (plus.firstFailure.compareAndSet(null, ex)) {
3875     if (plus.isDone())
3876 jsr166 1.128 plus.firstFailure.set(null);
3877 jsr166 1.137 }
3878     else {
3879     // first failure has precedence
3880     Throwable first = plus.firstFailure.getAndSet(null);
3881    
3882     // may fail with "Self-suppression not permitted"
3883     try { first.addSuppressed(ex); }
3884     catch (Exception ignored) {}
3885    
3886     plus.completeExceptionally(first);
3887     }
3888     } catch (Throwable unexpected) {
3889     plus.completeExceptionally(unexpected);
3890 jsr166 1.128 }
3891     };
3892     f.whenComplete(action);
3893     g.whenComplete(action);
3894     return plus;
3895     }
3896     }
3897    
3898     /**
3899     * CompletableFuture is an additive monad - sort of.
3900     * https://en.wikipedia.org/wiki/Monad_(functional_programming)#Additive_monads
3901     */
3902     public void testAdditiveMonad() throws Throwable {
3903 jsr166 1.129 Function<Long, CompletableFuture<Long>> unit = Monad::unit;
3904     CompletableFuture<Long> zero = Monad.zero();
3905    
3906 jsr166 1.128 // Some mutually non-commutative functions
3907     Function<Long, CompletableFuture<Long>> triple
3908 jsr166 1.129 = (x) -> Monad.unit(3 * x);
3909 jsr166 1.128 Function<Long, CompletableFuture<Long>> inc
3910 jsr166 1.129 = (x) -> Monad.unit(x + 1);
3911 jsr166 1.128
3912     // unit is a right identity: m >>= unit === m
3913 jsr166 1.129 Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit),
3914     inc.apply(5L));
3915 jsr166 1.128 // unit is a left identity: (unit x) >>= f === f x
3916 jsr166 1.129 Monad.assertFutureEquals(unit.apply(5L).thenCompose(inc),
3917     inc.apply(5L));
3918    
3919 jsr166 1.128 // associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) )
3920     Monad.assertFutureEquals(
3921     unit.apply(5L).thenCompose(inc).thenCompose(triple),
3922     unit.apply(5L).thenCompose((x) -> inc.apply(x).thenCompose(triple)));
3923    
3924 jsr166 1.129 // The case for CompletableFuture as an additive monad is weaker...
3925    
3926 jsr166 1.128 // zero is a monadic zero
3927 jsr166 1.129 Monad.assertZero(zero);
3928    
3929 jsr166 1.128 // left zero: zero >>= f === zero
3930 jsr166 1.129 Monad.assertZero(zero.thenCompose(inc));
3931     // right zero: f >>= (\x -> zero) === zero
3932     Monad.assertZero(inc.apply(5L).thenCompose((x) -> zero));
3933    
3934     // f plus zero === f
3935     Monad.assertFutureEquals(Monad.unit(5L),
3936     Monad.plus(Monad.unit(5L), zero));
3937     // zero plus f === f
3938     Monad.assertFutureEquals(Monad.unit(5L),
3939     Monad.plus(zero, Monad.unit(5L)));
3940 jsr166 1.128 // zero plus zero === zero
3941 jsr166 1.129 Monad.assertZero(Monad.plus(zero, zero));
3942 jsr166 1.128 {
3943 jsr166 1.129 CompletableFuture<Long> f = Monad.plus(Monad.unit(5L),
3944     Monad.unit(8L));
3945     // non-determinism
3946     assertTrue(f.get() == 5L || f.get() == 8L);
3947 jsr166 1.128 }
3948    
3949     CompletableFuture<Long> godot = new CompletableFuture<>();
3950 jsr166 1.129 // f plus godot === f (doesn't wait for godot)
3951     Monad.assertFutureEquals(Monad.unit(5L),
3952     Monad.plus(Monad.unit(5L), godot));
3953     // godot plus f === f (doesn't wait for godot)
3954     Monad.assertFutureEquals(Monad.unit(5L),
3955     Monad.plus(godot, Monad.unit(5L)));
3956 jsr166 1.128 }
3957    
3958 jsr166 1.140 /**
3959     * A single CompletableFuture with many dependents.
3960 jsr166 1.141 * A demo of scalability - runtime is O(n).
3961 jsr166 1.140 */
3962     public void testManyDependents() throws Throwable {
3963 jsr166 1.141 final int n = 1_000;
3964 jsr166 1.140 final CompletableFuture<Void> head = new CompletableFuture<>();
3965     final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null);
3966     final AtomicInteger count = new AtomicInteger(0);
3967     for (int i = 0; i < n; i++) {
3968     head.thenRun(() -> count.getAndIncrement());
3969     head.thenAccept((x) -> count.getAndIncrement());
3970     head.thenApply((x) -> count.getAndIncrement());
3971 jsr166 1.141
3972 jsr166 1.140 head.runAfterBoth(complete, () -> count.getAndIncrement());
3973     head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement());
3974     head.thenCombine(complete, (x, y) -> count.getAndIncrement());
3975 jsr166 1.141 complete.runAfterBoth(head, () -> count.getAndIncrement());
3976     complete.thenAcceptBoth(head, (x, y) -> count.getAndIncrement());
3977     complete.thenCombine(head, (x, y) -> count.getAndIncrement());
3978    
3979     head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement());
3980     head.acceptEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement());
3981     head.applyToEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement());
3982     new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement());
3983     new CompletableFuture<Void>().acceptEither(head, (x) -> count.getAndIncrement());
3984     new CompletableFuture<Void>().applyToEither(head, (x) -> count.getAndIncrement());
3985 jsr166 1.140 }
3986     head.complete(null);
3987 jsr166 1.141 assertEquals(5 * 3 * n, count.get());
3988 jsr166 1.140 }
3989    
3990 jsr166 1.123 // static <U> U join(CompletionStage<U> stage) {
3991     // CompletableFuture<U> f = new CompletableFuture<>();
3992     // stage.whenComplete((v, ex) -> {
3993     // if (ex != null) f.completeExceptionally(ex); else f.complete(v);
3994     // });
3995     // return f.join();
3996     // }
3997    
3998     // static <U> boolean isDone(CompletionStage<U> stage) {
3999     // CompletableFuture<U> f = new CompletableFuture<>();
4000     // stage.whenComplete((v, ex) -> {
4001     // if (ex != null) f.completeExceptionally(ex); else f.complete(v);
4002     // });
4003     // return f.isDone();
4004     // }
4005    
4006     // static <U> U join2(CompletionStage<U> stage) {
4007     // return stage.toCompletableFuture().copy().join();
4008     // }
4009    
4010     // static <U> boolean isDone2(CompletionStage<U> stage) {
4011     // return stage.toCompletableFuture().copy().isDone();
4012     // }
4013    
4014 jsr166 1.1 }