chat: the epub reader test retries a slow browser start instead of one 15s race (#684)
Cause: the test raced one cold browser launch against a fixed 15s timer. A passing CI run takes 1.3-1.5s (runs 41710048801, 41709930999, 41689486507); run 41718344236 took 16.6s, so a cold runner start, not the reader, failed the test and blocked every package's publish. Now each try gets its own profile and 10s (7x a normal run); a try that posts nothing is killed and replaced, up to 3 tries, with the test timeout at 60s. A browser that exits by itself still fails at once. The test only; no package file changes, so no version bump (chat's "files" omit tests). Run on mote (bun 1.4.2, Playwright's Chromium 1234): - Injected a wrapper browser that sleeps 25s on its first launch: the old test fails "browser timed out" at 15.06s, the new one passes in 11.7s. - Normal launch: new test passes in 2.0s. - Whole suite: 5349 pass, 19 skip, 0 fail (the browser test skips without a browser in PATH; it was run separately as above). bun scripts/check.mjs ok. Not tried: GitHub's runner itself; CI's run is the check.
This commit is contained in:
@@ -114,12 +114,26 @@ test.skipIf(!browserPath && !process.env.CI)("reads complete books in spine orde
|
||||
requested.push(path);
|
||||
return new Response("", { status: 404 });
|
||||
} });
|
||||
const profile = mkdtempSync(join(tmpdir(), "magpie-epub-browser-"));
|
||||
const proc = Bun.spawn([browserPath, "--headless", "--disable-gpu", "--disable-background-networking",
|
||||
"--no-first-run", "--use-mock-keychain", "--user-data-dir=" + profile, String(server.url)], { stdout: "ignore", stderr: "ignore" });
|
||||
// A browser that is slow to start (a cold CI runner took 16s where a run takes 1.3s) is not a reader
|
||||
// failure: each try gets its own profile and 10s (7x a normal run), and a try that posts nothing is
|
||||
// killed and replaced. A browser that exits by itself still fails at once.
|
||||
const tries = 3, tryMs = 10000;
|
||||
let result;
|
||||
try {
|
||||
const result = await Promise.race([promise, proc.exited.then(() => { throw new Error("browser exited"); }),
|
||||
new Promise((_, reject) => setTimeout(() => reject(new Error("browser timed out")), 15000).unref())]);
|
||||
for (let attempt = 1; !result; attempt++) {
|
||||
const profile = mkdtempSync(join(tmpdir(), "magpie-epub-browser-"));
|
||||
const proc = Bun.spawn([browserPath, "--headless", "--disable-gpu", "--disable-background-networking",
|
||||
"--no-first-run", "--use-mock-keychain", "--user-data-dir=" + profile, String(server.url)], { stdout: "ignore", stderr: "ignore" });
|
||||
let timer;
|
||||
try {
|
||||
result = await Promise.race([promise, proc.exited.then(() => { throw new Error("browser exited"); }),
|
||||
new Promise(resolve => { timer = setTimeout(() => resolve(null), tryMs); })]);
|
||||
if (!result && attempt >= tries) throw new Error(`browser timed out ${tries} times`);
|
||||
} finally {
|
||||
clearTimeout(timer); proc.kill(); await proc.exited;
|
||||
rmSync(profile, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
expect(result.mixed).toEqual({ text: "红楼梦𠀀。\n\n次章。\n\n有 空格\n\n下一行", characters: 14, imagePages: 0 });
|
||||
expect(result.duplicates).toEqual({ text: "同文\n\n同文", characters: 4, imagePages: 0 });
|
||||
expect(result.hybrid).toEqual({ text: "可读正文", characters: 4, imagePages: 2 });
|
||||
@@ -134,7 +148,6 @@ test.skipIf(!browserPath && !process.env.CI)("reads complete books in spine orde
|
||||
expect(result.injected).toBe(false);
|
||||
expect(requested).not.toContain("/unexpected");
|
||||
} finally {
|
||||
proc.kill(); await proc.exited; await server.stop(true);
|
||||
rmSync(profile, { recursive: true, force: true });
|
||||
await server.stop(true);
|
||||
}
|
||||
}, 20000);
|
||||
}, 60000);
|
||||
|
||||
Reference in New Issue
Block a user