Skip to content

Commit

Permalink
make all GoroutinesChecker methods be on a pointer receiver (#35125)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndersonQ committed Apr 20, 2023
1 parent 9807be1 commit b062987
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
15 changes: 8 additions & 7 deletions libbeat/tests/resources/goroutines.go
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
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

0 comments on commit b062987

Please sign in to comment.