I have the following test
@Test
fun combineUnendingFlows() = runBlockingTest {
val source1 = flow {
emit("a")
emit("b")
suspendCancellableCoroutine { /* Never complete. */ }
}
val source2 = flowOf(1, 2, 3)
val combinations = mutableListOf<String>()
combine(source1, source2) { first, second -> "$first$second" }
.onEach(::println)
.onEach(combinations::add)
.launchIn(this)
advanceUntilIdle()
assertThat(combinations).containsExactly("a1", "b1", "b2", "b3")
}
The assertion succeeds, however the test fails with the exception:
kotlinx.coroutines.test.UncompletedCoroutinesError: Test finished with active jobs
I know this is contrived, and we could easily make this pass by ensuring that source1
completes, but I'm wondering why it fails? Is runBlockingTest
the wrong approach for testing never-ending flows?
(This is with coroutines 1.4.0
).