-
Notifications
You must be signed in to change notification settings - Fork 19
Report download progress #155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 16 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
450c073
Report sync download progress
simolus3 931186d
Unit test for computing fractions
simolus3 ac5b01b
Reformat
simolus3 a7ca311
Remove old copy method entirely
simolus3 6dba5b6
Add sync progress to example
simolus3 5aefa29
Improve state on startup
simolus3 7b6ce58
Don't debounce by default
simolus3 0a92db7
Add changelog entries
simolus3 a99e791
Fix progress test
simolus3 5113f5c
Fix version check, raise minimum version
simolus3 83f25bc
Simplify API
simolus3 17cd768
Merge remote-tracking branch 'origin/main' into sync-progress
simolus3 290b306
Fix sync progress test
simolus3 643f8af
Merge remote-tracking branch 'origin/main' into sync-progress
simolus3 87230f3
Update progress docs
simolus3 b4565cc
Raise minimum veresion
simolus3 f03aeb4
Remove debouncing logic
simolus3 b338fae
Fix other lint and formatting errors
simolus3 af8a6af
Merge remote-tracking branch 'origin/main' into sync-progress
simolus3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
compose/src/commonMain/kotlin/com/powersync/compose/DatabaseState.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.powersync.compose | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.State | ||
import androidx.compose.runtime.collectAsState | ||
import com.powersync.sync.SyncStatus | ||
import com.powersync.sync.SyncStatusData | ||
import kotlinx.coroutines.FlowPreview | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.debounce | ||
import kotlin.time.Duration | ||
|
||
@OptIn(FlowPreview::class) | ||
@Composable | ||
public fun SyncStatus.composeState(debounce: Duration=Duration.ZERO): State<SyncStatusData> { | ||
var flow: Flow<SyncStatusData> = asFlow() | ||
if (debounce.isPositive()) { | ||
flow = flow.debounce(debounce) | ||
} | ||
|
||
return flow.collectAsState(initial = this) | ||
} |
File renamed without changes.
344 changes: 344 additions & 0 deletions
344
core/src/commonIntegrationTest/kotlin/com/powersync/sync/SyncProgressTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,344 @@ | ||
package com.powersync.sync | ||
|
||
import app.cash.turbine.ReceiveTurbine | ||
import app.cash.turbine.turbineScope | ||
import com.powersync.bucket.BucketChecksum | ||
import com.powersync.bucket.BucketPriority | ||
import com.powersync.bucket.Checkpoint | ||
import com.powersync.bucket.OpType | ||
import com.powersync.bucket.OplogEntry | ||
import com.powersync.testutils.ActiveDatabaseTest | ||
import com.powersync.testutils.databaseTest | ||
import com.powersync.testutils.waitFor | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.test.runTest | ||
import kotlin.test.BeforeTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertNull | ||
import kotlin.test.assertTrue | ||
|
||
class SyncProgressTest { | ||
private var lastOpId = 0 | ||
|
||
@BeforeTest | ||
fun resetOpId() { | ||
lastOpId = 0 | ||
} | ||
|
||
private fun bucket( | ||
name: String, | ||
count: Int, | ||
priority: BucketPriority = BucketPriority(3), | ||
): BucketChecksum = | ||
BucketChecksum( | ||
bucket = name, | ||
priority = priority, | ||
checksum = 0, | ||
count = count, | ||
) | ||
|
||
private suspend fun ActiveDatabaseTest.addDataLine( | ||
bucket: String, | ||
amount: Int, | ||
) { | ||
syncLines.send( | ||
SyncLine.SyncDataBucket( | ||
bucket = bucket, | ||
data = | ||
List(amount) { | ||
OplogEntry( | ||
checksum = 0, | ||
opId = (++lastOpId).toString(), | ||
op = OpType.PUT, | ||
rowId = lastOpId.toString(), | ||
rowType = bucket, | ||
data = "{}", | ||
) | ||
}, | ||
after = null, | ||
nextAfter = null, | ||
), | ||
) | ||
} | ||
|
||
private suspend fun ActiveDatabaseTest.addCheckpointComplete(priority: BucketPriority? = null) { | ||
if (priority != null) { | ||
syncLines.send( | ||
SyncLine.CheckpointPartiallyComplete( | ||
lastOpId = lastOpId.toString(), | ||
priority = priority, | ||
), | ||
) | ||
} else { | ||
syncLines.send(SyncLine.CheckpointComplete(lastOpId = lastOpId.toString())) | ||
} | ||
} | ||
|
||
private suspend fun ReceiveTurbine<SyncStatusData>.expectProgress( | ||
total: Pair<Int, Int>, | ||
priorities: Map<BucketPriority, Pair<Int, Int>> = emptyMap(), | ||
) { | ||
val item = awaitItem() | ||
val progress = item.downloadProgress ?: error("Expected download progress on $item") | ||
|
||
assertTrue { item.downloading } | ||
assertEquals(total.first, progress.downloadedOperations) | ||
assertEquals(total.second, progress.totalOperations) | ||
|
||
priorities.forEach { (priority, expected) -> | ||
val (expectedDownloaded, expectedTotal) = expected | ||
val progress = progress.untilPriority(priority) | ||
assertEquals(expectedDownloaded, progress.downloadedOperations) | ||
assertEquals(expectedTotal, progress.totalOperations) | ||
} | ||
} | ||
|
||
private suspend fun ReceiveTurbine<SyncStatusData>.expectNotDownloading() { | ||
awaitItem().also { | ||
assertFalse { it.downloading } | ||
assertNull(it.downloadProgress) | ||
} | ||
} | ||
|
||
@Test | ||
fun withoutPriorities() = | ||
databaseTest { | ||
database.connect(connector) | ||
|
||
turbineScope { | ||
val turbine = database.currentStatus.asFlow().testIn(this) | ||
turbine.waitFor { it.connected && !it.downloading } | ||
|
||
// Send checkpoint with 10 ops, progress should be 0/10 | ||
syncLines.send( | ||
SyncLine.FullCheckpoint( | ||
Checkpoint( | ||
lastOpId = "10", | ||
checksums = listOf(bucket("a", 10)), | ||
), | ||
), | ||
) | ||
turbine.expectProgress(0 to 10) | ||
|
||
addDataLine("a", 10) | ||
turbine.expectProgress(10 to 10) | ||
|
||
addCheckpointComplete() | ||
turbine.expectNotDownloading() | ||
|
||
// Emit new data, progress should be 0/2 instead of 10/12 | ||
syncLines.send( | ||
SyncLine.CheckpointDiff( | ||
lastOpId = "12", | ||
updatedBuckets = listOf(bucket("a", 12)), | ||
removedBuckets = emptyList(), | ||
), | ||
) | ||
turbine.expectProgress(0 to 2) | ||
|
||
addDataLine("a", 2) | ||
turbine.expectProgress(2 to 2) | ||
|
||
addCheckpointComplete() | ||
turbine.expectNotDownloading() | ||
|
||
turbine.cancel() | ||
} | ||
|
||
database.close() | ||
syncLines.close() | ||
} | ||
|
||
@Test | ||
fun interruptedSync() = | ||
databaseTest { | ||
database.connect(connector) | ||
|
||
turbineScope { | ||
val turbine = database.currentStatus.asFlow().testIn(this) | ||
turbine.waitFor { it.connected && !it.downloading } | ||
|
||
// Send checkpoint with 10 ops, progress should be 0/10 | ||
syncLines.send( | ||
SyncLine.FullCheckpoint( | ||
Checkpoint( | ||
lastOpId = "10", | ||
checksums = listOf(bucket("a", 10)), | ||
), | ||
), | ||
) | ||
turbine.expectProgress(0 to 10) | ||
|
||
addDataLine("a", 5) | ||
turbine.expectProgress(5 to 10) | ||
|
||
turbine.cancel() | ||
} | ||
|
||
// Emulate the app closing | ||
database.close() | ||
syncLines.close() | ||
|
||
// And reconnecting | ||
database = openDatabase() | ||
syncLines = Channel() | ||
database.connect(connector) | ||
|
||
turbineScope { | ||
val turbine = database.currentStatus.asFlow().testIn(this) | ||
turbine.waitFor { it.connected && !it.downloading } | ||
|
||
// Send the same checkpoint as before | ||
syncLines.send( | ||
SyncLine.FullCheckpoint( | ||
Checkpoint( | ||
lastOpId = "10", | ||
checksums = listOf(bucket("a", 10)), | ||
), | ||
), | ||
) | ||
|
||
// Progress should be restored: 5 / 10 instead of 0/5 | ||
turbine.expectProgress(5 to 10) | ||
|
||
addDataLine("a", 5) | ||
turbine.expectProgress(10 to 10) | ||
addCheckpointComplete() | ||
turbine.expectNotDownloading() | ||
|
||
turbine.cancel() | ||
} | ||
|
||
database.close() | ||
syncLines.close() | ||
} | ||
|
||
@Test | ||
fun interruptedSyncWithNewCheckpoint() = | ||
databaseTest { | ||
database.connect(connector) | ||
|
||
turbineScope { | ||
val turbine = database.currentStatus.asFlow().testIn(this) | ||
turbine.waitFor { it.connected && !it.downloading } | ||
syncLines.send( | ||
SyncLine.FullCheckpoint( | ||
Checkpoint( | ||
lastOpId = "10", | ||
checksums = listOf(bucket("a", 10)), | ||
), | ||
), | ||
) | ||
turbine.expectProgress(0 to 10) | ||
|
||
addDataLine("a", 5) | ||
turbine.expectProgress(5 to 10) | ||
|
||
turbine.cancel() | ||
} | ||
|
||
// Close and re-connect | ||
database.close() | ||
syncLines.close() | ||
database = openDatabase() | ||
syncLines = Channel() | ||
database.connect(connector) | ||
|
||
turbineScope { | ||
val turbine = database.currentStatus.asFlow().testIn(this) | ||
turbine.waitFor { it.connected && !it.downloading } | ||
|
||
// Send checkpoint with two more ops | ||
syncLines.send( | ||
SyncLine.FullCheckpoint( | ||
Checkpoint( | ||
lastOpId = "12", | ||
checksums = listOf(bucket("a", 12)), | ||
), | ||
), | ||
) | ||
|
||
turbine.expectProgress(5 to 12) | ||
|
||
addDataLine("a", 7) | ||
turbine.expectProgress(12 to 12) | ||
addCheckpointComplete() | ||
turbine.expectNotDownloading() | ||
|
||
turbine.cancel() | ||
} | ||
|
||
database.close() | ||
syncLines.close() | ||
} | ||
|
||
@Test | ||
fun differentPriorities() = | ||
databaseTest { | ||
database.connect(connector) | ||
|
||
turbineScope { | ||
val turbine = database.currentStatus.asFlow().testIn(this) | ||
turbine.waitFor { it.connected && !it.downloading } | ||
|
||
suspend fun expectProgress( | ||
prio0: Pair<Int, Int>, | ||
prio2: Pair<Int, Int>, | ||
) { | ||
turbine.expectProgress(prio2, mapOf(BucketPriority(0) to prio0, BucketPriority(2) to prio2)) | ||
} | ||
|
||
syncLines.send( | ||
SyncLine.FullCheckpoint( | ||
Checkpoint( | ||
lastOpId = "10", | ||
checksums = | ||
listOf( | ||
bucket("a", 5, BucketPriority(0)), | ||
bucket("b", 5, BucketPriority(2)), | ||
), | ||
), | ||
), | ||
) | ||
expectProgress(0 to 5, 0 to 10) | ||
|
||
addDataLine("a", 5) | ||
expectProgress(5 to 5, 5 to 10) | ||
|
||
addCheckpointComplete(BucketPriority(0)) | ||
expectProgress(5 to 5, 5 to 10) | ||
|
||
addDataLine("b", 2) | ||
expectProgress(5 to 5, 7 to 10) | ||
|
||
// Before syncing b fully, send a new checkpoint | ||
syncLines.send( | ||
SyncLine.CheckpointDiff( | ||
lastOpId = "14", | ||
updatedBuckets = | ||
listOf( | ||
bucket("a", 8, BucketPriority(0)), | ||
bucket("b", 6, BucketPriority(2)), | ||
), | ||
removedBuckets = emptyList(), | ||
), | ||
) | ||
expectProgress(5 to 8, 7 to 14) | ||
|
||
addDataLine("a", 3) | ||
expectProgress(8 to 8, 10 to 14) | ||
addDataLine("b", 4) | ||
expectProgress(8 to 8, 14 to 14) | ||
|
||
addCheckpointComplete() | ||
turbine.expectNotDownloading() | ||
|
||
turbine.cancel() | ||
} | ||
|
||
database.close() | ||
syncLines.close() | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.