-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: distinguish local mutex correctly
- Loading branch information
1 parent
79e0064
commit c889656
Showing
3 changed files
with
83 additions
and
5 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
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,73 @@ | ||
import { randomUUID } from 'node:crypto'; | ||
|
||
import { TestingModule } from '@nestjs/testing'; | ||
import ava, { TestFn } from 'ava'; | ||
import Sinon from 'sinon'; | ||
|
||
import { Locker, Mutex } from '../src/base/mutex'; | ||
import { SessionRedis } from '../src/base/redis'; | ||
import { createTestingModule, sleep } from './utils'; | ||
|
||
const test = ava as TestFn<{ | ||
module: TestingModule; | ||
mutex: Mutex; | ||
locker: Locker; | ||
session: SessionRedis; | ||
}>; | ||
|
||
test.beforeEach(async t => { | ||
const module = await createTestingModule(); | ||
|
||
t.context.module = module; | ||
t.context.mutex = module.get(Mutex); | ||
t.context.locker = module.get(Locker); | ||
t.context.session = module.get(SessionRedis); | ||
}); | ||
|
||
test.afterEach(async t => { | ||
await t.context.module.close(); | ||
}); | ||
|
||
const lockerPrefix = randomUUID(); | ||
test('should be able to acquire lock', async t => { | ||
const { mutex } = t.context; | ||
|
||
{ | ||
t.truthy( | ||
await mutex.acquire(`${lockerPrefix}1`), | ||
'should be able to acquire lock' | ||
); | ||
t.falsy( | ||
await mutex.acquire(`${lockerPrefix}1`), | ||
'should not be able to acquire lock again' | ||
); | ||
} | ||
|
||
{ | ||
const lock1 = await mutex.acquire(`${lockerPrefix}2`); | ||
t.truthy(lock1); | ||
await lock1?.release(); | ||
const lock2 = await mutex.acquire(`${lockerPrefix}2`); | ||
t.truthy(lock2); | ||
} | ||
}); | ||
|
||
test('should be able to acquire lock parallel', async t => { | ||
const { mutex, locker } = t.context; | ||
const spyedLocker = Sinon.spy(locker, 'lock'); | ||
const requestLock = async (key: string) => { | ||
const lock = mutex.acquire(key); | ||
await using _lock = await lock; | ||
await t.throwsAsync(spyedLocker.lastCall.returnValue, { | ||
message: `Failed to acquire lock for resource [${key}]`, | ||
}); | ||
await sleep(100); | ||
}; | ||
|
||
await t.notThrowsAsync( | ||
Promise.all( | ||
Array.from({ length: 10 }, _ => requestLock(`${lockerPrefix}3`)) | ||
), | ||
'should be able to acquire lock parallel' | ||
); | ||
}); |