Skip to content

Commit 3016975

Browse files
committed
tests: fix potential races with t.Parallel and loops ➿
Following the CommonMistake[1] in Go, for loop and go routines can be a big racey problem. This happens quite easily when using `t.Parallel` in for loop with tests. This fixes possible races by adding a "shadow" var that is evaluated at each iteration and placed on the stack for the goroutine, so each slice element is available to the goroutine when it is eventually executed. [1]: https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables Signed-off-by: Vincent Demeester <vdemeest@redhat.com>
1 parent 7b5b2fa commit 3016975

File tree

8 files changed

+12
-0
lines changed

8 files changed

+12
-0
lines changed

pkg/reconciler/pipelinerun/resources/apply_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ func TestApplyParameters(t *testing.T) {
247247
}},
248248
},
249249
}} {
250+
tt := tt // capture range variable
250251
t.Run(tt.name, func(t *testing.T) {
251252
t.Parallel()
252253
run := &v1beta1.PipelineRun{

test/cancel_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func TestTaskRunPipelineRunCancel(t *testing.T) {
4141
// on failure, to ensure that cancelling the PipelineRun doesn't cause
4242
// the retrying TaskRun to retry.
4343
for _, numRetries := range []int{0, 1} {
44+
numRetries := numRetries // capture range variable
4445
t.Run(fmt.Sprintf("retries=%d", numRetries), func(t *testing.T) {
4546
ctx := context.Background()
4647
ctx, cancel := context.WithCancel(ctx)

test/examples_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ func TestExamples(t *testing.T) {
218218

219219
t.Parallel()
220220
for _, path := range getExamplePaths(t, baseDir) {
221+
path := path // capture range variable
221222
testName := extractTestName(baseDir, path)
222223
waitValidateFunc := waitValidatePipelineRunDone
223224
kind := "pipelinerun"

test/git_checkout_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ func TestGitPipelineRun(t *testing.T) {
9696
repo: "https://github.com/spring-projects/spring-petclinic",
9797
revision: "main",
9898
}} {
99+
tc := tc // capture range variable
99100
t.Run(tc.name, func(t *testing.T) {
100101
t.Parallel()
101102
ctx := context.Background()
@@ -182,6 +183,7 @@ func TestGitPipelineRunFail(t *testing.T) {
182183
name: "invalid httpsproxy",
183184
httpsproxy: "invalid.https.proxy.example.com",
184185
}} {
186+
tc := tc // capture range variable
185187
t.Run(tc.name, func(t *testing.T) {
186188
t.Parallel()
187189
ctx := context.Background()

test/pipelinerun_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ func TestPipelineRun(t *testing.T) {
207207
}}
208208

209209
for i, td := range tds {
210+
i := i // capture range variable
211+
td := td // capture range variable
210212
t.Run(td.name, func(t *testing.T) {
211213
td := td
212214

test/v1alpha1/cancel_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func TestTaskRunPipelineRunCancel(t *testing.T) {
4141
// on failure, to ensure that cancelling the PipelineRun doesn't cause
4242
// the retrying TaskRun to retry.
4343
for _, numRetries := range []int{0, 1} {
44+
numRetries := numRetries // capture range variable
4445
t.Run(fmt.Sprintf("retries=%d", numRetries), func(t *testing.T) {
4546
ctx := context.Background()
4647
ctx, cancel := context.WithCancel(ctx)

test/v1alpha1/git_checkout_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ func TestGitPipelineRun(t *testing.T) {
9494
repo: "https://github.com/spring-projects/spring-petclinic",
9595
revision: "main",
9696
}} {
97+
tc := tc // capture range variable
9798
t.Run(tc.name, func(t *testing.T) {
9899
t.Parallel()
99100
ctx := context.Background()
@@ -180,6 +181,7 @@ func TestGitPipelineRunFail(t *testing.T) {
180181
name: "invalid httpsproxy",
181182
httpsproxy: "invalid.https.proxy.example.com",
182183
}} {
184+
tc := tc // capture range variable
183185
t.Run(tc.name, func(t *testing.T) {
184186
t.Parallel()
185187
ctx := context.Background()

test/v1alpha1/pipelinerun_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ func TestPipelineRun(t *testing.T) {
145145
}}
146146

147147
for i, td := range tds {
148+
i := i // capture range variable
149+
td := td // capture range variable
148150
t.Run(td.name, func(t *testing.T) {
149151
td := td
150152
t.Parallel()

0 commit comments

Comments
 (0)