Skip to content

Commit

Permalink
Add pollingWaitTimeMs to UpdatableOptions (#436)
Browse files Browse the repository at this point in the history
  • Loading branch information
maximpoleley authored Nov 12, 2023
1 parent 20d51a5 commit c7cf5c7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ Returns the current polling state of the consumer: `true` if it is actively poll
Updates the provided option with the provided value.
Please note that any update of the option `pollingWaitTimeMs` will take effect only on next polling cycle.
You can [find out more about this here](https://bbc.github.io/sqs-consumer/classes/Consumer.html#updateOption).
### Events
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ export interface ConsumerOptions {
export type UpdatableOptions =
| 'visibilityTimeout'
| 'batchSize'
| 'waitTimeSeconds';
| 'waitTimeSeconds'
| 'pollingWaitTimeMs';

export interface StopOptions {
/**
Expand Down
5 changes: 5 additions & 0 deletions src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ function validateOption(
throw new Error('waitTimeSeconds must be between 0 and 20.');
}
break;
case 'pollingWaitTimeMs':
if (value < 0) {
throw new Error('pollingWaitTimeMs must be greater than 0.');
}
break;
default:
if (strict) {
throw new Error(`The update ${option} cannot be updated`);
Expand Down
28 changes: 28 additions & 0 deletions test/tests/consumer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1551,6 +1551,34 @@ describe('Consumer', () => {
sandbox.assert.notCalled(optionUpdatedListener);
});

it('updates the pollingWaitTimeMs option and emits an event', () => {
const optionUpdatedListener = sandbox.stub();
consumer.on('option_updated', optionUpdatedListener);

consumer.updateOption('pollingWaitTimeMs', 1000);

assert.equal(consumer.pollingWaitTimeMs, 1000);

sandbox.assert.calledWithMatch(
optionUpdatedListener,
'pollingWaitTimeMs',
1000
);
});

it('does not update the pollingWaitTimeMs if the value is less than 0', () => {
const optionUpdatedListener = sandbox.stub();
consumer.on('option_updated', optionUpdatedListener);

assert.throws(() => {
consumer.updateOption('pollingWaitTimeMs', -1);
}, 'pollingWaitTimeMs must be greater than 0.');

assert.equal(consumer.pollingWaitTimeMs, 0);

sandbox.assert.notCalled(optionUpdatedListener);
});

it('throws an error for an unknown option', () => {
consumer = new Consumer({
region: REGION,
Expand Down

0 comments on commit c7cf5c7

Please sign in to comment.