1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 2 along with this work; if not, write to the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 */ 22 23 /* 24 * This file is available under and governed by the GNU General Public 25 * License version 2 only, as published by the Free Software Foundation. 26 * However, the following notice accompanied the original version of this 27 * file: 28 * 29 * Written by Doug Lea and Martin Buchholz with assistance from 30 * members of JCP JSR-166 Expert Group and released to the public 31 * domain, as explained at 32 * http://creativecommons.org/publicdomain/zero/1.0/ 33 */ 34 35 package test.java.util.concurrent.tck; 36 37 import static java.util.concurrent.TimeUnit.MILLISECONDS; 38 import static java.util.concurrent.TimeUnit.SECONDS; 39 import static java.util.concurrent.CompletableFuture.completedFuture; 40 import static java.util.concurrent.CompletableFuture.failedFuture; 41 42 import java.lang.reflect.Method; 43 import java.lang.reflect.Modifier; 44 45 import java.util.stream.Collectors; 46 import java.util.stream.Stream; 47 48 import java.util.ArrayList; 49 import java.util.Arrays; 50 import java.util.List; 51 import java.util.Objects; 52 import java.util.Set; 53 import java.util.concurrent.Callable; 54 import java.util.concurrent.CancellationException; 55 import java.util.concurrent.CompletableFuture; 56 import java.util.concurrent.CompletionException; 57 import java.util.concurrent.CompletionStage; 58 import java.util.concurrent.ExecutionException; 59 import java.util.concurrent.Executor; 60 import java.util.concurrent.ForkJoinPool; 61 import java.util.concurrent.ForkJoinTask; 62 import java.util.concurrent.RejectedExecutionException; 63 import java.util.concurrent.TimeoutException; 64 import java.util.concurrent.atomic.AtomicInteger; 65 import java.util.concurrent.atomic.AtomicReference; 66 import java.util.function.BiConsumer; 67 import java.util.function.BiFunction; 68 import java.util.function.Consumer; 69 import java.util.function.Function; 70 import java.util.function.Predicate; 71 import java.util.function.Supplier; 72 73 import junit.framework.AssertionFailedError; 74 import junit.framework.Test; 75 import junit.framework.TestSuite; 76 77 public class CompletableFutureTest extends JSR166TestCase { 78 main(String[] args)79 public static void main(String[] args) { 80 main(suite(), args); 81 } suite()82 public static Test suite() { 83 return new TestSuite(CompletableFutureTest.class); 84 } 85 86 static class CFException extends RuntimeException {} 87 checkIncomplete(CompletableFuture<?> f)88 void checkIncomplete(CompletableFuture<?> f) { 89 assertFalse(f.isDone()); 90 assertFalse(f.isCancelled()); 91 assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]")); 92 93 Object result = null; 94 try { 95 result = f.getNow(null); 96 } catch (Throwable fail) { threadUnexpectedException(fail); } 97 assertNull(result); 98 99 try { 100 f.get(randomExpiredTimeout(), randomTimeUnit()); 101 shouldThrow(); 102 } 103 catch (TimeoutException success) {} 104 catch (Throwable fail) { threadUnexpectedException(fail); } 105 } 106 checkCompletedNormally(CompletableFuture<T> f, T expectedValue)107 <T> void checkCompletedNormally(CompletableFuture<T> f, T expectedValue) { 108 checkTimedGet(f, expectedValue); 109 110 mustEqual(expectedValue, f.join()); 111 mustEqual(expectedValue, f.getNow(null)); 112 113 T result = null; 114 try { 115 result = f.get(); 116 } catch (Throwable fail) { threadUnexpectedException(fail); } 117 mustEqual(expectedValue, result); 118 119 assertTrue(f.isDone()); 120 assertFalse(f.isCancelled()); 121 assertFalse(f.isCompletedExceptionally()); 122 assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]")); 123 } 124 125 /** 126 * Returns the "raw" internal exceptional completion of f, 127 * without any additional wrapping with CompletionException. 128 */ exceptionalCompletion(CompletableFuture<?> f)129 Throwable exceptionalCompletion(CompletableFuture<?> f) { 130 // handle (and whenComplete and exceptionally) can distinguish 131 // between "direct" and "wrapped" exceptional completion 132 return f.handle((u, t) -> t).join(); 133 } 134 checkCompletedExceptionally(CompletableFuture<?> f, boolean wrapped, Consumer<Throwable> checker)135 void checkCompletedExceptionally(CompletableFuture<?> f, 136 boolean wrapped, 137 Consumer<Throwable> checker) { 138 Throwable cause = exceptionalCompletion(f); 139 if (wrapped) { 140 assertTrue(cause instanceof CompletionException); 141 cause = cause.getCause(); 142 } 143 checker.accept(cause); 144 145 long startTime = System.nanoTime(); 146 try { 147 f.get(LONG_DELAY_MS, MILLISECONDS); 148 shouldThrow(); 149 } catch (ExecutionException success) { 150 assertSame(cause, success.getCause()); 151 } catch (Throwable fail) { threadUnexpectedException(fail); } 152 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); 153 154 try { 155 f.join(); 156 shouldThrow(); 157 } catch (CompletionException success) { 158 assertSame(cause, success.getCause()); 159 } catch (Throwable fail) { threadUnexpectedException(fail); } 160 161 try { 162 f.getNow(null); 163 shouldThrow(); 164 } catch (CompletionException success) { 165 assertSame(cause, success.getCause()); 166 } catch (Throwable fail) { threadUnexpectedException(fail); } 167 168 try { 169 f.get(); 170 shouldThrow(); 171 } catch (ExecutionException success) { 172 assertSame(cause, success.getCause()); 173 } catch (Throwable fail) { threadUnexpectedException(fail); } 174 175 assertFalse(f.isCancelled()); 176 assertTrue(f.isDone()); 177 assertTrue(f.isCompletedExceptionally()); 178 assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); 179 } 180 181 void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { 182 checkCompletedExceptionally(f, true, 183 t -> assertTrue(t instanceof CFException)); 184 } 185 checkCompletedWithWrappedCancellationException(CompletableFuture<?> f)186 void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { 187 checkCompletedExceptionally(f, true, 188 t -> assertTrue(t instanceof CancellationException)); 189 } 190 checkCompletedWithTimeoutException(CompletableFuture<?> f)191 void checkCompletedWithTimeoutException(CompletableFuture<?> f) { 192 checkCompletedExceptionally(f, false, 193 t -> assertTrue(t instanceof TimeoutException)); 194 } 195 checkCompletedWithWrappedException(CompletableFuture<?> f, Throwable ex)196 void checkCompletedWithWrappedException(CompletableFuture<?> f, 197 Throwable ex) { 198 checkCompletedExceptionally(f, true, t -> assertSame(t, ex)); 199 } 200 checkCompletedExceptionally(CompletableFuture<?> f, Throwable ex)201 void checkCompletedExceptionally(CompletableFuture<?> f, Throwable ex) { 202 checkCompletedExceptionally(f, false, t -> assertSame(t, ex)); 203 } 204 checkCancelled(CompletableFuture<?> f)205 void checkCancelled(CompletableFuture<?> f) { 206 long startTime = System.nanoTime(); 207 try { 208 f.get(LONG_DELAY_MS, MILLISECONDS); 209 shouldThrow(); 210 } catch (CancellationException success) { 211 } catch (Throwable fail) { threadUnexpectedException(fail); } 212 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); 213 214 try { 215 f.join(); 216 shouldThrow(); 217 } catch (CancellationException success) {} 218 try { 219 f.getNow(null); 220 shouldThrow(); 221 } catch (CancellationException success) {} 222 try { 223 f.get(); 224 shouldThrow(); 225 } catch (CancellationException success) { 226 } catch (Throwable fail) { threadUnexpectedException(fail); } 227 228 assertTrue(exceptionalCompletion(f) instanceof CancellationException); 229 230 assertTrue(f.isDone()); 231 assertTrue(f.isCompletedExceptionally()); 232 assertTrue(f.isCancelled()); 233 assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); 234 } 235 236 /** 237 * A newly constructed CompletableFuture is incomplete, as indicated 238 * by methods isDone, isCancelled, and getNow 239 */ 240 public void testConstructor() { 241 CompletableFuture<Item> f = new CompletableFuture<>(); 242 checkIncomplete(f); 243 } 244 245 /** 246 * complete completes normally, as indicated by methods isDone, 247 * isCancelled, join, get, and getNow 248 */ 249 public void testComplete() { 250 for (Item v1 : new Item[] { itemOne, null }) 251 { 252 CompletableFuture<Item> f = new CompletableFuture<>(); 253 checkIncomplete(f); 254 assertTrue(f.complete(v1)); 255 assertFalse(f.complete(v1)); 256 checkCompletedNormally(f, v1); 257 }} 258 259 /** 260 * completeExceptionally completes exceptionally, as indicated by 261 * methods isDone, isCancelled, join, get, and getNow 262 */ 263 public void testCompleteExceptionally() { 264 CompletableFuture<Item> f = new CompletableFuture<>(); 265 CFException ex = new CFException(); 266 checkIncomplete(f); 267 f.completeExceptionally(ex); 268 checkCompletedExceptionally(f, ex); 269 } 270 271 /** 272 * cancel completes exceptionally and reports cancelled, as indicated by 273 * methods isDone, isCancelled, join, get, and getNow 274 */ 275 public void testCancel() { 276 for (boolean mayInterruptIfRunning : new boolean[] { true, false }) 277 { 278 CompletableFuture<Item> f = new CompletableFuture<>(); 279 checkIncomplete(f); 280 assertTrue(f.cancel(mayInterruptIfRunning)); 281 assertTrue(f.cancel(mayInterruptIfRunning)); 282 assertTrue(f.cancel(!mayInterruptIfRunning)); 283 checkCancelled(f); 284 }} 285 286 /** 287 * obtrudeValue forces completion with given value 288 */ 289 public void testObtrudeValue() { 290 CompletableFuture<Item> f = new CompletableFuture<>(); 291 checkIncomplete(f); 292 assertTrue(f.complete(itemOne)); 293 checkCompletedNormally(f, itemOne); 294 f.obtrudeValue(itemThree); 295 checkCompletedNormally(f, itemThree); 296 f.obtrudeValue(itemTwo); 297 checkCompletedNormally(f, itemTwo); 298 f = new CompletableFuture<>(); 299 f.obtrudeValue(itemThree); 300 checkCompletedNormally(f, itemThree); 301 f.obtrudeValue(null); 302 checkCompletedNormally(f, null); 303 f = new CompletableFuture<>(); 304 f.completeExceptionally(new CFException()); 305 f.obtrudeValue(itemFour); 306 checkCompletedNormally(f, itemFour); 307 } 308 309 /** 310 * obtrudeException forces completion with given exception 311 */ 312 public void testObtrudeException() { 313 for (Item v1 : new Item[] { itemOne, null }) 314 { 315 CFException ex; 316 CompletableFuture<Item> f; 317 318 f = new CompletableFuture<>(); 319 assertTrue(f.complete(v1)); 320 for (int i = 0; i < 2; i++) { 321 f.obtrudeException(ex = new CFException()); 322 checkCompletedExceptionally(f, ex); 323 } 324 325 f = new CompletableFuture<>(); 326 for (int i = 0; i < 2; i++) { 327 f.obtrudeException(ex = new CFException()); 328 checkCompletedExceptionally(f, ex); 329 } 330 331 f = new CompletableFuture<>(); 332 f.completeExceptionally(new CFException()); 333 f.obtrudeValue(v1); 334 checkCompletedNormally(f, v1); 335 f.obtrudeException(ex = new CFException()); 336 checkCompletedExceptionally(f, ex); 337 f.completeExceptionally(new CFException()); 338 checkCompletedExceptionally(f, ex); 339 assertFalse(f.complete(v1)); 340 checkCompletedExceptionally(f, ex); 341 }} 342 343 /** 344 * getNumberOfDependents returns number of dependent tasks 345 */ 346 public void testGetNumberOfDependents() { 347 for (ExecutionMode m : ExecutionMode.values()) 348 for (Item v1 : new Item[] { itemOne, null }) 349 { 350 CompletableFuture<Item> f = new CompletableFuture<>(); 351 mustEqual(0, f.getNumberOfDependents()); 352 final CompletableFuture<Void> g = m.thenRun(f, new Noop(m)); 353 mustEqual(1, f.getNumberOfDependents()); 354 mustEqual(0, g.getNumberOfDependents()); 355 final CompletableFuture<Void> h = m.thenRun(f, new Noop(m)); 356 mustEqual(2, f.getNumberOfDependents()); 357 mustEqual(0, h.getNumberOfDependents()); 358 assertTrue(f.complete(v1)); 359 checkCompletedNormally(g, null); 360 checkCompletedNormally(h, null); 361 mustEqual(0, f.getNumberOfDependents()); 362 mustEqual(0, g.getNumberOfDependents()); 363 mustEqual(0, h.getNumberOfDependents()); 364 }} 365 366 /** 367 * toString indicates current completion state 368 */ 369 public void testToString_incomplete() { 370 CompletableFuture<String> f = new CompletableFuture<>(); 371 assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]")); 372 } 373 374 public void testToString_normal() { 375 CompletableFuture<String> f = new CompletableFuture<>(); 376 assertTrue(f.complete("foo")); 377 assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]")); 378 } 379 380 public void testToString_exception() { 381 CompletableFuture<String> f = new CompletableFuture<>(); 382 assertTrue(f.completeExceptionally(new IndexOutOfBoundsException())); 383 assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); 384 } 385 386 public void testToString_cancelled() { 387 for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { 388 CompletableFuture<String> f = new CompletableFuture<>(); 389 assertTrue(f.cancel(mayInterruptIfRunning)); 390 assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); 391 } 392 } 393 394 /** 395 * completedFuture returns a completed CompletableFuture with given value 396 */ 397 public void testCompletedFuture() { 398 CompletableFuture<String> f = CompletableFuture.completedFuture("test"); 399 checkCompletedNormally(f, "test"); 400 } 401 402 abstract static class CheckedAction { 403 int invocationCount = 0; 404 final ExecutionMode m; 405 CheckedAction(ExecutionMode m) { this.m = m; } 406 void invoked() { 407 m.checkExecutionMode(); 408 mustEqual(0, invocationCount++); 409 } 410 void assertNotInvoked() { mustEqual(0, invocationCount); } 411 void assertInvoked() { mustEqual(1, invocationCount); } 412 } 413 414 abstract static class CheckedItemAction extends CheckedAction { 415 Item value; 416 CheckedItemAction(ExecutionMode m) { super(m); } 417 void assertValue(Item expected) { 418 assertInvoked(); 419 mustEqual(expected, value); 420 } 421 } 422 423 static class ItemSupplier extends CheckedAction 424 implements Supplier<Item> 425 { 426 final Item value; 427 ItemSupplier(ExecutionMode m, Item value) { 428 super(m); 429 this.value = value; 430 } 431 public Item get() { 432 invoked(); 433 return value; 434 } 435 } 436 437 // A function that handles and produces null values as well. 438 static Item inc(Item x) { 439 return (x == null) ? null : new Item(x.value + 1); 440 } 441 442 static class NoopConsumer extends CheckedItemAction 443 implements Consumer<Item> 444 { 445 NoopConsumer(ExecutionMode m) { super(m); } 446 public void accept(Item x) { 447 invoked(); 448 value = x; 449 } 450 } 451 452 static class IncFunction extends CheckedItemAction 453 implements Function<Item,Item> 454 { 455 IncFunction(ExecutionMode m) { super(m); } 456 public Item apply(Item x) { 457 invoked(); 458 return value = inc(x); 459 } 460 } 461 462 // Choose non-commutative actions for better coverage 463 // A non-commutative function that handles and produces null values as well. 464 static Item subtract(Item x, Item y) { 465 return (x == null && y == null) ? null : 466 new Item(((x == null) ? 42 : x.value) 467 - ((y == null) ? 99 : y.value)); 468 } 469 470 static class SubtractAction extends CheckedItemAction 471 implements BiConsumer<Item, Item> 472 { 473 SubtractAction(ExecutionMode m) { super(m); } 474 public void accept(Item x, Item y) { 475 invoked(); 476 value = subtract(x, y); 477 } 478 } 479 480 static class SubtractFunction extends CheckedItemAction 481 implements BiFunction<Item, Item, Item> 482 { 483 SubtractFunction(ExecutionMode m) { super(m); } 484 public Item apply(Item x, Item y) { 485 invoked(); 486 return value = subtract(x, y); 487 } 488 } 489 490 static class Noop extends CheckedAction implements Runnable { 491 Noop(ExecutionMode m) { super(m); } 492 public void run() { 493 invoked(); 494 } 495 } 496 497 static class FailingSupplier extends CheckedAction 498 implements Supplier<Item> 499 { 500 final CFException ex; 501 FailingSupplier(ExecutionMode m) { super(m); ex = new CFException(); } 502 public Item get() { 503 invoked(); 504 throw ex; 505 } 506 } 507 508 static class FailingConsumer extends CheckedItemAction 509 implements Consumer<Item> 510 { 511 final CFException ex; 512 FailingConsumer(ExecutionMode m) { super(m); ex = new CFException(); } 513 public void accept(Item x) { 514 invoked(); 515 value = x; 516 throw ex; 517 } 518 } 519 520 static class FailingBiConsumer extends CheckedItemAction 521 implements BiConsumer<Item, Item> 522 { 523 final CFException ex; 524 FailingBiConsumer(ExecutionMode m) { super(m); ex = new CFException(); } 525 public void accept(Item x, Item y) { 526 invoked(); 527 value = subtract(x, y); 528 throw ex; 529 } 530 } 531 532 static class FailingFunction extends CheckedItemAction 533 implements Function<Item, Item> 534 { 535 final CFException ex; 536 FailingFunction(ExecutionMode m) { super(m); ex = new CFException(); } 537 public Item apply(Item x) { 538 invoked(); 539 value = x; 540 throw ex; 541 } 542 } 543 544 static class FailingBiFunction extends CheckedItemAction 545 implements BiFunction<Item, Item, Item> 546 { 547 final CFException ex; 548 FailingBiFunction(ExecutionMode m) { super(m); ex = new CFException(); } 549 public Item apply(Item x, Item y) { 550 invoked(); 551 value = subtract(x, y); 552 throw ex; 553 } 554 } 555 556 static class FailingRunnable extends CheckedAction implements Runnable { 557 final CFException ex; 558 FailingRunnable(ExecutionMode m) { super(m); ex = new CFException(); } 559 public void run() { 560 invoked(); 561 throw ex; 562 } 563 } 564 565 static class CompletableFutureInc extends CheckedItemAction 566 implements Function<Item, CompletableFuture<Item>> 567 { 568 CompletableFutureInc(ExecutionMode m) { super(m); } 569 public CompletableFuture<Item> apply(Item x) { 570 invoked(); 571 value = x; 572 return CompletableFuture.completedFuture(inc(x)); 573 } 574 } 575 576 static class FailingExceptionalCompletableFutureFunction extends CheckedAction 577 implements Function<Throwable, CompletableFuture<Item>> 578 { 579 final CFException ex; 580 FailingExceptionalCompletableFutureFunction(ExecutionMode m) { super(m); ex = new CFException(); } 581 public CompletableFuture<Item> apply(Throwable x) { 582 invoked(); 583 throw ex; 584 } 585 } 586 587 static class ExceptionalCompletableFutureFunction extends CheckedAction 588 implements Function<Throwable, CompletionStage<Item>> { 589 final Item value = itemThree; 590 ExceptionalCompletableFutureFunction(ExecutionMode m) { super(m); } 591 public CompletionStage<Item> apply(Throwable x) { 592 invoked(); 593 return CompletableFuture.completedFuture(value); 594 } 595 } 596 597 static class FailingCompletableFutureFunction extends CheckedItemAction 598 implements Function<Item, CompletableFuture<Item>> 599 { 600 final CFException ex; 601 FailingCompletableFutureFunction(ExecutionMode m) { super(m); ex = new CFException(); } 602 public CompletableFuture<Item> apply(Item x) { 603 invoked(); 604 value = x; 605 throw ex; 606 } 607 } 608 609 static class CountingRejectingExecutor implements Executor { 610 final RejectedExecutionException ex = new RejectedExecutionException(); 611 final AtomicInteger count = new AtomicInteger(0); 612 public void execute(Runnable r) { 613 count.getAndIncrement(); 614 throw ex; 615 } 616 } 617 618 // Used for explicit executor tests 619 static final class ThreadExecutor implements Executor { 620 final AtomicInteger count = new AtomicInteger(0); 621 static final ThreadGroup tg = new ThreadGroup("ThreadExecutor"); 622 static boolean startedCurrentThread() { 623 return Thread.currentThread().getThreadGroup() == tg; 624 } 625 626 public void execute(Runnable r) { 627 count.getAndIncrement(); 628 new Thread(tg, r).start(); 629 } 630 } 631 632 static final boolean defaultExecutorIsCommonPool 633 = ForkJoinPool.getCommonPoolParallelism() > 1; 634 635 /** 636 * Permits the testing of parallel code for the 3 different 637 * execution modes without copy/pasting all the test methods. 638 */ 639 enum ExecutionMode { 640 SYNC { 641 public void checkExecutionMode() { 642 assertFalse(ThreadExecutor.startedCurrentThread()); 643 assertNull(ForkJoinTask.getPool()); 644 } 645 public CompletableFuture<Void> runAsync(Runnable a) { 646 throw new UnsupportedOperationException(); 647 } 648 public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { 649 throw new UnsupportedOperationException(); 650 } 651 public <T> CompletableFuture<Void> thenRun 652 (CompletableFuture<T> f, Runnable a) { 653 return f.thenRun(a); 654 } 655 public <T> CompletableFuture<Void> thenAccept 656 (CompletableFuture<T> f, Consumer<? super T> a) { 657 return f.thenAccept(a); 658 } 659 public <T,U> CompletableFuture<U> thenApply 660 (CompletableFuture<T> f, Function<? super T,U> a) { 661 return f.thenApply(a); 662 } 663 public <T,U> CompletableFuture<U> thenCompose 664 (CompletableFuture<T> f, 665 Function<? super T,? extends CompletionStage<U>> a) { 666 return f.thenCompose(a); 667 } 668 public <T,U> CompletableFuture<U> handle 669 (CompletableFuture<T> f, 670 BiFunction<? super T,Throwable,? extends U> a) { 671 return f.handle(a); 672 } 673 public <T> CompletableFuture<T> whenComplete 674 (CompletableFuture<T> f, 675 BiConsumer<? super T,? super Throwable> a) { 676 return f.whenComplete(a); 677 } 678 public <T,U> CompletableFuture<Void> runAfterBoth 679 (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) { 680 return f.runAfterBoth(g, a); 681 } 682 public <T,U> CompletableFuture<Void> thenAcceptBoth 683 (CompletableFuture<T> f, 684 CompletionStage<? extends U> g, 685 BiConsumer<? super T,? super U> a) { 686 return f.thenAcceptBoth(g, a); 687 } 688 public <T,U,V> CompletableFuture<V> thenCombine 689 (CompletableFuture<T> f, 690 CompletionStage<? extends U> g, 691 BiFunction<? super T,? super U,? extends V> a) { 692 return f.thenCombine(g, a); 693 } 694 public <T> CompletableFuture<Void> runAfterEither 695 (CompletableFuture<T> f, 696 CompletionStage<?> g, 697 java.lang.Runnable a) { 698 return f.runAfterEither(g, a); 699 } 700 public <T> CompletableFuture<Void> acceptEither 701 (CompletableFuture<T> f, 702 CompletionStage<? extends T> g, 703 Consumer<? super T> a) { 704 return f.acceptEither(g, a); 705 } 706 public <T,U> CompletableFuture<U> applyToEither 707 (CompletableFuture<T> f, 708 CompletionStage<? extends T> g, 709 Function<? super T,U> a) { 710 return f.applyToEither(g, a); 711 } 712 public <T> CompletableFuture<T> exceptionally 713 (CompletableFuture<T> f, 714 Function<Throwable, ? extends T> fn) { 715 return f.exceptionally(fn); 716 } 717 public <T> CompletableFuture<T> exceptionallyCompose 718 (CompletableFuture<T> f, Function<Throwable, ? extends CompletionStage<T>> fn) { 719 return f.exceptionallyCompose(fn); 720 } 721 }, 722 ASYNC { 723 public void checkExecutionMode() { 724 mustEqual(defaultExecutorIsCommonPool, 725 (ForkJoinPool.commonPool() == ForkJoinTask.getPool())); 726 } 727 public CompletableFuture<Void> runAsync(Runnable a) { 728 return CompletableFuture.runAsync(a); 729 } 730 public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { 731 return CompletableFuture.supplyAsync(a); 732 } 733 public <T> CompletableFuture<Void> thenRun 734 (CompletableFuture<T> f, Runnable a) { 735 return f.thenRunAsync(a); 736 } 737 public <T> CompletableFuture<Void> thenAccept 738 (CompletableFuture<T> f, Consumer<? super T> a) { 739 return f.thenAcceptAsync(a); 740 } 741 public <T,U> CompletableFuture<U> thenApply 742 (CompletableFuture<T> f, Function<? super T,U> a) { 743 return f.thenApplyAsync(a); 744 } 745 public <T,U> CompletableFuture<U> thenCompose 746 (CompletableFuture<T> f, 747 Function<? super T,? extends CompletionStage<U>> a) { 748 return f.thenComposeAsync(a); 749 } 750 public <T,U> CompletableFuture<U> handle 751 (CompletableFuture<T> f, 752 BiFunction<? super T,Throwable,? extends U> a) { 753 return f.handleAsync(a); 754 } 755 public <T> CompletableFuture<T> whenComplete 756 (CompletableFuture<T> f, 757 BiConsumer<? super T,? super Throwable> a) { 758 return f.whenCompleteAsync(a); 759 } 760 public <T,U> CompletableFuture<Void> runAfterBoth 761 (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) { 762 return f.runAfterBothAsync(g, a); 763 } 764 public <T,U> CompletableFuture<Void> thenAcceptBoth 765 (CompletableFuture<T> f, 766 CompletionStage<? extends U> g, 767 BiConsumer<? super T,? super U> a) { 768 return f.thenAcceptBothAsync(g, a); 769 } 770 public <T,U,V> CompletableFuture<V> thenCombine 771 (CompletableFuture<T> f, 772 CompletionStage<? extends U> g, 773 BiFunction<? super T,? super U,? extends V> a) { 774 return f.thenCombineAsync(g, a); 775 } 776 public <T> CompletableFuture<Void> runAfterEither 777 (CompletableFuture<T> f, 778 CompletionStage<?> g, 779 java.lang.Runnable a) { 780 return f.runAfterEitherAsync(g, a); 781 } 782 public <T> CompletableFuture<Void> acceptEither 783 (CompletableFuture<T> f, 784 CompletionStage<? extends T> g, 785 Consumer<? super T> a) { 786 return f.acceptEitherAsync(g, a); 787 } 788 public <T,U> CompletableFuture<U> applyToEither 789 (CompletableFuture<T> f, 790 CompletionStage<? extends T> g, 791 Function<? super T,U> a) { 792 return f.applyToEitherAsync(g, a); 793 } 794 public <T> CompletableFuture<T> exceptionally 795 (CompletableFuture<T> f, 796 Function<Throwable, ? extends T> fn) { 797 return f.exceptionallyAsync(fn); 798 } 799 800 public <T> CompletableFuture<T> exceptionallyCompose 801 (CompletableFuture<T> f, Function<Throwable, ? extends CompletionStage<T>> fn) { 802 return f.exceptionallyComposeAsync(fn); 803 } 804 805 }, 806 807 EXECUTOR { 808 public void checkExecutionMode() { 809 assertTrue(ThreadExecutor.startedCurrentThread()); 810 } 811 public CompletableFuture<Void> runAsync(Runnable a) { 812 return CompletableFuture.runAsync(a, new ThreadExecutor()); 813 } 814 public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { 815 return CompletableFuture.supplyAsync(a, new ThreadExecutor()); 816 } 817 public <T> CompletableFuture<Void> thenRun 818 (CompletableFuture<T> f, Runnable a) { 819 return f.thenRunAsync(a, new ThreadExecutor()); 820 } 821 public <T> CompletableFuture<Void> thenAccept 822 (CompletableFuture<T> f, Consumer<? super T> a) { 823 return f.thenAcceptAsync(a, new ThreadExecutor()); 824 } 825 public <T,U> CompletableFuture<U> thenApply 826 (CompletableFuture<T> f, Function<? super T,U> a) { 827 return f.thenApplyAsync(a, new ThreadExecutor()); 828 } 829 public <T,U> CompletableFuture<U> thenCompose 830 (CompletableFuture<T> f, 831 Function<? super T,? extends CompletionStage<U>> a) { 832 return f.thenComposeAsync(a, new ThreadExecutor()); 833 } 834 public <T,U> CompletableFuture<U> handle 835 (CompletableFuture<T> f, 836 BiFunction<? super T,Throwable,? extends U> a) { 837 return f.handleAsync(a, new ThreadExecutor()); 838 } 839 public <T> CompletableFuture<T> whenComplete 840 (CompletableFuture<T> f, 841 BiConsumer<? super T,? super Throwable> a) { 842 return f.whenCompleteAsync(a, new ThreadExecutor()); 843 } 844 public <T,U> CompletableFuture<Void> runAfterBoth 845 (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) { 846 return f.runAfterBothAsync(g, a, new ThreadExecutor()); 847 } 848 public <T,U> CompletableFuture<Void> thenAcceptBoth 849 (CompletableFuture<T> f, 850 CompletionStage<? extends U> g, 851 BiConsumer<? super T,? super U> a) { 852 return f.thenAcceptBothAsync(g, a, new ThreadExecutor()); 853 } 854 public <T,U,V> CompletableFuture<V> thenCombine 855 (CompletableFuture<T> f, 856 CompletionStage<? extends U> g, 857 BiFunction<? super T,? super U,? extends V> a) { 858 return f.thenCombineAsync(g, a, new ThreadExecutor()); 859 } 860 public <T> CompletableFuture<Void> runAfterEither 861 (CompletableFuture<T> f, 862 CompletionStage<?> g, 863 java.lang.Runnable a) { 864 return f.runAfterEitherAsync(g, a, new ThreadExecutor()); 865 } 866 public <T> CompletableFuture<Void> acceptEither 867 (CompletableFuture<T> f, 868 CompletionStage<? extends T> g, 869 Consumer<? super T> a) { 870 return f.acceptEitherAsync(g, a, new ThreadExecutor()); 871 } 872 public <T,U> CompletableFuture<U> applyToEither 873 (CompletableFuture<T> f, 874 CompletionStage<? extends T> g, 875 Function<? super T,U> a) { 876 return f.applyToEitherAsync(g, a, new ThreadExecutor()); 877 } 878 public <T> CompletableFuture<T> exceptionally 879 (CompletableFuture<T> f, 880 Function<Throwable, ? extends T> fn) { 881 return f.exceptionallyAsync(fn, new ThreadExecutor()); 882 } 883 public <T> CompletableFuture<T> exceptionallyCompose 884 (CompletableFuture<T> f, Function<Throwable, ? extends CompletionStage<T>> fn) { 885 return f.exceptionallyComposeAsync(fn, new ThreadExecutor()); 886 } 887 888 }; 889 890 public abstract void checkExecutionMode(); 891 public abstract CompletableFuture<Void> runAsync(Runnable a); 892 public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a); 893 public abstract <T> CompletableFuture<Void> thenRun 894 (CompletableFuture<T> f, Runnable a); 895 public abstract <T> CompletableFuture<Void> thenAccept 896 (CompletableFuture<T> f, Consumer<? super T> a); 897 public abstract <T,U> CompletableFuture<U> thenApply 898 (CompletableFuture<T> f, Function<? super T,U> a); 899 public abstract <T,U> CompletableFuture<U> thenCompose 900 (CompletableFuture<T> f, 901 Function<? super T,? extends CompletionStage<U>> a); 902 public abstract <T,U> CompletableFuture<U> handle 903 (CompletableFuture<T> f, 904 BiFunction<? super T,Throwable,? extends U> a); 905 public abstract <T> CompletableFuture<T> whenComplete 906 (CompletableFuture<T> f, 907 BiConsumer<? super T,? super Throwable> a); 908 public abstract <T,U> CompletableFuture<Void> runAfterBoth 909 (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a); 910 public abstract <T,U> CompletableFuture<Void> thenAcceptBoth 911 (CompletableFuture<T> f, 912 CompletionStage<? extends U> g, 913 BiConsumer<? super T,? super U> a); 914 public abstract <T,U,V> CompletableFuture<V> thenCombine 915 (CompletableFuture<T> f, 916 CompletionStage<? extends U> g, 917 BiFunction<? super T,? super U,? extends V> a); 918 public abstract <T> CompletableFuture<Void> runAfterEither 919 (CompletableFuture<T> f, 920 CompletionStage<?> g, 921 java.lang.Runnable a); 922 public abstract <T> CompletableFuture<Void> acceptEither 923 (CompletableFuture<T> f, 924 CompletionStage<? extends T> g, 925 Consumer<? super T> a); 926 public abstract <T,U> CompletableFuture<U> applyToEither 927 (CompletableFuture<T> f, 928 CompletionStage<? extends T> g, 929 Function<? super T,U> a); 930 public abstract <T> CompletableFuture<T> exceptionally 931 (CompletableFuture<T> f, 932 Function<Throwable, ? extends T> fn); 933 public abstract <T> CompletableFuture<T> exceptionallyCompose 934 (CompletableFuture<T> f, 935 Function<Throwable, ? extends CompletionStage<T>> fn); 936 } 937 938 /** 939 * exceptionally action is not invoked when source completes 940 * normally, and source result is propagated 941 */ 942 public void testExceptionally_normalCompletion() { 943 for (ExecutionMode m : ExecutionMode.values()) 944 for (boolean createIncomplete : new boolean[] { true, false }) 945 for (Item v1 : new Item[] { itemOne, null }) 946 { 947 final AtomicInteger ran = new AtomicInteger(0); 948 final CompletableFuture<Item> f = new CompletableFuture<>(); 949 if (!createIncomplete) assertTrue(f.complete(v1)); 950 final CompletableFuture<Item> g = m.exceptionally 951 (f, (Throwable t) -> { 952 ran.getAndIncrement(); 953 throw new AssertionError("should not be called"); 954 }); 955 if (createIncomplete) assertTrue(f.complete(v1)); 956 957 checkCompletedNormally(g, v1); 958 checkCompletedNormally(f, v1); 959 mustEqual(0, ran.get()); 960 }} 961 962 /** 963 * exceptionally action completes with function value on source 964 * exception 965 */ 966 public void testExceptionally_exceptionalCompletion() { 967 for (ExecutionMode m : ExecutionMode.values()) 968 for (boolean createIncomplete : new boolean[] { true, false }) 969 for (Item v1 : new Item[] { itemOne, null }) 970 { 971 final AtomicInteger ran = new AtomicInteger(0); 972 final CFException ex = new CFException(); 973 final CompletableFuture<Item> f = new CompletableFuture<>(); 974 if (!createIncomplete) f.completeExceptionally(ex); 975 final CompletableFuture<Item> g = m.exceptionally 976 (f, (Throwable t) -> { 977 m.checkExecutionMode(); 978 assertSame(t, ex); 979 ran.getAndIncrement(); 980 return v1; 981 }); 982 if (createIncomplete) f.completeExceptionally(ex); 983 984 checkCompletedNormally(g, v1); 985 mustEqual(1, ran.get()); 986 }} 987 988 /** 989 * If an "exceptionally action" throws an exception, it completes 990 * exceptionally with that exception 991 */ testExceptionally_exceptionalCompletionActionFailed()992 public void testExceptionally_exceptionalCompletionActionFailed() { 993 for (ExecutionMode m : ExecutionMode.values()) 994 for (boolean createIncomplete : new boolean[] { true, false }) 995 { 996 final AtomicInteger ran = new AtomicInteger(0); 997 final CFException ex1 = new CFException(); 998 final CFException ex2 = new CFException(); 999 final CompletableFuture<Item> f = new CompletableFuture<>(); 1000 if (!createIncomplete) f.completeExceptionally(ex1); 1001 final CompletableFuture<Item> g = m.exceptionally 1002 (f, (Throwable t) -> { 1003 m.checkExecutionMode(); 1004 assertSame(t, ex1); 1005 ran.getAndIncrement(); 1006 throw ex2; 1007 }); 1008 if (createIncomplete) f.completeExceptionally(ex1); 1009 1010 checkCompletedWithWrappedException(g, ex2); 1011 checkCompletedExceptionally(f, ex1); 1012 mustEqual(1, ran.get()); 1013 }} 1014 1015 /** 1016 * whenComplete action executes on normal completion, propagating 1017 * source result. 1018 */ testWhenComplete_normalCompletion()1019 public void testWhenComplete_normalCompletion() { 1020 for (ExecutionMode m : ExecutionMode.values()) 1021 for (boolean createIncomplete : new boolean[] { true, false }) 1022 for (Item v1 : new Item[] { itemOne, null }) 1023 { 1024 final AtomicInteger ran = new AtomicInteger(0); 1025 final CompletableFuture<Item> f = new CompletableFuture<>(); 1026 if (!createIncomplete) assertTrue(f.complete(v1)); 1027 final CompletableFuture<Item> g = m.whenComplete 1028 (f, 1029 (Item result, Throwable t) -> { 1030 m.checkExecutionMode(); 1031 assertSame(result, v1); 1032 assertNull(t); 1033 ran.getAndIncrement(); 1034 }); 1035 if (createIncomplete) assertTrue(f.complete(v1)); 1036 1037 checkCompletedNormally(g, v1); 1038 checkCompletedNormally(f, v1); 1039 mustEqual(1, ran.get()); 1040 }} 1041 1042 /** 1043 * whenComplete action executes on exceptional completion, propagating 1044 * source result. 1045 */ testWhenComplete_exceptionalCompletion()1046 public void testWhenComplete_exceptionalCompletion() { 1047 for (ExecutionMode m : ExecutionMode.values()) 1048 for (boolean createIncomplete : new boolean[] { true, false }) 1049 { 1050 final AtomicInteger ran = new AtomicInteger(0); 1051 final CFException ex = new CFException(); 1052 final CompletableFuture<Item> f = new CompletableFuture<>(); 1053 if (!createIncomplete) f.completeExceptionally(ex); 1054 final CompletableFuture<Item> g = m.whenComplete 1055 (f, 1056 (Item result, Throwable t) -> { 1057 m.checkExecutionMode(); 1058 assertNull(result); 1059 assertSame(t, ex); 1060 ran.getAndIncrement(); 1061 }); 1062 if (createIncomplete) f.completeExceptionally(ex); 1063 1064 checkCompletedWithWrappedException(g, ex); 1065 checkCompletedExceptionally(f, ex); 1066 mustEqual(1, ran.get()); 1067 }} 1068 1069 /** 1070 * whenComplete action executes on cancelled source, propagating 1071 * CancellationException. 1072 */ testWhenComplete_sourceCancelled()1073 public void testWhenComplete_sourceCancelled() { 1074 for (ExecutionMode m : ExecutionMode.values()) 1075 for (boolean mayInterruptIfRunning : new boolean[] { true, false }) 1076 for (boolean createIncomplete : new boolean[] { true, false }) 1077 { 1078 final AtomicInteger ran = new AtomicInteger(0); 1079 final CompletableFuture<Item> f = new CompletableFuture<>(); 1080 if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); 1081 final CompletableFuture<Item> g = m.whenComplete 1082 (f, 1083 (Item result, Throwable t) -> { 1084 m.checkExecutionMode(); 1085 assertNull(result); 1086 assertTrue(t instanceof CancellationException); 1087 ran.getAndIncrement(); 1088 }); 1089 if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); 1090 1091 checkCompletedWithWrappedCancellationException(g); 1092 checkCancelled(f); 1093 mustEqual(1, ran.get()); 1094 }} 1095 1096 /** 1097 * If a whenComplete action throws an exception when triggered by 1098 * a normal completion, it completes exceptionally 1099 */ testWhenComplete_sourceCompletedNormallyActionFailed()1100 public void testWhenComplete_sourceCompletedNormallyActionFailed() { 1101 for (boolean createIncomplete : new boolean[] { true, false }) 1102 for (ExecutionMode m : ExecutionMode.values()) 1103 for (Item v1 : new Item[] { itemOne, null }) 1104 { 1105 final AtomicInteger ran = new AtomicInteger(0); 1106 final CFException ex = new CFException(); 1107 final CompletableFuture<Item> f = new CompletableFuture<>(); 1108 if (!createIncomplete) assertTrue(f.complete(v1)); 1109 final CompletableFuture<Item> g = m.whenComplete 1110 (f, 1111 (Item result, Throwable t) -> { 1112 m.checkExecutionMode(); 1113 assertSame(result, v1); 1114 assertNull(t); 1115 ran.getAndIncrement(); 1116 throw ex; 1117 }); 1118 if (createIncomplete) assertTrue(f.complete(v1)); 1119 1120 checkCompletedWithWrappedException(g, ex); 1121 checkCompletedNormally(f, v1); 1122 mustEqual(1, ran.get()); 1123 }} 1124 1125 /** 1126 * If a whenComplete action throws an exception when triggered by 1127 * a source completion that also throws an exception, the source 1128 * exception takes precedence (unlike handle) 1129 */ testWhenComplete_sourceFailedActionFailed()1130 public void testWhenComplete_sourceFailedActionFailed() { 1131 for (boolean createIncomplete : new boolean[] { true, false }) 1132 for (ExecutionMode m : ExecutionMode.values()) 1133 { 1134 final AtomicInteger ran = new AtomicInteger(0); 1135 final CFException ex1 = new CFException(); 1136 final CFException ex2 = new CFException(); 1137 final CompletableFuture<Item> f = new CompletableFuture<>(); 1138 1139 if (!createIncomplete) f.completeExceptionally(ex1); 1140 final CompletableFuture<Item> g = m.whenComplete 1141 (f, 1142 (Item result, Throwable t) -> { 1143 m.checkExecutionMode(); 1144 assertSame(t, ex1); 1145 assertNull(result); 1146 ran.getAndIncrement(); 1147 throw ex2; 1148 }); 1149 if (createIncomplete) f.completeExceptionally(ex1); 1150 1151 checkCompletedWithWrappedException(g, ex1); 1152 checkCompletedExceptionally(f, ex1); 1153 if (testImplementationDetails) { 1154 mustEqual(1, ex1.getSuppressed().length); 1155 assertSame(ex2, ex1.getSuppressed()[0]); 1156 } 1157 mustEqual(1, ran.get()); 1158 }} 1159 1160 /** 1161 * handle action completes normally with function value on normal 1162 * completion of source 1163 */ testHandle_normalCompletion()1164 public void testHandle_normalCompletion() { 1165 for (ExecutionMode m : ExecutionMode.values()) 1166 for (boolean createIncomplete : new boolean[] { true, false }) 1167 for (Item v1 : new Item[] { itemOne, null }) 1168 { 1169 final CompletableFuture<Item> f = new CompletableFuture<>(); 1170 final AtomicInteger ran = new AtomicInteger(0); 1171 if (!createIncomplete) assertTrue(f.complete(v1)); 1172 final CompletableFuture<Item> g = m.handle 1173 (f, 1174 (Item result, Throwable t) -> { 1175 m.checkExecutionMode(); 1176 assertSame(result, v1); 1177 assertNull(t); 1178 ran.getAndIncrement(); 1179 return inc(v1); 1180 }); 1181 if (createIncomplete) assertTrue(f.complete(v1)); 1182 1183 checkCompletedNormally(g, inc(v1)); 1184 checkCompletedNormally(f, v1); 1185 mustEqual(1, ran.get()); 1186 }} 1187 1188 /** 1189 * handle action completes normally with function value on 1190 * exceptional completion of source 1191 */ testHandle_exceptionalCompletion()1192 public void testHandle_exceptionalCompletion() { 1193 for (ExecutionMode m : ExecutionMode.values()) 1194 for (boolean createIncomplete : new boolean[] { true, false }) 1195 for (Item v1 : new Item[] { itemOne, null }) 1196 { 1197 final CompletableFuture<Item> f = new CompletableFuture<>(); 1198 final AtomicInteger ran = new AtomicInteger(0); 1199 final CFException ex = new CFException(); 1200 if (!createIncomplete) f.completeExceptionally(ex); 1201 final CompletableFuture<Item> g = m.handle 1202 (f, 1203 (Item result, Throwable t) -> { 1204 m.checkExecutionMode(); 1205 assertNull(result); 1206 assertSame(t, ex); 1207 ran.getAndIncrement(); 1208 return v1; 1209 }); 1210 if (createIncomplete) f.completeExceptionally(ex); 1211 1212 checkCompletedNormally(g, v1); 1213 checkCompletedExceptionally(f, ex); 1214 mustEqual(1, ran.get()); 1215 }} 1216 1217 /** 1218 * handle action completes normally with function value on 1219 * cancelled source 1220 */ testHandle_sourceCancelled()1221 public void testHandle_sourceCancelled() { 1222 for (ExecutionMode m : ExecutionMode.values()) 1223 for (boolean mayInterruptIfRunning : new boolean[] { true, false }) 1224 for (boolean createIncomplete : new boolean[] { true, false }) 1225 for (Item v1 : new Item[] { itemOne, null }) 1226 { 1227 final CompletableFuture<Item> f = new CompletableFuture<>(); 1228 final AtomicInteger ran = new AtomicInteger(0); 1229 if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); 1230 final CompletableFuture<Item> g = m.handle 1231 (f, 1232 (Item result, Throwable t) -> { 1233 m.checkExecutionMode(); 1234 assertNull(result); 1235 assertTrue(t instanceof CancellationException); 1236 ran.getAndIncrement(); 1237 return v1; 1238 }); 1239 if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); 1240 1241 checkCompletedNormally(g, v1); 1242 checkCancelled(f); 1243 mustEqual(1, ran.get()); 1244 }} 1245 1246 /** 1247 * If a "handle action" throws an exception when triggered by 1248 * a normal completion, it completes exceptionally 1249 */ testHandle_sourceCompletedNormallyActionFailed()1250 public void testHandle_sourceCompletedNormallyActionFailed() { 1251 for (ExecutionMode m : ExecutionMode.values()) 1252 for (boolean createIncomplete : new boolean[] { true, false }) 1253 for (Item v1 : new Item[] { itemOne, null }) 1254 { 1255 final CompletableFuture<Item> f = new CompletableFuture<>(); 1256 final AtomicInteger ran = new AtomicInteger(0); 1257 final CFException ex = new CFException(); 1258 if (!createIncomplete) assertTrue(f.complete(v1)); 1259 final CompletableFuture<Item> g = m.handle 1260 (f, 1261 (Item result, Throwable t) -> { 1262 m.checkExecutionMode(); 1263 assertSame(result, v1); 1264 assertNull(t); 1265 ran.getAndIncrement(); 1266 throw ex; 1267 }); 1268 if (createIncomplete) assertTrue(f.complete(v1)); 1269 1270 checkCompletedWithWrappedException(g, ex); 1271 checkCompletedNormally(f, v1); 1272 mustEqual(1, ran.get()); 1273 }} 1274 1275 /** 1276 * If a "handle action" throws an exception when triggered by 1277 * a source completion that also throws an exception, the action 1278 * exception takes precedence (unlike whenComplete) 1279 */ testHandle_sourceFailedActionFailed()1280 public void testHandle_sourceFailedActionFailed() { 1281 for (boolean createIncomplete : new boolean[] { true, false }) 1282 for (ExecutionMode m : ExecutionMode.values()) 1283 { 1284 final AtomicInteger ran = new AtomicInteger(0); 1285 final CFException ex1 = new CFException(); 1286 final CFException ex2 = new CFException(); 1287 final CompletableFuture<Item> f = new CompletableFuture<>(); 1288 1289 if (!createIncomplete) f.completeExceptionally(ex1); 1290 final CompletableFuture<Item> g = m.handle 1291 (f, 1292 (Item result, Throwable t) -> { 1293 m.checkExecutionMode(); 1294 assertNull(result); 1295 assertSame(ex1, t); 1296 ran.getAndIncrement(); 1297 throw ex2; 1298 }); 1299 if (createIncomplete) f.completeExceptionally(ex1); 1300 1301 checkCompletedWithWrappedException(g, ex2); 1302 checkCompletedExceptionally(f, ex1); 1303 mustEqual(1, ran.get()); 1304 }} 1305 1306 /** 1307 * runAsync completes after running Runnable 1308 */ testRunAsync_normalCompletion()1309 public void testRunAsync_normalCompletion() { 1310 ExecutionMode[] executionModes = { 1311 ExecutionMode.ASYNC, 1312 ExecutionMode.EXECUTOR, 1313 }; 1314 for (ExecutionMode m : executionModes) 1315 { 1316 final Noop r = new Noop(m); 1317 final CompletableFuture<Void> f = m.runAsync(r); 1318 assertNull(f.join()); 1319 checkCompletedNormally(f, null); 1320 r.assertInvoked(); 1321 }} 1322 1323 /** 1324 * failing runAsync completes exceptionally after running Runnable 1325 */ testRunAsync_exceptionalCompletion()1326 public void testRunAsync_exceptionalCompletion() { 1327 ExecutionMode[] executionModes = { 1328 ExecutionMode.ASYNC, 1329 ExecutionMode.EXECUTOR, 1330 }; 1331 for (ExecutionMode m : executionModes) 1332 { 1333 final FailingRunnable r = new FailingRunnable(m); 1334 final CompletableFuture<Void> f = m.runAsync(r); 1335 checkCompletedWithWrappedException(f, r.ex); 1336 r.assertInvoked(); 1337 }} 1338 1339 @SuppressWarnings("FutureReturnValueIgnored") testRunAsync_rejectingExecutor()1340 public void testRunAsync_rejectingExecutor() { 1341 CountingRejectingExecutor e = new CountingRejectingExecutor(); 1342 try { 1343 CompletableFuture.runAsync(() -> {}, e); 1344 shouldThrow(); 1345 } catch (Throwable t) { 1346 assertSame(e.ex, t); 1347 } 1348 1349 mustEqual(1, e.count.get()); 1350 } 1351 1352 /** 1353 * supplyAsync completes with result of supplier 1354 */ testSupplyAsync_normalCompletion()1355 public void testSupplyAsync_normalCompletion() { 1356 ExecutionMode[] executionModes = { 1357 ExecutionMode.ASYNC, 1358 ExecutionMode.EXECUTOR, 1359 }; 1360 for (ExecutionMode m : executionModes) 1361 for (Item v1 : new Item[] { itemOne, null }) 1362 { 1363 final ItemSupplier r = new ItemSupplier(m, v1); 1364 final CompletableFuture<Item> f = m.supplyAsync(r); 1365 assertSame(v1, f.join()); 1366 checkCompletedNormally(f, v1); 1367 r.assertInvoked(); 1368 }} 1369 1370 /** 1371 * Failing supplyAsync completes exceptionally 1372 */ testSupplyAsync_exceptionalCompletion()1373 public void testSupplyAsync_exceptionalCompletion() { 1374 ExecutionMode[] executionModes = { 1375 ExecutionMode.ASYNC, 1376 ExecutionMode.EXECUTOR, 1377 }; 1378 for (ExecutionMode m : executionModes) 1379 { 1380 FailingSupplier r = new FailingSupplier(m); 1381 CompletableFuture<Item> f = m.supplyAsync(r); 1382 checkCompletedWithWrappedException(f, r.ex); 1383 r.assertInvoked(); 1384 }} 1385 1386 @SuppressWarnings("FutureReturnValueIgnored") testSupplyAsync_rejectingExecutor()1387 public void testSupplyAsync_rejectingExecutor() { 1388 CountingRejectingExecutor e = new CountingRejectingExecutor(); 1389 try { 1390 CompletableFuture.supplyAsync(() -> null, e); 1391 shouldThrow(); 1392 } catch (Throwable t) { 1393 assertSame(e.ex, t); 1394 } 1395 1396 mustEqual(1, e.count.get()); 1397 } 1398 1399 // seq completion methods 1400 1401 /** 1402 * thenRun result completes normally after normal completion of source 1403 */ testThenRun_normalCompletion()1404 public void testThenRun_normalCompletion() { 1405 for (ExecutionMode m : ExecutionMode.values()) 1406 for (Item v1 : new Item[] { itemOne, null }) 1407 { 1408 final CompletableFuture<Item> f = new CompletableFuture<>(); 1409 final Noop[] rs = new Noop[6]; 1410 for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); 1411 1412 final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); 1413 final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); 1414 final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); 1415 checkIncomplete(h0); 1416 checkIncomplete(h1); 1417 checkIncomplete(h2); 1418 assertTrue(f.complete(v1)); 1419 final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); 1420 final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); 1421 final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); 1422 1423 checkCompletedNormally(h0, null); 1424 checkCompletedNormally(h1, null); 1425 checkCompletedNormally(h2, null); 1426 checkCompletedNormally(h3, null); 1427 checkCompletedNormally(h4, null); 1428 checkCompletedNormally(h5, null); 1429 checkCompletedNormally(f, v1); 1430 for (Noop r : rs) r.assertInvoked(); 1431 }} 1432 1433 /** 1434 * thenRun result completes exceptionally after exceptional 1435 * completion of source 1436 */ testThenRun_exceptionalCompletion()1437 public void testThenRun_exceptionalCompletion() { 1438 for (ExecutionMode m : ExecutionMode.values()) 1439 { 1440 final CFException ex = new CFException(); 1441 final CompletableFuture<Item> f = new CompletableFuture<>(); 1442 final Noop[] rs = new Noop[6]; 1443 for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); 1444 1445 final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); 1446 final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); 1447 final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); 1448 checkIncomplete(h0); 1449 checkIncomplete(h1); 1450 checkIncomplete(h2); 1451 assertTrue(f.completeExceptionally(ex)); 1452 final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); 1453 final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); 1454 final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); 1455 1456 checkCompletedWithWrappedException(h0, ex); 1457 checkCompletedWithWrappedException(h1, ex); 1458 checkCompletedWithWrappedException(h2, ex); 1459 checkCompletedWithWrappedException(h3, ex); 1460 checkCompletedWithWrappedException(h4, ex); 1461 checkCompletedWithWrappedException(h5, ex); 1462 checkCompletedExceptionally(f, ex); 1463 for (Noop r : rs) r.assertNotInvoked(); 1464 }} 1465 1466 /** 1467 * thenRun result completes exceptionally if source cancelled 1468 */ testThenRun_sourceCancelled()1469 public void testThenRun_sourceCancelled() { 1470 for (ExecutionMode m : ExecutionMode.values()) 1471 for (boolean mayInterruptIfRunning : new boolean[] { true, false }) 1472 { 1473 final CompletableFuture<Item> f = new CompletableFuture<>(); 1474 final Noop[] rs = new Noop[6]; 1475 for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); 1476 1477 final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); 1478 final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); 1479 final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); 1480 checkIncomplete(h0); 1481 checkIncomplete(h1); 1482 checkIncomplete(h2); 1483 assertTrue(f.cancel(mayInterruptIfRunning)); 1484 final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); 1485 final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); 1486 final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); 1487 1488 checkCompletedWithWrappedCancellationException(h0); 1489 checkCompletedWithWrappedCancellationException(h1); 1490 checkCompletedWithWrappedCancellationException(h2); 1491 checkCompletedWithWrappedCancellationException(h3); 1492 checkCompletedWithWrappedCancellationException(h4); 1493 checkCompletedWithWrappedCancellationException(h5); 1494 checkCancelled(f); 1495 for (Noop r : rs) r.assertNotInvoked(); 1496 }} 1497 1498 /** 1499 * thenRun result completes exceptionally if action does 1500 */ testThenRun_actionFailed()1501 public void testThenRun_actionFailed() { 1502 for (ExecutionMode m : ExecutionMode.values()) 1503 for (Item v1 : new Item[] { itemOne, null }) 1504 { 1505 final CompletableFuture<Item> f = new CompletableFuture<>(); 1506 final FailingRunnable[] rs = new FailingRunnable[6]; 1507 for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m); 1508 1509 final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); 1510 final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); 1511 final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); 1512 assertTrue(f.complete(v1)); 1513 final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); 1514 final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); 1515 final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); 1516 1517 checkCompletedWithWrappedException(h0, rs[0].ex); 1518 checkCompletedWithWrappedException(h1, rs[1].ex); 1519 checkCompletedWithWrappedException(h2, rs[2].ex); 1520 checkCompletedWithWrappedException(h3, rs[3].ex); 1521 checkCompletedWithWrappedException(h4, rs[4].ex); 1522 checkCompletedWithWrappedException(h5, rs[5].ex); 1523 checkCompletedNormally(f, v1); 1524 }} 1525 1526 /** 1527 * thenApply result completes normally after normal completion of source 1528 */ testThenApply_normalCompletion()1529 public void testThenApply_normalCompletion() { 1530 for (ExecutionMode m : ExecutionMode.values()) 1531 for (Item v1 : new Item[] { itemOne, null }) 1532 { 1533 final CompletableFuture<Item> f = new CompletableFuture<>(); 1534 final IncFunction[] rs = new IncFunction[4]; 1535 for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); 1536 1537 final CompletableFuture<Item> h0 = m.thenApply(f, rs[0]); 1538 final CompletableFuture<Item> h1 = m.applyToEither(f, f, rs[1]); 1539 checkIncomplete(h0); 1540 checkIncomplete(h1); 1541 assertTrue(f.complete(v1)); 1542 final CompletableFuture<Item> h2 = m.thenApply(f, rs[2]); 1543 final CompletableFuture<Item> h3 = m.applyToEither(f, f, rs[3]); 1544 1545 checkCompletedNormally(h0, inc(v1)); 1546 checkCompletedNormally(h1, inc(v1)); 1547 checkCompletedNormally(h2, inc(v1)); 1548 checkCompletedNormally(h3, inc(v1)); 1549 checkCompletedNormally(f, v1); 1550 for (IncFunction r : rs) r.assertValue(inc(v1)); 1551 }} 1552 1553 /** 1554 * thenApply result completes exceptionally after exceptional 1555 * completion of source 1556 */ testThenApply_exceptionalCompletion()1557 public void testThenApply_exceptionalCompletion() { 1558 for (ExecutionMode m : ExecutionMode.values()) 1559 { 1560 final CFException ex = new CFException(); 1561 final CompletableFuture<Item> f = new CompletableFuture<>(); 1562 final IncFunction[] rs = new IncFunction[4]; 1563 for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); 1564 1565 final CompletableFuture<Item> h0 = m.thenApply(f, rs[0]); 1566 final CompletableFuture<Item> h1 = m.applyToEither(f, f, rs[1]); 1567 assertTrue(f.completeExceptionally(ex)); 1568 final CompletableFuture<Item> h2 = m.thenApply(f, rs[2]); 1569 final CompletableFuture<Item> h3 = m.applyToEither(f, f, rs[3]); 1570 1571 checkCompletedWithWrappedException(h0, ex); 1572 checkCompletedWithWrappedException(h1, ex); 1573 checkCompletedWithWrappedException(h2, ex); 1574 checkCompletedWithWrappedException(h3, ex); 1575 checkCompletedExceptionally(f, ex); 1576 for (IncFunction r : rs) r.assertNotInvoked(); 1577 }} 1578 1579 /** 1580 * thenApply result completes exceptionally if source cancelled 1581 */ testThenApply_sourceCancelled()1582 public void testThenApply_sourceCancelled() { 1583 for (ExecutionMode m : ExecutionMode.values()) 1584 for (boolean mayInterruptIfRunning : new boolean[] { true, false }) 1585 { 1586 final CompletableFuture<Item> f = new CompletableFuture<>(); 1587 final IncFunction[] rs = new IncFunction[4]; 1588 for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); 1589 1590 final CompletableFuture<Item> h0 = m.thenApply(f, rs[0]); 1591 final CompletableFuture<Item> h1 = m.applyToEither(f, f, rs[1]); 1592 assertTrue(f.cancel(mayInterruptIfRunning)); 1593 final CompletableFuture<Item> h2 = m.thenApply(f, rs[2]); 1594 final CompletableFuture<Item> h3 = m.applyToEither(f, f, rs[3]); 1595 1596 checkCompletedWithWrappedCancellationException(h0); 1597 checkCompletedWithWrappedCancellationException(h1); 1598 checkCompletedWithWrappedCancellationException(h2); 1599 checkCompletedWithWrappedCancellationException(h3); 1600 checkCancelled(f); 1601 for (IncFunction r : rs) r.assertNotInvoked(); 1602 }} 1603 1604 /** 1605 * thenApply result completes exceptionally if action does 1606 */ testThenApply_actionFailed()1607 public void testThenApply_actionFailed() { 1608 for (ExecutionMode m : ExecutionMode.values()) 1609 for (Item v1 : new Item[] { itemOne, null }) 1610 { 1611 final CompletableFuture<Item> f = new CompletableFuture<>(); 1612 final FailingFunction[] rs = new FailingFunction[4]; 1613 for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m); 1614 1615 final CompletableFuture<Item> h0 = m.thenApply(f, rs[0]); 1616 final CompletableFuture<Item> h1 = m.applyToEither(f, f, rs[1]); 1617 assertTrue(f.complete(v1)); 1618 final CompletableFuture<Item> h2 = m.thenApply(f, rs[2]); 1619 final CompletableFuture<Item> h3 = m.applyToEither(f, f, rs[3]); 1620 1621 checkCompletedWithWrappedException(h0, rs[0].ex); 1622 checkCompletedWithWrappedException(h1, rs[1].ex); 1623 checkCompletedWithWrappedException(h2, rs[2].ex); 1624 checkCompletedWithWrappedException(h3, rs[3].ex); 1625 checkCompletedNormally(f, v1); 1626 }} 1627 1628 /** 1629 * thenAccept result completes normally after normal completion of source 1630 */ testThenAccept_normalCompletion()1631 public void testThenAccept_normalCompletion() { 1632 for (ExecutionMode m : ExecutionMode.values()) 1633 for (Item v1 : new Item[] { itemOne, null }) 1634 { 1635 final CompletableFuture<Item> f = new CompletableFuture<>(); 1636 final NoopConsumer[] rs = new NoopConsumer[4]; 1637 for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); 1638 1639 final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); 1640 final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); 1641 checkIncomplete(h0); 1642 checkIncomplete(h1); 1643 assertTrue(f.complete(v1)); 1644 final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); 1645 final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); 1646 1647 checkCompletedNormally(h0, null); 1648 checkCompletedNormally(h1, null); 1649 checkCompletedNormally(h2, null); 1650 checkCompletedNormally(h3, null); 1651 checkCompletedNormally(f, v1); 1652 for (NoopConsumer r : rs) r.assertValue(v1); 1653 }} 1654 1655 /** 1656 * thenAccept result completes exceptionally after exceptional 1657 * completion of source 1658 */ testThenAccept_exceptionalCompletion()1659 public void testThenAccept_exceptionalCompletion() { 1660 for (ExecutionMode m : ExecutionMode.values()) 1661 { 1662 final CFException ex = new CFException(); 1663 final CompletableFuture<Item> f = new CompletableFuture<>(); 1664 final NoopConsumer[] rs = new NoopConsumer[4]; 1665 for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); 1666 1667 final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); 1668 final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); 1669 assertTrue(f.completeExceptionally(ex)); 1670 final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); 1671 final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); 1672 1673 checkCompletedWithWrappedException(h0, ex); 1674 checkCompletedWithWrappedException(h1, ex); 1675 checkCompletedWithWrappedException(h2, ex); 1676 checkCompletedWithWrappedException(h3, ex); 1677 checkCompletedExceptionally(f, ex); 1678 for (NoopConsumer r : rs) r.assertNotInvoked(); 1679 }} 1680 1681 /** 1682 * thenAccept result completes exceptionally if source cancelled 1683 */ testThenAccept_sourceCancelled()1684 public void testThenAccept_sourceCancelled() { 1685 for (ExecutionMode m : ExecutionMode.values()) 1686 for (boolean mayInterruptIfRunning : new boolean[] { true, false }) 1687 { 1688 final CompletableFuture<Item> f = new CompletableFuture<>(); 1689 final NoopConsumer[] rs = new NoopConsumer[4]; 1690 for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); 1691 1692 final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); 1693 final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); 1694 assertTrue(f.cancel(mayInterruptIfRunning)); 1695 final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); 1696 final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); 1697 1698 checkCompletedWithWrappedCancellationException(h0); 1699 checkCompletedWithWrappedCancellationException(h1); 1700 checkCompletedWithWrappedCancellationException(h2); 1701 checkCompletedWithWrappedCancellationException(h3); 1702 checkCancelled(f); 1703 for (NoopConsumer r : rs) r.assertNotInvoked(); 1704 }} 1705 1706 /** 1707 * thenAccept result completes exceptionally if action does 1708 */ testThenAccept_actionFailed()1709 public void testThenAccept_actionFailed() { 1710 for (ExecutionMode m : ExecutionMode.values()) 1711 for (Item v1 : new Item[] { itemOne, null }) 1712 { 1713 final CompletableFuture<Item> f = new CompletableFuture<>(); 1714 final FailingConsumer[] rs = new FailingConsumer[4]; 1715 for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m); 1716 1717 final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); 1718 final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); 1719 assertTrue(f.complete(v1)); 1720 final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); 1721 final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); 1722 1723 checkCompletedWithWrappedException(h0, rs[0].ex); 1724 checkCompletedWithWrappedException(h1, rs[1].ex); 1725 checkCompletedWithWrappedException(h2, rs[2].ex); 1726 checkCompletedWithWrappedException(h3, rs[3].ex); 1727 checkCompletedNormally(f, v1); 1728 }} 1729 1730 /** 1731 * thenCombine result completes normally after normal completion 1732 * of sources 1733 */ testThenCombine_normalCompletion()1734 public void testThenCombine_normalCompletion() { 1735 for (ExecutionMode m : ExecutionMode.values()) 1736 for (boolean fFirst : new boolean[] { true, false }) 1737 for (Item v1 : new Item[] { itemOne, null }) 1738 for (Item v2 : new Item[] { itemTwo, null }) 1739 { 1740 final CompletableFuture<Item> f = new CompletableFuture<>(); 1741 final CompletableFuture<Item> g = new CompletableFuture<>(); 1742 final SubtractFunction[] rs = new SubtractFunction[6]; 1743 for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m); 1744 1745 final CompletableFuture<Item> fst = fFirst ? f : g; 1746 final CompletableFuture<Item> snd = !fFirst ? f : g; 1747 final Item w1 = fFirst ? v1 : v2; 1748 final Item w2 = !fFirst ? v1 : v2; 1749 1750 final CompletableFuture<Item> h0 = m.thenCombine(f, g, rs[0]); 1751 final CompletableFuture<Item> h1 = m.thenCombine(fst, fst, rs[1]); 1752 assertTrue(fst.complete(w1)); 1753 final CompletableFuture<Item> h2 = m.thenCombine(f, g, rs[2]); 1754 final CompletableFuture<Item> h3 = m.thenCombine(fst, fst, rs[3]); 1755 checkIncomplete(h0); rs[0].assertNotInvoked(); 1756 checkIncomplete(h2); rs[2].assertNotInvoked(); 1757 checkCompletedNormally(h1, subtract(w1, w1)); 1758 checkCompletedNormally(h3, subtract(w1, w1)); 1759 rs[1].assertValue(subtract(w1, w1)); 1760 rs[3].assertValue(subtract(w1, w1)); 1761 assertTrue(snd.complete(w2)); 1762 final CompletableFuture<Item> h4 = m.thenCombine(f, g, rs[4]); 1763 1764 checkCompletedNormally(h0, subtract(v1, v2)); 1765 checkCompletedNormally(h2, subtract(v1, v2)); 1766 checkCompletedNormally(h4, subtract(v1, v2)); 1767 rs[0].assertValue(subtract(v1, v2)); 1768 rs[2].assertValue(subtract(v1, v2)); 1769 rs[4].assertValue(subtract(v1, v2)); 1770 1771 checkCompletedNormally(f, v1); 1772 checkCompletedNormally(g, v2); 1773 }} 1774 1775 /** 1776 * thenCombine result completes exceptionally after exceptional 1777 * completion of either source 1778 */ testThenCombine_exceptionalCompletion()1779 public void testThenCombine_exceptionalCompletion() throws Throwable { 1780 for (ExecutionMode m : ExecutionMode.values()) 1781 for (boolean fFirst : new boolean[] { true, false }) 1782 for (boolean failFirst : new boolean[] { true, false }) 1783 for (Item v1 : new Item[] { itemOne, null }) 1784 { 1785 final CompletableFuture<Item> f = new CompletableFuture<>(); 1786 final CompletableFuture<Item> g = new CompletableFuture<>(); 1787 final CFException ex = new CFException(); 1788 final SubtractFunction r1 = new SubtractFunction(m); 1789 final SubtractFunction r2 = new SubtractFunction(m); 1790 final SubtractFunction r3 = new SubtractFunction(m); 1791 1792 final CompletableFuture<Item> fst = fFirst ? f : g; 1793 final CompletableFuture<Item> snd = !fFirst ? f : g; 1794 final Callable<Boolean> complete1 = failFirst ? 1795 () -> fst.completeExceptionally(ex) : 1796 () -> fst.complete(v1); 1797 final Callable<Boolean> complete2 = failFirst ? 1798 () -> snd.complete(v1) : 1799 () -> snd.completeExceptionally(ex); 1800 1801 final CompletableFuture<Item> h1 = m.thenCombine(f, g, r1); 1802 assertTrue(complete1.call()); 1803 final CompletableFuture<Item> h2 = m.thenCombine(f, g, r2); 1804 checkIncomplete(h1); 1805 checkIncomplete(h2); 1806 assertTrue(complete2.call()); 1807 final CompletableFuture<Item> h3 = m.thenCombine(f, g, r3); 1808 1809 checkCompletedWithWrappedException(h1, ex); 1810 checkCompletedWithWrappedException(h2, ex); 1811 checkCompletedWithWrappedException(h3, ex); 1812 r1.assertNotInvoked(); 1813 r2.assertNotInvoked(); 1814 r3.assertNotInvoked(); 1815 checkCompletedNormally(failFirst ? snd : fst, v1); 1816 checkCompletedExceptionally(failFirst ? fst : snd, ex); 1817 }} 1818 1819 /** 1820 * thenCombine result completes exceptionally if either source cancelled 1821 */ testThenCombine_sourceCancelled()1822 public void testThenCombine_sourceCancelled() throws Throwable { 1823 for (ExecutionMode m : ExecutionMode.values()) 1824 for (boolean mayInterruptIfRunning : new boolean[] { true, false }) 1825 for (boolean fFirst : new boolean[] { true, false }) 1826 for (boolean failFirst : new boolean[] { true, false }) 1827 for (Item v1 : new Item[] { itemOne, null }) 1828 { 1829 final CompletableFuture<Item> f = new CompletableFuture<>(); 1830 final CompletableFuture<Item> g = new CompletableFuture<>(); 1831 final SubtractFunction r1 = new SubtractFunction(m); 1832 final SubtractFunction r2 = new SubtractFunction(m); 1833 final SubtractFunction r3 = new SubtractFunction(m); 1834 1835 final CompletableFuture<Item> fst = fFirst ? f : g; 1836 final CompletableFuture<Item> snd = !fFirst ? f : g; 1837 final Callable<Boolean> complete1 = failFirst ? 1838 () -> fst.cancel(mayInterruptIfRunning) : 1839 () -> fst.complete(v1); 1840 final Callable<Boolean> complete2 = failFirst ? 1841 () -> snd.complete(v1) : 1842 () -> snd.cancel(mayInterruptIfRunning); 1843 1844 final CompletableFuture<Item> h1 = m.thenCombine(f, g, r1); 1845 assertTrue(complete1.call()); 1846 final CompletableFuture<Item> h2 = m.thenCombine(f, g, r2); 1847 checkIncomplete(h1); 1848 checkIncomplete(h2); 1849 assertTrue(complete2.call()); 1850 final CompletableFuture<Item> h3 = m.thenCombine(f, g, r3); 1851 1852 checkCompletedWithWrappedCancellationException(h1); 1853 checkCompletedWithWrappedCancellationException(h2); 1854 checkCompletedWithWrappedCancellationException(h3); 1855 r1.assertNotInvoked(); 1856 r2.assertNotInvoked(); 1857 r3.assertNotInvoked(); 1858 checkCompletedNormally(failFirst ? snd : fst, v1); 1859 checkCancelled(failFirst ? fst : snd); 1860 }} 1861 1862 /** 1863 * thenCombine result completes exceptionally if action does 1864 */ testThenCombine_actionFailed()1865 public void testThenCombine_actionFailed() { 1866 for (ExecutionMode m : ExecutionMode.values()) 1867 for (boolean fFirst : new boolean[] { true, false }) 1868 for (Item v1 : new Item[] { itemOne, null }) 1869 for (Item v2 : new Item[] { itemTwo, null }) 1870 { 1871 final CompletableFuture<Item> f = new CompletableFuture<>(); 1872 final CompletableFuture<Item> g = new CompletableFuture<>(); 1873 final FailingBiFunction r1 = new FailingBiFunction(m); 1874 final FailingBiFunction r2 = new FailingBiFunction(m); 1875 final FailingBiFunction r3 = new FailingBiFunction(m); 1876 1877 final CompletableFuture<Item> fst = fFirst ? f : g; 1878 final CompletableFuture<Item> snd = !fFirst ? f : g; 1879 final Item w1 = fFirst ? v1 : v2; 1880 final Item w2 = !fFirst ? v1 : v2; 1881 1882 final CompletableFuture<Item> h1 = m.thenCombine(f, g, r1); 1883 assertTrue(fst.complete(w1)); 1884 final CompletableFuture<Item> h2 = m.thenCombine(f, g, r2); 1885 assertTrue(snd.complete(w2)); 1886 final CompletableFuture<Item> h3 = m.thenCombine(f, g, r3); 1887 1888 checkCompletedWithWrappedException(h1, r1.ex); 1889 checkCompletedWithWrappedException(h2, r2.ex); 1890 checkCompletedWithWrappedException(h3, r3.ex); 1891 r1.assertInvoked(); 1892 r2.assertInvoked(); 1893 r3.assertInvoked(); 1894 checkCompletedNormally(f, v1); 1895 checkCompletedNormally(g, v2); 1896 }} 1897 1898 /** 1899 * thenAcceptBoth result completes normally after normal 1900 * completion of sources 1901 */ testThenAcceptBoth_normalCompletion()1902 public void testThenAcceptBoth_normalCompletion() { 1903 for (ExecutionMode m : ExecutionMode.values()) 1904 for (boolean fFirst : new boolean[] { true, false }) 1905 for (Item v1 : new Item[] { itemOne, null }) 1906 for (Item v2 : new Item[] { itemTwo, null }) 1907 { 1908 final CompletableFuture<Item> f = new CompletableFuture<>(); 1909 final CompletableFuture<Item> g = new CompletableFuture<>(); 1910 final SubtractAction r1 = new SubtractAction(m); 1911 final SubtractAction r2 = new SubtractAction(m); 1912 final SubtractAction r3 = new SubtractAction(m); 1913 1914 final CompletableFuture<Item> fst = fFirst ? f : g; 1915 final CompletableFuture<Item> snd = !fFirst ? f : g; 1916 final Item w1 = fFirst ? v1 : v2; 1917 final Item w2 = !fFirst ? v1 : v2; 1918 1919 final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); 1920 assertTrue(fst.complete(w1)); 1921 final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); 1922 checkIncomplete(h1); 1923 checkIncomplete(h2); 1924 r1.assertNotInvoked(); 1925 r2.assertNotInvoked(); 1926 assertTrue(snd.complete(w2)); 1927 final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); 1928 1929 checkCompletedNormally(h1, null); 1930 checkCompletedNormally(h2, null); 1931 checkCompletedNormally(h3, null); 1932 r1.assertValue(subtract(v1, v2)); 1933 r2.assertValue(subtract(v1, v2)); 1934 r3.assertValue(subtract(v1, v2)); 1935 checkCompletedNormally(f, v1); 1936 checkCompletedNormally(g, v2); 1937 }} 1938 1939 /** 1940 * thenAcceptBoth result completes exceptionally after exceptional 1941 * completion of either source 1942 */ testThenAcceptBoth_exceptionalCompletion()1943 public void testThenAcceptBoth_exceptionalCompletion() throws Throwable { 1944 for (ExecutionMode m : ExecutionMode.values()) 1945 for (boolean fFirst : new boolean[] { true, false }) 1946 for (boolean failFirst : new boolean[] { true, false }) 1947 for (Item v1 : new Item[] { itemOne, null }) 1948 { 1949 final CompletableFuture<Item> f = new CompletableFuture<>(); 1950 final CompletableFuture<Item> g = new CompletableFuture<>(); 1951 final CFException ex = new CFException(); 1952 final SubtractAction r1 = new SubtractAction(m); 1953 final SubtractAction r2 = new SubtractAction(m); 1954 final SubtractAction r3 = new SubtractAction(m); 1955 1956 final CompletableFuture<Item> fst = fFirst ? f : g; 1957 final CompletableFuture<Item> snd = !fFirst ? f : g; 1958 final Callable<Boolean> complete1 = failFirst ? 1959 () -> fst.completeExceptionally(ex) : 1960 () -> fst.complete(v1); 1961 final Callable<Boolean> complete2 = failFirst ? 1962 () -> snd.complete(v1) : 1963 () -> snd.completeExceptionally(ex); 1964 1965 final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); 1966 assertTrue(complete1.call()); 1967 final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); 1968 checkIncomplete(h1); 1969 checkIncomplete(h2); 1970 assertTrue(complete2.call()); 1971 final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); 1972 1973 checkCompletedWithWrappedException(h1, ex); 1974 checkCompletedWithWrappedException(h2, ex); 1975 checkCompletedWithWrappedException(h3, ex); 1976 r1.assertNotInvoked(); 1977 r2.assertNotInvoked(); 1978 r3.assertNotInvoked(); 1979 checkCompletedNormally(failFirst ? snd : fst, v1); 1980 checkCompletedExceptionally(failFirst ? fst : snd, ex); 1981 }} 1982 1983 /** 1984 * thenAcceptBoth result completes exceptionally if either source cancelled 1985 */ testThenAcceptBoth_sourceCancelled()1986 public void testThenAcceptBoth_sourceCancelled() throws Throwable { 1987 for (ExecutionMode m : ExecutionMode.values()) 1988 for (boolean mayInterruptIfRunning : new boolean[] { true, false }) 1989 for (boolean fFirst : new boolean[] { true, false }) 1990 for (boolean failFirst : new boolean[] { true, false }) 1991 for (Item v1 : new Item[] { itemOne, null }) 1992 { 1993 final CompletableFuture<Item> f = new CompletableFuture<>(); 1994 final CompletableFuture<Item> g = new CompletableFuture<>(); 1995 final SubtractAction r1 = new SubtractAction(m); 1996 final SubtractAction r2 = new SubtractAction(m); 1997 final SubtractAction r3 = new SubtractAction(m); 1998 1999 final CompletableFuture<Item> fst = fFirst ? f : g; 2000 final CompletableFuture<Item> snd = !fFirst ? f : g; 2001 final Callable<Boolean> complete1 = failFirst ? 2002 () -> fst.cancel(mayInterruptIfRunning) : 2003 () -> fst.complete(v1); 2004 final Callable<Boolean> complete2 = failFirst ? 2005 () -> snd.complete(v1) : 2006 () -> snd.cancel(mayInterruptIfRunning); 2007 2008 final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); 2009 assertTrue(complete1.call()); 2010 final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); 2011 checkIncomplete(h1); 2012 checkIncomplete(h2); 2013 assertTrue(complete2.call()); 2014 final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); 2015 2016 checkCompletedWithWrappedCancellationException(h1); 2017 checkCompletedWithWrappedCancellationException(h2); 2018 checkCompletedWithWrappedCancellationException(h3); 2019 r1.assertNotInvoked(); 2020 r2.assertNotInvoked(); 2021 r3.assertNotInvoked(); 2022 checkCompletedNormally(failFirst ? snd : fst, v1); 2023 checkCancelled(failFirst ? fst : snd); 2024 }} 2025 2026 /** 2027 * thenAcceptBoth result completes exceptionally if action does 2028 */ testThenAcceptBoth_actionFailed()2029 public void testThenAcceptBoth_actionFailed() { 2030 for (ExecutionMode m : ExecutionMode.values()) 2031 for (boolean fFirst : new boolean[] { true, false }) 2032 for (Item v1 : new Item[] { itemOne, null }) 2033 for (Item v2 : new Item[] { itemTwo, null }) 2034 { 2035 final CompletableFuture<Item> f = new CompletableFuture<>(); 2036 final CompletableFuture<Item> g = new CompletableFuture<>(); 2037 final FailingBiConsumer r1 = new FailingBiConsumer(m); 2038 final FailingBiConsumer r2 = new FailingBiConsumer(m); 2039 final FailingBiConsumer r3 = new FailingBiConsumer(m); 2040 2041 final CompletableFuture<Item> fst = fFirst ? f : g; 2042 final CompletableFuture<Item> snd = !fFirst ? f : g; 2043 final Item w1 = fFirst ? v1 : v2; 2044 final Item w2 = !fFirst ? v1 : v2; 2045 2046 final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); 2047 assertTrue(fst.complete(w1)); 2048 final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); 2049 assertTrue(snd.complete(w2)); 2050 final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); 2051 2052 checkCompletedWithWrappedException(h1, r1.ex); 2053 checkCompletedWithWrappedException(h2, r2.ex); 2054 checkCompletedWithWrappedException(h3, r3.ex); 2055 r1.assertInvoked(); 2056 r2.assertInvoked(); 2057 r3.assertInvoked(); 2058 checkCompletedNormally(f, v1); 2059 checkCompletedNormally(g, v2); 2060 }} 2061 2062 /** 2063 * runAfterBoth result completes normally after normal 2064 * completion of sources 2065 */ testRunAfterBoth_normalCompletion()2066 public void testRunAfterBoth_normalCompletion() { 2067 for (ExecutionMode m : ExecutionMode.values()) 2068 for (boolean fFirst : new boolean[] { true, false }) 2069 for (Item v1 : new Item[] { itemOne, null }) 2070 for (Item v2 : new Item[] { itemTwo, null }) 2071 { 2072 final CompletableFuture<Item> f = new CompletableFuture<>(); 2073 final CompletableFuture<Item> g = new CompletableFuture<>(); 2074 final Noop r1 = new Noop(m); 2075 final Noop r2 = new Noop(m); 2076 final Noop r3 = new Noop(m); 2077 2078 final CompletableFuture<Item> fst = fFirst ? f : g; 2079 final CompletableFuture<Item> snd = !fFirst ? f : g; 2080 final Item w1 = fFirst ? v1 : v2; 2081 final Item w2 = !fFirst ? v1 : v2; 2082 2083 final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); 2084 assertTrue(fst.complete(w1)); 2085 final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); 2086 checkIncomplete(h1); 2087 checkIncomplete(h2); 2088 r1.assertNotInvoked(); 2089 r2.assertNotInvoked(); 2090 assertTrue(snd.complete(w2)); 2091 final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); 2092 2093 checkCompletedNormally(h1, null); 2094 checkCompletedNormally(h2, null); 2095 checkCompletedNormally(h3, null); 2096 r1.assertInvoked(); 2097 r2.assertInvoked(); 2098 r3.assertInvoked(); 2099 checkCompletedNormally(f, v1); 2100 checkCompletedNormally(g, v2); 2101 }} 2102 2103 /** 2104 * runAfterBoth result completes exceptionally after exceptional 2105 * completion of either source 2106 */ testRunAfterBoth_exceptionalCompletion()2107 public void testRunAfterBoth_exceptionalCompletion() throws Throwable { 2108 for (ExecutionMode m : ExecutionMode.values()) 2109 for (boolean fFirst : new boolean[] { true, false }) 2110 for (boolean failFirst : new boolean[] { true, false }) 2111 for (Item v1 : new Item[] { itemOne, null }) 2112 { 2113 final CompletableFuture<Item> f = new CompletableFuture<>(); 2114 final CompletableFuture<Item> g = new CompletableFuture<>(); 2115 final CFException ex = new CFException(); 2116 final Noop r1 = new Noop(m); 2117 final Noop r2 = new Noop(m); 2118 final Noop r3 = new Noop(m); 2119 2120 final CompletableFuture<Item> fst = fFirst ? f : g; 2121 final CompletableFuture<Item> snd = !fFirst ? f : g; 2122 final Callable<Boolean> complete1 = failFirst ? 2123 () -> fst.completeExceptionally(ex) : 2124 () -> fst.complete(v1); 2125 final Callable<Boolean> complete2 = failFirst ? 2126 () -> snd.complete(v1) : 2127 () -> snd.completeExceptionally(ex); 2128 2129 final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); 2130 assertTrue(complete1.call()); 2131 final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); 2132 checkIncomplete(h1); 2133 checkIncomplete(h2); 2134 assertTrue(complete2.call()); 2135 final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); 2136 2137 checkCompletedWithWrappedException(h1, ex); 2138 checkCompletedWithWrappedException(h2, ex); 2139 checkCompletedWithWrappedException(h3, ex); 2140 r1.assertNotInvoked(); 2141 r2.assertNotInvoked(); 2142 r3.assertNotInvoked(); 2143 checkCompletedNormally(failFirst ? snd : fst, v1); 2144 checkCompletedExceptionally(failFirst ? fst : snd, ex); 2145 }} 2146 2147 /** 2148 * runAfterBoth result completes exceptionally if either source cancelled 2149 */ testRunAfterBoth_sourceCancelled()2150 public void testRunAfterBoth_sourceCancelled() throws Throwable { 2151 for (ExecutionMode m : ExecutionMode.values()) 2152 for (boolean mayInterruptIfRunning : new boolean[] { true, false }) 2153 for (boolean fFirst : new boolean[] { true, false }) 2154 for (boolean failFirst : new boolean[] { true, false }) 2155 for (Item v1 : new Item[] { itemOne, null }) 2156 { 2157 final CompletableFuture<Item> f = new CompletableFuture<>(); 2158 final CompletableFuture<Item> g = new CompletableFuture<>(); 2159 final Noop r1 = new Noop(m); 2160 final Noop r2 = new Noop(m); 2161 final Noop r3 = new Noop(m); 2162 2163 final CompletableFuture<Item> fst = fFirst ? f : g; 2164 final CompletableFuture<Item> snd = !fFirst ? f : g; 2165 final Callable<Boolean> complete1 = failFirst ? 2166 () -> fst.cancel(mayInterruptIfRunning) : 2167 () -> fst.complete(v1); 2168 final Callable<Boolean> complete2 = failFirst ? 2169 () -> snd.complete(v1) : 2170 () -> snd.cancel(mayInterruptIfRunning); 2171 2172 final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); 2173 assertTrue(complete1.call()); 2174 final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); 2175 checkIncomplete(h1); 2176 checkIncomplete(h2); 2177 assertTrue(complete2.call()); 2178 final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); 2179 2180 checkCompletedWithWrappedCancellationException(h1); 2181 checkCompletedWithWrappedCancellationException(h2); 2182 checkCompletedWithWrappedCancellationException(h3); 2183 r1.assertNotInvoked(); 2184 r2.assertNotInvoked(); 2185 r3.assertNotInvoked(); 2186 checkCompletedNormally(failFirst ? snd : fst, v1); 2187 checkCancelled(failFirst ? fst : snd); 2188 }} 2189 2190 /** 2191 * runAfterBoth result completes exceptionally if action does 2192 */ testRunAfterBoth_actionFailed()2193 public void testRunAfterBoth_actionFailed() { 2194 for (ExecutionMode m : ExecutionMode.values()) 2195 for (boolean fFirst : new boolean[] { true, false }) 2196 for (Item v1 : new Item[] { itemOne, null }) 2197 for (Item v2 : new Item[] { itemTwo, null }) 2198 { 2199 final CompletableFuture<Item> f = new CompletableFuture<>(); 2200 final CompletableFuture<Item> g = new CompletableFuture<>(); 2201 final FailingRunnable r1 = new FailingRunnable(m); 2202 final FailingRunnable r2 = new FailingRunnable(m); 2203 final FailingRunnable r3 = new FailingRunnable(m); 2204 2205 final CompletableFuture<Item> fst = fFirst ? f : g; 2206 final CompletableFuture<Item> snd = !fFirst ? f : g; 2207 final Item w1 = fFirst ? v1 : v2; 2208 final Item w2 = !fFirst ? v1 : v2; 2209 2210 final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); 2211 assertTrue(fst.complete(w1)); 2212 final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); 2213 assertTrue(snd.complete(w2)); 2214 final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); 2215 2216 checkCompletedWithWrappedException(h1, r1.ex); 2217 checkCompletedWithWrappedException(h2, r2.ex); 2218 checkCompletedWithWrappedException(h3, r3.ex); 2219 r1.assertInvoked(); 2220 r2.assertInvoked(); 2221 r3.assertInvoked(); 2222 checkCompletedNormally(f, v1); 2223 checkCompletedNormally(g, v2); 2224 }} 2225 2226 /** 2227 * applyToEither result completes normally after normal completion 2228 * of either source 2229 */ testApplyToEither_normalCompletion()2230 public void testApplyToEither_normalCompletion() { 2231 for (ExecutionMode m : ExecutionMode.values()) 2232 for (Item v1 : new Item[] { itemOne, null }) 2233 for (Item v2 : new Item[] { itemTwo, null }) 2234 { 2235 final CompletableFuture<Item> f = new CompletableFuture<>(); 2236 final CompletableFuture<Item> g = new CompletableFuture<>(); 2237 final IncFunction[] rs = new IncFunction[6]; 2238 for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); 2239 2240 final CompletableFuture<Item> h0 = m.applyToEither(f, g, rs[0]); 2241 final CompletableFuture<Item> h1 = m.applyToEither(g, f, rs[1]); 2242 checkIncomplete(h0); 2243 checkIncomplete(h1); 2244 rs[0].assertNotInvoked(); 2245 rs[1].assertNotInvoked(); 2246 f.complete(v1); 2247 checkCompletedNormally(h0, inc(v1)); 2248 checkCompletedNormally(h1, inc(v1)); 2249 final CompletableFuture<Item> h2 = m.applyToEither(f, g, rs[2]); 2250 final CompletableFuture<Item> h3 = m.applyToEither(g, f, rs[3]); 2251 checkCompletedNormally(h2, inc(v1)); 2252 checkCompletedNormally(h3, inc(v1)); 2253 g.complete(v2); 2254 2255 // unspecified behavior - both source completions available 2256 final CompletableFuture<Item> h4 = m.applyToEither(f, g, rs[4]); 2257 final CompletableFuture<Item> h5 = m.applyToEither(g, f, rs[5]); 2258 rs[4].assertValue(h4.join()); 2259 rs[5].assertValue(h5.join()); 2260 assertTrue(Objects.equals(inc(v1), h4.join()) || 2261 Objects.equals(inc(v2), h4.join())); 2262 assertTrue(Objects.equals(inc(v1), h5.join()) || 2263 Objects.equals(inc(v2), h5.join())); 2264 2265 checkCompletedNormally(f, v1); 2266 checkCompletedNormally(g, v2); 2267 checkCompletedNormally(h0, inc(v1)); 2268 checkCompletedNormally(h1, inc(v1)); 2269 checkCompletedNormally(h2, inc(v1)); 2270 checkCompletedNormally(h3, inc(v1)); 2271 for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1)); 2272 }} 2273 2274 /** 2275 * applyToEither result completes exceptionally after exceptional 2276 * completion of either source 2277 */ testApplyToEither_exceptionalCompletion()2278 public void testApplyToEither_exceptionalCompletion() { 2279 for (ExecutionMode m : ExecutionMode.values()) 2280 for (Item v1 : new Item[] { itemOne, null }) 2281 { 2282 final CompletableFuture<Item> f = new CompletableFuture<>(); 2283 final CompletableFuture<Item> g = new CompletableFuture<>(); 2284 final CFException ex = new CFException(); 2285 final IncFunction[] rs = new IncFunction[6]; 2286 for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); 2287 2288 final CompletableFuture<Item> h0 = m.applyToEither(f, g, rs[0]); 2289 final CompletableFuture<Item> h1 = m.applyToEither(g, f, rs[1]); 2290 checkIncomplete(h0); 2291 checkIncomplete(h1); 2292 rs[0].assertNotInvoked(); 2293 rs[1].assertNotInvoked(); 2294 f.completeExceptionally(ex); 2295 checkCompletedWithWrappedException(h0, ex); 2296 checkCompletedWithWrappedException(h1, ex); 2297 final CompletableFuture<Item> h2 = m.applyToEither(f, g, rs[2]); 2298 final CompletableFuture<Item> h3 = m.applyToEither(g, f, rs[3]); 2299 checkCompletedWithWrappedException(h2, ex); 2300 checkCompletedWithWrappedException(h3, ex); 2301 g.complete(v1); 2302 2303 // unspecified behavior - both source completions available 2304 final CompletableFuture<Item> h4 = m.applyToEither(f, g, rs[4]); 2305 final CompletableFuture<Item> h5 = m.applyToEither(g, f, rs[5]); 2306 try { 2307 mustEqual(inc(v1), h4.join()); 2308 rs[4].assertValue(inc(v1)); 2309 } catch (CompletionException ok) { 2310 checkCompletedWithWrappedException(h4, ex); 2311 rs[4].assertNotInvoked(); 2312 } 2313 try { 2314 mustEqual(inc(v1), h5.join()); 2315 rs[5].assertValue(inc(v1)); 2316 } catch (CompletionException ok) { 2317 checkCompletedWithWrappedException(h5, ex); 2318 rs[5].assertNotInvoked(); 2319 } 2320 2321 checkCompletedExceptionally(f, ex); 2322 checkCompletedNormally(g, v1); 2323 checkCompletedWithWrappedException(h0, ex); 2324 checkCompletedWithWrappedException(h1, ex); 2325 checkCompletedWithWrappedException(h2, ex); 2326 checkCompletedWithWrappedException(h3, ex); 2327 checkCompletedWithWrappedException(h4, ex); 2328 for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); 2329 }} 2330 testApplyToEither_exceptionalCompletion2()2331 public void testApplyToEither_exceptionalCompletion2() { 2332 for (ExecutionMode m : ExecutionMode.values()) 2333 for (boolean fFirst : new boolean[] { true, false }) 2334 for (Item v1 : new Item[] { itemOne, null }) 2335 { 2336 final CompletableFuture<Item> f = new CompletableFuture<>(); 2337 final CompletableFuture<Item> g = new CompletableFuture<>(); 2338 final CFException ex = new CFException(); 2339 final IncFunction[] rs = new IncFunction[6]; 2340 for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); 2341 2342 final CompletableFuture<Item> h0 = m.applyToEither(f, g, rs[0]); 2343 final CompletableFuture<Item> h1 = m.applyToEither(g, f, rs[1]); 2344 assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex)); 2345 assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); 2346 final CompletableFuture<Item> h2 = m.applyToEither(f, g, rs[2]); 2347 final CompletableFuture<Item> h3 = m.applyToEither(g, f, rs[3]); 2348 2349 // unspecified behavior - both source completions available 2350 try { 2351 mustEqual(inc(v1), h0.join()); 2352 rs[0].assertValue(inc(v1)); 2353 } catch (CompletionException ok) { 2354 checkCompletedWithWrappedException(h0, ex); 2355 rs[0].assertNotInvoked(); 2356 } 2357 try { 2358 mustEqual(inc(v1), h1.join()); 2359 rs[1].assertValue(inc(v1)); 2360 } catch (CompletionException ok) { 2361 checkCompletedWithWrappedException(h1, ex); 2362 rs[1].assertNotInvoked(); 2363 } 2364 try { 2365 mustEqual(inc(v1), h2.join()); 2366 rs[2].assertValue(inc(v1)); 2367 } catch (CompletionException ok) { 2368 checkCompletedWithWrappedException(h2, ex); 2369 rs[2].assertNotInvoked(); 2370 } 2371 try { 2372 mustEqual(inc(v1), h3.join()); 2373 rs[3].assertValue(inc(v1)); 2374 } catch (CompletionException ok) { 2375 checkCompletedWithWrappedException(h3, ex); 2376 rs[3].assertNotInvoked(); 2377 } 2378 2379 checkCompletedNormally(f, v1); 2380 checkCompletedExceptionally(g, ex); 2381 }} 2382 2383 /** 2384 * applyToEither result completes exceptionally if either source cancelled 2385 */ testApplyToEither_sourceCancelled()2386 public void testApplyToEither_sourceCancelled() { 2387 for (ExecutionMode m : ExecutionMode.values()) 2388 for (boolean mayInterruptIfRunning : new boolean[] { true, false }) 2389 for (Item v1 : new Item[] { itemOne, null }) 2390 { 2391 final CompletableFuture<Item> f = new CompletableFuture<>(); 2392 final CompletableFuture<Item> g = new CompletableFuture<>(); 2393 final IncFunction[] rs = new IncFunction[6]; 2394 for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); 2395 2396 final CompletableFuture<Item> h0 = m.applyToEither(f, g, rs[0]); 2397 final CompletableFuture<Item> h1 = m.applyToEither(g, f, rs[1]); 2398 checkIncomplete(h0); 2399 checkIncomplete(h1); 2400 rs[0].assertNotInvoked(); 2401 rs[1].assertNotInvoked(); 2402 f.cancel(mayInterruptIfRunning); 2403 checkCompletedWithWrappedCancellationException(h0); 2404 checkCompletedWithWrappedCancellationException(h1); 2405 final CompletableFuture<Item> h2 = m.applyToEither(f, g, rs[2]); 2406 final CompletableFuture<Item> h3 = m.applyToEither(g, f, rs[3]); 2407 checkCompletedWithWrappedCancellationException(h2); 2408 checkCompletedWithWrappedCancellationException(h3); 2409 g.complete(v1); 2410 2411 // unspecified behavior - both source completions available 2412 final CompletableFuture<Item> h4 = m.applyToEither(f, g, rs[4]); 2413 final CompletableFuture<Item> h5 = m.applyToEither(g, f, rs[5]); 2414 try { 2415 mustEqual(inc(v1), h4.join()); 2416 rs[4].assertValue(inc(v1)); 2417 } catch (CompletionException ok) { 2418 checkCompletedWithWrappedCancellationException(h4); 2419 rs[4].assertNotInvoked(); 2420 } 2421 try { 2422 mustEqual(inc(v1), h5.join()); 2423 rs[5].assertValue(inc(v1)); 2424 } catch (CompletionException ok) { 2425 checkCompletedWithWrappedCancellationException(h5); 2426 rs[5].assertNotInvoked(); 2427 } 2428 2429 checkCancelled(f); 2430 checkCompletedNormally(g, v1); 2431 checkCompletedWithWrappedCancellationException(h0); 2432 checkCompletedWithWrappedCancellationException(h1); 2433 checkCompletedWithWrappedCancellationException(h2); 2434 checkCompletedWithWrappedCancellationException(h3); 2435 for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); 2436 }} 2437 testApplyToEither_sourceCancelled2()2438 public void testApplyToEither_sourceCancelled2() { 2439 for (ExecutionMode m : ExecutionMode.values()) 2440 for (boolean mayInterruptIfRunning : new boolean[] { true, false }) 2441 for (boolean fFirst : new boolean[] { true, false }) 2442 for (Item v1 : new Item[] { itemOne, null }) 2443 { 2444 final CompletableFuture<Item> f = new CompletableFuture<>(); 2445 final CompletableFuture<Item> g = new CompletableFuture<>(); 2446 final IncFunction[] rs = new IncFunction[6]; 2447 for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); 2448 2449 final CompletableFuture<Item> h0 = m.applyToEither(f, g, rs[0]); 2450 final CompletableFuture<Item> h1 = m.applyToEither(g, f, rs[1]); 2451 assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning)); 2452 assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning)); 2453 final CompletableFuture<Item> h2 = m.applyToEither(f, g, rs[2]); 2454 final CompletableFuture<Item> h3 = m.applyToEither(g, f, rs[3]); 2455 2456 // unspecified behavior - both source completions available 2457 try { 2458 mustEqual(inc(v1), h0.join()); 2459 rs[0].assertValue(inc(v1)); 2460 } catch (CompletionException ok) { 2461 checkCompletedWithWrappedCancellationException(h0); 2462 rs[0].assertNotInvoked(); 2463 } 2464 try { 2465 mustEqual(inc(v1), h1.join()); 2466 rs[1].assertValue(inc(v1)); 2467 } catch (CompletionException ok) { 2468 checkCompletedWithWrappedCancellationException(h1); 2469 rs[1].assertNotInvoked(); 2470 } 2471 try { 2472 mustEqual(inc(v1), h2.join()); 2473 rs[2].assertValue(inc(v1)); 2474 } catch (CompletionException ok) { 2475 checkCompletedWithWrappedCancellationException(h2); 2476 rs[2].assertNotInvoked(); 2477 } 2478 try { 2479 mustEqual(inc(v1), h3.join()); 2480 rs[3].assertValue(inc(v1)); 2481 } catch (CompletionException ok) { 2482 checkCompletedWithWrappedCancellationException(h3); 2483 rs[3].assertNotInvoked(); 2484 } 2485 2486 checkCompletedNormally(f, v1); 2487 checkCancelled(g); 2488 }} 2489 2490 /** 2491 * applyToEither result completes exceptionally if action does 2492 */ testApplyToEither_actionFailed()2493 public void testApplyToEither_actionFailed() { 2494 for (ExecutionMode m : ExecutionMode.values()) 2495 for (Item v1 : new Item[] { itemOne, null }) 2496 for (Item v2 : new Item[] { itemTwo, null }) 2497 { 2498 final CompletableFuture<Item> f = new CompletableFuture<>(); 2499 final CompletableFuture<Item> g = new CompletableFuture<>(); 2500 final FailingFunction[] rs = new FailingFunction[6]; 2501 for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m); 2502 2503 final CompletableFuture<Item> h0 = m.applyToEither(f, g, rs[0]); 2504 final CompletableFuture<Item> h1 = m.applyToEither(g, f, rs[1]); 2505 f.complete(v1); 2506 final CompletableFuture<Item> h2 = m.applyToEither(f, g, rs[2]); 2507 final CompletableFuture<Item> h3 = m.applyToEither(g, f, rs[3]); 2508 checkCompletedWithWrappedException(h0, rs[0].ex); 2509 checkCompletedWithWrappedException(h1, rs[1].ex); 2510 checkCompletedWithWrappedException(h2, rs[2].ex); 2511 checkCompletedWithWrappedException(h3, rs[3].ex); 2512 for (int i = 0; i < 4; i++) rs[i].assertValue(v1); 2513 2514 g.complete(v2); 2515 2516 // unspecified behavior - both source completions available 2517 final CompletableFuture<Item> h4 = m.applyToEither(f, g, rs[4]); 2518 final CompletableFuture<Item> h5 = m.applyToEither(g, f, rs[5]); 2519 2520 checkCompletedWithWrappedException(h4, rs[4].ex); 2521 assertTrue(Objects.equals(v1, rs[4].value) || 2522 Objects.equals(v2, rs[4].value)); 2523 checkCompletedWithWrappedException(h5, rs[5].ex); 2524 assertTrue(Objects.equals(v1, rs[5].value) || 2525 Objects.equals(v2, rs[5].value)); 2526 2527 checkCompletedNormally(f, v1); 2528 checkCompletedNormally(g, v2); 2529 }} 2530 2531 /** 2532 * acceptEither result completes normally after normal completion 2533 * of either source 2534 */ testAcceptEither_normalCompletion()2535 public void testAcceptEither_normalCompletion() { 2536 for (ExecutionMode m : ExecutionMode.values()) 2537 for (Item v1 : new Item[] { itemOne, null }) 2538 for (Item v2 : new Item[] { itemTwo, null }) 2539 { 2540 final CompletableFuture<Item> f = new CompletableFuture<>(); 2541 final CompletableFuture<Item> g = new CompletableFuture<>(); 2542 final NoopConsumer[] rs = new NoopConsumer[6]; 2543 for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); 2544 2545 final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); 2546 final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); 2547 checkIncomplete(h0); 2548 checkIncomplete(h1); 2549 rs[0].assertNotInvoked(); 2550 rs[1].assertNotInvoked(); 2551 f.complete(v1); 2552 checkCompletedNormally(h0, null); 2553 checkCompletedNormally(h1, null); 2554 rs[0].assertValue(v1); 2555 rs[1].assertValue(v1); 2556 final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); 2557 final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); 2558 checkCompletedNormally(h2, null); 2559 checkCompletedNormally(h3, null); 2560 rs[2].assertValue(v1); 2561 rs[3].assertValue(v1); 2562 g.complete(v2); 2563 2564 // unspecified behavior - both source completions available 2565 final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); 2566 final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); 2567 checkCompletedNormally(h4, null); 2568 checkCompletedNormally(h5, null); 2569 assertTrue(Objects.equals(v1, rs[4].value) || 2570 Objects.equals(v2, rs[4].value)); 2571 assertTrue(Objects.equals(v1, rs[5].value) || 2572 Objects.equals(v2, rs[5].value)); 2573 2574 checkCompletedNormally(f, v1); 2575 checkCompletedNormally(g, v2); 2576 checkCompletedNormally(h0, null); 2577 checkCompletedNormally(h1, null); 2578 checkCompletedNormally(h2, null); 2579 checkCompletedNormally(h3, null); 2580 for (int i = 0; i < 4; i++) rs[i].assertValue(v1); 2581 }} 2582 2583 /** 2584 * acceptEither result completes exceptionally after exceptional 2585 * completion of either source 2586 */ testAcceptEither_exceptionalCompletion()2587 public void testAcceptEither_exceptionalCompletion() { 2588 for (ExecutionMode m : ExecutionMode.values()) 2589 for (Item v1 : new Item[] { itemOne, null }) 2590 { 2591 final CompletableFuture<Item> f = new CompletableFuture<>(); 2592 final CompletableFuture<Item> g = new CompletableFuture<>(); 2593 final CFException ex = new CFException(); 2594 final NoopConsumer[] rs = new NoopConsumer[6]; 2595 for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); 2596 2597 final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); 2598 final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); 2599 checkIncomplete(h0); 2600 checkIncomplete(h1); 2601 rs[0].assertNotInvoked(); 2602 rs[1].assertNotInvoked(); 2603 f.completeExceptionally(ex); 2604 checkCompletedWithWrappedException(h0, ex); 2605 checkCompletedWithWrappedException(h1, ex); 2606 final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); 2607 final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); 2608 checkCompletedWithWrappedException(h2, ex); 2609 checkCompletedWithWrappedException(h3, ex); 2610 2611 g.complete(v1); 2612 2613 // unspecified behavior - both source completions available 2614 final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); 2615 final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); 2616 try { 2617 assertNull(h4.join()); 2618 rs[4].assertValue(v1); 2619 } catch (CompletionException ok) { 2620 checkCompletedWithWrappedException(h4, ex); 2621 rs[4].assertNotInvoked(); 2622 } 2623 try { 2624 assertNull(h5.join()); 2625 rs[5].assertValue(v1); 2626 } catch (CompletionException ok) { 2627 checkCompletedWithWrappedException(h5, ex); 2628 rs[5].assertNotInvoked(); 2629 } 2630 2631 checkCompletedExceptionally(f, ex); 2632 checkCompletedNormally(g, v1); 2633 checkCompletedWithWrappedException(h0, ex); 2634 checkCompletedWithWrappedException(h1, ex); 2635 checkCompletedWithWrappedException(h2, ex); 2636 checkCompletedWithWrappedException(h3, ex); 2637 checkCompletedWithWrappedException(h4, ex); 2638 for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); 2639 }} 2640 testAcceptEither_exceptionalCompletion2()2641 public void testAcceptEither_exceptionalCompletion2() { 2642 for (ExecutionMode m : ExecutionMode.values()) 2643 for (boolean fFirst : new boolean[] { true, false }) 2644 for (Item v1 : new Item[] { itemOne, null }) 2645 { 2646 final CompletableFuture<Item> f = new CompletableFuture<>(); 2647 final CompletableFuture<Item> g = new CompletableFuture<>(); 2648 final CFException ex = new CFException(); 2649 final NoopConsumer[] rs = new NoopConsumer[6]; 2650 for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); 2651 2652 final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); 2653 final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); 2654 assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex)); 2655 assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); 2656 final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); 2657 final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); 2658 2659 // unspecified behavior - both source completions available 2660 try { 2661 assertNull(h0.join()); 2662 rs[0].assertValue(v1); 2663 } catch (CompletionException ok) { 2664 checkCompletedWithWrappedException(h0, ex); 2665 rs[0].assertNotInvoked(); 2666 } 2667 try { 2668 assertNull(h1.join()); 2669 rs[1].assertValue(v1); 2670 } catch (CompletionException ok) { 2671 checkCompletedWithWrappedException(h1, ex); 2672 rs[1].assertNotInvoked(); 2673 } 2674 try { 2675 assertNull(h2.join()); 2676 rs[2].assertValue(v1); 2677 } catch (CompletionException ok) { 2678 checkCompletedWithWrappedException(h2, ex); 2679 rs[2].assertNotInvoked(); 2680 } 2681 try { 2682 assertNull(h3.join()); 2683 rs[3].assertValue(v1); 2684 } catch (CompletionException ok) { 2685 checkCompletedWithWrappedException(h3, ex); 2686 rs[3].assertNotInvoked(); 2687 } 2688 2689 checkCompletedNormally(f, v1); 2690 checkCompletedExceptionally(g, ex); 2691 }} 2692 2693 /** 2694 * acceptEither result completes exceptionally if either source cancelled 2695 */ testAcceptEither_sourceCancelled()2696 public void testAcceptEither_sourceCancelled() { 2697 for (ExecutionMode m : ExecutionMode.values()) 2698 for (boolean mayInterruptIfRunning : new boolean[] { true, false }) 2699 for (Item v1 : new Item[] { itemOne, null }) 2700 { 2701 final CompletableFuture<Item> f = new CompletableFuture<>(); 2702 final CompletableFuture<Item> g = new CompletableFuture<>(); 2703 final NoopConsumer[] rs = new NoopConsumer[6]; 2704 for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); 2705 2706 final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); 2707 final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); 2708 checkIncomplete(h0); 2709 checkIncomplete(h1); 2710 rs[0].assertNotInvoked(); 2711 rs[1].assertNotInvoked(); 2712 f.cancel(mayInterruptIfRunning); 2713 checkCompletedWithWrappedCancellationException(h0); 2714 checkCompletedWithWrappedCancellationException(h1); 2715 final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); 2716 final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); 2717 checkCompletedWithWrappedCancellationException(h2); 2718 checkCompletedWithWrappedCancellationException(h3); 2719 2720 g.complete(v1); 2721 2722 // unspecified behavior - both source completions available 2723 final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); 2724 final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); 2725 try { 2726 assertNull(h4.join()); 2727 rs[4].assertValue(v1); 2728 } catch (CompletionException ok) { 2729 checkCompletedWithWrappedCancellationException(h4); 2730 rs[4].assertNotInvoked(); 2731 } 2732 try { 2733 assertNull(h5.join()); 2734 rs[5].assertValue(v1); 2735 } catch (CompletionException ok) { 2736 checkCompletedWithWrappedCancellationException(h5); 2737 rs[5].assertNotInvoked(); 2738 } 2739 2740 checkCancelled(f); 2741 checkCompletedNormally(g, v1); 2742 checkCompletedWithWrappedCancellationException(h0); 2743 checkCompletedWithWrappedCancellationException(h1); 2744 checkCompletedWithWrappedCancellationException(h2); 2745 checkCompletedWithWrappedCancellationException(h3); 2746 for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); 2747 }} 2748 2749 /** 2750 * acceptEither result completes exceptionally if action does 2751 */ testAcceptEither_actionFailed()2752 public void testAcceptEither_actionFailed() { 2753 for (ExecutionMode m : ExecutionMode.values()) 2754 for (Item v1 : new Item[] { itemOne, null }) 2755 for (Item v2 : new Item[] { itemTwo, null }) 2756 { 2757 final CompletableFuture<Item> f = new CompletableFuture<>(); 2758 final CompletableFuture<Item> g = new CompletableFuture<>(); 2759 final FailingConsumer[] rs = new FailingConsumer[6]; 2760 for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m); 2761 2762 final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); 2763 final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); 2764 f.complete(v1); 2765 final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); 2766 final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); 2767 checkCompletedWithWrappedException(h0, rs[0].ex); 2768 checkCompletedWithWrappedException(h1, rs[1].ex); 2769 checkCompletedWithWrappedException(h2, rs[2].ex); 2770 checkCompletedWithWrappedException(h3, rs[3].ex); 2771 for (int i = 0; i < 4; i++) rs[i].assertValue(v1); 2772 2773 g.complete(v2); 2774 2775 // unspecified behavior - both source completions available 2776 final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); 2777 final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); 2778 2779 checkCompletedWithWrappedException(h4, rs[4].ex); 2780 assertTrue(Objects.equals(v1, rs[4].value) || 2781 Objects.equals(v2, rs[4].value)); 2782 checkCompletedWithWrappedException(h5, rs[5].ex); 2783 assertTrue(Objects.equals(v1, rs[5].value) || 2784 Objects.equals(v2, rs[5].value)); 2785 2786 checkCompletedNormally(f, v1); 2787 checkCompletedNormally(g, v2); 2788 }} 2789 2790 /** 2791 * runAfterEither result completes normally after normal completion 2792 * of either source 2793 */ testRunAfterEither_normalCompletion()2794 public void testRunAfterEither_normalCompletion() { 2795 for (ExecutionMode m : ExecutionMode.values()) 2796 for (Item v1 : new Item[] { itemOne, null }) 2797 for (Item v2 : new Item[] { itemTwo, null }) 2798 for (boolean pushNop : new boolean[] { true, false }) 2799 { 2800 final CompletableFuture<Item> f = new CompletableFuture<>(); 2801 final CompletableFuture<Item> g = new CompletableFuture<>(); 2802 final Noop[] rs = new Noop[6]; 2803 for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); 2804 2805 final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); 2806 final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); 2807 checkIncomplete(h0); 2808 checkIncomplete(h1); 2809 rs[0].assertNotInvoked(); 2810 rs[1].assertNotInvoked(); 2811 if (pushNop) { // ad hoc test of intra-completion interference 2812 m.thenRun(f, () -> {}); 2813 m.thenRun(g, () -> {}); 2814 } 2815 f.complete(v1); 2816 checkCompletedNormally(h0, null); 2817 checkCompletedNormally(h1, null); 2818 rs[0].assertInvoked(); 2819 rs[1].assertInvoked(); 2820 final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); 2821 final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); 2822 checkCompletedNormally(h2, null); 2823 checkCompletedNormally(h3, null); 2824 rs[2].assertInvoked(); 2825 rs[3].assertInvoked(); 2826 2827 g.complete(v2); 2828 2829 final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); 2830 final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); 2831 2832 checkCompletedNormally(f, v1); 2833 checkCompletedNormally(g, v2); 2834 checkCompletedNormally(h0, null); 2835 checkCompletedNormally(h1, null); 2836 checkCompletedNormally(h2, null); 2837 checkCompletedNormally(h3, null); 2838 checkCompletedNormally(h4, null); 2839 checkCompletedNormally(h5, null); 2840 for (int i = 0; i < 6; i++) rs[i].assertInvoked(); 2841 }} 2842 2843 /** 2844 * runAfterEither result completes exceptionally after exceptional 2845 * completion of either source 2846 */ testRunAfterEither_exceptionalCompletion()2847 public void testRunAfterEither_exceptionalCompletion() { 2848 for (ExecutionMode m : ExecutionMode.values()) 2849 for (Item v1 : new Item[] { itemOne, null }) 2850 { 2851 final CompletableFuture<Item> f = new CompletableFuture<>(); 2852 final CompletableFuture<Item> g = new CompletableFuture<>(); 2853 final CFException ex = new CFException(); 2854 final Noop[] rs = new Noop[6]; 2855 for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); 2856 2857 final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); 2858 final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); 2859 checkIncomplete(h0); 2860 checkIncomplete(h1); 2861 rs[0].assertNotInvoked(); 2862 rs[1].assertNotInvoked(); 2863 assertTrue(f.completeExceptionally(ex)); 2864 checkCompletedWithWrappedException(h0, ex); 2865 checkCompletedWithWrappedException(h1, ex); 2866 final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); 2867 final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); 2868 checkCompletedWithWrappedException(h2, ex); 2869 checkCompletedWithWrappedException(h3, ex); 2870 2871 assertTrue(g.complete(v1)); 2872 2873 // unspecified behavior - both source completions available 2874 final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); 2875 final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); 2876 try { 2877 assertNull(h4.join()); 2878 rs[4].assertInvoked(); 2879 } catch (CompletionException ok) { 2880 checkCompletedWithWrappedException(h4, ex); 2881 rs[4].assertNotInvoked(); 2882 } 2883 try { 2884 assertNull(h5.join()); 2885 rs[5].assertInvoked(); 2886 } catch (CompletionException ok) { 2887 checkCompletedWithWrappedException(h5, ex); 2888 rs[5].assertNotInvoked(); 2889 } 2890 2891 checkCompletedExceptionally(f, ex); 2892 checkCompletedNormally(g, v1); 2893 checkCompletedWithWrappedException(h0, ex); 2894 checkCompletedWithWrappedException(h1, ex); 2895 checkCompletedWithWrappedException(h2, ex); 2896 checkCompletedWithWrappedException(h3, ex); 2897 checkCompletedWithWrappedException(h4, ex); 2898 for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); 2899 }} 2900 testRunAfterEither_exceptionalCompletion2()2901 public void testRunAfterEither_exceptionalCompletion2() { 2902 for (ExecutionMode m : ExecutionMode.values()) 2903 for (boolean fFirst : new boolean[] { true, false }) 2904 for (Item v1 : new Item[] { itemOne, null }) 2905 { 2906 final CompletableFuture<Item> f = new CompletableFuture<>(); 2907 final CompletableFuture<Item> g = new CompletableFuture<>(); 2908 final CFException ex = new CFException(); 2909 final Noop[] rs = new Noop[6]; 2910 for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); 2911 2912 final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); 2913 final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); 2914 assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex)); 2915 assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); 2916 final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); 2917 final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); 2918 2919 // unspecified behavior - both source completions available 2920 try { 2921 assertNull(h0.join()); 2922 rs[0].assertInvoked(); 2923 } catch (CompletionException ok) { 2924 checkCompletedWithWrappedException(h0, ex); 2925 rs[0].assertNotInvoked(); 2926 } 2927 try { 2928 assertNull(h1.join()); 2929 rs[1].assertInvoked(); 2930 } catch (CompletionException ok) { 2931 checkCompletedWithWrappedException(h1, ex); 2932 rs[1].assertNotInvoked(); 2933 } 2934 try { 2935 assertNull(h2.join()); 2936 rs[2].assertInvoked(); 2937 } catch (CompletionException ok) { 2938 checkCompletedWithWrappedException(h2, ex); 2939 rs[2].assertNotInvoked(); 2940 } 2941 try { 2942 assertNull(h3.join()); 2943 rs[3].assertInvoked(); 2944 } catch (CompletionException ok) { 2945 checkCompletedWithWrappedException(h3, ex); 2946 rs[3].assertNotInvoked(); 2947 } 2948 2949 checkCompletedNormally(f, v1); 2950 checkCompletedExceptionally(g, ex); 2951 }} 2952 2953 /** 2954 * runAfterEither result completes exceptionally if either source cancelled 2955 */ testRunAfterEither_sourceCancelled()2956 public void testRunAfterEither_sourceCancelled() { 2957 for (ExecutionMode m : ExecutionMode.values()) 2958 for (boolean mayInterruptIfRunning : new boolean[] { true, false }) 2959 for (Item v1 : new Item[] { itemOne, null }) 2960 { 2961 final CompletableFuture<Item> f = new CompletableFuture<>(); 2962 final CompletableFuture<Item> g = new CompletableFuture<>(); 2963 final Noop[] rs = new Noop[6]; 2964 for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); 2965 2966 final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); 2967 final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); 2968 checkIncomplete(h0); 2969 checkIncomplete(h1); 2970 rs[0].assertNotInvoked(); 2971 rs[1].assertNotInvoked(); 2972 f.cancel(mayInterruptIfRunning); 2973 checkCompletedWithWrappedCancellationException(h0); 2974 checkCompletedWithWrappedCancellationException(h1); 2975 final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); 2976 final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); 2977 checkCompletedWithWrappedCancellationException(h2); 2978 checkCompletedWithWrappedCancellationException(h3); 2979 2980 assertTrue(g.complete(v1)); 2981 2982 // unspecified behavior - both source completions available 2983 final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); 2984 final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); 2985 try { 2986 assertNull(h4.join()); 2987 rs[4].assertInvoked(); 2988 } catch (CompletionException ok) { 2989 checkCompletedWithWrappedCancellationException(h4); 2990 rs[4].assertNotInvoked(); 2991 } 2992 try { 2993 assertNull(h5.join()); 2994 rs[5].assertInvoked(); 2995 } catch (CompletionException ok) { 2996 checkCompletedWithWrappedCancellationException(h5); 2997 rs[5].assertNotInvoked(); 2998 } 2999 3000 checkCancelled(f); 3001 checkCompletedNormally(g, v1); 3002 checkCompletedWithWrappedCancellationException(h0); 3003 checkCompletedWithWrappedCancellationException(h1); 3004 checkCompletedWithWrappedCancellationException(h2); 3005 checkCompletedWithWrappedCancellationException(h3); 3006 for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); 3007 }} 3008 3009 /** 3010 * runAfterEither result completes exceptionally if action does 3011 */ testRunAfterEither_actionFailed()3012 public void testRunAfterEither_actionFailed() { 3013 for (ExecutionMode m : ExecutionMode.values()) 3014 for (Item v1 : new Item[] { itemOne, null }) 3015 for (Item v2 : new Item[] { itemTwo, null }) 3016 { 3017 final CompletableFuture<Item> f = new CompletableFuture<>(); 3018 final CompletableFuture<Item> g = new CompletableFuture<>(); 3019 final FailingRunnable[] rs = new FailingRunnable[6]; 3020 for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m); 3021 3022 final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); 3023 final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); 3024 assertTrue(f.complete(v1)); 3025 final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); 3026 final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); 3027 checkCompletedWithWrappedException(h0, rs[0].ex); 3028 checkCompletedWithWrappedException(h1, rs[1].ex); 3029 checkCompletedWithWrappedException(h2, rs[2].ex); 3030 checkCompletedWithWrappedException(h3, rs[3].ex); 3031 for (int i = 0; i < 4; i++) rs[i].assertInvoked(); 3032 assertTrue(g.complete(v2)); 3033 final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); 3034 final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); 3035 checkCompletedWithWrappedException(h4, rs[4].ex); 3036 checkCompletedWithWrappedException(h5, rs[5].ex); 3037 3038 checkCompletedNormally(f, v1); 3039 checkCompletedNormally(g, v2); 3040 for (int i = 0; i < 6; i++) rs[i].assertInvoked(); 3041 }} 3042 3043 /** 3044 * thenCompose result completes normally after normal completion of source 3045 */ testThenCompose_normalCompletion()3046 public void testThenCompose_normalCompletion() { 3047 for (ExecutionMode m : ExecutionMode.values()) 3048 for (boolean createIncomplete : new boolean[] { true, false }) 3049 for (Item v1 : new Item[] { itemOne, null }) 3050 { 3051 final CompletableFuture<Item> f = new CompletableFuture<>(); 3052 final CompletableFutureInc r = new CompletableFutureInc(m); 3053 if (!createIncomplete) assertTrue(f.complete(v1)); 3054 final CompletableFuture<Item> g = m.thenCompose(f, r); 3055 if (createIncomplete) assertTrue(f.complete(v1)); 3056 3057 checkCompletedNormally(g, inc(v1)); 3058 checkCompletedNormally(f, v1); 3059 r.assertValue(v1); 3060 }} 3061 3062 /** 3063 * thenCompose result completes exceptionally after exceptional 3064 * completion of source 3065 */ testThenCompose_exceptionalCompletion()3066 public void testThenCompose_exceptionalCompletion() { 3067 for (ExecutionMode m : ExecutionMode.values()) 3068 for (boolean createIncomplete : new boolean[] { true, false }) 3069 { 3070 final CFException ex = new CFException(); 3071 final CompletableFutureInc r = new CompletableFutureInc(m); 3072 final CompletableFuture<Item> f = new CompletableFuture<>(); 3073 if (!createIncomplete) f.completeExceptionally(ex); 3074 final CompletableFuture<Item> g = m.thenCompose(f, r); 3075 if (createIncomplete) f.completeExceptionally(ex); 3076 3077 checkCompletedWithWrappedException(g, ex); 3078 checkCompletedExceptionally(f, ex); 3079 r.assertNotInvoked(); 3080 }} 3081 3082 /** 3083 * thenCompose result completes exceptionally if action does 3084 */ testThenCompose_actionFailed()3085 public void testThenCompose_actionFailed() { 3086 for (ExecutionMode m : ExecutionMode.values()) 3087 for (boolean createIncomplete : new boolean[] { true, false }) 3088 for (Item v1 : new Item[] { itemOne, null }) 3089 { 3090 final CompletableFuture<Item> f = new CompletableFuture<>(); 3091 final FailingCompletableFutureFunction r 3092 = new FailingCompletableFutureFunction(m); 3093 if (!createIncomplete) assertTrue(f.complete(v1)); 3094 final CompletableFuture<Item> g = m.thenCompose(f, r); 3095 if (createIncomplete) assertTrue(f.complete(v1)); 3096 3097 checkCompletedWithWrappedException(g, r.ex); 3098 checkCompletedNormally(f, v1); 3099 }} 3100 3101 /** 3102 * thenCompose result completes exceptionally if source cancelled 3103 */ testThenCompose_sourceCancelled()3104 public void testThenCompose_sourceCancelled() { 3105 for (ExecutionMode m : ExecutionMode.values()) 3106 for (boolean createIncomplete : new boolean[] { true, false }) 3107 for (boolean mayInterruptIfRunning : new boolean[] { true, false }) 3108 { 3109 final CompletableFuture<Item> f = new CompletableFuture<>(); 3110 final CompletableFutureInc r = new CompletableFutureInc(m); 3111 if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); 3112 final CompletableFuture<Item> g = m.thenCompose(f, r); 3113 if (createIncomplete) { 3114 checkIncomplete(g); 3115 assertTrue(f.cancel(mayInterruptIfRunning)); 3116 } 3117 3118 checkCompletedWithWrappedCancellationException(g); 3119 checkCancelled(f); 3120 }} 3121 3122 /** 3123 * thenCompose result completes exceptionally if the result of the action does 3124 */ testThenCompose_actionReturnsFailingFuture()3125 public void testThenCompose_actionReturnsFailingFuture() { 3126 for (ExecutionMode m : ExecutionMode.values()) 3127 for (int order = 0; order < 6; order++) 3128 for (Item v1 : new Item[] { itemOne, null }) 3129 { 3130 final CFException ex = new CFException(); 3131 final CompletableFuture<Item> f = new CompletableFuture<>(); 3132 final CompletableFuture<Item> g = new CompletableFuture<>(); 3133 final CompletableFuture<Item> h; 3134 // Test all permutations of orders 3135 switch (order) { 3136 case 0: 3137 assertTrue(f.complete(v1)); 3138 assertTrue(g.completeExceptionally(ex)); 3139 h = m.thenCompose(f, x -> g); 3140 break; 3141 case 1: 3142 assertTrue(f.complete(v1)); 3143 h = m.thenCompose(f, x -> g); 3144 assertTrue(g.completeExceptionally(ex)); 3145 break; 3146 case 2: 3147 assertTrue(g.completeExceptionally(ex)); 3148 assertTrue(f.complete(v1)); 3149 h = m.thenCompose(f, x -> g); 3150 break; 3151 case 3: 3152 assertTrue(g.completeExceptionally(ex)); 3153 h = m.thenCompose(f, x -> g); 3154 assertTrue(f.complete(v1)); 3155 break; 3156 case 4: 3157 h = m.thenCompose(f, x -> g); 3158 assertTrue(f.complete(v1)); 3159 assertTrue(g.completeExceptionally(ex)); 3160 break; 3161 case 5: 3162 h = m.thenCompose(f, x -> g); 3163 assertTrue(f.complete(v1)); 3164 assertTrue(g.completeExceptionally(ex)); 3165 break; 3166 default: throw new AssertionError(); 3167 } 3168 3169 checkCompletedExceptionally(g, ex); 3170 checkCompletedWithWrappedException(h, ex); 3171 checkCompletedNormally(f, v1); 3172 }} 3173 3174 /** 3175 * exceptionallyCompose result completes normally after normal 3176 * completion of source 3177 */ testExceptionallyCompose_normalCompletion()3178 public void testExceptionallyCompose_normalCompletion() { 3179 for (ExecutionMode m : ExecutionMode.values()) 3180 for (boolean createIncomplete : new boolean[] { true, false }) 3181 for (Item v1 : new Item[] { itemOne, null }) 3182 { 3183 final CompletableFuture<Item> f = new CompletableFuture<>(); 3184 final ExceptionalCompletableFutureFunction r = 3185 new ExceptionalCompletableFutureFunction(m); 3186 if (!createIncomplete) assertTrue(f.complete(v1)); 3187 final CompletableFuture<Item> g = m.exceptionallyCompose(f, r); 3188 if (createIncomplete) assertTrue(f.complete(v1)); 3189 3190 checkCompletedNormally(f, v1); 3191 checkCompletedNormally(g, v1); 3192 r.assertNotInvoked(); 3193 }} 3194 3195 /** 3196 * exceptionallyCompose result completes normally after exceptional 3197 * completion of source 3198 */ testExceptionallyCompose_exceptionalCompletion()3199 public void testExceptionallyCompose_exceptionalCompletion() { 3200 for (ExecutionMode m : ExecutionMode.values()) 3201 for (boolean createIncomplete : new boolean[] { true, false }) 3202 { 3203 final CFException ex = new CFException(); 3204 final ExceptionalCompletableFutureFunction r = 3205 new ExceptionalCompletableFutureFunction(m); 3206 final CompletableFuture<Item> f = new CompletableFuture<>(); 3207 if (!createIncomplete) f.completeExceptionally(ex); 3208 final CompletableFuture<Item> g = m.exceptionallyCompose(f, r); 3209 if (createIncomplete) f.completeExceptionally(ex); 3210 3211 checkCompletedExceptionally(f, ex); 3212 checkCompletedNormally(g, r.value); 3213 r.assertInvoked(); 3214 }} 3215 3216 /** 3217 * exceptionallyCompose completes exceptionally on exception if action does 3218 */ testExceptionallyCompose_actionFailed()3219 public void testExceptionallyCompose_actionFailed() { 3220 for (ExecutionMode m : ExecutionMode.values()) 3221 for (boolean createIncomplete : new boolean[] { true, false }) 3222 { 3223 final CFException ex = new CFException(); 3224 final CompletableFuture<Item> f = new CompletableFuture<>(); 3225 final FailingExceptionalCompletableFutureFunction r 3226 = new FailingExceptionalCompletableFutureFunction(m); 3227 if (!createIncomplete) f.completeExceptionally(ex); 3228 final CompletableFuture<Item> g = m.exceptionallyCompose(f, r); 3229 if (createIncomplete) f.completeExceptionally(ex); 3230 3231 checkCompletedExceptionally(f, ex); 3232 checkCompletedWithWrappedException(g, r.ex); 3233 r.assertInvoked(); 3234 }} 3235 3236 /** 3237 * exceptionallyCompose result completes exceptionally if the 3238 * result of the action does 3239 */ testExceptionallyCompose_actionReturnsFailingFuture()3240 public void testExceptionallyCompose_actionReturnsFailingFuture() { 3241 for (ExecutionMode m : ExecutionMode.values()) 3242 for (int order = 0; order < 6; order++) 3243 { 3244 final CFException ex0 = new CFException(); 3245 final CFException ex = new CFException(); 3246 final CompletableFuture<Item> f = new CompletableFuture<>(); 3247 final CompletableFuture<Item> g = new CompletableFuture<>(); 3248 final CompletableFuture<Item> h; 3249 // Test all permutations of orders 3250 switch (order) { 3251 case 0: 3252 assertTrue(f.completeExceptionally(ex0)); 3253 assertTrue(g.completeExceptionally(ex)); 3254 h = m.exceptionallyCompose(f, x -> g); 3255 break; 3256 case 1: 3257 assertTrue(f.completeExceptionally(ex0)); 3258 h = m.exceptionallyCompose(f, x -> g); 3259 assertTrue(g.completeExceptionally(ex)); 3260 break; 3261 case 2: 3262 assertTrue(g.completeExceptionally(ex)); 3263 assertTrue(f.completeExceptionally(ex0)); 3264 h = m.exceptionallyCompose(f, x -> g); 3265 break; 3266 case 3: 3267 assertTrue(g.completeExceptionally(ex)); 3268 h = m.exceptionallyCompose(f, x -> g); 3269 assertTrue(f.completeExceptionally(ex0)); 3270 break; 3271 case 4: 3272 h = m.exceptionallyCompose(f, x -> g); 3273 assertTrue(f.completeExceptionally(ex0)); 3274 assertTrue(g.completeExceptionally(ex)); 3275 break; 3276 case 5: 3277 h = m.exceptionallyCompose(f, x -> g); 3278 assertTrue(f.completeExceptionally(ex0)); 3279 assertTrue(g.completeExceptionally(ex)); 3280 break; 3281 default: throw new AssertionError(); 3282 } 3283 3284 checkCompletedExceptionally(g, ex); 3285 checkCompletedWithWrappedException(h, ex); 3286 checkCompletedExceptionally(f, ex0); 3287 }} 3288 3289 // other static methods 3290 3291 /** 3292 * allOf(no component futures) returns a future completed normally 3293 * with the value null 3294 */ testAllOf_empty()3295 public void testAllOf_empty() throws Exception { 3296 CompletableFuture<Void> f = CompletableFuture.allOf(); 3297 checkCompletedNormally(f, null); 3298 } 3299 3300 /** 3301 * allOf returns a future completed normally with the value null 3302 * when all components complete normally 3303 */ testAllOf_normal()3304 public void testAllOf_normal() throws Exception { 3305 for (int k = 1; k < 10; k++) { 3306 @SuppressWarnings("unchecked") 3307 CompletableFuture<Item>[] fs 3308 = (CompletableFuture<Item>[]) new CompletableFuture[k]; 3309 for (int i = 0; i < k; i++) 3310 fs[i] = new CompletableFuture<>(); 3311 CompletableFuture<Void> f = CompletableFuture.allOf(fs); 3312 for (int i = 0; i < k; i++) { 3313 checkIncomplete(f); 3314 checkIncomplete(CompletableFuture.allOf(fs)); 3315 fs[i].complete(itemOne); 3316 } 3317 checkCompletedNormally(f, null); 3318 checkCompletedNormally(CompletableFuture.allOf(fs), null); 3319 } 3320 } 3321 testAllOf_normal_backwards()3322 public void testAllOf_normal_backwards() throws Exception { 3323 for (int k = 1; k < 10; k++) { 3324 @SuppressWarnings("unchecked") 3325 CompletableFuture<Item>[] fs 3326 = (CompletableFuture<Item>[]) new CompletableFuture[k]; 3327 for (int i = 0; i < k; i++) 3328 fs[i] = new CompletableFuture<>(); 3329 CompletableFuture<Void> f = CompletableFuture.allOf(fs); 3330 for (int i = k - 1; i >= 0; i--) { 3331 checkIncomplete(f); 3332 checkIncomplete(CompletableFuture.allOf(fs)); 3333 fs[i].complete(itemOne); 3334 } 3335 checkCompletedNormally(f, null); 3336 checkCompletedNormally(CompletableFuture.allOf(fs), null); 3337 } 3338 } 3339 testAllOf_exceptional()3340 public void testAllOf_exceptional() throws Exception { 3341 for (int k = 1; k < 10; k++) { 3342 @SuppressWarnings("unchecked") 3343 CompletableFuture<Item>[] fs 3344 = (CompletableFuture<Item>[]) new CompletableFuture[k]; 3345 CFException ex = new CFException(); 3346 for (int i = 0; i < k; i++) 3347 fs[i] = new CompletableFuture<>(); 3348 CompletableFuture<Void> f = CompletableFuture.allOf(fs); 3349 for (int i = 0; i < k; i++) { 3350 Item I = itemFor(i); 3351 checkIncomplete(f); 3352 checkIncomplete(CompletableFuture.allOf(fs)); 3353 if (i != k / 2) { 3354 fs[i].complete(I); 3355 checkCompletedNormally(fs[i], I); 3356 } else { 3357 fs[i].completeExceptionally(ex); 3358 checkCompletedExceptionally(fs[i], ex); 3359 } 3360 } 3361 checkCompletedWithWrappedException(f, ex); 3362 checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex); 3363 } 3364 } 3365 3366 /** 3367 * anyOf(no component futures) returns an incomplete future 3368 */ testAnyOf_empty()3369 public void testAnyOf_empty() throws Exception { 3370 for (Item v1 : new Item[] { itemOne, null }) 3371 { 3372 CompletableFuture<Object> f = CompletableFuture.anyOf(); 3373 checkIncomplete(f); 3374 3375 f.complete(v1); 3376 checkCompletedNormally(f, v1); 3377 }} 3378 3379 /** 3380 * anyOf returns a future completed normally with a value when 3381 * a component future does 3382 */ testAnyOf_normal()3383 public void testAnyOf_normal() throws Exception { 3384 for (int k = 0; k < 10; k++) { 3385 @SuppressWarnings("unchecked") 3386 CompletableFuture<Item>[] fs = 3387 (CompletableFuture<Item>[])new CompletableFuture[k]; 3388 for (int i = 0; i < k; i++) 3389 fs[i] = new CompletableFuture<>(); 3390 CompletableFuture<Object> f = CompletableFuture.anyOf(fs); 3391 checkIncomplete(f); 3392 for (int i = 0; i < k; i++) { 3393 fs[i].complete(itemFor(i)); 3394 checkCompletedNormally(f, itemZero); 3395 Item x = (Item)CompletableFuture.anyOf(fs).join(); 3396 assertTrue(0 <= x.value && x.value <= i); 3397 } 3398 } 3399 } testAnyOf_normal_backwards()3400 public void testAnyOf_normal_backwards() throws Exception { 3401 for (int k = 0; k < 10; k++) { 3402 @SuppressWarnings("unchecked") 3403 CompletableFuture<Item>[] fs = 3404 (CompletableFuture<Item>[])new CompletableFuture[k]; 3405 for (int i = 0; i < k; i++) 3406 fs[i] = new CompletableFuture<>(); 3407 CompletableFuture<Object> f = CompletableFuture.anyOf(fs); 3408 checkIncomplete(f); 3409 for (int i = k - 1; i >= 0; i--) { 3410 fs[i].complete(itemFor(i)); 3411 checkCompletedNormally(f, itemFor(k - 1)); 3412 Item x = (Item)CompletableFuture.anyOf(fs).join(); 3413 assertTrue(i <= x.value && x.value <= k - 1); 3414 } 3415 } 3416 } 3417 3418 /** 3419 * anyOf result completes exceptionally when any component does. 3420 */ testAnyOf_exceptional()3421 public void testAnyOf_exceptional() throws Exception { 3422 for (int k = 0; k < 10; k++) { 3423 @SuppressWarnings("unchecked") 3424 CompletableFuture<Item>[] fs = 3425 (CompletableFuture<Item>[])new CompletableFuture[k]; 3426 CFException[] exs = new CFException[k]; 3427 for (int i = 0; i < k; i++) { 3428 fs[i] = new CompletableFuture<>(); 3429 exs[i] = new CFException(); 3430 } 3431 CompletableFuture<Object> f = CompletableFuture.anyOf(fs); 3432 checkIncomplete(f); 3433 for (int i = 0; i < k; i++) { 3434 fs[i].completeExceptionally(exs[i]); 3435 checkCompletedWithWrappedException(f, exs[0]); 3436 checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); 3437 } 3438 } 3439 } 3440 testAnyOf_exceptional_backwards()3441 public void testAnyOf_exceptional_backwards() throws Exception { 3442 for (int k = 0; k < 10; k++) { 3443 @SuppressWarnings("unchecked") 3444 CompletableFuture<Object>[] fs = 3445 (CompletableFuture<Object>[])new CompletableFuture[k]; 3446 CFException[] exs = new CFException[k]; 3447 for (int i = 0; i < k; i++) { 3448 fs[i] = new CompletableFuture<>(); 3449 exs[i] = new CFException(); 3450 } 3451 CompletableFuture<Object> f = CompletableFuture.anyOf(fs); 3452 checkIncomplete(f); 3453 for (int i = k - 1; i >= 0; i--) { 3454 fs[i].completeExceptionally(exs[i]); 3455 checkCompletedWithWrappedException(f, exs[k - 1]); 3456 checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); 3457 } 3458 } 3459 } 3460 3461 /** 3462 * Completion methods throw NullPointerException with null arguments 3463 */ 3464 @SuppressWarnings("FutureReturnValueIgnored") testNPE()3465 public void testNPE() { 3466 CompletableFuture<Item> f = new CompletableFuture<>(); 3467 CompletableFuture<Item> g = new CompletableFuture<>(); 3468 CompletableFuture<Item> nullFuture = (CompletableFuture<Item>)null; 3469 ThreadExecutor exec = new ThreadExecutor(); 3470 3471 assertThrows( 3472 NullPointerException.class, 3473 3474 () -> CompletableFuture.supplyAsync(null), 3475 () -> CompletableFuture.supplyAsync(null, exec), 3476 () -> CompletableFuture.supplyAsync(new ItemSupplier(ExecutionMode.SYNC, fortytwo), null), 3477 3478 () -> CompletableFuture.runAsync(null), 3479 () -> CompletableFuture.runAsync(null, exec), 3480 () -> CompletableFuture.runAsync(() -> {}, null), 3481 3482 () -> f.completeExceptionally(null), 3483 3484 () -> f.thenApply(null), 3485 () -> f.thenApplyAsync(null), 3486 () -> f.thenApplyAsync(x -> x, null), 3487 () -> f.thenApplyAsync(null, exec), 3488 3489 () -> f.thenAccept(null), 3490 () -> f.thenAcceptAsync(null), 3491 () -> f.thenAcceptAsync(x -> {} , null), 3492 () -> f.thenAcceptAsync(null, exec), 3493 3494 () -> f.thenRun(null), 3495 () -> f.thenRunAsync(null), 3496 () -> f.thenRunAsync(() -> {} , null), 3497 () -> f.thenRunAsync(null, exec), 3498 3499 () -> f.thenCombine(g, null), 3500 () -> f.thenCombineAsync(g, null), 3501 () -> f.thenCombineAsync(g, null, exec), 3502 () -> f.thenCombine(nullFuture, (x, y) -> x), 3503 () -> f.thenCombineAsync(nullFuture, (x, y) -> x), 3504 () -> f.thenCombineAsync(nullFuture, (x, y) -> x, exec), 3505 () -> f.thenCombineAsync(g, (x, y) -> x, null), 3506 3507 () -> f.thenAcceptBoth(g, null), 3508 () -> f.thenAcceptBothAsync(g, null), 3509 () -> f.thenAcceptBothAsync(g, null, exec), 3510 () -> f.thenAcceptBoth(nullFuture, (x, y) -> {}), 3511 () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}), 3512 () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec), 3513 () -> f.thenAcceptBothAsync(g, (x, y) -> {}, null), 3514 3515 () -> f.runAfterBoth(g, null), 3516 () -> f.runAfterBothAsync(g, null), 3517 () -> f.runAfterBothAsync(g, null, exec), 3518 () -> f.runAfterBoth(nullFuture, () -> {}), 3519 () -> f.runAfterBothAsync(nullFuture, () -> {}), 3520 () -> f.runAfterBothAsync(nullFuture, () -> {}, exec), 3521 () -> f.runAfterBothAsync(g, () -> {}, null), 3522 3523 () -> f.applyToEither(g, null), 3524 () -> f.applyToEitherAsync(g, null), 3525 () -> f.applyToEitherAsync(g, null, exec), 3526 () -> f.applyToEither(nullFuture, x -> x), 3527 () -> f.applyToEitherAsync(nullFuture, x -> x), 3528 () -> f.applyToEitherAsync(nullFuture, x -> x, exec), 3529 () -> f.applyToEitherAsync(g, x -> x, null), 3530 3531 () -> f.acceptEither(g, null), 3532 () -> f.acceptEitherAsync(g, null), 3533 () -> f.acceptEitherAsync(g, null, exec), 3534 () -> f.acceptEither(nullFuture, x -> {}), 3535 () -> f.acceptEitherAsync(nullFuture, x -> {}), 3536 () -> f.acceptEitherAsync(nullFuture, x -> {}, exec), 3537 () -> f.acceptEitherAsync(g, x -> {}, null), 3538 3539 () -> f.runAfterEither(g, null), 3540 () -> f.runAfterEitherAsync(g, null), 3541 () -> f.runAfterEitherAsync(g, null, exec), 3542 () -> f.runAfterEither(nullFuture, () -> {}), 3543 () -> f.runAfterEitherAsync(nullFuture, () -> {}), 3544 () -> f.runAfterEitherAsync(nullFuture, () -> {}, exec), 3545 () -> f.runAfterEitherAsync(g, () -> {}, null), 3546 3547 () -> f.thenCompose(null), 3548 () -> f.thenComposeAsync(null), 3549 () -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null), 3550 () -> f.thenComposeAsync(null, exec), 3551 3552 () -> f.exceptionally(null), 3553 3554 () -> f.handle(null), 3555 3556 () -> CompletableFuture.allOf((CompletableFuture<?>)null), 3557 () -> CompletableFuture.allOf((CompletableFuture<?>[])null), 3558 () -> CompletableFuture.allOf(f, null), 3559 () -> CompletableFuture.allOf(null, f), 3560 3561 () -> CompletableFuture.anyOf((CompletableFuture<?>)null), 3562 () -> CompletableFuture.anyOf((CompletableFuture<?>[])null), 3563 () -> CompletableFuture.anyOf(f, null), 3564 () -> CompletableFuture.anyOf(null, f), 3565 3566 () -> f.obtrudeException(null), 3567 3568 () -> CompletableFuture.delayedExecutor(1L, SECONDS, null), 3569 () -> CompletableFuture.delayedExecutor(1L, null, exec), 3570 () -> CompletableFuture.delayedExecutor(1L, null), 3571 3572 () -> f.orTimeout(1L, null), 3573 () -> f.completeOnTimeout(fortytwo, 1L, null), 3574 3575 () -> CompletableFuture.failedFuture(null), 3576 () -> CompletableFuture.failedStage(null)); 3577 3578 mustEqual(0, exec.count.get()); 3579 } 3580 3581 /** 3582 * Test submissions to an executor that rejects all tasks. 3583 */ testRejectingExecutor()3584 public void testRejectingExecutor() { 3585 for (Item v : new Item[] { itemOne, null }) 3586 { 3587 final CountingRejectingExecutor e = new CountingRejectingExecutor(); 3588 3589 final CompletableFuture<Item> complete = CompletableFuture.completedFuture(v); 3590 final CompletableFuture<Item> incomplete = new CompletableFuture<>(); 3591 3592 List<CompletableFuture<?>> futures = new ArrayList<>(); 3593 3594 List<CompletableFuture<Item>> srcs = new ArrayList<>(); 3595 srcs.add(complete); 3596 srcs.add(incomplete); 3597 3598 for (CompletableFuture<Item> src : srcs) { 3599 List<CompletableFuture<?>> fs = new ArrayList<>(); 3600 fs.add(src.thenRunAsync(() -> {}, e)); 3601 fs.add(src.thenAcceptAsync(z -> {}, e)); 3602 fs.add(src.thenApplyAsync(z -> z, e)); 3603 3604 fs.add(src.thenCombineAsync(src, (x, y) -> x, e)); 3605 fs.add(src.thenAcceptBothAsync(src, (x, y) -> {}, e)); 3606 fs.add(src.runAfterBothAsync(src, () -> {}, e)); 3607 3608 fs.add(src.applyToEitherAsync(src, z -> z, e)); 3609 fs.add(src.acceptEitherAsync(src, z -> {}, e)); 3610 fs.add(src.runAfterEitherAsync(src, () -> {}, e)); 3611 3612 fs.add(src.thenComposeAsync(z -> null, e)); 3613 fs.add(src.whenCompleteAsync((z, t) -> {}, e)); 3614 fs.add(src.handleAsync((z, t) -> null, e)); 3615 3616 for (CompletableFuture<?> future : fs) { 3617 if (src.isDone()) 3618 checkCompletedWithWrappedException(future, e.ex); 3619 else 3620 checkIncomplete(future); 3621 } 3622 futures.addAll(fs); 3623 } 3624 3625 { 3626 List<CompletableFuture<?>> fs = new ArrayList<>(); 3627 3628 fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e)); 3629 fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e)); 3630 3631 fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); 3632 fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e)); 3633 3634 fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e)); 3635 fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e)); 3636 3637 for (CompletableFuture<?> future : fs) 3638 checkIncomplete(future); 3639 futures.addAll(fs); 3640 } 3641 3642 { 3643 List<CompletableFuture<?>> fs = new ArrayList<>(); 3644 3645 fs.add(complete.applyToEitherAsync(incomplete, z -> z, e)); 3646 fs.add(incomplete.applyToEitherAsync(complete, z -> z, e)); 3647 3648 fs.add(complete.acceptEitherAsync(incomplete, z -> {}, e)); 3649 fs.add(incomplete.acceptEitherAsync(complete, z -> {}, e)); 3650 3651 fs.add(complete.runAfterEitherAsync(incomplete, () -> {}, e)); 3652 fs.add(incomplete.runAfterEitherAsync(complete, () -> {}, e)); 3653 3654 for (CompletableFuture<?> future : fs) 3655 checkCompletedWithWrappedException(future, e.ex); 3656 futures.addAll(fs); 3657 } 3658 3659 incomplete.complete(v); 3660 3661 for (CompletableFuture<?> future : futures) 3662 checkCompletedWithWrappedException(future, e.ex); 3663 3664 mustEqual(futures.size(), e.count.get()); 3665 }} 3666 3667 /** 3668 * Test submissions to an executor that rejects all tasks, but 3669 * should never be invoked because the dependent future is 3670 * explicitly completed. 3671 */ testRejectingExecutorNeverInvoked()3672 public void testRejectingExecutorNeverInvoked() { 3673 for (Item v : new Item[] { itemOne, null }) 3674 { 3675 final CountingRejectingExecutor e = new CountingRejectingExecutor(); 3676 3677 final CompletableFuture<Item> complete = CompletableFuture.completedFuture(v); 3678 final CompletableFuture<Item> incomplete = new CompletableFuture<>(); 3679 3680 List<CompletableFuture<?>> fs = new ArrayList<>(); 3681 fs.add(incomplete.thenRunAsync(() -> {}, e)); 3682 fs.add(incomplete.thenAcceptAsync(z -> {}, e)); 3683 fs.add(incomplete.thenApplyAsync(z -> z, e)); 3684 3685 fs.add(incomplete.thenCombineAsync(incomplete, (x, y) -> x, e)); 3686 fs.add(incomplete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); 3687 fs.add(incomplete.runAfterBothAsync(incomplete, () -> {}, e)); 3688 3689 fs.add(incomplete.applyToEitherAsync(incomplete, z -> z, e)); 3690 fs.add(incomplete.acceptEitherAsync(incomplete, z -> {}, e)); 3691 fs.add(incomplete.runAfterEitherAsync(incomplete, () -> {}, e)); 3692 3693 fs.add(incomplete.thenComposeAsync(z -> null, e)); 3694 fs.add(incomplete.whenCompleteAsync((z, t) -> {}, e)); 3695 fs.add(incomplete.handleAsync((z, t) -> null, e)); 3696 3697 fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e)); 3698 fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e)); 3699 3700 fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); 3701 fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e)); 3702 3703 fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e)); 3704 fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e)); 3705 3706 for (CompletableFuture<?> future : fs) 3707 checkIncomplete(future); 3708 3709 for (CompletableFuture<?> future : fs) 3710 future.complete(null); 3711 3712 incomplete.complete(v); 3713 3714 for (CompletableFuture<?> future : fs) 3715 checkCompletedNormally(future, null); 3716 3717 mustEqual(0, e.count.get()); 3718 }} 3719 3720 /** 3721 * toCompletableFuture returns this CompletableFuture. 3722 */ testToCompletableFuture()3723 public void testToCompletableFuture() { 3724 CompletableFuture<Item> f = new CompletableFuture<>(); 3725 assertSame(f, f.toCompletableFuture()); 3726 } 3727 3728 // jdk9 3729 3730 /** 3731 * newIncompleteFuture returns an incomplete CompletableFuture 3732 */ testNewIncompleteFuture()3733 public void testNewIncompleteFuture() { 3734 for (Item v1 : new Item[] { itemOne, null }) 3735 { 3736 CompletableFuture<Item> f = new CompletableFuture<>(); 3737 CompletableFuture<Item> g = f.newIncompleteFuture(); 3738 checkIncomplete(f); 3739 checkIncomplete(g); 3740 f.complete(v1); 3741 checkCompletedNormally(f, v1); 3742 checkIncomplete(g); 3743 g.complete(v1); 3744 checkCompletedNormally(g, v1); 3745 assertSame(g.getClass(), CompletableFuture.class); 3746 }} 3747 3748 /** 3749 * completedStage returns a completed CompletionStage 3750 */ testCompletedStage()3751 public void testCompletedStage() { 3752 AtomicInteger x = new AtomicInteger(0); 3753 AtomicReference<Throwable> r = new AtomicReference<>(); 3754 CompletionStage<Item> f = CompletableFuture.completedStage(itemOne); 3755 f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v.value);}); 3756 mustEqual(x.get(), 1); 3757 assertNull(r.get()); 3758 } 3759 3760 /** 3761 * defaultExecutor by default returns the commonPool if 3762 * it supports more than one thread. 3763 */ testDefaultExecutor()3764 public void testDefaultExecutor() { 3765 CompletableFuture<Item> f = new CompletableFuture<>(); 3766 Executor e = f.defaultExecutor(); 3767 Executor c = ForkJoinPool.commonPool(); 3768 if (ForkJoinPool.getCommonPoolParallelism() > 1) 3769 assertSame(e, c); 3770 else 3771 assertNotSame(e, c); 3772 } 3773 3774 /** 3775 * failedFuture returns a CompletableFuture completed 3776 * exceptionally with the given Exception 3777 */ testFailedFuture()3778 public void testFailedFuture() { 3779 CFException ex = new CFException(); 3780 CompletableFuture<Item> f = CompletableFuture.failedFuture(ex); 3781 checkCompletedExceptionally(f, ex); 3782 } 3783 3784 /** 3785 * copy returns a CompletableFuture that is completed normally, 3786 * with the same value, when source is. 3787 */ testCopy_normalCompletion()3788 public void testCopy_normalCompletion() { 3789 for (boolean createIncomplete : new boolean[] { true, false }) 3790 for (Item v1 : new Item[] { itemOne, null }) 3791 { 3792 CompletableFuture<Item> f = new CompletableFuture<>(); 3793 if (!createIncomplete) assertTrue(f.complete(v1)); 3794 CompletableFuture<Item> g = f.copy(); 3795 if (createIncomplete) { 3796 checkIncomplete(f); 3797 checkIncomplete(g); 3798 assertTrue(f.complete(v1)); 3799 } 3800 checkCompletedNormally(f, v1); 3801 checkCompletedNormally(g, v1); 3802 }} 3803 3804 /** 3805 * copy returns a CompletableFuture that is completed exceptionally 3806 * when source is. 3807 */ testCopy_exceptionalCompletion()3808 public void testCopy_exceptionalCompletion() { 3809 for (boolean createIncomplete : new boolean[] { true, false }) 3810 { 3811 CFException ex = new CFException(); 3812 CompletableFuture<Item> f = new CompletableFuture<>(); 3813 if (!createIncomplete) f.completeExceptionally(ex); 3814 CompletableFuture<Item> g = f.copy(); 3815 if (createIncomplete) { 3816 checkIncomplete(f); 3817 checkIncomplete(g); 3818 f.completeExceptionally(ex); 3819 } 3820 checkCompletedExceptionally(f, ex); 3821 checkCompletedWithWrappedException(g, ex); 3822 }} 3823 3824 /** 3825 * Completion of a copy does not complete its source. 3826 */ testCopy_oneWayPropagation()3827 public void testCopy_oneWayPropagation() { 3828 CompletableFuture<Item> f = new CompletableFuture<>(); 3829 assertTrue(f.copy().complete(itemOne)); 3830 assertTrue(f.copy().complete(null)); 3831 assertTrue(f.copy().cancel(true)); 3832 assertTrue(f.copy().cancel(false)); 3833 assertTrue(f.copy().completeExceptionally(new CFException())); 3834 checkIncomplete(f); 3835 } 3836 3837 /** 3838 * minimalCompletionStage returns a CompletableFuture that is 3839 * completed normally, with the same value, when source is. 3840 */ testMinimalCompletionStage()3841 public void testMinimalCompletionStage() { 3842 CompletableFuture<Item> f = new CompletableFuture<>(); 3843 CompletionStage<Item> g = f.minimalCompletionStage(); 3844 AtomicInteger x = new AtomicInteger(0); 3845 AtomicReference<Throwable> r = new AtomicReference<>(); 3846 checkIncomplete(f); 3847 g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v.value);}); 3848 f.complete(itemOne); 3849 checkCompletedNormally(f, itemOne); 3850 mustEqual(x.get(), 1); 3851 assertNull(r.get()); 3852 } 3853 3854 /** 3855 * minimalCompletionStage returns a CompletableFuture that is 3856 * completed exceptionally when source is. 3857 */ testMinimalCompletionStage2()3858 public void testMinimalCompletionStage2() { 3859 CompletableFuture<Item> f = new CompletableFuture<>(); 3860 CompletionStage<Item> g = f.minimalCompletionStage(); 3861 AtomicInteger x = new AtomicInteger(0); 3862 AtomicReference<Throwable> r = new AtomicReference<>(); 3863 g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v.value);}); 3864 checkIncomplete(f); 3865 CFException ex = new CFException(); 3866 f.completeExceptionally(ex); 3867 checkCompletedExceptionally(f, ex); 3868 mustEqual(x.get(), 0); 3869 mustEqual(r.get().getCause(), ex); 3870 } 3871 3872 /** 3873 * failedStage returns a CompletionStage completed 3874 * exceptionally with the given Exception 3875 */ testFailedStage()3876 public void testFailedStage() { 3877 CFException ex = new CFException(); 3878 CompletionStage<Item> f = CompletableFuture.failedStage(ex); 3879 AtomicInteger x = new AtomicInteger(0); 3880 AtomicReference<Throwable> r = new AtomicReference<>(); 3881 f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v.value);}); 3882 mustEqual(x.get(), 0); 3883 mustEqual(r.get(), ex); 3884 } 3885 3886 /** 3887 * completeAsync completes with value of given supplier 3888 */ testCompleteAsync()3889 public void testCompleteAsync() { 3890 for (Item v1 : new Item[] { itemOne, null }) 3891 { 3892 CompletableFuture<Item> f = new CompletableFuture<>(); 3893 f.completeAsync(() -> v1); 3894 f.join(); 3895 checkCompletedNormally(f, v1); 3896 }} 3897 3898 /** 3899 * completeAsync completes exceptionally if given supplier throws 3900 */ testCompleteAsync2()3901 public void testCompleteAsync2() { 3902 CompletableFuture<Item> f = new CompletableFuture<>(); 3903 CFException ex = new CFException(); 3904 f.completeAsync(() -> { throw ex; }); 3905 try { 3906 f.join(); 3907 shouldThrow(); 3908 } catch (CompletionException success) {} 3909 checkCompletedWithWrappedException(f, ex); 3910 } 3911 3912 /** 3913 * completeAsync with given executor completes with value of given supplier 3914 */ testCompleteAsync3()3915 public void testCompleteAsync3() { 3916 for (Item v1 : new Item[] { itemOne, null }) 3917 { 3918 CompletableFuture<Item> f = new CompletableFuture<>(); 3919 ThreadExecutor executor = new ThreadExecutor(); 3920 f.completeAsync(() -> v1, executor); 3921 assertSame(v1, f.join()); 3922 checkCompletedNormally(f, v1); 3923 mustEqual(1, executor.count.get()); 3924 }} 3925 3926 /** 3927 * completeAsync with given executor completes exceptionally if 3928 * given supplier throws 3929 */ testCompleteAsync4()3930 public void testCompleteAsync4() { 3931 CompletableFuture<Item> f = new CompletableFuture<>(); 3932 CFException ex = new CFException(); 3933 ThreadExecutor executor = new ThreadExecutor(); 3934 f.completeAsync(() -> { throw ex; }, executor); 3935 try { 3936 f.join(); 3937 shouldThrow(); 3938 } catch (CompletionException success) {} 3939 checkCompletedWithWrappedException(f, ex); 3940 mustEqual(1, executor.count.get()); 3941 } 3942 3943 /** 3944 * orTimeout completes with TimeoutException if not complete 3945 */ testOrTimeout_timesOut()3946 public void testOrTimeout_timesOut() { 3947 long timeoutMillis = timeoutMillis(); 3948 CompletableFuture<Item> f = new CompletableFuture<>(); 3949 long startTime = System.nanoTime(); 3950 assertSame(f, f.orTimeout(timeoutMillis, MILLISECONDS)); 3951 checkCompletedWithTimeoutException(f); 3952 assertTrue(millisElapsedSince(startTime) >= timeoutMillis); 3953 } 3954 3955 /** 3956 * orTimeout completes normally if completed before timeout 3957 */ testOrTimeout_completed()3958 public void testOrTimeout_completed() { 3959 for (Item v1 : new Item[] { itemOne, null }) 3960 { 3961 CompletableFuture<Item> f = new CompletableFuture<>(); 3962 CompletableFuture<Item> g = new CompletableFuture<>(); 3963 long startTime = System.nanoTime(); 3964 f.complete(v1); 3965 assertSame(f, f.orTimeout(LONG_DELAY_MS, MILLISECONDS)); 3966 assertSame(g, g.orTimeout(LONG_DELAY_MS, MILLISECONDS)); 3967 g.complete(v1); 3968 checkCompletedNormally(f, v1); 3969 checkCompletedNormally(g, v1); 3970 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); 3971 }} 3972 3973 /** 3974 * completeOnTimeout completes with given value if not complete 3975 */ 3976 public void testCompleteOnTimeout_timesOut() { 3977 testInParallel(() -> testCompleteOnTimeout_timesOut(fortytwo), 3978 () -> testCompleteOnTimeout_timesOut(null)); 3979 } 3980 3981 /** 3982 * completeOnTimeout completes with given value if not complete 3983 */ testCompleteOnTimeout_timesOut(Item v)3984 public void testCompleteOnTimeout_timesOut(Item v) { 3985 long timeoutMillis = timeoutMillis(); 3986 CompletableFuture<Item> f = new CompletableFuture<>(); 3987 long startTime = System.nanoTime(); 3988 assertSame(f, f.completeOnTimeout(v, timeoutMillis, MILLISECONDS)); 3989 assertSame(v, f.join()); 3990 assertTrue(millisElapsedSince(startTime) >= timeoutMillis); 3991 f.complete(ninetynine); // should have no effect 3992 checkCompletedNormally(f, v); 3993 } 3994 3995 /** 3996 * completeOnTimeout has no effect if completed within timeout 3997 */ testCompleteOnTimeout_completed()3998 public void testCompleteOnTimeout_completed() { 3999 for (Item v1 : new Item[] { itemOne, null }) 4000 { 4001 CompletableFuture<Item> f = new CompletableFuture<>(); 4002 CompletableFuture<Item> g = new CompletableFuture<>(); 4003 long startTime = System.nanoTime(); 4004 f.complete(v1); 4005 mustEqual(f, f.completeOnTimeout(minusOne, LONG_DELAY_MS, MILLISECONDS)); 4006 mustEqual(g, g.completeOnTimeout(minusOne, LONG_DELAY_MS, MILLISECONDS)); 4007 g.complete(v1); 4008 checkCompletedNormally(f, v1); 4009 checkCompletedNormally(g, v1); 4010 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); 4011 }} 4012 4013 /** 4014 * delayedExecutor returns an executor that delays submission 4015 */ 4016 public void testDelayedExecutor() { 4017 testInParallel(() -> testDelayedExecutor(null, null), 4018 () -> testDelayedExecutor(null, itemOne), 4019 () -> testDelayedExecutor(new ThreadExecutor(), itemOne), 4020 () -> testDelayedExecutor(new ThreadExecutor(), itemOne)); 4021 } 4022 testDelayedExecutor(Executor executor, Item v)4023 public void testDelayedExecutor(Executor executor, Item v) throws Exception { 4024 long timeoutMillis = timeoutMillis(); 4025 // Use an "unreasonably long" long timeout to catch lingering threads 4026 long longTimeoutMillis = 1000 * 60 * 60 * 24; 4027 final Executor delayer, longDelayer; 4028 if (executor == null) { 4029 delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS); 4030 longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS); 4031 } else { 4032 delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS, executor); 4033 longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS, executor); 4034 } 4035 long startTime = System.nanoTime(); 4036 CompletableFuture<Item> f = 4037 CompletableFuture.supplyAsync(() -> v, delayer); 4038 CompletableFuture<Item> g = 4039 CompletableFuture.supplyAsync(() -> v, longDelayer); 4040 4041 assertNull(g.getNow(null)); 4042 4043 assertSame(v, f.get(LONG_DELAY_MS, MILLISECONDS)); 4044 long millisElapsed = millisElapsedSince(startTime); 4045 assertTrue(millisElapsed >= timeoutMillis); 4046 assertTrue(millisElapsed < LONG_DELAY_MS / 2); 4047 4048 checkCompletedNormally(f, v); 4049 4050 checkIncomplete(g); 4051 assertTrue(g.cancel(true)); 4052 } 4053 4054 //--- tests of implementation details; not part of official tck --- 4055 4056 Object resultOf(CompletableFuture<?> f) { 4057 SecurityManager sm = System.getSecurityManager(); 4058 if (sm != null) { 4059 try { 4060 System.setSecurityManager(null); 4061 } catch (SecurityException giveUp) { 4062 return "Reflection not available"; 4063 } 4064 } 4065 4066 try { 4067 java.lang.reflect.Field resultField 4068 = CompletableFuture.class.getDeclaredField("result"); 4069 resultField.setAccessible(true); 4070 return resultField.get(f); 4071 } catch (Throwable t) { 4072 throw new AssertionError(t); 4073 } finally { 4074 if (sm != null) System.setSecurityManager(sm); 4075 } 4076 } 4077 4078 public void testExceptionPropagationReusesResultObject() { 4079 if (!testImplementationDetails) return; 4080 for (ExecutionMode m : ExecutionMode.values()) 4081 { 4082 final CFException ex = new CFException(); 4083 final CompletableFuture<Item> v42 = CompletableFuture.completedFuture(fortytwo); 4084 final CompletableFuture<Item> incomplete = new CompletableFuture<>(); 4085 4086 final Runnable noopRunnable = new Noop(m); 4087 final Consumer<Item> noopConsumer = new NoopConsumer(m); 4088 final Function<Item, Item> incFunction = new IncFunction(m); 4089 4090 List<Function<CompletableFuture<Item>, CompletableFuture<?>>> funs 4091 = new ArrayList<>(); 4092 4093 funs.add(y -> m.thenRun(y, noopRunnable)); 4094 funs.add(y -> m.thenAccept(y, noopConsumer)); 4095 funs.add(y -> m.thenApply(y, incFunction)); 4096 4097 funs.add(y -> m.runAfterEither(y, incomplete, noopRunnable)); 4098 funs.add(y -> m.acceptEither(y, incomplete, noopConsumer)); 4099 funs.add(y -> m.applyToEither(y, incomplete, incFunction)); 4100 4101 funs.add(y -> m.runAfterBoth(y, v42, noopRunnable)); 4102 funs.add(y -> m.runAfterBoth(v42, y, noopRunnable)); 4103 funs.add(y -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); 4104 funs.add(y -> m.thenAcceptBoth(v42, y, new SubtractAction(m))); 4105 funs.add(y -> m.thenCombine(y, v42, new SubtractFunction(m))); 4106 funs.add(y -> m.thenCombine(v42, y, new SubtractFunction(m))); 4107 4108 funs.add(y -> m.whenComplete(y, (Item r, Throwable t) -> {})); 4109 4110 funs.add(y -> m.thenCompose(y, new CompletableFutureInc(m))); 4111 4112 funs.add(y -> CompletableFuture.allOf(y)); 4113 funs.add(y -> CompletableFuture.allOf(y, v42)); 4114 funs.add(y -> CompletableFuture.allOf(v42, y)); 4115 funs.add(y -> CompletableFuture.anyOf(y)); 4116 funs.add(y -> CompletableFuture.anyOf(y, incomplete)); 4117 funs.add(y -> CompletableFuture.anyOf(incomplete, y)); 4118 4119 for (Function<CompletableFuture<Item>, CompletableFuture<?>> 4120 fun : funs) { 4121 CompletableFuture<Item> f = new CompletableFuture<>(); 4122 f.completeExceptionally(ex); 4123 CompletableFuture<Item> src = m.thenApply(f, incFunction); 4124 checkCompletedWithWrappedException(src, ex); 4125 CompletableFuture<?> dep = fun.apply(src); 4126 checkCompletedWithWrappedException(dep, ex); 4127 assertSame(resultOf(src), resultOf(dep)); 4128 } 4129 4130 for (Function<CompletableFuture<Item>, CompletableFuture<?>> 4131 fun : funs) { 4132 CompletableFuture<Item> f = new CompletableFuture<>(); 4133 CompletableFuture<Item> src = m.thenApply(f, incFunction); 4134 CompletableFuture<?> dep = fun.apply(src); 4135 f.completeExceptionally(ex); 4136 checkCompletedWithWrappedException(src, ex); 4137 checkCompletedWithWrappedException(dep, ex); 4138 assertSame(resultOf(src), resultOf(dep)); 4139 } 4140 4141 for (boolean mayInterruptIfRunning : new boolean[] { true, false }) 4142 for (Function<CompletableFuture<Item>, CompletableFuture<?>> 4143 fun : funs) { 4144 CompletableFuture<Item> f = new CompletableFuture<>(); 4145 f.cancel(mayInterruptIfRunning); 4146 checkCancelled(f); 4147 CompletableFuture<Item> src = m.thenApply(f, incFunction); 4148 checkCompletedWithWrappedCancellationException(src); 4149 CompletableFuture<?> dep = fun.apply(src); 4150 checkCompletedWithWrappedCancellationException(dep); 4151 assertSame(resultOf(src), resultOf(dep)); 4152 } 4153 4154 for (boolean mayInterruptIfRunning : new boolean[] { true, false }) 4155 for (Function<CompletableFuture<Item>, CompletableFuture<?>> 4156 fun : funs) { 4157 CompletableFuture<Item> f = new CompletableFuture<>(); 4158 CompletableFuture<Item> src = m.thenApply(f, incFunction); 4159 CompletableFuture<?> dep = fun.apply(src); 4160 f.cancel(mayInterruptIfRunning); 4161 checkCancelled(f); 4162 checkCompletedWithWrappedCancellationException(src); 4163 checkCompletedWithWrappedCancellationException(dep); 4164 assertSame(resultOf(src), resultOf(dep)); 4165 } 4166 }} 4167 4168 /** 4169 * Minimal completion stages throw UOE for most non-CompletionStage methods 4170 */ testMinimalCompletionStage_minimality()4171 public void testMinimalCompletionStage_minimality() { 4172 if (!testImplementationDetails) return; 4173 Function<Method, String> toSignature = 4174 method -> method.getName() + Arrays.toString(method.getParameterTypes()); 4175 Predicate<Method> isNotStatic = 4176 method -> (method.getModifiers() & Modifier.STATIC) == 0; 4177 List<Method> minimalMethods = 4178 Stream.of(Object.class, CompletionStage.class) 4179 .flatMap(klazz -> Stream.of(klazz.getMethods())) 4180 .filter(isNotStatic) 4181 .collect(Collectors.toList()); 4182 // Methods from CompletableFuture permitted NOT to throw UOE 4183 String[] signatureWhitelist = { 4184 "newIncompleteFuture[]", 4185 "defaultExecutor[]", 4186 "minimalCompletionStage[]", 4187 "copy[]", 4188 }; 4189 Set<String> permittedMethodSignatures = 4190 Stream.concat(minimalMethods.stream().map(toSignature), 4191 Stream.of(signatureWhitelist)) 4192 .collect(Collectors.toSet()); 4193 List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods()) 4194 .filter(isNotStatic) 4195 .filter(method -> !permittedMethodSignatures.contains(toSignature.apply(method))) 4196 .collect(Collectors.toList()); 4197 4198 List<CompletionStage<Item>> stages = new ArrayList<>(); 4199 CompletionStage<Item> min = 4200 new CompletableFuture<Item>().minimalCompletionStage(); 4201 stages.add(min); 4202 stages.add(min.thenApply(x -> x)); 4203 stages.add(CompletableFuture.completedStage(itemOne)); 4204 stages.add(CompletableFuture.failedStage(new CFException())); 4205 4206 List<Method> bugs = new ArrayList<>(); 4207 for (Method method : allMethods) { 4208 Class<?>[] parameterTypes = method.getParameterTypes(); 4209 Object[] args = new Object[parameterTypes.length]; 4210 // Manufacture boxed primitives for primitive params 4211 for (int i = 0; i < args.length; i++) { 4212 Class<?> type = parameterTypes[i]; 4213 if (type == boolean.class) args[i] = false; 4214 else if (type == int.class) args[i] = 0; 4215 else if (type == long.class) args[i] = 0L; 4216 } 4217 for (CompletionStage<Item> stage : stages) { 4218 try { 4219 method.invoke(stage, args); 4220 bugs.add(method); 4221 } 4222 catch (java.lang.reflect.InvocationTargetException expected) { 4223 if (! (expected.getCause() instanceof UnsupportedOperationException)) { 4224 bugs.add(method); 4225 // expected.getCause().printStackTrace(); 4226 } 4227 } 4228 catch (ReflectiveOperationException bad) { throw new Error(bad); } 4229 } 4230 } 4231 if (!bugs.isEmpty()) 4232 throw new Error("Methods did not throw UOE: " + bugs); 4233 } 4234 4235 /** 4236 * minimalStage.toCompletableFuture() returns a CompletableFuture that 4237 * is completed normally, with the same value, when source is. 4238 */ testMinimalCompletionStage_toCompletableFuture_normalCompletion()4239 public void testMinimalCompletionStage_toCompletableFuture_normalCompletion() { 4240 for (boolean createIncomplete : new boolean[] { true, false }) 4241 for (Item v1 : new Item[] { itemOne, null }) 4242 { 4243 CompletableFuture<Item> f = new CompletableFuture<>(); 4244 CompletionStage<Item> minimal = f.minimalCompletionStage(); 4245 if (!createIncomplete) assertTrue(f.complete(v1)); 4246 CompletableFuture<Item> g = minimal.toCompletableFuture(); 4247 if (createIncomplete) { 4248 checkIncomplete(f); 4249 checkIncomplete(g); 4250 assertTrue(f.complete(v1)); 4251 } 4252 checkCompletedNormally(f, v1); 4253 checkCompletedNormally(g, v1); 4254 }} 4255 4256 /** 4257 * minimalStage.toCompletableFuture() returns a CompletableFuture that 4258 * is completed exceptionally when source is. 4259 */ testMinimalCompletionStage_toCompletableFuture_exceptionalCompletion()4260 public void testMinimalCompletionStage_toCompletableFuture_exceptionalCompletion() { 4261 for (boolean createIncomplete : new boolean[] { true, false }) 4262 { 4263 CFException ex = new CFException(); 4264 CompletableFuture<Item> f = new CompletableFuture<>(); 4265 CompletionStage<Item> minimal = f.minimalCompletionStage(); 4266 if (!createIncomplete) f.completeExceptionally(ex); 4267 CompletableFuture<Item> g = minimal.toCompletableFuture(); 4268 if (createIncomplete) { 4269 checkIncomplete(f); 4270 checkIncomplete(g); 4271 f.completeExceptionally(ex); 4272 } 4273 checkCompletedExceptionally(f, ex); 4274 checkCompletedWithWrappedException(g, ex); 4275 }} 4276 4277 /** 4278 * minimalStage.toCompletableFuture() gives mutable CompletableFuture 4279 */ testMinimalCompletionStage_toCompletableFuture_mutable()4280 public void testMinimalCompletionStage_toCompletableFuture_mutable() { 4281 for (Item v1 : new Item[] { itemOne, null }) 4282 { 4283 CompletableFuture<Item> f = new CompletableFuture<>(); 4284 CompletionStage<Item> minimal = f.minimalCompletionStage(); 4285 CompletableFuture<Item> g = minimal.toCompletableFuture(); 4286 assertTrue(g.complete(v1)); 4287 checkCompletedNormally(g, v1); 4288 checkIncomplete(f); 4289 checkIncomplete(minimal.toCompletableFuture()); 4290 }} 4291 4292 /** 4293 * minimalStage.toCompletableFuture().join() awaits completion 4294 */ testMinimalCompletionStage_toCompletableFuture_join()4295 public void testMinimalCompletionStage_toCompletableFuture_join() throws Exception { 4296 for (boolean createIncomplete : new boolean[] { true, false }) 4297 for (Item v1 : new Item[] { itemOne, null }) 4298 { 4299 CompletableFuture<Item> f = new CompletableFuture<>(); 4300 if (!createIncomplete) assertTrue(f.complete(v1)); 4301 CompletionStage<Item> minimal = f.minimalCompletionStage(); 4302 if (createIncomplete) assertTrue(f.complete(v1)); 4303 mustEqual(v1, minimal.toCompletableFuture().join()); 4304 mustEqual(v1, minimal.toCompletableFuture().get()); 4305 checkCompletedNormally(minimal.toCompletableFuture(), v1); 4306 }} 4307 4308 /** 4309 * Completion of a toCompletableFuture copy of a minimal stage 4310 * does not complete its source. 4311 */ testMinimalCompletionStage_toCompletableFuture_oneWayPropagation()4312 public void testMinimalCompletionStage_toCompletableFuture_oneWayPropagation() { 4313 CompletableFuture<Item> f = new CompletableFuture<>(); 4314 CompletionStage<Item> g = f.minimalCompletionStage(); 4315 assertTrue(g.toCompletableFuture().complete(itemOne)); 4316 assertTrue(g.toCompletableFuture().complete(null)); 4317 assertTrue(g.toCompletableFuture().cancel(true)); 4318 assertTrue(g.toCompletableFuture().cancel(false)); 4319 assertTrue(g.toCompletableFuture().completeExceptionally(new CFException())); 4320 checkIncomplete(g.toCompletableFuture()); 4321 f.complete(itemOne); 4322 checkCompletedNormally(g.toCompletableFuture(), itemOne); 4323 } 4324 4325 /** Demo utility method for external reliable toCompletableFuture */ toCompletableFuture(CompletionStage<T> stage)4326 static <T> CompletableFuture<T> toCompletableFuture(CompletionStage<T> stage) { 4327 CompletableFuture<T> f = new CompletableFuture<>(); 4328 stage.handle((T t, Throwable ex) -> { 4329 if (ex != null) f.completeExceptionally(ex); 4330 else f.complete(t); 4331 return null; 4332 }); 4333 return f; 4334 } 4335 4336 /** Demo utility method to join a CompletionStage */ join(CompletionStage<T> stage)4337 static <T> T join(CompletionStage<T> stage) { 4338 return toCompletableFuture(stage).join(); 4339 } 4340 4341 /** 4342 * Joining a minimal stage "by hand" works 4343 */ testMinimalCompletionStage_join_by_hand()4344 public void testMinimalCompletionStage_join_by_hand() { 4345 for (boolean createIncomplete : new boolean[] { true, false }) 4346 for (Item v1 : new Item[] { itemOne, null }) 4347 { 4348 CompletableFuture<Item> f = new CompletableFuture<>(); 4349 CompletionStage<Item> minimal = f.minimalCompletionStage(); 4350 CompletableFuture<Item> g = new CompletableFuture<>(); 4351 if (!createIncomplete) assertTrue(f.complete(v1)); 4352 minimal.thenAccept(x -> g.complete(x)); 4353 if (createIncomplete) assertTrue(f.complete(v1)); 4354 g.join(); 4355 checkCompletedNormally(g, v1); 4356 checkCompletedNormally(f, v1); 4357 mustEqual(v1, join(minimal)); 4358 }} 4359 4360 static class Monad { 4361 static class ZeroException extends RuntimeException { ZeroException()4362 public ZeroException() { super("monadic zero"); } 4363 } 4364 // "return", "unit" unit(T value)4365 static <T> CompletableFuture<T> unit(T value) { 4366 return completedFuture(value); 4367 } 4368 // monadic zero ? zero()4369 static <T> CompletableFuture<T> zero() { 4370 return failedFuture(new ZeroException()); 4371 } 4372 // >=> compose(Function<T, CompletableFuture<U>> f, Function<U, CompletableFuture<V>> g)4373 static <T,U,V> Function<T, CompletableFuture<V>> compose 4374 (Function<T, CompletableFuture<U>> f, 4375 Function<U, CompletableFuture<V>> g) { 4376 return x -> f.apply(x).thenCompose(g); 4377 } 4378 assertZero(CompletableFuture<?> f)4379 static void assertZero(CompletableFuture<?> f) { 4380 try { 4381 f.getNow(null); 4382 throw new AssertionError("should throw"); 4383 } catch (CompletionException success) { 4384 assertTrue(success.getCause() instanceof ZeroException); 4385 } 4386 } 4387 assertFutureEquals(CompletableFuture<T> f, CompletableFuture<T> g)4388 static <T> void assertFutureEquals(CompletableFuture<T> f, 4389 CompletableFuture<T> g) { 4390 T fval = null, gval = null; 4391 Throwable fex = null, gex = null; 4392 4393 try { fval = f.get(); } 4394 catch (ExecutionException ex) { fex = ex.getCause(); } 4395 catch (Throwable ex) { fex = ex; } 4396 4397 try { gval = g.get(); } 4398 catch (ExecutionException ex) { gex = ex.getCause(); } 4399 catch (Throwable ex) { gex = ex; } 4400 4401 if (fex != null || gex != null) 4402 assertSame(fex.getClass(), gex.getClass()); 4403 else 4404 mustEqual(fval, gval); 4405 } 4406 4407 static class PlusFuture<T> extends CompletableFuture<T> { 4408 AtomicReference<Throwable> firstFailure = new AtomicReference<>(null); 4409 } 4410 4411 /** Implements "monadic plus". */ plus(CompletableFuture<? extends T> f, CompletableFuture<? extends T> g)4412 static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f, 4413 CompletableFuture<? extends T> g) { 4414 PlusFuture<T> plus = new PlusFuture<T>(); 4415 BiConsumer<T, Throwable> action = (T result, Throwable ex) -> { 4416 try { 4417 if (ex == null) { 4418 if (plus.complete(result)) 4419 if (plus.firstFailure.get() != null) 4420 plus.firstFailure.set(null); 4421 } 4422 else if (plus.firstFailure.compareAndSet(null, ex)) { 4423 if (plus.isDone()) 4424 plus.firstFailure.set(null); 4425 } 4426 else { 4427 // first failure has precedence 4428 Throwable first = plus.firstFailure.getAndSet(null); 4429 4430 // may fail with "Self-suppression not permitted" 4431 try { first.addSuppressed(ex); } 4432 catch (Exception ignored) {} 4433 4434 plus.completeExceptionally(first); 4435 } 4436 } catch (Throwable unexpected) { 4437 plus.completeExceptionally(unexpected); 4438 } 4439 }; 4440 f.whenComplete(action); 4441 g.whenComplete(action); 4442 return plus; 4443 } 4444 } 4445 4446 /** 4447 * CompletableFuture is an additive monad - sort of. 4448 * https://en.wikipedia.org/wiki/Monad_(functional_programming)#Additive_monads 4449 */ testAdditiveMonad()4450 public void testAdditiveMonad() throws Throwable { 4451 Function<Long, CompletableFuture<Long>> unit = Monad::unit; 4452 CompletableFuture<Long> zero = Monad.zero(); 4453 4454 // Some mutually non-commutative functions 4455 Function<Long, CompletableFuture<Long>> triple 4456 = x -> Monad.unit(3 * x); 4457 Function<Long, CompletableFuture<Long>> inc 4458 = x -> Monad.unit(x + 1); 4459 4460 // unit is a right identity: m >>= unit === m 4461 Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit), 4462 inc.apply(5L)); 4463 // unit is a left identity: (unit x) >>= f === f x 4464 Monad.assertFutureEquals(unit.apply(5L).thenCompose(inc), 4465 inc.apply(5L)); 4466 4467 // associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) ) 4468 Monad.assertFutureEquals( 4469 unit.apply(5L).thenCompose(inc).thenCompose(triple), 4470 unit.apply(5L).thenCompose(x -> inc.apply(x).thenCompose(triple))); 4471 4472 // The case for CompletableFuture as an additive monad is weaker... 4473 4474 // zero is a monadic zero 4475 Monad.assertZero(zero); 4476 4477 // left zero: zero >>= f === zero 4478 Monad.assertZero(zero.thenCompose(inc)); 4479 // right zero: f >>= (\x -> zero) === zero 4480 Monad.assertZero(inc.apply(5L).thenCompose(x -> zero)); 4481 4482 // f plus zero === f 4483 Monad.assertFutureEquals(Monad.unit(5L), 4484 Monad.plus(Monad.unit(5L), zero)); 4485 // zero plus f === f 4486 Monad.assertFutureEquals(Monad.unit(5L), 4487 Monad.plus(zero, Monad.unit(5L))); 4488 // zero plus zero === zero 4489 Monad.assertZero(Monad.plus(zero, zero)); 4490 { 4491 CompletableFuture<Long> f = Monad.plus(Monad.unit(5L), 4492 Monad.unit(8L)); 4493 // non-determinism 4494 assertTrue(f.get() == 5L || f.get() == 8L); 4495 } 4496 4497 CompletableFuture<Long> godot = new CompletableFuture<>(); 4498 // f plus godot === f (doesn't wait for godot) 4499 Monad.assertFutureEquals(Monad.unit(5L), 4500 Monad.plus(Monad.unit(5L), godot)); 4501 // godot plus f === f (doesn't wait for godot) 4502 Monad.assertFutureEquals(Monad.unit(5L), 4503 Monad.plus(godot, Monad.unit(5L))); 4504 } 4505 4506 /** Test long recursive chains of CompletableFutures with cascading completions */ 4507 @SuppressWarnings("FutureReturnValueIgnored") testRecursiveChains()4508 public void testRecursiveChains() throws Throwable { 4509 for (ExecutionMode m : ExecutionMode.values()) 4510 for (boolean addDeadEnds : new boolean[] { true, false }) 4511 { 4512 final int val = 42; 4513 final int n = expensiveTests ? 1_000 : 2; 4514 CompletableFuture<Item> head = new CompletableFuture<>(); 4515 CompletableFuture<Item> tail = head; 4516 for (int i = 0; i < n; i++) { 4517 if (addDeadEnds) m.thenApply(tail, v -> new Item(v.value + 1)); 4518 tail = m.thenApply(tail, v -> new Item(v.value + 1)); 4519 if (addDeadEnds) m.applyToEither(tail, tail, v -> new Item(v.value + 1)); 4520 tail = m.applyToEither(tail, tail, v -> new Item(v.value + 1)); 4521 if (addDeadEnds) m.thenCombine(tail, tail, (v, w) -> new Item(v.value + 1)); 4522 tail = m.thenCombine(tail, tail, (v, w) -> new Item(v.value + 1)); 4523 } 4524 head.complete(itemFor(val)); 4525 mustEqual(val + 3 * n, tail.join()); 4526 }} 4527 4528 /** 4529 * A single CompletableFuture with many dependents. 4530 * A demo of scalability - runtime is O(n). 4531 */ 4532 @SuppressWarnings("FutureReturnValueIgnored") testManyDependents()4533 public void testManyDependents() throws Throwable { 4534 final int n = expensiveTests ? 1_000_000 : 10; 4535 final CompletableFuture<Void> head = new CompletableFuture<>(); 4536 final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null); 4537 final AtomicInteger count = new AtomicInteger(0); 4538 for (int i = 0; i < n; i++) { 4539 head.thenRun(() -> count.getAndIncrement()); 4540 head.thenAccept(x -> count.getAndIncrement()); 4541 head.thenApply(x -> count.getAndIncrement()); 4542 4543 head.runAfterBoth(complete, () -> count.getAndIncrement()); 4544 head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement()); 4545 head.thenCombine(complete, (x, y) -> count.getAndIncrement()); 4546 complete.runAfterBoth(head, () -> count.getAndIncrement()); 4547 complete.thenAcceptBoth(head, (x, y) -> count.getAndIncrement()); 4548 complete.thenCombine(head, (x, y) -> count.getAndIncrement()); 4549 4550 head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement()); 4551 head.acceptEither(new CompletableFuture<Void>(), x -> count.getAndIncrement()); 4552 head.applyToEither(new CompletableFuture<Void>(), x -> count.getAndIncrement()); 4553 new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement()); 4554 new CompletableFuture<Void>().acceptEither(head, x -> count.getAndIncrement()); 4555 new CompletableFuture<Void>().applyToEither(head, x -> count.getAndIncrement()); 4556 } 4557 head.complete(null); 4558 mustEqual(5 * 3 * n, count.get()); 4559 } 4560 4561 /** ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck */ 4562 @SuppressWarnings("FutureReturnValueIgnored") testCoCompletionGarbageRetention()4563 public void testCoCompletionGarbageRetention() throws Throwable { 4564 final int n = expensiveTests ? 1_000_000 : 10; 4565 final CompletableFuture<Item> incomplete = new CompletableFuture<>(); 4566 CompletableFuture<Item> f; 4567 for (int i = 0; i < n; i++) { 4568 f = new CompletableFuture<>(); 4569 f.runAfterEither(incomplete, () -> {}); 4570 f.complete(null); 4571 4572 f = new CompletableFuture<>(); 4573 f.acceptEither(incomplete, x -> {}); 4574 f.complete(null); 4575 4576 f = new CompletableFuture<>(); 4577 f.applyToEither(incomplete, x -> x); 4578 f.complete(null); 4579 4580 f = new CompletableFuture<>(); 4581 CompletableFuture.anyOf(f, incomplete); 4582 f.complete(null); 4583 } 4584 4585 for (int i = 0; i < n; i++) { 4586 f = new CompletableFuture<>(); 4587 incomplete.runAfterEither(f, () -> {}); 4588 f.complete(null); 4589 4590 f = new CompletableFuture<>(); 4591 incomplete.acceptEither(f, x -> {}); 4592 f.complete(null); 4593 4594 f = new CompletableFuture<>(); 4595 incomplete.applyToEither(f, x -> x); 4596 f.complete(null); 4597 4598 f = new CompletableFuture<>(); 4599 CompletableFuture.anyOf(incomplete, f); 4600 f.complete(null); 4601 } 4602 } 4603 4604 /** 4605 * Reproduction recipe for: 4606 * 8160402: Garbage retention with CompletableFuture.anyOf 4607 * cvs update -D '2016-05-01' ./src/main/java/util/concurrent/CompletableFuture.java && ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testAnyOfGarbageRetention tck; cvs update -A 4608 */ testAnyOfGarbageRetention()4609 public void testAnyOfGarbageRetention() throws Throwable { 4610 for (Item v : new Item[] { itemOne, null }) 4611 { 4612 final int n = expensiveTests ? 100_000 : 10; 4613 @SuppressWarnings("unchecked") 4614 CompletableFuture<Item>[] fs = 4615 (CompletableFuture<Item>[])new CompletableFuture[100]; 4616 for (int i = 0; i < fs.length; i++) 4617 fs[i] = new CompletableFuture<>(); 4618 fs[fs.length - 1].complete(v); 4619 for (int i = 0; i < n; i++) 4620 checkCompletedNormally(CompletableFuture.anyOf(fs), v); 4621 }} 4622 4623 /** 4624 * Checks for garbage retention with allOf. 4625 * 4626 * As of 2016-07, fails with OOME: 4627 * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledAllOfGarbageRetention tck 4628 */ testCancelledAllOfGarbageRetention()4629 public void testCancelledAllOfGarbageRetention() throws Throwable { 4630 final int n = expensiveTests ? 100_000 : 10; 4631 @SuppressWarnings("unchecked") 4632 CompletableFuture<Item>[] fs 4633 = (CompletableFuture<Item>[]) new CompletableFuture<?>[100]; 4634 for (int i = 0; i < fs.length; i++) 4635 fs[i] = new CompletableFuture<>(); 4636 for (int i = 0; i < n; i++) 4637 assertTrue(CompletableFuture.allOf(fs).cancel(false)); 4638 } 4639 4640 /** 4641 * Checks for garbage retention when a dependent future is 4642 * cancelled and garbage-collected. 4643 * 8161600: Garbage retention when source CompletableFutures are never completed 4644 * 4645 * As of 2016-07, fails with OOME: 4646 * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledGarbageRetention tck 4647 */ testCancelledGarbageRetention()4648 public void testCancelledGarbageRetention() throws Throwable { 4649 final int n = expensiveTests ? 100_000 : 10; 4650 CompletableFuture<Item> neverCompleted = new CompletableFuture<>(); 4651 for (int i = 0; i < n; i++) 4652 assertTrue(neverCompleted.thenRun(() -> {}).cancel(true)); 4653 } 4654 4655 /** 4656 * Checks for garbage retention when MinimalStage.toCompletableFuture() 4657 * is invoked many times. 4658 * 8161600: Garbage retention when source CompletableFutures are never completed 4659 * 4660 * As of 2016-07, fails with OOME: 4661 * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testToCompletableFutureGarbageRetention tck 4662 */ testToCompletableFutureGarbageRetention()4663 public void testToCompletableFutureGarbageRetention() throws Throwable { 4664 final int n = expensiveTests ? 900_000 : 10; 4665 CompletableFuture<Item> neverCompleted = new CompletableFuture<>(); 4666 CompletionStage<Item> minimal = neverCompleted.minimalCompletionStage(); 4667 for (int i = 0; i < n; i++) 4668 assertTrue(minimal.toCompletableFuture().cancel(true)); 4669 } 4670 4671 // static <U> U join(CompletionStage<U> stage) { 4672 // CompletableFuture<U> f = new CompletableFuture<>(); 4673 // stage.whenComplete((v, ex) -> { 4674 // if (ex != null) f.completeExceptionally(ex); else f.complete(v); 4675 // }); 4676 // return f.join(); 4677 // } 4678 4679 // static <U> boolean isDone(CompletionStage<U> stage) { 4680 // CompletableFuture<U> f = new CompletableFuture<>(); 4681 // stage.whenComplete((v, ex) -> { 4682 // if (ex != null) f.completeExceptionally(ex); else f.complete(v); 4683 // }); 4684 // return f.isDone(); 4685 // } 4686 4687 // static <U> U join2(CompletionStage<U> stage) { 4688 // return stage.toCompletableFuture().copy().join(); 4689 // } 4690 4691 // static <U> boolean isDone2(CompletionStage<U> stage) { 4692 // return stage.toCompletableFuture().copy().isDone(); 4693 // } 4694 4695 // For testing default implementations 4696 // Only non-default interface methods defined. 4697 static final class DelegatedCompletionStage<T> implements CompletionStage<T> { 4698 final CompletableFuture<T> cf; DelegatedCompletionStage(CompletableFuture<T> cf)4699 DelegatedCompletionStage(CompletableFuture<T> cf) { this.cf = cf; } toCompletableFuture()4700 public CompletableFuture<T> toCompletableFuture() { 4701 return cf; } thenRun(Runnable action)4702 public CompletionStage<Void> thenRun 4703 (Runnable action) { 4704 return cf.thenRun(action); } thenRunAsync(Runnable action)4705 public CompletionStage<Void> thenRunAsync 4706 (Runnable action) { 4707 return cf.thenRunAsync(action); } thenRunAsync(Runnable action, Executor executor)4708 public CompletionStage<Void> thenRunAsync 4709 (Runnable action, 4710 Executor executor) { 4711 return cf.thenRunAsync(action, executor); } thenAccept(Consumer<? super T> action)4712 public CompletionStage<Void> thenAccept 4713 (Consumer<? super T> action) { 4714 return cf.thenAccept(action); } thenAcceptAsync(Consumer<? super T> action)4715 public CompletionStage<Void> thenAcceptAsync 4716 (Consumer<? super T> action) { 4717 return cf.thenAcceptAsync(action); } thenAcceptAsync(Consumer<? super T> action, Executor executor)4718 public CompletionStage<Void> thenAcceptAsync 4719 (Consumer<? super T> action, 4720 Executor executor) { 4721 return cf.thenAcceptAsync(action, executor); } thenApply(Function<? super T,? extends U> a)4722 public <U> CompletionStage<U> thenApply 4723 (Function<? super T,? extends U> a) { 4724 return cf.thenApply(a); } thenApplyAsync(Function<? super T,? extends U> fn)4725 public <U> CompletionStage<U> thenApplyAsync 4726 (Function<? super T,? extends U> fn) { 4727 return cf.thenApplyAsync(fn); } thenApplyAsync(Function<? super T,? extends U> fn, Executor executor)4728 public <U> CompletionStage<U> thenApplyAsync 4729 (Function<? super T,? extends U> fn, 4730 Executor executor) { 4731 return cf.thenApplyAsync(fn, executor); } thenCombine(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn)4732 public <U,V> CompletionStage<V> thenCombine 4733 (CompletionStage<? extends U> other, 4734 BiFunction<? super T,? super U,? extends V> fn) { 4735 return cf.thenCombine(other, fn); } thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn)4736 public <U,V> CompletionStage<V> thenCombineAsync 4737 (CompletionStage<? extends U> other, 4738 BiFunction<? super T,? super U,? extends V> fn) { 4739 return cf.thenCombineAsync(other, fn); } thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn, Executor executor)4740 public <U,V> CompletionStage<V> thenCombineAsync 4741 (CompletionStage<? extends U> other, 4742 BiFunction<? super T,? super U,? extends V> fn, 4743 Executor executor) { 4744 return cf.thenCombineAsync(other, fn, executor); } thenAcceptBoth(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action)4745 public <U> CompletionStage<Void> thenAcceptBoth 4746 (CompletionStage<? extends U> other, 4747 BiConsumer<? super T, ? super U> action) { 4748 return cf.thenAcceptBoth(other, action); } thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action)4749 public <U> CompletionStage<Void> thenAcceptBothAsync 4750 (CompletionStage<? extends U> other, 4751 BiConsumer<? super T, ? super U> action) { 4752 return cf.thenAcceptBothAsync(other, action); } thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action, Executor executor)4753 public <U> CompletionStage<Void> thenAcceptBothAsync 4754 (CompletionStage<? extends U> other, 4755 BiConsumer<? super T, ? super U> action, 4756 Executor executor) { 4757 return cf.thenAcceptBothAsync(other, action, executor); } runAfterBoth(CompletionStage<?> other, Runnable action)4758 public CompletionStage<Void> runAfterBoth 4759 (CompletionStage<?> other, 4760 Runnable action) { 4761 return cf.runAfterBoth(other, action); } runAfterBothAsync(CompletionStage<?> other, Runnable action)4762 public CompletionStage<Void> runAfterBothAsync 4763 (CompletionStage<?> other, 4764 Runnable action) { 4765 return cf.runAfterBothAsync(other, action); } runAfterBothAsync(CompletionStage<?> other, Runnable action, Executor executor)4766 public CompletionStage<Void> runAfterBothAsync 4767 (CompletionStage<?> other, 4768 Runnable action, 4769 Executor executor) { 4770 return cf.runAfterBothAsync(other, action, executor); } applyToEither(CompletionStage<? extends T> other, Function<? super T, U> fn)4771 public <U> CompletionStage<U> applyToEither 4772 (CompletionStage<? extends T> other, 4773 Function<? super T, U> fn) { 4774 return cf.applyToEither(other, fn); } applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn)4775 public <U> CompletionStage<U> applyToEitherAsync 4776 (CompletionStage<? extends T> other, 4777 Function<? super T, U> fn) { 4778 return cf.applyToEitherAsync(other, fn); } applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn, Executor executor)4779 public <U> CompletionStage<U> applyToEitherAsync 4780 (CompletionStage<? extends T> other, 4781 Function<? super T, U> fn, 4782 Executor executor) { 4783 return cf.applyToEitherAsync(other, fn, executor); } acceptEither(CompletionStage<? extends T> other, Consumer<? super T> action)4784 public CompletionStage<Void> acceptEither 4785 (CompletionStage<? extends T> other, 4786 Consumer<? super T> action) { 4787 return cf.acceptEither(other, action); } acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action)4788 public CompletionStage<Void> acceptEitherAsync 4789 (CompletionStage<? extends T> other, 4790 Consumer<? super T> action) { 4791 return cf.acceptEitherAsync(other, action); } acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action, Executor executor)4792 public CompletionStage<Void> acceptEitherAsync 4793 (CompletionStage<? extends T> other, 4794 Consumer<? super T> action, 4795 Executor executor) { 4796 return cf.acceptEitherAsync(other, action, executor); } runAfterEither(CompletionStage<?> other, Runnable action)4797 public CompletionStage<Void> runAfterEither 4798 (CompletionStage<?> other, 4799 Runnable action) { 4800 return cf.runAfterEither(other, action); } runAfterEitherAsync(CompletionStage<?> other, Runnable action)4801 public CompletionStage<Void> runAfterEitherAsync 4802 (CompletionStage<?> other, 4803 Runnable action) { 4804 return cf.runAfterEitherAsync(other, action); } runAfterEitherAsync(CompletionStage<?> other, Runnable action, Executor executor)4805 public CompletionStage<Void> runAfterEitherAsync 4806 (CompletionStage<?> other, 4807 Runnable action, 4808 Executor executor) { 4809 return cf.runAfterEitherAsync(other, action, executor); } thenCompose(Function<? super T, ? extends CompletionStage<U>> fn)4810 public <U> CompletionStage<U> thenCompose 4811 (Function<? super T, ? extends CompletionStage<U>> fn) { 4812 return cf.thenCompose(fn); } thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn)4813 public <U> CompletionStage<U> thenComposeAsync 4814 (Function<? super T, ? extends CompletionStage<U>> fn) { 4815 return cf.thenComposeAsync(fn); } thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn, Executor executor)4816 public <U> CompletionStage<U> thenComposeAsync 4817 (Function<? super T, ? extends CompletionStage<U>> fn, 4818 Executor executor) { 4819 return cf.thenComposeAsync(fn, executor); } handle(BiFunction<? super T, Throwable, ? extends U> fn)4820 public <U> CompletionStage<U> handle 4821 (BiFunction<? super T, Throwable, ? extends U> fn) { 4822 return cf.handle(fn); } handleAsync(BiFunction<? super T, Throwable, ? extends U> fn)4823 public <U> CompletionStage<U> handleAsync 4824 (BiFunction<? super T, Throwable, ? extends U> fn) { 4825 return cf.handleAsync(fn); } handleAsync(BiFunction<? super T, Throwable, ? extends U> fn, Executor executor)4826 public <U> CompletionStage<U> handleAsync 4827 (BiFunction<? super T, Throwable, ? extends U> fn, 4828 Executor executor) { 4829 return cf.handleAsync(fn, executor); } whenComplete(BiConsumer<? super T, ? super Throwable> action)4830 public CompletionStage<T> whenComplete 4831 (BiConsumer<? super T, ? super Throwable> action) { 4832 return cf.whenComplete(action); } whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action)4833 public CompletionStage<T> whenCompleteAsync 4834 (BiConsumer<? super T, ? super Throwable> action) { 4835 return cf.whenCompleteAsync(action); } whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action, Executor executor)4836 public CompletionStage<T> whenCompleteAsync 4837 (BiConsumer<? super T, ? super Throwable> action, 4838 Executor executor) { 4839 return cf.whenCompleteAsync(action, executor); } exceptionally(Function<Throwable, ? extends T> fn)4840 public CompletionStage<T> exceptionally 4841 (Function<Throwable, ? extends T> fn) { 4842 return cf.exceptionally(fn); } 4843 } 4844 4845 /** 4846 * default-implemented exceptionallyAsync action is not invoked when 4847 * source completes normally, and source result is propagated 4848 */ testDefaultExceptionallyAsync_normalCompletion()4849 public void testDefaultExceptionallyAsync_normalCompletion() { 4850 for (boolean createIncomplete : new boolean[] { true, false }) 4851 for (Item v1 : new Item[] { itemOne, null }) 4852 { 4853 final AtomicInteger ran = new AtomicInteger(0); 4854 final CompletableFuture<Item> f = new CompletableFuture<>(); 4855 final DelegatedCompletionStage<Item> d = 4856 new DelegatedCompletionStage<>(f); 4857 if (!createIncomplete) assertTrue(f.complete(v1)); 4858 final CompletionStage<Item> g = d.exceptionallyAsync 4859 ((Throwable t) -> { 4860 ran.getAndIncrement(); 4861 throw new AssertionError("should not be called"); 4862 }); 4863 if (createIncomplete) assertTrue(f.complete(v1)); 4864 4865 checkCompletedNormally(g.toCompletableFuture(), v1); 4866 checkCompletedNormally(f, v1); 4867 mustEqual(0, ran.get()); 4868 }} 4869 4870 /** 4871 * default-implemented exceptionallyAsync action completes with 4872 * function value on source exception 4873 */ testDefaultExceptionallyAsync_exceptionalCompletion()4874 public void testDefaultExceptionallyAsync_exceptionalCompletion() { 4875 for (boolean createIncomplete : new boolean[] { true, false }) 4876 for (Item v1 : new Item[] { itemOne, null }) 4877 { 4878 final AtomicInteger ran = new AtomicInteger(0); 4879 final CFException ex = new CFException(); 4880 final CompletableFuture<Item> f = new CompletableFuture<>(); 4881 final DelegatedCompletionStage<Item> d = 4882 new DelegatedCompletionStage<>(f); 4883 if (!createIncomplete) f.completeExceptionally(ex); 4884 final CompletionStage<Item> g = d.exceptionallyAsync 4885 ((Throwable t) -> { 4886 assertSame(t, ex); 4887 ran.getAndIncrement(); 4888 return v1; 4889 }); 4890 if (createIncomplete) f.completeExceptionally(ex); 4891 4892 checkCompletedNormally(g.toCompletableFuture(), v1); 4893 checkCompletedExceptionally(f, ex); 4894 mustEqual(1, ran.get()); 4895 }} 4896 4897 /** 4898 * Under default implementation, if an "exceptionally action" 4899 * throws an exception, it completes exceptionally with that 4900 * exception 4901 */ testDefaultExceptionallyAsync_exceptionalCompletionActionFailed()4902 public void testDefaultExceptionallyAsync_exceptionalCompletionActionFailed() { 4903 for (boolean createIncomplete : new boolean[] { true, false }) 4904 { 4905 final AtomicInteger ran = new AtomicInteger(0); 4906 final CFException ex1 = new CFException(); 4907 final CFException ex2 = new CFException(); 4908 final CompletableFuture<Item> f = new CompletableFuture<>(); 4909 final DelegatedCompletionStage<Item> d = 4910 new DelegatedCompletionStage<>(f); 4911 if (!createIncomplete) f.completeExceptionally(ex1); 4912 final CompletionStage<Item> g = d.exceptionallyAsync 4913 ((Throwable t) -> { 4914 assertSame(t, ex1); 4915 ran.getAndIncrement(); 4916 throw ex2; 4917 }); 4918 if (createIncomplete) f.completeExceptionally(ex1); 4919 4920 checkCompletedWithWrappedException(g.toCompletableFuture(), ex2); 4921 checkCompletedExceptionally(f, ex1); 4922 checkCompletedExceptionally(d.toCompletableFuture(), ex1); 4923 mustEqual(1, ran.get()); 4924 }} 4925 4926 /** 4927 * default-implemented exceptionallyCompose result completes 4928 * normally after normal completion of source 4929 */ testDefaultExceptionallyCompose_normalCompletion()4930 public void testDefaultExceptionallyCompose_normalCompletion() { 4931 for (boolean createIncomplete : new boolean[] { true, false }) 4932 for (Item v1 : new Item[] { itemOne, null }) 4933 { 4934 final CompletableFuture<Item> f = new CompletableFuture<>(); 4935 final ExceptionalCompletableFutureFunction r = 4936 new ExceptionalCompletableFutureFunction(ExecutionMode.SYNC); 4937 final DelegatedCompletionStage<Item> d = 4938 new DelegatedCompletionStage<>(f); 4939 if (!createIncomplete) assertTrue(f.complete(v1)); 4940 final CompletionStage<Item> g = d.exceptionallyCompose(r); 4941 if (createIncomplete) assertTrue(f.complete(v1)); 4942 4943 checkCompletedNormally(f, v1); 4944 checkCompletedNormally(g.toCompletableFuture(), v1); 4945 r.assertNotInvoked(); 4946 }} 4947 4948 /** 4949 * default-implemented exceptionallyCompose result completes 4950 * normally after exceptional completion of source 4951 */ testDefaultExceptionallyCompose_exceptionalCompletion()4952 public void testDefaultExceptionallyCompose_exceptionalCompletion() { 4953 for (boolean createIncomplete : new boolean[] { true, false }) 4954 { 4955 final CFException ex = new CFException(); 4956 final ExceptionalCompletableFutureFunction r = 4957 new ExceptionalCompletableFutureFunction(ExecutionMode.SYNC); 4958 final CompletableFuture<Item> f = new CompletableFuture<>(); 4959 final DelegatedCompletionStage<Item> d = 4960 new DelegatedCompletionStage<>(f); 4961 if (!createIncomplete) f.completeExceptionally(ex); 4962 final CompletionStage<Item> g = d.exceptionallyCompose(r); 4963 if (createIncomplete) f.completeExceptionally(ex); 4964 4965 checkCompletedExceptionally(f, ex); 4966 checkCompletedNormally(g.toCompletableFuture(), r.value); 4967 r.assertInvoked(); 4968 }} 4969 4970 /** 4971 * default-implemented exceptionallyCompose completes 4972 * exceptionally on exception if action does 4973 */ testDefaultExceptionallyCompose_actionFailed()4974 public void testDefaultExceptionallyCompose_actionFailed() { 4975 for (boolean createIncomplete : new boolean[] { true, false }) 4976 { 4977 final CFException ex = new CFException(); 4978 final CompletableFuture<Item> f = new CompletableFuture<>(); 4979 final FailingExceptionalCompletableFutureFunction r 4980 = new FailingExceptionalCompletableFutureFunction(ExecutionMode.SYNC); 4981 final DelegatedCompletionStage<Item> d = 4982 new DelegatedCompletionStage<>(f); 4983 if (!createIncomplete) f.completeExceptionally(ex); 4984 final CompletionStage<Item> g = d.exceptionallyCompose(r); 4985 if (createIncomplete) f.completeExceptionally(ex); 4986 4987 checkCompletedExceptionally(f, ex); 4988 checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex); 4989 r.assertInvoked(); 4990 }} 4991 4992 /** 4993 * default-implemented exceptionallyComposeAsync result completes 4994 * normally after normal completion of source 4995 */ testDefaultExceptionallyComposeAsync_normalCompletion()4996 public void testDefaultExceptionallyComposeAsync_normalCompletion() { 4997 for (boolean createIncomplete : new boolean[] { true, false }) 4998 for (Item v1 : new Item[] { itemOne, null }) 4999 { 5000 final CompletableFuture<Item> f = new CompletableFuture<>(); 5001 final ExceptionalCompletableFutureFunction r = 5002 new ExceptionalCompletableFutureFunction(ExecutionMode.ASYNC); 5003 final DelegatedCompletionStage<Item> d = 5004 new DelegatedCompletionStage<>(f); 5005 if (!createIncomplete) assertTrue(f.complete(v1)); 5006 final CompletionStage<Item> g = d.exceptionallyComposeAsync(r); 5007 if (createIncomplete) assertTrue(f.complete(v1)); 5008 5009 checkCompletedNormally(f, v1); 5010 checkCompletedNormally(g.toCompletableFuture(), v1); 5011 r.assertNotInvoked(); 5012 }} 5013 5014 /** 5015 * default-implemented exceptionallyComposeAsync result completes 5016 * normally after exceptional completion of source 5017 */ testDefaultExceptionallyComposeAsync_exceptionalCompletion()5018 public void testDefaultExceptionallyComposeAsync_exceptionalCompletion() { 5019 for (boolean createIncomplete : new boolean[] { true, false }) 5020 { 5021 final CFException ex = new CFException(); 5022 final ExceptionalCompletableFutureFunction r = 5023 new ExceptionalCompletableFutureFunction(ExecutionMode.ASYNC); 5024 final CompletableFuture<Item> f = new CompletableFuture<>(); 5025 final DelegatedCompletionStage<Item> d = 5026 new DelegatedCompletionStage<>(f); 5027 if (!createIncomplete) f.completeExceptionally(ex); 5028 final CompletionStage<Item> g = d.exceptionallyComposeAsync(r); 5029 if (createIncomplete) f.completeExceptionally(ex); 5030 5031 checkCompletedExceptionally(f, ex); 5032 checkCompletedNormally(g.toCompletableFuture(), r.value); 5033 r.assertInvoked(); 5034 }} 5035 5036 /** 5037 * default-implemented exceptionallyComposeAsync completes 5038 * exceptionally on exception if action does 5039 */ testDefaultExceptionallyComposeAsync_actionFailed()5040 public void testDefaultExceptionallyComposeAsync_actionFailed() { 5041 for (boolean createIncomplete : new boolean[] { true, false }) 5042 { 5043 final CFException ex = new CFException(); 5044 final CompletableFuture<Item> f = new CompletableFuture<>(); 5045 final FailingExceptionalCompletableFutureFunction r 5046 = new FailingExceptionalCompletableFutureFunction(ExecutionMode.ASYNC); 5047 final DelegatedCompletionStage<Item> d = 5048 new DelegatedCompletionStage<>(f); 5049 if (!createIncomplete) f.completeExceptionally(ex); 5050 final CompletionStage<Item> g = d.exceptionallyComposeAsync(r); 5051 if (createIncomplete) f.completeExceptionally(ex); 5052 5053 checkCompletedExceptionally(f, ex); 5054 checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex); 5055 r.assertInvoked(); 5056 }} 5057 5058 /** 5059 * default-implemented exceptionallyComposeAsync result completes 5060 * normally after normal completion of source 5061 */ testDefaultExceptionallyComposeAsyncExecutor_normalCompletion()5062 public void testDefaultExceptionallyComposeAsyncExecutor_normalCompletion() { 5063 for (boolean createIncomplete : new boolean[] { true, false }) 5064 for (Item v1 : new Item[] { itemOne, null }) 5065 { 5066 final CompletableFuture<Item> f = new CompletableFuture<>(); 5067 final ExceptionalCompletableFutureFunction r = 5068 new ExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR); 5069 final DelegatedCompletionStage<Item> d = 5070 new DelegatedCompletionStage<>(f); 5071 if (!createIncomplete) assertTrue(f.complete(v1)); 5072 final CompletionStage<Item> g = d.exceptionallyComposeAsync(r, new ThreadExecutor()); 5073 if (createIncomplete) assertTrue(f.complete(v1)); 5074 5075 checkCompletedNormally(f, v1); 5076 checkCompletedNormally(g.toCompletableFuture(), v1); 5077 r.assertNotInvoked(); 5078 }} 5079 5080 /** 5081 * default-implemented exceptionallyComposeAsync result completes 5082 * normally after exceptional completion of source 5083 */ testDefaultExceptionallyComposeAsyncExecutor_exceptionalCompletion()5084 public void testDefaultExceptionallyComposeAsyncExecutor_exceptionalCompletion() { 5085 for (boolean createIncomplete : new boolean[] { true, false }) 5086 { 5087 final CFException ex = new CFException(); 5088 final ExceptionalCompletableFutureFunction r = 5089 new ExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR); 5090 final CompletableFuture<Item> f = new CompletableFuture<>(); 5091 final DelegatedCompletionStage<Item> d = 5092 new DelegatedCompletionStage<>(f); 5093 if (!createIncomplete) f.completeExceptionally(ex); 5094 final CompletionStage<Item> g = d.exceptionallyComposeAsync(r, new ThreadExecutor()); 5095 if (createIncomplete) f.completeExceptionally(ex); 5096 5097 checkCompletedExceptionally(f, ex); 5098 checkCompletedNormally(g.toCompletableFuture(), r.value); 5099 r.assertInvoked(); 5100 }} 5101 5102 /** 5103 * default-implemented exceptionallyComposeAsync completes 5104 * exceptionally on exception if action does 5105 */ testDefaultExceptionallyComposeAsyncExecutor_actionFailed()5106 public void testDefaultExceptionallyComposeAsyncExecutor_actionFailed() { 5107 for (boolean createIncomplete : new boolean[] { true, false }) 5108 { 5109 final CFException ex = new CFException(); 5110 final CompletableFuture<Item> f = new CompletableFuture<>(); 5111 final FailingExceptionalCompletableFutureFunction r 5112 = new FailingExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR); 5113 final DelegatedCompletionStage<Item> d = 5114 new DelegatedCompletionStage<>(f); 5115 if (!createIncomplete) f.completeExceptionally(ex); 5116 final CompletionStage<Item> g = d.exceptionallyComposeAsync(r, new ThreadExecutor()); 5117 if (createIncomplete) f.completeExceptionally(ex); 5118 5119 checkCompletedExceptionally(f, ex); 5120 checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex); 5121 r.assertInvoked(); 5122 }} 5123 5124 } 5125