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.206 by jsr166, Sun Sep 23 02:04:03 2018 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines