Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Borewit committed Nov 4, 2023
1 parent 5178b19 commit c3f52d6
Show file tree
Hide file tree
Showing 6 changed files with 571 additions and 35 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/nodejs-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ jobs:
- name: Build
run: yarn run build

- name: Run unit test
run: yarn run test

- name: Upload build
uses: actions/upload-artifact@v2
with:
Expand All @@ -35,3 +38,34 @@ jobs:
src/**/*.js
src/**/*.js.map
src/**/*.d.ts
test:

runs-on: ubuntu-latest

needs: build

strategy:
matrix:
node-version: [14.x, 16.x, 18.x, 20.x]

steps:

- name: 'Checkout the repository'
uses: actions/checkout@v2

- name: Test with Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- name: Install dependencies
run: yarn install --ignore-engines

- name: Download build
uses: actions/download-artifact@v2
with:
name: build

- name: Test with Node.js ${{ matrix.node-version }}
run: yarn run test
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ node_modules

# Yarn
yarn-error.log

# Compiled JavaScript
src/**/*.js
src/**/*.d.ts
test/**/*.js
test/**/*.d.ts
7 changes: 7 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extension": ["ts", "tsx"],
"watch-files": ["lib/**/*.ts", "test/**/*.ts"],
"spec": ["test/*.ts"],
"loader": ["ts-node/esm"],
"extensions": ["ts", "tsx"]
}
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,29 @@
],
"contributors": [],
"scripts": {
"clean": "del-cli src/**/*.js src/**/*.js.map src/**/*.d.ts",
"clean": "del-cli src/**/*.js src/**/*.js.map src/**/*.d.ts test/**/*.js test/**/*.js.map test/**/*.d.ts",
"compile-src": "tsc -p src",
"build": "npm run clean && npm run compile-src",
"lint": "eslint '**/*.ts'"
"lint": "eslint src test --ext .ts",
"test": "mocha"
},
"devDependencies": {
"@types/chai": "^4.3.9",
"@types/mocha": "^10.0.3",
"@types/node": "^20.8.10",
"@typescript-eslint/eslint-plugin": "^6.9.1",
"@typescript-eslint/parser": "^6.9.1",
"chai": "^4.3.10",
"del-cli": "^5.1.0",
"eslint": "^8.52.0",
"eslint-config-prettier": "^9.0.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "^2.29.0",
"eslint-plugin-jsdoc": "^46.8.2",
"eslint-plugin-unicorn": "^49.0.0",
"mocha": "^10.0.0",
"npm-run-all": "^4.1.5",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
}
}
46 changes: 46 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {queue} from '../src/bounded-queue.js';
import {assert} from 'chai';

class MockProducer<T> {

constructor(private numberOfItems: number, private timeToProduce: number) {
}

async produce(): Promise<T | null> {
if (this.numberOfItems-- === 0) {
return null;
}
return new Promise(resolve => setTimeout(resolve, this.timeToProduce));
}
}

class MockConsumer<T> {

public itemsReceived: number = 0;

constructor(private timeToProduce: number) {
}

async consume(batchedItem: T): Promise<void> {
++this.itemsReceived;
return new Promise(resolve => setTimeout(resolve, this.timeToProduce));
}
}

describe('bounded-queue', () => {

it('slow consumer, fast consumer', async () => {
const producer = new MockProducer<object>(10, 50);
const consumer = new MockConsumer<object>(25);
await queue<object>(5, () => producer.produce(), item => consumer.consume(item));
assert.equal(consumer.itemsReceived, 10, "Consumer should receive all items");
});

it('fast consumer, slow consumer', async () => {
const producer = new MockProducer<object>(10, 25);
const consumer = new MockConsumer<object>(50);
await queue<object>(5, () => producer.produce(), item => consumer.consume(item));
assert.equal(consumer.itemsReceived, 10, "Consumer should receive all items");
});

});
Loading

0 comments on commit c3f52d6

Please sign in to comment.