878 Consumer<? super T> a); |
934 Consumer<? super T> a); |
879 public abstract <T,U> CompletableFuture<U> applyToEither |
935 public abstract <T,U> CompletableFuture<U> applyToEither |
880 (CompletableFuture<T> f, |
936 (CompletableFuture<T> f, |
881 CompletionStage<? extends T> g, |
937 CompletionStage<? extends T> g, |
882 Function<? super T,U> a); |
938 Function<? super T,U> a); |
|
939 public abstract <T> CompletableFuture<T> exceptionally |
|
940 (CompletableFuture<T> f, |
|
941 Function<Throwable, ? extends T> fn); |
|
942 public abstract <T> CompletableFuture<T> exceptionallyCompose |
|
943 (CompletableFuture<T> f, |
|
944 Function<Throwable, ? extends CompletionStage<T>> fn); |
883 } |
945 } |
884 |
946 |
885 /** |
947 /** |
886 * exceptionally action is not invoked when source completes |
948 * exceptionally action is not invoked when source completes |
887 * normally, and source result is propagated |
949 * normally, and source result is propagated |
888 */ |
950 */ |
889 public void testExceptionally_normalCompletion() { |
951 public void testExceptionally_normalCompletion() { |
|
952 for (ExecutionMode m : ExecutionMode.values()) |
890 for (boolean createIncomplete : new boolean[] { true, false }) |
953 for (boolean createIncomplete : new boolean[] { true, false }) |
891 for (Integer v1 : new Integer[] { 1, null }) |
954 for (Integer v1 : new Integer[] { 1, null }) |
892 { |
955 { |
893 final AtomicInteger a = new AtomicInteger(0); |
|
894 final CompletableFuture<Integer> f = new CompletableFuture<>(); |
956 final CompletableFuture<Integer> f = new CompletableFuture<>(); |
895 if (!createIncomplete) assertTrue(f.complete(v1)); |
957 if (!createIncomplete) assertTrue(f.complete(v1)); |
896 final CompletableFuture<Integer> g = f.exceptionally |
958 final CompletableFuture<Integer> g = m.exceptionally |
897 ((Throwable t) -> { |
959 (f, (Throwable t) -> { |
898 a.getAndIncrement(); |
|
899 threadFail("should not be called"); |
960 threadFail("should not be called"); |
900 return null; // unreached |
961 return null; // unreached |
901 }); |
962 }); |
902 if (createIncomplete) assertTrue(f.complete(v1)); |
963 if (createIncomplete) assertTrue(f.complete(v1)); |
903 |
964 |
904 checkCompletedNormally(g, v1); |
965 checkCompletedNormally(g, v1); |
905 checkCompletedNormally(f, v1); |
966 checkCompletedNormally(f, v1); |
906 assertEquals(0, a.get()); |
|
907 }} |
967 }} |
908 |
968 |
909 /** |
969 /** |
910 * exceptionally action completes with function value on source |
970 * exceptionally action completes with function value on source |
911 * exception |
971 * exception |
912 */ |
972 */ |
913 public void testExceptionally_exceptionalCompletion() { |
973 public void testExceptionally_exceptionalCompletion() { |
|
974 for (ExecutionMode m : ExecutionMode.values()) |
914 for (boolean createIncomplete : new boolean[] { true, false }) |
975 for (boolean createIncomplete : new boolean[] { true, false }) |
915 for (Integer v1 : new Integer[] { 1, null }) |
976 for (Integer v1 : new Integer[] { 1, null }) |
916 { |
977 { |
917 final AtomicInteger a = new AtomicInteger(0); |
978 final AtomicInteger a = new AtomicInteger(0); |
918 final CFException ex = new CFException(); |
979 final CFException ex = new CFException(); |
919 final CompletableFuture<Integer> f = new CompletableFuture<>(); |
980 final CompletableFuture<Integer> f = new CompletableFuture<>(); |
920 if (!createIncomplete) f.completeExceptionally(ex); |
981 if (!createIncomplete) f.completeExceptionally(ex); |
921 final CompletableFuture<Integer> g = f.exceptionally |
982 final CompletableFuture<Integer> g = m.exceptionally |
922 ((Throwable t) -> { |
983 (f, (Throwable t) -> { |
923 ExecutionMode.SYNC.checkExecutionMode(); |
984 m.checkExecutionMode(); |
924 threadAssertSame(t, ex); |
985 threadAssertSame(t, ex); |
925 a.getAndIncrement(); |
986 a.getAndIncrement(); |
926 return v1; |
987 return v1; |
927 }); |
988 }); |
928 if (createIncomplete) f.completeExceptionally(ex); |
989 if (createIncomplete) f.completeExceptionally(ex); |
3114 checkCompletedExceptionally(g, ex); |
3176 checkCompletedExceptionally(g, ex); |
3115 checkCompletedWithWrappedException(h, ex); |
3177 checkCompletedWithWrappedException(h, ex); |
3116 checkCompletedNormally(f, v1); |
3178 checkCompletedNormally(f, v1); |
3117 }} |
3179 }} |
3118 |
3180 |
|
3181 /** |
|
3182 * exceptionallyCompose result completes normally after normal |
|
3183 * completion of source |
|
3184 */ |
|
3185 public void testExceptionallyCompose_normalCompletion() { |
|
3186 for (ExecutionMode m : ExecutionMode.values()) |
|
3187 for (boolean createIncomplete : new boolean[] { true, false }) |
|
3188 for (Integer v1 : new Integer[] { 1, null }) |
|
3189 { |
|
3190 final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
3191 final ExceptionalCompletableFutureFunction r = |
|
3192 new ExceptionalCompletableFutureFunction(m); |
|
3193 if (!createIncomplete) assertTrue(f.complete(v1)); |
|
3194 final CompletableFuture<Integer> g = m.exceptionallyCompose(f, r); |
|
3195 if (createIncomplete) assertTrue(f.complete(v1)); |
|
3196 |
|
3197 checkCompletedNormally(f, v1); |
|
3198 checkCompletedNormally(g, v1); |
|
3199 r.assertNotInvoked(); |
|
3200 }} |
|
3201 |
|
3202 /** |
|
3203 * exceptionallyCompose result completes normally after exceptional |
|
3204 * completion of source |
|
3205 */ |
|
3206 public void testExceptionallyCompose_exceptionalCompletion() { |
|
3207 for (ExecutionMode m : ExecutionMode.values()) |
|
3208 for (boolean createIncomplete : new boolean[] { true, false }) |
|
3209 { |
|
3210 final CFException ex = new CFException(); |
|
3211 final ExceptionalCompletableFutureFunction r = |
|
3212 new ExceptionalCompletableFutureFunction(m); |
|
3213 final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
3214 if (!createIncomplete) f.completeExceptionally(ex); |
|
3215 final CompletableFuture<Integer> g = m.exceptionallyCompose(f, r); |
|
3216 if (createIncomplete) f.completeExceptionally(ex); |
|
3217 |
|
3218 checkCompletedExceptionally(f, ex); |
|
3219 checkCompletedNormally(g, r.value); |
|
3220 r.assertInvoked(); |
|
3221 }} |
|
3222 |
|
3223 /** |
|
3224 * exceptionallyCompose completes exceptionally on exception if action does |
|
3225 */ |
|
3226 public void testExceptionallyCompose_actionFailed() { |
|
3227 for (ExecutionMode m : ExecutionMode.values()) |
|
3228 for (boolean createIncomplete : new boolean[] { true, false }) |
|
3229 { |
|
3230 final CFException ex = new CFException(); |
|
3231 final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
3232 final FailingExceptionalCompletableFutureFunction r |
|
3233 = new FailingExceptionalCompletableFutureFunction(m); |
|
3234 if (!createIncomplete) f.completeExceptionally(ex); |
|
3235 final CompletableFuture<Integer> g = m.exceptionallyCompose(f, r); |
|
3236 if (createIncomplete) f.completeExceptionally(ex); |
|
3237 |
|
3238 checkCompletedExceptionally(f, ex); |
|
3239 checkCompletedWithWrappedException(g, r.ex); |
|
3240 r.assertInvoked(); |
|
3241 }} |
|
3242 |
|
3243 /** |
|
3244 * exceptionallyCompose result completes exceptionally if the |
|
3245 * result of the action does |
|
3246 */ |
|
3247 public void testExceptionallyCompose_actionReturnsFailingFuture() { |
|
3248 for (ExecutionMode m : ExecutionMode.values()) |
|
3249 for (int order = 0; order < 6; order++) |
|
3250 { |
|
3251 final CFException ex0 = new CFException(); |
|
3252 final CFException ex = new CFException(); |
|
3253 final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
3254 final CompletableFuture<Integer> g = new CompletableFuture<>(); |
|
3255 final CompletableFuture<Integer> h; |
|
3256 // Test all permutations of orders |
|
3257 switch (order) { |
|
3258 case 0: |
|
3259 assertTrue(f.completeExceptionally(ex0)); |
|
3260 assertTrue(g.completeExceptionally(ex)); |
|
3261 h = m.exceptionallyCompose(f, (x -> g)); |
|
3262 break; |
|
3263 case 1: |
|
3264 assertTrue(f.completeExceptionally(ex0)); |
|
3265 h = m.exceptionallyCompose(f, (x -> g)); |
|
3266 assertTrue(g.completeExceptionally(ex)); |
|
3267 break; |
|
3268 case 2: |
|
3269 assertTrue(g.completeExceptionally(ex)); |
|
3270 assertTrue(f.completeExceptionally(ex0)); |
|
3271 h = m.exceptionallyCompose(f, (x -> g)); |
|
3272 break; |
|
3273 case 3: |
|
3274 assertTrue(g.completeExceptionally(ex)); |
|
3275 h = m.exceptionallyCompose(f, (x -> g)); |
|
3276 assertTrue(f.completeExceptionally(ex0)); |
|
3277 break; |
|
3278 case 4: |
|
3279 h = m.exceptionallyCompose(f, (x -> g)); |
|
3280 assertTrue(f.completeExceptionally(ex0)); |
|
3281 assertTrue(g.completeExceptionally(ex)); |
|
3282 break; |
|
3283 case 5: |
|
3284 h = m.exceptionallyCompose(f, (x -> g)); |
|
3285 assertTrue(f.completeExceptionally(ex0)); |
|
3286 assertTrue(g.completeExceptionally(ex)); |
|
3287 break; |
|
3288 default: throw new AssertionError(); |
|
3289 } |
|
3290 |
|
3291 checkCompletedExceptionally(g, ex); |
|
3292 checkCompletedWithWrappedException(h, ex); |
|
3293 checkCompletedExceptionally(f, ex0); |
|
3294 }} |
|
3295 |
3119 // other static methods |
3296 // other static methods |
3120 |
3297 |
3121 /** |
3298 /** |
3122 * allOf(no component futures) returns a future completed normally |
3299 * allOf(no component futures) returns a future completed normally |
3123 * with the value null |
3300 * with the value null |
4525 |
4702 |
4526 // static <U> boolean isDone2(CompletionStage<U> stage) { |
4703 // static <U> boolean isDone2(CompletionStage<U> stage) { |
4527 // return stage.toCompletableFuture().copy().isDone(); |
4704 // return stage.toCompletableFuture().copy().isDone(); |
4528 // } |
4705 // } |
4529 |
4706 |
|
4707 // For testing default implementations |
|
4708 // Only non-default interface methods defined. |
|
4709 static final class DelegatedCompletionStage<T> implements CompletionStage<T> { |
|
4710 final CompletableFuture<T> cf; |
|
4711 DelegatedCompletionStage(CompletableFuture<T> cf) { this.cf = cf; } |
|
4712 public CompletableFuture<T> toCompletableFuture() { |
|
4713 return cf; } |
|
4714 public CompletionStage<Void> thenRun |
|
4715 (Runnable action) { |
|
4716 return cf.thenRun(action); } |
|
4717 public CompletionStage<Void> thenRunAsync |
|
4718 (Runnable action) { |
|
4719 return cf.thenRunAsync(action); } |
|
4720 public CompletionStage<Void> thenRunAsync |
|
4721 (Runnable action, |
|
4722 Executor executor) { |
|
4723 return cf.thenRunAsync(action, executor); } |
|
4724 public CompletionStage<Void> thenAccept |
|
4725 (Consumer<? super T> action) { |
|
4726 return cf.thenAccept(action); } |
|
4727 public CompletionStage<Void> thenAcceptAsync |
|
4728 (Consumer<? super T> action) { |
|
4729 return cf.thenAcceptAsync(action); } |
|
4730 public CompletionStage<Void> thenAcceptAsync |
|
4731 (Consumer<? super T> action, |
|
4732 Executor executor) { |
|
4733 return cf.thenAcceptAsync(action, executor); } |
|
4734 public <U> CompletionStage<U> thenApply |
|
4735 (Function<? super T,? extends U> a) { |
|
4736 return cf.thenApply(a); } |
|
4737 public <U> CompletionStage<U> thenApplyAsync |
|
4738 (Function<? super T,? extends U> fn) { |
|
4739 return cf.thenApplyAsync(fn); } |
|
4740 public <U> CompletionStage<U> thenApplyAsync |
|
4741 (Function<? super T,? extends U> fn, |
|
4742 Executor executor) { |
|
4743 return cf.thenApplyAsync(fn, executor); } |
|
4744 public <U,V> CompletionStage<V> thenCombine |
|
4745 (CompletionStage<? extends U> other, |
|
4746 BiFunction<? super T,? super U,? extends V> fn) { |
|
4747 return cf.thenCombine(other, fn); } |
|
4748 public <U,V> CompletionStage<V> thenCombineAsync |
|
4749 (CompletionStage<? extends U> other, |
|
4750 BiFunction<? super T,? super U,? extends V> fn) { |
|
4751 return cf.thenCombineAsync(other, fn); } |
|
4752 public <U,V> CompletionStage<V> thenCombineAsync |
|
4753 (CompletionStage<? extends U> other, |
|
4754 BiFunction<? super T,? super U,? extends V> fn, |
|
4755 Executor executor) { |
|
4756 return cf.thenCombineAsync(other, fn, executor); } |
|
4757 public <U> CompletionStage<Void> thenAcceptBoth |
|
4758 (CompletionStage<? extends U> other, |
|
4759 BiConsumer<? super T, ? super U> action) { |
|
4760 return cf.thenAcceptBoth(other, action); } |
|
4761 public <U> CompletionStage<Void> thenAcceptBothAsync |
|
4762 (CompletionStage<? extends U> other, |
|
4763 BiConsumer<? super T, ? super U> action) { |
|
4764 return cf.thenAcceptBothAsync(other, action); } |
|
4765 public <U> CompletionStage<Void> thenAcceptBothAsync |
|
4766 (CompletionStage<? extends U> other, |
|
4767 BiConsumer<? super T, ? super U> action, |
|
4768 Executor executor) { |
|
4769 return cf.thenAcceptBothAsync(other, action, executor); } |
|
4770 public CompletionStage<Void> runAfterBoth |
|
4771 (CompletionStage<?> other, |
|
4772 Runnable action) { |
|
4773 return cf.runAfterBoth(other, action); } |
|
4774 public CompletionStage<Void> runAfterBothAsync |
|
4775 (CompletionStage<?> other, |
|
4776 Runnable action) { |
|
4777 return cf.runAfterBothAsync(other, action); } |
|
4778 public CompletionStage<Void> runAfterBothAsync |
|
4779 (CompletionStage<?> other, |
|
4780 Runnable action, |
|
4781 Executor executor) { |
|
4782 return cf.runAfterBothAsync(other, action, executor); } |
|
4783 public <U> CompletionStage<U> applyToEither |
|
4784 (CompletionStage<? extends T> other, |
|
4785 Function<? super T, U> fn) { |
|
4786 return cf.applyToEither(other, fn); } |
|
4787 public <U> CompletionStage<U> applyToEitherAsync |
|
4788 (CompletionStage<? extends T> other, |
|
4789 Function<? super T, U> fn) { |
|
4790 return cf.applyToEitherAsync(other, fn); } |
|
4791 public <U> CompletionStage<U> applyToEitherAsync |
|
4792 (CompletionStage<? extends T> other, |
|
4793 Function<? super T, U> fn, |
|
4794 Executor executor) { |
|
4795 return cf.applyToEitherAsync(other, fn, executor); } |
|
4796 public CompletionStage<Void> acceptEither |
|
4797 (CompletionStage<? extends T> other, |
|
4798 Consumer<? super T> action) { |
|
4799 return cf.acceptEither(other, action); } |
|
4800 public CompletionStage<Void> acceptEitherAsync |
|
4801 (CompletionStage<? extends T> other, |
|
4802 Consumer<? super T> action) { |
|
4803 return cf.acceptEitherAsync(other, action); } |
|
4804 public CompletionStage<Void> acceptEitherAsync |
|
4805 (CompletionStage<? extends T> other, |
|
4806 Consumer<? super T> action, |
|
4807 Executor executor) { |
|
4808 return cf.acceptEitherAsync(other, action, executor); } |
|
4809 public CompletionStage<Void> runAfterEither |
|
4810 (CompletionStage<?> other, |
|
4811 Runnable action) { |
|
4812 return cf.runAfterEither(other, action); } |
|
4813 public CompletionStage<Void> runAfterEitherAsync |
|
4814 (CompletionStage<?> other, |
|
4815 Runnable action) { |
|
4816 return cf.runAfterEitherAsync(other, action); } |
|
4817 public CompletionStage<Void> runAfterEitherAsync |
|
4818 (CompletionStage<?> other, |
|
4819 Runnable action, |
|
4820 Executor executor) { |
|
4821 return cf.runAfterEitherAsync(other, action, executor); } |
|
4822 public <U> CompletionStage<U> thenCompose |
|
4823 (Function<? super T, ? extends CompletionStage<U>> fn) { |
|
4824 return cf.thenCompose(fn); } |
|
4825 public <U> CompletionStage<U> thenComposeAsync |
|
4826 (Function<? super T, ? extends CompletionStage<U>> fn) { |
|
4827 return cf.thenComposeAsync(fn); } |
|
4828 public <U> CompletionStage<U> thenComposeAsync |
|
4829 (Function<? super T, ? extends CompletionStage<U>> fn, |
|
4830 Executor executor) { |
|
4831 return cf.thenComposeAsync(fn, executor); } |
|
4832 public <U> CompletionStage<U> handle |
|
4833 (BiFunction<? super T, Throwable, ? extends U> fn) { |
|
4834 return cf.handle(fn); } |
|
4835 public <U> CompletionStage<U> handleAsync |
|
4836 (BiFunction<? super T, Throwable, ? extends U> fn) { |
|
4837 return cf.handleAsync(fn); } |
|
4838 public <U> CompletionStage<U> handleAsync |
|
4839 (BiFunction<? super T, Throwable, ? extends U> fn, |
|
4840 Executor executor) { |
|
4841 return cf.handleAsync(fn, executor); } |
|
4842 public CompletionStage<T> whenComplete |
|
4843 (BiConsumer<? super T, ? super Throwable> action) { |
|
4844 return cf.whenComplete(action); } |
|
4845 public CompletionStage<T> whenCompleteAsync |
|
4846 (BiConsumer<? super T, ? super Throwable> action) { |
|
4847 return cf.whenCompleteAsync(action); } |
|
4848 public CompletionStage<T> whenCompleteAsync |
|
4849 (BiConsumer<? super T, ? super Throwable> action, |
|
4850 Executor executor) { |
|
4851 return cf.whenCompleteAsync(action, executor); } |
|
4852 public CompletionStage<T> exceptionally |
|
4853 (Function<Throwable, ? extends T> fn) { |
|
4854 return cf.exceptionally(fn); } |
|
4855 } |
|
4856 |
|
4857 /** |
|
4858 * default-implemented exceptionallyAsync action is not invoked when |
|
4859 * source completes normally, and source result is propagated |
|
4860 */ |
|
4861 public void testDefaultExceptionallyAsync_normalCompletion() { |
|
4862 for (boolean createIncomplete : new boolean[] { true, false }) |
|
4863 for (Integer v1 : new Integer[] { 1, null }) |
|
4864 { |
|
4865 final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
4866 final DelegatedCompletionStage<Integer> d = |
|
4867 new DelegatedCompletionStage<Integer>(f); |
|
4868 if (!createIncomplete) assertTrue(f.complete(v1)); |
|
4869 final CompletionStage<Integer> g = d.exceptionallyAsync |
|
4870 ((Throwable t) -> { |
|
4871 threadFail("should not be called"); |
|
4872 return null; // unreached |
|
4873 }); |
|
4874 if (createIncomplete) assertTrue(f.complete(v1)); |
|
4875 |
|
4876 checkCompletedNormally(g.toCompletableFuture(), v1); |
|
4877 }} |
|
4878 |
|
4879 /** |
|
4880 * default-implemented exceptionallyAsync action completes with |
|
4881 * function value on source exception |
|
4882 */ |
|
4883 public void testDefaultExceptionallyAsync_exceptionalCompletion() { |
|
4884 for (boolean createIncomplete : new boolean[] { true, false }) |
|
4885 for (Integer v1 : new Integer[] { 1, null }) |
|
4886 { |
|
4887 final AtomicInteger a = new AtomicInteger(0); |
|
4888 final CFException ex = new CFException(); |
|
4889 final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
4890 final DelegatedCompletionStage<Integer> d = |
|
4891 new DelegatedCompletionStage<Integer>(f); |
|
4892 if (!createIncomplete) f.completeExceptionally(ex); |
|
4893 final CompletionStage<Integer> g = d.exceptionallyAsync |
|
4894 ((Throwable t) -> { |
|
4895 threadAssertSame(t, ex); |
|
4896 a.getAndIncrement(); |
|
4897 return v1; |
|
4898 }); |
|
4899 if (createIncomplete) f.completeExceptionally(ex); |
|
4900 |
|
4901 checkCompletedNormally(g.toCompletableFuture(), v1); |
|
4902 assertEquals(1, a.get()); |
|
4903 }} |
|
4904 |
|
4905 /** |
|
4906 * Under default implementation, if an "exceptionally action" |
|
4907 * throws an exception, it completes exceptionally with that |
|
4908 * exception |
|
4909 */ |
|
4910 public void testDefaultExceptionallyAsync_exceptionalCompletionActionFailed() { |
|
4911 for (boolean createIncomplete : new boolean[] { true, false }) |
|
4912 { |
|
4913 final AtomicInteger a = new AtomicInteger(0); |
|
4914 final CFException ex1 = new CFException(); |
|
4915 final CFException ex2 = new CFException(); |
|
4916 final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
4917 final DelegatedCompletionStage<Integer> d = |
|
4918 new DelegatedCompletionStage<Integer>(f); |
|
4919 if (!createIncomplete) f.completeExceptionally(ex1); |
|
4920 final CompletionStage<Integer> g = d.exceptionallyAsync |
|
4921 ((Throwable t) -> { |
|
4922 threadAssertSame(t, ex1); |
|
4923 a.getAndIncrement(); |
|
4924 throw ex2; |
|
4925 }); |
|
4926 if (createIncomplete) f.completeExceptionally(ex1); |
|
4927 |
|
4928 checkCompletedWithWrappedException(g.toCompletableFuture(), ex2); |
|
4929 checkCompletedExceptionally(f, ex1); |
|
4930 checkCompletedExceptionally(d.toCompletableFuture(), ex1); |
|
4931 assertEquals(1, a.get()); |
|
4932 }} |
|
4933 |
|
4934 /** |
|
4935 * default-implemented exceptionallyCompose result completes |
|
4936 * normally after normal completion of source |
|
4937 */ |
|
4938 public void testDefaultExceptionallyCompose_normalCompletion() { |
|
4939 for (boolean createIncomplete : new boolean[] { true, false }) |
|
4940 for (Integer v1 : new Integer[] { 1, null }) |
|
4941 { |
|
4942 final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
4943 final ExceptionalCompletableFutureFunction r = |
|
4944 new ExceptionalCompletableFutureFunction(ExecutionMode.SYNC); |
|
4945 final DelegatedCompletionStage<Integer> d = |
|
4946 new DelegatedCompletionStage<Integer>(f); |
|
4947 if (!createIncomplete) assertTrue(f.complete(v1)); |
|
4948 final CompletionStage<Integer> g = d.exceptionallyCompose(r); |
|
4949 if (createIncomplete) assertTrue(f.complete(v1)); |
|
4950 |
|
4951 checkCompletedNormally(f, v1); |
|
4952 checkCompletedNormally(g.toCompletableFuture(), v1); |
|
4953 r.assertNotInvoked(); |
|
4954 }} |
|
4955 |
|
4956 /** |
|
4957 * default-implemented exceptionallyCompose result completes |
|
4958 * normally after exceptional completion of source |
|
4959 */ |
|
4960 public void testDefaultExceptionallyCompose_exceptionalCompletion() { |
|
4961 for (boolean createIncomplete : new boolean[] { true, false }) |
|
4962 { |
|
4963 final CFException ex = new CFException(); |
|
4964 final ExceptionalCompletableFutureFunction r = |
|
4965 new ExceptionalCompletableFutureFunction(ExecutionMode.SYNC); |
|
4966 final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
4967 final DelegatedCompletionStage<Integer> d = |
|
4968 new DelegatedCompletionStage<Integer>(f); |
|
4969 if (!createIncomplete) f.completeExceptionally(ex); |
|
4970 final CompletionStage<Integer> g = d.exceptionallyCompose(r); |
|
4971 if (createIncomplete) f.completeExceptionally(ex); |
|
4972 |
|
4973 checkCompletedExceptionally(f, ex); |
|
4974 checkCompletedNormally(g.toCompletableFuture(), r.value); |
|
4975 r.assertInvoked(); |
|
4976 }} |
|
4977 |
|
4978 /** |
|
4979 * default-implemented exceptionallyCompose completes |
|
4980 * exceptionally on exception if action does |
|
4981 */ |
|
4982 public void testDefaultExceptionallyCompose_actionFailed() { |
|
4983 for (boolean createIncomplete : new boolean[] { true, false }) |
|
4984 { |
|
4985 final CFException ex = new CFException(); |
|
4986 final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
4987 final FailingExceptionalCompletableFutureFunction r |
|
4988 = new FailingExceptionalCompletableFutureFunction(ExecutionMode.SYNC); |
|
4989 final DelegatedCompletionStage<Integer> d = |
|
4990 new DelegatedCompletionStage<Integer>(f); |
|
4991 if (!createIncomplete) f.completeExceptionally(ex); |
|
4992 final CompletionStage<Integer> g = d.exceptionallyCompose(r); |
|
4993 if (createIncomplete) f.completeExceptionally(ex); |
|
4994 |
|
4995 checkCompletedExceptionally(f, ex); |
|
4996 checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex); |
|
4997 r.assertInvoked(); |
|
4998 }} |
|
4999 |
|
5000 /** |
|
5001 * default-implemented exceptionallyComposeAsync result completes |
|
5002 * normally after normal completion of source |
|
5003 */ |
|
5004 public void testDefaultExceptionallyComposeAsync_normalCompletion() { |
|
5005 for (boolean createIncomplete : new boolean[] { true, false }) |
|
5006 for (Integer v1 : new Integer[] { 1, null }) |
|
5007 { |
|
5008 final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
5009 final ExceptionalCompletableFutureFunction r = |
|
5010 new ExceptionalCompletableFutureFunction(ExecutionMode.ASYNC); |
|
5011 final DelegatedCompletionStage<Integer> d = |
|
5012 new DelegatedCompletionStage<Integer>(f); |
|
5013 if (!createIncomplete) assertTrue(f.complete(v1)); |
|
5014 final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r); |
|
5015 if (createIncomplete) assertTrue(f.complete(v1)); |
|
5016 |
|
5017 checkCompletedNormally(f, v1); |
|
5018 checkCompletedNormally(g.toCompletableFuture(), v1); |
|
5019 r.assertNotInvoked(); |
|
5020 }} |
|
5021 |
|
5022 /** |
|
5023 * default-implemented exceptionallyComposeAsync result completes |
|
5024 * normally after exceptional completion of source |
|
5025 */ |
|
5026 public void testDefaultExceptionallyComposeAsync_exceptionalCompletion() { |
|
5027 for (boolean createIncomplete : new boolean[] { true, false }) |
|
5028 { |
|
5029 final CFException ex = new CFException(); |
|
5030 final ExceptionalCompletableFutureFunction r = |
|
5031 new ExceptionalCompletableFutureFunction(ExecutionMode.ASYNC); |
|
5032 final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
5033 final DelegatedCompletionStage<Integer> d = |
|
5034 new DelegatedCompletionStage<Integer>(f); |
|
5035 if (!createIncomplete) f.completeExceptionally(ex); |
|
5036 final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r); |
|
5037 if (createIncomplete) f.completeExceptionally(ex); |
|
5038 |
|
5039 checkCompletedExceptionally(f, ex); |
|
5040 checkCompletedNormally(g.toCompletableFuture(), r.value); |
|
5041 r.assertInvoked(); |
|
5042 }} |
|
5043 |
|
5044 /** |
|
5045 * default-implemented exceptionallyComposeAsync completes |
|
5046 * exceptionally on exception if action does |
|
5047 */ |
|
5048 public void testDefaultExceptionallyComposeAsync_actionFailed() { |
|
5049 for (boolean createIncomplete : new boolean[] { true, false }) |
|
5050 { |
|
5051 final CFException ex = new CFException(); |
|
5052 final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
5053 final FailingExceptionalCompletableFutureFunction r |
|
5054 = new FailingExceptionalCompletableFutureFunction(ExecutionMode.ASYNC); |
|
5055 final DelegatedCompletionStage<Integer> d = |
|
5056 new DelegatedCompletionStage<Integer>(f); |
|
5057 if (!createIncomplete) f.completeExceptionally(ex); |
|
5058 final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r); |
|
5059 if (createIncomplete) f.completeExceptionally(ex); |
|
5060 |
|
5061 checkCompletedExceptionally(f, ex); |
|
5062 checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex); |
|
5063 r.assertInvoked(); |
|
5064 }} |
|
5065 |
|
5066 /** |
|
5067 * default-implemented exceptionallyComposeAsync result completes |
|
5068 * normally after normal completion of source |
|
5069 */ |
|
5070 public void testDefaultExceptionallyComposeAsyncExecutor_normalCompletion() { |
|
5071 for (boolean createIncomplete : new boolean[] { true, false }) |
|
5072 for (Integer v1 : new Integer[] { 1, null }) |
|
5073 { |
|
5074 final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
5075 final ExceptionalCompletableFutureFunction r = |
|
5076 new ExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR); |
|
5077 final DelegatedCompletionStage<Integer> d = |
|
5078 new DelegatedCompletionStage<Integer>(f); |
|
5079 if (!createIncomplete) assertTrue(f.complete(v1)); |
|
5080 final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r, new ThreadExecutor()); |
|
5081 if (createIncomplete) assertTrue(f.complete(v1)); |
|
5082 |
|
5083 checkCompletedNormally(f, v1); |
|
5084 checkCompletedNormally(g.toCompletableFuture(), v1); |
|
5085 r.assertNotInvoked(); |
|
5086 }} |
|
5087 |
|
5088 /** |
|
5089 * default-implemented exceptionallyComposeAsync result completes |
|
5090 * normally after exceptional completion of source |
|
5091 */ |
|
5092 public void testDefaultExceptionallyComposeAsyncExecutor_exceptionalCompletion() { |
|
5093 for (boolean createIncomplete : new boolean[] { true, false }) |
|
5094 { |
|
5095 final CFException ex = new CFException(); |
|
5096 final ExceptionalCompletableFutureFunction r = |
|
5097 new ExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR); |
|
5098 final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
5099 final DelegatedCompletionStage<Integer> d = |
|
5100 new DelegatedCompletionStage<Integer>(f); |
|
5101 if (!createIncomplete) f.completeExceptionally(ex); |
|
5102 final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r, new ThreadExecutor()); |
|
5103 if (createIncomplete) f.completeExceptionally(ex); |
|
5104 |
|
5105 checkCompletedExceptionally(f, ex); |
|
5106 checkCompletedNormally(g.toCompletableFuture(), r.value); |
|
5107 r.assertInvoked(); |
|
5108 }} |
|
5109 |
|
5110 /** |
|
5111 * default-implemented exceptionallyComposeAsync completes |
|
5112 * exceptionally on exception if action does |
|
5113 */ |
|
5114 public void testDefaultExceptionallyComposeAsyncExecutor_actionFailed() { |
|
5115 for (boolean createIncomplete : new boolean[] { true, false }) |
|
5116 { |
|
5117 final CFException ex = new CFException(); |
|
5118 final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
5119 final FailingExceptionalCompletableFutureFunction r |
|
5120 = new FailingExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR); |
|
5121 final DelegatedCompletionStage<Integer> d = |
|
5122 new DelegatedCompletionStage<Integer>(f); |
|
5123 if (!createIncomplete) f.completeExceptionally(ex); |
|
5124 final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r, new ThreadExecutor()); |
|
5125 if (createIncomplete) f.completeExceptionally(ex); |
|
5126 |
|
5127 checkCompletedExceptionally(f, ex); |
|
5128 checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex); |
|
5129 r.assertInvoked(); |
|
5130 }} |
|
5131 |
4530 } |
5132 } |