30 |
|
import java.util.concurrent.Executor; |
31 |
|
import java.util.concurrent.ForkJoinPool; |
32 |
|
import java.util.concurrent.ForkJoinTask; |
33 |
+ |
import java.util.concurrent.RejectedExecutionException; |
34 |
|
import java.util.concurrent.TimeoutException; |
35 |
|
import java.util.concurrent.TimeUnit; |
36 |
|
import java.util.concurrent.atomic.AtomicInteger; |
460 |
|
class FailingSupplier extends CheckedAction |
461 |
|
implements Supplier<Integer> |
462 |
|
{ |
463 |
< |
FailingSupplier(ExecutionMode m) { super(m); } |
463 |
> |
final CFException ex; |
464 |
> |
FailingSupplier(ExecutionMode m) { super(m); ex = new CFException(); } |
465 |
|
public Integer get() { |
466 |
|
invoked(); |
467 |
< |
throw new CFException(); |
467 |
> |
throw ex; |
468 |
|
} |
469 |
|
} |
470 |
|
|
471 |
|
class FailingConsumer extends CheckedIntegerAction |
472 |
|
implements Consumer<Integer> |
473 |
|
{ |
474 |
< |
FailingConsumer(ExecutionMode m) { super(m); } |
474 |
> |
final CFException ex; |
475 |
> |
FailingConsumer(ExecutionMode m) { super(m); ex = new CFException(); } |
476 |
|
public void accept(Integer x) { |
477 |
|
invoked(); |
478 |
|
value = x; |
479 |
< |
throw new CFException(); |
479 |
> |
throw ex; |
480 |
|
} |
481 |
|
} |
482 |
|
|
483 |
|
class FailingBiConsumer extends CheckedIntegerAction |
484 |
|
implements BiConsumer<Integer, Integer> |
485 |
|
{ |
486 |
< |
FailingBiConsumer(ExecutionMode m) { super(m); } |
486 |
> |
final CFException ex; |
487 |
> |
FailingBiConsumer(ExecutionMode m) { super(m); ex = new CFException(); } |
488 |
|
public void accept(Integer x, Integer y) { |
489 |
|
invoked(); |
490 |
|
value = subtract(x, y); |
491 |
< |
throw new CFException(); |
491 |
> |
throw ex; |
492 |
|
} |
493 |
|
} |
494 |
|
|
495 |
|
class FailingFunction extends CheckedIntegerAction |
496 |
|
implements Function<Integer, Integer> |
497 |
|
{ |
498 |
< |
FailingFunction(ExecutionMode m) { super(m); } |
498 |
> |
final CFException ex; |
499 |
> |
FailingFunction(ExecutionMode m) { super(m); ex = new CFException(); } |
500 |
|
public Integer apply(Integer x) { |
501 |
|
invoked(); |
502 |
|
value = x; |
503 |
< |
throw new CFException(); |
503 |
> |
throw ex; |
504 |
|
} |
505 |
|
} |
506 |
|
|
507 |
|
class FailingBiFunction extends CheckedIntegerAction |
508 |
|
implements BiFunction<Integer, Integer, Integer> |
509 |
|
{ |
510 |
< |
FailingBiFunction(ExecutionMode m) { super(m); } |
510 |
> |
final CFException ex; |
511 |
> |
FailingBiFunction(ExecutionMode m) { super(m); ex = new CFException(); } |
512 |
|
public Integer apply(Integer x, Integer y) { |
513 |
|
invoked(); |
514 |
|
value = subtract(x, y); |
515 |
< |
throw new CFException(); |
515 |
> |
throw ex; |
516 |
|
} |
517 |
|
} |
518 |
|
|
519 |
|
class FailingRunnable extends CheckedAction implements Runnable { |
520 |
< |
FailingRunnable(ExecutionMode m) { super(m); } |
520 |
> |
final CFException ex; |
521 |
> |
FailingRunnable(ExecutionMode m) { super(m); ex = new CFException(); } |
522 |
|
public void run() { |
523 |
|
invoked(); |
524 |
< |
throw new CFException(); |
524 |
> |
throw ex; |
525 |
|
} |
526 |
|
} |
527 |
|
|
541 |
|
class FailingCompletableFutureFunction extends CheckedIntegerAction |
542 |
|
implements Function<Integer, CompletableFuture<Integer>> |
543 |
|
{ |
544 |
< |
FailingCompletableFutureFunction(ExecutionMode m) { super(m); } |
544 |
> |
final CFException ex; |
545 |
> |
FailingCompletableFutureFunction(ExecutionMode m) { super(m); ex = new CFException(); } |
546 |
|
public CompletableFuture<Integer> apply(Integer x) { |
547 |
|
invoked(); |
548 |
|
value = x; |
549 |
< |
throw new CFException(); |
549 |
> |
throw ex; |
550 |
|
} |
551 |
|
} |
552 |
|
|
1048 |
|
|
1049 |
|
checkCompletedWithWrappedException(g, ex1); |
1050 |
|
checkCompletedExceptionally(f, ex1); |
1051 |
< |
assertEquals(1, ex1.getSuppressed().length); |
1052 |
< |
assertSame(ex2, ex1.getSuppressed()[0]); |
1051 |
> |
if (testImplementationDetails) { |
1052 |
> |
assertEquals(1, ex1.getSuppressed().length); |
1053 |
> |
assertSame(ex2, ex1.getSuppressed()[0]); |
1054 |
> |
} |
1055 |
|
assertEquals(1, a.get()); |
1056 |
|
}} |
1057 |
|
|
1230 |
|
{ |
1231 |
|
final FailingRunnable r = new FailingRunnable(m); |
1232 |
|
final CompletableFuture<Void> f = m.runAsync(r); |
1233 |
< |
checkCompletedWithWrappedCFException(f); |
1233 |
> |
checkCompletedWithWrappedException(f, r.ex); |
1234 |
|
r.assertInvoked(); |
1235 |
|
}} |
1236 |
|
|
1264 |
|
{ |
1265 |
|
FailingSupplier r = new FailingSupplier(m); |
1266 |
|
CompletableFuture<Integer> f = m.supplyAsync(r); |
1267 |
< |
checkCompletedWithWrappedCFException(f); |
1267 |
> |
checkCompletedWithWrappedException(f, r.ex); |
1268 |
|
r.assertInvoked(); |
1269 |
|
}} |
1270 |
|
|
1386 |
|
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1387 |
|
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1388 |
|
|
1389 |
< |
checkCompletedWithWrappedCFException(h0); |
1390 |
< |
checkCompletedWithWrappedCFException(h1); |
1391 |
< |
checkCompletedWithWrappedCFException(h2); |
1392 |
< |
checkCompletedWithWrappedCFException(h3); |
1393 |
< |
checkCompletedWithWrappedCFException(h4); |
1394 |
< |
checkCompletedWithWrappedCFException(h5); |
1389 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
1390 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
1391 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
1392 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
1393 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
1394 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
1395 |
|
checkCompletedNormally(f, v1); |
1396 |
|
}} |
1397 |
|
|
1490 |
|
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1491 |
|
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1492 |
|
|
1493 |
< |
checkCompletedWithWrappedCFException(h0); |
1494 |
< |
checkCompletedWithWrappedCFException(h1); |
1495 |
< |
checkCompletedWithWrappedCFException(h2); |
1496 |
< |
checkCompletedWithWrappedCFException(h3); |
1493 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
1494 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
1495 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
1496 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
1497 |
|
checkCompletedNormally(f, v1); |
1498 |
|
}} |
1499 |
|
|
1592 |
|
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1593 |
|
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1594 |
|
|
1595 |
< |
checkCompletedWithWrappedCFException(h0); |
1596 |
< |
checkCompletedWithWrappedCFException(h1); |
1597 |
< |
checkCompletedWithWrappedCFException(h2); |
1598 |
< |
checkCompletedWithWrappedCFException(h3); |
1595 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
1596 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
1597 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
1598 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
1599 |
|
checkCompletedNormally(f, v1); |
1600 |
|
}} |
1601 |
|
|
1757 |
|
assertTrue(snd.complete(w2)); |
1758 |
|
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1759 |
|
|
1760 |
< |
checkCompletedWithWrappedCFException(h1); |
1761 |
< |
checkCompletedWithWrappedCFException(h2); |
1762 |
< |
checkCompletedWithWrappedCFException(h3); |
1760 |
> |
checkCompletedWithWrappedException(h1, r1.ex); |
1761 |
> |
checkCompletedWithWrappedException(h2, r2.ex); |
1762 |
> |
checkCompletedWithWrappedException(h3, r3.ex); |
1763 |
|
r1.assertInvoked(); |
1764 |
|
r2.assertInvoked(); |
1765 |
|
r3.assertInvoked(); |
1921 |
|
assertTrue(snd.complete(w2)); |
1922 |
|
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1923 |
|
|
1924 |
< |
checkCompletedWithWrappedCFException(h1); |
1925 |
< |
checkCompletedWithWrappedCFException(h2); |
1926 |
< |
checkCompletedWithWrappedCFException(h3); |
1924 |
> |
checkCompletedWithWrappedException(h1, r1.ex); |
1925 |
> |
checkCompletedWithWrappedException(h2, r2.ex); |
1926 |
> |
checkCompletedWithWrappedException(h3, r3.ex); |
1927 |
|
r1.assertInvoked(); |
1928 |
|
r2.assertInvoked(); |
1929 |
|
r3.assertInvoked(); |
2085 |
|
assertTrue(snd.complete(w2)); |
2086 |
|
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2087 |
|
|
2088 |
< |
checkCompletedWithWrappedCFException(h1); |
2089 |
< |
checkCompletedWithWrappedCFException(h2); |
2090 |
< |
checkCompletedWithWrappedCFException(h3); |
2088 |
> |
checkCompletedWithWrappedException(h1, r1.ex); |
2089 |
> |
checkCompletedWithWrappedException(h2, r2.ex); |
2090 |
> |
checkCompletedWithWrappedException(h3, r3.ex); |
2091 |
|
r1.assertInvoked(); |
2092 |
|
r2.assertInvoked(); |
2093 |
|
r3.assertInvoked(); |
2377 |
|
f.complete(v1); |
2378 |
|
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2379 |
|
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2380 |
< |
checkCompletedWithWrappedCFException(h0); |
2381 |
< |
checkCompletedWithWrappedCFException(h1); |
2382 |
< |
checkCompletedWithWrappedCFException(h2); |
2383 |
< |
checkCompletedWithWrappedCFException(h3); |
2380 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
2381 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
2382 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
2383 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
2384 |
|
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2385 |
|
|
2386 |
|
g.complete(v2); |
2389 |
|
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2390 |
|
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2391 |
|
|
2392 |
< |
checkCompletedWithWrappedCFException(h4); |
2392 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
2393 |
|
assertTrue(Objects.equals(v1, rs[4].value) || |
2394 |
|
Objects.equals(v2, rs[4].value)); |
2395 |
< |
checkCompletedWithWrappedCFException(h5); |
2395 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
2396 |
|
assertTrue(Objects.equals(v1, rs[5].value) || |
2397 |
|
Objects.equals(v2, rs[5].value)); |
2398 |
|
|
2636 |
|
f.complete(v1); |
2637 |
|
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2638 |
|
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2639 |
< |
checkCompletedWithWrappedCFException(h0); |
2640 |
< |
checkCompletedWithWrappedCFException(h1); |
2641 |
< |
checkCompletedWithWrappedCFException(h2); |
2642 |
< |
checkCompletedWithWrappedCFException(h3); |
2639 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
2640 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
2641 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
2642 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
2643 |
|
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2644 |
|
|
2645 |
|
g.complete(v2); |
2648 |
|
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2649 |
|
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2650 |
|
|
2651 |
< |
checkCompletedWithWrappedCFException(h4); |
2651 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
2652 |
|
assertTrue(Objects.equals(v1, rs[4].value) || |
2653 |
|
Objects.equals(v2, rs[4].value)); |
2654 |
< |
checkCompletedWithWrappedCFException(h5); |
2654 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
2655 |
|
assertTrue(Objects.equals(v1, rs[5].value) || |
2656 |
|
Objects.equals(v2, rs[5].value)); |
2657 |
|
|
2891 |
|
assertTrue(f.complete(v1)); |
2892 |
|
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2893 |
|
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2894 |
< |
checkCompletedWithWrappedCFException(h0); |
2895 |
< |
checkCompletedWithWrappedCFException(h1); |
2896 |
< |
checkCompletedWithWrappedCFException(h2); |
2897 |
< |
checkCompletedWithWrappedCFException(h3); |
2894 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
2895 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
2896 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
2897 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
2898 |
|
for (int i = 0; i < 4; i++) rs[i].assertInvoked(); |
2899 |
|
assertTrue(g.complete(v2)); |
2900 |
|
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2901 |
|
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2902 |
< |
checkCompletedWithWrappedCFException(h4); |
2903 |
< |
checkCompletedWithWrappedCFException(h5); |
2902 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
2903 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
2904 |
|
|
2905 |
|
checkCompletedNormally(f, v1); |
2906 |
|
checkCompletedNormally(g, v2); |
2961 |
|
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2962 |
|
if (createIncomplete) assertTrue(f.complete(v1)); |
2963 |
|
|
2964 |
< |
checkCompletedWithWrappedCFException(g); |
2964 |
> |
checkCompletedWithWrappedException(g, r.ex); |
2965 |
|
checkCompletedNormally(f, v1); |
2966 |
|
}} |
2967 |
|
|
3070 |
|
} |
3071 |
|
} |
3072 |
|
|
3073 |
< |
public void testAllOf_backwards() throws Exception { |
3073 |
> |
public void testAllOf_normal_backwards() throws Exception { |
3074 |
|
for (int k = 1; k < 10; k++) { |
3075 |
|
CompletableFuture<Integer>[] fs |
3076 |
|
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3303 |
|
() -> f.obtrudeException(null), |
3304 |
|
|
3305 |
|
() -> CompletableFuture.delayedExecutor(1L, SECONDS, null), |
3306 |
< |
() -> CompletableFuture.delayedExecutor(1L, null, new ThreadExecutor()), |
3306 |
> |
() -> CompletableFuture.delayedExecutor(1L, null, exec), |
3307 |
|
() -> CompletableFuture.delayedExecutor(1L, null), |
3308 |
|
|
3309 |
|
() -> f.orTimeout(1L, null), |
3317 |
|
assertEquals(0, exec.count.get()); |
3318 |
|
} |
3319 |
|
|
3320 |
+ |
static class CountingRejectingExecutor implements Executor { |
3321 |
+ |
final RejectedExecutionException ex = new RejectedExecutionException(); |
3322 |
+ |
final AtomicInteger count = new AtomicInteger(0); |
3323 |
+ |
public void execute(Runnable r) { |
3324 |
+ |
count.getAndIncrement(); |
3325 |
+ |
throw ex; |
3326 |
+ |
} |
3327 |
+ |
} |
3328 |
+ |
|
3329 |
+ |
/** |
3330 |
+ |
* Test submissions to an executor that rejects all tasks. |
3331 |
+ |
*/ |
3332 |
+ |
public void testRejectingExecutor() { |
3333 |
+ |
for (Integer v : new Integer[] { 1, null }) { |
3334 |
+ |
|
3335 |
+ |
final CountingRejectingExecutor e = new CountingRejectingExecutor(); |
3336 |
+ |
|
3337 |
+ |
final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v); |
3338 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3339 |
+ |
|
3340 |
+ |
List<CompletableFuture<?>> futures = new ArrayList<>(); |
3341 |
+ |
|
3342 |
+ |
List<CompletableFuture<Integer>> srcs = new ArrayList<>(); |
3343 |
+ |
srcs.add(complete); |
3344 |
+ |
srcs.add(incomplete); |
3345 |
+ |
|
3346 |
+ |
for (CompletableFuture<Integer> src : srcs) { |
3347 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3348 |
+ |
fs.add(src.thenRunAsync(() -> {}, e)); |
3349 |
+ |
fs.add(src.thenAcceptAsync((z) -> {}, e)); |
3350 |
+ |
fs.add(src.thenApplyAsync((z) -> z, e)); |
3351 |
+ |
|
3352 |
+ |
fs.add(src.thenCombineAsync(src, (x, y) -> x, e)); |
3353 |
+ |
fs.add(src.thenAcceptBothAsync(src, (x, y) -> {}, e)); |
3354 |
+ |
fs.add(src.runAfterBothAsync(src, () -> {}, e)); |
3355 |
+ |
|
3356 |
+ |
fs.add(src.applyToEitherAsync(src, (z) -> z, e)); |
3357 |
+ |
fs.add(src.acceptEitherAsync(src, (z) -> {}, e)); |
3358 |
+ |
fs.add(src.runAfterEitherAsync(src, () -> {}, e)); |
3359 |
+ |
|
3360 |
+ |
fs.add(src.thenComposeAsync((z) -> null, e)); |
3361 |
+ |
fs.add(src.whenCompleteAsync((z, t) -> {}, e)); |
3362 |
+ |
fs.add(src.handleAsync((z, t) -> null, e)); |
3363 |
+ |
|
3364 |
+ |
for (CompletableFuture<?> future : fs) { |
3365 |
+ |
if (src.isDone()) |
3366 |
+ |
checkCompletedWithWrappedException(future, e.ex); |
3367 |
+ |
else |
3368 |
+ |
checkIncomplete(future); |
3369 |
+ |
} |
3370 |
+ |
futures.addAll(fs); |
3371 |
+ |
} |
3372 |
+ |
|
3373 |
+ |
{ |
3374 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3375 |
+ |
|
3376 |
+ |
fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3377 |
+ |
fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e)); |
3378 |
+ |
|
3379 |
+ |
fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3380 |
+ |
fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e)); |
3381 |
+ |
|
3382 |
+ |
fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e)); |
3383 |
+ |
fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e)); |
3384 |
+ |
|
3385 |
+ |
for (CompletableFuture<?> future : fs) |
3386 |
+ |
checkIncomplete(future); |
3387 |
+ |
futures.addAll(fs); |
3388 |
+ |
} |
3389 |
+ |
|
3390 |
+ |
{ |
3391 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3392 |
+ |
|
3393 |
+ |
fs.add(complete.applyToEitherAsync(incomplete, (z) -> z, e)); |
3394 |
+ |
fs.add(incomplete.applyToEitherAsync(complete, (z) -> z, e)); |
3395 |
+ |
|
3396 |
+ |
fs.add(complete.acceptEitherAsync(incomplete, (z) -> {}, e)); |
3397 |
+ |
fs.add(incomplete.acceptEitherAsync(complete, (z) -> {}, e)); |
3398 |
+ |
|
3399 |
+ |
fs.add(complete.runAfterEitherAsync(incomplete, () -> {}, e)); |
3400 |
+ |
fs.add(incomplete.runAfterEitherAsync(complete, () -> {}, e)); |
3401 |
+ |
|
3402 |
+ |
for (CompletableFuture<?> future : fs) |
3403 |
+ |
checkCompletedWithWrappedException(future, e.ex); |
3404 |
+ |
futures.addAll(fs); |
3405 |
+ |
} |
3406 |
+ |
|
3407 |
+ |
incomplete.complete(v); |
3408 |
+ |
|
3409 |
+ |
for (CompletableFuture<?> future : futures) |
3410 |
+ |
checkCompletedWithWrappedException(future, e.ex); |
3411 |
+ |
|
3412 |
+ |
assertEquals(futures.size(), e.count.get()); |
3413 |
+ |
|
3414 |
+ |
} |
3415 |
+ |
} |
3416 |
+ |
|
3417 |
+ |
/** |
3418 |
+ |
* Test submissions to an executor that rejects all tasks, but |
3419 |
+ |
* should never be invoked because the dependent future is |
3420 |
+ |
* explicitly completed. |
3421 |
+ |
*/ |
3422 |
+ |
public void testRejectingExecutorNeverInvoked() { |
3423 |
+ |
final CountingRejectingExecutor e = new CountingRejectingExecutor(); |
3424 |
+ |
|
3425 |
+ |
for (Integer v : new Integer[] { 1, null }) { |
3426 |
+ |
|
3427 |
+ |
final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v); |
3428 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3429 |
+ |
|
3430 |
+ |
List<CompletableFuture<?>> futures = new ArrayList<>(); |
3431 |
+ |
|
3432 |
+ |
List<CompletableFuture<Integer>> srcs = new ArrayList<>(); |
3433 |
+ |
srcs.add(complete); |
3434 |
+ |
srcs.add(incomplete); |
3435 |
+ |
|
3436 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3437 |
+ |
fs.add(incomplete.thenRunAsync(() -> {}, e)); |
3438 |
+ |
fs.add(incomplete.thenAcceptAsync((z) -> {}, e)); |
3439 |
+ |
fs.add(incomplete.thenApplyAsync((z) -> z, e)); |
3440 |
+ |
|
3441 |
+ |
fs.add(incomplete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3442 |
+ |
fs.add(incomplete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3443 |
+ |
fs.add(incomplete.runAfterBothAsync(incomplete, () -> {}, e)); |
3444 |
+ |
|
3445 |
+ |
fs.add(incomplete.applyToEitherAsync(incomplete, (z) -> z, e)); |
3446 |
+ |
fs.add(incomplete.acceptEitherAsync(incomplete, (z) -> {}, e)); |
3447 |
+ |
fs.add(incomplete.runAfterEitherAsync(incomplete, () -> {}, e)); |
3448 |
+ |
|
3449 |
+ |
fs.add(incomplete.thenComposeAsync((z) -> null, e)); |
3450 |
+ |
fs.add(incomplete.whenCompleteAsync((z, t) -> {}, e)); |
3451 |
+ |
fs.add(incomplete.handleAsync((z, t) -> null, e)); |
3452 |
+ |
|
3453 |
+ |
fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3454 |
+ |
fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e)); |
3455 |
+ |
|
3456 |
+ |
fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3457 |
+ |
fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e)); |
3458 |
+ |
|
3459 |
+ |
fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e)); |
3460 |
+ |
fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e)); |
3461 |
+ |
|
3462 |
+ |
for (CompletableFuture<?> future : fs) |
3463 |
+ |
checkIncomplete(future); |
3464 |
+ |
|
3465 |
+ |
for (CompletableFuture<?> future : fs) |
3466 |
+ |
future.complete(null); |
3467 |
+ |
|
3468 |
+ |
incomplete.complete(v); |
3469 |
+ |
|
3470 |
+ |
for (CompletableFuture<?> future : fs) |
3471 |
+ |
checkCompletedNormally(future, null); |
3472 |
+ |
|
3473 |
+ |
assertEquals(0, e.count.get()); |
3474 |
+ |
|
3475 |
+ |
} |
3476 |
+ |
} |
3477 |
+ |
|
3478 |
|
/** |
3479 |
|
* toCompletableFuture returns this CompletableFuture. |
3480 |
|
*/ |
3691 |
|
long timeoutMillis = timeoutMillis(); |
3692 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3693 |
|
long startTime = System.nanoTime(); |
3694 |
< |
f.orTimeout(timeoutMillis, MILLISECONDS); |
3694 |
> |
assertSame(f, f.orTimeout(timeoutMillis, MILLISECONDS)); |
3695 |
|
checkCompletedWithTimeoutException(f); |
3696 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3697 |
|
} |
3706 |
|
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3707 |
|
long startTime = System.nanoTime(); |
3708 |
|
f.complete(v1); |
3709 |
< |
f.orTimeout(LONG_DELAY_MS, MILLISECONDS); |
3710 |
< |
g.orTimeout(LONG_DELAY_MS, MILLISECONDS); |
3709 |
> |
assertSame(f, f.orTimeout(LONG_DELAY_MS, MILLISECONDS)); |
3710 |
> |
assertSame(g, g.orTimeout(LONG_DELAY_MS, MILLISECONDS)); |
3711 |
|
g.complete(v1); |
3712 |
|
checkCompletedNormally(f, v1); |
3713 |
|
checkCompletedNormally(g, v1); |
3722 |
|
() -> testCompleteOnTimeout_timesOut(null)); |
3723 |
|
} |
3724 |
|
|
3725 |
+ |
/** |
3726 |
+ |
* completeOnTimeout completes with given value if not complete |
3727 |
+ |
*/ |
3728 |
|
public void testCompleteOnTimeout_timesOut(Integer v) { |
3729 |
|
long timeoutMillis = timeoutMillis(); |
3730 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3731 |
|
long startTime = System.nanoTime(); |
3732 |
< |
f.completeOnTimeout(v, timeoutMillis, MILLISECONDS); |
3732 |
> |
assertSame(f, f.completeOnTimeout(v, timeoutMillis, MILLISECONDS)); |
3733 |
|
assertSame(v, f.join()); |
3734 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3735 |
|
f.complete(99); // should have no effect |
3746 |
|
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3747 |
|
long startTime = System.nanoTime(); |
3748 |
|
f.complete(v1); |
3749 |
< |
f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS); |
3750 |
< |
g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS); |
3749 |
> |
assertSame(f, f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS)); |
3750 |
> |
assertSame(g, g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS)); |
3751 |
|
g.complete(v1); |
3752 |
|
checkCompletedNormally(f, v1); |
3753 |
|
checkCompletedNormally(g, v1); |
3798 |
|
//--- tests of implementation details; not part of official tck --- |
3799 |
|
|
3800 |
|
Object resultOf(CompletableFuture<?> f) { |
3801 |
+ |
SecurityManager sm = System.getSecurityManager(); |
3802 |
+ |
if (sm != null) { |
3803 |
+ |
try { |
3804 |
+ |
System.setSecurityManager(null); |
3805 |
+ |
} catch (SecurityException giveUp) { |
3806 |
+ |
return "Reflection not available"; |
3807 |
+ |
} |
3808 |
+ |
} |
3809 |
+ |
|
3810 |
|
try { |
3811 |
|
java.lang.reflect.Field resultField |
3812 |
|
= CompletableFuture.class.getDeclaredField("result"); |
3813 |
|
resultField.setAccessible(true); |
3814 |
|
return resultField.get(f); |
3815 |
< |
} catch (Throwable t) { throw new AssertionError(t); } |
3815 |
> |
} catch (Throwable t) { |
3816 |
> |
throw new AssertionError(t); |
3817 |
> |
} finally { |
3818 |
> |
if (sm != null) System.setSecurityManager(sm); |
3819 |
> |
} |
3820 |
|
} |
3821 |
|
|
3822 |
|
public void testExceptionPropagationReusesResultObject() { |
3839 |
|
funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m))); |
3840 |
|
|
3841 |
|
funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m))); |
3842 |
+ |
funs.add((y) -> m.runAfterBoth(v42, y, new Noop(m))); |
3843 |
|
funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
3844 |
+ |
funs.add((y) -> m.thenAcceptBoth(v42, y, new SubtractAction(m))); |
3845 |
|
funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m))); |
3846 |
+ |
funs.add((y) -> m.thenCombine(v42, y, new SubtractFunction(m))); |
3847 |
|
|
3848 |
|
funs.add((y) -> m.whenComplete(y, (Integer r, Throwable t) -> {})); |
3849 |
|
|
3850 |
|
funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m))); |
3851 |
|
|
3852 |
+ |
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y})); |
3853 |
|
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42})); |
3854 |
+ |
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {v42, y})); |
3855 |
+ |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y})); |
3856 |
|
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete})); |
3857 |
+ |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {incomplete, y})); |
3858 |
|
|
3859 |
|
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3860 |
|
fun : funs) { |
4114 |
|
Monad.plus(godot, Monad.unit(5L))); |
4115 |
|
} |
4116 |
|
|
4117 |
+ |
/** |
4118 |
+ |
* A single CompletableFuture with many dependents. |
4119 |
+ |
* A demo of scalability - runtime is O(n). |
4120 |
+ |
*/ |
4121 |
+ |
public void testManyDependents() throws Throwable { |
4122 |
+ |
final int n = 1_000; |
4123 |
+ |
final CompletableFuture<Void> head = new CompletableFuture<>(); |
4124 |
+ |
final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null); |
4125 |
+ |
final AtomicInteger count = new AtomicInteger(0); |
4126 |
+ |
for (int i = 0; i < n; i++) { |
4127 |
+ |
head.thenRun(() -> count.getAndIncrement()); |
4128 |
+ |
head.thenAccept((x) -> count.getAndIncrement()); |
4129 |
+ |
head.thenApply((x) -> count.getAndIncrement()); |
4130 |
+ |
|
4131 |
+ |
head.runAfterBoth(complete, () -> count.getAndIncrement()); |
4132 |
+ |
head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement()); |
4133 |
+ |
head.thenCombine(complete, (x, y) -> count.getAndIncrement()); |
4134 |
+ |
complete.runAfterBoth(head, () -> count.getAndIncrement()); |
4135 |
+ |
complete.thenAcceptBoth(head, (x, y) -> count.getAndIncrement()); |
4136 |
+ |
complete.thenCombine(head, (x, y) -> count.getAndIncrement()); |
4137 |
+ |
|
4138 |
+ |
head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement()); |
4139 |
+ |
head.acceptEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement()); |
4140 |
+ |
head.applyToEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement()); |
4141 |
+ |
new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement()); |
4142 |
+ |
new CompletableFuture<Void>().acceptEither(head, (x) -> count.getAndIncrement()); |
4143 |
+ |
new CompletableFuture<Void>().applyToEither(head, (x) -> count.getAndIncrement()); |
4144 |
+ |
} |
4145 |
+ |
head.complete(null); |
4146 |
+ |
assertEquals(5 * 3 * n, count.get()); |
4147 |
+ |
} |
4148 |
+ |
|
4149 |
|
// static <U> U join(CompletionStage<U> stage) { |
4150 |
|
// CompletableFuture<U> f = new CompletableFuture<>(); |
4151 |
|
// stage.whenComplete((v, ex) -> { |