947 |
|
} |
948 |
|
|
949 |
|
/** |
950 |
+ |
* Permits the testing of parallel code for the 3 different |
951 |
+ |
* execution modes without repeating all the testing code. |
952 |
+ |
*/ |
953 |
+ |
enum ExecutionMode { |
954 |
+ |
DEFAULT { |
955 |
+ |
public <T,U> CompletableFuture<Void> runAfterBoth |
956 |
+ |
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable r) { |
957 |
+ |
return f.runAfterBoth(g, r); |
958 |
+ |
} |
959 |
+ |
}, |
960 |
+ |
|
961 |
+ |
DEFAULT_ASYNC { |
962 |
+ |
public <T,U> CompletableFuture<Void> runAfterBoth |
963 |
+ |
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable r) { |
964 |
+ |
return f.runAfterBothAsync(g, r); |
965 |
+ |
} |
966 |
+ |
}, |
967 |
+ |
|
968 |
+ |
EXECUTOR { |
969 |
+ |
public <T,U> CompletableFuture<Void> runAfterBoth |
970 |
+ |
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable r) { |
971 |
+ |
return f.runAfterBothAsync(g, r, new ThreadExecutor()); |
972 |
+ |
} |
973 |
+ |
}; |
974 |
+ |
|
975 |
+ |
public abstract <T,U> CompletableFuture<Void> runAfterBoth |
976 |
+ |
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable r); |
977 |
+ |
} |
978 |
+ |
|
979 |
+ |
/** |
980 |
|
* runAfterBoth result completes normally after normal |
981 |
|
* completion of sources |
982 |
|
*/ |
983 |
|
public void testRunAfterBoth_normalCompletion1() { |
984 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
985 |
|
for (Integer v1 : new Integer[] { 1, null }) |
986 |
|
for (Integer v2 : new Integer[] { 2, null }) { |
987 |
|
|
988 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
989 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
990 |
|
final Noop r = new Noop(); |
991 |
< |
final CompletableFuture<Void> h = f.runAfterBoth(g, r); |
991 |
> |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
992 |
|
|
993 |
|
f.complete(v1); |
994 |
|
checkIncomplete(h); |
1003 |
|
} |
1004 |
|
|
1005 |
|
public void testRunAfterBoth_normalCompletion2() { |
1006 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
1007 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1008 |
|
for (Integer v2 : new Integer[] { 2, null }) { |
1009 |
|
|
1010 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1011 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1012 |
|
final Noop r = new Noop(); |
1013 |
< |
final CompletableFuture<Void> h = f.runAfterBoth(g, r); |
1013 |
> |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1014 |
|
|
1015 |
|
g.complete(v2); |
1016 |
|
checkIncomplete(h); |
1025 |
|
} |
1026 |
|
|
1027 |
|
public void testRunAfterBoth_normalCompletion3() { |
1028 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
1029 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1030 |
|
for (Integer v2 : new Integer[] { 2, null }) { |
1031 |
|
|
1035 |
|
|
1036 |
|
g.complete(v2); |
1037 |
|
f.complete(v1); |
1038 |
< |
final CompletableFuture<Void> h = f.runAfterBoth(g, r); |
1038 |
> |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1039 |
|
|
1040 |
|
checkCompletedNormally(h, null); |
1041 |
|
assertTrue(r.ran); |
1045 |
|
} |
1046 |
|
|
1047 |
|
public void testRunAfterBoth_normalCompletion4() { |
1048 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
1049 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1050 |
|
for (Integer v2 : new Integer[] { 2, null }) { |
1051 |
|
|
1055 |
|
|
1056 |
|
f.complete(v1); |
1057 |
|
g.complete(v2); |
1058 |
< |
final CompletableFuture<Void> h = f.runAfterBoth(g, r); |
1058 |
> |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1059 |
|
|
1060 |
|
checkCompletedNormally(h, null); |
1061 |
|
assertTrue(r.ran); |
1069 |
|
* completion of either source |
1070 |
|
*/ |
1071 |
|
public void testRunAfterBoth_exceptionalCompletion1() { |
1072 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
1073 |
|
for (Integer v1 : new Integer[] { 1, null }) { |
1074 |
|
|
1075 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1076 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1077 |
|
final Noop r = new Noop(); |
1078 |
< |
final CompletableFuture<Void> h = f.runAfterBoth(g, r); |
1078 |
> |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1079 |
|
final CFException ex = new CFException(); |
1080 |
|
|
1081 |
|
f.completeExceptionally(ex); |
1090 |
|
} |
1091 |
|
|
1092 |
|
public void testRunAfterBoth_exceptionalCompletion2() { |
1093 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
1094 |
|
for (Integer v1 : new Integer[] { 1, null }) { |
1095 |
|
|
1096 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1097 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1098 |
|
final Noop r = new Noop(); |
1099 |
< |
final CompletableFuture<Void> h = f.runAfterBoth(g, r); |
1099 |
> |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1100 |
|
final CFException ex = new CFException(); |
1101 |
|
|
1102 |
|
g.completeExceptionally(ex); |
1111 |
|
} |
1112 |
|
|
1113 |
|
public void testRunAfterBoth_exceptionalCompletion3() { |
1114 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
1115 |
|
for (Integer v1 : new Integer[] { 1, null }) { |
1116 |
|
|
1117 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1121 |
|
|
1122 |
|
g.completeExceptionally(ex); |
1123 |
|
f.complete(v1); |
1124 |
< |
final CompletableFuture<Void> h = f.runAfterBoth(g, r); |
1124 |
> |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1125 |
|
|
1126 |
|
checkCompletedWithWrappedCFException(h, ex); |
1127 |
|
checkCompletedWithWrappedCFException(g, ex); |
1131 |
|
} |
1132 |
|
|
1133 |
|
public void testRunAfterBoth_exceptionalCompletion4() { |
1134 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
1135 |
|
for (Integer v1 : new Integer[] { 1, null }) { |
1136 |
|
|
1137 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1141 |
|
|
1142 |
|
f.completeExceptionally(ex); |
1143 |
|
g.complete(v1); |
1144 |
< |
final CompletableFuture<Void> h = f.runAfterBoth(g, r); |
1144 |
> |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1145 |
|
|
1146 |
|
checkCompletedWithWrappedCFException(h, ex); |
1147 |
|
checkCompletedWithWrappedCFException(f, ex); |
1154 |
|
* runAfterBoth result completes exceptionally if action does |
1155 |
|
*/ |
1156 |
|
public void testRunAfterBoth_actionFailed1() { |
1157 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
1158 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1159 |
|
for (Integer v2 : new Integer[] { 2, null }) { |
1160 |
|
|
1161 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1162 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1163 |
|
final FailingNoop r = new FailingNoop(); |
1164 |
< |
final CompletableFuture<Void> h = f.runAfterBoth(g, r); |
1164 |
> |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1165 |
|
|
1166 |
|
f.complete(v1); |
1167 |
|
checkIncomplete(h); |
1174 |
|
} |
1175 |
|
|
1176 |
|
public void testRunAfterBoth_actionFailed2() { |
1177 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
1178 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1179 |
|
for (Integer v2 : new Integer[] { 2, null }) { |
1180 |
|
|
1181 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1182 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1183 |
|
final FailingNoop r = new FailingNoop(); |
1184 |
< |
final CompletableFuture<Void> h = f.runAfterBoth(g, r); |
1184 |
> |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1185 |
|
|
1186 |
|
g.complete(v2); |
1187 |
|
checkIncomplete(h); |
1197 |
|
* runAfterBoth result completes exceptionally if either source cancelled |
1198 |
|
*/ |
1199 |
|
public void testRunAfterBoth_sourceCancelled1() { |
1200 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
1201 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1202 |
|
for (Integer v1 : new Integer[] { 1, null }) { |
1203 |
|
|
1204 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1205 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1206 |
|
final Noop r = new Noop(); |
1207 |
< |
final CompletableFuture<Void> h = f.runAfterBoth(g, r); |
1207 |
> |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1208 |
|
|
1209 |
|
assertTrue(f.cancel(mayInterruptIfRunning)); |
1210 |
|
checkIncomplete(h); |
1218 |
|
} |
1219 |
|
|
1220 |
|
public void testRunAfterBoth_sourceCancelled2() { |
1221 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
1222 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1223 |
|
for (Integer v1 : new Integer[] { 1, null }) { |
1224 |
|
|
1225 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1226 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1227 |
|
final Noop r = new Noop(); |
1228 |
< |
final CompletableFuture<Void> h = f.runAfterBoth(g, r); |
1228 |
> |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1229 |
|
|
1230 |
|
assertTrue(g.cancel(mayInterruptIfRunning)); |
1231 |
|
checkIncomplete(h); |
1239 |
|
} |
1240 |
|
|
1241 |
|
public void testRunAfterBoth_sourceCancelled3() { |
1242 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
1243 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1244 |
|
for (Integer v1 : new Integer[] { 1, null }) { |
1245 |
|
|
1249 |
|
|
1250 |
|
assertTrue(g.cancel(mayInterruptIfRunning)); |
1251 |
|
f.complete(v1); |
1252 |
< |
final CompletableFuture<Void> h = f.runAfterBoth(g, r); |
1252 |
> |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1253 |
|
|
1254 |
|
checkCompletedWithWrappedCancellationException(h); |
1255 |
|
checkCancelled(g); |
1259 |
|
} |
1260 |
|
|
1261 |
|
public void testRunAfterBoth_sourceCancelled4() { |
1262 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
1263 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1264 |
|
for (Integer v1 : new Integer[] { 1, null }) { |
1265 |
|
|
1269 |
|
|
1270 |
|
assertTrue(f.cancel(mayInterruptIfRunning)); |
1271 |
|
g.complete(v1); |
1272 |
< |
final CompletableFuture<Void> h = f.runAfterBoth(g, r); |
1272 |
> |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1273 |
|
|
1274 |
|
checkCompletedWithWrappedCancellationException(h); |
1275 |
|
checkCancelled(f); |
1967 |
|
} |
1968 |
|
|
1969 |
|
/** |
1926 |
– |
* runAfterBothAsync result completes normally after normal |
1927 |
– |
* completion of sources |
1928 |
– |
*/ |
1929 |
– |
public void testRunAfterBothAsync() { |
1930 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1931 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
1932 |
– |
Noop r = new Noop(); |
1933 |
– |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r); |
1934 |
– |
f.complete(one); |
1935 |
– |
checkIncomplete(g); |
1936 |
– |
f2.complete(two); |
1937 |
– |
checkCompletedNormally(g, null); |
1938 |
– |
assertTrue(r.ran); |
1939 |
– |
} |
1940 |
– |
|
1941 |
– |
/** |
1942 |
– |
* runAfterBothAsync result completes exceptionally after exceptional |
1943 |
– |
* completion of source |
1944 |
– |
*/ |
1945 |
– |
public void testRunAfterBothAsync2() { |
1946 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1947 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
1948 |
– |
Noop r = new Noop(); |
1949 |
– |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r); |
1950 |
– |
f.completeExceptionally(new CFException()); |
1951 |
– |
f2.complete(two); |
1952 |
– |
checkCompletedWithWrappedCFException(g); |
1953 |
– |
|
1954 |
– |
r = new Noop(); |
1955 |
– |
f = new CompletableFuture<>(); |
1956 |
– |
f2 = new CompletableFuture<>(); |
1957 |
– |
g = f.runAfterBothAsync(f2, r); |
1958 |
– |
f.complete(one); |
1959 |
– |
f2.completeExceptionally(new CFException()); |
1960 |
– |
checkCompletedWithWrappedCFException(g); |
1961 |
– |
} |
1962 |
– |
|
1963 |
– |
/** |
1964 |
– |
* runAfterBothAsync result completes exceptionally if action does |
1965 |
– |
*/ |
1966 |
– |
public void testRunAfterBothAsync3() { |
1967 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1968 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
1969 |
– |
FailingNoop r = new FailingNoop(); |
1970 |
– |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r); |
1971 |
– |
f.complete(one); |
1972 |
– |
checkIncomplete(g); |
1973 |
– |
f2.complete(two); |
1974 |
– |
checkCompletedWithWrappedCFException(g); |
1975 |
– |
} |
1976 |
– |
|
1977 |
– |
/** |
1978 |
– |
* runAfterBothAsync result completes exceptionally if either source cancelled |
1979 |
– |
*/ |
1980 |
– |
public void testRunAfterBothAsync4() { |
1981 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1982 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
1983 |
– |
Noop r = new Noop(); |
1984 |
– |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r); |
1985 |
– |
assertTrue(f.cancel(true)); |
1986 |
– |
f2.complete(two); |
1987 |
– |
checkCompletedWithWrappedCancellationException(g); |
1988 |
– |
|
1989 |
– |
r = new Noop(); |
1990 |
– |
f = new CompletableFuture<>(); |
1991 |
– |
f2 = new CompletableFuture<>(); |
1992 |
– |
g = f.runAfterBothAsync(f2, r); |
1993 |
– |
f.complete(one); |
1994 |
– |
assertTrue(f2.cancel(true)); |
1995 |
– |
checkCompletedWithWrappedCancellationException(g); |
1996 |
– |
} |
1997 |
– |
|
1998 |
– |
/** |
1970 |
|
* applyToEitherAsync result completes normally after normal |
1971 |
|
* completion of sources |
1972 |
|
*/ |
2686 |
|
} |
2687 |
|
|
2688 |
|
/** |
2718 |
– |
* runAfterBothAsync result completes normally after normal |
2719 |
– |
* completion of sources |
2720 |
– |
*/ |
2721 |
– |
public void testRunAfterBothAsyncE() { |
2722 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2723 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2724 |
– |
Noop r = new Noop(); |
2725 |
– |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2726 |
– |
f.complete(one); |
2727 |
– |
checkIncomplete(g); |
2728 |
– |
f2.complete(two); |
2729 |
– |
checkCompletedNormally(g, null); |
2730 |
– |
assertTrue(r.ran); |
2731 |
– |
} |
2732 |
– |
|
2733 |
– |
/** |
2734 |
– |
* runAfterBothAsync result completes exceptionally after exceptional |
2735 |
– |
* completion of source |
2736 |
– |
*/ |
2737 |
– |
public void testRunAfterBothAsync2E() { |
2738 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2739 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2740 |
– |
Noop r = new Noop(); |
2741 |
– |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2742 |
– |
f.completeExceptionally(new CFException()); |
2743 |
– |
f2.complete(two); |
2744 |
– |
checkCompletedWithWrappedCFException(g); |
2745 |
– |
|
2746 |
– |
r = new Noop(); |
2747 |
– |
f = new CompletableFuture<>(); |
2748 |
– |
f2 = new CompletableFuture<>(); |
2749 |
– |
g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2750 |
– |
f.complete(one); |
2751 |
– |
f2.completeExceptionally(new CFException()); |
2752 |
– |
checkCompletedWithWrappedCFException(g); |
2753 |
– |
} |
2754 |
– |
|
2755 |
– |
/** |
2756 |
– |
* runAfterBothAsync result completes exceptionally if action does |
2757 |
– |
*/ |
2758 |
– |
public void testRunAfterBothAsync3E() { |
2759 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2760 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2761 |
– |
FailingNoop r = new FailingNoop(); |
2762 |
– |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2763 |
– |
f.complete(one); |
2764 |
– |
checkIncomplete(g); |
2765 |
– |
f2.complete(two); |
2766 |
– |
checkCompletedWithWrappedCFException(g); |
2767 |
– |
} |
2768 |
– |
|
2769 |
– |
/** |
2770 |
– |
* runAfterBothAsync result completes exceptionally if either source cancelled |
2771 |
– |
*/ |
2772 |
– |
public void testRunAfterBothAsync4E() { |
2773 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2774 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2775 |
– |
Noop r = new Noop(); |
2776 |
– |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2777 |
– |
assertTrue(f.cancel(true)); |
2778 |
– |
f2.complete(two); |
2779 |
– |
checkCompletedWithWrappedCancellationException(g); |
2780 |
– |
|
2781 |
– |
r = new Noop(); |
2782 |
– |
f = new CompletableFuture<>(); |
2783 |
– |
f2 = new CompletableFuture<>(); |
2784 |
– |
g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2785 |
– |
f.complete(one); |
2786 |
– |
assertTrue(f2.cancel(true)); |
2787 |
– |
checkCompletedWithWrappedCancellationException(g); |
2788 |
– |
} |
2789 |
– |
|
2790 |
– |
/** |
2689 |
|
* applyToEitherAsync result completes normally after normal |
2690 |
|
* completion of sources |
2691 |
|
*/ |