Skip to content

Commit

Permalink
feat(parameters): add clearCaches function (#1382)
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamorosi authored Mar 25, 2023
1 parent 051f24c commit ec49023
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
12 changes: 12 additions & 0 deletions packages/parameters/src/BaseProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,21 @@ const transformValues = (value: Record<string, string | undefined>, 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,
};
39 changes: 38 additions & 1 deletion packages/parameters/tests/unit/BaseProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import {
BaseProvider,
ExpirableValue,
GetParameterError,
TransformParameterError
TransformParameterError,
clearCaches,
DEFAULT_PROVIDERS,
} from '../../src';
import { toBase64 } from '@aws-sdk/util-base64-node';

Expand Down Expand Up @@ -480,4 +482,39 @@ describe('Class: BaseProvider', () => {

});

});

describe('Function: clearCaches', () => {

class TestProvider extends BaseProvider {

public _get(_name: string): Promise<string> {
throw Error('Not implemented.');
}

public _getMultiple(_path: string): Promise<Record<string, string | undefined>> {
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);

});

});

0 comments on commit ec49023

Please sign in to comment.