ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
Revision: 1.177
Committed: Wed Sep 21 18:25:04 2016 UTC (7 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.176: +21 -0 lines
Log Message:
add testMinimalCompletionStage_toCompletableFuture_exceptionalCompletion

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