ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
Revision: 1.43
Committed: Mon Jun 2 04:13:54 2014 UTC (9 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.42: +15 -67 lines
Log Message:
more test method refactoring

File Contents

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