test(model): share cancellation read spy (#15782)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-02 05:05:22 +00:00
1 parent 3f8d97f4bb
commit 34abfa167c
1 file changed
+52 -99
@@ -7,6 +7,40 @@ import { projectAnalysisError } from "../../../src/domain/analysisErrorProjectio
import { ggufFixture, safetensorsFixture } from "../../fixtures/model/model.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
const spyOnModelReads = async (
path: string,
afterRead: (position: number, bytesRead: number) => void,
) => {
const sample = await open(path, "r");
const prototype = Object.getPrototypeOf(sample) as {
read: (
buffer: Buffer,
offset: number,
length: number,
position: number,
) => Promise<{ bytesRead: number }>;
};
await sample.close();
const originalRead = prototype.read;
return vi.spyOn(prototype, "read").mockImplementation(async function (
this: unknown,
buffer: Buffer,
offset: number,
length: number,
position: number,
) {
const result = await originalRead.call(
this,
buffer,
offset,
length,
position,
);
afterRead(position, result.bytesRead);
return result;
});
};
const cancelledArrayFixture = (alignment?: number) => {
const seed = ggufFixture();
const count = 200_000;
@@ -42,42 +76,15 @@ it.each([undefined, 64])(
const path = join(root, "model.gguf");
const { fixture, count, arrayStart } = cancelledArrayFixture(alignment);
await writeFile(path, fixture.bytes);
const sample = await open(path, "r");
const handlePrototype = Object.getPrototypeOf(sample) as {
read: (
buffer: Buffer,
offset: number,
length: number,
position: number,
) => Promise<{ bytesRead: number }>;
};
await sample.close();
const originalRead = handlePrototype.read;
const controller = new AbortController();
const reason = { marker: "caller-selected-parser-cancel" };
let arrayBytesObserved = 0;
const readSpy = vi
.spyOn(handlePrototype, "read")
.mockImplementation(async function (
this: unknown,
buffer: Buffer,
offset: number,
length: number,
position: number,
) {
const result = await originalRead.call(
this,
buffer,
offset,
length,
position,
);
if (position === arrayStart && result.bytesRead > 0) {
arrayBytesObserved = result.bytesRead;
controller.abort(reason);
}
return result;
});
const readSpy = await spyOnModelReads(path, (position, bytesRead) => {
if (position === arrayStart && bytesRead > 0) {
arrayBytesObserved = bytesRead;
controller.abort(reason);
}
});
const provider = new ModelArtifactProvider();
try {
const result = await provider.inspect(
@@ -152,42 +159,15 @@ it("retains GGUF tensor declarations and layout limitations before later tensor
})),
});
await writeFile(path, fixture.bytes);
const sample = await open(path, "r");
const handlePrototype = Object.getPrototypeOf(sample) as {
read: (
buffer: Buffer,
offset: number,
length: number,
position: number,
) => Promise<{ bytesRead: number }>;
};
await sample.close();
const originalRead = handlePrototype.read;
const controller = new AbortController();
const reason = { marker: "cancel-after-completed-tensor-declarations" };
let laterHeaderChunkSeen = false;
const readSpy = vi
.spyOn(handlePrototype, "read")
.mockImplementation(async function (
this: unknown,
buffer: Buffer,
offset: number,
length: number,
position: number,
) {
const result = await originalRead.call(
this,
buffer,
offset,
length,
position,
);
if (position >= 24 + 65536 && result.bytesRead > 0) {
laterHeaderChunkSeen = true;
controller.abort(reason);
}
return result;
});
const readSpy = await spyOnModelReads(path, (position, bytesRead) => {
if (position >= 24 + 65536 && bytesRead > 0) {
laterHeaderChunkSeen = true;
controller.abort(reason);
}
});
const provider = new ModelArtifactProvider();
try {
const result = await provider.inspect(
@@ -275,42 +255,15 @@ it.each([
],
});
await writeFile(path, fixture.bytes);
const sample = await open(path, "r");
const prototype = Object.getPrototypeOf(sample) as {
read: (
buffer: Buffer,
offset: number,
length: number,
position: number,
) => Promise<{ bytesRead: number }>;
};
await sample.close();
const originalRead = prototype.read;
const controller = new AbortController();
const reason = { marker: "cancel-during-full-model-hash" };
let payloadRead = false;
const readSpy = vi
.spyOn(prototype, "read")
.mockImplementation(async function (
this: unknown,
buffer: Buffer,
offset: number,
length: number,
position: number,
) {
const result = await originalRead.call(
this,
buffer,
offset,
length,
position,
);
if (position >= fixture.header.length && result.bytesRead > 0) {
payloadRead = true;
controller.abort(reason);
}
return result;
});
const readSpy = await spyOnModelReads(path, (position, bytesRead) => {
if (position >= fixture.header.length && bytesRead > 0) {
payloadRead = true;
controller.abort(reason);
}
});
const provider = new ModelArtifactProvider();
try {
const result = await provider.inspect(