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() 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"); } |