2922 |
|
* when all components complete normally |
2923 |
|
*/ |
2924 |
|
public void testAllOf_normal() throws Exception { |
2925 |
< |
for (int k = 1; k < 20; ++k) { |
2925 |
> |
for (int k = 1; k < 20; k++) { |
2926 |
|
CompletableFuture<Integer>[] fs |
2927 |
|
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
2928 |
< |
for (int i = 0; i < k; ++i) |
2928 |
> |
for (int i = 0; i < k; i++) |
2929 |
|
fs[i] = new CompletableFuture<>(); |
2930 |
|
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
2931 |
< |
for (int i = 0; i < k; ++i) { |
2931 |
> |
for (int i = 0; i < k; i++) { |
2932 |
|
checkIncomplete(f); |
2933 |
|
checkIncomplete(CompletableFuture.allOf(fs)); |
2934 |
|
fs[i].complete(one); |
2939 |
|
} |
2940 |
|
|
2941 |
|
public void testAllOf_backwards() throws Exception { |
2942 |
< |
for (int k = 1; k < 20; ++k) { |
2942 |
> |
for (int k = 1; k < 20; k++) { |
2943 |
|
CompletableFuture<Integer>[] fs |
2944 |
|
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
2945 |
< |
for (int i = 0; i < k; ++i) |
2945 |
> |
for (int i = 0; i < k; i++) |
2946 |
|
fs[i] = new CompletableFuture<>(); |
2947 |
|
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
2948 |
|
for (int i = k - 1; i >= 0; i--) { |
2959 |
|
* anyOf(no component futures) returns an incomplete future |
2960 |
|
*/ |
2961 |
|
public void testAnyOf_empty() throws Exception { |
2962 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
2963 |
+ |
{ |
2964 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(); |
2965 |
|
checkIncomplete(f); |
2966 |
< |
} |
2966 |
> |
|
2967 |
> |
f.complete(v1); |
2968 |
> |
checkCompletedNormally(f, v1); |
2969 |
> |
}} |
2970 |
|
|
2971 |
|
/** |
2972 |
|
* anyOf returns a future completed normally with a value when |
2973 |
|
* a component future does |
2974 |
|
*/ |
2975 |
|
public void testAnyOf_normal() throws Exception { |
2976 |
< |
for (int k = 0; k < 10; ++k) { |
2976 |
> |
for (int k = 0; k < 10; k++) { |
2977 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
2978 |
< |
for (int i = 0; i < k; ++i) |
2978 |
> |
for (int i = 0; i < k; i++) |
2979 |
|
fs[i] = new CompletableFuture<>(); |
2980 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
2981 |
|
checkIncomplete(f); |
2982 |
< |
for (int i = 0; i < k; ++i) { |
2983 |
< |
fs[i].complete(one); |
2984 |
< |
checkCompletedNormally(f, one); |
2985 |
< |
checkCompletedNormally(CompletableFuture.anyOf(fs), one); |
2982 |
> |
for (int i = 0; i < k; i++) { |
2983 |
> |
fs[i].complete(i); |
2984 |
> |
checkCompletedNormally(f, 0); |
2985 |
> |
int x = (int) CompletableFuture.anyOf(fs).join(); |
2986 |
> |
assertTrue(0 <= x && x <= i); |
2987 |
> |
} |
2988 |
> |
} |
2989 |
> |
} |
2990 |
> |
public void testAnyOf_normal_backwards() throws Exception { |
2991 |
> |
for (int k = 0; k < 10; k++) { |
2992 |
> |
CompletableFuture[] fs = new CompletableFuture[k]; |
2993 |
> |
for (int i = 0; i < k; i++) |
2994 |
> |
fs[i] = new CompletableFuture<>(); |
2995 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
2996 |
> |
checkIncomplete(f); |
2997 |
> |
for (int i = k - 1; i >= 0; i--) { |
2998 |
> |
fs[i].complete(i); |
2999 |
> |
checkCompletedNormally(f, k - 1); |
3000 |
> |
int x = (int) CompletableFuture.anyOf(fs).join(); |
3001 |
> |
assertTrue(i <= x && x <= k - 1); |
3002 |
|
} |
3003 |
|
} |
3004 |
|
} |
3007 |
|
* anyOf result completes exceptionally when any component does. |
3008 |
|
*/ |
3009 |
|
public void testAnyOf_exceptional() throws Exception { |
3010 |
< |
for (int k = 0; k < 10; ++k) { |
3010 |
> |
for (int k = 0; k < 10; k++) { |
3011 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
3012 |
< |
for (int i = 0; i < k; ++i) |
3012 |
> |
CFException[] exs = new CFException[k]; |
3013 |
> |
for (int i = 0; i < k; i++) { |
3014 |
|
fs[i] = new CompletableFuture<>(); |
3015 |
+ |
exs[i] = new CFException(); |
3016 |
+ |
} |
3017 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3018 |
|
checkIncomplete(f); |
3019 |
< |
for (int i = 0; i < k; ++i) { |
3020 |
< |
fs[i].completeExceptionally(new CFException()); |
3021 |
< |
checkCompletedWithWrappedCFException(f); |
3019 |
> |
for (int i = 0; i < k; i++) { |
3020 |
> |
fs[i].completeExceptionally(exs[i]); |
3021 |
> |
checkCompletedWithWrappedException(f, exs[0]); |
3022 |
> |
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
3023 |
> |
} |
3024 |
> |
} |
3025 |
> |
} |
3026 |
> |
|
3027 |
> |
public void testAnyOf_exceptional_backwards() throws Exception { |
3028 |
> |
for (int k = 0; k < 10; k++) { |
3029 |
> |
CompletableFuture[] fs = new CompletableFuture[k]; |
3030 |
> |
CFException[] exs = new CFException[k]; |
3031 |
> |
for (int i = 0; i < k; i++) { |
3032 |
> |
fs[i] = new CompletableFuture<>(); |
3033 |
> |
exs[i] = new CFException(); |
3034 |
> |
} |
3035 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3036 |
> |
checkIncomplete(f); |
3037 |
> |
for (int i = k - 1; i >= 0; i--) { |
3038 |
> |
fs[i].completeExceptionally(exs[i]); |
3039 |
> |
checkCompletedWithWrappedException(f, exs[k - 1]); |
3040 |
|
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
3041 |
|
} |
3042 |
|
} |
3160 |
|
assertSame(f, f.toCompletableFuture()); |
3161 |
|
} |
3162 |
|
|
3163 |
< |
// public void testRunAfterEither_resultDeterminedAtTimeOfCreation() { |
3164 |
< |
// for (ExecutionMode m : ExecutionMode.values()) |
3165 |
< |
// for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3166 |
< |
// for (Integer v1 : new Integer[] { 1, null }) |
3167 |
< |
// { |
3168 |
< |
// final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3169 |
< |
// final CompletableFuture<Integer> g = new CompletableFuture<>(); |
3170 |
< |
// final Noop[] rs = new Noop[2]; |
3171 |
< |
// for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
3172 |
< |
// f.complete(v1); |
3173 |
< |
// final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
3174 |
< |
// final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
3175 |
< |
// assertTrue(g.cancel(mayInterruptIfRunning)); |
3176 |
< |
// checkCompletedNormally(h0, null); |
3177 |
< |
// checkCompletedNormally(h1, null); |
3178 |
< |
// for (Noop r : rs) r.assertInvoked(); |
3179 |
< |
// }} |
3163 |
> |
//--- tests of implementation details; not part of official tck --- |
3164 |
> |
|
3165 |
> |
Object resultOf(CompletableFuture<?> f) { |
3166 |
> |
try { |
3167 |
> |
java.lang.reflect.Field resultField |
3168 |
> |
= CompletableFuture.class.getDeclaredField("result"); |
3169 |
> |
resultField.setAccessible(true); |
3170 |
> |
return resultField.get(f); |
3171 |
> |
} catch (Throwable t) { throw new AssertionError(t); } |
3172 |
> |
} |
3173 |
> |
|
3174 |
> |
public void testExceptionPropagationReusesResultObject() { |
3175 |
> |
if (!testImplementationDetails) return; |
3176 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
3177 |
> |
{ |
3178 |
> |
final CFException ex = new CFException(); |
3179 |
> |
final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42); |
3180 |
> |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3181 |
> |
|
3182 |
> |
List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> dependentFactories |
3183 |
> |
= new ArrayList<>(); |
3184 |
> |
|
3185 |
> |
dependentFactories.add((y) -> m.thenRun(y, new Noop(m))); |
3186 |
> |
dependentFactories.add((y) -> m.thenAccept(y, new NoopConsumer(m))); |
3187 |
> |
dependentFactories.add((y) -> m.thenApply(y, new IncFunction(m))); |
3188 |
> |
|
3189 |
> |
dependentFactories.add((y) -> m.runAfterEither(y, incomplete, new Noop(m))); |
3190 |
> |
dependentFactories.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m))); |
3191 |
> |
dependentFactories.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m))); |
3192 |
> |
|
3193 |
> |
dependentFactories.add((y) -> m.runAfterBoth(y, v42, new Noop(m))); |
3194 |
> |
dependentFactories.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
3195 |
> |
dependentFactories.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m))); |
3196 |
> |
|
3197 |
> |
dependentFactories.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {})); |
3198 |
> |
|
3199 |
> |
dependentFactories.add((y) -> m.thenCompose(y, new CompletableFutureInc(m))); |
3200 |
> |
|
3201 |
> |
dependentFactories.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42})); |
3202 |
> |
dependentFactories.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete})); |
3203 |
> |
|
3204 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3205 |
> |
dependentFactory : dependentFactories) { |
3206 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3207 |
> |
f.completeExceptionally(ex); |
3208 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3209 |
> |
checkCompletedWithWrappedException(src, ex); |
3210 |
> |
CompletableFuture<?> dep = dependentFactory.apply(src); |
3211 |
> |
checkCompletedWithWrappedException(dep, ex); |
3212 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3213 |
> |
} |
3214 |
> |
|
3215 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3216 |
> |
dependentFactory : dependentFactories) { |
3217 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3218 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3219 |
> |
CompletableFuture<?> dep = dependentFactory.apply(src); |
3220 |
> |
f.completeExceptionally(ex); |
3221 |
> |
checkCompletedWithWrappedException(src, ex); |
3222 |
> |
checkCompletedWithWrappedException(dep, ex); |
3223 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3224 |
> |
} |
3225 |
> |
|
3226 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3227 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3228 |
> |
dependentFactory : dependentFactories) { |
3229 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3230 |
> |
f.cancel(mayInterruptIfRunning); |
3231 |
> |
checkCancelled(f); |
3232 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3233 |
> |
checkCompletedWithWrappedCancellationException(src); |
3234 |
> |
CompletableFuture<?> dep = dependentFactory.apply(src); |
3235 |
> |
checkCompletedWithWrappedCancellationException(dep); |
3236 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3237 |
> |
} |
3238 |
> |
|
3239 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3240 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3241 |
> |
dependentFactory : dependentFactories) { |
3242 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3243 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3244 |
> |
CompletableFuture<?> dep = dependentFactory.apply(src); |
3245 |
> |
f.cancel(mayInterruptIfRunning); |
3246 |
> |
checkCancelled(f); |
3247 |
> |
checkCompletedWithWrappedCancellationException(src); |
3248 |
> |
checkCompletedWithWrappedCancellationException(dep); |
3249 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3250 |
> |
} |
3251 |
> |
}} |
3252 |
|
|
3253 |
|
} |