oficina / docs Public

Docs: the save queue's real-timer tests wait for what they check, not a fixed sleep

A 40-60ms sleep failed about one run in twenty on main as well, on a
loaded machine. They now wait for the condition, up to five seconds.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev e18b8f5955e7bb50268cf525fed6931aef458e06 parent 4691c60
1 file changed, +13 −4
  • fixtures/save-queue.test.mjs +13 −4
diff --git a/fixtures/save-queue.test.mjs b/fixtures/save-queue.test.mjs
index 107ac91..d81d680 100644
--- a/fixtures/save-queue.test.mjs
+++ b/fixtures/save-queue.test.mjs
@@ -24,6 +24,15 @@ function concatUpdates(updates) {
return new Uint8Array(out)
}
+// until waits for cond, on real timers, for as long as a loaded machine
+// may take (a fixed sleep here failed about one run in twenty), and then a
+// little longer, so a call that should not happen has the chance to.
+async function until(cond, ms = 5000) {
+ const end = Date.now() + ms
+ while (!cond() && Date.now() < end) await new Promise((r) => setTimeout(r, 5))
+ await new Promise((r) => setTimeout(r, 30))
+}
+
function makePostStub(responses) {
const calls = []
let i = 0
@@ -51,7 +60,7 @@ test('a permanent 4xx drops its batch instead of retrying it, and does not poiso
const bigPaste = new Uint8Array([9, 9, 9])
q.enqueue(bigPaste)
- await new Promise((r) => setTimeout(r, 40))
+ await until(() => q.status === 'failed')
assert.equal(calls.length, 1, 'the oversized batch should be posted exactly once')
assert.deepEqual(calls[0], bigPaste)
@@ -62,7 +71,7 @@ test('a permanent 4xx drops its batch instead of retrying it, and does not poiso
// reach the server on its own, never merged with the dead batch.
const secondEdit = new Uint8Array([1, 2, 3])
q.enqueue(secondEdit)
- await new Promise((r) => setTimeout(r, 40))
+ await until(() => calls.length === 2 && q.pendingBytes() === null)
assert.equal(calls.length, 2, 'the later edit must be posted, not swallowed')
assert.deepEqual(calls[1], secondEdit, 'the later edit must be posted on its own — merging it with the dead 413 batch would 413 it too')
@@ -76,7 +85,7 @@ test('a network failure or a 5xx is retried, not dropped', async () => {
const edit = new Uint8Array([7, 7])
q.enqueue(edit)
- await new Promise((r) => setTimeout(r, 60))
+ await until(() => calls.length === 2 && q.status === 'ok')
assert.equal(calls.length, 2, 'a network failure must be retried automatically, not just left queued')
assert.deepEqual(calls[0], edit)
@@ -89,7 +98,7 @@ test('a 500 is retried the same way a network failure is, never dropped', async
const q = createSaveQueue({ post, mergeUpdates: concatUpdates, debounceMs: 5 })
q.enqueue(new Uint8Array([4, 4]))
- await new Promise((r) => setTimeout(r, 60))
+ await until(() => calls.length === 2 && q.status === 'ok')
assert.equal(calls.length, 2, 'a 5xx is our own fault, not the client bytes — it must retry, not drop')
assert.equal(q.status, 'ok')