ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
Revision: 1.153
Committed: Sun Jun 26 19:17:08 2016 UTC (7 years, 10 months ago) by jsr166
Branch: MAIN
Changes since 1.152: +69 -0 lines
Log Message:
add testRejectingExecutorNeverInvoked

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