ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
Revision: 1.76
Committed: Sat Jun 7 21:45:13 2014 UTC (9 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.75: +8 -3 lines
Log Message:
improve tests for getNumberOfDependents

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