Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ivaosthu committed Nov 30, 2016
1 parent 4281c71 commit 5c563ae
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 107 deletions.
148 changes: 44 additions & 104 deletions lib/client-metrics/client-metrics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,26 @@ const { test } = require('ava');
const UnleashClientMetrics = require('./index');
const sinon = require('sinon');

const { EventEmitter } = require('events');

const appName = 'appName';
const instanceId = 'instanceId';

test('should work without state', (t) => {
const metrics = new UnleashClientMetrics();
const store = new EventEmitter();
const metrics = new UnleashClientMetrics(store);

t.truthy(metrics.getMetricsOverview());
t.truthy(metrics.getAppsWitToggles());
t.truthy(metrics.getTogglesMetrics());
t.truthy(metrics.toJSON());

metrics.destroy();
});

test.cb('data should expire', (t) => {
const clock = sinon.useFakeTimers();

const metrics = new UnleashClientMetrics();
const store = new EventEmitter();
const metrics = new UnleashClientMetrics(store);

metrics.addPayload({
appName,
Expand Down Expand Up @@ -59,58 +62,10 @@ test.cb('data should expire', (t) => {
t.end();
});

test.cb('instances should expire', t => {
const clock = sinon.useFakeTimers();

const metrics = new UnleashClientMetrics();

metrics.addPayload({
appName,
instanceId: 'i1',
bucket: {
start: new Date(),
stop: new Date(),
toggles: {
toggleX: {
yes: 123,
no: 0,
},
},
},
});

clock.tick(60 * 60 * 1000 + 1);

metrics.addPayload({
appName,
instanceId: 'i2',
bucket: {
start: new Date(),
stop: new Date(),
toggles: {
toggleX: {
yes: 10,
no: 10,
},
},
},
});


const overview = metrics.getMetricsOverview();

t.truthy(overview.apps[appName].instances.i1 === null);
t.truthy(overview.apps[appName].instances.i2 !== null);


metrics.destroy();
sinon.restore();
t.end();
});

test('addPayload', t => {
const metrics = new UnleashClientMetrics();
metrics.addPayload({
test('should listen to metrics from store', t => {
const store = new EventEmitter();
const metrics = new UnleashClientMetrics(store);
store.emit('metrics', {
appName,
instanceId,
bucket: {
Expand All @@ -125,7 +80,6 @@ test('addPayload', t => {
},
});

t.truthy(metrics.apps[appName].instances[instanceId].count === 123);
t.truthy(metrics.apps[appName].count === 123);
t.truthy(metrics.globalCount === 123);

Expand All @@ -148,17 +102,17 @@ test('addPayload', t => {
},
});

t.truthy(metrics.apps[appName].instances[instanceId].count === 143);
t.truthy(metrics.globalCount === 143);
t.deepEqual(metrics.getTogglesMetrics().lastHour.toggleX, { yes: 133, no: 10 });
t.deepEqual(metrics.getTogglesMetrics().lastMinute.toggleX, { yes: 133, no: 10 });

metrics.destroy();
});

test('addPayload should also increase feature toggles', t => {
const metrics = new UnleashClientMetrics();
metrics.addPayload({
test('should build up list of seend toggles when new metrics arrives', t => {
const store = new EventEmitter();
const metrics = new UnleashClientMetrics(store);
store.emit('metrics', {
appName,
instanceId,
bucket: {
Expand All @@ -177,57 +131,43 @@ test('addPayload should also increase feature toggles', t => {
},
});

t.truthy(metrics.globalCount === 223);
t.truthy(metrics.apps[appName].toggles.toggleX);
metrics.destroy();
});

const appToggles = metrics.getAppsWitToggles();
const togglesForApp = metrics.getSeenTogglesByAppName(appName);

t.truthy(appToggles.length === 1);
t.truthy(appToggles[0].seenToggles.length === 2);
t.truthy(appToggles[0].seenToggles.includes('toggleX'));
t.truthy(appToggles[0].seenToggles.includes('toggleY'));

test('addBucket', t => {
const metrics = new UnleashClientMetrics();
metrics.addInstance(appName, instanceId);
metrics.addBucket(appName, instanceId, {
start: new Date(),
stop: new Date(),
toggles: {
toggleX: {
yes: 123,
no: 0,
},
},
});
t.truthy(metrics.apps[appName].instances[instanceId].count === 123);
t.truthy(metrics.globalCount === 123);
t.deepEqual(metrics.getTogglesMetrics().lastMinute.toggleX, { yes: 123, no: 0 });

t.truthy(togglesForApp.length === 2);
t.truthy(togglesForApp.includes('toggleX'));
t.truthy(togglesForApp.includes('toggleY'));
metrics.destroy();
});

test('addClient', t => {
const metrics = new UnleashClientMetrics();

metrics.addInstance(appName, instanceId);
metrics.addInstance(appName, instanceId, new Date());

t.truthy(metrics.apps[appName].instances[instanceId].count === 0);
t.truthy(metrics.globalCount === 0);

metrics.destroy();
});
test('should handle a lot of toggles', t => {
const store = new EventEmitter();
const metrics = new UnleashClientMetrics(store);

test('addApp', t => {
const metrics = new UnleashClientMetrics();

metrics.addApp(appName, instanceId);
t.truthy(Object.keys(metrics.apps[appName].instances).length === 0);
metrics.addApp(appName, 'instanceId2');
t.truthy(Object.keys(metrics.apps[appName].instances).length === 0);
const toggleCounts = {};
for (let i=0; i<100; i++) {
toggleCounts[`toggle${i}`] = {yes: i, no: i}
}

store.emit('metrics', {
appName,
instanceId,
bucket: {
start: new Date(),
stop: new Date(),
toggles: toggleCounts,
},
});

metrics.addApp('appName2', 'instanceId2');
t.truthy(Object.keys(metrics.apps.appName2.instances).length === 0);
metrics.addApp('appName2', instanceId);
t.truthy(Object.keys(metrics.apps.appName2.instances).length === 0);
const seenToggles = metrics.getSeenTogglesByAppName(appName);

t.truthy(seenToggles.length === 100);
metrics.destroy();
});
});
7 changes: 7 additions & 0 deletions test/e2e/helpers/test-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ function createClientInstance (stores) {
started: Date.now(),
interval: 10,
},
{
appName: 'demo-seed-2',
instanceId: 'test-2',
strategies: ['default'],
started: Date.now(),
interval: 10,
},
].map(client => stores.clientInstanceStore.insert(client));
}

Expand Down
19 changes: 16 additions & 3 deletions test/e2e/metrics-api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,27 @@ test.serial('should get client strategies', async t => {
.then(destroy);
});

test.serial('should get client instances', async t => {
test.serial('should get application details', async t => {
const { request, destroy } = await setupApp('metrics_serial');
return request
.get('/api/client/instances')
.get('/api/client/applications/demo-seed')
.expect('Content-Type', /json/)
.expect((res) => {
t.true(res.status === 200);
t.true(res.body.length === 1);
t.true(res.body.appName === 'demo-seed');
t.true(res.body.instances.length === 1);
})
.then(destroy);
});

test.serial('should get list of applications', async t => {
const { request, destroy } = await setupApp('metrics_serial');
return request
.get('/api/client/applications')
.expect('Content-Type', /json/)
.expect((res) => {
t.true(res.status === 200);
t.true(res.body.applications.length === 2);
})
.then(destroy);
});
Expand Down

0 comments on commit 5c563ae

Please sign in to comment.