Skip to content

Commit

Permalink
added unit test blocks pool
Browse files Browse the repository at this point in the history
  • Loading branch information
ssd04 committed Mar 22, 2024
1 parent bb4c05f commit 205614c
Show file tree
Hide file tree
Showing 5 changed files with 256 additions and 3 deletions.
12 changes: 10 additions & 2 deletions process/blocksPool.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"sync"

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-core-go/data/outport"
"github.com/multiversx/mx-chain-core-go/marshal"
)
Expand All @@ -28,6 +29,13 @@ func NewBlocksPool(
maxDelta uint64,
cleanupInterval uint64,
) (*blocksPool, error) {
if check.IfNil(storer) {
return nil, ErrNilPruningStorer
}
if check.IfNil(marshaller) {
return nil, ErrNilMarshaller
}

bp := &blocksPool{
storer: storer,
marshaller: marshaller,
Expand Down Expand Up @@ -84,7 +92,7 @@ func (bp *blocksPool) PutBlock(hash []byte, outportBlock *outport.OutportBlock,
}

if round == 0 {
bp.putOutportBlock(hash, outportBlock, currentRound)
return bp.putOutportBlock(hash, outportBlock, currentRound)
}

metaRound := bp.roundsMap[core.MetachainShardId]
Expand Down Expand Up @@ -136,7 +144,7 @@ func (bp *blocksPool) putOutportBlock(
func (bp *blocksPool) GetBlock(hash []byte) (*outport.OutportBlock, error) {
data, err := bp.storer.Get(hash)
if err != nil {
return nil, fmt.Errorf("failed to get data from pool")
return nil, err
}

outportBlock := &outport.OutportBlock{}
Expand Down
204 changes: 203 additions & 1 deletion process/blocksPool_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,205 @@
package process_test

// TODO: add tests
import (
"errors"
"testing"

"github.com/multiversx/mx-chain-core-go/data/outport"
"github.com/multiversx/mx-chain-ws-connector-template-go/process"
"github.com/multiversx/mx-chain-ws-connector-template-go/testscommon"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type testStruct struct {

Check failure on line 14 in process/blocksPool_test.go

View workflow job for this annotation

GitHub Actions / golangci linter

`testStruct` is unused (deadcode)
s string
a, b int
}

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

t.Run("nil pruning storer", func(t *testing.T) {
t.Parallel()

bp, err := process.NewBlocksPool(
nil,
&testscommon.MarshallerMock{},
3,
10,
100,
)
require.Nil(t, bp)
require.Equal(t, process.ErrNilPruningStorer, err)
})

t.Run("nil marshaller", func(t *testing.T) {
t.Parallel()

bp, err := process.NewBlocksPool(
&testscommon.PruningStorerStub{},
nil,
3,
10,
100,
)
require.Nil(t, bp)
require.Equal(t, process.ErrNilMarshaller, err)
})

t.Run("should work", func(t *testing.T) {
t.Parallel()

bp, err := process.NewBlocksPool(
&testscommon.PruningStorerStub{},
&testscommon.MarshallerMock{},
3,
10,
100,
)
require.Nil(t, err)
require.False(t, bp.IsInterfaceNil())
})
}

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

t.Run("failed to get data from storer", func(t *testing.T) {
t.Parallel()

expectedErr := errors.New("expected error")
bp, _ := process.NewBlocksPool(
&testscommon.PruningStorerStub{
GetCalled: func(key []byte) ([]byte, error) {
return nil, expectedErr
},
},
protoMarshaller,
3,
10,
100,
)

ret, err := bp.GetBlock([]byte("hash1"))
require.Nil(t, ret)
require.Equal(t, expectedErr, err)
})

t.Run("should work", func(t *testing.T) {
t.Parallel()

outportBlock := &outport.OutportBlock{
ShardID: 1,
NotarizedHeadersHashes: []string{"hash1", "hash2"},
NumberOfShards: 3,
}
outportBlockBytes, _ := protoMarshaller.Marshal(outportBlock)

bp, _ := process.NewBlocksPool(
&testscommon.PruningStorerStub{
GetCalled: func(key []byte) ([]byte, error) {
return outportBlockBytes, nil
},
},
protoMarshaller,
3,
10,
100,
)

ret, err := bp.GetBlock([]byte("hash1"))
require.Nil(t, err)
require.Equal(t, outportBlock, ret)
})
}

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

t.Run("should not trigger prune if not cleanup interval", func(t *testing.T) {
t.Parallel()

cleanupInterval := uint64(100)

bp, _ := process.NewBlocksPool(
&testscommon.PruningStorerStub{
PruneCalled: func(index uint64) error {
assert.Fail(t, "should have not been called")

return nil
},
},
protoMarshaller,
3,
100,
cleanupInterval,
)

bp.UpdateMetaState(2)
})

t.Run("should trigger prune if cleanup interval", func(t *testing.T) {
t.Parallel()

cleanupInterval := uint64(100)

wasCalled := false
bp, _ := process.NewBlocksPool(
&testscommon.PruningStorerStub{
PruneCalled: func(index uint64) error {
wasCalled = true

return nil
},
},
protoMarshaller,
3,
100,
cleanupInterval,
)

bp.UpdateMetaState(100)

require.True(t, wasCalled)
})
}

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

t.Run("first put, should put directly", func(t *testing.T) {
t.Parallel()

maxDelta := uint64(10)

wasCalled := false
bp, _ := process.NewBlocksPool(
&testscommon.PruningStorerStub{
PutCalled: func(key, data []byte) error {
wasCalled = true

return nil
},
},
protoMarshaller,
3,
maxDelta,
100,
)

err := bp.PutBlock([]byte("hash1"), &outport.OutportBlock{}, 2)
require.Nil(t, err)

require.True(t, wasCalled)

bp.UpdateMetaState(2)
require.Nil(t, err)

err = bp.PutBlock([]byte("hash2"), &outport.OutportBlock{}, 2+maxDelta+1)
require.Nil(t, err)

err = bp.PutBlock([]byte("hash2"), &outport.OutportBlock{}, 2+maxDelta+2)
require.Error(t, err)
})
}
3 changes: 3 additions & 0 deletions process/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ var ErrInvalidNumberOfPersisters = errors.New("invalid number of persisters")

// ErrInvalidFilePath signals that an invalid file path has been provided
var ErrInvalidFilePath = errors.New("invalid file path provided")

// ErrNilPruningStorer signals that a nil pruning storer was provide
var ErrNilPruningStorer = errors.New("nil pruning storer")
40 changes: 40 additions & 0 deletions testscommon/pruningStorerStub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package testscommon

// PruningStorerStub -
type PruningStorerStub struct {
GetCalled func(key []byte) ([]byte, error)
PutCalled func(key []byte, data []byte) error
PruneCalled func(index uint64) error
}

// Get -
func (p *PruningStorerStub) Get(key []byte) ([]byte, error) {
if p.GetCalled != nil {
return p.GetCalled(key)
}

return nil, nil
}

// Put -
func (p *PruningStorerStub) Put(key []byte, data []byte) error {
if p.PutCalled != nil {
return p.PutCalled(key, data)
}

return nil
}

// Prune -
func (p *PruningStorerStub) Prune(index uint64) error {
if p.PruneCalled != nil {
return p.PruneCalled(index)
}

return nil
}

// IsInterfaceNil -
func (p *PruningStorerStub) IsInterfaceNil() bool {
return p == nil
}
File renamed without changes.

0 comments on commit 205614c

Please sign in to comment.