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

Improve the use of goleak in unit tests #72

Merged
merged 1 commit into from
Feb 1, 2025

Conversation

vbanos
Copy link
Contributor

@vbanos vbanos commented Feb 1, 2025

Instead of using goleak in every unit test, run it in TestMain and it applies to all unit tests.

The project didn't have a TestMain so I created a main_test.go for that reason. TestMain could be added to any test file and it would work. I thought that it would be clearer to make a separate main_test.go file.

This method is also referenced in https://github.com/uber-go/goleak?tab=readme-ov-file#quick-start

Instead of using goleak in every unit test, run it in `TestMain` and it
applies to all unit tests.

The project didn't have a `TestMain` so I created a `main_test.go` for
that reason. `TestMain` could be added to any test file and it would
work. I thought that it would be clearer to make a separate
`main_test.go` file.

This method is also referenced in https://github.com/uber-go/goleak?tab=readme-ov-file#quick-start
@vbanos
Copy link
Contributor Author

vbanos commented Feb 1, 2025

To verify this, I add the following leak in various places and it was caught by goleak in TestMain

go func() {
    time.Sleep(10 * time.Second) // Simulated leak: Goroutine never exits
}()

@CorentinB CorentinB merged commit 68cd367 into CorentinB:master Feb 1, 2025
1 check passed
@CorentinB
Copy link
Owner

LGTM! Thanks @vbanos

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.

2 participants