Stanislav Kral
02/18/2025, 8:51 AMrunTest to be finished (all child coroutines finished)? I can't seem to be able to wrap my head around why using SharedStarted.Eagerly or collecting the value inside runTest directly has any effect on that while Lazily does not.
class PreferencesActionRepository @Inject constructor(
private val scopedPreferencesService: ScopedPreferencesService,
scope: CoroutineScope,
) : ActionRepository {
// map to a stateflow so that the parsed object can be cached
override val actions =
scopedPreferencesService.watchUserRoleActions()
.map { userRolesToActionsJson ->
userRolesToActionsJson.takeIf {
it.isNotBlank()
}?.let { json ->
roleActionsMapFromJson(json)
}
}
.stateIn(
scope = scope,
started = SharingStarted.Lazily, // test succeeds with Eagerly
initialValue = hashMapOf()
)
}
// TEST, timeouts with an exception
@Test
fun `foo test`() = runTest {
val preferenceMock = mock<ScopedPreferencesService>()
val repository = PreferencesActionRepository(
scopedPreferencesService = preferenceMock,
scope = this
)
}Joffrey
02/18/2025, 8:56 AMLazily and you don't collect it at all, then no coroutine is started at all.
If you use Eagerly, a coroutine is immediately started to collect and share the upstream flow.
If you collect the state flow, even with Lazily, coroutine is started to collect and send values to your collector.Joffrey
02/18/2025, 8:57 AMbackgroundScope for this instead of this (TestScope) in your testStanislav Kral
02/18/2025, 9:04 AMbackgroundScope should be used.