Files
wehub-resource-sync 070959e133
landing-page-staging / Deploy landing page to staging (push) Has been skipped
landing-page-ci / Validate landing page (push) Failing after 4s
visual-baseline / Capture visual baselines (push) Has been cancelled
bake-plugin-previews / Bake plugin previews (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:00:47 +08:00

63 lines
1.8 KiB
TypeScript

import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { withDirectoryLock } from "../src/lock.js";
describe("withDirectoryLock", () => {
it("reclaims a stale lock when the owner process is gone", async () => {
const root = await mkdtemp(join(tmpdir(), "open-design-tools-pack-lock-"));
const lockRoot = join(root, "locks");
const lockPath = join(lockRoot, "global.lock");
try {
await mkdir(lockPath, { recursive: true });
await writeFile(
join(lockPath, "owner.json"),
`${JSON.stringify({ pid: 424242, startedAt: "2026-06-01T00:00:00.000Z" }, null, 2)}\n`,
"utf8",
);
const result = await withDirectoryLock(
lockRoot,
"global",
async () => readFile(join(lockPath, "owner.json"), "utf8"),
{
processExists: () => false,
timeoutMs: 500,
},
);
expect(result).toContain(`"pid": ${process.pid}`);
} finally {
await rm(root, { force: true, recursive: true });
}
});
it("reclaims an orphaned lock without owner metadata after the grace period", async () => {
const root = await mkdtemp(join(tmpdir(), "open-design-tools-pack-lock-orphan-"));
const lockRoot = join(root, "locks");
const lockPath = join(lockRoot, "global.lock");
try {
await mkdir(lockPath, { recursive: true });
const result = await withDirectoryLock(
lockRoot,
"global",
async () => readFile(join(lockPath, "owner.json"), "utf8"),
{
ownerGraceMs: 0,
timeoutMs: 500,
},
);
expect(result).toContain(`"pid": ${process.pid}`);
} finally {
await rm(root, { force: true, recursive: true });
}
});
});