kotlinx.coroutines icon indicating copy to clipboard operation
kotlinx.coroutines copied to clipboard

Tracking issue for stabilization of `UnconfinedTestDispatcher`

Open JakeWharton opened this issue 2 years ago • 2 comments
trafficstars

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.

JakeWharton avatar May 11 '23 14:05 JakeWharton

Hello, just checking in a few years later on your feelings toward this. Anything specific that holds it back?

JakeWharton avatar May 11 '25 20:05 JakeWharton

Yes.

  1. One specific problem is that we have not yet reworked Dispatchers.setMain to correctly implement Dispatchers.Main.immediate. Because of this, people are using Dispatchers.setMain(UnconfinedTestDispatcher()), even though this results in weird interleavings. We need to ensure that Dispatchers.setMain(StandardTestDispatcher()) works.
  2. 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.
  3. 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.

dkhalanskyjb avatar May 12 '25 07:05 dkhalanskyjb