From f252ca80ef8d6703006f70531335ac928b052073 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kol=C3=A1rik?= Date: Mon, 6 Jan 2025 20:46:15 +0100 Subject: [PATCH] test: update tests --- test/setup.ts | 2 +- test/tests/unit/measurement/store.test.ts | 34 ++++++++++---------- test/tests/unit/ws/synced-probe-list.test.ts | 15 +++++---- 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/test/setup.ts b/test/setup.ts index e7c00e30..3b7789a2 100644 --- a/test/setup.ts +++ b/test/setup.ts @@ -46,7 +46,7 @@ before(async () => { const persistentRedisClient = await initPersistentRedisClient(); await persistentRedisClient.flushDb(); const measurementRedisClient = await initMeasurementRedisClient(); - await measurementRedisClient.flushDb(); + await measurementRedisClient.mapMasters(client => client.flushDb()); const subscriptionRedisClient = await initSubscriptionRedisClient(); await subscriptionRedisClient.flushDb(); diff --git a/test/tests/unit/measurement/store.test.ts b/test/tests/unit/measurement/store.test.ts index a0aa4f9a..e363f4c2 100644 --- a/test/tests/unit/measurement/store.test.ts +++ b/test/tests/unit/measurement/store.test.ts @@ -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({ @@ -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', @@ -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 () => { @@ -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', @@ -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', @@ -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', @@ -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 () => { @@ -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', @@ -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', @@ -546,19 +546,19 @@ 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', ]); @@ -566,7 +566,7 @@ describe('measurement store', () => { 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(), ]); diff --git a/test/tests/unit/ws/synced-probe-list.test.ts b/test/tests/unit/ws/synced-probe-list.test.ts index fb8c75e4..f8635f72 100644 --- a/test/tests/unit/ws/synced-probe-list.test.ts +++ b/test/tests/unit/ws/synced-probe-list.test.ts @@ -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', @@ -54,7 +55,7 @@ describe('SyncedProbeList', () => { let syncedProbeList: SyncedProbeList; - beforeEach(() => { + beforeEach(async () => { redisXRange.resolves([]); redisJsonGet.callThrough(); redisPExpire.callThrough(); @@ -62,6 +63,8 @@ describe('SyncedProbeList', () => { probeOverride.addAdminData.returnsArg(0); probeOverride.addAdoptedData.returnsArg(0); + subRedisClient = await initSubscriptionRedisClient(); + redisSubscribe = sandbox.stub(subRedisClient, 'subscribe'); syncedProbeList = new SyncedProbeList(redisClient, subRedisClient, ioNamespace, probeOverride); });