Skip to content

Commit

Permalink
Query: add exemplarsServer data race test (thanos-io#4777)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanjm committed Oct 28, 2021
1 parent 02ce1ad commit 217ca54
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions pkg/exemplars/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"os"
"reflect"
"sync"
"testing"

"github.com/go-kit/kit/log"
Expand All @@ -17,14 +18,15 @@ import (
"github.com/thanos-io/thanos/pkg/store/labelpb"
"github.com/thanos-io/thanos/pkg/store/storepb"
"github.com/thanos-io/thanos/pkg/testutil"
"go.uber.org/atomic"
"google.golang.org/grpc"
)

type testExemplarClient struct {
grpc.ClientStream
exemplarErr, recvErr error
response *exemplarspb.ExemplarsResponse
sentResponse bool
sentResponse atomic.Bool
}

func (t *testExemplarClient) String() string {
Expand All @@ -37,10 +39,10 @@ func (t *testExemplarClient) Recv() (*exemplarspb.ExemplarsResponse, error) {
return nil, t.recvErr
}

if t.sentResponse {
if t.sentResponse.Load() {
return nil, io.EOF
}
t.sentResponse = true
t.sentResponse.Store(true)

return t.response, nil
}
Expand All @@ -55,6 +57,7 @@ type testExemplarServer struct {
grpc.ServerStream
sendErr error
responses []*exemplarspb.ExemplarsResponse
mu sync.Mutex
}

func (t *testExemplarServer) String() string {
Expand All @@ -65,6 +68,8 @@ func (t *testExemplarServer) Send(response *exemplarspb.ExemplarsResponse) error
if t.sendErr != nil {
return t.sendErr
}
t.mu.Lock()
defer t.mu.Unlock()
t.responses = append(t.responses, response)
return nil
}
Expand Down Expand Up @@ -286,3 +291,31 @@ func TestProxy(t *testing.T) {
})
}
}

// TestProxyDataRace find the concurrent data race bug.
// go test -race -run TestProxyDataRace -v
func TestProxyDataRace(t *testing.T) {
logger := log.NewLogfmtLogger(os.Stderr)
p := NewProxy(logger, func() []*exemplarspb.ExemplarStore {
es := &exemplarspb.ExemplarStore{
ExemplarsClient: &testExemplarClient{
recvErr: errors.New("err"),
},
LabelSets: []labels.Labels{labels.FromMap(map[string]string{"cluster": "A"})},
}
size := 100
endpoints := make([]*exemplarspb.ExemplarStore, 0, size)
for i := 0; i < size; i++ {
endpoints = append(endpoints, es)
}
return endpoints
}, labels.FromMap(map[string]string{"query": "foo"}))
req := &exemplarspb.ExemplarsRequest{
Query: `http_request_duration_bucket{query="foo"}`,
PartialResponseStrategy: storepb.PartialResponseStrategy_WARN,
}
s := &exemplarsServer{
ctx: context.Background(),
}
_ = p.Exemplars(req, s)
}

0 comments on commit 217ca54

Please sign in to comment.