From cda1b008203d841c7de671d9ec34769b87809419 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Sun, 22 Nov 2026 22:53:11 +0000 Subject: [PATCH] test(studio-server): a slow runner no longer leaves index.html's first copy owed in step-ahead tests (#8540) --- .../src/history/__fixtures__/stepThenDie.ts | 3 ++- .../src/history/projectHistory.ts | 26 +++++++++++++------ .../src/history/stepAheadOfCopy.test.ts | 3 ++- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/packages/studio-server/src/history/__fixtures__/stepThenDie.ts b/packages/studio-server/src/history/__fixtures__/stepThenDie.ts index fc2c1b9c7e..680b4f377a 100644 --- a/packages/studio-server/src/history/__fixtures__/stepThenDie.ts +++ b/packages/studio-server/src/history/__fixtures__/stepThenDie.ts @@ -23,10 +23,11 @@ fs.renameSync = ((from: fs.PathLike, to: fs.PathLike) => { syncBuiltinESMExports(); const { openProjectHistory } = await import("../projectHistory.js"); +const onlyHeldFilesOwed = (path: string) => (path === "index.html" ? 60_000 : 100); const history = await openProjectHistory({ projectDir: projectDir!, historyRoot: historyRoot!, - firstCopyFileMs: 100, + firstCopyFileMs: onlyHeldFilesOwed, now: () => Date.now() + 60_000, ...(closedWindow && { closedWindow: JSON.parse(closedWindow) }), }); diff --git a/packages/studio-server/src/history/projectHistory.ts b/packages/studio-server/src/history/projectHistory.ts index 92c857c8b1..6e402dcb8f 100644 --- a/packages/studio-server/src/history/projectHistory.ts +++ b/packages/studio-server/src/history/projectHistory.ts @@ -95,8 +95,8 @@ export interface ProjectHistoryOptions { onError?: (error: unknown) => void; /** How long an open waits for another process to close the same history (default 5 s). */ ownerWaitMs?: number; - /** How long the first copy waits on one file before leaving it owed (default 10 s). */ - firstCopyFileMs?: number; + /** How long the first copy waits on one file before leaving it owed (default 10 s), or that wait per file. */ + firstCopyFileMs?: number | ((path: string) => number); ownerUrl?: string; /** A CLI turn's window from an earlier open: writes since, within its idle limit, become the entry with its id. */ closedWindow?: ClosedWindow; @@ -818,9 +818,8 @@ class Engine { const sweptAt = this.now(); const files = await this.historyFiles(); const copied = files.filter((file) => file.size <= COPY_LATER_ABOVE_BYTES); - const waitMs = this.options.firstCopyFileMs ?? FIRST_COPY_FILE_MS; const copies = copied.map((file) => - firstCopyOrOwed(() => this.storeIfPresent(file.path, true), waitMs), + firstCopyOrOwed(() => this.storeIfPresent(file.path, true), this.firstCopyWaitMs(file.path)), ); const hashes = await Promise.allSettled(copies.map((copy) => copy.result)); const late = new Map>(); @@ -907,7 +906,6 @@ class Engine { sweptAt: number, lateFirstCopies = new Map>(), ): void { - const waitMs = this.options.firstCopyFileMs ?? FIRST_COPY_FILE_MS; let turn: Promise = Promise.resolve(); let stalledAhead = Promise.resolve(false); for (const file of files) { @@ -916,13 +914,20 @@ class Engine { const landed = adoption.firstTry.then(() => false); const stepMayBegin = late ? Promise.resolve(true) - : stalledAhead.then((stalled) => stalled || orAfter(landed, waitMs, true)); + : stalledAhead.then( + (stalled) => stalled || orAfter(landed, this.firstCopyWaitMs(file.path), true), + ); if (!late) [turn, stalledAhead] = [adoption.firstTry, stepMayBegin]; this.adopting.set(file.path, { ...adoption, stepMayBegin }); } this.saveAdopting(); } + firstCopyWaitMs(path: string): number { + const wait = this.options.firstCopyFileMs; + return typeof wait === "function" ? wait(path) : (wait ?? FIRST_COPY_FILE_MS); + } + saveAdopting(): void { this.saveJson("adopting.json", this.adopting.size ? [...this.adopting.keys()] : null); } @@ -1970,10 +1975,15 @@ class Engine { }, unsavedBlobBytes: () => this.unsavedBlobBytes(), claim: async (who, label, paths, options = {}) => { - const waitMs = this.options.firstCopyFileMs ?? FIRST_COPY_FILE_MS; const owed = (path: string) => this.adopting.get(this.logPath(path)); await Promise.all( - paths.map((path) => orAfter(Promise.resolve(owed(path)?.retryNow()), waitMs, undefined)), + paths.map((path) => + orAfter( + Promise.resolve(owed(path)?.retryNow()), + this.firstCopyWaitMs(this.logPath(path)), + undefined, + ), + ), ); const pending = paths.filter(owed).map((path) => this.logPath(path)); return this.queue(() => this.claimNow(who, label, paths, options, pending)); diff --git a/packages/studio-server/src/history/stepAheadOfCopy.test.ts b/packages/studio-server/src/history/stepAheadOfCopy.test.ts index a137906e65..4a68a142e2 100644 --- a/packages/studio-server/src/history/stepAheadOfCopy.test.ts +++ b/packages/studio-server/src/history/stepAheadOfCopy.test.ts @@ -72,7 +72,8 @@ function project() { const opts = { projectDir: dir, historyRoot, - firstCopyFileMs: 100, + // No test holds index.html: its first copy always lands, however slow the runner; held files are owed at 100 ms. + firstCopyFileMs: (path: string) => (path === "index.html" ? 60_000 : 100), now: () => Date.now() + ahead, }; // History trusts a stat it has seen unchanged for longer than a file system's time step.