ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
(Generate patch)

Comparing jsr166/src/test/tck/CompletableFutureTest.java (file contents):
Revision 1.25 by jsr166, Sat Apr 20 23:28:35 2013 UTC vs.
Revision 1.26 by dl, Fri Jul 5 15:47:52 2013 UTC

# Line 68 | Line 68 | public class CompletableFutureTest exten
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  
# Line 121 | Line 122 | public class CompletableFutureTest exten
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      }
# Line 152 | Line 154 | public class CompletableFutureTest exten
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  
# Line 3033 | Line 3036 | public class CompletableFutureTest exten
3036          assertEquals(0, exec.count.get());
3037      }
3038  
3039 +    /**
3040 +     * toCompletableFuture returns this CompletableFuture.
3041 +     */
3042 +    public void testToCompletableFuture() {
3043 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3044 +        assertSame(f, f.toCompletableFuture());
3045 +    }
3046 +
3047 +    /**
3048 +     * whenComplete action executes on normal completion, propagating
3049 +     * source result.
3050 +     */
3051 +    public void testWhenComplete1() {
3052 +        final AtomicInteger a = new AtomicInteger();
3053 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3054 +        CompletableFuture<Integer> g =
3055 +            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3056 +        f.complete(three);
3057 +        checkCompletedNormally(f, three);
3058 +        checkCompletedNormally(g, three);
3059 +        assertEquals(a.get(), 1);
3060 +    }
3061 +
3062 +    /**
3063 +     * whenComplete action executes on exceptional completion, propagating
3064 +     * source result.
3065 +     */
3066 +    public void testWhenComplete2() {
3067 +        final AtomicInteger a = new AtomicInteger();
3068 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3069 +        CompletableFuture<Integer> g =
3070 +            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3071 +        f.completeExceptionally(new CFException());
3072 +        assertTrue(f.isCompletedExceptionally());
3073 +        assertTrue(g.isCompletedExceptionally());
3074 +        assertEquals(a.get(), 1);
3075 +    }
3076 +
3077 +    /**
3078 +     * If a whenComplete action throws an exception when triggered by
3079 +     * a normal completion, it completes exceptionally
3080 +     */
3081 +    public void testWhenComplete3() {
3082 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3083 +        CompletableFuture<Integer> g =
3084 +            f.whenComplete((Integer x, Throwable t) ->
3085 +                           { throw new CFException(); } );
3086 +        f.complete(three);
3087 +        checkCompletedNormally(f, three);
3088 +        assertTrue(g.isCompletedExceptionally());
3089 +        checkCompletedWithWrappedCFException(g);
3090 +    }
3091 +
3092 +    /**
3093 +     * whenCompleteAsync action executes on normal completion, propagating
3094 +     * source result.
3095 +     */
3096 +    public void testWhenCompleteAsync1() {
3097 +        final AtomicInteger a = new AtomicInteger();
3098 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3099 +        CompletableFuture<Integer> g =
3100 +            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3101 +        f.complete(three);
3102 +        checkCompletedNormally(f, three);
3103 +        checkCompletedNormally(g, three);
3104 +        assertEquals(a.get(), 1);
3105 +    }
3106 +
3107 +    /**
3108 +     * whenCompleteAsync action executes on exceptional completion, propagating
3109 +     * source result.
3110 +     */
3111 +    public void testWhenCompleteAsync2() {
3112 +        final AtomicInteger a = new AtomicInteger();
3113 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3114 +        CompletableFuture<Integer> g =
3115 +            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3116 +        f.completeExceptionally(new CFException());
3117 +        checkCompletedWithWrappedCFException(f);
3118 +        checkCompletedWithWrappedCFException(g);
3119 +    }
3120 +
3121 +    /**
3122 +     * If a whenCompleteAsync action throws an exception when
3123 +     * triggered by a normal completion, it completes exceptionally
3124 +     */
3125 +    public void testWhenCompleteAsync3() {
3126 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3127 +        CompletableFuture<Integer> g =
3128 +            f.whenCompleteAsync((Integer x, Throwable t) ->
3129 +                           { throw new CFException(); } );
3130 +        f.complete(three);
3131 +        checkCompletedNormally(f, three);
3132 +        checkCompletedWithWrappedCFException(g);
3133 +    }
3134 +
3135 +    /**
3136 +     * whenCompleteAsync action executes on normal completion, propagating
3137 +     * source result.
3138 +     */
3139 +    public void testWhenCompleteAsync1e() {
3140 +        final AtomicInteger a = new AtomicInteger();
3141 +        ThreadExecutor exec = new ThreadExecutor();
3142 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3143 +        CompletableFuture<Integer> g =
3144 +            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3145 +                                exec);
3146 +        f.complete(three);
3147 +        checkCompletedNormally(f, three);
3148 +        checkCompletedNormally(g, three);
3149 +        assertEquals(a.get(), 1);
3150 +    }
3151 +
3152 +    /**
3153 +     * whenCompleteAsync action executes on exceptional completion, propagating
3154 +     * source result.
3155 +     */
3156 +    public void testWhenCompleteAsync2e() {
3157 +        final AtomicInteger a = new AtomicInteger();
3158 +        ThreadExecutor exec = new ThreadExecutor();
3159 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3160 +        CompletableFuture<Integer> g =
3161 +            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3162 +                                exec);
3163 +        f.completeExceptionally(new CFException());
3164 +        checkCompletedWithWrappedCFException(f);
3165 +        checkCompletedWithWrappedCFException(g);
3166 +    }
3167 +
3168 +    /**
3169 +     * If a whenCompleteAsync action throws an exception when triggered
3170 +     * by a normal completion, it completes exceptionally
3171 +     */
3172 +    public void testWhenCompleteAsync3e() {
3173 +        ThreadExecutor exec = new ThreadExecutor();
3174 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3175 +        CompletableFuture<Integer> g =
3176 +            f.whenCompleteAsync((Integer x, Throwable t) ->
3177 +                                { throw new CFException(); },
3178 +                                exec);
3179 +        f.complete(three);
3180 +        checkCompletedNormally(f, three);
3181 +        checkCompletedWithWrappedCFException(g);
3182 +    }
3183 +
3184 +
3185 +    /**
3186 +     * handleAsync action action completes normally with function
3187 +     * value on either normal or exceptional completion of source
3188 +     */
3189 +    public void testHandleAsync() {
3190 +        CompletableFuture<Integer> f, g;
3191 +        IntegerHandler r;
3192 +
3193 +        f = new CompletableFuture<>();
3194 +        g = f.handleAsync(r = new IntegerHandler());
3195 +        assertFalse(r.ran);
3196 +        f.completeExceptionally(new CFException());
3197 +        checkCompletedWithWrappedCFException(f);
3198 +        checkCompletedNormally(g, three);
3199 +        assertTrue(r.ran);
3200 +
3201 +        f = new CompletableFuture<>();
3202 +        g = f.handleAsync(r = new IntegerHandler());
3203 +        assertFalse(r.ran);
3204 +        f.completeExceptionally(new CFException());
3205 +        checkCompletedWithWrappedCFException(f);
3206 +        checkCompletedNormally(g, three);
3207 +        assertTrue(r.ran);
3208 +
3209 +        f = new CompletableFuture<>();
3210 +        g = f.handleAsync(r = new IntegerHandler());
3211 +        assertFalse(r.ran);
3212 +        f.complete(one);
3213 +        checkCompletedNormally(f, one);
3214 +        checkCompletedNormally(g, two);
3215 +        assertTrue(r.ran);
3216 +
3217 +        f = new CompletableFuture<>();
3218 +        g = f.handleAsync(r = new IntegerHandler());
3219 +        assertFalse(r.ran);
3220 +        f.complete(one);
3221 +        checkCompletedNormally(f, one);
3222 +        checkCompletedNormally(g, two);
3223 +        assertTrue(r.ran);
3224 +    }
3225 +
3226 +    /**
3227 +     * handleAsync action with Executor completes normally with
3228 +     * function value on either normal or exceptional completion of
3229 +     * source
3230 +     */
3231 +    public void testHandleAsync2() {
3232 +        CompletableFuture<Integer> f, g;
3233 +        ThreadExecutor exec = new ThreadExecutor();
3234 +        IntegerHandler r;
3235 +
3236 +        f = new CompletableFuture<>();
3237 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3238 +        assertFalse(r.ran);
3239 +        f.completeExceptionally(new CFException());
3240 +        checkCompletedWithWrappedCFException(f);
3241 +        checkCompletedNormally(g, three);
3242 +        assertTrue(r.ran);
3243 +
3244 +        f = new CompletableFuture<>();
3245 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3246 +        assertFalse(r.ran);
3247 +        f.completeExceptionally(new CFException());
3248 +        checkCompletedWithWrappedCFException(f);
3249 +        checkCompletedNormally(g, three);
3250 +        assertTrue(r.ran);
3251 +
3252 +        f = new CompletableFuture<>();
3253 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3254 +        assertFalse(r.ran);
3255 +        f.complete(one);
3256 +        checkCompletedNormally(f, one);
3257 +        checkCompletedNormally(g, two);
3258 +        assertTrue(r.ran);
3259 +
3260 +        f = new CompletableFuture<>();
3261 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3262 +        assertFalse(r.ran);
3263 +        f.complete(one);
3264 +        checkCompletedNormally(f, one);
3265 +        checkCompletedNormally(g, two);
3266 +        assertTrue(r.ran);
3267 +    }
3268 +
3269   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines