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; |
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); |
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 |
< |
|
3438 |
< |
} |
3439 |
< |
} |
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 |
|
|
3449 |
– |
for (Integer v : new Integer[] { 1, null }) { |
3450 |
– |
|
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 |
< |
|
3499 |
< |
} |
3500 |
< |
} |
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 = |
3967 |
< |
new CompletableFuture<Integer>().minimalCompletionStage(); |
3966 |
> |
List<CompletionStage<Integer>> stages = new ArrayList<>(); |
3967 |
> |
stages.add(new CompletableFuture<Integer>().minimalCompletionStage()); |
3968 |
> |
stages.add(CompletableFuture.completedStage(1)); |
3969 |
> |
stages.add(CompletableFuture.failedStage(new CFException())); |
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); |
3982 |
< |
} |
3983 |
< |
catch (java.lang.reflect.InvocationTargetException expected) { |
3984 |
< |
if (! (expected.getCause() instanceof UnsupportedOperationException)) { |
3985 |
> |
for (CompletionStage<Integer> stage : stages) { |
3986 |
> |
try { |
3987 |
> |
method.invoke(stage, args); |
3988 |
|
bugs.add(method); |
3986 |
– |
// 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 |
|
} |
3989 |
– |
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 |
< |
/** a66 -Dvmoptions=-Xmx8m -Djsr166.tckTestClass=CompletableFutureTest tck */ |
4203 |
< |
public void testCoCompletionGarbage() throws Throwable { |
4204 |
< |
// final int n = 3_000_000; |
4176 |
< |
final int n = 100; |
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++) { |
4241 |
|
} |
4242 |
|
} |
4243 |
|
|
4244 |
+ |
/** |
4245 |
+ |
* Reproduction recipe for: |
4246 |
+ |
* 8160402: Garbage retention with CompletableFuture.anyOf |
4247 |
+ |
* 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 |
4248 |
+ |
*/ |
4249 |
+ |
public void testAnyOfGarbageRetention() throws Throwable { |
4250 |
+ |
for (Integer v : new Integer[] { 1, null }) |
4251 |
+ |
{ |
4252 |
+ |
final int n = expensiveTests ? 100_000 : 10; |
4253 |
+ |
CompletableFuture<Integer>[] fs |
4254 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture<?>[100]; |
4255 |
+ |
for (int i = 0; i < fs.length; i++) |
4256 |
+ |
fs[i] = new CompletableFuture<>(); |
4257 |
+ |
fs[fs.length - 1].complete(v); |
4258 |
+ |
for (int i = 0; i < n; i++) |
4259 |
+ |
checkCompletedNormally(CompletableFuture.anyOf(fs), v); |
4260 |
+ |
}} |
4261 |
+ |
|
4262 |
+ |
/** |
4263 |
+ |
* Checks for garbage retention with allOf. |
4264 |
+ |
* |
4265 |
+ |
* As of 2016-07, fails with OOME: |
4266 |
+ |
* ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledAllOfGarbageRetention tck |
4267 |
+ |
*/ |
4268 |
+ |
public void testCancelledAllOfGarbageRetention() throws Throwable { |
4269 |
+ |
final int n = expensiveTests ? 100_000 : 10; |
4270 |
+ |
CompletableFuture<Integer>[] fs |
4271 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture<?>[100]; |
4272 |
+ |
for (int i = 0; i < fs.length; i++) |
4273 |
+ |
fs[i] = new CompletableFuture<>(); |
4274 |
+ |
for (int i = 0; i < n; i++) |
4275 |
+ |
assertTrue(CompletableFuture.allOf(fs).cancel(false)); |
4276 |
+ |
} |
4277 |
+ |
|
4278 |
+ |
/** |
4279 |
+ |
* Checks for garbage retention when a dependent future is |
4280 |
+ |
* cancelled and garbage-collected. |
4281 |
+ |
* 8161600: Garbage retention when source CompletableFutures are never completed |
4282 |
+ |
* |
4283 |
+ |
* As of 2016-07, fails with OOME: |
4284 |
+ |
* ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledGarbageRetention tck |
4285 |
+ |
*/ |
4286 |
+ |
public void testCancelledGarbageRetention() throws Throwable { |
4287 |
+ |
final int n = expensiveTests ? 100_000 : 10; |
4288 |
+ |
CompletableFuture<Integer> neverCompleted = new CompletableFuture<>(); |
4289 |
+ |
for (int i = 0; i < n; i++) |
4290 |
+ |
assertTrue(neverCompleted.thenRun(() -> {}).cancel(true)); |
4291 |
+ |
} |
4292 |
+ |
|
4293 |
|
// static <U> U join(CompletionStage<U> stage) { |
4294 |
|
// CompletableFuture<U> f = new CompletableFuture<>(); |
4295 |
|
// stage.whenComplete((v, ex) -> { |