Skip to content

Commit

Permalink
test: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinKolarik committed Jan 6, 2025
1 parent 7f0a5c3 commit cd57cfe
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
2 changes: 1 addition & 1 deletion test/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ before(async () => {
const persistentRedisClient = await initPersistentRedisClient();
await persistentRedisClient.flushDb();
const measurementRedisClient = await initMeasurementRedisClient();
await measurementRedisClient.flushDb();
await measurementRedisClient.mapMasters<string>(client => client.flushDb());
const subscriptionRedisClient = await initSubscriptionRedisClient();
await subscriptionRedisClient.flushDb();

Expand Down
34 changes: 17 additions & 17 deletions test/tests/unit/measurement/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ describe('measurement store', () => {
expect(redisMock.hScan.callCount).to.equal(1);
expect(redisMock.hScan.firstCall.args).to.deep.equal([ 'gp:in-progress', 0, { COUNT: 5000 }]);
expect(redisMock.json.mGet.callCount).to.equal(1);
expect(redisMock.json.mGet.firstCall.args).to.deep.equal([ [ 'gp:m:id1:results', 'gp:m:id2:results' ], '.' ]);
expect(redisMock.json.mGet.firstCall.args).to.deep.equal([ [ 'gp:m:{id1}:results', 'gp:m:{id2}:results' ], '.' ]);
expect(redisMock.hDel.callCount).to.equal(1);
expect(redisMock.hDel.firstCall.args).to.deep.equal([ 'gp:in-progress', [ 'id1', 'id2' ] ]);
expect(redisMock.json.set.callCount).to.equal(1);

expect(redisMock.json.set.firstCall.args).to.have.lengthOf(3);
expect(redisMock.json.set.firstCall.args[0]).to.equal('gp:m:id1:results');
expect(redisMock.json.set.firstCall.args[0]).to.equal('gp:m:{id1}:results');
expect(redisMock.json.set.firstCall.args[1]).to.equal('$');

expect(redisMock.json.set.firstCall.args[2]).to.deep.include({
Expand Down Expand Up @@ -147,10 +147,10 @@ describe('measurement store', () => {
expect(redisMock.hSet.callCount).to.equal(1);
expect(redisMock.hSet.args[0]).to.deep.equal([ 'gp:in-progress', 'measurementid', now ]);
expect(redisMock.set.callCount).to.equal(1);
expect(redisMock.set.args[0]).to.deep.equal([ 'gp:m:measurementid:probes_awaiting', 4, { EX: 35 }]);
expect(redisMock.set.args[0]).to.deep.equal([ 'gp:m:{measurementid}:probes_awaiting', 4, { EX: 35 }]);
expect(redisMock.json.set.callCount).to.equal(2);

expect(redisMock.json.set.args[0]).to.deep.equal([ 'gp:m:measurementid:results', '$', {
expect(redisMock.json.set.args[0]).to.deep.equal([ 'gp:m:{measurementid}:results', '$', {
id: 'measurementid',
type: 'ping',
status: 'in-progress',
Expand Down Expand Up @@ -225,11 +225,11 @@ describe('measurement store', () => {
}],
}]);

expect(redisMock.expire.args[0]).to.deep.equal([ 'gp:m:measurementid:results', 604800 ]);
expect(redisMock.expire.args[0]).to.deep.equal([ 'gp:m:{measurementid}:results', 604800 ]);

expect(redisMock.json.set.args[1]).to.deep.equal([ 'gp:m:measurementid:ips', '$', [ '1.1.1.1', '2.2.2.2', '3.3.3.3', '4.4.4.4' ] ]);
expect(redisMock.json.set.args[1]).to.deep.equal([ 'gp:m:{measurementid}:ips', '$', [ '1.1.1.1', '2.2.2.2', '3.3.3.3', '4.4.4.4' ] ]);

expect(redisMock.expire.args[1]).to.deep.equal([ 'gp:m:measurementid:ips', 604800 ]);
expect(redisMock.expire.args[1]).to.deep.equal([ 'gp:m:{measurementid}:ips', 604800 ]);
});

it('should initialize measurement object with the proper default values', async () => {
Expand All @@ -249,7 +249,7 @@ describe('measurement store', () => {
);

expect(redisMock.json.set.firstCall.args).to.deep.equal([
'gp:m:measurementid:results',
'gp:m:{measurementid}:results',
'$',
{
id: 'measurementid',
Expand Down Expand Up @@ -307,7 +307,7 @@ describe('measurement store', () => {
);

expect(redisMock.json.set.firstCall.args).to.deep.equal([
'gp:m:measurementid:results',
'gp:m:{measurementid}:results',
'$',
{
id: 'measurementid',
Expand Down Expand Up @@ -361,7 +361,7 @@ describe('measurement store', () => {
);

expect(redisMock.json.set.firstCall.args).to.deep.equal([
'gp:m:measurementid:results',
'gp:m:{measurementid}:results',
'$',
{
id: 'measurementid',
Expand Down Expand Up @@ -395,7 +395,7 @@ describe('measurement store', () => {
},
]);

expect(redisMock.set.args[0]).to.deep.equal([ 'gp:m:measurementid:probes_awaiting', 0, { EX: 35 }]);
expect(redisMock.set.args[0]).to.deep.equal([ 'gp:m:{measurementid}:probes_awaiting', 0, { EX: 35 }]);
});

it('should store non-default fields of the measurement request', async () => {
Expand Down Expand Up @@ -431,7 +431,7 @@ describe('measurement store', () => {
[ getProbe('id', '1.1.1.1') ],
);

expect(redisMock.json.set.args[0]).to.deep.equal([ 'gp:m:measurementid:results', '$', {
expect(redisMock.json.set.args[0]).to.deep.equal([ 'gp:m:{measurementid}:results', '$', {
id: 'measurementid',
type: 'http',
status: 'in-progress',
Expand Down Expand Up @@ -498,7 +498,7 @@ describe('measurement store', () => {
[ getProbe('id', '1.1.1.1') ],
);

expect(redisMock.json.set.args[0]).to.deep.equal([ 'gp:m:measurementid:results', '$', {
expect(redisMock.json.set.args[0]).to.deep.equal([ 'gp:m:{measurementid}:results', '$', {
id: 'measurementid',
type: 'http',
status: 'in-progress',
Expand Down Expand Up @@ -546,27 +546,27 @@ describe('measurement store', () => {
expect(redisMock.json.strAppend.callCount).to.equal(3);

expect(redisMock.json.strAppend.firstCall.args).to.deep.equal([
'gp:m:measurementid:results',
'gp:m:{measurementid}:results',
'$.results[testid].result.rawHeaders',
'headers',
]);

expect(redisMock.json.strAppend.secondCall.args).to.deep.equal([
'gp:m:measurementid:results',
'gp:m:{measurementid}:results',
'$.results[testid].result.rawBody',
'body',
]);

expect(redisMock.json.strAppend.thirdCall.args).to.deep.equal([
'gp:m:measurementid:results',
'gp:m:{measurementid}:results',
'$.results[testid].result.rawOutput',
'output',
]);

expect(redisMock.json.set.callCount).to.equal(1);

expect(redisMock.json.set.firstCall.args).to.deep.equal([
'gp:m:measurementid:results',
'gp:m:{measurementid}:results',
'$.updatedAt',
new Date(now).toISOString(),
]);
Expand Down
15 changes: 9 additions & 6 deletions test/tests/unit/ws/synced-probe-list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@ import { type WsServerNamespace } from '../../../../src/lib/ws/server.js';
import { SyncedProbeList } from '../../../../src/lib/ws/synced-probe-list.js';
import type { Probe } from '../../../../src/probe/types.js';
import { getRegionByCountry } from '../../../../src/lib/location/location.js';
import { getRedisClient } from '../../../../src/lib/redis/client.js';
import { getRedisClient, RedisClient } from '../../../../src/lib/redis/client.js';
import { ProbeOverride } from '../../../../src/lib/override/probe-override.js';
import { getSubscriptionRedisClient } from '../../../../src/lib/redis/subscription-client.js';
import { initSubscriptionRedisClient } from '../../../../src/lib/redis/subscription-client.js';

describe('SyncedProbeList', () => {
const sandbox = sinon.createSandbox();
const redisClient = getRedisClient();
const subRedisClient = getSubscriptionRedisClient();
const redisClient = getRedisClient().duplicate();
let subRedisClient: RedisClient;
const localFetchSocketsStub = sandbox.stub();
const redisXAdd = sandbox.stub(redisClient, 'xAdd');
const redisXRange = sandbox.stub(redisClient, 'xRange');
const redisPExpire = sandbox.stub(redisClient, 'pExpire');
const redisJsonGet = sandbox.stub(redisClient.json, 'get');
const redisPublish = sandbox.stub(redisClient, 'publish');
const redisSubscribe = sandbox.stub(subRedisClient, 'subscribe');
let redisSubscribe: sinon.SinonStub;
redisClient.connect();

const idToIp = {
A: '1.1.1.1',
Expand Down Expand Up @@ -54,14 +55,16 @@ describe('SyncedProbeList', () => {

let syncedProbeList: SyncedProbeList;

beforeEach(() => {
beforeEach(async () => {
redisXRange.resolves([]);
redisJsonGet.callThrough();
redisPExpire.callThrough();
localFetchSocketsStub.resolves([]);
probeOverride.addAdminData.returnsArg(0);
probeOverride.addAdoptedData.returnsArg(0);

subRedisClient = await initSubscriptionRedisClient();
redisSubscribe = sandbox.stub(subRedisClient, 'subscribe');
syncedProbeList = new SyncedProbeList(redisClient, subRedisClient, ioNamespace, probeOverride);
});

Expand Down

0 comments on commit cd57cfe

Please sign in to comment.