@@ -9,7 +9,7 @@ import java.util.concurrent.Executors
99import kotlin.coroutines.*
1010import kotlin.test.*
1111
12- class ThreadContextElementTest : TestBase () {
12+ class ThreadContextElementJvmTest : TestBase () {
1313
1414 @Test
1515 fun testExample () = runTest {
@@ -18,23 +18,23 @@ class ThreadContextElementTest : TestBase() {
1818 val mainThread = Thread .currentThread()
1919 val data = MyData ()
2020 val element = MyElement (data)
21- assertNull(myThreadLocal .get())
21+ assertNull(threadContextElementThreadLocal .get())
2222 val job = GlobalScope .launch(element + exceptionHandler) {
2323 assertTrue(mainThread != Thread .currentThread())
2424 assertSame(element, coroutineContext[MyElement ])
25- assertSame(data, myThreadLocal .get())
25+ assertSame(data, threadContextElementThreadLocal .get())
2626 withContext(mainDispatcher) {
2727 assertSame(mainThread, Thread .currentThread())
2828 assertSame(element, coroutineContext[MyElement ])
29- assertSame(data, myThreadLocal .get())
29+ assertSame(data, threadContextElementThreadLocal .get())
3030 }
3131 assertTrue(mainThread != Thread .currentThread())
3232 assertSame(element, coroutineContext[MyElement ])
33- assertSame(data, myThreadLocal .get())
33+ assertSame(data, threadContextElementThreadLocal .get())
3434 }
35- assertNull(myThreadLocal .get())
35+ assertNull(threadContextElementThreadLocal .get())
3636 job.join()
37- assertNull(myThreadLocal .get())
37+ assertNull(threadContextElementThreadLocal .get())
3838 }
3939
4040 @Test
@@ -46,13 +46,13 @@ class ThreadContextElementTest : TestBase() {
4646 context = Dispatchers .Default + exceptionHandler + element,
4747 start = CoroutineStart .UNDISPATCHED
4848 ) {
49- assertSame(data, myThreadLocal .get())
49+ assertSame(data, threadContextElementThreadLocal .get())
5050 yield ()
51- assertSame(data, myThreadLocal .get())
51+ assertSame(data, threadContextElementThreadLocal .get())
5252 }
53- assertNull(myThreadLocal .get())
53+ assertNull(threadContextElementThreadLocal .get())
5454 job.join()
55- assertNull(myThreadLocal .get())
55+ assertNull(threadContextElementThreadLocal .get())
5656 }
5757
5858 @Test
@@ -61,22 +61,22 @@ class ThreadContextElementTest : TestBase() {
6161 newSingleThreadContext(" withContext" ).use {
6262 val data = MyData ()
6363 GlobalScope .async(Dispatchers .Default + MyElement (data)) {
64- assertSame(data, myThreadLocal .get())
64+ assertSame(data, threadContextElementThreadLocal .get())
6565 expect(2 )
6666
6767 val newData = MyData ()
6868 GlobalScope .async(it + MyElement (newData)) {
69- assertSame(newData, myThreadLocal .get())
69+ assertSame(newData, threadContextElementThreadLocal .get())
7070 expect(3 )
7171 }.await()
7272
7373 withContext(it + MyElement (newData)) {
74- assertSame(newData, myThreadLocal .get())
74+ assertSame(newData, threadContextElementThreadLocal .get())
7575 expect(4 )
7676 }
7777
7878 GlobalScope .async(it) {
79- assertNull(myThreadLocal .get())
79+ assertNull(threadContextElementThreadLocal .get())
8080 expect(5 )
8181 }.await()
8282
@@ -129,31 +129,31 @@ class ThreadContextElementTest : TestBase() {
129129 newFixedThreadPoolContext(nThreads = 4 , name = " withContext" ).use {
130130 withContext(it + CopyForChildCoroutineElement (MyData ())) {
131131 val forBlockData = MyData ()
132- myThreadLocal .setForBlock(forBlockData) {
133- assertSame(myThreadLocal .get(), forBlockData)
132+ threadContextElementThreadLocal .setForBlock(forBlockData) {
133+ assertSame(threadContextElementThreadLocal .get(), forBlockData)
134134 launch {
135- assertSame(myThreadLocal .get(), forBlockData)
135+ assertSame(threadContextElementThreadLocal .get(), forBlockData)
136136 }
137137 launch {
138- assertSame(myThreadLocal .get(), forBlockData)
138+ assertSame(threadContextElementThreadLocal .get(), forBlockData)
139139 // Modify value in child coroutine. Writes to the ThreadLocal and
140140 // the (copied) ThreadLocalElement's memory are not visible to peer or
141141 // ancestor coroutines, so this write is both threadsafe and coroutinesafe.
142142 val innerCoroutineData = MyData ()
143- myThreadLocal .setForBlock(innerCoroutineData) {
144- assertSame(myThreadLocal .get(), innerCoroutineData)
143+ threadContextElementThreadLocal .setForBlock(innerCoroutineData) {
144+ assertSame(threadContextElementThreadLocal .get(), innerCoroutineData)
145145 }
146- assertSame(myThreadLocal .get(), forBlockData) // Asserts value was restored.
146+ assertSame(threadContextElementThreadLocal .get(), forBlockData) // Asserts value was restored.
147147 }
148148 launch {
149149 val innerCoroutineData = MyData ()
150- myThreadLocal .setForBlock(innerCoroutineData) {
151- assertSame(myThreadLocal .get(), innerCoroutineData)
150+ threadContextElementThreadLocal .setForBlock(innerCoroutineData) {
151+ assertSame(threadContextElementThreadLocal .get(), innerCoroutineData)
152152 }
153- assertSame(myThreadLocal .get(), forBlockData)
153+ assertSame(threadContextElementThreadLocal .get(), forBlockData)
154154 }
155155 }
156- assertNull(myThreadLocal .get()) // Asserts value was restored to its origin
156+ assertNull(threadContextElementThreadLocal .get()) // Asserts value was restored to its origin
157157 }
158158 }
159159 }
@@ -238,58 +238,31 @@ class ThreadContextElementTest : TestBase() {
238238 @Test
239239 fun testThreadLocalFlowOn () = runTest {
240240 val myData = MyData ()
241- myThreadLocal .set(myData)
241+ threadContextElementThreadLocal .set(myData)
242242 expect(1 )
243243 flow {
244- assertEquals(myData, myThreadLocal .get())
244+ assertEquals(myData, threadContextElementThreadLocal .get())
245245 emit(1 )
246246 }
247- .flowOn(myThreadLocal .asContextElement() + Dispatchers .Default )
247+ .flowOn(threadContextElementThreadLocal .asContextElement() + Dispatchers .Default )
248248 .single()
249- myThreadLocal .set(null )
249+ threadContextElementThreadLocal .set(null )
250250 finish(2 )
251251 }
252252}
253253
254- class MyData
255-
256- // declare thread local variable holding MyData
257- private val myThreadLocal = ThreadLocal <MyData ?>()
258-
259- // declare context element holding MyData
260- class MyElement (val data : MyData ) : ThreadContextElement<MyData?> {
261- // declare companion object for a key of this element in coroutine context
262- companion object Key : CoroutineContext.Key<MyElement>
263-
264- // provide the key of the corresponding context element
265- override val key: CoroutineContext .Key <MyElement >
266- get() = Key
267-
268- // this is invoked before coroutine is resumed on current thread
269- override fun updateThreadContext (context : CoroutineContext ): MyData ? {
270- val oldState = myThreadLocal.get()
271- myThreadLocal.set(data)
272- return oldState
273- }
274-
275- // this is invoked after coroutine has suspended on current thread
276- override fun restoreThreadContext (context : CoroutineContext , oldState : MyData ? ) {
277- myThreadLocal.set(oldState)
278- }
279- }
280-
281254/* *
282255 * A [ThreadContextElement] that implements copy semantics in [copyForChild].
283256 */
284- class CopyForChildCoroutineElement (val data : MyData ? ) : CopyableThreadContextElement<MyData?> {
257+ internal class CopyForChildCoroutineElement (val data : MyData ? ) : CopyableThreadContextElement<MyData?> {
285258 companion object Key : CoroutineContext.Key<CopyForChildCoroutineElement>
286259
287260 override val key: CoroutineContext .Key <CopyForChildCoroutineElement >
288261 get() = Key
289262
290263 override fun updateThreadContext (context : CoroutineContext ): MyData ? {
291- val oldState = myThreadLocal .get()
292- myThreadLocal .set(data)
264+ val oldState = threadContextElementThreadLocal .get()
265+ threadContextElementThreadLocal .set(data)
293266 return oldState
294267 }
295268
@@ -298,7 +271,7 @@ class CopyForChildCoroutineElement(val data: MyData?) : CopyableThreadContextEle
298271 }
299272
300273 override fun restoreThreadContext (context : CoroutineContext , oldState : MyData ? ) {
301- myThreadLocal .set(oldState)
274+ threadContextElementThreadLocal .set(oldState)
302275 }
303276
304277 /* *
@@ -313,7 +286,7 @@ class CopyForChildCoroutineElement(val data: MyData?) : CopyableThreadContextEle
313286 * thread and calls [restoreThreadContext].
314287 */
315288 override fun copyForChild (): CopyForChildCoroutineElement {
316- return CopyForChildCoroutineElement (myThreadLocal .get())
289+ return CopyForChildCoroutineElement (threadContextElementThreadLocal .get())
317290 }
318291}
319292
0 commit comments