Skip to content

Commit 48bdeb4

Browse files
committed
Fix broadcast builder with different thread
Fixes #1831
1 parent c6e494b commit 48bdeb4

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

kotlinx-coroutines-core/common/src/channels/Broadcast.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ private class LazyBroadcastCoroutine<E>(
170170
channel: BroadcastChannel<E>,
171171
block: suspend ProducerScope<E>.() -> Unit
172172
) : BroadcastCoroutine<E>(parentContext, channel, active = false) {
173-
private val continuation = block.createCoroutineUnintercepted(this, this)
173+
private val saved = saveLazyCoroutine(this, this, block)
174174

175175
override fun openSubscription(): ReceiveChannel<E> {
176176
// open subscription _first_
@@ -181,6 +181,6 @@ private class LazyBroadcastCoroutine<E>(
181181
}
182182

183183
override fun onStart() {
184-
continuation.startCoroutineCancellable(this)
184+
startLazyCoroutine(saved, this, this)
185185
}
186186
}

kotlinx-coroutines-core/native/test/WorkerDispatcherTest.kt

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
2+
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

55
package kotlinx.coroutines
@@ -296,5 +296,22 @@ class WorkerDispatcherTest : TestBase() {
296296
finish(2)
297297
}
298298

299+
@Test
300+
fun testBroadcastAsFlow() = runTest {
301+
expect(1)
302+
withContext(dispatcher) {
303+
expect(2)
304+
broadcast {
305+
expect(3)
306+
send("OK")
307+
}.asFlow().collect {
308+
expect(4)
309+
assertEquals("OK", it)
310+
}
311+
expect(5)
312+
}
313+
finish(6)
314+
}
315+
299316
private data class Data(val s: String)
300317
}

0 commit comments

Comments
 (0)