chore: add more tests (#3338)

This commit is contained in:
Kevin Wan
2023-06-12 01:22:20 +08:00
committed by GitHub
parent efa6940001
commit f6bdb6e1de
11 changed files with 99 additions and 46 deletions

View File

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