Skip to content

Commit

Permalink
options: Add IgnoreAnyFunction
Browse files Browse the repository at this point in the history
Adds a new IgnoreAnyFunction option to ignore stacks
that have the provided function anywhere in the stack,
not just the top.

To test this better, the helper blockedG.run function
was split into two.

Supersedes #80
  • Loading branch information
abhinav committed Oct 22, 2023
1 parent 7bdc274 commit f82c00b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
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)
})

Check warning on line 99 in options.go

View check run for this annotation

Codecov / codecov/patch

options.go#L98-L99

Added lines #L98 - L99 were not covered by tests
}

// 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
19 changes: 18 additions & 1 deletion options_test.go
Expand Up @@ -61,10 +61,27 @@ 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())
}

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) {
// With default options.
opts := buildOpts()
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

0 comments on commit f82c00b

Please sign in to comment.