mirror of
https://github.com/LBRYFoundation/lbcd.git
synced 2025-08-23 17:47:24 +00:00
blockchain: Consistency pass on subscribe code.
This takes care of a few minor nits on the recently merged subscribe code. In particular: - Avoid extra unnecessary allocation on notifications slice - Avoid defer overhead on notification mutex in sendNotifications - Make test function comment start with the name of the function per Effective Go guidelines - Use constant for number of subscribers in test - Don't exceed column 80 in test print
This commit is contained in:
parent
c5c46376ba
commit
3b5c2baa2e
3 changed files with 14 additions and 15 deletions
|
@ -1380,7 +1380,6 @@ func New(config *Config) (*BlockChain, error) {
|
||||||
db: config.DB,
|
db: config.DB,
|
||||||
chainParams: params,
|
chainParams: params,
|
||||||
timeSource: config.TimeSource,
|
timeSource: config.TimeSource,
|
||||||
notifications: make([]NotificationCallback, 0),
|
|
||||||
sigCache: config.SigCache,
|
sigCache: config.SigCache,
|
||||||
indexManager: config.IndexManager,
|
indexManager: config.IndexManager,
|
||||||
minRetargetTimespan: targetTimespan / adjustmentFactor,
|
minRetargetTimespan: targetTimespan / adjustmentFactor,
|
||||||
|
|
|
@ -71,12 +71,11 @@ func (b *BlockChain) Subscribe(callback NotificationCallback) {
|
||||||
// caller requested notifications by providing a callback function in the call
|
// caller requested notifications by providing a callback function in the call
|
||||||
// to New.
|
// to New.
|
||||||
func (b *BlockChain) sendNotification(typ NotificationType, data interface{}) {
|
func (b *BlockChain) sendNotification(typ NotificationType, data interface{}) {
|
||||||
b.notificationsLock.RLock()
|
|
||||||
defer b.notificationsLock.RUnlock()
|
|
||||||
|
|
||||||
// Generate and send the notification.
|
// Generate and send the notification.
|
||||||
n := Notification{Type: typ, Data: data}
|
n := Notification{Type: typ, Data: data}
|
||||||
|
b.notificationsLock.RLock()
|
||||||
for _, callback := range b.notifications {
|
for _, callback := range b.notifications {
|
||||||
callback(&n)
|
callback(&n)
|
||||||
}
|
}
|
||||||
|
b.notificationsLock.RUnlock()
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ import (
|
||||||
"github.com/btcsuite/btcd/chaincfg"
|
"github.com/btcsuite/btcd/chaincfg"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Test that notification callbacks are fired on events.
|
// TestNotifications ensures that notification callbacks are fired on events.
|
||||||
func TestNotifications(t *testing.T) {
|
func TestNotifications(t *testing.T) {
|
||||||
blocks, err := loadBlocks("blk_0_to_4.dat.bz2")
|
blocks, err := loadBlocks("blk_0_to_4.dat.bz2")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -19,33 +19,34 @@ func TestNotifications(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a new database and chain instance to run tests against.
|
// Create a new database and chain instance to run tests against.
|
||||||
chain, teardownFunc, err :=
|
chain, teardownFunc, err := chainSetup("notifications",
|
||||||
chainSetup("notifications", &chaincfg.MainNetParams)
|
&chaincfg.MainNetParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to setup chain instance: %v", err)
|
t.Fatalf("Failed to setup chain instance: %v", err)
|
||||||
}
|
}
|
||||||
defer teardownFunc()
|
defer teardownFunc()
|
||||||
|
|
||||||
notificationCount := 0
|
notificationCount := 0
|
||||||
|
|
||||||
callback := func(notification *blockchain.Notification) {
|
callback := func(notification *blockchain.Notification) {
|
||||||
if notification.Type == blockchain.NTBlockAccepted {
|
if notification.Type == blockchain.NTBlockAccepted {
|
||||||
notificationCount++
|
notificationCount++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register callback 3 times then assert it is called three times
|
// Register callback multiple times then assert it is called that many
|
||||||
chain.Subscribe(callback)
|
// times.
|
||||||
chain.Subscribe(callback)
|
const numSubscribers = 3
|
||||||
chain.Subscribe(callback)
|
for i := 0; i < numSubscribers; i++ {
|
||||||
|
chain.Subscribe(callback)
|
||||||
|
}
|
||||||
|
|
||||||
_, _, err = chain.ProcessBlock(blocks[1], blockchain.BFNone)
|
_, _, err = chain.ProcessBlock(blocks[1], blockchain.BFNone)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("ProcessBlock fail on block 1: %v\n", err)
|
t.Fatalf("ProcessBlock fail on block 1: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if notificationCount != 3 {
|
if notificationCount != numSubscribers {
|
||||||
t.Fatalf("Expected notification callback to be executed 3 times, found %d",
|
t.Fatalf("Expected notification callback to be executed %d "+
|
||||||
notificationCount)
|
"times, found %d", numSubscribers, notificationCount)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue