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.56 by jsr166, Mon Jun 2 22:20:32 2014 UTC vs.
Revision 1.59 by jsr166, Tue Jun 3 03:54:14 2014 UTC

# Line 334 | Line 334 | public class CompletableFutureTest exten
334          return (x == null) ? null : x + 1;
335      }
336  
337 <    static final Supplier<Integer> supplyOne =
338 <        () -> Integer.valueOf(1);
339 <    static final Function<Integer, Integer> inc =
340 <        (Integer x) -> Integer.valueOf(x.intValue() + 1);
341 <    static final BiFunction<Integer, Integer, Integer> subtract =
342 <        (Integer x, Integer y) -> subtract(x, y);
337 >    static final class IntegerSupplier implements Supplier<Integer> {
338 >        final ExecutionMode m;
339 >        int invocationCount = 0;
340 >        final Integer value;
341 >        IntegerSupplier(ExecutionMode m, Integer value) {
342 >            this.m = m;
343 >            this.value = value;
344 >        }
345 >        public Integer get() {
346 >            m.checkExecutionMode();
347 >            invocationCount++;
348 >            return value;
349 >        }
350 >    }
351 >        
352      static final class IncAction implements Consumer<Integer> {
353          int invocationCount = 0;
354          Integer value;
# Line 503 | Line 512 | public class CompletableFutureTest exten
512              public void checkExecutionMode() {
513                  assertNull(ForkJoinTask.getPool());
514              }
515 +            public CompletableFuture<Void> runAsync(Runnable a) {
516 +                throw new UnsupportedOperationException();
517 +            }
518 +            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
519 +                throw new UnsupportedOperationException();
520 +            }
521              public <T> CompletableFuture<Void> thenRun
522                  (CompletableFuture<T> f, Runnable a) {
523                  return f.thenRun(a);
# Line 571 | Line 586 | public class CompletableFutureTest exten
586                  assertSame(ForkJoinPool.commonPool(),
587                             ForkJoinTask.getPool());
588              }
589 +            public CompletableFuture<Void> runAsync(Runnable a) {
590 +                return CompletableFuture.runAsync(a);
591 +            }
592 +            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
593 +                return CompletableFuture.supplyAsync(a);
594 +            }
595              public <T> CompletableFuture<Void> thenRun
596                  (CompletableFuture<T> f, Runnable a) {
597                  return f.thenRunAsync(a);
# Line 638 | Line 659 | public class CompletableFutureTest exten
659              public void checkExecutionMode() {
660                  assertTrue(ThreadExecutor.startedCurrentThread());
661              }
662 +            public CompletableFuture<Void> runAsync(Runnable a) {
663 +                return CompletableFuture.runAsync(a, new ThreadExecutor());
664 +            }
665 +            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
666 +                return CompletableFuture.supplyAsync(a, new ThreadExecutor());
667 +            }
668              public <T> CompletableFuture<Void> thenRun
669                  (CompletableFuture<T> f, Runnable a) {
670                  return f.thenRunAsync(a, new ThreadExecutor());
# Line 702 | Line 729 | public class CompletableFutureTest exten
729          };
730  
731          public abstract void checkExecutionMode();
732 +        public abstract CompletableFuture<Void> runAsync(Runnable a);
733 +        public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a);
734          public abstract <T> CompletableFuture<Void> thenRun
735              (CompletableFuture<T> f, Runnable a);
736          public abstract <T> CompletableFuture<Void> thenAccept
# Line 780 | Line 809 | public class CompletableFutureTest exten
809          if (!createIncomplete) f.completeExceptionally(ex);
810          final CompletableFuture<Integer> g = f.exceptionally
811              ((Throwable t) -> {
812 +                ExecutionMode.DEFAULT.checkExecutionMode();
813                  threadAssertSame(t, ex);
814                  a.getAndIncrement();
815                  return v1;
# Line 801 | Line 831 | public class CompletableFutureTest exten
831          if (!createIncomplete) f.completeExceptionally(ex1);
832          final CompletableFuture<Integer> g = f.exceptionally
833              ((Throwable t) -> {
834 +                ExecutionMode.DEFAULT.checkExecutionMode();
835                  threadAssertSame(t, ex1);
836                  a.getAndIncrement();
837                  throw ex2;
# Line 826 | Line 857 | public class CompletableFutureTest exten
857          final CompletableFuture<Integer> g = m.handle
858              (f,
859               (Integer x, Throwable t) -> {
860 +                m.checkExecutionMode();
861                  threadAssertSame(x, v1);
862                  threadAssertNull(t);
863                  a.getAndIncrement();
# Line 854 | Line 886 | public class CompletableFutureTest exten
886          final CompletableFuture<Integer> g = m.handle
887              (f,
888               (Integer x, Throwable t) -> {
889 +                m.checkExecutionMode();
890                  threadAssertNull(x);
891                  threadAssertSame(t, ex);
892                  a.getAndIncrement();
# Line 882 | Line 915 | public class CompletableFutureTest exten
915          final CompletableFuture<Integer> g = m.handle
916              (f,
917               (Integer x, Throwable t) -> {
918 +                m.checkExecutionMode();
919                  threadAssertNull(x);
920                  threadAssertTrue(t instanceof CancellationException);
921                  a.getAndIncrement();
# Line 909 | Line 943 | public class CompletableFutureTest exten
943          final CompletableFuture<Integer> g = m.handle
944              (f,
945               (Integer x, Throwable t) -> {
946 +                m.checkExecutionMode();
947                  threadAssertNull(x);
948                  threadAssertSame(ex1, t);
949                  a.getAndIncrement();
# Line 933 | Line 968 | public class CompletableFutureTest exten
968          final CompletableFuture<Integer> g = m.handle
969              (f,
970               (Integer x, Throwable t) -> {
971 +                m.checkExecutionMode();
972                  threadAssertSame(x, v1);
973                  threadAssertNull(t);
974                  a.getAndIncrement();
# Line 948 | Line 984 | public class CompletableFutureTest exten
984      /**
985       * runAsync completes after running Runnable
986       */
987 <    public void testRunAsync() {
988 <        Noop r = new Noop(ExecutionMode.ASYNC);
989 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
987 >    public void testRunAsync_normalCompletion() {
988 >        ExecutionMode[] executionModes = {
989 >            ExecutionMode.ASYNC,
990 >            ExecutionMode.EXECUTOR,
991 >        };
992 >        for (ExecutionMode m : executionModes)
993 >    {
994 >        final Noop r = new Noop(m);
995 >        final CompletableFuture<Void> f = m.runAsync(r);
996          assertNull(f.join());
955        assertEquals(1, r.invocationCount);
997          checkCompletedNormally(f, null);
957    }
958
959    /**
960     * runAsync with executor completes after running Runnable
961     */
962    public void testRunAsync2() {
963        Noop r = new Noop(ExecutionMode.EXECUTOR);
964        ThreadExecutor exec = new ThreadExecutor();
965        CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec);
966        assertNull(f.join());
998          assertEquals(1, r.invocationCount);
999 <        checkCompletedNormally(f, null);
969 <        assertEquals(1, exec.count.get());
970 <    }
999 >    }}
1000  
1001      /**
1002       * failing runAsync completes exceptionally after running Runnable
1003       */
1004 <    public void testRunAsync3() {
1005 <        FailingRunnable r = new FailingRunnable(ExecutionMode.ASYNC);
1006 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
1004 >    public void testRunAsync_exceptionalCompletion() {
1005 >        ExecutionMode[] executionModes = {
1006 >            ExecutionMode.ASYNC,
1007 >            ExecutionMode.EXECUTOR,
1008 >        };
1009 >        for (ExecutionMode m : executionModes)
1010 >    {
1011 >        final FailingRunnable r = new FailingRunnable(m);
1012 >        final CompletableFuture<Void> f = m.runAsync(r);
1013          checkCompletedWithWrappedCFException(f);
1014          assertEquals(1, r.invocationCount);
1015 <    }
1015 >    }}
1016  
1017      /**
1018       * supplyAsync completes with result of supplier
1019       */
1020 <    public void testSupplyAsync() {
1021 <        CompletableFuture<Integer> f;
1022 <        f = CompletableFuture.supplyAsync(supplyOne);
1023 <        assertEquals(f.join(), one);
1024 <        checkCompletedNormally(f, one);
1025 <    }
1026 <
1027 <    /**
1028 <     * supplyAsync with executor completes with result of supplier
1029 <     */
1030 <    public void testSupplyAsync2() {
1031 <        CompletableFuture<Integer> f;
1032 <        f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor());
1033 <        assertEquals(f.join(), one);
999 <        checkCompletedNormally(f, one);
1000 <    }
1020 >    public void testSupplyAsync_normalCompletion() {
1021 >        ExecutionMode[] executionModes = {
1022 >            ExecutionMode.ASYNC,
1023 >            ExecutionMode.EXECUTOR,
1024 >        };
1025 >        for (ExecutionMode m : executionModes)
1026 >        for (Integer v1 : new Integer[] { 1, null })
1027 >    {
1028 >        final IntegerSupplier r = new IntegerSupplier(m, v1);
1029 >        final CompletableFuture<Integer> f = m.supplyAsync(r);
1030 >        assertSame(v1, f.join());
1031 >        checkCompletedNormally(f, v1);
1032 >        assertEquals(1, r.invocationCount);
1033 >    }}
1034  
1035      /**
1036       * Failing supplyAsync completes exceptionally
1037       */
1038 <    public void testSupplyAsync3() {
1039 <        FailingSupplier r = new FailingSupplier(ExecutionMode.ASYNC);
1040 <        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r);
1038 >    public void testSupplyAsync_exceptionalCompletion() {
1039 >        ExecutionMode[] executionModes = {
1040 >            ExecutionMode.ASYNC,
1041 >            ExecutionMode.EXECUTOR,
1042 >        };
1043 >        for (ExecutionMode m : executionModes)
1044 >    {
1045 >        FailingSupplier r = new FailingSupplier(m);
1046 >        CompletableFuture<Integer> f = m.supplyAsync(r);
1047          checkCompletedWithWrappedCFException(f);
1048          assertEquals(1, r.invocationCount);
1049 <    }
1049 >    }}
1050  
1051      // seq completion methods
1052  
# Line 2575 | Line 2614 | public class CompletableFutureTest exten
2614       */
2615      public void testAllOf_normal() throws Exception {
2616          for (int k = 1; k < 20; ++k) {
2617 <            CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2617 >            CompletableFuture<Integer>[] fs
2618 >                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2619              for (int i = 0; i < k; ++i)
2620                  fs[i] = new CompletableFuture<>();
2621              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
# Line 2589 | Line 2629 | public class CompletableFutureTest exten
2629          }
2630      }
2631  
2632 +    public void testAllOf_backwards() throws Exception {
2633 +        for (int k = 1; k < 20; ++k) {
2634 +            CompletableFuture<Integer>[] fs
2635 +                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2636 +            for (int i = 0; i < k; ++i)
2637 +                fs[i] = new CompletableFuture<>();
2638 +            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
2639 +            for (int i = k - 1; i >= 0; i--) {
2640 +                checkIncomplete(f);
2641 +                checkIncomplete(CompletableFuture.allOf(fs));
2642 +                fs[i].complete(one);
2643 +            }
2644 +            checkCompletedNormally(f, null);
2645 +            checkCompletedNormally(CompletableFuture.allOf(fs), null);
2646 +        }
2647 +    }
2648 +
2649      /**
2650       * anyOf(no component futures) returns an incomplete future
2651       */
# Line 2647 | Line 2704 | public class CompletableFutureTest exten
2704          Runnable[] throwingActions = {
2705              () -> CompletableFuture.supplyAsync(null),
2706              () -> CompletableFuture.supplyAsync(null, exec),
2707 <            () -> CompletableFuture.supplyAsync(supplyOne, null),
2707 >            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null),
2708  
2709              () -> CompletableFuture.runAsync(null),
2710              () -> CompletableFuture.runAsync(null, exec),

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines