-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathLog4jDiagnosticContextTest.kt
129 lines (114 loc) · 3.66 KB
/
Log4jDiagnosticContextTest.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines.log4j
import kotlinx.coroutines.*
import org.apache.logging.log4j.ThreadContext
import org.junit.*
import org.junit.Test
import kotlin.coroutines.*
import kotlin.test.*
class Log4jDiagnosticContextTest : TestBase() {
@Before
@After
fun clearThreadContext() {
ThreadContext.clearAll()
}
@Test
fun testContextIsNotPassedByDefaultBetweenCoroutines() = runTest {
expect(1)
ThreadContext.put("myKey", "myValue")
// Standalone launch
GlobalScope.launch {
assertEquals(null, ThreadContext.get("myKey"))
expect(2)
}.join()
finish(3)
}
@Test
fun testImmutableContextContainsOriginalContent() = runTest {
expect(1)
ThreadContext.put("myKey", "myValue")
// Standalone launch
GlobalScope.launch(immutableDiagnosticContext()) {
assertEquals("myValue", ThreadContext.get("myKey"))
expect(2)
}.join()
finish(3)
}
@Test
fun testMutableContextContainsOriginalContent() = runTest {
expect(1)
ThreadContext.put("myKey", "myValue")
// Standalone launch
GlobalScope.launch(MutableDiagnosticContext()) {
assertEquals("myValue", ThreadContext.get("myKey"))
expect(2)
}.join()
finish(3)
}
@Test
fun testContextInheritance() = runTest {
expect(1)
withContext(MutableDiagnosticContext()
.put("myKey", "myValue")
) {
// Update the global ThreadContext. This isn't tied to the CoroutineContext though, so shouldn't get used.
ThreadContext.put("myKey", "myValue2")
// Scoped launch with inherited Log4JThreadContext element
launch(Dispatchers.Default) {
assertEquals("myValue", ThreadContext.get("myKey"))
expect(2)
}.join()
finish(3)
}
assertEquals("myValue", ThreadContext.get("myKey"))
}
@Test
fun testContextPassedWhileOnSameThread() {
ThreadContext.put("myKey", "myValue")
// No ThreadContext element
runBlocking {
assertEquals("myValue", ThreadContext.get("myKey"))
}
}
@Test
fun testImmutableContextMayBeEmpty() {
runBlocking(immutableDiagnosticContext()) {
assertEquals(null, ThreadContext.get("myKey"))
}
}
@Test
fun testContextMayBeEmpty() {
runBlocking(MutableDiagnosticContext()) {
assertEquals(null, ThreadContext.get("myKey"))
}
}
@Test
fun testCoroutineContextWithLoggingContext() = runTest {
val mainDispatcher = kotlin.coroutines.coroutineContext[ContinuationInterceptor]!!
withContext(Dispatchers.Default
+ MutableDiagnosticContext().put("myKey", "myValue")
) {
assertEquals("myValue", ThreadContext.get("myKey"))
withContext(mainDispatcher) {
assertEquals("myValue", ThreadContext.get("myKey"))
}
}
}
@Test
fun testNestedContexts() {
runBlocking(MutableDiagnosticContext().put("key", "value")) {
withContext(MutableDiagnosticContext().put("key", "value2")){
assertEquals("value2", ThreadContext.get("key"))
}
assertEquals("value", ThreadContext.get("key"))
}
}
@Test
fun testAcceptsNullValues() {
runBlocking(MutableDiagnosticContext().put("key", null)) {
assertNull(ThreadContext.get("key"))
}
}
}