mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-04 08:54:03 +00:00
fix(utils): clamp shared sleep timers
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { MAX_TIMER_TIMEOUT_MS } from "./shared/number-coercion.js";
|
||||
import { withTempDir } from "./test-helpers/temp-dir.js";
|
||||
import {
|
||||
ensureDir,
|
||||
@@ -33,6 +34,22 @@ describe("sleep", () => {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
|
||||
it("clamps oversized sleep delays before scheduling", async () => {
|
||||
vi.useFakeTimers();
|
||||
const setTimeoutSpy = vi.spyOn(globalThis, "setTimeout");
|
||||
try {
|
||||
const promise = sleep(Number.MAX_SAFE_INTEGER);
|
||||
|
||||
expect(setTimeoutSpy).toHaveBeenCalledWith(expect.any(Function), MAX_TIMER_TIMEOUT_MS);
|
||||
|
||||
vi.advanceTimersByTime(MAX_TIMER_TIMEOUT_MS);
|
||||
await expect(promise).resolves.toBeUndefined();
|
||||
} finally {
|
||||
setTimeoutSpy.mockRestore();
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveConfigDir", () => {
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
resolveRequiredHomeDir,
|
||||
} from "./infra/home-dir.js";
|
||||
import { isPlainObject } from "./infra/plain-object.js";
|
||||
import { resolveTimerTimeoutMs } from "./shared/number-coercion.js";
|
||||
export { escapeRegExp } from "./shared/regexp.js";
|
||||
|
||||
export async function ensureDir(dir: string) {
|
||||
@@ -57,7 +58,7 @@ export function normalizeE164(number: string): string {
|
||||
}
|
||||
|
||||
export function sleep(ms: number) {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
return new Promise((resolve) => setTimeout(resolve, resolveTimerTimeoutMs(ms, 0, 0)));
|
||||
}
|
||||
|
||||
function isHighSurrogate(codeUnit: number): boolean {
|
||||
|
||||
Reference in New Issue
Block a user