ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
Revision: 1.131
Committed: Sun Nov 15 18:24:25 2015 UTC (8 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.130: +14 -7 lines
Log Message:
use addSuppressed to record both failures in Monad#plus

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