From c100de578a49915dfc7ca5c30534ccccfd23d46c Mon Sep 17 00:00:00 2001 From: Christophe Besson Date: Sat, 10 Oct 2026 10:06:59 +0200 Subject: test(hub): wait for a resumed transfer's state, not for 120 ms The pausable run in test_transfers.py moves in setTimeout(5) chunks, and the resume test gave it a fixed 120 ms to finish. A setTimeout(5) lasts about 15 ms on Windows' default timer, so the resumed run took 156 ms there and the test failed with nothing wrong; the 20 ms wait before pausing had the same thin margin. The run now records how far it got (state.at), and the test pauses after two chunks and waits for "done", bounded at 2 s. The assertions are unchanged, and a resume that restarts from zero is still caught (checked by introducing one in transfers.js). Co-Authored-By: Claude Opus 5.5 --- packages/meshbay-hub/tests/test_transfers.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'packages/meshbay-hub/tests/test_transfers.py') diff --git a/packages/meshbay-hub/tests/test_transfers.py b/packages/meshbay-hub/tests/test_transfers.py index c7a9dc5..bacf5ac 100644 --- a/packages/meshbay-hub/tests/test_transfers.py +++ b/packages/meshbay-hub/tests/test_transfers.py @@ -582,8 +582,10 @@ const mkStore = () => { state.starts.push(from); state.running = true; try { - // Runs until told to stop, one "chunk" at a time. + // Runs until told to stop, one "chunk" at a time. `state.at` is how far + // it got, for a test to wait on instead of guessing a duration. for (let i = from; i < 10; i++) { + state.at = i; await new Promise(r => setTimeout(r, 5)); if (signal.aborted) { const e = new Error('c'); e.name = 'AbortError'; throw e; } if (signal.paused) { @@ -623,12 +625,21 @@ def test_pausing_gives_the_slot_back(tmp_path): def test_resuming_asks_for_a_new_slot_and_continues_where_it_stopped(tmp_path): """Rejoining at the tail is the design, not an accident: a paused transfer - that could reclaim its old place would be a way to hold one.""" + that could reclaim its old place would be a way to hold one. + + It waits for states, not durations: a `setTimeout(5)` lasts about 15 ms on + Windows' default timer, so the ten-chunk run outlived a fixed 120 ms wait + there (156 ms measured) and the test failed with nothing wrong.""" out = _run(_lease_stub() + _pausable_run() + """ + const until = async (ok, ms = 2000) => { + for (const t0 = Date.now(); !ok() && Date.now() - t0 < ms;) { + await new Promise(r => setTimeout(r, 1)); + } + }; const { t, leases, state } = mkStore(); await new Promise(r => setTimeout(r, 5)); leases[0].grant(); - await new Promise(r => setTimeout(r, 20)); + await until(() => state.at >= 2); const id = t.list()[0].id; t.pause(id); await new Promise(r => setTimeout(r, 30)); @@ -636,7 +647,7 @@ def test_resuming_asks_for_a_new_slot_and_continues_where_it_stopped(tmp_path): await new Promise(r => setTimeout(r, 10)); say('queued:' + t.list()[0].status, 'leases:' + leases.length); leases[1].grant(); - await new Promise(r => setTimeout(r, 120)); + await until(() => t.list()[0].status === 'done'); say('end:' + t.list()[0].status); say('starts:' + state.starts.join(',')); """, tmp_path) -- cgit v1.2.3