From eb3e99ae2652ec99fdf0235ec54a342f83a2c815 Mon Sep 17 00:00:00 2001 From: Thomas Segismont Date: Wed, 11 Oct 2023 17:13:02 +0200 Subject: [PATCH] Coroutine execution always performed with dispatch if the dispatcher is bound to a DuplicatedContext (#244) Fixes #243 Signed-off-by: Thomas Segismont --- .../vertx/kotlin/coroutines/VertxCoroutine.kt | 4 ++-- .../kotlin/coroutines/VertxCoroutineTest.kt | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/vertx-lang-kotlin-coroutines/src/main/java/io/vertx/kotlin/coroutines/VertxCoroutine.kt b/vertx-lang-kotlin-coroutines/src/main/java/io/vertx/kotlin/coroutines/VertxCoroutine.kt index 493c84bb..b9de1f7f 100644 --- a/vertx-lang-kotlin-coroutines/src/main/java/io/vertx/kotlin/coroutines/VertxCoroutine.kt +++ b/vertx-lang-kotlin-coroutines/src/main/java/io/vertx/kotlin/coroutines/VertxCoroutine.kt @@ -160,8 +160,8 @@ private class ContextCoroutineDispatcher(val vertxContext: ContextInternal) : Co private val delegate = VertxCoroutineExecutor(vertxContext).asCoroutineDispatcher() override fun isDispatchNeeded(context: CoroutineContext): Boolean { - val current = ContextInternal.current()?.unwrap() - return current != vertxContext || !vertxContext.inThread() + val current = ContextInternal.current() ?: return true + return current != vertxContext && current.unwrap() != vertxContext } override fun dispatch(context: CoroutineContext, block: Runnable) { diff --git a/vertx-lang-kotlin-coroutines/src/test/kotlin/io/vertx/kotlin/coroutines/VertxCoroutineTest.kt b/vertx-lang-kotlin-coroutines/src/test/kotlin/io/vertx/kotlin/coroutines/VertxCoroutineTest.kt index a4592a28..93a1680f 100644 --- a/vertx-lang-kotlin-coroutines/src/test/kotlin/io/vertx/kotlin/coroutines/VertxCoroutineTest.kt +++ b/vertx-lang-kotlin-coroutines/src/test/kotlin/io/vertx/kotlin/coroutines/VertxCoroutineTest.kt @@ -22,6 +22,8 @@ import io.vertx.core.Vertx import io.vertx.core.http.HttpClientOptions import io.vertx.core.http.HttpMethod import io.vertx.core.http.HttpServerOptions +import io.vertx.core.http.RequestOptions +import io.vertx.core.impl.VertxInternal import io.vertx.ext.unit.TestContext import io.vertx.ext.unit.junit.RunTestOnContext import io.vertx.ext.unit.junit.VertxUnitRunner @@ -441,4 +443,22 @@ class VertxCoroutineTest { } } } + + @Test + fun `test Coroutine execution not always performed with dispatch`(testContext: TestContext) { + val latch = testContext.async() + val context = (vertx as VertxInternal).getOrCreateContext() + val duplicatedContext = context.duplicate() + val httpClient = vertx.createHttpClient() + duplicatedContext.runOnContext { + GlobalScope.launch(Vertx.currentContext().dispatcher()) { + val resp = httpClient.request(RequestOptions().setMethod(HttpMethod.GET).setAbsoluteURI("https://example.com")) + .await().apply { end().await() } + .response() + .await() + resp.body().await() + latch.complete() + } + } + } }