mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-04 04:08:16 +00:00 
			
		
		
		
	Fix sample_and_watermark_test.go for bad luck, repeated test
This commit is contained in:
		@@ -17,13 +17,13 @@ limitations under the License.
 | 
			
		||||
package metrics
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"math/rand"
 | 
			
		||||
	"testing"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	compbasemetrics "k8s.io/component-base/metrics"
 | 
			
		||||
	"k8s.io/component-base/metrics/legacyregistry"
 | 
			
		||||
	"k8s.io/klog/v2"
 | 
			
		||||
	testclock "k8s.io/utils/clock/testing"
 | 
			
		||||
)
 | 
			
		||||
@@ -36,6 +36,8 @@ const (
 | 
			
		||||
	numIterations         = 100
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var errMetricNotFound = errors.New("not found")
 | 
			
		||||
 | 
			
		||||
/* TestSampler does a rough behavioral test of the sampling in a
 | 
			
		||||
   SampleAndWatermarkHistograms.  The test creates one and exercises
 | 
			
		||||
   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},
 | 
			
		||||
		[]string{})
 | 
			
		||||
	saw := gen.Generate(0, 1, []string{})
 | 
			
		||||
	regs := gen.metrics()
 | 
			
		||||
	for _, reg := range regs {
 | 
			
		||||
		legacyregistry.MustRegister(reg)
 | 
			
		||||
	toRegister := gen.metrics()
 | 
			
		||||
	registry := compbasemetrics.NewKubeRegistry()
 | 
			
		||||
	for _, reg := range toRegister {
 | 
			
		||||
		registry.MustRegister(reg)
 | 
			
		||||
	}
 | 
			
		||||
	// `dt` is the admitted cumulative difference in fake time
 | 
			
		||||
	// since the start of the test.  "admitted" means this is
 | 
			
		||||
@@ -83,8 +86,8 @@ func TestSampler(t *testing.T) {
 | 
			
		||||
		clk.SetTime(t1)
 | 
			
		||||
		saw.Observe(1)
 | 
			
		||||
		expectedCount := int64(dt / samplingPeriod)
 | 
			
		||||
		actualCount, err := getHistogramCount(regs, samplesHistName)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
		actualCount, err := getHistogramCount(registry, samplesHistName)
 | 
			
		||||
		if err != nil && !(err == errMetricNotFound && expectedCount == 0) {
 | 
			
		||||
			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)
 | 
			
		||||
@@ -94,28 +97,26 @@ func TestSampler(t *testing.T) {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* getHistogramCount returns the count of the named histogram */
 | 
			
		||||
func getHistogramCount(regs Registerables, metricName string) (int64, error) {
 | 
			
		||||
	considered := []string{}
 | 
			
		||||
	mfs, err := legacyregistry.DefaultGatherer.Gather()
 | 
			
		||||
/* getHistogramCount returns the count of the named histogram or an error (if any) */
 | 
			
		||||
func getHistogramCount(registry compbasemetrics.KubeRegistry, metricName string) (int64, error) {
 | 
			
		||||
	mfs, err := registry.Gather()
 | 
			
		||||
	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 {
 | 
			
		||||
		thisName := mf.GetName()
 | 
			
		||||
		if thisName != metricName {
 | 
			
		||||
			considered = append(considered, thisName)
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		metric := mf.GetMetric()[0]
 | 
			
		||||
		hist := metric.GetHistogram()
 | 
			
		||||
		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 {
 | 
			
		||||
			return 0, fmt.Errorf("dto.Histogram has nil SampleCount")
 | 
			
		||||
			return 0, errors.New("dto.Histogram has nil SampleCount")
 | 
			
		||||
		}
 | 
			
		||||
		return int64(*hist.SampleCount), nil
 | 
			
		||||
	}
 | 
			
		||||
	return 0, fmt.Errorf("not found, considered=%#+v", considered)
 | 
			
		||||
	return 0, errMetricNotFound
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user