Files
clean-architecture-frontend…/tests/support/browser/browser-test.ts
T
DongHyeonka f81dda4913
frontend-quality-gates / merge / static checks (pull_request) Successful in 1m23s
frontend-quality-gates / merge / browser tests (pull_request) Canceled after 0s
frontend-quality-gates / FE-GATE-014 / config-compatibility (pull_request) Canceled after 0s
frontend-quality-gates / FE-GATE-028 / manual-accessibility-review (pull_request) Canceled after 0s
frontend-quality-gates / FE-GATE-026 / lab-performance (pull_request) Canceled after 0s
frontend-quality-gates / FE-GATE-015 / immutable-release-candidate (pull_request) Canceled after 0s
frontend-quality-gates / external-vulnerability-provider (pull_request) Canceled after 0s
frontend-quality-gates / external-provenance-provider (pull_request) Canceled after 0s
frontend-quality-gates / promote-verified-immutable-candidate (pull_request) Canceled after 0s
frontend-quality-gates / deploy-promoted-release (pull_request) Canceled after 0s
frontend-quality-gates / FE-GATE-019 / hosting-header (pull_request) Canceled after 0s
frontend-quality-gates / FE-GATE-021 / runbook-boot-config (pull_request) Canceled after 0s
frontend-quality-gates / FE-GATE-022 / runbook-chunk-mismatch (pull_request) Canceled after 0s
frontend-quality-gates / FE-GATE-023 / runbook-api-degradation (pull_request) Canceled after 0s
frontend-quality-gates / FE-GATE-024 / runbook-telemetry (pull_request) Canceled after 0s
frontend-quality-gates / FE-GATE-025 / runbook-release-rollback (pull_request) Canceled after 0s
frontend-quality-gates / FE-GATE-018 / field-web-vitals (pull_request) Canceled after 0s
frontend-quality-gates / FE-GATE-017 / diagram-review (pull_request) Canceled after 0s
frontend-quality-gates / main / self-tests and removal drills (pull_request) Canceled after 0s
frontend-quality-gates / merge / tests (pull_request) Canceled after 11m31s
test(e2e): Firefox 브라우저 프로필을 RAM 파일시스템에 둔다
FE-GATE-008의 Firefox 시험이 가끔 첫 화면 5초 대기에서 실패한다
(old-tab-chunk-recovery.spec.ts, preload-use.spec.ts,
platform-overview.spec.ts). runner run 127과 desktop merge 검증에서
실패했고, 다시 돌리면 통과해 묻혔다.
Playwright는 Firefox 프로필을 os.tmpdir()에 만든다. desktop과 CI job
모두 /tmp가 같은 SATA SSD 위에 있다. Firefox는 시험 context의
localStorage·IndexedDB를 그 프로필(디스크)에 둔다. 그래서 디스크가
붐비면 context마다 첫 localStorage 접근이 디스크 일을 기다린다.
Chromium·WebKit은 시험 context 저장소를 메모리에 둔다. 앱은 첫 렌더
전에 색 구성 설정을 localStorage에서 읽어, 그 대기가 첫 화면 대기에
들어간다. desktop e2e 전체 8회(2026-11-13)에서 Firefox 앱 부팅 463회
중 19회가 0.7~3.8초 멈췄고, Chromium·WebKit은 16 ms를 넘지 않았다.

tests/support/browser/browser-test.ts가 Firefox worker의 프로필을 RAM
파일시스템에 둔다. os.tmpdir()가 이미 tmpfs면 그대로 쓴다. 아니면
/dev/shm에 worker 수 x 64 MB 자리가 있을 때 그 안에 폴더를 만든다.
Playwright의 browser fixture가 launch하는 동안만 TMPDIR를 그 폴더로
돌리고 바로 되돌린다. 그래서 fixture build 같은 다른 임시 파일은
디스크에 그대로 남는다. 프로필이 그 폴더에 생기지 않으면 시험을
실패시킨다. 자리가 없으면 이유를 한 줄 알리고 디스크에 둔다. strict
fixture와 strict를 쓰지 않는 spec 둘(preload-use, chunk-load-error)이
이 test를 바탕으로 쓴다. docs/work/reference/testing.md 5절 표에 원인을
적었다.

버린 대안:
- worker마다 localStorage를 미리 한 번 건드리는 예열: 브라우저 프로세스당
  한 번 드는 준비 비용으로 봤다. 예열 뒤에도 19/463이 멈췄다. 예열
  context를 바로 닫는 판은 21/464로 더 나빴다.
- toolkit.storage.synchronous=0(SQLite fsync 끔): 15/224로 그대로였다.
- 첫 화면 대기 timeout 늘리기, worker 줄이기: 원인을 덮는다.
- 시험 실행 전체에 TMPDIR 걸기: fixture build까지 RAM에 들어간다.

위험·한계:
- CI job 컨테이너는 /dev/shm이 64 MB다(2026-11-13 runner 확인). /tmp는
  같은 SSD 위에 있다. 그래서 CI에서는 프로필이 디스크에 남고 로그에
  이유가 한 줄 찍힌다. CI까지 고치려면 runner가 job 컨테이너에 RAM
  자리를 줘야 한다(--shm-size 또는 --tmpfs /tmp). runner 설정이라 이
  커밋에 넣지 않았다.
- 64 MB/worker는 Firefox worker 6개 프로필 합이 가장 컸을 때 211 MB(1개
  35 MB)인 것을 보고 잡은 여유다.
- Playwright의 두 동작에 기댄다. fixture 의존성을 적힌 순서로 준비하는
  것과, launch할 때 os.tmpdir()를 읽는 것이다. 어느 쪽이 바뀌어도
  폴더가 비므로 시험이 실패해 알린다.

검증(desktop):
- 재현: Firefox 6개를 동시에 띄우고 context 8개씩 localStorage 첫 접근을
  쟀다. 프로필 /tmp에서 최대 8,157 ms(384회), /dev/shm에서 최대 17 ms
  (384회).
- 수정본 e2e 전체 10회: 매번 189 passed. Firefox 부팅 576회 중 0.5초를
  넘는 멈춤 0회(최대 36 ms), 부하 15.9~31.2. 프로필은 매번 /dev/shm에
  5~6개, 디스크 0개, 끝난 뒤 남은 폴더 0개.
- 변이: launch 전에 TMPDIR를 돌리지 않게 하면 theme.spec.ts(firefox)가
  2 failed, 안전장치 메시지로 실패한다.
- check:types:test, eslint(바뀐 파일), check:test-evidence:source 통과.
- scripts/verify.sh gate FE-GATE-008(CI=true, main ee95573f 위): rc=0 205초,
  e2e 189 passed, browser-capabilities 69, storybook 11, visual 7.
- scripts/verify.sh docs, desktop verify:documentation 통과.
2026-11-13 18:13:17 +09:00

118 lines
4.6 KiB
TypeScript

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<ProfileRoot> {
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<object, { firefoxProfileRoot: ProfileRoot }>({
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" },
],
});