Skip to content

Commit 8abef84

Browse files
committed
Move tests to common
1 parent 179d4a0 commit 8abef84

File tree

3 files changed

+24
-91
lines changed

3 files changed

+24
-91
lines changed

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

+24-1
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,40 @@ class ThreadContextElementTest : TestBase() {
3737
finish(2)
3838
}
3939

40+
@Test
41+
fun testDispatched() = runTest {
42+
val mainDispatcher = coroutineContext[ContinuationInterceptor]!!
43+
val data = MyData()
44+
val element = MyElement(data)
45+
assertNull(threadContextElementThreadLocal.get())
46+
val job = launch(Dispatchers.Default + element) {
47+
assertSame(element, coroutineContext[MyElement])
48+
assertSame(data, threadContextElementThreadLocal.get())
49+
withContext(mainDispatcher) {
50+
assertSame(element, coroutineContext[MyElement])
51+
assertSame(data, threadContextElementThreadLocal.get())
52+
}
53+
assertSame(element, coroutineContext[MyElement])
54+
assertSame(data, threadContextElementThreadLocal.get())
55+
}
56+
assertNull(threadContextElementThreadLocal.get())
57+
job.join()
58+
assertNull(threadContextElementThreadLocal.get())
59+
}
60+
4061
@Test
4162
fun testUndispatched() = runTest {
4263
val exceptionHandler = coroutineContext[CoroutineExceptionHandler]!!
4364
val data = MyData()
4465
val element = MyElement(data)
45-
val job = GlobalScope.launch(
66+
val job = launch(
4667
context = Dispatchers.Default + exceptionHandler + element,
4768
start = CoroutineStart.UNDISPATCHED
4869
) {
70+
assertSame(element, coroutineContext[MyElement])
4971
assertSame(data, threadContextElementThreadLocal.get())
5072
yield()
73+
assertSame(element, coroutineContext[MyElement])
5174
assertSame(data, threadContextElementThreadLocal.get())
5275
}
5376
assertNull(threadContextElementThreadLocal.get())

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

-44
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,6 @@ import kotlinx.coroutines.flow.*
88

99
class ThreadContextElementJvmTest : TestBase() {
1010

11-
@Test
12-
fun testExample() = runTest {
13-
val exceptionHandler = coroutineContext[CoroutineExceptionHandler]!!
14-
val mainDispatcher = coroutineContext[ContinuationInterceptor]!!
15-
val mainThread = Thread.currentThread()
16-
val data = MyData()
17-
val element = MyElement(data)
18-
assertNull(threadContextElementThreadLocal.get())
19-
val job = GlobalScope.launch(element + exceptionHandler) {
20-
assertTrue(mainThread != Thread.currentThread())
21-
assertSame(element, coroutineContext[MyElement])
22-
assertSame(data, threadContextElementThreadLocal.get())
23-
withContext(mainDispatcher) {
24-
assertSame(mainThread, Thread.currentThread())
25-
assertSame(element, coroutineContext[MyElement])
26-
assertSame(data, threadContextElementThreadLocal.get())
27-
}
28-
assertTrue(mainThread != Thread.currentThread())
29-
assertSame(element, coroutineContext[MyElement])
30-
assertSame(data, threadContextElementThreadLocal.get())
31-
}
32-
assertNull(threadContextElementThreadLocal.get())
33-
job.join()
34-
assertNull(threadContextElementThreadLocal.get())
35-
}
36-
37-
@Test
38-
fun testUndispatched() = runTest {
39-
val exceptionHandler = coroutineContext[CoroutineExceptionHandler]!!
40-
val data = MyData()
41-
val element = MyElement(data)
42-
val job = GlobalScope.launch(
43-
context = Dispatchers.Default + exceptionHandler + element,
44-
start = CoroutineStart.UNDISPATCHED
45-
) {
46-
assertSame(data, threadContextElementThreadLocal.get())
47-
yield()
48-
assertSame(data, threadContextElementThreadLocal.get())
49-
}
50-
assertNull(threadContextElementThreadLocal.get())
51-
job.join()
52-
assertNull(threadContextElementThreadLocal.get())
53-
}
54-
5511
@Test
5612
fun testWithContext() = runTest {
5713
expect(1)

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

-46
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,9 @@ package kotlinx.coroutines
33
import kotlinx.coroutines.testing.*
44
import kotlin.coroutines.*
55
import kotlin.test.*
6-
import kotlin.native.concurrent.*
76

87
class ThreadContextElementNativeTest : TestBase() {
98

10-
@OptIn(ObsoleteWorkersApi::class)
11-
@Test
12-
fun testExample() = runTest {
13-
val exceptionHandler = coroutineContext[CoroutineExceptionHandler]!!
14-
val mainDispatcher = coroutineContext[ContinuationInterceptor]!!
15-
val mainThread = Worker.current.id
16-
val data = MyData()
17-
val element = MyElement(data)
18-
assertNull(threadContextElementThreadLocal.get())
19-
val job = GlobalScope.launch(element + exceptionHandler) {
20-
assertTrue(mainThread != Worker.current.id)
21-
assertSame(element, coroutineContext[MyElement])
22-
assertSame(data, threadContextElementThreadLocal.get())
23-
withContext(mainDispatcher) {
24-
assertSame(mainThread, Worker.current.id)
25-
assertSame(element, coroutineContext[MyElement])
26-
assertSame(data, threadContextElementThreadLocal.get())
27-
}
28-
assertTrue(mainThread != Worker.current.id)
29-
assertSame(element, coroutineContext[MyElement])
30-
assertSame(data, threadContextElementThreadLocal.get())
31-
}
32-
assertNull(threadContextElementThreadLocal.get())
33-
job.join()
34-
assertNull(threadContextElementThreadLocal.get())
35-
}
36-
37-
@Test
38-
fun testUndispatched() = runTest {
39-
val exceptionHandler = coroutineContext[CoroutineExceptionHandler]!!
40-
val data = MyData()
41-
val element = MyElement(data)
42-
val job = GlobalScope.launch(
43-
context = Dispatchers.Default + exceptionHandler + element,
44-
start = CoroutineStart.UNDISPATCHED
45-
) {
46-
assertSame(data, threadContextElementThreadLocal.get())
47-
yield()
48-
assertSame(data, threadContextElementThreadLocal.get())
49-
}
50-
assertNull(threadContextElementThreadLocal.get())
51-
job.join()
52-
assertNull(threadContextElementThreadLocal.get())
53-
}
54-
559
@Test
5610
fun testWithContext() = runTest {
5711
expect(1)

0 commit comments

Comments
 (0)