-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
703 additions
and
1 deletion.
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,40 @@ | ||
import { Mutex } from '../../src/util/mutex.js' | ||
import assert from 'node:assert' | ||
|
||
describe('util/mutex.ts', () => { | ||
describe('Mutex', () => { | ||
it('serializes access to a critical section', async () => { | ||
let locked = false | ||
const mutex = new Mutex() | ||
const unlock = await mutex.lock() | ||
locked = true | ||
setTimeout(() => { | ||
unlock() | ||
locked = false | ||
}, 100) | ||
const unlock2 = await mutex.lock() | ||
assert.strictEqual(locked, false) | ||
unlock2() | ||
}) | ||
|
||
it('resolves in the order of lock calls', async () => { | ||
const mutex = new Mutex() | ||
let order = '' | ||
const promises: Array<Promise<any>> = [] | ||
promises.push(mutex.lock().then((unlock) => { | ||
order += '1' | ||
setTimeout(unlock, 50) | ||
})) | ||
promises.push(mutex.lock().then((unlock) => { | ||
order += '2' | ||
setTimeout(unlock, 50) | ||
})) | ||
promises.push(mutex.lock().then((unlock) => { | ||
order += '3' | ||
setTimeout(unlock, 50) | ||
})) | ||
await Promise.all(promises) | ||
assert.strictEqual(order, '123') | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.