diff --git a/docs/work/reference/testing.md b/docs/work/reference/testing.md index bd739ff0..c4f5943c 100644 --- a/docs/work/reference/testing.md +++ b/docs/work/reference/testing.md @@ -101,6 +101,7 @@ | 그리는 중인 화면을 누른다 | 앱 뒤에 붙인 버튼이 렌더링 중 12 px 밀려 클릭이 닿지 않았다 | 테스트가 붙이는 조작 요소는 앱 레이아웃과 무관한 고정 층에 둔다. "다 그려질 때까지 기다리기"는 신호가 없어 쓰지 않는다(결함 패턴 T5) | | 짧은 상태를 폴링으로 잡는다 | 500 ms만 이어지는 `BLOCKED` 상태를 폴링으로 잡으려다 두 엔진에서 실패 | 상태 변화를 이벤트·기록으로 받는다 | | 고정 상한과 첫 실행 비용 | provider 테스트 10개가 10초 상한에 묶이고, 하나만 고르면 첫 테스트가 fixture 빌드를 떠안아 실패 | 무거운 준비는 suite 준비 단계로 옮기고 suite가 자기 timeout을 선언한다 | +| 브라우저가 붐비는 디스크를 기다린다 | Firefox는 시험 context의 localStorage를 디스크의 프로필에 둔다. runner와 같이 쓰는 desktop SSD에서 context마다 첫 접근이 최대 8.2초 멈췄고, 첫 화면 5초 대기 안에서 실패했다. 프로필을 `/dev/shm`에 두면 최대 17 ms | Firefox 프로필은 RAM 파일시스템에 둔다(`tests/support/browser/browser-test.ts`). 자리가 없으면 그 사실과 이유를 한 줄로 알린다 | ## 6. 테스트 실행 비용 diff --git a/tests/browser-capabilities/chunk-load-error.spec.ts b/tests/browser-capabilities/chunk-load-error.spec.ts index 41e638fb..5929a857 100644 --- a/tests/browser-capabilities/chunk-load-error.spec.ts +++ b/tests/browser-capabilities/chunk-load-error.spec.ts @@ -1,4 +1,4 @@ -import { expect, test } from "@playwright/test"; +import { expect, test } from "../support/browser/browser-test.ts"; // R3-7. The chunk-recovery classifier matches engine error wording. Run the // app's own classifier (served by the dev server) against the error each real diff --git a/tests/e2e/preload-use.spec.ts b/tests/e2e/preload-use.spec.ts index 3652cf11..7f614f83 100644 --- a/tests/e2e/preload-use.spec.ts +++ b/tests/e2e/preload-use.spec.ts @@ -1,10 +1,13 @@ import { readFile } from "node:fs/promises"; import path from "node:path"; -import { expect, test, type Page } from "@playwright/test"; +import type { Page } from "@playwright/test"; import { addGeneratedFeature } from "../../scripts/lib/generated-feature.ts"; import { testRunPath } from "../../scripts/lib/run-isolation.ts"; +// The tracker's own tests run without the strict verdict, on the same +// browser set-up as every other spec. +import { expect, test } from "../support/browser/browser-test.ts"; import { trackPreloadUse } from "../support/browser/preload-use.ts"; import { test as strictTest } from "../support/browser/strict-browser-test.ts"; import { diff --git a/tests/support/browser/browser-test.ts b/tests/support/browser/browser-test.ts new file mode 100644 index 00000000..eee1e26a --- /dev/null +++ b/tests/support/browser/browser-test.ts @@ -0,0 +1,117 @@ +import { mkdtemp, readdir, rm, statfs } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import path from "node:path"; + +import { test as base } from "@playwright/test"; + +export { expect } from "@playwright/test"; + +/** + * The browser tests' base `test`: Playwright's, with each Firefox worker's + * browser profile on a RAM filesystem. Every browser spec reaches it, through + * tests/support/browser/strict-browser-test.ts or directly. + * + * Firefox keeps a test context's localStorage and IndexedDB in the profile on + * disk (Chromium and WebKit keep them in memory), and the first localStorage + * access of each context waits for that disk work. On desktop, whose SSD the + * CI runner shares with every other job, that access took up to 8.2 s with + * the profile in /tmp and never more than 17 ms with it in /dev/shm + * (2026-11-13, six browsers at a time, 384 accesses each way). The app reads + * the colour scheme preference before it renders, so a Firefox test spent the + * wait inside its 5 s wait for the first screen: 19 of 463 Firefox app boots + * took 0.7 to 3.8 s in eight full FE-GATE-008 runs, and none of 174 took more + * than 14 ms in three Firefox runs with the profiles in RAM. The failures it + * caused before were in `old-tab-chunk-recovery.spec.ts`, + * `preload-use.spec.ts` and `platform-overview.spec.ts`. + */ + +/** `f_type` of a tmpfs in statfs(2). */ +const TMPFS_MAGIC = 0x0102_1994; + +/** The RAM filesystem of Linux, the desktop and the CI job containers included. */ +const SHARED_MEMORY = "/dev/shm"; + +/** + * Room for one Firefox profile. Six Firefox workers' profiles held 211 MB + * together at their largest in a Firefox FE-GATE-008 run (2026-11-13). + */ +const PROFILE_ROOM_BYTES = 64 * 1024 * 1024; + +type ProfileRoot = Readonly<{ + /** The directory for the profile when it is not the temp directory. */ + root?: string; + /** Why the profile stays on disk. */ + note?: string; +}>; + +/** + * Where this worker's Firefox makes its profile: the temp directory when it is + * already in RAM, else a new directory in /dev/shm when that has room for + * every worker's profile, else the temp directory on disk, with the reason. + */ +async function firefoxProfileRoot(workers: number): Promise { + const needed = workers * PROFILE_ROOM_BYTES; + const notes: string[] = []; + for (const candidate of [tmpdir(), SHARED_MEMORY]) { + const space = await statfs(candidate).catch(() => undefined); + if (space === undefined) { + notes.push(`${candidate} is missing`); + } else if (space.type !== TMPFS_MAGIC) { + notes.push(`${candidate} is on disk`); + } else if (space.bavail * space.bsize < needed) { + notes.push( + `${candidate} has ${Math.floor((space.bavail * space.bsize) / 2 ** 20)} MB free, ` + + `${workers} workers need ${needed / 2 ** 20} MB`, + ); + } else { + return candidate === tmpdir() + ? {} + : { root: await mkdtemp(path.join(candidate, "playwright-firefox-")) }; + } + } + return { note: `Firefox profiles stay on disk in ${tmpdir()}: ${notes.join("; ")}.` }; +} + +/** TMPDIR as the worker started, put back once the browser has launched. */ +const workerTmpdir = process.env.TMPDIR; + +function restoreTmpdir() { + if (workerTmpdir === undefined) delete process.env.TMPDIR; + else process.env.TMPDIR = workerTmpdir; +} + +export const test = base.extend({ + firefoxProfileRoot: [ + async ({ browserName }, use, workerInfo) => { + const choice = + browserName === "firefox" ? await firefoxProfileRoot(workerInfo.config.workers) : {}; + if (choice.note !== undefined) console.warn(choice.note); + if (choice.root !== undefined) process.env.TMPDIR = choice.root; + try { + await use(choice); + } finally { + restoreTmpdir(); + if (choice.root !== undefined) await rm(choice.root, { recursive: true, force: true }); + } + }, + { scope: "worker" }, + ], + // Playwright sets up a fixture's dependencies in the order they are named, + // so the root is chosen before Playwright's own `browser` launches and + // makes its profile under os.tmpdir(). TMPDIR points at the root only until + // then: nothing else the worker puts in the temp directory goes to RAM. + browser: [ + async ({ firefoxProfileRoot, browser }, use) => { + restoreTmpdir(); + const { root } = firefoxProfileRoot; + if (root !== undefined && (await readdir(root)).length === 0) { + throw new Error( + `Firefox did not make its profile in ${root}: Playwright no longer reads ` + + "os.tmpdir() when it launches a browser (or the browser was connected, not launched).", + ); + } + await use(browser); + }, + { scope: "worker" }, + ], +}); diff --git a/tests/support/browser/strict-browser-test.ts b/tests/support/browser/strict-browser-test.ts index 0f144bee..1c0b5a94 100644 --- a/tests/support/browser/strict-browser-test.ts +++ b/tests/support/browser/strict-browser-test.ts @@ -1,10 +1,6 @@ -import { - expect, - test as base, - type ConsoleMessage, - type Page, -} from "@playwright/test"; +import type { ConsoleMessage, Page } from "@playwright/test"; +import { expect, test as base } from "./browser-test.ts"; import { trackPreloadUse } from "./preload-use.ts"; /**