Skip to content

Commit

Permalink
x/gov/keeper: fix flaky TestPaginatedVotesQuery (backport #9223) (#9225)
Browse files Browse the repository at this point in the history
* x/gov/keeper: fix flaky TestPaginatedVotesQuery (#9223)

When testing with -race, sometimes the random source generate the same
string for consecutive calls, causing duplicated voter address. So the
number of votes in DB is not 20.

To fix this, we ensure unique addresses are generated, by using a map
for tracking which one was produced, and skip the duplicated address and
generated new one. Testing with:

	go test -race -v -count=1000 -run=TestPaginatedVotesQuery

now passes.

Updates #9010

(cherry picked from commit 6ad84c5)

# Conflicts:
#	x/gov/keeper/querier_test.go

* fi merge conflict

Co-authored-by: Cuong Manh Le <[email protected]>
Co-authored-by: Amaury M <[email protected]>
  • Loading branch information
3 people authored Apr 29, 2021
1 parent e0ad579 commit 75f84b5
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions x/gov/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,13 +316,23 @@ func TestPaginatedVotesQuery(t *testing.T) {
app.GovKeeper.SetProposal(ctx, proposal)

votes := make([]types.Vote, 20)
rand := rand.New(rand.NewSource(time.Now().UnixNano()))
addr := make(sdk.AccAddress, 20)
random := rand.New(rand.NewSource(time.Now().UnixNano()))
addrMap := make(map[string]struct{})
genAddr := func() string {
addr := make(sdk.AccAddress, 20)
for {
random.Read(addr)
addrStr := addr.String()
if _, ok := addrMap[addrStr]; !ok {
addrMap[addrStr] = struct{}{}
return addrStr
}
}
}
for i := range votes {
rand.Read(addr)
vote := types.Vote{
ProposalId: proposal.ProposalId,
Voter: addr.String(),
Voter: genAddr(),
Option: types.OptionYes,
}
votes[i] = vote
Expand Down

0 comments on commit 75f84b5

Please sign in to comment.