diff --git a/kotlinx-coroutines-core/common/test/flow/channels/ChannelFlowTest.kt b/kotlinx-coroutines-core/common/test/flow/channels/ChannelFlowTest.kt index 6e5846125d..1d7ce160f2 100644 --- a/kotlinx-coroutines-core/common/test/flow/channels/ChannelFlowTest.kt +++ b/kotlinx-coroutines-core/common/test/flow/channels/ChannelFlowTest.kt @@ -204,4 +204,22 @@ class ChannelFlowTest : TestBase() { myFlow.collect() finish(4) } + + @Test + fun testDoesntDispatchUnnecessarilyWhenCollected() = runTest { + expect(1) + val myFlow = flow { + expect(3) + yield() // In other words, testing that this will be the first suspension point in `collectLatest`. + expect(5) + } + launch(start = CoroutineStart.UNDISPATCHED) { + expect(2) + myFlow.collectLatest { + expectUnreached() + } + finish(6) + } + expect(4) + } }