From 17aadd5934a4ebbade39de577f92b23fcc8ea67c Mon Sep 17 00:00:00 2001 From: TaprootFreak <142087526+TaprootFreak@users.noreply.github.com> Date: Fri, 2 Jan 2026 13:34:17 +0100 Subject: [PATCH] Fix flaky timing test in lock.spec.ts Replace timing-based test with Promise-based synchronization. The original test used setTimeout without delay causing race conditions. The new test uses explicit Promise signaling for deterministic behavior. --- src/shared/utils/__tests__/lock.spec.ts | 28 ++++++++++++++++++------- 1 file changed, 20 insertions(+), 8 deletions(-) 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 () => {