Skip to content

Commit 0b7ae20

Browse files
committed
Fix reconnect race in test
1 parent 8843612 commit 0b7ae20

File tree

6 files changed

+28
-8
lines changed

6 files changed

+28
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Add `PowerSyncDatabase.inMemory` to create an in-memory SQLite database with PowerSync.
66
This may be useful for testing.
77
- The Supabase connector can now be subclassed to customize how rows are uploaded and how errors are handled.
8+
- Experimental support for sync streams.
89

910
## 1.6.1
1011

core/src/commonIntegrationTest/kotlin/com/powersync/sync/SyncStreamTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,10 @@ class SyncStreamTest : AbstractSyncTest(true) {
163163
requestedSyncStreams.clear()
164164

165165
val subscription = database.syncStream("a").subscribe()
166+
waitForSyncLinesChannelClosed()
166167

167168
// Adding the subscription should reconnect
168-
turbine.waitFor { it.connected && !it.downloading }
169+
turbine.waitFor { it.connected }
169170
requestedSyncStreams shouldHaveSingleElement {
170171
val streams = it.jsonObject["streams"]!!.jsonObject
171172
val subscriptions = streams["subscriptions"]!!.jsonArray

core/src/commonIntegrationTest/kotlin/com/powersync/testutils/TestUtils.kt

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ import io.ktor.client.HttpClient
2323
import io.ktor.client.engine.mock.toByteArray
2424
import io.ktor.http.ContentType
2525
import kotlinx.coroutines.channels.Channel
26+
import kotlinx.coroutines.suspendCancellableCoroutine
2627
import kotlinx.coroutines.test.TestScope
2728
import kotlinx.coroutines.test.runTest
2829
import kotlinx.io.files.Path
2930
import kotlinx.serialization.json.JsonElement
31+
import kotlin.coroutines.resume
3032

3133
expect val factory: DatabaseDriverFactory
3234

@@ -102,6 +104,23 @@ internal class ActiveDatabaseTest(
102104

103105
var connector = TestConnector()
104106

107+
suspend fun waitForSyncLinesChannelClosed() {
108+
suspendCancellableCoroutine { continuation ->
109+
var cancelled = false
110+
continuation.invokeOnCancellation {
111+
cancelled = true
112+
}
113+
114+
syncLines.invokeOnClose {
115+
if (!cancelled) {
116+
continuation.resume(Unit)
117+
}
118+
119+
syncLines = Channel()
120+
}
121+
}
122+
}
123+
105124
fun openDatabase(schema: Schema = Schema(UserRow.table)): PowerSyncDatabaseImpl {
106125
logger.d { "Opening database $databaseName in directory $testDirectory" }
107126
val db =
@@ -123,7 +142,7 @@ internal class ActiveDatabaseTest(
123142
fun createSyncClient(): HttpClient {
124143
val engine =
125144
MockSyncService(
126-
lines = syncLines,
145+
lines = { syncLines },
127146
generateCheckpoint = { checkpointResponse() },
128147
syncLinesContentType = { syncLinesContentType },
129148
trackSyncRequest = {

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import kotlinx.serialization.json.JsonElement
3838
*/
3939
@OptIn(LegacySyncImplementation::class)
4040
internal class MockSyncService(
41-
private val lines: ReceiveChannel<Any>,
41+
private val lines: () -> ReceiveChannel<Any>,
4242
private val syncLinesContentType: () -> ContentType,
4343
private val generateCheckpoint: () -> WriteCheckpointResponse,
4444
private val trackSyncRequest: suspend (HttpRequestData) -> Unit,
@@ -60,12 +60,11 @@ internal class MockSyncService(
6060
trackSyncRequest(data)
6161
val job =
6262
scope.writer {
63-
lines.consume {
63+
lines().consume {
6464
while (true) {
6565
// Wait for a downstream listener being ready before requesting a sync line
6666
channel.awaitFreeSpace()
67-
val line = receive()
68-
when (line) {
67+
when (val line = receive()) {
6968
is SyncLine -> {
7069
val serializedLine = JsonUtil.json.encodeToString(line)
7170
channel.writeStringUtf8("$serializedLine\n")

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ development=true
1919
RELEASE_SIGNING_ENABLED=true
2020
# Library config
2121
GROUP=com.powersync
22-
LIBRARY_VERSION=1.6.1
22+
LIBRARY_VERSION=1.7.0
2323
GITHUB_REPO=https://github.com/powersync-ja/powersync-kotlin.git
2424
# POM
2525
POM_URL=https://github.com/powersync-ja/powersync-kotlin/

integrations/room/src/commonIntegrationTest/kotlin/com/powersync/integrations/room/TestDatabase.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ interface UserDao {
3737
suspend fun delete(user: User)
3838
}
3939

40-
@Database(entities = [User::class], version = 1)
40+
@Database(entities = [User::class], version = 1, exportSchema = false)
4141
@ConstructedBy(TestDatabaseConstructor::class)
4242
abstract class TestDatabase : RoomDatabase() {
4343
abstract fun userDao(): UserDao

0 commit comments

Comments
 (0)