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.80 by jsr166, Mon Jun 16 17:29:03 2014 UTC vs.
Revision 1.86 by jsr166, Mon Jun 16 21:04:23 2014 UTC

# Line 1680 | Line 1680 | public class CompletableFutureTest exten
1680          checkCompletedWithWrappedCFException(h1);
1681          checkCompletedWithWrappedCFException(h2);
1682          checkCompletedWithWrappedCFException(h3);
1683 +        r1.assertInvoked();
1684 +        r2.assertInvoked();
1685 +        r3.assertInvoked();
1686          checkCompletedNormally(f, v1);
1687          checkCompletedNormally(g, v2);
1688      }}
# Line 1841 | Line 1844 | public class CompletableFutureTest exten
1844          checkCompletedWithWrappedCFException(h1);
1845          checkCompletedWithWrappedCFException(h2);
1846          checkCompletedWithWrappedCFException(h3);
1847 +        r1.assertInvoked();
1848 +        r2.assertInvoked();
1849 +        r3.assertInvoked();
1850          checkCompletedNormally(f, v1);
1851          checkCompletedNormally(g, v2);
1852      }}
# Line 1851 | Line 1857 | public class CompletableFutureTest exten
1857       */
1858      public void testRunAfterBoth_normalCompletion() {
1859          for (ExecutionMode m : ExecutionMode.values())
1854        for (boolean createIncomplete : new boolean[] { true, false })
1860          for (boolean fFirst : new boolean[] { true, false })
1861          for (Integer v1 : new Integer[] { 1, null })
1862          for (Integer v2 : new Integer[] { 2, null })
1863      {
1864          final CompletableFuture<Integer> f = new CompletableFuture<>();
1865          final CompletableFuture<Integer> g = new CompletableFuture<>();
1866 <        final Noop r = new Noop(m);
1866 >        final Noop r1 = new Noop(m);
1867 >        final Noop r2 = new Noop(m);
1868 >        final Noop r3 = new Noop(m);
1869  
1870 <        assertTrue(fFirst ? f.complete(v1) : g.complete(v2));
1871 <        if (!createIncomplete)
1872 <            assertTrue(!fFirst ? f.complete(v1) : g.complete(v2));
1873 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1874 <        if (createIncomplete) {
1875 <            checkIncomplete(h);
1876 <            r.assertNotInvoked();
1877 <            assertTrue(!fFirst ? f.complete(v1) : g.complete(v2));
1878 <        }
1870 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1871 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1872 >        final Integer w1 =  fFirst ? v1 : v2;
1873 >        final Integer w2 = !fFirst ? v1 : v2;
1874 >
1875 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1876 >        assertTrue(fst.complete(w1));
1877 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1878 >        checkIncomplete(h1);
1879 >        checkIncomplete(h2);
1880 >        r1.assertNotInvoked();
1881 >        r2.assertNotInvoked();
1882 >        assertTrue(snd.complete(w2));
1883 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
1884  
1885 <        checkCompletedNormally(h, null);
1886 <        r.assertInvoked();
1885 >        checkCompletedNormally(h1, null);
1886 >        checkCompletedNormally(h2, null);
1887 >        checkCompletedNormally(h3, null);
1888 >        r1.assertInvoked();
1889 >        r2.assertInvoked();
1890 >        r3.assertInvoked();
1891          checkCompletedNormally(f, v1);
1892          checkCompletedNormally(g, v2);
1893      }}
# Line 1880 | Line 1896 | public class CompletableFutureTest exten
1896       * runAfterBoth result completes exceptionally after exceptional
1897       * completion of either source
1898       */
1899 <    public void testRunAfterBoth_exceptionalCompletion() {
1899 >    public void testRunAfterBoth_exceptionalCompletion() throws Throwable {
1900          for (ExecutionMode m : ExecutionMode.values())
1885        for (boolean createIncomplete : new boolean[] { true, false })
1901          for (boolean fFirst : new boolean[] { true, false })
1902 +        for (boolean failFirst : new boolean[] { true, false })
1903          for (Integer v1 : new Integer[] { 1, null })
1904      {
1905          final CompletableFuture<Integer> f = new CompletableFuture<>();
1906          final CompletableFuture<Integer> g = new CompletableFuture<>();
1907          final CFException ex = new CFException();
1908 <        final Noop r = new Noop(m);
1908 >        final Noop r1 = new Noop(m);
1909 >        final Noop r2 = new Noop(m);
1910 >        final Noop r3 = new Noop(m);
1911  
1912 <        assertTrue((fFirst ? f : g).complete(v1));
1913 <        if (!createIncomplete)
1914 <            assertTrue((!fFirst ? f : g).completeExceptionally(ex));
1915 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1916 <        if (createIncomplete) {
1917 <            checkIncomplete(h);
1918 <            assertTrue((!fFirst ? f : g).completeExceptionally(ex));
1919 <        }
1912 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1913 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1914 >        final Callable<Boolean> complete1 = failFirst ?
1915 >            () -> fst.completeExceptionally(ex) :
1916 >            () -> fst.complete(v1);
1917 >        final Callable<Boolean> complete2 = failFirst ?
1918 >            () -> snd.complete(v1) :
1919 >            () -> snd.completeExceptionally(ex);
1920  
1921 <        checkCompletedWithWrappedException(h, ex);
1922 <        r.assertNotInvoked();
1923 <        checkCompletedNormally(fFirst ? f : g, v1);
1924 <        checkCompletedExceptionally(!fFirst ? f : g, ex);
1921 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1922 >        assertTrue(complete1.call());
1923 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1924 >        checkIncomplete(h1);
1925 >        checkIncomplete(h2);
1926 >        assertTrue(complete2.call());
1927 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
1928 >
1929 >        checkCompletedWithWrappedException(h1, ex);
1930 >        checkCompletedWithWrappedException(h2, ex);
1931 >        checkCompletedWithWrappedException(h3, ex);
1932 >        r1.assertNotInvoked();
1933 >        r2.assertNotInvoked();
1934 >        r3.assertNotInvoked();
1935 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1936 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1937      }}
1938  
1939      /**
1940       * runAfterBoth result completes exceptionally if either source cancelled
1941       */
1942 <    public void testRunAfterBoth_sourceCancelled() {
1942 >    public void testRunAfterBoth_sourceCancelled() throws Throwable {
1943          for (ExecutionMode m : ExecutionMode.values())
1944          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1915        for (boolean createIncomplete : new boolean[] { true, false })
1945          for (boolean fFirst : new boolean[] { true, false })
1946 +        for (boolean failFirst : new boolean[] { true, false })
1947          for (Integer v1 : new Integer[] { 1, null })
1948      {
1949          final CompletableFuture<Integer> f = new CompletableFuture<>();
1950          final CompletableFuture<Integer> g = new CompletableFuture<>();
1951 <        final Noop r = new Noop(m);
1951 >        final Noop r1 = new Noop(m);
1952 >        final Noop r2 = new Noop(m);
1953 >        final Noop r3 = new Noop(m);
1954  
1955 <        assertTrue((fFirst ? f : g).complete(v1));
1956 <        if (!createIncomplete)
1957 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1958 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1959 <        if (createIncomplete) {
1960 <            checkIncomplete(h);
1961 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1962 <        }
1955 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1956 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1957 >        final Callable<Boolean> complete1 = failFirst ?
1958 >            () -> fst.cancel(mayInterruptIfRunning) :
1959 >            () -> fst.complete(v1);
1960 >        final Callable<Boolean> complete2 = failFirst ?
1961 >            () -> snd.complete(v1) :
1962 >            () -> snd.cancel(mayInterruptIfRunning);
1963  
1964 <        checkCompletedWithWrappedCancellationException(h);
1965 <        checkCancelled(!fFirst ? f : g);
1966 <        r.assertNotInvoked();
1967 <        checkCompletedNormally(fFirst ? f : g, v1);
1964 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1965 >        assertTrue(complete1.call());
1966 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1967 >        checkIncomplete(h1);
1968 >        checkIncomplete(h2);
1969 >        assertTrue(complete2.call());
1970 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
1971 >
1972 >        checkCompletedWithWrappedCancellationException(h1);
1973 >        checkCompletedWithWrappedCancellationException(h2);
1974 >        checkCompletedWithWrappedCancellationException(h3);
1975 >        r1.assertNotInvoked();
1976 >        r2.assertNotInvoked();
1977 >        r3.assertNotInvoked();
1978 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1979 >        checkCancelled(failFirst ? fst : snd);
1980      }}
1981  
1982      /**
# Line 1948 | Line 1992 | public class CompletableFutureTest exten
1992          final CompletableFuture<Integer> g = new CompletableFuture<>();
1993          final FailingRunnable r1 = new FailingRunnable(m);
1994          final FailingRunnable r2 = new FailingRunnable(m);
1995 +        final FailingRunnable r3 = new FailingRunnable(m);
1996  
1997 <        CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1998 <        assertTrue(fFirst ? f.complete(v1) : g.complete(v2));
1999 <        assertTrue(!fFirst ? f.complete(v1) : g.complete(v2));
2000 <        CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1997 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1998 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1999 >        final Integer w1 =  fFirst ? v1 : v2;
2000 >        final Integer w2 = !fFirst ? v1 : v2;
2001 >
2002 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2003 >        assertTrue(fst.complete(w1));
2004 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2005 >        assertTrue(snd.complete(w2));
2006 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2007  
2008          checkCompletedWithWrappedCFException(h1);
2009          checkCompletedWithWrappedCFException(h2);
2010 +        checkCompletedWithWrappedCFException(h3);
2011 +        r1.assertInvoked();
2012 +        r2.assertInvoked();
2013 +        r3.assertInvoked();
2014          checkCompletedNormally(f, v1);
2015          checkCompletedNormally(g, v2);
2016      }}
# Line 2867 | Line 2922 | public class CompletableFutureTest exten
2922       * when all components complete normally
2923       */
2924      public void testAllOf_normal() throws Exception {
2925 <        for (int k = 1; k < 20; ++k) {
2925 >        for (int k = 1; k < 20; k++) {
2926              CompletableFuture<Integer>[] fs
2927                  = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2928 <            for (int i = 0; i < k; ++i)
2928 >            for (int i = 0; i < k; i++)
2929                  fs[i] = new CompletableFuture<>();
2930              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
2931 <            for (int i = 0; i < k; ++i) {
2931 >            for (int i = 0; i < k; i++) {
2932                  checkIncomplete(f);
2933                  checkIncomplete(CompletableFuture.allOf(fs));
2934                  fs[i].complete(one);
# Line 2884 | Line 2939 | public class CompletableFutureTest exten
2939      }
2940  
2941      public void testAllOf_backwards() throws Exception {
2942 <        for (int k = 1; k < 20; ++k) {
2942 >        for (int k = 1; k < 20; k++) {
2943              CompletableFuture<Integer>[] fs
2944                  = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2945 <            for (int i = 0; i < k; ++i)
2945 >            for (int i = 0; i < k; i++)
2946                  fs[i] = new CompletableFuture<>();
2947              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
2948              for (int i = k - 1; i >= 0; i--) {
# Line 2904 | Line 2959 | public class CompletableFutureTest exten
2959       * anyOf(no component futures) returns an incomplete future
2960       */
2961      public void testAnyOf_empty() throws Exception {
2962 +        for (Integer v1 : new Integer[] { 1, null })
2963 +    {
2964          CompletableFuture<Object> f = CompletableFuture.anyOf();
2965          checkIncomplete(f);
2966 <    }
2966 >
2967 >        f.complete(v1);
2968 >        checkCompletedNormally(f, v1);
2969 >    }}
2970  
2971      /**
2972       * anyOf returns a future completed normally with a value when
2973       * a component future does
2974       */
2975      public void testAnyOf_normal() throws Exception {
2976 <        for (int k = 0; k < 10; ++k) {
2976 >        for (int k = 0; k < 10; k++) {
2977              CompletableFuture[] fs = new CompletableFuture[k];
2978 <            for (int i = 0; i < k; ++i)
2978 >            for (int i = 0; i < k; i++)
2979                  fs[i] = new CompletableFuture<>();
2980              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
2981              checkIncomplete(f);
2982 <            for (int i = 0; i < k; ++i) {
2983 <                fs[i].complete(one);
2984 <                checkCompletedNormally(f, one);
2985 <                checkCompletedNormally(CompletableFuture.anyOf(fs), one);
2982 >            for (int i = 0; i < k; i++) {
2983 >                fs[i].complete(i);
2984 >                checkCompletedNormally(f, 0);
2985 >                int x = (int) CompletableFuture.anyOf(fs).join();
2986 >                assertTrue(0 <= x && x <= i);
2987 >            }
2988 >        }
2989 >    }
2990 >    public void testAnyOf_normal_backwards() throws Exception {
2991 >        for (int k = 0; k < 10; k++) {
2992 >            CompletableFuture[] fs = new CompletableFuture[k];
2993 >            for (int i = 0; i < k; i++)
2994 >                fs[i] = new CompletableFuture<>();
2995 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
2996 >            checkIncomplete(f);
2997 >            for (int i = k - 1; i >= 0; i--) {
2998 >                fs[i].complete(i);
2999 >                checkCompletedNormally(f, k - 1);
3000 >                int x = (int) CompletableFuture.anyOf(fs).join();
3001 >                assertTrue(i <= x && x <= k - 1);
3002              }
3003          }
3004      }
# Line 2931 | Line 3007 | public class CompletableFutureTest exten
3007       * anyOf result completes exceptionally when any component does.
3008       */
3009      public void testAnyOf_exceptional() throws Exception {
3010 <        for (int k = 0; k < 10; ++k) {
3010 >        for (int k = 0; k < 10; k++) {
3011              CompletableFuture[] fs = new CompletableFuture[k];
3012 <            for (int i = 0; i < k; ++i)
3012 >            for (int i = 0; i < k; i++)
3013                  fs[i] = new CompletableFuture<>();
3014              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3015              checkIncomplete(f);
3016 <            for (int i = 0; i < k; ++i) {
3016 >            for (int i = 0; i < k; i++) {
3017                  fs[i].completeExceptionally(new CFException());
3018                  checkCompletedWithWrappedCFException(f);
3019                  checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
# Line 3063 | Line 3139 | public class CompletableFutureTest exten
3139          assertSame(f, f.toCompletableFuture());
3140      }
3141  
3142 < //     public void testRunAfterEither_resultDeterminedAtTimeOfCreation() {
3143 < //         for (ExecutionMode m : ExecutionMode.values())
3144 < //         for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3145 < //         for (Integer v1 : new Integer[] { 1, null })
3146 < //     {
3147 < //         final CompletableFuture<Integer> f = new CompletableFuture<>();
3148 < //         final CompletableFuture<Integer> g = new CompletableFuture<>();
3149 < //         final Noop[] rs = new Noop[2];
3150 < //         for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
3151 < //         f.complete(v1);
3152 < //         final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
3153 < //         final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
3154 < //         assertTrue(g.cancel(mayInterruptIfRunning));
3155 < //         checkCompletedNormally(h0, null);
3156 < //         checkCompletedNormally(h1, null);
3157 < //         for (Noop r : rs) r.assertInvoked();
3158 < //     }}
3142 >    //--- tests of implementation details; not part of official tck ---
3143 >
3144 >    Object resultOf(CompletableFuture<?> f) {
3145 >        try {
3146 >            java.lang.reflect.Field resultField
3147 >                = CompletableFuture.class.getDeclaredField("result");
3148 >            resultField.setAccessible(true);
3149 >            return resultField.get(f);
3150 >        } catch (Throwable t) { throw new AssertionError(t); }
3151 >    }
3152 >
3153 >    public void testExceptionPropagationReusesResultObject() {
3154 >        if (!testImplementationDetails) return;
3155 >        for (ExecutionMode m : ExecutionMode.values())
3156 >    {
3157 >        final CFException ex = new CFException();
3158 >        final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42);
3159 >        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3160 >
3161 >        List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> dependentFactories
3162 >            = new ArrayList<>();
3163 >
3164 >        dependentFactories.add((y) -> m.thenRun(y, new Noop(m)));
3165 >        dependentFactories.add((y) -> m.thenAccept(y, new NoopConsumer(m)));
3166 >        dependentFactories.add((y) -> m.thenApply(y, new IncFunction(m)));
3167 >
3168 >        dependentFactories.add((y) -> m.runAfterEither(y, incomplete, new Noop(m)));
3169 >        dependentFactories.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m)));
3170 >        dependentFactories.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m)));
3171 >
3172 >        dependentFactories.add((y) -> m.runAfterBoth(y, v42, new Noop(m)));
3173 >        dependentFactories.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
3174 >        dependentFactories.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m)));
3175 >
3176 >        dependentFactories.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {}));
3177 >
3178 >        dependentFactories.add((y) -> m.thenCompose(y, new CompletableFutureInc(m)));
3179 >
3180 >        dependentFactories.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42}));
3181 >        dependentFactories.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete}));
3182 >
3183 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3184 >                 dependentFactory : dependentFactories) {
3185 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3186 >            f.completeExceptionally(ex);
3187 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3188 >            checkCompletedWithWrappedException(src, ex);
3189 >            CompletableFuture<?> dep = dependentFactory.apply(src);
3190 >            checkCompletedWithWrappedException(dep, ex);
3191 >            assertSame(resultOf(src), resultOf(dep));
3192 >        }
3193 >
3194 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3195 >                 dependentFactory : dependentFactories) {
3196 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3197 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3198 >            CompletableFuture<?> dep = dependentFactory.apply(src);
3199 >            f.completeExceptionally(ex);
3200 >            checkCompletedWithWrappedException(src, ex);
3201 >            checkCompletedWithWrappedException(dep, ex);
3202 >            assertSame(resultOf(src), resultOf(dep));
3203 >        }
3204 >
3205 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3206 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3207 >                 dependentFactory : dependentFactories) {
3208 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3209 >            f.cancel(mayInterruptIfRunning);
3210 >            checkCancelled(f);
3211 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3212 >            checkCompletedWithWrappedCancellationException(src);
3213 >            CompletableFuture<?> dep = dependentFactory.apply(src);
3214 >            checkCompletedWithWrappedCancellationException(dep);
3215 >            assertSame(resultOf(src), resultOf(dep));
3216 >        }
3217 >
3218 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3219 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3220 >                 dependentFactory : dependentFactories) {
3221 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3222 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3223 >            CompletableFuture<?> dep = dependentFactory.apply(src);
3224 >            f.cancel(mayInterruptIfRunning);
3225 >            checkCancelled(f);
3226 >            checkCompletedWithWrappedCancellationException(src);
3227 >            checkCompletedWithWrappedCancellationException(dep);
3228 >            assertSame(resultOf(src), resultOf(dep));
3229 >        }
3230 >    }}
3231  
3232   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines