From ec49023c44c3873ba5396a45ee9b2a8ee031e84b Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Sat, 25 Mar 2023 20:18:31 +0100 Subject: [PATCH] feat(parameters): add `clearCaches` function (#1382) --- packages/parameters/src/BaseProvider.ts | 12 ++++++ .../tests/unit/BaseProvider.test.ts | 39 ++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/packages/parameters/src/BaseProvider.ts b/packages/parameters/src/BaseProvider.ts index e9456d8e00..865483d810 100644 --- a/packages/parameters/src/BaseProvider.ts +++ b/packages/parameters/src/BaseProvider.ts @@ -220,9 +220,21 @@ const transformValues = (value: Record, transform: T return transformedValues; }; +/** + * Utility function to clear all the caches of the default providers. + * + * This is useful when you want to clear the cache of all the providers at once, for example during testing. + */ +const clearCaches = (): void => { + for (const provider of Object.values(DEFAULT_PROVIDERS)) { + provider.clearCache(); + } +}; + export { BaseProvider, ExpirableValue, transformValue, DEFAULT_PROVIDERS, + clearCaches, }; \ No newline at end of file diff --git a/packages/parameters/tests/unit/BaseProvider.test.ts b/packages/parameters/tests/unit/BaseProvider.test.ts index 186e0c1cdc..71f5637e2a 100644 --- a/packages/parameters/tests/unit/BaseProvider.test.ts +++ b/packages/parameters/tests/unit/BaseProvider.test.ts @@ -7,7 +7,9 @@ import { BaseProvider, ExpirableValue, GetParameterError, - TransformParameterError + TransformParameterError, + clearCaches, + DEFAULT_PROVIDERS, } from '../../src'; import { toBase64 } from '@aws-sdk/util-base64-node'; @@ -480,4 +482,39 @@ describe('Class: BaseProvider', () => { }); +}); + +describe('Function: clearCaches', () => { + + class TestProvider extends BaseProvider { + + public _get(_name: string): Promise { + throw Error('Not implemented.'); + } + + public _getMultiple(_path: string): Promise> { + throw Error('Not implemented.'); + } + + } + + test('when called, it clears all the caches', () => { + + // Prepare + const provider1 = new TestProvider(); + const provider2 = new TestProvider(); + const provider1Spy = jest.spyOn(provider1, 'clearCache'); + const provider2Spy = jest.spyOn(provider2, 'clearCache'); + DEFAULT_PROVIDERS.ssm = provider1; + DEFAULT_PROVIDERS.secretsManager = provider2; + + // Act + clearCaches(); + + // Assess + expect(provider1Spy).toBeCalledTimes(1); + expect(provider2Spy).toBeCalledTimes(1); + + }); + }); \ No newline at end of file