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.49 by jsr166, Mon Jun 2 19:20:51 2014 UTC vs.
Revision 1.51 by jsr166, Mon Jun 2 19:49:28 2014 UTC

# Line 416 | Line 416 | public class CompletableFutureTest exten
416              throw new CFException();
417          }
418      }
419 <    static final class FailingNoop implements Runnable {
419 >    static final class FailingRunnable implements Runnable {
420          int invocationCount = 0;
421          public void run() {
422              invocationCount++;
# Line 933 | Line 933 | public class CompletableFutureTest exten
933       * failing runAsync completes exceptionally after running Runnable
934       */
935      public void testRunAsync3() {
936 <        FailingNoop r = new FailingNoop();
936 >        FailingRunnable r = new FailingRunnable();
937          CompletableFuture<Void> f = CompletableFuture.runAsync(r);
938          checkCompletedWithWrappedCFException(f);
939          assertEquals(1, r.invocationCount);
# Line 983 | Line 983 | public class CompletableFutureTest exten
983          final Noop r = new Noop();
984          if (!createIncomplete) f.complete(v1);
985          final CompletableFuture<Void> g = m.thenRun(f, r);
986 <        if (createIncomplete) f.complete(v1);
986 >        if (createIncomplete) {
987 >            checkIncomplete(g);
988 >            f.complete(v1);
989 >        }
990  
991          checkCompletedNormally(g, null);
992          checkCompletedNormally(f, v1);
# Line 1003 | Line 1006 | public class CompletableFutureTest exten
1006          final Noop r = new Noop();
1007          if (!createIncomplete) f.completeExceptionally(ex);
1008          final CompletableFuture<Void> g = m.thenRun(f, r);
1009 <        if (createIncomplete) f.completeExceptionally(ex);
1009 >        if (createIncomplete) {
1010 >            checkIncomplete(g);
1011 >            f.completeExceptionally(ex);
1012 >        }
1013  
1014          checkCompletedWithWrappedCFException(g, ex);
1015          checkCompletedWithWrappedCFException(f, ex);
# Line 1022 | Line 1028 | public class CompletableFutureTest exten
1028          final Noop r = new Noop();
1029          if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1030          final CompletableFuture<Void> g = f.thenRun(r);
1031 <        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1031 >        if (createIncomplete) {
1032 >            checkIncomplete(g);
1033 >            assertTrue(f.cancel(mayInterruptIfRunning));
1034 >        }
1035  
1036          checkCompletedWithWrappedCancellationException(g);
1037          checkCancelled(f);
# Line 1038 | Line 1047 | public class CompletableFutureTest exten
1047          for (Integer v1 : new Integer[] { 1, null })
1048      {
1049          final CompletableFuture<Integer> f = new CompletableFuture<>();
1050 <        final FailingNoop r = new FailingNoop();
1050 >        final FailingRunnable r = new FailingRunnable();
1051          if (!createIncomplete) f.complete(v1);
1052          final CompletableFuture<Void> g = f.thenRun(r);
1053 <        if (createIncomplete) f.complete(v1);
1053 >        if (createIncomplete) {
1054 >            checkIncomplete(g);
1055 >            f.complete(v1);
1056 >        }
1057  
1058          checkCompletedWithWrappedCFException(g);
1059          checkCompletedNormally(f, v1);
# Line 1082 | Line 1094 | public class CompletableFutureTest exten
1094          final IncFunction r = new IncFunction();
1095          if (!createIncomplete) f.completeExceptionally(ex);
1096          final CompletableFuture<Integer> g = m.thenApply(f, r);
1097 <        if (createIncomplete) f.completeExceptionally(ex);
1097 >        if (createIncomplete) {
1098 >            checkIncomplete(g);
1099 >            f.completeExceptionally(ex);
1100 >        }
1101  
1102          checkCompletedWithWrappedCFException(g, ex);
1103          checkCompletedWithWrappedCFException(f, ex);
# Line 1101 | Line 1116 | public class CompletableFutureTest exten
1116          final IncFunction r = new IncFunction();
1117          if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1118          final CompletableFuture<Integer> g = f.thenApply(r);
1119 <        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1119 >        if (createIncomplete) {
1120 >            checkIncomplete(g);
1121 >            assertTrue(f.cancel(mayInterruptIfRunning));
1122 >        }
1123  
1124          checkCompletedWithWrappedCancellationException(g);
1125          checkCancelled(f);
# Line 1120 | Line 1138 | public class CompletableFutureTest exten
1138          final FailingFunction r = new FailingFunction();
1139          if (!createIncomplete) f.complete(v1);
1140          final CompletableFuture<Integer> g = f.thenApply(r);
1141 <        if (createIncomplete) f.complete(v1);
1141 >        if (createIncomplete) {
1142 >            checkIncomplete(g);
1143 >            f.complete(v1);
1144 >        }
1145  
1146          checkCompletedWithWrappedCFException(g);
1147          checkCompletedNormally(f, v1);
# Line 1129 | Line 1150 | public class CompletableFutureTest exten
1150      /**
1151       * thenAccept result completes normally after normal completion of source
1152       */
1153 <    public void testThenAccept() {
1154 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1155 <        IncAction r = new IncAction();
1156 <        CompletableFuture<Void> g = f.thenAccept(r);
1157 <        f.complete(one);
1153 >    public void testThenAccept_normalCompletion() {
1154 >        for (ExecutionMode m : ExecutionMode.values())
1155 >        for (boolean createIncomplete : new boolean[] { true, false })
1156 >        for (Integer v1 : new Integer[] { 1, null })
1157 >    {
1158 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1159 >        final IncAction r = new IncAction();
1160 >        if (!createIncomplete) f.complete(v1);
1161 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1162 >        if (createIncomplete) {
1163 >            checkIncomplete(g);
1164 >            f.complete(v1);
1165 >        }
1166 >
1167          checkCompletedNormally(g, null);
1168 <        assertEquals(r.value, (Integer) 2);
1169 <    }
1168 >        checkCompletedNormally(f, v1);
1169 >        assertEquals(1, r.invocationCount);
1170 >        assertEquals(inc(v1), r.value);
1171 >    }}
1172  
1173      /**
1174       * thenAccept result completes exceptionally after exceptional
1175       * completion of source
1176       */
1177 <    public void testThenAccept2() {
1178 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1179 <        IncAction r = new IncAction();
1180 <        CompletableFuture<Void> g = f.thenAccept(r);
1181 <        f.completeExceptionally(new CFException());
1182 <        checkCompletedWithWrappedCFException(g);
1183 <    }
1177 >    public void testThenAccept_exceptionalCompletion() {
1178 >        for (ExecutionMode m : ExecutionMode.values())
1179 >        for (boolean createIncomplete : new boolean[] { true, false })
1180 >    {
1181 >        final CFException ex = new CFException();
1182 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1183 >        final IncAction r = new IncAction();
1184 >        if (!createIncomplete) f.completeExceptionally(ex);
1185 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1186 >        if (createIncomplete) {
1187 >            checkIncomplete(g);
1188 >            f.completeExceptionally(ex);
1189 >        }
1190 >
1191 >        checkCompletedWithWrappedCFException(g, ex);
1192 >        checkCompletedWithWrappedCFException(f, ex);
1193 >        assertEquals(0, r.invocationCount);
1194 >    }}
1195  
1196      /**
1197       * thenAccept result completes exceptionally if action does
1198       */
1199 <    public void testThenAccept3() {
1200 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1201 <        FailingConsumer r = new FailingConsumer();
1202 <        CompletableFuture<Void> g = f.thenAccept(r);
1203 <        f.complete(one);
1199 >    public void testThenAccept_actionFailed() {
1200 >        for (ExecutionMode m : ExecutionMode.values())
1201 >        for (boolean createIncomplete : new boolean[] { true, false })
1202 >        for (Integer v1 : new Integer[] { 1, null })
1203 >    {
1204 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1205 >        final FailingConsumer r = new FailingConsumer();
1206 >        if (!createIncomplete) f.complete(v1);
1207 >        final CompletableFuture<Void> g = f.thenAccept(r);
1208 >        if (createIncomplete) {
1209 >            checkIncomplete(g);
1210 >            f.complete(v1);
1211 >        }
1212 >
1213          checkCompletedWithWrappedCFException(g);
1214 <        assertEquals(1, r.invocationCount);
1215 <    }
1214 >        checkCompletedNormally(f, v1);
1215 >    }}
1216  
1217      /**
1218       * thenAccept result completes exceptionally if source cancelled
1219       */
1220 <    public void testThenAccept4() {
1221 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1222 <        IncAction r = new IncAction();
1223 <        CompletableFuture<Void> g = f.thenAccept(r);
1224 <        assertTrue(f.cancel(true));
1220 >    public void testThenAccept_sourceCancelled() {
1221 >        for (ExecutionMode m : ExecutionMode.values())
1222 >        for (boolean createIncomplete : new boolean[] { true, false })
1223 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1224 >    {
1225 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1226 >        final IncAction r = new IncAction();
1227 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1228 >        final CompletableFuture<Void> g = f.thenAccept(r);
1229 >        if (createIncomplete) {
1230 >            checkIncomplete(g);
1231 >            assertTrue(f.cancel(mayInterruptIfRunning));
1232 >        }
1233 >
1234          checkCompletedWithWrappedCancellationException(g);
1235 <    }
1235 >        checkCancelled(f);
1236 >        assertEquals(0, r.invocationCount);
1237 >    }}
1238  
1239      /**
1240       * thenCombine result completes normally after normal completion
1241       * of sources
1242       */
1243 <    public void testThenCombine_normalCompletion1() {
1243 >    public void testThenCombine_normalCompletion() {
1244 >        for (ExecutionMode m : ExecutionMode.values())
1245          for (boolean createIncomplete : new boolean[] { true, false })
1246          for (boolean fFirst : new boolean[] { true, false })
1183        for (ExecutionMode m : ExecutionMode.values())
1247          for (Integer v1 : new Integer[] { 1, null })
1248          for (Integer v2 : new Integer[] { 2, null })
1249      {
1250          final CompletableFuture<Integer> f = new CompletableFuture<>();
1251          final CompletableFuture<Integer> g = new CompletableFuture<>();
1252          final SubtractFunction r = new SubtractFunction();
1190        CompletableFuture<Integer> h = null;
1191        if (createIncomplete) h = m.thenCombine(f, g, r);
1253  
1254 <        if (fFirst)
1255 <            f.complete(v1);
1256 <        else
1257 <            g.complete(v2);
1258 <        if (createIncomplete) checkIncomplete(h);
1259 <        assertEquals(0, r.invocationCount);
1260 <        if (!fFirst)
1261 <            f.complete(v1);
1262 <        else
1202 <            g.complete(v2);
1203 <        if (!createIncomplete) h = m.thenCombine(f, g, r);
1254 >        if (fFirst) f.complete(v1); else g.complete(v2);
1255 >        if (!createIncomplete)
1256 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1257 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1258 >        if (createIncomplete) {
1259 >            checkIncomplete(h);
1260 >            assertEquals(0, r.invocationCount);
1261 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1262 >        }
1263  
1264          checkCompletedNormally(h, subtract(v1, v2));
1265          checkCompletedNormally(f, v1);
# Line 1876 | Line 1935 | public class CompletableFutureTest exten
1935      {
1936          final CompletableFuture<Integer> f = new CompletableFuture<>();
1937          final CompletableFuture<Integer> g = new CompletableFuture<>();
1938 <        final FailingNoop r = new FailingNoop();
1938 >        final FailingRunnable r = new FailingRunnable();
1939          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1940  
1941          f.complete(v1);
# Line 1895 | Line 1954 | public class CompletableFutureTest exten
1954      {
1955          final CompletableFuture<Integer> f = new CompletableFuture<>();
1956          final CompletableFuture<Integer> g = new CompletableFuture<>();
1957 <        final FailingNoop r = new FailingNoop();
1957 >        final FailingRunnable r = new FailingRunnable();
1958          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1959  
1960          g.complete(v2);
# Line 2759 | Line 2818 | public class CompletableFutureTest exten
2818      {
2819          final CompletableFuture<Integer> f = new CompletableFuture<>();
2820          final CompletableFuture<Integer> g = new CompletableFuture<>();
2821 <        final FailingNoop r = new FailingNoop();
2821 >        final FailingRunnable r = new FailingRunnable();
2822          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2823  
2824          f.complete(v1);
# Line 2776 | Line 2835 | public class CompletableFutureTest exten
2835      {
2836          final CompletableFuture<Integer> f = new CompletableFuture<>();
2837          final CompletableFuture<Integer> g = new CompletableFuture<>();
2838 <        final FailingNoop r = new FailingNoop();
2838 >        final FailingRunnable r = new FailingRunnable();
2839          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2840  
2841          g.complete(v2);
# Line 2953 | Line 3012 | public class CompletableFutureTest exten
3012          final CompletableFutureInc r = new CompletableFutureInc();
3013          if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
3014          final CompletableFuture<Integer> g = f.thenCompose(r);
3015 <        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
3015 >        if (createIncomplete) {
3016 >            checkIncomplete(g);
3017 >            assertTrue(f.cancel(mayInterruptIfRunning));
3018 >        }
3019  
3020          checkCompletedWithWrappedCancellationException(g);
3021          checkCancelled(f);
3022      }}
3023  
2962    // asyncs
2963
2964    /**
2965     * thenAcceptAsync result completes normally after normal
2966     * completion of source
2967     */
2968    public void testThenAcceptAsync() {
2969        CompletableFuture<Integer> f = new CompletableFuture<>();
2970        IncAction r = new IncAction();
2971        CompletableFuture<Void> g = f.thenAcceptAsync(r);
2972        f.complete(one);
2973        checkCompletedNormally(g, null);
2974        assertEquals(r.value, (Integer) 2);
2975    }
2976
2977    /**
2978     * thenAcceptAsync result completes exceptionally after exceptional
2979     * completion of source
2980     */
2981    public void testThenAcceptAsync2() {
2982        CompletableFuture<Integer> f = new CompletableFuture<>();
2983        IncAction r = new IncAction();
2984        CompletableFuture<Void> g = f.thenAcceptAsync(r);
2985        f.completeExceptionally(new CFException());
2986        checkCompletedWithWrappedCFException(g);
2987    }
2988
2989    /**
2990     * thenAcceptAsync result completes exceptionally if action does
2991     */
2992    public void testThenAcceptAsync3() {
2993        CompletableFuture<Integer> f = new CompletableFuture<>();
2994        FailingConsumer r = new FailingConsumer();
2995        CompletableFuture<Void> g = f.thenAcceptAsync(r);
2996        f.complete(null);
2997        checkCompletedWithWrappedCFException(g);
2998    }
2999
3000    /**
3001     * thenAcceptAsync result completes exceptionally if source cancelled
3002     */
3003    public void testThenAcceptAsync4() {
3004        CompletableFuture<Integer> f = new CompletableFuture<>();
3005        IncAction r = new IncAction();
3006        CompletableFuture<Void> g = f.thenAcceptAsync(r);
3007        assertTrue(f.cancel(true));
3008        checkCompletedWithWrappedCancellationException(g);
3009    }
3010
3011    // async with explicit executors
3012
3013    /**
3014     * thenAcceptAsync result completes normally after normal
3015     * completion of source
3016     */
3017    public void testThenAcceptAsyncE() {
3018        CompletableFuture<Integer> f = new CompletableFuture<>();
3019        IncAction r = new IncAction();
3020        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3021        f.complete(one);
3022        checkCompletedNormally(g, null);
3023        assertEquals(r.value, (Integer) 2);
3024    }
3025
3026    /**
3027     * thenAcceptAsync result completes exceptionally after exceptional
3028     * completion of source
3029     */
3030    public void testThenAcceptAsync2E() {
3031        CompletableFuture<Integer> f = new CompletableFuture<>();
3032        IncAction r = new IncAction();
3033        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3034        f.completeExceptionally(new CFException());
3035        checkCompletedWithWrappedCFException(g);
3036    }
3037
3038    /**
3039     * thenAcceptAsync result completes exceptionally if action does
3040     */
3041    public void testThenAcceptAsync3E() {
3042        CompletableFuture<Integer> f = new CompletableFuture<>();
3043        FailingConsumer r = new FailingConsumer();
3044        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3045        f.complete(null);
3046        checkCompletedWithWrappedCFException(g);
3047    }
3048
3049    /**
3050     * thenAcceptAsync result completes exceptionally if source cancelled
3051     */
3052    public void testThenAcceptAsync4E() {
3053        CompletableFuture<Integer> f = new CompletableFuture<>();
3054        IncAction r = new IncAction();
3055        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3056        assertTrue(f.cancel(true));
3057        checkCompletedWithWrappedCancellationException(g);
3058    }
3059
3024      // other static methods
3025  
3026      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines