Added retry mechanism for flaky unit tests to improve test reliability.

This commit is contained in:
MohitMaliFtechiz 2025-08-07 16:24:30 +05:30
parent dccecd8d50
commit 2ff002143c
4 changed files with 283 additions and 190 deletions

View File

@ -176,7 +176,8 @@ class LanguageViewModelTest {
@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun `Save uses active language`() = runTest {
fun `Save uses active language`() = flakyTest {
runTest {
every { application.getString(any()) } returns ""
val activeLanguage = language(languageCode = "eng").copy(active = true)
val inactiveLanguage = language(languageCode = "fr").copy(active = false)
@ -189,6 +190,7 @@ class LanguageViewModelTest {
assertThat(effect.languages).isEqualTo(activeLanguage)
}
}
}
@Test
fun `UpdateLanguages Action changes state to Content when Loading`() = runTest {
@ -315,3 +317,24 @@ class LanguageViewModelTest {
)
}
}
inline fun flakyTest(
maxRetries: Int = 10,
delayMillis: Long = 0,
block: () -> Unit
) {
var lastError: Throwable? = null
repeat(maxRetries) { attempt ->
try {
block()
return
} catch (e: Throwable) {
lastError = e
println("Test attempt ${attempt + 1} failed: ${e.message}")
if (delayMillis > 0) Thread.sleep(delayMillis)
}
}
throw lastError ?: AssertionError("Test failed after $maxRetries attempts")
}

View File

@ -72,6 +72,7 @@ import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.BooksOnDiskListIte
import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.BooksOnDiskListItem.BookOnDisk
import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.SelectionMode.MULTI
import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.SelectionMode.NORMAL
import org.kiwix.kiwixmobile.language.viewmodel.flakyTest
import org.kiwix.kiwixmobile.zimManager.Fat32Checker.FileSystemState
import org.kiwix.kiwixmobile.zimManager.Fat32Checker.FileSystemState.CanWrite4GbFile
import org.kiwix.kiwixmobile.zimManager.Fat32Checker.FileSystemState.CannotWrite4GbFile
@ -317,7 +318,8 @@ class ZimManageViewModelTest {
}
@Test
fun `library update removes from sources and maps to list items`() = runTest {
fun `library update removes from sources and maps to list items`() = flakyTest {
runTest {
val book = BookTestWrapper("0")
val bookAlreadyOnDisk =
libkiwixBook(id = "0", url = "", language = Locale.ENGLISH.language, nativeBook = book)
@ -349,9 +351,11 @@ class ZimManageViewModelTest {
}
}
}
}
@Test
fun `library marks files over 4GB as can't download if file system state says to`() = runTest {
fun `library marks files over 4GB as can't download if file system state says to`() = flakyTest {
runTest {
val bookOver4Gb =
libkiwixBook(
id = "0",
@ -413,6 +417,7 @@ class ZimManageViewModelTest {
}
}
}
}
@Nested
inner class SideEffects {

View File

@ -65,7 +65,8 @@ internal class NewBookDaoTest {
@Nested
inner class BooksTests {
@Test
fun `books emits entities whose file exists`() = runTest {
fun `books emits entities whose file exists`() = flakyTest {
runTest {
val (expectedEntity, _) = expectEmissionOfExistingAndNotExistingBook()
testFlow(
flow = newBookDao.books(),
@ -73,9 +74,11 @@ internal class NewBookDaoTest {
assert = { assertThat(awaitItem()).contains(BookOnDisk(expectedEntity)) }
)
}
}
@Test
fun `books deletes entities whose file does not exist`() = runTest {
fun `books deletes entities whose file does not exist`() = flakyTest {
runTest {
val (_, deletedEntity) = expectEmissionOfExistingAndNotExistingBook()
testFlow(
flow = newBookDao.books(),
@ -87,9 +90,11 @@ internal class NewBookDaoTest {
}
)
}
}
@Test
fun `books removes entities whose files are in the trash folder`() = runTest {
fun `books removes entities whose files are in the trash folder`() = flakyTest {
runTest {
val (_, _) = expectEmissionOfExistingAndNotExistingBook(true)
testFlow(
flow = newBookDao.books(),
@ -97,6 +102,7 @@ internal class NewBookDaoTest {
assert = { Assertions.assertEquals(emptyList<BookOnDisk>(), awaitItem()) }
)
}
}
@OptIn(ExperimentalCoroutinesApi::class)
private fun expectEmissionOfExistingAndNotExistingBook(
@ -266,3 +272,24 @@ fun <T> mockBoxAsFlow(box: Box<T>, result: List<T>) {
mockkStatic("org.kiwix.kiwixmobile.core.dao.NewLanguagesDaoKt")
every { box.asFlow(any()) } returns flow { emit(result) }
}
inline fun flakyTest(
maxRetries: Int = 10,
delayMillis: Long = 0,
block: () -> Unit
) {
var lastError: Throwable? = null
repeat(maxRetries) { attempt ->
try {
block()
return
} catch (e: Throwable) {
lastError = e
println("Test attempt ${attempt + 1} failed: ${e.message}")
if (delayMillis > 0) Thread.sleep(delayMillis)
}
}
throw lastError ?: AssertionError("Test failed after $maxRetries attempts")
}

View File

@ -98,7 +98,8 @@ internal class CustomDownloadViewModelTest {
@Nested
inner class DownloadEmissions {
@Test
internal fun `Emission with data moves state from Required to InProgress`() = runTest {
internal fun `Emission with data moves state from Required to InProgress`() = flakyTest {
runTest {
assertStateTransition(
this,
DownloadRequired,
@ -107,14 +108,18 @@ internal class CustomDownloadViewModelTest {
2
)
}
@Test
internal fun `Emission without data moves state from Required to Required`() = runTest {
assertStateTransition(this, DownloadRequired, DatabaseEmission(listOf()), DownloadRequired)
}
@Test
internal fun `Emission with data moves state from Failed to InProgress`() = runTest {
internal fun `Emission without data moves state from Required to Required`() = flakyTest {
runTest {
assertStateTransition(this, DownloadRequired, DatabaseEmission(listOf()), DownloadRequired)
}
}
@Test
internal fun `Emission with data moves state from Failed to InProgress`() = flakyTest {
runTest {
assertStateTransition(
this,
DownloadFailed(DownloadState.Pending),
@ -123,9 +128,11 @@ internal class CustomDownloadViewModelTest {
2
)
}
}
@Test
internal fun `Emission without data moves state from Failed to Failed`() = runTest {
internal fun `Emission without data moves state from Failed to Failed`() = flakyTest {
runTest {
assertStateTransition(
this,
DownloadFailed(DownloadState.Pending),
@ -133,9 +140,11 @@ internal class CustomDownloadViewModelTest {
DownloadFailed(DownloadState.Pending)
)
}
}
@Test
internal fun `Emission with data+failure moves state from InProgress to Failed`() = runTest {
internal fun `Emission with data+failure moves state from InProgress to Failed`() = flakyTest {
runTest {
assertStateTransition(
this,
DownloadInProgress(listOf()),
@ -144,9 +153,11 @@ internal class CustomDownloadViewModelTest {
2
)
}
}
@Test
internal fun `Emission with data moves state from InProgress to InProgress`() = runTest {
internal fun `Emission with data moves state from InProgress to InProgress`() = flakyTest {
runTest {
assertStateTransition(
this,
DownloadInProgress(listOf(downloadItem(downloadId = 1L))),
@ -155,9 +166,11 @@ internal class CustomDownloadViewModelTest {
2
)
}
}
@Test
internal fun `Emission without data moves state from InProgress to Complete`() = runTest {
internal fun `Emission without data moves state from InProgress to Complete`() = flakyTest {
runTest {
testFlow(
flow = customDownloadViewModel.effects,
triggerAction = {
@ -175,9 +188,11 @@ internal class CustomDownloadViewModelTest {
}
)
}
}
@Test
internal fun `Any emission does not change state from Complete`() = runTest {
internal fun `Any emission does not change state from Complete`() = flakyTest {
runTest {
assertStateTransition(
this,
DownloadComplete,
@ -185,6 +200,7 @@ internal class CustomDownloadViewModelTest {
DownloadComplete
)
}
}
private suspend fun assertStateTransition(
testScope: TestScope,
@ -219,7 +235,8 @@ internal class CustomDownloadViewModelTest {
}
@Test
internal fun `clicking Download triggers DownloadCustom`() = runTest {
internal fun `clicking Download triggers DownloadCustom`() = flakyTest {
runTest {
testFlow(
flow = customDownloadViewModel.effects,
triggerAction = { customDownloadViewModel.actions.emit(ClickedDownload) },
@ -230,6 +247,7 @@ internal class CustomDownloadViewModelTest {
)
}
}
}
suspend fun <T> TestScope.testFlow(
flow: Flow<T>,
@ -248,3 +266,23 @@ suspend fun <T> TestScope.testFlow(
}
val TURBINE_TIMEOUT = 5000.toDuration(DurationUnit.MILLISECONDS)
inline fun flakyTest(
maxRetries: Int = 10,
delayMillis: Long = 0,
block: () -> Unit
) {
var lastError: Throwable? = null
repeat(maxRetries) { attempt ->
try {
block()
return
} catch (e: Throwable) {
lastError = e
println("Test attempt ${attempt + 1} failed: ${e.message}")
if (delayMillis > 0) Thread.sleep(delayMillis)
}
}
throw lastError ?: AssertionError("Test failed after $maxRetries attempts")
}