Skip to content

Commit

Permalink
Dump previous goroutines if leakcheck fails.
Browse files Browse the repository at this point in the history
  • Loading branch information
fancycode committed Dec 7, 2023
1 parent a818019 commit 0d15971
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion hub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 11 additions & 3 deletions testutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
package signaling

import (
"bytes"
"io"
"os"
"os/signal"
"runtime/pprof"
Expand Down Expand Up @@ -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)

Expand All @@ -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
}

0 comments on commit 0d15971

Please sign in to comment.