From: Jérôme Benoit Date: Sat, 3 Aug 2024 13:14:51 +0000 (+0200) Subject: test: add missing tests/utils/AsyncLock.test.ts file X-Git-Tag: ocpp-server@v1.5.2~134 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=35f57b2c14853801f09f675a08decf17f5f6da4c;p=e-mobility-charging-stations-simulator.git test: add missing tests/utils/AsyncLock.test.ts file Signed-off-by: Jérôme Benoit --- 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) + } + }) +})