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.30 by jsr166, Mon Jul 22 18:25:42 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 256 | Line 259 | public class CompletableFutureTest exten
259          assertEquals(g.getNumberOfDependents(), 0);
260      }
261  
259
262      /**
263       * toString indicates current completion state
264       */
# Line 371 | Line 373 | public class CompletableFutureTest exten
373          }
374      }
375  
374
376      /**
377       * exceptionally action completes with function value on source
378 <     * exception;  otherwise with source value
378 >     * exception; otherwise with source value
379       */
380      public void testExceptionally() {
381          CompletableFuture<Integer> f = new CompletableFuture<>();
# Line 660 | Line 661 | public class CompletableFutureTest exten
661          checkCompletedWithWrappedCancellationException(g);
662      }
663  
663
664      /**
665       * thenCombine result completes normally after normal completion
666       * of sources
# Line 1168 | Line 1168 | public class CompletableFutureTest exten
1168          checkCompletedWithWrappedCancellationException(g);
1169      }
1170  
1171
1171      /**
1172       * runAfterEither result completes normally after normal completion
1173       * of either source
# Line 1317 | Line 1316 | public class CompletableFutureTest exten
1316          checkCompletedWithWrappedCancellationException(g);
1317      }
1318  
1320
1319      // asyncs
1320  
1321      /**
# Line 1350 | Line 1348 | public class CompletableFutureTest exten
1348          try {
1349              g.join();
1350              shouldThrow();
1351 <        } catch (Exception ok) {
1354 <        }
1351 >        } catch (CompletionException success) {}
1352          checkCompletedWithWrappedCFException(g);
1353      }
1354  
# Line 2085 | Line 2082 | public class CompletableFutureTest exten
2082          checkCompletedWithWrappedCancellationException(g);
2083      }
2084  
2088
2085      // async with explicit executors
2086  
2087      /**
# Line 2118 | Line 2114 | public class CompletableFutureTest exten
2114          try {
2115              g.join();
2116              shouldThrow();
2117 <        } catch (Exception ok) {
2122 <        }
2117 >        } catch (CompletionException success) {}
2118          checkCompletedWithWrappedCFException(g);
2119      }
2120  
# Line 3033 | Line 3028 | public class CompletableFutureTest exten
3028          assertEquals(0, exec.count.get());
3029      }
3030  
3031 +    /**
3032 +     * toCompletableFuture returns this CompletableFuture.
3033 +     */
3034 +    public void testToCompletableFuture() {
3035 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3036 +        assertSame(f, f.toCompletableFuture());
3037 +    }
3038 +
3039 +    /**
3040 +     * whenComplete action executes on normal completion, propagating
3041 +     * source result.
3042 +     */
3043 +    public void testWhenComplete1() {
3044 +        final AtomicInteger a = new AtomicInteger();
3045 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3046 +        CompletableFuture<Integer> g =
3047 +            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3048 +        f.complete(three);
3049 +        checkCompletedNormally(f, three);
3050 +        checkCompletedNormally(g, three);
3051 +        assertEquals(a.get(), 1);
3052 +    }
3053 +
3054 +    /**
3055 +     * whenComplete action executes on exceptional completion, propagating
3056 +     * source result.
3057 +     */
3058 +    public void testWhenComplete2() {
3059 +        final AtomicInteger a = new AtomicInteger();
3060 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3061 +        CompletableFuture<Integer> g =
3062 +            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3063 +        f.completeExceptionally(new CFException());
3064 +        assertTrue(f.isCompletedExceptionally());
3065 +        assertTrue(g.isCompletedExceptionally());
3066 +        assertEquals(a.get(), 1);
3067 +    }
3068 +
3069 +    /**
3070 +     * If a whenComplete action throws an exception when triggered by
3071 +     * a normal completion, it completes exceptionally
3072 +     */
3073 +    public void testWhenComplete3() {
3074 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3075 +        CompletableFuture<Integer> g =
3076 +            f.whenComplete((Integer x, Throwable t) ->
3077 +                           { throw new CFException(); } );
3078 +        f.complete(three);
3079 +        checkCompletedNormally(f, three);
3080 +        assertTrue(g.isCompletedExceptionally());
3081 +        checkCompletedWithWrappedCFException(g);
3082 +    }
3083 +
3084 +    /**
3085 +     * whenCompleteAsync action executes on normal completion, propagating
3086 +     * source result.
3087 +     */
3088 +    public void testWhenCompleteAsync1() {
3089 +        final AtomicInteger a = new AtomicInteger();
3090 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3091 +        CompletableFuture<Integer> g =
3092 +            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3093 +        f.complete(three);
3094 +        checkCompletedNormally(f, three);
3095 +        checkCompletedNormally(g, three);
3096 +        assertEquals(a.get(), 1);
3097 +    }
3098 +
3099 +    /**
3100 +     * whenCompleteAsync action executes on exceptional completion, propagating
3101 +     * source result.
3102 +     */
3103 +    public void testWhenCompleteAsync2() {
3104 +        final AtomicInteger a = new AtomicInteger();
3105 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3106 +        CompletableFuture<Integer> g =
3107 +            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3108 +        f.completeExceptionally(new CFException());
3109 +        checkCompletedWithWrappedCFException(f);
3110 +        checkCompletedWithWrappedCFException(g);
3111 +    }
3112 +
3113 +    /**
3114 +     * If a whenCompleteAsync action throws an exception when
3115 +     * triggered by a normal completion, it completes exceptionally
3116 +     */
3117 +    public void testWhenCompleteAsync3() {
3118 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3119 +        CompletableFuture<Integer> g =
3120 +            f.whenCompleteAsync((Integer x, Throwable t) ->
3121 +                           { throw new CFException(); } );
3122 +        f.complete(three);
3123 +        checkCompletedNormally(f, three);
3124 +        checkCompletedWithWrappedCFException(g);
3125 +    }
3126 +
3127 +    /**
3128 +     * whenCompleteAsync action executes on normal completion, propagating
3129 +     * source result.
3130 +     */
3131 +    public void testWhenCompleteAsync1e() {
3132 +        final AtomicInteger a = new AtomicInteger();
3133 +        ThreadExecutor exec = new ThreadExecutor();
3134 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3135 +        CompletableFuture<Integer> g =
3136 +            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3137 +                                exec);
3138 +        f.complete(three);
3139 +        checkCompletedNormally(f, three);
3140 +        checkCompletedNormally(g, three);
3141 +        assertEquals(a.get(), 1);
3142 +    }
3143 +
3144 +    /**
3145 +     * whenCompleteAsync action executes on exceptional completion, propagating
3146 +     * source result.
3147 +     */
3148 +    public void testWhenCompleteAsync2e() {
3149 +        final AtomicInteger a = new AtomicInteger();
3150 +        ThreadExecutor exec = new ThreadExecutor();
3151 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3152 +        CompletableFuture<Integer> g =
3153 +            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3154 +                                exec);
3155 +        f.completeExceptionally(new CFException());
3156 +        checkCompletedWithWrappedCFException(f);
3157 +        checkCompletedWithWrappedCFException(g);
3158 +    }
3159 +
3160 +    /**
3161 +     * If a whenCompleteAsync action throws an exception when triggered
3162 +     * by a normal completion, it completes exceptionally
3163 +     */
3164 +    public void testWhenCompleteAsync3e() {
3165 +        ThreadExecutor exec = new ThreadExecutor();
3166 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3167 +        CompletableFuture<Integer> g =
3168 +            f.whenCompleteAsync((Integer x, Throwable t) ->
3169 +                                { throw new CFException(); },
3170 +                                exec);
3171 +        f.complete(three);
3172 +        checkCompletedNormally(f, three);
3173 +        checkCompletedWithWrappedCFException(g);
3174 +    }
3175 +
3176 +    /**
3177 +     * handleAsync action completes normally with function value on
3178 +     * either normal or exceptional completion of source
3179 +     */
3180 +    public void testHandleAsync() {
3181 +        CompletableFuture<Integer> f, g;
3182 +        IntegerHandler r;
3183 +
3184 +        f = new CompletableFuture<>();
3185 +        g = f.handleAsync(r = new IntegerHandler());
3186 +        assertFalse(r.ran);
3187 +        f.completeExceptionally(new CFException());
3188 +        checkCompletedWithWrappedCFException(f);
3189 +        checkCompletedNormally(g, three);
3190 +        assertTrue(r.ran);
3191 +
3192 +        f = new CompletableFuture<>();
3193 +        g = f.handleAsync(r = new IntegerHandler());
3194 +        assertFalse(r.ran);
3195 +        f.completeExceptionally(new CFException());
3196 +        checkCompletedWithWrappedCFException(f);
3197 +        checkCompletedNormally(g, three);
3198 +        assertTrue(r.ran);
3199 +
3200 +        f = new CompletableFuture<>();
3201 +        g = f.handleAsync(r = new IntegerHandler());
3202 +        assertFalse(r.ran);
3203 +        f.complete(one);
3204 +        checkCompletedNormally(f, one);
3205 +        checkCompletedNormally(g, two);
3206 +        assertTrue(r.ran);
3207 +
3208 +        f = new CompletableFuture<>();
3209 +        g = f.handleAsync(r = new IntegerHandler());
3210 +        assertFalse(r.ran);
3211 +        f.complete(one);
3212 +        checkCompletedNormally(f, one);
3213 +        checkCompletedNormally(g, two);
3214 +        assertTrue(r.ran);
3215 +    }
3216 +
3217 +    /**
3218 +     * handleAsync action with Executor completes normally with
3219 +     * function value on either normal or exceptional completion of
3220 +     * source
3221 +     */
3222 +    public void testHandleAsync2() {
3223 +        CompletableFuture<Integer> f, g;
3224 +        ThreadExecutor exec = new ThreadExecutor();
3225 +        IntegerHandler r;
3226 +
3227 +        f = new CompletableFuture<>();
3228 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3229 +        assertFalse(r.ran);
3230 +        f.completeExceptionally(new CFException());
3231 +        checkCompletedWithWrappedCFException(f);
3232 +        checkCompletedNormally(g, three);
3233 +        assertTrue(r.ran);
3234 +
3235 +        f = new CompletableFuture<>();
3236 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3237 +        assertFalse(r.ran);
3238 +        f.completeExceptionally(new CFException());
3239 +        checkCompletedWithWrappedCFException(f);
3240 +        checkCompletedNormally(g, three);
3241 +        assertTrue(r.ran);
3242 +
3243 +        f = new CompletableFuture<>();
3244 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3245 +        assertFalse(r.ran);
3246 +        f.complete(one);
3247 +        checkCompletedNormally(f, one);
3248 +        checkCompletedNormally(g, two);
3249 +        assertTrue(r.ran);
3250 +
3251 +        f = new CompletableFuture<>();
3252 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3253 +        assertFalse(r.ran);
3254 +        f.complete(one);
3255 +        checkCompletedNormally(f, one);
3256 +        checkCompletedNormally(g, two);
3257 +        assertTrue(r.ran);
3258 +    }
3259 +
3260   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines