forked from Kotlin/kotlinx.coroutines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadContextElementTest.common.kt
127 lines (111 loc) · 3.47 KB
/
ThreadContextElementTest.common.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
/*
* Copyright 2016-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines
import kotlin.coroutines.*
import kotlin.test.*
class ThreadContextElementCommonTest : TestBase() {
interface TestThreadContextElement : ThreadContextElement<Int> {
companion object Key : CoroutineContext.Key<TestThreadContextElement>
}
@Test
fun updatesAndRestores() = runTest {
expect(1)
var update = 0
var restore = 0
val threadContextElement = object : TestThreadContextElement {
override fun updateThreadContext(context: CoroutineContext): Int {
update++
return 0
}
override fun restoreThreadContext(context: CoroutineContext, oldState: Int) {
restore++
}
override val key: CoroutineContext.Key<*>
get() = TestThreadContextElement.Key
}
launch(Dispatchers.Unconfined + threadContextElement) {
assertEquals(1, update)
assertEquals(0, restore)
}
assertEquals(1, update)
assertEquals(1, restore)
finish(2)
}
class TestThreadContextIntElement(
val update: () -> Int,
val restore: (Int) -> Unit
) : TestThreadContextElement {
override val key: CoroutineContext.Key<*>
get() = TestThreadContextElement.Key
override fun updateThreadContext(context: CoroutineContext): Int {
return update()
}
override fun restoreThreadContext(context: CoroutineContext, oldState: Int) {
restore(oldState)
}
}
@Test
fun twoCoroutinesUpdateAndRestore() = runTest {
expect(1)
var state = 0
var updateA = 0
var restoreA = 0
var updateB = 0
var restoreB = 0
val lock = Job()
println("Launch A")
val jobA = launch(Dispatchers.Unconfined + TestThreadContextIntElement(
update = {
updateA++
state = 10; 0
},
restore = {
restoreA++
state = it
}
)) {
println("A started")
assertEquals(1, updateA)
assertEquals(10, state)
println("A lock reached")
lock.join()
assertEquals(1, restoreA)
assertEquals(1, updateB)
assertEquals(1, restoreB)
assertEquals(2, updateA)
println("A resumed")
assertEquals(10, state)
println("A completes")
}
println("Launch B")
launch(Dispatchers.Unconfined + TestThreadContextIntElement(
update = {
updateB++
state = 20; 0
},
restore = {
restoreB++
state = it
}
)) {
println("B started")
assertEquals(1, updateB)
assertEquals(20, state)
println("B lock complete")
lock.complete()
println("B wait join A")
jobA.join()
assertEquals(2, updateB)
assertEquals(1, restoreB)
assertEquals(2, updateA)
assertEquals(2, restoreA)
println("B resumed")
assertEquals(20, state)
println("B completes")
}
println("All complete")
assertEquals(0, state)
finish(2)
}
}