Skip to content

Commit 223a0a2

Browse files
authored
test(windows): close the four residual shard-2 Windows failures (#2975)
* test(service): make retargeted shim fixture cross-platform * test(init): synchronize EOF check with first prompt * test(storage): handshake after restore file moves * test(storage): wait for cleanup slot acquisition
1 parent 4157593 commit 223a0a2

5 files changed

Lines changed: 127 additions & 32 deletions

File tree

‎src/storage/cleanup.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2578,6 +2578,11 @@ export interface RestoreTestHooks {
25782578
* reconcile, so cleanup can race an in-flight restore.
25792579
*/
25802580
holdAfterFileMovesMs?: number;
2581+
/**
2582+
* Test-only: publish a ready file after rollout moves, then wait until the
2583+
* release file exists. This makes cross-thread race tests phase-driven.
2584+
*/
2585+
pauseAfterFileMoves?: { readyPath: string; releasePath: string };
25812586
}
25822587

25832588
/**
@@ -2917,6 +2922,11 @@ export function restoreTrashEntry(
29172922
return { ok: false, trashDir: id, ...partialCounts, error };
29182923
};
29192924

2925+
if (hooks?.pauseAfterFileMoves) {
2926+
writeFileSync(hooks.pauseAfterFileMoves.readyPath, "ready\n");
2927+
while (!existsSync(hooks.pauseAfterFileMoves.releasePath)) Bun.sleepSync(10);
2928+
}
2929+
29202930
if (hooks?.holdAfterFileMovesMs !== undefined) {
29212931
const holdMs = Math.max(0, Math.floor(hooks.holdAfterFileMovesMs));
29222932
if (holdMs > 0) {

‎src/storage/storage-mutation-coordinator.ts‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ export type StorageMutationBusyError = "storage_mutation_busy";
1616
export interface StorageMutationCoordinatorTestHooks {
1717
/** Block after acquiring the slot, before mutation work (race tests). */
1818
blockMs?: number;
19+
/** Test-only cross-thread handshake after one mutation kind acquires its slot. */
20+
pauseAfterAcquire?: {
21+
kind: StorageMutationKind;
22+
readyPath: string;
23+
releasePath: string;
24+
};
1925
}
2026

