Skip to content

Commit

Permalink
test: Fix for a few data races (grafana#12129)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul1r authored and rhnasc committed Apr 12, 2024
1 parent dc318b4 commit 1359faf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
15 changes: 9 additions & 6 deletions pkg/querier/ingester_querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package querier
import (
"context"
"errors"
"go.uber.org/atomic"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -65,6 +66,8 @@ func TestIngesterQuerier_earlyExitOnQuorum(t *testing.T) {
},
}

var cnt atomic.Int32

for testName, testData := range tests {
for _, retErr := range []bool{true, false} {
testName, testData, retErr := testName, testData, retErr
Expand All @@ -75,9 +78,9 @@ func TestIngesterQuerier_earlyExitOnQuorum(t *testing.T) {
}

t.Run(testName, func(t *testing.T) {
cnt := 0
wg := sync.WaitGroup{}
wait := make(chan struct{})
cnt.Store(0)

runFn := func(args mock.Arguments) {
wg.Done()
Expand All @@ -88,7 +91,7 @@ func TestIngesterQuerier_earlyExitOnQuorum(t *testing.T) {
// ctx should be cancelled after the first two replicas return
require.ErrorIs(t, ctx.Err(), context.Canceled)
case <-wait:
cnt++
cnt.Add(1)
case <-time.After(time.Second):
t.Error("timed out waiting for ctx cancellation")
}
Expand Down Expand Up @@ -121,7 +124,7 @@ func TestIngesterQuerier_earlyExitOnQuorum(t *testing.T) {

err = testData.testFn(ingesterQuerier)
ingesterClient.AssertNumberOfCalls(t, testData.method, 3)
require.Equal(t, 2, cnt)
require.Equal(t, int32(2), cnt.Load())
if retErr {
require.ErrorContains(t, err, testData.method+" failed")
} else {
Expand Down Expand Up @@ -176,7 +179,7 @@ func TestIngesterQuerier_earlyExitOnQuorum(t *testing.T) {
}

t.Run(testName, func(t *testing.T) {
cnt := 0
cnt.Store(0)
wg := sync.WaitGroup{}
wait := make(chan struct{})

Expand All @@ -189,7 +192,7 @@ func TestIngesterQuerier_earlyExitOnQuorum(t *testing.T) {
// should not be cancelled by the tracker
require.NoError(t, ctx.Err())
case <-wait:
cnt++
cnt.Add(1)
case <-time.After(time.Second):
}
}
Expand Down Expand Up @@ -221,7 +224,7 @@ func TestIngesterQuerier_earlyExitOnQuorum(t *testing.T) {

err = testData.testFn(ingesterQuerier)
ingesterClient.AssertNumberOfCalls(t, testData.method, 3)
require.Equal(t, 2, cnt)
require.Equal(t, int32(2), cnt.Load())
if retErr {
require.ErrorContains(t, err, testData.method+" failed")
} else {
Expand Down
6 changes: 5 additions & 1 deletion pkg/querier/queryrange/split_by_interval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1852,7 +1852,11 @@ func Test_ExitEarly(t *testing.T) {

res, err := split.Do(ctx, req)

require.Equal(t, int(req.Limit), callCt)
mtx.Lock()
count := callCt
mtx.Unlock()

require.Equal(t, int(req.Limit), count)
require.NoError(t, err)
require.Equal(t, expected, res)
}
Expand Down

0 comments on commit 1359faf

Please sign in to comment.