Skip to content

feat(parameters): add clearCaches function #1382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);

});

});