mirror of
https://github.com/openclaw/openclaw.git
synced 2026-02-19 18:39:20 -05:00
perf(test): replace timeout ticks with microtask waits
This commit is contained in:
@@ -15,6 +15,10 @@ type MockIncomingMessage = IncomingMessage & {
|
||||
__unhandledDestroyError?: unknown;
|
||||
};
|
||||
|
||||
async function waitForMicrotaskTurn(): Promise<void> {
|
||||
await new Promise<void>((resolve) => queueMicrotask(resolve));
|
||||
}
|
||||
|
||||
function createMockRequest(params: {
|
||||
chunks?: string[];
|
||||
headers?: Record<string, string>;
|
||||
@@ -101,7 +105,7 @@ describe("http body limits", () => {
|
||||
const req = createMockRequest({ chunks: ["small", "x".repeat(256)], emitEnd: false });
|
||||
const res = createMockServerResponse();
|
||||
const guard = installRequestBodyLimitGuard(req, res, { maxBytes: 128, responseFormat: "text" });
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
await waitForMicrotaskTurn();
|
||||
expect(guard.isTripped()).toBe(true);
|
||||
expect(guard.code()).toBe("PAYLOAD_TOO_LARGE");
|
||||
expect(res.statusCode).toBe(413);
|
||||
@@ -127,7 +131,7 @@ describe("http body limits", () => {
|
||||
message: "PayloadTooLarge",
|
||||
});
|
||||
// Wait a tick for any async destroy(err) emission.
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
await waitForMicrotaskTurn();
|
||||
expect(req.__unhandledDestroyError).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -6,6 +6,10 @@ vi.mock("../infra/net/fetch-guard.js", () => ({
|
||||
fetchWithSsrFGuard: (...args: unknown[]) => fetchWithSsrFGuardMock(...args),
|
||||
}));
|
||||
|
||||
async function waitForMicrotaskTurn(): Promise<void> {
|
||||
await new Promise<void>((resolve) => queueMicrotask(resolve));
|
||||
}
|
||||
|
||||
describe("fetchWithGuard", () => {
|
||||
it("rejects oversized streamed payloads and cancels the stream", async () => {
|
||||
let canceled = false;
|
||||
@@ -47,7 +51,7 @@ describe("fetchWithGuard", () => {
|
||||
).rejects.toThrow("Content too large");
|
||||
|
||||
// Allow cancel() microtask to run.
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
await waitForMicrotaskTurn();
|
||||
|
||||
expect(canceled).toBe(true);
|
||||
expect(release).toHaveBeenCalledTimes(1);
|
||||
|
||||
Reference in New Issue
Block a user