Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add helper method for overriding env variables #3022

Merged
merged 28 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
08731d1
test: add helper method for overriding env variables
victor-yanev Sep 20, 2024
7e74328
chore: improve readability
victor-yanev Sep 20, 2024
c19d59a
fix: usages
victor-yanev Sep 24, 2024
3ddfb58
fix: cacheService.spec.ts
victor-yanev Sep 25, 2024
08dcd68
fix: utils.spec.ts
victor-yanev Sep 25, 2024
030df8f
fix: relay.spec.ts and sdkClient.spec.ts
victor-yanev Sep 25, 2024
fbbcec8
fix: eth_call.spec.ts, eth_estimateGas.spec.ts, ethGetBlockBy.spec.ts
victor-yanev Sep 25, 2024
00714c5
fix: use helper method to override envs in acceptance test specs
victor-yanev Sep 25, 2024
4d97b99
Merge branch main into 'add-helper-method-for-overriding-env-variables'
victor-yanev Sep 25, 2024
e771bac
fix: hapiService.spec.ts and precheck.spec.ts
victor-yanev Sep 25, 2024
f2f25e4
fix: sdkClient.spec.ts
victor-yanev Sep 25, 2024
ad61287
fix: cacheService.spec.ts
victor-yanev Sep 25, 2024
7b2585c
fix: metricService.spec.ts and subscriptionController.spec.ts
victor-yanev Sep 25, 2024
f8f5034
fix: rateLimiter.spec.ts
victor-yanev Sep 25, 2024
5c76ef8
fix: failing tests
victor-yanev Sep 25, 2024
fbf7eae
chore: revert some changes in rpc_batch3.spec.ts
victor-yanev Sep 25, 2024
73d4f08
fix: subscribe.spec.ts
victor-yanev Sep 25, 2024
03720d9
chore: add docs with examples to `overrideEnvs` and `withOverriddenEnvs`
victor-yanev Sep 26, 2024
274155c
chore: address comments
victor-yanev Sep 27, 2024
57c80b8
Merge branch 'main' into add-helper-method-for-overriding-env-variables
victor-yanev Sep 27, 2024
62cda73
fix: build image test
victor-yanev Sep 27, 2024
d1bc498
Merge branch 'main' into add-helper-method-for-overriding-env-variables
victor-yanev Oct 7, 2024
6c7e2c2
Merge branch 'main' into add-helper-method-for-overriding-env-variables
victor-yanev Oct 8, 2024
de2f554
Merge branch 'main' into add-helper-method-for-overriding-env-variables
victor-yanev Oct 8, 2024
7f9c5e2
chore: address comments + fix conflicts after merge
victor-yanev Oct 8, 2024
a07616d
chore: optimize TTL tests in localLRUCache.spec.ts
victor-yanev Oct 8, 2024
90a1ad4
fix: failing unit test after merge from main
victor-yanev Oct 11, 2024
358fff3
chore: optimize imports after merge from main
victor-yanev Oct 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export class FilterService implements IFilterService {
return predefined.UNSUPPORTED_METHOD;
}

public async getFilterLogs(filterId: string, requestDetails: RequestDetails): Promise<any> {
public async getFilterLogs(filterId: string, requestDetails: RequestDetails): Promise<Log[]> {
this.logger.trace(`${requestDetails.formattedRequestId} getFilterLogs(${filterId})`);
FilterService.requireFiltersEnabled();

Expand Down
96 changes: 77 additions & 19 deletions packages/relay/tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -885,40 +885,98 @@ export const calculateTxRecordChargeAmount = (exchangeRateIncents: number) => {
};

export const useInMemoryRedisServer = (logger: Logger, port: number) => {
let envsToReset: { TEST?: string; REDIS_ENABLED?: string; REDIS_URL?: string };
overrideEnvsInMochaDescribe({ TEST: 'false', REDIS_ENABLED: 'true', REDIS_URL: `redis://127.0.0.1:${port}` });

let redisInMemoryServer: RedisInMemoryServer;

before(async () => {
({ envsToReset, redisInMemoryServer } = await startRedisInMemoryServer(logger, port));
redisInMemoryServer = await startRedisInMemoryServer(logger, port);
});

after(async () => {
await stopRedisInMemoryServer(redisInMemoryServer, envsToReset);
await stopRedisInMemoryServer(redisInMemoryServer);
});
};

export const startRedisInMemoryServer = async (logger: Logger, port: number) => {
const redisInMemoryServer = new RedisInMemoryServer(logger.child({ name: 'RedisInMemoryServer' }), port);
await redisInMemoryServer.start();
const envsToReset = {
TEST: process.env.TEST,
REDIS_ENABLED: process.env.REDIS_ENABLED,
REDIS_URL: process.env.REDIS_URL,
};
process.env.TEST = 'false';
process.env.REDIS_ENABLED = 'true';
process.env.REDIS_URL = `redis://127.0.0.1:${port}`;
return { redisInMemoryServer, envsToReset };
return redisInMemoryServer;
};

export const stopRedisInMemoryServer = async (
redisInMemoryServer: RedisInMemoryServer,
envsToReset: { TEST?: string; REDIS_ENABLED?: string; REDIS_URL?: string },
): Promise<void> => {
export const stopRedisInMemoryServer = async (redisInMemoryServer: RedisInMemoryServer): Promise<void> => {
await redisInMemoryServer.stop();
process.env.TEST = envsToReset.TEST;
process.env.REDIS_ENABLED = envsToReset.REDIS_ENABLED;
process.env.REDIS_URL = envsToReset.REDIS_URL;
};

/**
* Temporarily overrides environment variables for the duration of the encapsulating describe block.
* @param envs - An object containing key-value pairs of environment variables to set.
*
* @example
* describe('given TEST is set to false', () => {
* overrideEnvsInMochaDescribe({ TEST: 'false' });
*
* it('should return false', () => {
* expect(process.env.TEST).to.equal('false');
* });
* });
*
* it('should return true', () => {
* expect(process.env.TEST).to.equal('true');
* });
*/
export const overrideEnvsInMochaDescribe = (envs: NodeJS.Dict<string>) => {
let envsToReset: NodeJS.Dict<string> = {};

const overrideEnv = (object: NodeJS.Dict<string>, key: string, value: string | undefined) => {
if (value === undefined) {
delete object[key];
} else {
object[key] = value;
}
};

before(() => {
for (const key in envs) {
envsToReset[key] = process.env[key];
overrideEnv(process.env, key, envs[key]);
}
});

after(() => {
for (const key in envs) {
overrideEnv(process.env, key, envsToReset[key]);
}
});
};

/**
* Overrides environment variables for the duration of the provided tests.
*
* @param {NodeJS.Dict<string>} envs - An object containing key-value pairs of environment variables to set.
* @param {Function} tests - A function containing the tests to run with the overridden environment variables.
*
* @example
* withOverriddenEnvsInMochaTest({ TEST: 'false' }, () => {
* it('should return false', () => {
* expect(process.env.TEST).to.equal('false');
* });
* });
*
* it('should return true', () => {
* expect(process.env.TEST).to.equal('true');
* });
*/
export const withOverriddenEnvsInMochaTest = (envs: NodeJS.Dict<string>, tests: () => void) => {
const overriddenEnvs = Object.entries(envs)
.map(([key, value]) => `${key}=${value}`)
.join(', ');

describe(`given ${overriddenEnvs} are set`, () => {
overrideEnvsInMochaDescribe(envs);

tests();
});
};

export const estimateFileTransactionsFee = (
Expand Down
13 changes: 5 additions & 8 deletions packages/relay/tests/lib/clients/localLRUCache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import chaiAsPromised from 'chai-as-promised';
import { Registry } from 'prom-client';
import pino from 'pino';
import { LocalLRUCache } from '../../../src/lib/clients';
import constants from '../../../src/lib/constants';
import { overrideEnvsInMochaDescribe } from '../../helpers';
import { RequestDetails } from '../../../src/lib/types';

const logger = pino();
Expand Down Expand Up @@ -107,13 +107,9 @@ describe('LocalLRUCache Test Suite', async function () {
});

describe('verify cache management', async function () {
beforeEach(() => {
process.env.CACHE_MAX = constants.CACHE_MAX.toString();
});
overrideEnvsInMochaDescribe({ CACHE_MAX: '2' });

it('verify cache size', async function () {
const cacheMaxSize = 2;
process.env.CACHE_MAX = `${cacheMaxSize}`;
const customLocalLRUCache = new LocalLRUCache(logger.child({ name: `cache` }), registry);
const keyValuePairs = {
key1: 'value1',
Expand Down Expand Up @@ -149,8 +145,9 @@ describe('LocalLRUCache Test Suite', async function () {
it('verify cache ttl nature', async function () {
const customLocalLRUCache = new LocalLRUCache(logger.child({ name: `cache` }), registry);
const key = 'key';
await customLocalLRUCache.set(key, 'value', callingMethod, requestDetails, 100); // set ttl to 1 ms
await new Promise((r) => setTimeout(r, 500)); // wait for ttl to expire
const ttl = 100; // set ttl to 100ms
await customLocalLRUCache.set(key, 'value', callingMethod, requestDetails, ttl);
await new Promise((r) => setTimeout(r, ttl + 100)); // wait for ttl to expire
const cacheValue = await customLocalLRUCache.get(key, callingMethod, requestDetails);
expect(cacheValue).to.be.null;
});
Expand Down
4 changes: 2 additions & 2 deletions packages/relay/tests/lib/clients/redisCache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describe('RedisCache Test Suite', async function () {
it('should be able to set cache with TTL less than 1000 milliseconds', async () => {
const key = 'int';
const value = 1;
const ttl = 500;
const ttl = 100;

await redisCache.set(key, value, callingMethod, requestDetails, ttl);

Expand All @@ -122,7 +122,7 @@ describe('RedisCache Test Suite', async function () {
it('should be able to set cache with TTL greater than 1000 milliseconds', async () => {
const key = 'int';
const value = 1;
const ttl = 1500;
const ttl = 1100;

await redisCache.set(key, value, callingMethod, requestDetails, ttl);

Expand Down
Loading
Loading