2021-12-09 21:50:06 +01:00
|
|
|
import ClientInstanceService from '../../../lib/services/client-metrics/instance-service';
|
2021-08-12 15:04:37 +02:00
|
|
|
import { IClientApp } from '../../../lib/types/model';
|
2021-11-02 15:13:46 +01:00
|
|
|
import { secondsToMilliseconds } from 'date-fns';
|
2022-12-12 14:05:56 +01:00
|
|
|
import { createTestConfig } from '../../config/test-config';
|
|
|
|
import { IUnleashConfig } from '../../../lib/types';
|
2021-06-24 19:22:12 +02:00
|
|
|
|
2021-03-05 14:01:15 +01:00
|
|
|
const faker = require('faker');
|
|
|
|
const dbInit = require('../helpers/database-init');
|
|
|
|
const getLogger = require('../../fixtures/no-logger');
|
2021-04-29 10:21:29 +02:00
|
|
|
const { APPLICATION_CREATED } = require('../../../lib/types/events');
|
2021-03-05 14:01:15 +01:00
|
|
|
|
|
|
|
let stores;
|
2021-03-19 22:25:21 +01:00
|
|
|
let db;
|
2021-12-09 21:25:06 +01:00
|
|
|
let clientInstanceService;
|
2022-12-12 14:05:56 +01:00
|
|
|
let config: IUnleashConfig;
|
2021-05-28 11:10:24 +02:00
|
|
|
beforeAll(async () => {
|
2021-03-19 22:25:21 +01:00
|
|
|
db = await dbInit('client_metrics_service_serial', getLogger);
|
2021-03-05 14:01:15 +01:00
|
|
|
stores = db.stores;
|
2022-12-12 14:05:56 +01:00
|
|
|
config = createTestConfig({});
|
2021-11-02 15:13:46 +01:00
|
|
|
const bulkInterval = secondsToMilliseconds(0.5);
|
|
|
|
const announcementInterval = secondsToMilliseconds(2);
|
|
|
|
|
2021-12-09 21:25:06 +01:00
|
|
|
clientInstanceService = new ClientInstanceService(
|
2021-06-24 19:22:12 +02:00
|
|
|
stores,
|
2022-12-12 14:05:56 +01:00
|
|
|
config,
|
2021-11-02 15:13:46 +01:00
|
|
|
bulkInterval,
|
|
|
|
announcementInterval,
|
2021-06-24 19:22:12 +02:00
|
|
|
);
|
2021-03-05 14:01:15 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
afterAll(async () => {
|
2021-12-09 21:25:06 +01:00
|
|
|
await clientInstanceService.destroy();
|
2021-03-19 22:25:21 +01:00
|
|
|
await db.destroy();
|
2021-03-05 14:01:15 +01:00
|
|
|
});
|
2021-05-28 11:10:24 +02:00
|
|
|
test('Apps registered should be announced', async () => {
|
|
|
|
expect.assertions(3);
|
2021-06-24 19:22:12 +02:00
|
|
|
const clientRegistration: IClientApp = {
|
2021-03-05 14:01:15 +01:00
|
|
|
appName: faker.internet.domainName(),
|
2021-05-21 19:30:57 +02:00
|
|
|
instanceId: faker.datatype.uuid(),
|
2021-03-05 14:01:15 +01:00
|
|
|
strategies: ['default'],
|
|
|
|
started: Date.now(),
|
2021-05-21 19:30:57 +02:00
|
|
|
interval: faker.datatype.number(),
|
2021-03-05 14:01:15 +01:00
|
|
|
icon: '',
|
|
|
|
description: faker.company.catchPhrase(),
|
|
|
|
color: faker.internet.color(),
|
|
|
|
};
|
|
|
|
const differentClient = {
|
2022-06-23 08:10:20 +02:00
|
|
|
appName: faker.datatype.uuid(),
|
2021-05-21 19:30:57 +02:00
|
|
|
instanceId: faker.datatype.uuid(),
|
2021-03-05 14:01:15 +01:00
|
|
|
strategies: ['default'],
|
|
|
|
started: Date.now(),
|
2021-05-21 19:30:57 +02:00
|
|
|
interval: faker.datatype.number(),
|
2021-03-05 14:01:15 +01:00
|
|
|
icon: '',
|
|
|
|
description: faker.company.catchPhrase(),
|
|
|
|
color: faker.internet.color(),
|
|
|
|
};
|
2021-12-09 21:25:06 +01:00
|
|
|
await clientInstanceService.registerClient(clientRegistration, '127.0.0.1');
|
|
|
|
await clientInstanceService.registerClient(differentClient, '127.0.0.1');
|
2021-08-12 15:04:37 +02:00
|
|
|
await new Promise((res) => setTimeout(res, 1200));
|
2021-03-05 14:01:15 +01:00
|
|
|
const first = await stores.clientApplicationsStore.getUnannounced();
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(first.length).toBe(2);
|
2021-12-09 21:25:06 +01:00
|
|
|
await clientInstanceService.registerClient(clientRegistration, '127.0.0.1');
|
2021-11-02 15:13:46 +01:00
|
|
|
await new Promise((res) => setTimeout(res, secondsToMilliseconds(2)));
|
2021-03-05 14:01:15 +01:00
|
|
|
const second = await stores.clientApplicationsStore.getUnannounced();
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(second.length).toBe(0);
|
2021-03-05 14:01:15 +01:00
|
|
|
const events = await stores.eventStore.getEvents();
|
2021-08-12 15:04:37 +02:00
|
|
|
const appCreatedEvents = events.filter(
|
|
|
|
(e) => e.type === APPLICATION_CREATED,
|
|
|
|
);
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(appCreatedEvents.length).toBe(2);
|
2021-03-05 14:01:15 +01:00
|
|
|
});
|