aboutsummaryrefslogtreecommitdiffstats
path: root/kotlinx-coroutines-core/common/test/flow/channels/ChannelBuildersFlowTest.kt
blob: f93d03993339e85bc33126b144df0b2174d479d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
 * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.coroutines.flow

import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
import kotlin.test.*

class ChannelBuildersFlowTest : TestBase() {
    @Test
    fun testChannelConsumeAsFlow() = runTest {
        val channel = produce {
           repeat(10) {
               send(it + 1)
           }
        }
        val flow = channel.consumeAsFlow()
        assertEquals(55, flow.sum())
        assertFailsWith<IllegalStateException> { flow.collect() }
    }

    @Test
    fun testChannelReceiveAsFlow() = runTest {
        val channel = produce {
           repeat(10) {
               send(it + 1)
           }
        }
        val flow = channel.receiveAsFlow()
        assertEquals(55, flow.sum())
        assertEquals(emptyList(), flow.toList())
    }

    @Test
    fun testConsumeAsFlowCancellation() = runTest {
        val channel = produce(NonCancellable) { // otherwise failure will cancel scope as well
            repeat(10) {
                send(it + 1)
            }
            throw TestException()
        }
        val flow = channel.consumeAsFlow()
        assertEquals(15, flow.take(5).sum())
        // the channel should have been canceled, even though took only 5 elements
        assertTrue(channel.isClosedForReceive)
        assertFailsWith<IllegalStateException> { flow.collect() }
    }

    @Test
    fun testReceiveAsFlowCancellation() = runTest {
        val channel = produce(NonCancellable) { // otherwise failure will cancel scope as well
            repeat(10) {
                send(it + 1)
            }
            throw TestException()
        }
        val flow = channel.receiveAsFlow()
        assertEquals(15, flow.take(5).sum()) // sum of first 5
        assertEquals(40, flow.take(5).sum()) // sum the rest 5
        assertFailsWith<TestException> { flow.sum() } // exception in the rest
    }

    @Test
    fun testConsumeAsFlowException() = runTest {
        val channel = produce(NonCancellable) { // otherwise failure will cancel scope as well
            repeat(10) {
                send(it + 1)
            }
            throw TestException()
        }
        val flow = channel.consumeAsFlow()
        assertFailsWith<TestException> { flow.sum() }
        assertFailsWith<IllegalStateException> { flow.collect() }
    }

    @Test
    fun testReceiveAsFlowException() = runTest {
        val channel = produce(NonCancellable) { // otherwise failure will cancel scope as well
            repeat(10) {
                send(it + 1)
            }
            throw TestException()
        }
        val flow = channel.receiveAsFlow()
        assertFailsWith<TestException> { flow.sum() }
        assertFailsWith<TestException> { flow.collect() } // repeated collection -- same exception
    }

    @Test
    fun testConsumeAsFlowProduceFusing() = runTest {
        val channel = produce { send("OK") }
        val flow = channel.consumeAsFlow()
        assertSame(channel, flow.produceIn(this))
        assertFailsWith<IllegalStateException> { flow.produceIn(this) }
        channel.cancel()
    }

    @Test
    fun testReceiveAsFlowProduceFusing() = runTest {
        val channel = produce { send("OK") }
        val flow = channel.receiveAsFlow()
        assertSame(channel, flow.produceIn(this))
        assertSame(channel, flow.produceIn(this)) // can use produce multiple times
        channel.cancel()
    }

    @Test
    fun testConsumeAsFlowProduceBuffered() = runTest {
        expect(1)
        val channel = produce {
            expect(3)
            (1..10).forEach { send(it) }
            expect(4) // produces everything because of buffering
        }
        val flow = channel.consumeAsFlow().buffer() // request buffering
        expect(2) // producer is not running yet
        val result = flow.produceIn(this)
        // run the flow pipeline until it consumes everything into buffer
        while (!channel.isClosedForReceive) yield()
        expect(5) // produced had done running (buffered stuff)
        assertNotSame(channel, result)
        assertFailsWith<IllegalStateException> { flow.produceIn(this) }
        // check that we received everything
        assertEquals((1..10).toList(), result.toList())
        finish(6)
    }

