Skip to content

Commit

Permalink
test(workerpool): coverage improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Jun 12, 2020
1 parent 75b28f6 commit baf7a4f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
6 changes: 5 additions & 1 deletion scripts/dummy_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ const { isMainThread, parentPort } = require('worker_threads');

if (isMainThread) throw new Error('It is not a worker, it is now at Main Thread.');

const job = ({ data = 10 }) => {
const job = ({ data = 10, error = false }) => {
if (error) {
throw new Error('There goes an Error!');
}

const start = new Date();
while (new Date() - start < data) { /* */ }

Expand Down
25 changes: 24 additions & 1 deletion test/workerpool.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

require('chai').should();

const { delay } = require('bluebird');

// Worker Thread is available since Node.js 10.5.0
// eslint-disable-next-line node/no-unsupported-features/node-builtins
const { Worker } = require('worker_threads');
Expand Down Expand Up @@ -37,18 +39,39 @@ describe('WorkerPool', () => {
Object.keys(pool2._activeWorkersById).length.should.eql(3);
});

it('init - numberOfThreads < 1', () => {
try {
// eslint-disable-next-line no-unused-vars
const pool = new WorkerPool(workerPath, 0);
} catch (e) {
e.message.should.eql('Number of threads should be greater or equal than 1!');
}
});

it('run() - this should show "(500ms)" =>', async () => {
const result = await pool.run({ data: 500 });
result.should.eql('Worker is Cool!');
});

it('run() - push jobs to queue', () => {
it('run() - push jobs to queue', async () => {
for (let i = 0; i < 10; i++) {
pool3.run({ data: 1000 });
}

pool3.getInactiveWorkerId().should.eql(-1);
pool3._queue.length.should.eql(8);

await delay(1000);

pool3._queue.length.should.lessThan(8);
});

it('run() - error handling', () => {
const pool = new WorkerPool(workerPath, 1);

pool.run({ error: true }).catch(e => {
e.message.should.eql('There goes an Error!');
});
});

it('destroy()', () => {
Expand Down

0 comments on commit baf7a4f

Please sign in to comment.