Skip to content

Commit c10e3a8

Browse files
committed
test(remote): settle the slow-beat count on arrivals instead of a wall clock
The slow-link case sampled `beatArrivals.length` at a fixed 4.5s and asserted 2. Both directions of that number depended on event-loop timing: a stall over the margin between the first answer landing and the assumed cadence arming a third beat read as three beats, and a stall long enough to keep the second beat out of the sample read as one. Neither says anything about the loop under test. The count now waits for the two beats it expects, then keeps watching through a quiet window of two assumed cadences and returns what arrived. A loop that budgets a beat at its cadence is still arriving throughout that window and is counted, not awaited; a loop that moved to the window it just renewed is silent until ten seconds in. A runner stall delays the read instead of moving a beat across it. Verified by mutation both ways: budgeting the beat at its interval reports 4 beats, and arming the opening beat late fails with "only 0 of 2 beats arrived" rather than hanging to the lane timeout. The assumed cadence is derived from MIN_LEASE_WINDOW_MS instead of restated, so it cannot drift from what the loop actually assumes.
1 parent 9ab78fb commit c10e3a8

1 file changed

Lines changed: 51 additions & 12 deletions

File tree

‎test/integration/provider-scenarios/remote-upload-lease-beat.test.ts‎

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import os from 'node:os';
55
import path from 'node:path';
66
import { test } from 'vitest';
77
import { AppError } from '@agent-device/kernel/errors';
8+
import { MIN_LEASE_WINDOW_MS } from '@agent-device/contracts/lease-scope';
89
import {
910
countDiagnosticEventsByPhase,
1011
withDiagnosticsScope,
@@ -36,8 +37,16 @@ const LEASE_WINDOW_MS = 3_000;
3637
const SLOW_BEAT_MS = 2_500;
3738
/** The window the fake daemon reports on the slow-link case, whose third is its cadence. */
3839
const SLOW_BEAT_LEASE_WINDOW_MS = 30_000;
39-
/** How long the slow-link case watches the beat before letting the artifact finish. */
40-
const SLOW_BEAT_OBSERVATION_MS = 4_500;
40+
/** The cadence the loop assumes before any window is known: a third of the registry minimum. */
41+
const ASSUMED_BEAT_INTERVAL_MS = Math.floor(MIN_LEASE_WINDOW_MS / 3);
42+
/**
43+
* How long the slow-link case watches for an extra beat after the second one lands. Two assumed
44+
* cadences: a loop that budgets a beat at its cadence re-sends every one of them and is caught
45+
* inside this window, while the correct loop has moved to a third of the 30s window by then and is
46+
* quiet until ten seconds in. Sampling on arrivals plus a quiet window — rather than a fixed wall
47+
* clock — keeps a runner stall from reading as a missing beat.
48+
*/
49+
const SLOW_BEAT_QUIET_WINDOW_MS = ASSUMED_BEAT_INTERVAL_MS * 2;
4150

4251
/**
4352
* How the fake daemon makes the upload the long phase it is.
@@ -298,6 +307,37 @@ function installRequest(baseUrl: string, apkPath: string): Omit<DaemonRequest, '
298307

299308
const TRANSPORT = { authToken: TOKEN } as const;
300309

310+
function sleep(ms: number): Promise<void> {
311+
return new Promise<void>((resolve) => {
312+
setTimeout(resolve, ms).unref();
313+
});
314+
}
315+
316+
/**
317+
* How many beats arrived, once the loop has been seen to go quiet.
318+
*
319+
* Waits for `expected` arrivals, then keeps watching through one quiet window and returns the count.
320+
* A loop that keeps beating at the cadence it assumed is still arriving inside that window, so it is
321+
* counted rather than awaited; a loop that moved to the window it just renewed is silent through it.
322+
* Settling on arrivals instead of a fixed wall clock means a runner stall delays the sample instead
323+
* of dropping a beat from it. The deadline bounds the wait so a beat that never comes fails on that
324+
* fact rather than on the lane's timeout.
325+
*/
326+
async function settleBeatArrivals(daemon: FakeDaemon, expected: number): Promise<number> {
327+
const deadline = Date.now() + ASSUMED_BEAT_INTERVAL_MS * 3;
328+
while (daemon.beatArrivals.length < expected) {
329+
if (Date.now() > deadline) {
330+
throw new Error(
331+
`only ${String(daemon.beatArrivals.length)} of ${String(expected)} beats arrived; ` +
332+
`daemon saw: ${daemon.seen.join(', ') || 'nothing'}`,
333+
);
334+
}
335+
await sleep(25);
336+
}
337+
await sleep(SLOW_BEAT_QUIET_WINDOW_MS);
338+
return daemon.beatArrivals.length;
339+
}
340+
301341
async function withUploadedArtifact<R>(
302342
t: { skip(reason?: string): void },
303343
mode: UploadMode,
@@ -363,24 +403,23 @@ test(
363403
// anything: outside one, `emitDiagnostic` records nothing at all and every count reads zero.
364404
await withDiagnosticsScope({ session: 'upload-beat', command: 'install' }, async () => {
365405
const running = sendToDaemon(installRequest(daemon.baseUrl, apkPath), TRANSPORT);
366-
await new Promise<void>((resolve) => {
367-
setTimeout(resolve, SLOW_BEAT_OBSERVATION_MS).unref();
368-
});
406+
// Two beats, not three: the opening one, and the successor the loop had already armed at
407+
// the cadence it assumes before any window is known. The first beat's late answer then
408+
// moves the loop to a third of the 30s window it just renewed, so nothing else is due for
409+
// ten seconds. A beat budgeted at its cadence is cut off before that answer lands, never
410+
// learns the window, and keeps arriving every assumed cadence with a `daemon_request_timeout`
411+
// behind it — which is #2946's slow link wearing the beat down instead of protecting it.
412+
// The count settles on arrivals going quiet rather than on a wall-clock sample, so a loaded
413+
// runner delays the read instead of moving a beat across it.
414+
beatsDuringUpload = await settleBeatArrivals(daemon, 2);
369415
timeoutsDuringUpload = countDiagnosticEventsByPhase(['daemon_request_timeout']);
370-
beatsDuringUpload = daemon.beatArrivals.length;
371416
// Only now does the artifact get the rest of its way in, so everything above was measured
372417
// while the upload was genuinely still running.
373418
daemon.resumeUpload();
374419
response = await running;
375420
});
376421

377422
assert.equal(response!.ok, true);
378-
// Two beats, not three: the opening one, and the successor the loop had already armed at the
379-
// cadence it assumes before any window is known. The first beat's late answer then moves the
380-
// loop to a third of the 30s window it just renewed, so nothing else is due inside the
381-
// observation. A beat budgeted at its cadence is cut off before that answer lands, never learns
382-
// the window, and keeps arriving every assumed cadence with a `daemon_request_timeout` behind it
383-
// — which is #2946's slow link wearing the beat down instead of protecting it.
384423
assert.equal(beatsDuringUpload, 2, `beats seen: ${String(beatsDuringUpload)}`);
385424
assert.equal(
386425
timeoutsDuringUpload,

0 commit comments

Comments
 (0)