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 3 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
52 changes: 52 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,57 @@ 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
}()
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) {
done := make(chan struct{})
defer VerifyNone(t)
denis-tingaikin marked this conversation as resolved.
Show resolved Hide resolved
defer close(done)
defer func(o Option) {
err := Find(o)
require.Error(t, err)
}(IgnoreCurrent())
go func() {
<-done
}()
})

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
wg := sync.WaitGroup{}
denis-tingaikin marked this conversation as resolved.
Show resolved Hide resolved
done := make(chan struct{})
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 :)

option := IgnoreCurrent()
close(done)
wg.Wait()
for i := 0; i < goroutinesCount; i++ {
ch := make(chan struct{})
go func() {
<-ch
}()
require.Error(t, Find(option))
close(ch)
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