ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
Revision: 1.56
Committed: Mon Jun 2 22:20:32 2014 UTC (9 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.55: +86 -49 lines
Log Message:
test pervasively for correct execution environment

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