317 |
|
* getNumberOfDependents returns number of dependent tasks |
318 |
|
*/ |
319 |
|
public void testGetNumberOfDependents() { |
320 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
321 |
+ |
{ |
322 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
323 |
|
assertEquals(0, f.getNumberOfDependents()); |
324 |
< |
CompletableFuture g = f.thenRun(new Noop(ExecutionMode.DEFAULT)); |
324 |
> |
final CompletableFuture<Void> g = m.thenRun(f, new Noop(m)); |
325 |
|
assertEquals(1, f.getNumberOfDependents()); |
326 |
|
assertEquals(0, g.getNumberOfDependents()); |
327 |
< |
CompletableFuture h = f.thenRun(new Noop(ExecutionMode.DEFAULT)); |
327 |
> |
final CompletableFuture<Void> h = m.thenRun(f, new Noop(m)); |
328 |
|
assertEquals(2, f.getNumberOfDependents()); |
329 |
+ |
assertEquals(0, h.getNumberOfDependents()); |
330 |
|
f.complete(1); |
331 |
|
checkCompletedNormally(g, null); |
332 |
+ |
checkCompletedNormally(h, null); |
333 |
|
assertEquals(0, f.getNumberOfDependents()); |
334 |
|
assertEquals(0, g.getNumberOfDependents()); |
335 |
< |
} |
335 |
> |
assertEquals(0, h.getNumberOfDependents()); |
336 |
> |
}} |
337 |
|
|
338 |
|
/** |
339 |
|
* toString indicates current completion state |
856 |
|
assertEquals(0, a.get()); |
857 |
|
}} |
858 |
|
|
854 |
– |
|
859 |
|
/** |
860 |
|
* exceptionally action completes with function value on source |
861 |
|
* exception |
904 |
|
}} |
905 |
|
|
906 |
|
/** |
907 |
+ |
* whenComplete action executes on normal completion, propagating |
908 |
+ |
* source result. |
909 |
+ |
*/ |
910 |
+ |
public void testWhenComplete_normalCompletion1() { |
911 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
912 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
913 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
914 |
+ |
{ |
915 |
+ |
final AtomicInteger a = new AtomicInteger(0); |
916 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
917 |
+ |
if (!createIncomplete) f.complete(v1); |
918 |
+ |
final CompletableFuture<Integer> g = m.whenComplete |
919 |
+ |
(f, |
920 |
+ |
(Integer x, Throwable t) -> { |
921 |
+ |
m.checkExecutionMode(); |
922 |
+ |
threadAssertSame(x, v1); |
923 |
+ |
threadAssertNull(t); |
924 |
+ |
a.getAndIncrement(); |
925 |
+ |
}); |
926 |
+ |
if (createIncomplete) f.complete(v1); |
927 |
+ |
|
928 |
+ |
checkCompletedNormally(g, v1); |
929 |
+ |
checkCompletedNormally(f, v1); |
930 |
+ |
assertEquals(1, a.get()); |
931 |
+ |
}} |
932 |
+ |
|
933 |
+ |
/** |
934 |
+ |
* whenComplete action executes on exceptional completion, propagating |
935 |
+ |
* source result. |
936 |
+ |
*/ |
937 |
+ |
public void testWhenComplete_exceptionalCompletion() { |
938 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
939 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
940 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
941 |
+ |
{ |
942 |
+ |
final AtomicInteger a = new AtomicInteger(0); |
943 |
+ |
final CFException ex = new CFException(); |
944 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
945 |
+ |
if (!createIncomplete) f.completeExceptionally(ex); |
946 |
+ |
final CompletableFuture<Integer> g = m.whenComplete |
947 |
+ |
(f, |
948 |
+ |
(Integer x, Throwable t) -> { |
949 |
+ |
m.checkExecutionMode(); |
950 |
+ |
threadAssertNull(x); |
951 |
+ |
threadAssertSame(t, ex); |
952 |
+ |
a.getAndIncrement(); |
953 |
+ |
}); |
954 |
+ |
if (createIncomplete) f.completeExceptionally(ex); |
955 |
+ |
|
956 |
+ |
checkCompletedWithWrappedException(g, ex); |
957 |
+ |
checkCompletedExceptionally(f, ex); |
958 |
+ |
assertEquals(1, a.get()); |
959 |
+ |
}} |
960 |
+ |
|
961 |
+ |
/** |
962 |
+ |
* whenComplete action executes on cancelled source, propagating |
963 |
+ |
* CancellationException. |
964 |
+ |
*/ |
965 |
+ |
public void testWhenComplete_sourceCancelled() { |
966 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
967 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
968 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
969 |
+ |
{ |
970 |
+ |
final AtomicInteger a = new AtomicInteger(0); |
971 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
972 |
+ |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
973 |
+ |
final CompletableFuture<Integer> g = m.whenComplete |
974 |
+ |
(f, |
975 |
+ |
(Integer x, Throwable t) -> { |
976 |
+ |
m.checkExecutionMode(); |
977 |
+ |
threadAssertNull(x); |
978 |
+ |
threadAssertTrue(t instanceof CancellationException); |
979 |
+ |
a.getAndIncrement(); |
980 |
+ |
}); |
981 |
+ |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
982 |
+ |
|
983 |
+ |
checkCompletedWithWrappedCancellationException(g); |
984 |
+ |
checkCancelled(f); |
985 |
+ |
assertEquals(1, a.get()); |
986 |
+ |
}} |
987 |
+ |
|
988 |
+ |
/** |
989 |
+ |
* If a whenComplete action throws an exception when triggered by |
990 |
+ |
* a normal completion, it completes exceptionally |
991 |
+ |
*/ |
992 |
+ |
public void testWhenComplete_actionFailed() { |
993 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
994 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
995 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
996 |
+ |
{ |
997 |
+ |
final AtomicInteger a = new AtomicInteger(0); |
998 |
+ |
final CFException ex = new CFException(); |
999 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1000 |
+ |
if (!createIncomplete) f.complete(v1); |
1001 |
+ |
final CompletableFuture<Integer> g = m.whenComplete |
1002 |
+ |
(f, |
1003 |
+ |
(Integer x, Throwable t) -> { |
1004 |
+ |
m.checkExecutionMode(); |
1005 |
+ |
threadAssertSame(x, v1); |
1006 |
+ |
threadAssertNull(t); |
1007 |
+ |
a.getAndIncrement(); |
1008 |
+ |
throw ex; |
1009 |
+ |
}); |
1010 |
+ |
if (createIncomplete) f.complete(v1); |
1011 |
+ |
|
1012 |
+ |
checkCompletedWithWrappedException(g, ex); |
1013 |
+ |
checkCompletedNormally(f, v1); |
1014 |
+ |
assertEquals(1, a.get()); |
1015 |
+ |
}} |
1016 |
+ |
|
1017 |
+ |
/** |
1018 |
+ |
* If a whenComplete action throws an exception when triggered by |
1019 |
+ |
* a source completion that also throws an exception, the source |
1020 |
+ |
* exception takes precedence. |
1021 |
+ |
*/ |
1022 |
+ |
public void testWhenComplete_actionFailedSourceFailed() { |
1023 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
1024 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
1025 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
1026 |
+ |
{ |
1027 |
+ |
final AtomicInteger a = new AtomicInteger(0); |
1028 |
+ |
final CFException ex1 = new CFException(); |
1029 |
+ |
final CFException ex2 = new CFException(); |
1030 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1031 |
+ |
|
1032 |
+ |
if (!createIncomplete) f.completeExceptionally(ex1); |
1033 |
+ |
final CompletableFuture<Integer> g = m.whenComplete |
1034 |
+ |
(f, |
1035 |
+ |
(Integer x, Throwable t) -> { |
1036 |
+ |
m.checkExecutionMode(); |
1037 |
+ |
threadAssertSame(t, ex1); |
1038 |
+ |
threadAssertNull(x); |
1039 |
+ |
a.getAndIncrement(); |
1040 |
+ |
throw ex2; |
1041 |
+ |
}); |
1042 |
+ |
if (createIncomplete) f.completeExceptionally(ex1); |
1043 |
+ |
|
1044 |
+ |
checkCompletedWithWrappedException(g, ex1); |
1045 |
+ |
checkCompletedExceptionally(f, ex1); |
1046 |
+ |
assertEquals(1, a.get()); |
1047 |
+ |
}} |
1048 |
+ |
|
1049 |
+ |
/** |
1050 |
|
* handle action completes normally with function value on normal |
1051 |
|
* completion of source |
1052 |
|
*/ |
1827 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1828 |
|
final Noop r = new Noop(m); |
1829 |
|
|
1683 |
– |
|
1830 |
|
(fFirst ? f : g).complete(v1); |
1831 |
|
if (!createIncomplete) |
1832 |
|
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
2995 |
|
assertSame(f, f.toCompletableFuture()); |
2996 |
|
} |
2997 |
|
|
2852 |
– |
/** |
2853 |
– |
* whenComplete action executes on normal completion, propagating |
2854 |
– |
* source result. |
2855 |
– |
*/ |
2856 |
– |
public void testWhenComplete_normalCompletion1() { |
2857 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2858 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2859 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2860 |
– |
{ |
2861 |
– |
final AtomicInteger a = new AtomicInteger(0); |
2862 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2863 |
– |
if (!createIncomplete) f.complete(v1); |
2864 |
– |
final CompletableFuture<Integer> g = m.whenComplete |
2865 |
– |
(f, |
2866 |
– |
(Integer x, Throwable t) -> { |
2867 |
– |
m.checkExecutionMode(); |
2868 |
– |
threadAssertSame(x, v1); |
2869 |
– |
threadAssertNull(t); |
2870 |
– |
a.getAndIncrement(); |
2871 |
– |
}); |
2872 |
– |
if (createIncomplete) f.complete(v1); |
2873 |
– |
|
2874 |
– |
checkCompletedNormally(g, v1); |
2875 |
– |
checkCompletedNormally(f, v1); |
2876 |
– |
assertEquals(1, a.get()); |
2877 |
– |
}} |
2878 |
– |
|
2879 |
– |
/** |
2880 |
– |
* whenComplete action executes on exceptional completion, propagating |
2881 |
– |
* source result. |
2882 |
– |
*/ |
2883 |
– |
public void testWhenComplete_exceptionalCompletion() { |
2884 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2885 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2886 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2887 |
– |
{ |
2888 |
– |
final AtomicInteger a = new AtomicInteger(0); |
2889 |
– |
final CFException ex = new CFException(); |
2890 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2891 |
– |
if (!createIncomplete) f.completeExceptionally(ex); |
2892 |
– |
final CompletableFuture<Integer> g = m.whenComplete |
2893 |
– |
(f, |
2894 |
– |
(Integer x, Throwable t) -> { |
2895 |
– |
m.checkExecutionMode(); |
2896 |
– |
threadAssertNull(x); |
2897 |
– |
threadAssertSame(t, ex); |
2898 |
– |
a.getAndIncrement(); |
2899 |
– |
}); |
2900 |
– |
if (createIncomplete) f.completeExceptionally(ex); |
2901 |
– |
checkCompletedExceptionally(f, ex); |
2902 |
– |
checkCompletedWithWrappedException(g, ex); |
2903 |
– |
assertEquals(1, a.get()); |
2904 |
– |
}} |
2905 |
– |
|
2906 |
– |
/** |
2907 |
– |
* whenComplete action executes on cancelled source, propagating |
2908 |
– |
* CancellationException. |
2909 |
– |
*/ |
2910 |
– |
public void testWhenComplete_sourceCancelled() { |
2911 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2912 |
– |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2913 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2914 |
– |
{ |
2915 |
– |
final AtomicInteger a = new AtomicInteger(0); |
2916 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2917 |
– |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
2918 |
– |
final CompletableFuture<Integer> g = m.whenComplete |
2919 |
– |
(f, |
2920 |
– |
(Integer x, Throwable t) -> { |
2921 |
– |
m.checkExecutionMode(); |
2922 |
– |
threadAssertNull(x); |
2923 |
– |
threadAssertTrue(t instanceof CancellationException); |
2924 |
– |
a.getAndIncrement(); |
2925 |
– |
}); |
2926 |
– |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
2927 |
– |
|
2928 |
– |
checkCompletedWithWrappedCancellationException(g); |
2929 |
– |
checkCancelled(f); |
2930 |
– |
assertEquals(1, a.get()); |
2931 |
– |
}} |
2932 |
– |
|
2933 |
– |
/** |
2934 |
– |
* If a whenComplete action throws an exception when triggered by |
2935 |
– |
* a normal completion, it completes exceptionally |
2936 |
– |
*/ |
2937 |
– |
public void testWhenComplete_actionFailed() { |
2938 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2939 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2940 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2941 |
– |
{ |
2942 |
– |
final AtomicInteger a = new AtomicInteger(0); |
2943 |
– |
final CFException ex = new CFException(); |
2944 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2945 |
– |
if (!createIncomplete) f.complete(v1); |
2946 |
– |
final CompletableFuture<Integer> g = m.whenComplete |
2947 |
– |
(f, |
2948 |
– |
(Integer x, Throwable t) -> { |
2949 |
– |
m.checkExecutionMode(); |
2950 |
– |
threadAssertSame(x, v1); |
2951 |
– |
threadAssertNull(t); |
2952 |
– |
a.getAndIncrement(); |
2953 |
– |
throw ex; |
2954 |
– |
}); |
2955 |
– |
if (createIncomplete) f.complete(v1); |
2956 |
– |
checkCompletedNormally(f, v1); |
2957 |
– |
checkCompletedWithWrappedException(g, ex); |
2958 |
– |
assertEquals(1, a.get()); |
2959 |
– |
}} |
2960 |
– |
|
2961 |
– |
/** |
2962 |
– |
* If a whenComplete action throws an exception when triggered by |
2963 |
– |
* a source completion that also throws an exception, the source |
2964 |
– |
* exception takes precedence. |
2965 |
– |
*/ |
2966 |
– |
public void testWhenComplete_actionFailedSourceFailed() { |
2967 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2968 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2969 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2970 |
– |
{ |
2971 |
– |
final AtomicInteger a = new AtomicInteger(0); |
2972 |
– |
final CFException ex1 = new CFException(); |
2973 |
– |
final CFException ex2 = new CFException(); |
2974 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2975 |
– |
|
2976 |
– |
if (!createIncomplete) f.completeExceptionally(ex1); |
2977 |
– |
final CompletableFuture<Integer> g = m.whenComplete |
2978 |
– |
(f, |
2979 |
– |
(Integer x, Throwable t) -> { |
2980 |
– |
m.checkExecutionMode(); |
2981 |
– |
threadAssertSame(t, ex1); |
2982 |
– |
threadAssertNull(x); |
2983 |
– |
a.getAndIncrement(); |
2984 |
– |
throw ex2; |
2985 |
– |
}); |
2986 |
– |
if (createIncomplete) f.completeExceptionally(ex1); |
2987 |
– |
|
2988 |
– |
checkCompletedExceptionally(f, ex1); |
2989 |
– |
checkCompletedWithWrappedException(g, ex1); |
2990 |
– |
assertEquals(1, a.get()); |
2991 |
– |
}} |
2992 |
– |
|
2998 |
|
} |