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.

Different behaviour when using coroutines 1.6.0

See original GitHub issue

As an example I would like to suggest following test:

private val stateFlow = MutableStateFlow(0)
private val flow: Flow<Int> = stateFlow

@Test
fun `turbine test`() = runBlockingTest {
    Dispatchers.setMain(TestCoroutineDispatcher())

    val scope = CoroutineScope(Job() + Dispatchers.Main)

    flow.test {
        flowOf(1, 2, 3)
            .onEach { stateFlow.value = it }
            .launchIn(scope)

        val events = cancelAndConsumeRemainingEvents()
    }

    Dispatchers.resetMain()
}

When using coroutines 1.5.2, as expected events variable will be a list with size of 4 which contains integers wrapped in Item class: [Item(0), Item(1), Item(2), Item(3)]

But when using coroutines 1.6.0 like so:

private val stateFlow = MutableStateFlow(0)
private val flow: Flow<Int> = stateFlow

@Test
fun `turbine test`() = runTest {
    Dispatchers.setMain(UnconfinedTestDispatcher())

    val scope = CoroutineScope(Job() + Dispatchers.Main)

    flow.test {
        flowOf(1, 2, 3)
            .onEach { stateFlow.value = it }
            .launchIn(scope)

        val events = cancelAndConsumeRemainingEvents()
    }

    Dispatchers.resetMain()
}

events will contain only the first and last items: [Item(0), Item(3)].

Also if I use StandardTestDispatcher instead of UnconfinedTestDispatcher, events will contain only the first item: [Item(0)].

Is it expected behaviour? Do I need to adjust my test somehow to get the same results as for coroutines version 1.5.2?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
JakeWhartoncommented, Apr 29, 2022

The timeout is gone on trunk as it is now provided by runTest automatically.

1reaction
alterpiecommented, Mar 12, 2022

Thank you for your reply @mhernand40 but I see only the first and the last values in case I use UnconfinedTestDispatcher, not the StandardTestDispatcher. Also it does not matter if I use Dispatchers.setMain inside the test or using @Before function - the result is the same, I receive only the first and the last emission. You can see that I don’t use scope provided by runTest, I create a scope manually using Job() + Dispatchers.Main and at the time of creating this scope Dispatchers.Main is already substituted with UnconfinedTestDispatcher.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing Coroutines — Update 1.6.0 | by Ralf Stuckert - Medium
If this behavior was unwanted, you could change it by pausing and resuming the dispatcher. This was meant to be comfortable, but was...
Read more >
Introducing kotlinx.coroutines 1.6.0 | The Kotlin Blog
Introducing kotlinx.coroutines 1.6.0 · A new API and multiplatform support for kotlinx-coroutines-test introduce a common solution for writing ...
Read more >
Coroutine 1.6.0 ANR on Android 11 · Issue #3113 - GitHub
In an activity in onCreate I have a simple runBlocking (so on main thread ui) for a call that is nearly instant. That...
Read more >
Weird behaviour with kotlin/kotlin-coroutines and list of result ...
I'm working with kotlin/kotlin-coroutines and facing a weird behaviour with a List of kotlin.Result. Here is my code snippet with example ...
Read more >
What's new in Kotlin Coroutines 1.6.0 - YouTube
In this episode, Anton (https://twitter.com/antonarhipov) talks about the highlights of Kotlin Coroutines 1.6.0 :0:00 Intro0:14 TL;DR0:58 New ...
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