-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSplitsCacheInMemory.spec.ts
More file actions
190 lines (136 loc) · 7.42 KB
/
SplitsCacheInMemory.spec.ts
File metadata and controls
190 lines (136 loc) · 7.42 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { SplitsCacheInMemory } from '../SplitsCacheInMemory';
import { ISplit } from '../../../dtos/types';
import { splitWithUserTT, splitWithAccountTT, something, somethingElse, featureFlagWithEmptyFS, featureFlagWithoutFS, featureFlagOne, featureFlagTwo, featureFlagThree } from '../../__tests__/testUtils';
test('SPLITS CACHE / In Memory', () => {
const cache = new SplitsCacheInMemory();
cache.update([something, somethingElse], [], -1);
let values = cache.getAll();
expect(values).toEqual([something, somethingElse]);
cache.removeSplit(something.name);
const splits = cache.getSplits([something.name, somethingElse.name]);
expect(splits[something.name]).toEqual(null);
expect(splits[somethingElse.name]).toEqual(somethingElse);
values = cache.getAll();
expect(values).toEqual([somethingElse]);
expect(cache.getSplit(something.name)).toEqual(null);
expect(cache.getSplit(somethingElse.name)).toEqual(somethingElse);
expect(cache.getChangeNumber()).toBe(-1);
cache.setChangeNumber(123);
expect(cache.getChangeNumber()).toBe(123);
});
test('SPLITS CACHE / In Memory / Get Keys', () => {
const cache = new SplitsCacheInMemory();
cache.update([something, somethingElse], [], 1);
const keys = cache.getSplitNames();
expect(keys.indexOf(something.name) !== -1).toBe(true);
expect(keys.indexOf(somethingElse.name) !== -1).toBe(true);
});
test('SPLITS CACHE / In Memory / Update Splits', () => {
const cache = new SplitsCacheInMemory();
cache.update([something, somethingElse], [], 1);
cache.update([], [something, somethingElse], 1);
expect(cache.getSplit(something.name)).toBe(null);
expect(cache.getSplit(somethingElse.name)).toBe(null);
});
test('SPLITS CACHE / In Memory / trafficTypeExists and ttcache tests', () => {
const cache = new SplitsCacheInMemory();
cache.update([
{ ...splitWithUserTT, name: 'split1' },
{ ...splitWithAccountTT, name: 'split2' },
{ ...splitWithUserTT, name: 'split3' },
], [], 1);
cache.addSplit({ ...splitWithUserTT, name: 'split4' });
expect(cache.trafficTypeExists('user_tt')).toBe(true);
expect(cache.trafficTypeExists('account_tt')).toBe(true);
expect(cache.trafficTypeExists('not_existent_tt')).toBe(false);
cache.removeSplit('split4');
expect(cache.trafficTypeExists('user_tt')).toBe(true);
expect(cache.trafficTypeExists('account_tt')).toBe(true);
cache.removeSplit('split3');
cache.removeSplit('split2');
expect(cache.trafficTypeExists('user_tt')).toBe(true);
expect(cache.trafficTypeExists('account_tt')).toBe(false);
cache.removeSplit('split1');
expect(cache.trafficTypeExists('user_tt')).toBe(false);
expect(cache.trafficTypeExists('account_tt')).toBe(false);
cache.addSplit({ ...splitWithUserTT, name: 'split1' });
expect(cache.trafficTypeExists('user_tt')).toBe(true);
cache.addSplit({ ...splitWithAccountTT, name: 'split1' });
expect(cache.trafficTypeExists('account_tt')).toBe(true);
expect(cache.trafficTypeExists('user_tt')).toBe(false);
});
test('SPLITS CACHE / In Memory / killLocally', () => {
const cache = new SplitsCacheInMemory();
cache.addSplit(something);
cache.addSplit(somethingElse);
const initialChangeNumber = cache.getChangeNumber();
// kill an non-existent split
let updated = cache.killLocally('nonexistent_split', 'other_treatment', 101);
const nonexistentSplit = cache.getSplit('nonexistent_split');
expect(updated).toBe(false); // killLocally resolves without update if split doesn't exist
expect(nonexistentSplit).toBe(null); // non-existent split keeps being non-existent
// kill an existent split
updated = cache.killLocally(something.name, 'some_treatment', 100);
let lol1Split = cache.getSplit(something.name) as ISplit;
expect(updated).toBe(true); // killLocally resolves with update if split is changed
expect(lol1Split.killed).toBe(true); // existing split must be killed
expect(lol1Split.defaultTreatment).toBe('some_treatment'); // existing split must have new default treatment
expect(lol1Split.changeNumber).toBe(100); // existing split must have the given change number
expect(cache.getChangeNumber()).toBe(initialChangeNumber); // cache changeNumber is not changed
// not update if changeNumber is old
updated = cache.killLocally(something.name, 'some_treatment_2', 90);
lol1Split = cache.getSplit(something.name) as ISplit;
expect(updated).toBe(false); // killLocally resolves without update if changeNumber is old
expect(lol1Split.defaultTreatment).not.toBe('some_treatment_2'); // existing split is not updated if given changeNumber is older
});
test('SPLITS CACHE / In Memory / flag set cache tests', () => {
// @ts-ignore
const cache = new SplitsCacheInMemory({ groupedFilters: { bySet: ['o', 'n', 'e', 'x'] } });
const emptySet = new Set([]);
cache.update([
featureFlagOne,
featureFlagTwo,
featureFlagThree,
], [], -1);
cache.addSplit(featureFlagWithEmptyFS);
// Adding an existing FF should not affect the cache
cache.update([featureFlagTwo], [], -1);
expect(cache.getNamesByFlagSets(['o'])).toEqual([new Set(['ff_one', 'ff_two'])]);
expect(cache.getNamesByFlagSets(['n'])).toEqual([new Set(['ff_one'])]);
expect(cache.getNamesByFlagSets(['e'])).toEqual([new Set(['ff_one', 'ff_three'])]);
expect(cache.getNamesByFlagSets(['t'])).toEqual([emptySet]); // 't' not in filter
expect(cache.getNamesByFlagSets(['o', 'n', 'e'])).toEqual([new Set(['ff_one', 'ff_two']), new Set(['ff_one']), new Set(['ff_one', 'ff_three'])]);
cache.addSplit({ ...featureFlagOne, sets: ['1'] });
expect(cache.getNamesByFlagSets(['1'])).toEqual([emptySet]); // '1' not in filter
expect(cache.getNamesByFlagSets(['o'])).toEqual([new Set(['ff_two'])]);
expect(cache.getNamesByFlagSets(['n'])).toEqual([emptySet]);
cache.addSplit({ ...featureFlagOne, sets: ['x'] });
expect(cache.getNamesByFlagSets(['x'])).toEqual([new Set(['ff_one'])]);
expect(cache.getNamesByFlagSets(['o', 'e', 'x'])).toEqual([new Set(['ff_two']), new Set(['ff_three']), new Set(['ff_one'])]);
cache.removeSplit(featureFlagOne.name);
expect(cache.getNamesByFlagSets(['x'])).toEqual([emptySet]);
cache.removeSplit(featureFlagOne.name);
expect(cache.getNamesByFlagSets(['y'])).toEqual([emptySet]); // 'y' not in filter
expect(cache.getNamesByFlagSets([])).toEqual([]);
cache.addSplit(featureFlagWithoutFS);
expect(cache.getNamesByFlagSets([])).toEqual([]);
cache.clear();
expect(cache.getNamesByFlagSets(['o', 'e', 'x'])).toEqual([emptySet, emptySet, emptySet]);
});
// if FlagSets are not defined, it should store all FlagSets in memory.
test('SPLITS CACHE / In Memory / flag set cache tests without filters', () => {
const cacheWithoutFilters = new SplitsCacheInMemory();
const emptySet = new Set([]);
cacheWithoutFilters.update([
featureFlagOne,
featureFlagTwo,
featureFlagThree,
], [], -1);
cacheWithoutFilters.addSplit(featureFlagWithEmptyFS);
expect(cacheWithoutFilters.getNamesByFlagSets(['o'])).toEqual([new Set(['ff_one', 'ff_two'])]);
expect(cacheWithoutFilters.getNamesByFlagSets(['n'])).toEqual([new Set(['ff_one'])]);
expect(cacheWithoutFilters.getNamesByFlagSets(['e'])).toEqual([new Set(['ff_one', 'ff_three'])]);
expect(cacheWithoutFilters.getNamesByFlagSets(['t'])).toEqual([new Set(['ff_two', 'ff_three'])]);
expect(cacheWithoutFilters.getNamesByFlagSets(['y'])).toEqual([emptySet]);
expect(cacheWithoutFilters.getNamesByFlagSets(['o', 'n', 'e'])).toEqual([new Set(['ff_one', 'ff_two']), new Set(['ff_one']), new Set(['ff_one', 'ff_three'])]);
});