fix: serialize local-agent heartbeats

This commit is contained in:
kris
2026-03-31 21:49:46 +08:00
parent 02fcc56332
commit be31503d22
3 changed files with 59 additions and 1 deletions

View File

@@ -0,0 +1,39 @@
import test from "node:test";
import assert from "node:assert/strict";
import { createSerializedRunner } from "../local-agent/serialized-runner.mjs";
test("createSerializedRunner coalesces overlapping calls into the same promise", async () => {
let runs = 0;
let release;
const gate = new Promise((resolve) => {
release = resolve;
});
const runner = createSerializedRunner(async () => {
runs += 1;
await gate;
return { runs };
});
const first = runner();
const second = runner();
assert.equal(first, second);
assert.equal(runs, 1);
release();
const result = await first;
assert.deepEqual(result, { runs: 1 });
});
test("createSerializedRunner allows the next call after the current run finishes", async () => {
let runs = 0;
const runner = createSerializedRunner(async () => {
runs += 1;
return runs;
});
assert.equal(await runner(), 1);
assert.equal(await runner(), 2);
assert.equal(runs, 2);
});