From 5770a581c3fa66723c805c001214f59ff07098c7 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Tue, 28 Dec 2021 17:07:52 +0300 Subject: [PATCH] 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. --- pkg/core/storage/memcached_store_test.go | 76 ++++++++++++------------ pkg/core/storage/storeandbatch_test.go | 60 +++++++------------ 2 files changed, 61 insertions(+), 75 deletions(-) diff --git a/pkg/core/storage/memcached_store_test.go b/pkg/core/storage/memcached_store_test.go index eb572d48f..f5c380238 100644 --- a/pkg/core/storage/memcached_store_test.go +++ b/pkg/core/storage/memcached_store_test.go @@ -138,33 +138,33 @@ func TestCachedSeek(t *testing.T) { // Given this prefix... goodPrefix = []byte{'f'} // these pairs should be found... - lowerKVs = []kvSeen{ - {[]byte("foo"), []byte("bar"), false}, - {[]byte("faa"), []byte("bra"), false}, + lowerKVs = []KeyValue{ + {[]byte("foo"), []byte("bar")}, + {[]byte("faa"), []byte("bra")}, } // and these should be not. - deletedKVs = []kvSeen{ - {[]byte("fee"), []byte("pow"), false}, - {[]byte("fii"), []byte("qaz"), false}, + deletedKVs = []KeyValue{ + {[]byte("fee"), []byte("pow")}, + {[]byte("fii"), []byte("qaz")}, } // and these should be not. - updatedKVs = []kvSeen{ - {[]byte("fuu"), []byte("wop"), false}, - {[]byte("fyy"), []byte("zaq"), false}, + updatedKVs = []KeyValue{ + {[]byte("fuu"), []byte("wop")}, + {[]byte("fyy"), []byte("zaq")}, } ps = NewMemoryStore() ts = NewMemCachedStore(ps) ) 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 { - require.NoError(t, ps.Put(v.key, v.val)) - require.NoError(t, ts.Delete(v.key)) + require.NoError(t, ps.Put(v.Key, v.Value)) + require.NoError(t, ts.Delete(v.Key)) } for _, v := range updatedKVs { - require.NoError(t, ps.Put(v.key, []byte("stub"))) - require.NoError(t, ts.Put(v.key, v.val)) + require.NoError(t, ps.Put(v.Key, []byte("stub"))) + require.NoError(t, ts.Put(v.Key, v.Value)) } foundKVs := make(map[string][]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)) 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 { - _, ok := foundKVs[string(kv.key)] + _, ok := foundKVs[string(kv.Key)] assert.Equal(t, false, ok) } 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... goodPrefix = []byte{1} // these pairs should be found... - lowerKVs = []kvSeen{ - {[]byte{1, 2, 3}, []byte("bra"), false}, - {[]byte{1, 2, 5}, []byte("bar"), false}, - {[]byte{1, 3, 3}, []byte("bra"), false}, - {[]byte{1, 3, 5}, []byte("bra"), false}, + lowerKVs = []KeyValue{ + {[]byte{1, 2, 3}, []byte("bra")}, + {[]byte{1, 2, 5}, []byte("bar")}, + {[]byte{1, 3, 3}, []byte("bra")}, + {[]byte{1, 3, 5}, []byte("bra")}, } // and these should be not. - deletedKVs = []kvSeen{ - {[]byte{1, 7, 3}, []byte("pow"), false}, - {[]byte{1, 7, 4}, []byte("qaz"), false}, + deletedKVs = []KeyValue{ + {[]byte{1, 7, 3}, []byte("pow")}, + {[]byte{1, 7, 4}, []byte("qaz")}, } // and these should be not. - updatedKVs = []kvSeen{ - {[]byte{1, 2, 4}, []byte("zaq"), false}, - {[]byte{1, 2, 6}, []byte("zaq"), false}, - {[]byte{1, 3, 2}, []byte("wop"), false}, - {[]byte{1, 3, 4}, []byte("zaq"), false}, + updatedKVs = []KeyValue{ + {[]byte{1, 2, 4}, []byte("zaq")}, + {[]byte{1, 2, 6}, []byte("zaq")}, + {[]byte{1, 3, 2}, []byte("wop")}, + {[]byte{1, 3, 4}, []byte("zaq")}, } ps = NewMemoryStore() ts = NewMemCachedStore(ps) ) 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 { - require.NoError(t, ps.Put(v.key, v.val)) - require.NoError(t, ts.Delete(v.key)) + require.NoError(t, ps.Put(v.Key, v.Value)) + require.NoError(t, ts.Delete(v.Key)) } for _, v := range updatedKVs { - require.NoError(t, ps.Put(v.key, []byte("stub"))) - require.NoError(t, ts.Put(v.key, v.val)) + require.NoError(t, ps.Put(v.Key, []byte("stub"))) + require.NoError(t, ts.Put(v.Key, v.Value)) } - var foundKVs []kvSeen + var foundKVs []KeyValue 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)) expected := append(lowerKVs, updatedKVs...) 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) } diff --git a/pkg/core/storage/storeandbatch_test.go b/pkg/core/storage/storeandbatch_test.go index a8591ddfe..591afbcd2 100644 --- a/pkg/core/storage/storeandbatch_test.go +++ b/pkg/core/storage/storeandbatch_test.go @@ -1,8 +1,10 @@ package storage import ( + "bytes" "reflect" "runtime" + "sort" "testing" "github.com/nspcc-dev/neo-go/pkg/util/slice" @@ -10,13 +12,6 @@ import ( "github.com/stretchr/testify/require" ) -// kvSeen is used to test Seek implementations. -type kvSeen struct { - key []byte - val []byte - seen bool -} - type dbSetup struct { name string create func(testing.TB) Store @@ -76,47 +71,38 @@ func testStoreSeek(t *testing.T, s Store) { // Given this prefix... goodprefix = []byte{'f'} // these pairs should be found... - goodkvs = []kvSeen{ - {[]byte("foo"), []byte("bar"), false}, - {[]byte("faa"), []byte("bra"), false}, - {[]byte("foox"), []byte("barx"), false}, + goodkvs = []KeyValue{ + {[]byte("foo"), []byte("bar")}, + {[]byte("faa"), []byte("bra")}, + {[]byte("foox"), []byte("barx")}, } // and these should be not. - badkvs = []kvSeen{ - {[]byte("doo"), []byte("pow"), false}, - {[]byte("mew"), []byte("qaz"), false}, + badkvs = []KeyValue{ + {[]byte("doo"), []byte("pow")}, + {[]byte("mew"), []byte("qaz")}, } ) 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 { - require.NoError(t, s.Put(v.key, v.val)) + require.NoError(t, s.Put(v.Key, v.Value)) } - numFound := 0 - s.Seek(goodprefix, func(k, v []byte) { - for i := 0; i < len(goodkvs); i++ { - 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++ + // Seek result expected to be sorted in an ascending way. + sort.Slice(goodkvs, func(i, j int) bool { + return bytes.Compare(goodkvs[i].Key, goodkvs[j].Key) < 0 }) - assert.Equal(t, len(goodkvs), numFound) - for i := 0; i < len(goodkvs); i++ { - assert.Equal(t, true, goodkvs[i].seen) - } - for i := 0; i < len(badkvs); i++ { - assert.Equal(t, false, badkvs[i].seen) - } + + actual := make([]KeyValue, 0, len(goodkvs)) + s.Seek(goodprefix, func(k, v []byte) { + actual = append(actual, KeyValue{ + Key: slice.Copy(k), + Value: slice.Copy(v), + }) + }) + assert.Equal(t, goodkvs, actual) require.NoError(t, s.Close()) }