|
| 1 | +package misk.logging |
| 2 | + |
| 3 | +import jakarta.inject.Inject |
| 4 | +import kotlinx.coroutines.delay |
| 5 | +import kotlinx.coroutines.slf4j.MDCContext |
| 6 | +import kotlinx.coroutines.test.runTest |
| 7 | +import misk.MiskTestingServiceModule |
| 8 | +import misk.testing.MiskTest |
| 9 | +import misk.testing.MiskTestModule |
| 10 | +import org.junit.jupiter.api.Test |
| 11 | +import kotlin.test.assertEquals |
| 12 | +import kotlin.test.assertNull |
| 13 | +import misk.logging.coroutines.withMdc as withMdcCoroutines |
| 14 | + |
| 15 | +@MiskTest(startService = false) |
| 16 | +internal class ScopedMdcKtTest { |
| 17 | + @MiskTestModule |
| 18 | + val module = MiskTestingServiceModule() |
| 19 | + |
| 20 | + @Inject |
| 21 | + lateinit var mdc: Mdc |
| 22 | + |
| 23 | + @Test |
| 24 | + fun `test withMdc in a coroutine for key value pairs`() = runTest(MDCContext()) { |
| 25 | + val tags = (1..3).map { "key$it" to "value$it" }.toTypedArray() |
| 26 | + mdc.withMdcCoroutines(*tags) { |
| 27 | + tags.assertTags() |
| 28 | + delay(100) |
| 29 | + tags.assertTags() |
| 30 | + } |
| 31 | + tags.asserMissingTags() |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + fun `test withMdc for key value pairs`() { |
| 36 | + val tags = (1..3).map { "key$it" to "value$it" }.toTypedArray() |
| 37 | + mdc.withMdc(*tags) { |
| 38 | + tags.assertTags() |
| 39 | + } |
| 40 | + tags.asserMissingTags() |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + fun `test withMdc in a coroutine for key value pair overrides`() = runTest(MDCContext()) { |
| 45 | + val tags = (1..3).map { "key$it" to "value$it" }.toTypedArray() |
| 46 | + mdc.withMdcCoroutines(*tags) { |
| 47 | + tags.assertTags() |
| 48 | + delay(100) |
| 49 | + tags.assertTags() |
| 50 | + val updatedTags = tags.map { if (it.first == "key1"){it.first to it.second+"00"} else {it} }.toTypedArray() |
| 51 | + mdc.withMdcCoroutines(*updatedTags) { |
| 52 | + updatedTags.assertTags() |
| 53 | + delay(100) |
| 54 | + updatedTags.assertTags() |
| 55 | + } |
| 56 | + tags.forEach { it.asserTag() } |
| 57 | + } |
| 58 | + tags.asserMissingTags() |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + fun `test withMdc for key value pair overrides`() { |
| 63 | + val tags = (1..3).map { "key$it" to "value$it" }.toTypedArray() |
| 64 | + mdc.withMdc(*tags) { |
| 65 | + tags.assertTags() |
| 66 | + val updatedTags = tags.map { if (it.first == "key1"){it.first to it.second+"00"} else {it} }.toTypedArray() |
| 67 | + mdc.withMdc(*updatedTags) { |
| 68 | + updatedTags.assertTags() |
| 69 | + } |
| 70 | + tags.forEach { it.asserTag() } |
| 71 | + } |
| 72 | + tags.asserMissingTags() |
| 73 | + } |
| 74 | + |
| 75 | + fun Pair<String, String>.asserTag() = assertEquals(second, mdc.get(first)) |
| 76 | + fun Array<Pair<String, String>>.assertTags() = forEach { it.asserTag() } |
| 77 | + fun Pair<String, String>.asserMissingTag() = assertNull( mdc.get(first)) |
| 78 | + fun Array<Pair<String, String>>.asserMissingTags() = forEach { it.asserMissingTag() } |
| 79 | +} |
0 commit comments