1## Leaktest [![Build Status](https://travis-ci.org/fortytw2/leaktest.svg?branch=master)](https://travis-ci.org/fortytw2/leaktest) [![codecov](https://codecov.io/gh/fortytw2/leaktest/branch/master/graph/badge.svg)](https://codecov.io/gh/fortytw2/leaktest) [![Sourcegraph](https://sourcegraph.com/github.com/fortytw2/leaktest/-/badge.svg)](https://sourcegraph.com/github.com/fortytw2/leaktest?badge) [![Documentation](https://godoc.org/github.com/fortytw2/gpt?status.svg)](http://godoc.org/github.com/fortytw2/leaktest) 2 3Refactored, tested variant of the goroutine leak detector found in both 4`net/http` tests and the `cockroachdb` source tree. 5 6Takes a snapshot of running goroutines at the start of a test, and at the end - 7compares the two and _voila_. Ignores runtime/sys goroutines. Doesn't play nice 8with `t.Parallel()` right now, but there are plans to do so. 9 10### Installation 11 12Go 1.7+ 13 14``` 15go get -u github.com/fortytw2/leaktest 16``` 17 18Go 1.5/1.6 need to use the tag `v1.0.0`, as newer versions depend on 19`context.Context`. 20 21### Example 22 23These tests fail, because they leak a goroutine 24 25```go 26// Default "Check" will poll for 5 seconds to check that all 27// goroutines are cleaned up 28func TestPool(t *testing.T) { 29 defer leaktest.Check(t)() 30 31 go func() { 32 for { 33 time.Sleep(time.Second) 34 } 35 }() 36} 37 38// Helper function to timeout after X duration 39func TestPoolTimeout(t *testing.T) { 40 defer leaktest.CheckTimeout(t, time.Second)() 41 42 go func() { 43 for { 44 time.Sleep(time.Second) 45 } 46 }() 47} 48 49// Use Go 1.7+ context.Context for cancellation 50func TestPoolContext(t *testing.T) { 51 ctx, _ := context.WithTimeout(context.Background(), time.Second) 52 defer leaktest.CheckContext(ctx, t)() 53 54 go func() { 55 for { 56 time.Sleep(time.Second) 57 } 58 }() 59} 60``` 61 62## LICENSE 63 64Same BSD-style as Go, see LICENSE 65