Skip to content

Commit 805e892

Browse files
committed
propagate transport coroutine context tests
1 parent ffbaa29 commit 805e892

File tree

4 files changed

+74
-7
lines changed

4 files changed

+74
-7
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.rpc.krpc.test
6+
7+
import kotlinx.coroutines.CoroutineScope
8+
import kotlinx.coroutines.currentCoroutineContext
9+
import kotlinx.coroutines.test.runTest
10+
import kotlinx.coroutines.withContext
11+
import kotlinx.rpc.krpc.rpcClientConfig
12+
import kotlinx.rpc.krpc.rpcServerConfig
13+
import kotlinx.rpc.krpc.serialization.json.json
14+
import kotlinx.rpc.withService
15+
import kotlin.coroutines.CoroutineContext
16+
import kotlin.test.Test
17+
import kotlin.test.assertEquals
18+
19+
class CoroutineContextPropagationTest {
20+
private val rpcServerConfig = rpcServerConfig {
21+
serialization {
22+
json()
23+
}
24+
}
25+
private val rpcClientConfig = rpcClientConfig {
26+
serialization {
27+
json {
28+
ignoreUnknownKeys = true
29+
}
30+
}
31+
}
32+
33+
data class CoroutineElement(val value: String) : CoroutineContext.Element {
34+
object Key : CoroutineContext.Key<CoroutineElement>
35+
36+
override val key: CoroutineContext.Key<*> = Key
37+
}
38+
39+
@Test
40+
fun test() = runTest {
41+
var actualContext: CoroutineElement? = null
42+
val transport = LocalTransport(CoroutineElement("transport"))
43+
val server = KrpcTestServer(rpcServerConfig, transport.server)
44+
val client = KrpcTestClient(rpcClientConfig, transport.client)
45+
withContext(CoroutineElement("server")) {
46+
server.registerService(Echo::class) {
47+
object : Echo {
48+
override suspend fun echo(message: String): String = run {
49+
actualContext = currentCoroutineContext().get(CoroutineElement.Key)
50+
"response"
51+
}
52+
}
53+
}
54+
}
55+
withContext(CoroutineElement("client")) {
56+
client.withService(Echo::class).echo("request")
57+
}
58+
assertEquals(CoroutineElement("transport"), actualContext)
59+
}
60+
}

krpc/krpc-test/src/commonTest/kotlin/kotlinx/rpc/krpc/test/LocalTransport.kt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ import kotlin.coroutines.CoroutineContext
1616
import kotlin.time.Clock
1717
import kotlin.time.ExperimentalTime
1818

19-
class LocalTransport(parentScope: CoroutineScope? = null) : CoroutineScope {
20-
override val coroutineContext = parentScope?.run { SupervisorJob(coroutineContext.job) }
21-
?: SupervisorJob()
19+
class LocalTransport(
20+
parentContext: CoroutineContext? = null,
21+
) : CoroutineScope {
22+
override val coroutineContext = SupervisorJob(parentContext?.job)
2223

2324
private val clientIncoming = Channel<KrpcTransportMessage>()
2425
private val serverIncoming = Channel<KrpcTransportMessage>()
@@ -27,7 +28,10 @@ class LocalTransport(parentScope: CoroutineScope? = null) : CoroutineScope {
2728
val lastMessageSentOnServer = atomic(0L)
2829

2930
val client: KrpcTransport = object : KrpcTransport {
30-
override val coroutineContext: CoroutineContext = Job(this@LocalTransport.coroutineContext.job)
31+
override val coroutineContext: CoroutineContext = Job(this@LocalTransport.coroutineContext.job).let {
32+
if(parentContext != null) parentContext + it
33+
else it
34+
}
3135

3236
@OptIn(ExperimentalTime::class)
3337
override suspend fun send(message: KrpcTransportMessage) {
@@ -41,7 +45,10 @@ class LocalTransport(parentScope: CoroutineScope? = null) : CoroutineScope {
4145
}
4246

4347
val server: KrpcTransport = object : KrpcTransport {
44-
override val coroutineContext: CoroutineContext = Job(this@LocalTransport.coroutineContext)
48+
override val coroutineContext: CoroutineContext = Job(this@LocalTransport.coroutineContext.job).let {
49+
if(parentContext != null) parentContext + it
50+
else it
51+
}
4552

4653
@OptIn(ExperimentalTime::class)
4754
override suspend fun send(message: KrpcTransportMessage) {

krpc/krpc-test/src/commonTest/kotlin/kotlinx/rpc/krpc/test/cancellation/CancellationToolkit.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class CancellationToolkit(scope: CoroutineScope) : CoroutineScope by scope {
5050
}
5151
}
5252

53-
val transport = LocalTransport(this)
53+
val transport = LocalTransport(this.coroutineContext.job)
5454

5555
val client by lazy {
5656
KrpcTestClient(rpcClientConfig {

krpc/krpc-test/src/jvmTest/kotlin/kotlinx/rpc/krpc/test/api/WireSamplingTestScope.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ class WireSamplingTestScope(private val sampleName: String, scope: TestScope) :
229229
}
230230

231231
private class WireToolkit(scope: CoroutineScope, format: SamplingFormat, val logger: RpcInternalCommonLogger? = null) {
232-
val transport = LocalTransport(scope)
232+
val transport = LocalTransport(scope.coroutineContext.job)
233233

234234
val client by lazy {
235235
KrpcTestClient(rpcClientConfig {

0 commit comments

Comments
 (0)