ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
Revision: 1.57
Committed: Mon Jun 2 22:48:50 2014 UTC (9 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.56: +37 -21 lines
Log Message:
improve tests for runAsync

File Contents

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