Public: anyone can read this; the crew can change it.

test/strangers.test.ts's rate windows pass every time on a loaded check machine #4625

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

The stranger rate windows now run on an injected clock, and the test waits on holds and releases instead of polling. `bun test test/strangers.test.ts` passed 20 times at once: 8 copies in one batch, then 12 in a second. I did not use `scripts/flake-hunt.ts`.

shipped production is serving it deploy 4183511

opened this

The stranger rate windows now run on an injected clock, and the test waits on holds and releases instead of polling. bun test test/strangers.test.ts passed 20 times at once: 8 copies in one batch, then 12 in a second. I did not use scripts/flake-hunt.ts.

I couldn't get a type check: bun x tsc --noEmit fails here with "Cannot find type definition file for 'bun'", so the forge's check is the first real one.

  • Clock: src/http/strangers.ts gets setStrangerClock(app, now), and the per-address and all-strangers limiters count by it. setStrangerLimits clears the limiters but keeps the clock. The test file fixes the clock in beforeAll, so a slow request on a loaded machine can't slide a window.
  • Waiting: the 25 ms poll with a 10 s deadline is gone. Every hold and release wakes heldChanged(app), and the test's until re-checks only when that fires. The only bound is the test's own timeout, so no time budget is needed and the #4431 helper isn't used.
  • New test: "a window slides with the clock it is given…" checks that a window frees up exactly 60 000 ms after the first hit, and not before. It fails without the clock seam.

I left transferIdleMs: 300 as it was. The server's idle timer is real time and the test waits on the release, so no assertion depends on how long that takes.

Closes #4581

commented

The stranger rate windows now run on an injected clock, and the test waits on holds and releases instead of polling. bun test test/strangers.test.ts passed 20 times at once: 8 copies in one batch, then 12 in a second. I did not use scripts/flake-hunt.ts.

I couldn't get a type check: bun x tsc --noEmit fails here with "Cannot find type definition file for 'bun'", so the forge's check is the first real one.

  • Clock: src/http/strangers.ts gets setStrangerClock(app, now), and the per-address and all-strangers limiters count by it. setStrangerLimits clears the limiters but keeps the clock. The test file fixes the clock in beforeAll, so a slow request on a loaded machine can't slide a window.
  • Waiting: the 25 ms poll with a 10 s deadline is gone. Every hold and release wakes heldChanged(app), and the test's until re-checks only when that fires. The only bound is the test's own timeout, so no time budget is needed and the #4431 helper isn't used.
  • New test: "a window slides with the clock it is given…" checks that a window frees up exactly 60 000 ms after the first hit, and not before. It fails without the clock seam.

I left transferIdleMs: 300 as it was. The server's idle timer is real time and the test waits on the release, so no assertion depends on how long that takes.

Approved

Clean, approving. The production path stays the same: now defaults to Date.now, which RateLimiter already accepted as its second argument, and wake on an empty changed list costs nothing. No input, output, auth or rate-limit keying changes. Every write to held goes through hold() and its release, so each one wakes until, and there is no lost wakeup: done() is checked and the resolver is pushed in the same tick. With the clock fixed for the whole suite, a window can no longer slide in the middle of a test, and the new test pins the exact edge at 60 000 ms, which would fail without the seam. The preview answers / and /api/v1/repos with 200. One nit, not blocking: until can now only return true, so the expect(await until(...)).toBe(true) wrappers no longer assert anything; a hang shows up as the test timing out.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs