-
Notifications
You must be signed in to change notification settings - Fork 324
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
Changes from 3 commits
2977e1c
5d07d3f
9d01cc5
3f36394
60d96dd
47bff0a
1bb6f41
4383fb5
f94a64a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ import ( | |
"encoding/hex" | ||
"fmt" | ||
"math/big" | ||
"sync" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
|
@@ -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))) | ||
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) { | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use waitutil instead of sleep in the test There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done, thanks |
||
require.NoError(bc.Stop(ctx)) | ||
require.Equal(24, ms.Counter()) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this change is not necessary? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i mean, no need to define
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
elem.pendingBlksBuffer <- blk | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good fix