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.57 by jsr166, Mon Jun 2 22:48:50 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 506 | Line 515 | public class CompletableFutureTest exten
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 577 | Line 589 | public class CompletableFutureTest exten
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 647 | Line 662 | public class CompletableFutureTest exten
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 712 | Line 730 | public class CompletableFutureTest exten
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 998 | Line 1017 | public class CompletableFutureTest exten
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);
1015 <        checkCompletedNormally(f, one);
1016 <    }
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 2591 | 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 2605 | 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 2663 | 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