From b83e1a2c98d61e53dd860ac5335760eef6f9c7cd Mon Sep 17 00:00:00 2001 From: daikaginza Date: Fri, 23 Oct 2026 12:39:32 +0700 Subject: [PATCH] test(adsbdb): size the limiter for two client queues and retry a 429 Review on #689: one tab runs two enrichment queues (Flights and Local ADS-B) at 200 ms each, so a real session reaches 600/min, not 300; and a 429 left the aircraft's metadata empty for the session, because both queues mark a key seen before dispatch and swallow the error. The cases pin 600/min fitting, the GEV_RATELIMIT_ADSBDB_PER_MIN override (0 off), and both queues asking again after the refusal. Four fail on the branch as it stands. --- scripts/format-scope.json | 3 +- src/adsbdbCacheGovernor.test.mjs | 43 ++++++-- src/layers/enrichmentRateLimitRetry.test.mjs | 110 +++++++++++++++++++ 3 files changed, 148 insertions(+), 8 deletions(-) create mode 100644 src/layers/enrichmentRateLimitRetry.test.mjs diff --git a/scripts/format-scope.json b/scripts/format-scope.json index 60e0d18a53..dd18717503 100644 --- a/scripts/format-scope.json +++ b/scripts/format-scope.json @@ -1087,5 +1087,6 @@ "src/ui/cctvBadges.test.mjs", "scripts/qa-status-badges.mjs", "src/dockerTrustedPeers.test.mjs", - "src/adsbdbCacheGovernor.test.mjs" + "src/adsbdbCacheGovernor.test.mjs", + "src/layers/enrichmentRateLimitRetry.test.mjs" ] diff --git a/src/adsbdbCacheGovernor.test.mjs b/src/adsbdbCacheGovernor.test.mjs index 98a60251b4..010ce9c9be 100644 --- a/src/adsbdbCacheGovernor.test.mjs +++ b/src/adsbdbCacheGovernor.test.mjs @@ -164,9 +164,9 @@ test('a caller enumerating the keyspace is refused, and refusal costs nothing', let refused = null; let served = 0; - // The browser cannot exceed 300/min (ENRICH_DISPATCH_GAP_MS is 200 ms), so - // an enumerating caller runs out well before a real session would. - for (let i = 0; i < 400 && !refused; i += 1) { + // Each client queue drips one lookup per ENRICH_DISPATCH_GAP_MS (200 ms), + // and one tab runs two (Flights and Local ADS-B): 600/min must fit. + for (let i = 0; i < 1000 && !refused; i += 1) { const res = await request(handler, { url: `/type/${hex(i)}` }); if (res.statusCode === 429) refused = res; else served += 1; @@ -176,8 +176,8 @@ test('a caller enumerating the keyspace is refused, and refusal costs nothing', assert.equal(refused.body.error, 'Rate limit exceeded'); assert.equal(refused.headers['retry-after'], '10'); assert.ok( - served >= 300, - `a real session's ceiling of 300/min must fit; served ${served}`, + served >= 600, + `both client queues together (600/min) must fit; served ${served}`, ); assert.equal( asked.length, @@ -192,14 +192,14 @@ test('a different client keeps its own budget', async (t) => { const handler = routes.get('/api/adsbdb'); let noisy = 0; - for (let i = 0; i < 400; i += 1) { + for (let i = 0; i < 1000; i += 1) { const res = await request(handler, { url: `/type/${hex(i)}`, remoteAddress: '10.0.0.1', }); if (res.statusCode === 200) noisy += 1; } - assert.ok(noisy > 0 && noisy < 400, 'the noisy client was throttled'); + assert.ok(noisy > 0 && noisy < 1000, 'the noisy client was throttled'); const quiet = await request(handler, { url: '/type/ffffff', @@ -207,3 +207,32 @@ test('a different client keeps its own budget', async (t) => { }); assert.equal(quiet.statusCode, 200, 'a quiet client is not collateral'); }); + +test('GEV_RATELIMIT_ADSBDB_PER_MIN sets the cap, and 0 turns it off', async (t) => { + stubUpstream(t); + const original = process.env.GEV_RATELIMIT_ADSBDB_PER_MIN; + t.after(() => { + if (original === undefined) delete process.env.GEV_RATELIMIT_ADSBDB_PER_MIN; + else process.env.GEV_RATELIMIT_ADSBDB_PER_MIN = original; + }); + + process.env.GEV_RATELIMIT_ADSBDB_PER_MIN = '5'; + const tight = install(adsbdbProxy({ cachePath: scratchCachePath() })).get( + '/api/adsbdb', + ); + const statuses = []; + for (let i = 0; i < 6; i += 1) + statuses.push( + (await request(tight, { url: `/type/${hex(i)}` })).statusCode, + ); + assert.deepEqual(statuses, [200, 200, 200, 200, 200, 429]); + + process.env.GEV_RATELIMIT_ADSBDB_PER_MIN = '0'; + const open = install(adsbdbProxy({ cachePath: scratchCachePath() })).get( + '/api/adsbdb', + ); + for (let i = 0; i < 1500; i += 1) { + const res = await request(open, { url: `/type/${hex(i)}` }); + assert.equal(res.statusCode, 200, `request ${i} with the limiter off`); + } +}); diff --git a/src/layers/enrichmentRateLimitRetry.test.mjs b/src/layers/enrichmentRateLimitRetry.test.mjs new file mode 100644 index 0000000000..06df7b6485 --- /dev/null +++ b/src/layers/enrichmentRateLimitRetry.test.mjs @@ -0,0 +1,110 @@ +// A 429 from /api/adsbdb must not leave an aircraft's metadata empty for the +// rest of the session. +// +// Both enrichment queues (Flights and Local ADS-B) mark a key as seen before +// they dispatch it, so a second request for the same aircraft is dropped. That +// is right for an answer or a 404, and wrong for a refusal: the server said +// "later", not "no". These cases drive the real queues against a source that +// refuses once and then answers. +// +// Run with: npm test +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { LiveSourceError } from '../sources/live/contract.js'; +import { createLocalAdsbEnrichment } from './localAdsb/enrichment.js'; +import { createFlightState } from './flights/state.js'; +import { createEnrichment } from './flights/enrichment.js'; + +/** Short real delay, so the case needs no clock control. */ +const RETRY_AFTER_MS = 20; + +/** A source that refuses the first lookup with a 429, then answers. */ +function refusingOnce() { + const calls = []; + return { + calls, + async getEnrichment(query) { + calls.push(query); + if (calls.length === 1) + throw new LiveSourceError('limited', 'Flights rate limited', { + status: 429, + retryAfterMs: RETRY_AFTER_MS, + }); + return { found: true, typeCode: 'B738', typeName: 'Boeing 737-800' }; + }, + }; +} + +const settle = (ms) => new Promise((done) => setTimeout(done, ms)); + +test('Local ADS-B asks again after a 429 once the refusal has passed', async (t) => { + const source = refusingOnce(); + const enrichment = createLocalAdsbEnrichment({ source, onChange() {} }); + t.after(() => enrichment.destroy()); + const record = { icao: 'abc123', category: 'A3' }; + + enrichment.request(record); + await settle(0); + assert.equal(source.calls.length, 1); + enrichment.request(record); + await settle(0); + assert.equal(source.calls.length, 1, 'no retry inside the refusal window'); + + await settle(RETRY_AFTER_MS + 250); // refusal window, then the 200 ms drip + enrichment.request(record); + await settle(250); + assert.equal(source.calls.length, 2, 'the aircraft is asked for again'); + assert.equal(enrichment.get('abc123')?.typeCode, 'B738'); +}); + +test('an answered lookup is still asked for only once', async (t) => { + const calls = []; + const enrichment = createLocalAdsbEnrichment({ + source: { + async getEnrichment(query) { + calls.push(query); + return { found: false }; + }, + }, + onChange() {}, + }); + t.after(() => enrichment.destroy()); + enrichment.request({ icao: 'def456' }); + await settle(RETRY_AFTER_MS + 250); + enrichment.request({ icao: 'def456' }); + await settle(250); + assert.equal(calls.length, 1, 'a 404 is an answer, not a refusal'); +}); + +test('Flights asks again after a 429 once the refusal has passed', async (t) => { + const source = refusingOnce(); + const flightState = createFlightState({ + source, + services: { + groundSnap: { createGroundSnap: () => ({ clear() {} }) }, + }, + }); + t.after(() => flightState.lifetime.abort()); + const enrichment = createEnrichment({ flightState, services: {}, parts: {} }); + let applied = null; + const ask = () => + enrichment._enqueueEnrich( + 't:abc123', + { kind: 'type', id: 'abc123' }, + (data) => { + applied = data; + }, + ); + + ask(); + await settle(0); + ask(); + await settle(0); + assert.equal(source.calls.length, 1, 'no retry inside the refusal window'); + + await settle(RETRY_AFTER_MS + 250); + ask(); + await settle(250); + assert.equal(source.calls.length, 2, 'the aircraft is asked for again'); + assert.equal(applied?.typeCode, 'B738'); +});