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

options: Add IgnoreAnyFunction #113

Merged
merged 2 commits into from Oct 23, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Built-in ignores now match function names more accurately.
They will no longer ignore stacks because of file names
that look similar to function names.
### Added
- Add an `IgnoreAnyFunction` option to ignore stack traces
that have the provided function anywhere in the stack.

## [1.2.1]
### Changed
Expand Down
16 changes: 16 additions & 0 deletions options.go
Expand Up @@ -83,6 +83,22 @@ func IgnoreTopFunction(f string) Option {
})
}

// IgnoreAnyFunction ignores goroutines where the specified function
// is present anywhere in the stack.
//
// The function name must be fully qualified, e.g.,
//
// go.uber.org/goleak.IgnoreAnyFunction
//
// For methods, the fully qualified form looks like:
//
// go.uber.org/goleak.(*MyType).MyMethod
func IgnoreAnyFunction(f string) Option {
return addFilter(func(s stack.Stack) bool {
return s.HasFunction(f)
})
}

// Cleanup sets up a cleanup function that will be executed at the
// end of the leak check.
// When passed to [VerifyTestMain], the exit code passed to cleanupFunc
Expand Down
25 changes: 24 additions & 1 deletion options_test.go
Expand Up @@ -61,8 +61,31 @@ func TestOptionsFilters(t *testing.T) {
require.Equal(t, 1, countUnfiltered(), "Expected blockedG goroutine to not match any filter")

// If we add an extra filter to ignore blockTill, it shouldn't match.
opts = buildOpts(IgnoreTopFunction("go.uber.org/goleak.(*blockedG).run"))
opts = buildOpts(IgnoreTopFunction("go.uber.org/goleak.(*blockedG).block"))
require.Zero(t, countUnfiltered(), "blockedG should be filtered out. running: %v", stack.All())

// If we ignore startBlockedG, that should not ignore the blockedG goroutine
// because startBlockedG should be the "created by" function in the stack.
opts = buildOpts(IgnoreAnyFunction("go.uber.org/goleak.startBlockedG"))
require.Equal(t, 1, countUnfiltered(),
"startBlockedG should not be filtered out. running: %v", stack.All())
}

abhinav marked this conversation as resolved.
Show resolved Hide resolved
func TestOptionsIgnoreAnyFunction(t *testing.T) {
cur := stack.Current()
opts := buildOpts(IgnoreAnyFunction("go.uber.org/goleak.(*blockedG).run"))

for _, s := range stack.All() {
if s.ID() == cur.ID() {
continue
}

if opts.filter(s) {
continue
}

t.Errorf("Unexpected goroutine: %v", s)
}
}

func TestBuildOptions(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions utils_test.go
Expand Up @@ -45,6 +45,10 @@ func startBlockedG() *blockedG {

func (bg *blockedG) run() {
close(bg.started)
bg.block()
}

func (bg *blockedG) block() {
<-bg.wait
}

Expand Down