Skip to content

[Rule-based segments] Add implementations for InMemory and LocalStorage #391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Update unit tests
  • Loading branch information
EmilianoSanchez committed Feb 27, 2025
commit e22b28601f1ae4ff46467978975cc1c56b02dc58
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { RBSegmentsCacheInMemory } from '../RBSegmentsCacheInMemory';
import { RBSegmentsCacheInLocal } from '../../inLocalStorage/RBSegmentsCacheInLocal';
import { KeyBuilderCS } from '../../KeyBuilderCS';
import { rbSegment, rbSegmentWithInSegmentMatcher } from '../../__tests__/testUtils';
import { IRBSegmentsCacheSync } from '../../types';
import { fullSettings } from '../../../utils/settingsValidation/__tests__/settings.mocks';
import { RBSegmentsCacheInMemory } from '../inMemory/RBSegmentsCacheInMemory';
import { RBSegmentsCacheInLocal } from '../inLocalStorage/RBSegmentsCacheInLocal';
import { KeyBuilderCS } from '../KeyBuilderCS';
import { rbSegment, rbSegmentWithInSegmentMatcher } from '../__tests__/testUtils';
import { IRBSegmentsCacheSync } from '../types';
import { fullSettings } from '../../utils/settingsValidation/__tests__/settings.mocks';

const cacheInMemory = new RBSegmentsCacheInMemory();
const cacheInLocal = new RBSegmentsCacheInLocal(fullSettings, new KeyBuilderCS('SPLITIO', 'user'));

describe.each([cacheInMemory, cacheInLocal])('RB SEGMENTS CACHE', (cache: IRBSegmentsCacheSync) => {
describe.each([cacheInMemory, cacheInLocal])('Rule-based segments cache sync (Memory & LocalStorage)', (cache: IRBSegmentsCacheSync) => {

beforeEach(() => {
cache.clear();
Expand All @@ -26,18 +26,26 @@ describe.each([cacheInMemory, cacheInLocal])('RB SEGMENTS CACHE', (cache: IRBSeg

test('update should add and remove segments correctly', () => {
// Add segments
const updated1 = cache.update([rbSegment, rbSegmentWithInSegmentMatcher], [], 1);
expect(updated1).toBe(true);
expect(cache.update([rbSegment, rbSegmentWithInSegmentMatcher], [], 1)).toBe(true);
expect(cache.get(rbSegment.name)).toEqual(rbSegment);
expect(cache.get(rbSegmentWithInSegmentMatcher.name)).toEqual(rbSegmentWithInSegmentMatcher);
expect(cache.getChangeNumber()).toBe(1);

// Remove segments
const updated2 = cache.update([], [rbSegment], 2);
expect(updated2).toBe(true);
// Remove a segment
expect(cache.update([], [rbSegment], 2)).toBe(true);
expect(cache.get(rbSegment.name)).toBeNull();
expect(cache.get(rbSegmentWithInSegmentMatcher.name)).toEqual(rbSegmentWithInSegmentMatcher);
expect(cache.getChangeNumber()).toBe(2);

// Remove remaining segment
expect(cache.update([], [rbSegmentWithInSegmentMatcher], 3)).toBe(true);
expect(cache.get(rbSegment.name)).toBeNull();
expect(cache.get(rbSegmentWithInSegmentMatcher.name)).toBeNull();
expect(cache.getChangeNumber()).toBe(3);

// No changes
expect(cache.update([], [rbSegmentWithInSegmentMatcher], 4)).toBe(false);
expect(cache.getChangeNumber()).toBe(4);
});

test('contains should check for segment existence correctly', () => {
Expand All @@ -47,6 +55,8 @@ describe.each([cacheInMemory, cacheInLocal])('RB SEGMENTS CACHE', (cache: IRBSeg
expect(cache.contains(new Set([rbSegment.name, rbSegmentWithInSegmentMatcher.name]))).toBe(true);
expect(cache.contains(new Set(['nonexistent']))).toBe(false);
expect(cache.contains(new Set([rbSegment.name, 'nonexistent']))).toBe(false);

cache.update([], [rbSegment, rbSegmentWithInSegmentMatcher], 2);
});

test('usesSegments should track segments usage correctly', () => {
Expand Down