Skip to content

Commit

Permalink
blockchain: Consistency pass on subscribe code.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
davecgh committed Aug 15, 2017
1 parent c5c4637 commit 3b5c2ba
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
1 change: 0 additions & 1 deletion blockchain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1380,7 +1380,6 @@ func New(config *Config) (*BlockChain, error) {
db: config.DB,
chainParams: params,
timeSource: config.TimeSource,
notifications: make([]NotificationCallback, 0),
sigCache: config.SigCache,
indexManager: config.IndexManager,
minRetargetTimespan: targetTimespan / adjustmentFactor,
Expand Down
5 changes: 2 additions & 3 deletions blockchain/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,11 @@ func (b *BlockChain) Subscribe(callback NotificationCallback) {
// caller requested notifications by providing a callback function in the call
// to New.
func (b *BlockChain) sendNotification(typ NotificationType, data interface{}) {
b.notificationsLock.RLock()
defer b.notificationsLock.RUnlock()

// Generate and send the notification.
n := Notification{Type: typ, Data: data}
b.notificationsLock.RLock()
for _, callback := range b.notifications {
callback(&n)
}
b.notificationsLock.RUnlock()
}
23 changes: 12 additions & 11 deletions blockchain/notifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,42 @@ import (
"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) {
blocks, err := loadBlocks("blk_0_to_4.dat.bz2")
if err != nil {
t.Fatalf("Error loading file: %v\n", err)
}

// Create a new database and chain instance to run tests against.
chain, teardownFunc, err :=
chainSetup("notifications", &chaincfg.MainNetParams)
chain, teardownFunc, err := chainSetup("notifications",
&chaincfg.MainNetParams)
if err != nil {
t.Fatalf("Failed to setup chain instance: %v", err)
}
defer teardownFunc()

notificationCount := 0

callback := func(notification *blockchain.Notification) {
if notification.Type == blockchain.NTBlockAccepted {
notificationCount++
}
}

// Register callback 3 times then assert it is called three times
chain.Subscribe(callback)
chain.Subscribe(callback)
chain.Subscribe(callback)
// Register callback multiple times then assert it is called that many
// times.
const numSubscribers = 3
for i := 0; i < numSubscribers; i++ {
chain.Subscribe(callback)
}

_, _, err = chain.ProcessBlock(blocks[1], blockchain.BFNone)
if err != nil {
t.Fatalf("ProcessBlock fail on block 1: %v\n", err)
}

if notificationCount != 3 {
t.Fatalf("Expected notification callback to be executed 3 times, found %d",
notificationCount)
if notificationCount != numSubscribers {
t.Fatalf("Expected notification callback to be executed %d "+
"times, found %d", numSubscribers, notificationCount)
}
}

0 comments on commit 3b5c2ba

Please sign in to comment.