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

fix(i): For loop foot guns in tests #1900

Closed

Conversation

nasdf
Copy link
Member

@nasdf nasdf commented Sep 20, 2023

Relevant issue(s)

Resolves #1879

Description

For loop range variable assignment can cause race conditions when used with go routines such as test.Run().

Read more about it here https://go.dev/blog/loopvar-preview

Tasks

  • I made sure the code is well commented, particularly hard-to-understand areas.
  • I made sure the repository-held documentation is changed accordingly.
  • I made sure the pull request title adheres to the conventional commit style (the subset used in the project can be found in tools/configs/chglog/config.yml).
  • I made sure to discuss its limitations such as threats to validity, vulnerability to mistake and misuse, robustness to invalidation of assumptions, resource requirements, ...

How has this been tested?

make test

Specify the platform(s) on which this was tested:

  • MacOS

@nasdf nasdf added bug Something isn't working area/testing Related to any test or testing suite labels Sep 20, 2023
@nasdf nasdf self-assigned this Sep 20, 2023
@nasdf nasdf requested a review from a team September 20, 2023 22:32
@codecov
Copy link

codecov bot commented Sep 20, 2023

Codecov Report

Patch and project coverage have no change.

Comparison is base (b8567c2) 70.26% compared to head (4409961) 70.26%.
Report is 2 commits behind head on develop.

Impacted file tree graph

@@           Coverage Diff            @@
##           develop    #1900   +/-   ##
========================================
  Coverage    70.26%   70.26%           
========================================
  Files          232      232           
  Lines        24192    24192           
========================================
  Hits         16998    16998           
  Misses        6029     6029           
  Partials      1165     1165           
Flag Coverage Δ
all-tests 70.26% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

see 4 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 84331e9...4409961. Read the comment docs.

Copy link
Collaborator

@fredcarle fredcarle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Note that this will no longer be an issue starting with Go1.21 as the variable created on range will be a new one instead of reusing the same one for every iteration.

@@ -161,7 +161,8 @@ func TestIsComplex(t *testing.T) {
}

mapping := getDocMapping()
for _, test := range tests {
for i := range tests {
test := tests[i]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: How sure on these are you? The original test variable is never really accessed concurrently - t.Run will block until completion of the func unless you explicitly mark it as Parralel.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I had forgotten that it blocked which mean that the variable wouldn't change value until the Run has completed anyways.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I misread the go docs for t.Run. The change detector does use t.Parallel, but I'll undo these changes.

Copy link
Contributor

@AndrewSisley AndrewSisley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced reading that this is the cause, could you please expand a little?

@@ -291,7 +291,8 @@ func TestNormalizeConditions(t *testing.T) {
}

mapping := getDocMapping()
for _, tt := range tests {
for i := range tests {
tt := tests[i]
Copy link
Contributor

@AndrewSisley AndrewSisley Sep 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Building upon my other comment, I did spend some time looking at this, and if you trim this test down to just the last usecase, and run it a bunch of times logging the output you can see that the output does vary. I am pretty convinced that there is an issue with the production code, and I think it is flaky due to the randomised map iteration order causing the test to only fail (correctly) every now and then.

@nasdf nasdf closed this Sep 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/testing Related to any test or testing suite bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TestNormalizeConditions is flaky
3 participants