Skip to content

Commit

Permalink
Bloombuild tests: protect fakeBuilder against race conditions
Browse files Browse the repository at this point in the history
Using atomic variables.

Signed-off-by: Bryan Boreham <[email protected]>
  • Loading branch information
bboreham committed Jun 7, 2024
1 parent 6f68ebe commit 327a81e
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions pkg/bloombuild/planner/planner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
"google.golang.org/grpc"

"github.com/grafana/loki/v3/pkg/bloombuild/protos"
Expand Down Expand Up @@ -809,15 +810,15 @@ func Test_processTenantTaskResults(t *testing.T) {
}

type fakeBuilder struct {
mx sync.Mutex
mx sync.Mutex // Protects tasks and currTaskIdx.
id string
tasks []*protos.Task
currTaskIdx int
grpc.ServerStream

returnError bool
returnErrorMsg bool
wait bool
returnError atomic.Bool
returnErrorMsg atomic.Bool
wait atomic.Bool
ctx context.Context
ctxCancel context.CancelFunc
}
Expand All @@ -840,15 +841,15 @@ func (f *fakeBuilder) ReceivedTasks() []*protos.Task {
}

func (f *fakeBuilder) SetReturnError(b bool) {
f.returnError = b
f.returnError.Store(b)
}

func (f *fakeBuilder) SetReturnErrorMsg(b bool) {
f.returnErrorMsg = b
f.returnErrorMsg.Store(b)
}

func (f *fakeBuilder) SetWait(b bool) {
f.wait = b
f.wait.Store(b)
}

func (f *fakeBuilder) CancelContext(b bool) {
Expand Down Expand Up @@ -891,12 +892,12 @@ func (f *fakeBuilder) Recv() (*protos.BuilderToPlanner, error) {
}, nil
}

if f.returnError {
if f.returnError.Load() {
return nil, fmt.Errorf("fake error from %s", f.id)
}

// Wait until `wait` is false
for f.wait {
for f.wait.Load() {
time.Sleep(time.Second)
}

Expand All @@ -906,10 +907,12 @@ func (f *fakeBuilder) Recv() (*protos.BuilderToPlanner, error) {
}

var errMsg string
if f.returnErrorMsg {
if f.returnErrorMsg.Load() {
errMsg = fmt.Sprintf("fake error from %s", f.id)
}

f.mx.Lock()
defer f.mx.Unlock()
return &protos.BuilderToPlanner{
BuilderID: f.id,
Result: protos.ProtoTaskResult{
Expand Down

0 comments on commit 327a81e

Please sign in to comment.