Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
~ Fixed iteration over the shared channel
Browse files Browse the repository at this point in the history
elizarov committed Nov 22, 2019
1 parent d2a012a commit ed5876e
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -822,7 +822,10 @@ internal abstract class AbstractChannel<E> : AbstractSendChannel<E>(), Channel<E
}

private class Itr<E>(val channel: AbstractChannel<E>) : ChannelIterator<E> {
var result: Any? = POLL_FAILED // E | POLL_FAILED | Closed
private val _result = atomic<Any?>(POLL_FAILED) // E | POLL_FAILED | Closed
var result: Any?
get() = _result.value
set(value) { _result.value = value }

override suspend fun hasNext(): Boolean {
// check for repeated hasNext
16 changes: 16 additions & 0 deletions kotlinx-coroutines-core/native/test/WorkerDispatcherTest.kt
Original file line number Diff line number Diff line change
@@ -124,6 +124,22 @@ class WorkerDispatcherTest : TestBase() {
}
}

@Test
fun testChannelIterator() = runTest {
expect(1)
val channel = RendezvousChannel<Int>()
launch(dispatcher) {
channel.send(1)
channel.send(2)
channel.close()
}
var expected = 1
for (x in channel) {
assertEquals(expected++, x)
}
finish(2)
}

@Test
fun testArrayBroadcast() = runTest {
expect(1)

0 comments on commit ed5876e

Please sign in to comment.