Skip to content

Commit 214ab6d

Browse files
authored
🐛 Add race option to detect raced codes (#10899)
* fix: race code Signed-off-by: sivchari <shibuuuu5@gmail.com> * remove sync variable Signed-off-by: sivchari <shibuuuu5@gmail.com> * use sync chan Signed-off-by: sivchari <shibuuuu5@gmail.com> * drop chan Signed-off-by: sivchari <shibuuuu5@gmail.com> * add -race option again Signed-off-by: sivchari <shibuuuu5@gmail.com> * use atomic package Signed-off-by: sivchari <shibuuuu5@gmail.com> --------- Signed-off-by: sivchari <shibuuuu5@gmail.com>
1 parent 76328ed commit 214ab6d

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

Makefile

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -941,43 +941,43 @@ test-cover: ## Run unit and integration tests and generate a coverage report
941941

942942
.PHONY: test-docker-infrastructure
943943
test-docker-infrastructure: $(SETUP_ENVTEST) ## Run unit and integration tests for docker infrastructure provider
944-
cd $(CAPD_DIR); KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test ./... $(TEST_ARGS)
944+
cd $(CAPD_DIR); KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -race ./... $(TEST_ARGS)
945945

946946
.PHONY: test-docker-infrastructure-verbose
947947
test-docker-infrastructure-verbose: ## Run unit and integration tests for docker infrastructure provider with verbose flag
948948
$(MAKE) test-docker-infrastructure TEST_ARGS="$(TEST_ARGS) -v"
949949

950950
.PHONY: test-docker-infrastructure-junit
951951
test-docker-infrastructure-junit: $(SETUP_ENVTEST) $(GOTESTSUM) ## Run unit and integration tests and generate a junit report for docker infrastructure provider
952-
cd $(CAPD_DIR); set +o errexit; (KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -json ./... $(TEST_ARGS); echo $$? > $(ARTIFACTS)/junit.infra_docker.exitcode) | tee $(ARTIFACTS)/junit.infra_docker.stdout
952+
cd $(CAPD_DIR); set +o errexit; (KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -race -json ./... $(TEST_ARGS); echo $$? > $(ARTIFACTS)/junit.infra_docker.exitcode) | tee $(ARTIFACTS)/junit.infra_docker.stdout
953953
$(GOTESTSUM) --junitfile $(ARTIFACTS)/junit.infra_docker.xml --raw-command cat $(ARTIFACTS)/junit.infra_docker.stdout
954954
exit $$(cat $(ARTIFACTS)/junit.infra_docker.exitcode)
955955

956956
.PHONY: test-in-memory-infrastructure
957957
test-in-memory-infrastructure: $(SETUP_ENVTEST) ## Run unit and integration tests for in-memory infrastructure provider
958-
cd $(CAPIM_DIR); KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test ./... $(TEST_ARGS)
958+
cd $(CAPIM_DIR); KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -race ./... $(TEST_ARGS)
959959

960960
.PHONY: test-in-memory-infrastructure-verbose
961961
test-in-memory-infrastructure-verbose: ## Run unit and integration tests for in-memory infrastructure provider with verbose flag
962962
$(MAKE) test-in-memory-infrastructure TEST_ARGS="$(TEST_ARGS) -v"
963963

964964
.PHONY: test-in-memory-infrastructure-junit
965965
test-in-memory-infrastructure-junit: $(SETUP_ENVTEST) $(GOTESTSUM) ## Run unit and integration tests and generate a junit report for in-memory infrastructure provider
966-
cd $(CAPIM_DIR); set +o errexit; (KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -json ./... $(TEST_ARGS); echo $$? > $(ARTIFACTS)/junit.infra_inmemory.exitcode) | tee $(ARTIFACTS)/junit.infra_inmemory.stdout
966+
cd $(CAPIM_DIR); set +o errexit; (KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -race -json ./... $(TEST_ARGS); echo $$? > $(ARTIFACTS)/junit.infra_inmemory.exitcode) | tee $(ARTIFACTS)/junit.infra_inmemory.stdout
967967
$(GOTESTSUM) --junitfile $(ARTIFACTS)/junit.infra_inmemory.xml --raw-command cat $(ARTIFACTS)/junit.infra_inmemory.stdout
968968
exit $$(cat $(ARTIFACTS)/junit.infra_inmemory.exitcode)
969969

970970
.PHONY: test-test-extension
971971
test-test-extension: $(SETUP_ENVTEST) ## Run unit and integration tests for the test extension
972-
cd $(TEST_EXTENSION_DIR); KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test ./... $(TEST_ARGS)
972+
cd $(TEST_EXTENSION_DIR); KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -race ./... $(TEST_ARGS)
973973

974974
.PHONY: test-test-extension-verbose
975975
test-test-extension-verbose: ## Run unit and integration tests with verbose flag
976976
$(MAKE) test-test-extension TEST_ARGS="$(TEST_ARGS) -v"
977977

978978
.PHONY: test-test-extension-junit
979979
test-test-extension-junit: $(SETUP_ENVTEST) $(GOTESTSUM) ## Run unit and integration tests and generate a junit report for the test extension
980-
cd $(TEST_EXTENSION_DIR); set +o errexit; (KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -json ./... $(TEST_ARGS); echo $$? > $(ARTIFACTS)/junit.test_extension.exitcode) | tee $(ARTIFACTS)/junit.test_extension.stdout
980+
cd $(TEST_EXTENSION_DIR); set +o errexit; (KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -race -json ./... $(TEST_ARGS); echo $$? > $(ARTIFACTS)/junit.test_extension.exitcode) | tee $(ARTIFACTS)/junit.test_extension.stdout
981981
$(GOTESTSUM) --junitfile $(ARTIFACTS)/junit.test_extension.xml --raw-command cat $(ARTIFACTS)/junit.test_extension.stdout
982982
exit $$(cat $(ARTIFACTS)/junit.test_extension.exitcode)
983983

@@ -1197,7 +1197,7 @@ release-notes: release-notes-tool
11971197

11981198
.PHONY: test-release-notes-tool
11991199
test-release-notes-tool:
1200-
go test -C hack/tools -v -tags tools,integration sigs.k8s.io/cluster-api/hack/tools/release/notes
1200+
go test -race -C hack/tools -v -tags tools,integration sigs.k8s.io/cluster-api/hack/tools/release/notes
12011201

12021202
.PHONY: release-provider-issues-tool
12031203
release-provider-issues-tool: # Creates GitHub issues in a pre-defined list of CAPI provider repositories

test/infrastructure/inmemory/pkg/runtime/cache/sync.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ func (c *cache) startSyncer(ctx context.Context) error {
5050
c.syncQueue.ShutDown()
5151
}()
5252

53-
syncLoopStarted := false
53+
var syncLoopStarted atomic.Bool
5454
go func() {
5555
log.Info("Starting sync loop")
56-
syncLoopStarted = true
56+
syncLoopStarted.Store(true)
5757
for {
5858
select {
5959
case <-time.After(c.syncPeriod / 4):
@@ -63,15 +63,14 @@ func (c *cache) startSyncer(ctx context.Context) error {
6363
}
6464
}
6565
}()
66-
67-
var workers int64
66+
var workers atomic.Int64
6867
go func() {
6968
log.Info("Starting sync workers", "count", c.syncConcurrency)
7069
wg := &sync.WaitGroup{}
7170
wg.Add(c.syncConcurrency)
7271
for range c.syncConcurrency {
7372
go func() {
74-
atomic.AddInt64(&workers, 1)
73+
workers.Add(1)
7574
defer wg.Done()
7675
for c.processSyncWorkItem(ctx) {
7776
}
@@ -82,7 +81,7 @@ func (c *cache) startSyncer(ctx context.Context) error {
8281
}()
8382

8483
if err := wait.PollUntilContextTimeout(ctx, 50*time.Millisecond, 5*time.Second, false, func(context.Context) (done bool, err error) {
85-
if !syncLoopStarted {
84+
if !syncLoopStarted.Load() {
8685
return false, nil
8786
}
8887
return true, nil
@@ -91,7 +90,7 @@ func (c *cache) startSyncer(ctx context.Context) error {
9190
}
9291

9392
if err := wait.PollUntilContextTimeout(ctx, 50*time.Millisecond, 5*time.Second, false, func(context.Context) (done bool, err error) {
94-
if atomic.LoadInt64(&workers) < int64(c.syncConcurrency) {
93+
if workers.Load() < int64(c.syncConcurrency) {
9594
return false, nil
9695
}
9796
return true, nil

0 commit comments

Comments
 (0)