]> Cypherpunks repositories - gostls13.git/commitdiff
os/exec: make TestPipeLookPathLeak more verbose when it fails
authorBrad Fitzpatrick <bradfitz@golang.org>
Wed, 16 Apr 2014 00:36:25 +0000 (17:36 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 16 Apr 2014 00:36:25 +0000 (17:36 -0700)
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

src/pkg/os/exec/exec.go
src/pkg/os/exec/exec_test.go

index 44b9cc08ce738a9f32fbf94f5175a35a91c23a69..a70ed0d20cbba995f25de65692138301d89c14ea 100644 (file)
@@ -429,15 +429,17 @@ func (c *Cmd) StdinPipe() (io.WriteCloser, error) {
 type closeOnce struct {
        *os.File
 
-       close    sync.Once
-       closeErr error
+       once sync.Once
+       err  error
 }
 
 func (c *closeOnce) Close() error {
-       c.close.Do(func() {
-               c.closeErr = c.File.Close()
-       })
-       return c.closeErr
+       c.once.Do(c.close)
+       return c.err
+}
+
+func (c *closeOnce) close() {
+       c.err = c.File.Close()
 }
 
 // StdoutPipe returns a pipe that will be connected to the command's
index e4ad7d31575dfa9ec89512fcd8ca5b6c724dd14f..ca19fe9bee5e15eba9b710e51fa6fc2543ff391e 100644 (file)
@@ -214,7 +214,7 @@ func TestStdinClose(t *testing.T) {
 
 // Issue 5071
 func TestPipeLookPathLeak(t *testing.T) {
-       fd0 := numOpenFDS(t)
+       fd0, lsof0 := numOpenFDS(t)
        for i := 0; i < 4; i++ {
                cmd := exec.Command("something-that-does-not-exist-binary")
                cmd.StdoutPipe()
@@ -224,19 +224,19 @@ func TestPipeLookPathLeak(t *testing.T) {
                        t.Fatal("unexpected success")
                }
        }
-       fdGrowth := numOpenFDS(t) - fd0
+       open, lsof := numOpenFDS(t)
+       fdGrowth := open - fd0
        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()
        if err != nil {
                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