-
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.
- Loading branch information
Showing
6 changed files
with
131 additions
and
2 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,44 @@ | ||
name: Build | ||
|
||
on: [push, pull_request] | ||
|
||
jobs: | ||
lint: | ||
name: Lint | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Setup | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: 20.x | ||
|
||
- name: Install tools | ||
run: npm install --location=global bslint | ||
|
||
- name: Install dependencies | ||
run: npm install | ||
|
||
- name: Lint | ||
run: npm run lint | ||
|
||
test: | ||
name: Test | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Setup | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: 20.x | ||
|
||
- name: Install dependencies | ||
run: npm install | ||
|
||
- name: Test | ||
run: npm run test | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Empty file.
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,41 @@ | ||
'use strict'; | ||
|
||
const assert = require('bsert'); | ||
const {Lock} = require('..'); | ||
|
||
describe('Lock', function() { | ||
it('should resolve queue in order', async () => { | ||
const genJob = (id) => { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve(id); | ||
}, randomRange(10, 100)); | ||
}); | ||
}; | ||
|
||
const locker = new Lock(); | ||
const results = []; | ||
const promises = []; | ||
|
||
const runJob = async (id) => { | ||
const unlock = await locker.lock(); | ||
|
||
try { | ||
results.push(await genJob(id)); | ||
} finally { | ||
unlock(); | ||
} | ||
}; | ||
|
||
for (let i = 0; i < 10; i++) | ||
promises.push(runJob(i)); | ||
|
||
await Promise.all(promises); | ||
|
||
assert.deepStrictEqual(results, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); | ||
}); | ||
}); | ||
|
||
function randomRange(min, max) { | ||
return Math.floor(Math.random() * (max - min) + min); | ||
} |