1
+ package kotlinx.coroutines.experimental.slf4j
2
+
3
+ import kotlinx.coroutines.experimental.*
4
+ import org.junit.After
5
+ import org.junit.Before
6
+ import org.junit.Test
7
+ import org.slf4j.MDC
8
+ import kotlin.test.assertEquals
9
+ import kotlin.test.assertNull
10
+
11
+ class MDCContextTest : TestBase () {
12
+
13
+ @Before
14
+ fun setUp () {
15
+ MDC .clear()
16
+ }
17
+
18
+ @After
19
+ fun tearDown () {
20
+ MDC .clear()
21
+ }
22
+
23
+ @Test
24
+ fun mdcContextIsNotPassedByDefaultBetweenCoRoutines () = runTest {
25
+ expect(1 )
26
+ MDC .put(" myKey" , " myValue" )
27
+
28
+ launch {
29
+ val mdcValue = MDC .get(" myKey" )
30
+ check(mdcValue == null )
31
+ expect(2 )
32
+ }.join()
33
+
34
+ expect(3 )
35
+
36
+ finish(4 )
37
+ }
38
+
39
+ @Test
40
+ fun mdcContextCanBePassedBetweenCoRoutines () = runTest {
41
+ expect(1 )
42
+ MDC .put(" myKey" , " myValue" )
43
+
44
+ launch(MDCContext (DefaultDispatcher )) {
45
+ val mdcValue = MDC .get(" myKey" )
46
+ check(mdcValue == " myValue" )
47
+ expect(2 )
48
+ }.join()
49
+
50
+ expect(3 )
51
+
52
+ finish(4 )
53
+ }
54
+
55
+ @Test
56
+ fun mdcContextNotNeededWhileOnMainThread () {
57
+ MDC .put(" myKey" , " myValue" )
58
+
59
+ runBlocking {
60
+ val mdcValue = MDC .get(" myKey" )
61
+
62
+ assertEquals(mdcValue, " myValue" )
63
+ }
64
+ }
65
+
66
+ @Test
67
+ fun mdcContextNeededWithOtherContext () {
68
+ MDC .put(" myKey" , " myValue" )
69
+
70
+ runBlocking(MDCContext (DefaultDispatcher )) {
71
+ val mdcValue = MDC .get(" myKey" )
72
+
73
+ assertEquals(mdcValue, " myValue" )
74
+ }
75
+ }
76
+
77
+ @Test
78
+ fun mdcContextMayBeEmpty () {
79
+ runBlocking(MDCContext (DefaultDispatcher )) {
80
+ val mdcValue = MDC .get(" myKey" )
81
+
82
+ assertNull(mdcValue)
83
+ }
84
+ }
85
+ }
0 commit comments