@@ -7,6 +7,7 @@ package kotlinx.rpc.krpc.test
77import kotlinx.coroutines.CoroutineScope
88import kotlinx.coroutines.currentCoroutineContext
99import kotlinx.coroutines.test.runTest
10+ import kotlinx.coroutines.withContext
1011import kotlinx.rpc.krpc.rpcClientConfig
1112import kotlinx.rpc.krpc.rpcServerConfig
1213import kotlinx.rpc.krpc.serialization.json.json
@@ -29,7 +30,7 @@ class CoroutineContextPropagationTest {
2930 }
3031 }
3132
32- object CoroutineElement : CoroutineContext.Element {
33+ data class CoroutineElement ( val value : String ) : CoroutineContext.Element {
3334 object Key : CoroutineContext.Key<CoroutineElement>
3435
3536 override val key: CoroutineContext .Key <* > = Key
@@ -38,20 +39,22 @@ class CoroutineContextPropagationTest {
3839 @Test
3940 fun test () = runTest {
4041 var actualContext: CoroutineElement ? = null
41- val transport = LocalTransport (transportContext = CoroutineElement )
42+ val transport = LocalTransport (transportContext = CoroutineElement ( " transport " ) )
4243 val server = KrpcTestServer (rpcServerConfig, transport.server)
4344 val client = KrpcTestClient (rpcClientConfig, transport.client)
44- server.registerService(Echo ::class ) {
45- object : Echo {
46- override suspend fun echo (message : String ): String = run {
47- actualContext = currentCoroutineContext().get(CoroutineElement .Key )
48- " response"
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+ }
4952 }
50-
51- override val coroutineContext: CoroutineContext = it
5253 }
5354 }
54- client.withService(Echo ::class ).echo(" request" )
55- assertEquals(actualContext, CoroutineElement )
55+ withContext(CoroutineElement (" client" )) {
56+ client.withService(Echo ::class ).echo(" request" )
57+ }
58+ assertEquals(CoroutineElement (" transport" ), actualContext)
5659 }
5760}
0 commit comments