Skip to content

Commit

Permalink
fix(test): Fixed cluster-specific test TestRemoveMetricsByPrefix.
Browse files Browse the repository at this point in the history
In case of Redis cluster, keys are distributed by nodes, so need to count keys from all masters. Added keys counting for all master nodes of cluster.
  • Loading branch information
Dimedrolity committed Jul 28, 2022
1 parent 597e71d commit 38da6f8
Showing 1 changed file with 106 additions and 19 deletions.
125 changes: 106 additions & 19 deletions database/redis/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ func TestRemoveMetricsByPrefix(t *testing.T) {
client := *dataBase.client
const pattern = "my.test.*.metric*"

Convey("Given metrics with pattern my.test.*", t, func() {
Convey("Given metrics with pattern my.test.*", t, func(c C) {
for i := 1; i <= 10; i++ {
err := dataBase.SaveMetrics(
map[string]*moira.MatchedMetric{
Expand Down Expand Up @@ -900,29 +900,116 @@ func TestRemoveMetricsByPrefix(t *testing.T) {
So(err, ShouldBeNil)
}

result := client.Keys(dataBase.context, "moira-metric-data:my.test*").Val()
So(len(result), ShouldResemble, 20)
result = client.Keys(dataBase.context, "moira-metric-retention:my.test*").Val()
So(len(result), ShouldResemble, 20)
var metricsCount int32
incrementMetric := func(ctx context.Context, shard redis.UniversalClient) error {
result := len(shard.Keys(dataBase.context, "moira-metric-data:my.test*").Val())
atomic.AddInt32(&metricsCount, int32(result))
return nil
}
switch cl := client.(type) {
case *redis.ClusterClient:
err := cl.ForEachMaster(dataBase.context, func(ctx context.Context, shard *redis.Client) error {
err := incrementMetric(dataBase.context, shard)
c.So(err, ShouldBeNil)
return nil
})
So(err, ShouldBeNil)
default:
err := incrementMetric(dataBase.context, cl)
So(err, ShouldBeNil)
}
So(atomic.LoadInt32(&metricsCount), ShouldEqual, 20)

var retentionsCount int32
incrementRetention := func(ctx context.Context, shard redis.UniversalClient) error {
result := len(shard.Keys(dataBase.context, "moira-metric-retention:my.test*").Val())
atomic.AddInt32(&retentionsCount, int32(result))
return nil
}
switch cl := client.(type) {
case *redis.ClusterClient:
err := cl.ForEachMaster(dataBase.context, func(ctx context.Context, shard *redis.Client) error {
err := incrementRetention(dataBase.context, shard)
c.So(err, ShouldBeNil)
return nil
})
So(err, ShouldBeNil)
default:
err := incrementRetention(dataBase.context, cl)
So(err, ShouldBeNil)
}
So(atomic.LoadInt32(&retentionsCount), ShouldEqual, 20)

patternMetricsCount := client.SCard(dataBase.context, "moira-pattern-metrics:my.test.*.metric*").Val()
So(patternMetricsCount, ShouldResemble, int64(20))
So(patternMetricsCount, ShouldEqual, int64(20))

Convey("When remove metrics by prefix my.test.super. was called", func() {
err := dataBase.RemoveMetricsByPrefix("my.test.super.")
So(err, ShouldBeNil)

Convey("No metric data for metrics with this prefix should not exist", func() {
result = client.Keys(dataBase.context, "moira-metric-data:my.test*").Val()
So(len(result), ShouldResemble, 10)
result = client.Keys(dataBase.context, "moira-metric-retention:my.test*").Val()
So(len(result), ShouldResemble, 10)
result = client.Keys(dataBase.context, "moira-metric-data:my.test.mega.*").Val()
So(len(result), ShouldResemble, 10)
result = client.Keys(dataBase.context, "moira-metric-retention:my.test.mega.*").Val()
So(len(result), ShouldResemble, 10)
Convey("No metric data for metrics with this prefix should exist", func() {
var allMetricsCount int
switch cl := client.(type) {
case *redis.ClusterClient:
err = cl.ForEachMaster(dataBase.context, func(ctx context.Context, shard *redis.Client) error {
result := shard.Keys(dataBase.context, "moira-metric-data:my.test*").Val()
allMetricsCount += len(result)
return nil
})
So(err, ShouldBeNil)
default:
result := client.Keys(dataBase.context, "moira-metric-data:my.test*").Val()
allMetricsCount += len(result)
}
So(allMetricsCount, ShouldEqual, 10)

var allRetentionsCount int
switch cl := client.(type) {
case *redis.ClusterClient:
err = cl.ForEachMaster(dataBase.context, func(ctx context.Context, shard *redis.Client) error {
result := shard.Keys(dataBase.context, "moira-metric-retention:my.test*").Val()
allRetentionsCount += len(result)
return nil
})
So(err, ShouldBeNil)
default:
result := client.Keys(dataBase.context, "moira-metric-retention:my.test*").Val()
allRetentionsCount += len(result)
}
So(allRetentionsCount, ShouldEqual, 10)

var megaMetricsCount int
switch cl := client.(type) {
case *redis.ClusterClient:
err = cl.ForEachMaster(dataBase.context, func(ctx context.Context, shard *redis.Client) error {
result := shard.Keys(dataBase.context, "moira-metric-data:my.test.mega.*").Val()
megaMetricsCount += len(result)
return nil
})
So(err, ShouldBeNil)
default:
result := client.Keys(dataBase.context, "moira-metric-data:my.test.mega.*").Val()
megaMetricsCount += len(result)
}
So(megaMetricsCount, ShouldEqual, 10)

var megaRetentionsCount int
switch cl := client.(type) {
case *redis.ClusterClient:
err = cl.ForEachMaster(dataBase.context, func(ctx context.Context, shard *redis.Client) error {
result := shard.Keys(dataBase.context, "moira-metric-retention:my.test.mega.*").Val()
megaRetentionsCount += len(result)
return nil
})
So(err, ShouldBeNil)
default:
result := client.Keys(dataBase.context, "moira-metric-retention:my.test.mega.*").Val()
megaRetentionsCount += len(result)
}
So(megaRetentionsCount, ShouldEqual, 10)

patternMetricsCount := client.SCard(dataBase.context, "moira-pattern-metrics:my.test.*.metric*").Val()
So(patternMetricsCount, ShouldResemble, int64(10))
So(patternMetricsCount, ShouldEqual, int64(10))
})
})
})
Expand Down Expand Up @@ -1017,7 +1104,7 @@ func TestRemoveAllMetrics(t *testing.T) {
switch cl := client.(type) {
case *redis.ClusterClient:
err = cl.ForEachMaster(dataBase.context, func(ctx context.Context, shard *redis.Client) error {
result := client.Keys(dataBase.context, "moira-metric-data:*").Val()
result := shard.Keys(dataBase.context, "moira-metric-data:*").Val()
metricsCount += len(result)
return nil
})
Expand All @@ -1032,7 +1119,7 @@ func TestRemoveAllMetrics(t *testing.T) {
switch cl := client.(type) {
case *redis.ClusterClient:
err = cl.ForEachMaster(dataBase.context, func(ctx context.Context, shard *redis.Client) error {
result := client.Keys(dataBase.context, "moira-metric-retention:*").Val()
result := shard.Keys(dataBase.context, "moira-metric-retention:*").Val()
retentionsCount += len(result)
return nil
})
Expand All @@ -1047,7 +1134,7 @@ func TestRemoveAllMetrics(t *testing.T) {
switch cl := client.(type) {
case *redis.ClusterClient:
err = cl.ForEachMaster(dataBase.context, func(ctx context.Context, shard *redis.Client) error {
result := client.Keys(dataBase.context, "moira-pattern-metrics*").Val()
result := shard.Keys(dataBase.context, "moira-pattern-metrics*").Val()
patternsCount += len(result)
return nil
})
Expand Down

0 comments on commit 38da6f8

Please sign in to comment.