Skip to content

Commit 1369d66

Browse files
authored
PL-56357 - Fix regex used to generate notifications (#359)
**What** - Updates the regex's used to extract flag names from redis keys to handle underscores **Why** - When the Primary restarts it diffs the new and old config and sends notifications if there have been any changes. The Regex used to extract the identifier wasn't handling underscores and was truncating flag identifiers. This meant it sent notifications with incorrect flag identifiers to SDKs. **Testing** - Added a unit test for the `BuildNotification` method - Tested manually locally
1 parent 42d14c7 commit 1369d66

File tree

2 files changed

+111
-2
lines changed

2 files changed

+111
-2
lines changed

repository/inventory_repo.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ const (
2222
)
2323

2424
var (
25-
featureConfigRegEx = regexp.MustCompile(`env-([a-zA-Z0-9-]+)-feature-config-([a-zA-Z0-9-]+)`)
26-
segmentConfigRegEx = regexp.MustCompile(`env-([a-zA-Z0-9-]+)-segment-([a-zA-Z0-9-]+)`)
25+
featureConfigRegEx = regexp.MustCompile(`env-([a-zA-Z0-9-_]+)-feature-config-([a-zA-Z0-9-_]+)`)
26+
segmentConfigRegEx = regexp.MustCompile(`env-([a-zA-Z0-9-]+)-segment-([a-zA-Z0-9-_]+)`)
2727
)
2828

2929
// InventoryRepo is a repository that stores all references to all assets for the key.

repository/inventory_repo_test.go

+109
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,112 @@ func TestInventoryRepo_Cleanup(t *testing.T) {
211211
})
212212
}
213213
}
214+
215+
func TestInventoryRepo_BuildNotificatons(t *testing.T) {
216+
217+
type args struct {
218+
assets domain.Assets
219+
}
220+
221+
type expected struct {
222+
notifications []domain.SSEMessage
223+
}
224+
225+
testCases := map[string]struct {
226+
args args
227+
expected expected
228+
}{
229+
"Given I have assets with no underscores": {
230+
args: args{
231+
assets: domain.Assets{
232+
Deleted: map[string]string{
233+
"env-1234-feature-config-foobar": "",
234+
},
235+
Created: map[string]string{
236+
"env-1234-feature-config-helloworld": "",
237+
},
238+
Patched: map[string]string{
239+
"env-1234-segment-foobar": "",
240+
},
241+
},
242+
},
243+
expected: expected{
244+
notifications: []domain.SSEMessage{
245+
{
246+
Event: "delete",
247+
Domain: "flag",
248+
Identifier: "foobar",
249+
Version: 0,
250+
Environment: "1234",
251+
},
252+
{
253+
Event: "create",
254+
Domain: "flag",
255+
Identifier: "helloworld",
256+
Version: 0,
257+
Environment: "1234",
258+
},
259+
{
260+
Event: "patch",
261+
Domain: "target-segment",
262+
Identifier: "foobar",
263+
Version: 0,
264+
Environment: "1234",
265+
},
266+
},
267+
},
268+
},
269+
"Given I have assets with underscores": {
270+
args: args{
271+
assets: domain.Assets{
272+
Deleted: map[string]string{
273+
"env-1234-feature-config-PIE_ENABLE_THIS_THING": "",
274+
},
275+
Created: map[string]string{
276+
"env-1234-feature-config-_CDS__ENABLED___FLAG": "",
277+
},
278+
Patched: map[string]string{
279+
"env-1234-segment-_SOME_SPECIAL_SEGMENT__": "",
280+
},
281+
},
282+
},
283+
expected: expected{
284+
notifications: []domain.SSEMessage{
285+
{
286+
Event: "delete",
287+
Domain: "flag",
288+
Identifier: "PIE_ENABLE_THIS_THING",
289+
Version: 0,
290+
Environment: "1234",
291+
},
292+
{
293+
Event: "create",
294+
Domain: "flag",
295+
Identifier: "_CDS__ENABLED___FLAG",
296+
Version: 0,
297+
Environment: "1234",
298+
},
299+
{
300+
Event: "patch",
301+
Domain: "target-segment",
302+
Identifier: "_SOME_SPECIAL_SEGMENT__",
303+
Version: 0,
304+
Environment: "1234",
305+
},
306+
},
307+
},
308+
},
309+
}
310+
311+
for desc, tc := range testCases {
312+
desc := desc
313+
tc := tc
314+
315+
t.Run(desc, func(t *testing.T) {
316+
317+
i := InventoryRepo{}
318+
actual := i.BuildNotifications(tc.args.assets)
319+
assert.Equal(t, tc.expected.notifications, actual)
320+
})
321+
}
322+
}

0 commit comments

Comments
 (0)