-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathDispatcherKeyTest.kt
52 lines (44 loc) · 1.99 KB
/
DispatcherKeyTest.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
/*
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines
import org.junit.Test
import kotlin.coroutines.*
import kotlin.test.*
@UseExperimental(ExperimentalStdlibApi::class)
class DispatcherKeyTest : TestBase() {
companion object CustomInterceptor : AbstractCoroutineContextElement(ContinuationInterceptor),
ContinuationInterceptor {
override fun <T> interceptContinuation(continuation: Continuation<T>): Continuation<T> {
return continuation
}
}
private val name = CoroutineName("test")
@Test
fun testDispatcher() {
val context = name + CustomInterceptor
assertNull(context[CoroutineDispatcher])
assertSame(CustomInterceptor, context[ContinuationInterceptor])
val updated = context + Dispatchers.Main
val result: CoroutineDispatcher? = updated[CoroutineDispatcher]
assertSame(Dispatchers.Main, result)
assertSame(Dispatchers.Main, updated[ContinuationInterceptor])
assertEquals(name, updated.minusKey(CoroutineDispatcher))
assertEquals(name, updated.minusKey(ContinuationInterceptor))
}
@Test
fun testExecutorCoroutineDispatcher() {
val context = name + CustomInterceptor
assertNull(context[ExecutorCoroutineDispatcher])
val updated = context + Dispatchers.Main
assertNull(updated[ExecutorCoroutineDispatcher])
val executor = Dispatchers.Default
val updated2 = updated + executor
assertSame(Dispatchers.Default, updated2[ContinuationInterceptor])
assertSame(Dispatchers.Default, updated2[CoroutineDispatcher])
assertSame(Dispatchers.Default as ExecutorCoroutineDispatcher, updated2[ExecutorCoroutineDispatcher])
assertEquals(name, updated2.minusKey(ContinuationInterceptor))
assertEquals(name, updated2.minusKey(CoroutineDispatcher))
assertEquals(name, updated2.minusKey(ExecutorCoroutineDispatcher))
}
}