Skip to content

Commit

Permalink
Add tests for info.
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeQ committed Apr 5, 2017
1 parent 973893f commit 9f91ebd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
# tdb
# tdb [![Build Status][ci-img]][ci] [![Coverage Status][cov-img]][cov]

[ci-img]: https://travis-ci.org/tracerun/tdb.svg?branch=master
[ci]: https://travis-ci.org/tracerun/tdb
[cov-img]: https://coveralls.io/repos/github/tracerun/tdb/badge.svg?branch=master
[cov]: https://coveralls.io/github/tracerun/tdb?branch=master
14 changes: 10 additions & 4 deletions info.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type info struct {
func createInfo(p string) (*info, error) {
one := &info{path: p}
one.content = make(map[string][]byte)
one.contentLock = new(sync.RWMutex)
one.fileLock = new(sync.RWMutex)

if stat, err := os.Stat(p); err != nil {
if os.IsNotExist(err) {
Expand All @@ -35,17 +37,21 @@ func createInfo(p string) (*info, error) {
}

func (one *info) loadContent() error {
one.fileLock.RLock()
b, _ := ioutil.ReadFile(one.path)
one.fileLock.RUnlock()

one.contentLock.Lock()
defer one.contentLock.Unlock()

one.fileLock.RLock()
b, err := ioutil.ReadFile(one.path)
one.fileLock.RUnlock()
if err != nil {
return err
}

var pbInfo Info
if err := proto.Unmarshal(b, &pbInfo); err != nil {
return err
}

one.content = pbInfo.Fields

return nil
Expand Down
13 changes: 11 additions & 2 deletions info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,23 @@ func TestInfoMethods(t *testing.T) {
}
defer os.RemoveAll(folderPath)

// creation on folder will fail
_, err := createInfo(folderPath)
assert.Equal(t, ErrInfoFilePath, err, "error should pop while creating info on a folder")

demoInfo := "demo_info"
defer os.Remove(demoInfo)
one, err := createInfo(demoInfo)
assert.NoError(t, err, "should have no error while creating info from non-existed file")
assert.Len(t, one.content, 0, "should have empty content")

one.setContent([]string{"key"}, [][]byte{[]byte("value")})
assert.Len(t, one.content, 1, "should have empty content")
key := "key"
value := []byte("value")
one.setContent([]string{key}, [][]byte{value})
assert.Len(t, one.content, 1, "should have one content")

two, err2 := createInfo(demoInfo)
assert.NoError(t, err2, "should have no error while creating info from an existed file")
assert.Len(t, two.content, 1, "should have empty content")
assert.EqualValues(t, one.content[key], two.content[key], "the values should be the same")
}

0 comments on commit 9f91ebd

Please sign in to comment.