-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathThreadContextElementTest.kt
321 lines (281 loc) · 12.4 KB
/
ThreadContextElementTest.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
package kotlinx.coroutines
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.testing.*
import org.junit.Test
import java.util.*
import java.util.concurrent.*
import kotlin.collections.ArrayList
import kotlin.coroutines.*
import kotlin.test.*
class ThreadContextElementTest : TestBase() {
@Test
fun testExample() = runTest {
val exceptionHandler = coroutineContext[CoroutineExceptionHandler]!!
val mainDispatcher = coroutineContext[ContinuationInterceptor]!!
val mainThread = Thread.currentThread()
val data = MyData()
val element = MyElement(data)
assertNull(myThreadLocal.get())
val job = GlobalScope.launch(element + exceptionHandler) {
assertTrue(mainThread != Thread.currentThread())
assertSame(element, coroutineContext[MyElement])
assertSame(data, myThreadLocal.get())
withContext(mainDispatcher) {
assertSame(mainThread, Thread.currentThread())
assertSame(element, coroutineContext[MyElement])
assertSame(data, myThreadLocal.get())
}
assertTrue(mainThread != Thread.currentThread())
assertSame(element, coroutineContext[MyElement])
assertSame(data, myThreadLocal.get())
}
assertNull(myThreadLocal.get())
job.join()
assertNull(myThreadLocal.get())
}
@Test
fun testUndispatched() = runTest {
val exceptionHandler = coroutineContext[CoroutineExceptionHandler]!!
val data = MyData()
val element = MyElement(data)
val job = GlobalScope.launch(
context = Dispatchers.Default + exceptionHandler + element,
start = CoroutineStart.UNDISPATCHED
) {
assertSame(data, myThreadLocal.get())
yield()
assertSame(data, myThreadLocal.get())
}
assertNull(myThreadLocal.get())
job.join()
assertNull(myThreadLocal.get())
}
@Test
fun testWithContext() = runTest {
expect(1)
newSingleThreadContext("withContext").use {
val data = MyData()
GlobalScope.async(Dispatchers.Default + MyElement(data)) {
assertSame(data, myThreadLocal.get())
expect(2)
val newData = MyData()
GlobalScope.async(it + MyElement(newData)) {
assertSame(newData, myThreadLocal.get())
expect(3)
}.await()
withContext(it + MyElement(newData)) {
assertSame(newData, myThreadLocal.get())
expect(4)
}
GlobalScope.async(it) {
assertNull(myThreadLocal.get())
expect(5)
}.await()
expect(6)
}.await()
}
finish(7)
}
@Test
fun testNonCopyableElementReferenceInheritedOnLaunch() = runTest {
var parentElement: MyElement? = null
var inheritedElement: MyElement? = null
newSingleThreadContext("withContext").use {
withContext(it + MyElement(MyData())) {
parentElement = coroutineContext[MyElement.Key]
launch {
inheritedElement = coroutineContext[MyElement.Key]
}
}
}
assertSame(inheritedElement, parentElement,
"Inner and outer coroutines did not have the same object reference to a" +
" ThreadContextElement that did not override `copyForChildCoroutine()`")
}
@Test
fun testCopyableElementCopiedOnLaunch() = runTest {
var parentElement: CopyForChildCoroutineElement? = null
var inheritedElement: CopyForChildCoroutineElement? = null
newSingleThreadContext("withContext").use {
withContext(it + CopyForChildCoroutineElement(MyData())) {
parentElement = coroutineContext[CopyForChildCoroutineElement.Key]
launch {
inheritedElement = coroutineContext[CopyForChildCoroutineElement.Key]
}
}
}
assertNotSame(inheritedElement, parentElement,
"Inner coroutine did not copy its copyable ThreadContextElement.")
}
@Test
fun testCopyableThreadContextElementImplementsWriteVisibility() = runTest {
newFixedThreadPoolContext(nThreads = 4, name = "withContext").use {
withContext(it + CopyForChildCoroutineElement(MyData())) {
val forBlockData = MyData()
myThreadLocal.setForBlock(forBlockData) {
assertSame(myThreadLocal.get(), forBlockData)
launch {
assertSame(myThreadLocal.get(), forBlockData)
}
launch {
assertSame(myThreadLocal.get(), forBlockData)
// Modify value in child coroutine. Writes to the ThreadLocal and
// the (copied) ThreadLocalElement's memory are not visible to peer or
// ancestor coroutines, so this write is both threadsafe and coroutinesafe.
val innerCoroutineData = MyData()
myThreadLocal.setForBlock(innerCoroutineData) {
assertSame(myThreadLocal.get(), innerCoroutineData)
}
assertSame(myThreadLocal.get(), forBlockData) // Asserts value was restored.
}
launch {
val innerCoroutineData = MyData()
myThreadLocal.setForBlock(innerCoroutineData) {
assertSame(myThreadLocal.get(), innerCoroutineData)
}
assertSame(myThreadLocal.get(), forBlockData)
}
}
assertNull(myThreadLocal.get()) // Asserts value was restored to its origin
}
}
}
class JobCaptor(val capturees: ArrayList<Job> = ArrayList()) : ThreadContextElement<Unit> {
companion object Key : CoroutineContext.Key<MyElement>
override val key: CoroutineContext.Key<*> get() = Key
override fun updateThreadContext(context: CoroutineContext) {
capturees.add(context.job)
}
override fun restoreThreadContext(context: CoroutineContext, oldState: Unit) {
}
}
/**
* For stability of the test, it is important to make sure that
* the parent job actually suspends when calling
* `withContext(dispatcher2 + CoroutineName("dispatched"))`.
*
* Here this requirement is fulfilled by forcing execution on a single thread.
* However, dispatching is performed with two non-equal dispatchers to simulate multithreaded behavior.
*
* Suspend of the parent coroutine [kotlinx.coroutines.DispatchedCoroutine.trySuspend] is out of the control of the test,
* while being executed concurrently with resume of the child coroutine [kotlinx.coroutines.DispatchedCoroutine.tryResume].
*/
@Test
fun testWithContextJobAccess() = runTest {
val executor = Executors.newSingleThreadExecutor()
// Emulate non-equal dispatchers
val executor1 = object : ExecutorService by executor {}
val executor2 = object : ExecutorService by executor {}
val dispatcher1 = executor1.asCoroutineDispatcher()
val dispatcher2 = executor2.asCoroutineDispatcher()
val captor = JobCaptor()
val manuallyCaptured = ArrayList<Job>()
runBlocking(captor + dispatcher1) {
manuallyCaptured += coroutineContext.job
withContext(CoroutineName("undispatched")) {
manuallyCaptured += coroutineContext.job
// Without forcing of single backing thread the code inside `withContext`
// may already complete at the moment when the parent coroutine decides
// whether it needs to suspend or not.
// If the parent coroutine does not need to suspend, no context capture will be called.
withContext(dispatcher2 + CoroutineName("dispatched")) {
manuallyCaptured += coroutineContext.job
}
// Context restored, captured again
manuallyCaptured += coroutineContext.job
}
// Context restored, captured again
manuallyCaptured += coroutineContext.job
}
assertEquals(manuallyCaptured, captor.capturees)
executor.shutdownNow()
}
@Test
fun testThreadLocalFlowOn() = runTest {
val myData = MyData()
myThreadLocal.set(myData)
expect(1)
flow {
assertEquals(myData, myThreadLocal.get())
emit(1)
}
.flowOn(myThreadLocal.asContextElement() + Dispatchers.Default)
.single()
myThreadLocal.set(null)
finish(2)
}
}
class MyData
// declare thread local variable holding MyData
private val myThreadLocal = ThreadLocal<MyData?>()
// declare context element holding MyData
class MyElement(val data: MyData) : ThreadContextElement<MyData?> {
// declare companion object for a key of this element in coroutine context
companion object Key : CoroutineContext.Key<MyElement>
// provide the key of the corresponding context element
override val key: CoroutineContext.Key<MyElement>
get() = Key
// this is invoked before coroutine is resumed on current thread
override fun updateThreadContext(context: CoroutineContext): MyData? {
val oldState = myThreadLocal.get()
myThreadLocal.set(data)
return oldState
}
// this is invoked after coroutine has suspended on current thread
override fun restoreThreadContext(context: CoroutineContext, oldState: MyData?) {
myThreadLocal.set(oldState)
}
}
/**
* A [ThreadContextElement] that implements copy semantics in [copyForChild].
*/
class CopyForChildCoroutineElement(val data: MyData?) : CopyableThreadContextElement<MyData?> {
companion object Key : CoroutineContext.Key<CopyForChildCoroutineElement>
override val key: CoroutineContext.Key<CopyForChildCoroutineElement>
get() = Key
override fun updateThreadContext(context: CoroutineContext): MyData? {
val oldState = myThreadLocal.get()
myThreadLocal.set(data)
return oldState
}
override fun mergeForChild(overwritingElement: CoroutineContext.Element): CopyForChildCoroutineElement {
TODO("Not used in tests")
}
override fun restoreThreadContext(context: CoroutineContext, oldState: MyData?) {
myThreadLocal.set(oldState)
}
/**
* At coroutine launch time, the _current value of the ThreadLocal_ is inherited by the new
* child coroutine, and that value is copied to a new, unique, ThreadContextElement memory
* reference for the child coroutine to use uniquely.
*
* n.b. the value copied to the child must be the __current value of the ThreadLocal__ and not
* the value initially passed to the ThreadContextElement in order to reflect writes made to the
* ThreadLocal between coroutine resumption and the child coroutine launch point. Those writes
* will be reflected in the parent coroutine's [CopyForChildCoroutineElement] when it yields the
* thread and calls [restoreThreadContext].
*/
override fun copyForChild(): CopyForChildCoroutineElement {
return CopyForChildCoroutineElement(myThreadLocal.get())
}
}
/**
* Calls [block], setting the value of [this] [ThreadLocal] for the duration of [block].
*
* When a [CopyForChildCoroutineElement] for `this` [ThreadLocal] is used within a
* [CoroutineContext], a ThreadLocal set this way will have the "correct" value expected lexically
* at every statement reached, whether that statement is reached immediately, across suspend and
* redispatch within one coroutine, or within a child coroutine. Writes made to the `ThreadLocal`
* by child coroutines will not be visible to the parent coroutine. Writes made to the `ThreadLocal`
* by the parent coroutine _after_ launching a child coroutine will not be visible to that child
* coroutine.
*/
private inline fun <ThreadLocalT, OutputT> ThreadLocal<ThreadLocalT>.setForBlock(
value: ThreadLocalT,
crossinline block: () -> OutputT
) {
val priorValue = get()
set(value)
block()
set(priorValue)
}