ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
Revision: 1.38
Committed: Sun Jun 1 23:51:44 2014 UTC (9 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.37: +136 -14 lines
Log Message:
improve infrastructure

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