Skip to content

Commit

Permalink
fix: stop storage save on repository stop
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew committed Nov 8, 2024
1 parent 5148fd1 commit c57cf1c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/repository/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ export default class Repository extends EventEmitter implements EventEmitter {
}

async save(response: ClientFeaturesResponse, fromApi: boolean): Promise<void> {
if (this.stopped) {
return;
}
if (fromApi) {
this.connected = true;
this.data = this.convertToMap(response.features);
Expand Down Expand Up @@ -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);
Expand Down
56 changes: 56 additions & 0 deletions src/test/repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<ClientFeaturesResponse> = 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);
});

0 comments on commit c57cf1c

Please sign in to comment.