Skip to content

Commit

Permalink
Merge PR #1 from 'nodech/pkg-ci'
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Jun 27, 2023
2 parents e507823 + e92769f commit 49ca1de
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
},
"rules": {
"max-len": "off",
"prefer-arrow-callback": "off"
"prefer-arrow-callback": "off",
"no-return-assign": "off"
}
}
],
Expand Down
44 changes: 44 additions & 0 deletions .github/workflows/build.yml
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

43 changes: 43 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"author": "Christopher Jeffrey <chjjeffrey@gmail.com>",
"main": "./lib/bmutex.js",
"scripts": {
"lint": "eslint lib/ test/ || exit 0",
"lint": "eslint lib/ test/",
"test": "bmocha --reporter spec test/*-test.js"
},
"dependencies": {
Expand Down
Empty file removed test/.gitkeep
Empty file.
41 changes: 41 additions & 0 deletions test/lock-test.js
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);
}

0 comments on commit 49ca1de

Please sign in to comment.