Skip to content

Commit

Permalink
test: add more adopted sockets tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-yarmosh committed Nov 7, 2023
1 parent 855fcc7 commit c48e9dc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { populateMemList as populateIpWhiteList } from './geoip/whitelist.js';
import { populateCitiesList } from './geoip/city-approximation.js';
import { adoptedProbes } from './adopted-probes.js';
import { reconnectProbes } from './ws/helper/reconnect-probes.js';
import { getWsServer } from './ws/server.js';

export const createServer = async (): Promise<Server> => {
await initRedis();
Expand All @@ -28,7 +29,6 @@ export const createServer = async (): Promise<Server> => {

reconnectProbes();

const { getWsServer } = await import('./ws/server.js');
const { getHttpServer } = await import('./http/server.js');

const httpServer = getHttpServer();
Expand Down
21 changes: 21 additions & 0 deletions test/tests/unit/probe/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -819,5 +819,26 @@ describe('probe router', () => {

expect(probes.length).to.equal(0);
});

it('should return match for user tag', async () => {
const socket = await buildSocket(String(Date.now()), location);
socket.data.probe.tags = [
...socket.data.probe.tags,
{ type: 'user', value: 'u-jimaek-dashboardtag' },
];

const sockets: DeepPartial<RemoteProbeSocket[]> = [ socket ];

const locations: Location[] = [
{ tags: [ 'u-jimaek-dashboardtag' ] },
];

fetchSocketsMock.resolves(sockets as never);

const probes = await router.findMatchingProbes(locations, 100);

expect(probes.length).to.equal(1);
expect(probes[0]!.location.country).to.equal('GB');
});
});
});
19 changes: 14 additions & 5 deletions test/tests/unit/ws/fetch-sockets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ const getUpdatedTags = sinon.stub();
describe('fetchSockets', () => {
let fetchSockets: (options?: LRUOptions) => Promise<RemoteProbeSocket[]>;

before(async () => {
beforeEach(async () => {
sinon.resetHistory();
td.reset();
await td.replaceEsm('../../../../src/lib/ws/server.ts', { fetchRawSockets });
await td.replaceEsm('../../../../src/lib/adopted-probes.ts', { adoptedProbes: { getByIp, getUpdatedLocation, getUpdatedTags } });
});

beforeEach(async () => {
({ fetchSockets } = await import('../../../../src/lib/ws/fetch-sockets.js'));
sinon.resetHistory();
});

after(() => {
Expand Down Expand Up @@ -162,6 +160,17 @@ describe('fetchSockets', () => {
]);
});

it('should return same socket object if there is no adoption data or it is not edited', async () => {
const socket1 = { id: '1', data: { probe: { ipAddress: '' } } };
const socket2 = { id: '2', data: { probe: { ipAddress: '' } } };
fetchRawSockets.resolves([ socket1, socket2 ]);
getByIp.onCall(0).returns(undefined);
getByIp.onCall(1).returns({ isCustomCity: false, tags: [] });
const result = await fetchSockets();
expect(result[0]).to.equal(socket1);
expect(result[1]).to.equal(socket2);
});

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

Expand Down

0 comments on commit c48e9dc

Please sign in to comment.