Commit fdade683 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

os/exec: make TestPipeLookPathLeak more verbose when it fails

Trying to understand the linux-386-387 failures:
http://build.golang.org/log/78a91da173c11e986b4e623527c2d0b746f4e814

Also modernize the closeOnce code with a method value, while I
was looking.

LGTM=adg
R=golang-codereviews, adg
CC=golang-codereviews, iant
https://golang.org/cl/87950044
parent a1ae3a05
...@@ -429,15 +429,17 @@ func (c *Cmd) StdinPipe() (io.WriteCloser, error) { ...@@ -429,15 +429,17 @@ func (c *Cmd) StdinPipe() (io.WriteCloser, error) {
type closeOnce struct { type closeOnce struct {
*os.File *os.File
close sync.Once once sync.Once
closeErr error err error
} }
func (c *closeOnce) Close() error { func (c *closeOnce) Close() error {
c.close.Do(func() { c.once.Do(c.close)
c.closeErr = c.File.Close() return c.err
}) }
return c.closeErr
func (c *closeOnce) close() {
c.err = c.File.Close()
} }
// StdoutPipe returns a pipe that will be connected to the command's // StdoutPipe returns a pipe that will be connected to the command's
......
...@@ -214,7 +214,7 @@ func TestStdinClose(t *testing.T) { ...@@ -214,7 +214,7 @@ func TestStdinClose(t *testing.T) {
// Issue 5071 // Issue 5071
func TestPipeLookPathLeak(t *testing.T) { func TestPipeLookPathLeak(t *testing.T) {
fd0 := numOpenFDS(t) fd0, lsof0 := numOpenFDS(t)
for i := 0; i < 4; i++ { for i := 0; i < 4; i++ {
cmd := exec.Command("something-that-does-not-exist-binary") cmd := exec.Command("something-that-does-not-exist-binary")
cmd.StdoutPipe() cmd.StdoutPipe()
...@@ -224,19 +224,19 @@ func TestPipeLookPathLeak(t *testing.T) { ...@@ -224,19 +224,19 @@ func TestPipeLookPathLeak(t *testing.T) {
t.Fatal("unexpected success") t.Fatal("unexpected success")
} }
} }
fdGrowth := numOpenFDS(t) - fd0 open, lsof := numOpenFDS(t)
fdGrowth := open - fd0
if fdGrowth > 2 { if fdGrowth > 2 {
t.Errorf("leaked %d fds; want ~0", fdGrowth) t.Errorf("leaked %d fds; want ~0; have:\n%s\noriginally:\n%s", fdGrowth, lsof, lsof0)
} }
} }
func numOpenFDS(t *testing.T) int { func numOpenFDS(t *testing.T) (n int, lsof []byte) {
lsof, err := exec.Command("lsof", "-n", "-p", strconv.Itoa(os.Getpid())).Output() lsof, err := exec.Command("lsof", "-n", "-p", strconv.Itoa(os.Getpid())).Output()
if err != nil { if err != nil {
t.Skip("skipping test; error finding or running lsof") t.Skip("skipping test; error finding or running lsof")
return 0
} }
return bytes.Count(lsof, []byte("\n")) return bytes.Count(lsof, []byte("\n")), lsof
} }
var testedAlreadyLeaked = false var testedAlreadyLeaked = false
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment