chore: add more tests (#3338)
This commit is contained in:
@@ -1,6 +0,0 @@
|
||||
//go:build windows
|
||||
|
||||
package proc
|
||||
|
||||
func dumpGoroutines() {
|
||||
}
|
||||
@@ -18,7 +18,11 @@ const (
|
||||
debugLevel = 2
|
||||
)
|
||||
|
||||
func dumpGoroutines() {
|
||||
type creator interface {
|
||||
Create(name string) (file *os.File, err error)
|
||||
}
|
||||
|
||||
func dumpGoroutines(ctor creator) {
|
||||
command := path.Base(os.Args[0])
|
||||
pid := syscall.Getpid()
|
||||
dumpFile := path.Join(os.TempDir(), fmt.Sprintf("%s-%d-goroutines-%s.dump",
|
||||
@@ -26,10 +30,16 @@ func dumpGoroutines() {
|
||||
|
||||
logx.Infof("Got dump goroutine signal, printing goroutine profile to %s", dumpFile)
|
||||
|
||||
if f, err := os.Create(dumpFile); err != nil {
|
||||
if f, err := ctor.Create(dumpFile); err != nil {
|
||||
logx.Errorf("Failed to dump goroutine profile, error: %v", err)
|
||||
} else {
|
||||
defer f.Close()
|
||||
pprof.Lookup(goroutineProfile).WriteTo(f, debugLevel)
|
||||
}
|
||||
}
|
||||
|
||||
type fileCreator struct{}
|
||||
|
||||
func (fc fileCreator) Create(name string) (file *os.File, err error) {
|
||||
return os.Create(name)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
//go:build linux || darwin
|
||||
|
||||
package proc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -9,7 +13,29 @@ import (
|
||||
)
|
||||
|
||||
func TestDumpGoroutines(t *testing.T) {
|
||||
buf := logtest.NewCollector(t)
|
||||
dumpGoroutines()
|
||||
assert.True(t, strings.Contains(buf.String(), ".dump"))
|
||||
t.Run("real file", func(t *testing.T) {
|
||||
buf := logtest.NewCollector(t)
|
||||
dumpGoroutines(fileCreator{})
|
||||
assert.True(t, strings.Contains(buf.String(), ".dump"))
|
||||
})
|
||||
|
||||
t.Run("fake file", func(t *testing.T) {
|
||||
const msg = "any message"
|
||||
buf := logtest.NewCollector(t)
|
||||
err := errors.New(msg)
|
||||
dumpGoroutines(fakeCreator{
|
||||
file: &os.File{},
|
||||
err: err,
|
||||
})
|
||||
assert.True(t, strings.Contains(buf.String(), msg))
|
||||
})
|
||||
}
|
||||
|
||||
type fakeCreator struct {
|
||||
file *os.File
|
||||
err error
|
||||
}
|
||||
|
||||
func (fc fakeCreator) Create(name string) (file *os.File, err error) {
|
||||
return fc.file, fc.err
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ func init() {
|
||||
v := <-signals
|
||||
switch v {
|
||||
case syscall.SIGUSR1:
|
||||
dumpGoroutines()
|
||||
dumpGoroutines(fileCreator{})
|
||||
case syscall.SIGUSR2:
|
||||
if profiler == nil {
|
||||
profiler = StartProfile()
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build linux || darwin
|
||||
|
||||
package proc
|
||||
|
||||
import (
|
||||
|
||||
Reference in New Issue
Block a user