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.157 by jsr166, Mon Jun 27 02:47:42 2016 UTC vs.
Revision 1.164 by jsr166, Sun Jul 3 18:58:15 2016 UTC

# Line 2700 | Line 2700 | public class CompletableFutureTest exten
2700          for (ExecutionMode m : ExecutionMode.values())
2701          for (Integer v1 : new Integer[] { 1, null })
2702          for (Integer v2 : new Integer[] { 2, null })
2703 +        for (boolean pushNop : new boolean[] { true, false })
2704      {
2705          final CompletableFuture<Integer> f = new CompletableFuture<>();
2706          final CompletableFuture<Integer> g = new CompletableFuture<>();
# Line 2712 | Line 2713 | public class CompletableFutureTest exten
2713          checkIncomplete(h1);
2714          rs[0].assertNotInvoked();
2715          rs[1].assertNotInvoked();
2716 +        if (pushNop) {          // ad hoc test of intra-completion interference
2717 +            m.thenRun(f, () -> {});
2718 +            m.thenRun(g, () -> {});
2719 +        }
2720          f.complete(v1);
2721          checkCompletedNormally(h0, null);
2722          checkCompletedNormally(h1, null);
# Line 3354 | Line 3359 | public class CompletableFutureTest exten
3359       * Test submissions to an executor that rejects all tasks.
3360       */
3361      public void testRejectingExecutor() {
3362 <        for (Integer v : new Integer[] { 1, null }) {
3363 <
3362 >        for (Integer v : new Integer[] { 1, null })
3363 >    {
3364          final CountingRejectingExecutor e = new CountingRejectingExecutor();
3365  
3366          final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v);
# Line 3434 | Line 3439 | public class CompletableFutureTest exten
3439              checkCompletedWithWrappedException(future, e.ex);
3440  
3441          assertEquals(futures.size(), e.count.get());
3442 <
3438 <        }
3439 <    }
3442 >    }}
3443  
3444      /**
3445       * Test submissions to an executor that rejects all tasks, but
# Line 3444 | Line 3447 | public class CompletableFutureTest exten
3447       * explicitly completed.
3448       */
3449      public void testRejectingExecutorNeverInvoked() {
3450 +        for (Integer v : new Integer[] { 1, null })
3451 +    {
3452          final CountingRejectingExecutor e = new CountingRejectingExecutor();
3453  
3449        for (Integer v : new Integer[] { 1, null }) {
3450
3454          final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v);
3455          final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3456  
# Line 3495 | Line 3498 | public class CompletableFutureTest exten
3498              checkCompletedNormally(future, null);
3499  
3500          assertEquals(0, e.count.get());
3501 <
3499 <        }
3500 <    }
3501 >    }}
3502  
3503      /**
3504       * toCompletableFuture returns this CompletableFuture.
# Line 3851 | Line 3852 | public class CompletableFutureTest exten
3852          final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42);
3853          final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3854  
3855 +        final Runnable noopRunnable = new Noop(m);
3856 +        final Consumer<Integer> noopConsumer = new NoopConsumer(m);
3857 +        final Function<Integer, Integer> incFunction = new IncFunction(m);
3858 +
3859          List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs
3860              = new ArrayList<>();
3861  
3862 <        funs.add((y) -> m.thenRun(y, new Noop(m)));
3863 <        funs.add((y) -> m.thenAccept(y, new NoopConsumer(m)));
3864 <        funs.add((y) -> m.thenApply(y, new IncFunction(m)));
3865 <
3866 <        funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m)));
3867 <        funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m)));
3868 <        funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m)));
3862 >        funs.add((y) -> m.thenRun(y, noopRunnable));
3863 >        funs.add((y) -> m.thenAccept(y, noopConsumer));
3864 >        funs.add((y) -> m.thenApply(y, incFunction));
3865 >
3866 >        funs.add((y) -> m.runAfterEither(y, incomplete, noopRunnable));
3867 >        funs.add((y) -> m.acceptEither(y, incomplete, noopConsumer));
3868 >        funs.add((y) -> m.applyToEither(y, incomplete, incFunction));
3869  
3870 <        funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m)));
3871 <        funs.add((y) -> m.runAfterBoth(v42, y, new Noop(m)));
3870 >        funs.add((y) -> m.runAfterBoth(y, v42, noopRunnable));
3871 >        funs.add((y) -> m.runAfterBoth(v42, y, noopRunnable));
3872          funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
3873          funs.add((y) -> m.thenAcceptBoth(v42, y, new SubtractAction(m)));
3874          funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m)));
# Line 3873 | Line 3878 | public class CompletableFutureTest exten
3878  
3879          funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m)));
3880  
3881 <        funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y}));
3882 <        funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42}));
3883 <        funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {v42, y}));
3884 <        funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y}));
3885 <        funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete}));
3886 <        funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {incomplete, y}));
3881 >        funs.add((y) -> CompletableFuture.allOf(y));
3882 >        funs.add((y) -> CompletableFuture.allOf(y, v42));
3883 >        funs.add((y) -> CompletableFuture.allOf(v42, y));
3884 >        funs.add((y) -> CompletableFuture.anyOf(y));
3885 >        funs.add((y) -> CompletableFuture.anyOf(y, incomplete));
3886 >        funs.add((y) -> CompletableFuture.anyOf(incomplete, y));
3887  
3888          for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3889                   fun : funs) {
3890              CompletableFuture<Integer> f = new CompletableFuture<>();
3891              f.completeExceptionally(ex);
3892 <            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3892 >            CompletableFuture<Integer> src = m.thenApply(f, incFunction);
3893              checkCompletedWithWrappedException(src, ex);
3894              CompletableFuture<?> dep = fun.apply(src);
3895              checkCompletedWithWrappedException(dep, ex);
# Line 3894 | Line 3899 | public class CompletableFutureTest exten
3899          for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3900                   fun : funs) {
3901              CompletableFuture<Integer> f = new CompletableFuture<>();
3902 <            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3902 >            CompletableFuture<Integer> src = m.thenApply(f, incFunction);
3903              CompletableFuture<?> dep = fun.apply(src);
3904              f.completeExceptionally(ex);
3905              checkCompletedWithWrappedException(src, ex);
# Line 3908 | Line 3913 | public class CompletableFutureTest exten
3913              CompletableFuture<Integer> f = new CompletableFuture<>();
3914              f.cancel(mayInterruptIfRunning);
3915              checkCancelled(f);
3916 <            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3916 >            CompletableFuture<Integer> src = m.thenApply(f, incFunction);
3917              checkCompletedWithWrappedCancellationException(src);
3918              CompletableFuture<?> dep = fun.apply(src);
3919              checkCompletedWithWrappedCancellationException(dep);
# Line 3919 | Line 3924 | public class CompletableFutureTest exten
3924          for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3925                   fun : funs) {
3926              CompletableFuture<Integer> f = new CompletableFuture<>();
3927 <            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3927 >            CompletableFuture<Integer> src = m.thenApply(f, incFunction);
3928              CompletableFuture<?> dep = fun.apply(src);
3929              f.cancel(mayInterruptIfRunning);
3930              checkCancelled(f);
# Line 4138 | Line 4143 | public class CompletableFutureTest exten
4143                                   Monad.plus(godot, Monad.unit(5L)));
4144      }
4145  
4146 +    /** Test long recursive chains of CompletableFutures with cascading completions */
4147 +    public void testRecursiveChains() throws Throwable {
4148 +        for (ExecutionMode m : ExecutionMode.values())
4149 +        for (boolean addDeadEnds : new boolean[] { true, false })
4150 +    {
4151 +        final int val = 42;
4152 +        final int n = expensiveTests ? 1_000 : 2;
4153 +        CompletableFuture<Integer> head = new CompletableFuture<>();
4154 +        CompletableFuture<Integer> tail = head;
4155 +        for (int i = 0; i < n; i++) {
4156 +            if (addDeadEnds) m.thenApply(tail, v -> v + 1);
4157 +            tail = m.thenApply(tail, v -> v + 1);
4158 +            if (addDeadEnds) m.applyToEither(tail, tail, v -> v + 1);
4159 +            tail = m.applyToEither(tail, tail, v -> v + 1);
4160 +            if (addDeadEnds) m.thenCombine(tail, tail, (v, w) -> v + 1);
4161 +            tail = m.thenCombine(tail, tail, (v, w) -> v + 1);
4162 +        }
4163 +        head.complete(val);
4164 +        assertEquals(val + 3 * n, (int) tail.join());
4165 +    }}
4166 +
4167      /**
4168       * A single CompletableFuture with many dependents.
4169       * A demo of scalability - runtime is O(n).
4170       */
4171      public void testManyDependents() throws Throwable {
4172 <        final int n = 1_000;
4172 >        final int n = expensiveTests ? 1_000_000 : 10;
4173          final CompletableFuture<Void> head = new CompletableFuture<>();
4174          final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null);
4175          final AtomicInteger count = new AtomicInteger(0);
# Line 4170 | Line 4196 | public class CompletableFutureTest exten
4196          assertEquals(5 * 3 * n, count.get());
4197      }
4198  
4199 <    /** ant -Dvmoptions=-Xmx8m -Djsr166.tckTestClass=CompletableFutureTest tck */
4200 <    public void testCoCompletionGarbage() throws Throwable {
4201 <        // final int n = 3_000_000;
4176 <        final int n = 100;
4199 >    /** ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck */
4200 >    public void testCoCompletionGarbageRetention() throws Throwable {
4201 >        final int n = expensiveTests ? 1_000_000 : 10;
4202          final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
4203          CompletableFuture<Integer> f;
4204          for (int i = 0; i < n; i++) {
# Line 4213 | Line 4238 | public class CompletableFutureTest exten
4238          }
4239      }
4240  
4241 +    /*
4242 +     * Tests below currently fail in stress mode due to memory retention.
4243 +     * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck
4244 +     */
4245 +
4246 +    /** Checks for garbage retention with anyOf. */
4247 +    public void testAnyOfGarbageRetention() throws Throwable {
4248 +        for (Integer v : new Integer[] { 1, null })
4249 +    {
4250 +        final int n = expensiveTests ? 100_000 : 10;
4251 +        CompletableFuture<Integer>[] fs
4252 +            = (CompletableFuture<Integer>[]) new CompletableFuture<?>[100];
4253 +        for (int i = 0; i < fs.length; i++)
4254 +            fs[i] = new CompletableFuture<>();
4255 +        fs[fs.length - 1].complete(v);
4256 +        for (int i = 0; i < n; i++)
4257 +            checkCompletedNormally(CompletableFuture.anyOf(fs), v);
4258 +    }}
4259 +
4260 +    /** Checks for garbage retention with allOf. */
4261 +    public void testCancelledAllOfGarbageRetention() throws Throwable {
4262 +        final int n = expensiveTests ? 100_000 : 10;
4263 +        CompletableFuture<Integer>[] fs
4264 +            = (CompletableFuture<Integer>[]) new CompletableFuture<?>[100];
4265 +        for (int i = 0; i < fs.length; i++)
4266 +            fs[i] = new CompletableFuture<>();
4267 +        for (int i = 0; i < n; i++)
4268 +            assertTrue(CompletableFuture.allOf(fs).cancel(false));
4269 +    }
4270 +
4271   //     static <U> U join(CompletionStage<U> stage) {
4272   //         CompletableFuture<U> f = new CompletableFuture<>();
4273   //         stage.whenComplete((v, ex) -> {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines