Skip to content

Colota - Android GPS Location tracker

refactor(sync): move the Overland batch sender and the rate-limit wait out of SyncManager (#987)

Max Dietrich796b297cd24a
diff --git a/apps/docs/docs/development/architecture.md b/apps/docs/docs/development/architecture.md
index 6a636cef..2ba5e5e3 100644
--- a/apps/docs/docs/development/architecture.md
+++ b/apps/docs/docs/development/architecture.md
@@ -208,6 +208,7 @@ Orchestrates batch location uploads with:
- Configurable batch size (50 items per batch, 10 concurrent HTTP requests)
- Exponential backoff on failure
- A pass stops on a 429, and nothing is sent until its `Retry-After` wait is over (60 s without one, capped at 15 min): the periodic tick, a manual or zone-entry flush and an instant send all hold. In the per-point format a pass also stops on a 5xx or a network error once a group of 10 requests got nothing through, so an unreachable server costs one group of requests. A batch upload stops on its first 429, 5xx or network error
+- Overland batch uploads go through `OverlandBatchSender`, which halves a batch the server rejects with a 4xx until the refused row is alone. The rate-limit wait lives in `SyncState`, so it outlives the service that met the 429
- Periodic sync scheduling. A new interval (a profile switch) applies to the wait already running, measured from when it began; a send or a backoff in progress finishes first
- Manual flush support
- One pass at a time: a periodic tick and a manual flush wait for the running pass. An instant send leaves its row queued while a pass runs, and a pass skips rows an instant send is still posting
diff --git a/apps/mobile/android/app/src/main/java/com/colota/sync/OverlandBatchSender.kt b/apps/mobile/android/app/src/main/java/com/colota/sync/OverlandBatchSender.kt
new file mode 100644
index 00000000..8b75a54f
--- /dev/null
+++ b/apps/mobile/android/app/src/main/java/com/colota/sync/OverlandBatchSender.kt
@@ -0,0 +1,98 @@
+/**
+ * Copyright (C) 2026 Max Dietrich
+ * Licensed under the GNU AGPLv3. See LICENSE in the project root for details.
+ */
+
+package com.Colota.sync
+
+import com.Colota.data.DatabaseHelper
+import com.Colota.data.QueuedLocation
+import com.Colota.sync.http.BatchResult
+import com.Colota.sync.http.NetworkManager
+import com.Colota.util.AppLogger
+import kotlinx.coroutines.yield
+import org.json.JSONObject
+
+/** Posts queue rows as one Overland batch and bisects a rejected batch down to the rows the server refuses. */
+class OverlandBatchSender(
+ private val dbHelper: DatabaseHelper,
+ private val networkManager: NetworkManager,
+ private val onRateLimited: (retryAfterSeconds: Long?) -> Unit
+) {
+ companion object {
+ private const val TAG = "SyncManager"
+ }
+
+ data class BatchSendResult(val processed: Int, val failed: Int, val stop: Boolean)
+
+ suspend fun sendBatchRecursive(
+ items: List<QueuedLocation>,
+ endpoint: String,
+ authHeaders: Map<String, String>,
+ ): BatchSendResult {
+ if (items.isEmpty()) return BatchSendResult(0, 0, false)
+
+ // One corrupt payload must not fail the whole batch
+ val parsed = items.mapNotNull { item ->
+ try {
+ item to JSONObject(item.payload)
+ } catch (e: Exception) {
+ AppLogger.e(TAG, "Corrupt queue row ${item.queueId}, bumping retry counter", e)
+ dbHelper.incrementRetryCount(item.queueId, "Corrupt payload")
+ null
+ }
+ }
+ val corruptedFailed = items.size - parsed.size
+ if (parsed.isEmpty()) {
+ return BatchSendResult(processed = 0, failed = corruptedFailed, stop = false)
+ }
+
+ val parseableItems = parsed.map { it.first }
+ val payloads = parsed.map { it.second }
+ val customFields = PayloadBuilder.extractEnvelopeCustomFields(payloads.first())
+
+ val result = networkManager.sendBatchToEndpoint(
+ items = payloads,
+ customFields = customFields,
+ endpoint = endpoint,
+ extraHeaders = authHeaders,
+ )
+
+ return when (result) {
+ BatchResult.Success -> {
+ dbHelper.markLocationsSent(parseableItems.map { it.locationId })
+ dbHelper.removeBatchFromQueue(parseableItems.map { it.queueId })
+ BatchSendResult(processed = parseableItems.size, failed = corruptedFailed, stop = false)
+ }
+ is BatchResult.ClientError -> {
+ if (parseableItems.size == 1) {
+ dbHelper.incrementRetryCount(parseableItems[0].queueId, "4xx: ${result.code}")
+ BatchSendResult(processed = 0, failed = 1 + corruptedFailed, stop = false)
+ } else {
+ val mid = parseableItems.size / 2
+ val left = sendBatchRecursive(parseableItems.take(mid), endpoint, authHeaders)
+ if (left.stop) return BatchSendResult(left.processed, left.failed + corruptedFailed, stop = true)
+ yield()
+ val right = sendBatchRecursive(parseableItems.drop(mid), endpoint, authHeaders)
+ BatchSendResult(
+ processed = left.processed + right.processed,
+ failed = left.failed + right.failed + corruptedFailed,
+ stop = left.stop || right.stop,
+ )
+ }
+ }
+ is BatchResult.ServerError -> {
+ retryReason(result)?.let { reason ->
+ parseableItems.forEach { dbHelper.incrementRetryCount(it.queueId, reason) }
+ }
+ BatchSendResult(processed = 0, failed = parseableItems.size + corruptedFailed, stop = true)
+ }
+ is BatchResult.RateLimited -> {
+ onRateLimited(result.retryAfterSeconds)
+ BatchSendResult(processed = 0, failed = parseableItems.size + corruptedFailed, stop = true)
+ }
+ BatchResult.NetworkError ->
+ BatchSendResult(processed = 0, failed = parseableItems.size + corruptedFailed, stop = true)
+ }
+ }
+}
diff --git a/apps/mobile/android/app/src/main/java/com/colota/sync/SyncManager.kt b/apps/mobile/android/app/src/main/java/com/colota/sync/SyncManager.kt
index 04692fd8..c5b050e2 100644
--- a/apps/mobile/android/app/src/main/java/com/colota/sync/SyncManager.kt
+++ b/apps/mobile/android/app/src/main/java/com/colota/sync/SyncManager.kt
@@ -34,10 +34,10 @@ class SyncManager(
companion object {
private const val TAG = "SyncManager"
private const val MAX_BATCHES_PER_SYNC = 10
- private const val DEFAULT_RATE_LIMIT_WAIT_SECONDS = 60L
- private const val MAX_RATE_LIMIT_WAIT_SECONDS = 900L
}
+ private val batchSender = OverlandBatchSender(dbHelper, networkManager, ::noteRateLimit)
+
@Volatile private var endpoint: String = ""
@Volatile private var syncIntervalSeconds: Int = 0
@Volatile private var retryIntervalSeconds: Int = 300
@@ -276,16 +276,10 @@ class SyncManager(
return success
}
- /** Clamped to the cap, because the wall clock can be set back. */
- fun rateLimitWaitSeconds(): Long {
- val left = (SyncState.rateLimitedUntil - clock()).coerceAtMost(MAX_RATE_LIMIT_WAIT_SECONDS * 1000)
- return if (left > 0) (left + 999) / 1000 else 0
- }
+ fun rateLimitWaitSeconds(): Long = SyncState.rateLimitWaitSeconds(clock())
- /** Capped so a bad `Retry-After` cannot hold the queue for a day. */
private fun noteRateLimit(retryAfterSeconds: Long?) {
- val wait = (retryAfterSeconds ?: DEFAULT_RATE_LIMIT_WAIT_SECONDS).coerceIn(1, MAX_RATE_LIMIT_WAIT_SECONDS)
- SyncState.holdUntil(clock() + wait * 1000)
+ val wait = SyncState.holdFor(retryAfterSeconds, clock())
AppLogger.w(TAG, "Rate limited: holding uploads for ${wait}s")
}
@@ -344,7 +338,7 @@ class SyncManager(
val processedBefore = totalProcessed
if (currentApiFormat == ApiFormat.OVERLAND_BATCH) {
- val result = sendBatchRecursive(queued, currentEndpoint, currentAuthHeaders)
+ val result = batchSender.sendBatchRecursive(queued, currentEndpoint, currentAuthHeaders)
totalProcessed += result.processed
totalSucceeded += result.processed
totalFailed += result.failed
@@ -405,7 +399,7 @@ class SyncManager(
val limited = results.mapNotNull { it.second as? BatchResult.RateLimited }
if (limited.isNotEmpty()) {
- noteRateLimit(limited.maxOf { it.retryAfterSeconds ?: DEFAULT_RATE_LIMIT_WAIT_SECONDS })
+ noteRateLimit(limited.maxOf { it.retryAfterSeconds ?: SyncState.DEFAULT_RATE_LIMIT_WAIT_SECONDS })
}
stopped = stopReason(results.map { it.second }, anySent = successfulIds.isNotEmpty())
if (stopped != null) break
@@ -438,101 +432,6 @@ class SyncManager(
SyncPass(totalSucceeded, totalFailed, skippedInFlight)
}
- /** Only a verdict about the row moves it back in the queue; an outage or a rate limit is the server's. */
- private fun retryReason(result: BatchResult): String? = when (result) {
- is BatchResult.ClientError -> "4xx: ${result.code}"
- is BatchResult.ServerError -> if (result.code in 502..504) null else "5xx: ${result.code}"
- BatchResult.Success, BatchResult.NetworkError, is BatchResult.RateLimited -> null
- }
-
- /** A 429 ends the pass outright; an outage or a 5xx does once a whole chunk got nothing through. */
- private fun stopReason(results: List<BatchResult?>, anySent: Boolean): String? {
- results.filterIsInstance<BatchResult.RateLimited>().firstOrNull()?.let { limited ->
- return "rate limited" + (limited.retryAfterSeconds?.let { ", retry after ${it}s" } ?: "")
- }
- if (anySent) return null
- results.filterIsInstance<BatchResult.ServerError>().firstOrNull()?.let { return "server error ${it.code}" }
- if (BatchResult.NetworkError in results) return "server unreachable"
- return null
- }
-
- private data class BatchSendResult(val processed: Int, val failed: Int, val stop: Boolean)
-
- /**
- * Endpoint and auth headers are passed by parameter (not read from volatiles)
- * so a settings change mid-pass cannot mix them across recursive calls.
- */
- private suspend fun sendBatchRecursive(
- items: List<com.Colota.data.QueuedLocation>,
- endpoint: String,
- authHeaders: Map<String, String>,
- ): BatchSendResult {
- if (items.isEmpty()) return BatchSendResult(0, 0, false)
-
- // One corrupt payload must not fail the whole batch
- val parsed = items.mapNotNull { item ->
- try {
- item to JSONObject(item.payload)
- } catch (e: Exception) {
- AppLogger.e(TAG, "Corrupt queue row ${item.queueId}, bumping retry counter", e)
- dbHelper.incrementRetryCount(item.queueId, "Corrupt payload")
- null
- }
- }
- val corruptedFailed = items.size - parsed.size
- if (parsed.isEmpty()) {
- return BatchSendResult(processed = 0, failed = corruptedFailed, stop = false)
- }
-
- val parseableItems = parsed.map { it.first }
- val payloads = parsed.map { it.second }
- val customFields = PayloadBuilder.extractEnvelopeCustomFields(payloads.first())
-
- val result = networkManager.sendBatchToEndpoint(
- items = payloads,
- customFields = customFields,
- endpoint = endpoint,
- extraHeaders = authHeaders,
- )
-
- return when (result) {
- BatchResult.Success -> {
- dbHelper.markLocationsSent(parseableItems.map { it.locationId })
- dbHelper.removeBatchFromQueue(parseableItems.map { it.queueId })
- BatchSendResult(processed = parseableItems.size, failed = corruptedFailed, stop = false)
- }
- is BatchResult.ClientError -> {
- if (parseableItems.size == 1) {
- dbHelper.incrementRetryCount(parseableItems[0].queueId, "4xx: ${result.code}")
- BatchSendResult(processed = 0, failed = 1 + corruptedFailed, stop = false)
- } else {
- val mid = parseableItems.size / 2
- val left = sendBatchRecursive(parseableItems.take(mid), endpoint, authHeaders)
- if (left.stop) return BatchSendResult(left.processed, left.failed + corruptedFailed, stop = true)
- yield()
- val right = sendBatchRecursive(parseableItems.drop(mid), endpoint, authHeaders)
- BatchSendResult(
- processed = left.processed + right.processed,
- failed = left.failed + right.failed + corruptedFailed,
- stop = left.stop || right.stop,
- )
- }
- }
- is BatchResult.ServerError -> {
- retryReason(result)?.let { reason ->
- parseableItems.forEach { dbHelper.incrementRetryCount(it.queueId, reason) }
- }
- BatchSendResult(processed = 0, failed = parseableItems.size + corruptedFailed, stop = true)
- }
- is BatchResult.RateLimited -> {
- noteRateLimit(result.retryAfterSeconds)
- BatchSendResult(processed = 0, failed = parseableItems.size + corruptedFailed, stop = true)
- }
- BatchResult.NetworkError ->
- BatchSendResult(processed = 0, failed = parseableItems.size + corruptedFailed, stop = true)
- }
- }
-
private fun calculateNextSyncDelay(resumingWait: Boolean): Long {
if (syncIntervalSeconds <= 0) {
return if (getCachedQueuedCount() > 0) {
diff --git a/apps/mobile/android/app/src/main/java/com/colota/sync/SyncState.kt b/apps/mobile/android/app/src/main/java/com/colota/sync/SyncState.kt
index e74043a6..fd35f13a 100644
--- a/apps/mobile/android/app/src/main/java/com/colota/sync/SyncState.kt
+++ b/apps/mobile/android/app/src/main/java/com/colota/sync/SyncState.kt
@@ -12,6 +12,9 @@ package com.Colota.sync
* too, because a Sync now while tracking is off runs on a service that stops after the pass.
*/
object SyncState {
+ const val DEFAULT_RATE_LIMIT_WAIT_SECONDS = 60L
+ const val MAX_RATE_LIMIT_WAIT_SECONDS = 900L
+
@Volatile var lastSuccessTime: Long = 0L
private set
@@ -21,10 +24,19 @@ object SyncState {
@Volatile var rateLimitedUntil: Long = 0L
private set
- /** A later, shorter Retry-After must not cut a longer wait. */
+ /** Capped so a bad `Retry-After` cannot hold the queue for a day; a later, shorter one must not cut a longer wait. */
@Synchronized
- fun holdUntil(untilMs: Long) {
+ fun holdFor(retryAfterSeconds: Long?, nowMs: Long): Long {
+ val wait = (retryAfterSeconds ?: DEFAULT_RATE_LIMIT_WAIT_SECONDS).coerceIn(1, MAX_RATE_LIMIT_WAIT_SECONDS)
+ val untilMs = nowMs + wait * 1000
if (untilMs > rateLimitedUntil) rateLimitedUntil = untilMs
+ return wait
+ }
+
+ /** Clamped to the cap, because the wall clock can be set back. */
+ fun rateLimitWaitSeconds(nowMs: Long): Long {
+ val left = (rateLimitedUntil - nowMs).coerceAtMost(MAX_RATE_LIMIT_WAIT_SECONDS * 1000)
+ return if (left > 0) (left + 999) / 1000 else 0
}
fun recordSuccess(timeMs: Long) {
diff --git a/apps/mobile/android/app/src/main/java/com/colota/sync/SyncVerdicts.kt b/apps/mobile/android/app/src/main/java/com/colota/sync/SyncVerdicts.kt
new file mode 100644
index 00000000..fcddbcef
--- /dev/null
+++ b/apps/mobile/android/app/src/main/java/com/colota/sync/SyncVerdicts.kt
@@ -0,0 +1,26 @@
+/**
+ * Copyright (C) 2026 Max Dietrich
+ * Licensed under the GNU AGPLv3. See LICENSE in the project root for details.
+ */
+
+package com.Colota.sync
+
+import com.Colota.sync.http.BatchResult
+
+/** Only a verdict about the row moves it back in the queue; an outage or a rate limit is the server's. */
+internal fun retryReason(result: BatchResult): String? = when (result) {
+ is BatchResult.ClientError -> "4xx: ${result.code}"
+ is BatchResult.ServerError -> if (result.code in 502..504) null else "5xx: ${result.code}"
+ BatchResult.Success, BatchResult.NetworkError, is BatchResult.RateLimited -> null
+}
+
+/** A 429 ends the pass outright; an outage or a 5xx does once a whole chunk got nothing through. */
+internal fun stopReason(results: List<BatchResult?>, anySent: Boolean): String? {
+ results.filterIsInstance<BatchResult.RateLimited>().firstOrNull()?.let { limited ->
+ return "rate limited" + (limited.retryAfterSeconds?.let { ", retry after ${it}s" } ?: "")
+ }
+ if (anySent) return null
+ results.filterIsInstance<BatchResult.ServerError>().firstOrNull()?.let { return "server error ${it.code}" }
+ if (BatchResult.NetworkError in results) return "server unreachable"
+ return null
+}
diff --git a/apps/mobile/android/app/src/test/java/com/Colota/sync/OverlandBatchSenderTest.kt b/apps/mobile/android/app/src/test/java/com/Colota/sync/OverlandBatchSenderTest.kt
new file mode 100644
index 00000000..a327e80c
--- /dev/null
+++ b/apps/mobile/android/app/src/test/java/com/Colota/sync/OverlandBatchSenderTest.kt
@@ -0,0 +1,171 @@
+package com.Colota.sync
+
+import com.Colota.data.DatabaseHelper
+import com.Colota.data.QueuedLocation
+import com.Colota.sync.http.BatchResult
+import com.Colota.sync.http.NetworkManager
+import com.Colota.util.AppLogger
+import io.mockk.*
+import kotlinx.coroutines.test.runTest
+import org.json.JSONObject
+import org.junit.After
+import org.junit.Assert.*
+import org.junit.Before
+import org.junit.Test
+
+class OverlandBatchSenderTest {
+
+ private lateinit var dbHelper: DatabaseHelper
+ private lateinit var networkManager: NetworkManager
+ private val rateLimits = mutableListOf<Long?>()
+ private lateinit var sender: OverlandBatchSender
+
+ private val endpoint = "https://dawarich.example/api/v1/overland/batches"
+
+ @Before
+ fun setUp() {
+ dbHelper = mockk(relaxed = true)
+ networkManager = mockk(relaxed = true)
+ rateLimits.clear()
+ sender = OverlandBatchSender(dbHelper, networkManager) { rateLimits.add(it) }
+
+ mockkObject(AppLogger)
+ every { AppLogger.e(any(), any(), any()) } just Runs
+ }
+
+ @After
+ fun tearDown() {
+ unmockkObject(AppLogger)
+ }
+
+ private fun rows(count: Long) = (1L..count).map {
+ QueuedLocation(it, it + 100, """{"lat":52.0,"lon":13.0,"tst":1700000000,"id":$it}""", 0)
+ }
+
+ @Test
+ fun `a batch the server accepts is one POST and leaves the queue`() = runTest {
+ val items = rows(50)
+ coEvery { networkManager.sendBatchToEndpoint(any(), any(), any(), any()) } returns BatchResult.Success
+
+ val result = sender.sendBatchRecursive(items, endpoint, emptyMap())
+
+ coVerify(exactly = 1) { networkManager.sendBatchToEndpoint(any(), any(), any(), any()) }
+ verify { dbHelper.markLocationsSent(items.map { it.locationId }) }
+ verify { dbHelper.removeBatchFromQueue(items.map { it.queueId }) }
+ assertEquals(OverlandBatchSender.BatchSendResult(processed = 50, failed = 0, stop = false), result)
+ }
+
+ /** Splitting on a 5xx would turn one outage into N more requests per cycle. */
+ @Test
+ fun `a 5xx ends the pass without splitting and bumps each row once`() = runTest {
+ val items = rows(4)
+ coEvery { networkManager.sendBatchToEndpoint(any(), any(), any(), any()) } returns BatchResult.ServerError(500)
+
+ val result = sender.sendBatchRecursive(items, endpoint, emptyMap())
+
+ coVerify(exactly = 1) { networkManager.sendBatchToEndpoint(any(), any(), any(), any()) }
+ items.forEach { verify(exactly = 1) { dbHelper.incrementRetryCount(it.queueId, "5xx: 500") } }
+ assertTrue(result.stop)
+ }
+
+ /** An outage says nothing about the rows, so they keep their place in the queue. */
+ @Test
+ fun `a network error ends the pass without splitting or bumping`() = runTest {
+ coEvery { networkManager.sendBatchToEndpoint(any(), any(), any(), any()) } returns BatchResult.NetworkError
+
+ val result = sender.sendBatchRecursive(rows(3), endpoint, emptyMap())
+
+ coVerify(exactly = 1) { networkManager.sendBatchToEndpoint(any(), any(), any(), any()) }
+ verify(exactly = 0) { dbHelper.incrementRetryCount(any(), any()) }
+ assertTrue(result.stop)
+ }
+
+ /** One row the server refuses must not hold back the rows batched with it. */
+ @Test
+ fun `a 4xx bisects down to the poison row and commits the rest`() = runTest {
+ coEvery { networkManager.sendBatchToEndpoint(any(), any(), any(), any()) } answers {
+ val containsPoison = arg<List<JSONObject>>(0).any { it.optInt("id", -1) == 4 }
+ if (containsPoison) BatchResult.ClientError(400) else BatchResult.Success
+ }
+ val sentLocationIds = mutableSetOf<Long>()
+ val removedQueueIds = mutableSetOf<Long>()
+ every { dbHelper.markLocationsSent(any()) } answers { sentLocationIds.addAll(firstArg<List<Long>>()) }
+ every { dbHelper.removeBatchFromQueue(any()) } answers { removedQueueIds.addAll(firstArg<List<Long>>()) }
+
+ val result = sender.sendBatchRecursive(rows(4), endpoint, emptyMap())
+
+ verify { dbHelper.incrementRetryCount(4L, "4xx: 400") }
+ assertEquals(setOf(101L, 102L, 103L), sentLocationIds)
+ assertEquals(setOf(1L, 2L, 3L), removedQueueIds)
+ assertEquals(OverlandBatchSender.BatchSendResult(processed = 3, failed = 1, stop = false), result)
+ }
+
+ /** Overland carries custom fields once on the envelope, never as location keys. */
+ @Test
+ fun `custom fields lift to the envelope and location keys stay out of it`() = runTest {
+ val item = QueuedLocation(1L, 100L, """{"device_id":"my-pixel","lat":52.0,"lon":13.0,"tst":1700000000}""", 0)
+ var capturedFields: Map<String, String>? = null
+ coEvery { networkManager.sendBatchToEndpoint(any(), any(), any(), any()) } answers {
+ capturedFields = arg<Map<String, String>>(1)
+ BatchResult.Success
+ }
+
+ sender.sendBatchRecursive(listOf(item), endpoint, emptyMap())
+
+ val fields = checkNotNull(capturedFields)
+ assertEquals("my-pixel", fields["device_id"])
+ assertFalse("lat is a canonical key, not a custom field", fields.containsKey("lat"))
+ assertFalse("tst is a canonical key, not a custom field", fields.containsKey("tst"))
+ }
+
+ /** A row that cannot be parsed would otherwise abort every pass and block the queue behind it. */
+ @Test
+ fun `a corrupt row is bumped and the valid rows still go out`() = runTest {
+ val corrupted = QueuedLocation(1L, 100L, "NOT_VALID_JSON{{{", 0)
+ val valid1 = QueuedLocation(2L, 101L, """{"lat":52.0,"lon":13.0,"tst":1700000000}""", 0)
+ val valid2 = QueuedLocation(3L, 102L, """{"lat":52.1,"lon":13.1,"tst":1700000001}""", 0)
+ var capturedItemCount = 0
+ coEvery { networkManager.sendBatchToEndpoint(any(), any(), any(), any()) } answers {
+ capturedItemCount = arg<List<JSONObject>>(0).size
+ BatchResult.Success
+ }
+
+ val result = sender.sendBatchRecursive(listOf(corrupted, valid1, valid2), endpoint, emptyMap())
+
+ verify { dbHelper.incrementRetryCount(1L, "Corrupt payload") }
+ assertEquals(2, capturedItemCount)
+ verify { dbHelper.markLocationsSent(listOf(101L, 102L)) }
+ verify { dbHelper.removeBatchFromQueue(listOf(2L, 3L)) }
+ verify(exactly = 0) { dbHelper.removeBatchFromQueue(match { it.contains(1L) }) }
+ assertEquals(OverlandBatchSender.BatchSendResult(processed = 2, failed = 1, stop = false), result)
+ }
+
+ /** Splitting a rate-limited batch would turn one refused request into many more. */
+ @Test
+ fun `a 429 ends the pass without splitting and hands its Retry-After to the caller`() = runTest {
+ coEvery { networkManager.sendBatchToEndpoint(any(), any(), any(), any()) } returns BatchResult.RateLimited(120)
+
+ val result = sender.sendBatchRecursive(rows(3), endpoint, emptyMap())
+
+ coVerify(exactly = 1) { networkManager.sendBatchToEndpoint(any(), any(), any(), any()) }
+ verify(exactly = 0) { dbHelper.incrementRetryCount(any(), any()) }
+ assertEquals(listOf<Long?>(120L), rateLimits)
+ assertTrue(result.stop)
+ }
+
+ /** A rate limit met while bisecting ends the pass, so the other half is not sent either. */
+ @Test
+ fun `a rate-limited half stops the bisect before the other half`() = runTest {
+ coEvery { networkManager.sendBatchToEndpoint(any(), any(), any(), any()) } returnsMany listOf(
+ BatchResult.ClientError(400),
+ BatchResult.RateLimited(null),
+ BatchResult.Success
+ )
+
+ val result = sender.sendBatchRecursive(rows(4), endpoint, emptyMap())
+
+ coVerify(exactly = 2) { networkManager.sendBatchToEndpoint(any(), any(), any(), any()) }
+ assertEquals(listOf<Long?>(null), rateLimits)
+ assertTrue(result.stop)
+ }
+}
diff --git a/apps/mobile/android/app/src/test/java/com/Colota/sync/SyncManagerTest.kt b/apps/mobile/android/app/src/test/java/com/Colota/sync/SyncManagerTest.kt
index 5c545fac..e1bf96c7 100644
--- a/apps/mobile/android/app/src/test/java/com/Colota/sync/SyncManagerTest.kt
+++ b/apps/mobile/android/app/src/test/java/com/Colota/sync/SyncManagerTest.kt
@@ -322,7 +322,6 @@ class SyncManagerTest {
@Test
fun `queueAndSend bypassInterval sends despite a periodic interval`() = scope.runTest {
- // Before #579 the heartbeat POSTed directly, ignoring the interval. This restores that
syncManager.updateConfig(
endpoint = "https://example.com",
syncIntervalSeconds = 900,
@@ -368,7 +367,6 @@ class SyncManagerTest {
@Test
fun `queueAndSend bypassInterval leaves a failed send in the queue`() = scope.runTest {
- // An unreachable server must not lose the point (#579)
every { dbHelper.addToQueue(any(), any()) } returns 42L
syncManager.updateConfig(
endpoint = "https://example.com",
@@ -517,13 +515,12 @@ class SyncManagerTest {
)
val item = QueuedLocation(1L, 100L, """{"lat":52.0}""", 0)
- // Always return the same item (it stays in queue after failure)
+ // A failed row stays at the head of the queue
every { dbHelper.getQueuedLocations(50) } returns listOf(item)
coEvery { networkManager.sendToEndpoint(any(), any(), any(), any()) } returns BatchResult.ClientError(400)
syncManager.manualFlush()
- // Should only fetch ONE batch, not loop 10 times re-fetching the same failing item
verify(exactly = 1) { dbHelper.getQueuedLocations(50) }
coVerify(exactly = 1) { networkManager.sendToEndpoint(any(), any(), any(), any()) }
}
@@ -549,9 +546,7 @@ class SyncManagerTest {
assertEquals(20, second)
}
- // ========================================================================
- // Exponential backoff (30s → 60s → 5min → 15min)
- // ========================================================================
+ // --- Backoff ---
@Test
fun `applyBackoffDelay waits 30s after 1 failure`() = scope.runTest {
@@ -585,9 +580,7 @@ class SyncManagerTest {
assertEquals(900_000L, currentTime - start)
}
- // ========================================================================
- // Batch processing limits and concurrent chunk sending
- // ========================================================================
+ // --- Batch limit and chunks ---
@Test
fun `syncQueue stops processing after 10 batches`() = scope.runTest {
@@ -601,7 +594,7 @@ class SyncManagerTest {
authHeaders = emptyMap()
)
- // Always return items - loop should still stop at batch 10
+ // The queue never empties, so only the batch cap ends the pass
val item = QueuedLocation(1L, 100L, """{"lat":52.0}""", 0)
every { dbHelper.getQueuedLocations(50) } returns listOf(item)
coEvery { networkManager.sendToEndpoint(any(), any(), any(), any()) } returns BatchResult.Success
@@ -623,7 +616,7 @@ class SyncManagerTest {
authHeaders = emptyMap()
)
- // 25 items → 3 chunks (10 + 10 + 5)
+ // Chunks of 10, 10 and 5
val items = (1L..25L).map { QueuedLocation(it, it + 100, """{"lat":52.0}""", 0) }
every { dbHelper.getQueuedLocations(50) } returnsMany listOf(items, emptyList())
coEvery { networkManager.sendToEndpoint(any(), any(), any(), any()) } returns BatchResult.Success
@@ -631,13 +624,10 @@ class SyncManagerTest {
syncManager.manualFlush()
coVerify(exactly = 25) { networkManager.sendToEndpoint(any(), any(), any(), any()) }
- // 3 chunks → 3 removeBatchFromQueue calls
verify(exactly = 3) { dbHelper.removeBatchFromQueue(any()) }
}
- // ========================================================================
- // Consecutive failure tracking (3+ failures → error event)
- // ========================================================================
+ // --- Consecutive failures ---
@Test
fun `periodic sync increments consecutiveFailures on each failure`() = scope.runTest {
@@ -653,16 +643,12 @@ class SyncManagerTest {
coEvery { networkMonitor.isNetworkAvailable() } returns true
every { dbHelper.getQueuedCount() } returns 5
- // getQueuedLocations returns empty → performSyncAndCheckSuccess sees
- // countBefore=5, countAfter=5 → failure
+ // Rows are queued but none can be fetched, so the count never drops and every pass fails
every { dbHelper.getQueuedLocations(50) } returns emptyList()
syncManager.startPeriodicSync()
- // Advance past 2 full iterations:
- // iter 1: delay(1s) + sync + backoff(30s) = 31s
- // iter 2: delay(1s) + sync + backoff(60s) = 61s
- // Total to complete 2 iterations: ~92s
+ // Two 1 s ticks, each followed by its backoff of 30 s and 60 s
advanceTimeBy(92_500)
assertEquals(2, getField("consecutiveFailures"))
@@ -690,11 +676,7 @@ class SyncManagerTest {
syncManager.startPeriodicSync()
- // Advance past 3 full iterations:
- // iter 1: delay(1s) + sync + backoff(30s) = 31s
- // iter 2: delay(1s) + sync + backoff(60s) = 61s
- // iter 3: delay(1s) + sync (error event fires here) + backoff(300s)
- // Need to reach iter 3's sync: 31 + 61 + 1 = 93s
+ // The third tick lands at 31 + 61 + 1 s and its failure sends the error event
advanceTimeBy(93_500)
verify(atLeast = 1) {
@@ -725,7 +707,7 @@ class SyncManagerTest {
syncManager.startPeriodicSync()
- // Only advance through 2 iterations (before 3rd sync)
+ // Two ticks; the error event needs a third
advanceTimeBy(92_500)
verify(exactly = 0) { LocationServiceModule.sendSyncErrorEvent(any(), any()) }
@@ -765,9 +747,7 @@ class SyncManagerTest {
syncManager.stopPeriodicSync()
}
- // ========================================================================
- // Periodic sync mode (job scheduling, lifecycle)
- // ========================================================================
+ // --- Periodic sync ---
@Test
fun `startPeriodicSync triggers sync at configured interval`() = scope.runTest {
@@ -793,11 +773,9 @@ class SyncManagerTest {
syncManager.startPeriodicSync()
- // Before interval elapses - no sync yet
advanceTimeBy(30_000)
coVerify(exactly = 0) { networkManager.sendToEndpoint(any(), any(), any(), any()) }
- // After interval elapses - sync happens
advanceTimeBy(31_000)
coVerify(atLeast = 1) { networkManager.sendToEndpoint(any(), any(), any(), any()) }
syncManager.stopPeriodicSync()
@@ -933,13 +911,10 @@ class SyncManagerTest {
advanceTimeBy(10_000)
- // No sync should have been attempted after cancellation
verify(exactly = 0) { dbHelper.getQueuedLocations(any()) }
}
- // ========================================================================
- // Interval change mid-wait (a profile switch swaps the sync interval)
- // ========================================================================
+ // --- A profile switch changes the interval mid-wait ---
private var ticks = 0
@@ -1025,9 +1000,7 @@ class SyncManagerTest {
manager.stopPeriodicSync()
}
- // ========================================================================
- // Async exception isolation (fix: one bad item must not kill the chunk)
- // ========================================================================
+ // --- One corrupt row in a chunk ---
@Test
fun `syncQueue isolates exception in single item without cancelling chunk`() = scope.runTest {
@@ -1044,7 +1017,6 @@ class SyncManagerTest {
authHeaders = emptyMap()
)
- // Item 1 has corrupted payload that will throw JSONException
val corrupted = QueuedLocation(1L, 100L, "NOT_VALID_JSON", 0)
val valid = QueuedLocation(2L, 101L, """{"lat":52.0}""", 0)
every { dbHelper.getQueuedLocations(50) } returnsMany listOf(
@@ -1055,18 +1027,14 @@ class SyncManagerTest {
syncManager.manualFlush()
- // Valid item should still be sent despite corrupted sibling
coVerify(atLeast = 1) { networkManager.sendToEndpoint(any(), any(), any(), any(), any()) }
- // Corrupted item gets retry increment, valid item gets removed
verify { dbHelper.incrementRetryCount(1L, "Corrupt payload") }
verify { dbHelper.markLocationsSent(listOf(101L)) }
unmockkObject(LocationServiceModule.Companion)
}
- // ========================================================================
- // apiFormat passthrough
- // ========================================================================
+ // --- apiFormat passthrough ---
@Test
fun `queueAndSend passes apiFormat to sendToEndpoint`() = scope.runTest {
@@ -1113,139 +1081,7 @@ class SyncManagerTest {
coVerify { networkManager.sendToEndpoint(any(), any(), any(), "POST", ApiFormat.TRACCAR_JSON) }
}
- // ========================================================================
- // OVERLAND_BATCH path
- // ========================================================================
-
- @Test
- fun `batch path sends one POST and removes all rows on success`() = scope.runTest {
- syncManager.updateConfig(
- endpoint = "https://dawarich.example/api/v1/overland/batches",
- syncIntervalSeconds = 300,
- retryIntervalSeconds = 30,
- isOfflineMode = false,
- syncCondition = "any",
- syncSsid = "",
- authHeaders = emptyMap(),
- apiFormat = ApiFormat.OVERLAND_BATCH,
- overlandBatchSize = 50
- )
-
- val items = (1L..50L).map {
- QueuedLocation(it, it + 100, """{"lat":52.0,"lon":13.0,"tst":1700000000}""", 0)
- }
- every { dbHelper.getQueuedLocations(50) } returnsMany listOf(items, emptyList())
- coEvery { networkManager.sendBatchToEndpoint(any(), any(), any(), any()) } returns BatchResult.Success
-
- syncManager.manualFlush()
-
- coVerify(exactly = 1) { networkManager.sendBatchToEndpoint(any(), any(), any(), any()) }
- verify { dbHelper.markLocationsSent(items.map { it.locationId }) }
- verify { dbHelper.removeBatchFromQueue(items.map { it.queueId }) }
- }
-
- @Test
- fun `batch path bails on 5xx without splitting and bumps retry counts once each`() = scope.runTest {
- syncManager.updateConfig(
- endpoint = "https://dawarich.example/api/v1/overland/batches",
- syncIntervalSeconds = 300,
- retryIntervalSeconds = 30,
- isOfflineMode = false,
- syncCondition = "any",
- syncSsid = "",
- authHeaders = emptyMap(),
- apiFormat = ApiFormat.OVERLAND_BATCH,
- overlandBatchSize = 50
- )
-
- val items = (1L..4L).map {
- QueuedLocation(it, it + 100, """{"lat":52.0,"lon":13.0,"tst":1700000000}""", 0)
- }
- every { dbHelper.getQueuedLocations(50) } returns items
- coEvery { networkManager.sendBatchToEndpoint(any(), any(), any(), any()) } returns BatchResult.ServerError(500)
-
- syncManager.manualFlush()
-
- // Critical: exactly ONE HTTP call. NOT recursive splits, that would amplify the outage.
- coVerify(exactly = 1) { networkManager.sendBatchToEndpoint(any(), any(), any(), any()) }
- // Each row gets one retry bump
- items.forEach { verify { dbHelper.incrementRetryCount(it.queueId, "5xx: 500") } }
- }
-
- @Test
- fun `batch path bails on network error without splitting`() = scope.runTest {
- syncManager.updateConfig(
- endpoint = "https://dawarich.example/api/v1/overland/batches",
- syncIntervalSeconds = 300,
- retryIntervalSeconds = 30,
- isOfflineMode = false,
- syncCondition = "any",
- syncSsid = "",
- authHeaders = emptyMap(),
- apiFormat = ApiFormat.OVERLAND_BATCH,
- overlandBatchSize = 50
- )
-
- val items = (1L..3L).map {
- QueuedLocation(it, it + 100, """{"lat":52.0,"lon":13.0,"tst":1700000000}""", 0)
- }
- every { dbHelper.getQueuedLocations(50) } returns items
- coEvery { networkManager.sendBatchToEndpoint(any(), any(), any(), any()) } returns BatchResult.NetworkError
-
- syncManager.manualFlush()
-
- coVerify(exactly = 1) { networkManager.sendBatchToEndpoint(any(), any(), any(), any()) }
- verify(exactly = 0) { dbHelper.incrementRetryCount(any(), any()) }
- }
-
- @Test
- fun `batch path splits on 4xx to isolate poison row`() = scope.runTest {
- syncManager.updateConfig(
- endpoint = "https://dawarich.example/api/v1/overland/batches",
- syncIntervalSeconds = 300,
- retryIntervalSeconds = 30,
- isOfflineMode = false,
- syncCondition = "any",
- syncSsid = "",
- authHeaders = emptyMap(),
- apiFormat = ApiFormat.OVERLAND_BATCH,
- overlandBatchSize = 50
- )
-
- // 4 items: items 1, 2, 3 are good. Item 4 (queueId=4) is the poison row.
- val items = (1L..4L).map {
- QueuedLocation(it, it + 100, """{"lat":52.0,"lon":13.0,"tst":1700000000,"id":$it}""", 0)
- }
- every { dbHelper.getQueuedLocations(50) } returnsMany listOf(items, emptyList())
-
- // Server rejects any batch that contains the poison item (queueId 4)
- coEvery { networkManager.sendBatchToEndpoint(any(), any(), any(), any()) } answers {
- val sent = arg<List<JSONObject>>(0)
- val containsPoison = sent.any { it.optInt("id", -1) == 4 }
- if (containsPoison) BatchResult.ClientError(400) else BatchResult.Success
- }
-
- // Capture every commit call so we can assert across all split-recovery calls
- val sentLocationIds = mutableSetOf<Long>()
- val removedQueueIds = mutableSetOf<Long>()
- every { dbHelper.markLocationsSent(any()) } answers {
- sentLocationIds.addAll(firstArg<List<Long>>())
- }
- every { dbHelper.removeBatchFromQueue(any()) } answers {
- removedQueueIds.addAll(firstArg<List<Long>>())
- }
-
- syncManager.manualFlush()
-
- // Poison row gets isolated to a 1-item batch that fails alone -> retry counter bumped
- verify { dbHelper.incrementRetryCount(4L, "4xx: 400") }
- // Good rows committed across one or more split-recovery calls
- assertEquals(setOf(101L, 102L, 103L), sentLocationIds)
- assertEquals(setOf(1L, 2L, 3L), removedQueueIds)
- // Poison row was NOT committed
- assertFalse(sentLocationIds.contains(104L))
- assertFalse(removedQueueIds.contains(4L))
- }
+ // --- Overland batch path ---
@Test
fun `batch path uses overlandBatchSize for the fetch limit`() = scope.runTest {
@@ -1265,97 +1101,10 @@ class SyncManagerTest {
syncManager.manualFlush()
- // Plan called this out: overlandBatchSize must thread into the fetch limit, not just the wire bundle.
verify { dbHelper.getQueuedLocations(200) }
}
- @Test
- fun `batch path lifts custom fields to envelope and sends device_id`() = scope.runTest {
- syncManager.updateConfig(
- endpoint = "https://dawarich.example/api/v1/overland/batches",
- syncIntervalSeconds = 300,
- retryIntervalSeconds = 30,
- isOfflineMode = false,
- syncCondition = "any",
- syncSsid = "",
- authHeaders = emptyMap(),
- apiFormat = ApiFormat.OVERLAND_BATCH,
- overlandBatchSize = 50
- )
-
- // Payload has a custom field (device_id) baked in by PayloadBuilder
- val item = QueuedLocation(
- 1L, 100L,
- """{"device_id":"my-pixel","lat":52.0,"lon":13.0,"tst":1700000000}""",
- 0
- )
- every { dbHelper.getQueuedLocations(50) } returnsMany listOf(listOf(item), emptyList())
-
- var capturedFields: Map<String, String>? = null
- coEvery { networkManager.sendBatchToEndpoint(any(), any(), any(), any()) } answers {
- @Suppress("UNCHECKED_CAST")
- capturedFields = arg<Map<String, String>>(1)
- BatchResult.Success
- }
-
- syncManager.manualFlush()
-
- // Custom fields must be extracted from the per-row payload and passed at envelope level.
- // Canonical location keys must NOT be passed as custom fields.
- val fields = checkNotNull(capturedFields)
- assertEquals("my-pixel", fields["device_id"])
- assertFalse("lat is a canonical key, not a custom field", fields.containsKey("lat"))
- assertFalse("tst is a canonical key, not a custom field", fields.containsKey("tst"))
- }
-
- @Test
- fun `batch path isolates corrupt payload without poisoning the bundle`() = scope.runTest {
- syncManager.updateConfig(
- endpoint = "https://dawarich.example/api/v1/overland/batches",
- syncIntervalSeconds = 300,
- retryIntervalSeconds = 30,
- isOfflineMode = false,
- syncCondition = "any",
- syncSsid = "",
- authHeaders = emptyMap(),
- apiFormat = ApiFormat.OVERLAND_BATCH,
- overlandBatchSize = 50
- )
-
- // Item 1 has a corrupted payload (not valid JSON). Items 2 and 3 are valid.
- // Without isolation, JSONObject(item.payload) would throw and abort the whole pass,
- // leaving the corrupt row to re-throw forever next cycle.
- val corrupted = QueuedLocation(1L, 100L, "NOT_VALID_JSON{{{", 0)
- val valid1 = QueuedLocation(2L, 101L, """{"lat":52.0,"lon":13.0,"tst":1700000000}""", 0)
- val valid2 = QueuedLocation(3L, 102L, """{"lat":52.1,"lon":13.1,"tst":1700000001}""", 0)
- every { dbHelper.getQueuedLocations(50) } returnsMany listOf(
- listOf(corrupted, valid1, valid2),
- emptyList()
- )
-
- var capturedItemCount = 0
- coEvery { networkManager.sendBatchToEndpoint(any(), any(), any(), any()) } answers {
- @Suppress("UNCHECKED_CAST")
- capturedItemCount = arg<List<JSONObject>>(0).size
- BatchResult.Success
- }
-
- syncManager.manualFlush()
-
- // Corrupt row gets its retry counter bumped
- verify { dbHelper.incrementRetryCount(1L, "Corrupt payload") }
- // Only the 2 valid rows are bundled into the wire request
- assertEquals(2, capturedItemCount)
- // Valid rows are committed
- verify { dbHelper.markLocationsSent(listOf(101L, 102L)) }
- verify { dbHelper.removeBatchFromQueue(listOf(2L, 3L)) }
- // Corrupt row is NOT removed from the queue (stays for next cycle, just deprioritized)
- verify(exactly = 0) { dbHelper.removeBatchFromQueue(match { it.contains(1L) }) }
- }
-
- // ========================================================================
- // One sender at a time
- // ========================================================================
+ // --- One sender at a time ---
@Test
fun `a manual flush during another waits instead of posting the same rows again`() = scope.runTest {
@@ -1618,9 +1367,7 @@ class SyncManagerTest {
coVerify(exactly = 1) { networkManager.sendToEndpoint(any(), any(), any(), any(), any()) }
}
- // ========================================================================
- // Helpers
- // ========================================================================
+ // --- Helpers ---
@Suppress("UNCHECKED_CAST")
private suspend fun callApplyBackoffDelay() {
@@ -1776,61 +1523,6 @@ class SyncManagerTest {
verify(exactly = 0) { dbHelper.removeFromQueueByLocationId(any()) }
}
- /** Splitting a rate-limited batch would turn one refused request into many more. */
- @Test
- fun `batch path stops on a rate limit without splitting`() = scope.runTest {
- syncManager.updateConfig(
- endpoint = "https://dawarich.example/api/v1/overland/batches",
- syncIntervalSeconds = 300,
- retryIntervalSeconds = 30,
- isOfflineMode = false,
- syncCondition = "any",
- syncSsid = "",
- authHeaders = emptyMap(),
- apiFormat = ApiFormat.OVERLAND_BATCH,
- overlandBatchSize = 50
- )
- val items = (1L..3L).map {
- QueuedLocation(it, it + 100, """{"lat":52.0,"lon":13.0,"tst":1700000000}""", 0)
- }
- every { dbHelper.getQueuedLocations(50) } returns items
- coEvery { networkManager.sendBatchToEndpoint(any(), any(), any(), any()) } returns BatchResult.RateLimited(null)
-
- syncManager.manualFlush()
-
- coVerify(exactly = 1) { networkManager.sendBatchToEndpoint(any(), any(), any(), any()) }
- verify(exactly = 0) { dbHelper.incrementRetryCount(any(), any()) }
- }
-
- /** A rate limit met while bisecting ends the pass, so the other half is not sent either. */
- @Test
- fun `batch path stops bisecting when a half is rate limited`() = scope.runTest {
- syncManager.updateConfig(
- endpoint = "https://dawarich.example/api/v1/overland/batches",
- syncIntervalSeconds = 300,
- retryIntervalSeconds = 30,
- isOfflineMode = false,
- syncCondition = "any",
- syncSsid = "",
- authHeaders = emptyMap(),
- apiFormat = ApiFormat.OVERLAND_BATCH,
- overlandBatchSize = 50
- )
- val items = (1L..4L).map {
- QueuedLocation(it, it + 100, """{"lat":52.0,"lon":13.0,"tst":1700000000}""", 0)
- }
- every { dbHelper.getQueuedLocations(50) } returns items
- coEvery { networkManager.sendBatchToEndpoint(any(), any(), any(), any()) } returnsMany listOf(
- BatchResult.ClientError(400),
- BatchResult.RateLimited(null),
- BatchResult.Success
- )
-
- syncManager.manualFlush()
-
- coVerify(exactly = 2) { networkManager.sendBatchToEndpoint(any(), any(), any(), any()) }
- }
-
// --- The wait a 429 asks for ---
private var now = 0L
@@ -1880,17 +1572,6 @@ class SyncManagerTest {
assertEquals(60L, manager.rateLimitWaitSeconds())
}
- @Test
- fun `a Retry-After longer than 15 min is capped`() = scope.runTest {
- val manager = rateLimitedManager()
- fakeQueue(1)
- coEvery { networkManager.sendToEndpoint(any(), any(), any(), any(), any()) } returns BatchResult.RateLimited(86_400)
-
- manager.manualFlush()
-
- assertEquals(900L, manager.rateLimitWaitSeconds())
- }
-
@Test
fun `a flush that ends rate limited tells the screen how long to wait`() = scope.runTest {
mockkObject(LocationServiceModule)
@@ -1939,18 +1620,6 @@ class SyncManagerTest {
coVerify(exactly = 0) { networkManager.sendToEndpoint(any(), any(), any(), any(), any()) }
}
- @Test
- fun `a clock set back cannot hold uploads longer than the cap`() = scope.runTest {
- val manager = rateLimitedManager()
- fakeQueue(1)
- coEvery { networkManager.sendToEndpoint(any(), any(), any(), any(), any()) } returns BatchResult.RateLimited(60)
- manager.manualFlush()
-
- now -= 2 * 60 * 60 * 1000L
-
- assertEquals(900L, manager.rateLimitWaitSeconds())
- }
-
@Test
fun `the longest Retry-After in a chunk sets the wait`() = scope.runTest {
val manager = rateLimitedManager()
diff --git a/apps/mobile/android/app/src/test/java/com/Colota/sync/SyncStateTest.kt b/apps/mobile/android/app/src/test/java/com/Colota/sync/SyncStateTest.kt
index 4d577457..2e808a56 100644
--- a/apps/mobile/android/app/src/test/java/com/Colota/sync/SyncStateTest.kt
+++ b/apps/mobile/android/app/src/test/java/com/Colota/sync/SyncStateTest.kt
@@ -29,9 +29,38 @@ class SyncStateTest {
/** Two senders can meet a 429 at once. */
@Test
fun `a shorter rate-limit wait never replaces a longer one`() {
- SyncState.holdUntil(600_000L)
- SyncState.holdUntil(60_000L)
+ SyncState.holdFor(600, nowMs = 0L)
+ SyncState.holdFor(60, nowMs = 0L)
- assertEquals(600_000L, SyncState.rateLimitedUntil)
+ assertEquals(600L, SyncState.rateLimitWaitSeconds(0L))
+ }
+
+ @Test
+ fun `a 429 without Retry-After holds uploads for 60 s`() {
+ assertEquals(60L, SyncState.holdFor(null, nowMs = 0L))
+ assertEquals(60L, SyncState.rateLimitWaitSeconds(0L))
+ }
+
+ /** A bad Retry-After must not hold the queue for a day. */
+ @Test
+ fun `a Retry-After longer than 15 min is capped`() {
+ assertEquals(900L, SyncState.holdFor(86_400, nowMs = 0L))
+ assertEquals(900L, SyncState.rateLimitWaitSeconds(0L))
+ }
+
+ @Test
+ fun `a clock set back cannot hold uploads longer than the cap`() {
+ SyncState.holdFor(60, nowMs = 0L)
+
+ assertEquals(900L, SyncState.rateLimitWaitSeconds(-2 * 60 * 60 * 1000L))
+ }
+
+ /** The screen shows the wait in whole seconds, so the last part-second still reads as one. */
+ @Test
+ fun `the wait rounds up and ends when its time has passed`() {
+ SyncState.holdFor(30, nowMs = 0L)
+
+ assertEquals(1L, SyncState.rateLimitWaitSeconds(29_001L))
+ assertEquals(0L, SyncState.rateLimitWaitSeconds(30_000L))
}
}