Skip to content

Commit

Permalink
additional asserts for metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
vpranckaitis committed Mar 5, 2021
1 parent 05131b5 commit 5afa0b4
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions src/dbnode/integration/bootstrap_retries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ func TestBootstrapRetriesDueToError(t *testing.T) {
}()

assert.True(t, setup.DB().IsBootstrapped(), "database should be bootstrapped")
retryCounter := getBootstrapRetriesCounter(testScope, "other")
require.NotNil(t, retryCounter)
assert.Equal(t, int64(1), retryCounter.Value())
assertRetryMetric(t, testScope, "other")
}

func TestBootstrapRetriesDueToObsoleteRanges(t *testing.T) {
Expand Down Expand Up @@ -131,9 +129,7 @@ func TestBootstrapRetriesDueToObsoleteRanges(t *testing.T) {
}()

assert.True(t, setup.DB().IsBootstrapped(), "database should be bootstrapped")
retryCounter := getBootstrapRetriesCounter(testScope, "obsolete-ranges")
require.NotNil(t, retryCounter)
assert.Equal(t, int64(1), retryCounter.Value())
assertRetryMetric(t, testScope, "obsolete-ranges")
}

func TestBootstrapRetriesDueToUnfulfilledRanges(t *testing.T) {
Expand Down Expand Up @@ -187,9 +183,7 @@ func TestBootstrapRetriesDueToUnfulfilledRanges(t *testing.T) {

assert.True(t, setup.DB().IsBootstrapped(), "database should be bootstrapped")

retryCounter := getBootstrapRetriesCounter(testScope, "other")
require.NotNil(t, retryCounter)
assert.Equal(t, int64(1), retryCounter.Value())
assertRetryMetric(t, testScope, "other")
}

type bootstrapFn = func(
Expand Down Expand Up @@ -236,12 +230,29 @@ func bootstrapRetryTestSetup(t *testing.T, bootstrapFn bootstrapFn) (TestSetup,
return setup, testScope
}

func getBootstrapRetriesCounter(testScope tally.TestScope, reason string) tally.CounterSnapshot {
for _, snapshot := range testScope.Snapshot().Counters() {
if r, ok := snapshot.Tags()["reason"]; ok &&
strings.Contains(snapshot.Name(), "bootstrap-retries") && r == reason {
return snapshot
func assertRetryMetric(t *testing.T, testScope tally.TestScope, expectedReason string) {
const (
metricName = "bootstrap-retries"
reasonTag = "reason"
)
valuesByReason := make(map[string]int)
for _, counter := range testScope.Snapshot().Counters() {
if strings.Contains(counter.Name(), metricName) {
reason := ""
if r, ok := counter.Tags()[reasonTag]; ok {
reason = r
}
valuesByReason[reason] = int(counter.Value())
}
}

val, ok := valuesByReason[expectedReason]
if assert.True(t, ok, "missing metric for expected reason") {
assert.Equal(t, 1, val)
}
for r, val := range valuesByReason {
if r != expectedReason {
assert.Equal(t, 0, val)
}
}
return nil
}

0 comments on commit 5afa0b4

Please sign in to comment.