Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class ThreadContextTest {
ContextStack.clear()
}

@DelicateCoroutinesApi
@Test
fun `Context is not passed by default between coroutines`() = runBlocking {
ContextMap["myKey"] = "myValue"
Expand All @@ -49,6 +50,7 @@ class ThreadContextTest {
}.join()
}

@DelicateCoroutinesApi
@Test
fun `Context can be passed between coroutines`() = runBlocking {
ContextMap["myKey"] = "myValue"
Expand Down Expand Up @@ -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)
}
}