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; |
550 |
|
} |
551 |
|
} |
552 |
|
|
553 |
+ |
static class CountingRejectingExecutor implements Executor { |
554 |
+ |
final RejectedExecutionException ex = new RejectedExecutionException(); |
555 |
+ |
final AtomicInteger count = new AtomicInteger(0); |
556 |
+ |
public void execute(Runnable r) { |
557 |
+ |
count.getAndIncrement(); |
558 |
+ |
throw ex; |
559 |
+ |
} |
560 |
+ |
} |
561 |
+ |
|
562 |
|
// Used for explicit executor tests |
563 |
|
static final class ThreadExecutor implements Executor { |
564 |
|
final AtomicInteger count = new AtomicInteger(0); |
1243 |
|
r.assertInvoked(); |
1244 |
|
}} |
1245 |
|
|
1246 |
+ |
public void testRunAsync_rejectingExecutor() { |
1247 |
+ |
CountingRejectingExecutor e = new CountingRejectingExecutor(); |
1248 |
+ |
try { |
1249 |
+ |
CompletableFuture.runAsync(() -> {}, e); |
1250 |
+ |
shouldThrow(); |
1251 |
+ |
} catch (Throwable t) { |
1252 |
+ |
assertSame(e.ex, t); |
1253 |
+ |
} |
1254 |
+ |
|
1255 |
+ |
assertEquals(1, e.count.get()); |
1256 |
+ |
} |
1257 |
+ |
|
1258 |
|
/** |
1259 |
|
* supplyAsync completes with result of supplier |
1260 |
|
*/ |
1289 |
|
r.assertInvoked(); |
1290 |
|
}} |
1291 |
|
|
1292 |
+ |
public void testSupplyAsync_rejectingExecutor() { |
1293 |
+ |
CountingRejectingExecutor e = new CountingRejectingExecutor(); |
1294 |
+ |
try { |
1295 |
+ |
CompletableFuture.supplyAsync(() -> null, e); |
1296 |
+ |
shouldThrow(); |
1297 |
+ |
} catch (Throwable t) { |
1298 |
+ |
assertSame(e.ex, t); |
1299 |
+ |
} |
1300 |
+ |
|
1301 |
+ |
assertEquals(1, e.count.get()); |
1302 |
+ |
} |
1303 |
+ |
|
1304 |
|
// seq completion methods |
1305 |
|
|
1306 |
|
/** |
2700 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2701 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2702 |
|
for (Integer v2 : new Integer[] { 2, null }) |
2703 |
+ |
for (boolean pushNop : new boolean[] { true, false }) |
2704 |
|
{ |
2705 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2706 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2713 |
|
checkIncomplete(h1); |
2714 |
|
rs[0].assertNotInvoked(); |
2715 |
|
rs[1].assertNotInvoked(); |
2716 |
+ |
if (pushNop) { // ad hoc test of intra-completion interference |
2717 |
+ |
m.thenRun(f, () -> {}); |
2718 |
+ |
m.thenRun(g, () -> {}); |
2719 |
+ |
} |
2720 |
|
f.complete(v1); |
2721 |
|
checkCompletedNormally(h0, null); |
2722 |
|
checkCompletedNormally(h1, null); |
3356 |
|
} |
3357 |
|
|
3358 |
|
/** |
3359 |
+ |
* Test submissions to an executor that rejects all tasks. |
3360 |
+ |
*/ |
3361 |
+ |
public void testRejectingExecutor() { |
3362 |
+ |
for (Integer v : new Integer[] { 1, null }) |
3363 |
+ |
{ |
3364 |
+ |
final CountingRejectingExecutor e = new CountingRejectingExecutor(); |
3365 |
+ |
|
3366 |
+ |
final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v); |
3367 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3368 |
+ |
|
3369 |
+ |
List<CompletableFuture<?>> futures = new ArrayList<>(); |
3370 |
+ |
|
3371 |
+ |
List<CompletableFuture<Integer>> srcs = new ArrayList<>(); |
3372 |
+ |
srcs.add(complete); |
3373 |
+ |
srcs.add(incomplete); |
3374 |
+ |
|
3375 |
+ |
for (CompletableFuture<Integer> src : srcs) { |
3376 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3377 |
+ |
fs.add(src.thenRunAsync(() -> {}, e)); |
3378 |
+ |
fs.add(src.thenAcceptAsync((z) -> {}, e)); |
3379 |
+ |
fs.add(src.thenApplyAsync((z) -> z, e)); |
3380 |
+ |
|
3381 |
+ |
fs.add(src.thenCombineAsync(src, (x, y) -> x, e)); |
3382 |
+ |
fs.add(src.thenAcceptBothAsync(src, (x, y) -> {}, e)); |
3383 |
+ |
fs.add(src.runAfterBothAsync(src, () -> {}, e)); |
3384 |
+ |
|
3385 |
+ |
fs.add(src.applyToEitherAsync(src, (z) -> z, e)); |
3386 |
+ |
fs.add(src.acceptEitherAsync(src, (z) -> {}, e)); |
3387 |
+ |
fs.add(src.runAfterEitherAsync(src, () -> {}, e)); |
3388 |
+ |
|
3389 |
+ |
fs.add(src.thenComposeAsync((z) -> null, e)); |
3390 |
+ |
fs.add(src.whenCompleteAsync((z, t) -> {}, e)); |
3391 |
+ |
fs.add(src.handleAsync((z, t) -> null, e)); |
3392 |
+ |
|
3393 |
+ |
for (CompletableFuture<?> future : fs) { |
3394 |
+ |
if (src.isDone()) |
3395 |
+ |
checkCompletedWithWrappedException(future, e.ex); |
3396 |
+ |
else |
3397 |
+ |
checkIncomplete(future); |
3398 |
+ |
} |
3399 |
+ |
futures.addAll(fs); |
3400 |
+ |
} |
3401 |
+ |
|
3402 |
+ |
{ |
3403 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3404 |
+ |
|
3405 |
+ |
fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3406 |
+ |
fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e)); |
3407 |
+ |
|
3408 |
+ |
fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3409 |
+ |
fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e)); |
3410 |
+ |
|
3411 |
+ |
fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e)); |
3412 |
+ |
fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e)); |
3413 |
+ |
|
3414 |
+ |
for (CompletableFuture<?> future : fs) |
3415 |
+ |
checkIncomplete(future); |
3416 |
+ |
futures.addAll(fs); |
3417 |
+ |
} |
3418 |
+ |
|
3419 |
+ |
{ |
3420 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3421 |
+ |
|
3422 |
+ |
fs.add(complete.applyToEitherAsync(incomplete, (z) -> z, e)); |
3423 |
+ |
fs.add(incomplete.applyToEitherAsync(complete, (z) -> z, e)); |
3424 |
+ |
|
3425 |
+ |
fs.add(complete.acceptEitherAsync(incomplete, (z) -> {}, e)); |
3426 |
+ |
fs.add(incomplete.acceptEitherAsync(complete, (z) -> {}, e)); |
3427 |
+ |
|
3428 |
+ |
fs.add(complete.runAfterEitherAsync(incomplete, () -> {}, e)); |
3429 |
+ |
fs.add(incomplete.runAfterEitherAsync(complete, () -> {}, e)); |
3430 |
+ |
|
3431 |
+ |
for (CompletableFuture<?> future : fs) |
3432 |
+ |
checkCompletedWithWrappedException(future, e.ex); |
3433 |
+ |
futures.addAll(fs); |
3434 |
+ |
} |
3435 |
+ |
|
3436 |
+ |
incomplete.complete(v); |
3437 |
+ |
|
3438 |
+ |
for (CompletableFuture<?> future : futures) |
3439 |
+ |
checkCompletedWithWrappedException(future, e.ex); |
3440 |
+ |
|
3441 |
+ |
assertEquals(futures.size(), e.count.get()); |
3442 |
+ |
}} |
3443 |
+ |
|
3444 |
+ |
/** |
3445 |
+ |
* Test submissions to an executor that rejects all tasks, but |
3446 |
+ |
* should never be invoked because the dependent future is |
3447 |
+ |
* explicitly completed. |
3448 |
+ |
*/ |
3449 |
+ |
public void testRejectingExecutorNeverInvoked() { |
3450 |
+ |
for (Integer v : new Integer[] { 1, null }) |
3451 |
+ |
{ |
3452 |
+ |
final CountingRejectingExecutor e = new CountingRejectingExecutor(); |
3453 |
+ |
|
3454 |
+ |
final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v); |
3455 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3456 |
+ |
|
3457 |
+ |
List<CompletableFuture<?>> futures = new ArrayList<>(); |
3458 |
+ |
|
3459 |
+ |
List<CompletableFuture<Integer>> srcs = new ArrayList<>(); |
3460 |
+ |
srcs.add(complete); |
3461 |
+ |
srcs.add(incomplete); |
3462 |
+ |
|
3463 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3464 |
+ |
fs.add(incomplete.thenRunAsync(() -> {}, e)); |
3465 |
+ |
fs.add(incomplete.thenAcceptAsync((z) -> {}, e)); |
3466 |
+ |
fs.add(incomplete.thenApplyAsync((z) -> z, e)); |
3467 |
+ |
|
3468 |
+ |
fs.add(incomplete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3469 |
+ |
fs.add(incomplete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3470 |
+ |
fs.add(incomplete.runAfterBothAsync(incomplete, () -> {}, e)); |
3471 |
+ |
|
3472 |
+ |
fs.add(incomplete.applyToEitherAsync(incomplete, (z) -> z, e)); |
3473 |
+ |
fs.add(incomplete.acceptEitherAsync(incomplete, (z) -> {}, e)); |
3474 |
+ |
fs.add(incomplete.runAfterEitherAsync(incomplete, () -> {}, e)); |
3475 |
+ |
|
3476 |
+ |
fs.add(incomplete.thenComposeAsync((z) -> null, e)); |
3477 |
+ |
fs.add(incomplete.whenCompleteAsync((z, t) -> {}, e)); |
3478 |
+ |
fs.add(incomplete.handleAsync((z, t) -> null, e)); |
3479 |
+ |
|
3480 |
+ |
fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3481 |
+ |
fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e)); |
3482 |
+ |
|
3483 |
+ |
fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3484 |
+ |
fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e)); |
3485 |
+ |
|
3486 |
+ |
fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e)); |
3487 |
+ |
fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e)); |
3488 |
+ |
|
3489 |
+ |
for (CompletableFuture<?> future : fs) |
3490 |
+ |
checkIncomplete(future); |
3491 |
+ |
|
3492 |
+ |
for (CompletableFuture<?> future : fs) |
3493 |
+ |
future.complete(null); |
3494 |
+ |
|
3495 |
+ |
incomplete.complete(v); |
3496 |
+ |
|
3497 |
+ |
for (CompletableFuture<?> future : fs) |
3498 |
+ |
checkCompletedNormally(future, null); |
3499 |
+ |
|
3500 |
+ |
assertEquals(0, e.count.get()); |
3501 |
+ |
}} |
3502 |
+ |
|
3503 |
+ |
/** |
3504 |
|
* toCompletableFuture returns this CompletableFuture. |
3505 |
|
*/ |
3506 |
|
public void testToCompletableFuture() { |
3852 |
|
final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42); |
3853 |
|
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3854 |
|
|
3855 |
+ |
final Runnable noopRunnable = new Noop(m); |
3856 |
+ |
final Consumer<Integer> noopConsumer = new NoopConsumer(m); |
3857 |
+ |
final Function<Integer, Integer> incFunction = new IncFunction(m); |
3858 |
+ |
|
3859 |
|
List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs |
3860 |
|
= new ArrayList<>(); |
3861 |
|
|
3862 |
< |
funs.add((y) -> m.thenRun(y, new Noop(m))); |
3863 |
< |
funs.add((y) -> m.thenAccept(y, new NoopConsumer(m))); |
3864 |
< |
funs.add((y) -> m.thenApply(y, new IncFunction(m))); |
3865 |
< |
|
3866 |
< |
funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m))); |
3867 |
< |
funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m))); |
3868 |
< |
funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m))); |
3862 |
> |
funs.add((y) -> m.thenRun(y, noopRunnable)); |
3863 |
> |
funs.add((y) -> m.thenAccept(y, noopConsumer)); |
3864 |
> |
funs.add((y) -> m.thenApply(y, incFunction)); |
3865 |
> |
|
3866 |
> |
funs.add((y) -> m.runAfterEither(y, incomplete, noopRunnable)); |
3867 |
> |
funs.add((y) -> m.acceptEither(y, incomplete, noopConsumer)); |
3868 |
> |
funs.add((y) -> m.applyToEither(y, incomplete, incFunction)); |
3869 |
|
|
3870 |
< |
funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m))); |
3871 |
< |
funs.add((y) -> m.runAfterBoth(v42, y, new Noop(m))); |
3870 |
> |
funs.add((y) -> m.runAfterBoth(y, v42, noopRunnable)); |
3871 |
> |
funs.add((y) -> m.runAfterBoth(v42, y, noopRunnable)); |
3872 |
|
funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
3873 |
|
funs.add((y) -> m.thenAcceptBoth(v42, y, new SubtractAction(m))); |
3874 |
|
funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m))); |
3878 |
|
|
3879 |
|
funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m))); |
3880 |
|
|
3881 |
< |
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y})); |
3882 |
< |
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42})); |
3883 |
< |
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {v42, y})); |
3884 |
< |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y})); |
3885 |
< |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete})); |
3886 |
< |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {incomplete, y})); |
3881 |
> |
funs.add((y) -> CompletableFuture.allOf(y)); |
3882 |
> |
funs.add((y) -> CompletableFuture.allOf(y, v42)); |
3883 |
> |
funs.add((y) -> CompletableFuture.allOf(v42, y)); |
3884 |
> |
funs.add((y) -> CompletableFuture.anyOf(y)); |
3885 |
> |
funs.add((y) -> CompletableFuture.anyOf(y, incomplete)); |
3886 |
> |
funs.add((y) -> CompletableFuture.anyOf(incomplete, y)); |
3887 |
|
|
3888 |
|
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3889 |
|
fun : funs) { |
3890 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3891 |
|
f.completeExceptionally(ex); |
3892 |
< |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3892 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3893 |
|
checkCompletedWithWrappedException(src, ex); |
3894 |
|
CompletableFuture<?> dep = fun.apply(src); |
3895 |
|
checkCompletedWithWrappedException(dep, ex); |
3899 |
|
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3900 |
|
fun : funs) { |
3901 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3902 |
< |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3902 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3903 |
|
CompletableFuture<?> dep = fun.apply(src); |
3904 |
|
f.completeExceptionally(ex); |
3905 |
|
checkCompletedWithWrappedException(src, ex); |
3913 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3914 |
|
f.cancel(mayInterruptIfRunning); |
3915 |
|
checkCancelled(f); |
3916 |
< |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3916 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3917 |
|
checkCompletedWithWrappedCancellationException(src); |
3918 |
|
CompletableFuture<?> dep = fun.apply(src); |
3919 |
|
checkCompletedWithWrappedCancellationException(dep); |
3924 |
|
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3925 |
|
fun : funs) { |
3926 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3927 |
< |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3927 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3928 |
|
CompletableFuture<?> dep = fun.apply(src); |
3929 |
|
f.cancel(mayInterruptIfRunning); |
3930 |
|
checkCancelled(f); |
3935 |
|
}} |
3936 |
|
|
3937 |
|
/** |
3938 |
< |
* Minimal completion stages throw UOE for all non-CompletionStage methods |
3938 |
> |
* Minimal completion stages throw UOE for most non-CompletionStage methods |
3939 |
|
*/ |
3940 |
|
public void testMinimalCompletionStage_minimality() { |
3941 |
|
if (!testImplementationDetails) return; |
3964 |
|
.filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method))) |
3965 |
|
.collect(Collectors.toList()); |
3966 |
|
|
3967 |
< |
CompletionStage<Integer> minimalStage = |
3968 |
< |
new CompletableFuture<Integer>().minimalCompletionStage(); |
3967 |
> |
List<CompletionStage<Integer>> stages = new ArrayList<>(); |
3968 |
> |
stages.add(new CompletableFuture<Integer>().minimalCompletionStage()); |
3969 |
> |
stages.add(CompletableFuture.completedStage(1)); |
3970 |
|
|
3971 |
|
List<Method> bugs = new ArrayList<>(); |
3972 |
|
for (Method method : allMethods) { |
3982 |
|
else if (parameterTypes[i] == long.class) |
3983 |
|
args[i] = 0L; |
3984 |
|
} |
3985 |
< |
try { |
3986 |
< |
method.invoke(minimalStage, args); |
3987 |
< |
bugs.add(method); |
3799 |
< |
} |
3800 |
< |
catch (java.lang.reflect.InvocationTargetException expected) { |
3801 |
< |
if (! (expected.getCause() instanceof UnsupportedOperationException)) { |
3985 |
> |
for (CompletionStage<Integer> stage : stages) { |
3986 |
> |
try { |
3987 |
> |
method.invoke(stage, args); |
3988 |
|
bugs.add(method); |
3803 |
– |
// expected.getCause().printStackTrace(); |
3989 |
|
} |
3990 |
+ |
catch (java.lang.reflect.InvocationTargetException expected) { |
3991 |
+ |
if (! (expected.getCause() instanceof UnsupportedOperationException)) { |
3992 |
+ |
bugs.add(method); |
3993 |
+ |
// expected.getCause().printStackTrace(); |
3994 |
+ |
} |
3995 |
+ |
} |
3996 |
+ |
catch (ReflectiveOperationException bad) { throw new Error(bad); } |
3997 |
|
} |
3806 |
– |
catch (ReflectiveOperationException bad) { throw new Error(bad); } |
3998 |
|
} |
3999 |
|
if (!bugs.isEmpty()) |
4000 |
< |
throw new Error("Methods did not throw UOE: " + bugs.toString()); |
4000 |
> |
throw new Error("Methods did not throw UOE: " + bugs); |
4001 |
|
} |
4002 |
|
|
4003 |
|
static class Monad { |
4146 |
|
Monad.plus(godot, Monad.unit(5L))); |
4147 |
|
} |
4148 |
|
|
4149 |
+ |
/** Test long recursive chains of CompletableFutures with cascading completions */ |
4150 |
+ |
public void testRecursiveChains() throws Throwable { |
4151 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
4152 |
+ |
for (boolean addDeadEnds : new boolean[] { true, false }) |
4153 |
+ |
{ |
4154 |
+ |
final int val = 42; |
4155 |
+ |
final int n = expensiveTests ? 1_000 : 2; |
4156 |
+ |
CompletableFuture<Integer> head = new CompletableFuture<>(); |
4157 |
+ |
CompletableFuture<Integer> tail = head; |
4158 |
+ |
for (int i = 0; i < n; i++) { |
4159 |
+ |
if (addDeadEnds) m.thenApply(tail, v -> v + 1); |
4160 |
+ |
tail = m.thenApply(tail, v -> v + 1); |
4161 |
+ |
if (addDeadEnds) m.applyToEither(tail, tail, v -> v + 1); |
4162 |
+ |
tail = m.applyToEither(tail, tail, v -> v + 1); |
4163 |
+ |
if (addDeadEnds) m.thenCombine(tail, tail, (v, w) -> v + 1); |
4164 |
+ |
tail = m.thenCombine(tail, tail, (v, w) -> v + 1); |
4165 |
+ |
} |
4166 |
+ |
head.complete(val); |
4167 |
+ |
assertEquals(val + 3 * n, (int) tail.join()); |
4168 |
+ |
}} |
4169 |
+ |
|
4170 |
|
/** |
4171 |
|
* A single CompletableFuture with many dependents. |
4172 |
|
* A demo of scalability - runtime is O(n). |
4173 |
|
*/ |
4174 |
|
public void testManyDependents() throws Throwable { |
4175 |
< |
final int n = 1_000; |
4175 |
> |
final int n = expensiveTests ? 1_000_000 : 10; |
4176 |
|
final CompletableFuture<Void> head = new CompletableFuture<>(); |
4177 |
|
final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null); |
4178 |
|
final AtomicInteger count = new AtomicInteger(0); |
4199 |
|
assertEquals(5 * 3 * n, count.get()); |
4200 |
|
} |
4201 |
|
|
4202 |
+ |
/** ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck */ |
4203 |
+ |
public void testCoCompletionGarbageRetention() throws Throwable { |
4204 |
+ |
final int n = expensiveTests ? 1_000_000 : 10; |
4205 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
4206 |
+ |
CompletableFuture<Integer> f; |
4207 |
+ |
for (int i = 0; i < n; i++) { |
4208 |
+ |
f = new CompletableFuture<>(); |
4209 |
+ |
f.runAfterEither(incomplete, () -> {}); |
4210 |
+ |
f.complete(null); |
4211 |
+ |
|
4212 |
+ |
f = new CompletableFuture<>(); |
4213 |
+ |
f.acceptEither(incomplete, (x) -> {}); |
4214 |
+ |
f.complete(null); |
4215 |
+ |
|
4216 |
+ |
f = new CompletableFuture<>(); |
4217 |
+ |
f.applyToEither(incomplete, (x) -> x); |
4218 |
+ |
f.complete(null); |
4219 |
+ |
|
4220 |
+ |
f = new CompletableFuture<>(); |
4221 |
+ |
CompletableFuture.anyOf(new CompletableFuture<?>[] { f, incomplete }); |
4222 |
+ |
f.complete(null); |
4223 |
+ |
} |
4224 |
+ |
|
4225 |
+ |
for (int i = 0; i < n; i++) { |
4226 |
+ |
f = new CompletableFuture<>(); |
4227 |
+ |
incomplete.runAfterEither(f, () -> {}); |
4228 |
+ |
f.complete(null); |
4229 |
+ |
|
4230 |
+ |
f = new CompletableFuture<>(); |
4231 |
+ |
incomplete.acceptEither(f, (x) -> {}); |
4232 |
+ |
f.complete(null); |
4233 |
+ |
|
4234 |
+ |
f = new CompletableFuture<>(); |
4235 |
+ |
incomplete.applyToEither(f, (x) -> x); |
4236 |
+ |
f.complete(null); |
4237 |
+ |
|
4238 |
+ |
f = new CompletableFuture<>(); |
4239 |
+ |
CompletableFuture.anyOf(new CompletableFuture<?>[] { incomplete, f }); |
4240 |
+ |
f.complete(null); |
4241 |
+ |
} |
4242 |
+ |
} |
4243 |
+ |
|
4244 |
+ |
/* |
4245 |
+ |
* Tests below currently fail in stress mode due to memory retention. |
4246 |
+ |
* ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck |
4247 |
+ |
*/ |
4248 |
+ |
|
4249 |
+ |
/** Checks for garbage retention with anyOf. */ |
4250 |
+ |
public void testAnyOfGarbageRetention() throws Throwable { |
4251 |
+ |
for (Integer v : new Integer[] { 1, null }) |
4252 |
+ |
{ |
4253 |
+ |
final int n = expensiveTests ? 100_000 : 10; |
4254 |
+ |
CompletableFuture<Integer>[] fs |
4255 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture<?>[100]; |
4256 |
+ |
for (int i = 0; i < fs.length; i++) |
4257 |
+ |
fs[i] = new CompletableFuture<>(); |
4258 |
+ |
fs[fs.length - 1].complete(v); |
4259 |
+ |
for (int i = 0; i < n; i++) |
4260 |
+ |
checkCompletedNormally(CompletableFuture.anyOf(fs), v); |
4261 |
+ |
}} |
4262 |
+ |
|
4263 |
+ |
/** Checks for garbage retention with allOf. */ |
4264 |
+ |
public void testCancelledAllOfGarbageRetention() throws Throwable { |
4265 |
+ |
final int n = expensiveTests ? 100_000 : 10; |
4266 |
+ |
CompletableFuture<Integer>[] fs |
4267 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture<?>[100]; |
4268 |
+ |
for (int i = 0; i < fs.length; i++) |
4269 |
+ |
fs[i] = new CompletableFuture<>(); |
4270 |
+ |
for (int i = 0; i < n; i++) |
4271 |
+ |
assertTrue(CompletableFuture.allOf(fs).cancel(false)); |
4272 |
+ |
} |
4273 |
+ |
|
4274 |
|
// static <U> U join(CompletionStage<U> stage) { |
4275 |
|
// CompletableFuture<U> f = new CompletableFuture<>(); |
4276 |
|
// stage.whenComplete((v, ex) -> { |