Skip to content

Commit

Permalink
refactor: rename fetchsockets
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-yarmosh committed Oct 30, 2023
1 parent e147d24 commit a3fee7e
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/lib/adopted-probes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import _ from 'lodash';

import { scopedLogger } from './logger.js';
import { client } from './sql/client.js';
import { fetchConnectedSockets } from './ws/server.js';
import { fetchRawSockets } from './ws/server.js';
import type { Probe } from '../probe/types.js';

const logger = scopedLogger('adopted-probes');
Expand Down Expand Up @@ -68,7 +68,7 @@ export class AdoptedProbes {

constructor (
private readonly sql: Knex,
private readonly fetchWsSockets: typeof fetchConnectedSockets,
private readonly fetchSockets: typeof fetchRawSockets,
) {}

getAdoptedIpToProbe () {
Expand All @@ -84,7 +84,7 @@ export class AdoptedProbes {
}

async syncDashboardData () {
const allSockets = await this.fetchWsSockets();
const allSockets = await this.fetchSockets();
this.connectedIpToProbe = new Map(allSockets.map(socket => [ socket.data.probe.ipAddress, socket.data.probe ]));
this.connectedUuidToIp = new Map(allSockets.map(socket => [ socket.data.probe.uuid, socket.data.probe.ipAddress ]));

Expand Down Expand Up @@ -196,4 +196,4 @@ export class AdoptedProbes {
}
}

export const adoptedProbes = new AdoptedProbes(client, fetchConnectedSockets);
export const adoptedProbes = new AdoptedProbes(client, fetchRawSockets);
6 changes: 3 additions & 3 deletions src/lib/ws/fetch-sockets.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import config from 'config';
import { throttle, LRUOptions } from './helper/throttle.js';
import { fetchConnectedSockets, RemoteProbeSocket } from './server.js';
import { fetchRawSockets, RemoteProbeSocket } from './server.js';
import { adoptedProbes } from '../adopted-probes.js';

const throttledFetchSockets = throttle<RemoteProbeSocket[]>(
async () => {
const connected = await fetchConnectedSockets();
const connected = await fetchRawSockets();
const adopted = adoptedProbes.getAdoptedIpToProbe();
console.log('adopted', adopted.size);
console.log('adopted', adopted);
return connected;
},
config.get<number>('ws.fetchSocketsCacheTTL'),
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ws/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const getWsServer = (): WsServer => {
return io;
};

export const fetchConnectedSockets = async () => {
export const fetchRawSockets = async () => {
if (!io) {
throw new Error('WS server not initialized yet');
}
Expand Down
8 changes: 4 additions & 4 deletions test/tests/unit/ws/fetch-sockets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import * as sinon from 'sinon';
import * as td from 'testdouble';
import { expect } from 'chai';

const fetchConnectedSockets = sinon.stub();
const fetchRawSockets = sinon.stub();

describe('fetchSockets', () => {
let fetchSockets;

before(async () => {
await td.replaceEsm('../../../../src/lib/ws/server.ts', {
fetchConnectedSockets,
fetchRawSockets,
});

({ fetchSockets } = await import('../../../../src/lib/ws/fetch-sockets.js'));
Expand All @@ -20,14 +20,14 @@ describe('fetchSockets', () => {
});

it('multiple calls to fetchSockets should result in one socket.io fetchSockets call', async () => {
expect(fetchConnectedSockets.callCount).to.equal(0);
expect(fetchRawSockets.callCount).to.equal(0);

await Promise.all([
fetchSockets(),
fetchSockets(),
fetchSockets(),
]);

expect(fetchConnectedSockets.callCount).to.equal(1);
expect(fetchRawSockets.callCount).to.equal(1);
});
});
4 changes: 2 additions & 2 deletions test/tests/unit/ws/reconnect-probes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { expect } from 'chai';
const disconnect = sinon.stub();
const fetchSockets = sinon.stub().resolves([{ disconnect }, { disconnect }]);

describe('fetchSockets', () => {
describe('reconnectProbes', () => {
let sandbox: sinon.SinonSandbox;
let reconnectProbes;

Expand All @@ -29,7 +29,7 @@ describe('fetchSockets', () => {
td.reset();
});

it('multiple calls to fetchSockets should result in one socket.io fetchSockets call', async () => {
it('should disconnect every probe in configured time', async () => {
reconnectProbes();

expect(fetchSockets.callCount).to.equal(0);
Expand Down

0 comments on commit a3fee7e

Please sign in to comment.