Skip to content

Commit

Permalink
fix: make buffered subscribe method in 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 a96cdc6 commit 59a01ca
Show file tree
Hide file tree
Showing 2 changed files with 25 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)
go func() {
defer close(testch)
for evt := range ch {
testch <- evt
}
}()

return testch
}

// -------------------------------------------------------------------------------
Expand Down
20 changes: 14 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 @@ -1451,6 +1450,7 @@ func TestStartNextHeightCorrectly(t *testing.T) {

ensureNewRound(newRoundCh, height, round)
ensureNewProposal(proposalCh, height, round)

rs := cs1.GetRoundState()
theBlockHash := rs.ProposalBlock.Hash()
theBlockParts := rs.ProposalBlockParts.Header()
Expand Down Expand Up @@ -1480,6 +1480,7 @@ func TestStartNextHeightCorrectly(t *testing.T) {
height, round = height+1, 0
ensureNewRound(newRoundCh, height, round)
ensureNewTimeout(timeoutProposeCh, height, round, cs1.config.Propose(round).Nanoseconds())

rs = cs1.GetRoundState()
assert.False(t, rs.TriggeredTimeoutPrecommit, "triggeredTimeoutPrecommit should be false at the beginning of each round")
}
Expand Down Expand Up @@ -1781,11 +1782,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 59a01ca

Please sign in to comment.