68 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
69 |
|
assertTrue(f.isDone()); |
70 |
|
assertFalse(f.isCancelled()); |
71 |
+ |
assertFalse(f.isCompletedExceptionally()); |
72 |
|
assertTrue(f.toString().contains("[Completed normally]")); |
73 |
|
} |
74 |
|
|
122 |
|
} catch (CancellationException success) { |
123 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
124 |
|
assertTrue(f.isDone()); |
125 |
+ |
assertTrue(f.isCompletedExceptionally()); |
126 |
|
assertTrue(f.isCancelled()); |
127 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
128 |
|
} |
154 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
155 |
|
assertTrue(f.isDone()); |
156 |
|
assertFalse(f.isCancelled()); |
157 |
+ |
assertTrue(f.isCompletedExceptionally()); |
158 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
159 |
|
} |
160 |
|
|
259 |
|
assertEquals(g.getNumberOfDependents(), 0); |
260 |
|
} |
261 |
|
|
259 |
– |
|
262 |
|
/** |
263 |
|
* toString indicates current completion state |
264 |
|
*/ |
373 |
|
} |
374 |
|
} |
375 |
|
|
374 |
– |
|
376 |
|
/** |
377 |
|
* exceptionally action completes with function value on source |
378 |
|
* exception; otherwise with source value |
661 |
|
checkCompletedWithWrappedCancellationException(g); |
662 |
|
} |
663 |
|
|
663 |
– |
|
664 |
|
/** |
665 |
|
* thenCombine result completes normally after normal completion |
666 |
|
* of sources |
1168 |
|
checkCompletedWithWrappedCancellationException(g); |
1169 |
|
} |
1170 |
|
|
1171 |
– |
|
1171 |
|
/** |
1172 |
|
* runAfterEither result completes normally after normal completion |
1173 |
|
* of either source |
1316 |
|
checkCompletedWithWrappedCancellationException(g); |
1317 |
|
} |
1318 |
|
|
1320 |
– |
|
1319 |
|
// asyncs |
1320 |
|
|
1321 |
|
/** |
2083 |
|
checkCompletedWithWrappedCancellationException(g); |
2084 |
|
} |
2085 |
|
|
2088 |
– |
|
2086 |
|
// async with explicit executors |
2087 |
|
|
2088 |
|
/** |
2853 |
|
* with the value null |
2854 |
|
*/ |
2855 |
|
public void testAllOf_empty() throws Exception { |
2856 |
< |
CompletableFuture<?> f = CompletableFuture.allOf(); |
2856 |
> |
CompletableFuture<Void> f = CompletableFuture.allOf(); |
2857 |
|
checkCompletedNormally(f, null); |
2858 |
|
} |
2859 |
|
|
2860 |
|
/** |
2861 |
< |
* allOf returns a future completed when all components complete |
2861 |
> |
* allOf returns a future completed normally with the value null |
2862 |
> |
* when all components complete normally |
2863 |
|
*/ |
2864 |
< |
public void testAllOf() throws Exception { |
2864 |
> |
public void testAllOf_normal() throws Exception { |
2865 |
|
for (int k = 1; k < 20; ++k) { |
2866 |
|
CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
2867 |
|
for (int i = 0; i < k; ++i) |
2869 |
|
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
2870 |
|
for (int i = 0; i < k; ++i) { |
2871 |
|
checkIncomplete(f); |
2872 |
+ |
checkIncomplete(CompletableFuture.allOf(fs)); |
2873 |
|
fs[i].complete(one); |
2874 |
|
} |
2875 |
|
checkCompletedNormally(f, null); |
2876 |
+ |
checkCompletedNormally(CompletableFuture.allOf(fs), null); |
2877 |
|
} |
2878 |
|
} |
2879 |
|
|
2881 |
|
* anyOf(no component futures) returns an incomplete future |
2882 |
|
*/ |
2883 |
|
public void testAnyOf_empty() throws Exception { |
2884 |
< |
CompletableFuture<?> f = CompletableFuture.anyOf(); |
2884 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(); |
2885 |
|
checkIncomplete(f); |
2886 |
|
} |
2887 |
|
|
2888 |
|
/** |
2889 |
< |
* anyOf returns a future completed when any components complete |
2889 |
> |
* anyOf returns a future completed normally with a value when |
2890 |
> |
* a component future does |
2891 |
|
*/ |
2892 |
< |
public void testAnyOf() throws Exception { |
2893 |
< |
for (int k = 1; k < 20; ++k) { |
2892 |
> |
public void testAnyOf_normal() throws Exception { |
2893 |
> |
for (int k = 0; k < 10; ++k) { |
2894 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
2895 |
|
for (int i = 0; i < k; ++i) |
2896 |
|
fs[i] = new CompletableFuture<>(); |
2899 |
|
for (int i = 0; i < k; ++i) { |
2900 |
|
fs[i].complete(one); |
2901 |
|
checkCompletedNormally(f, one); |
2902 |
+ |
checkCompletedNormally(CompletableFuture.anyOf(fs), one); |
2903 |
+ |
} |
2904 |
+ |
} |
2905 |
+ |
} |
2906 |
+ |
|
2907 |
+ |
/** |
2908 |
+ |
* anyOf result completes exceptionally when any component does. |
2909 |
+ |
*/ |
2910 |
+ |
public void testAnyOf_exceptional() throws Exception { |
2911 |
+ |
for (int k = 0; k < 10; ++k) { |
2912 |
+ |
CompletableFuture[] fs = new CompletableFuture[k]; |
2913 |
+ |
for (int i = 0; i < k; ++i) |
2914 |
+ |
fs[i] = new CompletableFuture<>(); |
2915 |
+ |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
2916 |
+ |
checkIncomplete(f); |
2917 |
+ |
for (int i = 0; i < k; ++i) { |
2918 |
+ |
fs[i].completeExceptionally(new CFException()); |
2919 |
+ |
checkCompletedWithWrappedCFException(f); |
2920 |
+ |
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
2921 |
|
} |
2922 |
|
} |
2923 |
|
} |
3030 |
|
assertEquals(0, exec.count.get()); |
3031 |
|
} |
3032 |
|
|
3033 |
+ |
/** |
3034 |
+ |
* toCompletableFuture returns this CompletableFuture. |
3035 |
+ |
*/ |
3036 |
+ |
public void testToCompletableFuture() { |
3037 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3038 |
+ |
assertSame(f, f.toCompletableFuture()); |
3039 |
+ |
} |
3040 |
+ |
|
3041 |
+ |
/** |
3042 |
+ |
* whenComplete action executes on normal completion, propagating |
3043 |
+ |
* source result. |
3044 |
+ |
*/ |
3045 |
+ |
public void testWhenComplete1() { |
3046 |
+ |
final AtomicInteger a = new AtomicInteger(); |
3047 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3048 |
+ |
CompletableFuture<Integer> g = |
3049 |
+ |
f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement()); |
3050 |
+ |
f.complete(three); |
3051 |
+ |
checkCompletedNormally(f, three); |
3052 |
+ |
checkCompletedNormally(g, three); |
3053 |
+ |
assertEquals(a.get(), 1); |
3054 |
+ |
} |
3055 |
+ |
|
3056 |
+ |
/** |
3057 |
+ |
* whenComplete action executes on exceptional completion, propagating |
3058 |
+ |
* source result. |
3059 |
+ |
*/ |
3060 |
+ |
public void testWhenComplete2() { |
3061 |
+ |
final AtomicInteger a = new AtomicInteger(); |
3062 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3063 |
+ |
CompletableFuture<Integer> g = |
3064 |
+ |
f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement()); |
3065 |
+ |
f.completeExceptionally(new CFException()); |
3066 |
+ |
assertTrue(f.isCompletedExceptionally()); |
3067 |
+ |
assertTrue(g.isCompletedExceptionally()); |
3068 |
+ |
assertEquals(a.get(), 1); |
3069 |
+ |
} |
3070 |
+ |
|
3071 |
+ |
/** |
3072 |
+ |
* If a whenComplete action throws an exception when triggered by |
3073 |
+ |
* a normal completion, it completes exceptionally |
3074 |
+ |
*/ |
3075 |
+ |
public void testWhenComplete3() { |
3076 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3077 |
+ |
CompletableFuture<Integer> g = |
3078 |
+ |
f.whenComplete((Integer x, Throwable t) -> |
3079 |
+ |
{ throw new CFException(); } ); |
3080 |
+ |
f.complete(three); |
3081 |
+ |
checkCompletedNormally(f, three); |
3082 |
+ |
assertTrue(g.isCompletedExceptionally()); |
3083 |
+ |
checkCompletedWithWrappedCFException(g); |
3084 |
+ |
} |
3085 |
+ |
|
3086 |
+ |
/** |
3087 |
+ |
* whenCompleteAsync action executes on normal completion, propagating |
3088 |
+ |
* source result. |
3089 |
+ |
*/ |
3090 |
+ |
public void testWhenCompleteAsync1() { |
3091 |
+ |
final AtomicInteger a = new AtomicInteger(); |
3092 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3093 |
+ |
CompletableFuture<Integer> g = |
3094 |
+ |
f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement()); |
3095 |
+ |
f.complete(three); |
3096 |
+ |
checkCompletedNormally(f, three); |
3097 |
+ |
checkCompletedNormally(g, three); |
3098 |
+ |
assertEquals(a.get(), 1); |
3099 |
+ |
} |
3100 |
+ |
|
3101 |
+ |
/** |
3102 |
+ |
* whenCompleteAsync action executes on exceptional completion, propagating |
3103 |
+ |
* source result. |
3104 |
+ |
*/ |
3105 |
+ |
public void testWhenCompleteAsync2() { |
3106 |
+ |
final AtomicInteger a = new AtomicInteger(); |
3107 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3108 |
+ |
CompletableFuture<Integer> g = |
3109 |
+ |
f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement()); |
3110 |
+ |
f.completeExceptionally(new CFException()); |
3111 |
+ |
checkCompletedWithWrappedCFException(f); |
3112 |
+ |
checkCompletedWithWrappedCFException(g); |
3113 |
+ |
} |
3114 |
+ |
|
3115 |
+ |
/** |
3116 |
+ |
* If a whenCompleteAsync action throws an exception when |
3117 |
+ |
* triggered by a normal completion, it completes exceptionally |
3118 |
+ |
*/ |
3119 |
+ |
public void testWhenCompleteAsync3() { |
3120 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3121 |
+ |
CompletableFuture<Integer> g = |
3122 |
+ |
f.whenCompleteAsync((Integer x, Throwable t) -> |
3123 |
+ |
{ throw new CFException(); } ); |
3124 |
+ |
f.complete(three); |
3125 |
+ |
checkCompletedNormally(f, three); |
3126 |
+ |
checkCompletedWithWrappedCFException(g); |
3127 |
+ |
} |
3128 |
+ |
|
3129 |
+ |
/** |
3130 |
+ |
* whenCompleteAsync action executes on normal completion, propagating |
3131 |
+ |
* source result. |
3132 |
+ |
*/ |
3133 |
+ |
public void testWhenCompleteAsync1e() { |
3134 |
+ |
final AtomicInteger a = new AtomicInteger(); |
3135 |
+ |
ThreadExecutor exec = new ThreadExecutor(); |
3136 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3137 |
+ |
CompletableFuture<Integer> g = |
3138 |
+ |
f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(), |
3139 |
+ |
exec); |
3140 |
+ |
f.complete(three); |
3141 |
+ |
checkCompletedNormally(f, three); |
3142 |
+ |
checkCompletedNormally(g, three); |
3143 |
+ |
assertEquals(a.get(), 1); |
3144 |
+ |
} |
3145 |
+ |
|
3146 |
+ |
/** |
3147 |
+ |
* whenCompleteAsync action executes on exceptional completion, propagating |
3148 |
+ |
* source result. |
3149 |
+ |
*/ |
3150 |
+ |
public void testWhenCompleteAsync2e() { |
3151 |
+ |
final AtomicInteger a = new AtomicInteger(); |
3152 |
+ |
ThreadExecutor exec = new ThreadExecutor(); |
3153 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3154 |
+ |
CompletableFuture<Integer> g = |
3155 |
+ |
f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(), |
3156 |
+ |
exec); |
3157 |
+ |
f.completeExceptionally(new CFException()); |
3158 |
+ |
checkCompletedWithWrappedCFException(f); |
3159 |
+ |
checkCompletedWithWrappedCFException(g); |
3160 |
+ |
} |
3161 |
+ |
|
3162 |
+ |
/** |
3163 |
+ |
* If a whenCompleteAsync action throws an exception when triggered |
3164 |
+ |
* by a normal completion, it completes exceptionally |
3165 |
+ |
*/ |
3166 |
+ |
public void testWhenCompleteAsync3e() { |
3167 |
+ |
ThreadExecutor exec = new ThreadExecutor(); |
3168 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3169 |
+ |
CompletableFuture<Integer> g = |
3170 |
+ |
f.whenCompleteAsync((Integer x, Throwable t) -> |
3171 |
+ |
{ throw new CFException(); }, |
3172 |
+ |
exec); |
3173 |
+ |
f.complete(three); |
3174 |
+ |
checkCompletedNormally(f, three); |
3175 |
+ |
checkCompletedWithWrappedCFException(g); |
3176 |
+ |
} |
3177 |
+ |
|
3178 |
+ |
/** |
3179 |
+ |
* handleAsync action completes normally with function value on |
3180 |
+ |
* either normal or exceptional completion of source |
3181 |
+ |
*/ |
3182 |
+ |
public void testHandleAsync() { |
3183 |
+ |
CompletableFuture<Integer> f, g; |
3184 |
+ |
IntegerHandler r; |
3185 |
+ |
|
3186 |
+ |
f = new CompletableFuture<>(); |
3187 |
+ |
g = f.handleAsync(r = new IntegerHandler()); |
3188 |
+ |
assertFalse(r.ran); |
3189 |
+ |
f.completeExceptionally(new CFException()); |
3190 |
+ |
checkCompletedWithWrappedCFException(f); |
3191 |
+ |
checkCompletedNormally(g, three); |
3192 |
+ |
assertTrue(r.ran); |
3193 |
+ |
|
3194 |
+ |
f = new CompletableFuture<>(); |
3195 |
+ |
g = f.handleAsync(r = new IntegerHandler()); |
3196 |
+ |
assertFalse(r.ran); |
3197 |
+ |
f.completeExceptionally(new CFException()); |
3198 |
+ |
checkCompletedWithWrappedCFException(f); |
3199 |
+ |
checkCompletedNormally(g, three); |
3200 |
+ |
assertTrue(r.ran); |
3201 |
+ |
|
3202 |
+ |
f = new CompletableFuture<>(); |
3203 |
+ |
g = f.handleAsync(r = new IntegerHandler()); |
3204 |
+ |
assertFalse(r.ran); |
3205 |
+ |
f.complete(one); |
3206 |
+ |
checkCompletedNormally(f, one); |
3207 |
+ |
checkCompletedNormally(g, two); |
3208 |
+ |
assertTrue(r.ran); |
3209 |
+ |
|
3210 |
+ |
f = new CompletableFuture<>(); |
3211 |
+ |
g = f.handleAsync(r = new IntegerHandler()); |
3212 |
+ |
assertFalse(r.ran); |
3213 |
+ |
f.complete(one); |
3214 |
+ |
checkCompletedNormally(f, one); |
3215 |
+ |
checkCompletedNormally(g, two); |
3216 |
+ |
assertTrue(r.ran); |
3217 |
+ |
} |
3218 |
+ |
|
3219 |
+ |
/** |
3220 |
+ |
* handleAsync action with Executor completes normally with |
3221 |
+ |
* function value on either normal or exceptional completion of |
3222 |
+ |
* source |
3223 |
+ |
*/ |
3224 |
+ |
public void testHandleAsync2() { |
3225 |
+ |
CompletableFuture<Integer> f, g; |
3226 |
+ |
ThreadExecutor exec = new ThreadExecutor(); |
3227 |
+ |
IntegerHandler r; |
3228 |
+ |
|
3229 |
+ |
f = new CompletableFuture<>(); |
3230 |
+ |
g = f.handleAsync(r = new IntegerHandler(), exec); |
3231 |
+ |
assertFalse(r.ran); |
3232 |
+ |
f.completeExceptionally(new CFException()); |
3233 |
+ |
checkCompletedWithWrappedCFException(f); |
3234 |
+ |
checkCompletedNormally(g, three); |
3235 |
+ |
assertTrue(r.ran); |
3236 |
+ |
|
3237 |
+ |
f = new CompletableFuture<>(); |
3238 |
+ |
g = f.handleAsync(r = new IntegerHandler(), exec); |
3239 |
+ |
assertFalse(r.ran); |
3240 |
+ |
f.completeExceptionally(new CFException()); |
3241 |
+ |
checkCompletedWithWrappedCFException(f); |
3242 |
+ |
checkCompletedNormally(g, three); |
3243 |
+ |
assertTrue(r.ran); |
3244 |
+ |
|
3245 |
+ |
f = new CompletableFuture<>(); |
3246 |
+ |
g = f.handleAsync(r = new IntegerHandler(), exec); |
3247 |
+ |
assertFalse(r.ran); |
3248 |
+ |
f.complete(one); |
3249 |
+ |
checkCompletedNormally(f, one); |
3250 |
+ |
checkCompletedNormally(g, two); |
3251 |
+ |
assertTrue(r.ran); |
3252 |
+ |
|
3253 |
+ |
f = new CompletableFuture<>(); |
3254 |
+ |
g = f.handleAsync(r = new IntegerHandler(), exec); |
3255 |
+ |
assertFalse(r.ran); |
3256 |
+ |
f.complete(one); |
3257 |
+ |
checkCompletedNormally(f, one); |
3258 |
+ |
checkCompletedNormally(g, two); |
3259 |
+ |
assertTrue(r.ran); |
3260 |
+ |
} |
3261 |
+ |
|
3262 |
|
} |