Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to ignore current goroutines #49

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions leaks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package goleak

import (
"fmt"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -83,6 +84,93 @@ func TestVerifyNone(t *testing.T) {
bg.unblock()
}

func TestIgnoreCurrent(t *testing.T) {
t.Run("Should ignore current", func(t *testing.T) {
defer VerifyNone(t)
denis-tingaikin marked this conversation as resolved.
Show resolved Hide resolved

done := make(chan struct{})
go func() {
<-done
}()

// We expect the above goroutine to be ignored.
VerifyNone(t, IgnoreCurrent())
denis-tingaikin marked this conversation as resolved.
Show resolved Hide resolved
close(done)
})

t.Run("Should detect new leaks", func(t *testing.T) {
defer VerifyNone(t)
denis-tingaikin marked this conversation as resolved.
Show resolved Hide resolved

// There are no leaks currently.
VerifyNone(t)

done1 := make(chan struct{})
done2 := make(chan struct{})

go func() {
<-done1
}()

err := Find()
require.Error(t, err, "Expected to find background goroutine as leak")

opt := IgnoreCurrent()
VerifyNone(t, opt)

// A second goroutine started after IgnoreCurrent is a leak
go func() {
<-done2
}()

err = Find(opt)
require.Error(t, err, "Expect second goroutine to be flagged as a leak")

close(done1)
close(done2)
})

t.Run("Should not ignore false positive", func(t *testing.T) {
defer VerifyNone(t)
denis-tingaikin marked this conversation as resolved.
Show resolved Hide resolved

const goroutinesCount = 5
var wg sync.WaitGroup
done := make(chan struct{})

// Spawn few goroutines before checking leaks
for i := 0; i < goroutinesCount; i++ {
wg.Add(1)
go func() {
<-done
wg.Done()
}()
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add some whitespace to this test to separate out the different parts, and add comments on what we are trying to test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add some whitespace to this test to separate out the different parts, and add comments on what we are trying to test?

Sure, done. Please take a look :)


// Store all goroutines
option := IgnoreCurrent()

// Free goroutines
close(done)
wg.Wait()

// We expect the below goroutines to be founded.
for i := 0; i < goroutinesCount; i++ {
ch := make(chan struct{})

go func() {
<-ch
}()

require.Error(t, Find(option), "Expect spawned goroutine to be flagged as a leak")

// Free spawned goroutine
close(ch)

// Make sure that there are no leaks
VerifyNone(t)
}
})
}

func TestVerifyParallel(t *testing.T) {
t.Run("parallel", func(t *testing.T) {
t.Parallel()
Expand Down
12 changes: 12 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ func IgnoreTopFunction(f string) Option {
})
}

// IgnoreCurrent records all current goroutines when the option is created, and ignores
// them in any future Find/Verify calls.
func IgnoreCurrent() Option {
excludeIDSet := map[int]bool{}
for _, s := range stack.All() {
excludeIDSet[s.ID()] = true
}
return addFilter(func(s stack.Stack) bool {
return excludeIDSet[s.ID()]
})
}

func maxSleep(d time.Duration) Option {
return optionFunc(func(opts *opts) {
opts.maxSleep = d
Expand Down