ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
Revision: 1.133
Committed: Sun Nov 15 19:39:25 2015 UTC (8 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.132: +4 -0 lines
Log Message:
document testHandle_sourceCompletedNormallyActionFailed

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