diff options
| author | Joel Sing <jsing@google.com> | 2012-01-31 22:09:06 +1100 |
|---|---|---|
| committer | Joel Sing <jsing@google.com> | 2012-01-31 22:09:06 +1100 |
| commit | 1677f1a1632ac2204ad6ed3e892b5beed8e1b654 (patch) | |
| tree | 948c23d5c171b653ab0fc1814f77e94b60819f64 /src/pkg/os/exec/exec_test.go | |
| parent | 2d7495d287005b87047f353ac3574146ef50ac29 (diff) | |
| download | go-1677f1a1632ac2204ad6ed3e892b5beed8e1b654.tar.xz | |
os/exec: TestExtraFiles - close any leaked file descriptors
Ensure that file descriptors have not already been leaked into our
environment - close any that are open at the start of the
TestExtraFiles test.
Also use the appropriate command for listing open files.
R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/5574062
Diffstat (limited to 'src/pkg/os/exec/exec_test.go')
| -rw-r--r-- | src/pkg/os/exec/exec_test.go | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/src/pkg/os/exec/exec_test.go b/src/pkg/os/exec/exec_test.go index c68498047f..d456dfb53d 100644 --- a/src/pkg/os/exec/exec_test.go +++ b/src/pkg/os/exec/exec_test.go @@ -17,6 +17,7 @@ import ( "runtime" "strconv" "strings" + "syscall" "testing" ) @@ -150,6 +151,15 @@ func TestExtraFiles(t *testing.T) { return } + // Ensure that file descriptors have not already been leaked into + // our environment. + for fd := os.Stderr.Fd() + 1; fd <= 101; fd++ { + err := syscall.Close(fd) + if err == nil { + t.Logf("Something already leaked - closed fd %d", fd) + } + } + // Force network usage, to verify the epoll (or whatever) fd // doesn't leak to the child, ln, err := net.Listen("tcp", "127.0.0.1:0") @@ -202,6 +212,13 @@ func TestHelperProcess(*testing.T) { } defer os.Exit(0) + // Determine which command to use to display open files. + ofcmd := "lsof" + switch runtime.GOOS { + case "freebsd", "netbsd", "openbsd": + ofcmd = "fstat" + } + args := os.Args for len(args) > 0 { if args[0] == "--" { @@ -282,7 +299,7 @@ func TestHelperProcess(*testing.T) { } if got := f.Fd(); got != wantfd { fmt.Printf("leaked parent file. fd = %d; want %d\n", got, wantfd) - out, _ := Command("lsof", "-p", fmt.Sprint(os.Getpid())).CombinedOutput() + out, _ := Command(ofcmd, "-p", fmt.Sprint(os.Getpid())).CombinedOutput() fmt.Print(string(out)) os.Exit(1) } |
