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.74 by jsr166, Fri Jun 6 21:19:22 2014 UTC vs.
Revision 1.75 by jsr166, Sat Jun 7 21:14:42 2014 UTC

# Line 851 | Line 851 | public class CompletableFutureTest exten
851          assertEquals(0, a.get());
852      }}
853  
854
854      /**
855       * exceptionally action completes with function value on source
856       * exception
# Line 900 | Line 899 | public class CompletableFutureTest exten
899      }}
900  
901      /**
902 +     * whenComplete action executes on normal completion, propagating
903 +     * source result.
904 +     */
905 +    public void testWhenComplete_normalCompletion1() {
906 +        for (ExecutionMode m : ExecutionMode.values())
907 +        for (boolean createIncomplete : new boolean[] { true, false })
908 +        for (Integer v1 : new Integer[] { 1, null })
909 +    {
910 +        final AtomicInteger a = new AtomicInteger(0);
911 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
912 +        if (!createIncomplete) f.complete(v1);
913 +        final CompletableFuture<Integer> g = m.whenComplete
914 +            (f,
915 +             (Integer x, Throwable t) -> {
916 +                m.checkExecutionMode();
917 +                threadAssertSame(x, v1);
918 +                threadAssertNull(t);
919 +                a.getAndIncrement();
920 +            });
921 +        if (createIncomplete) f.complete(v1);
922 +
923 +        checkCompletedNormally(g, v1);
924 +        checkCompletedNormally(f, v1);
925 +        assertEquals(1, a.get());
926 +    }}
927 +
928 +    /**
929 +     * whenComplete action executes on exceptional completion, propagating
930 +     * source result.
931 +     */
932 +    public void testWhenComplete_exceptionalCompletion() {
933 +        for (ExecutionMode m : ExecutionMode.values())
934 +        for (boolean createIncomplete : new boolean[] { true, false })
935 +        for (Integer v1 : new Integer[] { 1, null })
936 +    {
937 +        final AtomicInteger a = new AtomicInteger(0);
938 +        final CFException ex = new CFException();
939 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
940 +        if (!createIncomplete) f.completeExceptionally(ex);
941 +        final CompletableFuture<Integer> g = m.whenComplete
942 +            (f,
943 +             (Integer x, Throwable t) -> {
944 +                m.checkExecutionMode();
945 +                threadAssertNull(x);
946 +                threadAssertSame(t, ex);
947 +                a.getAndIncrement();
948 +            });
949 +        if (createIncomplete) f.completeExceptionally(ex);
950 +
951 +        checkCompletedWithWrappedException(g, ex);
952 +        checkCompletedExceptionally(f, ex);
953 +        assertEquals(1, a.get());
954 +    }}
955 +
956 +    /**
957 +     * whenComplete action executes on cancelled source, propagating
958 +     * CancellationException.
959 +     */
960 +    public void testWhenComplete_sourceCancelled() {
961 +        for (ExecutionMode m : ExecutionMode.values())
962 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
963 +        for (boolean createIncomplete : new boolean[] { true, false })
964 +    {
965 +        final AtomicInteger a = new AtomicInteger(0);
966 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
967 +        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
968 +        final CompletableFuture<Integer> g = m.whenComplete
969 +            (f,
970 +             (Integer x, Throwable t) -> {
971 +                m.checkExecutionMode();
972 +                threadAssertNull(x);
973 +                threadAssertTrue(t instanceof CancellationException);
974 +                a.getAndIncrement();
975 +            });
976 +        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
977 +
978 +        checkCompletedWithWrappedCancellationException(g);
979 +        checkCancelled(f);
980 +        assertEquals(1, a.get());
981 +    }}
982 +
983 +    /**
984 +     * If a whenComplete action throws an exception when triggered by
985 +     * a normal completion, it completes exceptionally
986 +     */
987 +    public void testWhenComplete_actionFailed() {
988 +        for (boolean createIncomplete : new boolean[] { true, false })
989 +        for (ExecutionMode m : ExecutionMode.values())
990 +        for (Integer v1 : new Integer[] { 1, null })
991 +    {
992 +        final AtomicInteger a = new AtomicInteger(0);
993 +        final CFException ex = new CFException();
994 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
995 +        if (!createIncomplete) f.complete(v1);
996 +        final CompletableFuture<Integer> g = m.whenComplete
997 +            (f,
998 +             (Integer x, Throwable t) -> {
999 +                m.checkExecutionMode();
1000 +                threadAssertSame(x, v1);
1001 +                threadAssertNull(t);
1002 +                a.getAndIncrement();
1003 +                throw ex;
1004 +            });
1005 +        if (createIncomplete) f.complete(v1);
1006 +
1007 +        checkCompletedWithWrappedException(g, ex);
1008 +        checkCompletedNormally(f, v1);
1009 +        assertEquals(1, a.get());
1010 +    }}
1011 +
1012 +    /**
1013 +     * If a whenComplete action throws an exception when triggered by
1014 +     * a source completion that also throws an exception, the source
1015 +     * exception takes precedence.
1016 +     */
1017 +    public void testWhenComplete_actionFailedSourceFailed() {
1018 +        for (boolean createIncomplete : new boolean[] { true, false })
1019 +        for (ExecutionMode m : ExecutionMode.values())
1020 +        for (Integer v1 : new Integer[] { 1, null })
1021 +    {
1022 +        final AtomicInteger a = new AtomicInteger(0);
1023 +        final CFException ex1 = new CFException();
1024 +        final CFException ex2 = new CFException();
1025 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1026 +
1027 +        if (!createIncomplete) f.completeExceptionally(ex1);
1028 +        final CompletableFuture<Integer> g = m.whenComplete
1029 +            (f,
1030 +             (Integer x, Throwable t) -> {
1031 +                m.checkExecutionMode();
1032 +                threadAssertSame(t, ex1);
1033 +                threadAssertNull(x);
1034 +                a.getAndIncrement();
1035 +                throw ex2;
1036 +            });
1037 +        if (createIncomplete) f.completeExceptionally(ex1);
1038 +
1039 +        checkCompletedWithWrappedException(g, ex1);
1040 +        checkCompletedExceptionally(f, ex1);
1041 +        assertEquals(1, a.get());
1042 +    }}
1043 +
1044 +    /**
1045       * handle action completes normally with function value on normal
1046       * completion of source
1047       */
# Line 1680 | Line 1822 | public class CompletableFutureTest exten
1822          final CompletableFuture<Integer> g = new CompletableFuture<>();
1823          final Noop r = new Noop(m);
1824  
1683
1825          (fFirst ? f : g).complete(v1);
1826          if (!createIncomplete)
1827              assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
# Line 2849 | Line 2990 | public class CompletableFutureTest exten
2990          assertSame(f, f.toCompletableFuture());
2991      }
2992  
2852    /**
2853     * whenComplete action executes on normal completion, propagating
2854     * source result.
2855     */
2856    public void testWhenComplete_normalCompletion1() {
2857        for (ExecutionMode m : ExecutionMode.values())
2858        for (boolean createIncomplete : new boolean[] { true, false })
2859        for (Integer v1 : new Integer[] { 1, null })
2860    {
2861        final AtomicInteger a = new AtomicInteger(0);
2862        final CompletableFuture<Integer> f = new CompletableFuture<>();
2863        if (!createIncomplete) f.complete(v1);
2864        final CompletableFuture<Integer> g = m.whenComplete
2865            (f,
2866             (Integer x, Throwable t) -> {
2867                m.checkExecutionMode();
2868                threadAssertSame(x, v1);
2869                threadAssertNull(t);
2870                a.getAndIncrement();
2871            });
2872        if (createIncomplete) f.complete(v1);
2873
2874        checkCompletedNormally(g, v1);
2875        checkCompletedNormally(f, v1);
2876        assertEquals(1, a.get());
2877    }}
2878
2879    /**
2880     * whenComplete action executes on exceptional completion, propagating
2881     * source result.
2882     */
2883    public void testWhenComplete_exceptionalCompletion() {
2884        for (ExecutionMode m : ExecutionMode.values())
2885        for (boolean createIncomplete : new boolean[] { true, false })
2886        for (Integer v1 : new Integer[] { 1, null })
2887    {
2888        final AtomicInteger a = new AtomicInteger(0);
2889        final CFException ex = new CFException();
2890        final CompletableFuture<Integer> f = new CompletableFuture<>();
2891        if (!createIncomplete) f.completeExceptionally(ex);
2892        final CompletableFuture<Integer> g = m.whenComplete
2893            (f,
2894             (Integer x, Throwable t) -> {
2895                m.checkExecutionMode();
2896                threadAssertNull(x);
2897                threadAssertSame(t, ex);
2898                a.getAndIncrement();
2899            });
2900        if (createIncomplete) f.completeExceptionally(ex);
2901        checkCompletedExceptionally(f, ex);
2902        checkCompletedWithWrappedException(g, ex);
2903        assertEquals(1, a.get());
2904    }}
2905
2906    /**
2907     * whenComplete action executes on cancelled source, propagating
2908     * CancellationException.
2909     */
2910    public void testWhenComplete_sourceCancelled() {
2911        for (ExecutionMode m : ExecutionMode.values())
2912        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2913        for (boolean createIncomplete : new boolean[] { true, false })
2914    {
2915        final AtomicInteger a = new AtomicInteger(0);
2916        final CompletableFuture<Integer> f = new CompletableFuture<>();
2917        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2918        final CompletableFuture<Integer> g = m.whenComplete
2919            (f,
2920             (Integer x, Throwable t) -> {
2921                m.checkExecutionMode();
2922                threadAssertNull(x);
2923                threadAssertTrue(t instanceof CancellationException);
2924                a.getAndIncrement();
2925            });
2926        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2927
2928        checkCompletedWithWrappedCancellationException(g);
2929        checkCancelled(f);
2930        assertEquals(1, a.get());
2931    }}
2932
2933    /**
2934     * If a whenComplete action throws an exception when triggered by
2935     * a normal completion, it completes exceptionally
2936     */
2937    public void testWhenComplete_actionFailed() {
2938        for (boolean createIncomplete : new boolean[] { true, false })
2939        for (ExecutionMode m : ExecutionMode.values())
2940        for (Integer v1 : new Integer[] { 1, null })
2941    {
2942        final AtomicInteger a = new AtomicInteger(0);
2943        final CFException ex = new CFException();
2944        final CompletableFuture<Integer> f = new CompletableFuture<>();
2945        if (!createIncomplete) f.complete(v1);
2946        final CompletableFuture<Integer> g = m.whenComplete
2947            (f,
2948             (Integer x, Throwable t) -> {
2949                m.checkExecutionMode();
2950                threadAssertSame(x, v1);
2951                threadAssertNull(t);
2952                a.getAndIncrement();
2953                throw ex;
2954            });
2955        if (createIncomplete) f.complete(v1);
2956        checkCompletedNormally(f, v1);
2957        checkCompletedWithWrappedException(g, ex);
2958        assertEquals(1, a.get());
2959    }}
2960
2961    /**
2962     * If a whenComplete action throws an exception when triggered by
2963     * a source completion that also throws an exception, the source
2964     * exception takes precedence.
2965     */
2966    public void testWhenComplete_actionFailedSourceFailed() {
2967        for (boolean createIncomplete : new boolean[] { true, false })
2968        for (ExecutionMode m : ExecutionMode.values())
2969        for (Integer v1 : new Integer[] { 1, null })
2970    {
2971        final AtomicInteger a = new AtomicInteger(0);
2972        final CFException ex1 = new CFException();
2973        final CFException ex2 = new CFException();
2974        final CompletableFuture<Integer> f = new CompletableFuture<>();
2975
2976        if (!createIncomplete) f.completeExceptionally(ex1);
2977        final CompletableFuture<Integer> g = m.whenComplete
2978            (f,
2979             (Integer x, Throwable t) -> {
2980                m.checkExecutionMode();
2981                threadAssertSame(t, ex1);
2982                threadAssertNull(x);
2983                a.getAndIncrement();
2984                throw ex2;
2985            });
2986        if (createIncomplete) f.completeExceptionally(ex1);
2987
2988        checkCompletedExceptionally(f, ex1);
2989        checkCompletedWithWrappedException(g, ex1);
2990        assertEquals(1, a.get());
2991    }}
2992
2993   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines