test: fix unit tests

pull/3178/head
Audric Ackermann 8 months ago
parent 7a3a80825b
commit 96e69d13be
No known key found for this signature in database

@ -5,8 +5,6 @@ function envAppInstanceIncludes(prefix: string) {
return !!process.env.NODE_APP_INSTANCE.includes(prefix); return !!process.env.NODE_APP_INSTANCE.includes(prefix);
} }
export function isDevProd() { export function isDevProd() {
return envAppInstanceIncludes('devprod'); return envAppInstanceIncludes('devprod');
} }

@ -14,6 +14,7 @@ import {
} from '../../../test-utils/utils'; } from '../../../test-utils/utils';
import { SeedNodeAPI } from '../../../../session/apis/seed_node_api'; import { SeedNodeAPI } from '../../../../session/apis/seed_node_api';
import { Snode } from '../../../../data/types'; import { Snode } from '../../../../data/types';
import { minSnodePoolCount } from '../../../../session/apis/snode_api/snodePool';
chai.use(chaiAsPromised as any); chai.use(chaiAsPromised as any);
chai.should(); chai.should();
@ -51,7 +52,7 @@ describe('GuardNodes', () => {
Sinon.restore(); 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); stubData('getSnodePoolFromDb').resolves(fakeSnodePool);
getSnodePoolFromDBOrFetchFromSeed = Sinon.stub( getSnodePoolFromDBOrFetchFromSeed = Sinon.stub(
@ -76,14 +77,12 @@ describe('GuardNodes', () => {
fetchFromSeedWithRetriesAndWriteToDb.callCount, fetchFromSeedWithRetriesAndWriteToDb.callCount,
'fetchFromSeedWithRetriesAndWriteToDb should not have been called' 'fetchFromSeedWithRetriesAndWriteToDb should not have been called'
).to.be.eq(0); ).to.be.eq(0);
expect( expect(testGuardNode.callCount, 'testGuardNode should have been called two times').to.be.eq(
testGuardNode.callCount, 2
'firstGuardNode should have been called three times' ); // this should be desiredGuardCount
).to.be.eq(3);
const firstGuardNode = testGuardNode.firstCall.args[0]; const firstGuardNode = testGuardNode.firstCall.args[0];
const secondGuardNode = testGuardNode.secondCall.args[0]; const secondGuardNode = testGuardNode.secondCall.args[0];
const thirdGuardNode = testGuardNode.thirdCall.args[0]; expect(fetchedGuardNodes).to.deep.equal([firstGuardNode, secondGuardNode]);
expect(fetchedGuardNodes).to.deep.equal([firstGuardNode, secondGuardNode, thirdGuardNode]);
}); });
it('throws an error if we got enough snodes in the db but none test passes', async () => { 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.callCount,
'fetchFromSeedWithRetriesAndWriteToDb should not have been called' 'fetchFromSeedWithRetriesAndWriteToDb should not have been called'
).to.be.eq(0); ).to.be.eq(0);
expect( expect(testGuardNode.callCount, 'testGuardNode should have been called 12 times').to.be.eq(
testGuardNode.callCount, 12
'firstGuardNode should have been called three times' );
).to.be.eq(18);
expect(throwedError).to.be.equal('selectGuardNodes stopping after attempts: 6'); expect(throwedError).to.be.equal('selectGuardNodes stopping after attempts: 6');
}); });
@ -168,13 +166,14 @@ describe('GuardNodes', () => {
// run the command // run the command
const guardNodes = await OnionPaths.selectGuardNodes(); const guardNodes = await OnionPaths.selectGuardNodes();
expect(guardNodes.length).to.be.equal(3); expect(guardNodes.length).to.be.equal(2);
expect(testGuardNode.callCount).to.be.equal(3); 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 () => { 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); const invalidLength = minSnodePoolCount - 1;
stubData('getSnodePoolFromDb').resolves(invalidSndodePool); const invalidSnodePool = fakeSnodePool.slice(0, invalidLength);
stubData('getSnodePoolFromDb').resolves(invalidSnodePool);
TestUtils.stubWindow('getSeedNodeList', () => [{ url: 'whatever' }]); TestUtils.stubWindow('getSeedNodeList', () => [{ url: 'whatever' }]);
getSnodePoolFromDBOrFetchFromSeed = Sinon.stub( getSnodePoolFromDBOrFetchFromSeed = Sinon.stub(
@ -184,7 +183,7 @@ describe('GuardNodes', () => {
fetchFromSeedWithRetriesAndWriteToDb = Sinon.stub( fetchFromSeedWithRetriesAndWriteToDb = Sinon.stub(
SeedNodeAPI, SeedNodeAPI,
'fetchSnodePoolFromSeedNodeWithRetries' 'fetchSnodePoolFromSeedNodeWithRetries'
).resolves(invalidSndodePool); ).resolves(invalidSnodePool);
stubData('updateGuardNodes').resolves(); stubData('updateGuardNodes').resolves();
// run the command // run the command
@ -195,7 +194,7 @@ describe('GuardNodes', () => {
throwedError = e.message; throwedError = e.message;
} }
expect(throwedError).to.be.equal( 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
); );
}); });
}); });

