16 |
|
import java.util.concurrent.Future; |
17 |
|
import java.util.concurrent.CompletableFuture; |
18 |
|
import java.util.concurrent.CompletionException; |
19 |
+ |
import java.util.concurrent.CompletionStage; |
20 |
|
import java.util.concurrent.TimeoutException; |
21 |
|
import java.util.concurrent.atomic.AtomicInteger; |
22 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
329 |
|
public void accept(Integer x) { value = x.intValue() + 1; } |
330 |
|
} |
331 |
|
static final class SubtractAction implements BiConsumer<Integer, Integer> { |
332 |
+ |
// Handle null values as well |
333 |
+ |
public int subtract(Integer x, Integer y) { |
334 |
+ |
return ((x == null) ? 42 : x.intValue()) |
335 |
+ |
- ((y == null) ? 99 : y.intValue()); |
336 |
+ |
} |
337 |
|
int value; |
338 |
+ |
public boolean ran() { return value != 0; } |
339 |
|
public void accept(Integer x, Integer y) { |
340 |
< |
value = x.intValue() - y.intValue(); |
340 |
> |
value = subtract(x, y); |
341 |
|
} |
342 |
|
} |
343 |
|
static final class Noop implements Runnable { |
412 |
|
} |
413 |
|
|
414 |
|
/** |
415 |
+ |
* Permits the testing of parallel code for the 3 different |
416 |
+ |
* execution modes without repeating all the testing code. |
417 |
+ |
*/ |
418 |
+ |
enum ExecutionMode { |
419 |
+ |
DEFAULT { |
420 |
+ |
public <T,U> CompletableFuture<Void> runAfterBoth |
421 |
+ |
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) { |
422 |
+ |
return f.runAfterBoth(g, a); |
423 |
+ |
} |
424 |
+ |
public <T,U> CompletableFuture<Void> thenAcceptBoth |
425 |
+ |
(CompletableFuture<T> f, |
426 |
+ |
CompletionStage<? extends U> g, |
427 |
+ |
BiConsumer<? super T,? super U> a) { |
428 |
+ |
return f.thenAcceptBoth(g, a); |
429 |
+ |
} |
430 |
+ |
}, |
431 |
+ |
|
432 |
+ |
// /** Experimental way to do more testing */ |
433 |
+ |
// REVERSE_DEFAULT { |
434 |
+ |
// public <T,U> CompletableFuture<Void> runAfterBoth |
435 |
+ |
// (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) { |
436 |
+ |
// return g.runAfterBoth(f, a); |
437 |
+ |
// } |
438 |
+ |
// public <T,U> CompletableFuture<Void> thenAcceptBoth |
439 |
+ |
// (CompletableFuture<T> f, |
440 |
+ |
// CompletionStage<? extends U> g, |
441 |
+ |
// BiConsumer<? super T,? super U> a) { |
442 |
+ |
// return DEFAULT.thenAcceptBoth(f, g, a); |
443 |
+ |
// } |
444 |
+ |
// }, |
445 |
+ |
|
446 |
+ |
DEFAULT_ASYNC { |
447 |
+ |
public <T,U> CompletableFuture<Void> runAfterBoth |
448 |
+ |
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) { |
449 |
+ |
return f.runAfterBothAsync(g, a); |
450 |
+ |
} |
451 |
+ |
public <T,U> CompletableFuture<Void> thenAcceptBoth |
452 |
+ |
(CompletableFuture<T> f, |
453 |
+ |
CompletionStage<? extends U> g, |
454 |
+ |
BiConsumer<? super T,? super U> a) { |
455 |
+ |
return f.thenAcceptBothAsync(g, a); |
456 |
+ |
} |
457 |
+ |
}, |
458 |
+ |
|
459 |
+ |
// REVERSE_DEFAULT_ASYNC { |
460 |
+ |
// public <T,U> CompletableFuture<Void> runAfterBoth |
461 |
+ |
// (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) { |
462 |
+ |
// return f.runAfterBothAsync(g, a); |
463 |
+ |
// } |
464 |
+ |
// public <T,U> CompletableFuture<Void> thenAcceptBoth |
465 |
+ |
// (CompletableFuture<T> f, |
466 |
+ |
// CompletionStage<? extends U> g, |
467 |
+ |
// BiConsumer<? super T,? super U> a) { |
468 |
+ |
// return DEFAULT_ASYNC.thenAcceptBoth(f, g, a); |
469 |
+ |
// } |
470 |
+ |
// }, |
471 |
+ |
|
472 |
+ |
EXECUTOR { |
473 |
+ |
public <T,U> CompletableFuture<Void> runAfterBoth |
474 |
+ |
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) { |
475 |
+ |
return f.runAfterBothAsync(g, a, new ThreadExecutor()); |
476 |
+ |
} |
477 |
+ |
public <T,U> CompletableFuture<Void> thenAcceptBoth |
478 |
+ |
(CompletableFuture<T> f, |
479 |
+ |
CompletionStage<? extends U> g, |
480 |
+ |
BiConsumer<? super T,? super U> a) { |
481 |
+ |
return f.thenAcceptBothAsync(g, a, new ThreadExecutor()); |
482 |
+ |
} |
483 |
+ |
}; |
484 |
+ |
|
485 |
+ |
public abstract <T,U> CompletableFuture<Void> runAfterBoth |
486 |
+ |
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable a); |
487 |
+ |
public abstract <T,U> CompletableFuture<Void> thenAcceptBoth |
488 |
+ |
(CompletableFuture<T> f, |
489 |
+ |
CompletionStage<? extends U> g, |
490 |
+ |
BiConsumer<? super T,? super U> a); |
491 |
+ |
} |
492 |
+ |
|
493 |
+ |
/** |
494 |
|
* exceptionally action completes with function value on source |
495 |
|
* exception; otherwise with source value |
496 |
|
*/ |
897 |
|
* thenAcceptBoth result completes normally after normal |
898 |
|
* completion of sources |
899 |
|
*/ |
900 |
< |
public void testThenAcceptBoth() { |
901 |
< |
CompletableFuture<Integer> f, g; |
902 |
< |
CompletableFuture<Void> h; |
903 |
< |
SubtractAction r; |
900 |
> |
public void testThenAcceptBoth_normalCompletion1() { |
901 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
902 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
903 |
> |
for (Integer v2 : new Integer[] { 2, null }) { |
904 |
|
|
905 |
< |
f = new CompletableFuture<>(); |
906 |
< |
g = new CompletableFuture<>(); |
907 |
< |
h = f.thenAcceptBoth(g, r = new SubtractAction()); |
908 |
< |
f.complete(3); |
905 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
906 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
907 |
> |
final SubtractAction r = new SubtractAction(); |
908 |
> |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
909 |
> |
|
910 |
> |
f.complete(v1); |
911 |
|
checkIncomplete(h); |
912 |
< |
g.complete(1); |
912 |
> |
assertEquals(r.value, 0); |
913 |
> |
g.complete(v2); |
914 |
> |
|
915 |
|
checkCompletedNormally(h, null); |
916 |
< |
assertEquals(r.value, 2); |
916 |
> |
assertEquals(r.value, r.subtract(v1, v2)); |
917 |
> |
checkCompletedNormally(f, v1); |
918 |
> |
checkCompletedNormally(g, v2); |
919 |
> |
} |
920 |
> |
} |
921 |
|
|
922 |
< |
f = new CompletableFuture<>(); |
923 |
< |
g = new CompletableFuture<>(); |
924 |
< |
h = f.thenAcceptBoth(g, r = new SubtractAction()); |
925 |
< |
g.complete(1); |
922 |
> |
public void testThenAcceptBoth_normalCompletion2() { |
923 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
924 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
925 |
> |
for (Integer v2 : new Integer[] { 2, null }) { |
926 |
> |
|
927 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
928 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
929 |
> |
final SubtractAction r = new SubtractAction(); |
930 |
> |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
931 |
> |
|
932 |
> |
g.complete(v2); |
933 |
|
checkIncomplete(h); |
934 |
< |
f.complete(3); |
934 |
> |
assertEquals(r.value, 0); |
935 |
> |
f.complete(v1); |
936 |
> |
|
937 |
|
checkCompletedNormally(h, null); |
938 |
< |
assertEquals(r.value, 2); |
938 |
> |
assertEquals(r.value, r.subtract(v1, v2)); |
939 |
> |
checkCompletedNormally(f, v1); |
940 |
> |
checkCompletedNormally(g, v2); |
941 |
> |
} |
942 |
> |
} |
943 |
> |
|
944 |
> |
public void testThenAcceptBoth_normalCompletion3() { |
945 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
946 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
947 |
> |
for (Integer v2 : new Integer[] { 2, null }) { |
948 |
> |
|
949 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
950 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
951 |
> |
final SubtractAction r = new SubtractAction(); |
952 |
> |
|
953 |
> |
g.complete(v2); |
954 |
> |
f.complete(v1); |
955 |
> |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
956 |
|
|
837 |
– |
f = new CompletableFuture<>(); |
838 |
– |
g = new CompletableFuture<>(); |
839 |
– |
g.complete(1); |
840 |
– |
f.complete(3); |
841 |
– |
h = f.thenAcceptBoth(g, r = new SubtractAction()); |
957 |
|
checkCompletedNormally(h, null); |
958 |
< |
assertEquals(r.value, 2); |
958 |
> |
assertEquals(r.value, r.subtract(v1, v2)); |
959 |
> |
checkCompletedNormally(f, v1); |
960 |
> |
checkCompletedNormally(g, v2); |
961 |
> |
} |
962 |
> |
} |
963 |
> |
|
964 |
> |
public void testThenAcceptBoth_normalCompletion4() { |
965 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
966 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
967 |
> |
for (Integer v2 : new Integer[] { 2, null }) { |
968 |
> |
|
969 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
970 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
971 |
> |
final SubtractAction r = new SubtractAction(); |
972 |
> |
|
973 |
> |
f.complete(v1); |
974 |
> |
g.complete(v2); |
975 |
> |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
976 |
> |
|
977 |
> |
checkCompletedNormally(h, null); |
978 |
> |
assertEquals(r.value, r.subtract(v1, v2)); |
979 |
> |
checkCompletedNormally(f, v1); |
980 |
> |
checkCompletedNormally(g, v2); |
981 |
> |
} |
982 |
|
} |
983 |
|
|
984 |
|
/** |
985 |
|
* thenAcceptBoth result completes exceptionally after exceptional |
986 |
|
* completion of either source |
987 |
|
*/ |
988 |
< |
public void testThenAcceptBoth2() { |
989 |
< |
CompletableFuture<Integer> f, g; |
990 |
< |
CompletableFuture<Void> h; |
853 |
< |
SubtractAction r; |
988 |
> |
public void testThenAcceptBoth_exceptionalCompletion1() { |
989 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
990 |
> |
for (Integer v1 : new Integer[] { 1, null }) { |
991 |
|
|
992 |
< |
f = new CompletableFuture<>(); |
993 |
< |
g = new CompletableFuture<>(); |
994 |
< |
h = f.thenAcceptBoth(g, r = new SubtractAction()); |
995 |
< |
f.completeExceptionally(new CFException()); |
992 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
993 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
994 |
> |
final SubtractAction r = new SubtractAction(); |
995 |
> |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
996 |
> |
final CFException ex = new CFException(); |
997 |
> |
|
998 |
> |
f.completeExceptionally(ex); |
999 |
|
checkIncomplete(h); |
1000 |
< |
g.complete(1); |
861 |
< |
checkCompletedWithWrappedCFException(h); |
1000 |
> |
g.complete(v1); |
1001 |
|
|
1002 |
< |
f = new CompletableFuture<>(); |
1003 |
< |
g = new CompletableFuture<>(); |
1004 |
< |
h = f.thenAcceptBoth(g, r = new SubtractAction()); |
1005 |
< |
g.completeExceptionally(new CFException()); |
1002 |
> |
checkCompletedWithWrappedCFException(h, ex); |
1003 |
> |
checkCompletedWithWrappedCFException(f, ex); |
1004 |
> |
assertFalse(r.ran()); |
1005 |
> |
checkCompletedNormally(g, v1); |
1006 |
> |
} |
1007 |
> |
} |
1008 |
> |
|
1009 |
> |
public void testThenAcceptBoth_exceptionalCompletion2() { |
1010 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1011 |
> |
for (Integer v1 : new Integer[] { 1, null }) { |
1012 |
> |
|
1013 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1014 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1015 |
> |
final SubtractAction r = new SubtractAction(); |
1016 |
> |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1017 |
> |
final CFException ex = new CFException(); |
1018 |
> |
|
1019 |
> |
g.completeExceptionally(ex); |
1020 |
|
checkIncomplete(h); |
1021 |
< |
f.complete(3); |
869 |
< |
checkCompletedWithWrappedCFException(h); |
1021 |
> |
f.complete(v1); |
1022 |
|
|
1023 |
< |
f = new CompletableFuture<>(); |
1024 |
< |
g = new CompletableFuture<>(); |
1025 |
< |
f.complete(3); |
1026 |
< |
g.completeExceptionally(new CFException()); |
1027 |
< |
h = f.thenAcceptBoth(g, r = new SubtractAction()); |
1028 |
< |
checkCompletedWithWrappedCFException(h); |
1023 |
> |
checkCompletedWithWrappedCFException(h, ex); |
1024 |
> |
checkCompletedWithWrappedCFException(g, ex); |
1025 |
> |
assertFalse(r.ran()); |
1026 |
> |
checkCompletedNormally(f, v1); |
1027 |
> |
} |
1028 |
> |
} |
1029 |
|
|
1030 |
< |
f = new CompletableFuture<>(); |
1031 |
< |
g = new CompletableFuture<>(); |
1032 |
< |
f.completeExceptionally(new CFException()); |
1033 |
< |
g.complete(3); |
1034 |
< |
h = f.thenAcceptBoth(g, r = new SubtractAction()); |
1035 |
< |
checkCompletedWithWrappedCFException(h); |
1030 |
> |
public void testThenAcceptBoth_exceptionalCompletion3() { |
1031 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1032 |
> |
for (Integer v1 : new Integer[] { 1, null }) { |
1033 |
> |
|
1034 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1035 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1036 |
> |
final SubtractAction r = new SubtractAction(); |
1037 |
> |
final CFException ex = new CFException(); |
1038 |
> |
|
1039 |
> |
g.completeExceptionally(ex); |
1040 |
> |
f.complete(v1); |
1041 |
> |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1042 |
> |
|
1043 |
> |
checkCompletedWithWrappedCFException(h, ex); |
1044 |
> |
checkCompletedWithWrappedCFException(g, ex); |
1045 |
> |
assertFalse(r.ran()); |
1046 |
> |
checkCompletedNormally(f, v1); |
1047 |
> |
} |
1048 |
> |
} |
1049 |
> |
|
1050 |
> |
public void testThenAcceptBoth_exceptionalCompletion4() { |
1051 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1052 |
> |
for (Integer v1 : new Integer[] { 1, null }) { |
1053 |
> |
|
1054 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1055 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1056 |
> |
final SubtractAction r = new SubtractAction(); |
1057 |
> |
final CFException ex = new CFException(); |
1058 |
> |
|
1059 |
> |
f.completeExceptionally(ex); |
1060 |
> |
g.complete(v1); |
1061 |
> |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1062 |
> |
|
1063 |
> |
checkCompletedWithWrappedCFException(h, ex); |
1064 |
> |
checkCompletedWithWrappedCFException(f, ex); |
1065 |
> |
assertFalse(r.ran()); |
1066 |
> |
checkCompletedNormally(g, v1); |
1067 |
> |
} |
1068 |
|
} |
1069 |
|
|
1070 |
|
/** |
1071 |
|
* thenAcceptBoth result completes exceptionally if action does |
1072 |
|
*/ |
1073 |
< |
public void testThenAcceptBoth3() { |
1074 |
< |
CompletableFuture<Integer> f, g; |
1075 |
< |
CompletableFuture<Void> h; |
1076 |
< |
FailingBiConsumer r; |
1073 |
> |
public void testThenAcceptBoth_actionFailed1() { |
1074 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1075 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1076 |
> |
for (Integer v2 : new Integer[] { 2, null }) { |
1077 |
|
|
1078 |
< |
f = new CompletableFuture<>(); |
1079 |
< |
g = new CompletableFuture<>(); |
1080 |
< |
h = f.thenAcceptBoth(g, r = new FailingBiConsumer()); |
1081 |
< |
f.complete(3); |
1078 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1079 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1080 |
> |
final FailingBiConsumer r = new FailingBiConsumer(); |
1081 |
> |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1082 |
> |
|
1083 |
> |
f.complete(v1); |
1084 |
|
checkIncomplete(h); |
1085 |
< |
g.complete(1); |
1085 |
> |
g.complete(v2); |
1086 |
> |
|
1087 |
|
checkCompletedWithWrappedCFException(h); |
1088 |
+ |
checkCompletedNormally(f, v1); |
1089 |
+ |
checkCompletedNormally(g, v2); |
1090 |
+ |
} |
1091 |
+ |
} |
1092 |
+ |
|
1093 |
+ |
public void testThenAcceptBoth_actionFailed2() { |
1094 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
1095 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
1096 |
+ |
for (Integer v2 : new Integer[] { 2, null }) { |
1097 |
+ |
|
1098 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1099 |
+ |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1100 |
+ |
final FailingBiConsumer r = new FailingBiConsumer(); |
1101 |
+ |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1102 |
+ |
|
1103 |
+ |
g.complete(v2); |
1104 |
+ |
checkIncomplete(h); |
1105 |
+ |
f.complete(v1); |
1106 |
|
|
902 |
– |
f = new CompletableFuture<>(); |
903 |
– |
g = new CompletableFuture<>(); |
904 |
– |
f.complete(3); |
905 |
– |
g.complete(1); |
906 |
– |
h = f.thenAcceptBoth(g, r = new FailingBiConsumer()); |
1107 |
|
checkCompletedWithWrappedCFException(h); |
1108 |
+ |
checkCompletedNormally(f, v1); |
1109 |
+ |
checkCompletedNormally(g, v2); |
1110 |
+ |
} |
1111 |
|
} |
1112 |
|
|
1113 |
|
/** |
1114 |
|
* thenAcceptBoth result completes exceptionally if either source cancelled |
1115 |
|
*/ |
1116 |
< |
public void testThenAcceptBoth4() { |
1117 |
< |
CompletableFuture<Integer> f, g; |
1118 |
< |
CompletableFuture<Void> h; |
1119 |
< |
SubtractAction r; |
1116 |
> |
public void testThenAcceptBoth_sourceCancelled1() { |
1117 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1118 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1119 |
> |
for (Integer v1 : new Integer[] { 1, null }) { |
1120 |
|
|
1121 |
< |
f = new CompletableFuture<>(); |
1122 |
< |
g = new CompletableFuture<>(); |
1123 |
< |
h = f.thenAcceptBoth(g, r = new SubtractAction()); |
1124 |
< |
assertTrue(f.cancel(true)); |
1121 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1122 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1123 |
> |
final SubtractAction r = new SubtractAction(); |
1124 |
> |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1125 |
> |
|
1126 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1127 |
|
checkIncomplete(h); |
1128 |
< |
g.complete(1); |
1128 |
> |
g.complete(v1); |
1129 |
> |
|
1130 |
|
checkCompletedWithWrappedCancellationException(h); |
1131 |
+ |
checkCancelled(f); |
1132 |
+ |
assertFalse(r.ran()); |
1133 |
+ |
checkCompletedNormally(g, v1); |
1134 |
+ |
} |
1135 |
+ |
} |
1136 |
|
|
1137 |
< |
f = new CompletableFuture<>(); |
1138 |
< |
g = new CompletableFuture<>(); |
1139 |
< |
h = f.thenAcceptBoth(g, r = new SubtractAction()); |
1140 |
< |
assertTrue(g.cancel(true)); |
1137 |
> |
public void testThenAcceptBoth_sourceCancelled2() { |
1138 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1139 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1140 |
> |
for (Integer v1 : new Integer[] { 1, null }) { |
1141 |
> |
|
1142 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1143 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1144 |
> |
final SubtractAction r = new SubtractAction(); |
1145 |
> |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1146 |
> |
|
1147 |
> |
assertTrue(g.cancel(mayInterruptIfRunning)); |
1148 |
|
checkIncomplete(h); |
1149 |
< |
f.complete(3); |
932 |
< |
checkCompletedWithWrappedCancellationException(h); |
1149 |
> |
f.complete(v1); |
1150 |
|
|
934 |
– |
f = new CompletableFuture<>(); |
935 |
– |
g = new CompletableFuture<>(); |
936 |
– |
f.complete(3); |
937 |
– |
assertTrue(g.cancel(true)); |
938 |
– |
h = f.thenAcceptBoth(g, r = new SubtractAction()); |
1151 |
|
checkCompletedWithWrappedCancellationException(h); |
1152 |
+ |
checkCancelled(g); |
1153 |
+ |
assertFalse(r.ran()); |
1154 |
+ |
checkCompletedNormally(f, v1); |
1155 |
+ |
} |
1156 |
+ |
} |
1157 |
+ |
|
1158 |
+ |
public void testThenAcceptBoth_sourceCancelled3() { |
1159 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
1160 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1161 |
+ |
for (Integer v1 : new Integer[] { 1, null }) { |
1162 |
+ |
|
1163 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1164 |
+ |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1165 |
+ |
final SubtractAction r = new SubtractAction(); |
1166 |
+ |
|
1167 |
+ |
assertTrue(g.cancel(mayInterruptIfRunning)); |
1168 |
+ |
f.complete(v1); |
1169 |
+ |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1170 |
|
|
941 |
– |
f = new CompletableFuture<>(); |
942 |
– |
g = new CompletableFuture<>(); |
943 |
– |
assertTrue(f.cancel(true)); |
944 |
– |
g.complete(3); |
945 |
– |
h = f.thenAcceptBoth(g, r = new SubtractAction()); |
1171 |
|
checkCompletedWithWrappedCancellationException(h); |
1172 |
+ |
checkCancelled(g); |
1173 |
+ |
assertFalse(r.ran()); |
1174 |
+ |
checkCompletedNormally(f, v1); |
1175 |
+ |
} |
1176 |
|
} |
1177 |
|
|
1178 |
< |
/** |
1179 |
< |
* Permits the testing of parallel code for the 3 different |
1180 |
< |
* execution modes without repeating all the testing code. |
1181 |
< |
*/ |
953 |
< |
enum ExecutionMode { |
954 |
< |
DEFAULT { |
955 |
< |
public <T,U> CompletableFuture<Void> runAfterBoth |
956 |
< |
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable r) { |
957 |
< |
return f.runAfterBoth(g, r); |
958 |
< |
} |
959 |
< |
}, |
1178 |
> |
public void testThenAcceptBoth_sourceCancelled4() { |
1179 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1180 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1181 |
> |
for (Integer v1 : new Integer[] { 1, null }) { |
1182 |
|
|
1183 |
< |
DEFAULT_ASYNC { |
1184 |
< |
public <T,U> CompletableFuture<Void> runAfterBoth |
1185 |
< |
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable r) { |
964 |
< |
return f.runAfterBothAsync(g, r); |
965 |
< |
} |
966 |
< |
}, |
1183 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1184 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1185 |
> |
final SubtractAction r = new SubtractAction(); |
1186 |
|
|
1187 |
< |
EXECUTOR { |
1188 |
< |
public <T,U> CompletableFuture<Void> runAfterBoth |
1189 |
< |
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable r) { |
971 |
< |
return f.runAfterBothAsync(g, r, new ThreadExecutor()); |
972 |
< |
} |
973 |
< |
}; |
1187 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1188 |
> |
g.complete(v1); |
1189 |
> |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1190 |
|
|
1191 |
< |
public abstract <T,U> CompletableFuture<Void> runAfterBoth |
1192 |
< |
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable r); |
1191 |
> |
checkCompletedWithWrappedCancellationException(h); |
1192 |
> |
checkCancelled(f); |
1193 |
> |
assertFalse(r.ran()); |
1194 |
> |
checkCompletedNormally(g, v1); |
1195 |
> |
} |
1196 |
|
} |
1197 |
|
|
1198 |
|
/** |
2047 |
|
} |
2048 |
|
|
2049 |
|
/** |
1831 |
– |
* thenAcceptBothAsync result completes normally after normal |
1832 |
– |
* completion of sources |
1833 |
– |
*/ |
1834 |
– |
public void testThenAcceptBothAsync() { |
1835 |
– |
CompletableFuture<Integer> f, g; |
1836 |
– |
CompletableFuture<Void> h; |
1837 |
– |
SubtractAction r; |
1838 |
– |
|
1839 |
– |
f = new CompletableFuture<>(); |
1840 |
– |
g = new CompletableFuture<>(); |
1841 |
– |
h = f.thenAcceptBothAsync(g, r = new SubtractAction()); |
1842 |
– |
f.complete(3); |
1843 |
– |
checkIncomplete(h); |
1844 |
– |
g.complete(1); |
1845 |
– |
checkCompletedNormally(h, null); |
1846 |
– |
assertEquals(r.value, 2); |
1847 |
– |
|
1848 |
– |
f = new CompletableFuture<>(); |
1849 |
– |
g = new CompletableFuture<>(); |
1850 |
– |
h = f.thenAcceptBothAsync(g, r = new SubtractAction()); |
1851 |
– |
g.complete(1); |
1852 |
– |
checkIncomplete(h); |
1853 |
– |
f.complete(3); |
1854 |
– |
checkCompletedNormally(h, null); |
1855 |
– |
assertEquals(r.value, 2); |
1856 |
– |
|
1857 |
– |
f = new CompletableFuture<>(); |
1858 |
– |
g = new CompletableFuture<>(); |
1859 |
– |
g.complete(1); |
1860 |
– |
f.complete(3); |
1861 |
– |
h = f.thenAcceptBothAsync(g, r = new SubtractAction()); |
1862 |
– |
checkCompletedNormally(h, null); |
1863 |
– |
assertEquals(r.value, 2); |
1864 |
– |
} |
1865 |
– |
|
1866 |
– |
/** |
1867 |
– |
* thenAcceptBothAsync result completes exceptionally after exceptional |
1868 |
– |
* completion of source |
1869 |
– |
*/ |
1870 |
– |
public void testThenAcceptBothAsync2() { |
1871 |
– |
CompletableFuture<Integer> f, g; |
1872 |
– |
CompletableFuture<Void> h; |
1873 |
– |
SubtractAction r; |
1874 |
– |
|
1875 |
– |
f = new CompletableFuture<>(); |
1876 |
– |
g = new CompletableFuture<>(); |
1877 |
– |
h = f.thenAcceptBothAsync(g, r = new SubtractAction()); |
1878 |
– |
f.completeExceptionally(new CFException()); |
1879 |
– |
checkIncomplete(h); |
1880 |
– |
g.complete(1); |
1881 |
– |
checkCompletedWithWrappedCFException(h); |
1882 |
– |
|
1883 |
– |
f = new CompletableFuture<>(); |
1884 |
– |
g = new CompletableFuture<>(); |
1885 |
– |
h = f.thenAcceptBothAsync(g, r = new SubtractAction()); |
1886 |
– |
g.completeExceptionally(new CFException()); |
1887 |
– |
checkIncomplete(h); |
1888 |
– |
f.complete(3); |
1889 |
– |
checkCompletedWithWrappedCFException(h); |
1890 |
– |
|
1891 |
– |
f = new CompletableFuture<>(); |
1892 |
– |
g = new CompletableFuture<>(); |
1893 |
– |
f.complete(3); |
1894 |
– |
g.completeExceptionally(new CFException()); |
1895 |
– |
h = f.thenAcceptBothAsync(g, r = new SubtractAction()); |
1896 |
– |
checkCompletedWithWrappedCFException(h); |
1897 |
– |
|
1898 |
– |
f = new CompletableFuture<>(); |
1899 |
– |
g = new CompletableFuture<>(); |
1900 |
– |
f.completeExceptionally(new CFException()); |
1901 |
– |
g.complete(3); |
1902 |
– |
h = f.thenAcceptBothAsync(g, r = new SubtractAction()); |
1903 |
– |
checkCompletedWithWrappedCFException(h); |
1904 |
– |
} |
1905 |
– |
|
1906 |
– |
/** |
1907 |
– |
* thenAcceptBothAsync result completes exceptionally if action does |
1908 |
– |
*/ |
1909 |
– |
public void testThenAcceptBothAsync3() { |
1910 |
– |
CompletableFuture<Integer> f, g; |
1911 |
– |
CompletableFuture<Void> h; |
1912 |
– |
FailingBiConsumer r; |
1913 |
– |
|
1914 |
– |
f = new CompletableFuture<>(); |
1915 |
– |
g = new CompletableFuture<>(); |
1916 |
– |
h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer()); |
1917 |
– |
f.complete(3); |
1918 |
– |
checkIncomplete(h); |
1919 |
– |
g.complete(1); |
1920 |
– |
checkCompletedWithWrappedCFException(h); |
1921 |
– |
|
1922 |
– |
f = new CompletableFuture<>(); |
1923 |
– |
g = new CompletableFuture<>(); |
1924 |
– |
f.complete(3); |
1925 |
– |
g.complete(1); |
1926 |
– |
h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer()); |
1927 |
– |
checkCompletedWithWrappedCFException(h); |
1928 |
– |
} |
1929 |
– |
|
1930 |
– |
/** |
1931 |
– |
* thenAcceptBothAsync result completes exceptionally if either source cancelled |
1932 |
– |
*/ |
1933 |
– |
public void testThenAcceptBothAsync4() { |
1934 |
– |
CompletableFuture<Integer> f, g; |
1935 |
– |
CompletableFuture<Void> h; |
1936 |
– |
SubtractAction r; |
1937 |
– |
|
1938 |
– |
f = new CompletableFuture<>(); |
1939 |
– |
g = new CompletableFuture<>(); |
1940 |
– |
h = f.thenAcceptBothAsync(g, r = new SubtractAction()); |
1941 |
– |
assertTrue(f.cancel(true)); |
1942 |
– |
checkIncomplete(h); |
1943 |
– |
g.complete(1); |
1944 |
– |
checkCompletedWithWrappedCancellationException(h); |
1945 |
– |
|
1946 |
– |
f = new CompletableFuture<>(); |
1947 |
– |
g = new CompletableFuture<>(); |
1948 |
– |
h = f.thenAcceptBothAsync(g, r = new SubtractAction()); |
1949 |
– |
assertTrue(g.cancel(true)); |
1950 |
– |
checkIncomplete(h); |
1951 |
– |
f.complete(3); |
1952 |
– |
checkCompletedWithWrappedCancellationException(h); |
1953 |
– |
|
1954 |
– |
f = new CompletableFuture<>(); |
1955 |
– |
g = new CompletableFuture<>(); |
1956 |
– |
f.complete(3); |
1957 |
– |
assertTrue(g.cancel(true)); |
1958 |
– |
h = f.thenAcceptBothAsync(g, r = new SubtractAction()); |
1959 |
– |
checkCompletedWithWrappedCancellationException(h); |
1960 |
– |
|
1961 |
– |
f = new CompletableFuture<>(); |
1962 |
– |
g = new CompletableFuture<>(); |
1963 |
– |
assertTrue(f.cancel(true)); |
1964 |
– |
g.complete(3); |
1965 |
– |
h = f.thenAcceptBothAsync(g, r = new SubtractAction()); |
1966 |
– |
checkCompletedWithWrappedCancellationException(h); |
1967 |
– |
} |
1968 |
– |
|
1969 |
– |
/** |
2050 |
|
* applyToEitherAsync result completes normally after normal |
2051 |
|
* completion of sources |
2052 |
|
*/ |
2612 |
|
checkCompletedWithWrappedCancellationException(h); |
2613 |
|
|
2614 |
|
assertEquals(0, e.count.get()); |
2535 |
– |
} |
2536 |
– |
|
2537 |
– |
/** |
2538 |
– |
* thenAcceptBothAsync result completes normally after normal |
2539 |
– |
* completion of sources |
2540 |
– |
*/ |
2541 |
– |
public void testThenAcceptBothAsyncE() { |
2542 |
– |
CompletableFuture<Integer> f, g; |
2543 |
– |
CompletableFuture<Void> h; |
2544 |
– |
SubtractAction r; |
2545 |
– |
ThreadExecutor e = new ThreadExecutor(); |
2546 |
– |
|
2547 |
– |
f = new CompletableFuture<>(); |
2548 |
– |
g = new CompletableFuture<>(); |
2549 |
– |
h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e); |
2550 |
– |
f.complete(3); |
2551 |
– |
checkIncomplete(h); |
2552 |
– |
g.complete(1); |
2553 |
– |
checkCompletedNormally(h, null); |
2554 |
– |
assertEquals(r.value, 2); |
2555 |
– |
|
2556 |
– |
f = new CompletableFuture<>(); |
2557 |
– |
g = new CompletableFuture<>(); |
2558 |
– |
h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e); |
2559 |
– |
g.complete(1); |
2560 |
– |
checkIncomplete(h); |
2561 |
– |
f.complete(3); |
2562 |
– |
checkCompletedNormally(h, null); |
2563 |
– |
assertEquals(r.value, 2); |
2564 |
– |
|
2565 |
– |
f = new CompletableFuture<>(); |
2566 |
– |
g = new CompletableFuture<>(); |
2567 |
– |
g.complete(1); |
2568 |
– |
f.complete(3); |
2569 |
– |
h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e); |
2570 |
– |
checkCompletedNormally(h, null); |
2571 |
– |
assertEquals(r.value, 2); |
2572 |
– |
|
2573 |
– |
assertEquals(3, e.count.get()); |
2574 |
– |
} |
2575 |
– |
|
2576 |
– |
/** |
2577 |
– |
* thenAcceptBothAsync result completes exceptionally after exceptional |
2578 |
– |
* completion of source |
2579 |
– |
*/ |
2580 |
– |
public void testThenAcceptBothAsync2E() { |
2581 |
– |
CompletableFuture<Integer> f, g; |
2582 |
– |
CompletableFuture<Void> h; |
2583 |
– |
SubtractAction r; |
2584 |
– |
ThreadExecutor e = new ThreadExecutor(); |
2585 |
– |
|
2586 |
– |
f = new CompletableFuture<>(); |
2587 |
– |
g = new CompletableFuture<>(); |
2588 |
– |
h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e); |
2589 |
– |
f.completeExceptionally(new CFException()); |
2590 |
– |
checkIncomplete(h); |
2591 |
– |
g.complete(1); |
2592 |
– |
checkCompletedWithWrappedCFException(h); |
2593 |
– |
|
2594 |
– |
f = new CompletableFuture<>(); |
2595 |
– |
g = new CompletableFuture<>(); |
2596 |
– |
h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e); |
2597 |
– |
g.completeExceptionally(new CFException()); |
2598 |
– |
checkIncomplete(h); |
2599 |
– |
f.complete(3); |
2600 |
– |
checkCompletedWithWrappedCFException(h); |
2601 |
– |
|
2602 |
– |
f = new CompletableFuture<>(); |
2603 |
– |
g = new CompletableFuture<>(); |
2604 |
– |
f.complete(3); |
2605 |
– |
g.completeExceptionally(new CFException()); |
2606 |
– |
h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e); |
2607 |
– |
checkCompletedWithWrappedCFException(h); |
2608 |
– |
|
2609 |
– |
f = new CompletableFuture<>(); |
2610 |
– |
g = new CompletableFuture<>(); |
2611 |
– |
f.completeExceptionally(new CFException()); |
2612 |
– |
g.complete(3); |
2613 |
– |
h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e); |
2614 |
– |
checkCompletedWithWrappedCFException(h); |
2615 |
– |
|
2616 |
– |
assertEquals(0, e.count.get()); |
2617 |
– |
} |
2618 |
– |
|
2619 |
– |
/** |
2620 |
– |
* thenAcceptBothAsync result completes exceptionally if action does |
2621 |
– |
*/ |
2622 |
– |
public void testThenAcceptBothAsync3E() { |
2623 |
– |
CompletableFuture<Integer> f, g; |
2624 |
– |
CompletableFuture<Void> h; |
2625 |
– |
FailingBiConsumer r; |
2626 |
– |
ThreadExecutor e = new ThreadExecutor(); |
2627 |
– |
|
2628 |
– |
f = new CompletableFuture<>(); |
2629 |
– |
g = new CompletableFuture<>(); |
2630 |
– |
h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer(), e); |
2631 |
– |
f.complete(3); |
2632 |
– |
checkIncomplete(h); |
2633 |
– |
g.complete(1); |
2634 |
– |
checkCompletedWithWrappedCFException(h); |
2635 |
– |
|
2636 |
– |
f = new CompletableFuture<>(); |
2637 |
– |
g = new CompletableFuture<>(); |
2638 |
– |
f.complete(3); |
2639 |
– |
g.complete(1); |
2640 |
– |
h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer(), e); |
2641 |
– |
checkCompletedWithWrappedCFException(h); |
2642 |
– |
|
2643 |
– |
assertEquals(2, e.count.get()); |
2644 |
– |
} |
2645 |
– |
|
2646 |
– |
/** |
2647 |
– |
* thenAcceptBothAsync result completes exceptionally if either source cancelled |
2648 |
– |
*/ |
2649 |
– |
public void testThenAcceptBothAsync4E() { |
2650 |
– |
CompletableFuture<Integer> f, g; |
2651 |
– |
CompletableFuture<Void> h; |
2652 |
– |
SubtractAction r; |
2653 |
– |
ThreadExecutor e = new ThreadExecutor(); |
2654 |
– |
|
2655 |
– |
f = new CompletableFuture<>(); |
2656 |
– |
g = new CompletableFuture<>(); |
2657 |
– |
h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e); |
2658 |
– |
assertTrue(f.cancel(true)); |
2659 |
– |
checkIncomplete(h); |
2660 |
– |
g.complete(1); |
2661 |
– |
checkCompletedWithWrappedCancellationException(h); |
2662 |
– |
|
2663 |
– |
f = new CompletableFuture<>(); |
2664 |
– |
g = new CompletableFuture<>(); |
2665 |
– |
h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e); |
2666 |
– |
assertTrue(g.cancel(true)); |
2667 |
– |
checkIncomplete(h); |
2668 |
– |
f.complete(3); |
2669 |
– |
checkCompletedWithWrappedCancellationException(h); |
2670 |
– |
|
2671 |
– |
f = new CompletableFuture<>(); |
2672 |
– |
g = new CompletableFuture<>(); |
2673 |
– |
f.complete(3); |
2674 |
– |
assertTrue(g.cancel(true)); |
2675 |
– |
h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e); |
2676 |
– |
checkCompletedWithWrappedCancellationException(h); |
2677 |
– |
|
2678 |
– |
f = new CompletableFuture<>(); |
2679 |
– |
g = new CompletableFuture<>(); |
2680 |
– |
assertTrue(f.cancel(true)); |
2681 |
– |
g.complete(3); |
2682 |
– |
h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e); |
2683 |
– |
checkCompletedWithWrappedCancellationException(h); |
2684 |
– |
|
2685 |
– |
assertEquals(0, e.count.get()); |
2615 |
|
} |
2616 |
|
|
2617 |
|
/** |