Skip to content

Commit

Permalink
CheckRoutinesStrict checks test startup
Browse files Browse the repository at this point in the history
  • Loading branch information
edaniels committed Jul 25, 2024
1 parent 8754ef1 commit 16b1e45
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions test/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,28 @@ func TimeOut(t time.Duration) *time.Timer {
})
}

// CheckRoutines is used to check for leaked go-routines
func CheckRoutines(t *testing.T) func() {
tryLoop := func(failMessage string) {
try := 0
ticker := time.NewTicker(200 * time.Millisecond)
defer ticker.Stop()
for range ticker.C {
runtime.GC()
routines := getRoutines()
if len(routines) == 0 {
return
}
if try >= 50 {
t.Fatalf("%s: \n%s", failMessage, strings.Join(routines, "\n\n")) // nolint
}
try++
func tryCheckRoutinesLoop(tb testing.TB, failMessage string) {
try := 0
ticker := time.NewTicker(200 * time.Millisecond)
defer ticker.Stop()
for range ticker.C {
runtime.GC()
routines := getRoutines()
if len(routines) == 0 {
return
}
if try >= 50 {
tb.Fatalf("%s: \n%s", failMessage, strings.Join(routines, "\n\n")) // nolint
}
try++
}
}

tryLoop("Unexpected routines on test startup")
// CheckRoutines is used to check for leaked go-routines
func CheckRoutines(t *testing.T) func() {
tryCheckRoutinesLoop(t, "Unexpected routines on test startup")
return func() {
tryLoop("Unexpected routines on test end")
tryCheckRoutinesLoop(t, "Unexpected routines on test end")
}
}

Expand All @@ -58,6 +58,7 @@ func CheckRoutines(t *testing.T) func() {
// for lingering goroutines. This is helpful for tests that need
// to ensure clean closure of resources.
func CheckRoutinesStrict(tb testing.TB) func() {
tryCheckRoutinesLoop(tb, "Unexpected routines on test startup")
return func() {
routines := getRoutines()
if len(routines) == 0 {
Expand Down

0 comments on commit 16b1e45

Please sign in to comment.