Skip to content

Commit 153f674

Browse files
committed
fix(race): fix race
Signed-off-by: liubo02 <liubo02@pingcap.com>
1 parent b618aba commit 153f674

File tree

4 files changed

+23
-16
lines changed

4 files changed

+23
-16
lines changed

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ lint-fix: bin/golangci-lint
129129

130130
.PHONY: unit
131131
unit:
132-
cd $(VALIDATION_TEST_PATH) && go test ./...
133-
go test $$(go list -e ./... | grep -v cmd | grep -v tools | grep -v tests/e2e | grep -v third_party) \
132+
cd $(VALIDATION_TEST_PATH) && go test -race ./...
133+
go test -race $$(go list -e ./... | grep -v cmd | grep -v tools | grep -v tests/e2e | grep -v third_party) \
134134
-cover -coverprofile=coverage.txt -covermode=atomic
135135
sed -i.bak '/generated/d;/fake.go/d' coverage.txt && rm coverage.txt.bak
136136

pkg/timanager/manager_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ func TestClientManagerSource(t *testing.T) {
325325
})
326326
assert.True(tt, synced)
327327

328-
lister.L.Items = c.updated
328+
lister.UpdateItems(c.updated)
329329

330330
select {
331331
case <-ctx.Done():

pkg/timanager/poller_test.go

+19-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"cmp"
1919
"context"
2020
"slices"
21+
"sync"
2122
"testing"
2223
"time"
2324

@@ -33,17 +34,23 @@ import (
3334
)
3435

3536
type FakeLister[T any, PT Object[T]] struct {
36-
L List[T, PT]
37+
lock sync.Mutex
38+
L List[T, PT]
3739
}
3840

3941
func (l *FakeLister[T, PT]) List(_ context.Context) (*List[T, PT], error) {
42+
l.lock.Lock()
43+
defer l.lock.Unlock()
4044
return &l.L, nil
4145
}
4246

43-
func (l *FakeLister[T, PT]) GetItems(_ *List[T, PT]) []PT {
44-
objs := make([]PT, 0, len(l.L.Items))
45-
for i := range l.L.Items {
46-
objs = append(objs, &l.L.Items[i])
47+
func (l *FakeLister[T, PT]) GetItems(list *List[T, PT]) []PT {
48+
l.lock.Lock()
49+
defer l.lock.Unlock()
50+
51+
objs := make([]PT, 0, len(list.Items))
52+
for i := range list.Items {
53+
objs = append(objs, &list.Items[i])
4754
}
4855
return objs
4956
}
@@ -52,6 +59,13 @@ func (*FakeLister[T, PT]) MarkAsInvalid(PT) bool {
5259
return false
5360
}
5461

62+
func (l *FakeLister[T, PT]) UpdateItems(items []T) {
63+
l.lock.Lock()
64+
defer l.lock.Unlock()
65+
66+
l.L.Items = items
67+
}
68+
5569
func TestPoller(t *testing.T) {
5670
cases := []struct {
5771
desc string

pkg/timanager/util.go

+1-8
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,6 @@ type cached[Client, UnderlayClient any] struct {
131131
c Client
132132
f SharedInformerFactory[UnderlayClient]
133133

134-
cancel context.CancelFunc
135-
136134
cacheKeys []string
137135
}
138136

@@ -157,14 +155,9 @@ func (c *cached[Client, UnderlayClient]) Keys() []string {
157155
}
158156

159157
func (c *cached[Client, UnderlayClient]) Start(ctx context.Context) {
160-
nctx, cancel := context.WithCancel(ctx)
161-
c.cancel = cancel
162-
c.f.Start(nctx.Done())
158+
c.f.Start(ctx.Done())
163159
}
164160

165161
func (c *cached[Client, UnderlayClient]) Stop() {
166-
if c.cancel != nil {
167-
c.cancel()
168-
}
169162
c.f.Shutdown()
170163
}

0 commit comments

Comments
 (0)