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.72 by jsr166, Fri Jun 6 21:10:34 2014 UTC vs.
Revision 1.76 by jsr166, Sat Jun 7 21:45:13 2014 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines