@@ -10,7 +10,11 @@ import kotlinx.coroutines.test.TestResult
10
10
import kotlinx.coroutines.test.TestScope
11
11
import kotlinx.rpc.*
12
12
import kotlinx.rpc.annotations.Rpc
13
+ import kotlinx.rpc.krpc.KrpcConfig
13
14
import kotlinx.rpc.krpc.KrpcConfigBuilder
15
+ import kotlinx.rpc.krpc.KrpcTransport
16
+ import kotlinx.rpc.krpc.client.KrpcClient
17
+ import kotlinx.rpc.krpc.internal.KrpcProtocolMessage
14
18
import kotlinx.rpc.krpc.internal.logging.RpcInternalCommonLogger
15
19
import kotlinx.rpc.krpc.internal.logging.RpcInternalDumpLogger
16
20
import kotlinx.rpc.krpc.internal.logging.RpcInternalDumpLoggerContainer
@@ -76,21 +80,24 @@ class TransportTest {
76
80
return KrpcTestServer (serverConfig, localTransport.server)
77
81
}
78
82
79
- private fun runTest (block : suspend TestScope .() -> Unit ): TestResult =
83
+ private fun runTest (block : suspend TestScope .(logs: List < String > ) -> Unit ): TestResult =
80
84
kotlinx.coroutines.test.runTest(timeout = 20 .seconds) {
81
85
debugCoroutines()
82
86
83
87
val logger = RpcInternalCommonLogger .logger(" TransportTest" )
84
88
89
+ val logs = mutableListOf<String >()
85
90
RpcInternalDumpLoggerContainer .set(object : RpcInternalDumpLogger {
86
91
override val isEnabled: Boolean = true
87
92
88
93
override fun dump (vararg tags : String , message : () -> String ) {
89
- logger.info { " ${tags.joinToString(" " ) { " [$it ]" }} ${message()} " }
94
+ val message = " ${tags.joinToString(" " ) { " [$it ]" }} ${message()} "
95
+ logs.add(message)
96
+ logger.info { message }
90
97
}
91
98
})
92
99
93
- block()
100
+ block(logs )
94
101
95
102
RpcInternalDumpLoggerContainer .set(null )
96
103
}
@@ -240,6 +247,38 @@ class TransportTest {
240
247
transports.cancel()
241
248
}
242
249
250
+ private val clientHandshake = " .*\\ [Client] \\ [Send] \\ {\" type\" :\" ${KrpcProtocolMessage .Handshake .serializer().descriptor.serialName} \" .*+" .toRegex()
251
+
252
+ @Test
253
+ fun transportInitializedOnlyOnce () = runTest { logs ->
254
+ val localTransport = LocalTransport ()
255
+ var transportInitialized = 0
256
+ var configInitialized = 0
257
+ val client = object : KrpcClient () {
258
+ override suspend fun initializeTransport (): KrpcTransport {
259
+ transportInitialized++
260
+ return localTransport.client
261
+ }
262
+
263
+ override fun initializeConfig (): KrpcConfig .Client {
264
+ configInitialized++
265
+ return clientConfig
266
+ }
267
+ }
268
+
269
+ val server = serverOf(localTransport)
270
+
271
+ server.registerServiceAndReturn<Echo , _ > { EchoImpl () }
272
+ server.registerServiceAndReturn<Second , _ > { SecondServer () }
273
+
274
+ client.withService<Echo >().apply { echo(" foo" ); echo(" bar" ) }
275
+ client.withService<Second >().apply { second(" bar" ); second(" baz" ) }
276
+
277
+ assertEquals(1 , transportInitialized)
278
+ assertEquals(1 , configInitialized)
279
+ assertEquals(1 , logs.count { it.matches(clientHandshake) })
280
+ }
281
+
243
282
private inline fun <@Rpc reified Service : Any , reified Impl : Service > RpcServer.registerServiceAndReturn (
244
283
crossinline body : () -> Impl ,
245
284
): List <Impl > {
0 commit comments