Skip to content

Commit cadb459

Browse files
committed
test: increase cov by running lru cache directly with no fallbck (#4558)
Signed-off-by: Mariusz Jasuwienas <[email protected]>
1 parent fd8e5b7 commit cadb459

File tree

3 files changed

+264
-131
lines changed

3 files changed

+264
-131
lines changed

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

Lines changed: 99 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import chai, { expect } from 'chai';
44
import chaiAsPromised from 'chai-as-promised';
5-
import pino from 'pino';
65
import { Registry } from 'prom-client';
76
import sinon from 'sinon';
87

@@ -13,8 +12,11 @@ chai.use(chaiAsPromised);
1312

1413
describe('LocalLRUCache Test Suite', async function () {
1514
this.timeout(10000);
16-
17-
const logger = pino({ level: 'silent' });
15+
const logger = {
16+
child: sinon.stub().returnsThis(),
17+
trace: sinon.stub(),
18+
isLevelEnabled: sinon.stub().returns(true),
19+
};
1820
const registry = new Registry();
1921
const callingMethod = 'localLRUCacheTest';
2022

@@ -284,4 +286,98 @@ describe('LocalLRUCache Test Suite', async function () {
284286
expect(await localLRUCache.get('number', callingMethod)).to.equal(5644);
285287
});
286288
});
289+
290+
describe('incrBy', function () {
291+
it('increments an existing integer value', async function () {
292+
const key = 'counter';
293+
await localLRUCache.set(key, 5, callingMethod);
294+
const newValue = await localLRUCache.incrBy(key, 3, callingMethod);
295+
expect(newValue).to.equal(8);
296+
297+
const stored = await localLRUCache.get(key, callingMethod);
298+
expect(stored).to.equal(8);
299+
});
300+
301+
it('increments when value is zero', async function () {
302+
const key = 'counter';
303+
await localLRUCache.set(key, 0, callingMethod);
304+
const newValue = await localLRUCache.incrBy(key, 10, callingMethod);
305+
expect(newValue).to.equal(10);
306+
});
307+
308+
it('increments when key does not exist', async function () {
309+
await localLRUCache.clear();
310+
const key = 'missing';
311+
const newValue = await localLRUCache.incrBy(key, 5, callingMethod);
312+
expect(newValue).to.equal(5);
313+
});
314+
});
315+
316+
describe('lRange', function () {
317+
it('returns the correct range for a valid array', async function () {
318+
const key = 'list';
319+
const value = [10, 20, 30, 40];
320+
await localLRUCache.set(key, value, callingMethod);
321+
322+
const result = await localLRUCache.lRange(key, 1, 2, callingMethod);
323+
expect(result).to.deep.equal([20, 30]);
324+
});
325+
326+
it('supports negative end index', async function () {
327+
const key = 'list';
328+
const value = [10, 20, 30, 40];
329+
await localLRUCache.set(key, value, callingMethod);
330+
331+
const result = await localLRUCache.lRange(key, 1, -1, callingMethod);
332+
expect(result).to.deep.equal([20, 30, 40]);
333+
});
334+
335+
it('returns empty array when key does not exist', async function () {
336+
const key = 'missing';
337+
const result = await localLRUCache.lRange(key, 0, 10, callingMethod);
338+
expect(result).to.deep.equal([]);
339+
});
340+
341+
it('throws when the value is not an array', async function () {
342+
const key = 'notList';
343+
await localLRUCache.set(key, 123, callingMethod);
344+
345+
await expect(localLRUCache.lRange(key, 0, 1, callingMethod)).to.be.rejectedWith(
346+
`Value at key ${key} is not an array`,
347+
);
348+
});
349+
});
350+
351+
describe('rPush', function () {
352+
it('pushes value to end of an existing list', async function () {
353+
const key = 'list';
354+
const initial = [1, 2];
355+
await localLRUCache.set(key, initial, callingMethod);
356+
357+
const length = await localLRUCache.rPush(key, 3, callingMethod);
358+
expect(length).to.equal(3);
359+
360+
const stored = await localLRUCache.get(key, callingMethod);
361+
expect(stored).to.deep.equal([1, 2, 3]);
362+
});
363+
364+
it('initializes a new list when key does not exist', async function () {
365+
const key = 'missing';
366+
367+
const length = await localLRUCache.rPush(key, 'a', callingMethod);
368+
expect(length).to.equal(1);
369+
370+
const stored = await localLRUCache.get(key, callingMethod);
371+
expect(stored).to.deep.equal(['a']);
372+
});
373+
374+
it('throws when existing value is not an array', async function () {
375+
const key = 'wrongType';
376+
await localLRUCache.set(key, 999, callingMethod);
377+
378+
await expect(localLRUCache.rPush(key, 'x', callingMethod)).to.be.rejectedWith(
379+
`Value at key ${key} is not an array`,
380+
);
381+
});
382+
});
287383
});

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,19 @@ chai.use(chaiAsPromised);
1515
describe('RedisCache Test Suite', async function () {
1616
this.timeout(10000);
1717

18-
const logger = pino({ level: 'silent' });
18+
const mockLogger = {
19+
child: sinon.stub().returnsThis(),
20+
trace: sinon.stub(),
21+
info: sinon.stub(),
22+
isLevelEnabled: sinon.stub().returns(true),
23+
};
1924
const callingMethod = 'RedisCacheTest';
2025

2126
let redisCache: RedisCache;
2227
let redisClient: RedisClientType;
2328

29+
const logger = mockLogger.child({ name: 'mock' });
30+
2431
useInMemoryRedisServer(logger, 6379);
2532

2633
this.beforeAll(async () => {
@@ -161,6 +168,23 @@ describe('RedisCache Test Suite', async function () {
161168
expect(cachedValue).deep.equal(keyValuePairs[key]);
162169
}
163170
});
171+
172+
it('should fallback to pipeline set when multiset disabled', async function () {
173+
const keyValuePairs = {
174+
int: 1,
175+
string: 'test',
176+
boolean: false,
177+
array: ['false'],
178+
object: { result: true },
179+
};
180+
redisCache['options'].multiSetEnabled = false;
181+
await redisCache.multiSet(keyValuePairs, callingMethod);
182+
183+
for (const key in keyValuePairs) {
184+
const cachedValue = await redisCache.get(key, callingMethod);
185+
expect(cachedValue).deep.equal(keyValuePairs[key]);
186+
}
187+
});
164188
});
165189

166190
describe('PipelineSet Test Suite', async function () {

0 commit comments

Comments
 (0)