The documentation states:
In the absence of subscribers only the most recent
replayvalues are stored and the buffer overflow behavior is never triggered and has no effect. In particular, in the absence of subscribers emitter never suspends despite BufferOverflow.SUSPEND option and BufferOverflow.DROP_LATEST option does not have effect either. Essentially, the behavior in the absence of subscribers is always similar to BufferOverflow.DROP_OLDEST, but the buffer is just ofreplaysize (without anyextraBufferCapacity).
In a unit test, I need to simulate a device’s behavior. Normally, I get input data from it through a SharedFlow. In the test, I want to simulate this with a hardcoded list of simulated incoming data. However, the problem is that if I do that, all that data is emitted immediately, because the SharedFlow doesn’t suspend if there are no subscribers. This means that I can’t do this as part of a test setup:
fun testInit() {
testScope.launch {
while (simulatedInputList.hasNext()) {
simulatedSharedFlow.emit(simulatedInputList.next())
}
}
}
This loop will run immediately and go through the entire list because emit will drop all data.
What I want instead is for emit to suspend until a subscriber consumes data.
Is there a way to do this?