forked from Kotlin/kotlinx.coroutines
/
CatchTest.kt
204 lines (182 loc) · 5.62 KB
/
CatchTest.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
/*
* Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines.flow
import kotlinx.coroutines.*
import kotlin.coroutines.*
import kotlin.test.*
class CatchTest : TestBase() {
@Test
fun testCatchEmit() = runTest {
val flow = flow {
emit(1)
throw TestException()
}
assertEquals(42, flow.catch { emit(41) }.sum())
assertFailsWith<TestException>(flow)
}
@Test
fun testCatchEmitExceptionFromDownstream() = runTest {
var executed = 0
val flow = flow {
emit(1)
}.catch { emit(42) }.map {
++executed
throw TestException()
}
assertFailsWith<TestException>(flow)
assertEquals(1, executed)
}
@Test
fun testCatchEmitAll() = runTest {
val flow = flow {
emit(1)
throw TestException()
}.catch { emitAll(flowOf(2)) }
assertEquals(3, flow.sum())
}
@Test
fun testCatchEmitAllExceptionFromDownstream() = runTest {
var executed = 0
val flow = flow {
emit(1)
}.catch { emitAll(flowOf(1, 2, 3)) }.map {
++executed
throw TestException()
}
assertFailsWith<TestException>(flow)
assertEquals(1, executed)
}
@Test
fun testWithTimeoutCatch() = runTest {
val flow = flow<Int> {
withTimeout(1) {
hang { expect(1) }
}
expectUnreached()
}.catch { emit(1) }
assertEquals(1, flow.single())
finish(2)
}
@Test
fun testCancellationFromUpstreamCatch() = runTest {
val flow = flow<Int> {
hang { }
}.catch { expectUnreached() }
val job = launch {
expect(1)
flow.collect { }
}
yield()
expect(2)
job.cancelAndJoin()
finish(3)
}
@Test
fun testCatchContext() = runTest {
expect(1)
val flow = flow {
expect(2)
emit("OK")
expect(3)
throw TestException()
}
val d0 = coroutineContext[ContinuationInterceptor] as CoroutineContext
val d1 = wrapperDispatcher(coroutineContext)
val d2 = wrapperDispatcher(coroutineContext)
flow
.catch { e ->
expect(4)
assertTrue(e is TestException)
assertEquals("A", kotlin.coroutines.coroutineContext[CoroutineName]?.name)
assertSame(d1, kotlin.coroutines.coroutineContext[ContinuationInterceptor] as CoroutineContext)
throw e // rethrow downstream
}
.flowOn(CoroutineName("A"))
.catch { e ->
expect(5)
assertTrue(e is TestException)
assertEquals("B", kotlin.coroutines.coroutineContext[CoroutineName]?.name)
assertSame(d1, kotlin.coroutines.coroutineContext[ContinuationInterceptor] as CoroutineContext)
throw e // rethrow downstream
}
.flowOn(CoroutineName("B"))
.catch { e ->
expect(6)
assertTrue(e is TestException)
assertSame(d1, kotlin.coroutines.coroutineContext[ContinuationInterceptor] as CoroutineContext)
throw e // rethrow downstream
}
.flowOn(d1)
.catch { e ->
expect(7)
assertTrue(e is TestException)
assertSame(d2, kotlin.coroutines.coroutineContext[ContinuationInterceptor] as CoroutineContext)
throw e // rethrow downstream
}
.flowOn(d2)
// flowOn with a different dispatcher introduces asynchrony so that all exceptions in the
// upstream flows are handled before they go downstream
.onEach {
expectUnreached() // already cancelled
}
.catch { e ->
expect(8)
assertTrue(e is TestException)
assertSame(d0, kotlin.coroutines.coroutineContext[ContinuationInterceptor] as CoroutineContext)
}
.collect()
finish(9)
}
@Test
fun testUpstreamExceptionConcurrentWithDownstream() = runTest {
val flow = flow {
try {
expect(1)
emit(1)
} finally {
expect(3)
throw TestException()
}
}.catch { expectUnreached() }.onEach {
expect(2)
throw TestException2()
}
assertFailsWith<TestException>(flow)
finish(4)
}
@Test
fun testUpstreamExceptionConcurrentWithDownstreamCancellation() = runTest {
val flow = flow {
try {
expect(1)
emit(1)
} finally {
expect(3)
throw TestException()
}
}.catch { expectUnreached() }.onEach {
expect(2)
throw CancellationException("")
}
assertFailsWith<TestException>(flow)
finish(4)
}
@Test
fun testUpstreamCancellationIsIgnoredWhenDownstreamFails() = runTest {
val flow = flow {
try {
expect(1)
emit(1)
} finally {
expect(3)
throw CancellationException("")
}
}.catch { expectUnreached() }.onEach {
expect(2)
throw TestException("")
}
assertFailsWith<TestException>(flow)
finish(4)
}
}