Skip to content

unit test for block preparer #4613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 7 additions & 3 deletions state/factory/blockpreparer.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@ func (d *blockPreparer) PrepareOrWait(ctx context.Context, prevHash []byte, time
}

d.mu.Lock()
res := d.results[hash.Hash256(prevHash)][timestamp.UnixNano()]
d.mu.Unlock()
return res.blk, res.err
defer d.mu.Unlock()
if blks, ok := d.results[hash.BytesToHash256(prevHash)]; ok && blks[timestamp.UnixNano()] != nil {
res := blks[timestamp.UnixNano()]
return res.blk, res.err
}
return nil, errors.New("mint result not found")
}

func (d *blockPreparer) prepare(prevHash []byte, timestamp time.Time, mintFn func() (*block.Block, error)) chan struct{} {
Expand Down Expand Up @@ -80,6 +83,7 @@ func (d *blockPreparer) prepare(prevHash []byte, timestamp time.Time, mintFn fun
func (d *blockPreparer) ReceiveBlock(blk *block.Block) error {
d.mu.Lock()
delete(d.tasks, blk.PrevHash())
delete(d.results, blk.PrevHash())
Copy link
Member

Choose a reason for hiding this comment

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

also cleanup results

d.mu.Unlock()
return nil
}
113 changes: 113 additions & 0 deletions state/factory/blockpreparer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package factory

import (
"context"
"sync"
"testing"
"time"

"github.com/iotexproject/go-pkgs/hash"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"

"github.com/iotexproject/iotex-core/v2/action"
"github.com/iotexproject/iotex-core/v2/blockchain/block"
"github.com/iotexproject/iotex-core/v2/test/identityset"
)

func TestBlockPreparer_PrepareOrWait(t *testing.T) {
preparer := newBlockPreparer()
prevHash := hash.Hash256b([]byte("previousHash"))
timestamp := time.Now()
mockBlk := &block.Block{}
called := false

// Mock mint function
mintFn := func() (*block.Block, error) {
if called {
return nil, errors.New("block already minted")
}
called = true
return mockBlk, nil
}
mintFn2 := func() (*block.Block, error) {
return &block.Block{
Body: block.Body{
Actions: []*action.SealedEnvelope{},
},
}, nil
}

ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

wg := sync.WaitGroup{}
wg.Add(2)
go func() {
blk1, err := preparer.PrepareOrWait(ctx, prevHash[:], timestamp, mintFn)
require.NoError(t, err)
require.Equal(t, mockBlk, blk1)
wg.Done()
}()
go func() {
blk2, err := preparer.PrepareOrWait(ctx, prevHash[:], timestamp.Add(time.Second), mintFn2)
require.NoError(t, err)
require.NotEqual(t, mockBlk, blk2)
wg.Done()
}()
blk, err := preparer.PrepareOrWait(ctx, prevHash[:], timestamp, mintFn)
require.NoError(t, err)
require.Equal(t, mockBlk, blk)
wg.Wait()
}

func TestBlockPreparer_PrepareOrWait_Timeout(t *testing.T) {
preparer := newBlockPreparer()
prevHash := hash.Hash256b([]byte("previousHash"))
timestamp := time.Now()

// Mock mint function that takes too long
mintFn := func() (*block.Block, error) {
time.Sleep(2 * time.Second)
return &block.Block{}, nil
}

ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

blk, err := preparer.PrepareOrWait(ctx, prevHash[:], timestamp, mintFn)
require.Error(t, err)
require.Nil(t, blk)
}

func TestBlockPreparer_ReceiveBlock(t *testing.T) {
preparer := newBlockPreparer()
prevHash := hash.Hash256b([]byte("previousHash"))
timestamp := time.Now()

// Mock mint function
mintFn := func() (*block.Block, error) {
builder := &block.TestingBuilder{}
blk, err := builder.SetPrevBlockHash(prevHash).SignAndBuild(identityset.PrivateKey(0))
return &blk, err
}

ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

blk, err := preparer.PrepareOrWait(ctx, prevHash[:], timestamp, mintFn)
require.NoError(t, err)

emptyblk := &block.Block{}
require.NoError(t, preparer.ReceiveBlock(emptyblk))
_, ok := preparer.tasks[prevHash]
require.True(t, ok)
_, ok = preparer.results[prevHash]
require.True(t, ok)

require.NoError(t, preparer.ReceiveBlock(blk))
_, ok = preparer.tasks[prevHash]
require.False(t, ok)
_, ok = preparer.results[prevHash]
require.False(t, ok)
}