-
Notifications
You must be signed in to change notification settings - Fork 100
/
flow.kt
149 lines (135 loc) · 4.66 KB
/
flow.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
/*
* Copyright (C) 2022 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 kotlin.time.Duration
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineStart.UNDISPATCHED
import kotlinx.coroutines.Dispatchers.Unconfined
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.Channel.Factory.UNLIMITED
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.job
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.TestCoroutineScheduler
import kotlinx.coroutines.test.UnconfinedTestDispatcher
/**
* 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 If non-null, overrides the current Turbine timeout inside [validate]. See also:
* [withTurbineTimeout].
*/
public suspend fun <T> Flow<T>.test(
timeout: Duration? = null,
name: String? = null,
validate: suspend ReceiveTurbine<T>.() -> Unit,
) {
coroutineScope {
collectTurbineIn(this, null, name).apply {
if (timeout != null) {
withTurbineTimeout(timeout) {
validate()
}
} else {
validate()
}
cancel()
ensureAllEventsConsumed()
}
}
}
/**
* Terminal flow operator that collects events from given flow and returns a [ReceiveTurbine] for
* consuming and asserting properties on them in order. If any exception occurs during validation the
* exception is rethrown from this method.
*
* ```kotlin
* val turbine = flowOf("one", "two").testIn(this)
* assertEquals("one", turbine.expectItem())
* assertEquals("two", turbine.expectItem())
* turbine.expectComplete()
* ```
*
* Unlike [test] which automatically cancels the flow at the end of the lambda, the returned
* [ReceiveTurbine] must either consume a terminal event (complete or error) or be explicitly canceled.
*
* @param timeout If non-null, overrides the current Turbine timeout for this [Turbine]. See also:
* [withTurbineTimeout].
*/
public fun <T> Flow<T>.testIn(
scope: CoroutineScope,
timeout: Duration? = null,
name: String? = null,
): ReceiveTurbine<T> {
if (timeout != null) {
// Eager check to throw early rather than in a subsequent 'await' call.
checkTimeout(timeout)
}
val turbine = collectTurbineIn(scope, timeout, name)
scope.coroutineContext.job.invokeOnCompletion { exception ->
if (debug) println("Scope ending ${exception ?: ""}")
// Only validate events were consumed if the scope is exiting normally.
if (exception == null) {
turbine.ensureAllEventsConsumed()
}
}
return turbine
}
@OptIn(ExperimentalCoroutinesApi::class) // New kotlinx.coroutines test APIs are not stable 😬
private fun <T> Flow<T>.collectTurbineIn(scope: CoroutineScope, timeout: Duration?, name: String?): Turbine<T> {
lateinit var channel: Channel<T>
// Use test-specific unconfined if test scheduler is in use to inherit its virtual time.
val unconfined = scope.coroutineContext[TestCoroutineScheduler]
?.let(::UnconfinedTestDispatcher)
?: Unconfined
val job = scope.launch(unconfined, start = UNDISPATCHED) {
channel = collectIntoChannel(this)
}
return ChannelTurbine(channel, job, timeout, name)
}
internal fun <T> Flow<T>.collectIntoChannel(scope: CoroutineScope): Channel<T> {
val output = Channel<T>(UNLIMITED)
val job = scope.launch(start = UNDISPATCHED) {
try {
collect { output.trySend(it) }
output.close()
} catch (e: Exception) {
output.close(e)
}
}
return object : Channel<T> by output {
override fun cancel(cause: CancellationException?) {
job.cancel()
output.close(cause)
}
override fun close(cause: Throwable?): Boolean {
job.cancel()
return output.close(cause)
}
}
}