ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
Revision: 1.33
Committed: Sun Jun 1 20:40:13 2014 UTC (9 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.32: +276 -71 lines
Log Message:
testRunAfterBoth should test null values

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