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.77 by jsr166, Sat Jun 7 21:46:50 2014 UTC

# Line 317 | Line 317 | public class CompletableFutureTest exten
317       * getNumberOfDependents returns number of dependent tasks
318       */
319      public void testGetNumberOfDependents() {
320 +        for (ExecutionMode m : ExecutionMode.values())
321 +    {
322          CompletableFuture<Integer> f = new CompletableFuture<>();
323          assertEquals(0, f.getNumberOfDependents());
324 <        CompletableFuture g = f.thenRun(new Noop(ExecutionMode.DEFAULT));
324 >        final CompletableFuture<Void> g = m.thenRun(f, new Noop(m));
325          assertEquals(1, f.getNumberOfDependents());
326          assertEquals(0, g.getNumberOfDependents());
327 <        CompletableFuture h = f.thenRun(new Noop(ExecutionMode.DEFAULT));
327 >        final CompletableFuture<Void> h = m.thenRun(f, new Noop(m));
328          assertEquals(2, f.getNumberOfDependents());
329 +        assertEquals(0, h.getNumberOfDependents());
330          f.complete(1);
331          checkCompletedNormally(g, null);
332 +        checkCompletedNormally(h, null);
333          assertEquals(0, f.getNumberOfDependents());
334          assertEquals(0, g.getNumberOfDependents());
335 <    }
335 >        assertEquals(0, h.getNumberOfDependents());
336 >    }}
337  
338      /**
339       * toString indicates current completion state
# Line 346 | Line 351 | public class CompletableFutureTest exten
351          f.completeExceptionally(new IndexOutOfBoundsException());
352          assertTrue(f.toString().contains("[Completed exceptionally]"));
353  
354 <        f = new CompletableFuture<String>();
355 <        f.cancel(true);
356 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
357 <
358 <        f = new CompletableFuture<String>();
354 <        f.cancel(false);
355 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
354 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
355 >            f = new CompletableFuture<String>();
356 >            f.cancel(mayInterruptIfRunning);
357 >            assertTrue(f.toString().contains("[Completed exceptionally]"));
358 >        }
359      }
360  
361      /**
# Line 851 | Line 854 | public class CompletableFutureTest exten
854          assertEquals(0, a.get());
855      }}
856  
854
857      /**
858       * exceptionally action completes with function value on source
859       * exception
# Line 900 | Line 902 | public class CompletableFutureTest exten
902      }}
903  
904      /**
905 +     * whenComplete action executes on normal completion, propagating
906 +     * source result.
907 +     */
908 +    public void testWhenComplete_normalCompletion1() {
909 +        for (ExecutionMode m : ExecutionMode.values())
910 +        for (boolean createIncomplete : new boolean[] { true, false })
911 +        for (Integer v1 : new Integer[] { 1, null })
912 +    {
913 +        final AtomicInteger a = new AtomicInteger(0);
914 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
915 +        if (!createIncomplete) f.complete(v1);
916 +        final CompletableFuture<Integer> g = m.whenComplete
917 +            (f,
918 +             (Integer x, Throwable t) -> {
919 +                m.checkExecutionMode();
920 +                threadAssertSame(x, v1);
921 +                threadAssertNull(t);
922 +                a.getAndIncrement();
923 +            });
924 +        if (createIncomplete) f.complete(v1);
925 +
926 +        checkCompletedNormally(g, v1);
927 +        checkCompletedNormally(f, v1);
928 +        assertEquals(1, a.get());
929 +    }}
930 +
931 +    /**
932 +     * whenComplete action executes on exceptional completion, propagating
933 +     * source result.
934 +     */
935 +    public void testWhenComplete_exceptionalCompletion() {
936 +        for (ExecutionMode m : ExecutionMode.values())
937 +        for (boolean createIncomplete : new boolean[] { true, false })
938 +        for (Integer v1 : new Integer[] { 1, null })
939 +    {
940 +        final AtomicInteger a = new AtomicInteger(0);
941 +        final CFException ex = new CFException();
942 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
943 +        if (!createIncomplete) f.completeExceptionally(ex);
944 +        final CompletableFuture<Integer> g = m.whenComplete
945 +            (f,
946 +             (Integer x, Throwable t) -> {
947 +                m.checkExecutionMode();
948 +                threadAssertNull(x);
949 +                threadAssertSame(t, ex);
950 +                a.getAndIncrement();
951 +            });
952 +        if (createIncomplete) f.completeExceptionally(ex);
953 +
954 +        checkCompletedWithWrappedException(g, ex);
955 +        checkCompletedExceptionally(f, ex);
956 +        assertEquals(1, a.get());
957 +    }}
958 +
959 +    /**
960 +     * whenComplete action executes on cancelled source, propagating
961 +     * CancellationException.
962 +     */
963 +    public void testWhenComplete_sourceCancelled() {
964 +        for (ExecutionMode m : ExecutionMode.values())
965 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
966 +        for (boolean createIncomplete : new boolean[] { true, false })
967 +    {
968 +        final AtomicInteger a = new AtomicInteger(0);
969 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
970 +        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
971 +        final CompletableFuture<Integer> g = m.whenComplete
972 +            (f,
973 +             (Integer x, Throwable t) -> {
974 +                m.checkExecutionMode();
975 +                threadAssertNull(x);
976 +                threadAssertTrue(t instanceof CancellationException);
977 +                a.getAndIncrement();
978 +            });
979 +        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
980 +
981 +        checkCompletedWithWrappedCancellationException(g);
982 +        checkCancelled(f);
983 +        assertEquals(1, a.get());
984 +    }}
985 +
986 +    /**
987 +     * If a whenComplete action throws an exception when triggered by
988 +     * a normal completion, it completes exceptionally
989 +     */
990 +    public void testWhenComplete_actionFailed() {
991 +        for (boolean createIncomplete : new boolean[] { true, false })
992 +        for (ExecutionMode m : ExecutionMode.values())
993 +        for (Integer v1 : new Integer[] { 1, null })
994 +    {
995 +        final AtomicInteger a = new AtomicInteger(0);
996 +        final CFException ex = new CFException();
997 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
998 +        if (!createIncomplete) f.complete(v1);
999 +        final CompletableFuture<Integer> g = m.whenComplete
1000 +            (f,
1001 +             (Integer x, Throwable t) -> {
1002 +                m.checkExecutionMode();
1003 +                threadAssertSame(x, v1);
1004 +                threadAssertNull(t);
1005 +                a.getAndIncrement();
1006 +                throw ex;
1007 +            });
1008 +        if (createIncomplete) f.complete(v1);
1009 +
1010 +        checkCompletedWithWrappedException(g, ex);
1011 +        checkCompletedNormally(f, v1);
1012 +        assertEquals(1, a.get());
1013 +    }}
1014 +
1015 +    /**
1016 +     * If a whenComplete action throws an exception when triggered by
1017 +     * a source completion that also throws an exception, the source
1018 +     * exception takes precedence.
1019 +     */
1020 +    public void testWhenComplete_actionFailedSourceFailed() {
1021 +        for (boolean createIncomplete : new boolean[] { true, false })
1022 +        for (ExecutionMode m : ExecutionMode.values())
1023 +        for (Integer v1 : new Integer[] { 1, null })
1024 +    {
1025 +        final AtomicInteger a = new AtomicInteger(0);
1026 +        final CFException ex1 = new CFException();
1027 +        final CFException ex2 = new CFException();
1028 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1029 +
1030 +        if (!createIncomplete) f.completeExceptionally(ex1);
1031 +        final CompletableFuture<Integer> g = m.whenComplete
1032 +            (f,
1033 +             (Integer x, Throwable t) -> {
1034 +                m.checkExecutionMode();
1035 +                threadAssertSame(t, ex1);
1036 +                threadAssertNull(x);
1037 +                a.getAndIncrement();
1038 +                throw ex2;
1039 +            });
1040 +        if (createIncomplete) f.completeExceptionally(ex1);
1041 +
1042 +        checkCompletedWithWrappedException(g, ex1);
1043 +        checkCompletedExceptionally(f, ex1);
1044 +        assertEquals(1, a.get());
1045 +    }}
1046 +
1047 +    /**
1048       * handle action completes normally with function value on normal
1049       * completion of source
1050       */
# Line 1680 | Line 1825 | public class CompletableFutureTest exten
1825          final CompletableFuture<Integer> g = new CompletableFuture<>();
1826          final Noop r = new Noop(m);
1827  
1683
1828          (fFirst ? f : g).complete(v1);
1829          if (!createIncomplete)
1830              assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
# Line 2849 | Line 2993 | public class CompletableFutureTest exten
2993          assertSame(f, f.toCompletableFuture());
2994      }
2995  
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
2996   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines