ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
Revision: 1.74
Committed: Fri Jun 6 21:19:22 2014 UTC (9 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.73: +8 -0 lines
Log Message:
improve tests for toString

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