perf: speed up test parallelism

This commit is contained in:
Peter Steinberger
2026-03-26 20:08:49 +00:00
parent 2fc017788c
commit 663ba5a3cd
13 changed files with 1517 additions and 99 deletions

View File

@@ -0,0 +1,58 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import {
getSessionStoreLockQueueSizeForTest,
withSessionStoreLockForTest,
} from "../config/sessions/store.js";
import { cleanupSessionStateForTest } from "./session-state-cleanup.js";
function createDeferred<T>() {
let resolve!: (value: T | PromiseLike<T>) => void;
let reject!: (reason?: unknown) => void;
const promise = new Promise<T>((nextResolve, nextReject) => {
resolve = nextResolve;
reject = nextReject;
});
return { promise, resolve, reject };
}
describe("cleanupSessionStateForTest", () => {
afterEach(async () => {
await cleanupSessionStateForTest();
});
it("waits for in-flight session store locks before clearing test state", async () => {
const fixtureRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-session-cleanup-"));
const storePath = path.join(fixtureRoot, "openclaw-sessions.json");
const started = createDeferred<void>();
const release = createDeferred<void>();
try {
const running = withSessionStoreLockForTest(storePath, async () => {
started.resolve();
await release.promise;
});
await started.promise;
expect(getSessionStoreLockQueueSizeForTest()).toBe(1);
let settled = false;
const cleanupPromise = cleanupSessionStateForTest().then(() => {
settled = true;
});
await new Promise((resolve) => setTimeout(resolve, 25));
expect(settled).toBe(false);
release.resolve();
await running;
await cleanupPromise;
expect(getSessionStoreLockQueueSizeForTest()).toBe(0);
} finally {
release.resolve();
await fs.rm(fixtureRoot, { recursive: true, force: true });
}
});
});

View File

@@ -1,8 +1,12 @@
import { drainSessionWriteLockStateForTest } from "../agents/session-write-lock.js";
import { clearSessionStoreCacheForTest } from "../config/sessions/store.js";
import {
clearSessionStoreCacheForTest,
drainSessionStoreLockQueuesForTest,
} from "../config/sessions/store.js";
import { drainFileLockStateForTest } from "../infra/file-lock.js";
export async function cleanupSessionStateForTest(): Promise<void> {
await drainSessionStoreLockQueuesForTest();
clearSessionStoreCacheForTest();
await drainFileLockStateForTest();
await drainSessionWriteLockStateForTest();