Skip to content

Commit 9f674e6

Browse files
authored
Fix flows that emit Units (#323)
1 parent 8ec90e0 commit 9f674e6

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

krpc/krpc-server/src/commonMain/kotlin/kotlinx/rpc/krpc/server/internal/KrpcServerService.kt

+5-4
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ internal class KrpcServerService<@Rpc T : Any>(
163163
if (callable.isNonSuspendFunction && !markedNonSuspending) {
164164
@Suppress("detekt.MaxLineLength")
165165
error(
166-
"Server flow returned from non-suspend function but marked so by a client: ${descriptor.fqName}::$callableName." +
166+
"Server flow returned from non-suspend function but marked so by a client: ${descriptor.fqName}::$callableName. " +
167167
"Probable cause is outdated client version, that does not support non-suspending flows, " +
168168
"but calls the function with the same name. Change the function name or update the client."
169169
)
@@ -181,7 +181,7 @@ internal class KrpcServerService<@Rpc T : Any>(
181181
}
182182
}.let { interceptedValue ->
183183
// KRPC-173
184-
if (callable.returnType.kType == typeOf<Unit>()) {
184+
if (!callable.isNonSuspendFunction && callable.returnType.kType == typeOf<Unit>()) {
185185
Unit
186186
} else {
187187
interceptedValue
@@ -194,8 +194,9 @@ internal class KrpcServerService<@Rpc T : Any>(
194194
if (callable.isNonSuspendFunction) {
195195
if (value !is Flow<*>) {
196196
error(
197-
"Return value of non-suspend function must be a non nullable flow, " +
198-
"but was: ${value?.let { it::class.simpleName }}"
197+
"Return value of non-suspend function '${callable.name}' " +
198+
"with callId '$callId' must be a non nullable flow, " +
199+
"but was: ${value?.let { it::class.simpleName }}"
199200
)
200201
}
201202

krpc/krpc-test/src/commonMain/kotlin/kotlinx/rpc/krpc/test/KrpcTestService.kt

+2
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ interface KrpcTestService : RemoteService {
113113

114114
suspend fun krpc173()
115115

116+
fun unitFlow(): Flow<Unit>
117+
116118
val plainFlowOfInts : Flow<Int>
117119

118120
val plainFlowOfFlowsOfInts : Flow<Flow<Int>>

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

+6
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,12 @@ class KrpcTestServiceBackend(override val coroutineContext: CoroutineContext) :
282282
doWork()
283283
}
284284

285+
override fun unitFlow(): Flow<Unit> {
286+
return flow {
287+
emit(Unit)
288+
}
289+
}
290+
285291
override val plainFlowOfInts: Flow<Int> = plainFlow { it }
286292

287293
override val plainFlowOfFlowsOfInts: Flow<Flow<Int>> = plainFlow { plainFlow { i -> i } }

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

+5
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,11 @@ abstract class KrpcTransportTestBase {
569569
assertEquals(Unit, client.krpc173())
570570
}
571571

572+
@Test
573+
fun testUnitFlow() = runTest {
574+
assertEquals(Unit, client.unitFlow().toList().single())
575+
}
576+
572577
@Test
573578
fun testPlainFlowOfInts() = runTest {
574579
val flow = client.plainFlowOfInts.toList()

0 commit comments

Comments
 (0)