@@ -6,6 +6,7 @@ package kotlinx.rpc.krpc.test
6
6
7
7
import kotlinx.atomicfu.atomic
8
8
import kotlinx.coroutines.*
9
+ import kotlinx.coroutines.channels.Channel
9
10
import kotlinx.coroutines.test.TestResult
10
11
import kotlinx.coroutines.test.TestScope
11
12
import kotlinx.rpc.*
@@ -87,19 +88,29 @@ class TransportTest {
87
88
val logger = RpcInternalCommonLogger .logger(" TransportTest" )
88
89
89
90
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
+
90
99
RpcInternalDumpLoggerContainer .set(object : RpcInternalDumpLogger {
91
100
override val isEnabled: Boolean = true
92
101
93
102
override fun dump (vararg tags : String , message : () -> String ) {
94
103
val message = " ${tags.joinToString(" " ) { " [$it ]" }} ${message()} "
95
- logs.add (message)
104
+ logsChannel.trySend (message)
96
105
logger.info { message }
97
106
}
98
107
})
99
108
100
109
block(logs)
101
110
102
111
RpcInternalDumpLoggerContainer .set(null )
112
+ logsJob.cancelAndJoin()
113
+ logsChannel.close()
103
114
}
104
115
105
116
@Test
@@ -247,21 +258,23 @@ class TransportTest {
247
258
transports.cancel()
248
259
}
249
260
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 )
251
266
252
267
@Test
253
268
fun transportInitializedOnlyOnce () = runTest { logs ->
254
269
val localTransport = LocalTransport ()
255
- var transportInitialized = 0
256
- var configInitialized = 0
257
270
val client = object : KrpcClient () {
258
271
override suspend fun initializeTransport (): KrpcTransport {
259
- transportInitialized++
272
+ transportInitialized.getAndIncrement()
260
273
return localTransport.client
261
274
}
262
275
263
276
override fun initializeConfig (): KrpcConfig .Client {
264
- configInitialized++
277
+ configInitialized.getAndIncrement()
265
278
return clientConfig
266
279
}
267
280
}
@@ -274,8 +287,8 @@ class TransportTest {
274
287
client.withService<Echo >().apply { echo(" foo" ); echo(" bar" ) }
275
288
client.withService<Second >().apply { second(" bar" ); second(" baz" ) }
276
289
277
- assertEquals(1 , transportInitialized)
278
- assertEquals(1 , configInitialized)
290
+ assertEquals(1 , transportInitialized.value )
291
+ assertEquals(1 , configInitialized.value )
279
292
assertEquals(1 , logs.count { it.matches(clientHandshake) })
280
293
}
281
294
0 commit comments