Stage 3 of the int-suite speedup (gate G2 showed Vitest 4's pool scheduler only reuses worker processes between isolate:false tasks, so per-file fork spawning made clone+boot costs unavoidable at stage 2): - config: workers branch runs pool=forks with isolate:false, cap 8 via VITEST_MAX_FORKS, node default env (jsdom docblocks on DOM specs), failure-marker reporter, and idempotent setupFiles chain - vitest-worker-setup: one template clone PER FORK PROCESS named by pid (VITEST_WORKER_ID churns per task, which silently re-cloned per file); DATABASE_URI repoint + TEST_PG_POOL_MAX=8 (measured hook deadlocks at 3) - vitest.setup: once-per-process serial test URI markers; afterEach useRealTimers guard (restoreAllMocks omitted: it resurrects vi.mock module factories mid-file); logging absorber for fire-and-forget event writes that reject after their spec's cleanup deleted fixture users - helpers/get-test-payload.ts: memoized Payload boot shared across files in a fork; codemod converted all 91 self-booting specs mechanically - pinned opt-outs (own boot + comments): game-structures-labor-route, mission-reminder, notification-inbox-scope (jwtSign writes leak into the shared instance's auth state across files) - promotion-nominations: create the superuser before its rank ladder so the Users-hook Recruit rank gets leading orderable tokens (its top-of-ladder guard needs Col to remain ladder-max on fresh clones); operation-economy: itemType-scope the market-state assertion (asset and resource ids collide as bare integers under per-fork databases) Gate G3: 5 consecutive full green runs (149/149 files), warm wall 53-65s vs 598s baseline (~10x), zero clone residue after green runs. Vitest 4 note: globalTeardown config no longer exists; teardown returns from globalSetup.
56 lines
2.7 KiB
TypeScript
56 lines
2.7 KiB
TypeScript
import { cloneTemplateInto, workerDatabaseUri } from "./test-db";
|
|
|
|
/**
|
|
* Per-fork vitest setup, appended AFTER vitest.setup.ts in setupFiles. Gives
|
|
* every fork its own clone of the template database (`<base>_w<ID>`),
|
|
* repoints DATABASE_URI at it, and caps the pg pool per Payload boot so N
|
|
* forks stay far below Postgres' max_connections.
|
|
*
|
|
* No-ops outside workers mode: with `TEST_DATABASE_URI` set the suite runs in
|
|
* legacy serial mode against the single override db (bisect hatch), and a
|
|
* missing VITEST_WORKER_ID means this is not a vitest worker process.
|
|
*
|
|
* NOTE: vitest imports setupFiles but never calls their default export, so
|
|
* the work happens in a top-level await below. The vite-node SSR transform
|
|
* hoists top-level await into an async wrapper, so side effects MUST live
|
|
* inside the awaited function, not before it.
|
|
*
|
|
* The clone happens ONCE PER FORK, not per test file: setup files re-run
|
|
* before every test file (even under isolate:false), but process.env survives
|
|
* those module resets, so the marker env var short-circuits repeats. Within a
|
|
* fork, files keep running sequentially against one database that started
|
|
* empty plus the seeded baseline, exactly mirroring the old cross-file
|
|
* semantics while two forks never share tables.
|
|
*/
|
|
const CLONED_MARKER_ENV = "PTF_WORKER_DB_CLONED";
|
|
|
|
async function attachWorkerDatabase(): Promise<void> {
|
|
if (process.env.TEST_DATABASE_URI || !process.env.VITEST_WORKER_ID) return;
|
|
|
|
// Clone identity is the FORK PROCESS (pid), not VITEST_WORKER_ID: Vitest
|
|
// increments that env var per task even when it REUSES a fork process, so
|
|
// naming off it re-cloned once per test file. The pid is stable for the
|
|
// process lifetime, so exactly one clone amortizes across every file the
|
|
// scheduler runs on this fork - which is also what keeps the memoized
|
|
// getTestPayload() instance, booted against this URI, honest.
|
|
const serialUri =
|
|
process.env.PTF_SERIAL_TEST_DATABASE_URI ??
|
|
(() => {
|
|
throw new Error("PTF_SERIAL_TEST_DATABASE_URI missing - vitest.setup.ts must run first");
|
|
})();
|
|
const uri = workerDatabaseUri(process.pid, serialUri);
|
|
process.env.DATABASE_URI = uri;
|
|
if (process.env[CLONED_MARKER_ENV] === uri) return;
|
|
|
|
await cloneTemplateInto(uri);
|
|
process.env[CLONED_MARKER_ENV] = uri;
|
|
if (!process.env.TEST_PG_POOL_MAX) {
|
|
// Reads as pg PoolConfig.max in src/payload.config.ts (unset elsewhere).
|
|
// Cap 8: transaction-fan-out specs stall the payload helper hooks when
|
|
// the pool is tight (measured deadlocks at 3); 8 forks x 8 still fits
|
|
// under Postgres max_connections=100 with headroom for dev/prd idles.
|
|
process.env.TEST_PG_POOL_MAX = "8";
|
|
}
|
|
}
|
|
|
|
await attachWorkerDatabase();
|