320 |
|
checkCompletedNormally(f, "test"); |
321 |
|
} |
322 |
|
|
323 |
< |
// Choose non-commutative actions for better coverage |
324 |
< |
|
325 |
< |
// A non-commutative function that handles and produces null values as well. |
326 |
< |
static Integer subtract(Integer x, Integer y) { |
327 |
< |
return (x == null && y == null) ? null : |
328 |
< |
((x == null) ? 42 : x.intValue()) |
329 |
< |
- ((y == null) ? 99 : y.intValue()); |
323 |
> |
static final class IntegerSupplier implements Supplier<Integer> { |
324 |
> |
final ExecutionMode m; |
325 |
> |
int invocationCount = 0; |
326 |
> |
final Integer value; |
327 |
> |
IntegerSupplier(ExecutionMode m, Integer value) { |
328 |
> |
this.m = m; |
329 |
> |
this.value = value; |
330 |
> |
} |
331 |
> |
public Integer get() { |
332 |
> |
m.checkExecutionMode(); |
333 |
> |
invocationCount++; |
334 |
> |
return value; |
335 |
> |
} |
336 |
|
} |
337 |
|
|
338 |
|
// A function that handles and produces null values as well. |
340 |
|
return (x == null) ? null : x + 1; |
341 |
|
} |
342 |
|
|
337 |
– |
static final Supplier<Integer> supplyOne = |
338 |
– |
() -> Integer.valueOf(1); |
339 |
– |
static final Function<Integer, Integer> inc = |
340 |
– |
(Integer x) -> Integer.valueOf(x.intValue() + 1); |
341 |
– |
static final BiFunction<Integer, Integer, Integer> subtract = |
342 |
– |
(Integer x, Integer y) -> subtract(x, y); |
343 |
|
static final class IncAction implements Consumer<Integer> { |
344 |
|
int invocationCount = 0; |
345 |
|
Integer value; |
359 |
|
return value = inc(x); |
360 |
|
} |
361 |
|
} |
362 |
+ |
|
363 |
+ |
// Choose non-commutative actions for better coverage |
364 |
+ |
// A non-commutative function that handles and produces null values as well. |
365 |
+ |
static Integer subtract(Integer x, Integer y) { |
366 |
+ |
return (x == null && y == null) ? null : |
367 |
+ |
((x == null) ? 42 : x.intValue()) |
368 |
+ |
- ((y == null) ? 99 : y.intValue()); |
369 |
+ |
} |
370 |
+ |
|
371 |
|
static final class SubtractAction implements BiConsumer<Integer, Integer> { |
372 |
|
final ExecutionMode m; |
373 |
|
int invocationCount = 0; |
392 |
|
return value = subtract(x, y); |
393 |
|
} |
394 |
|
} |
395 |
+ |
|
396 |
|
static final class Noop implements Runnable { |
397 |
|
final ExecutionMode m; |
398 |
|
int invocationCount = 0; |
506 |
|
|
507 |
|
/** |
508 |
|
* Permits the testing of parallel code for the 3 different |
509 |
< |
* execution modes without repeating all the testing code. |
509 |
> |
* execution modes without copy/pasting all the test methods. |
510 |
|
*/ |
511 |
|
enum ExecutionMode { |
512 |
|
DEFAULT { |
513 |
|
public void checkExecutionMode() { |
514 |
+ |
assertFalse(ThreadExecutor.startedCurrentThread()); |
515 |
|
assertNull(ForkJoinTask.getPool()); |
516 |
|
} |
517 |
+ |
public CompletableFuture<Void> runAsync(Runnable a) { |
518 |
+ |
throw new UnsupportedOperationException(); |
519 |
+ |
} |
520 |
+ |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
521 |
+ |
throw new UnsupportedOperationException(); |
522 |
+ |
} |
523 |
|
public <T> CompletableFuture<Void> thenRun |
524 |
|
(CompletableFuture<T> f, Runnable a) { |
525 |
|
return f.thenRun(a); |
588 |
|
assertSame(ForkJoinPool.commonPool(), |
589 |
|
ForkJoinTask.getPool()); |
590 |
|
} |
591 |
+ |
public CompletableFuture<Void> runAsync(Runnable a) { |
592 |
+ |
return CompletableFuture.runAsync(a); |
593 |
+ |
} |
594 |
+ |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
595 |
+ |
return CompletableFuture.supplyAsync(a); |
596 |
+ |
} |
597 |
|
public <T> CompletableFuture<Void> thenRun |
598 |
|
(CompletableFuture<T> f, Runnable a) { |
599 |
|
return f.thenRunAsync(a); |
661 |
|
public void checkExecutionMode() { |
662 |
|
assertTrue(ThreadExecutor.startedCurrentThread()); |
663 |
|
} |
664 |
+ |
public CompletableFuture<Void> runAsync(Runnable a) { |
665 |
+ |
return CompletableFuture.runAsync(a, new ThreadExecutor()); |
666 |
+ |
} |
667 |
+ |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
668 |
+ |
return CompletableFuture.supplyAsync(a, new ThreadExecutor()); |
669 |
+ |
} |
670 |
|
public <T> CompletableFuture<Void> thenRun |
671 |
|
(CompletableFuture<T> f, Runnable a) { |
672 |
|
return f.thenRunAsync(a, new ThreadExecutor()); |
731 |
|
}; |
732 |
|
|
733 |
|
public abstract void checkExecutionMode(); |
734 |
+ |
public abstract CompletableFuture<Void> runAsync(Runnable a); |
735 |
+ |
public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a); |
736 |
|
public abstract <T> CompletableFuture<Void> thenRun |
737 |
|
(CompletableFuture<T> f, Runnable a); |
738 |
|
public abstract <T> CompletableFuture<Void> thenAccept |
811 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
812 |
|
final CompletableFuture<Integer> g = f.exceptionally |
813 |
|
((Throwable t) -> { |
814 |
+ |
ExecutionMode.DEFAULT.checkExecutionMode(); |
815 |
|
threadAssertSame(t, ex); |
816 |
|
a.getAndIncrement(); |
817 |
|
return v1; |
833 |
|
if (!createIncomplete) f.completeExceptionally(ex1); |
834 |
|
final CompletableFuture<Integer> g = f.exceptionally |
835 |
|
((Throwable t) -> { |
836 |
+ |
ExecutionMode.DEFAULT.checkExecutionMode(); |
837 |
|
threadAssertSame(t, ex1); |
838 |
|
a.getAndIncrement(); |
839 |
|
throw ex2; |
859 |
|
final CompletableFuture<Integer> g = m.handle |
860 |
|
(f, |
861 |
|
(Integer x, Throwable t) -> { |
862 |
+ |
m.checkExecutionMode(); |
863 |
|
threadAssertSame(x, v1); |
864 |
|
threadAssertNull(t); |
865 |
|
a.getAndIncrement(); |
888 |
|
final CompletableFuture<Integer> g = m.handle |
889 |
|
(f, |
890 |
|
(Integer x, Throwable t) -> { |
891 |
+ |
m.checkExecutionMode(); |
892 |
|
threadAssertNull(x); |
893 |
|
threadAssertSame(t, ex); |
894 |
|
a.getAndIncrement(); |
917 |
|
final CompletableFuture<Integer> g = m.handle |
918 |
|
(f, |
919 |
|
(Integer x, Throwable t) -> { |
920 |
+ |
m.checkExecutionMode(); |
921 |
|
threadAssertNull(x); |
922 |
|
threadAssertTrue(t instanceof CancellationException); |
923 |
|
a.getAndIncrement(); |
945 |
|
final CompletableFuture<Integer> g = m.handle |
946 |
|
(f, |
947 |
|
(Integer x, Throwable t) -> { |
948 |
+ |
m.checkExecutionMode(); |
949 |
|
threadAssertNull(x); |
950 |
|
threadAssertSame(ex1, t); |
951 |
|
a.getAndIncrement(); |
970 |
|
final CompletableFuture<Integer> g = m.handle |
971 |
|
(f, |
972 |
|
(Integer x, Throwable t) -> { |
973 |
+ |
m.checkExecutionMode(); |
974 |
|
threadAssertSame(x, v1); |
975 |
|
threadAssertNull(t); |
976 |
|
a.getAndIncrement(); |
986 |
|
/** |
987 |
|
* runAsync completes after running Runnable |
988 |
|
*/ |
989 |
< |
public void testRunAsync() { |
990 |
< |
Noop r = new Noop(ExecutionMode.ASYNC); |
991 |
< |
CompletableFuture<Void> f = CompletableFuture.runAsync(r); |
989 |
> |
public void testRunAsync_normalCompletion() { |
990 |
> |
ExecutionMode[] executionModes = { |
991 |
> |
ExecutionMode.ASYNC, |
992 |
> |
ExecutionMode.EXECUTOR, |
993 |
> |
}; |
994 |
> |
for (ExecutionMode m : executionModes) |
995 |
> |
{ |
996 |
> |
final Noop r = new Noop(m); |
997 |
> |
final CompletableFuture<Void> f = m.runAsync(r); |
998 |
|
assertNull(f.join()); |
955 |
– |
assertEquals(1, r.invocationCount); |
999 |
|
checkCompletedNormally(f, null); |
957 |
– |
} |
958 |
– |
|
959 |
– |
/** |
960 |
– |
* runAsync with executor completes after running Runnable |
961 |
– |
*/ |
962 |
– |
public void testRunAsync2() { |
963 |
– |
Noop r = new Noop(ExecutionMode.EXECUTOR); |
964 |
– |
ThreadExecutor exec = new ThreadExecutor(); |
965 |
– |
CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec); |
966 |
– |
assertNull(f.join()); |
1000 |
|
assertEquals(1, r.invocationCount); |
1001 |
< |
checkCompletedNormally(f, null); |
969 |
< |
assertEquals(1, exec.count.get()); |
970 |
< |
} |
1001 |
> |
}} |
1002 |
|
|
1003 |
|
/** |
1004 |
|
* failing runAsync completes exceptionally after running Runnable |
1005 |
|
*/ |
1006 |
< |
public void testRunAsync3() { |
1007 |
< |
FailingRunnable r = new FailingRunnable(ExecutionMode.ASYNC); |
1008 |
< |
CompletableFuture<Void> f = CompletableFuture.runAsync(r); |
1006 |
> |
public void testRunAsync_exceptionalCompletion() { |
1007 |
> |
ExecutionMode[] executionModes = { |
1008 |
> |
ExecutionMode.ASYNC, |
1009 |
> |
ExecutionMode.EXECUTOR, |
1010 |
> |
}; |
1011 |
> |
for (ExecutionMode m : executionModes) |
1012 |
> |
{ |
1013 |
> |
final FailingRunnable r = new FailingRunnable(m); |
1014 |
> |
final CompletableFuture<Void> f = m.runAsync(r); |
1015 |
|
checkCompletedWithWrappedCFException(f); |
1016 |
|
assertEquals(1, r.invocationCount); |
1017 |
< |
} |
1017 |
> |
}} |
1018 |
|
|
1019 |
|
/** |
1020 |
|
* supplyAsync completes with result of supplier |
1021 |
|
*/ |
1022 |
< |
public void testSupplyAsync() { |
1023 |
< |
CompletableFuture<Integer> f; |
1024 |
< |
f = CompletableFuture.supplyAsync(supplyOne); |
1025 |
< |
assertEquals(f.join(), one); |
1026 |
< |
checkCompletedNormally(f, one); |
1027 |
< |
} |
1028 |
< |
|
1029 |
< |
/** |
1030 |
< |
* supplyAsync with executor completes with result of supplier |
1031 |
< |
*/ |
1032 |
< |
public void testSupplyAsync2() { |
1033 |
< |
CompletableFuture<Integer> f; |
1034 |
< |
f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor()); |
1035 |
< |
assertEquals(f.join(), one); |
999 |
< |
checkCompletedNormally(f, one); |
1000 |
< |
} |
1022 |
> |
public void testSupplyAsync_normalCompletion() { |
1023 |
> |
ExecutionMode[] executionModes = { |
1024 |
> |
ExecutionMode.ASYNC, |
1025 |
> |
ExecutionMode.EXECUTOR, |
1026 |
> |
}; |
1027 |
> |
for (ExecutionMode m : executionModes) |
1028 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1029 |
> |
{ |
1030 |
> |
final IntegerSupplier r = new IntegerSupplier(m, v1); |
1031 |
> |
final CompletableFuture<Integer> f = m.supplyAsync(r); |
1032 |
> |
assertSame(v1, f.join()); |
1033 |
> |
checkCompletedNormally(f, v1); |
1034 |
> |
assertEquals(1, r.invocationCount); |
1035 |
> |
}} |
1036 |
|
|
1037 |
|
/** |
1038 |
|
* Failing supplyAsync completes exceptionally |
1039 |
|
*/ |
1040 |
< |
public void testSupplyAsync3() { |
1041 |
< |
FailingSupplier r = new FailingSupplier(ExecutionMode.ASYNC); |
1042 |
< |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r); |
1040 |
> |
public void testSupplyAsync_exceptionalCompletion() { |
1041 |
> |
ExecutionMode[] executionModes = { |
1042 |
> |
ExecutionMode.ASYNC, |
1043 |
> |
ExecutionMode.EXECUTOR, |
1044 |
> |
}; |
1045 |
> |
for (ExecutionMode m : executionModes) |
1046 |
> |
{ |
1047 |
> |
FailingSupplier r = new FailingSupplier(m); |
1048 |
> |
CompletableFuture<Integer> f = m.supplyAsync(r); |
1049 |
|
checkCompletedWithWrappedCFException(f); |
1050 |
|
assertEquals(1, r.invocationCount); |
1051 |
< |
} |
1051 |
> |
}} |
1052 |
|
|
1053 |
|
// seq completion methods |
1054 |
|
|
1275 |
|
}} |
1276 |
|
|
1277 |
|
/** |
1278 |
< |
* thenAccept result completes exceptionally if action does |
1278 |
> |
* thenAccept result completes exceptionally if source cancelled |
1279 |
|
*/ |
1280 |
< |
public void testThenAccept_actionFailed() { |
1280 |
> |
public void testThenAccept_sourceCancelled() { |
1281 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1282 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1283 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1283 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1284 |
|
{ |
1285 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1286 |
< |
final FailingConsumer r = new FailingConsumer(m); |
1287 |
< |
if (!createIncomplete) f.complete(v1); |
1286 |
> |
final IncAction r = new IncAction(); |
1287 |
> |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1288 |
|
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1289 |
|
if (createIncomplete) { |
1290 |
|
checkIncomplete(g); |
1291 |
< |
f.complete(v1); |
1291 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1292 |
|
} |
1293 |
|
|
1294 |
< |
checkCompletedWithWrappedCFException(g); |
1295 |
< |
checkCompletedNormally(f, v1); |
1294 |
> |
checkCompletedWithWrappedCancellationException(g); |
1295 |
> |
checkCancelled(f); |
1296 |
> |
assertEquals(0, r.invocationCount); |
1297 |
|
}} |
1298 |
|
|
1299 |
|
/** |
1300 |
< |
* thenAccept result completes exceptionally if source cancelled |
1300 |
> |
* thenAccept result completes exceptionally if action does |
1301 |
|
*/ |
1302 |
< |
public void testThenAccept_sourceCancelled() { |
1302 |
> |
public void testThenAccept_actionFailed() { |
1303 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1304 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1305 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1305 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1306 |
|
{ |
1307 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1308 |
< |
final IncAction r = new IncAction(); |
1309 |
< |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1308 |
> |
final FailingConsumer r = new FailingConsumer(m); |
1309 |
> |
if (!createIncomplete) f.complete(v1); |
1310 |
|
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1311 |
|
if (createIncomplete) { |
1312 |
|
checkIncomplete(g); |
1313 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1313 |
> |
f.complete(v1); |
1314 |
|
} |
1315 |
|
|
1316 |
< |
checkCompletedWithWrappedCancellationException(g); |
1317 |
< |
checkCancelled(f); |
1276 |
< |
assertEquals(0, r.invocationCount); |
1316 |
> |
checkCompletedWithWrappedCFException(g); |
1317 |
> |
checkCompletedNormally(f, v1); |
1318 |
|
}} |
1319 |
|
|
1320 |
|
/** |
1379 |
|
}} |
1380 |
|
|
1381 |
|
/** |
1341 |
– |
* thenCombine result completes exceptionally if action does |
1342 |
– |
*/ |
1343 |
– |
public void testThenCombine_actionFailed() { |
1344 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
1345 |
– |
for (boolean fFirst : new boolean[] { true, false }) |
1346 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
1347 |
– |
for (Integer v2 : new Integer[] { 2, null }) |
1348 |
– |
{ |
1349 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1350 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1351 |
– |
final FailingBiFunction r = new FailingBiFunction(m); |
1352 |
– |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1353 |
– |
|
1354 |
– |
if (fFirst) { |
1355 |
– |
f.complete(v1); |
1356 |
– |
g.complete(v2); |
1357 |
– |
} else { |
1358 |
– |
g.complete(v2); |
1359 |
– |
f.complete(v1); |
1360 |
– |
} |
1361 |
– |
|
1362 |
– |
checkCompletedWithWrappedCFException(h); |
1363 |
– |
checkCompletedNormally(f, v1); |
1364 |
– |
checkCompletedNormally(g, v2); |
1365 |
– |
}} |
1366 |
– |
|
1367 |
– |
/** |
1382 |
|
* thenCombine result completes exceptionally if either source cancelled |
1383 |
|
*/ |
1384 |
|
public void testThenCombine_sourceCancelled() { |
1408 |
|
}} |
1409 |
|
|
1410 |
|
/** |
1411 |
+ |
* thenCombine result completes exceptionally if action does |
1412 |
+ |
*/ |
1413 |
+ |
public void testThenCombine_actionFailed() { |
1414 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
1415 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
1416 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
1417 |
+ |
for (Integer v2 : new Integer[] { 2, null }) |
1418 |
+ |
{ |
1419 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1420 |
+ |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1421 |
+ |
final FailingBiFunction r = new FailingBiFunction(m); |
1422 |
+ |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1423 |
+ |
|
1424 |
+ |
if (fFirst) { |
1425 |
+ |
f.complete(v1); |
1426 |
+ |
g.complete(v2); |
1427 |
+ |
} else { |
1428 |
+ |
g.complete(v2); |
1429 |
+ |
f.complete(v1); |
1430 |
+ |
} |
1431 |
+ |
|
1432 |
+ |
checkCompletedWithWrappedCFException(h); |
1433 |
+ |
checkCompletedNormally(f, v1); |
1434 |
+ |
checkCompletedNormally(g, v2); |
1435 |
+ |
}} |
1436 |
+ |
|
1437 |
+ |
/** |
1438 |
|
* thenAcceptBoth result completes normally after normal |
1439 |
|
* completion of sources |
1440 |
|
*/ |
1496 |
|
}} |
1497 |
|
|
1498 |
|
/** |
1458 |
– |
* thenAcceptBoth result completes exceptionally if action does |
1459 |
– |
*/ |
1460 |
– |
public void testThenAcceptBoth_actionFailed() { |
1461 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
1462 |
– |
for (boolean fFirst : new boolean[] { true, false }) |
1463 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
1464 |
– |
for (Integer v2 : new Integer[] { 2, null }) |
1465 |
– |
{ |
1466 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1467 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1468 |
– |
final FailingBiConsumer r = new FailingBiConsumer(m); |
1469 |
– |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1470 |
– |
|
1471 |
– |
if (fFirst) { |
1472 |
– |
f.complete(v1); |
1473 |
– |
g.complete(v2); |
1474 |
– |
} else { |
1475 |
– |
g.complete(v2); |
1476 |
– |
f.complete(v1); |
1477 |
– |
} |
1478 |
– |
|
1479 |
– |
checkCompletedWithWrappedCFException(h); |
1480 |
– |
checkCompletedNormally(f, v1); |
1481 |
– |
checkCompletedNormally(g, v2); |
1482 |
– |
}} |
1483 |
– |
|
1484 |
– |
/** |
1499 |
|
* thenAcceptBoth result completes exceptionally if either source cancelled |
1500 |
|
*/ |
1501 |
|
public void testThenAcceptBoth_sourceCancelled() { |
1525 |
|
}} |
1526 |
|
|
1527 |
|
/** |
1528 |
+ |
* thenAcceptBoth result completes exceptionally if action does |
1529 |
+ |
*/ |
1530 |
+ |
public void testThenAcceptBoth_actionFailed() { |
1531 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
1532 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
1533 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
1534 |
+ |
for (Integer v2 : new Integer[] { 2, null }) |
1535 |
+ |
{ |
1536 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1537 |
+ |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1538 |
+ |
final FailingBiConsumer r = new FailingBiConsumer(m); |
1539 |
+ |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1540 |
+ |
|
1541 |
+ |
if (fFirst) { |
1542 |
+ |
f.complete(v1); |
1543 |
+ |
g.complete(v2); |
1544 |
+ |
} else { |
1545 |
+ |
g.complete(v2); |
1546 |
+ |
f.complete(v1); |
1547 |
+ |
} |
1548 |
+ |
|
1549 |
+ |
checkCompletedWithWrappedCFException(h); |
1550 |
+ |
checkCompletedNormally(f, v1); |
1551 |
+ |
checkCompletedNormally(g, v2); |
1552 |
+ |
}} |
1553 |
+ |
|
1554 |
+ |
/** |
1555 |
|
* runAfterBoth result completes normally after normal |
1556 |
|
* completion of sources |
1557 |
|
*/ |
1613 |
|
}} |
1614 |
|
|
1615 |
|
/** |
1575 |
– |
* runAfterBoth result completes exceptionally if action does |
1576 |
– |
*/ |
1577 |
– |
public void testRunAfterBoth_actionFailed() { |
1578 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
1579 |
– |
for (boolean fFirst : new boolean[] { true, false }) |
1580 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
1581 |
– |
for (Integer v2 : new Integer[] { 2, null }) |
1582 |
– |
{ |
1583 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1584 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1585 |
– |
final FailingRunnable r = new FailingRunnable(m); |
1586 |
– |
|
1587 |
– |
CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r); |
1588 |
– |
if (fFirst) { |
1589 |
– |
f.complete(v1); |
1590 |
– |
g.complete(v2); |
1591 |
– |
} else { |
1592 |
– |
g.complete(v2); |
1593 |
– |
f.complete(v1); |
1594 |
– |
} |
1595 |
– |
CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r); |
1596 |
– |
|
1597 |
– |
checkCompletedWithWrappedCFException(h1); |
1598 |
– |
checkCompletedWithWrappedCFException(h2); |
1599 |
– |
checkCompletedNormally(f, v1); |
1600 |
– |
checkCompletedNormally(g, v2); |
1601 |
– |
}} |
1602 |
– |
|
1603 |
– |
/** |
1616 |
|
* runAfterBoth result completes exceptionally if either source cancelled |
1617 |
|
*/ |
1618 |
|
public void testRunAfterBoth_sourceCancelled() { |
1643 |
|
}} |
1644 |
|
|
1645 |
|
/** |
1646 |
+ |
* runAfterBoth result completes exceptionally if action does |
1647 |
+ |
*/ |
1648 |
+ |
public void testRunAfterBoth_actionFailed() { |
1649 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
1650 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
1651 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
1652 |
+ |
for (Integer v2 : new Integer[] { 2, null }) |
1653 |
+ |
{ |
1654 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1655 |
+ |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1656 |
+ |
final FailingRunnable r = new FailingRunnable(m); |
1657 |
+ |
|
1658 |
+ |
CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r); |
1659 |
+ |
if (fFirst) { |
1660 |
+ |
f.complete(v1); |
1661 |
+ |
g.complete(v2); |
1662 |
+ |
} else { |
1663 |
+ |
g.complete(v2); |
1664 |
+ |
f.complete(v1); |
1665 |
+ |
} |
1666 |
+ |
CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r); |
1667 |
+ |
|
1668 |
+ |
checkCompletedWithWrappedCFException(h1); |
1669 |
+ |
checkCompletedWithWrappedCFException(h2); |
1670 |
+ |
checkCompletedNormally(f, v1); |
1671 |
+ |
checkCompletedNormally(g, v2); |
1672 |
+ |
}} |
1673 |
+ |
|
1674 |
+ |
/** |
1675 |
|
* applyToEither result completes normally after normal completion |
1676 |
|
* of either source |
1677 |
|
*/ |
2616 |
|
*/ |
2617 |
|
public void testAllOf_normal() throws Exception { |
2618 |
|
for (int k = 1; k < 20; ++k) { |
2619 |
< |
CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
2619 |
> |
CompletableFuture<Integer>[] fs |
2620 |
> |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
2621 |
|
for (int i = 0; i < k; ++i) |
2622 |
|
fs[i] = new CompletableFuture<>(); |
2623 |
|
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
2631 |
|
} |
2632 |
|
} |
2633 |
|
|
2634 |
+ |
public void testAllOf_backwards() throws Exception { |
2635 |
+ |
for (int k = 1; k < 20; ++k) { |
2636 |
+ |
CompletableFuture<Integer>[] fs |
2637 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
2638 |
+ |
for (int i = 0; i < k; ++i) |
2639 |
+ |
fs[i] = new CompletableFuture<>(); |
2640 |
+ |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
2641 |
+ |
for (int i = k - 1; i >= 0; i--) { |
2642 |
+ |
checkIncomplete(f); |
2643 |
+ |
checkIncomplete(CompletableFuture.allOf(fs)); |
2644 |
+ |
fs[i].complete(one); |
2645 |
+ |
} |
2646 |
+ |
checkCompletedNormally(f, null); |
2647 |
+ |
checkCompletedNormally(CompletableFuture.allOf(fs), null); |
2648 |
+ |
} |
2649 |
+ |
} |
2650 |
+ |
|
2651 |
|
/** |
2652 |
|
* anyOf(no component futures) returns an incomplete future |
2653 |
|
*/ |
2706 |
|
Runnable[] throwingActions = { |
2707 |
|
() -> CompletableFuture.supplyAsync(null), |
2708 |
|
() -> CompletableFuture.supplyAsync(null, exec), |
2709 |
< |
() -> CompletableFuture.supplyAsync(supplyOne, null), |
2709 |
> |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null), |
2710 |
|
|
2711 |
|
() -> CompletableFuture.runAsync(null), |
2712 |
|
() -> CompletableFuture.runAsync(null, exec), |