@@ -53,6 +53,10 @@ class ThreadContextTest {
53
53
assertNull(ContextMap [" myKey" ])
54
54
assertTrue(ContextStack .empty)
55
55
}.join()
56
+ GlobalScope .launch(loggingContext()) {
57
+ assertNull(ContextMap [" myKey" ])
58
+ assertTrue(ContextStack .empty)
59
+ }.join()
56
60
}
57
61
58
62
@DelicateCoroutinesApi
@@ -65,6 +69,10 @@ class ThreadContextTest {
65
69
assertEquals(" myValue" , ContextMap [" myKey" ])
66
70
assertEquals(" test" , ContextStack .peek())
67
71
}.join()
72
+ GlobalScope .launch(additionalLoggingContext()) {
73
+ assertEquals(" myValue" , ContextMap [" myKey" ])
74
+ assertEquals(" test" , ContextStack .peek())
75
+ }.join()
68
76
}
69
77
70
78
@Test
@@ -75,11 +83,37 @@ class ThreadContextTest {
75
83
withContext(CoroutineThreadContext ()) {
76
84
ContextMap [" myKey" ] = " myValue2"
77
85
ContextStack .push(" test2" )
78
- // Scoped launch with inherited MDContext element
86
+ // Scoped launch with non- inherited MDContext element
79
87
launch(Dispatchers .Default ) {
80
88
assertEquals(" myValue" , ContextMap [" myKey" ])
81
89
assertEquals(" test" , ContextStack .peek())
82
90
}
91
+ // Scoped launch with non-inherited MDContext element
92
+ launch(Dispatchers .Default + loggingContext()) {
93
+ assertTrue(ContextMap .empty)
94
+ assertTrue(ContextStack .empty)
95
+ }
96
+ // Scoped launch with non-inherited MDContext element
97
+ launch(Dispatchers .Default + loggingContext(mapOf (" myKey2" to " myValue2" ), listOf (" test3" ))) {
98
+ assertEquals(null , ContextMap [" myKey" ])
99
+ assertEquals(" myValue2" , ContextMap [" myKey2" ])
100
+ assertEquals(listOf (" test3" ), ContextStack .view.asList())
101
+ }
102
+ // Scoped launch with inherited MDContext element
103
+ launch(Dispatchers .Default + CoroutineThreadContext ()) {
104
+ assertEquals(" myValue2" , ContextMap [" myKey" ])
105
+ assertEquals(" test2" , ContextStack .peek())
106
+ }
107
+ // Scoped launch with inherited plus additional empty MDContext element
108
+ launch(Dispatchers .Default + additionalLoggingContext()) {
109
+ assertEquals(" myValue2" , ContextMap [" myKey" ])
110
+ assertEquals(listOf (" test" , " test2" ), ContextStack .view.asList())
111
+ }
112
+ launch(Dispatchers .Default + additionalLoggingContext(mapOf (" myKey2" to " myValue2" ), listOf (" test3" ))) {
113
+ assertEquals(" myValue2" , ContextMap [" myKey" ])
114
+ assertEquals(" myValue2" , ContextMap [" myKey2" ])
115
+ assertEquals(listOf (" test" , " test2" , " test3" ), ContextStack .view.asList())
116
+ }
83
117
}
84
118
assertEquals(" myValue" , ContextMap [" myKey" ])
85
119
assertEquals(" test" , ContextStack .peek())
@@ -104,6 +138,10 @@ class ThreadContextTest {
104
138
assertEquals(" myValue" , ContextMap [" myKey" ])
105
139
assertEquals(" test" , ContextStack .peek())
106
140
}
141
+ runBlocking(additionalLoggingContext()) {
142
+ assertEquals(" myValue" , ContextMap [" myKey" ])
143
+ assertEquals(" test" , ContextStack .peek())
144
+ }
107
145
}
108
146
109
147
@Test
@@ -112,10 +150,18 @@ class ThreadContextTest {
112
150
assertTrue(ContextMap .empty)
113
151
assertTrue(ContextStack .empty)
114
152
}
153
+ runBlocking(loggingContext()) {
154
+ assertTrue(ContextMap .empty)
155
+ assertTrue(ContextStack .empty)
156
+ }
157
+ runBlocking(additionalLoggingContext()) {
158
+ assertTrue(ContextMap .empty)
159
+ assertTrue(ContextStack .empty)
160
+ }
115
161
}
116
162
117
163
@Test
118
- fun `Context with context ` () = runBlocking {
164
+ fun `Context using withContext ` () = runBlocking {
119
165
ContextMap [" myKey" ] = " myValue"
120
166
ContextStack .push(" test" )
121
167
val mainDispatcher = coroutineContext[ContinuationInterceptor ]!!
@@ -127,6 +173,80 @@ class ThreadContextTest {
127
173
assertEquals(" test" , ContextStack .peek())
128
174
}
129
175
}
176
+ withContext(Dispatchers .Default + additionalLoggingContext()) {
177
+ assertEquals(" myValue" , ContextMap [" myKey" ])
178
+ assertEquals(" test" , ContextStack .peek())
179
+ withContext(mainDispatcher) {
180
+ assertEquals(" myValue" , ContextMap [" myKey" ])
181
+ assertEquals(" test" , ContextStack .peek())
182
+ }
183
+ }
184
+ withContext(Dispatchers .Default + additionalLoggingContext(mapOf (" myKey2" to " myValue2" ), listOf (" test2" ))) {
185
+ assertEquals(" myValue" , ContextMap [" myKey" ])
186
+ assertEquals(" myValue2" , ContextMap [" myKey2" ])
187
+ assertEquals(listOf (" test" , " test2" ), ContextStack .view.asList())
188
+ withContext(mainDispatcher) {
189
+ assertEquals(" myValue" , ContextMap [" myKey" ])
190
+ assertEquals(" myValue2" , ContextMap [" myKey2" ])
191
+ assertEquals(listOf (" test" , " test2" ), ContextStack .view.asList())
192
+ }
193
+ }
194
+ withContext(Dispatchers .Default + loggingContext(mapOf (" myKey2" to " myValue2" ), listOf (" test2" ))) {
195
+ assertEquals(null , ContextMap [" myKey" ])
196
+ assertEquals(" myValue2" , ContextMap [" myKey2" ])
197
+ assertEquals(listOf (" test2" ), ContextStack .view.asList())
198
+ withContext(mainDispatcher) {
199
+ assertEquals(null , ContextMap [" myKey" ])
200
+ assertEquals(" myValue2" , ContextMap [" myKey2" ])
201
+ assertEquals(listOf (" test2" ), ContextStack .view.asList())
202
+ }
203
+ }
204
+ }
205
+
206
+ @Test
207
+ fun `Context using withLoggingContext` () = runBlocking {
208
+ ContextMap [" myKey" ] = " myValue"
209
+ ContextStack .push(" test" )
210
+ val mainDispatcher = coroutineContext[ContinuationInterceptor ]!!
211
+ withAdditionalLoggingContext(mapOf (" myKey2" to " myValue2" ), listOf (" test2" )) {
212
+ assertEquals(" myValue" , ContextMap [" myKey" ])
213
+ assertEquals(" myValue2" , ContextMap [" myKey2" ])
214
+ assertEquals(listOf (" test" , " test2" ), ContextStack .view.asList())
215
+ withContext(mainDispatcher) {
216
+ assertEquals(" myValue" , ContextMap [" myKey" ])
217
+ assertEquals(" myValue2" , ContextMap [" myKey2" ])
218
+ assertEquals(listOf (" test" , " test2" ), ContextStack .view.asList())
219
+ }
220
+ withAdditionalLoggingContext(mapOf (" myKey3" to " myValue3" ), listOf (" test3" )) {
221
+ assertEquals(" myValue" , ContextMap [" myKey" ])
222
+ assertEquals(" myValue2" , ContextMap [" myKey2" ])
223
+ assertEquals(" myValue3" , ContextMap [" myKey3" ])
224
+ assertEquals(listOf (" test" , " test2" , " test3" ), ContextStack .view.asList())
225
+ }
226
+ assertEquals(" myValue" , ContextMap [" myKey" ])
227
+ assertEquals(" myValue2" , ContextMap [" myKey2" ])
228
+ assertEquals(null , ContextMap [" myKey3" ])
229
+ assertEquals(listOf (" test" , " test2" ), ContextStack .view.asList())
230
+ }
231
+ withLoggingContext(mapOf (" myKey2" to " myValue2" ), listOf (" test2" )) {
232
+ assertEquals(null , ContextMap [" myKey" ])
233
+ assertEquals(" myValue2" , ContextMap [" myKey2" ])
234
+ assertEquals(listOf (" test2" ), ContextStack .view.asList())
235
+ withContext(mainDispatcher) {
236
+ assertEquals(null , ContextMap [" myKey" ])
237
+ assertEquals(" myValue2" , ContextMap [" myKey2" ])
238
+ assertEquals(listOf (" test2" ), ContextStack .view.asList())
239
+ }
240
+ withLoggingContext(mapOf (" myKey3" to " myValue3" ), listOf (" test3" )) {
241
+ assertEquals(null , ContextMap [" myKey" ])
242
+ assertEquals(null , ContextMap [" myKey2" ])
243
+ assertEquals(listOf (" test3" ), ContextStack .view.asList())
244
+ }
245
+ assertEquals(null , ContextMap [" myKey" ])
246
+ assertEquals(" myValue2" , ContextMap [" myKey2" ])
247
+ assertEquals(null , ContextMap [" myKey3" ])
248
+ assertEquals(listOf (" test2" ), ContextStack .view.asList())
249
+ }
130
250
}
131
251
132
252
@Test
@@ -136,6 +256,80 @@ class ThreadContextTest {
136
256
withContext(CoroutineThreadContext (ThreadContextData (mapOf (" myKey" to " myValue" ), listOf (" test" )))) {
137
257
assertEquals(" myValue" , ContextMap [" myKey" ])
138
258
assertEquals(" test" , ContextStack .peek())
259
+ withContext(CoroutineThreadContext (ThreadContextData (mapOf (" myKey2" to " myValue2" ), listOf (" test2" )))) {
260
+ assertEquals(null , ContextMap [" myKey" ])
261
+ assertEquals(" myValue2" , ContextMap [" myKey2" ])
262
+ assertEquals(listOf (" test2" ), ContextStack .view.asList())
263
+ }
264
+ assertEquals(" myValue" , ContextMap [" myKey" ])
265
+ assertEquals(" test" , ContextStack .peek())
266
+ }
267
+ assertTrue(ContextMap .empty)
268
+ assertTrue(ContextStack .empty)
269
+
270
+ withContext(loggingContext(mapOf (" myKey" to " myValue" ), listOf (" test" ))) {
271
+ assertEquals(" myValue" , ContextMap [" myKey" ])
272
+ assertEquals(" test" , ContextStack .peek())
273
+ withContext(loggingContext(mapOf (" myKey2" to " myValue2" ), listOf (" test2" ))) {
274
+ assertEquals(null , ContextMap [" myKey" ])
275
+ assertEquals(" myValue2" , ContextMap [" myKey2" ])
276
+ assertEquals(listOf (" test2" ), ContextStack .view.asList())
277
+ }
278
+ assertEquals(" myValue" , ContextMap [" myKey" ])
279
+ assertEquals(" test" , ContextStack .peek())
280
+ }
281
+ assertTrue(ContextMap .empty)
282
+ assertTrue(ContextStack .empty)
283
+
284
+ withLoggingContext(mapOf (" myKey" to " myValue" ), listOf (" test" )) {
285
+ assertEquals(" myValue" , ContextMap [" myKey" ])
286
+ assertEquals(" test" , ContextStack .peek())
287
+ withLoggingContext(mapOf (" myKey2" to " myValue2" ), listOf (" test2" )) {
288
+ assertEquals(null , ContextMap [" myKey" ])
289
+ assertEquals(" myValue2" , ContextMap [" myKey2" ])
290
+ assertEquals(listOf (" test2" ), ContextStack .view.asList())
291
+ }
292
+ assertEquals(" myValue" , ContextMap [" myKey" ])
293
+ assertEquals(" test" , ContextStack .peek())
294
+ }
295
+ assertTrue(ContextMap .empty)
296
+ assertTrue(ContextStack .empty)
297
+
298
+ withAdditionalLoggingContext(mapOf (" myKey" to " myValue" ), listOf (" test" )) {
299
+ assertEquals(" myValue" , ContextMap [" myKey" ])
300
+ assertEquals(" test" , ContextStack .peek())
301
+ withAdditionalLoggingContext(mapOf (" myKey2" to " myValue2" ), listOf (" test2" )) {
302
+ assertEquals(" myValue" , ContextMap [" myKey" ])
303
+ assertEquals(" myValue2" , ContextMap [" myKey2" ])
304
+ assertEquals(listOf (" test" , " test2" ), ContextStack .view.asList())
305
+ }
306
+ assertEquals(" myValue" , ContextMap [" myKey" ])
307
+ assertEquals(null , ContextMap [" myKey2" ])
308
+ assertEquals(" test" , ContextStack .peek())
309
+ }
310
+ assertTrue(ContextMap .empty)
311
+ assertTrue(ContextStack .empty)
312
+ }
313
+
314
+ @Test
315
+ fun `Can override existing context, and restore it` () = runBlocking {
316
+ assertTrue(ContextMap .empty)
317
+ assertTrue(ContextStack .empty)
318
+ withLoggingContext(mapOf (" myKey" to " myValue" , " myKey2" to " myValue2" ), listOf (" test1" , " test2" )) {
319
+ assertEquals(mapOf (" myKey" to " myValue" , " myKey2" to " myValue2" ), ContextMap .view)
320
+ assertEquals(listOf (" test1" , " test2" ), ContextStack .view.asList())
321
+ withLoggingContext(mapOf (" myKey3" to " myValue3" , " myKey4" to " myValue4" ), listOf (" test3" , " test4" )) {
322
+ assertEquals(mapOf (" myKey3" to " myValue3" , " myKey4" to " myValue4" ), ContextMap .view)
323
+ assertEquals(listOf (" test3" , " test4" ), ContextStack .view.asList())
324
+ withAdditionalLoggingContext(mapOf (" myKey4" to " myValue4Modified" , " myKey5" to " myValue5" ), listOf (" test5" )) {
325
+ assertEquals(mapOf (" myKey3" to " myValue3" , " myKey4" to " myValue4Modified" , " myKey5" to " myValue5" ), ContextMap .view)
326
+ assertEquals(listOf (" test3" , " test4" , " test5" ), ContextStack .view.asList())
327
+ }
328
+ assertEquals(mapOf (" myKey3" to " myValue3" , " myKey4" to " myValue4" ), ContextMap .view)
329
+ assertEquals(listOf (" test3" , " test4" ), ContextStack .view.asList())
330
+ }
331
+ assertEquals(mapOf (" myKey" to " myValue" , " myKey2" to " myValue2" ), ContextMap .view)
332
+ assertEquals(listOf (" test1" , " test2" ), ContextStack .view.asList())
139
333
}
140
334
assertTrue(ContextMap .empty)
141
335
assertTrue(ContextStack .empty)
0 commit comments