Skip to content

Commit

Permalink
test: add missing tests/utils/AsyncLock.test.ts file
Browse files Browse the repository at this point in the history
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
  • Loading branch information
jerome-benoit committed Aug 3, 2024
1 parent b842c65 commit 35f57b2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/utils/AsyncLock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>(resolve => {
queuedResolve()
asyncLock.resolveQueue.dequeue()
resolve()
})
}
Expand Down
42 changes: 42 additions & 0 deletions tests/utils/AsyncLock.test.ts
Original file line number Diff line number Diff line change
@@ -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)
}
})
})

0 comments on commit 35f57b2

Please sign in to comment.