Fix sample_and_watermark_test.go for bad luck, repeated test

This commit is contained in:
Mike Spreitzer 2021-11-10 15:30:50 -05:00
parent 45f77ca4ba
commit 06e17165b1

View File

@ -17,13 +17,13 @@ limitations under the License.
package metrics package metrics
import ( import (
"errors"
"fmt" "fmt"
"math/rand" "math/rand"
"testing" "testing"
"time" "time"
compbasemetrics "k8s.io/component-base/metrics" compbasemetrics "k8s.io/component-base/metrics"
"k8s.io/component-base/metrics/legacyregistry"
"k8s.io/klog/v2" "k8s.io/klog/v2"
testclock "k8s.io/utils/clock/testing" testclock "k8s.io/utils/clock/testing"
) )
@ -36,6 +36,8 @@ const (
numIterations = 100 numIterations = 100
) )
var errMetricNotFound = errors.New("not found")
/* TestSampler does a rough behavioral test of the sampling in a /* TestSampler does a rough behavioral test of the sampling in a
SampleAndWatermarkHistograms. The test creates one and exercises SampleAndWatermarkHistograms. The test creates one and exercises
it, checking that the count in the sampling histogram is correct at it, checking that the count in the sampling histogram is correct at
@ -59,9 +61,10 @@ func TestSampler(t *testing.T) {
&compbasemetrics.HistogramOpts{Name: "marks", Buckets: buckets}, &compbasemetrics.HistogramOpts{Name: "marks", Buckets: buckets},
[]string{}) []string{})
saw := gen.Generate(0, 1, []string{}) saw := gen.Generate(0, 1, []string{})
regs := gen.metrics() toRegister := gen.metrics()
for _, reg := range regs { registry := compbasemetrics.NewKubeRegistry()
legacyregistry.MustRegister(reg) for _, reg := range toRegister {
registry.MustRegister(reg)
} }
// `dt` is the admitted cumulative difference in fake time // `dt` is the admitted cumulative difference in fake time
// since the start of the test. "admitted" means this is // since the start of the test. "admitted" means this is
@ -83,8 +86,8 @@ func TestSampler(t *testing.T) {
clk.SetTime(t1) clk.SetTime(t1)
saw.Observe(1) saw.Observe(1)
expectedCount := int64(dt / samplingPeriod) expectedCount := int64(dt / samplingPeriod)
actualCount, err := getHistogramCount(regs, samplesHistName) actualCount, err := getHistogramCount(registry, samplesHistName)
if err != nil { if err != nil && !(err == errMetricNotFound && expectedCount == 0) {
t.Fatalf("For t0=%s, t1=%s, failed to getHistogramCount: %#+v", t0, t1, err) t.Fatalf("For t0=%s, t1=%s, failed to getHistogramCount: %#+v", t0, t1, err)
} }
t.Logf("For i=%d, ddt=%s, t1=%s, diff=%s, dt=%s, count=%d", i, ddt, t1, diff, dt, actualCount) t.Logf("For i=%d, ddt=%s, t1=%s, diff=%s, dt=%s, count=%d", i, ddt, t1, diff, dt, actualCount)
@ -94,28 +97,26 @@ func TestSampler(t *testing.T) {
} }
} }
/* getHistogramCount returns the count of the named histogram */ /* getHistogramCount returns the count of the named histogram or an error (if any) */
func getHistogramCount(regs Registerables, metricName string) (int64, error) { func getHistogramCount(registry compbasemetrics.KubeRegistry, metricName string) (int64, error) {
considered := []string{} mfs, err := registry.Gather()
mfs, err := legacyregistry.DefaultGatherer.Gather()
if err != nil { if err != nil {
return 0, fmt.Errorf("failed to gather metrics: %s", err) return 0, fmt.Errorf("failed to gather metrics: %w", err)
} }
for _, mf := range mfs { for _, mf := range mfs {
thisName := mf.GetName() thisName := mf.GetName()
if thisName != metricName { if thisName != metricName {
considered = append(considered, thisName)
continue continue
} }
metric := mf.GetMetric()[0] metric := mf.GetMetric()[0]
hist := metric.GetHistogram() hist := metric.GetHistogram()
if hist == nil { if hist == nil {
return 0, fmt.Errorf("dto.Metric has nil Histogram") return 0, errors.New("dto.Metric has nil Histogram")
} }
if hist.SampleCount == nil { if hist.SampleCount == nil {
return 0, fmt.Errorf("dto.Histogram has nil SampleCount") return 0, errors.New("dto.Histogram has nil SampleCount")
} }
return int64(*hist.SampleCount), nil return int64(*hist.SampleCount), nil
} }
return 0, fmt.Errorf("not found, considered=%#+v", considered) return 0, errMetricNotFound
} }