diff --git a/lib/client-metrics/client-metrics.test.js b/lib/client-metrics/client-metrics.test.js new file mode 100644 index 0000000000..4c1eee55a4 --- /dev/null +++ b/lib/client-metrics/client-metrics.test.js @@ -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(); +}); diff --git a/lib/client-metrics/index.js b/lib/client-metrics/index.js index 03f6281d43..2facdf45df 100644 --- a/lib/client-metrics/index.js +++ b/lib/client-metrics/index.js @@ -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(); + } }; diff --git a/lib/client-metrics/projection.test.js b/lib/client-metrics/projection.test.js new file mode 100644 index 0000000000..f5af642e18 --- /dev/null +++ b/lib/client-metrics/projection.test.js @@ -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 }); +}); diff --git a/test/unit/event-differ.test.js b/lib/event-differ.test.js similarity index 95% rename from test/unit/event-differ.test.js rename to lib/event-differ.test.js index aec3b5a0de..56d96b02b3 100644 --- a/test/unit/event-differ.test.js +++ b/lib/event-differ.test.js @@ -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'); diff --git a/package.json b/package.json index af9e52e2a3..69f088d6d4 100644 --- a/package.json +++ b/package.json @@ -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",