ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
(Generate patch)

Comparing jsr166/src/test/tck/CompletableFutureTest.java (file contents):
Revision 1.198 by dl, Mon Sep 17 11:50:55 2018 UTC vs.
Revision 1.199 by dl, Thu Sep 20 00:01:22 2018 UTC

# Line 4633 | Line 4633 | public class CompletableFutureTest exten
4633   //         return stage.toCompletableFuture().copy().isDone();
4634   //     }
4635  
4636 +    // For testing default implementations
4637 +    // Only non-default interface methods defined.
4638 +    static final class DelegatedCompletionStage<T> implements CompletionStage<T> {
4639 +        final CompletableFuture<T> cf;
4640 +        DelegatedCompletionStage(CompletableFuture<T> cf) { this.cf = cf; }
4641 +        public CompletableFuture<T> toCompletableFuture() {
4642 +            return cf; }
4643 +        public CompletionStage<Void> thenRun
4644 +            (Runnable action) {
4645 +            return cf.thenRun(action); }
4646 +        public CompletionStage<Void> thenRunAsync
4647 +            (Runnable action) {
4648 +            return cf.thenRunAsync(action); }
4649 +        public CompletionStage<Void> thenRunAsync
4650 +            (Runnable action,
4651 +             Executor executor) {
4652 +            return cf.thenRunAsync(action, executor); }
4653 +        public CompletionStage<Void> thenAccept
4654 +            (Consumer<? super T> action) {
4655 +            return cf.thenAccept(action); }
4656 +        public CompletionStage<Void> thenAcceptAsync
4657 +            (Consumer<? super T> action) {
4658 +            return cf.thenAcceptAsync(action); }
4659 +        public CompletionStage<Void> thenAcceptAsync
4660 +            (Consumer<? super T> action,
4661 +             Executor executor) {
4662 +            return cf.thenAcceptAsync(action, executor); }
4663 +        public <U> CompletionStage<U> thenApply
4664 +            (Function<? super T,? extends U> a) {
4665 +            return cf.thenApply(a); }
4666 +        public <U> CompletionStage<U> thenApplyAsync
4667 +            (Function<? super T,? extends U> fn) {
4668 +            return cf.thenApplyAsync(fn); }
4669 +        public <U> CompletionStage<U> thenApplyAsync
4670 +            (Function<? super T,? extends U> fn,
4671 +             Executor executor) {
4672 +            return cf.thenApplyAsync(fn, executor); }
4673 +        public <U,V> CompletionStage<V> thenCombine
4674 +            (CompletionStage<? extends U> other,
4675 +             BiFunction<? super T,? super U,? extends V> fn) {
4676 +            return cf.thenCombine(other, fn); }
4677 +        public <U,V> CompletionStage<V> thenCombineAsync
4678 +            (CompletionStage<? extends U> other,
4679 +             BiFunction<? super T,? super U,? extends V> fn) {
4680 +            return cf.thenCombineAsync(other, fn); }
4681 +        public <U,V> CompletionStage<V> thenCombineAsync
4682 +            (CompletionStage<? extends U> other,
4683 +             BiFunction<? super T,? super U,? extends V> fn,
4684 +             Executor executor) {
4685 +            return cf.thenCombineAsync(other, fn, executor); }
4686 +        public <U> CompletionStage<Void> thenAcceptBoth
4687 +            (CompletionStage<? extends U> other,
4688 +             BiConsumer<? super T, ? super U> action) {
4689 +            return cf.thenAcceptBoth(other, action); }
4690 +        public <U> CompletionStage<Void> thenAcceptBothAsync
4691 +            (CompletionStage<? extends U> other,
4692 +             BiConsumer<? super T, ? super U> action) {
4693 +            return cf.thenAcceptBothAsync(other, action); }
4694 +        public <U> CompletionStage<Void> thenAcceptBothAsync
4695 +            (CompletionStage<? extends U> other,
4696 +             BiConsumer<? super T, ? super U> action,
4697 +             Executor executor) {
4698 +            return cf.thenAcceptBothAsync(other, action, executor); }
4699 +        public CompletionStage<Void> runAfterBoth
4700 +            (CompletionStage<?> other,
4701 +             Runnable action) {
4702 +            return cf.runAfterBoth(other, action); }
4703 +        public CompletionStage<Void> runAfterBothAsync
4704 +            (CompletionStage<?> other,
4705 +             Runnable action) {
4706 +            return cf.runAfterBothAsync(other, action); }
4707 +        public CompletionStage<Void> runAfterBothAsync
4708 +            (CompletionStage<?> other,
4709 +             Runnable action,
4710 +             Executor executor) {
4711 +            return cf.runAfterBothAsync(other, action, executor); }
4712 +        public <U> CompletionStage<U> applyToEither
4713 +            (CompletionStage<? extends T> other,
4714 +             Function<? super T, U> fn) {
4715 +            return cf.applyToEither(other, fn); }
4716 +        public <U> CompletionStage<U> applyToEitherAsync
4717 +            (CompletionStage<? extends T> other,
4718 +             Function<? super T, U> fn) {
4719 +            return cf.applyToEitherAsync(other, fn); }
4720 +        public <U> CompletionStage<U> applyToEitherAsync
4721 +            (CompletionStage<? extends T> other,
4722 +             Function<? super T, U> fn,
4723 +             Executor executor) {
4724 +            return cf.applyToEitherAsync(other, fn, executor); }
4725 +        public CompletionStage<Void> acceptEither
4726 +            (CompletionStage<? extends T> other,
4727 +             Consumer<? super T> action) {
4728 +            return cf.acceptEither(other, action); }
4729 +        public CompletionStage<Void> acceptEitherAsync
4730 +            (CompletionStage<? extends T> other,
4731 +             Consumer<? super T> action) {
4732 +            return cf.acceptEitherAsync(other, action); }
4733 +        public CompletionStage<Void> acceptEitherAsync
4734 +            (CompletionStage<? extends T> other,
4735 +             Consumer<? super T> action,
4736 +             Executor executor) {
4737 +            return cf.acceptEitherAsync(other, action, executor); }
4738 +        public CompletionStage<Void> runAfterEither
4739 +            (CompletionStage<?> other,
4740 +             Runnable action) {
4741 +            return cf.runAfterEither(other, action); }
4742 +        public CompletionStage<Void> runAfterEitherAsync
4743 +            (CompletionStage<?> other,
4744 +             Runnable action) {
4745 +            return cf.runAfterEitherAsync(other, action); }
4746 +        public CompletionStage<Void> runAfterEitherAsync
4747 +            (CompletionStage<?> other,
4748 +             Runnable action,
4749 +             Executor executor) {
4750 +            return cf.runAfterEitherAsync(other, action, executor); }
4751 +        public <U> CompletionStage<U> thenCompose
4752 +            (Function<? super T, ? extends CompletionStage<U>> fn) {
4753 +            return cf.thenCompose(fn); }
4754 +        public <U> CompletionStage<U> thenComposeAsync
4755 +            (Function<? super T, ? extends CompletionStage<U>> fn) {
4756 +            return cf.thenComposeAsync(fn); }
4757 +        public <U> CompletionStage<U> thenComposeAsync
4758 +            (Function<? super T, ? extends CompletionStage<U>> fn,
4759 +             Executor executor) {
4760 +            return cf.thenComposeAsync(fn, executor); }
4761 +        public <U> CompletionStage<U> handle
4762 +            (BiFunction<? super T, Throwable, ? extends U> fn) {
4763 +            return cf.handle(fn); }
4764 +        public <U> CompletionStage<U> handleAsync
4765 +            (BiFunction<? super T, Throwable, ? extends U> fn) {
4766 +            return cf.handleAsync(fn); }
4767 +        public <U> CompletionStage<U> handleAsync
4768 +            (BiFunction<? super T, Throwable, ? extends U> fn,
4769 +             Executor executor) {
4770 +            return cf.handleAsync(fn, executor); }
4771 +        public CompletionStage<T> whenComplete
4772 +            (BiConsumer<? super T, ? super Throwable> action) {
4773 +            return cf.whenComplete(action); }
4774 +        public CompletionStage<T> whenCompleteAsync
4775 +            (BiConsumer<? super T, ? super Throwable> action) {
4776 +            return cf.whenCompleteAsync(action); }
4777 +        public CompletionStage<T> whenCompleteAsync
4778 +            (BiConsumer<? super T, ? super Throwable> action,
4779 +             Executor executor) {
4780 +            return cf.whenCompleteAsync(action, executor); }
4781 +        public CompletionStage<T> exceptionally
4782 +            (Function<Throwable, ? extends T> fn) {
4783 +            return cf.exceptionally(fn); }
4784 +    }
4785 +
4786 +    /**
4787 +     * default-implemented exceptionallyAsync action completes with
4788 +     * function value on source exception
4789 +     */
4790 +    public void testDefaulExceptionallyAsync_exceptionalCompletion() {
4791 +        for (boolean createIncomplete : new boolean[] { true, false })
4792 +        for (Integer v1 : new Integer[] { 1, null })
4793 +    {
4794 +        final AtomicInteger a = new AtomicInteger(0);
4795 +        final CFException ex = new CFException();
4796 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4797 +        final DelegatedCompletionStage<Integer> d =
4798 +            new DelegatedCompletionStage<Integer>(f);
4799 +        if (!createIncomplete) f.completeExceptionally(ex);
4800 +        final CompletionStage<Integer> g = d.exceptionallyAsync
4801 +            ((Throwable t) -> {
4802 +                threadAssertSame(t, ex);
4803 +                a.getAndIncrement();
4804 +                return v1;
4805 +            });
4806 +        if (createIncomplete) f.completeExceptionally(ex);
4807 +
4808 +        checkCompletedNormally(g.toCompletableFuture(), v1);
4809 +        assertEquals(1, a.get());
4810 +    }}
4811 +
4812 +    /**
4813 +     * Under default implementation, if an "exceptionally action"
4814 +     * throws an exception, it completes exceptionally with that
4815 +     * exception
4816 +     */
4817 +    public void testDefaulExceptionallyAsync_exceptionalCompletionActionFailed() {
4818 +        for (boolean createIncomplete : new boolean[] { true, false })
4819 +    {
4820 +        final AtomicInteger a = new AtomicInteger(0);
4821 +        final CFException ex1 = new CFException();
4822 +        final CFException ex2 = new CFException();
4823 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4824 +        final DelegatedCompletionStage<Integer> d =
4825 +            new DelegatedCompletionStage<Integer>(f);
4826 +        if (!createIncomplete) f.completeExceptionally(ex1);
4827 +        final CompletionStage<Integer> g = d.exceptionallyAsync
4828 +            ((Throwable t) -> {
4829 +                threadAssertSame(t, ex1);
4830 +                a.getAndIncrement();
4831 +                throw ex2;
4832 +            });
4833 +        if (createIncomplete) f.completeExceptionally(ex1);
4834 +
4835 +        checkCompletedWithWrappedException(g.toCompletableFuture(), ex2);
4836 +        checkCompletedExceptionally(f, ex1);
4837 +        checkCompletedExceptionally(d.toCompletableFuture(), ex1);
4838 +        assertEquals(1, a.get());
4839 +    }}
4840 +
4841 +    /**
4842 +     * defult exceptionallyCompose result completes normally after normal
4843 +     * completion of source
4844 +     */
4845 +    public void testDefaultExceptionallyCompose_normalCompletion() {
4846 +        for (boolean createIncomplete : new boolean[] { true, false })
4847 +        for (Integer v1 : new Integer[] { 1, null })
4848 +    {
4849 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4850 +        final ExceptionalCompletableFutureFunction r =
4851 +            new ExceptionalCompletableFutureFunction(ExecutionMode.SYNC);
4852 +        final DelegatedCompletionStage<Integer> d =
4853 +            new DelegatedCompletionStage<Integer>(f);
4854 +        if (!createIncomplete) assertTrue(f.complete(v1));
4855 +        final CompletionStage<Integer> g = d.exceptionallyCompose(r);
4856 +        if (createIncomplete) assertTrue(f.complete(v1));
4857 +
4858 +        checkCompletedNormally(f, v1);
4859 +        checkCompletedNormally(g.toCompletableFuture(), v1);
4860 +        r.assertNotInvoked();
4861 +    }}
4862 +
4863 +    /**
4864 +     * default-implemented exceptionallyCompose result completes
4865 +     * normally after exceptional completion of source
4866 +     */
4867 +    public void testDefaultExceptionallyCompose_exceptionalCompletion() {
4868 +        for (boolean createIncomplete : new boolean[] { true, false })
4869 +    {
4870 +        final CFException ex = new CFException();
4871 +        final ExceptionalCompletableFutureFunction r =
4872 +            new ExceptionalCompletableFutureFunction(ExecutionMode.SYNC);
4873 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4874 +        final DelegatedCompletionStage<Integer> d =
4875 +            new DelegatedCompletionStage<Integer>(f);
4876 +        if (!createIncomplete) f.completeExceptionally(ex);
4877 +        final CompletionStage<Integer> g = d.exceptionallyCompose(r);
4878 +        if (createIncomplete) f.completeExceptionally(ex);
4879 +
4880 +        checkCompletedExceptionally(f, ex);
4881 +        checkCompletedNormally(g.toCompletableFuture(), r.value);
4882 +        r.assertInvoked();
4883 +    }}
4884 +
4885 +    /**
4886 +     * default-implemented exceptionallyCompose completes
4887 +     * exceptionally on exception if action does
4888 +     */
4889 +    public void testDefaultExceptionallyCompose_actionFailed() {
4890 +        for (boolean createIncomplete : new boolean[] { true, false })
4891 +        for (Integer v1 : new Integer[] { 1, null })
4892 +    {
4893 +        final CFException ex = new CFException();
4894 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4895 +        final FailingExceptionalCompletableFutureFunction r
4896 +            = new FailingExceptionalCompletableFutureFunction(ExecutionMode.SYNC);
4897 +        final DelegatedCompletionStage<Integer> d =
4898 +            new DelegatedCompletionStage<Integer>(f);
4899 +        if (!createIncomplete) f.completeExceptionally(ex);
4900 +        final CompletionStage<Integer> g = d.exceptionallyCompose(r);
4901 +        if (createIncomplete) f.completeExceptionally(ex);
4902 +
4903 +        checkCompletedExceptionally(f, ex);
4904 +        checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex);
4905 +        r.assertInvoked();
4906 +    }}
4907 +
4908 +    /**
4909 +     * defult exceptionallyComposeAsync result completes normally after normal
4910 +     * completion of source
4911 +     */
4912 +    public void testDefaultExceptionallyComposeAsync_normalCompletion() {
4913 +        for (boolean createIncomplete : new boolean[] { true, false })
4914 +        for (Integer v1 : new Integer[] { 1, null })
4915 +    {
4916 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4917 +        final ExceptionalCompletableFutureFunction r =
4918 +            new ExceptionalCompletableFutureFunction(ExecutionMode.ASYNC);
4919 +        final DelegatedCompletionStage<Integer> d =
4920 +            new DelegatedCompletionStage<Integer>(f);
4921 +        if (!createIncomplete) assertTrue(f.complete(v1));
4922 +        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r);
4923 +        if (createIncomplete) assertTrue(f.complete(v1));
4924 +
4925 +        checkCompletedNormally(f, v1);
4926 +        checkCompletedNormally(g.toCompletableFuture(), v1);
4927 +        r.assertNotInvoked();
4928 +    }}
4929 +
4930 +    /**
4931 +     * default-implemented exceptionallyComposeAsync result completes
4932 +     * normally after exceptional completion of source
4933 +     */
4934 +    public void testDefaultExceptionallyComposeAsync_exceptionalCompletion() {
4935 +        for (boolean createIncomplete : new boolean[] { true, false })
4936 +    {
4937 +        final CFException ex = new CFException();
4938 +        final ExceptionalCompletableFutureFunction r =
4939 +            new ExceptionalCompletableFutureFunction(ExecutionMode.ASYNC);
4940 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4941 +        final DelegatedCompletionStage<Integer> d =
4942 +            new DelegatedCompletionStage<Integer>(f);
4943 +        if (!createIncomplete) f.completeExceptionally(ex);
4944 +        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r);
4945 +        if (createIncomplete) f.completeExceptionally(ex);
4946 +
4947 +        checkCompletedExceptionally(f, ex);
4948 +        checkCompletedNormally(g.toCompletableFuture(), r.value);
4949 +        r.assertInvoked();
4950 +    }}
4951 +
4952 +    /**
4953 +     * default-implemented exceptionallyComposeAsync completes
4954 +     * exceptionally on exception if action does
4955 +     */
4956 +    public void testDefaultExceptionallyComposeAsync_actionFailed() {
4957 +        for (boolean createIncomplete : new boolean[] { true, false })
4958 +        for (Integer v1 : new Integer[] { 1, null })
4959 +    {
4960 +        final CFException ex = new CFException();
4961 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4962 +        final FailingExceptionalCompletableFutureFunction r
4963 +            = new FailingExceptionalCompletableFutureFunction(ExecutionMode.ASYNC);
4964 +        final DelegatedCompletionStage<Integer> d =
4965 +            new DelegatedCompletionStage<Integer>(f);
4966 +        if (!createIncomplete) f.completeExceptionally(ex);
4967 +        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r);
4968 +        if (createIncomplete) f.completeExceptionally(ex);
4969 +
4970 +        checkCompletedExceptionally(f, ex);
4971 +        checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex);
4972 +        r.assertInvoked();
4973 +    }}
4974 +
4975 +
4976 +    /**
4977 +     * defult exceptionallyComposeAsync result completes normally after normal
4978 +     * completion of source
4979 +     */
4980 +    public void testDefaultExceptionallyComposeAsyncExecutor_normalCompletion() {
4981 +        for (boolean createIncomplete : new boolean[] { true, false })
4982 +        for (Integer v1 : new Integer[] { 1, null })
4983 +    {
4984 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4985 +        final ExceptionalCompletableFutureFunction r =
4986 +            new ExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR);
4987 +        final DelegatedCompletionStage<Integer> d =
4988 +            new DelegatedCompletionStage<Integer>(f);
4989 +        if (!createIncomplete) assertTrue(f.complete(v1));
4990 +        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r,  new ThreadExecutor());
4991 +        if (createIncomplete) assertTrue(f.complete(v1));
4992 +
4993 +        checkCompletedNormally(f, v1);
4994 +        checkCompletedNormally(g.toCompletableFuture(), v1);
4995 +        r.assertNotInvoked();
4996 +    }}
4997 +
4998 +    /**
4999 +     * default-implemented exceptionallyComposeAsync result completes
5000 +     * normally after exceptional completion of source
5001 +     */
5002 +    public void testDefaultExceptionallyComposeAsyncExecutor_exceptionalCompletion() {
5003 +        for (boolean createIncomplete : new boolean[] { true, false })
5004 +    {
5005 +        final CFException ex = new CFException();
5006 +        final ExceptionalCompletableFutureFunction r =
5007 +            new ExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR);
5008 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
5009 +        final DelegatedCompletionStage<Integer> d =
5010 +            new DelegatedCompletionStage<Integer>(f);
5011 +        if (!createIncomplete) f.completeExceptionally(ex);
5012 +        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r,  new ThreadExecutor());
5013 +        if (createIncomplete) f.completeExceptionally(ex);
5014 +
5015 +        checkCompletedExceptionally(f, ex);
5016 +        checkCompletedNormally(g.toCompletableFuture(), r.value);
5017 +        r.assertInvoked();
5018 +    }}
5019 +
5020 +    /**
5021 +     * default-implemented exceptionallyComposeAsync completes
5022 +     * exceptionally on exception if action does
5023 +     */
5024 +    public void testDefaultExceptionallyComposeAsyncExecutor_actionFailed() {
5025 +        for (boolean createIncomplete : new boolean[] { true, false })
5026 +        for (Integer v1 : new Integer[] { 1, null })
5027 +    {
5028 +        final CFException ex = new CFException();
5029 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
5030 +        final FailingExceptionalCompletableFutureFunction r
5031 +            = new FailingExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR);
5032 +        final DelegatedCompletionStage<Integer> d =
5033 +            new DelegatedCompletionStage<Integer>(f);
5034 +        if (!createIncomplete) f.completeExceptionally(ex);
5035 +        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r,  new ThreadExecutor());
5036 +        if (createIncomplete) f.completeExceptionally(ex);
5037 +
5038 +        checkCompletedExceptionally(f, ex);
5039 +        checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex);
5040 +        r.assertInvoked();
5041 +    }}
5042 +
5043   }
5044 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines