Skip to content

Commit

Permalink
Merge pull request #56 from tendermint/memdb-fix-close
Browse files Browse the repository at this point in the history
db: fix MemDB.Close
  • Loading branch information
ebuchman committed Oct 3, 2017
2 parents 0948343 + 8be8127 commit a55ec42
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
8 changes: 5 additions & 3 deletions db/mem_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ func (db *MemDB) DeleteSync(key []byte) {
}

func (db *MemDB) Close() {
db.mtx.Lock()
defer db.mtx.Unlock()
db = nil
// Close is a noop since for an in-memory
// database, we don't have a destination
// to flush contents to nor do we want
// any data loss on invoking Close()
// See the discussion in https://github.com/tendermint/tmlibs/pull/56
}

func (db *MemDB) Print() {
Expand Down
20 changes: 20 additions & 0 deletions db/mem_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestMemDbIterator(t *testing.T) {
Expand All @@ -26,3 +27,22 @@ func TestMemDbIterator(t *testing.T) {
}
assert.Equal(t, i, len(db.db), "iterator didnt cover whole db")
}

func TestMemDBClose(t *testing.T) {
db := NewMemDB()
copyDB := func(orig map[string][]byte) map[string][]byte {
copy := make(map[string][]byte)
for k, v := range orig {
copy[k] = v
}
return copy
}
k, v := []byte("foo"), []byte("bar")
db.Set(k, v)
require.Equal(t, db.Get(k), v, "expecting a successful get")
copyBefore := copyDB(db.db)
db.Close()
require.Equal(t, db.Get(k), v, "Close is a noop, expecting a successful get")
copyAfter := copyDB(db.db)
require.Equal(t, copyBefore, copyAfter, "Close is a noop and shouldn't modify any internal data")
}

0 comments on commit a55ec42

Please sign in to comment.