Konrad Biernacki
11/22/2023, 7:31 AMSharingStarted.WhileSubscribed
, and using StandardTestDispatcher
to advance the clock without triggering unsubscription is doing something unexpected.
https://github.com/Kotlin/kotlinx.coroutines/issues/3949Dmitry Khalanskiy [JB]
11/22/2023, 11:35 AMtestUnconfinedScope
and scheduledScope
use different test coroutine schedulers. So, advanceTimeBy
and runCurrent
don't affect what happens in testUnconfinedScope
at all.Dmitry Khalanskiy [JB]
11/22/2023, 11:38 AMval scheduler = TestCoroutineScheduler(); val testUnconfinedScope = CoroutineScope(UnconfinedTestDispatcher(scheduler))
• Make sure Dispatchers.setMain
executes before any other dispatchers are created. According to https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-standard-test-dispatcher.html, if Dispatchers.Main
uses a test dispatcher, all newly-created dispatchers share a scheduler with it.Dmitry Khalanskiy [JB]
11/22/2023, 11:38 AMDmitry Khalanskiy [JB]
11/22/2023, 12:19 PMDispatchers.Main
vs explicitly using a scheduler, become important, as in one case, the system recognizes "hey, that's the same dispatcher" and performs one suspension fewer, and in the other case it doesn't.Dmitry Khalanskiy [JB]
11/22/2023, 12:20 PMdelay(5.seconds - 1.milliseconds)
instead of advanceUntilBy
, and instead of runCurrent
, do delay(1.millisecond)
after that.Dmitry Khalanskiy [JB]
11/22/2023, 12:21 PMKonrad Biernacki
11/22/2023, 11:00 PMscheduledScope
to operate "slower" at the pace of the test scheduler, and I am expecting the collectors in the testUnconfinedScope
to operate "faster" to collect any emissions, without caring in particular about what any other schedulers are doing.Konrad Biernacki
11/22/2023, 11:04 PMKonrad Biernacki
11/22/2023, 11:06 PM- 1.milliseconds
may end up the recommended approach, but happy to fall back to it as it is familiar.
I'd like to use the following approach:
advanceUntilBy()
// verify an event has not yet occurred
runCurrent()
// verify an event has occurred
over the other one in almost all circumstances when testing a delay()
Konrad Biernacki
11/22/2023, 11:56 PMrunTest
.Konrad Biernacki
11/22/2023, 11:57 PMUnconfinedTestDispatcher()
with its own test schedulerDmitry Khalanskiy [JB]
11/23/2023, 8:33 AMI'd like to use the following approachIt's error-prone, and we're planning to deprecate it: https://github.com/Kotlin/kotlinx.coroutines/issues/3919 Why do you dislike using
delay
?Dmitry Khalanskiy [JB]
11/23/2023, 9:25 AMthe key is indeed providing the same test coroutine scheduler instance toNo, this just introduces the same extra suspension that using.runTest
Dispatchers.Main
in scheduledScope
does. This is not at all a solution, as it doesn't change the fundamental fact that you have a data race in your test.Dmitry Khalanskiy [JB]
11/23/2023, 9:26 AM