@@ -38,12 +38,14 @@ import com.ichi2.anki.libanki.getNotetype
3838import com.ichi2.anki.libanki.testutils.ext.addNote
3939import kotlinx.coroutines.Dispatchers
4040import kotlinx.coroutines.test.StandardTestDispatcher
41+ import kotlinx.coroutines.test.TestCoroutineScheduler
4142import kotlinx.coroutines.test.TestDispatcher
4243import kotlinx.coroutines.test.TestScope
4344import kotlinx.coroutines.test.UnconfinedTestDispatcher
4445import kotlinx.coroutines.test.setMain
4546import net.ankiweb.rsdroid.exceptions.BackendDeckIsFilteredException
4647import timber.log.Timber
48+ import kotlin.coroutines.ContinuationInterceptor
4749import kotlin.coroutines.CoroutineContext
4850import kotlin.coroutines.EmptyCoroutineContext
4951import kotlin.time.Duration
@@ -396,12 +398,18 @@ interface AnkiTest {
396398 times : Int = 1,
397399 testBody : suspend TestScope .() -> Unit ,
398400 ) {
399- val dispatcher = UnconfinedTestDispatcher ()
401+ // Use a unified scheduler on `Dispatchers.Main` and runTest uses, so
402+ // advanceUntilIdle()/runCurrent() handle coroutines launched on `Main`.
403+ val scheduler =
404+ (context[ContinuationInterceptor ] as ? TestDispatcher )?.scheduler
405+ ? : TestCoroutineScheduler ()
406+ val dispatcher = UnconfinedTestDispatcher (scheduler)
400407 Dispatchers .setMain(dispatcher)
401408 setupTestDispatcher(dispatcher)
409+
402410 repeat(times) {
403411 if (times != 1 ) Timber .d(" ------ Executing test $it /$times ------" )
404- kotlinx.coroutines.test.runTest(context, dispatchTimeout) {
412+ kotlinx.coroutines.test.runTest(context + scheduler , dispatchTimeout) {
405413 runTestInner(testBody)
406414 }
407415 }
0 commit comments