diff --git a/src/shared/utils/__tests__/lock.spec.ts b/src/shared/utils/__tests__/lock.spec.ts index dc6dbb52e7..a9d5a90236 100644 --- a/src/shared/utils/__tests__/lock.spec.ts +++ b/src/shared/utils/__tests__/lock.spec.ts @@ -23,19 +23,31 @@ describe('Lock', () => { }); it('should lock', async () => { - let hasRun = false; let hasUpdated = false; + let resolveLockHeld: () => void; + const lockHeld = new Promise((r) => (resolveLockHeld = r)); + let releaseFirstLock: () => void; + const waitForRelease = new Promise((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 () => {