diff --git a/.eslintrc.json b/.eslintrc.json index 4db51ca..3de38b4 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -39,7 +39,8 @@ }, "rules": { "max-len": "off", - "prefer-arrow-callback": "off" + "prefer-arrow-callback": "off", + "no-return-assign": "off" } } ], diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..a6d167d --- /dev/null +++ b/.github/workflows/build.yml @@ -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 + diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..b6ebf8c --- /dev/null +++ b/package-lock.json @@ -0,0 +1,43 @@ +{ + "name": "bmutex", + "version": "0.1.6", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "bmutex", + "version": "0.1.6", + "license": "MIT", + "dependencies": { + "bsert": "~0.0.10" + }, + "devDependencies": { + "bmocha": "^2.1.0" + }, + "engines": { + "node": ">=8.0.0" + } + }, + "node_modules/bmocha": { + "version": "2.1.8", + "resolved": "https://registry.npmjs.org/bmocha/-/bmocha-2.1.8.tgz", + "integrity": "sha512-bog23Ckl9lRyBxrsi4FmX1rTz4d1WhHRpIA+q2lpoiXmNuroMHr1JUOU5sPiMZwvhLCxqffvWv3xCJC7PC126w==", + "dev": true, + "bin": { + "_bmocha": "bin/_bmocha", + "bmocha": "bin/bmocha" + }, + "engines": { + "node": ">=8.0.0" + } + }, + "node_modules/bsert": { + "version": "0.0.12", + "resolved": "https://registry.npmjs.org/bsert/-/bsert-0.0.12.tgz", + "integrity": "sha512-lUB0EMu4KhIf+VQ6RZJ7J3dFdohYSeta+gNgDi00Hi/t3k/W6xZlwm9PSSG0q7hJ2zW9Rsn5yaMPymETxroTRw==", + "engines": { + "node": ">=8.0.0" + } + } + } +} diff --git a/package.json b/package.json index 3f9a6d1..01d74b6 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "author": "Christopher Jeffrey ", "main": "./lib/bmutex.js", "scripts": { - "lint": "eslint lib/ test/ || exit 0", + "lint": "eslint lib/ test/", "test": "bmocha --reporter spec test/*-test.js" }, "dependencies": { diff --git a/test/.gitkeep b/test/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/test/lock-test.js b/test/lock-test.js new file mode 100644 index 0000000..35d6f95 --- /dev/null +++ b/test/lock-test.js @@ -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); +}