From c57cf1c2e0585d083a931bc1665e61c3e44ca59d Mon Sep 17 00:00:00 2001 From: kwasniew Date: Fri, 8 Nov 2024 13:05:02 +0100 Subject: [PATCH] fix: stop storage save on repository stop --- src/repository/index.ts | 4 +++ src/test/repository.test.ts | 56 +++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/src/repository/index.ts b/src/repository/index.ts index 34a05db9..f5a3e7d1 100644 --- a/src/repository/index.ts +++ b/src/repository/index.ts @@ -194,6 +194,9 @@ export default class Repository extends EventEmitter implements EventEmitter { } async save(response: ClientFeaturesResponse, fromApi: boolean): Promise { + if (this.stopped) { + return; + } if (fromApi) { this.connected = true; this.data = this.convertToMap(response.features); @@ -353,6 +356,7 @@ Message: ${err.message}`, httpOptions: this.httpOptions, supportedSpecVersion: SUPPORTED_SPEC_VERSION, }); + if (res.status === 304) { // No new data this.emit(UnleashEvents.Unchanged); diff --git a/src/test/repository.test.ts b/src/test/repository.test.ts index 5c7271b1..ca0bb887 100644 --- a/src/test/repository.test.ts +++ b/src/test/repository.test.ts @@ -8,6 +8,8 @@ import InMemStorageProvider from '../repository/storage-provider-in-mem'; import FileStorageProvider from '../repository/storage-provider-file'; import Repository from '../repository'; import { DefaultBootstrapProvider } from '../repository/bootstrap-provider'; +import { StorageProvider } from '../repository/storage-provider'; +import { ClientFeaturesResponse } from '../feature'; const appName = 'foo'; const instanceId = 'bar'; @@ -1304,3 +1306,57 @@ test('should return full segment data when requested', (t) => repo.on('error', () => {}); repo.start(); })); + +test('Stopping repository should stop unchanged event reporting', async (t) => { + t.plan(0); + const url = 'http://unleash-test-stop-304.app'; + nock(url).persist().get('/client/features').reply(304, ''); + const repo = new Repository({ + url, + appName, + instanceId, + refreshInterval: 10, + // @ts-expect-error + bootstrapProvider: new DefaultBootstrapProvider({}), + storageProvider: new InMemStorageProvider(), + }); + repo.on('unchanged', () => { + t.fail('Should not emit unchanged event after stopping'); + }); + + const promise = repo.fetch(); + repo.stop(); // remove all listeners + await promise; +}); + +test('Stopping repository should stop storage provider updates', async (t) => { + t.plan(1); + const url = 'http://unleash-test-stop-200.app'; + const feature = { + name: 'feature', + enabled: true, + strategies: [ + { + name: 'default', + }, + ], + }; + setup(url, [feature]); + const storageProvider: StorageProvider = new InMemStorageProvider(); + const repo = new Repository({ + url, + appName, + instanceId, + refreshInterval: 10, + // @ts-expect-error + bootstrapProvider: new DefaultBootstrapProvider({}), + storageProvider, + }); + + const promise = repo.fetch(); + repo.stop(); // stop storage provider from accepting commands + await promise; + + const result = await storageProvider.get(appName); + t.is(result, undefined); +});