ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
Revision: 1.72
Committed: Fri Jun 6 21:10:34 2014 UTC (9 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.71: +127 -92 lines
Log Message:
tighten assertions for exception handling

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