102 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
103 |
|
} |
104 |
|
|
105 |
+ |
void checkCompletedWithWrappedCFException(CompletableFuture<?> f, |
106 |
+ |
CFException ex) { |
107 |
+ |
try { |
108 |
+ |
f.get(LONG_DELAY_MS, MILLISECONDS); |
109 |
+ |
shouldThrow(); |
110 |
+ |
} catch (ExecutionException success) { |
111 |
+ |
assertSame(ex, success.getCause()); |
112 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
113 |
+ |
try { |
114 |
+ |
f.join(); |
115 |
+ |
shouldThrow(); |
116 |
+ |
} catch (CompletionException success) { |
117 |
+ |
assertSame(ex, success.getCause()); |
118 |
+ |
} |
119 |
+ |
try { |
120 |
+ |
f.getNow(null); |
121 |
+ |
shouldThrow(); |
122 |
+ |
} catch (CompletionException success) { |
123 |
+ |
assertSame(ex, success.getCause()); |
124 |
+ |
} |
125 |
+ |
try { |
126 |
+ |
f.get(); |
127 |
+ |
shouldThrow(); |
128 |
+ |
} catch (ExecutionException success) { |
129 |
+ |
assertSame(ex, success.getCause()); |
130 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
131 |
+ |
assertTrue(f.isDone()); |
132 |
+ |
assertFalse(f.isCancelled()); |
133 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
134 |
+ |
} |
135 |
+ |
|
136 |
|
void checkCancelled(CompletableFuture<?> f) { |
137 |
|
try { |
138 |
|
f.get(LONG_DELAY_MS, MILLISECONDS); |
950 |
|
* runAfterBoth result completes normally after normal |
951 |
|
* completion of sources |
952 |
|
*/ |
953 |
< |
public void testRunAfterBoth() { |
954 |
< |
CompletableFuture<Integer> f, g; |
955 |
< |
CompletableFuture<Void> h; |
956 |
< |
Noop r; |
953 |
> |
public void testRunAfterBoth_normalCompletion1() { |
954 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
955 |
> |
for (Integer v2 : new Integer[] { 2, null }) { |
956 |
> |
|
957 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
958 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
959 |
> |
final Noop r = new Noop(); |
960 |
> |
final CompletableFuture<Void> h = f.runAfterBoth(g, r); |
961 |
|
|
962 |
< |
f = new CompletableFuture<>(); |
928 |
< |
g = new CompletableFuture<>(); |
929 |
< |
h = f.runAfterBoth(g, r = new Noop()); |
930 |
< |
f.complete(3); |
962 |
> |
f.complete(v1); |
963 |
|
checkIncomplete(h); |
964 |
< |
g.complete(1); |
964 |
> |
assertFalse(r.ran); |
965 |
> |
g.complete(v2); |
966 |
> |
|
967 |
|
checkCompletedNormally(h, null); |
968 |
|
assertTrue(r.ran); |
969 |
+ |
checkCompletedNormally(f, v1); |
970 |
+ |
checkCompletedNormally(g, v2); |
971 |
+ |
} |
972 |
+ |
} |
973 |
|
|
974 |
< |
f = new CompletableFuture<>(); |
975 |
< |
g = new CompletableFuture<>(); |
976 |
< |
h = f.runAfterBoth(g, r = new Noop()); |
977 |
< |
g.complete(1); |
974 |
> |
public void testRunAfterBoth_normalCompletion2() { |
975 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
976 |
> |
for (Integer v2 : new Integer[] { 2, null }) { |
977 |
> |
|
978 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
979 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
980 |
> |
final Noop r = new Noop(); |
981 |
> |
final CompletableFuture<Void> h = f.runAfterBoth(g, r); |
982 |
> |
|
983 |
> |
g.complete(v2); |
984 |
|
checkIncomplete(h); |
985 |
< |
f.complete(3); |
985 |
> |
assertFalse(r.ran); |
986 |
> |
f.complete(v1); |
987 |
> |
|
988 |
|
checkCompletedNormally(h, null); |
989 |
|
assertTrue(r.ran); |
990 |
+ |
checkCompletedNormally(f, v1); |
991 |
+ |
checkCompletedNormally(g, v2); |
992 |
+ |
} |
993 |
+ |
} |
994 |
+ |
|
995 |
+ |
public void testRunAfterBoth_normalCompletion3() { |
996 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
997 |
+ |
for (Integer v2 : new Integer[] { 2, null }) { |
998 |
+ |
|
999 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1000 |
+ |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1001 |
+ |
final Noop r = new Noop(); |
1002 |
+ |
|
1003 |
+ |
g.complete(v2); |
1004 |
+ |
f.complete(v1); |
1005 |
+ |
final CompletableFuture<Void> h = f.runAfterBoth(g, r); |
1006 |
|
|
945 |
– |
f = new CompletableFuture<>(); |
946 |
– |
g = new CompletableFuture<>(); |
947 |
– |
g.complete(1); |
948 |
– |
f.complete(3); |
949 |
– |
h = f.runAfterBoth(g, r = new Noop()); |
1007 |
|
checkCompletedNormally(h, null); |
1008 |
|
assertTrue(r.ran); |
1009 |
+ |
checkCompletedNormally(f, v1); |
1010 |
+ |
checkCompletedNormally(g, v2); |
1011 |
+ |
} |
1012 |
+ |
} |
1013 |
+ |
|
1014 |
+ |
public void testRunAfterBoth_normalCompletion4() { |
1015 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
1016 |
+ |
for (Integer v2 : new Integer[] { 2, null }) { |
1017 |
+ |
|
1018 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1019 |
+ |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1020 |
+ |
final Noop r = new Noop(); |
1021 |
+ |
|
1022 |
+ |
f.complete(v1); |
1023 |
+ |
g.complete(v2); |
1024 |
+ |
final CompletableFuture<Void> h = f.runAfterBoth(g, r); |
1025 |
+ |
|
1026 |
+ |
checkCompletedNormally(h, null); |
1027 |
+ |
assertTrue(r.ran); |
1028 |
+ |
checkCompletedNormally(f, v1); |
1029 |
+ |
checkCompletedNormally(g, v2); |
1030 |
+ |
} |
1031 |
|
} |
1032 |
|
|
1033 |
|
/** |
1034 |
|
* runAfterBoth result completes exceptionally after exceptional |
1035 |
|
* completion of either source |
1036 |
|
*/ |
1037 |
< |
public void testRunAfterBoth2() { |
1038 |
< |
CompletableFuture<Integer> f, g; |
960 |
< |
CompletableFuture<Void> h; |
961 |
< |
Noop r; |
1037 |
> |
public void testRunAfterBoth_exceptionalCompletion1() { |
1038 |
> |
for (Integer v1 : new Integer[] { 1, null }) { |
1039 |
|
|
1040 |
< |
f = new CompletableFuture<>(); |
1041 |
< |
g = new CompletableFuture<>(); |
1042 |
< |
h = f.runAfterBoth(g, r = new Noop()); |
1043 |
< |
f.completeExceptionally(new CFException()); |
1040 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1041 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1042 |
> |
final Noop r = new Noop(); |
1043 |
> |
final CompletableFuture<Void> h = f.runAfterBoth(g, r); |
1044 |
> |
final CFException ex = new CFException(); |
1045 |
> |
|
1046 |
> |
f.completeExceptionally(ex); |
1047 |
|
checkIncomplete(h); |
1048 |
< |
g.complete(1); |
1049 |
< |
checkCompletedWithWrappedCFException(h); |
1048 |
> |
g.complete(v1); |
1049 |
> |
|
1050 |
> |
checkCompletedWithWrappedCFException(h, ex); |
1051 |
> |
checkCompletedWithWrappedCFException(f, ex); |
1052 |
|
assertFalse(r.ran); |
1053 |
+ |
checkCompletedNormally(g, v1); |
1054 |
+ |
} |
1055 |
+ |
} |
1056 |
|
|
1057 |
< |
f = new CompletableFuture<>(); |
1058 |
< |
g = new CompletableFuture<>(); |
1059 |
< |
h = f.runAfterBoth(g, r = new Noop()); |
1060 |
< |
g.completeExceptionally(new CFException()); |
1057 |
> |
public void testRunAfterBoth_exceptionalCompletion2() { |
1058 |
> |
for (Integer v1 : new Integer[] { 1, null }) { |
1059 |
> |
|
1060 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1061 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1062 |
> |
final Noop r = new Noop(); |
1063 |
> |
final CompletableFuture<Void> h = f.runAfterBoth(g, r); |
1064 |
> |
final CFException ex = new CFException(); |
1065 |
> |
|
1066 |
> |
g.completeExceptionally(ex); |
1067 |
|
checkIncomplete(h); |
1068 |
< |
f.complete(3); |
1069 |
< |
checkCompletedWithWrappedCFException(h); |
1068 |
> |
f.complete(v1); |
1069 |
> |
|
1070 |
> |
checkCompletedWithWrappedCFException(h, ex); |
1071 |
> |
checkCompletedWithWrappedCFException(g, ex); |
1072 |
|
assertFalse(r.ran); |
1073 |
+ |
checkCompletedNormally(f, v1); |
1074 |
+ |
} |
1075 |
+ |
} |
1076 |
|
|
1077 |
< |
f = new CompletableFuture<>(); |
1078 |
< |
g = new CompletableFuture<>(); |
1079 |
< |
g.completeExceptionally(new CFException()); |
1080 |
< |
f.complete(3); |
1081 |
< |
h = f.runAfterBoth(g, r = new Noop()); |
1082 |
< |
checkCompletedWithWrappedCFException(h); |
1077 |
> |
public void testRunAfterBoth_exceptionalCompletion3() { |
1078 |
> |
for (Integer v1 : new Integer[] { 1, null }) { |
1079 |
> |
|
1080 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1081 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1082 |
> |
final Noop r = new Noop(); |
1083 |
> |
final CFException ex = new CFException(); |
1084 |
> |
|
1085 |
> |
g.completeExceptionally(ex); |
1086 |
> |
f.complete(v1); |
1087 |
> |
final CompletableFuture<Void> h = f.runAfterBoth(g, r); |
1088 |
> |
|
1089 |
> |
checkCompletedWithWrappedCFException(h, ex); |
1090 |
> |
checkCompletedWithWrappedCFException(g, ex); |
1091 |
|
assertFalse(r.ran); |
1092 |
+ |
checkCompletedNormally(f, v1); |
1093 |
+ |
} |
1094 |
+ |
} |
1095 |
|
|
1096 |
< |
f = new CompletableFuture<>(); |
1097 |
< |
g = new CompletableFuture<>(); |
1098 |
< |
f.completeExceptionally(new CFException()); |
1099 |
< |
g.complete(1); |
1100 |
< |
h = f.runAfterBoth(g, r = new Noop()); |
1101 |
< |
checkCompletedWithWrappedCFException(h); |
1096 |
> |
public void testRunAfterBoth_exceptionalCompletion4() { |
1097 |
> |
for (Integer v1 : new Integer[] { 1, null }) { |
1098 |
> |
|
1099 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1100 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1101 |
> |
final Noop r = new Noop(); |
1102 |
> |
final CFException ex = new CFException(); |
1103 |
> |
|
1104 |
> |
f.completeExceptionally(ex); |
1105 |
> |
g.complete(v1); |
1106 |
> |
final CompletableFuture<Void> h = f.runAfterBoth(g, r); |
1107 |
> |
|
1108 |
> |
checkCompletedWithWrappedCFException(h, ex); |
1109 |
> |
checkCompletedWithWrappedCFException(f, ex); |
1110 |
|
assertFalse(r.ran); |
1111 |
+ |
checkCompletedNormally(g, v1); |
1112 |
+ |
} |
1113 |
|
} |
1114 |
|
|
1115 |
|
/** |
1116 |
|
* runAfterBoth result completes exceptionally if action does |
1117 |
|
*/ |
1118 |
< |
public void testRunAfterBoth3() { |
1119 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1120 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
1121 |
< |
FailingNoop r = new FailingNoop(); |
1122 |
< |
CompletableFuture<Void> g = f.runAfterBoth(f2, r); |
1123 |
< |
f.complete(one); |
1124 |
< |
checkIncomplete(g); |
1125 |
< |
f2.complete(two); |
1126 |
< |
checkCompletedWithWrappedCFException(g); |
1118 |
> |
public void testRunAfterBoth_actionFailed1() { |
1119 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1120 |
> |
for (Integer v2 : new Integer[] { 2, null }) { |
1121 |
> |
|
1122 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1123 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1124 |
> |
final FailingNoop r = new FailingNoop(); |
1125 |
> |
final CompletableFuture<Void> h = f.runAfterBoth(g, r); |
1126 |
> |
|
1127 |
> |
f.complete(v1); |
1128 |
> |
checkIncomplete(h); |
1129 |
> |
g.complete(v2); |
1130 |
> |
|
1131 |
> |
checkCompletedWithWrappedCFException(h); |
1132 |
> |
checkCompletedNormally(f, v1); |
1133 |
> |
checkCompletedNormally(g, v2); |
1134 |
> |
} |
1135 |
> |
} |
1136 |
> |
|
1137 |
> |
public void testRunAfterBoth_actionFailed2() { |
1138 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1139 |
> |
for (Integer v2 : new Integer[] { 2, null }) { |
1140 |
> |
|
1141 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1142 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1143 |
> |
final FailingNoop r = new FailingNoop(); |
1144 |
> |
final CompletableFuture<Void> h = f.runAfterBoth(g, r); |
1145 |
> |
|
1146 |
> |
g.complete(v2); |
1147 |
> |
checkIncomplete(h); |
1148 |
> |
f.complete(v1); |
1149 |
> |
|
1150 |
> |
checkCompletedWithWrappedCFException(h); |
1151 |
> |
checkCompletedNormally(f, v1); |
1152 |
> |
checkCompletedNormally(g, v2); |
1153 |
> |
} |
1154 |
|
} |
1155 |
|
|
1156 |
|
/** |
1157 |
|
* runAfterBoth result completes exceptionally if either source cancelled |
1158 |
|
*/ |
1159 |
< |
public void testRunAfterBoth4() { |
1160 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1161 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
1162 |
< |
Noop r = new Noop(); |
1163 |
< |
CompletableFuture<Void> g = f.runAfterBoth(f2, r); |
1164 |
< |
assertTrue(f.cancel(true)); |
1165 |
< |
f2.complete(two); |
1166 |
< |
checkCompletedWithWrappedCancellationException(g); |
1167 |
< |
f = new CompletableFuture<>(); |
1168 |
< |
f2 = new CompletableFuture<>(); |
1169 |
< |
r = new Noop(); |
1170 |
< |
g = f.runAfterBoth(f2, r); |
1171 |
< |
f.complete(one); |
1172 |
< |
assertTrue(f2.cancel(true)); |
1173 |
< |
checkCompletedWithWrappedCancellationException(g); |
1159 |
> |
public void testRunAfterBoth_sourceCancelled1() { |
1160 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1161 |
> |
for (Integer v1 : new Integer[] { 1, null }) { |
1162 |
> |
|
1163 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1164 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1165 |
> |
final Noop r = new Noop(); |
1166 |
> |
final CompletableFuture<Void> h = f.runAfterBoth(g, r); |
1167 |
> |
|
1168 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1169 |
> |
checkIncomplete(h); |
1170 |
> |
g.complete(v1); |
1171 |
> |
|
1172 |
> |
checkCompletedWithWrappedCancellationException(h); |
1173 |
> |
checkCancelled(f); |
1174 |
> |
assertFalse(r.ran); |
1175 |
> |
checkCompletedNormally(g, v1); |
1176 |
> |
} |
1177 |
> |
} |
1178 |
> |
|
1179 |
> |
public void testRunAfterBoth_sourceCancelled2() { |
1180 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1181 |
> |
for (Integer v1 : new Integer[] { 1, null }) { |
1182 |
> |
|
1183 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1184 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1185 |
> |
final Noop r = new Noop(); |
1186 |
> |
final CompletableFuture<Void> h = f.runAfterBoth(g, r); |
1187 |
> |
|
1188 |
> |
assertTrue(g.cancel(mayInterruptIfRunning)); |
1189 |
> |
checkIncomplete(h); |
1190 |
> |
f.complete(v1); |
1191 |
> |
|
1192 |
> |
checkCompletedWithWrappedCancellationException(h); |
1193 |
> |
checkCancelled(g); |
1194 |
> |
assertFalse(r.ran); |
1195 |
> |
checkCompletedNormally(f, v1); |
1196 |
> |
} |
1197 |
> |
} |
1198 |
> |
|
1199 |
> |
public void testRunAfterBoth_sourceCancelled3() { |
1200 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1201 |
> |
for (Integer v1 : new Integer[] { 1, null }) { |
1202 |
> |
|
1203 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1204 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1205 |
> |
final Noop r = new Noop(); |
1206 |
> |
|
1207 |
> |
assertTrue(g.cancel(mayInterruptIfRunning)); |
1208 |
> |
f.complete(v1); |
1209 |
> |
final CompletableFuture<Void> h = f.runAfterBoth(g, r); |
1210 |
> |
|
1211 |
> |
checkCompletedWithWrappedCancellationException(h); |
1212 |
> |
checkCancelled(g); |
1213 |
> |
assertFalse(r.ran); |
1214 |
> |
checkCompletedNormally(f, v1); |
1215 |
> |
} |
1216 |
> |
} |
1217 |
> |
|
1218 |
> |
public void testRunAfterBoth_sourceCancelled4() { |
1219 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1220 |
> |
for (Integer v1 : new Integer[] { 1, null }) { |
1221 |
> |
|
1222 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1223 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1224 |
> |
final Noop r = new Noop(); |
1225 |
> |
|
1226 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1227 |
> |
g.complete(v1); |
1228 |
> |
final CompletableFuture<Void> h = f.runAfterBoth(g, r); |
1229 |
> |
|
1230 |
> |
checkCompletedWithWrappedCancellationException(h); |
1231 |
> |
checkCancelled(f); |
1232 |
> |
assertFalse(r.ran); |
1233 |
> |
checkCompletedNormally(g, v1); |
1234 |
> |
} |
1235 |
|
} |
1236 |
|
|
1237 |
|
/** |