MCPcopy Index your code
hub / github.com/coder/coder / TestUpdateMetrics_MetricsExpire

Function TestUpdateMetrics_MetricsExpire

coderd/prometheusmetrics/aggregator_test.go:267–308  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

265}
266
267func TestUpdateMetrics_MetricsExpire(t *testing.T) {
268 t.Parallel()
269
270 // given
271 registry := prometheus.NewRegistry()
272 metricsAggregator, err := prometheusmetrics.NewMetricsAggregator(slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}), registry, time.Millisecond, agentmetrics.LabelAll)
273 require.NoError(t, err)
274
275 ctx, cancelFunc := context.WithCancel(context.Background())
276 t.Cleanup(cancelFunc)
277
278 closeFunc := metricsAggregator.Run(ctx)
279 t.Cleanup(closeFunc)
280
281 given := []*agentproto.Stats_Metric{
282 {Name: "a_counter_one", Type: agentproto.Stats_Metric_COUNTER, Value: 1},
283 }
284
285 // when
286 metricsAggregator.Update(ctx, testLabels, given)
287
288 time.Sleep(time.Millisecond * 10) // Ensure that metric is expired
289
290 // then
291 require.Eventually(t, func() bool {
292 var actual []prometheus.Metric
293 metricsCh := make(chan prometheus.Metric)
294
295 done := make(chan struct{}, 1)
296 defer close(done)
297 go func() {
298 for m := range metricsCh {
299 actual = append(actual, m)
300 }
301 done <- struct{}{}
302 }()
303 metricsAggregator.Collect(metricsCh)
304 close(metricsCh)
305 <-done
306 return len(actual) == 0
307 }, testutil.WaitShort, testutil.IntervalFast)
308}
309
310func TestLabelsAggregation(t *testing.T) {
311 t.Parallel()

Callers

nothing calls this directly

Calls 5

RunMethod · 0.95
UpdateMethod · 0.95
CollectMethod · 0.95
NewMetricsAggregatorFunction · 0.92
CleanupMethod · 0.65

Tested by

no test coverage detected