Skip to content

Commit

Permalink
Act kvs test (#27)
Browse files Browse the repository at this point in the history
* (test:) Refactor tests

* (fix:) Save reset counter

---------

Co-authored-by: Ferenc Sárai <[email protected]>
  • Loading branch information
2 people authored and aranyia committed Jun 6, 2024
1 parent 4a12e29 commit fee9296
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 93 deletions.
17 changes: 16 additions & 1 deletion pkg/kvs/kvs.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
// Copyright 2024 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package kvs

import (
"context"
"encoding/hex"
"errors"

"github.com/ethersphere/bee/pkg/file"
"github.com/ethersphere/bee/pkg/manifest"
Expand All @@ -19,6 +24,7 @@ type KeyValueStore interface {
type keyValueStore struct {
manifest manifest.Interface
putter storer.PutterSession
putCnt int
}

var _ KeyValueStore = (*keyValueStore)(nil)
Expand All @@ -34,10 +40,18 @@ func (s *keyValueStore) Get(key []byte) ([]byte, error) {
}

func (s *keyValueStore) Put(key []byte, value []byte) error {
return s.manifest.Add(context.Background(), hex.EncodeToString(key), manifest.NewEntry(swarm.NewAddress(value), map[string]string{}))
err := s.manifest.Add(context.Background(), hex.EncodeToString(key), manifest.NewEntry(swarm.NewAddress(value), map[string]string{}))
if err != nil {
return err
}
s.putCnt++
return nil
}

func (s *keyValueStore) Save() (swarm.Address, error) {
if s.putCnt == 0 {
return swarm.ZeroAddress, errors.New("nothing to save")
}
ref, err := s.manifest.Store(context.Background())
if err != nil {
return swarm.ZeroAddress, err
Expand All @@ -46,6 +60,7 @@ func (s *keyValueStore) Save() (swarm.Address, error) {
if err != nil {
return swarm.ZeroAddress, err
}
s.putCnt = 0
return ref, nil
}

Expand Down
221 changes: 129 additions & 92 deletions pkg/kvs/kvs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
package kvs_test

import (
"bytes"
"context"
"encoding/hex"
"testing"

"github.com/ethersphere/bee/pkg/file"
Expand All @@ -19,6 +17,7 @@ import (
"github.com/ethersphere/bee/pkg/storage"
mockstorer "github.com/ethersphere/bee/pkg/storer/mock"
"github.com/ethersphere/bee/pkg/swarm"
"github.com/stretchr/testify/assert"
)

var mockStorer = mockstorer.New()
Expand All @@ -37,96 +36,134 @@ func keyValuePair(t *testing.T) ([]byte, []byte) {
return swarm.RandAddress(t).Bytes(), swarm.RandAddress(t).Bytes()
}

func TestKvsAddLookup(t *testing.T) {
ls := createLs()

putter := mockStorer.DirectUpload()
s := kvs.New(ls, putter, swarm.ZeroAddress)

lookupKey, encryptedAccesskey := keyValuePair(t)

err := s.Put(lookupKey, encryptedAccesskey)
if err != nil {
t.Errorf("Add() should not return an error: %v", err)
}

key, err := s.Get(lookupKey)
if err != nil {
t.Errorf("Lookup() should not return an error: %v", err)
}

if !bytes.Equal(key, encryptedAccesskey) {
t.Errorf("Get() value is not the expected %s != %s", hex.EncodeToString(key), hex.EncodeToString(encryptedAccesskey))
}

}

func TestKvsAddLookupWithSave(t *testing.T) {
ls := createLs()
putter := mockStorer.DirectUpload()
s1 := kvs.New(ls, putter, swarm.ZeroAddress)
lookupKey, encryptedAccesskey := keyValuePair(t)

err := s1.Put(lookupKey, encryptedAccesskey)
if err != nil {
t.Fatalf("Add() should not return an error: %v", err)
}
ref, err := s1.Save()
if err != nil {
t.Fatalf("Save() should not return an error: %v", err)
}
s2 := kvs.New(ls, putter, ref)
key, err := s2.Get(lookupKey)
if err != nil {
t.Fatalf("Lookup() should not return an error: %v", err)
}

if !bytes.Equal(key, encryptedAccesskey) {
t.Errorf("Get() value is not the expected %s != %s", hex.EncodeToString(key), hex.EncodeToString(encryptedAccesskey))
}

func TestKvs(t *testing.T) {

s := kvs.New(createLs(), mockStorer.DirectUpload(), swarm.ZeroAddress)
key, val := keyValuePair(t)

t.Run("Get non-existent key should return error", func(t *testing.T) {
_, err := s.Get([]byte{1})
assert.Error(t, err)
})

t.Run("Multiple Get with same key, no error", func(t *testing.T) {
err := s.Put(key, val)
assert.NoError(t, err)

// get #1
v, err := s.Get(key)
assert.NoError(t, err)
assert.Equal(t, val, v)
// get #2
v, err = s.Get(key)
assert.NoError(t, err)
assert.Equal(t, val, v)
})

t.Run("Get should return value equal to put value", func(t *testing.T) {
var (
key1 []byte = []byte{1}
key2 []byte = []byte{2}
key3 []byte = []byte{3}
)
testCases := []struct {
name string
key []byte
val []byte
}{
{
name: "Test key = 1",
key: key1,
val: []byte{11},
},
{
name: "Test key = 2",
key: key2,
val: []byte{22},
},
{
name: "Test overwrite key = 1",
key: key1,
val: []byte{111},
},
{
name: "Test key = 3",
key: key3,
val: []byte{33},
},
{
name: "Test key = 3 with same value",
key: key3,
val: []byte{33},
},
{
name: "Test key = 3 with value for key1",
key: key3,
val: []byte{11},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := s.Put(tc.key, tc.val)
assert.NoError(t, err)
retVal, err := s.Get(tc.key)
assert.NoError(t, err)
assert.Equal(t, tc.val, retVal)
})
}
})
}

func TestKvsAddSaveAdd(t *testing.T) {
ls := createLs()
putter := mockStorer.DirectUpload()
kvs1 := kvs.New(ls, putter, swarm.ZeroAddress)
kvs1key1, kvs1val1 := keyValuePair(t)

err := kvs1.Put(kvs1key1, kvs1val1)
if err != nil {
t.Fatalf("Add() should not return an error: %v", err)
}
ref, err := kvs1.Save()
if err != nil {
t.Fatalf("Save() should not return an error: %v", err)
}

// New KVS
kvs2 := kvs.New(ls, putter, ref)

kvs2key1, kvs2val1 := keyValuePair(t)

// put after save
kvs2.Put(kvs2key1, kvs2val1)

// get after save
kvs2get1, err := kvs2.Get(kvs2key1)
if err != nil {
t.Fatalf("Lookup() should not return an error: %v", err)
}
if !bytes.Equal(kvs2get1, kvs2val1) {
t.Errorf("Get() value is not the expected %s != %s", hex.EncodeToString(kvs2key1), hex.EncodeToString(kvs2val1))
}

// get before Save
kvs2get2, err := kvs2.Get(kvs1key1)

if err != nil {
t.Fatalf("Lookup() should not return an error: %v", err)
}
if !bytes.Equal(kvs2get2, kvs1val1) {
t.Errorf("Get() value is not the expected %s != %s", hex.EncodeToString(kvs2get2), hex.EncodeToString(kvs1val1))
}

func TestKvs_Save(t *testing.T) {
key1, val1 := keyValuePair(t)
key2, val2 := keyValuePair(t)
t.Run("Save empty KVS return error", func(t *testing.T) {
s := kvs.New(createLs(), mockStorer.DirectUpload(), swarm.ZeroAddress)
_, err := s.Save()
assert.Error(t, err)
})
t.Run("Save not empty KVS return valid swarm address", func(t *testing.T) {
s := kvs.New(createLs(), mockStorer.DirectUpload(), swarm.ZeroAddress)
s.Put(key1, val1)
ref, err := s.Save()
assert.NoError(t, err)
assert.True(t, ref.IsValidNonEmpty())
})
t.Run("Save KVS with one item, no error, pre-save value exist", func(t *testing.T) {
ls := createLs()
putter := mockStorer.DirectUpload()
s1 := kvs.New(ls, putter, swarm.ZeroAddress)

err := s1.Put(key1, val1)
assert.NoError(t, err)

ref, err := s1.Save()
assert.NoError(t, err)

s2 := kvs.New(ls, putter, ref)
val, err := s2.Get(key1)
assert.NoError(t, err)
assert.Equal(t, val1, val)
})
t.Run("Save KVS and add one item, no error, after-save value exist", func(t *testing.T) {
ls := createLs()
putter := mockStorer.DirectUpload()

kvs1 := kvs.New(ls, putter, swarm.ZeroAddress)

err := kvs1.Put(key1, val1)
assert.NoError(t, err)
ref, err := kvs1.Save()
assert.NoError(t, err)

// New KVS
kvs2 := kvs.New(ls, putter, ref)
err = kvs2.Put(key2, val2)
assert.NoError(t, err)

val, err := kvs2.Get(key2)
assert.NoError(t, err)
assert.Equal(t, val2, val)
})
}

0 comments on commit fee9296

Please sign in to comment.