mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 20:24:33 +00:00
perf(test): remove fixed sleeps in async test flows
This commit is contained in:
@@ -1146,7 +1146,7 @@ describe("createReplyDispatcher", () => {
|
|||||||
const deliver = vi.fn(async (_payload, info) => {
|
const deliver = vi.fn(async (_payload, info) => {
|
||||||
delivered.push(info.kind);
|
delivered.push(info.kind);
|
||||||
if (info.kind === "tool") {
|
if (info.kind === "tool") {
|
||||||
await new Promise((resolve) => setTimeout(resolve, 5));
|
await Promise.resolve();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
const dispatcher = createReplyDispatcher({ deliver });
|
const dispatcher = createReplyDispatcher({ deliver });
|
||||||
@@ -1161,7 +1161,7 @@ describe("createReplyDispatcher", () => {
|
|||||||
|
|
||||||
it("fires onIdle when the queue drains", async () => {
|
it("fires onIdle when the queue drains", async () => {
|
||||||
const deliver: Parameters<typeof createReplyDispatcher>[0]["deliver"] = async () =>
|
const deliver: Parameters<typeof createReplyDispatcher>[0]["deliver"] = async () =>
|
||||||
await new Promise((resolve) => setTimeout(resolve, 5));
|
await Promise.resolve();
|
||||||
const onIdle = vi.fn();
|
const onIdle = vi.fn();
|
||||||
const dispatcher = createReplyDispatcher({ deliver, onIdle });
|
const dispatcher = createReplyDispatcher({ deliver, onIdle });
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,10 @@ vi.mock("./index.js", () => {
|
|||||||
}
|
}
|
||||||
const onAbort = () => reject(signal?.reason ?? new Error("aborted"));
|
const onAbort = () => reject(signal?.reason ?? new Error("aborted"));
|
||||||
signal?.addEventListener("abort", onAbort, { once: true });
|
signal?.addEventListener("abort", onAbort, { once: true });
|
||||||
setTimeout(resolve, 50);
|
queueMicrotask(() => {
|
||||||
|
signal?.removeEventListener("abort", onAbort);
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
res.json({ ok: true });
|
res.json({ ok: true });
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -15,6 +15,14 @@ const getLoggerInfo = vi.fn();
|
|||||||
const asString = (value: unknown, fallback: string) =>
|
const asString = (value: unknown, fallback: string) =>
|
||||||
typeof value === "string" && value.trim() ? value : fallback;
|
typeof value === "string" && value.trim() ? value : fallback;
|
||||||
|
|
||||||
|
function enableAdvertiserUnitMode(hostname = "test-host") {
|
||||||
|
// Allow advertiser to run in unit tests.
|
||||||
|
delete process.env.VITEST;
|
||||||
|
process.env.NODE_ENV = "development";
|
||||||
|
vi.spyOn(os, "hostname").mockReturnValue(hostname);
|
||||||
|
process.env.OPENCLAW_MDNS_HOSTNAME = hostname;
|
||||||
|
}
|
||||||
|
|
||||||
function mockCiaoService(params?: {
|
function mockCiaoService(params?: {
|
||||||
advertise?: ReturnType<typeof vi.fn>;
|
advertise?: ReturnType<typeof vi.fn>;
|
||||||
destroy?: ReturnType<typeof vi.fn>;
|
destroy?: ReturnType<typeof vi.fn>;
|
||||||
@@ -105,19 +113,14 @@ describe("gateway bonjour advertiser", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("does not block on advertise and publishes expected txt keys", async () => {
|
it("does not block on advertise and publishes expected txt keys", async () => {
|
||||||
// Allow advertiser to run in unit tests.
|
enableAdvertiserUnitMode();
|
||||||
delete process.env.VITEST;
|
|
||||||
process.env.NODE_ENV = "development";
|
|
||||||
|
|
||||||
vi.spyOn(os, "hostname").mockReturnValue("test-host");
|
|
||||||
process.env.OPENCLAW_MDNS_HOSTNAME = "test-host";
|
|
||||||
process.env.OPENCLAW_MDNS_HOSTNAME = "test-host";
|
|
||||||
|
|
||||||
const destroy = vi.fn().mockResolvedValue(undefined);
|
const destroy = vi.fn().mockResolvedValue(undefined);
|
||||||
|
let resolveAdvertise = () => {};
|
||||||
const advertise = vi.fn().mockImplementation(
|
const advertise = vi.fn().mockImplementation(
|
||||||
async () =>
|
async () =>
|
||||||
await new Promise<void>((resolve) => {
|
await new Promise<void>((resolve) => {
|
||||||
setTimeout(resolve, 250);
|
resolveAdvertise = resolve;
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
mockCiaoService({ advertise, destroy });
|
mockCiaoService({ advertise, destroy });
|
||||||
@@ -147,6 +150,8 @@ describe("gateway bonjour advertiser", () => {
|
|||||||
|
|
||||||
// We don't await `advertise()`, but it should still be called for each service.
|
// We don't await `advertise()`, but it should still be called for each service.
|
||||||
expect(advertise).toHaveBeenCalledTimes(1);
|
expect(advertise).toHaveBeenCalledTimes(1);
|
||||||
|
resolveAdvertise();
|
||||||
|
await Promise.resolve();
|
||||||
|
|
||||||
await started.stop();
|
await started.stop();
|
||||||
expect(destroy).toHaveBeenCalledTimes(1);
|
expect(destroy).toHaveBeenCalledTimes(1);
|
||||||
@@ -154,11 +159,7 @@ describe("gateway bonjour advertiser", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("omits cliPath and sshPort in minimal mode", async () => {
|
it("omits cliPath and sshPort in minimal mode", async () => {
|
||||||
// Allow advertiser to run in unit tests.
|
enableAdvertiserUnitMode();
|
||||||
delete process.env.VITEST;
|
|
||||||
process.env.NODE_ENV = "development";
|
|
||||||
|
|
||||||
vi.spyOn(os, "hostname").mockReturnValue("test-host");
|
|
||||||
|
|
||||||
const destroy = vi.fn().mockResolvedValue(undefined);
|
const destroy = vi.fn().mockResolvedValue(undefined);
|
||||||
const advertise = vi.fn().mockResolvedValue(undefined);
|
const advertise = vi.fn().mockResolvedValue(undefined);
|
||||||
@@ -179,12 +180,7 @@ describe("gateway bonjour advertiser", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("attaches conflict listeners for services", async () => {
|
it("attaches conflict listeners for services", async () => {
|
||||||
// Allow advertiser to run in unit tests.
|
enableAdvertiserUnitMode();
|
||||||
delete process.env.VITEST;
|
|
||||||
process.env.NODE_ENV = "development";
|
|
||||||
|
|
||||||
vi.spyOn(os, "hostname").mockReturnValue("test-host");
|
|
||||||
process.env.OPENCLAW_MDNS_HOSTNAME = "test-host";
|
|
||||||
|
|
||||||
const destroy = vi.fn().mockResolvedValue(undefined);
|
const destroy = vi.fn().mockResolvedValue(undefined);
|
||||||
const advertise = vi.fn().mockResolvedValue(undefined);
|
const advertise = vi.fn().mockResolvedValue(undefined);
|
||||||
@@ -207,12 +203,7 @@ describe("gateway bonjour advertiser", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("cleans up unhandled rejection handler after shutdown", async () => {
|
it("cleans up unhandled rejection handler after shutdown", async () => {
|
||||||
// Allow advertiser to run in unit tests.
|
enableAdvertiserUnitMode();
|
||||||
delete process.env.VITEST;
|
|
||||||
process.env.NODE_ENV = "development";
|
|
||||||
|
|
||||||
vi.spyOn(os, "hostname").mockReturnValue("test-host");
|
|
||||||
process.env.OPENCLAW_MDNS_HOSTNAME = "test-host";
|
|
||||||
|
|
||||||
const destroy = vi.fn().mockResolvedValue(undefined);
|
const destroy = vi.fn().mockResolvedValue(undefined);
|
||||||
const advertise = vi.fn().mockResolvedValue(undefined);
|
const advertise = vi.fn().mockResolvedValue(undefined);
|
||||||
@@ -240,13 +231,8 @@ describe("gateway bonjour advertiser", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("logs advertise failures and retries via watchdog", async () => {
|
it("logs advertise failures and retries via watchdog", async () => {
|
||||||
// Allow advertiser to run in unit tests.
|
enableAdvertiserUnitMode();
|
||||||
delete process.env.VITEST;
|
|
||||||
process.env.NODE_ENV = "development";
|
|
||||||
|
|
||||||
vi.useFakeTimers();
|
vi.useFakeTimers();
|
||||||
vi.spyOn(os, "hostname").mockReturnValue("test-host");
|
|
||||||
process.env.OPENCLAW_MDNS_HOSTNAME = "test-host";
|
|
||||||
|
|
||||||
const destroy = vi.fn().mockResolvedValue(undefined);
|
const destroy = vi.fn().mockResolvedValue(undefined);
|
||||||
const advertise = vi
|
const advertise = vi
|
||||||
@@ -278,12 +264,7 @@ describe("gateway bonjour advertiser", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("handles advertise throwing synchronously", async () => {
|
it("handles advertise throwing synchronously", async () => {
|
||||||
// Allow advertiser to run in unit tests.
|
enableAdvertiserUnitMode();
|
||||||
delete process.env.VITEST;
|
|
||||||
process.env.NODE_ENV = "development";
|
|
||||||
|
|
||||||
vi.spyOn(os, "hostname").mockReturnValue("test-host");
|
|
||||||
process.env.OPENCLAW_MDNS_HOSTNAME = "test-host";
|
|
||||||
|
|
||||||
const destroy = vi.fn().mockResolvedValue(undefined);
|
const destroy = vi.fn().mockResolvedValue(undefined);
|
||||||
const advertise = vi.fn(() => {
|
const advertise = vi.fn(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user