Skip to content

Commit

Permalink
Allow SDK to startup when backup data is corrupt (Unleash#418)
Browse files Browse the repository at this point in the history
fix: make the backup file start up sequence warn rather than fail when the JSON is corrupt
  • Loading branch information
sighphyre authored Jan 19, 2023
1 parent cc5b40e commit be165b5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/repository/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export default class Repository extends EventEmitter implements EventEmitter {
this.setReady();
}
} catch (err) {
this.emit(UnleashEvents.Error, err);
this.emit(UnleashEvents.Warn, err);
}
}

Expand Down
35 changes: 31 additions & 4 deletions test/global.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import nock from 'nock';
import { tmpdir } from 'os';
import { join } from 'path';
import mkdirp from 'mkdirp';
import { writeFileSync } from 'fs';
import { Unleash } from '../lib/unleash';

import { initialize, isEnabled, destroy } from '../lib/index';

Expand All @@ -24,9 +26,7 @@ let counter = 0;
function mockNetwork(toggles = defaultToggles) {
counter += 1;
const url = `http://unleash-${counter}.app`;
nock(url)
.get('/client/features')
.reply(200, { features: toggles });
nock(url).get('/client/features').reply(200, { features: toggles });
return url;
}

Expand All @@ -44,6 +44,33 @@ test('should be able to call api', (t) => {
destroy();
});

test.cb('should load if backup file is corrupted', (t) => {
const url = mockNetwork();
const backupPath = join(tmpdir());
const backupFile = join(backupPath, `/unleash-backup-with-corrupted-JSON.json`);
writeFileSync(backupFile, '{broken-json');

const instance = new Unleash({
appName: 'with-corrupted-JSON',
metricsInterval: 0,
url,
backupPath,
refreshInterval: 0,
});

instance
.on('error', (err) => {
destroy();
throw err;
})
.on('warn', (err) => {
if (err?.message?.includes('Unleash storage failed parsing file')) {
destroy();
t.end();
}
});
});

test.cb('should be able to call isEnabled eventually', (t) => {
const url = mockNetwork();
const instance = initialize({
Expand All @@ -69,4 +96,4 @@ test.cb('should return fallbackValue if init was not called', (t) => {
t.true(isEnabled('feature', {}, false) === false);
t.true(isEnabled('feature', {}, true) === true);
t.end();
})
});
4 changes: 2 additions & 2 deletions test/repository.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ test('bootstrap should not override load backup-file', async t => {
.persist()
.get('/client/features')
.reply(408);

nock(url)
.persist()
.get('/bootstrap')
Expand Down Expand Up @@ -734,4 +734,4 @@ test('bootstrap should not override load backup-file', async t => {
await repo.start();

t.is(repo.getToggle('feature-backup').enabled, true);
});
});

0 comments on commit be165b5

Please sign in to comment.