2020-02-05 22:16:58 +00:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2020-04-29 03:56:11 +01: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) {
|
|
|
|
startN, startStacks := goroutines()
|
|
|
|
tb.Cleanup(func() {
|
|
|
|
if tb.Failed() {
|
|
|
|
// Something else went wrong.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tb.Helper()
|
|
|
|
// 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
|
|
|
}
|
|
|
|
time.Sleep(1 * time.Millisecond)
|
|
|
|
}
|
2021-02-02 19:30:46 +00:00
|
|
|
endN, endStacks := goroutines()
|
|
|
|
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
|
|
|
}
|