Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(worker): throw exception with NaN as concurrency #2184

Merged
merged 2 commits into from Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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