Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return Errors instead of Panicing #30

Merged
merged 15 commits into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

/.vscode
.idea
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ linters-settings:
maligned:
# print struct with more effective memory layout or not, false by default
suggest-new: true
errcheck:
# report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`;
# default is false: such cases aren't reported by default.
check-blank: true
129 changes: 89 additions & 40 deletions backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,46 @@ func testBackendGetSetDelete(t *testing.T, backend BackendType) {
defer cleanupDBDir(dirname, "testdb")

// A nonexistent key should return nil, even if the key is empty
require.Nil(t, db.Get([]byte("")))
item, err := db.Get([]byte(""))
require.NoError(t, err)
require.Nil(t, item)

// A nonexistent key should return nil, even if the key is nil
require.Nil(t, db.Get(nil))
value, err := db.Get(nil)
require.NoError(t, err)
require.Nil(t, value)

// A nonexistent key should return nil.
key := []byte("abc")
require.Nil(t, db.Get(key))
value, err = db.Get(key)
require.NoError(t, err)
require.Nil(t, value)

// Set empty value.
db.Set(key, []byte(""))
require.NotNil(t, db.Get(key))
require.Empty(t, db.Get(key))
err = db.Set(key, []byte(""))
require.NoError(t, err)

value, err = db.Get(key)
require.NoError(t, err)
require.NotNil(t, value)
require.Empty(t, value)

// Set nil value.
db.Set(key, nil)
require.NotNil(t, db.Get(key))
require.Empty(t, db.Get(key))
err = db.Set(key, nil)
require.NoError(t, err)

value, err = db.Get(key)
require.NoError(t, err)
require.NotNil(t, value)
require.Empty(t, value)

// Delete.
db.Delete(key)
require.Nil(t, db.Get(key))
err = db.Delete(key)
require.NoError(t, err)

value, err = db.Get(key)
require.NoError(t, err)
require.Nil(t, value)
}

func TestBackendsGetSetDelete(t *testing.T) {
Expand All @@ -72,74 +90,101 @@ func TestBackendsNilKeys(t *testing.T) {
for dbType, creator := range backends {
withDB(t, creator, func(db DB) {
t.Run(fmt.Sprintf("Testing %s", dbType), func(t *testing.T) {

// Nil keys are treated as the empty key for most operations.
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
expect := func(key, value []byte) {
if len(key) == 0 { // nil or empty
assert.Equal(t, db.Get(nil), db.Get([]byte("")))
assert.Equal(t, db.Has(nil), db.Has([]byte("")))
nilValue, err := db.Get(nil)
assert.NoError(t, err)
byteValue, err := db.Get([]byte(""))
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
assert.NoError(t, err)
assert.Equal(t, nilValue, byteValue)
hasNil, err := db.Has(nil)
assert.NoError(t, err)
hasStr, err := db.Has([]byte(""))
assert.NoError(t, err)
assert.Equal(t, hasNil, hasStr)
}
assert.Equal(t, db.Get(key), value)
assert.Equal(t, db.Has(key), value != nil)
value2, err := db.Get(key)
assert.Equal(t, value2, value)
assert.NoError(t, err)
hasKey, err := db.Has(key)
assert.NoError(t, err)
assert.Equal(t, hasKey, value != nil)
}

// Not set
expect(nil, nil)

// Set nil value
db.Set(nil, nil)
err := db.Set(nil, nil)
require.NoError(t, err)
expect(nil, []byte(""))

// Set empty value
db.Set(nil, []byte(""))
err = db.Set(nil, []byte(""))
require.NoError(t, err)
expect(nil, []byte(""))

// Set nil, Delete nil
db.Set(nil, []byte("abc"))
err = db.Set(nil, []byte("abc"))
expect(nil, []byte("abc"))
db.Delete(nil)
require.NoError(t, err)
err = db.Delete(nil)
require.NoError(t, err)
expect(nil, nil)

// Set nil, Delete empty
db.Set(nil, []byte("abc"))
err = db.Set(nil, []byte("abc"))
expect(nil, []byte("abc"))
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
db.Delete([]byte(""))
require.NoError(t, err)
err = db.Delete([]byte(""))
require.NoError(t, err)
expect(nil, nil)

// Set empty, Delete nil
db.Set([]byte(""), []byte("abc"))
err = db.Set([]byte(""), []byte("abc"))
expect(nil, []byte("abc"))
db.Delete(nil)
require.NoError(t, err)
err = db.Delete(nil)
require.NoError(t, err)
expect(nil, nil)

// Set empty, Delete empty
db.Set([]byte(""), []byte("abc"))
err = db.Set([]byte(""), []byte("abc"))
require.NoError(t, err)
expect(nil, []byte("abc"))
db.Delete([]byte(""))

err = db.Delete([]byte(""))
require.NoError(t, err)
expect(nil, nil)

// SetSync nil, DeleteSync nil
db.SetSync(nil, []byte("abc"))
err = db.SetSync(nil, []byte("abc"))
require.NoError(t, err)
expect(nil, []byte("abc"))
db.DeleteSync(nil)
err = db.DeleteSync(nil)
require.NoError(t, err)
expect(nil, nil)

// SetSync nil, DeleteSync empty
db.SetSync(nil, []byte("abc"))
expect(nil, []byte("abc"))
db.DeleteSync([]byte(""))
expect(nil, nil)
err = db.SetSync(nil, []byte("abc"))
require.NoError(t, err)
err = db.DeleteSync([]byte(""))
require.NoError(t, err)

// SetSync empty, DeleteSync nil
db.SetSync([]byte(""), []byte("abc"))
err = db.SetSync([]byte(""), []byte("abc"))
require.NoError(t, err)
expect(nil, []byte("abc"))
db.DeleteSync(nil)
err = db.DeleteSync(nil)
require.NoError(t, err)
expect(nil, nil)

// SetSync empty, DeleteSync empty
db.SetSync([]byte(""), []byte("abc"))
err = db.SetSync([]byte(""), []byte("abc"))
require.NoError(t, err)
expect(nil, []byte("abc"))
db.DeleteSync([]byte(""))
err = db.DeleteSync([]byte(""))
require.NoError(t, err)
expect(nil, nil)
})
})
Expand Down Expand Up @@ -171,7 +216,8 @@ func testDBIterator(t *testing.T, backend BackendType) {

for i := 0; i < 10; i++ {
if i != 6 { // but skip 6.
db.Set(int642Bytes(int64(i)), nil)
err := db.Set(int642Bytes(int64(i)), nil)
require.NoError(t, err)
}
}

Expand Down Expand Up @@ -222,8 +268,11 @@ func testDBIterator(t *testing.T, backend BackendType) {
func verifyIterator(t *testing.T, itr Iterator, expected []int64, msg string) {
var list []int64
for itr.Valid() {
list = append(list, bytes2Int64(itr.Key()))
itr.Next()
key, err := itr.Key()
assert.NoError(t, err)
list = append(list, bytes2Int64(key))
err = itr.Next()
assert.NoError(t, err)
}
assert.Equal(t, expected, list, msg)
}
Loading