Path: blob/main/component/loki/process/internal/metric/counters_test.go
4096 views
package metric12import (3"testing"4"time"56"github.com/prometheus/client_golang/prometheus"7"github.com/prometheus/common/model"8"github.com/stretchr/testify/assert"9)1011func TestCounterExpiration(t *testing.T) {12t.Parallel()13cfg := &CounterConfig{14Action: "inc",15Description: "HELP ME!!!!!",16MaxIdle: 1 * time.Second,17}1819cnt, err := NewCounters("test1", cfg)20assert.Nil(t, err)2122// Create a label and increment the counter23lbl1 := model.LabelSet{}24lbl1["test"] = "i don't wanna make this a constant"25cnt.With(lbl1).Inc()2627// Collect the metrics, should still find the metric in the map28collect(cnt)29assert.Contains(t, cnt.metrics, lbl1.Fingerprint())3031time.Sleep(1100 * time.Millisecond) // Wait just past our max idle of 1 sec3233//Add another counter with new label val34lbl2 := model.LabelSet{}35lbl2["test"] = "eat this linter"36cnt.With(lbl2).Inc()3738// Collect the metrics, first counter should have expired and removed, second should still be present39collect(cnt)40assert.NotContains(t, cnt.metrics, lbl1.Fingerprint())41assert.Contains(t, cnt.metrics, lbl2.Fingerprint())42}4344func collect(c prometheus.Collector) {45done := make(chan struct{})46collector := make(chan prometheus.Metric)4748go func() {49defer close(done)50c.Collect(collector)51}()5253for {54select {55case <-collector:56case <-done:57return58}59}60}616263