• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3  */
4 
5 package kotlinx.coroutines
6 
7 import org.junit.Test
8 import kotlin.coroutines.*
9 import kotlin.test.*
10 
11 @OptIn(ExperimentalStdlibApi::class)
12 class DispatcherKeyTest : TestBase() {
13 
14     companion object CustomInterceptor : AbstractCoroutineContextElement(ContinuationInterceptor),
15         ContinuationInterceptor {
interceptContinuationnull16         override fun <T> interceptContinuation(continuation: Continuation<T>): Continuation<T> {
17             return continuation
18         }
19     }
20 
21     private val name = CoroutineName("test")
22 
23     @Test
testDispatchernull24     fun testDispatcher() {
25         val context = name + CustomInterceptor
26         assertNull(context[CoroutineDispatcher])
27         assertSame(CustomInterceptor, context[ContinuationInterceptor])
28 
29         val updated = context + Dispatchers.Main
30         val result: CoroutineDispatcher? = updated[CoroutineDispatcher]
31         assertSame(Dispatchers.Main, result)
32         assertSame(Dispatchers.Main, updated[ContinuationInterceptor])
33         assertEquals(name, updated.minusKey(CoroutineDispatcher))
34         assertEquals(name, updated.minusKey(ContinuationInterceptor))
35     }
36 
37     @Test
testExecutorCoroutineDispatchernull38     fun testExecutorCoroutineDispatcher() {
39         val context = name + CustomInterceptor
40         assertNull(context[ExecutorCoroutineDispatcher])
41         val updated = context + Dispatchers.Main
42         assertNull(updated[ExecutorCoroutineDispatcher])
43         val executor = Dispatchers.Default
44         val updated2 = updated + executor
45         assertSame(Dispatchers.Default, updated2[ContinuationInterceptor])
46         assertSame(Dispatchers.Default, updated2[CoroutineDispatcher])
47         assertSame(Dispatchers.Default as ExecutorCoroutineDispatcher, updated2[ExecutorCoroutineDispatcher])
48         assertEquals(name, updated2.minusKey(ContinuationInterceptor))
49         assertEquals(name, updated2.minusKey(CoroutineDispatcher))
50         assertEquals(name, updated2.minusKey(ExecutorCoroutineDispatcher))
51     }
52 }
53