Erik
12/03/2020, 5:47 PMMutableSharedFlow<T>(replay = 0, extraBufferCapacity = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST)
equals 1
(in reality it's variable). I can't figure out how to get this working in a unit test: every time I emit something it is immediately collected (I use Turbine to test
the flow). I basically want to emit 2 items and verify that only the last one is collected, the oldest is dropped.@Test
fun test() = testCoroutineDispatcher.runBlockingTest {
val flow = MutableSharedFlow<Int>(
replay = 0,
extraBufferCapacity = 1,
BufferOverflow.DROP_OLDEST
)
val emissions = mutableListOf<Int>()
val subscription = flow.onEach { emissions += it }.launchIn(this)
pauseDispatcher()
flow.tryEmit(1)
flow.tryEmit(2)
resumeDispatcher()
assertEquals(2, emissions.single())
subscription.cancel()
}
@Test
fun test() = testCoroutineDispatcher.runBlockingTest {
val flow = MutableSharedFlow<Int>(
replay = 0,
extraBufferCapacity = 1,
BufferOverflow.DROP_OLDEST
)
flow.test {
pauseDispatcher()
flow.tryEmit(1)
flow.tryEmit(2)
resumeDispatcher()
assertEquals(2, expectItem())
expectNoEvents()
}
}
but that fails with:
java.lang.AssertionError:
Expected :2
Actual :1