diff --git a/README.md b/README.md index 446616d7..c97c6d94 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/types.ts b/src/types.ts index d26492d1..c1fa5fed 100644 --- a/src/types.ts +++ b/src/types.ts @@ -124,7 +124,8 @@ export interface ConsumerOptions { export type UpdatableOptions = | 'visibilityTimeout' | 'batchSize' - | 'waitTimeSeconds'; + | 'waitTimeSeconds' + | 'pollingWaitTimeMs'; export interface StopOptions { /** diff --git a/src/validation.ts b/src/validation.ts index 7bab4660..5fe6abb8 100644 --- a/src/validation.ts +++ b/src/validation.ts @@ -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`); diff --git a/test/tests/consumer.test.ts b/test/tests/consumer.test.ts index 0e8c916c..4f0c9d67 100644 --- a/test/tests/consumer.test.ts +++ b/test/tests/consumer.test.ts @@ -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,