Skip to content

Commit

Permalink
test: add test coverage for parallelLimit()
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Oct 5, 2019
1 parent 294c191 commit d19b849
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/helpers/parallelLimit.js
Expand Up @@ -11,11 +11,15 @@ function parallelLimit(fns, limit, callback) {
let numFinished = 0;
let error = null;

if (limit <= 0) {
throw new Error('Limit must be positive');
}

if (fns.length === 0) {
return callback(null, []);
}

for (let i = 0; i < fns.length && i <= limit; ++i) {
for (let i = 0; i < fns.length && i < limit; ++i) {
_start();
}

Expand Down
43 changes: 43 additions & 0 deletions test/parallelLimit.test.js
@@ -0,0 +1,43 @@
'use strict';

const assert = require('assert');
const parallelLimit = require('../lib/helpers/parallelLimit');

describe('parallelLimit', function() {
it('works with zero functions', function(done) {
parallelLimit([], 1, (err, res) => {
assert.ifError(err);
assert.deepEqual(res, []);
done();
});
});

it('executes functions in parallel', function(done) {
let called = 0;
const fns = [
cb => {
setTimeout(() => {
++called;
setTimeout(cb, 0);
}, 100);
},
cb => {
setTimeout(() => {
++called;
setTimeout(cb, 0);
}, 100);
},
cb => {
assert.equal(called, 2);
++called;
setTimeout(cb, 100);
}
];

parallelLimit(fns, 2, (err) => {
assert.ifError(err);
assert.equal(called, 3);
done();
});
});
});

0 comments on commit d19b849

Please sign in to comment.