generated from streamich/tpl-ts-lib
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🎸 improve locking interface
- Loading branch information
Showing
2 changed files
with
89 additions
and
27 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,63 @@ | ||
import {Locks} from '../Locks'; | ||
import {tick} from '../tick'; | ||
|
||
describe('.acquire()', () => { | ||
test('should return true on first call', () => { | ||
const locks = new Locks({}); | ||
const unlock = locks.acquire('my-lock'); | ||
expect(unlock).toBeInstanceOf(Function); | ||
}); | ||
|
||
test('should not lock on second calls', () => { | ||
const locks = new Locks({}); | ||
const unlock1 = locks.acquire('my-lock'); | ||
const unlock2 = locks.acquire('my-lock'); | ||
const unlock3 = locks.acquire('my-lock'); | ||
expect(!!unlock1).toBe(true); | ||
expect(!!unlock2).toBe(false); | ||
expect(!!unlock3).toBe(false); | ||
}); | ||
|
||
test('can acquire lock which was released', () => { | ||
const locks = new Locks({}); | ||
const unlock1 = locks.acquire('my-lock'); | ||
unlock1!(); | ||
const unlock2 = locks.acquire('my-lock'); | ||
expect(!!unlock1).toBe(true); | ||
expect(!!unlock2).toBe(true); | ||
}); | ||
|
||
test('can release lock before timeout', async () => { | ||
const locks = new Locks({}); | ||
const unlock1 = locks.acquire('my-lock', 100); | ||
const unlock2 = locks.acquire('my-lock'); | ||
expect(!!unlock1).toBe(true); | ||
expect(!!unlock2).toBe(false); | ||
const unlock3 = locks.acquire('my-lock'); | ||
expect(!!unlock3).toBe(false); | ||
await tick(2); | ||
const unlock4 = locks.acquire('my-lock'); | ||
expect(!!unlock4).toBe(false); | ||
unlock1!(); | ||
const unlock5 = locks.acquire('my-lock'); | ||
expect(!!unlock5).toBe(true); | ||
}); | ||
|
||
test('cannot release other expired lock', async () => { | ||
const locks = new Locks({}); | ||
const unlock1 = locks.acquire('my-lock', 20); | ||
expect(!!unlock1).toBe(true); | ||
expect(locks.isLocked('my-lock')).toBe(true); | ||
await tick(21); | ||
expect(locks.isLocked('my-lock')).toBe(false); | ||
const unlock2 = locks.acquire('my-lock', 1); | ||
expect(!!unlock2).toBe(true); | ||
expect(locks.isLocked('my-lock')).toBe(true); | ||
unlock1!(); | ||
expect(locks.isLocked('my-lock')).toBe(true); | ||
expect(locks.isLocked('my-lock')).toBe(true); | ||
expect(locks.isLocked('my-lock')).toBe(true); | ||
unlock2!(); | ||
expect(locks.isLocked('my-lock')).toBe(false); | ||
}); | ||
}); |