Commit a00958aa authored by Dave Cheney's avatar Dave Cheney

all: avoid leaking fds during tests

trivial: it is not a serious problem to leak a fd in a short lived process, but it was obscuring my investigation of issue 5593.

R=golang-dev, iant, bradfitz
CC=golang-dev
https://golang.org/cl/10391043
parent 0bc7e79a
...@@ -171,6 +171,7 @@ testLoop: ...@@ -171,6 +171,7 @@ testLoop:
t.Errorf("test %d: Unexpected error: %v", i, err) t.Errorf("test %d: Unexpected error: %v", i, err)
continue continue
} }
defer f.Close()
tr := NewReader(f) tr := NewReader(f)
for j, header := range test.headers { for j, header := range test.headers {
hdr, err := tr.Next() hdr, err := tr.Next()
...@@ -191,7 +192,6 @@ testLoop: ...@@ -191,7 +192,6 @@ testLoop:
if hdr != nil || err != nil { if hdr != nil || err != nil {
t.Errorf("test %d: Unexpected entry or error: hdr=%v err=%v", i, hdr, err) t.Errorf("test %d: Unexpected entry or error: hdr=%v err=%v", i, hdr, err)
} }
f.Close()
} }
} }
......
...@@ -276,6 +276,7 @@ func readTestZip(t *testing.T, zt ZipTest) { ...@@ -276,6 +276,7 @@ func readTestZip(t *testing.T, zt ZipTest) {
var rc *ReadCloser var rc *ReadCloser
rc, err = OpenReader(filepath.Join("testdata", zt.Name)) rc, err = OpenReader(filepath.Join("testdata", zt.Name))
if err == nil { if err == nil {
defer rc.Close()
z = &rc.Reader z = &rc.Reader
} }
} }
......
...@@ -166,6 +166,7 @@ func TestOpen(t *testing.T) { ...@@ -166,6 +166,7 @@ func TestOpen(t *testing.T) {
} else { } else {
f, err = Open(tt.file) f, err = Open(tt.file)
} }
defer f.Close()
if err != nil { if err != nil {
t.Errorf("cannot open file %s: %v", tt.file, err) t.Errorf("cannot open file %s: %v", tt.file, err)
continue continue
......
...@@ -255,6 +255,7 @@ func TestWrite(t *testing.T) { ...@@ -255,6 +255,7 @@ func TestWrite(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("syslog.Dial() failed: %v", err) t.Fatalf("syslog.Dial() failed: %v", err)
} }
defer l.Close()
_, err = io.WriteString(l, test.msg) _, err = io.WriteString(l, test.msg)
if err != nil { if err != nil {
t.Fatalf("WriteString() failed: %v", err) t.Fatalf("WriteString() failed: %v", err)
...@@ -328,6 +329,7 @@ func TestConcurrentReconnect(t *testing.T) { ...@@ -328,6 +329,7 @@ func TestConcurrentReconnect(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("syslog.Dial() failed: %v", err) t.Fatalf("syslog.Dial() failed: %v", err)
} }
defer w.Close()
for i := 0; i < M; i++ { for i := 0; i < M; i++ {
err := w.Info("test") err := w.Info("test")
if err != nil { if err != nil {
......
...@@ -25,6 +25,7 @@ func TestShutdown(t *testing.T) { ...@@ -25,6 +25,7 @@ func TestShutdown(t *testing.T) {
} }
go func() { go func() {
defer ln.Close()
c, err := ln.Accept() c, err := ln.Accept()
if err != nil { if err != nil {
t.Fatalf("Accept: %v", err) t.Fatalf("Accept: %v", err)
...@@ -75,7 +76,10 @@ func TestShutdownUnix(t *testing.T) { ...@@ -75,7 +76,10 @@ func TestShutdownUnix(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("ListenUnix on %s: %s", tmpname, err) t.Fatalf("ListenUnix on %s: %s", tmpname, err)
} }
defer os.Remove(tmpname) defer func() {
ln.Close()
os.Remove(tmpname)
}()
go func() { go func() {
c, err := ln.Accept() c, err := ln.Accept()
......
...@@ -23,12 +23,14 @@ func TestReadLine(t *testing.T) { ...@@ -23,12 +23,14 @@ func TestReadLine(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("open %s: %v", filename, err) t.Fatalf("open %s: %v", filename, err)
} }
defer fd.Close()
br := bufio.NewReader(fd) br := bufio.NewReader(fd)
file, err := open(filename) file, err := open(filename)
if file == nil { if file == nil {
t.Fatalf("net.open(%s) = nil", filename) t.Fatalf("net.open(%s) = nil", filename)
} }
defer file.close()
lineno := 1 lineno := 1
byteno := 0 byteno := 0
......
...@@ -299,6 +299,7 @@ func TestReaddirnamesOneAtATime(t *testing.T) { ...@@ -299,6 +299,7 @@ func TestReaddirnamesOneAtATime(t *testing.T) {
if err2 != nil { if err2 != nil {
t.Fatalf("open %q failed: %v", dir, err2) t.Fatalf("open %q failed: %v", dir, err2)
} }
defer file1.Close()
small := smallReaddirnames(file1, len(all)+100, t) // +100 in case we screw up small := smallReaddirnames(file1, len(all)+100, t) // +100 in case we screw up
if len(small) < len(all) { if len(small) < len(all) {
t.Fatalf("len(small) is %d, less than %d", len(small), len(all)) t.Fatalf("len(small) is %d, less than %d", len(small), len(all))
...@@ -526,6 +527,7 @@ func exec(t *testing.T, dir, cmd string, args []string, expect string) { ...@@ -526,6 +527,7 @@ func exec(t *testing.T, dir, cmd string, args []string, expect string) {
if err != nil { if err != nil {
t.Fatalf("Pipe: %v", err) t.Fatalf("Pipe: %v", err)
} }
defer r.Close()
attr := &ProcAttr{Dir: dir, Files: []*File{nil, w, Stderr}} attr := &ProcAttr{Dir: dir, Files: []*File{nil, w, Stderr}}
p, err := StartProcess(cmd, args, attr) p, err := StartProcess(cmd, args, attr)
if err != nil { if err != nil {
...@@ -844,6 +846,7 @@ func run(t *testing.T, cmd []string) string { ...@@ -844,6 +846,7 @@ func run(t *testing.T, cmd []string) string {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer r.Close()
p, err := StartProcess("/bin/hostname", []string{"hostname"}, &ProcAttr{Files: []*File{nil, w, Stderr}}) p, err := StartProcess("/bin/hostname", []string{"hostname"}, &ProcAttr{Files: []*File{nil, w, Stderr}})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
......
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