2017-08-11 22:17:54 +02:00
|
|
|
// Copyright (c) 2017 The btcsuite developers
|
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package blockchain_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/blockchain"
|
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
|
|
|
)
|
|
|
|
|
2017-08-15 04:48:23 +02:00
|
|
|
// TestNotifications ensures that notification callbacks are fired on events.
|
2017-08-11 22:17:54 +02:00
|
|
|
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.
|
2017-08-15 04:48:23 +02:00
|
|
|
chain, teardownFunc, err := chainSetup("notifications",
|
|
|
|
&chaincfg.MainNetParams)
|
2017-08-11 22:17:54 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to setup chain instance: %v", err)
|
|
|
|
}
|
|
|
|
defer teardownFunc()
|
|
|
|
|
2017-08-15 05:48:32 +02:00
|
|
|
notificationCount := 0
|
|
|
|
callback := func(notification *blockchain.Notification) {
|
|
|
|
if notification.Type == blockchain.NTBlockAccepted {
|
|
|
|
notificationCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-15 04:48:23 +02:00
|
|
|
// Register callback multiple times then assert it is called that many
|
|
|
|
// times.
|
|
|
|
const numSubscribers = 3
|
|
|
|
for i := 0; i < numSubscribers; i++ {
|
|
|
|
chain.Subscribe(callback)
|
|
|
|
}
|
2017-08-11 22:17:54 +02:00
|
|
|
|
|
|
|
_, _, err = chain.ProcessBlock(blocks[1], blockchain.BFNone)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ProcessBlock fail on block 1: %v\n", err)
|
|
|
|
}
|
|
|
|
|
2017-08-15 04:48:23 +02:00
|
|
|
if notificationCount != numSubscribers {
|
|
|
|
t.Fatalf("Expected notification callback to be executed %d "+
|
|
|
|
"times, found %d", numSubscribers, notificationCount)
|
2017-08-11 22:17:54 +02:00
|
|
|
}
|
|
|
|
}
|