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.182 by jsr166, Tue Jan 3 03:18:02 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 59 | Line 58 | public class CompletableFutureTest exten
58      void checkIncomplete(CompletableFuture<?> f) {
59          assertFalse(f.isDone());
60          assertFalse(f.isCancelled());
61 <        assertTrue(f.toString().contains("Not completed"));
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(0L, SECONDS);
70 >            f.get(randomExpiredTimeout(), randomTimeUnit());
71              shouldThrow();
72          }
73          catch (TimeoutException success) {}
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 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
80 <        try {
81 <            assertEquals(value, f.getNow(null));
82 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
83 <        try {
84 <            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());
92 <        assertTrue(f.toString().contains("[Completed normally]"));
92 >        assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]"));
93      }
94  
95      /**
# Line 142 | Line 145 | public class CompletableFutureTest exten
145          assertFalse(f.isCancelled());
146          assertTrue(f.isDone());
147          assertTrue(f.isCompletedExceptionally());
148 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
148 >        assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
149      }
150  
151      void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
# Line 197 | Line 200 | public class CompletableFutureTest exten
200          assertTrue(f.isDone());
201          assertTrue(f.isCompletedExceptionally());
202          assertTrue(f.isCancelled());
203 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
203 >        assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
204      }
205  
206      /**
# Line 296 | 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 333 | Line 336 | public class CompletableFutureTest exten
336      /**
337       * toString indicates current completion state
338       */
339 <    public void testToString() {
340 <        CompletableFuture<String> f;
341 <
342 <        f = new CompletableFuture<String>();
343 <        assertTrue(f.toString().contains("[Not completed]"));
339 >    public void testToString_incomplete() {
340 >        CompletableFuture<String> f = new CompletableFuture<>();
341 >        assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]"));
342 >        if (testImplementationDetails)
343 >            assertEquals(identityString(f) + "[Not completed]",
344 >                         f.toString());
345 >    }
346  
347 +    public void testToString_normal() {
348 +        CompletableFuture<String> f = new CompletableFuture<>();
349          assertTrue(f.complete("foo"));
350 <        assertTrue(f.toString().contains("[Completed normally]"));
350 >        assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]"));
351 >        if (testImplementationDetails)
352 >            assertEquals(identityString(f) + "[Completed normally]",
353 >                         f.toString());
354 >    }
355  
356 <        f = new CompletableFuture<String>();
356 >    public void testToString_exception() {
357 >        CompletableFuture<String> f = new CompletableFuture<>();
358          assertTrue(f.completeExceptionally(new IndexOutOfBoundsException()));
359 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
359 >        assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
360 >        if (testImplementationDetails)
361 >            assertTrue(f.toString().startsWith(
362 >                               identityString(f) + "[Completed exceptionally: "));
363 >    }
364  
365 +    public void testToString_cancelled() {
366          for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
367 <            f = new CompletableFuture<String>();
367 >            CompletableFuture<String> f = new CompletableFuture<>();
368              assertTrue(f.cancel(mayInterruptIfRunning));
369 <            assertTrue(f.toString().contains("[Completed exceptionally]"));
369 >            assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
370 >            if (testImplementationDetails)
371 >                assertTrue(f.toString().startsWith(
372 >                                   identityString(f) + "[Completed exceptionally: "));
373          }
374      }
375  
# Line 531 | 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 652 | 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          },
656
704          ASYNC {
705              public void checkExecutionMode() {
706                  assertEquals(defaultExecutorIsCommonPool,
# Line 726 | 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 799 | 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 841 | 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 848 | 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 872 | 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 879 | 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 897 | 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 904 | 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 1242 | Line 1319 | public class CompletableFutureTest exten
1319          r.assertInvoked();
1320      }}
1321  
1322 +    @SuppressWarnings("FutureReturnValueIgnored")
1323      public void testRunAsync_rejectingExecutor() {
1324          CountingRejectingExecutor e = new CountingRejectingExecutor();
1325          try {
# Line 1288 | Line 1366 | public class CompletableFutureTest exten
1366          r.assertInvoked();
1367      }}
1368  
1369 +    @SuppressWarnings("FutureReturnValueIgnored")
1370      public void testSupplyAsync_rejectingExecutor() {
1371          CountingRejectingExecutor e = new CountingRejectingExecutor();
1372          try {
# Line 2562 | Line 2641 | public class CompletableFutureTest exten
2641  
2642          // unspecified behavior - both source completions available
2643          try {
2644 <            assertEquals(null, h0.join());
2644 >            assertNull(h0.join());
2645              rs[0].assertValue(v1);
2646          } catch (CompletionException ok) {
2647              checkCompletedWithWrappedException(h0, ex);
2648              rs[0].assertNotInvoked();
2649          }
2650          try {
2651 <            assertEquals(null, h1.join());
2651 >            assertNull(h1.join());
2652              rs[1].assertValue(v1);
2653          } catch (CompletionException ok) {
2654              checkCompletedWithWrappedException(h1, ex);
2655              rs[1].assertNotInvoked();
2656          }
2657          try {
2658 <            assertEquals(null, h2.join());
2658 >            assertNull(h2.join());
2659              rs[2].assertValue(v1);
2660          } catch (CompletionException ok) {
2661              checkCompletedWithWrappedException(h2, ex);
2662              rs[2].assertNotInvoked();
2663          }
2664          try {
2665 <            assertEquals(null, h3.join());
2665 >            assertNull(h3.join());
2666              rs[3].assertValue(v1);
2667          } catch (CompletionException ok) {
2668              checkCompletedWithWrappedException(h3, ex);
# Line 2822 | Line 2901 | public class CompletableFutureTest exten
2901  
2902          // unspecified behavior - both source completions available
2903          try {
2904 <            assertEquals(null, h0.join());
2904 >            assertNull(h0.join());
2905              rs[0].assertInvoked();
2906          } catch (CompletionException ok) {
2907              checkCompletedWithWrappedException(h0, ex);
2908              rs[0].assertNotInvoked();
2909          }
2910          try {
2911 <            assertEquals(null, h1.join());
2911 >            assertNull(h1.join());
2912              rs[1].assertInvoked();
2913          } catch (CompletionException ok) {
2914              checkCompletedWithWrappedException(h1, ex);
2915              rs[1].assertNotInvoked();
2916          }
2917          try {
2918 <            assertEquals(null, h2.join());
2918 >            assertNull(h2.join());
2919              rs[2].assertInvoked();
2920          } catch (CompletionException ok) {
2921              checkCompletedWithWrappedException(h2, ex);
2922              rs[2].assertNotInvoked();
2923          }
2924          try {
2925 <            assertEquals(null, h3.join());
2925 >            assertNull(h3.join());
2926              rs[3].assertInvoked();
2927          } catch (CompletionException ok) {
2928              checkCompletedWithWrappedException(h3, ex);
# Line 3075 | 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 3238 | Line 3381 | public class CompletableFutureTest exten
3381      /**
3382       * Completion methods throw NullPointerException with null arguments
3383       */
3384 +    @SuppressWarnings("FutureReturnValueIgnored")
3385      public void testNPE() {
3386          CompletableFuture<Integer> f = new CompletableFuture<>();
3387          CompletableFuture<Integer> g = new CompletableFuture<>();
# Line 3532 | Line 3676 | public class CompletableFutureTest exten
3676       */
3677      public void testCompletedStage() {
3678          AtomicInteger x = new AtomicInteger(0);
3679 <        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3679 >        AtomicReference<Throwable> r = new AtomicReference<>();
3680          CompletionStage<Integer> f = CompletableFuture.completedStage(1);
3681          f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3682          assertEquals(x.get(), 1);
# Line 3634 | Line 3778 | public class CompletableFutureTest exten
3778          CompletableFuture<Integer> f = new CompletableFuture<>();
3779          CompletionStage<Integer> g = f.minimalCompletionStage();
3780          AtomicInteger x = new AtomicInteger(0);
3781 <        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3781 >        AtomicReference<Throwable> r = new AtomicReference<>();
3782          checkIncomplete(f);
3783          g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3784          f.complete(1);
# Line 3651 | Line 3795 | public class CompletableFutureTest exten
3795          CompletableFuture<Integer> f = new CompletableFuture<>();
3796          CompletionStage<Integer> g = f.minimalCompletionStage();
3797          AtomicInteger x = new AtomicInteger(0);
3798 <        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3798 >        AtomicReference<Throwable> r = new AtomicReference<>();
3799          g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3800          checkIncomplete(f);
3801          CFException ex = new CFException();
# Line 3669 | Line 3813 | public class CompletableFutureTest exten
3813          CFException ex = new CFException();
3814          CompletionStage<Integer> f = CompletableFuture.failedStage(ex);
3815          AtomicInteger x = new AtomicInteger(0);
3816 <        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3816 >        AtomicReference<Throwable> r = new AtomicReference<>();
3817          f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3818          assertEquals(x.get(), 0);
3819          assertEquals(r.get(), ex);
# Line 4174 | 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 4299 | Line 4443 | public class CompletableFutureTest exten
4443      }
4444  
4445      /** Test long recursive chains of CompletableFutures with cascading completions */
4446 +    @SuppressWarnings("FutureReturnValueIgnored")
4447      public void testRecursiveChains() throws Throwable {
4448          for (ExecutionMode m : ExecutionMode.values())
4449          for (boolean addDeadEnds : new boolean[] { true, false })
# Line 4323 | Line 4468 | public class CompletableFutureTest exten
4468       * A single CompletableFuture with many dependents.
4469       * A demo of scalability - runtime is O(n).
4470       */
4471 +    @SuppressWarnings("FutureReturnValueIgnored")
4472      public void testManyDependents() throws Throwable {
4473          final int n = expensiveTests ? 1_000_000 : 10;
4474          final CompletableFuture<Void> head = new CompletableFuture<>();
# Line 4352 | Line 4498 | public class CompletableFutureTest exten
4498      }
4499  
4500      /** ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck */
4501 +    @SuppressWarnings("FutureReturnValueIgnored")
4502      public void testCoCompletionGarbageRetention() throws Throwable {
4503          final int n = expensiveTests ? 1_000_000 : 10;
4504          final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
# Line 4370 | 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 4388 | 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 4482 | 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