perf(test): drop polling waits in qmd manager suite

This commit is contained in:
Peter Steinberger
2026-02-15 00:11:47 +00:00
parent ed2ae5886d
commit ae1214140e

View File

@@ -227,11 +227,13 @@ describe("QmdMemoryManager", () => {
}, },
} as OpenClawConfig; } as OpenClawConfig;
const updateSpawned = createDeferred<void>();
let releaseUpdate: (() => void) | null = null; let releaseUpdate: (() => void) | null = null;
spawnMock.mockImplementation((_cmd: string, args: string[]) => { spawnMock.mockImplementation((_cmd: string, args: string[]) => {
if (args[0] === "update") { if (args[0] === "update") {
const child = createMockChild({ autoClose: false }); const child = createMockChild({ autoClose: false });
releaseUpdate = () => child.closeWith(0); releaseUpdate = () => child.closeWith(0);
updateSpawned.resolve();
return child; return child;
} }
return createMockChild(); return createMockChild();
@@ -239,7 +241,7 @@ describe("QmdMemoryManager", () => {
const resolved = resolveMemoryBackendConfig({ cfg, agentId }); const resolved = resolveMemoryBackendConfig({ cfg, agentId });
const createPromise = QmdMemoryManager.create({ cfg, agentId, resolved }); const createPromise = QmdMemoryManager.create({ cfg, agentId, resolved });
await waitForCondition(() => releaseUpdate !== null, 400); await updateSpawned.promise;
let created = false; let created = false;
void createPromise.then(() => { void createPromise.then(() => {
created = true; created = true;
@@ -446,6 +448,7 @@ describe("QmdMemoryManager", () => {
}, },
} as OpenClawConfig; } as OpenClawConfig;
const firstUpdateSpawned = createDeferred<void>();
let updateCalls = 0; let updateCalls = 0;
let releaseFirstUpdate: (() => void) | null = null; let releaseFirstUpdate: (() => void) | null = null;
spawnMock.mockImplementation((_cmd: string, args: string[]) => { spawnMock.mockImplementation((_cmd: string, args: string[]) => {
@@ -454,6 +457,7 @@ describe("QmdMemoryManager", () => {
if (updateCalls === 1) { if (updateCalls === 1) {
const first = createMockChild({ autoClose: false }); const first = createMockChild({ autoClose: false });
releaseFirstUpdate = () => first.closeWith(0); releaseFirstUpdate = () => first.closeWith(0);
firstUpdateSpawned.resolve();
return first; return first;
} }
return createMockChild(); return createMockChild();
@@ -471,7 +475,7 @@ describe("QmdMemoryManager", () => {
const inFlight = manager.sync({ reason: "interval" }); const inFlight = manager.sync({ reason: "interval" });
const forced = manager.sync({ reason: "manual", force: true }); const forced = manager.sync({ reason: "manual", force: true });
await waitForCondition(() => updateCalls >= 1, 80); await firstUpdateSpawned.promise;
expect(updateCalls).toBe(1); expect(updateCalls).toBe(1);
if (!releaseFirstUpdate) { if (!releaseFirstUpdate) {
throw new Error("first update release missing"); throw new Error("first update release missing");
@@ -501,6 +505,8 @@ describe("QmdMemoryManager", () => {
}, },
} as OpenClawConfig; } as OpenClawConfig;
const firstUpdateSpawned = createDeferred<void>();
const secondUpdateSpawned = createDeferred<void>();
let updateCalls = 0; let updateCalls = 0;
let releaseFirstUpdate: (() => void) | null = null; let releaseFirstUpdate: (() => void) | null = null;
let releaseSecondUpdate: (() => void) | null = null; let releaseSecondUpdate: (() => void) | null = null;
@@ -510,11 +516,13 @@ describe("QmdMemoryManager", () => {
if (updateCalls === 1) { if (updateCalls === 1) {
const first = createMockChild({ autoClose: false }); const first = createMockChild({ autoClose: false });
releaseFirstUpdate = () => first.closeWith(0); releaseFirstUpdate = () => first.closeWith(0);
firstUpdateSpawned.resolve();
return first; return first;
} }
if (updateCalls === 2) { if (updateCalls === 2) {
const second = createMockChild({ autoClose: false }); const second = createMockChild({ autoClose: false });
releaseSecondUpdate = () => second.closeWith(0); releaseSecondUpdate = () => second.closeWith(0);
secondUpdateSpawned.resolve();
return second; return second;
} }
return createMockChild(); return createMockChild();
@@ -532,14 +540,14 @@ describe("QmdMemoryManager", () => {
const inFlight = manager.sync({ reason: "interval" }); const inFlight = manager.sync({ reason: "interval" });
const forcedOne = manager.sync({ reason: "manual", force: true }); const forcedOne = manager.sync({ reason: "manual", force: true });
await waitForCondition(() => updateCalls >= 1, 80); await firstUpdateSpawned.promise;
expect(updateCalls).toBe(1); expect(updateCalls).toBe(1);
if (!releaseFirstUpdate) { if (!releaseFirstUpdate) {
throw new Error("first update release missing"); throw new Error("first update release missing");
} }
releaseFirstUpdate(); releaseFirstUpdate();
await waitForCondition(() => updateCalls >= 2, 120); await secondUpdateSpawned.promise;
const forcedTwo = manager.sync({ reason: "manual-again", force: true }); const forcedTwo = manager.sync({ reason: "manual-again", force: true });
if (!releaseSecondUpdate) { if (!releaseSecondUpdate) {
@@ -1211,17 +1219,12 @@ describe("QmdMemoryManager", () => {
}); });
}); });
async function waitForCondition(check: () => boolean, timeoutMs: number): Promise<void> { function createDeferred<T>() {
// Tests only need to yield the event loop a few times; real-time sleeps slow the suite down. let resolve!: (value: T) => void;
const maxTicks = Math.max(10, Math.min(5000, timeoutMs * 5)); let reject!: (reason?: unknown) => void;
for (let tick = 0; tick < maxTicks; tick += 1) { const promise = new Promise<T>((res, rej) => {
if (check()) { resolve = res;
return; reject = rej;
} });
await new Promise<void>((resolve) => setImmediate(resolve)); return { promise, resolve, reject };
}
if (check()) {
return;
}
throw new Error("condition was not met in time");
} }