Skip to content

Commit 1db2f28

Browse files
committed
Wait for downstream listener
1 parent 29eb968 commit 1db2f28

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

core/src/commonTest/kotlin/com/powersync/testutils/MockSyncService.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ import io.ktor.http.HttpStatusCode
1616
import io.ktor.http.headersOf
1717
import io.ktor.util.date.GMTDate
1818
import io.ktor.utils.io.InternalAPI
19+
import io.ktor.utils.io.awaitFreeSpace
1920
import io.ktor.utils.io.writeStringUtf8
2021
import io.ktor.utils.io.writer
2122
import kotlinx.coroutines.CoroutineScope
2223
import kotlinx.coroutines.channels.ReceiveChannel
24+
import kotlinx.coroutines.channels.consume
2325
import kotlinx.coroutines.channels.consumeEach
2426
import kotlinx.serialization.encodeToString
2527

@@ -49,10 +51,15 @@ internal class MockSyncService(
4951
return if (data.url.encodedPath == "/sync/stream") {
5052
val job =
5153
scope.writer {
52-
lines.consumeEach {
53-
val serializedLine = JsonUtil.json.encodeToString(it)
54-
channel.writeStringUtf8("$serializedLine\n")
55-
channel.flush()
54+
lines.consume {
55+
while (true) {
56+
// Wait for a downstream listener being ready before requesting a sync line
57+
channel.awaitFreeSpace()
58+
val line = receive()
59+
val serializedLine = JsonUtil.json.encodeToString(line)
60+
channel.writeStringUtf8("$serializedLine\n")
61+
channel.flush()
62+
}
5663
}
5764
}
5865

0 commit comments

Comments
 (0)