question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Regression: Testing MutableSharedFlow(extraBufferCapacity = 1) + flatMapLatest + tryEmit results in awaitItem() timeout

See original GitHub issue

The 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 of flatMapLatest
  • Test passes if replay = 1 is used instead of extraBufferCapacity = 1.
  • Test passes if runTest(UnconfinedTestDispatcher()) is used.
  • Test still fails if sharedFlow.emit(1) is used instead of sharedFlow.tryEmit(1).

I’m using Kotlin 1.7.10 and Coroutines 1.6.4.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
JakeWhartoncommented, Sep 14, 2022

Nevermind I forgot to pull trunk before testing. Your test passes on the latest. So this is a dupe of #138.

0reactions
mhernand40commented, Sep 15, 2022

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. 👍

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found