test: avoid timer hangs in cron tests
This commit is contained in:
@@ -13,6 +13,11 @@ import {
|
|||||||
|
|
||||||
installGatewayTestHooks();
|
installGatewayTestHooks();
|
||||||
|
|
||||||
|
async function yieldToEventLoop() {
|
||||||
|
// Avoid relying on timers (fake timers can leak between tests).
|
||||||
|
await fs.stat(process.cwd()).catch(() => {});
|
||||||
|
}
|
||||||
|
|
||||||
describe("gateway server cron", () => {
|
describe("gateway server cron", () => {
|
||||||
test("supports cron.add and cron.list", async () => {
|
test("supports cron.add and cron.list", async () => {
|
||||||
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-gw-cron-"));
|
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) {
|
for (let i = 0; i < 200; i += 1) {
|
||||||
const raw = await fs.readFile(logPath, "utf-8").catch(() => "");
|
const raw = await fs.readFile(logPath, "utf-8").catch(() => "");
|
||||||
if (raw.trim().length > 0) return raw;
|
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");
|
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) {
|
for (let i = 0; i < 200; i += 1) {
|
||||||
const raw = await fs.readFile(logPath, "utf-8").catch(() => "");
|
const raw = await fs.readFile(logPath, "utf-8").catch(() => "");
|
||||||
if (raw.trim().length > 0) return raw;
|
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");
|
throw new Error("timeout waiting for per-job cron run log");
|
||||||
};
|
};
|
||||||
@@ -414,7 +419,7 @@ describe("gateway server cron", () => {
|
|||||||
expect(runsRes.ok).toBe(true);
|
expect(runsRes.ok).toBe(true);
|
||||||
const entries = (runsRes.payload as { entries?: unknown } | null)?.entries;
|
const entries = (runsRes.payload as { entries?: unknown } | null)?.entries;
|
||||||
if (Array.isArray(entries) && entries.length > 0) return 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");
|
throw new Error("timeout waiting for cron.runs entries");
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user