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