• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3  */
4 
5 package kotlinx.coroutines.slf4j
6 
7 import kotlinx.coroutines.*
8 import org.junit.*
9 import org.junit.Test
10 import org.slf4j.*
11 import kotlin.coroutines.*
12 import kotlin.test.*
13 
14 class MDCContextTest : TestBase() {
15     @Before
setUpnull16     fun setUp() {
17         MDC.clear()
18     }
19 
20     @After
tearDownnull21     fun tearDown() {
22         MDC.clear()
23     }
24 
25     @Test
<lambda>null26     fun testContextIsNotPassedByDefaultBetweenCoroutines() = runTest {
27         expect(1)
28         MDC.put("myKey", "myValue")
29         // Standalone launch
30         GlobalScope.launch {
31             assertNull(MDC.get("myKey"))
32             expect(2)
33         }.join()
34         finish(3)
35     }
36 
37     @Test
<lambda>null38     fun testContextCanBePassedBetweenCoroutines() = runTest {
39         expect(1)
40         MDC.put("myKey", "myValue")
41         // Scoped launch with MDCContext element
42         launch(MDCContext()) {
43             assertEquals("myValue", MDC.get("myKey"))
44             expect(2)
45         }.join()
46 
47         finish(3)
48     }
49 
50     @Test
<lambda>null51     fun testContextInheritance() = runTest {
52         expect(1)
53         MDC.put("myKey", "myValue")
54         withContext(MDCContext()) {
55             MDC.put("myKey", "myValue2")
56             // Scoped launch with inherited MDContext element
57             launch(Dispatchers.Default) {
58                 assertEquals("myValue", MDC.get("myKey"))
59                 expect(2)
60             }.join()
61 
62             finish(3)
63         }
64         assertEquals("myValue", MDC.get("myKey"))
65     }
66 
67     @Test
testContextPassedWhileOnMainThreadnull68     fun testContextPassedWhileOnMainThread() {
69         MDC.put("myKey", "myValue")
70         // No MDCContext element
71         runBlocking {
72             assertEquals("myValue", MDC.get("myKey"))
73         }
74     }
75 
76     @Test
testContextCanBePassedWhileOnMainThreadnull77     fun testContextCanBePassedWhileOnMainThread() {
78         MDC.put("myKey", "myValue")
79         runBlocking(MDCContext()) {
80             assertEquals("myValue", MDC.get("myKey"))
81         }
82     }
83 
84     @Test
testContextNeededWithOtherContextnull85     fun testContextNeededWithOtherContext() {
86         MDC.put("myKey", "myValue")
87         runBlocking(MDCContext()) {
88             assertEquals("myValue", MDC.get("myKey"))
89         }
90     }
91 
92     @Test
testContextMayBeEmptynull93     fun testContextMayBeEmpty() {
94         runBlocking(MDCContext()) {
95             assertNull(MDC.get("myKey"))
96         }
97     }
98 
99     @Test
<lambda>null100     fun testContextWithContext() = runTest {
101         MDC.put("myKey", "myValue")
102         val mainDispatcher = kotlin.coroutines.coroutineContext[ContinuationInterceptor]!!
103         withContext(Dispatchers.Default + MDCContext()) {
104             assertEquals("myValue", MDC.get("myKey"))
105             assertEquals("myValue", coroutineContext[MDCContext]?.contextMap?.get("myKey"))
106             withContext(mainDispatcher) {
107                 assertEquals("myValue", MDC.get("myKey"))
108             }
109         }
110     }
111 }
112