-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathWithContextTest.kt
381 lines (349 loc) · 10.4 KB
/
WithContextTest.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
@file:Suppress("NAMED_ARGUMENTS_NOT_ALLOWED") // KT-22237
package kotlinx.coroutines
import kotlin.test.*
class WithContextTest : TestBase() {
@Test
fun testThrowException() = runTest {
expect(1)
try {
withContext<Unit>(coroutineContext) {
expect(2)
throw AssertionError()
}
} catch (e: AssertionError) {
expect(3)
}
yield()
finish(4)
}
@Test
fun testThrowExceptionFromWrappedContext() = runTest {
expect(1)
try {
withContext<Unit>(wrapperDispatcher(coroutineContext)) {
expect(2)
throw AssertionError()
}
} catch (e: AssertionError) {
expect(3)
}
yield()
finish(4)
}
@Test
fun testSameContextNoSuspend() = runTest {
expect(1)
launch(coroutineContext) { // make sure there is not early dispatch here
finish(5) // after main exits
}
expect(2)
val result = withContext(coroutineContext) { // same context!
expect(3) // still here
"OK".wrap()
}.unwrap()
assertEquals("OK", result)
expect(4)
// will wait for the first coroutine
}
@Test
fun testSameContextWithSuspend() = runTest {
expect(1)
launch(coroutineContext) { // make sure there is not early dispatch here
expect(4)
}
expect(2)
val result = withContext(coroutineContext) { // same context!
expect(3) // still here
yield() // now yields to launch!
expect(5)
"OK".wrap()
}.unwrap()
assertEquals("OK", result)
finish(6)
}
@Test
fun testCancelWithJobNoSuspend() = runTest {
expect(1)
launch(coroutineContext) { // make sure there is not early dispatch to here
finish(6) // after main exits
}
expect(2)
val job = Job()
try {
withContext(coroutineContext + job) {
// same context + new job
expect(3) // still here
job.cancel() // cancel out job!
try {
yield() // shall throw CancellationException
expectUnreached()
} catch (e: CancellationException) {
expect(4)
}
"OK".wrap()
}
expectUnreached()
} catch (e: CancellationException) {
expect(5)
// will wait for the first coroutine
}
}
@Test
fun testCancelWithJobWithSuspend() = runTest(
expected = { it is CancellationException }
) {
expect(1)
launch(coroutineContext) { // make sure there is not early dispatch to here
expect(4)
}
expect(2)
val job = Job()
withContext(coroutineContext + job) { // same context + new job
expect(3) // still here
yield() // now yields to launch!
expect(5)
job.cancel() // cancel out job!
try {
yield() // shall throw CancellationException
expectUnreached()
} catch (e: CancellationException) {
finish(6)
}
"OK".wrap()
}
// still fails, because parent job was cancelled
expectUnreached()
}
@Test
fun testRunCancellableDefault() = runTest(
expected = { it is CancellationException }
) {
val job = Job()
job.cancel() // cancel before it has a chance to run
withContext(job + wrapperDispatcher(coroutineContext)) {
expectUnreached() // will get cancelled
}
}
@Test
fun testRunCancellationUndispatchedVsException() = runTest {
expect(1)
var job: Job? = null
job = launch(start = CoroutineStart.UNDISPATCHED) {
expect(2)
try {
// Same dispatcher, different context
withContext<Unit>(CoroutineName("testRunCancellationUndispatchedVsException")) {
expect(3)
yield() // must suspend
expect(5)
job!!.cancel() // cancel this job _before_ it throws
throw TestException()
}
} catch (e: TestException) {
// must have caught TextException
expect(6)
}
}
expect(4)
yield() // to coroutineScope
finish(7)
}
@Test
fun testRunCancellationDispatchedVsException() = runTest {
expect(1)
var job: Job? = null
job = launch(start = CoroutineStart.UNDISPATCHED) {
expect(2)
try {
// "Different" dispatcher (schedules execution back and forth)
withContext<Unit>(wrapperDispatcher(coroutineContext)) {
expect(4)
yield() // must suspend
expect(6)
job!!.cancel() // cancel this job _before_ it throws
throw TestException()
}
} catch (e: TestException) {
// must have caught TextException
expect(8)
}
}
expect(3)
yield() // withContext is next
expect(5)
yield() // withContext again
expect(7)
yield() // to catch block
finish(9)
}
@Test
fun testRunSelfCancellationWithException() = runTest {
expect(1)
var job: Job? = null
job = launch(Job()) {
try {
expect(3)
withContext<Unit>(wrapperDispatcher(coroutineContext)) {
require(isActive)
expect(5)
job!!.cancel()
require(!isActive)
throw TestException() // but throw an exception
}
} catch (e: Throwable) {
expect(7)
// make sure TestException, not CancellationException is thrown
assertTrue(e is TestException, "Caught $e")
}
}
expect(2)
yield() // to the launched job
expect(4)
yield() // again to the job
expect(6)
yield() // again to exception handler
finish(8)
}
@Test
fun testRunSelfCancellation() = runTest {
expect(1)
var job: Job? = null
job = launch(Job()) {
try {
expect(3)
withContext(wrapperDispatcher(coroutineContext)) {
require(isActive)
expect(5)
job!!.cancel() // cancel itself
require(!isActive)
"OK".wrap()
}
expectUnreached()
} catch (e: Throwable) {
expect(7)
// make sure CancellationException is thrown
assertTrue(e is CancellationException, "Caught $e")
}
}
expect(2)
yield() // to the launched job
expect(4)
yield() // again to the job
expect(6)
yield() // again to exception handler
finish(8)
}
@Test
fun testWithContextScopeFailure() = runTest {
expect(1)
try {
withContext(wrapperDispatcher(coroutineContext)) {
expect(2)
// launch a child that fails
launch {
expect(4)
throw TestException()
}
expect(3)
"OK".wrap()
}
expectUnreached()
} catch (e: TestException) {
// ensure that we can catch exception outside of the scope
expect(5)
}
finish(6)
}
@Test
fun testWithContextChildWaitSameContext() = runTest {
expect(1)
withContext(coroutineContext) {
expect(2)
launch {
// ^^^ schedules to main thread
expect(4) // waits before return
}
expect(3)
"OK".wrap()
}.unwrap()
finish(5)
}
@Test
fun testWithContextChildWaitWrappedContext() = runTest {
expect(1)
withContext(wrapperDispatcher(coroutineContext)) {
expect(2)
launch {
// ^^^ schedules to main thread
expect(4) // waits before return
}
expect(3)
"OK".wrap()
}.unwrap()
finish(5)
}
@Test
fun testIncompleteWithContextState() = runTest {
lateinit var ctxJob: Job
withContext(wrapperDispatcher(coroutineContext)) {
ctxJob = coroutineContext[Job]!!
ctxJob.invokeOnCompletion { }
}
ctxJob.join()
assertTrue(ctxJob.isCompleted)
assertFalse(ctxJob.isActive)
assertFalse(ctxJob.isCancelled)
}
@Test
fun testWithContextCancelledJob() = runTest {
expect(1)
val job = Job()
job.cancel()
try {
withContext(job) {
expectUnreached()
}
} catch (e: CancellationException) {
expect(2)
}
finish(3)
}
@Test
fun testWithContextCancelledThisJob() = runTest(
expected = { it is CancellationException }
) {
coroutineContext.cancel()
withContext(wrapperDispatcher(coroutineContext)) {
expectUnreached()
}
expectUnreached()
}
@Test
fun testSequentialCancellation() = runTest {
val job = launch {
expect(1)
withContext(wrapperDispatcher()) {
expect(2)
}
expectUnreached()
}
yield()
val job2 = launch {
expect(3)
job.cancel()
}
joinAll(job, job2)
finish(4)
}
private class Wrapper(val value: String) : Incomplete {
override val isActive: Boolean
get() = error("")
override val list: NodeList?
get() = error("")
}
private fun String.wrap() = Wrapper(this)
private fun Wrapper.unwrap() = value
}