Skip to content

Commit 5e31992

Browse files
committed
even more fixes
1 parent 87aca3e commit 5e31992

File tree

5 files changed

+20
-3
lines changed

5 files changed

+20
-3
lines changed

core/src/commonIntegrationTest/kotlin/com/powersync/AttachmentsTest.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import dev.mokkery.verifySuspend
2929
import io.kotest.matchers.shouldBe
3030
import io.kotest.matchers.shouldNotBe
3131
import kotlinx.coroutines.flow.Flow
32+
import kotlinx.coroutines.flow.distinctUntilChanged
3233
import kotlinx.coroutines.flow.flowOf
3334
import kotlinx.coroutines.flow.onEach
3435
import kotlinx.io.files.Path
@@ -60,7 +61,17 @@ class AttachmentsTest {
6061
database
6162
.watch("SELECT * FROM attachments") {
6263
Attachment.fromCursor(it)
63-
}.onEach { logger.i { "attachments table results: $it" } }
64+
}
65+
// Because tests run on slow machines, it's possible for a schedule like the following
66+
// to happen:
67+
// 1. the attachment is initially saved with QUEUED_DOWNLOAD, triggering a query.
68+
// 2. the attachment is downloaded fast, but the query flow is paused.
69+
// 3. only now is the query scheduled by 1 actually running, reporting SYNCED.
70+
// 4. we delete the attachment.
71+
// 5. thanks to 2, the query runs again, again reporting SYNCED.
72+
// 6. Our test now fails because the second event should be ARCHIVED.
73+
.distinctUntilChanged()
74+
.onEach { logger.i { "attachments table results: $it" } }
6475

6576
suspend fun updateSchema(db: PowerSyncDatabase) {
6677
db.updateSchema(

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ internal class ActiveDatabaseTest(
112112
dbDirectory = testDirectory,
113113
logger = logger,
114114
scope = scope,
115+
// We want to reduce concurrency in tests to make their output more predictable.
116+
readPoolSize = 1,
115117
)
116118
doOnCleanup { db.close() }
117119
return db

core/src/commonMain/kotlin/com/powersync/PowerSyncDatabaseFactory.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public fun PowerSyncDatabase(
4040
scope = scope,
4141
logger = generatedLogger,
4242
dbDirectory = dbDirectory,
43+
readPoolSize = 5,
4344
)
4445
}
4546

@@ -51,6 +52,7 @@ internal fun createPowerSyncDatabaseImpl(
5152
scope: CoroutineScope,
5253
logger: Logger,
5354
dbDirectory: String?,
55+
readPoolSize: Int,
5456
): PowerSyncDatabaseImpl {
5557
val identifier = dbDirectory + dbFilename
5658
val activeDatabaseGroup = ActiveDatabaseGroup.referenceDatabase(logger, identifier)
@@ -63,6 +65,7 @@ internal fun createPowerSyncDatabaseImpl(
6365
dbFilename,
6466
dbDirectory,
6567
activeDatabaseGroup.first.group.writeLockMutex,
68+
readPoolSize = readPoolSize,
6669
)
6770
}
6871

core/src/commonMain/kotlin/com/powersync/db/driver/InternalConnectionPool.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ internal class InternalConnectionPool(
2020
private val dbFilename: String,
2121
private val dbDirectory: String?,
2222
private val writeLockMutex: Mutex,
23+
readPoolSize: Int,
2324
) : SQLiteConnectionPool {
2425
private val writeConnection = newConnection(false)
25-
private val readPool = ReadPool({ newConnection(true) }, scope = scope)
26+
private val readPool = ReadPool({ newConnection(true) }, size = readPoolSize, scope = scope)
2627

2728
// MutableSharedFlow to emit batched table updates
2829
private val tableUpdatesFlow = MutableSharedFlow<Set<String>>(replay = 0)

core/src/commonMain/kotlin/com/powersync/db/driver/ReadPool.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import kotlinx.coroutines.launch
1818
@OptIn(ExperimentalPowerSyncAPI::class)
1919
internal class ReadPool(
2020
factory: () -> SQLiteConnection,
21-
size: Int = 5,
21+
size: Int,
2222
private val scope: CoroutineScope,
2323
) {
2424
private val available = Channel<Pair<SQLiteConnection, CompletableDeferred<Unit>>>()

0 commit comments

Comments
 (0)