From 036ba74c43a59945c5d89487dcfe26d0f8f136fb Mon Sep 17 00:00:00 2001 From: _Kerman Date: Wed, 19 Aug 2026 11:41:49 +0800 Subject: [PATCH] test(fs-local): attribute diff-basis cancellation to fsio allocations The observes-cancellation-after-open/stat test asserted that the process-wide Buffer.allocUnsafe call count stayed flat after abort, but vitest's fork IPC (node:internal/child_process serialization) also calls Buffer.allocUnsafe, so unrelated IPC traffic made the assertion timing-racy under CI load. Attribute each allocation to the fsio read path by stack and assert that the abort prevents the diff-basis buffer allocation. --- packages/fs/fs-local/tests/fsio.spec.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/fs/fs-local/tests/fsio.spec.ts b/packages/fs/fs-local/tests/fsio.spec.ts index b51f6ba83b..5cf5ffd5d3 100644 --- a/packages/fs/fs-local/tests/fsio.spec.ts +++ b/packages/fs/fs-local/tests/fsio.spec.ts @@ -475,7 +475,17 @@ describe('readTextForDiff', () => { const reached = Promise.withResolvers() const release = Promise.withResolvers() let statCalls = 0 - const allocate = vi.spyOn(Buffer, 'allocUnsafe') + const allocUnsafe = Buffer.allocUnsafe.bind(Buffer) + // Buffer.allocUnsafe is also called by vitest's fork IPC + // (node:internal/child_process serialization), so a process-wide call count + // is timing-racy under CI load. Attribute allocations to the fsio read path + // instead: the abort must prevent the diff-basis buffer allocation. + const fsioAllocations: string[] = [] + const allocate = vi.spyOn(Buffer, 'allocUnsafe').mockImplementation((size: number) => { + const stack = new Error().stack ?? '' + if (stack.includes('readTextForDiff')) fsioAllocations.push(stack) + return allocUnsafe(size) + }) vi.resetModules() vi.doMock('node:fs/promises', async (importOriginal) => { const actual = await importOriginal() @@ -509,12 +519,11 @@ describe('readTextForDiff', () => { const controller = new AbortController() const pending = isolatedReadTextForDiff(file, 8, controller.signal) await reached.promise - const allocationCalls = allocate.mock.calls.length controller.abort() release.resolve(undefined) await expect(pending).rejects.toMatchObject({ code: 'FS_ABORTED' }) expect(statCalls).toBe(stage === 'open' ? 0 : 1) - expect(allocate).toHaveBeenCalledTimes(allocationCalls) + expect(fsioAllocations).toEqual([]) } finally { release.resolve(undefined) allocate.mockRestore()