diff --git a/core/src/main/java/io/smallrye/context/CompletableFutureWrapper.java b/core/src/main/java/io/smallrye/context/CompletableFutureWrapper.java index 3a398c4b..b2f84fbb 100644 --- a/core/src/main/java/io/smallrye/context/CompletableFutureWrapper.java +++ b/core/src/main/java/io/smallrye/context/CompletableFutureWrapper.java @@ -29,8 +29,7 @@ public class CompletableFutureWrapper extends CompletableFuture implements public CompletableFutureWrapper(SmallRyeThreadContext context, CompletableFuture f, Executor executor, int flags) { this.context = context; - this.f = f; - f.whenComplete((r, t) -> { + CompletableFuture whenComplete = f.whenComplete((r, t) -> { if (t != null) { if (t instanceof CompletionException) t = t.getCause(); @@ -40,6 +39,11 @@ public CompletableFutureWrapper(SmallRyeThreadContext context, CompletableFuture }); this.executor = executor; this.flags = flags; + if (!isDependent()) { + this.f = f; + } else { + this.f = whenComplete; + } } protected void checkDefaultExecutor() { diff --git a/testsuite/extra/src/test/java/io/smallrye/context/test/MiscTest.java b/testsuite/extra/src/test/java/io/smallrye/context/test/MiscTest.java index a42d0fa6..712e2a5e 100644 --- a/testsuite/extra/src/test/java/io/smallrye/context/test/MiscTest.java +++ b/testsuite/extra/src/test/java/io/smallrye/context/test/MiscTest.java @@ -17,6 +17,7 @@ import io.smallrye.context.SmallRyeContextManager; import io.smallrye.context.SmallRyeContextManagerProvider; +import io.smallrye.context.SmallRyeThreadContext; import io.smallrye.context.test.util.AbstractTest; class MiscTest extends AbstractTest { @@ -71,4 +72,26 @@ void contextCaptureDependentStageForcedCompletion() throws ExecutionException, I executorService.shutdown(); } + @Test + public void issue444() { + CompletableFuture cs = new CompletableFuture(); + + CompletableFuture waitFor1 = cs.whenComplete((result, error) -> { + assertEquals(true, cs.isDone()); + }); + + cs.complete("something"); + waitFor1.join(); + + CompletableFuture cs2 = new CompletableFuture(); + CompletableFuture csw = SmallRyeThreadContext.getCurrentThreadContextOrDefaultContexts() + .withContextCapture(cs2); + + CompletableFuture waitFor2 = csw.whenComplete((result, error) -> { + assertEquals(true, csw.isDone()); + }); + + cs2.complete("something"); + waitFor2.join(); + } }