|
| 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 | +}); |
0 commit comments