mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 16:34:33 +00:00
perf(test): remove polling loop from announce queue tests
This commit is contained in:
@@ -1,15 +1,28 @@
|
|||||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||||
import { enqueueAnnounce, resetAnnounceQueuesForTests } from "./subagent-announce-queue.js";
|
import { enqueueAnnounce, resetAnnounceQueuesForTests } from "./subagent-announce-queue.js";
|
||||||
|
|
||||||
async function waitFor(predicate: () => boolean, timeoutMs = 2_000): Promise<void> {
|
function createRetryingSend() {
|
||||||
const startedAt = Date.now();
|
const prompts: string[] = [];
|
||||||
while (Date.now() - startedAt < timeoutMs) {
|
let attempts = 0;
|
||||||
if (predicate()) {
|
let resolved = false;
|
||||||
return;
|
let resolveSecondAttempt = () => {};
|
||||||
|
const waitForSecondAttempt = new Promise<void>((resolve) => {
|
||||||
|
resolveSecondAttempt = resolve;
|
||||||
|
});
|
||||||
|
|
||||||
|
const send = vi.fn(async (item: { prompt: string }) => {
|
||||||
|
attempts += 1;
|
||||||
|
prompts.push(item.prompt);
|
||||||
|
if (attempts >= 2 && !resolved) {
|
||||||
|
resolved = true;
|
||||||
|
resolveSecondAttempt();
|
||||||
}
|
}
|
||||||
await new Promise((resolve) => setTimeout(resolve, 10));
|
if (attempts === 1) {
|
||||||
}
|
throw new Error("gateway timeout after 60000ms");
|
||||||
throw new Error("timed out waiting for condition");
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return { send, prompts, waitForSecondAttempt };
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("subagent-announce-queue", () => {
|
describe("subagent-announce-queue", () => {
|
||||||
@@ -18,15 +31,7 @@ describe("subagent-announce-queue", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("retries failed sends without dropping queued announce items", async () => {
|
it("retries failed sends without dropping queued announce items", async () => {
|
||||||
const sendPrompts: string[] = [];
|
const sender = createRetryingSend();
|
||||||
let attempts = 0;
|
|
||||||
const send = vi.fn(async (item: { prompt: string }) => {
|
|
||||||
attempts += 1;
|
|
||||||
sendPrompts.push(item.prompt);
|
|
||||||
if (attempts === 1) {
|
|
||||||
throw new Error("gateway timeout after 60000ms");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
enqueueAnnounce({
|
enqueueAnnounce({
|
||||||
key: "announce:test:retry",
|
key: "announce:test:retry",
|
||||||
@@ -36,24 +41,16 @@ describe("subagent-announce-queue", () => {
|
|||||||
sessionKey: "agent:main:telegram:dm:u1",
|
sessionKey: "agent:main:telegram:dm:u1",
|
||||||
},
|
},
|
||||||
settings: { mode: "followup", debounceMs: 0 },
|
settings: { mode: "followup", debounceMs: 0 },
|
||||||
send,
|
send: sender.send,
|
||||||
});
|
});
|
||||||
|
|
||||||
await waitFor(() => attempts >= 2);
|
await sender.waitForSecondAttempt;
|
||||||
expect(send).toHaveBeenCalledTimes(2);
|
expect(sender.send).toHaveBeenCalledTimes(2);
|
||||||
expect(sendPrompts).toEqual(["subagent completed", "subagent completed"]);
|
expect(sender.prompts).toEqual(["subagent completed", "subagent completed"]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("preserves queue summary state across failed summary delivery retries", async () => {
|
it("preserves queue summary state across failed summary delivery retries", async () => {
|
||||||
const sendPrompts: string[] = [];
|
const sender = createRetryingSend();
|
||||||
let attempts = 0;
|
|
||||||
const send = vi.fn(async (item: { prompt: string }) => {
|
|
||||||
attempts += 1;
|
|
||||||
sendPrompts.push(item.prompt);
|
|
||||||
if (attempts === 1) {
|
|
||||||
throw new Error("gateway timeout after 60000ms");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
enqueueAnnounce({
|
enqueueAnnounce({
|
||||||
key: "announce:test:summary-retry",
|
key: "announce:test:summary-retry",
|
||||||
@@ -64,7 +61,7 @@ describe("subagent-announce-queue", () => {
|
|||||||
sessionKey: "agent:main:telegram:dm:u1",
|
sessionKey: "agent:main:telegram:dm:u1",
|
||||||
},
|
},
|
||||||
settings: { mode: "followup", debounceMs: 0, cap: 1, dropPolicy: "summarize" },
|
settings: { mode: "followup", debounceMs: 0, cap: 1, dropPolicy: "summarize" },
|
||||||
send,
|
send: sender.send,
|
||||||
});
|
});
|
||||||
enqueueAnnounce({
|
enqueueAnnounce({
|
||||||
key: "announce:test:summary-retry",
|
key: "announce:test:summary-retry",
|
||||||
@@ -75,25 +72,17 @@ describe("subagent-announce-queue", () => {
|
|||||||
sessionKey: "agent:main:telegram:dm:u1",
|
sessionKey: "agent:main:telegram:dm:u1",
|
||||||
},
|
},
|
||||||
settings: { mode: "followup", debounceMs: 0, cap: 1, dropPolicy: "summarize" },
|
settings: { mode: "followup", debounceMs: 0, cap: 1, dropPolicy: "summarize" },
|
||||||
send,
|
send: sender.send,
|
||||||
});
|
});
|
||||||
|
|
||||||
await waitFor(() => attempts >= 2);
|
await sender.waitForSecondAttempt;
|
||||||
expect(send).toHaveBeenCalledTimes(2);
|
expect(sender.send).toHaveBeenCalledTimes(2);
|
||||||
expect(sendPrompts[0]).toContain("[Queue overflow]");
|
expect(sender.prompts[0]).toContain("[Queue overflow]");
|
||||||
expect(sendPrompts[1]).toContain("[Queue overflow]");
|
expect(sender.prompts[1]).toContain("[Queue overflow]");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("retries collect-mode batches without losing queued items", async () => {
|
it("retries collect-mode batches without losing queued items", async () => {
|
||||||
const sendPrompts: string[] = [];
|
const sender = createRetryingSend();
|
||||||
let attempts = 0;
|
|
||||||
const send = vi.fn(async (item: { prompt: string }) => {
|
|
||||||
attempts += 1;
|
|
||||||
sendPrompts.push(item.prompt);
|
|
||||||
if (attempts === 1) {
|
|
||||||
throw new Error("gateway timeout after 60000ms");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
enqueueAnnounce({
|
enqueueAnnounce({
|
||||||
key: "announce:test:collect-retry",
|
key: "announce:test:collect-retry",
|
||||||
@@ -103,7 +92,7 @@ describe("subagent-announce-queue", () => {
|
|||||||
sessionKey: "agent:main:telegram:dm:u1",
|
sessionKey: "agent:main:telegram:dm:u1",
|
||||||
},
|
},
|
||||||
settings: { mode: "collect", debounceMs: 0 },
|
settings: { mode: "collect", debounceMs: 0 },
|
||||||
send,
|
send: sender.send,
|
||||||
});
|
});
|
||||||
enqueueAnnounce({
|
enqueueAnnounce({
|
||||||
key: "announce:test:collect-retry",
|
key: "announce:test:collect-retry",
|
||||||
@@ -113,18 +102,18 @@ describe("subagent-announce-queue", () => {
|
|||||||
sessionKey: "agent:main:telegram:dm:u1",
|
sessionKey: "agent:main:telegram:dm:u1",
|
||||||
},
|
},
|
||||||
settings: { mode: "collect", debounceMs: 0 },
|
settings: { mode: "collect", debounceMs: 0 },
|
||||||
send,
|
send: sender.send,
|
||||||
});
|
});
|
||||||
|
|
||||||
await waitFor(() => attempts >= 2);
|
await sender.waitForSecondAttempt;
|
||||||
expect(send).toHaveBeenCalledTimes(2);
|
expect(sender.send).toHaveBeenCalledTimes(2);
|
||||||
expect(sendPrompts[0]).toContain("Queued #1");
|
expect(sender.prompts[0]).toContain("Queued #1");
|
||||||
expect(sendPrompts[0]).toContain("queued item one");
|
expect(sender.prompts[0]).toContain("queued item one");
|
||||||
expect(sendPrompts[0]).toContain("Queued #2");
|
expect(sender.prompts[0]).toContain("Queued #2");
|
||||||
expect(sendPrompts[0]).toContain("queued item two");
|
expect(sender.prompts[0]).toContain("queued item two");
|
||||||
expect(sendPrompts[1]).toContain("Queued #1");
|
expect(sender.prompts[1]).toContain("Queued #1");
|
||||||
expect(sendPrompts[1]).toContain("queued item one");
|
expect(sender.prompts[1]).toContain("queued item one");
|
||||||
expect(sendPrompts[1]).toContain("Queued #2");
|
expect(sender.prompts[1]).toContain("Queued #2");
|
||||||
expect(sendPrompts[1]).toContain("queued item two");
|
expect(sender.prompts[1]).toContain("queued item two");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user