ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
Revision: 1.59
Committed: Tue Jun 3 03:54:14 2014 UTC (9 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.58: +19 -1 lines
Log Message:
improve tests for allOf

File Contents

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