Skip to content

Commit 71afb09

Browse files
committed
fixes tests after client creation moved outside of cache service
Signed-off-by: Konstantina Blazhukova <konstantina.blajukova@gmail.com>
1 parent 91a25e8 commit 71afb09

File tree

4 files changed

+39
-27
lines changed

4 files changed

+39
-27
lines changed

packages/relay/tests/lib/clients/redisCache.spec.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import chai, { expect } from 'chai';
44
import chaiAsPromised from 'chai-as-promised';
55
import { pino } from 'pino';
66
import { Registry } from 'prom-client';
7-
import { RedisClientType } from 'redis';
7+
import { createClient, RedisClientType } from 'redis';
88
import sinon from 'sinon';
99

1010
import { RedisCache } from '../../../src/lib/clients';
@@ -20,14 +20,15 @@ describe('RedisCache Test Suite', async function () {
2020
const callingMethod = 'RedisCacheTest';
2121

2222
let redisCache: RedisCache;
23-
let redisClientSpy: sinon.SinonSpiedInstance<RedisClientType>;
23+
let rawClient: RedisClientType;
2424

2525
useInMemoryRedisServer(logger, 6379);
2626

2727
this.beforeAll(async () => {
28-
redisCache = new RedisCache(logger.child({ name: `cache` }), registry);
29-
redisCache['options'].ttl = 100; // set default cache ttl to 100ms for testing
30-
redisClientSpy = sinon.spy(redisCache['client']);
28+
rawClient = createClient({ url: 'redis://127.0.0.1:6379' });
29+
redisCache = new RedisCache(logger.child({ name: `cache` }), registry, rawClient);
30+
redisCache['options'].ttl = 100;
31+
sinon.spy(rawClient, 'set');
3132
});
3233

3334
this.beforeEach(async () => {
@@ -96,7 +97,7 @@ describe('RedisCache Test Suite', async function () {
9697
const ttl = 100;
9798

9899
await redisCache.set(key, value, callingMethod, ttl);
99-
sinon.assert.calledOnceWithExactly(redisClientSpy.set, key, JSON.stringify(value), { PX: ttl });
100+
sinon.assert.calledOnceWithExactly(rawClient.set as sinon.SinonSpy, key, JSON.stringify(value), { PX: ttl });
100101

101102
const cachedValue = await redisCache.get(key, callingMethod);
102103
expect(cachedValue).equal(value);
@@ -113,7 +114,7 @@ describe('RedisCache Test Suite', async function () {
113114
const ttl = 1100;
114115

115116
await redisCache.set(key, value, callingMethod, ttl);
116-
sinon.assert.calledOnceWithExactly(redisClientSpy.set, key, JSON.stringify(value), { PX: ttl });
117+
sinon.assert.calledOnceWithExactly(rawClient.set as sinon.SinonSpy, key, JSON.stringify(value), { PX: ttl });
117118

118119
const cachedValue = await redisCache.get(key, callingMethod);
119120
expect(cachedValue).equal(value);
@@ -130,7 +131,7 @@ describe('RedisCache Test Suite', async function () {
130131
const ttl = -1;
131132

132133
await redisCache.set(key, value, callingMethod, ttl);
133-
sinon.assert.calledOnceWithExactly(redisClientSpy.set, key, JSON.stringify(value));
134+
sinon.assert.calledOnceWithExactly(rawClient.set as sinon.SinonSpy, key, JSON.stringify(value));
134135

135136
const cachedValue = await redisCache.get(key, callingMethod);
136137
expect(cachedValue).equal(value);

packages/relay/tests/lib/config/hbarSpendingPlanConfigService.spec.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import findConfig from 'find-config';
77
import fs from 'fs';
88
import pino, { Logger } from 'pino';
99
import { Registry } from 'prom-client';
10+
import { createClient } from 'redis';
1011
import sinon from 'sinon';
1112

1213
import { HbarSpendingPlanConfigService } from '../../../src/lib/config/hbarSpendingPlanConfigService';
@@ -158,7 +159,13 @@ describe('HbarSpendingPlanConfigService', function () {
158159

159160
before(async function () {
160161
const reservedKeys = HbarSpendingPlanConfigService.getPreconfiguredSpendingPlanKeys(logger);
161-
cacheService = new CacheService(logger.child({ name: 'cache-service' }), registry, reservedKeys);
162+
const redisClient = createClient({ url: 'redis://127.0.0.1:6384' });
163+
cacheService = new CacheService(
164+
logger.child({ name: 'cache-service' }),
165+
registry,
166+
reservedKeys,
167+
redisClient as any,
168+
);
162169
hbarSpendingPlanRepository = new HbarSpendingPlanRepository(
163170
cacheService,
164171
logger.child({ name: 'hbar-spending-plan-repository' }),

packages/relay/tests/lib/ethGetBlockBy.spec.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import MockAdapter from 'axios-mock-adapter';
55
import { expect, use } from 'chai';
66
import chaiAsPromised from 'chai-as-promised';
77
import pino from 'pino';
8-
import { register, Registry } from 'prom-client';
8+
import { Registry } from 'prom-client';
9+
import sinon from 'sinon';
910

1011
import { nanOrNumberTo0x, nullableNumberTo0x, numberTo0x, toHash32 } from '../../src/formatters';
1112
import { MirrorNodeClient } from '../../src/lib/clients';
@@ -101,7 +102,13 @@ describe('eth_getBlockBy', async function () {
101102
overrideEnvsInMochaDescribe({ ETH_FEE_HISTORY_FIXED: false });
102103

103104
this.beforeAll(async () => {
104-
cacheService = new CacheService(logger, registry);
105+
const redisClientMock = {
106+
connect: sinon.stub().resolves(true),
107+
on: sinon.stub(),
108+
eval: sinon.stub(),
109+
quit: sinon.stub().resolves(true),
110+
} as any;
111+
cacheService = new CacheService(logger, registry, new Set(), redisClientMock as any);
105112

106113
// @ts-ignore
107114
mirrorNodeInstance = new MirrorNodeClient(
@@ -119,7 +126,7 @@ describe('eth_getBlockBy', async function () {
119126
});
120127

121128
this.beforeEach(async () => {
122-
await cacheService.clear(requestDetails);
129+
await cacheService.clear();
123130
restMock.reset();
124131
});
125132

@@ -149,7 +156,7 @@ describe('eth_getBlockBy', async function () {
149156

150157
it('populateSyntheticTransactions with no dupes in empty transactionHashes', async function () {
151158
const initHashes = [];
152-
blockService['populateSyntheticTransactions'](showDetails, referenceLogs, initHashes, requestDetails);
159+
blockService['populateSyntheticTransactions'](showDetails, referenceLogs, initHashes);
153160
expect(initHashes.length).to.equal(defaultLogs1.length);
154161
expect(initHashes[0]).to.equal(modelLog1.transactionHash);
155162
expect(initHashes[1]).to.equal(modelLog2.transactionHash);
@@ -159,7 +166,7 @@ describe('eth_getBlockBy', async function () {
159166
it('populateSyntheticTransactions with no dupes in non empty transactionHashes', async function () {
160167
const initHashes = ['txHash1', 'txHash2'];
161168
const txHashes = initHashes.slice();
162-
blockService['populateSyntheticTransactions'](showDetails, referenceLogs, txHashes, requestDetails);
169+
blockService['populateSyntheticTransactions'](showDetails, referenceLogs, txHashes);
163170
expect(txHashes.length).to.equal(initHashes.length + defaultLogs1.length);
164171
expect(txHashes[initHashes.length + 0]).to.equal(modelLog1.transactionHash);
165172
expect(txHashes[initHashes.length + 1]).to.equal(modelLog2.transactionHash);
@@ -169,7 +176,7 @@ describe('eth_getBlockBy', async function () {
169176
it('populateSyntheticTransactions with 1 transaction dupes in transactionHashes', async function () {
170177
const initHashes = [modelLog2.transactionHash];
171178
const txHashes = initHashes.slice();
172-
blockService['populateSyntheticTransactions'](showDetails, referenceLogs, txHashes, requestDetails);
179+
blockService['populateSyntheticTransactions'](showDetails, referenceLogs, txHashes);
173180
expect(txHashes.length).to.equal(referenceLogs.length);
174181
expect(txHashes[0]).to.equal(contractHash2);
175182
expect(txHashes[1]).to.equal(modelLog1.transactionHash);
@@ -179,7 +186,7 @@ describe('eth_getBlockBy', async function () {
179186
it('populateSyntheticTransactions with all dupes in transactionHashes', async function () {
180187
const initHashes = [modelLog1.transactionHash, modelLog2.transactionHash, modelLog3.transactionHash];
181188
const txHashes = initHashes.slice();
182-
blockService['populateSyntheticTransactions'](showDetails, referenceLogs, txHashes, requestDetails);
189+
blockService['populateSyntheticTransactions'](showDetails, referenceLogs, txHashes);
183190
expect(txHashes.length).to.equal(referenceLogs.length);
184191
expect(txHashes[0]).to.equal(modelLog1.transactionHash);
185192
expect(txHashes[1]).to.equal(modelLog2.transactionHash);
@@ -215,7 +222,7 @@ describe('eth_getBlockBy', async function () {
215222
const showDetails = true;
216223
it('populateSyntheticTransactions with no dupes in empty txObjects', async function () {
217224
const initTxObjects: Transaction[] = [];
218-
blockService['populateSyntheticTransactions'](showDetails, referenceLogs, initTxObjects, requestDetails);
225+
blockService['populateSyntheticTransactions'](showDetails, referenceLogs, initTxObjects);
219226
expect(initTxObjects.length).to.equal(defaultLogs1.length);
220227
expect(initTxObjects[0].hash).to.equal(modelLog1.transactionHash);
221228
expect(initTxObjects[1].hash).to.equal(modelLog2.transactionHash);
@@ -225,7 +232,7 @@ describe('eth_getBlockBy', async function () {
225232
it('populateSyntheticTransactions with no dupes in non empty txObjects', async function () {
226233
const initTxObjects = [getTransactionModel('txHash1'), getTransactionModel('txHash2')];
227234
const txObjects = initTxObjects.slice();
228-
blockService['populateSyntheticTransactions'](showDetails, referenceLogs, txObjects, requestDetails);
235+
blockService['populateSyntheticTransactions'](showDetails, referenceLogs, txObjects);
229236
expect(txObjects.length).to.equal(initTxObjects.length + defaultLogs1.length);
230237
expect(txObjects[initTxObjects.length + 0].hash).to.equal(modelLog1.transactionHash);
231238
expect(txObjects[initTxObjects.length + 1].hash).to.equal(modelLog2.transactionHash);
@@ -235,7 +242,7 @@ describe('eth_getBlockBy', async function () {
235242
it('populateSyntheticTransactions with 1 transaction dupes in txObjects', async function () {
236243
const initTxObjects = [getTransactionModel(modelLog2.transactionHash)];
237244
const txObjects = initTxObjects.slice();
238-
blockService['populateSyntheticTransactions'](showDetails, referenceLogs, txObjects, requestDetails);
245+
blockService['populateSyntheticTransactions'](showDetails, referenceLogs, txObjects);
239246
expect(txObjects.length).to.equal(referenceLogs.length);
240247
expect(txObjects[0].hash).to.equal(contractHash2);
241248
expect(txObjects[1].hash).to.equal(modelLog1.transactionHash);
@@ -249,7 +256,7 @@ describe('eth_getBlockBy', async function () {
249256
getTransactionModel(modelLog3.transactionHash),
250257
];
251258
const txObjects = initTxObjects.slice();
252-
blockService['populateSyntheticTransactions'](showDetails, referenceLogs, txObjects, requestDetails);
259+
blockService['populateSyntheticTransactions'](showDetails, referenceLogs, txObjects);
253260
expect(txObjects.length).to.equal(referenceLogs.length);
254261
expect(txObjects[0].hash).to.equal(modelLog1.transactionHash);
255262
expect(txObjects[1].hash).to.equal(modelLog2.transactionHash);
@@ -262,12 +269,7 @@ describe('eth_getBlockBy', async function () {
262269
const initTxObjects = [tx1, tx2];
263270

264271
const txObjects = initTxObjects.slice();
265-
const returnedTxObjects = blockService['populateSyntheticTransactions'](
266-
true,
267-
referenceLogs,
268-
txObjects,
269-
requestDetails,
270-
);
272+
const returnedTxObjects = blockService['populateSyntheticTransactions'](true, referenceLogs, txObjects);
271273

272274
// Should only have one object with modelLog1.transactionHash
273275
const count = returnedTxObjects.filter((tx) => (tx as Transaction).hash === modelLog1.transactionHash).length;

packages/relay/tests/lib/services/cacheService/cacheService.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import chai, { expect } from 'chai';
44
import chaiAsPromised from 'chai-as-promised';
55
import { pino } from 'pino';
66
import { Registry } from 'prom-client';
7+
import { createClient } from 'redis';
78
import * as sinon from 'sinon';
89

910
import { CacheService } from '../../../../src/lib/services/cacheService/cacheService';
@@ -259,7 +260,8 @@ describe('CacheService Test Suite', async function () {
259260
overrideEnvsInMochaDescribe({ MULTI_SET: true });
260261

261262
this.beforeAll(async () => {
262-
cacheService = new CacheService(logger, registry);
263+
const redisClient = createClient({ url: 'redis://127.0.0.1:6381' });
264+
cacheService = new CacheService(logger, registry, new Set(), redisClient as any);
263265
});
264266

265267
this.afterAll(async () => {

0 commit comments

Comments
 (0)