kotlinx.coroutines
kotlinx.coroutines copied to clipboard
Tracking issue for stabilization of `UnconfinedTestDispatcher`
UnconfinedTestDispatcher didn't get the stable treatment in #3622. This is a tracking issue for its stabilization at some point in the future.
For us, this is the final remaining unstable API preventing Turbine from going 1.0. Turbine collects flows in a coroutine which was traditionally launched on the UNCONFINED dispatcher. This is to ensure we do not miss any emitted values from a flow when it performs some operation on backpressure (conflating, dropping, etc.). With the new runTest infrastructure, we were forced to take on a 'kotlinx-coroutines-test' dependency in order to conditionally use UnconfinedTestDispatcher wrapped around the caller's TestCoroutineScheduler so as to not break virtual time.
Hello, just checking in a few years later on your feelings toward this. Anything specific that holds it back?
Yes.
- One specific problem is that we have not yet reworked
Dispatchers.setMainto correctly implementDispatchers.Main.immediate. Because of this, people are usingDispatchers.setMain(UnconfinedTestDispatcher()), even though this results in weird interleavings. We need to ensure thatDispatchers.setMain(StandardTestDispatcher())works. - Another problem is this in general: https://github.com/Kotlin/kotlinx.coroutines/issues/3760 Immediate dispatchers are weird, and unconfined ones doubly so. We should discourage their usage where possible, but we would need to provide a reasonable alternative.
- The use case of checking all flow emissions is clear, but even with an
UnconfinedTestDispatcher, it's not robustly supported: https://github.com/Kotlin/kotlinx.coroutines/issues/3939 We need to either think of a semantically clear way to denote that tests may not reflect the actual execution order, or immediate dispatchers in such a way that this order becomes identical.
These three problems are related and should be solved in tandem, which will clarify the role and the future of UnconfinedTestDispatcher.