notifications_test.go raw
1 package blockchain
2
3 import (
4 "testing"
5
6 "github.com/p9c/p9/pkg/chaincfg"
7 )
8
9 // TestNotifications ensures that notification callbacks are fired on events.
10 func TestNotifications(t *testing.T) {
11 blocks, e := loadBlocks("blk_0_to_4.dat.bz2")
12 if e != nil {
13 t.Fatalf("Error loading file: %v\n", e)
14 }
15 // Create a new database and chain instance to run tests against.
16 chain, teardownFunc, e := chainSetup("notifications",
17 &chaincfg.MainNetParams,
18 )
19 if e != nil {
20 t.Fatalf("Failed to setup chain instance: %v", e)
21 }
22 defer teardownFunc()
23 notificationCount := 0
24 callback := func(notification *Notification) {
25 if notification.Type == NTBlockAccepted {
26 notificationCount++
27 }
28 }
29 // Register callback multiple times then assert it is called that many times.
30 const numSubscribers = 3
31 for i := 0; i < numSubscribers; i++ {
32 chain.Subscribe(callback)
33 }
34 _, _, e = chain.ProcessBlock(0, blocks[1], BFNone, blocks[1].Height())
35 if e != nil {
36 t.Fatalf("ProcessBlock fail on block 1: %v\n", e)
37 }
38 if notificationCount != numSubscribers {
39 t.Fatalf("Expected notification callback to be executed %d "+
40 "times, found %d", numSubscribers, notificationCount,
41 )
42 }
43 }
44