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.
This commit is contained in:
_Kerman
2026-08-19 11:41:49 +08:00
parent f97ea54ca9
commit 036ba74c43
+12 -3
View File
@@ -475,7 +475,17 @@ describe('readTextForDiff', () => {
const reached = Promise.withResolvers<undefined>()
const release = Promise.withResolvers<undefined>()
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<typeof import('node:fs/promises')>()
@@ -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()