ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
Revision: 1.23
Committed: Tue Apr 9 07:50:55 2013 UTC (11 years, 1 month ago) by jsr166
Branch: MAIN
Changes since 1.22: +45 -15 lines
Log Message:
more tests for testThenRun

File Contents

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