1
- import { ISplit } from '../../../../dtos/types' ;
1
+ import { IRBSegment , ISplit } from '../../../../dtos/types' ;
2
2
import { readinessManagerFactory } from '../../../../readiness/readinessManager' ;
3
3
import { splitApiFactory } from '../../../../services/splitApi' ;
4
4
import { SegmentsCacheInMemory } from '../../../../storages/inMemory/SegmentsCacheInMemory' ;
5
5
import { SplitsCacheInMemory } from '../../../../storages/inMemory/SplitsCacheInMemory' ;
6
6
import { splitChangesFetcherFactory } from '../../fetchers/splitChangesFetcher' ;
7
- import { splitChangesUpdaterFactory , parseSegments , computeSplitsMutation } from '../splitChangesUpdater' ;
7
+ import { splitChangesUpdaterFactory , parseSegments , computeMutation } from '../splitChangesUpdater' ;
8
8
import splitChangesMock1 from '../../../../__tests__/mocks/splitchanges.since.-1.json' ;
9
9
import fetchMock from '../../../../__tests__/testUtils/fetchMock' ;
10
10
import { fullSettings , settingsSplitApi } from '../../../../utils/settingsValidation/__tests__/settings.mocks' ;
11
11
import { EventEmitter } from '../../../../utils/MinEvents' ;
12
12
import { loggerMock } from '../../../../logger/__tests__/sdkLogger.mock' ;
13
13
import { telemetryTrackerFactory } from '../../../../trackers/telemetryTracker' ;
14
14
import { splitNotifications } from '../../../streaming/__tests__/dataMocks' ;
15
+ import { RBSegmentsCacheInMemory } from '../../../../storages/inMemory/RBSegmentsCacheInMemory' ;
15
16
16
17
const ARCHIVED_FF = 'ARCHIVED' ;
17
18
@@ -94,58 +95,60 @@ test('splitChangesUpdater / segments parser', () => {
94
95
test ( 'splitChangesUpdater / compute splits mutation' , ( ) => {
95
96
const splitFiltersValidation = { queryString : null , groupedFilters : { bySet : [ ] , byName : [ ] , byPrefix : [ ] } , validFilters : [ ] } ;
96
97
97
- let splitsMutation = computeSplitsMutation ( [ activeSplitWithSegments , archivedSplit ] as ISplit [ ] , splitFiltersValidation ) ;
98
+ let segments = new Set < string > ( ) ;
99
+ let splitsMutation = computeMutation ( [ activeSplitWithSegments , archivedSplit ] as ISplit [ ] , segments , splitFiltersValidation ) ;
98
100
99
101
expect ( splitsMutation . added ) . toEqual ( [ activeSplitWithSegments ] ) ;
100
102
expect ( splitsMutation . removed ) . toEqual ( [ archivedSplit ] ) ;
101
- expect ( splitsMutation . segments ) . toEqual ( [ 'A' , 'B' ] ) ;
103
+ expect ( Array . from ( segments ) ) . toEqual ( [ 'A' , 'B' ] ) ;
102
104
103
105
// SDK initialization without sets
104
106
// should process all the notifications
105
- splitsMutation = computeSplitsMutation ( [ testFFSetsAB , test2FFSetsX ] as ISplit [ ] , splitFiltersValidation ) ;
107
+ segments = new Set < string > ( ) ;
108
+ splitsMutation = computeMutation ( [ testFFSetsAB , test2FFSetsX ] as ISplit [ ] , segments , splitFiltersValidation ) ;
106
109
107
110
expect ( splitsMutation . added ) . toEqual ( [ testFFSetsAB , test2FFSetsX ] ) ;
108
111
expect ( splitsMutation . removed ) . toEqual ( [ ] ) ;
109
- expect ( splitsMutation . segments ) . toEqual ( [ ] ) ;
112
+ expect ( Array . from ( segments ) ) . toEqual ( [ ] ) ;
110
113
} ) ;
111
114
112
115
test ( 'splitChangesUpdater / compute splits mutation with filters' , ( ) => {
113
116
// SDK initialization with sets: [set_a, set_b]
114
117
let splitFiltersValidation = { queryString : '&sets=set_a,set_b' , groupedFilters : { bySet : [ 'set_a' , 'set_b' ] , byName : [ 'name_1' ] , byPrefix : [ ] } , validFilters : [ ] } ;
115
118
116
119
// fetching new feature flag in sets A & B
117
- let splitsMutation = computeSplitsMutation ( [ testFFSetsAB ] , splitFiltersValidation ) ;
120
+ let splitsMutation = computeMutation ( [ testFFSetsAB ] , new Set ( ) , splitFiltersValidation ) ;
118
121
119
122
// should add it to mutations
120
123
expect ( splitsMutation . added ) . toEqual ( [ testFFSetsAB ] ) ;
121
124
expect ( splitsMutation . removed ) . toEqual ( [ ] ) ;
122
125
123
126
// fetching existing test feature flag removed from set B
124
- splitsMutation = computeSplitsMutation ( [ testFFRemoveSetB ] , splitFiltersValidation ) ;
127
+ splitsMutation = computeMutation ( [ testFFRemoveSetB ] , new Set ( ) , splitFiltersValidation ) ;
125
128
126
129
expect ( splitsMutation . added ) . toEqual ( [ testFFRemoveSetB ] ) ;
127
130
expect ( splitsMutation . removed ) . toEqual ( [ ] ) ;
128
131
129
132
// fetching existing test feature flag removed from set B
130
- splitsMutation = computeSplitsMutation ( [ testFFRemoveSetA ] , splitFiltersValidation ) ;
133
+ splitsMutation = computeMutation ( [ testFFRemoveSetA ] , new Set ( ) , splitFiltersValidation ) ;
131
134
132
135
expect ( splitsMutation . added ) . toEqual ( [ ] ) ;
133
136
expect ( splitsMutation . removed ) . toEqual ( [ testFFRemoveSetA ] ) ;
134
137
135
138
// fetching existing test feature flag removed from set B
136
- splitsMutation = computeSplitsMutation ( [ testFFEmptySet ] , splitFiltersValidation ) ;
139
+ splitsMutation = computeMutation ( [ testFFEmptySet ] , new Set ( ) , splitFiltersValidation ) ;
137
140
138
141
expect ( splitsMutation . added ) . toEqual ( [ ] ) ;
139
142
expect ( splitsMutation . removed ) . toEqual ( [ testFFEmptySet ] ) ;
140
143
141
144
// SDK initialization with names: ['test2']
142
145
splitFiltersValidation = { queryString : '&names=test2' , groupedFilters : { bySet : [ ] , byName : [ 'test2' ] , byPrefix : [ ] } , validFilters : [ ] } ;
143
- splitsMutation = computeSplitsMutation ( [ testFFSetsAB ] , splitFiltersValidation ) ;
146
+ splitsMutation = computeMutation ( [ testFFSetsAB ] , new Set ( ) , splitFiltersValidation ) ;
144
147
145
148
expect ( splitsMutation . added ) . toEqual ( [ ] ) ;
146
149
expect ( splitsMutation . removed ) . toEqual ( [ testFFSetsAB ] ) ;
147
150
148
- splitsMutation = computeSplitsMutation ( [ test2FFSetsX , testFFEmptySet ] , splitFiltersValidation ) ;
151
+ splitsMutation = computeMutation ( [ test2FFSetsX , testFFEmptySet ] , new Set ( ) , splitFiltersValidation ) ;
149
152
150
153
expect ( splitsMutation . added ) . toEqual ( [ test2FFSetsX ] ) ;
151
154
expect ( splitsMutation . removed ) . toEqual ( [ testFFEmptySet ] ) ;
@@ -161,10 +164,13 @@ describe('splitChangesUpdater', () => {
161
164
const splits = new SplitsCacheInMemory ( ) ;
162
165
const updateSplits = jest . spyOn ( splits , 'update' ) ;
163
166
167
+ const rbSegments = new RBSegmentsCacheInMemory ( ) ;
168
+ const updateRbSegments = jest . spyOn ( rbSegments , 'update' ) ;
169
+
164
170
const segments = new SegmentsCacheInMemory ( ) ;
165
171
const registerSegments = jest . spyOn ( segments , 'registerSegments' ) ;
166
172
167
- const storage = { splits, segments } ;
173
+ const storage = { splits, rbSegments , segments } ;
168
174
169
175
const readinessManager = readinessManagerFactory ( EventEmitter , fullSettings ) ;
170
176
const splitsEmitSpy = jest . spyOn ( readinessManager . splits , 'emit' ) ;
@@ -179,22 +185,29 @@ describe('splitChangesUpdater', () => {
179
185
180
186
test ( 'test without payload' , async ( ) => {
181
187
const result = await splitChangesUpdater ( ) ;
188
+
189
+ expect ( fetchSplitChanges ) . toBeCalledTimes ( 1 ) ;
190
+ expect ( fetchSplitChanges ) . lastCalledWith ( - 1 , undefined , undefined , - 1 ) ;
182
191
expect ( updateSplits ) . toBeCalledTimes ( 1 ) ;
183
- expect ( updateSplits ) . lastCalledWith ( splitChangesMock1 . splits , [ ] , splitChangesMock1 . till ) ;
192
+ expect ( updateSplits ) . lastCalledWith ( splitChangesMock1 . ff . d , [ ] , splitChangesMock1 . ff . t ) ;
193
+ expect ( updateRbSegments ) . toBeCalledTimes ( 0 ) ; // no rbSegments to update
184
194
expect ( registerSegments ) . toBeCalledTimes ( 1 ) ;
185
195
expect ( splitsEmitSpy ) . toBeCalledWith ( 'state::splits-arrived' ) ;
186
196
expect ( result ) . toBe ( true ) ;
187
197
} ) ;
188
198
189
- test ( 'test with payload' , async ( ) => {
199
+ test ( 'test with ff payload' , async ( ) => {
190
200
let index = 0 ;
191
201
for ( const notification of splitNotifications ) {
192
202
const payload = notification . decoded as Pick < ISplit , 'name' | 'changeNumber' | 'killed' | 'defaultTreatment' | 'trafficTypeName' | 'conditions' | 'status' | 'seed' | 'trafficAllocation' | 'trafficAllocationSeed' | 'configurations' > ;
193
203
const changeNumber = payload . changeNumber ;
194
204
195
205
await expect ( splitChangesUpdater ( undefined , undefined , { payload, changeNumber : changeNumber } ) ) . resolves . toBe ( true ) ;
196
- // fetch not being called
206
+
207
+ // fetch and RBSegments.update not being called
197
208
expect ( fetchSplitChanges ) . toBeCalledTimes ( 0 ) ;
209
+ expect ( updateRbSegments ) . toBeCalledTimes ( 0 ) ;
210
+
198
211
expect ( updateSplits ) . toBeCalledTimes ( index + 1 ) ;
199
212
// Change number being updated
200
213
expect ( updateSplits . mock . calls [ index ] [ 2 ] ) . toEqual ( changeNumber ) ;
@@ -209,6 +222,23 @@ describe('splitChangesUpdater', () => {
209
222
}
210
223
} ) ;
211
224
225
+ test ( 'test with rbsegment payload' , async ( ) => {
226
+ const payload = { name : 'rbsegment' , status : 'ACTIVE' , changeNumber : 1684329854385 , conditions : [ ] } as unknown as IRBSegment ;
227
+ const changeNumber = payload . changeNumber ;
228
+
229
+ await expect ( splitChangesUpdater ( undefined , undefined , { payload, changeNumber : changeNumber } ) ) . resolves . toBe ( true ) ;
230
+
231
+ // fetch and Splits.update not being called
232
+ expect ( fetchSplitChanges ) . toBeCalledTimes ( 0 ) ;
233
+ expect ( updateSplits ) . toBeCalledTimes ( 0 ) ;
234
+
235
+ expect ( updateRbSegments ) . toBeCalledTimes ( 1 ) ;
236
+ expect ( updateRbSegments ) . toBeCalledWith ( [ payload ] , [ ] , changeNumber ) ;
237
+
238
+ expect ( registerSegments ) . toBeCalledTimes ( 1 ) ;
239
+ expect ( registerSegments ) . toBeCalledWith ( [ ] ) ;
240
+ } ) ;
241
+
212
242
test ( 'flag sets splits-arrived emission' , async ( ) => {
213
243
const payload = splitNotifications [ 3 ] . decoded as Pick < ISplit , 'name' | 'changeNumber' | 'killed' | 'defaultTreatment' | 'trafficTypeName' | 'conditions' | 'status' | 'seed' | 'trafficAllocation' | 'trafficAllocationSeed' | 'configurations' > ;
214
244
const setMocks = [
0 commit comments