ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
Revision: 1.136
Committed: Sun Nov 15 20:17:11 2015 UTC (8 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.135: +5 -0 lines
Log Message:
improve testExceptionally_exceptionalCompletionActionFailed

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