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

Make all GoroutinesChecker methods be on a pointer receiver #35125

Merged
merged 5 commits into from
Apr 20, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions libbeat/tests/resources/goroutines.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package resources

import (
"errors"
"fmt"
"os"
"runtime"
Expand All @@ -41,16 +42,16 @@ type GoroutinesChecker struct {
}

// NewGoroutinesChecker creates a new GoroutinesChecker
func NewGoroutinesChecker() GoroutinesChecker {
return GoroutinesChecker{
func NewGoroutinesChecker() *GoroutinesChecker {
return &GoroutinesChecker{
before: runtime.NumGoroutine(),
FinalizationTimeout: defaultFinalizationTimeout,
}
}

// Check if the number of goroutines has increased since the checker
// was created
func (c GoroutinesChecker) Check(t testing.TB) {
func (c *GoroutinesChecker) Check(t testing.TB) {
t.Helper()
err := c.check()
if err != nil {
Expand All @@ -61,12 +62,12 @@ func (c GoroutinesChecker) Check(t testing.TB) {

func dumpGoroutines() {
profile := pprof.Lookup("goroutine")
profile.WriteTo(os.Stdout, 2)
_ = profile.WriteTo(os.Stdout, 2)
}

func (c GoroutinesChecker) check() error {
func (c *GoroutinesChecker) check() error {
after, err := c.WaitUntilOriginalCount()
if err == ErrTimeout {
if errors.Is(err, ErrTimeout) {
return fmt.Errorf("possible goroutines leak, before: %d, after: %d", c.before, after)
}
return err
Expand All @@ -88,7 +89,7 @@ var ErrTimeout = fmt.Errorf("timeout waiting for finalization of goroutines")
// present before we created the resource checker.
// It returns the number of goroutines after the check and a timeout error
// in case the wait has expired.
func (c GoroutinesChecker) WaitUntilOriginalCount() (int, error) {
func (c *GoroutinesChecker) WaitUntilOriginalCount() (int, error) {
timeout := time.Now().Add(c.FinalizationTimeout)

var after int
Expand Down
2 changes: 1 addition & 1 deletion libbeat/tests/resources/goroutines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestGoroutinesChecker(t *testing.T) {

// goroutineTesterControl helps keeping track of goroutines started for each test case.
type goroutineTesterControl struct {
checker GoroutinesChecker
checker *GoroutinesChecker
blocker chan struct{}
}

Expand Down