ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
Revision: 1.101
Committed: Sun Feb 22 04:34:44 2015 UTC (9 years, 2 months ago) by jsr166
Branch: MAIN
Changes since 1.100: +3 -6 lines
Log Message:
unused variable cleanup

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