ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
Revision: 1.170
Committed: Mon Jul 18 19:30:49 2016 UTC (7 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.169: +3 -3 lines
Log Message:
testAnyOfGarbageRetention: update repro recipe

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