Skip to content

Commit 185a51d

Browse files
authored
Fix println tests (#277)
1 parent 190344e commit 185a51d

File tree

3 files changed

+54
-34
lines changed

3 files changed

+54
-34
lines changed

krpc/krpc-test/src/jvmMain/kotlin/kotlinx/rpc/krpc/test/KrpcTestServiceBackend.kt

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
2+
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

55
package kotlinx.rpc.krpc.test
@@ -12,6 +12,7 @@ import java.time.LocalDateTime
1212
import java.time.format.DateTimeFormatter
1313
import kotlin.coroutines.CoroutineContext
1414
import kotlin.coroutines.resumeWithException
15+
import kotlin.test.assertContentEquals
1516
import kotlin.test.assertEquals
1617

1718
@OptIn(ExperimentalCoroutinesApi::class)
@@ -20,9 +21,7 @@ class KrpcTestServiceBackend(override val coroutineContext: CoroutineContext) :
2021
const val SHARED_FLOW_REPLAY = 5
2122
}
2223

23-
override suspend fun empty() {
24-
println("empty")
25-
}
24+
override suspend fun empty() {}
2625

2726
override suspend fun returnType(): String {
2827
return "test"
@@ -41,44 +40,52 @@ class KrpcTestServiceBackend(override val coroutineContext: CoroutineContext) :
4140
}
4241

4342
override suspend fun paramsSingle(arg1: String) {
44-
println("SINGLE: $arg1")
43+
assertEquals("test", arg1)
4544
}
4645

4746
override suspend fun paramsDouble(arg1: String, arg2: String) {
48-
println("double $arg1 $arg2")
47+
assertEquals("test", arg1)
48+
assertEquals("test2", arg2)
4949
}
5050

5151
override suspend fun varargParams(arg1: String, vararg arg2: String) {
52-
println("vararg $arg1 ${arg2.joinToString()}")
52+
assertEquals("test", arg1)
53+
assertEquals("test2", arg2[0])
54+
assertEquals("test3", arg2[1])
55+
assertEquals(emptyList(), arg2.drop(2))
5356
}
5457

5558
override suspend fun genericParams(arg1: List<String>) {
56-
println("Received list: ${arg1.joinToString()}")
59+
assertEquals(listOf("test", "test2", "test3"), arg1)
5760
}
5861

5962
override suspend fun doubleGenericParams(arg1: List<List<String>>) {
60-
println("Received list of lists: ${arg1.joinToString { it.joinToString() }} }}")
63+
assertContentEquals(listOf(listOf("test", "test2", "test3")), arg1)
6164
}
6265

6366
@Suppress("detekt.MaxLineLength")
6467
override suspend fun mapParams(arg1: Map<String, Map<Int, List<String>>>) {
65-
println("Received map: ${arg1.entries.joinToString { "${it.key} -> ${it.value.entries.joinToString { (key, value) -> "$key -> ${value.joinToString()}" }}" }}")
68+
assertEquals(arg1.size, 1)
69+
assertContentEquals(arg1.keys, listOf("key"))
70+
assertEquals(arg1["key"]?.size, 1)
71+
assertContentEquals(arg1["key"]?.keys, listOf(1))
72+
assertContentEquals(arg1["key"]?.get(1), listOf("test", "test2", "test3"))
6673
}
6774

6875
override suspend fun customType(arg1: TestClass): TestClass {
6976
return arg1
7077
}
7178

7279
override suspend fun nullable(arg1: String?): TestClass? {
73-
println("nullable $arg1")
7480
return if (arg1 == null) null else TestClass()
7581
}
7682

7783
override suspend fun variance(
7884
arg2: TestList<in TestClass>,
7985
arg3: TestList2<*>
8086
): TestList<out TestClass> {
81-
println("variance: $arg2 $arg3")
87+
assertEquals(arg2.value, 42)
88+
assertEquals(arg3.value, 42)
8289
return TestList(3)
8390
}
8491

@@ -98,7 +105,7 @@ class KrpcTestServiceBackend(override val coroutineContext: CoroutineContext) :
98105
override suspend fun incomingStreamAsyncCollect(arg1: Flow<String>): Int {
99106
@Suppress("detekt.GlobalCoroutineUsage")
100107
GlobalScope.launch {
101-
arg1.collect { println("incomingStreamAsyncCollect item $it") }
108+
assertContentEquals(listOf("test1", "test2", "test3"), arg1.toList())
102109
}
103110
return 5
104111
}
@@ -144,13 +151,13 @@ class KrpcTestServiceBackend(override val coroutineContext: CoroutineContext) :
144151
}
145152

146153
override suspend fun streamInDataClassWithStream(payloadWithPayload: PayloadWithPayload): Int {
147-
payloadWithPayload.collectAndPrint()
154+
assertContentEquals(KrpcTransportTestBase.expectedPayloadWithPayload(10), payloadWithPayload.collect())
148155
return 5
149156
}
150157

