Skip to content

Commit

Permalink
fix: use sized channel in subscribe methods in state tests
Browse files Browse the repository at this point in the history
Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com>
  • Loading branch information
gfanton committed Nov 20, 2023
1 parent d6acd41 commit ea0f308
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
12 changes: 11 additions & 1 deletion tm2/pkg/bft/consensus/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,24 @@ func validatePrevoteAndPrecommit(t *testing.T, cs *ConsensusState, thisRound, lo
}

func subscribeToVoter(cs *ConsensusState, addr crypto.Address) <-chan events.Event {
return events.SubscribeFiltered(cs.evsw, testSubscriber, func(event events.Event) bool {
ch := events.SubscribeFiltered(cs.evsw, testSubscriber, func(event events.Event) bool {
if vote, ok := event.(types.EventVote); ok {
if vote.Vote.ValidatorAddress == addr {
return true
}
}
return false
})

testch := make(chan events.Event, 16)
go func() {
defer close(testch)
for evt := range ch {
testch <- evt
}
}()

return testch
}

// -------------------------------------------------------------------------------
Expand Down
18 changes: 12 additions & 6 deletions tm2/pkg/bft/consensus/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"reflect"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -1781,11 +1780,18 @@ func TestStateOutputVoteStats(t *testing.T) {
}
}

var eventid uint32

func subscribe(evsw events.EventSwitch, protoevent events.Event) <-chan events.Event {
name := reflect.ValueOf(protoevent).Type().Name()
id := atomic.AddUint32(&eventid, 1)
listenerID := fmt.Sprintf("%s-%s-%d", testSubscriber, name, id)
return events.SubscribeToEvent(evsw, listenerID, protoevent)
listenerID := fmt.Sprintf("%s-%s", testSubscriber, name)
ch := events.SubscribeToEvent(evsw, listenerID, protoevent)

testch := make(chan events.Event, 16)
go func() {
defer close(testch)
for evt := range ch {
testch <- evt
}
}()

return testch
}

0 comments on commit ea0f308

Please sign in to comment.