aboutsummaryrefslogtreecommitdiffstats
path: root/kotlinx-coroutines-core/common/test/flow/operators/FlowOnTest.kt
blob: 34c0476ef6d9f785e3765a886ef2868122263bc9 (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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
 * 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 FlowOnTest : TestBase() {

    @Test
    fun testFlowOn() = runTest {
        val source = Source(42)
        val consumer = Consumer(42)

        val flow = source::produce.asFlow()
        flow.flowOn(NamedDispatchers("ctx1")).launchIn(this) {
            onEach { consumer.consume(it) }
        }.join()

        assertEquals("ctx1", source.contextName)
        assertEquals("main", consumer.contextName)

        flow.flowOn(NamedDispatchers("ctx2")).launchIn(this) {
            onEach { consumer.consume(it) }
        }.join()

        assertEquals("ctx2", source.contextName)
        assertEquals("main", consumer.contextName)
    }

    @Test
    fun testFlowOnAndOperators() = runTest {
        val source = Source(42)
        val consumer = Consumer(42)
        val captured = ArrayList<String>()
        val mapper: suspend (Int) -> Int = {
            captured += NamedDispatchers.nameOr("main")
            it
        }

        val flow = source::produce.asFlow()
        flow.map(mapper)
            .flowOn(NamedDispatchers("ctx1"))
            .map(mapper)
            .flowOn(NamedDispatchers("ctx2"))
            .map(mapper)
            .launchIn(this) {
                onEach { consumer.consume(it) }
            }.join()

        assertEquals(listOf("ctx1", "ctx2", "main"), captured)
        assertEquals("ctx1", source.contextName)
        assertEquals("main", consumer.contextName)
    }

    @Test
    public fun testFlowOnThrowingSource() = runTest {
        val flow = flow {
            expect(1)
            emit(NamedDispatchers.name())
            expect(3)
            throw TestException()
        }.map {
            expect(2)
            assertEquals("throwing", it)
            it
        }.flowOn(NamedDispatchers("throwing"))

        assertFailsWith<TestException> { flow.single() }
        ensureActive()
        finish(4)
    }

    @Test
    public fun testFlowOnThrowingOperator() = runTest {
        val flow = flow {
            expect(1)
            emit(NamedDispatchers.name())
            expectUnreached()
        }.map {
            expect(2)
            assertEquals("throwing", it)
            throw TestException(); it
        }.flowOn(NamedDispatchers("throwing"))

        assertFailsWith<TestException>(flow)
        ensureActive()
        finish(3)
    }

    @Test
    public fun testFlowOnDownstreamOperator() = runTest() {
        val flow = flow {
            expect(2)
            emit(NamedDispatchers.name())
            hang { expect(5) }
            delay(Long.MAX_VALUE)
        }.map {
            expect(3)
            it
        }.flowOn(NamedDispatchers("throwing"))
            .map<String, String> {
                expect(4);
                throw TestException()
            }

        expect(1)
        assertFailsWith<TestException> { flow.single() }
        ensureActive()
        finish(6)
    }

    @Test
    public fun testFlowOnThrowingConsumer() = runTest {
        val flow = flow {
            expect(2)
            emit(NamedDispatchers.name())
            hang { expect(4) }
        }

        expect(1)
        flow.flowOn(NamedDispatchers("...")).launchIn(this + NamedDispatchers("launch")) {
            onEach {
                expect(3)
                throw TestException()
            }
            catch<Throwable> { expect(5) }
        }.join()

        ensureActive()
        finish(6)
    }

    @Test
    fun testFlowOnWithJob() = runTest({ it is IllegalArgumentException }) {
        flow {
            emit(1)
        }.flowOn(NamedDispatchers("foo") + Job())
    }

    @Test
    fun testFlowOnCancellation() = runTest {
        val latch = Channel<Unit>()
        expect(1)
        val job = launch(NamedDispatchers("launch")) {
            flow<Int> {
                expect(2)
                latch.send(Unit)
                expect(3)
                hang {
                    assertEquals("cancelled", NamedDispatchers.name())
                    expect(5)
                }
            }.flowOn(NamedDispatchers("cancelled")).single()
        }

        latch.receive()
        expect(4)
        job.cancel()
        job.join()
        ensureActive()
        finish(6)
    }

    @Test
    fun testFlowOnCancellationHappensBefore() = runTest {
        launch {
            try {
                flow<Int> {
                    expect(1)
                    val flowJob = kotlin.coroutines.coroutineContext[Job]!!
                    launch {
                        expect(2)
                        flowJob.cancel()
                    }
                    hang { expect(3) }
                }.flowOn(NamedDispatchers("upstream")).single()
            } catch (e: CancellationException) {
                expect(4)
            }
        }.join()
        ensureActive()
        finish(5)
    }

    @Test
    fun testIndependentOperatorContext() = runTest {
        val value = flow {
            assertEquals("base", NamedDispatchers.nameOr("main"))
            expect(1)
            emit(-239)
        }.map {
            assertEquals("base", NamedDispatchers.nameOr("main"))
            expect(2)
            it
        }.flowOn(NamedDispatchers("base"))
            .map {
                assertEquals("main", NamedDispatchers.nameOr("main"))
                expect(3)
                it
            }.single()

        assertEquals(-239, value)
        finish(4)
    }

    @Test
    fun testMultipleFlowOn() = runTest {
        flow {
            assertEquals("ctx1", NamedDispatchers.nameOr("main"))
            expect(1)
            emit(1)
        }.map {
            assertEquals("ctx1", NamedDispatchers.nameOr("main"))
            expect(2)
        }.flowOn(NamedDispatchers("ctx1"))
            .map {
                assertEquals("ctx2", NamedDispatchers.nameOr("main"))
                expect(3)
            }.flowOn(NamedDispatchers("ctx2"))
            .map {
                assertEquals("ctx3", NamedDispatchers.nameOr("main"))
                expect(4)
            }.flowOn(NamedDispatchers("ctx3"))
            .map {
                assertEquals("main", NamedDispatchers.nameOr("main"))
                expect(5)
            }
            .single()

        finish(6)
    }

    @Test
    fun testTimeoutExceptionUpstream() = runTest {
        val flow = flow {
            emit(1)
            yield()
            withTimeout(-1) {}
            emit(42)
        }.flowOn(NamedDispatchers("foo")).onEach {
            expect(1)
        }
        assertFailsWith<TimeoutCancellationException>(flow)
        finish(2)
    }

    @Test
    fun testTimeoutExceptionDownstream() = runTest {
        val flow = flow {
            emit(1)
            hang { expect(2) }
        }.flowOn(NamedDispatchers("foo")).onEach {
            expect(1)
            withTimeout(-1) {}
        }
        assertFailsWith<TimeoutCancellationException>(flow)
        finish(3)
    }

    @Test
    fun testCancellation() = runTest {
        val result = flow {
            emit(1)
            emit(2)
            emit(3)
            expectUnreached()
            emit(4)
        }.flowOn(wrapperDispatcher())
            .buffer(0)
            .take(2)
            .toList()
        assertEquals(listOf(1, 2), result)
    }

    @Test
    fun testException() = runTest {
        val flow = flow {
            emit(314)
            delay(Long.MAX_VALUE)
        }.flowOn(NamedDispatchers("upstream"))
            .map {
                throw TestException()
            }

        assertFailsWith<TestException> { flow.single() }
        assertFailsWith<TestException>(flow)
        ensureActive()
    }

    @Test
    fun testIllegalArgumentException() {
        val flow = emptyFlow<Int>()
        assertFailsWith<IllegalArgumentException> { flow.flowOn(Job()) }
    }

    private inner class Source(private val value: Int) {
        public var contextName: String = "unknown"

        fun produce(): Int {
            contextName = NamedDispatchers.nameOr("main")
            return value
        }
    }

    private inner class Consumer(private val expected: Int) {
        public var contextName: String = "unknown"

        fun consume(value: Int) {
            contextName = NamedDispatchers.nameOr("main")
            assertEquals(expected, value)
        }
    }
}