@@ -7,6 +7,7 @@ package kotlinx.rpc.krpc.test
7
7
import kotlinx.coroutines.CoroutineScope
8
8
import kotlinx.coroutines.currentCoroutineContext
9
9
import kotlinx.coroutines.test.runTest
10
+ import kotlinx.coroutines.withContext
10
11
import kotlinx.rpc.krpc.rpcClientConfig
11
12
import kotlinx.rpc.krpc.rpcServerConfig
12
13
import kotlinx.rpc.krpc.serialization.json.json
@@ -29,7 +30,7 @@ class CoroutineContextPropagationTest {
29
30
}
30
31
}
31
32
32
- object CoroutineElement : CoroutineContext.Element {
33
+ data class CoroutineElement ( val value : String ) : CoroutineContext.Element {
33
34
object Key : CoroutineContext.Key<CoroutineElement>
34
35
35
36
override val key: CoroutineContext .Key <* > = Key
@@ -38,20 +39,22 @@ class CoroutineContextPropagationTest {
38
39
@Test
39
40
fun test () = runTest {
40
41
var actualContext: CoroutineElement ? = null
41
- val transport = LocalTransport (transportContext = CoroutineElement )
42
+ val transport = LocalTransport (transportContext = CoroutineElement ( " transport " ) )
42
43
val server = KrpcTestServer (rpcServerConfig, transport.server)
43
44
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
+ }
49
52
}
50
-
51
- override val coroutineContext: CoroutineContext = it
52
53
}
53
54
}
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)
56
59
}
57
60
}
0 commit comments