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.22 by jsr166, Mon Apr 8 20:46:59 2013 UTC vs.
Revision 1.28 by jsr166, Sun Jul 14 16:34:49 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
# Line 495 | Line 496 | public class CompletableFutureTest exten
496       * thenRun result completes normally after normal completion of source
497       */
498      public void testThenRun() {
499 <        CompletableFuture<Integer> f = new CompletableFuture<>();
500 <        Noop r = new Noop();
501 <        CompletableFuture<Void> g = f.thenRun(r);
499 >        CompletableFuture<Integer> f;
500 >        CompletableFuture<Void> g;
501 >        Noop r;
502 >
503 >        f = new CompletableFuture<>();
504 >        g = f.thenRun(r = new Noop());
505          f.complete(null);
506          checkCompletedNormally(g, null);
507 <        // reordered version
507 >        assertTrue(r.ran);
508 >
509          f = new CompletableFuture<>();
510          f.complete(null);
511 <        r = new Noop();
507 <        g = f.thenRun(r);
511 >        g = f.thenRun(r = new Noop());
512          checkCompletedNormally(g, null);
513 +        assertTrue(r.ran);
514      }
515  
516      /**
# Line 513 | Line 518 | public class CompletableFutureTest exten
518       * completion of source
519       */
520      public void testThenRun2() {
521 <        CompletableFuture<Integer> f = new CompletableFuture<>();
522 <        Noop r = new Noop();
523 <        CompletableFuture<Void> g = f.thenRun(r);
521 >        CompletableFuture<Integer> f;
522 >        CompletableFuture<Void> g;
523 >        Noop r;
524 >
525 >        f = new CompletableFuture<>();
526 >        g = f.thenRun(r = new Noop());
527          f.completeExceptionally(new CFException());
528          checkCompletedWithWrappedCFException(g);
529 +        assertFalse(r.ran);
530 +
531 +        f = new CompletableFuture<>();
532 +        f.completeExceptionally(new CFException());
533 +        g = f.thenRun(r = new Noop());
534 +        checkCompletedWithWrappedCFException(g);
535 +        assertFalse(r.ran);
536      }
537  
538      /**
539       * thenRun result completes exceptionally if action does
540       */
541      public void testThenRun3() {
542 <        CompletableFuture<Integer> f = new CompletableFuture<>();
543 <        FailingNoop r = new FailingNoop();
544 <        CompletableFuture<Void> g = f.thenRun(r);
542 >        CompletableFuture<Integer> f;
543 >        CompletableFuture<Void> g;
544 >        FailingNoop r;
545 >
546 >        f = new CompletableFuture<>();
547 >        g = f.thenRun(r = new FailingNoop());
548          f.complete(null);
549          checkCompletedWithWrappedCFException(g);
550 +
551 +        f = new CompletableFuture<>();
552 +        f.complete(null);
553 +        g = f.thenRun(r = new FailingNoop());
554 +        checkCompletedWithWrappedCFException(g);
555      }
556  
557      /**
558       * thenRun result completes exceptionally if source cancelled
559       */
560      public void testThenRun4() {
561 <        CompletableFuture<Integer> f = new CompletableFuture<>();
562 <        Noop r = new Noop();
563 <        CompletableFuture<Void> g = f.thenRun(r);
561 >        CompletableFuture<Integer> f;
562 >        CompletableFuture<Void> g;
563 >        Noop r;
564 >
565 >        f = new CompletableFuture<>();
566 >        g = f.thenRun(r = new Noop());
567 >        assertTrue(f.cancel(true));
568 >        checkCompletedWithWrappedCancellationException(g);
569 >
570 >        f = new CompletableFuture<>();
571          assertTrue(f.cancel(true));
572 +        g = f.thenRun(r = new Noop());
573          checkCompletedWithWrappedCancellationException(g);
574      }
575  
# Line 630 | Line 661 | public class CompletableFutureTest exten
661          checkCompletedWithWrappedCancellationException(g);
662      }
663  
633
664      /**
665       * thenCombine result completes normally after normal completion
666       * of sources
# Line 1138 | Line 1168 | public class CompletableFutureTest exten
1168          checkCompletedWithWrappedCancellationException(g);
1169      }
1170  
1141
1171      /**
1172       * runAfterEither result completes normally after normal completion
1173       * of either source
# Line 1287 | Line 1316 | public class CompletableFutureTest exten
1316          checkCompletedWithWrappedCancellationException(g);
1317      }
1318  
1290
1319      // asyncs
1320  
1321      /**
# Line 2055 | Line 2083 | public class CompletableFutureTest exten
2083          checkCompletedWithWrappedCancellationException(g);
2084      }
2085  
2058
2086      // async with explicit executors
2087  
2088      /**
# Line 2826 | Line 2853 | public class CompletableFutureTest exten
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)
# Line 2841 | Line 2869 | public class CompletableFutureTest exten
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  
# Line 2851 | Line 2881 | public class CompletableFutureTest exten
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<>();
# Line 2868 | Line 2899 | public class CompletableFutureTest exten
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      }
# Line 2980 | Line 3030 | public class CompletableFutureTest exten
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   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines