diff --git a/hub_test.go b/hub_test.go index 0958d76..f01684b 100644 --- a/hub_test.go +++ b/hub_test.go @@ -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() diff --git a/testutils_test.go b/testutils_test.go index 296e328..5263ed7 100644 --- a/testutils_test.go +++ b/testutils_test.go @@ -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 }