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.188 by jsr166, Wed Aug 16 17:18:34 2017 UTC vs.
Revision 1.202 by jsr166, Sat Sep 22 22:23:08 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 550 | Line 557 | public class CompletableFutureTest exten
557          }
558      }
559  
560 +    static class FailingExceptionalCompletableFutureFunction extends CheckedAction
561 +        implements Function<Throwable, CompletableFuture<Integer>>
562 +    {
563 +        final CFException ex;
564 +        FailingExceptionalCompletableFutureFunction(ExecutionMode m) { super(m); ex = new CFException(); }
565 +        public CompletableFuture<Integer> apply(Throwable x) {
566 +            invoked();
567 +            throw ex;
568 +        }
569 +    }
570 +
571 +    static class ExceptionalCompletableFutureFunction extends CheckedAction
572 +        implements Function<Throwable, CompletionStage<Integer>> {
573 +        final Integer value = 3;
574 +        ExceptionalCompletableFutureFunction(ExecutionMode m) { super(m); }
575 +        public CompletionStage<Integer> apply(Throwable x) {
576 +            invoked();
577 +            return CompletableFuture.completedFuture(value);
578 +        }
579 +    }
580 +
581      static class FailingCompletableFutureFunction extends CheckedIntegerAction
582          implements Function<Integer, CompletableFuture<Integer>>
583      {
# Line 665 | Line 693 | public class CompletableFutureTest exten
693                   Function<? super T,U> a) {
694                  return f.applyToEither(g, a);
695              }
696 +            public <T> CompletableFuture<T> exceptionally
697 +                (CompletableFuture<T> f,
698 +                 Function<Throwable, ? extends T> fn) {
699 +                return f.exceptionally(fn);
700 +            }
701 +            public <T> CompletableFuture<T> exceptionallyCompose
702 +                (CompletableFuture<T> f, Function<Throwable, ? extends CompletionStage<T>> fn) {
703 +                return f.exceptionallyCompose(fn);
704 +            }
705          },
669
706          ASYNC {
707              public void checkExecutionMode() {
708                  assertEquals(defaultExecutorIsCommonPool,
# Line 739 | Line 775 | public class CompletableFutureTest exten
775                   Function<? super T,U> a) {
776                  return f.applyToEitherAsync(g, a);
777              }
778 +            public <T> CompletableFuture<T> exceptionally
779 +                (CompletableFuture<T> f,
780 +                 Function<Throwable, ? extends T> fn) {
781 +                return f.exceptionallyAsync(fn);
782 +            }
783 +
784 +            public <T> CompletableFuture<T> exceptionallyCompose
785 +                (CompletableFuture<T> f, Function<Throwable, ? extends CompletionStage<T>> fn) {
786 +                return f.exceptionallyComposeAsync(fn);
787 +            }
788 +
789          },
790  
791          EXECUTOR {
# Line 812 | Line 859 | public class CompletableFutureTest exten
859                   Function<? super T,U> a) {
860                  return f.applyToEitherAsync(g, a, new ThreadExecutor());
861              }
862 +            public <T> CompletableFuture<T> exceptionally
863 +                (CompletableFuture<T> f,
864 +                 Function<Throwable, ? extends T> fn) {
865 +                return f.exceptionallyAsync(fn, new ThreadExecutor());
866 +            }
867 +            public <T> CompletableFuture<T> exceptionallyCompose
868 +                (CompletableFuture<T> f, Function<Throwable, ? extends CompletionStage<T>> fn) {
869 +                return f.exceptionallyComposeAsync(fn, new ThreadExecutor());
870 +            }
871 +
872          };
873  
874          public abstract void checkExecutionMode();
# Line 854 | Line 911 | public class CompletableFutureTest exten
911              (CompletableFuture<T> f,
912               CompletionStage<? extends T> g,
913               Function<? super T,U> a);
914 +        public abstract <T> CompletableFuture<T> exceptionally
915 +            (CompletableFuture<T> f,
916 +             Function<Throwable, ? extends T> fn);
917 +        public abstract <T> CompletableFuture<T> exceptionallyCompose
918 +            (CompletableFuture<T> f,
919 +             Function<Throwable, ? extends CompletionStage<T>> fn);
920      }
921  
922      /**
# Line 861 | Line 924 | public class CompletableFutureTest exten
924       * normally, and source result is propagated
925       */
926      public void testExceptionally_normalCompletion() {
927 +        for (ExecutionMode m : ExecutionMode.values())
928          for (boolean createIncomplete : new boolean[] { true, false })
929          for (Integer v1 : new Integer[] { 1, null })
930      {
931          final AtomicInteger a = new AtomicInteger(0);
932          final CompletableFuture<Integer> f = new CompletableFuture<>();
933          if (!createIncomplete) assertTrue(f.complete(v1));
934 <        final CompletableFuture<Integer> g = f.exceptionally
935 <            ((Throwable t) -> {
934 >        final CompletableFuture<Integer> g = m.exceptionally
935 >            (f, (Throwable t) -> {
936                  a.getAndIncrement();
937                  threadFail("should not be called");
938                  return null;            // unreached
# Line 885 | Line 949 | public class CompletableFutureTest exten
949       * exception
950       */
951      public void testExceptionally_exceptionalCompletion() {
952 +        for (ExecutionMode m : ExecutionMode.values())
953          for (boolean createIncomplete : new boolean[] { true, false })
954          for (Integer v1 : new Integer[] { 1, null })
955      {
# Line 892 | Line 957 | public class CompletableFutureTest exten
957          final CFException ex = new CFException();
958          final CompletableFuture<Integer> f = new CompletableFuture<>();
959          if (!createIncomplete) f.completeExceptionally(ex);
960 <        final CompletableFuture<Integer> g = f.exceptionally
961 <            ((Throwable t) -> {
962 <                ExecutionMode.SYNC.checkExecutionMode();
960 >        final CompletableFuture<Integer> g = m.exceptionally
961 >            (f, (Throwable t) -> {
962 >                m.checkExecutionMode();
963                  threadAssertSame(t, ex);
964                  a.getAndIncrement();
965                  return v1;
# Line 910 | Line 975 | public class CompletableFutureTest exten
975       * exceptionally with that exception
976       */
977      public void testExceptionally_exceptionalCompletionActionFailed() {
978 +        for (ExecutionMode m : ExecutionMode.values())
979          for (boolean createIncomplete : new boolean[] { true, false })
980      {
981          final AtomicInteger a = new AtomicInteger(0);
# Line 917 | Line 983 | public class CompletableFutureTest exten
983          final CFException ex2 = new CFException();
984          final CompletableFuture<Integer> f = new CompletableFuture<>();
985          if (!createIncomplete) f.completeExceptionally(ex1);
986 <        final CompletableFuture<Integer> g = f.exceptionally
987 <            ((Throwable t) -> {
988 <                ExecutionMode.SYNC.checkExecutionMode();
986 >        final CompletableFuture<Integer> g = m.exceptionally
987 >            (f, (Throwable t) -> {
988 >                m.checkExecutionMode();
989                  threadAssertSame(t, ex1);
990                  a.getAndIncrement();
991                  throw ex2;
# Line 3090 | Line 3156 | public class CompletableFutureTest exten
3156          checkCompletedNormally(f, v1);
3157      }}
3158  
3159 +    /**
3160 +     * exceptionallyCompose result completes normally after normal
3161 +     * completion of source
3162 +     */
3163 +    public void testExceptionallyCompose_normalCompletion() {
3164 +        for (ExecutionMode m : ExecutionMode.values())
3165 +        for (boolean createIncomplete : new boolean[] { true, false })
3166 +        for (Integer v1 : new Integer[] { 1, null })
3167 +    {
3168 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3169 +        final ExceptionalCompletableFutureFunction r =
3170 +            new ExceptionalCompletableFutureFunction(m);
3171 +        if (!createIncomplete) assertTrue(f.complete(v1));
3172 +        final CompletableFuture<Integer> g = m.exceptionallyCompose(f, r);
3173 +        if (createIncomplete) assertTrue(f.complete(v1));
3174 +
3175 +        checkCompletedNormally(f, v1);
3176 +        checkCompletedNormally(g, v1);
3177 +        r.assertNotInvoked();
3178 +    }}
3179 +
3180 +    /**
3181 +     * exceptionallyCompose result completes normally after exceptional
3182 +     * completion of source
3183 +     */
3184 +    public void testExceptionallyCompose_exceptionalCompletion() {
3185 +        for (ExecutionMode m : ExecutionMode.values())
3186 +        for (boolean createIncomplete : new boolean[] { true, false })
3187 +    {
3188 +        final CFException ex = new CFException();
3189 +        final ExceptionalCompletableFutureFunction r =
3190 +            new ExceptionalCompletableFutureFunction(m);
3191 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3192 +        if (!createIncomplete) f.completeExceptionally(ex);
3193 +        final CompletableFuture<Integer> g = m.exceptionallyCompose(f, r);
3194 +        if (createIncomplete) f.completeExceptionally(ex);
3195 +
3196 +        checkCompletedExceptionally(f, ex);
3197 +        checkCompletedNormally(g, r.value);
3198 +        r.assertInvoked();
3199 +    }}
3200 +
3201 +    /**
3202 +     * exceptionallyCompose completes exceptionally on exception if action does
3203 +     */
3204 +    public void testExceptionallyCompose_actionFailed() {
3205 +        for (ExecutionMode m : ExecutionMode.values())
3206 +        for (boolean createIncomplete : new boolean[] { true, false })
3207 +        for (Integer v1 : new Integer[] { 1, null })
3208 +    {
3209 +        final CFException ex = new CFException();
3210 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3211 +        final FailingExceptionalCompletableFutureFunction r
3212 +            = new FailingExceptionalCompletableFutureFunction(m);
3213 +        if (!createIncomplete) f.completeExceptionally(ex);
3214 +        final CompletableFuture<Integer> g = m.exceptionallyCompose(f, r);
3215 +        if (createIncomplete) f.completeExceptionally(ex);
3216 +
3217 +        checkCompletedExceptionally(f, ex);
3218 +        checkCompletedWithWrappedException(g, r.ex);
3219 +        r.assertInvoked();
3220 +    }}
3221 +
3222 +
3223      // other static methods
3224  
3225      /**
# Line 4190 | Line 4320 | public class CompletableFutureTest exten
4320          static void assertZero(CompletableFuture<?> f) {
4321              try {
4322                  f.getNow(null);
4323 <                throw new AssertionFailedError("should throw");
4323 >                throw new AssertionError("should throw");
4324              } catch (CompletionException success) {
4325                  assertTrue(success.getCause() instanceof ZeroException);
4326              }
# Line 4389 | Line 4519 | public class CompletableFutureTest exten
4519              f.complete(null);
4520  
4521              f = new CompletableFuture<>();
4522 <            CompletableFuture.anyOf(new CompletableFuture<?>[] { f, incomplete });
4522 >            CompletableFuture.anyOf(f, incomplete);
4523              f.complete(null);
4524          }
4525  
# Line 4407 | Line 4537 | public class CompletableFutureTest exten
4537              f.complete(null);
4538  
4539              f = new CompletableFuture<>();
4540 <            CompletableFuture.anyOf(new CompletableFuture<?>[] { incomplete, f });
4540 >            CompletableFuture.anyOf(incomplete, f);
4541              f.complete(null);
4542          }
4543      }
# Line 4501 | Line 4631 | public class CompletableFutureTest exten
4631   //         return stage.toCompletableFuture().copy().isDone();
4632   //     }
4633  
4634 +    // For testing default implementations
4635 +    // Only non-default interface methods defined.
4636 +    static final class DelegatedCompletionStage<T> implements CompletionStage<T> {
4637 +        final CompletableFuture<T> cf;
4638 +        DelegatedCompletionStage(CompletableFuture<T> cf) { this.cf = cf; }
4639 +        public CompletableFuture<T> toCompletableFuture() {
4640 +            return cf; }
4641 +        public CompletionStage<Void> thenRun
4642 +            (Runnable action) {
4643 +            return cf.thenRun(action); }
4644 +        public CompletionStage<Void> thenRunAsync
4645 +            (Runnable action) {
4646 +            return cf.thenRunAsync(action); }
4647 +        public CompletionStage<Void> thenRunAsync
4648 +            (Runnable action,
4649 +             Executor executor) {
4650 +            return cf.thenRunAsync(action, executor); }
4651 +        public CompletionStage<Void> thenAccept
4652 +            (Consumer<? super T> action) {
4653 +            return cf.thenAccept(action); }
4654 +        public CompletionStage<Void> thenAcceptAsync
4655 +            (Consumer<? super T> action) {
4656 +            return cf.thenAcceptAsync(action); }
4657 +        public CompletionStage<Void> thenAcceptAsync
4658 +            (Consumer<? super T> action,
4659 +             Executor executor) {
4660 +            return cf.thenAcceptAsync(action, executor); }
4661 +        public <U> CompletionStage<U> thenApply
4662 +            (Function<? super T,? extends U> a) {
4663 +            return cf.thenApply(a); }
4664 +        public <U> CompletionStage<U> thenApplyAsync
4665 +            (Function<? super T,? extends U> fn) {
4666 +            return cf.thenApplyAsync(fn); }
4667 +        public <U> CompletionStage<U> thenApplyAsync
4668 +            (Function<? super T,? extends U> fn,
4669 +             Executor executor) {
4670 +            return cf.thenApplyAsync(fn, executor); }
4671 +        public <U,V> CompletionStage<V> thenCombine
4672 +            (CompletionStage<? extends U> other,
4673 +             BiFunction<? super T,? super U,? extends V> fn) {
4674 +            return cf.thenCombine(other, fn); }
4675 +        public <U,V> CompletionStage<V> thenCombineAsync
4676 +            (CompletionStage<? extends U> other,
4677 +             BiFunction<? super T,? super U,? extends V> fn) {
4678 +            return cf.thenCombineAsync(other, fn); }
4679 +        public <U,V> CompletionStage<V> thenCombineAsync
4680 +            (CompletionStage<? extends U> other,
4681 +             BiFunction<? super T,? super U,? extends V> fn,
4682 +             Executor executor) {
4683 +            return cf.thenCombineAsync(other, fn, executor); }
4684 +        public <U> CompletionStage<Void> thenAcceptBoth
4685 +            (CompletionStage<? extends U> other,
4686 +             BiConsumer<? super T, ? super U> action) {
4687 +            return cf.thenAcceptBoth(other, action); }
4688 +        public <U> CompletionStage<Void> thenAcceptBothAsync
4689 +            (CompletionStage<? extends U> other,
4690 +             BiConsumer<? super T, ? super U> action) {
4691 +            return cf.thenAcceptBothAsync(other, action); }
4692 +        public <U> CompletionStage<Void> thenAcceptBothAsync
4693 +            (CompletionStage<? extends U> other,
4694 +             BiConsumer<? super T, ? super U> action,
4695 +             Executor executor) {
4696 +            return cf.thenAcceptBothAsync(other, action, executor); }
4697 +        public CompletionStage<Void> runAfterBoth
4698 +            (CompletionStage<?> other,
4699 +             Runnable action) {
4700 +            return cf.runAfterBoth(other, action); }
4701 +        public CompletionStage<Void> runAfterBothAsync
4702 +            (CompletionStage<?> other,
4703 +             Runnable action) {
4704 +            return cf.runAfterBothAsync(other, action); }
4705 +        public CompletionStage<Void> runAfterBothAsync
4706 +            (CompletionStage<?> other,
4707 +             Runnable action,
4708 +             Executor executor) {
4709 +            return cf.runAfterBothAsync(other, action, executor); }
4710 +        public <U> CompletionStage<U> applyToEither
4711 +            (CompletionStage<? extends T> other,
4712 +             Function<? super T, U> fn) {
4713 +            return cf.applyToEither(other, fn); }
4714 +        public <U> CompletionStage<U> applyToEitherAsync
4715 +            (CompletionStage<? extends T> other,
4716 +             Function<? super T, U> fn) {
4717 +            return cf.applyToEitherAsync(other, fn); }
4718 +        public <U> CompletionStage<U> applyToEitherAsync
4719 +            (CompletionStage<? extends T> other,
4720 +             Function<? super T, U> fn,
4721 +             Executor executor) {
4722 +            return cf.applyToEitherAsync(other, fn, executor); }
4723 +        public CompletionStage<Void> acceptEither
4724 +            (CompletionStage<? extends T> other,
4725 +             Consumer<? super T> action) {
4726 +            return cf.acceptEither(other, action); }
4727 +        public CompletionStage<Void> acceptEitherAsync
4728 +            (CompletionStage<? extends T> other,
4729 +             Consumer<? super T> action) {
4730 +            return cf.acceptEitherAsync(other, action); }
4731 +        public CompletionStage<Void> acceptEitherAsync
4732 +            (CompletionStage<? extends T> other,
4733 +             Consumer<? super T> action,
4734 +             Executor executor) {
4735 +            return cf.acceptEitherAsync(other, action, executor); }
4736 +        public CompletionStage<Void> runAfterEither
4737 +            (CompletionStage<?> other,
4738 +             Runnable action) {
4739 +            return cf.runAfterEither(other, action); }
4740 +        public CompletionStage<Void> runAfterEitherAsync
4741 +            (CompletionStage<?> other,
4742 +             Runnable action) {
4743 +            return cf.runAfterEitherAsync(other, action); }
4744 +        public CompletionStage<Void> runAfterEitherAsync
4745 +            (CompletionStage<?> other,
4746 +             Runnable action,
4747 +             Executor executor) {
4748 +            return cf.runAfterEitherAsync(other, action, executor); }
4749 +        public <U> CompletionStage<U> thenCompose
4750 +            (Function<? super T, ? extends CompletionStage<U>> fn) {
4751 +            return cf.thenCompose(fn); }
4752 +        public <U> CompletionStage<U> thenComposeAsync
4753 +            (Function<? super T, ? extends CompletionStage<U>> fn) {
4754 +            return cf.thenComposeAsync(fn); }
4755 +        public <U> CompletionStage<U> thenComposeAsync
4756 +            (Function<? super T, ? extends CompletionStage<U>> fn,
4757 +             Executor executor) {
4758 +            return cf.thenComposeAsync(fn, executor); }
4759 +        public <U> CompletionStage<U> handle
4760 +            (BiFunction<? super T, Throwable, ? extends U> fn) {
4761 +            return cf.handle(fn); }
4762 +        public <U> CompletionStage<U> handleAsync
4763 +            (BiFunction<? super T, Throwable, ? extends U> fn) {
4764 +            return cf.handleAsync(fn); }
4765 +        public <U> CompletionStage<U> handleAsync
4766 +            (BiFunction<? super T, Throwable, ? extends U> fn,
4767 +             Executor executor) {
4768 +            return cf.handleAsync(fn, executor); }
4769 +        public CompletionStage<T> whenComplete
4770 +            (BiConsumer<? super T, ? super Throwable> action) {
4771 +            return cf.whenComplete(action); }
4772 +        public CompletionStage<T> whenCompleteAsync
4773 +            (BiConsumer<? super T, ? super Throwable> action) {
4774 +            return cf.whenCompleteAsync(action); }
4775 +        public CompletionStage<T> whenCompleteAsync
4776 +            (BiConsumer<? super T, ? super Throwable> action,
4777 +             Executor executor) {
4778 +            return cf.whenCompleteAsync(action, executor); }
4779 +        public CompletionStage<T> exceptionally
4780 +            (Function<Throwable, ? extends T> fn) {
4781 +            return cf.exceptionally(fn); }
4782 +    }
4783 +
4784 +    /**
4785 +     * default-implemented exceptionallyAsync action completes with
4786 +     * function value on source exception
4787 +     */
4788 +    public void testDefaulExceptionallyAsync_exceptionalCompletion() {
4789 +        for (boolean createIncomplete : new boolean[] { true, false })
4790 +        for (Integer v1 : new Integer[] { 1, null })
4791 +    {
4792 +        final AtomicInteger a = new AtomicInteger(0);
4793 +        final CFException ex = new CFException();
4794 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4795 +        final DelegatedCompletionStage<Integer> d =
4796 +            new DelegatedCompletionStage<Integer>(f);
4797 +        if (!createIncomplete) f.completeExceptionally(ex);
4798 +        final CompletionStage<Integer> g = d.exceptionallyAsync
4799 +            ((Throwable t) -> {
4800 +                threadAssertSame(t, ex);
4801 +                a.getAndIncrement();
4802 +                return v1;
4803 +            });
4804 +        if (createIncomplete) f.completeExceptionally(ex);
4805 +
4806 +        checkCompletedNormally(g.toCompletableFuture(), v1);
4807 +        assertEquals(1, a.get());
4808 +    }}
4809 +
4810 +    /**
4811 +     * Under default implementation, if an "exceptionally action"
4812 +     * throws an exception, it completes exceptionally with that
4813 +     * exception
4814 +     */
4815 +    public void testDefaulExceptionallyAsync_exceptionalCompletionActionFailed() {
4816 +        for (boolean createIncomplete : new boolean[] { true, false })
4817 +    {
4818 +        final AtomicInteger a = new AtomicInteger(0);
4819 +        final CFException ex1 = new CFException();
4820 +        final CFException ex2 = new CFException();
4821 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4822 +        final DelegatedCompletionStage<Integer> d =
4823 +            new DelegatedCompletionStage<Integer>(f);
4824 +        if (!createIncomplete) f.completeExceptionally(ex1);
4825 +        final CompletionStage<Integer> g = d.exceptionallyAsync
4826 +            ((Throwable t) -> {
4827 +                threadAssertSame(t, ex1);
4828 +                a.getAndIncrement();
4829 +                throw ex2;
4830 +            });
4831 +        if (createIncomplete) f.completeExceptionally(ex1);
4832 +
4833 +        checkCompletedWithWrappedException(g.toCompletableFuture(), ex2);
4834 +        checkCompletedExceptionally(f, ex1);
4835 +        checkCompletedExceptionally(d.toCompletableFuture(), ex1);
4836 +        assertEquals(1, a.get());
4837 +    }}
4838 +
4839 +    /**
4840 +     * default exceptionallyCompose result completes normally after normal
4841 +     * completion of source
4842 +     */
4843 +    public void testDefaultExceptionallyCompose_normalCompletion() {
4844 +        for (boolean createIncomplete : new boolean[] { true, false })
4845 +        for (Integer v1 : new Integer[] { 1, null })
4846 +    {
4847 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4848 +        final ExceptionalCompletableFutureFunction r =
4849 +            new ExceptionalCompletableFutureFunction(ExecutionMode.SYNC);
4850 +        final DelegatedCompletionStage<Integer> d =
4851 +            new DelegatedCompletionStage<Integer>(f);
4852 +        if (!createIncomplete) assertTrue(f.complete(v1));
4853 +        final CompletionStage<Integer> g = d.exceptionallyCompose(r);
4854 +        if (createIncomplete) assertTrue(f.complete(v1));
4855 +
4856 +        checkCompletedNormally(f, v1);
4857 +        checkCompletedNormally(g.toCompletableFuture(), v1);
4858 +        r.assertNotInvoked();
4859 +    }}
4860 +
4861 +    /**
4862 +     * default-implemented exceptionallyCompose result completes
4863 +     * normally after exceptional completion of source
4864 +     */
4865 +    public void testDefaultExceptionallyCompose_exceptionalCompletion() {
4866 +        for (boolean createIncomplete : new boolean[] { true, false })
4867 +    {
4868 +        final CFException ex = new CFException();
4869 +        final ExceptionalCompletableFutureFunction r =
4870 +            new ExceptionalCompletableFutureFunction(ExecutionMode.SYNC);
4871 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4872 +        final DelegatedCompletionStage<Integer> d =
4873 +            new DelegatedCompletionStage<Integer>(f);
4874 +        if (!createIncomplete) f.completeExceptionally(ex);
4875 +        final CompletionStage<Integer> g = d.exceptionallyCompose(r);
4876 +        if (createIncomplete) f.completeExceptionally(ex);
4877 +
4878 +        checkCompletedExceptionally(f, ex);
4879 +        checkCompletedNormally(g.toCompletableFuture(), r.value);
4880 +        r.assertInvoked();
4881 +    }}
4882 +
4883 +    /**
4884 +     * default-implemented exceptionallyCompose completes
4885 +     * exceptionally on exception if action does
4886 +     */
4887 +    public void testDefaultExceptionallyCompose_actionFailed() {
4888 +        for (boolean createIncomplete : new boolean[] { true, false })
4889 +        for (Integer v1 : new Integer[] { 1, null })
4890 +    {
4891 +        final CFException ex = new CFException();
4892 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4893 +        final FailingExceptionalCompletableFutureFunction r
4894 +            = new FailingExceptionalCompletableFutureFunction(ExecutionMode.SYNC);
4895 +        final DelegatedCompletionStage<Integer> d =
4896 +            new DelegatedCompletionStage<Integer>(f);
4897 +        if (!createIncomplete) f.completeExceptionally(ex);
4898 +        final CompletionStage<Integer> g = d.exceptionallyCompose(r);
4899 +        if (createIncomplete) f.completeExceptionally(ex);
4900 +
4901 +        checkCompletedExceptionally(f, ex);
4902 +        checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex);
4903 +        r.assertInvoked();
4904 +    }}
4905 +
4906 +    /**
4907 +     * default exceptionallyComposeAsync result completes normally after normal
4908 +     * completion of source
4909 +     */
4910 +    public void testDefaultExceptionallyComposeAsync_normalCompletion() {
4911 +        for (boolean createIncomplete : new boolean[] { true, false })
4912 +        for (Integer v1 : new Integer[] { 1, null })
4913 +    {
4914 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4915 +        final ExceptionalCompletableFutureFunction r =
4916 +            new ExceptionalCompletableFutureFunction(ExecutionMode.ASYNC);
4917 +        final DelegatedCompletionStage<Integer> d =
4918 +            new DelegatedCompletionStage<Integer>(f);
4919 +        if (!createIncomplete) assertTrue(f.complete(v1));
4920 +        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r);
4921 +        if (createIncomplete) assertTrue(f.complete(v1));
4922 +
4923 +        checkCompletedNormally(f, v1);
4924 +        checkCompletedNormally(g.toCompletableFuture(), v1);
4925 +        r.assertNotInvoked();
4926 +    }}
4927 +
4928 +    /**
4929 +     * default-implemented exceptionallyComposeAsync result completes
4930 +     * normally after exceptional completion of source
4931 +     */
4932 +    public void testDefaultExceptionallyComposeAsync_exceptionalCompletion() {
4933 +        for (boolean createIncomplete : new boolean[] { true, false })
4934 +    {
4935 +        final CFException ex = new CFException();
4936 +        final ExceptionalCompletableFutureFunction r =
4937 +            new ExceptionalCompletableFutureFunction(ExecutionMode.ASYNC);
4938 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4939 +        final DelegatedCompletionStage<Integer> d =
4940 +            new DelegatedCompletionStage<Integer>(f);
4941 +        if (!createIncomplete) f.completeExceptionally(ex);
4942 +        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r);
4943 +        if (createIncomplete) f.completeExceptionally(ex);
4944 +
4945 +        checkCompletedExceptionally(f, ex);
4946 +        checkCompletedNormally(g.toCompletableFuture(), r.value);
4947 +        r.assertInvoked();
4948 +    }}
4949 +
4950 +    /**
4951 +     * default-implemented exceptionallyComposeAsync completes
4952 +     * exceptionally on exception if action does
4953 +     */
4954 +    public void testDefaultExceptionallyComposeAsync_actionFailed() {
4955 +        for (boolean createIncomplete : new boolean[] { true, false })
4956 +        for (Integer v1 : new Integer[] { 1, null })
4957 +    {
4958 +        final CFException ex = new CFException();
4959 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4960 +        final FailingExceptionalCompletableFutureFunction r
4961 +            = new FailingExceptionalCompletableFutureFunction(ExecutionMode.ASYNC);
4962 +        final DelegatedCompletionStage<Integer> d =
4963 +            new DelegatedCompletionStage<Integer>(f);
4964 +        if (!createIncomplete) f.completeExceptionally(ex);
4965 +        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r);
4966 +        if (createIncomplete) f.completeExceptionally(ex);
4967 +
4968 +        checkCompletedExceptionally(f, ex);
4969 +        checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex);
4970 +        r.assertInvoked();
4971 +    }}
4972 +
4973 +
4974 +    /**
4975 +     * default exceptionallyComposeAsync result completes normally after normal
4976 +     * completion of source
4977 +     */
4978 +    public void testDefaultExceptionallyComposeAsyncExecutor_normalCompletion() {
4979 +        for (boolean createIncomplete : new boolean[] { true, false })
4980 +        for (Integer v1 : new Integer[] { 1, null })
4981 +    {
4982 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4983 +        final ExceptionalCompletableFutureFunction r =
4984 +            new ExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR);
4985 +        final DelegatedCompletionStage<Integer> d =
4986 +            new DelegatedCompletionStage<Integer>(f);
4987 +        if (!createIncomplete) assertTrue(f.complete(v1));
4988 +        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r,  new ThreadExecutor());
4989 +        if (createIncomplete) assertTrue(f.complete(v1));
4990 +
4991 +        checkCompletedNormally(f, v1);
4992 +        checkCompletedNormally(g.toCompletableFuture(), v1);
4993 +        r.assertNotInvoked();
4994 +    }}
4995 +
4996 +    /**
4997 +     * default-implemented exceptionallyComposeAsync result completes
4998 +     * normally after exceptional completion of source
4999 +     */
5000 +    public void testDefaultExceptionallyComposeAsyncExecutor_exceptionalCompletion() {
5001 +        for (boolean createIncomplete : new boolean[] { true, false })
5002 +    {
5003 +        final CFException ex = new CFException();
5004 +        final ExceptionalCompletableFutureFunction r =
5005 +            new ExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR);
5006 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
5007 +        final DelegatedCompletionStage<Integer> d =
5008 +            new DelegatedCompletionStage<Integer>(f);
5009 +        if (!createIncomplete) f.completeExceptionally(ex);
5010 +        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r,  new ThreadExecutor());
5011 +        if (createIncomplete) f.completeExceptionally(ex);
5012 +
5013 +        checkCompletedExceptionally(f, ex);
5014 +        checkCompletedNormally(g.toCompletableFuture(), r.value);
5015 +        r.assertInvoked();
5016 +    }}
5017 +
5018 +    /**
5019 +     * default-implemented exceptionallyComposeAsync completes
5020 +     * exceptionally on exception if action does
5021 +     */
5022 +    public void testDefaultExceptionallyComposeAsyncExecutor_actionFailed() {
5023 +        for (boolean createIncomplete : new boolean[] { true, false })
5024 +        for (Integer v1 : new Integer[] { 1, null })
5025 +    {
5026 +        final CFException ex = new CFException();
5027 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
5028 +        final FailingExceptionalCompletableFutureFunction r
5029 +            = new FailingExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR);
5030 +        final DelegatedCompletionStage<Integer> d =
5031 +            new DelegatedCompletionStage<Integer>(f);
5032 +        if (!createIncomplete) f.completeExceptionally(ex);
5033 +        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r,  new ThreadExecutor());
5034 +        if (createIncomplete) f.completeExceptionally(ex);
5035 +
5036 +        checkCompletedExceptionally(f, ex);
5037 +        checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex);
5038 +        r.assertInvoked();
5039 +    }}
5040 +
5041   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines