Leaktest

Goroutine Leak Detector

Github stars Tracking Chart

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

Main metrics

Overview
Name With Ownerfortytw2/leaktest
Primary LanguageGo
Program languageGo (Language Count: 1)
Platform
License:BSD 3-Clause "New" or "Revised" License
所有者活动
Created At2016-04-26 15:19:27
Pushed At2020-10-15 17:02:31
Last Commit At2019-06-06 09:38:08
Release Count4
Last Release Namev1.3.0 (Posted on )
First Release Namev1.0.0 (Posted on )
用户参与
Stargazers Count1k
Watchers Count20
Fork Count49
Commits Count49
Has Issues Enabled
Issues Count18
Issue Open Count3
Pull Requests Count15
Pull Requests Open Count2
Pull Requests Close Count3
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private