Skip to content

Add collect once check for client streams #431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package kotlinx.rpc.krpc.server.internal

import kotlinx.atomicfu.atomic
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
Expand Down Expand Up @@ -65,7 +66,13 @@ internal class ServerStreamContext {
map.remove(streamId)
}

val collected = AtomicBoolean(false)

val flow = flow {
check(collected.value.compareAndSet(expect = false, update = true)) {
"Request streams can only be collected once"
}

for (message in channel) {
when (message) {
is StreamCancel -> {
Expand All @@ -92,5 +99,9 @@ internal class ServerStreamContext {
private fun streamCanceled(): Throwable = NoSuchElementException("Stream canceled")
}

private class AtomicBoolean(initialValue: Boolean) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😁

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🌚

val value = atomic(initialValue)
}

private data class StreamCancel(val cause: Throwable? = null)
private data object StreamEnd
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ interface KrpcTestService {
suspend fun nullableParam(arg1: String?): String
suspend fun nullableReturn(returnNull: Boolean): TestClass?
suspend fun variance(arg2: TestList<in TestClass>, arg3: TestList2<TestClass>): TestList<out TestClass>?
suspend fun collectOnce(flow: Flow<String>)

suspend fun nonSerializableClass(localDate: LocalDate): LocalDate
suspend fun nonSerializableClassWithSerializer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import kotlinx.serialization.Serializable
import kotlin.coroutines.resumeWithException
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith

@OptIn(ExperimentalCoroutinesApi::class)
class KrpcTestServiceBackend : KrpcTestService {
Expand Down Expand Up @@ -114,6 +115,11 @@ class KrpcTestServiceBackend : KrpcTestService {
return TestList(3)
}

override suspend fun collectOnce(flow: Flow<String>) {
flow.toList()
assertFailsWith<IllegalStateException> { flow.toList() }
}

override suspend fun nonSerializableClass(localDate: LocalDate): LocalDate {
return LocalDate(localDate.year, localDate.month, localDate.day + 1)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ abstract class KrpcTransportTestBase {
assertEquals(TestList(3), result)
}

@Test
fun collectOnce() = runTest {
client.collectOnce(flowOf("test1", "test2", "test3"))
}

@Test
fun incomingStreamSyncCollect() = runTest {
val result = client.incomingStreamSyncCollect(flowOf("test1", "test2", "test3"))
Expand Down