    @Test
    fun testBroadcastChannelAsFlow() = runTest {
        val channel = broadcast {
           repeat(10) {
               send(it + 1)
           }
        }

        val sum = channel.asFlow().sum()
        assertEquals(55, sum)
    }

    @Test
    fun testExceptionInBroadcast() = runTest {
        expect(1)
        val channel = broadcast(NonCancellable) { // otherwise failure will cancel scope as well
            repeat(10) {
                send(it + 1)
            }
            throw TestException()
        }
        assertEquals(15, channel.asFlow().take(5).sum())

        // Workaround for JS bug
        try {
            channel.asFlow().collect { /* Do nothing */ }
            expectUnreached()
        } catch (e: TestException) {
            finish(2)
        }
    }

    @Test
    fun testBroadcastChannelAsFlowLimits() = runTest {
        val channel = BroadcastChannel<Int>(1)
        val flow = channel.asFlow().map { it * it }.drop(1).take(2)

        var expected = 0
        launch {
            assertTrue(channel.offer(1)) // Handed to the coroutine
            assertTrue(channel.offer(2)) // Buffered
            assertFalse(channel.offer(3)) // Failed to offer
            channel.send(3)
            yield()
            assertEquals(1, expected)
            assertTrue(channel.offer(4)) // Handed to the coroutine
            assertTrue(channel.offer(5)) // Buffered
            assertFalse(channel.offer(6))  // Failed to offer
            channel.send(6)
            assertEquals(2, expected)
        }

        val sum = flow.sum()
        assertEquals(13, sum)
        ++expected
        val sum2 = flow.sum()
        assertEquals(61, sum2)
        ++expected
    }

    @Test
    fun flowAsBroadcast() = runTest {
        val flow = flow {
            repeat(10) {
                emit(it)
            }
        }

        val channel = flow.broadcastIn(this)
        assertEquals((0..9).toList(), channel.openSubscription().toList())
    }

    @Test
    fun flowAsBroadcastMultipleSubscription() = runTest {
        val flow = flow {
            repeat(10) {
                emit(it)
            }
        }

        val broadcast = flow.broadcastIn(this)
        val channel = broadcast.openSubscription()
        val channel2 = broadcast.openSubscription()

        assertEquals(0, channel.receive())
        assertEquals(0, channel2.receive())
        yield()
        assertEquals(1, channel.receive())
        assertEquals(1, channel2.receive())

        channel.cancel()
        channel2.cancel()
        yield()
        ensureActive()
    }

    @Test
    fun flowAsBroadcastException() = runTest {
        val flow = flow {
            repeat(10) {
                emit(it)
            }

            throw TestException()
        }

        val channel = flow.broadcastIn(this + NonCancellable)
        assertFailsWith<TestException> { channel.openSubscription().toList() }
        assertTrue(channel.isClosedForSend) // Failure in the flow fails the channel
    }

    // Semantics of these tests puzzle me, we should figure out the way to prohibit such chains
    @Test
    fun testFlowAsBroadcastAsFlow() = runTest {
        val flow = flow {
            emit(1)
            emit(2)
            emit(3)
        }.broadcastIn(this).asFlow()

        assertEquals(6, flow.sum())
        assertEquals(0, flow.sum()) // Well suddenly flow is no longer idempotent and cold
    }

    @Test
    fun testBroadcastAsFlowAsBroadcast() = runTest {
        val channel = broadcast {
            send(1)
        }.asFlow().broadcastIn(this)

        channel.openSubscription().consumeEach {
            assertEquals(1, it)
        }

        channel.openSubscription().consumeEach {
            fail()
        }
    }

    @Test
    fun testProduceInAtomicity() = runTest {
        val flow = flowOf(1).onCompletion { expect(2) }
        val scope = CoroutineScope(wrapperDispatcher())
        flow.produceIn(scope)
        expect(1)
        scope.cancel()
        scope.coroutineContext[Job]?.join()
        finish(3)
    }
}