From 1c5e7f1ab0418784bcf555309226d391580f4256 Mon Sep 17 00:00:00 2001 From: Mikhail Nozdrachev Date: Tue, 3 Sep 2024 12:07:32 +0200 Subject: [PATCH] test: Fix flaky receive/multitsdb test (#7694) There is race condition in `TestMultiTSDBPrune` due to a dangling goroutine which can fail outside of the test function's lifetime if the database object is closed before `Sync()` is finished. Signed-off-by: Mikhail Nozdrachev --- pkg/receive/multitsdb_test.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg/receive/multitsdb_test.go b/pkg/receive/multitsdb_test.go index 6f5dd01ae7..eb82c22281 100644 --- a/pkg/receive/multitsdb_test.go +++ b/pkg/receive/multitsdb_test.go @@ -462,10 +462,14 @@ func TestMultiTSDBPrune(t *testing.T) { testutil.Equals(t, 3, len(m.TSDBLocalClients())) ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + + g := sync.WaitGroup{} + defer func() { cancel(); g.Wait() }() if test.bucket != nil { + g.Add(1) go func() { + defer g.Done() testutil.Ok(t, syncTSDBs(ctx, m, 10*time.Millisecond)) }() }