Test: Fix counting / comparing of goroutines.

This commit is contained in:
Joachim Bauch 2023-01-17 14:34:01 +01:00
parent 58bbe76b06
commit 5a12959821
No known key found for this signature in database
GPG key ID: 77C1D22D53E15F02

View file

@ -29,29 +29,27 @@ import (
)
func ensureNoGoroutinesLeak(t *testing.T, f func()) {
profile := pprof.Lookup("goroutine")
// Give time for things to settle before capturing the number of
// go routines
time.Sleep(500 * time.Millisecond)
before := pprof.Lookup("goroutine")
before := profile.Count()
f()
var after *pprof.Profile
var after int
// Give time for things to settle before capturing the number of
// go routines
timeout := time.Now().Add(time.Second)
for time.Now().Before(timeout) {
after = pprof.Lookup("goroutine")
if after.Count() == before.Count() {
after = profile.Count()
if after == before {
break
}
}
if after.Count() != before.Count() {
os.Stderr.WriteString("Before:\n")
before.WriteTo(os.Stderr, 1) // nolint
os.Stderr.WriteString("After:\n")
after.WriteTo(os.Stderr, 1) // nolint
t.Fatalf("Number of Go routines has changed in %s", t.Name())
if after != before {
profile.WriteTo(os.Stderr, 2) // nolint
t.Fatalf("Number of Go routines has changed in %s from %d to %d", t.Name(), before, after)
}
}