Skip to content

Commit

Permalink
fix(test): resolve race condition while running tests
Browse files Browse the repository at this point in the history
Signed-off-by: michaelrren <[email protected]>
  • Loading branch information
MichaelRren authored and furykerry committed Sep 13, 2024
1 parent 2d992bf commit f32166c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ lint: golangci-lint ## Run golangci-lint against code.
test: generate fmt vet manifests envtest ## Run tests
echo $(ENVTEST)
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
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./pkg/... -coverprofile cover.out
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test -race ./pkg/... -coverprofile cover.out
rm pkg/daemon/criruntime/imageruntime/fake_plugin/fake-credential-plugin

coverage-report: ## Generate cover.html from cover.out
Expand Down
14 changes: 10 additions & 4 deletions pkg/controller/statefulset/stateful_set_control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"sort"
"strconv"
"strings"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -2887,6 +2888,7 @@ func TestStatefulSetControlRollback(t *testing.T) {
}

type requestTracker struct {
l sync.Mutex
requests int
err error
after int
Expand All @@ -2897,10 +2899,14 @@ func (rt *requestTracker) errorReady() bool {
}

func (rt *requestTracker) inc() {
rt.l.Lock()
defer rt.l.Unlock()
rt.requests++
}

func (rt *requestTracker) reset() {
rt.l.Lock()
defer rt.l.Unlock()
rt.err = nil
rt.after = 0
}
Expand Down Expand Up @@ -2935,9 +2941,9 @@ func newFakeObjectManager(informerFactory informers.SharedInformerFactory, kruis
claimInformer.Informer().GetIndexer(),
setInformer.Informer().GetIndexer(),
revisionInformer.Informer().GetIndexer(),
requestTracker{0, nil, 0},
requestTracker{0, nil, 0},
requestTracker{0, nil, 0}}
requestTracker{sync.Mutex{}, 0, nil, 0},
requestTracker{sync.Mutex{}, 0, nil, 0},
requestTracker{sync.Mutex{}, 0, nil, 0}}
}

func (om *fakeObjectManager) CreatePod(ctx context.Context, pod *v1.Pod) error {
Expand Down Expand Up @@ -3131,7 +3137,7 @@ func newFakeStatefulSetStatusUpdater(setInformer kruiseappsinformers.StatefulSet
return &fakeStatefulSetStatusUpdater{
setInformer.Lister(),
setInformer.Informer().GetIndexer(),
requestTracker{0, nil, 0},
requestTracker{sync.Mutex{}, 0, nil, 0},
}
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/daemon/podprobe/pod_probe_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,9 +600,13 @@ func TestSyncNodePodProbe(t *testing.T) {
return
}
time.Sleep(time.Second)

c.workerLock.RLock()
if len(c.workers) != len(cs.expectWorkers(c)) {
t.Fatalf("expect(%d), but get(%d)", len(cs.expectWorkers(c)), len(c.workers))
}
c.workerLock.RUnlock()

for _, worker := range cs.expectWorkers(c) {
obj, ok := c.workers[worker.key]
if !ok {
Expand Down

0 comments on commit f32166c

Please sign in to comment.