-
Notifications
You must be signed in to change notification settings - Fork 99
/
ChannelTest.kt
362 lines (314 loc) · 11.3 KB
/
ChannelTest.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
* 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.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertSame
import kotlin.time.Duration.Companion.milliseconds
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.withContext
class ChannelTest {
@Test
fun exceptionsPropagateWhenExpectMostRecentItem() = runTest {
val expected = CustomRuntimeException("hello")
val actual = assertFailsWith<RuntimeException> {
val channel = flow {
emit(1)
emit(2)
emit(3)
throw expected
}.collectIntoChannel(this)
channel.expectMostRecentItem()
}
assertSame(expected, actual)
}
@Test
fun expectMostRecentItemButNoItemWasFoundThrows() = runTest {
val actual = assertFailsWith<AssertionError> {
val channel = emptyFlow<Any>().collectIntoChannel(this)
channel.expectMostRecentItem()
}
assertEquals("No item was found", actual.message)
}
@Test
fun expectMostRecentItem() = runTest {
val onTwoSent = CompletableDeferred<Unit>()
val onTwoContinue = CompletableDeferred<Unit>()
val onCompleteSent = CompletableDeferred<Unit>()
val onCompleteContinue = CompletableDeferred<Unit>()
val channel = flowOf(1, 2, 3, 4, 5)
.map {
if (it == 3) {
onTwoSent.complete(Unit)
onTwoContinue.await()
}
it
}
.onCompletion {
onCompleteSent.complete(Unit)
onCompleteContinue.await()
}
.collectIntoChannel(this)
onTwoSent.await()
assertEquals(2, channel.expectMostRecentItem())
onTwoContinue.complete(Unit)
onCompleteSent.await()
assertEquals(5, channel.expectMostRecentItem())
onCompleteContinue.complete(Unit)
channel.cancel()
}
@Test
fun assertNullValuesWithExpectMostRecentItem() = runTest {
val channel = flowOf(1, 2, null).collectIntoChannel(this)
assertEquals(null, channel.expectMostRecentItem())
}
@Test fun awaitItemsAreSkipped() = runTest {
val channel = flowOf(1, 2, 3).collectIntoChannel(this)
channel.skipItems(2)
assertEquals(3, channel.awaitItem())
}
@Test fun skipItemsThrowsOnComplete() = runTest {
val channel = flowOf(1, 2).collectIntoChannel(this)
val message = assertFailsWith<AssertionError> {
channel.skipItems(3)
}.message
assertEquals("Expected 3 items but got 2 items and Complete", message)
}
@Test fun expectErrorOnCompletionBeforeAllItemsWereSkipped() = runTest {
val channel = flowOf(1).collectIntoChannel(this)
assertFailsWith<AssertionError> {
channel.skipItems(2)
}
}
@Test fun expectErrorOnErrorReceivedBeforeAllItemsWereSkipped() = runTest {
val error = CustomRuntimeException("hello")
val channel = flow {
emit(1)
throw error
}.collectIntoChannel(this)
val actual = assertFailsWith<AssertionError> {
channel.skipItems(2)
}
assertSame(error, actual.cause)
}
@Test fun expectNoEvents() = runTest {
val channel = neverFlow().collectIntoChannel(this)
channel.expectNoEvents()
channel.cancel()
}
@Test fun awaitItemEvent() = runTest {
val item = Any()
val channel = flowOf(item).collectIntoChannel(this)
val event = channel.awaitEvent()
assertEquals(Event.Item(item), event)
}
@Test fun expectCompleteEvent() = runTest {
val channel = emptyFlow<Nothing>().collectIntoChannel(this)
val event = channel.awaitEvent()
assertEquals(Event.Complete, event)
}
@Test fun expectErrorEvent() = runTest {
val exception = CustomRuntimeException("hello")
val channel = flow<Nothing> { throw exception }.collectIntoChannel(this)
val event = channel.awaitEvent()
assertEquals(Event.Error(exception), event)
}
@Test fun awaitItem() = runTest {
val item = Any()
val channel = flowOf(item).collectIntoChannel(this)
assertSame(item, channel.awaitItem())
}
@Test fun awaitItemButWasCloseThrows() = runTest {
val actual = assertFailsWith<AssertionError> {
emptyFlow<Unit>().collectIntoChannel(this).awaitItem()
}
assertEquals("Expected item but found Complete", actual.message)
}
@Test fun awaitItemButWasErrorThrows() = runTest {
val error = CustomRuntimeException("hello")
val actual = assertFailsWith<AssertionError> {
flow<Unit> { throw error }.collectIntoChannel(this)
.awaitItem()
}
assertEquals("Expected item but found Error(CustomRuntimeException)", actual.message)
assertSame(error, actual.cause)
}
@Test fun awaitComplete() = runTest {
emptyFlow<Nothing>().collectIntoChannel(this).awaitComplete()
}
@Test fun awaitCompleteButWasItemThrows() = runTest {
val actual = assertFailsWith<AssertionError> {
flowOf("item!").collectIntoChannel(this)
.awaitComplete()
}
assertEquals("Expected complete but found Item(item!)", actual.message)
}
@Test fun awaitCompleteButWasErrorThrows() = runTest {
val actual = assertFailsWith<AssertionError> {
flow<Unit> { throw RuntimeException() }.collectIntoChannel(this)
.awaitComplete()
}
assertEquals("Expected complete but found Error(RuntimeException)", actual.message)
}
@Test fun awaitError() = runTest {
val error = CustomRuntimeException("hello")
val channel = flow<Nothing> { throw error }.collectIntoChannel(this)
assertSame(error, channel.awaitError())
}
@Test fun awaitErrorButWasItemThrows() = runTest {
val actual = assertFailsWith<AssertionError> {
flowOf("item!").collectIntoChannel(this).awaitError()
}
assertEquals("Expected error but found Item(item!)", actual.message)
}
@Test fun awaitErrorButWasCompleteThrows() = runTest {
val actual = assertFailsWith<AssertionError> {
emptyFlow<Nothing>().collectIntoChannel(this).awaitError()
}
assertEquals("Expected error but found Complete", actual.message)
}
@Test fun failsOnDefaultTimeout() = runTest {
val actual = assertFailsWith<AssertionError> {
coroutineScope {
neverFlow().collectIntoChannel(this).awaitItem()
}
}
assertEquals("No value produced in 1s", actual.message)
}
@Test fun awaitHonorsCoroutineContextTimeoutNoTimeout() = runTest {
withTurbineTimeout(1500.milliseconds) {
val job = launch {
neverFlow().collectIntoChannel(this).awaitItem()
}
withContext(Dispatchers.Default) {
delay(1100)
}
job.cancel()
}
}
@Test fun awaitHonorsCoroutineContextTimeoutTimeout() = runTest {
val actual = assertFailsWith<AssertionError> {
withTurbineTimeout(10.milliseconds) {
neverFlow().collectIntoChannel(this).awaitItem()
}
}
assertEquals("No value produced in 10ms", actual.message)
}
@Test fun negativeTurbineTimeoutThrows() = runTest {
val actual = assertFailsWith<IllegalStateException> {
withTurbineTimeout((-10).milliseconds) {
}
}
assertEquals("Turbine timeout must be greater than 0: -10ms", actual.message)
}
@Test fun zeroTurbineTimeoutThrows() = runTest {
val actual = assertFailsWith<IllegalStateException> {
withTurbineTimeout(0.milliseconds) {
}
}
assertEquals("Turbine timeout must be greater than 0: 0s", actual.message)
}
@Test fun takeItem() = withTestScope {
val item = Any()
val channel = flowOf(item).collectIntoChannel(this)
assertSame(item, channel.takeItem())
}
@Test fun takeItemButWasCloseThrows() = withTestScope {
val actual = assertFailsWith<AssertionError> {
emptyFlow<Unit>().collectIntoChannel(this).takeItem()
}
assertEquals("Expected item but found Complete", actual.message)
}
@Test fun takeItemButWasErrorThrows() = withTestScope {
val error = CustomRuntimeException("hello")
val actual = assertFailsWith<AssertionError> {
flow<Unit> { throw error }.collectIntoChannel(this)
.takeItem()
}
assertEquals("Expected item but found Error(CustomRuntimeException)", actual.message)
assertSame(error, actual.cause)
}
@Test
fun expectMostRecentItemButNoItemWasFoundThrowsWithName() = runTest {
val actual = assertFailsWith<AssertionError> {
val channel = emptyFlow<Any>().collectIntoChannel(this)
channel.expectMostRecentItem(name = "empty flow")
}
assertEquals("No item was found for empty flow", actual.message)
}
@Test fun awaitItemButWasCloseThrowsWithName() = runTest {
val actual = assertFailsWith<AssertionError> {
emptyFlow<Unit>().collectIntoChannel(this).awaitItem(name = "closed flow")
}
assertEquals("Expected item for closed flow but found Complete", actual.message)
}
@Test fun awaitCompleteButWasItemThrowsWithName() = runTest {
val actual = assertFailsWith<AssertionError> {
flowOf("item!").collectIntoChannel(this)
.awaitComplete(name = "item flow")
}
assertEquals("Expected complete for item flow but found Item(item!)", actual.message)
}
@Test fun awaitErrorButWasItemThrowsWithName() = runTest {
val actual = assertFailsWith<AssertionError> {
flowOf("item!").collectIntoChannel(this).awaitError(name = "item flow")
}
assertEquals("Expected error for item flow but found Item(item!)", actual.message)
}
@Test fun awaitHonorsCoroutineContextTimeoutTimeoutWithName() = runTest {
val actual = assertFailsWith<AssertionError> {
withTurbineTimeout(10.milliseconds) {
neverFlow().collectIntoChannel(this).awaitItem(name = "never flow")
}
}
assertEquals("No value produced for never flow in 10ms", actual.message)
}
@Test fun takeItemButWasCloseThrowsWithName() = withTestScope {
val actual = assertFailsWith<AssertionError> {
emptyFlow<Unit>().collectIntoChannel(this).takeItem(name = "empty flow")
}
assertEquals("Expected item for empty flow but found Complete", actual.message)
}
@Test fun skipItemsThrowsOnCompleteWithName() = runTest {
val channel = flowOf(1, 2).collectIntoChannel(this)
val message = assertFailsWith<AssertionError> {
channel.skipItems(3, name = "two item channel")
}.message
assertEquals("Expected 3 items for two item channel but got 2 items and Complete", message)
}
/**
* Used to run test code with a [TestScope], but still outside a suspending context.
*/
private fun withTestScope(block: TestScope.()->Unit) {
val job = Job()
TestScope(job).block()
job.cancel()
}
}