forked from Kotlin/kotlinx.coroutines
/
FirstTest.kt
179 lines (154 loc) · 4.42 KB
/
FirstTest.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
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
/*
* 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 kotlinx.coroutines.flow.internal.*
import kotlin.test.*
class FirstTest : TestBase() {
@Test
fun testFirst() = runTest {
val flow = flowOf(1, 2, 3)
assertEquals(1, flow.first())
}
@Test
fun testNulls() = runTest {
val flow = flowOf(null, 1)
assertNull(flow.first())
assertNull(flow.first { it == null })
assertEquals(1, flow.first { it != null })
}
@Test
fun testFirstWithPredicate() = runTest {
val flow = flowOf(1, 2, 3)
assertEquals(1, flow.first { it > 0 })
assertEquals(2, flow.first { it > 1 })
assertFailsWith<NoSuchElementException> { flow.first { it > 3 } }
}
@Test
fun testFirstCancellation() = runTest {
val latch = Channel<Unit>()
val flow = flow {
coroutineScope {
launch {
latch.send(Unit)
hang { expect(1) }
}
emit(1)
emit(2)
}
}
val result = flow.first {
latch.receive()
true
}
assertEquals(1, result)
finish(2)
}
@Test
fun testEmptyFlow() = runTest {
assertFailsWith<NoSuchElementException> { emptyFlow<Int>().first() }
assertFailsWith<NoSuchElementException> { emptyFlow<Int>().first { true } }
}
@Test
fun testErrorCancelsUpstream() = runTest {
val latch = Channel<Unit>()
val flow = flow {
coroutineScope {
launch {
latch.send(Unit)
hang { expect(1) }
}
emit(1)
}
}
assertFailsWith<TestException> {
flow.first {
latch.receive()
throw TestException()
}
}
assertEquals(1, flow.first())
finish(2)
}
@Test
fun testFirstOrNull() = runTest {
val flow = flowOf(1, 2, 3)
assertEquals(1, flow.firstOrNull())
}
@Test
fun testFirstOrNullWithPredicate() = runTest {
val flow = flowOf(1, 2, 3)
assertEquals(1, flow.firstOrNull { it > 0 })
assertEquals(2, flow.firstOrNull { it > 1 })
assertNull(flow.firstOrNull { it > 3 })
}
@Test
fun testFirstOrNullCancellation() = runTest {
val latch = Channel<Unit>()
val flow = flow {
coroutineScope {
launch {
latch.send(Unit)
hang { expect(1) }
}
emit(1)
emit(2)
}
}
val result = flow.firstOrNull {
latch.receive()
true
}
assertEquals(1, result)
finish(2)
}
@Test
fun testFirstOrNullWithEmptyFlow() = runTest {
assertNull(emptyFlow<Int>().firstOrNull())
assertNull(emptyFlow<Int>().firstOrNull { true })
}
@Test
fun testFirstOrNullWithNullElement() = runTest {
assertNull(flowOf<String?>(null).firstOrNull())
assertNull(flowOf<String?>(null).firstOrNull { true })
}
@Test
fun testFirstOrNullWhenErrorCancelsUpstream() = runTest {
val latch = Channel<Unit>()
val flow = flow {
coroutineScope {
launch {
latch.send(Unit)
hang { expect(1) }
}
emit(1)
}
}
assertFailsWith<TestException> {
flow.firstOrNull {
latch.receive()
throw TestException()
}
}
assertEquals(1, flow.firstOrNull())
finish(2)
}
@Test
fun testBadClass() = runTest {
val instance = BadClass()
val flow = flowOf(instance)
assertSame(instance, flow.first())
assertSame(instance, flow.firstOrNull())
assertSame(instance, flow.first { true })
assertSame(instance, flow.firstOrNull { true })
}
@Test
fun testAbortFlowException() = runTest {
val flow = flow<Int> {
throw AbortFlowException(NopCollector) // Emulate cancellation
}
assertFailsWith<CancellationException> { flow.first() }
}
}