Skip to content

Commit 261b86e

Browse files
Unit tests
1 parent 53b2073 commit 261b86e

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { RBSegmentsCacheInRedis } from '../inRedis/RBSegmentsCacheInRedis';
2+
import { RBSegmentsCachePluggable } from '../pluggable/RBSegmentsCachePluggable';
3+
import { KeyBuilderSS } from '../KeyBuilderSS';
4+
import { rbSegment, rbSegmentWithInSegmentMatcher } from '../__tests__/testUtils';
5+
import { loggerMock } from '../../logger/__tests__/sdkLogger.mock';
6+
import { metadata } from './KeyBuilder.spec';
7+
import { RedisAdapter } from '../inRedis/RedisAdapter';
8+
import { wrapperMockFactory } from '../pluggable/__tests__/wrapper.mock';
9+
10+
const keys = new KeyBuilderSS('RBSEGMENT', metadata);
11+
12+
const redisClient = new RedisAdapter(loggerMock);
13+
const cacheInRedis = new RBSegmentsCacheInRedis(loggerMock, keys, redisClient);
14+
15+
const storageWrapper = wrapperMockFactory();
16+
const cachePluggable = new RBSegmentsCachePluggable(loggerMock, keys, storageWrapper);
17+
18+
describe.each([{ cache: cacheInRedis, wrapper: redisClient }, { cache: cachePluggable, wrapper: storageWrapper }])('Rule-based segments cache async (Redis & Pluggable)', ({ cache, wrapper }) => {
19+
20+
afterAll(async () => {
21+
await wrapper.del(keys.buildRBSegmentsTillKey());
22+
await wrapper.disconnect();
23+
});
24+
25+
test('update should add and remove segments correctly', async () => {
26+
// Add segments
27+
expect(await cache.update([rbSegment, rbSegmentWithInSegmentMatcher], [], 1)).toBe(true);
28+
expect(await cache.get(rbSegment.name)).toEqual(rbSegment);
29+
expect(await cache.get(rbSegmentWithInSegmentMatcher.name)).toEqual(rbSegmentWithInSegmentMatcher);
30+
expect(await cache.getChangeNumber()).toBe(1);
31+
32+
// Remove a segment
33+
expect(await cache.update([], [rbSegment], 2)).toBe(true);
34+
expect(await cache.get(rbSegment.name)).toBeNull();
35+
expect(await cache.get(rbSegmentWithInSegmentMatcher.name)).toEqual(rbSegmentWithInSegmentMatcher);
36+
expect(await cache.getChangeNumber()).toBe(2);
37+
38+
// Remove remaining segment
39+
expect(await cache.update([], [rbSegmentWithInSegmentMatcher], 3)).toBe(true);
40+
expect(await cache.get(rbSegment.name)).toBeNull();
41+
expect(await cache.get(rbSegmentWithInSegmentMatcher.name)).toBeNull();
42+
expect(await cache.getChangeNumber()).toBe(3);
43+
44+
// No changes
45+
expect(await cache.update([], [rbSegmentWithInSegmentMatcher], 4)).toBe(false);
46+
expect(await cache.getChangeNumber()).toBe(4);
47+
});
48+
49+
test('contains should check for segment existence correctly', async () => {
50+
await cache.update([rbSegment, rbSegmentWithInSegmentMatcher], [], 1);
51+
52+
expect(await cache.contains(new Set([rbSegment.name]))).toBe(true);
53+
expect(await cache.contains(new Set([rbSegment.name, rbSegmentWithInSegmentMatcher.name]))).toBe(true);
54+
expect(await cache.contains(new Set(['nonexistent']))).toBe(false);
55+
expect(await cache.contains(new Set([rbSegment.name, 'nonexistent']))).toBe(false);
56+
57+
await cache.update([], [rbSegment, rbSegmentWithInSegmentMatcher], 2);
58+
});
59+
});

src/storages/inRedis/RBSegmentsCacheInRedis.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class RBSegmentsCacheInRedis implements IRBSegmentsCacheAsync {
7373

7474
// @TODO implement if required by DataLoader or producer mode
7575
clear() {
76-
return Promise.resolve(true);
76+
return Promise.resolve();
7777
}
7878

7979
}

src/storages/pluggable/RBSegmentsCachePluggable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class RBSegmentsCachePluggable implements IRBSegmentsCacheAsync {
7070

7171
// @TODO implement if required by DataLoader or producer mode
7272
clear() {
73-
return Promise.resolve(true);
73+
return Promise.resolve();
7474
}
7575

7676
}

0 commit comments

Comments
 (0)