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 2 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
10 changes: 10 additions & 0 deletions leaks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ func TestVerifyNone(t *testing.T) {
bg.unblock()
}

func TestIgnoreCurrentStacks(t *testing.T) {
done := make(chan struct{})
go func() {
<-done
}()
VerifyNone(t, IgnoreCurrentStacks())
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 a couple more test cases here:

  • Add a goroutine after IgnoreCurrentStacks and ensure that it is captured (you can use Find)
  • Ensure that if a goroutine is started before IgnoreCurrentStacks, it ends, and another goroutine is started, the other goroutine is noticed. (This should help ensure that goroutine IDs are not reused and so we won't have false negatives)

I'd suggest making them independent tests using t.Run rather than one long test as well, will be easier to understand what each test is testing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added tests for proposed use cases, please take a look :)

close(done)
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 {
})
}

// IgnoreCurrentStacks remembers all current stacks and ignores them on verifying.
// This Option may be used in big projects that recently started to use go-leak.
denis-tingaikin marked this conversation as resolved.
Show resolved Hide resolved
func IgnoreCurrentStacks() Option {
denis-tingaikin marked this conversation as resolved.
Show resolved Hide resolved
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