From 01d1e49c54a4233c67e16e510cc56e0288b8ce5a Mon Sep 17 00:00:00 2001 From: Joe Blubaugh Date: Tue, 5 Jul 2022 22:00:13 +0800 Subject: [PATCH] Simplify Silence test to remove unnecessary wait. As noted in #2867, there is an unnecessary require.Eventually in a silence test. This PR addresses that by using a channel to signal that that the maintenance loop has completed. Signed-off-by: Joe Blubaugh --- silence/silence_test.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/silence/silence_test.go b/silence/silence_test.go index a0d1c07b..20a84dd1 100644 --- a/silence/silence_test.go +++ b/silence/silence_test.go @@ -29,7 +29,6 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/common/model" "github.com/stretchr/testify/require" - "go.uber.org/atomic" pb "github.com/prometheus/alertmanager/silence/silencepb" "github.com/prometheus/alertmanager/types" @@ -211,21 +210,26 @@ func TestSilences_Maintenance_SupportsCustomCallback(t *testing.T) { s := &Silences{st: state{}, logger: log.NewNopLogger(), clock: clock, metrics: newMetrics(nil, nil)} stopc := make(chan struct{}) - calls := atomic.NewInt32(0) - go s.Maintenance(100*time.Millisecond, f.Name(), stopc, func() (int64, error) { - calls.Add(1) - return 0, nil - }) + called := make(chan struct{}, 5) + go func() { + s.Maintenance(100*time.Millisecond, f.Name(), stopc, func() (int64, error) { + called <- struct{}{} + return 0, nil + }) + close(called) + }() runtime.Gosched() clock.Add(100 * time.Millisecond) // Stop the maintenance loop. We should get exactly one more execution of the maintenance func. close(stopc) + calls := 0 + for range called { + calls++ + } - require.Eventually(t, func() bool { - return calls.Load() == 2 - }, 100*time.Millisecond, 1*time.Millisecond) + require.EqualValues(t, 2, calls) } func TestSilencesSetSilence(t *testing.T) {