2016-11-28 17:11:11 +01:00
|
|
|
'use strict';
|
|
|
|
|
2018-12-17 09:24:49 +01:00
|
|
|
const test = require('ava');
|
2019-08-04 11:10:51 +02:00
|
|
|
const { EventEmitter } = require('events');
|
2016-11-28 17:11:11 +01:00
|
|
|
const ClientMetricStore = require('./client-metrics-store');
|
2017-08-23 11:21:34 +02:00
|
|
|
const lolex = require('lolex');
|
2019-04-30 21:14:23 +02:00
|
|
|
const getLogger = require('../../test/fixtures/no-logger');
|
2016-11-28 17:11:11 +01:00
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
function getMockDb() {
|
2016-12-04 14:09:37 +01:00
|
|
|
const list = [
|
|
|
|
{ id: 4, metrics: { appName: 'test' } },
|
|
|
|
{ id: 3, metrics: { appName: 'test' } },
|
|
|
|
{ id: 2, metrics: { appName: 'test' } },
|
|
|
|
];
|
2016-11-28 17:11:11 +01:00
|
|
|
return {
|
2017-06-28 10:17:14 +02:00
|
|
|
getMetricsLastHour() {
|
2016-12-04 14:09:37 +01:00
|
|
|
return Promise.resolve([{ id: 1, metrics: { appName: 'test' } }]);
|
2016-11-28 17:11:11 +01:00
|
|
|
},
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
getNewMetrics() {
|
2016-11-28 17:11:11 +01:00
|
|
|
return Promise.resolve([list.pop() || { id: 0 }]);
|
2016-12-04 14:09:37 +01:00
|
|
|
},
|
2016-11-28 17:11:11 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
test.cb('should call database on startup', t => {
|
2016-11-28 17:11:11 +01:00
|
|
|
const mock = getMockDb();
|
2019-08-04 11:10:51 +02:00
|
|
|
const ee = new EventEmitter();
|
|
|
|
const store = new ClientMetricStore(mock, ee, getLogger);
|
2016-12-04 14:09:37 +01:00
|
|
|
|
2016-11-28 17:11:11 +01:00
|
|
|
t.plan(2);
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
store.on('metrics', metrics => {
|
2016-11-28 17:11:11 +01:00
|
|
|
t.true(store.highestIdSeen === 1);
|
|
|
|
t.true(metrics.appName === 'test');
|
|
|
|
store.destroy();
|
2016-12-04 14:09:37 +01:00
|
|
|
|
2016-11-28 17:11:11 +01:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-03-07 21:31:15 +01:00
|
|
|
test.cb('should start poller even if inital database fetch fails', t => {
|
|
|
|
const clock = lolex.install();
|
|
|
|
const mock = getMockDb();
|
|
|
|
mock.getMetricsLastHour = () => Promise.reject('oops');
|
2019-08-04 11:10:51 +02:00
|
|
|
const ee = new EventEmitter();
|
|
|
|
const store = new ClientMetricStore(mock, ee, getLogger, 100);
|
2019-03-07 21:31:15 +01:00
|
|
|
|
|
|
|
const metrics = [];
|
|
|
|
store.on('metrics', m => metrics.push(m));
|
|
|
|
|
|
|
|
store.on('ready', () => {
|
|
|
|
t.true(metrics.length === 0);
|
|
|
|
clock.tick(300);
|
|
|
|
process.nextTick(() => {
|
|
|
|
t.true(metrics.length === 3);
|
|
|
|
t.true(store.highestIdSeen === 4);
|
|
|
|
store.destroy();
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
test.cb('should poll for updates', t => {
|
2017-08-23 11:21:34 +02:00
|
|
|
const clock = lolex.install();
|
2016-11-28 17:11:11 +01:00
|
|
|
|
|
|
|
const mock = getMockDb();
|
2019-08-04 11:10:51 +02:00
|
|
|
const ee = new EventEmitter();
|
|
|
|
const store = new ClientMetricStore(mock, ee, getLogger, 100);
|
2016-11-28 17:11:11 +01:00
|
|
|
|
|
|
|
const metrics = [];
|
2017-06-28 10:17:14 +02:00
|
|
|
store.on('metrics', m => metrics.push(m));
|
2016-11-28 17:11:11 +01:00
|
|
|
|
|
|
|
t.true(metrics.length === 0);
|
|
|
|
|
|
|
|
store.on('ready', () => {
|
|
|
|
t.true(metrics.length === 1);
|
|
|
|
clock.tick(300);
|
|
|
|
process.nextTick(() => {
|
|
|
|
t.true(metrics.length === 4);
|
|
|
|
t.true(store.highestIdSeen === 4);
|
|
|
|
store.destroy();
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|