32 |
|
import java.util.concurrent.ForkJoinTask; |
33 |
|
import java.util.concurrent.RejectedExecutionException; |
34 |
|
import java.util.concurrent.TimeoutException; |
35 |
– |
import java.util.concurrent.TimeUnit; |
35 |
|
import java.util.concurrent.atomic.AtomicInteger; |
36 |
|
import java.util.concurrent.atomic.AtomicReference; |
37 |
|
import java.util.function.BiConsumer; |
361 |
|
checkCompletedNormally(f, "test"); |
362 |
|
} |
363 |
|
|
364 |
< |
abstract class CheckedAction { |
364 |
> |
abstract static class CheckedAction { |
365 |
|
int invocationCount = 0; |
366 |
|
final ExecutionMode m; |
367 |
|
CheckedAction(ExecutionMode m) { this.m = m; } |
373 |
|
void assertInvoked() { assertEquals(1, invocationCount); } |
374 |
|
} |
375 |
|
|
376 |
< |
abstract class CheckedIntegerAction extends CheckedAction { |
376 |
> |
abstract static class CheckedIntegerAction extends CheckedAction { |
377 |
|
Integer value; |
378 |
|
CheckedIntegerAction(ExecutionMode m) { super(m); } |
379 |
|
void assertValue(Integer expected) { |
382 |
|
} |
383 |
|
} |
384 |
|
|
385 |
< |
class IntegerSupplier extends CheckedAction |
385 |
> |
static class IntegerSupplier extends CheckedAction |
386 |
|
implements Supplier<Integer> |
387 |
|
{ |
388 |
|
final Integer value; |
401 |
|
return (x == null) ? null : x + 1; |
402 |
|
} |
403 |
|
|
404 |
< |
class NoopConsumer extends CheckedIntegerAction |
404 |
> |
static class NoopConsumer extends CheckedIntegerAction |
405 |
|
implements Consumer<Integer> |
406 |
|
{ |
407 |
|
NoopConsumer(ExecutionMode m) { super(m); } |
411 |
|
} |
412 |
|
} |
413 |
|
|
414 |
< |
class IncFunction extends CheckedIntegerAction |
414 |
> |
static class IncFunction extends CheckedIntegerAction |
415 |
|
implements Function<Integer,Integer> |
416 |
|
{ |
417 |
|
IncFunction(ExecutionMode m) { super(m); } |
429 |
|
- ((y == null) ? 99 : y.intValue()); |
430 |
|
} |
431 |
|
|
432 |
< |
class SubtractAction extends CheckedIntegerAction |
432 |
> |
static class SubtractAction extends CheckedIntegerAction |
433 |
|
implements BiConsumer<Integer, Integer> |
434 |
|
{ |
435 |
|
SubtractAction(ExecutionMode m) { super(m); } |
439 |
|
} |
440 |
|
} |
441 |
|
|
442 |
< |
class SubtractFunction extends CheckedIntegerAction |
442 |
> |
static class SubtractFunction extends CheckedIntegerAction |
443 |
|
implements BiFunction<Integer, Integer, Integer> |
444 |
|
{ |
445 |
|
SubtractFunction(ExecutionMode m) { super(m); } |
449 |
|
} |
450 |
|
} |
451 |
|
|
452 |
< |
class Noop extends CheckedAction implements Runnable { |
452 |
> |
static class Noop extends CheckedAction implements Runnable { |
453 |
|
Noop(ExecutionMode m) { super(m); } |
454 |
|
public void run() { |
455 |
|
invoked(); |
456 |
|
} |
457 |
|
} |
458 |
|
|
459 |
< |
class FailingSupplier extends CheckedAction |
459 |
> |
static class FailingSupplier extends CheckedAction |
460 |
|
implements Supplier<Integer> |
461 |
|
{ |
462 |
|
final CFException ex; |
467 |
|
} |
468 |
|
} |
469 |
|
|
470 |
< |
class FailingConsumer extends CheckedIntegerAction |
470 |
> |
static class FailingConsumer extends CheckedIntegerAction |
471 |
|
implements Consumer<Integer> |
472 |
|
{ |
473 |
|
final CFException ex; |
479 |
|
} |
480 |
|
} |
481 |
|
|
482 |
< |
class FailingBiConsumer extends CheckedIntegerAction |
482 |
> |
static class FailingBiConsumer extends CheckedIntegerAction |
483 |
|
implements BiConsumer<Integer, Integer> |
484 |
|
{ |
485 |
|
final CFException ex; |
491 |
|
} |
492 |
|
} |
493 |
|
|
494 |
< |
class FailingFunction extends CheckedIntegerAction |
494 |
> |
static class FailingFunction extends CheckedIntegerAction |
495 |
|
implements Function<Integer, Integer> |
496 |
|
{ |
497 |
|
final CFException ex; |
503 |
|
} |
504 |
|
} |
505 |
|
|
506 |
< |
class FailingBiFunction extends CheckedIntegerAction |
506 |
> |
static class FailingBiFunction extends CheckedIntegerAction |
507 |
|
implements BiFunction<Integer, Integer, Integer> |
508 |
|
{ |
509 |
|
final CFException ex; |
515 |
|
} |
516 |
|
} |
517 |
|
|
518 |
< |
class FailingRunnable extends CheckedAction implements Runnable { |
518 |
> |
static class FailingRunnable extends CheckedAction implements Runnable { |
519 |
|
final CFException ex; |
520 |
|
FailingRunnable(ExecutionMode m) { super(m); ex = new CFException(); } |
521 |
|
public void run() { |
524 |
|
} |
525 |
|
} |
526 |
|
|
527 |
< |
class CompletableFutureInc extends CheckedIntegerAction |
527 |
> |
static class CompletableFutureInc extends CheckedIntegerAction |
528 |
|
implements Function<Integer, CompletableFuture<Integer>> |
529 |
|
{ |
530 |
|
CompletableFutureInc(ExecutionMode m) { super(m); } |
537 |
|
} |
538 |
|
} |
539 |
|
|
540 |
< |
class FailingCompletableFutureFunction extends CheckedIntegerAction |
540 |
> |
static class FailingCompletableFutureFunction extends CheckedIntegerAction |
541 |
|
implements Function<Integer, CompletableFuture<Integer>> |
542 |
|
{ |
543 |
|
final CFException ex; |
549 |
|
} |
550 |
|
} |
551 |
|
|
552 |
+ |
static class CountingRejectingExecutor implements Executor { |
553 |
+ |
final RejectedExecutionException ex = new RejectedExecutionException(); |
554 |
+ |
final AtomicInteger count = new AtomicInteger(0); |
555 |
+ |
public void execute(Runnable r) { |
556 |
+ |
count.getAndIncrement(); |
557 |
+ |
throw ex; |
558 |
+ |
} |
559 |
+ |
} |
560 |
+ |
|
561 |
|
// Used for explicit executor tests |
562 |
|
static final class ThreadExecutor implements Executor { |
563 |
|
final AtomicInteger count = new AtomicInteger(0); |
1242 |
|
r.assertInvoked(); |
1243 |
|
}} |
1244 |
|
|
1245 |
+ |
public void testRunAsync_rejectingExecutor() { |
1246 |
+ |
CountingRejectingExecutor e = new CountingRejectingExecutor(); |
1247 |
+ |
try { |
1248 |
+ |
CompletableFuture.runAsync(() -> {}, e); |
1249 |
+ |
shouldThrow(); |
1250 |
+ |
} catch (Throwable t) { |
1251 |
+ |
assertSame(e.ex, t); |
1252 |
+ |
} |
1253 |
+ |
|
1254 |
+ |
assertEquals(1, e.count.get()); |
1255 |
+ |
} |
1256 |
+ |
|
1257 |
|
/** |
1258 |
|
* supplyAsync completes with result of supplier |
1259 |
|
*/ |
1288 |
|
r.assertInvoked(); |
1289 |
|
}} |
1290 |
|
|
1291 |
+ |
public void testSupplyAsync_rejectingExecutor() { |
1292 |
+ |
CountingRejectingExecutor e = new CountingRejectingExecutor(); |
1293 |
+ |
try { |
1294 |
+ |
CompletableFuture.supplyAsync(() -> null, e); |
1295 |
+ |
shouldThrow(); |
1296 |
+ |
} catch (Throwable t) { |
1297 |
+ |
assertSame(e.ex, t); |
1298 |
+ |
} |
1299 |
+ |
|
1300 |
+ |
assertEquals(1, e.count.get()); |
1301 |
+ |
} |
1302 |
+ |
|
1303 |
|
// seq completion methods |
1304 |
|
|
1305 |
|
/** |
2699 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2700 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2701 |
|
for (Integer v2 : new Integer[] { 2, null }) |
2702 |
+ |
for (boolean pushNop : new boolean[] { true, false }) |
2703 |
|
{ |
2704 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2705 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2712 |
|
checkIncomplete(h1); |
2713 |
|
rs[0].assertNotInvoked(); |
2714 |
|
rs[1].assertNotInvoked(); |
2715 |
+ |
if (pushNop) { // ad hoc test of intra-completion interference |
2716 |
+ |
m.thenRun(f, () -> {}); |
2717 |
+ |
m.thenRun(g, () -> {}); |
2718 |
+ |
} |
2719 |
|
f.complete(v1); |
2720 |
|
checkCompletedNormally(h0, null); |
2721 |
|
checkCompletedNormally(h1, null); |
3354 |
|
assertEquals(0, exec.count.get()); |
3355 |
|
} |
3356 |
|
|
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 |
– |
|
3357 |
|
/** |
3358 |
|
* Test submissions to an executor that rejects all tasks. |
3359 |
|
*/ |
3360 |
|
public void testRejectingExecutor() { |
3361 |
< |
for (Integer v : new Integer[] { 1, null }) { |
3362 |
< |
|
3361 |
> |
for (Integer v : new Integer[] { 1, null }) |
3362 |
> |
{ |
3363 |
|
final CountingRejectingExecutor e = new CountingRejectingExecutor(); |
3364 |
|
|
3365 |
|
final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v); |
3438 |
|
checkCompletedWithWrappedException(future, e.ex); |
3439 |
|
|
3440 |
|
assertEquals(futures.size(), e.count.get()); |
3441 |
< |
|
3414 |
< |
} |
3415 |
< |
} |
3441 |
> |
}} |
3442 |
|
|
3443 |
|
/** |
3444 |
|
* Test submissions to an executor that rejects all tasks, but |
3446 |
|
* explicitly completed. |
3447 |
|
*/ |
3448 |
|
public void testRejectingExecutorNeverInvoked() { |
3449 |
+ |
for (Integer v : new Integer[] { 1, null }) |
3450 |
+ |
{ |
3451 |
|
final CountingRejectingExecutor e = new CountingRejectingExecutor(); |
3452 |
|
|
3425 |
– |
for (Integer v : new Integer[] { 1, null }) { |
3426 |
– |
|
3453 |
|
final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v); |
3454 |
|
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3455 |
|
|
3497 |
|
checkCompletedNormally(future, null); |
3498 |
|
|
3499 |
|
assertEquals(0, e.count.get()); |
3500 |
< |
|
3475 |
< |
} |
3476 |
< |
} |
3500 |
> |
}} |
3501 |
|
|
3502 |
|
/** |
3503 |
|
* toCompletableFuture returns this CompletableFuture. |
3851 |
|
final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42); |
3852 |
|
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3853 |
|
|
3854 |
+ |
final Runnable noopRunnable = new Noop(m); |
3855 |
+ |
final Consumer<Integer> noopConsumer = new NoopConsumer(m); |
3856 |
+ |
final Function<Integer, Integer> incFunction = new IncFunction(m); |
3857 |
+ |
|
3858 |
|
List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs |
3859 |
|
= new ArrayList<>(); |
3860 |
|
|
3861 |
< |
funs.add((y) -> m.thenRun(y, new Noop(m))); |
3862 |
< |
funs.add((y) -> m.thenAccept(y, new NoopConsumer(m))); |
3863 |
< |
funs.add((y) -> m.thenApply(y, new IncFunction(m))); |
3864 |
< |
|
3865 |
< |
funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m))); |
3866 |
< |
funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m))); |
3867 |
< |
funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m))); |
3861 |
> |
funs.add((y) -> m.thenRun(y, noopRunnable)); |
3862 |
> |
funs.add((y) -> m.thenAccept(y, noopConsumer)); |
3863 |
> |
funs.add((y) -> m.thenApply(y, incFunction)); |
3864 |
> |
|
3865 |
> |
funs.add((y) -> m.runAfterEither(y, incomplete, noopRunnable)); |
3866 |
> |
funs.add((y) -> m.acceptEither(y, incomplete, noopConsumer)); |
3867 |
> |
funs.add((y) -> m.applyToEither(y, incomplete, incFunction)); |
3868 |
|
|
3869 |
< |
funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m))); |
3870 |
< |
funs.add((y) -> m.runAfterBoth(v42, y, new Noop(m))); |
3869 |
> |
funs.add((y) -> m.runAfterBoth(y, v42, noopRunnable)); |
3870 |
> |
funs.add((y) -> m.runAfterBoth(v42, y, noopRunnable)); |
3871 |
|
funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
3872 |
|
funs.add((y) -> m.thenAcceptBoth(v42, y, new SubtractAction(m))); |
3873 |
|
funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m))); |
3877 |
|
|
3878 |
|
funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m))); |
3879 |
|
|
3880 |
< |
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y})); |
3881 |
< |
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42})); |
3882 |
< |
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {v42, y})); |
3883 |
< |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y})); |
3884 |
< |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete})); |
3885 |
< |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {incomplete, y})); |
3880 |
> |
funs.add((y) -> CompletableFuture.allOf(y)); |
3881 |
> |
funs.add((y) -> CompletableFuture.allOf(y, v42)); |
3882 |
> |
funs.add((y) -> CompletableFuture.allOf(v42, y)); |
3883 |
> |
funs.add((y) -> CompletableFuture.anyOf(y)); |
3884 |
> |
funs.add((y) -> CompletableFuture.anyOf(y, incomplete)); |
3885 |
> |
funs.add((y) -> CompletableFuture.anyOf(incomplete, y)); |
3886 |
|
|
3887 |
|
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3888 |
|
fun : funs) { |
3889 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3890 |
|
f.completeExceptionally(ex); |
3891 |
< |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3891 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3892 |
|
checkCompletedWithWrappedException(src, ex); |
3893 |
|
CompletableFuture<?> dep = fun.apply(src); |
3894 |
|
checkCompletedWithWrappedException(dep, ex); |
3898 |
|
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3899 |
|
fun : funs) { |
3900 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3901 |
< |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3901 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3902 |
|
CompletableFuture<?> dep = fun.apply(src); |
3903 |
|
f.completeExceptionally(ex); |
3904 |
|
checkCompletedWithWrappedException(src, ex); |
3912 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3913 |
|
f.cancel(mayInterruptIfRunning); |
3914 |
|
checkCancelled(f); |
3915 |
< |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3915 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3916 |
|
checkCompletedWithWrappedCancellationException(src); |
3917 |
|
CompletableFuture<?> dep = fun.apply(src); |
3918 |
|
checkCompletedWithWrappedCancellationException(dep); |
3923 |
|
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3924 |
|
fun : funs) { |
3925 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3926 |
< |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3926 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3927 |
|
CompletableFuture<?> dep = fun.apply(src); |
3928 |
|
f.cancel(mayInterruptIfRunning); |
3929 |
|
checkCancelled(f); |
3934 |
|
}} |
3935 |
|
|
3936 |
|
/** |
3937 |
< |
* Minimal completion stages throw UOE for all non-CompletionStage methods |
3937 |
> |
* Minimal completion stages throw UOE for most non-CompletionStage methods |
3938 |
|
*/ |
3939 |
|
public void testMinimalCompletionStage_minimality() { |
3940 |
|
if (!testImplementationDetails) return; |
3963 |
|
.filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method))) |
3964 |
|
.collect(Collectors.toList()); |
3965 |
|
|
3966 |
< |
CompletionStage<Integer> minimalStage = |
3966 |
> |
List<CompletionStage<Integer>> stages = new ArrayList<>(); |
3967 |
> |
CompletionStage<Integer> min = |
3968 |
|
new CompletableFuture<Integer>().minimalCompletionStage(); |
3969 |
+ |
stages.add(min); |
3970 |
+ |
stages.add(min.thenApply(x -> x)); |
3971 |
+ |
stages.add(CompletableFuture.completedStage(1)); |
3972 |
+ |
stages.add(CompletableFuture.failedStage(new CFException())); |
3973 |
|
|
3974 |
|
List<Method> bugs = new ArrayList<>(); |
3975 |
|
for (Method method : allMethods) { |
3985 |
|
else if (parameterTypes[i] == long.class) |
3986 |
|
args[i] = 0L; |
3987 |
|
} |
3988 |
< |
try { |
3989 |
< |
method.invoke(minimalStage, args); |
3990 |
< |
bugs.add(method); |
3958 |
< |
} |
3959 |
< |
catch (java.lang.reflect.InvocationTargetException expected) { |
3960 |
< |
if (! (expected.getCause() instanceof UnsupportedOperationException)) { |
3988 |
> |
for (CompletionStage<Integer> stage : stages) { |
3989 |
> |
try { |
3990 |
> |
method.invoke(stage, args); |
3991 |
|
bugs.add(method); |
3962 |
– |
// expected.getCause().printStackTrace(); |
3992 |
|
} |
3993 |
+ |
catch (java.lang.reflect.InvocationTargetException expected) { |
3994 |
+ |
if (! (expected.getCause() instanceof UnsupportedOperationException)) { |
3995 |
+ |
bugs.add(method); |
3996 |
+ |
// expected.getCause().printStackTrace(); |
3997 |
+ |
} |
3998 |
+ |
} |
3999 |
+ |
catch (ReflectiveOperationException bad) { throw new Error(bad); } |
4000 |
|
} |
3965 |
– |
catch (ReflectiveOperationException bad) { throw new Error(bad); } |
4001 |
|
} |
4002 |
|
if (!bugs.isEmpty()) |
4003 |
< |
throw new Error("Methods did not throw UOE: " + bugs.toString()); |
4003 |
> |
throw new Error("Methods did not throw UOE: " + bugs); |
4004 |
|
} |
4005 |
|
|
4006 |
+ |
/** |
4007 |
+ |
* minimalStage.toCompletableFuture() gives mutable CompletableFuture |
4008 |
+ |
*/ |
4009 |
+ |
public void testMinimalCompletionStage_toCompletableFuture_mutable() { |
4010 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
4011 |
+ |
{ |
4012 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4013 |
+ |
CompletionStage minimal = f.minimalCompletionStage(); |
4014 |
+ |
CompletableFuture<Integer> g = minimal.toCompletableFuture(); |
4015 |
+ |
g.complete(v1); |
4016 |
+ |
checkCompletedNormally(g, v1); |
4017 |
+ |
checkIncomplete(f); |
4018 |
+ |
checkIncomplete(minimal.toCompletableFuture()); |
4019 |
+ |
}} |
4020 |
+ |
|
4021 |
+ |
/** |
4022 |
+ |
* minimalStage.toCompletableFuture().join() awaits completion |
4023 |
+ |
*/ |
4024 |
+ |
public void testMinimalCompletionStage_toCompletableFuture_join() throws Exception { |
4025 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
4026 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
4027 |
+ |
{ |
4028 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4029 |
+ |
if (!createIncomplete) assertTrue(f.complete(v1)); |
4030 |
+ |
CompletionStage<Integer> minimal = f.minimalCompletionStage(); |
4031 |
+ |
if (createIncomplete) assertTrue(f.complete(v1)); |
4032 |
+ |
assertEquals(v1, minimal.toCompletableFuture().join()); |
4033 |
+ |
assertEquals(v1, minimal.toCompletableFuture().get()); |
4034 |
+ |
checkCompletedNormally(minimal.toCompletableFuture(), v1); |
4035 |
+ |
}} |
4036 |
+ |
|
4037 |
+ |
/** Demo utility method for external reliable toCompletableFuture */ |
4038 |
+ |
static <T> CompletableFuture<T> toCompletableFuture(CompletionStage<T> stage) { |
4039 |
+ |
CompletableFuture<T> f = new CompletableFuture<>(); |
4040 |
+ |
stage.handle((T t, Throwable ex) -> { |
4041 |
+ |
if (ex != null) f.completeExceptionally(ex); |
4042 |
+ |
else f.complete(t); |
4043 |
+ |
return null; |
4044 |
+ |
}); |
4045 |
+ |
return f; |
4046 |
+ |
} |
4047 |
+ |
|
4048 |
+ |
/** Demo utility method to join a CompletionStage */ |
4049 |
+ |
static <T> T join(CompletionStage<T> stage) { |
4050 |
+ |
return toCompletableFuture(stage).join(); |
4051 |
+ |
} |
4052 |
+ |
|
4053 |
+ |
/** |
4054 |
+ |
* Joining a minimal stage "by hand" works |
4055 |
+ |
*/ |
4056 |
+ |
public void testMinimalCompletionStage_join_by_hand() { |
4057 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
4058 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
4059 |
+ |
{ |
4060 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4061 |
+ |
CompletionStage<Integer> minimal = f.minimalCompletionStage(); |
4062 |
+ |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
4063 |
+ |
if (!createIncomplete) assertTrue(f.complete(v1)); |
4064 |
+ |
minimal.thenAccept((x) -> g.complete(x)); |
4065 |
+ |
if (createIncomplete) assertTrue(f.complete(v1)); |
4066 |
+ |
g.join(); |
4067 |
+ |
checkCompletedNormally(g, v1); |
4068 |
+ |
checkCompletedNormally(f, v1); |
4069 |
+ |
assertEquals(v1, join(minimal)); |
4070 |
+ |
}} |
4071 |
+ |
|
4072 |
|
static class Monad { |
4073 |
|
static class ZeroException extends RuntimeException { |
4074 |
|
public ZeroException() { super("monadic zero"); } |
4215 |
|
Monad.plus(godot, Monad.unit(5L))); |
4216 |
|
} |
4217 |
|
|
4218 |
+ |
/** Test long recursive chains of CompletableFutures with cascading completions */ |
4219 |
+ |
public void testRecursiveChains() throws Throwable { |
4220 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
4221 |
+ |
for (boolean addDeadEnds : new boolean[] { true, false }) |
4222 |
+ |
{ |
4223 |
+ |
final int val = 42; |
4224 |
+ |
final int n = expensiveTests ? 1_000 : 2; |
4225 |
+ |
CompletableFuture<Integer> head = new CompletableFuture<>(); |
4226 |
+ |
CompletableFuture<Integer> tail = head; |
4227 |
+ |
for (int i = 0; i < n; i++) { |
4228 |
+ |
if (addDeadEnds) m.thenApply(tail, v -> v + 1); |
4229 |
+ |
tail = m.thenApply(tail, v -> v + 1); |
4230 |
+ |
if (addDeadEnds) m.applyToEither(tail, tail, v -> v + 1); |
4231 |
+ |
tail = m.applyToEither(tail, tail, v -> v + 1); |
4232 |
+ |
if (addDeadEnds) m.thenCombine(tail, tail, (v, w) -> v + 1); |
4233 |
+ |
tail = m.thenCombine(tail, tail, (v, w) -> v + 1); |
4234 |
+ |
} |
4235 |
+ |
head.complete(val); |
4236 |
+ |
assertEquals(val + 3 * n, (int) tail.join()); |
4237 |
+ |
}} |
4238 |
+ |
|
4239 |
|
/** |
4240 |
|
* A single CompletableFuture with many dependents. |
4241 |
|
* A demo of scalability - runtime is O(n). |
4242 |
|
*/ |
4243 |
|
public void testManyDependents() throws Throwable { |
4244 |
< |
final int n = 1_000; |
4244 |
> |
final int n = expensiveTests ? 1_000_000 : 10; |
4245 |
|
final CompletableFuture<Void> head = new CompletableFuture<>(); |
4246 |
|
final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null); |
4247 |
|
final AtomicInteger count = new AtomicInteger(0); |
4268 |
|
assertEquals(5 * 3 * n, count.get()); |
4269 |
|
} |
4270 |
|
|
4271 |
+ |
/** ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck */ |
4272 |
+ |
public void testCoCompletionGarbageRetention() throws Throwable { |
4273 |
+ |
final int n = expensiveTests ? 1_000_000 : 10; |
4274 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
4275 |
+ |
CompletableFuture<Integer> f; |
4276 |
+ |
for (int i = 0; i < n; i++) { |
4277 |
+ |
f = new CompletableFuture<>(); |
4278 |
+ |
f.runAfterEither(incomplete, () -> {}); |
4279 |
+ |
f.complete(null); |
4280 |
+ |
|
4281 |
+ |
f = new CompletableFuture<>(); |
4282 |
+ |
f.acceptEither(incomplete, (x) -> {}); |
4283 |
+ |
f.complete(null); |
4284 |
+ |
|
4285 |
+ |
f = new CompletableFuture<>(); |
4286 |
+ |
f.applyToEither(incomplete, (x) -> x); |
4287 |
+ |
f.complete(null); |
4288 |
+ |
|
4289 |
+ |
f = new CompletableFuture<>(); |
4290 |
+ |
CompletableFuture.anyOf(new CompletableFuture<?>[] { f, incomplete }); |
4291 |
+ |
f.complete(null); |
4292 |
+ |
} |
4293 |
+ |
|
4294 |
+ |
for (int i = 0; i < n; i++) { |
4295 |
+ |
f = new CompletableFuture<>(); |
4296 |
+ |
incomplete.runAfterEither(f, () -> {}); |
4297 |
+ |
f.complete(null); |
4298 |
+ |
|
4299 |
+ |
f = new CompletableFuture<>(); |
4300 |
+ |
incomplete.acceptEither(f, (x) -> {}); |
4301 |
+ |
f.complete(null); |
4302 |
+ |
|
4303 |
+ |
f = new CompletableFuture<>(); |
4304 |
+ |
incomplete.applyToEither(f, (x) -> x); |
4305 |
+ |
f.complete(null); |
4306 |
+ |
|
4307 |
+ |
f = new CompletableFuture<>(); |
4308 |
+ |
CompletableFuture.anyOf(new CompletableFuture<?>[] { incomplete, f }); |
4309 |
+ |
f.complete(null); |
4310 |
+ |
} |
4311 |
+ |
} |
4312 |
+ |
|
4313 |
+ |
/** |
4314 |
+ |
* Reproduction recipe for: |
4315 |
+ |
* 8160402: Garbage retention with CompletableFuture.anyOf |
4316 |
+ |
* 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 |
4317 |
+ |
*/ |
4318 |
+ |
public void testAnyOfGarbageRetention() throws Throwable { |
4319 |
+ |
for (Integer v : new Integer[] { 1, null }) |
4320 |
+ |
{ |
4321 |
+ |
final int n = expensiveTests ? 100_000 : 10; |
4322 |
+ |
CompletableFuture<Integer>[] fs |
4323 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture<?>[100]; |
4324 |
+ |
for (int i = 0; i < fs.length; i++) |
4325 |
+ |
fs[i] = new CompletableFuture<>(); |
4326 |
+ |
fs[fs.length - 1].complete(v); |
4327 |
+ |
for (int i = 0; i < n; i++) |
4328 |
+ |
checkCompletedNormally(CompletableFuture.anyOf(fs), v); |
4329 |
+ |
}} |
4330 |
+ |
|
4331 |
+ |
/** |
4332 |
+ |
* Checks for garbage retention with allOf. |
4333 |
+ |
* |
4334 |
+ |
* As of 2016-07, fails with OOME: |
4335 |
+ |
* ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledAllOfGarbageRetention tck |
4336 |
+ |
*/ |
4337 |
+ |
public void testCancelledAllOfGarbageRetention() throws Throwable { |
4338 |
+ |
final int n = expensiveTests ? 100_000 : 10; |
4339 |
+ |
CompletableFuture<Integer>[] fs |
4340 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture<?>[100]; |
4341 |
+ |
for (int i = 0; i < fs.length; i++) |
4342 |
+ |
fs[i] = new CompletableFuture<>(); |
4343 |
+ |
for (int i = 0; i < n; i++) |
4344 |
+ |
assertTrue(CompletableFuture.allOf(fs).cancel(false)); |
4345 |
+ |
} |
4346 |
+ |
|
4347 |
+ |
/** |
4348 |
+ |
* Checks for garbage retention when a dependent future is |
4349 |
+ |
* cancelled and garbage-collected. |
4350 |
+ |
* 8161600: Garbage retention when source CompletableFutures are never completed |
4351 |
+ |
* |
4352 |
+ |
* As of 2016-07, fails with OOME: |
4353 |
+ |
* ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledGarbageRetention tck |
4354 |
+ |
*/ |
4355 |
+ |
public void testCancelledGarbageRetention() throws Throwable { |
4356 |
+ |
final int n = expensiveTests ? 100_000 : 10; |
4357 |
+ |
CompletableFuture<Integer> neverCompleted = new CompletableFuture<>(); |
4358 |
+ |
for (int i = 0; i < n; i++) |
4359 |
+ |
assertTrue(neverCompleted.thenRun(() -> {}).cancel(true)); |
4360 |
+ |
} |
4361 |
+ |
|
4362 |
|
// static <U> U join(CompletionStage<U> stage) { |
4363 |
|
// CompletableFuture<U> f = new CompletableFuture<>(); |
4364 |
|
// stage.whenComplete((v, ex) -> { |