From 3f70ac2a35c33cb0e3f1aaca6f4c7d908502e986 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Thu, 29 Apr 2021 00:08:42 +0700 Subject: [PATCH] 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 6ad84c5065ad50d6a2f1a2ee4f781d3fe68acf61) # Conflicts: # x/gov/keeper/querier_test.go --- x/gov/keeper/querier_test.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/x/gov/keeper/querier_test.go b/x/gov/keeper/querier_test.go index 9960fb116bed..5bb9130be2ec 100644 --- a/x/gov/keeper/querier_test.go +++ b/x/gov/keeper/querier_test.go @@ -316,14 +316,29 @@ 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, +<<<<<<< HEAD Voter: addr.String(), Option: types.OptionYes, +======= + Voter: genAddr(), + Options: types.NewNonSplitVoteOption(types.OptionYes), +>>>>>>> 6ad84c506... x/gov/keeper: fix flaky TestPaginatedVotesQuery (#9223) } votes[i] = vote app.GovKeeper.SetVote(ctx, vote)