Leaktest

Goroutine Leak Detector

Github星跟踪图

Leaktest Build Status codecov Sourcegraph Documentation

Refactored, tested variant of the goroutine leak detector found in both
net/http tests and the cockroachdb source tree.

Takes a snapshot of running goroutines at the start of a test, and at the end -
compares the two and voila. Ignores runtime/sys goroutines. Doesn't play nice
with t.Parallel() right now, but there are plans to do so.

Installation

Go 1.7+

go get -u github.com/fortytw2/leaktest

Go 1.5/1.6 need to use the tag v1.0.0, as newer versions depend on
context.Context.

Example

These tests fail, because they leak a goroutine

// Default "Check" will poll for 5 seconds to check that all
// goroutines are cleaned up
func TestPool(t *testing.T) {
    defer leaktest.Check(t)()

    go func() {
        for {
            time.Sleep(time.Second)
        }
    }()
}

// Helper function to timeout after X duration
func TestPoolTimeout(t *testing.T) {
    defer leaktest.CheckTimeout(t, time.Second)()

    go func() {
        for {
            time.Sleep(time.Second)
        }
    }()
}

// Use Go 1.7+ context.Context for cancellation
func TestPoolContext(t *testing.T) {
    ctx, cancel := context.WithTimeout(context.Background(), time.Second)
    defer cancel()
    defer leaktest.CheckContext(ctx, t)()

    go func() {
        for {
            time.Sleep(time.Second)
        }
    }()
}

LICENSE

Same BSD-style as Go, see LICENSE

主要指标

概览
名称与所有者fortytw2/leaktest
主编程语言Go
编程语言Go (语言数: 1)
平台
许可证BSD 3-Clause "New" or "Revised" License
所有者活动
创建于2016-04-26 15:19:27
推送于2020-10-15 17:02:31
最后一次提交2019-06-06 09:38:08
发布数4
最新版本名称v1.3.0 (发布于 )
第一版名称v1.0.0 (发布于 )
用户参与
星数1k
关注者数20
派生数49
提交数49
已启用问题?
问题数18
打开的问题数3
拉请求数15
打开的拉请求数2
关闭的拉请求数3
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?