ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
Revision: 1.47
Committed: Mon Jun 2 18:21:34 2014 UTC (9 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.46: +267 -352 lines
Log Message:
use consistent stylized coding style for parameterized tests

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