perf(test): reduce lock wait and fixture setup overhead

This commit is contained in:
Peter Steinberger
2026-02-14 01:42:42 +00:00
parent 0c00dd92a4
commit db8cabedde
3 changed files with 69 additions and 66 deletions

View File

@@ -50,9 +50,8 @@ describe("session store lock (Promise chain mutex)", () => {
Array.from({ length: N }, (_, i) =>
updateSessionStore(storePath, async (store) => {
const entry = store[key] as Record<string, unknown>;
// Simulate async work so that without proper serialization
// multiple readers would see the same stale value.
await sleep(Math.random() * 3);
// Keep an async boundary so stale-read races would surface without serialization.
await Promise.resolve();
entry.counter = (entry.counter as number) + 1;
entry.tag = `writer-${i}`;
}),
@@ -74,7 +73,7 @@ describe("session store lock (Promise chain mutex)", () => {
storePath,
sessionKey: key,
update: async () => {
await sleep(9);
await Promise.resolve();
return { modelOverride: "model-a" };
},
}),
@@ -82,7 +81,7 @@ describe("session store lock (Promise chain mutex)", () => {
storePath,
sessionKey: key,
update: async () => {
await sleep(3);
await Promise.resolve();
return { thinkingLevel: "high" as const };
},
}),
@@ -90,7 +89,7 @@ describe("session store lock (Promise chain mutex)", () => {
storePath,
sessionKey: key,
update: async () => {
await sleep(6);
await Promise.resolve();
return { systemPromptOverride: "custom" };
},
}),
@@ -165,17 +164,30 @@ describe("session store lock (Promise chain mutex)", () => {
});
const order: string[] = [];
let started = 0;
let releaseBoth: (() => void) | undefined;
const gate = new Promise<void>((resolve) => {
releaseBoth = resolve;
});
const markStarted = () => {
started += 1;
if (started === 2) {
releaseBoth?.();
}
};
const opA = updateSessionStore(pathA, async (store) => {
order.push("a-start");
await sleep(12);
markStarted();
await gate;
store.a = { ...store.a, modelOverride: "done-a" } as unknown as SessionEntry;
order.push("a-end");
});
const opB = updateSessionStore(pathB, async (store) => {
order.push("b-start");
await sleep(3);
markStarted();
await gate;
store.b = { ...store.b, modelOverride: "done-b" } as unknown as SessionEntry;
order.push("b-end");
});
@@ -211,7 +223,7 @@ describe("session store lock (Promise chain mutex)", () => {
});
// Allow microtask (finally) to run.
await sleep(0);
await Promise.resolve();
expect(getSessionStoreLockQueueSizeForTest()).toBe(0);
});
@@ -223,7 +235,7 @@ describe("session store lock (Promise chain mutex)", () => {
throw new Error("fail");
}).catch(() => undefined);
await sleep(0);
await Promise.resolve();
expect(getSessionStoreLockQueueSizeForTest()).toBe(0);
});
@@ -266,21 +278,21 @@ describe("session store lock (Promise chain mutex)", () => {
const lockHolder = withSessionStoreLockForTest(
storePath,
async () => {
await sleep(40);
await sleep(15);
},
{ timeoutMs: 2_000 },
{ timeoutMs: 1_000 },
);
const timedOut = withSessionStoreLockForTest(
storePath,
async () => {
timedOutRan = true;
},
{ timeoutMs: 20 },
{ timeoutMs: 5 },
);
await expect(timedOut).rejects.toThrow("timeout waiting for session store lock");
await lockHolder;
await sleep(8);
await sleep(2);
expect(timedOutRan).toBe(false);
});
@@ -291,7 +303,7 @@ describe("session store lock (Promise chain mutex)", () => {
});
const write = updateSessionStore(storePath, async (store) => {
await sleep(18);
await sleep(8);
store[key] = { ...store[key], modelOverride: "v" } as unknown as SessionEntry;
});
@@ -303,7 +315,7 @@ describe("session store lock (Promise chain mutex)", () => {
lockSeen = true;
break;
} catch {
await sleep(2);
await sleep(1);
}
}
expect(lockSeen).toBe(true);