131 |
|
} catch (ExecutionException success) { |
132 |
|
assertSame(ex, success.getCause()); |
133 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
134 |
< |
|
134 |
> |
|
135 |
|
assertTrue(f.isDone()); |
136 |
|
assertFalse(f.isCancelled()); |
137 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
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 |
350 |
|
f = new CompletableFuture<String>(); |
351 |
|
f.completeExceptionally(new IndexOutOfBoundsException()); |
352 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
353 |
+ |
|
354 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
355 |
+ |
f = new CompletableFuture<String>(); |
356 |
+ |
f.cancel(mayInterruptIfRunning); |
357 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
358 |
+ |
} |
359 |
|
} |
360 |
|
|
361 |
|
/** |
854 |
|
assertEquals(0, a.get()); |
855 |
|
}} |
856 |
|
|
846 |
– |
|
857 |
|
/** |
858 |
|
* exceptionally action completes with function value on source |
859 |
|
* exception |
902 |
|
}} |
903 |
|
|
904 |
|
/** |
905 |
+ |
* whenComplete action executes on normal completion, propagating |
906 |
+ |
* source result. |
907 |
+ |
*/ |
908 |
+ |
public void testWhenComplete_normalCompletion1() { |
909 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
910 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
911 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
912 |
+ |
{ |
913 |
+ |
final AtomicInteger a = new AtomicInteger(0); |
914 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
915 |
+ |
if (!createIncomplete) f.complete(v1); |
916 |
+ |
final CompletableFuture<Integer> g = m.whenComplete |
917 |
+ |
(f, |
918 |
+ |
(Integer x, Throwable t) -> { |
919 |
+ |
m.checkExecutionMode(); |
920 |
+ |
threadAssertSame(x, v1); |
921 |
+ |
threadAssertNull(t); |
922 |
+ |
a.getAndIncrement(); |
923 |
+ |
}); |
924 |
+ |
if (createIncomplete) f.complete(v1); |
925 |
+ |
|
926 |
+ |
checkCompletedNormally(g, v1); |
927 |
+ |
checkCompletedNormally(f, v1); |
928 |
+ |
assertEquals(1, a.get()); |
929 |
+ |
}} |
930 |
+ |
|
931 |
+ |
/** |
932 |
+ |
* whenComplete action executes on exceptional completion, propagating |
933 |
+ |
* source result. |
934 |
+ |
*/ |
935 |
+ |
public void testWhenComplete_exceptionalCompletion() { |
936 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
937 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
938 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
939 |
+ |
{ |
940 |
+ |
final AtomicInteger a = new AtomicInteger(0); |
941 |
+ |
final CFException ex = new CFException(); |
942 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
943 |
+ |
if (!createIncomplete) f.completeExceptionally(ex); |
944 |
+ |
final CompletableFuture<Integer> g = m.whenComplete |
945 |
+ |
(f, |
946 |
+ |
(Integer x, Throwable t) -> { |
947 |
+ |
m.checkExecutionMode(); |
948 |
+ |
threadAssertNull(x); |
949 |
+ |
threadAssertSame(t, ex); |
950 |
+ |
a.getAndIncrement(); |
951 |
+ |
}); |
952 |
+ |
if (createIncomplete) f.completeExceptionally(ex); |
953 |
+ |
|
954 |
+ |
checkCompletedWithWrappedException(g, ex); |
955 |
+ |
checkCompletedExceptionally(f, ex); |
956 |
+ |
assertEquals(1, a.get()); |
957 |
+ |
}} |
958 |
+ |
|
959 |
+ |
/** |
960 |
+ |
* whenComplete action executes on cancelled source, propagating |
961 |
+ |
* CancellationException. |
962 |
+ |
*/ |
963 |
+ |
public void testWhenComplete_sourceCancelled() { |
964 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
965 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
966 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
967 |
+ |
{ |
968 |
+ |
final AtomicInteger a = new AtomicInteger(0); |
969 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
970 |
+ |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
971 |
+ |
final CompletableFuture<Integer> g = m.whenComplete |
972 |
+ |
(f, |
973 |
+ |
(Integer x, Throwable t) -> { |
974 |
+ |
m.checkExecutionMode(); |
975 |
+ |
threadAssertNull(x); |
976 |
+ |
threadAssertTrue(t instanceof CancellationException); |
977 |
+ |
a.getAndIncrement(); |
978 |
+ |
}); |
979 |
+ |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
980 |
+ |
|
981 |
+ |
checkCompletedWithWrappedCancellationException(g); |
982 |
+ |
checkCancelled(f); |
983 |
+ |
assertEquals(1, a.get()); |
984 |
+ |
}} |
985 |
+ |
|
986 |
+ |
/** |
987 |
+ |
* If a whenComplete action throws an exception when triggered by |
988 |
+ |
* a normal completion, it completes exceptionally |
989 |
+ |
*/ |
990 |
+ |
public void testWhenComplete_actionFailed() { |
991 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
992 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
993 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
994 |
+ |
{ |
995 |
+ |
final AtomicInteger a = new AtomicInteger(0); |
996 |
+ |
final CFException ex = new CFException(); |
997 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
998 |
+ |
if (!createIncomplete) f.complete(v1); |
999 |
+ |
final CompletableFuture<Integer> g = m.whenComplete |
1000 |
+ |
(f, |
1001 |
+ |
(Integer x, Throwable t) -> { |
1002 |
+ |
m.checkExecutionMode(); |
1003 |
+ |
threadAssertSame(x, v1); |
1004 |
+ |
threadAssertNull(t); |
1005 |
+ |
a.getAndIncrement(); |
1006 |
+ |
throw ex; |
1007 |
+ |
}); |
1008 |
+ |
if (createIncomplete) f.complete(v1); |
1009 |
+ |
|
1010 |
+ |
checkCompletedWithWrappedException(g, ex); |
1011 |
+ |
checkCompletedNormally(f, v1); |
1012 |
+ |
assertEquals(1, a.get()); |
1013 |
+ |
}} |
1014 |
+ |
|
1015 |
+ |
/** |
1016 |
+ |
* If a whenComplete action throws an exception when triggered by |
1017 |
+ |
* a source completion that also throws an exception, the source |
1018 |
+ |
* exception takes precedence. |
1019 |
+ |
*/ |
1020 |
+ |
public void testWhenComplete_actionFailedSourceFailed() { |
1021 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
1022 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
1023 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
1024 |
+ |
{ |
1025 |
+ |
final AtomicInteger a = new AtomicInteger(0); |
1026 |
+ |
final CFException ex1 = new CFException(); |
1027 |
+ |
final CFException ex2 = new CFException(); |
1028 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1029 |
+ |
|
1030 |
+ |
if (!createIncomplete) f.completeExceptionally(ex1); |
1031 |
+ |
final CompletableFuture<Integer> g = m.whenComplete |
1032 |
+ |
(f, |
1033 |
+ |
(Integer x, Throwable t) -> { |
1034 |
+ |
m.checkExecutionMode(); |
1035 |
+ |
threadAssertSame(t, ex1); |
1036 |
+ |
threadAssertNull(x); |
1037 |
+ |
a.getAndIncrement(); |
1038 |
+ |
throw ex2; |
1039 |
+ |
}); |
1040 |
+ |
if (createIncomplete) f.completeExceptionally(ex1); |
1041 |
+ |
|
1042 |
+ |
checkCompletedWithWrappedException(g, ex1); |
1043 |
+ |
checkCompletedExceptionally(f, ex1); |
1044 |
+ |
assertEquals(1, a.get()); |
1045 |
+ |
}} |
1046 |
+ |
|
1047 |
+ |
/** |
1048 |
|
* handle action completes normally with function value on normal |
1049 |
|
* completion of source |
1050 |
|
*/ |
1825 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1826 |
|
final Noop r = new Noop(m); |
1827 |
|
|
1675 |
– |
|
1828 |
|
(fFirst ? f : g).complete(v1); |
1829 |
|
if (!createIncomplete) |
1830 |
|
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
2993 |
|
assertSame(f, f.toCompletableFuture()); |
2994 |
|
} |
2995 |
|
|
2844 |
– |
/** |
2845 |
– |
* whenComplete action executes on normal completion, propagating |
2846 |
– |
* source result. |
2847 |
– |
*/ |
2848 |
– |
public void testWhenComplete_normalCompletion1() { |
2849 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2850 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2851 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2852 |
– |
{ |
2853 |
– |
final AtomicInteger a = new AtomicInteger(0); |
2854 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2855 |
– |
if (!createIncomplete) f.complete(v1); |
2856 |
– |
final CompletableFuture<Integer> g = m.whenComplete |
2857 |
– |
(f, |
2858 |
– |
(Integer x, Throwable t) -> { |
2859 |
– |
m.checkExecutionMode(); |
2860 |
– |
threadAssertSame(x, v1); |
2861 |
– |
threadAssertNull(t); |
2862 |
– |
a.getAndIncrement(); |
2863 |
– |
}); |
2864 |
– |
if (createIncomplete) f.complete(v1); |
2865 |
– |
|
2866 |
– |
checkCompletedNormally(g, v1); |
2867 |
– |
checkCompletedNormally(f, v1); |
2868 |
– |
assertEquals(1, a.get()); |
2869 |
– |
}} |
2870 |
– |
|
2871 |
– |
/** |
2872 |
– |
* whenComplete action executes on exceptional completion, propagating |
2873 |
– |
* source result. |
2874 |
– |
*/ |
2875 |
– |
public void testWhenComplete_exceptionalCompletion() { |
2876 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2877 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2878 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2879 |
– |
{ |
2880 |
– |
final AtomicInteger a = new AtomicInteger(0); |
2881 |
– |
final CFException ex = new CFException(); |
2882 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2883 |
– |
if (!createIncomplete) f.completeExceptionally(ex); |
2884 |
– |
final CompletableFuture<Integer> g = m.whenComplete |
2885 |
– |
(f, |
2886 |
– |
(Integer x, Throwable t) -> { |
2887 |
– |
m.checkExecutionMode(); |
2888 |
– |
threadAssertNull(x); |
2889 |
– |
threadAssertSame(t, ex); |
2890 |
– |
a.getAndIncrement(); |
2891 |
– |
}); |
2892 |
– |
if (createIncomplete) f.completeExceptionally(ex); |
2893 |
– |
checkCompletedExceptionally(f, ex); |
2894 |
– |
checkCompletedWithWrappedException(g, ex); |
2895 |
– |
assertEquals(1, a.get()); |
2896 |
– |
}} |
2897 |
– |
|
2898 |
– |
/** |
2899 |
– |
* whenComplete action executes on cancelled source, propagating |
2900 |
– |
* CancellationException. |
2901 |
– |
*/ |
2902 |
– |
public void testWhenComplete_sourceCancelled() { |
2903 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2904 |
– |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2905 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2906 |
– |
{ |
2907 |
– |
final AtomicInteger a = new AtomicInteger(0); |
2908 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2909 |
– |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
2910 |
– |
final CompletableFuture<Integer> g = m.whenComplete |
2911 |
– |
(f, |
2912 |
– |
(Integer x, Throwable t) -> { |
2913 |
– |
m.checkExecutionMode(); |
2914 |
– |
threadAssertNull(x); |
2915 |
– |
threadAssertTrue(t instanceof CancellationException); |
2916 |
– |
a.getAndIncrement(); |
2917 |
– |
}); |
2918 |
– |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
2919 |
– |
|
2920 |
– |
checkCompletedWithWrappedCancellationException(g); |
2921 |
– |
checkCancelled(f); |
2922 |
– |
assertEquals(1, a.get()); |
2923 |
– |
}} |
2924 |
– |
|
2925 |
– |
/** |
2926 |
– |
* If a whenComplete action throws an exception when triggered by |
2927 |
– |
* a normal completion, it completes exceptionally |
2928 |
– |
*/ |
2929 |
– |
public void testWhenComplete_actionFailed() { |
2930 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2931 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2932 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2933 |
– |
{ |
2934 |
– |
final AtomicInteger a = new AtomicInteger(0); |
2935 |
– |
final CFException ex = new CFException(); |
2936 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2937 |
– |
if (!createIncomplete) f.complete(v1); |
2938 |
– |
final CompletableFuture<Integer> g = m.whenComplete |
2939 |
– |
(f, |
2940 |
– |
(Integer x, Throwable t) -> { |
2941 |
– |
m.checkExecutionMode(); |
2942 |
– |
threadAssertSame(x, v1); |
2943 |
– |
threadAssertNull(t); |
2944 |
– |
a.getAndIncrement(); |
2945 |
– |
throw ex; |
2946 |
– |
}); |
2947 |
– |
if (createIncomplete) f.complete(v1); |
2948 |
– |
checkCompletedNormally(f, v1); |
2949 |
– |
checkCompletedWithWrappedException(g, ex); |
2950 |
– |
assertEquals(1, a.get()); |
2951 |
– |
}} |
2952 |
– |
|
2953 |
– |
/** |
2954 |
– |
* If a whenComplete action throws an exception when triggered by |
2955 |
– |
* a source completion that also throws an exception, the source |
2956 |
– |
* exception takes precedence. |
2957 |
– |
*/ |
2958 |
– |
public void testWhenComplete_actionFailedSourceFailed() { |
2959 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2960 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2961 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2962 |
– |
{ |
2963 |
– |
final AtomicInteger a = new AtomicInteger(0); |
2964 |
– |
final CFException ex1 = new CFException(); |
2965 |
– |
final CFException ex2 = new CFException(); |
2966 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2967 |
– |
|
2968 |
– |
if (!createIncomplete) f.completeExceptionally(ex1); |
2969 |
– |
final CompletableFuture<Integer> g = m.whenComplete |
2970 |
– |
(f, |
2971 |
– |
(Integer x, Throwable t) -> { |
2972 |
– |
m.checkExecutionMode(); |
2973 |
– |
threadAssertSame(t, ex1); |
2974 |
– |
threadAssertNull(x); |
2975 |
– |
a.getAndIncrement(); |
2976 |
– |
throw ex2; |
2977 |
– |
}); |
2978 |
– |
if (createIncomplete) f.completeExceptionally(ex1); |
2979 |
– |
|
2980 |
– |
checkCompletedExceptionally(f, ex1); |
2981 |
– |
checkCompletedWithWrappedException(g, ex1); |
2982 |
– |
assertEquals(1, a.get()); |
2983 |
– |
}} |
2984 |
– |
|
2996 |
|
} |