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; |
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) { |
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; |
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); |
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), |
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); |
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)); |
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 |
|
|
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 |
|
|
3497 |
|
checkCompletedNormally(future, null); |
3498 |
|
|
3499 |
|
assertEquals(0, e.count.get()); |
3500 |
< |
|
3499 |
< |
} |
3500 |
< |
} |
3500 |
> |
}} |
3501 |
|
|
3502 |
|
/** |
3503 |
|
* toCompletableFuture returns this CompletableFuture. |
3577 |
|
* copy returns a CompletableFuture that is completed normally, |
3578 |
|
* with the same value, when source is. |
3579 |
|
*/ |
3580 |
< |
public void testCopy() { |
3580 |
> |
public void testCopy_normalCompletion() { |
3581 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
3582 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3583 |
> |
{ |
3584 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3585 |
+ |
if (!createIncomplete) assertTrue(f.complete(v1)); |
3586 |
|
CompletableFuture<Integer> g = f.copy(); |
3587 |
< |
checkIncomplete(f); |
3588 |
< |
checkIncomplete(g); |
3589 |
< |
f.complete(1); |
3590 |
< |
checkCompletedNormally(f, 1); |
3591 |
< |
checkCompletedNormally(g, 1); |
3592 |
< |
} |
3587 |
> |
if (createIncomplete) { |
3588 |
> |
checkIncomplete(f); |
3589 |
> |
checkIncomplete(g); |
3590 |
> |
assertTrue(f.complete(v1)); |
3591 |
> |
} |
3592 |
> |
checkCompletedNormally(f, v1); |
3593 |
> |
checkCompletedNormally(g, v1); |
3594 |
> |
}} |
3595 |
|
|
3596 |
|
/** |
3597 |
|
* copy returns a CompletableFuture that is completed exceptionally |
3598 |
|
* when source is. |
3599 |
|
*/ |
3600 |
< |
public void testCopy2() { |
3600 |
> |
public void testCopy_exceptionalCompletion() { |
3601 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
3602 |
> |
{ |
3603 |
> |
CFException ex = new CFException(); |
3604 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3605 |
+ |
if (!createIncomplete) f.completeExceptionally(ex); |
3606 |
|
CompletableFuture<Integer> g = f.copy(); |
3607 |
< |
checkIncomplete(f); |
3608 |
< |
checkIncomplete(g); |
3609 |
< |
CFException ex = new CFException(); |
3610 |
< |
f.completeExceptionally(ex); |
3607 |
> |
if (createIncomplete) { |
3608 |
> |
checkIncomplete(f); |
3609 |
> |
checkIncomplete(g); |
3610 |
> |
f.completeExceptionally(ex); |
3611 |
> |
} |
3612 |
|
checkCompletedExceptionally(f, ex); |
3613 |
|
checkCompletedWithWrappedException(g, ex); |
3614 |
+ |
}} |
3615 |
+ |
|
3616 |
+ |
/** |
3617 |
+ |
* Completion of a copy does not complete its source. |
3618 |
+ |
*/ |
3619 |
+ |
public void testCopy_oneWayPropagation() { |
3620 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3621 |
+ |
assertTrue(f.copy().complete(1)); |
3622 |
+ |
assertTrue(f.copy().complete(null)); |
3623 |
+ |
assertTrue(f.copy().cancel(true)); |
3624 |
+ |
assertTrue(f.copy().cancel(false)); |
3625 |
+ |
assertTrue(f.copy().completeExceptionally(new CFException())); |
3626 |
+ |
checkIncomplete(f); |
3627 |
|
} |
3628 |
|
|
3629 |
|
/** |
3875 |
|
final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42); |
3876 |
|
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3877 |
|
|
3878 |
+ |
final Runnable noopRunnable = new Noop(m); |
3879 |
+ |
final Consumer<Integer> noopConsumer = new NoopConsumer(m); |
3880 |
+ |
final Function<Integer, Integer> incFunction = new IncFunction(m); |
3881 |
+ |
|
3882 |
|
List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs |
3883 |
|
= new ArrayList<>(); |
3884 |
|
|
3885 |
< |
funs.add((y) -> m.thenRun(y, new Noop(m))); |
3886 |
< |
funs.add((y) -> m.thenAccept(y, new NoopConsumer(m))); |
3887 |
< |
funs.add((y) -> m.thenApply(y, new IncFunction(m))); |
3888 |
< |
|
3889 |
< |
funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m))); |
3890 |
< |
funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m))); |
3891 |
< |
funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m))); |
3892 |
< |
|
3893 |
< |
funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m))); |
3894 |
< |
funs.add((y) -> m.runAfterBoth(v42, y, new Noop(m))); |
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(new CompletableFuture<?>[] {y})); |
3905 |
< |
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42})); |
3906 |
< |
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {v42, y})); |
3907 |
< |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y})); |
3908 |
< |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete})); |
3909 |
< |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {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) { |
3913 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3914 |
|
f.completeExceptionally(ex); |
3915 |
< |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3915 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3916 |
|
checkCompletedWithWrappedException(src, ex); |
3917 |
|
CompletableFuture<?> dep = fun.apply(src); |
3918 |
|
checkCompletedWithWrappedException(dep, ex); |
3922 |
|
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3923 |
|
fun : funs) { |
3924 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3925 |
< |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3925 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3926 |
|
CompletableFuture<?> dep = fun.apply(src); |
3927 |
|
f.completeExceptionally(ex); |
3928 |
|
checkCompletedWithWrappedException(src, ex); |
3936 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3937 |
|
f.cancel(mayInterruptIfRunning); |
3938 |
|
checkCancelled(f); |
3939 |
< |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3939 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3940 |
|
checkCompletedWithWrappedCancellationException(src); |
3941 |
|
CompletableFuture<?> dep = fun.apply(src); |
3942 |
|
checkCompletedWithWrappedCancellationException(dep); |
3947 |
|
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3948 |
|
fun : funs) { |
3949 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3950 |
< |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3950 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3951 |
|
CompletableFuture<?> dep = fun.apply(src); |
3952 |
|
f.cancel(mayInterruptIfRunning); |
3953 |
|
checkCancelled(f); |
3958 |
|
}} |
3959 |
|
|
3960 |
|
/** |
3961 |
< |
* Minimal completion stages throw UOE for all non-CompletionStage methods |
3961 |
> |
* Minimal completion stages throw UOE for most non-CompletionStage methods |
3962 |
|
*/ |
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 |
< |
CompletionStage<Integer> minimalStage = |
3990 |
> |
List<CompletionStage<Integer>> stages = new ArrayList<>(); |
3991 |
> |
CompletionStage<Integer> min = |
3992 |
|
new CompletableFuture<Integer>().minimalCompletionStage(); |
3993 |
+ |
stages.add(min); |
3994 |
+ |
stages.add(min.thenApply(x -> x)); |
3995 |
+ |
stages.add(CompletableFuture.completedStage(1)); |
3996 |
+ |
stages.add(CompletableFuture.failedStage(new CFException())); |
3997 |
|
|
3998 |
|
List<Method> bugs = new ArrayList<>(); |
3999 |
|
for (Method method : allMethods) { |
4009 |
|
else if (parameterTypes[i] == long.class) |
4010 |
|
args[i] = 0L; |
4011 |
|
} |
4012 |
< |
try { |
4013 |
< |
method.invoke(minimalStage, args); |
4014 |
< |
bugs.add(method); |
3982 |
< |
} |
3983 |
< |
catch (java.lang.reflect.InvocationTargetException expected) { |
3984 |
< |
if (! (expected.getCause() instanceof UnsupportedOperationException)) { |
4012 |
> |
for (CompletionStage<Integer> stage : stages) { |
4013 |
> |
try { |
4014 |
> |
method.invoke(stage, args); |
4015 |
|
bugs.add(method); |
3986 |
– |
// expected.getCause().printStackTrace(); |
4016 |
|
} |
4017 |
+ |
catch (java.lang.reflect.InvocationTargetException expected) { |
4018 |
+ |
if (! (expected.getCause() instanceof UnsupportedOperationException)) { |
4019 |
+ |
bugs.add(method); |
4020 |
+ |
// expected.getCause().printStackTrace(); |
4021 |
+ |
} |
4022 |
+ |
} |
4023 |
+ |
catch (ReflectiveOperationException bad) { throw new Error(bad); } |
4024 |
|
} |
3989 |
– |
catch (ReflectiveOperationException bad) { throw new Error(bad); } |
4025 |
|
} |
4026 |
|
if (!bugs.isEmpty()) |
4027 |
< |
throw new Error("Methods did not throw UOE: " + bugs.toString()); |
4027 |
> |
throw new Error("Methods did not throw UOE: " + bugs); |
4028 |
> |
} |
4029 |
> |
|
4030 |
> |
/** |
4031 |
> |
* minimalStage.toCompletableFuture() returns a CompletableFuture that |
4032 |
> |
* is completed normally, with the same value, when source is. |
4033 |
> |
*/ |
4034 |
> |
public void testMinimalCompletionStage_toCompletableFuture_normalCompletion() { |
4035 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
4036 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
4037 |
> |
{ |
4038 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4039 |
> |
CompletionStage<Integer> minimal = f.minimalCompletionStage(); |
4040 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
4041 |
> |
CompletableFuture<Integer> g = minimal.toCompletableFuture(); |
4042 |
> |
if (createIncomplete) { |
4043 |
> |
checkIncomplete(f); |
4044 |
> |
checkIncomplete(g); |
4045 |
> |
assertTrue(f.complete(v1)); |
4046 |
> |
} |
4047 |
> |
checkCompletedNormally(f, v1); |
4048 |
> |
checkCompletedNormally(g, v1); |
4049 |
> |
}} |
4050 |
> |
|
4051 |
> |
/** |
4052 |
> |
* minimalStage.toCompletableFuture() returns a CompletableFuture that |
4053 |
> |
* is completed exceptionally when source is. |
4054 |
> |
*/ |
4055 |
> |
public void testMinimalCompletionStage_toCompletableFuture_exceptionalCompletion() { |
4056 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
4057 |
> |
{ |
4058 |
> |
CFException ex = new CFException(); |
4059 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4060 |
> |
CompletionStage<Integer> minimal = f.minimalCompletionStage(); |
4061 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
4062 |
> |
CompletableFuture<Integer> g = minimal.toCompletableFuture(); |
4063 |
> |
if (createIncomplete) { |
4064 |
> |
checkIncomplete(f); |
4065 |
> |
checkIncomplete(g); |
4066 |
> |
f.completeExceptionally(ex); |
4067 |
> |
} |
4068 |
> |
checkCompletedExceptionally(f, ex); |
4069 |
> |
checkCompletedWithWrappedException(g, ex); |
4070 |
> |
}} |
4071 |
> |
|
4072 |
> |
/** |
4073 |
> |
* minimalStage.toCompletableFuture() gives mutable CompletableFuture |
4074 |
> |
*/ |
4075 |
> |
public void testMinimalCompletionStage_toCompletableFuture_mutable() { |
4076 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
4077 |
> |
{ |
4078 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4079 |
> |
CompletionStage minimal = f.minimalCompletionStage(); |
4080 |
> |
CompletableFuture<Integer> g = minimal.toCompletableFuture(); |
4081 |
> |
assertTrue(g.complete(v1)); |
4082 |
> |
checkCompletedNormally(g, v1); |
4083 |
> |
checkIncomplete(f); |
4084 |
> |
checkIncomplete(minimal.toCompletableFuture()); |
4085 |
> |
}} |
4086 |
> |
|
4087 |
> |
/** |
4088 |
> |
* minimalStage.toCompletableFuture().join() awaits completion |
4089 |
> |
*/ |
4090 |
> |
public void testMinimalCompletionStage_toCompletableFuture_join() throws Exception { |
4091 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
4092 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
4093 |
> |
{ |
4094 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4095 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
4096 |
> |
CompletionStage<Integer> minimal = f.minimalCompletionStage(); |
4097 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
4098 |
> |
assertEquals(v1, minimal.toCompletableFuture().join()); |
4099 |
> |
assertEquals(v1, minimal.toCompletableFuture().get()); |
4100 |
> |
checkCompletedNormally(minimal.toCompletableFuture(), v1); |
4101 |
> |
}} |
4102 |
> |
|
4103 |
> |
/** |
4104 |
> |
* Completion of a toCompletableFuture copy of a minimal stage |
4105 |
> |
* does not complete its source. |
4106 |
> |
*/ |
4107 |
> |
public void testMinimalCompletionStage_toCompletableFuture_oneWayPropagation() { |
4108 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4109 |
> |
CompletionStage<Integer> g = f.minimalCompletionStage(); |
4110 |
> |
assertTrue(g.toCompletableFuture().complete(1)); |
4111 |
> |
assertTrue(g.toCompletableFuture().complete(null)); |
4112 |
> |
assertTrue(g.toCompletableFuture().cancel(true)); |
4113 |
> |
assertTrue(g.toCompletableFuture().cancel(false)); |
4114 |
> |
assertTrue(g.toCompletableFuture().completeExceptionally(new CFException())); |
4115 |
> |
checkIncomplete(g.toCompletableFuture()); |
4116 |
> |
f.complete(1); |
4117 |
> |
checkCompletedNormally(g.toCompletableFuture(), 1); |
4118 |
|
} |
4119 |
|
|
4120 |
+ |
/** Demo utility method for external reliable toCompletableFuture */ |
4121 |
+ |
static <T> CompletableFuture<T> toCompletableFuture(CompletionStage<T> stage) { |
4122 |
+ |
CompletableFuture<T> f = new CompletableFuture<>(); |
4123 |
+ |
stage.handle((T t, Throwable ex) -> { |
4124 |
+ |
if (ex != null) f.completeExceptionally(ex); |
4125 |
+ |
else f.complete(t); |
4126 |
+ |
return null; |
4127 |
+ |
}); |
4128 |
+ |
return f; |
4129 |
+ |
} |
4130 |
+ |
|
4131 |
+ |
/** Demo utility method to join a CompletionStage */ |
4132 |
+ |
static <T> T join(CompletionStage<T> stage) { |
4133 |
+ |
return toCompletableFuture(stage).join(); |
4134 |
+ |
} |
4135 |
+ |
|
4136 |
+ |
/** |
4137 |
+ |
* Joining a minimal stage "by hand" works |
4138 |
+ |
*/ |
4139 |
+ |
public void testMinimalCompletionStage_join_by_hand() { |
4140 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
4141 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
4142 |
+ |
{ |
4143 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
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)); |
4148 |
+ |
if (createIncomplete) assertTrue(f.complete(v1)); |
4149 |
+ |
g.join(); |
4150 |
+ |
checkCompletedNormally(g, v1); |
4151 |
+ |
checkCompletedNormally(f, v1); |
4152 |
+ |
assertEquals(v1, join(minimal)); |
4153 |
+ |
}} |
4154 |
+ |
|
4155 |
|
static class Monad { |
4156 |
|
static class ZeroException extends RuntimeException { |
4157 |
|
public ZeroException() { super("monadic zero"); } |
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), |
4298 |
|
Monad.plus(godot, Monad.unit(5L))); |
4299 |
|
} |
4300 |
|
|
4301 |
+ |
/** Test long recursive chains of CompletableFutures with cascading completions */ |
4302 |
+ |
public void testRecursiveChains() throws Throwable { |
4303 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
4304 |
+ |
for (boolean addDeadEnds : new boolean[] { true, false }) |
4305 |
+ |
{ |
4306 |
+ |
final int val = 42; |
4307 |
+ |
final int n = expensiveTests ? 1_000 : 2; |
4308 |
+ |
CompletableFuture<Integer> head = new CompletableFuture<>(); |
4309 |
+ |
CompletableFuture<Integer> tail = head; |
4310 |
+ |
for (int i = 0; i < n; i++) { |
4311 |
+ |
if (addDeadEnds) m.thenApply(tail, v -> v + 1); |
4312 |
+ |
tail = m.thenApply(tail, v -> v + 1); |
4313 |
+ |
if (addDeadEnds) m.applyToEither(tail, tail, v -> v + 1); |
4314 |
+ |
tail = m.applyToEither(tail, tail, v -> v + 1); |
4315 |
+ |
if (addDeadEnds) m.thenCombine(tail, tail, (v, w) -> v + 1); |
4316 |
+ |
tail = m.thenCombine(tail, tail, (v, w) -> v + 1); |
4317 |
+ |
} |
4318 |
+ |
head.complete(val); |
4319 |
+ |
assertEquals(val + 3 * n, (int) tail.join()); |
4320 |
+ |
}} |
4321 |
+ |
|
4322 |
|
/** |
4323 |
|
* A single CompletableFuture with many dependents. |
4324 |
|
* A demo of scalability - runtime is O(n). |
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()); |
4352 |
|
} |
4353 |
|
|
4354 |
< |
/** ant -Dvmoptions=-Xmx8m -Djsr166.tckTestClass=CompletableFutureTest tck */ |
4355 |
< |
public void testCoCompletionGarbage() throws Throwable { |
4354 |
> |
/** ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck */ |
4355 |
> |
public void testCoCompletionGarbageRetention() throws Throwable { |
4356 |
|
final int n = expensiveTests ? 1_000_000 : 10; |
4357 |
|
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
4358 |
|
CompletableFuture<Integer> f; |
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<>(); |
4393 |
|
} |
4394 |
|
} |
4395 |
|
|
4396 |
< |
/* |
4397 |
< |
* Tests below currently fail in stress mode due to memory retention. |
4398 |
< |
* ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck |
4396 |
> |
/** |
4397 |
> |
* Reproduction recipe for: |
4398 |
> |
* 8160402: Garbage retention with CompletableFuture.anyOf |
4399 |
> |
* 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 |
4400 |
|
*/ |
4219 |
– |
|
4220 |
– |
/** Checks for garbage retention with anyOf. */ |
4401 |
|
public void testAnyOfGarbageRetention() throws Throwable { |
4402 |
|
for (Integer v : new Integer[] { 1, null }) |
4403 |
|
{ |
4411 |
|
checkCompletedNormally(CompletableFuture.anyOf(fs), v); |
4412 |
|
}} |
4413 |
|
|
4414 |
< |
/** Checks for garbage retention with allOf. */ |
4414 |
> |
/** |
4415 |
> |
* Checks for garbage retention with allOf. |
4416 |
> |
* |
4417 |
> |
* As of 2016-07, fails with OOME: |
4418 |
> |
* ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledAllOfGarbageRetention tck |
4419 |
> |
*/ |
4420 |
|
public void testCancelledAllOfGarbageRetention() throws Throwable { |
4421 |
|
final int n = expensiveTests ? 100_000 : 10; |
4422 |
|
CompletableFuture<Integer>[] fs |
4427 |
|
assertTrue(CompletableFuture.allOf(fs).cancel(false)); |
4428 |
|
} |
4429 |
|
|
4430 |
+ |
/** |
4431 |
+ |
* Checks for garbage retention when a dependent future is |
4432 |
+ |
* cancelled and garbage-collected. |
4433 |
+ |
* 8161600: Garbage retention when source CompletableFutures are never completed |
4434 |
+ |
* |
4435 |
+ |
* As of 2016-07, fails with OOME: |
4436 |
+ |
* ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledGarbageRetention tck |
4437 |
+ |
*/ |
4438 |
+ |
public void testCancelledGarbageRetention() throws Throwable { |
4439 |
+ |
final int n = expensiveTests ? 100_000 : 10; |
4440 |
+ |
CompletableFuture<Integer> neverCompleted = new CompletableFuture<>(); |
4441 |
+ |
for (int i = 0; i < n; i++) |
4442 |
+ |
assertTrue(neverCompleted.thenRun(() -> {}).cancel(true)); |
4443 |
+ |
} |
4444 |
+ |
|
4445 |
+ |
/** |
4446 |
+ |
* Checks for garbage retention when MinimalStage.toCompletableFuture() |
4447 |
+ |
* is invoked many times. |
4448 |
+ |
* 8161600: Garbage retention when source CompletableFutures are never completed |
4449 |
+ |
* |
4450 |
+ |
* As of 2016-07, fails with OOME: |
4451 |
+ |
* ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testToCompletableFutureGarbageRetention tck |
4452 |
+ |
*/ |
4453 |
+ |
public void testToCompletableFutureGarbageRetention() throws Throwable { |
4454 |
+ |
final int n = expensiveTests ? 900_000 : 10; |
4455 |
+ |
CompletableFuture<Integer> neverCompleted = new CompletableFuture<>(); |
4456 |
+ |
CompletionStage minimal = neverCompleted.minimalCompletionStage(); |
4457 |
+ |
for (int i = 0; i < n; i++) |
4458 |
+ |
assertTrue(minimal.toCompletableFuture().cancel(true)); |
4459 |
+ |
} |
4460 |
+ |
|
4461 |
|
// static <U> U join(CompletionStage<U> stage) { |
4462 |
|
// CompletableFuture<U> f = new CompletableFuture<>(); |
4463 |
|
// stage.whenComplete((v, ex) -> { |