mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 02:11:23 +00:00
fix(telegram): restart stalled polling after unhandled network errors
This commit is contained in:
@@ -28,6 +28,7 @@ const { initSpy, runSpy, loadConfig } = vi.hoisted(() => ({
|
||||
runSpy: vi.fn(() => ({
|
||||
task: () => Promise.resolve(),
|
||||
stop: vi.fn(),
|
||||
isRunning: () => false,
|
||||
})),
|
||||
loadConfig: vi.fn(() => ({
|
||||
agents: { defaults: { maxConcurrent: 2 } },
|
||||
@@ -35,6 +36,25 @@ const { initSpy, runSpy, loadConfig } = vi.hoisted(() => ({
|
||||
})),
|
||||
}));
|
||||
|
||||
const { registerUnhandledRejectionHandlerMock, emitUnhandledRejection, resetUnhandledRejection } =
|
||||
vi.hoisted(() => {
|
||||
let handler: ((reason: unknown) => boolean) | undefined;
|
||||
return {
|
||||
registerUnhandledRejectionHandlerMock: vi.fn((next: (reason: unknown) => boolean) => {
|
||||
handler = next;
|
||||
return () => {
|
||||
if (handler === next) {
|
||||
handler = undefined;
|
||||
}
|
||||
};
|
||||
}),
|
||||
emitUnhandledRejection: (reason: unknown) => handler?.(reason) ?? false,
|
||||
resetUnhandledRejection: () => {
|
||||
handler = undefined;
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
const { computeBackoff, sleepWithAbort } = vi.hoisted(() => ({
|
||||
computeBackoff: vi.fn(() => 0),
|
||||
sleepWithAbort: vi.fn(async () => undefined),
|
||||
@@ -87,6 +107,10 @@ vi.mock("../infra/backoff.js", () => ({
|
||||
sleepWithAbort,
|
||||
}));
|
||||
|
||||
vi.mock("../infra/unhandled-rejections.js", () => ({
|
||||
registerUnhandledRejectionHandler: registerUnhandledRejectionHandlerMock,
|
||||
}));
|
||||
|
||||
vi.mock("./webhook.js", () => ({
|
||||
startTelegramWebhook: startTelegramWebhookSpy,
|
||||
}));
|
||||
@@ -108,6 +132,8 @@ describe("monitorTelegramProvider (grammY)", () => {
|
||||
computeBackoff.mockClear();
|
||||
sleepWithAbort.mockClear();
|
||||
startTelegramWebhookSpy.mockClear();
|
||||
registerUnhandledRejectionHandlerMock.mockClear();
|
||||
resetUnhandledRejection();
|
||||
});
|
||||
|
||||
it("processes a DM and sends reply", async () => {
|
||||
@@ -201,6 +227,41 @@ describe("monitorTelegramProvider (grammY)", () => {
|
||||
await expect(monitorTelegramProvider({ token: "tok" })).rejects.toThrow("bad token");
|
||||
});
|
||||
|
||||
it("force-restarts polling when unhandled network rejection stalls runner", async () => {
|
||||
let running = true;
|
||||
let releaseTask: (() => void) | undefined;
|
||||
const stop = vi.fn(async () => {
|
||||
running = false;
|
||||
releaseTask?.();
|
||||
});
|
||||
|
||||
runSpy
|
||||
.mockImplementationOnce(() => ({
|
||||
task: () =>
|
||||
new Promise<void>((resolve) => {
|
||||
releaseTask = resolve;
|
||||
}),
|
||||
stop,
|
||||
isRunning: () => running,
|
||||
}))
|
||||
.mockImplementationOnce(() => ({
|
||||
task: () => Promise.resolve(),
|
||||
stop: vi.fn(),
|
||||
isRunning: () => false,
|
||||
}));
|
||||
|
||||
const monitor = monitorTelegramProvider({ token: "tok" });
|
||||
await vi.waitFor(() => expect(runSpy).toHaveBeenCalledTimes(1));
|
||||
|
||||
expect(emitUnhandledRejection(new TypeError("fetch failed"))).toBe(true);
|
||||
await monitor;
|
||||
|
||||
expect(stop).toHaveBeenCalledTimes(1);
|
||||
expect(computeBackoff).toHaveBeenCalled();
|
||||
expect(sleepWithAbort).toHaveBeenCalled();
|
||||
expect(runSpy).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("passes configured webhookHost to webhook listener", async () => {
|
||||
await monitorTelegramProvider({
|
||||
token: "tok",
|
||||
|
||||
Reference in New Issue
Block a user