Add dup and remove same name reopened file

This commit is contained in:
pkositsyn 2021-03-17 00:07:14 +03:00 committed by Fedor Korotkiy
parent d90ffa8163
commit b4b319671c

View file

@ -6,6 +6,7 @@ import (
"io/ioutil" "io/ioutil"
"net" "net"
"os" "os"
"syscall"
"testing" "testing"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -80,20 +81,6 @@ func TestFileLeak_ReopenFile(t *testing.T) {
}) })
} }
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) { func TestFileLeak_PipeLeak(t *testing.T) {
checkLeak(t, true, func() { checkLeak(t, true, func() {
f, _, err := os.Pipe() f, _, err := os.Pipe()
@ -133,3 +120,23 @@ func TestFileLeak_SocketNoLeak(t *testing.T) {
_ = conn.Close() _ = conn.Close()
}) })
} }
func TestFileLeak_DupLeak(t *testing.T) {
var fd int
defer syscall.Close(fd)
checkLeak(t, true, func() {
var err error
fd, err = syscall.Dup(1)
require.NoError(t, err)
})
}
func TestFileLeak_DupNoLeak(t *testing.T) {
checkLeak(t, false, func() {
fd, err := syscall.Dup(1)
require.NoError(t, err)
_ = syscall.Close(fd)
})
}