kotlinchannelcoroutinekotlin-coroutine-channel

coroutine channel - channel capacity is 3 but send 4 items before receive


This is my test code.

    test("capacity = 3, but send 4 before receive") {
        val current = System.currentTimeMillis()
        val channel = Channel<Int>(capacity = 3)
        launch {
            repeat(5) {
                delay(100)
                println("[${getElapsedTime(current)}] send $it")
                channel.send(it)
            }
            channel.close()
        }

        delay(1000)
        launch {
            for (msg in channel) {
                println("[${getElapsedTime(current)}] receive $msg")
                delay(1000)
            }
        }
    }

I expected to send 3 items and stop sending. And after receiving 1 item, I expected the sending to resume.

But, after 4 items sending and stop sending. Why does it work like?

This is result:

[110 ms] send 0
[221 ms] send 1
[326 ms] send 2
[432 ms] send 3
[1017 ms] receive 0
[1123 ms] send 4
[2019 ms] receive 1
[3026 ms] receive 2
[4028 ms] receive 3
[5034 ms] receive 4

I expected:

[110 ms] send 0
[221 ms] send 1
[326 ms] send 2
[1017 ms] receive 0
[1123 ms] send 3
[2019 ms] receive 1
[2128 ms] send 4
[3026 ms] receive 2
[4028 ms] receive 3
[5034 ms] receive 4

Solution

  • After putting 3 items to the channel, sender still progresses, "produces" the 4th item, tries to send it, and only then it has to wait on send() due to lack of space in the channel. 4th item was not really sent at that time, but producer is already past the println line.