Skip to content

Commit

Permalink
Close file after reading from it
Browse files Browse the repository at this point in the history
Added test to check number of open fds.
  • Loading branch information
Dan Brown committed Sep 23, 2013
1 parent c54e097 commit e26b5d7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions ini.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (f File) LoadFile(file string) (err error) {
if err != nil {
return
}
defer in.Close()
return f.Load(in)
}

Expand Down
34 changes: 34 additions & 0 deletions ini_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"reflect"
"strings"
"testing"
"syscall"
)

func TestLoad(t *testing.T) {
Expand Down Expand Up @@ -87,3 +88,36 @@ func TestDefinedSectionBehaviour(t *testing.T) {
"a": {"this": "that"},
})
}

func TestLoadFile(t *testing.T) {
originalOpenFiles := numFilesOpen()

file, err := LoadFile("test.ini")
if err != nil {
t.Fatal(err)
}

if originalOpenFiles != numFilesOpen() {
t.Error("test.ini not closed")
}

if !reflect.DeepEqual(file, File{"default": {"stuff": "things"}}) {
t.Error("file not read correctly")
}
}

func numFilesOpen() (num uint64) {
var rlimit syscall.Rlimit
syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlimit)
maxFds := int(rlimit.Cur)

var stat syscall.Stat_t
for i := 0; i < maxFds; i++ {
if syscall.Fstat(i, &stat) == nil {
num++
} else {
return
}
}
return
}
2 changes: 2 additions & 0 deletions test.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[default]
stuff = things

0 comments on commit e26b5d7

Please sign in to comment.