Skip to content

Commit 7b9c927

Browse files
committed
Clean up tests (and find a bug in the JS implementation?)
1 parent cfde2d1 commit 7b9c927

6 files changed

+56
-68
lines changed

Diff for: kotlinx-coroutines-core/common/test/CommonThreadContextElementTest.kt renamed to kotlinx-coroutines-core/common/test/ThreadContextElementTest.kt

+38-5
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ import kotlin.coroutines.*
55
import kotlin.test.*
66
import kotlinx.coroutines.internal.*
77

8-
class CommonThreadContextElementTest : TestBase() {
8+
class ThreadContextElementTest : TestBase() {
99
interface TestThreadContextElement : ThreadContextElement<Int> {
1010
companion object Key : CoroutineContext.Key<TestThreadContextElement>
1111
}
1212

1313
@Test
14-
fun updatesAndRestores() = runTest {
15-
expect(1)
14+
fun testUpdatesAndRestores() = runTest {
1615
var updateCount = 0
1716
var restoreCount = 0
1817
val threadContextElement = object : TestThreadContextElement {
@@ -34,7 +33,6 @@ class CommonThreadContextElementTest : TestBase() {
3433
}
3534
assertEquals(1, updateCount)
3635
assertEquals(1, restoreCount)
37-
finish(2)
3836
}
3937

4038
@Test
@@ -77,6 +75,42 @@ class CommonThreadContextElementTest : TestBase() {
7775
job.join()
7876
assertNull(threadContextElementThreadLocal.get())
7977
}
78+
79+
@Test
80+
fun testWithContextJobAccess() = runTest {
81+
val captor = JobCaptor()
82+
val manuallyCaptured = ArrayList<Job>()
83+
withContext(captor) {
84+
manuallyCaptured += coroutineContext.job
85+
withContext(CoroutineName("undispatched")) {
86+
manuallyCaptured += coroutineContext.job
87+
withContext(Dispatchers.Default) {
88+
manuallyCaptured += coroutineContext.job
89+
}
90+
// Context restored, captured again
91+
manuallyCaptured += coroutineContext.job
92+
}
93+
// Context restored, captured again
94+
manuallyCaptured += coroutineContext.job
95+
}
96+
assertEquals(manuallyCaptured, captor.capturees)
97+
}
98+
}
99+
100+
private class JobCaptor() : ThreadContextElement<Unit> {
101+
102+
val capturees: MutableList<Job> = mutableListOf()
103+
104+
companion object Key : CoroutineContext.Key<MyElement>
105+
106+
override val key: CoroutineContext.Key<*> get() = Key
107+
108+
override fun updateThreadContext(context: CoroutineContext) {
109+
capturees.add(context.job)
110+
}
111+
112+
override fun restoreThreadContext(context: CoroutineContext, oldState: Unit) {
113+
}
80114
}
81115

82116
internal class MyData
@@ -105,4 +139,3 @@ internal class MyElement(val data: MyData) : ThreadContextElement<MyData?> {
105139
threadContextElementThreadLocal.set(oldState)
106140
}
107141
}
108-

Diff for: kotlinx-coroutines-core/common/test/ThreadContextMutableCopiesTest.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import kotlin.test.Test
1111
import kotlin.test.assertEquals
1212
import kotlin.test.assertNotSame
1313

14-
class CommonThreadContextMutableCopiesTest : TestBase() {
14+
class ThreadContextMutableCopiesTest : TestBase() {
1515
companion object {
1616
internal val threadLocalData = commonThreadLocal<MutableList<String>>(Symbol("ThreadLocalData")).also {
1717
it.set(mutableListOf())
@@ -156,4 +156,4 @@ class CommonThreadContextMutableCopiesTest : TestBase() {
156156
.single()
157157
finish(2)
158158
}
159-
}
159+
}

Diff for: kotlinx-coroutines-core/concurrent/test/ThreadContextElementTest.kt renamed to kotlinx-coroutines-core/concurrent/test/ThreadContextElementConcurrentTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package kotlinx.coroutines
33
import kotlinx.coroutines.testing.*
44
import kotlin.test.*
55

6-
class ThreadContextElementTest : TestBase() {
6+
class ThreadContextElementConcurrentTest : TestBase() {
77
@Test
88
fun testWithContext() = runTest {
99
expect(1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package kotlinx.coroutines
2+
3+
import kotlinx.coroutines.testing.*
4+
import kotlin.test.*
5+
6+
class ThreadContextMutableCopiesConcurrentTest : TestBase() {
7+
@Test
8+
fun testDataIsCopiedForRunBlocking() = runTest {
9+
val root = ThreadContextMutableCopiesTest.MyMutableElement(ArrayList())
10+
val originalData = root.mutableData
11+
runBlocking(root) {
12+
assertNotSame(originalData, ThreadContextMutableCopiesTest.Companion.threadLocalData.get())
13+
}
14+
}
15+
}

Diff for: kotlinx-coroutines-core/jvm/test/ThreadContextMutableCopiesTest.kt

-17
This file was deleted.

Diff for: kotlinx-coroutines-core/native/test/ThreadContextElementNativeTest.kt

-43
This file was deleted.

0 commit comments

Comments
 (0)