From e630c084250d54abecf831eced7170de1bc302d0 Mon Sep 17 00:00:00 2001 From: Maxim Poleley Date: Sun, 12 Nov 2023 22:36:51 +0100 Subject: [PATCH 1/2] Add pollingWaitTimeMs to UpdatableOptions --- README.md | 2 ++ src/types.ts | 3 ++- src/validation.ts | 5 +++++ test/tests/consumer.test.ts | 28 ++++++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 446616d7..2518d07c 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 update of `pollingWaitTimeMs` will take effect only on next poll 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 6a5eefa0..ebd37c49 100644 --- a/test/tests/consumer.test.ts +++ b/test/tests/consumer.test.ts @@ -1495,6 +1495,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, From 3849ee25e5d14d4b0e125d7e9f4776073d17dbe1 Mon Sep 17 00:00:00 2001 From: Maxim Poleley Date: Sun, 12 Nov 2023 22:45:03 +0100 Subject: [PATCH 2/2] Update README.md Co-authored-by: Nicholas Griffin --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2518d07c..c97c6d94 100644 --- a/README.md +++ b/README.md @@ -129,7 +129,7 @@ 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 update of `pollingWaitTimeMs` will take effect only on next poll cycle. +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).