Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions src/shared/utils/__tests__/lock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,31 @@ describe('Lock', () => {
});

it('should lock', async () => {
let hasRun = false;
let hasUpdated = false;
let resolveLockHeld: () => void;
const lockHeld = new Promise<void>((r) => (resolveLockHeld = r));
let releaseFirstLock: () => void;
const waitForRelease = new Promise<void>((r) => (releaseFirstLock = r));

// Start first lock and signal when acquired
const firstLockPromise = lock(async () => {
resolveLockHeld();
await waitForRelease;
});

// Wait until first lock is definitely held
await lockHeld;

setTimeout(async () => {
hasRun = true;
await lock(async () => {
hasUpdated = true;
});
// Try to acquire second lock while first is held - should be rejected
await lock(async () => {
hasUpdated = true;
});
await lock(() => Util.delay(2));

expect(hasRun).toBeTruthy();
expect(hasUpdated).toBeFalsy();

// Release first lock and wait for completion
releaseFirstLock();
await firstLockPromise;
});

it('should unlock on completion', async () => {
Expand Down