tailscale/tstest/resource.go
Maisem Ali c2241248c8 tstest: relax ResourceCheck to 3s
It was only waiting for 0.5s (5ms * 100), but our CI
is too slow so make it wait up to 3s (10ms * 300).

Updates tailscale/corp#14515

Signed-off-by: Maisem Ali <maisem@tailscale.com>
2023-09-06 15:42:08 -07:00

46 lines
892 B
Go

// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
package tstest
import (
"bytes"
"runtime"
"runtime/pprof"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
func ResourceCheck(tb testing.TB) {
tb.Helper()
startN, startStacks := goroutines()
tb.Cleanup(func() {
if tb.Failed() {
// Something else went wrong.
return
}
// Goroutines might be still exiting.
for i := 0; i < 300; i++ {
if runtime.NumGoroutine() <= startN {
return
}
time.Sleep(10 * time.Millisecond)
}
endN, endStacks := goroutines()
if endN <= startN {
return
}
tb.Logf("goroutine diff:\n%v\n", cmp.Diff(startStacks, endStacks))
tb.Fatalf("goroutine count: expected %d, got %d\n", startN, endN)
})
}
func goroutines() (int, []byte) {
p := pprof.Lookup("goroutine")
b := new(bytes.Buffer)
p.WriteTo(b, 1)
return p.Count(), b.Bytes()
}