Different behaviour when using coroutines 1.6.0
See original GitHub issueAs 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:
- Created 2 years ago
- Reactions:3
- Comments:8 (2 by maintainers)
Top 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 >
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 Free
Top 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
The timeout is gone on
trunk
as it is now provided byrunTest
automatically.Thank you for your reply @mhernand40 but I see only the first and the last values in case I use
UnconfinedTestDispatcher
, not theStandardTestDispatcher
. Also it does not matter if I useDispatchers.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 byrunTest
, I create a scope manually usingJob() + Dispatchers.Main
and at the time of creating this scopeDispatchers.Main
is already substituted withUnconfinedTestDispatcher
.