@@ -6,6 +6,7 @@ package kotlinx.rpc.krpc.test
66
77import kotlinx.atomicfu.atomic
88import kotlinx.coroutines.*
9+ import kotlinx.coroutines.channels.Channel
910import kotlinx.coroutines.test.TestResult
1011import kotlinx.coroutines.test.TestScope
1112import kotlinx.rpc.*
@@ -87,19 +88,29 @@ class TransportTest {
8788 val logger = RpcInternalCommonLogger .logger(" TransportTest" )
8889
8990 val logs = mutableListOf<String >()
91+ val logsChannel = Channel <String >(Channel .UNLIMITED )
92+
93+ val logsJob = launch(CoroutineName (" logs collector" )) {
94+ for (log in logsChannel) {
95+ logs.add(log)
96+ }
97+ }
98+
9099 RpcInternalDumpLoggerContainer .set(object : RpcInternalDumpLogger {
91100 override val isEnabled: Boolean = true
92101
93102 override fun dump (vararg tags : String , message : () -> String ) {
94103 val message = " ${tags.joinToString(" " ) { " [$it ]" }} ${message()} "
95- logs.add (message)
104+ logsChannel.trySend (message)
96105 logger.info { message }
97106 }
98107 })
99108
100109 block(logs)
101110
102111 RpcInternalDumpLoggerContainer .set(null )
112+ logsJob.cancelAndJoin()
113+ logsChannel.close()
103114 }
104115
105116 @Test
@@ -247,21 +258,23 @@ class TransportTest {
247258 transports.cancel()
248259 }
249260
250- private val clientHandshake = " .*\\ [Client] \\ [Send] \\ {\" type\" :\" ${KrpcProtocolMessage .Handshake .serializer().descriptor.serialName} \" .*+" .toRegex()
261+ private val handshakeClassSerialName = KrpcProtocolMessage .Handshake .serializer().descriptor.serialName
262+ private val clientHandshake = " .*\\ [Client] \\ [Send] \\ {\" type\" :\" $handshakeClassSerialName \" .*+" .toRegex()
263+
264+ private val transportInitialized = atomic(0 )
265+ private val configInitialized = atomic(0 )
251266
252267 @Test
253268 fun transportInitializedOnlyOnce () = runTest { logs ->
254269 val localTransport = LocalTransport ()
255- var transportInitialized = 0
256- var configInitialized = 0
257270 val client = object : KrpcClient () {
258271 override suspend fun initializeTransport (): KrpcTransport {
259- transportInitialized++
272+ transportInitialized.getAndIncrement()
260273 return localTransport.client
261274 }
262275
263276 override fun initializeConfig (): KrpcConfig .Client {
264- configInitialized++
277+ configInitialized.getAndIncrement()
265278 return clientConfig
266279 }
267280 }
@@ -274,8 +287,8 @@ class TransportTest {
274287 client.withService<Echo >().apply { echo(" foo" ); echo(" bar" ) }
275288 client.withService<Second >().apply { second(" bar" ); second(" baz" ) }
276289
277- assertEquals(1 , transportInitialized)
278- assertEquals(1 , configInitialized)
290+ assertEquals(1 , transportInitialized.value )
291+ assertEquals(1 , configInitialized.value )
279292 assertEquals(1 , logs.count { it.matches(clientHandshake) })
280293 }
281294
0 commit comments