From 96e69d13be4a3d963f6a934b0f2f2fe472954723 Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Wed, 28 Aug 2024 10:06:31 +1000 Subject: [PATCH] test: fix unit tests --- ts/shared/env_vars.ts | 2 -- ts/test/session/unit/onion/GuardNodes_test.ts | 33 +++++++++---------- .../unit/onion/SnodePoolUpdate_test.ts | 8 ++--- .../unit/utils/job_runner/JobRunner_test.ts | 11 +++++-- 4 files changed, 28 insertions(+), 26 deletions(-) diff --git a/ts/shared/env_vars.ts b/ts/shared/env_vars.ts index 8be7390e5..8d95a89cd 100644 --- a/ts/shared/env_vars.ts +++ b/ts/shared/env_vars.ts @@ -5,8 +5,6 @@ function envAppInstanceIncludes(prefix: string) { return !!process.env.NODE_APP_INSTANCE.includes(prefix); } - - export function isDevProd() { return envAppInstanceIncludes('devprod'); } diff --git a/ts/test/session/unit/onion/GuardNodes_test.ts b/ts/test/session/unit/onion/GuardNodes_test.ts index a43680eb0..1295e8a69 100644 --- a/ts/test/session/unit/onion/GuardNodes_test.ts +++ b/ts/test/session/unit/onion/GuardNodes_test.ts @@ -14,6 +14,7 @@ import { } from '../../../test-utils/utils'; import { SeedNodeAPI } from '../../../../session/apis/seed_node_api'; import { Snode } from '../../../../data/types'; +import { minSnodePoolCount } from '../../../../session/apis/snode_api/snodePool'; chai.use(chaiAsPromised as any); chai.should(); @@ -51,7 +52,7 @@ describe('GuardNodes', () => { Sinon.restore(); }); - it('does not fetch from seed if we got 12 or more snodes in the db', async () => { + it('does not fetch from seed if we got 8 or more snodes in the db', async () => { stubData('getSnodePoolFromDb').resolves(fakeSnodePool); getSnodePoolFromDBOrFetchFromSeed = Sinon.stub( @@ -76,14 +77,12 @@ describe('GuardNodes', () => { fetchFromSeedWithRetriesAndWriteToDb.callCount, 'fetchFromSeedWithRetriesAndWriteToDb should not have been called' ).to.be.eq(0); - expect( - testGuardNode.callCount, - 'firstGuardNode should have been called three times' - ).to.be.eq(3); + expect(testGuardNode.callCount, 'testGuardNode should have been called two times').to.be.eq( + 2 + ); // this should be desiredGuardCount const firstGuardNode = testGuardNode.firstCall.args[0]; const secondGuardNode = testGuardNode.secondCall.args[0]; - const thirdGuardNode = testGuardNode.thirdCall.args[0]; - expect(fetchedGuardNodes).to.deep.equal([firstGuardNode, secondGuardNode, thirdGuardNode]); + expect(fetchedGuardNodes).to.deep.equal([firstGuardNode, secondGuardNode]); }); it('throws an error if we got enough snodes in the db but none test passes', async () => { @@ -116,10 +115,9 @@ describe('GuardNodes', () => { fetchFromSeedWithRetriesAndWriteToDb.callCount, 'fetchFromSeedWithRetriesAndWriteToDb should not have been called' ).to.be.eq(0); - expect( - testGuardNode.callCount, - 'firstGuardNode should have been called three times' - ).to.be.eq(18); + expect(testGuardNode.callCount, 'testGuardNode should have been called 12 times').to.be.eq( + 12 + ); expect(throwedError).to.be.equal('selectGuardNodes stopping after attempts: 6'); }); @@ -168,13 +166,14 @@ describe('GuardNodes', () => { // run the command const guardNodes = await OnionPaths.selectGuardNodes(); - expect(guardNodes.length).to.be.equal(3); - expect(testGuardNode.callCount).to.be.equal(3); + expect(guardNodes.length).to.be.equal(2); + expect(testGuardNode.callCount).to.be.equal(2); }); it('throws if we have to fetch from seed, fetch from seed but not have enough fetched snodes', async () => { - const invalidSndodePool = fakeSnodePool.slice(0, 11); - stubData('getSnodePoolFromDb').resolves(invalidSndodePool); + const invalidLength = minSnodePoolCount - 1; + const invalidSnodePool = fakeSnodePool.slice(0, invalidLength); + stubData('getSnodePoolFromDb').resolves(invalidSnodePool); TestUtils.stubWindow('getSeedNodeList', () => [{ url: 'whatever' }]); getSnodePoolFromDBOrFetchFromSeed = Sinon.stub( @@ -184,7 +183,7 @@ describe('GuardNodes', () => { fetchFromSeedWithRetriesAndWriteToDb = Sinon.stub( SeedNodeAPI, 'fetchSnodePoolFromSeedNodeWithRetries' - ).resolves(invalidSndodePool); + ).resolves(invalidSnodePool); stubData('updateGuardNodes').resolves(); // run the command @@ -195,7 +194,7 @@ describe('GuardNodes', () => { throwedError = e.message; } expect(throwedError).to.be.equal( - 'Could not select guard nodes. Not enough nodes in the pool: 11' + 'Could not select guard nodes. Not enough nodes in the pool: 7' // this is invalidLength but we want this test to fail if we change minSnodePoolCount ); }); }); diff --git a/ts/test/session/unit/onion/SnodePoolUpdate_test.ts b/ts/test/session/unit/onion/SnodePoolUpdate_test.ts index 7f6d8cdde..7f141da27 100644 --- a/ts/test/session/unit/onion/SnodePoolUpdate_test.ts +++ b/ts/test/session/unit/onion/SnodePoolUpdate_test.ts @@ -67,10 +67,10 @@ describe('OnionPaths', () => { expect(fetched).to.deep.equal(fakeSnodePool); }); - it('if the cached snode pool 12 or less snodes, trigger a fetch from the seed nodes', async () => { - const length12 = fakeSnodePool.slice(0, 12); - expect(length12.length).to.eq(12); - getSnodePoolFromDb = stubData('getSnodePoolFromDb').resolves(length12); + it('if the cached snode pool 8 or less snodes, trigger a fetch from the seed nodes', async () => { + const length8 = fakeSnodePool.slice(0, 8); + expect(length8.length).to.eq(8); + getSnodePoolFromDb = stubData('getSnodePoolFromDb').resolves(length8); stubData('updateSnodePoolOnDb').resolves(); fetchFromSeedWithRetriesAndWriteToDb = Sinon.stub( diff --git a/ts/test/session/unit/utils/job_runner/JobRunner_test.ts b/ts/test/session/unit/utils/job_runner/JobRunner_test.ts index 121358a0d..090b3d377 100644 --- a/ts/test/session/unit/utils/job_runner/JobRunner_test.ts +++ b/ts/test/session/unit/utils/job_runner/JobRunner_test.ts @@ -271,6 +271,7 @@ describe('JobRunner', () => { it('adding one job after the first is done schedules it', async () => { await runnerMulti.loadJobsFromDb(); + TestUtils.stubWindowLog(); const job = getFakeSleepForMultiJob({ timestamp: 100 }); runnerMulti.startProcessing(); clock.tick(110); @@ -303,6 +304,8 @@ describe('JobRunner', () => { await job2.waitForCurrentTry(); await runnerMulti.waitCurrentJob(); + // we need to give sometime to the jobrunner to handle the return of job2 and remove it + await sleepFor(100); expect(runnerMulti.getJobList()).to.deep.eq([]); }); @@ -401,7 +404,7 @@ describe('JobRunner', () => { currentRetry: 1, }; // just give time for the runnerMulti to pick up a new job - await sleepFor(10); + await sleepFor(100); // the job failed, so the job should still be there expect(runnerMulti.getJobList()).to.deep.eq([jobUpdated]); @@ -409,14 +412,16 @@ describe('JobRunner', () => { // that job should be retried now clock.tick(11000); await runner.waitCurrentJob(); + await runnerMulti.waitCurrentJob(); + const jobUpdated2 = { ...job.serializeJob(), - nextAttemptTimestamp: clock.now + 10000, + nextAttemptTimestamp: clock.now + job.persistedData.delayBetweenRetries, currentRetry: 2, }; + await sleepFor(10); - await runnerMulti.waitCurrentJob(); expect(runnerMulti.getJobList()).to.deep.eq([jobUpdated2]); // that job should be retried one more time and then removed from the list of jobs to be run