Add tests for file leak

This commit is contained in:
pkositsyn 2021-03-16 23:50:48 +03:00 committed by Fedor Korotkiy
parent 2fe452be06
commit d90ffa8163

View file

@ -4,12 +4,14 @@ import (
"bytes"
"fmt"
"io/ioutil"
"net"
"os"
"testing"
"github.com/stretchr/testify/require"
"gitlab.com/slon/shad-go/fileleak"
"gitlab.com/slon/shad-go/tools/testtool"
)
type fakeT struct {
@ -77,3 +79,57 @@ func TestFileLeak_ReopenFile(t *testing.T) {
f = ff
})
}
func TestFileLeak_ReopenSameFile(t *testing.T) {
f, err := os.Open("/proc/self/exe")
require.NoError(t, err)
defer f.Close()
checkLeak(t, true, func() {
_ = f.Close()
ff, err := os.Open("/proc/self/exe")
require.NoError(t, err)
f = ff
})
}
func TestFileLeak_PipeLeak(t *testing.T) {
checkLeak(t, true, func() {
f, _, err := os.Pipe()
require.NoError(t, err)
_ = f.Close()
})
}
func TestFileLeak_PipeNoLeak(t *testing.T) {
checkLeak(t, false, func() {
f, w, err := os.Pipe()
require.NoError(t, err)
_, _ = f.Close(), w.Close()
})
}
func TestFileLeak_SocketLeak(t *testing.T) {
var conn net.Listener
defer conn.Close()
checkLeak(t, true, func() {
addr, err := testtool.GetFreePort()
require.NoError(t, err)
conn, err = net.Listen("tcp", addr)
require.NoError(t, err)
})
}
func TestFileLeak_SocketNoLeak(t *testing.T) {
checkLeak(t, false, func() {
addr, err := testtool.GetFreePort()
require.NoError(t, err)
conn, err := net.Listen("tcp", addr)
require.NoError(t, err)
_ = conn.Close()
})
}