Skip to content

Commit

Permalink
fix(pkg/finality-grandpa): fix tests by closing network goroutines (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
timwu20 authored Nov 6, 2024
1 parent eb1f1c1 commit 6a30228
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
23 changes: 19 additions & 4 deletions pkg/finality-grandpa/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,14 @@ type BroadcastNetwork[M, N any] struct {
senders []chan M
history []M
routing bool
wg sync.WaitGroup
}

func NewBroadcastNetwork[M, N any]() BroadcastNetwork[M, N] {
func NewBroadcastNetwork[M, N any]() *BroadcastNetwork[M, N] {
bn := BroadcastNetwork[M, N]{
receiver: make(chan M, 10000),
}
return bn
return &bn
}

func (bm *BroadcastNetwork[M, N]) SendMessage(message M) {
Expand All @@ -231,6 +232,7 @@ func (bm *BroadcastNetwork[M, N]) AddNode(f func(N) M, out chan N) (in chan M) {

if !bm.routing {
bm.routing = true
bm.wg.Add(1)
go bm.route()
}

Expand All @@ -243,6 +245,7 @@ func (bm *BroadcastNetwork[M, N]) AddNode(f func(N) M, out chan N) (in chan M) {
}

func (bm *BroadcastNetwork[M, N]) route() {
defer bm.wg.Done()
for msg := range bm.receiver {
bm.history = append(bm.history, msg)
for _, sender := range bm.senders {
Expand All @@ -251,8 +254,13 @@ func (bm *BroadcastNetwork[M, N]) route() {
}
}

func (bm *BroadcastNetwork[M, N]) Stop() {
close(bm.receiver)
bm.wg.Wait()
}

type RoundNetwork struct {
BroadcastNetwork[SignedMessageError[string, uint32, Signature, ID], Message[string, uint32]]
*BroadcastNetwork[SignedMessageError[string, uint32, Signature, ID], Message[string, uint32]]
}

func NewRoundNetwork() *RoundNetwork {
Expand All @@ -269,7 +277,7 @@ func (rn *RoundNetwork) AddNode(
}

type GlobalMessageNetwork struct {
BroadcastNetwork[globalInItem, CommunicationOut]
*BroadcastNetwork[globalInItem, CommunicationOut]
}

func NewGlobalMessageNetwork() *GlobalMessageNetwork {
Expand Down Expand Up @@ -299,6 +307,13 @@ func NewNetwork() *Network {
}
}

func (n *Network) Stop() {
for _, rn := range n.rounds {
rn.Stop()
}
n.globalMessages.Stop()
}

func (n *Network) MakeRoundComms(
roundNumber uint64,
nodeID ID,
Expand Down
10 changes: 10 additions & 0 deletions pkg/finality-grandpa/voter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestVoter_TalkingToMyself(t *testing.T) {
})

network := NewNetwork()
defer network.Stop()

env := newEnvironment(network, localID)

Expand Down Expand Up @@ -64,6 +65,7 @@ func TestVoter_FinalizingAtFaultThreshold(t *testing.T) {
voters := NewVoterSet(weights)

network := NewNetwork()
defer network.Stop()

var wg sync.WaitGroup
// 3 voters offline.
Expand Down Expand Up @@ -115,6 +117,7 @@ func TestVoter_ExposingVoterState(t *testing.T) {
voterSet := NewVoterSet(weights)

network := NewNetwork()
defer network.Stop()

var wg sync.WaitGroup
voters := make([]*Voter[string, uint32, Signature, ID], votersOnline)
Expand Down Expand Up @@ -204,6 +207,7 @@ func TestVoter_BroadcastCommit(t *testing.T) {
voterSet := NewVoterSet([]IDWeight[ID]{{localID, 100}})

network := NewNetwork()
defer network.Stop()

env := newEnvironment(network, localID)

Expand Down Expand Up @@ -243,6 +247,7 @@ func TestVoter_BroadcastCommitOnlyIfNewer(t *testing.T) {
voterSet := NewVoterSet([]IDWeight[ID]{{localID, 100}, {testID, 201}})

network := NewNetwork()
defer network.Stop()

commitsOut := make(chan CommunicationOut)
commitsIn := network.MakeGlobalComms(commitsOut)
Expand Down Expand Up @@ -345,6 +350,8 @@ func TestVoter_ImportCommitForAnyRound(t *testing.T) {
voterSet := NewVoterSet([]IDWeight[ID]{{localID, 100}, {testID, 201}})

network := NewNetwork()
defer network.Stop()

commitsOut := make(chan CommunicationOut)
_ = network.MakeGlobalComms(commitsOut)

Expand Down Expand Up @@ -414,6 +421,7 @@ func TestVoter_SkipsToLatestRoundAfterCatchUp(t *testing.T) {
thresholdWeight := voterSet.Threshold()

network := NewNetwork()
defer network.Stop()

// initialize unsynced voter at round 0
localID := ID(4)
Expand Down Expand Up @@ -528,6 +536,7 @@ func TestVoter_PickUpFromPriorWithoutGrandparentState(t *testing.T) {
voterSet := NewVoterSet([]IDWeight[ID]{{localID, 100}})

network := NewNetwork()
defer network.Stop()

env := newEnvironment(network, localID)

Expand Down Expand Up @@ -571,6 +580,7 @@ func TestVoter_PickUpFromPriorWithGrandparentStatus(t *testing.T) {
voterSet := NewVoterSet(weights)

network := NewNetwork()
defer network.Stop()

env := newEnvironment(network, localID)

Expand Down

0 comments on commit 6a30228

Please sign in to comment.