Skip to content

A spy satellite simulator in your browser, except the data is real. Live open source spatial intelligence on a photorealistic 3D globe.

test: fix flaky timing and timeout guards (#969)

Ian Blenke504d56ba2b3d
diff --git a/server/providers/transitHistory.test.mjs b/server/providers/transitHistory.test.mjs
index 6ea9209f1..cb06c182a 100644
--- a/server/providers/transitHistory.test.mjs
+++ b/server/providers/transitHistory.test.mjs
@@ -142,9 +142,11 @@ test('three identical-order 15000-vehicle snapshots retain paths within a bounde
const rows = Array.from({ length: 15000 }, (_, i) =>
row(now, `vehicle-${i}`),
);
- const start = performance.now();
+ // Main-thread CPU: excludes scheduler waits and parallel GC helper threads.
+ const start = process.threadCpuUsage();
h.ingest(feed, rows, now);
- times.push(performance.now() - start);
+ const usage = process.threadCpuUsage(start);
+ times.push((usage.user + usage.system) / 1000);
}
assert.equal(h.diagnostics().keys, 15000);
for (let i = 0; i < 15000; i++) {
@@ -154,12 +156,12 @@ test('three identical-order 15000-vehicle snapshots retain paths within a bounde
}
assert.ok(h.diagnostics().allocatedBytes < 16 * 1024 * 1024);
t.diagnostic(
- `15000-vehicle ingestion ms: ${times.map((n) => n.toFixed(1)).join('/')}`,
+ `15000-vehicle ingestion CPU ms: ${times.map((n) => n.toFixed(1)).join('/')}`,
);
- // A ceiling that says "not quadratic", not a benchmark: 15,000 rows in
- // well under two seconds on a loaded laptop or a slow CI runner. The
- // per-run figure above is the diagnostic to read.
- assert.ok(Math.max(...times) < 2000, `ingestion ms: ${times}`);
+ // CPU time measures ingestion work without counting scheduler delays on
+ // loaded runners. Keep the two-second ceiling as a guard against quadratic
+ // work, not a benchmark; the per-poll figures above show any slowdown.
+ assert.ok(Math.max(...times) < 2000, `ingestion CPU ms: ${times}`);
});
test('snapshot capacity pressure preserves members not yet visited and discloses recreated history', (t) => {
diff --git a/src/annotations/regionRing.test.mjs b/src/annotations/regionRing.test.mjs
index 09814247b..cd8ad6447 100644
--- a/src/annotations/regionRing.test.mjs
+++ b/src/annotations/regionRing.test.mjs
@@ -51,9 +51,11 @@ test('region ring: Natural Earth names resolve without a geocoder', async () =>
test('region ring: a slow admin-boundary lookup returns region-timeout within the budget', async () => {
let adminSignal;
let release;
+ let lookupStarted;
const { resolveRegionRingForQuery } = createAnnotationResolver({
featureSource: featureSource({
getAdministrativeAreas(_point, { signal }) {
+ lookupStarted = Date.now();
adminSignal = signal;
return new Promise((resolve) => {
release = resolve;
@@ -61,7 +63,6 @@ test('region ring: a slow admin-boundary lookup returns region-timeout within th
},
}),
});
- const started = Date.now();
const region = await resolveRegionRingForQuery(
'Atlantis',
undefined,
@@ -73,8 +74,10 @@ test('region ring: a slow admin-boundary lookup returns region-timeout within th
ring: null,
error: 'region-timeout',
});
+ // Timed from the boundary lookup, so the bundled pack scans before it do not
+ // count. Under the 3 s default budget, so an ignored budgetMs still fails.
assert.ok(
- Date.now() - started < 1000,
+ Date.now() - lookupStarted < 2_000,
'returns at the budget, not the lookup',
);
// The lookup is left running so it can fill the boundary cache.
diff --git a/src/data/manager.test.mjs b/src/data/manager.test.mjs
index fce64ae8f..23641c962 100644
--- a/src/data/manager.test.mjs
+++ b/src/data/manager.test.mjs
@@ -2398,10 +2398,13 @@ test('a newer absolute intent aborts a hung guard-compensation instead of starvi
removeGuard();
// The newest intent must be able to abort the hung compensation and run.
const pendingFinalOn = mgr.setEnabled('flights', true, { origin: 'user' });
+ let giveUpTimer;
const finalOn = await Promise.race([
pendingFinalOn,
- new Promise((resolve) => setTimeout(() => resolve('starved'), 2000)),
- ]);
+ new Promise((resolve) => {
+ giveUpTimer = setTimeout(() => resolve('starved'), 2000);
+ }),
+ ]).finally(() => clearTimeout(giveUpTimer));
assert.notEqual(finalOn, 'starved', 'newest intent must not starve behind the compensation');
assert.equal(compensationAborted, true, 'the hung compensation was aborted');
assert.equal(await pendingBlockedOn, false, 'the guard-blocked request stays unfulfilled');
@@ -2451,10 +2454,13 @@ test('re-entrant setEnabled from a blocked-adoption listener supersedes the comp
releaseFirstDisable();
const blockedOn = await pendingBlockedOn;
+ let giveUpTimer;
const reentrant = await Promise.race([
(async () => reentrantResult === null ? 'never-fired' : await reentrantResult)(),
- new Promise((resolve) => setTimeout(() => resolve('starved'), 2000)),
- ]);
+ new Promise((resolve) => {
+ giveUpTimer = setTimeout(() => resolve('starved'), 2000);
+ }),
+ ]).finally(() => clearTimeout(giveUpTimer));
await pendingOff;
await mgr.waitForLayerSettled('flights');
unsubscribe();
diff --git a/src/sdr/controller.test.mjs b/src/sdr/controller.test.mjs
index 95b1eb5f2..f8078dbe5 100644
--- a/src/sdr/controller.test.mjs
+++ b/src/sdr/controller.test.mjs
@@ -357,10 +357,11 @@ test('WebUSB picker remains available when no authorized receiver matches', asyn
/** Resolve with the promise's value, or 'timed out' after `ms`. */
function within(promise, ms = 1_000) {
- return Promise.race([
- promise,
- new Promise((resolve) => setTimeout(() => resolve('timed out'), ms)),
- ]);
+ let timer;
+ const timeout = new Promise((resolve) => {
+ timer = setTimeout(() => resolve('timed out'), ms);
+ });
+ return Promise.race([promise, timeout]).finally(() => clearTimeout(timer));
}
/**