Add a unit test guarantees ClearState will fully clear a collector.

This commit is contained in:
RainbowMango 2019-12-12 20:05:28 +08:00
parent 0d58709016
commit 44a0d04b96
2 changed files with 35 additions and 0 deletions

View File

@ -175,6 +175,7 @@ func (bsc *BaseStableCollector) ClearState() {
bsc.descriptors = nil
bsc.registrable = nil
bsc.hidden = nil
bsc.self = nil
}
// HiddenMetrics tells the list of hidden metrics with fqName.

View File

@ -18,6 +18,7 @@ package metrics
import (
"fmt"
"reflect"
"strings"
"testing"
@ -134,3 +135,36 @@ func TestInvalidCustomCollector(t *testing.T) {
})
}
}
// TestCustomCollectorClearState guarantees `ClearState()` will fully clear a collector.
// It is necessary because we may forget to clear some new-added fields in the future.
func TestCustomCollectorClearState(t *testing.T) {
var currentVersion = parseVersion(apimachineryversion.Info{
Major: "1",
Minor: "17",
GitVersion: "v1.17.0-alpha-1.12345",
})
var (
alphaDesc = NewDesc("metric_alpha", "alpha metric", []string{"name"}, nil,
ALPHA, "")
stableDesc = NewDesc("metric_stable", "stable metrics", []string{"name"}, nil,
STABLE, "")
deprecatedDesc = NewDesc("metric_deprecated", "stable deprecated metrics", []string{"name"}, nil,
STABLE, "1.17.0")
hiddenDesc = NewDesc("metric_hidden", "stable hidden metrics", []string{"name"}, nil,
STABLE, "1.16.0")
)
benchmarkA := newTestCustomCollector(alphaDesc, stableDesc, deprecatedDesc, hiddenDesc)
benchmarkB := newTestCustomCollector(alphaDesc, stableDesc, deprecatedDesc, hiddenDesc)
if benchmarkA.Create(&currentVersion, benchmarkA) == false {
t.Fatal("collector should be created")
}
benchmarkA.ClearState()
if !reflect.DeepEqual(*benchmarkA, *benchmarkB) {
t.Fatal("custom collector state hasn't be fully cleared")
}
}