|
| 1 | +package com.powersync |
| 2 | + |
| 3 | +import app.cash.turbine.turbineScope |
| 4 | +import co.touchlab.kermit.Logger |
| 5 | +import co.touchlab.kermit.Severity |
| 6 | +import co.touchlab.kermit.TestConfig |
| 7 | +import com.powersync.bucket.BucketChecksum |
| 8 | +import com.powersync.bucket.BucketPriority |
| 9 | +import com.powersync.bucket.Checkpoint |
| 10 | +import com.powersync.bucket.OpType |
| 11 | +import com.powersync.bucket.OplogEntry |
| 12 | +import com.powersync.connectors.PowerSyncBackendConnector |
| 13 | +import com.powersync.connectors.PowerSyncCredentials |
| 14 | +import com.powersync.db.PowerSyncDatabaseImpl |
| 15 | +import com.powersync.db.schema.Schema |
| 16 | +import com.powersync.sync.SyncLine |
| 17 | +import com.powersync.sync.SyncStream |
| 18 | +import com.powersync.testutils.MockSyncService |
| 19 | +import com.powersync.testutils.UserRow |
| 20 | +import com.powersync.testutils.cleanup |
| 21 | +import com.powersync.testutils.waitFor |
| 22 | +import com.powersync.utils.JsonUtil |
| 23 | +import dev.mokkery.answering.returns |
| 24 | +import dev.mokkery.everySuspend |
| 25 | +import dev.mokkery.mock |
| 26 | +import kotlinx.coroutines.CoroutineScope |
| 27 | +import kotlinx.coroutines.channels.Channel |
| 28 | +import kotlinx.coroutines.flow.receiveAsFlow |
| 29 | +import kotlinx.coroutines.runBlocking |
| 30 | +import kotlinx.coroutines.test.runTest |
| 31 | +import kotlinx.serialization.encodeToString |
| 32 | +import kotlinx.serialization.json.JsonObject |
| 33 | +import kotlin.test.AfterTest |
| 34 | +import kotlin.test.BeforeTest |
| 35 | +import kotlin.test.Test |
| 36 | +import kotlin.test.assertEquals |
| 37 | +import kotlin.test.assertFalse |
| 38 | +import kotlin.test.assertTrue |
| 39 | +import kotlin.time.Duration.Companion.seconds |
| 40 | + |
| 41 | +@OptIn(co.touchlab.kermit.ExperimentalKermitApi::class) |
| 42 | +class SyncIntegrationTest { |
| 43 | + private val logger = |
| 44 | + Logger( |
| 45 | + TestConfig( |
| 46 | + minSeverity = Severity.Debug, |
| 47 | + logWriterList = listOf(), |
| 48 | + ), |
| 49 | + ) |
| 50 | + private lateinit var database: PowerSyncDatabaseImpl |
| 51 | + private lateinit var connector: PowerSyncBackendConnector |
| 52 | + private lateinit var syncLines: Channel<SyncLine> |
| 53 | + |
| 54 | + @BeforeTest |
| 55 | + fun setup() { |
| 56 | + cleanup("testdb") |
| 57 | + database = openDb() |
| 58 | + connector = |
| 59 | + mock<PowerSyncBackendConnector> { |
| 60 | + everySuspend { getCredentialsCached() } returns |
| 61 | + PowerSyncCredentials( |
| 62 | + token = "test-token", |
| 63 | + userId = "test-user", |
| 64 | + endpoint = "https://test.com", |
| 65 | + ) |
| 66 | + |
| 67 | + everySuspend { invalidateCredentials() } returns Unit |
| 68 | + } |
| 69 | + syncLines = Channel() |
| 70 | + |
| 71 | + runBlocking { |
| 72 | + database.disconnectAndClear(true) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @AfterTest |
| 77 | + fun teardown() { |
| 78 | + cleanup("testdb") |
| 79 | + } |
| 80 | + |
| 81 | + private fun openDb() = |
| 82 | + PowerSyncDatabase( |
| 83 | + factory = com.powersync.testutils.factory, |
| 84 | + schema = Schema(UserRow.table), |
| 85 | + dbFilename = "testdb", |
| 86 | + ) as PowerSyncDatabaseImpl |
| 87 | + |
| 88 | + private fun CoroutineScope.syncStream(): SyncStream { |
| 89 | + val client = MockSyncService.client(this, syncLines.receiveAsFlow()) |
| 90 | + return SyncStream( |
| 91 | + bucketStorage = database.bucketStorage, |
| 92 | + connector = connector, |
| 93 | + httpEngine = client, |
| 94 | + uploadCrud = { }, |
| 95 | + retryDelayMs = 10, |
| 96 | + logger = logger, |
| 97 | + params = JsonObject(emptyMap()), |
| 98 | + ) |
| 99 | + } |
| 100 | + |
| 101 | + private suspend fun expectUserCount(amount: Int) { |
| 102 | + val users = database.getAll("SELECT * FROM users;") { UserRow.from(it) } |
| 103 | + assertEquals(amount, users.size, "Expected $amount users, got $users") |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + fun testPartialSync() = |
| 108 | + runTest { |
| 109 | + val syncStream = syncStream() |
| 110 | + database.connect(syncStream, 1000L) |
| 111 | + |
| 112 | + val checksums = |
| 113 | + buildList { |
| 114 | + for (prio in 0..3) { |
| 115 | + add( |
| 116 | + BucketChecksum( |
| 117 | + bucket = "bucket$prio", |
| 118 | + priority = BucketPriority(prio), |
| 119 | + checksum = 10 + prio, |
| 120 | + ), |
| 121 | + ) |
| 122 | + } |
| 123 | + } |
| 124 | + var operationId = 1 |
| 125 | + |
| 126 | + suspend fun pushData(priority: Int) { |
| 127 | + val id = operationId++ |
| 128 | + |
| 129 | + syncLines.send( |
| 130 | + SyncLine.SyncDataBucket( |
| 131 | + bucket = "bucket$priority", |
| 132 | + data = |
| 133 | + listOf( |
| 134 | + OplogEntry( |
| 135 | + checksum = (priority + 10).toLong(), |
| 136 | + data = |
| 137 | + JsonUtil.json.encodeToString( |
| 138 | + mapOf( |
| 139 | + "name" to "user $priority", |
| 140 | + "email" to "$priority@example.org", |
| 141 | + ), |
| 142 | + ), |
| 143 | + op = OpType.PUT, |
| 144 | + opId = id.toString(), |
| 145 | + rowId = "prio$priority", |
| 146 | + rowType = "users", |
| 147 | + ), |
| 148 | + ), |
| 149 | + after = null, |
| 150 | + nextAfter = null, |
| 151 | + ), |
| 152 | + ) |
| 153 | + } |
| 154 | + |
| 155 | + turbineScope(timeout = 10.0.seconds) { |
| 156 | + val turbine = syncStream.status.asFlow().testIn(this) |
| 157 | + turbine.waitFor { it.connected } |
| 158 | + expectUserCount(0) |
| 159 | + |
| 160 | + syncLines.send( |
| 161 | + SyncLine.FullCheckpoint( |
| 162 | + Checkpoint( |
| 163 | + lastOpId = "4", |
| 164 | + checksums = checksums, |
| 165 | + ), |
| 166 | + ), |
| 167 | + ) |
| 168 | + |
| 169 | + // Emit a partial sync complete for each priority but the last. |
| 170 | + for (priorityNo in 0..<3) { |
| 171 | + val priority = BucketPriority(priorityNo) |
| 172 | + pushData(priorityNo) |
| 173 | + syncLines.send( |
| 174 | + SyncLine.CheckpointPartiallyComplete( |
| 175 | + lastOpId = operationId.toString(), |
| 176 | + priority = priority, |
| 177 | + ), |
| 178 | + ) |
| 179 | + |
| 180 | + turbine.waitFor { it.priorityStatusFor(priority).hasSynced == true } |
| 181 | + expectUserCount(priorityNo + 1) |
| 182 | + } |
| 183 | + |
| 184 | + // Then complete the sync |
| 185 | + pushData(3) |
| 186 | + syncLines.send( |
| 187 | + SyncLine.CheckpointComplete( |
| 188 | + lastOpId = operationId.toString(), |
| 189 | + ), |
| 190 | + ) |
| 191 | + turbine.waitFor { it.hasSynced == true } |
| 192 | + expectUserCount(4) |
| 193 | + |
| 194 | + turbine.cancel() |
| 195 | + } |
| 196 | + |
| 197 | + syncLines.close() |
| 198 | + } |
| 199 | + |
| 200 | + @Test |
| 201 | + fun testRemembersLastPartialSync() = |
| 202 | + runTest { |
| 203 | + val syncStream = syncStream() |
| 204 | + database.connect(syncStream, 1000L) |
| 205 | + |
| 206 | + syncLines.send( |
| 207 | + SyncLine.FullCheckpoint( |
| 208 | + Checkpoint( |
| 209 | + lastOpId = "4", |
| 210 | + checksums = listOf(BucketChecksum(bucket = "bkt", priority = BucketPriority(1), checksum = 0)), |
| 211 | + ), |
| 212 | + ), |
| 213 | + ) |
| 214 | + syncLines.send( |
| 215 | + SyncLine.CheckpointPartiallyComplete( |
| 216 | + lastOpId = "0", |
| 217 | + priority = BucketPriority(1), |
| 218 | + ), |
| 219 | + ) |
| 220 | + |
| 221 | + database.waitForFirstSync(BucketPriority(1)) |
| 222 | + database.close() |
| 223 | + |
| 224 | + // Connect to the same database again |
| 225 | + database = openDb() |
| 226 | + assertFalse { database.currentStatus.hasSynced == true } |
| 227 | + assertTrue { database.currentStatus.priorityStatusFor(BucketPriority(1)).hasSynced == true } |
| 228 | + database.close() |
| 229 | + syncLines.close() |
| 230 | + } |
| 231 | +} |
0 commit comments