105 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
106 |
|
} |
107 |
|
|
108 |
< |
void checkCompletedWithWrappedCFException(CompletableFuture<?> f, |
109 |
< |
CFException ex) { |
108 |
> |
<U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f, |
109 |
> |
Throwable ex) { |
110 |
|
try { |
111 |
|
f.get(LONG_DELAY_MS, MILLISECONDS); |
112 |
|
shouldThrow(); |
131 |
|
} catch (ExecutionException success) { |
132 |
|
assertSame(ex, success.getCause()); |
133 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
134 |
+ |
|
135 |
|
assertTrue(f.isDone()); |
136 |
|
assertFalse(f.isCancelled()); |
137 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
138 |
|
} |
139 |
|
|
140 |
+ |
<U> void checkCompletedWithWrappedException(CompletableFuture<U> f, |
141 |
+ |
Throwable ex) { |
142 |
+ |
checkCompletedExceptionallyWithRootCause(f, ex); |
143 |
+ |
try { |
144 |
+ |
CompletableFuture<Throwable> spy = f.handle |
145 |
+ |
((U u, Throwable t) -> t); |
146 |
+ |
assertTrue(spy.join() instanceof CompletionException); |
147 |
+ |
assertSame(ex, spy.join().getCause()); |
148 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
149 |
+ |
} |
150 |
+ |
|
151 |
+ |
<U> void checkCompletedExceptionally(CompletableFuture<U> f, Throwable ex) { |
152 |
+ |
checkCompletedExceptionallyWithRootCause(f, ex); |
153 |
+ |
try { |
154 |
+ |
CompletableFuture<Throwable> spy = f.handle |
155 |
+ |
((U u, Throwable t) -> t); |
156 |
+ |
assertSame(ex, spy.join()); |
157 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
158 |
+ |
} |
159 |
+ |
|
160 |
|
void checkCancelled(CompletableFuture<?> f) { |
161 |
|
try { |
162 |
|
f.get(LONG_DELAY_MS, MILLISECONDS); |
239 |
|
*/ |
240 |
|
public void testCompleteExceptionally() { |
241 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
242 |
+ |
CFException ex = new CFException(); |
243 |
|
checkIncomplete(f); |
244 |
< |
f.completeExceptionally(new CFException()); |
245 |
< |
checkCompletedWithWrappedCFException(f); |
244 |
> |
f.completeExceptionally(ex); |
245 |
> |
checkCompletedExceptionally(f, ex); |
246 |
|
} |
247 |
|
|
248 |
|
/** |
283 |
|
* obtrudeException forces completion with given exception |
284 |
|
*/ |
285 |
|
public void testObtrudeException() { |
286 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
287 |
< |
checkIncomplete(f); |
288 |
< |
f.complete(one); |
289 |
< |
checkCompletedNormally(f, one); |
290 |
< |
f.obtrudeException(new CFException()); |
269 |
< |
checkCompletedWithWrappedCFException(f); |
286 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
287 |
> |
{ |
288 |
> |
CFException ex; |
289 |
> |
CompletableFuture<Integer> f; |
290 |
> |
|
291 |
|
f = new CompletableFuture<>(); |
292 |
< |
f.obtrudeException(new CFException()); |
293 |
< |
checkCompletedWithWrappedCFException(f); |
292 |
> |
f.complete(v1); |
293 |
> |
for (int i = 0; i < 2; i++) { |
294 |
> |
f.obtrudeException(ex = new CFException()); |
295 |
> |
checkCompletedExceptionally(f, ex); |
296 |
> |
} |
297 |
> |
|
298 |
> |
f = new CompletableFuture<>(); |
299 |
> |
for (int i = 0; i < 2; i++) { |
300 |
> |
f.obtrudeException(ex = new CFException()); |
301 |
> |
checkCompletedExceptionally(f, ex); |
302 |
> |
} |
303 |
> |
|
304 |
|
f = new CompletableFuture<>(); |
305 |
+ |
f.completeExceptionally(ex = new CFException()); |
306 |
+ |
f.obtrudeValue(v1); |
307 |
+ |
checkCompletedNormally(f, v1); |
308 |
+ |
f.obtrudeException(ex = new CFException()); |
309 |
+ |
checkCompletedExceptionally(f, ex); |
310 |
|
f.completeExceptionally(new CFException()); |
311 |
< |
f.obtrudeValue(four); |
312 |
< |
checkCompletedNormally(f, four); |
313 |
< |
f.obtrudeException(new CFException()); |
314 |
< |
checkCompletedWithWrappedCFException(f); |
279 |
< |
} |
311 |
> |
checkCompletedExceptionally(f, ex); |
312 |
> |
f.complete(v1); |
313 |
> |
checkCompletedExceptionally(f, ex); |
314 |
> |
}} |
315 |
|
|
316 |
|
/** |
317 |
|
* getNumberOfDependents returns number of dependent tasks |
318 |
|
*/ |
319 |
|
public void testGetNumberOfDependents() { |
320 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
321 |
+ |
{ |
322 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
323 |
|
assertEquals(0, f.getNumberOfDependents()); |
324 |
< |
CompletableFuture g = f.thenRun(new Noop(ExecutionMode.DEFAULT)); |
324 |
> |
final CompletableFuture<Void> g = m.thenRun(f, new Noop(m)); |
325 |
|
assertEquals(1, f.getNumberOfDependents()); |
326 |
|
assertEquals(0, g.getNumberOfDependents()); |
327 |
< |
CompletableFuture h = f.thenRun(new Noop(ExecutionMode.DEFAULT)); |
327 |
> |
final CompletableFuture<Void> h = m.thenRun(f, new Noop(m)); |
328 |
|
assertEquals(2, f.getNumberOfDependents()); |
329 |
+ |
assertEquals(0, h.getNumberOfDependents()); |
330 |
|
f.complete(1); |
331 |
|
checkCompletedNormally(g, null); |
332 |
+ |
checkCompletedNormally(h, null); |
333 |
|
assertEquals(0, f.getNumberOfDependents()); |
334 |
|
assertEquals(0, g.getNumberOfDependents()); |
335 |
< |
} |
335 |
> |
assertEquals(0, h.getNumberOfDependents()); |
336 |
> |
}} |
337 |
|
|
338 |
|
/** |
339 |
|
* toString indicates current completion state |
350 |
|
f = new CompletableFuture<String>(); |
351 |
|
f.completeExceptionally(new IndexOutOfBoundsException()); |
352 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
353 |
+ |
|
354 |
+ |
f = new CompletableFuture<String>(); |
355 |
+ |
f.cancel(true); |
356 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
357 |
+ |
|
358 |
+ |
f = new CompletableFuture<String>(); |
359 |
+ |
f.cancel(false); |
360 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
361 |
|
} |
362 |
|
|
363 |
|
/** |
856 |
|
assertEquals(0, a.get()); |
857 |
|
}} |
858 |
|
|
811 |
– |
|
859 |
|
/** |
860 |
|
* exceptionally action completes with function value on source |
861 |
|
* exception |
899 |
|
}); |
900 |
|
if (createIncomplete) f.completeExceptionally(ex1); |
901 |
|
|
902 |
< |
checkCompletedWithWrappedCFException(g, ex2); |
902 |
> |
checkCompletedWithWrappedException(g, ex2); |
903 |
> |
assertEquals(1, a.get()); |
904 |
> |
}} |
905 |
> |
|
906 |
> |
/** |
907 |
> |
* whenComplete action executes on normal completion, propagating |
908 |
> |
* source result. |
909 |
> |
*/ |
910 |
> |
public void testWhenComplete_normalCompletion1() { |
911 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
912 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
913 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
914 |
> |
{ |
915 |
> |
final AtomicInteger a = new AtomicInteger(0); |
916 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
917 |
> |
if (!createIncomplete) f.complete(v1); |
918 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
919 |
> |
(f, |
920 |
> |
(Integer x, Throwable t) -> { |
921 |
> |
m.checkExecutionMode(); |
922 |
> |
threadAssertSame(x, v1); |
923 |
> |
threadAssertNull(t); |
924 |
> |
a.getAndIncrement(); |
925 |
> |
}); |
926 |
> |
if (createIncomplete) f.complete(v1); |
927 |
> |
|
928 |
> |
checkCompletedNormally(g, v1); |
929 |
> |
checkCompletedNormally(f, v1); |
930 |
> |
assertEquals(1, a.get()); |
931 |
> |
}} |
932 |
> |
|
933 |
> |
/** |
934 |
> |
* whenComplete action executes on exceptional completion, propagating |
935 |
> |
* source result. |
936 |
> |
*/ |
937 |
> |
public void testWhenComplete_exceptionalCompletion() { |
938 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
939 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
940 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
941 |
> |
{ |
942 |
> |
final AtomicInteger a = new AtomicInteger(0); |
943 |
> |
final CFException ex = new CFException(); |
944 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
945 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
946 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
947 |
> |
(f, |
948 |
> |
(Integer x, Throwable t) -> { |
949 |
> |
m.checkExecutionMode(); |
950 |
> |
threadAssertNull(x); |
951 |
> |
threadAssertSame(t, ex); |
952 |
> |
a.getAndIncrement(); |
953 |
> |
}); |
954 |
> |
if (createIncomplete) f.completeExceptionally(ex); |
955 |
> |
|
956 |
> |
checkCompletedWithWrappedException(g, ex); |
957 |
> |
checkCompletedExceptionally(f, ex); |
958 |
> |
assertEquals(1, a.get()); |
959 |
> |
}} |
960 |
> |
|
961 |
> |
/** |
962 |
> |
* whenComplete action executes on cancelled source, propagating |
963 |
> |
* CancellationException. |
964 |
> |
*/ |
965 |
> |
public void testWhenComplete_sourceCancelled() { |
966 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
967 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
968 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
969 |
> |
{ |
970 |
> |
final AtomicInteger a = new AtomicInteger(0); |
971 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
972 |
> |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
973 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
974 |
> |
(f, |
975 |
> |
(Integer x, Throwable t) -> { |
976 |
> |
m.checkExecutionMode(); |
977 |
> |
threadAssertNull(x); |
978 |
> |
threadAssertTrue(t instanceof CancellationException); |
979 |
> |
a.getAndIncrement(); |
980 |
> |
}); |
981 |
> |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
982 |
> |
|
983 |
> |
checkCompletedWithWrappedCancellationException(g); |
984 |
> |
checkCancelled(f); |
985 |
> |
assertEquals(1, a.get()); |
986 |
> |
}} |
987 |
> |
|
988 |
> |
/** |
989 |
> |
* If a whenComplete action throws an exception when triggered by |
990 |
> |
* a normal completion, it completes exceptionally |
991 |
> |
*/ |
992 |
> |
public void testWhenComplete_actionFailed() { |
993 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
994 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
995 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
996 |
> |
{ |
997 |
> |
final AtomicInteger a = new AtomicInteger(0); |
998 |
> |
final CFException ex = new CFException(); |
999 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1000 |
> |
if (!createIncomplete) f.complete(v1); |
1001 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
1002 |
> |
(f, |
1003 |
> |
(Integer x, Throwable t) -> { |
1004 |
> |
m.checkExecutionMode(); |
1005 |
> |
threadAssertSame(x, v1); |
1006 |
> |
threadAssertNull(t); |
1007 |
> |
a.getAndIncrement(); |
1008 |
> |
throw ex; |
1009 |
> |
}); |
1010 |
> |
if (createIncomplete) f.complete(v1); |
1011 |
> |
|
1012 |
> |
checkCompletedWithWrappedException(g, ex); |
1013 |
> |
checkCompletedNormally(f, v1); |
1014 |
> |
assertEquals(1, a.get()); |
1015 |
> |
}} |
1016 |
> |
|
1017 |
> |
/** |
1018 |
> |
* If a whenComplete action throws an exception when triggered by |
1019 |
> |
* a source completion that also throws an exception, the source |
1020 |
> |
* exception takes precedence. |
1021 |
> |
*/ |
1022 |
> |
public void testWhenComplete_actionFailedSourceFailed() { |
1023 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1024 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1025 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1026 |
> |
{ |
1027 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1028 |
> |
final CFException ex1 = new CFException(); |
1029 |
> |
final CFException ex2 = new CFException(); |
1030 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1031 |
> |
|
1032 |
> |
if (!createIncomplete) f.completeExceptionally(ex1); |
1033 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
1034 |
> |
(f, |
1035 |
> |
(Integer x, Throwable t) -> { |
1036 |
> |
m.checkExecutionMode(); |
1037 |
> |
threadAssertSame(t, ex1); |
1038 |
> |
threadAssertNull(x); |
1039 |
> |
a.getAndIncrement(); |
1040 |
> |
throw ex2; |
1041 |
> |
}); |
1042 |
> |
if (createIncomplete) f.completeExceptionally(ex1); |
1043 |
> |
|
1044 |
> |
checkCompletedWithWrappedException(g, ex1); |
1045 |
> |
checkCompletedExceptionally(f, ex1); |
1046 |
|
assertEquals(1, a.get()); |
1047 |
|
}} |
1048 |
|
|
1099 |
|
if (createIncomplete) f.completeExceptionally(ex); |
1100 |
|
|
1101 |
|
checkCompletedNormally(g, v1); |
1102 |
< |
checkCompletedWithWrappedCFException(f, ex); |
1102 |
> |
checkCompletedExceptionally(f, ex); |
1103 |
|
assertEquals(1, a.get()); |
1104 |
|
}} |
1105 |
|
|
1155 |
|
}); |
1156 |
|
if (createIncomplete) f.completeExceptionally(ex1); |
1157 |
|
|
1158 |
< |
checkCompletedWithWrappedCFException(g, ex2); |
1159 |
< |
checkCompletedWithWrappedCFException(f, ex1); |
1158 |
> |
checkCompletedWithWrappedException(g, ex2); |
1159 |
> |
checkCompletedExceptionally(f, ex1); |
1160 |
|
assertEquals(1, a.get()); |
1161 |
|
}} |
1162 |
|
|
1180 |
|
}); |
1181 |
|
if (createIncomplete) f.complete(v1); |
1182 |
|
|
1183 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1183 |
> |
checkCompletedWithWrappedException(g, ex); |
1184 |
|
checkCompletedNormally(f, v1); |
1185 |
|
assertEquals(1, a.get()); |
1186 |
|
}} |
1294 |
|
f.completeExceptionally(ex); |
1295 |
|
} |
1296 |
|
|
1297 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1298 |
< |
checkCompletedWithWrappedCFException(f, ex); |
1297 |
> |
checkCompletedWithWrappedException(g, ex); |
1298 |
> |
checkCompletedExceptionally(f, ex); |
1299 |
|
r.assertNotInvoked(); |
1300 |
|
}} |
1301 |
|
|
1382 |
|
f.completeExceptionally(ex); |
1383 |
|
} |
1384 |
|
|
1385 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1386 |
< |
checkCompletedWithWrappedCFException(f, ex); |
1385 |
> |
checkCompletedWithWrappedException(g, ex); |
1386 |
> |
checkCompletedExceptionally(f, ex); |
1387 |
|
r.assertNotInvoked(); |
1388 |
|
}} |
1389 |
|
|
1470 |
|
f.completeExceptionally(ex); |
1471 |
|
} |
1472 |
|
|
1473 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1474 |
< |
checkCompletedWithWrappedCFException(f, ex); |
1473 |
> |
checkCompletedWithWrappedException(g, ex); |
1474 |
> |
checkCompletedExceptionally(f, ex); |
1475 |
|
r.assertNotInvoked(); |
1476 |
|
}} |
1477 |
|
|
1573 |
|
(!fFirst ? f : g).completeExceptionally(ex); |
1574 |
|
} |
1575 |
|
|
1576 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1576 |
> |
checkCompletedWithWrappedException(h, ex); |
1577 |
|
r.assertNotInvoked(); |
1578 |
|
checkCompletedNormally(fFirst ? f : g, v1); |
1579 |
< |
checkCompletedWithWrappedCFException(!fFirst ? f : g, ex); |
1579 |
> |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
1580 |
|
}} |
1581 |
|
|
1582 |
|
/** |
1690 |
|
(!fFirst ? f : g).completeExceptionally(ex); |
1691 |
|
} |
1692 |
|
|
1693 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1693 |
> |
checkCompletedWithWrappedException(h, ex); |
1694 |
|
r.assertNotInvoked(); |
1695 |
|
checkCompletedNormally(fFirst ? f : g, v1); |
1696 |
< |
checkCompletedWithWrappedCFException(!fFirst ? f : g, ex); |
1696 |
> |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
1697 |
|
}} |
1698 |
|
|
1699 |
|
/** |
1807 |
|
(!fFirst ? f : g).completeExceptionally(ex); |
1808 |
|
} |
1809 |
|
|
1810 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1810 |
> |
checkCompletedWithWrappedException(h, ex); |
1811 |
|
r.assertNotInvoked(); |
1812 |
|
checkCompletedNormally(fFirst ? f : g, v1); |
1813 |
< |
checkCompletedWithWrappedCFException(!fFirst ? f : g, ex); |
1813 |
> |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
1814 |
|
}} |
1815 |
|
|
1816 |
|
/** |
1827 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1828 |
|
final Noop r = new Noop(m); |
1829 |
|
|
1640 |
– |
|
1830 |
|
(fFirst ? f : g).complete(v1); |
1831 |
|
if (!createIncomplete) |
1832 |
|
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1941 |
|
rs[0].assertNotInvoked(); |
1942 |
|
rs[1].assertNotInvoked(); |
1943 |
|
f.completeExceptionally(ex); |
1944 |
< |
checkCompletedWithWrappedCFException(h0, ex); |
1945 |
< |
checkCompletedWithWrappedCFException(h1, ex); |
1944 |
> |
checkCompletedWithWrappedException(h0, ex); |
1945 |
> |
checkCompletedWithWrappedException(h1, ex); |
1946 |
|
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
1947 |
|
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
1948 |
< |
checkCompletedWithWrappedCFException(h2, ex); |
1949 |
< |
checkCompletedWithWrappedCFException(h3, ex); |
1948 |
> |
checkCompletedWithWrappedException(h2, ex); |
1949 |
> |
checkCompletedWithWrappedException(h3, ex); |
1950 |
|
g.complete(v1); |
1951 |
|
|
1952 |
|
// unspecified behavior - both source completions available |
1956 |
|
assertEquals(inc(v1), h4.join()); |
1957 |
|
rs[4].assertValue(inc(v1)); |
1958 |
|
} catch (CompletionException ok) { |
1959 |
< |
checkCompletedWithWrappedCFException(h4, ex); |
1959 |
> |
checkCompletedWithWrappedException(h4, ex); |
1960 |
|
rs[4].assertNotInvoked(); |
1961 |
|
} |
1962 |
|
try { |
1963 |
|
assertEquals(inc(v1), h5.join()); |
1964 |
|
rs[5].assertValue(inc(v1)); |
1965 |
|
} catch (CompletionException ok) { |
1966 |
< |
checkCompletedWithWrappedCFException(h5, ex); |
1966 |
> |
checkCompletedWithWrappedException(h5, ex); |
1967 |
|
rs[5].assertNotInvoked(); |
1968 |
|
} |
1969 |
|
|
1970 |
< |
checkCompletedWithWrappedCFException(f, ex); |
1970 |
> |
checkCompletedExceptionally(f, ex); |
1971 |
|
checkCompletedNormally(g, v1); |
1972 |
< |
checkCompletedWithWrappedCFException(h0, ex); |
1973 |
< |
checkCompletedWithWrappedCFException(h1, ex); |
1974 |
< |
checkCompletedWithWrappedCFException(h2, ex); |
1975 |
< |
checkCompletedWithWrappedCFException(h3, ex); |
1976 |
< |
checkCompletedWithWrappedCFException(h4, ex); |
1972 |
> |
checkCompletedWithWrappedException(h0, ex); |
1973 |
> |
checkCompletedWithWrappedException(h1, ex); |
1974 |
> |
checkCompletedWithWrappedException(h2, ex); |
1975 |
> |
checkCompletedWithWrappedException(h3, ex); |
1976 |
> |
checkCompletedWithWrappedException(h4, ex); |
1977 |
|
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
1978 |
|
}} |
1979 |
|
|
2005 |
|
assertEquals(inc(v1), h0.join()); |
2006 |
|
rs[0].assertValue(inc(v1)); |
2007 |
|
} catch (CompletionException ok) { |
2008 |
< |
checkCompletedWithWrappedCFException(h0, ex); |
2008 |
> |
checkCompletedWithWrappedException(h0, ex); |
2009 |
|
rs[0].assertNotInvoked(); |
2010 |
|
} |
2011 |
|
try { |
2012 |
|
assertEquals(inc(v1), h1.join()); |
2013 |
|
rs[1].assertValue(inc(v1)); |
2014 |
|
} catch (CompletionException ok) { |
2015 |
< |
checkCompletedWithWrappedCFException(h1, ex); |
2015 |
> |
checkCompletedWithWrappedException(h1, ex); |
2016 |
|
rs[1].assertNotInvoked(); |
2017 |
|
} |
2018 |
|
try { |
2019 |
|
assertEquals(inc(v1), h2.join()); |
2020 |
|
rs[2].assertValue(inc(v1)); |
2021 |
|
} catch (CompletionException ok) { |
2022 |
< |
checkCompletedWithWrappedCFException(h2, ex); |
2022 |
> |
checkCompletedWithWrappedException(h2, ex); |
2023 |
|
rs[2].assertNotInvoked(); |
2024 |
|
} |
2025 |
|
try { |
2026 |
|
assertEquals(inc(v1), h3.join()); |
2027 |
|
rs[3].assertValue(inc(v1)); |
2028 |
|
} catch (CompletionException ok) { |
2029 |
< |
checkCompletedWithWrappedCFException(h3, ex); |
2029 |
> |
checkCompletedWithWrappedException(h3, ex); |
2030 |
|
rs[3].assertNotInvoked(); |
2031 |
|
} |
2032 |
|
|
2033 |
|
checkCompletedNormally(f, v1); |
2034 |
< |
checkCompletedWithWrappedCFException(g, ex); |
2034 |
> |
checkCompletedExceptionally(g, ex); |
2035 |
|
}} |
2036 |
|
|
2037 |
|
/** |
2260 |
|
rs[0].assertNotInvoked(); |
2261 |
|
rs[1].assertNotInvoked(); |
2262 |
|
f.completeExceptionally(ex); |
2263 |
< |
checkCompletedWithWrappedCFException(h0, ex); |
2264 |
< |
checkCompletedWithWrappedCFException(h1, ex); |
2263 |
> |
checkCompletedWithWrappedException(h0, ex); |
2264 |
> |
checkCompletedWithWrappedException(h1, ex); |
2265 |
|
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2266 |
|
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2267 |
< |
checkCompletedWithWrappedCFException(h2, ex); |
2268 |
< |
checkCompletedWithWrappedCFException(h3, ex); |
2267 |
> |
checkCompletedWithWrappedException(h2, ex); |
2268 |
> |
checkCompletedWithWrappedException(h3, ex); |
2269 |
|
|
2270 |
|
g.complete(v1); |
2271 |
|
|
2276 |
|
assertNull(h4.join()); |
2277 |
|
rs[4].assertValue(v1); |
2278 |
|
} catch (CompletionException ok) { |
2279 |
< |
checkCompletedWithWrappedCFException(h4, ex); |
2279 |
> |
checkCompletedWithWrappedException(h4, ex); |
2280 |
|
rs[4].assertNotInvoked(); |
2281 |
|
} |
2282 |
|
try { |
2283 |
|
assertNull(h5.join()); |
2284 |
|
rs[5].assertValue(v1); |
2285 |
|
} catch (CompletionException ok) { |
2286 |
< |
checkCompletedWithWrappedCFException(h5, ex); |
2286 |
> |
checkCompletedWithWrappedException(h5, ex); |
2287 |
|
rs[5].assertNotInvoked(); |
2288 |
|
} |
2289 |
|
|
2290 |
< |
checkCompletedWithWrappedCFException(f, ex); |
2290 |
> |
checkCompletedExceptionally(f, ex); |
2291 |
|
checkCompletedNormally(g, v1); |
2292 |
< |
checkCompletedWithWrappedCFException(h0, ex); |
2293 |
< |
checkCompletedWithWrappedCFException(h1, ex); |
2294 |
< |
checkCompletedWithWrappedCFException(h2, ex); |
2295 |
< |
checkCompletedWithWrappedCFException(h3, ex); |
2296 |
< |
checkCompletedWithWrappedCFException(h4, ex); |
2292 |
> |
checkCompletedWithWrappedException(h0, ex); |
2293 |
> |
checkCompletedWithWrappedException(h1, ex); |
2294 |
> |
checkCompletedWithWrappedException(h2, ex); |
2295 |
> |
checkCompletedWithWrappedException(h3, ex); |
2296 |
> |
checkCompletedWithWrappedException(h4, ex); |
2297 |
|
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2298 |
|
}} |
2299 |
|
|
2325 |
|
assertEquals(null, h0.join()); |
2326 |
|
rs[0].assertValue(v1); |
2327 |
|
} catch (CompletionException ok) { |
2328 |
< |
checkCompletedWithWrappedCFException(h0, ex); |
2328 |
> |
checkCompletedWithWrappedException(h0, ex); |
2329 |
|
rs[0].assertNotInvoked(); |
2330 |
|
} |
2331 |
|
try { |
2332 |
|
assertEquals(null, h1.join()); |
2333 |
|
rs[1].assertValue(v1); |
2334 |
|
} catch (CompletionException ok) { |
2335 |
< |
checkCompletedWithWrappedCFException(h1, ex); |
2335 |
> |
checkCompletedWithWrappedException(h1, ex); |
2336 |
|
rs[1].assertNotInvoked(); |
2337 |
|
} |
2338 |
|
try { |
2339 |
|
assertEquals(null, h2.join()); |
2340 |
|
rs[2].assertValue(v1); |
2341 |
|
} catch (CompletionException ok) { |
2342 |
< |
checkCompletedWithWrappedCFException(h2, ex); |
2342 |
> |
checkCompletedWithWrappedException(h2, ex); |
2343 |
|
rs[2].assertNotInvoked(); |
2344 |
|
} |
2345 |
|
try { |
2346 |
|
assertEquals(null, h3.join()); |
2347 |
|
rs[3].assertValue(v1); |
2348 |
|
} catch (CompletionException ok) { |
2349 |
< |
checkCompletedWithWrappedCFException(h3, ex); |
2349 |
> |
checkCompletedWithWrappedException(h3, ex); |
2350 |
|
rs[3].assertNotInvoked(); |
2351 |
|
} |
2352 |
|
|
2353 |
|
checkCompletedNormally(f, v1); |
2354 |
< |
checkCompletedWithWrappedCFException(g, ex); |
2354 |
> |
checkCompletedExceptionally(g, ex); |
2355 |
|
}} |
2356 |
|
|
2357 |
|
/** |
2520 |
|
rs[0].assertNotInvoked(); |
2521 |
|
rs[1].assertNotInvoked(); |
2522 |
|
f.completeExceptionally(ex); |
2523 |
< |
checkCompletedWithWrappedCFException(h0, ex); |
2524 |
< |
checkCompletedWithWrappedCFException(h1, ex); |
2523 |
> |
checkCompletedWithWrappedException(h0, ex); |
2524 |
> |
checkCompletedWithWrappedException(h1, ex); |
2525 |
|
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2526 |
|
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2527 |
< |
checkCompletedWithWrappedCFException(h2, ex); |
2528 |
< |
checkCompletedWithWrappedCFException(h3, ex); |
2527 |
> |
checkCompletedWithWrappedException(h2, ex); |
2528 |
> |
checkCompletedWithWrappedException(h3, ex); |
2529 |
|
|
2530 |
|
g.complete(v1); |
2531 |
|
|
2536 |
|
assertNull(h4.join()); |
2537 |
|
rs[4].assertInvoked(); |
2538 |
|
} catch (CompletionException ok) { |
2539 |
< |
checkCompletedWithWrappedCFException(h4, ex); |
2539 |
> |
checkCompletedWithWrappedException(h4, ex); |
2540 |
|
rs[4].assertNotInvoked(); |
2541 |
|
} |
2542 |
|
try { |
2543 |
|
assertNull(h5.join()); |
2544 |
|
rs[5].assertInvoked(); |
2545 |
|
} catch (CompletionException ok) { |
2546 |
< |
checkCompletedWithWrappedCFException(h5, ex); |
2546 |
> |
checkCompletedWithWrappedException(h5, ex); |
2547 |
|
rs[5].assertNotInvoked(); |
2548 |
|
} |
2549 |
|
|
2550 |
< |
checkCompletedWithWrappedCFException(f, ex); |
2550 |
> |
checkCompletedExceptionally(f, ex); |
2551 |
|
checkCompletedNormally(g, v1); |
2552 |
< |
checkCompletedWithWrappedCFException(h0, ex); |
2553 |
< |
checkCompletedWithWrappedCFException(h1, ex); |
2554 |
< |
checkCompletedWithWrappedCFException(h2, ex); |
2555 |
< |
checkCompletedWithWrappedCFException(h3, ex); |
2556 |
< |
checkCompletedWithWrappedCFException(h4, ex); |
2552 |
> |
checkCompletedWithWrappedException(h0, ex); |
2553 |
> |
checkCompletedWithWrappedException(h1, ex); |
2554 |
> |
checkCompletedWithWrappedException(h2, ex); |
2555 |
> |
checkCompletedWithWrappedException(h3, ex); |
2556 |
> |
checkCompletedWithWrappedException(h4, ex); |
2557 |
|
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2558 |
|
}} |
2559 |
|
|
2585 |
|
assertEquals(null, h0.join()); |
2586 |
|
rs[0].assertInvoked(); |
2587 |
|
} catch (CompletionException ok) { |
2588 |
< |
checkCompletedWithWrappedCFException(h0, ex); |
2588 |
> |
checkCompletedWithWrappedException(h0, ex); |
2589 |
|
rs[0].assertNotInvoked(); |
2590 |
|
} |
2591 |
|
try { |
2592 |
|
assertEquals(null, h1.join()); |
2593 |
|
rs[1].assertInvoked(); |
2594 |
|
} catch (CompletionException ok) { |
2595 |
< |
checkCompletedWithWrappedCFException(h1, ex); |
2595 |
> |
checkCompletedWithWrappedException(h1, ex); |
2596 |
|
rs[1].assertNotInvoked(); |
2597 |
|
} |
2598 |
|
try { |
2599 |
|
assertEquals(null, h2.join()); |
2600 |
|
rs[2].assertInvoked(); |
2601 |
|
} catch (CompletionException ok) { |
2602 |
< |
checkCompletedWithWrappedCFException(h2, ex); |
2602 |
> |
checkCompletedWithWrappedException(h2, ex); |
2603 |
|
rs[2].assertNotInvoked(); |
2604 |
|
} |
2605 |
|
try { |
2606 |
|
assertEquals(null, h3.join()); |
2607 |
|
rs[3].assertInvoked(); |
2608 |
|
} catch (CompletionException ok) { |
2609 |
< |
checkCompletedWithWrappedCFException(h3, ex); |
2609 |
> |
checkCompletedWithWrappedException(h3, ex); |
2610 |
|
rs[3].assertNotInvoked(); |
2611 |
|
} |
2612 |
|
|
2613 |
|
checkCompletedNormally(f, v1); |
2614 |
< |
checkCompletedWithWrappedCFException(g, ex); |
2614 |
> |
checkCompletedExceptionally(g, ex); |
2615 |
|
}} |
2616 |
|
|
2617 |
|
/** |
2738 |
|
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2739 |
|
if (createIncomplete) f.completeExceptionally(ex); |
2740 |
|
|
2741 |
< |
checkCompletedWithWrappedCFException(g, ex); |
2742 |
< |
checkCompletedWithWrappedCFException(f, ex); |
2741 |
> |
checkCompletedWithWrappedException(g, ex); |
2742 |
> |
checkCompletedExceptionally(f, ex); |
2743 |
|
r.assertNotInvoked(); |
2744 |
|
}} |
2745 |
|
|
2995 |
|
assertSame(f, f.toCompletableFuture()); |
2996 |
|
} |
2997 |
|
|
2809 |
– |
/** |
2810 |
– |
* whenComplete action executes on normal completion, propagating |
2811 |
– |
* source result. |
2812 |
– |
*/ |
2813 |
– |
public void testWhenComplete_normalCompletion1() { |
2814 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2815 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2816 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2817 |
– |
{ |
2818 |
– |
final AtomicInteger a = new AtomicInteger(0); |
2819 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2820 |
– |
if (!createIncomplete) f.complete(v1); |
2821 |
– |
final CompletableFuture<Integer> g = m.whenComplete |
2822 |
– |
(f, |
2823 |
– |
(Integer x, Throwable t) -> { |
2824 |
– |
m.checkExecutionMode(); |
2825 |
– |
threadAssertSame(x, v1); |
2826 |
– |
threadAssertNull(t); |
2827 |
– |
a.getAndIncrement(); |
2828 |
– |
}); |
2829 |
– |
if (createIncomplete) f.complete(v1); |
2830 |
– |
|
2831 |
– |
checkCompletedNormally(g, v1); |
2832 |
– |
checkCompletedNormally(f, v1); |
2833 |
– |
assertEquals(1, a.get()); |
2834 |
– |
}} |
2835 |
– |
|
2836 |
– |
/** |
2837 |
– |
* whenComplete action executes on exceptional completion, propagating |
2838 |
– |
* source result. |
2839 |
– |
*/ |
2840 |
– |
public void testWhenComplete_exceptionalCompletion() { |
2841 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2842 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2843 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2844 |
– |
{ |
2845 |
– |
final AtomicInteger a = new AtomicInteger(0); |
2846 |
– |
final CFException ex = new CFException(); |
2847 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2848 |
– |
if (!createIncomplete) f.completeExceptionally(ex); |
2849 |
– |
final CompletableFuture<Integer> g = m.whenComplete |
2850 |
– |
(f, |
2851 |
– |
(Integer x, Throwable t) -> { |
2852 |
– |
m.checkExecutionMode(); |
2853 |
– |
threadAssertNull(x); |
2854 |
– |
threadAssertSame(t, ex); |
2855 |
– |
a.getAndIncrement(); |
2856 |
– |
}); |
2857 |
– |
if (createIncomplete) f.completeExceptionally(ex); |
2858 |
– |
checkCompletedWithWrappedCFException(f, ex); |
2859 |
– |
checkCompletedWithWrappedCFException(g, ex); |
2860 |
– |
assertEquals(1, a.get()); |
2861 |
– |
}} |
2862 |
– |
|
2863 |
– |
/** |
2864 |
– |
* whenComplete action executes on cancelled source, propagating |
2865 |
– |
* CancellationException. |
2866 |
– |
*/ |
2867 |
– |
public void testWhenComplete_sourceCancelled() { |
2868 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2869 |
– |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2870 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2871 |
– |
{ |
2872 |
– |
final AtomicInteger a = new AtomicInteger(0); |
2873 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2874 |
– |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
2875 |
– |
final CompletableFuture<Integer> g = m.whenComplete |
2876 |
– |
(f, |
2877 |
– |
(Integer x, Throwable t) -> { |
2878 |
– |
m.checkExecutionMode(); |
2879 |
– |
threadAssertNull(x); |
2880 |
– |
threadAssertTrue(t instanceof CancellationException); |
2881 |
– |
a.getAndIncrement(); |
2882 |
– |
}); |
2883 |
– |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
2884 |
– |
|
2885 |
– |
checkCompletedWithWrappedCancellationException(g); |
2886 |
– |
checkCancelled(f); |
2887 |
– |
assertEquals(1, a.get()); |
2888 |
– |
}} |
2889 |
– |
|
2890 |
– |
/** |
2891 |
– |
* If a whenComplete action throws an exception when triggered by |
2892 |
– |
* a normal completion, it completes exceptionally |
2893 |
– |
*/ |
2894 |
– |
public void testWhenComplete_actionFailed() { |
2895 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2896 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2897 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2898 |
– |
{ |
2899 |
– |
final AtomicInteger a = new AtomicInteger(0); |
2900 |
– |
final CFException ex = new CFException(); |
2901 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2902 |
– |
if (!createIncomplete) f.complete(v1); |
2903 |
– |
final CompletableFuture<Integer> g = m.whenComplete |
2904 |
– |
(f, |
2905 |
– |
(Integer x, Throwable t) -> { |
2906 |
– |
m.checkExecutionMode(); |
2907 |
– |
threadAssertSame(x, v1); |
2908 |
– |
threadAssertNull(t); |
2909 |
– |
a.getAndIncrement(); |
2910 |
– |
throw ex; |
2911 |
– |
}); |
2912 |
– |
if (createIncomplete) f.complete(v1); |
2913 |
– |
checkCompletedNormally(f, v1); |
2914 |
– |
checkCompletedWithWrappedCFException(g, ex); |
2915 |
– |
assertEquals(1, a.get()); |
2916 |
– |
}} |
2917 |
– |
|
2918 |
– |
/** |
2919 |
– |
* If a whenComplete action throws an exception when triggered by |
2920 |
– |
* a source completion that also throws an exception, the source |
2921 |
– |
* exception takes precedence. |
2922 |
– |
*/ |
2923 |
– |
public void testWhenComplete_actionFailedSourceFailed() { |
2924 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2925 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2926 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2927 |
– |
{ |
2928 |
– |
final AtomicInteger a = new AtomicInteger(0); |
2929 |
– |
final CFException ex1 = new CFException(); |
2930 |
– |
final CFException ex2 = new CFException(); |
2931 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2932 |
– |
|
2933 |
– |
if (!createIncomplete) f.completeExceptionally(ex1); |
2934 |
– |
final CompletableFuture<Integer> g = m.whenComplete |
2935 |
– |
(f, |
2936 |
– |
(Integer x, Throwable t) -> { |
2937 |
– |
m.checkExecutionMode(); |
2938 |
– |
threadAssertSame(t, ex1); |
2939 |
– |
threadAssertNull(x); |
2940 |
– |
a.getAndIncrement(); |
2941 |
– |
throw ex2; |
2942 |
– |
}); |
2943 |
– |
if (createIncomplete) f.completeExceptionally(ex1); |
2944 |
– |
|
2945 |
– |
checkCompletedWithWrappedCFException(f, ex1); |
2946 |
– |
checkCompletedWithWrappedCFException(g, ex1); |
2947 |
– |
assertEquals(1, a.get()); |
2948 |
– |
}} |
2949 |
– |
|
2998 |
|
} |