From 52cb285d46c44e0290db825d1221b6b05f4998df Mon Sep 17 00:00:00 2001 From: Thomas Segismont Date: Thu, 6 Feb 2020 18:19:53 +0100 Subject: [PATCH 1/2] Completed futures should operate on provided context SucceededFuture and FailedFuture, like other futures, take a context as constructor param. If not null, this context should be used to dispatch the execution of future handlers. Signed-off-by: Thomas Segismont --- .../java/io/vertx/core/impl/FailedFuture.java | 6 ++- .../io/vertx/core/impl/SucceededFuture.java | 6 ++- src/test/java/io/vertx/core/FutureTest.java | 39 ++++++++++++++++++- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/vertx/core/impl/FailedFuture.java b/src/main/java/io/vertx/core/impl/FailedFuture.java index 11e5e1f49..b243915d8 100644 --- a/src/main/java/io/vertx/core/impl/FailedFuture.java +++ b/src/main/java/io/vertx/core/impl/FailedFuture.java @@ -52,7 +52,11 @@ public class FailedFuture implements Future { @Override public Future onComplete(Handler> handler) { - handler.handle(this); + if (context != null) { + context.dispatch(this, handler); + } else { + handler.handle(this); + } return this; } diff --git a/src/main/java/io/vertx/core/impl/SucceededFuture.java b/src/main/java/io/vertx/core/impl/SucceededFuture.java index 8bd3df496..b6b9af408 100644 --- a/src/main/java/io/vertx/core/impl/SucceededFuture.java +++ b/src/main/java/io/vertx/core/impl/SucceededFuture.java @@ -45,7 +45,11 @@ class SucceededFuture implements Future { @Override public Future onComplete(Handler> handler) { - handler.handle(this); + if (context != null) { + context.dispatch(this, handler); + } else { + handler.handle(this); + } return this; } diff --git a/src/test/java/io/vertx/core/FutureTest.java b/src/test/java/io/vertx/core/FutureTest.java index ba636cf70..b0d377951 100644 --- a/src/test/java/io/vertx/core/FutureTest.java +++ b/src/test/java/io/vertx/core/FutureTest.java @@ -21,7 +21,10 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.concurrent.*; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ForkJoinPool; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; @@ -1570,4 +1573,38 @@ public class FutureTest extends VertxTestBase { await(); } + + @Test + public void testCompletedFuturesContext() { + waitFor(4); + + Thread testThread = Thread.currentThread(); + ContextInternal context = (ContextInternal) vertx.getOrCreateContext(); + + Future.succeededFuture().onSuccess(v -> { + assertSame(testThread, Thread.currentThread()); + assertNull(Vertx.currentContext()); + complete(); + }); + + context.succeededFuture().onSuccess(v -> { + assertNotSame(testThread, Thread.currentThread()); + assertSame(context, Vertx.currentContext()); + complete(); + }); + + Future.failedFuture(new Exception()).onFailure(v -> { + assertSame(testThread, Thread.currentThread()); + assertNull(Vertx.currentContext()); + complete(); + }); + + context.failedFuture(new Exception()).onFailure(v -> { + assertNotSame(testThread, Thread.currentThread()); + assertSame(context, Vertx.currentContext()); + complete(); + }); + + await(); + } } From 1b9c1647307dfa34f8ce0c5b477796299aeaf2fc Mon Sep 17 00:00:00 2001 From: Thomas Segismont Date: Sat, 8 Feb 2020 07:21:41 +0100 Subject: [PATCH 2/2] Added context thread checks Signed-off-by: Thomas Segismont --- src/test/java/io/vertx/core/FutureTest.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/test/java/io/vertx/core/FutureTest.java b/src/test/java/io/vertx/core/FutureTest.java index b0d377951..e77f8228a 100644 --- a/src/test/java/io/vertx/core/FutureTest.java +++ b/src/test/java/io/vertx/core/FutureTest.java @@ -1575,12 +1575,17 @@ public class FutureTest extends VertxTestBase { } @Test - public void testCompletedFuturesContext() { + public void testCompletedFuturesContext() throws Exception { waitFor(4); Thread testThread = Thread.currentThread(); ContextInternal context = (ContextInternal) vertx.getOrCreateContext(); + + CompletableFuture cf = new CompletableFuture<>(); + context.runOnContext(v -> cf.complete(Thread.currentThread())); + Thread contextThread = cf.get(); + Future.succeededFuture().onSuccess(v -> { assertSame(testThread, Thread.currentThread()); assertNull(Vertx.currentContext()); @@ -1590,6 +1595,7 @@ public class FutureTest extends VertxTestBase { context.succeededFuture().onSuccess(v -> { assertNotSame(testThread, Thread.currentThread()); assertSame(context, Vertx.currentContext()); + assertSame(contextThread, Thread.currentThread()); complete(); }); @@ -1602,6 +1608,7 @@ public class FutureTest extends VertxTestBase { context.failedFuture(new Exception()).onFailure(v -> { assertNotSame(testThread, Thread.currentThread()); assertSame(context, Vertx.currentContext()); + assertSame(contextThread, Thread.currentThread()); complete(); });