-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Support injection of default context elements #2932
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
Comments
Notable: both use cases reported for this involve Question: is there a reason why https://github.com/kotest/kotest/blob/42d571cb22bae6ef60db701615b63c27130223b8/kotest-assertions/kotest-assertions-shared/src/jvmMain/kotlin/io/kotest/assertions/ErrorCollector.kt#L40-L62 is not just |
|
ServiceLoader API will also help us with tracing and other observability. We also wanted to override the default dispatcher but gave up. Since we have a separate fork, we might return to it, so it'd be great to have the flexibility in the library instead. Also we'd like to be able to bind an arbitrary coroutine to our global root job if it does not have a parent. |
CoroutineScope.newCoroutineContext
currently sets up coroutine contexts via a hard-wired mechanism:kotlinx.coroutines/kotlinx-coroutines-core/jvm/src/CoroutineContext.kt
Lines 32 to 37 in 287a931
Here is a use case where a customizable set of default context elements would help:
The Kotest assertions library has a Soft Assertions feature which collects multiple failures and finally reports them in a single exception. It uses a thread-local error-collecting context to achieve this.
ThreadLocal<T>.asContextElement
makes this work with thread-switching coroutines.However, the user must either use the Kotest framework (which sets up the coroutine context) or remember to insert a provided
errorCollectorContextElement
wherever a top-level coroutine context is created:Ideally, the Kotest assertions library would take care of such initialization, allowing the user to just use
runBlocking(Dispatchers.Unconfined) { ... }
without having to care about theerrorCollectorContextElement
.An idea would be to provide a
DefaultContextFactory
, which could be set up on the JVM viaServiceLoader
, analogous to the existingMainDispatcherFactory
.Related:
The text was updated successfully, but these errors were encountered: