ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
Revision: 1.75
Committed: Sat Jun 7 21:14:42 2014 UTC (9 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.74: +143 -143 lines
Log Message:
minor improvements

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     import junit.framework.*;
9     import java.util.concurrent.Callable;
10 dl 1.5 import java.util.concurrent.Executor;
11     import java.util.concurrent.ExecutorService;
12     import java.util.concurrent.Executors;
13 jsr166 1.1 import java.util.concurrent.CancellationException;
14     import java.util.concurrent.CountDownLatch;
15     import java.util.concurrent.ExecutionException;
16     import java.util.concurrent.Future;
17     import java.util.concurrent.CompletableFuture;
18 dl 1.5 import java.util.concurrent.CompletionException;
19 jsr166 1.35 import java.util.concurrent.CompletionStage;
20 jsr166 1.48 import java.util.concurrent.ForkJoinPool;
21     import java.util.concurrent.ForkJoinTask;
22 jsr166 1.1 import java.util.concurrent.TimeoutException;
23     import java.util.concurrent.atomic.AtomicInteger;
24     import static java.util.concurrent.TimeUnit.MILLISECONDS;
25     import static java.util.concurrent.TimeUnit.SECONDS;
26     import java.util.*;
27 dl 1.5 import java.util.function.Supplier;
28     import java.util.function.Consumer;
29     import java.util.function.BiConsumer;
30     import java.util.function.Function;
31     import java.util.function.BiFunction;
32 jsr166 1.1
33     public class CompletableFutureTest extends JSR166TestCase {
34    
35     public static void main(String[] args) {
36     junit.textui.TestRunner.run(suite());
37     }
38     public static Test suite() {
39     return new TestSuite(CompletableFutureTest.class);
40     }
41    
42 dl 1.5 static class CFException extends RuntimeException {}
43    
44 jsr166 1.4 void checkIncomplete(CompletableFuture<?> f) {
45     assertFalse(f.isDone());
46     assertFalse(f.isCancelled());
47     assertTrue(f.toString().contains("[Not completed]"));
48     try {
49     assertNull(f.getNow(null));
50     } catch (Throwable fail) { threadUnexpectedException(fail); }
51     try {
52     f.get(0L, SECONDS);
53     shouldThrow();
54     }
55     catch (TimeoutException success) {}
56     catch (Throwable fail) { threadUnexpectedException(fail); }
57     }
58    
59 jsr166 1.11 <T> void checkCompletedNormally(CompletableFuture<T> f, T value) {
60 jsr166 1.4 try {
61 jsr166 1.20 assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS));
62     } catch (Throwable fail) { threadUnexpectedException(fail); }
63     try {
64 dl 1.5 assertEquals(value, f.join());
65 jsr166 1.4 } catch (Throwable fail) { threadUnexpectedException(fail); }
66     try {
67 dl 1.5 assertEquals(value, f.getNow(null));
68 jsr166 1.4 } catch (Throwable fail) { threadUnexpectedException(fail); }
69     try {
70 dl 1.5 assertEquals(value, f.get());
71 jsr166 1.4 } catch (Throwable fail) { threadUnexpectedException(fail); }
72 dl 1.5 assertTrue(f.isDone());
73     assertFalse(f.isCancelled());
74 dl 1.26 assertFalse(f.isCompletedExceptionally());
75 dl 1.5 assertTrue(f.toString().contains("[Completed normally]"));
76     }
77    
78     void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
79     try {
80 jsr166 1.20 f.get(LONG_DELAY_MS, MILLISECONDS);
81     shouldThrow();
82     } catch (ExecutionException success) {
83     assertTrue(success.getCause() instanceof CFException);
84     } catch (Throwable fail) { threadUnexpectedException(fail); }
85     try {
86 dl 1.5 f.join();
87     shouldThrow();
88 jsr166 1.8 } catch (CompletionException success) {
89     assertTrue(success.getCause() instanceof CFException);
90 dl 1.5 }
91     try {
92     f.getNow(null);
93     shouldThrow();
94 jsr166 1.8 } catch (CompletionException success) {
95     assertTrue(success.getCause() instanceof CFException);
96 dl 1.5 }
97     try {
98     f.get();
99     shouldThrow();
100 jsr166 1.8 } catch (ExecutionException success) {
101     assertTrue(success.getCause() instanceof CFException);
102     } catch (Throwable fail) { threadUnexpectedException(fail); }
103 dl 1.5 assertTrue(f.isDone());
104     assertFalse(f.isCancelled());
105 jsr166 1.14 assertTrue(f.toString().contains("[Completed exceptionally]"));
106 dl 1.5 }
107    
108 jsr166 1.72 <U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f,
109     Throwable ex) {
110 jsr166 1.33 try {
111     f.get(LONG_DELAY_MS, MILLISECONDS);
112     shouldThrow();
113     } catch (ExecutionException success) {
114     assertSame(ex, success.getCause());
115     } catch (Throwable fail) { threadUnexpectedException(fail); }
116     try {
117     f.join();
118     shouldThrow();
119     } catch (CompletionException success) {
120     assertSame(ex, success.getCause());
121     }
122     try {
123     f.getNow(null);
124     shouldThrow();
125     } catch (CompletionException success) {
126     assertSame(ex, success.getCause());
127     }
128     try {
129     f.get();
130     shouldThrow();
131     } catch (ExecutionException success) {
132     assertSame(ex, success.getCause());
133     } catch (Throwable fail) { threadUnexpectedException(fail); }
134 jsr166 1.73
135 jsr166 1.33 assertTrue(f.isDone());
136     assertFalse(f.isCancelled());
137     assertTrue(f.toString().contains("[Completed exceptionally]"));
138     }
139    
140 jsr166 1.72 <U> void checkCompletedWithWrappedException(CompletableFuture<U> f,
141     Throwable ex) {
142     checkCompletedExceptionallyWithRootCause(f, ex);
143     try {
144     CompletableFuture<Throwable> spy = f.handle
145     ((U u, Throwable t) -> t);
146     assertTrue(spy.join() instanceof CompletionException);
147     assertSame(ex, spy.join().getCause());
148     } catch (Throwable fail) { threadUnexpectedException(fail); }
149     }
150    
151     <U> void checkCompletedExceptionally(CompletableFuture<U> f, Throwable ex) {
152     checkCompletedExceptionallyWithRootCause(f, ex);
153     try {
154     CompletableFuture<Throwable> spy = f.handle
155     ((U u, Throwable t) -> t);
156     assertSame(ex, spy.join());
157     } catch (Throwable fail) { threadUnexpectedException(fail); }
158     }
159    
160 dl 1.5 void checkCancelled(CompletableFuture<?> f) {
161     try {
162 jsr166 1.20 f.get(LONG_DELAY_MS, MILLISECONDS);
163     shouldThrow();
164     } catch (CancellationException success) {
165     } catch (Throwable fail) { threadUnexpectedException(fail); }
166     try {
167 dl 1.5 f.join();
168     shouldThrow();
169 jsr166 1.8 } catch (CancellationException success) {}
170 dl 1.5 try {
171     f.getNow(null);
172     shouldThrow();
173 jsr166 1.8 } catch (CancellationException success) {}
174 dl 1.5 try {
175     f.get();
176     shouldThrow();
177 jsr166 1.8 } catch (CancellationException success) {
178     } catch (Throwable fail) { threadUnexpectedException(fail); }
179 dl 1.5 assertTrue(f.isDone());
180 dl 1.26 assertTrue(f.isCompletedExceptionally());
181 dl 1.5 assertTrue(f.isCancelled());
182 jsr166 1.14 assertTrue(f.toString().contains("[Completed exceptionally]"));
183 dl 1.5 }
184    
185     void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
186     try {
187 jsr166 1.20 f.get(LONG_DELAY_MS, MILLISECONDS);
188     shouldThrow();
189     } catch (ExecutionException success) {
190     assertTrue(success.getCause() instanceof CancellationException);
191     } catch (Throwable fail) { threadUnexpectedException(fail); }
192     try {
193 dl 1.5 f.join();
194     shouldThrow();
195 jsr166 1.8 } catch (CompletionException success) {
196     assertTrue(success.getCause() instanceof CancellationException);
197 dl 1.5 }
198     try {
199     f.getNow(null);
200     shouldThrow();
201 jsr166 1.8 } catch (CompletionException success) {
202     assertTrue(success.getCause() instanceof CancellationException);
203 dl 1.5 }
204     try {
205     f.get();
206     shouldThrow();
207 jsr166 1.8 } catch (ExecutionException success) {
208     assertTrue(success.getCause() instanceof CancellationException);
209     } catch (Throwable fail) { threadUnexpectedException(fail); }
210 dl 1.5 assertTrue(f.isDone());
211     assertFalse(f.isCancelled());
212 dl 1.26 assertTrue(f.isCompletedExceptionally());
213 jsr166 1.14 assertTrue(f.toString().contains("[Completed exceptionally]"));
214 dl 1.5 }
215    
216     /**
217     * A newly constructed CompletableFuture is incomplete, as indicated
218     * by methods isDone, isCancelled, and getNow
219     */
220     public void testConstructor() {
221 jsr166 1.22 CompletableFuture<Integer> f = new CompletableFuture<>();
222 dl 1.5 checkIncomplete(f);
223     }
224    
225     /**
226     * complete completes normally, as indicated by methods isDone,
227     * isCancelled, join, get, and getNow
228     */
229     public void testComplete() {
230 jsr166 1.22 CompletableFuture<Integer> f = new CompletableFuture<>();
231 dl 1.5 checkIncomplete(f);
232     f.complete(one);
233     checkCompletedNormally(f, one);
234     }
235    
236     /**
237     * completeExceptionally completes exceptionally, as indicated by
238     * methods isDone, isCancelled, join, get, and getNow
239     */
240     public void testCompleteExceptionally() {
241 jsr166 1.22 CompletableFuture<Integer> f = new CompletableFuture<>();
242 jsr166 1.72 CFException ex = new CFException();
243 dl 1.5 checkIncomplete(f);
244 jsr166 1.72 f.completeExceptionally(ex);
245     checkCompletedExceptionally(f, ex);
246 dl 1.5 }
247    
248     /**
249     * cancel completes exceptionally and reports cancelled, as indicated by
250     * methods isDone, isCancelled, join, get, and getNow
251     */
252     public void testCancel() {
253 jsr166 1.22 CompletableFuture<Integer> f = new CompletableFuture<>();
254 dl 1.5 checkIncomplete(f);
255     assertTrue(f.cancel(true));
256     checkCancelled(f);
257     }
258    
259     /**
260     * obtrudeValue forces completion with given value
261     */
262     public void testObtrudeValue() {
263 jsr166 1.22 CompletableFuture<Integer> f = new CompletableFuture<>();
264 dl 1.5 checkIncomplete(f);
265     f.complete(one);
266     checkCompletedNormally(f, one);
267     f.obtrudeValue(three);
268     checkCompletedNormally(f, three);
269     f.obtrudeValue(two);
270     checkCompletedNormally(f, two);
271 jsr166 1.22 f = new CompletableFuture<>();
272 dl 1.5 f.obtrudeValue(three);
273     checkCompletedNormally(f, three);
274 jsr166 1.46 f.obtrudeValue(null);
275     checkCompletedNormally(f, null);
276 jsr166 1.22 f = new CompletableFuture<>();
277 dl 1.5 f.completeExceptionally(new CFException());
278     f.obtrudeValue(four);
279     checkCompletedNormally(f, four);
280 jsr166 1.4 }
281    
282 dl 1.5 /**
283     * obtrudeException forces completion with given exception
284     */
285     public void testObtrudeException() {
286 jsr166 1.72 for (Integer v1 : new Integer[] { 1, null })
287     {
288     CFException ex;
289     CompletableFuture<Integer> f;
290    
291     f = new CompletableFuture<>();
292     f.complete(v1);
293     for (int i = 0; i < 2; i++) {
294     f.obtrudeException(ex = new CFException());
295     checkCompletedExceptionally(f, ex);
296     }
297    
298 jsr166 1.22 f = new CompletableFuture<>();
299 jsr166 1.72 for (int i = 0; i < 2; i++) {
300     f.obtrudeException(ex = new CFException());
301     checkCompletedExceptionally(f, ex);
302     }
303    
304 jsr166 1.22 f = new CompletableFuture<>();
305 jsr166 1.72 f.completeExceptionally(ex = new CFException());
306     f.obtrudeValue(v1);
307     checkCompletedNormally(f, v1);
308     f.obtrudeException(ex = new CFException());
309     checkCompletedExceptionally(f, ex);
310 dl 1.5 f.completeExceptionally(new CFException());
311 jsr166 1.72 checkCompletedExceptionally(f, ex);
312     f.complete(v1);
313     checkCompletedExceptionally(f, ex);
314     }}
315 jsr166 1.6
316 dl 1.5 /**
317     * getNumberOfDependents returns number of dependent tasks
318     */
319     public void testGetNumberOfDependents() {
320 jsr166 1.22 CompletableFuture<Integer> f = new CompletableFuture<>();
321 jsr166 1.46 assertEquals(0, f.getNumberOfDependents());
322 jsr166 1.55 CompletableFuture g = f.thenRun(new Noop(ExecutionMode.DEFAULT));
323 jsr166 1.46 assertEquals(1, f.getNumberOfDependents());
324     assertEquals(0, g.getNumberOfDependents());
325 jsr166 1.55 CompletableFuture h = f.thenRun(new Noop(ExecutionMode.DEFAULT));
326 jsr166 1.46 assertEquals(2, f.getNumberOfDependents());
327 dl 1.5 f.complete(1);
328     checkCompletedNormally(g, null);
329 jsr166 1.46 assertEquals(0, f.getNumberOfDependents());
330     assertEquals(0, g.getNumberOfDependents());
331 jsr166 1.3 }
332    
333 dl 1.5 /**
334     * toString indicates current completion state
335     */
336 jsr166 1.1 public void testToString() {
337     CompletableFuture<String> f;
338 jsr166 1.2
339 jsr166 1.1 f = new CompletableFuture<String>();
340 jsr166 1.2 assertTrue(f.toString().contains("[Not completed]"));
341    
342 jsr166 1.1 f.complete("foo");
343     assertTrue(f.toString().contains("[Completed normally]"));
344 jsr166 1.2
345 jsr166 1.1 f = new CompletableFuture<String>();
346     f.completeExceptionally(new IndexOutOfBoundsException());
347     assertTrue(f.toString().contains("[Completed exceptionally]"));
348 jsr166 1.74
349     f = new CompletableFuture<String>();
350     f.cancel(true);
351     assertTrue(f.toString().contains("[Completed exceptionally]"));
352    
353     f = new CompletableFuture<String>();
354     f.cancel(false);
355     assertTrue(f.toString().contains("[Completed exceptionally]"));
356 jsr166 1.1 }
357 jsr166 1.4
358 dl 1.9 /**
359 jsr166 1.10 * completedFuture returns a completed CompletableFuture with given value
360 dl 1.9 */
361     public void testCompletedFuture() {
362     CompletableFuture<String> f = CompletableFuture.completedFuture("test");
363     checkCompletedNormally(f, "test");
364     }
365    
366 jsr166 1.62 abstract class CheckedAction {
367     int invocationCount = 0;
368 jsr166 1.58 final ExecutionMode m;
369 jsr166 1.62 CheckedAction(ExecutionMode m) { this.m = m; }
370     void invoked() {
371     m.checkExecutionMode();
372     assertEquals(0, invocationCount++);
373     }
374     void assertNotInvoked() { assertEquals(0, invocationCount); }
375     void assertInvoked() { assertEquals(1, invocationCount); }
376     }
377    
378     abstract class CheckedIntegerAction extends CheckedAction {
379     Integer value;
380     CheckedIntegerAction(ExecutionMode m) { super(m); }
381     void assertValue(Integer expected) {
382     assertInvoked();
383     assertEquals(expected, value);
384     }
385     }
386    
387     class IntegerSupplier extends CheckedAction
388     implements Supplier<Integer>
389     {
390 jsr166 1.58 final Integer value;
391     IntegerSupplier(ExecutionMode m, Integer value) {
392 jsr166 1.62 super(m);
393 jsr166 1.58 this.value = value;
394     }
395     public Integer get() {
396 jsr166 1.62 invoked();
397 jsr166 1.58 return value;
398     }
399     }
400 jsr166 1.60
401     // A function that handles and produces null values as well.
402     static Integer inc(Integer x) {
403     return (x == null) ? null : x + 1;
404     }
405    
406 jsr166 1.64 class NoopConsumer extends CheckedIntegerAction
407 jsr166 1.62 implements Consumer<Integer>
408     {
409 jsr166 1.64 NoopConsumer(ExecutionMode m) { super(m); }
410 jsr166 1.38 public void accept(Integer x) {
411 jsr166 1.62 invoked();
412 jsr166 1.64 value = x;
413 jsr166 1.38 }
414     }
415 jsr166 1.62
416     class IncFunction extends CheckedIntegerAction
417     implements Function<Integer,Integer>
418     {
419     IncFunction(ExecutionMode m) { super(m); }
420 jsr166 1.38 public Integer apply(Integer x) {
421 jsr166 1.62 invoked();
422 jsr166 1.38 return value = inc(x);
423     }
424 dl 1.5 }
425 jsr166 1.60
426     // Choose non-commutative actions for better coverage
427     // A non-commutative function that handles and produces null values as well.
428     static Integer subtract(Integer x, Integer y) {
429     return (x == null && y == null) ? null :
430     ((x == null) ? 42 : x.intValue())
431     - ((y == null) ? 99 : y.intValue());
432     }
433    
434 jsr166 1.62 class SubtractAction extends CheckedIntegerAction
435     implements BiConsumer<Integer, Integer>
436     {
437     SubtractAction(ExecutionMode m) { super(m); }
438 jsr166 1.6 public void accept(Integer x, Integer y) {
439 jsr166 1.62 invoked();
440 jsr166 1.35 value = subtract(x, y);
441 dl 1.5 }
442     }
443 jsr166 1.62
444     class SubtractFunction extends CheckedIntegerAction
445     implements BiFunction<Integer, Integer, Integer>
446     {
447     SubtractFunction(ExecutionMode m) { super(m); }
448 jsr166 1.36 public Integer apply(Integer x, Integer y) {
449 jsr166 1.62 invoked();
450 jsr166 1.37 return value = subtract(x, y);
451 jsr166 1.36 }
452     }
453 jsr166 1.60
454 jsr166 1.62 class Noop extends CheckedAction implements Runnable {
455     Noop(ExecutionMode m) { super(m); }
456 jsr166 1.41 public void run() {
457 jsr166 1.62 invoked();
458 jsr166 1.41 }
459 dl 1.5 }
460    
461 jsr166 1.63 class FailingSupplier extends CheckedAction
462     implements Supplier<Integer>
463 jsr166 1.62 {
464     FailingSupplier(ExecutionMode m) { super(m); }
465 jsr166 1.44 public Integer get() {
466 jsr166 1.62 invoked();
467 jsr166 1.44 throw new CFException();
468     }
469 dl 1.5 }
470 jsr166 1.62
471 jsr166 1.63 class FailingConsumer extends CheckedIntegerAction
472     implements Consumer<Integer>
473     {
474 jsr166 1.62 FailingConsumer(ExecutionMode m) { super(m); }
475 jsr166 1.44 public void accept(Integer x) {
476 jsr166 1.62 invoked();
477 jsr166 1.63 value = x;
478 jsr166 1.44 throw new CFException();
479     }
480 dl 1.5 }
481 jsr166 1.62
482 jsr166 1.63 class FailingBiConsumer extends CheckedIntegerAction
483 jsr166 1.62 implements BiConsumer<Integer, Integer>
484     {
485     FailingBiConsumer(ExecutionMode m) { super(m); }
486 jsr166 1.44 public void accept(Integer x, Integer y) {
487 jsr166 1.62 invoked();
488 jsr166 1.63 value = subtract(x, y);
489 jsr166 1.44 throw new CFException();
490     }
491 dl 1.5 }
492 jsr166 1.62
493 jsr166 1.63 class FailingFunction extends CheckedIntegerAction
494 jsr166 1.62 implements Function<Integer, Integer>
495     {
496     FailingFunction(ExecutionMode m) { super(m); }
497 jsr166 1.44 public Integer apply(Integer x) {
498 jsr166 1.62 invoked();
499 jsr166 1.63 value = x;
500 jsr166 1.44 throw new CFException();
501     }
502 dl 1.5 }
503 jsr166 1.62
504 jsr166 1.63 class FailingBiFunction extends CheckedIntegerAction
505 jsr166 1.62 implements BiFunction<Integer, Integer, Integer>
506     {
507     FailingBiFunction(ExecutionMode m) { super(m); }
508 jsr166 1.44 public Integer apply(Integer x, Integer y) {
509 jsr166 1.62 invoked();
510 jsr166 1.63 value = subtract(x, y);
511 jsr166 1.44 throw new CFException();
512     }
513 dl 1.5 }
514 jsr166 1.62
515     class FailingRunnable extends CheckedAction implements Runnable {
516     FailingRunnable(ExecutionMode m) { super(m); }
517 jsr166 1.44 public void run() {
518 jsr166 1.62 invoked();
519 jsr166 1.44 throw new CFException();
520     }
521 dl 1.5 }
522    
523 jsr166 1.62
524 jsr166 1.63 class CompletableFutureInc extends CheckedIntegerAction
525 jsr166 1.62 implements Function<Integer, CompletableFuture<Integer>>
526     {
527     CompletableFutureInc(ExecutionMode m) { super(m); }
528 dl 1.5 public CompletableFuture<Integer> apply(Integer x) {
529 jsr166 1.62 invoked();
530 jsr166 1.63 value = x;
531 jsr166 1.22 CompletableFuture<Integer> f = new CompletableFuture<>();
532 jsr166 1.44 f.complete(inc(x));
533 dl 1.5 return f;
534     }
535     }
536    
537 jsr166 1.63 class FailingCompletableFutureFunction extends CheckedIntegerAction
538 jsr166 1.62 implements Function<Integer, CompletableFuture<Integer>>
539     {
540     FailingCompletableFutureFunction(ExecutionMode m) { super(m); }
541 dl 1.5 public CompletableFuture<Integer> apply(Integer x) {
542 jsr166 1.62 invoked();
543 jsr166 1.63 value = x;
544 jsr166 1.44 throw new CFException();
545 dl 1.5 }
546     }
547 jsr166 1.6
548 dl 1.5 // Used for explicit executor tests
549     static final class ThreadExecutor implements Executor {
550 jsr166 1.56 final AtomicInteger count = new AtomicInteger(0);
551     static final ThreadGroup tg = new ThreadGroup("ThreadExecutor");
552     static boolean startedCurrentThread() {
553     return Thread.currentThread().getThreadGroup() == tg;
554     }
555 jsr166 1.17
556 dl 1.5 public void execute(Runnable r) {
557 jsr166 1.17 count.getAndIncrement();
558 jsr166 1.56 new Thread(tg, r).start();
559 dl 1.5 }
560     }
561    
562     /**
563 jsr166 1.35 * Permits the testing of parallel code for the 3 different
564 jsr166 1.60 * execution modes without copy/pasting all the test methods.
565 jsr166 1.35 */
566     enum ExecutionMode {
567     DEFAULT {
568 jsr166 1.48 public void checkExecutionMode() {
569 jsr166 1.60 assertFalse(ThreadExecutor.startedCurrentThread());
570 jsr166 1.48 assertNull(ForkJoinTask.getPool());
571     }
572 jsr166 1.57 public CompletableFuture<Void> runAsync(Runnable a) {
573     throw new UnsupportedOperationException();
574     }
575 jsr166 1.58 public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
576     throw new UnsupportedOperationException();
577     }
578 jsr166 1.46 public <T> CompletableFuture<Void> thenRun
579     (CompletableFuture<T> f, Runnable a) {
580     return f.thenRun(a);
581     }
582     public <T> CompletableFuture<Void> thenAccept
583     (CompletableFuture<T> f, Consumer<? super T> a) {
584     return f.thenAccept(a);
585     }
586     public <T,U> CompletableFuture<U> thenApply
587     (CompletableFuture<T> f, Function<? super T,U> a) {
588     return f.thenApply(a);
589     }
590     public <T,U> CompletableFuture<U> thenCompose
591     (CompletableFuture<T> f,
592     Function<? super T,? extends CompletionStage<U>> a) {
593     return f.thenCompose(a);
594     }
595     public <T,U> CompletableFuture<U> handle
596     (CompletableFuture<T> f,
597     BiFunction<? super T,Throwable,? extends U> a) {
598     return f.handle(a);
599     }
600     public <T> CompletableFuture<T> whenComplete
601     (CompletableFuture<T> f,
602     BiConsumer<? super T,? super Throwable> a) {
603     return f.whenComplete(a);
604     }
605 jsr166 1.35 public <T,U> CompletableFuture<Void> runAfterBoth
606     (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
607     return f.runAfterBoth(g, a);
608     }
609     public <T,U> CompletableFuture<Void> thenAcceptBoth
610     (CompletableFuture<T> f,
611     CompletionStage<? extends U> g,
612     BiConsumer<? super T,? super U> a) {
613     return f.thenAcceptBoth(g, a);
614     }
615 jsr166 1.36 public <T,U,V> CompletableFuture<V> thenCombine
616     (CompletableFuture<T> f,
617     CompletionStage<? extends U> g,
618     BiFunction<? super T,? super U,? extends V> a) {
619     return f.thenCombine(g, a);
620     }
621 jsr166 1.46 public <T> CompletableFuture<Void> runAfterEither
622 jsr166 1.38 (CompletableFuture<T> f,
623 jsr166 1.46 CompletionStage<?> g,
624     java.lang.Runnable a) {
625     return f.runAfterEither(g, a);
626 jsr166 1.38 }
627     public <T> CompletableFuture<Void> acceptEither
628     (CompletableFuture<T> f,
629     CompletionStage<? extends T> g,
630     Consumer<? super T> a) {
631     return f.acceptEither(g, a);
632     }
633 jsr166 1.46 public <T,U> CompletableFuture<U> applyToEither
634 jsr166 1.38 (CompletableFuture<T> f,
635 jsr166 1.46 CompletionStage<? extends T> g,
636     Function<? super T,U> a) {
637     return f.applyToEither(g, a);
638     }
639     },
640    
641 jsr166 1.48 ASYNC {
642     public void checkExecutionMode() {
643     assertSame(ForkJoinPool.commonPool(),
644     ForkJoinTask.getPool());
645     }
646 jsr166 1.57 public CompletableFuture<Void> runAsync(Runnable a) {
647     return CompletableFuture.runAsync(a);
648     }
649 jsr166 1.58 public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
650     return CompletableFuture.supplyAsync(a);
651     }
652 jsr166 1.46 public <T> CompletableFuture<Void> thenRun
653     (CompletableFuture<T> f, Runnable a) {
654     return f.thenRunAsync(a);
655     }
656     public <T> CompletableFuture<Void> thenAccept
657     (CompletableFuture<T> f, Consumer<? super T> a) {
658     return f.thenAcceptAsync(a);
659     }
660     public <T,U> CompletableFuture<U> thenApply
661     (CompletableFuture<T> f, Function<? super T,U> a) {
662     return f.thenApplyAsync(a);
663 jsr166 1.38 }
664     public <T,U> CompletableFuture<U> thenCompose
665     (CompletableFuture<T> f,
666     Function<? super T,? extends CompletionStage<U>> a) {
667 jsr166 1.46 return f.thenComposeAsync(a);
668     }
669     public <T,U> CompletableFuture<U> handle
670     (CompletableFuture<T> f,
671     BiFunction<? super T,Throwable,? extends U> a) {
672     return f.handleAsync(a);
673 jsr166 1.38 }
674     public <T> CompletableFuture<T> whenComplete
675     (CompletableFuture<T> f,
676     BiConsumer<? super T,? super Throwable> a) {
677 jsr166 1.46 return f.whenCompleteAsync(a);
678 jsr166 1.38 }
679 jsr166 1.35 public <T,U> CompletableFuture<Void> runAfterBoth
680     (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
681     return f.runAfterBothAsync(g, a);
682     }
683     public <T,U> CompletableFuture<Void> thenAcceptBoth
684     (CompletableFuture<T> f,
685     CompletionStage<? extends U> g,
686     BiConsumer<? super T,? super U> a) {
687     return f.thenAcceptBothAsync(g, a);
688     }
689 jsr166 1.36 public <T,U,V> CompletableFuture<V> thenCombine
690     (CompletableFuture<T> f,
691     CompletionStage<? extends U> g,
692     BiFunction<? super T,? super U,? extends V> a) {
693     return f.thenCombineAsync(g, a);
694     }
695 jsr166 1.46 public <T> CompletableFuture<Void> runAfterEither
696 jsr166 1.38 (CompletableFuture<T> f,
697 jsr166 1.46 CompletionStage<?> g,
698     java.lang.Runnable a) {
699     return f.runAfterEitherAsync(g, a);
700 jsr166 1.38 }
701     public <T> CompletableFuture<Void> acceptEither
702     (CompletableFuture<T> f,
703     CompletionStage<? extends T> g,
704     Consumer<? super T> a) {
705     return f.acceptEitherAsync(g, a);
706     }
707 jsr166 1.46 public <T,U> CompletableFuture<U> applyToEither
708 jsr166 1.38 (CompletableFuture<T> f,
709 jsr166 1.46 CompletionStage<? extends T> g,
710     Function<? super T,U> a) {
711     return f.applyToEitherAsync(g, a);
712     }
713     },
714    
715     EXECUTOR {
716 jsr166 1.48 public void checkExecutionMode() {
717 jsr166 1.56 assertTrue(ThreadExecutor.startedCurrentThread());
718 jsr166 1.48 }
719 jsr166 1.57 public CompletableFuture<Void> runAsync(Runnable a) {
720     return CompletableFuture.runAsync(a, new ThreadExecutor());
721     }
722 jsr166 1.58 public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
723     return CompletableFuture.supplyAsync(a, new ThreadExecutor());
724     }
725 jsr166 1.46 public <T> CompletableFuture<Void> thenRun
726     (CompletableFuture<T> f, Runnable a) {
727     return f.thenRunAsync(a, new ThreadExecutor());
728     }
729     public <T> CompletableFuture<Void> thenAccept
730     (CompletableFuture<T> f, Consumer<? super T> a) {
731     return f.thenAcceptAsync(a, new ThreadExecutor());
732     }
733     public <T,U> CompletableFuture<U> thenApply
734     (CompletableFuture<T> f, Function<? super T,U> a) {
735     return f.thenApplyAsync(a, new ThreadExecutor());
736 jsr166 1.38 }
737     public <T,U> CompletableFuture<U> thenCompose
738     (CompletableFuture<T> f,
739     Function<? super T,? extends CompletionStage<U>> a) {
740 jsr166 1.46 return f.thenComposeAsync(a, new ThreadExecutor());
741     }
742     public <T,U> CompletableFuture<U> handle
743     (CompletableFuture<T> f,
744     BiFunction<? super T,Throwable,? extends U> a) {
745     return f.handleAsync(a, new ThreadExecutor());
746 jsr166 1.38 }
747     public <T> CompletableFuture<T> whenComplete
748     (CompletableFuture<T> f,
749     BiConsumer<? super T,? super Throwable> a) {
750 jsr166 1.46 return f.whenCompleteAsync(a, new ThreadExecutor());
751 jsr166 1.38 }
752 jsr166 1.35 public <T,U> CompletableFuture<Void> runAfterBoth
753     (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
754     return f.runAfterBothAsync(g, a, new ThreadExecutor());
755     }
756     public <T,U> CompletableFuture<Void> thenAcceptBoth
757     (CompletableFuture<T> f,
758     CompletionStage<? extends U> g,
759     BiConsumer<? super T,? super U> a) {
760     return f.thenAcceptBothAsync(g, a, new ThreadExecutor());
761     }
762 jsr166 1.36 public <T,U,V> CompletableFuture<V> thenCombine
763     (CompletableFuture<T> f,
764     CompletionStage<? extends U> g,
765     BiFunction<? super T,? super U,? extends V> a) {
766     return f.thenCombineAsync(g, a, new ThreadExecutor());
767     }
768 jsr166 1.46 public <T> CompletableFuture<Void> runAfterEither
769 jsr166 1.38 (CompletableFuture<T> f,
770 jsr166 1.46 CompletionStage<?> g,
771     java.lang.Runnable a) {
772     return f.runAfterEitherAsync(g, a, new ThreadExecutor());
773 jsr166 1.38 }
774     public <T> CompletableFuture<Void> acceptEither
775     (CompletableFuture<T> f,
776     CompletionStage<? extends T> g,
777     Consumer<? super T> a) {
778     return f.acceptEitherAsync(g, a, new ThreadExecutor());
779     }
780 jsr166 1.46 public <T,U> CompletableFuture<U> applyToEither
781 jsr166 1.38 (CompletableFuture<T> f,
782 jsr166 1.46 CompletionStage<? extends T> g,
783     Function<? super T,U> a) {
784     return f.applyToEitherAsync(g, a, new ThreadExecutor());
785 jsr166 1.38 }
786 jsr166 1.35 };
787    
788 jsr166 1.48 public abstract void checkExecutionMode();
789 jsr166 1.57 public abstract CompletableFuture<Void> runAsync(Runnable a);
790 jsr166 1.58 public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a);
791 jsr166 1.46 public abstract <T> CompletableFuture<Void> thenRun
792     (CompletableFuture<T> f, Runnable a);
793     public abstract <T> CompletableFuture<Void> thenAccept
794     (CompletableFuture<T> f, Consumer<? super T> a);
795     public abstract <T,U> CompletableFuture<U> thenApply
796     (CompletableFuture<T> f, Function<? super T,U> a);
797     public abstract <T,U> CompletableFuture<U> thenCompose
798     (CompletableFuture<T> f,
799     Function<? super T,? extends CompletionStage<U>> a);
800     public abstract <T,U> CompletableFuture<U> handle
801     (CompletableFuture<T> f,
802     BiFunction<? super T,Throwable,? extends U> a);
803     public abstract <T> CompletableFuture<T> whenComplete
804     (CompletableFuture<T> f,
805     BiConsumer<? super T,? super Throwable> a);
806 jsr166 1.35 public abstract <T,U> CompletableFuture<Void> runAfterBoth
807     (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a);
808     public abstract <T,U> CompletableFuture<Void> thenAcceptBoth
809     (CompletableFuture<T> f,
810     CompletionStage<? extends U> g,
811     BiConsumer<? super T,? super U> a);
812 jsr166 1.36 public abstract <T,U,V> CompletableFuture<V> thenCombine
813     (CompletableFuture<T> f,
814     CompletionStage<? extends U> g,
815     BiFunction<? super T,? super U,? extends V> a);
816 jsr166 1.46 public abstract <T> CompletableFuture<Void> runAfterEither
817 jsr166 1.38 (CompletableFuture<T> f,
818 jsr166 1.46 CompletionStage<?> g,
819     java.lang.Runnable a);
820 jsr166 1.38 public abstract <T> CompletableFuture<Void> acceptEither
821     (CompletableFuture<T> f,
822     CompletionStage<? extends T> g,
823     Consumer<? super T> a);
824 jsr166 1.46 public abstract <T,U> CompletableFuture<U> applyToEither
825 jsr166 1.38 (CompletableFuture<T> f,
826 jsr166 1.46 CompletionStage<? extends T> g,
827     Function<? super T,U> a);
828     }
829    
830     /**
831     * exceptionally action is not invoked when source completes
832     * normally, and source result is propagated
833     */
834     public void testExceptionally_normalCompletion() {
835     for (boolean createIncomplete : new boolean[] { true, false })
836     for (Integer v1 : new Integer[] { 1, null })
837     {
838     final AtomicInteger a = new AtomicInteger(0);
839     final CompletableFuture<Integer> f = new CompletableFuture<>();
840     if (!createIncomplete) f.complete(v1);
841     final CompletableFuture<Integer> g = f.exceptionally
842     ((Throwable t) -> {
843     // Should not be called
844     a.getAndIncrement();
845     throw new AssertionError();
846     });
847     if (createIncomplete) f.complete(v1);
848 jsr166 1.38
849 jsr166 1.46 checkCompletedNormally(g, v1);
850     checkCompletedNormally(f, v1);
851     assertEquals(0, a.get());
852     }}
853 jsr166 1.38
854 jsr166 1.35 /**
855 dl 1.5 * exceptionally action completes with function value on source
856 jsr166 1.46 * exception
857     */
858     public void testExceptionally_exceptionalCompletion() {
859     for (boolean createIncomplete : new boolean[] { true, false })
860     for (Integer v1 : new Integer[] { 1, null })
861     {
862     final AtomicInteger a = new AtomicInteger(0);
863     final CFException ex = new CFException();
864     final CompletableFuture<Integer> f = new CompletableFuture<>();
865     if (!createIncomplete) f.completeExceptionally(ex);
866     final CompletableFuture<Integer> g = f.exceptionally
867     ((Throwable t) -> {
868 jsr166 1.57 ExecutionMode.DEFAULT.checkExecutionMode();
869 jsr166 1.46 threadAssertSame(t, ex);
870     a.getAndIncrement();
871     return v1;
872     });
873     if (createIncomplete) f.completeExceptionally(ex);
874    
875     checkCompletedNormally(g, v1);
876     assertEquals(1, a.get());
877     }}
878    
879     public void testExceptionally_exceptionalCompletionActionFailed() {
880     for (boolean createIncomplete : new boolean[] { true, false })
881     for (Integer v1 : new Integer[] { 1, null })
882     {
883     final AtomicInteger a = new AtomicInteger(0);
884     final CFException ex1 = new CFException();
885     final CFException ex2 = new CFException();
886     final CompletableFuture<Integer> f = new CompletableFuture<>();
887     if (!createIncomplete) f.completeExceptionally(ex1);
888     final CompletableFuture<Integer> g = f.exceptionally
889     ((Throwable t) -> {
890 jsr166 1.57 ExecutionMode.DEFAULT.checkExecutionMode();
891 jsr166 1.46 threadAssertSame(t, ex1);
892     a.getAndIncrement();
893     throw ex2;
894     });
895     if (createIncomplete) f.completeExceptionally(ex1);
896    
897 jsr166 1.72 checkCompletedWithWrappedException(g, ex2);
898 jsr166 1.46 assertEquals(1, a.get());
899     }}
900    
901     /**
902 jsr166 1.75 * whenComplete action executes on normal completion, propagating
903     * source result.
904     */
905     public void testWhenComplete_normalCompletion1() {
906     for (ExecutionMode m : ExecutionMode.values())
907     for (boolean createIncomplete : new boolean[] { true, false })
908     for (Integer v1 : new Integer[] { 1, null })
909     {
910     final AtomicInteger a = new AtomicInteger(0);
911     final CompletableFuture<Integer> f = new CompletableFuture<>();
912     if (!createIncomplete) f.complete(v1);
913     final CompletableFuture<Integer> g = m.whenComplete
914     (f,
915     (Integer x, Throwable t) -> {
916     m.checkExecutionMode();
917     threadAssertSame(x, v1);
918     threadAssertNull(t);
919     a.getAndIncrement();
920     });
921     if (createIncomplete) f.complete(v1);
922    
923     checkCompletedNormally(g, v1);
924     checkCompletedNormally(f, v1);
925     assertEquals(1, a.get());
926     }}
927    
928     /**
929     * whenComplete action executes on exceptional completion, propagating
930     * source result.
931     */
932     public void testWhenComplete_exceptionalCompletion() {
933     for (ExecutionMode m : ExecutionMode.values())
934     for (boolean createIncomplete : new boolean[] { true, false })
935     for (Integer v1 : new Integer[] { 1, null })
936     {
937     final AtomicInteger a = new AtomicInteger(0);
938     final CFException ex = new CFException();
939     final CompletableFuture<Integer> f = new CompletableFuture<>();
940     if (!createIncomplete) f.completeExceptionally(ex);
941     final CompletableFuture<Integer> g = m.whenComplete
942     (f,
943     (Integer x, Throwable t) -> {
944     m.checkExecutionMode();
945     threadAssertNull(x);
946     threadAssertSame(t, ex);
947     a.getAndIncrement();
948     });
949     if (createIncomplete) f.completeExceptionally(ex);
950    
951     checkCompletedWithWrappedException(g, ex);
952     checkCompletedExceptionally(f, ex);
953     assertEquals(1, a.get());
954     }}
955    
956     /**
957     * whenComplete action executes on cancelled source, propagating
958     * CancellationException.
959     */
960     public void testWhenComplete_sourceCancelled() {
961     for (ExecutionMode m : ExecutionMode.values())
962     for (boolean mayInterruptIfRunning : new boolean[] { true, false })
963     for (boolean createIncomplete : new boolean[] { true, false })
964     {
965     final AtomicInteger a = new AtomicInteger(0);
966     final CompletableFuture<Integer> f = new CompletableFuture<>();
967     if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
968     final CompletableFuture<Integer> g = m.whenComplete
969     (f,
970     (Integer x, Throwable t) -> {
971     m.checkExecutionMode();
972     threadAssertNull(x);
973     threadAssertTrue(t instanceof CancellationException);
974     a.getAndIncrement();
975     });
976     if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
977    
978     checkCompletedWithWrappedCancellationException(g);
979     checkCancelled(f);
980     assertEquals(1, a.get());
981     }}
982    
983     /**
984     * If a whenComplete action throws an exception when triggered by
985     * a normal completion, it completes exceptionally
986     */
987     public void testWhenComplete_actionFailed() {
988     for (boolean createIncomplete : new boolean[] { true, false })
989     for (ExecutionMode m : ExecutionMode.values())
990     for (Integer v1 : new Integer[] { 1, null })
991     {
992     final AtomicInteger a = new AtomicInteger(0);
993     final CFException ex = new CFException();
994     final CompletableFuture<Integer> f = new CompletableFuture<>();
995     if (!createIncomplete) f.complete(v1);
996     final CompletableFuture<Integer> g = m.whenComplete
997     (f,
998     (Integer x, Throwable t) -> {
999     m.checkExecutionMode();
1000     threadAssertSame(x, v1);
1001     threadAssertNull(t);
1002     a.getAndIncrement();
1003     throw ex;
1004     });
1005     if (createIncomplete) f.complete(v1);
1006    
1007     checkCompletedWithWrappedException(g, ex);
1008     checkCompletedNormally(f, v1);
1009     assertEquals(1, a.get());
1010     }}
1011    
1012     /**
1013     * If a whenComplete action throws an exception when triggered by
1014     * a source completion that also throws an exception, the source
1015     * exception takes precedence.
1016     */
1017     public void testWhenComplete_actionFailedSourceFailed() {
1018     for (boolean createIncomplete : new boolean[] { true, false })
1019     for (ExecutionMode m : ExecutionMode.values())
1020     for (Integer v1 : new Integer[] { 1, null })
1021     {
1022     final AtomicInteger a = new AtomicInteger(0);
1023     final CFException ex1 = new CFException();
1024     final CFException ex2 = new CFException();
1025     final CompletableFuture<Integer> f = new CompletableFuture<>();
1026    
1027     if (!createIncomplete) f.completeExceptionally(ex1);
1028     final CompletableFuture<Integer> g = m.whenComplete
1029     (f,
1030     (Integer x, Throwable t) -> {
1031     m.checkExecutionMode();
1032     threadAssertSame(t, ex1);
1033     threadAssertNull(x);
1034     a.getAndIncrement();
1035     throw ex2;
1036     });
1037     if (createIncomplete) f.completeExceptionally(ex1);
1038    
1039     checkCompletedWithWrappedException(g, ex1);
1040     checkCompletedExceptionally(f, ex1);
1041     assertEquals(1, a.get());
1042     }}
1043    
1044     /**
1045 jsr166 1.46 * handle action completes normally with function value on normal
1046     * completion of source
1047     */
1048     public void testHandle_normalCompletion() {
1049     for (ExecutionMode m : ExecutionMode.values())
1050     for (boolean createIncomplete : new boolean[] { true, false })
1051     for (Integer v1 : new Integer[] { 1, null })
1052     {
1053     final CompletableFuture<Integer> f = new CompletableFuture<>();
1054     final AtomicInteger a = new AtomicInteger(0);
1055     if (!createIncomplete) f.complete(v1);
1056     final CompletableFuture<Integer> g = m.handle
1057     (f,
1058     (Integer x, Throwable t) -> {
1059 jsr166 1.57 m.checkExecutionMode();
1060 jsr166 1.46 threadAssertSame(x, v1);
1061     threadAssertNull(t);
1062     a.getAndIncrement();
1063     return inc(v1);
1064     });
1065     if (createIncomplete) f.complete(v1);
1066    
1067     checkCompletedNormally(g, inc(v1));
1068     checkCompletedNormally(f, v1);
1069     assertEquals(1, a.get());
1070     }}
1071    
1072     /**
1073     * handle action completes normally with function value on
1074     * exceptional completion of source
1075 dl 1.5 */
1076 jsr166 1.46 public void testHandle_exceptionalCompletion() {
1077     for (ExecutionMode m : ExecutionMode.values())
1078     for (boolean createIncomplete : new boolean[] { true, false })
1079     for (Integer v1 : new Integer[] { 1, null })
1080     {
1081     final CompletableFuture<Integer> f = new CompletableFuture<>();
1082     final AtomicInteger a = new AtomicInteger(0);
1083     final CFException ex = new CFException();
1084     if (!createIncomplete) f.completeExceptionally(ex);
1085     final CompletableFuture<Integer> g = m.handle
1086     (f,
1087     (Integer x, Throwable t) -> {
1088 jsr166 1.57 m.checkExecutionMode();
1089 jsr166 1.46 threadAssertNull(x);
1090     threadAssertSame(t, ex);
1091     a.getAndIncrement();
1092     return v1;
1093     });
1094     if (createIncomplete) f.completeExceptionally(ex);
1095 dl 1.5
1096 jsr166 1.46 checkCompletedNormally(g, v1);
1097 jsr166 1.72 checkCompletedExceptionally(f, ex);
1098 jsr166 1.46 assertEquals(1, a.get());
1099     }}
1100 dl 1.5
1101     /**
1102 jsr166 1.46 * handle action completes normally with function value on
1103     * cancelled source
1104 dl 1.5 */
1105 jsr166 1.46 public void testHandle_sourceCancelled() {
1106     for (ExecutionMode m : ExecutionMode.values())
1107     for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1108     for (boolean createIncomplete : new boolean[] { true, false })
1109     for (Integer v1 : new Integer[] { 1, null })
1110     {
1111     final CompletableFuture<Integer> f = new CompletableFuture<>();
1112     final AtomicInteger a = new AtomicInteger(0);
1113     if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1114     final CompletableFuture<Integer> g = m.handle
1115     (f,
1116     (Integer x, Throwable t) -> {
1117 jsr166 1.57 m.checkExecutionMode();
1118 jsr166 1.46 threadAssertNull(x);
1119     threadAssertTrue(t instanceof CancellationException);
1120     a.getAndIncrement();
1121     return v1;
1122     });
1123     if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1124    
1125     checkCompletedNormally(g, v1);
1126     checkCancelled(f);
1127     assertEquals(1, a.get());
1128     }}
1129 jsr166 1.15
1130 jsr166 1.46 /**
1131     * handle result completes exceptionally if action does
1132     */
1133     public void testHandle_sourceFailedActionFailed() {
1134     for (ExecutionMode m : ExecutionMode.values())
1135     for (boolean createIncomplete : new boolean[] { true, false })
1136     {
1137     final CompletableFuture<Integer> f = new CompletableFuture<>();
1138     final AtomicInteger a = new AtomicInteger(0);
1139     final CFException ex1 = new CFException();
1140     final CFException ex2 = new CFException();
1141     if (!createIncomplete) f.completeExceptionally(ex1);
1142     final CompletableFuture<Integer> g = m.handle
1143     (f,
1144     (Integer x, Throwable t) -> {
1145 jsr166 1.57 m.checkExecutionMode();
1146 jsr166 1.46 threadAssertNull(x);
1147     threadAssertSame(ex1, t);
1148     a.getAndIncrement();
1149     throw ex2;
1150     });
1151     if (createIncomplete) f.completeExceptionally(ex1);
1152 dl 1.5
1153 jsr166 1.72 checkCompletedWithWrappedException(g, ex2);
1154     checkCompletedExceptionally(f, ex1);
1155 jsr166 1.46 assertEquals(1, a.get());
1156     }}
1157 jsr166 1.15
1158 jsr166 1.46 public void testHandle_sourceCompletedNormallyActionFailed() {
1159     for (ExecutionMode m : ExecutionMode.values())
1160     for (boolean createIncomplete : new boolean[] { true, false })
1161     for (Integer v1 : new Integer[] { 1, null })
1162     {
1163     final CompletableFuture<Integer> f = new CompletableFuture<>();
1164     final AtomicInteger a = new AtomicInteger(0);
1165     final CFException ex = new CFException();
1166     if (!createIncomplete) f.complete(v1);
1167     final CompletableFuture<Integer> g = m.handle
1168     (f,
1169     (Integer x, Throwable t) -> {
1170 jsr166 1.57 m.checkExecutionMode();
1171 jsr166 1.46 threadAssertSame(x, v1);
1172     threadAssertNull(t);
1173     a.getAndIncrement();
1174     throw ex;
1175     });
1176     if (createIncomplete) f.complete(v1);
1177 jsr166 1.15
1178 jsr166 1.72 checkCompletedWithWrappedException(g, ex);
1179 jsr166 1.46 checkCompletedNormally(f, v1);
1180     assertEquals(1, a.get());
1181     }}
1182 dl 1.5
1183     /**
1184     * runAsync completes after running Runnable
1185     */
1186 jsr166 1.57 public void testRunAsync_normalCompletion() {
1187     ExecutionMode[] executionModes = {
1188     ExecutionMode.ASYNC,
1189     ExecutionMode.EXECUTOR,
1190     };
1191     for (ExecutionMode m : executionModes)
1192     {
1193     final Noop r = new Noop(m);
1194     final CompletableFuture<Void> f = m.runAsync(r);
1195 dl 1.5 assertNull(f.join());
1196 jsr166 1.14 checkCompletedNormally(f, null);
1197 jsr166 1.62 r.assertInvoked();
1198 jsr166 1.57 }}
1199 dl 1.5
1200     /**
1201     * failing runAsync completes exceptionally after running Runnable
1202     */
1203 jsr166 1.57 public void testRunAsync_exceptionalCompletion() {
1204     ExecutionMode[] executionModes = {
1205     ExecutionMode.ASYNC,
1206     ExecutionMode.EXECUTOR,
1207     };
1208     for (ExecutionMode m : executionModes)
1209     {
1210     final FailingRunnable r = new FailingRunnable(m);
1211     final CompletableFuture<Void> f = m.runAsync(r);
1212 dl 1.5 checkCompletedWithWrappedCFException(f);
1213 jsr166 1.62 r.assertInvoked();
1214 jsr166 1.57 }}
1215 dl 1.5
1216     /**
1217     * supplyAsync completes with result of supplier
1218     */
1219 jsr166 1.58 public void testSupplyAsync_normalCompletion() {
1220     ExecutionMode[] executionModes = {
1221     ExecutionMode.ASYNC,
1222     ExecutionMode.EXECUTOR,
1223     };
1224     for (ExecutionMode m : executionModes)
1225     for (Integer v1 : new Integer[] { 1, null })
1226     {
1227     final IntegerSupplier r = new IntegerSupplier(m, v1);
1228     final CompletableFuture<Integer> f = m.supplyAsync(r);
1229     assertSame(v1, f.join());
1230     checkCompletedNormally(f, v1);
1231 jsr166 1.62 r.assertInvoked();
1232 jsr166 1.58 }}
1233 dl 1.5
1234     /**
1235     * Failing supplyAsync completes exceptionally
1236     */
1237 jsr166 1.58 public void testSupplyAsync_exceptionalCompletion() {
1238     ExecutionMode[] executionModes = {
1239     ExecutionMode.ASYNC,
1240     ExecutionMode.EXECUTOR,
1241     };
1242     for (ExecutionMode m : executionModes)
1243     {
1244     FailingSupplier r = new FailingSupplier(m);
1245     CompletableFuture<Integer> f = m.supplyAsync(r);
1246 dl 1.5 checkCompletedWithWrappedCFException(f);
1247 jsr166 1.62 r.assertInvoked();
1248 jsr166 1.58 }}
1249 dl 1.5
1250 jsr166 1.7 // seq completion methods
1251 jsr166 1.6
1252 dl 1.5 /**
1253     * thenRun result completes normally after normal completion of source
1254     */
1255 jsr166 1.48 public void testThenRun_normalCompletion() {
1256     for (ExecutionMode m : ExecutionMode.values())
1257     for (boolean createIncomplete : new boolean[] { true, false })
1258     for (Integer v1 : new Integer[] { 1, null })
1259     {
1260     final CompletableFuture<Integer> f = new CompletableFuture<>();
1261 jsr166 1.55 final Noop r = new Noop(m);
1262 jsr166 1.48 if (!createIncomplete) f.complete(v1);
1263     final CompletableFuture<Void> g = m.thenRun(f, r);
1264 jsr166 1.51 if (createIncomplete) {
1265     checkIncomplete(g);
1266     f.complete(v1);
1267     }
1268 jsr166 1.23
1269 dl 1.5 checkCompletedNormally(g, null);
1270 jsr166 1.48 checkCompletedNormally(f, v1);
1271 jsr166 1.62 r.assertInvoked();
1272 jsr166 1.48 }}
1273 dl 1.5
1274     /**
1275     * thenRun result completes exceptionally after exceptional
1276     * completion of source
1277     */
1278 jsr166 1.48 public void testThenRun_exceptionalCompletion() {
1279     for (ExecutionMode m : ExecutionMode.values())
1280     for (boolean createIncomplete : new boolean[] { true, false })
1281     {
1282     final CFException ex = new CFException();
1283     final CompletableFuture<Integer> f = new CompletableFuture<>();
1284 jsr166 1.55 final Noop r = new Noop(m);
1285 jsr166 1.48 if (!createIncomplete) f.completeExceptionally(ex);
1286     final CompletableFuture<Void> g = m.thenRun(f, r);
1287 jsr166 1.51 if (createIncomplete) {
1288     checkIncomplete(g);
1289     f.completeExceptionally(ex);
1290     }
1291 jsr166 1.23
1292 jsr166 1.72 checkCompletedWithWrappedException(g, ex);
1293     checkCompletedExceptionally(f, ex);
1294 jsr166 1.62 r.assertNotInvoked();
1295 jsr166 1.48 }}
1296    
1297     /**
1298     * thenRun result completes exceptionally if source cancelled
1299     */
1300     public void testThenRun_sourceCancelled() {
1301     for (ExecutionMode m : ExecutionMode.values())
1302     for (boolean createIncomplete : new boolean[] { true, false })
1303     for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1304     {
1305     final CompletableFuture<Integer> f = new CompletableFuture<>();
1306 jsr166 1.55 final Noop r = new Noop(m);
1307 jsr166 1.48 if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1308 jsr166 1.56 final CompletableFuture<Void> g = m.thenRun(f, r);
1309 jsr166 1.51 if (createIncomplete) {
1310     checkIncomplete(g);
1311     assertTrue(f.cancel(mayInterruptIfRunning));
1312     }
1313 jsr166 1.23
1314 jsr166 1.48 checkCompletedWithWrappedCancellationException(g);
1315     checkCancelled(f);
1316 jsr166 1.62 r.assertNotInvoked();
1317 jsr166 1.48 }}
1318 dl 1.5
1319     /**
1320     * thenRun result completes exceptionally if action does
1321     */
1322 jsr166 1.48 public void testThenRun_actionFailed() {
1323     for (ExecutionMode m : ExecutionMode.values())
1324     for (boolean createIncomplete : new boolean[] { true, false })
1325     for (Integer v1 : new Integer[] { 1, null })
1326     {
1327     final CompletableFuture<Integer> f = new CompletableFuture<>();
1328 jsr166 1.56 final FailingRunnable r = new FailingRunnable(m);
1329 jsr166 1.48 if (!createIncomplete) f.complete(v1);
1330 jsr166 1.56 final CompletableFuture<Void> g = m.thenRun(f, r);
1331 jsr166 1.51 if (createIncomplete) {
1332     checkIncomplete(g);
1333     f.complete(v1);
1334     }
1335 jsr166 1.23
1336     checkCompletedWithWrappedCFException(g);
1337 jsr166 1.48 checkCompletedNormally(f, v1);
1338     }}
1339 dl 1.5
1340     /**
1341     * thenApply result completes normally after normal completion of source
1342     */
1343 jsr166 1.49 public void testThenApply_normalCompletion() {
1344     for (ExecutionMode m : ExecutionMode.values())
1345     for (boolean createIncomplete : new boolean[] { true, false })
1346     for (Integer v1 : new Integer[] { 1, null })
1347     {
1348     final CompletableFuture<Integer> f = new CompletableFuture<>();
1349 jsr166 1.56 final IncFunction r = new IncFunction(m);
1350 jsr166 1.49 if (!createIncomplete) f.complete(v1);
1351     final CompletableFuture<Integer> g = m.thenApply(f, r);
1352     if (createIncomplete) {
1353     checkIncomplete(g);
1354     f.complete(v1);
1355     }
1356    
1357     checkCompletedNormally(g, inc(v1));
1358     checkCompletedNormally(f, v1);
1359 jsr166 1.70 r.assertValue(inc(v1));
1360 jsr166 1.49 }}
1361 dl 1.5
1362     /**
1363     * thenApply result completes exceptionally after exceptional
1364     * completion of source
1365     */
1366 jsr166 1.49 public void testThenApply_exceptionalCompletion() {
1367     for (ExecutionMode m : ExecutionMode.values())
1368     for (boolean createIncomplete : new boolean[] { true, false })
1369     {
1370     final CFException ex = new CFException();
1371     final CompletableFuture<Integer> f = new CompletableFuture<>();
1372 jsr166 1.56 final IncFunction r = new IncFunction(m);
1373 jsr166 1.49 if (!createIncomplete) f.completeExceptionally(ex);
1374     final CompletableFuture<Integer> g = m.thenApply(f, r);
1375 jsr166 1.51 if (createIncomplete) {
1376     checkIncomplete(g);
1377     f.completeExceptionally(ex);
1378     }
1379 jsr166 1.49
1380 jsr166 1.72 checkCompletedWithWrappedException(g, ex);
1381     checkCompletedExceptionally(f, ex);
1382 jsr166 1.62 r.assertNotInvoked();
1383 jsr166 1.49 }}
1384 dl 1.5
1385     /**
1386 jsr166 1.49 * thenApply result completes exceptionally if source cancelled
1387 dl 1.5 */
1388 jsr166 1.49 public void testThenApply_sourceCancelled() {
1389     for (ExecutionMode m : ExecutionMode.values())
1390     for (boolean createIncomplete : new boolean[] { true, false })
1391     for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1392     {
1393     final CompletableFuture<Integer> f = new CompletableFuture<>();
1394 jsr166 1.56 final IncFunction r = new IncFunction(m);
1395 jsr166 1.49 if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1396 jsr166 1.56 final CompletableFuture<Integer> g = m.thenApply(f, r);
1397 jsr166 1.51 if (createIncomplete) {
1398     checkIncomplete(g);
1399     assertTrue(f.cancel(mayInterruptIfRunning));
1400     }
1401 jsr166 1.49
1402     checkCompletedWithWrappedCancellationException(g);
1403     checkCancelled(f);
1404 jsr166 1.62 r.assertNotInvoked();
1405 jsr166 1.49 }}
1406 dl 1.5
1407     /**
1408 jsr166 1.49 * thenApply result completes exceptionally if action does
1409 dl 1.5 */
1410 jsr166 1.49 public void testThenApply_actionFailed() {
1411     for (ExecutionMode m : ExecutionMode.values())
1412     for (boolean createIncomplete : new boolean[] { true, false })
1413     for (Integer v1 : new Integer[] { 1, null })
1414     {
1415     final CompletableFuture<Integer> f = new CompletableFuture<>();
1416 jsr166 1.56 final FailingFunction r = new FailingFunction(m);
1417 jsr166 1.49 if (!createIncomplete) f.complete(v1);
1418 jsr166 1.56 final CompletableFuture<Integer> g = m.thenApply(f, r);
1419 jsr166 1.51 if (createIncomplete) {
1420     checkIncomplete(g);
1421     f.complete(v1);
1422     }
1423 jsr166 1.49
1424     checkCompletedWithWrappedCFException(g);
1425     checkCompletedNormally(f, v1);
1426     }}
1427 dl 1.5
1428     /**
1429     * thenAccept result completes normally after normal completion of source
1430     */
1431 jsr166 1.50 public void testThenAccept_normalCompletion() {
1432     for (ExecutionMode m : ExecutionMode.values())
1433     for (boolean createIncomplete : new boolean[] { true, false })
1434     for (Integer v1 : new Integer[] { 1, null })
1435     {
1436     final CompletableFuture<Integer> f = new CompletableFuture<>();
1437 jsr166 1.64 final NoopConsumer r = new NoopConsumer(m);
1438 jsr166 1.50 if (!createIncomplete) f.complete(v1);
1439     final CompletableFuture<Void> g = m.thenAccept(f, r);
1440 jsr166 1.51 if (createIncomplete) {
1441     checkIncomplete(g);
1442     f.complete(v1);
1443     }
1444 jsr166 1.50
1445 dl 1.5 checkCompletedNormally(g, null);
1446 jsr166 1.64 r.assertValue(v1);
1447 jsr166 1.50 checkCompletedNormally(f, v1);
1448     }}
1449 dl 1.5
1450     /**
1451     * thenAccept result completes exceptionally after exceptional
1452     * completion of source
1453     */
1454 jsr166 1.50 public void testThenAccept_exceptionalCompletion() {
1455     for (ExecutionMode m : ExecutionMode.values())
1456     for (boolean createIncomplete : new boolean[] { true, false })
1457     {
1458     final CFException ex = new CFException();
1459     final CompletableFuture<Integer> f = new CompletableFuture<>();
1460 jsr166 1.64 final NoopConsumer r = new NoopConsumer(m);
1461 jsr166 1.50 if (!createIncomplete) f.completeExceptionally(ex);
1462     final CompletableFuture<Void> g = m.thenAccept(f, r);
1463 jsr166 1.51 if (createIncomplete) {
1464     checkIncomplete(g);
1465     f.completeExceptionally(ex);
1466     }
1467 jsr166 1.50
1468 jsr166 1.72 checkCompletedWithWrappedException(g, ex);
1469     checkCompletedExceptionally(f, ex);
1470 jsr166 1.62 r.assertNotInvoked();
1471 jsr166 1.50 }}
1472 dl 1.5
1473     /**
1474 jsr166 1.61 * thenAccept result completes exceptionally if source cancelled
1475 dl 1.5 */
1476 jsr166 1.61 public void testThenAccept_sourceCancelled() {
1477 jsr166 1.50 for (ExecutionMode m : ExecutionMode.values())
1478     for (boolean createIncomplete : new boolean[] { true, false })
1479 jsr166 1.61 for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1480 jsr166 1.50 {
1481     final CompletableFuture<Integer> f = new CompletableFuture<>();
1482 jsr166 1.64 final NoopConsumer r = new NoopConsumer(m);
1483 jsr166 1.61 if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1484 jsr166 1.56 final CompletableFuture<Void> g = m.thenAccept(f, r);
1485 jsr166 1.51 if (createIncomplete) {
1486     checkIncomplete(g);
1487 jsr166 1.61 assertTrue(f.cancel(mayInterruptIfRunning));
1488 jsr166 1.51 }
1489 jsr166 1.50
1490 jsr166 1.61 checkCompletedWithWrappedCancellationException(g);
1491     checkCancelled(f);
1492 jsr166 1.62 r.assertNotInvoked();
1493 jsr166 1.50 }}
1494 dl 1.5
1495     /**
1496 jsr166 1.61 * thenAccept result completes exceptionally if action does
1497 dl 1.5 */
1498 jsr166 1.61 public void testThenAccept_actionFailed() {
1499 jsr166 1.50 for (ExecutionMode m : ExecutionMode.values())
1500     for (boolean createIncomplete : new boolean[] { true, false })
1501 jsr166 1.61 for (Integer v1 : new Integer[] { 1, null })
1502 jsr166 1.50 {
1503     final CompletableFuture<Integer> f = new CompletableFuture<>();
1504 jsr166 1.61 final FailingConsumer r = new FailingConsumer(m);
1505     if (!createIncomplete) f.complete(v1);
1506 jsr166 1.56 final CompletableFuture<Void> g = m.thenAccept(f, r);
1507 jsr166 1.50 if (createIncomplete) {
1508     checkIncomplete(g);
1509 jsr166 1.61 f.complete(v1);
1510 jsr166 1.50 }
1511    
1512 jsr166 1.61 checkCompletedWithWrappedCFException(g);
1513     checkCompletedNormally(f, v1);
1514 jsr166 1.50 }}
1515 dl 1.5
1516     /**
1517 jsr166 1.18 * thenCombine result completes normally after normal completion
1518     * of sources
1519 dl 1.5 */
1520 jsr166 1.51 public void testThenCombine_normalCompletion() {
1521     for (ExecutionMode m : ExecutionMode.values())
1522 jsr166 1.44 for (boolean createIncomplete : new boolean[] { true, false })
1523 jsr166 1.43 for (boolean fFirst : new boolean[] { true, false })
1524 jsr166 1.36 for (Integer v1 : new Integer[] { 1, null })
1525 jsr166 1.47 for (Integer v2 : new Integer[] { 2, null })
1526     {
1527 jsr166 1.36 final CompletableFuture<Integer> f = new CompletableFuture<>();
1528     final CompletableFuture<Integer> g = new CompletableFuture<>();
1529 jsr166 1.56 final SubtractFunction r = new SubtractFunction(m);
1530 jsr166 1.36
1531 jsr166 1.51 if (fFirst) f.complete(v1); else g.complete(v2);
1532     if (!createIncomplete)
1533     if (!fFirst) f.complete(v1); else g.complete(v2);
1534     final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1535     if (createIncomplete) {
1536     checkIncomplete(h);
1537 jsr166 1.62 r.assertNotInvoked();
1538 jsr166 1.51 if (!fFirst) f.complete(v1); else g.complete(v2);
1539     }
1540 jsr166 1.36
1541     checkCompletedNormally(h, subtract(v1, v2));
1542     checkCompletedNormally(f, v1);
1543     checkCompletedNormally(g, v2);
1544 jsr166 1.70 r.assertValue(subtract(v1, v2));
1545 jsr166 1.47 }}
1546 dl 1.5
1547     /**
1548     * thenCombine result completes exceptionally after exceptional
1549     * completion of either source
1550     */
1551 jsr166 1.52 public void testThenCombine_exceptionalCompletion() {
1552 jsr166 1.36 for (ExecutionMode m : ExecutionMode.values())
1553 jsr166 1.52 for (boolean createIncomplete : new boolean[] { true, false })
1554     for (boolean fFirst : new boolean[] { true, false })
1555 jsr166 1.47 for (Integer v1 : new Integer[] { 1, null })
1556     {
1557 jsr166 1.36 final CompletableFuture<Integer> f = new CompletableFuture<>();
1558     final CompletableFuture<Integer> g = new CompletableFuture<>();
1559     final CFException ex = new CFException();
1560 jsr166 1.56 final SubtractFunction r = new SubtractFunction(m);
1561 jsr166 1.18
1562 jsr166 1.52 (fFirst ? f : g).complete(v1);
1563     if (!createIncomplete)
1564     (!fFirst ? f : g).completeExceptionally(ex);
1565 jsr166 1.37 final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1566 jsr166 1.52 if (createIncomplete) {
1567     checkIncomplete(h);
1568     (!fFirst ? f : g).completeExceptionally(ex);
1569     }
1570 dl 1.5
1571 jsr166 1.72 checkCompletedWithWrappedException(h, ex);
1572 jsr166 1.62 r.assertNotInvoked();
1573 jsr166 1.52 checkCompletedNormally(fFirst ? f : g, v1);
1574 jsr166 1.72 checkCompletedExceptionally(!fFirst ? f : g, ex);
1575 jsr166 1.47 }}
1576 dl 1.5
1577     /**
1578     * thenCombine result completes exceptionally if either source cancelled
1579     */
1580 jsr166 1.52 public void testThenCombine_sourceCancelled() {
1581 jsr166 1.36 for (ExecutionMode m : ExecutionMode.values())
1582     for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1583 jsr166 1.52 for (boolean createIncomplete : new boolean[] { true, false })
1584     for (boolean fFirst : new boolean[] { true, false })
1585 jsr166 1.47 for (Integer v1 : new Integer[] { 1, null })
1586     {
1587 jsr166 1.36 final CompletableFuture<Integer> f = new CompletableFuture<>();
1588     final CompletableFuture<Integer> g = new CompletableFuture<>();
1589 jsr166 1.56 final SubtractFunction r = new SubtractFunction(m);
1590 jsr166 1.18
1591 jsr166 1.52 (fFirst ? f : g).complete(v1);
1592     if (!createIncomplete)
1593     assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1594 jsr166 1.36 final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1595 jsr166 1.52 if (createIncomplete) {
1596     checkIncomplete(h);
1597     assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1598     }
1599 jsr166 1.36
1600 jsr166 1.18 checkCompletedWithWrappedCancellationException(h);
1601 jsr166 1.52 checkCancelled(!fFirst ? f : g);
1602 jsr166 1.62 r.assertNotInvoked();
1603 jsr166 1.52 checkCompletedNormally(fFirst ? f : g, v1);
1604 jsr166 1.47 }}
1605 dl 1.5
1606     /**
1607 jsr166 1.61 * thenCombine result completes exceptionally if action does
1608     */
1609     public void testThenCombine_actionFailed() {
1610     for (ExecutionMode m : ExecutionMode.values())
1611     for (boolean fFirst : new boolean[] { true, false })
1612     for (Integer v1 : new Integer[] { 1, null })
1613     for (Integer v2 : new Integer[] { 2, null })
1614     {
1615     final CompletableFuture<Integer> f = new CompletableFuture<>();
1616     final CompletableFuture<Integer> g = new CompletableFuture<>();
1617     final FailingBiFunction r = new FailingBiFunction(m);
1618     final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1619    
1620     if (fFirst) {
1621     f.complete(v1);
1622     g.complete(v2);
1623     } else {
1624     g.complete(v2);
1625     f.complete(v1);
1626     }
1627    
1628     checkCompletedWithWrappedCFException(h);
1629     checkCompletedNormally(f, v1);
1630     checkCompletedNormally(g, v2);
1631     }}
1632    
1633     /**
1634 dl 1.5 * thenAcceptBoth result completes normally after normal
1635     * completion of sources
1636     */
1637 jsr166 1.53 public void testThenAcceptBoth_normalCompletion() {
1638 jsr166 1.35 for (ExecutionMode m : ExecutionMode.values())
1639 jsr166 1.53 for (boolean createIncomplete : new boolean[] { true, false })
1640     for (boolean fFirst : new boolean[] { true, false })
1641 jsr166 1.35 for (Integer v1 : new Integer[] { 1, null })
1642 jsr166 1.47 for (Integer v2 : new Integer[] { 2, null })
1643     {
1644 jsr166 1.35 final CompletableFuture<Integer> f = new CompletableFuture<>();
1645     final CompletableFuture<Integer> g = new CompletableFuture<>();
1646 jsr166 1.56 final SubtractAction r = new SubtractAction(m);
1647 jsr166 1.35
1648 jsr166 1.53 if (fFirst) f.complete(v1); else g.complete(v2);
1649     if (!createIncomplete)
1650     if (!fFirst) f.complete(v1); else g.complete(v2);
1651 jsr166 1.35 final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1652 jsr166 1.53 if (createIncomplete) {
1653     checkIncomplete(h);
1654 jsr166 1.62 r.assertNotInvoked();
1655 jsr166 1.53 if (!fFirst) f.complete(v1); else g.complete(v2);
1656     }
1657 dl 1.5
1658 jsr166 1.22 checkCompletedNormally(h, null);
1659 jsr166 1.62 r.assertValue(subtract(v1, v2));
1660 jsr166 1.35 checkCompletedNormally(f, v1);
1661     checkCompletedNormally(g, v2);
1662 jsr166 1.47 }}
1663 dl 1.5
1664     /**
1665     * thenAcceptBoth result completes exceptionally after exceptional
1666     * completion of either source
1667     */
1668 jsr166 1.53 public void testThenAcceptBoth_exceptionalCompletion() {
1669 jsr166 1.35 for (ExecutionMode m : ExecutionMode.values())
1670 jsr166 1.53 for (boolean createIncomplete : new boolean[] { true, false })
1671     for (boolean fFirst : new boolean[] { true, false })
1672 jsr166 1.47 for (Integer v1 : new Integer[] { 1, null })
1673     {
1674 jsr166 1.35 final CompletableFuture<Integer> f = new CompletableFuture<>();
1675     final CompletableFuture<Integer> g = new CompletableFuture<>();
1676     final CFException ex = new CFException();
1677 jsr166 1.56 final SubtractAction r = new SubtractAction(m);
1678 jsr166 1.35
1679 jsr166 1.53 (fFirst ? f : g).complete(v1);
1680     if (!createIncomplete)
1681     (!fFirst ? f : g).completeExceptionally(ex);
1682 jsr166 1.35 final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1683 jsr166 1.53 if (createIncomplete) {
1684     checkIncomplete(h);
1685     (!fFirst ? f : g).completeExceptionally(ex);
1686     }
1687 jsr166 1.35
1688 jsr166 1.72 checkCompletedWithWrappedException(h, ex);
1689 jsr166 1.62 r.assertNotInvoked();
1690 jsr166 1.53 checkCompletedNormally(fFirst ? f : g, v1);
1691 jsr166 1.72 checkCompletedExceptionally(!fFirst ? f : g, ex);
1692 jsr166 1.47 }}
1693 dl 1.5
1694     /**
1695     * thenAcceptBoth result completes exceptionally if either source cancelled
1696     */
1697 jsr166 1.53 public void testThenAcceptBoth_sourceCancelled() {
1698 jsr166 1.35 for (ExecutionMode m : ExecutionMode.values())
1699     for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1700 jsr166 1.53 for (boolean createIncomplete : new boolean[] { true, false })
1701     for (boolean fFirst : new boolean[] { true, false })
1702 jsr166 1.47 for (Integer v1 : new Integer[] { 1, null })
1703     {
1704 jsr166 1.35 final CompletableFuture<Integer> f = new CompletableFuture<>();
1705     final CompletableFuture<Integer> g = new CompletableFuture<>();
1706 jsr166 1.56 final SubtractAction r = new SubtractAction(m);
1707 jsr166 1.35
1708 jsr166 1.53 (fFirst ? f : g).complete(v1);
1709     if (!createIncomplete)
1710     assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1711 jsr166 1.35 final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1712 jsr166 1.53 if (createIncomplete) {
1713     checkIncomplete(h);
1714     assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1715     }
1716 jsr166 1.22
1717     checkCompletedWithWrappedCancellationException(h);
1718 jsr166 1.53 checkCancelled(!fFirst ? f : g);
1719 jsr166 1.62 r.assertNotInvoked();
1720 jsr166 1.53 checkCompletedNormally(fFirst ? f : g, v1);
1721 jsr166 1.47 }}
1722 jsr166 1.34
1723     /**
1724 jsr166 1.61 * thenAcceptBoth result completes exceptionally if action does
1725     */
1726     public void testThenAcceptBoth_actionFailed() {
1727     for (ExecutionMode m : ExecutionMode.values())
1728     for (boolean fFirst : new boolean[] { true, false })
1729     for (Integer v1 : new Integer[] { 1, null })
1730     for (Integer v2 : new Integer[] { 2, null })
1731     {
1732     final CompletableFuture<Integer> f = new CompletableFuture<>();
1733     final CompletableFuture<Integer> g = new CompletableFuture<>();
1734     final FailingBiConsumer r = new FailingBiConsumer(m);
1735     final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1736    
1737     if (fFirst) {
1738     f.complete(v1);
1739     g.complete(v2);
1740     } else {
1741     g.complete(v2);
1742     f.complete(v1);
1743     }
1744    
1745     checkCompletedWithWrappedCFException(h);
1746     checkCompletedNormally(f, v1);
1747     checkCompletedNormally(g, v2);
1748     }}
1749    
1750     /**
1751 dl 1.5 * runAfterBoth result completes normally after normal
1752     * completion of sources
1753     */
1754 jsr166 1.54 public void testRunAfterBoth_normalCompletion() {
1755 jsr166 1.34 for (ExecutionMode m : ExecutionMode.values())
1756 jsr166 1.54 for (boolean createIncomplete : new boolean[] { true, false })
1757     for (boolean fFirst : new boolean[] { true, false })
1758 jsr166 1.33 for (Integer v1 : new Integer[] { 1, null })
1759 jsr166 1.47 for (Integer v2 : new Integer[] { 2, null })
1760     {
1761 jsr166 1.33 final CompletableFuture<Integer> f = new CompletableFuture<>();
1762     final CompletableFuture<Integer> g = new CompletableFuture<>();
1763 jsr166 1.55 final Noop r = new Noop(m);
1764 jsr166 1.33
1765 jsr166 1.54 if (fFirst) f.complete(v1); else g.complete(v2);
1766     if (!createIncomplete)
1767     if (!fFirst) f.complete(v1); else g.complete(v2);
1768 jsr166 1.34 final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1769 jsr166 1.54 if (createIncomplete) {
1770     checkIncomplete(h);
1771 jsr166 1.62 r.assertNotInvoked();
1772 jsr166 1.54 if (!fFirst) f.complete(v1); else g.complete(v2);
1773     }
1774 dl 1.5
1775 jsr166 1.22 checkCompletedNormally(h, null);
1776 jsr166 1.62 r.assertInvoked();
1777 jsr166 1.33 checkCompletedNormally(f, v1);
1778     checkCompletedNormally(g, v2);
1779 jsr166 1.47 }}
1780 dl 1.5
1781     /**
1782     * runAfterBoth result completes exceptionally after exceptional
1783     * completion of either source
1784     */
1785 jsr166 1.54 public void testRunAfterBoth_exceptionalCompletion() {
1786 jsr166 1.34 for (ExecutionMode m : ExecutionMode.values())
1787 jsr166 1.54 for (boolean createIncomplete : new boolean[] { true, false })
1788     for (boolean fFirst : new boolean[] { true, false })
1789 jsr166 1.47 for (Integer v1 : new Integer[] { 1, null })
1790     {
1791 jsr166 1.33 final CompletableFuture<Integer> f = new CompletableFuture<>();
1792     final CompletableFuture<Integer> g = new CompletableFuture<>();
1793     final CFException ex = new CFException();
1794 jsr166 1.55 final Noop r = new Noop(m);
1795 jsr166 1.33
1796 jsr166 1.54 (fFirst ? f : g).complete(v1);
1797     if (!createIncomplete)
1798     (!fFirst ? f : g).completeExceptionally(ex);
1799 jsr166 1.34 final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1800 jsr166 1.54 if (createIncomplete) {
1801     checkIncomplete(h);
1802     (!fFirst ? f : g).completeExceptionally(ex);
1803     }
1804 dl 1.5
1805 jsr166 1.72 checkCompletedWithWrappedException(h, ex);
1806 jsr166 1.62 r.assertNotInvoked();
1807 jsr166 1.54 checkCompletedNormally(fFirst ? f : g, v1);
1808 jsr166 1.72 checkCompletedExceptionally(!fFirst ? f : g, ex);
1809 jsr166 1.47 }}
1810 dl 1.5
1811 jsr166 1.4 /**
1812 dl 1.5 * runAfterBoth result completes exceptionally if either source cancelled
1813 jsr166 1.4 */
1814 jsr166 1.54 public void testRunAfterBoth_sourceCancelled() {
1815 jsr166 1.34 for (ExecutionMode m : ExecutionMode.values())
1816 jsr166 1.33 for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1817 jsr166 1.54 for (boolean createIncomplete : new boolean[] { true, false })
1818     for (boolean fFirst : new boolean[] { true, false })
1819 jsr166 1.47 for (Integer v1 : new Integer[] { 1, null })
1820     {
1821 jsr166 1.33 final CompletableFuture<Integer> f = new CompletableFuture<>();
1822     final CompletableFuture<Integer> g = new CompletableFuture<>();
1823 jsr166 1.55 final Noop r = new Noop(m);
1824 jsr166 1.33
1825 jsr166 1.54 (fFirst ? f : g).complete(v1);
1826     if (!createIncomplete)
1827     assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1828 jsr166 1.34 final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1829 jsr166 1.54 if (createIncomplete) {
1830     checkIncomplete(h);
1831     assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1832     }
1833 jsr166 1.33
1834     checkCompletedWithWrappedCancellationException(h);
1835 jsr166 1.54 checkCancelled(!fFirst ? f : g);
1836 jsr166 1.62 r.assertNotInvoked();
1837 jsr166 1.54 checkCompletedNormally(fFirst ? f : g, v1);
1838 jsr166 1.47 }}
1839 dl 1.5
1840     /**
1841 jsr166 1.61 * runAfterBoth result completes exceptionally if action does
1842     */
1843     public void testRunAfterBoth_actionFailed() {
1844     for (ExecutionMode m : ExecutionMode.values())
1845     for (boolean fFirst : new boolean[] { true, false })
1846     for (Integer v1 : new Integer[] { 1, null })
1847     for (Integer v2 : new Integer[] { 2, null })
1848     {
1849     final CompletableFuture<Integer> f = new CompletableFuture<>();
1850     final CompletableFuture<Integer> g = new CompletableFuture<>();
1851 jsr166 1.62 final FailingRunnable r1 = new FailingRunnable(m);
1852     final FailingRunnable r2 = new FailingRunnable(m);
1853 jsr166 1.61
1854 jsr166 1.62 CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1855 jsr166 1.61 if (fFirst) {
1856     f.complete(v1);
1857     g.complete(v2);
1858     } else {
1859     g.complete(v2);
1860     f.complete(v1);
1861     }
1862 jsr166 1.62 CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1863 jsr166 1.61
1864     checkCompletedWithWrappedCFException(h1);
1865     checkCompletedWithWrappedCFException(h2);
1866     checkCompletedNormally(f, v1);
1867     checkCompletedNormally(g, v2);
1868     }}
1869    
1870     /**
1871 dl 1.5 * applyToEither result completes normally after normal completion
1872     * of either source
1873     */
1874 jsr166 1.54 public void testApplyToEither_normalCompletion() {
1875 jsr166 1.39 for (ExecutionMode m : ExecutionMode.values())
1876     for (Integer v1 : new Integer[] { 1, null })
1877 jsr166 1.47 for (Integer v2 : new Integer[] { 2, null })
1878     {
1879 jsr166 1.39 final CompletableFuture<Integer> f = new CompletableFuture<>();
1880     final CompletableFuture<Integer> g = new CompletableFuture<>();
1881 jsr166 1.62 final IncFunction[] rs = new IncFunction[6];
1882     for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1883 jsr166 1.54
1884 jsr166 1.62 final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1885     final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1886     checkIncomplete(h0);
1887     checkIncomplete(h1);
1888     rs[0].assertNotInvoked();
1889     rs[1].assertNotInvoked();
1890     f.complete(v1);
1891     checkCompletedNormally(h0, inc(v1));
1892     checkCompletedNormally(h1, inc(v1));
1893     final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1894     final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1895     checkCompletedNormally(h2, inc(v1));
1896     checkCompletedNormally(h3, inc(v1));
1897     g.complete(v2);
1898 jsr166 1.39
1899 jsr166 1.62 // unspecified behavior - both source completions available
1900     final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
1901     final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
1902     rs[4].assertValue(h4.join());
1903     rs[5].assertValue(h5.join());
1904     assertTrue(Objects.equals(inc(v1), h4.join()) ||
1905     Objects.equals(inc(v2), h4.join()));
1906     assertTrue(Objects.equals(inc(v1), h5.join()) ||
1907     Objects.equals(inc(v2), h5.join()));
1908 jsr166 1.39
1909     checkCompletedNormally(f, v1);
1910     checkCompletedNormally(g, v2);
1911 jsr166 1.62 checkCompletedNormally(h0, inc(v1));
1912     checkCompletedNormally(h1, inc(v1));
1913     checkCompletedNormally(h2, inc(v1));
1914     checkCompletedNormally(h3, inc(v1));
1915     for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1));
1916 jsr166 1.47 }}
1917 dl 1.5
1918     /**
1919     * applyToEither result completes exceptionally after exceptional
1920     * completion of either source
1921     */
1922 jsr166 1.62 public void testApplyToEither_exceptionalCompletion() {
1923 jsr166 1.39 for (ExecutionMode m : ExecutionMode.values())
1924 jsr166 1.47 for (Integer v1 : new Integer[] { 1, null })
1925     {
1926 jsr166 1.39 final CompletableFuture<Integer> f = new CompletableFuture<>();
1927     final CompletableFuture<Integer> g = new CompletableFuture<>();
1928 jsr166 1.54 final CFException ex = new CFException();
1929 jsr166 1.62 final IncFunction[] rs = new IncFunction[6];
1930     for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1931    
1932     final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1933     final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1934     checkIncomplete(h0);
1935     checkIncomplete(h1);
1936     rs[0].assertNotInvoked();
1937     rs[1].assertNotInvoked();
1938     f.completeExceptionally(ex);
1939 jsr166 1.72 checkCompletedWithWrappedException(h0, ex);
1940     checkCompletedWithWrappedException(h1, ex);
1941 jsr166 1.62 final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1942     final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1943 jsr166 1.72 checkCompletedWithWrappedException(h2, ex);
1944     checkCompletedWithWrappedException(h3, ex);
1945 jsr166 1.62 g.complete(v1);
1946 jsr166 1.54
1947 jsr166 1.62 // unspecified behavior - both source completions available
1948     final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
1949     final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
1950     try {
1951     assertEquals(inc(v1), h4.join());
1952 jsr166 1.66 rs[4].assertValue(inc(v1));
1953 jsr166 1.62 } catch (CompletionException ok) {
1954 jsr166 1.72 checkCompletedWithWrappedException(h4, ex);
1955 jsr166 1.62 rs[4].assertNotInvoked();
1956     }
1957     try {
1958     assertEquals(inc(v1), h5.join());
1959 jsr166 1.66 rs[5].assertValue(inc(v1));
1960 jsr166 1.62 } catch (CompletionException ok) {
1961 jsr166 1.72 checkCompletedWithWrappedException(h5, ex);
1962 jsr166 1.62 rs[5].assertNotInvoked();
1963 jsr166 1.54 }
1964 jsr166 1.39
1965 jsr166 1.72 checkCompletedExceptionally(f, ex);
1966 jsr166 1.62 checkCompletedNormally(g, v1);
1967 jsr166 1.72 checkCompletedWithWrappedException(h0, ex);
1968     checkCompletedWithWrappedException(h1, ex);
1969     checkCompletedWithWrappedException(h2, ex);
1970     checkCompletedWithWrappedException(h3, ex);
1971     checkCompletedWithWrappedException(h4, ex);
1972 jsr166 1.62 for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
1973 jsr166 1.47 }}
1974 jsr166 1.39
1975 jsr166 1.66 public void testApplyToEither_exceptionalCompletion2() {
1976     for (ExecutionMode m : ExecutionMode.values())
1977     for (boolean fFirst : new boolean[] { true, false })
1978     for (Integer v1 : new Integer[] { 1, null })
1979     {
1980     final CompletableFuture<Integer> f = new CompletableFuture<>();
1981     final CompletableFuture<Integer> g = new CompletableFuture<>();
1982     final CFException ex = new CFException();
1983     final IncFunction[] rs = new IncFunction[6];
1984     for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1985    
1986     final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1987     final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1988     if (fFirst) {
1989     f.complete(v1);
1990     g.completeExceptionally(ex);
1991     } else {
1992     g.completeExceptionally(ex);
1993     f.complete(v1);
1994     }
1995     final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1996     final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1997    
1998     // unspecified behavior - both source completions available
1999     try {
2000     assertEquals(inc(v1), h0.join());
2001     rs[0].assertValue(inc(v1));
2002     } catch (CompletionException ok) {
2003 jsr166 1.72 checkCompletedWithWrappedException(h0, ex);
2004 jsr166 1.66 rs[0].assertNotInvoked();
2005     }
2006     try {
2007     assertEquals(inc(v1), h1.join());
2008     rs[1].assertValue(inc(v1));
2009     } catch (CompletionException ok) {
2010 jsr166 1.72 checkCompletedWithWrappedException(h1, ex);
2011 jsr166 1.66 rs[1].assertNotInvoked();
2012     }
2013     try {
2014     assertEquals(inc(v1), h2.join());
2015     rs[2].assertValue(inc(v1));
2016     } catch (CompletionException ok) {
2017 jsr166 1.72 checkCompletedWithWrappedException(h2, ex);
2018 jsr166 1.66 rs[2].assertNotInvoked();
2019     }
2020     try {
2021     assertEquals(inc(v1), h3.join());
2022     rs[3].assertValue(inc(v1));
2023     } catch (CompletionException ok) {
2024 jsr166 1.72 checkCompletedWithWrappedException(h3, ex);
2025 jsr166 1.66 rs[3].assertNotInvoked();
2026     }
2027    
2028     checkCompletedNormally(f, v1);
2029 jsr166 1.72 checkCompletedExceptionally(g, ex);
2030 jsr166 1.66 }}
2031    
2032 jsr166 1.62 /**
2033     * applyToEither result completes exceptionally if either source cancelled
2034     */
2035     public void testApplyToEither_sourceCancelled() {
2036 jsr166 1.39 for (ExecutionMode m : ExecutionMode.values())
2037 jsr166 1.62 for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2038 jsr166 1.47 for (Integer v1 : new Integer[] { 1, null })
2039     {
2040 jsr166 1.39 final CompletableFuture<Integer> f = new CompletableFuture<>();
2041     final CompletableFuture<Integer> g = new CompletableFuture<>();
2042 jsr166 1.62 final IncFunction[] rs = new IncFunction[6];
2043     for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2044    
2045     final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2046     final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2047     checkIncomplete(h0);
2048     checkIncomplete(h1);
2049     rs[0].assertNotInvoked();
2050     rs[1].assertNotInvoked();
2051     f.cancel(mayInterruptIfRunning);
2052     checkCompletedWithWrappedCancellationException(h0);
2053     checkCompletedWithWrappedCancellationException(h1);
2054     final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2055     final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2056     checkCompletedWithWrappedCancellationException(h2);
2057     checkCompletedWithWrappedCancellationException(h3);
2058     g.complete(v1);
2059 jsr166 1.39
2060 jsr166 1.62 // unspecified behavior - both source completions available
2061     final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2062     final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2063 jsr166 1.39 try {
2064 jsr166 1.62 assertEquals(inc(v1), h4.join());
2065 jsr166 1.66 rs[4].assertValue(inc(v1));
2066 jsr166 1.39 } catch (CompletionException ok) {
2067 jsr166 1.62 checkCompletedWithWrappedCancellationException(h4);
2068     rs[4].assertNotInvoked();
2069 jsr166 1.39 }
2070     try {
2071 jsr166 1.62 assertEquals(inc(v1), h5.join());
2072 jsr166 1.66 rs[5].assertValue(inc(v1));
2073 jsr166 1.39 } catch (CompletionException ok) {
2074 jsr166 1.62 checkCompletedWithWrappedCancellationException(h5);
2075     rs[5].assertNotInvoked();
2076 jsr166 1.39 }
2077 dl 1.5
2078 jsr166 1.62 checkCancelled(f);
2079     checkCompletedNormally(g, v1);
2080     checkCompletedWithWrappedCancellationException(h0);
2081     checkCompletedWithWrappedCancellationException(h1);
2082     checkCompletedWithWrappedCancellationException(h2);
2083     checkCompletedWithWrappedCancellationException(h3);
2084     for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2085 jsr166 1.47 }}
2086 dl 1.5
2087 jsr166 1.67 public void testApplyToEither_sourceCancelled2() {
2088     for (ExecutionMode m : ExecutionMode.values())
2089     for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2090     for (boolean fFirst : new boolean[] { true, false })
2091     for (Integer v1 : new Integer[] { 1, null })
2092     {
2093     final CompletableFuture<Integer> f = new CompletableFuture<>();
2094     final CompletableFuture<Integer> g = new CompletableFuture<>();
2095     final IncFunction[] rs = new IncFunction[6];
2096     for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2097    
2098     final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2099     final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2100     if (fFirst) {
2101     f.complete(v1);
2102     g.cancel(mayInterruptIfRunning);
2103     } else {
2104     g.cancel(mayInterruptIfRunning);
2105     f.complete(v1);
2106     }
2107     final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2108     final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2109    
2110     // unspecified behavior - both source completions available
2111     try {
2112     assertEquals(inc(v1), h0.join());
2113     rs[0].assertValue(inc(v1));
2114     } catch (CompletionException ok) {
2115     checkCompletedWithWrappedCancellationException(h0);
2116     rs[0].assertNotInvoked();
2117     }
2118     try {
2119     assertEquals(inc(v1), h1.join());
2120     rs[1].assertValue(inc(v1));
2121     } catch (CompletionException ok) {
2122     checkCompletedWithWrappedCancellationException(h1);
2123     rs[1].assertNotInvoked();
2124     }
2125     try {
2126     assertEquals(inc(v1), h2.join());
2127     rs[2].assertValue(inc(v1));
2128     } catch (CompletionException ok) {
2129     checkCompletedWithWrappedCancellationException(h2);
2130     rs[2].assertNotInvoked();
2131     }
2132     try {
2133     assertEquals(inc(v1), h3.join());
2134     rs[3].assertValue(inc(v1));
2135     } catch (CompletionException ok) {
2136     checkCompletedWithWrappedCancellationException(h3);
2137     rs[3].assertNotInvoked();
2138     }
2139    
2140     checkCompletedNormally(f, v1);
2141     checkCancelled(g);
2142     }}
2143    
2144 dl 1.5 /**
2145     * applyToEither result completes exceptionally if action does
2146     */
2147 jsr166 1.62 public void testApplyToEither_actionFailed() {
2148 jsr166 1.39 for (ExecutionMode m : ExecutionMode.values())
2149     for (Integer v1 : new Integer[] { 1, null })
2150 jsr166 1.47 for (Integer v2 : new Integer[] { 2, null })
2151     {
2152 jsr166 1.39 final CompletableFuture<Integer> f = new CompletableFuture<>();
2153     final CompletableFuture<Integer> g = new CompletableFuture<>();
2154 jsr166 1.62 final FailingFunction[] rs = new FailingFunction[6];
2155     for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
2156 jsr166 1.39
2157 jsr166 1.62 final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2158     final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2159 jsr166 1.39 f.complete(v1);
2160 jsr166 1.62 final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2161     final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2162     checkCompletedWithWrappedCFException(h0);
2163     checkCompletedWithWrappedCFException(h1);
2164     checkCompletedWithWrappedCFException(h2);
2165     checkCompletedWithWrappedCFException(h3);
2166 jsr166 1.63 for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2167    
2168     g.complete(v2);
2169    
2170     // unspecified behavior - both source completions available
2171     final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2172     final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2173    
2174 jsr166 1.62 checkCompletedWithWrappedCFException(h4);
2175 jsr166 1.63 assertTrue(Objects.equals(v1, rs[4].value) ||
2176     Objects.equals(v2, rs[4].value));
2177 jsr166 1.62 checkCompletedWithWrappedCFException(h5);
2178 jsr166 1.63 assertTrue(Objects.equals(v1, rs[5].value) ||
2179     Objects.equals(v2, rs[5].value));
2180    
2181     checkCompletedNormally(f, v1);
2182     checkCompletedNormally(g, v2);
2183 jsr166 1.47 }}
2184 dl 1.5
2185     /**
2186     * acceptEither result completes normally after normal completion
2187     * of either source
2188     */
2189 jsr166 1.63 public void testAcceptEither_normalCompletion() {
2190 jsr166 1.40 for (ExecutionMode m : ExecutionMode.values())
2191     for (Integer v1 : new Integer[] { 1, null })
2192 jsr166 1.47 for (Integer v2 : new Integer[] { 2, null })
2193     {
2194 jsr166 1.40 final CompletableFuture<Integer> f = new CompletableFuture<>();
2195     final CompletableFuture<Integer> g = new CompletableFuture<>();
2196 jsr166 1.64 final NoopConsumer[] rs = new NoopConsumer[6];
2197     for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2198 jsr166 1.40
2199 jsr166 1.63 final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2200     final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2201     checkIncomplete(h0);
2202     checkIncomplete(h1);
2203     rs[0].assertNotInvoked();
2204     rs[1].assertNotInvoked();
2205 jsr166 1.40 f.complete(v1);
2206 jsr166 1.63 checkCompletedNormally(h0, null);
2207     checkCompletedNormally(h1, null);
2208 jsr166 1.64 rs[0].assertValue(v1);
2209     rs[1].assertValue(v1);
2210 jsr166 1.63 final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2211     final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2212     checkCompletedNormally(h2, null);
2213     checkCompletedNormally(h3, null);
2214 jsr166 1.64 rs[2].assertValue(v1);
2215     rs[3].assertValue(v1);
2216 jsr166 1.40 g.complete(v2);
2217    
2218 jsr166 1.63 // unspecified behavior - both source completions available
2219     final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2220     final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2221     checkCompletedNormally(h4, null);
2222     checkCompletedNormally(h5, null);
2223 jsr166 1.64 assertTrue(Objects.equals(v1, rs[4].value) ||
2224     Objects.equals(v2, rs[4].value));
2225     assertTrue(Objects.equals(v1, rs[5].value) ||
2226     Objects.equals(v2, rs[5].value));
2227 jsr166 1.40
2228     checkCompletedNormally(f, v1);
2229     checkCompletedNormally(g, v2);
2230 jsr166 1.63 checkCompletedNormally(h0, null);
2231     checkCompletedNormally(h1, null);
2232     checkCompletedNormally(h2, null);
2233     checkCompletedNormally(h3, null);
2234 jsr166 1.64 for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2235 jsr166 1.47 }}
2236 dl 1.5
2237     /**
2238     * acceptEither result completes exceptionally after exceptional
2239     * completion of either source
2240     */
2241 jsr166 1.63 public void testAcceptEither_exceptionalCompletion() {
2242 jsr166 1.40 for (ExecutionMode m : ExecutionMode.values())
2243 jsr166 1.47 for (Integer v1 : new Integer[] { 1, null })
2244     {
2245 jsr166 1.40 final CompletableFuture<Integer> f = new CompletableFuture<>();
2246     final CompletableFuture<Integer> g = new CompletableFuture<>();
2247     final CFException ex = new CFException();
2248 jsr166 1.64 final NoopConsumer[] rs = new NoopConsumer[6];
2249     for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2250 jsr166 1.40
2251 jsr166 1.63 final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2252     final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2253     checkIncomplete(h0);
2254     checkIncomplete(h1);
2255     rs[0].assertNotInvoked();
2256     rs[1].assertNotInvoked();
2257 jsr166 1.40 f.completeExceptionally(ex);
2258 jsr166 1.72 checkCompletedWithWrappedException(h0, ex);
2259     checkCompletedWithWrappedException(h1, ex);
2260 jsr166 1.63 final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2261     final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2262 jsr166 1.72 checkCompletedWithWrappedException(h2, ex);
2263     checkCompletedWithWrappedException(h3, ex);
2264 jsr166 1.63
2265 jsr166 1.40 g.complete(v1);
2266    
2267 jsr166 1.63 // unspecified behavior - both source completions available
2268     final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2269     final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2270 jsr166 1.40 try {
2271 jsr166 1.63 assertNull(h4.join());
2272 jsr166 1.64 rs[4].assertValue(v1);
2273 jsr166 1.40 } catch (CompletionException ok) {
2274 jsr166 1.72 checkCompletedWithWrappedException(h4, ex);
2275 jsr166 1.63 rs[4].assertNotInvoked();
2276 jsr166 1.40 }
2277     try {
2278 jsr166 1.63 assertNull(h5.join());
2279 jsr166 1.64 rs[5].assertValue(v1);
2280 jsr166 1.40 } catch (CompletionException ok) {
2281 jsr166 1.72 checkCompletedWithWrappedException(h5, ex);
2282 jsr166 1.63 rs[5].assertNotInvoked();
2283 jsr166 1.40 }
2284 dl 1.5
2285 jsr166 1.72 checkCompletedExceptionally(f, ex);
2286 jsr166 1.40 checkCompletedNormally(g, v1);
2287 jsr166 1.72 checkCompletedWithWrappedException(h0, ex);
2288     checkCompletedWithWrappedException(h1, ex);
2289     checkCompletedWithWrappedException(h2, ex);
2290     checkCompletedWithWrappedException(h3, ex);
2291     checkCompletedWithWrappedException(h4, ex);
2292 jsr166 1.63 for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2293 jsr166 1.47 }}
2294 dl 1.5
2295 jsr166 1.68 public void testAcceptEither_exceptionalCompletion2() {
2296     for (ExecutionMode m : ExecutionMode.values())
2297     for (boolean fFirst : new boolean[] { true, false })
2298     for (Integer v1 : new Integer[] { 1, null })
2299     {
2300     final CompletableFuture<Integer> f = new CompletableFuture<>();
2301     final CompletableFuture<Integer> g = new CompletableFuture<>();
2302     final CFException ex = new CFException();
2303     final NoopConsumer[] rs = new NoopConsumer[6];
2304     for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2305    
2306     final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2307     final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2308     if (fFirst) {
2309     f.complete(v1);
2310     g.completeExceptionally(ex);
2311     } else {
2312     g.completeExceptionally(ex);
2313     f.complete(v1);
2314     }
2315     final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2316     final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2317    
2318     // unspecified behavior - both source completions available
2319     try {
2320     assertEquals(null, h0.join());
2321     rs[0].assertValue(v1);
2322     } catch (CompletionException ok) {
2323 jsr166 1.72 checkCompletedWithWrappedException(h0, ex);
2324 jsr166 1.68 rs[0].assertNotInvoked();
2325     }
2326     try {
2327     assertEquals(null, h1.join());
2328     rs[1].assertValue(v1);
2329     } catch (CompletionException ok) {
2330 jsr166 1.72 checkCompletedWithWrappedException(h1, ex);
2331 jsr166 1.68 rs[1].assertNotInvoked();
2332     }
2333     try {
2334     assertEquals(null, h2.join());
2335     rs[2].assertValue(v1);
2336     } catch (CompletionException ok) {
2337 jsr166 1.72 checkCompletedWithWrappedException(h2, ex);
2338 jsr166 1.68 rs[2].assertNotInvoked();
2339     }
2340     try {
2341     assertEquals(null, h3.join());
2342     rs[3].assertValue(v1);
2343     } catch (CompletionException ok) {
2344 jsr166 1.72 checkCompletedWithWrappedException(h3, ex);
2345 jsr166 1.68 rs[3].assertNotInvoked();
2346     }
2347    
2348     checkCompletedNormally(f, v1);
2349 jsr166 1.72 checkCompletedExceptionally(g, ex);
2350 jsr166 1.68 }}
2351    
2352 dl 1.5 /**
2353     * acceptEither result completes exceptionally if either source cancelled
2354     */
2355 jsr166 1.63 public void testAcceptEither_sourceCancelled() {
2356 jsr166 1.40 for (ExecutionMode m : ExecutionMode.values())
2357     for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2358 jsr166 1.47 for (Integer v1 : new Integer[] { 1, null })
2359     {
2360 jsr166 1.40 final CompletableFuture<Integer> f = new CompletableFuture<>();
2361     final CompletableFuture<Integer> g = new CompletableFuture<>();
2362 jsr166 1.64 final NoopConsumer[] rs = new NoopConsumer[6];
2363     for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2364 jsr166 1.63
2365     final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2366     final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2367     checkIncomplete(h0);
2368     checkIncomplete(h1);
2369     rs[0].assertNotInvoked();
2370     rs[1].assertNotInvoked();
2371     f.cancel(mayInterruptIfRunning);
2372     checkCompletedWithWrappedCancellationException(h0);
2373     checkCompletedWithWrappedCancellationException(h1);
2374     final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2375     final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2376     checkCompletedWithWrappedCancellationException(h2);
2377     checkCompletedWithWrappedCancellationException(h3);
2378 jsr166 1.40
2379     g.complete(v1);
2380    
2381 jsr166 1.63 // unspecified behavior - both source completions available
2382     final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2383     final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2384     try {
2385     assertNull(h4.join());
2386 jsr166 1.64 rs[4].assertValue(v1);
2387 jsr166 1.63 } catch (CompletionException ok) {
2388     checkCompletedWithWrappedCancellationException(h4);
2389     rs[4].assertNotInvoked();
2390     }
2391     try {
2392     assertNull(h5.join());
2393 jsr166 1.64 rs[5].assertValue(v1);
2394 jsr166 1.63 } catch (CompletionException ok) {
2395     checkCompletedWithWrappedCancellationException(h5);
2396     rs[5].assertNotInvoked();
2397     }
2398    
2399 jsr166 1.40 checkCancelled(f);
2400     checkCompletedNormally(g, v1);
2401 jsr166 1.63 checkCompletedWithWrappedCancellationException(h0);
2402     checkCompletedWithWrappedCancellationException(h1);
2403     checkCompletedWithWrappedCancellationException(h2);
2404     checkCompletedWithWrappedCancellationException(h3);
2405     for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2406 jsr166 1.47 }}
2407 jsr166 1.40
2408 jsr166 1.63 /**
2409     * acceptEither result completes exceptionally if action does
2410     */
2411     public void testAcceptEither_actionFailed() {
2412 jsr166 1.40 for (ExecutionMode m : ExecutionMode.values())
2413 jsr166 1.47 for (Integer v1 : new Integer[] { 1, null })
2414 jsr166 1.63 for (Integer v2 : new Integer[] { 2, null })
2415 jsr166 1.47 {
2416 jsr166 1.40 final CompletableFuture<Integer> f = new CompletableFuture<>();
2417     final CompletableFuture<Integer> g = new CompletableFuture<>();
2418 jsr166 1.63 final FailingConsumer[] rs = new FailingConsumer[6];
2419     for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
2420 jsr166 1.40
2421 jsr166 1.63 final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2422     final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2423 jsr166 1.40 f.complete(v1);
2424 jsr166 1.63 final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2425     final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2426     checkCompletedWithWrappedCFException(h0);
2427     checkCompletedWithWrappedCFException(h1);
2428     checkCompletedWithWrappedCFException(h2);
2429     checkCompletedWithWrappedCFException(h3);
2430     for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2431 jsr166 1.40
2432 jsr166 1.63 g.complete(v2);
2433 jsr166 1.40
2434 jsr166 1.63 // unspecified behavior - both source completions available
2435     final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2436     final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2437 jsr166 1.40
2438 jsr166 1.63 checkCompletedWithWrappedCFException(h4);
2439     assertTrue(Objects.equals(v1, rs[4].value) ||
2440     Objects.equals(v2, rs[4].value));
2441     checkCompletedWithWrappedCFException(h5);
2442     assertTrue(Objects.equals(v1, rs[5].value) ||
2443     Objects.equals(v2, rs[5].value));
2444 jsr166 1.40
2445     checkCompletedNormally(f, v1);
2446 jsr166 1.63 checkCompletedNormally(g, v2);
2447 jsr166 1.47 }}
2448 dl 1.5
2449     /**
2450     * runAfterEither result completes normally after normal completion
2451     * of either source
2452     */
2453 jsr166 1.65 public void testRunAfterEither_normalCompletion() {
2454 jsr166 1.41 for (ExecutionMode m : ExecutionMode.values())
2455     for (Integer v1 : new Integer[] { 1, null })
2456 jsr166 1.47 for (Integer v2 : new Integer[] { 2, null })
2457     {
2458 jsr166 1.41 final CompletableFuture<Integer> f = new CompletableFuture<>();
2459     final CompletableFuture<Integer> g = new CompletableFuture<>();
2460 jsr166 1.65 final Noop[] rs = new Noop[6];
2461     for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2462 jsr166 1.41
2463 jsr166 1.65 final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2464     final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2465     checkIncomplete(h0);
2466     checkIncomplete(h1);
2467     rs[0].assertNotInvoked();
2468     rs[1].assertNotInvoked();
2469 jsr166 1.41 f.complete(v1);
2470 jsr166 1.65 checkCompletedNormally(h0, null);
2471     checkCompletedNormally(h1, null);
2472     rs[0].assertInvoked();
2473     rs[1].assertInvoked();
2474     final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2475     final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2476     checkCompletedNormally(h2, null);
2477     checkCompletedNormally(h3, null);
2478     rs[2].assertInvoked();
2479     rs[3].assertInvoked();
2480 jsr166 1.41
2481     g.complete(v2);
2482    
2483 jsr166 1.65 final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2484     final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2485 jsr166 1.47
2486 jsr166 1.41 checkCompletedNormally(f, v1);
2487     checkCompletedNormally(g, v2);
2488 jsr166 1.65 checkCompletedNormally(h0, null);
2489     checkCompletedNormally(h1, null);
2490     checkCompletedNormally(h2, null);
2491     checkCompletedNormally(h3, null);
2492     checkCompletedNormally(h4, null);
2493     checkCompletedNormally(h5, null);
2494     for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2495 jsr166 1.47 }}
2496 dl 1.5
2497     /**
2498     * runAfterEither result completes exceptionally after exceptional
2499     * completion of either source
2500     */
2501 jsr166 1.65 public void testRunAfterEither_exceptionalCompletion() {
2502 jsr166 1.41 for (ExecutionMode m : ExecutionMode.values())
2503 jsr166 1.47 for (Integer v1 : new Integer[] { 1, null })
2504     {
2505 jsr166 1.41 final CompletableFuture<Integer> f = new CompletableFuture<>();
2506     final CompletableFuture<Integer> g = new CompletableFuture<>();
2507     final CFException ex = new CFException();
2508 jsr166 1.65 final Noop[] rs = new Noop[6];
2509     for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2510 jsr166 1.41
2511 jsr166 1.65 final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2512     final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2513     checkIncomplete(h0);
2514     checkIncomplete(h1);
2515     rs[0].assertNotInvoked();
2516     rs[1].assertNotInvoked();
2517 jsr166 1.41 f.completeExceptionally(ex);
2518 jsr166 1.72 checkCompletedWithWrappedException(h0, ex);
2519     checkCompletedWithWrappedException(h1, ex);
2520 jsr166 1.65 final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2521     final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2522 jsr166 1.72 checkCompletedWithWrappedException(h2, ex);
2523     checkCompletedWithWrappedException(h3, ex);
2524 jsr166 1.65
2525 jsr166 1.41 g.complete(v1);
2526    
2527 jsr166 1.65 // unspecified behavior - both source completions available
2528     final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2529     final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2530     try {
2531     assertNull(h4.join());
2532     rs[4].assertInvoked();
2533     } catch (CompletionException ok) {
2534 jsr166 1.72 checkCompletedWithWrappedException(h4, ex);
2535 jsr166 1.65 rs[4].assertNotInvoked();
2536     }
2537     try {
2538     assertNull(h5.join());
2539     rs[5].assertInvoked();
2540     } catch (CompletionException ok) {
2541 jsr166 1.72 checkCompletedWithWrappedException(h5, ex);
2542 jsr166 1.65 rs[5].assertNotInvoked();
2543     }
2544    
2545 jsr166 1.72 checkCompletedExceptionally(f, ex);
2546 jsr166 1.41 checkCompletedNormally(g, v1);
2547 jsr166 1.72 checkCompletedWithWrappedException(h0, ex);
2548     checkCompletedWithWrappedException(h1, ex);
2549     checkCompletedWithWrappedException(h2, ex);
2550     checkCompletedWithWrappedException(h3, ex);
2551     checkCompletedWithWrappedException(h4, ex);
2552 jsr166 1.65 for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2553 jsr166 1.47 }}
2554 jsr166 1.41
2555 jsr166 1.69 public void testRunAfterEither_exceptionalCompletion2() {
2556     for (ExecutionMode m : ExecutionMode.values())
2557     for (boolean fFirst : new boolean[] { true, false })
2558     for (Integer v1 : new Integer[] { 1, null })
2559     {
2560     final CompletableFuture<Integer> f = new CompletableFuture<>();
2561     final CompletableFuture<Integer> g = new CompletableFuture<>();
2562     final CFException ex = new CFException();
2563     final Noop[] rs = new Noop[6];
2564     for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2565    
2566     final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2567     final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2568     if (fFirst) {
2569     f.complete(v1);
2570     g.completeExceptionally(ex);
2571     } else {
2572     g.completeExceptionally(ex);
2573     f.complete(v1);
2574     }
2575     final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2576     final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2577    
2578     // unspecified behavior - both source completions available
2579     try {
2580     assertEquals(null, h0.join());
2581     rs[0].assertInvoked();
2582     } catch (CompletionException ok) {
2583 jsr166 1.72 checkCompletedWithWrappedException(h0, ex);
2584 jsr166 1.69 rs[0].assertNotInvoked();
2585     }
2586     try {
2587     assertEquals(null, h1.join());
2588     rs[1].assertInvoked();
2589     } catch (CompletionException ok) {
2590 jsr166 1.72 checkCompletedWithWrappedException(h1, ex);
2591 jsr166 1.69 rs[1].assertNotInvoked();
2592     }
2593     try {
2594     assertEquals(null, h2.join());
2595     rs[2].assertInvoked();
2596     } catch (CompletionException ok) {
2597 jsr166 1.72 checkCompletedWithWrappedException(h2, ex);
2598 jsr166 1.69 rs[2].assertNotInvoked();
2599     }
2600     try {
2601     assertEquals(null, h3.join());
2602     rs[3].assertInvoked();
2603     } catch (CompletionException ok) {
2604 jsr166 1.72 checkCompletedWithWrappedException(h3, ex);
2605 jsr166 1.69 rs[3].assertNotInvoked();
2606     }
2607    
2608     checkCompletedNormally(f, v1);
2609 jsr166 1.72 checkCompletedExceptionally(g, ex);
2610 jsr166 1.69 }}
2611    
2612 jsr166 1.65 /**
2613     * runAfterEither result completes exceptionally if either source cancelled
2614     */
2615     public void testRunAfterEither_sourceCancelled() {
2616 jsr166 1.41 for (ExecutionMode m : ExecutionMode.values())
2617 jsr166 1.65 for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2618 jsr166 1.47 for (Integer v1 : new Integer[] { 1, null })
2619     {
2620 jsr166 1.41 final CompletableFuture<Integer> f = new CompletableFuture<>();
2621     final CompletableFuture<Integer> g = new CompletableFuture<>();
2622 jsr166 1.65 final Noop[] rs = new Noop[6];
2623     for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2624 jsr166 1.41
2625 jsr166 1.65 final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2626     final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2627     checkIncomplete(h0);
2628     checkIncomplete(h1);
2629     rs[0].assertNotInvoked();
2630     rs[1].assertNotInvoked();
2631     f.cancel(mayInterruptIfRunning);
2632     checkCompletedWithWrappedCancellationException(h0);
2633     checkCompletedWithWrappedCancellationException(h1);
2634     final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2635     final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2636     checkCompletedWithWrappedCancellationException(h2);
2637     checkCompletedWithWrappedCancellationException(h3);
2638 jsr166 1.41
2639 jsr166 1.65 g.complete(v1);
2640 jsr166 1.41
2641 jsr166 1.65 // unspecified behavior - both source completions available
2642     final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2643     final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2644 jsr166 1.41 try {
2645 jsr166 1.65 assertNull(h4.join());
2646     rs[4].assertInvoked();
2647 jsr166 1.41 } catch (CompletionException ok) {
2648 jsr166 1.65 checkCompletedWithWrappedCancellationException(h4);
2649     rs[4].assertNotInvoked();
2650 jsr166 1.41 }
2651     try {
2652 jsr166 1.65 assertNull(h5.join());
2653     rs[5].assertInvoked();
2654 jsr166 1.41 } catch (CompletionException ok) {
2655 jsr166 1.65 checkCompletedWithWrappedCancellationException(h5);
2656     rs[5].assertNotInvoked();
2657 jsr166 1.41 }
2658 dl 1.5
2659 jsr166 1.65 checkCancelled(f);
2660 jsr166 1.41 checkCompletedNormally(g, v1);
2661 jsr166 1.65 checkCompletedWithWrappedCancellationException(h0);
2662     checkCompletedWithWrappedCancellationException(h1);
2663     checkCompletedWithWrappedCancellationException(h2);
2664     checkCompletedWithWrappedCancellationException(h3);
2665     for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2666 jsr166 1.47 }}
2667 dl 1.5
2668     /**
2669     * runAfterEither result completes exceptionally if action does
2670     */
2671 jsr166 1.65 public void testRunAfterEither_actionFailed() {
2672 jsr166 1.41 for (ExecutionMode m : ExecutionMode.values())
2673     for (Integer v1 : new Integer[] { 1, null })
2674 jsr166 1.47 for (Integer v2 : new Integer[] { 2, null })
2675     {
2676 jsr166 1.41 final CompletableFuture<Integer> f = new CompletableFuture<>();
2677     final CompletableFuture<Integer> g = new CompletableFuture<>();
2678 jsr166 1.65 final FailingRunnable[] rs = new FailingRunnable[6];
2679     for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
2680 jsr166 1.41
2681 jsr166 1.65 final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2682     final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2683 jsr166 1.41 f.complete(v1);
2684 jsr166 1.65 final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2685     final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2686     checkCompletedWithWrappedCFException(h0);
2687     checkCompletedWithWrappedCFException(h1);
2688     checkCompletedWithWrappedCFException(h2);
2689     checkCompletedWithWrappedCFException(h3);
2690     for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2691 jsr166 1.41 g.complete(v2);
2692 jsr166 1.65 final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2693     final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2694     checkCompletedWithWrappedCFException(h4);
2695     checkCompletedWithWrappedCFException(h5);
2696 jsr166 1.41
2697     checkCompletedNormally(f, v1);
2698     checkCompletedNormally(g, v2);
2699 jsr166 1.65 for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2700 jsr166 1.47 }}
2701 dl 1.5
2702     /**
2703     * thenCompose result completes normally after normal completion of source
2704     */
2705 jsr166 1.48 public void testThenCompose_normalCompletion() {
2706 jsr166 1.44 for (ExecutionMode m : ExecutionMode.values())
2707 jsr166 1.48 for (boolean createIncomplete : new boolean[] { true, false })
2708 jsr166 1.47 for (Integer v1 : new Integer[] { 1, null })
2709     {
2710 jsr166 1.44 final CompletableFuture<Integer> f = new CompletableFuture<>();
2711 jsr166 1.56 final CompletableFutureInc r = new CompletableFutureInc(m);
2712 jsr166 1.48 if (!createIncomplete) f.complete(v1);
2713 jsr166 1.56 final CompletableFuture<Integer> g = m.thenCompose(f, r);
2714 jsr166 1.48 if (createIncomplete) f.complete(v1);
2715 jsr166 1.21
2716 jsr166 1.44 checkCompletedNormally(g, inc(v1));
2717     checkCompletedNormally(f, v1);
2718 jsr166 1.70 r.assertValue(v1);
2719 jsr166 1.47 }}
2720 dl 1.5
2721     /**
2722     * thenCompose result completes exceptionally after exceptional
2723     * completion of source
2724     */
2725 jsr166 1.48 public void testThenCompose_exceptionalCompletion() {
2726 jsr166 1.47 for (ExecutionMode m : ExecutionMode.values())
2727 jsr166 1.48 for (boolean createIncomplete : new boolean[] { true, false })
2728 jsr166 1.47 {
2729 jsr166 1.44 final CFException ex = new CFException();
2730 jsr166 1.56 final CompletableFutureInc r = new CompletableFutureInc(m);
2731 jsr166 1.44 final CompletableFuture<Integer> f = new CompletableFuture<>();
2732 jsr166 1.48 if (!createIncomplete) f.completeExceptionally(ex);
2733 jsr166 1.56 final CompletableFuture<Integer> g = m.thenCompose(f, r);
2734 jsr166 1.48 if (createIncomplete) f.completeExceptionally(ex);
2735 jsr166 1.21
2736 jsr166 1.72 checkCompletedWithWrappedException(g, ex);
2737     checkCompletedExceptionally(f, ex);
2738 jsr166 1.62 r.assertNotInvoked();
2739 jsr166 1.47 }}
2740 dl 1.5
2741     /**
2742     * thenCompose result completes exceptionally if action does
2743     */
2744 jsr166 1.48 public void testThenCompose_actionFailed() {
2745 jsr166 1.44 for (ExecutionMode m : ExecutionMode.values())
2746 jsr166 1.48 for (boolean createIncomplete : new boolean[] { true, false })
2747 jsr166 1.47 for (Integer v1 : new Integer[] { 1, null })
2748     {
2749 jsr166 1.44 final CompletableFuture<Integer> f = new CompletableFuture<>();
2750     final FailingCompletableFutureFunction r
2751 jsr166 1.56 = new FailingCompletableFutureFunction(m);
2752 jsr166 1.48 if (!createIncomplete) f.complete(v1);
2753 jsr166 1.56 final CompletableFuture<Integer> g = m.thenCompose(f, r);
2754 jsr166 1.48 if (createIncomplete) f.complete(v1);
2755 jsr166 1.44
2756 dl 1.5 checkCompletedWithWrappedCFException(g);
2757 jsr166 1.44 checkCompletedNormally(f, v1);
2758 jsr166 1.47 }}
2759 dl 1.5
2760     /**
2761     * thenCompose result completes exceptionally if source cancelled
2762     */
2763 jsr166 1.48 public void testThenCompose_sourceCancelled() {
2764 jsr166 1.44 for (ExecutionMode m : ExecutionMode.values())
2765 jsr166 1.48 for (boolean createIncomplete : new boolean[] { true, false })
2766 jsr166 1.47 for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2767     {
2768 jsr166 1.44 final CompletableFuture<Integer> f = new CompletableFuture<>();
2769 jsr166 1.56 final CompletableFutureInc r = new CompletableFutureInc(m);
2770 jsr166 1.48 if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2771 jsr166 1.56 final CompletableFuture<Integer> g = m.thenCompose(f, r);
2772 jsr166 1.50 if (createIncomplete) {
2773     checkIncomplete(g);
2774     assertTrue(f.cancel(mayInterruptIfRunning));
2775     }
2776 jsr166 1.44
2777 dl 1.5 checkCompletedWithWrappedCancellationException(g);
2778 jsr166 1.44 checkCancelled(f);
2779 jsr166 1.47 }}
2780 dl 1.5
2781 jsr166 1.6 // other static methods
2782 dl 1.5
2783     /**
2784     * allOf(no component futures) returns a future completed normally
2785     * with the value null
2786     */
2787     public void testAllOf_empty() throws Exception {
2788 jsr166 1.24 CompletableFuture<Void> f = CompletableFuture.allOf();
2789 dl 1.5 checkCompletedNormally(f, null);
2790     }
2791    
2792     /**
2793 jsr166 1.25 * allOf returns a future completed normally with the value null
2794     * when all components complete normally
2795 dl 1.5 */
2796 jsr166 1.25 public void testAllOf_normal() throws Exception {
2797 dl 1.5 for (int k = 1; k < 20; ++k) {
2798 jsr166 1.59 CompletableFuture<Integer>[] fs
2799     = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2800 jsr166 1.6 for (int i = 0; i < k; ++i)
2801 jsr166 1.22 fs[i] = new CompletableFuture<>();
2802 dl 1.9 CompletableFuture<Void> f = CompletableFuture.allOf(fs);
2803 dl 1.5 for (int i = 0; i < k; ++i) {
2804     checkIncomplete(f);
2805 jsr166 1.24 checkIncomplete(CompletableFuture.allOf(fs));
2806 dl 1.5 fs[i].complete(one);
2807     }
2808 dl 1.9 checkCompletedNormally(f, null);
2809 jsr166 1.24 checkCompletedNormally(CompletableFuture.allOf(fs), null);
2810 dl 1.5 }
2811     }
2812    
2813 jsr166 1.59 public void testAllOf_backwards() throws Exception {
2814     for (int k = 1; k < 20; ++k) {
2815     CompletableFuture<Integer>[] fs
2816     = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2817     for (int i = 0; i < k; ++i)
2818     fs[i] = new CompletableFuture<>();
2819     CompletableFuture<Void> f = CompletableFuture.allOf(fs);
2820     for (int i = k - 1; i >= 0; i--) {
2821     checkIncomplete(f);
2822     checkIncomplete(CompletableFuture.allOf(fs));
2823     fs[i].complete(one);
2824     }
2825     checkCompletedNormally(f, null);
2826     checkCompletedNormally(CompletableFuture.allOf(fs), null);
2827     }
2828     }
2829    
2830 dl 1.5 /**
2831     * anyOf(no component futures) returns an incomplete future
2832     */
2833     public void testAnyOf_empty() throws Exception {
2834 jsr166 1.24 CompletableFuture<Object> f = CompletableFuture.anyOf();
2835 dl 1.5 checkIncomplete(f);
2836     }
2837    
2838     /**
2839 jsr166 1.25 * anyOf returns a future completed normally with a value when
2840     * a component future does
2841 dl 1.5 */
2842 jsr166 1.24 public void testAnyOf_normal() throws Exception {
2843     for (int k = 0; k < 10; ++k) {
2844 dl 1.5 CompletableFuture[] fs = new CompletableFuture[k];
2845 jsr166 1.6 for (int i = 0; i < k; ++i)
2846 jsr166 1.22 fs[i] = new CompletableFuture<>();
2847 dl 1.9 CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
2848 dl 1.5 checkIncomplete(f);
2849     for (int i = 0; i < k; ++i) {
2850     fs[i].complete(one);
2851 dl 1.9 checkCompletedNormally(f, one);
2852 jsr166 1.24 checkCompletedNormally(CompletableFuture.anyOf(fs), one);
2853     }
2854     }
2855     }
2856    
2857     /**
2858     * anyOf result completes exceptionally when any component does.
2859     */
2860     public void testAnyOf_exceptional() throws Exception {
2861     for (int k = 0; k < 10; ++k) {
2862     CompletableFuture[] fs = new CompletableFuture[k];
2863     for (int i = 0; i < k; ++i)
2864     fs[i] = new CompletableFuture<>();
2865     CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
2866     checkIncomplete(f);
2867     for (int i = 0; i < k; ++i) {
2868     fs[i].completeExceptionally(new CFException());
2869     checkCompletedWithWrappedCFException(f);
2870     checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
2871 dl 1.5 }
2872     }
2873     }
2874    
2875     /**
2876     * Completion methods throw NullPointerException with null arguments
2877     */
2878     public void testNPE() {
2879 jsr166 1.22 CompletableFuture<Integer> f = new CompletableFuture<>();
2880     CompletableFuture<Integer> g = new CompletableFuture<>();
2881 jsr166 1.14 CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null;
2882     CompletableFuture<?> h;
2883 jsr166 1.17 ThreadExecutor exec = new ThreadExecutor();
2884 jsr166 1.14
2885     Runnable[] throwingActions = {
2886 jsr166 1.31 () -> CompletableFuture.supplyAsync(null),
2887     () -> CompletableFuture.supplyAsync(null, exec),
2888 jsr166 1.58 () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null),
2889 jsr166 1.31
2890     () -> CompletableFuture.runAsync(null),
2891     () -> CompletableFuture.runAsync(null, exec),
2892     () -> CompletableFuture.runAsync(() -> {}, null),
2893    
2894     () -> f.completeExceptionally(null),
2895    
2896     () -> f.thenApply(null),
2897     () -> f.thenApplyAsync(null),
2898     () -> f.thenApplyAsync((x) -> x, null),
2899     () -> f.thenApplyAsync(null, exec),
2900    
2901     () -> f.thenAccept(null),
2902     () -> f.thenAcceptAsync(null),
2903     () -> f.thenAcceptAsync((x) -> {} , null),
2904     () -> f.thenAcceptAsync(null, exec),
2905    
2906     () -> f.thenRun(null),
2907     () -> f.thenRunAsync(null),
2908     () -> f.thenRunAsync(() -> {} , null),
2909     () -> f.thenRunAsync(null, exec),
2910    
2911     () -> f.thenCombine(g, null),
2912     () -> f.thenCombineAsync(g, null),
2913     () -> f.thenCombineAsync(g, null, exec),
2914     () -> f.thenCombine(nullFuture, (x, y) -> x),
2915     () -> f.thenCombineAsync(nullFuture, (x, y) -> x),
2916     () -> f.thenCombineAsync(nullFuture, (x, y) -> x, exec),
2917     () -> f.thenCombineAsync(g, (x, y) -> x, null),
2918    
2919     () -> f.thenAcceptBoth(g, null),
2920     () -> f.thenAcceptBothAsync(g, null),
2921     () -> f.thenAcceptBothAsync(g, null, exec),
2922     () -> f.thenAcceptBoth(nullFuture, (x, y) -> {}),
2923     () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}),
2924     () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec),
2925     () -> f.thenAcceptBothAsync(g, (x, y) -> {}, null),
2926    
2927     () -> f.runAfterBoth(g, null),
2928     () -> f.runAfterBothAsync(g, null),
2929     () -> f.runAfterBothAsync(g, null, exec),
2930     () -> f.runAfterBoth(nullFuture, () -> {}),
2931     () -> f.runAfterBothAsync(nullFuture, () -> {}),
2932     () -> f.runAfterBothAsync(nullFuture, () -> {}, exec),
2933     () -> f.runAfterBothAsync(g, () -> {}, null),
2934    
2935     () -> f.applyToEither(g, null),
2936     () -> f.applyToEitherAsync(g, null),
2937     () -> f.applyToEitherAsync(g, null, exec),
2938     () -> f.applyToEither(nullFuture, (x) -> x),
2939     () -> f.applyToEitherAsync(nullFuture, (x) -> x),
2940     () -> f.applyToEitherAsync(nullFuture, (x) -> x, exec),
2941     () -> f.applyToEitherAsync(g, (x) -> x, null),
2942    
2943     () -> f.acceptEither(g, null),
2944     () -> f.acceptEitherAsync(g, null),
2945     () -> f.acceptEitherAsync(g, null, exec),
2946     () -> f.acceptEither(nullFuture, (x) -> {}),
2947     () -> f.acceptEitherAsync(nullFuture, (x) -> {}),
2948     () -> f.acceptEitherAsync(nullFuture, (x) -> {}, exec),
2949     () -> f.acceptEitherAsync(g, (x) -> {}, null),
2950    
2951     () -> f.runAfterEither(g, null),
2952     () -> f.runAfterEitherAsync(g, null),
2953     () -> f.runAfterEitherAsync(g, null, exec),
2954     () -> f.runAfterEither(nullFuture, () -> {}),
2955     () -> f.runAfterEitherAsync(nullFuture, () -> {}),
2956     () -> f.runAfterEitherAsync(nullFuture, () -> {}, exec),
2957     () -> f.runAfterEitherAsync(g, () -> {}, null),
2958    
2959     () -> f.thenCompose(null),
2960     () -> f.thenComposeAsync(null),
2961 jsr166 1.56 () -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null),
2962 jsr166 1.31 () -> f.thenComposeAsync(null, exec),
2963    
2964     () -> f.exceptionally(null),
2965    
2966     () -> f.handle(null),
2967    
2968     () -> CompletableFuture.allOf((CompletableFuture<?>)null),
2969     () -> CompletableFuture.allOf((CompletableFuture<?>[])null),
2970     () -> CompletableFuture.allOf(f, null),
2971     () -> CompletableFuture.allOf(null, f),
2972    
2973     () -> CompletableFuture.anyOf((CompletableFuture<?>)null),
2974     () -> CompletableFuture.anyOf((CompletableFuture<?>[])null),
2975     () -> CompletableFuture.anyOf(f, null),
2976     () -> CompletableFuture.anyOf(null, f),
2977 jsr166 1.32
2978     () -> f.obtrudeException(null),
2979 jsr166 1.14 };
2980 dl 1.5
2981 jsr166 1.14 assertThrows(NullPointerException.class, throwingActions);
2982 jsr166 1.17 assertEquals(0, exec.count.get());
2983 dl 1.5 }
2984    
2985 dl 1.26 /**
2986     * toCompletableFuture returns this CompletableFuture.
2987     */
2988     public void testToCompletableFuture() {
2989     CompletableFuture<Integer> f = new CompletableFuture<>();
2990     assertSame(f, f.toCompletableFuture());
2991     }
2992    
2993 jsr166 1.1 }