ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
Revision: 1.96
Committed: Sat Nov 1 14:50:26 2014 UTC (9 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.95: +5 -2 lines
Log Message:
Fix assertions in presence of -Djava.util.concurrent.ForkJoinPool.common.parallelism=1

File Contents

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