Skip to content

Commit

Permalink
fix(worker): throw exception with NaN as concurrency (#2184)
Browse files Browse the repository at this point in the history
  • Loading branch information
manast committed Sep 20, 2023
1 parent 28c817f commit f36ac8b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/classes/worker.ts
Expand Up @@ -350,8 +350,12 @@ export class Worker<
}

set concurrency(concurrency: number) {
if (typeof concurrency !== 'number' || concurrency < 1) {
throw new Error('concurrency must be a number greater than 0');
if (
typeof concurrency !== 'number' ||
concurrency < 1 ||
!isFinite(concurrency)
) {
throw new Error('concurrency must be a finite number greater than 0');
}
this.opts.concurrency = concurrency;
}
Expand Down Expand Up @@ -595,6 +599,8 @@ export class Worker<
}
}

// Update limitUntil and delayUntil
// TODO: Refactor out of this function
this.limitUntil = Math.max(limitUntil, 0) || 0;
if (delayUntil) {
this.blockUntil = Math.max(delayUntil, 0) || 0;
Expand Down
16 changes: 15 additions & 1 deletion tests/test_worker.ts
Expand Up @@ -1631,7 +1631,21 @@ describe('workers', function () {
throw new Error('Should have thrown an exception');
} catch (err) {
expect(err.message).to.be.equal(
'concurrency must be a number greater than 0',
'concurrency must be a finite number greater than 0',
);
}
});

it('should thrown an exception if I specify a NaN concurrency', () => {
try {
const worker = new Worker(queueName, async () => {}, {
connection,
concurrency: NaN,
});
throw new Error('Should have thrown an exception');
} catch (err) {
expect(err.message).to.be.equal(
'concurrency must be a finite number greater than 0',
);
}
});
Expand Down

0 comments on commit f36ac8b

Please sign in to comment.