2127
interface ActiveSlot {
@@ -93,7 +99,12 @@ export function endStorageMutation(codexHome?: string): void {
9399
slot.lease.release();
94100
}
95101

96-
async function applyCoordinatorBlock(): Promise<void> {
102+
async function applyCoordinatorBlock(kind: StorageMutationKind): Promise<void> {
103+
const pause = testHooks?.pauseAfterAcquire;
104+
if (pause?.kind === kind) {
105+
await Bun.write(pause.readyPath, "ready\n");
106+
while (!Bun.file(pause.releasePath).size) await Bun.sleep(10);
107+
}
97108
const blockMs = testHooks?.blockMs;
98109
if (typeof blockMs === "number" && Number.isFinite(blockMs) && blockMs > 0) {
99110
await Bun.sleep(Math.floor(blockMs));
@@ -114,7 +125,7 @@ export async function runPolicyStorageMutation<T>(
114125
return { ok: false, error: "storage_mutation_busy" };
115126
}
116127
try {
117-
await applyCoordinatorBlock();
128+
await applyCoordinatorBlock("policy");
118129
return await work();
119130
} finally {
120131
gate.lease.release();
@@ -131,7 +142,7 @@ export async function withStorageMutationSlot<T>(
131142
return { ok: false, error: "storage_mutation_busy" };
132143
}
133144
try {
134-
await applyCoordinatorBlock();
145+
await applyCoordinatorBlock(kind);
135146
return await work();
136147
} finally {
137148
gate.lease.release();

‎tests/init-eof.test.ts‎

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,24 @@ import { existsSync, mkdtempSync, rmSync } from "node:fs";
33
import { tmpdir } from "node:os";
44
import { join } from "node:path";
55

6+
async function waitForOutput(
7+
stream: ReadableStream<Uint8Array>,
8+
expected: string,
9+
): Promise<void> {
10+
const reader = stream.getReader();
11+
const decoder = new TextDecoder();
12+
let output = "";
13+
try {
14+
while (!output.includes(expected)) {
15+
const { value, done } = await reader.read();
16+
if (done) throw new Error(`init exited before writing ${JSON.stringify(expected)}`);
17+
output += decoder.decode(value, { stream: true });
18+
}
19+
} finally {
20+
reader.releaseLock();
21+
}
22+
}
23+
624
describe("ocx init piped stdin (#754)", () => {
725
const dirs: string[] = [];
826
afterEach(() => {
@@ -20,14 +38,20 @@ describe("ocx init piped stdin (#754)", () => {
2038
stdout: "pipe",
2139
stderr: "pipe",
2240
});
23-
proc.stdin.end();
24-
const exit = await Promise.race([
25-
proc.exited,
26-
new Promise<number>((_, reject) => setTimeout(() => reject(new Error("init did not exit after stdin EOF")), 8_000)),
27-
]);
28-
expect(exit).toBe(1);
29-
const stderr = await new Response(proc.stderr).text();
30-
expect(stderr.toLowerCase()).toMatch(/stdin (closed|reached eof)/);
31-
expect(existsSync(join(home, "config.json"))).toBe(false);
32-
});
41+
const stderrPromise = new Response(proc.stderr).text();
42+
try {
43+
// Synchronize on the behavior under test, not Windows process startup/import time.
44+
// EOF now arrives while readline is waiting for the first answer.
45+
await waitForOutput(proc.stdout, "Select default provider (number):");
46+
proc.stdin.end();
47+
48+
expect(await proc.exited).toBe(1);
49+
const stderr = await stderrPromise;
50+
expect(stderr.toLowerCase()).toMatch(/stdin (closed|reached eof)/);
51+
expect(existsSync(join(home, "config.json"))).toBe(false);
52+
} finally {
53+
if (proc.exitCode === null) proc.kill();
54+
await proc.exited.catch(() => {});
55+
}
56+
}, 30_000);
3357
});

‎tests/service.test.ts‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, wri
33
import { execFileSync } from "node:child_process";
44
import { tmpdir } from "node:os";
55
import { delimiter, isAbsolute, join, posix, win32 } from "node:path";
6+
import { pathToFileURL } from "node:url";
67
import * as serviceModule from "../src/service";
78
import { saveConfig } from "../src/config";
89
import { windowsEnvIndirectBatchValue } from "../src/lib/win-paths";
@@ -956,15 +957,24 @@ describe("launchd service plist", () => {
956957
const shimDir = join(root, "shims");
957958
const v1 = join(root, "installs", "2.35.0 package's");
958959
const v2 = join(root, "installs", "2.36.0 package's");
959-
const quoteForSh = (value: string): string => `'${value.replaceAll("'", "'\"'\"'")}'`;
960960
mkdirSync(shimDir, { recursive: true });
961961
mkdirSync(v1, { recursive: true });
962962
mkdirSync(v2, { recursive: true });
963-
writeFileSync(join(v1, "ocx"), "#!/bin/sh\necho V1 \"$@\"\n", { mode: 0o755 });
964-
writeFileSync(join(v2, "ocx"), "#!/bin/sh\necho V2 \"$@\"\n", { mode: 0o755 });
963+
const v1Entry = join(v1, "ocx");
964+
const v2Entry = join(v2, "ocx");
965+
writeFileSync(v1Entry, 'console.log("V1", Bun.argv.slice(2).join(" "));\n');
966+
writeFileSync(v2Entry, 'console.log("V2", Bun.argv.slice(2).join(" "));\n');
965967

966968
const shim = join(shimDir, "ocx");
967-
writeFileSync(shim, `#!/bin/sh\nexec ${quoteForSh(join(v1, "ocx"))} "\$@"\n`, { mode: 0o755 });
969+
const retargetShim = (target: string): void => {
970+
writeFileSync(shim, `await import(${JSON.stringify(pathToFileURL(target).href)});\n`);
971+
};
972+
const runShim = (): string => execFileSync(
973+
process.execPath,
974+
[shim, "start", "--port", "1"],
975+
{ encoding: "utf8" },
976+
);
977+
retargetShim(v1Entry);
968978

969979
// stableLauncherEntry finds the shim lexically from PATH — not its versioned target.
970980
const found = buildUnit(resolvedProxyEnv({}), { launcher: shim });
@@ -977,13 +987,15 @@ describe("launchd service plist", () => {
977987
const windowsUnit = buildUnit(resolvedProxyEnv({}), { launcher: windowsShim });
978988
expectTextToContainPath(windowsUnit, windowsShim);
979989

980-
expect(execFileSync(shim, ["start", "--port", "1"], { encoding: "utf8" })).toContain("V1");
990+
// Exercise the retarget through Bun on every host. Directly executing the old
991+
// extensionless #!/bin/sh fixture was itself a POSIX-only assumption.
992+
expect(runShim()).toContain("V1");
981993

982994
// The upgrade: shim retargeted, old version removed.
983-
writeFileSync(shim, `#!/bin/sh\nexec ${quoteForSh(join(v2, "ocx"))} "\$@"\n`, { mode: 0o755 });
995+
retargetShim(v2Entry);
984996
rmSync(v1, { recursive: true, force: true });
985-
expect(existsSync(join(v1, "ocx"))).toBe(false);
986-
expect(execFileSync(shim, ["start", "--port", "1"], { encoding: "utf8" })).toContain("V2");
997+
expect(existsSync(v1Entry)).toBe(false);
998+
expect(runShim()).toContain("V2");
987999

9881000
rmSync(root, { recursive: true, force: true });
9891001
});

‎tests/storage-mutation-race.test.ts‎

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,18 @@ async function waitForPolicyJob(
144144
throw new Error("policy job did not finish");
145145
}
146146

147+
async function waitForCondition(
148+
description: string,
149+
condition: () => boolean,
150+
timeoutMs = 8_000,
151+
): Promise<void> {
152+
const deadline = Date.now() + timeoutMs;
153+
while (!condition()) {
154+
if (Date.now() >= deadline) throw new Error(`timed out waiting for ${description}`);
155+
await Bun.sleep(20);
156+
}
157+
}
158+
147159
/** Windows can keep SQLite/job handles briefly after stop; retry only transient cleanup codes. */
148160
function removeTree(path: string): void {
149161
let lastError: unknown;
@@ -313,13 +325,20 @@ describe("storage mutation coordinator", () => {
313325

314326
test("cleanup quarantine and permanent are rejected while restore holds slot after file moves", async () => {
315327
const home = isolatedCodexHome!.path;
316-
const holdMs = 2500;
328+
const movedReadyPath = join(testDir, "restore-files-moved.ready");
329+
const releaseRestorePath = join(testDir, "release-restore");
317330
setRestoreTrashJobTestHooks({
318-
restoreTest: { holdAfterFileMovesMs: holdMs },
331+
restoreTest: {
332+
pauseAfterFileMoves: {
333+
readyPath: movedReadyPath,
334+
releasePath: releaseRestorePath,
335+
},
336+
},
319337
});
320338
seedArchivedPair(home);
321339

322340
const server = startServer(0);
341+
let restorePromise: Promise<Response> | null = null;
323342
try {
324343
const preview = await previewDigest(server.url, 50);
325344
const cleanupRes = await fetch(new URL("/api/storage/cleanup", server.url), {
@@ -336,17 +355,17 @@ describe("storage mutation coordinator", () => {
336355
const remainingPreview = await previewDigest(server.url, 50);
337356
expect(remainingPreview.count).toBe(1);
338357

339-
const restorePromise = fetch(new URL("/api/storage/trash/restore", server.url), {
358+
restorePromise = fetch(new URL("/api/storage/trash/restore", server.url), {
340359
method: "POST",
341360
headers: { "content-type": "application/json" },
342361
body: JSON.stringify({ id: trashId }),
343362
});
344363

345364
const restoredPath = join(home, "archived_sessions", "rollout-old.jsonl");
346-
const movedDeadline = Date.now() + 8000;
347-
while (!existsSync(restoredPath) && Date.now() < movedDeadline) {
348-
await Bun.sleep(20);
349-
}
365+
await waitForCondition(
366+
"restore worker to finish file moves",
367+
() => existsSync(movedReadyPath),
368+
);
350369
expect(existsSync(restoredPath)).toBe(true);
351370
expect(existsSync(join(trashStage, "rollout-old.jsonl"))).toBe(false);
352371
expect(existsSync(join(trashStage, "restore-pending.json"))).toBe(true);
@@ -378,6 +397,7 @@ describe("storage mutation coordinator", () => {
378397
expect(permanentDuring.status).toBe(409);
379398
expect((await permanentDuring.json()).error).toBe("storage_mutation_busy");
380399

400+
writeFileSync(releaseRestorePath, "release\n");
381401
const restoreRes = await restorePromise;
382402
expect(restoreRes.status).toBe(200);
383403
const restored = await restoreRes.json();
@@ -386,26 +406,41 @@ describe("storage mutation coordinator", () => {
386406
expect(threadCount(home)).toBe(2);
387407
expect(readFileSync(restoredPath, "utf8")).toBe("o".repeat(100));
388408
} finally {
409+
// Never strand the Worker if an assertion above fails; also consume the
410+
// request so its rejection cannot leak into the next isolated test.
411+
writeFileSync(releaseRestorePath, "release\n");
412+
if (restorePromise) await restorePromise.catch(() => undefined);
389413
await stopRaceServer(server);
390414
}
391415
}, { timeout: 45_000 });
392416

393417
test("restore is rejected while cleanup holds the shared mutation slot", async () => {
394418
const home = isolatedCodexHome!.path;
395-
const blockMs = 1200;
396-
setArchivedCleanupJobTestHooks({ blockMs });
419+
const cleanupReadyPath = join(testDir, "cleanup-slot-acquired.ready");
420+
const releaseCleanupPath = join(testDir, "release-cleanup");
421+
setArchivedCleanupJobTestHooks({
422+
pauseAfterAcquire: {
423+
kind: "cleanup",
424+
readyPath: cleanupReadyPath,
425+
releasePath: releaseCleanupPath,
426+
},
427+
});
397428
seedArchivedPair(home);
398429

399430
const server = startServer(0);
431+
let cleanupPromise: Promise<Response> | null = null;
400432
try {
401433
const preview = await previewDigest(server.url, 50);
402-
const cleanupPromise = fetch(new URL("/api/storage/cleanup", server.url), {
434+
cleanupPromise = fetch(new URL("/api/storage/cleanup", server.url), {
403435
method: "POST",
404436
headers: { "content-type": "application/json" },
405437
body: JSON.stringify({ percent: 50, mode: "quarantine", digest: preview.digest }),
406438
});
407439

408-
await Bun.sleep(80);
440+
await waitForCondition(
441+
"manual cleanup to acquire the storage mutation slot",
442+
() => existsSync(cleanupReadyPath),
443+
);
409444

410445
const restoreAttempt = await fetch(new URL("/api/storage/trash/restore", server.url), {
411446
method: "POST",
@@ -418,6 +453,7 @@ describe("storage mutation coordinator", () => {
418453
expect(existsSync(join(home, "archived_sessions", "rollout-new.jsonl"))).toBe(true);
419454
expect(trashStageCount(home)).toBe(0);
420455

456+
writeFileSync(releaseCleanupPath, "release\n");
421457
const cleanupRes = await cleanupPromise;
422458
expect(cleanupRes.status).toBe(200);
423459
const cleanup = await cleanupRes.json();
@@ -427,6 +463,8 @@ describe("storage mutation coordinator", () => {
427463
expect(existsSync(join(home, "archived_sessions", "rollout-new.jsonl"))).toBe(true);
428464
expect(threadCount(home)).toBe(1);
429465
} finally {
466+
writeFileSync(releaseCleanupPath, "release\n");
467+
if (cleanupPromise) await cleanupPromise.catch(() => undefined);
430468
await stopRaceServer(server);
431469
}
432470
}, { timeout: 30_000 });

0 commit comments

Comments
 (0)