mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-07-06 04:42:31 +00:00
blockchain: Fix race condition in notifications_test
This commit is contained in:
parent
b71d6c3010
commit
ca9baf77ec
@ -6,7 +6,6 @@ package blockchain_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/blockchain"
|
"github.com/btcsuite/btcd/blockchain"
|
||||||
"github.com/btcsuite/btcd/chaincfg"
|
"github.com/btcsuite/btcd/chaincfg"
|
||||||
@ -27,24 +26,26 @@ func TestNotifications(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer teardownFunc()
|
defer teardownFunc()
|
||||||
|
|
||||||
notifications := make(chan blockchain.Notification)
|
notificationCount := 0
|
||||||
chain.Subscribe(func(notification *blockchain.Notification) {
|
|
||||||
go func() {
|
callback := func(notification *blockchain.Notification) {
|
||||||
notifications <- *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)
|
||||||
|
|
||||||
_, _, 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
select {
|
if notificationCount != 3 {
|
||||||
case notification := <-notifications:
|
t.Fatalf("Expected notification callback to be executed 3 times, found %d",
|
||||||
if notification.Type != blockchain.NTBlockAccepted {
|
notificationCount)
|
||||||
t.Errorf("Expected NTBlockAccepted notification, got %v", notification.Type)
|
|
||||||
}
|
|
||||||
case <-time.After(time.Second):
|
|
||||||
t.Error("Expected blockchain notification callback to fire")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user