noleak

package module
v0.0.0-...-345842f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 11, 2023 License: BSD-3-Clause Imports: 7 Imported by: 0

README

Simple goroutine leak detector

It compares the list of active goroutines before and after the test and reports an error on mismatch.

To enable detector add a main_test.go file to the package.

To check a specific test only add noleak.Check(t) at the top.

Tests using http.Client or http.DefaultClient should close idle connections.

See and run all examples:

GODEBUG=tracebackancestors=10 go test ./examples/...

Setting GODEBUG=tracebackancestors=N extends tracebacks with the stacks at which goroutines were created, where N limits the number of ancestor goroutines to report, see https://pkg.go.dev/runtime#hdr-Environment_Variables.

Credits

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Check

func Check(t *testing.T)

Check reports test error if there are active goroutines after test ends.

Example:

func TestLeak(t *testing.T) {
	noleak.Check(t)

	...
}

func CheckMain

func CheckMain(m *testing.M) (code int)

CheckMain prints active goroutines after all tests end. It returns result of m.Run() or non-zero if there are active goroutines.

Example:

func TestMain(m *testing.M) {
	os.Exit(noleak.CheckMain(m))
}

func CheckMainFunc

func CheckMainFunc(m func() int) (code int)

CheckMainFunc prints active goroutines after m ends. It returns result of m or non-zero if there are active goroutines.

Example:

func TestMain(m *testing.M) {
	os.Exit(noleak.CheckMainFunc(func() int {
		code := m.Run()

		// perform cleanup
		...

		return code
	}))
}

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL