Dump previous goroutines if leakcheck fails.

This commit is contained in:
Joachim Bauch 2023-12-07 12:04:29 +01:00
parent a8180194ef
commit 0d15971506
No known key found for this signature in database
GPG key ID: 77C1D22D53E15F02
2 changed files with 12 additions and 4 deletions

View file

@ -288,7 +288,7 @@ func WaitForHub(ctx context.Context, t *testing.T, h *Hub) {
case <-ctx.Done():
h.mu.Lock()
h.ru.Lock()
dumpGoroutines()
dumpGoroutines("", os.Stderr)
t.Errorf("Error waiting for clients %+v / rooms %+v / sessions %+v / %d read / %d write to terminate: %s", h.clients, h.rooms, h.sessions, readActive, writeActive, ctx.Err())
h.ru.Unlock()
h.mu.Unlock()

View file

@ -22,6 +22,8 @@
package signaling
import (
"bytes"
"io"
"os"
"os/signal"
"runtime/pprof"
@ -53,6 +55,8 @@ func ensureNoGoroutinesLeak(t *testing.T, f func(t *testing.T)) {
// go routines
time.Sleep(500 * time.Millisecond)
before := profile.Count()
var prev bytes.Buffer
dumpGoroutines("Before:", &prev)
t.Run("leakcheck", f)
@ -68,12 +72,16 @@ func ensureNoGoroutinesLeak(t *testing.T, f func(t *testing.T)) {
}
if after != before {
dumpGoroutines()
io.Copy(os.Stderr, &prev) // nolint
dumpGoroutines("After:", os.Stderr)
t.Fatalf("Number of Go routines has changed from %d to %d", before, after)
}
}
func dumpGoroutines() {
func dumpGoroutines(prefix string, w io.Writer) {
if prefix != "" {
io.WriteString(w, prefix+"\n") // nolint
}
profile := pprof.Lookup("goroutine")
profile.WriteTo(os.Stderr, 2) // nolint
profile.WriteTo(w, 2) // nolint
}