File tree 3 files changed +42
-7
lines changed
integration/kotlinx-coroutines-slf4j/src
main/kotlin/kotlinx/coroutines/experimental/slf4j
kotlin/kotlinx/coroutines/experimental/slf4j
3 files changed +42
-7
lines changed Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ import kotlin.coroutines.experimental.CoroutineContext
8
8
9
9
class MDCContext (
10
10
private val context : CoroutineContext ,
11
- private val contextMap : Map <String , String > = MDC .getCopyOfContextMap()
11
+ private val contextMap : Map <String , String >? = MDC .getCopyOfContextMap()
12
12
) : AbstractCoroutineContextElement(ContinuationInterceptor ), ContinuationInterceptor {
13
13
14
14
override fun <T > interceptContinuation (continuation : Continuation <T >): Continuation <T > {
@@ -25,7 +25,10 @@ class MDCContext(
25
25
private inner class Wrapper <T >(private val continuation : Continuation <T >) : Continuation<T> {
26
26
private inline fun wrap (block : () -> Unit ) {
27
27
try {
28
- MDC .setContextMap(contextMap)
28
+ contextMap?.let {
29
+ MDC .setContextMap(contextMap)
30
+ }
31
+
29
32
block()
30
33
} finally {
31
34
MDC .clear()
Original file line number Diff line number Diff line change 1
1
package kotlinx.coroutines.experimental.slf4j
2
2
3
3
import kotlinx.coroutines.experimental.*
4
+ import org.junit.After
5
+ import org.junit.Before
4
6
import org.junit.Test
5
7
import org.slf4j.MDC
6
8
import kotlin.test.assertEquals
9
+ import kotlin.test.assertNull
7
10
8
11
class MDCContextTest : TestBase () {
9
12
13
+ @Before
14
+ fun setUp () {
15
+ MDC .clear()
16
+ }
17
+
18
+ @After
19
+ fun tearDown () {
20
+ MDC .clear()
21
+ }
22
+
10
23
@Test
11
24
fun mdcContextIsNotPassedByDefaultBetweenCoRoutines () = runTest {
12
25
expect(1 )
@@ -60,4 +73,13 @@ class MDCContextTest : TestBase() {
60
73
assertEquals(mdcValue, " myValue" )
61
74
}
62
75
}
76
+
77
+ @Test
78
+ fun mdcContextMayBeEmpty () {
79
+ runBlocking(MDCContext (DefaultDispatcher )) {
80
+ val mdcValue = MDC .get(" myKey" )
81
+
82
+ assertNull(mdcValue)
83
+ }
84
+ }
63
85
}
Original file line number Diff line number Diff line change 1
- <appender name =" CONSOLE" class =" ch.qos.logback.core.ConsoleAppender" >
2
- <layout >
3
- <Pattern >%X{first} %X{last} - %m%n</Pattern >
4
- </layout >
5
- </appender >
1
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2
+ <configuration debug =" false" >
3
+
4
+ <appender name =" CONSOLE" class =" ch.qos.logback.core.ConsoleAppender" >
5
+ <layout >
6
+ <Pattern >%X{first} %X{last} - %m%n</Pattern >
7
+ </layout >
8
+ </appender >
9
+
10
+ <root level =" DEBUG" >
11
+ <appender-ref ref =" CONSOLE" />
12
+ </root >
13
+ </configuration >
14
+
15
+
You can’t perform that action at this time.
0 commit comments