Skip to content

Commit 1359faf

Browse files
paul1rrhnasc
authored andcommitted
test: Fix for a few data races (grafana#12129)
1 parent dc318b4 commit 1359faf

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

pkg/querier/ingester_querier_test.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package querier
33
import (
44
"context"
55
"errors"
6+
"go.uber.org/atomic"
67
"sync"
78
"testing"
89
"time"
@@ -65,6 +66,8 @@ func TestIngesterQuerier_earlyExitOnQuorum(t *testing.T) {
6566
},
6667
}
6768

69+
var cnt atomic.Int32
70+
6871
for testName, testData := range tests {
6972
for _, retErr := range []bool{true, false} {
7073
testName, testData, retErr := testName, testData, retErr
@@ -75,9 +78,9 @@ func TestIngesterQuerier_earlyExitOnQuorum(t *testing.T) {
7578
}
7679

7780
t.Run(testName, func(t *testing.T) {
78-
cnt := 0
7981
wg := sync.WaitGroup{}
8082
wait := make(chan struct{})
83+
cnt.Store(0)
8184

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

122125
err = testData.testFn(ingesterQuerier)
123126
ingesterClient.AssertNumberOfCalls(t, testData.method, 3)
124-
require.Equal(t, 2, cnt)
127+
require.Equal(t, int32(2), cnt.Load())
125128
if retErr {
126129
require.ErrorContains(t, err, testData.method+" failed")
127130
} else {
@@ -176,7 +179,7 @@ func TestIngesterQuerier_earlyExitOnQuorum(t *testing.T) {
176179
}
177180

178181
t.Run(testName, func(t *testing.T) {
179-
cnt := 0
182+
cnt.Store(0)
180183
wg := sync.WaitGroup{}
181184
wait := make(chan struct{})
182185

@@ -189,7 +192,7 @@ func TestIngesterQuerier_earlyExitOnQuorum(t *testing.T) {
189192
// should not be cancelled by the tracker
190193
require.NoError(t, ctx.Err())
191194
case <-wait:
192-
cnt++
195+
cnt.Add(1)
193196
case <-time.After(time.Second):
194197
}
195198
}
@@ -221,7 +224,7 @@ func TestIngesterQuerier_earlyExitOnQuorum(t *testing.T) {
221224

222225
err = testData.testFn(ingesterQuerier)
223226
ingesterClient.AssertNumberOfCalls(t, testData.method, 3)
224-
require.Equal(t, 2, cnt)
227+
require.Equal(t, int32(2), cnt.Load())
225228
if retErr {
226229
require.ErrorContains(t, err, testData.method+" failed")
227230
} else {

pkg/querier/queryrange/split_by_interval_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -1852,7 +1852,11 @@ func Test_ExitEarly(t *testing.T) {
18521852

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

1855-
require.Equal(t, int(req.Limit), callCt)
1855+
mtx.Lock()
1856+
count := callCt
1857+
mtx.Unlock()
1858+
1859+
require.Equal(t, int(req.Limit), count)
18561860
require.NoError(t, err)
18571861
require.Equal(t, expected, res)
18581862
}

0 commit comments

Comments
 (0)