Skip to content

Commit 5081f42

Browse files
authored
Merge pull request nutanix-cloud-native#15 from dlipovetsky/test-data-race
test: Fix data race
2 parents 47e578a + 5dba101 commit 5081f42

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

make/go.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ define go_test
2626
-covermode=atomic \
2727
-coverprofile=coverage.out \
2828
-short \
29+
-race \
2930
-v \
3031
$(if $(GOTEST_RUN),-run "$(GOTEST_RUN)") \
3132
./... && \

pkg/handlers/aws/mutation/suite_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package mutation
55

66
import (
7+
"context"
78
"fmt"
89
"testing"
910

@@ -18,12 +19,13 @@ var (
1819
)
1920

2021
func TestMain(m *testing.M) {
21-
setup()
22-
defer teardown()
22+
setupCtx, cancel := context.WithCancel(ctx)
23+
setup(setupCtx)
24+
defer teardown(cancel)
2325
m.Run()
2426
}
2527

26-
func setup() {
28+
func setup(ctx context.Context) {
2729
testEnvConfig := helpers.NewTestEnvironmentConfiguration()
2830
var err error
2931
testEnv, err = testEnvConfig.Build()
@@ -38,7 +40,8 @@ func setup() {
3840
}()
3941
}
4042

41-
func teardown() {
43+
func teardown(cancel context.CancelFunc) {
44+
cancel()
4245
if err := testEnv.Stop(); err != nil {
4346
panic(fmt.Sprintf("Failed to stop envtest: %v", err))
4447
}

pkg/handlers/docker/mutation/suite_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package mutation
55

66
import (
7+
"context"
78
"fmt"
89
"testing"
910

@@ -18,12 +19,13 @@ var (
1819
)
1920

2021
func TestMain(m *testing.M) {
21-
setup()
22-
defer teardown()
22+
setupCtx, cancel := context.WithCancel(ctx)
23+
setup(setupCtx)
24+
defer teardown(cancel)
2325
m.Run()
2426
}
2527

26-
func setup() {
28+
func setup(ctx context.Context) {
2729
testEnvConfig := helpers.NewTestEnvironmentConfiguration()
2830
var err error
2931
testEnv, err = testEnvConfig.Build()
@@ -38,7 +40,8 @@ func setup() {
3840
}()
3941
}
4042

41-
func teardown() {
43+
func teardown(cancel context.CancelFunc) {
44+
cancel()
4245
if err := testEnv.Stop(); err != nil {
4346
panic(fmt.Sprintf("Failed to stop envtest: %v", err))
4447
}

test/helpers/envtest.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,11 @@ func (t *TestEnvironmentConfiguration) Build() (*TestEnvironment, error) {
132132

133133
// StartManager starts the test controller against the local API server.
134134
func (t *TestEnvironment) StartManager(ctx context.Context) error {
135-
ctx, cancel := context.WithCancel(ctx)
136-
t.cancel = cancel
137135
return t.Manager.Start(ctx)
138136
}
139137

140138
// Stop stops the test environment.
141139
func (t *TestEnvironment) Stop() error {
142-
t.cancel()
143140
return t.env.Stop()
144141
}
145142

0 commit comments

Comments
 (0)