Dmitry Khalanskiy [JB]
10/18/2023, 2:52 PMkotlinx-coroutines-test
, it is recommended to avoid runCurrent
, advanceUntilIdle
, and especially `advanceTimeBy`: https://github.com/Kotlin/kotlinx.coroutines/issues/3919 Already, many tests can be written more idiomatically without them, though clearly far from all. The issue describes the plan to increase the number of tests that don't need these functions.
A request to you: the next time you reach for one of these functions, please check the "Alternatives to each manual execution control function" section of the issue to see if the proposed replacements would suit your needs; if not, leave a comment; if yes, leave a thumbs-up under one of the comments.Pablichjenkov
10/18/2023, 3:12 PMadvanceUntilIdle
. I will check the thread later.Joffrey
10/18/2023, 3:13 PM___ A ___
is, all the "real-time" stuff is already done (we already waited for the real-life second with job1.join()
), so it doesn't show the problem you're trying to discuss there I believe.
Maybe you meant to suggest a scenario where job1.join()
is not called, and instead we have advanceUntilIdle
or advanceTimeBy
.Dmitry Khalanskiy [JB]
10/18/2023, 3:17 PMA
upwards.Joffrey
10/18/2023, 3:18 PMsuspend
and wait for actual time when actual time is necessary (e.g. if there are tasks that run under NoDelaySkipping
context)?Dmitry Khalanskiy [JB]
10/18/2023, 3:36 PMJoffrey
10/18/2023, 3:38 PMsuspend
equivalents for the ones that cannot be covered by delay
or yield
(if such things are in fact needed). Correct?Dmitry Khalanskiy [JB]
10/18/2023, 3:44 PMadvanceUntilIdle
and runCurrent
, but there's a suspicion that they don't properly work even for them: namely, advanceUntilIdle
is used to wait for spawned coroutines to finish (but doesn't do quite that), and runCurrent
is used to wait for all initialization to complete (but a suspend
function could suit the use case better). See the "Alternatives to each manual execution control function" section.Joffrey
10/18/2023, 3:45 PMDmitry Khalanskiy [JB]
10/18/2023, 4:09 PMYou mean it doesn't wait for coroutines that are outside the test scheduler, right?No, we can't do anything about this. The test framework needs to learn about the coroutines somehow.
Or do you mean it doesn't wait for the coroutines to finish, but only to reach a suspension point that doesn't resume unless something else happens?Where this "something else" is outside the control of the test scheduler, yes. I think it's exactly the reason why people find
advanceUntilIdle
confusing sometimes: structured concurrency urges us to think in terms of tasks (each represented as a coroutine), but advanceUntilIdle
breaks this abstraction a little, forgetting about tasks (and the test framework's involvement) in the middle of their execution.