Skip to content

Commit

Permalink
Add more tests for database, fix dead lock
Browse files Browse the repository at this point in the history
Signed-off-by: Igor Shishkin <[email protected]>
  • Loading branch information
teran committed May 14, 2019
1 parent 8dd5270 commit 7d4c766
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 3 additions & 1 deletion database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,16 @@ func (d *Database) DeleteOne(path string) bool {
func (d *Database) Count() int {
mutex.Lock()
defer mutex.Unlock()

return len(d.Schema.Data)
}

// ListPaths returns list of files present in database
func (d *Database) ListPaths() []string {
keys := make([]string, 0, d.Count())

mutex.Lock()
defer mutex.Unlock()
keys := make([]string, 0, d.Count())
for k := range d.Schema.Data {
keys = append(keys, k)
}
Expand Down
17 changes: 17 additions & 0 deletions database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func (s *DatabaseTestSuite) TestAll() {
SHA256: "deadbeaf",
}

cnt := s.db.Count()
s.Require().Equal(0, cnt)

res, ok := s.db.WriteOne("test-path", dataObject)
s.Require().True(ok)
s.Require().NotNil(res)
Expand All @@ -55,6 +58,20 @@ func (s *DatabaseTestSuite) TestAll() {
s.Require().NotNil(res)
s.Require().Equal(dataObject, res)

resMap := s.db.MapObjects()
s.Require().NotNil(resMap)
s.Require().Equal(map[string]*DataObject{
"test-path": dataObject,
}, resMap)

resKeys := s.db.ListPaths()
s.Require().NotNil(resKeys)
s.Require().Len(resKeys, 1)
s.Require().Equal([]string{"test-path"}, resKeys)

cnt = s.db.Count()
s.Require().Equal(1, cnt)

ok = s.db.DeleteOne("not-existent-path")
s.Require().False(ok)

Expand Down

0 comments on commit 7d4c766

Please sign in to comment.