Fix the data race in the indexcoord unittest (#19515)

Signed-off-by: SimFG <bang.fu@zilliz.com>

Signed-off-by: SimFG <bang.fu@zilliz.com>
This commit is contained in:
SimFG 2022-09-28 19:28:54 +08:00 committed by GitHub
parent 5550f883de
commit 7ca844ab99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 4 deletions

View File

@ -19,6 +19,7 @@ package indexcoord
import (
"context"
"math/rand"
"sync"
"time"
clientv3 "go.etcd.io/etcd/client/v3"
@ -348,6 +349,8 @@ func NewRootCoordMock() *RootCoordMock {
type DataCoordMock struct {
types.DataCoord
FuncLock sync.RWMutex
CallInit func() error
CallStart func() error
CallGetComponentStates func(ctx context.Context) (*internalpb.ComponentStates, error)
@ -370,7 +373,17 @@ func (dcm *DataCoordMock) GetComponentStates(ctx context.Context) (*internalpb.C
return dcm.CallGetComponentStates(ctx)
}
func (dcm *DataCoordMock) SetFunc(f func()) {
dcm.FuncLock.Lock()
defer dcm.FuncLock.Unlock()
f()
}
func (dcm *DataCoordMock) GetSegmentInfo(ctx context.Context, req *datapb.GetSegmentInfoRequest) (*datapb.GetSegmentInfoResponse, error) {
dcm.FuncLock.RLock()
defer dcm.FuncLock.RUnlock()
return dcm.CallGetSegmentInfo(ctx, req)
}
func (dcm *DataCoordMock) AcquireSegmentLock(ctx context.Context, req *datapb.AcquireSegmentLockRequest) (*commonpb.Status, error) {

View File

@ -211,13 +211,17 @@ func TestIndexCoord(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, resp.Status.ErrorCode, commonpb.ErrorCode_UnexpectedError)
dcm.CallGetFlushedSegment = originFunc1
dcm.CallGetSegmentInfo = func(ctx context.Context, req *datapb.GetSegmentInfoRequest) (*datapb.GetSegmentInfoResponse, error) {
return nil, errors.New("mock error")
}
dcm.SetFunc(func() {
dcm.CallGetSegmentInfo = func(ctx context.Context, req *datapb.GetSegmentInfoRequest) (*datapb.GetSegmentInfoResponse, error) {
return nil, errors.New("mock error")
}
})
resp, err = ic.DescribeIndex(ctx, req)
assert.NoError(t, err)
assert.Equal(t, resp.Status.ErrorCode, commonpb.ErrorCode_UnexpectedError)
dcm.CallGetSegmentInfo = originFunc2
dcm.SetFunc(func() {
dcm.CallGetSegmentInfo = originFunc2
})
})
t.Run("FlushedSegmentWatcher", func(t *testing.T) {