You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rg...@apache.org on 2023/12/07 00:19:28 UTC
(logging-log4j-kotlin) 01/03: Merge branch 'issue-54'
This is an automated email from the ASF dual-hosted git repository.
rgupta pushed a commit to branch release/1.3.1
in repository https://gitbox.apache.org/repos/asf/logging-log4j-kotlin.git
commit f394122ac1ceaedea78523fdba26ada1fac62234
Author: Raman Gupta <ro...@gmail.com>
AuthorDate: Wed Dec 6 16:19:46 2023 -0500
Merge branch 'issue-54'
* issue-54:
Restore context correctly
---
.../apache/logging/log4j/kotlin/CoroutineThreadContext.kt | 6 ++++--
.../org.apache.logging.log4j.kotlin/ThreadContextTest.kt | 14 ++++++++++++++
2 files changed, 18 insertions(+), 2 deletions(-)
diff --git a/log4j-api-kotlin/src/main/kotlin/org/apache/logging/log4j/kotlin/CoroutineThreadContext.kt b/log4j-api-kotlin/src/main/kotlin/org/apache/logging/log4j/kotlin/CoroutineThreadContext.kt
index 9c38f56..c266702 100644
--- a/log4j-api-kotlin/src/main/kotlin/org/apache/logging/log4j/kotlin/CoroutineThreadContext.kt
+++ b/log4j-api-kotlin/src/main/kotlin/org/apache/logging/log4j/kotlin/CoroutineThreadContext.kt
@@ -89,7 +89,9 @@ class CoroutineThreadContext(
}
private fun setCurrent(contextData: ThreadContextData) {
- contextData.map?.let { ContextMap += it } ?: ContextMap.clear()
- contextData.stack?.let { ContextStack.set(it) } ?: ContextStack.clear()
+ ContextMap.clear()
+ ContextStack.clear()
+ contextData.map?.let { ContextMap += it }
+ contextData.stack?.let { ContextStack.set(it) }
}
}
diff --git a/log4j-api-kotlin/src/test/kotlin/org.apache.logging.log4j.kotlin/ThreadContextTest.kt b/log4j-api-kotlin/src/test/kotlin/org.apache.logging.log4j.kotlin/ThreadContextTest.kt
index f725f26..daab166 100644
--- a/log4j-api-kotlin/src/test/kotlin/org.apache.logging.log4j.kotlin/ThreadContextTest.kt
+++ b/log4j-api-kotlin/src/test/kotlin/org.apache.logging.log4j.kotlin/ThreadContextTest.kt
@@ -38,6 +38,7 @@ class ThreadContextTest {
ContextStack.clear()
}
+ @DelicateCoroutinesApi
@Test
fun `Context is not passed by default between coroutines`() = runBlocking {
ContextMap["myKey"] = "myValue"
@@ -49,6 +50,7 @@ class ThreadContextTest {
}.join()
}
+ @DelicateCoroutinesApi
@Test
fun `Context can be passed between coroutines`() = runBlocking {
ContextMap["myKey"] = "myValue"
@@ -121,4 +123,16 @@ class ThreadContextTest {
}
}
}
+
+ @Test
+ fun `Context is restored after a context block is complete`() = runBlocking {
+ assertTrue(ContextMap.empty)
+ assertTrue(ContextStack.empty)
+ withContext(CoroutineThreadContext(ThreadContextData(mapOf("myKey" to "myValue"), listOf("test")))) {
+ assertEquals("myValue", ContextMap["myKey"])
+ assertEquals("test", ContextStack.peek())
+ }
+ assertTrue(ContextMap.empty)
+ assertTrue(ContextStack.empty)
+ }
}