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 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
355 |
+ |
f = new CompletableFuture<String>(); |
356 |
+ |
f.cancel(mayInterruptIfRunning); |
357 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
358 |
+ |
} |
359 |
|
} |
360 |
|
|
361 |
|
/** |
854 |
|
assertEquals(0, a.get()); |
855 |
|
}} |
856 |
|
|
811 |
– |
|
857 |
|
/** |
858 |
|
* exceptionally action completes with function value on source |
859 |
|
* exception |
897 |
|
}); |
898 |
|
if (createIncomplete) f.completeExceptionally(ex1); |
899 |
|
|
900 |
< |
checkCompletedWithWrappedCFException(g, ex2); |
900 |
> |
checkCompletedWithWrappedException(g, ex2); |
901 |
> |
assertEquals(1, a.get()); |
902 |
> |
}} |
903 |
> |
|
904 |
> |
/** |
905 |
> |
* whenComplete action executes on normal completion, propagating |
906 |
> |
* source result. |
907 |
> |
*/ |
908 |
> |
public void testWhenComplete_normalCompletion1() { |
909 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
910 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
911 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
912 |
> |
{ |
913 |
> |
final AtomicInteger a = new AtomicInteger(0); |
914 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
915 |
> |
if (!createIncomplete) f.complete(v1); |
916 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
917 |
> |
(f, |
918 |
> |
(Integer x, Throwable t) -> { |
919 |
> |
m.checkExecutionMode(); |
920 |
> |
threadAssertSame(x, v1); |
921 |
> |
threadAssertNull(t); |
922 |
> |
a.getAndIncrement(); |
923 |
> |
}); |
924 |
> |
if (createIncomplete) f.complete(v1); |
925 |
> |
|
926 |
> |
checkCompletedNormally(g, v1); |
927 |
> |
checkCompletedNormally(f, v1); |
928 |
> |
assertEquals(1, a.get()); |
929 |
> |
}} |
930 |
> |
|
931 |
> |
/** |
932 |
> |
* whenComplete action executes on exceptional completion, propagating |
933 |
> |
* source result. |
934 |
> |
*/ |
935 |
> |
public void testWhenComplete_exceptionalCompletion() { |
936 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
937 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
938 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
939 |
> |
{ |
940 |
> |
final AtomicInteger a = new AtomicInteger(0); |
941 |
> |
final CFException ex = new CFException(); |
942 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
943 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
944 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
945 |
> |
(f, |
946 |
> |
(Integer x, Throwable t) -> { |
947 |
> |
m.checkExecutionMode(); |
948 |
> |
threadAssertNull(x); |
949 |
> |
threadAssertSame(t, ex); |
950 |
> |
a.getAndIncrement(); |
951 |
> |
}); |
952 |
> |
if (createIncomplete) f.completeExceptionally(ex); |
953 |
> |
|
954 |
> |
checkCompletedWithWrappedException(g, ex); |
955 |
> |
checkCompletedExceptionally(f, ex); |
956 |
> |
assertEquals(1, a.get()); |
957 |
> |
}} |
958 |
> |
|
959 |
> |
/** |
960 |
> |
* whenComplete action executes on cancelled source, propagating |
961 |
> |
* CancellationException. |
962 |
> |
*/ |
963 |
> |
public void testWhenComplete_sourceCancelled() { |
964 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
965 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
966 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
967 |
> |
{ |
968 |
> |
final AtomicInteger a = new AtomicInteger(0); |
969 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
970 |
> |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
971 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
972 |
> |
(f, |
973 |
> |
(Integer x, Throwable t) -> { |
974 |
> |
m.checkExecutionMode(); |
975 |
> |
threadAssertNull(x); |
976 |
> |
threadAssertTrue(t instanceof CancellationException); |
977 |
> |
a.getAndIncrement(); |
978 |
> |
}); |
979 |
> |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
980 |
> |
|
981 |
> |
checkCompletedWithWrappedCancellationException(g); |
982 |
> |
checkCancelled(f); |
983 |
> |
assertEquals(1, a.get()); |
984 |
> |
}} |
985 |
> |
|
986 |
> |
/** |
987 |
> |
* If a whenComplete action throws an exception when triggered by |
988 |
> |
* a normal completion, it completes exceptionally |
989 |
> |
*/ |
990 |
> |
public void testWhenComplete_actionFailed() { |
991 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
992 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
993 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
994 |
> |
{ |
995 |
> |
final AtomicInteger a = new AtomicInteger(0); |
996 |
> |
final CFException ex = new CFException(); |
997 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
998 |
> |
if (!createIncomplete) f.complete(v1); |
999 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
1000 |
> |
(f, |
1001 |
> |
(Integer x, Throwable t) -> { |
1002 |
> |
m.checkExecutionMode(); |
1003 |
> |
threadAssertSame(x, v1); |
1004 |
> |
threadAssertNull(t); |
1005 |
> |
a.getAndIncrement(); |
1006 |
> |
throw ex; |
1007 |
> |
}); |
1008 |
> |
if (createIncomplete) f.complete(v1); |
1009 |
> |
|
1010 |
> |
checkCompletedWithWrappedException(g, ex); |
1011 |
> |
checkCompletedNormally(f, v1); |
1012 |
> |
assertEquals(1, a.get()); |
1013 |
> |
}} |
1014 |
> |
|
1015 |
> |
/** |
1016 |
> |
* If a whenComplete action throws an exception when triggered by |
1017 |
> |
* a source completion that also throws an exception, the source |
1018 |
> |
* exception takes precedence. |
1019 |
> |
*/ |
1020 |
> |
public void testWhenComplete_actionFailedSourceFailed() { |
1021 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1022 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1023 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1024 |
> |
{ |
1025 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1026 |
> |
final CFException ex1 = new CFException(); |
1027 |
> |
final CFException ex2 = new CFException(); |
1028 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1029 |
> |
|
1030 |
> |
if (!createIncomplete) f.completeExceptionally(ex1); |
1031 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
1032 |
> |
(f, |
1033 |
> |
(Integer x, Throwable t) -> { |
1034 |
> |
m.checkExecutionMode(); |
1035 |
> |
threadAssertSame(t, ex1); |
1036 |
> |
threadAssertNull(x); |
1037 |
> |
a.getAndIncrement(); |
1038 |
> |
throw ex2; |
1039 |
> |
}); |
1040 |
> |
if (createIncomplete) f.completeExceptionally(ex1); |
1041 |
> |
|
1042 |
> |
checkCompletedWithWrappedException(g, ex1); |
1043 |
> |
checkCompletedExceptionally(f, ex1); |
1044 |
|
assertEquals(1, a.get()); |
1045 |
|
}} |
1046 |
|
|
1097 |
|
if (createIncomplete) f.completeExceptionally(ex); |
1098 |
|
|
1099 |
|
checkCompletedNormally(g, v1); |
1100 |
< |
checkCompletedWithWrappedCFException(f, ex); |
1100 |
> |
checkCompletedExceptionally(f, ex); |
1101 |
|
assertEquals(1, a.get()); |
1102 |
|
}} |
1103 |
|
|
1153 |
|
}); |
1154 |
|
if (createIncomplete) f.completeExceptionally(ex1); |
1155 |
|
|
1156 |
< |
checkCompletedWithWrappedCFException(g, ex2); |
1157 |
< |
checkCompletedWithWrappedCFException(f, ex1); |
1156 |
> |
checkCompletedWithWrappedException(g, ex2); |
1157 |
> |
checkCompletedExceptionally(f, ex1); |
1158 |
|
assertEquals(1, a.get()); |
1159 |
|
}} |
1160 |
|
|
1178 |
|
}); |
1179 |
|
if (createIncomplete) f.complete(v1); |
1180 |
|
|
1181 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1181 |
> |
checkCompletedWithWrappedException(g, ex); |
1182 |
|
checkCompletedNormally(f, v1); |
1183 |
|
assertEquals(1, a.get()); |
1184 |
|
}} |
1292 |
|
f.completeExceptionally(ex); |
1293 |
|
} |
1294 |
|
|
1295 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1296 |
< |
checkCompletedWithWrappedCFException(f, ex); |
1295 |
> |
checkCompletedWithWrappedException(g, ex); |
1296 |
> |
checkCompletedExceptionally(f, ex); |
1297 |
|
r.assertNotInvoked(); |
1298 |
|
}} |
1299 |
|
|
1359 |
|
|
1360 |
|
checkCompletedNormally(g, inc(v1)); |
1361 |
|
checkCompletedNormally(f, v1); |
1362 |
< |
r.assertInvoked(); |
1362 |
> |
r.assertValue(inc(v1)); |
1363 |
|
}} |
1364 |
|
|
1365 |
|
/** |
1380 |
|
f.completeExceptionally(ex); |
1381 |
|
} |
1382 |
|
|
1383 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1384 |
< |
checkCompletedWithWrappedCFException(f, ex); |
1383 |
> |
checkCompletedWithWrappedException(g, ex); |
1384 |
> |
checkCompletedExceptionally(f, ex); |
1385 |
|
r.assertNotInvoked(); |
1386 |
|
}} |
1387 |
|
|
1468 |
|
f.completeExceptionally(ex); |
1469 |
|
} |
1470 |
|
|
1471 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1472 |
< |
checkCompletedWithWrappedCFException(f, ex); |
1471 |
> |
checkCompletedWithWrappedException(g, ex); |
1472 |
> |
checkCompletedExceptionally(f, ex); |
1473 |
|
r.assertNotInvoked(); |
1474 |
|
}} |
1475 |
|
|
1544 |
|
checkCompletedNormally(h, subtract(v1, v2)); |
1545 |
|
checkCompletedNormally(f, v1); |
1546 |
|
checkCompletedNormally(g, v2); |
1547 |
< |
r.assertInvoked(); |
1547 |
> |
r.assertValue(subtract(v1, v2)); |
1548 |
|
}} |
1549 |
|
|
1550 |
|
/** |
1571 |
|
(!fFirst ? f : g).completeExceptionally(ex); |
1572 |
|
} |
1573 |
|
|
1574 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1574 |
> |
checkCompletedWithWrappedException(h, ex); |
1575 |
|
r.assertNotInvoked(); |
1576 |
|
checkCompletedNormally(fFirst ? f : g, v1); |
1577 |
< |
checkCompletedWithWrappedCFException(!fFirst ? f : g, ex); |
1577 |
> |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
1578 |
|
}} |
1579 |
|
|
1580 |
|
/** |
1688 |
|
(!fFirst ? f : g).completeExceptionally(ex); |
1689 |
|
} |
1690 |
|
|
1691 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1691 |
> |
checkCompletedWithWrappedException(h, ex); |
1692 |
|
r.assertNotInvoked(); |
1693 |
|
checkCompletedNormally(fFirst ? f : g, v1); |
1694 |
< |
checkCompletedWithWrappedCFException(!fFirst ? f : g, ex); |
1694 |
> |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
1695 |
|
}} |
1696 |
|
|
1697 |
|
/** |
1805 |
|
(!fFirst ? f : g).completeExceptionally(ex); |
1806 |
|
} |
1807 |
|
|
1808 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1808 |
> |
checkCompletedWithWrappedException(h, ex); |
1809 |
|
r.assertNotInvoked(); |
1810 |
|
checkCompletedNormally(fFirst ? f : g, v1); |
1811 |
< |
checkCompletedWithWrappedCFException(!fFirst ? f : g, ex); |
1811 |
> |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
1812 |
|
}} |
1813 |
|
|
1814 |
|
/** |
1825 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1826 |
|
final Noop r = new Noop(m); |
1827 |
|
|
1640 |
– |
|
1828 |
|
(fFirst ? f : g).complete(v1); |
1829 |
|
if (!createIncomplete) |
1830 |
|
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1939 |
|
rs[0].assertNotInvoked(); |
1940 |
|
rs[1].assertNotInvoked(); |
1941 |
|
f.completeExceptionally(ex); |
1942 |
< |
checkCompletedWithWrappedCFException(h0, ex); |
1943 |
< |
checkCompletedWithWrappedCFException(h1, ex); |
1942 |
> |
checkCompletedWithWrappedException(h0, ex); |
1943 |
> |
checkCompletedWithWrappedException(h1, ex); |
1944 |
|
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
1945 |
|
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
1946 |
< |
checkCompletedWithWrappedCFException(h2, ex); |
1947 |
< |
checkCompletedWithWrappedCFException(h3, ex); |
1946 |
> |
checkCompletedWithWrappedException(h2, ex); |
1947 |
> |
checkCompletedWithWrappedException(h3, ex); |
1948 |
|
g.complete(v1); |
1949 |
|
|
1950 |
|
// unspecified behavior - both source completions available |
1952 |
|
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
1953 |
|
try { |
1954 |
|
assertEquals(inc(v1), h4.join()); |
1955 |
< |
rs[4].assertInvoked(); |
1955 |
> |
rs[4].assertValue(inc(v1)); |
1956 |
|
} catch (CompletionException ok) { |
1957 |
< |
checkCompletedWithWrappedCFException(h4, ex); |
1957 |
> |
checkCompletedWithWrappedException(h4, ex); |
1958 |
|
rs[4].assertNotInvoked(); |
1959 |
|
} |
1960 |
|
try { |
1961 |
|
assertEquals(inc(v1), h5.join()); |
1962 |
< |
rs[5].assertInvoked(); |
1962 |
> |
rs[5].assertValue(inc(v1)); |
1963 |
|
} catch (CompletionException ok) { |
1964 |
< |
checkCompletedWithWrappedCFException(h5, ex); |
1964 |
> |
checkCompletedWithWrappedException(h5, ex); |
1965 |
|
rs[5].assertNotInvoked(); |
1966 |
|
} |
1967 |
|
|
1968 |
< |
checkCompletedWithWrappedCFException(f, ex); |
1968 |
> |
checkCompletedExceptionally(f, ex); |
1969 |
|
checkCompletedNormally(g, v1); |
1970 |
< |
checkCompletedWithWrappedCFException(h0, ex); |
1971 |
< |
checkCompletedWithWrappedCFException(h1, ex); |
1972 |
< |
checkCompletedWithWrappedCFException(h2, ex); |
1973 |
< |
checkCompletedWithWrappedCFException(h3, ex); |
1974 |
< |
checkCompletedWithWrappedCFException(h4, ex); |
1970 |
> |
checkCompletedWithWrappedException(h0, ex); |
1971 |
> |
checkCompletedWithWrappedException(h1, ex); |
1972 |
> |
checkCompletedWithWrappedException(h2, ex); |
1973 |
> |
checkCompletedWithWrappedException(h3, ex); |
1974 |
> |
checkCompletedWithWrappedException(h4, ex); |
1975 |
|
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
1976 |
|
}} |
1977 |
|
|
1978 |
+ |
public void testApplyToEither_exceptionalCompletion2() { |
1979 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
1980 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
1981 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
1982 |
+ |
{ |
1983 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1984 |
+ |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1985 |
+ |
final CFException ex = new CFException(); |
1986 |
+ |
final IncFunction[] rs = new IncFunction[6]; |
1987 |
+ |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1988 |
+ |
|
1989 |
+ |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
1990 |
+ |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
1991 |
+ |
if (fFirst) { |
1992 |
+ |
f.complete(v1); |
1993 |
+ |
g.completeExceptionally(ex); |
1994 |
+ |
} else { |
1995 |
+ |
g.completeExceptionally(ex); |
1996 |
+ |
f.complete(v1); |
1997 |
+ |
} |
1998 |
+ |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
1999 |
+ |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2000 |
+ |
|
2001 |
+ |
// unspecified behavior - both source completions available |
2002 |
+ |
try { |
2003 |
+ |
assertEquals(inc(v1), h0.join()); |
2004 |
+ |
rs[0].assertValue(inc(v1)); |
2005 |
+ |
} catch (CompletionException ok) { |
2006 |
+ |
checkCompletedWithWrappedException(h0, ex); |
2007 |
+ |
rs[0].assertNotInvoked(); |
2008 |
+ |
} |
2009 |
+ |
try { |
2010 |
+ |
assertEquals(inc(v1), h1.join()); |
2011 |
+ |
rs[1].assertValue(inc(v1)); |
2012 |
+ |
} catch (CompletionException ok) { |
2013 |
+ |
checkCompletedWithWrappedException(h1, ex); |
2014 |
+ |
rs[1].assertNotInvoked(); |
2015 |
+ |
} |
2016 |
+ |
try { |
2017 |
+ |
assertEquals(inc(v1), h2.join()); |
2018 |
+ |
rs[2].assertValue(inc(v1)); |
2019 |
+ |
} catch (CompletionException ok) { |
2020 |
+ |
checkCompletedWithWrappedException(h2, ex); |
2021 |
+ |
rs[2].assertNotInvoked(); |
2022 |
+ |
} |
2023 |
+ |
try { |
2024 |
+ |
assertEquals(inc(v1), h3.join()); |
2025 |
+ |
rs[3].assertValue(inc(v1)); |
2026 |
+ |
} catch (CompletionException ok) { |
2027 |
+ |
checkCompletedWithWrappedException(h3, ex); |
2028 |
+ |
rs[3].assertNotInvoked(); |
2029 |
+ |
} |
2030 |
+ |
|
2031 |
+ |
checkCompletedNormally(f, v1); |
2032 |
+ |
checkCompletedExceptionally(g, ex); |
2033 |
+ |
}} |
2034 |
+ |
|
2035 |
|
/** |
2036 |
|
* applyToEither result completes exceptionally if either source cancelled |
2037 |
|
*/ |
2065 |
|
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2066 |
|
try { |
2067 |
|
assertEquals(inc(v1), h4.join()); |
2068 |
< |
rs[4].assertInvoked(); |
2068 |
> |
rs[4].assertValue(inc(v1)); |
2069 |
|
} catch (CompletionException ok) { |
2070 |
|
checkCompletedWithWrappedCancellationException(h4); |
2071 |
|
rs[4].assertNotInvoked(); |
2072 |
|
} |
2073 |
|
try { |
2074 |
|
assertEquals(inc(v1), h5.join()); |
2075 |
< |
rs[5].assertInvoked(); |
2075 |
> |
rs[5].assertValue(inc(v1)); |
2076 |
|
} catch (CompletionException ok) { |
2077 |
|
checkCompletedWithWrappedCancellationException(h5); |
2078 |
|
rs[5].assertNotInvoked(); |
2087 |
|
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2088 |
|
}} |
2089 |
|
|
2090 |
+ |
public void testApplyToEither_sourceCancelled2() { |
2091 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
2092 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2093 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
2094 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
2095 |
+ |
{ |
2096 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2097 |
+ |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2098 |
+ |
final IncFunction[] rs = new IncFunction[6]; |
2099 |
+ |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
2100 |
+ |
|
2101 |
+ |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2102 |
+ |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2103 |
+ |
if (fFirst) { |
2104 |
+ |
f.complete(v1); |
2105 |
+ |
g.cancel(mayInterruptIfRunning); |
2106 |
+ |
} else { |
2107 |
+ |
g.cancel(mayInterruptIfRunning); |
2108 |
+ |
f.complete(v1); |
2109 |
+ |
} |
2110 |
+ |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2111 |
+ |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2112 |
+ |
|
2113 |
+ |
// unspecified behavior - both source completions available |
2114 |
+ |
try { |
2115 |
+ |
assertEquals(inc(v1), h0.join()); |
2116 |
+ |
rs[0].assertValue(inc(v1)); |
2117 |
+ |
} catch (CompletionException ok) { |
2118 |
+ |
checkCompletedWithWrappedCancellationException(h0); |
2119 |
+ |
rs[0].assertNotInvoked(); |
2120 |
+ |
} |
2121 |
+ |
try { |
2122 |
+ |
assertEquals(inc(v1), h1.join()); |
2123 |
+ |
rs[1].assertValue(inc(v1)); |
2124 |
+ |
} catch (CompletionException ok) { |
2125 |
+ |
checkCompletedWithWrappedCancellationException(h1); |
2126 |
+ |
rs[1].assertNotInvoked(); |
2127 |
+ |
} |
2128 |
+ |
try { |
2129 |
+ |
assertEquals(inc(v1), h2.join()); |
2130 |
+ |
rs[2].assertValue(inc(v1)); |
2131 |
+ |
} catch (CompletionException ok) { |
2132 |
+ |
checkCompletedWithWrappedCancellationException(h2); |
2133 |
+ |
rs[2].assertNotInvoked(); |
2134 |
+ |
} |
2135 |
+ |
try { |
2136 |
+ |
assertEquals(inc(v1), h3.join()); |
2137 |
+ |
rs[3].assertValue(inc(v1)); |
2138 |
+ |
} catch (CompletionException ok) { |
2139 |
+ |
checkCompletedWithWrappedCancellationException(h3); |
2140 |
+ |
rs[3].assertNotInvoked(); |
2141 |
+ |
} |
2142 |
+ |
|
2143 |
+ |
checkCompletedNormally(f, v1); |
2144 |
+ |
checkCancelled(g); |
2145 |
+ |
}} |
2146 |
+ |
|
2147 |
|
/** |
2148 |
|
* applyToEither result completes exceptionally if action does |
2149 |
|
*/ |
2258 |
|
rs[0].assertNotInvoked(); |
2259 |
|
rs[1].assertNotInvoked(); |
2260 |
|
f.completeExceptionally(ex); |
2261 |
< |
checkCompletedWithWrappedCFException(h0, ex); |
2262 |
< |
checkCompletedWithWrappedCFException(h1, ex); |
2261 |
> |
checkCompletedWithWrappedException(h0, ex); |
2262 |
> |
checkCompletedWithWrappedException(h1, ex); |
2263 |
|
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2264 |
|
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2265 |
< |
checkCompletedWithWrappedCFException(h2, ex); |
2266 |
< |
checkCompletedWithWrappedCFException(h3, ex); |
2265 |
> |
checkCompletedWithWrappedException(h2, ex); |
2266 |
> |
checkCompletedWithWrappedException(h3, ex); |
2267 |
|
|
2268 |
|
g.complete(v1); |
2269 |
|
|
2274 |
|
assertNull(h4.join()); |
2275 |
|
rs[4].assertValue(v1); |
2276 |
|
} catch (CompletionException ok) { |
2277 |
< |
checkCompletedWithWrappedCFException(h4, ex); |
2277 |
> |
checkCompletedWithWrappedException(h4, ex); |
2278 |
|
rs[4].assertNotInvoked(); |
2279 |
|
} |
2280 |
|
try { |
2281 |
|
assertNull(h5.join()); |
2282 |
|
rs[5].assertValue(v1); |
2283 |
|
} catch (CompletionException ok) { |
2284 |
< |
checkCompletedWithWrappedCFException(h5, ex); |
2284 |
> |
checkCompletedWithWrappedException(h5, ex); |
2285 |
|
rs[5].assertNotInvoked(); |
2286 |
|
} |
2287 |
|
|
2288 |
< |
checkCompletedWithWrappedCFException(f, ex); |
2288 |
> |
checkCompletedExceptionally(f, ex); |
2289 |
|
checkCompletedNormally(g, v1); |
2290 |
< |
checkCompletedWithWrappedCFException(h0, ex); |
2291 |
< |
checkCompletedWithWrappedCFException(h1, ex); |
2292 |
< |
checkCompletedWithWrappedCFException(h2, ex); |
2293 |
< |
checkCompletedWithWrappedCFException(h3, ex); |
2294 |
< |
checkCompletedWithWrappedCFException(h4, ex); |
2290 |
> |
checkCompletedWithWrappedException(h0, ex); |
2291 |
> |
checkCompletedWithWrappedException(h1, ex); |
2292 |
> |
checkCompletedWithWrappedException(h2, ex); |
2293 |
> |
checkCompletedWithWrappedException(h3, ex); |
2294 |
> |
checkCompletedWithWrappedException(h4, ex); |
2295 |
|
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2296 |
|
}} |
2297 |
|
|
2298 |
+ |
public void testAcceptEither_exceptionalCompletion2() { |
2299 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
2300 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
2301 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
2302 |
+ |
{ |
2303 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2304 |
+ |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2305 |
+ |
final CFException ex = new CFException(); |
2306 |
+ |
final NoopConsumer[] rs = new NoopConsumer[6]; |
2307 |
+ |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
2308 |
+ |
|
2309 |
+ |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2310 |
+ |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2311 |
+ |
if (fFirst) { |
2312 |
+ |
f.complete(v1); |
2313 |
+ |
g.completeExceptionally(ex); |
2314 |
+ |
} else { |
2315 |
+ |
g.completeExceptionally(ex); |
2316 |
+ |
f.complete(v1); |
2317 |
+ |
} |
2318 |
+ |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2319 |
+ |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2320 |
+ |
|
2321 |
+ |
// unspecified behavior - both source completions available |
2322 |
+ |
try { |
2323 |
+ |
assertEquals(null, h0.join()); |
2324 |
+ |
rs[0].assertValue(v1); |
2325 |
+ |
} catch (CompletionException ok) { |
2326 |
+ |
checkCompletedWithWrappedException(h0, ex); |
2327 |
+ |
rs[0].assertNotInvoked(); |
2328 |
+ |
} |
2329 |
+ |
try { |
2330 |
+ |
assertEquals(null, h1.join()); |
2331 |
+ |
rs[1].assertValue(v1); |
2332 |
+ |
} catch (CompletionException ok) { |
2333 |
+ |
checkCompletedWithWrappedException(h1, ex); |
2334 |
+ |
rs[1].assertNotInvoked(); |
2335 |
+ |
} |
2336 |
+ |
try { |
2337 |
+ |
assertEquals(null, h2.join()); |
2338 |
+ |
rs[2].assertValue(v1); |
2339 |
+ |
} catch (CompletionException ok) { |
2340 |
+ |
checkCompletedWithWrappedException(h2, ex); |
2341 |
+ |
rs[2].assertNotInvoked(); |
2342 |
+ |
} |
2343 |
+ |
try { |
2344 |
+ |
assertEquals(null, h3.join()); |
2345 |
+ |
rs[3].assertValue(v1); |
2346 |
+ |
} catch (CompletionException ok) { |
2347 |
+ |
checkCompletedWithWrappedException(h3, ex); |
2348 |
+ |
rs[3].assertNotInvoked(); |
2349 |
+ |
} |
2350 |
+ |
|
2351 |
+ |
checkCompletedNormally(f, v1); |
2352 |
+ |
checkCompletedExceptionally(g, ex); |
2353 |
+ |
}} |
2354 |
+ |
|
2355 |
|
/** |
2356 |
|
* acceptEither result completes exceptionally if either source cancelled |
2357 |
|
*/ |
2518 |
|
rs[0].assertNotInvoked(); |
2519 |
|
rs[1].assertNotInvoked(); |
2520 |
|
f.completeExceptionally(ex); |
2521 |
< |
checkCompletedWithWrappedCFException(h0, ex); |
2522 |
< |
checkCompletedWithWrappedCFException(h1, ex); |
2521 |
> |
checkCompletedWithWrappedException(h0, ex); |
2522 |
> |
checkCompletedWithWrappedException(h1, ex); |
2523 |
|
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2524 |
|
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2525 |
< |
checkCompletedWithWrappedCFException(h2, ex); |
2526 |
< |
checkCompletedWithWrappedCFException(h3, ex); |
2525 |
> |
checkCompletedWithWrappedException(h2, ex); |
2526 |
> |
checkCompletedWithWrappedException(h3, ex); |
2527 |
|
|
2528 |
|
g.complete(v1); |
2529 |
|
|
2534 |
|
assertNull(h4.join()); |
2535 |
|
rs[4].assertInvoked(); |
2536 |
|
} catch (CompletionException ok) { |
2537 |
< |
checkCompletedWithWrappedCFException(h4, ex); |
2537 |
> |
checkCompletedWithWrappedException(h4, ex); |
2538 |
|
rs[4].assertNotInvoked(); |
2539 |
|
} |
2540 |
|
try { |
2541 |
|
assertNull(h5.join()); |
2542 |
|
rs[5].assertInvoked(); |
2543 |
|
} catch (CompletionException ok) { |
2544 |
< |
checkCompletedWithWrappedCFException(h5, ex); |
2544 |
> |
checkCompletedWithWrappedException(h5, ex); |
2545 |
|
rs[5].assertNotInvoked(); |
2546 |
|
} |
2547 |
|
|
2548 |
< |
checkCompletedWithWrappedCFException(f, ex); |
2548 |
> |
checkCompletedExceptionally(f, ex); |
2549 |
|
checkCompletedNormally(g, v1); |
2550 |
< |
checkCompletedWithWrappedCFException(h0, ex); |
2551 |
< |
checkCompletedWithWrappedCFException(h1, ex); |
2552 |
< |
checkCompletedWithWrappedCFException(h2, ex); |
2553 |
< |
checkCompletedWithWrappedCFException(h3, ex); |
2554 |
< |
checkCompletedWithWrappedCFException(h4, ex); |
2550 |
> |
checkCompletedWithWrappedException(h0, ex); |
2551 |
> |
checkCompletedWithWrappedException(h1, ex); |
2552 |
> |
checkCompletedWithWrappedException(h2, ex); |
2553 |
> |
checkCompletedWithWrappedException(h3, ex); |
2554 |
> |
checkCompletedWithWrappedException(h4, ex); |
2555 |
|
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2556 |
|
}} |
2557 |
|
|
2558 |
+ |
public void testRunAfterEither_exceptionalCompletion2() { |
2559 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
2560 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
2561 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
2562 |
+ |
{ |
2563 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2564 |
+ |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2565 |
+ |
final CFException ex = new CFException(); |
2566 |
+ |
final Noop[] rs = new Noop[6]; |
2567 |
+ |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2568 |
+ |
|
2569 |
+ |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2570 |
+ |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2571 |
+ |
if (fFirst) { |
2572 |
+ |
f.complete(v1); |
2573 |
+ |
g.completeExceptionally(ex); |
2574 |
+ |
} else { |
2575 |
+ |
g.completeExceptionally(ex); |
2576 |
+ |
f.complete(v1); |
2577 |
+ |
} |
2578 |
+ |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2579 |
+ |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2580 |
+ |
|
2581 |
+ |
// unspecified behavior - both source completions available |
2582 |
+ |
try { |
2583 |
+ |
assertEquals(null, h0.join()); |
2584 |
+ |
rs[0].assertInvoked(); |
2585 |
+ |
} catch (CompletionException ok) { |
2586 |
+ |
checkCompletedWithWrappedException(h0, ex); |
2587 |
+ |
rs[0].assertNotInvoked(); |
2588 |
+ |
} |
2589 |
+ |
try { |
2590 |
+ |
assertEquals(null, h1.join()); |
2591 |
+ |
rs[1].assertInvoked(); |
2592 |
+ |
} catch (CompletionException ok) { |
2593 |
+ |
checkCompletedWithWrappedException(h1, ex); |
2594 |
+ |
rs[1].assertNotInvoked(); |
2595 |
+ |
} |
2596 |
+ |
try { |
2597 |
+ |
assertEquals(null, h2.join()); |
2598 |
+ |
rs[2].assertInvoked(); |
2599 |
+ |
} catch (CompletionException ok) { |
2600 |
+ |
checkCompletedWithWrappedException(h2, ex); |
2601 |
+ |
rs[2].assertNotInvoked(); |
2602 |
+ |
} |
2603 |
+ |
try { |
2604 |
+ |
assertEquals(null, h3.join()); |
2605 |
+ |
rs[3].assertInvoked(); |
2606 |
+ |
} catch (CompletionException ok) { |
2607 |
+ |
checkCompletedWithWrappedException(h3, ex); |
2608 |
+ |
rs[3].assertNotInvoked(); |
2609 |
+ |
} |
2610 |
+ |
|
2611 |
+ |
checkCompletedNormally(f, v1); |
2612 |
+ |
checkCompletedExceptionally(g, ex); |
2613 |
+ |
}} |
2614 |
+ |
|
2615 |
|
/** |
2616 |
|
* runAfterEither result completes exceptionally if either source cancelled |
2617 |
|
*/ |
2718 |
|
|
2719 |
|
checkCompletedNormally(g, inc(v1)); |
2720 |
|
checkCompletedNormally(f, v1); |
2721 |
< |
r.assertInvoked(); |
2721 |
> |
r.assertValue(v1); |
2722 |
|
}} |
2723 |
|
|
2724 |
|
/** |
2736 |
|
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2737 |
|
if (createIncomplete) f.completeExceptionally(ex); |
2738 |
|
|
2739 |
< |
checkCompletedWithWrappedCFException(g, ex); |
2740 |
< |
checkCompletedWithWrappedCFException(f, ex); |
2739 |
> |
checkCompletedWithWrappedException(g, ex); |
2740 |
> |
checkCompletedExceptionally(f, ex); |
2741 |
|
r.assertNotInvoked(); |
2742 |
|
}} |
2743 |
|
|
2993 |
|
assertSame(f, f.toCompletableFuture()); |
2994 |
|
} |
2995 |
|
|
2581 |
– |
/** |
2582 |
– |
* whenComplete action executes on normal completion, propagating |
2583 |
– |
* source result. |
2584 |
– |
*/ |
2585 |
– |
public void testWhenComplete_normalCompletion1() { |
2586 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2587 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2588 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2589 |
– |
{ |
2590 |
– |
final AtomicInteger a = new AtomicInteger(0); |
2591 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2592 |
– |
if (!createIncomplete) f.complete(v1); |
2593 |
– |
final CompletableFuture<Integer> g = m.whenComplete |
2594 |
– |
(f, |
2595 |
– |
(Integer x, Throwable t) -> { |
2596 |
– |
threadAssertSame(x, v1); |
2597 |
– |
threadAssertNull(t); |
2598 |
– |
a.getAndIncrement(); |
2599 |
– |
}); |
2600 |
– |
if (createIncomplete) f.complete(v1); |
2601 |
– |
|
2602 |
– |
checkCompletedNormally(g, v1); |
2603 |
– |
checkCompletedNormally(f, v1); |
2604 |
– |
assertEquals(1, a.get()); |
2605 |
– |
}} |
2606 |
– |
|
2607 |
– |
/** |
2608 |
– |
* whenComplete action executes on exceptional completion, propagating |
2609 |
– |
* source result. |
2610 |
– |
*/ |
2611 |
– |
public void testWhenComplete_exceptionalCompletion() { |
2612 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2613 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2614 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2615 |
– |
{ |
2616 |
– |
final AtomicInteger a = new AtomicInteger(0); |
2617 |
– |
final CFException ex = new CFException(); |
2618 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2619 |
– |
if (!createIncomplete) f.completeExceptionally(ex); |
2620 |
– |
final CompletableFuture<Integer> g = m.whenComplete |
2621 |
– |
(f, |
2622 |
– |
(Integer x, Throwable t) -> { |
2623 |
– |
threadAssertNull(x); |
2624 |
– |
threadAssertSame(t, ex); |
2625 |
– |
a.getAndIncrement(); |
2626 |
– |
}); |
2627 |
– |
if (createIncomplete) f.completeExceptionally(ex); |
2628 |
– |
checkCompletedWithWrappedCFException(f, ex); |
2629 |
– |
checkCompletedWithWrappedCFException(g, ex); |
2630 |
– |
assertEquals(1, a.get()); |
2631 |
– |
}} |
2632 |
– |
|
2633 |
– |
/** |
2634 |
– |
* whenComplete action executes on cancelled source, propagating |
2635 |
– |
* CancellationException. |
2636 |
– |
*/ |
2637 |
– |
public void testWhenComplete_sourceCancelled() { |
2638 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2639 |
– |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2640 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2641 |
– |
{ |
2642 |
– |
final AtomicInteger a = new AtomicInteger(0); |
2643 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2644 |
– |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
2645 |
– |
final CompletableFuture<Integer> g = m.whenComplete |
2646 |
– |
(f, |
2647 |
– |
(Integer x, Throwable t) -> { |
2648 |
– |
threadAssertNull(x); |
2649 |
– |
threadAssertTrue(t instanceof CancellationException); |
2650 |
– |
a.getAndIncrement(); |
2651 |
– |
}); |
2652 |
– |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
2653 |
– |
|
2654 |
– |
//try { g.join(); } catch (Throwable t) { throw new Error(t); } |
2655 |
– |
checkCompletedWithWrappedCancellationException(g); |
2656 |
– |
checkCancelled(f); |
2657 |
– |
assertEquals(1, a.get()); |
2658 |
– |
}} |
2659 |
– |
|
2660 |
– |
/** |
2661 |
– |
* If a whenComplete action throws an exception when triggered by |
2662 |
– |
* a normal completion, it completes exceptionally |
2663 |
– |
*/ |
2664 |
– |
public void testWhenComplete_actionFailed() { |
2665 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2666 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2667 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2668 |
– |
{ |
2669 |
– |
final AtomicInteger a = new AtomicInteger(0); |
2670 |
– |
final CFException ex = new CFException(); |
2671 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2672 |
– |
if (!createIncomplete) f.complete(v1); |
2673 |
– |
final CompletableFuture<Integer> g = m.whenComplete |
2674 |
– |
(f, |
2675 |
– |
(Integer x, Throwable t) -> { |
2676 |
– |
threadAssertSame(x, v1); |
2677 |
– |
threadAssertNull(t); |
2678 |
– |
a.getAndIncrement(); |
2679 |
– |
throw ex; |
2680 |
– |
}); |
2681 |
– |
if (createIncomplete) f.complete(v1); |
2682 |
– |
checkCompletedNormally(f, v1); |
2683 |
– |
checkCompletedWithWrappedCFException(g, ex); |
2684 |
– |
assertEquals(1, a.get()); |
2685 |
– |
}} |
2686 |
– |
|
2687 |
– |
/** |
2688 |
– |
* If a whenComplete action throws an exception when triggered by |
2689 |
– |
* a source completion that also throws an exception, the source |
2690 |
– |
* exception takes precedence. |
2691 |
– |
*/ |
2692 |
– |
public void testWhenComplete_actionFailedSourceFailed() { |
2693 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2694 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2695 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2696 |
– |
{ |
2697 |
– |
final AtomicInteger a = new AtomicInteger(0); |
2698 |
– |
final CFException ex1 = new CFException(); |
2699 |
– |
final CFException ex2 = new CFException(); |
2700 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2701 |
– |
|
2702 |
– |
if (!createIncomplete) f.completeExceptionally(ex1); |
2703 |
– |
final CompletableFuture<Integer> g = m.whenComplete |
2704 |
– |
(f, |
2705 |
– |
(Integer x, Throwable t) -> { |
2706 |
– |
threadAssertSame(t, ex1); |
2707 |
– |
threadAssertNull(x); |
2708 |
– |
a.getAndIncrement(); |
2709 |
– |
throw ex2; |
2710 |
– |
}); |
2711 |
– |
if (createIncomplete) f.completeExceptionally(ex1); |
2712 |
– |
|
2713 |
– |
checkCompletedWithWrappedCFException(f, ex1); |
2714 |
– |
checkCompletedWithWrappedCFException(g, ex1); |
2715 |
– |
assertEquals(1, a.get()); |
2716 |
– |
}} |
2717 |
– |
|
2996 |
|
} |