93 |
|
* Returns the "raw" internal exceptional completion of f, |
94 |
|
* without any additional wrapping with CompletionException. |
95 |
|
*/ |
96 |
< |
<U> Throwable exceptionalCompletion(CompletableFuture<U> f) { |
97 |
< |
// handle (and whenComplete) can distinguish between "direct" |
98 |
< |
// and "wrapped" exceptional completion |
99 |
< |
return f.handle((U u, Throwable t) -> t).join(); |
96 |
> |
Throwable exceptionalCompletion(CompletableFuture<?> f) { |
97 |
> |
// handle (and whenComplete and exceptionally) can distinguish |
98 |
> |
// between "direct" and "wrapped" exceptional completion |
99 |
> |
return f.handle((u, t) -> t).join(); |
100 |
|
} |
101 |
|
|
102 |
|
void checkCompletedExceptionally(CompletableFuture<?> f, |
147 |
|
|
148 |
|
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
149 |
|
checkCompletedExceptionally(f, true, |
150 |
< |
(t) -> assertTrue(t instanceof CFException)); |
150 |
> |
t -> assertTrue(t instanceof CFException)); |
151 |
|
} |
152 |
|
|
153 |
|
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
154 |
|
checkCompletedExceptionally(f, true, |
155 |
< |
(t) -> assertTrue(t instanceof CancellationException)); |
155 |
> |
t -> assertTrue(t instanceof CancellationException)); |
156 |
|
} |
157 |
|
|
158 |
|
void checkCompletedWithTimeoutException(CompletableFuture<?> f) { |
159 |
|
checkCompletedExceptionally(f, false, |
160 |
< |
(t) -> assertTrue(t instanceof TimeoutException)); |
160 |
> |
t -> assertTrue(t instanceof TimeoutException)); |
161 |
|
} |
162 |
|
|
163 |
|
void checkCompletedWithWrappedException(CompletableFuture<?> f, |
164 |
|
Throwable ex) { |
165 |
< |
checkCompletedExceptionally(f, true, (t) -> assertSame(t, ex)); |
165 |
> |
checkCompletedExceptionally(f, true, t -> assertSame(t, ex)); |
166 |
|
} |
167 |
|
|
168 |
|
void checkCompletedExceptionally(CompletableFuture<?> f, Throwable ex) { |
169 |
< |
checkCompletedExceptionally(f, false, (t) -> assertSame(t, ex)); |
169 |
> |
checkCompletedExceptionally(f, false, t -> assertSame(t, ex)); |
170 |
|
} |
171 |
|
|
172 |
|
void checkCancelled(CompletableFuture<?> f) { |
3257 |
|
|
3258 |
|
() -> f.thenApply(null), |
3259 |
|
() -> f.thenApplyAsync(null), |
3260 |
< |
() -> f.thenApplyAsync((x) -> x, null), |
3260 |
> |
() -> f.thenApplyAsync(x -> x, null), |
3261 |
|
() -> f.thenApplyAsync(null, exec), |
3262 |
|
|
3263 |
|
() -> f.thenAccept(null), |
3264 |
|
() -> f.thenAcceptAsync(null), |
3265 |
< |
() -> f.thenAcceptAsync((x) -> {} , null), |
3265 |
> |
() -> f.thenAcceptAsync(x -> {} , null), |
3266 |
|
() -> f.thenAcceptAsync(null, exec), |
3267 |
|
|
3268 |
|
() -> f.thenRun(null), |
3297 |
|
() -> f.applyToEither(g, null), |
3298 |
|
() -> f.applyToEitherAsync(g, null), |
3299 |
|
() -> f.applyToEitherAsync(g, null, exec), |
3300 |
< |
() -> f.applyToEither(nullFuture, (x) -> x), |
3301 |
< |
() -> f.applyToEitherAsync(nullFuture, (x) -> x), |
3302 |
< |
() -> f.applyToEitherAsync(nullFuture, (x) -> x, exec), |
3303 |
< |
() -> f.applyToEitherAsync(g, (x) -> x, null), |
3300 |
> |
() -> f.applyToEither(nullFuture, x -> x), |
3301 |
> |
() -> f.applyToEitherAsync(nullFuture, x -> x), |
3302 |
> |
() -> f.applyToEitherAsync(nullFuture, x -> x, exec), |
3303 |
> |
() -> f.applyToEitherAsync(g, x -> x, null), |
3304 |
|
|
3305 |
|
() -> f.acceptEither(g, null), |
3306 |
|
() -> f.acceptEitherAsync(g, null), |
3307 |
|
() -> f.acceptEitherAsync(g, null, exec), |
3308 |
< |
() -> f.acceptEither(nullFuture, (x) -> {}), |
3309 |
< |
() -> f.acceptEitherAsync(nullFuture, (x) -> {}), |
3310 |
< |
() -> f.acceptEitherAsync(nullFuture, (x) -> {}, exec), |
3311 |
< |
() -> f.acceptEitherAsync(g, (x) -> {}, null), |
3308 |
> |
() -> f.acceptEither(nullFuture, x -> {}), |
3309 |
> |
() -> f.acceptEitherAsync(nullFuture, x -> {}), |
3310 |
> |
() -> f.acceptEitherAsync(nullFuture, x -> {}, exec), |
3311 |
> |
() -> f.acceptEitherAsync(g, x -> {}, null), |
3312 |
|
|
3313 |
|
() -> f.runAfterEither(g, null), |
3314 |
|
() -> f.runAfterEitherAsync(g, null), |
3374 |
|
for (CompletableFuture<Integer> src : srcs) { |
3375 |
|
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3376 |
|
fs.add(src.thenRunAsync(() -> {}, e)); |
3377 |
< |
fs.add(src.thenAcceptAsync((z) -> {}, e)); |
3378 |
< |
fs.add(src.thenApplyAsync((z) -> z, e)); |
3377 |
> |
fs.add(src.thenAcceptAsync(z -> {}, e)); |
3378 |
> |
fs.add(src.thenApplyAsync(z -> z, e)); |
3379 |
|
|
3380 |
|
fs.add(src.thenCombineAsync(src, (x, y) -> x, e)); |
3381 |
|
fs.add(src.thenAcceptBothAsync(src, (x, y) -> {}, e)); |
3382 |
|
fs.add(src.runAfterBothAsync(src, () -> {}, e)); |
3383 |
|
|
3384 |
< |
fs.add(src.applyToEitherAsync(src, (z) -> z, e)); |
3385 |
< |
fs.add(src.acceptEitherAsync(src, (z) -> {}, e)); |
3384 |
> |
fs.add(src.applyToEitherAsync(src, z -> z, e)); |
3385 |
> |
fs.add(src.acceptEitherAsync(src, z -> {}, e)); |
3386 |
|
fs.add(src.runAfterEitherAsync(src, () -> {}, e)); |
3387 |
|
|
3388 |
< |
fs.add(src.thenComposeAsync((z) -> null, e)); |
3388 |
> |
fs.add(src.thenComposeAsync(z -> null, e)); |
3389 |
|
fs.add(src.whenCompleteAsync((z, t) -> {}, e)); |
3390 |
|
fs.add(src.handleAsync((z, t) -> null, e)); |
3391 |
|
|
3418 |
|
{ |
3419 |
|
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3420 |
|
|
3421 |
< |
fs.add(complete.applyToEitherAsync(incomplete, (z) -> z, e)); |
3422 |
< |
fs.add(incomplete.applyToEitherAsync(complete, (z) -> z, e)); |
3421 |
> |
fs.add(complete.applyToEitherAsync(incomplete, z -> z, e)); |
3422 |
> |
fs.add(incomplete.applyToEitherAsync(complete, z -> z, e)); |
3423 |
|
|
3424 |
< |
fs.add(complete.acceptEitherAsync(incomplete, (z) -> {}, e)); |
3425 |
< |
fs.add(incomplete.acceptEitherAsync(complete, (z) -> {}, e)); |
3424 |
> |
fs.add(complete.acceptEitherAsync(incomplete, z -> {}, e)); |
3425 |
> |
fs.add(incomplete.acceptEitherAsync(complete, z -> {}, e)); |
3426 |
|
|
3427 |
|
fs.add(complete.runAfterEitherAsync(incomplete, () -> {}, e)); |
3428 |
|
fs.add(incomplete.runAfterEitherAsync(complete, () -> {}, e)); |
3461 |
|
|
3462 |
|
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3463 |
|
fs.add(incomplete.thenRunAsync(() -> {}, e)); |
3464 |
< |
fs.add(incomplete.thenAcceptAsync((z) -> {}, e)); |
3465 |
< |
fs.add(incomplete.thenApplyAsync((z) -> z, e)); |
3464 |
> |
fs.add(incomplete.thenAcceptAsync(z -> {}, e)); |
3465 |
> |
fs.add(incomplete.thenApplyAsync(z -> z, e)); |
3466 |
|
|
3467 |
|
fs.add(incomplete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3468 |
|
fs.add(incomplete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3469 |
|
fs.add(incomplete.runAfterBothAsync(incomplete, () -> {}, e)); |
3470 |
|
|
3471 |
< |
fs.add(incomplete.applyToEitherAsync(incomplete, (z) -> z, e)); |
3472 |
< |
fs.add(incomplete.acceptEitherAsync(incomplete, (z) -> {}, e)); |
3471 |
> |
fs.add(incomplete.applyToEitherAsync(incomplete, z -> z, e)); |
3472 |
> |
fs.add(incomplete.acceptEitherAsync(incomplete, z -> {}, e)); |
3473 |
|
fs.add(incomplete.runAfterEitherAsync(incomplete, () -> {}, e)); |
3474 |
|
|
3475 |
< |
fs.add(incomplete.thenComposeAsync((z) -> null, e)); |
3475 |
> |
fs.add(incomplete.thenComposeAsync(z -> null, e)); |
3476 |
|
fs.add(incomplete.whenCompleteAsync((z, t) -> {}, e)); |
3477 |
|
fs.add(incomplete.handleAsync((z, t) -> null, e)); |
3478 |
|
|
3693 |
|
public void testCompleteAsync2() { |
3694 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3695 |
|
CFException ex = new CFException(); |
3696 |
< |
f.completeAsync(() -> {if (true) throw ex; return 1;}); |
3696 |
> |
f.completeAsync(() -> { throw ex; }); |
3697 |
|
try { |
3698 |
|
f.join(); |
3699 |
|
shouldThrow(); |
3723 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3724 |
|
CFException ex = new CFException(); |
3725 |
|
ThreadExecutor executor = new ThreadExecutor(); |
3726 |
< |
f.completeAsync(() -> {if (true) throw ex; return 1;}, executor); |
3726 |
> |
f.completeAsync(() -> { throw ex; }, executor); |
3727 |
|
try { |
3728 |
|
f.join(); |
3729 |
|
shouldThrow(); |
3882 |
|
List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs |
3883 |
|
= new ArrayList<>(); |
3884 |
|
|
3885 |
< |
funs.add((y) -> m.thenRun(y, noopRunnable)); |
3886 |
< |
funs.add((y) -> m.thenAccept(y, noopConsumer)); |
3887 |
< |
funs.add((y) -> m.thenApply(y, incFunction)); |
3888 |
< |
|
3889 |
< |
funs.add((y) -> m.runAfterEither(y, incomplete, noopRunnable)); |
3890 |
< |
funs.add((y) -> m.acceptEither(y, incomplete, noopConsumer)); |
3891 |
< |
funs.add((y) -> m.applyToEither(y, incomplete, incFunction)); |
3892 |
< |
|
3893 |
< |
funs.add((y) -> m.runAfterBoth(y, v42, noopRunnable)); |
3894 |
< |
funs.add((y) -> m.runAfterBoth(v42, y, noopRunnable)); |
3895 |
< |
funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
3896 |
< |
funs.add((y) -> m.thenAcceptBoth(v42, y, new SubtractAction(m))); |
3897 |
< |
funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m))); |
3898 |
< |
funs.add((y) -> m.thenCombine(v42, y, new SubtractFunction(m))); |
3899 |
< |
|
3900 |
< |
funs.add((y) -> m.whenComplete(y, (Integer r, Throwable t) -> {})); |
3901 |
< |
|
3902 |
< |
funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m))); |
3903 |
< |
|
3904 |
< |
funs.add((y) -> CompletableFuture.allOf(y)); |
3905 |
< |
funs.add((y) -> CompletableFuture.allOf(y, v42)); |
3906 |
< |
funs.add((y) -> CompletableFuture.allOf(v42, y)); |
3907 |
< |
funs.add((y) -> CompletableFuture.anyOf(y)); |
3908 |
< |
funs.add((y) -> CompletableFuture.anyOf(y, incomplete)); |
3909 |
< |
funs.add((y) -> CompletableFuture.anyOf(incomplete, y)); |
3885 |
> |
funs.add(y -> m.thenRun(y, noopRunnable)); |
3886 |
> |
funs.add(y -> m.thenAccept(y, noopConsumer)); |
3887 |
> |
funs.add(y -> m.thenApply(y, incFunction)); |
3888 |
> |
|
3889 |
> |
funs.add(y -> m.runAfterEither(y, incomplete, noopRunnable)); |
3890 |
> |
funs.add(y -> m.acceptEither(y, incomplete, noopConsumer)); |
3891 |
> |
funs.add(y -> m.applyToEither(y, incomplete, incFunction)); |
3892 |
> |
|
3893 |
> |
funs.add(y -> m.runAfterBoth(y, v42, noopRunnable)); |
3894 |
> |
funs.add(y -> m.runAfterBoth(v42, y, noopRunnable)); |
3895 |
> |
funs.add(y -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
3896 |
> |
funs.add(y -> m.thenAcceptBoth(v42, y, new SubtractAction(m))); |
3897 |
> |
funs.add(y -> m.thenCombine(y, v42, new SubtractFunction(m))); |
3898 |
> |
funs.add(y -> m.thenCombine(v42, y, new SubtractFunction(m))); |
3899 |
> |
|
3900 |
> |
funs.add(y -> m.whenComplete(y, (Integer r, Throwable t) -> {})); |
3901 |
> |
|
3902 |
> |
funs.add(y -> m.thenCompose(y, new CompletableFutureInc(m))); |
3903 |
> |
|
3904 |
> |
funs.add(y -> CompletableFuture.allOf(y)); |
3905 |
> |
funs.add(y -> CompletableFuture.allOf(y, v42)); |
3906 |
> |
funs.add(y -> CompletableFuture.allOf(v42, y)); |
3907 |
> |
funs.add(y -> CompletableFuture.anyOf(y)); |
3908 |
> |
funs.add(y -> CompletableFuture.anyOf(y, incomplete)); |
3909 |
> |
funs.add(y -> CompletableFuture.anyOf(incomplete, y)); |
3910 |
|
|
3911 |
|
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3912 |
|
fun : funs) { |
3963 |
|
public void testMinimalCompletionStage_minimality() { |
3964 |
|
if (!testImplementationDetails) return; |
3965 |
|
Function<Method, String> toSignature = |
3966 |
< |
(method) -> method.getName() + Arrays.toString(method.getParameterTypes()); |
3966 |
> |
method -> method.getName() + Arrays.toString(method.getParameterTypes()); |
3967 |
|
Predicate<Method> isNotStatic = |
3968 |
< |
(method) -> (method.getModifiers() & Modifier.STATIC) == 0; |
3968 |
> |
method -> (method.getModifiers() & Modifier.STATIC) == 0; |
3969 |
|
List<Method> minimalMethods = |
3970 |
|
Stream.of(Object.class, CompletionStage.class) |
3971 |
< |
.flatMap((klazz) -> Stream.of(klazz.getMethods())) |
3971 |
> |
.flatMap(klazz -> Stream.of(klazz.getMethods())) |
3972 |
|
.filter(isNotStatic) |
3973 |
|
.collect(Collectors.toList()); |
3974 |
|
// Methods from CompletableFuture permitted NOT to throw UOE |
3984 |
|
.collect(Collectors.toSet()); |
3985 |
|
List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods()) |
3986 |
|
.filter(isNotStatic) |
3987 |
< |
.filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method))) |
3987 |
> |
.filter(method -> !permittedMethodSignatures.contains(toSignature.apply(method))) |
3988 |
|
.collect(Collectors.toList()); |
3989 |
|
|
3990 |
|
List<CompletionStage<Integer>> stages = new ArrayList<>(); |
4144 |
|
CompletionStage<Integer> minimal = f.minimalCompletionStage(); |
4145 |
|
CompletableFuture<Integer> g = new CompletableFuture<>(); |
4146 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
4147 |
< |
minimal.thenAccept((x) -> g.complete(x)); |
4147 |
> |
minimal.thenAccept(x -> g.complete(x)); |
4148 |
|
if (createIncomplete) assertTrue(f.complete(v1)); |
4149 |
|
g.join(); |
4150 |
|
checkCompletedNormally(g, v1); |
4168 |
|
static <T,U,V> Function<T, CompletableFuture<V>> compose |
4169 |
|
(Function<T, CompletableFuture<U>> f, |
4170 |
|
Function<U, CompletableFuture<V>> g) { |
4171 |
< |
return (x) -> f.apply(x).thenCompose(g); |
4171 |
> |
return x -> f.apply(x).thenCompose(g); |
4172 |
|
} |
4173 |
|
|
4174 |
|
static void assertZero(CompletableFuture<?> f) { |
4248 |
|
|
4249 |
|
// Some mutually non-commutative functions |
4250 |
|
Function<Long, CompletableFuture<Long>> triple |
4251 |
< |
= (x) -> Monad.unit(3 * x); |
4251 |
> |
= x -> Monad.unit(3 * x); |
4252 |
|
Function<Long, CompletableFuture<Long>> inc |
4253 |
< |
= (x) -> Monad.unit(x + 1); |
4253 |
> |
= x -> Monad.unit(x + 1); |
4254 |
|
|
4255 |
|
// unit is a right identity: m >>= unit === m |
4256 |
|
Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit), |
4262 |
|
// associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) ) |
4263 |
|
Monad.assertFutureEquals( |
4264 |
|
unit.apply(5L).thenCompose(inc).thenCompose(triple), |
4265 |
< |
unit.apply(5L).thenCompose((x) -> inc.apply(x).thenCompose(triple))); |
4265 |
> |
unit.apply(5L).thenCompose(x -> inc.apply(x).thenCompose(triple))); |
4266 |
|
|
4267 |
|
// The case for CompletableFuture as an additive monad is weaker... |
4268 |
|
|
4272 |
|
// left zero: zero >>= f === zero |
4273 |
|
Monad.assertZero(zero.thenCompose(inc)); |
4274 |
|
// right zero: f >>= (\x -> zero) === zero |
4275 |
< |
Monad.assertZero(inc.apply(5L).thenCompose((x) -> zero)); |
4275 |
> |
Monad.assertZero(inc.apply(5L).thenCompose(x -> zero)); |
4276 |
|
|
4277 |
|
// f plus zero === f |
4278 |
|
Monad.assertFutureEquals(Monad.unit(5L), |
4330 |
|
final AtomicInteger count = new AtomicInteger(0); |
4331 |
|
for (int i = 0; i < n; i++) { |
4332 |
|
head.thenRun(() -> count.getAndIncrement()); |
4333 |
< |
head.thenAccept((x) -> count.getAndIncrement()); |
4334 |
< |
head.thenApply((x) -> count.getAndIncrement()); |
4333 |
> |
head.thenAccept(x -> count.getAndIncrement()); |
4334 |
> |
head.thenApply(x -> count.getAndIncrement()); |
4335 |
|
|
4336 |
|
head.runAfterBoth(complete, () -> count.getAndIncrement()); |
4337 |
|
head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement()); |
4341 |
|
complete.thenCombine(head, (x, y) -> count.getAndIncrement()); |
4342 |
|
|
4343 |
|
head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement()); |
4344 |
< |
head.acceptEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement()); |
4345 |
< |
head.applyToEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement()); |
4344 |
> |
head.acceptEither(new CompletableFuture<Void>(), x -> count.getAndIncrement()); |
4345 |
> |
head.applyToEither(new CompletableFuture<Void>(), x -> count.getAndIncrement()); |
4346 |
|
new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement()); |
4347 |
< |
new CompletableFuture<Void>().acceptEither(head, (x) -> count.getAndIncrement()); |
4348 |
< |
new CompletableFuture<Void>().applyToEither(head, (x) -> count.getAndIncrement()); |
4347 |
> |
new CompletableFuture<Void>().acceptEither(head, x -> count.getAndIncrement()); |
4348 |
> |
new CompletableFuture<Void>().applyToEither(head, x -> count.getAndIncrement()); |
4349 |
|
} |
4350 |
|
head.complete(null); |
4351 |
|
assertEquals(5 * 3 * n, count.get()); |
4362 |
|
f.complete(null); |
4363 |
|
|
4364 |
|
f = new CompletableFuture<>(); |
4365 |
< |
f.acceptEither(incomplete, (x) -> {}); |
4365 |
> |
f.acceptEither(incomplete, x -> {}); |
4366 |
|
f.complete(null); |
4367 |
|
|
4368 |
|
f = new CompletableFuture<>(); |
4369 |
< |
f.applyToEither(incomplete, (x) -> x); |
4369 |
> |
f.applyToEither(incomplete, x -> x); |
4370 |
|
f.complete(null); |
4371 |
|
|
4372 |
|
f = new CompletableFuture<>(); |
4380 |
|
f.complete(null); |
4381 |
|
|
4382 |
|
f = new CompletableFuture<>(); |
4383 |
< |
incomplete.acceptEither(f, (x) -> {}); |
4383 |
> |
incomplete.acceptEither(f, x -> {}); |
4384 |
|
f.complete(null); |
4385 |
|
|
4386 |
|
f = new CompletableFuture<>(); |
4387 |
< |
incomplete.applyToEither(f, (x) -> x); |
4387 |
> |
incomplete.applyToEither(f, x -> x); |
4388 |
|
f.complete(null); |
4389 |
|
|
4390 |
|
f = new CompletableFuture<>(); |