Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated eventing code guidelines #901

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions concepts/eventing-code-guidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -825,22 +825,6 @@ func TestTwoDimensions(t *testing.T) {
}
```

<details>
<summary>Reason for variable reassignment (tc := tc)</summary>

The loop iteration variable in Go is a single variable. The closure for the second t.Run is executed inside a goroutine.
If there are multiple entries in the `cloudEvents` struct, `ce` references the last entry in `cloudEvents` in every iteration. The problem occurs because the closure referring to `tc` and `ce` is not executed in **sync** with the **for loop** (because of t.parallel).
To prevent this problem, `ce` and `tc` must be copied (`ce := ce`).
The linter [scopelint](https://github.com/golangci/golangci-lint/blob/master/pkg/golinters/scopelint.go) warns about the possible problem whenever `ce` or `tc` is used.

>**CAUTION:** If you add `// nolint:scopelint` to silence scopelint, you might not notice when `ce` or `tc` references the wrong entry in the test case list.

**See Also**:
- [Go Wiki - Common Mistakes](https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables)
- [Example when using t.Parallel and for loops in table-driven tests](https://gist.github.com/posener/92a55c4cd441fc5e5e85f27bca008721)

</details>

<details>
<summary>Reason for using nested t.Run</summary>

Expand Down