From 316b19a5d164fd508e53b46d637f326bceb1cdf0 Mon Sep 17 00:00:00 2001 From: Roman Khavronenko Date: Fri, 19 Apr 2024 09:06:40 +0200 Subject: [PATCH] app/vmalert: make `TestGroupStart` more reliable (#6130) There was a sleep statement in the test, waiting for Group to perform a couple of evaluation. But looks like it worked unreliable for some CI tests like the one below https://github.com/VictoriaMetrics/VictoriaMetrics/actions/runs/8718213844/job/23915007958?pr=6115 This commit changes the sleep statement on a function that waits for a specific number of evaluations. It should make this test faster in general case, and more reliable for slow environemnts. --- app/vmalert/rule/group_test.go | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/app/vmalert/rule/group_test.go b/app/vmalert/rule/group_test.go index d1777b6c5..b7560c1ae 100644 --- a/app/vmalert/rule/group_test.go +++ b/app/vmalert/rule/group_test.go @@ -217,7 +217,6 @@ func TestGroupStart(t *testing.T) { const evalInterval = time.Millisecond g := NewGroup(groups[0], fs, evalInterval, map[string]string{"cluster": "east-1"}) - g.Concurrency = 2 const inst1, inst2, job = "foo", "bar", "baz" m1 := metricWithLabels(t, "instance", inst1, "job", job) @@ -262,8 +261,25 @@ func TestGroupStart(t *testing.T) { close(finished) }() - // wait for multiple evals - time.Sleep(20 * evalInterval) + waitForIterations := func(n int, interval time.Duration) { + t.Helper() + + var cur uint64 + prev := g.metrics.iterationTotal.Get() + for i := 0; ; i++ { + if i > 40 { + t.Fatalf("group wasn't able to perform %d evaluations during %d eval intervals", n, i) + } + cur = g.metrics.iterationTotal.Get() + if int(cur-prev) >= n { + return + } + time.Sleep(interval) + } + } + + // wait for multiple evaluation iterations + waitForIterations(4, evalInterval) gotAlerts := fn.GetAlerts() expectedAlerts := []notifier.Alert{*alert1, *alert2} @@ -280,8 +296,8 @@ func TestGroupStart(t *testing.T) { // and set only one datapoint for response fs.Add(m1) - // wait for multiple evals - time.Sleep(20 * evalInterval) + // wait for multiple evaluation iterations + waitForIterations(4, evalInterval) gotAlerts = fn.GetAlerts() alert2.State = notifier.StateInactive