Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
go.uber.org/goleak
Goroutine leak detector to help avoid Goroutine leaks.
You can use go get
to get the latest version:
go get -u go.uber.org/goleak
goleak
also supports semver releases.
Note that go-leak only supports the two most recent minor versions of Go.
To verify that there are no unexpected goroutines running at the end of a test:
func TestA(t *testing.T) {
defer goleak.VerifyNone(t)
// test logic here.
}
Instead of checking for leaks at the end of every test, goleak
can also be run
at the end of every test package by creating a TestMain
function for your
package:
func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}
When verifying leaks using TestMain
, the leak test is only run once after all tests
have been run. This is typically enough to ensure there's no goroutines leaked from
tests, but when there are leaks, it's hard to determine which test is causing them.
You can use the following bash script to determine the source of the failing test:
# Create a test binary which will be used to run each test individually
$ go test -c -o tests
# Run each test individually, printing "." for successful tests, or the test name
# for failing tests.
$ for test in $(go test -list . | grep -E "^(Test|Example)"); do ./tests -test.run "^$test\$" &>/dev/null && echo -n "." || echo -e "\n$test failed"; done
This will only print names of failing tests which can be investigated individually. E.g.,
.....
TestLeakyTest failed
.......
goleak is v1 and follows SemVer strictly.
No breaking changes will be made to exported APIs before 2.0.
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.