-
Notifications
You must be signed in to change notification settings - Fork 100
/
FlowTurbine.kt
312 lines (275 loc) · 8.97 KB
/
FlowTurbine.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
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
/*
* Copyright (C) 2018 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.cash.turbine
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineStart.UNDISPATCHED
import kotlinx.coroutines.Dispatchers.Unconfined
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.Channel.Factory.UNLIMITED
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlinx.coroutines.withTimeout
import kotlin.coroutines.CoroutineContext
import kotlin.time.Duration
import kotlin.time.ExperimentalTime
import kotlin.time.seconds
private const val debug = false
/**
* Terminal flow operator that collects events from given flow and allows the [validate] lambda to
* consume and assert properties on them in order. If any exception occurs during validation the
* exception is rethrown from this method.
*
* ```kotlin
* flowOf("one", "two").test {
* assertEquals("one", expectItem())
* assertEquals("two", expectItem())
* expectComplete()
* }
* ```
*
* @param timeout Duration each suspending function on [FlowTurbine] will wait before timing out.
*/
@ExperimentalTime // For timeout.
@ExperimentalCoroutinesApi // For start=UNDISPATCHED
suspend fun <T> Flow<T>.test(
timeout: Duration = 1.seconds,
context: CoroutineContext = Unconfined,
validate: suspend FlowTurbine<T>.() -> Unit
) {
coroutineScope {
val events = Channel<Event<T>>(UNLIMITED)
val collectJob = launch(start = UNDISPATCHED, context = context) {
val terminalEvent = try {
if (debug) println("Collect starting!")
collect { item ->
if (debug) println("Collect got: $item")
events.send(Event.Item(item))
}
if (debug) println("Collect complete!")
Event.Complete
} catch (_: CancellationException) {
if (debug) println("Collect canceled!")
null
} catch (t: Throwable) {
if (debug) println("Collect error! $t")
Event.Error(t)
}
if (terminalEvent != null) {
events.send(terminalEvent)
}
if (debug) println("Collect closing event channel")
events.close()
}
val flowTurbine = ChannelBasedFlowTurbine(events, collectJob, timeout)
val ensureConsumed = try {
flowTurbine.validate()
if (debug) println("Validate lambda returning normally")
true
} catch (e: CancellationException) {
if (e !== ignoreRemainingEventsException) {
if (debug) println("Validate ignoring remaining events")
throw e
}
if (debug) println("Validate canceling $e")
false
}
flowTurbine.cancel()
if (ensureConsumed) {
flowTurbine.ensureAllEventsConsumed()
}
}
}
/**
* Represents active collection on a source [Flow] which buffers item emissions, completion,
* and/or errors as events for consuming.
*/
interface FlowTurbine<T> {
/**
* Duration that [expectItem], [expectComplete], and [expectError] will wait for an event before
* throwing a timeout exception.
*/
@ExperimentalTime
val timeout: Duration
/**
* Cancel collecting events from the source [Flow]. Any events which have already been received
* will still need consumed using the "expect" functions.
*/
suspend fun cancel()
/**
* Cancel collecting events from the source [Flow] and ignore any events which have already
* been received. Calling this function will exit the [test] block.
*/
suspend fun cancelAndIgnoreRemainingEvents(): Nothing
/**
* Cancel collecting events from the source [Flow]. Any events which have already been received
* will be returned.
*/
suspend fun cancelAndConsumeRemainingEvents(): List<Event<T>>
/**
* Assert that there are no unconsumed events which have been received.
*
* @throws AssertionError if unconsumed events are found.
*/
fun expectNoEvents()
/**
* Assert that an event was received and return it.
* If no events have been received, this function will suspend for up to [timeout].
*
* @throws TimeoutCancellationException if no event was received in time.
*/
suspend fun expectEvent(): Event<T>
/**
* Assert that the next event received was an item and return it.
* If no events have been received, this function will suspend for up to [timeout].
*
* @throws AssertionError if the next event was completion or an error.
* @throws TimeoutCancellationException if no event was received in time.
*/
suspend fun expectItem(): T
/**
* Assert that the next event received was the flow completing.
* If no events have been received, this function will suspend for up to [timeout].
*
* @throws AssertionError if the next event was an item or an error.
* @throws TimeoutCancellationException if no event was received in time.
*/
suspend fun expectComplete()
/**
* Assert that the next event received was an error terminating the flow.
* If no events have been received, this function will suspend for up to [timeout].
*
* @throws AssertionError if the next event was an item or completion.
* @throws TimeoutCancellationException if no event was received in time.
*/
suspend fun expectError(): Throwable
}
private val ignoreRemainingEventsException = CancellationException("Ignore remaining events")
sealed class Event<out T> {
object Complete : Event<Nothing>() {
override fun toString() = "Complete"
}
data class Error(val throwable: Throwable) : Event<Nothing>() {
override fun toString() = "Error(${throwable::class.simpleName})"
}
data class Item<T>(val value: T) : Event<T>() {
override fun toString() = "Item($value)"
}
}
@ExperimentalTime
private class ChannelBasedFlowTurbine<T>(
private val events: Channel<Event<T>>,
private val collectJob: Job,
override val timeout: Duration
) : FlowTurbine<T> {
private suspend fun <T> withTimeout(body: suspend () -> T): T {
return if (timeout == Duration.ZERO) {
body()
} else {
withTimeout(timeout) {
body()
}
}
}
override suspend fun cancel() {
collectJob.cancel()
}
override suspend fun cancelAndIgnoreRemainingEvents(): Nothing {
cancel()
throw ignoreRemainingEventsException
}
override suspend fun cancelAndConsumeRemainingEvents(): List<Event<T>> {
cancel()
return mutableListOf<Event<T>>().apply {
while (true) {
this += events.poll() ?: break
}
}
}
override fun expectNoEvents() {
val event = events.poll()
if (event != null) {
unexpectedEvent(event, "no events")
}
}
override suspend fun expectEvent(): Event<T> {
return withTimeout {
events.receive()
}
}
override suspend fun expectItem(): T {
val event = expectEvent()
if (event !is Event.Item<T>) {
unexpectedEvent(event, "item")
}
return event.value
}
override suspend fun expectComplete() {
val event = expectEvent()
if (event != Event.Complete) {
unexpectedEvent(event, "complete")
}
}
override suspend fun expectError(): Throwable {
val event = expectEvent()
if (event !is Event.Error) {
unexpectedEvent(event, "error")
}
return event.throwable
}
private fun unexpectedEvent(event: Event<*>, expected: String): Nothing {
val cause = (event as? Event.Error)?.throwable
throw AssertionError("Expected $expected but found $event", cause)
}
fun ensureAllEventsConsumed() {
val unconsumed = mutableListOf<Event<T>>()
var cause: Throwable? = null
while (true) {
val event = events.poll() ?: break
unconsumed += event
if (event is Event.Error) {
check(cause == null)
cause = event.throwable
}
}
if (debug) println("Unconsumed events: $unconsumed")
if (unconsumed.isNotEmpty()) {
throw AssertionError(
buildString {
append("Unconsumed events found:")
for (event in unconsumed) {
append("\n - $event")
}
},
cause
)
}
}
}
/**
* A plain [AssertionError] working around three bugs:
*
* 1. No two-arg constructor in common (https://youtrack.jetbrains.com/issue/KT-40728).
* 2. No two-arg constructor in Java 6.
* 3. Public exceptions with public constructors have referential equality broken by coroutines.
*/
private class AssertionError(
message: String,
override val cause: Throwable?
) : kotlin.AssertionError(message)