2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-02-05 22:16:58 +00:00
|
|
|
|
2020-04-29 02:56:11 +00:00
|
|
|
package tstest
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"runtime"
|
|
|
|
"runtime/pprof"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
)
|
|
|
|
|
2021-02-02 19:30:46 +00:00
|
|
|
func ResourceCheck(tb testing.TB) {
|
2021-06-23 04:53:43 +00:00
|
|
|
tb.Helper()
|
2021-02-02 19:30:46 +00:00
|
|
|
startN, startStacks := goroutines()
|
|
|
|
tb.Cleanup(func() {
|
|
|
|
if tb.Failed() {
|
|
|
|
// Something else went wrong.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Goroutines might be still exiting.
|
2020-02-05 22:16:58 +00:00
|
|
|
for i := 0; i < 100; i++ {
|
2021-02-02 19:30:46 +00:00
|
|
|
if runtime.NumGoroutine() <= startN {
|
|
|
|
return
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2021-06-23 04:53:43 +00:00
|
|
|
time.Sleep(5 * time.Millisecond)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2021-02-02 19:30:46 +00:00
|
|
|
endN, endStacks := goroutines()
|
2022-07-29 16:48:35 +00:00
|
|
|
if endN <= startN {
|
|
|
|
return
|
|
|
|
}
|
2021-02-02 19:30:46 +00:00
|
|
|
tb.Logf("goroutine diff:\n%v\n", cmp.Diff(startStacks, endStacks))
|
|
|
|
tb.Fatalf("goroutine count: expected %d, got %d\n", startN, endN)
|
|
|
|
})
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
|
2021-02-02 19:30:46 +00:00
|
|
|
func goroutines() (int, []byte) {
|
|
|
|
p := pprof.Lookup("goroutine")
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
p.WriteTo(b, 1)
|
|
|
|
return p.Count(), b.Bytes()
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|