ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
Revision: 1.60
Committed: Tue Jun 3 06:16:41 2014 UTC (9 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.59: +18 -16 lines
Log Message:
minor improvements

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