test.cb('sanity check: child_process.exec also has killed.false if killed indirectly', t => { const {pid} = childProcess.exec('noop', error => { t.truthy(error); t.false(error.killed); t.end(); }); process.kill(pid, 'SIGINT'); });
test.cb('should emit error when non-existent target backupPath', t => { const storage = new Storage({ backupPath: join(tmpdir(), `random-${Math.round(Math.random() * 10000)}`), appName: 'test', }); storage.reset({ random: Math.random() }); storage.on('error', err => { t.truthy(err); t.true(err.code === 'ENOENT'); t.end(); }); });
test.cb('should emit error when stored data is invalid', t => { const dir = join(tmpdir(), `random-${Math.round(Math.random() * 10123000)}`); mkdirp.sync(dir); writeFileSync(join(dir, 'unleash-repo-schema-v1-test.json'), '{invalid: json, asd}', 'utf8'); const storage = new Storage({ backupPath: dir, appName: 'test', }); storage.on('persisted', console.log); storage.on('error', err => { t.truthy(err); t.regex(err.message, /Unexpected token/); t.end(); }); });
test.cb('registerInstance should emit error when request error', t => { const url = 'http://metrics1.app/'; const metrics = new Metrics({ url, }); metrics.on('error', e => { t.truthy(e); t.end(); }); metrics.registerInstance().then(result => { t.true(result); t.end(); }); });
test.cb('flush data', function(t) { const buf1 = Buffer.from('Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'); const buf2 = Buffer.from('Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.'); const stream = brotli.compressStream(); const writeStream = new BufferWriter(); stream .pipe(brotli.decompressStream()) .pipe(writeStream); stream.write(buf1); stream.flush(); stream.once('data', function() { stream.end(buf2); }); writeStream.on('finish', function() { t.truthy(writeStream.data.equals(Buffer.concat([buf1, buf2]))); t.end(); }); });
test.cb('should handle old url', t => { const url = mockNetwork([]); const instance = new Unleash({ appName: 'foo', refreshInterval: 0, metricsInterval: 0, disableMetrics: true, url: `${url}features`, }); t.plan(1); instance.on('warn', e => { t.truthy(e); t.end(); }); instance.destroy(); });
test.cb('should handle 404 request error and emit error event', t => { const url = 'http://unleash-test-5.app'; nock(url) .persist() .get('/client/features') .reply(404, 'asd'); const repo = new Repository({ backupPath: 'foo', url, appName, instanceId, refreshInterval: 0, StorageImpl: MockStorage, }); repo.on('error', err => { t.truthy(err); t.true(err.message.startsWith('Response was not statusCode 2')); t.end(); }); });
t.truthy(featureToggle); t.end(); });
test.cb('repository should surface error when invalid basePath', t => { const url = 'http://unleash-surface.app/'; nock(url) .get('/client/features') .delay(100) .reply(200, { features: [] }); const backupPath = join(tmpdir(), `test-tmp-${Math.round(Math.random() * 100000)}`); const instance = new Unleash({ appName: 'foo', disableMetrics: true, refreshInterval: 0, url, backupPath, }); instance.once('error', err => { t.truthy(err); t.true(err.code === 'ENOENT'); instance.destroy(); t.end(); }); });
test.cb('should emit network errors', t => { t.plan(3); const backupPath = join(tmpdir(), `test-tmp-${Math.round(Math.random() * 100000)}`); const unleash = new Unleash({ appName: 'network', url: 'http://blocked.app', refreshInterval: 20000, metricsInterval: 20000, disableMetrics: false, backupPath, }); unleash.on('error', e => { t.truthy(e); }); unleash.isEnabled('some-toggle'); unleash.metrics.sendMetrics(); setTimeout(() => { unleash.destroy(); process.nextTick(() => { t.end(); }); }, 5); });
test.cb('sendMetrics should emit error when request error', t => { const url = 'http://metrics2.app/'; const metrics = new Metrics({ url, }); metrics.on('error', e => { t.truthy(e); t.end(); }); metrics.count('x', true); metrics.sendMetrics().then(result => { t.true(result); t.end(); }); });
t.truthy(e); t.end(); });
t.truthy(err); t.end(); });