test: avoid timer hangs in cron tests

This commit is contained in:
Peter Steinberger
2026-01-18 05:35:26 +00:00
parent 208398973b
commit d5be8fa576

View File

@@ -13,6 +13,11 @@ import {
installGatewayTestHooks();
async function yieldToEventLoop() {
// Avoid relying on timers (fake timers can leak between tests).
await fs.stat(process.cwd()).catch(() => {});
}
describe("gateway server cron", () => {
test("supports cron.add and cron.list", async () => {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-gw-cron-"));
@@ -265,7 +270,7 @@ describe("gateway server cron", () => {
for (let i = 0; i < 200; i += 1) {
const raw = await fs.readFile(logPath, "utf-8").catch(() => "");
if (raw.trim().length > 0) return raw;
await new Promise((r) => setTimeout(r, 10));
await yieldToEventLoop();
}
throw new Error("timeout waiting for cron run log");
};
@@ -333,7 +338,7 @@ describe("gateway server cron", () => {
for (let i = 0; i < 200; i += 1) {
const raw = await fs.readFile(logPath, "utf-8").catch(() => "");
if (raw.trim().length > 0) return raw;
await new Promise((r) => setTimeout(r, 10));
await yieldToEventLoop();
}
throw new Error("timeout waiting for per-job cron run log");
};
@@ -414,7 +419,7 @@ describe("gateway server cron", () => {
expect(runsRes.ok).toBe(true);
const entries = (runsRes.payload as { entries?: unknown } | null)?.entries;
if (Array.isArray(entries) && entries.length > 0) return entries;
await new Promise((r) => setTimeout(r, 20));
await yieldToEventLoop();
}
throw new Error("timeout waiting for cron.runs entries");
};