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 |
|
/** |
3988 |
|
.collect(Collectors.toList()); |
3989 |
|
|
3990 |
|
List<CompletionStage<Integer>> stages = new ArrayList<>(); |
3991 |
< |
stages.add(new CompletableFuture<Integer>().minimalCompletionStage()); |
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 |
|
|
4027 |
|
throw new Error("Methods did not throw UOE: " + bugs); |
4028 |
|
} |
4029 |
|
|
4030 |
+ |
/** |
4031 |
+ |
* minimalStage.toCompletableFuture() gives mutable CompletableFuture |
4032 |
+ |
*/ |
4033 |
+ |
public void testMinimalCompletionStage_toCompletableFuture_mutable() { |
4034 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
4035 |
+ |
{ |
4036 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4037 |
+ |
CompletionStage minimal = f.minimalCompletionStage(); |
4038 |
+ |
CompletableFuture<Integer> g = minimal.toCompletableFuture(); |
4039 |
+ |
g.complete(v1); |
4040 |
+ |
checkCompletedNormally(g, v1); |
4041 |
+ |
checkIncomplete(f); |
4042 |
+ |
checkIncomplete(minimal.toCompletableFuture()); |
4043 |
+ |
}} |
4044 |
+ |
|
4045 |
+ |
/** |
4046 |
+ |
* minimalStage.toCompletableFuture().join() awaits completion |
4047 |
+ |
*/ |
4048 |
+ |
public void testMinimalCompletionStage_toCompletableFuture_join() throws Exception { |
4049 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
4050 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
4051 |
+ |
{ |
4052 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4053 |
+ |
if (!createIncomplete) assertTrue(f.complete(v1)); |
4054 |
+ |
CompletionStage<Integer> minimal = f.minimalCompletionStage(); |
4055 |
+ |
if (createIncomplete) assertTrue(f.complete(v1)); |
4056 |
+ |
assertEquals(v1, minimal.toCompletableFuture().join()); |
4057 |
+ |
assertEquals(v1, minimal.toCompletableFuture().get()); |
4058 |
+ |
checkCompletedNormally(minimal.toCompletableFuture(), v1); |
4059 |
+ |
}} |
4060 |
+ |
|
4061 |
+ |
/** Demo utility method for external reliable toCompletableFuture */ |
4062 |
+ |
static <T> CompletableFuture<T> toCompletableFuture(CompletionStage<T> stage) { |
4063 |
+ |
CompletableFuture<T> f = new CompletableFuture<>(); |
4064 |
+ |
stage.handle((T t, Throwable ex) -> { |
4065 |
+ |
if (ex != null) f.completeExceptionally(ex); |
4066 |
+ |
else f.complete(t); |
4067 |
+ |
return null; |
4068 |
+ |
}); |
4069 |
+ |
return f; |
4070 |
+ |
} |
4071 |
+ |
|
4072 |
+ |
/** Demo utility method to join a CompletionStage */ |
4073 |
+ |
static <T> T join(CompletionStage<T> stage) { |
4074 |
+ |
return toCompletableFuture(stage).join(); |
4075 |
+ |
} |
4076 |
+ |
|
4077 |
+ |
/** |
4078 |
+ |
* Joining a minimal stage "by hand" works |
4079 |
+ |
*/ |
4080 |
+ |
public void testMinimalCompletionStage_join_by_hand() { |
4081 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
4082 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
4083 |
+ |
{ |
4084 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4085 |
+ |
CompletionStage<Integer> minimal = f.minimalCompletionStage(); |
4086 |
+ |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
4087 |
+ |
if (!createIncomplete) assertTrue(f.complete(v1)); |
4088 |
+ |
minimal.thenAccept((x) -> g.complete(x)); |
4089 |
+ |
if (createIncomplete) assertTrue(f.complete(v1)); |
4090 |
+ |
g.join(); |
4091 |
+ |
checkCompletedNormally(g, v1); |
4092 |
+ |
checkCompletedNormally(f, v1); |
4093 |
+ |
assertEquals(v1, join(minimal)); |
4094 |
+ |
}} |
4095 |
+ |
|
4096 |
|
static class Monad { |
4097 |
|
static class ZeroException extends RuntimeException { |
4098 |
|
public ZeroException() { super("monadic zero"); } |