Skip to content

Commit cc785f0

Browse files
authored
Fix race in TestFrontend_LogsSlowQueriesFormValues. (#3440)
Race is between buf.String() and writes to the buf from other goroutines. Signed-off-by: Peter Štibraný <peter.stibrany@grafana.com>
1 parent 2acc6db commit cc785f0

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

pkg/querier/frontend/frontend_test.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"net/url"
1313
strconv "strconv"
1414
"strings"
15+
"sync"
1516
"testing"
1617
"time"
1718

@@ -250,6 +251,25 @@ func TestFrontendCheckReady(t *testing.T) {
250251
}
251252
}
252253

254+
type syncBuf struct {
255+
mu sync.Mutex
256+
buf bytes.Buffer
257+
}
258+
259+
func (sb *syncBuf) Write(p []byte) (n int, err error) {
260+
sb.mu.Lock()
261+
defer sb.mu.Unlock()
262+
263+
return sb.buf.Write(p)
264+
}
265+
266+
func (sb *syncBuf) String() string {
267+
sb.mu.Lock()
268+
defer sb.mu.Unlock()
269+
270+
return sb.buf.String()
271+
}
272+
253273
func TestFrontend_LogsSlowQueriesFormValues(t *testing.T) {
254274
// Create an HTTP server listening locally. This server mocks the downstream
255275
// Prometheus API-compatible server.
@@ -271,8 +291,8 @@ func TestFrontend_LogsSlowQueriesFormValues(t *testing.T) {
271291
config.Handler.LogQueriesLongerThan = 1 * time.Microsecond
272292
config.DownstreamURL = fmt.Sprintf("http://%s", downstreamListen.Addr())
273293

274-
var buf bytes.Buffer
275-
l := log.NewLogfmtLogger(log.NewSyncWriter(&buf))
294+
var buf syncBuf
295+
l := log.NewLogfmtLogger(&buf)
276296

277297
test := func(addr string) {
278298
data := url.Values{}

0 commit comments

Comments
 (0)