Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 7 additions & 1 deletion dataRetriever/resolvers/baseFullHistoryResolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ type baseFullHistoryResolver struct {

func (bfhr *baseFullHistoryResolver) getFromStorage(key []byte, epoch uint32) ([]byte, error) {
//we just call the storer to search in the provided epoch. (it will search automatically also in the next epoch)
return bfhr.storer.GetFromEpoch(key, epoch)
buff, err := bfhr.storer.GetFromEpoch(key, epoch)
if err != nil {
// default to a search first, maximize the chance of getting recent data
return bfhr.storer.SearchFirst(key)
}

return buff, err
}

func (bfhr *baseFullHistoryResolver) searchFirst(key []byte) ([]byte, error) {
Expand Down
101 changes: 101 additions & 0 deletions dataRetriever/resolvers/baseFullHistoryResolver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package resolvers

import (
"errors"
"testing"

"github.com/multiversx/mx-chain-go/testscommon/storage"
"github.com/stretchr/testify/assert"
)

func TestBaseFullHistoryResolver_SearchFirst(t *testing.T) {
t.Parallel()

testKey := []byte("key")
testValue := []byte("value")
resolver := &baseFullHistoryResolver{
storer: &storage.StorerStub{
SearchFirstCalled: func(key []byte) ([]byte, error) {
assert.Equal(t, testKey, key)

return testValue, nil
},
},
}

val, err := resolver.searchFirst(testKey)
assert.Nil(t, err)
assert.Equal(t, testValue, val)
}

func TestBaseFullHistoryResolver_GetFromStorage(t *testing.T) {
t.Parallel()

testKey := []byte("key")
testValue := []byte("value")
testEpoch := uint32(37)

t.Run("get from epoch returned nil error and not empty buffer", func(t *testing.T) {
resolver := &baseFullHistoryResolver{
storer: &storage.StorerStub{
SearchFirstCalled: func(key []byte) ([]byte, error) {
assert.Fail(t, "should have not called SearchFirst")

return nil, nil
},
GetFromEpochCalled: func(key []byte, epoch uint32) ([]byte, error) {
assert.Equal(t, testKey, key)
assert.Equal(t, testEpoch, epoch)

return testValue, nil
},
},
}

val, err := resolver.getFromStorage(testKey, testEpoch)
assert.Nil(t, err)
assert.Equal(t, testValue, val)
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting / newline needed (and below).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generally prefer inner tests not separated by a new line. We should agree in a future clean code meeting if we will add empty lines or not in this particular case as to slowly align all existing code

t.Run("get from epoch returned nil error and nil buffer", func(t *testing.T) {
resolver := &baseFullHistoryResolver{
storer: &storage.StorerStub{
SearchFirstCalled: func(key []byte) ([]byte, error) {
assert.Fail(t, "should have not called SearchFirst")

return nil, nil
},
GetFromEpochCalled: func(key []byte, epoch uint32) ([]byte, error) {
assert.Equal(t, testKey, key)
assert.Equal(t, testEpoch, epoch)

return nil, nil
},
},
}

val, err := resolver.getFromStorage(testKey, testEpoch)
assert.Nil(t, err)
assert.Equal(t, 0, len(val))
})
t.Run("get from epoch returned error will fallback to search first", func(t *testing.T) {
resolver := &baseFullHistoryResolver{
storer: &storage.StorerStub{
SearchFirstCalled: func(key []byte) ([]byte, error) {
assert.Equal(t, testKey, key)

return testValue, nil
},
GetFromEpochCalled: func(key []byte, epoch uint32) ([]byte, error) {
assert.Equal(t, testKey, key)
assert.Equal(t, testEpoch, epoch)

return nil, errors.New("not found")
},
},
}

val, err := resolver.getFromStorage(testKey, testEpoch)
assert.Nil(t, err)
assert.Equal(t, testValue, val)
})
}