Regression: Testing MutableSharedFlow(extraBufferCapacity = 1) + flatMapLatest + tryEmit results in awaitItem() timeout
See original GitHub issueThe following test passes on 0.8.0
but fails on 0.9.0
due to a timeout with awaitItem()
:
@Test
fun repro() = runTest(dispatchTimeoutMs = 100L) {
val sharedFlow = MutableSharedFlow<Int>(extraBufferCapacity = 1)
sharedFlow
.flatMapLatest { value -> flowOf(value) }
.test {
sharedFlow.tryEmit(1)
assertThat(awaitItem()).isEqualTo(1)
}
}
Observations:
- Test passes when using
flatMapConcat
instead offlatMapLatest
- Test passes if
replay = 1
is used instead ofextraBufferCapacity = 1
. - Test passes if
runTest(UnconfinedTestDispatcher())
is used. - Test still fails if
sharedFlow.emit(1)
is used instead ofsharedFlow.tryEmit(1)
.
I’m using Kotlin 1.7.10
and Coroutines 1.6.4
.
Issue Analytics
- State:
- Created a year ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Regression: Testing MutableSharedFlow(extraBufferCapacity = 1) + ...
Regression : Testing MutableSharedFlow(extraBufferCapacity = 1) + flatMapLatest + tryEmit results in awaitItem() timeout.
Read more >Unexpected tryEmit behaviour · Issue #2387 - GitHub
tryEmit doesn't attempt to emit a value after first subscriber joined and returns false . Setting replay or extraBufferCapacity > 0 or replacing ......
Read more >How to unit test that a MutableSharedFlow<T>(replay=0) has ...
If you want to test with replay=1, you can try to emit before "observing/collecting", so before the job started.
Read more >Testing Kotlin flows on Android
The way you test units or modules that communicate with flow depends on whether the subject under test uses the flow as input...
Read more >Learning State & Shared Flows with Unit Tests
However, a State Flow is an alternative that is provided by the coroutines library. stateflow-diagram-1. How do we set up a StateFlow? val ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Nevermind I forgot to pull
trunk
before testing. Your test passes on the latest. So this is a dupe of #138.The snippet I pasted in the Issue Summary is a dumbed down version of a scenario one of my team project’s tests was running. I have confirmed that with
0.10.0
the test now passes. 👍