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.190 by jsr166, Wed Nov 8 02:21:43 2017 UTC vs.
Revision 1.204 by jsr166, Sun Sep 23 00:52:18 2018 UTC

# Line 41 | Line 41 | import java.util.function.Function;
41   import java.util.function.Predicate;
42   import java.util.function.Supplier;
43  
44 import junit.framework.AssertionFailedError;
44   import junit.framework.Test;
45   import junit.framework.TestSuite;
46  
# Line 60 | Line 59 | public class CompletableFutureTest exten
59          assertFalse(f.isDone());
60          assertFalse(f.isCancelled());
61          assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]"));
62 +
63 +        Object result = null;
64          try {
65 <            assertNull(f.getNow(null));
65 >            result = f.getNow(null);
66          } catch (Throwable fail) { threadUnexpectedException(fail); }
67 +        assertNull(result);
68 +
69          try {
70              f.get(randomExpiredTimeout(), randomTimeUnit());
71              shouldThrow();
# Line 71 | Line 74 | public class CompletableFutureTest exten
74          catch (Throwable fail) { threadUnexpectedException(fail); }
75      }
76  
77 <    <T> void checkCompletedNormally(CompletableFuture<T> f, T value) {
78 <        checkTimedGet(f, value);
77 >    <T> void checkCompletedNormally(CompletableFuture<T> f, T expectedValue) {
78 >        checkTimedGet(f, expectedValue);
79  
80 +        assertEquals(expectedValue, f.join());
81 +        assertEquals(expectedValue, f.getNow(null));
82 +
83 +        T result = null;
84          try {
85 <            assertEquals(value, f.join());
79 <            assertEquals(value, f.getNow(null));
80 <            assertEquals(value, f.get());
85 >            result = f.get();
86          } catch (Throwable fail) { threadUnexpectedException(fail); }
87 +        assertEquals(expectedValue, result);
88 +
89          assertTrue(f.isDone());
90          assertFalse(f.isCancelled());
91          assertFalse(f.isCompletedExceptionally());
# Line 292 | Line 299 | public class CompletableFutureTest exten
299          }
300  
301          f = new CompletableFuture<>();
302 <        f.completeExceptionally(ex = new CFException());
302 >        f.completeExceptionally(new CFException());
303          f.obtrudeValue(v1);
304          checkCompletedNormally(f, v1);
305          f.obtrudeException(ex = new CFException());
# Line 330 | Line 337 | public class CompletableFutureTest exten
337       * toString indicates current completion state
338       */
339      public void testToString_incomplete() {
340 <        CompletableFuture<String> f = new CompletableFuture<String>();
340 >        CompletableFuture<String> f = new CompletableFuture<>();
341          assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]"));
342          if (testImplementationDetails)
343              assertEquals(identityString(f) + "[Not completed]",
# Line 338 | Line 345 | public class CompletableFutureTest exten
345      }
346  
347      public void testToString_normal() {
348 <        CompletableFuture<String> f = new CompletableFuture<String>();
348 >        CompletableFuture<String> f = new CompletableFuture<>();
349          assertTrue(f.complete("foo"));
350          assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]"));
351          if (testImplementationDetails)
# Line 347 | Line 354 | public class CompletableFutureTest exten
354      }
355  
356      public void testToString_exception() {
357 <        CompletableFuture<String> f = new CompletableFuture<String>();
357 >        CompletableFuture<String> f = new CompletableFuture<>();
358          assertTrue(f.completeExceptionally(new IndexOutOfBoundsException()));
359          assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
360          if (testImplementationDetails)
# Line 357 | Line 364 | public class CompletableFutureTest exten
364  
365      public void testToString_cancelled() {
366          for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
367 <            CompletableFuture<String> f = new CompletableFuture<String>();
367 >            CompletableFuture<String> f = new CompletableFuture<>();
368              assertTrue(f.cancel(mayInterruptIfRunning));
369              assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
370              if (testImplementationDetails)
# Line 544 | 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 >
558 >    static class FailingExceptionalCompletableFutureFunction extends CheckedAction
559 >        implements Function<Throwable, CompletableFuture<Integer>>
560 >    {
561 >        final CFException ex;
562 >        FailingExceptionalCompletableFutureFunction(ExecutionMode m) { super(m); ex = new CFException(); }
563 >        public CompletableFuture<Integer> apply(Throwable x) {
564 >            invoked();
565 >            throw ex;
566 >        }
567 >    }
568 >
569 >    static class ExceptionalCompletableFutureFunction extends CheckedAction
570 >        implements Function<Throwable, CompletionStage<Integer>> {
571 >        final Integer value = 3;
572 >        ExceptionalCompletableFutureFunction(ExecutionMode m) { super(m); }
573 >        public CompletionStage<Integer> apply(Throwable x) {
574 >            invoked();
575 >            return CompletableFuture.completedFuture(value);
576          }
577      }
578  
# Line 665 | Line 691 | public class CompletableFutureTest exten
691                   Function<? super T,U> a) {
692                  return f.applyToEither(g, a);
693              }
694 +            public <T> CompletableFuture<T> exceptionally
695 +                (CompletableFuture<T> f,
696 +                 Function<Throwable, ? extends T> fn) {
697 +                return f.exceptionally(fn);
698 +            }
699 +            public <T> CompletableFuture<T> exceptionallyCompose
700 +                (CompletableFuture<T> f, Function<Throwable, ? extends CompletionStage<T>> fn) {
701 +                return f.exceptionallyCompose(fn);
702 +            }
703          },
669
704          ASYNC {
705              public void checkExecutionMode() {
706                  assertEquals(defaultExecutorIsCommonPool,
# Line 739 | Line 773 | public class CompletableFutureTest exten
773                   Function<? super T,U> a) {
774                  return f.applyToEitherAsync(g, a);
775              }
776 +            public <T> CompletableFuture<T> exceptionally
777 +                (CompletableFuture<T> f,
778 +                 Function<Throwable, ? extends T> fn) {
779 +                return f.exceptionallyAsync(fn);
780 +            }
781 +
782 +            public <T> CompletableFuture<T> exceptionallyCompose
783 +                (CompletableFuture<T> f, Function<Throwable, ? extends CompletionStage<T>> fn) {
784 +                return f.exceptionallyComposeAsync(fn);
785 +            }
786 +
787          },
788  
789          EXECUTOR {
# Line 812 | Line 857 | public class CompletableFutureTest exten
857                   Function<? super T,U> a) {
858                  return f.applyToEitherAsync(g, a, new ThreadExecutor());
859              }
860 +            public <T> CompletableFuture<T> exceptionally
861 +                (CompletableFuture<T> f,
862 +                 Function<Throwable, ? extends T> fn) {
863 +                return f.exceptionallyAsync(fn, new ThreadExecutor());
864 +            }
865 +            public <T> CompletableFuture<T> exceptionallyCompose
866 +                (CompletableFuture<T> f, Function<Throwable, ? extends CompletionStage<T>> fn) {
867 +                return f.exceptionallyComposeAsync(fn, new ThreadExecutor());
868 +            }
869 +
870          };
871  
872          public abstract void checkExecutionMode();
# Line 854 | Line 909 | public class CompletableFutureTest exten
909              (CompletableFuture<T> f,
910               CompletionStage<? extends T> g,
911               Function<? super T,U> a);
912 +        public abstract <T> CompletableFuture<T> exceptionally
913 +            (CompletableFuture<T> f,
914 +             Function<Throwable, ? extends T> fn);
915 +        public abstract <T> CompletableFuture<T> exceptionallyCompose
916 +            (CompletableFuture<T> f,
917 +             Function<Throwable, ? extends CompletionStage<T>> fn);
918      }
919  
920      /**
# Line 861 | Line 922 | public class CompletableFutureTest exten
922       * normally, and source result is propagated
923       */
924      public void testExceptionally_normalCompletion() {
925 +        for (ExecutionMode m : ExecutionMode.values())
926          for (boolean createIncomplete : new boolean[] { true, false })
927          for (Integer v1 : new Integer[] { 1, null })
928      {
929          final AtomicInteger a = new AtomicInteger(0);
930          final CompletableFuture<Integer> f = new CompletableFuture<>();
931          if (!createIncomplete) assertTrue(f.complete(v1));
932 <        final CompletableFuture<Integer> g = f.exceptionally
933 <            ((Throwable t) -> {
932 >        final CompletableFuture<Integer> g = m.exceptionally
933 >            (f, (Throwable t) -> {
934                  a.getAndIncrement();
935                  threadFail("should not be called");
936                  return null;            // unreached
# Line 885 | Line 947 | public class CompletableFutureTest exten
947       * exception
948       */
949      public void testExceptionally_exceptionalCompletion() {
950 +        for (ExecutionMode m : ExecutionMode.values())
951          for (boolean createIncomplete : new boolean[] { true, false })
952          for (Integer v1 : new Integer[] { 1, null })
953      {
# Line 892 | Line 955 | public class CompletableFutureTest exten
955          final CFException ex = new CFException();
956          final CompletableFuture<Integer> f = new CompletableFuture<>();
957          if (!createIncomplete) f.completeExceptionally(ex);
958 <        final CompletableFuture<Integer> g = f.exceptionally
959 <            ((Throwable t) -> {
960 <                ExecutionMode.SYNC.checkExecutionMode();
958 >        final CompletableFuture<Integer> g = m.exceptionally
959 >            (f, (Throwable t) -> {
960 >                m.checkExecutionMode();
961                  threadAssertSame(t, ex);
962                  a.getAndIncrement();
963                  return v1;
# Line 910 | Line 973 | public class CompletableFutureTest exten
973       * exceptionally with that exception
974       */
975      public void testExceptionally_exceptionalCompletionActionFailed() {
976 +        for (ExecutionMode m : ExecutionMode.values())
977          for (boolean createIncomplete : new boolean[] { true, false })
978      {
979          final AtomicInteger a = new AtomicInteger(0);
# Line 917 | Line 981 | public class CompletableFutureTest exten
981          final CFException ex2 = new CFException();
982          final CompletableFuture<Integer> f = new CompletableFuture<>();
983          if (!createIncomplete) f.completeExceptionally(ex1);
984 <        final CompletableFuture<Integer> g = f.exceptionally
985 <            ((Throwable t) -> {
986 <                ExecutionMode.SYNC.checkExecutionMode();
984 >        final CompletableFuture<Integer> g = m.exceptionally
985 >            (f, (Throwable t) -> {
986 >                m.checkExecutionMode();
987                  threadAssertSame(t, ex1);
988                  a.getAndIncrement();
989                  throw ex2;
# Line 3090 | Line 3154 | public class CompletableFutureTest exten
3154          checkCompletedNormally(f, v1);
3155      }}
3156  
3157 +    /**
3158 +     * exceptionallyCompose result completes normally after normal
3159 +     * completion of source
3160 +     */
3161 +    public void testExceptionallyCompose_normalCompletion() {
3162 +        for (ExecutionMode m : ExecutionMode.values())
3163 +        for (boolean createIncomplete : new boolean[] { true, false })
3164 +        for (Integer v1 : new Integer[] { 1, null })
3165 +    {
3166 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3167 +        final ExceptionalCompletableFutureFunction r =
3168 +            new ExceptionalCompletableFutureFunction(m);
3169 +        if (!createIncomplete) assertTrue(f.complete(v1));
3170 +        final CompletableFuture<Integer> g = m.exceptionallyCompose(f, r);
3171 +        if (createIncomplete) assertTrue(f.complete(v1));
3172 +
3173 +        checkCompletedNormally(f, v1);
3174 +        checkCompletedNormally(g, v1);
3175 +        r.assertNotInvoked();
3176 +    }}
3177 +
3178 +    /**
3179 +     * exceptionallyCompose result completes normally after exceptional
3180 +     * completion of source
3181 +     */
3182 +    public void testExceptionallyCompose_exceptionalCompletion() {
3183 +        for (ExecutionMode m : ExecutionMode.values())
3184 +        for (boolean createIncomplete : new boolean[] { true, false })
3185 +    {
3186 +        final CFException ex = new CFException();
3187 +        final ExceptionalCompletableFutureFunction r =
3188 +            new ExceptionalCompletableFutureFunction(m);
3189 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3190 +        if (!createIncomplete) f.completeExceptionally(ex);
3191 +        final CompletableFuture<Integer> g = m.exceptionallyCompose(f, r);
3192 +        if (createIncomplete) f.completeExceptionally(ex);
3193 +
3194 +        checkCompletedExceptionally(f, ex);
3195 +        checkCompletedNormally(g, r.value);
3196 +        r.assertInvoked();
3197 +    }}
3198 +
3199 +    /**
3200 +     * exceptionallyCompose completes exceptionally on exception if action does
3201 +     */
3202 +    public void testExceptionallyCompose_actionFailed() {
3203 +        for (ExecutionMode m : ExecutionMode.values())
3204 +        for (boolean createIncomplete : new boolean[] { true, false })
3205 +        for (Integer v1 : new Integer[] { 1, null })
3206 +    {
3207 +        final CFException ex = new CFException();
3208 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3209 +        final FailingExceptionalCompletableFutureFunction r
3210 +            = new FailingExceptionalCompletableFutureFunction(m);
3211 +        if (!createIncomplete) f.completeExceptionally(ex);
3212 +        final CompletableFuture<Integer> g = m.exceptionallyCompose(f, r);
3213 +        if (createIncomplete) f.completeExceptionally(ex);
3214 +
3215 +        checkCompletedExceptionally(f, ex);
3216 +        checkCompletedWithWrappedException(g, r.ex);
3217 +        r.assertInvoked();
3218 +    }}
3219 +
3220 +
3221      // other static methods
3222  
3223      /**
# Line 4190 | Line 4318 | public class CompletableFutureTest exten
4318          static void assertZero(CompletableFuture<?> f) {
4319              try {
4320                  f.getNow(null);
4321 <                throw new AssertionFailedError("should throw");
4321 >                throw new AssertionError("should throw");
4322              } catch (CompletionException success) {
4323                  assertTrue(success.getCause() instanceof ZeroException);
4324              }
# Line 4389 | Line 4517 | public class CompletableFutureTest exten
4517              f.complete(null);
4518  
4519              f = new CompletableFuture<>();
4520 <            CompletableFuture.anyOf(new CompletableFuture<?>[] { f, incomplete });
4520 >            CompletableFuture.anyOf(f, incomplete);
4521              f.complete(null);
4522          }
4523  
# Line 4407 | Line 4535 | public class CompletableFutureTest exten
4535              f.complete(null);
4536  
4537              f = new CompletableFuture<>();
4538 <            CompletableFuture.anyOf(new CompletableFuture<?>[] { incomplete, f });
4538 >            CompletableFuture.anyOf(incomplete, f);
4539              f.complete(null);
4540          }
4541      }
# Line 4501 | 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 completes with
4784 +     * function value on source exception
4785 +     */
4786 +    public void testDefaultExceptionallyAsync_exceptionalCompletion() {
4787 +        for (boolean createIncomplete : new boolean[] { true, false })
4788 +        for (Integer v1 : new Integer[] { 1, null })
4789 +    {
4790 +        final AtomicInteger a = new AtomicInteger(0);
4791 +        final CFException ex = new CFException();
4792 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4793 +        final DelegatedCompletionStage<Integer> d =
4794 +            new DelegatedCompletionStage<Integer>(f);
4795 +        if (!createIncomplete) f.completeExceptionally(ex);
4796 +        final CompletionStage<Integer> g = d.exceptionallyAsync
4797 +            ((Throwable t) -> {
4798 +                threadAssertSame(t, ex);
4799 +                a.getAndIncrement();
4800 +                return v1;
4801 +            });
4802 +        if (createIncomplete) f.completeExceptionally(ex);
4803 +
4804 +        checkCompletedNormally(g.toCompletableFuture(), v1);
4805 +        assertEquals(1, a.get());
4806 +    }}
4807 +
4808 +    /**
4809 +     * Under default implementation, if an "exceptionally action"
4810 +     * throws an exception, it completes exceptionally with that
4811 +     * exception
4812 +     */
4813 +    public void testDefaultExceptionallyAsync_exceptionalCompletionActionFailed() {
4814 +        for (boolean createIncomplete : new boolean[] { true, false })
4815 +    {
4816 +        final AtomicInteger a = new AtomicInteger(0);
4817 +        final CFException ex1 = new CFException();
4818 +        final CFException ex2 = new CFException();
4819 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4820 +        final DelegatedCompletionStage<Integer> d =
4821 +            new DelegatedCompletionStage<Integer>(f);
4822 +        if (!createIncomplete) f.completeExceptionally(ex1);
4823 +        final CompletionStage<Integer> g = d.exceptionallyAsync
4824 +            ((Throwable t) -> {
4825 +                threadAssertSame(t, ex1);
4826 +                a.getAndIncrement();
4827 +                throw ex2;
4828 +            });
4829 +        if (createIncomplete) f.completeExceptionally(ex1);
4830 +
4831 +        checkCompletedWithWrappedException(g.toCompletableFuture(), ex2);
4832 +        checkCompletedExceptionally(f, ex1);
4833 +        checkCompletedExceptionally(d.toCompletableFuture(), ex1);
4834 +        assertEquals(1, a.get());
4835 +    }}
4836 +
4837 +    /**
4838 +     * default exceptionallyCompose result completes normally after normal
4839 +     * completion of source
4840 +     */
4841 +    public void testDefaultExceptionallyCompose_normalCompletion() {
4842 +        for (boolean createIncomplete : new boolean[] { true, false })
4843 +        for (Integer v1 : new Integer[] { 1, null })
4844 +    {
4845 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4846 +        final ExceptionalCompletableFutureFunction r =
4847 +            new ExceptionalCompletableFutureFunction(ExecutionMode.SYNC);
4848 +        final DelegatedCompletionStage<Integer> d =
4849 +            new DelegatedCompletionStage<Integer>(f);
4850 +        if (!createIncomplete) assertTrue(f.complete(v1));
4851 +        final CompletionStage<Integer> g = d.exceptionallyCompose(r);
4852 +        if (createIncomplete) assertTrue(f.complete(v1));
4853 +
4854 +        checkCompletedNormally(f, v1);
4855 +        checkCompletedNormally(g.toCompletableFuture(), v1);
4856 +        r.assertNotInvoked();
4857 +    }}
4858 +
4859 +    /**
4860 +     * default-implemented exceptionallyCompose result completes
4861 +     * normally after exceptional completion of source
4862 +     */
4863 +    public void testDefaultExceptionallyCompose_exceptionalCompletion() {
4864 +        for (boolean createIncomplete : new boolean[] { true, false })
4865 +    {
4866 +        final CFException ex = new CFException();
4867 +        final ExceptionalCompletableFutureFunction r =
4868 +            new ExceptionalCompletableFutureFunction(ExecutionMode.SYNC);
4869 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4870 +        final DelegatedCompletionStage<Integer> d =
4871 +            new DelegatedCompletionStage<Integer>(f);
4872 +        if (!createIncomplete) f.completeExceptionally(ex);
4873 +        final CompletionStage<Integer> g = d.exceptionallyCompose(r);
4874 +        if (createIncomplete) f.completeExceptionally(ex);
4875 +
4876 +        checkCompletedExceptionally(f, ex);
4877 +        checkCompletedNormally(g.toCompletableFuture(), r.value);
4878 +        r.assertInvoked();
4879 +    }}
4880 +
4881 +    /**
4882 +     * default-implemented exceptionallyCompose completes
4883 +     * exceptionally on exception if action does
4884 +     */
4885 +    public void testDefaultExceptionallyCompose_actionFailed() {
4886 +        for (boolean createIncomplete : new boolean[] { true, false })
4887 +        for (Integer v1 : new Integer[] { 1, null })
4888 +    {
4889 +        final CFException ex = new CFException();
4890 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4891 +        final FailingExceptionalCompletableFutureFunction r
4892 +            = new FailingExceptionalCompletableFutureFunction(ExecutionMode.SYNC);
4893 +        final DelegatedCompletionStage<Integer> d =
4894 +            new DelegatedCompletionStage<Integer>(f);
4895 +        if (!createIncomplete) f.completeExceptionally(ex);
4896 +        final CompletionStage<Integer> g = d.exceptionallyCompose(r);
4897 +        if (createIncomplete) f.completeExceptionally(ex);
4898 +
4899 +        checkCompletedExceptionally(f, ex);
4900 +        checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex);
4901 +        r.assertInvoked();
4902 +    }}
4903 +
4904 +    /**
4905 +     * default exceptionallyComposeAsync result completes normally after normal
4906 +     * completion of source
4907 +     */
4908 +    public void testDefaultExceptionallyComposeAsync_normalCompletion() {
4909 +        for (boolean createIncomplete : new boolean[] { true, false })
4910 +        for (Integer v1 : new Integer[] { 1, null })
4911 +    {
4912 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4913 +        final ExceptionalCompletableFutureFunction r =
4914 +            new ExceptionalCompletableFutureFunction(ExecutionMode.ASYNC);
4915 +        final DelegatedCompletionStage<Integer> d =
4916 +            new DelegatedCompletionStage<Integer>(f);
4917 +        if (!createIncomplete) assertTrue(f.complete(v1));
4918 +        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r);
4919 +        if (createIncomplete) assertTrue(f.complete(v1));
4920 +
4921 +        checkCompletedNormally(f, v1);
4922 +        checkCompletedNormally(g.toCompletableFuture(), v1);
4923 +        r.assertNotInvoked();
4924 +    }}
4925 +
4926 +    /**
4927 +     * default-implemented exceptionallyComposeAsync result completes
4928 +     * normally after exceptional completion of source
4929 +     */
4930 +    public void testDefaultExceptionallyComposeAsync_exceptionalCompletion() {
4931 +        for (boolean createIncomplete : new boolean[] { true, false })
4932 +    {
4933 +        final CFException ex = new CFException();
4934 +        final ExceptionalCompletableFutureFunction r =
4935 +            new ExceptionalCompletableFutureFunction(ExecutionMode.ASYNC);
4936 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4937 +        final DelegatedCompletionStage<Integer> d =
4938 +            new DelegatedCompletionStage<Integer>(f);
4939 +        if (!createIncomplete) f.completeExceptionally(ex);
4940 +        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r);
4941 +        if (createIncomplete) f.completeExceptionally(ex);
4942 +
4943 +        checkCompletedExceptionally(f, ex);
4944 +        checkCompletedNormally(g.toCompletableFuture(), r.value);
4945 +        r.assertInvoked();
4946 +    }}
4947 +
4948 +    /**
4949 +     * default-implemented exceptionallyComposeAsync completes
4950 +     * exceptionally on exception if action does
4951 +     */
4952 +    public void testDefaultExceptionallyComposeAsync_actionFailed() {
4953 +        for (boolean createIncomplete : new boolean[] { true, false })
4954 +        for (Integer v1 : new Integer[] { 1, null })
4955 +    {
4956 +        final CFException ex = new CFException();
4957 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4958 +        final FailingExceptionalCompletableFutureFunction r
4959 +            = new FailingExceptionalCompletableFutureFunction(ExecutionMode.ASYNC);
4960 +        final DelegatedCompletionStage<Integer> d =
4961 +            new DelegatedCompletionStage<Integer>(f);
4962 +        if (!createIncomplete) f.completeExceptionally(ex);
4963 +        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r);
4964 +        if (createIncomplete) f.completeExceptionally(ex);
4965 +
4966 +        checkCompletedExceptionally(f, ex);
4967 +        checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex);
4968 +        r.assertInvoked();
4969 +    }}
4970 +
4971 +
4972 +    /**
4973 +     * default exceptionallyComposeAsync result completes normally after normal
4974 +     * completion of source
4975 +     */
4976 +    public void testDefaultExceptionallyComposeAsyncExecutor_normalCompletion() {
4977 +        for (boolean createIncomplete : new boolean[] { true, false })
4978 +        for (Integer v1 : new Integer[] { 1, null })
4979 +    {
4980 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4981 +        final ExceptionalCompletableFutureFunction r =
4982 +            new ExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR);
4983 +        final DelegatedCompletionStage<Integer> d =
4984 +            new DelegatedCompletionStage<Integer>(f);
4985 +        if (!createIncomplete) assertTrue(f.complete(v1));
4986 +        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r,  new ThreadExecutor());
4987 +        if (createIncomplete) assertTrue(f.complete(v1));
4988 +
4989 +        checkCompletedNormally(f, v1);
4990 +        checkCompletedNormally(g.toCompletableFuture(), v1);
4991 +        r.assertNotInvoked();
4992 +    }}
4993 +
4994 +    /**
4995 +     * default-implemented exceptionallyComposeAsync result completes
4996 +     * normally after exceptional completion of source
4997 +     */
4998 +    public void testDefaultExceptionallyComposeAsyncExecutor_exceptionalCompletion() {
4999 +        for (boolean createIncomplete : new boolean[] { true, false })
5000 +    {
5001 +        final CFException ex = new CFException();
5002 +        final ExceptionalCompletableFutureFunction r =
5003 +            new ExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR);
5004 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
5005 +        final DelegatedCompletionStage<Integer> d =
5006 +            new DelegatedCompletionStage<Integer>(f);
5007 +        if (!createIncomplete) f.completeExceptionally(ex);
5008 +        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r,  new ThreadExecutor());
5009 +        if (createIncomplete) f.completeExceptionally(ex);
5010 +
5011 +        checkCompletedExceptionally(f, ex);
5012 +        checkCompletedNormally(g.toCompletableFuture(), r.value);
5013 +        r.assertInvoked();
5014 +    }}
5015 +
5016 +    /**
5017 +     * default-implemented exceptionallyComposeAsync completes
5018 +     * exceptionally on exception if action does
5019 +     */
5020 +    public void testDefaultExceptionallyComposeAsyncExecutor_actionFailed() {
5021 +        for (boolean createIncomplete : new boolean[] { true, false })
5022 +        for (Integer v1 : new Integer[] { 1, null })
5023 +    {
5024 +        final CFException ex = new CFException();
5025 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
5026 +        final FailingExceptionalCompletableFutureFunction r
5027 +            = new FailingExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR);
5028 +        final DelegatedCompletionStage<Integer> d =
5029 +            new DelegatedCompletionStage<Integer>(f);
5030 +        if (!createIncomplete) f.completeExceptionally(ex);
5031 +        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r,  new ThreadExecutor());
5032 +        if (createIncomplete) f.completeExceptionally(ex);
5033 +
5034 +        checkCompletedExceptionally(f, ex);
5035 +        checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex);
5036 +        r.assertInvoked();
5037 +    }}
5038 +
5039   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines