ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
Revision: 1.17
Committed: Thu Apr 4 04:16:02 2013 UTC (11 years, 1 month ago) by jsr166
Branch: MAIN
Changes since 1.16: +14 -4 lines
Log Message:
more assertions

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 dl 1.5 assertEquals(value, f.join());
59 jsr166 1.4 } catch (Throwable fail) { threadUnexpectedException(fail); }
60     try {
61 dl 1.5 assertEquals(value, f.getNow(null));
62 jsr166 1.4 } catch (Throwable fail) { threadUnexpectedException(fail); }
63     try {
64 dl 1.5 assertEquals(value, f.get());
65 jsr166 1.4 } catch (Throwable fail) { threadUnexpectedException(fail); }
66     try {
67 dl 1.5 assertEquals(value, f.get(0L, SECONDS));
68 jsr166 1.4 } catch (Throwable fail) { threadUnexpectedException(fail); }
69 dl 1.5 assertTrue(f.isDone());
70     assertFalse(f.isCancelled());
71     assertTrue(f.toString().contains("[Completed normally]"));
72     }
73    
74     void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
75     try {
76     f.join();
77     shouldThrow();
78 jsr166 1.8 } catch (CompletionException success) {
79     assertTrue(success.getCause() instanceof CFException);
80 dl 1.5 }
81     try {
82     f.getNow(null);
83     shouldThrow();
84 jsr166 1.8 } catch (CompletionException success) {
85     assertTrue(success.getCause() instanceof CFException);
86 dl 1.5 }
87     try {
88     f.get();
89     shouldThrow();
90 jsr166 1.8 } catch (ExecutionException success) {
91     assertTrue(success.getCause() instanceof CFException);
92     } catch (Throwable fail) { threadUnexpectedException(fail); }
93 dl 1.5 try {
94     f.get(0L, SECONDS);
95     shouldThrow();
96 jsr166 1.8 } catch (ExecutionException success) {
97     assertTrue(success.getCause() instanceof CFException);
98     } catch (Throwable fail) { threadUnexpectedException(fail); }
99 dl 1.5 assertTrue(f.isDone());
100     assertFalse(f.isCancelled());
101 jsr166 1.14 assertTrue(f.toString().contains("[Completed exceptionally]"));
102 dl 1.5 }
103    
104     void checkCancelled(CompletableFuture<?> f) {
105     try {
106     f.join();
107     shouldThrow();
108 jsr166 1.8 } catch (CancellationException success) {}
109 dl 1.5 try {
110     f.getNow(null);
111     shouldThrow();
112 jsr166 1.8 } catch (CancellationException success) {}
113 dl 1.5 try {
114     f.get();
115     shouldThrow();
116 jsr166 1.8 } catch (CancellationException success) {
117     } catch (Throwable fail) { threadUnexpectedException(fail); }
118 dl 1.5 try {
119     f.get(0L, SECONDS);
120     shouldThrow();
121 jsr166 1.8 } catch (CancellationException success) {
122     } catch (Throwable fail) { threadUnexpectedException(fail); }
123 dl 1.5 assertTrue(f.isDone());
124     assertTrue(f.isCancelled());
125 jsr166 1.14 assertTrue(f.toString().contains("[Completed exceptionally]"));
126 dl 1.5 }
127    
128     void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
129     try {
130     f.join();
131     shouldThrow();
132 jsr166 1.8 } catch (CompletionException success) {
133     assertTrue(success.getCause() instanceof CancellationException);
134 dl 1.5 }
135     try {
136     f.getNow(null);
137     shouldThrow();
138 jsr166 1.8 } catch (CompletionException success) {
139     assertTrue(success.getCause() instanceof CancellationException);
140 dl 1.5 }
141     try {
142     f.get();
143     shouldThrow();
144 jsr166 1.8 } catch (ExecutionException success) {
145     assertTrue(success.getCause() instanceof CancellationException);
146     } catch (Throwable fail) { threadUnexpectedException(fail); }
147 dl 1.5 try {
148     f.get(0L, SECONDS);
149     shouldThrow();
150 jsr166 1.8 } catch (ExecutionException success) {
151     assertTrue(success.getCause() instanceof CancellationException);
152     } catch (Throwable fail) { threadUnexpectedException(fail); }
153 dl 1.5 assertTrue(f.isDone());
154     assertFalse(f.isCancelled());
155 jsr166 1.14 assertTrue(f.toString().contains("[Completed exceptionally]"));
156 dl 1.5 }
157    
158     /**
159     * A newly constructed CompletableFuture is incomplete, as indicated
160     * by methods isDone, isCancelled, and getNow
161     */
162     public void testConstructor() {
163     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
164     checkIncomplete(f);
165     }
166    
167     /**
168     * complete completes normally, as indicated by methods isDone,
169     * isCancelled, join, get, and getNow
170     */
171     public void testComplete() {
172     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
173     checkIncomplete(f);
174     f.complete(one);
175     checkCompletedNormally(f, one);
176     }
177    
178     /**
179     * completeExceptionally completes exceptionally, as indicated by
180     * methods isDone, isCancelled, join, get, and getNow
181     */
182     public void testCompleteExceptionally() {
183     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
184     checkIncomplete(f);
185     f.completeExceptionally(new CFException());
186     checkCompletedWithWrappedCFException(f);
187     }
188    
189     /**
190     * cancel completes exceptionally and reports cancelled, as indicated by
191     * methods isDone, isCancelled, join, get, and getNow
192     */
193     public void testCancel() {
194     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
195     checkIncomplete(f);
196     assertTrue(f.cancel(true));
197     checkCancelled(f);
198     }
199    
200     /**
201     * obtrudeValue forces completion with given value
202     */
203     public void testObtrudeValue() {
204     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
205     checkIncomplete(f);
206     f.complete(one);
207     checkCompletedNormally(f, one);
208     f.obtrudeValue(three);
209     checkCompletedNormally(f, three);
210     f.obtrudeValue(two);
211     checkCompletedNormally(f, two);
212     f = new CompletableFuture<Integer>();
213     f.obtrudeValue(three);
214     checkCompletedNormally(f, three);
215     f = new CompletableFuture<Integer>();
216     f.completeExceptionally(new CFException());
217     f.obtrudeValue(four);
218     checkCompletedNormally(f, four);
219 jsr166 1.4 }
220    
221 dl 1.5 /**
222     * obtrudeException forces completion with given exception
223     */
224     public void testObtrudeException() {
225     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
226     checkIncomplete(f);
227     f.complete(one);
228     checkCompletedNormally(f, one);
229     f.obtrudeException(new CFException());
230     checkCompletedWithWrappedCFException(f);
231     f = new CompletableFuture<Integer>();
232     f.obtrudeException(new CFException());
233     checkCompletedWithWrappedCFException(f);
234     f = new CompletableFuture<Integer>();
235     f.completeExceptionally(new CFException());
236     f.obtrudeValue(four);
237     checkCompletedNormally(f, four);
238     f.obtrudeException(new CFException());
239     checkCompletedWithWrappedCFException(f);
240     }
241 jsr166 1.6
242 dl 1.5 /**
243     * getNumberOfDependents returns number of dependent tasks
244     */
245     public void testGetNumberOfDependents() {
246     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
247     assertEquals(f.getNumberOfDependents(), 0);
248     CompletableFuture g = f.thenRun(new Noop());
249     assertEquals(f.getNumberOfDependents(), 1);
250     assertEquals(g.getNumberOfDependents(), 0);
251     CompletableFuture h = f.thenRun(new Noop());
252     assertEquals(f.getNumberOfDependents(), 2);
253     f.complete(1);
254     checkCompletedNormally(g, null);
255     assertEquals(f.getNumberOfDependents(), 0);
256     assertEquals(g.getNumberOfDependents(), 0);
257 jsr166 1.3 }
258    
259 dl 1.5
260     /**
261     * toString indicates current completion state
262     */
263 jsr166 1.1 public void testToString() {
264     CompletableFuture<String> f;
265 jsr166 1.2
266 jsr166 1.1 f = new CompletableFuture<String>();
267 jsr166 1.2 assertTrue(f.toString().contains("[Not completed]"));
268    
269 jsr166 1.1 f.complete("foo");
270     assertTrue(f.toString().contains("[Completed normally]"));
271 jsr166 1.2
272 jsr166 1.1 f = new CompletableFuture<String>();
273     f.completeExceptionally(new IndexOutOfBoundsException());
274     assertTrue(f.toString().contains("[Completed exceptionally]"));
275     }
276 jsr166 1.4
277 dl 1.9 /**
278 jsr166 1.10 * completedFuture returns a completed CompletableFuture with given value
279 dl 1.9 */
280     public void testCompletedFuture() {
281     CompletableFuture<String> f = CompletableFuture.completedFuture("test");
282     checkCompletedNormally(f, "test");
283     }
284    
285 jsr166 1.6 static final Supplier<Integer> supplyOne =
286 dl 1.5 () -> Integer.valueOf(1);
287 jsr166 1.6 static final Function<Integer, Integer> inc =
288 dl 1.5 (Integer x) -> Integer.valueOf(x.intValue() + 1);
289 jsr166 1.6 static final BiFunction<Integer, Integer, Integer> add =
290 dl 1.5 (Integer x, Integer y) -> Integer.valueOf(x.intValue() + y.intValue());
291     static final class IncAction implements Consumer<Integer> {
292     int value;
293     public void accept(Integer x) { value = x.intValue() + 1; }
294     }
295     static final class AddAction implements BiConsumer<Integer, Integer> {
296     int value;
297 jsr166 1.6 public void accept(Integer x, Integer y) {
298     value = x.intValue() + y.intValue();
299 dl 1.5 }
300     }
301     static final class Noop implements Runnable {
302     boolean ran;
303     public void run() { ran = true; }
304     }
305    
306     static final class FailingSupplier implements Supplier<Integer> {
307     boolean ran;
308     public Integer get() { ran = true; throw new CFException(); }
309     }
310     static final class FailingConsumer implements Consumer<Integer> {
311     boolean ran;
312     public void accept(Integer x) { ran = true; throw new CFException(); }
313     }
314     static final class FailingBiConsumer implements BiConsumer<Integer, Integer> {
315     boolean ran;
316     public void accept(Integer x, Integer y) { ran = true; throw new CFException(); }
317     }
318     static final class FailingFunction implements Function<Integer, Integer> {
319     boolean ran;
320     public Integer apply(Integer x) { ran = true; throw new CFException(); }
321     }
322     static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> {
323     boolean ran;
324     public Integer apply(Integer x, Integer y) { ran = true; throw new CFException(); }
325     }
326     static final class FailingNoop implements Runnable {
327     boolean ran;
328     public void run() { ran = true; throw new CFException(); }
329     }
330    
331     static final class CompletableFutureInc implements Function<Integer, CompletableFuture<Integer>> {
332     public CompletableFuture<Integer> apply(Integer x) {
333     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
334     f.complete(Integer.valueOf(x.intValue() + 1));
335     return f;
336     }
337     }
338    
339     static final class FailingCompletableFutureFunction implements Function<Integer, CompletableFuture<Integer>> {
340     boolean ran;
341     public CompletableFuture<Integer> apply(Integer x) {
342     ran = true; throw new CFException();
343     }
344     }
345 jsr166 1.6
346 dl 1.5 // Used for explicit executor tests
347     static final class ThreadExecutor implements Executor {
348 jsr166 1.17 AtomicInteger count = new AtomicInteger(0);
349    
350 dl 1.5 public void execute(Runnable r) {
351 jsr166 1.17 count.getAndIncrement();
352 dl 1.5 new Thread(r).start();
353     }
354     }
355    
356     static final class ExceptionToInteger implements Function<Throwable, Integer> {
357     public Integer apply(Throwable x) { return Integer.valueOf(3); }
358     }
359    
360     static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> {
361 jsr166 1.15 boolean ran;
362 jsr166 1.6 public Integer apply(Integer x, Throwable t) {
363 jsr166 1.15 ran = true;
364 dl 1.5 return (t == null) ? two : three;
365     }
366     }
367    
368    
369     /**
370     * exceptionally action completes with function value on source
371     * exception; otherwise with source value
372     */
373     public void testExceptionally() {
374     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
375     ExceptionToInteger r = new ExceptionToInteger();
376     CompletableFuture<Integer> g = f.exceptionally(r);
377     f.completeExceptionally(new CFException());
378     checkCompletedNormally(g, three);
379    
380     f = new CompletableFuture<Integer>();
381     r = new ExceptionToInteger();
382     g = f.exceptionally(r);
383     f.complete(one);
384     checkCompletedNormally(g, one);
385     }
386    
387     /**
388     * handle action completes normally with function value on either
389     * normal or exceptional completion of source
390     */
391     public void testHandle() {
392 jsr166 1.15 CompletableFuture<Integer> f, g;
393     IntegerHandler r;
394    
395     f = new CompletableFuture<Integer>();
396 dl 1.5 f.completeExceptionally(new CFException());
397 jsr166 1.15 g = f.handle(r = new IntegerHandler());
398     assertTrue(r.ran);
399 dl 1.5 checkCompletedNormally(g, three);
400    
401     f = new CompletableFuture<Integer>();
402 jsr166 1.15 g = f.handle(r = new IntegerHandler());
403     assertFalse(r.ran);
404     f.completeExceptionally(new CFException());
405     checkCompletedNormally(g, three);
406     assertTrue(r.ran);
407    
408     f = new CompletableFuture<Integer>();
409 dl 1.5 f.complete(one);
410 jsr166 1.15 g = f.handle(r = new IntegerHandler());
411     assertTrue(r.ran);
412     checkCompletedNormally(g, two);
413    
414     f = new CompletableFuture<Integer>();
415     g = f.handle(r = new IntegerHandler());
416     assertFalse(r.ran);
417     f.complete(one);
418     assertTrue(r.ran);
419 dl 1.5 checkCompletedNormally(g, two);
420     }
421    
422     /**
423     * runAsync completes after running Runnable
424     */
425     public void testRunAsync() {
426     Noop r = new Noop();
427     CompletableFuture<Void> f = CompletableFuture.runAsync(r);
428     assertNull(f.join());
429     assertTrue(r.ran);
430 jsr166 1.14 checkCompletedNormally(f, null);
431 dl 1.5 }
432    
433     /**
434     * runAsync with executor completes after running Runnable
435     */
436     public void testRunAsync2() {
437     Noop r = new Noop();
438 jsr166 1.17 ThreadExecutor exec = new ThreadExecutor();
439     CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec);
440 dl 1.5 assertNull(f.join());
441     assertTrue(r.ran);
442 jsr166 1.14 checkCompletedNormally(f, null);
443 jsr166 1.17 assertEquals(1, exec.count.get());
444 dl 1.5 }
445    
446     /**
447     * failing runAsync completes exceptionally after running Runnable
448     */
449     public void testRunAsync3() {
450     FailingNoop r = new FailingNoop();
451     CompletableFuture<Void> f = CompletableFuture.runAsync(r);
452     checkCompletedWithWrappedCFException(f);
453     assertTrue(r.ran);
454     }
455    
456     /**
457     * supplyAsync completes with result of supplier
458     */
459     public void testSupplyAsync() {
460 jsr166 1.17 CompletableFuture<Integer> f;
461     f = CompletableFuture.supplyAsync(supplyOne);
462 dl 1.5 assertEquals(f.join(), one);
463 jsr166 1.17 checkCompletedNormally(f, one);
464 dl 1.5 }
465    
466     /**
467     * supplyAsync with executor completes with result of supplier
468     */
469     public void testSupplyAsync2() {
470 jsr166 1.17 CompletableFuture<Integer> f;
471     f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor());
472 dl 1.5 assertEquals(f.join(), one);
473 jsr166 1.17 checkCompletedNormally(f, one);
474 dl 1.5 }
475    
476     /**
477     * Failing supplyAsync completes exceptionally
478     */
479     public void testSupplyAsync3() {
480     FailingSupplier r = new FailingSupplier();
481     CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r);
482     checkCompletedWithWrappedCFException(f);
483     assertTrue(r.ran);
484     }
485    
486 jsr166 1.7 // seq completion methods
487 jsr166 1.6
488 dl 1.5 /**
489     * thenRun result completes normally after normal completion of source
490     */
491     public void testThenRun() {
492     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
493     Noop r = new Noop();
494     CompletableFuture<Void> g = f.thenRun(r);
495     f.complete(null);
496     checkCompletedNormally(g, null);
497     // reordered version
498     f = new CompletableFuture<Integer>();
499     f.complete(null);
500     r = new Noop();
501     g = f.thenRun(r);
502     checkCompletedNormally(g, null);
503     }
504    
505     /**
506     * thenRun result completes exceptionally after exceptional
507     * completion of source
508     */
509     public void testThenRun2() {
510     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
511     Noop r = new Noop();
512     CompletableFuture<Void> g = f.thenRun(r);
513     f.completeExceptionally(new CFException());
514     checkCompletedWithWrappedCFException(g);
515     }
516    
517     /**
518     * thenRun result completes exceptionally if action does
519     */
520     public void testThenRun3() {
521     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
522     FailingNoop r = new FailingNoop();
523     CompletableFuture<Void> g = f.thenRun(r);
524     f.complete(null);
525     checkCompletedWithWrappedCFException(g);
526     }
527    
528     /**
529     * thenRun result completes exceptionally if source cancelled
530     */
531     public void testThenRun4() {
532     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
533     Noop r = new Noop();
534     CompletableFuture<Void> g = f.thenRun(r);
535     assertTrue(f.cancel(true));
536     checkCompletedWithWrappedCancellationException(g);
537     }
538    
539     /**
540     * thenApply result completes normally after normal completion of source
541     */
542     public void testThenApply() {
543     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
544     CompletableFuture<Integer> g = f.thenApply(inc);
545     f.complete(one);
546     checkCompletedNormally(g, two);
547     }
548    
549     /**
550     * thenApply result completes exceptionally after exceptional
551     * completion of source
552     */
553     public void testThenApply2() {
554     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
555     CompletableFuture<Integer> g = f.thenApply(inc);
556     f.completeExceptionally(new CFException());
557     checkCompletedWithWrappedCFException(g);
558     }
559    
560     /**
561     * thenApply result completes exceptionally if action does
562     */
563     public void testThenApply3() {
564     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
565     CompletableFuture<Integer> g = f.thenApply(new FailingFunction());
566     f.complete(one);
567     checkCompletedWithWrappedCFException(g);
568     }
569    
570     /**
571     * thenApply result completes exceptionally if source cancelled
572     */
573     public void testThenApply4() {
574     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
575     CompletableFuture<Integer> g = f.thenApply(inc);
576     assertTrue(f.cancel(true));
577     checkCompletedWithWrappedCancellationException(g);
578     }
579    
580     /**
581     * thenAccept result completes normally after normal completion of source
582     */
583     public void testThenAccept() {
584     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
585     IncAction r = new IncAction();
586     CompletableFuture<Void> g = f.thenAccept(r);
587     f.complete(one);
588     checkCompletedNormally(g, null);
589     assertEquals(r.value, 2);
590     }
591    
592     /**
593     * thenAccept result completes exceptionally after exceptional
594     * completion of source
595     */
596     public void testThenAccept2() {
597     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
598     IncAction r = new IncAction();
599     CompletableFuture<Void> g = f.thenAccept(r);
600     f.completeExceptionally(new CFException());
601     checkCompletedWithWrappedCFException(g);
602     }
603    
604     /**
605     * thenAccept result completes exceptionally if action does
606     */
607     public void testThenAccept3() {
608     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
609     FailingConsumer r = new FailingConsumer();
610     CompletableFuture<Void> g = f.thenAccept(r);
611     f.complete(one);
612     checkCompletedWithWrappedCFException(g);
613     assertTrue(r.ran);
614     }
615    
616     /**
617     * thenAccept result completes exceptionally if source cancelled
618     */
619     public void testThenAccept4() {
620     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
621     IncAction r = new IncAction();
622     CompletableFuture<Void> g = f.thenAccept(r);
623     assertTrue(f.cancel(true));
624     checkCompletedWithWrappedCancellationException(g);
625     }
626    
627    
628     /**
629     * thenCombine result completes normally after normal completion of sources
630     */
631     public void testThenCombine() {
632     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
633     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
634     CompletableFuture<Integer> g = f.thenCombine(f2, add);
635     f.complete(one);
636     checkIncomplete(g);
637     f2.complete(two);
638     checkCompletedNormally(g, three);
639    
640     f = new CompletableFuture<Integer>();
641     f.complete(one);
642     f2 = new CompletableFuture<Integer>();
643     g = f.thenCombine(f2, add);
644     checkIncomplete(g);
645     f2.complete(two);
646     checkCompletedNormally(g, three);
647     }
648    
649     /**
650     * thenCombine result completes exceptionally after exceptional
651     * completion of either source
652     */
653     public void testThenCombine2() {
654     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
655     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
656     CompletableFuture<Integer> g = f.thenCombine(f2, add);
657     f.completeExceptionally(new CFException());
658     f2.complete(two);
659     checkCompletedWithWrappedCFException(g);
660    
661     f = new CompletableFuture<Integer>();
662     f.complete(one);
663     f2 = new CompletableFuture<Integer>();
664     g = f.thenCombine(f2, add);
665     f2.completeExceptionally(new CFException());
666     checkCompletedWithWrappedCFException(g);
667     }
668    
669     /**
670     * thenCombine result completes exceptionally if action does
671     */
672     public void testThenCombine3() {
673     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
674     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
675     FailingBiFunction r = new FailingBiFunction();
676     CompletableFuture<Integer> g = f.thenCombine(f2, r);
677     f.complete(one);
678     checkIncomplete(g);
679     f2.complete(two);
680     checkCompletedWithWrappedCFException(g);
681     }
682    
683     /**
684     * thenCombine result completes exceptionally if either source cancelled
685     */
686     public void testThenCombine4() {
687     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
688     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
689     CompletableFuture<Integer> g = f.thenCombine(f2, add);
690     assertTrue(f.cancel(true));
691     f2.complete(two);
692     checkCompletedWithWrappedCancellationException(g);
693     f = new CompletableFuture<Integer>();
694     f2 = new CompletableFuture<Integer>();
695     g = f.thenCombine(f2, add);
696     f.complete(one);
697     assertTrue(f2.cancel(true));
698     checkCompletedWithWrappedCancellationException(g);
699     }
700    
701     /**
702     * thenAcceptBoth result completes normally after normal
703     * completion of sources
704     */
705     public void testThenAcceptBoth() {
706     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
707     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
708     AddAction r = new AddAction();
709     CompletableFuture<Void> g = f.thenAcceptBoth(f2, r);
710     f.complete(one);
711     checkIncomplete(g);
712     f2.complete(two);
713     checkCompletedNormally(g, null);
714     assertEquals(r.value, 3);
715    
716     r = new AddAction();
717     f = new CompletableFuture<Integer>();
718     f.complete(one);
719     f2 = new CompletableFuture<Integer>();
720     g = f.thenAcceptBoth(f2, r);
721     checkIncomplete(g);
722     f2.complete(two);
723     checkCompletedNormally(g, null);
724     assertEquals(r.value, 3);
725     }
726    
727     /**
728     * thenAcceptBoth result completes exceptionally after exceptional
729     * completion of either source
730     */
731     public void testThenAcceptBoth2() {
732     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
733     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
734     AddAction r = new AddAction();
735     CompletableFuture<Void> g = f.thenAcceptBoth(f2, r);
736     f.completeExceptionally(new CFException());
737     f2.complete(two);
738     checkCompletedWithWrappedCFException(g);
739    
740     r = new AddAction();
741     f = new CompletableFuture<Integer>();
742     f.complete(one);
743     f2 = new CompletableFuture<Integer>();
744     g = f.thenAcceptBoth(f2, r);
745     f2.completeExceptionally(new CFException());
746     checkCompletedWithWrappedCFException(g);
747     }
748    
749     /**
750     * thenAcceptBoth result completes exceptionally if action does
751     */
752     public void testThenAcceptBoth3() {
753     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
754     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
755     FailingBiConsumer r = new FailingBiConsumer();
756     CompletableFuture<Void> g = f.thenAcceptBoth(f2, r);
757     f.complete(one);
758     checkIncomplete(g);
759     f2.complete(two);
760     checkCompletedWithWrappedCFException(g);
761     }
762    
763     /**
764     * thenAcceptBoth result completes exceptionally if either source cancelled
765     */
766     public void testThenAcceptBoth4() {
767     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
768     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
769     AddAction r = new AddAction();
770     CompletableFuture<Void> g = f.thenAcceptBoth(f2, r);
771     assertTrue(f.cancel(true));
772     f2.complete(two);
773     checkCompletedWithWrappedCancellationException(g);
774     f = new CompletableFuture<Integer>();
775     f2 = new CompletableFuture<Integer>();
776     r = new AddAction();
777     g = f.thenAcceptBoth(f2, r);
778     f.complete(one);
779     assertTrue(f2.cancel(true));
780     checkCompletedWithWrappedCancellationException(g);
781     }
782    
783     /**
784     * runAfterBoth result completes normally after normal
785     * completion of sources
786     */
787     public void testRunAfterBoth() {
788     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
789     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
790     Noop r = new Noop();
791     CompletableFuture<Void> g = f.runAfterBoth(f2, r);
792     f.complete(one);
793     checkIncomplete(g);
794     f2.complete(two);
795     checkCompletedNormally(g, null);
796     assertTrue(r.ran);
797    
798     r = new Noop();
799     f = new CompletableFuture<Integer>();
800     f.complete(one);
801     f2 = new CompletableFuture<Integer>();
802     g = f.runAfterBoth(f2, r);
803     checkIncomplete(g);
804     f2.complete(two);
805     checkCompletedNormally(g, null);
806     assertTrue(r.ran);
807     }
808    
809     /**
810     * runAfterBoth result completes exceptionally after exceptional
811     * completion of either source
812     */
813     public void testRunAfterBoth2() {
814     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
815     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
816     Noop r = new Noop();
817     CompletableFuture<Void> g = f.runAfterBoth(f2, r);
818     f.completeExceptionally(new CFException());
819     f2.complete(two);
820     checkCompletedWithWrappedCFException(g);
821    
822     r = new Noop();
823     f = new CompletableFuture<Integer>();
824     f.complete(one);
825     f2 = new CompletableFuture<Integer>();
826     g = f.runAfterBoth(f2, r);
827     f2.completeExceptionally(new CFException());
828     checkCompletedWithWrappedCFException(g);
829     }
830    
831 jsr166 1.4 /**
832 dl 1.5 * runAfterBoth result completes exceptionally if action does
833 jsr166 1.4 */
834 dl 1.5 public void testRunAfterBoth3() {
835     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
836     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
837     FailingNoop r = new FailingNoop();
838     CompletableFuture<Void> g = f.runAfterBoth(f2, r);
839     f.complete(one);
840     checkIncomplete(g);
841     f2.complete(two);
842     checkCompletedWithWrappedCFException(g);
843 jsr166 1.4 }
844    
845     /**
846 dl 1.5 * runAfterBoth result completes exceptionally if either source cancelled
847 jsr166 1.4 */
848 dl 1.5 public void testRunAfterBoth4() {
849     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
850     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
851     Noop r = new Noop();
852     CompletableFuture<Void> g = f.runAfterBoth(f2, r);
853     assertTrue(f.cancel(true));
854     f2.complete(two);
855     checkCompletedWithWrappedCancellationException(g);
856     f = new CompletableFuture<Integer>();
857     f2 = new CompletableFuture<Integer>();
858     r = new Noop();
859     g = f.runAfterBoth(f2, r);
860     f.complete(one);
861     assertTrue(f2.cancel(true));
862     checkCompletedWithWrappedCancellationException(g);
863 jsr166 1.4 }
864 dl 1.5
865     /**
866     * applyToEither result completes normally after normal completion
867     * of either source
868     */
869     public void testApplyToEither() {
870     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
871     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
872     CompletableFuture<Integer> g = f.applyToEither(f2, inc);
873     f.complete(one);
874     checkCompletedNormally(g, two);
875     f2.complete(one);
876     checkCompletedNormally(g, two);
877    
878     f = new CompletableFuture<Integer>();
879     f.complete(one);
880     f2 = new CompletableFuture<Integer>();
881     g = f.applyToEither(f2, inc);
882     checkCompletedNormally(g, two);
883     }
884    
885     /**
886     * applyToEither result completes exceptionally after exceptional
887     * completion of either source
888     */
889     public void testApplyToEither2() {
890     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
891     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
892     CompletableFuture<Integer> g = f.applyToEither(f2, inc);
893     f.completeExceptionally(new CFException());
894     f2.complete(one);
895     checkCompletedWithWrappedCFException(g);
896    
897     f = new CompletableFuture<Integer>();
898     f2 = new CompletableFuture<Integer>();
899     f2.completeExceptionally(new CFException());
900     g = f.applyToEither(f2, inc);
901     checkCompletedWithWrappedCFException(g);
902     }
903    
904     /**
905     * applyToEither result completes exceptionally if action does
906     */
907     public void testApplyToEither3() {
908     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
909     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
910     FailingFunction r = new FailingFunction();
911     CompletableFuture<Integer> g = f.applyToEither(f2, r);
912     f2.complete(two);
913     checkCompletedWithWrappedCFException(g);
914     }
915    
916     /**
917     * applyToEither result completes exceptionally if either source cancelled
918     */
919     public void testApplyToEither4() {
920     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
921     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
922     CompletableFuture<Integer> g = f.applyToEither(f2, inc);
923     assertTrue(f.cancel(true));
924     checkCompletedWithWrappedCancellationException(g);
925     f = new CompletableFuture<Integer>();
926     f2 = new CompletableFuture<Integer>();
927     assertTrue(f2.cancel(true));
928     checkCompletedWithWrappedCancellationException(g);
929     }
930    
931     /**
932     * acceptEither result completes normally after normal completion
933     * of either source
934     */
935     public void testAcceptEither() {
936     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
937     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
938     IncAction r = new IncAction();
939     CompletableFuture<Void> g = f.acceptEither(f2, r);
940     f.complete(one);
941     checkCompletedNormally(g, null);
942     f2.complete(one);
943     checkCompletedNormally(g, null);
944     assertEquals(r.value, 2);
945    
946     r = new IncAction();
947     f = new CompletableFuture<Integer>();
948     f.complete(one);
949     f2 = new CompletableFuture<Integer>();
950     g = f.acceptEither(f2, r);
951     checkCompletedNormally(g, null);
952     assertEquals(r.value, 2);
953     }
954    
955     /**
956     * acceptEither result completes exceptionally after exceptional
957     * completion of either source
958     */
959     public void testAcceptEither2() {
960     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
961     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
962     IncAction r = new IncAction();
963     CompletableFuture<Void> g = f.acceptEither(f2, r);
964     f.completeExceptionally(new CFException());
965     f2.complete(one);
966     checkCompletedWithWrappedCFException(g);
967    
968     r = new IncAction();
969     f = new CompletableFuture<Integer>();
970     f2 = new CompletableFuture<Integer>();
971     f2.completeExceptionally(new CFException());
972     g = f.acceptEither(f2, r);
973     checkCompletedWithWrappedCFException(g);
974     }
975    
976     /**
977     * acceptEither result completes exceptionally if action does
978     */
979     public void testAcceptEither3() {
980     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
981     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
982     FailingConsumer r = new FailingConsumer();
983     CompletableFuture<Void> g = f.acceptEither(f2, r);
984     f2.complete(two);
985     checkCompletedWithWrappedCFException(g);
986     }
987    
988     /**
989     * acceptEither result completes exceptionally if either source cancelled
990     */
991     public void testAcceptEither4() {
992     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
993     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
994     IncAction r = new IncAction();
995     CompletableFuture<Void> g = f.acceptEither(f2, r);
996     assertTrue(f.cancel(true));
997     checkCompletedWithWrappedCancellationException(g);
998     f = new CompletableFuture<Integer>();
999     f2 = new CompletableFuture<Integer>();
1000     assertTrue(f2.cancel(true));
1001     checkCompletedWithWrappedCancellationException(g);
1002     }
1003    
1004    
1005     /**
1006     * runAfterEither result completes normally after normal completion
1007     * of either source
1008     */
1009     public void testRunAfterEither() {
1010     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1011     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1012     Noop r = new Noop();
1013     CompletableFuture<Void> g = f.runAfterEither(f2, r);
1014     f.complete(one);
1015     checkCompletedNormally(g, null);
1016     f2.complete(one);
1017     checkCompletedNormally(g, null);
1018     assertTrue(r.ran);
1019    
1020     r = new Noop();
1021     f = new CompletableFuture<Integer>();
1022     f.complete(one);
1023     f2 = new CompletableFuture<Integer>();
1024     g = f.runAfterEither(f2, r);
1025     checkCompletedNormally(g, null);
1026     assertTrue(r.ran);
1027     }
1028    
1029     /**
1030     * runAfterEither result completes exceptionally after exceptional
1031     * completion of either source
1032     */
1033     public void testRunAfterEither2() {
1034     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1035     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1036     Noop r = new Noop();
1037     CompletableFuture<Void> g = f.runAfterEither(f2, r);
1038     f.completeExceptionally(new CFException());
1039     f2.complete(one);
1040     checkCompletedWithWrappedCFException(g);
1041    
1042     r = new Noop();
1043     f = new CompletableFuture<Integer>();
1044     f2 = new CompletableFuture<Integer>();
1045     f2.completeExceptionally(new CFException());
1046     g = f.runAfterEither(f2, r);
1047     checkCompletedWithWrappedCFException(g);
1048     }
1049    
1050     /**
1051     * runAfterEither result completes exceptionally if action does
1052     */
1053     public void testRunAfterEither3() {
1054     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1055     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1056     FailingNoop r = new FailingNoop();
1057     CompletableFuture<Void> g = f.runAfterEither(f2, r);
1058     f2.complete(two);
1059     checkCompletedWithWrappedCFException(g);
1060     }
1061    
1062     /**
1063     * runAfterEither result completes exceptionally if either source cancelled
1064     */
1065     public void testRunAfterEither4() {
1066     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1067     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1068     Noop r = new Noop();
1069     CompletableFuture<Void> g = f.runAfterEither(f2, r);
1070     assertTrue(f.cancel(true));
1071     checkCompletedWithWrappedCancellationException(g);
1072     f = new CompletableFuture<Integer>();
1073     f2 = new CompletableFuture<Integer>();
1074     assertTrue(f2.cancel(true));
1075     checkCompletedWithWrappedCancellationException(g);
1076     }
1077    
1078     /**
1079     * thenCompose result completes normally after normal completion of source
1080     */
1081     public void testThenCompose() {
1082     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1083     CompletableFutureInc r = new CompletableFutureInc();
1084     CompletableFuture<Integer> g = f.thenCompose(r);
1085     f.complete(one);
1086     checkCompletedNormally(g, two);
1087     }
1088    
1089     /**
1090     * thenCompose result completes exceptionally after exceptional
1091     * completion of source
1092     */
1093     public void testThenCompose2() {
1094     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1095     CompletableFutureInc r = new CompletableFutureInc();
1096     CompletableFuture<Integer> g = f.thenCompose(r);
1097     f.completeExceptionally(new CFException());
1098     checkCompletedWithWrappedCFException(g);
1099     }
1100    
1101     /**
1102     * thenCompose result completes exceptionally if action does
1103     */
1104     public void testThenCompose3() {
1105     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1106     FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
1107     CompletableFuture<Integer> g = f.thenCompose(r);
1108     f.complete(one);
1109     checkCompletedWithWrappedCFException(g);
1110     }
1111    
1112     /**
1113     * thenCompose result completes exceptionally if source cancelled
1114     */
1115     public void testThenCompose4() {
1116     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1117     CompletableFutureInc r = new CompletableFutureInc();
1118     CompletableFuture<Integer> g = f.thenCompose(r);
1119     assertTrue(f.cancel(true));
1120     checkCompletedWithWrappedCancellationException(g);
1121     }
1122    
1123    
1124     // asyncs
1125    
1126     /**
1127     * thenRunAsync result completes normally after normal completion of source
1128     */
1129     public void testThenRunAsync() {
1130     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1131     Noop r = new Noop();
1132     CompletableFuture<Void> g = f.thenRunAsync(r);
1133     f.complete(null);
1134     checkCompletedNormally(g, null);
1135    
1136     // reordered version
1137     f = new CompletableFuture<Integer>();
1138     f.complete(null);
1139     r = new Noop();
1140     g = f.thenRunAsync(r);
1141     checkCompletedNormally(g, null);
1142     }
1143    
1144     /**
1145     * thenRunAsync result completes exceptionally after exceptional
1146     * completion of source
1147     */
1148     public void testThenRunAsync2() {
1149     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1150     Noop r = new Noop();
1151     CompletableFuture<Void> g = f.thenRunAsync(r);
1152     f.completeExceptionally(new CFException());
1153     try {
1154     g.join();
1155     shouldThrow();
1156 jsr166 1.6 } catch (Exception ok) {
1157 dl 1.5 }
1158     checkCompletedWithWrappedCFException(g);
1159     }
1160    
1161     /**
1162     * thenRunAsync result completes exceptionally if action does
1163     */
1164     public void testThenRunAsync3() {
1165     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1166     FailingNoop r = new FailingNoop();
1167     CompletableFuture<Void> g = f.thenRunAsync(r);
1168     f.complete(null);
1169     checkCompletedWithWrappedCFException(g);
1170     }
1171 jsr166 1.6
1172 dl 1.5 /**
1173     * thenRunAsync result completes exceptionally if source cancelled
1174     */
1175     public void testThenRunAsync4() {
1176     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1177     Noop r = new Noop();
1178     CompletableFuture<Void> g = f.thenRunAsync(r);
1179     assertTrue(f.cancel(true));
1180     checkCompletedWithWrappedCancellationException(g);
1181     }
1182    
1183     /**
1184     * thenApplyAsync result completes normally after normal completion of source
1185     */
1186     public void testThenApplyAsync() {
1187     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1188     CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1189     f.complete(one);
1190     checkCompletedNormally(g, two);
1191     }
1192    
1193     /**
1194     * thenApplyAsync result completes exceptionally after exceptional
1195     * completion of source
1196     */
1197     public void testThenApplyAsync2() {
1198     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1199     CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1200     f.completeExceptionally(new CFException());
1201     checkCompletedWithWrappedCFException(g);
1202     }
1203    
1204     /**
1205     * thenApplyAsync result completes exceptionally if action does
1206     */
1207     public void testThenApplyAsync3() {
1208     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1209     FailingFunction r = new FailingFunction();
1210     CompletableFuture<Integer> g = f.thenApplyAsync(r);
1211     f.complete(null);
1212     checkCompletedWithWrappedCFException(g);
1213     }
1214 jsr166 1.6
1215 dl 1.5 /**
1216     * thenApplyAsync result completes exceptionally if source cancelled
1217     */
1218     public void testThenApplyAsync4() {
1219     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1220     CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1221     assertTrue(f.cancel(true));
1222     checkCompletedWithWrappedCancellationException(g);
1223     }
1224    
1225     /**
1226     * thenAcceptAsync result completes normally after normal
1227     * completion of source
1228     */
1229     public void testThenAcceptAsync() {
1230     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1231     IncAction r = new IncAction();
1232     CompletableFuture<Void> g = f.thenAcceptAsync(r);
1233     f.complete(one);
1234     checkCompletedNormally(g, null);
1235     assertEquals(r.value, 2);
1236     }
1237    
1238     /**
1239     * thenAcceptAsync result completes exceptionally after exceptional
1240     * completion of source
1241     */
1242     public void testThenAcceptAsync2() {
1243     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1244     IncAction r = new IncAction();
1245     CompletableFuture<Void> g = f.thenAcceptAsync(r);
1246     f.completeExceptionally(new CFException());
1247     checkCompletedWithWrappedCFException(g);
1248     }
1249    
1250     /**
1251     * thenAcceptAsync result completes exceptionally if action does
1252     */
1253     public void testThenAcceptAsync3() {
1254     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1255     FailingConsumer r = new FailingConsumer();
1256     CompletableFuture<Void> g = f.thenAcceptAsync(r);
1257     f.complete(null);
1258     checkCompletedWithWrappedCFException(g);
1259     }
1260 jsr166 1.6
1261 dl 1.5 /**
1262     * thenAcceptAsync result completes exceptionally if source cancelled
1263     */
1264     public void testThenAcceptAsync4() {
1265     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1266     IncAction r = new IncAction();
1267     CompletableFuture<Void> g = f.thenAcceptAsync(r);
1268     assertTrue(f.cancel(true));
1269     checkCompletedWithWrappedCancellationException(g);
1270     }
1271     /**
1272     * thenCombineAsync result completes normally after normal
1273     * completion of sources
1274     */
1275     public void testThenCombineAsync() {
1276     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1277     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1278     CompletableFuture<Integer> g = f.thenCombineAsync(f2, add);
1279     f.complete(one);
1280     checkIncomplete(g);
1281     f2.complete(two);
1282     checkCompletedNormally(g, three);
1283     }
1284    
1285     /**
1286     * thenCombineAsync result completes exceptionally after exceptional
1287     * completion of source
1288     */
1289     public void testThenCombineAsync2() {
1290     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1291     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1292     CompletableFuture<Integer> g = f.thenCombineAsync(f2, add);
1293     f.completeExceptionally(new CFException());
1294     f2.complete(two);
1295     checkCompletedWithWrappedCFException(g);
1296    
1297     f = new CompletableFuture<Integer>();
1298     f2 = new CompletableFuture<Integer>();
1299     g = f.thenCombineAsync(f2, add);
1300     f.complete(one);
1301     f2.completeExceptionally(new CFException());
1302     checkCompletedWithWrappedCFException(g);
1303     }
1304    
1305     /**
1306     * thenCombineAsync result completes exceptionally if action does
1307     */
1308     public void testThenCombineAsync3() {
1309     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1310     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1311     FailingBiFunction r = new FailingBiFunction();
1312     CompletableFuture<Integer> g = f.thenCombineAsync(f2, r);
1313     f.complete(one);
1314     checkIncomplete(g);
1315     f2.complete(two);
1316     checkCompletedWithWrappedCFException(g);
1317     }
1318 jsr166 1.6
1319 dl 1.5 /**
1320     * thenCombineAsync result completes exceptionally if either source cancelled
1321     */
1322     public void testThenCombineAsync4() {
1323     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1324     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1325     CompletableFuture<Integer> g = f.thenCombineAsync(f2, add);
1326     assertTrue(f.cancel(true));
1327     f2.complete(two);
1328     checkCompletedWithWrappedCancellationException(g);
1329 jsr166 1.6
1330 dl 1.5 f = new CompletableFuture<Integer>();
1331     f2 = new CompletableFuture<Integer>();
1332     g = f.thenCombineAsync(f2, add);
1333     f.complete(one);
1334     assertTrue(f2.cancel(true));
1335     checkCompletedWithWrappedCancellationException(g);
1336     }
1337    
1338     /**
1339     * thenAcceptBothAsync result completes normally after normal
1340     * completion of sources
1341     */
1342     public void testThenAcceptBothAsync() {
1343     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1344     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1345     AddAction r = new AddAction();
1346     CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r);
1347     f.complete(one);
1348     checkIncomplete(g);
1349     f2.complete(two);
1350     checkCompletedNormally(g, null);
1351     assertEquals(r.value, 3);
1352     }
1353    
1354     /**
1355     * thenAcceptBothAsync result completes exceptionally after exceptional
1356     * completion of source
1357     */
1358     public void testThenAcceptBothAsync2() {
1359     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1360     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1361     AddAction r = new AddAction();
1362     CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r);
1363     f.completeExceptionally(new CFException());
1364     f2.complete(two);
1365     checkCompletedWithWrappedCFException(g);
1366    
1367     r = new AddAction();
1368     f = new CompletableFuture<Integer>();
1369     f2 = new CompletableFuture<Integer>();
1370     g = f.thenAcceptBothAsync(f2, r);
1371     f.complete(one);
1372     f2.completeExceptionally(new CFException());
1373     checkCompletedWithWrappedCFException(g);
1374     }
1375    
1376     /**
1377     * thenAcceptBothAsync result completes exceptionally if action does
1378     */
1379     public void testThenAcceptBothAsync3() {
1380     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1381     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1382     FailingBiConsumer r = new FailingBiConsumer();
1383 jsr166 1.6 CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r);
1384 dl 1.5 f.complete(one);
1385     checkIncomplete(g);
1386     f2.complete(two);
1387     checkCompletedWithWrappedCFException(g);
1388     }
1389 jsr166 1.6
1390 dl 1.5 /**
1391     * thenAcceptBothAsync result completes exceptionally if either source cancelled
1392     */
1393     public void testThenAcceptBothAsync4() {
1394     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1395     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1396     AddAction r = new AddAction();
1397     CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r);
1398     assertTrue(f.cancel(true));
1399     f2.complete(two);
1400     checkCompletedWithWrappedCancellationException(g);
1401 jsr166 1.6
1402 dl 1.5 r = new AddAction();
1403     f = new CompletableFuture<Integer>();
1404     f2 = new CompletableFuture<Integer>();
1405     g = f.thenAcceptBothAsync(f2, r);
1406     f.complete(one);
1407     assertTrue(f2.cancel(true));
1408     checkCompletedWithWrappedCancellationException(g);
1409     }
1410    
1411     /**
1412     * runAfterBothAsync result completes normally after normal
1413     * completion of sources
1414     */
1415     public void testRunAfterBothAsync() {
1416     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1417     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1418     Noop r = new Noop();
1419     CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1420     f.complete(one);
1421     checkIncomplete(g);
1422     f2.complete(two);
1423     checkCompletedNormally(g, null);
1424     assertTrue(r.ran);
1425     }
1426    
1427     /**
1428     * runAfterBothAsync result completes exceptionally after exceptional
1429     * completion of source
1430     */
1431     public void testRunAfterBothAsync2() {
1432     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1433     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1434     Noop r = new Noop();
1435     CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1436     f.completeExceptionally(new CFException());
1437     f2.complete(two);
1438     checkCompletedWithWrappedCFException(g);
1439    
1440     r = new Noop();
1441     f = new CompletableFuture<Integer>();
1442     f2 = new CompletableFuture<Integer>();
1443     g = f.runAfterBothAsync(f2, r);
1444     f.complete(one);
1445     f2.completeExceptionally(new CFException());
1446     checkCompletedWithWrappedCFException(g);
1447     }
1448    
1449     /**
1450     * runAfterBothAsync result completes exceptionally if action does
1451     */
1452     public void testRunAfterBothAsync3() {
1453     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1454     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1455     FailingNoop r = new FailingNoop();
1456 jsr166 1.6 CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1457 dl 1.5 f.complete(one);
1458     checkIncomplete(g);
1459     f2.complete(two);
1460     checkCompletedWithWrappedCFException(g);
1461     }
1462 jsr166 1.6
1463 dl 1.5 /**
1464     * runAfterBothAsync result completes exceptionally if either source cancelled
1465     */
1466     public void testRunAfterBothAsync4() {
1467     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1468     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1469     Noop r = new Noop();
1470     CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1471     assertTrue(f.cancel(true));
1472     f2.complete(two);
1473     checkCompletedWithWrappedCancellationException(g);
1474 jsr166 1.6
1475 dl 1.5 r = new Noop();
1476     f = new CompletableFuture<Integer>();
1477     f2 = new CompletableFuture<Integer>();
1478     g = f.runAfterBothAsync(f2, r);
1479     f.complete(one);
1480     assertTrue(f2.cancel(true));
1481     checkCompletedWithWrappedCancellationException(g);
1482     }
1483    
1484     /**
1485     * applyToEitherAsync result completes normally after normal
1486     * completion of sources
1487     */
1488     public void testApplyToEitherAsync() {
1489     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1490     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1491     CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1492     f.complete(one);
1493     checkCompletedNormally(g, two);
1494    
1495     f = new CompletableFuture<Integer>();
1496     f.complete(one);
1497     f2 = new CompletableFuture<Integer>();
1498     g = f.applyToEitherAsync(f2, inc);
1499     checkCompletedNormally(g, two);
1500     }
1501    
1502     /**
1503     * applyToEitherAsync result completes exceptionally after exceptional
1504     * completion of source
1505     */
1506     public void testApplyToEitherAsync2() {
1507     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1508     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1509     CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1510     f.completeExceptionally(new CFException());
1511     checkCompletedWithWrappedCFException(g);
1512    
1513     f = new CompletableFuture<Integer>();
1514     f2 = new CompletableFuture<Integer>();
1515     f2.completeExceptionally(new CFException());
1516     g = f.applyToEitherAsync(f2, inc);
1517     f.complete(one);
1518     checkCompletedWithWrappedCFException(g);
1519     }
1520    
1521     /**
1522     * applyToEitherAsync result completes exceptionally if action does
1523     */
1524     public void testApplyToEitherAsync3() {
1525     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1526     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1527     FailingFunction r = new FailingFunction();
1528 jsr166 1.6 CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r);
1529 dl 1.5 f.complete(one);
1530     checkCompletedWithWrappedCFException(g);
1531     }
1532 jsr166 1.6
1533 dl 1.5 /**
1534     * applyToEitherAsync result completes exceptionally if either source cancelled
1535     */
1536     public void testApplyToEitherAsync4() {
1537     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1538     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1539     CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1540     assertTrue(f.cancel(true));
1541     checkCompletedWithWrappedCancellationException(g);
1542 jsr166 1.6
1543 dl 1.5 f = new CompletableFuture<Integer>();
1544     f2 = new CompletableFuture<Integer>();
1545     assertTrue(f2.cancel(true));
1546     g = f.applyToEitherAsync(f2, inc);
1547     checkCompletedWithWrappedCancellationException(g);
1548     }
1549    
1550     /**
1551     * acceptEitherAsync result completes normally after normal
1552     * completion of sources
1553     */
1554     public void testAcceptEitherAsync() {
1555     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1556     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1557     IncAction r = new IncAction();
1558     CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1559     f.complete(one);
1560     checkCompletedNormally(g, null);
1561     assertEquals(r.value, 2);
1562    
1563     r = new IncAction();
1564     f = new CompletableFuture<Integer>();
1565     f.complete(one);
1566     f2 = new CompletableFuture<Integer>();
1567     g = f.acceptEitherAsync(f2, r);
1568     checkCompletedNormally(g, null);
1569     assertEquals(r.value, 2);
1570     }
1571    
1572     /**
1573     * acceptEitherAsync result completes exceptionally after exceptional
1574     * completion of source
1575     */
1576     public void testAcceptEitherAsync2() {
1577     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1578     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1579     IncAction r = new IncAction();
1580     CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1581     f.completeExceptionally(new CFException());
1582     checkCompletedWithWrappedCFException(g);
1583    
1584     r = new IncAction();
1585     f = new CompletableFuture<Integer>();
1586     f2 = new CompletableFuture<Integer>();
1587     f2.completeExceptionally(new CFException());
1588     g = f.acceptEitherAsync(f2, r);
1589     f.complete(one);
1590     checkCompletedWithWrappedCFException(g);
1591     }
1592    
1593     /**
1594     * acceptEitherAsync result completes exceptionally if action does
1595     */
1596     public void testAcceptEitherAsync3() {
1597     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1598     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1599     FailingConsumer r = new FailingConsumer();
1600 jsr166 1.6 CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1601 dl 1.5 f.complete(one);
1602     checkCompletedWithWrappedCFException(g);
1603     }
1604 jsr166 1.6
1605 dl 1.5 /**
1606     * acceptEitherAsync result completes exceptionally if either
1607     * source cancelled
1608     */
1609     public void testAcceptEitherAsync4() {
1610     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1611     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1612     IncAction r = new IncAction();
1613     CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1614     assertTrue(f.cancel(true));
1615     checkCompletedWithWrappedCancellationException(g);
1616 jsr166 1.6
1617 dl 1.5 r = new IncAction();
1618     f = new CompletableFuture<Integer>();
1619     f2 = new CompletableFuture<Integer>();
1620     assertTrue(f2.cancel(true));
1621     g = f.acceptEitherAsync(f2, r);
1622     checkCompletedWithWrappedCancellationException(g);
1623     }
1624    
1625     /**
1626     * runAfterEitherAsync result completes normally after normal
1627     * completion of sources
1628     */
1629     public void testRunAfterEitherAsync() {
1630     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1631     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1632     Noop r = new Noop();
1633     CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1634     f.complete(one);
1635     checkCompletedNormally(g, null);
1636     assertTrue(r.ran);
1637    
1638     r = new Noop();
1639     f = new CompletableFuture<Integer>();
1640     f.complete(one);
1641     f2 = new CompletableFuture<Integer>();
1642     g = f.runAfterEitherAsync(f2, r);
1643     checkCompletedNormally(g, null);
1644     assertTrue(r.ran);
1645     }
1646    
1647     /**
1648     * runAfterEitherAsync result completes exceptionally after exceptional
1649     * completion of source
1650     */
1651     public void testRunAfterEitherAsync2() {
1652     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1653     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1654     Noop r = new Noop();
1655     CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1656     f.completeExceptionally(new CFException());
1657     checkCompletedWithWrappedCFException(g);
1658    
1659     r = new Noop();
1660     f = new CompletableFuture<Integer>();
1661     f2 = new CompletableFuture<Integer>();
1662     f2.completeExceptionally(new CFException());
1663     g = f.runAfterEitherAsync(f2, r);
1664     f.complete(one);
1665     checkCompletedWithWrappedCFException(g);
1666     }
1667    
1668     /**
1669     * runAfterEitherAsync result completes exceptionally if action does
1670     */
1671     public void testRunAfterEitherAsync3() {
1672     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1673     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1674     FailingNoop r = new FailingNoop();
1675 jsr166 1.6 CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1676 dl 1.5 f.complete(one);
1677     checkCompletedWithWrappedCFException(g);
1678     }
1679 jsr166 1.6
1680 dl 1.5 /**
1681     * runAfterEitherAsync result completes exceptionally if either
1682     * source cancelled
1683     */
1684     public void testRunAfterEitherAsync4() {
1685     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1686     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1687     Noop r = new Noop();
1688     CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1689     assertTrue(f.cancel(true));
1690     checkCompletedWithWrappedCancellationException(g);
1691 jsr166 1.6
1692 dl 1.5 r = new Noop();
1693     f = new CompletableFuture<Integer>();
1694     f2 = new CompletableFuture<Integer>();
1695     assertTrue(f2.cancel(true));
1696     g = f.runAfterEitherAsync(f2, r);
1697     checkCompletedWithWrappedCancellationException(g);
1698     }
1699    
1700     /**
1701 jsr166 1.7 * thenComposeAsync result completes normally after normal
1702     * completion of source
1703 dl 1.5 */
1704     public void testThenComposeAsync() {
1705     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1706     CompletableFutureInc r = new CompletableFutureInc();
1707     CompletableFuture<Integer> g = f.thenComposeAsync(r);
1708     f.complete(one);
1709     checkCompletedNormally(g, two);
1710     }
1711    
1712     /**
1713 jsr166 1.7 * thenComposeAsync result completes exceptionally after
1714     * exceptional completion of source
1715 dl 1.5 */
1716     public void testThenComposeAsync2() {
1717     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1718     CompletableFutureInc r = new CompletableFutureInc();
1719     CompletableFuture<Integer> g = f.thenComposeAsync(r);
1720     f.completeExceptionally(new CFException());
1721     checkCompletedWithWrappedCFException(g);
1722     }
1723    
1724     /**
1725     * thenComposeAsync result completes exceptionally if action does
1726     */
1727     public void testThenComposeAsync3() {
1728     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1729     FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
1730     CompletableFuture<Integer> g = f.thenComposeAsync(r);
1731     f.complete(one);
1732     checkCompletedWithWrappedCFException(g);
1733     }
1734    
1735     /**
1736     * thenComposeAsync result completes exceptionally if source cancelled
1737     */
1738     public void testThenComposeAsync4() {
1739     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1740     CompletableFutureInc r = new CompletableFutureInc();
1741     CompletableFuture<Integer> g = f.thenComposeAsync(r);
1742     assertTrue(f.cancel(true));
1743     checkCompletedWithWrappedCancellationException(g);
1744     }
1745    
1746    
1747 jsr166 1.7 // async with explicit executors
1748 dl 1.5
1749     /**
1750     * thenRunAsync result completes normally after normal completion of source
1751     */
1752     public void testThenRunAsyncE() {
1753     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1754     Noop r = new Noop();
1755     CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
1756     f.complete(null);
1757     checkCompletedNormally(g, null);
1758    
1759     // reordered version
1760     f = new CompletableFuture<Integer>();
1761     f.complete(null);
1762     r = new Noop();
1763     g = f.thenRunAsync(r, new ThreadExecutor());
1764     checkCompletedNormally(g, null);
1765     }
1766    
1767     /**
1768     * thenRunAsync result completes exceptionally after exceptional
1769     * completion of source
1770     */
1771     public void testThenRunAsync2E() {
1772     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1773     Noop r = new Noop();
1774     CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
1775     f.completeExceptionally(new CFException());
1776     try {
1777     g.join();
1778     shouldThrow();
1779 jsr166 1.6 } catch (Exception ok) {
1780 dl 1.5 }
1781     checkCompletedWithWrappedCFException(g);
1782     }
1783    
1784     /**
1785     * thenRunAsync result completes exceptionally if action does
1786     */
1787     public void testThenRunAsync3E() {
1788     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1789     FailingNoop r = new FailingNoop();
1790     CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
1791     f.complete(null);
1792     checkCompletedWithWrappedCFException(g);
1793     }
1794 jsr166 1.6
1795 dl 1.5 /**
1796     * thenRunAsync result completes exceptionally if source cancelled
1797     */
1798     public void testThenRunAsync4E() {
1799     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1800     Noop r = new Noop();
1801     CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
1802     assertTrue(f.cancel(true));
1803     checkCompletedWithWrappedCancellationException(g);
1804     }
1805    
1806     /**
1807     * thenApplyAsync result completes normally after normal completion of source
1808     */
1809     public void testThenApplyAsyncE() {
1810     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1811     CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
1812     f.complete(one);
1813     checkCompletedNormally(g, two);
1814     }
1815    
1816     /**
1817     * thenApplyAsync result completes exceptionally after exceptional
1818     * completion of source
1819     */
1820     public void testThenApplyAsync2E() {
1821     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1822     CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
1823     f.completeExceptionally(new CFException());
1824     checkCompletedWithWrappedCFException(g);
1825     }
1826    
1827     /**
1828     * thenApplyAsync result completes exceptionally if action does
1829     */
1830     public void testThenApplyAsync3E() {
1831     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1832     FailingFunction r = new FailingFunction();
1833     CompletableFuture<Integer> g = f.thenApplyAsync(r, new ThreadExecutor());
1834     f.complete(null);
1835     checkCompletedWithWrappedCFException(g);
1836     }
1837 jsr166 1.6
1838 dl 1.5 /**
1839     * thenApplyAsync result completes exceptionally if source cancelled
1840     */
1841     public void testThenApplyAsync4E() {
1842     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1843     CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
1844     assertTrue(f.cancel(true));
1845     checkCompletedWithWrappedCancellationException(g);
1846     }
1847    
1848     /**
1849     * thenAcceptAsync result completes normally after normal
1850     * completion of source
1851     */
1852     public void testThenAcceptAsyncE() {
1853     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1854     IncAction r = new IncAction();
1855     CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
1856     f.complete(one);
1857     checkCompletedNormally(g, null);
1858     assertEquals(r.value, 2);
1859     }
1860    
1861     /**
1862     * thenAcceptAsync result completes exceptionally after exceptional
1863     * completion of source
1864     */
1865     public void testThenAcceptAsync2E() {
1866     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1867     IncAction r = new IncAction();
1868     CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
1869     f.completeExceptionally(new CFException());
1870     checkCompletedWithWrappedCFException(g);
1871     }
1872    
1873     /**
1874     * thenAcceptAsync result completes exceptionally if action does
1875     */
1876     public void testThenAcceptAsync3E() {
1877     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1878     FailingConsumer r = new FailingConsumer();
1879     CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
1880     f.complete(null);
1881     checkCompletedWithWrappedCFException(g);
1882     }
1883 jsr166 1.6
1884 dl 1.5 /**
1885     * thenAcceptAsync result completes exceptionally if source cancelled
1886     */
1887     public void testThenAcceptAsync4E() {
1888     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1889     IncAction r = new IncAction();
1890     CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
1891     assertTrue(f.cancel(true));
1892     checkCompletedWithWrappedCancellationException(g);
1893     }
1894     /**
1895     * thenCombineAsync result completes normally after normal
1896     * completion of sources
1897     */
1898     public void testThenCombineAsyncE() {
1899     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1900     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1901     CompletableFuture<Integer> g = f.thenCombineAsync(f2, add, new ThreadExecutor());
1902     f.complete(one);
1903     checkIncomplete(g);
1904     f2.complete(two);
1905     checkCompletedNormally(g, three);
1906     }
1907    
1908     /**
1909     * thenCombineAsync result completes exceptionally after exceptional
1910     * completion of source
1911     */
1912     public void testThenCombineAsync2E() {
1913     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1914     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1915     CompletableFuture<Integer> g = f.thenCombineAsync(f2, add, new ThreadExecutor());
1916     f.completeExceptionally(new CFException());
1917     f2.complete(two);
1918     checkCompletedWithWrappedCFException(g);
1919    
1920     f = new CompletableFuture<Integer>();
1921     f2 = new CompletableFuture<Integer>();
1922     g = f.thenCombineAsync(f2, add, new ThreadExecutor());
1923     f.complete(one);
1924     f2.completeExceptionally(new CFException());
1925     checkCompletedWithWrappedCFException(g);
1926     }
1927    
1928     /**
1929     * thenCombineAsync result completes exceptionally if action does
1930     */
1931     public void testThenCombineAsync3E() {
1932     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1933     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1934     FailingBiFunction r = new FailingBiFunction();
1935     CompletableFuture<Integer> g = f.thenCombineAsync(f2, r, new ThreadExecutor());
1936     f.complete(one);
1937     checkIncomplete(g);
1938     f2.complete(two);
1939     checkCompletedWithWrappedCFException(g);
1940     }
1941 jsr166 1.6
1942 dl 1.5 /**
1943     * thenCombineAsync result completes exceptionally if either source cancelled
1944     */
1945     public void testThenCombineAsync4E() {
1946     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1947     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1948     CompletableFuture<Integer> g = f.thenCombineAsync(f2, add, new ThreadExecutor());
1949     assertTrue(f.cancel(true));
1950     f2.complete(two);
1951     checkCompletedWithWrappedCancellationException(g);
1952 jsr166 1.6
1953 dl 1.5 f = new CompletableFuture<Integer>();
1954     f2 = new CompletableFuture<Integer>();
1955     g = f.thenCombineAsync(f2, add, new ThreadExecutor());
1956     f.complete(one);
1957     assertTrue(f2.cancel(true));
1958     checkCompletedWithWrappedCancellationException(g);
1959     }
1960    
1961     /**
1962     * thenAcceptBothAsync result completes normally after normal
1963     * completion of sources
1964     */
1965     public void testThenAcceptBothAsyncE() {
1966     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1967     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1968     AddAction r = new AddAction();
1969     CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
1970     f.complete(one);
1971     checkIncomplete(g);
1972     f2.complete(two);
1973     checkCompletedNormally(g, null);
1974     assertEquals(r.value, 3);
1975     }
1976    
1977     /**
1978     * thenAcceptBothAsync result completes exceptionally after exceptional
1979     * completion of source
1980     */
1981     public void testThenAcceptBothAsync2E() {
1982     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1983     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1984     AddAction r = new AddAction();
1985     CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
1986     f.completeExceptionally(new CFException());
1987     f2.complete(two);
1988     checkCompletedWithWrappedCFException(g);
1989    
1990     r = new AddAction();
1991     f = new CompletableFuture<Integer>();
1992     f2 = new CompletableFuture<Integer>();
1993     g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
1994     f.complete(one);
1995     f2.completeExceptionally(new CFException());
1996     checkCompletedWithWrappedCFException(g);
1997     }
1998    
1999     /**
2000     * thenAcceptBothAsync result completes exceptionally if action does
2001     */
2002     public void testThenAcceptBothAsync3E() {
2003     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2004     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2005     FailingBiConsumer r = new FailingBiConsumer();
2006 jsr166 1.6 CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
2007 dl 1.5 f.complete(one);
2008     checkIncomplete(g);
2009     f2.complete(two);
2010     checkCompletedWithWrappedCFException(g);
2011     }
2012 jsr166 1.6
2013 dl 1.5 /**
2014     * thenAcceptBothAsync result completes exceptionally if either source cancelled
2015     */
2016     public void testThenAcceptBothAsync4E() {
2017     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2018     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2019     AddAction r = new AddAction();
2020     CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
2021     assertTrue(f.cancel(true));
2022     f2.complete(two);
2023     checkCompletedWithWrappedCancellationException(g);
2024 jsr166 1.6
2025 dl 1.5 r = new AddAction();
2026     f = new CompletableFuture<Integer>();
2027     f2 = new CompletableFuture<Integer>();
2028     g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
2029     f.complete(one);
2030     assertTrue(f2.cancel(true));
2031     checkCompletedWithWrappedCancellationException(g);
2032     }
2033    
2034     /**
2035     * runAfterBothAsync result completes normally after normal
2036     * completion of sources
2037     */
2038     public void testRunAfterBothAsyncE() {
2039     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2040     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2041     Noop r = new Noop();
2042     CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2043     f.complete(one);
2044     checkIncomplete(g);
2045     f2.complete(two);
2046     checkCompletedNormally(g, null);
2047     assertTrue(r.ran);
2048     }
2049    
2050     /**
2051     * runAfterBothAsync result completes exceptionally after exceptional
2052     * completion of source
2053     */
2054     public void testRunAfterBothAsync2E() {
2055     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2056     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2057     Noop r = new Noop();
2058     CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2059     f.completeExceptionally(new CFException());
2060     f2.complete(two);
2061     checkCompletedWithWrappedCFException(g);
2062    
2063     r = new Noop();
2064     f = new CompletableFuture<Integer>();
2065     f2 = new CompletableFuture<Integer>();
2066     g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2067     f.complete(one);
2068     f2.completeExceptionally(new CFException());
2069     checkCompletedWithWrappedCFException(g);
2070     }
2071    
2072     /**
2073     * runAfterBothAsync result completes exceptionally if action does
2074     */
2075     public void testRunAfterBothAsync3E() {
2076     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2077     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2078     FailingNoop r = new FailingNoop();
2079 jsr166 1.6 CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2080 dl 1.5 f.complete(one);
2081     checkIncomplete(g);
2082     f2.complete(two);
2083     checkCompletedWithWrappedCFException(g);
2084     }
2085 jsr166 1.6
2086 dl 1.5 /**
2087     * runAfterBothAsync result completes exceptionally if either source cancelled
2088     */
2089     public void testRunAfterBothAsync4E() {
2090     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2091     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2092     Noop r = new Noop();
2093     CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2094     assertTrue(f.cancel(true));
2095     f2.complete(two);
2096     checkCompletedWithWrappedCancellationException(g);
2097 jsr166 1.6
2098 dl 1.5 r = new Noop();
2099     f = new CompletableFuture<Integer>();
2100     f2 = new CompletableFuture<Integer>();
2101     g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2102     f.complete(one);
2103     assertTrue(f2.cancel(true));
2104     checkCompletedWithWrappedCancellationException(g);
2105     }
2106    
2107     /**
2108     * applyToEitherAsync result completes normally after normal
2109     * completion of sources
2110     */
2111     public void testApplyToEitherAsyncE() {
2112     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2113     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2114     CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2115     f.complete(one);
2116     checkCompletedNormally(g, two);
2117    
2118     f = new CompletableFuture<Integer>();
2119     f.complete(one);
2120     f2 = new CompletableFuture<Integer>();
2121     g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2122     checkCompletedNormally(g, two);
2123     }
2124    
2125     /**
2126     * applyToEitherAsync result completes exceptionally after exceptional
2127     * completion of source
2128     */
2129     public void testApplyToEitherAsync2E() {
2130     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2131     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2132     CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2133     f.completeExceptionally(new CFException());
2134     checkCompletedWithWrappedCFException(g);
2135    
2136     f = new CompletableFuture<Integer>();
2137     f2 = new CompletableFuture<Integer>();
2138     f2.completeExceptionally(new CFException());
2139     g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2140     f.complete(one);
2141     checkCompletedWithWrappedCFException(g);
2142     }
2143    
2144     /**
2145     * applyToEitherAsync result completes exceptionally if action does
2146     */
2147     public void testApplyToEitherAsync3E() {
2148     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2149     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2150     FailingFunction r = new FailingFunction();
2151 jsr166 1.6 CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor());
2152 dl 1.5 f.complete(one);
2153     checkCompletedWithWrappedCFException(g);
2154     }
2155 jsr166 1.6
2156 dl 1.5 /**
2157     * applyToEitherAsync result completes exceptionally if either source cancelled
2158     */
2159     public void testApplyToEitherAsync4E() {
2160     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2161     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2162     CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2163     assertTrue(f.cancel(true));
2164     checkCompletedWithWrappedCancellationException(g);
2165 jsr166 1.6
2166 dl 1.5 f = new CompletableFuture<Integer>();
2167     f2 = new CompletableFuture<Integer>();
2168     assertTrue(f2.cancel(true));
2169     g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2170     checkCompletedWithWrappedCancellationException(g);
2171     }
2172    
2173     /**
2174     * acceptEitherAsync result completes normally after normal
2175     * completion of sources
2176     */
2177     public void testAcceptEitherAsyncE() {
2178     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2179     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2180     IncAction r = new IncAction();
2181     CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2182     f.complete(one);
2183     checkCompletedNormally(g, null);
2184     assertEquals(r.value, 2);
2185    
2186     r = new IncAction();
2187     f = new CompletableFuture<Integer>();
2188     f.complete(one);
2189     f2 = new CompletableFuture<Integer>();
2190     g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2191     checkCompletedNormally(g, null);
2192     assertEquals(r.value, 2);
2193     }
2194    
2195     /**
2196     * acceptEitherAsync result completes exceptionally after exceptional
2197     * completion of source
2198     */
2199     public void testAcceptEitherAsync2E() {
2200     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2201     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2202     IncAction r = new IncAction();
2203     CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2204     f.completeExceptionally(new CFException());
2205     checkCompletedWithWrappedCFException(g);
2206    
2207     r = new IncAction();
2208     f = new CompletableFuture<Integer>();
2209     f2 = new CompletableFuture<Integer>();
2210     f2.completeExceptionally(new CFException());
2211     g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2212     f.complete(one);
2213     checkCompletedWithWrappedCFException(g);
2214     }
2215    
2216     /**
2217     * acceptEitherAsync result completes exceptionally if action does
2218     */
2219     public void testAcceptEitherAsync3E() {
2220     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2221     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2222     FailingConsumer r = new FailingConsumer();
2223 jsr166 1.6 CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2224 dl 1.5 f.complete(one);
2225     checkCompletedWithWrappedCFException(g);
2226     }
2227 jsr166 1.6
2228 dl 1.5 /**
2229     * acceptEitherAsync result completes exceptionally if either
2230     * source cancelled
2231     */
2232     public void testAcceptEitherAsync4E() {
2233     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2234     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2235     IncAction r = new IncAction();
2236     CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2237     assertTrue(f.cancel(true));
2238     checkCompletedWithWrappedCancellationException(g);
2239 jsr166 1.6
2240 dl 1.5 r = new IncAction();
2241     f = new CompletableFuture<Integer>();
2242     f2 = new CompletableFuture<Integer>();
2243     assertTrue(f2.cancel(true));
2244     g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2245     checkCompletedWithWrappedCancellationException(g);
2246     }
2247    
2248     /**
2249     * runAfterEitherAsync result completes normally after normal
2250     * completion of sources
2251     */
2252     public void testRunAfterEitherAsyncE() {
2253     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2254     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2255     Noop r = new Noop();
2256     CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2257     f.complete(one);
2258     checkCompletedNormally(g, null);
2259     assertTrue(r.ran);
2260    
2261     r = new Noop();
2262     f = new CompletableFuture<Integer>();
2263     f.complete(one);
2264     f2 = new CompletableFuture<Integer>();
2265     g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2266     checkCompletedNormally(g, null);
2267     assertTrue(r.ran);
2268     }
2269    
2270     /**
2271     * runAfterEitherAsync result completes exceptionally after exceptional
2272     * completion of source
2273     */
2274     public void testRunAfterEitherAsync2E() {
2275     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2276     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2277     Noop r = new Noop();
2278     CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2279     f.completeExceptionally(new CFException());
2280     checkCompletedWithWrappedCFException(g);
2281    
2282     r = new Noop();
2283     f = new CompletableFuture<Integer>();
2284     f2 = new CompletableFuture<Integer>();
2285     f2.completeExceptionally(new CFException());
2286     g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2287     f.complete(one);
2288     checkCompletedWithWrappedCFException(g);
2289     }
2290    
2291     /**
2292     * runAfterEitherAsync result completes exceptionally if action does
2293     */
2294     public void testRunAfterEitherAsync3E() {
2295     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2296     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2297     FailingNoop r = new FailingNoop();
2298 jsr166 1.6 CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2299 dl 1.5 f.complete(one);
2300     checkCompletedWithWrappedCFException(g);
2301     }
2302 jsr166 1.6
2303 dl 1.5 /**
2304     * runAfterEitherAsync result completes exceptionally if either
2305     * source cancelled
2306     */
2307     public void testRunAfterEitherAsync4E() {
2308     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2309     CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2310     Noop r = new Noop();
2311     CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2312     assertTrue(f.cancel(true));
2313     checkCompletedWithWrappedCancellationException(g);
2314 jsr166 1.6
2315 dl 1.5 r = new Noop();
2316     f = new CompletableFuture<Integer>();
2317     f2 = new CompletableFuture<Integer>();
2318     assertTrue(f2.cancel(true));
2319     g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2320     checkCompletedWithWrappedCancellationException(g);
2321     }
2322    
2323     /**
2324 jsr166 1.7 * thenComposeAsync result completes normally after normal
2325     * completion of source
2326 dl 1.5 */
2327     public void testThenComposeAsyncE() {
2328     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2329     CompletableFutureInc r = new CompletableFutureInc();
2330     CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2331     f.complete(one);
2332     checkCompletedNormally(g, two);
2333     }
2334    
2335     /**
2336 jsr166 1.7 * thenComposeAsync result completes exceptionally after
2337     * exceptional completion of source
2338 dl 1.5 */
2339     public void testThenComposeAsync2E() {
2340     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2341     CompletableFutureInc r = new CompletableFutureInc();
2342     CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2343     f.completeExceptionally(new CFException());
2344     checkCompletedWithWrappedCFException(g);
2345     }
2346    
2347     /**
2348     * thenComposeAsync result completes exceptionally if action does
2349     */
2350     public void testThenComposeAsync3E() {
2351     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2352     FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
2353     CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2354     f.complete(one);
2355     checkCompletedWithWrappedCFException(g);
2356     }
2357    
2358     /**
2359     * thenComposeAsync result completes exceptionally if source cancelled
2360     */
2361     public void testThenComposeAsync4E() {
2362     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2363     CompletableFutureInc r = new CompletableFutureInc();
2364     CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2365     assertTrue(f.cancel(true));
2366     checkCompletedWithWrappedCancellationException(g);
2367     }
2368    
2369 jsr166 1.6 // other static methods
2370 dl 1.5
2371     /**
2372     * allOf(no component futures) returns a future completed normally
2373     * with the value null
2374     */
2375     public void testAllOf_empty() throws Exception {
2376     CompletableFuture<?> f = CompletableFuture.allOf();
2377     checkCompletedNormally(f, null);
2378     }
2379    
2380     /**
2381     * allOf returns a future completed when all components complete
2382     */
2383     public void testAllOf() throws Exception {
2384     for (int k = 1; k < 20; ++k) {
2385     CompletableFuture[] fs = new CompletableFuture[k];
2386 jsr166 1.6 for (int i = 0; i < k; ++i)
2387 dl 1.5 fs[i] = new CompletableFuture<Integer>();
2388 dl 1.9 CompletableFuture<Void> f = CompletableFuture.allOf(fs);
2389 dl 1.5 for (int i = 0; i < k; ++i) {
2390     checkIncomplete(f);
2391     fs[i].complete(one);
2392     }
2393 dl 1.9 checkCompletedNormally(f, null);
2394 dl 1.5 }
2395     }
2396    
2397     /**
2398     * anyOf(no component futures) returns an incomplete future
2399     */
2400     public void testAnyOf_empty() throws Exception {
2401     CompletableFuture<?> f = CompletableFuture.anyOf();
2402     checkIncomplete(f);
2403     }
2404    
2405     /**
2406 jsr166 1.14 * anyOf returns a future completed when any components complete
2407 dl 1.5 */
2408     public void testAnyOf() throws Exception {
2409     for (int k = 1; k < 20; ++k) {
2410     CompletableFuture[] fs = new CompletableFuture[k];
2411 jsr166 1.6 for (int i = 0; i < k; ++i)
2412 dl 1.5 fs[i] = new CompletableFuture<Integer>();
2413 dl 1.9 CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
2414 dl 1.5 checkIncomplete(f);
2415     for (int i = 0; i < k; ++i) {
2416     fs[i].complete(one);
2417 dl 1.9 checkCompletedNormally(f, one);
2418 dl 1.5 }
2419     }
2420     }
2421    
2422     /**
2423     * Completion methods throw NullPointerException with null arguments
2424     */
2425     public void testNPE() {
2426     CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2427     CompletableFuture<Integer> g = new CompletableFuture<Integer>();
2428 jsr166 1.14 CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null;
2429     CompletableFuture<?> h;
2430 jsr166 1.17 ThreadExecutor exec = new ThreadExecutor();
2431 jsr166 1.14
2432     Runnable[] throwingActions = {
2433     () -> { CompletableFuture.supplyAsync(null); },
2434     () -> { CompletableFuture.supplyAsync(null, exec); },
2435 jsr166 1.16 () -> { CompletableFuture.supplyAsync(supplyOne, null); },
2436 jsr166 1.14
2437     () -> { CompletableFuture.runAsync(null); },
2438     () -> { CompletableFuture.runAsync(null, exec); },
2439     () -> { CompletableFuture.runAsync(() -> {}, null); },
2440    
2441     () -> { f.completeExceptionally(null); },
2442    
2443     () -> { f.thenApply(null); },
2444     () -> { f.thenApplyAsync(null); },
2445     () -> { f.thenApplyAsync((x) -> x, null); },
2446     () -> { f.thenApplyAsync(null, exec); },
2447    
2448     () -> { f.thenAccept(null); },
2449     () -> { f.thenAcceptAsync(null); },
2450     () -> { f.thenAcceptAsync((x) -> { ; }, null); },
2451     () -> { f.thenAcceptAsync(null, exec); },
2452    
2453     () -> { f.thenRun(null); },
2454     () -> { f.thenRunAsync(null); },
2455     () -> { f.thenRunAsync(() -> { ; }, null); },
2456     () -> { f.thenRunAsync(null, exec); },
2457    
2458     () -> { f.thenCombine(g, null); },
2459     () -> { f.thenCombineAsync(g, null); },
2460     () -> { f.thenCombineAsync(g, null, exec); },
2461     () -> { f.thenCombine(nullFuture, (x, y) -> x); },
2462     () -> { f.thenCombineAsync(nullFuture, (x, y) -> x); },
2463     () -> { f.thenCombineAsync(nullFuture, (x, y) -> x, exec); },
2464     () -> { f.thenCombineAsync(g, (x, y) -> x, null); },
2465    
2466     () -> { f.thenAcceptBoth(g, null); },
2467     () -> { f.thenAcceptBothAsync(g, null); },
2468     () -> { f.thenAcceptBothAsync(g, null, exec); },
2469     () -> { f.thenAcceptBoth(nullFuture, (x, y) -> {}); },
2470     () -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}); },
2471     () -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec); },
2472     () -> { f.thenAcceptBothAsync(g, (x, y) -> {}, null); },
2473    
2474     () -> { f.runAfterBoth(g, null); },
2475     () -> { f.runAfterBothAsync(g, null); },
2476     () -> { f.runAfterBothAsync(g, null, exec); },
2477     () -> { f.runAfterBoth(nullFuture, () -> {}); },
2478     () -> { f.runAfterBothAsync(nullFuture, () -> {}); },
2479     () -> { f.runAfterBothAsync(nullFuture, () -> {}, exec); },
2480     () -> { f.runAfterBothAsync(g, () -> {}, null); },
2481    
2482     () -> { f.applyToEither(g, null); },
2483     () -> { f.applyToEitherAsync(g, null); },
2484     () -> { f.applyToEitherAsync(g, null, exec); },
2485     () -> { f.applyToEither(nullFuture, (x) -> x); },
2486     () -> { f.applyToEitherAsync(nullFuture, (x) -> x); },
2487     () -> { f.applyToEitherAsync(nullFuture, (x) -> x, exec); },
2488     () -> { f.applyToEitherAsync(g, (x) -> x, null); },
2489    
2490     () -> { f.acceptEither(g, null); },
2491     () -> { f.acceptEitherAsync(g, null); },
2492     () -> { f.acceptEitherAsync(g, null, exec); },
2493     () -> { f.acceptEither(nullFuture, (x) -> {}); },
2494     () -> { f.acceptEitherAsync(nullFuture, (x) -> {}); },
2495     () -> { f.acceptEitherAsync(nullFuture, (x) -> {}, exec); },
2496     () -> { f.acceptEitherAsync(g, (x) -> {}, null); },
2497    
2498     () -> { f.runAfterEither(g, null); },
2499     () -> { f.runAfterEitherAsync(g, null); },
2500     () -> { f.runAfterEitherAsync(g, null, exec); },
2501     () -> { f.runAfterEither(nullFuture, () -> {}); },
2502     () -> { f.runAfterEitherAsync(nullFuture, () -> {}); },
2503     () -> { f.runAfterEitherAsync(nullFuture, () -> {}, exec); },
2504     () -> { f.runAfterEitherAsync(g, () -> {}, null); },
2505    
2506     () -> { f.thenCompose(null); },
2507     () -> { f.thenComposeAsync(null); },
2508     () -> { f.thenComposeAsync(new CompletableFutureInc(), null); },
2509     () -> { f.thenComposeAsync(null, exec); },
2510    
2511     () -> { f.exceptionally(null); },
2512    
2513     () -> { f.handle(null); },
2514    
2515     () -> { CompletableFuture.allOf((CompletableFuture<?>)null); },
2516     () -> { CompletableFuture.allOf((CompletableFuture<?>[])null); },
2517     () -> { CompletableFuture.allOf(f, null); },
2518     () -> { CompletableFuture.allOf(null, f); },
2519    
2520     () -> { CompletableFuture.anyOf((CompletableFuture<?>)null); },
2521     () -> { CompletableFuture.anyOf((CompletableFuture<?>[])null); },
2522     () -> { CompletableFuture.anyOf(f, null); },
2523     () -> { CompletableFuture.anyOf(null, f); },
2524     };
2525 dl 1.5
2526 jsr166 1.14 assertThrows(NullPointerException.class, throwingActions);
2527 jsr166 1.17 assertEquals(0, exec.count.get());
2528 dl 1.5 }
2529    
2530 jsr166 1.1 }