131 |
|
} catch (ExecutionException success) { |
132 |
|
assertSame(ex, success.getCause()); |
133 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
134 |
< |
|
134 |
> |
|
135 |
|
assertTrue(f.isDone()); |
136 |
|
assertFalse(f.isCancelled()); |
137 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
227 |
|
* isCancelled, join, get, and getNow |
228 |
|
*/ |
229 |
|
public void testComplete() { |
230 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
231 |
+ |
{ |
232 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
233 |
|
checkIncomplete(f); |
234 |
< |
f.complete(one); |
235 |
< |
checkCompletedNormally(f, one); |
236 |
< |
} |
234 |
> |
assertTrue(f.complete(v1)); |
235 |
> |
assertFalse(f.complete(v1)); |
236 |
> |
checkCompletedNormally(f, v1); |
237 |
> |
}} |
238 |
|
|
239 |
|
/** |
240 |
|
* completeExceptionally completes exceptionally, as indicated by |
253 |
|
* methods isDone, isCancelled, join, get, and getNow |
254 |
|
*/ |
255 |
|
public void testCancel() { |
256 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
257 |
+ |
{ |
258 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
259 |
|
checkIncomplete(f); |
260 |
|
assertTrue(f.cancel(true)); |
261 |
+ |
assertTrue(f.cancel(true)); |
262 |
|
checkCancelled(f); |
263 |
< |
} |
263 |
> |
}} |
264 |
|
|
265 |
|
/** |
266 |
|
* obtrudeValue forces completion with given value |
268 |
|
public void testObtrudeValue() { |
269 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
270 |
|
checkIncomplete(f); |
271 |
< |
f.complete(one); |
271 |
> |
assertTrue(f.complete(one)); |
272 |
|
checkCompletedNormally(f, one); |
273 |
|
f.obtrudeValue(three); |
274 |
|
checkCompletedNormally(f, three); |
295 |
|
CompletableFuture<Integer> f; |
296 |
|
|
297 |
|
f = new CompletableFuture<>(); |
298 |
< |
f.complete(v1); |
298 |
> |
assertTrue(f.complete(v1)); |
299 |
|
for (int i = 0; i < 2; i++) { |
300 |
|
f.obtrudeException(ex = new CFException()); |
301 |
|
checkCompletedExceptionally(f, ex); |
315 |
|
checkCompletedExceptionally(f, ex); |
316 |
|
f.completeExceptionally(new CFException()); |
317 |
|
checkCompletedExceptionally(f, ex); |
318 |
< |
f.complete(v1); |
318 |
> |
assertFalse(f.complete(v1)); |
319 |
|
checkCompletedExceptionally(f, ex); |
320 |
|
}} |
321 |
|
|
323 |
|
* getNumberOfDependents returns number of dependent tasks |
324 |
|
*/ |
325 |
|
public void testGetNumberOfDependents() { |
326 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
327 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
328 |
+ |
{ |
329 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
330 |
|
assertEquals(0, f.getNumberOfDependents()); |
331 |
< |
CompletableFuture g = f.thenRun(new Noop(ExecutionMode.DEFAULT)); |
331 |
> |
final CompletableFuture<Void> g = m.thenRun(f, new Noop(m)); |
332 |
|
assertEquals(1, f.getNumberOfDependents()); |
333 |
|
assertEquals(0, g.getNumberOfDependents()); |
334 |
< |
CompletableFuture h = f.thenRun(new Noop(ExecutionMode.DEFAULT)); |
334 |
> |
final CompletableFuture<Void> h = m.thenRun(f, new Noop(m)); |
335 |
|
assertEquals(2, f.getNumberOfDependents()); |
336 |
< |
f.complete(1); |
336 |
> |
assertEquals(0, h.getNumberOfDependents()); |
337 |
> |
assertTrue(f.complete(v1)); |
338 |
|
checkCompletedNormally(g, null); |
339 |
+ |
checkCompletedNormally(h, null); |
340 |
|
assertEquals(0, f.getNumberOfDependents()); |
341 |
|
assertEquals(0, g.getNumberOfDependents()); |
342 |
< |
} |
342 |
> |
assertEquals(0, h.getNumberOfDependents()); |
343 |
> |
}} |
344 |
|
|
345 |
|
/** |
346 |
|
* toString indicates current completion state |
351 |
|
f = new CompletableFuture<String>(); |
352 |
|
assertTrue(f.toString().contains("[Not completed]")); |
353 |
|
|
354 |
< |
f.complete("foo"); |
354 |
> |
assertTrue(f.complete("foo")); |
355 |
|
assertTrue(f.toString().contains("[Completed normally]")); |
356 |
|
|
357 |
|
f = new CompletableFuture<String>(); |
358 |
< |
f.completeExceptionally(new IndexOutOfBoundsException()); |
358 |
> |
assertTrue(f.completeExceptionally(new IndexOutOfBoundsException())); |
359 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
360 |
+ |
|
361 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
362 |
+ |
f = new CompletableFuture<String>(); |
363 |
+ |
assertTrue(f.cancel(mayInterruptIfRunning)); |
364 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
365 |
+ |
} |
366 |
|
} |
367 |
|
|
368 |
|
/** |
539 |
|
invoked(); |
540 |
|
value = x; |
541 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
542 |
< |
f.complete(inc(x)); |
542 |
> |
assertTrue(f.complete(inc(x))); |
543 |
|
return f; |
544 |
|
} |
545 |
|
} |
847 |
|
{ |
848 |
|
final AtomicInteger a = new AtomicInteger(0); |
849 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
850 |
< |
if (!createIncomplete) f.complete(v1); |
850 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
851 |
|
final CompletableFuture<Integer> g = f.exceptionally |
852 |
|
((Throwable t) -> { |
853 |
|
// Should not be called |
854 |
|
a.getAndIncrement(); |
855 |
|
throw new AssertionError(); |
856 |
|
}); |
857 |
< |
if (createIncomplete) f.complete(v1); |
857 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
858 |
|
|
859 |
|
checkCompletedNormally(g, v1); |
860 |
|
checkCompletedNormally(f, v1); |
861 |
|
assertEquals(0, a.get()); |
862 |
|
}} |
863 |
|
|
846 |
– |
|
864 |
|
/** |
865 |
|
* exceptionally action completes with function value on source |
866 |
|
* exception |
909 |
|
}} |
910 |
|
|
911 |
|
/** |
912 |
+ |
* whenComplete action executes on normal completion, propagating |
913 |
+ |
* source result. |
914 |
+ |
*/ |
915 |
+ |
public void testWhenComplete_normalCompletion1() { |
916 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
917 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
918 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
919 |
+ |
{ |
920 |
+ |
final AtomicInteger a = new AtomicInteger(0); |
921 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
922 |
+ |
if (!createIncomplete) assertTrue(f.complete(v1)); |
923 |
+ |
final CompletableFuture<Integer> g = m.whenComplete |
924 |
+ |
(f, |
925 |
+ |
(Integer x, Throwable t) -> { |
926 |
+ |
m.checkExecutionMode(); |
927 |
+ |
threadAssertSame(x, v1); |
928 |
+ |
threadAssertNull(t); |
929 |
+ |
a.getAndIncrement(); |
930 |
+ |
}); |
931 |
+ |
if (createIncomplete) assertTrue(f.complete(v1)); |
932 |
+ |
|
933 |
+ |
checkCompletedNormally(g, v1); |
934 |
+ |
checkCompletedNormally(f, v1); |
935 |
+ |
assertEquals(1, a.get()); |
936 |
+ |
}} |
937 |
+ |
|
938 |
+ |
/** |
939 |
+ |
* whenComplete action executes on exceptional completion, propagating |
940 |
+ |
* source result. |
941 |
+ |
*/ |
942 |
+ |
public void testWhenComplete_exceptionalCompletion() { |
943 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
944 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
945 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
946 |
+ |
{ |
947 |
+ |
final AtomicInteger a = new AtomicInteger(0); |
948 |
+ |
final CFException ex = new CFException(); |
949 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
950 |
+ |
if (!createIncomplete) f.completeExceptionally(ex); |
951 |
+ |
final CompletableFuture<Integer> g = m.whenComplete |
952 |
+ |
(f, |
953 |
+ |
(Integer x, Throwable t) -> { |
954 |
+ |
m.checkExecutionMode(); |
955 |
+ |
threadAssertNull(x); |
956 |
+ |
threadAssertSame(t, ex); |
957 |
+ |
a.getAndIncrement(); |
958 |
+ |
}); |
959 |
+ |
if (createIncomplete) f.completeExceptionally(ex); |
960 |
+ |
|
961 |
+ |
checkCompletedWithWrappedException(g, ex); |
962 |
+ |
checkCompletedExceptionally(f, ex); |
963 |
+ |
assertEquals(1, a.get()); |
964 |
+ |
}} |
965 |
+ |
|
966 |
+ |
/** |
967 |
+ |
* whenComplete action executes on cancelled source, propagating |
968 |
+ |
* CancellationException. |
969 |
+ |
*/ |
970 |
+ |
public void testWhenComplete_sourceCancelled() { |
971 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
972 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
973 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
974 |
+ |
{ |
975 |
+ |
final AtomicInteger a = new AtomicInteger(0); |
976 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
977 |
+ |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
978 |
+ |
final CompletableFuture<Integer> g = m.whenComplete |
979 |
+ |
(f, |
980 |
+ |
(Integer x, Throwable t) -> { |
981 |
+ |
m.checkExecutionMode(); |
982 |
+ |
threadAssertNull(x); |
983 |
+ |
threadAssertTrue(t instanceof CancellationException); |
984 |
+ |
a.getAndIncrement(); |
985 |
+ |
}); |
986 |
+ |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
987 |
+ |
|
988 |
+ |
checkCompletedWithWrappedCancellationException(g); |
989 |
+ |
checkCancelled(f); |
990 |
+ |
assertEquals(1, a.get()); |
991 |
+ |
}} |
992 |
+ |
|
993 |
+ |
/** |
994 |
+ |
* If a whenComplete action throws an exception when triggered by |
995 |
+ |
* a normal completion, it completes exceptionally |
996 |
+ |
*/ |
997 |
+ |
public void testWhenComplete_actionFailed() { |
998 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
999 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
1000 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
1001 |
+ |
{ |
1002 |
+ |
final AtomicInteger a = new AtomicInteger(0); |
1003 |
+ |
final CFException ex = new CFException(); |
1004 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1005 |
+ |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1006 |
+ |
final CompletableFuture<Integer> g = m.whenComplete |
1007 |
+ |
(f, |
1008 |
+ |
(Integer x, Throwable t) -> { |
1009 |
+ |
m.checkExecutionMode(); |
1010 |
+ |
threadAssertSame(x, v1); |
1011 |
+ |
threadAssertNull(t); |
1012 |
+ |
a.getAndIncrement(); |
1013 |
+ |
throw ex; |
1014 |
+ |
}); |
1015 |
+ |
if (createIncomplete) assertTrue(f.complete(v1)); |
1016 |
+ |
|
1017 |
+ |
checkCompletedWithWrappedException(g, ex); |
1018 |
+ |
checkCompletedNormally(f, v1); |
1019 |
+ |
assertEquals(1, a.get()); |
1020 |
+ |
}} |
1021 |
+ |
|
1022 |
+ |
/** |
1023 |
+ |
* If a whenComplete action throws an exception when triggered by |
1024 |
+ |
* a source completion that also throws an exception, the source |
1025 |
+ |
* exception takes precedence. |
1026 |
+ |
*/ |
1027 |
+ |
public void testWhenComplete_actionFailedSourceFailed() { |
1028 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
1029 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
1030 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
1031 |
+ |
{ |
1032 |
+ |
final AtomicInteger a = new AtomicInteger(0); |
1033 |
+ |
final CFException ex1 = new CFException(); |
1034 |
+ |
final CFException ex2 = new CFException(); |
1035 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1036 |
+ |
|
1037 |
+ |
if (!createIncomplete) f.completeExceptionally(ex1); |
1038 |
+ |
final CompletableFuture<Integer> g = m.whenComplete |
1039 |
+ |
(f, |
1040 |
+ |
(Integer x, Throwable t) -> { |
1041 |
+ |
m.checkExecutionMode(); |
1042 |
+ |
threadAssertSame(t, ex1); |
1043 |
+ |
threadAssertNull(x); |
1044 |
+ |
a.getAndIncrement(); |
1045 |
+ |
throw ex2; |
1046 |
+ |
}); |
1047 |
+ |
if (createIncomplete) f.completeExceptionally(ex1); |
1048 |
+ |
|
1049 |
+ |
checkCompletedWithWrappedException(g, ex1); |
1050 |
+ |
checkCompletedExceptionally(f, ex1); |
1051 |
+ |
assertEquals(1, a.get()); |
1052 |
+ |
}} |
1053 |
+ |
|
1054 |
+ |
/** |
1055 |
|
* handle action completes normally with function value on normal |
1056 |
|
* completion of source |
1057 |
|
*/ |
1062 |
|
{ |
1063 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1064 |
|
final AtomicInteger a = new AtomicInteger(0); |
1065 |
< |
if (!createIncomplete) f.complete(v1); |
1065 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1066 |
|
final CompletableFuture<Integer> g = m.handle |
1067 |
|
(f, |
1068 |
|
(Integer x, Throwable t) -> { |
1072 |
|
a.getAndIncrement(); |
1073 |
|
return inc(v1); |
1074 |
|
}); |
1075 |
< |
if (createIncomplete) f.complete(v1); |
1075 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1076 |
|
|
1077 |
|
checkCompletedNormally(g, inc(v1)); |
1078 |
|
checkCompletedNormally(f, v1); |
1173 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1174 |
|
final AtomicInteger a = new AtomicInteger(0); |
1175 |
|
final CFException ex = new CFException(); |
1176 |
< |
if (!createIncomplete) f.complete(v1); |
1176 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1177 |
|
final CompletableFuture<Integer> g = m.handle |
1178 |
|
(f, |
1179 |
|
(Integer x, Throwable t) -> { |
1183 |
|
a.getAndIncrement(); |
1184 |
|
throw ex; |
1185 |
|
}); |
1186 |
< |
if (createIncomplete) f.complete(v1); |
1186 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1187 |
|
|
1188 |
|
checkCompletedWithWrappedException(g, ex); |
1189 |
|
checkCompletedNormally(f, v1); |
1269 |
|
{ |
1270 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1271 |
|
final Noop r = new Noop(m); |
1272 |
< |
if (!createIncomplete) f.complete(v1); |
1272 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1273 |
|
final CompletableFuture<Void> g = m.thenRun(f, r); |
1274 |
|
if (createIncomplete) { |
1275 |
|
checkIncomplete(g); |
1276 |
< |
f.complete(v1); |
1276 |
> |
assertTrue(f.complete(v1)); |
1277 |
|
} |
1278 |
|
|
1279 |
|
checkCompletedNormally(g, null); |
1336 |
|
{ |
1337 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1338 |
|
final FailingRunnable r = new FailingRunnable(m); |
1339 |
< |
if (!createIncomplete) f.complete(v1); |
1339 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1340 |
|
final CompletableFuture<Void> g = m.thenRun(f, r); |
1341 |
|
if (createIncomplete) { |
1342 |
|
checkIncomplete(g); |
1343 |
< |
f.complete(v1); |
1343 |
> |
assertTrue(f.complete(v1)); |
1344 |
|
} |
1345 |
|
|
1346 |
|
checkCompletedWithWrappedCFException(g); |
1357 |
|
{ |
1358 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1359 |
|
final IncFunction r = new IncFunction(m); |
1360 |
< |
if (!createIncomplete) f.complete(v1); |
1360 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1361 |
|
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1362 |
|
if (createIncomplete) { |
1363 |
|
checkIncomplete(g); |
1364 |
< |
f.complete(v1); |
1364 |
> |
assertTrue(f.complete(v1)); |
1365 |
|
} |
1366 |
|
|
1367 |
|
checkCompletedNormally(g, inc(v1)); |
1424 |
|
{ |
1425 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1426 |
|
final FailingFunction r = new FailingFunction(m); |
1427 |
< |
if (!createIncomplete) f.complete(v1); |
1427 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1428 |
|
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1429 |
|
if (createIncomplete) { |
1430 |
|
checkIncomplete(g); |
1431 |
< |
f.complete(v1); |
1431 |
> |
assertTrue(f.complete(v1)); |
1432 |
|
} |
1433 |
|
|
1434 |
|
checkCompletedWithWrappedCFException(g); |
1445 |
|
{ |
1446 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1447 |
|
final NoopConsumer r = new NoopConsumer(m); |
1448 |
< |
if (!createIncomplete) f.complete(v1); |
1448 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1449 |
|
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1450 |
|
if (createIncomplete) { |
1451 |
|
checkIncomplete(g); |
1452 |
< |
f.complete(v1); |
1452 |
> |
assertTrue(f.complete(v1)); |
1453 |
|
} |
1454 |
|
|
1455 |
|
checkCompletedNormally(g, null); |
1538 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1539 |
|
final SubtractFunction r = new SubtractFunction(m); |
1540 |
|
|
1541 |
< |
if (fFirst) f.complete(v1); else g.complete(v2); |
1541 |
> |
assertTrue(fFirst ? f.complete(v1) : g.complete(v2)); |
1542 |
|
if (!createIncomplete) |
1543 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1543 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
1544 |
|
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1545 |
|
if (createIncomplete) { |
1546 |
|
checkIncomplete(h); |
1547 |
|
r.assertNotInvoked(); |
1548 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1548 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
1549 |
|
} |
1550 |
|
|
1551 |
|
checkCompletedNormally(h, subtract(v1, v2)); |
1569 |
|
final CFException ex = new CFException(); |
1570 |
|
final SubtractFunction r = new SubtractFunction(m); |
1571 |
|
|
1572 |
< |
(fFirst ? f : g).complete(v1); |
1572 |
> |
assertTrue((fFirst ? f : g).complete(v1)); |
1573 |
|
if (!createIncomplete) |
1574 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1574 |
> |
assertTrue((!fFirst ? f : g).completeExceptionally(ex)); |
1575 |
|
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1576 |
|
if (createIncomplete) { |
1577 |
|
checkIncomplete(h); |
1578 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1578 |
> |
assertTrue((!fFirst ? f : g).completeExceptionally(ex)); |
1579 |
|
} |
1580 |
|
|
1581 |
|
checkCompletedWithWrappedException(h, ex); |
1598 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1599 |
|
final SubtractFunction r = new SubtractFunction(m); |
1600 |
|
|
1601 |
< |
(fFirst ? f : g).complete(v1); |
1601 |
> |
assertTrue((fFirst ? f : g).complete(v1)); |
1602 |
|
if (!createIncomplete) |
1603 |
|
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1604 |
|
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1627 |
|
final FailingBiFunction r = new FailingBiFunction(m); |
1628 |
|
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1629 |
|
|
1630 |
< |
if (fFirst) { |
1631 |
< |
f.complete(v1); |
1472 |
< |
g.complete(v2); |
1473 |
< |
} else { |
1474 |
< |
g.complete(v2); |
1475 |
< |
f.complete(v1); |
1476 |
< |
} |
1630 |
> |
assertTrue( fFirst ? f.complete(v1) : g.complete(v2)); |
1631 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
1632 |
|
|
1633 |
|
checkCompletedWithWrappedCFException(h); |
1634 |
|
checkCompletedNormally(f, v1); |
1650 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1651 |
|
final SubtractAction r = new SubtractAction(m); |
1652 |
|
|
1653 |
< |
if (fFirst) f.complete(v1); else g.complete(v2); |
1653 |
> |
assertTrue(fFirst ? f.complete(v1) : g.complete(v2)); |
1654 |
|
if (!createIncomplete) |
1655 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1655 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
1656 |
|
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1657 |
|
if (createIncomplete) { |
1658 |
|
checkIncomplete(h); |
1659 |
|
r.assertNotInvoked(); |
1660 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1660 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
1661 |
|
} |
1662 |
|
|
1663 |
|
checkCompletedNormally(h, null); |
1681 |
|
final CFException ex = new CFException(); |
1682 |
|
final SubtractAction r = new SubtractAction(m); |
1683 |
|
|
1684 |
< |
(fFirst ? f : g).complete(v1); |
1684 |
> |
assertTrue((fFirst ? f : g).complete(v1)); |
1685 |
|
if (!createIncomplete) |
1686 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1686 |
> |
assertTrue((!fFirst ? f : g).completeExceptionally(ex)); |
1687 |
|
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1688 |
|
if (createIncomplete) { |
1689 |
|
checkIncomplete(h); |
1690 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1690 |
> |
assertTrue((!fFirst ? f : g).completeExceptionally(ex)); |
1691 |
|
} |
1692 |
|
|
1693 |
|
checkCompletedWithWrappedException(h, ex); |
1710 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1711 |
|
final SubtractAction r = new SubtractAction(m); |
1712 |
|
|
1713 |
< |
(fFirst ? f : g).complete(v1); |
1713 |
> |
assertTrue((fFirst ? f : g).complete(v1)); |
1714 |
|
if (!createIncomplete) |
1715 |
|
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1716 |
|
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1739 |
|
final FailingBiConsumer r = new FailingBiConsumer(m); |
1740 |
|
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1741 |
|
|
1742 |
< |
if (fFirst) { |
1743 |
< |
f.complete(v1); |
1589 |
< |
g.complete(v2); |
1590 |
< |
} else { |
1591 |
< |
g.complete(v2); |
1592 |
< |
f.complete(v1); |
1593 |
< |
} |
1742 |
> |
assertTrue(fFirst ? f.complete(v1) : g.complete(v2)); |
1743 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
1744 |
|
|
1745 |
|
checkCompletedWithWrappedCFException(h); |
1746 |
|
checkCompletedNormally(f, v1); |
1762 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1763 |
|
final Noop r = new Noop(m); |
1764 |
|
|
1765 |
< |
if (fFirst) f.complete(v1); else g.complete(v2); |
1765 |
> |
assertTrue(fFirst ? f.complete(v1) : g.complete(v2)); |
1766 |
|
if (!createIncomplete) |
1767 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1767 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
1768 |
|
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1769 |
|
if (createIncomplete) { |
1770 |
|
checkIncomplete(h); |
1771 |
|
r.assertNotInvoked(); |
1772 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1772 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
1773 |
|
} |
1774 |
|
|
1775 |
|
checkCompletedNormally(h, null); |
1793 |
|
final CFException ex = new CFException(); |
1794 |
|
final Noop r = new Noop(m); |
1795 |
|
|
1796 |
< |
(fFirst ? f : g).complete(v1); |
1796 |
> |
assertTrue((fFirst ? f : g).complete(v1)); |
1797 |
|
if (!createIncomplete) |
1798 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1798 |
> |
assertTrue((!fFirst ? f : g).completeExceptionally(ex)); |
1799 |
|
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1800 |
|
if (createIncomplete) { |
1801 |
|
checkIncomplete(h); |
1802 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1802 |
> |
assertTrue((!fFirst ? f : g).completeExceptionally(ex)); |
1803 |
|
} |
1804 |
|
|
1805 |
|
checkCompletedWithWrappedException(h, ex); |
1822 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1823 |
|
final Noop r = new Noop(m); |
1824 |
|
|
1825 |
< |
|
1676 |
< |
(fFirst ? f : g).complete(v1); |
1825 |
> |
assertTrue((fFirst ? f : g).complete(v1)); |
1826 |
|
if (!createIncomplete) |
1827 |
|
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1828 |
|
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1852 |
|
final FailingRunnable r2 = new FailingRunnable(m); |
1853 |
|
|
1854 |
|
CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
1855 |
< |
if (fFirst) { |
1856 |
< |
f.complete(v1); |
1708 |
< |
g.complete(v2); |
1709 |
< |
} else { |
1710 |
< |
g.complete(v2); |
1711 |
< |
f.complete(v1); |
1712 |
< |
} |
1855 |
> |
assertTrue(fFirst ? f.complete(v1) : g.complete(v2)); |
1856 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
1857 |
|
CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
1858 |
|
|
1859 |
|
checkCompletedWithWrappedCFException(h1); |
1980 |
|
|
1981 |
|
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
1982 |
|
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
1983 |
< |
if (fFirst) { |
1984 |
< |
f.complete(v1); |
1841 |
< |
g.completeExceptionally(ex); |
1842 |
< |
} else { |
1843 |
< |
g.completeExceptionally(ex); |
1844 |
< |
f.complete(v1); |
1845 |
< |
} |
1983 |
> |
assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
1984 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
1985 |
|
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
1986 |
|
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
1987 |
|
|
2087 |
|
|
2088 |
|
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2089 |
|
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2090 |
< |
if (fFirst) { |
2091 |
< |
f.complete(v1); |
1953 |
< |
g.cancel(mayInterruptIfRunning); |
1954 |
< |
} else { |
1955 |
< |
g.cancel(mayInterruptIfRunning); |
1956 |
< |
f.complete(v1); |
1957 |
< |
} |
2090 |
> |
assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning)); |
2091 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning)); |
2092 |
|
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2093 |
|
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2094 |
|
|
2290 |
|
|
2291 |
|
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2292 |
|
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2293 |
< |
if (fFirst) { |
2294 |
< |
f.complete(v1); |
2161 |
< |
g.completeExceptionally(ex); |
2162 |
< |
} else { |
2163 |
< |
g.completeExceptionally(ex); |
2164 |
< |
f.complete(v1); |
2165 |
< |
} |
2293 |
> |
assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2294 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2295 |
|
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2296 |
|
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2297 |
|
|
2494 |
|
checkIncomplete(h1); |
2495 |
|
rs[0].assertNotInvoked(); |
2496 |
|
rs[1].assertNotInvoked(); |
2497 |
< |
f.completeExceptionally(ex); |
2497 |
> |
assertTrue(f.completeExceptionally(ex)); |
2498 |
|
checkCompletedWithWrappedException(h0, ex); |
2499 |
|
checkCompletedWithWrappedException(h1, ex); |
2500 |
|
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2502 |
|
checkCompletedWithWrappedException(h2, ex); |
2503 |
|
checkCompletedWithWrappedException(h3, ex); |
2504 |
|
|
2505 |
< |
g.complete(v1); |
2505 |
> |
assertTrue(g.complete(v1)); |
2506 |
|
|
2507 |
|
// unspecified behavior - both source completions available |
2508 |
|
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2545 |
|
|
2546 |
|
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2547 |
|
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2548 |
< |
if (fFirst) { |
2549 |
< |
f.complete(v1); |
2421 |
< |
g.completeExceptionally(ex); |
2422 |
< |
} else { |
2423 |
< |
g.completeExceptionally(ex); |
2424 |
< |
f.complete(v1); |
2425 |
< |
} |
2548 |
> |
assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2549 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2550 |
|
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2551 |
|
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2552 |
|
|
2611 |
|
checkCompletedWithWrappedCancellationException(h2); |
2612 |
|
checkCompletedWithWrappedCancellationException(h3); |
2613 |
|
|
2614 |
< |
g.complete(v1); |
2614 |
> |
assertTrue(g.complete(v1)); |
2615 |
|
|
2616 |
|
// unspecified behavior - both source completions available |
2617 |
|
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2655 |
|
|
2656 |
|
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2657 |
|
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2658 |
< |
f.complete(v1); |
2658 |
> |
assertTrue(f.complete(v1)); |
2659 |
|
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2660 |
|
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2661 |
|
checkCompletedWithWrappedCFException(h0); |
2663 |
|
checkCompletedWithWrappedCFException(h2); |
2664 |
|
checkCompletedWithWrappedCFException(h3); |
2665 |
|
for (int i = 0; i < 4; i++) rs[i].assertInvoked(); |
2666 |
< |
g.complete(v2); |
2666 |
> |
assertTrue(g.complete(v2)); |
2667 |
|
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2668 |
|
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2669 |
|
checkCompletedWithWrappedCFException(h4); |
2684 |
|
{ |
2685 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2686 |
|
final CompletableFutureInc r = new CompletableFutureInc(m); |
2687 |
< |
if (!createIncomplete) f.complete(v1); |
2687 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
2688 |
|
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2689 |
< |
if (createIncomplete) f.complete(v1); |
2689 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
2690 |
|
|
2691 |
|
checkCompletedNormally(g, inc(v1)); |
2692 |
|
checkCompletedNormally(f, v1); |
2724 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2725 |
|
final FailingCompletableFutureFunction r |
2726 |
|
= new FailingCompletableFutureFunction(m); |
2727 |
< |
if (!createIncomplete) f.complete(v1); |
2727 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
2728 |
|
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2729 |
< |
if (createIncomplete) f.complete(v1); |
2729 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
2730 |
|
|
2731 |
|
checkCompletedWithWrappedCFException(g); |
2732 |
|
checkCompletedNormally(f, v1); |
2965 |
|
assertSame(f, f.toCompletableFuture()); |
2966 |
|
} |
2967 |
|
|
2968 |
< |
/** |
2969 |
< |
* whenComplete action executes on normal completion, propagating |
2970 |
< |
* source result. |
2971 |
< |
*/ |
2972 |
< |
public void testWhenComplete_normalCompletion1() { |
2973 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2974 |
< |
for (boolean createIncomplete : new boolean[] { true, false }) |
2975 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
2976 |
< |
{ |
2977 |
< |
final AtomicInteger a = new AtomicInteger(0); |
2978 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2979 |
< |
if (!createIncomplete) f.complete(v1); |
2980 |
< |
final CompletableFuture<Integer> g = m.whenComplete |
2981 |
< |
(f, |
2982 |
< |
(Integer x, Throwable t) -> { |
2983 |
< |
m.checkExecutionMode(); |
2984 |
< |
threadAssertSame(x, v1); |
2861 |
< |
threadAssertNull(t); |
2862 |
< |
a.getAndIncrement(); |
2863 |
< |
}); |
2864 |
< |
if (createIncomplete) f.complete(v1); |
2865 |
< |
|
2866 |
< |
checkCompletedNormally(g, v1); |
2867 |
< |
checkCompletedNormally(f, v1); |
2868 |
< |
assertEquals(1, a.get()); |
2869 |
< |
}} |
2870 |
< |
|
2871 |
< |
/** |
2872 |
< |
* whenComplete action executes on exceptional completion, propagating |
2873 |
< |
* source result. |
2874 |
< |
*/ |
2875 |
< |
public void testWhenComplete_exceptionalCompletion() { |
2876 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2877 |
< |
for (boolean createIncomplete : new boolean[] { true, false }) |
2878 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
2879 |
< |
{ |
2880 |
< |
final AtomicInteger a = new AtomicInteger(0); |
2881 |
< |
final CFException ex = new CFException(); |
2882 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2883 |
< |
if (!createIncomplete) f.completeExceptionally(ex); |
2884 |
< |
final CompletableFuture<Integer> g = m.whenComplete |
2885 |
< |
(f, |
2886 |
< |
(Integer x, Throwable t) -> { |
2887 |
< |
m.checkExecutionMode(); |
2888 |
< |
threadAssertNull(x); |
2889 |
< |
threadAssertSame(t, ex); |
2890 |
< |
a.getAndIncrement(); |
2891 |
< |
}); |
2892 |
< |
if (createIncomplete) f.completeExceptionally(ex); |
2893 |
< |
checkCompletedExceptionally(f, ex); |
2894 |
< |
checkCompletedWithWrappedException(g, ex); |
2895 |
< |
assertEquals(1, a.get()); |
2896 |
< |
}} |
2897 |
< |
|
2898 |
< |
/** |
2899 |
< |
* whenComplete action executes on cancelled source, propagating |
2900 |
< |
* CancellationException. |
2901 |
< |
*/ |
2902 |
< |
public void testWhenComplete_sourceCancelled() { |
2903 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2904 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2905 |
< |
for (boolean createIncomplete : new boolean[] { true, false }) |
2906 |
< |
{ |
2907 |
< |
final AtomicInteger a = new AtomicInteger(0); |
2908 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2909 |
< |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
2910 |
< |
final CompletableFuture<Integer> g = m.whenComplete |
2911 |
< |
(f, |
2912 |
< |
(Integer x, Throwable t) -> { |
2913 |
< |
m.checkExecutionMode(); |
2914 |
< |
threadAssertNull(x); |
2915 |
< |
threadAssertTrue(t instanceof CancellationException); |
2916 |
< |
a.getAndIncrement(); |
2917 |
< |
}); |
2918 |
< |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
2919 |
< |
|
2920 |
< |
checkCompletedWithWrappedCancellationException(g); |
2921 |
< |
checkCancelled(f); |
2922 |
< |
assertEquals(1, a.get()); |
2923 |
< |
}} |
2924 |
< |
|
2925 |
< |
/** |
2926 |
< |
* If a whenComplete action throws an exception when triggered by |
2927 |
< |
* a normal completion, it completes exceptionally |
2928 |
< |
*/ |
2929 |
< |
public void testWhenComplete_actionFailed() { |
2930 |
< |
for (boolean createIncomplete : new boolean[] { true, false }) |
2931 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2932 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
2933 |
< |
{ |
2934 |
< |
final AtomicInteger a = new AtomicInteger(0); |
2935 |
< |
final CFException ex = new CFException(); |
2936 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2937 |
< |
if (!createIncomplete) f.complete(v1); |
2938 |
< |
final CompletableFuture<Integer> g = m.whenComplete |
2939 |
< |
(f, |
2940 |
< |
(Integer x, Throwable t) -> { |
2941 |
< |
m.checkExecutionMode(); |
2942 |
< |
threadAssertSame(x, v1); |
2943 |
< |
threadAssertNull(t); |
2944 |
< |
a.getAndIncrement(); |
2945 |
< |
throw ex; |
2946 |
< |
}); |
2947 |
< |
if (createIncomplete) f.complete(v1); |
2948 |
< |
checkCompletedNormally(f, v1); |
2949 |
< |
checkCompletedWithWrappedException(g, ex); |
2950 |
< |
assertEquals(1, a.get()); |
2951 |
< |
}} |
2952 |
< |
|
2953 |
< |
/** |
2954 |
< |
* If a whenComplete action throws an exception when triggered by |
2955 |
< |
* a source completion that also throws an exception, the source |
2956 |
< |
* exception takes precedence. |
2957 |
< |
*/ |
2958 |
< |
public void testWhenComplete_actionFailedSourceFailed() { |
2959 |
< |
for (boolean createIncomplete : new boolean[] { true, false }) |
2960 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2961 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
2962 |
< |
{ |
2963 |
< |
final AtomicInteger a = new AtomicInteger(0); |
2964 |
< |
final CFException ex1 = new CFException(); |
2965 |
< |
final CFException ex2 = new CFException(); |
2966 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2967 |
< |
|
2968 |
< |
if (!createIncomplete) f.completeExceptionally(ex1); |
2969 |
< |
final CompletableFuture<Integer> g = m.whenComplete |
2970 |
< |
(f, |
2971 |
< |
(Integer x, Throwable t) -> { |
2972 |
< |
m.checkExecutionMode(); |
2973 |
< |
threadAssertSame(t, ex1); |
2974 |
< |
threadAssertNull(x); |
2975 |
< |
a.getAndIncrement(); |
2976 |
< |
throw ex2; |
2977 |
< |
}); |
2978 |
< |
if (createIncomplete) f.completeExceptionally(ex1); |
2979 |
< |
|
2980 |
< |
checkCompletedExceptionally(f, ex1); |
2981 |
< |
checkCompletedWithWrappedException(g, ex1); |
2982 |
< |
assertEquals(1, a.get()); |
2983 |
< |
}} |
2968 |
> |
// public void testRunAfterEither_resultDeterminedAtTimeOfCreation() { |
2969 |
> |
// for (ExecutionMode m : ExecutionMode.values()) |
2970 |
> |
// for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2971 |
> |
// for (Integer v1 : new Integer[] { 1, null }) |
2972 |
> |
// { |
2973 |
> |
// final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2974 |
> |
// final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2975 |
> |
// final Noop[] rs = new Noop[2]; |
2976 |
> |
// for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2977 |
> |
// f.complete(v1); |
2978 |
> |
// final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2979 |
> |
// final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2980 |
> |
// assertTrue(g.cancel(mayInterruptIfRunning)); |
2981 |
> |
// checkCompletedNormally(h0, null); |
2982 |
> |
// checkCompletedNormally(h1, null); |
2983 |
> |
// for (Noop r : rs) r.assertInvoked(); |
2984 |
> |
// }} |
2985 |
|
|
2986 |
|
} |