mirror of
https://github.com/morluto/rea.git
synced 2026-11-21 14:05:49 +00:00
test(filesystem): consolidate stable hash and descriptor ownership checks (#12879)
This commit is contained in:
1 parent
6dde865ccf
commit
79bd830f4e
2 files changed
+23
-37
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { appendFile, symlink, truncate, writeFile } from "node:fs/promises";
|
||||
import { symlink, truncate, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
@@ -19,7 +19,7 @@ const admitted = async (contents: string) => {
|
||||
};
|
||||
|
||||
describe("hashStableRegularFileDescriptor", () => {
|
||||
it("hashes the admitted bytes and captures the requested prefix", async () => {
|
||||
it("hashes the admitted bytes and refuses a subsequent shrink", async () => {
|
||||
const { path, source } = await admitted("abcdef");
|
||||
try {
|
||||
expect(
|
||||
@@ -32,18 +32,7 @@ describe("hashStableRegularFileDescriptor", () => {
|
||||
bytes: 6,
|
||||
prefix: Buffer.from("ab"),
|
||||
});
|
||||
} finally {
|
||||
await source.handle.close();
|
||||
}
|
||||
});
|
||||
|
||||
it.each([
|
||||
["grows", (path: string) => appendFile(path, "more")],
|
||||
["shrinks", (path: string) => truncate(path, 2)],
|
||||
])("refuses a file that %s after admission", async (_change, mutate) => {
|
||||
const { path, source } = await admitted("abcdef");
|
||||
try {
|
||||
await mutate(path);
|
||||
await truncate(path, 2);
|
||||
await expect(
|
||||
hashStableRegularFileDescriptor(source, path, {
|
||||
symlinks: "reject",
|
||||
|
||||
@@ -99,12 +99,13 @@ it("captures a two-byte prefix across short reads and buffer reuse", async () =>
|
||||
const path = join(root, "script");
|
||||
const bytes = Buffer.from("#!rest-of-file");
|
||||
await writeFile(path, bytes);
|
||||
const retained: OwnedFileHandle[] = [];
|
||||
const retainFailedClose = vi.fn();
|
||||
let descriptor: regularFile.StableRegularFileDescriptor | undefined;
|
||||
const originalOpen = regularFile.openRegularFile;
|
||||
const open = vi
|
||||
.spyOn(regularFile, "openRegularFile")
|
||||
.mockImplementation(async (...args) => {
|
||||
const descriptor = await originalOpen(...args);
|
||||
descriptor = await originalOpen(...args);
|
||||
const { handle } = descriptor;
|
||||
const positional: {
|
||||
read(
|
||||
@@ -127,17 +128,18 @@ it("captures a two-byte prefix across short reads and buffer reuse", async () =>
|
||||
return descriptor;
|
||||
});
|
||||
try {
|
||||
const observation = await observeStableFile(path, {
|
||||
retainFailedClose: (owner) => retained.push(owner),
|
||||
});
|
||||
const observation = await observeStableFile(path, { retainFailedClose });
|
||||
expect(observation).toMatchObject({
|
||||
sha256: createHash("sha256").update(bytes).digest("hex"),
|
||||
bytes: bytes.length,
|
||||
prefix: Buffer.from("#!"),
|
||||
});
|
||||
expect(descriptor?.handle.fd).toBe(-1);
|
||||
expect(retainFailedClose).not.toHaveBeenCalled();
|
||||
} finally {
|
||||
open.mockRestore();
|
||||
for (const owner of retained) if (owner.handle.fd >= 0) await owner.close();
|
||||
if (descriptor !== undefined && descriptor.handle.fd >= 0)
|
||||
await descriptor.handle.close();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -242,17 +244,6 @@ it("preserves a read cancellation cause when close also fails", async () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("closes a normally hashed file without adopting an owner", async () => {
|
||||
const root = await createTestTempDirectory("rea-stable-hash-control-");
|
||||
const path = join(root, "input.bin");
|
||||
await writeFile(path, "owned bytes");
|
||||
const retainFailedClose = vi.fn();
|
||||
await expect(
|
||||
hashStableFile(path, { retainFailedClose }),
|
||||
).resolves.toMatchObject({ bytes: 11 });
|
||||
expect(retainFailedClose).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("retains a completed digest when cancellation follows successful close", async () => {
|
||||
const root = await createTestTempDirectory("rea-stable-hash-cancel-");
|
||||
const path = join(root, "input.bin");
|
||||
@@ -465,7 +456,6 @@ it("retries pending cleanup before acquiring a publisher temporary root", async
|
||||
|
||||
it.each([
|
||||
{ stage: "close", reason: null },
|
||||
{ stage: "close", reason: "caller stopped" },
|
||||
{ stage: "close", reason: new Error("caller stopped") },
|
||||
{ stage: "retention", reason: "after the failed close" },
|
||||
] as const)(
|
||||
@@ -607,11 +597,12 @@ it.each([false, true])(
|
||||
},
|
||||
);
|
||||
|
||||
it.each(
|
||||
[null, "caller stopped", new Error("caller stopped")].flatMap((reason) =>
|
||||
[false, true].map((failClose) => ({ reason, failClose })),
|
||||
),
|
||||
)(
|
||||
it.each([
|
||||
{ reason: null, failClose: false },
|
||||
{ reason: null, failClose: true },
|
||||
{ reason: "caller stopped", failClose: false },
|
||||
{ reason: new Error("caller stopped"), failClose: true },
|
||||
])(
|
||||
"retains the exact read checkpoint reason with failed close=$failClose ($reason)",
|
||||
async ({ reason, failClose }) => {
|
||||
const root = await createTestTempDirectory("rea-hash-read-checkpoint-");
|
||||
@@ -647,6 +638,12 @@ it.each(
|
||||
retained = owner;
|
||||
},
|
||||
}).catch((cause: unknown) => cause);
|
||||
if (failClose) {
|
||||
expect(failure).toBeInstanceOf(RegularFileCleanupFailure);
|
||||
expect(retained?.handle.fd).toBeGreaterThanOrEqual(0);
|
||||
} else {
|
||||
expect(retained).toBeUndefined();
|
||||
}
|
||||
const primary =
|
||||
failure instanceof RegularFileCleanupFailure &&
|
||||
failure.outcome.kind === "failed"
|
||||
|
||||
Reference in new issue
Block a user