ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck-jsr166e/CompletableFutureTest.java
Revision: 1.2
Committed: Mon Jul 15 03:50:08 2013 UTC (10 years, 10 months ago) by jsr166
Branch: MAIN
Changes since 1.1: +2 -4 lines
Log Message:
catch (CompletionException success) {}

File Contents

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