Skip to content

Commit

Permalink
feat: do not invalidate credentials on io error (#104)
Browse files Browse the repository at this point in the history
* feat: do not invalidate credentials on io error

* chore: remove tests as they don't really work

* chore: update changelog

* chore: remove invalidation

* chore: update changelog
  • Loading branch information
DominicGBauer authored Jan 15, 2025
1 parent 92f1570 commit 3f2afa4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

## 1.0.0-BETA16

* Add `close` method to database methods

* Add `close` method to database methods
* Throw when error is a `CancellationError` and remove invalidation for all errors in `streamingSync` catch.
## 1.0.0-BETA15

* Update powersync-sqlite-core to 0.3.8
Expand Down
8 changes: 4 additions & 4 deletions core/src/commonMain/kotlin/com/powersync/sync/SyncStream.kt
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ internal class SyncStream(
// break;
// }
} catch (e: Exception) {
// If the coroutine was cancelled, don't log an error
if (e !is CancellationException) {
logger.e(e) { "Error in streamingSync" }
if (e is CancellationException) {
throw e
}
invalidCredentials = true

logger.e(e) { "Error in streamingSync: $e" }
status.update(
downloadError = e,
)
Expand Down
54 changes: 54 additions & 0 deletions core/src/commonTest/kotlin/com/powersync/sync/SyncStreamTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ import co.touchlab.kermit.TestConfig
import co.touchlab.kermit.TestLogWriter
import com.powersync.bucket.BucketStorage
import com.powersync.connectors.PowerSyncBackendConnector
import com.powersync.connectors.PowerSyncCredentials
import com.powersync.db.crud.CrudEntry
import com.powersync.db.crud.UpdateType
import dev.mokkery.answering.returns
import dev.mokkery.everySuspend
import dev.mokkery.mock
import dev.mokkery.verify
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.withTimeout
import kotlinx.serialization.json.JsonObject
import kotlin.test.BeforeTest
import kotlin.test.Test
Expand Down Expand Up @@ -112,4 +116,54 @@ class SyncStreamTest {
assertEquals(Severity.Error, severity)
}
}

@Test
fun testStreamingSyncBasicFlow() =
runTest {
bucketStorage =
mock<BucketStorage> {
everySuspend { getClientId() } returns "test-client-id"
everySuspend { getBucketStates() } returns emptyList()
}

connector =
mock<PowerSyncBackendConnector> {
everySuspend { getCredentialsCached() } returns
PowerSyncCredentials(
token = "test-token",
userId = "test-user",
endpoint = "https://test.com",
)
}

syncStream =
SyncStream(
bucketStorage = bucketStorage,
connector = connector,
uploadCrud = { },
retryDelayMs = 10,
logger = logger,
params = JsonObject(emptyMap()),
)

// Launch streaming sync in a coroutine that we'll cancel after verification
val job =
launch {
syncStream.streamingSync()
}

// Wait for status to update
withTimeout(1000) {
while (!syncStream.status.connecting) {
delay(10)
}
}

// Verify initial state
assertEquals(true, syncStream.status.connecting)
assertEquals(false, syncStream.status.connected)

// Clean up
job.cancel()
}
}

0 comments on commit 3f2afa4

Please sign in to comment.