318 |
|
|
319 |
|
// Choose non-commutative actions for better coverage |
320 |
|
|
321 |
+ |
// A non-commutative function that handles null values as well |
322 |
+ |
public static int subtract(Integer x, Integer y) { |
323 |
+ |
return ((x == null) ? 42 : x.intValue()) |
324 |
+ |
- ((y == null) ? 99 : y.intValue()); |
325 |
+ |
} |
326 |
+ |
|
327 |
|
static final Supplier<Integer> supplyOne = |
328 |
|
() -> Integer.valueOf(1); |
329 |
|
static final Function<Integer, Integer> inc = |
330 |
|
(Integer x) -> Integer.valueOf(x.intValue() + 1); |
331 |
|
static final BiFunction<Integer, Integer, Integer> subtract = |
332 |
< |
(Integer x, Integer y) -> Integer.valueOf(x.intValue() - y.intValue()); |
332 |
> |
(Integer x, Integer y) -> subtract(x, y); |
333 |
|
static final class IncAction implements Consumer<Integer> { |
334 |
|
int value; |
335 |
|
public void accept(Integer x) { value = x.intValue() + 1; } |
336 |
|
} |
337 |
|
static final class SubtractAction implements BiConsumer<Integer, Integer> { |
338 |
< |
// 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 |
< |
} |
338 |
> |
volatile int invocationCount = 0; |
339 |
|
int value; |
340 |
< |
public boolean ran() { return value != 0; } |
340 |
> |
// Check this action was invoked exactly once when result is computed. |
341 |
> |
public boolean ran() { return invocationCount == 1; } |
342 |
|
public void accept(Integer x, Integer y) { |
343 |
+ |
invocationCount++; |
344 |
|
value = subtract(x, y); |
345 |
|
} |
346 |
|
} |
347 |
+ |
static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> { |
348 |
+ |
volatile int invocationCount = 0; |
349 |
+ |
int value; |
350 |
+ |
// Check this action was invoked exactly once when result is computed. |
351 |
+ |
public boolean ran() { return invocationCount == 1; } |
352 |
+ |
public Integer apply(Integer x, Integer y) { |
353 |
+ |
invocationCount++; |
354 |
+ |
return subtract(x, y); |
355 |
+ |
} |
356 |
+ |
} |
357 |
|
static final class Noop implements Runnable { |
358 |
|
boolean ran; |
359 |
|
public void run() { ran = true; } |
441 |
|
BiConsumer<? super T,? super U> a) { |
442 |
|
return f.thenAcceptBoth(g, a); |
443 |
|
} |
444 |
+ |
public <T,U,V> CompletableFuture<V> thenCombine |
445 |
+ |
(CompletableFuture<T> f, |
446 |
+ |
CompletionStage<? extends U> g, |
447 |
+ |
BiFunction<? super T,? super U,? extends V> a) { |
448 |
+ |
return f.thenCombine(g, a); |
449 |
+ |
} |
450 |
|
}, |
451 |
|
|
452 |
|
// /** Experimental way to do more testing */ |
474 |
|
BiConsumer<? super T,? super U> a) { |
475 |
|
return f.thenAcceptBothAsync(g, a); |
476 |
|
} |
477 |
+ |
public <T,U,V> CompletableFuture<V> thenCombine |
478 |
+ |
(CompletableFuture<T> f, |
479 |
+ |
CompletionStage<? extends U> g, |
480 |
+ |
BiFunction<? super T,? super U,? extends V> a) { |
481 |
+ |
return f.thenCombineAsync(g, a); |
482 |
+ |
} |
483 |
|
}, |
484 |
|
|
485 |
|
// REVERSE_DEFAULT_ASYNC { |
506 |
|
BiConsumer<? super T,? super U> a) { |
507 |
|
return f.thenAcceptBothAsync(g, a, new ThreadExecutor()); |
508 |
|
} |
509 |
+ |
public <T,U,V> CompletableFuture<V> thenCombine |
510 |
+ |
(CompletableFuture<T> f, |
511 |
+ |
CompletionStage<? extends U> g, |
512 |
+ |
BiFunction<? super T,? super U,? extends V> a) { |
513 |
+ |
return f.thenCombineAsync(g, a, new ThreadExecutor()); |
514 |
+ |
} |
515 |
|
}; |
516 |
|
|
517 |
|
public abstract <T,U> CompletableFuture<Void> runAfterBoth |
520 |
|
(CompletableFuture<T> f, |
521 |
|
CompletionStage<? extends U> g, |
522 |
|
BiConsumer<? super T,? super U> a); |
523 |
+ |
public abstract <T,U,V> CompletableFuture<V> thenCombine |
524 |
+ |
(CompletableFuture<T> f, |
525 |
+ |
CompletionStage<? extends U> g, |
526 |
+ |
BiFunction<? super T,? super U,? extends V> a); |
527 |
|
} |
528 |
|
|
529 |
|
/** |
818 |
|
* thenCombine result completes normally after normal completion |
819 |
|
* of sources |
820 |
|
*/ |
821 |
< |
public void testThenCombine() { |
822 |
< |
CompletableFuture<Integer> f, g, h; |
821 |
> |
public void testThenCombine_normalCompletion1() { |
822 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
823 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
824 |
> |
for (Integer v2 : new Integer[] { 2, null }) { |
825 |
|
|
826 |
< |
f = new CompletableFuture<>(); |
827 |
< |
g = new CompletableFuture<>(); |
828 |
< |
h = f.thenCombine(g, subtract); |
829 |
< |
f.complete(3); |
826 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
827 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
828 |
> |
final SubtractFunction r = new SubtractFunction(); |
829 |
> |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
830 |
> |
|
831 |
> |
f.complete(v1); |
832 |
|
checkIncomplete(h); |
833 |
< |
g.complete(1); |
834 |
< |
checkCompletedNormally(h, 2); |
833 |
> |
assertFalse(r.ran()); |
834 |
> |
g.complete(v2); |
835 |
|
|
836 |
< |
f = new CompletableFuture<>(); |
837 |
< |
g = new CompletableFuture<>(); |
838 |
< |
h = f.thenCombine(g, subtract); |
839 |
< |
g.complete(1); |
836 |
> |
checkCompletedNormally(h, subtract(v1, v2)); |
837 |
> |
checkCompletedNormally(f, v1); |
838 |
> |
checkCompletedNormally(g, v2); |
839 |
> |
} |
840 |
> |
} |
841 |
> |
|
842 |
> |
public void testThenCombine_normalCompletion2() { |
843 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
844 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
845 |
> |
for (Integer v2 : new Integer[] { 2, null }) { |
846 |
> |
|
847 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
848 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
849 |
> |
final SubtractFunction r = new SubtractFunction(); |
850 |
> |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
851 |
> |
|
852 |
> |
g.complete(v2); |
853 |
|
checkIncomplete(h); |
854 |
< |
f.complete(3); |
855 |
< |
checkCompletedNormally(h, 2); |
854 |
> |
assertFalse(r.ran()); |
855 |
> |
f.complete(v1); |
856 |
|
|
857 |
< |
f = new CompletableFuture<>(); |
858 |
< |
g = new CompletableFuture<>(); |
859 |
< |
g.complete(1); |
860 |
< |
f.complete(3); |
861 |
< |
h = f.thenCombine(g, subtract); |
862 |
< |
checkCompletedNormally(h, 2); |
857 |
> |
checkCompletedNormally(h, subtract(v1, v2)); |
858 |
> |
checkCompletedNormally(f, v1); |
859 |
> |
checkCompletedNormally(g, v2); |
860 |
> |
} |
861 |
> |
} |
862 |
> |
|
863 |
> |
public void testThenCombine_normalCompletion3() { |
864 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
865 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
866 |
> |
for (Integer v2 : new Integer[] { 2, null }) { |
867 |
> |
|
868 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
869 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
870 |
> |
final SubtractFunction r = new SubtractFunction(); |
871 |
> |
|
872 |
> |
g.complete(v2); |
873 |
> |
f.complete(v1); |
874 |
> |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
875 |
> |
|
876 |
> |
checkCompletedNormally(h, subtract(v1, v2)); |
877 |
> |
checkCompletedNormally(f, v1); |
878 |
> |
checkCompletedNormally(g, v2); |
879 |
> |
} |
880 |
> |
} |
881 |
> |
|
882 |
> |
public void testThenCombine_normalCompletion4() { |
883 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
884 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
885 |
> |
for (Integer v2 : new Integer[] { 2, null }) { |
886 |
> |
|
887 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
888 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
889 |
> |
final SubtractFunction r = new SubtractFunction(); |
890 |
> |
|
891 |
> |
f.complete(v1); |
892 |
> |
g.complete(v2); |
893 |
> |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
894 |
> |
|
895 |
> |
checkCompletedNormally(h, subtract(v1, v2)); |
896 |
> |
checkCompletedNormally(f, v1); |
897 |
> |
checkCompletedNormally(g, v2); |
898 |
> |
} |
899 |
|
} |
900 |
|
|
901 |
|
/** |
902 |
|
* thenCombine result completes exceptionally after exceptional |
903 |
|
* completion of either source |
904 |
|
*/ |
905 |
< |
public void testThenCombine2() { |
906 |
< |
CompletableFuture<Integer> f, g, h; |
905 |
> |
public void testThenCombine_exceptionalCompletion1() { |
906 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
907 |
> |
for (Integer v1 : new Integer[] { 1, null }) { |
908 |
|
|
909 |
< |
f = new CompletableFuture<>(); |
910 |
< |
g = new CompletableFuture<>(); |
911 |
< |
h = f.thenCombine(g, subtract); |
912 |
< |
f.completeExceptionally(new CFException()); |
909 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
910 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
911 |
> |
final SubtractFunction r = new SubtractFunction(); |
912 |
> |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
913 |
> |
final CFException ex = new CFException(); |
914 |
> |
|
915 |
> |
f.completeExceptionally(ex); |
916 |
|
checkIncomplete(h); |
917 |
< |
g.complete(1); |
825 |
< |
checkCompletedWithWrappedCFException(h); |
917 |
> |
g.complete(v1); |
918 |
|
|
919 |
< |
f = new CompletableFuture<>(); |
920 |
< |
g = new CompletableFuture<>(); |
921 |
< |
h = f.thenCombine(g, subtract); |
922 |
< |
g.completeExceptionally(new CFException()); |
919 |
> |
checkCompletedWithWrappedCFException(h, ex); |
920 |
> |
checkCompletedWithWrappedCFException(f, ex); |
921 |
> |
assertFalse(r.ran()); |
922 |
> |
checkCompletedNormally(g, v1); |
923 |
> |
} |
924 |
> |
} |
925 |
> |
|
926 |
> |
public void testThenCombine_exceptionalCompletion2() { |
927 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
928 |
> |
for (Integer v1 : new Integer[] { 1, null }) { |
929 |
> |
|
930 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
931 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
932 |
> |
final SubtractFunction r = new SubtractFunction(); |
933 |
> |
final CompletableFuture<Integer> h = m.thenCombine(f, g, subtract); |
934 |
> |
final CFException ex = new CFException(); |
935 |
> |
|
936 |
> |
g.completeExceptionally(ex); |
937 |
|
checkIncomplete(h); |
938 |
< |
f.complete(3); |
833 |
< |
checkCompletedWithWrappedCFException(h); |
938 |
> |
f.complete(v1); |
939 |
|
|
940 |
< |
f = new CompletableFuture<>(); |
941 |
< |
g = new CompletableFuture<>(); |
942 |
< |
f.complete(3); |
943 |
< |
g.completeExceptionally(new CFException()); |
944 |
< |
h = f.thenCombine(g, subtract); |
945 |
< |
checkCompletedWithWrappedCFException(h); |
940 |
> |
checkCompletedWithWrappedCFException(h, ex); |
941 |
> |
checkCompletedWithWrappedCFException(g, ex); |
942 |
> |
assertFalse(r.ran()); |
943 |
> |
checkCompletedNormally(f, v1); |
944 |
> |
} |
945 |
> |
} |
946 |
|
|
947 |
< |
f = new CompletableFuture<>(); |
948 |
< |
g = new CompletableFuture<>(); |
949 |
< |
f.completeExceptionally(new CFException()); |
950 |
< |
g.complete(3); |
951 |
< |
h = f.thenCombine(g, subtract); |
952 |
< |
checkCompletedWithWrappedCFException(h); |
947 |
> |
public void testThenCombine_exceptionalCompletion3() { |
948 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
949 |
> |
for (Integer v1 : new Integer[] { 1, null }) { |
950 |
> |
|
951 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
952 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
953 |
> |
final SubtractFunction r = new SubtractFunction(); |
954 |
> |
final CFException ex = new CFException(); |
955 |
> |
|
956 |
> |
g.completeExceptionally(ex); |
957 |
> |
f.complete(v1); |
958 |
> |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
959 |
> |
|
960 |
> |
checkCompletedWithWrappedCFException(h, ex); |
961 |
> |
checkCompletedWithWrappedCFException(g, ex); |
962 |
> |
assertFalse(r.ran()); |
963 |
> |
checkCompletedNormally(f, v1); |
964 |
> |
} |
965 |
> |
} |
966 |
> |
|
967 |
> |
public void testThenCombine_exceptionalCompletion4() { |
968 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
969 |
> |
for (Integer v1 : new Integer[] { 1, null }) { |
970 |
> |
|
971 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
972 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
973 |
> |
final SubtractFunction r = new SubtractFunction(); |
974 |
> |
final CFException ex = new CFException(); |
975 |
> |
|
976 |
> |
f.completeExceptionally(ex); |
977 |
> |
g.complete(v1); |
978 |
> |
final CompletableFuture<Integer> h = m.thenCombine(f, g, subtract); |
979 |
> |
|
980 |
> |
checkCompletedWithWrappedCFException(h, ex); |
981 |
> |
checkCompletedWithWrappedCFException(f, ex); |
982 |
> |
assertFalse(r.ran()); |
983 |
> |
checkCompletedNormally(g, v1); |
984 |
> |
} |
985 |
|
} |
986 |
|
|
987 |
|
/** |
988 |
|
* thenCombine result completes exceptionally if action does |
989 |
|
*/ |
990 |
< |
public void testThenCombine3() { |
991 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
992 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
993 |
< |
FailingBiFunction r = new FailingBiFunction(); |
994 |
< |
CompletableFuture<Integer> g = f.thenCombine(f2, r); |
995 |
< |
f.complete(one); |
996 |
< |
checkIncomplete(g); |
997 |
< |
assertFalse(r.ran); |
998 |
< |
f2.complete(two); |
999 |
< |
checkCompletedWithWrappedCFException(g); |
1000 |
< |
assertTrue(r.ran); |
990 |
> |
public void testThenCombine_actionFailed1() { |
991 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
992 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
993 |
> |
for (Integer v2 : new Integer[] { 2, null }) { |
994 |
> |
|
995 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
996 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
997 |
> |
final FailingBiFunction r = new FailingBiFunction(); |
998 |
> |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
999 |
> |
|
1000 |
> |
f.complete(v1); |
1001 |
> |
checkIncomplete(h); |
1002 |
> |
g.complete(v2); |
1003 |
> |
|
1004 |
> |
checkCompletedWithWrappedCFException(h); |
1005 |
> |
checkCompletedNormally(f, v1); |
1006 |
> |
checkCompletedNormally(g, v2); |
1007 |
> |
} |
1008 |
> |
} |
1009 |
> |
|
1010 |
> |
public void testThenCombine_actionFailed2() { |
1011 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1012 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1013 |
> |
for (Integer v2 : new Integer[] { 2, null }) { |
1014 |
> |
|
1015 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1016 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1017 |
> |
final FailingBiFunction r = new FailingBiFunction(); |
1018 |
> |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1019 |
> |
|
1020 |
> |
g.complete(v2); |
1021 |
> |
checkIncomplete(h); |
1022 |
> |
f.complete(v1); |
1023 |
> |
|
1024 |
> |
checkCompletedWithWrappedCFException(h); |
1025 |
> |
checkCompletedNormally(f, v1); |
1026 |
> |
checkCompletedNormally(g, v2); |
1027 |
> |
} |
1028 |
|
} |
1029 |
|
|
1030 |
|
/** |
1031 |
|
* thenCombine result completes exceptionally if either source cancelled |
1032 |
|
*/ |
1033 |
< |
public void testThenCombine4() { |
1034 |
< |
CompletableFuture<Integer> f, g, h; |
1033 |
> |
public void testThenCombine_sourceCancelled1() { |
1034 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1035 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1036 |
> |
for (Integer v1 : new Integer[] { 1, null }) { |
1037 |
|
|
1038 |
< |
f = new CompletableFuture<>(); |
1039 |
< |
g = new CompletableFuture<>(); |
1040 |
< |
h = f.thenCombine(g, subtract); |
1041 |
< |
assertTrue(f.cancel(true)); |
1038 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1039 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1040 |
> |
final SubtractFunction r = new SubtractFunction(); |
1041 |
> |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1042 |
> |
|
1043 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1044 |
|
checkIncomplete(h); |
1045 |
< |
g.complete(1); |
1045 |
> |
g.complete(v1); |
1046 |
> |
|
1047 |
|
checkCompletedWithWrappedCancellationException(h); |
1048 |
+ |
checkCancelled(f); |
1049 |
+ |
assertFalse(r.ran()); |
1050 |
+ |
checkCompletedNormally(g, v1); |
1051 |
+ |
} |
1052 |
+ |
} |
1053 |
|
|
1054 |
< |
f = new CompletableFuture<>(); |
1055 |
< |
g = new CompletableFuture<>(); |
1056 |
< |
h = f.thenCombine(g, subtract); |
1057 |
< |
assertTrue(g.cancel(true)); |
1054 |
> |
public void testThenCombine_sourceCancelled2() { |
1055 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1056 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1057 |
> |
for (Integer v1 : new Integer[] { 1, null }) { |
1058 |
> |
|
1059 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1060 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1061 |
> |
final SubtractFunction r = new SubtractFunction(); |
1062 |
> |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1063 |
> |
|
1064 |
> |
assertTrue(g.cancel(mayInterruptIfRunning)); |
1065 |
|
checkIncomplete(h); |
1066 |
< |
f.complete(3); |
1066 |
> |
f.complete(v1); |
1067 |
> |
|
1068 |
|
checkCompletedWithWrappedCancellationException(h); |
1069 |
+ |
checkCancelled(g); |
1070 |
+ |
assertFalse(r.ran()); |
1071 |
+ |
checkCompletedNormally(f, v1); |
1072 |
+ |
} |
1073 |
+ |
} |
1074 |
+ |
|
1075 |
+ |
public void testThenCombine_sourceCancelled3() { |
1076 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
1077 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1078 |
+ |
for (Integer v1 : new Integer[] { 1, null }) { |
1079 |
+ |
|
1080 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1081 |
+ |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1082 |
+ |
final SubtractFunction r = new SubtractFunction(); |
1083 |
+ |
|
1084 |
+ |
assertTrue(g.cancel(mayInterruptIfRunning)); |
1085 |
+ |
f.complete(v1); |
1086 |
+ |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1087 |
|
|
888 |
– |
f = new CompletableFuture<>(); |
889 |
– |
g = new CompletableFuture<>(); |
890 |
– |
assertTrue(f.cancel(true)); |
891 |
– |
assertTrue(g.cancel(true)); |
892 |
– |
h = f.thenCombine(g, subtract); |
1088 |
|
checkCompletedWithWrappedCancellationException(h); |
1089 |
+ |
checkCancelled(g); |
1090 |
+ |
assertFalse(r.ran()); |
1091 |
+ |
checkCompletedNormally(f, v1); |
1092 |
+ |
} |
1093 |
+ |
} |
1094 |
+ |
|
1095 |
+ |
public void testThenCombine_sourceCancelled4() { |
1096 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
1097 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1098 |
+ |
for (Integer v1 : new Integer[] { 1, null }) { |
1099 |
+ |
|
1100 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1101 |
+ |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1102 |
+ |
final SubtractFunction r = new SubtractFunction(); |
1103 |
+ |
|
1104 |
+ |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1105 |
+ |
g.complete(v1); |
1106 |
+ |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1107 |
+ |
|
1108 |
+ |
checkCompletedWithWrappedCancellationException(h); |
1109 |
+ |
checkCancelled(f); |
1110 |
+ |
assertFalse(r.ran()); |
1111 |
+ |
checkCompletedNormally(g, v1); |
1112 |
+ |
} |
1113 |
|
} |
1114 |
|
|
1115 |
|
/** |
1132 |
|
g.complete(v2); |
1133 |
|
|
1134 |
|
checkCompletedNormally(h, null); |
1135 |
< |
assertEquals(r.value, r.subtract(v1, v2)); |
1135 |
> |
assertEquals(r.value, subtract(v1, v2)); |
1136 |
|
checkCompletedNormally(f, v1); |
1137 |
|
checkCompletedNormally(g, v2); |
1138 |
|
} |
1154 |
|
f.complete(v1); |
1155 |
|
|
1156 |
|
checkCompletedNormally(h, null); |
1157 |
< |
assertEquals(r.value, r.subtract(v1, v2)); |
1157 |
> |
assertEquals(r.value, subtract(v1, v2)); |
1158 |
|
checkCompletedNormally(f, v1); |
1159 |
|
checkCompletedNormally(g, v2); |
1160 |
|
} |
1174 |
|
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1175 |
|
|
1176 |
|
checkCompletedNormally(h, null); |
1177 |
< |
assertEquals(r.value, r.subtract(v1, v2)); |
1177 |
> |
assertEquals(r.value, subtract(v1, v2)); |
1178 |
|
checkCompletedNormally(f, v1); |
1179 |
|
checkCompletedNormally(g, v2); |
1180 |
|
} |
1194 |
|
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1195 |
|
|
1196 |
|
checkCompletedNormally(h, null); |
1197 |
< |
assertEquals(r.value, r.subtract(v1, v2)); |
1197 |
> |
assertEquals(r.value, subtract(v1, v2)); |
1198 |
|
checkCompletedNormally(f, v1); |
1199 |
|
checkCompletedNormally(g, v2); |
1200 |
|
} |
2151 |
|
} |
2152 |
|
|
2153 |
|
/** |
1935 |
– |
* thenCombineAsync result completes normally after normal |
1936 |
– |
* completion of sources |
1937 |
– |
*/ |
1938 |
– |
public void testThenCombineAsync() { |
1939 |
– |
CompletableFuture<Integer> f, g, h; |
1940 |
– |
|
1941 |
– |
f = new CompletableFuture<>(); |
1942 |
– |
g = new CompletableFuture<>(); |
1943 |
– |
h = f.thenCombineAsync(g, subtract); |
1944 |
– |
f.complete(3); |
1945 |
– |
checkIncomplete(h); |
1946 |
– |
g.complete(1); |
1947 |
– |
checkCompletedNormally(h, 2); |
1948 |
– |
|
1949 |
– |
f = new CompletableFuture<>(); |
1950 |
– |
g = new CompletableFuture<>(); |
1951 |
– |
h = f.thenCombineAsync(g, subtract); |
1952 |
– |
g.complete(1); |
1953 |
– |
checkIncomplete(h); |
1954 |
– |
f.complete(3); |
1955 |
– |
checkCompletedNormally(h, 2); |
1956 |
– |
|
1957 |
– |
f = new CompletableFuture<>(); |
1958 |
– |
g = new CompletableFuture<>(); |
1959 |
– |
g.complete(1); |
1960 |
– |
f.complete(3); |
1961 |
– |
h = f.thenCombineAsync(g, subtract); |
1962 |
– |
checkCompletedNormally(h, 2); |
1963 |
– |
} |
1964 |
– |
|
1965 |
– |
/** |
1966 |
– |
* thenCombineAsync result completes exceptionally after exceptional |
1967 |
– |
* completion of either source |
1968 |
– |
*/ |
1969 |
– |
public void testThenCombineAsync2() { |
1970 |
– |
CompletableFuture<Integer> f, g, h; |
1971 |
– |
|
1972 |
– |
f = new CompletableFuture<>(); |
1973 |
– |
g = new CompletableFuture<>(); |
1974 |
– |
h = f.thenCombineAsync(g, subtract); |
1975 |
– |
f.completeExceptionally(new CFException()); |
1976 |
– |
checkIncomplete(h); |
1977 |
– |
g.complete(1); |
1978 |
– |
checkCompletedWithWrappedCFException(h); |
1979 |
– |
|
1980 |
– |
f = new CompletableFuture<>(); |
1981 |
– |
g = new CompletableFuture<>(); |
1982 |
– |
h = f.thenCombineAsync(g, subtract); |
1983 |
– |
g.completeExceptionally(new CFException()); |
1984 |
– |
checkIncomplete(h); |
1985 |
– |
f.complete(3); |
1986 |
– |
checkCompletedWithWrappedCFException(h); |
1987 |
– |
|
1988 |
– |
f = new CompletableFuture<>(); |
1989 |
– |
g = new CompletableFuture<>(); |
1990 |
– |
g.completeExceptionally(new CFException()); |
1991 |
– |
f.complete(3); |
1992 |
– |
h = f.thenCombineAsync(g, subtract); |
1993 |
– |
checkCompletedWithWrappedCFException(h); |
1994 |
– |
} |
1995 |
– |
|
1996 |
– |
/** |
1997 |
– |
* thenCombineAsync result completes exceptionally if action does |
1998 |
– |
*/ |
1999 |
– |
public void testThenCombineAsync3() { |
2000 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2001 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2002 |
– |
FailingBiFunction r = new FailingBiFunction(); |
2003 |
– |
CompletableFuture<Integer> g = f.thenCombineAsync(f2, r); |
2004 |
– |
f.complete(one); |
2005 |
– |
checkIncomplete(g); |
2006 |
– |
assertFalse(r.ran); |
2007 |
– |
f2.complete(two); |
2008 |
– |
checkCompletedWithWrappedCFException(g); |
2009 |
– |
assertTrue(r.ran); |
2010 |
– |
} |
2011 |
– |
|
2012 |
– |
/** |
2013 |
– |
* thenCombineAsync result completes exceptionally if either source cancelled |
2014 |
– |
*/ |
2015 |
– |
public void testThenCombineAsync4() { |
2016 |
– |
CompletableFuture<Integer> f, g, h; |
2017 |
– |
|
2018 |
– |
f = new CompletableFuture<>(); |
2019 |
– |
g = new CompletableFuture<>(); |
2020 |
– |
h = f.thenCombineAsync(g, subtract); |
2021 |
– |
assertTrue(f.cancel(true)); |
2022 |
– |
checkIncomplete(h); |
2023 |
– |
g.complete(1); |
2024 |
– |
checkCompletedWithWrappedCancellationException(h); |
2025 |
– |
|
2026 |
– |
f = new CompletableFuture<>(); |
2027 |
– |
g = new CompletableFuture<>(); |
2028 |
– |
h = f.thenCombineAsync(g, subtract); |
2029 |
– |
assertTrue(g.cancel(true)); |
2030 |
– |
checkIncomplete(h); |
2031 |
– |
f.complete(3); |
2032 |
– |
checkCompletedWithWrappedCancellationException(h); |
2033 |
– |
|
2034 |
– |
f = new CompletableFuture<>(); |
2035 |
– |
g = new CompletableFuture<>(); |
2036 |
– |
g.complete(3); |
2037 |
– |
assertTrue(f.cancel(true)); |
2038 |
– |
h = f.thenCombineAsync(g, subtract); |
2039 |
– |
checkCompletedWithWrappedCancellationException(h); |
2040 |
– |
|
2041 |
– |
f = new CompletableFuture<>(); |
2042 |
– |
g = new CompletableFuture<>(); |
2043 |
– |
f.complete(3); |
2044 |
– |
assertTrue(g.cancel(true)); |
2045 |
– |
h = f.thenCombineAsync(g, subtract); |
2046 |
– |
checkCompletedWithWrappedCancellationException(h); |
2047 |
– |
} |
2048 |
– |
|
2049 |
– |
/** |
2154 |
|
* applyToEitherAsync result completes normally after normal |
2155 |
|
* completion of sources |
2156 |
|
*/ |
2590 |
|
} |
2591 |
|
|
2592 |
|
/** |
2489 |
– |
* thenCombineAsync result completes normally after normal |
2490 |
– |
* completion of sources |
2491 |
– |
*/ |
2492 |
– |
public void testThenCombineAsyncE() { |
2493 |
– |
CompletableFuture<Integer> f, g, h; |
2494 |
– |
ThreadExecutor e = new ThreadExecutor(); |
2495 |
– |
int count = 0; |
2496 |
– |
|
2497 |
– |
f = new CompletableFuture<>(); |
2498 |
– |
g = new CompletableFuture<>(); |
2499 |
– |
h = f.thenCombineAsync(g, subtract, e); |
2500 |
– |
f.complete(3); |
2501 |
– |
checkIncomplete(h); |
2502 |
– |
g.complete(1); |
2503 |
– |
checkCompletedNormally(h, 2); |
2504 |
– |
assertEquals(++count, e.count.get()); |
2505 |
– |
|
2506 |
– |
f = new CompletableFuture<>(); |
2507 |
– |
g = new CompletableFuture<>(); |
2508 |
– |
h = f.thenCombineAsync(g, subtract, e); |
2509 |
– |
g.complete(1); |
2510 |
– |
checkIncomplete(h); |
2511 |
– |
f.complete(3); |
2512 |
– |
checkCompletedNormally(h, 2); |
2513 |
– |
assertEquals(++count, e.count.get()); |
2514 |
– |
|
2515 |
– |
f = new CompletableFuture<>(); |
2516 |
– |
g = new CompletableFuture<>(); |
2517 |
– |
g.complete(1); |
2518 |
– |
f.complete(3); |
2519 |
– |
h = f.thenCombineAsync(g, subtract, e); |
2520 |
– |
checkCompletedNormally(h, 2); |
2521 |
– |
assertEquals(++count, e.count.get()); |
2522 |
– |
} |
2523 |
– |
|
2524 |
– |
/** |
2525 |
– |
* thenCombineAsync result completes exceptionally after exceptional |
2526 |
– |
* completion of either source |
2527 |
– |
*/ |
2528 |
– |
public void testThenCombineAsync2E() { |
2529 |
– |
CompletableFuture<Integer> f, g, h; |
2530 |
– |
ThreadExecutor e = new ThreadExecutor(); |
2531 |
– |
int count = 0; |
2532 |
– |
|
2533 |
– |
f = new CompletableFuture<>(); |
2534 |
– |
g = new CompletableFuture<>(); |
2535 |
– |
h = f.thenCombineAsync(g, subtract, e); |
2536 |
– |
f.completeExceptionally(new CFException()); |
2537 |
– |
checkIncomplete(h); |
2538 |
– |
g.complete(1); |
2539 |
– |
checkCompletedWithWrappedCFException(h); |
2540 |
– |
|
2541 |
– |
f = new CompletableFuture<>(); |
2542 |
– |
g = new CompletableFuture<>(); |
2543 |
– |
h = f.thenCombineAsync(g, subtract, e); |
2544 |
– |
g.completeExceptionally(new CFException()); |
2545 |
– |
checkIncomplete(h); |
2546 |
– |
f.complete(3); |
2547 |
– |
checkCompletedWithWrappedCFException(h); |
2548 |
– |
|
2549 |
– |
f = new CompletableFuture<>(); |
2550 |
– |
g = new CompletableFuture<>(); |
2551 |
– |
g.completeExceptionally(new CFException()); |
2552 |
– |
h = f.thenCombineAsync(g, subtract, e); |
2553 |
– |
checkIncomplete(h); |
2554 |
– |
f.complete(3); |
2555 |
– |
checkCompletedWithWrappedCFException(h); |
2556 |
– |
|
2557 |
– |
assertEquals(0, e.count.get()); |
2558 |
– |
} |
2559 |
– |
|
2560 |
– |
/** |
2561 |
– |
* thenCombineAsync result completes exceptionally if action does |
2562 |
– |
*/ |
2563 |
– |
public void testThenCombineAsync3E() { |
2564 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2565 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2566 |
– |
FailingBiFunction r = new FailingBiFunction(); |
2567 |
– |
CompletableFuture<Integer> g = f.thenCombineAsync(f2, r, new ThreadExecutor()); |
2568 |
– |
f.complete(one); |
2569 |
– |
checkIncomplete(g); |
2570 |
– |
assertFalse(r.ran); |
2571 |
– |
f2.complete(two); |
2572 |
– |
checkCompletedWithWrappedCFException(g); |
2573 |
– |
assertTrue(r.ran); |
2574 |
– |
} |
2575 |
– |
|
2576 |
– |
/** |
2577 |
– |
* thenCombineAsync result completes exceptionally if either source cancelled |
2578 |
– |
*/ |
2579 |
– |
public void testThenCombineAsync4E() { |
2580 |
– |
CompletableFuture<Integer> f, g, h; |
2581 |
– |
ThreadExecutor e = new ThreadExecutor(); |
2582 |
– |
|
2583 |
– |
f = new CompletableFuture<>(); |
2584 |
– |
g = new CompletableFuture<>(); |
2585 |
– |
h = f.thenCombineAsync(g, subtract, e); |
2586 |
– |
assertTrue(f.cancel(true)); |
2587 |
– |
checkIncomplete(h); |
2588 |
– |
g.complete(1); |
2589 |
– |
checkCompletedWithWrappedCancellationException(h); |
2590 |
– |
|
2591 |
– |
f = new CompletableFuture<>(); |
2592 |
– |
g = new CompletableFuture<>(); |
2593 |
– |
h = f.thenCombineAsync(g, subtract, e); |
2594 |
– |
assertTrue(g.cancel(true)); |
2595 |
– |
checkIncomplete(h); |
2596 |
– |
f.complete(3); |
2597 |
– |
checkCompletedWithWrappedCancellationException(h); |
2598 |
– |
|
2599 |
– |
f = new CompletableFuture<>(); |
2600 |
– |
g = new CompletableFuture<>(); |
2601 |
– |
assertTrue(g.cancel(true)); |
2602 |
– |
h = f.thenCombineAsync(g, subtract, e); |
2603 |
– |
checkIncomplete(h); |
2604 |
– |
f.complete(3); |
2605 |
– |
checkCompletedWithWrappedCancellationException(h); |
2606 |
– |
|
2607 |
– |
f = new CompletableFuture<>(); |
2608 |
– |
g = new CompletableFuture<>(); |
2609 |
– |
assertTrue(f.cancel(true)); |
2610 |
– |
assertTrue(g.cancel(true)); |
2611 |
– |
h = f.thenCombineAsync(g, subtract, e); |
2612 |
– |
checkCompletedWithWrappedCancellationException(h); |
2613 |
– |
|
2614 |
– |
assertEquals(0, e.count.get()); |
2615 |
– |
} |
2616 |
– |
|
2617 |
– |
/** |
2593 |
|
* applyToEitherAsync result completes normally after normal |
2594 |
|
* completion of sources |
2595 |
|
*/ |