1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-28 00:17:12 +01:00

add more unit tests for metrics

This commit is contained in:
sveisvei 2016-11-13 18:14:29 +01:00 committed by Ivar Conradi Østhus
parent c8746b85f7
commit e730937849
5 changed files with 156 additions and 5 deletions

View File

@ -0,0 +1,113 @@
'use strict';
const { test } = require('ava');
const UnleashClientMetrics = require('./index');
const sinon = require('sinon');
const appName = 'appName';
const instanceId = 'instanceId';
test('should work without state', (t) => {
const metrics = new UnleashClientMetrics();
t.truthy(metrics.getMetricsOverview());
t.truthy(metrics.getTogglesMetrics());
metrics.destroy();
});
test('addPayload', t => {
const metrics = new UnleashClientMetrics();
metrics.addPayload({
appName,
instanceId,
bucket: {
start: new Date(),
stop: new Date(),
toggles: {
toggleX: {
yes: 123,
no: 0,
},
},
},
});
t.truthy(metrics.clients[instanceId].appName === appName);
t.truthy(metrics.clients[instanceId].count === 123);
t.truthy(metrics.globalCount === 123);
t.deepEqual(metrics.getTogglesMetrics().lastHour.toggleX, { yes: 123, no: 0 });
t.deepEqual(metrics.getTogglesMetrics().lastMinute.toggleX, { yes: 123, no: 0 });
metrics.addPayload({
appName,
instanceId,
bucket: {
start: new Date(),
stop: new Date(),
toggles: {
toggleX: {
yes: 10,
no: 10,
},
},
},
});
t.truthy(metrics.clients[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('addBucket', t => {
const metrics = new UnleashClientMetrics();
metrics.addClient(appName, instanceId);
metrics.addBucket(appName, instanceId, {
start: new Date(),
stop: new Date(),
toggles: {
toggleX: {
yes: 123,
no: 0,
},
},
});
t.truthy(metrics.clients[instanceId].count === 123);
t.truthy(metrics.globalCount === 123);
t.deepEqual(metrics.getTogglesMetrics().lastMinute.toggleX, { yes: 123, no: 0 });
metrics.destroy();
});
test('addClient', t => {
const metrics = new UnleashClientMetrics();
metrics.addClient(appName, instanceId);
metrics.addClient(appName, instanceId, new Date());
t.truthy(metrics.clients[instanceId].count === 0);
t.truthy(metrics.globalCount === 0);
metrics.destroy();
});
test('addApp', t => {
const metrics = new UnleashClientMetrics();
metrics.addApp(appName, instanceId);
t.truthy(metrics.apps[appName].clients.length === 1);
metrics.addApp(appName, 'instanceId2');
t.truthy(metrics.apps[appName].clients.length === 2);
metrics.addApp('appName2', 'instanceId2');
t.truthy(metrics.apps.appName2.clients.length === 1);
metrics.addApp('appName2', instanceId);
t.truthy(metrics.apps.appName2.clients.length === 2);
metrics.destroy();
});

View File

@ -8,7 +8,6 @@ module.exports = class UnleashClientMetrics {
this.globalCount = 0;
this.apps = {};
this.clients = {};
this.buckets = {};
this.lastHourProjection = new Projection();
this.lastMinuteProjection = new Projection();
@ -114,4 +113,9 @@ module.exports = class UnleashClientMetrics {
this.apps[appName].clients.push(instanceId);
}
}
destroy () {
this.lastHourList.destroy();
this.lastMinuteList.destroy();
}
};

View File

@ -0,0 +1,34 @@
'use strict';
const { test } = require('ava');
const Projection = require('./projection');
test('should return set empty if missing', t => {
const projection = new Projection();
projection.substract('name-1', { yes: 1, no: 2 });
t.deepEqual(projection.getProjection()['name-1'], { yes: 0, no: 0 });
});
test('should add and substract', t => {
const projection = new Projection();
t.truthy(projection.store);
projection.add('name-1', { yes: 1, no: 2 });
t.deepEqual(projection.getProjection()['name-1'], { yes: 1, no: 2 });
projection.add('name-1', { yes: 1, no: 2 });
t.deepEqual(projection.getProjection()['name-1'], { yes: 2, no: 4 });
projection.substract('name-1', { yes: 1, no: 2 });
t.deepEqual(projection.getProjection()['name-1'], { yes: 1, no: 2 });
projection.substract('name-1', { yes: 1, no: 2 });
t.deepEqual(projection.getProjection()['name-1'], { yes: 0, no: 0 });
projection.substract('name-2', { yes: 23213, no: 23213 });
projection.add('name-2', { yes: 3, no: 2 });
t.deepEqual(projection.getProjection()['name-2'], { yes: 3, no: 2 });
});

View File

@ -1,9 +1,9 @@
'use strict';
const test = require('ava');
const eventDiffer = require('../../lib/event-differ');
const eventType = require('../../lib/event-type');
const logger = require('../../lib/logger');
const eventDiffer = require('./event-differ');
const eventType = require('./event-type');
const logger = require('./logger');
test.beforeEach(() => {
logger.setLevel('FATAL');

View File

@ -39,7 +39,7 @@
"start:dev:pg-chain": "export DATABASE_URL=postgres://$PGUSER:$PGPASSWORD@localhost:$PGPORT/postgres ; db-migrate up && npm run start:dev",
"db-migrate": "db-migrate up",
"db-migrate:down": "db-migrate down",
"test": "PORT=4243 ava **/**test.js",
"test": "PORT=4243 ava **/**/*test.js",
"test:docker": "./scripts/docker-postgres.sh",
"test:watch": "npm run test -- --watch",
"test:pg-virtualenv": "pg_virtualenv npm run test:pg-virtualenv-chai",