consul/vendor/go.uber.org/goleak
Matt Keeler 8853e38c72
Various go routine leak fixes
2020-06-25 09:36:14 -04:00
..
internal/stack Various go routine leak fixes 2020-06-25 09:36:14 -04:00
.gitignore Various go routine leak fixes 2020-06-25 09:36:14 -04:00
.travis.yml Various go routine leak fixes 2020-06-25 09:36:14 -04:00
CHANGELOG.md Various go routine leak fixes 2020-06-25 09:36:14 -04:00
LICENSE Various go routine leak fixes 2020-06-25 09:36:14 -04:00
Makefile Various go routine leak fixes 2020-06-25 09:36:14 -04:00
README.md Various go routine leak fixes 2020-06-25 09:36:14 -04:00
doc.go Various go routine leak fixes 2020-06-25 09:36:14 -04:00
glide.yaml Various go routine leak fixes 2020-06-25 09:36:14 -04:00
go.mod Various go routine leak fixes 2020-06-25 09:36:14 -04:00
go.sum Various go routine leak fixes 2020-06-25 09:36:14 -04:00
leaks.go Various go routine leak fixes 2020-06-25 09:36:14 -04:00
options.go Various go routine leak fixes 2020-06-25 09:36:14 -04:00
testmain.go Various go routine leak fixes 2020-06-25 09:36:14 -04:00
tools.go Various go routine leak fixes 2020-06-25 09:36:14 -04:00

README.md

goleak GoDoc Build Status Coverage Status

Goroutine leak detector to help avoid Goroutine leaks.

Development Status: Alpha

goleak is still in development, and APIs are still in flux.

Installation

You can use go get to get the latest version:

go get -u go.uber.org/goleak

goleak also supports semver releases. It is compatible with Go 1.5+.

Quick Start

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)
}

Determine Source of Package Leaks

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 "^Test"); do ./tests -test.run "^$test\$" &>/dev/null && echo -n "." || echo "\n$test failed"; done

This will only print names of failing tests which can be investigated individually. E.g.,

.....
TestLeakyTest failed
.......