Skip to content

Commit cd9c345

Browse files
committed
Updated test cases
1 parent 2d6b033 commit cd9c345

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

storage/redis/rulebasedsegments.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"github.com/splitio/go-toolkit/v5/redis"
1717
)
1818

19+
const rbsKey = "{rbsegment}"
20+
1921
// RuleBasedSegmentStorage is a redis-based implementation of rule-based segment storage
2022
type RuleBasedSegmentStorage struct {
2123
client *redis.PrefixedRedisClient
@@ -52,7 +54,7 @@ func (r *RuleBasedSegmentStorage) SetChangeNumber(till int64) error {
5254
}
5355

5456
func (r *RuleBasedSegmentStorage) ruleBasedSegment(ruleBased string) (*dtos.RuleBasedSegmentDTO, error) {
55-
keyToFetch := strings.Replace(KeyRuleBasedSegment, "{rbsegment}", ruleBased, 1)
57+
keyToFetch := strings.Replace(KeyRuleBasedSegment, rbsKey, ruleBased, 1)
5658
val, err := r.client.Get(keyToFetch)
5759

5860
if err != nil {
@@ -113,14 +115,14 @@ func (r *RuleBasedSegmentStorage) RuleBasedSegmentNames() ([]string, error) {
113115
if err != nil {
114116
return nil, err
115117
}
116-
return cleanPrefixedKeys(keys, strings.Replace(KeyRuleBasedSegment, "{rbsegment}", "", 1)), nil
118+
return cleanPrefixedKeys(keys, strings.Replace(KeyRuleBasedSegment, rbsKey, "", 1)), nil
117119
}
118120

119121
func (r *RuleBasedSegmentStorage) ruleBasedSegmentKeys() ([]string, error) {
120122
if !r.client.ClusterMode() {
121123
var cursor uint64
122124
ruleBasedSegmentKeys := make([]string, 0)
123-
scanKey := strings.Replace(KeyRuleBasedSegment, "{rbsegment}", "*", 1)
125+
scanKey := strings.Replace(KeyRuleBasedSegment, rbsKey, "*", 1)
124126
for {
125127
keys, rCursor, err := r.client.Scan(cursor, scanKey, DefaultScanCount)
126128
if err != nil {
@@ -287,7 +289,7 @@ func (r *RuleBasedSegmentStorage) fetchCurrentRuleBasedSegments(toAdd []dtos.Rul
287289
keys := make([]string, 0, len(toAdd)+len(toRemove))
288290
for _, source := range [][]dtos.RuleBasedSegmentDTO{toAdd, toRemove} {
289291
for idx := range source {
290-
keys = append(keys, strings.Replace(KeyRuleBasedSegment, "{rbsegment}", source[idx].Name, 1))
292+
keys = append(keys, strings.Replace(KeyRuleBasedSegment, rbsKey, source[idx].Name, 1))
291293
}
292294
}
293295

storage/redis/splits_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/splitio/go-toolkit/v5/logging"
1919
"github.com/splitio/go-toolkit/v5/redis"
2020
"github.com/splitio/go-toolkit/v5/redis/mocks"
21+
"github.com/stretchr/testify/assert"
2122
)
2223

2324
func createSampleSplit(name string, sets []string) dtos.SplitDTO {
@@ -1372,3 +1373,49 @@ func TestLargeSegmentNames(t *testing.T) {
13721373
t.Error(segments)
13731374
}
13741375
}
1376+
1377+
func TestReplaceAll(t *testing.T) {
1378+
logger := logging.NewLogger(nil)
1379+
prefix := "commons_update_prefix"
1380+
1381+
redisClient, err := NewRedisClient(&conf.RedisConfig{
1382+
Host: "localhost",
1383+
Port: 6379,
1384+
Prefix: prefix,
1385+
Database: 1,
1386+
}, logger)
1387+
if err != nil {
1388+
t.Error("It should be nil")
1389+
}
1390+
toAdd := []dtos.SplitDTO{createSampleSplit("split1", []string{}), createSampleSplit("split2", []string{}), createSampleSplit("split3", []string{})}
1391+
1392+
splitStorage := NewSplitStorage(redisClient, logging.NewLogger(&logging.LoggerOptions{}), flagsets.NewFlagSetFilter(nil))
1393+
splitStorage.Update(toAdd, []dtos.SplitDTO{}, 1)
1394+
1395+
splits := splitStorage.All()
1396+
assert.Equal(t, 3, len(splits), "Unexpected amount of split")
1397+
till, _ := redisClient.Get("SPLITIO.splits.till")
1398+
tillInt, _ := strconv.ParseInt(till, 0, 64)
1399+
assert.Equal(t, int64(1), tillInt, "ChangeNumber should be 1")
1400+
1401+
toReplace := []dtos.SplitDTO{createSampleSplit("split4", []string{}), createSampleSplit("split5", []string{})}
1402+
1403+
splitStorage.ReplaceAll(toReplace, 1)
1404+
1405+
splits = splitStorage.All()
1406+
assert.Equal(t, 2, len(splits), "Unexpected size")
1407+
1408+
till, _ = redisClient.Get("SPLITIO.splits.till")
1409+
tillInt, _ = strconv.ParseInt(till, 0, 64)
1410+
assert.Equal(t, int64(1), tillInt, "ChangeNumber should be 1")
1411+
1412+
keys := []string{
1413+
"SPLITIO.split.split1",
1414+
"SPLITIO.split.split2",
1415+
"SPLITIO.split.split3",
1416+
"SPLITIO.split.split4",
1417+
"SPLITIO.split.split5",
1418+
"SPLITIO.splits.till",
1419+
}
1420+
redisClient.Del(keys...)
1421+
}

0 commit comments

Comments
 (0)