Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(test): 修复测试门禁锁用例竞态与失败路径的监听泄漏

DavidShenc740db28535a
diff --git a/scripts/__tests__/test-workspaces.test.mjs b/scripts/__tests__/test-workspaces.test.mjs
index 6506493c41..2a2202d823 100644
--- a/scripts/__tests__/test-workspaces.test.mjs
+++ b/scripts/__tests__/test-workspaces.test.mjs
@@ -915,10 +915,28 @@ test("test gate lock reports the holder, waits, and acquires after release", asy
}
});
+// A real probe round connects to every candidate port, and a port that accepts
+// the connection without answering only resolves once the probe socket times
+// out. So the window that waits for the WAIT report has to be far wider than a
+// single probe round, otherwise a slow round loses the race and the assertion
+// fails for reasons unrelated to the lock protocol.
+const REAL_LOCK_WAIT_WINDOW_MS = 30_000;
+const REAL_LOCK_ACQUIRE_TIMEOUT_MS = 60_000;
+
+function raceWithDeadline(candidates, deadlineMs, deadlineValue) {
+ let timer;
+ const deadline = new Promise((resolve) => {
+ timer = setTimeout(() => resolve(deadlineValue), deadlineMs);
+ });
+ return Promise.race([...candidates, deadline]).finally(() => {
+ clearTimeout(timer);
+ });
+}
+
test("two real test gate lock holders serialize on the same identity", async () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "test-gate-real-lock-"));
let firstLock;
- let secondLock;
+ let secondLockPromise;
let reportWaiting;
const waiting = new Promise((resolve) => {
reportWaiting = resolve;
@@ -929,30 +947,39 @@ test("two real test gate lock holders serialize on the same identity", async ()
owner: { pid: 41, tier: "unit", cwd: path.join(root, "first") },
output: () => {},
});
- const secondLockPromise = acquireTestGateLock({
+ secondLockPromise = acquireTestGateLock({
repoRoot: root,
owner: { pid: 42, tier: "db", cwd: path.join(root, "second") },
- timeoutMs: 2_000,
+ timeoutMs: REAL_LOCK_ACQUIRE_TIMEOUT_MS,
retryDelayMs: 10,
output: reportWaiting,
- }).then((lock) => {
- secondLock = lock;
- return lock;
});
- const outcome = await Promise.race([
- waiting.then(() => "waiting"),
- secondLockPromise.then(() => "acquired"),
- new Promise((resolve) => setTimeout(() => resolve("timed-out"), 1_000)),
- ]);
+ // A failing assertion below jumps straight to `finally` while this
+ // acquisition is still running. Attach a no-op handler so an eventual
+ // rejection is never unhandled; the real await and release happen in
+ // `finally`, otherwise a lock bound after the failure keeps its listener
+ // open and `node --test` never exits.
+ secondLockPromise.catch(() => {});
+
+ const outcome = await raceWithDeadline(
+ [waiting.then(() => "waiting"), secondLockPromise.then(() => "acquired")],
+ REAL_LOCK_WAIT_WINDOW_MS,
+ "timed-out",
+ );
assert.equal(outcome, "waiting");
await firstLock.release();
firstLock = undefined;
- await secondLockPromise;
+ const secondLock = await secondLockPromise;
assert.ok(secondLock.port >= 49_152);
} finally {
- await secondLock?.release();
+ // Order matters: releasing the first lock lets the second acquisition
+ // settle immediately instead of waiting out its own timeout.
await firstLock?.release();
+ await secondLockPromise?.then(
+ (lock) => lock.release(),
+ () => {},
+ );
fs.rmSync(root, { recursive: true, force: true });
}
});
diff --git a/scripts/test-gate-lock.mjs b/scripts/test-gate-lock.mjs
index b60b763d5d..d7e15d9d41 100644
--- a/scripts/test-gate-lock.mjs
+++ b/scripts/test-gate-lock.mjs
@@ -188,6 +188,11 @@ async function listenForLock(port, banner) {
socket.end(`${banner}\n`);
});
await listen(server, port);
+ // The lock lives only as long as its holder has real work in flight (a child
+ // test process, a timer, a pending file operation). Keeping the listener
+ // unref'd means a holder that forgets to release still exits instead of
+ // hanging its runner until the CI job times out.
+ server.unref();
return {
port,
release: () => close(server),