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

testutil: NewLogBuffer - buffer logs until a test fails #7948

Merged
merged 1 commit into from
Jul 21, 2020

Conversation

dnephin
Copy link
Contributor

@dnephin dnephin commented May 21, 2020

Replaces #7559
Fixes this data race (and likely many others): #8329 (comment)

Running tests in parallel, with background goroutines, results in test output not being associated with the correct test. go test does not make any guarantees about output from goroutines being attributed to the correct test case.

Attaching log output from background goroutines also cause data races. If the goroutine outlives the test, it will race with the test being marked done. Previously this was noticed as a panic when logging, but with the race detector enabled it is shown as a data race.

This commit attempts a new approach. Instead of printing all the logs, only print when a test fails. All of the logs are printed from the test goroutine, so they should be associated with the correct test.

Another nice property of this approach is that when multiple agents are run in a test case, all of the logs for a single agent are grouped together. Previously they would have been interspersed.

Also removes some test helpers that were not used, or only had a single caller. Packages which expose many functions with similar names can be difficult to use correctly.

Tested some of the data races locally with -count=100 to ensure they no long race with this change.

Related:
golang/go#38458 (a regression in go1.14 that was just fixed in go1.14.6)
golang/go#38382 (comment)

@dnephin dnephin mentioned this pull request May 21, 2020
@dnephin
Copy link
Contributor Author

dnephin commented May 22, 2020

Another nice property of this approach seems to be that when multiple agents are run in a test case, all of the logs for a single agent are grouped together. I suspect previously they would have been interspersed.

It also means that all assertion failures will appear first before any logs, which should make them easier to find in the output.

Replaces #7559

Running tests in parallel, with background goroutines, results in test output not being associated with the correct test. `go test` does not make any guarantees about output from goroutines being attributed to the correct test case.

Attaching log output from background goroutines also cause data races.  If the goroutine outlives the test, it will race with the test being marked done. Previously this was noticed as a panic when logging, but with the race detector enabled it is shown as a data race.

The previous solution did not address the problem of correct test attribution because test output could still be hidden when it was associated with a test that did not fail. You would have to look at all of the log output to find the relevant lines. It also made debugging test failures more difficult because each log line was very long.

This commit attempts a new approach. Instead of printing all the logs, only print when a test fails. This should work well when there are a small number of failures, but may not work well when there are many test failures at the same time. In those cases the failures are unlikely a result of a specific test, and the log output is likely less useful.

All of the logs are printed from the test goroutine, so they should be associated with the correct test.

Also removes some test helpers that were not used, or only had a single caller. Packages which expose many functions with similar names can be difficult to use correctly.

Related:
golang/go#38458 (may be fixed in go1.15)
golang/go#38382 (comment)
@dnephin dnephin marked this pull request as ready for review July 21, 2020 17:02
@dnephin dnephin requested a review from a team July 21, 2020 17:02
Copy link
Member

@mkeeler mkeeler left a comment

Choose a reason for hiding this comment

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

LGTM

@dnephin dnephin merged commit 3570ce6 into master Jul 21, 2020
@dnephin dnephin deleted the dnephin/buffer-test-logs branch July 21, 2020 19:21
@hashicorp-ci
Copy link
Contributor

🍒✅ Cherry pick of commit 3570ce6 onto release/1.8.x succeeded!

hashicorp-ci pushed a commit that referenced this pull request Jul 21, 2020
testutil: NewLogBuffer - buffer logs until a test fails
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants