store: improve Seek tests

After #2193 Seek results are sorted in an ascending way, so technically
the test was needed to be fixed along with these changes.
This commit is contained in:
Anna Shaleva 2021-12-28 17:07:52 +03:00
parent 9879514412
commit 5770a581c3
2 changed files with 61 additions and 75 deletions

View file

@ -138,33 +138,33 @@ func TestCachedSeek(t *testing.T) {
// Given this prefix... // Given this prefix...
goodPrefix = []byte{'f'} goodPrefix = []byte{'f'}
// these pairs should be found... // these pairs should be found...
lowerKVs = []kvSeen{ lowerKVs = []KeyValue{
{[]byte("foo"), []byte("bar"), false}, {[]byte("foo"), []byte("bar")},
{[]byte("faa"), []byte("bra"), false}, {[]byte("faa"), []byte("bra")},
} }
// and these should be not. // and these should be not.
deletedKVs = []kvSeen{ deletedKVs = []KeyValue{
{[]byte("fee"), []byte("pow"), false}, {[]byte("fee"), []byte("pow")},
{[]byte("fii"), []byte("qaz"), false}, {[]byte("fii"), []byte("qaz")},
} }
// and these should be not. // and these should be not.
updatedKVs = []kvSeen{ updatedKVs = []KeyValue{
{[]byte("fuu"), []byte("wop"), false}, {[]byte("fuu"), []byte("wop")},
{[]byte("fyy"), []byte("zaq"), false}, {[]byte("fyy"), []byte("zaq")},
} }
ps = NewMemoryStore() ps = NewMemoryStore()
ts = NewMemCachedStore(ps) ts = NewMemCachedStore(ps)
) )
for _, v := range lowerKVs { for _, v := range lowerKVs {
require.NoError(t, ps.Put(v.key, v.val)) require.NoError(t, ps.Put(v.Key, v.Value))
} }
for _, v := range deletedKVs { for _, v := range deletedKVs {
require.NoError(t, ps.Put(v.key, v.val)) require.NoError(t, ps.Put(v.Key, v.Value))
require.NoError(t, ts.Delete(v.key)) require.NoError(t, ts.Delete(v.Key))
} }
for _, v := range updatedKVs { for _, v := range updatedKVs {
require.NoError(t, ps.Put(v.key, []byte("stub"))) require.NoError(t, ps.Put(v.Key, []byte("stub")))
require.NoError(t, ts.Put(v.key, v.val)) require.NoError(t, ts.Put(v.Key, v.Value))
} }
foundKVs := make(map[string][]byte) foundKVs := make(map[string][]byte)
ts.Seek(goodPrefix, func(k, v []byte) { ts.Seek(goodPrefix, func(k, v []byte) {
@ -172,14 +172,14 @@ func TestCachedSeek(t *testing.T) {
}) })
assert.Equal(t, len(foundKVs), len(lowerKVs)+len(updatedKVs)) assert.Equal(t, len(foundKVs), len(lowerKVs)+len(updatedKVs))
for _, kv := range lowerKVs { for _, kv := range lowerKVs {
assert.Equal(t, kv.val, foundKVs[string(kv.key)]) assert.Equal(t, kv.Value, foundKVs[string(kv.Key)])
} }
for _, kv := range deletedKVs { for _, kv := range deletedKVs {
_, ok := foundKVs[string(kv.key)] _, ok := foundKVs[string(kv.Key)]
assert.Equal(t, false, ok) assert.Equal(t, false, ok)
} }
for _, kv := range updatedKVs { for _, kv := range updatedKVs {
assert.Equal(t, kv.val, foundKVs[string(kv.key)]) assert.Equal(t, kv.Value, foundKVs[string(kv.Key)])
} }
} }
@ -332,46 +332,46 @@ func TestCachedSeekSorting(t *testing.T) {
// Given this prefix... // Given this prefix...
goodPrefix = []byte{1} goodPrefix = []byte{1}
// these pairs should be found... // these pairs should be found...
lowerKVs = []kvSeen{ lowerKVs = []KeyValue{
{[]byte{1, 2, 3}, []byte("bra"), false}, {[]byte{1, 2, 3}, []byte("bra")},
{[]byte{1, 2, 5}, []byte("bar"), false}, {[]byte{1, 2, 5}, []byte("bar")},
{[]byte{1, 3, 3}, []byte("bra"), false}, {[]byte{1, 3, 3}, []byte("bra")},
{[]byte{1, 3, 5}, []byte("bra"), false}, {[]byte{1, 3, 5}, []byte("bra")},
} }
// and these should be not. // and these should be not.
deletedKVs = []kvSeen{ deletedKVs = []KeyValue{
{[]byte{1, 7, 3}, []byte("pow"), false}, {[]byte{1, 7, 3}, []byte("pow")},
{[]byte{1, 7, 4}, []byte("qaz"), false}, {[]byte{1, 7, 4}, []byte("qaz")},
} }
// and these should be not. // and these should be not.
updatedKVs = []kvSeen{ updatedKVs = []KeyValue{
{[]byte{1, 2, 4}, []byte("zaq"), false}, {[]byte{1, 2, 4}, []byte("zaq")},
{[]byte{1, 2, 6}, []byte("zaq"), false}, {[]byte{1, 2, 6}, []byte("zaq")},
{[]byte{1, 3, 2}, []byte("wop"), false}, {[]byte{1, 3, 2}, []byte("wop")},
{[]byte{1, 3, 4}, []byte("zaq"), false}, {[]byte{1, 3, 4}, []byte("zaq")},
} }
ps = NewMemoryStore() ps = NewMemoryStore()
ts = NewMemCachedStore(ps) ts = NewMemCachedStore(ps)
) )
for _, v := range lowerKVs { for _, v := range lowerKVs {
require.NoError(t, ps.Put(v.key, v.val)) require.NoError(t, ps.Put(v.Key, v.Value))
} }
for _, v := range deletedKVs { for _, v := range deletedKVs {
require.NoError(t, ps.Put(v.key, v.val)) require.NoError(t, ps.Put(v.Key, v.Value))
require.NoError(t, ts.Delete(v.key)) require.NoError(t, ts.Delete(v.Key))
} }
for _, v := range updatedKVs { for _, v := range updatedKVs {
require.NoError(t, ps.Put(v.key, []byte("stub"))) require.NoError(t, ps.Put(v.Key, []byte("stub")))
require.NoError(t, ts.Put(v.key, v.val)) require.NoError(t, ts.Put(v.Key, v.Value))
} }
var foundKVs []kvSeen var foundKVs []KeyValue
ts.Seek(goodPrefix, func(k, v []byte) { ts.Seek(goodPrefix, func(k, v []byte) {
foundKVs = append(foundKVs, kvSeen{key: slice.Copy(k), val: slice.Copy(v)}) foundKVs = append(foundKVs, KeyValue{Key: slice.Copy(k), Value: slice.Copy(v)})
}) })
assert.Equal(t, len(foundKVs), len(lowerKVs)+len(updatedKVs)) assert.Equal(t, len(foundKVs), len(lowerKVs)+len(updatedKVs))
expected := append(lowerKVs, updatedKVs...) expected := append(lowerKVs, updatedKVs...)
sort.Slice(expected, func(i, j int) bool { sort.Slice(expected, func(i, j int) bool {
return bytes.Compare(expected[i].key, expected[j].key) < 0 return bytes.Compare(expected[i].Key, expected[j].Key) < 0
}) })
require.Equal(t, expected, foundKVs) require.Equal(t, expected, foundKVs)
} }

View file

@ -1,8 +1,10 @@
package storage package storage
import ( import (
"bytes"
"reflect" "reflect"
"runtime" "runtime"
"sort"
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/util/slice" "github.com/nspcc-dev/neo-go/pkg/util/slice"
@ -10,13 +12,6 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
// kvSeen is used to test Seek implementations.
type kvSeen struct {
key []byte
val []byte
seen bool
}
type dbSetup struct { type dbSetup struct {
name string name string
create func(testing.TB) Store create func(testing.TB) Store
@ -76,47 +71,38 @@ func testStoreSeek(t *testing.T, s Store) {
// Given this prefix... // Given this prefix...
goodprefix = []byte{'f'} goodprefix = []byte{'f'}
// these pairs should be found... // these pairs should be found...
goodkvs = []kvSeen{ goodkvs = []KeyValue{
{[]byte("foo"), []byte("bar"), false}, {[]byte("foo"), []byte("bar")},
{[]byte("faa"), []byte("bra"), false}, {[]byte("faa"), []byte("bra")},
{[]byte("foox"), []byte("barx"), false}, {[]byte("foox"), []byte("barx")},
} }
// and these should be not. // and these should be not.
badkvs = []kvSeen{ badkvs = []KeyValue{
{[]byte("doo"), []byte("pow"), false}, {[]byte("doo"), []byte("pow")},
{[]byte("mew"), []byte("qaz"), false}, {[]byte("mew"), []byte("qaz")},
} }
) )
for _, v := range goodkvs { for _, v := range goodkvs {
require.NoError(t, s.Put(v.key, v.val)) require.NoError(t, s.Put(v.Key, v.Value))
} }
for _, v := range badkvs { for _, v := range badkvs {
require.NoError(t, s.Put(v.key, v.val)) require.NoError(t, s.Put(v.Key, v.Value))
} }
numFound := 0 // Seek result expected to be sorted in an ascending way.
s.Seek(goodprefix, func(k, v []byte) { sort.Slice(goodkvs, func(i, j int) bool {
for i := 0; i < len(goodkvs); i++ { return bytes.Compare(goodkvs[i].Key, goodkvs[j].Key) < 0
if string(k) == string(goodkvs[i].key) {
assert.Equal(t, string(goodkvs[i].val), string(v))
goodkvs[i].seen = true
}
}
for i := 0; i < len(badkvs); i++ {
if string(k) == string(badkvs[i].key) {
badkvs[i].seen = true
}
}
numFound++
}) })
assert.Equal(t, len(goodkvs), numFound)
for i := 0; i < len(goodkvs); i++ { actual := make([]KeyValue, 0, len(goodkvs))
assert.Equal(t, true, goodkvs[i].seen) s.Seek(goodprefix, func(k, v []byte) {
} actual = append(actual, KeyValue{
for i := 0; i < len(badkvs); i++ { Key: slice.Copy(k),
assert.Equal(t, false, badkvs[i].seen) Value: slice.Copy(v),
} })
})
assert.Equal(t, goodkvs, actual)
require.NoError(t, s.Close()) require.NoError(t, s.Close())
} }