fix(arena): clamp early-timer catch-up so the tick loop holds 20Hz
Node timers can fire a sub-millisecond early relative to performance.now(); Math.floor((now - scheduledFor) / TICK_MS) then yields -1, and (1 + extra) shaved a whole tick off the next schedule. The resulting delay-0 re-fire doubled the sim rate (~33-40Hz observed on the live arena during integration testing; local repro confirmed it is the scheduler, not the host). Clamp extra to [0, TICK_CATCHUP_CAP] and add a wall-clock regression test that pins driftReport ticksRun to ~20/s (fails at the doubled rate).
This commit is contained in:
parent
c0167ac44b
commit
2713a786e4
2 changed files with 37 additions and 1 deletions
|
|
@ -241,7 +241,11 @@ export class BomperatorGameModule implements RealtimeGameModule {
|
|||
record.ticksRun += 1;
|
||||
if (!record.timer) return; // step completed or disposed the room
|
||||
|
||||
const extra = Math.min(Math.floor((now - scheduledFor) / TICK_MS), TICK_CATCHUP_CAP);
|
||||
// Clamp BOTH sides: an early fire (Node timers can land a sub-millisecond
|
||||
// ahead of performance.now()) floors to -1, and an unclamped negative
|
||||
// "extra" shaves a whole tick off the next schedule, producing an instant
|
||||
// catch-up re-fire that doubles the sim rate (~33Hz observed in prod).
|
||||
const extra = Math.max(0, Math.min(Math.floor((now - scheduledFor) / TICK_MS), TICK_CATCHUP_CAP));
|
||||
for (let i = 0; i < extra; i += 1) {
|
||||
this.step(record);
|
||||
record.ticksRun += 1;
|
||||
|
|
|
|||
|
|
@ -54,6 +54,38 @@ beforeEach(() => {
|
|||
});
|
||||
afterEach(() => ctx.mod.clear());
|
||||
|
||||
describe("arena bomperator module: scheduler rate", () => {
|
||||
it(
|
||||
"wall-clock loop advances the sim at ~20Hz, not faster (early-timer regression)",
|
||||
async () => {
|
||||
// Prod bug this pins: Node timers may fire a sub-millisecond early vs
|
||||
// performance.now(); an unclamped negative catch-up `extra` shaved a
|
||||
// tick off the next schedule and doubled the rate (~33-40Hz observed).
|
||||
const mod = new BomperatorGameModule({
|
||||
deliver: () => {},
|
||||
onResults: () => {},
|
||||
autoTick: true,
|
||||
});
|
||||
try {
|
||||
const matchId = mod.createRoom(8001, "HzHost");
|
||||
mod.join(matchId, 8002, "HzGuest");
|
||||
mod.start(matchId, 8001);
|
||||
mod.driftReport(); // reset counters
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
const report = mod.driftReport()[0];
|
||||
expect(report).toBeDefined();
|
||||
// 20Hz over 1s = ~20 ticks; generous bounds for slow CI, but firmly
|
||||
// below the doubled rate and above a starved one.
|
||||
expect(report!.ticksRun).toBeGreaterThanOrEqual(14);
|
||||
expect(report!.ticksRun).toBeLessThanOrEqual(26);
|
||||
} finally {
|
||||
mod.clear();
|
||||
}
|
||||
},
|
||||
15_000,
|
||||
);
|
||||
});
|
||||
|
||||
describe("arena bomperator module: lobby", () => {
|
||||
it("seats the host and reports a waiting match", () => {
|
||||
const matchId = ctx.mod.createRoom(9001, "Host");
|
||||
|
|
|
|||
Loading…
Reference in a new issue