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

Goleak detects leaked goroutines from other tests #48

Closed
denis-tingaikin opened this issue Jul 1, 2020 · 3 comments
Closed

Goleak detects leaked goroutines from other tests #48

denis-tingaikin opened this issue Jul 1, 2020 · 3 comments

Comments

@denis-tingaikin
Copy link
Contributor

Description

We've found an interesting thing during using goleak on our CI, that goleak can detect leaked goroutine from other tests.

Steps to reproduce

  1. Use go1.14.1 darwin/amd64
  2. Use goleak v1.0.0
  3. Run test file below
    goleak_test.go
package goleak_test

import (
	"go.uber.org/goleak"
	"testing"
	"time"
)

func TestA(t *testing.T) {
	go func() {
		for {
			<-time.After(time.Millisecond * 100)
		}
	}()
}

func TestB(t *testing.T) {
	defer goleak.VerifyNone(t)
}

Expected: TestB should pass
Actual: TestB fails with error "found unexpected goroutines:"

@prashantv
Copy link
Collaborator

Yes, this is expected, as goleak cannot determine which test specifically is responsible for a leak, it just identifies that there is a leak when VerifyNone is run. If you had the same defer goleak.VerifyNone(t) in TestAt, it would detect that test as leaking too.

You could run the leak detection at the beginning and end of a test to ensure there were none before the test is started,

func TestB(t *testing.T) {
	goleak.VerifyNone(t) // ensure there's no leaks before the test starts
	defer goleak.VerifyNone(t)
}

You could write your own helper for this as well.

@denis-tingaikin
Copy link
Contributor Author

denis-tingaikin commented Jul 1, 2020

@prashantv thanks for the clarifications, I also got a question, can we add some option for goleak to ignore set of stacks. For example

func IngoreStacks(stacks ...stack.Stack) Option {
	excludeIDSet := map[int]bool{}
	for _, s := range stacks {
		excludeIDSet[s.ID()] = true
	}
	return addFilter(func(s stack.Stack) bool {
		return excludeIDSet[s.ID()],
	})
}

And with this option I could use the next options for the example above:

defer goleak.VerifyNone(t, goleak.IngoreStacks(stack.All()...))

What do you think about this approach? :)

prashantv pushed a commit that referenced this issue Jul 17, 2020
This allows usage specific tests in big projects that recently started to use go-leak check.

Signed-off-by: denis-tingajkin <denis.tingajkin@xored.com>


## Motivation

#48
@prashantv
Copy link
Collaborator

Fixed in #49

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants