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