151158
override suspend fun streamInStreamWithStream(payloadWithPayload: Flow<PayloadWithPayload>): Int {
152-
payloadWithPayload.collect {
153-
it.collectAndPrint()
159+
payloadWithPayload.collectIndexed { index, it ->
160+
assertContentEquals(KrpcTransportTestBase.expectedPayloadWithPayload(index), it.collect())
154161
}
155162
return 5
156163
}
@@ -228,7 +235,6 @@ class KrpcTestServiceBackend(override val coroutineContext: CoroutineContext) :
228235
}
229236

230237
override suspend fun answerToAnything(arg: String): Int {
231-
println("Return 42")
232238
return 42
233239
}
234240

@@ -276,9 +282,7 @@ class KrpcTestServiceBackend(override val coroutineContext: CoroutineContext) :
276282

277283
launch {
278284
assertEquals(listOf(0, 1, 2, 3, 4), sharedFlow.take(5).toList())
279-
println("hello 1")
280285
state.emit(1)
281-
println("hello 2")
282286
}
283287

284288
return state

krpc/krpc-test/src/jvmMain/kotlin/kotlinx/rpc/krpc/test/KrpcTransportTestBase.kt

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
2+
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

55
@file:Suppress("FunctionName")
@@ -243,6 +243,16 @@ abstract class KrpcTransportTestBase {
243243
assertEquals(3, result)
244244
}
245245

246+
@Test
247+
fun incomingStreamAsyncCollect() {
248+
val result = runBlocking {
249+
streamScoped {
250+
client.incomingStreamAsyncCollect(flowOf("test1", "test2", "test3"))
251+
}
252+
}
253+
assertEquals(5, result)
254+
}
255+
246256
@Test
247257
fun outgoingStream() {
248258
runBlocking {
@@ -349,7 +359,7 @@ abstract class KrpcTransportTestBase {
349359
fun returnPayloadWithPayload() {
350360
runBlocking {
351361
streamScoped {
352-
client.returnPayloadWithPayload().collectAndPrint()
362+
assertContentEquals(expectedPayloadWithPayload(10), client.returnPayloadWithPayload().collect())
353363
}
354364
}
355365
}
@@ -358,8 +368,8 @@ abstract class KrpcTransportTestBase {
358368
fun returnFlowPayloadWithPayload() {
359369
runBlocking {
360370
streamScoped {
361-
client.returnFlowPayloadWithPayload().collect {
362-
it.collectAndPrint()
371+
client.returnFlowPayloadWithPayload().collectIndexed { index, payloadWithPayload ->
372+
assertContentEquals(expectedPayloadWithPayload(index), payloadWithPayload.collect())
363373
}
364374
}
365375
}
@@ -377,9 +387,11 @@ abstract class KrpcTransportTestBase {
377387
},
378388
)
379389

380-
result.collect {
381-
it.collectAndPrint()
382-
}
390+
val all = result.toList().onEach {
391+
assertContentEquals(expectedPayloadWithPayload(10), it.collect())
392+
}.size
393+
394+
assertEquals(5, all)
383395
}
384396
}
385397
}
@@ -667,10 +679,8 @@ abstract class KrpcTransportTestBase {
667679
runBlocking {
668680
streamScoped {
669681
val flow = sharedFlowOfT { it }
670-
println("hello 1.1")
671682

672683
val state = client.sharedFlowInFunction(flow)
673-
println("hello 1.2")
674684

675685
assertEquals(1, state.first { it == 1 })
676686
}
@@ -702,4 +712,8 @@ abstract class KrpcTransportTestBase {
702712
}
703713
}
704714
}
715+
716+
companion object {
717+
fun expectedPayloadWithPayload(size: Int) = List(size) { listOf("a$it", "b$it", "c$it") }
718+
}
705719
}

krpc/krpc-test/src/jvmMain/kotlin/kotlinx/rpc/krpc/test/Payloads.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
2+
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

55
package kotlinx.rpc.krpc.test
@@ -9,6 +9,8 @@ import kotlinx.coroutines.flow.Flow
99
import kotlinx.coroutines.flow.MutableSharedFlow
1010
import kotlinx.coroutines.flow.MutableStateFlow
1111
import kotlinx.coroutines.flow.flow
12+
import kotlinx.coroutines.flow.map
13+
import kotlinx.coroutines.flow.toList
1214
import kotlinx.coroutines.launch
1315
import kotlinx.serialization.Contextual
1416
import kotlinx.serialization.Serializable
@@ -18,10 +20,10 @@ data class PayloadWithStream(val payload: String, val stream: @Contextual Flow<S
1820

1921
@Serializable
2022
data class PayloadWithPayload(val payload: String, val flow: @Contextual Flow<PayloadWithStream>) {
21-
suspend fun collectAndPrint() {
22-
flow.collect {
23-
it.stream.collect { item -> println("item $item") }
24-
}
23+
suspend fun collect(): List<List<String>> {
24+
return flow.map {
25+
it.stream.toList()
26+
}.toList()
2527
}
2628
}
2729

@@ -32,7 +34,7 @@ fun payload(index: Int = 0): PayloadWithStream {
3234
)
3335
}
3436

35-
fun payloadWithPayload(index: Int = 0): PayloadWithPayload {
37+
fun payloadWithPayload(index: Int = 10): PayloadWithPayload {
3638
return PayloadWithPayload("test$index", payloadStream(index))
3739
}
3840

0 commit comments

Comments
 (0)