Open
Description
When testing flows with the new api runTest I generally collect values of the flow I want to test, advanceUntilIdle, and read the latest value. This way, I ignore intermediate values that are generally not relevant, and stick to the final value.
If I do this with a flow that uses the sample
operator the test runs forever, stuck at the advanceUntilIdle()
call.
Simplified example:
private val dispatcher = StandardTestDispatcher()
@Test
fun `test ab`() = runTest(dispatcher) {
val testVar = AbTest()
val values = mutableListOf<String>()
val observer: Job = launch {
testVar.c.collect { values.add(it) }
}
advanceUntilIdle()
assertEquals("ab", values.last())
observer.cancel()
}
class AbTest {
val a = MutableStateFlow("a")
val b = flowOf("b")
val c = combine(a.sample(500),b) { a,b -> a+b }
}
Also, in other tests that did not complete with runTest, for example due to using a different dispatcher, the test would finish after 1 min with an exception. In the case of sample it does not, it runs forever.