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.168 by jsr166, Mon Jul 18 17:19:01 2016 UTC vs.
Revision 1.175 by jsr166, Wed Sep 21 18:07:37 2016 UTC

# Line 32 | Line 32 | import java.util.concurrent.ForkJoinPool
32   import java.util.concurrent.ForkJoinTask;
33   import java.util.concurrent.RejectedExecutionException;
34   import java.util.concurrent.TimeoutException;
35 import java.util.concurrent.TimeUnit;
35   import java.util.concurrent.atomic.AtomicInteger;
36   import java.util.concurrent.atomic.AtomicReference;
37   import java.util.function.BiConsumer;
# Line 362 | Line 361 | public class CompletableFutureTest exten
361          checkCompletedNormally(f, "test");
362      }
363  
364 <    abstract class CheckedAction {
364 >    abstract static class CheckedAction {
365          int invocationCount = 0;
366          final ExecutionMode m;
367          CheckedAction(ExecutionMode m) { this.m = m; }
# Line 374 | Line 373 | public class CompletableFutureTest exten
373          void assertInvoked() { assertEquals(1, invocationCount); }
374      }
375  
376 <    abstract class CheckedIntegerAction extends CheckedAction {
376 >    abstract static class CheckedIntegerAction extends CheckedAction {
377          Integer value;
378          CheckedIntegerAction(ExecutionMode m) { super(m); }
379          void assertValue(Integer expected) {
# Line 383 | Line 382 | public class CompletableFutureTest exten
382          }
383      }
384  
385 <    class IntegerSupplier extends CheckedAction
385 >    static class IntegerSupplier extends CheckedAction
386          implements Supplier<Integer>
387      {
388          final Integer value;
# Line 402 | Line 401 | public class CompletableFutureTest exten
401          return (x == null) ? null : x + 1;
402      }
403  
404 <    class NoopConsumer extends CheckedIntegerAction
404 >    static class NoopConsumer extends CheckedIntegerAction
405          implements Consumer<Integer>
406      {
407          NoopConsumer(ExecutionMode m) { super(m); }
# Line 412 | Line 411 | public class CompletableFutureTest exten
411          }
412      }
413  
414 <    class IncFunction extends CheckedIntegerAction
414 >    static class IncFunction extends CheckedIntegerAction
415          implements Function<Integer,Integer>
416      {
417          IncFunction(ExecutionMode m) { super(m); }
# Line 430 | Line 429 | public class CompletableFutureTest exten
429              - ((y == null) ? 99 : y.intValue());
430      }
431  
432 <    class SubtractAction extends CheckedIntegerAction
432 >    static class SubtractAction extends CheckedIntegerAction
433          implements BiConsumer<Integer, Integer>
434      {
435          SubtractAction(ExecutionMode m) { super(m); }
# Line 440 | Line 439 | public class CompletableFutureTest exten
439          }
440      }
441  
442 <    class SubtractFunction extends CheckedIntegerAction
442 >    static class SubtractFunction extends CheckedIntegerAction
443          implements BiFunction<Integer, Integer, Integer>
444      {
445          SubtractFunction(ExecutionMode m) { super(m); }
# Line 450 | Line 449 | public class CompletableFutureTest exten
449          }
450      }
451  
452 <    class Noop extends CheckedAction implements Runnable {
452 >    static class Noop extends CheckedAction implements Runnable {
453          Noop(ExecutionMode m) { super(m); }
454          public void run() {
455              invoked();
456          }
457      }
458  
459 <    class FailingSupplier extends CheckedAction
459 >    static class FailingSupplier extends CheckedAction
460          implements Supplier<Integer>
461      {
462          final CFException ex;
# Line 468 | Line 467 | public class CompletableFutureTest exten
467          }
468      }
469  
470 <    class FailingConsumer extends CheckedIntegerAction
470 >    static class FailingConsumer extends CheckedIntegerAction
471          implements Consumer<Integer>
472      {
473          final CFException ex;
# Line 480 | Line 479 | public class CompletableFutureTest exten
479          }
480      }
481  
482 <    class FailingBiConsumer extends CheckedIntegerAction
482 >    static class FailingBiConsumer extends CheckedIntegerAction
483          implements BiConsumer<Integer, Integer>
484      {
485          final CFException ex;
# Line 492 | Line 491 | public class CompletableFutureTest exten
491          }
492      }
493  
494 <    class FailingFunction extends CheckedIntegerAction
494 >    static class FailingFunction extends CheckedIntegerAction
495          implements Function<Integer, Integer>
496      {
497          final CFException ex;
# Line 504 | Line 503 | public class CompletableFutureTest exten
503          }
504      }
505  
506 <    class FailingBiFunction extends CheckedIntegerAction
506 >    static class FailingBiFunction extends CheckedIntegerAction
507          implements BiFunction<Integer, Integer, Integer>
508      {
509          final CFException ex;
# Line 516 | Line 515 | public class CompletableFutureTest exten
515          }
516      }
517  
518 <    class FailingRunnable extends CheckedAction implements Runnable {
518 >    static class FailingRunnable extends CheckedAction implements Runnable {
519          final CFException ex;
520          FailingRunnable(ExecutionMode m) { super(m); ex = new CFException(); }
521          public void run() {
# Line 525 | Line 524 | public class CompletableFutureTest exten
524          }
525      }
526  
527 <    class CompletableFutureInc extends CheckedIntegerAction
527 >    static class CompletableFutureInc extends CheckedIntegerAction
528          implements Function<Integer, CompletableFuture<Integer>>
529      {
530          CompletableFutureInc(ExecutionMode m) { super(m); }
# Line 538 | Line 537 | public class CompletableFutureTest exten
537          }
538      }
539  
540 <    class FailingCompletableFutureFunction extends CheckedIntegerAction
540 >    static class FailingCompletableFutureFunction extends CheckedIntegerAction
541          implements Function<Integer, CompletableFuture<Integer>>
542      {
543          final CFException ex;
# Line 3578 | Line 3577 | public class CompletableFutureTest exten
3577       * copy returns a CompletableFuture that is completed normally,
3578       * with the same value, when source is.
3579       */
3580 <    public void testCopy() {
3580 >    public void testCopy_normalCompletion() {
3581 >        for (boolean createIncomplete : new boolean[] { true, false })
3582 >        for (Integer v1 : new Integer[] { 1, null })
3583 >    {
3584          CompletableFuture<Integer> f = new CompletableFuture<>();
3585 +        if (!createIncomplete) assertTrue(f.complete(v1));
3586          CompletableFuture<Integer> g = f.copy();
3587 <        checkIncomplete(f);
3588 <        checkIncomplete(g);
3589 <        f.complete(1);
3590 <        checkCompletedNormally(f, 1);
3591 <        checkCompletedNormally(g, 1);
3592 <    }
3587 >        if (createIncomplete) {
3588 >            checkIncomplete(f);
3589 >            checkIncomplete(g);
3590 >            assertTrue(f.complete(v1));
3591 >        }
3592 >        checkCompletedNormally(f, v1);
3593 >        checkCompletedNormally(g, v1);
3594 >    }}
3595  
3596      /**
3597       * copy returns a CompletableFuture that is completed exceptionally
3598       * when source is.
3599       */
3600 <    public void testCopy2() {
3600 >    public void testCopy_exceptionalCompletion() {
3601 >        for (boolean createIncomplete : new boolean[] { true, false })
3602 >    {
3603 >        CFException ex = new CFException();
3604          CompletableFuture<Integer> f = new CompletableFuture<>();
3605 +        if (!createIncomplete) f.completeExceptionally(ex);
3606          CompletableFuture<Integer> g = f.copy();
3607 <        checkIncomplete(f);
3608 <        checkIncomplete(g);
3609 <        CFException ex = new CFException();
3610 <        f.completeExceptionally(ex);
3607 >        if (createIncomplete) {
3608 >            checkIncomplete(f);
3609 >            checkIncomplete(g);
3610 >            f.completeExceptionally(ex);
3611 >        }
3612          checkCompletedExceptionally(f, ex);
3613          checkCompletedWithWrappedException(g, ex);
3614 +    }}
3615 +
3616 +    /**
3617 +     * Completion of a copy does not complete its source.
3618 +     */
3619 +    public void testCopy_oneWayPropagation() {
3620 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3621 +        assertTrue(f.copy().complete(1));
3622 +        assertTrue(f.copy().complete(null));
3623 +        assertTrue(f.copy().cancel(true));
3624 +        assertTrue(f.copy().cancel(false));
3625 +        assertTrue(f.copy().completeExceptionally(new CFException()));
3626 +        checkIncomplete(f);
3627      }
3628  
3629      /**
# Line 3965 | Line 3988 | public class CompletableFutureTest exten
3988              .collect(Collectors.toList());
3989  
3990          List<CompletionStage<Integer>> stages = new ArrayList<>();
3991 <        stages.add(new CompletableFuture<Integer>().minimalCompletionStage());
3991 >        CompletionStage<Integer> min =
3992 >            new CompletableFuture<Integer>().minimalCompletionStage();
3993 >        stages.add(min);
3994 >        stages.add(min.thenApply(x -> x));
3995          stages.add(CompletableFuture.completedStage(1));
3996          stages.add(CompletableFuture.failedStage(new CFException()));
3997  
# Line 4001 | Line 4027 | public class CompletableFutureTest exten
4027              throw new Error("Methods did not throw UOE: " + bugs);
4028      }
4029  
4030 +    /**
4031 +     * minimalStage.toCompletableFuture() gives mutable CompletableFuture
4032 +     */
4033 +    public void testMinimalCompletionStage_toCompletableFuture_mutable() {
4034 +        for (Integer v1 : new Integer[] { 1, null })
4035 +    {
4036 +        CompletableFuture<Integer> f = new CompletableFuture<>();
4037 +        CompletionStage minimal = f.minimalCompletionStage();
4038 +        CompletableFuture<Integer> g = minimal.toCompletableFuture();
4039 +        g.complete(v1);
4040 +        checkCompletedNormally(g, v1);
4041 +        checkIncomplete(f);
4042 +        checkIncomplete(minimal.toCompletableFuture());
4043 +    }}
4044 +
4045 +    /**
4046 +     * minimalStage.toCompletableFuture().join() awaits completion
4047 +     */
4048 +    public void testMinimalCompletionStage_toCompletableFuture_join() throws Exception {
4049 +        for (boolean createIncomplete : new boolean[] { true, false })
4050 +        for (Integer v1 : new Integer[] { 1, null })
4051 +    {
4052 +        CompletableFuture<Integer> f = new CompletableFuture<>();
4053 +        if (!createIncomplete) assertTrue(f.complete(v1));
4054 +        CompletionStage<Integer> minimal = f.minimalCompletionStage();
4055 +        if (createIncomplete) assertTrue(f.complete(v1));
4056 +        assertEquals(v1, minimal.toCompletableFuture().join());
4057 +        assertEquals(v1, minimal.toCompletableFuture().get());
4058 +        checkCompletedNormally(minimal.toCompletableFuture(), v1);
4059 +    }}
4060 +
4061 +    /**
4062 +     * Completion of a toCompletableFuture copy of a minimal stage
4063 +     * does not complete its source.
4064 +     */
4065 +    public void testMinimalCompletionStage_toCompletableFuture_oneWayPropagation() {
4066 +        CompletableFuture<Integer> f = new CompletableFuture<>();
4067 +        CompletionStage<Integer> g = f.minimalCompletionStage();
4068 +        assertTrue(g.toCompletableFuture().complete(1));
4069 +        assertTrue(g.toCompletableFuture().complete(null));
4070 +        assertTrue(g.toCompletableFuture().cancel(true));
4071 +        assertTrue(g.toCompletableFuture().cancel(false));
4072 +        assertTrue(g.toCompletableFuture().completeExceptionally(new CFException()));
4073 +        checkIncomplete(g.toCompletableFuture());
4074 +        f.complete(1);
4075 +        checkCompletedNormally(g.toCompletableFuture(), 1);
4076 +    }
4077 +
4078 +    /** Demo utility method for external reliable toCompletableFuture */
4079 +    static <T> CompletableFuture<T> toCompletableFuture(CompletionStage<T> stage) {
4080 +        CompletableFuture<T> f = new CompletableFuture<>();
4081 +        stage.handle((T t, Throwable ex) -> {
4082 +                         if (ex != null) f.completeExceptionally(ex);
4083 +                         else f.complete(t);
4084 +                         return null;
4085 +                     });
4086 +        return f;
4087 +    }
4088 +
4089 +    /** Demo utility method to join a CompletionStage */
4090 +    static <T> T join(CompletionStage<T> stage) {
4091 +        return toCompletableFuture(stage).join();
4092 +    }
4093 +
4094 +    /**
4095 +     * Joining a minimal stage "by hand" works
4096 +     */
4097 +    public void testMinimalCompletionStage_join_by_hand() {
4098 +        for (boolean createIncomplete : new boolean[] { true, false })
4099 +        for (Integer v1 : new Integer[] { 1, null })
4100 +    {
4101 +        CompletableFuture<Integer> f = new CompletableFuture<>();
4102 +        CompletionStage<Integer> minimal = f.minimalCompletionStage();
4103 +        CompletableFuture<Integer> g = new CompletableFuture<>();
4104 +        if (!createIncomplete) assertTrue(f.complete(v1));
4105 +        minimal.thenAccept((x) -> g.complete(x));
4106 +        if (createIncomplete) assertTrue(f.complete(v1));
4107 +        g.join();
4108 +        checkCompletedNormally(g, v1);
4109 +        checkCompletedNormally(f, v1);
4110 +        assertEquals(v1, join(minimal));
4111 +    }}
4112 +
4113      static class Monad {
4114          static class ZeroException extends RuntimeException {
4115              public ZeroException() { super("monadic zero"); }
# Line 4243 | Line 4352 | public class CompletableFutureTest exten
4352      }
4353  
4354      /**
4355 <     * Checks for garbage retention with anyOf.
4356 <     * Following used to fail with OOME:
4357 <     * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testAnyOfGarbageRetention tck
4355 >     * Reproduction recipe for:
4356 >     * 8160402: Garbage retention with CompletableFuture.anyOf
4357 >     * cvs update -D '2016-05-01' ./src/main/java/util/concurrent/CompletableFuture.java && ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testAnyOfGarbageRetention tck; cvs update -A
4358       */
4359      public void testAnyOfGarbageRetention() throws Throwable {
4360          for (Integer v : new Integer[] { 1, null })
# Line 4279 | Line 4388 | public class CompletableFutureTest exten
4388      /**
4389       * Checks for garbage retention when a dependent future is
4390       * cancelled and garbage-collected.
4391 +     * 8161600: Garbage retention when source CompletableFutures are never completed
4392       *
4393       * As of 2016-07, fails with OOME:
4394       * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledGarbageRetention tck

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines