generated from SAP/repository-template
-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add missing tests/utils/AsyncLock.test.ts file
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
- Loading branch information
1 parent
b842c65
commit 35f57b2
Showing
2 changed files
with
43 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
}) |