Skip to content

Commit 634e330

Browse files
committed
fix(tests): unify schedulers
This ensures that `advanceUntilIdle`/`runCurrent` Note: this does not impact `advanceRobolectricLooper` Assisted-by: Claude Opus 4.7
1 parent a24fe43 commit 634e330

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

  • libanki/src/testFixtures/java/com/ichi2/anki/libanki/testutils

libanki/src/testFixtures/java/com/ichi2/anki/libanki/testutils/AnkiTest.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@ import com.ichi2.anki.libanki.getNotetype
3838
import com.ichi2.anki.libanki.testutils.ext.addNote
3939
import kotlinx.coroutines.Dispatchers
4040
import kotlinx.coroutines.test.StandardTestDispatcher
41+
import kotlinx.coroutines.test.TestCoroutineScheduler
4142
import kotlinx.coroutines.test.TestDispatcher
4243
import kotlinx.coroutines.test.TestScope
4344
import kotlinx.coroutines.test.UnconfinedTestDispatcher
4445
import kotlinx.coroutines.test.setMain
4546
import net.ankiweb.rsdroid.exceptions.BackendDeckIsFilteredException
4647
import timber.log.Timber
48+
import kotlin.coroutines.ContinuationInterceptor
4749
import kotlin.coroutines.CoroutineContext
4850
import kotlin.coroutines.EmptyCoroutineContext
4951
import 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

Comments
 (0)