Move files

This commit is contained in:
Andras Schmelczer 2025-02-19 20:47:52 +00:00
parent 6bb051460e
commit dd6f63f357
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
50 changed files with 72 additions and 78 deletions

View file

@ -0,0 +1,79 @@
import { RelativePath } from "../database/document-metadata";
import {
tryLockDocument,
waitForDocumentLock,
unlockDocument
} from "./document-lock";
describe("Document Lock Operations", () => {
const testPath: RelativePath = "test/document/path";
beforeEach(() => {
// Reset the state before each test
(global as any).locked = new Set<RelativePath>();
(global as any).waiters = new Map<RelativePath, (() => void)[]>();
});
test("should lock a document successfully", () => {
const result = tryLockDocument(testPath);
expect(result).toBe(true);
});
test("should not lock a document that is already locked", () => {
tryLockDocument(testPath);
const result = tryLockDocument(testPath);
expect(result).toBe(false);
});
test("should unlock a locked document", () => {
tryLockDocument(testPath);
unlockDocument(testPath);
const result = tryLockDocument(testPath);
expect(result).toBe(true);
unlockDocument(testPath);
});
test("should throw an error when unlocking a document that is not locked", () => {
expect(() => {
unlockDocument(testPath);
}).toThrow(`Document ${testPath} is not locked, cannot unlock`);
});
test("should wait for a document lock and resolve when unlocked", async () => {
tryLockDocument(testPath);
let resolved = false;
const waitPromise = waitForDocumentLock(testPath).then(() => {
resolved = true;
});
unlockDocument(testPath);
await waitPromise;
expect(resolved).toBe(true);
});
test("should resolve multiple waiters in FIFO order", async () => {
tryLockDocument(testPath);
let firstResolved = false;
let secondResolved = false;
const firstWaitPromise = waitForDocumentLock(testPath).then(() => {
firstResolved = true;
});
const secondWaitPromise = waitForDocumentLock(testPath).then(() => {
secondResolved = true;
});
unlockDocument(testPath);
await firstWaitPromise;
expect(firstResolved).toBe(true);
expect(secondResolved).toBe(false);
unlockDocument(testPath);
await secondWaitPromise;
expect(secondResolved).toBe(true);
});
});