@ -67,10 +67,10 @@ describe('OnionPaths', () => {
expect(fetched).to.deep.equal(fakeSnodePool); expect(fetched).to.deep.equal(fakeSnodePool);
}); });
it('if the cached snode pool 12 or less snodes, trigger a fetch from the seed nodes', async () => { it('if the cached snode pool 8 or less snodes, trigger a fetch from the seed nodes', async () => {
const length12 = fakeSnodePool.slice(0, 12); const length8 = fakeSnodePool.slice(0, 8);
expect(length12.length).to.eq(12); expect(length8.length).to.eq(8);
getSnodePoolFromDb = stubData('getSnodePoolFromDb').resolves(length12); getSnodePoolFromDb = stubData('getSnodePoolFromDb').resolves(length8);
stubData('updateSnodePoolOnDb').resolves(); stubData('updateSnodePoolOnDb').resolves();
fetchFromSeedWithRetriesAndWriteToDb = Sinon.stub( fetchFromSeedWithRetriesAndWriteToDb = Sinon.stub(

@ -271,6 +271,7 @@ describe('JobRunner', () => {
it('adding one job after the first is done schedules it', async () => { it('adding one job after the first is done schedules it', async () => {
await runnerMulti.loadJobsFromDb(); await runnerMulti.loadJobsFromDb();
TestUtils.stubWindowLog();
const job = getFakeSleepForMultiJob({ timestamp: 100 }); const job = getFakeSleepForMultiJob({ timestamp: 100 });
runnerMulti.startProcessing(); runnerMulti.startProcessing();
clock.tick(110); clock.tick(110);
@ -303,6 +304,8 @@ describe('JobRunner', () => {
await job2.waitForCurrentTry(); await job2.waitForCurrentTry();
await runnerMulti.waitCurrentJob(); 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([]); expect(runnerMulti.getJobList()).to.deep.eq([]);
}); });
@ -401,7 +404,7 @@ describe('JobRunner', () => {
currentRetry: 1, currentRetry: 1,
}; };
// just give time for the runnerMulti to pick up a new job // 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 // the job failed, so the job should still be there
expect(runnerMulti.getJobList()).to.deep.eq([jobUpdated]); expect(runnerMulti.getJobList()).to.deep.eq([jobUpdated]);
@ -409,14 +412,16 @@ describe('JobRunner', () => {
// that job should be retried now // that job should be retried now
clock.tick(11000); clock.tick(11000);
await runner.waitCurrentJob(); await runner.waitCurrentJob();
await runnerMulti.waitCurrentJob();
const jobUpdated2 = { const jobUpdated2 = {
...job.serializeJob(), ...job.serializeJob(),
nextAttemptTimestamp: clock.now + 10000, nextAttemptTimestamp: clock.now + job.persistedData.delayBetweenRetries,
currentRetry: 2, currentRetry: 2,
}; };
await sleepFor(10); await sleepFor(10);
await runnerMulti.waitCurrentJob();
expect(runnerMulti.getJobList()).to.deep.eq([jobUpdated2]); 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 // that job should be retried one more time and then removed from the list of jobs to be run

Loading…
Cancel
Save