/
FlattenConcatTest.kt
52 lines (43 loc) · 1.24 KB
/
FlattenConcatTest.kt
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
/*
* 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 kotlin.test.*
class FlattenConcatTest : FlatMapBaseTest() {
override fun <T> Flow<T>.flatMap(mapper: suspend (T) -> Flow<T>): Flow<T> = map(mapper).flattenConcat()
@Test
fun testFlatMapConcurrency() = runTest {
var concurrentRequests = 0
val flow = (1..100).asFlow().map { value ->
flow {
++concurrentRequests
emit(value)
delay(Long.MAX_VALUE)
}
}.flattenConcat()
val consumer = launch {
flow.collect { value ->
expect(value)
}
}
repeat(4) {
yield()
}
assertEquals(1, concurrentRequests)
consumer.cancelAndJoin()
finish(2)
}
@Test
fun testCancellation() = runTest {
val flow = flow {
repeat(5) {
emit(flow {
if (it == 2) throw CancellationException("")
emit(1)
})
}
}
assertFailsWith<CancellationException>(flow.flattenConcat())
}
}