-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlowBuildersTest.kt
149 lines (120 loc) · 3.15 KB
/
FlowBuildersTest.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
package dev.olog.flow.test.observer
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.channels.sendBlocking
import kotlinx.coroutines.channels.trySendBlocking
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runBlockingTest
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.yield
import org.junit.Test
/**
* Tests that checks completition of different flow builders
*/
internal class FlowBuildersTest {
@Test
fun testEmptyFlow() = runTest(UnconfinedTestDispatcher()) {
val flow: Flow<Int> = emptyFlow()
flow.test(this) {
assertNoValues()
assertComplete()
}
}
@Test
fun testFlowOf() = runTest(UnconfinedTestDispatcher()) {
val flow: Flow<Int> = flowOf(1, 2)
flow.test(this) {
assertValues(1, 2)
assertComplete()
}
}
@Test
fun testFlow() = runTest(UnconfinedTestDispatcher()) {
val flow: Flow<Int> = flow {
delay(10_000)
yield()
emit(1)
emit(2)
}
flow.test(this) {
assertValues(1, 2)
assertComplete()
}
}
@Test
fun testNestedFlow() = runTest(UnconfinedTestDispatcher()) {
val nestedFlow: Flow<Int> = flow {
emit(2)
emit(3)
}
val flow: Flow<Int> = flow {
delay(10_000)
yield()
emit(1)
emitAll(nestedFlow)
}
flow.test(this) {
assertValues(1, 2, 3)
assertComplete()
}
}
@Test
fun testChannelFlow() = runTest(UnconfinedTestDispatcher()) {
val flow: Flow<Int> = channelFlow {
delay(10_000)
yield()
trySend(1)
send(2)
trySendBlocking(3)
}
flow.test(this) {
assertValues(1, 2, 3)
assertComplete()
}
}
@Test
fun testInfiniteChannelFlow() = runTest(UnconfinedTestDispatcher()) {
val flow: Flow<Int> = channelFlow {
delay(10_000)
yield()
trySend(1)
send(2)
trySendBlocking(3)
awaitClose()
}
flow.test(this) {
assertValues(1, 2, 3)
assertNotComplete()
}
}
@Test
fun testCallbackFlow() = runTest(UnconfinedTestDispatcher()) {
val flow: Flow<Int> = callbackFlow {
delay(10_000)
yield()
trySend(1)
send(2)
trySendBlocking(3)
}
flow.test(this) {
assertValues(1, 2, 3)
assertComplete()
}
}
@Test
fun testInfiniteCallbackFlow() = runTest(UnconfinedTestDispatcher()) {
val flow: Flow<Int> = callbackFlow {
delay(10_000)
yield()
trySend(1)
send(2)
trySendBlocking(3)
awaitClose()
}
flow.test(this) {
assertValues(1, 2, 3)
assertNotComplete()
}
}
}