-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Test passes for 'runBlocking' but fails for 'runBlockingTest' #1626
Comments
Should a test that passes using |
Most of the time (except really long timeouts). |
Hey, I'm facing the same issue on maybe a simpler scenario:
|
Another test case that fails for @Test fun test1() = runBlockingTest {
val list = flowOf(1).onStart { emit(0) }
.combine(flowOf("A")) { int, str -> "$str$int" }
.toList()
Assert.assertEquals(list, listOf("A0", "A1"))
} Interestingly when @Test fun test2() = runBlockingTest {
val list = flowOf(1)
.combine(flowOf("A")) { int, str -> "$str$int" }
.onStart { emit("A0") }
.toList()
Assert.assertEquals(list, listOf("A0", "A1"))
} |
I would say definitely not. Code which uses delays will execute differently. |
I think we can more easily illustrate the disconnect here with a different look at this example: @Test fun test1() = runBlockingTest {
val list = flowOf(1).onStart { emit(0) }
.combine(flowOf("A")) { int, str -> "$str$int" }
.toList()
Assert.assertEquals(list, listOf("A0", "A1"))
} Let's reconstruct this a bit. @Test fun test1() = runBlockingTest {
val ints = flowOf(0, 1, 2, 3)
val strs = flowOf("A", "B", "C")
val list = combine(ints, strs) { int, str -> "$str$int" }
.flowOn(TestCoroutineDispatcher())
.toList()
list shouldBe listOf("A0", "A1", "B1", "B2", "C2", "C3")
} The result:
When The order of parameters matters here. The flow of Ints is processed completely before the flow of Strings is touched. This is because the The same can be said for the tests using In both cases,
This is because you're now emitting Also, the order of your parameters in |
You're not actually using the dispatcher from When overriding the dispatcher, you'd need to use |
Hmmm, but how does that work for If I need to have the same dispatcher, then should I encapsulate all |
Yes, that's a common complaint. The typical solution is to wrap all references up into a single injectable object like so: interface DispatcherProvider {
val main: CoroutineDispatcher
val mainImmediate: CoroutineDispatcher
...
} A different solution is in the works for #1365 , where you'll be able to use You can do things to mitigate this issue, such as only specifying the dispatcher when it's really necessary (for instance immediately before doing I/O in a repository). Typically, you shouldn't need to specify the dispatcher as the main thread is typically capable of much more than we give it credit for. Also, if you're using a third party library with coroutine support for I/O, such as Room or Retrofit, they already use their own dispatchers internally, so switching before calling a suspend function in their library is just wasteful. Not to plug, but a final option would be my not-quite-complete little library DispatcherProvider, which uses the |
Are there any news or a timeline for this? Removing delays in tests via RunBlockingTest would be very helpful, but only if it works reliably without a lot of extra coding. |
Defines two test dispatchers: * StandardTestDispatcher, which, combined with runTest, gives an illusion of an event loop; * UnconfinedTestDispatcher, which is like Dispatchers.Unconfined, but skips delays. By default, StandardTestDispatcher is used due to the somewhat chaotic execution order of Dispatchers.Unconfined. TestCoroutineDispatcher is deprecated. Fixes #1626 Fixes #1742 Fixes #2082 Fixes #2102 Fixes #2405 Fixes #2462
Defines two test dispatchers: * StandardTestDispatcher, which, combined with runTest, gives an illusion of an event loop; * UnconfinedTestDispatcher, which is like Dispatchers.Unconfined, but skips delays. By default, StandardTestDispatcher is used due to the somewhat chaotic execution order of Dispatchers.Unconfined. TestCoroutineDispatcher is deprecated. Fixes #1626 Fixes #1742 Fixes #2082 Fixes #2102 Fixes #2405 Fixes #2462
Defines two test dispatchers: * StandardTestDispatcher, which, combined with runTest, gives an illusion of an event loop; * UnconfinedTestDispatcher, which is like Dispatchers.Unconfined, but skips delays. By default, StandardTestDispatcher is used due to the somewhat chaotic execution order of Dispatchers.Unconfined. TestCoroutineDispatcher is deprecated. Fixes #1626 Fixes #1742 Fixes #2082 Fixes #2102 Fixes #2405 Fixes #2462
Defines two test dispatchers: * StandardTestDispatcher, which, combined with runTest, gives an illusion of an event loop; * UnconfinedTestDispatcher, which is like Dispatchers.Unconfined, but skips delays. By default, StandardTestDispatcher is used due to the somewhat chaotic execution order of Dispatchers.Unconfined. TestCoroutineDispatcher is deprecated. Fixes #1626 Fixes #1742 Fixes #2082 Fixes #2102 Fixes #2405 Fixes #2462
This commit introduces the new version of the test module. Please see README.md and MIGRATION.md for a thorough discussion of the changes. Fixes Kotlin#1203 Fixes Kotlin#1609 Fixes Kotlin#2379 Fixes Kotlin#1749 Fixes Kotlin#1204 Fixes Kotlin#1390 Fixes Kotlin#1222 Fixes Kotlin#1395 Fixes Kotlin#1881 Fixes Kotlin#1910 Fixes Kotlin#1772 Fixes Kotlin#1626 Fixes Kotlin#1742 Fixes Kotlin#2082 Fixes Kotlin#2102 Fixes Kotlin#2405 Fixes Kotlin#2462 Co-authored-by: Vsevolod Tolstopyatov <qwwdfsad@gmail.com>
This commit introduces the new version of the test module. Please see README.md and MIGRATION.md for a thorough discussion of the changes. Fixes Kotlin#1203 Fixes Kotlin#1609 Fixes Kotlin#2379 Fixes Kotlin#1749 Fixes Kotlin#1204 Fixes Kotlin#1390 Fixes Kotlin#1222 Fixes Kotlin#1395 Fixes Kotlin#1881 Fixes Kotlin#1910 Fixes Kotlin#1772 Fixes Kotlin#1626 Fixes Kotlin#1742 Fixes Kotlin#2082 Fixes Kotlin#2102 Fixes Kotlin#2405 Fixes Kotlin#2462 Co-authored-by: Vsevolod Tolstopyatov <qwwdfsad@gmail.com>
The test below passes when
runBlocking
is used but fails withrunBlockingTest
.Events are missing from the expected result.
Kotlin:
1.3.50
.Coroutines:
1.3.2
.The text was updated successfully, but these errors were encountered: