Fix flaky migration tests (#2663)

This commit is contained in:
NGB-Was-Taken
2025-11-07 15:06:17 +05:45
committed by GitHub
parent 9f9155121c
commit 506d51a007

View File

@@ -1,9 +1,11 @@
package mihon.core.migration
import io.kotest.assertions.nondeterministic.eventually
import io.mockk.slot
import io.mockk.spyk
import io.mockk.verify
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.newSingleThreadContext
@@ -17,6 +19,7 @@ import org.junit.jupiter.api.Assertions.assertInstanceOf
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import kotlin.time.Duration.Companion.seconds
class MigratorTest {
@@ -26,7 +29,7 @@ class MigratorTest {
lateinit var migrationStrategyFactory: MigrationStrategyFactory
@BeforeEach
fun initilize() {
fun initialize() {
migrationContext = MigrationContext(false)
migrationJobFactory = spyk(MigrationJobFactory(migrationContext, CoroutineScope(Dispatchers.Main + Job())))
migrationCompletedListener = spyk<MigrationCompletedListener>(block = {})
@@ -45,7 +48,7 @@ class MigratorTest {
verify { migrationJobFactory.create(capture(migrations)) }
assertEquals(1, migrations.captured.size)
verify { migrationCompletedListener() }
eventually(2.seconds) { verify { migrationCompletedListener() } }
}
@Test
@@ -86,7 +89,7 @@ class MigratorTest {
verify { migrationJobFactory.create(capture(migrations)) }
assertEquals(2, migrations.captured.size)
verify { migrationCompletedListener() }
eventually(2.seconds) { verify { migrationCompletedListener() } }
}
@Test
@@ -114,7 +117,7 @@ class MigratorTest {
verify { migrationJobFactory.create(capture(migrations)) }
assertEquals(10, migrations.captured.size)
verify { migrationCompletedListener() }
eventually(2.seconds) { verify { migrationCompletedListener() } }
}
@Test
@@ -135,11 +138,12 @@ class MigratorTest {
verify { migrationJobFactory.create(capture(migrations)) }
assertEquals(2, migrations.captured.size)
verify { migrationCompletedListener() }
eventually(2.seconds) { verify { migrationCompletedListener() } }
}
companion object {
@OptIn(DelicateCoroutinesApi::class)
val mainThreadSurrogate = newSingleThreadContext("UI thread")
@BeforeAll