Skip to content

Commit f32166c

Browse files
MichaelRrenfurykerry
authored andcommitted
fix(test): resolve race condition while running tests
Signed-off-by: michaelrren <m.renhaoshi@gmail.com>
1 parent 2d992bf commit f32166c

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ lint: golangci-lint ## Run golangci-lint against code.
4949
test: generate fmt vet manifests envtest ## Run tests
5050
echo $(ENVTEST)
5151
go build -o pkg/daemon/criruntime/imageruntime/fake_plugin/fake-credential-plugin pkg/daemon/criruntime/imageruntime/fake_plugin/main.go && chmod +x pkg/daemon/criruntime/imageruntime/fake_plugin/fake-credential-plugin
52-
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./pkg/... -coverprofile cover.out
52+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test -race ./pkg/... -coverprofile cover.out
5353
rm pkg/daemon/criruntime/imageruntime/fake_plugin/fake-credential-plugin
5454

5555
coverage-report: ## Generate cover.html from cover.out

pkg/controller/statefulset/stateful_set_control_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"sort"
2828
"strconv"
2929
"strings"
30+
"sync"
3031
"testing"
3132
"time"
3233

@@ -2887,6 +2888,7 @@ func TestStatefulSetControlRollback(t *testing.T) {
28872888
}
28882889

28892890
type requestTracker struct {
2891+
l sync.Mutex
28902892
requests int
28912893
err error
28922894
after int
@@ -2897,10 +2899,14 @@ func (rt *requestTracker) errorReady() bool {
28972899
}
28982900

28992901
func (rt *requestTracker) inc() {
2902+
rt.l.Lock()
2903+
defer rt.l.Unlock()
29002904
rt.requests++
29012905
}
29022906

29032907
func (rt *requestTracker) reset() {
2908+
rt.l.Lock()
2909+
defer rt.l.Unlock()
29042910
rt.err = nil
29052911
rt.after = 0
29062912
}
@@ -2935,9 +2941,9 @@ func newFakeObjectManager(informerFactory informers.SharedInformerFactory, kruis
29352941
claimInformer.Informer().GetIndexer(),
29362942
setInformer.Informer().GetIndexer(),
29372943
revisionInformer.Informer().GetIndexer(),
2938-
requestTracker{0, nil, 0},
2939-
requestTracker{0, nil, 0},
2940-
requestTracker{0, nil, 0}}
2944+
requestTracker{sync.Mutex{}, 0, nil, 0},
2945+
requestTracker{sync.Mutex{}, 0, nil, 0},
2946+
requestTracker{sync.Mutex{}, 0, nil, 0}}
29412947
}
29422948

29432949
func (om *fakeObjectManager) CreatePod(ctx context.Context, pod *v1.Pod) error {
@@ -3131,7 +3137,7 @@ func newFakeStatefulSetStatusUpdater(setInformer kruiseappsinformers.StatefulSet
31313137
return &fakeStatefulSetStatusUpdater{
31323138
setInformer.Lister(),
31333139
setInformer.Informer().GetIndexer(),
3134-
requestTracker{0, nil, 0},
3140+
requestTracker{sync.Mutex{}, 0, nil, 0},
31353141
}
31363142
}
31373143

pkg/daemon/podprobe/pod_probe_controller_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,9 +600,13 @@ func TestSyncNodePodProbe(t *testing.T) {
600600
return
601601
}
602602
time.Sleep(time.Second)
603+
604+
c.workerLock.RLock()
603605
if len(c.workers) != len(cs.expectWorkers(c)) {
604606
t.Fatalf("expect(%d), but get(%d)", len(cs.expectWorkers(c)), len(c.workers))
605607
}
608+
c.workerLock.RUnlock()
609+
606610
for _, worker := range cs.expectWorkers(c) {
607611
obj, ok := c.workers[worker.key]
608612
if !ok {

0 commit comments

Comments
 (0)