Skip to content
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

[test] fix TestLoadBlockchainfromDB #3521

Merged
merged 9 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 6 additions & 9 deletions blockchain/integrity/integrity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"encoding/hex"
"fmt"
"math/big"
"sync"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -832,22 +832,17 @@ func TestBlockchain_MintNewBlock_PopAccount(t *testing.T) {
}

type MockSubscriber struct {
counter int
mu sync.RWMutex
counter int32
}

func (ms *MockSubscriber) ReceiveBlock(blk *block.Block) error {
ms.mu.Lock()
tsfs, _ := classifyActions(blk.Actions)
ms.counter += len(tsfs)
ms.mu.Unlock()
atomic.AddInt32(&ms.counter, int32(len(tsfs)))
Copy link
Member

Choose a reason for hiding this comment

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

good fix

return nil
}

func (ms *MockSubscriber) Counter() int {
ms.mu.RLock()
defer ms.mu.RUnlock()
return ms.counter
return int(atomic.LoadInt32(&ms.counter))
}

func TestConstantinople(t *testing.T) {
Expand Down Expand Up @@ -1142,6 +1137,8 @@ func TestLoadBlockchainfromDB(t *testing.T) {
height := bc.TipHeight()
fmt.Printf("Open blockchain pass, height = %d\n", height)
require.NoError(addTestingTsfBlocks(cfg, bc, dao, ap))
//make sure pubsub is completed
time.Sleep(time.Millisecond * 200)
Copy link
Member

Choose a reason for hiding this comment

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

use waitutil instead of sleep in the test

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done, thanks

require.NoError(bc.Stop(ctx))
require.Equal(24, ms.Counter())

Expand Down
7 changes: 4 additions & 3 deletions blockchain/pubsubmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ func (ps *pubSub) RemoveBlockListener(s BlockCreationSubscriber) error {

// SendBlockToSubscribers sends block to every subscriber by using buffer channel
func (ps *pubSub) SendBlockToSubscribers(blk *block.Block) {
ps.lock.Lock()
defer ps.lock.Unlock()
for _, elem := range ps.blocklisteners {
ps.lock.RLock()
defer ps.lock.RUnlock()
blocklisteners := ps.blocklisteners
for _, elem := range blocklisteners {
Copy link
Member

Choose a reason for hiding this comment

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

this change is not necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

RLock,RUnlock is better here

Copy link
Member

Choose a reason for hiding this comment

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

i mean, no need to define blocklisteners := ps.blocklisteners, use like before

for _, elem := range ps.blocklisteners

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

elem.pendingBlksBuffer <- blk
}
}
Expand Down