From b488ce5727704fac2349238d5a30a1bf4477863f Mon Sep 17 00:00:00 2001 From: Natasha Murashkina Date: Fri, 25 Jul 2025 15:15:38 +0100 Subject: [PATCH 1/2] add test --- .../test/flow/channels/ChannelFlowTest.kt | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/kotlinx-coroutines-core/common/test/flow/channels/ChannelFlowTest.kt b/kotlinx-coroutines-core/common/test/flow/channels/ChannelFlowTest.kt index 6e5846125d..26c596da07 100644 --- a/kotlinx-coroutines-core/common/test/flow/channels/ChannelFlowTest.kt +++ b/kotlinx-coroutines-core/common/test/flow/channels/ChannelFlowTest.kt @@ -204,4 +204,25 @@ class ChannelFlowTest : TestBase() { myFlow.collect() finish(4) } + + @Test + fun testDoesntDispatchWhenUnnecessarilyWhenCollected() = runTest { + expect(1) + var subscribed = false + val myFlow = flow { + expect(3) + subscribed = true + yield() + expect(5) + } + launch(start = CoroutineStart.UNDISPATCHED) { + expect(2) + myFlow.collectLatest { + expectUnreached() + } + finish(6) + } + expect(4) + assertTrue(subscribed) + } } From 8596d0cda3c11502a7f163a8e0599e5925f98963 Mon Sep 17 00:00:00 2001 From: Natasha Murashkina Date: Fri, 25 Jul 2025 15:35:39 +0100 Subject: [PATCH 2/2] remove subscribed assert + reword --- .../common/test/flow/channels/ChannelFlowTest.kt | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/kotlinx-coroutines-core/common/test/flow/channels/ChannelFlowTest.kt b/kotlinx-coroutines-core/common/test/flow/channels/ChannelFlowTest.kt index 26c596da07..1d7ce160f2 100644 --- a/kotlinx-coroutines-core/common/test/flow/channels/ChannelFlowTest.kt +++ b/kotlinx-coroutines-core/common/test/flow/channels/ChannelFlowTest.kt @@ -206,13 +206,11 @@ class ChannelFlowTest : TestBase() { } @Test - fun testDoesntDispatchWhenUnnecessarilyWhenCollected() = runTest { + fun testDoesntDispatchUnnecessarilyWhenCollected() = runTest { expect(1) - var subscribed = false val myFlow = flow { expect(3) - subscribed = true - yield() + yield() // In other words, testing that this will be the first suspension point in `collectLatest`. expect(5) } launch(start = CoroutineStart.UNDISPATCHED) { @@ -223,6 +221,5 @@ class ChannelFlowTest : TestBase() { finish(6) } expect(4) - assertTrue(subscribed) } }