Skip to content

Commit 6a734ab

Browse files
committed
Make checkpoint during upload test more reliable
1 parent eb5c7d1 commit 6a734ab

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import io.kotest.matchers.shouldNotBe
3232
import io.kotest.matchers.string.shouldContain
3333
import kotlinx.coroutines.CompletableDeferred
3434
import kotlinx.coroutines.DelicateCoroutinesApi
35+
import kotlinx.coroutines.Dispatchers
36+
import kotlinx.coroutines.withContext
3537
import kotlinx.serialization.json.jsonObject
3638
import kotlinx.serialization.json.jsonPrimitive
3739
import kotlin.test.Test
@@ -482,10 +484,21 @@ abstract class BaseSyncIntegrationTest(
482484
turbineScope {
483485
val turbine = database.currentStatus.asFlow().testIn(this)
484486
syncLines.send(SyncLine.KeepAlive(1234))
485-
turbine.waitFor { it.connected && !it.uploading }
487+
turbine.waitFor { it.connected }
486488
turbine.cancelAndIgnoreRemainingEvents()
487489
}
488490

491+
// Wait for the first upload task triggered when connecting to be complete.
492+
withContext(Dispatchers.Default) {
493+
waitFor {
494+
assertNotNull(
495+
logWriter.logs.find {
496+
it.message.contains("crud upload: notify completion")
497+
},
498+
)
499+
}
500+
}
501+
489502
database.execute("INSERT INTO users (id, name, email) VALUES (uuid(), ?, ?)", listOf("local", "[email protected]"))
490503

491504
expectUserRows(1)

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import co.touchlab.kermit.Severity
99
import co.touchlab.kermit.TestConfig
1010
import co.touchlab.kermit.TestLogWriter
1111
import com.powersync.DatabaseDriverFactory
12+
import com.powersync.PowerSyncTestLogWriter
1213
import com.powersync.TestConnector
1314
import com.powersync.bucket.WriteCheckpointData
1415
import com.powersync.bucket.WriteCheckpointResponse
@@ -73,8 +74,8 @@ internal class ActiveDatabaseTest(
7374
lateinit var database: PowerSyncDatabaseImpl
7475

7576
val logWriter =
76-
TestLogWriter(
77-
loggable = Severity.Debug,
77+
PowerSyncTestLogWriter(
78+
loggable = Severity.Verbose,
7879
)
7980
val logger =
8081
Logger(

core/src/commonMain/kotlin/com/powersync/sync/SyncStream.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import io.ktor.utils.io.ByteReadChannel
3636
import io.ktor.utils.io.readUTF8Line
3737
import kotlinx.coroutines.CancellationException
3838
import kotlinx.coroutines.CompletableDeferred
39+
import kotlinx.coroutines.CoroutineName
3940
import kotlinx.coroutines.CoroutineScope
4041
import kotlinx.coroutines.Job
4142
import kotlinx.coroutines.NonCancellable
@@ -122,7 +123,7 @@ internal class SyncStream(
122123
}
123124

124125
fun triggerCrudUploadAsync(): Job =
125-
uploadScope.launch {
126+
uploadScope.launch(CoroutineName("triggerCrudUploadAsync")) {
126127
val thisIteration = PendingCrudUpload(CompletableDeferred())
127128
var holdingUploadLock = false
128129

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.powersync
2+
3+
import co.touchlab.kermit.ExperimentalKermitApi
4+
import co.touchlab.kermit.LogWriter
5+
import co.touchlab.kermit.Severity
6+
import co.touchlab.kermit.TestLogWriter.LogEntry
7+
import kotlinx.atomicfu.locks.reentrantLock
8+
import kotlinx.atomicfu.locks.withLock
9+
10+
/**
11+
* A version of the `TestLogWriter` from Kermit that uses a mutex around logs instead of throwing
12+
* for concurrent access.
13+
*/
14+
@OptIn(ExperimentalKermitApi::class)
15+
class PowerSyncTestLogWriter(private val loggable: Severity) : LogWriter() {
16+
private val lock = reentrantLock()
17+
private val _logs = mutableListOf<LogEntry>()
18+
19+
val logs: List<LogEntry>
20+
get() = lock.withLock { _logs.toList() }
21+
22+
override fun isLoggable(tag: String, severity: Severity): Boolean {
23+
return severity.ordinal >= loggable.ordinal
24+
}
25+
26+
override fun log(severity: Severity, message: String, tag: String, throwable: Throwable?) {
27+
lock.withLock {
28+
_logs.add(LogEntry(severity, message, tag, throwable))
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)