Michal Klimczak
06/13/2022, 8:50 AMfun test() = runTest(UnconfinedTestDispatcher(), dispatchTimeoutMs = 200) {
val scope = this
val counter = MutableStateFlow(0)
counter.test {
awaitItem() shouldBe 0
scope.launch {
counter.value = 1
counter.value = 0
}
awaitItem() shouldBe 1
awaitItem() shouldBe 0
}
}
This code will run fine on StandardTestDispatcher
but will time out on UnconfinedTestDispatcher
. It's not flakiness, it will time out every single time, so I assume it's a difference between the two dispatchers.Michal Klimczak
06/13/2022, 8:52 AMfun playSound(sound: Sound) {
currentlyPlayingJob?.cancel()
currentlyPlayingJob = playerScope.launch {
_state.value = Playing(sound)
realPlayer.playSound(sound.filePath)
_state.value = Stopped
}
}
and going from Stopped to Playing to Stopped again with realPlayer.playSound
being mocked (coJustRun { realPlayer.playSound(any()) }
) and 0-delay caused similar effect (works if you add even a 1ms delay between state changes)ephemient
06/13/2022, 9:17 AMMichal Klimczak
06/13/2022, 9:18 AMRobert Williams
06/13/2022, 9:29 AMPlease be aware that, like Dispatchers.Unconfined, this is a specific dispatcher with execution order guarantees that are unusual and not shared by most other dispatchers, so it can only be used reliably for testing functionality, not the specific order of actions. See Dispatchers.Unconfined for a discussion of the execution order guarantees.
Michal Klimczak
06/13/2022, 9:29 AMStandardTestDispatcher
causes it to register both valuesMichal Klimczak
06/13/2022, 9:38 AM<http://Dispatchers.IO|Dispatchers.IO>
but it works fine. If I do`runBlocking(Dispatchers.Unconfined) {` it'll also hang forever. So the unconfined dispatcher causes a coroutine to never finish - I guess my question boils down to why this is the case.Robert Williams
06/13/2022, 9:41 AMawaitItem
to be woken up between the two counter.value
callsRobert Williams
06/13/2022, 9:42 AMawaitItem
will hangMichal Klimczak
06/13/2022, 9:44 AMawaitItem()
has a default mechanism that should only wait for one second and then cancel...Michal Klimczak
06/13/2022, 9:45 AMRobert Williams
06/13/2022, 9:49 AMRobert Williams
06/13/2022, 9:50 AMtest
Michal Klimczak
06/13/2022, 9:50 AMRobert Williams
06/13/2022, 9:50 AMMichal Klimczak
06/13/2022, 9:50 AMRobert Williams
06/13/2022, 9:51 AMrunTest
now times outRobert Williams
06/13/2022, 9:51 AMMichal Klimczak
06/13/2022, 9:52 AMRobert Williams
06/13/2022, 9:58 AM