2017-11-09 14:42:32 -08:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2017-12-12 17:58:35 -08:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-11-09 14:42:32 -08:00
|
|
|
cmn "github.com/tendermint/tmlibs/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
func checkValid(t *testing.T, itr Iterator, expected bool) {
|
|
|
|
valid := itr.Valid()
|
2017-12-12 17:58:35 -08:00
|
|
|
require.Equal(t, expected, valid)
|
2017-11-09 14:42:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkNext(t *testing.T, itr Iterator, expected bool) {
|
|
|
|
itr.Next()
|
|
|
|
valid := itr.Valid()
|
2017-12-12 17:58:35 -08:00
|
|
|
require.Equal(t, expected, valid)
|
2017-11-09 14:42:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkNextPanics(t *testing.T, itr Iterator) {
|
|
|
|
assert.Panics(t, func() { itr.Next() }, "checkNextPanics expected panic but didn't")
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkItem(t *testing.T, itr Iterator, key []byte, value []byte) {
|
|
|
|
k, v := itr.Key(), itr.Value()
|
|
|
|
assert.Exactly(t, key, k)
|
|
|
|
assert.Exactly(t, value, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkInvalid(t *testing.T, itr Iterator) {
|
|
|
|
checkValid(t, itr, false)
|
|
|
|
checkKeyPanics(t, itr)
|
|
|
|
checkValuePanics(t, itr)
|
|
|
|
checkNextPanics(t, itr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkKeyPanics(t *testing.T, itr Iterator) {
|
|
|
|
assert.Panics(t, func() { itr.Key() }, "checkKeyPanics expected panic but didn't")
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkValuePanics(t *testing.T, itr Iterator) {
|
|
|
|
assert.Panics(t, func() { itr.Key() }, "checkValuePanics expected panic but didn't")
|
|
|
|
}
|
|
|
|
|
2018-02-02 09:50:24 -08:00
|
|
|
func newTempDB(t *testing.T, backend DbBackendType) (db DB) {
|
2017-11-09 14:42:32 -08:00
|
|
|
dir, dirname := cmn.Tempdir("test_go_iterator")
|
|
|
|
db = NewDB("testdb", backend, dirname)
|
|
|
|
dir.Close()
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDBIteratorSingleKey(t *testing.T) {
|
|
|
|
for backend, _ := range backends {
|
|
|
|
t.Run(fmt.Sprintf("Backend %s", backend), func(t *testing.T) {
|
|
|
|
db := newTempDB(t, backend)
|
|
|
|
db.SetSync(bz("1"), bz("value_1"))
|
2017-12-17 13:11:28 -08:00
|
|
|
itr := db.Iterator(nil, nil)
|
2017-11-09 14:42:32 -08:00
|
|
|
|
|
|
|
checkValid(t, itr, true)
|
|
|
|
checkNext(t, itr, false)
|
|
|
|
checkValid(t, itr, false)
|
|
|
|
checkNextPanics(t, itr)
|
|
|
|
|
|
|
|
// Once invalid...
|
|
|
|
checkInvalid(t, itr)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDBIteratorTwoKeys(t *testing.T) {
|
|
|
|
for backend, _ := range backends {
|
|
|
|
t.Run(fmt.Sprintf("Backend %s", backend), func(t *testing.T) {
|
|
|
|
db := newTempDB(t, backend)
|
|
|
|
db.SetSync(bz("1"), bz("value_1"))
|
|
|
|
db.SetSync(bz("2"), bz("value_1"))
|
|
|
|
|
|
|
|
{ // Fail by calling Next too much
|
2017-12-17 13:11:28 -08:00
|
|
|
itr := db.Iterator(nil, nil)
|
2017-11-09 14:42:32 -08:00
|
|
|
checkValid(t, itr, true)
|
|
|
|
|
|
|
|
checkNext(t, itr, true)
|
|
|
|
checkValid(t, itr, true)
|
|
|
|
|
|
|
|
checkNext(t, itr, false)
|
|
|
|
checkValid(t, itr, false)
|
|
|
|
|
|
|
|
checkNextPanics(t, itr)
|
|
|
|
|
|
|
|
// Once invalid...
|
|
|
|
checkInvalid(t, itr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-17 13:11:28 -08:00
|
|
|
func TestDBIteratorMany(t *testing.T) {
|
|
|
|
for backend, _ := range backends {
|
|
|
|
t.Run(fmt.Sprintf("Backend %s", backend), func(t *testing.T) {
|
|
|
|
db := newTempDB(t, backend)
|
|
|
|
|
|
|
|
keys := make([][]byte, 100)
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
keys[i] = []byte{byte(i)}
|
|
|
|
}
|
|
|
|
|
|
|
|
value := []byte{5}
|
|
|
|
for _, k := range keys {
|
|
|
|
db.Set(k, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
itr := db.Iterator(nil, nil)
|
|
|
|
defer itr.Close()
|
|
|
|
for ; itr.Valid(); itr.Next() {
|
|
|
|
assert.Equal(t, db.Get(itr.Key()), itr.Value())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-09 14:42:32 -08:00
|
|
|
func TestDBIteratorEmpty(t *testing.T) {
|
|
|
|
for backend, _ := range backends {
|
|
|
|
t.Run(fmt.Sprintf("Backend %s", backend), func(t *testing.T) {
|
|
|
|
db := newTempDB(t, backend)
|
2017-12-17 13:11:28 -08:00
|
|
|
itr := db.Iterator(nil, nil)
|
2017-11-09 14:42:32 -08:00
|
|
|
|
|
|
|
checkInvalid(t, itr)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-12 14:45:31 -08:00
|
|
|
func TestDBIteratorEmptyBeginAfter(t *testing.T) {
|
2017-11-09 14:42:32 -08:00
|
|
|
for backend, _ := range backends {
|
|
|
|
t.Run(fmt.Sprintf("Backend %s", backend), func(t *testing.T) {
|
|
|
|
db := newTempDB(t, backend)
|
2017-12-17 13:11:28 -08:00
|
|
|
itr := db.Iterator(bz("1"), nil)
|
2017-11-09 14:42:32 -08:00
|
|
|
|
|
|
|
checkInvalid(t, itr)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-12 14:45:31 -08:00
|
|
|
func TestDBIteratorNonemptyBeginAfter(t *testing.T) {
|
2017-11-09 14:42:32 -08:00
|
|
|
for backend, _ := range backends {
|
|
|
|
t.Run(fmt.Sprintf("Backend %s", backend), func(t *testing.T) {
|
|
|
|
db := newTempDB(t, backend)
|
|
|
|
db.SetSync(bz("1"), bz("value_1"))
|
2017-12-17 13:11:28 -08:00
|
|
|
itr := db.Iterator(bz("2"), nil)
|
2017-11-09 14:42:32 -08:00
|
|
|
|
|
|
|
checkInvalid(t, itr)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|