From 198e9856f64d20e54998468ee9e1efa06fd11a64 Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Thu, 12 Nov 2026 22:17:32 +0900 Subject: [PATCH] =?UTF-8?q?test(schemas):=20=EC=8A=A4=ED=82=A4=EB=A7=88=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20=EC=8B=9C=ED=97=98=EC=9D=B4=20fsync=20?= =?UTF-8?q?=EC=97=86=EB=8A=94=20writer=EB=A1=9C=20=EC=9E=84=EC=8B=9C=20?= =?UTF-8?q?=ED=8F=B4=EB=8D=94=EC=97=90=20=EC=93=B4=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 문제: json-schema 시험 「generates deterministic … detects drift」가 임시 폴더에 스키마 넷을 쓸 때 파일과 폴더를 fsync한다. 이 시험의 대부분이 fsync이고, 디스크가 바쁘면 시간이 크게 뛴다(desktop 89~1,075ms, CPU를 제한한 coverage 측정에서 7.4초). 이 시험은 생성한 바이트와 drift 판정을 보고, 내구성은 validated-json-artifact 시험이 본다. 기본 timeout 10초 아래라 자기 timeout도 없다. 고른 방법: `generateArtifactSchemas`가 writer를 받는다(기본은 지금의 `writeValidatedJsonArtifact`). 시험은 같은 검증·원자적 rename writer를 `createValidatedJsonArtifactWriter`로 만들고 sync만 뺀다. - 버린 대안: 시험에 timeout 선언. 시간을 줄이지 않고 흔들림을 덮는다. 위험·한계: 생성 명령과 check:artifact-schemas의 쓰기는 그대로다. 검증(desktop, 고치기 전·후 번갈아 3회, 부하 11~16): 89/1,075/98 → 36/48/35ms. coverage + CPU 소모 12개에서도 208~519 → 73~151ms. 시험 통과, check:artifact-schemas rc=0, check:types·eslint rc=0. 로그 /tmp/claude-gates/w2fe2-slow2·6/. --- scripts/generate-artifact-schemas.ts | 14 ++++++++++++-- tests/unit/json-schema.test.ts | 28 ++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/scripts/generate-artifact-schemas.ts b/scripts/generate-artifact-schemas.ts index 333f25e1..ed8d89f6 100644 --- a/scripts/generate-artifact-schemas.ts +++ b/scripts/generate-artifact-schemas.ts @@ -11,7 +11,10 @@ import { registrySnapshotArtifactSchema, supplyChainVerificationArtifactSchema, } from "./contracts/release-artifacts.ts"; -import { writeValidatedJsonArtifact } from "./lib/validated-json-artifact.ts"; +import { + type ValidatedJsonArtifactInput, + writeValidatedJsonArtifact, +} from "./lib/validated-json-artifact.ts"; const ARTIFACT_SCHEMA_DEFINITIONS = Object.freeze([ { @@ -39,6 +42,13 @@ const ARTIFACT_SCHEMA_DEFINITIONS = Object.freeze([ type GenerateArtifactSchemasOptions = Readonly<{ root?: string; check?: boolean; + /** + * Publishes one schema document. The default writes durably (fsync of the + * file and its directory). A test that only checks the generated bytes in a + * temporary root passes a writer without fsync: four fsync pairs were most + * of its time and grew to seconds under the parallel suite's disk load. + */ + writeArtifact?: (input: ValidatedJsonArtifactInput) => Promise; }>; function canonicalize(value: unknown): unknown { @@ -90,7 +100,7 @@ export async function generateArtifactSchemas( continue; } await mkdir(path.dirname(destination), { recursive: true }); - await writeValidatedJsonArtifact({ + await (options.writeArtifact ?? writeValidatedJsonArtifact)({ path: destination, schema: jsonSchemaDocumentArtifactSchema, value: document, diff --git a/tests/unit/json-schema.test.ts b/tests/unit/json-schema.test.ts index fb5bfd0a..ebc27389 100644 --- a/tests/unit/json-schema.test.ts +++ b/tests/unit/json-schema.test.ts @@ -1,4 +1,4 @@ -import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; +import { mkdtemp, open, readFile, rename, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; @@ -6,6 +6,30 @@ import { afterEach, describe, expect, it } from "vitest"; import { generateArtifactSchemas } from "../../scripts/generate-artifact-schemas.ts"; import { assertMatchesJsonSchema } from "../../scripts/lib/json-schema.ts"; +import { createValidatedJsonArtifactWriter } from "../../scripts/lib/validated-json-artifact.ts"; + +// The same validated, atomic-rename writer as the generator's default, without +// fsync. This suite checks generated bytes in a temporary root; durability is +// validated-json-artifact.test.ts's subject, and the fsync pairs were most of +// this test's time under the parallel suite's disk load. +const writeArtifactWithoutSync = createValidatedJsonArtifactWriter({ + fileSystem: { + open: async (target, flags, mode) => { + const handle = await open(target, flags, mode); + return { + writeFile: async (data, encoding) => handle.writeFile(data, encoding), + sync: async () => undefined, + close: async () => handle.close(), + }; + }, + openDirectory: async () => ({ + sync: async () => undefined, + close: async () => undefined, + }), + rename: async (source, destination) => rename(source, destination), + rm: async (target, options) => rm(target, options), + }, +}); const temporaryDirectories: string[] = []; @@ -77,7 +101,7 @@ describe("checked-in JSON Schema execution", () => { it("generates deterministic draft 2020-12 artifact schemas and detects drift", async () => { const root = await temporaryDirectory(); - await generateArtifactSchemas({ root }); + await generateArtifactSchemas({ root, writeArtifact: writeArtifactWithoutSync }); const generatedPath = path.join( root, "config/schemas/artifacts/dependency-inventory.schema.json",