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.82 by jsr166, Mon Jun 16 18:04:07 2014 UTC vs.
Revision 1.89 by jsr166, Mon Jun 16 21:34:49 2014 UTC

# Line 2922 | Line 2922 | public class CompletableFutureTest exten
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 < 10; 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);
# Line 2939 | Line 2939 | public class CompletableFutureTest exten
2939      }
2940  
2941      public void testAllOf_backwards() throws Exception {
2942 <        for (int k = 1; k < 20; ++k) {
2942 >        for (int k = 1; k < 10; 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--) {
# Line 2955 | Line 2955 | public class CompletableFutureTest exten
2955          }
2956      }
2957  
2958 +    public void testAllOf_exceptional() throws Exception {
2959 +        for (int k = 1; k < 10; k++) {
2960 +            CompletableFuture<Integer>[] fs
2961 +                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2962 +            CFException ex = new CFException();
2963 +            for (int i = 0; i < k; i++)
2964 +                fs[i] = new CompletableFuture<>();
2965 +            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
2966 +            for (int i = 0; i < k; i++) {
2967 +                checkIncomplete(f);
2968 +                checkIncomplete(CompletableFuture.allOf(fs));
2969 +                if (i != k/2) {
2970 +                    fs[i].complete(i);
2971 +                    checkCompletedNormally(fs[i], i);
2972 +                } else {
2973 +                    fs[i].completeExceptionally(ex);
2974 +                    checkCompletedExceptionally(fs[i], ex);
2975 +                }
2976 +            }
2977 +            checkCompletedWithWrappedException(f, ex);
2978 +            checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex);
2979 +        }
2980 +    }
2981 +
2982      /**
2983       * anyOf(no component futures) returns an incomplete future
2984       */
2985      public void testAnyOf_empty() throws Exception {
2986 +        for (Integer v1 : new Integer[] { 1, null })
2987 +    {
2988          CompletableFuture<Object> f = CompletableFuture.anyOf();
2989          checkIncomplete(f);
2990 <    }
2990 >
2991 >        f.complete(v1);
2992 >        checkCompletedNormally(f, v1);
2993 >    }}
2994  
2995      /**
2996       * anyOf returns a future completed normally with a value when
2997       * a component future does
2998       */
2999      public void testAnyOf_normal() throws Exception {
3000 <        for (int k = 0; k < 10; ++k) {
3000 >        for (int k = 0; k < 10; k++) {
3001              CompletableFuture[] fs = new CompletableFuture[k];
3002 <            for (int i = 0; i < k; ++i)
3002 >            for (int i = 0; i < k; i++)
3003                  fs[i] = new CompletableFuture<>();
3004              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3005              checkIncomplete(f);
3006 <            for (int i = 0; i < k; ++i) {
3007 <                fs[i].complete(one);
3008 <                checkCompletedNormally(f, one);
3009 <                checkCompletedNormally(CompletableFuture.anyOf(fs), one);
3006 >            for (int i = 0; i < k; i++) {
3007 >                fs[i].complete(i);
3008 >                checkCompletedNormally(f, 0);
3009 >                int x = (int) CompletableFuture.anyOf(fs).join();
3010 >                assertTrue(0 <= x && x <= i);
3011 >            }
3012 >        }
3013 >    }
3014 >    public void testAnyOf_normal_backwards() throws Exception {
3015 >        for (int k = 0; k < 10; k++) {
3016 >            CompletableFuture[] fs = new CompletableFuture[k];
3017 >            for (int i = 0; i < k; i++)
3018 >                fs[i] = new CompletableFuture<>();
3019 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3020 >            checkIncomplete(f);
3021 >            for (int i = k - 1; i >= 0; i--) {
3022 >                fs[i].complete(i);
3023 >                checkCompletedNormally(f, k - 1);
3024 >                int x = (int) CompletableFuture.anyOf(fs).join();
3025 >                assertTrue(i <= x && x <= k - 1);
3026              }
3027          }
3028      }
# Line 2986 | Line 3031 | public class CompletableFutureTest exten
3031       * anyOf result completes exceptionally when any component does.
3032       */
3033      public void testAnyOf_exceptional() throws Exception {
3034 <        for (int k = 0; k < 10; ++k) {
3034 >        for (int k = 0; k < 10; k++) {
3035              CompletableFuture[] fs = new CompletableFuture[k];
3036 <            for (int i = 0; i < k; ++i)
3036 >            CFException[] exs = new CFException[k];
3037 >            for (int i = 0; i < k; i++) {
3038                  fs[i] = new CompletableFuture<>();
3039 +                exs[i] = new CFException();
3040 +            }
3041              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3042              checkIncomplete(f);
3043 <            for (int i = 0; i < k; ++i) {
3044 <                fs[i].completeExceptionally(new CFException());
3045 <                checkCompletedWithWrappedCFException(f);
3043 >            for (int i = 0; i < k; i++) {
3044 >                fs[i].completeExceptionally(exs[i]);
3045 >                checkCompletedWithWrappedException(f, exs[0]);
3046 >                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3047 >            }
3048 >        }
3049 >    }
3050 >
3051 >    public void testAnyOf_exceptional_backwards() throws Exception {
3052 >        for (int k = 0; k < 10; k++) {
3053 >            CompletableFuture[] fs = new CompletableFuture[k];
3054 >            CFException[] exs = new CFException[k];
3055 >            for (int i = 0; i < k; i++) {
3056 >                fs[i] = new CompletableFuture<>();
3057 >                exs[i] = new CFException();
3058 >            }
3059 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3060 >            checkIncomplete(f);
3061 >            for (int i = k - 1; i >= 0; i--) {
3062 >                fs[i].completeExceptionally(exs[i]);
3063 >                checkCompletedWithWrappedException(f, exs[k - 1]);
3064                  checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3065              }
3066          }
# Line 3133 | Line 3199 | public class CompletableFutureTest exten
3199          if (!testImplementationDetails) return;
3200          for (ExecutionMode m : ExecutionMode.values())
3201      {
3202 <        final CompletableFuture<Void> f = new CompletableFuture<>();
3203 <        CFException ex = new CFException();
3204 <        f.completeExceptionally(ex);
3205 <        final CompletableFuture<Void> g = f.thenRun(new Noop(m));
3206 <        checkCompletedWithWrappedException(g, ex);
3207 <        final CompletableFuture<Void> h = g.thenRun(new Noop(m));
3208 <        checkCompletedWithWrappedException(h, ex);
3209 <        assertSame(resultOf(g), resultOf(h));
3210 <    }}
3202 >        final CFException ex = new CFException();
3203 >        final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42);
3204 >        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3205 >
3206 >        List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs
3207 >            = new ArrayList<>();
3208 >
3209 >        funs.add((y) -> m.thenRun(y, new Noop(m)));
3210 >        funs.add((y) -> m.thenAccept(y, new NoopConsumer(m)));
3211 >        funs.add((y) -> m.thenApply(y, new IncFunction(m)));
3212 >
3213 >        funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m)));
3214 >        funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m)));
3215 >        funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m)));
3216 >
3217 >        funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m)));
3218 >        funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
3219 >        funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m)));
3220 >
3221 >        funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {}));
3222 >
3223 >        funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m)));
3224 >
3225 >        funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42}));
3226 >        funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete}));
3227 >
3228 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3229 >                 fun : funs) {
3230 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3231 >            f.completeExceptionally(ex);
3232 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3233 >            checkCompletedWithWrappedException(src, ex);
3234 >            CompletableFuture<?> dep = fun.apply(src);
3235 >            checkCompletedWithWrappedException(dep, ex);
3236 >            assertSame(resultOf(src), resultOf(dep));
3237 >        }
3238 >
3239 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3240 >                 fun : funs) {
3241 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3242 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3243 >            CompletableFuture<?> dep = fun.apply(src);
3244 >            f.completeExceptionally(ex);
3245 >            checkCompletedWithWrappedException(src, ex);
3246 >            checkCompletedWithWrappedException(dep, ex);
3247 >            assertSame(resultOf(src), resultOf(dep));
3248 >        }
3249 >
3250 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3251 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3252 >                 fun : funs) {
3253 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3254 >            f.cancel(mayInterruptIfRunning);
3255 >            checkCancelled(f);
3256 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3257 >            checkCompletedWithWrappedCancellationException(src);
3258 >            CompletableFuture<?> dep = fun.apply(src);
3259 >            checkCompletedWithWrappedCancellationException(dep);
3260 >            assertSame(resultOf(src), resultOf(dep));
3261 >        }
3262  
3263 < //     public void testRunAfterEither_resultDeterminedAtTimeOfCreation() {
3264 < //         for (ExecutionMode m : ExecutionMode.values())
3265 < //         for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3266 < //         for (Integer v1 : new Integer[] { 1, null })
3267 < //     {
3268 < //         final CompletableFuture<Integer> f = new CompletableFuture<>();
3269 < //         final CompletableFuture<Integer> g = new CompletableFuture<>();
3270 < //         final Noop[] rs = new Noop[2];
3271 < //         for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
3272 < //         f.complete(v1);
3273 < //         final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
3274 < //         final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
3275 < //         assertTrue(g.cancel(mayInterruptIfRunning));
3159 < //         checkCompletedNormally(h0, null);
3160 < //         checkCompletedNormally(h1, null);
3161 < //         for (Noop r : rs) r.assertInvoked();
3162 < //     }}
3263 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3264 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3265 >                 fun : funs) {
3266 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3267 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3268 >            CompletableFuture<?> dep = fun.apply(src);
3269 >            f.cancel(mayInterruptIfRunning);
3270 >            checkCancelled(f);
3271 >            checkCompletedWithWrappedCancellationException(src);
3272 >            checkCompletedWithWrappedCancellationException(dep);
3273 >            assertSame(resultOf(src), resultOf(dep));
3274 >        }
3275 >    }}
3276  
3277   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines