Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coroutine execution always performed with dispatch if the dispatcher is bound to a DuplicatedContext #245

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}
}
}