From 35f57b2c14853801f09f675a08decf17f5f6da4c Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 3 Aug 2024 15:14:51 +0200 Subject: [PATCH] test: add missing tests/utils/AsyncLock.test.ts file MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/utils/AsyncLock.ts | 4 +--- tests/utils/AsyncLock.test.ts | 42 +++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 tests/utils/AsyncLock.test.ts diff --git a/src/utils/AsyncLock.ts b/src/utils/AsyncLock.ts index f69faac8..eef006db 100644 --- a/src/utils/AsyncLock.ts +++ b/src/utils/AsyncLock.ts @@ -51,10 +51,8 @@ export class AsyncLock { asyncLock.acquired = false return } - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const queuedResolve = asyncLock.resolveQueue.dequeue()! await new Promise(resolve => { - queuedResolve() + asyncLock.resolveQueue.dequeue() resolve() }) } diff --git a/tests/utils/AsyncLock.test.ts b/tests/utils/AsyncLock.test.ts new file mode 100644 index 00000000..dbec498a --- /dev/null +++ b/tests/utils/AsyncLock.test.ts @@ -0,0 +1,42 @@ +import { describe, it } from 'node:test' + +import { expect } from 'expect' + +import { AsyncLock, AsyncLockType } from '../../src/utils/AsyncLock.js' + +await describe('AsyncLock test suite', async () => { + await it('Verify runExclusive()', () => { + const runs = 10 + let executed: number[] = [] + let count = 0 + const fn = () => { + executed.push(++count) + } + for (let i = 0; i < runs; i++) { + AsyncLock.runExclusive(AsyncLockType.configuration, fn) + .then(() => { + expect(executed).toEqual(new Array(count).fill(0).map((_, i) => ++i)) + return undefined + }) + // eslint-disable-next-line @typescript-eslint/use-unknown-in-catch-callback-variable + .catch(console.error) + } + executed = [] + count = 0 + const asyncFn = async () => { + await new Promise(resolve => { + setTimeout(resolve, 100) + }) + executed.push(++count) + } + for (let i = 0; i < runs; i++) { + AsyncLock.runExclusive(AsyncLockType.configuration, asyncFn) + .then(() => { + expect(executed).toEqual(new Array(count).fill(0).map((_, i) => ++i)) + return undefined + }) + // eslint-disable-next-line @typescript-eslint/use-unknown-in-catch-callback-variable + .catch(console.error) + } + }) +}) -- 2.34.1