/
OnEmptyTest.kt
81 lines (71 loc) · 1.95 KB
/
OnEmptyTest.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
/*
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines.flow.operators
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import kotlin.test.*
class OnEmptyTest : TestBase() {
@Test
fun testOnEmptyInvoked() = runTest {
val flow = emptyFlow<Int>().onEmpty { emit(1) }
assertEquals(1, flow.single())
}
@Test
fun testOnEmptyNotInvoked() = runTest {
val flow = flowOf(1).onEmpty { emit(2) }
assertEquals(1, flow.single())
}
@Test
fun testOnEmptyNotInvokedOnError() = runTest {
val flow = flow<Int> {
throw TestException()
}.onEmpty { expectUnreached() }
assertFailsWith<TestException>(flow)
}
@Test
fun testOnEmptyNotInvokedOnCancellation() = runTest {
val flow = flow<Int> {
expect(2)
hang { expect(4) }
}.onEmpty { expectUnreached() }
expect(1)
val job = flow.onEach { expectUnreached() }.launchIn(this)
yield()
expect(3)
job.cancelAndJoin()
finish(5)
}
@Test
fun testOnEmptyCancellation() = runTest {
val flow = emptyFlow<Int>().onEmpty {
expect(2)
hang { expect(4) }
emit(1)
}
expect(1)
val job = flow.onEach { expectUnreached() }.launchIn(this)
yield()
expect(3)
job.cancelAndJoin()
finish(5)
}
@Test
fun testOnEmptyTransparencyViolation() = runTest {
val flow = emptyFlow<Int>().onEmpty {
expect(2)
coroutineScope {
launch {
try {
emit(1)
} catch (e: IllegalStateException) {
expect(3)
}
}
}
}
expect(1)
assertNull(flow.singleOrNull())
finish(4)
}
}