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.189 by jsr166, Sat Oct 21 06:51:52 2017 UTC vs.
Revision 1.198 by dl, Mon Sep 17 11:50:55 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));
66 <        } catch (Exception fail) { threadUnexpectedException(fail); }
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());
86 <            assertEquals(value, f.getNow(null));
87 <            assertEquals(value, f.get());
88 <        } catch (Exception fail) { threadUnexpectedException(fail); }
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 +            CompletableFuture<Integer> d = new CompletableFuture<Integer>();
578 +            d.complete(value);
579 +            return d;
580 +        }
581 +    }
582 +
583      static class FailingCompletableFutureFunction extends CheckedIntegerAction
584          implements Function<Integer, CompletableFuture<Integer>>
585      {
# Line 665 | Line 695 | public class CompletableFutureTest exten
695                   Function<? super T,U> a) {
696                  return f.applyToEither(g, a);
697              }
698 +            public <T> CompletableFuture<T> exceptionally
699 +                (CompletableFuture<T> f,
700 +                 Function<Throwable, ? extends T> fn) {
701 +                return f.exceptionally(fn);
702 +            }
703 +            public <T> CompletableFuture<T> exceptionallyCompose
704 +                (CompletableFuture<T> f, Function<Throwable, ? extends CompletionStage<T>> fn) {
705 +                return f.exceptionallyCompose(fn);
706 +            }
707          },
669
708          ASYNC {
709              public void checkExecutionMode() {
710                  assertEquals(defaultExecutorIsCommonPool,
# Line 739 | Line 777 | public class CompletableFutureTest exten
777                   Function<? super T,U> a) {
778                  return f.applyToEitherAsync(g, a);
779              }
780 +            public <T> CompletableFuture<T> exceptionally
781 +                (CompletableFuture<T> f,
782 +                 Function<Throwable, ? extends T> fn) {
783 +                return f.exceptionallyAsync(fn);
784 +            }
785 +
786 +            public <T> CompletableFuture<T> exceptionallyCompose
787 +                (CompletableFuture<T> f, Function<Throwable, ? extends CompletionStage<T>> fn) {
788 +                return f.exceptionallyComposeAsync(fn);
789 +            }
790 +
791          },
792  
793          EXECUTOR {
# Line 812 | Line 861 | public class CompletableFutureTest exten
861                   Function<? super T,U> a) {
862                  return f.applyToEitherAsync(g, a, new ThreadExecutor());
863              }
864 +            public <T> CompletableFuture<T> exceptionally
865 +                (CompletableFuture<T> f,
866 +                 Function<Throwable, ? extends T> fn) {
867 +                return f.exceptionallyAsync(fn, new ThreadExecutor());
868 +            }
869 +            public <T> CompletableFuture<T> exceptionallyCompose
870 +                (CompletableFuture<T> f, Function<Throwable, ? extends CompletionStage<T>> fn) {
871 +                return f.exceptionallyComposeAsync(fn, new ThreadExecutor());
872 +            }
873 +
874          };
875  
876          public abstract void checkExecutionMode();
# Line 854 | Line 913 | public class CompletableFutureTest exten
913              (CompletableFuture<T> f,
914               CompletionStage<? extends T> g,
915               Function<? super T,U> a);
916 +        public abstract <T> CompletableFuture<T> exceptionally
917 +            (CompletableFuture<T> f,
918 +             Function<Throwable, ? extends T> fn);
919 +        public abstract <T> CompletableFuture<T> exceptionallyCompose
920 +            (CompletableFuture<T> f,
921 +             Function<Throwable, ? extends CompletionStage<T>> fn);
922      }
923  
924      /**
# Line 861 | Line 926 | public class CompletableFutureTest exten
926       * normally, and source result is propagated
927       */
928      public void testExceptionally_normalCompletion() {
929 +        for (ExecutionMode m : ExecutionMode.values())
930          for (boolean createIncomplete : new boolean[] { true, false })
931          for (Integer v1 : new Integer[] { 1, null })
932      {
933          final AtomicInteger a = new AtomicInteger(0);
934          final CompletableFuture<Integer> f = new CompletableFuture<>();
935          if (!createIncomplete) assertTrue(f.complete(v1));
936 <        final CompletableFuture<Integer> g = f.exceptionally
937 <            ((Throwable t) -> {
936 >        final CompletableFuture<Integer> g = m.exceptionally
937 >            (f, (Throwable t) -> {
938                  a.getAndIncrement();
939                  threadFail("should not be called");
940                  return null;            // unreached
# Line 885 | Line 951 | public class CompletableFutureTest exten
951       * exception
952       */
953      public void testExceptionally_exceptionalCompletion() {
954 +        for (ExecutionMode m : ExecutionMode.values())
955          for (boolean createIncomplete : new boolean[] { true, false })
956          for (Integer v1 : new Integer[] { 1, null })
957      {
# Line 892 | Line 959 | public class CompletableFutureTest exten
959          final CFException ex = new CFException();
960          final CompletableFuture<Integer> f = new CompletableFuture<>();
961          if (!createIncomplete) f.completeExceptionally(ex);
962 <        final CompletableFuture<Integer> g = f.exceptionally
963 <            ((Throwable t) -> {
964 <                ExecutionMode.SYNC.checkExecutionMode();
962 >        final CompletableFuture<Integer> g = m.exceptionally
963 >            (f, (Throwable t) -> {
964 >                m.checkExecutionMode();
965                  threadAssertSame(t, ex);
966                  a.getAndIncrement();
967                  return v1;
# Line 910 | Line 977 | public class CompletableFutureTest exten
977       * exceptionally with that exception
978       */
979      public void testExceptionally_exceptionalCompletionActionFailed() {
980 +        for (ExecutionMode m : ExecutionMode.values())
981          for (boolean createIncomplete : new boolean[] { true, false })
982      {
983          final AtomicInteger a = new AtomicInteger(0);
# Line 917 | Line 985 | public class CompletableFutureTest exten
985          final CFException ex2 = new CFException();
986          final CompletableFuture<Integer> f = new CompletableFuture<>();
987          if (!createIncomplete) f.completeExceptionally(ex1);
988 <        final CompletableFuture<Integer> g = f.exceptionally
989 <            ((Throwable t) -> {
990 <                ExecutionMode.SYNC.checkExecutionMode();
988 >        final CompletableFuture<Integer> g = m.exceptionally
989 >            (f, (Throwable t) -> {
990 >                m.checkExecutionMode();
991                  threadAssertSame(t, ex1);
992                  a.getAndIncrement();
993                  throw ex2;
# Line 3090 | Line 3158 | public class CompletableFutureTest exten
3158          checkCompletedNormally(f, v1);
3159      }}
3160  
3161 +    /**
3162 +     * exceptionallyCompose result completes normally after normal
3163 +     * completion of source
3164 +     */
3165 +    public void testExceptionallyCompose_normalCompletion() {
3166 +        for (ExecutionMode m : ExecutionMode.values())
3167 +        for (boolean createIncomplete : new boolean[] { true, false })
3168 +        for (Integer v1 : new Integer[] { 1, null })
3169 +    {
3170 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3171 +        final ExceptionalCompletableFutureFunction r =
3172 +            new ExceptionalCompletableFutureFunction(m);
3173 +        if (!createIncomplete) assertTrue(f.complete(v1));
3174 +        final CompletableFuture<Integer> g = m.exceptionallyCompose(f, r);
3175 +        if (createIncomplete) assertTrue(f.complete(v1));
3176 +
3177 +        checkCompletedNormally(f, v1);
3178 +        checkCompletedNormally(g, v1);
3179 +        r.assertNotInvoked();
3180 +    }}
3181 +
3182 +    /**
3183 +     * exceptionallyCompose result completes normally after exceptional
3184 +     * completion of source
3185 +     */
3186 +    public void testExceptionallyCompose_exceptionalCompletion() {
3187 +        for (ExecutionMode m : ExecutionMode.values())
3188 +        for (boolean createIncomplete : new boolean[] { true, false })
3189 +    {
3190 +        final CFException ex = new CFException();
3191 +        final ExceptionalCompletableFutureFunction r =
3192 +            new ExceptionalCompletableFutureFunction(m);
3193 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3194 +        if (!createIncomplete) f.completeExceptionally(ex);
3195 +        final CompletableFuture<Integer> g = m.exceptionallyCompose(f, r);
3196 +        if (createIncomplete) f.completeExceptionally(ex);
3197 +
3198 +        checkCompletedExceptionally(f, ex);
3199 +        checkCompletedNormally(g, r.value);
3200 +        r.assertInvoked();
3201 +    }}
3202 +
3203 +    /**
3204 +     * exceptionallyCompose completes exceptionally on exception if action does
3205 +     */
3206 +    public void testExceptionallyCompose_actionFailed() {
3207 +        for (ExecutionMode m : ExecutionMode.values())
3208 +        for (boolean createIncomplete : new boolean[] { true, false })
3209 +        for (Integer v1 : new Integer[] { 1, null })
3210 +    {
3211 +        final CFException ex = new CFException();
3212 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3213 +        final FailingExceptionalCompletableFutureFunction r
3214 +            = new FailingExceptionalCompletableFutureFunction(m);
3215 +        if (!createIncomplete) f.completeExceptionally(ex);
3216 +        final CompletableFuture<Integer> g = m.exceptionallyCompose(f, r);
3217 +        if (createIncomplete) f.completeExceptionally(ex);
3218 +
3219 +        checkCompletedExceptionally(f, ex);
3220 +        checkCompletedWithWrappedException(g, r.ex);
3221 +        r.assertInvoked();
3222 +    }}
3223 +
3224 +
3225      // other static methods
3226  
3227      /**
# Line 4190 | Line 4322 | public class CompletableFutureTest exten
4322          static void assertZero(CompletableFuture<?> f) {
4323              try {
4324                  f.getNow(null);
4325 <                throw new AssertionFailedError("should throw");
4325 >                throw new AssertionError("should throw");
4326              } catch (CompletionException success) {
4327                  assertTrue(success.getCause() instanceof ZeroException);
4328              }
# Line 4389 | Line 4521 | public class CompletableFutureTest exten
4521              f.complete(null);
4522  
4523              f = new CompletableFuture<>();
4524 <            CompletableFuture.anyOf(new CompletableFuture<?>[] { f, incomplete });
4524 >            CompletableFuture.anyOf(f, incomplete);
4525              f.complete(null);
4526          }
4527  
# Line 4407 | Line 4539 | public class CompletableFutureTest exten
4539              f.complete(null);
4540  
4541              f = new CompletableFuture<>();
4542 <            CompletableFuture.anyOf(new CompletableFuture<?>[] { incomplete, f });
4542 >            CompletableFuture.anyOf(incomplete, f);
4543              f.complete(null);
4544          }
4545      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines